xref: /openbmc/linux/fs/btrfs/backref.h (revision eb96e221)
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>
109b569ea0SJosef Bacik #include "messages.h"
118da6d581SJan Schmidt #include "ulist.h"
12741188d3SQu Wenruo #include "disk-io.h"
1391cb916cSAlexander Block #include "extent_io.h"
14a542ad1bSJan Schmidt 
156ce6ba53SFilipe Manana /*
1688ffb665SFilipe Manana  * Used by implementations of iterate_extent_inodes_t (see definition below) to
1788ffb665SFilipe Manana  * signal that backref iteration can stop immediately and no error happened.
1888ffb665SFilipe Manana  * The value must be non-negative and must not be 0, 1 (which is a common return
1988ffb665SFilipe Manana  * value from things like btrfs_search_slot() and used internally in the backref
2088ffb665SFilipe Manana  * walking code) and different from BACKREF_FOUND_SHARED and
2188ffb665SFilipe Manana  * BACKREF_FOUND_NOT_SHARED
2288ffb665SFilipe Manana  */
2388ffb665SFilipe Manana #define BTRFS_ITERATE_EXTENT_INODES_STOP 5
2488ffb665SFilipe Manana 
2588ffb665SFilipe Manana /*
2688ffb665SFilipe Manana  * Should return 0 if no errors happened and iteration of backrefs should
2788ffb665SFilipe Manana  * continue. Can return BTRFS_ITERATE_EXTENT_INODES_STOP or any other non-zero
2888ffb665SFilipe Manana  * value to immediately stop iteration and possibly signal an error back to
2988ffb665SFilipe Manana  * the caller.
3088ffb665SFilipe Manana  */
3188ffb665SFilipe Manana typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 num_bytes,
3288ffb665SFilipe Manana 				      u64 root, void *ctx);
3388ffb665SFilipe Manana 
3488ffb665SFilipe Manana /*
35a2c8d27eSFilipe Manana  * Context and arguments for backref walking functions. Some of the fields are
36a2c8d27eSFilipe Manana  * to be filled by the caller of such functions while other are filled by the
37a2c8d27eSFilipe Manana  * functions themselves, as described below.
386ce6ba53SFilipe Manana  */
39a2c8d27eSFilipe Manana struct btrfs_backref_walk_ctx {
40a2c8d27eSFilipe Manana 	/*
41a2c8d27eSFilipe Manana 	 * The address of the extent for which we are doing backref walking.
42a2c8d27eSFilipe Manana 	 * Can be either a data extent or a metadata extent.
43a2c8d27eSFilipe Manana 	 *
44a2c8d27eSFilipe Manana 	 * Must always be set by the top level caller.
45a2c8d27eSFilipe Manana 	 */
46a2c8d27eSFilipe Manana 	u64 bytenr;
47a2c8d27eSFilipe Manana 	/*
48a2c8d27eSFilipe Manana 	 * Offset relative to the target extent. This is only used for data
49a2c8d27eSFilipe Manana 	 * extents, and it's meaningful because we can have file extent items
50a2c8d27eSFilipe Manana 	 * that point only to a section of a data extent ("bookend" extents),
51a2c8d27eSFilipe Manana 	 * and we want to filter out any that don't point to a section of the
52a2c8d27eSFilipe Manana 	 * data extent containing the given offset.
53a2c8d27eSFilipe Manana 	 *
54a2c8d27eSFilipe Manana 	 * Must always be set by the top level caller.
55a2c8d27eSFilipe Manana 	 */
56a2c8d27eSFilipe Manana 	u64 extent_item_pos;
57a2c8d27eSFilipe Manana 	/*
58a2c8d27eSFilipe Manana 	 * If true and bytenr corresponds to a data extent, then references from
59a2c8d27eSFilipe Manana 	 * all file extent items that point to the data extent are considered,
60a2c8d27eSFilipe Manana 	 * @extent_item_pos is ignored.
61a2c8d27eSFilipe Manana 	 */
62a2c8d27eSFilipe Manana 	bool ignore_extent_item_pos;
630cad8f14SFilipe Manana 	/*
640cad8f14SFilipe Manana 	 * If true and bytenr corresponds to a data extent, then the inode list
650cad8f14SFilipe Manana 	 * (each member describing inode number, file offset and root) is not
660cad8f14SFilipe Manana 	 * added to each reference added to the @refs ulist.
670cad8f14SFilipe Manana 	 */
680cad8f14SFilipe Manana 	bool skip_inode_ref_list;
69a2c8d27eSFilipe Manana 	/* A valid transaction handle or NULL. */
70a2c8d27eSFilipe Manana 	struct btrfs_trans_handle *trans;
71a2c8d27eSFilipe Manana 	/*
72a2c8d27eSFilipe Manana 	 * The file system's info object, can not be NULL.
73a2c8d27eSFilipe Manana 	 *
74a2c8d27eSFilipe Manana 	 * Must always be set by the top level caller.
75a2c8d27eSFilipe Manana 	 */
76a2c8d27eSFilipe Manana 	struct btrfs_fs_info *fs_info;
77a2c8d27eSFilipe Manana 	/*
78a2c8d27eSFilipe Manana 	 * Time sequence acquired from btrfs_get_tree_mod_seq(), in case the
79a2c8d27eSFilipe Manana 	 * caller joined the tree mod log to get a consistent view of b+trees
80a2c8d27eSFilipe Manana 	 * while we do backref walking, or BTRFS_SEQ_LAST.
81a2c8d27eSFilipe Manana 	 * When using BTRFS_SEQ_LAST, delayed refs are not checked and it uses
82a2c8d27eSFilipe Manana 	 * commit roots when searching b+trees - this is a special case for
83a2c8d27eSFilipe Manana 	 * qgroups used during a transaction commit.
84a2c8d27eSFilipe Manana 	 */
85a2c8d27eSFilipe Manana 	u64 time_seq;
86a2c8d27eSFilipe Manana 	/*
87a2c8d27eSFilipe Manana 	 * Used to collect the bytenr of metadata extents that point to the
88a2c8d27eSFilipe Manana 	 * target extent.
89a2c8d27eSFilipe Manana 	 */
90a2c8d27eSFilipe Manana 	struct ulist *refs;
91a2c8d27eSFilipe Manana 	/*
92a2c8d27eSFilipe Manana 	 * List used to collect the IDs of the roots from which the target
93a2c8d27eSFilipe Manana 	 * extent is accessible. Can be NULL in case the caller does not care
94a2c8d27eSFilipe Manana 	 * about collecting root IDs.
95a2c8d27eSFilipe Manana 	 */
96a2c8d27eSFilipe Manana 	struct ulist *roots;
9766d04209SFilipe Manana 	/*
9888ffb665SFilipe Manana 	 * Used by iterate_extent_inodes() and the main backref walk code
9988ffb665SFilipe Manana 	 * (find_parent_nodes()). Lookup and store functions for an optional
10088ffb665SFilipe Manana 	 * cache which maps the logical address (bytenr) of leaves to an array
10188ffb665SFilipe Manana 	 * of root IDs.
10266d04209SFilipe Manana 	 */
10366d04209SFilipe Manana 	bool (*cache_lookup)(u64 leaf_bytenr, void *user_ctx,
10466d04209SFilipe Manana 			     const u64 **root_ids_ret, int *root_count_ret);
10566d04209SFilipe Manana 	void (*cache_store)(u64 leaf_bytenr, const struct ulist *root_ids,
10666d04209SFilipe Manana 			    void *user_ctx);
10788ffb665SFilipe Manana 	/*
10888ffb665SFilipe Manana 	 * If this is not NULL, then the backref walking code will call this
10988ffb665SFilipe Manana 	 * for each indirect data extent reference as soon as it finds one,
11088ffb665SFilipe Manana 	 * before collecting all the remaining backrefs and before resolving
11188ffb665SFilipe Manana 	 * indirect backrefs. This allows for the caller to terminate backref
11288ffb665SFilipe Manana 	 * walking as soon as it finds one backref that matches some specific
11388ffb665SFilipe Manana 	 * criteria. The @cache_lookup and @cache_store callbacks should not
11488ffb665SFilipe Manana 	 * be NULL in order to use this callback.
11588ffb665SFilipe Manana 	 */
11688ffb665SFilipe Manana 	iterate_extent_inodes_t *indirect_ref_iterator;
11788ffb665SFilipe Manana 	/*
118f73853c7SFilipe Manana 	 * If this is not NULL, then the backref walking code will call this for
119f73853c7SFilipe Manana 	 * each extent item it's meant to process before it actually starts
120f73853c7SFilipe Manana 	 * processing it. If this returns anything other than 0, then it stops
121f73853c7SFilipe Manana 	 * the backref walking code immediately.
12288ffb665SFilipe Manana 	 */
123f73853c7SFilipe Manana 	int (*check_extent_item)(u64 bytenr, const struct btrfs_extent_item *ei,
124f73853c7SFilipe Manana 				 const struct extent_buffer *leaf, void *user_ctx);
125adf02418SFilipe Manana 	/*
126adf02418SFilipe Manana 	 * If this is not NULL, then the backref walking code will call this for
127adf02418SFilipe Manana 	 * each extent data ref it finds (BTRFS_EXTENT_DATA_REF_KEY keys) before
128adf02418SFilipe Manana 	 * processing that data ref. If this callback return false, then it will
129adf02418SFilipe Manana 	 * ignore this data ref and it will never resolve the indirect data ref,
130adf02418SFilipe Manana 	 * saving time searching for leaves in a fs tree with file extent items
131adf02418SFilipe Manana 	 * matching the data ref.
132adf02418SFilipe Manana 	 */
133adf02418SFilipe Manana 	bool (*skip_data_ref)(u64 root, u64 ino, u64 offset, void *user_ctx);
134f73853c7SFilipe Manana 	/* Context object to pass to the callbacks defined above. */
13566d04209SFilipe Manana 	void *user_ctx;
136a2c8d27eSFilipe Manana };
1376ce6ba53SFilipe Manana 
138a542ad1bSJan Schmidt struct inode_fs_paths {
139a542ad1bSJan Schmidt 	struct btrfs_path		*btrfs_path;
140a542ad1bSJan Schmidt 	struct btrfs_root		*fs_root;
141a542ad1bSJan Schmidt 	struct btrfs_data_container	*fspath;
142a542ad1bSJan Schmidt };
143a542ad1bSJan Schmidt 
14412a824dcSFilipe Manana struct btrfs_backref_shared_cache_entry {
14512a824dcSFilipe Manana 	u64 bytenr;
14612a824dcSFilipe Manana 	u64 gen;
14712a824dcSFilipe Manana 	bool is_shared;
14812a824dcSFilipe Manana };
14912a824dcSFilipe Manana 
15073e339e6SFilipe Manana #define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8
15173e339e6SFilipe Manana 
15261dbb952SFilipe Manana struct btrfs_backref_share_check_ctx {
15384a7949dSFilipe Manana 	/* Ulists used during backref walking. */
15484a7949dSFilipe Manana 	struct ulist refs;
15512a824dcSFilipe Manana 	/*
156877c1476SFilipe Manana 	 * The current leaf the caller of btrfs_is_data_extent_shared() is at.
157877c1476SFilipe Manana 	 * Typically the caller (at the moment only fiemap) tries to determine
158877c1476SFilipe Manana 	 * the sharedness of data extents point by file extent items from entire
159877c1476SFilipe Manana 	 * leaves.
160877c1476SFilipe Manana 	 */
161877c1476SFilipe Manana 	u64 curr_leaf_bytenr;
162877c1476SFilipe Manana 	/*
163877c1476SFilipe Manana 	 * The previous leaf the caller was at in the previous call to
164877c1476SFilipe Manana 	 * btrfs_is_data_extent_shared(). This may be the same as the current
165877c1476SFilipe Manana 	 * leaf. On the first call it must be 0.
166877c1476SFilipe Manana 	 */
167877c1476SFilipe Manana 	u64 prev_leaf_bytenr;
168877c1476SFilipe Manana 	/*
16912a824dcSFilipe Manana 	 * A path from a root to a leaf that has a file extent item pointing to
17012a824dcSFilipe Manana 	 * a given data extent should never exceed the maximum b+tree height.
17112a824dcSFilipe Manana 	 */
17261dbb952SFilipe Manana 	struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL];
17361dbb952SFilipe Manana 	bool use_path_cache;
17473e339e6SFilipe Manana 	/*
17573e339e6SFilipe Manana 	 * Cache the sharedness result for the last few extents we have found,
17673e339e6SFilipe Manana 	 * but only for extents for which we have multiple file extent items
17773e339e6SFilipe Manana 	 * that point to them.
17873e339e6SFilipe Manana 	 * It's very common to have several file extent items that point to the
17973e339e6SFilipe Manana 	 * same extent (bytenr) but with different offsets and lengths. This
18073e339e6SFilipe Manana 	 * typically happens for COW writes, partial writes into prealloc
18173e339e6SFilipe Manana 	 * extents, NOCOW writes after snapshoting a root, hole punching or
18273e339e6SFilipe Manana 	 * reflinking within the same file (less common perhaps).
18373e339e6SFilipe Manana 	 * So keep a small cache with the lookup results for the extent pointed
18473e339e6SFilipe Manana 	 * by the last few file extent items. This cache is checked, with a
18573e339e6SFilipe Manana 	 * linear scan, whenever btrfs_is_data_extent_shared() is called, so
18673e339e6SFilipe Manana 	 * it must be small so that it does not negatively affect performance in
18773e339e6SFilipe Manana 	 * case we don't have multiple file extent items that point to the same
18873e339e6SFilipe Manana 	 * data extent.
18973e339e6SFilipe Manana 	 */
19073e339e6SFilipe Manana 	struct {
19173e339e6SFilipe Manana 		u64 bytenr;
19273e339e6SFilipe Manana 		bool is_shared;
19373e339e6SFilipe Manana 	} prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE];
19473e339e6SFilipe Manana 	/*
19573e339e6SFilipe Manana 	 * The slot in the prev_extents_cache array that will be used for
19673e339e6SFilipe Manana 	 * storing the sharedness result of a new data extent.
19773e339e6SFilipe Manana 	 */
19873e339e6SFilipe Manana 	int prev_extents_cache_slot;
19912a824dcSFilipe Manana };
20012a824dcSFilipe Manana 
20184a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void);
20284a7949dSFilipe Manana void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx);
20384a7949dSFilipe Manana 
204a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
20569917e43SLiu Bo 			struct btrfs_path *path, struct btrfs_key *found_key,
20669917e43SLiu Bo 			u64 *flags);
207a542ad1bSJan Schmidt 
208a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
2096eda71d0SLiu Bo 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
2106eda71d0SLiu Bo 			    u32 item_size, u64 *out_root, u8 *out_level);
211a542ad1bSJan Schmidt 
212a2c8d27eSFilipe Manana int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx,
213a2c8d27eSFilipe Manana 			  bool search_commit_root,
214a2c8d27eSFilipe Manana 			  iterate_extent_inodes_t *iterate, void *user_ctx);
215a542ad1bSJan Schmidt 
216a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
217e3059ec0SDavid Sterba 				struct btrfs_path *path, void *ctx,
218c995ab3cSZygo Blaxell 				bool ignore_offset);
219a542ad1bSJan Schmidt 
220a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
221a542ad1bSJan Schmidt 
222a2c8d27eSFilipe Manana int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx);
223a2c8d27eSFilipe Manana int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx,
2248949b9a1SFilipe Manana 			 bool skip_commit_root_sem);
22596b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
22696b5bd77SJan Schmidt 			u32 name_len, unsigned long name_off,
22796b5bd77SJan Schmidt 			struct extent_buffer *eb_in, u64 parent,
22896b5bd77SJan Schmidt 			char *dest, u32 size);
2298da6d581SJan Schmidt 
230a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes);
231a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
232a542ad1bSJan Schmidt 					struct btrfs_path *path);
233a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath);
234a542ad1bSJan Schmidt 
235f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
236f186373fSMark Fasheh 			  u64 start_off, struct btrfs_path *path,
237f186373fSMark Fasheh 			  struct btrfs_inode_extref **ret_extref,
238f186373fSMark Fasheh 			  u64 *found_off);
239ceb707daSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr,
240b8f164e3SFilipe Manana 				u64 extent_gen,
24161dbb952SFilipe Manana 				struct btrfs_backref_share_check_ctx *ctx);
242f186373fSMark Fasheh 
243b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void);
244e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void);
24500142756SJeff Mahoney 
24600142756SJeff Mahoney struct prelim_ref {
24700142756SJeff Mahoney 	struct rb_node rbnode;
24800142756SJeff Mahoney 	u64 root_id;
24900142756SJeff Mahoney 	struct btrfs_key key_for_search;
25000142756SJeff Mahoney 	int level;
25100142756SJeff Mahoney 	int count;
25200142756SJeff Mahoney 	struct extent_inode_elem *inode_list;
25300142756SJeff Mahoney 	u64 parent;
25400142756SJeff Mahoney 	u64 wanted_disk_byte;
25500142756SJeff Mahoney };
25600142756SJeff Mahoney 
257a37f232bSQu Wenruo /*
258a37f232bSQu Wenruo  * Iterate backrefs of one extent.
259a37f232bSQu Wenruo  *
260a37f232bSQu Wenruo  * Now it only supports iteration of tree block in commit root.
261a37f232bSQu Wenruo  */
262a37f232bSQu Wenruo struct btrfs_backref_iter {
263a37f232bSQu Wenruo 	u64 bytenr;
264a37f232bSQu Wenruo 	struct btrfs_path *path;
265a37f232bSQu Wenruo 	struct btrfs_fs_info *fs_info;
266a37f232bSQu Wenruo 	struct btrfs_key cur_key;
267a37f232bSQu Wenruo 	u32 item_ptr;
268a37f232bSQu Wenruo 	u32 cur_ptr;
269a37f232bSQu Wenruo 	u32 end_ptr;
270a37f232bSQu Wenruo };
271a37f232bSQu Wenruo 
272d68194b2SDavid Sterba struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info);
273a37f232bSQu Wenruo 
btrfs_backref_iter_free(struct btrfs_backref_iter * iter)274a37f232bSQu Wenruo static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
275a37f232bSQu Wenruo {
276a37f232bSQu Wenruo 	if (!iter)
277a37f232bSQu Wenruo 		return;
278a37f232bSQu Wenruo 	btrfs_free_path(iter->path);
279a37f232bSQu Wenruo 	kfree(iter);
280a37f232bSQu Wenruo }
281a37f232bSQu Wenruo 
btrfs_backref_get_eb(struct btrfs_backref_iter * iter)282c39c2ddcSQu Wenruo static inline struct extent_buffer *btrfs_backref_get_eb(
283c39c2ddcSQu Wenruo 		struct btrfs_backref_iter *iter)
284c39c2ddcSQu Wenruo {
285c39c2ddcSQu Wenruo 	if (!iter)
286c39c2ddcSQu Wenruo 		return NULL;
287c39c2ddcSQu Wenruo 	return iter->path->nodes[0];
288c39c2ddcSQu Wenruo }
289c39c2ddcSQu Wenruo 
290c39c2ddcSQu Wenruo /*
291c39c2ddcSQu Wenruo  * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
292c39c2ddcSQu Wenruo  * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
293c39c2ddcSQu Wenruo  *
294c39c2ddcSQu Wenruo  * This helper determines if that's the case.
295c39c2ddcSQu Wenruo  */
btrfs_backref_has_tree_block_info(struct btrfs_backref_iter * iter)296c39c2ddcSQu Wenruo static inline bool btrfs_backref_has_tree_block_info(
297c39c2ddcSQu Wenruo 		struct btrfs_backref_iter *iter)
298c39c2ddcSQu Wenruo {
299c39c2ddcSQu Wenruo 	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
300c39c2ddcSQu Wenruo 	    iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
301c39c2ddcSQu Wenruo 		return true;
302c39c2ddcSQu Wenruo 	return false;
303c39c2ddcSQu Wenruo }
304c39c2ddcSQu Wenruo 
305a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
306a37f232bSQu Wenruo 
307c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
308c39c2ddcSQu Wenruo 
btrfs_backref_iter_is_inline_ref(struct btrfs_backref_iter * iter)309c39c2ddcSQu Wenruo static inline bool btrfs_backref_iter_is_inline_ref(
310c39c2ddcSQu Wenruo 		struct btrfs_backref_iter *iter)
311c39c2ddcSQu Wenruo {
312c39c2ddcSQu Wenruo 	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
313c39c2ddcSQu Wenruo 	    iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
314c39c2ddcSQu Wenruo 		return true;
315c39c2ddcSQu Wenruo 	return false;
316c39c2ddcSQu Wenruo }
317c39c2ddcSQu Wenruo 
btrfs_backref_iter_release(struct btrfs_backref_iter * iter)318a37f232bSQu Wenruo static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
319a37f232bSQu Wenruo {
320a37f232bSQu Wenruo 	iter->bytenr = 0;
321a37f232bSQu Wenruo 	iter->item_ptr = 0;
322a37f232bSQu Wenruo 	iter->cur_ptr = 0;
323a37f232bSQu Wenruo 	iter->end_ptr = 0;
324a37f232bSQu Wenruo 	btrfs_release_path(iter->path);
325a37f232bSQu Wenruo 	memset(&iter->cur_key, 0, sizeof(iter->cur_key));
326a37f232bSQu Wenruo }
327a37f232bSQu Wenruo 
32870535441SQu Wenruo /*
32970535441SQu Wenruo  * Backref cache related structures
33070535441SQu Wenruo  *
33170535441SQu Wenruo  * The whole objective of backref_cache is to build a bi-directional map
33270535441SQu Wenruo  * of tree blocks (represented by backref_node) and all their parents.
33370535441SQu Wenruo  */
33470535441SQu Wenruo 
33570535441SQu Wenruo /*
33670535441SQu Wenruo  * Represent a tree block in the backref cache
33770535441SQu Wenruo  */
33870535441SQu Wenruo struct btrfs_backref_node {
339e9a28dc5SQu Wenruo 	struct {
34070535441SQu Wenruo 		struct rb_node rb_node;
34170535441SQu Wenruo 		u64 bytenr;
342e9a28dc5SQu Wenruo 	}; /* Use rb_simple_node for search/insert */
34370535441SQu Wenruo 
34470535441SQu Wenruo 	u64 new_bytenr;
34570535441SQu Wenruo 	/* Objectid of tree block owner, can be not uptodate */
34670535441SQu Wenruo 	u64 owner;
34770535441SQu Wenruo 	/* Link to pending, changed or detached list */
34870535441SQu Wenruo 	struct list_head list;
34970535441SQu Wenruo 
35070535441SQu Wenruo 	/* List of upper level edges, which link this node to its parents */
35170535441SQu Wenruo 	struct list_head upper;
35270535441SQu Wenruo 	/* List of lower level edges, which link this node to its children */
35370535441SQu Wenruo 	struct list_head lower;
35470535441SQu Wenruo 
35570535441SQu Wenruo 	/* NULL if this node is not tree root */
35670535441SQu Wenruo 	struct btrfs_root *root;
35770535441SQu Wenruo 	/* Extent buffer got by COWing the block */
35870535441SQu Wenruo 	struct extent_buffer *eb;
35970535441SQu Wenruo 	/* Level of the tree block */
36070535441SQu Wenruo 	unsigned int level:8;
36192a7cc42SQu Wenruo 	/* Is the block in a non-shareable tree */
36270535441SQu Wenruo 	unsigned int cowonly:1;
36370535441SQu Wenruo 	/* 1 if no child node is in the cache */
36470535441SQu Wenruo 	unsigned int lowest:1;
36570535441SQu Wenruo 	/* Is the extent buffer locked */
36670535441SQu Wenruo 	unsigned int locked:1;
36770535441SQu Wenruo 	/* Has the block been processed */
36870535441SQu Wenruo 	unsigned int processed:1;
36970535441SQu Wenruo 	/* Have backrefs of this block been checked */
37070535441SQu Wenruo 	unsigned int checked:1;
37170535441SQu Wenruo 	/*
37270535441SQu Wenruo 	 * 1 if corresponding block has been COWed but some upper level block
37370535441SQu Wenruo 	 * pointers may not point to the new location
37470535441SQu Wenruo 	 */
37570535441SQu Wenruo 	unsigned int pending:1;
37670535441SQu Wenruo 	/* 1 if the backref node isn't connected to any other backref node */
37770535441SQu Wenruo 	unsigned int detached:1;
37870535441SQu Wenruo 
37970535441SQu Wenruo 	/*
38070535441SQu Wenruo 	 * For generic purpose backref cache, where we only care if it's a reloc
38170535441SQu Wenruo 	 * root, doesn't care the source subvolid.
38270535441SQu Wenruo 	 */
38370535441SQu Wenruo 	unsigned int is_reloc_root:1;
38470535441SQu Wenruo };
38570535441SQu Wenruo 
38670535441SQu Wenruo #define LOWER	0
38770535441SQu Wenruo #define UPPER	1
38870535441SQu Wenruo 
38970535441SQu Wenruo /*
39070535441SQu Wenruo  * Represent an edge connecting upper and lower backref nodes.
39170535441SQu Wenruo  */
39270535441SQu Wenruo struct btrfs_backref_edge {
39370535441SQu Wenruo 	/*
39470535441SQu Wenruo 	 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
39570535441SQu Wenruo 	 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
39670535441SQu Wenruo 	 * upper level node.
39770535441SQu Wenruo 	 *
39870535441SQu Wenruo 	 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
39970535441SQu Wenruo 	 * linking list[UPPER] to its upper level nodes.
40070535441SQu Wenruo 	 */
40170535441SQu Wenruo 	struct list_head list[2];
40270535441SQu Wenruo 
40370535441SQu Wenruo 	/* Two related nodes */
40470535441SQu Wenruo 	struct btrfs_backref_node *node[2];
40570535441SQu Wenruo };
40670535441SQu Wenruo 
40770535441SQu Wenruo struct btrfs_backref_cache {
40870535441SQu Wenruo 	/* Red black tree of all backref nodes in the cache */
40970535441SQu Wenruo 	struct rb_root rb_root;
41070535441SQu Wenruo 	/* For passing backref nodes to btrfs_reloc_cow_block */
41170535441SQu Wenruo 	struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
41270535441SQu Wenruo 	/*
41370535441SQu Wenruo 	 * List of blocks that have been COWed but some block pointers in upper
41470535441SQu Wenruo 	 * level blocks may not reflect the new location
41570535441SQu Wenruo 	 */
41670535441SQu Wenruo 	struct list_head pending[BTRFS_MAX_LEVEL];
41770535441SQu Wenruo 	/* List of backref nodes with no child node */
41870535441SQu Wenruo 	struct list_head leaves;
41970535441SQu Wenruo 	/* List of blocks that have been COWed in current transaction */
42070535441SQu Wenruo 	struct list_head changed;
42170535441SQu Wenruo 	/* List of detached backref node. */
42270535441SQu Wenruo 	struct list_head detached;
42370535441SQu Wenruo 
42470535441SQu Wenruo 	u64 last_trans;
42570535441SQu Wenruo 
42670535441SQu Wenruo 	int nr_nodes;
42770535441SQu Wenruo 	int nr_edges;
42870535441SQu Wenruo 
42970535441SQu Wenruo 	/* List of unchecked backref edges during backref cache build */
43070535441SQu Wenruo 	struct list_head pending_edge;
43170535441SQu Wenruo 
43270535441SQu Wenruo 	/* List of useless backref nodes during backref cache build */
43370535441SQu Wenruo 	struct list_head useless_node;
43470535441SQu Wenruo 
43570535441SQu Wenruo 	struct btrfs_fs_info *fs_info;
43670535441SQu Wenruo 
43770535441SQu Wenruo 	/*
43870535441SQu Wenruo 	 * Whether this cache is for relocation
43970535441SQu Wenruo 	 *
44070535441SQu Wenruo 	 * Reloction backref cache require more info for reloc root compared
44170535441SQu Wenruo 	 * to generic backref cache.
44270535441SQu Wenruo 	 */
44370535441SQu Wenruo 	unsigned int is_reloc;
44470535441SQu Wenruo };
44570535441SQu Wenruo 
446584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
447584fb121SQu Wenruo 			      struct btrfs_backref_cache *cache, int is_reloc);
448b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node(
449b1818dabSQu Wenruo 		struct btrfs_backref_cache *cache, u64 bytenr, int level);
45047254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge(
45147254d07SQu Wenruo 		struct btrfs_backref_cache *cache);
452584fb121SQu Wenruo 
453f39911e5SQu Wenruo #define		LINK_LOWER	(1 << 0)
454f39911e5SQu Wenruo #define		LINK_UPPER	(1 << 1)
btrfs_backref_link_edge(struct btrfs_backref_edge * edge,struct btrfs_backref_node * lower,struct btrfs_backref_node * upper,int link_which)455f39911e5SQu Wenruo static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
456f39911e5SQu Wenruo 					   struct btrfs_backref_node *lower,
457f39911e5SQu Wenruo 					   struct btrfs_backref_node *upper,
458f39911e5SQu Wenruo 					   int link_which)
459f39911e5SQu Wenruo {
460f39911e5SQu Wenruo 	ASSERT(upper && lower && upper->level == lower->level + 1);
461f39911e5SQu Wenruo 	edge->node[LOWER] = lower;
462f39911e5SQu Wenruo 	edge->node[UPPER] = upper;
463f39911e5SQu Wenruo 	if (link_which & LINK_LOWER)
464f39911e5SQu Wenruo 		list_add_tail(&edge->list[LOWER], &lower->upper);
465f39911e5SQu Wenruo 	if (link_which & LINK_UPPER)
466f39911e5SQu Wenruo 		list_add_tail(&edge->list[UPPER], &upper->lower);
467f39911e5SQu Wenruo }
468f39911e5SQu Wenruo 
btrfs_backref_free_node(struct btrfs_backref_cache * cache,struct btrfs_backref_node * node)469741188d3SQu Wenruo static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
470741188d3SQu Wenruo 					   struct btrfs_backref_node *node)
471741188d3SQu Wenruo {
472741188d3SQu Wenruo 	if (node) {
473eddda68dSJosef Bacik 		ASSERT(list_empty(&node->list));
474eddda68dSJosef Bacik 		ASSERT(list_empty(&node->lower));
475eddda68dSJosef Bacik 		ASSERT(node->eb == NULL);
476741188d3SQu Wenruo 		cache->nr_nodes--;
477741188d3SQu Wenruo 		btrfs_put_root(node->root);
478741188d3SQu Wenruo 		kfree(node);
479741188d3SQu Wenruo 	}
480741188d3SQu Wenruo }
481741188d3SQu Wenruo 
btrfs_backref_free_edge(struct btrfs_backref_cache * cache,struct btrfs_backref_edge * edge)482741188d3SQu Wenruo static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
483741188d3SQu Wenruo 					   struct btrfs_backref_edge *edge)
484741188d3SQu Wenruo {
485741188d3SQu Wenruo 	if (edge) {
486741188d3SQu Wenruo 		cache->nr_edges--;
487741188d3SQu Wenruo 		kfree(edge);
488741188d3SQu Wenruo 	}
489741188d3SQu Wenruo }
490741188d3SQu Wenruo 
btrfs_backref_unlock_node_buffer(struct btrfs_backref_node * node)491b0fe7078SQu Wenruo static inline void btrfs_backref_unlock_node_buffer(
492b0fe7078SQu Wenruo 		struct btrfs_backref_node *node)
493b0fe7078SQu Wenruo {
494b0fe7078SQu Wenruo 	if (node->locked) {
495b0fe7078SQu Wenruo 		btrfs_tree_unlock(node->eb);
496b0fe7078SQu Wenruo 		node->locked = 0;
497b0fe7078SQu Wenruo 	}
498b0fe7078SQu Wenruo }
499b0fe7078SQu Wenruo 
btrfs_backref_drop_node_buffer(struct btrfs_backref_node * node)500b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node_buffer(
501b0fe7078SQu Wenruo 		struct btrfs_backref_node *node)
502b0fe7078SQu Wenruo {
503b0fe7078SQu Wenruo 	if (node->eb) {
504b0fe7078SQu Wenruo 		btrfs_backref_unlock_node_buffer(node);
505b0fe7078SQu Wenruo 		free_extent_buffer(node->eb);
506b0fe7078SQu Wenruo 		node->eb = NULL;
507b0fe7078SQu Wenruo 	}
508b0fe7078SQu Wenruo }
509b0fe7078SQu Wenruo 
510b0fe7078SQu Wenruo /*
511b0fe7078SQu Wenruo  * Drop the backref node from cache without cleaning up its children
512b0fe7078SQu Wenruo  * edges.
513b0fe7078SQu Wenruo  *
514b0fe7078SQu Wenruo  * This can only be called on node without parent edges.
515b0fe7078SQu Wenruo  * The children edges are still kept as is.
516b0fe7078SQu Wenruo  */
btrfs_backref_drop_node(struct btrfs_backref_cache * tree,struct btrfs_backref_node * node)517b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
518b0fe7078SQu Wenruo 					   struct btrfs_backref_node *node)
519b0fe7078SQu Wenruo {
520eddda68dSJosef Bacik 	ASSERT(list_empty(&node->upper));
521b0fe7078SQu Wenruo 
522b0fe7078SQu Wenruo 	btrfs_backref_drop_node_buffer(node);
523eddda68dSJosef Bacik 	list_del_init(&node->list);
524eddda68dSJosef Bacik 	list_del_init(&node->lower);
525b0fe7078SQu Wenruo 	if (!RB_EMPTY_NODE(&node->rb_node))
526b0fe7078SQu Wenruo 		rb_erase(&node->rb_node, &tree->rb_root);
527b0fe7078SQu Wenruo 	btrfs_backref_free_node(tree, node);
528b0fe7078SQu Wenruo }
529b0fe7078SQu Wenruo 
530023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
531023acb07SQu Wenruo 				struct btrfs_backref_node *node);
532023acb07SQu Wenruo 
53313fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
53413fe1bdbSQu Wenruo 
btrfs_backref_panic(struct btrfs_fs_info * fs_info,u64 bytenr,int errno)535982c92cbSQu Wenruo static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
536982c92cbSQu Wenruo 				       u64 bytenr, int errno)
537982c92cbSQu Wenruo {
538982c92cbSQu Wenruo 	btrfs_panic(fs_info, errno,
539982c92cbSQu Wenruo 		    "Inconsistency in backref cache found at offset %llu",
540982c92cbSQu Wenruo 		    bytenr);
541982c92cbSQu Wenruo }
542982c92cbSQu Wenruo 
543*eb96e221SFilipe Manana int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
544*eb96e221SFilipe Manana 				struct btrfs_backref_cache *cache,
5451b60d2ecSQu Wenruo 				struct btrfs_path *path,
5461b60d2ecSQu Wenruo 				struct btrfs_backref_iter *iter,
5471b60d2ecSQu Wenruo 				struct btrfs_key *node_key,
5481b60d2ecSQu Wenruo 				struct btrfs_backref_node *cur);
5491b60d2ecSQu Wenruo 
550fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
551fc997ed0SQu Wenruo 				     struct btrfs_backref_node *start);
552fc997ed0SQu Wenruo 
5531b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
5541b23ea18SQu Wenruo 				 struct btrfs_backref_node *node);
5551b23ea18SQu Wenruo 
556a542ad1bSJan Schmidt #endif
557