xref: /openbmc/linux/fs/btrfs/backref.c (revision d3b010640e5c59b98d3b11229ba4cc2838dc7cbf)
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 
19a542ad1bSJan Schmidt #include "ctree.h"
20a542ad1bSJan Schmidt #include "disk-io.h"
21a542ad1bSJan Schmidt #include "backref.h"
228da6d581SJan Schmidt #include "ulist.h"
238da6d581SJan Schmidt #include "transaction.h"
248da6d581SJan Schmidt #include "delayed-ref.h"
25a542ad1bSJan Schmidt 
268da6d581SJan Schmidt /*
278da6d581SJan Schmidt  * this structure records all encountered refs on the way up to the root
288da6d581SJan Schmidt  */
298da6d581SJan Schmidt struct __prelim_ref {
308da6d581SJan Schmidt 	struct list_head list;
318da6d581SJan Schmidt 	u64 root_id;
328da6d581SJan Schmidt 	struct btrfs_key key;
338da6d581SJan Schmidt 	int level;
348da6d581SJan Schmidt 	int count;
358da6d581SJan Schmidt 	u64 parent;
368da6d581SJan Schmidt 	u64 wanted_disk_byte;
378da6d581SJan Schmidt };
388da6d581SJan Schmidt 
398da6d581SJan Schmidt static int __add_prelim_ref(struct list_head *head, u64 root_id,
408da6d581SJan Schmidt 			    struct btrfs_key *key, int level, u64 parent,
418da6d581SJan Schmidt 			    u64 wanted_disk_byte, int count)
428da6d581SJan Schmidt {
438da6d581SJan Schmidt 	struct __prelim_ref *ref;
448da6d581SJan Schmidt 
458da6d581SJan Schmidt 	/* in case we're adding delayed refs, we're holding the refs spinlock */
468da6d581SJan Schmidt 	ref = kmalloc(sizeof(*ref), GFP_ATOMIC);
478da6d581SJan Schmidt 	if (!ref)
488da6d581SJan Schmidt 		return -ENOMEM;
498da6d581SJan Schmidt 
508da6d581SJan Schmidt 	ref->root_id = root_id;
518da6d581SJan Schmidt 	if (key)
528da6d581SJan Schmidt 		ref->key = *key;
538da6d581SJan Schmidt 	else
548da6d581SJan Schmidt 		memset(&ref->key, 0, sizeof(ref->key));
558da6d581SJan Schmidt 
568da6d581SJan Schmidt 	ref->level = level;
578da6d581SJan Schmidt 	ref->count = count;
588da6d581SJan Schmidt 	ref->parent = parent;
598da6d581SJan Schmidt 	ref->wanted_disk_byte = wanted_disk_byte;
608da6d581SJan Schmidt 	list_add_tail(&ref->list, head);
618da6d581SJan Schmidt 
628da6d581SJan Schmidt 	return 0;
638da6d581SJan Schmidt }
648da6d581SJan Schmidt 
658da6d581SJan Schmidt static int add_all_parents(struct btrfs_root *root, struct btrfs_path *path,
668da6d581SJan Schmidt 				struct ulist *parents,
678da6d581SJan Schmidt 				struct extent_buffer *eb, int level,
688da6d581SJan Schmidt 				u64 wanted_objectid, u64 wanted_disk_byte)
698da6d581SJan Schmidt {
708da6d581SJan Schmidt 	int ret;
718da6d581SJan Schmidt 	int slot;
728da6d581SJan Schmidt 	struct btrfs_file_extent_item *fi;
738da6d581SJan Schmidt 	struct btrfs_key key;
748da6d581SJan Schmidt 	u64 disk_byte;
758da6d581SJan Schmidt 
768da6d581SJan Schmidt add_parent:
778da6d581SJan Schmidt 	ret = ulist_add(parents, eb->start, 0, GFP_NOFS);
788da6d581SJan Schmidt 	if (ret < 0)
798da6d581SJan Schmidt 		return ret;
808da6d581SJan Schmidt 
818da6d581SJan Schmidt 	if (level != 0)
828da6d581SJan Schmidt 		return 0;
838da6d581SJan Schmidt 
848da6d581SJan Schmidt 	/*
858da6d581SJan Schmidt 	 * if the current leaf is full with EXTENT_DATA items, we must
868da6d581SJan Schmidt 	 * check the next one if that holds a reference as well.
878da6d581SJan Schmidt 	 * ref->count cannot be used to skip this check.
888da6d581SJan Schmidt 	 * repeat this until we don't find any additional EXTENT_DATA items.
898da6d581SJan Schmidt 	 */
908da6d581SJan Schmidt 	while (1) {
918da6d581SJan Schmidt 		ret = btrfs_next_leaf(root, path);
928da6d581SJan Schmidt 		if (ret < 0)
938da6d581SJan Schmidt 			return ret;
948da6d581SJan Schmidt 		if (ret)
958da6d581SJan Schmidt 			return 0;
968da6d581SJan Schmidt 
978da6d581SJan Schmidt 		eb = path->nodes[0];
988da6d581SJan Schmidt 		for (slot = 0; slot < btrfs_header_nritems(eb); ++slot) {
998da6d581SJan Schmidt 			btrfs_item_key_to_cpu(eb, &key, slot);
1008da6d581SJan Schmidt 			if (key.objectid != wanted_objectid ||
1018da6d581SJan Schmidt 			    key.type != BTRFS_EXTENT_DATA_KEY)
1028da6d581SJan Schmidt 				return 0;
1038da6d581SJan Schmidt 			fi = btrfs_item_ptr(eb, slot,
1048da6d581SJan Schmidt 						struct btrfs_file_extent_item);
1058da6d581SJan Schmidt 			disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1068da6d581SJan Schmidt 			if (disk_byte == wanted_disk_byte)
1078da6d581SJan Schmidt 				goto add_parent;
1088da6d581SJan Schmidt 		}
1098da6d581SJan Schmidt 	}
1108da6d581SJan Schmidt 
1118da6d581SJan Schmidt 	return 0;
1128da6d581SJan Schmidt }
1138da6d581SJan Schmidt 
1148da6d581SJan Schmidt /*
1158da6d581SJan Schmidt  * resolve an indirect backref in the form (root_id, key, level)
1168da6d581SJan Schmidt  * to a logical address
1178da6d581SJan Schmidt  */
1188da6d581SJan Schmidt static int __resolve_indirect_ref(struct btrfs_fs_info *fs_info,
1198da6d581SJan Schmidt 					struct __prelim_ref *ref,
1208da6d581SJan Schmidt 					struct ulist *parents)
1218da6d581SJan Schmidt {
1228da6d581SJan Schmidt 	struct btrfs_path *path;
1238da6d581SJan Schmidt 	struct btrfs_root *root;
1248da6d581SJan Schmidt 	struct btrfs_key root_key;
1258da6d581SJan Schmidt 	struct btrfs_key key = {0};
1268da6d581SJan Schmidt 	struct extent_buffer *eb;
1278da6d581SJan Schmidt 	int ret = 0;
1288da6d581SJan Schmidt 	int root_level;
1298da6d581SJan Schmidt 	int level = ref->level;
1308da6d581SJan Schmidt 
1318da6d581SJan Schmidt 	path = btrfs_alloc_path();
1328da6d581SJan Schmidt 	if (!path)
1338da6d581SJan Schmidt 		return -ENOMEM;
1348da6d581SJan Schmidt 
1358da6d581SJan Schmidt 	root_key.objectid = ref->root_id;
1368da6d581SJan Schmidt 	root_key.type = BTRFS_ROOT_ITEM_KEY;
1378da6d581SJan Schmidt 	root_key.offset = (u64)-1;
1388da6d581SJan Schmidt 	root = btrfs_read_fs_root_no_name(fs_info, &root_key);
1398da6d581SJan Schmidt 	if (IS_ERR(root)) {
1408da6d581SJan Schmidt 		ret = PTR_ERR(root);
1418da6d581SJan Schmidt 		goto out;
1428da6d581SJan Schmidt 	}
1438da6d581SJan Schmidt 
1448da6d581SJan Schmidt 	rcu_read_lock();
1458da6d581SJan Schmidt 	root_level = btrfs_header_level(root->node);
1468da6d581SJan Schmidt 	rcu_read_unlock();
1478da6d581SJan Schmidt 
1488da6d581SJan Schmidt 	if (root_level + 1 == level)
1498da6d581SJan Schmidt 		goto out;
1508da6d581SJan Schmidt 
1518da6d581SJan Schmidt 	path->lowest_level = level;
1528da6d581SJan Schmidt 	ret = btrfs_search_slot(NULL, root, &ref->key, path, 0, 0);
1538da6d581SJan Schmidt 	pr_debug("search slot in root %llu (level %d, ref count %d) returned "
1548da6d581SJan Schmidt 		 "%d for key (%llu %u %llu)\n",
1558da6d581SJan Schmidt 		 (unsigned long long)ref->root_id, level, ref->count, ret,
1568da6d581SJan Schmidt 		 (unsigned long long)ref->key.objectid, ref->key.type,
1578da6d581SJan Schmidt 		 (unsigned long long)ref->key.offset);
1588da6d581SJan Schmidt 	if (ret < 0)
1598da6d581SJan Schmidt 		goto out;
1608da6d581SJan Schmidt 
1618da6d581SJan Schmidt 	eb = path->nodes[level];
1628da6d581SJan Schmidt 	if (!eb) {
1638da6d581SJan Schmidt 		WARN_ON(1);
1648da6d581SJan Schmidt 		ret = 1;
1658da6d581SJan Schmidt 		goto out;
1668da6d581SJan Schmidt 	}
1678da6d581SJan Schmidt 
1688da6d581SJan Schmidt 	if (level == 0) {
1698da6d581SJan Schmidt 		if (ret == 1 && path->slots[0] >= btrfs_header_nritems(eb)) {
1708da6d581SJan Schmidt 			ret = btrfs_next_leaf(root, path);
1718da6d581SJan Schmidt 			if (ret)
1728da6d581SJan Schmidt 				goto out;
1738da6d581SJan Schmidt 			eb = path->nodes[0];
1748da6d581SJan Schmidt 		}
1758da6d581SJan Schmidt 
1768da6d581SJan Schmidt 		btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
1778da6d581SJan Schmidt 	}
1788da6d581SJan Schmidt 
1798da6d581SJan Schmidt 	/* the last two parameters will only be used for level == 0 */
1808da6d581SJan Schmidt 	ret = add_all_parents(root, path, parents, eb, level, key.objectid,
1818da6d581SJan Schmidt 				ref->wanted_disk_byte);
1828da6d581SJan Schmidt out:
1838da6d581SJan Schmidt 	btrfs_free_path(path);
1848da6d581SJan Schmidt 	return ret;
1858da6d581SJan Schmidt }
1868da6d581SJan Schmidt 
1878da6d581SJan Schmidt /*
1888da6d581SJan Schmidt  * resolve all indirect backrefs from the list
1898da6d581SJan Schmidt  */
1908da6d581SJan Schmidt static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
1918da6d581SJan Schmidt 				   struct list_head *head)
1928da6d581SJan Schmidt {
1938da6d581SJan Schmidt 	int err;
1948da6d581SJan Schmidt 	int ret = 0;
1958da6d581SJan Schmidt 	struct __prelim_ref *ref;
1968da6d581SJan Schmidt 	struct __prelim_ref *ref_safe;
1978da6d581SJan Schmidt 	struct __prelim_ref *new_ref;
1988da6d581SJan Schmidt 	struct ulist *parents;
1998da6d581SJan Schmidt 	struct ulist_node *node;
2008da6d581SJan Schmidt 
2018da6d581SJan Schmidt 	parents = ulist_alloc(GFP_NOFS);
2028da6d581SJan Schmidt 	if (!parents)
2038da6d581SJan Schmidt 		return -ENOMEM;
2048da6d581SJan Schmidt 
2058da6d581SJan Schmidt 	/*
2068da6d581SJan Schmidt 	 * _safe allows us to insert directly after the current item without
2078da6d581SJan Schmidt 	 * iterating over the newly inserted items.
2088da6d581SJan Schmidt 	 * we're also allowed to re-assign ref during iteration.
2098da6d581SJan Schmidt 	 */
2108da6d581SJan Schmidt 	list_for_each_entry_safe(ref, ref_safe, head, list) {
2118da6d581SJan Schmidt 		if (ref->parent)	/* already direct */
2128da6d581SJan Schmidt 			continue;
2138da6d581SJan Schmidt 		if (ref->count == 0)
2148da6d581SJan Schmidt 			continue;
2158da6d581SJan Schmidt 		err = __resolve_indirect_ref(fs_info, ref, parents);
2168da6d581SJan Schmidt 		if (err) {
2178da6d581SJan Schmidt 			if (ret == 0)
2188da6d581SJan Schmidt 				ret = err;
2198da6d581SJan Schmidt 			continue;
2208da6d581SJan Schmidt 		}
2218da6d581SJan Schmidt 
2228da6d581SJan Schmidt 		/* we put the first parent into the ref at hand */
2238da6d581SJan Schmidt 		node = ulist_next(parents, NULL);
2248da6d581SJan Schmidt 		ref->parent = node ? node->val : 0;
2258da6d581SJan Schmidt 
2268da6d581SJan Schmidt 		/* additional parents require new refs being added here */
2278da6d581SJan Schmidt 		while ((node = ulist_next(parents, node))) {
2288da6d581SJan Schmidt 			new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS);
2298da6d581SJan Schmidt 			if (!new_ref) {
2308da6d581SJan Schmidt 				ret = -ENOMEM;
2318da6d581SJan Schmidt 				break;
2328da6d581SJan Schmidt 			}
2338da6d581SJan Schmidt 			memcpy(new_ref, ref, sizeof(*ref));
2348da6d581SJan Schmidt 			new_ref->parent = node->val;
2358da6d581SJan Schmidt 			list_add(&new_ref->list, &ref->list);
2368da6d581SJan Schmidt 		}
2378da6d581SJan Schmidt 		ulist_reinit(parents);
2388da6d581SJan Schmidt 	}
2398da6d581SJan Schmidt 
2408da6d581SJan Schmidt 	ulist_free(parents);
2418da6d581SJan Schmidt 	return ret;
2428da6d581SJan Schmidt }
2438da6d581SJan Schmidt 
2448da6d581SJan Schmidt /*
2458da6d581SJan Schmidt  * merge two lists of backrefs and adjust counts accordingly
2468da6d581SJan Schmidt  *
2478da6d581SJan Schmidt  * mode = 1: merge identical keys, if key is set
2488da6d581SJan Schmidt  * mode = 2: merge identical parents
2498da6d581SJan Schmidt  */
2508da6d581SJan Schmidt static int __merge_refs(struct list_head *head, int mode)
2518da6d581SJan Schmidt {
2528da6d581SJan Schmidt 	struct list_head *pos1;
2538da6d581SJan Schmidt 
2548da6d581SJan Schmidt 	list_for_each(pos1, head) {
2558da6d581SJan Schmidt 		struct list_head *n2;
2568da6d581SJan Schmidt 		struct list_head *pos2;
2578da6d581SJan Schmidt 		struct __prelim_ref *ref1;
2588da6d581SJan Schmidt 
2598da6d581SJan Schmidt 		ref1 = list_entry(pos1, struct __prelim_ref, list);
2608da6d581SJan Schmidt 
2618da6d581SJan Schmidt 		if (mode == 1 && ref1->key.type == 0)
2628da6d581SJan Schmidt 			continue;
2638da6d581SJan Schmidt 		for (pos2 = pos1->next, n2 = pos2->next; pos2 != head;
2648da6d581SJan Schmidt 		     pos2 = n2, n2 = pos2->next) {
2658da6d581SJan Schmidt 			struct __prelim_ref *ref2;
2668da6d581SJan Schmidt 
2678da6d581SJan Schmidt 			ref2 = list_entry(pos2, struct __prelim_ref, list);
2688da6d581SJan Schmidt 
2698da6d581SJan Schmidt 			if (mode == 1) {
2708da6d581SJan Schmidt 				if (memcmp(&ref1->key, &ref2->key,
2718da6d581SJan Schmidt 					   sizeof(ref1->key)) ||
2728da6d581SJan Schmidt 				    ref1->level != ref2->level ||
2738da6d581SJan Schmidt 				    ref1->root_id != ref2->root_id)
2748da6d581SJan Schmidt 					continue;
2758da6d581SJan Schmidt 				ref1->count += ref2->count;
2768da6d581SJan Schmidt 			} else {
2778da6d581SJan Schmidt 				if (ref1->parent != ref2->parent)
2788da6d581SJan Schmidt 					continue;
2798da6d581SJan Schmidt 				ref1->count += ref2->count;
2808da6d581SJan Schmidt 			}
2818da6d581SJan Schmidt 			list_del(&ref2->list);
2828da6d581SJan Schmidt 			kfree(ref2);
2838da6d581SJan Schmidt 		}
2848da6d581SJan Schmidt 
2858da6d581SJan Schmidt 	}
2868da6d581SJan Schmidt 	return 0;
2878da6d581SJan Schmidt }
2888da6d581SJan Schmidt 
2898da6d581SJan Schmidt /*
2908da6d581SJan Schmidt  * add all currently queued delayed refs from this head whose seq nr is
2918da6d581SJan Schmidt  * smaller or equal that seq to the list
2928da6d581SJan Schmidt  */
2938da6d581SJan Schmidt static int __add_delayed_refs(struct btrfs_delayed_ref_head *head, u64 seq,
2948da6d581SJan Schmidt 			      struct btrfs_key *info_key,
2958da6d581SJan Schmidt 			      struct list_head *prefs)
2968da6d581SJan Schmidt {
2978da6d581SJan Schmidt 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2988da6d581SJan Schmidt 	struct rb_node *n = &head->node.rb_node;
2998da6d581SJan Schmidt 	int sgn;
300b1375d64SJan Schmidt 	int ret = 0;
3018da6d581SJan Schmidt 
3028da6d581SJan Schmidt 	if (extent_op && extent_op->update_key)
3038da6d581SJan Schmidt 		btrfs_disk_key_to_cpu(info_key, &extent_op->key);
3048da6d581SJan Schmidt 
3058da6d581SJan Schmidt 	while ((n = rb_prev(n))) {
3068da6d581SJan Schmidt 		struct btrfs_delayed_ref_node *node;
3078da6d581SJan Schmidt 		node = rb_entry(n, struct btrfs_delayed_ref_node,
3088da6d581SJan Schmidt 				rb_node);
3098da6d581SJan Schmidt 		if (node->bytenr != head->node.bytenr)
3108da6d581SJan Schmidt 			break;
3118da6d581SJan Schmidt 		WARN_ON(node->is_head);
3128da6d581SJan Schmidt 
3138da6d581SJan Schmidt 		if (node->seq > seq)
3148da6d581SJan Schmidt 			continue;
3158da6d581SJan Schmidt 
3168da6d581SJan Schmidt 		switch (node->action) {
3178da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_EXTENT:
3188da6d581SJan Schmidt 		case BTRFS_UPDATE_DELAYED_HEAD:
3198da6d581SJan Schmidt 			WARN_ON(1);
3208da6d581SJan Schmidt 			continue;
3218da6d581SJan Schmidt 		case BTRFS_ADD_DELAYED_REF:
3228da6d581SJan Schmidt 			sgn = 1;
3238da6d581SJan Schmidt 			break;
3248da6d581SJan Schmidt 		case BTRFS_DROP_DELAYED_REF:
3258da6d581SJan Schmidt 			sgn = -1;
3268da6d581SJan Schmidt 			break;
3278da6d581SJan Schmidt 		default:
3288da6d581SJan Schmidt 			BUG_ON(1);
3298da6d581SJan Schmidt 		}
3308da6d581SJan Schmidt 		switch (node->type) {
3318da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY: {
3328da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
3338da6d581SJan Schmidt 
3348da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
3358da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, info_key,
3368da6d581SJan Schmidt 					       ref->level + 1, 0, node->bytenr,
3378da6d581SJan Schmidt 					       node->ref_mod * sgn);
3388da6d581SJan Schmidt 			break;
3398da6d581SJan Schmidt 		}
3408da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY: {
3418da6d581SJan Schmidt 			struct btrfs_delayed_tree_ref *ref;
3428da6d581SJan Schmidt 
3438da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_tree_ref(node);
3448da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, info_key,
3458da6d581SJan Schmidt 					       ref->level + 1, ref->parent,
3468da6d581SJan Schmidt 					       node->bytenr,
3478da6d581SJan Schmidt 					       node->ref_mod * sgn);
3488da6d581SJan Schmidt 			break;
3498da6d581SJan Schmidt 		}
3508da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
3518da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
3528da6d581SJan Schmidt 			struct btrfs_key key;
3538da6d581SJan Schmidt 
3548da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
3558da6d581SJan Schmidt 
3568da6d581SJan Schmidt 			key.objectid = ref->objectid;
3578da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
3588da6d581SJan Schmidt 			key.offset = ref->offset;
3598da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &key, 0, 0,
3608da6d581SJan Schmidt 					       node->bytenr,
3618da6d581SJan Schmidt 					       node->ref_mod * sgn);
3628da6d581SJan Schmidt 			break;
3638da6d581SJan Schmidt 		}
3648da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
3658da6d581SJan Schmidt 			struct btrfs_delayed_data_ref *ref;
3668da6d581SJan Schmidt 			struct btrfs_key key;
3678da6d581SJan Schmidt 
3688da6d581SJan Schmidt 			ref = btrfs_delayed_node_to_data_ref(node);
3698da6d581SJan Schmidt 
3708da6d581SJan Schmidt 			key.objectid = ref->objectid;
3718da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
3728da6d581SJan Schmidt 			key.offset = ref->offset;
3738da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, ref->root, &key, 0,
3748da6d581SJan Schmidt 					       ref->parent, node->bytenr,
3758da6d581SJan Schmidt 					       node->ref_mod * sgn);
3768da6d581SJan Schmidt 			break;
3778da6d581SJan Schmidt 		}
3788da6d581SJan Schmidt 		default:
3798da6d581SJan Schmidt 			WARN_ON(1);
3808da6d581SJan Schmidt 		}
3818da6d581SJan Schmidt 		BUG_ON(ret);
3828da6d581SJan Schmidt 	}
3838da6d581SJan Schmidt 
3848da6d581SJan Schmidt 	return 0;
3858da6d581SJan Schmidt }
3868da6d581SJan Schmidt 
3878da6d581SJan Schmidt /*
3888da6d581SJan Schmidt  * add all inline backrefs for bytenr to the list
3898da6d581SJan Schmidt  */
3908da6d581SJan Schmidt static int __add_inline_refs(struct btrfs_fs_info *fs_info,
3918da6d581SJan Schmidt 			     struct btrfs_path *path, u64 bytenr,
3928da6d581SJan Schmidt 			     struct btrfs_key *info_key, int *info_level,
3938da6d581SJan Schmidt 			     struct list_head *prefs)
3948da6d581SJan Schmidt {
395b1375d64SJan Schmidt 	int ret = 0;
3968da6d581SJan Schmidt 	int slot;
3978da6d581SJan Schmidt 	struct extent_buffer *leaf;
3988da6d581SJan Schmidt 	struct btrfs_key key;
3998da6d581SJan Schmidt 	unsigned long ptr;
4008da6d581SJan Schmidt 	unsigned long end;
4018da6d581SJan Schmidt 	struct btrfs_extent_item *ei;
4028da6d581SJan Schmidt 	u64 flags;
4038da6d581SJan Schmidt 	u64 item_size;
4048da6d581SJan Schmidt 
4058da6d581SJan Schmidt 	/*
4068da6d581SJan Schmidt 	 * enumerate all inline refs
4078da6d581SJan Schmidt 	 */
4088da6d581SJan Schmidt 	leaf = path->nodes[0];
4098da6d581SJan Schmidt 	slot = path->slots[0] - 1;
4108da6d581SJan Schmidt 
4118da6d581SJan Schmidt 	item_size = btrfs_item_size_nr(leaf, slot);
4128da6d581SJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
4138da6d581SJan Schmidt 
4148da6d581SJan Schmidt 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
4158da6d581SJan Schmidt 	flags = btrfs_extent_flags(leaf, ei);
4168da6d581SJan Schmidt 
4178da6d581SJan Schmidt 	ptr = (unsigned long)(ei + 1);
4188da6d581SJan Schmidt 	end = (unsigned long)ei + item_size;
4198da6d581SJan Schmidt 
4208da6d581SJan Schmidt 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
4218da6d581SJan Schmidt 		struct btrfs_tree_block_info *info;
4228da6d581SJan Schmidt 		struct btrfs_disk_key disk_key;
4238da6d581SJan Schmidt 
4248da6d581SJan Schmidt 		info = (struct btrfs_tree_block_info *)ptr;
4258da6d581SJan Schmidt 		*info_level = btrfs_tree_block_level(leaf, info);
4268da6d581SJan Schmidt 		btrfs_tree_block_key(leaf, info, &disk_key);
4278da6d581SJan Schmidt 		btrfs_disk_key_to_cpu(info_key, &disk_key);
4288da6d581SJan Schmidt 		ptr += sizeof(struct btrfs_tree_block_info);
4298da6d581SJan Schmidt 		BUG_ON(ptr > end);
4308da6d581SJan Schmidt 	} else {
4318da6d581SJan Schmidt 		BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
4328da6d581SJan Schmidt 	}
4338da6d581SJan Schmidt 
4348da6d581SJan Schmidt 	while (ptr < end) {
4358da6d581SJan Schmidt 		struct btrfs_extent_inline_ref *iref;
4368da6d581SJan Schmidt 		u64 offset;
4378da6d581SJan Schmidt 		int type;
4388da6d581SJan Schmidt 
4398da6d581SJan Schmidt 		iref = (struct btrfs_extent_inline_ref *)ptr;
4408da6d581SJan Schmidt 		type = btrfs_extent_inline_ref_type(leaf, iref);
4418da6d581SJan Schmidt 		offset = btrfs_extent_inline_ref_offset(leaf, iref);
4428da6d581SJan Schmidt 
4438da6d581SJan Schmidt 		switch (type) {
4448da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
4458da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, info_key,
4468da6d581SJan Schmidt 						*info_level + 1, offset,
4478da6d581SJan Schmidt 						bytenr, 1);
4488da6d581SJan Schmidt 			break;
4498da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
4508da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
4518da6d581SJan Schmidt 			int count;
4528da6d581SJan Schmidt 
4538da6d581SJan Schmidt 			sdref = (struct btrfs_shared_data_ref *)(iref + 1);
4548da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
4558da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, offset,
4568da6d581SJan Schmidt 					       bytenr, count);
4578da6d581SJan Schmidt 			break;
4588da6d581SJan Schmidt 		}
4598da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
4608da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, offset, info_key,
4618da6d581SJan Schmidt 					       *info_level + 1, 0, bytenr, 1);
4628da6d581SJan Schmidt 			break;
4638da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
4648da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
4658da6d581SJan Schmidt 			int count;
4668da6d581SJan Schmidt 			u64 root;
4678da6d581SJan Schmidt 
4688da6d581SJan Schmidt 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
4698da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
4708da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
4718da6d581SJan Schmidt 								      dref);
4728da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
4738da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
4748da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
4758da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0, bytenr,
4768da6d581SJan Schmidt 						count);
4778da6d581SJan Schmidt 			break;
4788da6d581SJan Schmidt 		}
4798da6d581SJan Schmidt 		default:
4808da6d581SJan Schmidt 			WARN_ON(1);
4818da6d581SJan Schmidt 		}
4828da6d581SJan Schmidt 		BUG_ON(ret);
4838da6d581SJan Schmidt 		ptr += btrfs_extent_inline_ref_size(type);
4848da6d581SJan Schmidt 	}
4858da6d581SJan Schmidt 
4868da6d581SJan Schmidt 	return 0;
4878da6d581SJan Schmidt }
4888da6d581SJan Schmidt 
4898da6d581SJan Schmidt /*
4908da6d581SJan Schmidt  * add all non-inline backrefs for bytenr to the list
4918da6d581SJan Schmidt  */
4928da6d581SJan Schmidt static int __add_keyed_refs(struct btrfs_fs_info *fs_info,
4938da6d581SJan Schmidt 			    struct btrfs_path *path, u64 bytenr,
4948da6d581SJan Schmidt 			    struct btrfs_key *info_key, int info_level,
4958da6d581SJan Schmidt 			    struct list_head *prefs)
4968da6d581SJan Schmidt {
4978da6d581SJan Schmidt 	struct btrfs_root *extent_root = fs_info->extent_root;
4988da6d581SJan Schmidt 	int ret;
4998da6d581SJan Schmidt 	int slot;
5008da6d581SJan Schmidt 	struct extent_buffer *leaf;
5018da6d581SJan Schmidt 	struct btrfs_key key;
5028da6d581SJan Schmidt 
5038da6d581SJan Schmidt 	while (1) {
5048da6d581SJan Schmidt 		ret = btrfs_next_item(extent_root, path);
5058da6d581SJan Schmidt 		if (ret < 0)
5068da6d581SJan Schmidt 			break;
5078da6d581SJan Schmidt 		if (ret) {
5088da6d581SJan Schmidt 			ret = 0;
5098da6d581SJan Schmidt 			break;
5108da6d581SJan Schmidt 		}
5118da6d581SJan Schmidt 
5128da6d581SJan Schmidt 		slot = path->slots[0];
5138da6d581SJan Schmidt 		leaf = path->nodes[0];
5148da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
5158da6d581SJan Schmidt 
5168da6d581SJan Schmidt 		if (key.objectid != bytenr)
5178da6d581SJan Schmidt 			break;
5188da6d581SJan Schmidt 		if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
5198da6d581SJan Schmidt 			continue;
5208da6d581SJan Schmidt 		if (key.type > BTRFS_SHARED_DATA_REF_KEY)
5218da6d581SJan Schmidt 			break;
5228da6d581SJan Schmidt 
5238da6d581SJan Schmidt 		switch (key.type) {
5248da6d581SJan Schmidt 		case BTRFS_SHARED_BLOCK_REF_KEY:
5258da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, info_key,
5268da6d581SJan Schmidt 						info_level + 1, key.offset,
5278da6d581SJan Schmidt 						bytenr, 1);
5288da6d581SJan Schmidt 			break;
5298da6d581SJan Schmidt 		case BTRFS_SHARED_DATA_REF_KEY: {
5308da6d581SJan Schmidt 			struct btrfs_shared_data_ref *sdref;
5318da6d581SJan Schmidt 			int count;
5328da6d581SJan Schmidt 
5338da6d581SJan Schmidt 			sdref = btrfs_item_ptr(leaf, slot,
5348da6d581SJan Schmidt 					      struct btrfs_shared_data_ref);
5358da6d581SJan Schmidt 			count = btrfs_shared_data_ref_count(leaf, sdref);
5368da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, 0, NULL, 0, key.offset,
5378da6d581SJan Schmidt 						bytenr, count);
5388da6d581SJan Schmidt 			break;
5398da6d581SJan Schmidt 		}
5408da6d581SJan Schmidt 		case BTRFS_TREE_BLOCK_REF_KEY:
5418da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, key.offset, info_key,
5428da6d581SJan Schmidt 						info_level + 1, 0, bytenr, 1);
5438da6d581SJan Schmidt 			break;
5448da6d581SJan Schmidt 		case BTRFS_EXTENT_DATA_REF_KEY: {
5458da6d581SJan Schmidt 			struct btrfs_extent_data_ref *dref;
5468da6d581SJan Schmidt 			int count;
5478da6d581SJan Schmidt 			u64 root;
5488da6d581SJan Schmidt 
5498da6d581SJan Schmidt 			dref = btrfs_item_ptr(leaf, slot,
5508da6d581SJan Schmidt 					      struct btrfs_extent_data_ref);
5518da6d581SJan Schmidt 			count = btrfs_extent_data_ref_count(leaf, dref);
5528da6d581SJan Schmidt 			key.objectid = btrfs_extent_data_ref_objectid(leaf,
5538da6d581SJan Schmidt 								      dref);
5548da6d581SJan Schmidt 			key.type = BTRFS_EXTENT_DATA_KEY;
5558da6d581SJan Schmidt 			key.offset = btrfs_extent_data_ref_offset(leaf, dref);
5568da6d581SJan Schmidt 			root = btrfs_extent_data_ref_root(leaf, dref);
5578da6d581SJan Schmidt 			ret = __add_prelim_ref(prefs, root, &key, 0, 0,
5588da6d581SJan Schmidt 						bytenr, count);
5598da6d581SJan Schmidt 			break;
5608da6d581SJan Schmidt 		}
5618da6d581SJan Schmidt 		default:
5628da6d581SJan Schmidt 			WARN_ON(1);
5638da6d581SJan Schmidt 		}
5648da6d581SJan Schmidt 		BUG_ON(ret);
5658da6d581SJan Schmidt 	}
5668da6d581SJan Schmidt 
5678da6d581SJan Schmidt 	return ret;
5688da6d581SJan Schmidt }
5698da6d581SJan Schmidt 
5708da6d581SJan Schmidt /*
5718da6d581SJan Schmidt  * this adds all existing backrefs (inline backrefs, backrefs and delayed
5728da6d581SJan Schmidt  * refs) for the given bytenr to the refs list, merges duplicates and resolves
5738da6d581SJan Schmidt  * indirect refs to their parent bytenr.
5748da6d581SJan Schmidt  * When roots are found, they're added to the roots list
5758da6d581SJan Schmidt  *
5768da6d581SJan Schmidt  * FIXME some caching might speed things up
5778da6d581SJan Schmidt  */
5788da6d581SJan Schmidt static int find_parent_nodes(struct btrfs_trans_handle *trans,
5798da6d581SJan Schmidt 			     struct btrfs_fs_info *fs_info, u64 bytenr,
5808da6d581SJan Schmidt 			     u64 seq, struct ulist *refs, struct ulist *roots)
5818da6d581SJan Schmidt {
5828da6d581SJan Schmidt 	struct btrfs_key key;
5838da6d581SJan Schmidt 	struct btrfs_path *path;
5848da6d581SJan Schmidt 	struct btrfs_key info_key = { 0 };
5858da6d581SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs = NULL;
586*d3b01064SLi Zefan 	struct btrfs_delayed_ref_head *head;
5878da6d581SJan Schmidt 	int info_level = 0;
5888da6d581SJan Schmidt 	int ret;
5898da6d581SJan Schmidt 	struct list_head prefs_delayed;
5908da6d581SJan Schmidt 	struct list_head prefs;
5918da6d581SJan Schmidt 	struct __prelim_ref *ref;
5928da6d581SJan Schmidt 
5938da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs);
5948da6d581SJan Schmidt 	INIT_LIST_HEAD(&prefs_delayed);
5958da6d581SJan Schmidt 
5968da6d581SJan Schmidt 	key.objectid = bytenr;
5978da6d581SJan Schmidt 	key.type = BTRFS_EXTENT_ITEM_KEY;
5988da6d581SJan Schmidt 	key.offset = (u64)-1;
5998da6d581SJan Schmidt 
6008da6d581SJan Schmidt 	path = btrfs_alloc_path();
6018da6d581SJan Schmidt 	if (!path)
6028da6d581SJan Schmidt 		return -ENOMEM;
6038da6d581SJan Schmidt 
6048da6d581SJan Schmidt 	/*
6058da6d581SJan Schmidt 	 * grab both a lock on the path and a lock on the delayed ref head.
6068da6d581SJan Schmidt 	 * We need both to get a consistent picture of how the refs look
6078da6d581SJan Schmidt 	 * at a specified point in time
6088da6d581SJan Schmidt 	 */
6098da6d581SJan Schmidt again:
610*d3b01064SLi Zefan 	head = NULL;
611*d3b01064SLi Zefan 
6128da6d581SJan Schmidt 	ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
6138da6d581SJan Schmidt 	if (ret < 0)
6148da6d581SJan Schmidt 		goto out;
6158da6d581SJan Schmidt 	BUG_ON(ret == 0);
6168da6d581SJan Schmidt 
6178da6d581SJan Schmidt 	/*
6188da6d581SJan Schmidt 	 * look if there are updates for this ref queued and lock the head
6198da6d581SJan Schmidt 	 */
6208da6d581SJan Schmidt 	delayed_refs = &trans->transaction->delayed_refs;
6218da6d581SJan Schmidt 	spin_lock(&delayed_refs->lock);
6228da6d581SJan Schmidt 	head = btrfs_find_delayed_ref_head(trans, bytenr);
6238da6d581SJan Schmidt 	if (head) {
6248da6d581SJan Schmidt 		if (!mutex_trylock(&head->mutex)) {
6258da6d581SJan Schmidt 			atomic_inc(&head->node.refs);
6268da6d581SJan Schmidt 			spin_unlock(&delayed_refs->lock);
6278da6d581SJan Schmidt 
6288da6d581SJan Schmidt 			btrfs_release_path(path);
6298da6d581SJan Schmidt 
6308da6d581SJan Schmidt 			/*
6318da6d581SJan Schmidt 			 * Mutex was contended, block until it's
6328da6d581SJan Schmidt 			 * released and try again
6338da6d581SJan Schmidt 			 */
6348da6d581SJan Schmidt 			mutex_lock(&head->mutex);
6358da6d581SJan Schmidt 			mutex_unlock(&head->mutex);
6368da6d581SJan Schmidt 			btrfs_put_delayed_ref(&head->node);
6378da6d581SJan Schmidt 			goto again;
6388da6d581SJan Schmidt 		}
6398da6d581SJan Schmidt 		ret = __add_delayed_refs(head, seq, &info_key, &prefs_delayed);
640*d3b01064SLi Zefan 		if (ret) {
641*d3b01064SLi Zefan 			spin_unlock(&delayed_refs->lock);
6428da6d581SJan Schmidt 			goto out;
6438da6d581SJan Schmidt 		}
644*d3b01064SLi Zefan 	}
6458da6d581SJan Schmidt 	spin_unlock(&delayed_refs->lock);
6468da6d581SJan Schmidt 
6478da6d581SJan Schmidt 	if (path->slots[0]) {
6488da6d581SJan Schmidt 		struct extent_buffer *leaf;
6498da6d581SJan Schmidt 		int slot;
6508da6d581SJan Schmidt 
6518da6d581SJan Schmidt 		leaf = path->nodes[0];
6528da6d581SJan Schmidt 		slot = path->slots[0] - 1;
6538da6d581SJan Schmidt 		btrfs_item_key_to_cpu(leaf, &key, slot);
6548da6d581SJan Schmidt 		if (key.objectid == bytenr &&
6558da6d581SJan Schmidt 		    key.type == BTRFS_EXTENT_ITEM_KEY) {
6568da6d581SJan Schmidt 			ret = __add_inline_refs(fs_info, path, bytenr,
6578da6d581SJan Schmidt 						&info_key, &info_level, &prefs);
6588da6d581SJan Schmidt 			if (ret)
6598da6d581SJan Schmidt 				goto out;
6608da6d581SJan Schmidt 			ret = __add_keyed_refs(fs_info, path, bytenr, &info_key,
6618da6d581SJan Schmidt 					       info_level, &prefs);
6628da6d581SJan Schmidt 			if (ret)
6638da6d581SJan Schmidt 				goto out;
6648da6d581SJan Schmidt 		}
6658da6d581SJan Schmidt 	}
6668da6d581SJan Schmidt 	btrfs_release_path(path);
6678da6d581SJan Schmidt 
6688da6d581SJan Schmidt 	/*
6698da6d581SJan Schmidt 	 * when adding the delayed refs above, the info_key might not have
6708da6d581SJan Schmidt 	 * been known yet. Go over the list and replace the missing keys
6718da6d581SJan Schmidt 	 */
6728da6d581SJan Schmidt 	list_for_each_entry(ref, &prefs_delayed, list) {
6738da6d581SJan Schmidt 		if ((ref->key.offset | ref->key.type | ref->key.objectid) == 0)
6748da6d581SJan Schmidt 			memcpy(&ref->key, &info_key, sizeof(ref->key));
6758da6d581SJan Schmidt 	}
6768da6d581SJan Schmidt 	list_splice_init(&prefs_delayed, &prefs);
6778da6d581SJan Schmidt 
6788da6d581SJan Schmidt 	ret = __merge_refs(&prefs, 1);
6798da6d581SJan Schmidt 	if (ret)
6808da6d581SJan Schmidt 		goto out;
6818da6d581SJan Schmidt 
6828da6d581SJan Schmidt 	ret = __resolve_indirect_refs(fs_info, &prefs);
6838da6d581SJan Schmidt 	if (ret)
6848da6d581SJan Schmidt 		goto out;
6858da6d581SJan Schmidt 
6868da6d581SJan Schmidt 	ret = __merge_refs(&prefs, 2);
6878da6d581SJan Schmidt 	if (ret)
6888da6d581SJan Schmidt 		goto out;
6898da6d581SJan Schmidt 
6908da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
6918da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
6928da6d581SJan Schmidt 		list_del(&ref->list);
6938da6d581SJan Schmidt 		if (ref->count < 0)
6948da6d581SJan Schmidt 			WARN_ON(1);
6958da6d581SJan Schmidt 		if (ref->count && ref->root_id && ref->parent == 0) {
6968da6d581SJan Schmidt 			/* no parent == root of tree */
6978da6d581SJan Schmidt 			ret = ulist_add(roots, ref->root_id, 0, GFP_NOFS);
6988da6d581SJan Schmidt 			BUG_ON(ret < 0);
6998da6d581SJan Schmidt 		}
7008da6d581SJan Schmidt 		if (ref->count && ref->parent) {
7018da6d581SJan Schmidt 			ret = ulist_add(refs, ref->parent, 0, GFP_NOFS);
7028da6d581SJan Schmidt 			BUG_ON(ret < 0);
7038da6d581SJan Schmidt 		}
7048da6d581SJan Schmidt 		kfree(ref);
7058da6d581SJan Schmidt 	}
7068da6d581SJan Schmidt 
7078da6d581SJan Schmidt out:
7088da6d581SJan Schmidt 	if (head)
7098da6d581SJan Schmidt 		mutex_unlock(&head->mutex);
7108da6d581SJan Schmidt 	btrfs_free_path(path);
7118da6d581SJan Schmidt 	while (!list_empty(&prefs)) {
7128da6d581SJan Schmidt 		ref = list_first_entry(&prefs, struct __prelim_ref, list);
7138da6d581SJan Schmidt 		list_del(&ref->list);
7148da6d581SJan Schmidt 		kfree(ref);
7158da6d581SJan Schmidt 	}
7168da6d581SJan Schmidt 	while (!list_empty(&prefs_delayed)) {
7178da6d581SJan Schmidt 		ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
7188da6d581SJan Schmidt 				       list);
7198da6d581SJan Schmidt 		list_del(&ref->list);
7208da6d581SJan Schmidt 		kfree(ref);
7218da6d581SJan Schmidt 	}
7228da6d581SJan Schmidt 
7238da6d581SJan Schmidt 	return ret;
7248da6d581SJan Schmidt }
7258da6d581SJan Schmidt 
7268da6d581SJan Schmidt /*
7278da6d581SJan Schmidt  * Finds all leafs with a reference to the specified combination of bytenr and
7288da6d581SJan Schmidt  * offset. key_list_head will point to a list of corresponding keys (caller must
7298da6d581SJan Schmidt  * free each list element). The leafs will be stored in the leafs ulist, which
7308da6d581SJan Schmidt  * must be freed with ulist_free.
7318da6d581SJan Schmidt  *
7328da6d581SJan Schmidt  * returns 0 on success, <0 on error
7338da6d581SJan Schmidt  */
7348da6d581SJan Schmidt static int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
7358da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
7368da6d581SJan Schmidt 				u64 num_bytes, u64 seq, struct ulist **leafs)
7378da6d581SJan Schmidt {
7388da6d581SJan Schmidt 	struct ulist *tmp;
7398da6d581SJan Schmidt 	int ret;
7408da6d581SJan Schmidt 
7418da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
7428da6d581SJan Schmidt 	if (!tmp)
7438da6d581SJan Schmidt 		return -ENOMEM;
7448da6d581SJan Schmidt 	*leafs = ulist_alloc(GFP_NOFS);
7458da6d581SJan Schmidt 	if (!*leafs) {
7468da6d581SJan Schmidt 		ulist_free(tmp);
7478da6d581SJan Schmidt 		return -ENOMEM;
7488da6d581SJan Schmidt 	}
7498da6d581SJan Schmidt 
7508da6d581SJan Schmidt 	ret = find_parent_nodes(trans, fs_info, bytenr, seq, *leafs, tmp);
7518da6d581SJan Schmidt 	ulist_free(tmp);
7528da6d581SJan Schmidt 
7538da6d581SJan Schmidt 	if (ret < 0 && ret != -ENOENT) {
7548da6d581SJan Schmidt 		ulist_free(*leafs);
7558da6d581SJan Schmidt 		return ret;
7568da6d581SJan Schmidt 	}
7578da6d581SJan Schmidt 
7588da6d581SJan Schmidt 	return 0;
7598da6d581SJan Schmidt }
7608da6d581SJan Schmidt 
7618da6d581SJan Schmidt /*
7628da6d581SJan Schmidt  * walk all backrefs for a given extent to find all roots that reference this
7638da6d581SJan Schmidt  * extent. Walking a backref means finding all extents that reference this
7648da6d581SJan Schmidt  * extent and in turn walk the backrefs of those, too. Naturally this is a
7658da6d581SJan Schmidt  * recursive process, but here it is implemented in an iterative fashion: We
7668da6d581SJan Schmidt  * find all referencing extents for the extent in question and put them on a
7678da6d581SJan Schmidt  * list. In turn, we find all referencing extents for those, further appending
7688da6d581SJan Schmidt  * to the list. The way we iterate the list allows adding more elements after
7698da6d581SJan Schmidt  * the current while iterating. The process stops when we reach the end of the
7708da6d581SJan Schmidt  * list. Found roots are added to the roots list.
7718da6d581SJan Schmidt  *
7728da6d581SJan Schmidt  * returns 0 on success, < 0 on error.
7738da6d581SJan Schmidt  */
7748da6d581SJan Schmidt int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
7758da6d581SJan Schmidt 				struct btrfs_fs_info *fs_info, u64 bytenr,
7768da6d581SJan Schmidt 				u64 num_bytes, u64 seq, struct ulist **roots)
7778da6d581SJan Schmidt {
7788da6d581SJan Schmidt 	struct ulist *tmp;
7798da6d581SJan Schmidt 	struct ulist_node *node = NULL;
7808da6d581SJan Schmidt 	int ret;
7818da6d581SJan Schmidt 
7828da6d581SJan Schmidt 	tmp = ulist_alloc(GFP_NOFS);
7838da6d581SJan Schmidt 	if (!tmp)
7848da6d581SJan Schmidt 		return -ENOMEM;
7858da6d581SJan Schmidt 	*roots = ulist_alloc(GFP_NOFS);
7868da6d581SJan Schmidt 	if (!*roots) {
7878da6d581SJan Schmidt 		ulist_free(tmp);
7888da6d581SJan Schmidt 		return -ENOMEM;
7898da6d581SJan Schmidt 	}
7908da6d581SJan Schmidt 
7918da6d581SJan Schmidt 	while (1) {
7928da6d581SJan Schmidt 		ret = find_parent_nodes(trans, fs_info, bytenr, seq,
7938da6d581SJan Schmidt 					tmp, *roots);
7948da6d581SJan Schmidt 		if (ret < 0 && ret != -ENOENT) {
7958da6d581SJan Schmidt 			ulist_free(tmp);
7968da6d581SJan Schmidt 			ulist_free(*roots);
7978da6d581SJan Schmidt 			return ret;
7988da6d581SJan Schmidt 		}
7998da6d581SJan Schmidt 		node = ulist_next(tmp, node);
8008da6d581SJan Schmidt 		if (!node)
8018da6d581SJan Schmidt 			break;
8028da6d581SJan Schmidt 		bytenr = node->val;
8038da6d581SJan Schmidt 	}
8048da6d581SJan Schmidt 
8058da6d581SJan Schmidt 	ulist_free(tmp);
8068da6d581SJan Schmidt 	return 0;
8078da6d581SJan Schmidt }
8088da6d581SJan Schmidt 
8098da6d581SJan Schmidt 
810a542ad1bSJan Schmidt static int __inode_info(u64 inum, u64 ioff, u8 key_type,
811a542ad1bSJan Schmidt 			struct btrfs_root *fs_root, struct btrfs_path *path,
812a542ad1bSJan Schmidt 			struct btrfs_key *found_key)
813a542ad1bSJan Schmidt {
814a542ad1bSJan Schmidt 	int ret;
815a542ad1bSJan Schmidt 	struct btrfs_key key;
816a542ad1bSJan Schmidt 	struct extent_buffer *eb;
817a542ad1bSJan Schmidt 
818a542ad1bSJan Schmidt 	key.type = key_type;
819a542ad1bSJan Schmidt 	key.objectid = inum;
820a542ad1bSJan Schmidt 	key.offset = ioff;
821a542ad1bSJan Schmidt 
822a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
823a542ad1bSJan Schmidt 	if (ret < 0)
824a542ad1bSJan Schmidt 		return ret;
825a542ad1bSJan Schmidt 
826a542ad1bSJan Schmidt 	eb = path->nodes[0];
827a542ad1bSJan Schmidt 	if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
828a542ad1bSJan Schmidt 		ret = btrfs_next_leaf(fs_root, path);
829a542ad1bSJan Schmidt 		if (ret)
830a542ad1bSJan Schmidt 			return ret;
831a542ad1bSJan Schmidt 		eb = path->nodes[0];
832a542ad1bSJan Schmidt 	}
833a542ad1bSJan Schmidt 
834a542ad1bSJan Schmidt 	btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
835a542ad1bSJan Schmidt 	if (found_key->type != key.type || found_key->objectid != key.objectid)
836a542ad1bSJan Schmidt 		return 1;
837a542ad1bSJan Schmidt 
838a542ad1bSJan Schmidt 	return 0;
839a542ad1bSJan Schmidt }
840a542ad1bSJan Schmidt 
841a542ad1bSJan Schmidt /*
842a542ad1bSJan Schmidt  * this makes the path point to (inum INODE_ITEM ioff)
843a542ad1bSJan Schmidt  */
844a542ad1bSJan Schmidt int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
845a542ad1bSJan Schmidt 			struct btrfs_path *path)
846a542ad1bSJan Schmidt {
847a542ad1bSJan Schmidt 	struct btrfs_key key;
848a542ad1bSJan Schmidt 	return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path,
849a542ad1bSJan Schmidt 				&key);
850a542ad1bSJan Schmidt }
851a542ad1bSJan Schmidt 
852a542ad1bSJan Schmidt static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root,
853a542ad1bSJan Schmidt 				struct btrfs_path *path,
854a542ad1bSJan Schmidt 				struct btrfs_key *found_key)
855a542ad1bSJan Schmidt {
856a542ad1bSJan Schmidt 	return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path,
857a542ad1bSJan Schmidt 				found_key);
858a542ad1bSJan Schmidt }
859a542ad1bSJan Schmidt 
860a542ad1bSJan Schmidt /*
861a542ad1bSJan Schmidt  * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
862a542ad1bSJan Schmidt  * of the path are separated by '/' and the path is guaranteed to be
863a542ad1bSJan Schmidt  * 0-terminated. the path is only given within the current file system.
864a542ad1bSJan Schmidt  * Therefore, it never starts with a '/'. the caller is responsible to provide
865a542ad1bSJan Schmidt  * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
866a542ad1bSJan Schmidt  * the start point of the resulting string is returned. this pointer is within
867a542ad1bSJan Schmidt  * dest, normally.
868a542ad1bSJan Schmidt  * in case the path buffer would overflow, the pointer is decremented further
869a542ad1bSJan Schmidt  * as if output was written to the buffer, though no more output is actually
870a542ad1bSJan Schmidt  * generated. that way, the caller can determine how much space would be
871a542ad1bSJan Schmidt  * required for the path to fit into the buffer. in that case, the returned
872a542ad1bSJan Schmidt  * value will be smaller than dest. callers must check this!
873a542ad1bSJan Schmidt  */
874a542ad1bSJan Schmidt static char *iref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
875a542ad1bSJan Schmidt 				struct btrfs_inode_ref *iref,
876a542ad1bSJan Schmidt 				struct extent_buffer *eb_in, u64 parent,
877a542ad1bSJan Schmidt 				char *dest, u32 size)
878a542ad1bSJan Schmidt {
879a542ad1bSJan Schmidt 	u32 len;
880a542ad1bSJan Schmidt 	int slot;
881a542ad1bSJan Schmidt 	u64 next_inum;
882a542ad1bSJan Schmidt 	int ret;
883a542ad1bSJan Schmidt 	s64 bytes_left = size - 1;
884a542ad1bSJan Schmidt 	struct extent_buffer *eb = eb_in;
885a542ad1bSJan Schmidt 	struct btrfs_key found_key;
886a542ad1bSJan Schmidt 
887a542ad1bSJan Schmidt 	if (bytes_left >= 0)
888a542ad1bSJan Schmidt 		dest[bytes_left] = '\0';
889a542ad1bSJan Schmidt 
890a542ad1bSJan Schmidt 	while (1) {
891a542ad1bSJan Schmidt 		len = btrfs_inode_ref_name_len(eb, iref);
892a542ad1bSJan Schmidt 		bytes_left -= len;
893a542ad1bSJan Schmidt 		if (bytes_left >= 0)
894a542ad1bSJan Schmidt 			read_extent_buffer(eb, dest + bytes_left,
895a542ad1bSJan Schmidt 						(unsigned long)(iref + 1), len);
896a542ad1bSJan Schmidt 		if (eb != eb_in)
897a542ad1bSJan Schmidt 			free_extent_buffer(eb);
898a542ad1bSJan Schmidt 		ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
8998f24b496SJan Schmidt 		if (ret > 0)
9008f24b496SJan Schmidt 			ret = -ENOENT;
901a542ad1bSJan Schmidt 		if (ret)
902a542ad1bSJan Schmidt 			break;
903a542ad1bSJan Schmidt 		next_inum = found_key.offset;
904a542ad1bSJan Schmidt 
905a542ad1bSJan Schmidt 		/* regular exit ahead */
906a542ad1bSJan Schmidt 		if (parent == next_inum)
907a542ad1bSJan Schmidt 			break;
908a542ad1bSJan Schmidt 
909a542ad1bSJan Schmidt 		slot = path->slots[0];
910a542ad1bSJan Schmidt 		eb = path->nodes[0];
911a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
912a542ad1bSJan Schmidt 		if (eb != eb_in)
913a542ad1bSJan Schmidt 			atomic_inc(&eb->refs);
914a542ad1bSJan Schmidt 		btrfs_release_path(path);
915a542ad1bSJan Schmidt 
916a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
917a542ad1bSJan Schmidt 		parent = next_inum;
918a542ad1bSJan Schmidt 		--bytes_left;
919a542ad1bSJan Schmidt 		if (bytes_left >= 0)
920a542ad1bSJan Schmidt 			dest[bytes_left] = '/';
921a542ad1bSJan Schmidt 	}
922a542ad1bSJan Schmidt 
923a542ad1bSJan Schmidt 	btrfs_release_path(path);
924a542ad1bSJan Schmidt 
925a542ad1bSJan Schmidt 	if (ret)
926a542ad1bSJan Schmidt 		return ERR_PTR(ret);
927a542ad1bSJan Schmidt 
928a542ad1bSJan Schmidt 	return dest + bytes_left;
929a542ad1bSJan Schmidt }
930a542ad1bSJan Schmidt 
931a542ad1bSJan Schmidt /*
932a542ad1bSJan Schmidt  * this makes the path point to (logical EXTENT_ITEM *)
933a542ad1bSJan Schmidt  * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
934a542ad1bSJan Schmidt  * tree blocks and <0 on error.
935a542ad1bSJan Schmidt  */
936a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
937a542ad1bSJan Schmidt 			struct btrfs_path *path, struct btrfs_key *found_key)
938a542ad1bSJan Schmidt {
939a542ad1bSJan Schmidt 	int ret;
940a542ad1bSJan Schmidt 	u64 flags;
941a542ad1bSJan Schmidt 	u32 item_size;
942a542ad1bSJan Schmidt 	struct extent_buffer *eb;
943a542ad1bSJan Schmidt 	struct btrfs_extent_item *ei;
944a542ad1bSJan Schmidt 	struct btrfs_key key;
945a542ad1bSJan Schmidt 
946a542ad1bSJan Schmidt 	key.type = BTRFS_EXTENT_ITEM_KEY;
947a542ad1bSJan Schmidt 	key.objectid = logical;
948a542ad1bSJan Schmidt 	key.offset = (u64)-1;
949a542ad1bSJan Schmidt 
950a542ad1bSJan Schmidt 	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
951a542ad1bSJan Schmidt 	if (ret < 0)
952a542ad1bSJan Schmidt 		return ret;
953a542ad1bSJan Schmidt 	ret = btrfs_previous_item(fs_info->extent_root, path,
954a542ad1bSJan Schmidt 					0, BTRFS_EXTENT_ITEM_KEY);
955a542ad1bSJan Schmidt 	if (ret < 0)
956a542ad1bSJan Schmidt 		return ret;
957a542ad1bSJan Schmidt 
958a542ad1bSJan Schmidt 	btrfs_item_key_to_cpu(path->nodes[0], found_key, path->slots[0]);
959a542ad1bSJan Schmidt 	if (found_key->type != BTRFS_EXTENT_ITEM_KEY ||
960a542ad1bSJan Schmidt 	    found_key->objectid > logical ||
9614692cf58SJan Schmidt 	    found_key->objectid + found_key->offset <= logical) {
9624692cf58SJan Schmidt 		pr_debug("logical %llu is not within any extent\n",
9634692cf58SJan Schmidt 			 (unsigned long long)logical);
964a542ad1bSJan Schmidt 		return -ENOENT;
9654692cf58SJan Schmidt 	}
966a542ad1bSJan Schmidt 
967a542ad1bSJan Schmidt 	eb = path->nodes[0];
968a542ad1bSJan Schmidt 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
969a542ad1bSJan Schmidt 	BUG_ON(item_size < sizeof(*ei));
970a542ad1bSJan Schmidt 
971a542ad1bSJan Schmidt 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
972a542ad1bSJan Schmidt 	flags = btrfs_extent_flags(eb, ei);
973a542ad1bSJan Schmidt 
9744692cf58SJan Schmidt 	pr_debug("logical %llu is at position %llu within the extent (%llu "
9754692cf58SJan Schmidt 		 "EXTENT_ITEM %llu) flags %#llx size %u\n",
9764692cf58SJan Schmidt 		 (unsigned long long)logical,
9774692cf58SJan Schmidt 		 (unsigned long long)(logical - found_key->objectid),
9784692cf58SJan Schmidt 		 (unsigned long long)found_key->objectid,
9794692cf58SJan Schmidt 		 (unsigned long long)found_key->offset,
9804692cf58SJan Schmidt 		 (unsigned long long)flags, item_size);
981a542ad1bSJan Schmidt 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
982a542ad1bSJan Schmidt 		return BTRFS_EXTENT_FLAG_TREE_BLOCK;
983a542ad1bSJan Schmidt 	if (flags & BTRFS_EXTENT_FLAG_DATA)
984a542ad1bSJan Schmidt 		return BTRFS_EXTENT_FLAG_DATA;
985a542ad1bSJan Schmidt 
986a542ad1bSJan Schmidt 	return -EIO;
987a542ad1bSJan Schmidt }
988a542ad1bSJan Schmidt 
989a542ad1bSJan Schmidt /*
990a542ad1bSJan Schmidt  * helper function to iterate extent inline refs. ptr must point to a 0 value
991a542ad1bSJan Schmidt  * for the first call and may be modified. it is used to track state.
992a542ad1bSJan Schmidt  * if more refs exist, 0 is returned and the next call to
993a542ad1bSJan Schmidt  * __get_extent_inline_ref must pass the modified ptr parameter to get the
994a542ad1bSJan Schmidt  * next ref. after the last ref was processed, 1 is returned.
995a542ad1bSJan Schmidt  * returns <0 on error
996a542ad1bSJan Schmidt  */
997a542ad1bSJan Schmidt static int __get_extent_inline_ref(unsigned long *ptr, struct extent_buffer *eb,
998a542ad1bSJan Schmidt 				struct btrfs_extent_item *ei, u32 item_size,
999a542ad1bSJan Schmidt 				struct btrfs_extent_inline_ref **out_eiref,
1000a542ad1bSJan Schmidt 				int *out_type)
1001a542ad1bSJan Schmidt {
1002a542ad1bSJan Schmidt 	unsigned long end;
1003a542ad1bSJan Schmidt 	u64 flags;
1004a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1005a542ad1bSJan Schmidt 
1006a542ad1bSJan Schmidt 	if (!*ptr) {
1007a542ad1bSJan Schmidt 		/* first call */
1008a542ad1bSJan Schmidt 		flags = btrfs_extent_flags(eb, ei);
1009a542ad1bSJan Schmidt 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1010a542ad1bSJan Schmidt 			info = (struct btrfs_tree_block_info *)(ei + 1);
1011a542ad1bSJan Schmidt 			*out_eiref =
1012a542ad1bSJan Schmidt 				(struct btrfs_extent_inline_ref *)(info + 1);
1013a542ad1bSJan Schmidt 		} else {
1014a542ad1bSJan Schmidt 			*out_eiref = (struct btrfs_extent_inline_ref *)(ei + 1);
1015a542ad1bSJan Schmidt 		}
1016a542ad1bSJan Schmidt 		*ptr = (unsigned long)*out_eiref;
1017a542ad1bSJan Schmidt 		if ((void *)*ptr >= (void *)ei + item_size)
1018a542ad1bSJan Schmidt 			return -ENOENT;
1019a542ad1bSJan Schmidt 	}
1020a542ad1bSJan Schmidt 
1021a542ad1bSJan Schmidt 	end = (unsigned long)ei + item_size;
1022a542ad1bSJan Schmidt 	*out_eiref = (struct btrfs_extent_inline_ref *)*ptr;
1023a542ad1bSJan Schmidt 	*out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1024a542ad1bSJan Schmidt 
1025a542ad1bSJan Schmidt 	*ptr += btrfs_extent_inline_ref_size(*out_type);
1026a542ad1bSJan Schmidt 	WARN_ON(*ptr > end);
1027a542ad1bSJan Schmidt 	if (*ptr == end)
1028a542ad1bSJan Schmidt 		return 1; /* last */
1029a542ad1bSJan Schmidt 
1030a542ad1bSJan Schmidt 	return 0;
1031a542ad1bSJan Schmidt }
1032a542ad1bSJan Schmidt 
1033a542ad1bSJan Schmidt /*
1034a542ad1bSJan Schmidt  * reads the tree block backref for an extent. tree level and root are returned
1035a542ad1bSJan Schmidt  * through out_level and out_root. ptr must point to a 0 value for the first
1036a542ad1bSJan Schmidt  * call and may be modified (see __get_extent_inline_ref comment).
1037a542ad1bSJan Schmidt  * returns 0 if data was provided, 1 if there was no more data to provide or
1038a542ad1bSJan Schmidt  * <0 on error.
1039a542ad1bSJan Schmidt  */
1040a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
1041a542ad1bSJan Schmidt 				struct btrfs_extent_item *ei, u32 item_size,
1042a542ad1bSJan Schmidt 				u64 *out_root, u8 *out_level)
1043a542ad1bSJan Schmidt {
1044a542ad1bSJan Schmidt 	int ret;
1045a542ad1bSJan Schmidt 	int type;
1046a542ad1bSJan Schmidt 	struct btrfs_tree_block_info *info;
1047a542ad1bSJan Schmidt 	struct btrfs_extent_inline_ref *eiref;
1048a542ad1bSJan Schmidt 
1049a542ad1bSJan Schmidt 	if (*ptr == (unsigned long)-1)
1050a542ad1bSJan Schmidt 		return 1;
1051a542ad1bSJan Schmidt 
1052a542ad1bSJan Schmidt 	while (1) {
1053a542ad1bSJan Schmidt 		ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
1054a542ad1bSJan Schmidt 						&eiref, &type);
1055a542ad1bSJan Schmidt 		if (ret < 0)
1056a542ad1bSJan Schmidt 			return ret;
1057a542ad1bSJan Schmidt 
1058a542ad1bSJan Schmidt 		if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1059a542ad1bSJan Schmidt 		    type == BTRFS_SHARED_BLOCK_REF_KEY)
1060a542ad1bSJan Schmidt 			break;
1061a542ad1bSJan Schmidt 
1062a542ad1bSJan Schmidt 		if (ret == 1)
1063a542ad1bSJan Schmidt 			return 1;
1064a542ad1bSJan Schmidt 	}
1065a542ad1bSJan Schmidt 
1066a542ad1bSJan Schmidt 	/* we can treat both ref types equally here */
1067a542ad1bSJan Schmidt 	info = (struct btrfs_tree_block_info *)(ei + 1);
1068a542ad1bSJan Schmidt 	*out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1069a542ad1bSJan Schmidt 	*out_level = btrfs_tree_block_level(eb, info);
1070a542ad1bSJan Schmidt 
1071a542ad1bSJan Schmidt 	if (ret == 1)
1072a542ad1bSJan Schmidt 		*ptr = (unsigned long)-1;
1073a542ad1bSJan Schmidt 
1074a542ad1bSJan Schmidt 	return 0;
1075a542ad1bSJan Schmidt }
1076a542ad1bSJan Schmidt 
10774692cf58SJan Schmidt static int iterate_leaf_refs(struct btrfs_fs_info *fs_info,
10784692cf58SJan Schmidt 				struct btrfs_path *path, u64 logical,
10794692cf58SJan Schmidt 				u64 orig_extent_item_objectid,
10804692cf58SJan Schmidt 				u64 extent_item_pos, u64 root,
10814692cf58SJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1082a542ad1bSJan Schmidt {
1083a542ad1bSJan Schmidt 	u64 disk_byte;
1084a542ad1bSJan Schmidt 	struct btrfs_key key;
1085a542ad1bSJan Schmidt 	struct btrfs_file_extent_item *fi;
1086a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1087a542ad1bSJan Schmidt 	int slot;
1088a542ad1bSJan Schmidt 	int nritems;
10894692cf58SJan Schmidt 	int ret = 0;
10904692cf58SJan Schmidt 	int extent_type;
10914692cf58SJan Schmidt 	u64 data_offset;
10924692cf58SJan Schmidt 	u64 data_len;
1093a542ad1bSJan Schmidt 
1094a542ad1bSJan Schmidt 	eb = read_tree_block(fs_info->tree_root, logical,
1095a542ad1bSJan Schmidt 				fs_info->tree_root->leafsize, 0);
1096a542ad1bSJan Schmidt 	if (!eb)
1097a542ad1bSJan Schmidt 		return -EIO;
1098a542ad1bSJan Schmidt 
1099a542ad1bSJan Schmidt 	/*
1100a542ad1bSJan Schmidt 	 * from the shared data ref, we only have the leaf but we need
1101a542ad1bSJan Schmidt 	 * the key. thus, we must look into all items and see that we
1102a542ad1bSJan Schmidt 	 * find one (some) with a reference to our extent item.
1103a542ad1bSJan Schmidt 	 */
1104a542ad1bSJan Schmidt 	nritems = btrfs_header_nritems(eb);
1105a542ad1bSJan Schmidt 	for (slot = 0; slot < nritems; ++slot) {
1106a542ad1bSJan Schmidt 		btrfs_item_key_to_cpu(eb, &key, slot);
1107a542ad1bSJan Schmidt 		if (key.type != BTRFS_EXTENT_DATA_KEY)
1108a542ad1bSJan Schmidt 			continue;
1109a542ad1bSJan Schmidt 		fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
11104692cf58SJan Schmidt 		extent_type = btrfs_file_extent_type(eb, fi);
11114692cf58SJan Schmidt 		if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1112a542ad1bSJan Schmidt 			continue;
11134692cf58SJan Schmidt 		/* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
11144692cf58SJan Schmidt 		disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
11154692cf58SJan Schmidt 		if (disk_byte != orig_extent_item_objectid)
11164692cf58SJan Schmidt 			continue;
11174692cf58SJan Schmidt 
11184692cf58SJan Schmidt 		data_offset = btrfs_file_extent_offset(eb, fi);
11194692cf58SJan Schmidt 		data_len = btrfs_file_extent_num_bytes(eb, fi);
11204692cf58SJan Schmidt 
11214692cf58SJan Schmidt 		if (extent_item_pos < data_offset ||
11224692cf58SJan Schmidt 		    extent_item_pos >= data_offset + data_len)
11234692cf58SJan Schmidt 			continue;
11244692cf58SJan Schmidt 
11254692cf58SJan Schmidt 		pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
11264692cf58SJan Schmidt 				"root %llu\n", orig_extent_item_objectid,
11274692cf58SJan Schmidt 				key.objectid, key.offset, root);
11284692cf58SJan Schmidt 		ret = iterate(key.objectid,
11294692cf58SJan Schmidt 				key.offset + (extent_item_pos - data_offset),
11304692cf58SJan Schmidt 				root, ctx);
11314692cf58SJan Schmidt 		if (ret) {
11324692cf58SJan Schmidt 			pr_debug("stopping iteration because ret=%d\n", ret);
1133a542ad1bSJan Schmidt 			break;
1134a542ad1bSJan Schmidt 		}
1135a542ad1bSJan Schmidt 	}
1136a542ad1bSJan Schmidt 
1137a542ad1bSJan Schmidt 	free_extent_buffer(eb);
11384692cf58SJan Schmidt 
1139a542ad1bSJan Schmidt 	return ret;
1140a542ad1bSJan Schmidt }
1141a542ad1bSJan Schmidt 
1142a542ad1bSJan Schmidt /*
1143a542ad1bSJan Schmidt  * calls iterate() for every inode that references the extent identified by
11444692cf58SJan Schmidt  * the given parameters.
1145a542ad1bSJan Schmidt  * when the iterator function returns a non-zero value, iteration stops.
11464692cf58SJan Schmidt  * path is guaranteed to be in released state when iterate() is called.
1147a542ad1bSJan Schmidt  */
1148a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
1149a542ad1bSJan Schmidt 				struct btrfs_path *path,
11504692cf58SJan Schmidt 				u64 extent_item_objectid, u64 extent_item_pos,
1151a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1152a542ad1bSJan Schmidt {
1153a542ad1bSJan Schmidt 	int ret;
1154a542ad1bSJan Schmidt 	struct list_head data_refs = LIST_HEAD_INIT(data_refs);
1155a542ad1bSJan Schmidt 	struct list_head shared_refs = LIST_HEAD_INIT(shared_refs);
11564692cf58SJan Schmidt 	struct btrfs_trans_handle *trans;
11574692cf58SJan Schmidt 	struct ulist *refs;
11584692cf58SJan Schmidt 	struct ulist *roots;
11594692cf58SJan Schmidt 	struct ulist_node *ref_node = NULL;
11604692cf58SJan Schmidt 	struct ulist_node *root_node = NULL;
11614692cf58SJan Schmidt 	struct seq_list seq_elem;
11624692cf58SJan Schmidt 	struct btrfs_delayed_ref_root *delayed_refs;
1163a542ad1bSJan Schmidt 
11644692cf58SJan Schmidt 	trans = btrfs_join_transaction(fs_info->extent_root);
11654692cf58SJan Schmidt 	if (IS_ERR(trans))
11664692cf58SJan Schmidt 		return PTR_ERR(trans);
1167a542ad1bSJan Schmidt 
11684692cf58SJan Schmidt 	pr_debug("resolving all inodes for extent %llu\n",
11694692cf58SJan Schmidt 			extent_item_objectid);
11704692cf58SJan Schmidt 
11714692cf58SJan Schmidt 	delayed_refs = &trans->transaction->delayed_refs;
11724692cf58SJan Schmidt 	spin_lock(&delayed_refs->lock);
11734692cf58SJan Schmidt 	btrfs_get_delayed_seq(delayed_refs, &seq_elem);
11744692cf58SJan Schmidt 	spin_unlock(&delayed_refs->lock);
11754692cf58SJan Schmidt 
11764692cf58SJan Schmidt 	ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
11774692cf58SJan Schmidt 				   extent_item_pos, seq_elem.seq,
11784692cf58SJan Schmidt 				   &refs);
11794692cf58SJan Schmidt 
11804692cf58SJan Schmidt 	if (ret)
11814692cf58SJan Schmidt 		goto out;
11824692cf58SJan Schmidt 
11834692cf58SJan Schmidt 	while (!ret && (ref_node = ulist_next(refs, ref_node))) {
11844692cf58SJan Schmidt 		ret = btrfs_find_all_roots(trans, fs_info, ref_node->val, -1,
11854692cf58SJan Schmidt 						seq_elem.seq, &roots);
11864692cf58SJan Schmidt 		if (ret)
1187a542ad1bSJan Schmidt 			break;
11884692cf58SJan Schmidt 		while (!ret && (root_node = ulist_next(roots, root_node))) {
11894692cf58SJan Schmidt 			pr_debug("root %llu references leaf %llu\n",
11904692cf58SJan Schmidt 					root_node->val, ref_node->val);
11914692cf58SJan Schmidt 			ret = iterate_leaf_refs(fs_info, path, ref_node->val,
1192a542ad1bSJan Schmidt 						extent_item_objectid,
11934692cf58SJan Schmidt 						extent_item_pos, root_node->val,
1194a542ad1bSJan Schmidt 						iterate, ctx);
11954692cf58SJan Schmidt 		}
1196a542ad1bSJan Schmidt 	}
1197a542ad1bSJan Schmidt 
11984692cf58SJan Schmidt 	ulist_free(refs);
11994692cf58SJan Schmidt 	ulist_free(roots);
12004692cf58SJan Schmidt out:
12014692cf58SJan Schmidt 	btrfs_put_delayed_seq(delayed_refs, &seq_elem);
12024692cf58SJan Schmidt 	btrfs_end_transaction(trans, fs_info->extent_root);
1203a542ad1bSJan Schmidt 	return ret;
1204a542ad1bSJan Schmidt }
1205a542ad1bSJan Schmidt 
1206a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
1207a542ad1bSJan Schmidt 				struct btrfs_path *path,
1208a542ad1bSJan Schmidt 				iterate_extent_inodes_t *iterate, void *ctx)
1209a542ad1bSJan Schmidt {
1210a542ad1bSJan Schmidt 	int ret;
12114692cf58SJan Schmidt 	u64 extent_item_pos;
1212a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1213a542ad1bSJan Schmidt 
1214a542ad1bSJan Schmidt 	ret = extent_from_logical(fs_info, logical, path,
1215a542ad1bSJan Schmidt 					&found_key);
12164692cf58SJan Schmidt 	btrfs_release_path(path);
1217a542ad1bSJan Schmidt 	if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1218a542ad1bSJan Schmidt 		ret = -EINVAL;
1219a542ad1bSJan Schmidt 	if (ret < 0)
1220a542ad1bSJan Schmidt 		return ret;
1221a542ad1bSJan Schmidt 
12224692cf58SJan Schmidt 	extent_item_pos = logical - found_key.objectid;
1223a542ad1bSJan Schmidt 	ret = iterate_extent_inodes(fs_info, path, found_key.objectid,
12244692cf58SJan Schmidt 					extent_item_pos, iterate, ctx);
1225a542ad1bSJan Schmidt 
1226a542ad1bSJan Schmidt 	return ret;
1227a542ad1bSJan Schmidt }
1228a542ad1bSJan Schmidt 
1229a542ad1bSJan Schmidt static int iterate_irefs(u64 inum, struct btrfs_root *fs_root,
1230a542ad1bSJan Schmidt 				struct btrfs_path *path,
1231a542ad1bSJan Schmidt 				iterate_irefs_t *iterate, void *ctx)
1232a542ad1bSJan Schmidt {
1233a542ad1bSJan Schmidt 	int ret;
1234a542ad1bSJan Schmidt 	int slot;
1235a542ad1bSJan Schmidt 	u32 cur;
1236a542ad1bSJan Schmidt 	u32 len;
1237a542ad1bSJan Schmidt 	u32 name_len;
1238a542ad1bSJan Schmidt 	u64 parent = 0;
1239a542ad1bSJan Schmidt 	int found = 0;
1240a542ad1bSJan Schmidt 	struct extent_buffer *eb;
1241a542ad1bSJan Schmidt 	struct btrfs_item *item;
1242a542ad1bSJan Schmidt 	struct btrfs_inode_ref *iref;
1243a542ad1bSJan Schmidt 	struct btrfs_key found_key;
1244a542ad1bSJan Schmidt 
1245a542ad1bSJan Schmidt 	while (1) {
1246a542ad1bSJan Schmidt 		ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
1247a542ad1bSJan Schmidt 					&found_key);
1248a542ad1bSJan Schmidt 		if (ret < 0)
1249a542ad1bSJan Schmidt 			break;
1250a542ad1bSJan Schmidt 		if (ret) {
1251a542ad1bSJan Schmidt 			ret = found ? 0 : -ENOENT;
1252a542ad1bSJan Schmidt 			break;
1253a542ad1bSJan Schmidt 		}
1254a542ad1bSJan Schmidt 		++found;
1255a542ad1bSJan Schmidt 
1256a542ad1bSJan Schmidt 		parent = found_key.offset;
1257a542ad1bSJan Schmidt 		slot = path->slots[0];
1258a542ad1bSJan Schmidt 		eb = path->nodes[0];
1259a542ad1bSJan Schmidt 		/* make sure we can use eb after releasing the path */
1260a542ad1bSJan Schmidt 		atomic_inc(&eb->refs);
1261a542ad1bSJan Schmidt 		btrfs_release_path(path);
1262a542ad1bSJan Schmidt 
1263a542ad1bSJan Schmidt 		item = btrfs_item_nr(eb, slot);
1264a542ad1bSJan Schmidt 		iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1265a542ad1bSJan Schmidt 
1266a542ad1bSJan Schmidt 		for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1267a542ad1bSJan Schmidt 			name_len = btrfs_inode_ref_name_len(eb, iref);
1268a542ad1bSJan Schmidt 			/* path must be released before calling iterate()! */
12694692cf58SJan Schmidt 			pr_debug("following ref at offset %u for inode %llu in "
12704692cf58SJan Schmidt 				 "tree %llu\n", cur,
12714692cf58SJan Schmidt 				 (unsigned long long)found_key.objectid,
12724692cf58SJan Schmidt 				 (unsigned long long)fs_root->objectid);
1273a542ad1bSJan Schmidt 			ret = iterate(parent, iref, eb, ctx);
1274a542ad1bSJan Schmidt 			if (ret) {
1275a542ad1bSJan Schmidt 				free_extent_buffer(eb);
1276a542ad1bSJan Schmidt 				break;
1277a542ad1bSJan Schmidt 			}
1278a542ad1bSJan Schmidt 			len = sizeof(*iref) + name_len;
1279a542ad1bSJan Schmidt 			iref = (struct btrfs_inode_ref *)((char *)iref + len);
1280a542ad1bSJan Schmidt 		}
1281a542ad1bSJan Schmidt 		free_extent_buffer(eb);
1282a542ad1bSJan Schmidt 	}
1283a542ad1bSJan Schmidt 
1284a542ad1bSJan Schmidt 	btrfs_release_path(path);
1285a542ad1bSJan Schmidt 
1286a542ad1bSJan Schmidt 	return ret;
1287a542ad1bSJan Schmidt }
1288a542ad1bSJan Schmidt 
1289a542ad1bSJan Schmidt /*
1290a542ad1bSJan Schmidt  * returns 0 if the path could be dumped (probably truncated)
1291a542ad1bSJan Schmidt  * returns <0 in case of an error
1292a542ad1bSJan Schmidt  */
1293a542ad1bSJan Schmidt static int inode_to_path(u64 inum, struct btrfs_inode_ref *iref,
1294a542ad1bSJan Schmidt 				struct extent_buffer *eb, void *ctx)
1295a542ad1bSJan Schmidt {
1296a542ad1bSJan Schmidt 	struct inode_fs_paths *ipath = ctx;
1297a542ad1bSJan Schmidt 	char *fspath;
1298a542ad1bSJan Schmidt 	char *fspath_min;
1299a542ad1bSJan Schmidt 	int i = ipath->fspath->elem_cnt;
1300a542ad1bSJan Schmidt 	const int s_ptr = sizeof(char *);
1301a542ad1bSJan Schmidt 	u32 bytes_left;
1302a542ad1bSJan Schmidt 
1303a542ad1bSJan Schmidt 	bytes_left = ipath->fspath->bytes_left > s_ptr ?
1304a542ad1bSJan Schmidt 					ipath->fspath->bytes_left - s_ptr : 0;
1305a542ad1bSJan Schmidt 
1306740c3d22SChris Mason 	fspath_min = (char *)ipath->fspath->val + (i + 1) * s_ptr;
1307a542ad1bSJan Schmidt 	fspath = iref_to_path(ipath->fs_root, ipath->btrfs_path, iref, eb,
1308a542ad1bSJan Schmidt 				inum, fspath_min, bytes_left);
1309a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
1310a542ad1bSJan Schmidt 		return PTR_ERR(fspath);
1311a542ad1bSJan Schmidt 
1312a542ad1bSJan Schmidt 	if (fspath > fspath_min) {
13134692cf58SJan Schmidt 		pr_debug("path resolved: %s\n", fspath);
1314745c4d8eSJeff Mahoney 		ipath->fspath->val[i] = (u64)(unsigned long)fspath;
1315a542ad1bSJan Schmidt 		++ipath->fspath->elem_cnt;
1316a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = fspath - fspath_min;
1317a542ad1bSJan Schmidt 	} else {
13184692cf58SJan Schmidt 		pr_debug("missed path, not enough space. missing bytes: %lu, "
13194692cf58SJan Schmidt 			 "constructed so far: %s\n",
13204692cf58SJan Schmidt 			 (unsigned long)(fspath_min - fspath), fspath_min);
1321a542ad1bSJan Schmidt 		++ipath->fspath->elem_missed;
1322a542ad1bSJan Schmidt 		ipath->fspath->bytes_missing += fspath_min - fspath;
1323a542ad1bSJan Schmidt 		ipath->fspath->bytes_left = 0;
1324a542ad1bSJan Schmidt 	}
1325a542ad1bSJan Schmidt 
1326a542ad1bSJan Schmidt 	return 0;
1327a542ad1bSJan Schmidt }
1328a542ad1bSJan Schmidt 
1329a542ad1bSJan Schmidt /*
1330a542ad1bSJan Schmidt  * this dumps all file system paths to the inode into the ipath struct, provided
1331a542ad1bSJan Schmidt  * is has been created large enough. each path is zero-terminated and accessed
1332740c3d22SChris Mason  * from ipath->fspath->val[i].
1333a542ad1bSJan Schmidt  * when it returns, there are ipath->fspath->elem_cnt number of paths available
1334740c3d22SChris Mason  * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
1335a542ad1bSJan Schmidt  * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1336a542ad1bSJan Schmidt  * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1337a542ad1bSJan Schmidt  * have been needed to return all paths.
1338a542ad1bSJan Schmidt  */
1339a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath)
1340a542ad1bSJan Schmidt {
1341a542ad1bSJan Schmidt 	return iterate_irefs(inum, ipath->fs_root, ipath->btrfs_path,
1342a542ad1bSJan Schmidt 				inode_to_path, ipath);
1343a542ad1bSJan Schmidt }
1344a542ad1bSJan Schmidt 
1345a542ad1bSJan Schmidt /*
1346a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
1347a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
1348a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
1349a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
1350a542ad1bSJan Schmidt  */
1351a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes)
1352a542ad1bSJan Schmidt {
1353a542ad1bSJan Schmidt 	struct btrfs_data_container *data;
1354a542ad1bSJan Schmidt 	size_t alloc_bytes;
1355a542ad1bSJan Schmidt 
1356a542ad1bSJan Schmidt 	alloc_bytes = max_t(size_t, total_bytes, sizeof(*data));
1357a542ad1bSJan Schmidt 	data = kmalloc(alloc_bytes, GFP_NOFS);
1358a542ad1bSJan Schmidt 	if (!data)
1359a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
1360a542ad1bSJan Schmidt 
1361a542ad1bSJan Schmidt 	if (total_bytes >= sizeof(*data)) {
1362a542ad1bSJan Schmidt 		data->bytes_left = total_bytes - sizeof(*data);
1363a542ad1bSJan Schmidt 		data->bytes_missing = 0;
1364a542ad1bSJan Schmidt 	} else {
1365a542ad1bSJan Schmidt 		data->bytes_missing = sizeof(*data) - total_bytes;
1366a542ad1bSJan Schmidt 		data->bytes_left = 0;
1367a542ad1bSJan Schmidt 	}
1368a542ad1bSJan Schmidt 
1369a542ad1bSJan Schmidt 	data->elem_cnt = 0;
1370a542ad1bSJan Schmidt 	data->elem_missed = 0;
1371a542ad1bSJan Schmidt 
1372a542ad1bSJan Schmidt 	return data;
1373a542ad1bSJan Schmidt }
1374a542ad1bSJan Schmidt 
1375a542ad1bSJan Schmidt /*
1376a542ad1bSJan Schmidt  * allocates space to return multiple file system paths for an inode.
1377a542ad1bSJan Schmidt  * total_bytes to allocate are passed, note that space usable for actual path
1378a542ad1bSJan Schmidt  * information will be total_bytes - sizeof(struct inode_fs_paths).
1379a542ad1bSJan Schmidt  * the returned pointer must be freed with free_ipath() in the end.
1380a542ad1bSJan Schmidt  */
1381a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
1382a542ad1bSJan Schmidt 					struct btrfs_path *path)
1383a542ad1bSJan Schmidt {
1384a542ad1bSJan Schmidt 	struct inode_fs_paths *ifp;
1385a542ad1bSJan Schmidt 	struct btrfs_data_container *fspath;
1386a542ad1bSJan Schmidt 
1387a542ad1bSJan Schmidt 	fspath = init_data_container(total_bytes);
1388a542ad1bSJan Schmidt 	if (IS_ERR(fspath))
1389a542ad1bSJan Schmidt 		return (void *)fspath;
1390a542ad1bSJan Schmidt 
1391a542ad1bSJan Schmidt 	ifp = kmalloc(sizeof(*ifp), GFP_NOFS);
1392a542ad1bSJan Schmidt 	if (!ifp) {
1393a542ad1bSJan Schmidt 		kfree(fspath);
1394a542ad1bSJan Schmidt 		return ERR_PTR(-ENOMEM);
1395a542ad1bSJan Schmidt 	}
1396a542ad1bSJan Schmidt 
1397a542ad1bSJan Schmidt 	ifp->btrfs_path = path;
1398a542ad1bSJan Schmidt 	ifp->fspath = fspath;
1399a542ad1bSJan Schmidt 	ifp->fs_root = fs_root;
1400a542ad1bSJan Schmidt 
1401a542ad1bSJan Schmidt 	return ifp;
1402a542ad1bSJan Schmidt }
1403a542ad1bSJan Schmidt 
1404a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath)
1405a542ad1bSJan Schmidt {
1406a542ad1bSJan Schmidt 	kfree(ipath);
1407a542ad1bSJan Schmidt }
1408