xref: /openbmc/linux/fs/btrfs/backref.c (revision dc046b10c8b7d4f40befe457acb82340bf8b0699)
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 
28*dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */
29*dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6
30*dc046b10SJosef 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;
2098da6d581SJan Schmidt 	if (key)
210d5c88b73SJan Schmidt 		ref->key_for_search = *key;
2118da6d581SJan Schmidt 	else
212d5c88b73SJan Schmidt 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
2138da6d581SJan Schmidt 
2143301958bSJan Schmidt 	ref->inode_list = NULL;
2158da6d581SJan Schmidt 	ref->level = level;
2168da6d581SJan Schmidt 	ref->count = count;
2178da6d581SJan Schmidt 	ref->parent = parent;
2188da6d581SJan Schmidt 	ref->wanted_disk_byte = wanted_disk_byte;
2198da6d581SJan Schmidt 	list_add_tail(&ref->list, head);
2208da6d581SJan Schmidt 
2218da6d581SJan Schmidt 	return 0;
2228da6d581SJan Schmidt }
2238da6d581SJan Schmidt 
2248da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
2257ef81ac8SJosef Bacik 			   struct ulist *parents, struct __prelim_ref *ref,
22644853868SJosef Bacik 			   int level, u64 time_seq, const u64 *extent_item_pos,
22744853868SJosef Bacik 			   u64 total_refs)
2288da6d581SJan Schmidt {
22969bca40dSAlexander Block 	int ret = 0;
23069bca40dSAlexander Block 	int slot;
23169bca40dSAlexander Block 	struct extent_buffer *eb;
23269bca40dSAlexander Block 	struct btrfs_key key;
2337ef81ac8SJosef Bacik 	struct btrfs_key *key_for_search = &ref->key_for_search;
2348da6d581SJan Schmidt 	struct btrfs_file_extent_item *fi;
235ed8c4913SJosef Bacik 	struct extent_inode_elem *eie = NULL, *old = NULL;
2368da6d581SJan Schmidt 	u64 disk_byte;
2377ef81ac8SJosef Bacik 	u64 wanted_disk_byte = ref->wanted_disk_byte;
2387ef81ac8SJosef Bacik 	u64 count = 0;
2398da6d581SJan Schmidt 
24069bca40dSAlexander Block 	if (level != 0) {
24169bca40dSAlexander Block 		eb = path->nodes[level];
24269bca40dSAlexander Block 		ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
2433301958bSJan Schmidt 		if (ret < 0)
2443301958bSJan Schmidt 			return ret;
2458da6d581SJan Schmidt 		return 0;
24669bca40dSAlexander Block 	}
2478da6d581SJan Schmidt 
2488da6d581SJan Schmidt 	/*
24969bca40dSAlexander Block 	 * We normally enter this function with the path already pointing to
25069bca40dSAlexander Block 	 * the first item to check. But sometimes, we may enter it with
25169bca40dSAlexander Block 	 * slot==nritems. In that case, go to the next leaf before we continue.
2528da6d581SJan Schmidt 	 */
25369bca40dSAlexander Block 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0]))
2543d7806ecSJan Schmidt 		ret = btrfs_next_old_leaf(root, path, time_seq);
2558da6d581SJan Schmidt 
25644853868SJosef Bacik 	while (!ret && count < total_refs) {
2578da6d581SJan Schmidt 		eb = path->nodes[0];
25869bca40dSAlexander Block 		slot = path->slots[0];
25969bca40dSAlexander Block 
26069bca40dSAlexander Block 		btrfs_item_key_to_cpu(eb, &key, slot);
26169bca40dSAlexander Block 
26269bca40dSAlexander Block 		if (key.objectid != key_for_search->objectid ||
26369bca40dSAlexander Block 		    key.type != BTRFS_EXTENT_DATA_KEY)
26469bca40dSAlexander Block 			break;
26569bca40dSAlexander Block 
26669bca40dSAlexander Block 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
2678da6d581SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
26869bca40dSAlexander Block 
26969bca40dSAlexander Block 		if (disk_byte == wanted_disk_byte) {
27069bca40dSAlexander Block 			eie = NULL;
271ed8c4913SJosef Bacik 			old = NULL;
2727ef81ac8SJosef Bacik 			count++;
27369bca40dSAlexander Block 			if (extent_item_pos) {
27469bca40dSAlexander Block 				ret = check_extent_in_eb(&key, eb, fi,
27569bca40dSAlexander Block 						*extent_item_pos,
27669bca40dSAlexander Block 						&eie);
27769bca40dSAlexander Block 				if (ret < 0)
27869bca40dSAlexander Block 					break;
2798da6d581SJan Schmidt 			}
280ed8c4913SJosef Bacik 			if (ret > 0)
281ed8c4913SJosef Bacik 				goto next;
2824eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(parents, eb->start,
2834eb1f66dSTakashi Iwai 						  eie, (void **)&old, GFP_NOFS);
28469bca40dSAlexander Block 			if (ret < 0)
28569bca40dSAlexander Block 				break;
286ed8c4913SJosef Bacik 			if (!ret && extent_item_pos) {
287ed8c4913SJosef Bacik 				while (old->next)
288ed8c4913SJosef Bacik 					old = old->next;
289ed8c4913SJosef Bacik 				old->next = eie;
29069bca40dSAlexander Block 			}
291f05c4746SWang Shilong 			eie = NULL;
29269bca40dSAlexander Block 		}
293ed8c4913SJosef Bacik next:
29469bca40dSAlexander Block 		ret = btrfs_next_old_item(root, path, time_seq);
2958da6d581SJan Schmidt 	}
2968da6d581SJan Schmidt 
29769bca40dSAlexander Block 	if (ret > 0)
29869bca40dSAlexander Block 		ret = 0;
299f05c4746SWang Shilong 	else if (ret < 0)
300f05c4746SWang Shilong 		free_inode_elem_list(eie);
30169bca40dSAlexander Block 	return ret;
3028da6d581SJan Schmidt }
3038da6d581SJan Schmidt 
3048da6d581SJan Schmidt /*
3058da6d581SJan Schmidt  * resolve an indirect backref in the form (root_id, key, level)
3068da6d581SJan Schmidt  * to a logical address
3078da6d581SJan Schmidt  */
3088da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
309da61d31aSJosef Bacik 				  struct btrfs_path *path, u64 time_seq,
3108da6d581SJan Schmidt 				  struct __prelim_ref *ref,
311976b1908SJan Schmidt 				  struct ulist *parents,
31244853868SJosef Bacik 				  const u64 *extent_item_pos, u64 total_refs)
3138da6d581SJan Schmidt {
3148da6d581SJan Schmidt 	struct btrfs_root *root;
3158da6d581SJan Schmidt 	struct btrfs_key root_key;
3168da6d581SJan Schmidt 	struct extent_buffer *eb;
3178da6d581SJan Schmidt 	int ret = 0;
3188da6d581SJan Schmidt 	int root_level;
3198da6d581SJan Schmidt 	int level = ref->level;
320538f72cdSWang Shilong 	int index;
3218da6d581SJan Schmidt 
3228da6d581SJan Schmidt 	root_key.objectid = ref->root_id;
3238da6d581SJan Schmidt 	root_key.type = BTRFS_ROOT_ITEM_KEY;
3248da6d581SJan Schmidt 	root_key.offset = (u64)-1;
325538f72cdSWang Shilong 
326538f72cdSWang Shilong 	index = srcu_read_lock(&fs_info->subvol_srcu);
327538f72cdSWang Shilong 
3288da6d581SJan Schmidt 	root = btrfs_read_fs_root_no_name(fs_info, &root_key);
3298da6d581SJan Schmidt 	if (IS_ERR(root)) {
330538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
3318da6d581SJan Schmidt 		ret = PTR_ERR(root);
3328da6d581SJan Schmidt 		goto out;
3338da6d581SJan Schmidt 	}
3348da6d581SJan Schmidt 
3359e351cc8SJosef Bacik 	if (path->search_commit_root)
3369e351cc8SJosef Bacik 		root_level = btrfs_header_level(root->commit_root);
3379e351cc8SJosef Bacik 	else
3385b6602e7SJan Schmidt 		root_level = btrfs_old_root_level(root, time_seq);
3398da6d581SJan Schmidt 
340538f72cdSWang Shilong 	if (root_level + 1 == level) {
341538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
3428da6d581SJan Schmidt 		goto out;
343538f72cdSWang Shilong 	}
3448da6d581SJan Schmidt 
3458da6d581SJan Schmidt 	path->lowest_level = level;
3468445f61cSJan Schmidt 	ret = btrfs_search_old_slot(root, &ref->key_for_search, path, time_seq);
347538f72cdSWang Shilong 
348538f72cdSWang Shilong 	/* root node has been locked, we can release @subvol_srcu safely here */
349538f72cdSWang Shilong 	srcu_read_unlock(&fs_info->subvol_srcu, index);
350538f72cdSWang Shilong 
3518da6d581SJan Schmidt 	pr_debug("search slot in root %llu (level %d, ref count %d) returned "
3528da6d581SJan Schmidt 		 "%d for key (%llu %u %llu)\n",
353c1c9ff7cSGeert Uytterhoeven 		 ref->root_id, level, ref->count, ret,
354c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.objectid, ref->key_for_search.type,
355c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.offset);
3568da6d581SJan Schmidt 	if (ret < 0)
3578da6d581SJan Schmidt 		goto out;
3588da6d581SJan Schmidt 
3598da6d581SJan Schmidt 	eb = path->nodes[level];
3609345457fSJan Schmidt 	while (!eb) {
361fae7f21cSDulshani Gunawardhana 		if (WARN_ON(!level)) {
3628da6d581SJan Schmidt 			ret = 1;
3638da6d581SJan Schmidt 			goto out;
3648da6d581SJan Schmidt 		}
3659345457fSJan Schmidt 		level--;
3669345457fSJan Schmidt 		eb = path->nodes[level];
3679345457fSJan Schmidt 	}
3688da6d581SJan Schmidt 
3697ef81ac8SJosef Bacik 	ret = add_all_parents(root, path, parents, ref, level, time_seq,
37044853868SJosef Bacik 			      extent_item_pos, total_refs);
3718da6d581SJan Schmidt out:
372da61d31aSJosef Bacik 	path->lowest_level = 0;
373da61d31aSJosef Bacik 	btrfs_release_path(path);
3748da6d581SJan Schmidt 	return ret;
3758da6d581SJan Schmidt }
3768da6d581SJan Schmidt 
3778da6d581SJan Schmidt /*
3788da6d581SJan Schmidt  * resolve all indirect backrefs from the list
3798da6d581SJan Schmidt  */
3808da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
381da61d31aSJosef Bacik 				   struct btrfs_path *path, u64 time_seq,
382976b1908SJan Schmidt 				   struct list_head *head,
383*dc046b10SJosef Bacik 				   const u64 *extent_item_pos, u64 total_refs,
384*dc046b10SJosef Bacik 				   u64 root_objectid)
3858da6d581SJan Schmidt {
3868da6d581SJan Schmidt 	int err;
3878da6d581SJan Schmidt 	int ret = 0;
3888da6d581SJan Schmidt 	struct __prelim_ref *ref;
3898da6d581SJan Schmidt 	struct __prelim_ref *ref_safe;
3908da6d581SJan Schmidt 	struct __prelim_ref *new_ref;
3918da6d581SJan Schmidt 	struct ulist *parents;
3928da6d581SJan Schmidt 	struct ulist_node *node;
393cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
3948da6d581SJan Schmidt 
3958da6d581SJan Schmidt 	parents = ulist_alloc(GFP_NOFS);
3968da6d581SJan Schmidt 	if (!parents)
3978da6d581SJan Schmidt 		return -ENOMEM;
3988da6d581SJan Schmidt 
3998da6d581SJan Schmidt 	/*
4008da6d581SJan Schmidt 	 * _safe allows us to insert directly after the current item without
4018da6d581SJan Schmidt 	 * iterating over the newly inserted items.
4028da6d581SJan Schmidt 	 * we're also allowed to re-assign ref during iteration.
4038da6d581SJan Schmidt 	 */
4048da6d581SJan Schmidt 	list_for_each_entry_safe(ref, ref_safe, head, list) {
4058da6d581SJan Schmidt 		if (ref->parent)	/* already direct */
4068da6d581SJan Schmidt 			continue;
4078da6d581SJan Schmidt 		if (ref->count == 0)
4088da6d581SJan Schmidt 			continue;
409*dc046b10SJosef Bacik 		if (root_objectid && ref->root_id != root_objectid) {
410*dc046b10SJosef Bacik 			ret = BACKREF_FOUND_SHARED;
411*dc046b10SJosef Bacik 			goto out;
412*dc046b10SJosef Bacik 		}
413da61d31aSJosef Bacik 		err = __resolve_indirect_ref(fs_info, path, time_seq, ref,
41444853868SJosef Bacik 					     parents, extent_item_pos,
41544853868SJosef Bacik 					     total_refs);
41695def2edSWang Shilong 		/*
41795def2edSWang Shilong 		 * we can only tolerate ENOENT,otherwise,we should catch error
41895def2edSWang Shilong 		 * and return directly.
41995def2edSWang Shilong 		 */
42095def2edSWang Shilong 		if (err == -ENOENT) {
4218da6d581SJan Schmidt 			continue;
42295def2edSWang Shilong 		} else if (err) {
42395def2edSWang Shilong 			ret = err;
42495def2edSWang Shilong 			goto out;
42595def2edSWang Shilong 		}
4268da6d581SJan Schmidt 
4278da6d581SJan Schmidt 		/* we put the first parent into the ref at hand */
428cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&uiter);
429cd1b413cSJan Schmidt 		node = ulist_next(parents, &uiter);
4308da6d581SJan Schmidt 		ref->parent = node ? node->val : 0;
431995e01b7SJan Schmidt 		ref->inode_list = node ?
43235a3621bSStefan Behrens 			(struct extent_inode_elem *)(uintptr_t)node->aux : NULL;
4338da6d581SJan Schmidt 
4348da6d581SJan Schmidt 		/* additional parents require new refs being added here */
435cd1b413cSJan Schmidt 		while ((node = ulist_next(parents, &uiter))) {
436b9e9a6cbSWang Shilong 			new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
437b9e9a6cbSWang Shilong 						   GFP_NOFS);
4388da6d581SJan Schmidt 			if (!new_ref) {
4398da6d581SJan Schmidt 				ret = -ENOMEM;
440e36902d4SWang Shilong 				goto out;
4418da6d581SJan Schmidt 			}
4428da6d581SJan Schmidt 			memcpy(new_ref, ref, sizeof(*ref));
4438da6d581SJan Schmidt 			new_ref->parent = node->val;
444995e01b7SJan Schmidt 			new_ref->inode_list = (struct extent_inode_elem *)
445995e01b7SJan Schmidt 							(uintptr_t)node->aux;
4468da6d581SJan Schmidt 			list_add(&new_ref->list, &ref->list);
4478da6d581SJan Schmidt 		}
4488da6d581SJan Schmidt 		ulist_reinit(parents);
4498da6d581SJan Schmidt 	}
450e36902d4SWang Shilong out:
4518da6d581SJan Schmidt 	ulist_free(parents);
4528da6d581SJan Schmidt 	return ret;
4538da6d581SJan Schmidt }
4548da6d581SJan Schmidt 
455d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1,
456d5c88b73SJan Schmidt 				     struct __prelim_ref *ref2)
457d5c88b73SJan Schmidt {
458d5c88b73SJan Schmidt 	if (ref1->level != ref2->level)
459d5c88b73SJan Schmidt 		return 0;
460d5c88b73SJan Schmidt 	if (ref1->root_id != ref2->root_id)
461d5c88b73SJan Schmidt 		return 0;
462d5c88b73SJan Schmidt 	if (ref1->key_for_search.type != ref2->key_for_search.type)
463d5c88b73SJan Schmidt 		return 0;
464d5c88b73SJan Schmidt 	if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
465d5c88b73SJan Schmidt 		return 0;
466d5c88b73SJan Schmidt 	if (ref1->key_for_search.offset != ref2->key_for_search.offset)
467d5c88b73SJan Schmidt 		return 0;
468d5c88b73SJan Schmidt 	if (ref1->parent != ref2->parent)
469d5c88b73SJan Schmidt 		return 0;
470d5c88b73SJan Schmidt 
471d5c88b73SJan Schmidt 	return 1;
472d5c88b73SJan Schmidt }
473d5c88b73SJan Schmidt 
474d5c88b73SJan Schmidt /*
475d5c88b73SJan Schmidt  * read tree blocks and add keys where required.
476d5c88b73SJan Schmidt  */
477d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info,
478d5c88b73SJan Schmidt 			      struct list_head *head)
479d5c88b73SJan Schmidt {
480d5c88b73SJan Schmidt 	struct list_head *pos;
481d5c88b73SJan Schmidt 	struct extent_buffer *eb;
482d5c88b73SJan Schmidt 
483d5c88b73SJan Schmidt 	list_for_each(pos, head) {
484d5c88b73SJan Schmidt 		struct __prelim_ref *ref;
485d5c88b73SJan Schmidt 		ref = list_entry(pos, struct __prelim_ref, list);
486d5c88b73SJan Schmidt 
487d5c88b73SJan Schmidt 		if (ref->parent)
488d5c88b73SJan Schmidt 			continue;
489d5c88b73SJan Schmidt 		if (ref->key_for_search.type)
490d5c88b73SJan Schmidt 			continue;
491d5c88b73SJan Schmidt 		BUG_ON(!ref->wanted_disk_byte);
492d5c88b73SJan Schmidt 		eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
493707e8a07SDavid Sterba 				     fs_info->tree_root->nodesize, 0);
494416bc658SJosef Bacik 		if (!eb || !extent_buffer_uptodate(eb)) {
495416bc658SJosef Bacik 			free_extent_buffer(eb);
496416bc658SJosef Bacik 			return -EIO;
497416bc658SJosef Bacik 		}
498d5c88b73SJan Schmidt 		btrfs_tree_read_lock(eb);
499d5c88b73SJan Schmidt 		if (btrfs_header_level(eb) == 0)
500d5c88b73SJan Schmidt 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
501d5c88b73SJan Schmidt 		else
502d5c88b73SJan Schmidt 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
503d5c88b73SJan Schmidt 		btrfs_tree_read_unlock(eb);
504d5c88b73SJan Schmidt 		free_extent_buffer(eb);
505d5c88b73SJan Schmidt 	}
506d5c88b73SJan Schmidt 	return 0;
507d5c88b73SJan Schmidt }
508d5c88b73SJan Schmidt 
5098da6d581SJan Schmidt /*
5108da6d581SJan Schmidt  * merge two lists of backrefs and adjust counts accordingly
5118da6d581SJan Schmidt  *
5128da6d581SJan Schmidt  * mode = 1: merge identical keys, if key is set
513d5c88b73SJan Schmidt  *    FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
514d5c88b73SJan Schmidt  *           additionally, we could even add a key range for the blocks we
515d5c88b73SJan Schmidt  *           looked into to merge even more (-> replace unresolved refs by those
516d5c88b73SJan Schmidt  *           having a parent).
5178da6d581SJan Schmidt  * mode = 2: merge identical parents
5188da6d581SJan Schmidt  */
519692206b1SWang Shilong static void __merge_refs(struct list_head *head, int mode)
5208da6d581SJan Schmidt {
5218da6d581SJan Schmidt 	struct list_head *pos1;
5228da6d581SJan Schmidt 
5238da6d581SJan Schmidt 	list_for_each(pos1, head) {
5248da6d581SJan Schmidt 		struct list_head *n2;
5258da6d581SJan Schmidt 		struct list_head *pos2;
5268da6d581SJan Schmidt 		struct __prelim_ref *ref1;
5278da6d581SJan Schmidt 
5288da6d581SJan Schmidt 		ref1 = list_entry(pos1, struct __prelim_ref, list);
5298da6d581SJan Schmidt 
5308da6d581SJan Schmidt 		for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
5318da6d581SJan Schmidt 		     pos2 = n2, n2 = pos2->next) {
5328da6d581SJan Schmidt 			struct __prelim_ref *ref2;
533d5c88b73SJan Schmidt 			struct __prelim_ref *xchg;
5343ef5969cSAlexander Block 			struct extent_inode_elem *eie;
5358da6d581SJan Schmidt 
5368da6d581SJan Schmidt 			ref2 = list_entry(pos2, struct __prelim_ref, list);
5378da6d581SJan Schmidt 
5388da6d581SJan Schmidt 			if (mode == 1) {
539d5c88b73SJan Schmidt 				if (!ref_for_same_block(ref1, ref2))
5408da6d581SJan Schmidt 					continue;
541d5c88b73SJan Schmidt 				if (!ref1->parent && ref2->parent) {
542d5c88b73SJan Schmidt 					xchg = ref1;
543d5c88b73SJan Schmidt 					ref1 = ref2;
544d5c88b73SJan Schmidt 					ref2 = xchg;
545d5c88b73SJan Schmidt 				}
5468da6d581SJan Schmidt 			} else {
5478da6d581SJan Schmidt 				if (ref1->parent != ref2->parent)
5488da6d581SJan Schmidt 					continue;
5498da6d581SJan Schmidt 			}
5503ef5969cSAlexander Block 
5513ef5969cSAlexander Block 			eie = ref1->inode_list;
5523ef5969cSAlexander Block 			while (eie && eie->next)
5533ef5969cSAlexander Block 				eie = eie->next;
5543ef5969cSAlexander Block 			if (eie)
5553ef5969cSAlexander Block 				eie->next = ref2->inode_list;
5563ef5969cSAlexander Block 			else
5573ef5969cSAlexander Block 				ref1->inode_list = ref2->inode_list;
5583ef5969cSAlexander Block 			ref1->count += ref2->count;
5593ef5969cSAlexander Block 
5608da6d581SJan Schmidt 			list_del(&ref2->list);
561b9e9a6cbSWang Shilong 			kmem_cache_free(btrfs_prelim_ref_cache, ref2);
5628da6d581SJan Schmidt 		}
5638da6d581SJan Schmidt 
5648da6d581SJan Schmidt 	}
5658da6d581SJan Schmidt }
5668da6d581SJan Schmidt 
5678da6d581SJan Schmidt /*
5688da6d581SJan Schmidt  * add all currently queued delayed refs from this head whose seq nr is
5698da6d581SJan Schmidt  * smaller or equal that seq to the list
5708da6d581SJan Schmidt  */
5718da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
572*dc046b10SJosef Bacik 			      struct list_head *prefs, u64 *total_refs,
573*dc046b10SJosef Bacik 			      u64 inum)
5748da6d581SJan Schmidt {
5758da6d581SJan Schmidt 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
5768da6d581SJan Schmidt 	struct rb_node *n = &head->node.rb_node;
577d5c88b73SJan Schmidt 	struct btrfs_key key;
578d5c88b73SJan Schmidt 	struct btrfs_key op_key = {0};
5798da6d581SJan Schmidt 	int sgn;
580b1375d64SJan Schmidt 	int ret = 0;
5818da6d581SJan Schmidt 
5828da6d581SJan Schmidt 	if (extent_op && extent_op->update_key)
583d5c88b73SJan Schmidt 		btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
5848da6d581SJan Schmidt 
585d7df2c79SJosef Bacik 	spin_lock(&head->lock);
586d7df2c79SJosef Bacik 	n = rb_first(&head->ref_root);
587d7df2c79SJosef Bacik 	while (n) {
5888da6d581SJan Schmidt 		struct btrfs_delayed_ref_node *node;
5898da6d581SJan Schmidt 		node = rb_entry(n, struct btrfs_delayed_ref_node,
5908da6d581SJan Schmidt 				rb_node);
591d7df2c79SJosef Bacik 		n = rb_next(n);
5928da6d581SJan Schmidt 		if (node->seq > seq)
5938da6d581SJan Schmidt 			continue;
5948da6d581SJan Schmidt 
5958da6d581SJan Schmidt 		switch (node->action) {
5968da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_EXTENT:
5978da6d581SJan Schmidt 		case BTRFS_UPDATE_DELAYED_HEAD:
5988da6d581SJan Schmidt 			WARN_ON(1);
5998da6d581SJan Schmidt 			continue;
6008da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_REF:
6018da6d581SJan Schmidt 			sgn = 1;
6028da6d581SJan Schmidt 			break;
6038da6d581SJan Schmidt 		case BTRFS_DROP_DELAYED_REF:
6048da6d581SJan Schmidt 			sgn = -1;
6058da6d581SJan Schmidt 			break;
6068da6d581SJan Schmidt 		default:
6078da6d581SJan Schmidt 			BUG_ON(1);
6088da6d581SJan Schmidt 		}
60944853868SJosef Bacik 		*total_refs += (node->ref_mod * sgn);
6108da6d581SJan Schmidt 		switch (node->type) {
6118da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY: {
6128da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
6138da6d581SJan Schmidt 
6148da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
615d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &op_key,
6168da6d581SJan Schmidt 					       ref->level + 1, 0, node->bytenr,
617742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6188da6d581SJan Schmidt 			break;
6198da6d581SJan Schmidt 		}
6208da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY: {
6218da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
6228da6d581SJan Schmidt 
6238da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
624d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, NULL,
6258da6d581SJan Schmidt 					       ref->level + 1, ref->parent,
6268da6d581SJan Schmidt 					       node->bytenr,
627742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6288da6d581SJan Schmidt 			break;
6298da6d581SJan Schmidt 		}
6308da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
6318da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
6328da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
6338da6d581SJan Schmidt 
6348da6d581SJan Schmidt 			key.objectid = ref->objectid;
6358da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
6368da6d581SJan Schmidt 			key.offset = ref->offset;
637*dc046b10SJosef Bacik 
638*dc046b10SJosef Bacik 			/*
639*dc046b10SJosef Bacik 			 * Found a inum that doesn't match our known inum, we
640*dc046b10SJosef Bacik 			 * know it's shared.
641*dc046b10SJosef Bacik 			 */
642*dc046b10SJosef Bacik 			if (inum && ref->objectid != inum) {
643*dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
644*dc046b10SJosef Bacik 				break;
645*dc046b10SJosef Bacik 			}
646*dc046b10SJosef Bacik 
6478da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
6488da6d581SJan Schmidt 					       node->bytenr,
649742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6508da6d581SJan Schmidt 			break;
6518da6d581SJan Schmidt 		}
6528da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
6538da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
6548da6d581SJan Schmidt 
6558da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
6568da6d581SJan Schmidt 
6578da6d581SJan Schmidt 			key.objectid = ref->objectid;
6588da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
6598da6d581SJan Schmidt 			key.offset = ref->offset;
6608da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &key, 0,
6618da6d581SJan Schmidt 					       ref->parent, node->bytenr,
662742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
6638da6d581SJan Schmidt 			break;
6648da6d581SJan Schmidt 		}
6658da6d581SJan Schmidt 		default:
6668da6d581SJan Schmidt 			WARN_ON(1);
6678da6d581SJan Schmidt 		}
6681149ab6bSWang Shilong 		if (ret)
669d7df2c79SJosef Bacik 			break;
6708da6d581SJan Schmidt 	}
671d7df2c79SJosef Bacik 	spin_unlock(&head->lock);
672d7df2c79SJosef Bacik 	return ret;
6738da6d581SJan Schmidt }
6748da6d581SJan Schmidt 
6758da6d581SJan Schmidt /*
6768da6d581SJan Schmidt  * add all inline backrefs for bytenr to the list
6778da6d581SJan Schmidt  */
6788da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info,
6798da6d581SJan Schmidt 			     struct btrfs_path *path, u64 bytenr,
68044853868SJosef Bacik 			     int *info_level, struct list_head *prefs,
681*dc046b10SJosef Bacik 			     u64 *total_refs, u64 inum)
6828da6d581SJan Schmidt {
683b1375d64SJan Schmidt 	int ret = 0;
6848da6d581SJan Schmidt 	int slot;
6858da6d581SJan Schmidt 	struct extent_buffer *leaf;
6868da6d581SJan Schmidt 	struct btrfs_key key;
687261c84b6SJosef Bacik 	struct btrfs_key found_key;
6888da6d581SJan Schmidt 	unsigned long ptr;
6898da6d581SJan Schmidt 	unsigned long end;
6908da6d581SJan Schmidt 	struct btrfs_extent_item *ei;
6918da6d581SJan Schmidt 	u64 flags;
6928da6d581SJan Schmidt 	u64 item_size;
6938da6d581SJan Schmidt 
6948da6d581SJan Schmidt 	/*
6958da6d581SJan Schmidt 	 * enumerate all inline refs
6968da6d581SJan Schmidt 	 */
6978da6d581SJan Schmidt 	leaf = path->nodes[0];
698dadcaf78SJan Schmidt 	slot = path->slots[0];
6998da6d581SJan Schmidt 
7008da6d581SJan Schmidt 	item_size = btrfs_item_size_nr(leaf, slot);
7018da6d581SJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
7028da6d581SJan Schmidt 
7038da6d581SJan Schmidt 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
7048da6d581SJan Schmidt 	flags = btrfs_extent_flags(leaf, ei);
70544853868SJosef Bacik 	*total_refs += btrfs_extent_refs(leaf, ei);
706261c84b6SJosef Bacik 	btrfs_item_key_to_cpu(leaf, &found_key, slot);
7078da6d581SJan Schmidt 
7088da6d581SJan Schmidt 	ptr = (unsigned long)(ei + 1);
7098da6d581SJan Schmidt 	end = (unsigned long)ei + item_size;
7108da6d581SJan Schmidt 
711261c84b6SJosef Bacik 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
712261c84b6SJosef Bacik 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
7138da6d581SJan Schmidt 		struct btrfs_tree_block_info *info;
7148da6d581SJan Schmidt 
7158da6d581SJan Schmidt 		info = (struct btrfs_tree_block_info *)ptr;
7168da6d581SJan Schmidt 		*info_level = btrfs_tree_block_level(leaf, info);
7178da6d581SJan Schmidt 		ptr += sizeof(struct btrfs_tree_block_info);
7188da6d581SJan Schmidt 		BUG_ON(ptr > end);
719261c84b6SJosef Bacik 	} else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
720261c84b6SJosef Bacik 		*info_level = found_key.offset;
7218da6d581SJan Schmidt 	} else {
7228da6d581SJan Schmidt 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
7238da6d581SJan Schmidt 	}
7248da6d581SJan Schmidt 
7258da6d581SJan Schmidt 	while (ptr < end) {
7268da6d581SJan Schmidt 		struct btrfs_extent_inline_ref *iref;
7278da6d581SJan Schmidt 		u64 offset;
7288da6d581SJan Schmidt 		int type;
7298da6d581SJan Schmidt 
7308da6d581SJan Schmidt 		iref = (struct btrfs_extent_inline_ref *)ptr;
7318da6d581SJan Schmidt 		type = btrfs_extent_inline_ref_type(leaf, iref);
7328da6d581SJan Schmidt 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
7338da6d581SJan Schmidt 
7348da6d581SJan Schmidt 		switch (type) {
7358da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
736d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL,
7378da6d581SJan Schmidt 						*info_level + 1, offset,
738742916b8SWang Shilong 						bytenr, 1, GFP_NOFS);
7398da6d581SJan Schmidt 			break;
7408da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
7418da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
7428da6d581SJan Schmidt 			int count;
7438da6d581SJan Schmidt 
7448da6d581SJan Schmidt 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
7458da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
7468da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
747742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
7488da6d581SJan Schmidt 			break;
7498da6d581SJan Schmidt 		}
7508da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
751d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, offset, NULL,
752d5c88b73SJan Schmidt 					       *info_level + 1, 0,
753742916b8SWang Shilong 					       bytenr, 1, GFP_NOFS);
7548da6d581SJan Schmidt 			break;
7558da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
7568da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
7578da6d581SJan Schmidt 			int count;
7588da6d581SJan Schmidt 			u64 root;
7598da6d581SJan Schmidt 
7608da6d581SJan Schmidt 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
7618da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
7628da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
7638da6d581SJan Schmidt 								      dref);
7648da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
7658da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
766*dc046b10SJosef Bacik 
767*dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
768*dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
769*dc046b10SJosef Bacik 				break;
770*dc046b10SJosef Bacik 			}
771*dc046b10SJosef Bacik 
7728da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
773d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
774742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
7758da6d581SJan Schmidt 			break;
7768da6d581SJan Schmidt 		}
7778da6d581SJan Schmidt 		default:
7788da6d581SJan Schmidt 			WARN_ON(1);
7798da6d581SJan Schmidt 		}
7801149ab6bSWang Shilong 		if (ret)
7811149ab6bSWang Shilong 			return ret;
7828da6d581SJan Schmidt 		ptr += btrfs_extent_inline_ref_size(type);
7838da6d581SJan Schmidt 	}
7848da6d581SJan Schmidt 
7858da6d581SJan Schmidt 	return 0;
7868da6d581SJan Schmidt }
7878da6d581SJan Schmidt 
7888da6d581SJan Schmidt /*
7898da6d581SJan Schmidt  * add all non-inline backrefs for bytenr to the list
7908da6d581SJan Schmidt  */
7918da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
7928da6d581SJan Schmidt 			    struct btrfs_path *path, u64 bytenr,
793*dc046b10SJosef Bacik 			    int info_level, struct list_head *prefs, u64 inum)
7948da6d581SJan Schmidt {
7958da6d581SJan Schmidt 	struct btrfs_root *extent_root = fs_info->extent_root;
7968da6d581SJan Schmidt 	int ret;
7978da6d581SJan Schmidt 	int slot;
7988da6d581SJan Schmidt 	struct extent_buffer *leaf;
7998da6d581SJan Schmidt 	struct btrfs_key key;
8008da6d581SJan Schmidt 
8018da6d581SJan Schmidt 	while (1) {
8028da6d581SJan Schmidt 		ret = btrfs_next_item(extent_root, path);
8038da6d581SJan Schmidt 		if (ret < 0)
8048da6d581SJan Schmidt 			break;
8058da6d581SJan Schmidt 		if (ret) {
8068da6d581SJan Schmidt 			ret = 0;
8078da6d581SJan Schmidt 			break;
8088da6d581SJan Schmidt 		}
8098da6d581SJan Schmidt 
8108da6d581SJan Schmidt 		slot = path->slots[0];
8118da6d581SJan Schmidt 		leaf = path->nodes[0];
8128da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
8138da6d581SJan Schmidt 
8148da6d581SJan Schmidt 		if (key.objectid != bytenr)
8158da6d581SJan Schmidt 			break;
8168da6d581SJan Schmidt 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
8178da6d581SJan Schmidt 			continue;
8188da6d581SJan Schmidt 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
8198da6d581SJan Schmidt 			break;
8208da6d581SJan Schmidt 
8218da6d581SJan Schmidt 		switch (key.type) {
8228da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
823d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL,
8248da6d581SJan Schmidt 						info_level + 1, key.offset,
825742916b8SWang Shilong 						bytenr, 1, GFP_NOFS);
8268da6d581SJan Schmidt 			break;
8278da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
8288da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
8298da6d581SJan Schmidt 			int count;
8308da6d581SJan Schmidt 
8318da6d581SJan Schmidt 			sdref = btrfs_item_ptr(leaf, slot,
8328da6d581SJan Schmidt 					      struct btrfs_shared_data_ref);
8338da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
8348da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
835742916b8SWang Shilong 						bytenr, count, GFP_NOFS);
8368da6d581SJan Schmidt 			break;
8378da6d581SJan Schmidt 		}
8388da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
839d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, key.offset, NULL,
840d5c88b73SJan Schmidt 					       info_level + 1, 0,
841742916b8SWang Shilong 					       bytenr, 1, GFP_NOFS);
8428da6d581SJan Schmidt 			break;
8438da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
8448da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
8458da6d581SJan Schmidt 			int count;
8468da6d581SJan Schmidt 			u64 root;
8478da6d581SJan Schmidt 
8488da6d581SJan Schmidt 			dref = btrfs_item_ptr(leaf, slot,
8498da6d581SJan Schmidt 					      struct btrfs_extent_data_ref);
8508da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
8518da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
8528da6d581SJan Schmidt 								      dref);
8538da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
8548da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
855*dc046b10SJosef Bacik 
856*dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
857*dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
858*dc046b10SJosef Bacik 				break;
859*dc046b10SJosef Bacik 			}
860*dc046b10SJosef Bacik 
8618da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
8628da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
863742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
8648da6d581SJan Schmidt 			break;
8658da6d581SJan Schmidt 		}
8668da6d581SJan Schmidt 		default:
8678da6d581SJan Schmidt 			WARN_ON(1);
8688da6d581SJan Schmidt 		}
8691149ab6bSWang Shilong 		if (ret)
8701149ab6bSWang Shilong 			return ret;
8711149ab6bSWang Shilong 
8728da6d581SJan Schmidt 	}
8738da6d581SJan Schmidt 
8748da6d581SJan Schmidt 	return ret;
8758da6d581SJan Schmidt }
8768da6d581SJan Schmidt 
8778da6d581SJan Schmidt /*
8788da6d581SJan Schmidt  * this adds all existing backrefs (inline backrefs, backrefs and delayed
8798da6d581SJan Schmidt  * refs) for the given bytenr to the refs list, merges duplicates and resolves
8808da6d581SJan Schmidt  * indirect refs to their parent bytenr.
8818da6d581SJan Schmidt  * When roots are found, they're added to the roots list
8828da6d581SJan Schmidt  *
8838da6d581SJan Schmidt  * FIXME some caching might speed things up
8848da6d581SJan Schmidt  */
8858da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans,
8868da6d581SJan Schmidt 			     struct btrfs_fs_info *fs_info, u64 bytenr,
887097b8a7cSJan Schmidt 			     u64 time_seq, struct ulist *refs,
888*dc046b10SJosef Bacik 			     struct ulist *roots, const u64 *extent_item_pos,
889*dc046b10SJosef Bacik 			     u64 root_objectid, u64 inum)
8908da6d581SJan Schmidt {
8918da6d581SJan Schmidt 	struct btrfs_key key;
8928da6d581SJan Schmidt 	struct btrfs_path *path;
8938da6d581SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
894d3b01064SLi Zefan 	struct btrfs_delayed_ref_head *head;
8958da6d581SJan Schmidt 	int info_level = 0;
8968da6d581SJan Schmidt 	int ret;
8978da6d581SJan Schmidt 	struct list_head prefs_delayed;
8988da6d581SJan Schmidt 	struct list_head prefs;
8998da6d581SJan Schmidt 	struct __prelim_ref *ref;
900f05c4746SWang Shilong 	struct extent_inode_elem *eie = NULL;
90144853868SJosef Bacik 	u64 total_refs = 0;
9028da6d581SJan Schmidt 
9038da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs);
9048da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs_delayed);
9058da6d581SJan Schmidt 
9068da6d581SJan Schmidt 	key.objectid = bytenr;
9078da6d581SJan Schmidt 	key.offset = (u64)-1;
908261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
909261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
910261c84b6SJosef Bacik 	else
911261c84b6SJosef Bacik 		key.type = BTRFS_EXTENT_ITEM_KEY;
9128da6d581SJan Schmidt 
9138da6d581SJan Schmidt 	path = btrfs_alloc_path();
9148da6d581SJan Schmidt 	if (!path)
9158da6d581SJan Schmidt 		return -ENOMEM;
916e84752d4SWang Shilong 	if (!trans) {
917da61d31aSJosef Bacik 		path->search_commit_root = 1;
918e84752d4SWang Shilong 		path->skip_locking = 1;
919e84752d4SWang Shilong 	}
9208da6d581SJan Schmidt 
9218da6d581SJan Schmidt 	/*
9228da6d581SJan Schmidt 	 * grab both a lock on the path and a lock on the delayed ref head.
9238da6d581SJan Schmidt 	 * We need both to get a consistent picture of how the refs look
9248da6d581SJan Schmidt 	 * at a specified point in time
9258da6d581SJan Schmidt 	 */
9268da6d581SJan Schmidt again:
927d3b01064SLi Zefan 	head = NULL;
928d3b01064SLi Zefan 
9298da6d581SJan Schmidt 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
9308da6d581SJan Schmidt 	if (ret < 0)
9318da6d581SJan Schmidt 		goto out;
9328da6d581SJan Schmidt 	BUG_ON(ret == 0);
9338da6d581SJan Schmidt 
934faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
935faa2dbf0SJosef Bacik 	if (trans && likely(trans->type != __TRANS_DUMMY)) {
936faa2dbf0SJosef Bacik #else
937da61d31aSJosef Bacik 	if (trans) {
938faa2dbf0SJosef Bacik #endif
9398da6d581SJan Schmidt 		/*
9407a3ae2f8SJan Schmidt 		 * look if there are updates for this ref queued and lock the
9417a3ae2f8SJan Schmidt 		 * head
9428da6d581SJan Schmidt 		 */
9438da6d581SJan Schmidt 		delayed_refs = &trans->transaction->delayed_refs;
9448da6d581SJan Schmidt 		spin_lock(&delayed_refs->lock);
9458da6d581SJan Schmidt 		head = btrfs_find_delayed_ref_head(trans, bytenr);
9468da6d581SJan Schmidt 		if (head) {
9478da6d581SJan Schmidt 			if (!mutex_trylock(&head->mutex)) {
9488da6d581SJan Schmidt 				atomic_inc(&head->node.refs);
9498da6d581SJan Schmidt 				spin_unlock(&delayed_refs->lock);
9508da6d581SJan Schmidt 
9518da6d581SJan Schmidt 				btrfs_release_path(path);
9528da6d581SJan Schmidt 
9538da6d581SJan Schmidt 				/*
9548da6d581SJan Schmidt 				 * Mutex was contended, block until it's
9558da6d581SJan Schmidt 				 * released and try again
9568da6d581SJan Schmidt 				 */
9578da6d581SJan Schmidt 				mutex_lock(&head->mutex);
9588da6d581SJan Schmidt 				mutex_unlock(&head->mutex);
9598da6d581SJan Schmidt 				btrfs_put_delayed_ref(&head->node);
9608da6d581SJan Schmidt 				goto again;
9618da6d581SJan Schmidt 			}
962d7df2c79SJosef Bacik 			spin_unlock(&delayed_refs->lock);
963097b8a7cSJan Schmidt 			ret = __add_delayed_refs(head, time_seq,
964*dc046b10SJosef Bacik 						 &prefs_delayed, &total_refs,
965*dc046b10SJosef Bacik 						 inum);
966155725c9SJan Schmidt 			mutex_unlock(&head->mutex);
967d7df2c79SJosef Bacik 			if (ret)
9688da6d581SJan Schmidt 				goto out;
969d7df2c79SJosef Bacik 		} else {
9708da6d581SJan Schmidt 			spin_unlock(&delayed_refs->lock);
9717a3ae2f8SJan Schmidt 		}
972d7df2c79SJosef Bacik 	}
9738da6d581SJan Schmidt 
9748da6d581SJan Schmidt 	if (path->slots[0]) {
9758da6d581SJan Schmidt 		struct extent_buffer *leaf;
9768da6d581SJan Schmidt 		int slot;
9778da6d581SJan Schmidt 
978dadcaf78SJan Schmidt 		path->slots[0]--;
9798da6d581SJan Schmidt 		leaf = path->nodes[0];
980dadcaf78SJan Schmidt 		slot = path->slots[0];
9818da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
9828da6d581SJan Schmidt 		if (key.objectid == bytenr &&
983261c84b6SJosef Bacik 		    (key.type == BTRFS_EXTENT_ITEM_KEY ||
984261c84b6SJosef Bacik 		     key.type == BTRFS_METADATA_ITEM_KEY)) {
9858da6d581SJan Schmidt 			ret = __add_inline_refs(fs_info, path, bytenr,
98644853868SJosef Bacik 						&info_level, &prefs,
987*dc046b10SJosef Bacik 						&total_refs, inum);
9888da6d581SJan Schmidt 			if (ret)
9898da6d581SJan Schmidt 				goto out;
990d5c88b73SJan Schmidt 			ret = __add_keyed_refs(fs_info, path, bytenr,
991*dc046b10SJosef Bacik 					       info_level, &prefs, inum);
9928da6d581SJan Schmidt 			if (ret)
9938da6d581SJan Schmidt 				goto out;
9948da6d581SJan Schmidt 		}
9958da6d581SJan Schmidt 	}
9968da6d581SJan Schmidt 	btrfs_release_path(path);
9978da6d581SJan Schmidt 
9988da6d581SJan Schmidt 	list_splice_init(&prefs_delayed, &prefs);
9998da6d581SJan Schmidt 
1000d5c88b73SJan Schmidt 	ret = __add_missing_keys(fs_info, &prefs);
1001d5c88b73SJan Schmidt 	if (ret)
1002d5c88b73SJan Schmidt 		goto out;
1003d5c88b73SJan Schmidt 
1004692206b1SWang Shilong 	__merge_refs(&prefs, 1);
10058da6d581SJan Schmidt 
1006da61d31aSJosef Bacik 	ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs,
1007*dc046b10SJosef Bacik 				      extent_item_pos, total_refs,
1008*dc046b10SJosef Bacik 				      root_objectid);
10098da6d581SJan Schmidt 	if (ret)
10108da6d581SJan Schmidt 		goto out;
10118da6d581SJan Schmidt 
1012692206b1SWang Shilong 	__merge_refs(&prefs, 2);
10138da6d581SJan Schmidt 
10148da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
10158da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
10166c1500f2SJulia Lawall 		WARN_ON(ref->count < 0);
101798cfee21SWang Shilong 		if (roots && ref->count && ref->root_id && ref->parent == 0) {
1018*dc046b10SJosef Bacik 			if (root_objectid && ref->root_id != root_objectid) {
1019*dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1020*dc046b10SJosef Bacik 				goto out;
1021*dc046b10SJosef Bacik 			}
1022*dc046b10SJosef Bacik 
10238da6d581SJan Schmidt 			/* no parent == root of tree */
10248da6d581SJan Schmidt 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
1025f1723939SWang Shilong 			if (ret < 0)
1026f1723939SWang Shilong 				goto out;
10278da6d581SJan Schmidt 		}
10288da6d581SJan Schmidt 		if (ref->count && ref->parent) {
10298a56457fSJosef Bacik 			if (extent_item_pos && !ref->inode_list &&
10308a56457fSJosef Bacik 			    ref->level == 0) {
1031976b1908SJan Schmidt 				u32 bsz;
1032976b1908SJan Schmidt 				struct extent_buffer *eb;
1033707e8a07SDavid Sterba 
1034707e8a07SDavid Sterba 				bsz = fs_info->extent_root->nodesize;
1035976b1908SJan Schmidt 				eb = read_tree_block(fs_info->extent_root,
1036976b1908SJan Schmidt 							   ref->parent, bsz, 0);
1037416bc658SJosef Bacik 				if (!eb || !extent_buffer_uptodate(eb)) {
1038416bc658SJosef Bacik 					free_extent_buffer(eb);
1039c16c2e2eSWang Shilong 					ret = -EIO;
1040c16c2e2eSWang Shilong 					goto out;
1041416bc658SJosef Bacik 				}
10426f7ff6d7SFilipe Manana 				btrfs_tree_read_lock(eb);
10436f7ff6d7SFilipe Manana 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1044976b1908SJan Schmidt 				ret = find_extent_in_eb(eb, bytenr,
1045976b1908SJan Schmidt 							*extent_item_pos, &eie);
10466f7ff6d7SFilipe Manana 				btrfs_tree_read_unlock_blocking(eb);
1047976b1908SJan Schmidt 				free_extent_buffer(eb);
1048f5929cd8SFilipe David Borba Manana 				if (ret < 0)
1049f5929cd8SFilipe David Borba Manana 					goto out;
1050f5929cd8SFilipe David Borba Manana 				ref->inode_list = eie;
1051976b1908SJan Schmidt 			}
10524eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(refs, ref->parent,
10534eb1f66dSTakashi Iwai 						  ref->inode_list,
10544eb1f66dSTakashi Iwai 						  (void **)&eie, GFP_NOFS);
1055f1723939SWang Shilong 			if (ret < 0)
1056f1723939SWang Shilong 				goto out;
10573301958bSJan Schmidt 			if (!ret && extent_item_pos) {
10583301958bSJan Schmidt 				/*
10593301958bSJan Schmidt 				 * we've recorded that parent, so we must extend
10603301958bSJan Schmidt 				 * its inode list here
10613301958bSJan Schmidt 				 */
10623301958bSJan Schmidt 				BUG_ON(!eie);
10633301958bSJan Schmidt 				while (eie->next)
10643301958bSJan Schmidt 					eie = eie->next;
10653301958bSJan Schmidt 				eie->next = ref->inode_list;
10663301958bSJan Schmidt 			}
1067f05c4746SWang Shilong 			eie = NULL;
10688da6d581SJan Schmidt 		}
1069a4fdb61eSWang Shilong 		list_del(&ref->list);
1070b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
10718da6d581SJan Schmidt 	}
10728da6d581SJan Schmidt 
10738da6d581SJan Schmidt out:
10748da6d581SJan Schmidt 	btrfs_free_path(path);
10758da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
10768da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
10778da6d581SJan Schmidt 		list_del(&ref->list);
1078b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
10798da6d581SJan Schmidt 	}
10808da6d581SJan Schmidt 	while (!list_empty(&prefs_delayed)) {
10818da6d581SJan Schmidt 		ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
10828da6d581SJan Schmidt 				       list);
10838da6d581SJan Schmidt 		list_del(&ref->list);
1084b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
10858da6d581SJan Schmidt 	}
1086f05c4746SWang Shilong 	if (ret < 0)
1087f05c4746SWang Shilong 		free_inode_elem_list(eie);
10888da6d581SJan Schmidt 	return ret;
10898da6d581SJan Schmidt }
10908da6d581SJan Schmidt 
1091976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks)
1092976b1908SJan Schmidt {
1093976b1908SJan Schmidt 	struct ulist_node *node = NULL;
1094976b1908SJan Schmidt 	struct extent_inode_elem *eie;
1095976b1908SJan Schmidt 	struct ulist_iterator uiter;
1096976b1908SJan Schmidt 
1097976b1908SJan Schmidt 	ULIST_ITER_INIT(&uiter);
1098976b1908SJan Schmidt 	while ((node = ulist_next(blocks, &uiter))) {
1099976b1908SJan Schmidt 		if (!node->aux)
1100976b1908SJan Schmidt 			continue;
1101995e01b7SJan Schmidt 		eie = (struct extent_inode_elem *)(uintptr_t)node->aux;
1102f05c4746SWang Shilong 		free_inode_elem_list(eie);
1103976b1908SJan Schmidt 		node->aux = 0;
1104976b1908SJan Schmidt 	}
1105976b1908SJan Schmidt 
1106976b1908SJan Schmidt 	ulist_free(blocks);
1107976b1908SJan Schmidt }
1108976b1908SJan Schmidt 
11098da6d581SJan Schmidt /*
11108da6d581SJan Schmidt  * Finds all leafs with a reference to the specified combination of bytenr and
11118da6d581SJan Schmidt  * offset. key_list_head will point to a list of corresponding keys (caller must
11128da6d581SJan Schmidt  * free each list element). The leafs will be stored in the leafs ulist, which
11138da6d581SJan Schmidt  * must be freed with ulist_free.
11148da6d581SJan Schmidt  *
11158da6d581SJan Schmidt  * returns 0 on success, <0 on error
11168da6d581SJan Schmidt  */
11178da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
11188da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
1119097b8a7cSJan Schmidt 				u64 time_seq, struct ulist **leafs,
1120976b1908SJan Schmidt 				const u64 *extent_item_pos)
11218da6d581SJan Schmidt {
11228da6d581SJan Schmidt 	int ret;
11238da6d581SJan Schmidt 
11248da6d581SJan Schmidt 	*leafs = ulist_alloc(GFP_NOFS);
112598cfee21SWang Shilong 	if (!*leafs)
11268da6d581SJan Schmidt 		return -ENOMEM;
11278da6d581SJan Schmidt 
1128097b8a7cSJan Schmidt 	ret = find_parent_nodes(trans, fs_info, bytenr,
1129*dc046b10SJosef Bacik 				time_seq, *leafs, NULL, extent_item_pos, 0, 0);
11308da6d581SJan Schmidt 	if (ret < 0 && ret != -ENOENT) {
1131976b1908SJan Schmidt 		free_leaf_list(*leafs);
11328da6d581SJan Schmidt 		return ret;
11338da6d581SJan Schmidt 	}
11348da6d581SJan Schmidt 
11358da6d581SJan Schmidt 	return 0;
11368da6d581SJan Schmidt }
11378da6d581SJan Schmidt 
11388da6d581SJan Schmidt /*
11398da6d581SJan Schmidt  * walk all backrefs for a given extent to find all roots that reference this
11408da6d581SJan Schmidt  * extent. Walking a backref means finding all extents that reference this
11418da6d581SJan Schmidt  * extent and in turn walk the backrefs of those, too. Naturally this is a
11428da6d581SJan Schmidt  * recursive process, but here it is implemented in an iterative fashion: We
11438da6d581SJan Schmidt  * find all referencing extents for the extent in question and put them on a
11448da6d581SJan Schmidt  * list. In turn, we find all referencing extents for those, further appending
11458da6d581SJan Schmidt  * to the list. The way we iterate the list allows adding more elements after
11468da6d581SJan Schmidt  * the current while iterating. The process stops when we reach the end of the
11478da6d581SJan Schmidt  * list. Found roots are added to the roots list.
11488da6d581SJan Schmidt  *
11498da6d581SJan Schmidt  * returns 0 on success, < 0 on error.
11508da6d581SJan Schmidt  */
11519e351cc8SJosef Bacik static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans,
11528da6d581SJan Schmidt 				  struct btrfs_fs_info *fs_info, u64 bytenr,
1153097b8a7cSJan Schmidt 				  u64 time_seq, struct ulist **roots)
11548da6d581SJan Schmidt {
11558da6d581SJan Schmidt 	struct ulist *tmp;
11568da6d581SJan Schmidt 	struct ulist_node *node = NULL;
1157cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
11588da6d581SJan Schmidt 	int ret;
11598da6d581SJan Schmidt 
11608da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
11618da6d581SJan Schmidt 	if (!tmp)
11628da6d581SJan Schmidt 		return -ENOMEM;
11638da6d581SJan Schmidt 	*roots = ulist_alloc(GFP_NOFS);
11648da6d581SJan Schmidt 	if (!*roots) {
11658da6d581SJan Schmidt 		ulist_free(tmp);
11668da6d581SJan Schmidt 		return -ENOMEM;
11678da6d581SJan Schmidt 	}
11688da6d581SJan Schmidt 
1169cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&uiter);
11708da6d581SJan Schmidt 	while (1) {
1171097b8a7cSJan Schmidt 		ret = find_parent_nodes(trans, fs_info, bytenr,
1172*dc046b10SJosef Bacik 					time_seq, tmp, *roots, NULL, 0, 0);
11738da6d581SJan Schmidt 		if (ret < 0 && ret != -ENOENT) {
11748da6d581SJan Schmidt 			ulist_free(tmp);
11758da6d581SJan Schmidt 			ulist_free(*roots);
11768da6d581SJan Schmidt 			return ret;
11778da6d581SJan Schmidt 		}
1178cd1b413cSJan Schmidt 		node = ulist_next(tmp, &uiter);
11798da6d581SJan Schmidt 		if (!node)
11808da6d581SJan Schmidt 			break;
11818da6d581SJan Schmidt 		bytenr = node->val;
1182bca1a290SWang Shilong 		cond_resched();
11838da6d581SJan Schmidt 	}
11848da6d581SJan Schmidt 
11858da6d581SJan Schmidt 	ulist_free(tmp);
11868da6d581SJan Schmidt 	return 0;
11878da6d581SJan Schmidt }
11888da6d581SJan Schmidt 
11899e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
11909e351cc8SJosef Bacik 			 struct btrfs_fs_info *fs_info, u64 bytenr,
11919e351cc8SJosef Bacik 			 u64 time_seq, struct ulist **roots)
11929e351cc8SJosef Bacik {
11939e351cc8SJosef Bacik 	int ret;
11949e351cc8SJosef Bacik 
11959e351cc8SJosef Bacik 	if (!trans)
11969e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
11979e351cc8SJosef Bacik 	ret = __btrfs_find_all_roots(trans, fs_info, bytenr, time_seq, roots);
11989e351cc8SJosef Bacik 	if (!trans)
11999e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
12009e351cc8SJosef Bacik 	return ret;
12019e351cc8SJosef Bacik }
12029e351cc8SJosef Bacik 
1203*dc046b10SJosef Bacik int btrfs_check_shared(struct btrfs_trans_handle *trans,
1204*dc046b10SJosef Bacik 		       struct btrfs_fs_info *fs_info, u64 root_objectid,
1205*dc046b10SJosef Bacik 		       u64 inum, u64 bytenr)
1206*dc046b10SJosef Bacik {
1207*dc046b10SJosef Bacik 	struct ulist *tmp = NULL;
1208*dc046b10SJosef Bacik 	struct ulist *roots = NULL;
1209*dc046b10SJosef Bacik 	struct ulist_iterator uiter;
1210*dc046b10SJosef Bacik 	struct ulist_node *node;
1211*dc046b10SJosef Bacik 	struct seq_list elem = {};
1212*dc046b10SJosef Bacik 	int ret = 0;
1213*dc046b10SJosef Bacik 
1214*dc046b10SJosef Bacik 	tmp = ulist_alloc(GFP_NOFS);
1215*dc046b10SJosef Bacik 	roots = ulist_alloc(GFP_NOFS);
1216*dc046b10SJosef Bacik 	if (!tmp || !roots) {
1217*dc046b10SJosef Bacik 		ulist_free(tmp);
1218*dc046b10SJosef Bacik 		ulist_free(roots);
1219*dc046b10SJosef Bacik 		return -ENOMEM;
1220*dc046b10SJosef Bacik 	}
1221*dc046b10SJosef Bacik 
1222*dc046b10SJosef Bacik 	if (trans)
1223*dc046b10SJosef Bacik 		btrfs_get_tree_mod_seq(fs_info, &elem);
1224*dc046b10SJosef Bacik 	else
1225*dc046b10SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1226*dc046b10SJosef Bacik 	ULIST_ITER_INIT(&uiter);
1227*dc046b10SJosef Bacik 	while (1) {
1228*dc046b10SJosef Bacik 		ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1229*dc046b10SJosef Bacik 					roots, NULL, root_objectid, inum);
1230*dc046b10SJosef Bacik 		if (ret == BACKREF_FOUND_SHARED) {
1231*dc046b10SJosef Bacik 			ret = 1;
1232*dc046b10SJosef Bacik 			break;
1233*dc046b10SJosef Bacik 		}
1234*dc046b10SJosef Bacik 		if (ret < 0 && ret != -ENOENT)
1235*dc046b10SJosef Bacik 			break;
1236*dc046b10SJosef Bacik 		node = ulist_next(tmp, &uiter);
1237*dc046b10SJosef Bacik 		if (!node)
1238*dc046b10SJosef Bacik 			break;
1239*dc046b10SJosef Bacik 		bytenr = node->val;
1240*dc046b10SJosef Bacik 		cond_resched();
1241*dc046b10SJosef Bacik 	}
1242*dc046b10SJosef Bacik 	if (trans)
1243*dc046b10SJosef Bacik 		btrfs_put_tree_mod_seq(fs_info, &elem);
1244*dc046b10SJosef Bacik 	else
1245*dc046b10SJosef Bacik 		up_read(&fs_info->commit_root_sem);
1246*dc046b10SJosef Bacik 	ulist_free(tmp);
1247*dc046b10SJosef Bacik 	ulist_free(roots);
1248*dc046b10SJosef Bacik 	return ret;
1249*dc046b10SJosef Bacik }
1250*dc046b10SJosef Bacik 
1251a542ad1bSJan Schmidt /*
1252a542ad1bSJan Schmidt  * this makes the path point to (inum INODE_ITEM ioff)
1253a542ad1bSJan Schmidt  */
1254a542ad1bSJan Schmidt int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
1255a542ad1bSJan Schmidt 			struct btrfs_path *path)
1256a542ad1bSJan Schmidt {
1257a542ad1bSJan Schmidt 	struct btrfs_key key;
1258e33d5c3dSKelley Nielsen 	return btrfs_find_item(fs_root, path, inum, ioff,
1259e33d5c3dSKelley Nielsen 			BTRFS_INODE_ITEM_KEY, &key);
1260a542ad1bSJan Schmidt }
1261a542ad1bSJan Schmidt 
1262a542ad1bSJan Schmidt static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
1263a542ad1bSJan Schmidt 				struct btrfs_path *path,
1264a542ad1bSJan Schmidt 				struct btrfs_key *found_key)
1265a542ad1bSJan Schmidt {
1266e33d5c3dSKelley Nielsen 	return btrfs_find_item(fs_root, path, inum, ioff,
1267e33d5c3dSKelley Nielsen 			BTRFS_INODE_REF_KEY, found_key);
1268a542ad1bSJan Schmidt }
1269a542ad1bSJan Schmidt 
1270f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1271f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
1272f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
1273f186373fSMark Fasheh 			  u64 *found_off)
1274f186373fSMark Fasheh {
1275f186373fSMark Fasheh 	int ret, slot;
1276f186373fSMark Fasheh 	struct btrfs_key key;
1277f186373fSMark Fasheh 	struct btrfs_key found_key;
1278f186373fSMark Fasheh 	struct btrfs_inode_extref *extref;
1279f186373fSMark Fasheh 	struct extent_buffer *leaf;
1280f186373fSMark Fasheh 	unsigned long ptr;
1281f186373fSMark Fasheh 
1282f186373fSMark Fasheh 	key.objectid = inode_objectid;
1283962a298fSDavid Sterba 	key.type = BTRFS_INODE_EXTREF_KEY;
1284f186373fSMark Fasheh 	key.offset = start_off;
1285f186373fSMark Fasheh 
1286f186373fSMark Fasheh 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1287f186373fSMark Fasheh 	if (ret < 0)
1288f186373fSMark Fasheh 		return ret;
1289f186373fSMark Fasheh 
1290f186373fSMark Fasheh 	while (1) {
1291f186373fSMark Fasheh 		leaf = path->nodes[0];
1292f186373fSMark Fasheh 		slot = path->slots[0];
1293f186373fSMark Fasheh 		if (slot >= btrfs_header_nritems(leaf)) {
1294f186373fSMark Fasheh 			/*
1295f186373fSMark Fasheh 			 * If the item at offset is not found,
1296f186373fSMark Fasheh 			 * btrfs_search_slot will point us to the slot
1297f186373fSMark Fasheh 			 * where it should be inserted. In our case
1298f186373fSMark Fasheh 			 * that will be the slot directly before the
1299f186373fSMark Fasheh 			 * next INODE_REF_KEY_V2 item. In the case
1300f186373fSMark Fasheh 			 * that we're pointing to the last slot in a
1301f186373fSMark Fasheh 			 * leaf, we must move one leaf over.
1302f186373fSMark Fasheh 			 */
1303f186373fSMark Fasheh 			ret = btrfs_next_leaf(root, path);
1304f186373fSMark Fasheh 			if (ret) {
1305f186373fSMark Fasheh 				if (ret >= 1)
1306f186373fSMark Fasheh 					ret = -ENOENT;
1307f186373fSMark Fasheh 				break;
1308f186373fSMark Fasheh 			}
1309f186373fSMark Fasheh 			continue;
1310f186373fSMark Fasheh 		}
1311f186373fSMark Fasheh 
1312f186373fSMark Fasheh 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1313f186373fSMark Fasheh 
1314f186373fSMark Fasheh 		/*
1315f186373fSMark Fasheh 		 * Check that we're still looking at an extended ref key for
1316f186373fSMark Fasheh 		 * this particular objectid. If we have different
1317f186373fSMark Fasheh 		 * objectid or type then there are no more to be found
1318f186373fSMark Fasheh 		 * in the tree and we can exit.
1319f186373fSMark Fasheh 		 */
1320f186373fSMark Fasheh 		ret = -ENOENT;
1321f186373fSMark Fasheh 		if (found_key.objectid != inode_objectid)
1322f186373fSMark Fasheh 			break;
1323962a298fSDavid Sterba 		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
1324f186373fSMark Fasheh 			break;
1325f186373fSMark Fasheh 
1326f186373fSMark Fasheh 		ret = 0;
1327f186373fSMark Fasheh 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1328f186373fSMark Fasheh 		extref = (struct btrfs_inode_extref *)ptr;
1329f186373fSMark Fasheh 		*ret_extref = extref;
1330f186373fSMark Fasheh 		if (found_off)
1331f186373fSMark Fasheh 			*found_off = found_key.offset;
1332f186373fSMark Fasheh 		break;
1333f186373fSMark Fasheh 	}
1334f186373fSMark Fasheh 
1335f186373fSMark Fasheh 	return ret;
1336f186373fSMark Fasheh }
1337f186373fSMark Fasheh 
133848a3b636SEric Sandeen /*
133948a3b636SEric Sandeen  * this iterates to turn a name (from iref/extref) into a full filesystem path.
134048a3b636SEric Sandeen  * Elements of the path are separated by '/' and the path is guaranteed to be
134148a3b636SEric Sandeen  * 0-terminated. the path is only given within the current file system.
134248a3b636SEric Sandeen  * Therefore, it never starts with a '/'. the caller is responsible to provide
134348a3b636SEric Sandeen  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
134448a3b636SEric Sandeen  * the start point of the resulting string is returned. this pointer is within
134548a3b636SEric Sandeen  * dest, normally.
134648a3b636SEric Sandeen  * in case the path buffer would overflow, the pointer is decremented further
134748a3b636SEric Sandeen  * as if output was written to the buffer, though no more output is actually
134848a3b636SEric Sandeen  * generated. that way, the caller can determine how much space would be
134948a3b636SEric Sandeen  * required for the path to fit into the buffer. in that case, the returned
135048a3b636SEric Sandeen  * value will be smaller than dest. callers must check this!
135148a3b636SEric Sandeen  */
135296b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1353d24bec3aSMark Fasheh 			u32 name_len, unsigned long name_off,
1354a542ad1bSJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
1355a542ad1bSJan Schmidt 			char *dest, u32 size)
1356a542ad1bSJan Schmidt {
1357a542ad1bSJan Schmidt 	int slot;
1358a542ad1bSJan Schmidt 	u64 next_inum;
1359a542ad1bSJan Schmidt 	int ret;
1360661bec6bSGabriel de Perthuis 	s64 bytes_left = ((s64)size) - 1;
1361a542ad1bSJan Schmidt 	struct extent_buffer *eb = eb_in;
1362a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1363b916a59aSJan Schmidt 	int leave_spinning = path->leave_spinning;
1364d24bec3aSMark Fasheh 	struct btrfs_inode_ref *iref;
1365a542ad1bSJan Schmidt 
1366a542ad1bSJan Schmidt 	if (bytes_left >= 0)
1367a542ad1bSJan Schmidt 		dest[bytes_left] = '\0';
1368a542ad1bSJan Schmidt 
1369b916a59aSJan Schmidt 	path->leave_spinning = 1;
1370a542ad1bSJan Schmidt 	while (1) {
1371d24bec3aSMark Fasheh 		bytes_left -= name_len;
1372a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1373a542ad1bSJan Schmidt 			read_extent_buffer(eb, dest + bytes_left,
1374d24bec3aSMark Fasheh 					   name_off, name_len);
1375b916a59aSJan Schmidt 		if (eb != eb_in) {
1376b916a59aSJan Schmidt 			btrfs_tree_read_unlock_blocking(eb);
1377a542ad1bSJan Schmidt 			free_extent_buffer(eb);
1378b916a59aSJan Schmidt 		}
1379a542ad1bSJan Schmidt 		ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
13808f24b496SJan Schmidt 		if (ret > 0)
13818f24b496SJan Schmidt 			ret = -ENOENT;
1382a542ad1bSJan Schmidt 		if (ret)
1383a542ad1bSJan Schmidt 			break;
1384d24bec3aSMark Fasheh 
1385a542ad1bSJan Schmidt 		next_inum = found_key.offset;
1386a542ad1bSJan Schmidt 
1387a542ad1bSJan Schmidt 		/* regular exit ahead */
1388a542ad1bSJan Schmidt 		if (parent == next_inum)
1389a542ad1bSJan Schmidt 			break;
1390a542ad1bSJan Schmidt 
1391a542ad1bSJan Schmidt 		slot = path->slots[0];
1392a542ad1bSJan Schmidt 		eb = path->nodes[0];
1393a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
1394b916a59aSJan Schmidt 		if (eb != eb_in) {
1395a542ad1bSJan Schmidt 			atomic_inc(&eb->refs);
1396b916a59aSJan Schmidt 			btrfs_tree_read_lock(eb);
1397b916a59aSJan Schmidt 			btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1398b916a59aSJan Schmidt 		}
1399a542ad1bSJan Schmidt 		btrfs_release_path(path);
1400a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1401d24bec3aSMark Fasheh 
1402d24bec3aSMark Fasheh 		name_len = btrfs_inode_ref_name_len(eb, iref);
1403d24bec3aSMark Fasheh 		name_off = (unsigned long)(iref + 1);
1404d24bec3aSMark Fasheh 
1405a542ad1bSJan Schmidt 		parent = next_inum;
1406a542ad1bSJan Schmidt 		--bytes_left;
1407a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1408a542ad1bSJan Schmidt 			dest[bytes_left] = '/';
1409a542ad1bSJan Schmidt 	}
1410a542ad1bSJan Schmidt 
1411a542ad1bSJan Schmidt 	btrfs_release_path(path);
1412b916a59aSJan Schmidt 	path->leave_spinning = leave_spinning;
1413a542ad1bSJan Schmidt 
1414a542ad1bSJan Schmidt 	if (ret)
1415a542ad1bSJan Schmidt 		return ERR_PTR(ret);
1416a542ad1bSJan Schmidt 
1417a542ad1bSJan Schmidt 	return dest + bytes_left;
1418a542ad1bSJan Schmidt }
1419a542ad1bSJan Schmidt 
1420a542ad1bSJan Schmidt /*
1421a542ad1bSJan Schmidt  * this makes the path point to (logical EXTENT_ITEM *)
1422a542ad1bSJan Schmidt  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1423a542ad1bSJan Schmidt  * tree blocks and <0 on error.
1424a542ad1bSJan Schmidt  */
1425a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
142669917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
142769917e43SLiu Bo 			u64 *flags_ret)
1428a542ad1bSJan Schmidt {
1429a542ad1bSJan Schmidt 	int ret;
1430a542ad1bSJan Schmidt 	u64 flags;
1431261c84b6SJosef Bacik 	u64 size = 0;
1432a542ad1bSJan Schmidt 	u32 item_size;
1433a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1434a542ad1bSJan Schmidt 	struct btrfs_extent_item *ei;
1435a542ad1bSJan Schmidt 	struct btrfs_key key;
1436a542ad1bSJan Schmidt 
1437261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1438261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1439261c84b6SJosef Bacik 	else
1440a542ad1bSJan Schmidt 		key.type = BTRFS_EXTENT_ITEM_KEY;
1441a542ad1bSJan Schmidt 	key.objectid = logical;
1442a542ad1bSJan Schmidt 	key.offset = (u64)-1;
1443a542ad1bSJan Schmidt 
1444a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1445a542ad1bSJan Schmidt 	if (ret < 0)
1446a542ad1bSJan Schmidt 		return ret;
1447a542ad1bSJan Schmidt 
1448850a8cdfSWang Shilong 	ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1449850a8cdfSWang Shilong 	if (ret) {
1450850a8cdfSWang Shilong 		if (ret > 0)
1451580f0a67SJosef Bacik 			ret = -ENOENT;
1452580f0a67SJosef Bacik 		return ret;
1453580f0a67SJosef Bacik 	}
1454850a8cdfSWang Shilong 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1455261c84b6SJosef Bacik 	if (found_key->type == BTRFS_METADATA_ITEM_KEY)
1456707e8a07SDavid Sterba 		size = fs_info->extent_root->nodesize;
1457261c84b6SJosef Bacik 	else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1458261c84b6SJosef Bacik 		size = found_key->offset;
1459261c84b6SJosef Bacik 
1460580f0a67SJosef Bacik 	if (found_key->objectid > logical ||
1461261c84b6SJosef Bacik 	    found_key->objectid + size <= logical) {
1462c1c9ff7cSGeert Uytterhoeven 		pr_debug("logical %llu is not within any extent\n", logical);
1463a542ad1bSJan Schmidt 		return -ENOENT;
14644692cf58SJan Schmidt 	}
1465a542ad1bSJan Schmidt 
1466a542ad1bSJan Schmidt 	eb = path->nodes[0];
1467a542ad1bSJan Schmidt 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1468a542ad1bSJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
1469a542ad1bSJan Schmidt 
1470a542ad1bSJan Schmidt 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1471a542ad1bSJan Schmidt 	flags = btrfs_extent_flags(eb, ei);
1472a542ad1bSJan Schmidt 
14734692cf58SJan Schmidt 	pr_debug("logical %llu is at position %llu within the extent (%llu "
14744692cf58SJan Schmidt 		 "EXTENT_ITEM %llu) flags %#llx size %u\n",
1475c1c9ff7cSGeert Uytterhoeven 		 logical, logical - found_key->objectid, found_key->objectid,
1476c1c9ff7cSGeert Uytterhoeven 		 found_key->offset, flags, item_size);
147769917e43SLiu Bo 
147869917e43SLiu Bo 	WARN_ON(!flags_ret);
147969917e43SLiu Bo 	if (flags_ret) {
1480a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
148169917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
148269917e43SLiu Bo 		else if (flags & BTRFS_EXTENT_FLAG_DATA)
148369917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_DATA;
148469917e43SLiu Bo 		else
148569917e43SLiu Bo 			BUG_ON(1);
148669917e43SLiu Bo 		return 0;
148769917e43SLiu Bo 	}
1488a542ad1bSJan Schmidt 
1489a542ad1bSJan Schmidt 	return -EIO;
1490a542ad1bSJan Schmidt }
1491a542ad1bSJan Schmidt 
1492a542ad1bSJan Schmidt /*
1493a542ad1bSJan Schmidt  * helper function to iterate extent inline refs. ptr must point to a 0 value
1494a542ad1bSJan Schmidt  * for the first call and may be modified. it is used to track state.
1495a542ad1bSJan Schmidt  * if more refs exist, 0 is returned and the next call to
1496a542ad1bSJan Schmidt  * __get_extent_inline_ref must pass the modified ptr parameter to get the
1497a542ad1bSJan Schmidt  * next ref. after the last ref was processed, 1 is returned.
1498a542ad1bSJan Schmidt  * returns <0 on error
1499a542ad1bSJan Schmidt  */
1500a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
15016eda71d0SLiu Bo 				   struct btrfs_key *key,
1502a542ad1bSJan Schmidt 				   struct btrfs_extent_item *ei, u32 item_size,
1503a542ad1bSJan Schmidt 				   struct btrfs_extent_inline_ref **out_eiref,
1504a542ad1bSJan Schmidt 				   int *out_type)
1505a542ad1bSJan Schmidt {
1506a542ad1bSJan Schmidt 	unsigned long end;
1507a542ad1bSJan Schmidt 	u64 flags;
1508a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1509a542ad1bSJan Schmidt 
1510a542ad1bSJan Schmidt 	if (!*ptr) {
1511a542ad1bSJan Schmidt 		/* first call */
1512a542ad1bSJan Schmidt 		flags = btrfs_extent_flags(eb, ei);
1513a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
15146eda71d0SLiu Bo 			if (key->type == BTRFS_METADATA_ITEM_KEY) {
15156eda71d0SLiu Bo 				/* a skinny metadata extent */
15166eda71d0SLiu Bo 				*out_eiref =
15176eda71d0SLiu Bo 				     (struct btrfs_extent_inline_ref *)(ei + 1);
15186eda71d0SLiu Bo 			} else {
15196eda71d0SLiu Bo 				WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1520a542ad1bSJan Schmidt 				info = (struct btrfs_tree_block_info *)(ei + 1);
1521a542ad1bSJan Schmidt 				*out_eiref =
1522a542ad1bSJan Schmidt 				   (struct btrfs_extent_inline_ref *)(info + 1);
15236eda71d0SLiu Bo 			}
1524a542ad1bSJan Schmidt 		} else {
1525a542ad1bSJan Schmidt 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1526a542ad1bSJan Schmidt 		}
1527a542ad1bSJan Schmidt 		*ptr = (unsigned long)*out_eiref;
1528cd857dd6SLiu Bo 		if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
1529a542ad1bSJan Schmidt 			return -ENOENT;
1530a542ad1bSJan Schmidt 	}
1531a542ad1bSJan Schmidt 
1532a542ad1bSJan Schmidt 	end = (unsigned long)ei + item_size;
15336eda71d0SLiu Bo 	*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
1534a542ad1bSJan Schmidt 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1535a542ad1bSJan Schmidt 
1536a542ad1bSJan Schmidt 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1537a542ad1bSJan Schmidt 	WARN_ON(*ptr > end);
1538a542ad1bSJan Schmidt 	if (*ptr == end)
1539a542ad1bSJan Schmidt 		return 1; /* last */
1540a542ad1bSJan Schmidt 
1541a542ad1bSJan Schmidt 	return 0;
1542a542ad1bSJan Schmidt }
1543a542ad1bSJan Schmidt 
1544a542ad1bSJan Schmidt /*
1545a542ad1bSJan Schmidt  * reads the tree block backref for an extent. tree level and root are returned
1546a542ad1bSJan Schmidt  * through out_level and out_root. ptr must point to a 0 value for the first
1547a542ad1bSJan Schmidt  * call and may be modified (see __get_extent_inline_ref comment).
1548a542ad1bSJan Schmidt  * returns 0 if data was provided, 1 if there was no more data to provide or
1549a542ad1bSJan Schmidt  * <0 on error.
1550a542ad1bSJan Schmidt  */
1551a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
15526eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
15536eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level)
1554a542ad1bSJan Schmidt {
1555a542ad1bSJan Schmidt 	int ret;
1556a542ad1bSJan Schmidt 	int type;
1557a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1558a542ad1bSJan Schmidt 	struct btrfs_extent_inline_ref *eiref;
1559a542ad1bSJan Schmidt 
1560a542ad1bSJan Schmidt 	if (*ptr == (unsigned long)-1)
1561a542ad1bSJan Schmidt 		return 1;
1562a542ad1bSJan Schmidt 
1563a542ad1bSJan Schmidt 	while (1) {
15646eda71d0SLiu Bo 		ret = __get_extent_inline_ref(ptr, eb, key, ei, item_size,
1565a542ad1bSJan Schmidt 					      &eiref, &type);
1566a542ad1bSJan Schmidt 		if (ret < 0)
1567a542ad1bSJan Schmidt 			return ret;
1568a542ad1bSJan Schmidt 
1569a542ad1bSJan Schmidt 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1570a542ad1bSJan Schmidt 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1571a542ad1bSJan Schmidt 			break;
1572a542ad1bSJan Schmidt 
1573a542ad1bSJan Schmidt 		if (ret == 1)
1574a542ad1bSJan Schmidt 			return 1;
1575a542ad1bSJan Schmidt 	}
1576a542ad1bSJan Schmidt 
1577a542ad1bSJan Schmidt 	/* we can treat both ref types equally here */
1578a542ad1bSJan Schmidt 	info = (struct btrfs_tree_block_info *)(ei + 1);
1579a542ad1bSJan Schmidt 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1580a542ad1bSJan Schmidt 	*out_level = btrfs_tree_block_level(eb, info);
1581a542ad1bSJan Schmidt 
1582a542ad1bSJan Schmidt 	if (ret == 1)
1583a542ad1bSJan Schmidt 		*ptr = (unsigned long)-1;
1584a542ad1bSJan Schmidt 
1585a542ad1bSJan Schmidt 	return 0;
1586a542ad1bSJan Schmidt }
1587a542ad1bSJan Schmidt 
1588976b1908SJan Schmidt static int iterate_leaf_refs(struct extent_inode_elem *inode_list,
1589976b1908SJan Schmidt 				u64 root, u64 extent_item_objectid,
15904692cf58SJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1591a542ad1bSJan Schmidt {
1592976b1908SJan Schmidt 	struct extent_inode_elem *eie;
15934692cf58SJan Schmidt 	int ret = 0;
1594a542ad1bSJan Schmidt 
1595976b1908SJan Schmidt 	for (eie = inode_list; eie; eie = eie->next) {
15964692cf58SJan Schmidt 		pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
1597976b1908SJan Schmidt 			 "root %llu\n", extent_item_objectid,
1598976b1908SJan Schmidt 			 eie->inum, eie->offset, root);
1599976b1908SJan Schmidt 		ret = iterate(eie->inum, eie->offset, root, ctx);
16004692cf58SJan Schmidt 		if (ret) {
1601976b1908SJan Schmidt 			pr_debug("stopping iteration for %llu due to ret=%d\n",
1602976b1908SJan Schmidt 				 extent_item_objectid, ret);
1603a542ad1bSJan Schmidt 			break;
1604a542ad1bSJan Schmidt 		}
1605a542ad1bSJan Schmidt 	}
1606a542ad1bSJan Schmidt 
1607a542ad1bSJan Schmidt 	return ret;
1608a542ad1bSJan Schmidt }
1609a542ad1bSJan Schmidt 
1610a542ad1bSJan Schmidt /*
1611a542ad1bSJan Schmidt  * calls iterate() for every inode that references the extent identified by
16124692cf58SJan Schmidt  * the given parameters.
1613a542ad1bSJan Schmidt  * when the iterator function returns a non-zero value, iteration stops.
1614a542ad1bSJan Schmidt  */
1615a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
16164692cf58SJan Schmidt 				u64 extent_item_objectid, u64 extent_item_pos,
16177a3ae2f8SJan Schmidt 				int search_commit_root,
1618a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1619a542ad1bSJan Schmidt {
1620a542ad1bSJan Schmidt 	int ret;
1621da61d31aSJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
16227a3ae2f8SJan Schmidt 	struct ulist *refs = NULL;
16237a3ae2f8SJan Schmidt 	struct ulist *roots = NULL;
16244692cf58SJan Schmidt 	struct ulist_node *ref_node = NULL;
16254692cf58SJan Schmidt 	struct ulist_node *root_node = NULL;
16268445f61cSJan Schmidt 	struct seq_list tree_mod_seq_elem = {};
1627cd1b413cSJan Schmidt 	struct ulist_iterator ref_uiter;
1628cd1b413cSJan Schmidt 	struct ulist_iterator root_uiter;
1629a542ad1bSJan Schmidt 
16304692cf58SJan Schmidt 	pr_debug("resolving all inodes for extent %llu\n",
16314692cf58SJan Schmidt 			extent_item_objectid);
16324692cf58SJan Schmidt 
1633da61d31aSJosef Bacik 	if (!search_commit_root) {
16347a3ae2f8SJan Schmidt 		trans = btrfs_join_transaction(fs_info->extent_root);
16357a3ae2f8SJan Schmidt 		if (IS_ERR(trans))
16367a3ae2f8SJan Schmidt 			return PTR_ERR(trans);
16378445f61cSJan Schmidt 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
16389e351cc8SJosef Bacik 	} else {
16399e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
16407a3ae2f8SJan Schmidt 	}
16414692cf58SJan Schmidt 
16424692cf58SJan Schmidt 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1643097b8a7cSJan Schmidt 				   tree_mod_seq_elem.seq, &refs,
16448445f61cSJan Schmidt 				   &extent_item_pos);
16454692cf58SJan Schmidt 	if (ret)
16464692cf58SJan Schmidt 		goto out;
16474692cf58SJan Schmidt 
1648cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&ref_uiter);
1649cd1b413cSJan Schmidt 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
16509e351cc8SJosef Bacik 		ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val,
16518445f61cSJan Schmidt 					     tree_mod_seq_elem.seq, &roots);
16524692cf58SJan Schmidt 		if (ret)
1653a542ad1bSJan Schmidt 			break;
1654cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&root_uiter);
1655cd1b413cSJan Schmidt 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
1656976b1908SJan Schmidt 			pr_debug("root %llu references leaf %llu, data list "
165734d73f54SAlexander Block 				 "%#llx\n", root_node->val, ref_node->val,
1658c1c9ff7cSGeert Uytterhoeven 				 ref_node->aux);
1659995e01b7SJan Schmidt 			ret = iterate_leaf_refs((struct extent_inode_elem *)
1660995e01b7SJan Schmidt 						(uintptr_t)ref_node->aux,
1661995e01b7SJan Schmidt 						root_node->val,
1662995e01b7SJan Schmidt 						extent_item_objectid,
1663a542ad1bSJan Schmidt 						iterate, ctx);
16644692cf58SJan Schmidt 		}
1665976b1908SJan Schmidt 		ulist_free(roots);
1666a542ad1bSJan Schmidt 	}
1667a542ad1bSJan Schmidt 
1668976b1908SJan Schmidt 	free_leaf_list(refs);
16694692cf58SJan Schmidt out:
16707a3ae2f8SJan Schmidt 	if (!search_commit_root) {
16718445f61cSJan Schmidt 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
16724692cf58SJan Schmidt 		btrfs_end_transaction(trans, fs_info->extent_root);
16739e351cc8SJosef Bacik 	} else {
16749e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
16757a3ae2f8SJan Schmidt 	}
16767a3ae2f8SJan Schmidt 
1677a542ad1bSJan Schmidt 	return ret;
1678a542ad1bSJan Schmidt }
1679a542ad1bSJan Schmidt 
1680a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1681a542ad1bSJan Schmidt 				struct btrfs_path *path,
1682a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1683a542ad1bSJan Schmidt {
1684a542ad1bSJan Schmidt 	int ret;
16854692cf58SJan Schmidt 	u64 extent_item_pos;
168669917e43SLiu Bo 	u64 flags = 0;
1687a542ad1bSJan Schmidt 	struct btrfs_key found_key;
16887a3ae2f8SJan Schmidt 	int search_commit_root = path->search_commit_root;
1689a542ad1bSJan Schmidt 
169069917e43SLiu Bo 	ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
16914692cf58SJan Schmidt 	btrfs_release_path(path);
1692a542ad1bSJan Schmidt 	if (ret < 0)
1693a542ad1bSJan Schmidt 		return ret;
169469917e43SLiu Bo 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
16953627bf45SStefan Behrens 		return -EINVAL;
1696a542ad1bSJan Schmidt 
16974692cf58SJan Schmidt 	extent_item_pos = logical - found_key.objectid;
16987a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
16997a3ae2f8SJan Schmidt 					extent_item_pos, search_commit_root,
17007a3ae2f8SJan Schmidt 					iterate, ctx);
1701a542ad1bSJan Schmidt 
1702a542ad1bSJan Schmidt 	return ret;
1703a542ad1bSJan Schmidt }
1704a542ad1bSJan Schmidt 
1705d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1706d24bec3aSMark Fasheh 			      struct extent_buffer *eb, void *ctx);
1707d24bec3aSMark Fasheh 
1708d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1709a542ad1bSJan Schmidt 			      struct btrfs_path *path,
1710a542ad1bSJan Schmidt 			      iterate_irefs_t *iterate, void *ctx)
1711a542ad1bSJan Schmidt {
1712aefc1eb1SJan Schmidt 	int ret = 0;
1713a542ad1bSJan Schmidt 	int slot;
1714a542ad1bSJan Schmidt 	u32 cur;
1715a542ad1bSJan Schmidt 	u32 len;
1716a542ad1bSJan Schmidt 	u32 name_len;
1717a542ad1bSJan Schmidt 	u64 parent = 0;
1718a542ad1bSJan Schmidt 	int found = 0;
1719a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1720a542ad1bSJan Schmidt 	struct btrfs_item *item;
1721a542ad1bSJan Schmidt 	struct btrfs_inode_ref *iref;
1722a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1723a542ad1bSJan Schmidt 
1724aefc1eb1SJan Schmidt 	while (!ret) {
1725a542ad1bSJan Schmidt 		ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
1726a542ad1bSJan Schmidt 				     &found_key);
1727a542ad1bSJan Schmidt 		if (ret < 0)
1728a542ad1bSJan Schmidt 			break;
1729a542ad1bSJan Schmidt 		if (ret) {
1730a542ad1bSJan Schmidt 			ret = found ? 0 : -ENOENT;
1731a542ad1bSJan Schmidt 			break;
1732a542ad1bSJan Schmidt 		}
1733a542ad1bSJan Schmidt 		++found;
1734a542ad1bSJan Schmidt 
1735a542ad1bSJan Schmidt 		parent = found_key.offset;
1736a542ad1bSJan Schmidt 		slot = path->slots[0];
17373fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
17383fe81ce2SFilipe David Borba Manana 		if (!eb) {
17393fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
17403fe81ce2SFilipe David Borba Manana 			break;
17413fe81ce2SFilipe David Borba Manana 		}
17423fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
1743b916a59aSJan Schmidt 		btrfs_tree_read_lock(eb);
1744b916a59aSJan Schmidt 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1745a542ad1bSJan Schmidt 		btrfs_release_path(path);
1746a542ad1bSJan Schmidt 
1747dd3cc16bSRoss Kirk 		item = btrfs_item_nr(slot);
1748a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1749a542ad1bSJan Schmidt 
1750a542ad1bSJan Schmidt 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1751a542ad1bSJan Schmidt 			name_len = btrfs_inode_ref_name_len(eb, iref);
1752a542ad1bSJan Schmidt 			/* path must be released before calling iterate()! */
17534692cf58SJan Schmidt 			pr_debug("following ref at offset %u for inode %llu in "
1754c1c9ff7cSGeert Uytterhoeven 				 "tree %llu\n", cur, found_key.objectid,
1755c1c9ff7cSGeert Uytterhoeven 				 fs_root->objectid);
1756d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
1757d24bec3aSMark Fasheh 				      (unsigned long)(iref + 1), eb, ctx);
1758aefc1eb1SJan Schmidt 			if (ret)
1759a542ad1bSJan Schmidt 				break;
1760a542ad1bSJan Schmidt 			len = sizeof(*iref) + name_len;
1761a542ad1bSJan Schmidt 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
1762a542ad1bSJan Schmidt 		}
1763b916a59aSJan Schmidt 		btrfs_tree_read_unlock_blocking(eb);
1764a542ad1bSJan Schmidt 		free_extent_buffer(eb);
1765a542ad1bSJan Schmidt 	}
1766a542ad1bSJan Schmidt 
1767a542ad1bSJan Schmidt 	btrfs_release_path(path);
1768a542ad1bSJan Schmidt 
1769a542ad1bSJan Schmidt 	return ret;
1770a542ad1bSJan Schmidt }
1771a542ad1bSJan Schmidt 
1772d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
1773d24bec3aSMark Fasheh 				 struct btrfs_path *path,
1774d24bec3aSMark Fasheh 				 iterate_irefs_t *iterate, void *ctx)
1775d24bec3aSMark Fasheh {
1776d24bec3aSMark Fasheh 	int ret;
1777d24bec3aSMark Fasheh 	int slot;
1778d24bec3aSMark Fasheh 	u64 offset = 0;
1779d24bec3aSMark Fasheh 	u64 parent;
1780d24bec3aSMark Fasheh 	int found = 0;
1781d24bec3aSMark Fasheh 	struct extent_buffer *eb;
1782d24bec3aSMark Fasheh 	struct btrfs_inode_extref *extref;
1783d24bec3aSMark Fasheh 	struct extent_buffer *leaf;
1784d24bec3aSMark Fasheh 	u32 item_size;
1785d24bec3aSMark Fasheh 	u32 cur_offset;
1786d24bec3aSMark Fasheh 	unsigned long ptr;
1787d24bec3aSMark Fasheh 
1788d24bec3aSMark Fasheh 	while (1) {
1789d24bec3aSMark Fasheh 		ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
1790d24bec3aSMark Fasheh 					    &offset);
1791d24bec3aSMark Fasheh 		if (ret < 0)
1792d24bec3aSMark Fasheh 			break;
1793d24bec3aSMark Fasheh 		if (ret) {
1794d24bec3aSMark Fasheh 			ret = found ? 0 : -ENOENT;
1795d24bec3aSMark Fasheh 			break;
1796d24bec3aSMark Fasheh 		}
1797d24bec3aSMark Fasheh 		++found;
1798d24bec3aSMark Fasheh 
1799d24bec3aSMark Fasheh 		slot = path->slots[0];
18003fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
18013fe81ce2SFilipe David Borba Manana 		if (!eb) {
18023fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
18033fe81ce2SFilipe David Borba Manana 			break;
18043fe81ce2SFilipe David Borba Manana 		}
18053fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
1806d24bec3aSMark Fasheh 
1807d24bec3aSMark Fasheh 		btrfs_tree_read_lock(eb);
1808d24bec3aSMark Fasheh 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1809d24bec3aSMark Fasheh 		btrfs_release_path(path);
1810d24bec3aSMark Fasheh 
1811d24bec3aSMark Fasheh 		leaf = path->nodes[0];
1812e94acd86SValentina Giusti 		item_size = btrfs_item_size_nr(leaf, slot);
1813e94acd86SValentina Giusti 		ptr = btrfs_item_ptr_offset(leaf, slot);
1814d24bec3aSMark Fasheh 		cur_offset = 0;
1815d24bec3aSMark Fasheh 
1816d24bec3aSMark Fasheh 		while (cur_offset < item_size) {
1817d24bec3aSMark Fasheh 			u32 name_len;
1818d24bec3aSMark Fasheh 
1819d24bec3aSMark Fasheh 			extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
1820d24bec3aSMark Fasheh 			parent = btrfs_inode_extref_parent(eb, extref);
1821d24bec3aSMark Fasheh 			name_len = btrfs_inode_extref_name_len(eb, extref);
1822d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
1823d24bec3aSMark Fasheh 				      (unsigned long)&extref->name, eb, ctx);
1824d24bec3aSMark Fasheh 			if (ret)
1825d24bec3aSMark Fasheh 				break;
1826d24bec3aSMark Fasheh 
1827d24bec3aSMark Fasheh 			cur_offset += btrfs_inode_extref_name_len(leaf, extref);
1828d24bec3aSMark Fasheh 			cur_offset += sizeof(*extref);
1829d24bec3aSMark Fasheh 		}
1830d24bec3aSMark Fasheh 		btrfs_tree_read_unlock_blocking(eb);
1831d24bec3aSMark Fasheh 		free_extent_buffer(eb);
1832d24bec3aSMark Fasheh 
1833d24bec3aSMark Fasheh 		offset++;
1834d24bec3aSMark Fasheh 	}
1835d24bec3aSMark Fasheh 
1836d24bec3aSMark Fasheh 	btrfs_release_path(path);
1837d24bec3aSMark Fasheh 
1838d24bec3aSMark Fasheh 	return ret;
1839d24bec3aSMark Fasheh }
1840d24bec3aSMark Fasheh 
1841d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
1842d24bec3aSMark Fasheh 			 struct btrfs_path *path, iterate_irefs_t *iterate,
1843d24bec3aSMark Fasheh 			 void *ctx)
1844d24bec3aSMark Fasheh {
1845d24bec3aSMark Fasheh 	int ret;
1846d24bec3aSMark Fasheh 	int found_refs = 0;
1847d24bec3aSMark Fasheh 
1848d24bec3aSMark Fasheh 	ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
1849d24bec3aSMark Fasheh 	if (!ret)
1850d24bec3aSMark Fasheh 		++found_refs;
1851d24bec3aSMark Fasheh 	else if (ret != -ENOENT)
1852d24bec3aSMark Fasheh 		return ret;
1853d24bec3aSMark Fasheh 
1854d24bec3aSMark Fasheh 	ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
1855d24bec3aSMark Fasheh 	if (ret == -ENOENT && found_refs)
1856d24bec3aSMark Fasheh 		return 0;
1857d24bec3aSMark Fasheh 
1858d24bec3aSMark Fasheh 	return ret;
1859d24bec3aSMark Fasheh }
1860d24bec3aSMark Fasheh 
1861a542ad1bSJan Schmidt /*
1862a542ad1bSJan Schmidt  * returns 0 if the path could be dumped (probably truncated)
1863a542ad1bSJan Schmidt  * returns <0 in case of an error
1864a542ad1bSJan Schmidt  */
1865d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
1866a542ad1bSJan Schmidt 			 struct extent_buffer *eb, void *ctx)
1867a542ad1bSJan Schmidt {
1868a542ad1bSJan Schmidt 	struct inode_fs_paths *ipath = ctx;
1869a542ad1bSJan Schmidt 	char *fspath;
1870a542ad1bSJan Schmidt 	char *fspath_min;
1871a542ad1bSJan Schmidt 	int i = ipath->fspath->elem_cnt;
1872a542ad1bSJan Schmidt 	const int s_ptr = sizeof(char *);
1873a542ad1bSJan Schmidt 	u32 bytes_left;
1874a542ad1bSJan Schmidt 
1875a542ad1bSJan Schmidt 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
1876a542ad1bSJan Schmidt 					ipath->fspath->bytes_left - s_ptr : 0;
1877a542ad1bSJan Schmidt 
1878740c3d22SChris Mason 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
187996b5bd77SJan Schmidt 	fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
188096b5bd77SJan Schmidt 				   name_off, eb, inum, fspath_min, bytes_left);
1881a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
1882a542ad1bSJan Schmidt 		return PTR_ERR(fspath);
1883a542ad1bSJan Schmidt 
1884a542ad1bSJan Schmidt 	if (fspath > fspath_min) {
1885745c4d8eSJeff Mahoney 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
1886a542ad1bSJan Schmidt 		++ipath->fspath->elem_cnt;
1887a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = fspath - fspath_min;
1888a542ad1bSJan Schmidt 	} else {
1889a542ad1bSJan Schmidt 		++ipath->fspath->elem_missed;
1890a542ad1bSJan Schmidt 		ipath->fspath->bytes_missing += fspath_min - fspath;
1891a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = 0;
1892a542ad1bSJan Schmidt 	}
1893a542ad1bSJan Schmidt 
1894a542ad1bSJan Schmidt 	return 0;
1895a542ad1bSJan Schmidt }
1896a542ad1bSJan Schmidt 
1897a542ad1bSJan Schmidt /*
1898a542ad1bSJan Schmidt  * this dumps all file system paths to the inode into the ipath struct, provided
1899a542ad1bSJan Schmidt  * is has been created large enough. each path is zero-terminated and accessed
1900740c3d22SChris Mason  * from ipath->fspath->val[i].
1901a542ad1bSJan Schmidt  * when it returns, there are ipath->fspath->elem_cnt number of paths available
1902740c3d22SChris Mason  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
1903a542ad1bSJan Schmidt  * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1904a542ad1bSJan Schmidt  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1905a542ad1bSJan Schmidt  * have been needed to return all paths.
1906a542ad1bSJan Schmidt  */
1907a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
1908a542ad1bSJan Schmidt {
1909a542ad1bSJan Schmidt 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
1910a542ad1bSJan Schmidt 			     inode_to_path, ipath);
1911a542ad1bSJan Schmidt }
1912a542ad1bSJan Schmidt 
1913a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes)
1914a542ad1bSJan Schmidt {
1915a542ad1bSJan Schmidt 	struct btrfs_data_container *data;
1916a542ad1bSJan Schmidt 	size_t alloc_bytes;
1917a542ad1bSJan Schmidt 
1918a542ad1bSJan Schmidt 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
1919425d17a2SLiu Bo 	data = vmalloc(alloc_bytes);
1920a542ad1bSJan Schmidt 	if (!data)
1921a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
1922a542ad1bSJan Schmidt 
1923a542ad1bSJan Schmidt 	if (total_bytes >= sizeof(*data)) {
1924a542ad1bSJan Schmidt 		data->bytes_left = total_bytes - sizeof(*data);
1925a542ad1bSJan Schmidt 		data->bytes_missing = 0;
1926a542ad1bSJan Schmidt 	} else {
1927a542ad1bSJan Schmidt 		data->bytes_missing = sizeof(*data) - total_bytes;
1928a542ad1bSJan Schmidt 		data->bytes_left = 0;
1929a542ad1bSJan Schmidt 	}
1930a542ad1bSJan Schmidt 
1931a542ad1bSJan Schmidt 	data->elem_cnt = 0;
1932a542ad1bSJan Schmidt 	data->elem_missed = 0;
1933a542ad1bSJan Schmidt 
1934a542ad1bSJan Schmidt 	return data;
1935a542ad1bSJan Schmidt }
1936a542ad1bSJan Schmidt 
1937a542ad1bSJan Schmidt /*
1938a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
1939a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
1940a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
1941a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
1942a542ad1bSJan Schmidt  */
1943a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
1944a542ad1bSJan Schmidt 					struct btrfs_path *path)
1945a542ad1bSJan Schmidt {
1946a542ad1bSJan Schmidt 	struct inode_fs_paths *ifp;
1947a542ad1bSJan Schmidt 	struct btrfs_data_container *fspath;
1948a542ad1bSJan Schmidt 
1949a542ad1bSJan Schmidt 	fspath = init_data_container(total_bytes);
1950a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
1951a542ad1bSJan Schmidt 		return (void *)fspath;
1952a542ad1bSJan Schmidt 
1953a542ad1bSJan Schmidt 	ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
1954a542ad1bSJan Schmidt 	if (!ifp) {
1955a542ad1bSJan Schmidt 		kfree(fspath);
1956a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
1957a542ad1bSJan Schmidt 	}
1958a542ad1bSJan Schmidt 
1959a542ad1bSJan Schmidt 	ifp->btrfs_path = path;
1960a542ad1bSJan Schmidt 	ifp->fspath = fspath;
1961a542ad1bSJan Schmidt 	ifp->fs_root = fs_root;
1962a542ad1bSJan Schmidt 
1963a542ad1bSJan Schmidt 	return ifp;
1964a542ad1bSJan Schmidt }
1965a542ad1bSJan Schmidt 
1966a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath)
1967a542ad1bSJan Schmidt {
19684735fb28SJesper Juhl 	if (!ipath)
19694735fb28SJesper Juhl 		return;
1970425d17a2SLiu Bo 	vfree(ipath->fspath);
1971a542ad1bSJan Schmidt 	kfree(ipath);
1972a542ad1bSJan Schmidt }
1973