162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2011 STRATO.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef BTRFS_BACKREF_H
762306a36Sopenharmony_ci#define BTRFS_BACKREF_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/btrfs.h>
1062306a36Sopenharmony_ci#include "messages.h"
1162306a36Sopenharmony_ci#include "ulist.h"
1262306a36Sopenharmony_ci#include "disk-io.h"
1362306a36Sopenharmony_ci#include "extent_io.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Used by implementations of iterate_extent_inodes_t (see definition below) to
1762306a36Sopenharmony_ci * signal that backref iteration can stop immediately and no error happened.
1862306a36Sopenharmony_ci * The value must be non-negative and must not be 0, 1 (which is a common return
1962306a36Sopenharmony_ci * value from things like btrfs_search_slot() and used internally in the backref
2062306a36Sopenharmony_ci * walking code) and different from BACKREF_FOUND_SHARED and
2162306a36Sopenharmony_ci * BACKREF_FOUND_NOT_SHARED
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci#define BTRFS_ITERATE_EXTENT_INODES_STOP 5
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * Should return 0 if no errors happened and iteration of backrefs should
2762306a36Sopenharmony_ci * continue. Can return BTRFS_ITERATE_EXTENT_INODES_STOP or any other non-zero
2862306a36Sopenharmony_ci * value to immediately stop iteration and possibly signal an error back to
2962306a36Sopenharmony_ci * the caller.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_citypedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 num_bytes,
3262306a36Sopenharmony_ci				      u64 root, void *ctx);
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci * Context and arguments for backref walking functions. Some of the fields are
3662306a36Sopenharmony_ci * to be filled by the caller of such functions while other are filled by the
3762306a36Sopenharmony_ci * functions themselves, as described below.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_cistruct btrfs_backref_walk_ctx {
4062306a36Sopenharmony_ci	/*
4162306a36Sopenharmony_ci	 * The address of the extent for which we are doing backref walking.
4262306a36Sopenharmony_ci	 * Can be either a data extent or a metadata extent.
4362306a36Sopenharmony_ci	 *
4462306a36Sopenharmony_ci	 * Must always be set by the top level caller.
4562306a36Sopenharmony_ci	 */
4662306a36Sopenharmony_ci	u64 bytenr;
4762306a36Sopenharmony_ci	/*
4862306a36Sopenharmony_ci	 * Offset relative to the target extent. This is only used for data
4962306a36Sopenharmony_ci	 * extents, and it's meaningful because we can have file extent items
5062306a36Sopenharmony_ci	 * that point only to a section of a data extent ("bookend" extents),
5162306a36Sopenharmony_ci	 * and we want to filter out any that don't point to a section of the
5262306a36Sopenharmony_ci	 * data extent containing the given offset.
5362306a36Sopenharmony_ci	 *
5462306a36Sopenharmony_ci	 * Must always be set by the top level caller.
5562306a36Sopenharmony_ci	 */
5662306a36Sopenharmony_ci	u64 extent_item_pos;
5762306a36Sopenharmony_ci	/*
5862306a36Sopenharmony_ci	 * If true and bytenr corresponds to a data extent, then references from
5962306a36Sopenharmony_ci	 * all file extent items that point to the data extent are considered,
6062306a36Sopenharmony_ci	 * @extent_item_pos is ignored.
6162306a36Sopenharmony_ci	 */
6262306a36Sopenharmony_ci	bool ignore_extent_item_pos;
6362306a36Sopenharmony_ci	/*
6462306a36Sopenharmony_ci	 * If true and bytenr corresponds to a data extent, then the inode list
6562306a36Sopenharmony_ci	 * (each member describing inode number, file offset and root) is not
6662306a36Sopenharmony_ci	 * added to each reference added to the @refs ulist.
6762306a36Sopenharmony_ci	 */
6862306a36Sopenharmony_ci	bool skip_inode_ref_list;
6962306a36Sopenharmony_ci	/* A valid transaction handle or NULL. */
7062306a36Sopenharmony_ci	struct btrfs_trans_handle *trans;
7162306a36Sopenharmony_ci	/*
7262306a36Sopenharmony_ci	 * The file system's info object, can not be NULL.
7362306a36Sopenharmony_ci	 *
7462306a36Sopenharmony_ci	 * Must always be set by the top level caller.
7562306a36Sopenharmony_ci	 */
7662306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info;
7762306a36Sopenharmony_ci	/*
7862306a36Sopenharmony_ci	 * Time sequence acquired from btrfs_get_tree_mod_seq(), in case the
7962306a36Sopenharmony_ci	 * caller joined the tree mod log to get a consistent view of b+trees
8062306a36Sopenharmony_ci	 * while we do backref walking, or BTRFS_SEQ_LAST.
8162306a36Sopenharmony_ci	 * When using BTRFS_SEQ_LAST, delayed refs are not checked and it uses
8262306a36Sopenharmony_ci	 * commit roots when searching b+trees - this is a special case for
8362306a36Sopenharmony_ci	 * qgroups used during a transaction commit.
8462306a36Sopenharmony_ci	 */
8562306a36Sopenharmony_ci	u64 time_seq;
8662306a36Sopenharmony_ci	/*
8762306a36Sopenharmony_ci	 * Used to collect the bytenr of metadata extents that point to the
8862306a36Sopenharmony_ci	 * target extent.
8962306a36Sopenharmony_ci	 */
9062306a36Sopenharmony_ci	struct ulist *refs;
9162306a36Sopenharmony_ci	/*
9262306a36Sopenharmony_ci	 * List used to collect the IDs of the roots from which the target
9362306a36Sopenharmony_ci	 * extent is accessible. Can be NULL in case the caller does not care
9462306a36Sopenharmony_ci	 * about collecting root IDs.
9562306a36Sopenharmony_ci	 */
9662306a36Sopenharmony_ci	struct ulist *roots;
9762306a36Sopenharmony_ci	/*
9862306a36Sopenharmony_ci	 * Used by iterate_extent_inodes() and the main backref walk code
9962306a36Sopenharmony_ci	 * (find_parent_nodes()). Lookup and store functions for an optional
10062306a36Sopenharmony_ci	 * cache which maps the logical address (bytenr) of leaves to an array
10162306a36Sopenharmony_ci	 * of root IDs.
10262306a36Sopenharmony_ci	 */
10362306a36Sopenharmony_ci	bool (*cache_lookup)(u64 leaf_bytenr, void *user_ctx,
10462306a36Sopenharmony_ci			     const u64 **root_ids_ret, int *root_count_ret);
10562306a36Sopenharmony_ci	void (*cache_store)(u64 leaf_bytenr, const struct ulist *root_ids,
10662306a36Sopenharmony_ci			    void *user_ctx);
10762306a36Sopenharmony_ci	/*
10862306a36Sopenharmony_ci	 * If this is not NULL, then the backref walking code will call this
10962306a36Sopenharmony_ci	 * for each indirect data extent reference as soon as it finds one,
11062306a36Sopenharmony_ci	 * before collecting all the remaining backrefs and before resolving
11162306a36Sopenharmony_ci	 * indirect backrefs. This allows for the caller to terminate backref
11262306a36Sopenharmony_ci	 * walking as soon as it finds one backref that matches some specific
11362306a36Sopenharmony_ci	 * criteria. The @cache_lookup and @cache_store callbacks should not
11462306a36Sopenharmony_ci	 * be NULL in order to use this callback.
11562306a36Sopenharmony_ci	 */
11662306a36Sopenharmony_ci	iterate_extent_inodes_t *indirect_ref_iterator;
11762306a36Sopenharmony_ci	/*
11862306a36Sopenharmony_ci	 * If this is not NULL, then the backref walking code will call this for
11962306a36Sopenharmony_ci	 * each extent item it's meant to process before it actually starts
12062306a36Sopenharmony_ci	 * processing it. If this returns anything other than 0, then it stops
12162306a36Sopenharmony_ci	 * the backref walking code immediately.
12262306a36Sopenharmony_ci	 */
12362306a36Sopenharmony_ci	int (*check_extent_item)(u64 bytenr, const struct btrfs_extent_item *ei,
12462306a36Sopenharmony_ci				 const struct extent_buffer *leaf, void *user_ctx);
12562306a36Sopenharmony_ci	/*
12662306a36Sopenharmony_ci	 * If this is not NULL, then the backref walking code will call this for
12762306a36Sopenharmony_ci	 * each extent data ref it finds (BTRFS_EXTENT_DATA_REF_KEY keys) before
12862306a36Sopenharmony_ci	 * processing that data ref. If this callback return false, then it will
12962306a36Sopenharmony_ci	 * ignore this data ref and it will never resolve the indirect data ref,
13062306a36Sopenharmony_ci	 * saving time searching for leaves in a fs tree with file extent items
13162306a36Sopenharmony_ci	 * matching the data ref.
13262306a36Sopenharmony_ci	 */
13362306a36Sopenharmony_ci	bool (*skip_data_ref)(u64 root, u64 ino, u64 offset, void *user_ctx);
13462306a36Sopenharmony_ci	/* Context object to pass to the callbacks defined above. */
13562306a36Sopenharmony_ci	void *user_ctx;
13662306a36Sopenharmony_ci};
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistruct inode_fs_paths {
13962306a36Sopenharmony_ci	struct btrfs_path		*btrfs_path;
14062306a36Sopenharmony_ci	struct btrfs_root		*fs_root;
14162306a36Sopenharmony_ci	struct btrfs_data_container	*fspath;
14262306a36Sopenharmony_ci};
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistruct btrfs_backref_shared_cache_entry {
14562306a36Sopenharmony_ci	u64 bytenr;
14662306a36Sopenharmony_ci	u64 gen;
14762306a36Sopenharmony_ci	bool is_shared;
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci#define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistruct btrfs_backref_share_check_ctx {
15362306a36Sopenharmony_ci	/* Ulists used during backref walking. */
15462306a36Sopenharmony_ci	struct ulist refs;
15562306a36Sopenharmony_ci	/*
15662306a36Sopenharmony_ci	 * The current leaf the caller of btrfs_is_data_extent_shared() is at.
15762306a36Sopenharmony_ci	 * Typically the caller (at the moment only fiemap) tries to determine
15862306a36Sopenharmony_ci	 * the sharedness of data extents point by file extent items from entire
15962306a36Sopenharmony_ci	 * leaves.
16062306a36Sopenharmony_ci	 */
16162306a36Sopenharmony_ci	u64 curr_leaf_bytenr;
16262306a36Sopenharmony_ci	/*
16362306a36Sopenharmony_ci	 * The previous leaf the caller was at in the previous call to
16462306a36Sopenharmony_ci	 * btrfs_is_data_extent_shared(). This may be the same as the current
16562306a36Sopenharmony_ci	 * leaf. On the first call it must be 0.
16662306a36Sopenharmony_ci	 */
16762306a36Sopenharmony_ci	u64 prev_leaf_bytenr;
16862306a36Sopenharmony_ci	/*
16962306a36Sopenharmony_ci	 * A path from a root to a leaf that has a file extent item pointing to
17062306a36Sopenharmony_ci	 * a given data extent should never exceed the maximum b+tree height.
17162306a36Sopenharmony_ci	 */
17262306a36Sopenharmony_ci	struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL];
17362306a36Sopenharmony_ci	bool use_path_cache;
17462306a36Sopenharmony_ci	/*
17562306a36Sopenharmony_ci	 * Cache the sharedness result for the last few extents we have found,
17662306a36Sopenharmony_ci	 * but only for extents for which we have multiple file extent items
17762306a36Sopenharmony_ci	 * that point to them.
17862306a36Sopenharmony_ci	 * It's very common to have several file extent items that point to the
17962306a36Sopenharmony_ci	 * same extent (bytenr) but with different offsets and lengths. This
18062306a36Sopenharmony_ci	 * typically happens for COW writes, partial writes into prealloc
18162306a36Sopenharmony_ci	 * extents, NOCOW writes after snapshoting a root, hole punching or
18262306a36Sopenharmony_ci	 * reflinking within the same file (less common perhaps).
18362306a36Sopenharmony_ci	 * So keep a small cache with the lookup results for the extent pointed
18462306a36Sopenharmony_ci	 * by the last few file extent items. This cache is checked, with a
18562306a36Sopenharmony_ci	 * linear scan, whenever btrfs_is_data_extent_shared() is called, so
18662306a36Sopenharmony_ci	 * it must be small so that it does not negatively affect performance in
18762306a36Sopenharmony_ci	 * case we don't have multiple file extent items that point to the same
18862306a36Sopenharmony_ci	 * data extent.
18962306a36Sopenharmony_ci	 */
19062306a36Sopenharmony_ci	struct {
19162306a36Sopenharmony_ci		u64 bytenr;
19262306a36Sopenharmony_ci		bool is_shared;
19362306a36Sopenharmony_ci	} prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE];
19462306a36Sopenharmony_ci	/*
19562306a36Sopenharmony_ci	 * The slot in the prev_extents_cache array that will be used for
19662306a36Sopenharmony_ci	 * storing the sharedness result of a new data extent.
19762306a36Sopenharmony_ci	 */
19862306a36Sopenharmony_ci	int prev_extents_cache_slot;
19962306a36Sopenharmony_ci};
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistruct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void);
20262306a36Sopenharmony_civoid btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciint extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
20562306a36Sopenharmony_ci			struct btrfs_path *path, struct btrfs_key *found_key,
20662306a36Sopenharmony_ci			u64 *flags);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciint tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
20962306a36Sopenharmony_ci			    struct btrfs_key *key, struct btrfs_extent_item *ei,
21062306a36Sopenharmony_ci			    u32 item_size, u64 *out_root, u8 *out_level);
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciint iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx,
21362306a36Sopenharmony_ci			  bool search_commit_root,
21462306a36Sopenharmony_ci			  iterate_extent_inodes_t *iterate, void *user_ctx);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciint iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
21762306a36Sopenharmony_ci				struct btrfs_path *path, void *ctx,
21862306a36Sopenharmony_ci				bool ignore_offset);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ciint paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ciint btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx);
22362306a36Sopenharmony_ciint btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx,
22462306a36Sopenharmony_ci			 bool skip_commit_root_sem);
22562306a36Sopenharmony_cichar *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
22662306a36Sopenharmony_ci			u32 name_len, unsigned long name_off,
22762306a36Sopenharmony_ci			struct extent_buffer *eb_in, u64 parent,
22862306a36Sopenharmony_ci			char *dest, u32 size);
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cistruct btrfs_data_container *init_data_container(u32 total_bytes);
23162306a36Sopenharmony_cistruct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
23262306a36Sopenharmony_ci					struct btrfs_path *path);
23362306a36Sopenharmony_civoid free_ipath(struct inode_fs_paths *ipath);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciint btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
23662306a36Sopenharmony_ci			  u64 start_off, struct btrfs_path *path,
23762306a36Sopenharmony_ci			  struct btrfs_inode_extref **ret_extref,
23862306a36Sopenharmony_ci			  u64 *found_off);
23962306a36Sopenharmony_ciint btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr,
24062306a36Sopenharmony_ci				u64 extent_gen,
24162306a36Sopenharmony_ci				struct btrfs_backref_share_check_ctx *ctx);
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ciint __init btrfs_prelim_ref_init(void);
24462306a36Sopenharmony_civoid __cold btrfs_prelim_ref_exit(void);
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_cistruct prelim_ref {
24762306a36Sopenharmony_ci	struct rb_node rbnode;
24862306a36Sopenharmony_ci	u64 root_id;
24962306a36Sopenharmony_ci	struct btrfs_key key_for_search;
25062306a36Sopenharmony_ci	int level;
25162306a36Sopenharmony_ci	int count;
25262306a36Sopenharmony_ci	struct extent_inode_elem *inode_list;
25362306a36Sopenharmony_ci	u64 parent;
25462306a36Sopenharmony_ci	u64 wanted_disk_byte;
25562306a36Sopenharmony_ci};
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci/*
25862306a36Sopenharmony_ci * Iterate backrefs of one extent.
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci * Now it only supports iteration of tree block in commit root.
26162306a36Sopenharmony_ci */
26262306a36Sopenharmony_cistruct btrfs_backref_iter {
26362306a36Sopenharmony_ci	u64 bytenr;
26462306a36Sopenharmony_ci	struct btrfs_path *path;
26562306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info;
26662306a36Sopenharmony_ci	struct btrfs_key cur_key;
26762306a36Sopenharmony_ci	u32 item_ptr;
26862306a36Sopenharmony_ci	u32 cur_ptr;
26962306a36Sopenharmony_ci	u32 end_ptr;
27062306a36Sopenharmony_ci};
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_cistruct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info);
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_cistatic inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
27562306a36Sopenharmony_ci{
27662306a36Sopenharmony_ci	if (!iter)
27762306a36Sopenharmony_ci		return;
27862306a36Sopenharmony_ci	btrfs_free_path(iter->path);
27962306a36Sopenharmony_ci	kfree(iter);
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_cistatic inline struct extent_buffer *btrfs_backref_get_eb(
28362306a36Sopenharmony_ci		struct btrfs_backref_iter *iter)
28462306a36Sopenharmony_ci{
28562306a36Sopenharmony_ci	if (!iter)
28662306a36Sopenharmony_ci		return NULL;
28762306a36Sopenharmony_ci	return iter->path->nodes[0];
28862306a36Sopenharmony_ci}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/*
29162306a36Sopenharmony_ci * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
29262306a36Sopenharmony_ci * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
29362306a36Sopenharmony_ci *
29462306a36Sopenharmony_ci * This helper determines if that's the case.
29562306a36Sopenharmony_ci */
29662306a36Sopenharmony_cistatic inline bool btrfs_backref_has_tree_block_info(
29762306a36Sopenharmony_ci		struct btrfs_backref_iter *iter)
29862306a36Sopenharmony_ci{
29962306a36Sopenharmony_ci	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
30062306a36Sopenharmony_ci	    iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
30162306a36Sopenharmony_ci		return true;
30262306a36Sopenharmony_ci	return false;
30362306a36Sopenharmony_ci}
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ciint btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ciint btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_cistatic inline bool btrfs_backref_iter_is_inline_ref(
31062306a36Sopenharmony_ci		struct btrfs_backref_iter *iter)
31162306a36Sopenharmony_ci{
31262306a36Sopenharmony_ci	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
31362306a36Sopenharmony_ci	    iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
31462306a36Sopenharmony_ci		return true;
31562306a36Sopenharmony_ci	return false;
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cistatic inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	iter->bytenr = 0;
32162306a36Sopenharmony_ci	iter->item_ptr = 0;
32262306a36Sopenharmony_ci	iter->cur_ptr = 0;
32362306a36Sopenharmony_ci	iter->end_ptr = 0;
32462306a36Sopenharmony_ci	btrfs_release_path(iter->path);
32562306a36Sopenharmony_ci	memset(&iter->cur_key, 0, sizeof(iter->cur_key));
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci/*
32962306a36Sopenharmony_ci * Backref cache related structures
33062306a36Sopenharmony_ci *
33162306a36Sopenharmony_ci * The whole objective of backref_cache is to build a bi-directional map
33262306a36Sopenharmony_ci * of tree blocks (represented by backref_node) and all their parents.
33362306a36Sopenharmony_ci */
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci/*
33662306a36Sopenharmony_ci * Represent a tree block in the backref cache
33762306a36Sopenharmony_ci */
33862306a36Sopenharmony_cistruct btrfs_backref_node {
33962306a36Sopenharmony_ci	struct {
34062306a36Sopenharmony_ci		struct rb_node rb_node;
34162306a36Sopenharmony_ci		u64 bytenr;
34262306a36Sopenharmony_ci	}; /* Use rb_simple_node for search/insert */
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	u64 new_bytenr;
34562306a36Sopenharmony_ci	/* Objectid of tree block owner, can be not uptodate */
34662306a36Sopenharmony_ci	u64 owner;
34762306a36Sopenharmony_ci	/* Link to pending, changed or detached list */
34862306a36Sopenharmony_ci	struct list_head list;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	/* List of upper level edges, which link this node to its parents */
35162306a36Sopenharmony_ci	struct list_head upper;
35262306a36Sopenharmony_ci	/* List of lower level edges, which link this node to its children */
35362306a36Sopenharmony_ci	struct list_head lower;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	/* NULL if this node is not tree root */
35662306a36Sopenharmony_ci	struct btrfs_root *root;
35762306a36Sopenharmony_ci	/* Extent buffer got by COWing the block */
35862306a36Sopenharmony_ci	struct extent_buffer *eb;
35962306a36Sopenharmony_ci	/* Level of the tree block */
36062306a36Sopenharmony_ci	unsigned int level:8;
36162306a36Sopenharmony_ci	/* Is the block in a non-shareable tree */
36262306a36Sopenharmony_ci	unsigned int cowonly:1;
36362306a36Sopenharmony_ci	/* 1 if no child node is in the cache */
36462306a36Sopenharmony_ci	unsigned int lowest:1;
36562306a36Sopenharmony_ci	/* Is the extent buffer locked */
36662306a36Sopenharmony_ci	unsigned int locked:1;
36762306a36Sopenharmony_ci	/* Has the block been processed */
36862306a36Sopenharmony_ci	unsigned int processed:1;
36962306a36Sopenharmony_ci	/* Have backrefs of this block been checked */
37062306a36Sopenharmony_ci	unsigned int checked:1;
37162306a36Sopenharmony_ci	/*
37262306a36Sopenharmony_ci	 * 1 if corresponding block has been COWed but some upper level block
37362306a36Sopenharmony_ci	 * pointers may not point to the new location
37462306a36Sopenharmony_ci	 */
37562306a36Sopenharmony_ci	unsigned int pending:1;
37662306a36Sopenharmony_ci	/* 1 if the backref node isn't connected to any other backref node */
37762306a36Sopenharmony_ci	unsigned int detached:1;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	/*
38062306a36Sopenharmony_ci	 * For generic purpose backref cache, where we only care if it's a reloc
38162306a36Sopenharmony_ci	 * root, doesn't care the source subvolid.
38262306a36Sopenharmony_ci	 */
38362306a36Sopenharmony_ci	unsigned int is_reloc_root:1;
38462306a36Sopenharmony_ci};
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci#define LOWER	0
38762306a36Sopenharmony_ci#define UPPER	1
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci/*
39062306a36Sopenharmony_ci * Represent an edge connecting upper and lower backref nodes.
39162306a36Sopenharmony_ci */
39262306a36Sopenharmony_cistruct btrfs_backref_edge {
39362306a36Sopenharmony_ci	/*
39462306a36Sopenharmony_ci	 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
39562306a36Sopenharmony_ci	 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
39662306a36Sopenharmony_ci	 * upper level node.
39762306a36Sopenharmony_ci	 *
39862306a36Sopenharmony_ci	 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
39962306a36Sopenharmony_ci	 * linking list[UPPER] to its upper level nodes.
40062306a36Sopenharmony_ci	 */
40162306a36Sopenharmony_ci	struct list_head list[2];
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	/* Two related nodes */
40462306a36Sopenharmony_ci	struct btrfs_backref_node *node[2];
40562306a36Sopenharmony_ci};
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_cistruct btrfs_backref_cache {
40862306a36Sopenharmony_ci	/* Red black tree of all backref nodes in the cache */
40962306a36Sopenharmony_ci	struct rb_root rb_root;
41062306a36Sopenharmony_ci	/* For passing backref nodes to btrfs_reloc_cow_block */
41162306a36Sopenharmony_ci	struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
41262306a36Sopenharmony_ci	/*
41362306a36Sopenharmony_ci	 * List of blocks that have been COWed but some block pointers in upper
41462306a36Sopenharmony_ci	 * level blocks may not reflect the new location
41562306a36Sopenharmony_ci	 */
41662306a36Sopenharmony_ci	struct list_head pending[BTRFS_MAX_LEVEL];
41762306a36Sopenharmony_ci	/* List of backref nodes with no child node */
41862306a36Sopenharmony_ci	struct list_head leaves;
41962306a36Sopenharmony_ci	/* List of blocks that have been COWed in current transaction */
42062306a36Sopenharmony_ci	struct list_head changed;
42162306a36Sopenharmony_ci	/* List of detached backref node. */
42262306a36Sopenharmony_ci	struct list_head detached;
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	u64 last_trans;
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	int nr_nodes;
42762306a36Sopenharmony_ci	int nr_edges;
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	/* List of unchecked backref edges during backref cache build */
43062306a36Sopenharmony_ci	struct list_head pending_edge;
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci	/* List of useless backref nodes during backref cache build */
43362306a36Sopenharmony_ci	struct list_head useless_node;
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info;
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	/*
43862306a36Sopenharmony_ci	 * Whether this cache is for relocation
43962306a36Sopenharmony_ci	 *
44062306a36Sopenharmony_ci	 * Reloction backref cache require more info for reloc root compared
44162306a36Sopenharmony_ci	 * to generic backref cache.
44262306a36Sopenharmony_ci	 */
44362306a36Sopenharmony_ci	unsigned int is_reloc;
44462306a36Sopenharmony_ci};
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_civoid btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
44762306a36Sopenharmony_ci			      struct btrfs_backref_cache *cache, int is_reloc);
44862306a36Sopenharmony_cistruct btrfs_backref_node *btrfs_backref_alloc_node(
44962306a36Sopenharmony_ci		struct btrfs_backref_cache *cache, u64 bytenr, int level);
45062306a36Sopenharmony_cistruct btrfs_backref_edge *btrfs_backref_alloc_edge(
45162306a36Sopenharmony_ci		struct btrfs_backref_cache *cache);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci#define		LINK_LOWER	(1 << 0)
45462306a36Sopenharmony_ci#define		LINK_UPPER	(1 << 1)
45562306a36Sopenharmony_cistatic inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
45662306a36Sopenharmony_ci					   struct btrfs_backref_node *lower,
45762306a36Sopenharmony_ci					   struct btrfs_backref_node *upper,
45862306a36Sopenharmony_ci					   int link_which)
45962306a36Sopenharmony_ci{
46062306a36Sopenharmony_ci	ASSERT(upper && lower && upper->level == lower->level + 1);
46162306a36Sopenharmony_ci	edge->node[LOWER] = lower;
46262306a36Sopenharmony_ci	edge->node[UPPER] = upper;
46362306a36Sopenharmony_ci	if (link_which & LINK_LOWER)
46462306a36Sopenharmony_ci		list_add_tail(&edge->list[LOWER], &lower->upper);
46562306a36Sopenharmony_ci	if (link_which & LINK_UPPER)
46662306a36Sopenharmony_ci		list_add_tail(&edge->list[UPPER], &upper->lower);
46762306a36Sopenharmony_ci}
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_cistatic inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
47062306a36Sopenharmony_ci					   struct btrfs_backref_node *node)
47162306a36Sopenharmony_ci{
47262306a36Sopenharmony_ci	if (node) {
47362306a36Sopenharmony_ci		ASSERT(list_empty(&node->list));
47462306a36Sopenharmony_ci		ASSERT(list_empty(&node->lower));
47562306a36Sopenharmony_ci		ASSERT(node->eb == NULL);
47662306a36Sopenharmony_ci		cache->nr_nodes--;
47762306a36Sopenharmony_ci		btrfs_put_root(node->root);
47862306a36Sopenharmony_ci		kfree(node);
47962306a36Sopenharmony_ci	}
48062306a36Sopenharmony_ci}
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_cistatic inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
48362306a36Sopenharmony_ci					   struct btrfs_backref_edge *edge)
48462306a36Sopenharmony_ci{
48562306a36Sopenharmony_ci	if (edge) {
48662306a36Sopenharmony_ci		cache->nr_edges--;
48762306a36Sopenharmony_ci		kfree(edge);
48862306a36Sopenharmony_ci	}
48962306a36Sopenharmony_ci}
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_cistatic inline void btrfs_backref_unlock_node_buffer(
49262306a36Sopenharmony_ci		struct btrfs_backref_node *node)
49362306a36Sopenharmony_ci{
49462306a36Sopenharmony_ci	if (node->locked) {
49562306a36Sopenharmony_ci		btrfs_tree_unlock(node->eb);
49662306a36Sopenharmony_ci		node->locked = 0;
49762306a36Sopenharmony_ci	}
49862306a36Sopenharmony_ci}
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_cistatic inline void btrfs_backref_drop_node_buffer(
50162306a36Sopenharmony_ci		struct btrfs_backref_node *node)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci	if (node->eb) {
50462306a36Sopenharmony_ci		btrfs_backref_unlock_node_buffer(node);
50562306a36Sopenharmony_ci		free_extent_buffer(node->eb);
50662306a36Sopenharmony_ci		node->eb = NULL;
50762306a36Sopenharmony_ci	}
50862306a36Sopenharmony_ci}
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci/*
51162306a36Sopenharmony_ci * Drop the backref node from cache without cleaning up its children
51262306a36Sopenharmony_ci * edges.
51362306a36Sopenharmony_ci *
51462306a36Sopenharmony_ci * This can only be called on node without parent edges.
51562306a36Sopenharmony_ci * The children edges are still kept as is.
51662306a36Sopenharmony_ci */
51762306a36Sopenharmony_cistatic inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
51862306a36Sopenharmony_ci					   struct btrfs_backref_node *node)
51962306a36Sopenharmony_ci{
52062306a36Sopenharmony_ci	ASSERT(list_empty(&node->upper));
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	btrfs_backref_drop_node_buffer(node);
52362306a36Sopenharmony_ci	list_del_init(&node->list);
52462306a36Sopenharmony_ci	list_del_init(&node->lower);
52562306a36Sopenharmony_ci	if (!RB_EMPTY_NODE(&node->rb_node))
52662306a36Sopenharmony_ci		rb_erase(&node->rb_node, &tree->rb_root);
52762306a36Sopenharmony_ci	btrfs_backref_free_node(tree, node);
52862306a36Sopenharmony_ci}
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_civoid btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
53162306a36Sopenharmony_ci				struct btrfs_backref_node *node);
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_civoid btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_cistatic inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
53662306a36Sopenharmony_ci				       u64 bytenr, int errno)
53762306a36Sopenharmony_ci{
53862306a36Sopenharmony_ci	btrfs_panic(fs_info, errno,
53962306a36Sopenharmony_ci		    "Inconsistency in backref cache found at offset %llu",
54062306a36Sopenharmony_ci		    bytenr);
54162306a36Sopenharmony_ci}
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ciint btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
54462306a36Sopenharmony_ci				struct btrfs_backref_cache *cache,
54562306a36Sopenharmony_ci				struct btrfs_path *path,
54662306a36Sopenharmony_ci				struct btrfs_backref_iter *iter,
54762306a36Sopenharmony_ci				struct btrfs_key *node_key,
54862306a36Sopenharmony_ci				struct btrfs_backref_node *cur);
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ciint btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
55162306a36Sopenharmony_ci				     struct btrfs_backref_node *start);
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_civoid btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
55462306a36Sopenharmony_ci				 struct btrfs_backref_node *node);
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci#endif
557