xref: /openbmc/linux/fs/btrfs/backref.c (revision da17066c40472c2d6a1aab7bb0090c3d285531c9)
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>
20afce772eSLu Fengqi #include <linux/rbtree.h>
21a542ad1bSJan Schmidt #include "ctree.h"
22a542ad1bSJan Schmidt #include "disk-io.h"
23a542ad1bSJan Schmidt #include "backref.h"
248da6d581SJan Schmidt #include "ulist.h"
258da6d581SJan Schmidt #include "transaction.h"
268da6d581SJan Schmidt #include "delayed-ref.h"
27b916a59aSJan Schmidt #include "locking.h"
28a542ad1bSJan Schmidt 
29dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */
30dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6
31dc046b10SJosef Bacik 
32976b1908SJan Schmidt struct extent_inode_elem {
33976b1908SJan Schmidt 	u64 inum;
34976b1908SJan Schmidt 	u64 offset;
35976b1908SJan Schmidt 	struct extent_inode_elem *next;
36976b1908SJan Schmidt };
37976b1908SJan Schmidt 
38afce772eSLu Fengqi /*
39afce772eSLu Fengqi  * ref_root is used as the root of the ref tree that hold a collection
40afce772eSLu Fengqi  * of unique references.
41afce772eSLu Fengqi  */
42afce772eSLu Fengqi struct ref_root {
43afce772eSLu Fengqi 	struct rb_root rb_root;
44afce772eSLu Fengqi 
45afce772eSLu Fengqi 	/*
46afce772eSLu Fengqi 	 * The unique_refs represents the number of ref_nodes with a positive
47afce772eSLu Fengqi 	 * count stored in the tree. Even if a ref_node (the count is greater
48afce772eSLu Fengqi 	 * than one) is added, the unique_refs will only increase by one.
49afce772eSLu Fengqi 	 */
50afce772eSLu Fengqi 	unsigned int unique_refs;
51afce772eSLu Fengqi };
52afce772eSLu Fengqi 
53afce772eSLu Fengqi /* ref_node is used to store a unique reference to the ref tree. */
54afce772eSLu Fengqi struct ref_node {
55afce772eSLu Fengqi 	struct rb_node rb_node;
56afce772eSLu Fengqi 
57afce772eSLu Fengqi 	/* For NORMAL_REF, otherwise all these fields should be set to 0 */
58afce772eSLu Fengqi 	u64 root_id;
59afce772eSLu Fengqi 	u64 object_id;
60afce772eSLu Fengqi 	u64 offset;
61afce772eSLu Fengqi 
62afce772eSLu Fengqi 	/* For SHARED_REF, otherwise parent field should be set to 0 */
63afce772eSLu Fengqi 	u64 parent;
64afce772eSLu Fengqi 
65afce772eSLu Fengqi 	/* Ref to the ref_mod of btrfs_delayed_ref_node */
66afce772eSLu Fengqi 	int ref_mod;
67afce772eSLu Fengqi };
68afce772eSLu Fengqi 
69afce772eSLu Fengqi /* Dynamically allocate and initialize a ref_root */
70afce772eSLu Fengqi static struct ref_root *ref_root_alloc(void)
71afce772eSLu Fengqi {
72afce772eSLu Fengqi 	struct ref_root *ref_tree;
73afce772eSLu Fengqi 
74afce772eSLu Fengqi 	ref_tree = kmalloc(sizeof(*ref_tree), GFP_NOFS);
75afce772eSLu Fengqi 	if (!ref_tree)
76afce772eSLu Fengqi 		return NULL;
77afce772eSLu Fengqi 
78afce772eSLu Fengqi 	ref_tree->rb_root = RB_ROOT;
79afce772eSLu Fengqi 	ref_tree->unique_refs = 0;
80afce772eSLu Fengqi 
81afce772eSLu Fengqi 	return ref_tree;
82afce772eSLu Fengqi }
83afce772eSLu Fengqi 
84afce772eSLu Fengqi /* Free all nodes in the ref tree, and reinit ref_root */
85afce772eSLu Fengqi static void ref_root_fini(struct ref_root *ref_tree)
86afce772eSLu Fengqi {
87afce772eSLu Fengqi 	struct ref_node *node;
88afce772eSLu Fengqi 	struct rb_node *next;
89afce772eSLu Fengqi 
90afce772eSLu Fengqi 	while ((next = rb_first(&ref_tree->rb_root)) != NULL) {
91afce772eSLu Fengqi 		node = rb_entry(next, struct ref_node, rb_node);
92afce772eSLu Fengqi 		rb_erase(next, &ref_tree->rb_root);
93afce772eSLu Fengqi 		kfree(node);
94afce772eSLu Fengqi 	}
95afce772eSLu Fengqi 
96afce772eSLu Fengqi 	ref_tree->rb_root = RB_ROOT;
97afce772eSLu Fengqi 	ref_tree->unique_refs = 0;
98afce772eSLu Fengqi }
99afce772eSLu Fengqi 
100afce772eSLu Fengqi static void ref_root_free(struct ref_root *ref_tree)
101afce772eSLu Fengqi {
102afce772eSLu Fengqi 	if (!ref_tree)
103afce772eSLu Fengqi 		return;
104afce772eSLu Fengqi 
105afce772eSLu Fengqi 	ref_root_fini(ref_tree);
106afce772eSLu Fengqi 	kfree(ref_tree);
107afce772eSLu Fengqi }
108afce772eSLu Fengqi 
109afce772eSLu Fengqi /*
110afce772eSLu Fengqi  * Compare ref_node with (root_id, object_id, offset, parent)
111afce772eSLu Fengqi  *
112afce772eSLu Fengqi  * The function compares two ref_node a and b. It returns an integer less
113afce772eSLu Fengqi  * than, equal to, or greater than zero , respectively, to be less than, to
114afce772eSLu Fengqi  * equal, or be greater than b.
115afce772eSLu Fengqi  */
116afce772eSLu Fengqi static int ref_node_cmp(struct ref_node *a, struct ref_node *b)
117afce772eSLu Fengqi {
118afce772eSLu Fengqi 	if (a->root_id < b->root_id)
119afce772eSLu Fengqi 		return -1;
120afce772eSLu Fengqi 	else if (a->root_id > b->root_id)
121afce772eSLu Fengqi 		return 1;
122afce772eSLu Fengqi 
123afce772eSLu Fengqi 	if (a->object_id < b->object_id)
124afce772eSLu Fengqi 		return -1;
125afce772eSLu Fengqi 	else if (a->object_id > b->object_id)
126afce772eSLu Fengqi 		return 1;
127afce772eSLu Fengqi 
128afce772eSLu Fengqi 	if (a->offset < b->offset)
129afce772eSLu Fengqi 		return -1;
130afce772eSLu Fengqi 	else if (a->offset > b->offset)
131afce772eSLu Fengqi 		return 1;
132afce772eSLu Fengqi 
133afce772eSLu Fengqi 	if (a->parent < b->parent)
134afce772eSLu Fengqi 		return -1;
135afce772eSLu Fengqi 	else if (a->parent > b->parent)
136afce772eSLu Fengqi 		return 1;
137afce772eSLu Fengqi 
138afce772eSLu Fengqi 	return 0;
139afce772eSLu Fengqi }
140afce772eSLu Fengqi 
141afce772eSLu Fengqi /*
142afce772eSLu Fengqi  * Search ref_node with (root_id, object_id, offset, parent) in the tree
143afce772eSLu Fengqi  *
144afce772eSLu Fengqi  * if found, the pointer of the ref_node will be returned;
145afce772eSLu Fengqi  * if not found, NULL will be returned and pos will point to the rb_node for
146afce772eSLu Fengqi  * insert, pos_parent will point to pos'parent for insert;
147afce772eSLu Fengqi */
148afce772eSLu Fengqi static struct ref_node *__ref_tree_search(struct ref_root *ref_tree,
149afce772eSLu Fengqi 					  struct rb_node ***pos,
150afce772eSLu Fengqi 					  struct rb_node **pos_parent,
151afce772eSLu Fengqi 					  u64 root_id, u64 object_id,
152afce772eSLu Fengqi 					  u64 offset, u64 parent)
153afce772eSLu Fengqi {
154afce772eSLu Fengqi 	struct ref_node *cur = NULL;
155afce772eSLu Fengqi 	struct ref_node entry;
156afce772eSLu Fengqi 	int ret;
157afce772eSLu Fengqi 
158afce772eSLu Fengqi 	entry.root_id = root_id;
159afce772eSLu Fengqi 	entry.object_id = object_id;
160afce772eSLu Fengqi 	entry.offset = offset;
161afce772eSLu Fengqi 	entry.parent = parent;
162afce772eSLu Fengqi 
163afce772eSLu Fengqi 	*pos = &ref_tree->rb_root.rb_node;
164afce772eSLu Fengqi 
165afce772eSLu Fengqi 	while (**pos) {
166afce772eSLu Fengqi 		*pos_parent = **pos;
167afce772eSLu Fengqi 		cur = rb_entry(*pos_parent, struct ref_node, rb_node);
168afce772eSLu Fengqi 
169afce772eSLu Fengqi 		ret = ref_node_cmp(cur, &entry);
170afce772eSLu Fengqi 		if (ret > 0)
171afce772eSLu Fengqi 			*pos = &(**pos)->rb_left;
172afce772eSLu Fengqi 		else if (ret < 0)
173afce772eSLu Fengqi 			*pos = &(**pos)->rb_right;
174afce772eSLu Fengqi 		else
175afce772eSLu Fengqi 			return cur;
176afce772eSLu Fengqi 	}
177afce772eSLu Fengqi 
178afce772eSLu Fengqi 	return NULL;
179afce772eSLu Fengqi }
180afce772eSLu Fengqi 
181afce772eSLu Fengqi /*
182afce772eSLu Fengqi  * Insert a ref_node to the ref tree
183afce772eSLu Fengqi  * @pos used for specifiy the position to insert
184afce772eSLu Fengqi  * @pos_parent for specifiy pos's parent
185afce772eSLu Fengqi  *
186afce772eSLu Fengqi  * success, return 0;
187afce772eSLu Fengqi  * ref_node already exists, return -EEXIST;
188afce772eSLu Fengqi */
189afce772eSLu Fengqi static int ref_tree_insert(struct ref_root *ref_tree, struct rb_node **pos,
190afce772eSLu Fengqi 			   struct rb_node *pos_parent, struct ref_node *ins)
191afce772eSLu Fengqi {
192afce772eSLu Fengqi 	struct rb_node **p = NULL;
193afce772eSLu Fengqi 	struct rb_node *parent = NULL;
194afce772eSLu Fengqi 	struct ref_node *cur = NULL;
195afce772eSLu Fengqi 
196afce772eSLu Fengqi 	if (!pos) {
197afce772eSLu Fengqi 		cur = __ref_tree_search(ref_tree, &p, &parent, ins->root_id,
198afce772eSLu Fengqi 					ins->object_id, ins->offset,
199afce772eSLu Fengqi 					ins->parent);
200afce772eSLu Fengqi 		if (cur)
201afce772eSLu Fengqi 			return -EEXIST;
202afce772eSLu Fengqi 	} else {
203afce772eSLu Fengqi 		p = pos;
204afce772eSLu Fengqi 		parent = pos_parent;
205afce772eSLu Fengqi 	}
206afce772eSLu Fengqi 
207afce772eSLu Fengqi 	rb_link_node(&ins->rb_node, parent, p);
208afce772eSLu Fengqi 	rb_insert_color(&ins->rb_node, &ref_tree->rb_root);
209afce772eSLu Fengqi 
210afce772eSLu Fengqi 	return 0;
211afce772eSLu Fengqi }
212afce772eSLu Fengqi 
213afce772eSLu Fengqi /* Erase and free ref_node, caller should update ref_root->unique_refs */
214afce772eSLu Fengqi static void ref_tree_remove(struct ref_root *ref_tree, struct ref_node *node)
215afce772eSLu Fengqi {
216afce772eSLu Fengqi 	rb_erase(&node->rb_node, &ref_tree->rb_root);
217afce772eSLu Fengqi 	kfree(node);
218afce772eSLu Fengqi }
219afce772eSLu Fengqi 
220afce772eSLu Fengqi /*
221afce772eSLu Fengqi  * Update ref_root->unique_refs
222afce772eSLu Fengqi  *
223afce772eSLu Fengqi  * Call __ref_tree_search
224afce772eSLu Fengqi  *	1. if ref_node doesn't exist, ref_tree_insert this node, and update
225afce772eSLu Fengqi  *	ref_root->unique_refs:
226afce772eSLu Fengqi  *		if ref_node->ref_mod > 0, ref_root->unique_refs++;
227afce772eSLu Fengqi  *		if ref_node->ref_mod < 0, do noting;
228afce772eSLu Fengqi  *
229afce772eSLu Fengqi  *	2. if ref_node is found, then get origin ref_node->ref_mod, and update
230afce772eSLu Fengqi  *	ref_node->ref_mod.
231afce772eSLu Fengqi  *		if ref_node->ref_mod is equal to 0,then call ref_tree_remove
232afce772eSLu Fengqi  *
233afce772eSLu Fengqi  *		according to origin_mod and new_mod, update ref_root->items
234afce772eSLu Fengqi  *		+----------------+--------------+-------------+
235afce772eSLu Fengqi  *		|		 |new_count <= 0|new_count > 0|
236afce772eSLu Fengqi  *		+----------------+--------------+-------------+
237afce772eSLu Fengqi  *		|origin_count < 0|       0      |      1      |
238afce772eSLu Fengqi  *		+----------------+--------------+-------------+
239afce772eSLu Fengqi  *		|origin_count > 0|      -1      |      0      |
240afce772eSLu Fengqi  *		+----------------+--------------+-------------+
241afce772eSLu Fengqi  *
242afce772eSLu Fengqi  * In case of allocation failure, -ENOMEM is returned and the ref_tree stays
243afce772eSLu Fengqi  * unaltered.
244afce772eSLu Fengqi  * Success, return 0
245afce772eSLu Fengqi  */
246afce772eSLu Fengqi static int ref_tree_add(struct ref_root *ref_tree, u64 root_id, u64 object_id,
247afce772eSLu Fengqi 			u64 offset, u64 parent, int count)
248afce772eSLu Fengqi {
249afce772eSLu Fengqi 	struct ref_node *node = NULL;
250afce772eSLu Fengqi 	struct rb_node **pos = NULL;
251afce772eSLu Fengqi 	struct rb_node *pos_parent = NULL;
252afce772eSLu Fengqi 	int origin_count;
253afce772eSLu Fengqi 	int ret;
254afce772eSLu Fengqi 
255afce772eSLu Fengqi 	if (!count)
256afce772eSLu Fengqi 		return 0;
257afce772eSLu Fengqi 
258afce772eSLu Fengqi 	node = __ref_tree_search(ref_tree, &pos, &pos_parent, root_id,
259afce772eSLu Fengqi 				 object_id, offset, parent);
260afce772eSLu Fengqi 	if (node == NULL) {
261afce772eSLu Fengqi 		node = kmalloc(sizeof(*node), GFP_NOFS);
262afce772eSLu Fengqi 		if (!node)
263afce772eSLu Fengqi 			return -ENOMEM;
264afce772eSLu Fengqi 
265afce772eSLu Fengqi 		node->root_id = root_id;
266afce772eSLu Fengqi 		node->object_id = object_id;
267afce772eSLu Fengqi 		node->offset = offset;
268afce772eSLu Fengqi 		node->parent = parent;
269afce772eSLu Fengqi 		node->ref_mod = count;
270afce772eSLu Fengqi 
271afce772eSLu Fengqi 		ret = ref_tree_insert(ref_tree, pos, pos_parent, node);
272afce772eSLu Fengqi 		ASSERT(!ret);
273afce772eSLu Fengqi 		if (ret) {
274afce772eSLu Fengqi 			kfree(node);
275afce772eSLu Fengqi 			return ret;
276afce772eSLu Fengqi 		}
277afce772eSLu Fengqi 
278afce772eSLu Fengqi 		ref_tree->unique_refs += node->ref_mod > 0 ? 1 : 0;
279afce772eSLu Fengqi 
280afce772eSLu Fengqi 		return 0;
281afce772eSLu Fengqi 	}
282afce772eSLu Fengqi 
283afce772eSLu Fengqi 	origin_count = node->ref_mod;
284afce772eSLu Fengqi 	node->ref_mod += count;
285afce772eSLu Fengqi 
286afce772eSLu Fengqi 	if (node->ref_mod > 0)
287afce772eSLu Fengqi 		ref_tree->unique_refs += origin_count > 0 ? 0 : 1;
288afce772eSLu Fengqi 	else if (node->ref_mod <= 0)
289afce772eSLu Fengqi 		ref_tree->unique_refs += origin_count > 0 ? -1 : 0;
290afce772eSLu Fengqi 
291afce772eSLu Fengqi 	if (!node->ref_mod)
292afce772eSLu Fengqi 		ref_tree_remove(ref_tree, node);
293afce772eSLu Fengqi 
294afce772eSLu Fengqi 	return 0;
295afce772eSLu Fengqi }
296afce772eSLu Fengqi 
297976b1908SJan Schmidt static int check_extent_in_eb(struct btrfs_key *key, struct extent_buffer *eb,
298976b1908SJan Schmidt 				struct btrfs_file_extent_item *fi,
299976b1908SJan Schmidt 				u64 extent_item_pos,
300976b1908SJan Schmidt 				struct extent_inode_elem **eie)
301976b1908SJan Schmidt {
3028ca15e05SJosef Bacik 	u64 offset = 0;
3038ca15e05SJosef Bacik 	struct extent_inode_elem *e;
3048ca15e05SJosef Bacik 
3058ca15e05SJosef Bacik 	if (!btrfs_file_extent_compression(eb, fi) &&
3068ca15e05SJosef Bacik 	    !btrfs_file_extent_encryption(eb, fi) &&
3078ca15e05SJosef Bacik 	    !btrfs_file_extent_other_encoding(eb, fi)) {
308976b1908SJan Schmidt 		u64 data_offset;
309976b1908SJan Schmidt 		u64 data_len;
310976b1908SJan Schmidt 
311976b1908SJan Schmidt 		data_offset = btrfs_file_extent_offset(eb, fi);
312976b1908SJan Schmidt 		data_len = btrfs_file_extent_num_bytes(eb, fi);
313976b1908SJan Schmidt 
314976b1908SJan Schmidt 		if (extent_item_pos < data_offset ||
315976b1908SJan Schmidt 		    extent_item_pos >= data_offset + data_len)
316976b1908SJan Schmidt 			return 1;
3178ca15e05SJosef Bacik 		offset = extent_item_pos - data_offset;
3188ca15e05SJosef Bacik 	}
319976b1908SJan Schmidt 
320976b1908SJan Schmidt 	e = kmalloc(sizeof(*e), GFP_NOFS);
321976b1908SJan Schmidt 	if (!e)
322976b1908SJan Schmidt 		return -ENOMEM;
323976b1908SJan Schmidt 
324976b1908SJan Schmidt 	e->next = *eie;
325976b1908SJan Schmidt 	e->inum = key->objectid;
3268ca15e05SJosef Bacik 	e->offset = key->offset + offset;
327976b1908SJan Schmidt 	*eie = e;
328976b1908SJan Schmidt 
329976b1908SJan Schmidt 	return 0;
330976b1908SJan Schmidt }
331976b1908SJan Schmidt 
332f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie)
333f05c4746SWang Shilong {
334f05c4746SWang Shilong 	struct extent_inode_elem *eie_next;
335f05c4746SWang Shilong 
336f05c4746SWang Shilong 	for (; eie; eie = eie_next) {
337f05c4746SWang Shilong 		eie_next = eie->next;
338f05c4746SWang Shilong 		kfree(eie);
339f05c4746SWang Shilong 	}
340f05c4746SWang Shilong }
341f05c4746SWang Shilong 
342976b1908SJan Schmidt static int find_extent_in_eb(struct extent_buffer *eb, u64 wanted_disk_byte,
343976b1908SJan Schmidt 				u64 extent_item_pos,
344976b1908SJan Schmidt 				struct extent_inode_elem **eie)
345976b1908SJan Schmidt {
346976b1908SJan Schmidt 	u64 disk_byte;
347976b1908SJan Schmidt 	struct btrfs_key key;
348976b1908SJan Schmidt 	struct btrfs_file_extent_item *fi;
349976b1908SJan Schmidt 	int slot;
350976b1908SJan Schmidt 	int nritems;
351976b1908SJan Schmidt 	int extent_type;
352976b1908SJan Schmidt 	int ret;
353976b1908SJan Schmidt 
354976b1908SJan Schmidt 	/*
355976b1908SJan Schmidt 	 * from the shared data ref, we only have the leaf but we need
356976b1908SJan Schmidt 	 * the key. thus, we must look into all items and see that we
357976b1908SJan Schmidt 	 * find one (some) with a reference to our extent item.
358976b1908SJan Schmidt 	 */
359976b1908SJan Schmidt 	nritems = btrfs_header_nritems(eb);
360976b1908SJan Schmidt 	for (slot = 0; slot < nritems; ++slot) {
361976b1908SJan Schmidt 		btrfs_item_key_to_cpu(eb, &key, slot);
362976b1908SJan Schmidt 		if (key.type != BTRFS_EXTENT_DATA_KEY)
363976b1908SJan Schmidt 			continue;
364976b1908SJan Schmidt 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
365976b1908SJan Schmidt 		extent_type = btrfs_file_extent_type(eb, fi);
366976b1908SJan Schmidt 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
367976b1908SJan Schmidt 			continue;
368976b1908SJan Schmidt 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
369976b1908SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
370976b1908SJan Schmidt 		if (disk_byte != wanted_disk_byte)
371976b1908SJan Schmidt 			continue;
372976b1908SJan Schmidt 
373976b1908SJan Schmidt 		ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
374976b1908SJan Schmidt 		if (ret < 0)
375976b1908SJan Schmidt 			return ret;
376976b1908SJan Schmidt 	}
377976b1908SJan Schmidt 
378976b1908SJan Schmidt 	return 0;
379976b1908SJan Schmidt }
380976b1908SJan Schmidt 
3818da6d581SJan Schmidt /*
3828da6d581SJan Schmidt  * this structure records all encountered refs on the way up to the root
3838da6d581SJan Schmidt  */
3848da6d581SJan Schmidt struct __prelim_ref {
3858da6d581SJan Schmidt 	struct list_head list;
3868da6d581SJan Schmidt 	u64 root_id;
387d5c88b73SJan Schmidt 	struct btrfs_key key_for_search;
3888da6d581SJan Schmidt 	int level;
3898da6d581SJan Schmidt 	int count;
3903301958bSJan Schmidt 	struct extent_inode_elem *inode_list;
3918da6d581SJan Schmidt 	u64 parent;
3928da6d581SJan Schmidt 	u64 wanted_disk_byte;
3938da6d581SJan Schmidt };
3948da6d581SJan Schmidt 
395b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache;
396b9e9a6cbSWang Shilong 
397b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void)
398b9e9a6cbSWang Shilong {
399b9e9a6cbSWang Shilong 	btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
400b9e9a6cbSWang Shilong 					sizeof(struct __prelim_ref),
401b9e9a6cbSWang Shilong 					0,
402fba4b697SNikolay Borisov 					SLAB_MEM_SPREAD,
403b9e9a6cbSWang Shilong 					NULL);
404b9e9a6cbSWang Shilong 	if (!btrfs_prelim_ref_cache)
405b9e9a6cbSWang Shilong 		return -ENOMEM;
406b9e9a6cbSWang Shilong 	return 0;
407b9e9a6cbSWang Shilong }
408b9e9a6cbSWang Shilong 
409b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void)
410b9e9a6cbSWang Shilong {
411b9e9a6cbSWang Shilong 	kmem_cache_destroy(btrfs_prelim_ref_cache);
412b9e9a6cbSWang Shilong }
413b9e9a6cbSWang Shilong 
414d5c88b73SJan Schmidt /*
415d5c88b73SJan Schmidt  * the rules for all callers of this function are:
416d5c88b73SJan Schmidt  * - obtaining the parent is the goal
417d5c88b73SJan Schmidt  * - if you add a key, you must know that it is a correct key
418d5c88b73SJan Schmidt  * - if you cannot add the parent or a correct key, then we will look into the
419d5c88b73SJan Schmidt  *   block later to set a correct key
420d5c88b73SJan Schmidt  *
421d5c88b73SJan Schmidt  * delayed refs
422d5c88b73SJan Schmidt  * ============
423d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
424d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
425d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
426d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    -   |     -
427d5c88b73SJan Schmidt  *      key to resolve |    -   |     y    |    y   |     y
428d5c88b73SJan Schmidt  *  tree block logical |    -   |     -    |    -   |     -
429d5c88b73SJan Schmidt  *  root for resolving |    y   |     y    |    y   |     y
430d5c88b73SJan Schmidt  *
431d5c88b73SJan Schmidt  * - column 1:       we've the parent -> done
432d5c88b73SJan Schmidt  * - column 2, 3, 4: we use the key to find the parent
433d5c88b73SJan Schmidt  *
434d5c88b73SJan Schmidt  * on disk refs (inline or keyed)
435d5c88b73SJan Schmidt  * ==============================
436d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
437d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
438d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
439d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    y   |     -
440d5c88b73SJan Schmidt  *      key to resolve |    -   |     -    |    -   |     y
441d5c88b73SJan Schmidt  *  tree block logical |    y   |     y    |    y   |     y
442d5c88b73SJan Schmidt  *  root for resolving |    -   |     y    |    y   |     y
443d5c88b73SJan Schmidt  *
444d5c88b73SJan Schmidt  * - column 1, 3: we've the parent -> done
445d5c88b73SJan Schmidt  * - column 2:    we take the first key from the block to find the parent
446d5c88b73SJan Schmidt  *                (see __add_missing_keys)
447d5c88b73SJan Schmidt  * - column 4:    we use the key to find the parent
448d5c88b73SJan Schmidt  *
449d5c88b73SJan Schmidt  * additional information that's available but not required to find the parent
450d5c88b73SJan Schmidt  * block might help in merging entries to gain some speed.
451d5c88b73SJan Schmidt  */
452d5c88b73SJan Schmidt 
4538da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id,
454d5c88b73SJan Schmidt 			    struct btrfs_key *key, int level,
455742916b8SWang Shilong 			    u64 parent, u64 wanted_disk_byte, int count,
456742916b8SWang Shilong 			    gfp_t gfp_mask)
4578da6d581SJan Schmidt {
4588da6d581SJan Schmidt 	struct __prelim_ref *ref;
4598da6d581SJan Schmidt 
46048ec4736SLiu Bo 	if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
46148ec4736SLiu Bo 		return 0;
46248ec4736SLiu Bo 
463b9e9a6cbSWang Shilong 	ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
4648da6d581SJan Schmidt 	if (!ref)
4658da6d581SJan Schmidt 		return -ENOMEM;
4668da6d581SJan Schmidt 
4678da6d581SJan Schmidt 	ref->root_id = root_id;
468d6589101SFilipe Manana 	if (key) {
469d5c88b73SJan Schmidt 		ref->key_for_search = *key;
470d6589101SFilipe Manana 		/*
471d6589101SFilipe Manana 		 * We can often find data backrefs with an offset that is too
472d6589101SFilipe Manana 		 * large (>= LLONG_MAX, maximum allowed file offset) due to
473d6589101SFilipe Manana 		 * underflows when subtracting a file's offset with the data
474d6589101SFilipe Manana 		 * offset of its corresponding extent data item. This can
475d6589101SFilipe Manana 		 * happen for example in the clone ioctl.
476d6589101SFilipe Manana 		 * So if we detect such case we set the search key's offset to
477d6589101SFilipe Manana 		 * zero to make sure we will find the matching file extent item
478d6589101SFilipe Manana 		 * at add_all_parents(), otherwise we will miss it because the
479d6589101SFilipe Manana 		 * offset taken form the backref is much larger then the offset
480d6589101SFilipe Manana 		 * of the file extent item. This can make us scan a very large
481d6589101SFilipe Manana 		 * number of file extent items, but at least it will not make
482d6589101SFilipe Manana 		 * us miss any.
483d6589101SFilipe Manana 		 * This is an ugly workaround for a behaviour that should have
484d6589101SFilipe Manana 		 * never existed, but it does and a fix for the clone ioctl
485d6589101SFilipe Manana 		 * would touch a lot of places, cause backwards incompatibility
486d6589101SFilipe Manana 		 * and would not fix the problem for extents cloned with older
487d6589101SFilipe Manana 		 * kernels.
488d6589101SFilipe Manana 		 */
489d6589101SFilipe Manana 		if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
490d6589101SFilipe Manana 		    ref->key_for_search.offset >= LLONG_MAX)
491d6589101SFilipe Manana 			ref->key_for_search.offset = 0;
492d6589101SFilipe Manana 	} else {
493d5c88b73SJan Schmidt 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
494d6589101SFilipe Manana 	}
4958da6d581SJan Schmidt 
4963301958bSJan Schmidt 	ref->inode_list = NULL;
4978da6d581SJan Schmidt 	ref->level = level;
4988da6d581SJan Schmidt 	ref->count = count;
4998da6d581SJan Schmidt 	ref->parent = parent;
5008da6d581SJan Schmidt 	ref->wanted_disk_byte = wanted_disk_byte;
5018da6d581SJan Schmidt 	list_add_tail(&ref->list, head);
5028da6d581SJan Schmidt 
5038da6d581SJan Schmidt 	return 0;
5048da6d581SJan Schmidt }
5058da6d581SJan Schmidt 
5068da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
5077ef81ac8SJosef Bacik 			   struct ulist *parents, struct __prelim_ref *ref,
50844853868SJosef Bacik 			   int level, u64 time_seq, const u64 *extent_item_pos,
50944853868SJosef Bacik 			   u64 total_refs)
5108da6d581SJan Schmidt {
51169bca40dSAlexander Block 	int ret = 0;
51269bca40dSAlexander Block 	int slot;
51369bca40dSAlexander Block 	struct extent_buffer *eb;
51469bca40dSAlexander Block 	struct btrfs_key key;
5157ef81ac8SJosef Bacik 	struct btrfs_key *key_for_search = &ref->key_for_search;
5168da6d581SJan Schmidt 	struct btrfs_file_extent_item *fi;
517ed8c4913SJosef Bacik 	struct extent_inode_elem *eie = NULL, *old = NULL;
5188da6d581SJan Schmidt 	u64 disk_byte;
5197ef81ac8SJosef Bacik 	u64 wanted_disk_byte = ref->wanted_disk_byte;
5207ef81ac8SJosef Bacik 	u64 count = 0;
5218da6d581SJan Schmidt 
52269bca40dSAlexander Block 	if (level != 0) {
52369bca40dSAlexander Block 		eb = path->nodes[level];
52469bca40dSAlexander Block 		ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
5253301958bSJan Schmidt 		if (ret < 0)
5263301958bSJan Schmidt 			return ret;
5278da6d581SJan Schmidt 		return 0;
52869bca40dSAlexander Block 	}
5298da6d581SJan Schmidt 
5308da6d581SJan Schmidt 	/*
53169bca40dSAlexander Block 	 * We normally enter this function with the path already pointing to
53269bca40dSAlexander Block 	 * the first item to check. But sometimes, we may enter it with
53369bca40dSAlexander Block 	 * slot==nritems. In that case, go to the next leaf before we continue.
5348da6d581SJan Schmidt 	 */
53521633fc6SQu Wenruo 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
53621633fc6SQu Wenruo 		if (time_seq == (u64)-1)
53721633fc6SQu Wenruo 			ret = btrfs_next_leaf(root, path);
53821633fc6SQu Wenruo 		else
5393d7806ecSJan Schmidt 			ret = btrfs_next_old_leaf(root, path, time_seq);
54021633fc6SQu Wenruo 	}
5418da6d581SJan Schmidt 
54244853868SJosef Bacik 	while (!ret && count < total_refs) {
5438da6d581SJan Schmidt 		eb = path->nodes[0];
54469bca40dSAlexander Block 		slot = path->slots[0];
54569bca40dSAlexander Block 
54669bca40dSAlexander Block 		btrfs_item_key_to_cpu(eb, &key, slot);
54769bca40dSAlexander Block 
54869bca40dSAlexander Block 		if (key.objectid != key_for_search->objectid ||
54969bca40dSAlexander Block 		    key.type != BTRFS_EXTENT_DATA_KEY)
55069bca40dSAlexander Block 			break;
55169bca40dSAlexander Block 
55269bca40dSAlexander Block 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5538da6d581SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
55469bca40dSAlexander Block 
55569bca40dSAlexander Block 		if (disk_byte == wanted_disk_byte) {
55669bca40dSAlexander Block 			eie = NULL;
557ed8c4913SJosef Bacik 			old = NULL;
5587ef81ac8SJosef Bacik 			count++;
55969bca40dSAlexander Block 			if (extent_item_pos) {
56069bca40dSAlexander Block 				ret = check_extent_in_eb(&key, eb, fi,
56169bca40dSAlexander Block 						*extent_item_pos,
56269bca40dSAlexander Block 						&eie);
56369bca40dSAlexander Block 				if (ret < 0)
56469bca40dSAlexander Block 					break;
5658da6d581SJan Schmidt 			}
566ed8c4913SJosef Bacik 			if (ret > 0)
567ed8c4913SJosef Bacik 				goto next;
5684eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(parents, eb->start,
5694eb1f66dSTakashi Iwai 						  eie, (void **)&old, GFP_NOFS);
57069bca40dSAlexander Block 			if (ret < 0)
57169bca40dSAlexander Block 				break;
572ed8c4913SJosef Bacik 			if (!ret && extent_item_pos) {
573ed8c4913SJosef Bacik 				while (old->next)
574ed8c4913SJosef Bacik 					old = old->next;
575ed8c4913SJosef Bacik 				old->next = eie;
57669bca40dSAlexander Block 			}
577f05c4746SWang Shilong 			eie = NULL;
57869bca40dSAlexander Block 		}
579ed8c4913SJosef Bacik next:
58021633fc6SQu Wenruo 		if (time_seq == (u64)-1)
58121633fc6SQu Wenruo 			ret = btrfs_next_item(root, path);
58221633fc6SQu Wenruo 		else
58369bca40dSAlexander Block 			ret = btrfs_next_old_item(root, path, time_seq);
5848da6d581SJan Schmidt 	}
5858da6d581SJan Schmidt 
58669bca40dSAlexander Block 	if (ret > 0)
58769bca40dSAlexander Block 		ret = 0;
588f05c4746SWang Shilong 	else if (ret < 0)
589f05c4746SWang Shilong 		free_inode_elem_list(eie);
59069bca40dSAlexander Block 	return ret;
5918da6d581SJan Schmidt }
5928da6d581SJan Schmidt 
5938da6d581SJan Schmidt /*
5948da6d581SJan Schmidt  * resolve an indirect backref in the form (root_id, key, level)
5958da6d581SJan Schmidt  * to a logical address
5968da6d581SJan Schmidt  */
5978da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
598da61d31aSJosef Bacik 				  struct btrfs_path *path, u64 time_seq,
5998da6d581SJan Schmidt 				  struct __prelim_ref *ref,
600976b1908SJan Schmidt 				  struct ulist *parents,
60144853868SJosef Bacik 				  const u64 *extent_item_pos, u64 total_refs)
6028da6d581SJan Schmidt {
6038da6d581SJan Schmidt 	struct btrfs_root *root;
6048da6d581SJan Schmidt 	struct btrfs_key root_key;
6058da6d581SJan Schmidt 	struct extent_buffer *eb;
6068da6d581SJan Schmidt 	int ret = 0;
6078da6d581SJan Schmidt 	int root_level;
6088da6d581SJan Schmidt 	int level = ref->level;
609538f72cdSWang Shilong 	int index;
6108da6d581SJan Schmidt 
6118da6d581SJan Schmidt 	root_key.objectid = ref->root_id;
6128da6d581SJan Schmidt 	root_key.type = BTRFS_ROOT_ITEM_KEY;
6138da6d581SJan Schmidt 	root_key.offset = (u64)-1;
614538f72cdSWang Shilong 
615538f72cdSWang Shilong 	index = srcu_read_lock(&fs_info->subvol_srcu);
616538f72cdSWang Shilong 
6172d9e9776SJosef Bacik 	root = btrfs_get_fs_root(fs_info, &root_key, false);
6188da6d581SJan Schmidt 	if (IS_ERR(root)) {
619538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
6208da6d581SJan Schmidt 		ret = PTR_ERR(root);
6218da6d581SJan Schmidt 		goto out;
6228da6d581SJan Schmidt 	}
6238da6d581SJan Schmidt 
624f5ee5c9aSJeff Mahoney 	if (btrfs_is_testing(fs_info)) {
625d9ee522bSJosef Bacik 		srcu_read_unlock(&fs_info->subvol_srcu, index);
626d9ee522bSJosef Bacik 		ret = -ENOENT;
627d9ee522bSJosef Bacik 		goto out;
628d9ee522bSJosef Bacik 	}
629d9ee522bSJosef Bacik 
6309e351cc8SJosef Bacik 	if (path->search_commit_root)
6319e351cc8SJosef Bacik 		root_level = btrfs_header_level(root->commit_root);
63221633fc6SQu Wenruo 	else if (time_seq == (u64)-1)
63321633fc6SQu Wenruo 		root_level = btrfs_header_level(root->node);
6349e351cc8SJosef Bacik 	else
6355b6602e7SJan Schmidt 		root_level = btrfs_old_root_level(root, time_seq);
6368da6d581SJan Schmidt 
637538f72cdSWang Shilong 	if (root_level + 1 == level) {
638538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
6398da6d581SJan Schmidt 		goto out;
640538f72cdSWang Shilong 	}
6418da6d581SJan Schmidt 
6428da6d581SJan Schmidt 	path->lowest_level = level;
64321633fc6SQu Wenruo 	if (time_seq == (u64)-1)
64421633fc6SQu Wenruo 		ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
64521633fc6SQu Wenruo 					0, 0);
64621633fc6SQu Wenruo 	else
64721633fc6SQu Wenruo 		ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
64821633fc6SQu Wenruo 					    time_seq);
649538f72cdSWang Shilong 
650538f72cdSWang Shilong 	/* root node has been locked, we can release @subvol_srcu safely here */
651538f72cdSWang Shilong 	srcu_read_unlock(&fs_info->subvol_srcu, index);
652538f72cdSWang Shilong 
653ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info,
654ab8d0fc4SJeff Mahoney 		"search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
655c1c9ff7cSGeert Uytterhoeven 		 ref->root_id, level, ref->count, ret,
656c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.objectid, ref->key_for_search.type,
657c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.offset);
6588da6d581SJan Schmidt 	if (ret < 0)
6598da6d581SJan Schmidt 		goto out;
6608da6d581SJan Schmidt 
6618da6d581SJan Schmidt 	eb = path->nodes[level];
6629345457fSJan Schmidt 	while (!eb) {
663fae7f21cSDulshani Gunawardhana 		if (WARN_ON(!level)) {
6648da6d581SJan Schmidt 			ret = 1;
6658da6d581SJan Schmidt 			goto out;
6668da6d581SJan Schmidt 		}
6679345457fSJan Schmidt 		level--;
6689345457fSJan Schmidt 		eb = path->nodes[level];
6699345457fSJan Schmidt 	}
6708da6d581SJan Schmidt 
6717ef81ac8SJosef Bacik 	ret = add_all_parents(root, path, parents, ref, level, time_seq,
67244853868SJosef Bacik 			      extent_item_pos, total_refs);
6738da6d581SJan Schmidt out:
674da61d31aSJosef Bacik 	path->lowest_level = 0;
675da61d31aSJosef Bacik 	btrfs_release_path(path);
6768da6d581SJan Schmidt 	return ret;
6778da6d581SJan Schmidt }
6788da6d581SJan Schmidt 
6798da6d581SJan Schmidt /*
6808da6d581SJan Schmidt  * resolve all indirect backrefs from the list
6818da6d581SJan Schmidt  */
6828da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
683da61d31aSJosef Bacik 				   struct btrfs_path *path, u64 time_seq,
684976b1908SJan Schmidt 				   struct list_head *head,
685dc046b10SJosef Bacik 				   const u64 *extent_item_pos, u64 total_refs,
686dc046b10SJosef Bacik 				   u64 root_objectid)
6878da6d581SJan Schmidt {
6888da6d581SJan Schmidt 	int err;
6898da6d581SJan Schmidt 	int ret = 0;
6908da6d581SJan Schmidt 	struct __prelim_ref *ref;
6918da6d581SJan Schmidt 	struct __prelim_ref *ref_safe;
6928da6d581SJan Schmidt 	struct __prelim_ref *new_ref;
6938da6d581SJan Schmidt 	struct ulist *parents;
6948da6d581SJan Schmidt 	struct ulist_node *node;
695cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
6968da6d581SJan Schmidt 
6978da6d581SJan Schmidt 	parents = ulist_alloc(GFP_NOFS);
6988da6d581SJan Schmidt 	if (!parents)
6998da6d581SJan Schmidt 		return -ENOMEM;
7008da6d581SJan Schmidt 
7018da6d581SJan Schmidt 	/*
7028da6d581SJan Schmidt 	 * _safe allows us to insert directly after the current item without
7038da6d581SJan Schmidt 	 * iterating over the newly inserted items.
7048da6d581SJan Schmidt 	 * we're also allowed to re-assign ref during iteration.
7058da6d581SJan Schmidt 	 */
7068da6d581SJan Schmidt 	list_for_each_entry_safe(ref, ref_safe, head, list) {
7078da6d581SJan Schmidt 		if (ref->parent)	/* already direct */
7088da6d581SJan Schmidt 			continue;
7098da6d581SJan Schmidt 		if (ref->count == 0)
7108da6d581SJan Schmidt 			continue;
711dc046b10SJosef Bacik 		if (root_objectid && ref->root_id != root_objectid) {
712dc046b10SJosef Bacik 			ret = BACKREF_FOUND_SHARED;
713dc046b10SJosef Bacik 			goto out;
714dc046b10SJosef Bacik 		}
715da61d31aSJosef Bacik 		err = __resolve_indirect_ref(fs_info, path, time_seq, ref,
71644853868SJosef Bacik 					     parents, extent_item_pos,
71744853868SJosef Bacik 					     total_refs);
71895def2edSWang Shilong 		/*
71995def2edSWang Shilong 		 * we can only tolerate ENOENT,otherwise,we should catch error
72095def2edSWang Shilong 		 * and return directly.
72195def2edSWang Shilong 		 */
72295def2edSWang Shilong 		if (err == -ENOENT) {
7238da6d581SJan Schmidt 			continue;
72495def2edSWang Shilong 		} else if (err) {
72595def2edSWang Shilong 			ret = err;
72695def2edSWang Shilong 			goto out;
72795def2edSWang Shilong 		}
7288da6d581SJan Schmidt 
7298da6d581SJan Schmidt 		/* we put the first parent into the ref at hand */
730cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&uiter);
731cd1b413cSJan Schmidt 		node = ulist_next(parents, &uiter);
7328da6d581SJan Schmidt 		ref->parent = node ? node->val : 0;
733995e01b7SJan Schmidt 		ref->inode_list = node ?
73435a3621bSStefan Behrens 			(struct extent_inode_elem *)(uintptr_t)node->aux : NULL;
7358da6d581SJan Schmidt 
7368da6d581SJan Schmidt 		/* additional parents require new refs being added here */
737cd1b413cSJan Schmidt 		while ((node = ulist_next(parents, &uiter))) {
738b9e9a6cbSWang Shilong 			new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
739b9e9a6cbSWang Shilong 						   GFP_NOFS);
7408da6d581SJan Schmidt 			if (!new_ref) {
7418da6d581SJan Schmidt 				ret = -ENOMEM;
742e36902d4SWang Shilong 				goto out;
7438da6d581SJan Schmidt 			}
7448da6d581SJan Schmidt 			memcpy(new_ref, ref, sizeof(*ref));
7458da6d581SJan Schmidt 			new_ref->parent = node->val;
746995e01b7SJan Schmidt 			new_ref->inode_list = (struct extent_inode_elem *)
747995e01b7SJan Schmidt 							(uintptr_t)node->aux;
7488da6d581SJan Schmidt 			list_add(&new_ref->list, &ref->list);
7498da6d581SJan Schmidt 		}
7508da6d581SJan Schmidt 		ulist_reinit(parents);
7518da6d581SJan Schmidt 	}
752e36902d4SWang Shilong out:
7538da6d581SJan Schmidt 	ulist_free(parents);
7548da6d581SJan Schmidt 	return ret;
7558da6d581SJan Schmidt }
7568da6d581SJan Schmidt 
757d5c88b73SJan Schmidt static inline int ref_for_same_block(struct __prelim_ref *ref1,
758d5c88b73SJan Schmidt 				     struct __prelim_ref *ref2)
759d5c88b73SJan Schmidt {
760d5c88b73SJan Schmidt 	if (ref1->level != ref2->level)
761d5c88b73SJan Schmidt 		return 0;
762d5c88b73SJan Schmidt 	if (ref1->root_id != ref2->root_id)
763d5c88b73SJan Schmidt 		return 0;
764d5c88b73SJan Schmidt 	if (ref1->key_for_search.type != ref2->key_for_search.type)
765d5c88b73SJan Schmidt 		return 0;
766d5c88b73SJan Schmidt 	if (ref1->key_for_search.objectid != ref2->key_for_search.objectid)
767d5c88b73SJan Schmidt 		return 0;
768d5c88b73SJan Schmidt 	if (ref1->key_for_search.offset != ref2->key_for_search.offset)
769d5c88b73SJan Schmidt 		return 0;
770d5c88b73SJan Schmidt 	if (ref1->parent != ref2->parent)
771d5c88b73SJan Schmidt 		return 0;
772d5c88b73SJan Schmidt 
773d5c88b73SJan Schmidt 	return 1;
774d5c88b73SJan Schmidt }
775d5c88b73SJan Schmidt 
776d5c88b73SJan Schmidt /*
777d5c88b73SJan Schmidt  * read tree blocks and add keys where required.
778d5c88b73SJan Schmidt  */
779d5c88b73SJan Schmidt static int __add_missing_keys(struct btrfs_fs_info *fs_info,
780d5c88b73SJan Schmidt 			      struct list_head *head)
781d5c88b73SJan Schmidt {
782a7ca4225SGeliang Tang 	struct __prelim_ref *ref;
783d5c88b73SJan Schmidt 	struct extent_buffer *eb;
784d5c88b73SJan Schmidt 
785a7ca4225SGeliang Tang 	list_for_each_entry(ref, head, list) {
786d5c88b73SJan Schmidt 		if (ref->parent)
787d5c88b73SJan Schmidt 			continue;
788d5c88b73SJan Schmidt 		if (ref->key_for_search.type)
789d5c88b73SJan Schmidt 			continue;
790d5c88b73SJan Schmidt 		BUG_ON(!ref->wanted_disk_byte);
791d5c88b73SJan Schmidt 		eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte,
792ce86cd59SDavid Sterba 				     0);
79364c043deSLiu Bo 		if (IS_ERR(eb)) {
79464c043deSLiu Bo 			return PTR_ERR(eb);
79564c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
796416bc658SJosef Bacik 			free_extent_buffer(eb);
797416bc658SJosef Bacik 			return -EIO;
798416bc658SJosef Bacik 		}
799d5c88b73SJan Schmidt 		btrfs_tree_read_lock(eb);
800d5c88b73SJan Schmidt 		if (btrfs_header_level(eb) == 0)
801d5c88b73SJan Schmidt 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
802d5c88b73SJan Schmidt 		else
803d5c88b73SJan Schmidt 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
804d5c88b73SJan Schmidt 		btrfs_tree_read_unlock(eb);
805d5c88b73SJan Schmidt 		free_extent_buffer(eb);
806d5c88b73SJan Schmidt 	}
807d5c88b73SJan Schmidt 	return 0;
808d5c88b73SJan Schmidt }
809d5c88b73SJan Schmidt 
8108da6d581SJan Schmidt /*
81100db646dSQu Wenruo  * merge backrefs and adjust counts accordingly
8128da6d581SJan Schmidt  *
8138da6d581SJan Schmidt  * mode = 1: merge identical keys, if key is set
814d5c88b73SJan Schmidt  *    FIXME: if we add more keys in __add_prelim_ref, we can merge more here.
815d5c88b73SJan Schmidt  *           additionally, we could even add a key range for the blocks we
816d5c88b73SJan Schmidt  *           looked into to merge even more (-> replace unresolved refs by those
817d5c88b73SJan Schmidt  *           having a parent).
8188da6d581SJan Schmidt  * mode = 2: merge identical parents
8198da6d581SJan Schmidt  */
820692206b1SWang Shilong static void __merge_refs(struct list_head *head, int mode)
8218da6d581SJan Schmidt {
8228e217858SGeliang Tang 	struct __prelim_ref *pos1;
8238da6d581SJan Schmidt 
8248e217858SGeliang Tang 	list_for_each_entry(pos1, head, list) {
8258e217858SGeliang Tang 		struct __prelim_ref *pos2 = pos1, *tmp;
8268da6d581SJan Schmidt 
8278e217858SGeliang Tang 		list_for_each_entry_safe_continue(pos2, tmp, head, list) {
8288f682f69SDave Jones 			struct __prelim_ref *ref1 = pos1, *ref2 = pos2;
8293ef5969cSAlexander Block 			struct extent_inode_elem *eie;
8308da6d581SJan Schmidt 
831d5c88b73SJan Schmidt 			if (!ref_for_same_block(ref1, ref2))
8328da6d581SJan Schmidt 				continue;
83300db646dSQu Wenruo 			if (mode == 1) {
8348f682f69SDave Jones 				if (!ref1->parent && ref2->parent)
8358f682f69SDave Jones 					swap(ref1, ref2);
8368da6d581SJan Schmidt 			} else {
8378da6d581SJan Schmidt 				if (ref1->parent != ref2->parent)
8388da6d581SJan Schmidt 					continue;
8398da6d581SJan Schmidt 			}
8403ef5969cSAlexander Block 
8413ef5969cSAlexander Block 			eie = ref1->inode_list;
8423ef5969cSAlexander Block 			while (eie && eie->next)
8433ef5969cSAlexander Block 				eie = eie->next;
8443ef5969cSAlexander Block 			if (eie)
8453ef5969cSAlexander Block 				eie->next = ref2->inode_list;
8463ef5969cSAlexander Block 			else
8473ef5969cSAlexander Block 				ref1->inode_list = ref2->inode_list;
8483ef5969cSAlexander Block 			ref1->count += ref2->count;
8493ef5969cSAlexander Block 
8508da6d581SJan Schmidt 			list_del(&ref2->list);
851b9e9a6cbSWang Shilong 			kmem_cache_free(btrfs_prelim_ref_cache, ref2);
852d8422ba3SQu Wenruo 			cond_resched();
8538da6d581SJan Schmidt 		}
8548da6d581SJan Schmidt 
8558da6d581SJan Schmidt 	}
8568da6d581SJan Schmidt }
8578da6d581SJan Schmidt 
8588da6d581SJan Schmidt /*
8598da6d581SJan Schmidt  * add all currently queued delayed refs from this head whose seq nr is
8608da6d581SJan Schmidt  * smaller or equal that seq to the list
8618da6d581SJan Schmidt  */
8628da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
863dc046b10SJosef Bacik 			      struct list_head *prefs, u64 *total_refs,
864dc046b10SJosef Bacik 			      u64 inum)
8658da6d581SJan Schmidt {
866c6fc2454SQu Wenruo 	struct btrfs_delayed_ref_node *node;
8678da6d581SJan Schmidt 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
868d5c88b73SJan Schmidt 	struct btrfs_key key;
869d5c88b73SJan Schmidt 	struct btrfs_key op_key = {0};
8708da6d581SJan Schmidt 	int sgn;
871b1375d64SJan Schmidt 	int ret = 0;
8728da6d581SJan Schmidt 
8738da6d581SJan Schmidt 	if (extent_op && extent_op->update_key)
874d5c88b73SJan Schmidt 		btrfs_disk_key_to_cpu(&op_key, &extent_op->key);
8758da6d581SJan Schmidt 
876d7df2c79SJosef Bacik 	spin_lock(&head->lock);
877c6fc2454SQu Wenruo 	list_for_each_entry(node, &head->ref_list, list) {
8788da6d581SJan Schmidt 		if (node->seq > seq)
8798da6d581SJan Schmidt 			continue;
8808da6d581SJan Schmidt 
8818da6d581SJan Schmidt 		switch (node->action) {
8828da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_EXTENT:
8838da6d581SJan Schmidt 		case BTRFS_UPDATE_DELAYED_HEAD:
8848da6d581SJan Schmidt 			WARN_ON(1);
8858da6d581SJan Schmidt 			continue;
8868da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_REF:
8878da6d581SJan Schmidt 			sgn = 1;
8888da6d581SJan Schmidt 			break;
8898da6d581SJan Schmidt 		case BTRFS_DROP_DELAYED_REF:
8908da6d581SJan Schmidt 			sgn = -1;
8918da6d581SJan Schmidt 			break;
8928da6d581SJan Schmidt 		default:
8938da6d581SJan Schmidt 			BUG_ON(1);
8948da6d581SJan Schmidt 		}
89544853868SJosef Bacik 		*total_refs += (node->ref_mod * sgn);
8968da6d581SJan Schmidt 		switch (node->type) {
8978da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY: {
8988da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
8998da6d581SJan Schmidt 
9008da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
901d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &op_key,
9028da6d581SJan Schmidt 					       ref->level + 1, 0, node->bytenr,
903742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
9048da6d581SJan Schmidt 			break;
9058da6d581SJan Schmidt 		}
9068da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY: {
9078da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
9088da6d581SJan Schmidt 
9098da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
910acdf898dSLiu Bo 			ret = __add_prelim_ref(prefs, 0, NULL,
9118da6d581SJan Schmidt 					       ref->level + 1, ref->parent,
9128da6d581SJan Schmidt 					       node->bytenr,
913742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
9148da6d581SJan Schmidt 			break;
9158da6d581SJan Schmidt 		}
9168da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
9178da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
9188da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
9198da6d581SJan Schmidt 
9208da6d581SJan Schmidt 			key.objectid = ref->objectid;
9218da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
9228da6d581SJan Schmidt 			key.offset = ref->offset;
923dc046b10SJosef Bacik 
924dc046b10SJosef Bacik 			/*
925dc046b10SJosef Bacik 			 * Found a inum that doesn't match our known inum, we
926dc046b10SJosef Bacik 			 * know it's shared.
927dc046b10SJosef Bacik 			 */
928dc046b10SJosef Bacik 			if (inum && ref->objectid != inum) {
929dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
930dc046b10SJosef Bacik 				break;
931dc046b10SJosef Bacik 			}
932dc046b10SJosef Bacik 
9338da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
9348da6d581SJan Schmidt 					       node->bytenr,
935742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
9368da6d581SJan Schmidt 			break;
9378da6d581SJan Schmidt 		}
9388da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
9398da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
9408da6d581SJan Schmidt 
9418da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
942acdf898dSLiu Bo 			ret = __add_prelim_ref(prefs, 0, NULL, 0,
9438da6d581SJan Schmidt 					       ref->parent, node->bytenr,
944742916b8SWang Shilong 					       node->ref_mod * sgn, GFP_ATOMIC);
9458da6d581SJan Schmidt 			break;
9468da6d581SJan Schmidt 		}
9478da6d581SJan Schmidt 		default:
9488da6d581SJan Schmidt 			WARN_ON(1);
9498da6d581SJan Schmidt 		}
9501149ab6bSWang Shilong 		if (ret)
951d7df2c79SJosef Bacik 			break;
9528da6d581SJan Schmidt 	}
953d7df2c79SJosef Bacik 	spin_unlock(&head->lock);
954d7df2c79SJosef Bacik 	return ret;
9558da6d581SJan Schmidt }
9568da6d581SJan Schmidt 
9578da6d581SJan Schmidt /*
9588da6d581SJan Schmidt  * add all inline backrefs for bytenr to the list
9598da6d581SJan Schmidt  */
9608da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info,
9618da6d581SJan Schmidt 			     struct btrfs_path *path, u64 bytenr,
96244853868SJosef Bacik 			     int *info_level, struct list_head *prefs,
963afce772eSLu Fengqi 			     struct ref_root *ref_tree,
964dc046b10SJosef Bacik 			     u64 *total_refs, u64 inum)
9658da6d581SJan Schmidt {
966b1375d64SJan Schmidt 	int ret = 0;
9678da6d581SJan Schmidt 	int slot;
9688da6d581SJan Schmidt 	struct extent_buffer *leaf;
9698da6d581SJan Schmidt 	struct btrfs_key key;
970261c84b6SJosef Bacik 	struct btrfs_key found_key;
9718da6d581SJan Schmidt 	unsigned long ptr;
9728da6d581SJan Schmidt 	unsigned long end;
9738da6d581SJan Schmidt 	struct btrfs_extent_item *ei;
9748da6d581SJan Schmidt 	u64 flags;
9758da6d581SJan Schmidt 	u64 item_size;
9768da6d581SJan Schmidt 
9778da6d581SJan Schmidt 	/*
9788da6d581SJan Schmidt 	 * enumerate all inline refs
9798da6d581SJan Schmidt 	 */
9808da6d581SJan Schmidt 	leaf = path->nodes[0];
981dadcaf78SJan Schmidt 	slot = path->slots[0];
9828da6d581SJan Schmidt 
9838da6d581SJan Schmidt 	item_size = btrfs_item_size_nr(leaf, slot);
9848da6d581SJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
9858da6d581SJan Schmidt 
9868da6d581SJan Schmidt 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
9878da6d581SJan Schmidt 	flags = btrfs_extent_flags(leaf, ei);
98844853868SJosef Bacik 	*total_refs += btrfs_extent_refs(leaf, ei);
989261c84b6SJosef Bacik 	btrfs_item_key_to_cpu(leaf, &found_key, slot);
9908da6d581SJan Schmidt 
9918da6d581SJan Schmidt 	ptr = (unsigned long)(ei + 1);
9928da6d581SJan Schmidt 	end = (unsigned long)ei + item_size;
9938da6d581SJan Schmidt 
994261c84b6SJosef Bacik 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
995261c84b6SJosef Bacik 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
9968da6d581SJan Schmidt 		struct btrfs_tree_block_info *info;
9978da6d581SJan Schmidt 
9988da6d581SJan Schmidt 		info = (struct btrfs_tree_block_info *)ptr;
9998da6d581SJan Schmidt 		*info_level = btrfs_tree_block_level(leaf, info);
10008da6d581SJan Schmidt 		ptr += sizeof(struct btrfs_tree_block_info);
10018da6d581SJan Schmidt 		BUG_ON(ptr > end);
1002261c84b6SJosef Bacik 	} else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
1003261c84b6SJosef Bacik 		*info_level = found_key.offset;
10048da6d581SJan Schmidt 	} else {
10058da6d581SJan Schmidt 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
10068da6d581SJan Schmidt 	}
10078da6d581SJan Schmidt 
10088da6d581SJan Schmidt 	while (ptr < end) {
10098da6d581SJan Schmidt 		struct btrfs_extent_inline_ref *iref;
10108da6d581SJan Schmidt 		u64 offset;
10118da6d581SJan Schmidt 		int type;
10128da6d581SJan Schmidt 
10138da6d581SJan Schmidt 		iref = (struct btrfs_extent_inline_ref *)ptr;
10148da6d581SJan Schmidt 		type = btrfs_extent_inline_ref_type(leaf, iref);
10158da6d581SJan Schmidt 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
10168da6d581SJan Schmidt 
10178da6d581SJan Schmidt 		switch (type) {
10188da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
1019d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL,
10208da6d581SJan Schmidt 						*info_level + 1, offset,
1021742916b8SWang Shilong 						bytenr, 1, GFP_NOFS);
10228da6d581SJan Schmidt 			break;
10238da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
10248da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
10258da6d581SJan Schmidt 			int count;
10268da6d581SJan Schmidt 
10278da6d581SJan Schmidt 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
10288da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
10298da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
1030742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
1031afce772eSLu Fengqi 			if (ref_tree) {
1032afce772eSLu Fengqi 				if (!ret)
1033afce772eSLu Fengqi 					ret = ref_tree_add(ref_tree, 0, 0, 0,
1034afce772eSLu Fengqi 							   bytenr, count);
1035afce772eSLu Fengqi 				if (!ret && ref_tree->unique_refs > 1)
1036afce772eSLu Fengqi 					ret = BACKREF_FOUND_SHARED;
1037afce772eSLu Fengqi 			}
10388da6d581SJan Schmidt 			break;
10398da6d581SJan Schmidt 		}
10408da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
1041d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, offset, NULL,
1042d5c88b73SJan Schmidt 					       *info_level + 1, 0,
1043742916b8SWang Shilong 					       bytenr, 1, GFP_NOFS);
10448da6d581SJan Schmidt 			break;
10458da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
10468da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
10478da6d581SJan Schmidt 			int count;
10488da6d581SJan Schmidt 			u64 root;
10498da6d581SJan Schmidt 
10508da6d581SJan Schmidt 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10518da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
10528da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
10538da6d581SJan Schmidt 								      dref);
10548da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
10558da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
1056dc046b10SJosef Bacik 
1057dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
1058dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1059dc046b10SJosef Bacik 				break;
1060dc046b10SJosef Bacik 			}
1061dc046b10SJosef Bacik 
10628da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
1063d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
1064742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
1065afce772eSLu Fengqi 			if (ref_tree) {
1066afce772eSLu Fengqi 				if (!ret)
1067afce772eSLu Fengqi 					ret = ref_tree_add(ref_tree, root,
1068afce772eSLu Fengqi 							   key.objectid,
1069afce772eSLu Fengqi 							   key.offset, 0,
1070afce772eSLu Fengqi 							   count);
1071afce772eSLu Fengqi 				if (!ret && ref_tree->unique_refs > 1)
1072afce772eSLu Fengqi 					ret = BACKREF_FOUND_SHARED;
1073afce772eSLu Fengqi 			}
10748da6d581SJan Schmidt 			break;
10758da6d581SJan Schmidt 		}
10768da6d581SJan Schmidt 		default:
10778da6d581SJan Schmidt 			WARN_ON(1);
10788da6d581SJan Schmidt 		}
10791149ab6bSWang Shilong 		if (ret)
10801149ab6bSWang Shilong 			return ret;
10818da6d581SJan Schmidt 		ptr += btrfs_extent_inline_ref_size(type);
10828da6d581SJan Schmidt 	}
10838da6d581SJan Schmidt 
10848da6d581SJan Schmidt 	return 0;
10858da6d581SJan Schmidt }
10868da6d581SJan Schmidt 
10878da6d581SJan Schmidt /*
10888da6d581SJan Schmidt  * add all non-inline backrefs for bytenr to the list
10898da6d581SJan Schmidt  */
10908da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
10918da6d581SJan Schmidt 			    struct btrfs_path *path, u64 bytenr,
1092afce772eSLu Fengqi 			    int info_level, struct list_head *prefs,
1093afce772eSLu Fengqi 			    struct ref_root *ref_tree, u64 inum)
10948da6d581SJan Schmidt {
10958da6d581SJan Schmidt 	struct btrfs_root *extent_root = fs_info->extent_root;
10968da6d581SJan Schmidt 	int ret;
10978da6d581SJan Schmidt 	int slot;
10988da6d581SJan Schmidt 	struct extent_buffer *leaf;
10998da6d581SJan Schmidt 	struct btrfs_key key;
11008da6d581SJan Schmidt 
11018da6d581SJan Schmidt 	while (1) {
11028da6d581SJan Schmidt 		ret = btrfs_next_item(extent_root, path);
11038da6d581SJan Schmidt 		if (ret < 0)
11048da6d581SJan Schmidt 			break;
11058da6d581SJan Schmidt 		if (ret) {
11068da6d581SJan Schmidt 			ret = 0;
11078da6d581SJan Schmidt 			break;
11088da6d581SJan Schmidt 		}
11098da6d581SJan Schmidt 
11108da6d581SJan Schmidt 		slot = path->slots[0];
11118da6d581SJan Schmidt 		leaf = path->nodes[0];
11128da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
11138da6d581SJan Schmidt 
11148da6d581SJan Schmidt 		if (key.objectid != bytenr)
11158da6d581SJan Schmidt 			break;
11168da6d581SJan Schmidt 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
11178da6d581SJan Schmidt 			continue;
11188da6d581SJan Schmidt 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
11198da6d581SJan Schmidt 			break;
11208da6d581SJan Schmidt 
11218da6d581SJan Schmidt 		switch (key.type) {
11228da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
1123d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL,
11248da6d581SJan Schmidt 						info_level + 1, key.offset,
1125742916b8SWang Shilong 						bytenr, 1, GFP_NOFS);
11268da6d581SJan Schmidt 			break;
11278da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
11288da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
11298da6d581SJan Schmidt 			int count;
11308da6d581SJan Schmidt 
11318da6d581SJan Schmidt 			sdref = btrfs_item_ptr(leaf, slot,
11328da6d581SJan Schmidt 					      struct btrfs_shared_data_ref);
11338da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
11348da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
1135742916b8SWang Shilong 						bytenr, count, GFP_NOFS);
1136afce772eSLu Fengqi 			if (ref_tree) {
1137afce772eSLu Fengqi 				if (!ret)
1138afce772eSLu Fengqi 					ret = ref_tree_add(ref_tree, 0, 0, 0,
1139afce772eSLu Fengqi 							   bytenr, count);
1140afce772eSLu Fengqi 				if (!ret && ref_tree->unique_refs > 1)
1141afce772eSLu Fengqi 					ret = BACKREF_FOUND_SHARED;
1142afce772eSLu Fengqi 			}
11438da6d581SJan Schmidt 			break;
11448da6d581SJan Schmidt 		}
11458da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
1146d5c88b73SJan Schmidt 			ret = __add_prelim_ref(prefs, key.offset, NULL,
1147d5c88b73SJan Schmidt 					       info_level + 1, 0,
1148742916b8SWang Shilong 					       bytenr, 1, GFP_NOFS);
11498da6d581SJan Schmidt 			break;
11508da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
11518da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
11528da6d581SJan Schmidt 			int count;
11538da6d581SJan Schmidt 			u64 root;
11548da6d581SJan Schmidt 
11558da6d581SJan Schmidt 			dref = btrfs_item_ptr(leaf, slot,
11568da6d581SJan Schmidt 					      struct btrfs_extent_data_ref);
11578da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
11588da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
11598da6d581SJan Schmidt 								      dref);
11608da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
11618da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
1162dc046b10SJosef Bacik 
1163dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
1164dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1165dc046b10SJosef Bacik 				break;
1166dc046b10SJosef Bacik 			}
1167dc046b10SJosef Bacik 
11688da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
11698da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
1170742916b8SWang Shilong 					       bytenr, count, GFP_NOFS);
1171afce772eSLu Fengqi 			if (ref_tree) {
1172afce772eSLu Fengqi 				if (!ret)
1173afce772eSLu Fengqi 					ret = ref_tree_add(ref_tree, root,
1174afce772eSLu Fengqi 							   key.objectid,
1175afce772eSLu Fengqi 							   key.offset, 0,
1176afce772eSLu Fengqi 							   count);
1177afce772eSLu Fengqi 				if (!ret && ref_tree->unique_refs > 1)
1178afce772eSLu Fengqi 					ret = BACKREF_FOUND_SHARED;
1179afce772eSLu Fengqi 			}
11808da6d581SJan Schmidt 			break;
11818da6d581SJan Schmidt 		}
11828da6d581SJan Schmidt 		default:
11838da6d581SJan Schmidt 			WARN_ON(1);
11848da6d581SJan Schmidt 		}
11851149ab6bSWang Shilong 		if (ret)
11861149ab6bSWang Shilong 			return ret;
11871149ab6bSWang Shilong 
11888da6d581SJan Schmidt 	}
11898da6d581SJan Schmidt 
11908da6d581SJan Schmidt 	return ret;
11918da6d581SJan Schmidt }
11928da6d581SJan Schmidt 
11938da6d581SJan Schmidt /*
11948da6d581SJan Schmidt  * this adds all existing backrefs (inline backrefs, backrefs and delayed
11958da6d581SJan Schmidt  * refs) for the given bytenr to the refs list, merges duplicates and resolves
11968da6d581SJan Schmidt  * indirect refs to their parent bytenr.
11978da6d581SJan Schmidt  * When roots are found, they're added to the roots list
11988da6d581SJan Schmidt  *
11992c2ed5aaSMark Fasheh  * NOTE: This can return values > 0
12002c2ed5aaSMark Fasheh  *
120121633fc6SQu Wenruo  * If time_seq is set to (u64)-1, it will not search delayed_refs, and behave
120221633fc6SQu Wenruo  * much like trans == NULL case, the difference only lies in it will not
120321633fc6SQu Wenruo  * commit root.
120421633fc6SQu Wenruo  * The special case is for qgroup to search roots in commit_transaction().
120521633fc6SQu Wenruo  *
1206afce772eSLu Fengqi  * If check_shared is set to 1, any extent has more than one ref item, will
1207afce772eSLu Fengqi  * be returned BACKREF_FOUND_SHARED immediately.
1208afce772eSLu Fengqi  *
12098da6d581SJan Schmidt  * FIXME some caching might speed things up
12108da6d581SJan Schmidt  */
12118da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans,
12128da6d581SJan Schmidt 			     struct btrfs_fs_info *fs_info, u64 bytenr,
1213097b8a7cSJan Schmidt 			     u64 time_seq, struct ulist *refs,
1214dc046b10SJosef Bacik 			     struct ulist *roots, const u64 *extent_item_pos,
1215afce772eSLu Fengqi 			     u64 root_objectid, u64 inum, int check_shared)
12168da6d581SJan Schmidt {
12178da6d581SJan Schmidt 	struct btrfs_key key;
12188da6d581SJan Schmidt 	struct btrfs_path *path;
12198da6d581SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
1220d3b01064SLi Zefan 	struct btrfs_delayed_ref_head *head;
12218da6d581SJan Schmidt 	int info_level = 0;
12228da6d581SJan Schmidt 	int ret;
12238da6d581SJan Schmidt 	struct list_head prefs_delayed;
12248da6d581SJan Schmidt 	struct list_head prefs;
12258da6d581SJan Schmidt 	struct __prelim_ref *ref;
1226f05c4746SWang Shilong 	struct extent_inode_elem *eie = NULL;
1227afce772eSLu Fengqi 	struct ref_root *ref_tree = NULL;
122844853868SJosef Bacik 	u64 total_refs = 0;
12298da6d581SJan Schmidt 
12308da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs);
12318da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs_delayed);
12328da6d581SJan Schmidt 
12338da6d581SJan Schmidt 	key.objectid = bytenr;
12348da6d581SJan Schmidt 	key.offset = (u64)-1;
1235261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1236261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1237261c84b6SJosef Bacik 	else
1238261c84b6SJosef Bacik 		key.type = BTRFS_EXTENT_ITEM_KEY;
12398da6d581SJan Schmidt 
12408da6d581SJan Schmidt 	path = btrfs_alloc_path();
12418da6d581SJan Schmidt 	if (!path)
12428da6d581SJan Schmidt 		return -ENOMEM;
1243e84752d4SWang Shilong 	if (!trans) {
1244da61d31aSJosef Bacik 		path->search_commit_root = 1;
1245e84752d4SWang Shilong 		path->skip_locking = 1;
1246e84752d4SWang Shilong 	}
12478da6d581SJan Schmidt 
124821633fc6SQu Wenruo 	if (time_seq == (u64)-1)
124921633fc6SQu Wenruo 		path->skip_locking = 1;
125021633fc6SQu Wenruo 
12518da6d581SJan Schmidt 	/*
12528da6d581SJan Schmidt 	 * grab both a lock on the path and a lock on the delayed ref head.
12538da6d581SJan Schmidt 	 * We need both to get a consistent picture of how the refs look
12548da6d581SJan Schmidt 	 * at a specified point in time
12558da6d581SJan Schmidt 	 */
12568da6d581SJan Schmidt again:
1257d3b01064SLi Zefan 	head = NULL;
1258d3b01064SLi Zefan 
1259afce772eSLu Fengqi 	if (check_shared) {
1260afce772eSLu Fengqi 		if (!ref_tree) {
1261afce772eSLu Fengqi 			ref_tree = ref_root_alloc();
1262afce772eSLu Fengqi 			if (!ref_tree) {
1263afce772eSLu Fengqi 				ret = -ENOMEM;
1264afce772eSLu Fengqi 				goto out;
1265afce772eSLu Fengqi 			}
1266afce772eSLu Fengqi 		} else {
1267afce772eSLu Fengqi 			ref_root_fini(ref_tree);
1268afce772eSLu Fengqi 		}
1269afce772eSLu Fengqi 	}
1270afce772eSLu Fengqi 
12718da6d581SJan Schmidt 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
12728da6d581SJan Schmidt 	if (ret < 0)
12738da6d581SJan Schmidt 		goto out;
12748da6d581SJan Schmidt 	BUG_ON(ret == 0);
12758da6d581SJan Schmidt 
1276faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
127721633fc6SQu Wenruo 	if (trans && likely(trans->type != __TRANS_DUMMY) &&
127821633fc6SQu Wenruo 	    time_seq != (u64)-1) {
1279faa2dbf0SJosef Bacik #else
128021633fc6SQu Wenruo 	if (trans && time_seq != (u64)-1) {
1281faa2dbf0SJosef Bacik #endif
12828da6d581SJan Schmidt 		/*
12837a3ae2f8SJan Schmidt 		 * look if there are updates for this ref queued and lock the
12847a3ae2f8SJan Schmidt 		 * head
12858da6d581SJan Schmidt 		 */
12868da6d581SJan Schmidt 		delayed_refs = &trans->transaction->delayed_refs;
12878da6d581SJan Schmidt 		spin_lock(&delayed_refs->lock);
12888da6d581SJan Schmidt 		head = btrfs_find_delayed_ref_head(trans, bytenr);
12898da6d581SJan Schmidt 		if (head) {
12908da6d581SJan Schmidt 			if (!mutex_trylock(&head->mutex)) {
12918da6d581SJan Schmidt 				atomic_inc(&head->node.refs);
12928da6d581SJan Schmidt 				spin_unlock(&delayed_refs->lock);
12938da6d581SJan Schmidt 
12948da6d581SJan Schmidt 				btrfs_release_path(path);
12958da6d581SJan Schmidt 
12968da6d581SJan Schmidt 				/*
12978da6d581SJan Schmidt 				 * Mutex was contended, block until it's
12988da6d581SJan Schmidt 				 * released and try again
12998da6d581SJan Schmidt 				 */
13008da6d581SJan Schmidt 				mutex_lock(&head->mutex);
13018da6d581SJan Schmidt 				mutex_unlock(&head->mutex);
13028da6d581SJan Schmidt 				btrfs_put_delayed_ref(&head->node);
13038da6d581SJan Schmidt 				goto again;
13048da6d581SJan Schmidt 			}
1305d7df2c79SJosef Bacik 			spin_unlock(&delayed_refs->lock);
1306097b8a7cSJan Schmidt 			ret = __add_delayed_refs(head, time_seq,
1307dc046b10SJosef Bacik 						 &prefs_delayed, &total_refs,
1308dc046b10SJosef Bacik 						 inum);
1309155725c9SJan Schmidt 			mutex_unlock(&head->mutex);
1310d7df2c79SJosef Bacik 			if (ret)
13118da6d581SJan Schmidt 				goto out;
1312d7df2c79SJosef Bacik 		} else {
13138da6d581SJan Schmidt 			spin_unlock(&delayed_refs->lock);
13147a3ae2f8SJan Schmidt 		}
1315afce772eSLu Fengqi 
1316afce772eSLu Fengqi 		if (check_shared && !list_empty(&prefs_delayed)) {
1317afce772eSLu Fengqi 			/*
1318afce772eSLu Fengqi 			 * Add all delay_ref to the ref_tree and check if there
1319afce772eSLu Fengqi 			 * are multiple ref items added.
1320afce772eSLu Fengqi 			 */
1321afce772eSLu Fengqi 			list_for_each_entry(ref, &prefs_delayed, list) {
1322afce772eSLu Fengqi 				if (ref->key_for_search.type) {
1323afce772eSLu Fengqi 					ret = ref_tree_add(ref_tree,
1324afce772eSLu Fengqi 						ref->root_id,
1325afce772eSLu Fengqi 						ref->key_for_search.objectid,
1326afce772eSLu Fengqi 						ref->key_for_search.offset,
1327afce772eSLu Fengqi 						0, ref->count);
1328afce772eSLu Fengqi 					if (ret)
1329afce772eSLu Fengqi 						goto out;
1330afce772eSLu Fengqi 				} else {
1331afce772eSLu Fengqi 					ret = ref_tree_add(ref_tree, 0, 0, 0,
1332afce772eSLu Fengqi 						     ref->parent, ref->count);
1333afce772eSLu Fengqi 					if (ret)
1334afce772eSLu Fengqi 						goto out;
1335afce772eSLu Fengqi 				}
1336afce772eSLu Fengqi 
1337afce772eSLu Fengqi 			}
1338afce772eSLu Fengqi 
1339afce772eSLu Fengqi 			if (ref_tree->unique_refs > 1) {
1340afce772eSLu Fengqi 				ret = BACKREF_FOUND_SHARED;
1341afce772eSLu Fengqi 				goto out;
1342afce772eSLu Fengqi 			}
1343afce772eSLu Fengqi 
1344afce772eSLu Fengqi 		}
1345d7df2c79SJosef Bacik 	}
13468da6d581SJan Schmidt 
13478da6d581SJan Schmidt 	if (path->slots[0]) {
13488da6d581SJan Schmidt 		struct extent_buffer *leaf;
13498da6d581SJan Schmidt 		int slot;
13508da6d581SJan Schmidt 
1351dadcaf78SJan Schmidt 		path->slots[0]--;
13528da6d581SJan Schmidt 		leaf = path->nodes[0];
1353dadcaf78SJan Schmidt 		slot = path->slots[0];
13548da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
13558da6d581SJan Schmidt 		if (key.objectid == bytenr &&
1356261c84b6SJosef Bacik 		    (key.type == BTRFS_EXTENT_ITEM_KEY ||
1357261c84b6SJosef Bacik 		     key.type == BTRFS_METADATA_ITEM_KEY)) {
13588da6d581SJan Schmidt 			ret = __add_inline_refs(fs_info, path, bytenr,
135944853868SJosef Bacik 						&info_level, &prefs,
1360afce772eSLu Fengqi 						ref_tree, &total_refs,
1361afce772eSLu Fengqi 						inum);
13628da6d581SJan Schmidt 			if (ret)
13638da6d581SJan Schmidt 				goto out;
1364d5c88b73SJan Schmidt 			ret = __add_keyed_refs(fs_info, path, bytenr,
1365afce772eSLu Fengqi 					       info_level, &prefs,
1366afce772eSLu Fengqi 					       ref_tree, inum);
13678da6d581SJan Schmidt 			if (ret)
13688da6d581SJan Schmidt 				goto out;
13698da6d581SJan Schmidt 		}
13708da6d581SJan Schmidt 	}
13718da6d581SJan Schmidt 	btrfs_release_path(path);
13728da6d581SJan Schmidt 
13738da6d581SJan Schmidt 	list_splice_init(&prefs_delayed, &prefs);
13748da6d581SJan Schmidt 
1375d5c88b73SJan Schmidt 	ret = __add_missing_keys(fs_info, &prefs);
1376d5c88b73SJan Schmidt 	if (ret)
1377d5c88b73SJan Schmidt 		goto out;
1378d5c88b73SJan Schmidt 
1379692206b1SWang Shilong 	__merge_refs(&prefs, 1);
13808da6d581SJan Schmidt 
1381da61d31aSJosef Bacik 	ret = __resolve_indirect_refs(fs_info, path, time_seq, &prefs,
1382dc046b10SJosef Bacik 				      extent_item_pos, total_refs,
1383dc046b10SJosef Bacik 				      root_objectid);
13848da6d581SJan Schmidt 	if (ret)
13858da6d581SJan Schmidt 		goto out;
13868da6d581SJan Schmidt 
1387692206b1SWang Shilong 	__merge_refs(&prefs, 2);
13888da6d581SJan Schmidt 
13898da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
13908da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
13916c1500f2SJulia Lawall 		WARN_ON(ref->count < 0);
139298cfee21SWang Shilong 		if (roots && ref->count && ref->root_id && ref->parent == 0) {
1393dc046b10SJosef Bacik 			if (root_objectid && ref->root_id != root_objectid) {
1394dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1395dc046b10SJosef Bacik 				goto out;
1396dc046b10SJosef Bacik 			}
1397dc046b10SJosef Bacik 
13988da6d581SJan Schmidt 			/* no parent == root of tree */
13998da6d581SJan Schmidt 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
1400f1723939SWang Shilong 			if (ret < 0)
1401f1723939SWang Shilong 				goto out;
14028da6d581SJan Schmidt 		}
14038da6d581SJan Schmidt 		if (ref->count && ref->parent) {
14048a56457fSJosef Bacik 			if (extent_item_pos && !ref->inode_list &&
14058a56457fSJosef Bacik 			    ref->level == 0) {
1406976b1908SJan Schmidt 				struct extent_buffer *eb;
1407707e8a07SDavid Sterba 
1408976b1908SJan Schmidt 				eb = read_tree_block(fs_info->extent_root,
1409ce86cd59SDavid Sterba 							   ref->parent, 0);
141064c043deSLiu Bo 				if (IS_ERR(eb)) {
141164c043deSLiu Bo 					ret = PTR_ERR(eb);
141264c043deSLiu Bo 					goto out;
141364c043deSLiu Bo 				} else if (!extent_buffer_uptodate(eb)) {
1414416bc658SJosef Bacik 					free_extent_buffer(eb);
1415c16c2e2eSWang Shilong 					ret = -EIO;
1416c16c2e2eSWang Shilong 					goto out;
1417416bc658SJosef Bacik 				}
14186f7ff6d7SFilipe Manana 				btrfs_tree_read_lock(eb);
14196f7ff6d7SFilipe Manana 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1420976b1908SJan Schmidt 				ret = find_extent_in_eb(eb, bytenr,
1421976b1908SJan Schmidt 							*extent_item_pos, &eie);
14226f7ff6d7SFilipe Manana 				btrfs_tree_read_unlock_blocking(eb);
1423976b1908SJan Schmidt 				free_extent_buffer(eb);
1424f5929cd8SFilipe David Borba Manana 				if (ret < 0)
1425f5929cd8SFilipe David Borba Manana 					goto out;
1426f5929cd8SFilipe David Borba Manana 				ref->inode_list = eie;
1427976b1908SJan Schmidt 			}
14284eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(refs, ref->parent,
14294eb1f66dSTakashi Iwai 						  ref->inode_list,
14304eb1f66dSTakashi Iwai 						  (void **)&eie, GFP_NOFS);
1431f1723939SWang Shilong 			if (ret < 0)
1432f1723939SWang Shilong 				goto out;
14333301958bSJan Schmidt 			if (!ret && extent_item_pos) {
14343301958bSJan Schmidt 				/*
14353301958bSJan Schmidt 				 * we've recorded that parent, so we must extend
14363301958bSJan Schmidt 				 * its inode list here
14373301958bSJan Schmidt 				 */
14383301958bSJan Schmidt 				BUG_ON(!eie);
14393301958bSJan Schmidt 				while (eie->next)
14403301958bSJan Schmidt 					eie = eie->next;
14413301958bSJan Schmidt 				eie->next = ref->inode_list;
14423301958bSJan Schmidt 			}
1443f05c4746SWang Shilong 			eie = NULL;
14448da6d581SJan Schmidt 		}
1445a4fdb61eSWang Shilong 		list_del(&ref->list);
1446b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
14478da6d581SJan Schmidt 	}
14488da6d581SJan Schmidt 
14498da6d581SJan Schmidt out:
14508da6d581SJan Schmidt 	btrfs_free_path(path);
1451afce772eSLu Fengqi 	ref_root_free(ref_tree);
14528da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
14538da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
14548da6d581SJan Schmidt 		list_del(&ref->list);
1455b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
14568da6d581SJan Schmidt 	}
14578da6d581SJan Schmidt 	while (!list_empty(&prefs_delayed)) {
14588da6d581SJan Schmidt 		ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
14598da6d581SJan Schmidt 				       list);
14608da6d581SJan Schmidt 		list_del(&ref->list);
1461b9e9a6cbSWang Shilong 		kmem_cache_free(btrfs_prelim_ref_cache, ref);
14628da6d581SJan Schmidt 	}
1463f05c4746SWang Shilong 	if (ret < 0)
1464f05c4746SWang Shilong 		free_inode_elem_list(eie);
14658da6d581SJan Schmidt 	return ret;
14668da6d581SJan Schmidt }
14678da6d581SJan Schmidt 
1468976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks)
1469976b1908SJan Schmidt {
1470976b1908SJan Schmidt 	struct ulist_node *node = NULL;
1471976b1908SJan Schmidt 	struct extent_inode_elem *eie;
1472976b1908SJan Schmidt 	struct ulist_iterator uiter;
1473976b1908SJan Schmidt 
1474976b1908SJan Schmidt 	ULIST_ITER_INIT(&uiter);
1475976b1908SJan Schmidt 	while ((node = ulist_next(blocks, &uiter))) {
1476976b1908SJan Schmidt 		if (!node->aux)
1477976b1908SJan Schmidt 			continue;
1478995e01b7SJan Schmidt 		eie = (struct extent_inode_elem *)(uintptr_t)node->aux;
1479f05c4746SWang Shilong 		free_inode_elem_list(eie);
1480976b1908SJan Schmidt 		node->aux = 0;
1481976b1908SJan Schmidt 	}
1482976b1908SJan Schmidt 
1483976b1908SJan Schmidt 	ulist_free(blocks);
1484976b1908SJan Schmidt }
1485976b1908SJan Schmidt 
14868da6d581SJan Schmidt /*
14878da6d581SJan Schmidt  * Finds all leafs with a reference to the specified combination of bytenr and
14888da6d581SJan Schmidt  * offset. key_list_head will point to a list of corresponding keys (caller must
14898da6d581SJan Schmidt  * free each list element). The leafs will be stored in the leafs ulist, which
14908da6d581SJan Schmidt  * must be freed with ulist_free.
14918da6d581SJan Schmidt  *
14928da6d581SJan Schmidt  * returns 0 on success, <0 on error
14938da6d581SJan Schmidt  */
14948da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
14958da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
1496097b8a7cSJan Schmidt 				u64 time_seq, struct ulist **leafs,
1497976b1908SJan Schmidt 				const u64 *extent_item_pos)
14988da6d581SJan Schmidt {
14998da6d581SJan Schmidt 	int ret;
15008da6d581SJan Schmidt 
15018da6d581SJan Schmidt 	*leafs = ulist_alloc(GFP_NOFS);
150298cfee21SWang Shilong 	if (!*leafs)
15038da6d581SJan Schmidt 		return -ENOMEM;
15048da6d581SJan Schmidt 
1505afce772eSLu Fengqi 	ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1506afce772eSLu Fengqi 				*leafs, NULL, extent_item_pos, 0, 0, 0);
15078da6d581SJan Schmidt 	if (ret < 0 && ret != -ENOENT) {
1508976b1908SJan Schmidt 		free_leaf_list(*leafs);
15098da6d581SJan Schmidt 		return ret;
15108da6d581SJan Schmidt 	}
15118da6d581SJan Schmidt 
15128da6d581SJan Schmidt 	return 0;
15138da6d581SJan Schmidt }
15148da6d581SJan Schmidt 
15158da6d581SJan Schmidt /*
15168da6d581SJan Schmidt  * walk all backrefs for a given extent to find all roots that reference this
15178da6d581SJan Schmidt  * extent. Walking a backref means finding all extents that reference this
15188da6d581SJan Schmidt  * extent and in turn walk the backrefs of those, too. Naturally this is a
15198da6d581SJan Schmidt  * recursive process, but here it is implemented in an iterative fashion: We
15208da6d581SJan Schmidt  * find all referencing extents for the extent in question and put them on a
15218da6d581SJan Schmidt  * list. In turn, we find all referencing extents for those, further appending
15228da6d581SJan Schmidt  * to the list. The way we iterate the list allows adding more elements after
15238da6d581SJan Schmidt  * the current while iterating. The process stops when we reach the end of the
15248da6d581SJan Schmidt  * list. Found roots are added to the roots list.
15258da6d581SJan Schmidt  *
15268da6d581SJan Schmidt  * returns 0 on success, < 0 on error.
15278da6d581SJan Schmidt  */
15289e351cc8SJosef Bacik static int __btrfs_find_all_roots(struct btrfs_trans_handle *trans,
15298da6d581SJan Schmidt 				  struct btrfs_fs_info *fs_info, u64 bytenr,
1530097b8a7cSJan Schmidt 				  u64 time_seq, struct ulist **roots)
15318da6d581SJan Schmidt {
15328da6d581SJan Schmidt 	struct ulist *tmp;
15338da6d581SJan Schmidt 	struct ulist_node *node = NULL;
1534cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
15358da6d581SJan Schmidt 	int ret;
15368da6d581SJan Schmidt 
15378da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
15388da6d581SJan Schmidt 	if (!tmp)
15398da6d581SJan Schmidt 		return -ENOMEM;
15408da6d581SJan Schmidt 	*roots = ulist_alloc(GFP_NOFS);
15418da6d581SJan Schmidt 	if (!*roots) {
15428da6d581SJan Schmidt 		ulist_free(tmp);
15438da6d581SJan Schmidt 		return -ENOMEM;
15448da6d581SJan Schmidt 	}
15458da6d581SJan Schmidt 
1546cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&uiter);
15478da6d581SJan Schmidt 	while (1) {
1548afce772eSLu Fengqi 		ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1549afce772eSLu Fengqi 					tmp, *roots, NULL, 0, 0, 0);
15508da6d581SJan Schmidt 		if (ret < 0 && ret != -ENOENT) {
15518da6d581SJan Schmidt 			ulist_free(tmp);
15528da6d581SJan Schmidt 			ulist_free(*roots);
15538da6d581SJan Schmidt 			return ret;
15548da6d581SJan Schmidt 		}
1555cd1b413cSJan Schmidt 		node = ulist_next(tmp, &uiter);
15568da6d581SJan Schmidt 		if (!node)
15578da6d581SJan Schmidt 			break;
15588da6d581SJan Schmidt 		bytenr = node->val;
1559bca1a290SWang Shilong 		cond_resched();
15608da6d581SJan Schmidt 	}
15618da6d581SJan Schmidt 
15628da6d581SJan Schmidt 	ulist_free(tmp);
15638da6d581SJan Schmidt 	return 0;
15648da6d581SJan Schmidt }
15658da6d581SJan Schmidt 
15669e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
15679e351cc8SJosef Bacik 			 struct btrfs_fs_info *fs_info, u64 bytenr,
15689e351cc8SJosef Bacik 			 u64 time_seq, struct ulist **roots)
15699e351cc8SJosef Bacik {
15709e351cc8SJosef Bacik 	int ret;
15719e351cc8SJosef Bacik 
15729e351cc8SJosef Bacik 	if (!trans)
15739e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
15749e351cc8SJosef Bacik 	ret = __btrfs_find_all_roots(trans, fs_info, bytenr, time_seq, roots);
15759e351cc8SJosef Bacik 	if (!trans)
15769e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
15779e351cc8SJosef Bacik 	return ret;
15789e351cc8SJosef Bacik }
15799e351cc8SJosef Bacik 
15802c2ed5aaSMark Fasheh /**
15812c2ed5aaSMark Fasheh  * btrfs_check_shared - tell us whether an extent is shared
15822c2ed5aaSMark Fasheh  *
15832c2ed5aaSMark Fasheh  * @trans: optional trans handle
15842c2ed5aaSMark Fasheh  *
15852c2ed5aaSMark Fasheh  * btrfs_check_shared uses the backref walking code but will short
15862c2ed5aaSMark Fasheh  * circuit as soon as it finds a root or inode that doesn't match the
15872c2ed5aaSMark Fasheh  * one passed in. This provides a significant performance benefit for
15882c2ed5aaSMark Fasheh  * callers (such as fiemap) which want to know whether the extent is
15892c2ed5aaSMark Fasheh  * shared but do not need a ref count.
15902c2ed5aaSMark Fasheh  *
15912c2ed5aaSMark Fasheh  * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
15922c2ed5aaSMark Fasheh  */
1593dc046b10SJosef Bacik int btrfs_check_shared(struct btrfs_trans_handle *trans,
1594dc046b10SJosef Bacik 		       struct btrfs_fs_info *fs_info, u64 root_objectid,
1595dc046b10SJosef Bacik 		       u64 inum, u64 bytenr)
1596dc046b10SJosef Bacik {
1597dc046b10SJosef Bacik 	struct ulist *tmp = NULL;
1598dc046b10SJosef Bacik 	struct ulist *roots = NULL;
1599dc046b10SJosef Bacik 	struct ulist_iterator uiter;
1600dc046b10SJosef Bacik 	struct ulist_node *node;
16013284da7bSDavid Sterba 	struct seq_list elem = SEQ_LIST_INIT(elem);
1602dc046b10SJosef Bacik 	int ret = 0;
1603dc046b10SJosef Bacik 
1604dc046b10SJosef Bacik 	tmp = ulist_alloc(GFP_NOFS);
1605dc046b10SJosef Bacik 	roots = ulist_alloc(GFP_NOFS);
1606dc046b10SJosef Bacik 	if (!tmp || !roots) {
1607dc046b10SJosef Bacik 		ulist_free(tmp);
1608dc046b10SJosef Bacik 		ulist_free(roots);
1609dc046b10SJosef Bacik 		return -ENOMEM;
1610dc046b10SJosef Bacik 	}
1611dc046b10SJosef Bacik 
1612dc046b10SJosef Bacik 	if (trans)
1613dc046b10SJosef Bacik 		btrfs_get_tree_mod_seq(fs_info, &elem);
1614dc046b10SJosef Bacik 	else
1615dc046b10SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1616dc046b10SJosef Bacik 	ULIST_ITER_INIT(&uiter);
1617dc046b10SJosef Bacik 	while (1) {
1618dc046b10SJosef Bacik 		ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1619afce772eSLu Fengqi 					roots, NULL, root_objectid, inum, 1);
1620dc046b10SJosef Bacik 		if (ret == BACKREF_FOUND_SHARED) {
16212c2ed5aaSMark Fasheh 			/* this is the only condition under which we return 1 */
1622dc046b10SJosef Bacik 			ret = 1;
1623dc046b10SJosef Bacik 			break;
1624dc046b10SJosef Bacik 		}
1625dc046b10SJosef Bacik 		if (ret < 0 && ret != -ENOENT)
1626dc046b10SJosef Bacik 			break;
16272c2ed5aaSMark Fasheh 		ret = 0;
1628dc046b10SJosef Bacik 		node = ulist_next(tmp, &uiter);
1629dc046b10SJosef Bacik 		if (!node)
1630dc046b10SJosef Bacik 			break;
1631dc046b10SJosef Bacik 		bytenr = node->val;
1632dc046b10SJosef Bacik 		cond_resched();
1633dc046b10SJosef Bacik 	}
1634dc046b10SJosef Bacik 	if (trans)
1635dc046b10SJosef Bacik 		btrfs_put_tree_mod_seq(fs_info, &elem);
1636dc046b10SJosef Bacik 	else
1637dc046b10SJosef Bacik 		up_read(&fs_info->commit_root_sem);
1638dc046b10SJosef Bacik 	ulist_free(tmp);
1639dc046b10SJosef Bacik 	ulist_free(roots);
1640dc046b10SJosef Bacik 	return ret;
1641dc046b10SJosef Bacik }
1642dc046b10SJosef Bacik 
1643f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1644f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
1645f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
1646f186373fSMark Fasheh 			  u64 *found_off)
1647f186373fSMark Fasheh {
1648f186373fSMark Fasheh 	int ret, slot;
1649f186373fSMark Fasheh 	struct btrfs_key key;
1650f186373fSMark Fasheh 	struct btrfs_key found_key;
1651f186373fSMark Fasheh 	struct btrfs_inode_extref *extref;
1652f186373fSMark Fasheh 	struct extent_buffer *leaf;
1653f186373fSMark Fasheh 	unsigned long ptr;
1654f186373fSMark Fasheh 
1655f186373fSMark Fasheh 	key.objectid = inode_objectid;
1656962a298fSDavid Sterba 	key.type = BTRFS_INODE_EXTREF_KEY;
1657f186373fSMark Fasheh 	key.offset = start_off;
1658f186373fSMark Fasheh 
1659f186373fSMark Fasheh 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1660f186373fSMark Fasheh 	if (ret < 0)
1661f186373fSMark Fasheh 		return ret;
1662f186373fSMark Fasheh 
1663f186373fSMark Fasheh 	while (1) {
1664f186373fSMark Fasheh 		leaf = path->nodes[0];
1665f186373fSMark Fasheh 		slot = path->slots[0];
1666f186373fSMark Fasheh 		if (slot >= btrfs_header_nritems(leaf)) {
1667f186373fSMark Fasheh 			/*
1668f186373fSMark Fasheh 			 * If the item at offset is not found,
1669f186373fSMark Fasheh 			 * btrfs_search_slot will point us to the slot
1670f186373fSMark Fasheh 			 * where it should be inserted. In our case
1671f186373fSMark Fasheh 			 * that will be the slot directly before the
1672f186373fSMark Fasheh 			 * next INODE_REF_KEY_V2 item. In the case
1673f186373fSMark Fasheh 			 * that we're pointing to the last slot in a
1674f186373fSMark Fasheh 			 * leaf, we must move one leaf over.
1675f186373fSMark Fasheh 			 */
1676f186373fSMark Fasheh 			ret = btrfs_next_leaf(root, path);
1677f186373fSMark Fasheh 			if (ret) {
1678f186373fSMark Fasheh 				if (ret >= 1)
1679f186373fSMark Fasheh 					ret = -ENOENT;
1680f186373fSMark Fasheh 				break;
1681f186373fSMark Fasheh 			}
1682f186373fSMark Fasheh 			continue;
1683f186373fSMark Fasheh 		}
1684f186373fSMark Fasheh 
1685f186373fSMark Fasheh 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1686f186373fSMark Fasheh 
1687f186373fSMark Fasheh 		/*
1688f186373fSMark Fasheh 		 * Check that we're still looking at an extended ref key for
1689f186373fSMark Fasheh 		 * this particular objectid. If we have different
1690f186373fSMark Fasheh 		 * objectid or type then there are no more to be found
1691f186373fSMark Fasheh 		 * in the tree and we can exit.
1692f186373fSMark Fasheh 		 */
1693f186373fSMark Fasheh 		ret = -ENOENT;
1694f186373fSMark Fasheh 		if (found_key.objectid != inode_objectid)
1695f186373fSMark Fasheh 			break;
1696962a298fSDavid Sterba 		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
1697f186373fSMark Fasheh 			break;
1698f186373fSMark Fasheh 
1699f186373fSMark Fasheh 		ret = 0;
1700f186373fSMark Fasheh 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1701f186373fSMark Fasheh 		extref = (struct btrfs_inode_extref *)ptr;
1702f186373fSMark Fasheh 		*ret_extref = extref;
1703f186373fSMark Fasheh 		if (found_off)
1704f186373fSMark Fasheh 			*found_off = found_key.offset;
1705f186373fSMark Fasheh 		break;
1706f186373fSMark Fasheh 	}
1707f186373fSMark Fasheh 
1708f186373fSMark Fasheh 	return ret;
1709f186373fSMark Fasheh }
1710f186373fSMark Fasheh 
171148a3b636SEric Sandeen /*
171248a3b636SEric Sandeen  * this iterates to turn a name (from iref/extref) into a full filesystem path.
171348a3b636SEric Sandeen  * Elements of the path are separated by '/' and the path is guaranteed to be
171448a3b636SEric Sandeen  * 0-terminated. the path is only given within the current file system.
171548a3b636SEric Sandeen  * Therefore, it never starts with a '/'. the caller is responsible to provide
171648a3b636SEric Sandeen  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
171748a3b636SEric Sandeen  * the start point of the resulting string is returned. this pointer is within
171848a3b636SEric Sandeen  * dest, normally.
171948a3b636SEric Sandeen  * in case the path buffer would overflow, the pointer is decremented further
172048a3b636SEric Sandeen  * as if output was written to the buffer, though no more output is actually
172148a3b636SEric Sandeen  * generated. that way, the caller can determine how much space would be
172248a3b636SEric Sandeen  * required for the path to fit into the buffer. in that case, the returned
172348a3b636SEric Sandeen  * value will be smaller than dest. callers must check this!
172448a3b636SEric Sandeen  */
172596b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1726d24bec3aSMark Fasheh 			u32 name_len, unsigned long name_off,
1727a542ad1bSJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
1728a542ad1bSJan Schmidt 			char *dest, u32 size)
1729a542ad1bSJan Schmidt {
1730a542ad1bSJan Schmidt 	int slot;
1731a542ad1bSJan Schmidt 	u64 next_inum;
1732a542ad1bSJan Schmidt 	int ret;
1733661bec6bSGabriel de Perthuis 	s64 bytes_left = ((s64)size) - 1;
1734a542ad1bSJan Schmidt 	struct extent_buffer *eb = eb_in;
1735a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1736b916a59aSJan Schmidt 	int leave_spinning = path->leave_spinning;
1737d24bec3aSMark Fasheh 	struct btrfs_inode_ref *iref;
1738a542ad1bSJan Schmidt 
1739a542ad1bSJan Schmidt 	if (bytes_left >= 0)
1740a542ad1bSJan Schmidt 		dest[bytes_left] = '\0';
1741a542ad1bSJan Schmidt 
1742b916a59aSJan Schmidt 	path->leave_spinning = 1;
1743a542ad1bSJan Schmidt 	while (1) {
1744d24bec3aSMark Fasheh 		bytes_left -= name_len;
1745a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1746a542ad1bSJan Schmidt 			read_extent_buffer(eb, dest + bytes_left,
1747d24bec3aSMark Fasheh 					   name_off, name_len);
1748b916a59aSJan Schmidt 		if (eb != eb_in) {
17490c0fe3b0SFilipe Manana 			if (!path->skip_locking)
1750b916a59aSJan Schmidt 				btrfs_tree_read_unlock_blocking(eb);
1751a542ad1bSJan Schmidt 			free_extent_buffer(eb);
1752b916a59aSJan Schmidt 		}
1753c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, parent, 0,
1754c234a24dSDavid Sterba 				BTRFS_INODE_REF_KEY, &found_key);
17558f24b496SJan Schmidt 		if (ret > 0)
17568f24b496SJan Schmidt 			ret = -ENOENT;
1757a542ad1bSJan Schmidt 		if (ret)
1758a542ad1bSJan Schmidt 			break;
1759d24bec3aSMark Fasheh 
1760a542ad1bSJan Schmidt 		next_inum = found_key.offset;
1761a542ad1bSJan Schmidt 
1762a542ad1bSJan Schmidt 		/* regular exit ahead */
1763a542ad1bSJan Schmidt 		if (parent == next_inum)
1764a542ad1bSJan Schmidt 			break;
1765a542ad1bSJan Schmidt 
1766a542ad1bSJan Schmidt 		slot = path->slots[0];
1767a542ad1bSJan Schmidt 		eb = path->nodes[0];
1768a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
1769b916a59aSJan Schmidt 		if (eb != eb_in) {
17700c0fe3b0SFilipe Manana 			if (!path->skip_locking)
1771b916a59aSJan Schmidt 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
17720c0fe3b0SFilipe Manana 			path->nodes[0] = NULL;
17730c0fe3b0SFilipe Manana 			path->locks[0] = 0;
1774b916a59aSJan Schmidt 		}
1775a542ad1bSJan Schmidt 		btrfs_release_path(path);
1776a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1777d24bec3aSMark Fasheh 
1778d24bec3aSMark Fasheh 		name_len = btrfs_inode_ref_name_len(eb, iref);
1779d24bec3aSMark Fasheh 		name_off = (unsigned long)(iref + 1);
1780d24bec3aSMark Fasheh 
1781a542ad1bSJan Schmidt 		parent = next_inum;
1782a542ad1bSJan Schmidt 		--bytes_left;
1783a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1784a542ad1bSJan Schmidt 			dest[bytes_left] = '/';
1785a542ad1bSJan Schmidt 	}
1786a542ad1bSJan Schmidt 
1787a542ad1bSJan Schmidt 	btrfs_release_path(path);
1788b916a59aSJan Schmidt 	path->leave_spinning = leave_spinning;
1789a542ad1bSJan Schmidt 
1790a542ad1bSJan Schmidt 	if (ret)
1791a542ad1bSJan Schmidt 		return ERR_PTR(ret);
1792a542ad1bSJan Schmidt 
1793a542ad1bSJan Schmidt 	return dest + bytes_left;
1794a542ad1bSJan Schmidt }
1795a542ad1bSJan Schmidt 
1796a542ad1bSJan Schmidt /*
1797a542ad1bSJan Schmidt  * this makes the path point to (logical EXTENT_ITEM *)
1798a542ad1bSJan Schmidt  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1799a542ad1bSJan Schmidt  * tree blocks and <0 on error.
1800a542ad1bSJan Schmidt  */
1801a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
180269917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
180369917e43SLiu Bo 			u64 *flags_ret)
1804a542ad1bSJan Schmidt {
1805a542ad1bSJan Schmidt 	int ret;
1806a542ad1bSJan Schmidt 	u64 flags;
1807261c84b6SJosef Bacik 	u64 size = 0;
1808a542ad1bSJan Schmidt 	u32 item_size;
1809a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1810a542ad1bSJan Schmidt 	struct btrfs_extent_item *ei;
1811a542ad1bSJan Schmidt 	struct btrfs_key key;
1812a542ad1bSJan Schmidt 
1813261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1814261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1815261c84b6SJosef Bacik 	else
1816a542ad1bSJan Schmidt 		key.type = BTRFS_EXTENT_ITEM_KEY;
1817a542ad1bSJan Schmidt 	key.objectid = logical;
1818a542ad1bSJan Schmidt 	key.offset = (u64)-1;
1819a542ad1bSJan Schmidt 
1820a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1821a542ad1bSJan Schmidt 	if (ret < 0)
1822a542ad1bSJan Schmidt 		return ret;
1823a542ad1bSJan Schmidt 
1824850a8cdfSWang Shilong 	ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1825850a8cdfSWang Shilong 	if (ret) {
1826850a8cdfSWang Shilong 		if (ret > 0)
1827580f0a67SJosef Bacik 			ret = -ENOENT;
1828580f0a67SJosef Bacik 		return ret;
1829580f0a67SJosef Bacik 	}
1830850a8cdfSWang Shilong 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1831261c84b6SJosef Bacik 	if (found_key->type == BTRFS_METADATA_ITEM_KEY)
1832*da17066cSJeff Mahoney 		size = fs_info->nodesize;
1833261c84b6SJosef Bacik 	else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1834261c84b6SJosef Bacik 		size = found_key->offset;
1835261c84b6SJosef Bacik 
1836580f0a67SJosef Bacik 	if (found_key->objectid > logical ||
1837261c84b6SJosef Bacik 	    found_key->objectid + size <= logical) {
1838ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
1839ab8d0fc4SJeff Mahoney 			"logical %llu is not within any extent", logical);
1840a542ad1bSJan Schmidt 		return -ENOENT;
18414692cf58SJan Schmidt 	}
1842a542ad1bSJan Schmidt 
1843a542ad1bSJan Schmidt 	eb = path->nodes[0];
1844a542ad1bSJan Schmidt 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1845a542ad1bSJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
1846a542ad1bSJan Schmidt 
1847a542ad1bSJan Schmidt 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1848a542ad1bSJan Schmidt 	flags = btrfs_extent_flags(eb, ei);
1849a542ad1bSJan Schmidt 
1850ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info,
1851ab8d0fc4SJeff Mahoney 		"logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
1852c1c9ff7cSGeert Uytterhoeven 		 logical, logical - found_key->objectid, found_key->objectid,
1853c1c9ff7cSGeert Uytterhoeven 		 found_key->offset, flags, item_size);
185469917e43SLiu Bo 
185569917e43SLiu Bo 	WARN_ON(!flags_ret);
185669917e43SLiu Bo 	if (flags_ret) {
1857a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
185869917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
185969917e43SLiu Bo 		else if (flags & BTRFS_EXTENT_FLAG_DATA)
186069917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_DATA;
186169917e43SLiu Bo 		else
186269917e43SLiu Bo 			BUG_ON(1);
186369917e43SLiu Bo 		return 0;
186469917e43SLiu Bo 	}
1865a542ad1bSJan Schmidt 
1866a542ad1bSJan Schmidt 	return -EIO;
1867a542ad1bSJan Schmidt }
1868a542ad1bSJan Schmidt 
1869a542ad1bSJan Schmidt /*
1870a542ad1bSJan Schmidt  * helper function to iterate extent inline refs. ptr must point to a 0 value
1871a542ad1bSJan Schmidt  * for the first call and may be modified. it is used to track state.
1872a542ad1bSJan Schmidt  * if more refs exist, 0 is returned and the next call to
1873a542ad1bSJan Schmidt  * __get_extent_inline_ref must pass the modified ptr parameter to get the
1874a542ad1bSJan Schmidt  * next ref. after the last ref was processed, 1 is returned.
1875a542ad1bSJan Schmidt  * returns <0 on error
1876a542ad1bSJan Schmidt  */
1877a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
18786eda71d0SLiu Bo 				   struct btrfs_key *key,
1879a542ad1bSJan Schmidt 				   struct btrfs_extent_item *ei, u32 item_size,
1880a542ad1bSJan Schmidt 				   struct btrfs_extent_inline_ref **out_eiref,
1881a542ad1bSJan Schmidt 				   int *out_type)
1882a542ad1bSJan Schmidt {
1883a542ad1bSJan Schmidt 	unsigned long end;
1884a542ad1bSJan Schmidt 	u64 flags;
1885a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1886a542ad1bSJan Schmidt 
1887a542ad1bSJan Schmidt 	if (!*ptr) {
1888a542ad1bSJan Schmidt 		/* first call */
1889a542ad1bSJan Schmidt 		flags = btrfs_extent_flags(eb, ei);
1890a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
18916eda71d0SLiu Bo 			if (key->type == BTRFS_METADATA_ITEM_KEY) {
18926eda71d0SLiu Bo 				/* a skinny metadata extent */
18936eda71d0SLiu Bo 				*out_eiref =
18946eda71d0SLiu Bo 				     (struct btrfs_extent_inline_ref *)(ei + 1);
18956eda71d0SLiu Bo 			} else {
18966eda71d0SLiu Bo 				WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1897a542ad1bSJan Schmidt 				info = (struct btrfs_tree_block_info *)(ei + 1);
1898a542ad1bSJan Schmidt 				*out_eiref =
1899a542ad1bSJan Schmidt 				   (struct btrfs_extent_inline_ref *)(info + 1);
19006eda71d0SLiu Bo 			}
1901a542ad1bSJan Schmidt 		} else {
1902a542ad1bSJan Schmidt 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1903a542ad1bSJan Schmidt 		}
1904a542ad1bSJan Schmidt 		*ptr = (unsigned long)*out_eiref;
1905cd857dd6SLiu Bo 		if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
1906a542ad1bSJan Schmidt 			return -ENOENT;
1907a542ad1bSJan Schmidt 	}
1908a542ad1bSJan Schmidt 
1909a542ad1bSJan Schmidt 	end = (unsigned long)ei + item_size;
19106eda71d0SLiu Bo 	*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
1911a542ad1bSJan Schmidt 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1912a542ad1bSJan Schmidt 
1913a542ad1bSJan Schmidt 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1914a542ad1bSJan Schmidt 	WARN_ON(*ptr > end);
1915a542ad1bSJan Schmidt 	if (*ptr == end)
1916a542ad1bSJan Schmidt 		return 1; /* last */
1917a542ad1bSJan Schmidt 
1918a542ad1bSJan Schmidt 	return 0;
1919a542ad1bSJan Schmidt }
1920a542ad1bSJan Schmidt 
1921a542ad1bSJan Schmidt /*
1922a542ad1bSJan Schmidt  * reads the tree block backref for an extent. tree level and root are returned
1923a542ad1bSJan Schmidt  * through out_level and out_root. ptr must point to a 0 value for the first
1924a542ad1bSJan Schmidt  * call and may be modified (see __get_extent_inline_ref comment).
1925a542ad1bSJan Schmidt  * returns 0 if data was provided, 1 if there was no more data to provide or
1926a542ad1bSJan Schmidt  * <0 on error.
1927a542ad1bSJan Schmidt  */
1928a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
19296eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
19306eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level)
1931a542ad1bSJan Schmidt {
1932a542ad1bSJan Schmidt 	int ret;
1933a542ad1bSJan Schmidt 	int type;
1934a542ad1bSJan Schmidt 	struct btrfs_extent_inline_ref *eiref;
1935a542ad1bSJan Schmidt 
1936a542ad1bSJan Schmidt 	if (*ptr == (unsigned long)-1)
1937a542ad1bSJan Schmidt 		return 1;
1938a542ad1bSJan Schmidt 
1939a542ad1bSJan Schmidt 	while (1) {
19406eda71d0SLiu Bo 		ret = __get_extent_inline_ref(ptr, eb, key, ei, item_size,
1941a542ad1bSJan Schmidt 					      &eiref, &type);
1942a542ad1bSJan Schmidt 		if (ret < 0)
1943a542ad1bSJan Schmidt 			return ret;
1944a542ad1bSJan Schmidt 
1945a542ad1bSJan Schmidt 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1946a542ad1bSJan Schmidt 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1947a542ad1bSJan Schmidt 			break;
1948a542ad1bSJan Schmidt 
1949a542ad1bSJan Schmidt 		if (ret == 1)
1950a542ad1bSJan Schmidt 			return 1;
1951a542ad1bSJan Schmidt 	}
1952a542ad1bSJan Schmidt 
1953a542ad1bSJan Schmidt 	/* we can treat both ref types equally here */
1954a542ad1bSJan Schmidt 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1955a1317f45SFilipe Manana 
1956a1317f45SFilipe Manana 	if (key->type == BTRFS_EXTENT_ITEM_KEY) {
1957a1317f45SFilipe Manana 		struct btrfs_tree_block_info *info;
1958a1317f45SFilipe Manana 
1959a1317f45SFilipe Manana 		info = (struct btrfs_tree_block_info *)(ei + 1);
1960a542ad1bSJan Schmidt 		*out_level = btrfs_tree_block_level(eb, info);
1961a1317f45SFilipe Manana 	} else {
1962a1317f45SFilipe Manana 		ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
1963a1317f45SFilipe Manana 		*out_level = (u8)key->offset;
1964a1317f45SFilipe Manana 	}
1965a542ad1bSJan Schmidt 
1966a542ad1bSJan Schmidt 	if (ret == 1)
1967a542ad1bSJan Schmidt 		*ptr = (unsigned long)-1;
1968a542ad1bSJan Schmidt 
1969a542ad1bSJan Schmidt 	return 0;
1970a542ad1bSJan Schmidt }
1971a542ad1bSJan Schmidt 
1972ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
1973ab8d0fc4SJeff Mahoney 			     struct extent_inode_elem *inode_list,
1974976b1908SJan Schmidt 			     u64 root, u64 extent_item_objectid,
19754692cf58SJan Schmidt 			     iterate_extent_inodes_t *iterate, void *ctx)
1976a542ad1bSJan Schmidt {
1977976b1908SJan Schmidt 	struct extent_inode_elem *eie;
19784692cf58SJan Schmidt 	int ret = 0;
1979a542ad1bSJan Schmidt 
1980976b1908SJan Schmidt 	for (eie = inode_list; eie; eie = eie->next) {
1981ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
1982ab8d0fc4SJeff Mahoney 			    "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
1983ab8d0fc4SJeff Mahoney 			    extent_item_objectid, eie->inum,
1984ab8d0fc4SJeff Mahoney 			    eie->offset, root);
1985976b1908SJan Schmidt 		ret = iterate(eie->inum, eie->offset, root, ctx);
19864692cf58SJan Schmidt 		if (ret) {
1987ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_info,
1988ab8d0fc4SJeff Mahoney 				    "stopping iteration for %llu due to ret=%d",
1989976b1908SJan Schmidt 				    extent_item_objectid, ret);
1990a542ad1bSJan Schmidt 			break;
1991a542ad1bSJan Schmidt 		}
1992a542ad1bSJan Schmidt 	}
1993a542ad1bSJan Schmidt 
1994a542ad1bSJan Schmidt 	return ret;
1995a542ad1bSJan Schmidt }
1996a542ad1bSJan Schmidt 
1997a542ad1bSJan Schmidt /*
1998a542ad1bSJan Schmidt  * calls iterate() for every inode that references the extent identified by
19994692cf58SJan Schmidt  * the given parameters.
2000a542ad1bSJan Schmidt  * when the iterator function returns a non-zero value, iteration stops.
2001a542ad1bSJan Schmidt  */
2002a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
20034692cf58SJan Schmidt 				u64 extent_item_objectid, u64 extent_item_pos,
20047a3ae2f8SJan Schmidt 				int search_commit_root,
2005a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
2006a542ad1bSJan Schmidt {
2007a542ad1bSJan Schmidt 	int ret;
2008da61d31aSJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
20097a3ae2f8SJan Schmidt 	struct ulist *refs = NULL;
20107a3ae2f8SJan Schmidt 	struct ulist *roots = NULL;
20114692cf58SJan Schmidt 	struct ulist_node *ref_node = NULL;
20124692cf58SJan Schmidt 	struct ulist_node *root_node = NULL;
20133284da7bSDavid Sterba 	struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
2014cd1b413cSJan Schmidt 	struct ulist_iterator ref_uiter;
2015cd1b413cSJan Schmidt 	struct ulist_iterator root_uiter;
2016a542ad1bSJan Schmidt 
2017ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info, "resolving all inodes for extent %llu",
20184692cf58SJan Schmidt 			extent_item_objectid);
20194692cf58SJan Schmidt 
2020da61d31aSJosef Bacik 	if (!search_commit_root) {
20217a3ae2f8SJan Schmidt 		trans = btrfs_join_transaction(fs_info->extent_root);
20227a3ae2f8SJan Schmidt 		if (IS_ERR(trans))
20237a3ae2f8SJan Schmidt 			return PTR_ERR(trans);
20248445f61cSJan Schmidt 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
20259e351cc8SJosef Bacik 	} else {
20269e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
20277a3ae2f8SJan Schmidt 	}
20284692cf58SJan Schmidt 
20294692cf58SJan Schmidt 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
2030097b8a7cSJan Schmidt 				   tree_mod_seq_elem.seq, &refs,
20318445f61cSJan Schmidt 				   &extent_item_pos);
20324692cf58SJan Schmidt 	if (ret)
20334692cf58SJan Schmidt 		goto out;
20344692cf58SJan Schmidt 
2035cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&ref_uiter);
2036cd1b413cSJan Schmidt 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
20379e351cc8SJosef Bacik 		ret = __btrfs_find_all_roots(trans, fs_info, ref_node->val,
20388445f61cSJan Schmidt 					     tree_mod_seq_elem.seq, &roots);
20394692cf58SJan Schmidt 		if (ret)
2040a542ad1bSJan Schmidt 			break;
2041cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&root_uiter);
2042cd1b413cSJan Schmidt 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
2043ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_info,
2044ab8d0fc4SJeff Mahoney 				    "root %llu references leaf %llu, data list %#llx",
2045ab8d0fc4SJeff Mahoney 				    root_node->val, ref_node->val,
2046c1c9ff7cSGeert Uytterhoeven 				    ref_node->aux);
2047ab8d0fc4SJeff Mahoney 			ret = iterate_leaf_refs(fs_info,
2048ab8d0fc4SJeff Mahoney 						(struct extent_inode_elem *)
2049995e01b7SJan Schmidt 						(uintptr_t)ref_node->aux,
2050995e01b7SJan Schmidt 						root_node->val,
2051995e01b7SJan Schmidt 						extent_item_objectid,
2052a542ad1bSJan Schmidt 						iterate, ctx);
20534692cf58SJan Schmidt 		}
2054976b1908SJan Schmidt 		ulist_free(roots);
2055a542ad1bSJan Schmidt 	}
2056a542ad1bSJan Schmidt 
2057976b1908SJan Schmidt 	free_leaf_list(refs);
20584692cf58SJan Schmidt out:
20597a3ae2f8SJan Schmidt 	if (!search_commit_root) {
20608445f61cSJan Schmidt 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
20614692cf58SJan Schmidt 		btrfs_end_transaction(trans, fs_info->extent_root);
20629e351cc8SJosef Bacik 	} else {
20639e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
20647a3ae2f8SJan Schmidt 	}
20657a3ae2f8SJan Schmidt 
2066a542ad1bSJan Schmidt 	return ret;
2067a542ad1bSJan Schmidt }
2068a542ad1bSJan Schmidt 
2069a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
2070a542ad1bSJan Schmidt 				struct btrfs_path *path,
2071a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
2072a542ad1bSJan Schmidt {
2073a542ad1bSJan Schmidt 	int ret;
20744692cf58SJan Schmidt 	u64 extent_item_pos;
207569917e43SLiu Bo 	u64 flags = 0;
2076a542ad1bSJan Schmidt 	struct btrfs_key found_key;
20777a3ae2f8SJan Schmidt 	int search_commit_root = path->search_commit_root;
2078a542ad1bSJan Schmidt 
207969917e43SLiu Bo 	ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
20804692cf58SJan Schmidt 	btrfs_release_path(path);
2081a542ad1bSJan Schmidt 	if (ret < 0)
2082a542ad1bSJan Schmidt 		return ret;
208369917e43SLiu Bo 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
20843627bf45SStefan Behrens 		return -EINVAL;
2085a542ad1bSJan Schmidt 
20864692cf58SJan Schmidt 	extent_item_pos = logical - found_key.objectid;
20877a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
20887a3ae2f8SJan Schmidt 					extent_item_pos, search_commit_root,
20897a3ae2f8SJan Schmidt 					iterate, ctx);
2090a542ad1bSJan Schmidt 
2091a542ad1bSJan Schmidt 	return ret;
2092a542ad1bSJan Schmidt }
2093a542ad1bSJan Schmidt 
2094d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
2095d24bec3aSMark Fasheh 			      struct extent_buffer *eb, void *ctx);
2096d24bec3aSMark Fasheh 
2097d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
2098a542ad1bSJan Schmidt 			      struct btrfs_path *path,
2099a542ad1bSJan Schmidt 			      iterate_irefs_t *iterate, void *ctx)
2100a542ad1bSJan Schmidt {
2101aefc1eb1SJan Schmidt 	int ret = 0;
2102a542ad1bSJan Schmidt 	int slot;
2103a542ad1bSJan Schmidt 	u32 cur;
2104a542ad1bSJan Schmidt 	u32 len;
2105a542ad1bSJan Schmidt 	u32 name_len;
2106a542ad1bSJan Schmidt 	u64 parent = 0;
2107a542ad1bSJan Schmidt 	int found = 0;
2108a542ad1bSJan Schmidt 	struct extent_buffer *eb;
2109a542ad1bSJan Schmidt 	struct btrfs_item *item;
2110a542ad1bSJan Schmidt 	struct btrfs_inode_ref *iref;
2111a542ad1bSJan Schmidt 	struct btrfs_key found_key;
2112a542ad1bSJan Schmidt 
2113aefc1eb1SJan Schmidt 	while (!ret) {
2114c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, inum,
2115c234a24dSDavid Sterba 				parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
2116a542ad1bSJan Schmidt 				&found_key);
2117c234a24dSDavid Sterba 
2118a542ad1bSJan Schmidt 		if (ret < 0)
2119a542ad1bSJan Schmidt 			break;
2120a542ad1bSJan Schmidt 		if (ret) {
2121a542ad1bSJan Schmidt 			ret = found ? 0 : -ENOENT;
2122a542ad1bSJan Schmidt 			break;
2123a542ad1bSJan Schmidt 		}
2124a542ad1bSJan Schmidt 		++found;
2125a542ad1bSJan Schmidt 
2126a542ad1bSJan Schmidt 		parent = found_key.offset;
2127a542ad1bSJan Schmidt 		slot = path->slots[0];
21283fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
21293fe81ce2SFilipe David Borba Manana 		if (!eb) {
21303fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
21313fe81ce2SFilipe David Borba Manana 			break;
21323fe81ce2SFilipe David Borba Manana 		}
21333fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
2134b916a59aSJan Schmidt 		btrfs_tree_read_lock(eb);
2135b916a59aSJan Schmidt 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2136a542ad1bSJan Schmidt 		btrfs_release_path(path);
2137a542ad1bSJan Schmidt 
2138dd3cc16bSRoss Kirk 		item = btrfs_item_nr(slot);
2139a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
2140a542ad1bSJan Schmidt 
2141a542ad1bSJan Schmidt 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
2142a542ad1bSJan Schmidt 			name_len = btrfs_inode_ref_name_len(eb, iref);
2143a542ad1bSJan Schmidt 			/* path must be released before calling iterate()! */
2144ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_root->fs_info,
2145ab8d0fc4SJeff Mahoney 				"following ref at offset %u for inode %llu in tree %llu",
2146ab8d0fc4SJeff Mahoney 				cur, found_key.objectid, fs_root->objectid);
2147d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
2148d24bec3aSMark Fasheh 				      (unsigned long)(iref + 1), eb, ctx);
2149aefc1eb1SJan Schmidt 			if (ret)
2150a542ad1bSJan Schmidt 				break;
2151a542ad1bSJan Schmidt 			len = sizeof(*iref) + name_len;
2152a542ad1bSJan Schmidt 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
2153a542ad1bSJan Schmidt 		}
2154b916a59aSJan Schmidt 		btrfs_tree_read_unlock_blocking(eb);
2155a542ad1bSJan Schmidt 		free_extent_buffer(eb);
2156a542ad1bSJan Schmidt 	}
2157a542ad1bSJan Schmidt 
2158a542ad1bSJan Schmidt 	btrfs_release_path(path);
2159a542ad1bSJan Schmidt 
2160a542ad1bSJan Schmidt 	return ret;
2161a542ad1bSJan Schmidt }
2162a542ad1bSJan Schmidt 
2163d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
2164d24bec3aSMark Fasheh 				 struct btrfs_path *path,
2165d24bec3aSMark Fasheh 				 iterate_irefs_t *iterate, void *ctx)
2166d24bec3aSMark Fasheh {
2167d24bec3aSMark Fasheh 	int ret;
2168d24bec3aSMark Fasheh 	int slot;
2169d24bec3aSMark Fasheh 	u64 offset = 0;
2170d24bec3aSMark Fasheh 	u64 parent;
2171d24bec3aSMark Fasheh 	int found = 0;
2172d24bec3aSMark Fasheh 	struct extent_buffer *eb;
2173d24bec3aSMark Fasheh 	struct btrfs_inode_extref *extref;
2174d24bec3aSMark Fasheh 	u32 item_size;
2175d24bec3aSMark Fasheh 	u32 cur_offset;
2176d24bec3aSMark Fasheh 	unsigned long ptr;
2177d24bec3aSMark Fasheh 
2178d24bec3aSMark Fasheh 	while (1) {
2179d24bec3aSMark Fasheh 		ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
2180d24bec3aSMark Fasheh 					    &offset);
2181d24bec3aSMark Fasheh 		if (ret < 0)
2182d24bec3aSMark Fasheh 			break;
2183d24bec3aSMark Fasheh 		if (ret) {
2184d24bec3aSMark Fasheh 			ret = found ? 0 : -ENOENT;
2185d24bec3aSMark Fasheh 			break;
2186d24bec3aSMark Fasheh 		}
2187d24bec3aSMark Fasheh 		++found;
2188d24bec3aSMark Fasheh 
2189d24bec3aSMark Fasheh 		slot = path->slots[0];
21903fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
21913fe81ce2SFilipe David Borba Manana 		if (!eb) {
21923fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
21933fe81ce2SFilipe David Borba Manana 			break;
21943fe81ce2SFilipe David Borba Manana 		}
21953fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
2196d24bec3aSMark Fasheh 
2197d24bec3aSMark Fasheh 		btrfs_tree_read_lock(eb);
2198d24bec3aSMark Fasheh 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2199d24bec3aSMark Fasheh 		btrfs_release_path(path);
2200d24bec3aSMark Fasheh 
22012849a854SChris Mason 		item_size = btrfs_item_size_nr(eb, slot);
22022849a854SChris Mason 		ptr = btrfs_item_ptr_offset(eb, slot);
2203d24bec3aSMark Fasheh 		cur_offset = 0;
2204d24bec3aSMark Fasheh 
2205d24bec3aSMark Fasheh 		while (cur_offset < item_size) {
2206d24bec3aSMark Fasheh 			u32 name_len;
2207d24bec3aSMark Fasheh 
2208d24bec3aSMark Fasheh 			extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
2209d24bec3aSMark Fasheh 			parent = btrfs_inode_extref_parent(eb, extref);
2210d24bec3aSMark Fasheh 			name_len = btrfs_inode_extref_name_len(eb, extref);
2211d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
2212d24bec3aSMark Fasheh 				      (unsigned long)&extref->name, eb, ctx);
2213d24bec3aSMark Fasheh 			if (ret)
2214d24bec3aSMark Fasheh 				break;
2215d24bec3aSMark Fasheh 
22162849a854SChris Mason 			cur_offset += btrfs_inode_extref_name_len(eb, extref);
2217d24bec3aSMark Fasheh 			cur_offset += sizeof(*extref);
2218d24bec3aSMark Fasheh 		}
2219d24bec3aSMark Fasheh 		btrfs_tree_read_unlock_blocking(eb);
2220d24bec3aSMark Fasheh 		free_extent_buffer(eb);
2221d24bec3aSMark Fasheh 
2222d24bec3aSMark Fasheh 		offset++;
2223d24bec3aSMark Fasheh 	}
2224d24bec3aSMark Fasheh 
2225d24bec3aSMark Fasheh 	btrfs_release_path(path);
2226d24bec3aSMark Fasheh 
2227d24bec3aSMark Fasheh 	return ret;
2228d24bec3aSMark Fasheh }
2229d24bec3aSMark Fasheh 
2230d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
2231d24bec3aSMark Fasheh 			 struct btrfs_path *path, iterate_irefs_t *iterate,
2232d24bec3aSMark Fasheh 			 void *ctx)
2233d24bec3aSMark Fasheh {
2234d24bec3aSMark Fasheh 	int ret;
2235d24bec3aSMark Fasheh 	int found_refs = 0;
2236d24bec3aSMark Fasheh 
2237d24bec3aSMark Fasheh 	ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
2238d24bec3aSMark Fasheh 	if (!ret)
2239d24bec3aSMark Fasheh 		++found_refs;
2240d24bec3aSMark Fasheh 	else if (ret != -ENOENT)
2241d24bec3aSMark Fasheh 		return ret;
2242d24bec3aSMark Fasheh 
2243d24bec3aSMark Fasheh 	ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
2244d24bec3aSMark Fasheh 	if (ret == -ENOENT && found_refs)
2245d24bec3aSMark Fasheh 		return 0;
2246d24bec3aSMark Fasheh 
2247d24bec3aSMark Fasheh 	return ret;
2248d24bec3aSMark Fasheh }
2249d24bec3aSMark Fasheh 
2250a542ad1bSJan Schmidt /*
2251a542ad1bSJan Schmidt  * returns 0 if the path could be dumped (probably truncated)
2252a542ad1bSJan Schmidt  * returns <0 in case of an error
2253a542ad1bSJan Schmidt  */
2254d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
2255a542ad1bSJan Schmidt 			 struct extent_buffer *eb, void *ctx)
2256a542ad1bSJan Schmidt {
2257a542ad1bSJan Schmidt 	struct inode_fs_paths *ipath = ctx;
2258a542ad1bSJan Schmidt 	char *fspath;
2259a542ad1bSJan Schmidt 	char *fspath_min;
2260a542ad1bSJan Schmidt 	int i = ipath->fspath->elem_cnt;
2261a542ad1bSJan Schmidt 	const int s_ptr = sizeof(char *);
2262a542ad1bSJan Schmidt 	u32 bytes_left;
2263a542ad1bSJan Schmidt 
2264a542ad1bSJan Schmidt 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
2265a542ad1bSJan Schmidt 					ipath->fspath->bytes_left - s_ptr : 0;
2266a542ad1bSJan Schmidt 
2267740c3d22SChris Mason 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
226896b5bd77SJan Schmidt 	fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
226996b5bd77SJan Schmidt 				   name_off, eb, inum, fspath_min, bytes_left);
2270a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
2271a542ad1bSJan Schmidt 		return PTR_ERR(fspath);
2272a542ad1bSJan Schmidt 
2273a542ad1bSJan Schmidt 	if (fspath > fspath_min) {
2274745c4d8eSJeff Mahoney 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
2275a542ad1bSJan Schmidt 		++ipath->fspath->elem_cnt;
2276a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = fspath - fspath_min;
2277a542ad1bSJan Schmidt 	} else {
2278a542ad1bSJan Schmidt 		++ipath->fspath->elem_missed;
2279a542ad1bSJan Schmidt 		ipath->fspath->bytes_missing += fspath_min - fspath;
2280a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = 0;
2281a542ad1bSJan Schmidt 	}
2282a542ad1bSJan Schmidt 
2283a542ad1bSJan Schmidt 	return 0;
2284a542ad1bSJan Schmidt }
2285a542ad1bSJan Schmidt 
2286a542ad1bSJan Schmidt /*
2287a542ad1bSJan Schmidt  * this dumps all file system paths to the inode into the ipath struct, provided
2288a542ad1bSJan Schmidt  * is has been created large enough. each path is zero-terminated and accessed
2289740c3d22SChris Mason  * from ipath->fspath->val[i].
2290a542ad1bSJan Schmidt  * when it returns, there are ipath->fspath->elem_cnt number of paths available
2291740c3d22SChris Mason  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
229201327610SNicholas D Steeves  * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
2293a542ad1bSJan Schmidt  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
2294a542ad1bSJan Schmidt  * have been needed to return all paths.
2295a542ad1bSJan Schmidt  */
2296a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
2297a542ad1bSJan Schmidt {
2298a542ad1bSJan Schmidt 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
2299a542ad1bSJan Schmidt 			     inode_to_path, ipath);
2300a542ad1bSJan Schmidt }
2301a542ad1bSJan Schmidt 
2302a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes)
2303a542ad1bSJan Schmidt {
2304a542ad1bSJan Schmidt 	struct btrfs_data_container *data;
2305a542ad1bSJan Schmidt 	size_t alloc_bytes;
2306a542ad1bSJan Schmidt 
2307a542ad1bSJan Schmidt 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
2308425d17a2SLiu Bo 	data = vmalloc(alloc_bytes);
2309a542ad1bSJan Schmidt 	if (!data)
2310a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2311a542ad1bSJan Schmidt 
2312a542ad1bSJan Schmidt 	if (total_bytes >= sizeof(*data)) {
2313a542ad1bSJan Schmidt 		data->bytes_left = total_bytes - sizeof(*data);
2314a542ad1bSJan Schmidt 		data->bytes_missing = 0;
2315a542ad1bSJan Schmidt 	} else {
2316a542ad1bSJan Schmidt 		data->bytes_missing = sizeof(*data) - total_bytes;
2317a542ad1bSJan Schmidt 		data->bytes_left = 0;
2318a542ad1bSJan Schmidt 	}
2319a542ad1bSJan Schmidt 
2320a542ad1bSJan Schmidt 	data->elem_cnt = 0;
2321a542ad1bSJan Schmidt 	data->elem_missed = 0;
2322a542ad1bSJan Schmidt 
2323a542ad1bSJan Schmidt 	return data;
2324a542ad1bSJan Schmidt }
2325a542ad1bSJan Schmidt 
2326a542ad1bSJan Schmidt /*
2327a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
2328a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
2329a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
2330a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
2331a542ad1bSJan Schmidt  */
2332a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
2333a542ad1bSJan Schmidt 					struct btrfs_path *path)
2334a542ad1bSJan Schmidt {
2335a542ad1bSJan Schmidt 	struct inode_fs_paths *ifp;
2336a542ad1bSJan Schmidt 	struct btrfs_data_container *fspath;
2337a542ad1bSJan Schmidt 
2338a542ad1bSJan Schmidt 	fspath = init_data_container(total_bytes);
2339a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
2340a542ad1bSJan Schmidt 		return (void *)fspath;
2341a542ad1bSJan Schmidt 
2342a542ad1bSJan Schmidt 	ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
2343a542ad1bSJan Schmidt 	if (!ifp) {
234472928f24SVincent Stehlé 		vfree(fspath);
2345a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2346a542ad1bSJan Schmidt 	}
2347a542ad1bSJan Schmidt 
2348a542ad1bSJan Schmidt 	ifp->btrfs_path = path;
2349a542ad1bSJan Schmidt 	ifp->fspath = fspath;
2350a542ad1bSJan Schmidt 	ifp->fs_root = fs_root;
2351a542ad1bSJan Schmidt 
2352a542ad1bSJan Schmidt 	return ifp;
2353a542ad1bSJan Schmidt }
2354a542ad1bSJan Schmidt 
2355a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath)
2356a542ad1bSJan Schmidt {
23574735fb28SJesper Juhl 	if (!ipath)
23584735fb28SJesper Juhl 		return;
2359425d17a2SLiu Bo 	vfree(ipath->fspath);
2360a542ad1bSJan Schmidt 	kfree(ipath);
2361a542ad1bSJan Schmidt }
2362