xref: /openbmc/linux/fs/btrfs/backref.c (revision c995ab3cda3f4178c1f1a47926bea5f8372880cb)
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>
2100142756SJeff 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,
43*c995ab3cSZygo Blaxell 			      struct extent_inode_elem **eie,
44*c995ab3cSZygo Blaxell 			      bool ignore_offset)
45976b1908SJan Schmidt {
468ca15e05SJosef Bacik 	u64 offset = 0;
478ca15e05SJosef Bacik 	struct extent_inode_elem *e;
488ca15e05SJosef Bacik 
49*c995ab3cSZygo Blaxell 	if (!ignore_offset &&
50*c995ab3cSZygo Blaxell 	    !btrfs_file_extent_compression(eb, fi) &&
518ca15e05SJosef Bacik 	    !btrfs_file_extent_encryption(eb, fi) &&
528ca15e05SJosef Bacik 	    !btrfs_file_extent_other_encoding(eb, fi)) {
53976b1908SJan Schmidt 		u64 data_offset;
54976b1908SJan Schmidt 		u64 data_len;
55976b1908SJan Schmidt 
56976b1908SJan Schmidt 		data_offset = btrfs_file_extent_offset(eb, fi);
57976b1908SJan Schmidt 		data_len = btrfs_file_extent_num_bytes(eb, fi);
58976b1908SJan Schmidt 
59976b1908SJan Schmidt 		if (extent_item_pos < data_offset ||
60976b1908SJan Schmidt 		    extent_item_pos >= data_offset + data_len)
61976b1908SJan Schmidt 			return 1;
628ca15e05SJosef Bacik 		offset = extent_item_pos - data_offset;
638ca15e05SJosef Bacik 	}
64976b1908SJan Schmidt 
65976b1908SJan Schmidt 	e = kmalloc(sizeof(*e), GFP_NOFS);
66976b1908SJan Schmidt 	if (!e)
67976b1908SJan Schmidt 		return -ENOMEM;
68976b1908SJan Schmidt 
69976b1908SJan Schmidt 	e->next = *eie;
70976b1908SJan Schmidt 	e->inum = key->objectid;
718ca15e05SJosef Bacik 	e->offset = key->offset + offset;
72976b1908SJan Schmidt 	*eie = e;
73976b1908SJan Schmidt 
74976b1908SJan Schmidt 	return 0;
75976b1908SJan Schmidt }
76976b1908SJan Schmidt 
77f05c4746SWang Shilong static void free_inode_elem_list(struct extent_inode_elem *eie)
78f05c4746SWang Shilong {
79f05c4746SWang Shilong 	struct extent_inode_elem *eie_next;
80f05c4746SWang Shilong 
81f05c4746SWang Shilong 	for (; eie; eie = eie_next) {
82f05c4746SWang Shilong 		eie_next = eie->next;
83f05c4746SWang Shilong 		kfree(eie);
84f05c4746SWang Shilong 	}
85f05c4746SWang Shilong }
86f05c4746SWang Shilong 
8773980becSJeff Mahoney static int find_extent_in_eb(const struct extent_buffer *eb,
8873980becSJeff Mahoney 			     u64 wanted_disk_byte, u64 extent_item_pos,
89*c995ab3cSZygo Blaxell 			     struct extent_inode_elem **eie,
90*c995ab3cSZygo Blaxell 			     bool ignore_offset)
91976b1908SJan Schmidt {
92976b1908SJan Schmidt 	u64 disk_byte;
93976b1908SJan Schmidt 	struct btrfs_key key;
94976b1908SJan Schmidt 	struct btrfs_file_extent_item *fi;
95976b1908SJan Schmidt 	int slot;
96976b1908SJan Schmidt 	int nritems;
97976b1908SJan Schmidt 	int extent_type;
98976b1908SJan Schmidt 	int ret;
99976b1908SJan Schmidt 
100976b1908SJan Schmidt 	/*
101976b1908SJan Schmidt 	 * from the shared data ref, we only have the leaf but we need
102976b1908SJan Schmidt 	 * the key. thus, we must look into all items and see that we
103976b1908SJan Schmidt 	 * find one (some) with a reference to our extent item.
104976b1908SJan Schmidt 	 */
105976b1908SJan Schmidt 	nritems = btrfs_header_nritems(eb);
106976b1908SJan Schmidt 	for (slot = 0; slot < nritems; ++slot) {
107976b1908SJan Schmidt 		btrfs_item_key_to_cpu(eb, &key, slot);
108976b1908SJan Schmidt 		if (key.type != BTRFS_EXTENT_DATA_KEY)
109976b1908SJan Schmidt 			continue;
110976b1908SJan Schmidt 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
111976b1908SJan Schmidt 		extent_type = btrfs_file_extent_type(eb, fi);
112976b1908SJan Schmidt 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
113976b1908SJan Schmidt 			continue;
114976b1908SJan Schmidt 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
115976b1908SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
116976b1908SJan Schmidt 		if (disk_byte != wanted_disk_byte)
117976b1908SJan Schmidt 			continue;
118976b1908SJan Schmidt 
119*c995ab3cSZygo Blaxell 		ret = check_extent_in_eb(&key, eb, fi, extent_item_pos, eie, ignore_offset);
120976b1908SJan Schmidt 		if (ret < 0)
121976b1908SJan Schmidt 			return ret;
122976b1908SJan Schmidt 	}
123976b1908SJan Schmidt 
124976b1908SJan Schmidt 	return 0;
125976b1908SJan Schmidt }
126976b1908SJan Schmidt 
12786d5f994SEdmund Nadolski struct preftree {
12886d5f994SEdmund Nadolski 	struct rb_root root;
1296c336b21SJeff Mahoney 	unsigned int count;
13086d5f994SEdmund Nadolski };
13186d5f994SEdmund Nadolski 
1326c336b21SJeff Mahoney #define PREFTREE_INIT	{ .root = RB_ROOT, .count = 0 }
13386d5f994SEdmund Nadolski 
13486d5f994SEdmund Nadolski struct preftrees {
13586d5f994SEdmund Nadolski 	struct preftree direct;    /* BTRFS_SHARED_[DATA|BLOCK]_REF_KEY */
13686d5f994SEdmund Nadolski 	struct preftree indirect;  /* BTRFS_[TREE_BLOCK|EXTENT_DATA]_REF_KEY */
13786d5f994SEdmund Nadolski 	struct preftree indirect_missing_keys;
13886d5f994SEdmund Nadolski };
13986d5f994SEdmund Nadolski 
1403ec4d323SEdmund Nadolski /*
1413ec4d323SEdmund Nadolski  * Checks for a shared extent during backref search.
1423ec4d323SEdmund Nadolski  *
1433ec4d323SEdmund Nadolski  * The share_count tracks prelim_refs (direct and indirect) having a
1443ec4d323SEdmund Nadolski  * ref->count >0:
1453ec4d323SEdmund Nadolski  *  - incremented when a ref->count transitions to >0
1463ec4d323SEdmund Nadolski  *  - decremented when a ref->count transitions to <1
1473ec4d323SEdmund Nadolski  */
1483ec4d323SEdmund Nadolski struct share_check {
1493ec4d323SEdmund Nadolski 	u64 root_objectid;
1503ec4d323SEdmund Nadolski 	u64 inum;
1513ec4d323SEdmund Nadolski 	int share_count;
1523ec4d323SEdmund Nadolski };
1533ec4d323SEdmund Nadolski 
1543ec4d323SEdmund Nadolski static inline int extent_is_shared(struct share_check *sc)
1553ec4d323SEdmund Nadolski {
1563ec4d323SEdmund Nadolski 	return (sc && sc->share_count > 1) ? BACKREF_FOUND_SHARED : 0;
1573ec4d323SEdmund Nadolski }
1583ec4d323SEdmund Nadolski 
159b9e9a6cbSWang Shilong static struct kmem_cache *btrfs_prelim_ref_cache;
160b9e9a6cbSWang Shilong 
161b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void)
162b9e9a6cbSWang Shilong {
163b9e9a6cbSWang Shilong 	btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
164e0c476b1SJeff Mahoney 					sizeof(struct prelim_ref),
165b9e9a6cbSWang Shilong 					0,
166fba4b697SNikolay Borisov 					SLAB_MEM_SPREAD,
167b9e9a6cbSWang Shilong 					NULL);
168b9e9a6cbSWang Shilong 	if (!btrfs_prelim_ref_cache)
169b9e9a6cbSWang Shilong 		return -ENOMEM;
170b9e9a6cbSWang Shilong 	return 0;
171b9e9a6cbSWang Shilong }
172b9e9a6cbSWang Shilong 
173b9e9a6cbSWang Shilong void btrfs_prelim_ref_exit(void)
174b9e9a6cbSWang Shilong {
175b9e9a6cbSWang Shilong 	kmem_cache_destroy(btrfs_prelim_ref_cache);
176b9e9a6cbSWang Shilong }
177b9e9a6cbSWang Shilong 
17886d5f994SEdmund Nadolski static void free_pref(struct prelim_ref *ref)
17986d5f994SEdmund Nadolski {
18086d5f994SEdmund Nadolski 	kmem_cache_free(btrfs_prelim_ref_cache, ref);
18186d5f994SEdmund Nadolski }
18286d5f994SEdmund Nadolski 
18386d5f994SEdmund Nadolski /*
18486d5f994SEdmund Nadolski  * Return 0 when both refs are for the same block (and can be merged).
18586d5f994SEdmund Nadolski  * A -1 return indicates ref1 is a 'lower' block than ref2, while 1
18686d5f994SEdmund Nadolski  * indicates a 'higher' block.
18786d5f994SEdmund Nadolski  */
18886d5f994SEdmund Nadolski static int prelim_ref_compare(struct prelim_ref *ref1,
18986d5f994SEdmund Nadolski 			      struct prelim_ref *ref2)
19086d5f994SEdmund Nadolski {
19186d5f994SEdmund Nadolski 	if (ref1->level < ref2->level)
19286d5f994SEdmund Nadolski 		return -1;
19386d5f994SEdmund Nadolski 	if (ref1->level > ref2->level)
19486d5f994SEdmund Nadolski 		return 1;
19586d5f994SEdmund Nadolski 	if (ref1->root_id < ref2->root_id)
19686d5f994SEdmund Nadolski 		return -1;
19786d5f994SEdmund Nadolski 	if (ref1->root_id > ref2->root_id)
19886d5f994SEdmund Nadolski 		return 1;
19986d5f994SEdmund Nadolski 	if (ref1->key_for_search.type < ref2->key_for_search.type)
20086d5f994SEdmund Nadolski 		return -1;
20186d5f994SEdmund Nadolski 	if (ref1->key_for_search.type > ref2->key_for_search.type)
20286d5f994SEdmund Nadolski 		return 1;
20386d5f994SEdmund Nadolski 	if (ref1->key_for_search.objectid < ref2->key_for_search.objectid)
20486d5f994SEdmund Nadolski 		return -1;
20586d5f994SEdmund Nadolski 	if (ref1->key_for_search.objectid > ref2->key_for_search.objectid)
20686d5f994SEdmund Nadolski 		return 1;
20786d5f994SEdmund Nadolski 	if (ref1->key_for_search.offset < ref2->key_for_search.offset)
20886d5f994SEdmund Nadolski 		return -1;
20986d5f994SEdmund Nadolski 	if (ref1->key_for_search.offset > ref2->key_for_search.offset)
21086d5f994SEdmund Nadolski 		return 1;
21186d5f994SEdmund Nadolski 	if (ref1->parent < ref2->parent)
21286d5f994SEdmund Nadolski 		return -1;
21386d5f994SEdmund Nadolski 	if (ref1->parent > ref2->parent)
21486d5f994SEdmund Nadolski 		return 1;
21586d5f994SEdmund Nadolski 
21686d5f994SEdmund Nadolski 	return 0;
21786d5f994SEdmund Nadolski }
21886d5f994SEdmund Nadolski 
2193ec4d323SEdmund Nadolski void update_share_count(struct share_check *sc, int oldcount, int newcount)
2203ec4d323SEdmund Nadolski {
2213ec4d323SEdmund Nadolski 	if ((!sc) || (oldcount == 0 && newcount < 1))
2223ec4d323SEdmund Nadolski 		return;
2233ec4d323SEdmund Nadolski 
2243ec4d323SEdmund Nadolski 	if (oldcount > 0 && newcount < 1)
2253ec4d323SEdmund Nadolski 		sc->share_count--;
2263ec4d323SEdmund Nadolski 	else if (oldcount < 1 && newcount > 0)
2273ec4d323SEdmund Nadolski 		sc->share_count++;
2283ec4d323SEdmund Nadolski }
2293ec4d323SEdmund Nadolski 
23086d5f994SEdmund Nadolski /*
23186d5f994SEdmund Nadolski  * Add @newref to the @root rbtree, merging identical refs.
23286d5f994SEdmund Nadolski  *
2333ec4d323SEdmund Nadolski  * Callers should assume that newref has been freed after calling.
23486d5f994SEdmund Nadolski  */
23500142756SJeff Mahoney static void prelim_ref_insert(const struct btrfs_fs_info *fs_info,
23600142756SJeff Mahoney 			      struct preftree *preftree,
2373ec4d323SEdmund Nadolski 			      struct prelim_ref *newref,
2383ec4d323SEdmund Nadolski 			      struct share_check *sc)
23986d5f994SEdmund Nadolski {
24086d5f994SEdmund Nadolski 	struct rb_root *root;
24186d5f994SEdmund Nadolski 	struct rb_node **p;
24286d5f994SEdmund Nadolski 	struct rb_node *parent = NULL;
24386d5f994SEdmund Nadolski 	struct prelim_ref *ref;
24486d5f994SEdmund Nadolski 	int result;
24586d5f994SEdmund Nadolski 
24686d5f994SEdmund Nadolski 	root = &preftree->root;
24786d5f994SEdmund Nadolski 	p = &root->rb_node;
24886d5f994SEdmund Nadolski 
24986d5f994SEdmund Nadolski 	while (*p) {
25086d5f994SEdmund Nadolski 		parent = *p;
25186d5f994SEdmund Nadolski 		ref = rb_entry(parent, struct prelim_ref, rbnode);
25286d5f994SEdmund Nadolski 		result = prelim_ref_compare(ref, newref);
25386d5f994SEdmund Nadolski 		if (result < 0) {
25486d5f994SEdmund Nadolski 			p = &(*p)->rb_left;
25586d5f994SEdmund Nadolski 		} else if (result > 0) {
25686d5f994SEdmund Nadolski 			p = &(*p)->rb_right;
25786d5f994SEdmund Nadolski 		} else {
25886d5f994SEdmund Nadolski 			/* Identical refs, merge them and free @newref */
25986d5f994SEdmund Nadolski 			struct extent_inode_elem *eie = ref->inode_list;
26086d5f994SEdmund Nadolski 
26186d5f994SEdmund Nadolski 			while (eie && eie->next)
26286d5f994SEdmund Nadolski 				eie = eie->next;
26386d5f994SEdmund Nadolski 
26486d5f994SEdmund Nadolski 			if (!eie)
26586d5f994SEdmund Nadolski 				ref->inode_list = newref->inode_list;
26686d5f994SEdmund Nadolski 			else
26786d5f994SEdmund Nadolski 				eie->next = newref->inode_list;
26800142756SJeff Mahoney 			trace_btrfs_prelim_ref_merge(fs_info, ref, newref,
26900142756SJeff Mahoney 						     preftree->count);
2703ec4d323SEdmund Nadolski 			/*
2713ec4d323SEdmund Nadolski 			 * A delayed ref can have newref->count < 0.
2723ec4d323SEdmund Nadolski 			 * The ref->count is updated to follow any
2733ec4d323SEdmund Nadolski 			 * BTRFS_[ADD|DROP]_DELAYED_REF actions.
2743ec4d323SEdmund Nadolski 			 */
2753ec4d323SEdmund Nadolski 			update_share_count(sc, ref->count,
2763ec4d323SEdmund Nadolski 					   ref->count + newref->count);
27786d5f994SEdmund Nadolski 			ref->count += newref->count;
27886d5f994SEdmund Nadolski 			free_pref(newref);
27986d5f994SEdmund Nadolski 			return;
28086d5f994SEdmund Nadolski 		}
28186d5f994SEdmund Nadolski 	}
28286d5f994SEdmund Nadolski 
2833ec4d323SEdmund Nadolski 	update_share_count(sc, 0, newref->count);
2846c336b21SJeff Mahoney 	preftree->count++;
28500142756SJeff Mahoney 	trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count);
28686d5f994SEdmund Nadolski 	rb_link_node(&newref->rbnode, parent, p);
28786d5f994SEdmund Nadolski 	rb_insert_color(&newref->rbnode, root);
28886d5f994SEdmund Nadolski }
28986d5f994SEdmund Nadolski 
29086d5f994SEdmund Nadolski /*
29186d5f994SEdmund Nadolski  * Release the entire tree.  We don't care about internal consistency so
29286d5f994SEdmund Nadolski  * just free everything and then reset the tree root.
29386d5f994SEdmund Nadolski  */
29486d5f994SEdmund Nadolski static void prelim_release(struct preftree *preftree)
29586d5f994SEdmund Nadolski {
29686d5f994SEdmund Nadolski 	struct prelim_ref *ref, *next_ref;
29786d5f994SEdmund Nadolski 
29886d5f994SEdmund Nadolski 	rbtree_postorder_for_each_entry_safe(ref, next_ref, &preftree->root,
29986d5f994SEdmund Nadolski 					     rbnode)
30086d5f994SEdmund Nadolski 		free_pref(ref);
30186d5f994SEdmund Nadolski 
30286d5f994SEdmund Nadolski 	preftree->root = RB_ROOT;
3036c336b21SJeff Mahoney 	preftree->count = 0;
30486d5f994SEdmund Nadolski }
30586d5f994SEdmund Nadolski 
306d5c88b73SJan Schmidt /*
307d5c88b73SJan Schmidt  * the rules for all callers of this function are:
308d5c88b73SJan Schmidt  * - obtaining the parent is the goal
309d5c88b73SJan Schmidt  * - if you add a key, you must know that it is a correct key
310d5c88b73SJan Schmidt  * - if you cannot add the parent or a correct key, then we will look into the
311d5c88b73SJan Schmidt  *   block later to set a correct key
312d5c88b73SJan Schmidt  *
313d5c88b73SJan Schmidt  * delayed refs
314d5c88b73SJan Schmidt  * ============
315d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
316d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
317d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
318d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    -   |     -
319d5c88b73SJan Schmidt  *      key to resolve |    -   |     y    |    y   |     y
320d5c88b73SJan Schmidt  *  tree block logical |    -   |     -    |    -   |     -
321d5c88b73SJan Schmidt  *  root for resolving |    y   |     y    |    y   |     y
322d5c88b73SJan Schmidt  *
323d5c88b73SJan Schmidt  * - column 1:       we've the parent -> done
324d5c88b73SJan Schmidt  * - column 2, 3, 4: we use the key to find the parent
325d5c88b73SJan Schmidt  *
326d5c88b73SJan Schmidt  * on disk refs (inline or keyed)
327d5c88b73SJan Schmidt  * ==============================
328d5c88b73SJan Schmidt  *        backref type | shared | indirect | shared | indirect
329d5c88b73SJan Schmidt  * information         |   tree |     tree |   data |     data
330d5c88b73SJan Schmidt  * --------------------+--------+----------+--------+----------
331d5c88b73SJan Schmidt  *      parent logical |    y   |     -    |    y   |     -
332d5c88b73SJan Schmidt  *      key to resolve |    -   |     -    |    -   |     y
333d5c88b73SJan Schmidt  *  tree block logical |    y   |     y    |    y   |     y
334d5c88b73SJan Schmidt  *  root for resolving |    -   |     y    |    y   |     y
335d5c88b73SJan Schmidt  *
336d5c88b73SJan Schmidt  * - column 1, 3: we've the parent -> done
337d5c88b73SJan Schmidt  * - column 2:    we take the first key from the block to find the parent
338e0c476b1SJeff Mahoney  *                (see add_missing_keys)
339d5c88b73SJan Schmidt  * - column 4:    we use the key to find the parent
340d5c88b73SJan Schmidt  *
341d5c88b73SJan Schmidt  * additional information that's available but not required to find the parent
342d5c88b73SJan Schmidt  * block might help in merging entries to gain some speed.
343d5c88b73SJan Schmidt  */
34400142756SJeff Mahoney static int add_prelim_ref(const struct btrfs_fs_info *fs_info,
34500142756SJeff Mahoney 			  struct preftree *preftree, u64 root_id,
346e0c476b1SJeff Mahoney 			  const struct btrfs_key *key, int level, u64 parent,
3473ec4d323SEdmund Nadolski 			  u64 wanted_disk_byte, int count,
3483ec4d323SEdmund Nadolski 			  struct share_check *sc, gfp_t gfp_mask)
3498da6d581SJan Schmidt {
350e0c476b1SJeff Mahoney 	struct prelim_ref *ref;
3518da6d581SJan Schmidt 
35248ec4736SLiu Bo 	if (root_id == BTRFS_DATA_RELOC_TREE_OBJECTID)
35348ec4736SLiu Bo 		return 0;
35448ec4736SLiu Bo 
355b9e9a6cbSWang Shilong 	ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
3568da6d581SJan Schmidt 	if (!ref)
3578da6d581SJan Schmidt 		return -ENOMEM;
3588da6d581SJan Schmidt 
3598da6d581SJan Schmidt 	ref->root_id = root_id;
360d6589101SFilipe Manana 	if (key) {
361d5c88b73SJan Schmidt 		ref->key_for_search = *key;
362d6589101SFilipe Manana 		/*
363d6589101SFilipe Manana 		 * We can often find data backrefs with an offset that is too
364d6589101SFilipe Manana 		 * large (>= LLONG_MAX, maximum allowed file offset) due to
365d6589101SFilipe Manana 		 * underflows when subtracting a file's offset with the data
366d6589101SFilipe Manana 		 * offset of its corresponding extent data item. This can
367d6589101SFilipe Manana 		 * happen for example in the clone ioctl.
368d6589101SFilipe Manana 		 * So if we detect such case we set the search key's offset to
369d6589101SFilipe Manana 		 * zero to make sure we will find the matching file extent item
370d6589101SFilipe Manana 		 * at add_all_parents(), otherwise we will miss it because the
371d6589101SFilipe Manana 		 * offset taken form the backref is much larger then the offset
372d6589101SFilipe Manana 		 * of the file extent item. This can make us scan a very large
373d6589101SFilipe Manana 		 * number of file extent items, but at least it will not make
374d6589101SFilipe Manana 		 * us miss any.
375d6589101SFilipe Manana 		 * This is an ugly workaround for a behaviour that should have
376d6589101SFilipe Manana 		 * never existed, but it does and a fix for the clone ioctl
377d6589101SFilipe Manana 		 * would touch a lot of places, cause backwards incompatibility
378d6589101SFilipe Manana 		 * and would not fix the problem for extents cloned with older
379d6589101SFilipe Manana 		 * kernels.
380d6589101SFilipe Manana 		 */
381d6589101SFilipe Manana 		if (ref->key_for_search.type == BTRFS_EXTENT_DATA_KEY &&
382d6589101SFilipe Manana 		    ref->key_for_search.offset >= LLONG_MAX)
383d6589101SFilipe Manana 			ref->key_for_search.offset = 0;
384d6589101SFilipe Manana 	} else {
385d5c88b73SJan Schmidt 		memset(&ref->key_for_search, 0, sizeof(ref->key_for_search));
386d6589101SFilipe Manana 	}
3878da6d581SJan Schmidt 
3883301958bSJan Schmidt 	ref->inode_list = NULL;
3898da6d581SJan Schmidt 	ref->level = level;
3908da6d581SJan Schmidt 	ref->count = count;
3918da6d581SJan Schmidt 	ref->parent = parent;
3928da6d581SJan Schmidt 	ref->wanted_disk_byte = wanted_disk_byte;
3933ec4d323SEdmund Nadolski 	prelim_ref_insert(fs_info, preftree, ref, sc);
3943ec4d323SEdmund Nadolski 	return extent_is_shared(sc);
3958da6d581SJan Schmidt }
3968da6d581SJan Schmidt 
39786d5f994SEdmund Nadolski /* direct refs use root == 0, key == NULL */
39800142756SJeff Mahoney static int add_direct_ref(const struct btrfs_fs_info *fs_info,
39900142756SJeff Mahoney 			  struct preftrees *preftrees, int level, u64 parent,
4003ec4d323SEdmund Nadolski 			  u64 wanted_disk_byte, int count,
4013ec4d323SEdmund Nadolski 			  struct share_check *sc, gfp_t gfp_mask)
40286d5f994SEdmund Nadolski {
40300142756SJeff Mahoney 	return add_prelim_ref(fs_info, &preftrees->direct, 0, NULL, level,
4043ec4d323SEdmund Nadolski 			      parent, wanted_disk_byte, count, sc, gfp_mask);
40586d5f994SEdmund Nadolski }
40686d5f994SEdmund Nadolski 
40786d5f994SEdmund Nadolski /* indirect refs use parent == 0 */
40800142756SJeff Mahoney static int add_indirect_ref(const struct btrfs_fs_info *fs_info,
40900142756SJeff Mahoney 			    struct preftrees *preftrees, u64 root_id,
41086d5f994SEdmund Nadolski 			    const struct btrfs_key *key, int level,
4113ec4d323SEdmund Nadolski 			    u64 wanted_disk_byte, int count,
4123ec4d323SEdmund Nadolski 			    struct share_check *sc, gfp_t gfp_mask)
41386d5f994SEdmund Nadolski {
41486d5f994SEdmund Nadolski 	struct preftree *tree = &preftrees->indirect;
41586d5f994SEdmund Nadolski 
41686d5f994SEdmund Nadolski 	if (!key)
41786d5f994SEdmund Nadolski 		tree = &preftrees->indirect_missing_keys;
41800142756SJeff Mahoney 	return add_prelim_ref(fs_info, tree, root_id, key, level, 0,
4193ec4d323SEdmund Nadolski 			      wanted_disk_byte, count, sc, gfp_mask);
42086d5f994SEdmund Nadolski }
42186d5f994SEdmund Nadolski 
4228da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
423e0c476b1SJeff Mahoney 			   struct ulist *parents, struct prelim_ref *ref,
42444853868SJosef Bacik 			   int level, u64 time_seq, const u64 *extent_item_pos,
425*c995ab3cSZygo Blaxell 			   u64 total_refs, bool ignore_offset)
4268da6d581SJan Schmidt {
42769bca40dSAlexander Block 	int ret = 0;
42869bca40dSAlexander Block 	int slot;
42969bca40dSAlexander Block 	struct extent_buffer *eb;
43069bca40dSAlexander Block 	struct btrfs_key key;
4317ef81ac8SJosef Bacik 	struct btrfs_key *key_for_search = &ref->key_for_search;
4328da6d581SJan Schmidt 	struct btrfs_file_extent_item *fi;
433ed8c4913SJosef Bacik 	struct extent_inode_elem *eie = NULL, *old = NULL;
4348da6d581SJan Schmidt 	u64 disk_byte;
4357ef81ac8SJosef Bacik 	u64 wanted_disk_byte = ref->wanted_disk_byte;
4367ef81ac8SJosef Bacik 	u64 count = 0;
4378da6d581SJan Schmidt 
43869bca40dSAlexander Block 	if (level != 0) {
43969bca40dSAlexander Block 		eb = path->nodes[level];
44069bca40dSAlexander Block 		ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
4413301958bSJan Schmidt 		if (ret < 0)
4423301958bSJan Schmidt 			return ret;
4438da6d581SJan Schmidt 		return 0;
44469bca40dSAlexander Block 	}
4458da6d581SJan Schmidt 
4468da6d581SJan Schmidt 	/*
44769bca40dSAlexander Block 	 * We normally enter this function with the path already pointing to
44869bca40dSAlexander Block 	 * the first item to check. But sometimes, we may enter it with
44969bca40dSAlexander Block 	 * slot==nritems. In that case, go to the next leaf before we continue.
4508da6d581SJan Schmidt 	 */
45121633fc6SQu Wenruo 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
452de47c9d3SEdmund Nadolski 		if (time_seq == SEQ_LAST)
45321633fc6SQu Wenruo 			ret = btrfs_next_leaf(root, path);
45421633fc6SQu Wenruo 		else
4553d7806ecSJan Schmidt 			ret = btrfs_next_old_leaf(root, path, time_seq);
45621633fc6SQu Wenruo 	}
4578da6d581SJan Schmidt 
45844853868SJosef Bacik 	while (!ret && count < total_refs) {
4598da6d581SJan Schmidt 		eb = path->nodes[0];
46069bca40dSAlexander Block 		slot = path->slots[0];
46169bca40dSAlexander Block 
46269bca40dSAlexander Block 		btrfs_item_key_to_cpu(eb, &key, slot);
46369bca40dSAlexander Block 
46469bca40dSAlexander Block 		if (key.objectid != key_for_search->objectid ||
46569bca40dSAlexander Block 		    key.type != BTRFS_EXTENT_DATA_KEY)
46669bca40dSAlexander Block 			break;
46769bca40dSAlexander Block 
46869bca40dSAlexander Block 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4698da6d581SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
47069bca40dSAlexander Block 
47169bca40dSAlexander Block 		if (disk_byte == wanted_disk_byte) {
47269bca40dSAlexander Block 			eie = NULL;
473ed8c4913SJosef Bacik 			old = NULL;
4747ef81ac8SJosef Bacik 			count++;
47569bca40dSAlexander Block 			if (extent_item_pos) {
47669bca40dSAlexander Block 				ret = check_extent_in_eb(&key, eb, fi,
47769bca40dSAlexander Block 						*extent_item_pos,
478*c995ab3cSZygo Blaxell 						&eie, ignore_offset);
47969bca40dSAlexander Block 				if (ret < 0)
48069bca40dSAlexander Block 					break;
4818da6d581SJan Schmidt 			}
482ed8c4913SJosef Bacik 			if (ret > 0)
483ed8c4913SJosef Bacik 				goto next;
4844eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(parents, eb->start,
4854eb1f66dSTakashi Iwai 						  eie, (void **)&old, GFP_NOFS);
48669bca40dSAlexander Block 			if (ret < 0)
48769bca40dSAlexander Block 				break;
488ed8c4913SJosef Bacik 			if (!ret && extent_item_pos) {
489ed8c4913SJosef Bacik 				while (old->next)
490ed8c4913SJosef Bacik 					old = old->next;
491ed8c4913SJosef Bacik 				old->next = eie;
49269bca40dSAlexander Block 			}
493f05c4746SWang Shilong 			eie = NULL;
49469bca40dSAlexander Block 		}
495ed8c4913SJosef Bacik next:
496de47c9d3SEdmund Nadolski 		if (time_seq == SEQ_LAST)
49721633fc6SQu Wenruo 			ret = btrfs_next_item(root, path);
49821633fc6SQu Wenruo 		else
49969bca40dSAlexander Block 			ret = btrfs_next_old_item(root, path, time_seq);
5008da6d581SJan Schmidt 	}
5018da6d581SJan Schmidt 
50269bca40dSAlexander Block 	if (ret > 0)
50369bca40dSAlexander Block 		ret = 0;
504f05c4746SWang Shilong 	else if (ret < 0)
505f05c4746SWang Shilong 		free_inode_elem_list(eie);
50669bca40dSAlexander Block 	return ret;
5078da6d581SJan Schmidt }
5088da6d581SJan Schmidt 
5098da6d581SJan Schmidt /*
5108da6d581SJan Schmidt  * resolve an indirect backref in the form (root_id, key, level)
5118da6d581SJan Schmidt  * to a logical address
5128da6d581SJan Schmidt  */
513e0c476b1SJeff Mahoney static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
514da61d31aSJosef Bacik 				struct btrfs_path *path, u64 time_seq,
515e0c476b1SJeff Mahoney 				struct prelim_ref *ref, struct ulist *parents,
516*c995ab3cSZygo Blaxell 				const u64 *extent_item_pos, u64 total_refs,
517*c995ab3cSZygo Blaxell 				bool ignore_offset)
5188da6d581SJan Schmidt {
5198da6d581SJan Schmidt 	struct btrfs_root *root;
5208da6d581SJan Schmidt 	struct btrfs_key root_key;
5218da6d581SJan Schmidt 	struct extent_buffer *eb;
5228da6d581SJan Schmidt 	int ret = 0;
5238da6d581SJan Schmidt 	int root_level;
5248da6d581SJan Schmidt 	int level = ref->level;
525538f72cdSWang Shilong 	int index;
5268da6d581SJan Schmidt 
5278da6d581SJan Schmidt 	root_key.objectid = ref->root_id;
5288da6d581SJan Schmidt 	root_key.type = BTRFS_ROOT_ITEM_KEY;
5298da6d581SJan Schmidt 	root_key.offset = (u64)-1;
530538f72cdSWang Shilong 
531538f72cdSWang Shilong 	index = srcu_read_lock(&fs_info->subvol_srcu);
532538f72cdSWang Shilong 
5332d9e9776SJosef Bacik 	root = btrfs_get_fs_root(fs_info, &root_key, false);
5348da6d581SJan Schmidt 	if (IS_ERR(root)) {
535538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
5368da6d581SJan Schmidt 		ret = PTR_ERR(root);
5378da6d581SJan Schmidt 		goto out;
5388da6d581SJan Schmidt 	}
5398da6d581SJan Schmidt 
540f5ee5c9aSJeff Mahoney 	if (btrfs_is_testing(fs_info)) {
541d9ee522bSJosef Bacik 		srcu_read_unlock(&fs_info->subvol_srcu, index);
542d9ee522bSJosef Bacik 		ret = -ENOENT;
543d9ee522bSJosef Bacik 		goto out;
544d9ee522bSJosef Bacik 	}
545d9ee522bSJosef Bacik 
5469e351cc8SJosef Bacik 	if (path->search_commit_root)
5479e351cc8SJosef Bacik 		root_level = btrfs_header_level(root->commit_root);
548de47c9d3SEdmund Nadolski 	else if (time_seq == SEQ_LAST)
54921633fc6SQu Wenruo 		root_level = btrfs_header_level(root->node);
5509e351cc8SJosef Bacik 	else
5515b6602e7SJan Schmidt 		root_level = btrfs_old_root_level(root, time_seq);
5528da6d581SJan Schmidt 
553538f72cdSWang Shilong 	if (root_level + 1 == level) {
554538f72cdSWang Shilong 		srcu_read_unlock(&fs_info->subvol_srcu, index);
5558da6d581SJan Schmidt 		goto out;
556538f72cdSWang Shilong 	}
5578da6d581SJan Schmidt 
5588da6d581SJan Schmidt 	path->lowest_level = level;
559de47c9d3SEdmund Nadolski 	if (time_seq == SEQ_LAST)
56021633fc6SQu Wenruo 		ret = btrfs_search_slot(NULL, root, &ref->key_for_search, path,
56121633fc6SQu Wenruo 					0, 0);
56221633fc6SQu Wenruo 	else
56321633fc6SQu Wenruo 		ret = btrfs_search_old_slot(root, &ref->key_for_search, path,
56421633fc6SQu Wenruo 					    time_seq);
565538f72cdSWang Shilong 
566538f72cdSWang Shilong 	/* root node has been locked, we can release @subvol_srcu safely here */
567538f72cdSWang Shilong 	srcu_read_unlock(&fs_info->subvol_srcu, index);
568538f72cdSWang Shilong 
569ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info,
570ab8d0fc4SJeff Mahoney 		"search slot in root %llu (level %d, ref count %d) returned %d for key (%llu %u %llu)",
571c1c9ff7cSGeert Uytterhoeven 		 ref->root_id, level, ref->count, ret,
572c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.objectid, ref->key_for_search.type,
573c1c9ff7cSGeert Uytterhoeven 		 ref->key_for_search.offset);
5748da6d581SJan Schmidt 	if (ret < 0)
5758da6d581SJan Schmidt 		goto out;
5768da6d581SJan Schmidt 
5778da6d581SJan Schmidt 	eb = path->nodes[level];
5789345457fSJan Schmidt 	while (!eb) {
579fae7f21cSDulshani Gunawardhana 		if (WARN_ON(!level)) {
5808da6d581SJan Schmidt 			ret = 1;
5818da6d581SJan Schmidt 			goto out;
5828da6d581SJan Schmidt 		}
5839345457fSJan Schmidt 		level--;
5849345457fSJan Schmidt 		eb = path->nodes[level];
5859345457fSJan Schmidt 	}
5868da6d581SJan Schmidt 
5877ef81ac8SJosef Bacik 	ret = add_all_parents(root, path, parents, ref, level, time_seq,
588*c995ab3cSZygo Blaxell 			      extent_item_pos, total_refs, ignore_offset);
5898da6d581SJan Schmidt out:
590da61d31aSJosef Bacik 	path->lowest_level = 0;
591da61d31aSJosef Bacik 	btrfs_release_path(path);
5928da6d581SJan Schmidt 	return ret;
5938da6d581SJan Schmidt }
5948da6d581SJan Schmidt 
5954dae077aSJeff Mahoney static struct extent_inode_elem *
5964dae077aSJeff Mahoney unode_aux_to_inode_list(struct ulist_node *node)
5974dae077aSJeff Mahoney {
5984dae077aSJeff Mahoney 	if (!node)
5994dae077aSJeff Mahoney 		return NULL;
6004dae077aSJeff Mahoney 	return (struct extent_inode_elem *)(uintptr_t)node->aux;
6014dae077aSJeff Mahoney }
6024dae077aSJeff Mahoney 
6038da6d581SJan Schmidt /*
60486d5f994SEdmund Nadolski  * We maintain three seperate rbtrees: one for direct refs, one for
60586d5f994SEdmund Nadolski  * indirect refs which have a key, and one for indirect refs which do not
60686d5f994SEdmund Nadolski  * have a key. Each tree does merge on insertion.
60786d5f994SEdmund Nadolski  *
60886d5f994SEdmund Nadolski  * Once all of the references are located, we iterate over the tree of
60986d5f994SEdmund Nadolski  * indirect refs with missing keys. An appropriate key is located and
61086d5f994SEdmund Nadolski  * the ref is moved onto the tree for indirect refs. After all missing
61186d5f994SEdmund Nadolski  * keys are thus located, we iterate over the indirect ref tree, resolve
61286d5f994SEdmund Nadolski  * each reference, and then insert the resolved reference onto the
61386d5f994SEdmund Nadolski  * direct tree (merging there too).
61486d5f994SEdmund Nadolski  *
61586d5f994SEdmund Nadolski  * New backrefs (i.e., for parent nodes) are added to the appropriate
61686d5f994SEdmund Nadolski  * rbtree as they are encountered. The new backrefs are subsequently
61786d5f994SEdmund Nadolski  * resolved as above.
6188da6d581SJan Schmidt  */
619e0c476b1SJeff Mahoney static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
620da61d31aSJosef Bacik 				 struct btrfs_path *path, u64 time_seq,
62186d5f994SEdmund Nadolski 				 struct preftrees *preftrees,
622dc046b10SJosef Bacik 				 const u64 *extent_item_pos, u64 total_refs,
623*c995ab3cSZygo Blaxell 				 struct share_check *sc, bool ignore_offset)
6248da6d581SJan Schmidt {
6258da6d581SJan Schmidt 	int err;
6268da6d581SJan Schmidt 	int ret = 0;
6278da6d581SJan Schmidt 	struct ulist *parents;
6288da6d581SJan Schmidt 	struct ulist_node *node;
629cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
63086d5f994SEdmund Nadolski 	struct rb_node *rnode;
6318da6d581SJan Schmidt 
6328da6d581SJan Schmidt 	parents = ulist_alloc(GFP_NOFS);
6338da6d581SJan Schmidt 	if (!parents)
6348da6d581SJan Schmidt 		return -ENOMEM;
6358da6d581SJan Schmidt 
6368da6d581SJan Schmidt 	/*
63786d5f994SEdmund Nadolski 	 * We could trade memory usage for performance here by iterating
63886d5f994SEdmund Nadolski 	 * the tree, allocating new refs for each insertion, and then
63986d5f994SEdmund Nadolski 	 * freeing the entire indirect tree when we're done.  In some test
64086d5f994SEdmund Nadolski 	 * cases, the tree can grow quite large (~200k objects).
6418da6d581SJan Schmidt 	 */
64286d5f994SEdmund Nadolski 	while ((rnode = rb_first(&preftrees->indirect.root))) {
64386d5f994SEdmund Nadolski 		struct prelim_ref *ref;
64486d5f994SEdmund Nadolski 
64586d5f994SEdmund Nadolski 		ref = rb_entry(rnode, struct prelim_ref, rbnode);
64686d5f994SEdmund Nadolski 		if (WARN(ref->parent,
64786d5f994SEdmund Nadolski 			 "BUG: direct ref found in indirect tree")) {
64886d5f994SEdmund Nadolski 			ret = -EINVAL;
64986d5f994SEdmund Nadolski 			goto out;
65086d5f994SEdmund Nadolski 		}
65186d5f994SEdmund Nadolski 
65286d5f994SEdmund Nadolski 		rb_erase(&ref->rbnode, &preftrees->indirect.root);
6536c336b21SJeff Mahoney 		preftrees->indirect.count--;
65486d5f994SEdmund Nadolski 
65586d5f994SEdmund Nadolski 		if (ref->count == 0) {
65686d5f994SEdmund Nadolski 			free_pref(ref);
6578da6d581SJan Schmidt 			continue;
65886d5f994SEdmund Nadolski 		}
65986d5f994SEdmund Nadolski 
6603ec4d323SEdmund Nadolski 		if (sc && sc->root_objectid &&
6613ec4d323SEdmund Nadolski 		    ref->root_id != sc->root_objectid) {
66286d5f994SEdmund Nadolski 			free_pref(ref);
663dc046b10SJosef Bacik 			ret = BACKREF_FOUND_SHARED;
664dc046b10SJosef Bacik 			goto out;
665dc046b10SJosef Bacik 		}
666e0c476b1SJeff Mahoney 		err = resolve_indirect_ref(fs_info, path, time_seq, ref,
66744853868SJosef Bacik 					   parents, extent_item_pos,
668*c995ab3cSZygo Blaxell 					   total_refs, ignore_offset);
66995def2edSWang Shilong 		/*
67095def2edSWang Shilong 		 * we can only tolerate ENOENT,otherwise,we should catch error
67195def2edSWang Shilong 		 * and return directly.
67295def2edSWang Shilong 		 */
67395def2edSWang Shilong 		if (err == -ENOENT) {
6743ec4d323SEdmund Nadolski 			prelim_ref_insert(fs_info, &preftrees->direct, ref,
6753ec4d323SEdmund Nadolski 					  NULL);
6768da6d581SJan Schmidt 			continue;
67795def2edSWang Shilong 		} else if (err) {
67886d5f994SEdmund Nadolski 			free_pref(ref);
67995def2edSWang Shilong 			ret = err;
68095def2edSWang Shilong 			goto out;
68195def2edSWang Shilong 		}
6828da6d581SJan Schmidt 
6838da6d581SJan Schmidt 		/* we put the first parent into the ref at hand */
684cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&uiter);
685cd1b413cSJan Schmidt 		node = ulist_next(parents, &uiter);
6868da6d581SJan Schmidt 		ref->parent = node ? node->val : 0;
6874dae077aSJeff Mahoney 		ref->inode_list = unode_aux_to_inode_list(node);
6888da6d581SJan Schmidt 
68986d5f994SEdmund Nadolski 		/* Add a prelim_ref(s) for any other parent(s). */
690cd1b413cSJan Schmidt 		while ((node = ulist_next(parents, &uiter))) {
69186d5f994SEdmund Nadolski 			struct prelim_ref *new_ref;
69286d5f994SEdmund Nadolski 
693b9e9a6cbSWang Shilong 			new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
694b9e9a6cbSWang Shilong 						   GFP_NOFS);
6958da6d581SJan Schmidt 			if (!new_ref) {
69686d5f994SEdmund Nadolski 				free_pref(ref);
6978da6d581SJan Schmidt 				ret = -ENOMEM;
698e36902d4SWang Shilong 				goto out;
6998da6d581SJan Schmidt 			}
7008da6d581SJan Schmidt 			memcpy(new_ref, ref, sizeof(*ref));
7018da6d581SJan Schmidt 			new_ref->parent = node->val;
7024dae077aSJeff Mahoney 			new_ref->inode_list = unode_aux_to_inode_list(node);
7033ec4d323SEdmund Nadolski 			prelim_ref_insert(fs_info, &preftrees->direct,
7043ec4d323SEdmund Nadolski 					  new_ref, NULL);
7058da6d581SJan Schmidt 		}
70686d5f994SEdmund Nadolski 
7073ec4d323SEdmund Nadolski 		/*
7083ec4d323SEdmund Nadolski 		 * Now it's a direct ref, put it in the the direct tree. We must
7093ec4d323SEdmund Nadolski 		 * do this last because the ref could be merged/freed here.
7103ec4d323SEdmund Nadolski 		 */
7113ec4d323SEdmund Nadolski 		prelim_ref_insert(fs_info, &preftrees->direct, ref, NULL);
71286d5f994SEdmund Nadolski 
7138da6d581SJan Schmidt 		ulist_reinit(parents);
7149dd14fd6SEdmund Nadolski 		cond_resched();
7158da6d581SJan Schmidt 	}
716e36902d4SWang Shilong out:
7178da6d581SJan Schmidt 	ulist_free(parents);
7188da6d581SJan Schmidt 	return ret;
7198da6d581SJan Schmidt }
7208da6d581SJan Schmidt 
721d5c88b73SJan Schmidt /*
722d5c88b73SJan Schmidt  * read tree blocks and add keys where required.
723d5c88b73SJan Schmidt  */
724e0c476b1SJeff Mahoney static int add_missing_keys(struct btrfs_fs_info *fs_info,
72586d5f994SEdmund Nadolski 			    struct preftrees *preftrees)
726d5c88b73SJan Schmidt {
727e0c476b1SJeff Mahoney 	struct prelim_ref *ref;
728d5c88b73SJan Schmidt 	struct extent_buffer *eb;
72986d5f994SEdmund Nadolski 	struct preftree *tree = &preftrees->indirect_missing_keys;
73086d5f994SEdmund Nadolski 	struct rb_node *node;
731d5c88b73SJan Schmidt 
73286d5f994SEdmund Nadolski 	while ((node = rb_first(&tree->root))) {
73386d5f994SEdmund Nadolski 		ref = rb_entry(node, struct prelim_ref, rbnode);
73486d5f994SEdmund Nadolski 		rb_erase(node, &tree->root);
73586d5f994SEdmund Nadolski 
73686d5f994SEdmund Nadolski 		BUG_ON(ref->parent);	/* should not be a direct ref */
73786d5f994SEdmund Nadolski 		BUG_ON(ref->key_for_search.type);
738d5c88b73SJan Schmidt 		BUG_ON(!ref->wanted_disk_byte);
73986d5f994SEdmund Nadolski 
7402ff7e61eSJeff Mahoney 		eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
74164c043deSLiu Bo 		if (IS_ERR(eb)) {
74286d5f994SEdmund Nadolski 			free_pref(ref);
74364c043deSLiu Bo 			return PTR_ERR(eb);
74464c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
74586d5f994SEdmund Nadolski 			free_pref(ref);
746416bc658SJosef Bacik 			free_extent_buffer(eb);
747416bc658SJosef Bacik 			return -EIO;
748416bc658SJosef Bacik 		}
749d5c88b73SJan Schmidt 		btrfs_tree_read_lock(eb);
750d5c88b73SJan Schmidt 		if (btrfs_header_level(eb) == 0)
751d5c88b73SJan Schmidt 			btrfs_item_key_to_cpu(eb, &ref->key_for_search, 0);
752d5c88b73SJan Schmidt 		else
753d5c88b73SJan Schmidt 			btrfs_node_key_to_cpu(eb, &ref->key_for_search, 0);
754d5c88b73SJan Schmidt 		btrfs_tree_read_unlock(eb);
755d5c88b73SJan Schmidt 		free_extent_buffer(eb);
7563ec4d323SEdmund Nadolski 		prelim_ref_insert(fs_info, &preftrees->indirect, ref, NULL);
7579dd14fd6SEdmund Nadolski 		cond_resched();
758d5c88b73SJan Schmidt 	}
759d5c88b73SJan Schmidt 	return 0;
760d5c88b73SJan Schmidt }
761d5c88b73SJan Schmidt 
7628da6d581SJan Schmidt /*
7638da6d581SJan Schmidt  * add all currently queued delayed refs from this head whose seq nr is
7648da6d581SJan Schmidt  * smaller or equal that seq to the list
7658da6d581SJan Schmidt  */
76600142756SJeff Mahoney static int add_delayed_refs(const struct btrfs_fs_info *fs_info,
76700142756SJeff Mahoney 			    struct btrfs_delayed_ref_head *head, u64 seq,
76886d5f994SEdmund Nadolski 			    struct preftrees *preftrees, u64 *total_refs,
7693ec4d323SEdmund Nadolski 			    struct share_check *sc)
7708da6d581SJan Schmidt {
771c6fc2454SQu Wenruo 	struct btrfs_delayed_ref_node *node;
7728da6d581SJan Schmidt 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
773d5c88b73SJan Schmidt 	struct btrfs_key key;
77486d5f994SEdmund Nadolski 	struct btrfs_key tmp_op_key;
77586d5f994SEdmund Nadolski 	struct btrfs_key *op_key = NULL;
77601747e92SEdmund Nadolski 	int count;
777b1375d64SJan Schmidt 	int ret = 0;
7788da6d581SJan Schmidt 
77986d5f994SEdmund Nadolski 	if (extent_op && extent_op->update_key) {
78086d5f994SEdmund Nadolski 		btrfs_disk_key_to_cpu(&tmp_op_key, &extent_op->key);
78186d5f994SEdmund Nadolski 		op_key = &tmp_op_key;
78286d5f994SEdmund Nadolski 	}
7838da6d581SJan Schmidt 
784d7df2c79SJosef Bacik 	spin_lock(&head->lock);
785c6fc2454SQu Wenruo 	list_for_each_entry(node, &head->ref_list, list) {
7868da6d581SJan Schmidt 		if (node->seq > seq)
7878da6d581SJan Schmidt 			continue;
7888da6d581SJan Schmidt 
7898da6d581SJan Schmidt 		switch (node->action) {
7908da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_EXTENT:
7918da6d581SJan Schmidt 		case BTRFS_UPDATE_DELAYED_HEAD:
7928da6d581SJan Schmidt 			WARN_ON(1);
7938da6d581SJan Schmidt 			continue;
7948da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_REF:
79501747e92SEdmund Nadolski 			count = node->ref_mod;
7968da6d581SJan Schmidt 			break;
7978da6d581SJan Schmidt 		case BTRFS_DROP_DELAYED_REF:
79801747e92SEdmund Nadolski 			count = node->ref_mod * -1;
7998da6d581SJan Schmidt 			break;
8008da6d581SJan Schmidt 		default:
8018da6d581SJan Schmidt 			BUG_ON(1);
8028da6d581SJan Schmidt 		}
80301747e92SEdmund Nadolski 		*total_refs += count;
8048da6d581SJan Schmidt 		switch (node->type) {
8058da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY: {
80686d5f994SEdmund Nadolski 			/* NORMAL INDIRECT METADATA backref */
8078da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
8088da6d581SJan Schmidt 
8098da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
81000142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, ref->root,
81100142756SJeff Mahoney 					       &tmp_op_key, ref->level + 1,
81201747e92SEdmund Nadolski 					       node->bytenr, count, sc,
81301747e92SEdmund Nadolski 					       GFP_ATOMIC);
8148da6d581SJan Schmidt 			break;
8158da6d581SJan Schmidt 		}
8168da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY: {
81786d5f994SEdmund Nadolski 			/* SHARED DIRECT METADATA backref */
8188da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
8198da6d581SJan Schmidt 
8208da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
82186d5f994SEdmund Nadolski 
82201747e92SEdmund Nadolski 			ret = add_direct_ref(fs_info, preftrees, ref->level + 1,
82301747e92SEdmund Nadolski 					     ref->parent, node->bytenr, count,
8243ec4d323SEdmund Nadolski 					     sc, GFP_ATOMIC);
8258da6d581SJan Schmidt 			break;
8268da6d581SJan Schmidt 		}
8278da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
82886d5f994SEdmund Nadolski 			/* NORMAL INDIRECT DATA backref */
8298da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
8308da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
8318da6d581SJan Schmidt 
8328da6d581SJan Schmidt 			key.objectid = ref->objectid;
8338da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
8348da6d581SJan Schmidt 			key.offset = ref->offset;
835dc046b10SJosef Bacik 
836dc046b10SJosef Bacik 			/*
837dc046b10SJosef Bacik 			 * Found a inum that doesn't match our known inum, we
838dc046b10SJosef Bacik 			 * know it's shared.
839dc046b10SJosef Bacik 			 */
8403ec4d323SEdmund Nadolski 			if (sc && sc->inum && ref->objectid != sc->inum) {
841dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
8423ec4d323SEdmund Nadolski 				goto out;
843dc046b10SJosef Bacik 			}
844dc046b10SJosef Bacik 
84500142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, ref->root,
84601747e92SEdmund Nadolski 					       &key, 0, node->bytenr, count, sc,
84701747e92SEdmund Nadolski 					       GFP_ATOMIC);
8488da6d581SJan Schmidt 			break;
8498da6d581SJan Schmidt 		}
8508da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
85186d5f994SEdmund Nadolski 			/* SHARED DIRECT FULL backref */
8528da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
8538da6d581SJan Schmidt 
8548da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
85586d5f994SEdmund Nadolski 
85601747e92SEdmund Nadolski 			ret = add_direct_ref(fs_info, preftrees, 0, ref->parent,
85701747e92SEdmund Nadolski 					     node->bytenr, count, sc,
85801747e92SEdmund Nadolski 					     GFP_ATOMIC);
8598da6d581SJan Schmidt 			break;
8608da6d581SJan Schmidt 		}
8618da6d581SJan Schmidt 		default:
8628da6d581SJan Schmidt 			WARN_ON(1);
8638da6d581SJan Schmidt 		}
8643ec4d323SEdmund Nadolski 		/*
8653ec4d323SEdmund Nadolski 		 * We must ignore BACKREF_FOUND_SHARED until all delayed
8663ec4d323SEdmund Nadolski 		 * refs have been checked.
8673ec4d323SEdmund Nadolski 		 */
8683ec4d323SEdmund Nadolski 		if (ret && (ret != BACKREF_FOUND_SHARED))
869d7df2c79SJosef Bacik 			break;
8708da6d581SJan Schmidt 	}
8713ec4d323SEdmund Nadolski 	if (!ret)
8723ec4d323SEdmund Nadolski 		ret = extent_is_shared(sc);
8733ec4d323SEdmund Nadolski out:
874d7df2c79SJosef Bacik 	spin_unlock(&head->lock);
875d7df2c79SJosef Bacik 	return ret;
8768da6d581SJan Schmidt }
8778da6d581SJan Schmidt 
8788da6d581SJan Schmidt /*
8798da6d581SJan Schmidt  * add all inline backrefs for bytenr to the list
8803ec4d323SEdmund Nadolski  *
8813ec4d323SEdmund Nadolski  * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
8828da6d581SJan Schmidt  */
88300142756SJeff Mahoney static int add_inline_refs(const struct btrfs_fs_info *fs_info,
88400142756SJeff Mahoney 			   struct btrfs_path *path, u64 bytenr,
88586d5f994SEdmund Nadolski 			   int *info_level, struct preftrees *preftrees,
8863ec4d323SEdmund Nadolski 			   u64 *total_refs, struct share_check *sc)
8878da6d581SJan Schmidt {
888b1375d64SJan Schmidt 	int ret = 0;
8898da6d581SJan Schmidt 	int slot;
8908da6d581SJan Schmidt 	struct extent_buffer *leaf;
8918da6d581SJan Schmidt 	struct btrfs_key key;
892261c84b6SJosef Bacik 	struct btrfs_key found_key;
8938da6d581SJan Schmidt 	unsigned long ptr;
8948da6d581SJan Schmidt 	unsigned long end;
8958da6d581SJan Schmidt 	struct btrfs_extent_item *ei;
8968da6d581SJan Schmidt 	u64 flags;
8978da6d581SJan Schmidt 	u64 item_size;
8988da6d581SJan Schmidt 
8998da6d581SJan Schmidt 	/*
9008da6d581SJan Schmidt 	 * enumerate all inline refs
9018da6d581SJan Schmidt 	 */
9028da6d581SJan Schmidt 	leaf = path->nodes[0];
903dadcaf78SJan Schmidt 	slot = path->slots[0];
9048da6d581SJan Schmidt 
9058da6d581SJan Schmidt 	item_size = btrfs_item_size_nr(leaf, slot);
9068da6d581SJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
9078da6d581SJan Schmidt 
9088da6d581SJan Schmidt 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
9098da6d581SJan Schmidt 	flags = btrfs_extent_flags(leaf, ei);
91044853868SJosef Bacik 	*total_refs += btrfs_extent_refs(leaf, ei);
911261c84b6SJosef Bacik 	btrfs_item_key_to_cpu(leaf, &found_key, slot);
9128da6d581SJan Schmidt 
9138da6d581SJan Schmidt 	ptr = (unsigned long)(ei + 1);
9148da6d581SJan Schmidt 	end = (unsigned long)ei + item_size;
9158da6d581SJan Schmidt 
916261c84b6SJosef Bacik 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
917261c84b6SJosef Bacik 	    flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
9188da6d581SJan Schmidt 		struct btrfs_tree_block_info *info;
9198da6d581SJan Schmidt 
9208da6d581SJan Schmidt 		info = (struct btrfs_tree_block_info *)ptr;
9218da6d581SJan Schmidt 		*info_level = btrfs_tree_block_level(leaf, info);
9228da6d581SJan Schmidt 		ptr += sizeof(struct btrfs_tree_block_info);
9238da6d581SJan Schmidt 		BUG_ON(ptr > end);
924261c84b6SJosef Bacik 	} else if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
925261c84b6SJosef Bacik 		*info_level = found_key.offset;
9268da6d581SJan Schmidt 	} else {
9278da6d581SJan Schmidt 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
9288da6d581SJan Schmidt 	}
9298da6d581SJan Schmidt 
9308da6d581SJan Schmidt 	while (ptr < end) {
9318da6d581SJan Schmidt 		struct btrfs_extent_inline_ref *iref;
9328da6d581SJan Schmidt 		u64 offset;
9338da6d581SJan Schmidt 		int type;
9348da6d581SJan Schmidt 
9358da6d581SJan Schmidt 		iref = (struct btrfs_extent_inline_ref *)ptr;
9363de28d57SLiu Bo 		type = btrfs_get_extent_inline_ref_type(leaf, iref,
9373de28d57SLiu Bo 							BTRFS_REF_TYPE_ANY);
9383de28d57SLiu Bo 		if (type == BTRFS_REF_TYPE_INVALID)
9393de28d57SLiu Bo 			return -EINVAL;
9403de28d57SLiu Bo 
9418da6d581SJan Schmidt 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
9428da6d581SJan Schmidt 
9438da6d581SJan Schmidt 		switch (type) {
9448da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
94500142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees,
94600142756SJeff Mahoney 					     *info_level + 1, offset,
9473ec4d323SEdmund Nadolski 					     bytenr, 1, NULL, GFP_NOFS);
9488da6d581SJan Schmidt 			break;
9498da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
9508da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
9518da6d581SJan Schmidt 			int count;
9528da6d581SJan Schmidt 
9538da6d581SJan Schmidt 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
9548da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
95586d5f994SEdmund Nadolski 
95600142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees, 0, offset,
9573ec4d323SEdmund Nadolski 					     bytenr, count, sc, GFP_NOFS);
9588da6d581SJan Schmidt 			break;
9598da6d581SJan Schmidt 		}
9608da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
96100142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, offset,
96200142756SJeff Mahoney 					       NULL, *info_level + 1,
9633ec4d323SEdmund Nadolski 					       bytenr, 1, NULL, GFP_NOFS);
9648da6d581SJan Schmidt 			break;
9658da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
9668da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
9678da6d581SJan Schmidt 			int count;
9688da6d581SJan Schmidt 			u64 root;
9698da6d581SJan Schmidt 
9708da6d581SJan Schmidt 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
9718da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
9728da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
9738da6d581SJan Schmidt 								      dref);
9748da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
9758da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
976dc046b10SJosef Bacik 
9773ec4d323SEdmund Nadolski 			if (sc && sc->inum && key.objectid != sc->inum) {
978dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
979dc046b10SJosef Bacik 				break;
980dc046b10SJosef Bacik 			}
981dc046b10SJosef Bacik 
9828da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
98386d5f994SEdmund Nadolski 
98400142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, root,
98500142756SJeff Mahoney 					       &key, 0, bytenr, count,
9863ec4d323SEdmund Nadolski 					       sc, GFP_NOFS);
9878da6d581SJan Schmidt 			break;
9888da6d581SJan Schmidt 		}
9898da6d581SJan Schmidt 		default:
9908da6d581SJan Schmidt 			WARN_ON(1);
9918da6d581SJan Schmidt 		}
9921149ab6bSWang Shilong 		if (ret)
9931149ab6bSWang Shilong 			return ret;
9948da6d581SJan Schmidt 		ptr += btrfs_extent_inline_ref_size(type);
9958da6d581SJan Schmidt 	}
9968da6d581SJan Schmidt 
9978da6d581SJan Schmidt 	return 0;
9988da6d581SJan Schmidt }
9998da6d581SJan Schmidt 
10008da6d581SJan Schmidt /*
10018da6d581SJan Schmidt  * add all non-inline backrefs for bytenr to the list
10023ec4d323SEdmund Nadolski  *
10033ec4d323SEdmund Nadolski  * Returns 0 on success, <0 on error, or BACKREF_FOUND_SHARED.
10048da6d581SJan Schmidt  */
1005e0c476b1SJeff Mahoney static int add_keyed_refs(struct btrfs_fs_info *fs_info,
10068da6d581SJan Schmidt 			  struct btrfs_path *path, u64 bytenr,
100786d5f994SEdmund Nadolski 			  int info_level, struct preftrees *preftrees,
10083ec4d323SEdmund Nadolski 			  struct share_check *sc)
10098da6d581SJan Schmidt {
10108da6d581SJan Schmidt 	struct btrfs_root *extent_root = fs_info->extent_root;
10118da6d581SJan Schmidt 	int ret;
10128da6d581SJan Schmidt 	int slot;
10138da6d581SJan Schmidt 	struct extent_buffer *leaf;
10148da6d581SJan Schmidt 	struct btrfs_key key;
10158da6d581SJan Schmidt 
10168da6d581SJan Schmidt 	while (1) {
10178da6d581SJan Schmidt 		ret = btrfs_next_item(extent_root, path);
10188da6d581SJan Schmidt 		if (ret < 0)
10198da6d581SJan Schmidt 			break;
10208da6d581SJan Schmidt 		if (ret) {
10218da6d581SJan Schmidt 			ret = 0;
10228da6d581SJan Schmidt 			break;
10238da6d581SJan Schmidt 		}
10248da6d581SJan Schmidt 
10258da6d581SJan Schmidt 		slot = path->slots[0];
10268da6d581SJan Schmidt 		leaf = path->nodes[0];
10278da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
10288da6d581SJan Schmidt 
10298da6d581SJan Schmidt 		if (key.objectid != bytenr)
10308da6d581SJan Schmidt 			break;
10318da6d581SJan Schmidt 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
10328da6d581SJan Schmidt 			continue;
10338da6d581SJan Schmidt 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
10348da6d581SJan Schmidt 			break;
10358da6d581SJan Schmidt 
10368da6d581SJan Schmidt 		switch (key.type) {
10378da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
103886d5f994SEdmund Nadolski 			/* SHARED DIRECT METADATA backref */
103900142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees,
104000142756SJeff Mahoney 					     info_level + 1, key.offset,
10413ec4d323SEdmund Nadolski 					     bytenr, 1, NULL, GFP_NOFS);
10428da6d581SJan Schmidt 			break;
10438da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
104486d5f994SEdmund Nadolski 			/* SHARED DIRECT FULL backref */
10458da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
10468da6d581SJan Schmidt 			int count;
10478da6d581SJan Schmidt 
10488da6d581SJan Schmidt 			sdref = btrfs_item_ptr(leaf, slot,
10498da6d581SJan Schmidt 					      struct btrfs_shared_data_ref);
10508da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
105100142756SJeff Mahoney 			ret = add_direct_ref(fs_info, preftrees, 0,
105200142756SJeff Mahoney 					     key.offset, bytenr, count,
10533ec4d323SEdmund Nadolski 					     sc, GFP_NOFS);
10548da6d581SJan Schmidt 			break;
10558da6d581SJan Schmidt 		}
10568da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
105786d5f994SEdmund Nadolski 			/* NORMAL INDIRECT METADATA backref */
105800142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, key.offset,
105900142756SJeff Mahoney 					       NULL, info_level + 1, bytenr,
10603ec4d323SEdmund Nadolski 					       1, NULL, GFP_NOFS);
10618da6d581SJan Schmidt 			break;
10628da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
106386d5f994SEdmund Nadolski 			/* NORMAL INDIRECT DATA backref */
10648da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
10658da6d581SJan Schmidt 			int count;
10668da6d581SJan Schmidt 			u64 root;
10678da6d581SJan Schmidt 
10688da6d581SJan Schmidt 			dref = btrfs_item_ptr(leaf, slot,
10698da6d581SJan Schmidt 					      struct btrfs_extent_data_ref);
10708da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
10718da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
10728da6d581SJan Schmidt 								      dref);
10738da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
10748da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
1075dc046b10SJosef Bacik 
10763ec4d323SEdmund Nadolski 			if (sc && sc->inum && key.objectid != sc->inum) {
1077dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1078dc046b10SJosef Bacik 				break;
1079dc046b10SJosef Bacik 			}
1080dc046b10SJosef Bacik 
10818da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
108200142756SJeff Mahoney 			ret = add_indirect_ref(fs_info, preftrees, root,
108300142756SJeff Mahoney 					       &key, 0, bytenr, count,
10843ec4d323SEdmund Nadolski 					       sc, GFP_NOFS);
10858da6d581SJan Schmidt 			break;
10868da6d581SJan Schmidt 		}
10878da6d581SJan Schmidt 		default:
10888da6d581SJan Schmidt 			WARN_ON(1);
10898da6d581SJan Schmidt 		}
10901149ab6bSWang Shilong 		if (ret)
10911149ab6bSWang Shilong 			return ret;
10921149ab6bSWang Shilong 
10938da6d581SJan Schmidt 	}
10948da6d581SJan Schmidt 
10958da6d581SJan Schmidt 	return ret;
10968da6d581SJan Schmidt }
10978da6d581SJan Schmidt 
10988da6d581SJan Schmidt /*
10998da6d581SJan Schmidt  * this adds all existing backrefs (inline backrefs, backrefs and delayed
11008da6d581SJan Schmidt  * refs) for the given bytenr to the refs list, merges duplicates and resolves
11018da6d581SJan Schmidt  * indirect refs to their parent bytenr.
11028da6d581SJan Schmidt  * When roots are found, they're added to the roots list
11038da6d581SJan Schmidt  *
1104de47c9d3SEdmund Nadolski  * If time_seq is set to SEQ_LAST, it will not search delayed_refs, and behave
110521633fc6SQu Wenruo  * much like trans == NULL case, the difference only lies in it will not
110621633fc6SQu Wenruo  * commit root.
110721633fc6SQu Wenruo  * The special case is for qgroup to search roots in commit_transaction().
110821633fc6SQu Wenruo  *
11093ec4d323SEdmund Nadolski  * @sc - if !NULL, then immediately return BACKREF_FOUND_SHARED when a
11103ec4d323SEdmund Nadolski  * shared extent is detected.
11113ec4d323SEdmund Nadolski  *
11123ec4d323SEdmund Nadolski  * Otherwise this returns 0 for success and <0 for an error.
11133ec4d323SEdmund Nadolski  *
1114*c995ab3cSZygo Blaxell  * If ignore_offset is set to false, only extent refs whose offsets match
1115*c995ab3cSZygo Blaxell  * extent_item_pos are returned.  If true, every extent ref is returned
1116*c995ab3cSZygo Blaxell  * and extent_item_pos is ignored.
1117*c995ab3cSZygo Blaxell  *
11188da6d581SJan Schmidt  * FIXME some caching might speed things up
11198da6d581SJan Schmidt  */
11208da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans,
11218da6d581SJan Schmidt 			     struct btrfs_fs_info *fs_info, u64 bytenr,
1122097b8a7cSJan Schmidt 			     u64 time_seq, struct ulist *refs,
1123dc046b10SJosef Bacik 			     struct ulist *roots, const u64 *extent_item_pos,
1124*c995ab3cSZygo Blaxell 			     struct share_check *sc, bool ignore_offset)
11258da6d581SJan Schmidt {
11268da6d581SJan Schmidt 	struct btrfs_key key;
11278da6d581SJan Schmidt 	struct btrfs_path *path;
11288da6d581SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
1129d3b01064SLi Zefan 	struct btrfs_delayed_ref_head *head;
11308da6d581SJan Schmidt 	int info_level = 0;
11318da6d581SJan Schmidt 	int ret;
1132e0c476b1SJeff Mahoney 	struct prelim_ref *ref;
113386d5f994SEdmund Nadolski 	struct rb_node *node;
1134f05c4746SWang Shilong 	struct extent_inode_elem *eie = NULL;
113586d5f994SEdmund Nadolski 	/* total of both direct AND indirect refs! */
113644853868SJosef Bacik 	u64 total_refs = 0;
113786d5f994SEdmund Nadolski 	struct preftrees preftrees = {
113886d5f994SEdmund Nadolski 		.direct = PREFTREE_INIT,
113986d5f994SEdmund Nadolski 		.indirect = PREFTREE_INIT,
114086d5f994SEdmund Nadolski 		.indirect_missing_keys = PREFTREE_INIT
114186d5f994SEdmund Nadolski 	};
11428da6d581SJan Schmidt 
11438da6d581SJan Schmidt 	key.objectid = bytenr;
11448da6d581SJan Schmidt 	key.offset = (u64)-1;
1145261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1146261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1147261c84b6SJosef Bacik 	else
1148261c84b6SJosef Bacik 		key.type = BTRFS_EXTENT_ITEM_KEY;
11498da6d581SJan Schmidt 
11508da6d581SJan Schmidt 	path = btrfs_alloc_path();
11518da6d581SJan Schmidt 	if (!path)
11528da6d581SJan Schmidt 		return -ENOMEM;
1153e84752d4SWang Shilong 	if (!trans) {
1154da61d31aSJosef Bacik 		path->search_commit_root = 1;
1155e84752d4SWang Shilong 		path->skip_locking = 1;
1156e84752d4SWang Shilong 	}
11578da6d581SJan Schmidt 
1158de47c9d3SEdmund Nadolski 	if (time_seq == SEQ_LAST)
115921633fc6SQu Wenruo 		path->skip_locking = 1;
116021633fc6SQu Wenruo 
11618da6d581SJan Schmidt 	/*
11628da6d581SJan Schmidt 	 * grab both a lock on the path and a lock on the delayed ref head.
11638da6d581SJan Schmidt 	 * We need both to get a consistent picture of how the refs look
11648da6d581SJan Schmidt 	 * at a specified point in time
11658da6d581SJan Schmidt 	 */
11668da6d581SJan Schmidt again:
1167d3b01064SLi Zefan 	head = NULL;
1168d3b01064SLi Zefan 
11698da6d581SJan Schmidt 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
11708da6d581SJan Schmidt 	if (ret < 0)
11718da6d581SJan Schmidt 		goto out;
11728da6d581SJan Schmidt 	BUG_ON(ret == 0);
11738da6d581SJan Schmidt 
1174faa2dbf0SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
117521633fc6SQu Wenruo 	if (trans && likely(trans->type != __TRANS_DUMMY) &&
1176de47c9d3SEdmund Nadolski 	    time_seq != SEQ_LAST) {
1177faa2dbf0SJosef Bacik #else
1178de47c9d3SEdmund Nadolski 	if (trans && time_seq != SEQ_LAST) {
1179faa2dbf0SJosef Bacik #endif
11808da6d581SJan Schmidt 		/*
11817a3ae2f8SJan Schmidt 		 * look if there are updates for this ref queued and lock the
11827a3ae2f8SJan Schmidt 		 * head
11838da6d581SJan Schmidt 		 */
11848da6d581SJan Schmidt 		delayed_refs = &trans->transaction->delayed_refs;
11858da6d581SJan Schmidt 		spin_lock(&delayed_refs->lock);
1186f72ad18eSLiu Bo 		head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
11878da6d581SJan Schmidt 		if (head) {
11888da6d581SJan Schmidt 			if (!mutex_trylock(&head->mutex)) {
1189d278850eSJosef Bacik 				refcount_inc(&head->refs);
11908da6d581SJan Schmidt 				spin_unlock(&delayed_refs->lock);
11918da6d581SJan Schmidt 
11928da6d581SJan Schmidt 				btrfs_release_path(path);
11938da6d581SJan Schmidt 
11948da6d581SJan Schmidt 				/*
11958da6d581SJan Schmidt 				 * Mutex was contended, block until it's
11968da6d581SJan Schmidt 				 * released and try again
11978da6d581SJan Schmidt 				 */
11988da6d581SJan Schmidt 				mutex_lock(&head->mutex);
11998da6d581SJan Schmidt 				mutex_unlock(&head->mutex);
1200d278850eSJosef Bacik 				btrfs_put_delayed_ref_head(head);
12018da6d581SJan Schmidt 				goto again;
12028da6d581SJan Schmidt 			}
1203d7df2c79SJosef Bacik 			spin_unlock(&delayed_refs->lock);
120400142756SJeff Mahoney 			ret = add_delayed_refs(fs_info, head, time_seq,
12053ec4d323SEdmund Nadolski 					       &preftrees, &total_refs, sc);
1206155725c9SJan Schmidt 			mutex_unlock(&head->mutex);
1207d7df2c79SJosef Bacik 			if (ret)
12088da6d581SJan Schmidt 				goto out;
1209d7df2c79SJosef Bacik 		} else {
12108da6d581SJan Schmidt 			spin_unlock(&delayed_refs->lock);
12117a3ae2f8SJan Schmidt 		}
1212d7df2c79SJosef Bacik 	}
12138da6d581SJan Schmidt 
12148da6d581SJan Schmidt 	if (path->slots[0]) {
12158da6d581SJan Schmidt 		struct extent_buffer *leaf;
12168da6d581SJan Schmidt 		int slot;
12178da6d581SJan Schmidt 
1218dadcaf78SJan Schmidt 		path->slots[0]--;
12198da6d581SJan Schmidt 		leaf = path->nodes[0];
1220dadcaf78SJan Schmidt 		slot = path->slots[0];
12218da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
12228da6d581SJan Schmidt 		if (key.objectid == bytenr &&
1223261c84b6SJosef Bacik 		    (key.type == BTRFS_EXTENT_ITEM_KEY ||
1224261c84b6SJosef Bacik 		     key.type == BTRFS_METADATA_ITEM_KEY)) {
122500142756SJeff Mahoney 			ret = add_inline_refs(fs_info, path, bytenr,
122600142756SJeff Mahoney 					      &info_level, &preftrees,
12273ec4d323SEdmund Nadolski 					      &total_refs, sc);
12288da6d581SJan Schmidt 			if (ret)
12298da6d581SJan Schmidt 				goto out;
1230e0c476b1SJeff Mahoney 			ret = add_keyed_refs(fs_info, path, bytenr, info_level,
12313ec4d323SEdmund Nadolski 					     &preftrees, sc);
12328da6d581SJan Schmidt 			if (ret)
12338da6d581SJan Schmidt 				goto out;
12348da6d581SJan Schmidt 		}
12358da6d581SJan Schmidt 	}
123686d5f994SEdmund Nadolski 
12378da6d581SJan Schmidt 	btrfs_release_path(path);
12388da6d581SJan Schmidt 
123986d5f994SEdmund Nadolski 	ret = add_missing_keys(fs_info, &preftrees);
1240d5c88b73SJan Schmidt 	if (ret)
1241d5c88b73SJan Schmidt 		goto out;
1242d5c88b73SJan Schmidt 
124386d5f994SEdmund Nadolski 	WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect_missing_keys.root));
12448da6d581SJan Schmidt 
124586d5f994SEdmund Nadolski 	ret = resolve_indirect_refs(fs_info, path, time_seq, &preftrees,
1246*c995ab3cSZygo Blaxell 				    extent_item_pos, total_refs, sc, ignore_offset);
12478da6d581SJan Schmidt 	if (ret)
12488da6d581SJan Schmidt 		goto out;
12498da6d581SJan Schmidt 
125086d5f994SEdmund Nadolski 	WARN_ON(!RB_EMPTY_ROOT(&preftrees.indirect.root));
12518da6d581SJan Schmidt 
125286d5f994SEdmund Nadolski 	/*
125386d5f994SEdmund Nadolski 	 * This walks the tree of merged and resolved refs. Tree blocks are
125486d5f994SEdmund Nadolski 	 * read in as needed. Unique entries are added to the ulist, and
125586d5f994SEdmund Nadolski 	 * the list of found roots is updated.
125686d5f994SEdmund Nadolski 	 *
125786d5f994SEdmund Nadolski 	 * We release the entire tree in one go before returning.
125886d5f994SEdmund Nadolski 	 */
125986d5f994SEdmund Nadolski 	node = rb_first(&preftrees.direct.root);
126086d5f994SEdmund Nadolski 	while (node) {
126186d5f994SEdmund Nadolski 		ref = rb_entry(node, struct prelim_ref, rbnode);
126286d5f994SEdmund Nadolski 		node = rb_next(&ref->rbnode);
12636c1500f2SJulia Lawall 		WARN_ON(ref->count < 0);
126498cfee21SWang Shilong 		if (roots && ref->count && ref->root_id && ref->parent == 0) {
12653ec4d323SEdmund Nadolski 			if (sc && sc->root_objectid &&
12663ec4d323SEdmund Nadolski 			    ref->root_id != sc->root_objectid) {
1267dc046b10SJosef Bacik 				ret = BACKREF_FOUND_SHARED;
1268dc046b10SJosef Bacik 				goto out;
1269dc046b10SJosef Bacik 			}
1270dc046b10SJosef Bacik 
12718da6d581SJan Schmidt 			/* no parent == root of tree */
12728da6d581SJan Schmidt 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
1273f1723939SWang Shilong 			if (ret < 0)
1274f1723939SWang Shilong 				goto out;
12758da6d581SJan Schmidt 		}
12768da6d581SJan Schmidt 		if (ref->count && ref->parent) {
12778a56457fSJosef Bacik 			if (extent_item_pos && !ref->inode_list &&
12788a56457fSJosef Bacik 			    ref->level == 0) {
1279976b1908SJan Schmidt 				struct extent_buffer *eb;
1280707e8a07SDavid Sterba 
12812ff7e61eSJeff Mahoney 				eb = read_tree_block(fs_info, ref->parent, 0);
128264c043deSLiu Bo 				if (IS_ERR(eb)) {
128364c043deSLiu Bo 					ret = PTR_ERR(eb);
128464c043deSLiu Bo 					goto out;
128564c043deSLiu Bo 				} else if (!extent_buffer_uptodate(eb)) {
1286416bc658SJosef Bacik 					free_extent_buffer(eb);
1287c16c2e2eSWang Shilong 					ret = -EIO;
1288c16c2e2eSWang Shilong 					goto out;
1289416bc658SJosef Bacik 				}
12906f7ff6d7SFilipe Manana 				btrfs_tree_read_lock(eb);
12916f7ff6d7SFilipe Manana 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1292976b1908SJan Schmidt 				ret = find_extent_in_eb(eb, bytenr,
1293*c995ab3cSZygo Blaxell 							*extent_item_pos, &eie, ignore_offset);
12946f7ff6d7SFilipe Manana 				btrfs_tree_read_unlock_blocking(eb);
1295976b1908SJan Schmidt 				free_extent_buffer(eb);
1296f5929cd8SFilipe David Borba Manana 				if (ret < 0)
1297f5929cd8SFilipe David Borba Manana 					goto out;
1298f5929cd8SFilipe David Borba Manana 				ref->inode_list = eie;
1299976b1908SJan Schmidt 			}
13004eb1f66dSTakashi Iwai 			ret = ulist_add_merge_ptr(refs, ref->parent,
13014eb1f66dSTakashi Iwai 						  ref->inode_list,
13024eb1f66dSTakashi Iwai 						  (void **)&eie, GFP_NOFS);
1303f1723939SWang Shilong 			if (ret < 0)
1304f1723939SWang Shilong 				goto out;
13053301958bSJan Schmidt 			if (!ret && extent_item_pos) {
13063301958bSJan Schmidt 				/*
13073301958bSJan Schmidt 				 * we've recorded that parent, so we must extend
13083301958bSJan Schmidt 				 * its inode list here
13093301958bSJan Schmidt 				 */
13103301958bSJan Schmidt 				BUG_ON(!eie);
13113301958bSJan Schmidt 				while (eie->next)
13123301958bSJan Schmidt 					eie = eie->next;
13133301958bSJan Schmidt 				eie->next = ref->inode_list;
13143301958bSJan Schmidt 			}
1315f05c4746SWang Shilong 			eie = NULL;
13168da6d581SJan Schmidt 		}
13179dd14fd6SEdmund Nadolski 		cond_resched();
13188da6d581SJan Schmidt 	}
13198da6d581SJan Schmidt 
13208da6d581SJan Schmidt out:
13218da6d581SJan Schmidt 	btrfs_free_path(path);
132286d5f994SEdmund Nadolski 
132386d5f994SEdmund Nadolski 	prelim_release(&preftrees.direct);
132486d5f994SEdmund Nadolski 	prelim_release(&preftrees.indirect);
132586d5f994SEdmund Nadolski 	prelim_release(&preftrees.indirect_missing_keys);
132686d5f994SEdmund Nadolski 
1327f05c4746SWang Shilong 	if (ret < 0)
1328f05c4746SWang Shilong 		free_inode_elem_list(eie);
13298da6d581SJan Schmidt 	return ret;
13308da6d581SJan Schmidt }
13318da6d581SJan Schmidt 
1332976b1908SJan Schmidt static void free_leaf_list(struct ulist *blocks)
1333976b1908SJan Schmidt {
1334976b1908SJan Schmidt 	struct ulist_node *node = NULL;
1335976b1908SJan Schmidt 	struct extent_inode_elem *eie;
1336976b1908SJan Schmidt 	struct ulist_iterator uiter;
1337976b1908SJan Schmidt 
1338976b1908SJan Schmidt 	ULIST_ITER_INIT(&uiter);
1339976b1908SJan Schmidt 	while ((node = ulist_next(blocks, &uiter))) {
1340976b1908SJan Schmidt 		if (!node->aux)
1341976b1908SJan Schmidt 			continue;
13424dae077aSJeff Mahoney 		eie = unode_aux_to_inode_list(node);
1343f05c4746SWang Shilong 		free_inode_elem_list(eie);
1344976b1908SJan Schmidt 		node->aux = 0;
1345976b1908SJan Schmidt 	}
1346976b1908SJan Schmidt 
1347976b1908SJan Schmidt 	ulist_free(blocks);
1348976b1908SJan Schmidt }
1349976b1908SJan Schmidt 
13508da6d581SJan Schmidt /*
13518da6d581SJan Schmidt  * Finds all leafs with a reference to the specified combination of bytenr and
13528da6d581SJan Schmidt  * offset. key_list_head will point to a list of corresponding keys (caller must
13538da6d581SJan Schmidt  * free each list element). The leafs will be stored in the leafs ulist, which
13548da6d581SJan Schmidt  * must be freed with ulist_free.
13558da6d581SJan Schmidt  *
13568da6d581SJan Schmidt  * returns 0 on success, <0 on error
13578da6d581SJan Schmidt  */
13588da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
13598da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
1360097b8a7cSJan Schmidt 				u64 time_seq, struct ulist **leafs,
1361*c995ab3cSZygo Blaxell 				const u64 *extent_item_pos, bool ignore_offset)
13628da6d581SJan Schmidt {
13638da6d581SJan Schmidt 	int ret;
13648da6d581SJan Schmidt 
13658da6d581SJan Schmidt 	*leafs = ulist_alloc(GFP_NOFS);
136698cfee21SWang Shilong 	if (!*leafs)
13678da6d581SJan Schmidt 		return -ENOMEM;
13688da6d581SJan Schmidt 
1369afce772eSLu Fengqi 	ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1370*c995ab3cSZygo Blaxell 				*leafs, NULL, extent_item_pos, NULL, ignore_offset);
13718da6d581SJan Schmidt 	if (ret < 0 && ret != -ENOENT) {
1372976b1908SJan Schmidt 		free_leaf_list(*leafs);
13738da6d581SJan Schmidt 		return ret;
13748da6d581SJan Schmidt 	}
13758da6d581SJan Schmidt 
13768da6d581SJan Schmidt 	return 0;
13778da6d581SJan Schmidt }
13788da6d581SJan Schmidt 
13798da6d581SJan Schmidt /*
13808da6d581SJan Schmidt  * walk all backrefs for a given extent to find all roots that reference this
13818da6d581SJan Schmidt  * extent. Walking a backref means finding all extents that reference this
13828da6d581SJan Schmidt  * extent and in turn walk the backrefs of those, too. Naturally this is a
13838da6d581SJan Schmidt  * recursive process, but here it is implemented in an iterative fashion: We
13848da6d581SJan Schmidt  * find all referencing extents for the extent in question and put them on a
13858da6d581SJan Schmidt  * list. In turn, we find all referencing extents for those, further appending
13868da6d581SJan Schmidt  * to the list. The way we iterate the list allows adding more elements after
13878da6d581SJan Schmidt  * the current while iterating. The process stops when we reach the end of the
13888da6d581SJan Schmidt  * list. Found roots are added to the roots list.
13898da6d581SJan Schmidt  *
13908da6d581SJan Schmidt  * returns 0 on success, < 0 on error.
13918da6d581SJan Schmidt  */
1392e0c476b1SJeff Mahoney static int btrfs_find_all_roots_safe(struct btrfs_trans_handle *trans,
13938da6d581SJan Schmidt 				     struct btrfs_fs_info *fs_info, u64 bytenr,
1394*c995ab3cSZygo Blaxell 				     u64 time_seq, struct ulist **roots,
1395*c995ab3cSZygo Blaxell 				     bool ignore_offset)
13968da6d581SJan Schmidt {
13978da6d581SJan Schmidt 	struct ulist *tmp;
13988da6d581SJan Schmidt 	struct ulist_node *node = NULL;
1399cd1b413cSJan Schmidt 	struct ulist_iterator uiter;
14008da6d581SJan Schmidt 	int ret;
14018da6d581SJan Schmidt 
14028da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
14038da6d581SJan Schmidt 	if (!tmp)
14048da6d581SJan Schmidt 		return -ENOMEM;
14058da6d581SJan Schmidt 	*roots = ulist_alloc(GFP_NOFS);
14068da6d581SJan Schmidt 	if (!*roots) {
14078da6d581SJan Schmidt 		ulist_free(tmp);
14088da6d581SJan Schmidt 		return -ENOMEM;
14098da6d581SJan Schmidt 	}
14108da6d581SJan Schmidt 
1411cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&uiter);
14128da6d581SJan Schmidt 	while (1) {
1413afce772eSLu Fengqi 		ret = find_parent_nodes(trans, fs_info, bytenr, time_seq,
1414*c995ab3cSZygo Blaxell 					tmp, *roots, NULL, NULL, ignore_offset);
14158da6d581SJan Schmidt 		if (ret < 0 && ret != -ENOENT) {
14168da6d581SJan Schmidt 			ulist_free(tmp);
14178da6d581SJan Schmidt 			ulist_free(*roots);
14188da6d581SJan Schmidt 			return ret;
14198da6d581SJan Schmidt 		}
1420cd1b413cSJan Schmidt 		node = ulist_next(tmp, &uiter);
14218da6d581SJan Schmidt 		if (!node)
14228da6d581SJan Schmidt 			break;
14238da6d581SJan Schmidt 		bytenr = node->val;
1424bca1a290SWang Shilong 		cond_resched();
14258da6d581SJan Schmidt 	}
14268da6d581SJan Schmidt 
14278da6d581SJan Schmidt 	ulist_free(tmp);
14288da6d581SJan Schmidt 	return 0;
14298da6d581SJan Schmidt }
14308da6d581SJan Schmidt 
14319e351cc8SJosef Bacik int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
14329e351cc8SJosef Bacik 			 struct btrfs_fs_info *fs_info, u64 bytenr,
1433*c995ab3cSZygo Blaxell 			 u64 time_seq, struct ulist **roots,
1434*c995ab3cSZygo Blaxell 			 bool ignore_offset)
14359e351cc8SJosef Bacik {
14369e351cc8SJosef Bacik 	int ret;
14379e351cc8SJosef Bacik 
14389e351cc8SJosef Bacik 	if (!trans)
14399e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1440e0c476b1SJeff Mahoney 	ret = btrfs_find_all_roots_safe(trans, fs_info, bytenr,
1441*c995ab3cSZygo Blaxell 					time_seq, roots, ignore_offset);
14429e351cc8SJosef Bacik 	if (!trans)
14439e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
14449e351cc8SJosef Bacik 	return ret;
14459e351cc8SJosef Bacik }
14469e351cc8SJosef Bacik 
14472c2ed5aaSMark Fasheh /**
14482c2ed5aaSMark Fasheh  * btrfs_check_shared - tell us whether an extent is shared
14492c2ed5aaSMark Fasheh  *
14502c2ed5aaSMark Fasheh  * btrfs_check_shared uses the backref walking code but will short
14512c2ed5aaSMark Fasheh  * circuit as soon as it finds a root or inode that doesn't match the
14522c2ed5aaSMark Fasheh  * one passed in. This provides a significant performance benefit for
14532c2ed5aaSMark Fasheh  * callers (such as fiemap) which want to know whether the extent is
14542c2ed5aaSMark Fasheh  * shared but do not need a ref count.
14552c2ed5aaSMark Fasheh  *
1456bb739cf0SEdmund Nadolski  * This attempts to allocate a transaction in order to account for
1457bb739cf0SEdmund Nadolski  * delayed refs, but continues on even when the alloc fails.
1458bb739cf0SEdmund Nadolski  *
14592c2ed5aaSMark Fasheh  * Return: 0 if extent is not shared, 1 if it is shared, < 0 on error.
14602c2ed5aaSMark Fasheh  */
1461bb739cf0SEdmund Nadolski int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr)
1462dc046b10SJosef Bacik {
1463bb739cf0SEdmund Nadolski 	struct btrfs_fs_info *fs_info = root->fs_info;
1464bb739cf0SEdmund Nadolski 	struct btrfs_trans_handle *trans;
1465dc046b10SJosef Bacik 	struct ulist *tmp = NULL;
1466dc046b10SJosef Bacik 	struct ulist *roots = NULL;
1467dc046b10SJosef Bacik 	struct ulist_iterator uiter;
1468dc046b10SJosef Bacik 	struct ulist_node *node;
14693284da7bSDavid Sterba 	struct seq_list elem = SEQ_LIST_INIT(elem);
1470dc046b10SJosef Bacik 	int ret = 0;
14713ec4d323SEdmund Nadolski 	struct share_check shared = {
14723ec4d323SEdmund Nadolski 		.root_objectid = root->objectid,
14733ec4d323SEdmund Nadolski 		.inum = inum,
14743ec4d323SEdmund Nadolski 		.share_count = 0,
14753ec4d323SEdmund Nadolski 	};
1476dc046b10SJosef Bacik 
1477dc046b10SJosef Bacik 	tmp = ulist_alloc(GFP_NOFS);
1478dc046b10SJosef Bacik 	roots = ulist_alloc(GFP_NOFS);
1479dc046b10SJosef Bacik 	if (!tmp || !roots) {
1480dc046b10SJosef Bacik 		ulist_free(tmp);
1481dc046b10SJosef Bacik 		ulist_free(roots);
1482dc046b10SJosef Bacik 		return -ENOMEM;
1483dc046b10SJosef Bacik 	}
1484dc046b10SJosef Bacik 
1485bb739cf0SEdmund Nadolski 	trans = btrfs_join_transaction(root);
1486bb739cf0SEdmund Nadolski 	if (IS_ERR(trans)) {
1487bb739cf0SEdmund Nadolski 		trans = NULL;
1488dc046b10SJosef Bacik 		down_read(&fs_info->commit_root_sem);
1489bb739cf0SEdmund Nadolski 	} else {
1490bb739cf0SEdmund Nadolski 		btrfs_get_tree_mod_seq(fs_info, &elem);
1491bb739cf0SEdmund Nadolski 	}
1492bb739cf0SEdmund Nadolski 
1493dc046b10SJosef Bacik 	ULIST_ITER_INIT(&uiter);
1494dc046b10SJosef Bacik 	while (1) {
1495dc046b10SJosef Bacik 		ret = find_parent_nodes(trans, fs_info, bytenr, elem.seq, tmp,
1496*c995ab3cSZygo Blaxell 					roots, NULL, &shared, false);
1497dc046b10SJosef Bacik 		if (ret == BACKREF_FOUND_SHARED) {
14982c2ed5aaSMark Fasheh 			/* this is the only condition under which we return 1 */
1499dc046b10SJosef Bacik 			ret = 1;
1500dc046b10SJosef Bacik 			break;
1501dc046b10SJosef Bacik 		}
1502dc046b10SJosef Bacik 		if (ret < 0 && ret != -ENOENT)
1503dc046b10SJosef Bacik 			break;
15042c2ed5aaSMark Fasheh 		ret = 0;
1505dc046b10SJosef Bacik 		node = ulist_next(tmp, &uiter);
1506dc046b10SJosef Bacik 		if (!node)
1507dc046b10SJosef Bacik 			break;
1508dc046b10SJosef Bacik 		bytenr = node->val;
1509dc046b10SJosef Bacik 		cond_resched();
1510dc046b10SJosef Bacik 	}
1511bb739cf0SEdmund Nadolski 
1512bb739cf0SEdmund Nadolski 	if (trans) {
1513dc046b10SJosef Bacik 		btrfs_put_tree_mod_seq(fs_info, &elem);
1514bb739cf0SEdmund Nadolski 		btrfs_end_transaction(trans);
1515bb739cf0SEdmund Nadolski 	} else {
1516dc046b10SJosef Bacik 		up_read(&fs_info->commit_root_sem);
1517bb739cf0SEdmund Nadolski 	}
1518dc046b10SJosef Bacik 	ulist_free(tmp);
1519dc046b10SJosef Bacik 	ulist_free(roots);
1520dc046b10SJosef Bacik 	return ret;
1521dc046b10SJosef Bacik }
1522dc046b10SJosef Bacik 
1523f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
1524f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
1525f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
1526f186373fSMark Fasheh 			  u64 *found_off)
1527f186373fSMark Fasheh {
1528f186373fSMark Fasheh 	int ret, slot;
1529f186373fSMark Fasheh 	struct btrfs_key key;
1530f186373fSMark Fasheh 	struct btrfs_key found_key;
1531f186373fSMark Fasheh 	struct btrfs_inode_extref *extref;
153273980becSJeff Mahoney 	const struct extent_buffer *leaf;
1533f186373fSMark Fasheh 	unsigned long ptr;
1534f186373fSMark Fasheh 
1535f186373fSMark Fasheh 	key.objectid = inode_objectid;
1536962a298fSDavid Sterba 	key.type = BTRFS_INODE_EXTREF_KEY;
1537f186373fSMark Fasheh 	key.offset = start_off;
1538f186373fSMark Fasheh 
1539f186373fSMark Fasheh 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1540f186373fSMark Fasheh 	if (ret < 0)
1541f186373fSMark Fasheh 		return ret;
1542f186373fSMark Fasheh 
1543f186373fSMark Fasheh 	while (1) {
1544f186373fSMark Fasheh 		leaf = path->nodes[0];
1545f186373fSMark Fasheh 		slot = path->slots[0];
1546f186373fSMark Fasheh 		if (slot >= btrfs_header_nritems(leaf)) {
1547f186373fSMark Fasheh 			/*
1548f186373fSMark Fasheh 			 * If the item at offset is not found,
1549f186373fSMark Fasheh 			 * btrfs_search_slot will point us to the slot
1550f186373fSMark Fasheh 			 * where it should be inserted. In our case
1551f186373fSMark Fasheh 			 * that will be the slot directly before the
1552f186373fSMark Fasheh 			 * next INODE_REF_KEY_V2 item. In the case
1553f186373fSMark Fasheh 			 * that we're pointing to the last slot in a
1554f186373fSMark Fasheh 			 * leaf, we must move one leaf over.
1555f186373fSMark Fasheh 			 */
1556f186373fSMark Fasheh 			ret = btrfs_next_leaf(root, path);
1557f186373fSMark Fasheh 			if (ret) {
1558f186373fSMark Fasheh 				if (ret >= 1)
1559f186373fSMark Fasheh 					ret = -ENOENT;
1560f186373fSMark Fasheh 				break;
1561f186373fSMark Fasheh 			}
1562f186373fSMark Fasheh 			continue;
1563f186373fSMark Fasheh 		}
1564f186373fSMark Fasheh 
1565f186373fSMark Fasheh 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
1566f186373fSMark Fasheh 
1567f186373fSMark Fasheh 		/*
1568f186373fSMark Fasheh 		 * Check that we're still looking at an extended ref key for
1569f186373fSMark Fasheh 		 * this particular objectid. If we have different
1570f186373fSMark Fasheh 		 * objectid or type then there are no more to be found
1571f186373fSMark Fasheh 		 * in the tree and we can exit.
1572f186373fSMark Fasheh 		 */
1573f186373fSMark Fasheh 		ret = -ENOENT;
1574f186373fSMark Fasheh 		if (found_key.objectid != inode_objectid)
1575f186373fSMark Fasheh 			break;
1576962a298fSDavid Sterba 		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
1577f186373fSMark Fasheh 			break;
1578f186373fSMark Fasheh 
1579f186373fSMark Fasheh 		ret = 0;
1580f186373fSMark Fasheh 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1581f186373fSMark Fasheh 		extref = (struct btrfs_inode_extref *)ptr;
1582f186373fSMark Fasheh 		*ret_extref = extref;
1583f186373fSMark Fasheh 		if (found_off)
1584f186373fSMark Fasheh 			*found_off = found_key.offset;
1585f186373fSMark Fasheh 		break;
1586f186373fSMark Fasheh 	}
1587f186373fSMark Fasheh 
1588f186373fSMark Fasheh 	return ret;
1589f186373fSMark Fasheh }
1590f186373fSMark Fasheh 
159148a3b636SEric Sandeen /*
159248a3b636SEric Sandeen  * this iterates to turn a name (from iref/extref) into a full filesystem path.
159348a3b636SEric Sandeen  * Elements of the path are separated by '/' and the path is guaranteed to be
159448a3b636SEric Sandeen  * 0-terminated. the path is only given within the current file system.
159548a3b636SEric Sandeen  * Therefore, it never starts with a '/'. the caller is responsible to provide
159648a3b636SEric Sandeen  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
159748a3b636SEric Sandeen  * the start point of the resulting string is returned. this pointer is within
159848a3b636SEric Sandeen  * dest, normally.
159948a3b636SEric Sandeen  * in case the path buffer would overflow, the pointer is decremented further
160048a3b636SEric Sandeen  * as if output was written to the buffer, though no more output is actually
160148a3b636SEric Sandeen  * generated. that way, the caller can determine how much space would be
160248a3b636SEric Sandeen  * required for the path to fit into the buffer. in that case, the returned
160348a3b636SEric Sandeen  * value will be smaller than dest. callers must check this!
160448a3b636SEric Sandeen  */
160596b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
1606d24bec3aSMark Fasheh 			u32 name_len, unsigned long name_off,
1607a542ad1bSJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
1608a542ad1bSJan Schmidt 			char *dest, u32 size)
1609a542ad1bSJan Schmidt {
1610a542ad1bSJan Schmidt 	int slot;
1611a542ad1bSJan Schmidt 	u64 next_inum;
1612a542ad1bSJan Schmidt 	int ret;
1613661bec6bSGabriel de Perthuis 	s64 bytes_left = ((s64)size) - 1;
1614a542ad1bSJan Schmidt 	struct extent_buffer *eb = eb_in;
1615a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1616b916a59aSJan Schmidt 	int leave_spinning = path->leave_spinning;
1617d24bec3aSMark Fasheh 	struct btrfs_inode_ref *iref;
1618a542ad1bSJan Schmidt 
1619a542ad1bSJan Schmidt 	if (bytes_left >= 0)
1620a542ad1bSJan Schmidt 		dest[bytes_left] = '\0';
1621a542ad1bSJan Schmidt 
1622b916a59aSJan Schmidt 	path->leave_spinning = 1;
1623a542ad1bSJan Schmidt 	while (1) {
1624d24bec3aSMark Fasheh 		bytes_left -= name_len;
1625a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1626a542ad1bSJan Schmidt 			read_extent_buffer(eb, dest + bytes_left,
1627d24bec3aSMark Fasheh 					   name_off, name_len);
1628b916a59aSJan Schmidt 		if (eb != eb_in) {
16290c0fe3b0SFilipe Manana 			if (!path->skip_locking)
1630b916a59aSJan Schmidt 				btrfs_tree_read_unlock_blocking(eb);
1631a542ad1bSJan Schmidt 			free_extent_buffer(eb);
1632b916a59aSJan Schmidt 		}
1633c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, parent, 0,
1634c234a24dSDavid Sterba 				BTRFS_INODE_REF_KEY, &found_key);
16358f24b496SJan Schmidt 		if (ret > 0)
16368f24b496SJan Schmidt 			ret = -ENOENT;
1637a542ad1bSJan Schmidt 		if (ret)
1638a542ad1bSJan Schmidt 			break;
1639d24bec3aSMark Fasheh 
1640a542ad1bSJan Schmidt 		next_inum = found_key.offset;
1641a542ad1bSJan Schmidt 
1642a542ad1bSJan Schmidt 		/* regular exit ahead */
1643a542ad1bSJan Schmidt 		if (parent == next_inum)
1644a542ad1bSJan Schmidt 			break;
1645a542ad1bSJan Schmidt 
1646a542ad1bSJan Schmidt 		slot = path->slots[0];
1647a542ad1bSJan Schmidt 		eb = path->nodes[0];
1648a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
1649b916a59aSJan Schmidt 		if (eb != eb_in) {
16500c0fe3b0SFilipe Manana 			if (!path->skip_locking)
1651b916a59aSJan Schmidt 				btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
16520c0fe3b0SFilipe Manana 			path->nodes[0] = NULL;
16530c0fe3b0SFilipe Manana 			path->locks[0] = 0;
1654b916a59aSJan Schmidt 		}
1655a542ad1bSJan Schmidt 		btrfs_release_path(path);
1656a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1657d24bec3aSMark Fasheh 
1658d24bec3aSMark Fasheh 		name_len = btrfs_inode_ref_name_len(eb, iref);
1659d24bec3aSMark Fasheh 		name_off = (unsigned long)(iref + 1);
1660d24bec3aSMark Fasheh 
1661a542ad1bSJan Schmidt 		parent = next_inum;
1662a542ad1bSJan Schmidt 		--bytes_left;
1663a542ad1bSJan Schmidt 		if (bytes_left >= 0)
1664a542ad1bSJan Schmidt 			dest[bytes_left] = '/';
1665a542ad1bSJan Schmidt 	}
1666a542ad1bSJan Schmidt 
1667a542ad1bSJan Schmidt 	btrfs_release_path(path);
1668b916a59aSJan Schmidt 	path->leave_spinning = leave_spinning;
1669a542ad1bSJan Schmidt 
1670a542ad1bSJan Schmidt 	if (ret)
1671a542ad1bSJan Schmidt 		return ERR_PTR(ret);
1672a542ad1bSJan Schmidt 
1673a542ad1bSJan Schmidt 	return dest + bytes_left;
1674a542ad1bSJan Schmidt }
1675a542ad1bSJan Schmidt 
1676a542ad1bSJan Schmidt /*
1677a542ad1bSJan Schmidt  * this makes the path point to (logical EXTENT_ITEM *)
1678a542ad1bSJan Schmidt  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
1679a542ad1bSJan Schmidt  * tree blocks and <0 on error.
1680a542ad1bSJan Schmidt  */
1681a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
168269917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
168369917e43SLiu Bo 			u64 *flags_ret)
1684a542ad1bSJan Schmidt {
1685a542ad1bSJan Schmidt 	int ret;
1686a542ad1bSJan Schmidt 	u64 flags;
1687261c84b6SJosef Bacik 	u64 size = 0;
1688a542ad1bSJan Schmidt 	u32 item_size;
168973980becSJeff Mahoney 	const struct extent_buffer *eb;
1690a542ad1bSJan Schmidt 	struct btrfs_extent_item *ei;
1691a542ad1bSJan Schmidt 	struct btrfs_key key;
1692a542ad1bSJan Schmidt 
1693261c84b6SJosef Bacik 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1694261c84b6SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
1695261c84b6SJosef Bacik 	else
1696a542ad1bSJan Schmidt 		key.type = BTRFS_EXTENT_ITEM_KEY;
1697a542ad1bSJan Schmidt 	key.objectid = logical;
1698a542ad1bSJan Schmidt 	key.offset = (u64)-1;
1699a542ad1bSJan Schmidt 
1700a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
1701a542ad1bSJan Schmidt 	if (ret < 0)
1702a542ad1bSJan Schmidt 		return ret;
1703a542ad1bSJan Schmidt 
1704850a8cdfSWang Shilong 	ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0);
1705850a8cdfSWang Shilong 	if (ret) {
1706850a8cdfSWang Shilong 		if (ret > 0)
1707580f0a67SJosef Bacik 			ret = -ENOENT;
1708580f0a67SJosef Bacik 		return ret;
1709580f0a67SJosef Bacik 	}
1710850a8cdfSWang Shilong 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
1711261c84b6SJosef Bacik 	if (found_key->type == BTRFS_METADATA_ITEM_KEY)
1712da17066cSJeff Mahoney 		size = fs_info->nodesize;
1713261c84b6SJosef Bacik 	else if (found_key->type == BTRFS_EXTENT_ITEM_KEY)
1714261c84b6SJosef Bacik 		size = found_key->offset;
1715261c84b6SJosef Bacik 
1716580f0a67SJosef Bacik 	if (found_key->objectid > logical ||
1717261c84b6SJosef Bacik 	    found_key->objectid + size <= logical) {
1718ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
1719ab8d0fc4SJeff Mahoney 			"logical %llu is not within any extent", logical);
1720a542ad1bSJan Schmidt 		return -ENOENT;
17214692cf58SJan Schmidt 	}
1722a542ad1bSJan Schmidt 
1723a542ad1bSJan Schmidt 	eb = path->nodes[0];
1724a542ad1bSJan Schmidt 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
1725a542ad1bSJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
1726a542ad1bSJan Schmidt 
1727a542ad1bSJan Schmidt 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
1728a542ad1bSJan Schmidt 	flags = btrfs_extent_flags(eb, ei);
1729a542ad1bSJan Schmidt 
1730ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info,
1731ab8d0fc4SJeff Mahoney 		"logical %llu is at position %llu within the extent (%llu EXTENT_ITEM %llu) flags %#llx size %u",
1732c1c9ff7cSGeert Uytterhoeven 		 logical, logical - found_key->objectid, found_key->objectid,
1733c1c9ff7cSGeert Uytterhoeven 		 found_key->offset, flags, item_size);
173469917e43SLiu Bo 
173569917e43SLiu Bo 	WARN_ON(!flags_ret);
173669917e43SLiu Bo 	if (flags_ret) {
1737a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
173869917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_TREE_BLOCK;
173969917e43SLiu Bo 		else if (flags & BTRFS_EXTENT_FLAG_DATA)
174069917e43SLiu Bo 			*flags_ret = BTRFS_EXTENT_FLAG_DATA;
174169917e43SLiu Bo 		else
174269917e43SLiu Bo 			BUG_ON(1);
174369917e43SLiu Bo 		return 0;
174469917e43SLiu Bo 	}
1745a542ad1bSJan Schmidt 
1746a542ad1bSJan Schmidt 	return -EIO;
1747a542ad1bSJan Schmidt }
1748a542ad1bSJan Schmidt 
1749a542ad1bSJan Schmidt /*
1750a542ad1bSJan Schmidt  * helper function to iterate extent inline refs. ptr must point to a 0 value
1751a542ad1bSJan Schmidt  * for the first call and may be modified. it is used to track state.
1752a542ad1bSJan Schmidt  * if more refs exist, 0 is returned and the next call to
1753e0c476b1SJeff Mahoney  * get_extent_inline_ref must pass the modified ptr parameter to get the
1754a542ad1bSJan Schmidt  * next ref. after the last ref was processed, 1 is returned.
1755a542ad1bSJan Schmidt  * returns <0 on error
1756a542ad1bSJan Schmidt  */
1757e0c476b1SJeff Mahoney static int get_extent_inline_ref(unsigned long *ptr,
175873980becSJeff Mahoney 				 const struct extent_buffer *eb,
175973980becSJeff Mahoney 				 const struct btrfs_key *key,
176073980becSJeff Mahoney 				 const struct btrfs_extent_item *ei,
176173980becSJeff Mahoney 				 u32 item_size,
1762a542ad1bSJan Schmidt 				 struct btrfs_extent_inline_ref **out_eiref,
1763a542ad1bSJan Schmidt 				 int *out_type)
1764a542ad1bSJan Schmidt {
1765a542ad1bSJan Schmidt 	unsigned long end;
1766a542ad1bSJan Schmidt 	u64 flags;
1767a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1768a542ad1bSJan Schmidt 
1769a542ad1bSJan Schmidt 	if (!*ptr) {
1770a542ad1bSJan Schmidt 		/* first call */
1771a542ad1bSJan Schmidt 		flags = btrfs_extent_flags(eb, ei);
1772a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
17736eda71d0SLiu Bo 			if (key->type == BTRFS_METADATA_ITEM_KEY) {
17746eda71d0SLiu Bo 				/* a skinny metadata extent */
17756eda71d0SLiu Bo 				*out_eiref =
17766eda71d0SLiu Bo 				     (struct btrfs_extent_inline_ref *)(ei + 1);
17776eda71d0SLiu Bo 			} else {
17786eda71d0SLiu Bo 				WARN_ON(key->type != BTRFS_EXTENT_ITEM_KEY);
1779a542ad1bSJan Schmidt 				info = (struct btrfs_tree_block_info *)(ei + 1);
1780a542ad1bSJan Schmidt 				*out_eiref =
1781a542ad1bSJan Schmidt 				   (struct btrfs_extent_inline_ref *)(info + 1);
17826eda71d0SLiu Bo 			}
1783a542ad1bSJan Schmidt 		} else {
1784a542ad1bSJan Schmidt 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1785a542ad1bSJan Schmidt 		}
1786a542ad1bSJan Schmidt 		*ptr = (unsigned long)*out_eiref;
1787cd857dd6SLiu Bo 		if ((unsigned long)(*ptr) >= (unsigned long)ei + item_size)
1788a542ad1bSJan Schmidt 			return -ENOENT;
1789a542ad1bSJan Schmidt 	}
1790a542ad1bSJan Schmidt 
1791a542ad1bSJan Schmidt 	end = (unsigned long)ei + item_size;
17926eda71d0SLiu Bo 	*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
17933de28d57SLiu Bo 	*out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref,
17943de28d57SLiu Bo 						     BTRFS_REF_TYPE_ANY);
17953de28d57SLiu Bo 	if (*out_type == BTRFS_REF_TYPE_INVALID)
17963de28d57SLiu Bo 		return -EINVAL;
1797a542ad1bSJan Schmidt 
1798a542ad1bSJan Schmidt 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1799a542ad1bSJan Schmidt 	WARN_ON(*ptr > end);
1800a542ad1bSJan Schmidt 	if (*ptr == end)
1801a542ad1bSJan Schmidt 		return 1; /* last */
1802a542ad1bSJan Schmidt 
1803a542ad1bSJan Schmidt 	return 0;
1804a542ad1bSJan Schmidt }
1805a542ad1bSJan Schmidt 
1806a542ad1bSJan Schmidt /*
1807a542ad1bSJan Schmidt  * reads the tree block backref for an extent. tree level and root are returned
1808a542ad1bSJan Schmidt  * through out_level and out_root. ptr must point to a 0 value for the first
1809e0c476b1SJeff Mahoney  * call and may be modified (see get_extent_inline_ref comment).
1810a542ad1bSJan Schmidt  * returns 0 if data was provided, 1 if there was no more data to provide or
1811a542ad1bSJan Schmidt  * <0 on error.
1812a542ad1bSJan Schmidt  */
1813a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
18146eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
18156eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level)
1816a542ad1bSJan Schmidt {
1817a542ad1bSJan Schmidt 	int ret;
1818a542ad1bSJan Schmidt 	int type;
1819a542ad1bSJan Schmidt 	struct btrfs_extent_inline_ref *eiref;
1820a542ad1bSJan Schmidt 
1821a542ad1bSJan Schmidt 	if (*ptr == (unsigned long)-1)
1822a542ad1bSJan Schmidt 		return 1;
1823a542ad1bSJan Schmidt 
1824a542ad1bSJan Schmidt 	while (1) {
1825e0c476b1SJeff Mahoney 		ret = get_extent_inline_ref(ptr, eb, key, ei, item_size,
1826a542ad1bSJan Schmidt 					      &eiref, &type);
1827a542ad1bSJan Schmidt 		if (ret < 0)
1828a542ad1bSJan Schmidt 			return ret;
1829a542ad1bSJan Schmidt 
1830a542ad1bSJan Schmidt 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1831a542ad1bSJan Schmidt 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1832a542ad1bSJan Schmidt 			break;
1833a542ad1bSJan Schmidt 
1834a542ad1bSJan Schmidt 		if (ret == 1)
1835a542ad1bSJan Schmidt 			return 1;
1836a542ad1bSJan Schmidt 	}
1837a542ad1bSJan Schmidt 
1838a542ad1bSJan Schmidt 	/* we can treat both ref types equally here */
1839a542ad1bSJan Schmidt 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1840a1317f45SFilipe Manana 
1841a1317f45SFilipe Manana 	if (key->type == BTRFS_EXTENT_ITEM_KEY) {
1842a1317f45SFilipe Manana 		struct btrfs_tree_block_info *info;
1843a1317f45SFilipe Manana 
1844a1317f45SFilipe Manana 		info = (struct btrfs_tree_block_info *)(ei + 1);
1845a542ad1bSJan Schmidt 		*out_level = btrfs_tree_block_level(eb, info);
1846a1317f45SFilipe Manana 	} else {
1847a1317f45SFilipe Manana 		ASSERT(key->type == BTRFS_METADATA_ITEM_KEY);
1848a1317f45SFilipe Manana 		*out_level = (u8)key->offset;
1849a1317f45SFilipe Manana 	}
1850a542ad1bSJan Schmidt 
1851a542ad1bSJan Schmidt 	if (ret == 1)
1852a542ad1bSJan Schmidt 		*ptr = (unsigned long)-1;
1853a542ad1bSJan Schmidt 
1854a542ad1bSJan Schmidt 	return 0;
1855a542ad1bSJan Schmidt }
1856a542ad1bSJan Schmidt 
1857ab8d0fc4SJeff Mahoney static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
1858ab8d0fc4SJeff Mahoney 			     struct extent_inode_elem *inode_list,
1859976b1908SJan Schmidt 			     u64 root, u64 extent_item_objectid,
18604692cf58SJan Schmidt 			     iterate_extent_inodes_t *iterate, void *ctx)
1861a542ad1bSJan Schmidt {
1862976b1908SJan Schmidt 	struct extent_inode_elem *eie;
18634692cf58SJan Schmidt 	int ret = 0;
1864a542ad1bSJan Schmidt 
1865976b1908SJan Schmidt 	for (eie = inode_list; eie; eie = eie->next) {
1866ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
1867ab8d0fc4SJeff Mahoney 			    "ref for %llu resolved, key (%llu EXTEND_DATA %llu), root %llu",
1868ab8d0fc4SJeff Mahoney 			    extent_item_objectid, eie->inum,
1869ab8d0fc4SJeff Mahoney 			    eie->offset, root);
1870976b1908SJan Schmidt 		ret = iterate(eie->inum, eie->offset, root, ctx);
18714692cf58SJan Schmidt 		if (ret) {
1872ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_info,
1873ab8d0fc4SJeff Mahoney 				    "stopping iteration for %llu due to ret=%d",
1874976b1908SJan Schmidt 				    extent_item_objectid, ret);
1875a542ad1bSJan Schmidt 			break;
1876a542ad1bSJan Schmidt 		}
1877a542ad1bSJan Schmidt 	}
1878a542ad1bSJan Schmidt 
1879a542ad1bSJan Schmidt 	return ret;
1880a542ad1bSJan Schmidt }
1881a542ad1bSJan Schmidt 
1882a542ad1bSJan Schmidt /*
1883a542ad1bSJan Schmidt  * calls iterate() for every inode that references the extent identified by
18844692cf58SJan Schmidt  * the given parameters.
1885a542ad1bSJan Schmidt  * when the iterator function returns a non-zero value, iteration stops.
1886a542ad1bSJan Schmidt  */
1887a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
18884692cf58SJan Schmidt 				u64 extent_item_objectid, u64 extent_item_pos,
18897a3ae2f8SJan Schmidt 				int search_commit_root,
1890*c995ab3cSZygo Blaxell 				iterate_extent_inodes_t *iterate, void *ctx,
1891*c995ab3cSZygo Blaxell 				bool ignore_offset)
1892a542ad1bSJan Schmidt {
1893a542ad1bSJan Schmidt 	int ret;
1894da61d31aSJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
18957a3ae2f8SJan Schmidt 	struct ulist *refs = NULL;
18967a3ae2f8SJan Schmidt 	struct ulist *roots = NULL;
18974692cf58SJan Schmidt 	struct ulist_node *ref_node = NULL;
18984692cf58SJan Schmidt 	struct ulist_node *root_node = NULL;
18993284da7bSDavid Sterba 	struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
1900cd1b413cSJan Schmidt 	struct ulist_iterator ref_uiter;
1901cd1b413cSJan Schmidt 	struct ulist_iterator root_uiter;
1902a542ad1bSJan Schmidt 
1903ab8d0fc4SJeff Mahoney 	btrfs_debug(fs_info, "resolving all inodes for extent %llu",
19044692cf58SJan Schmidt 			extent_item_objectid);
19054692cf58SJan Schmidt 
1906da61d31aSJosef Bacik 	if (!search_commit_root) {
19077a3ae2f8SJan Schmidt 		trans = btrfs_join_transaction(fs_info->extent_root);
19087a3ae2f8SJan Schmidt 		if (IS_ERR(trans))
19097a3ae2f8SJan Schmidt 			return PTR_ERR(trans);
19108445f61cSJan Schmidt 		btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
19119e351cc8SJosef Bacik 	} else {
19129e351cc8SJosef Bacik 		down_read(&fs_info->commit_root_sem);
19137a3ae2f8SJan Schmidt 	}
19144692cf58SJan Schmidt 
19154692cf58SJan Schmidt 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1916097b8a7cSJan Schmidt 				   tree_mod_seq_elem.seq, &refs,
1917*c995ab3cSZygo Blaxell 				   &extent_item_pos, ignore_offset);
19184692cf58SJan Schmidt 	if (ret)
19194692cf58SJan Schmidt 		goto out;
19204692cf58SJan Schmidt 
1921cd1b413cSJan Schmidt 	ULIST_ITER_INIT(&ref_uiter);
1922cd1b413cSJan Schmidt 	while (!ret && (ref_node = ulist_next(refs, &ref_uiter))) {
1923e0c476b1SJeff Mahoney 		ret = btrfs_find_all_roots_safe(trans, fs_info, ref_node->val,
1924*c995ab3cSZygo Blaxell 						tree_mod_seq_elem.seq, &roots,
1925*c995ab3cSZygo Blaxell 						ignore_offset);
19264692cf58SJan Schmidt 		if (ret)
1927a542ad1bSJan Schmidt 			break;
1928cd1b413cSJan Schmidt 		ULIST_ITER_INIT(&root_uiter);
1929cd1b413cSJan Schmidt 		while (!ret && (root_node = ulist_next(roots, &root_uiter))) {
1930ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_info,
1931ab8d0fc4SJeff Mahoney 				    "root %llu references leaf %llu, data list %#llx",
1932ab8d0fc4SJeff Mahoney 				    root_node->val, ref_node->val,
1933c1c9ff7cSGeert Uytterhoeven 				    ref_node->aux);
1934ab8d0fc4SJeff Mahoney 			ret = iterate_leaf_refs(fs_info,
1935ab8d0fc4SJeff Mahoney 						(struct extent_inode_elem *)
1936995e01b7SJan Schmidt 						(uintptr_t)ref_node->aux,
1937995e01b7SJan Schmidt 						root_node->val,
1938995e01b7SJan Schmidt 						extent_item_objectid,
1939a542ad1bSJan Schmidt 						iterate, ctx);
19404692cf58SJan Schmidt 		}
1941976b1908SJan Schmidt 		ulist_free(roots);
1942a542ad1bSJan Schmidt 	}
1943a542ad1bSJan Schmidt 
1944976b1908SJan Schmidt 	free_leaf_list(refs);
19454692cf58SJan Schmidt out:
19467a3ae2f8SJan Schmidt 	if (!search_commit_root) {
19478445f61cSJan Schmidt 		btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
19483a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
19499e351cc8SJosef Bacik 	} else {
19509e351cc8SJosef Bacik 		up_read(&fs_info->commit_root_sem);
19517a3ae2f8SJan Schmidt 	}
19527a3ae2f8SJan Schmidt 
1953a542ad1bSJan Schmidt 	return ret;
1954a542ad1bSJan Schmidt }
1955a542ad1bSJan Schmidt 
1956a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1957a542ad1bSJan Schmidt 				struct btrfs_path *path,
1958*c995ab3cSZygo Blaxell 				iterate_extent_inodes_t *iterate, void *ctx,
1959*c995ab3cSZygo Blaxell 				bool ignore_offset)
1960a542ad1bSJan Schmidt {
1961a542ad1bSJan Schmidt 	int ret;
19624692cf58SJan Schmidt 	u64 extent_item_pos;
196369917e43SLiu Bo 	u64 flags = 0;
1964a542ad1bSJan Schmidt 	struct btrfs_key found_key;
19657a3ae2f8SJan Schmidt 	int search_commit_root = path->search_commit_root;
1966a542ad1bSJan Schmidt 
196769917e43SLiu Bo 	ret = extent_from_logical(fs_info, logical, path, &found_key, &flags);
19684692cf58SJan Schmidt 	btrfs_release_path(path);
1969a542ad1bSJan Schmidt 	if (ret < 0)
1970a542ad1bSJan Schmidt 		return ret;
197169917e43SLiu Bo 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
19723627bf45SStefan Behrens 		return -EINVAL;
1973a542ad1bSJan Schmidt 
19744692cf58SJan Schmidt 	extent_item_pos = logical - found_key.objectid;
19757a3ae2f8SJan Schmidt 	ret = iterate_extent_inodes(fs_info, found_key.objectid,
19767a3ae2f8SJan Schmidt 					extent_item_pos, search_commit_root,
1977*c995ab3cSZygo Blaxell 					iterate, ctx, ignore_offset);
1978a542ad1bSJan Schmidt 
1979a542ad1bSJan Schmidt 	return ret;
1980a542ad1bSJan Schmidt }
1981a542ad1bSJan Schmidt 
1982d24bec3aSMark Fasheh typedef int (iterate_irefs_t)(u64 parent, u32 name_len, unsigned long name_off,
1983d24bec3aSMark Fasheh 			      struct extent_buffer *eb, void *ctx);
1984d24bec3aSMark Fasheh 
1985d24bec3aSMark Fasheh static int iterate_inode_refs(u64 inum, struct btrfs_root *fs_root,
1986a542ad1bSJan Schmidt 			      struct btrfs_path *path,
1987a542ad1bSJan Schmidt 			      iterate_irefs_t *iterate, void *ctx)
1988a542ad1bSJan Schmidt {
1989aefc1eb1SJan Schmidt 	int ret = 0;
1990a542ad1bSJan Schmidt 	int slot;
1991a542ad1bSJan Schmidt 	u32 cur;
1992a542ad1bSJan Schmidt 	u32 len;
1993a542ad1bSJan Schmidt 	u32 name_len;
1994a542ad1bSJan Schmidt 	u64 parent = 0;
1995a542ad1bSJan Schmidt 	int found = 0;
1996a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1997a542ad1bSJan Schmidt 	struct btrfs_item *item;
1998a542ad1bSJan Schmidt 	struct btrfs_inode_ref *iref;
1999a542ad1bSJan Schmidt 	struct btrfs_key found_key;
2000a542ad1bSJan Schmidt 
2001aefc1eb1SJan Schmidt 	while (!ret) {
2002c234a24dSDavid Sterba 		ret = btrfs_find_item(fs_root, path, inum,
2003c234a24dSDavid Sterba 				parent ? parent + 1 : 0, BTRFS_INODE_REF_KEY,
2004a542ad1bSJan Schmidt 				&found_key);
2005c234a24dSDavid Sterba 
2006a542ad1bSJan Schmidt 		if (ret < 0)
2007a542ad1bSJan Schmidt 			break;
2008a542ad1bSJan Schmidt 		if (ret) {
2009a542ad1bSJan Schmidt 			ret = found ? 0 : -ENOENT;
2010a542ad1bSJan Schmidt 			break;
2011a542ad1bSJan Schmidt 		}
2012a542ad1bSJan Schmidt 		++found;
2013a542ad1bSJan Schmidt 
2014a542ad1bSJan Schmidt 		parent = found_key.offset;
2015a542ad1bSJan Schmidt 		slot = path->slots[0];
20163fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
20173fe81ce2SFilipe David Borba Manana 		if (!eb) {
20183fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
20193fe81ce2SFilipe David Borba Manana 			break;
20203fe81ce2SFilipe David Borba Manana 		}
20213fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
2022b916a59aSJan Schmidt 		btrfs_tree_read_lock(eb);
2023b916a59aSJan Schmidt 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2024a542ad1bSJan Schmidt 		btrfs_release_path(path);
2025a542ad1bSJan Schmidt 
2026dd3cc16bSRoss Kirk 		item = btrfs_item_nr(slot);
2027a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
2028a542ad1bSJan Schmidt 
2029a542ad1bSJan Schmidt 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
2030a542ad1bSJan Schmidt 			name_len = btrfs_inode_ref_name_len(eb, iref);
2031a542ad1bSJan Schmidt 			/* path must be released before calling iterate()! */
2032ab8d0fc4SJeff Mahoney 			btrfs_debug(fs_root->fs_info,
2033ab8d0fc4SJeff Mahoney 				"following ref at offset %u for inode %llu in tree %llu",
2034ab8d0fc4SJeff Mahoney 				cur, found_key.objectid, fs_root->objectid);
2035d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
2036d24bec3aSMark Fasheh 				      (unsigned long)(iref + 1), eb, ctx);
2037aefc1eb1SJan Schmidt 			if (ret)
2038a542ad1bSJan Schmidt 				break;
2039a542ad1bSJan Schmidt 			len = sizeof(*iref) + name_len;
2040a542ad1bSJan Schmidt 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
2041a542ad1bSJan Schmidt 		}
2042b916a59aSJan Schmidt 		btrfs_tree_read_unlock_blocking(eb);
2043a542ad1bSJan Schmidt 		free_extent_buffer(eb);
2044a542ad1bSJan Schmidt 	}
2045a542ad1bSJan Schmidt 
2046a542ad1bSJan Schmidt 	btrfs_release_path(path);
2047a542ad1bSJan Schmidt 
2048a542ad1bSJan Schmidt 	return ret;
2049a542ad1bSJan Schmidt }
2050a542ad1bSJan Schmidt 
2051d24bec3aSMark Fasheh static int iterate_inode_extrefs(u64 inum, struct btrfs_root *fs_root,
2052d24bec3aSMark Fasheh 				 struct btrfs_path *path,
2053d24bec3aSMark Fasheh 				 iterate_irefs_t *iterate, void *ctx)
2054d24bec3aSMark Fasheh {
2055d24bec3aSMark Fasheh 	int ret;
2056d24bec3aSMark Fasheh 	int slot;
2057d24bec3aSMark Fasheh 	u64 offset = 0;
2058d24bec3aSMark Fasheh 	u64 parent;
2059d24bec3aSMark Fasheh 	int found = 0;
2060d24bec3aSMark Fasheh 	struct extent_buffer *eb;
2061d24bec3aSMark Fasheh 	struct btrfs_inode_extref *extref;
2062d24bec3aSMark Fasheh 	u32 item_size;
2063d24bec3aSMark Fasheh 	u32 cur_offset;
2064d24bec3aSMark Fasheh 	unsigned long ptr;
2065d24bec3aSMark Fasheh 
2066d24bec3aSMark Fasheh 	while (1) {
2067d24bec3aSMark Fasheh 		ret = btrfs_find_one_extref(fs_root, inum, offset, path, &extref,
2068d24bec3aSMark Fasheh 					    &offset);
2069d24bec3aSMark Fasheh 		if (ret < 0)
2070d24bec3aSMark Fasheh 			break;
2071d24bec3aSMark Fasheh 		if (ret) {
2072d24bec3aSMark Fasheh 			ret = found ? 0 : -ENOENT;
2073d24bec3aSMark Fasheh 			break;
2074d24bec3aSMark Fasheh 		}
2075d24bec3aSMark Fasheh 		++found;
2076d24bec3aSMark Fasheh 
2077d24bec3aSMark Fasheh 		slot = path->slots[0];
20783fe81ce2SFilipe David Borba Manana 		eb = btrfs_clone_extent_buffer(path->nodes[0]);
20793fe81ce2SFilipe David Borba Manana 		if (!eb) {
20803fe81ce2SFilipe David Borba Manana 			ret = -ENOMEM;
20813fe81ce2SFilipe David Borba Manana 			break;
20823fe81ce2SFilipe David Borba Manana 		}
20833fe81ce2SFilipe David Borba Manana 		extent_buffer_get(eb);
2084d24bec3aSMark Fasheh 
2085d24bec3aSMark Fasheh 		btrfs_tree_read_lock(eb);
2086d24bec3aSMark Fasheh 		btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
2087d24bec3aSMark Fasheh 		btrfs_release_path(path);
2088d24bec3aSMark Fasheh 
20892849a854SChris Mason 		item_size = btrfs_item_size_nr(eb, slot);
20902849a854SChris Mason 		ptr = btrfs_item_ptr_offset(eb, slot);
2091d24bec3aSMark Fasheh 		cur_offset = 0;
2092d24bec3aSMark Fasheh 
2093d24bec3aSMark Fasheh 		while (cur_offset < item_size) {
2094d24bec3aSMark Fasheh 			u32 name_len;
2095d24bec3aSMark Fasheh 
2096d24bec3aSMark Fasheh 			extref = (struct btrfs_inode_extref *)(ptr + cur_offset);
2097d24bec3aSMark Fasheh 			parent = btrfs_inode_extref_parent(eb, extref);
2098d24bec3aSMark Fasheh 			name_len = btrfs_inode_extref_name_len(eb, extref);
2099d24bec3aSMark Fasheh 			ret = iterate(parent, name_len,
2100d24bec3aSMark Fasheh 				      (unsigned long)&extref->name, eb, ctx);
2101d24bec3aSMark Fasheh 			if (ret)
2102d24bec3aSMark Fasheh 				break;
2103d24bec3aSMark Fasheh 
21042849a854SChris Mason 			cur_offset += btrfs_inode_extref_name_len(eb, extref);
2105d24bec3aSMark Fasheh 			cur_offset += sizeof(*extref);
2106d24bec3aSMark Fasheh 		}
2107d24bec3aSMark Fasheh 		btrfs_tree_read_unlock_blocking(eb);
2108d24bec3aSMark Fasheh 		free_extent_buffer(eb);
2109d24bec3aSMark Fasheh 
2110d24bec3aSMark Fasheh 		offset++;
2111d24bec3aSMark Fasheh 	}
2112d24bec3aSMark Fasheh 
2113d24bec3aSMark Fasheh 	btrfs_release_path(path);
2114d24bec3aSMark Fasheh 
2115d24bec3aSMark Fasheh 	return ret;
2116d24bec3aSMark Fasheh }
2117d24bec3aSMark Fasheh 
2118d24bec3aSMark Fasheh static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
2119d24bec3aSMark Fasheh 			 struct btrfs_path *path, iterate_irefs_t *iterate,
2120d24bec3aSMark Fasheh 			 void *ctx)
2121d24bec3aSMark Fasheh {
2122d24bec3aSMark Fasheh 	int ret;
2123d24bec3aSMark Fasheh 	int found_refs = 0;
2124d24bec3aSMark Fasheh 
2125d24bec3aSMark Fasheh 	ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
2126d24bec3aSMark Fasheh 	if (!ret)
2127d24bec3aSMark Fasheh 		++found_refs;
2128d24bec3aSMark Fasheh 	else if (ret != -ENOENT)
2129d24bec3aSMark Fasheh 		return ret;
2130d24bec3aSMark Fasheh 
2131d24bec3aSMark Fasheh 	ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
2132d24bec3aSMark Fasheh 	if (ret == -ENOENT && found_refs)
2133d24bec3aSMark Fasheh 		return 0;
2134d24bec3aSMark Fasheh 
2135d24bec3aSMark Fasheh 	return ret;
2136d24bec3aSMark Fasheh }
2137d24bec3aSMark Fasheh 
2138a542ad1bSJan Schmidt /*
2139a542ad1bSJan Schmidt  * returns 0 if the path could be dumped (probably truncated)
2140a542ad1bSJan Schmidt  * returns <0 in case of an error
2141a542ad1bSJan Schmidt  */
2142d24bec3aSMark Fasheh static int inode_to_path(u64 inum, u32 name_len, unsigned long name_off,
2143a542ad1bSJan Schmidt 			 struct extent_buffer *eb, void *ctx)
2144a542ad1bSJan Schmidt {
2145a542ad1bSJan Schmidt 	struct inode_fs_paths *ipath = ctx;
2146a542ad1bSJan Schmidt 	char *fspath;
2147a542ad1bSJan Schmidt 	char *fspath_min;
2148a542ad1bSJan Schmidt 	int i = ipath->fspath->elem_cnt;
2149a542ad1bSJan Schmidt 	const int s_ptr = sizeof(char *);
2150a542ad1bSJan Schmidt 	u32 bytes_left;
2151a542ad1bSJan Schmidt 
2152a542ad1bSJan Schmidt 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
2153a542ad1bSJan Schmidt 					ipath->fspath->bytes_left - s_ptr : 0;
2154a542ad1bSJan Schmidt 
2155740c3d22SChris Mason 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
215696b5bd77SJan Schmidt 	fspath = btrfs_ref_to_path(ipath->fs_root, ipath->btrfs_path, name_len,
215796b5bd77SJan Schmidt 				   name_off, eb, inum, fspath_min, bytes_left);
2158a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
2159a542ad1bSJan Schmidt 		return PTR_ERR(fspath);
2160a542ad1bSJan Schmidt 
2161a542ad1bSJan Schmidt 	if (fspath > fspath_min) {
2162745c4d8eSJeff Mahoney 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
2163a542ad1bSJan Schmidt 		++ipath->fspath->elem_cnt;
2164a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = fspath - fspath_min;
2165a542ad1bSJan Schmidt 	} else {
2166a542ad1bSJan Schmidt 		++ipath->fspath->elem_missed;
2167a542ad1bSJan Schmidt 		ipath->fspath->bytes_missing += fspath_min - fspath;
2168a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = 0;
2169a542ad1bSJan Schmidt 	}
2170a542ad1bSJan Schmidt 
2171a542ad1bSJan Schmidt 	return 0;
2172a542ad1bSJan Schmidt }
2173a542ad1bSJan Schmidt 
2174a542ad1bSJan Schmidt /*
2175a542ad1bSJan Schmidt  * this dumps all file system paths to the inode into the ipath struct, provided
2176a542ad1bSJan Schmidt  * is has been created large enough. each path is zero-terminated and accessed
2177740c3d22SChris Mason  * from ipath->fspath->val[i].
2178a542ad1bSJan Schmidt  * when it returns, there are ipath->fspath->elem_cnt number of paths available
2179740c3d22SChris Mason  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
218001327610SNicholas D Steeves  * number of missed paths is recorded in ipath->fspath->elem_missed, otherwise,
2181a542ad1bSJan Schmidt  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
2182a542ad1bSJan Schmidt  * have been needed to return all paths.
2183a542ad1bSJan Schmidt  */
2184a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
2185a542ad1bSJan Schmidt {
2186a542ad1bSJan Schmidt 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
2187a542ad1bSJan Schmidt 			     inode_to_path, ipath);
2188a542ad1bSJan Schmidt }
2189a542ad1bSJan Schmidt 
2190a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes)
2191a542ad1bSJan Schmidt {
2192a542ad1bSJan Schmidt 	struct btrfs_data_container *data;
2193a542ad1bSJan Schmidt 	size_t alloc_bytes;
2194a542ad1bSJan Schmidt 
2195a542ad1bSJan Schmidt 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
2196f54de068SDavid Sterba 	data = kvmalloc(alloc_bytes, GFP_KERNEL);
2197a542ad1bSJan Schmidt 	if (!data)
2198a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2199a542ad1bSJan Schmidt 
2200a542ad1bSJan Schmidt 	if (total_bytes >= sizeof(*data)) {
2201a542ad1bSJan Schmidt 		data->bytes_left = total_bytes - sizeof(*data);
2202a542ad1bSJan Schmidt 		data->bytes_missing = 0;
2203a542ad1bSJan Schmidt 	} else {
2204a542ad1bSJan Schmidt 		data->bytes_missing = sizeof(*data) - total_bytes;
2205a542ad1bSJan Schmidt 		data->bytes_left = 0;
2206a542ad1bSJan Schmidt 	}
2207a542ad1bSJan Schmidt 
2208a542ad1bSJan Schmidt 	data->elem_cnt = 0;
2209a542ad1bSJan Schmidt 	data->elem_missed = 0;
2210a542ad1bSJan Schmidt 
2211a542ad1bSJan Schmidt 	return data;
2212a542ad1bSJan Schmidt }
2213a542ad1bSJan Schmidt 
2214a542ad1bSJan Schmidt /*
2215a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
2216a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
2217a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
2218a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
2219a542ad1bSJan Schmidt  */
2220a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
2221a542ad1bSJan Schmidt 					struct btrfs_path *path)
2222a542ad1bSJan Schmidt {
2223a542ad1bSJan Schmidt 	struct inode_fs_paths *ifp;
2224a542ad1bSJan Schmidt 	struct btrfs_data_container *fspath;
2225a542ad1bSJan Schmidt 
2226a542ad1bSJan Schmidt 	fspath = init_data_container(total_bytes);
2227a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
2228a542ad1bSJan Schmidt 		return (void *)fspath;
2229a542ad1bSJan Schmidt 
2230f54de068SDavid Sterba 	ifp = kmalloc(sizeof(*ifp), GFP_KERNEL);
2231a542ad1bSJan Schmidt 	if (!ifp) {
2232f54de068SDavid Sterba 		kvfree(fspath);
2233a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
2234a542ad1bSJan Schmidt 	}
2235a542ad1bSJan Schmidt 
2236a542ad1bSJan Schmidt 	ifp->btrfs_path = path;
2237a542ad1bSJan Schmidt 	ifp->fspath = fspath;
2238a542ad1bSJan Schmidt 	ifp->fs_root = fs_root;
2239a542ad1bSJan Schmidt 
2240a542ad1bSJan Schmidt 	return ifp;
2241a542ad1bSJan Schmidt }
2242a542ad1bSJan Schmidt 
2243a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath)
2244a542ad1bSJan Schmidt {
22454735fb28SJesper Juhl 	if (!ipath)
22464735fb28SJesper Juhl 		return;
2247f54de068SDavid Sterba 	kvfree(ipath->fspath);
2248a542ad1bSJan Schmidt 	kfree(ipath);
2249a542ad1bSJan Schmidt }
2250