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