xref: /openbmc/linux/fs/btrfs/backref.c (revision 00142756e1f8015d2f8ce96532d156689db7e448)
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 
19f54de068SDavid Sterba #include <linux/mm.h>
20afce772eSLu Fengqi #include <linux/rbtree.h>
21*00142756SJeff Mahoney #include <trace/events/btrfs.h>
22a542ad1bSJan Schmidt #include "ctree.h"
23a542ad1bSJan Schmidt #include "disk-io.h"
24a542ad1bSJan Schmidt #include "backref.h"
258da6d581SJan Schmidt #include "ulist.h"
268da6d581SJan Schmidt #include "transaction.h"
278da6d581SJan Schmidt #include "delayed-ref.h"
28b916a59aSJan Schmidt #include "locking.h"
29a542ad1bSJan Schmidt 
30dc046b10SJosef Bacik /* Just an arbitrary number so we can be sure this happened */
31dc046b10SJosef Bacik #define BACKREF_FOUND_SHARED 6
32dc046b10SJosef Bacik 
33976b1908SJan Schmidt struct extent_inode_elem {
34976b1908SJan Schmidt 	u64 inum;
35976b1908SJan Schmidt 	u64 offset;
36976b1908SJan Schmidt 	struct extent_inode_elem *next;
37976b1908SJan Schmidt };
38976b1908SJan Schmidt 
3973980becSJeff Mahoney static int check_extent_in_eb(const struct btrfs_key *key,
4073980becSJeff Mahoney 			      const struct extent_buffer *eb,
4173980becSJeff Mahoney 			      const struct btrfs_file_extent_item *fi,
42976b1908SJan Schmidt 			      u64 extent_item_pos,
43976b1908SJan Schmidt 			      struct extent_inode_elem **eie)
44976b1908SJan Schmidt {
458ca15e05SJosef Bacik 	u64 offset = 0;
468ca15e05SJosef Bacik 	struct extent_inode_elem *e;
478ca15e05SJosef Bacik 
488ca15e05SJosef Bacik 	if (!btrfs_file_extent_compression(eb, fi) &&
498ca15e05SJosef Bacik 	    !btrfs_file_extent_encryption(eb, fi) &&
508ca15e05SJosef Bacik 	    !btrfs_file_extent_other_encoding(eb, fi)) {
51976b1908SJan Schmidt 		u64 data_offset;
52976b1908SJan Schmidt 		u64 data_len;
53976b1908SJan Schmidt 
54976b1908SJan Schmidt 		data_offset = btrfs_file_extent_offset(eb, fi);
55976b1908SJan Schmidt 		data_len = btrfs_file_extent_num_bytes(eb, fi);
56976b1908SJan Schmidt 
57976b1908SJan Schmidt 		if (extent_item_pos < data_offset ||
58976b1908SJan Schmidt 		    extent_item_pos >= data_offset + data_len)
59976b1908SJan Schmidt 			return 1;
608ca15e05SJosef Bacik 		offset = extent_item_pos - data_offset;
618ca15e05SJosef Bacik 	}
62976b1908SJan Schmidt 
63976b1908SJan Schmidt 	e = kmalloc(sizeof(*e), GFP_NOFS);
64976b1908SJan Schmidt 	if (!e)
65976b1908SJan Schmidt 		return -ENOMEM;
66976b1908SJan Schmidt 
67976b1908SJan Schmidt 	e->next = *eie;
68976b1908SJan Schmidt 	e->inum = key->objectid;
698ca15e05SJosef Bacik 	e->offset = key->offset + offset;
70976b1908SJan Schmidt 	*eie = e;
71976b1908SJan Schmidt 
72976b1908SJan Schmidt 	return 0;
73976b1908SJan Schmidt }
74976b1908SJan Schmidt 
75f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie)
76f05c4746SWang Shilong {
77f05c4746SWang Shilong 	struct extent_inode_elem *eie_next;
78f05c4746SWang Shilong 
79f05c4746SWang Shilong 	for (; eie; eie = eie_next) {
80f05c4746SWang Shilong 		eie_next = eie->next;
81f05c4746SWang Shilong 		kfree(eie);
82f05c4746SWang Shilong 	}
83f05c4746SWang Shilong }
84f05c4746SWang Shilong 
8573980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb,
8673980becSJeff Mahoney 			     u64 wanted_disk_byte, u64 extent_item_pos,
87976b1908SJan Schmidt 			     struct extent_inode_elem **eie)
88976b1908SJan Schmidt {
89976b1908SJan Schmidt 	u64 disk_byte;
90976b1908SJan Schmidt 	struct btrfs_key key;
91976b1908SJan Schmidt 	struct btrfs_file_extent_item *fi;
92976b1908SJan Schmidt 	int slot;
93976b1908SJan Schmidt 	int nritems;
94976b1908SJan Schmidt 	int extent_type;
95976b1908SJan Schmidt 	int ret;
96976b1908SJan Schmidt 
97976b1908SJan Schmidt 	/*
98976b1908SJan Schmidt 	 * from the shared data ref, we only have the leaf but we need
99976b1908SJan Schmidt 	 * the key. thus, we must look into all items and see that we
100976b1908SJan Schmidt 	 * find one (some) with a reference to our extent item.
101976b1908SJan Schmidt 	 */
102976b1908SJan Schmidt 	nritems = btrfs_header_nritems(eb);
103976b1908SJan Schmidt 	for (slot = 0; slot < nritems; ++slot) {
104976b1908SJan Schmidt 		btrfs_item_key_to_cpu(eb, &key, slot);
105976b1908SJan Schmidt 		if (key.type != BTRFS_EXTENT_DATA_KEY)
106976b1908SJan Schmidt 			continue;
107976b1908SJan Schmidt 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
108976b1908SJan Schmidt 		extent_type = btrfs_file_extent_type(eb, fi);
109976b1908SJan Schmidt 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
110976b1908SJan Schmidt 			continue;
111976b1908SJan Schmidt 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
112976b1908SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
113976b1908SJan Schmidt 		if (disk_byte != wanted_disk_byte)
114976b1908SJan Schmidt 			continue;
115976b1908SJan Schmidt 
116976b1908SJan Schmidt 		ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie);
117976b1908SJan Schmidt 		if (ret < 0)
118976b1908SJan Schmidt 			return ret;
119976b1908SJan Schmidt 	}
120976b1908SJan Schmidt 
121976b1908SJan Schmidt 	return 0;
122976b1908SJan Schmidt }
123976b1908SJan Schmidt 
12486d5f994SEdmund Nadolski struct preftree {
12586d5f994SEdmund Nadolski 	struct rb_root root;
1266c336b21SJeff Mahoney 	unsigned int count;
12786d5f994SEdmund Nadolski };
12886d5f994SEdmund Nadolski 
1296c336b21SJeff Mahoney #define PREFTREE_INIT	{ .root = RB_ROOT, .count = 0 }
13086d5f994SEdmund Nadolski 
13186d5f994SEdmund Nadolski struct preftrees {
13286d5f994SEdmund Nadolski 	struct preftree direct;    /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
13386d5f994SEdmund Nadolski 	struct preftree indirect;  /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
13486d5f994SEdmund Nadolski 	struct preftree indirect_missing_keys;
13586d5f994SEdmund Nadolski };
13686d5f994SEdmund Nadolski 
137b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache;
138b9e9a6cbSWang Shilong 
139b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void)
140b9e9a6cbSWang Shilong {
141b9e9a6cbSWang Shilong 	btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
142e0c476b1SJeff Mahoney 					sizeof(struct prelim_ref),
143b9e9a6cbSWang Shilong 					0,
144fba4b697SNikolay Borisov 					SLAB_MEM_SPREAD,
145b9e9a6cbSWang Shilong 					NULL);
146b9e9a6cbSWang Shilong 	if (!btrfs_prelim_ref_cache)
147b9e9a6cbSWang Shilong 		return -ENOMEM;
148b9e9a6cbSWang Shilong 	return 0;
149b9e9a6cbSWang Shilong }
150b9e9a6cbSWang Shilong 
151b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void)
152b9e9a6cbSWang Shilong {
153b9e9a6cbSWang Shilong 	kmem_cache_destroy(btrfs_prelim_ref_cache);
154b9e9a6cbSWang Shilong }
155b9e9a6cbSWang Shilong 
15686d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref)
15786d5f994SEdmund Nadolski {
15886d5f994SEdmund Nadolski 	kmem_cache_free(btrfs_prelim_ref_cache, ref);
15986d5f994SEdmund Nadolski }
16086d5f994SEdmund Nadolski 
16186d5f994SEdmund Nadolski /*
16286d5f994SEdmund Nadolski  * Return 0 when both refs are for the same block (and can be merged).
16386d5f994SEdmund Nadolski  * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
16486d5f994SEdmund Nadolski  * indicates a 'higher' block.
16586d5f994SEdmund Nadolski  */
16686d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1,
16786d5f994SEdmund Nadolski 			      struct prelim_ref *ref2)
16886d5f994SEdmund Nadolski {
16986d5f994SEdmund Nadolski 	if (ref1->level < ref2->level)
17086d5f994SEdmund Nadolski 		return -1;
17186d5f994SEdmund Nadolski 	if (ref1->level > ref2->level)
17286d5f994SEdmund Nadolski 		return 1;
17386d5f994SEdmund Nadolski 	if (ref1->root_id < ref2->root_id)
17486d5f994SEdmund Nadolski 		return -1;
17586d5f994SEdmund Nadolski 	if (ref1->root_id > ref2->root_id)
17686d5f994SEdmund Nadolski 		return 1;
17786d5f994SEdmund Nadolski 	if (ref1->key_for_search.type < ref2->key_for_search.type)
17886d5f994SEdmund Nadolski 		return -1;
17986d5f994SEdmund Nadolski 	if (ref1->key_for_search.type > ref2->key_for_search.type)
18086d5f994SEdmund Nadolski 		return 1;
18186d5f994SEdmund Nadolski 	if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
18286d5f994SEdmund Nadolski 		return -1;
18386d5f994SEdmund Nadolski 	if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
18486d5f994SEdmund Nadolski 		return 1;
18586d5f994SEdmund Nadolski 	if (ref1->key_for_search.offset < ref2->key_for_search.offset)
18686d5f994SEdmund Nadolski 		return -1;
18786d5f994SEdmund Nadolski 	if (ref1->key_for_search.offset > ref2->key_for_search.offset)
18886d5f994SEdmund Nadolski 		return 1;
18986d5f994SEdmund Nadolski 	if (ref1->parent < ref2->parent)
19086d5f994SEdmund Nadolski 		return -1;
19186d5f994SEdmund Nadolski 	if (ref1->parent > ref2->parent)
19286d5f994SEdmund Nadolski 		return 1;
19386d5f994SEdmund Nadolski 
19486d5f994SEdmund Nadolski 	return 0;
19586d5f994SEdmund Nadolski }
19686d5f994SEdmund Nadolski 
19786d5f994SEdmund Nadolski /*
19886d5f994SEdmund Nadolski  * Add @newref to the @root rbtree, merging identical refs.
19986d5f994SEdmund Nadolski  *
20086d5f994SEdmund Nadolski  * Callers should assumed that newref has been freed after calling.
20186d5f994SEdmund Nadolski  */
202*00142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
203*00142756SJeff Mahoney 			      struct preftree *preftree,
20486d5f994SEdmund Nadolski 			      struct prelim_ref *newref)
20586d5f994SEdmund Nadolski {
20686d5f994SEdmund Nadolski 	struct rb_root *root;
20786d5f994SEdmund Nadolski 	struct rb_node **p;
20886d5f994SEdmund Nadolski 	struct rb_node *parent = NULL;
20986d5f994SEdmund Nadolski 	struct prelim_ref *ref;
21086d5f994SEdmund Nadolski 	int result;
21186d5f994SEdmund Nadolski 
21286d5f994SEdmund Nadolski 	root = &preftree->root;
21386d5f994SEdmund Nadolski 	p = &root->rb_node;
21486d5f994SEdmund Nadolski 
21586d5f994SEdmund Nadolski 	while (*p) {
21686d5f994SEdmund Nadolski 		parent = *p;
21786d5f994SEdmund Nadolski 		ref = rb_entry(parent, struct prelim_ref, rbnode);
21886d5f994SEdmund Nadolski 		result = prelim_ref_compare(ref, newref);
21986d5f994SEdmund Nadolski 		if (result < 0) {
22086d5f994SEdmund Nadolski 			p = &(*p)->rb_left;
22186d5f994SEdmund Nadolski 		} else if (result > 0) {
22286d5f994SEdmund Nadolski 			p = &(*p)->rb_right;
22386d5f994SEdmund Nadolski 		} else {
22486d5f994SEdmund Nadolski 			/* Identical refs, merge them and free @newref */
22586d5f994SEdmund Nadolski 			struct extent_inode_elem *eie = ref->inode_list;
22686d5f994SEdmund Nadolski 
22786d5f994SEdmund Nadolski 			while (eie && eie->next)
22886d5f994SEdmund Nadolski 				eie = eie->next;
22986d5f994SEdmund Nadolski 
23086d5f994SEdmund Nadolski 			if (!eie)
23186d5f994SEdmund Nadolski 				ref->inode_list = newref->inode_list;
23286d5f994SEdmund Nadolski 			else
23386d5f994SEdmund Nadolski 				eie->next = newref->inode_list;
234*00142756SJeff Mahoney 			trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
235*00142756SJeff Mahoney 						     preftree->count);
23686d5f994SEdmund Nadolski 			ref->count += newref->count;
23786d5f994SEdmund Nadolski 			free_pref(newref);
23886d5f994SEdmund Nadolski 			return;
23986d5f994SEdmund Nadolski 		}
24086d5f994SEdmund Nadolski 	}
24186d5f994SEdmund Nadolski 
2426c336b21SJeff Mahoney 	preftree->count++;
243*00142756SJeff Mahoney 	trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
24486d5f994SEdmund Nadolski 	rb_link_node(&newref->rbnode, parent, p);
24586d5f994SEdmund Nadolski 	rb_insert_color(&newref->rbnode, root);
24686d5f994SEdmund Nadolski }
24786d5f994SEdmund Nadolski 
24886d5f994SEdmund Nadolski /*
24986d5f994SEdmund Nadolski  * Release the entire tree.  We don't care about internal consistency so
25086d5f994SEdmund Nadolski  * just free everything and then reset the tree root.
25186d5f994SEdmund Nadolski  */
25286d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree)
25386d5f994SEdmund Nadolski {
25486d5f994SEdmund Nadolski 	struct prelim_ref *ref, *next_ref;
25586d5f994SEdmund Nadolski 
25686d5f994SEdmund Nadolski 	rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root,
25786d5f994SEdmund Nadolski 					     rbnode)
25886d5f994SEdmund Nadolski 		free_pref(ref);
25986d5f994SEdmund Nadolski 
26086d5f994SEdmund Nadolski 	preftree->root = RB_ROOT;
2616c336b21SJeff Mahoney 	preftree->count = 0;
26286d5f994SEdmund Nadolski }
26386d5f994SEdmund Nadolski 
264d5c88b73SJan Schmidt /*
265d5c88b73SJan Schmidt  * the rules for all callers of this function are:
266d5c88b73SJan Schmidt  * - obtaining the parent is the goal
267d5c88b73SJan Schmidt  * - if you add a key, you must know that it is a correct key
268d5c88b73SJan Schmidt  * - if you cannot add the parent or a correct key, then we will look into the
269d5c88b73SJan Schmidt  *   block later to set a correct key
270d5c88b73SJan Schmidt  *
271d5c88b73SJan Schmidt  * delayed refs
272d5c88b73SJan Schmidt  * ============
273d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
274d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
275d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
276d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    -   |     -
277d5c88b73SJan Schmidt  *      key to resolve |    -   |     y    |    y   |     y
278d5c88b73SJan Schmidt  *  tree block logical |    -   |     -    |    -   |     -
279d5c88b73SJan Schmidt  *  root for resolving |    y   |     y    |    y   |     y
280d5c88b73SJan Schmidt  *
281d5c88b73SJan Schmidt  * - column 1:       we've the parent -> done
282d5c88b73SJan Schmidt  * - column 2, 3, 4: we use the key to find the parent
283d5c88b73SJan Schmidt  *
284d5c88b73SJan Schmidt  * on disk refs (inline or keyed)
285d5c88b73SJan Schmidt  * ==============================
286d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
287d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
288d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
289d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    y   |     -
290d5c88b73SJan Schmidt  *      key to resolve |    -   |     -    |    -   |     y
291d5c88b73SJan Schmidt  *  tree block logical |    y   |     y    |    y   |     y
292d5c88b73SJan Schmidt  *  root for resolving |    -   |     y    |    y   |     y
293d5c88b73SJan Schmidt  *
294d5c88b73SJan Schmidt  * - column 1, 3: we've the parent -> done
295d5c88b73SJan Schmidt  * - column 2:    we take the first key from the block to find the parent
296e0c476b1SJeff Mahoney  *                (see add_missing_keys)
297d5c88b73SJan Schmidt  * - column 4:    we use the key to find the parent
298d5c88b73SJan Schmidt  *
299d5c88b73SJan Schmidt  * additional information that's available but not required to find the parent
300d5c88b73SJan Schmidt  * block might help in merging entries to gain some speed.
301d5c88b73SJan Schmidt  */
302*00142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
303*00142756SJeff Mahoney 			  struct preftree *preftree, u64 root_id,
304e0c476b1SJeff Mahoney 			  const struct btrfs_key *key, int level, u64 parent,
305e0c476b1SJeff Mahoney 			  u64 wanted_disk_byte, int count, gfp_t gfp_mask)
3068da6d581SJan Schmidt {
307e0c476b1SJeff Mahoney 	struct prelim_ref *ref;
3088da6d581SJan Schmidt 
30948ec4736SLiu Bo 	if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
31048ec4736SLiu Bo 		return 0;
31148ec4736SLiu Bo 
312b9e9a6cbSWang Shilong 	ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
3138da6d581SJan Schmidt 	if (!ref)
3148da6d581SJan Schmidt 		return -ENOMEM;
3158da6d581SJan Schmidt 
3168da6d581SJan Schmidt 	ref->root_id = root_id;
317d6589101SFilipe Manana 	if (key) {
318d5c88b73SJan Schmidt 		ref->key_for_search = *key;
319d6589101SFilipe Manana 		/*
320d6589101SFilipe Manana 		 * We can often find data backrefs with an offset that is too
321d6589101SFilipe Manana 		 * large (>= LLONG_MAX, maximum allowed file offset) due to
322d6589101SFilipe Manana 		 * underflows when subtracting a file's offset with the data
323d6589101SFilipe Manana 		 * offset of its corresponding extent data item. This can
324d6589101SFilipe Manana 		 * happen for example in the clone ioctl.
325d6589101SFilipe Manana 		 * So if we detect such case we set the search key's offset to
326d6589101SFilipe Manana 		 * zero to make sure we will find the matching file extent item
327d6589101SFilipe Manana 		 * at add_all_parents(), otherwise we will miss it because the
328d6589101SFilipe Manana 		 * offset taken form the backref is much larger then the offset
329d6589101SFilipe Manana 		 * of the file extent item. This can make us scan a very large
330d6589101SFilipe Manana 		 * number of file extent items, but at least it will not make
331d6589101SFilipe Manana 		 * us miss any.
332d6589101SFilipe Manana 		 * This is an ugly workaround for a behaviour that should have
333d6589101SFilipe Manana 		 * never existed, but it does and a fix for the clone ioctl
334d6589101SFilipe Manana 		 * would touch a lot of places, cause backwards incompatibility
335d6589101SFilipe Manana 		 * and would not fix the problem for extents cloned with older
336d6589101SFilipe Manana 		 * kernels.
337d6589101SFilipe Manana 		 */
338d6589101SFilipe Manana 		if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
339d6589101SFilipe Manana 		    ref->key_for_search.offset >= LLONG_MAX)
340d6589101SFilipe Manana 			ref->key_for_search.offset = 0;
341d6589101SFilipe Manana 	} else {
342d5c88b73SJan Schmidt 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
343d6589101SFilipe Manana 	}
3448da6d581SJan Schmidt 
3453301958bSJan Schmidt 	ref->inode_list = NULL;
3468da6d581SJan Schmidt 	ref->level = level;
3478da6d581SJan Schmidt 	ref->count = count;
3488da6d581SJan Schmidt 	ref->parent = parent;
3498da6d581SJan Schmidt 	ref->wanted_disk_byte = wanted_disk_byte;
350*00142756SJeff Mahoney 	prelim_ref_insert(fs_info, preftree, ref);
3518da6d581SJan Schmidt 
3528da6d581SJan Schmidt 	return 0;
3538da6d581SJan Schmidt }
3548da6d581SJan Schmidt 
35586d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */
356*00142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info,
357*00142756SJeff Mahoney 			  struct preftrees *preftrees, int level, u64 parent,
35886d5f994SEdmund Nadolski 			  u64 wanted_disk_byte, int count, gfp_t gfp_mask)
35986d5f994SEdmund Nadolski {
360*00142756SJeff Mahoney 	return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
361*00142756SJeff Mahoney 			      parent, wanted_disk_byte, count, gfp_mask);
36286d5f994SEdmund Nadolski }
36386d5f994SEdmund Nadolski 
36486d5f994SEdmund Nadolski /* indirect refs use parent == 0 */
365*00142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
366*00142756SJeff Mahoney 			    struct preftrees *preftrees, u64 root_id,
36786d5f994SEdmund Nadolski 			    const struct btrfs_key *key, int level,
36886d5f994SEdmund Nadolski 			    u64 wanted_disk_byte, int count, gfp_t gfp_mask)
36986d5f994SEdmund Nadolski {
37086d5f994SEdmund Nadolski 	struct preftree *tree = &preftrees->indirect;
37186d5f994SEdmund Nadolski 
37286d5f994SEdmund Nadolski 	if (!key)
37386d5f994SEdmund Nadolski 		tree = &preftrees->indirect_missing_keys;
374*00142756SJeff Mahoney 	return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
37586d5f994SEdmund Nadolski 			      wanted_disk_byte, count, gfp_mask);
37686d5f994SEdmund Nadolski }
37786d5f994SEdmund Nadolski 
3788da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
379e0c476b1SJeff Mahoney 			   struct ulist *parents, struct prelim_ref *ref,
38044853868SJosef Bacik 			   int level, u64 time_seq, const u64 *extent_item_pos,
38144853868SJosef Bacik 			   u64 total_refs)
3828da6d581SJan Schmidt {
38369bca40dSAlexander Block 	int ret = 0;
38469bca40dSAlexander Block 	int slot;
38569bca40dSAlexander Block 	struct extent_buffer *eb;
38669bca40dSAlexander Block 	struct btrfs_key key;
3877ef81ac8SJosef Bacik 	struct btrfs_key *key_for_search = &ref->key_for_search;
3888da6d581SJan Schmidt 	struct btrfs_file_extent_item *fi;
389ed8c4913SJosef Bacik 	struct extent_inode_elem *eie = NULL, *old = NULL;
3908da6d581SJan Schmidt 	u64 disk_byte;
3917ef81ac8SJosef Bacik 	u64 wanted_disk_byte = ref->wanted_disk_byte;
3927ef81ac8SJosef Bacik 	u64 count = 0;
3938da6d581SJan Schmidt 
39469bca40dSAlexander Block 	if (level != 0) {
39569bca40dSAlexander Block 		eb = path->nodes[level];
39669bca40dSAlexander Block 		ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
3973301958bSJan Schmidt 		if (ret < 0)
3983301958bSJan Schmidt 			return ret;
3998da6d581SJan Schmidt 		return 0;
40069bca40dSAlexander Block 	}
4018da6d581SJan Schmidt 
4028da6d581SJan Schmidt 	/*
40369bca40dSAlexander Block 	 * We normally enter this function with the path already pointing to
40469bca40dSAlexander Block 	 * the first item to check. But sometimes, we may enter it with
40569bca40dSAlexander Block 	 * slot==nritems. In that case, go to the next leaf before we continue.
4068da6d581SJan Schmidt 	 */
40721633fc6SQu Wenruo 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
408de47c9d3SEdmund Nadolski 		if (time_seq == SEQ_LAST)
40921633fc6SQu Wenruo 			ret = btrfs_next_leaf(root, path);
41021633fc6SQu Wenruo 		else
4113d7806ecSJan Schmidt 			ret = btrfs_next_old_leaf(root, path, time_seq);
41221633fc6SQu Wenruo 	}
4138da6d581SJan Schmidt 
41444853868SJosef Bacik 	while (!ret && count < total_refs) {
4158da6d581SJan Schmidt 		eb = path->nodes[0];
41669bca40dSAlexander Block 		slot = path->slots[0];
41769bca40dSAlexander Block 
41869bca40dSAlexander Block 		btrfs_item_key_to_cpu(eb, &key, slot);
41969bca40dSAlexander Block 
42069bca40dSAlexander Block 		if (key.objectid != key_for_search->objectid ||
42169bca40dSAlexander Block 		    key.type != BTRFS_EXTENT_DATA_KEY)
42269bca40dSAlexander Block 			break;
42369bca40dSAlexander Block 
42469bca40dSAlexander Block 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4258da6d581SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
42669bca40dSAlexander Block 
42769bca40dSAlexander Block 		if (disk_byte == wanted_disk_byte) {
42869bca40dSAlexander Block 			eie = NULL;
429ed8c4913SJosef Bacik 			old = NULL;
4307ef81ac8SJosef Bacik 			count++;
43169bca40dSAlexander Block 			if (extent_item_pos) {
43269bca40dSAlexander Block 				ret = check_extent_in_eb(&key, eb, fi,
43369bca40dSAlexander Block 						*extent_item_pos,
43469bca40dSAlexander Block 						&eie);
43569bca40dSAlexander Block 				if (ret < 0)
43669bca40dSAlexander Block 					break;
4378da6d581SJan Schmidt 			}
438ed8c4913SJosef Bacik 			if (ret > 0)
439ed8c4913SJosef Bacik 				goto next;
4404eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(parents, eb->start,
4414eb1f66dSTakashi Iwai 						  eie, (void **)&old, GFP_NOFS);
44269bca40dSAlexander Block 			if (ret < 0)
44369bca40dSAlexander Block 				break;
444ed8c4913SJosef Bacik 			if (!ret && extent_item_pos) {
445ed8c4913SJosef Bacik 				while (old->next)
446ed8c4913SJosef Bacik 					old = old->next;
447ed8c4913SJosef Bacik 				old->next = eie;
44869bca40dSAlexander Block 			}
449f05c4746SWang Shilong 			eie = NULL;
45069bca40dSAlexander Block 		}
451ed8c4913SJosef Bacik next:
452de47c9d3SEdmund Nadolski 		if (time_seq == SEQ_LAST)
45321633fc6SQu Wenruo 			ret = btrfs_next_item(root, path);
45421633fc6SQu Wenruo 		else
45569bca40dSAlexander Block 			ret = btrfs_next_old_item(root, path, time_seq);
4568da6d581SJan Schmidt 	}
4578da6d581SJan Schmidt 
45869bca40dSAlexander Block 	if (ret > 0)
45969bca40dSAlexander Block 		ret = 0;
460f05c4746SWang Shilong 	else if (ret < 0)
461f05c4746SWang Shilong 		free_inode_elem_list(eie);
46269bca40dSAlexander Block 	return ret;
4638da6d581SJan Schmidt }
4648da6d581SJan Schmidt 
4658da6d581SJan Schmidt /*
4668da6d581SJan Schmidt  * resolve an indirect backref in the form (root_id, key, level)
4678da6d581SJan Schmidt  * to a logical address
4688da6d581SJan Schmidt  */
469e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
470da61d31aSJosef Bacik 				struct btrfs_path *path, u64 time_seq,
471e0c476b1SJeff Mahoney 				struct prelim_ref *ref, struct ulist *parents,
47244853868SJosef Bacik 				const u64 *extent_item_pos, u64 total_refs)
4738da6d581SJan Schmidt {
4748da6d581SJan Schmidt 	struct btrfs_root *root;
4758da6d581SJan Schmidt 	struct btrfs_key root_key;
4768da6d581SJan Schmidt 	struct extent_buffer *eb;
4778da6d581SJan Schmidt 	int ret = 0;
4788da6d581SJan Schmidt 	int root_level;
4798da6d581SJan Schmidt 	int level = ref->level;
480538f72cdSWang Shilong 	int index;
4818da6d581SJan Schmidt 
4828da6d581SJan Schmidt 	root_key.objectid = ref->root_id;
4838da6d581SJan Schmidt 	root_key.type = BTRFS_ROOT_ITEM_KEY;
4848da6d581SJan Schmidt 	root_key.offset = (u64)-1;
485538f72cdSWang Shilong 
486538f72cdSWang Shilong 	index = srcu_read_lock(&fs_info->subvol_srcu);
487538f72cdSWang Shilong 
4882d9e9776SJosef Bacik 	root = btrfs_get_fs_root(fs_info, &root_key, false);
4898da6d581SJan Schmidt 	if (IS_ERR(root)) {
490538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
4918da6d581SJan Schmidt 		ret = PTR_ERR(root);
4928da6d581SJan Schmidt 		goto out;
4938da6d581SJan Schmidt 	}
4948da6d581SJan Schmidt 
495f5ee5c9aSJeff Mahoney 	if (btrfs_is_testing(fs_info)) {
496d9ee522bSJosef Bacik 		srcu_read_unlock(&fs_info->subvol_srcu, index);
497d9ee522bSJosef Bacik 		ret = -ENOENT;
498d9ee522bSJosef Bacik 		goto out;
499d9ee522bSJosef Bacik 	}
500d9ee522bSJosef Bacik 
5019e351cc8SJosef Bacik 	if (path->search_commit_root)
5029e351cc8SJosef Bacik 		root_level = btrfs_header_level(root->commit_root);
503de47c9d3SEdmund Nadolski 	else if (time_seq == SEQ_LAST)
50421633fc6SQu Wenruo 		root_level = btrfs_header_level(root->node);
5059e351cc8SJosef Bacik 	else
5065b6602e7SJan Schmidt 		root_level = btrfs_old_root_level(root, time_seq);
5078da6d581SJan Schmidt 
508538f72cdSWang Shilong 	if (root_level + 1 == level) {
509538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
5108da6d581SJan Schmidt 		goto out;
511538f72cdSWang Shilong 	}
5128da6d581SJan Schmidt 
5138da6d581SJan Schmidt 	path->lowest_level = level;
514de47c9d3SEdmund Nadolski 	if (time_seq == SEQ_LAST)
51521633fc6SQu Wenruo 		ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
51621633fc6SQu Wenruo 					0, 0);
51721633fc6SQu Wenruo 	else
51821633fc6SQu Wenruo 		ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
51921633fc6SQu Wenruo 					    time_seq);
520538f72cdSWang Shilong 
521538f72cdSWang Shilong 	/* root node has been locked, we can release @subvol_srcu safely here */
522538f72cdSWang Shilong 	srcu_read_unlock(&fs_info->subvol_srcu, index);
523538f72cdSWang Shilong 
524ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info,
525ab8d0fc4SJeff Mahoney 		"search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
526c1c9ff7cSGeert Uytterhoeven 		 ref->root_id, level, ref->count, ret,
527c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.objectid, ref->key_for_search.type,
528c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.offset);
5298da6d581SJan Schmidt 	if (ret < 0)
5308da6d581SJan Schmidt 		goto out;
5318da6d581SJan Schmidt 
5328da6d581SJan Schmidt 	eb = path->nodes[level];
5339345457fSJan Schmidt 	while (!eb) {
534fae7f21cSDulshani Gunawardhana 		if (WARN_ON(!level)) {
5358da6d581SJan Schmidt 			ret = 1;
5368da6d581SJan Schmidt 			goto out;
5378da6d581SJan Schmidt 		}
5389345457fSJan Schmidt 		level--;
5399345457fSJan Schmidt 		eb = path->nodes[level];
5409345457fSJan Schmidt 	}
5418da6d581SJan Schmidt 
5427ef81ac8SJosef Bacik 	ret = add_all_parents(root, path, parents, ref, level, time_seq,
54344853868SJosef Bacik 			      extent_item_pos, total_refs);
5448da6d581SJan Schmidt out:
545da61d31aSJosef Bacik 	path->lowest_level = 0;
546da61d31aSJosef Bacik 	btrfs_release_path(path);
5478da6d581SJan Schmidt 	return ret;
5488da6d581SJan Schmidt }
5498da6d581SJan Schmidt 
5504dae077aSJeff Mahoney static struct extent_inode_elem *
5514dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node)
5524dae077aSJeff Mahoney {
5534dae077aSJeff Mahoney 	if (!node)
5544dae077aSJeff Mahoney 		return NULL;
5554dae077aSJeff Mahoney 	return (struct extent_inode_elem *)(uintptr_t)node->aux;
5564dae077aSJeff Mahoney }
5574dae077aSJeff Mahoney 
5588da6d581SJan Schmidt /*
55986d5f994SEdmund Nadolski  * We maintain three seperate rbtrees: one for direct refs, one for
56086d5f994SEdmund Nadolski  * indirect refs which have a key, and one for indirect refs which do not
56186d5f994SEdmund Nadolski  * have a key. Each tree does merge on insertion.
56286d5f994SEdmund Nadolski  *
56386d5f994SEdmund Nadolski  * Once all of the references are located, we iterate over the tree of
56486d5f994SEdmund Nadolski  * indirect refs with missing keys. An appropriate key is located and
56586d5f994SEdmund Nadolski  * the ref is moved onto the tree for indirect refs. After all missing
56686d5f994SEdmund Nadolski  * keys are thus located, we iterate over the indirect ref tree, resolve
56786d5f994SEdmund Nadolski  * each reference, and then insert the resolved reference onto the
56886d5f994SEdmund Nadolski  * direct tree (merging there too).
56986d5f994SEdmund Nadolski  *
57086d5f994SEdmund Nadolski  * New backrefs (i.e., for parent nodes) are added to the appropriate
57186d5f994SEdmund Nadolski  * rbtree as they are encountered. The new backrefs are subsequently
57286d5f994SEdmund Nadolski  * resolved as above.
5738da6d581SJan Schmidt  */
574e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
575da61d31aSJosef Bacik 				 struct btrfs_path *path, u64 time_seq,
57686d5f994SEdmund Nadolski 				 struct preftrees *preftrees,
577dc046b10SJosef Bacik 				 const u64 *extent_item_pos, u64 total_refs,
578dc046b10SJosef Bacik 				 u64 root_objectid)
5798da6d581SJan Schmidt {
5808da6d581SJan Schmidt 	int err;
5818da6d581SJan Schmidt 	int ret = 0;
5828da6d581SJan Schmidt 	struct ulist *parents;
5838da6d581SJan Schmidt 	struct ulist_node *node;
584cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
58586d5f994SEdmund Nadolski 	struct rb_node *rnode;
5868da6d581SJan Schmidt 
5878da6d581SJan Schmidt 	parents = ulist_alloc(GFP_NOFS);
5888da6d581SJan Schmidt 	if (!parents)
5898da6d581SJan Schmidt 		return -ENOMEM;
5908da6d581SJan Schmidt 
5918da6d581SJan Schmidt 	/*
59286d5f994SEdmund Nadolski 	 * We could trade memory usage for performance here by iterating
59386d5f994SEdmund Nadolski 	 * the tree, allocating new refs for each insertion, and then
59486d5f994SEdmund Nadolski 	 * freeing the entire indirect tree when we're done.  In some test
59586d5f994SEdmund Nadolski 	 * cases, the tree can grow quite large (~200k objects).
5968da6d581SJan Schmidt 	 */
59786d5f994SEdmund Nadolski 	while ((rnode = rb_first(&preftrees->indirect.root))) {
59886d5f994SEdmund Nadolski 		struct prelim_ref *ref;
59986d5f994SEdmund Nadolski 
60086d5f994SEdmund Nadolski 		ref = rb_entry(rnode, struct prelim_ref, rbnode);
60186d5f994SEdmund Nadolski 		if (WARN(ref->parent,
60286d5f994SEdmund Nadolski 			 "BUG: direct ref found in indirect tree")) {
60386d5f994SEdmund Nadolski 			ret = -EINVAL;
60486d5f994SEdmund Nadolski 			goto out;
60586d5f994SEdmund Nadolski 		}
60686d5f994SEdmund Nadolski 
60786d5f994SEdmund Nadolski 		rb_erase(&ref->rbnode, &preftrees->indirect.root);
6086c336b21SJeff Mahoney 		preftrees->indirect.count--;
60986d5f994SEdmund Nadolski 
61086d5f994SEdmund Nadolski 		if (ref->count == 0) {
61186d5f994SEdmund Nadolski 			free_pref(ref);
6128da6d581SJan Schmidt 			continue;
61386d5f994SEdmund Nadolski 		}
61486d5f994SEdmund Nadolski 
615dc046b10SJosef Bacik 		if (root_objectid && ref->root_id != root_objectid) {
61686d5f994SEdmund Nadolski 			free_pref(ref);
617dc046b10SJosef Bacik 			ret = BACKREF_FOUND_SHARED;
618dc046b10SJosef Bacik 			goto out;
619dc046b10SJosef Bacik 		}
620e0c476b1SJeff Mahoney 		err = resolve_indirect_ref(fs_info, path, time_seq, ref,
62144853868SJosef Bacik 					   parents, extent_item_pos,
62244853868SJosef Bacik 					   total_refs);
62395def2edSWang Shilong 		/*
62495def2edSWang Shilong 		 * we can only tolerate ENOENT,otherwise,we should catch error
62595def2edSWang Shilong 		 * and return directly.
62695def2edSWang Shilong 		 */
62795def2edSWang Shilong 		if (err == -ENOENT) {
628*00142756SJeff Mahoney 			prelim_ref_insert(fs_info, &preftrees->direct, ref);
6298da6d581SJan Schmidt 			continue;
63095def2edSWang Shilong 		} else if (err) {
63186d5f994SEdmund Nadolski 			free_pref(ref);
63295def2edSWang Shilong 			ret = err;
63395def2edSWang Shilong 			goto out;
63495def2edSWang Shilong 		}
6358da6d581SJan Schmidt 
6368da6d581SJan Schmidt 		/* we put the first parent into the ref at hand */
637cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&uiter);
638cd1b413cSJan Schmidt 		node = ulist_next(parents, &uiter);
6398da6d581SJan Schmidt 		ref->parent = node ? node->val : 0;
6404dae077aSJeff Mahoney 		ref->inode_list = unode_aux_to_inode_list(node);
6418da6d581SJan Schmidt 
64286d5f994SEdmund Nadolski 		/* Add a prelim_ref(s) for any other parent(s). */
643cd1b413cSJan Schmidt 		while ((node = ulist_next(parents, &uiter))) {
64486d5f994SEdmund Nadolski 			struct prelim_ref *new_ref;
64586d5f994SEdmund Nadolski 
646b9e9a6cbSWang Shilong 			new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
647b9e9a6cbSWang Shilong 						   GFP_NOFS);
6488da6d581SJan Schmidt 			if (!new_ref) {
64986d5f994SEdmund Nadolski 				free_pref(ref);
6508da6d581SJan Schmidt 				ret = -ENOMEM;
651e36902d4SWang Shilong 				goto out;
6528da6d581SJan Schmidt 			}
6538da6d581SJan Schmidt 			memcpy(new_ref, ref, sizeof(*ref));
6548da6d581SJan Schmidt 			new_ref->parent = node->val;
6554dae077aSJeff Mahoney 			new_ref->inode_list = unode_aux_to_inode_list(node);
656*00142756SJeff Mahoney 			prelim_ref_insert(fs_info, &preftrees->direct, new_ref);
6578da6d581SJan Schmidt 		}
65886d5f994SEdmund Nadolski 
65986d5f994SEdmund Nadolski 		/* Now it's a direct ref, put it in the the direct tree */
660*00142756SJeff Mahoney 		prelim_ref_insert(fs_info, &preftrees->direct, ref);
66186d5f994SEdmund Nadolski 
6628da6d581SJan Schmidt 		ulist_reinit(parents);
6638da6d581SJan Schmidt 	}
664e36902d4SWang Shilong out:
6658da6d581SJan Schmidt 	ulist_free(parents);
6668da6d581SJan Schmidt 	return ret;
6678da6d581SJan Schmidt }
6688da6d581SJan Schmidt 
669d5c88b73SJan Schmidt /*
670d5c88b73SJan Schmidt  * read tree blocks and add keys where required.
671d5c88b73SJan Schmidt  */
672e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info,
67386d5f994SEdmund Nadolski 			    struct preftrees *preftrees)
674d5c88b73SJan Schmidt {
675e0c476b1SJeff Mahoney 	struct prelim_ref *ref;
676d5c88b73SJan Schmidt 	struct extent_buffer *eb;
67786d5f994SEdmund Nadolski 	struct preftree *tree = &preftrees->indirect_missing_keys;
67886d5f994SEdmund Nadolski 	struct rb_node *node;
679d5c88b73SJan Schmidt 
68086d5f994SEdmund Nadolski 	while ((node = rb_first(&tree->root))) {
68186d5f994SEdmund Nadolski 		ref = rb_entry(node, struct prelim_ref, rbnode);
68286d5f994SEdmund Nadolski 		rb_erase(node, &tree->root);
68386d5f994SEdmund Nadolski 
68486d5f994SEdmund Nadolski 		BUG_ON(ref->parent);	/* should not be a direct ref */
68586d5f994SEdmund Nadolski 		BUG_ON(ref->key_for_search.type);
686d5c88b73SJan Schmidt 		BUG_ON(!ref->wanted_disk_byte);
68786d5f994SEdmund Nadolski 
6882ff7e61eSJeff Mahoney 		eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
68964c043deSLiu Bo 		if (IS_ERR(eb)) {
69086d5f994SEdmund Nadolski 			free_pref(ref);
69164c043deSLiu Bo 			return PTR_ERR(eb);
69264c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
69386d5f994SEdmund Nadolski 			free_pref(ref);
694416bc658SJosef Bacik 			free_extent_buffer(eb);
695416bc658SJosef Bacik 			return -EIO;
696416bc658SJosef Bacik 		}
697d5c88b73SJan Schmidt 		btrfs_tree_read_lock(eb);
698d5c88b73SJan Schmidt 		if (btrfs_header_level(eb) == 0)
699d5c88b73SJan Schmidt 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
700d5c88b73SJan Schmidt 		else
701d5c88b73SJan Schmidt 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
702d5c88b73SJan Schmidt 		btrfs_tree_read_unlock(eb);
703d5c88b73SJan Schmidt 		free_extent_buffer(eb);
704*00142756SJeff Mahoney 		prelim_ref_insert(fs_info, &preftrees->indirect, ref);
705d5c88b73SJan Schmidt 	}
706d5c88b73SJan Schmidt 	return 0;
707d5c88b73SJan Schmidt }
708d5c88b73SJan Schmidt 
7098da6d581SJan Schmidt /*
7108da6d581SJan Schmidt  * add all currently queued delayed refs from this head whose seq nr is
7118da6d581SJan Schmidt  * smaller or equal that seq to the list
7128da6d581SJan Schmidt  */
713*00142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
714*00142756SJeff Mahoney 			    struct btrfs_delayed_ref_head *head, u64 seq,
71586d5f994SEdmund Nadolski 			    struct preftrees *preftrees, u64 *total_refs,
716dc046b10SJosef Bacik 			    u64 inum)
7178da6d581SJan Schmidt {
718c6fc2454SQu Wenruo 	struct btrfs_delayed_ref_node *node;
7198da6d581SJan Schmidt 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
720d5c88b73SJan Schmidt 	struct btrfs_key key;
72186d5f994SEdmund Nadolski 	struct btrfs_key tmp_op_key;
72286d5f994SEdmund Nadolski 	struct btrfs_key *op_key = NULL;
7238da6d581SJan Schmidt 	int sgn;
724b1375d64SJan Schmidt 	int ret = 0;
7258da6d581SJan Schmidt 
72686d5f994SEdmund Nadolski 	if (extent_op && extent_op->update_key) {
72786d5f994SEdmund Nadolski 		btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
72886d5f994SEdmund Nadolski 		op_key = &tmp_op_key;
72986d5f994SEdmund Nadolski 	}
7308da6d581SJan Schmidt 
731d7df2c79SJosef Bacik 	spin_lock(&head->lock);
732c6fc2454SQu Wenruo 	list_for_each_entry(node, &head->ref_list, list) {
7338da6d581SJan Schmidt 		if (node->seq > seq)
7348da6d581SJan Schmidt 			continue;
7358da6d581SJan Schmidt 
7368da6d581SJan Schmidt 		switch (node->action) {
7378da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_EXTENT:
7388da6d581SJan Schmidt 		case BTRFS_UPDATE_DELAYED_HEAD:
7398da6d581SJan Schmidt 			WARN_ON(1);
7408da6d581SJan Schmidt 			continue;
7418da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_REF:
7428da6d581SJan Schmidt 			sgn = 1;
7438da6d581SJan Schmidt 			break;
7448da6d581SJan Schmidt 		case BTRFS_DROP_DELAYED_REF:
7458da6d581SJan Schmidt 			sgn = -1;
7468da6d581SJan Schmidt 			break;
7478da6d581SJan Schmidt 		default:
7488da6d581SJan Schmidt 			BUG_ON(1);
7498da6d581SJan Schmidt 		}
75044853868SJosef Bacik 		*total_refs += (node->ref_mod * sgn);
7518da6d581SJan Schmidt 		switch (node->type) {
7528da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY: {
75386d5f994SEdmund Nadolski 			/* NORMAL INDIRECT METADATA backref */
7548da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
7558da6d581SJan Schmidt 
7568da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
757*00142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, ref->root,
758*00142756SJeff Mahoney 					       &tmp_op_key, ref->level + 1,
759*00142756SJeff Mahoney 					       node->bytenr,
76086d5f994SEdmund Nadolski 					       node->ref_mod * sgn,
76186d5f994SEdmund Nadolski 					       GFP_ATOMIC);
7628da6d581SJan Schmidt 			break;
7638da6d581SJan Schmidt 		}
7648da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY: {
76586d5f994SEdmund Nadolski 			/* SHARED DIRECT METADATA backref */
7668da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
7678da6d581SJan Schmidt 
7688da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
76986d5f994SEdmund Nadolski 
770*00142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees,
771*00142756SJeff Mahoney 					     ref->level + 1, ref->parent,
772*00142756SJeff Mahoney 					     node->bytenr, node->ref_mod * sgn,
77386d5f994SEdmund Nadolski 					     GFP_ATOMIC);
7748da6d581SJan Schmidt 			break;
7758da6d581SJan Schmidt 		}
7768da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
77786d5f994SEdmund Nadolski 			/* NORMAL INDIRECT DATA backref */
7788da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
7798da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
7808da6d581SJan Schmidt 
7818da6d581SJan Schmidt 			key.objectid = ref->objectid;
7828da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
7838da6d581SJan Schmidt 			key.offset = ref->offset;
784dc046b10SJosef Bacik 
785dc046b10SJosef Bacik 			/*
786dc046b10SJosef Bacik 			 * Found a inum that doesn't match our known inum, we
787dc046b10SJosef Bacik 			 * know it's shared.
788dc046b10SJosef Bacik 			 */
789dc046b10SJosef Bacik 			if (inum && ref->objectid != inum) {
790dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
791dc046b10SJosef Bacik 				break;
792dc046b10SJosef Bacik 			}
793dc046b10SJosef Bacik 
794*00142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, ref->root,
795*00142756SJeff Mahoney 					       &key, 0, node->bytenr,
79686d5f994SEdmund Nadolski 					       node->ref_mod * sgn,
797e0c476b1SJeff Mahoney 					       GFP_ATOMIC);
7988da6d581SJan Schmidt 			break;
7998da6d581SJan Schmidt 		}
8008da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
80186d5f994SEdmund Nadolski 			/* SHARED DIRECT FULL backref */
8028da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
8038da6d581SJan Schmidt 
8048da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
80586d5f994SEdmund Nadolski 
806*00142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees, 0,
807*00142756SJeff Mahoney 					     ref->parent, node->bytenr,
80886d5f994SEdmund Nadolski 					     node->ref_mod * sgn,
809e0c476b1SJeff Mahoney 					     GFP_ATOMIC);
8108da6d581SJan Schmidt 			break;
8118da6d581SJan Schmidt 		}
8128da6d581SJan Schmidt 		default:
8138da6d581SJan Schmidt 			WARN_ON(1);
8148da6d581SJan Schmidt 		}
8151149ab6bSWang Shilong 		if (ret)
816d7df2c79SJosef Bacik 			break;
8178da6d581SJan Schmidt 	}
818d7df2c79SJosef Bacik 	spin_unlock(&head->lock);
819d7df2c79SJosef Bacik 	return ret;
8208da6d581SJan Schmidt }
8218da6d581SJan Schmidt 
8228da6d581SJan Schmidt /*
8238da6d581SJan Schmidt  * add all inline backrefs for bytenr to the list
8248da6d581SJan Schmidt  */
825*00142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info,
826*00142756SJeff Mahoney 			   struct btrfs_path *path, u64 bytenr,
82786d5f994SEdmund Nadolski 			   int *info_level, struct preftrees *preftrees,
828dc046b10SJosef Bacik 			   u64 *total_refs, u64 inum)
8298da6d581SJan Schmidt {
830b1375d64SJan Schmidt 	int ret = 0;
8318da6d581SJan Schmidt 	int slot;
8328da6d581SJan Schmidt 	struct extent_buffer *leaf;
8338da6d581SJan Schmidt 	struct btrfs_key key;
834261c84b6SJosef Bacik 	struct btrfs_key found_key;
8358da6d581SJan Schmidt 	unsigned long ptr;
8368da6d581SJan Schmidt 	unsigned long end;
8378da6d581SJan Schmidt 	struct btrfs_extent_item *ei;
8388da6d581SJan Schmidt 	u64 flags;
8398da6d581SJan Schmidt 	u64 item_size;
8408da6d581SJan Schmidt 
8418da6d581SJan Schmidt 	/*
8428da6d581SJan Schmidt 	 * enumerate all inline refs
8438da6d581SJan Schmidt 	 */
8448da6d581SJan Schmidt 	leaf = path->nodes[0];
845dadcaf78SJan Schmidt 	slot = path->slots[0];
8468da6d581SJan Schmidt 
8478da6d581SJan Schmidt 	item_size = btrfs_item_size_nr(leaf, slot);
8488da6d581SJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
8498da6d581SJan Schmidt 
8508da6d581SJan Schmidt 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
8518da6d581SJan Schmidt 	flags = btrfs_extent_flags(leaf, ei);
85244853868SJosef Bacik 	*total_refs += btrfs_extent_refs(leaf, ei);
853261c84b6SJosef Bacik 	btrfs_item_key_to_cpu(leaf, &found_key, slot);
8548da6d581SJan Schmidt 
8558da6d581SJan Schmidt 	ptr = (unsigned long)(ei + 1);
8568da6d581SJan Schmidt 	end = (unsigned long)ei + item_size;
8578da6d581SJan Schmidt 
858261c84b6SJosef Bacik 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
859261c84b6SJosef Bacik 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
8608da6d581SJan Schmidt 		struct btrfs_tree_block_info *info;
8618da6d581SJan Schmidt 
8628da6d581SJan Schmidt 		info = (struct btrfs_tree_block_info *)ptr;
8638da6d581SJan Schmidt 		*info_level = btrfs_tree_block_level(leaf, info);
8648da6d581SJan Schmidt 		ptr += sizeof(struct btrfs_tree_block_info);
8658da6d581SJan Schmidt 		BUG_ON(ptr > end);
866261c84b6SJosef Bacik 	} else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
867261c84b6SJosef Bacik 		*info_level = found_key.offset;
8688da6d581SJan Schmidt 	} else {
8698da6d581SJan Schmidt 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
8708da6d581SJan Schmidt 	}
8718da6d581SJan Schmidt 
8728da6d581SJan Schmidt 	while (ptr < end) {
8738da6d581SJan Schmidt 		struct btrfs_extent_inline_ref *iref;
8748da6d581SJan Schmidt 		u64 offset;
8758da6d581SJan Schmidt 		int type;
8768da6d581SJan Schmidt 
8778da6d581SJan Schmidt 		iref = (struct btrfs_extent_inline_ref *)ptr;
8788da6d581SJan Schmidt 		type = btrfs_extent_inline_ref_type(leaf, iref);
8798da6d581SJan Schmidt 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
8808da6d581SJan Schmidt 
8818da6d581SJan Schmidt 		switch (type) {
8828da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
883*00142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees,
884*00142756SJeff Mahoney 					     *info_level + 1, offset,
88586d5f994SEdmund Nadolski 					     bytenr, 1, GFP_NOFS);
8868da6d581SJan Schmidt 			break;
8878da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
8888da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
8898da6d581SJan Schmidt 			int count;
8908da6d581SJan Schmidt 
8918da6d581SJan Schmidt 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
8928da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
89386d5f994SEdmund Nadolski 
894*00142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees, 0, offset,
895742916b8SWang Shilong 					     bytenr, count, GFP_NOFS);
8968da6d581SJan Schmidt 			break;
8978da6d581SJan Schmidt 		}
8988da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
899*00142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, offset,
900*00142756SJeff Mahoney 					       NULL, *info_level + 1,
901*00142756SJeff Mahoney 					       bytenr, 1, GFP_NOFS);
9028da6d581SJan Schmidt 			break;
9038da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
9048da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
9058da6d581SJan Schmidt 			int count;
9068da6d581SJan Schmidt 			u64 root;
9078da6d581SJan Schmidt 
9088da6d581SJan Schmidt 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
9098da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
9108da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
9118da6d581SJan Schmidt 								      dref);
9128da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
9138da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
914dc046b10SJosef Bacik 
915dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
916dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
917dc046b10SJosef Bacik 				break;
918dc046b10SJosef Bacik 			}
919dc046b10SJosef Bacik 
9208da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
92186d5f994SEdmund Nadolski 
922*00142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, root,
923*00142756SJeff Mahoney 					       &key, 0, bytenr, count,
924*00142756SJeff Mahoney 					       GFP_NOFS);
9258da6d581SJan Schmidt 			break;
9268da6d581SJan Schmidt 		}
9278da6d581SJan Schmidt 		default:
9288da6d581SJan Schmidt 			WARN_ON(1);
9298da6d581SJan Schmidt 		}
9301149ab6bSWang Shilong 		if (ret)
9311149ab6bSWang Shilong 			return ret;
9328da6d581SJan Schmidt 		ptr += btrfs_extent_inline_ref_size(type);
9338da6d581SJan Schmidt 	}
9348da6d581SJan Schmidt 
9358da6d581SJan Schmidt 	return 0;
9368da6d581SJan Schmidt }
9378da6d581SJan Schmidt 
9388da6d581SJan Schmidt /*
9398da6d581SJan Schmidt  * add all non-inline backrefs for bytenr to the list
9408da6d581SJan Schmidt  */
941e0c476b1SJeff Mahoney static int add_keyed_refs(struct btrfs_fs_info *fs_info,
9428da6d581SJan Schmidt 			  struct btrfs_path *path, u64 bytenr,
94386d5f994SEdmund Nadolski 			  int info_level, struct preftrees *preftrees,
94486d5f994SEdmund Nadolski 			  u64 inum)
9458da6d581SJan Schmidt {
9468da6d581SJan Schmidt 	struct btrfs_root *extent_root = fs_info->extent_root;
9478da6d581SJan Schmidt 	int ret;
9488da6d581SJan Schmidt 	int slot;
9498da6d581SJan Schmidt 	struct extent_buffer *leaf;
9508da6d581SJan Schmidt 	struct btrfs_key key;
9518da6d581SJan Schmidt 
9528da6d581SJan Schmidt 	while (1) {
9538da6d581SJan Schmidt 		ret = btrfs_next_item(extent_root, path);
9548da6d581SJan Schmidt 		if (ret < 0)
9558da6d581SJan Schmidt 			break;
9568da6d581SJan Schmidt 		if (ret) {
9578da6d581SJan Schmidt 			ret = 0;
9588da6d581SJan Schmidt 			break;
9598da6d581SJan Schmidt 		}
9608da6d581SJan Schmidt 
9618da6d581SJan Schmidt 		slot = path->slots[0];
9628da6d581SJan Schmidt 		leaf = path->nodes[0];
9638da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
9648da6d581SJan Schmidt 
9658da6d581SJan Schmidt 		if (key.objectid != bytenr)
9668da6d581SJan Schmidt 			break;
9678da6d581SJan Schmidt 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
9688da6d581SJan Schmidt 			continue;
9698da6d581SJan Schmidt 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
9708da6d581SJan Schmidt 			break;
9718da6d581SJan Schmidt 
9728da6d581SJan Schmidt 		switch (key.type) {
9738da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
97486d5f994SEdmund Nadolski 			/* SHARED DIRECT METADATA backref */
975*00142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees,
976*00142756SJeff Mahoney 					     info_level + 1, key.offset,
977*00142756SJeff Mahoney 					     bytenr, 1, GFP_NOFS);
9788da6d581SJan Schmidt 			break;
9798da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
98086d5f994SEdmund Nadolski 			/* SHARED DIRECT FULL backref */
9818da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
9828da6d581SJan Schmidt 			int count;
9838da6d581SJan Schmidt 
9848da6d581SJan Schmidt 			sdref = btrfs_item_ptr(leaf, slot,
9858da6d581SJan Schmidt 					      struct btrfs_shared_data_ref);
9868da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
987*00142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees, 0,
988*00142756SJeff Mahoney 					     key.offset, bytenr, count,
989*00142756SJeff Mahoney 					     GFP_NOFS);
9908da6d581SJan Schmidt 			break;
9918da6d581SJan Schmidt 		}
9928da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
99386d5f994SEdmund Nadolski 			/* NORMAL INDIRECT METADATA backref */
994*00142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, key.offset,
995*00142756SJeff Mahoney 					       NULL, info_level + 1, bytenr,
996*00142756SJeff Mahoney 					       1, GFP_NOFS);
9978da6d581SJan Schmidt 			break;
9988da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
99986d5f994SEdmund Nadolski 			/* NORMAL INDIRECT DATA backref */
10008da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
10018da6d581SJan Schmidt 			int count;
10028da6d581SJan Schmidt 			u64 root;
10038da6d581SJan Schmidt 
10048da6d581SJan Schmidt 			dref = btrfs_item_ptr(leaf, slot,
10058da6d581SJan Schmidt 					      struct btrfs_extent_data_ref);
10068da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
10078da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
10088da6d581SJan Schmidt 								      dref);
10098da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
10108da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
1011dc046b10SJosef Bacik 
1012dc046b10SJosef Bacik 			if (inum && key.objectid != inum) {
1013dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1014dc046b10SJosef Bacik 				break;
1015dc046b10SJosef Bacik 			}
1016dc046b10SJosef Bacik 
10178da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
1018*00142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, root,
1019*00142756SJeff Mahoney 					       &key, 0, bytenr, count,
1020*00142756SJeff Mahoney 					       GFP_NOFS);
10218da6d581SJan Schmidt 			break;
10228da6d581SJan Schmidt 		}
10238da6d581SJan Schmidt 		default:
10248da6d581SJan Schmidt 			WARN_ON(1);
10258da6d581SJan Schmidt 		}
10261149ab6bSWang Shilong 		if (ret)
10271149ab6bSWang Shilong 			return ret;
10281149ab6bSWang Shilong 
10298da6d581SJan Schmidt 	}
10308da6d581SJan Schmidt 
10318da6d581SJan Schmidt 	return ret;
10328da6d581SJan Schmidt }
10338da6d581SJan Schmidt 
10348da6d581SJan Schmidt /*
10358da6d581SJan Schmidt  * this adds all existing backrefs (inline backrefs, backrefs and delayed
10368da6d581SJan Schmidt  * refs) for the given bytenr to the refs list, merges duplicates and resolves
10378da6d581SJan Schmidt  * indirect refs to their parent bytenr.
10388da6d581SJan Schmidt  * When roots are found, they're added to the roots list
10398da6d581SJan Schmidt  *
10402c2ed5aaSMark Fasheh  * NOTE: This can return values > 0
10412c2ed5aaSMark Fasheh  *
1042de47c9d3SEdmund Nadolski  * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
104321633fc6SQu Wenruo  * much like trans == NULL case, the difference only lies in it will not
104421633fc6SQu Wenruo  * commit root.
104521633fc6SQu Wenruo  * The special case is for qgroup to search roots in commit_transaction().
104621633fc6SQu Wenruo  *
10478da6d581SJan Schmidt  * FIXME some caching might speed things up
10488da6d581SJan Schmidt  */
10498da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans,
10508da6d581SJan Schmidt 			     struct btrfs_fs_info *fs_info, u64 bytenr,
1051097b8a7cSJan Schmidt 			     u64 time_seq, struct ulist *refs,
1052dc046b10SJosef Bacik 			     struct ulist *roots, const u64 *extent_item_pos,
1053f6954245SEdmund Nadolski 			     u64 root_objectid, u64 inum)
10548da6d581SJan Schmidt {
10558da6d581SJan Schmidt 	struct btrfs_key key;
10568da6d581SJan Schmidt 	struct btrfs_path *path;
10578da6d581SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
1058d3b01064SLi Zefan 	struct btrfs_delayed_ref_head *head;
10598da6d581SJan Schmidt 	int info_level = 0;
10608da6d581SJan Schmidt 	int ret;
1061e0c476b1SJeff Mahoney 	struct prelim_ref *ref;
106286d5f994SEdmund Nadolski 	struct rb_node *node;
1063f05c4746SWang Shilong 	struct extent_inode_elem *eie = NULL;
106486d5f994SEdmund Nadolski 	/* total of both direct AND indirect refs! */
106544853868SJosef Bacik 	u64 total_refs = 0;
106686d5f994SEdmund Nadolski 	struct preftrees preftrees = {
106786d5f994SEdmund Nadolski 		.direct = PREFTREE_INIT,
106886d5f994SEdmund Nadolski 		.indirect = PREFTREE_INIT,
106986d5f994SEdmund Nadolski 		.indirect_missing_keys = PREFTREE_INIT
107086d5f994SEdmund Nadolski 	};
10718da6d581SJan Schmidt 
10728da6d581SJan Schmidt 	key.objectid = bytenr;
10738da6d581SJan Schmidt 	key.offset = (u64)-1;
1074261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1075261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1076261c84b6SJosef Bacik 	else
1077261c84b6SJosef Bacik 		key.type = BTRFS_EXTENT_ITEM_KEY;
10788da6d581SJan Schmidt 
10798da6d581SJan Schmidt 	path = btrfs_alloc_path();
10808da6d581SJan Schmidt 	if (!path)
10818da6d581SJan Schmidt 		return -ENOMEM;
1082e84752d4SWang Shilong 	if (!trans) {
1083da61d31aSJosef Bacik 		path->search_commit_root = 1;
1084e84752d4SWang Shilong 		path->skip_locking = 1;
1085e84752d4SWang Shilong 	}
10868da6d581SJan Schmidt 
1087de47c9d3SEdmund Nadolski 	if (time_seq == SEQ_LAST)
108821633fc6SQu Wenruo 		path->skip_locking = 1;
108921633fc6SQu Wenruo 
10908da6d581SJan Schmidt 	/*
10918da6d581SJan Schmidt 	 * grab both a lock on the path and a lock on the delayed ref head.
10928da6d581SJan Schmidt 	 * We need both to get a consistent picture of how the refs look
10938da6d581SJan Schmidt 	 * at a specified point in time
10948da6d581SJan Schmidt 	 */
10958da6d581SJan Schmidt again:
1096d3b01064SLi Zefan 	head = NULL;
1097d3b01064SLi Zefan 
10988da6d581SJan Schmidt 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
10998da6d581SJan Schmidt 	if (ret < 0)
11008da6d581SJan Schmidt 		goto out;
11018da6d581SJan Schmidt 	BUG_ON(ret == 0);
11028da6d581SJan Schmidt 
1103faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
110421633fc6SQu Wenruo 	if (trans && likely(trans->type != __TRANS_DUMMY) &&
1105de47c9d3SEdmund Nadolski 	    time_seq != SEQ_LAST) {
1106faa2dbf0SJosef Bacik #else
1107de47c9d3SEdmund Nadolski 	if (trans && time_seq != SEQ_LAST) {
1108faa2dbf0SJosef Bacik #endif
11098da6d581SJan Schmidt 		/*
11107a3ae2f8SJan Schmidt 		 * look if there are updates for this ref queued and lock the
11117a3ae2f8SJan Schmidt 		 * head
11128da6d581SJan Schmidt 		 */
11138da6d581SJan Schmidt 		delayed_refs = &trans->transaction->delayed_refs;
11148da6d581SJan Schmidt 		spin_lock(&delayed_refs->lock);
1115f72ad18eSLiu Bo 		head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
11168da6d581SJan Schmidt 		if (head) {
11178da6d581SJan Schmidt 			if (!mutex_trylock(&head->mutex)) {
11186df8cdf5SElena Reshetova 				refcount_inc(&head->node.refs);
11198da6d581SJan Schmidt 				spin_unlock(&delayed_refs->lock);
11208da6d581SJan Schmidt 
11218da6d581SJan Schmidt 				btrfs_release_path(path);
11228da6d581SJan Schmidt 
11238da6d581SJan Schmidt 				/*
11248da6d581SJan Schmidt 				 * Mutex was contended, block until it's
11258da6d581SJan Schmidt 				 * released and try again
11268da6d581SJan Schmidt 				 */
11278da6d581SJan Schmidt 				mutex_lock(&head->mutex);
11288da6d581SJan Schmidt 				mutex_unlock(&head->mutex);
11298da6d581SJan Schmidt 				btrfs_put_delayed_ref(&head->node);
11308da6d581SJan Schmidt 				goto again;
11318da6d581SJan Schmidt 			}
1132d7df2c79SJosef Bacik 			spin_unlock(&delayed_refs->lock);
1133*00142756SJeff Mahoney 			ret = add_delayed_refs(fs_info, head, time_seq,
1134*00142756SJeff Mahoney 					       &preftrees, &total_refs, inum);
1135155725c9SJan Schmidt 			mutex_unlock(&head->mutex);
1136d7df2c79SJosef Bacik 			if (ret)
11378da6d581SJan Schmidt 				goto out;
1138d7df2c79SJosef Bacik 		} else {
11398da6d581SJan Schmidt 			spin_unlock(&delayed_refs->lock);
11407a3ae2f8SJan Schmidt 		}
1141d7df2c79SJosef Bacik 	}
11428da6d581SJan Schmidt 
11438da6d581SJan Schmidt 	if (path->slots[0]) {
11448da6d581SJan Schmidt 		struct extent_buffer *leaf;
11458da6d581SJan Schmidt 		int slot;
11468da6d581SJan Schmidt 
1147dadcaf78SJan Schmidt 		path->slots[0]--;
11488da6d581SJan Schmidt 		leaf = path->nodes[0];
1149dadcaf78SJan Schmidt 		slot = path->slots[0];
11508da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
11518da6d581SJan Schmidt 		if (key.objectid == bytenr &&
1152261c84b6SJosef Bacik 		    (key.type == BTRFS_EXTENT_ITEM_KEY ||
1153261c84b6SJosef Bacik 		     key.type == BTRFS_METADATA_ITEM_KEY)) {
1154*00142756SJeff Mahoney 			ret = add_inline_refs(fs_info, path, bytenr,
1155*00142756SJeff Mahoney 					      &info_level, &preftrees,
1156*00142756SJeff Mahoney 					      &total_refs, inum);
11578da6d581SJan Schmidt 			if (ret)
11588da6d581SJan Schmidt 				goto out;
1159e0c476b1SJeff Mahoney 			ret = add_keyed_refs(fs_info, path, bytenr, info_level,
116086d5f994SEdmund Nadolski 					     &preftrees, inum);
11618da6d581SJan Schmidt 			if (ret)
11628da6d581SJan Schmidt 				goto out;
11638da6d581SJan Schmidt 		}
11648da6d581SJan Schmidt 	}
116586d5f994SEdmund Nadolski 
11668da6d581SJan Schmidt 	btrfs_release_path(path);
11678da6d581SJan Schmidt 
116886d5f994SEdmund Nadolski 	ret = add_missing_keys(fs_info, &preftrees);
1169d5c88b73SJan Schmidt 	if (ret)
1170d5c88b73SJan Schmidt 		goto out;
1171d5c88b73SJan Schmidt 
117286d5f994SEdmund Nadolski 	WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
11738da6d581SJan Schmidt 
117486d5f994SEdmund Nadolski 	ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
1175dc046b10SJosef Bacik 				    extent_item_pos, total_refs,
1176dc046b10SJosef Bacik 				    root_objectid);
11778da6d581SJan Schmidt 	if (ret)
11788da6d581SJan Schmidt 		goto out;
11798da6d581SJan Schmidt 
118086d5f994SEdmund Nadolski 	WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
11818da6d581SJan Schmidt 
118286d5f994SEdmund Nadolski 	/*
118386d5f994SEdmund Nadolski 	 * This walks the tree of merged and resolved refs. Tree blocks are
118486d5f994SEdmund Nadolski 	 * read in as needed. Unique entries are added to the ulist, and
118586d5f994SEdmund Nadolski 	 * the list of found roots is updated.
118686d5f994SEdmund Nadolski 	 *
118786d5f994SEdmund Nadolski 	 * We release the entire tree in one go before returning.
118886d5f994SEdmund Nadolski 	 */
118986d5f994SEdmund Nadolski 	node = rb_first(&preftrees.direct.root);
119086d5f994SEdmund Nadolski 	while (node) {
119186d5f994SEdmund Nadolski 		ref = rb_entry(node, struct prelim_ref, rbnode);
119286d5f994SEdmund Nadolski 		node = rb_next(&ref->rbnode);
11936c1500f2SJulia Lawall 		WARN_ON(ref->count < 0);
119498cfee21SWang Shilong 		if (roots && ref->count && ref->root_id && ref->parent == 0) {
1195dc046b10SJosef Bacik 			if (root_objectid && ref->root_id != root_objectid) {
1196dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1197dc046b10SJosef Bacik 				goto out;
1198dc046b10SJosef Bacik 			}
1199dc046b10SJosef Bacik 
12008da6d581SJan Schmidt 			/* no parent == root of tree */
12018da6d581SJan Schmidt 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
1202f1723939SWang Shilong 			if (ret < 0)
1203f1723939SWang Shilong 				goto out;
12048da6d581SJan Schmidt 		}
12058da6d581SJan Schmidt 		if (ref->count && ref->parent) {
12068a56457fSJosef Bacik 			if (extent_item_pos && !ref->inode_list &&
12078a56457fSJosef Bacik 			    ref->level == 0) {
1208976b1908SJan Schmidt 				struct extent_buffer *eb;
1209707e8a07SDavid Sterba 
12102ff7e61eSJeff Mahoney 				eb = read_tree_block(fs_info, ref->parent, 0);
121164c043deSLiu Bo 				if (IS_ERR(eb)) {
121264c043deSLiu Bo 					ret = PTR_ERR(eb);
121364c043deSLiu Bo 					goto out;
121464c043deSLiu Bo 				} else if (!extent_buffer_uptodate(eb)) {
1215416bc658SJosef Bacik 					free_extent_buffer(eb);
1216c16c2e2eSWang Shilong 					ret = -EIO;
1217c16c2e2eSWang Shilong 					goto out;
1218416bc658SJosef Bacik 				}
12196f7ff6d7SFilipe Manana 				btrfs_tree_read_lock(eb);
12206f7ff6d7SFilipe Manana 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1221976b1908SJan Schmidt 				ret = find_extent_in_eb(eb, bytenr,
1222976b1908SJan Schmidt 							*extent_item_pos, &eie);
12236f7ff6d7SFilipe Manana 				btrfs_tree_read_unlock_blocking(eb);
1224976b1908SJan Schmidt 				free_extent_buffer(eb);
1225f5929cd8SFilipe David Borba Manana 				if (ret < 0)
1226f5929cd8SFilipe David Borba Manana 					goto out;
1227f5929cd8SFilipe David Borba Manana 				ref->inode_list = eie;
1228976b1908SJan Schmidt 			}
12294eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(refs, ref->parent,
12304eb1f66dSTakashi Iwai 						  ref->inode_list,
12314eb1f66dSTakashi Iwai 						  (void **)&eie, GFP_NOFS);
1232f1723939SWang Shilong 			if (ret < 0)
1233f1723939SWang Shilong 				goto out;
12343301958bSJan Schmidt 			if (!ret && extent_item_pos) {
12353301958bSJan Schmidt 				/*
12363301958bSJan Schmidt 				 * we've recorded that parent, so we must extend
12373301958bSJan Schmidt 				 * its inode list here
12383301958bSJan Schmidt 				 */
12393301958bSJan Schmidt 				BUG_ON(!eie);
12403301958bSJan Schmidt 				while (eie->next)
12413301958bSJan Schmidt 					eie = eie->next;
12423301958bSJan Schmidt 				eie->next = ref->inode_list;
12433301958bSJan Schmidt 			}
1244f05c4746SWang Shilong 			eie = NULL;
12458da6d581SJan Schmidt 		}
12468da6d581SJan Schmidt 	}
12478da6d581SJan Schmidt 
12488da6d581SJan Schmidt out:
12498da6d581SJan Schmidt 	btrfs_free_path(path);
125086d5f994SEdmund Nadolski 
125186d5f994SEdmund Nadolski 	prelim_release(&preftrees.direct);
125286d5f994SEdmund Nadolski 	prelim_release(&preftrees.indirect);
125386d5f994SEdmund Nadolski 	prelim_release(&preftrees.indirect_missing_keys);
125486d5f994SEdmund Nadolski 
1255f05c4746SWang Shilong 	if (ret < 0)
1256f05c4746SWang Shilong 		free_inode_elem_list(eie);
12578da6d581SJan Schmidt 	return ret;
12588da6d581SJan Schmidt }
12598da6d581SJan Schmidt 
1260976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks)
1261976b1908SJan Schmidt {
1262976b1908SJan Schmidt 	struct ulist_node *node = NULL;
1263976b1908SJan Schmidt 	struct extent_inode_elem *eie;
1264976b1908SJan Schmidt 	struct ulist_iterator uiter;
1265976b1908SJan Schmidt 
1266976b1908SJan Schmidt 	ULIST_ITER_INIT(&uiter);
1267976b1908SJan Schmidt 	while ((node = ulist_next(blocks, &uiter))) {
1268976b1908SJan Schmidt 		if (!node->aux)
1269976b1908SJan Schmidt 			continue;
12704dae077aSJeff Mahoney 		eie = unode_aux_to_inode_list(node);
1271f05c4746SWang Shilong 		free_inode_elem_list(eie);
1272976b1908SJan Schmidt 		node->aux = 0;
1273976b1908SJan Schmidt 	}
1274976b1908SJan Schmidt 
1275976b1908SJan Schmidt 	ulist_free(blocks);
1276976b1908SJan Schmidt }
1277976b1908SJan Schmidt 
12788da6d581SJan Schmidt /*
12798da6d581SJan Schmidt  * Finds all leafs with a reference to the specified combination of bytenr and
12808da6d581SJan Schmidt  * offset. key_list_head will point to a list of corresponding keys (caller must
12818da6d581SJan Schmidt  * free each list element). The leafs will be stored in the leafs ulist, which
12828da6d581SJan Schmidt  * must be freed with ulist_free.
12838da6d581SJan Schmidt  *
12848da6d581SJan Schmidt  * returns 0 on success, <0 on error
12858da6d581SJan Schmidt  */
12868da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
12878da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
1288097b8a7cSJan Schmidt 				u64 time_seq, struct ulist **leafs,
1289976b1908SJan Schmidt 				const u64 *extent_item_pos)
12908da6d581SJan Schmidt {
12918da6d581SJan Schmidt 	int ret;
12928da6d581SJan Schmidt 
12938da6d581SJan Schmidt 	*leafs = ulist_alloc(GFP_NOFS);
129498cfee21SWang Shilong 	if (!*leafs)
12958da6d581SJan Schmidt 		return -ENOMEM;
12968da6d581SJan Schmidt 
1297afce772eSLu Fengqi 	ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1298f6954245SEdmund Nadolski 				*leafs, NULL, extent_item_pos, 0, 0);
12998da6d581SJan Schmidt 	if (ret < 0 && ret != -ENOENT) {
1300976b1908SJan Schmidt 		free_leaf_list(*leafs);
13018da6d581SJan Schmidt 		return ret;
13028da6d581SJan Schmidt 	}
13038da6d581SJan Schmidt 
13048da6d581SJan Schmidt 	return 0;
13058da6d581SJan Schmidt }
13068da6d581SJan Schmidt 
13078da6d581SJan Schmidt /*
13088da6d581SJan Schmidt  * walk all backrefs for a given extent to find all roots that reference this
13098da6d581SJan Schmidt  * extent. Walking a backref means finding all extents that reference this
13108da6d581SJan Schmidt  * extent and in turn walk the backrefs of those, too. Naturally this is a
13118da6d581SJan Schmidt  * recursive process, but here it is implemented in an iterative fashion: We
13128da6d581SJan Schmidt  * find all referencing extents for the extent in question and put them on a
13138da6d581SJan Schmidt  * list. In turn, we find all referencing extents for those, further appending
13148da6d581SJan Schmidt  * to the list. The way we iterate the list allows adding more elements after
13158da6d581SJan Schmidt  * the current while iterating. The process stops when we reach the end of the
13168da6d581SJan Schmidt  * list. Found roots are added to the roots list.
13178da6d581SJan Schmidt  *
13188da6d581SJan Schmidt  * returns 0 on success, < 0 on error.
13198da6d581SJan Schmidt  */
1320e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
13218da6d581SJan Schmidt 				     struct btrfs_fs_info *fs_info, u64 bytenr,
1322097b8a7cSJan Schmidt 				     u64 time_seq, struct ulist **roots)
13238da6d581SJan Schmidt {
13248da6d581SJan Schmidt 	struct ulist *tmp;
13258da6d581SJan Schmidt 	struct ulist_node *node = NULL;
1326cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
13278da6d581SJan Schmidt 	int ret;
13288da6d581SJan Schmidt 
13298da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
13308da6d581SJan Schmidt 	if (!tmp)
13318da6d581SJan Schmidt 		return -ENOMEM;
13328da6d581SJan Schmidt 	*roots = ulist_alloc(GFP_NOFS);
13338da6d581SJan Schmidt 	if (!*roots) {
13348da6d581SJan Schmidt 		ulist_free(tmp);
13358da6d581SJan Schmidt 		return -ENOMEM;
13368da6d581SJan Schmidt 	}
13378da6d581SJan Schmidt 
1338cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&uiter);
13398da6d581SJan Schmidt 	while (1) {
1340afce772eSLu Fengqi 		ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1341f6954245SEdmund Nadolski 					tmp, *roots, NULL, 0, 0);
13428da6d581SJan Schmidt 		if (ret < 0 && ret != -ENOENT) {
13438da6d581SJan Schmidt 			ulist_free(tmp);
13448da6d581SJan Schmidt 			ulist_free(*roots);
13458da6d581SJan Schmidt 			return ret;
13468da6d581SJan Schmidt 		}
1347cd1b413cSJan Schmidt 		node = ulist_next(tmp, &uiter);
13488da6d581SJan Schmidt 		if (!node)
13498da6d581SJan Schmidt 			break;
13508da6d581SJan Schmidt 		bytenr = node->val;
1351bca1a290SWang Shilong 		cond_resched();
13528da6d581SJan Schmidt 	}
13538da6d581SJan Schmidt 
13548da6d581SJan Schmidt 	ulist_free(tmp);
13558da6d581SJan Schmidt 	return 0;
13568da6d581SJan Schmidt }
13578da6d581SJan Schmidt 
13589e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
13599e351cc8SJosef Bacik 			 struct btrfs_fs_info *fs_info, u64 bytenr,
13609e351cc8SJosef Bacik 			 u64 time_seq, struct ulist **roots)
13619e351cc8SJosef Bacik {
13629e351cc8SJosef Bacik 	int ret;
13639e351cc8SJosef Bacik 
13649e351cc8SJosef Bacik 	if (!trans)
13659e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1366e0c476b1SJeff Mahoney 	ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
1367e0c476b1SJeff Mahoney 					time_seq, roots);
13689e351cc8SJosef Bacik 	if (!trans)
13699e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
13709e351cc8SJosef Bacik 	return ret;
13719e351cc8SJosef Bacik }
13729e351cc8SJosef Bacik 
13732c2ed5aaSMark Fasheh /**
13742c2ed5aaSMark Fasheh  * btrfs_check_shared - tell us whether an extent is shared
13752c2ed5aaSMark Fasheh  *
13762c2ed5aaSMark Fasheh  * btrfs_check_shared uses the backref walking code but will short
13772c2ed5aaSMark Fasheh  * circuit as soon as it finds a root or inode that doesn't match the
13782c2ed5aaSMark Fasheh  * one passed in. This provides a significant performance benefit for
13792c2ed5aaSMark Fasheh  * callers (such as fiemap) which want to know whether the extent is
13802c2ed5aaSMark Fasheh  * shared but do not need a ref count.
13812c2ed5aaSMark Fasheh  *
1382bb739cf0SEdmund Nadolski  * This attempts to allocate a transaction in order to account for
1383bb739cf0SEdmund Nadolski  * delayed refs, but continues on even when the alloc fails.
1384bb739cf0SEdmund Nadolski  *
13852c2ed5aaSMark Fasheh  * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
13862c2ed5aaSMark Fasheh  */
1387bb739cf0SEdmund Nadolski int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
1388dc046b10SJosef Bacik {
1389bb739cf0SEdmund Nadolski 	struct btrfs_fs_info *fs_info = root->fs_info;
1390bb739cf0SEdmund Nadolski 	struct btrfs_trans_handle *trans;
1391dc046b10SJosef Bacik 	struct ulist *tmp = NULL;
1392dc046b10SJosef Bacik 	struct ulist *roots = NULL;
1393dc046b10SJosef Bacik 	struct ulist_iterator uiter;
1394dc046b10SJosef Bacik 	struct ulist_node *node;
13953284da7bSDavid Sterba 	struct seq_list elem = SEQ_LIST_INIT(elem);
1396dc046b10SJosef Bacik 	int ret = 0;
1397dc046b10SJosef Bacik 
1398dc046b10SJosef Bacik 	tmp = ulist_alloc(GFP_NOFS);
1399dc046b10SJosef Bacik 	roots = ulist_alloc(GFP_NOFS);
1400dc046b10SJosef Bacik 	if (!tmp || !roots) {
1401dc046b10SJosef Bacik 		ulist_free(tmp);
1402dc046b10SJosef Bacik 		ulist_free(roots);
1403dc046b10SJosef Bacik 		return -ENOMEM;
1404dc046b10SJosef Bacik 	}
1405dc046b10SJosef Bacik 
1406bb739cf0SEdmund Nadolski 	trans = btrfs_join_transaction(root);
1407bb739cf0SEdmund Nadolski 	if (IS_ERR(trans)) {
1408bb739cf0SEdmund Nadolski 		trans = NULL;
1409dc046b10SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1410bb739cf0SEdmund Nadolski 	} else {
1411bb739cf0SEdmund Nadolski 		btrfs_get_tree_mod_seq(fs_info, &elem);
1412bb739cf0SEdmund Nadolski 	}
1413bb739cf0SEdmund Nadolski 
1414dc046b10SJosef Bacik 	ULIST_ITER_INIT(&uiter);
1415dc046b10SJosef Bacik 	while (1) {
1416dc046b10SJosef Bacik 		ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1417f6954245SEdmund Nadolski 					roots, NULL, root->objectid, inum);
1418dc046b10SJosef Bacik 		if (ret == BACKREF_FOUND_SHARED) {
14192c2ed5aaSMark Fasheh 			/* this is the only condition under which we return 1 */
1420dc046b10SJosef Bacik 			ret = 1;
1421dc046b10SJosef Bacik 			break;
1422dc046b10SJosef Bacik 		}
1423dc046b10SJosef Bacik 		if (ret < 0 && ret != -ENOENT)
1424dc046b10SJosef Bacik 			break;
14252c2ed5aaSMark Fasheh 		ret = 0;
1426dc046b10SJosef Bacik 		node = ulist_next(tmp, &uiter);
1427dc046b10SJosef Bacik 		if (!node)
1428dc046b10SJosef Bacik 			break;
1429dc046b10SJosef Bacik 		bytenr = node->val;
1430dc046b10SJosef Bacik 		cond_resched();
1431dc046b10SJosef Bacik 	}
1432bb739cf0SEdmund Nadolski 
1433bb739cf0SEdmund Nadolski 	if (trans) {
1434dc046b10SJosef Bacik 		btrfs_put_tree_mod_seq(fs_info, &elem);
1435bb739cf0SEdmund Nadolski 		btrfs_end_transaction(trans);
1436bb739cf0SEdmund Nadolski 	} else {
1437dc046b10SJosef Bacik 		up_read(&fs_info->commit_root_sem);
1438bb739cf0SEdmund Nadolski 	}
1439dc046b10SJosef Bacik 	ulist_free(tmp);
1440dc046b10SJosef Bacik 	ulist_free(roots);
1441dc046b10SJosef Bacik 	return ret;
1442dc046b10SJosef Bacik }
1443dc046b10SJosef Bacik 
1444f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1445f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
1446f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
1447f186373fSMark Fasheh 			  u64 *found_off)
1448f186373fSMark Fasheh {
1449f186373fSMark Fasheh 	int ret, slot;
1450f186373fSMark Fasheh 	struct btrfs_key key;
1451f186373fSMark Fasheh 	struct btrfs_key found_key;
1452f186373fSMark Fasheh 	struct btrfs_inode_extref *extref;
145373980becSJeff Mahoney 	const struct extent_buffer *leaf;
1454f186373fSMark Fasheh 	unsigned long ptr;
1455f186373fSMark Fasheh 
1456f186373fSMark Fasheh 	key.objectid = inode_objectid;
1457962a298fSDavid Sterba 	key.type = BTRFS_INODE_EXTREF_KEY;
1458f186373fSMark Fasheh 	key.offset = start_off;
1459f186373fSMark Fasheh 
1460f186373fSMark Fasheh 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1461f186373fSMark Fasheh 	if (ret < 0)
1462f186373fSMark Fasheh 		return ret;
1463f186373fSMark Fasheh 
1464f186373fSMark Fasheh 	while (1) {
1465f186373fSMark Fasheh 		leaf = path->nodes[0];
1466f186373fSMark Fasheh 		slot = path->slots[0];
1467f186373fSMark Fasheh 		if (slot >= btrfs_header_nritems(leaf)) {
1468f186373fSMark Fasheh 			/*
1469f186373fSMark Fasheh 			 * If the item at offset is not found,
1470f186373fSMark Fasheh 			 * btrfs_search_slot will point us to the slot
1471f186373fSMark Fasheh 			 * where it should be inserted. In our case
1472f186373fSMark Fasheh 			 * that will be the slot directly before the
1473f186373fSMark Fasheh 			 * next INODE_REF_KEY_V2 item. In the case
1474f186373fSMark Fasheh 			 * that we're pointing to the last slot in a
1475f186373fSMark Fasheh 			 * leaf, we must move one leaf over.
1476f186373fSMark Fasheh 			 */
1477f186373fSMark Fasheh 			ret = btrfs_next_leaf(root, path);
1478f186373fSMark Fasheh 			if (ret) {
1479f186373fSMark Fasheh 				if (ret >= 1)
1480f186373fSMark Fasheh 					ret = -ENOENT;
1481f186373fSMark Fasheh 				break;
1482f186373fSMark Fasheh 			}
1483f186373fSMark Fasheh 			continue;
1484f186373fSMark Fasheh 		}
1485f186373fSMark Fasheh 
1486f186373fSMark Fasheh 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1487f186373fSMark Fasheh 
1488f186373fSMark Fasheh 		/*
1489f186373fSMark Fasheh 		 * Check that we're still looking at an extended ref key for
1490f186373fSMark Fasheh 		 * this particular objectid. If we have different
1491f186373fSMark Fasheh 		 * objectid or type then there are no more to be found
1492f186373fSMark Fasheh 		 * in the tree and we can exit.
1493f186373fSMark Fasheh 		 */
1494f186373fSMark Fasheh 		ret = -ENOENT;
1495f186373fSMark Fasheh 		if (found_key.objectid != inode_objectid)
1496f186373fSMark Fasheh 			break;
1497962a298fSDavid Sterba 		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
1498f186373fSMark Fasheh 			break;
1499f186373fSMark Fasheh 
1500f186373fSMark Fasheh 		ret = 0;
1501f186373fSMark Fasheh 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1502f186373fSMark Fasheh 		extref = (struct btrfs_inode_extref *)ptr;
1503f186373fSMark Fasheh 		*ret_extref = extref;
1504f186373fSMark Fasheh 		if (found_off)
1505f186373fSMark Fasheh 			*found_off = found_key.offset;
1506f186373fSMark Fasheh 		break;
1507f186373fSMark Fasheh 	}
1508f186373fSMark Fasheh 
1509f186373fSMark Fasheh 	return ret;
1510f186373fSMark Fasheh }
1511f186373fSMark Fasheh 
151248a3b636SEric Sandeen /*
151348a3b636SEric Sandeen  * this iterates to turn a name (from iref/extref) into a full filesystem path.
151448a3b636SEric Sandeen  * Elements of the path are separated by '/' and the path is guaranteed to be
151548a3b636SEric Sandeen  * 0-terminated. the path is only given within the current file system.
151648a3b636SEric Sandeen  * Therefore, it never starts with a '/'. the caller is responsible to provide
151748a3b636SEric Sandeen  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
151848a3b636SEric Sandeen  * the start point of the resulting string is returned. this pointer is within
151948a3b636SEric Sandeen  * dest, normally.
152048a3b636SEric Sandeen  * in case the path buffer would overflow, the pointer is decremented further
152148a3b636SEric Sandeen  * as if output was written to the buffer, though no more output is actually
152248a3b636SEric Sandeen  * generated. that way, the caller can determine how much space would be
152348a3b636SEric Sandeen  * required for the path to fit into the buffer. in that case, the returned
152448a3b636SEric Sandeen  * value will be smaller than dest. callers must check this!
152548a3b636SEric Sandeen  */
152696b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1527d24bec3aSMark Fasheh 			u32 name_len, unsigned long name_off,
1528a542ad1bSJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
1529a542ad1bSJan Schmidt 			char *dest, u32 size)
1530a542ad1bSJan Schmidt {
1531a542ad1bSJan Schmidt 	int slot;
1532a542ad1bSJan Schmidt 	u64 next_inum;
1533a542ad1bSJan Schmidt 	int ret;
1534661bec6bSGabriel de Perthuis 	s64 bytes_left = ((s64)size) - 1;
1535a542ad1bSJan Schmidt 	struct extent_buffer *eb = eb_in;
1536a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1537b916a59aSJan Schmidt 	int leave_spinning = path->leave_spinning;
1538d24bec3aSMark Fasheh 	struct btrfs_inode_ref *iref;
1539a542ad1bSJan Schmidt 
1540a542ad1bSJan Schmidt 	if (bytes_left >= 0)
1541a542ad1bSJan Schmidt 		dest[bytes_left] = '\0';
1542a542ad1bSJan Schmidt 
1543b916a59aSJan Schmidt 	path->leave_spinning = 1;
1544a542ad1bSJan Schmidt 	while (1) {
1545d24bec3aSMark Fasheh 		bytes_left -= name_len;
1546a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1547a542ad1bSJan Schmidt 			read_extent_buffer(eb, dest + bytes_left,
1548d24bec3aSMark Fasheh 					   name_off, name_len);
1549b916a59aSJan Schmidt 		if (eb != eb_in) {
15500c0fe3b0SFilipe Manana 			if (!path->skip_locking)
1551b916a59aSJan Schmidt 				btrfs_tree_read_unlock_blocking(eb);
1552a542ad1bSJan Schmidt 			free_extent_buffer(eb);
1553b916a59aSJan Schmidt 		}
1554c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, parent, 0,
1555c234a24dSDavid Sterba 				BTRFS_INODE_REF_KEY, &found_key);
15568f24b496SJan Schmidt 		if (ret > 0)
15578f24b496SJan Schmidt 			ret = -ENOENT;
1558a542ad1bSJan Schmidt 		if (ret)
1559a542ad1bSJan Schmidt 			break;
1560d24bec3aSMark Fasheh 
1561a542ad1bSJan Schmidt 		next_inum = found_key.offset;
1562a542ad1bSJan Schmidt 
1563a542ad1bSJan Schmidt 		/* regular exit ahead */
1564a542ad1bSJan Schmidt 		if (parent == next_inum)
1565a542ad1bSJan Schmidt 			break;
1566a542ad1bSJan Schmidt 
1567a542ad1bSJan Schmidt 		slot = path->slots[0];
1568a542ad1bSJan Schmidt 		eb = path->nodes[0];
1569a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
1570b916a59aSJan Schmidt 		if (eb != eb_in) {
15710c0fe3b0SFilipe Manana 			if (!path->skip_locking)
1572b916a59aSJan Schmidt 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
15730c0fe3b0SFilipe Manana 			path->nodes[0] = NULL;
15740c0fe3b0SFilipe Manana 			path->locks[0] = 0;
1575b916a59aSJan Schmidt 		}
1576a542ad1bSJan Schmidt 		btrfs_release_path(path);
1577a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1578d24bec3aSMark Fasheh 
1579d24bec3aSMark Fasheh 		name_len = btrfs_inode_ref_name_len(eb, iref);
1580d24bec3aSMark Fasheh 		name_off = (unsigned long)(iref + 1);
1581d24bec3aSMark Fasheh 
1582a542ad1bSJan Schmidt 		parent = next_inum;
1583a542ad1bSJan Schmidt 		--bytes_left;
1584a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1585a542ad1bSJan Schmidt 			dest[bytes_left] = '/';
1586a542ad1bSJan Schmidt 	}
1587a542ad1bSJan Schmidt 
1588a542ad1bSJan Schmidt 	btrfs_release_path(path);
1589b916a59aSJan Schmidt 	path->leave_spinning = leave_spinning;
1590a542ad1bSJan Schmidt 
1591a542ad1bSJan Schmidt 	if (ret)
1592a542ad1bSJan Schmidt 		return ERR_PTR(ret);
1593a542ad1bSJan Schmidt 
1594a542ad1bSJan Schmidt 	return dest + bytes_left;
1595a542ad1bSJan Schmidt }
1596a542ad1bSJan Schmidt 
1597a542ad1bSJan Schmidt /*
1598a542ad1bSJan Schmidt  * this makes the path point to (logical EXTENT_ITEM *)
1599a542ad1bSJan Schmidt  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1600a542ad1bSJan Schmidt  * tree blocks and <0 on error.
1601a542ad1bSJan Schmidt  */
1602a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
160369917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
160469917e43SLiu Bo 			u64 *flags_ret)
1605a542ad1bSJan Schmidt {
1606a542ad1bSJan Schmidt 	int ret;
1607a542ad1bSJan Schmidt 	u64 flags;
1608261c84b6SJosef Bacik 	u64 size = 0;
1609a542ad1bSJan Schmidt 	u32 item_size;
161073980becSJeff Mahoney 	const struct extent_buffer *eb;
1611a542ad1bSJan Schmidt 	struct btrfs_extent_item *ei;
1612a542ad1bSJan Schmidt 	struct btrfs_key key;
1613a542ad1bSJan Schmidt 
1614261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1615261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1616261c84b6SJosef Bacik 	else
1617a542ad1bSJan Schmidt 		key.type = BTRFS_EXTENT_ITEM_KEY;
1618a542ad1bSJan Schmidt 	key.objectid = logical;
1619a542ad1bSJan Schmidt 	key.offset = (u64)-1;
1620a542ad1bSJan Schmidt 
1621a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1622a542ad1bSJan Schmidt 	if (ret < 0)
1623a542ad1bSJan Schmidt 		return ret;
1624a542ad1bSJan Schmidt 
1625850a8cdfSWang Shilong 	ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1626850a8cdfSWang Shilong 	if (ret) {
1627850a8cdfSWang Shilong 		if (ret > 0)
1628580f0a67SJosef Bacik 			ret = -ENOENT;
1629580f0a67SJosef Bacik 		return ret;
1630580f0a67SJosef Bacik 	}
1631850a8cdfSWang Shilong 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1632261c84b6SJosef Bacik 	if (found_key->type == BTRFS_METADATA_ITEM_KEY)
1633da17066cSJeff Mahoney 		size = fs_info->nodesize;
1634261c84b6SJosef Bacik 	else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1635261c84b6SJosef Bacik 		size = found_key->offset;
1636261c84b6SJosef Bacik 
1637580f0a67SJosef Bacik 	if (found_key->objectid > logical ||
1638261c84b6SJosef Bacik 	    found_key->objectid + size <= logical) {
1639ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
1640ab8d0fc4SJeff Mahoney 			"logical %llu is not within any extent", logical);
1641a542ad1bSJan Schmidt 		return -ENOENT;
16424692cf58SJan Schmidt 	}
1643a542ad1bSJan Schmidt 
1644a542ad1bSJan Schmidt 	eb = path->nodes[0];
1645a542ad1bSJan Schmidt 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1646a542ad1bSJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
1647a542ad1bSJan Schmidt 
1648a542ad1bSJan Schmidt 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1649a542ad1bSJan Schmidt 	flags = btrfs_extent_flags(eb, ei);
1650a542ad1bSJan Schmidt 
1651ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info,
1652ab8d0fc4SJeff Mahoney 		"logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
1653c1c9ff7cSGeert Uytterhoeven 		 logical, logical - found_key->objectid, found_key->objectid,
1654c1c9ff7cSGeert Uytterhoeven 		 found_key->offset, flags, item_size);
165569917e43SLiu Bo 
165669917e43SLiu Bo 	WARN_ON(!flags_ret);
165769917e43SLiu Bo 	if (flags_ret) {
1658a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
165969917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
166069917e43SLiu Bo 		else if (flags & BTRFS_EXTENT_FLAG_DATA)
166169917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_DATA;
166269917e43SLiu Bo 		else
166369917e43SLiu Bo 			BUG_ON(1);
166469917e43SLiu Bo 		return 0;
166569917e43SLiu Bo 	}
1666a542ad1bSJan Schmidt 
1667a542ad1bSJan Schmidt 	return -EIO;
1668a542ad1bSJan Schmidt }
1669a542ad1bSJan Schmidt 
1670a542ad1bSJan Schmidt /*
1671a542ad1bSJan Schmidt  * helper function to iterate extent inline refs. ptr must point to a 0 value
1672a542ad1bSJan Schmidt  * for the first call and may be modified. it is used to track state.
1673a542ad1bSJan Schmidt  * if more refs exist, 0 is returned and the next call to
1674e0c476b1SJeff Mahoney  * get_extent_inline_ref must pass the modified ptr parameter to get the
1675a542ad1bSJan Schmidt  * next ref. after the last ref was processed, 1 is returned.
1676a542ad1bSJan Schmidt  * returns <0 on error
1677a542ad1bSJan Schmidt  */
1678e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr,
167973980becSJeff Mahoney 				 const struct extent_buffer *eb,
168073980becSJeff Mahoney 				 const struct btrfs_key *key,
168173980becSJeff Mahoney 				 const struct btrfs_extent_item *ei,
168273980becSJeff Mahoney 				 u32 item_size,
1683a542ad1bSJan Schmidt 				 struct btrfs_extent_inline_ref **out_eiref,
1684a542ad1bSJan Schmidt 				 int *out_type)
1685a542ad1bSJan Schmidt {
1686a542ad1bSJan Schmidt 	unsigned long end;
1687a542ad1bSJan Schmidt 	u64 flags;
1688a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1689a542ad1bSJan Schmidt 
1690a542ad1bSJan Schmidt 	if (!*ptr) {
1691a542ad1bSJan Schmidt 		/* first call */
1692a542ad1bSJan Schmidt 		flags = btrfs_extent_flags(eb, ei);
1693a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
16946eda71d0SLiu Bo 			if (key->type == BTRFS_METADATA_ITEM_KEY) {
16956eda71d0SLiu Bo 				/* a skinny metadata extent */
16966eda71d0SLiu Bo 				*out_eiref =
16976eda71d0SLiu Bo 				     (struct btrfs_extent_inline_ref *)(ei + 1);
16986eda71d0SLiu Bo 			} else {
16996eda71d0SLiu Bo 				WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1700a542ad1bSJan Schmidt 				info = (struct btrfs_tree_block_info *)(ei + 1);
1701a542ad1bSJan Schmidt 				*out_eiref =
1702a542ad1bSJan Schmidt 				   (struct btrfs_extent_inline_ref *)(info + 1);
17036eda71d0SLiu Bo 			}
1704a542ad1bSJan Schmidt 		} else {
1705a542ad1bSJan Schmidt 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1706a542ad1bSJan Schmidt 		}
1707a542ad1bSJan Schmidt 		*ptr = (unsigned long)*out_eiref;
1708cd857dd6SLiu Bo 		if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
1709a542ad1bSJan Schmidt 			return -ENOENT;
1710a542ad1bSJan Schmidt 	}
1711a542ad1bSJan Schmidt 
1712a542ad1bSJan Schmidt 	end = (unsigned long)ei + item_size;
17136eda71d0SLiu Bo 	*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
1714a542ad1bSJan Schmidt 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1715a542ad1bSJan Schmidt 
1716a542ad1bSJan Schmidt 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1717a542ad1bSJan Schmidt 	WARN_ON(*ptr > end);
1718a542ad1bSJan Schmidt 	if (*ptr == end)
1719a542ad1bSJan Schmidt 		return 1; /* last */
1720a542ad1bSJan Schmidt 
1721a542ad1bSJan Schmidt 	return 0;
1722a542ad1bSJan Schmidt }
1723a542ad1bSJan Schmidt 
1724a542ad1bSJan Schmidt /*
1725a542ad1bSJan Schmidt  * reads the tree block backref for an extent. tree level and root are returned
1726a542ad1bSJan Schmidt  * through out_level and out_root. ptr must point to a 0 value for the first
1727e0c476b1SJeff Mahoney  * call and may be modified (see get_extent_inline_ref comment).
1728a542ad1bSJan Schmidt  * returns 0 if data was provided, 1 if there was no more data to provide or
1729a542ad1bSJan Schmidt  * <0 on error.
1730a542ad1bSJan Schmidt  */
1731a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
17326eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
17336eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level)
1734a542ad1bSJan Schmidt {
1735a542ad1bSJan Schmidt 	int ret;
1736a542ad1bSJan Schmidt 	int type;
1737a542ad1bSJan Schmidt 	struct btrfs_extent_inline_ref *eiref;
1738a542ad1bSJan Schmidt 
1739a542ad1bSJan Schmidt 	if (*ptr == (unsigned long)-1)
1740a542ad1bSJan Schmidt 		return 1;
1741a542ad1bSJan Schmidt 
1742a542ad1bSJan Schmidt 	while (1) {
1743e0c476b1SJeff Mahoney 		ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
1744a542ad1bSJan Schmidt 					      &eiref, &type);
1745a542ad1bSJan Schmidt 		if (ret < 0)
1746a542ad1bSJan Schmidt 			return ret;
1747a542ad1bSJan Schmidt 
1748a542ad1bSJan Schmidt 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1749a542ad1bSJan Schmidt 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1750a542ad1bSJan Schmidt 			break;
1751a542ad1bSJan Schmidt 
1752a542ad1bSJan Schmidt 		if (ret == 1)
1753a542ad1bSJan Schmidt 			return 1;
1754a542ad1bSJan Schmidt 	}
1755a542ad1bSJan Schmidt 
1756a542ad1bSJan Schmidt 	/* we can treat both ref types equally here */
1757a542ad1bSJan Schmidt 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1758a1317f45SFilipe Manana 
1759a1317f45SFilipe Manana 	if (key->type == BTRFS_EXTENT_ITEM_KEY) {
1760a1317f45SFilipe Manana 		struct btrfs_tree_block_info *info;
1761a1317f45SFilipe Manana 
1762a1317f45SFilipe Manana 		info = (struct btrfs_tree_block_info *)(ei + 1);
1763a542ad1bSJan Schmidt 		*out_level = btrfs_tree_block_level(eb, info);
1764a1317f45SFilipe Manana 	} else {
1765a1317f45SFilipe Manana 		ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
1766a1317f45SFilipe Manana 		*out_level = (u8)key->offset;
1767a1317f45SFilipe Manana 	}
1768a542ad1bSJan Schmidt 
1769a542ad1bSJan Schmidt 	if (ret == 1)
1770a542ad1bSJan Schmidt 		*ptr = (unsigned long)-1;
1771a542ad1bSJan Schmidt 
1772a542ad1bSJan Schmidt 	return 0;
1773a542ad1bSJan Schmidt }
1774a542ad1bSJan Schmidt 
1775ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
1776ab8d0fc4SJeff Mahoney 			     struct extent_inode_elem *inode_list,
1777976b1908SJan Schmidt 			     u64 root, u64 extent_item_objectid,
17784692cf58SJan Schmidt 			     iterate_extent_inodes_t *iterate, void *ctx)
1779a542ad1bSJan Schmidt {
1780976b1908SJan Schmidt 	struct extent_inode_elem *eie;
17814692cf58SJan Schmidt 	int ret = 0;
1782a542ad1bSJan Schmidt 
1783976b1908SJan Schmidt 	for (eie = inode_list; eie; eie = eie->next) {
1784ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
1785ab8d0fc4SJeff Mahoney 			    "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
1786ab8d0fc4SJeff Mahoney 			    extent_item_objectid, eie->inum,
1787ab8d0fc4SJeff Mahoney 			    eie->offset, root);
1788976b1908SJan Schmidt 		ret = iterate(eie->inum, eie->offset, root, ctx);
17894692cf58SJan Schmidt 		if (ret) {
1790ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_info,
1791ab8d0fc4SJeff Mahoney 				    "stopping iteration for %llu due to ret=%d",
1792976b1908SJan Schmidt 				    extent_item_objectid, ret);
1793a542ad1bSJan Schmidt 			break;
1794a542ad1bSJan Schmidt 		}
1795a542ad1bSJan Schmidt 	}
1796a542ad1bSJan Schmidt 
1797a542ad1bSJan Schmidt 	return ret;
1798a542ad1bSJan Schmidt }
1799a542ad1bSJan Schmidt 
1800a542ad1bSJan Schmidt /*
1801a542ad1bSJan Schmidt  * calls iterate() for every inode that references the extent identified by
18024692cf58SJan Schmidt  * the given parameters.
1803a542ad1bSJan Schmidt  * when the iterator function returns a non-zero value, iteration stops.
1804a542ad1bSJan Schmidt  */
1805a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
18064692cf58SJan Schmidt 				u64 extent_item_objectid, u64 extent_item_pos,
18077a3ae2f8SJan Schmidt 				int search_commit_root,
1808a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1809a542ad1bSJan Schmidt {
1810a542ad1bSJan Schmidt 	int ret;
1811da61d31aSJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
18127a3ae2f8SJan Schmidt 	struct ulist *refs = NULL;
18137a3ae2f8SJan Schmidt 	struct ulist *roots = NULL;
18144692cf58SJan Schmidt 	struct ulist_node *ref_node = NULL;
18154692cf58SJan Schmidt 	struct ulist_node *root_node = NULL;
18163284da7bSDavid Sterba 	struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
1817cd1b413cSJan Schmidt 	struct ulist_iterator ref_uiter;
1818cd1b413cSJan Schmidt 	struct ulist_iterator root_uiter;
1819a542ad1bSJan Schmidt 
1820ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info, "resolving all inodes for extent %llu",
18214692cf58SJan Schmidt 			extent_item_objectid);
18224692cf58SJan Schmidt 
1823da61d31aSJosef Bacik 	if (!search_commit_root) {
18247a3ae2f8SJan Schmidt 		trans = btrfs_join_transaction(fs_info->extent_root);
18257a3ae2f8SJan Schmidt 		if (IS_ERR(trans))
18267a3ae2f8SJan Schmidt 			return PTR_ERR(trans);
18278445f61cSJan Schmidt 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
18289e351cc8SJosef Bacik 	} else {
18299e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
18307a3ae2f8SJan Schmidt 	}
18314692cf58SJan Schmidt 
18324692cf58SJan Schmidt 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1833097b8a7cSJan Schmidt 				   tree_mod_seq_elem.seq, &refs,
18348445f61cSJan Schmidt 				   &extent_item_pos);
18354692cf58SJan Schmidt 	if (ret)
18364692cf58SJan Schmidt 		goto out;
18374692cf58SJan Schmidt 
1838cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&ref_uiter);
1839cd1b413cSJan Schmidt 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
1840e0c476b1SJeff Mahoney 		ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
18418445f61cSJan Schmidt 						tree_mod_seq_elem.seq, &roots);
18424692cf58SJan Schmidt 		if (ret)
1843a542ad1bSJan Schmidt 			break;
1844cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&root_uiter);
1845cd1b413cSJan Schmidt 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
1846ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_info,
1847ab8d0fc4SJeff Mahoney 				    "root %llu references leaf %llu, data list %#llx",
1848ab8d0fc4SJeff Mahoney 				    root_node->val, ref_node->val,
1849c1c9ff7cSGeert Uytterhoeven 				    ref_node->aux);
1850ab8d0fc4SJeff Mahoney 			ret = iterate_leaf_refs(fs_info,
1851ab8d0fc4SJeff Mahoney 						(struct extent_inode_elem *)
1852995e01b7SJan Schmidt 						(uintptr_t)ref_node->aux,
1853995e01b7SJan Schmidt 						root_node->val,
1854995e01b7SJan Schmidt 						extent_item_objectid,
1855a542ad1bSJan Schmidt 						iterate, ctx);
18564692cf58SJan Schmidt 		}
1857976b1908SJan Schmidt 		ulist_free(roots);
1858a542ad1bSJan Schmidt 	}
1859a542ad1bSJan Schmidt 
1860976b1908SJan Schmidt 	free_leaf_list(refs);
18614692cf58SJan Schmidt out:
18627a3ae2f8SJan Schmidt 	if (!search_commit_root) {
18638445f61cSJan Schmidt 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
18643a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
18659e351cc8SJosef Bacik 	} else {
18669e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
18677a3ae2f8SJan Schmidt 	}
18687a3ae2f8SJan Schmidt 
1869a542ad1bSJan Schmidt 	return ret;
1870a542ad1bSJan Schmidt }
1871a542ad1bSJan Schmidt 
1872a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1873a542ad1bSJan Schmidt 				struct btrfs_path *path,
1874a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1875a542ad1bSJan Schmidt {
1876a542ad1bSJan Schmidt 	int ret;
18774692cf58SJan Schmidt 	u64 extent_item_pos;
187869917e43SLiu Bo 	u64 flags = 0;
1879a542ad1bSJan Schmidt 	struct btrfs_key found_key;
18807a3ae2f8SJan Schmidt 	int search_commit_root = path->search_commit_root;
1881a542ad1bSJan Schmidt 
188269917e43SLiu Bo 	ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
18834692cf58SJan Schmidt 	btrfs_release_path(path);
1884a542ad1bSJan Schmidt 	if (ret < 0)
1885a542ad1bSJan Schmidt 		return ret;
188669917e43SLiu Bo 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
18873627bf45SStefan Behrens 		return -EINVAL;
1888a542ad1bSJan Schmidt 
18894692cf58SJan Schmidt 	extent_item_pos = logical - found_key.objectid;
18907a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
18917a3ae2f8SJan Schmidt 					extent_item_pos, search_commit_root,
18927a3ae2f8SJan Schmidt 					iterate, ctx);
1893a542ad1bSJan Schmidt 
1894a542ad1bSJan Schmidt 	return ret;
1895a542ad1bSJan Schmidt }
1896a542ad1bSJan Schmidt 
1897d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1898d24bec3aSMark Fasheh 			      struct extent_buffer *eb, void *ctx);
1899d24bec3aSMark Fasheh 
1900d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1901a542ad1bSJan Schmidt 			      struct btrfs_path *path,
1902a542ad1bSJan Schmidt 			      iterate_irefs_t *iterate, void *ctx)
1903a542ad1bSJan Schmidt {
1904aefc1eb1SJan Schmidt 	int ret = 0;
1905a542ad1bSJan Schmidt 	int slot;
1906a542ad1bSJan Schmidt 	u32 cur;
1907a542ad1bSJan Schmidt 	u32 len;
1908a542ad1bSJan Schmidt 	u32 name_len;
1909a542ad1bSJan Schmidt 	u64 parent = 0;
1910a542ad1bSJan Schmidt 	int found = 0;
1911a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1912a542ad1bSJan Schmidt 	struct btrfs_item *item;
1913a542ad1bSJan Schmidt 	struct btrfs_inode_ref *iref;
1914a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1915a542ad1bSJan Schmidt 
1916aefc1eb1SJan Schmidt 	while (!ret) {
1917c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, inum,
1918c234a24dSDavid Sterba 				parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
1919a542ad1bSJan Schmidt 				&found_key);
1920c234a24dSDavid Sterba 
1921a542ad1bSJan Schmidt 		if (ret < 0)
1922a542ad1bSJan Schmidt 			break;
1923a542ad1bSJan Schmidt 		if (ret) {
1924a542ad1bSJan Schmidt 			ret = found ? 0 : -ENOENT;
1925a542ad1bSJan Schmidt 			break;
1926a542ad1bSJan Schmidt 		}
1927a542ad1bSJan Schmidt 		++found;
1928a542ad1bSJan Schmidt 
1929a542ad1bSJan Schmidt 		parent = found_key.offset;
1930a542ad1bSJan Schmidt 		slot = path->slots[0];
19313fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
19323fe81ce2SFilipe David Borba Manana 		if (!eb) {
19333fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
19343fe81ce2SFilipe David Borba Manana 			break;
19353fe81ce2SFilipe David Borba Manana 		}
19363fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
1937b916a59aSJan Schmidt 		btrfs_tree_read_lock(eb);
1938b916a59aSJan Schmidt 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1939a542ad1bSJan Schmidt 		btrfs_release_path(path);
1940a542ad1bSJan Schmidt 
1941dd3cc16bSRoss Kirk 		item = btrfs_item_nr(slot);
1942a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1943a542ad1bSJan Schmidt 
1944a542ad1bSJan Schmidt 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1945a542ad1bSJan Schmidt 			name_len = btrfs_inode_ref_name_len(eb, iref);
1946a542ad1bSJan Schmidt 			/* path must be released before calling iterate()! */
1947ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_root->fs_info,
1948ab8d0fc4SJeff Mahoney 				"following ref at offset %u for inode %llu in tree %llu",
1949ab8d0fc4SJeff Mahoney 				cur, found_key.objectid, fs_root->objectid);
1950d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
1951d24bec3aSMark Fasheh 				      (unsigned long)(iref + 1), eb, ctx);
1952aefc1eb1SJan Schmidt 			if (ret)
1953a542ad1bSJan Schmidt 				break;
1954a542ad1bSJan Schmidt 			len = sizeof(*iref) + name_len;
1955a542ad1bSJan Schmidt 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
1956a542ad1bSJan Schmidt 		}
1957b916a59aSJan Schmidt 		btrfs_tree_read_unlock_blocking(eb);
1958a542ad1bSJan Schmidt 		free_extent_buffer(eb);
1959a542ad1bSJan Schmidt 	}
1960a542ad1bSJan Schmidt 
1961a542ad1bSJan Schmidt 	btrfs_release_path(path);
1962a542ad1bSJan Schmidt 
1963a542ad1bSJan Schmidt 	return ret;
1964a542ad1bSJan Schmidt }
1965a542ad1bSJan Schmidt 
1966d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
1967d24bec3aSMark Fasheh 				 struct btrfs_path *path,
1968d24bec3aSMark Fasheh 				 iterate_irefs_t *iterate, void *ctx)
1969d24bec3aSMark Fasheh {
1970d24bec3aSMark Fasheh 	int ret;
1971d24bec3aSMark Fasheh 	int slot;
1972d24bec3aSMark Fasheh 	u64 offset = 0;
1973d24bec3aSMark Fasheh 	u64 parent;
1974d24bec3aSMark Fasheh 	int found = 0;
1975d24bec3aSMark Fasheh 	struct extent_buffer *eb;
1976d24bec3aSMark Fasheh 	struct btrfs_inode_extref *extref;
1977d24bec3aSMark Fasheh 	u32 item_size;
1978d24bec3aSMark Fasheh 	u32 cur_offset;
1979d24bec3aSMark Fasheh 	unsigned long ptr;
1980d24bec3aSMark Fasheh 
1981d24bec3aSMark Fasheh 	while (1) {
1982d24bec3aSMark Fasheh 		ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
1983d24bec3aSMark Fasheh 					    &offset);
1984d24bec3aSMark Fasheh 		if (ret < 0)
1985d24bec3aSMark Fasheh 			break;
1986d24bec3aSMark Fasheh 		if (ret) {
1987d24bec3aSMark Fasheh 			ret = found ? 0 : -ENOENT;
1988d24bec3aSMark Fasheh 			break;
1989d24bec3aSMark Fasheh 		}
1990d24bec3aSMark Fasheh 		++found;
1991d24bec3aSMark Fasheh 
1992d24bec3aSMark Fasheh 		slot = path->slots[0];
19933fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
19943fe81ce2SFilipe David Borba Manana 		if (!eb) {
19953fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
19963fe81ce2SFilipe David Borba Manana 			break;
19973fe81ce2SFilipe David Borba Manana 		}
19983fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
1999d24bec3aSMark Fasheh 
2000d24bec3aSMark Fasheh 		btrfs_tree_read_lock(eb);
2001d24bec3aSMark Fasheh 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2002d24bec3aSMark Fasheh 		btrfs_release_path(path);
2003d24bec3aSMark Fasheh 
20042849a854SChris Mason 		item_size = btrfs_item_size_nr(eb, slot);
20052849a854SChris Mason 		ptr = btrfs_item_ptr_offset(eb, slot);
2006d24bec3aSMark Fasheh 		cur_offset = 0;
2007d24bec3aSMark Fasheh 
2008d24bec3aSMark Fasheh 		while (cur_offset < item_size) {
2009d24bec3aSMark Fasheh 			u32 name_len;
2010d24bec3aSMark Fasheh 
2011d24bec3aSMark Fasheh 			extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
2012d24bec3aSMark Fasheh 			parent = btrfs_inode_extref_parent(eb, extref);
2013d24bec3aSMark Fasheh 			name_len = btrfs_inode_extref_name_len(eb, extref);
2014d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
2015d24bec3aSMark Fasheh 				      (unsigned long)&extref->name, eb, ctx);
2016d24bec3aSMark Fasheh 			if (ret)
2017d24bec3aSMark Fasheh 				break;
2018d24bec3aSMark Fasheh 
20192849a854SChris Mason 			cur_offset += btrfs_inode_extref_name_len(eb, extref);
2020d24bec3aSMark Fasheh 			cur_offset += sizeof(*extref);
2021d24bec3aSMark Fasheh 		}
2022d24bec3aSMark Fasheh 		btrfs_tree_read_unlock_blocking(eb);
2023d24bec3aSMark Fasheh 		free_extent_buffer(eb);
2024d24bec3aSMark Fasheh 
2025d24bec3aSMark Fasheh 		offset++;
2026d24bec3aSMark Fasheh 	}
2027d24bec3aSMark Fasheh 
2028d24bec3aSMark Fasheh 	btrfs_release_path(path);
2029d24bec3aSMark Fasheh 
2030d24bec3aSMark Fasheh 	return ret;
2031d24bec3aSMark Fasheh }
2032d24bec3aSMark Fasheh 
2033d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
2034d24bec3aSMark Fasheh 			 struct btrfs_path *path, iterate_irefs_t *iterate,
2035d24bec3aSMark Fasheh 			 void *ctx)
2036d24bec3aSMark Fasheh {
2037d24bec3aSMark Fasheh 	int ret;
2038d24bec3aSMark Fasheh 	int found_refs = 0;
2039d24bec3aSMark Fasheh 
2040d24bec3aSMark Fasheh 	ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
2041d24bec3aSMark Fasheh 	if (!ret)
2042d24bec3aSMark Fasheh 		++found_refs;
2043d24bec3aSMark Fasheh 	else if (ret != -ENOENT)
2044d24bec3aSMark Fasheh 		return ret;
2045d24bec3aSMark Fasheh 
2046d24bec3aSMark Fasheh 	ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
2047d24bec3aSMark Fasheh 	if (ret == -ENOENT && found_refs)
2048d24bec3aSMark Fasheh 		return 0;
2049d24bec3aSMark Fasheh 
2050d24bec3aSMark Fasheh 	return ret;
2051d24bec3aSMark Fasheh }
2052d24bec3aSMark Fasheh 
2053a542ad1bSJan Schmidt /*
2054a542ad1bSJan Schmidt  * returns 0 if the path could be dumped (probably truncated)
2055a542ad1bSJan Schmidt  * returns <0 in case of an error
2056a542ad1bSJan Schmidt  */
2057d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
2058a542ad1bSJan Schmidt 			 struct extent_buffer *eb, void *ctx)
2059a542ad1bSJan Schmidt {
2060a542ad1bSJan Schmidt 	struct inode_fs_paths *ipath = ctx;
2061a542ad1bSJan Schmidt 	char *fspath;
2062a542ad1bSJan Schmidt 	char *fspath_min;
2063a542ad1bSJan Schmidt 	int i = ipath->fspath->elem_cnt;
2064a542ad1bSJan Schmidt 	const int s_ptr = sizeof(char *);
2065a542ad1bSJan Schmidt 	u32 bytes_left;
2066a542ad1bSJan Schmidt 
2067a542ad1bSJan Schmidt 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
2068a542ad1bSJan Schmidt 					ipath->fspath->bytes_left - s_ptr : 0;
2069a542ad1bSJan Schmidt 
2070740c3d22SChris Mason 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
207196b5bd77SJan Schmidt 	fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
207296b5bd77SJan Schmidt 				   name_off, eb, inum, fspath_min, bytes_left);
2073a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
2074a542ad1bSJan Schmidt 		return PTR_ERR(fspath);
2075a542ad1bSJan Schmidt 
2076a542ad1bSJan Schmidt 	if (fspath > fspath_min) {
2077745c4d8eSJeff Mahoney 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
2078a542ad1bSJan Schmidt 		++ipath->fspath->elem_cnt;
2079a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = fspath - fspath_min;
2080a542ad1bSJan Schmidt 	} else {
2081a542ad1bSJan Schmidt 		++ipath->fspath->elem_missed;
2082a542ad1bSJan Schmidt 		ipath->fspath->bytes_missing += fspath_min - fspath;
2083a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = 0;
2084a542ad1bSJan Schmidt 	}
2085a542ad1bSJan Schmidt 
2086a542ad1bSJan Schmidt 	return 0;
2087a542ad1bSJan Schmidt }
2088a542ad1bSJan Schmidt 
2089a542ad1bSJan Schmidt /*
2090a542ad1bSJan Schmidt  * this dumps all file system paths to the inode into the ipath struct, provided
2091a542ad1bSJan Schmidt  * is has been created large enough. each path is zero-terminated and accessed
2092740c3d22SChris Mason  * from ipath->fspath->val[i].
2093a542ad1bSJan Schmidt  * when it returns, there are ipath->fspath->elem_cnt number of paths available
2094740c3d22SChris Mason  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
209501327610SNicholas D Steeves  * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
2096a542ad1bSJan Schmidt  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
2097a542ad1bSJan Schmidt  * have been needed to return all paths.
2098a542ad1bSJan Schmidt  */
2099a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
2100a542ad1bSJan Schmidt {
2101a542ad1bSJan Schmidt 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
2102a542ad1bSJan Schmidt 			     inode_to_path, ipath);
2103a542ad1bSJan Schmidt }
2104a542ad1bSJan Schmidt 
2105a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes)
2106a542ad1bSJan Schmidt {
2107a542ad1bSJan Schmidt 	struct btrfs_data_container *data;
2108a542ad1bSJan Schmidt 	size_t alloc_bytes;
2109a542ad1bSJan Schmidt 
2110a542ad1bSJan Schmidt 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
2111f54de068SDavid Sterba 	data = kvmalloc(alloc_bytes, GFP_KERNEL);
2112a542ad1bSJan Schmidt 	if (!data)
2113a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2114a542ad1bSJan Schmidt 
2115a542ad1bSJan Schmidt 	if (total_bytes >= sizeof(*data)) {
2116a542ad1bSJan Schmidt 		data->bytes_left = total_bytes - sizeof(*data);
2117a542ad1bSJan Schmidt 		data->bytes_missing = 0;
2118a542ad1bSJan Schmidt 	} else {
2119a542ad1bSJan Schmidt 		data->bytes_missing = sizeof(*data) - total_bytes;
2120a542ad1bSJan Schmidt 		data->bytes_left = 0;
2121a542ad1bSJan Schmidt 	}
2122a542ad1bSJan Schmidt 
2123a542ad1bSJan Schmidt 	data->elem_cnt = 0;
2124a542ad1bSJan Schmidt 	data->elem_missed = 0;
2125a542ad1bSJan Schmidt 
2126a542ad1bSJan Schmidt 	return data;
2127a542ad1bSJan Schmidt }
2128a542ad1bSJan Schmidt 
2129a542ad1bSJan Schmidt /*
2130a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
2131a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
2132a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
2133a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
2134a542ad1bSJan Schmidt  */
2135a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
2136a542ad1bSJan Schmidt 					struct btrfs_path *path)
2137a542ad1bSJan Schmidt {
2138a542ad1bSJan Schmidt 	struct inode_fs_paths *ifp;
2139a542ad1bSJan Schmidt 	struct btrfs_data_container *fspath;
2140a542ad1bSJan Schmidt 
2141a542ad1bSJan Schmidt 	fspath = init_data_container(total_bytes);
2142a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
2143a542ad1bSJan Schmidt 		return (void *)fspath;
2144a542ad1bSJan Schmidt 
2145f54de068SDavid Sterba 	ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
2146a542ad1bSJan Schmidt 	if (!ifp) {
2147f54de068SDavid Sterba 		kvfree(fspath);
2148a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2149a542ad1bSJan Schmidt 	}
2150a542ad1bSJan Schmidt 
2151a542ad1bSJan Schmidt 	ifp->btrfs_path = path;
2152a542ad1bSJan Schmidt 	ifp->fspath = fspath;
2153a542ad1bSJan Schmidt 	ifp->fs_root = fs_root;
2154a542ad1bSJan Schmidt 
2155a542ad1bSJan Schmidt 	return ifp;
2156a542ad1bSJan Schmidt }
2157a542ad1bSJan Schmidt 
2158a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath)
2159a542ad1bSJan Schmidt {
21604735fb28SJesper Juhl 	if (!ipath)
21614735fb28SJesper Juhl 		return;
2162f54de068SDavid Sterba 	kvfree(ipath->fspath);
2163a542ad1bSJan Schmidt 	kfree(ipath);
2164a542ad1bSJan Schmidt }
2165