xref: /openbmc/linux/fs/btrfs/backref.c (revision acdf898de8903f50bb10bbce4b774432bcd63c85)
1a542ad1bSJan Schmidt /*
2a542ad1bSJan Schmidt  * Copyright (C) 2011 STRATO.  All rights reserved.
3a542ad1bSJan Schmidt  *
4a542ad1bSJan Schmidt  * This program is free software; you can redistribute it and/or
5a542ad1bSJan Schmidt  * modify it under the terms of the GNU General Public
6a542ad1bSJan Schmidt  * License v2 as published by the Free Software Foundation.
7a542ad1bSJan Schmidt  *
8a542ad1bSJan Schmidt  * This program is distributed in the hope that it will be useful,
9a542ad1bSJan Schmidt  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10a542ad1bSJan Schmidt  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11a542ad1bSJan Schmidt  * General Public License for more details.
12a542ad1bSJan Schmidt  *
13a542ad1bSJan Schmidt  * You should have received a copy of the GNU General Public
14a542ad1bSJan Schmidt  * License along with this program; if not, write to the
15a542ad1bSJan Schmidt  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16a542ad1bSJan Schmidt  * Boston, MA 021110-1307, USA.
17a542ad1bSJan Schmidt  */
18a542ad1bSJan Schmidt 
19425d17a2SLiu Bo #include <linux/vmalloc.h>
20a542ad1bSJan Schmidt #include "ctree.h"
21a542ad1bSJan Schmidt #include "disk-io.h"
22a542ad1bSJan Schmidt #include "backref.h"
238da6d581SJan Schmidt #include "ulist.h"
248da6d581SJan Schmidt #include "transaction.h"
258da6d581SJan Schmidt #include "delayed-ref.h"
26b916a59aSJan Schmidt #include "locking.h"
27a542ad1bSJan Schmidt 
28dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */
29dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6
30dc046b10SJosef Bacik 
31976b1908SJan Schmidt struct extent_inode_elem {
32976b1908SJan Schmidt 	u64 inum;
33976b1908SJan Schmidt 	u64 offset;
34976b1908SJan Schmidt 	struct extent_inode_elem *next;
35976b1908SJan Schmidt };
36976b1908SJan Schmidt 
37976b1908SJan Schmidt static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb,
38976b1908SJan Schmidt 				struct btrfs_file_extent_item *fi,
39976b1908SJan Schmidt 				u64 extent_item_pos,
40976b1908SJan Schmidt 				struct extent_inode_elem **eie)
41976b1908SJan Schmidt {
428ca15e05SJosef Bacik 	u64 offset = 0;
438ca15e05SJosef Bacik 	struct extent_inode_elem *e;
448ca15e05SJosef Bacik 
458ca15e05SJosef Bacik 	if (!btrfs_file_extent_compression(eb, fi) &&
468ca15e05SJosef Bacik 	    !btrfs_file_extent_encryption(eb, fi) &&
478ca15e05SJosef Bacik 	    !btrfs_file_extent_other_encoding(eb, fi)) {
48976b1908SJan Schmidt 		u64 data_offset;
49976b1908SJan Schmidt 		u64 data_len;
50976b1908SJan Schmidt 
51976b1908SJan Schmidt 		data_offset = btrfs_file_extent_offset(eb, fi);
52976b1908SJan Schmidt 		data_len = btrfs_file_extent_num_bytes(eb, fi);
53976b1908SJan Schmidt 
54976b1908SJan Schmidt 		if (extent_item_pos < data_offset ||
55976b1908SJan Schmidt 		    extent_item_pos >= data_offset + data_len)
56976b1908SJan Schmidt 			return 1;
578ca15e05SJosef Bacik 		offset = extent_item_pos - data_offset;
588ca15e05SJosef Bacik 	}
59976b1908SJan Schmidt 
60976b1908SJan Schmidt 	e = kmalloc(sizeof(*e), GFP_NOFS);
61976b1908SJan Schmidt 	if (!e)
62976b1908SJan Schmidt 		return -ENOMEM;
63976b1908SJan Schmidt 
64976b1908SJan Schmidt 	e->next = *eie;
65976b1908SJan Schmidt 	e->inum = key->objectid;
668ca15e05SJosef Bacik 	e->offset = key->offset + offset;
67976b1908SJan Schmidt 	*eie = e;
68976b1908SJan Schmidt 
69976b1908SJan Schmidt 	return 0;
70976b1908SJan Schmidt }
71976b1908SJan Schmidt 
72f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie)
73f05c4746SWang Shilong {
74f05c4746SWang Shilong 	struct extent_inode_elem *eie_next;
75f05c4746SWang Shilong 
76f05c4746SWang Shilong 	for (; eie; eie = eie_next) {
77f05c4746SWang Shilong 		eie_next = eie->next;
78f05c4746SWang Shilong 		kfree(eie);
79f05c4746SWang Shilong 	}
80f05c4746SWang Shilong }
81f05c4746SWang Shilong 
82976b1908SJan Schmidt static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte,
83976b1908SJan Schmidt 				u64 extent_item_pos,
84976b1908SJan Schmidt 				struct extent_inode_elem **eie)
85976b1908SJan Schmidt {
86976b1908SJan Schmidt 	u64 disk_byte;
87976b1908SJan Schmidt 	struct btrfs_key key;
88976b1908SJan Schmidt 	struct btrfs_file_extent_item *fi;
89976b1908SJan Schmidt 	int slot;
90976b1908SJan Schmidt 	int nritems;
91976b1908SJan Schmidt 	int extent_type;
92976b1908SJan Schmidt 	int ret;
93976b1908SJan Schmidt 
94976b1908SJan Schmidt 	/*
95976b1908SJan Schmidt 	 * from the shared data ref, we only have the leaf but we need
96976b1908SJan Schmidt 	 * the key. thus, we must look into all items and see that we
97976b1908SJan Schmidt 	 * find one (some) with a reference to our extent item.
98976b1908SJan Schmidt 	 */
99976b1908SJan Schmidt 	nritems = btrfs_header_nritems(eb);
100976b1908SJan Schmidt 	for (slot = 0; slot < nritems; ++slot) {
101976b1908SJan Schmidt 		btrfs_item_key_to_cpu(eb, &key, slot);
102976b1908SJan Schmidt 		if (key.type != BTRFS_EXTENT_DATA_KEY)
103976b1908SJan Schmidt 			continue;
104976b1908SJan Schmidt 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
105976b1908SJan Schmidt 		extent_type = btrfs_file_extent_type(eb, fi);
106976b1908SJan Schmidt 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
107976b1908SJan Schmidt 			continue;
108976b1908SJan Schmidt 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
109976b1908SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
110976b1908SJan Schmidt 		if (disk_byte != wanted_disk_byte)
111976b1908SJan Schmidt 			continue;
112976b1908SJan Schmidt 
113976b1908SJan Schmidt 		ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
114976b1908SJan Schmidt 		if (ret < 0)
115976b1908SJan Schmidt 			return ret;
116976b1908SJan Schmidt 	}
117976b1908SJan Schmidt 
118976b1908SJan Schmidt 	return 0;
119976b1908SJan Schmidt }
120976b1908SJan Schmidt 
1218da6d581SJan Schmidt /*
1228da6d581SJan Schmidt  * this structure records all encountered refs on the way up to the root
1238da6d581SJan Schmidt  */
1248da6d581SJan Schmidt struct __prelim_ref {
1258da6d581SJan Schmidt 	struct list_head list;
1268da6d581SJan Schmidt 	u64 root_id;
127d5c88b73SJan Schmidt 	struct btrfs_key key_for_search;
1288da6d581SJan Schmidt 	int level;
1298da6d581SJan Schmidt 	int count;
1303301958bSJan Schmidt 	struct extent_inode_elem *inode_list;
1318da6d581SJan Schmidt 	u64 parent;
1328da6d581SJan Schmidt 	u64 wanted_disk_byte;
1338da6d581SJan Schmidt };
1348da6d581SJan Schmidt 
135b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache;
136b9e9a6cbSWang Shilong 
137b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void)
138b9e9a6cbSWang Shilong {
139b9e9a6cbSWang Shilong 	btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
140b9e9a6cbSWang Shilong 					sizeof(struct __prelim_ref),
141b9e9a6cbSWang Shilong 					0,
142b9e9a6cbSWang Shilong 					SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
143b9e9a6cbSWang Shilong 					NULL);
144b9e9a6cbSWang Shilong 	if (!btrfs_prelim_ref_cache)
145b9e9a6cbSWang Shilong 		return -ENOMEM;
146b9e9a6cbSWang Shilong 	return 0;
147b9e9a6cbSWang Shilong }
148b9e9a6cbSWang Shilong 
149b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void)
150b9e9a6cbSWang Shilong {
151b9e9a6cbSWang Shilong 	if (btrfs_prelim_ref_cache)
152b9e9a6cbSWang Shilong 		kmem_cache_destroy(btrfs_prelim_ref_cache);
153b9e9a6cbSWang Shilong }
154b9e9a6cbSWang Shilong 
155d5c88b73SJan Schmidt /*
156d5c88b73SJan Schmidt  * the rules for all callers of this function are:
157d5c88b73SJan Schmidt  * - obtaining the parent is the goal
158d5c88b73SJan Schmidt  * - if you add a key, you must know that it is a correct key
159d5c88b73SJan Schmidt  * - if you cannot add the parent or a correct key, then we will look into the
160d5c88b73SJan Schmidt  *   block later to set a correct key
161d5c88b73SJan Schmidt  *
162d5c88b73SJan Schmidt  * delayed refs
163d5c88b73SJan Schmidt  * ============
164d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
165d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
166d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
167d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    -   |     -
168d5c88b73SJan Schmidt  *      key to resolve |    -   |     y    |    y   |     y
169d5c88b73SJan Schmidt  *  tree block logical |    -   |     -    |    -   |     -
170d5c88b73SJan Schmidt  *  root for resolving |    y   |     y    |    y   |     y
171d5c88b73SJan Schmidt  *
172d5c88b73SJan Schmidt  * - column 1:       we've the parent -> done
173d5c88b73SJan Schmidt  * - column 2, 3, 4: we use the key to find the parent
174d5c88b73SJan Schmidt  *
175d5c88b73SJan Schmidt  * on disk refs (inline or keyed)
176d5c88b73SJan Schmidt  * ==============================
177d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
178d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
179d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
180d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    y   |     -
181d5c88b73SJan Schmidt  *      key to resolve |    -   |     -    |    -   |     y
182d5c88b73SJan Schmidt  *  tree block logical |    y   |     y    |    y   |     y
183d5c88b73SJan Schmidt  *  root for resolving |    -   |     y    |    y   |     y
184d5c88b73SJan Schmidt  *
185d5c88b73SJan Schmidt  * - column 1, 3: we've the parent -> done
186d5c88b73SJan Schmidt  * - column 2:    we take the first key from the block to find the parent
187d5c88b73SJan Schmidt  *                (see __add_missing_keys)
188d5c88b73SJan Schmidt  * - column 4:    we use the key to find the parent
189d5c88b73SJan Schmidt  *
190d5c88b73SJan Schmidt  * additional information that's available but not required to find the parent
191d5c88b73SJan Schmidt  * block might help in merging entries to gain some speed.
192d5c88b73SJan Schmidt  */
193d5c88b73SJan Schmidt 
1948da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id,
195d5c88b73SJan Schmidt 			    struct btrfs_key *key, int level,
196742916b8SWang Shilong 			    u64 parent, u64 wanted_disk_byte, int count,
197742916b8SWang Shilong 			    gfp_t gfp_mask)
1988da6d581SJan Schmidt {
1998da6d581SJan Schmidt 	struct __prelim_ref *ref;
2008da6d581SJan Schmidt 
20148ec4736SLiu Bo 	if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
20248ec4736SLiu Bo 		return 0;
20348ec4736SLiu Bo 
204b9e9a6cbSWang Shilong 	ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
2058da6d581SJan Schmidt 	if (!ref)
2068da6d581SJan Schmidt 		return -ENOMEM;
2078da6d581SJan Schmidt 
2088da6d581SJan Schmidt 	ref->root_id = root_id;
209d6589101SFilipe Manana 	if (key) {
210d5c88b73SJan Schmidt 		ref->key_for_search = *key;
211d6589101SFilipe Manana 		/*
212d6589101SFilipe Manana 		 * We can often find data backrefs with an offset that is too
213d6589101SFilipe Manana 		 * large (>= LLONG_MAX, maximum allowed file offset) due to
214d6589101SFilipe Manana 		 * underflows when subtracting a file's offset with the data
215d6589101SFilipe Manana 		 * offset of its corresponding extent data item. This can
216d6589101SFilipe Manana 		 * happen for example in the clone ioctl.
217d6589101SFilipe Manana 		 * So if we detect such case we set the search key's offset to
218d6589101SFilipe Manana 		 * zero to make sure we will find the matching file extent item
219d6589101SFilipe Manana 		 * at add_all_parents(), otherwise we will miss it because the
220d6589101SFilipe Manana 		 * offset taken form the backref is much larger then the offset
221d6589101SFilipe Manana 		 * of the file extent item. This can make us scan a very large
222d6589101SFilipe Manana 		 * number of file extent items, but at least it will not make
223d6589101SFilipe Manana 		 * us miss any.
224d6589101SFilipe Manana 		 * This is an ugly workaround for a behaviour that should have
225d6589101SFilipe Manana 		 * never existed, but it does and a fix for the clone ioctl
226d6589101SFilipe Manana 		 * would touch a lot of places, cause backwards incompatibility
227d6589101SFilipe Manana 		 * and would not fix the problem for extents cloned with older
228d6589101SFilipe Manana 		 * kernels.
229d6589101SFilipe Manana 		 */
230d6589101SFilipe Manana 		if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
231d6589101SFilipe Manana 		    ref->key_for_search.offset >= LLONG_MAX)
232d6589101SFilipe Manana 			ref->key_for_search.offset = 0;
233d6589101SFilipe Manana 	} else {
234d5c88b73SJan Schmidt 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
235d6589101SFilipe Manana 	}
2368da6d581SJan Schmidt 
2373301958bSJan Schmidt 	ref->inode_list = NULL;
2388da6d581SJan Schmidt 	ref->level = level;
2398da6d581SJan Schmidt 	ref->count = count;
2408da6d581SJan Schmidt 	ref->parent = parent;
2418da6d581SJan Schmidt 	ref->wanted_disk_byte = wanted_disk_byte;
2428da6d581SJan Schmidt 	list_add_tail(&ref->list, head);
2438da6d581SJan Schmidt 
2448da6d581SJan Schmidt 	return 0;
2458da6d581SJan Schmidt }
2468da6d581SJan Schmidt 
2478da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
2487ef81ac8SJosef Bacik 			   struct ulist *parents, struct __prelim_ref *ref,
24944853868SJosef Bacik 			   int level, u64 time_seq, const u64 *extent_item_pos,
25044853868SJosef Bacik 			   u64 total_refs)
2518da6d581SJan Schmidt {
25269bca40dSAlexander Block 	int ret = 0;
25369bca40dSAlexander Block 	int slot;
25469bca40dSAlexander Block 	struct extent_buffer *eb;
25569bca40dSAlexander Block 	struct btrfs_key key;
2567ef81ac8SJosef Bacik 	struct btrfs_key *key_for_search = &ref->key_for_search;
2578da6d581SJan Schmidt 	struct btrfs_file_extent_item *fi;
258ed8c4913SJosef Bacik 	struct extent_inode_elem *eie = NULL, *old = NULL;
2598da6d581SJan Schmidt 	u64 disk_byte;
2607ef81ac8SJosef Bacik 	u64 wanted_disk_byte = ref->wanted_disk_byte;
2617ef81ac8SJosef Bacik 	u64 count = 0;
2628da6d581SJan Schmidt 
26369bca40dSAlexander Block 	if (level != 0) {
26469bca40dSAlexander Block 		eb = path->nodes[level];
26569bca40dSAlexander Block 		ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
2663301958bSJan Schmidt 		if (ret < 0)
2673301958bSJan Schmidt 			return ret;
2688da6d581SJan Schmidt 		return 0;
26969bca40dSAlexander Block 	}
2708da6d581SJan Schmidt 
2718da6d581SJan Schmidt 	/*
27269bca40dSAlexander Block 	 * We normally enter this function with the path already pointing to
27369bca40dSAlexander Block 	 * the first item to check. But sometimes, we may enter it with
27469bca40dSAlexander Block 	 * slot==nritems. In that case, go to the next leaf before we continue.
2758da6d581SJan Schmidt 	 */
27621633fc6SQu Wenruo 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
27721633fc6SQu Wenruo 		if (time_seq == (u64)-1)
27821633fc6SQu Wenruo 			ret = btrfs_next_leaf(root, path);
27921633fc6SQu Wenruo 		else
2803d7806ecSJan Schmidt 			ret = btrfs_next_old_leaf(root, path, time_seq);
28121633fc6SQu Wenruo 	}
2828da6d581SJan Schmidt 
28344853868SJosef Bacik 	while (!ret && count < total_refs) {
2848da6d581SJan Schmidt 		eb = path->nodes[0];
28569bca40dSAlexander Block 		slot = path->slots[0];
28669bca40dSAlexander Block 
28769bca40dSAlexander Block 		btrfs_item_key_to_cpu(eb, &key, slot);
28869bca40dSAlexander Block 
28969bca40dSAlexander Block 		if (key.objectid != key_for_search->objectid ||
29069bca40dSAlexander Block 		    key.type != BTRFS_EXTENT_DATA_KEY)
29169bca40dSAlexander Block 			break;
29269bca40dSAlexander Block 
29369bca40dSAlexander Block 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
2948da6d581SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
29569bca40dSAlexander Block 
29669bca40dSAlexander Block 		if (disk_byte == wanted_disk_byte) {
29769bca40dSAlexander Block 			eie = NULL;
298ed8c4913SJosef Bacik 			old = NULL;
2997ef81ac8SJosef Bacik 			count++;
30069bca40dSAlexander Block 			if (extent_item_pos) {
30169bca40dSAlexander Block 				ret = check_extent_in_eb(&key, eb, fi,
30269bca40dSAlexander Block 						*extent_item_pos,
30369bca40dSAlexander Block 						&eie);
30469bca40dSAlexander Block 				if (ret < 0)
30569bca40dSAlexander Block 					break;
3068da6d581SJan Schmidt 			}
307ed8c4913SJosef Bacik 			if (ret > 0)
308ed8c4913SJosef Bacik 				goto next;
3094eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(parents, eb->start,
3104eb1f66dSTakashi Iwai 						  eie, (void **)&old, GFP_NOFS);
31169bca40dSAlexander Block 			if (ret < 0)
31269bca40dSAlexander Block 				break;
313ed8c4913SJosef Bacik 			if (!ret && extent_item_pos) {
314ed8c4913SJosef Bacik 				while (old->next)
315ed8c4913SJosef Bacik 					old = old->next;
316ed8c4913SJosef Bacik 				old->next = eie;
31769bca40dSAlexander Block 			}
318f05c4746SWang Shilong 			eie = NULL;
31969bca40dSAlexander Block 		}
320ed8c4913SJosef Bacik next:
32121633fc6SQu Wenruo 		if (time_seq == (u64)-1)
32221633fc6SQu Wenruo 			ret = btrfs_next_item(root, path);
32321633fc6SQu Wenruo 		else
32469bca40dSAlexander Block 			ret = btrfs_next_old_item(root, path, time_seq);
3258da6d581SJan Schmidt 	}
3268da6d581SJan Schmidt 
32769bca40dSAlexander Block 	if (ret > 0)
32869bca40dSAlexander Block 		ret = 0;
329f05c4746SWang Shilong 	else if (ret < 0)
330f05c4746SWang Shilong 		free_inode_elem_list(eie);
33169bca40dSAlexander Block 	return ret;
3328da6d581SJan Schmidt }
3338da6d581SJan Schmidt 
3348da6d581SJan Schmidt /*
3358da6d581SJan Schmidt  * resolve an indirect backref in the form (root_id, key, level)
3368da6d581SJan Schmidt  * to a logical address
3378da6d581SJan Schmidt  */
3388da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
339da61d31aSJosef Bacik 				  struct btrfs_path *path, u64 time_seq,
3408da6d581SJan Schmidt 				  struct __prelim_ref *ref,
341976b1908SJan Schmidt 				  struct ulist *parents,
34244853868SJosef Bacik 				  const u64 *extent_item_pos, u64 total_refs)
3438da6d581SJan Schmidt {
3448da6d581SJan Schmidt 	struct btrfs_root *root;
3458da6d581SJan Schmidt 	struct btrfs_key root_key;
3468da6d581SJan Schmidt 	struct extent_buffer *eb;
3478da6d581SJan Schmidt 	int ret = 0;
3488da6d581SJan Schmidt 	int root_level;
3498da6d581SJan Schmidt 	int level = ref->level;
350538f72cdSWang Shilong 	int index;
3518da6d581SJan Schmidt 
3528da6d581SJan Schmidt 	root_key.objectid = ref->root_id;
3538da6d581SJan Schmidt 	root_key.type = BTRFS_ROOT_ITEM_KEY;
3548da6d581SJan Schmidt 	root_key.offset = (u64)-1;
355538f72cdSWang Shilong 
356538f72cdSWang Shilong 	index = srcu_read_lock(&fs_info->subvol_srcu);
357538f72cdSWang Shilong 
3588da6d581SJan Schmidt 	root = btrfs_read_fs_root_no_name(fs_info, &root_key);
3598da6d581SJan Schmidt 	if (IS_ERR(root)) {
360538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
3618da6d581SJan Schmidt 		ret = PTR_ERR(root);
3628da6d581SJan Schmidt 		goto out;
3638da6d581SJan Schmidt 	}
3648da6d581SJan Schmidt 
3659e351cc8SJosef Bacik 	if (path->search_commit_root)
3669e351cc8SJosef Bacik 		root_level = btrfs_header_level(root->commit_root);
36721633fc6SQu Wenruo 	else if (time_seq == (u64)-1)
36821633fc6SQu Wenruo 		root_level = btrfs_header_level(root->node);
3699e351cc8SJosef Bacik 	else
3705b6602e7SJan Schmidt 		root_level = btrfs_old_root_level(root, time_seq);
3718da6d581SJan Schmidt 
372538f72cdSWang Shilong 	if (root_level + 1 == level) {
373538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
3748da6d581SJan Schmidt 		goto out;
375538f72cdSWang Shilong 	}
3768da6d581SJan Schmidt 
3778da6d581SJan Schmidt 	path->lowest_level = level;
37821633fc6SQu Wenruo 	if (time_seq == (u64)-1)
37921633fc6SQu Wenruo 		ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
38021633fc6SQu Wenruo 					0, 0);
38121633fc6SQu Wenruo 	else
38221633fc6SQu Wenruo 		ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
38321633fc6SQu Wenruo 					    time_seq);
384538f72cdSWang Shilong 
385538f72cdSWang Shilong 	/* root node has been locked, we can release @subvol_srcu safely here */
386538f72cdSWang Shilong 	srcu_read_unlock(&fs_info->subvol_srcu, index);
387538f72cdSWang Shilong 
3888da6d581SJan Schmidt 	pr_debug("search slot in root %llu (level %d, ref count %d) returned "
3898da6d581SJan Schmidt 		 "%d for key (%llu %u %llu)\n",
390c1c9ff7cSGeert Uytterhoeven 		 ref->root_id, level, ref->count, ret,
391c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.objectid, ref->key_for_search.type,
392c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.offset);
3938da6d581SJan Schmidt 	if (ret < 0)
3948da6d581SJan Schmidt 		goto out;
3958da6d581SJan Schmidt 
3968da6d581SJan Schmidt 	eb = path->nodes[level];
3979345457fSJan Schmidt 	while (!eb) {
398fae7f21cSDulshani Gunawardhana 		if (WARN_ON(!level)) {
3998da6d581SJan Schmidt 			ret = 1;
4008da6d581SJan Schmidt 			goto out;
4018da6d581SJan Schmidt 		}
4029345457fSJan Schmidt 		level--;
4039345457fSJan Schmidt 		eb = path->nodes[level];
4049345457fSJan Schmidt 	}
4058da6d581SJan Schmidt 
4067ef81ac8SJosef Bacik 	ret = add_all_parents(root, path, parents, ref, level, time_seq,
40744853868SJosef Bacik 			      extent_item_pos, total_refs);
4088da6d581SJan Schmidt out:
409da61d31aSJosef Bacik 	path->lowest_level = 0;
410da61d31aSJosef Bacik 	btrfs_release_path(path);
4118da6d581SJan Schmidt 	return ret;
4128da6d581SJan Schmidt }
4138da6d581SJan Schmidt 
4148da6d581SJan Schmidt /*
4158da6d581SJan Schmidt  * resolve all indirect backrefs from the list
4168da6d581SJan Schmidt  */
4178da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
418da61d31aSJosef Bacik 				   struct btrfs_path *path, u64 time_seq,
419976b1908SJan Schmidt 				   struct list_head *head,
420dc046b10SJosef Bacik 				   const u64 *extent_item_pos, u64 total_refs,
421dc046b10SJosef Bacik 				   u64 root_objectid)
4228da6d581SJan Schmidt {
4238da6d581SJan Schmidt 	int err;
4248da6d581SJan Schmidt 	int ret = 0;
4258da6d581SJan Schmidt 	struct __prelim_ref *ref;
4268da6d581SJan Schmidt 	struct __prelim_ref *ref_safe;
4278da6d581SJan Schmidt 	struct __prelim_ref *new_ref;
4288da6d581SJan Schmidt 	struct ulist *parents;
4298da6d581SJan Schmidt 	struct ulist_node *node;
430cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
4318da6d581SJan Schmidt 
4328da6d581SJan Schmidt 	parents = ulist_alloc(GFP_NOFS);
4338da6d581SJan Schmidt 	if (!parents)
4348da6d581SJan Schmidt 		return -ENOMEM;
4358da6d581SJan Schmidt 
4368da6d581SJan Schmidt 	/*
4378da6d581SJan Schmidt 	 * _safe allows us to insert directly after the current item without
4388da6d581SJan Schmidt 	 * iterating over the newly inserted items.
4398da6d581SJan Schmidt 	 * we're also allowed to re-assign ref during iteration.
4408da6d581SJan Schmidt 	 */
4418da6d581SJan Schmidt 	list_for_each_entry_safe(ref, ref_safe, head, list) {
4428da6d581SJan Schmidt 		if (ref->parent)	/* already direct */
4438da6d581SJan Schmidt 			continue;
4448da6d581SJan Schmidt 		if (ref->count == 0)
4458da6d581SJan Schmidt 			continue;
446dc046b10SJosef Bacik 		if (root_objectid && ref->root_id != root_objectid) {
447dc046b10SJosef Bacik 			ret = BACKREF_FOUND_SHARED;
448dc046b10SJosef Bacik 			goto out;
449dc046b10SJosef Bacik 		}
450da61d31aSJosef Bacik 		err = __resolve_indirect_ref(fs_info, path, time_seq, ref,
45144853868SJosef Bacik 					     parents, extent_item_pos,
45244853868SJosef Bacik 					     total_refs);
45395def2edSWang Shilong 		/*
45495def2edSWang Shilong 		 * we can only tolerate ENOENT,otherwise,we should catch error
45595def2edSWang Shilong 		 * and return directly.
45695def2edSWang Shilong 		 */
45795def2edSWang Shilong 		if (err == -ENOENT) {
4588da6d581SJan Schmidt 			continue;
45995def2edSWang Shilong 		} else if (err) {
46095def2edSWang Shilong 			ret = err;
46195def2edSWang Shilong 			goto out;
46295def2edSWang Shilong 		}
4638da6d581SJan Schmidt 
4648da6d581SJan Schmidt 		/* we put the first parent into the ref at hand */
465cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&uiter);
466cd1b413cSJan Schmidt 		node = ulist_next(parents, &uiter);
4678da6d581SJan Schmidt 		ref->parent = node ? node->val : 0;
468995e01b7SJan Schmidt 		ref->inode_list = node ?
46935a3621bSStefan Behrens 			(struct extent_inode_elem *)(uintptr_t)node->aux : NULL;
4708da6d581SJan Schmidt 
4718da6d581SJan Schmidt 		/* additional parents require new refs being added here */
472cd1b413cSJan Schmidt 		while ((node = ulist_next(parents, &uiter))) {
473b9e9a6cbSWang Shilong 			new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
474b9e9a6cbSWang Shilong 						   GFP_NOFS);
4758da6d581SJan Schmidt 			if (!new_ref) {
4768da6d581SJan Schmidt 				ret = -ENOMEM;
477e36902d4SWang Shilong 				goto out;
4788da6d581SJan Schmidt 			}
4798da6d581SJan Schmidt 			memcpy(new_ref, ref, sizeof(*ref));
4808da6d581SJan Schmidt 			new_ref->parent = node->val;
481995e01b7SJan Schmidt 			new_ref->inode_list = (struct extent_inode_elem *)
482995e01b7SJan Schmidt 							(uintptr_t)node->aux;
4838da6d581SJan Schmidt 			list_add(&new_ref->list, &ref->list);
4848da6d581SJan Schmidt 		}
4858da6d581SJan Schmidt 		ulist_reinit(parents);
4868da6d581SJan Schmidt 	}
487e36902d4SWang Shilong out:
4888da6d581SJan Schmidt 	ulist_free(parents);
4898da6d581SJan Schmidt 	return ret;
4908da6d581SJan Schmidt }
4918da6d581SJan Schmidt 
492d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1,
493d5c88b73SJan Schmidt 				     struct __prelim_ref *ref2)
494d5c88b73SJan Schmidt {
495d5c88b73SJan Schmidt 	if (ref1->level != ref2->level)
496d5c88b73SJan Schmidt 		return 0;
497d5c88b73SJan Schmidt 	if (ref1->root_id != ref2->root_id)
498d5c88b73SJan Schmidt 		return 0;
499d5c88b73SJan Schmidt 	if (ref1->key_for_search.type != ref2->key_for_search.type)
500d5c88b73SJan Schmidt 		return 0;
501d5c88b73SJan Schmidt 	if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
502d5c88b73SJan Schmidt 		return 0;
503d5c88b73SJan Schmidt 	if (ref1->key_for_search.offset != ref2->key_for_search.offset)
504d5c88b73SJan Schmidt 		return 0;
505d5c88b73SJan Schmidt 	if (ref1->parent != ref2->parent)
506d5c88b73SJan Schmidt 		return 0;
507d5c88b73SJan Schmidt 
508d5c88b73SJan Schmidt 	return 1;
509d5c88b73SJan Schmidt }
510d5c88b73SJan Schmidt 
511d5c88b73SJan Schmidt /*
512d5c88b73SJan Schmidt  * read tree blocks and add keys where required.
513d5c88b73SJan Schmidt  */
514d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info,
515d5c88b73SJan Schmidt 			      struct list_head *head)
516d5c88b73SJan Schmidt {
517d5c88b73SJan Schmidt 	struct list_head *pos;
518d5c88b73SJan Schmidt 	struct extent_buffer *eb;
519d5c88b73SJan Schmidt 
520d5c88b73SJan Schmidt 	list_for_each(pos, head) {
521d5c88b73SJan Schmidt 		struct __prelim_ref *ref;
522d5c88b73SJan Schmidt 		ref = list_entry(pos, struct __prelim_ref, list);
523d5c88b73SJan Schmidt 
524d5c88b73SJan Schmidt 		if (ref->parent)
525d5c88b73SJan Schmidt 			continue;
526d5c88b73SJan Schmidt 		if (ref->key_for_search.type)
527d5c88b73SJan Schmidt 			continue;
528d5c88b73SJan Schmidt 		BUG_ON(!ref->wanted_disk_byte);
529d5c88b73SJan Schmidt 		eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
530ce86cd59SDavid Sterba 				     0);
53164c043deSLiu Bo 		if (IS_ERR(eb)) {
53264c043deSLiu Bo 			return PTR_ERR(eb);
53364c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
534416bc658SJosef Bacik 			free_extent_buffer(eb);
535416bc658SJosef Bacik 			return -EIO;
536416bc658SJosef Bacik 		}
537d5c88b73SJan Schmidt 		btrfs_tree_read_lock(eb);
538d5c88b73SJan Schmidt 		if (btrfs_header_level(eb) == 0)
539d5c88b73SJan Schmidt 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
540d5c88b73SJan Schmidt 		else
541d5c88b73SJan Schmidt 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
542d5c88b73SJan Schmidt 		btrfs_tree_read_unlock(eb);
543d5c88b73SJan Schmidt 		free_extent_buffer(eb);
544d5c88b73SJan Schmidt 	}
545d5c88b73SJan Schmidt 	return 0;
546d5c88b73SJan Schmidt }
547d5c88b73SJan Schmidt 
5488da6d581SJan Schmidt /*
54900db646dSQu Wenruo  * merge backrefs and adjust counts accordingly
5508da6d581SJan Schmidt  *
5518da6d581SJan Schmidt  * mode = 1: merge identical keys, if key is set
552d5c88b73SJan Schmidt  *    FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
553d5c88b73SJan Schmidt  *           additionally, we could even add a key range for the blocks we
554d5c88b73SJan Schmidt  *           looked into to merge even more (-> replace unresolved refs by those
555d5c88b73SJan Schmidt  *           having a parent).
5568da6d581SJan Schmidt  * mode = 2: merge identical parents
5578da6d581SJan Schmidt  */
558692206b1SWang Shilong static void __merge_refs(struct list_head *head, int mode)
5598da6d581SJan Schmidt {
5608da6d581SJan Schmidt 	struct list_head *pos1;
5618da6d581SJan Schmidt 
5628da6d581SJan Schmidt 	list_for_each(pos1, head) {
5638da6d581SJan Schmidt 		struct list_head *n2;
5648da6d581SJan Schmidt 		struct list_head *pos2;
5658da6d581SJan Schmidt 		struct __prelim_ref *ref1;
5668da6d581SJan Schmidt 
5678da6d581SJan Schmidt 		ref1 = list_entry(pos1, struct __prelim_ref, list);
5688da6d581SJan Schmidt 
5698da6d581SJan Schmidt 		for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
5708da6d581SJan Schmidt 		     pos2 = n2, n2 = pos2->next) {
5718da6d581SJan Schmidt 			struct __prelim_ref *ref2;
572d5c88b73SJan Schmidt 			struct __prelim_ref *xchg;
5733ef5969cSAlexander Block 			struct extent_inode_elem *eie;
5748da6d581SJan Schmidt 
5758da6d581SJan Schmidt 			ref2 = list_entry(pos2, struct __prelim_ref, list);
5768da6d581SJan Schmidt 
577d5c88b73SJan Schmidt 			if (!ref_for_same_block(ref1, ref2))
5788da6d581SJan Schmidt 				continue;
57900db646dSQu Wenruo 			if (mode == 1) {
580d5c88b73SJan Schmidt 				if (!ref1->parent && ref2->parent) {
581d5c88b73SJan Schmidt 					xchg = ref1;
582d5c88b73SJan Schmidt 					ref1 = ref2;
583d5c88b73SJan Schmidt 					ref2 = xchg;
584d5c88b73SJan Schmidt 				}
5858da6d581SJan Schmidt 			} else {
5868da6d581SJan Schmidt 				if (ref1->parent != ref2->parent)
5878da6d581SJan Schmidt 					continue;
5888da6d581SJan Schmidt 			}
5893ef5969cSAlexander Block 
5903ef5969cSAlexander Block 			eie = ref1->inode_list;
5913ef5969cSAlexander Block 			while (eie && eie->next)
5923ef5969cSAlexander Block 				eie = eie->next;
5933ef5969cSAlexander Block 			if (eie)
5943ef5969cSAlexander Block 				eie->next = ref2->inode_list;
5953ef5969cSAlexander Block 			else
5963ef5969cSAlexander Block 				ref1->inode_list = ref2->inode_list;
5973ef5969cSAlexander Block 			ref1->count += ref2->count;
5983ef5969cSAlexander Block 
5998da6d581SJan Schmidt 			list_del(&ref2->list);
600b9e9a6cbSWang Shilong 			kmem_cache_free(btrfs_prelim_ref_cache, ref2);
6018da6d581SJan Schmidt 		}
6028da6d581SJan Schmidt 
6038da6d581SJan Schmidt 	}
6048da6d581SJan Schmidt }
6058da6d581SJan Schmidt 
6068da6d581SJan Schmidt /*
6078da6d581SJan Schmidt  * add all currently queued delayed refs from this head whose seq nr is
6088da6d581SJan Schmidt  * smaller or equal that seq to the list
6098da6d581SJan Schmidt  */
6108da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
611dc046b10SJosef Bacik 			      struct list_head *prefs, u64 *total_refs,
612dc046b10SJosef Bacik 			      u64 inum)
6138da6d581SJan Schmidt {
614c6fc2454SQu Wenruo 	struct btrfs_delayed_ref_node *node;
6158da6d581SJan Schmidt 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
616d5c88b73SJan Schmidt 	struct btrfs_key key;
617d5c88b73SJan Schmidt 	struct btrfs_key op_key = {0};
6188da6d581SJan Schmidt 	int sgn;
619b1375d64SJan Schmidt 	int ret = 0;
6208da6d581SJan Schmidt 
6218da6d581SJan Schmidt 	if (extent_op && extent_op->update_key)
622d5c88b73SJan Schmidt 		btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
6238da6d581SJan Schmidt 
624d7df2c79SJosef Bacik 	spin_lock(&head->lock);
625c6fc2454SQu Wenruo 	list_for_each_entry(node, &head->ref_list, list) {
6268da6d581SJan Schmidt 		if (node->seq > seq)
6278da6d581SJan Schmidt 			continue;
6288da6d581SJan Schmidt 
6298da6d581SJan Schmidt 		switch (node->action) {
6308da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_EXTENT:
6318da6d581SJan Schmidt 		case BTRFS_UPDATE_DELAYED_HEAD:
6328da6d581SJan Schmidt 			WARN_ON(1);
6338da6d581SJan Schmidt 			continue;
6348da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_REF:
6358da6d581SJan Schmidt 			sgn = 1;
6368da6d581SJan Schmidt 			break;
6378da6d581SJan Schmidt 		case BTRFS_DROP_DELAYED_REF:
6388da6d581SJan Schmidt 			sgn = -1;
6398da6d581SJan Schmidt 			break;
6408da6d581SJan Schmidt 		default:
6418da6d581SJan Schmidt 			BUG_ON(1);
6428da6d581SJan Schmidt 		}
64344853868SJosef Bacik 		*total_refs += (node->ref_mod * sgn);
6448da6d581SJan Schmidt 		switch (node->type) {
6458da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY: {
6468da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
6478da6d581SJan Schmidt 
6488da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
649d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &op_key,
6508da6d581SJan Schmidt 					       ref->level + 1, 0, node->bytenr,
651742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6528da6d581SJan Schmidt 			break;
6538da6d581SJan Schmidt 		}
6548da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY: {
6558da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
6568da6d581SJan Schmidt 
6578da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
658*acdf898dSLiu Bo 			ret = __add_prelim_ref(prefs, 0, NULL,
6598da6d581SJan Schmidt 					       ref->level + 1, ref->parent,
6608da6d581SJan Schmidt 					       node->bytenr,
661742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6628da6d581SJan Schmidt 			break;
6638da6d581SJan Schmidt 		}
6648da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
6658da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
6668da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
6678da6d581SJan Schmidt 
6688da6d581SJan Schmidt 			key.objectid = ref->objectid;
6698da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
6708da6d581SJan Schmidt 			key.offset = ref->offset;
671dc046b10SJosef Bacik 
672dc046b10SJosef Bacik 			/*
673dc046b10SJosef Bacik 			 * Found a inum that doesn't match our known inum, we
674dc046b10SJosef Bacik 			 * know it's shared.
675dc046b10SJosef Bacik 			 */
676dc046b10SJosef Bacik 			if (inum && ref->objectid != inum) {
677dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
678dc046b10SJosef Bacik 				break;
679dc046b10SJosef Bacik 			}
680dc046b10SJosef Bacik 
6818da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
6828da6d581SJan Schmidt 					       node->bytenr,
683742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6848da6d581SJan Schmidt 			break;
6858da6d581SJan Schmidt 		}
6868da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
6878da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
6888da6d581SJan Schmidt 
6898da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
690*acdf898dSLiu Bo 			ret = __add_prelim_ref(prefs, 0, NULL, 0,
6918da6d581SJan Schmidt 					       ref->parent, node->bytenr,
692742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6938da6d581SJan Schmidt 			break;
6948da6d581SJan Schmidt 		}
6958da6d581SJan Schmidt 		default:
6968da6d581SJan Schmidt 			WARN_ON(1);
6978da6d581SJan Schmidt 		}
6981149ab6bSWang Shilong 		if (ret)
699d7df2c79SJosef Bacik 			break;
7008da6d581SJan Schmidt 	}
701d7df2c79SJosef Bacik 	spin_unlock(&head->lock);
702d7df2c79SJosef Bacik 	return ret;
7038da6d581SJan Schmidt }
7048da6d581SJan Schmidt 
7058da6d581SJan Schmidt /*
7068da6d581SJan Schmidt  * add all inline backrefs for bytenr to the list
7078da6d581SJan Schmidt  */
7088da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info,
7098da6d581SJan Schmidt 			     struct btrfs_path *path, u64 bytenr,
71044853868SJosef Bacik 			     int *info_level, struct list_head *prefs,
711dc046b10SJosef Bacik 			     u64 *total_refs, u64 inum)
7128da6d581SJan Schmidt {
713b1375d64SJan Schmidt 	int ret = 0;
7148da6d581SJan Schmidt 	int slot;
7158da6d581SJan Schmidt 	struct extent_buffer *leaf;
7168da6d581SJan Schmidt 	struct btrfs_key key;
717261c84b6SJosef Bacik 	struct btrfs_key found_key;
7188da6d581SJan Schmidt 	unsigned long ptr;
7198da6d581SJan Schmidt 	unsigned long end;
7208da6d581SJan Schmidt 	struct btrfs_extent_item *ei;
7218da6d581SJan Schmidt 	u64 flags;
7228da6d581SJan Schmidt 	u64 item_size;
7238da6d581SJan Schmidt 
7248da6d581SJan Schmidt 	/*
7258da6d581SJan Schmidt 	 * enumerate all inline refs
7268da6d581SJan Schmidt 	 */
7278da6d581SJan Schmidt 	leaf = path->nodes[0];
728dadcaf78SJan Schmidt 	slot = path->slots[0];
7298da6d581SJan Schmidt 
7308da6d581SJan Schmidt 	item_size = btrfs_item_size_nr(leaf, slot);
7318da6d581SJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
7328da6d581SJan Schmidt 
7338da6d581SJan Schmidt 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
7348da6d581SJan Schmidt 	flags = btrfs_extent_flags(leaf, ei);
73544853868SJosef Bacik 	*total_refs += btrfs_extent_refs(leaf, ei);
736261c84b6SJosef Bacik 	btrfs_item_key_to_cpu(leaf, &found_key, slot);
7378da6d581SJan Schmidt 
7388da6d581SJan Schmidt 	ptr = (unsigned long)(ei + 1);
7398da6d581SJan Schmidt 	end = (unsigned long)ei + item_size;
7408da6d581SJan Schmidt 
741261c84b6SJosef Bacik 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
742261c84b6SJosef Bacik 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
7438da6d581SJan Schmidt 		struct btrfs_tree_block_info *info;
7448da6d581SJan Schmidt 
7458da6d581SJan Schmidt 		info = (struct btrfs_tree_block_info *)ptr;
7468da6d581SJan Schmidt 		*info_level = btrfs_tree_block_level(leaf, info);
7478da6d581SJan Schmidt 		ptr += sizeof(struct btrfs_tree_block_info);
7488da6d581SJan Schmidt 		BUG_ON(ptr > end);
749261c84b6SJosef Bacik 	} else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
750261c84b6SJosef Bacik 		*info_level = found_key.offset;
7518da6d581SJan Schmidt 	} else {
7528da6d581SJan Schmidt 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
7538da6d581SJan Schmidt 	}
7548da6d581SJan Schmidt 
7558da6d581SJan Schmidt 	while (ptr < end) {
7568da6d581SJan Schmidt 		struct btrfs_extent_inline_ref *iref;
7578da6d581SJan Schmidt 		u64 offset;
7588da6d581SJan Schmidt 		int type;
7598da6d581SJan Schmidt 
7608da6d581SJan Schmidt 		iref = (struct btrfs_extent_inline_ref *)ptr;
7618da6d581SJan Schmidt 		type = btrfs_extent_inline_ref_type(leaf, iref);
7628da6d581SJan Schmidt 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
7638da6d581SJan Schmidt 
7648da6d581SJan Schmidt 		switch (type) {
7658da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
766d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL,
7678da6d581SJan Schmidt 						*info_level + 1, offset,
768742916b8SWang Shilong 						bytenr, 1, GFP_NOFS);
7698da6d581SJan Schmidt 			break;
7708da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
7718da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
7728da6d581SJan Schmidt 			int count;
7738da6d581SJan Schmidt 
7748da6d581SJan Schmidt 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
7758da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
7768da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
777742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
7788da6d581SJan Schmidt 			break;
7798da6d581SJan Schmidt 		}
7808da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
781d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, offset, NULL,
782d5c88b73SJan Schmidt 					       *info_level + 1, 0,
783742916b8SWang Shilong 					       bytenr, 1, GFP_NOFS);
7848da6d581SJan Schmidt 			break;
7858da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
7868da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
7878da6d581SJan Schmidt 			int count;
7888da6d581SJan Schmidt 			u64 root;
7898da6d581SJan Schmidt 
7908da6d581SJan Schmidt 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
7918da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
7928da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
7938da6d581SJan Schmidt 								      dref);
7948da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
7958da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
796dc046b10SJosef Bacik 
797dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
798dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
799dc046b10SJosef Bacik 				break;
800dc046b10SJosef Bacik 			}
801dc046b10SJosef Bacik 
8028da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
803d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
804742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
8058da6d581SJan Schmidt 			break;
8068da6d581SJan Schmidt 		}
8078da6d581SJan Schmidt 		default:
8088da6d581SJan Schmidt 			WARN_ON(1);
8098da6d581SJan Schmidt 		}
8101149ab6bSWang Shilong 		if (ret)
8111149ab6bSWang Shilong 			return ret;
8128da6d581SJan Schmidt 		ptr += btrfs_extent_inline_ref_size(type);
8138da6d581SJan Schmidt 	}
8148da6d581SJan Schmidt 
8158da6d581SJan Schmidt 	return 0;
8168da6d581SJan Schmidt }
8178da6d581SJan Schmidt 
8188da6d581SJan Schmidt /*
8198da6d581SJan Schmidt  * add all non-inline backrefs for bytenr to the list
8208da6d581SJan Schmidt  */
8218da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
8228da6d581SJan Schmidt 			    struct btrfs_path *path, u64 bytenr,
823dc046b10SJosef Bacik 			    int info_level, struct list_head *prefs, u64 inum)
8248da6d581SJan Schmidt {
8258da6d581SJan Schmidt 	struct btrfs_root *extent_root = fs_info->extent_root;
8268da6d581SJan Schmidt 	int ret;
8278da6d581SJan Schmidt 	int slot;
8288da6d581SJan Schmidt 	struct extent_buffer *leaf;
8298da6d581SJan Schmidt 	struct btrfs_key key;
8308da6d581SJan Schmidt 
8318da6d581SJan Schmidt 	while (1) {
8328da6d581SJan Schmidt 		ret = btrfs_next_item(extent_root, path);
8338da6d581SJan Schmidt 		if (ret < 0)
8348da6d581SJan Schmidt 			break;
8358da6d581SJan Schmidt 		if (ret) {
8368da6d581SJan Schmidt 			ret = 0;
8378da6d581SJan Schmidt 			break;
8388da6d581SJan Schmidt 		}
8398da6d581SJan Schmidt 
8408da6d581SJan Schmidt 		slot = path->slots[0];
8418da6d581SJan Schmidt 		leaf = path->nodes[0];
8428da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
8438da6d581SJan Schmidt 
8448da6d581SJan Schmidt 		if (key.objectid != bytenr)
8458da6d581SJan Schmidt 			break;
8468da6d581SJan Schmidt 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
8478da6d581SJan Schmidt 			continue;
8488da6d581SJan Schmidt 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
8498da6d581SJan Schmidt 			break;
8508da6d581SJan Schmidt 
8518da6d581SJan Schmidt 		switch (key.type) {
8528da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
853d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL,
8548da6d581SJan Schmidt 						info_level + 1, key.offset,
855742916b8SWang Shilong 						bytenr, 1, GFP_NOFS);
8568da6d581SJan Schmidt 			break;
8578da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
8588da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
8598da6d581SJan Schmidt 			int count;
8608da6d581SJan Schmidt 
8618da6d581SJan Schmidt 			sdref = btrfs_item_ptr(leaf, slot,
8628da6d581SJan Schmidt 					      struct btrfs_shared_data_ref);
8638da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
8648da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
865742916b8SWang Shilong 						bytenr, count, GFP_NOFS);
8668da6d581SJan Schmidt 			break;
8678da6d581SJan Schmidt 		}
8688da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
869d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, key.offset, NULL,
870d5c88b73SJan Schmidt 					       info_level + 1, 0,
871742916b8SWang Shilong 					       bytenr, 1, GFP_NOFS);
8728da6d581SJan Schmidt 			break;
8738da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
8748da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
8758da6d581SJan Schmidt 			int count;
8768da6d581SJan Schmidt 			u64 root;
8778da6d581SJan Schmidt 
8788da6d581SJan Schmidt 			dref = btrfs_item_ptr(leaf, slot,
8798da6d581SJan Schmidt 					      struct btrfs_extent_data_ref);
8808da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
8818da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
8828da6d581SJan Schmidt 								      dref);
8838da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
8848da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
885dc046b10SJosef Bacik 
886dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
887dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
888dc046b10SJosef Bacik 				break;
889dc046b10SJosef Bacik 			}
890dc046b10SJosef Bacik 
8918da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
8928da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
893742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
8948da6d581SJan Schmidt 			break;
8958da6d581SJan Schmidt 		}
8968da6d581SJan Schmidt 		default:
8978da6d581SJan Schmidt 			WARN_ON(1);
8988da6d581SJan Schmidt 		}
8991149ab6bSWang Shilong 		if (ret)
9001149ab6bSWang Shilong 			return ret;
9011149ab6bSWang Shilong 
9028da6d581SJan Schmidt 	}
9038da6d581SJan Schmidt 
9048da6d581SJan Schmidt 	return ret;
9058da6d581SJan Schmidt }
9068da6d581SJan Schmidt 
9078da6d581SJan Schmidt /*
9088da6d581SJan Schmidt  * this adds all existing backrefs (inline backrefs, backrefs and delayed
9098da6d581SJan Schmidt  * refs) for the given bytenr to the refs list, merges duplicates and resolves
9108da6d581SJan Schmidt  * indirect refs to their parent bytenr.
9118da6d581SJan Schmidt  * When roots are found, they're added to the roots list
9128da6d581SJan Schmidt  *
9132c2ed5aaSMark Fasheh  * NOTE: This can return values > 0
9142c2ed5aaSMark Fasheh  *
91521633fc6SQu Wenruo  * If time_seq is set to (u64)-1, it will not search delayed_refs, and behave
91621633fc6SQu Wenruo  * much like trans == NULL case, the difference only lies in it will not
91721633fc6SQu Wenruo  * commit root.
91821633fc6SQu Wenruo  * The special case is for qgroup to search roots in commit_transaction().
91921633fc6SQu Wenruo  *
9208da6d581SJan Schmidt  * FIXME some caching might speed things up
9218da6d581SJan Schmidt  */
9228da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans,
9238da6d581SJan Schmidt 			     struct btrfs_fs_info *fs_info, u64 bytenr,
924097b8a7cSJan Schmidt 			     u64 time_seq, struct ulist *refs,
925dc046b10SJosef Bacik 			     struct ulist *roots, const u64 *extent_item_pos,
926dc046b10SJosef Bacik 			     u64 root_objectid, u64 inum)
9278da6d581SJan Schmidt {
9288da6d581SJan Schmidt 	struct btrfs_key key;
9298da6d581SJan Schmidt 	struct btrfs_path *path;
9308da6d581SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
931d3b01064SLi Zefan 	struct btrfs_delayed_ref_head *head;
9328da6d581SJan Schmidt 	int info_level = 0;
9338da6d581SJan Schmidt 	int ret;
9348da6d581SJan Schmidt 	struct list_head prefs_delayed;
9358da6d581SJan Schmidt 	struct list_head prefs;
9368da6d581SJan Schmidt 	struct __prelim_ref *ref;
937f05c4746SWang Shilong 	struct extent_inode_elem *eie = NULL;
93844853868SJosef Bacik 	u64 total_refs = 0;
9398da6d581SJan Schmidt 
9408da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs);
9418da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs_delayed);
9428da6d581SJan Schmidt 
9438da6d581SJan Schmidt 	key.objectid = bytenr;
9448da6d581SJan Schmidt 	key.offset = (u64)-1;
945261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
946261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
947261c84b6SJosef Bacik 	else
948261c84b6SJosef Bacik 		key.type = BTRFS_EXTENT_ITEM_KEY;
9498da6d581SJan Schmidt 
9508da6d581SJan Schmidt 	path = btrfs_alloc_path();
9518da6d581SJan Schmidt 	if (!path)
9528da6d581SJan Schmidt 		return -ENOMEM;
953e84752d4SWang Shilong 	if (!trans) {
954da61d31aSJosef Bacik 		path->search_commit_root = 1;
955e84752d4SWang Shilong 		path->skip_locking = 1;
956e84752d4SWang Shilong 	}
9578da6d581SJan Schmidt 
95821633fc6SQu Wenruo 	if (time_seq == (u64)-1)
95921633fc6SQu Wenruo 		path->skip_locking = 1;
96021633fc6SQu Wenruo 
9618da6d581SJan Schmidt 	/*
9628da6d581SJan Schmidt 	 * grab both a lock on the path and a lock on the delayed ref head.
9638da6d581SJan Schmidt 	 * We need both to get a consistent picture of how the refs look
9648da6d581SJan Schmidt 	 * at a specified point in time
9658da6d581SJan Schmidt 	 */
9668da6d581SJan Schmidt again:
967d3b01064SLi Zefan 	head = NULL;
968d3b01064SLi Zefan 
9698da6d581SJan Schmidt 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
9708da6d581SJan Schmidt 	if (ret < 0)
9718da6d581SJan Schmidt 		goto out;
9728da6d581SJan Schmidt 	BUG_ON(ret == 0);
9738da6d581SJan Schmidt 
974faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
97521633fc6SQu Wenruo 	if (trans && likely(trans->type != __TRANS_DUMMY) &&
97621633fc6SQu Wenruo 	    time_seq != (u64)-1) {
977faa2dbf0SJosef Bacik #else
97821633fc6SQu Wenruo 	if (trans && time_seq != (u64)-1) {
979faa2dbf0SJosef Bacik #endif
9808da6d581SJan Schmidt 		/*
9817a3ae2f8SJan Schmidt 		 * look if there are updates for this ref queued and lock the
9827a3ae2f8SJan Schmidt 		 * head
9838da6d581SJan Schmidt 		 */
9848da6d581SJan Schmidt 		delayed_refs = &trans->transaction->delayed_refs;
9858da6d581SJan Schmidt 		spin_lock(&delayed_refs->lock);
9868da6d581SJan Schmidt 		head = btrfs_find_delayed_ref_head(trans, bytenr);
9878da6d581SJan Schmidt 		if (head) {
9888da6d581SJan Schmidt 			if (!mutex_trylock(&head->mutex)) {
9898da6d581SJan Schmidt 				atomic_inc(&head->node.refs);
9908da6d581SJan Schmidt 				spin_unlock(&delayed_refs->lock);
9918da6d581SJan Schmidt 
9928da6d581SJan Schmidt 				btrfs_release_path(path);
9938da6d581SJan Schmidt 
9948da6d581SJan Schmidt 				/*
9958da6d581SJan Schmidt 				 * Mutex was contended, block until it's
9968da6d581SJan Schmidt 				 * released and try again
9978da6d581SJan Schmidt 				 */
9988da6d581SJan Schmidt 				mutex_lock(&head->mutex);
9998da6d581SJan Schmidt 				mutex_unlock(&head->mutex);
10008da6d581SJan Schmidt 				btrfs_put_delayed_ref(&head->node);
10018da6d581SJan Schmidt 				goto again;
10028da6d581SJan Schmidt 			}
1003d7df2c79SJosef Bacik 			spin_unlock(&delayed_refs->lock);
1004097b8a7cSJan Schmidt 			ret = __add_delayed_refs(head, time_seq,
1005dc046b10SJosef Bacik 						 &prefs_delayed, &total_refs,
1006dc046b10SJosef Bacik 						 inum);
1007155725c9SJan Schmidt 			mutex_unlock(&head->mutex);
1008d7df2c79SJosef Bacik 			if (ret)
10098da6d581SJan Schmidt 				goto out;
1010d7df2c79SJosef Bacik 		} else {
10118da6d581SJan Schmidt 			spin_unlock(&delayed_refs->lock);
10127a3ae2f8SJan Schmidt 		}
1013d7df2c79SJosef Bacik 	}
10148da6d581SJan Schmidt 
10158da6d581SJan Schmidt 	if (path->slots[0]) {
10168da6d581SJan Schmidt 		struct extent_buffer *leaf;
10178da6d581SJan Schmidt 		int slot;
10188da6d581SJan Schmidt 
1019dadcaf78SJan Schmidt 		path->slots[0]--;
10208da6d581SJan Schmidt 		leaf = path->nodes[0];
1021dadcaf78SJan Schmidt 		slot = path->slots[0];
10228da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
10238da6d581SJan Schmidt 		if (key.objectid == bytenr &&
1024261c84b6SJosef Bacik 		    (key.type == BTRFS_EXTENT_ITEM_KEY ||
1025261c84b6SJosef Bacik 		     key.type == BTRFS_METADATA_ITEM_KEY)) {
10268da6d581SJan Schmidt 			ret = __add_inline_refs(fs_info, path, bytenr,
102744853868SJosef Bacik 						&info_level, &prefs,
1028dc046b10SJosef Bacik 						&total_refs, inum);
10298da6d581SJan Schmidt 			if (ret)
10308da6d581SJan Schmidt 				goto out;
1031d5c88b73SJan Schmidt 			ret = __add_keyed_refs(fs_info, path, bytenr,
1032dc046b10SJosef Bacik 					       info_level, &prefs, inum);
10338da6d581SJan Schmidt 			if (ret)
10348da6d581SJan Schmidt 				goto out;
10358da6d581SJan Schmidt 		}
10368da6d581SJan Schmidt 	}
10378da6d581SJan Schmidt 	btrfs_release_path(path);
10388da6d581SJan Schmidt 
10398da6d581SJan Schmidt 	list_splice_init(&prefs_delayed, &prefs);
10408da6d581SJan Schmidt 
1041d5c88b73SJan Schmidt 	ret = __add_missing_keys(fs_info, &prefs);
1042d5c88b73SJan Schmidt 	if (ret)
1043d5c88b73SJan Schmidt 		goto out;
1044d5c88b73SJan Schmidt 
1045692206b1SWang Shilong 	__merge_refs(&prefs, 1);
10468da6d581SJan Schmidt 
1047da61d31aSJosef Bacik 	ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs,
1048dc046b10SJosef Bacik 				      extent_item_pos, total_refs,
1049dc046b10SJosef Bacik 				      root_objectid);
10508da6d581SJan Schmidt 	if (ret)
10518da6d581SJan Schmidt 		goto out;
10528da6d581SJan Schmidt 
1053692206b1SWang Shilong 	__merge_refs(&prefs, 2);
10548da6d581SJan Schmidt 
10558da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
10568da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
10576c1500f2SJulia Lawall 		WARN_ON(ref->count < 0);
105898cfee21SWang Shilong 		if (roots && ref->count && ref->root_id && ref->parent == 0) {
1059dc046b10SJosef Bacik 			if (root_objectid && ref->root_id != root_objectid) {
1060dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1061dc046b10SJosef Bacik 				goto out;
1062dc046b10SJosef Bacik 			}
1063dc046b10SJosef Bacik 
10648da6d581SJan Schmidt 			/* no parent == root of tree */
10658da6d581SJan Schmidt 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
1066f1723939SWang Shilong 			if (ret < 0)
1067f1723939SWang Shilong 				goto out;
10688da6d581SJan Schmidt 		}
10698da6d581SJan Schmidt 		if (ref->count && ref->parent) {
10708a56457fSJosef Bacik 			if (extent_item_pos && !ref->inode_list &&
10718a56457fSJosef Bacik 			    ref->level == 0) {
1072976b1908SJan Schmidt 				struct extent_buffer *eb;
1073707e8a07SDavid Sterba 
1074976b1908SJan Schmidt 				eb = read_tree_block(fs_info->extent_root,
1075ce86cd59SDavid Sterba 							   ref->parent, 0);
107664c043deSLiu Bo 				if (IS_ERR(eb)) {
107764c043deSLiu Bo 					ret = PTR_ERR(eb);
107864c043deSLiu Bo 					goto out;
107964c043deSLiu Bo 				} else if (!extent_buffer_uptodate(eb)) {
1080416bc658SJosef Bacik 					free_extent_buffer(eb);
1081c16c2e2eSWang Shilong 					ret = -EIO;
1082c16c2e2eSWang Shilong 					goto out;
1083416bc658SJosef Bacik 				}
10846f7ff6d7SFilipe Manana 				btrfs_tree_read_lock(eb);
10856f7ff6d7SFilipe Manana 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1086976b1908SJan Schmidt 				ret = find_extent_in_eb(eb, bytenr,
1087976b1908SJan Schmidt 							*extent_item_pos, &eie);
10886f7ff6d7SFilipe Manana 				btrfs_tree_read_unlock_blocking(eb);
1089976b1908SJan Schmidt 				free_extent_buffer(eb);
1090f5929cd8SFilipe David Borba Manana 				if (ret < 0)
1091f5929cd8SFilipe David Borba Manana 					goto out;
1092f5929cd8SFilipe David Borba Manana 				ref->inode_list = eie;
1093976b1908SJan Schmidt 			}
10944eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(refs, ref->parent,
10954eb1f66dSTakashi Iwai 						  ref->inode_list,
10964eb1f66dSTakashi Iwai 						  (void **)&eie, GFP_NOFS);
1097f1723939SWang Shilong 			if (ret < 0)
1098f1723939SWang Shilong 				goto out;
10993301958bSJan Schmidt 			if (!ret && extent_item_pos) {
11003301958bSJan Schmidt 				/*
11013301958bSJan Schmidt 				 * we've recorded that parent, so we must extend
11023301958bSJan Schmidt 				 * its inode list here
11033301958bSJan Schmidt 				 */
11043301958bSJan Schmidt 				BUG_ON(!eie);
11053301958bSJan Schmidt 				while (eie->next)
11063301958bSJan Schmidt 					eie = eie->next;
11073301958bSJan Schmidt 				eie->next = ref->inode_list;
11083301958bSJan Schmidt 			}
1109f05c4746SWang Shilong 			eie = NULL;
11108da6d581SJan Schmidt 		}
1111a4fdb61eSWang Shilong 		list_del(&ref->list);
1112b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
11138da6d581SJan Schmidt 	}
11148da6d581SJan Schmidt 
11158da6d581SJan Schmidt out:
11168da6d581SJan Schmidt 	btrfs_free_path(path);
11178da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
11188da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
11198da6d581SJan Schmidt 		list_del(&ref->list);
1120b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
11218da6d581SJan Schmidt 	}
11228da6d581SJan Schmidt 	while (!list_empty(&prefs_delayed)) {
11238da6d581SJan Schmidt 		ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
11248da6d581SJan Schmidt 				       list);
11258da6d581SJan Schmidt 		list_del(&ref->list);
1126b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
11278da6d581SJan Schmidt 	}
1128f05c4746SWang Shilong 	if (ret < 0)
1129f05c4746SWang Shilong 		free_inode_elem_list(eie);
11308da6d581SJan Schmidt 	return ret;
11318da6d581SJan Schmidt }
11328da6d581SJan Schmidt 
1133976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks)
1134976b1908SJan Schmidt {
1135976b1908SJan Schmidt 	struct ulist_node *node = NULL;
1136976b1908SJan Schmidt 	struct extent_inode_elem *eie;
1137976b1908SJan Schmidt 	struct ulist_iterator uiter;
1138976b1908SJan Schmidt 
1139976b1908SJan Schmidt 	ULIST_ITER_INIT(&uiter);
1140976b1908SJan Schmidt 	while ((node = ulist_next(blocks, &uiter))) {
1141976b1908SJan Schmidt 		if (!node->aux)
1142976b1908SJan Schmidt 			continue;
1143995e01b7SJan Schmidt 		eie = (struct extent_inode_elem *)(uintptr_t)node->aux;
1144f05c4746SWang Shilong 		free_inode_elem_list(eie);
1145976b1908SJan Schmidt 		node->aux = 0;
1146976b1908SJan Schmidt 	}
1147976b1908SJan Schmidt 
1148976b1908SJan Schmidt 	ulist_free(blocks);
1149976b1908SJan Schmidt }
1150976b1908SJan Schmidt 
11518da6d581SJan Schmidt /*
11528da6d581SJan Schmidt  * Finds all leafs with a reference to the specified combination of bytenr and
11538da6d581SJan Schmidt  * offset. key_list_head will point to a list of corresponding keys (caller must
11548da6d581SJan Schmidt  * free each list element). The leafs will be stored in the leafs ulist, which
11558da6d581SJan Schmidt  * must be freed with ulist_free.
11568da6d581SJan Schmidt  *
11578da6d581SJan Schmidt  * returns 0 on success, <0 on error
11588da6d581SJan Schmidt  */
11598da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
11608da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
1161097b8a7cSJan Schmidt 				u64 time_seq, struct ulist **leafs,
1162976b1908SJan Schmidt 				const u64 *extent_item_pos)
11638da6d581SJan Schmidt {
11648da6d581SJan Schmidt 	int ret;
11658da6d581SJan Schmidt 
11668da6d581SJan Schmidt 	*leafs = ulist_alloc(GFP_NOFS);
116798cfee21SWang Shilong 	if (!*leafs)
11688da6d581SJan Schmidt 		return -ENOMEM;
11698da6d581SJan Schmidt 
1170097b8a7cSJan Schmidt 	ret = find_parent_nodes(trans, fs_info, bytenr,
1171dc046b10SJosef Bacik 				time_seq, *leafs, NULL, extent_item_pos, 0, 0);
11728da6d581SJan Schmidt 	if (ret < 0 && ret != -ENOENT) {
1173976b1908SJan Schmidt 		free_leaf_list(*leafs);
11748da6d581SJan Schmidt 		return ret;
11758da6d581SJan Schmidt 	}
11768da6d581SJan Schmidt 
11778da6d581SJan Schmidt 	return 0;
11788da6d581SJan Schmidt }
11798da6d581SJan Schmidt 
11808da6d581SJan Schmidt /*
11818da6d581SJan Schmidt  * walk all backrefs for a given extent to find all roots that reference this
11828da6d581SJan Schmidt  * extent. Walking a backref means finding all extents that reference this
11838da6d581SJan Schmidt  * extent and in turn walk the backrefs of those, too. Naturally this is a
11848da6d581SJan Schmidt  * recursive process, but here it is implemented in an iterative fashion: We
11858da6d581SJan Schmidt  * find all referencing extents for the extent in question and put them on a
11868da6d581SJan Schmidt  * list. In turn, we find all referencing extents for those, further appending
11878da6d581SJan Schmidt  * to the list. The way we iterate the list allows adding more elements after
11888da6d581SJan Schmidt  * the current while iterating. The process stops when we reach the end of the
11898da6d581SJan Schmidt  * list. Found roots are added to the roots list.
11908da6d581SJan Schmidt  *
11918da6d581SJan Schmidt  * returns 0 on success, < 0 on error.
11928da6d581SJan Schmidt  */
11939e351cc8SJosef Bacik static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans,
11948da6d581SJan Schmidt 				  struct btrfs_fs_info *fs_info, u64 bytenr,
1195097b8a7cSJan Schmidt 				  u64 time_seq, struct ulist **roots)
11968da6d581SJan Schmidt {
11978da6d581SJan Schmidt 	struct ulist *tmp;
11988da6d581SJan Schmidt 	struct ulist_node *node = NULL;
1199cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
12008da6d581SJan Schmidt 	int ret;
12018da6d581SJan Schmidt 
12028da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
12038da6d581SJan Schmidt 	if (!tmp)
12048da6d581SJan Schmidt 		return -ENOMEM;
12058da6d581SJan Schmidt 	*roots = ulist_alloc(GFP_NOFS);
12068da6d581SJan Schmidt 	if (!*roots) {
12078da6d581SJan Schmidt 		ulist_free(tmp);
12088da6d581SJan Schmidt 		return -ENOMEM;
12098da6d581SJan Schmidt 	}
12108da6d581SJan Schmidt 
1211cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&uiter);
12128da6d581SJan Schmidt 	while (1) {
1213097b8a7cSJan Schmidt 		ret = find_parent_nodes(trans, fs_info, bytenr,
1214dc046b10SJosef Bacik 					time_seq, tmp, *roots, NULL, 0, 0);
12158da6d581SJan Schmidt 		if (ret < 0 && ret != -ENOENT) {
12168da6d581SJan Schmidt 			ulist_free(tmp);
12178da6d581SJan Schmidt 			ulist_free(*roots);
12188da6d581SJan Schmidt 			return ret;
12198da6d581SJan Schmidt 		}
1220cd1b413cSJan Schmidt 		node = ulist_next(tmp, &uiter);
12218da6d581SJan Schmidt 		if (!node)
12228da6d581SJan Schmidt 			break;
12238da6d581SJan Schmidt 		bytenr = node->val;
1224bca1a290SWang Shilong 		cond_resched();
12258da6d581SJan Schmidt 	}
12268da6d581SJan Schmidt 
12278da6d581SJan Schmidt 	ulist_free(tmp);
12288da6d581SJan Schmidt 	return 0;
12298da6d581SJan Schmidt }
12308da6d581SJan Schmidt 
12319e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
12329e351cc8SJosef Bacik 			 struct btrfs_fs_info *fs_info, u64 bytenr,
12339e351cc8SJosef Bacik 			 u64 time_seq, struct ulist **roots)
12349e351cc8SJosef Bacik {
12359e351cc8SJosef Bacik 	int ret;
12369e351cc8SJosef Bacik 
12379e351cc8SJosef Bacik 	if (!trans)
12389e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
12399e351cc8SJosef Bacik 	ret = __btrfs_find_all_roots(trans, fs_info, bytenr, time_seq, roots);
12409e351cc8SJosef Bacik 	if (!trans)
12419e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
12429e351cc8SJosef Bacik 	return ret;
12439e351cc8SJosef Bacik }
12449e351cc8SJosef Bacik 
12452c2ed5aaSMark Fasheh /**
12462c2ed5aaSMark Fasheh  * btrfs_check_shared - tell us whether an extent is shared
12472c2ed5aaSMark Fasheh  *
12482c2ed5aaSMark Fasheh  * @trans: optional trans handle
12492c2ed5aaSMark Fasheh  *
12502c2ed5aaSMark Fasheh  * btrfs_check_shared uses the backref walking code but will short
12512c2ed5aaSMark Fasheh  * circuit as soon as it finds a root or inode that doesn't match the
12522c2ed5aaSMark Fasheh  * one passed in. This provides a significant performance benefit for
12532c2ed5aaSMark Fasheh  * callers (such as fiemap) which want to know whether the extent is
12542c2ed5aaSMark Fasheh  * shared but do not need a ref count.
12552c2ed5aaSMark Fasheh  *
12562c2ed5aaSMark Fasheh  * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
12572c2ed5aaSMark Fasheh  */
1258dc046b10SJosef Bacik int btrfs_check_shared(struct btrfs_trans_handle *trans,
1259dc046b10SJosef Bacik 		       struct btrfs_fs_info *fs_info, u64 root_objectid,
1260dc046b10SJosef Bacik 		       u64 inum, u64 bytenr)
1261dc046b10SJosef Bacik {
1262dc046b10SJosef Bacik 	struct ulist *tmp = NULL;
1263dc046b10SJosef Bacik 	struct ulist *roots = NULL;
1264dc046b10SJosef Bacik 	struct ulist_iterator uiter;
1265dc046b10SJosef Bacik 	struct ulist_node *node;
12663284da7bSDavid Sterba 	struct seq_list elem = SEQ_LIST_INIT(elem);
1267dc046b10SJosef Bacik 	int ret = 0;
1268dc046b10SJosef Bacik 
1269dc046b10SJosef Bacik 	tmp = ulist_alloc(GFP_NOFS);
1270dc046b10SJosef Bacik 	roots = ulist_alloc(GFP_NOFS);
1271dc046b10SJosef Bacik 	if (!tmp || !roots) {
1272dc046b10SJosef Bacik 		ulist_free(tmp);
1273dc046b10SJosef Bacik 		ulist_free(roots);
1274dc046b10SJosef Bacik 		return -ENOMEM;
1275dc046b10SJosef Bacik 	}
1276dc046b10SJosef Bacik 
1277dc046b10SJosef Bacik 	if (trans)
1278dc046b10SJosef Bacik 		btrfs_get_tree_mod_seq(fs_info, &elem);
1279dc046b10SJosef Bacik 	else
1280dc046b10SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1281dc046b10SJosef Bacik 	ULIST_ITER_INIT(&uiter);
1282dc046b10SJosef Bacik 	while (1) {
1283dc046b10SJosef Bacik 		ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1284dc046b10SJosef Bacik 					roots, NULL, root_objectid, inum);
1285dc046b10SJosef Bacik 		if (ret == BACKREF_FOUND_SHARED) {
12862c2ed5aaSMark Fasheh 			/* this is the only condition under which we return 1 */
1287dc046b10SJosef Bacik 			ret = 1;
1288dc046b10SJosef Bacik 			break;
1289dc046b10SJosef Bacik 		}
1290dc046b10SJosef Bacik 		if (ret < 0 && ret != -ENOENT)
1291dc046b10SJosef Bacik 			break;
12922c2ed5aaSMark Fasheh 		ret = 0;
1293dc046b10SJosef Bacik 		node = ulist_next(tmp, &uiter);
1294dc046b10SJosef Bacik 		if (!node)
1295dc046b10SJosef Bacik 			break;
1296dc046b10SJosef Bacik 		bytenr = node->val;
1297dc046b10SJosef Bacik 		cond_resched();
1298dc046b10SJosef Bacik 	}
1299dc046b10SJosef Bacik 	if (trans)
1300dc046b10SJosef Bacik 		btrfs_put_tree_mod_seq(fs_info, &elem);
1301dc046b10SJosef Bacik 	else
1302dc046b10SJosef Bacik 		up_read(&fs_info->commit_root_sem);
1303dc046b10SJosef Bacik 	ulist_free(tmp);
1304dc046b10SJosef Bacik 	ulist_free(roots);
1305dc046b10SJosef Bacik 	return ret;
1306dc046b10SJosef Bacik }
1307dc046b10SJosef Bacik 
1308f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1309f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
1310f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
1311f186373fSMark Fasheh 			  u64 *found_off)
1312f186373fSMark Fasheh {
1313f186373fSMark Fasheh 	int ret, slot;
1314f186373fSMark Fasheh 	struct btrfs_key key;
1315f186373fSMark Fasheh 	struct btrfs_key found_key;
1316f186373fSMark Fasheh 	struct btrfs_inode_extref *extref;
1317f186373fSMark Fasheh 	struct extent_buffer *leaf;
1318f186373fSMark Fasheh 	unsigned long ptr;
1319f186373fSMark Fasheh 
1320f186373fSMark Fasheh 	key.objectid = inode_objectid;
1321962a298fSDavid Sterba 	key.type = BTRFS_INODE_EXTREF_KEY;
1322f186373fSMark Fasheh 	key.offset = start_off;
1323f186373fSMark Fasheh 
1324f186373fSMark Fasheh 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1325f186373fSMark Fasheh 	if (ret < 0)
1326f186373fSMark Fasheh 		return ret;
1327f186373fSMark Fasheh 
1328f186373fSMark Fasheh 	while (1) {
1329f186373fSMark Fasheh 		leaf = path->nodes[0];
1330f186373fSMark Fasheh 		slot = path->slots[0];
1331f186373fSMark Fasheh 		if (slot >= btrfs_header_nritems(leaf)) {
1332f186373fSMark Fasheh 			/*
1333f186373fSMark Fasheh 			 * If the item at offset is not found,
1334f186373fSMark Fasheh 			 * btrfs_search_slot will point us to the slot
1335f186373fSMark Fasheh 			 * where it should be inserted. In our case
1336f186373fSMark Fasheh 			 * that will be the slot directly before the
1337f186373fSMark Fasheh 			 * next INODE_REF_KEY_V2 item. In the case
1338f186373fSMark Fasheh 			 * that we're pointing to the last slot in a
1339f186373fSMark Fasheh 			 * leaf, we must move one leaf over.
1340f186373fSMark Fasheh 			 */
1341f186373fSMark Fasheh 			ret = btrfs_next_leaf(root, path);
1342f186373fSMark Fasheh 			if (ret) {
1343f186373fSMark Fasheh 				if (ret >= 1)
1344f186373fSMark Fasheh 					ret = -ENOENT;
1345f186373fSMark Fasheh 				break;
1346f186373fSMark Fasheh 			}
1347f186373fSMark Fasheh 			continue;
1348f186373fSMark Fasheh 		}
1349f186373fSMark Fasheh 
1350f186373fSMark Fasheh 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1351f186373fSMark Fasheh 
1352f186373fSMark Fasheh 		/*
1353f186373fSMark Fasheh 		 * Check that we're still looking at an extended ref key for
1354f186373fSMark Fasheh 		 * this particular objectid. If we have different
1355f186373fSMark Fasheh 		 * objectid or type then there are no more to be found
1356f186373fSMark Fasheh 		 * in the tree and we can exit.
1357f186373fSMark Fasheh 		 */
1358f186373fSMark Fasheh 		ret = -ENOENT;
1359f186373fSMark Fasheh 		if (found_key.objectid != inode_objectid)
1360f186373fSMark Fasheh 			break;
1361962a298fSDavid Sterba 		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
1362f186373fSMark Fasheh 			break;
1363f186373fSMark Fasheh 
1364f186373fSMark Fasheh 		ret = 0;
1365f186373fSMark Fasheh 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1366f186373fSMark Fasheh 		extref = (struct btrfs_inode_extref *)ptr;
1367f186373fSMark Fasheh 		*ret_extref = extref;
1368f186373fSMark Fasheh 		if (found_off)
1369f186373fSMark Fasheh 			*found_off = found_key.offset;
1370f186373fSMark Fasheh 		break;
1371f186373fSMark Fasheh 	}
1372f186373fSMark Fasheh 
1373f186373fSMark Fasheh 	return ret;
1374f186373fSMark Fasheh }
1375f186373fSMark Fasheh 
137648a3b636SEric Sandeen /*
137748a3b636SEric Sandeen  * this iterates to turn a name (from iref/extref) into a full filesystem path.
137848a3b636SEric Sandeen  * Elements of the path are separated by '/' and the path is guaranteed to be
137948a3b636SEric Sandeen  * 0-terminated. the path is only given within the current file system.
138048a3b636SEric Sandeen  * Therefore, it never starts with a '/'. the caller is responsible to provide
138148a3b636SEric Sandeen  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
138248a3b636SEric Sandeen  * the start point of the resulting string is returned. this pointer is within
138348a3b636SEric Sandeen  * dest, normally.
138448a3b636SEric Sandeen  * in case the path buffer would overflow, the pointer is decremented further
138548a3b636SEric Sandeen  * as if output was written to the buffer, though no more output is actually
138648a3b636SEric Sandeen  * generated. that way, the caller can determine how much space would be
138748a3b636SEric Sandeen  * required for the path to fit into the buffer. in that case, the returned
138848a3b636SEric Sandeen  * value will be smaller than dest. callers must check this!
138948a3b636SEric Sandeen  */
139096b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1391d24bec3aSMark Fasheh 			u32 name_len, unsigned long name_off,
1392a542ad1bSJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
1393a542ad1bSJan Schmidt 			char *dest, u32 size)
1394a542ad1bSJan Schmidt {
1395a542ad1bSJan Schmidt 	int slot;
1396a542ad1bSJan Schmidt 	u64 next_inum;
1397a542ad1bSJan Schmidt 	int ret;
1398661bec6bSGabriel de Perthuis 	s64 bytes_left = ((s64)size) - 1;
1399a542ad1bSJan Schmidt 	struct extent_buffer *eb = eb_in;
1400a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1401b916a59aSJan Schmidt 	int leave_spinning = path->leave_spinning;
1402d24bec3aSMark Fasheh 	struct btrfs_inode_ref *iref;
1403a542ad1bSJan Schmidt 
1404a542ad1bSJan Schmidt 	if (bytes_left >= 0)
1405a542ad1bSJan Schmidt 		dest[bytes_left] = '\0';
1406a542ad1bSJan Schmidt 
1407b916a59aSJan Schmidt 	path->leave_spinning = 1;
1408a542ad1bSJan Schmidt 	while (1) {
1409d24bec3aSMark Fasheh 		bytes_left -= name_len;
1410a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1411a542ad1bSJan Schmidt 			read_extent_buffer(eb, dest + bytes_left,
1412d24bec3aSMark Fasheh 					   name_off, name_len);
1413b916a59aSJan Schmidt 		if (eb != eb_in) {
1414b916a59aSJan Schmidt 			btrfs_tree_read_unlock_blocking(eb);
1415a542ad1bSJan Schmidt 			free_extent_buffer(eb);
1416b916a59aSJan Schmidt 		}
1417c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, parent, 0,
1418c234a24dSDavid Sterba 				BTRFS_INODE_REF_KEY, &found_key);
14198f24b496SJan Schmidt 		if (ret > 0)
14208f24b496SJan Schmidt 			ret = -ENOENT;
1421a542ad1bSJan Schmidt 		if (ret)
1422a542ad1bSJan Schmidt 			break;
1423d24bec3aSMark Fasheh 
1424a542ad1bSJan Schmidt 		next_inum = found_key.offset;
1425a542ad1bSJan Schmidt 
1426a542ad1bSJan Schmidt 		/* regular exit ahead */
1427a542ad1bSJan Schmidt 		if (parent == next_inum)
1428a542ad1bSJan Schmidt 			break;
1429a542ad1bSJan Schmidt 
1430a542ad1bSJan Schmidt 		slot = path->slots[0];
1431a542ad1bSJan Schmidt 		eb = path->nodes[0];
1432a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
1433b916a59aSJan Schmidt 		if (eb != eb_in) {
1434a542ad1bSJan Schmidt 			atomic_inc(&eb->refs);
1435b916a59aSJan Schmidt 			btrfs_tree_read_lock(eb);
1436b916a59aSJan Schmidt 			btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1437b916a59aSJan Schmidt 		}
1438a542ad1bSJan Schmidt 		btrfs_release_path(path);
1439a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1440d24bec3aSMark Fasheh 
1441d24bec3aSMark Fasheh 		name_len = btrfs_inode_ref_name_len(eb, iref);
1442d24bec3aSMark Fasheh 		name_off = (unsigned long)(iref + 1);
1443d24bec3aSMark Fasheh 
1444a542ad1bSJan Schmidt 		parent = next_inum;
1445a542ad1bSJan Schmidt 		--bytes_left;
1446a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1447a542ad1bSJan Schmidt 			dest[bytes_left] = '/';
1448a542ad1bSJan Schmidt 	}
1449a542ad1bSJan Schmidt 
1450a542ad1bSJan Schmidt 	btrfs_release_path(path);
1451b916a59aSJan Schmidt 	path->leave_spinning = leave_spinning;
1452a542ad1bSJan Schmidt 
1453a542ad1bSJan Schmidt 	if (ret)
1454a542ad1bSJan Schmidt 		return ERR_PTR(ret);
1455a542ad1bSJan Schmidt 
1456a542ad1bSJan Schmidt 	return dest + bytes_left;
1457a542ad1bSJan Schmidt }
1458a542ad1bSJan Schmidt 
1459a542ad1bSJan Schmidt /*
1460a542ad1bSJan Schmidt  * this makes the path point to (logical EXTENT_ITEM *)
1461a542ad1bSJan Schmidt  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1462a542ad1bSJan Schmidt  * tree blocks and <0 on error.
1463a542ad1bSJan Schmidt  */
1464a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
146569917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
146669917e43SLiu Bo 			u64 *flags_ret)
1467a542ad1bSJan Schmidt {
1468a542ad1bSJan Schmidt 	int ret;
1469a542ad1bSJan Schmidt 	u64 flags;
1470261c84b6SJosef Bacik 	u64 size = 0;
1471a542ad1bSJan Schmidt 	u32 item_size;
1472a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1473a542ad1bSJan Schmidt 	struct btrfs_extent_item *ei;
1474a542ad1bSJan Schmidt 	struct btrfs_key key;
1475a542ad1bSJan Schmidt 
1476261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1477261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1478261c84b6SJosef Bacik 	else
1479a542ad1bSJan Schmidt 		key.type = BTRFS_EXTENT_ITEM_KEY;
1480a542ad1bSJan Schmidt 	key.objectid = logical;
1481a542ad1bSJan Schmidt 	key.offset = (u64)-1;
1482a542ad1bSJan Schmidt 
1483a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1484a542ad1bSJan Schmidt 	if (ret < 0)
1485a542ad1bSJan Schmidt 		return ret;
1486a542ad1bSJan Schmidt 
1487850a8cdfSWang Shilong 	ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1488850a8cdfSWang Shilong 	if (ret) {
1489850a8cdfSWang Shilong 		if (ret > 0)
1490580f0a67SJosef Bacik 			ret = -ENOENT;
1491580f0a67SJosef Bacik 		return ret;
1492580f0a67SJosef Bacik 	}
1493850a8cdfSWang Shilong 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1494261c84b6SJosef Bacik 	if (found_key->type == BTRFS_METADATA_ITEM_KEY)
1495707e8a07SDavid Sterba 		size = fs_info->extent_root->nodesize;
1496261c84b6SJosef Bacik 	else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1497261c84b6SJosef Bacik 		size = found_key->offset;
1498261c84b6SJosef Bacik 
1499580f0a67SJosef Bacik 	if (found_key->objectid > logical ||
1500261c84b6SJosef Bacik 	    found_key->objectid + size <= logical) {
1501c1c9ff7cSGeert Uytterhoeven 		pr_debug("logical %llu is not within any extent\n", logical);
1502a542ad1bSJan Schmidt 		return -ENOENT;
15034692cf58SJan Schmidt 	}
1504a542ad1bSJan Schmidt 
1505a542ad1bSJan Schmidt 	eb = path->nodes[0];
1506a542ad1bSJan Schmidt 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1507a542ad1bSJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
1508a542ad1bSJan Schmidt 
1509a542ad1bSJan Schmidt 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1510a542ad1bSJan Schmidt 	flags = btrfs_extent_flags(eb, ei);
1511a542ad1bSJan Schmidt 
15124692cf58SJan Schmidt 	pr_debug("logical %llu is at position %llu within the extent (%llu "
15134692cf58SJan Schmidt 		 "EXTENT_ITEM %llu) flags %#llx size %u\n",
1514c1c9ff7cSGeert Uytterhoeven 		 logical, logical - found_key->objectid, found_key->objectid,
1515c1c9ff7cSGeert Uytterhoeven 		 found_key->offset, flags, item_size);
151669917e43SLiu Bo 
151769917e43SLiu Bo 	WARN_ON(!flags_ret);
151869917e43SLiu Bo 	if (flags_ret) {
1519a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
152069917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
152169917e43SLiu Bo 		else if (flags & BTRFS_EXTENT_FLAG_DATA)
152269917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_DATA;
152369917e43SLiu Bo 		else
152469917e43SLiu Bo 			BUG_ON(1);
152569917e43SLiu Bo 		return 0;
152669917e43SLiu Bo 	}
1527a542ad1bSJan Schmidt 
1528a542ad1bSJan Schmidt 	return -EIO;
1529a542ad1bSJan Schmidt }
1530a542ad1bSJan Schmidt 
1531a542ad1bSJan Schmidt /*
1532a542ad1bSJan Schmidt  * helper function to iterate extent inline refs. ptr must point to a 0 value
1533a542ad1bSJan Schmidt  * for the first call and may be modified. it is used to track state.
1534a542ad1bSJan Schmidt  * if more refs exist, 0 is returned and the next call to
1535a542ad1bSJan Schmidt  * __get_extent_inline_ref must pass the modified ptr parameter to get the
1536a542ad1bSJan Schmidt  * next ref. after the last ref was processed, 1 is returned.
1537a542ad1bSJan Schmidt  * returns <0 on error
1538a542ad1bSJan Schmidt  */
1539a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
15406eda71d0SLiu Bo 				   struct btrfs_key *key,
1541a542ad1bSJan Schmidt 				   struct btrfs_extent_item *ei, u32 item_size,
1542a542ad1bSJan Schmidt 				   struct btrfs_extent_inline_ref **out_eiref,
1543a542ad1bSJan Schmidt 				   int *out_type)
1544a542ad1bSJan Schmidt {
1545a542ad1bSJan Schmidt 	unsigned long end;
1546a542ad1bSJan Schmidt 	u64 flags;
1547a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1548a542ad1bSJan Schmidt 
1549a542ad1bSJan Schmidt 	if (!*ptr) {
1550a542ad1bSJan Schmidt 		/* first call */
1551a542ad1bSJan Schmidt 		flags = btrfs_extent_flags(eb, ei);
1552a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
15536eda71d0SLiu Bo 			if (key->type == BTRFS_METADATA_ITEM_KEY) {
15546eda71d0SLiu Bo 				/* a skinny metadata extent */
15556eda71d0SLiu Bo 				*out_eiref =
15566eda71d0SLiu Bo 				     (struct btrfs_extent_inline_ref *)(ei + 1);
15576eda71d0SLiu Bo 			} else {
15586eda71d0SLiu Bo 				WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1559a542ad1bSJan Schmidt 				info = (struct btrfs_tree_block_info *)(ei + 1);
1560a542ad1bSJan Schmidt 				*out_eiref =
1561a542ad1bSJan Schmidt 				   (struct btrfs_extent_inline_ref *)(info + 1);
15626eda71d0SLiu Bo 			}
1563a542ad1bSJan Schmidt 		} else {
1564a542ad1bSJan Schmidt 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1565a542ad1bSJan Schmidt 		}
1566a542ad1bSJan Schmidt 		*ptr = (unsigned long)*out_eiref;
1567cd857dd6SLiu Bo 		if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
1568a542ad1bSJan Schmidt 			return -ENOENT;
1569a542ad1bSJan Schmidt 	}
1570a542ad1bSJan Schmidt 
1571a542ad1bSJan Schmidt 	end = (unsigned long)ei + item_size;
15726eda71d0SLiu Bo 	*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
1573a542ad1bSJan Schmidt 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1574a542ad1bSJan Schmidt 
1575a542ad1bSJan Schmidt 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1576a542ad1bSJan Schmidt 	WARN_ON(*ptr > end);
1577a542ad1bSJan Schmidt 	if (*ptr == end)
1578a542ad1bSJan Schmidt 		return 1; /* last */
1579a542ad1bSJan Schmidt 
1580a542ad1bSJan Schmidt 	return 0;
1581a542ad1bSJan Schmidt }
1582a542ad1bSJan Schmidt 
1583a542ad1bSJan Schmidt /*
1584a542ad1bSJan Schmidt  * reads the tree block backref for an extent. tree level and root are returned
1585a542ad1bSJan Schmidt  * through out_level and out_root. ptr must point to a 0 value for the first
1586a542ad1bSJan Schmidt  * call and may be modified (see __get_extent_inline_ref comment).
1587a542ad1bSJan Schmidt  * returns 0 if data was provided, 1 if there was no more data to provide or
1588a542ad1bSJan Schmidt  * <0 on error.
1589a542ad1bSJan Schmidt  */
1590a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
15916eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
15926eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level)
1593a542ad1bSJan Schmidt {
1594a542ad1bSJan Schmidt 	int ret;
1595a542ad1bSJan Schmidt 	int type;
1596a542ad1bSJan Schmidt 	struct btrfs_extent_inline_ref *eiref;
1597a542ad1bSJan Schmidt 
1598a542ad1bSJan Schmidt 	if (*ptr == (unsigned long)-1)
1599a542ad1bSJan Schmidt 		return 1;
1600a542ad1bSJan Schmidt 
1601a542ad1bSJan Schmidt 	while (1) {
16026eda71d0SLiu Bo 		ret = __get_extent_inline_ref(ptr, eb, key, ei, item_size,
1603a542ad1bSJan Schmidt 					      &eiref, &type);
1604a542ad1bSJan Schmidt 		if (ret < 0)
1605a542ad1bSJan Schmidt 			return ret;
1606a542ad1bSJan Schmidt 
1607a542ad1bSJan Schmidt 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1608a542ad1bSJan Schmidt 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1609a542ad1bSJan Schmidt 			break;
1610a542ad1bSJan Schmidt 
1611a542ad1bSJan Schmidt 		if (ret == 1)
1612a542ad1bSJan Schmidt 			return 1;
1613a542ad1bSJan Schmidt 	}
1614a542ad1bSJan Schmidt 
1615a542ad1bSJan Schmidt 	/* we can treat both ref types equally here */
1616a542ad1bSJan Schmidt 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1617a1317f45SFilipe Manana 
1618a1317f45SFilipe Manana 	if (key->type == BTRFS_EXTENT_ITEM_KEY) {
1619a1317f45SFilipe Manana 		struct btrfs_tree_block_info *info;
1620a1317f45SFilipe Manana 
1621a1317f45SFilipe Manana 		info = (struct btrfs_tree_block_info *)(ei + 1);
1622a542ad1bSJan Schmidt 		*out_level = btrfs_tree_block_level(eb, info);
1623a1317f45SFilipe Manana 	} else {
1624a1317f45SFilipe Manana 		ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
1625a1317f45SFilipe Manana 		*out_level = (u8)key->offset;
1626a1317f45SFilipe Manana 	}
1627a542ad1bSJan Schmidt 
1628a542ad1bSJan Schmidt 	if (ret == 1)
1629a542ad1bSJan Schmidt 		*ptr = (unsigned long)-1;
1630a542ad1bSJan Schmidt 
1631a542ad1bSJan Schmidt 	return 0;
1632a542ad1bSJan Schmidt }
1633a542ad1bSJan Schmidt 
1634976b1908SJan Schmidt static int iterate_leaf_refs(struct extent_inode_elem *inode_list,
1635976b1908SJan Schmidt 				u64 root, u64 extent_item_objectid,
16364692cf58SJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1637a542ad1bSJan Schmidt {
1638976b1908SJan Schmidt 	struct extent_inode_elem *eie;
16394692cf58SJan Schmidt 	int ret = 0;
1640a542ad1bSJan Schmidt 
1641976b1908SJan Schmidt 	for (eie = inode_list; eie; eie = eie->next) {
16424692cf58SJan Schmidt 		pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
1643976b1908SJan Schmidt 			 "root %llu\n", extent_item_objectid,
1644976b1908SJan Schmidt 			 eie->inum, eie->offset, root);
1645976b1908SJan Schmidt 		ret = iterate(eie->inum, eie->offset, root, ctx);
16464692cf58SJan Schmidt 		if (ret) {
1647976b1908SJan Schmidt 			pr_debug("stopping iteration for %llu due to ret=%d\n",
1648976b1908SJan Schmidt 				 extent_item_objectid, ret);
1649a542ad1bSJan Schmidt 			break;
1650a542ad1bSJan Schmidt 		}
1651a542ad1bSJan Schmidt 	}
1652a542ad1bSJan Schmidt 
1653a542ad1bSJan Schmidt 	return ret;
1654a542ad1bSJan Schmidt }
1655a542ad1bSJan Schmidt 
1656a542ad1bSJan Schmidt /*
1657a542ad1bSJan Schmidt  * calls iterate() for every inode that references the extent identified by
16584692cf58SJan Schmidt  * the given parameters.
1659a542ad1bSJan Schmidt  * when the iterator function returns a non-zero value, iteration stops.
1660a542ad1bSJan Schmidt  */
1661a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
16624692cf58SJan Schmidt 				u64 extent_item_objectid, u64 extent_item_pos,
16637a3ae2f8SJan Schmidt 				int search_commit_root,
1664a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1665a542ad1bSJan Schmidt {
1666a542ad1bSJan Schmidt 	int ret;
1667da61d31aSJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
16687a3ae2f8SJan Schmidt 	struct ulist *refs = NULL;
16697a3ae2f8SJan Schmidt 	struct ulist *roots = NULL;
16704692cf58SJan Schmidt 	struct ulist_node *ref_node = NULL;
16714692cf58SJan Schmidt 	struct ulist_node *root_node = NULL;
16723284da7bSDavid Sterba 	struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
1673cd1b413cSJan Schmidt 	struct ulist_iterator ref_uiter;
1674cd1b413cSJan Schmidt 	struct ulist_iterator root_uiter;
1675a542ad1bSJan Schmidt 
16764692cf58SJan Schmidt 	pr_debug("resolving all inodes for extent %llu\n",
16774692cf58SJan Schmidt 			extent_item_objectid);
16784692cf58SJan Schmidt 
1679da61d31aSJosef Bacik 	if (!search_commit_root) {
16807a3ae2f8SJan Schmidt 		trans = btrfs_join_transaction(fs_info->extent_root);
16817a3ae2f8SJan Schmidt 		if (IS_ERR(trans))
16827a3ae2f8SJan Schmidt 			return PTR_ERR(trans);
16838445f61cSJan Schmidt 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
16849e351cc8SJosef Bacik 	} else {
16859e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
16867a3ae2f8SJan Schmidt 	}
16874692cf58SJan Schmidt 
16884692cf58SJan Schmidt 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1689097b8a7cSJan Schmidt 				   tree_mod_seq_elem.seq, &refs,
16908445f61cSJan Schmidt 				   &extent_item_pos);
16914692cf58SJan Schmidt 	if (ret)
16924692cf58SJan Schmidt 		goto out;
16934692cf58SJan Schmidt 
1694cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&ref_uiter);
1695cd1b413cSJan Schmidt 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
16969e351cc8SJosef Bacik 		ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val,
16978445f61cSJan Schmidt 					     tree_mod_seq_elem.seq, &roots);
16984692cf58SJan Schmidt 		if (ret)
1699a542ad1bSJan Schmidt 			break;
1700cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&root_uiter);
1701cd1b413cSJan Schmidt 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
1702976b1908SJan Schmidt 			pr_debug("root %llu references leaf %llu, data list "
170334d73f54SAlexander Block 				 "%#llx\n", root_node->val, ref_node->val,
1704c1c9ff7cSGeert Uytterhoeven 				 ref_node->aux);
1705995e01b7SJan Schmidt 			ret = iterate_leaf_refs((struct extent_inode_elem *)
1706995e01b7SJan Schmidt 						(uintptr_t)ref_node->aux,
1707995e01b7SJan Schmidt 						root_node->val,
1708995e01b7SJan Schmidt 						extent_item_objectid,
1709a542ad1bSJan Schmidt 						iterate, ctx);
17104692cf58SJan Schmidt 		}
1711976b1908SJan Schmidt 		ulist_free(roots);
1712a542ad1bSJan Schmidt 	}
1713a542ad1bSJan Schmidt 
1714976b1908SJan Schmidt 	free_leaf_list(refs);
17154692cf58SJan Schmidt out:
17167a3ae2f8SJan Schmidt 	if (!search_commit_root) {
17178445f61cSJan Schmidt 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
17184692cf58SJan Schmidt 		btrfs_end_transaction(trans, fs_info->extent_root);
17199e351cc8SJosef Bacik 	} else {
17209e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
17217a3ae2f8SJan Schmidt 	}
17227a3ae2f8SJan Schmidt 
1723a542ad1bSJan Schmidt 	return ret;
1724a542ad1bSJan Schmidt }
1725a542ad1bSJan Schmidt 
1726a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1727a542ad1bSJan Schmidt 				struct btrfs_path *path,
1728a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1729a542ad1bSJan Schmidt {
1730a542ad1bSJan Schmidt 	int ret;
17314692cf58SJan Schmidt 	u64 extent_item_pos;
173269917e43SLiu Bo 	u64 flags = 0;
1733a542ad1bSJan Schmidt 	struct btrfs_key found_key;
17347a3ae2f8SJan Schmidt 	int search_commit_root = path->search_commit_root;
1735a542ad1bSJan Schmidt 
173669917e43SLiu Bo 	ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
17374692cf58SJan Schmidt 	btrfs_release_path(path);
1738a542ad1bSJan Schmidt 	if (ret < 0)
1739a542ad1bSJan Schmidt 		return ret;
174069917e43SLiu Bo 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
17413627bf45SStefan Behrens 		return -EINVAL;
1742a542ad1bSJan Schmidt 
17434692cf58SJan Schmidt 	extent_item_pos = logical - found_key.objectid;
17447a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
17457a3ae2f8SJan Schmidt 					extent_item_pos, search_commit_root,
17467a3ae2f8SJan Schmidt 					iterate, ctx);
1747a542ad1bSJan Schmidt 
1748a542ad1bSJan Schmidt 	return ret;
1749a542ad1bSJan Schmidt }
1750a542ad1bSJan Schmidt 
1751d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1752d24bec3aSMark Fasheh 			      struct extent_buffer *eb, void *ctx);
1753d24bec3aSMark Fasheh 
1754d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1755a542ad1bSJan Schmidt 			      struct btrfs_path *path,
1756a542ad1bSJan Schmidt 			      iterate_irefs_t *iterate, void *ctx)
1757a542ad1bSJan Schmidt {
1758aefc1eb1SJan Schmidt 	int ret = 0;
1759a542ad1bSJan Schmidt 	int slot;
1760a542ad1bSJan Schmidt 	u32 cur;
1761a542ad1bSJan Schmidt 	u32 len;
1762a542ad1bSJan Schmidt 	u32 name_len;
1763a542ad1bSJan Schmidt 	u64 parent = 0;
1764a542ad1bSJan Schmidt 	int found = 0;
1765a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1766a542ad1bSJan Schmidt 	struct btrfs_item *item;
1767a542ad1bSJan Schmidt 	struct btrfs_inode_ref *iref;
1768a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1769a542ad1bSJan Schmidt 
1770aefc1eb1SJan Schmidt 	while (!ret) {
1771c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, inum,
1772c234a24dSDavid Sterba 				parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
1773a542ad1bSJan Schmidt 				&found_key);
1774c234a24dSDavid Sterba 
1775a542ad1bSJan Schmidt 		if (ret < 0)
1776a542ad1bSJan Schmidt 			break;
1777a542ad1bSJan Schmidt 		if (ret) {
1778a542ad1bSJan Schmidt 			ret = found ? 0 : -ENOENT;
1779a542ad1bSJan Schmidt 			break;
1780a542ad1bSJan Schmidt 		}
1781a542ad1bSJan Schmidt 		++found;
1782a542ad1bSJan Schmidt 
1783a542ad1bSJan Schmidt 		parent = found_key.offset;
1784a542ad1bSJan Schmidt 		slot = path->slots[0];
17853fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
17863fe81ce2SFilipe David Borba Manana 		if (!eb) {
17873fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
17883fe81ce2SFilipe David Borba Manana 			break;
17893fe81ce2SFilipe David Borba Manana 		}
17903fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
1791b916a59aSJan Schmidt 		btrfs_tree_read_lock(eb);
1792b916a59aSJan Schmidt 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1793a542ad1bSJan Schmidt 		btrfs_release_path(path);
1794a542ad1bSJan Schmidt 
1795dd3cc16bSRoss Kirk 		item = btrfs_item_nr(slot);
1796a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1797a542ad1bSJan Schmidt 
1798a542ad1bSJan Schmidt 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1799a542ad1bSJan Schmidt 			name_len = btrfs_inode_ref_name_len(eb, iref);
1800a542ad1bSJan Schmidt 			/* path must be released before calling iterate()! */
18014692cf58SJan Schmidt 			pr_debug("following ref at offset %u for inode %llu in "
1802c1c9ff7cSGeert Uytterhoeven 				 "tree %llu\n", cur, found_key.objectid,
1803c1c9ff7cSGeert Uytterhoeven 				 fs_root->objectid);
1804d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
1805d24bec3aSMark Fasheh 				      (unsigned long)(iref + 1), eb, ctx);
1806aefc1eb1SJan Schmidt 			if (ret)
1807a542ad1bSJan Schmidt 				break;
1808a542ad1bSJan Schmidt 			len = sizeof(*iref) + name_len;
1809a542ad1bSJan Schmidt 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
1810a542ad1bSJan Schmidt 		}
1811b916a59aSJan Schmidt 		btrfs_tree_read_unlock_blocking(eb);
1812a542ad1bSJan Schmidt 		free_extent_buffer(eb);
1813a542ad1bSJan Schmidt 	}
1814a542ad1bSJan Schmidt 
1815a542ad1bSJan Schmidt 	btrfs_release_path(path);
1816a542ad1bSJan Schmidt 
1817a542ad1bSJan Schmidt 	return ret;
1818a542ad1bSJan Schmidt }
1819a542ad1bSJan Schmidt 
1820d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
1821d24bec3aSMark Fasheh 				 struct btrfs_path *path,
1822d24bec3aSMark Fasheh 				 iterate_irefs_t *iterate, void *ctx)
1823d24bec3aSMark Fasheh {
1824d24bec3aSMark Fasheh 	int ret;
1825d24bec3aSMark Fasheh 	int slot;
1826d24bec3aSMark Fasheh 	u64 offset = 0;
1827d24bec3aSMark Fasheh 	u64 parent;
1828d24bec3aSMark Fasheh 	int found = 0;
1829d24bec3aSMark Fasheh 	struct extent_buffer *eb;
1830d24bec3aSMark Fasheh 	struct btrfs_inode_extref *extref;
1831d24bec3aSMark Fasheh 	struct extent_buffer *leaf;
1832d24bec3aSMark Fasheh 	u32 item_size;
1833d24bec3aSMark Fasheh 	u32 cur_offset;
1834d24bec3aSMark Fasheh 	unsigned long ptr;
1835d24bec3aSMark Fasheh 
1836d24bec3aSMark Fasheh 	while (1) {
1837d24bec3aSMark Fasheh 		ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
1838d24bec3aSMark Fasheh 					    &offset);
1839d24bec3aSMark Fasheh 		if (ret < 0)
1840d24bec3aSMark Fasheh 			break;
1841d24bec3aSMark Fasheh 		if (ret) {
1842d24bec3aSMark Fasheh 			ret = found ? 0 : -ENOENT;
1843d24bec3aSMark Fasheh 			break;
1844d24bec3aSMark Fasheh 		}
1845d24bec3aSMark Fasheh 		++found;
1846d24bec3aSMark Fasheh 
1847d24bec3aSMark Fasheh 		slot = path->slots[0];
18483fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
18493fe81ce2SFilipe David Borba Manana 		if (!eb) {
18503fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
18513fe81ce2SFilipe David Borba Manana 			break;
18523fe81ce2SFilipe David Borba Manana 		}
18533fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
1854d24bec3aSMark Fasheh 
1855d24bec3aSMark Fasheh 		btrfs_tree_read_lock(eb);
1856d24bec3aSMark Fasheh 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1857d24bec3aSMark Fasheh 		btrfs_release_path(path);
1858d24bec3aSMark Fasheh 
1859d24bec3aSMark Fasheh 		leaf = path->nodes[0];
1860e94acd86SValentina Giusti 		item_size = btrfs_item_size_nr(leaf, slot);
1861e94acd86SValentina Giusti 		ptr = btrfs_item_ptr_offset(leaf, slot);
1862d24bec3aSMark Fasheh 		cur_offset = 0;
1863d24bec3aSMark Fasheh 
1864d24bec3aSMark Fasheh 		while (cur_offset < item_size) {
1865d24bec3aSMark Fasheh 			u32 name_len;
1866d24bec3aSMark Fasheh 
1867d24bec3aSMark Fasheh 			extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
1868d24bec3aSMark Fasheh 			parent = btrfs_inode_extref_parent(eb, extref);
1869d24bec3aSMark Fasheh 			name_len = btrfs_inode_extref_name_len(eb, extref);
1870d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
1871d24bec3aSMark Fasheh 				      (unsigned long)&extref->name, eb, ctx);
1872d24bec3aSMark Fasheh 			if (ret)
1873d24bec3aSMark Fasheh 				break;
1874d24bec3aSMark Fasheh 
1875d24bec3aSMark Fasheh 			cur_offset += btrfs_inode_extref_name_len(leaf, extref);
1876d24bec3aSMark Fasheh 			cur_offset += sizeof(*extref);
1877d24bec3aSMark Fasheh 		}
1878d24bec3aSMark Fasheh 		btrfs_tree_read_unlock_blocking(eb);
1879d24bec3aSMark Fasheh 		free_extent_buffer(eb);
1880d24bec3aSMark Fasheh 
1881d24bec3aSMark Fasheh 		offset++;
1882d24bec3aSMark Fasheh 	}
1883d24bec3aSMark Fasheh 
1884d24bec3aSMark Fasheh 	btrfs_release_path(path);
1885d24bec3aSMark Fasheh 
1886d24bec3aSMark Fasheh 	return ret;
1887d24bec3aSMark Fasheh }
1888d24bec3aSMark Fasheh 
1889d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
1890d24bec3aSMark Fasheh 			 struct btrfs_path *path, iterate_irefs_t *iterate,
1891d24bec3aSMark Fasheh 			 void *ctx)
1892d24bec3aSMark Fasheh {
1893d24bec3aSMark Fasheh 	int ret;
1894d24bec3aSMark Fasheh 	int found_refs = 0;
1895d24bec3aSMark Fasheh 
1896d24bec3aSMark Fasheh 	ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
1897d24bec3aSMark Fasheh 	if (!ret)
1898d24bec3aSMark Fasheh 		++found_refs;
1899d24bec3aSMark Fasheh 	else if (ret != -ENOENT)
1900d24bec3aSMark Fasheh 		return ret;
1901d24bec3aSMark Fasheh 
1902d24bec3aSMark Fasheh 	ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
1903d24bec3aSMark Fasheh 	if (ret == -ENOENT && found_refs)
1904d24bec3aSMark Fasheh 		return 0;
1905d24bec3aSMark Fasheh 
1906d24bec3aSMark Fasheh 	return ret;
1907d24bec3aSMark Fasheh }
1908d24bec3aSMark Fasheh 
1909a542ad1bSJan Schmidt /*
1910a542ad1bSJan Schmidt  * returns 0 if the path could be dumped (probably truncated)
1911a542ad1bSJan Schmidt  * returns <0 in case of an error
1912a542ad1bSJan Schmidt  */
1913d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
1914a542ad1bSJan Schmidt 			 struct extent_buffer *eb, void *ctx)
1915a542ad1bSJan Schmidt {
1916a542ad1bSJan Schmidt 	struct inode_fs_paths *ipath = ctx;
1917a542ad1bSJan Schmidt 	char *fspath;
1918a542ad1bSJan Schmidt 	char *fspath_min;
1919a542ad1bSJan Schmidt 	int i = ipath->fspath->elem_cnt;
1920a542ad1bSJan Schmidt 	const int s_ptr = sizeof(char *);
1921a542ad1bSJan Schmidt 	u32 bytes_left;
1922a542ad1bSJan Schmidt 
1923a542ad1bSJan Schmidt 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
1924a542ad1bSJan Schmidt 					ipath->fspath->bytes_left - s_ptr : 0;
1925a542ad1bSJan Schmidt 
1926740c3d22SChris Mason 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
192796b5bd77SJan Schmidt 	fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
192896b5bd77SJan Schmidt 				   name_off, eb, inum, fspath_min, bytes_left);
1929a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
1930a542ad1bSJan Schmidt 		return PTR_ERR(fspath);
1931a542ad1bSJan Schmidt 
1932a542ad1bSJan Schmidt 	if (fspath > fspath_min) {
1933745c4d8eSJeff Mahoney 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
1934a542ad1bSJan Schmidt 		++ipath->fspath->elem_cnt;
1935a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = fspath - fspath_min;
1936a542ad1bSJan Schmidt 	} else {
1937a542ad1bSJan Schmidt 		++ipath->fspath->elem_missed;
1938a542ad1bSJan Schmidt 		ipath->fspath->bytes_missing += fspath_min - fspath;
1939a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = 0;
1940a542ad1bSJan Schmidt 	}
1941a542ad1bSJan Schmidt 
1942a542ad1bSJan Schmidt 	return 0;
1943a542ad1bSJan Schmidt }
1944a542ad1bSJan Schmidt 
1945a542ad1bSJan Schmidt /*
1946a542ad1bSJan Schmidt  * this dumps all file system paths to the inode into the ipath struct, provided
1947a542ad1bSJan Schmidt  * is has been created large enough. each path is zero-terminated and accessed
1948740c3d22SChris Mason  * from ipath->fspath->val[i].
1949a542ad1bSJan Schmidt  * when it returns, there are ipath->fspath->elem_cnt number of paths available
1950740c3d22SChris Mason  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
1951a542ad1bSJan Schmidt  * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1952a542ad1bSJan Schmidt  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1953a542ad1bSJan Schmidt  * have been needed to return all paths.
1954a542ad1bSJan Schmidt  */
1955a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
1956a542ad1bSJan Schmidt {
1957a542ad1bSJan Schmidt 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
1958a542ad1bSJan Schmidt 			     inode_to_path, ipath);
1959a542ad1bSJan Schmidt }
1960a542ad1bSJan Schmidt 
1961a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes)
1962a542ad1bSJan Schmidt {
1963a542ad1bSJan Schmidt 	struct btrfs_data_container *data;
1964a542ad1bSJan Schmidt 	size_t alloc_bytes;
1965a542ad1bSJan Schmidt 
1966a542ad1bSJan Schmidt 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
1967425d17a2SLiu Bo 	data = vmalloc(alloc_bytes);
1968a542ad1bSJan Schmidt 	if (!data)
1969a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
1970a542ad1bSJan Schmidt 
1971a542ad1bSJan Schmidt 	if (total_bytes >= sizeof(*data)) {
1972a542ad1bSJan Schmidt 		data->bytes_left = total_bytes - sizeof(*data);
1973a542ad1bSJan Schmidt 		data->bytes_missing = 0;
1974a542ad1bSJan Schmidt 	} else {
1975a542ad1bSJan Schmidt 		data->bytes_missing = sizeof(*data) - total_bytes;
1976a542ad1bSJan Schmidt 		data->bytes_left = 0;
1977a542ad1bSJan Schmidt 	}
1978a542ad1bSJan Schmidt 
1979a542ad1bSJan Schmidt 	data->elem_cnt = 0;
1980a542ad1bSJan Schmidt 	data->elem_missed = 0;
1981a542ad1bSJan Schmidt 
1982a542ad1bSJan Schmidt 	return data;
1983a542ad1bSJan Schmidt }
1984a542ad1bSJan Schmidt 
1985a542ad1bSJan Schmidt /*
1986a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
1987a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
1988a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
1989a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
1990a542ad1bSJan Schmidt  */
1991a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
1992a542ad1bSJan Schmidt 					struct btrfs_path *path)
1993a542ad1bSJan Schmidt {
1994a542ad1bSJan Schmidt 	struct inode_fs_paths *ifp;
1995a542ad1bSJan Schmidt 	struct btrfs_data_container *fspath;
1996a542ad1bSJan Schmidt 
1997a542ad1bSJan Schmidt 	fspath = init_data_container(total_bytes);
1998a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
1999a542ad1bSJan Schmidt 		return (void *)fspath;
2000a542ad1bSJan Schmidt 
2001a542ad1bSJan Schmidt 	ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
2002a542ad1bSJan Schmidt 	if (!ifp) {
2003a542ad1bSJan Schmidt 		kfree(fspath);
2004a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2005a542ad1bSJan Schmidt 	}
2006a542ad1bSJan Schmidt 
2007a542ad1bSJan Schmidt 	ifp->btrfs_path = path;
2008a542ad1bSJan Schmidt 	ifp->fspath = fspath;
2009a542ad1bSJan Schmidt 	ifp->fs_root = fs_root;
2010a542ad1bSJan Schmidt 
2011a542ad1bSJan Schmidt 	return ifp;
2012a542ad1bSJan Schmidt }
2013a542ad1bSJan Schmidt 
2014a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath)
2015a542ad1bSJan Schmidt {
20164735fb28SJesper Juhl 	if (!ipath)
20174735fb28SJesper Juhl 		return;
2018425d17a2SLiu Bo 	vfree(ipath->fspath);
2019a542ad1bSJan Schmidt 	kfree(ipath);
2020a542ad1bSJan Schmidt }
2021