xref: /linux/fs/btrfs/backref.c (revision 1525e06e4414dbe053ea10cbd8cc03bb150e8e27)
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 "ctree.h"
20 #include "disk-io.h"
21 #include "backref.h"
22 #include "ulist.h"
23 #include "transaction.h"
24 #include "delayed-ref.h"
25 #include "locking.h"
26 
27 struct extent_inode_elem {
28 	u64 inum;
29 	u64 offset;
30 	struct extent_inode_elem *next;
31 };
32 
33 static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb,
34 				struct btrfs_file_extent_item *fi,
35 				u64 extent_item_pos,
36 				struct extent_inode_elem **eie)
37 {
38 	u64 data_offset;
39 	u64 data_len;
40 	struct extent_inode_elem *e;
41 
42 	data_offset = btrfs_file_extent_offset(eb, fi);
43 	data_len = btrfs_file_extent_num_bytes(eb, fi);
44 
45 	if (extent_item_pos < data_offset ||
46 	    extent_item_pos >= data_offset + data_len)
47 		return 1;
48 
49 	e = kmalloc(sizeof(*e), GFP_NOFS);
50 	if (!e)
51 		return -ENOMEM;
52 
53 	e->next = *eie;
54 	e->inum = key->objectid;
55 	e->offset = key->offset + (extent_item_pos - data_offset);
56 	*eie = e;
57 
58 	return 0;
59 }
60 
61 static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte,
62 				u64 extent_item_pos,
63 				struct extent_inode_elem **eie)
64 {
65 	u64 disk_byte;
66 	struct btrfs_key key;
67 	struct btrfs_file_extent_item *fi;
68 	int slot;
69 	int nritems;
70 	int extent_type;
71 	int ret;
72 
73 	/*
74 	 * from the shared data ref, we only have the leaf but we need
75 	 * the key. thus, we must look into all items and see that we
76 	 * find one (some) with a reference to our extent item.
77 	 */
78 	nritems = btrfs_header_nritems(eb);
79 	for (slot = 0; slot < nritems; ++slot) {
80 		btrfs_item_key_to_cpu(eb, &key, slot);
81 		if (key.type != BTRFS_EXTENT_DATA_KEY)
82 			continue;
83 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
84 		extent_type = btrfs_file_extent_type(eb, fi);
85 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
86 			continue;
87 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
88 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
89 		if (disk_byte != wanted_disk_byte)
90 			continue;
91 
92 		ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
93 		if (ret < 0)
94 			return ret;
95 	}
96 
97 	return 0;
98 }
99 
100 /*
101  * this structure records all encountered refs on the way up to the root
102  */
103 struct __prelim_ref {
104 	struct list_head list;
105 	u64 root_id;
106 	struct btrfs_key key_for_search;
107 	int level;
108 	int count;
109 	struct extent_inode_elem *inode_list;
110 	u64 parent;
111 	u64 wanted_disk_byte;
112 };
113 
114 /*
115  * the rules for all callers of this function are:
116  * - obtaining the parent is the goal
117  * - if you add a key, you must know that it is a correct key
118  * - if you cannot add the parent or a correct key, then we will look into the
119  *   block later to set a correct key
120  *
121  * delayed refs
122  * ============
123  *        backref type | shared | indirect | shared | indirect
124  * information         |   tree |     tree |   data |     data
125  * --------------------+--------+----------+--------+----------
126  *      parent logical |    y   |     -    |    -   |     -
127  *      key to resolve |    -   |     y    |    y   |     y
128  *  tree block logical |    -   |     -    |    -   |     -
129  *  root for resolving |    y   |     y    |    y   |     y
130  *
131  * - column 1:       we've the parent -> done
132  * - column 2, 3, 4: we use the key to find the parent
133  *
134  * on disk refs (inline or keyed)
135  * ==============================
136  *        backref type | shared | indirect | shared | indirect
137  * information         |   tree |     tree |   data |     data
138  * --------------------+--------+----------+--------+----------
139  *      parent logical |    y   |     -    |    y   |     -
140  *      key to resolve |    -   |     -    |    -   |     y
141  *  tree block logical |    y   |     y    |    y   |     y
142  *  root for resolving |    -   |     y    |    y   |     y
143  *
144  * - column 1, 3: we've the parent -> done
145  * - column 2:    we take the first key from the block to find the parent
146  *                (see __add_missing_keys)
147  * - column 4:    we use the key to find the parent
148  *
149  * additional information that's available but not required to find the parent
150  * block might help in merging entries to gain some speed.
151  */
152 
153 static int __add_prelim_ref(struct list_head *head, u64 root_id,
154 			    struct btrfs_key *key, int level,
155 			    u64 parent, u64 wanted_disk_byte, int count)
156 {
157 	struct __prelim_ref *ref;
158 
159 	/* in case we're adding delayed refs, we're holding the refs spinlock */
160 	ref = kmalloc(sizeof(*ref), GFP_ATOMIC);
161 	if (!ref)
162 		return -ENOMEM;
163 
164 	ref->root_id = root_id;
165 	if (key)
166 		ref->key_for_search = *key;
167 	else
168 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
169 
170 	ref->inode_list = NULL;
171 	ref->level = level;
172 	ref->count = count;
173 	ref->parent = parent;
174 	ref->wanted_disk_byte = wanted_disk_byte;
175 	list_add_tail(&ref->list, head);
176 
177 	return 0;
178 }
179 
180 static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
181 				struct ulist *parents, int level,
182 				struct btrfs_key *key, u64 wanted_disk_byte,
183 				const u64 *extent_item_pos)
184 {
185 	int ret;
186 	int slot = path->slots[level];
187 	struct extent_buffer *eb = path->nodes[level];
188 	struct btrfs_file_extent_item *fi;
189 	struct extent_inode_elem *eie = NULL;
190 	u64 disk_byte;
191 	u64 wanted_objectid = key->objectid;
192 
193 add_parent:
194 	if (level == 0 && extent_item_pos) {
195 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
196 		ret = check_extent_in_eb(key, eb, fi, *extent_item_pos, &eie);
197 		if (ret < 0)
198 			return ret;
199 	}
200 	ret = ulist_add(parents, eb->start, (unsigned long)eie, GFP_NOFS);
201 	if (ret < 0)
202 		return ret;
203 
204 	if (level != 0)
205 		return 0;
206 
207 	/*
208 	 * if the current leaf is full with EXTENT_DATA items, we must
209 	 * check the next one if that holds a reference as well.
210 	 * ref->count cannot be used to skip this check.
211 	 * repeat this until we don't find any additional EXTENT_DATA items.
212 	 */
213 	while (1) {
214 		eie = NULL;
215 		ret = btrfs_next_leaf(root, path);
216 		if (ret < 0)
217 			return ret;
218 		if (ret)
219 			return 0;
220 
221 		eb = path->nodes[0];
222 		for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) {
223 			btrfs_item_key_to_cpu(eb, key, slot);
224 			if (key->objectid != wanted_objectid ||
225 			    key->type != BTRFS_EXTENT_DATA_KEY)
226 				return 0;
227 			fi = btrfs_item_ptr(eb, slot,
228 						struct btrfs_file_extent_item);
229 			disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
230 			if (disk_byte == wanted_disk_byte)
231 				goto add_parent;
232 		}
233 	}
234 
235 	return 0;
236 }
237 
238 /*
239  * resolve an indirect backref in the form (root_id, key, level)
240  * to a logical address
241  */
242 static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
243 					int search_commit_root,
244 					u64 time_seq,
245 					struct __prelim_ref *ref,
246 					struct ulist *parents,
247 					const u64 *extent_item_pos)
248 {
249 	struct btrfs_path *path;
250 	struct btrfs_root *root;
251 	struct btrfs_key root_key;
252 	struct btrfs_key key = {0};
253 	struct extent_buffer *eb;
254 	int ret = 0;
255 	int root_level;
256 	int level = ref->level;
257 
258 	path = btrfs_alloc_path();
259 	if (!path)
260 		return -ENOMEM;
261 	path->search_commit_root = !!search_commit_root;
262 
263 	root_key.objectid = ref->root_id;
264 	root_key.type = BTRFS_ROOT_ITEM_KEY;
265 	root_key.offset = (u64)-1;
266 	root = btrfs_read_fs_root_no_name(fs_info, &root_key);
267 	if (IS_ERR(root)) {
268 		ret = PTR_ERR(root);
269 		goto out;
270 	}
271 
272 	rcu_read_lock();
273 	root_level = btrfs_header_level(root->node);
274 	rcu_read_unlock();
275 
276 	if (root_level + 1 == level)
277 		goto out;
278 
279 	path->lowest_level = level;
280 	ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq);
281 	pr_debug("search slot in root %llu (level %d, ref count %d) returned "
282 		 "%d for key (%llu %u %llu)\n",
283 		 (unsigned long long)ref->root_id, level, ref->count, ret,
284 		 (unsigned long long)ref->key_for_search.objectid,
285 		 ref->key_for_search.type,
286 		 (unsigned long long)ref->key_for_search.offset);
287 	if (ret < 0)
288 		goto out;
289 
290 	eb = path->nodes[level];
291 	if (!eb) {
292 		WARN_ON(1);
293 		ret = 1;
294 		goto out;
295 	}
296 
297 	if (level == 0) {
298 		if (ret == 1 && path->slots[0] >= btrfs_header_nritems(eb)) {
299 			ret = btrfs_next_leaf(root, path);
300 			if (ret)
301 				goto out;
302 			eb = path->nodes[0];
303 		}
304 
305 		btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
306 	}
307 
308 	ret = add_all_parents(root, path, parents, level, &key,
309 				ref->wanted_disk_byte, extent_item_pos);
310 out:
311 	btrfs_free_path(path);
312 	return ret;
313 }
314 
315 /*
316  * resolve all indirect backrefs from the list
317  */
318 static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
319 				   int search_commit_root, u64 time_seq,
320 				   struct list_head *head,
321 				   const u64 *extent_item_pos)
322 {
323 	int err;
324 	int ret = 0;
325 	struct __prelim_ref *ref;
326 	struct __prelim_ref *ref_safe;
327 	struct __prelim_ref *new_ref;
328 	struct ulist *parents;
329 	struct ulist_node *node;
330 	struct ulist_iterator uiter;
331 
332 	parents = ulist_alloc(GFP_NOFS);
333 	if (!parents)
334 		return -ENOMEM;
335 
336 	/*
337 	 * _safe allows us to insert directly after the current item without
338 	 * iterating over the newly inserted items.
339 	 * we're also allowed to re-assign ref during iteration.
340 	 */
341 	list_for_each_entry_safe(ref, ref_safe, head, list) {
342 		if (ref->parent)	/* already direct */
343 			continue;
344 		if (ref->count == 0)
345 			continue;
346 		err = __resolve_indirect_ref(fs_info, search_commit_root,
347 					     time_seq, ref, parents,
348 					     extent_item_pos);
349 		if (err) {
350 			if (ret == 0)
351 				ret = err;
352 			continue;
353 		}
354 
355 		/* we put the first parent into the ref at hand */
356 		ULIST_ITER_INIT(&uiter);
357 		node = ulist_next(parents, &uiter);
358 		ref->parent = node ? node->val : 0;
359 		ref->inode_list =
360 			node ? (struct extent_inode_elem *)node->aux : 0;
361 
362 		/* additional parents require new refs being added here */
363 		while ((node = ulist_next(parents, &uiter))) {
364 			new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS);
365 			if (!new_ref) {
366 				ret = -ENOMEM;
367 				break;
368 			}
369 			memcpy(new_ref, ref, sizeof(*ref));
370 			new_ref->parent = node->val;
371 			new_ref->inode_list =
372 					(struct extent_inode_elem *)node->aux;
373 			list_add(&new_ref->list, &ref->list);
374 		}
375 		ulist_reinit(parents);
376 	}
377 
378 	ulist_free(parents);
379 	return ret;
380 }
381 
382 static inline int ref_for_same_block(struct __prelim_ref *ref1,
383 				     struct __prelim_ref *ref2)
384 {
385 	if (ref1->level != ref2->level)
386 		return 0;
387 	if (ref1->root_id != ref2->root_id)
388 		return 0;
389 	if (ref1->key_for_search.type != ref2->key_for_search.type)
390 		return 0;
391 	if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
392 		return 0;
393 	if (ref1->key_for_search.offset != ref2->key_for_search.offset)
394 		return 0;
395 	if (ref1->parent != ref2->parent)
396 		return 0;
397 
398 	return 1;
399 }
400 
401 /*
402  * read tree blocks and add keys where required.
403  */
404 static int __add_missing_keys(struct btrfs_fs_info *fs_info,
405 			      struct list_head *head)
406 {
407 	struct list_head *pos;
408 	struct extent_buffer *eb;
409 
410 	list_for_each(pos, head) {
411 		struct __prelim_ref *ref;
412 		ref = list_entry(pos, struct __prelim_ref, list);
413 
414 		if (ref->parent)
415 			continue;
416 		if (ref->key_for_search.type)
417 			continue;
418 		BUG_ON(!ref->wanted_disk_byte);
419 		eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
420 				     fs_info->tree_root->leafsize, 0);
421 		BUG_ON(!eb);
422 		btrfs_tree_read_lock(eb);
423 		if (btrfs_header_level(eb) == 0)
424 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
425 		else
426 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
427 		btrfs_tree_read_unlock(eb);
428 		free_extent_buffer(eb);
429 	}
430 	return 0;
431 }
432 
433 /*
434  * merge two lists of backrefs and adjust counts accordingly
435  *
436  * mode = 1: merge identical keys, if key is set
437  *    FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
438  *           additionally, we could even add a key range for the blocks we
439  *           looked into to merge even more (-> replace unresolved refs by those
440  *           having a parent).
441  * mode = 2: merge identical parents
442  */
443 static int __merge_refs(struct list_head *head, int mode)
444 {
445 	struct list_head *pos1;
446 
447 	list_for_each(pos1, head) {
448 		struct list_head *n2;
449 		struct list_head *pos2;
450 		struct __prelim_ref *ref1;
451 
452 		ref1 = list_entry(pos1, struct __prelim_ref, list);
453 
454 		for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
455 		     pos2 = n2, n2 = pos2->next) {
456 			struct __prelim_ref *ref2;
457 			struct __prelim_ref *xchg;
458 
459 			ref2 = list_entry(pos2, struct __prelim_ref, list);
460 
461 			if (mode == 1) {
462 				if (!ref_for_same_block(ref1, ref2))
463 					continue;
464 				if (!ref1->parent && ref2->parent) {
465 					xchg = ref1;
466 					ref1 = ref2;
467 					ref2 = xchg;
468 				}
469 				ref1->count += ref2->count;
470 			} else {
471 				if (ref1->parent != ref2->parent)
472 					continue;
473 				ref1->count += ref2->count;
474 			}
475 			list_del(&ref2->list);
476 			kfree(ref2);
477 		}
478 
479 	}
480 	return 0;
481 }
482 
483 /*
484  * add all currently queued delayed refs from this head whose seq nr is
485  * smaller or equal that seq to the list
486  */
487 static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
488 			      struct list_head *prefs)
489 {
490 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
491 	struct rb_node *n = &head->node.rb_node;
492 	struct btrfs_key key;
493 	struct btrfs_key op_key = {0};
494 	int sgn;
495 	int ret = 0;
496 
497 	if (extent_op && extent_op->update_key)
498 		btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
499 
500 	while ((n = rb_prev(n))) {
501 		struct btrfs_delayed_ref_node *node;
502 		node = rb_entry(n, struct btrfs_delayed_ref_node,
503 				rb_node);
504 		if (node->bytenr != head->node.bytenr)
505 			break;
506 		WARN_ON(node->is_head);
507 
508 		if (node->seq > seq)
509 			continue;
510 
511 		switch (node->action) {
512 		case BTRFS_ADD_DELAYED_EXTENT:
513 		case BTRFS_UPDATE_DELAYED_HEAD:
514 			WARN_ON(1);
515 			continue;
516 		case BTRFS_ADD_DELAYED_REF:
517 			sgn = 1;
518 			break;
519 		case BTRFS_DROP_DELAYED_REF:
520 			sgn = -1;
521 			break;
522 		default:
523 			BUG_ON(1);
524 		}
525 		switch (node->type) {
526 		case BTRFS_TREE_BLOCK_REF_KEY: {
527 			struct btrfs_delayed_tree_ref *ref;
528 
529 			ref = btrfs_delayed_node_to_tree_ref(node);
530 			ret = __add_prelim_ref(prefs, ref->root, &op_key,
531 					       ref->level + 1, 0, node->bytenr,
532 					       node->ref_mod * sgn);
533 			break;
534 		}
535 		case BTRFS_SHARED_BLOCK_REF_KEY: {
536 			struct btrfs_delayed_tree_ref *ref;
537 
538 			ref = btrfs_delayed_node_to_tree_ref(node);
539 			ret = __add_prelim_ref(prefs, ref->root, NULL,
540 					       ref->level + 1, ref->parent,
541 					       node->bytenr,
542 					       node->ref_mod * sgn);
543 			break;
544 		}
545 		case BTRFS_EXTENT_DATA_REF_KEY: {
546 			struct btrfs_delayed_data_ref *ref;
547 			ref = btrfs_delayed_node_to_data_ref(node);
548 
549 			key.objectid = ref->objectid;
550 			key.type = BTRFS_EXTENT_DATA_KEY;
551 			key.offset = ref->offset;
552 			ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
553 					       node->bytenr,
554 					       node->ref_mod * sgn);
555 			break;
556 		}
557 		case BTRFS_SHARED_DATA_REF_KEY: {
558 			struct btrfs_delayed_data_ref *ref;
559 
560 			ref = btrfs_delayed_node_to_data_ref(node);
561 
562 			key.objectid = ref->objectid;
563 			key.type = BTRFS_EXTENT_DATA_KEY;
564 			key.offset = ref->offset;
565 			ret = __add_prelim_ref(prefs, ref->root, &key, 0,
566 					       ref->parent, node->bytenr,
567 					       node->ref_mod * sgn);
568 			break;
569 		}
570 		default:
571 			WARN_ON(1);
572 		}
573 		BUG_ON(ret);
574 	}
575 
576 	return 0;
577 }
578 
579 /*
580  * add all inline backrefs for bytenr to the list
581  */
582 static int __add_inline_refs(struct btrfs_fs_info *fs_info,
583 			     struct btrfs_path *path, u64 bytenr,
584 			     int *info_level, struct list_head *prefs)
585 {
586 	int ret = 0;
587 	int slot;
588 	struct extent_buffer *leaf;
589 	struct btrfs_key key;
590 	unsigned long ptr;
591 	unsigned long end;
592 	struct btrfs_extent_item *ei;
593 	u64 flags;
594 	u64 item_size;
595 
596 	/*
597 	 * enumerate all inline refs
598 	 */
599 	leaf = path->nodes[0];
600 	slot = path->slots[0];
601 
602 	item_size = btrfs_item_size_nr(leaf, slot);
603 	BUG_ON(item_size < sizeof(*ei));
604 
605 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
606 	flags = btrfs_extent_flags(leaf, ei);
607 
608 	ptr = (unsigned long)(ei + 1);
609 	end = (unsigned long)ei + item_size;
610 
611 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
612 		struct btrfs_tree_block_info *info;
613 
614 		info = (struct btrfs_tree_block_info *)ptr;
615 		*info_level = btrfs_tree_block_level(leaf, info);
616 		ptr += sizeof(struct btrfs_tree_block_info);
617 		BUG_ON(ptr > end);
618 	} else {
619 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
620 	}
621 
622 	while (ptr < end) {
623 		struct btrfs_extent_inline_ref *iref;
624 		u64 offset;
625 		int type;
626 
627 		iref = (struct btrfs_extent_inline_ref *)ptr;
628 		type = btrfs_extent_inline_ref_type(leaf, iref);
629 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
630 
631 		switch (type) {
632 		case BTRFS_SHARED_BLOCK_REF_KEY:
633 			ret = __add_prelim_ref(prefs, 0, NULL,
634 						*info_level + 1, offset,
635 						bytenr, 1);
636 			break;
637 		case BTRFS_SHARED_DATA_REF_KEY: {
638 			struct btrfs_shared_data_ref *sdref;
639 			int count;
640 
641 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
642 			count = btrfs_shared_data_ref_count(leaf, sdref);
643 			ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
644 					       bytenr, count);
645 			break;
646 		}
647 		case BTRFS_TREE_BLOCK_REF_KEY:
648 			ret = __add_prelim_ref(prefs, offset, NULL,
649 					       *info_level + 1, 0,
650 					       bytenr, 1);
651 			break;
652 		case BTRFS_EXTENT_DATA_REF_KEY: {
653 			struct btrfs_extent_data_ref *dref;
654 			int count;
655 			u64 root;
656 
657 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
658 			count = btrfs_extent_data_ref_count(leaf, dref);
659 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
660 								      dref);
661 			key.type = BTRFS_EXTENT_DATA_KEY;
662 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
663 			root = btrfs_extent_data_ref_root(leaf, dref);
664 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
665 					       bytenr, count);
666 			break;
667 		}
668 		default:
669 			WARN_ON(1);
670 		}
671 		BUG_ON(ret);
672 		ptr += btrfs_extent_inline_ref_size(type);
673 	}
674 
675 	return 0;
676 }
677 
678 /*
679  * add all non-inline backrefs for bytenr to the list
680  */
681 static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
682 			    struct btrfs_path *path, u64 bytenr,
683 			    int info_level, struct list_head *prefs)
684 {
685 	struct btrfs_root *extent_root = fs_info->extent_root;
686 	int ret;
687 	int slot;
688 	struct extent_buffer *leaf;
689 	struct btrfs_key key;
690 
691 	while (1) {
692 		ret = btrfs_next_item(extent_root, path);
693 		if (ret < 0)
694 			break;
695 		if (ret) {
696 			ret = 0;
697 			break;
698 		}
699 
700 		slot = path->slots[0];
701 		leaf = path->nodes[0];
702 		btrfs_item_key_to_cpu(leaf, &key, slot);
703 
704 		if (key.objectid != bytenr)
705 			break;
706 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
707 			continue;
708 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
709 			break;
710 
711 		switch (key.type) {
712 		case BTRFS_SHARED_BLOCK_REF_KEY:
713 			ret = __add_prelim_ref(prefs, 0, NULL,
714 						info_level + 1, key.offset,
715 						bytenr, 1);
716 			break;
717 		case BTRFS_SHARED_DATA_REF_KEY: {
718 			struct btrfs_shared_data_ref *sdref;
719 			int count;
720 
721 			sdref = btrfs_item_ptr(leaf, slot,
722 					      struct btrfs_shared_data_ref);
723 			count = btrfs_shared_data_ref_count(leaf, sdref);
724 			ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
725 						bytenr, count);
726 			break;
727 		}
728 		case BTRFS_TREE_BLOCK_REF_KEY:
729 			ret = __add_prelim_ref(prefs, key.offset, NULL,
730 					       info_level + 1, 0,
731 					       bytenr, 1);
732 			break;
733 		case BTRFS_EXTENT_DATA_REF_KEY: {
734 			struct btrfs_extent_data_ref *dref;
735 			int count;
736 			u64 root;
737 
738 			dref = btrfs_item_ptr(leaf, slot,
739 					      struct btrfs_extent_data_ref);
740 			count = btrfs_extent_data_ref_count(leaf, dref);
741 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
742 								      dref);
743 			key.type = BTRFS_EXTENT_DATA_KEY;
744 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
745 			root = btrfs_extent_data_ref_root(leaf, dref);
746 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
747 					       bytenr, count);
748 			break;
749 		}
750 		default:
751 			WARN_ON(1);
752 		}
753 		BUG_ON(ret);
754 	}
755 
756 	return ret;
757 }
758 
759 /*
760  * this adds all existing backrefs (inline backrefs, backrefs and delayed
761  * refs) for the given bytenr to the refs list, merges duplicates and resolves
762  * indirect refs to their parent bytenr.
763  * When roots are found, they're added to the roots list
764  *
765  * FIXME some caching might speed things up
766  */
767 static int find_parent_nodes(struct btrfs_trans_handle *trans,
768 			     struct btrfs_fs_info *fs_info, u64 bytenr,
769 			     u64 delayed_ref_seq, u64 time_seq,
770 			     struct ulist *refs, struct ulist *roots,
771 			     const u64 *extent_item_pos)
772 {
773 	struct btrfs_key key;
774 	struct btrfs_path *path;
775 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
776 	struct btrfs_delayed_ref_head *head;
777 	int info_level = 0;
778 	int ret;
779 	int search_commit_root = (trans == BTRFS_BACKREF_SEARCH_COMMIT_ROOT);
780 	struct list_head prefs_delayed;
781 	struct list_head prefs;
782 	struct __prelim_ref *ref;
783 
784 	INIT_LIST_HEAD(&prefs);
785 	INIT_LIST_HEAD(&prefs_delayed);
786 
787 	key.objectid = bytenr;
788 	key.type = BTRFS_EXTENT_ITEM_KEY;
789 	key.offset = (u64)-1;
790 
791 	path = btrfs_alloc_path();
792 	if (!path)
793 		return -ENOMEM;
794 	path->search_commit_root = !!search_commit_root;
795 
796 	/*
797 	 * grab both a lock on the path and a lock on the delayed ref head.
798 	 * We need both to get a consistent picture of how the refs look
799 	 * at a specified point in time
800 	 */
801 again:
802 	head = NULL;
803 
804 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
805 	if (ret < 0)
806 		goto out;
807 	BUG_ON(ret == 0);
808 
809 	if (trans != BTRFS_BACKREF_SEARCH_COMMIT_ROOT) {
810 		/*
811 		 * look if there are updates for this ref queued and lock the
812 		 * head
813 		 */
814 		delayed_refs = &trans->transaction->delayed_refs;
815 		spin_lock(&delayed_refs->lock);
816 		head = btrfs_find_delayed_ref_head(trans, bytenr);
817 		if (head) {
818 			if (!mutex_trylock(&head->mutex)) {
819 				atomic_inc(&head->node.refs);
820 				spin_unlock(&delayed_refs->lock);
821 
822 				btrfs_release_path(path);
823 
824 				/*
825 				 * Mutex was contended, block until it's
826 				 * released and try again
827 				 */
828 				mutex_lock(&head->mutex);
829 				mutex_unlock(&head->mutex);
830 				btrfs_put_delayed_ref(&head->node);
831 				goto again;
832 			}
833 			ret = __add_delayed_refs(head, delayed_ref_seq,
834 						 &prefs_delayed);
835 			if (ret) {
836 				spin_unlock(&delayed_refs->lock);
837 				goto out;
838 			}
839 		}
840 		spin_unlock(&delayed_refs->lock);
841 	}
842 
843 	if (path->slots[0]) {
844 		struct extent_buffer *leaf;
845 		int slot;
846 
847 		path->slots[0]--;
848 		leaf = path->nodes[0];
849 		slot = path->slots[0];
850 		btrfs_item_key_to_cpu(leaf, &key, slot);
851 		if (key.objectid == bytenr &&
852 		    key.type == BTRFS_EXTENT_ITEM_KEY) {
853 			ret = __add_inline_refs(fs_info, path, bytenr,
854 						&info_level, &prefs);
855 			if (ret)
856 				goto out;
857 			ret = __add_keyed_refs(fs_info, path, bytenr,
858 					       info_level, &prefs);
859 			if (ret)
860 				goto out;
861 		}
862 	}
863 	btrfs_release_path(path);
864 
865 	list_splice_init(&prefs_delayed, &prefs);
866 
867 	ret = __add_missing_keys(fs_info, &prefs);
868 	if (ret)
869 		goto out;
870 
871 	ret = __merge_refs(&prefs, 1);
872 	if (ret)
873 		goto out;
874 
875 	ret = __resolve_indirect_refs(fs_info, search_commit_root, time_seq,
876 				      &prefs, extent_item_pos);
877 	if (ret)
878 		goto out;
879 
880 	ret = __merge_refs(&prefs, 2);
881 	if (ret)
882 		goto out;
883 
884 	while (!list_empty(&prefs)) {
885 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
886 		list_del(&ref->list);
887 		if (ref->count < 0)
888 			WARN_ON(1);
889 		if (ref->count && ref->root_id && ref->parent == 0) {
890 			/* no parent == root of tree */
891 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
892 			BUG_ON(ret < 0);
893 		}
894 		if (ref->count && ref->parent) {
895 			struct extent_inode_elem *eie = NULL;
896 			if (extent_item_pos && !ref->inode_list) {
897 				u32 bsz;
898 				struct extent_buffer *eb;
899 				bsz = btrfs_level_size(fs_info->extent_root,
900 							info_level);
901 				eb = read_tree_block(fs_info->extent_root,
902 							   ref->parent, bsz, 0);
903 				BUG_ON(!eb);
904 				ret = find_extent_in_eb(eb, bytenr,
905 							*extent_item_pos, &eie);
906 				ref->inode_list = eie;
907 				free_extent_buffer(eb);
908 			}
909 			ret = ulist_add_merge(refs, ref->parent,
910 					      (unsigned long)ref->inode_list,
911 					      (unsigned long *)&eie, GFP_NOFS);
912 			if (!ret && extent_item_pos) {
913 				/*
914 				 * we've recorded that parent, so we must extend
915 				 * its inode list here
916 				 */
917 				BUG_ON(!eie);
918 				while (eie->next)
919 					eie = eie->next;
920 				eie->next = ref->inode_list;
921 			}
922 			BUG_ON(ret < 0);
923 		}
924 		kfree(ref);
925 	}
926 
927 out:
928 	if (head)
929 		mutex_unlock(&head->mutex);
930 	btrfs_free_path(path);
931 	while (!list_empty(&prefs)) {
932 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
933 		list_del(&ref->list);
934 		kfree(ref);
935 	}
936 	while (!list_empty(&prefs_delayed)) {
937 		ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
938 				       list);
939 		list_del(&ref->list);
940 		kfree(ref);
941 	}
942 
943 	return ret;
944 }
945 
946 static void free_leaf_list(struct ulist *blocks)
947 {
948 	struct ulist_node *node = NULL;
949 	struct extent_inode_elem *eie;
950 	struct extent_inode_elem *eie_next;
951 	struct ulist_iterator uiter;
952 
953 	ULIST_ITER_INIT(&uiter);
954 	while ((node = ulist_next(blocks, &uiter))) {
955 		if (!node->aux)
956 			continue;
957 		eie = (struct extent_inode_elem *)node->aux;
958 		for (; eie; eie = eie_next) {
959 			eie_next = eie->next;
960 			kfree(eie);
961 		}
962 		node->aux = 0;
963 	}
964 
965 	ulist_free(blocks);
966 }
967 
968 /*
969  * Finds all leafs with a reference to the specified combination of bytenr and
970  * offset. key_list_head will point to a list of corresponding keys (caller must
971  * free each list element). The leafs will be stored in the leafs ulist, which
972  * must be freed with ulist_free.
973  *
974  * returns 0 on success, <0 on error
975  */
976 static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
977 				struct btrfs_fs_info *fs_info, u64 bytenr,
978 				u64 delayed_ref_seq, u64 time_seq,
979 				struct ulist **leafs,
980 				const u64 *extent_item_pos)
981 {
982 	struct ulist *tmp;
983 	int ret;
984 
985 	tmp = ulist_alloc(GFP_NOFS);
986 	if (!tmp)
987 		return -ENOMEM;
988 	*leafs = ulist_alloc(GFP_NOFS);
989 	if (!*leafs) {
990 		ulist_free(tmp);
991 		return -ENOMEM;
992 	}
993 
994 	ret = find_parent_nodes(trans, fs_info, bytenr, delayed_ref_seq,
995 				time_seq, *leafs, tmp, extent_item_pos);
996 	ulist_free(tmp);
997 
998 	if (ret < 0 && ret != -ENOENT) {
999 		free_leaf_list(*leafs);
1000 		return ret;
1001 	}
1002 
1003 	return 0;
1004 }
1005 
1006 /*
1007  * walk all backrefs for a given extent to find all roots that reference this
1008  * extent. Walking a backref means finding all extents that reference this
1009  * extent and in turn walk the backrefs of those, too. Naturally this is a
1010  * recursive process, but here it is implemented in an iterative fashion: We
1011  * find all referencing extents for the extent in question and put them on a
1012  * list. In turn, we find all referencing extents for those, further appending
1013  * to the list. The way we iterate the list allows adding more elements after
1014  * the current while iterating. The process stops when we reach the end of the
1015  * list. Found roots are added to the roots list.
1016  *
1017  * returns 0 on success, < 0 on error.
1018  */
1019 int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
1020 				struct btrfs_fs_info *fs_info, u64 bytenr,
1021 				u64 delayed_ref_seq, u64 time_seq,
1022 				struct ulist **roots)
1023 {
1024 	struct ulist *tmp;
1025 	struct ulist_node *node = NULL;
1026 	struct ulist_iterator uiter;
1027 	int ret;
1028 
1029 	tmp = ulist_alloc(GFP_NOFS);
1030 	if (!tmp)
1031 		return -ENOMEM;
1032 	*roots = ulist_alloc(GFP_NOFS);
1033 	if (!*roots) {
1034 		ulist_free(tmp);
1035 		return -ENOMEM;
1036 	}
1037 
1038 	ULIST_ITER_INIT(&uiter);
1039 	while (1) {
1040 		ret = find_parent_nodes(trans, fs_info, bytenr, delayed_ref_seq,
1041 					time_seq, tmp, *roots, NULL);
1042 		if (ret < 0 && ret != -ENOENT) {
1043 			ulist_free(tmp);
1044 			ulist_free(*roots);
1045 			return ret;
1046 		}
1047 		node = ulist_next(tmp, &uiter);
1048 		if (!node)
1049 			break;
1050 		bytenr = node->val;
1051 	}
1052 
1053 	ulist_free(tmp);
1054 	return 0;
1055 }
1056 
1057 
1058 static int __inode_info(u64 inum, u64 ioff, u8 key_type,
1059 			struct btrfs_root *fs_root, struct btrfs_path *path,
1060 			struct btrfs_key *found_key)
1061 {
1062 	int ret;
1063 	struct btrfs_key key;
1064 	struct extent_buffer *eb;
1065 
1066 	key.type = key_type;
1067 	key.objectid = inum;
1068 	key.offset = ioff;
1069 
1070 	ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1071 	if (ret < 0)
1072 		return ret;
1073 
1074 	eb = path->nodes[0];
1075 	if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1076 		ret = btrfs_next_leaf(fs_root, path);
1077 		if (ret)
1078 			return ret;
1079 		eb = path->nodes[0];
1080 	}
1081 
1082 	btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1083 	if (found_key->type != key.type || found_key->objectid != key.objectid)
1084 		return 1;
1085 
1086 	return 0;
1087 }
1088 
1089 /*
1090  * this makes the path point to (inum INODE_ITEM ioff)
1091  */
1092 int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
1093 			struct btrfs_path *path)
1094 {
1095 	struct btrfs_key key;
1096 	return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path,
1097 				&key);
1098 }
1099 
1100 static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
1101 				struct btrfs_path *path,
1102 				struct btrfs_key *found_key)
1103 {
1104 	return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path,
1105 				found_key);
1106 }
1107 
1108 /*
1109  * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
1110  * of the path are separated by '/' and the path is guaranteed to be
1111  * 0-terminated. the path is only given within the current file system.
1112  * Therefore, it never starts with a '/'. the caller is responsible to provide
1113  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
1114  * the start point of the resulting string is returned. this pointer is within
1115  * dest, normally.
1116  * in case the path buffer would overflow, the pointer is decremented further
1117  * as if output was written to the buffer, though no more output is actually
1118  * generated. that way, the caller can determine how much space would be
1119  * required for the path to fit into the buffer. in that case, the returned
1120  * value will be smaller than dest. callers must check this!
1121  */
1122 static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1123 				struct btrfs_inode_ref *iref,
1124 				struct extent_buffer *eb_in, u64 parent,
1125 				char *dest, u32 size)
1126 {
1127 	u32 len;
1128 	int slot;
1129 	u64 next_inum;
1130 	int ret;
1131 	s64 bytes_left = size - 1;
1132 	struct extent_buffer *eb = eb_in;
1133 	struct btrfs_key found_key;
1134 	int leave_spinning = path->leave_spinning;
1135 
1136 	if (bytes_left >= 0)
1137 		dest[bytes_left] = '\0';
1138 
1139 	path->leave_spinning = 1;
1140 	while (1) {
1141 		len = btrfs_inode_ref_name_len(eb, iref);
1142 		bytes_left -= len;
1143 		if (bytes_left >= 0)
1144 			read_extent_buffer(eb, dest + bytes_left,
1145 						(unsigned long)(iref + 1), len);
1146 		if (eb != eb_in) {
1147 			btrfs_tree_read_unlock_blocking(eb);
1148 			free_extent_buffer(eb);
1149 		}
1150 		ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
1151 		if (ret > 0)
1152 			ret = -ENOENT;
1153 		if (ret)
1154 			break;
1155 		next_inum = found_key.offset;
1156 
1157 		/* regular exit ahead */
1158 		if (parent == next_inum)
1159 			break;
1160 
1161 		slot = path->slots[0];
1162 		eb = path->nodes[0];
1163 		/* make sure we can use eb after releasing the path */
1164 		if (eb != eb_in) {
1165 			atomic_inc(&eb->refs);
1166 			btrfs_tree_read_lock(eb);
1167 			btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1168 		}
1169 		btrfs_release_path(path);
1170 
1171 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1172 		parent = next_inum;
1173 		--bytes_left;
1174 		if (bytes_left >= 0)
1175 			dest[bytes_left] = '/';
1176 	}
1177 
1178 	btrfs_release_path(path);
1179 	path->leave_spinning = leave_spinning;
1180 
1181 	if (ret)
1182 		return ERR_PTR(ret);
1183 
1184 	return dest + bytes_left;
1185 }
1186 
1187 /*
1188  * this makes the path point to (logical EXTENT_ITEM *)
1189  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1190  * tree blocks and <0 on error.
1191  */
1192 int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
1193 			struct btrfs_path *path, struct btrfs_key *found_key)
1194 {
1195 	int ret;
1196 	u64 flags;
1197 	u32 item_size;
1198 	struct extent_buffer *eb;
1199 	struct btrfs_extent_item *ei;
1200 	struct btrfs_key key;
1201 
1202 	key.type = BTRFS_EXTENT_ITEM_KEY;
1203 	key.objectid = logical;
1204 	key.offset = (u64)-1;
1205 
1206 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1207 	if (ret < 0)
1208 		return ret;
1209 	ret = btrfs_previous_item(fs_info->extent_root, path,
1210 					0, BTRFS_EXTENT_ITEM_KEY);
1211 	if (ret < 0)
1212 		return ret;
1213 
1214 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1215 	if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
1216 	    found_key->objectid > logical ||
1217 	    found_key->objectid + found_key->offset <= logical) {
1218 		pr_debug("logical %llu is not within any extent\n",
1219 			 (unsigned long long)logical);
1220 		return -ENOENT;
1221 	}
1222 
1223 	eb = path->nodes[0];
1224 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1225 	BUG_ON(item_size < sizeof(*ei));
1226 
1227 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1228 	flags = btrfs_extent_flags(eb, ei);
1229 
1230 	pr_debug("logical %llu is at position %llu within the extent (%llu "
1231 		 "EXTENT_ITEM %llu) flags %#llx size %u\n",
1232 		 (unsigned long long)logical,
1233 		 (unsigned long long)(logical - found_key->objectid),
1234 		 (unsigned long long)found_key->objectid,
1235 		 (unsigned long long)found_key->offset,
1236 		 (unsigned long long)flags, item_size);
1237 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1238 		return BTRFS_EXTENT_FLAG_TREE_BLOCK;
1239 	if (flags & BTRFS_EXTENT_FLAG_DATA)
1240 		return BTRFS_EXTENT_FLAG_DATA;
1241 
1242 	return -EIO;
1243 }
1244 
1245 /*
1246  * helper function to iterate extent inline refs. ptr must point to a 0 value
1247  * for the first call and may be modified. it is used to track state.
1248  * if more refs exist, 0 is returned and the next call to
1249  * __get_extent_inline_ref must pass the modified ptr parameter to get the
1250  * next ref. after the last ref was processed, 1 is returned.
1251  * returns <0 on error
1252  */
1253 static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
1254 				struct btrfs_extent_item *ei, u32 item_size,
1255 				struct btrfs_extent_inline_ref **out_eiref,
1256 				int *out_type)
1257 {
1258 	unsigned long end;
1259 	u64 flags;
1260 	struct btrfs_tree_block_info *info;
1261 
1262 	if (!*ptr) {
1263 		/* first call */
1264 		flags = btrfs_extent_flags(eb, ei);
1265 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1266 			info = (struct btrfs_tree_block_info *)(ei + 1);
1267 			*out_eiref =
1268 				(struct btrfs_extent_inline_ref *)(info + 1);
1269 		} else {
1270 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1271 		}
1272 		*ptr = (unsigned long)*out_eiref;
1273 		if ((void *)*ptr >= (void *)ei + item_size)
1274 			return -ENOENT;
1275 	}
1276 
1277 	end = (unsigned long)ei + item_size;
1278 	*out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
1279 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1280 
1281 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1282 	WARN_ON(*ptr > end);
1283 	if (*ptr == end)
1284 		return 1; /* last */
1285 
1286 	return 0;
1287 }
1288 
1289 /*
1290  * reads the tree block backref for an extent. tree level and root are returned
1291  * through out_level and out_root. ptr must point to a 0 value for the first
1292  * call and may be modified (see __get_extent_inline_ref comment).
1293  * returns 0 if data was provided, 1 if there was no more data to provide or
1294  * <0 on error.
1295  */
1296 int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
1297 				struct btrfs_extent_item *ei, u32 item_size,
1298 				u64 *out_root, u8 *out_level)
1299 {
1300 	int ret;
1301 	int type;
1302 	struct btrfs_tree_block_info *info;
1303 	struct btrfs_extent_inline_ref *eiref;
1304 
1305 	if (*ptr == (unsigned long)-1)
1306 		return 1;
1307 
1308 	while (1) {
1309 		ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
1310 						&eiref, &type);
1311 		if (ret < 0)
1312 			return ret;
1313 
1314 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1315 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1316 			break;
1317 
1318 		if (ret == 1)
1319 			return 1;
1320 	}
1321 
1322 	/* we can treat both ref types equally here */
1323 	info = (struct btrfs_tree_block_info *)(ei + 1);
1324 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1325 	*out_level = btrfs_tree_block_level(eb, info);
1326 
1327 	if (ret == 1)
1328 		*ptr = (unsigned long)-1;
1329 
1330 	return 0;
1331 }
1332 
1333 static int iterate_leaf_refs(struct extent_inode_elem *inode_list,
1334 				u64 root, u64 extent_item_objectid,
1335 				iterate_extent_inodes_t *iterate, void *ctx)
1336 {
1337 	struct extent_inode_elem *eie;
1338 	int ret = 0;
1339 
1340 	for (eie = inode_list; eie; eie = eie->next) {
1341 		pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
1342 			 "root %llu\n", extent_item_objectid,
1343 			 eie->inum, eie->offset, root);
1344 		ret = iterate(eie->inum, eie->offset, root, ctx);
1345 		if (ret) {
1346 			pr_debug("stopping iteration for %llu due to ret=%d\n",
1347 				 extent_item_objectid, ret);
1348 			break;
1349 		}
1350 	}
1351 
1352 	return ret;
1353 }
1354 
1355 /*
1356  * calls iterate() for every inode that references the extent identified by
1357  * the given parameters.
1358  * when the iterator function returns a non-zero value, iteration stops.
1359  */
1360 int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
1361 				u64 extent_item_objectid, u64 extent_item_pos,
1362 				int search_commit_root,
1363 				iterate_extent_inodes_t *iterate, void *ctx)
1364 {
1365 	int ret;
1366 	struct list_head data_refs = LIST_HEAD_INIT(data_refs);
1367 	struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
1368 	struct btrfs_trans_handle *trans;
1369 	struct ulist *refs = NULL;
1370 	struct ulist *roots = NULL;
1371 	struct ulist_node *ref_node = NULL;
1372 	struct ulist_node *root_node = NULL;
1373 	struct seq_list seq_elem = {};
1374 	struct seq_list tree_mod_seq_elem = {};
1375 	struct ulist_iterator ref_uiter;
1376 	struct ulist_iterator root_uiter;
1377 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
1378 
1379 	pr_debug("resolving all inodes for extent %llu\n",
1380 			extent_item_objectid);
1381 
1382 	if (search_commit_root) {
1383 		trans = BTRFS_BACKREF_SEARCH_COMMIT_ROOT;
1384 	} else {
1385 		trans = btrfs_join_transaction(fs_info->extent_root);
1386 		if (IS_ERR(trans))
1387 			return PTR_ERR(trans);
1388 
1389 		delayed_refs = &trans->transaction->delayed_refs;
1390 		spin_lock(&delayed_refs->lock);
1391 		btrfs_get_delayed_seq(delayed_refs, &seq_elem);
1392 		spin_unlock(&delayed_refs->lock);
1393 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1394 	}
1395 
1396 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1397 				   seq_elem.seq, tree_mod_seq_elem.seq, &refs,
1398 				   &extent_item_pos);
1399 	if (ret)
1400 		goto out;
1401 
1402 	ULIST_ITER_INIT(&ref_uiter);
1403 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
1404 		ret = btrfs_find_all_roots(trans, fs_info, ref_node->val,
1405 						seq_elem.seq,
1406 						tree_mod_seq_elem.seq, &roots);
1407 		if (ret)
1408 			break;
1409 		ULIST_ITER_INIT(&root_uiter);
1410 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
1411 			pr_debug("root %llu references leaf %llu, data list "
1412 				 "%#lx\n", root_node->val, ref_node->val,
1413 				 ref_node->aux);
1414 			ret = iterate_leaf_refs(
1415 				(struct extent_inode_elem *)ref_node->aux,
1416 				root_node->val, extent_item_objectid,
1417 				iterate, ctx);
1418 		}
1419 		ulist_free(roots);
1420 		roots = NULL;
1421 	}
1422 
1423 	free_leaf_list(refs);
1424 	ulist_free(roots);
1425 out:
1426 	if (!search_commit_root) {
1427 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
1428 		btrfs_put_delayed_seq(delayed_refs, &seq_elem);
1429 		btrfs_end_transaction(trans, fs_info->extent_root);
1430 	}
1431 
1432 	return ret;
1433 }
1434 
1435 int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1436 				struct btrfs_path *path,
1437 				iterate_extent_inodes_t *iterate, void *ctx)
1438 {
1439 	int ret;
1440 	u64 extent_item_pos;
1441 	struct btrfs_key found_key;
1442 	int search_commit_root = path->search_commit_root;
1443 
1444 	ret = extent_from_logical(fs_info, logical, path,
1445 					&found_key);
1446 	btrfs_release_path(path);
1447 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1448 		ret = -EINVAL;
1449 	if (ret < 0)
1450 		return ret;
1451 
1452 	extent_item_pos = logical - found_key.objectid;
1453 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
1454 					extent_item_pos, search_commit_root,
1455 					iterate, ctx);
1456 
1457 	return ret;
1458 }
1459 
1460 static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
1461 				struct btrfs_path *path,
1462 				iterate_irefs_t *iterate, void *ctx)
1463 {
1464 	int ret = 0;
1465 	int slot;
1466 	u32 cur;
1467 	u32 len;
1468 	u32 name_len;
1469 	u64 parent = 0;
1470 	int found = 0;
1471 	struct extent_buffer *eb;
1472 	struct btrfs_item *item;
1473 	struct btrfs_inode_ref *iref;
1474 	struct btrfs_key found_key;
1475 
1476 	while (!ret) {
1477 		path->leave_spinning = 1;
1478 		ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
1479 					&found_key);
1480 		if (ret < 0)
1481 			break;
1482 		if (ret) {
1483 			ret = found ? 0 : -ENOENT;
1484 			break;
1485 		}
1486 		++found;
1487 
1488 		parent = found_key.offset;
1489 		slot = path->slots[0];
1490 		eb = path->nodes[0];
1491 		/* make sure we can use eb after releasing the path */
1492 		atomic_inc(&eb->refs);
1493 		btrfs_tree_read_lock(eb);
1494 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1495 		btrfs_release_path(path);
1496 
1497 		item = btrfs_item_nr(eb, slot);
1498 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1499 
1500 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1501 			name_len = btrfs_inode_ref_name_len(eb, iref);
1502 			/* path must be released before calling iterate()! */
1503 			pr_debug("following ref at offset %u for inode %llu in "
1504 				 "tree %llu\n", cur,
1505 				 (unsigned long long)found_key.objectid,
1506 				 (unsigned long long)fs_root->objectid);
1507 			ret = iterate(parent, iref, eb, ctx);
1508 			if (ret)
1509 				break;
1510 			len = sizeof(*iref) + name_len;
1511 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
1512 		}
1513 		btrfs_tree_read_unlock_blocking(eb);
1514 		free_extent_buffer(eb);
1515 	}
1516 
1517 	btrfs_release_path(path);
1518 
1519 	return ret;
1520 }
1521 
1522 /*
1523  * returns 0 if the path could be dumped (probably truncated)
1524  * returns <0 in case of an error
1525  */
1526 static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
1527 				struct extent_buffer *eb, void *ctx)
1528 {
1529 	struct inode_fs_paths *ipath = ctx;
1530 	char *fspath;
1531 	char *fspath_min;
1532 	int i = ipath->fspath->elem_cnt;
1533 	const int s_ptr = sizeof(char *);
1534 	u32 bytes_left;
1535 
1536 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
1537 					ipath->fspath->bytes_left - s_ptr : 0;
1538 
1539 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
1540 	fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
1541 				inum, fspath_min, bytes_left);
1542 	if (IS_ERR(fspath))
1543 		return PTR_ERR(fspath);
1544 
1545 	if (fspath > fspath_min) {
1546 		pr_debug("path resolved: %s\n", fspath);
1547 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
1548 		++ipath->fspath->elem_cnt;
1549 		ipath->fspath->bytes_left = fspath - fspath_min;
1550 	} else {
1551 		pr_debug("missed path, not enough space. missing bytes: %lu, "
1552 			 "constructed so far: %s\n",
1553 			 (unsigned long)(fspath_min - fspath), fspath_min);
1554 		++ipath->fspath->elem_missed;
1555 		ipath->fspath->bytes_missing += fspath_min - fspath;
1556 		ipath->fspath->bytes_left = 0;
1557 	}
1558 
1559 	return 0;
1560 }
1561 
1562 /*
1563  * this dumps all file system paths to the inode into the ipath struct, provided
1564  * is has been created large enough. each path is zero-terminated and accessed
1565  * from ipath->fspath->val[i].
1566  * when it returns, there are ipath->fspath->elem_cnt number of paths available
1567  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
1568  * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1569  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1570  * have been needed to return all paths.
1571  */
1572 int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
1573 {
1574 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
1575 				inode_to_path, ipath);
1576 }
1577 
1578 struct btrfs_data_container *init_data_container(u32 total_bytes)
1579 {
1580 	struct btrfs_data_container *data;
1581 	size_t alloc_bytes;
1582 
1583 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
1584 	data = kmalloc(alloc_bytes, GFP_NOFS);
1585 	if (!data)
1586 		return ERR_PTR(-ENOMEM);
1587 
1588 	if (total_bytes >= sizeof(*data)) {
1589 		data->bytes_left = total_bytes - sizeof(*data);
1590 		data->bytes_missing = 0;
1591 	} else {
1592 		data->bytes_missing = sizeof(*data) - total_bytes;
1593 		data->bytes_left = 0;
1594 	}
1595 
1596 	data->elem_cnt = 0;
1597 	data->elem_missed = 0;
1598 
1599 	return data;
1600 }
1601 
1602 /*
1603  * allocates space to return multiple file system paths for an inode.
1604  * total_bytes to allocate are passed, note that space usable for actual path
1605  * information will be total_bytes - sizeof(struct inode_fs_paths).
1606  * the returned pointer must be freed with free_ipath() in the end.
1607  */
1608 struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
1609 					struct btrfs_path *path)
1610 {
1611 	struct inode_fs_paths *ifp;
1612 	struct btrfs_data_container *fspath;
1613 
1614 	fspath = init_data_container(total_bytes);
1615 	if (IS_ERR(fspath))
1616 		return (void *)fspath;
1617 
1618 	ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
1619 	if (!ifp) {
1620 		kfree(fspath);
1621 		return ERR_PTR(-ENOMEM);
1622 	}
1623 
1624 	ifp->btrfs_path = path;
1625 	ifp->fspath = fspath;
1626 	ifp->fs_root = fs_root;
1627 
1628 	return ifp;
1629 }
1630 
1631 void free_ipath(struct inode_fs_paths *ipath)
1632 {
1633 	if (!ipath)
1634 		return;
1635 	kfree(ipath->fspath);
1636 	kfree(ipath);
1637 }
1638