xref: /openbmc/linux/fs/btrfs/backref.c (revision 00142756e1f8015d2f8ce96532d156689db7e448)
1 /*
2  * Copyright (C) 2011 STRATO.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18 
19 #include <linux/mm.h>
20 #include <linux/rbtree.h>
21 #include <trace/events/btrfs.h>
22 #include "ctree.h"
23 #include "disk-io.h"
24 #include "backref.h"
25 #include "ulist.h"
26 #include "transaction.h"
27 #include "delayed-ref.h"
28 #include "locking.h"
29 
30 /* Just an arbitrary number so we can be sure this happened */
31 #define BACKREF_FOUND_SHARED 6
32 
33 struct extent_inode_elem {
34 	u64 inum;
35 	u64 offset;
36 	struct extent_inode_elem *next;
37 };
38 
39 static int check_extent_in_eb(const struct btrfs_key *key,
40 			      const struct extent_buffer *eb,
41 			      const struct btrfs_file_extent_item *fi,
42 			      u64 extent_item_pos,
43 			      struct extent_inode_elem **eie)
44 {
45 	u64 offset = 0;
46 	struct extent_inode_elem *e;
47 
48 	if (!btrfs_file_extent_compression(eb, fi) &&
49 	    !btrfs_file_extent_encryption(eb, fi) &&
50 	    !btrfs_file_extent_other_encoding(eb, fi)) {
51 		u64 data_offset;
52 		u64 data_len;
53 
54 		data_offset = btrfs_file_extent_offset(eb, fi);
55 		data_len = btrfs_file_extent_num_bytes(eb, fi);
56 
57 		if (extent_item_pos < data_offset ||
58 		    extent_item_pos >= data_offset + data_len)
59 			return 1;
60 		offset = extent_item_pos - data_offset;
61 	}
62 
63 	e = kmalloc(sizeof(*e), GFP_NOFS);
64 	if (!e)
65 		return -ENOMEM;
66 
67 	e->next = *eie;
68 	e->inum = key->objectid;
69 	e->offset = key->offset + offset;
70 	*eie = e;
71 
72 	return 0;
73 }
74 
75 static void free_inode_elem_list(struct extent_inode_elem *eie)
76 {
77 	struct extent_inode_elem *eie_next;
78 
79 	for (; eie; eie = eie_next) {
80 		eie_next = eie->next;
81 		kfree(eie);
82 	}
83 }
84 
85 static int find_extent_in_eb(const struct extent_buffer *eb,
86 			     u64 wanted_disk_byte, u64 extent_item_pos,
87 			     struct extent_inode_elem **eie)
88 {
89 	u64 disk_byte;
90 	struct btrfs_key key;
91 	struct btrfs_file_extent_item *fi;
92 	int slot;
93 	int nritems;
94 	int extent_type;
95 	int ret;
96 
97 	/*
98 	 * from the shared data ref, we only have the leaf but we need
99 	 * the key. thus, we must look into all items and see that we
100 	 * find one (some) with a reference to our extent item.
101 	 */
102 	nritems = btrfs_header_nritems(eb);
103 	for (slot = 0; slot < nritems; ++slot) {
104 		btrfs_item_key_to_cpu(eb, &key, slot);
105 		if (key.type != BTRFS_EXTENT_DATA_KEY)
106 			continue;
107 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
108 		extent_type = btrfs_file_extent_type(eb, fi);
109 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
110 			continue;
111 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
112 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
113 		if (disk_byte != wanted_disk_byte)
114 			continue;
115 
116 		ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
117 		if (ret < 0)
118 			return ret;
119 	}
120 
121 	return 0;
122 }
123 
124 struct preftree {
125 	struct rb_root root;
126 	unsigned int count;
127 };
128 
129 #define PREFTREE_INIT	{ .root = RB_ROOT, .count = 0 }
130 
131 struct preftrees {
132 	struct preftree direct;    /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
133 	struct preftree indirect;  /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
134 	struct preftree indirect_missing_keys;
135 };
136 
137 static struct kmem_cache *btrfs_prelim_ref_cache;
138 
139 int __init btrfs_prelim_ref_init(void)
140 {
141 	btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
142 					sizeof(struct prelim_ref),
143 					0,
144 					SLAB_MEM_SPREAD,
145 					NULL);
146 	if (!btrfs_prelim_ref_cache)
147 		return -ENOMEM;
148 	return 0;
149 }
150 
151 void btrfs_prelim_ref_exit(void)
152 {
153 	kmem_cache_destroy(btrfs_prelim_ref_cache);
154 }
155 
156 static void free_pref(struct prelim_ref *ref)
157 {
158 	kmem_cache_free(btrfs_prelim_ref_cache, ref);
159 }
160 
161 /*
162  * Return 0 when both refs are for the same block (and can be merged).
163  * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
164  * indicates a 'higher' block.
165  */
166 static int prelim_ref_compare(struct prelim_ref *ref1,
167 			      struct prelim_ref *ref2)
168 {
169 	if (ref1->level < ref2->level)
170 		return -1;
171 	if (ref1->level > ref2->level)
172 		return 1;
173 	if (ref1->root_id < ref2->root_id)
174 		return -1;
175 	if (ref1->root_id > ref2->root_id)
176 		return 1;
177 	if (ref1->key_for_search.type < ref2->key_for_search.type)
178 		return -1;
179 	if (ref1->key_for_search.type > ref2->key_for_search.type)
180 		return 1;
181 	if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
182 		return -1;
183 	if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
184 		return 1;
185 	if (ref1->key_for_search.offset < ref2->key_for_search.offset)
186 		return -1;
187 	if (ref1->key_for_search.offset > ref2->key_for_search.offset)
188 		return 1;
189 	if (ref1->parent < ref2->parent)
190 		return -1;
191 	if (ref1->parent > ref2->parent)
192 		return 1;
193 
194 	return 0;
195 }
196 
197 /*
198  * Add @newref to the @root rbtree, merging identical refs.
199  *
200  * Callers should assumed that newref has been freed after calling.
201  */
202 static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
203 			      struct preftree *preftree,
204 			      struct prelim_ref *newref)
205 {
206 	struct rb_root *root;
207 	struct rb_node **p;
208 	struct rb_node *parent = NULL;
209 	struct prelim_ref *ref;
210 	int result;
211 
212 	root = &preftree->root;
213 	p = &root->rb_node;
214 
215 	while (*p) {
216 		parent = *p;
217 		ref = rb_entry(parent, struct prelim_ref, rbnode);
218 		result = prelim_ref_compare(ref, newref);
219 		if (result < 0) {
220 			p = &(*p)->rb_left;
221 		} else if (result > 0) {
222 			p = &(*p)->rb_right;
223 		} else {
224 			/* Identical refs, merge them and free @newref */
225 			struct extent_inode_elem *eie = ref->inode_list;
226 
227 			while (eie && eie->next)
228 				eie = eie->next;
229 
230 			if (!eie)
231 				ref->inode_list = newref->inode_list;
232 			else
233 				eie->next = newref->inode_list;
234 			trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
235 						     preftree->count);
236 			ref->count += newref->count;
237 			free_pref(newref);
238 			return;
239 		}
240 	}
241 
242 	preftree->count++;
243 	trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
244 	rb_link_node(&newref->rbnode, parent, p);
245 	rb_insert_color(&newref->rbnode, root);
246 }
247 
248 /*
249  * Release the entire tree.  We don't care about internal consistency so
250  * just free everything and then reset the tree root.
251  */
252 static void prelim_release(struct preftree *preftree)
253 {
254 	struct prelim_ref *ref, *next_ref;
255 
256 	rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root,
257 					     rbnode)
258 		free_pref(ref);
259 
260 	preftree->root = RB_ROOT;
261 	preftree->count = 0;
262 }
263 
264 /*
265  * the rules for all callers of this function are:
266  * - obtaining the parent is the goal
267  * - if you add a key, you must know that it is a correct key
268  * - if you cannot add the parent or a correct key, then we will look into the
269  *   block later to set a correct key
270  *
271  * delayed refs
272  * ============
273  *        backref type | shared | indirect | shared | indirect
274  * information         |   tree |     tree |   data |     data
275  * --------------------+--------+----------+--------+----------
276  *      parent logical |    y   |     -    |    -   |     -
277  *      key to resolve |    -   |     y    |    y   |     y
278  *  tree block logical |    -   |     -    |    -   |     -
279  *  root for resolving |    y   |     y    |    y   |     y
280  *
281  * - column 1:       we've the parent -> done
282  * - column 2, 3, 4: we use the key to find the parent
283  *
284  * on disk refs (inline or keyed)
285  * ==============================
286  *        backref type | shared | indirect | shared | indirect
287  * information         |   tree |     tree |   data |     data
288  * --------------------+--------+----------+--------+----------
289  *      parent logical |    y   |     -    |    y   |     -
290  *      key to resolve |    -   |     -    |    -   |     y
291  *  tree block logical |    y   |     y    |    y   |     y
292  *  root for resolving |    -   |     y    |    y   |     y
293  *
294  * - column 1, 3: we've the parent -> done
295  * - column 2:    we take the first key from the block to find the parent
296  *                (see add_missing_keys)
297  * - column 4:    we use the key to find the parent
298  *
299  * additional information that's available but not required to find the parent
300  * block might help in merging entries to gain some speed.
301  */
302 static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
303 			  struct preftree *preftree, u64 root_id,
304 			  const struct btrfs_key *key, int level, u64 parent,
305 			  u64 wanted_disk_byte, int count, gfp_t gfp_mask)
306 {
307 	struct prelim_ref *ref;
308 
309 	if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
310 		return 0;
311 
312 	ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
313 	if (!ref)
314 		return -ENOMEM;
315 
316 	ref->root_id = root_id;
317 	if (key) {
318 		ref->key_for_search = *key;
319 		/*
320 		 * We can often find data backrefs with an offset that is too
321 		 * large (>= LLONG_MAX, maximum allowed file offset) due to
322 		 * underflows when subtracting a file's offset with the data
323 		 * offset of its corresponding extent data item. This can
324 		 * happen for example in the clone ioctl.
325 		 * So if we detect such case we set the search key's offset to
326 		 * zero to make sure we will find the matching file extent item
327 		 * at add_all_parents(), otherwise we will miss it because the
328 		 * offset taken form the backref is much larger then the offset
329 		 * of the file extent item. This can make us scan a very large
330 		 * number of file extent items, but at least it will not make
331 		 * us miss any.
332 		 * This is an ugly workaround for a behaviour that should have
333 		 * never existed, but it does and a fix for the clone ioctl
334 		 * would touch a lot of places, cause backwards incompatibility
335 		 * and would not fix the problem for extents cloned with older
336 		 * kernels.
337 		 */
338 		if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
339 		    ref->key_for_search.offset >= LLONG_MAX)
340 			ref->key_for_search.offset = 0;
341 	} else {
342 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
343 	}
344 
345 	ref->inode_list = NULL;
346 	ref->level = level;
347 	ref->count = count;
348 	ref->parent = parent;
349 	ref->wanted_disk_byte = wanted_disk_byte;
350 	prelim_ref_insert(fs_info, preftree, ref);
351 
352 	return 0;
353 }
354 
355 /* direct refs use root == 0, key == NULL */
356 static int add_direct_ref(const struct btrfs_fs_info *fs_info,
357 			  struct preftrees *preftrees, int level, u64 parent,
358 			  u64 wanted_disk_byte, int count, gfp_t gfp_mask)
359 {
360 	return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
361 			      parent, wanted_disk_byte, count, gfp_mask);
362 }
363 
364 /* indirect refs use parent == 0 */
365 static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
366 			    struct preftrees *preftrees, u64 root_id,
367 			    const struct btrfs_key *key, int level,
368 			    u64 wanted_disk_byte, int count, gfp_t gfp_mask)
369 {
370 	struct preftree *tree = &preftrees->indirect;
371 
372 	if (!key)
373 		tree = &preftrees->indirect_missing_keys;
374 	return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
375 			      wanted_disk_byte, count, gfp_mask);
376 }
377 
378 static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
379 			   struct ulist *parents, struct prelim_ref *ref,
380 			   int level, u64 time_seq, const u64 *extent_item_pos,
381 			   u64 total_refs)
382 {
383 	int ret = 0;
384 	int slot;
385 	struct extent_buffer *eb;
386 	struct btrfs_key key;
387 	struct btrfs_key *key_for_search = &ref->key_for_search;
388 	struct btrfs_file_extent_item *fi;
389 	struct extent_inode_elem *eie = NULL, *old = NULL;
390 	u64 disk_byte;
391 	u64 wanted_disk_byte = ref->wanted_disk_byte;
392 	u64 count = 0;
393 
394 	if (level != 0) {
395 		eb = path->nodes[level];
396 		ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
397 		if (ret < 0)
398 			return ret;
399 		return 0;
400 	}
401 
402 	/*
403 	 * We normally enter this function with the path already pointing to
404 	 * the first item to check. But sometimes, we may enter it with
405 	 * slot==nritems. In that case, go to the next leaf before we continue.
406 	 */
407 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
408 		if (time_seq == SEQ_LAST)
409 			ret = btrfs_next_leaf(root, path);
410 		else
411 			ret = btrfs_next_old_leaf(root, path, time_seq);
412 	}
413 
414 	while (!ret && count < total_refs) {
415 		eb = path->nodes[0];
416 		slot = path->slots[0];
417 
418 		btrfs_item_key_to_cpu(eb, &key, slot);
419 
420 		if (key.objectid != key_for_search->objectid ||
421 		    key.type != BTRFS_EXTENT_DATA_KEY)
422 			break;
423 
424 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
425 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
426 
427 		if (disk_byte == wanted_disk_byte) {
428 			eie = NULL;
429 			old = NULL;
430 			count++;
431 			if (extent_item_pos) {
432 				ret = check_extent_in_eb(&key, eb, fi,
433 						*extent_item_pos,
434 						&eie);
435 				if (ret < 0)
436 					break;
437 			}
438 			if (ret > 0)
439 				goto next;
440 			ret = ulist_add_merge_ptr(parents, eb->start,
441 						  eie, (void **)&old, GFP_NOFS);
442 			if (ret < 0)
443 				break;
444 			if (!ret && extent_item_pos) {
445 				while (old->next)
446 					old = old->next;
447 				old->next = eie;
448 			}
449 			eie = NULL;
450 		}
451 next:
452 		if (time_seq == SEQ_LAST)
453 			ret = btrfs_next_item(root, path);
454 		else
455 			ret = btrfs_next_old_item(root, path, time_seq);
456 	}
457 
458 	if (ret > 0)
459 		ret = 0;
460 	else if (ret < 0)
461 		free_inode_elem_list(eie);
462 	return ret;
463 }
464 
465 /*
466  * resolve an indirect backref in the form (root_id, key, level)
467  * to a logical address
468  */
469 static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
470 				struct btrfs_path *path, u64 time_seq,
471 				struct prelim_ref *ref, struct ulist *parents,
472 				const u64 *extent_item_pos, u64 total_refs)
473 {
474 	struct btrfs_root *root;
475 	struct btrfs_key root_key;
476 	struct extent_buffer *eb;
477 	int ret = 0;
478 	int root_level;
479 	int level = ref->level;
480 	int index;
481 
482 	root_key.objectid = ref->root_id;
483 	root_key.type = BTRFS_ROOT_ITEM_KEY;
484 	root_key.offset = (u64)-1;
485 
486 	index = srcu_read_lock(&fs_info->subvol_srcu);
487 
488 	root = btrfs_get_fs_root(fs_info, &root_key, false);
489 	if (IS_ERR(root)) {
490 		srcu_read_unlock(&fs_info->subvol_srcu, index);
491 		ret = PTR_ERR(root);
492 		goto out;
493 	}
494 
495 	if (btrfs_is_testing(fs_info)) {
496 		srcu_read_unlock(&fs_info->subvol_srcu, index);
497 		ret = -ENOENT;
498 		goto out;
499 	}
500 
501 	if (path->search_commit_root)
502 		root_level = btrfs_header_level(root->commit_root);
503 	else if (time_seq == SEQ_LAST)
504 		root_level = btrfs_header_level(root->node);
505 	else
506 		root_level = btrfs_old_root_level(root, time_seq);
507 
508 	if (root_level + 1 == level) {
509 		srcu_read_unlock(&fs_info->subvol_srcu, index);
510 		goto out;
511 	}
512 
513 	path->lowest_level = level;
514 	if (time_seq == SEQ_LAST)
515 		ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
516 					0, 0);
517 	else
518 		ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
519 					    time_seq);
520 
521 	/* root node has been locked, we can release @subvol_srcu safely here */
522 	srcu_read_unlock(&fs_info->subvol_srcu, index);
523 
524 	btrfs_debug(fs_info,
525 		"search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
526 		 ref->root_id, level, ref->count, ret,
527 		 ref->key_for_search.objectid, ref->key_for_search.type,
528 		 ref->key_for_search.offset);
529 	if (ret < 0)
530 		goto out;
531 
532 	eb = path->nodes[level];
533 	while (!eb) {
534 		if (WARN_ON(!level)) {
535 			ret = 1;
536 			goto out;
537 		}
538 		level--;
539 		eb = path->nodes[level];
540 	}
541 
542 	ret = add_all_parents(root, path, parents, ref, level, time_seq,
543 			      extent_item_pos, total_refs);
544 out:
545 	path->lowest_level = 0;
546 	btrfs_release_path(path);
547 	return ret;
548 }
549 
550 static struct extent_inode_elem *
551 unode_aux_to_inode_list(struct ulist_node *node)
552 {
553 	if (!node)
554 		return NULL;
555 	return (struct extent_inode_elem *)(uintptr_t)node->aux;
556 }
557 
558 /*
559  * We maintain three seperate rbtrees: one for direct refs, one for
560  * indirect refs which have a key, and one for indirect refs which do not
561  * have a key. Each tree does merge on insertion.
562  *
563  * Once all of the references are located, we iterate over the tree of
564  * indirect refs with missing keys. An appropriate key is located and
565  * the ref is moved onto the tree for indirect refs. After all missing
566  * keys are thus located, we iterate over the indirect ref tree, resolve
567  * each reference, and then insert the resolved reference onto the
568  * direct tree (merging there too).
569  *
570  * New backrefs (i.e., for parent nodes) are added to the appropriate
571  * rbtree as they are encountered. The new backrefs are subsequently
572  * resolved as above.
573  */
574 static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
575 				 struct btrfs_path *path, u64 time_seq,
576 				 struct preftrees *preftrees,
577 				 const u64 *extent_item_pos, u64 total_refs,
578 				 u64 root_objectid)
579 {
580 	int err;
581 	int ret = 0;
582 	struct ulist *parents;
583 	struct ulist_node *node;
584 	struct ulist_iterator uiter;
585 	struct rb_node *rnode;
586 
587 	parents = ulist_alloc(GFP_NOFS);
588 	if (!parents)
589 		return -ENOMEM;
590 
591 	/*
592 	 * We could trade memory usage for performance here by iterating
593 	 * the tree, allocating new refs for each insertion, and then
594 	 * freeing the entire indirect tree when we're done.  In some test
595 	 * cases, the tree can grow quite large (~200k objects).
596 	 */
597 	while ((rnode = rb_first(&preftrees->indirect.root))) {
598 		struct prelim_ref *ref;
599 
600 		ref = rb_entry(rnode, struct prelim_ref, rbnode);
601 		if (WARN(ref->parent,
602 			 "BUG: direct ref found in indirect tree")) {
603 			ret = -EINVAL;
604 			goto out;
605 		}
606 
607 		rb_erase(&ref->rbnode, &preftrees->indirect.root);
608 		preftrees->indirect.count--;
609 
610 		if (ref->count == 0) {
611 			free_pref(ref);
612 			continue;
613 		}
614 
615 		if (root_objectid && ref->root_id != root_objectid) {
616 			free_pref(ref);
617 			ret = BACKREF_FOUND_SHARED;
618 			goto out;
619 		}
620 		err = resolve_indirect_ref(fs_info, path, time_seq, ref,
621 					   parents, extent_item_pos,
622 					   total_refs);
623 		/*
624 		 * we can only tolerate ENOENT,otherwise,we should catch error
625 		 * and return directly.
626 		 */
627 		if (err == -ENOENT) {
628 			prelim_ref_insert(fs_info, &preftrees->direct, ref);
629 			continue;
630 		} else if (err) {
631 			free_pref(ref);
632 			ret = err;
633 			goto out;
634 		}
635 
636 		/* we put the first parent into the ref at hand */
637 		ULIST_ITER_INIT(&uiter);
638 		node = ulist_next(parents, &uiter);
639 		ref->parent = node ? node->val : 0;
640 		ref->inode_list = unode_aux_to_inode_list(node);
641 
642 		/* Add a prelim_ref(s) for any other parent(s). */
643 		while ((node = ulist_next(parents, &uiter))) {
644 			struct prelim_ref *new_ref;
645 
646 			new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
647 						   GFP_NOFS);
648 			if (!new_ref) {
649 				free_pref(ref);
650 				ret = -ENOMEM;
651 				goto out;
652 			}
653 			memcpy(new_ref, ref, sizeof(*ref));
654 			new_ref->parent = node->val;
655 			new_ref->inode_list = unode_aux_to_inode_list(node);
656 			prelim_ref_insert(fs_info, &preftrees->direct, new_ref);
657 		}
658 
659 		/* Now it's a direct ref, put it in the the direct tree */
660 		prelim_ref_insert(fs_info, &preftrees->direct, ref);
661 
662 		ulist_reinit(parents);
663 	}
664 out:
665 	ulist_free(parents);
666 	return ret;
667 }
668 
669 /*
670  * read tree blocks and add keys where required.
671  */
672 static int add_missing_keys(struct btrfs_fs_info *fs_info,
673 			    struct preftrees *preftrees)
674 {
675 	struct prelim_ref *ref;
676 	struct extent_buffer *eb;
677 	struct preftree *tree = &preftrees->indirect_missing_keys;
678 	struct rb_node *node;
679 
680 	while ((node = rb_first(&tree->root))) {
681 		ref = rb_entry(node, struct prelim_ref, rbnode);
682 		rb_erase(node, &tree->root);
683 
684 		BUG_ON(ref->parent);	/* should not be a direct ref */
685 		BUG_ON(ref->key_for_search.type);
686 		BUG_ON(!ref->wanted_disk_byte);
687 
688 		eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
689 		if (IS_ERR(eb)) {
690 			free_pref(ref);
691 			return PTR_ERR(eb);
692 		} else if (!extent_buffer_uptodate(eb)) {
693 			free_pref(ref);
694 			free_extent_buffer(eb);
695 			return -EIO;
696 		}
697 		btrfs_tree_read_lock(eb);
698 		if (btrfs_header_level(eb) == 0)
699 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
700 		else
701 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
702 		btrfs_tree_read_unlock(eb);
703 		free_extent_buffer(eb);
704 		prelim_ref_insert(fs_info, &preftrees->indirect, ref);
705 	}
706 	return 0;
707 }
708 
709 /*
710  * add all currently queued delayed refs from this head whose seq nr is
711  * smaller or equal that seq to the list
712  */
713 static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
714 			    struct btrfs_delayed_ref_head *head, u64 seq,
715 			    struct preftrees *preftrees, u64 *total_refs,
716 			    u64 inum)
717 {
718 	struct btrfs_delayed_ref_node *node;
719 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
720 	struct btrfs_key key;
721 	struct btrfs_key tmp_op_key;
722 	struct btrfs_key *op_key = NULL;
723 	int sgn;
724 	int ret = 0;
725 
726 	if (extent_op && extent_op->update_key) {
727 		btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
728 		op_key = &tmp_op_key;
729 	}
730 
731 	spin_lock(&head->lock);
732 	list_for_each_entry(node, &head->ref_list, list) {
733 		if (node->seq > seq)
734 			continue;
735 
736 		switch (node->action) {
737 		case BTRFS_ADD_DELAYED_EXTENT:
738 		case BTRFS_UPDATE_DELAYED_HEAD:
739 			WARN_ON(1);
740 			continue;
741 		case BTRFS_ADD_DELAYED_REF:
742 			sgn = 1;
743 			break;
744 		case BTRFS_DROP_DELAYED_REF:
745 			sgn = -1;
746 			break;
747 		default:
748 			BUG_ON(1);
749 		}
750 		*total_refs += (node->ref_mod * sgn);
751 		switch (node->type) {
752 		case BTRFS_TREE_BLOCK_REF_KEY: {
753 			/* NORMAL INDIRECT METADATA backref */
754 			struct btrfs_delayed_tree_ref *ref;
755 
756 			ref = btrfs_delayed_node_to_tree_ref(node);
757 			ret = add_indirect_ref(fs_info, preftrees, ref->root,
758 					       &tmp_op_key, ref->level + 1,
759 					       node->bytenr,
760 					       node->ref_mod * sgn,
761 					       GFP_ATOMIC);
762 			break;
763 		}
764 		case BTRFS_SHARED_BLOCK_REF_KEY: {
765 			/* SHARED DIRECT METADATA backref */
766 			struct btrfs_delayed_tree_ref *ref;
767 
768 			ref = btrfs_delayed_node_to_tree_ref(node);
769 
770 			ret = add_direct_ref(fs_info, preftrees,
771 					     ref->level + 1, ref->parent,
772 					     node->bytenr, node->ref_mod * sgn,
773 					     GFP_ATOMIC);
774 			break;
775 		}
776 		case BTRFS_EXTENT_DATA_REF_KEY: {
777 			/* NORMAL INDIRECT DATA backref */
778 			struct btrfs_delayed_data_ref *ref;
779 			ref = btrfs_delayed_node_to_data_ref(node);
780 
781 			key.objectid = ref->objectid;
782 			key.type = BTRFS_EXTENT_DATA_KEY;
783 			key.offset = ref->offset;
784 
785 			/*
786 			 * Found a inum that doesn't match our known inum, we
787 			 * know it's shared.
788 			 */
789 			if (inum && ref->objectid != inum) {
790 				ret = BACKREF_FOUND_SHARED;
791 				break;
792 			}
793 
794 			ret = add_indirect_ref(fs_info, preftrees, ref->root,
795 					       &key, 0, node->bytenr,
796 					       node->ref_mod * sgn,
797 					       GFP_ATOMIC);
798 			break;
799 		}
800 		case BTRFS_SHARED_DATA_REF_KEY: {
801 			/* SHARED DIRECT FULL backref */
802 			struct btrfs_delayed_data_ref *ref;
803 
804 			ref = btrfs_delayed_node_to_data_ref(node);
805 
806 			ret = add_direct_ref(fs_info, preftrees, 0,
807 					     ref->parent, node->bytenr,
808 					     node->ref_mod * sgn,
809 					     GFP_ATOMIC);
810 			break;
811 		}
812 		default:
813 			WARN_ON(1);
814 		}
815 		if (ret)
816 			break;
817 	}
818 	spin_unlock(&head->lock);
819 	return ret;
820 }
821 
822 /*
823  * add all inline backrefs for bytenr to the list
824  */
825 static int add_inline_refs(const struct btrfs_fs_info *fs_info,
826 			   struct btrfs_path *path, u64 bytenr,
827 			   int *info_level, struct preftrees *preftrees,
828 			   u64 *total_refs, u64 inum)
829 {
830 	int ret = 0;
831 	int slot;
832 	struct extent_buffer *leaf;
833 	struct btrfs_key key;
834 	struct btrfs_key found_key;
835 	unsigned long ptr;
836 	unsigned long end;
837 	struct btrfs_extent_item *ei;
838 	u64 flags;
839 	u64 item_size;
840 
841 	/*
842 	 * enumerate all inline refs
843 	 */
844 	leaf = path->nodes[0];
845 	slot = path->slots[0];
846 
847 	item_size = btrfs_item_size_nr(leaf, slot);
848 	BUG_ON(item_size < sizeof(*ei));
849 
850 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
851 	flags = btrfs_extent_flags(leaf, ei);
852 	*total_refs += btrfs_extent_refs(leaf, ei);
853 	btrfs_item_key_to_cpu(leaf, &found_key, slot);
854 
855 	ptr = (unsigned long)(ei + 1);
856 	end = (unsigned long)ei + item_size;
857 
858 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
859 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
860 		struct btrfs_tree_block_info *info;
861 
862 		info = (struct btrfs_tree_block_info *)ptr;
863 		*info_level = btrfs_tree_block_level(leaf, info);
864 		ptr += sizeof(struct btrfs_tree_block_info);
865 		BUG_ON(ptr > end);
866 	} else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
867 		*info_level = found_key.offset;
868 	} else {
869 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
870 	}
871 
872 	while (ptr < end) {
873 		struct btrfs_extent_inline_ref *iref;
874 		u64 offset;
875 		int type;
876 
877 		iref = (struct btrfs_extent_inline_ref *)ptr;
878 		type = btrfs_extent_inline_ref_type(leaf, iref);
879 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
880 
881 		switch (type) {
882 		case BTRFS_SHARED_BLOCK_REF_KEY:
883 			ret = add_direct_ref(fs_info, preftrees,
884 					     *info_level + 1, offset,
885 					     bytenr, 1, GFP_NOFS);
886 			break;
887 		case BTRFS_SHARED_DATA_REF_KEY: {
888 			struct btrfs_shared_data_ref *sdref;
889 			int count;
890 
891 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
892 			count = btrfs_shared_data_ref_count(leaf, sdref);
893 
894 			ret = add_direct_ref(fs_info, preftrees, 0, offset,
895 					     bytenr, count, GFP_NOFS);
896 			break;
897 		}
898 		case BTRFS_TREE_BLOCK_REF_KEY:
899 			ret = add_indirect_ref(fs_info, preftrees, offset,
900 					       NULL, *info_level + 1,
901 					       bytenr, 1, GFP_NOFS);
902 			break;
903 		case BTRFS_EXTENT_DATA_REF_KEY: {
904 			struct btrfs_extent_data_ref *dref;
905 			int count;
906 			u64 root;
907 
908 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
909 			count = btrfs_extent_data_ref_count(leaf, dref);
910 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
911 								      dref);
912 			key.type = BTRFS_EXTENT_DATA_KEY;
913 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
914 
915 			if (inum && key.objectid != inum) {
916 				ret = BACKREF_FOUND_SHARED;
917 				break;
918 			}
919 
920 			root = btrfs_extent_data_ref_root(leaf, dref);
921 
922 			ret = add_indirect_ref(fs_info, preftrees, root,
923 					       &key, 0, bytenr, count,
924 					       GFP_NOFS);
925 			break;
926 		}
927 		default:
928 			WARN_ON(1);
929 		}
930 		if (ret)
931 			return ret;
932 		ptr += btrfs_extent_inline_ref_size(type);
933 	}
934 
935 	return 0;
936 }
937 
938 /*
939  * add all non-inline backrefs for bytenr to the list
940  */
941 static int add_keyed_refs(struct btrfs_fs_info *fs_info,
942 			  struct btrfs_path *path, u64 bytenr,
943 			  int info_level, struct preftrees *preftrees,
944 			  u64 inum)
945 {
946 	struct btrfs_root *extent_root = fs_info->extent_root;
947 	int ret;
948 	int slot;
949 	struct extent_buffer *leaf;
950 	struct btrfs_key key;
951 
952 	while (1) {
953 		ret = btrfs_next_item(extent_root, path);
954 		if (ret < 0)
955 			break;
956 		if (ret) {
957 			ret = 0;
958 			break;
959 		}
960 
961 		slot = path->slots[0];
962 		leaf = path->nodes[0];
963 		btrfs_item_key_to_cpu(leaf, &key, slot);
964 
965 		if (key.objectid != bytenr)
966 			break;
967 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
968 			continue;
969 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
970 			break;
971 
972 		switch (key.type) {
973 		case BTRFS_SHARED_BLOCK_REF_KEY:
974 			/* SHARED DIRECT METADATA backref */
975 			ret = add_direct_ref(fs_info, preftrees,
976 					     info_level + 1, key.offset,
977 					     bytenr, 1, GFP_NOFS);
978 			break;
979 		case BTRFS_SHARED_DATA_REF_KEY: {
980 			/* SHARED DIRECT FULL backref */
981 			struct btrfs_shared_data_ref *sdref;
982 			int count;
983 
984 			sdref = btrfs_item_ptr(leaf, slot,
985 					      struct btrfs_shared_data_ref);
986 			count = btrfs_shared_data_ref_count(leaf, sdref);
987 			ret = add_direct_ref(fs_info, preftrees, 0,
988 					     key.offset, bytenr, count,
989 					     GFP_NOFS);
990 			break;
991 		}
992 		case BTRFS_TREE_BLOCK_REF_KEY:
993 			/* NORMAL INDIRECT METADATA backref */
994 			ret = add_indirect_ref(fs_info, preftrees, key.offset,
995 					       NULL, info_level + 1, bytenr,
996 					       1, GFP_NOFS);
997 			break;
998 		case BTRFS_EXTENT_DATA_REF_KEY: {
999 			/* NORMAL INDIRECT DATA backref */
1000 			struct btrfs_extent_data_ref *dref;
1001 			int count;
1002 			u64 root;
1003 
1004 			dref = btrfs_item_ptr(leaf, slot,
1005 					      struct btrfs_extent_data_ref);
1006 			count = btrfs_extent_data_ref_count(leaf, dref);
1007 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
1008 								      dref);
1009 			key.type = BTRFS_EXTENT_DATA_KEY;
1010 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
1011 
1012 			if (inum && key.objectid != inum) {
1013 				ret = BACKREF_FOUND_SHARED;
1014 				break;
1015 			}
1016 
1017 			root = btrfs_extent_data_ref_root(leaf, dref);
1018 			ret = add_indirect_ref(fs_info, preftrees, root,
1019 					       &key, 0, bytenr, count,
1020 					       GFP_NOFS);
1021 			break;
1022 		}
1023 		default:
1024 			WARN_ON(1);
1025 		}
1026 		if (ret)
1027 			return ret;
1028 
1029 	}
1030 
1031 	return ret;
1032 }
1033 
1034 /*
1035  * this adds all existing backrefs (inline backrefs, backrefs and delayed
1036  * refs) for the given bytenr to the refs list, merges duplicates and resolves
1037  * indirect refs to their parent bytenr.
1038  * When roots are found, they're added to the roots list
1039  *
1040  * NOTE: This can return values > 0
1041  *
1042  * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
1043  * much like trans == NULL case, the difference only lies in it will not
1044  * commit root.
1045  * The special case is for qgroup to search roots in commit_transaction().
1046  *
1047  * FIXME some caching might speed things up
1048  */
1049 static int find_parent_nodes(struct btrfs_trans_handle *trans,
1050 			     struct btrfs_fs_info *fs_info, u64 bytenr,
1051 			     u64 time_seq, struct ulist *refs,
1052 			     struct ulist *roots, const u64 *extent_item_pos,
1053 			     u64 root_objectid, u64 inum)
1054 {
1055 	struct btrfs_key key;
1056 	struct btrfs_path *path;
1057 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
1058 	struct btrfs_delayed_ref_head *head;
1059 	int info_level = 0;
1060 	int ret;
1061 	struct prelim_ref *ref;
1062 	struct rb_node *node;
1063 	struct extent_inode_elem *eie = NULL;
1064 	/* total of both direct AND indirect refs! */
1065 	u64 total_refs = 0;
1066 	struct preftrees preftrees = {
1067 		.direct = PREFTREE_INIT,
1068 		.indirect = PREFTREE_INIT,
1069 		.indirect_missing_keys = PREFTREE_INIT
1070 	};
1071 
1072 	key.objectid = bytenr;
1073 	key.offset = (u64)-1;
1074 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1075 		key.type = BTRFS_METADATA_ITEM_KEY;
1076 	else
1077 		key.type = BTRFS_EXTENT_ITEM_KEY;
1078 
1079 	path = btrfs_alloc_path();
1080 	if (!path)
1081 		return -ENOMEM;
1082 	if (!trans) {
1083 		path->search_commit_root = 1;
1084 		path->skip_locking = 1;
1085 	}
1086 
1087 	if (time_seq == SEQ_LAST)
1088 		path->skip_locking = 1;
1089 
1090 	/*
1091 	 * grab both a lock on the path and a lock on the delayed ref head.
1092 	 * We need both to get a consistent picture of how the refs look
1093 	 * at a specified point in time
1094 	 */
1095 again:
1096 	head = NULL;
1097 
1098 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
1099 	if (ret < 0)
1100 		goto out;
1101 	BUG_ON(ret == 0);
1102 
1103 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1104 	if (trans && likely(trans->type != __TRANS_DUMMY) &&
1105 	    time_seq != SEQ_LAST) {
1106 #else
1107 	if (trans && time_seq != SEQ_LAST) {
1108 #endif
1109 		/*
1110 		 * look if there are updates for this ref queued and lock the
1111 		 * head
1112 		 */
1113 		delayed_refs = &trans->transaction->delayed_refs;
1114 		spin_lock(&delayed_refs->lock);
1115 		head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
1116 		if (head) {
1117 			if (!mutex_trylock(&head->mutex)) {
1118 				refcount_inc(&head->node.refs);
1119 				spin_unlock(&delayed_refs->lock);
1120 
1121 				btrfs_release_path(path);
1122 
1123 				/*
1124 				 * Mutex was contended, block until it's
1125 				 * released and try again
1126 				 */
1127 				mutex_lock(&head->mutex);
1128 				mutex_unlock(&head->mutex);
1129 				btrfs_put_delayed_ref(&head->node);
1130 				goto again;
1131 			}
1132 			spin_unlock(&delayed_refs->lock);
1133 			ret = add_delayed_refs(fs_info, head, time_seq,
1134 					       &preftrees, &total_refs, inum);
1135 			mutex_unlock(&head->mutex);
1136 			if (ret)
1137 				goto out;
1138 		} else {
1139 			spin_unlock(&delayed_refs->lock);
1140 		}
1141 	}
1142 
1143 	if (path->slots[0]) {
1144 		struct extent_buffer *leaf;
1145 		int slot;
1146 
1147 		path->slots[0]--;
1148 		leaf = path->nodes[0];
1149 		slot = path->slots[0];
1150 		btrfs_item_key_to_cpu(leaf, &key, slot);
1151 		if (key.objectid == bytenr &&
1152 		    (key.type == BTRFS_EXTENT_ITEM_KEY ||
1153 		     key.type == BTRFS_METADATA_ITEM_KEY)) {
1154 			ret = add_inline_refs(fs_info, path, bytenr,
1155 					      &info_level, &preftrees,
1156 					      &total_refs, inum);
1157 			if (ret)
1158 				goto out;
1159 			ret = add_keyed_refs(fs_info, path, bytenr, info_level,
1160 					     &preftrees, inum);
1161 			if (ret)
1162 				goto out;
1163 		}
1164 	}
1165 
1166 	btrfs_release_path(path);
1167 
1168 	ret = add_missing_keys(fs_info, &preftrees);
1169 	if (ret)
1170 		goto out;
1171 
1172 	WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
1173 
1174 	ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
1175 				    extent_item_pos, total_refs,
1176 				    root_objectid);
1177 	if (ret)
1178 		goto out;
1179 
1180 	WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
1181 
1182 	/*
1183 	 * This walks the tree of merged and resolved refs. Tree blocks are
1184 	 * read in as needed. Unique entries are added to the ulist, and
1185 	 * the list of found roots is updated.
1186 	 *
1187 	 * We release the entire tree in one go before returning.
1188 	 */
1189 	node = rb_first(&preftrees.direct.root);
1190 	while (node) {
1191 		ref = rb_entry(node, struct prelim_ref, rbnode);
1192 		node = rb_next(&ref->rbnode);
1193 		WARN_ON(ref->count < 0);
1194 		if (roots && ref->count && ref->root_id && ref->parent == 0) {
1195 			if (root_objectid && ref->root_id != root_objectid) {
1196 				ret = BACKREF_FOUND_SHARED;
1197 				goto out;
1198 			}
1199 
1200 			/* no parent == root of tree */
1201 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
1202 			if (ret < 0)
1203 				goto out;
1204 		}
1205 		if (ref->count && ref->parent) {
1206 			if (extent_item_pos && !ref->inode_list &&
1207 			    ref->level == 0) {
1208 				struct extent_buffer *eb;
1209 
1210 				eb = read_tree_block(fs_info, ref->parent, 0);
1211 				if (IS_ERR(eb)) {
1212 					ret = PTR_ERR(eb);
1213 					goto out;
1214 				} else if (!extent_buffer_uptodate(eb)) {
1215 					free_extent_buffer(eb);
1216 					ret = -EIO;
1217 					goto out;
1218 				}
1219 				btrfs_tree_read_lock(eb);
1220 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1221 				ret = find_extent_in_eb(eb, bytenr,
1222 							*extent_item_pos, &eie);
1223 				btrfs_tree_read_unlock_blocking(eb);
1224 				free_extent_buffer(eb);
1225 				if (ret < 0)
1226 					goto out;
1227 				ref->inode_list = eie;
1228 			}
1229 			ret = ulist_add_merge_ptr(refs, ref->parent,
1230 						  ref->inode_list,
1231 						  (void **)&eie, GFP_NOFS);
1232 			if (ret < 0)
1233 				goto out;
1234 			if (!ret && extent_item_pos) {
1235 				/*
1236 				 * we've recorded that parent, so we must extend
1237 				 * its inode list here
1238 				 */
1239 				BUG_ON(!eie);
1240 				while (eie->next)
1241 					eie = eie->next;
1242 				eie->next = ref->inode_list;
1243 			}
1244 			eie = NULL;
1245 		}
1246 	}
1247 
1248 out:
1249 	btrfs_free_path(path);
1250 
1251 	prelim_release(&preftrees.direct);
1252 	prelim_release(&preftrees.indirect);
1253 	prelim_release(&preftrees.indirect_missing_keys);
1254 
1255 	if (ret < 0)
1256 		free_inode_elem_list(eie);
1257 	return ret;
1258 }
1259 
1260 static void free_leaf_list(struct ulist *blocks)
1261 {
1262 	struct ulist_node *node = NULL;
1263 	struct extent_inode_elem *eie;
1264 	struct ulist_iterator uiter;
1265 
1266 	ULIST_ITER_INIT(&uiter);
1267 	while ((node = ulist_next(blocks, &uiter))) {
1268 		if (!node->aux)
1269 			continue;
1270 		eie = unode_aux_to_inode_list(node);
1271 		free_inode_elem_list(eie);
1272 		node->aux = 0;
1273 	}
1274 
1275 	ulist_free(blocks);
1276 }
1277 
1278 /*
1279  * Finds all leafs with a reference to the specified combination of bytenr and
1280  * offset. key_list_head will point to a list of corresponding keys (caller must
1281  * free each list element). The leafs will be stored in the leafs ulist, which
1282  * must be freed with ulist_free.
1283  *
1284  * returns 0 on success, <0 on error
1285  */
1286 static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
1287 				struct btrfs_fs_info *fs_info, u64 bytenr,
1288 				u64 time_seq, struct ulist **leafs,
1289 				const u64 *extent_item_pos)
1290 {
1291 	int ret;
1292 
1293 	*leafs = ulist_alloc(GFP_NOFS);
1294 	if (!*leafs)
1295 		return -ENOMEM;
1296 
1297 	ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1298 				*leafs, NULL, extent_item_pos, 0, 0);
1299 	if (ret < 0 && ret != -ENOENT) {
1300 		free_leaf_list(*leafs);
1301 		return ret;
1302 	}
1303 
1304 	return 0;
1305 }
1306 
1307 /*
1308  * walk all backrefs for a given extent to find all roots that reference this
1309  * extent. Walking a backref means finding all extents that reference this
1310  * extent and in turn walk the backrefs of those, too. Naturally this is a
1311  * recursive process, but here it is implemented in an iterative fashion: We
1312  * find all referencing extents for the extent in question and put them on a
1313  * list. In turn, we find all referencing extents for those, further appending
1314  * to the list. The way we iterate the list allows adding more elements after
1315  * the current while iterating. The process stops when we reach the end of the
1316  * list. Found roots are added to the roots list.
1317  *
1318  * returns 0 on success, < 0 on error.
1319  */
1320 static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
1321 				     struct btrfs_fs_info *fs_info, u64 bytenr,
1322 				     u64 time_seq, struct ulist **roots)
1323 {
1324 	struct ulist *tmp;
1325 	struct ulist_node *node = NULL;
1326 	struct ulist_iterator uiter;
1327 	int ret;
1328 
1329 	tmp = ulist_alloc(GFP_NOFS);
1330 	if (!tmp)
1331 		return -ENOMEM;
1332 	*roots = ulist_alloc(GFP_NOFS);
1333 	if (!*roots) {
1334 		ulist_free(tmp);
1335 		return -ENOMEM;
1336 	}
1337 
1338 	ULIST_ITER_INIT(&uiter);
1339 	while (1) {
1340 		ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1341 					tmp, *roots, NULL, 0, 0);
1342 		if (ret < 0 && ret != -ENOENT) {
1343 			ulist_free(tmp);
1344 			ulist_free(*roots);
1345 			return ret;
1346 		}
1347 		node = ulist_next(tmp, &uiter);
1348 		if (!node)
1349 			break;
1350 		bytenr = node->val;
1351 		cond_resched();
1352 	}
1353 
1354 	ulist_free(tmp);
1355 	return 0;
1356 }
1357 
1358 int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
1359 			 struct btrfs_fs_info *fs_info, u64 bytenr,
1360 			 u64 time_seq, struct ulist **roots)
1361 {
1362 	int ret;
1363 
1364 	if (!trans)
1365 		down_read(&fs_info->commit_root_sem);
1366 	ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
1367 					time_seq, roots);
1368 	if (!trans)
1369 		up_read(&fs_info->commit_root_sem);
1370 	return ret;
1371 }
1372 
1373 /**
1374  * btrfs_check_shared - tell us whether an extent is shared
1375  *
1376  * btrfs_check_shared uses the backref walking code but will short
1377  * circuit as soon as it finds a root or inode that doesn't match the
1378  * one passed in. This provides a significant performance benefit for
1379  * callers (such as fiemap) which want to know whether the extent is
1380  * shared but do not need a ref count.
1381  *
1382  * This attempts to allocate a transaction in order to account for
1383  * delayed refs, but continues on even when the alloc fails.
1384  *
1385  * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
1386  */
1387 int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
1388 {
1389 	struct btrfs_fs_info *fs_info = root->fs_info;
1390 	struct btrfs_trans_handle *trans;
1391 	struct ulist *tmp = NULL;
1392 	struct ulist *roots = NULL;
1393 	struct ulist_iterator uiter;
1394 	struct ulist_node *node;
1395 	struct seq_list elem = SEQ_LIST_INIT(elem);
1396 	int ret = 0;
1397 
1398 	tmp = ulist_alloc(GFP_NOFS);
1399 	roots = ulist_alloc(GFP_NOFS);
1400 	if (!tmp || !roots) {
1401 		ulist_free(tmp);
1402 		ulist_free(roots);
1403 		return -ENOMEM;
1404 	}
1405 
1406 	trans = btrfs_join_transaction(root);
1407 	if (IS_ERR(trans)) {
1408 		trans = NULL;
1409 		down_read(&fs_info->commit_root_sem);
1410 	} else {
1411 		btrfs_get_tree_mod_seq(fs_info, &elem);
1412 	}
1413 
1414 	ULIST_ITER_INIT(&uiter);
1415 	while (1) {
1416 		ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1417 					roots, NULL, root->objectid, inum);
1418 		if (ret == BACKREF_FOUND_SHARED) {
1419 			/* this is the only condition under which we return 1 */
1420 			ret = 1;
1421 			break;
1422 		}
1423 		if (ret < 0 && ret != -ENOENT)
1424 			break;
1425 		ret = 0;
1426 		node = ulist_next(tmp, &uiter);
1427 		if (!node)
1428 			break;
1429 		bytenr = node->val;
1430 		cond_resched();
1431 	}
1432 
1433 	if (trans) {
1434 		btrfs_put_tree_mod_seq(fs_info, &elem);
1435 		btrfs_end_transaction(trans);
1436 	} else {
1437 		up_read(&fs_info->commit_root_sem);
1438 	}
1439 	ulist_free(tmp);
1440 	ulist_free(roots);
1441 	return ret;
1442 }
1443 
1444 int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1445 			  u64 start_off, struct btrfs_path *path,
1446 			  struct btrfs_inode_extref **ret_extref,
1447 			  u64 *found_off)
1448 {
1449 	int ret, slot;
1450 	struct btrfs_key key;
1451 	struct btrfs_key found_key;
1452 	struct btrfs_inode_extref *extref;
1453 	const struct extent_buffer *leaf;
1454 	unsigned long ptr;
1455 
1456 	key.objectid = inode_objectid;
1457 	key.type = BTRFS_INODE_EXTREF_KEY;
1458 	key.offset = start_off;
1459 
1460 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1461 	if (ret < 0)
1462 		return ret;
1463 
1464 	while (1) {
1465 		leaf = path->nodes[0];
1466 		slot = path->slots[0];
1467 		if (slot >= btrfs_header_nritems(leaf)) {
1468 			/*
1469 			 * If the item at offset is not found,
1470 			 * btrfs_search_slot will point us to the slot
1471 			 * where it should be inserted. In our case
1472 			 * that will be the slot directly before the
1473 			 * next INODE_REF_KEY_V2 item. In the case
1474 			 * that we're pointing to the last slot in a
1475 			 * leaf, we must move one leaf over.
1476 			 */
1477 			ret = btrfs_next_leaf(root, path);
1478 			if (ret) {
1479 				if (ret >= 1)
1480 					ret = -ENOENT;
1481 				break;
1482 			}
1483 			continue;
1484 		}
1485 
1486 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1487 
1488 		/*
1489 		 * Check that we're still looking at an extended ref key for
1490 		 * this particular objectid. If we have different
1491 		 * objectid or type then there are no more to be found
1492 		 * in the tree and we can exit.
1493 		 */
1494 		ret = -ENOENT;
1495 		if (found_key.objectid != inode_objectid)
1496 			break;
1497 		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
1498 			break;
1499 
1500 		ret = 0;
1501 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1502 		extref = (struct btrfs_inode_extref *)ptr;
1503 		*ret_extref = extref;
1504 		if (found_off)
1505 			*found_off = found_key.offset;
1506 		break;
1507 	}
1508 
1509 	return ret;
1510 }
1511 
1512 /*
1513  * this iterates to turn a name (from iref/extref) into a full filesystem path.
1514  * Elements of the path are separated by '/' and the path is guaranteed to be
1515  * 0-terminated. the path is only given within the current file system.
1516  * Therefore, it never starts with a '/'. the caller is responsible to provide
1517  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
1518  * the start point of the resulting string is returned. this pointer is within
1519  * dest, normally.
1520  * in case the path buffer would overflow, the pointer is decremented further
1521  * as if output was written to the buffer, though no more output is actually
1522  * generated. that way, the caller can determine how much space would be
1523  * required for the path to fit into the buffer. in that case, the returned
1524  * value will be smaller than dest. callers must check this!
1525  */
1526 char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1527 			u32 name_len, unsigned long name_off,
1528 			struct extent_buffer *eb_in, u64 parent,
1529 			char *dest, u32 size)
1530 {
1531 	int slot;
1532 	u64 next_inum;
1533 	int ret;
1534 	s64 bytes_left = ((s64)size) - 1;
1535 	struct extent_buffer *eb = eb_in;
1536 	struct btrfs_key found_key;
1537 	int leave_spinning = path->leave_spinning;
1538 	struct btrfs_inode_ref *iref;
1539 
1540 	if (bytes_left >= 0)
1541 		dest[bytes_left] = '\0';
1542 
1543 	path->leave_spinning = 1;
1544 	while (1) {
1545 		bytes_left -= name_len;
1546 		if (bytes_left >= 0)
1547 			read_extent_buffer(eb, dest + bytes_left,
1548 					   name_off, name_len);
1549 		if (eb != eb_in) {
1550 			if (!path->skip_locking)
1551 				btrfs_tree_read_unlock_blocking(eb);
1552 			free_extent_buffer(eb);
1553 		}
1554 		ret = btrfs_find_item(fs_root, path, parent, 0,
1555 				BTRFS_INODE_REF_KEY, &found_key);
1556 		if (ret > 0)
1557 			ret = -ENOENT;
1558 		if (ret)
1559 			break;
1560 
1561 		next_inum = found_key.offset;
1562 
1563 		/* regular exit ahead */
1564 		if (parent == next_inum)
1565 			break;
1566 
1567 		slot = path->slots[0];
1568 		eb = path->nodes[0];
1569 		/* make sure we can use eb after releasing the path */
1570 		if (eb != eb_in) {
1571 			if (!path->skip_locking)
1572 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1573 			path->nodes[0] = NULL;
1574 			path->locks[0] = 0;
1575 		}
1576 		btrfs_release_path(path);
1577 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1578 
1579 		name_len = btrfs_inode_ref_name_len(eb, iref);
1580 		name_off = (unsigned long)(iref + 1);
1581 
1582 		parent = next_inum;
1583 		--bytes_left;
1584 		if (bytes_left >= 0)
1585 			dest[bytes_left] = '/';
1586 	}
1587 
1588 	btrfs_release_path(path);
1589 	path->leave_spinning = leave_spinning;
1590 
1591 	if (ret)
1592 		return ERR_PTR(ret);
1593 
1594 	return dest + bytes_left;
1595 }
1596 
1597 /*
1598  * this makes the path point to (logical EXTENT_ITEM *)
1599  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1600  * tree blocks and <0 on error.
1601  */
1602 int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
1603 			struct btrfs_path *path, struct btrfs_key *found_key,
1604 			u64 *flags_ret)
1605 {
1606 	int ret;
1607 	u64 flags;
1608 	u64 size = 0;
1609 	u32 item_size;
1610 	const struct extent_buffer *eb;
1611 	struct btrfs_extent_item *ei;
1612 	struct btrfs_key key;
1613 
1614 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1615 		key.type = BTRFS_METADATA_ITEM_KEY;
1616 	else
1617 		key.type = BTRFS_EXTENT_ITEM_KEY;
1618 	key.objectid = logical;
1619 	key.offset = (u64)-1;
1620 
1621 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1622 	if (ret < 0)
1623 		return ret;
1624 
1625 	ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1626 	if (ret) {
1627 		if (ret > 0)
1628 			ret = -ENOENT;
1629 		return ret;
1630 	}
1631 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1632 	if (found_key->type == BTRFS_METADATA_ITEM_KEY)
1633 		size = fs_info->nodesize;
1634 	else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1635 		size = found_key->offset;
1636 
1637 	if (found_key->objectid > logical ||
1638 	    found_key->objectid + size <= logical) {
1639 		btrfs_debug(fs_info,
1640 			"logical %llu is not within any extent", logical);
1641 		return -ENOENT;
1642 	}
1643 
1644 	eb = path->nodes[0];
1645 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1646 	BUG_ON(item_size < sizeof(*ei));
1647 
1648 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1649 	flags = btrfs_extent_flags(eb, ei);
1650 
1651 	btrfs_debug(fs_info,
1652 		"logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
1653 		 logical, logical - found_key->objectid, found_key->objectid,
1654 		 found_key->offset, flags, item_size);
1655 
1656 	WARN_ON(!flags_ret);
1657 	if (flags_ret) {
1658 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1659 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
1660 		else if (flags & BTRFS_EXTENT_FLAG_DATA)
1661 			*flags_ret = BTRFS_EXTENT_FLAG_DATA;
1662 		else
1663 			BUG_ON(1);
1664 		return 0;
1665 	}
1666 
1667 	return -EIO;
1668 }
1669 
1670 /*
1671  * helper function to iterate extent inline refs. ptr must point to a 0 value
1672  * for the first call and may be modified. it is used to track state.
1673  * if more refs exist, 0 is returned and the next call to
1674  * get_extent_inline_ref must pass the modified ptr parameter to get the
1675  * next ref. after the last ref was processed, 1 is returned.
1676  * returns <0 on error
1677  */
1678 static int get_extent_inline_ref(unsigned long *ptr,
1679 				 const struct extent_buffer *eb,
1680 				 const struct btrfs_key *key,
1681 				 const struct btrfs_extent_item *ei,
1682 				 u32 item_size,
1683 				 struct btrfs_extent_inline_ref **out_eiref,
1684 				 int *out_type)
1685 {
1686 	unsigned long end;
1687 	u64 flags;
1688 	struct btrfs_tree_block_info *info;
1689 
1690 	if (!*ptr) {
1691 		/* first call */
1692 		flags = btrfs_extent_flags(eb, ei);
1693 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1694 			if (key->type == BTRFS_METADATA_ITEM_KEY) {
1695 				/* a skinny metadata extent */
1696 				*out_eiref =
1697 				     (struct btrfs_extent_inline_ref *)(ei + 1);
1698 			} else {
1699 				WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1700 				info = (struct btrfs_tree_block_info *)(ei + 1);
1701 				*out_eiref =
1702 				   (struct btrfs_extent_inline_ref *)(info + 1);
1703 			}
1704 		} else {
1705 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1706 		}
1707 		*ptr = (unsigned long)*out_eiref;
1708 		if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
1709 			return -ENOENT;
1710 	}
1711 
1712 	end = (unsigned long)ei + item_size;
1713 	*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
1714 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1715 
1716 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1717 	WARN_ON(*ptr > end);
1718 	if (*ptr == end)
1719 		return 1; /* last */
1720 
1721 	return 0;
1722 }
1723 
1724 /*
1725  * reads the tree block backref for an extent. tree level and root are returned
1726  * through out_level and out_root. ptr must point to a 0 value for the first
1727  * call and may be modified (see get_extent_inline_ref comment).
1728  * returns 0 if data was provided, 1 if there was no more data to provide or
1729  * <0 on error.
1730  */
1731 int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
1732 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
1733 			    u32 item_size, u64 *out_root, u8 *out_level)
1734 {
1735 	int ret;
1736 	int type;
1737 	struct btrfs_extent_inline_ref *eiref;
1738 
1739 	if (*ptr == (unsigned long)-1)
1740 		return 1;
1741 
1742 	while (1) {
1743 		ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
1744 					      &eiref, &type);
1745 		if (ret < 0)
1746 			return ret;
1747 
1748 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1749 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1750 			break;
1751 
1752 		if (ret == 1)
1753 			return 1;
1754 	}
1755 
1756 	/* we can treat both ref types equally here */
1757 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1758 
1759 	if (key->type == BTRFS_EXTENT_ITEM_KEY) {
1760 		struct btrfs_tree_block_info *info;
1761 
1762 		info = (struct btrfs_tree_block_info *)(ei + 1);
1763 		*out_level = btrfs_tree_block_level(eb, info);
1764 	} else {
1765 		ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
1766 		*out_level = (u8)key->offset;
1767 	}
1768 
1769 	if (ret == 1)
1770 		*ptr = (unsigned long)-1;
1771 
1772 	return 0;
1773 }
1774 
1775 static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
1776 			     struct extent_inode_elem *inode_list,
1777 			     u64 root, u64 extent_item_objectid,
1778 			     iterate_extent_inodes_t *iterate, void *ctx)
1779 {
1780 	struct extent_inode_elem *eie;
1781 	int ret = 0;
1782 
1783 	for (eie = inode_list; eie; eie = eie->next) {
1784 		btrfs_debug(fs_info,
1785 			    "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
1786 			    extent_item_objectid, eie->inum,
1787 			    eie->offset, root);
1788 		ret = iterate(eie->inum, eie->offset, root, ctx);
1789 		if (ret) {
1790 			btrfs_debug(fs_info,
1791 				    "stopping iteration for %llu due to ret=%d",
1792 				    extent_item_objectid, ret);
1793 			break;
1794 		}
1795 	}
1796 
1797 	return ret;
1798 }
1799 
1800 /*
1801  * calls iterate() for every inode that references the extent identified by
1802  * the given parameters.
1803  * when the iterator function returns a non-zero value, iteration stops.
1804  */
1805 int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
1806 				u64 extent_item_objectid, u64 extent_item_pos,
1807 				int search_commit_root,
1808 				iterate_extent_inodes_t *iterate, void *ctx)
1809 {
1810 	int ret;
1811 	struct btrfs_trans_handle *trans = NULL;
1812 	struct ulist *refs = NULL;
1813 	struct ulist *roots = NULL;
1814 	struct ulist_node *ref_node = NULL;
1815 	struct ulist_node *root_node = NULL;
1816 	struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
1817 	struct ulist_iterator ref_uiter;
1818 	struct ulist_iterator root_uiter;
1819 
1820 	btrfs_debug(fs_info, "resolving all inodes for extent %llu",
1821 			extent_item_objectid);
1822 
1823 	if (!search_commit_root) {
1824 		trans = btrfs_join_transaction(fs_info->extent_root);
1825 		if (IS_ERR(trans))
1826 			return PTR_ERR(trans);
1827 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1828 	} else {
1829 		down_read(&fs_info->commit_root_sem);
1830 	}
1831 
1832 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1833 				   tree_mod_seq_elem.seq, &refs,
1834 				   &extent_item_pos);
1835 	if (ret)
1836 		goto out;
1837 
1838 	ULIST_ITER_INIT(&ref_uiter);
1839 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
1840 		ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
1841 						tree_mod_seq_elem.seq, &roots);
1842 		if (ret)
1843 			break;
1844 		ULIST_ITER_INIT(&root_uiter);
1845 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
1846 			btrfs_debug(fs_info,
1847 				    "root %llu references leaf %llu, data list %#llx",
1848 				    root_node->val, ref_node->val,
1849 				    ref_node->aux);
1850 			ret = iterate_leaf_refs(fs_info,
1851 						(struct extent_inode_elem *)
1852 						(uintptr_t)ref_node->aux,
1853 						root_node->val,
1854 						extent_item_objectid,
1855 						iterate, ctx);
1856 		}
1857 		ulist_free(roots);
1858 	}
1859 
1860 	free_leaf_list(refs);
1861 out:
1862 	if (!search_commit_root) {
1863 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1864 		btrfs_end_transaction(trans);
1865 	} else {
1866 		up_read(&fs_info->commit_root_sem);
1867 	}
1868 
1869 	return ret;
1870 }
1871 
1872 int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1873 				struct btrfs_path *path,
1874 				iterate_extent_inodes_t *iterate, void *ctx)
1875 {
1876 	int ret;
1877 	u64 extent_item_pos;
1878 	u64 flags = 0;
1879 	struct btrfs_key found_key;
1880 	int search_commit_root = path->search_commit_root;
1881 
1882 	ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
1883 	btrfs_release_path(path);
1884 	if (ret < 0)
1885 		return ret;
1886 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1887 		return -EINVAL;
1888 
1889 	extent_item_pos = logical - found_key.objectid;
1890 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
1891 					extent_item_pos, search_commit_root,
1892 					iterate, ctx);
1893 
1894 	return ret;
1895 }
1896 
1897 typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1898 			      struct extent_buffer *eb, void *ctx);
1899 
1900 static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1901 			      struct btrfs_path *path,
1902 			      iterate_irefs_t *iterate, void *ctx)
1903 {
1904 	int ret = 0;
1905 	int slot;
1906 	u32 cur;
1907 	u32 len;
1908 	u32 name_len;
1909 	u64 parent = 0;
1910 	int found = 0;
1911 	struct extent_buffer *eb;
1912 	struct btrfs_item *item;
1913 	struct btrfs_inode_ref *iref;
1914 	struct btrfs_key found_key;
1915 
1916 	while (!ret) {
1917 		ret = btrfs_find_item(fs_root, path, inum,
1918 				parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
1919 				&found_key);
1920 
1921 		if (ret < 0)
1922 			break;
1923 		if (ret) {
1924 			ret = found ? 0 : -ENOENT;
1925 			break;
1926 		}
1927 		++found;
1928 
1929 		parent = found_key.offset;
1930 		slot = path->slots[0];
1931 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
1932 		if (!eb) {
1933 			ret = -ENOMEM;
1934 			break;
1935 		}
1936 		extent_buffer_get(eb);
1937 		btrfs_tree_read_lock(eb);
1938 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1939 		btrfs_release_path(path);
1940 
1941 		item = btrfs_item_nr(slot);
1942 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1943 
1944 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1945 			name_len = btrfs_inode_ref_name_len(eb, iref);
1946 			/* path must be released before calling iterate()! */
1947 			btrfs_debug(fs_root->fs_info,
1948 				"following ref at offset %u for inode %llu in tree %llu",
1949 				cur, found_key.objectid, fs_root->objectid);
1950 			ret = iterate(parent, name_len,
1951 				      (unsigned long)(iref + 1), eb, ctx);
1952 			if (ret)
1953 				break;
1954 			len = sizeof(*iref) + name_len;
1955 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
1956 		}
1957 		btrfs_tree_read_unlock_blocking(eb);
1958 		free_extent_buffer(eb);
1959 	}
1960 
1961 	btrfs_release_path(path);
1962 
1963 	return ret;
1964 }
1965 
1966 static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
1967 				 struct btrfs_path *path,
1968 				 iterate_irefs_t *iterate, void *ctx)
1969 {
1970 	int ret;
1971 	int slot;
1972 	u64 offset = 0;
1973 	u64 parent;
1974 	int found = 0;
1975 	struct extent_buffer *eb;
1976 	struct btrfs_inode_extref *extref;
1977 	u32 item_size;
1978 	u32 cur_offset;
1979 	unsigned long ptr;
1980 
1981 	while (1) {
1982 		ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
1983 					    &offset);
1984 		if (ret < 0)
1985 			break;
1986 		if (ret) {
1987 			ret = found ? 0 : -ENOENT;
1988 			break;
1989 		}
1990 		++found;
1991 
1992 		slot = path->slots[0];
1993 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
1994 		if (!eb) {
1995 			ret = -ENOMEM;
1996 			break;
1997 		}
1998 		extent_buffer_get(eb);
1999 
2000 		btrfs_tree_read_lock(eb);
2001 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2002 		btrfs_release_path(path);
2003 
2004 		item_size = btrfs_item_size_nr(eb, slot);
2005 		ptr = btrfs_item_ptr_offset(eb, slot);
2006 		cur_offset = 0;
2007 
2008 		while (cur_offset < item_size) {
2009 			u32 name_len;
2010 
2011 			extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
2012 			parent = btrfs_inode_extref_parent(eb, extref);
2013 			name_len = btrfs_inode_extref_name_len(eb, extref);
2014 			ret = iterate(parent, name_len,
2015 				      (unsigned long)&extref->name, eb, ctx);
2016 			if (ret)
2017 				break;
2018 
2019 			cur_offset += btrfs_inode_extref_name_len(eb, extref);
2020 			cur_offset += sizeof(*extref);
2021 		}
2022 		btrfs_tree_read_unlock_blocking(eb);
2023 		free_extent_buffer(eb);
2024 
2025 		offset++;
2026 	}
2027 
2028 	btrfs_release_path(path);
2029 
2030 	return ret;
2031 }
2032 
2033 static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
2034 			 struct btrfs_path *path, iterate_irefs_t *iterate,
2035 			 void *ctx)
2036 {
2037 	int ret;
2038 	int found_refs = 0;
2039 
2040 	ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
2041 	if (!ret)
2042 		++found_refs;
2043 	else if (ret != -ENOENT)
2044 		return ret;
2045 
2046 	ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
2047 	if (ret == -ENOENT && found_refs)
2048 		return 0;
2049 
2050 	return ret;
2051 }
2052 
2053 /*
2054  * returns 0 if the path could be dumped (probably truncated)
2055  * returns <0 in case of an error
2056  */
2057 static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
2058 			 struct extent_buffer *eb, void *ctx)
2059 {
2060 	struct inode_fs_paths *ipath = ctx;
2061 	char *fspath;
2062 	char *fspath_min;
2063 	int i = ipath->fspath->elem_cnt;
2064 	const int s_ptr = sizeof(char *);
2065 	u32 bytes_left;
2066 
2067 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
2068 					ipath->fspath->bytes_left - s_ptr : 0;
2069 
2070 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
2071 	fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
2072 				   name_off, eb, inum, fspath_min, bytes_left);
2073 	if (IS_ERR(fspath))
2074 		return PTR_ERR(fspath);
2075 
2076 	if (fspath > fspath_min) {
2077 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
2078 		++ipath->fspath->elem_cnt;
2079 		ipath->fspath->bytes_left = fspath - fspath_min;
2080 	} else {
2081 		++ipath->fspath->elem_missed;
2082 		ipath->fspath->bytes_missing += fspath_min - fspath;
2083 		ipath->fspath->bytes_left = 0;
2084 	}
2085 
2086 	return 0;
2087 }
2088 
2089 /*
2090  * this dumps all file system paths to the inode into the ipath struct, provided
2091  * is has been created large enough. each path is zero-terminated and accessed
2092  * from ipath->fspath->val[i].
2093  * when it returns, there are ipath->fspath->elem_cnt number of paths available
2094  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
2095  * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
2096  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
2097  * have been needed to return all paths.
2098  */
2099 int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
2100 {
2101 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
2102 			     inode_to_path, ipath);
2103 }
2104 
2105 struct btrfs_data_container *init_data_container(u32 total_bytes)
2106 {
2107 	struct btrfs_data_container *data;
2108 	size_t alloc_bytes;
2109 
2110 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
2111 	data = kvmalloc(alloc_bytes, GFP_KERNEL);
2112 	if (!data)
2113 		return ERR_PTR(-ENOMEM);
2114 
2115 	if (total_bytes >= sizeof(*data)) {
2116 		data->bytes_left = total_bytes - sizeof(*data);
2117 		data->bytes_missing = 0;
2118 	} else {
2119 		data->bytes_missing = sizeof(*data) - total_bytes;
2120 		data->bytes_left = 0;
2121 	}
2122 
2123 	data->elem_cnt = 0;
2124 	data->elem_missed = 0;
2125 
2126 	return data;
2127 }
2128 
2129 /*
2130  * allocates space to return multiple file system paths for an inode.
2131  * total_bytes to allocate are passed, note that space usable for actual path
2132  * information will be total_bytes - sizeof(struct inode_fs_paths).
2133  * the returned pointer must be freed with free_ipath() in the end.
2134  */
2135 struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
2136 					struct btrfs_path *path)
2137 {
2138 	struct inode_fs_paths *ifp;
2139 	struct btrfs_data_container *fspath;
2140 
2141 	fspath = init_data_container(total_bytes);
2142 	if (IS_ERR(fspath))
2143 		return (void *)fspath;
2144 
2145 	ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
2146 	if (!ifp) {
2147 		kvfree(fspath);
2148 		return ERR_PTR(-ENOMEM);
2149 	}
2150 
2151 	ifp->btrfs_path = path;
2152 	ifp->fspath = fspath;
2153 	ifp->fs_root = fs_root;
2154 
2155 	return ifp;
2156 }
2157 
2158 void free_ipath(struct inode_fs_paths *ipath)
2159 {
2160 	if (!ipath)
2161 		return;
2162 	kvfree(ipath->fspath);
2163 	kfree(ipath);
2164 }
2165