xref: /openbmc/linux/fs/btrfs/backref.h (revision 9b569ea0)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
2a542ad1bSJan Schmidt /*
3a542ad1bSJan Schmidt  * Copyright (C) 2011 STRATO.  All rights reserved.
4a542ad1bSJan Schmidt  */
5a542ad1bSJan Schmidt 
69888c340SDavid Sterba #ifndef BTRFS_BACKREF_H
79888c340SDavid Sterba #define BTRFS_BACKREF_H
8a542ad1bSJan Schmidt 
955e301fdSFilipe Brandenburger #include <linux/btrfs.h>
10*9b569ea0SJosef Bacik #include "messages.h"
118da6d581SJan Schmidt #include "ulist.h"
12741188d3SQu Wenruo #include "disk-io.h"
1391cb916cSAlexander Block #include "extent_io.h"
14a542ad1bSJan Schmidt 
15a542ad1bSJan Schmidt struct inode_fs_paths {
16a542ad1bSJan Schmidt 	struct btrfs_path		*btrfs_path;
17a542ad1bSJan Schmidt 	struct btrfs_root		*fs_root;
18a542ad1bSJan Schmidt 	struct btrfs_data_container	*fspath;
19a542ad1bSJan Schmidt };
20a542ad1bSJan Schmidt 
2112a824dcSFilipe Manana struct btrfs_backref_shared_cache_entry {
2212a824dcSFilipe Manana 	u64 bytenr;
2312a824dcSFilipe Manana 	u64 gen;
2412a824dcSFilipe Manana 	bool is_shared;
2512a824dcSFilipe Manana };
2612a824dcSFilipe Manana 
2773e339e6SFilipe Manana #define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8
2873e339e6SFilipe Manana 
2961dbb952SFilipe Manana struct btrfs_backref_share_check_ctx {
3084a7949dSFilipe Manana 	/* Ulists used during backref walking. */
3184a7949dSFilipe Manana 	struct ulist refs;
3212a824dcSFilipe Manana 	/*
33877c1476SFilipe Manana 	 * The current leaf the caller of btrfs_is_data_extent_shared() is at.
34877c1476SFilipe Manana 	 * Typically the caller (at the moment only fiemap) tries to determine
35877c1476SFilipe Manana 	 * the sharedness of data extents point by file extent items from entire
36877c1476SFilipe Manana 	 * leaves.
37877c1476SFilipe Manana 	 */
38877c1476SFilipe Manana 	u64 curr_leaf_bytenr;
39877c1476SFilipe Manana 	/*
40877c1476SFilipe Manana 	 * The previous leaf the caller was at in the previous call to
41877c1476SFilipe Manana 	 * btrfs_is_data_extent_shared(). This may be the same as the current
42877c1476SFilipe Manana 	 * leaf. On the first call it must be 0.
43877c1476SFilipe Manana 	 */
44877c1476SFilipe Manana 	u64 prev_leaf_bytenr;
45877c1476SFilipe Manana 	/*
4612a824dcSFilipe Manana 	 * A path from a root to a leaf that has a file extent item pointing to
4712a824dcSFilipe Manana 	 * a given data extent should never exceed the maximum b+tree height.
4812a824dcSFilipe Manana 	 */
4961dbb952SFilipe Manana 	struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL];
5061dbb952SFilipe Manana 	bool use_path_cache;
5173e339e6SFilipe Manana 	/*
5273e339e6SFilipe Manana 	 * Cache the sharedness result for the last few extents we have found,
5373e339e6SFilipe Manana 	 * but only for extents for which we have multiple file extent items
5473e339e6SFilipe Manana 	 * that point to them.
5573e339e6SFilipe Manana 	 * It's very common to have several file extent items that point to the
5673e339e6SFilipe Manana 	 * same extent (bytenr) but with different offsets and lengths. This
5773e339e6SFilipe Manana 	 * typically happens for COW writes, partial writes into prealloc
5873e339e6SFilipe Manana 	 * extents, NOCOW writes after snapshoting a root, hole punching or
5973e339e6SFilipe Manana 	 * reflinking within the same file (less common perhaps).
6073e339e6SFilipe Manana 	 * So keep a small cache with the lookup results for the extent pointed
6173e339e6SFilipe Manana 	 * by the last few file extent items. This cache is checked, with a
6273e339e6SFilipe Manana 	 * linear scan, whenever btrfs_is_data_extent_shared() is called, so
6373e339e6SFilipe Manana 	 * it must be small so that it does not negatively affect performance in
6473e339e6SFilipe Manana 	 * case we don't have multiple file extent items that point to the same
6573e339e6SFilipe Manana 	 * data extent.
6673e339e6SFilipe Manana 	 */
6773e339e6SFilipe Manana 	struct {
6873e339e6SFilipe Manana 		u64 bytenr;
6973e339e6SFilipe Manana 		bool is_shared;
7073e339e6SFilipe Manana 	} prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE];
7173e339e6SFilipe Manana 	/*
7273e339e6SFilipe Manana 	 * The slot in the prev_extents_cache array that will be used for
7373e339e6SFilipe Manana 	 * storing the sharedness result of a new data extent.
7473e339e6SFilipe Manana 	 */
7573e339e6SFilipe Manana 	int prev_extents_cache_slot;
7612a824dcSFilipe Manana };
7712a824dcSFilipe Manana 
78a542ad1bSJan Schmidt typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 root,
79a542ad1bSJan Schmidt 		void *ctx);
80a542ad1bSJan Schmidt 
8184a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void);
8284a7949dSFilipe Manana void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx);
8384a7949dSFilipe Manana 
84a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
8569917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
8669917e43SLiu Bo 			u64 *flags);
87a542ad1bSJan Schmidt 
88a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
896eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
906eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level);
91a542ad1bSJan Schmidt 
92a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
93a542ad1bSJan Schmidt 				u64 extent_item_objectid,
947a3ae2f8SJan Schmidt 				u64 extent_offset, int search_commit_root,
95c995ab3cSZygo Blaxell 				iterate_extent_inodes_t *iterate, void *ctx,
96c995ab3cSZygo Blaxell 				bool ignore_offset);
97a542ad1bSJan Schmidt 
98a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
99e3059ec0SDavid Sterba 				struct btrfs_path *path, void *ctx,
100c995ab3cSZygo Blaxell 				bool ignore_offset);
101a542ad1bSJan Schmidt 
102a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
103a542ad1bSJan Schmidt 
10419b546d7SQu Wenruo int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
10519b546d7SQu Wenruo 			 struct btrfs_fs_info *fs_info, u64 bytenr,
10619b546d7SQu Wenruo 			 u64 time_seq, struct ulist **leafs,
10719b546d7SQu Wenruo 			 const u64 *extent_item_pos, bool ignore_offset);
1088da6d581SJan Schmidt int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
1098da6d581SJan Schmidt 			 struct btrfs_fs_info *fs_info, u64 bytenr,
110c7bcbb21SFilipe Manana 			 u64 time_seq, struct ulist **roots,
1118949b9a1SFilipe Manana 			 bool skip_commit_root_sem);
11296b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
11396b5bd77SJan Schmidt 			u32 name_len, unsigned long name_off,
11496b5bd77SJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
11596b5bd77SJan Schmidt 			char *dest, u32 size);
1168da6d581SJan Schmidt 
117a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes);
118a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
119a542ad1bSJan Schmidt 					struct btrfs_path *path);
120a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath);
121a542ad1bSJan Schmidt 
122f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
123f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
124f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
125f186373fSMark Fasheh 			  u64 *found_off);
126ceb707daSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr,
127b8f164e3SFilipe Manana 				u64 extent_gen,
12861dbb952SFilipe Manana 				struct btrfs_backref_share_check_ctx *ctx);
129f186373fSMark Fasheh 
130b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void);
131e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void);
13200142756SJeff Mahoney 
13300142756SJeff Mahoney struct prelim_ref {
13400142756SJeff Mahoney 	struct rb_node rbnode;
13500142756SJeff Mahoney 	u64 root_id;
13600142756SJeff Mahoney 	struct btrfs_key key_for_search;
13700142756SJeff Mahoney 	int level;
13800142756SJeff Mahoney 	int count;
13900142756SJeff Mahoney 	struct extent_inode_elem *inode_list;
14000142756SJeff Mahoney 	u64 parent;
14100142756SJeff Mahoney 	u64 wanted_disk_byte;
14200142756SJeff Mahoney };
14300142756SJeff Mahoney 
144a37f232bSQu Wenruo /*
145a37f232bSQu Wenruo  * Iterate backrefs of one extent.
146a37f232bSQu Wenruo  *
147a37f232bSQu Wenruo  * Now it only supports iteration of tree block in commit root.
148a37f232bSQu Wenruo  */
149a37f232bSQu Wenruo struct btrfs_backref_iter {
150a37f232bSQu Wenruo 	u64 bytenr;
151a37f232bSQu Wenruo 	struct btrfs_path *path;
152a37f232bSQu Wenruo 	struct btrfs_fs_info *fs_info;
153a37f232bSQu Wenruo 	struct btrfs_key cur_key;
154a37f232bSQu Wenruo 	u32 item_ptr;
155a37f232bSQu Wenruo 	u32 cur_ptr;
156a37f232bSQu Wenruo 	u32 end_ptr;
157a37f232bSQu Wenruo };
158a37f232bSQu Wenruo 
159a37f232bSQu Wenruo struct btrfs_backref_iter *btrfs_backref_iter_alloc(
160a37f232bSQu Wenruo 		struct btrfs_fs_info *fs_info, gfp_t gfp_flag);
161a37f232bSQu Wenruo 
162a37f232bSQu Wenruo static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
163a37f232bSQu Wenruo {
164a37f232bSQu Wenruo 	if (!iter)
165a37f232bSQu Wenruo 		return;
166a37f232bSQu Wenruo 	btrfs_free_path(iter->path);
167a37f232bSQu Wenruo 	kfree(iter);
168a37f232bSQu Wenruo }
169a37f232bSQu Wenruo 
170c39c2ddcSQu Wenruo static inline struct extent_buffer *btrfs_backref_get_eb(
171c39c2ddcSQu Wenruo 		struct btrfs_backref_iter *iter)
172c39c2ddcSQu Wenruo {
173c39c2ddcSQu Wenruo 	if (!iter)
174c39c2ddcSQu Wenruo 		return NULL;
175c39c2ddcSQu Wenruo 	return iter->path->nodes[0];
176c39c2ddcSQu Wenruo }
177c39c2ddcSQu Wenruo 
178c39c2ddcSQu Wenruo /*
179c39c2ddcSQu Wenruo  * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
180c39c2ddcSQu Wenruo  * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
181c39c2ddcSQu Wenruo  *
182c39c2ddcSQu Wenruo  * This helper determines if that's the case.
183c39c2ddcSQu Wenruo  */
184c39c2ddcSQu Wenruo static inline bool btrfs_backref_has_tree_block_info(
185c39c2ddcSQu Wenruo 		struct btrfs_backref_iter *iter)
186c39c2ddcSQu Wenruo {
187c39c2ddcSQu Wenruo 	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
188c39c2ddcSQu Wenruo 	    iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
189c39c2ddcSQu Wenruo 		return true;
190c39c2ddcSQu Wenruo 	return false;
191c39c2ddcSQu Wenruo }
192c39c2ddcSQu Wenruo 
193a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
194a37f232bSQu Wenruo 
195c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
196c39c2ddcSQu Wenruo 
197c39c2ddcSQu Wenruo static inline bool btrfs_backref_iter_is_inline_ref(
198c39c2ddcSQu Wenruo 		struct btrfs_backref_iter *iter)
199c39c2ddcSQu Wenruo {
200c39c2ddcSQu Wenruo 	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
201c39c2ddcSQu Wenruo 	    iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
202c39c2ddcSQu Wenruo 		return true;
203c39c2ddcSQu Wenruo 	return false;
204c39c2ddcSQu Wenruo }
205c39c2ddcSQu Wenruo 
206a37f232bSQu Wenruo static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
207a37f232bSQu Wenruo {
208a37f232bSQu Wenruo 	iter->bytenr = 0;
209a37f232bSQu Wenruo 	iter->item_ptr = 0;
210a37f232bSQu Wenruo 	iter->cur_ptr = 0;
211a37f232bSQu Wenruo 	iter->end_ptr = 0;
212a37f232bSQu Wenruo 	btrfs_release_path(iter->path);
213a37f232bSQu Wenruo 	memset(&iter->cur_key, 0, sizeof(iter->cur_key));
214a37f232bSQu Wenruo }
215a37f232bSQu Wenruo 
21670535441SQu Wenruo /*
21770535441SQu Wenruo  * Backref cache related structures
21870535441SQu Wenruo  *
21970535441SQu Wenruo  * The whole objective of backref_cache is to build a bi-directional map
22070535441SQu Wenruo  * of tree blocks (represented by backref_node) and all their parents.
22170535441SQu Wenruo  */
22270535441SQu Wenruo 
22370535441SQu Wenruo /*
22470535441SQu Wenruo  * Represent a tree block in the backref cache
22570535441SQu Wenruo  */
22670535441SQu Wenruo struct btrfs_backref_node {
227e9a28dc5SQu Wenruo 	struct {
22870535441SQu Wenruo 		struct rb_node rb_node;
22970535441SQu Wenruo 		u64 bytenr;
230e9a28dc5SQu Wenruo 	}; /* Use rb_simple_node for search/insert */
23170535441SQu Wenruo 
23270535441SQu Wenruo 	u64 new_bytenr;
23370535441SQu Wenruo 	/* Objectid of tree block owner, can be not uptodate */
23470535441SQu Wenruo 	u64 owner;
23570535441SQu Wenruo 	/* Link to pending, changed or detached list */
23670535441SQu Wenruo 	struct list_head list;
23770535441SQu Wenruo 
23870535441SQu Wenruo 	/* List of upper level edges, which link this node to its parents */
23970535441SQu Wenruo 	struct list_head upper;
24070535441SQu Wenruo 	/* List of lower level edges, which link this node to its children */
24170535441SQu Wenruo 	struct list_head lower;
24270535441SQu Wenruo 
24370535441SQu Wenruo 	/* NULL if this node is not tree root */
24470535441SQu Wenruo 	struct btrfs_root *root;
24570535441SQu Wenruo 	/* Extent buffer got by COWing the block */
24670535441SQu Wenruo 	struct extent_buffer *eb;
24770535441SQu Wenruo 	/* Level of the tree block */
24870535441SQu Wenruo 	unsigned int level:8;
24992a7cc42SQu Wenruo 	/* Is the block in a non-shareable tree */
25070535441SQu Wenruo 	unsigned int cowonly:1;
25170535441SQu Wenruo 	/* 1 if no child node is in the cache */
25270535441SQu Wenruo 	unsigned int lowest:1;
25370535441SQu Wenruo 	/* Is the extent buffer locked */
25470535441SQu Wenruo 	unsigned int locked:1;
25570535441SQu Wenruo 	/* Has the block been processed */
25670535441SQu Wenruo 	unsigned int processed:1;
25770535441SQu Wenruo 	/* Have backrefs of this block been checked */
25870535441SQu Wenruo 	unsigned int checked:1;
25970535441SQu Wenruo 	/*
26070535441SQu Wenruo 	 * 1 if corresponding block has been COWed but some upper level block
26170535441SQu Wenruo 	 * pointers may not point to the new location
26270535441SQu Wenruo 	 */
26370535441SQu Wenruo 	unsigned int pending:1;
26470535441SQu Wenruo 	/* 1 if the backref node isn't connected to any other backref node */
26570535441SQu Wenruo 	unsigned int detached:1;
26670535441SQu Wenruo 
26770535441SQu Wenruo 	/*
26870535441SQu Wenruo 	 * For generic purpose backref cache, where we only care if it's a reloc
26970535441SQu Wenruo 	 * root, doesn't care the source subvolid.
27070535441SQu Wenruo 	 */
27170535441SQu Wenruo 	unsigned int is_reloc_root:1;
27270535441SQu Wenruo };
27370535441SQu Wenruo 
27470535441SQu Wenruo #define LOWER	0
27570535441SQu Wenruo #define UPPER	1
27670535441SQu Wenruo 
27770535441SQu Wenruo /*
27870535441SQu Wenruo  * Represent an edge connecting upper and lower backref nodes.
27970535441SQu Wenruo  */
28070535441SQu Wenruo struct btrfs_backref_edge {
28170535441SQu Wenruo 	/*
28270535441SQu Wenruo 	 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
28370535441SQu Wenruo 	 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
28470535441SQu Wenruo 	 * upper level node.
28570535441SQu Wenruo 	 *
28670535441SQu Wenruo 	 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
28770535441SQu Wenruo 	 * linking list[UPPER] to its upper level nodes.
28870535441SQu Wenruo 	 */
28970535441SQu Wenruo 	struct list_head list[2];
29070535441SQu Wenruo 
29170535441SQu Wenruo 	/* Two related nodes */
29270535441SQu Wenruo 	struct btrfs_backref_node *node[2];
29370535441SQu Wenruo };
29470535441SQu Wenruo 
29570535441SQu Wenruo struct btrfs_backref_cache {
29670535441SQu Wenruo 	/* Red black tree of all backref nodes in the cache */
29770535441SQu Wenruo 	struct rb_root rb_root;
29870535441SQu Wenruo 	/* For passing backref nodes to btrfs_reloc_cow_block */
29970535441SQu Wenruo 	struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
30070535441SQu Wenruo 	/*
30170535441SQu Wenruo 	 * List of blocks that have been COWed but some block pointers in upper
30270535441SQu Wenruo 	 * level blocks may not reflect the new location
30370535441SQu Wenruo 	 */
30470535441SQu Wenruo 	struct list_head pending[BTRFS_MAX_LEVEL];
30570535441SQu Wenruo 	/* List of backref nodes with no child node */
30670535441SQu Wenruo 	struct list_head leaves;
30770535441SQu Wenruo 	/* List of blocks that have been COWed in current transaction */
30870535441SQu Wenruo 	struct list_head changed;
30970535441SQu Wenruo 	/* List of detached backref node. */
31070535441SQu Wenruo 	struct list_head detached;
31170535441SQu Wenruo 
31270535441SQu Wenruo 	u64 last_trans;
31370535441SQu Wenruo 
31470535441SQu Wenruo 	int nr_nodes;
31570535441SQu Wenruo 	int nr_edges;
31670535441SQu Wenruo 
31770535441SQu Wenruo 	/* List of unchecked backref edges during backref cache build */
31870535441SQu Wenruo 	struct list_head pending_edge;
31970535441SQu Wenruo 
32070535441SQu Wenruo 	/* List of useless backref nodes during backref cache build */
32170535441SQu Wenruo 	struct list_head useless_node;
32270535441SQu Wenruo 
32370535441SQu Wenruo 	struct btrfs_fs_info *fs_info;
32470535441SQu Wenruo 
32570535441SQu Wenruo 	/*
32670535441SQu Wenruo 	 * Whether this cache is for relocation
32770535441SQu Wenruo 	 *
32870535441SQu Wenruo 	 * Reloction backref cache require more info for reloc root compared
32970535441SQu Wenruo 	 * to generic backref cache.
33070535441SQu Wenruo 	 */
33170535441SQu Wenruo 	unsigned int is_reloc;
33270535441SQu Wenruo };
33370535441SQu Wenruo 
334584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
335584fb121SQu Wenruo 			      struct btrfs_backref_cache *cache, int is_reloc);
336b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node(
337b1818dabSQu Wenruo 		struct btrfs_backref_cache *cache, u64 bytenr, int level);
33847254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge(
33947254d07SQu Wenruo 		struct btrfs_backref_cache *cache);
340584fb121SQu Wenruo 
341f39911e5SQu Wenruo #define		LINK_LOWER	(1 << 0)
342f39911e5SQu Wenruo #define		LINK_UPPER	(1 << 1)
343f39911e5SQu Wenruo static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
344f39911e5SQu Wenruo 					   struct btrfs_backref_node *lower,
345f39911e5SQu Wenruo 					   struct btrfs_backref_node *upper,
346f39911e5SQu Wenruo 					   int link_which)
347f39911e5SQu Wenruo {
348f39911e5SQu Wenruo 	ASSERT(upper && lower && upper->level == lower->level + 1);
349f39911e5SQu Wenruo 	edge->node[LOWER] = lower;
350f39911e5SQu Wenruo 	edge->node[UPPER] = upper;
351f39911e5SQu Wenruo 	if (link_which & LINK_LOWER)
352f39911e5SQu Wenruo 		list_add_tail(&edge->list[LOWER], &lower->upper);
353f39911e5SQu Wenruo 	if (link_which & LINK_UPPER)
354f39911e5SQu Wenruo 		list_add_tail(&edge->list[UPPER], &upper->lower);
355f39911e5SQu Wenruo }
356f39911e5SQu Wenruo 
357741188d3SQu Wenruo static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
358741188d3SQu Wenruo 					   struct btrfs_backref_node *node)
359741188d3SQu Wenruo {
360741188d3SQu Wenruo 	if (node) {
361eddda68dSJosef Bacik 		ASSERT(list_empty(&node->list));
362eddda68dSJosef Bacik 		ASSERT(list_empty(&node->lower));
363eddda68dSJosef Bacik 		ASSERT(node->eb == NULL);
364741188d3SQu Wenruo 		cache->nr_nodes--;
365741188d3SQu Wenruo 		btrfs_put_root(node->root);
366741188d3SQu Wenruo 		kfree(node);
367741188d3SQu Wenruo 	}
368741188d3SQu Wenruo }
369741188d3SQu Wenruo 
370741188d3SQu Wenruo static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
371741188d3SQu Wenruo 					   struct btrfs_backref_edge *edge)
372741188d3SQu Wenruo {
373741188d3SQu Wenruo 	if (edge) {
374741188d3SQu Wenruo 		cache->nr_edges--;
375741188d3SQu Wenruo 		kfree(edge);
376741188d3SQu Wenruo 	}
377741188d3SQu Wenruo }
378741188d3SQu Wenruo 
379b0fe7078SQu Wenruo static inline void btrfs_backref_unlock_node_buffer(
380b0fe7078SQu Wenruo 		struct btrfs_backref_node *node)
381b0fe7078SQu Wenruo {
382b0fe7078SQu Wenruo 	if (node->locked) {
383b0fe7078SQu Wenruo 		btrfs_tree_unlock(node->eb);
384b0fe7078SQu Wenruo 		node->locked = 0;
385b0fe7078SQu Wenruo 	}
386b0fe7078SQu Wenruo }
387b0fe7078SQu Wenruo 
388b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node_buffer(
389b0fe7078SQu Wenruo 		struct btrfs_backref_node *node)
390b0fe7078SQu Wenruo {
391b0fe7078SQu Wenruo 	if (node->eb) {
392b0fe7078SQu Wenruo 		btrfs_backref_unlock_node_buffer(node);
393b0fe7078SQu Wenruo 		free_extent_buffer(node->eb);
394b0fe7078SQu Wenruo 		node->eb = NULL;
395b0fe7078SQu Wenruo 	}
396b0fe7078SQu Wenruo }
397b0fe7078SQu Wenruo 
398b0fe7078SQu Wenruo /*
399b0fe7078SQu Wenruo  * Drop the backref node from cache without cleaning up its children
400b0fe7078SQu Wenruo  * edges.
401b0fe7078SQu Wenruo  *
402b0fe7078SQu Wenruo  * This can only be called on node without parent edges.
403b0fe7078SQu Wenruo  * The children edges are still kept as is.
404b0fe7078SQu Wenruo  */
405b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
406b0fe7078SQu Wenruo 					   struct btrfs_backref_node *node)
407b0fe7078SQu Wenruo {
408eddda68dSJosef Bacik 	ASSERT(list_empty(&node->upper));
409b0fe7078SQu Wenruo 
410b0fe7078SQu Wenruo 	btrfs_backref_drop_node_buffer(node);
411eddda68dSJosef Bacik 	list_del_init(&node->list);
412eddda68dSJosef Bacik 	list_del_init(&node->lower);
413b0fe7078SQu Wenruo 	if (!RB_EMPTY_NODE(&node->rb_node))
414b0fe7078SQu Wenruo 		rb_erase(&node->rb_node, &tree->rb_root);
415b0fe7078SQu Wenruo 	btrfs_backref_free_node(tree, node);
416b0fe7078SQu Wenruo }
417b0fe7078SQu Wenruo 
418023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
419023acb07SQu Wenruo 				struct btrfs_backref_node *node);
420023acb07SQu Wenruo 
42113fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
42213fe1bdbSQu Wenruo 
423982c92cbSQu Wenruo static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
424982c92cbSQu Wenruo 				       u64 bytenr, int errno)
425982c92cbSQu Wenruo {
426982c92cbSQu Wenruo 	btrfs_panic(fs_info, errno,
427982c92cbSQu Wenruo 		    "Inconsistency in backref cache found at offset %llu",
428982c92cbSQu Wenruo 		    bytenr);
429982c92cbSQu Wenruo }
430982c92cbSQu Wenruo 
4311b60d2ecSQu Wenruo int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
4321b60d2ecSQu Wenruo 				struct btrfs_path *path,
4331b60d2ecSQu Wenruo 				struct btrfs_backref_iter *iter,
4341b60d2ecSQu Wenruo 				struct btrfs_key *node_key,
4351b60d2ecSQu Wenruo 				struct btrfs_backref_node *cur);
4361b60d2ecSQu Wenruo 
437fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
438fc997ed0SQu Wenruo 				     struct btrfs_backref_node *start);
439fc997ed0SQu Wenruo 
4401b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
4411b23ea18SQu Wenruo 				 struct btrfs_backref_node *node);
4421b23ea18SQu Wenruo 
443a542ad1bSJan Schmidt #endif
444