/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (C) 2011 STRATO. All rights reserved.3*/45#ifndef BTRFS_BACKREF_H6#define BTRFS_BACKREF_H78#include <linux/types.h>9#include <linux/rbtree.h>10#include <linux/list.h>11#include <linux/slab.h>12#include <uapi/linux/btrfs.h>13#include <uapi/linux/btrfs_tree.h>14#include "messages.h"15#include "locking.h"16#include "disk-io.h"17#include "extent_io.h"18#include "ctree.h"1920struct extent_inode_elem;21struct ulist;22struct btrfs_extent_item;23struct btrfs_trans_handle;24struct btrfs_fs_info;2526/*27* Used by implementations of iterate_extent_inodes_t (see definition below) to28* signal that backref iteration can stop immediately and no error happened.29* The value must be non-negative and must not be 0, 1 (which is a common return30* value from things like btrfs_search_slot() and used internally in the backref31* walking code) and different from BACKREF_FOUND_SHARED and32* BACKREF_FOUND_NOT_SHARED33*/34#define BTRFS_ITERATE_EXTENT_INODES_STOP 53536/*37* Should return 0 if no errors happened and iteration of backrefs should38* continue. Can return BTRFS_ITERATE_EXTENT_INODES_STOP or any other non-zero39* value to immediately stop iteration and possibly signal an error back to40* the caller.41*/42typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 num_bytes,43u64 root, void *ctx);4445/*46* Context and arguments for backref walking functions. Some of the fields are47* to be filled by the caller of such functions while other are filled by the48* functions themselves, as described below.49*/50struct btrfs_backref_walk_ctx {51/*52* The address of the extent for which we are doing backref walking.53* Can be either a data extent or a metadata extent.54*55* Must always be set by the top level caller.56*/57u64 bytenr;58/*59* Offset relative to the target extent. This is only used for data60* extents, and it's meaningful because we can have file extent items61* that point only to a section of a data extent ("bookend" extents),62* and we want to filter out any that don't point to a section of the63* data extent containing the given offset.64*65* Must always be set by the top level caller.66*/67u64 extent_item_pos;68/*69* If true and bytenr corresponds to a data extent, then references from70* all file extent items that point to the data extent are considered,71* @extent_item_pos is ignored.72*/73bool ignore_extent_item_pos;74/*75* If true and bytenr corresponds to a data extent, then the inode list76* (each member describing inode number, file offset and root) is not77* added to each reference added to the @refs ulist.78*/79bool skip_inode_ref_list;80/* A valid transaction handle or NULL. */81struct btrfs_trans_handle *trans;82/*83* The file system's info object, can not be NULL.84*85* Must always be set by the top level caller.86*/87struct btrfs_fs_info *fs_info;88/*89* Time sequence acquired from btrfs_get_tree_mod_seq(), in case the90* caller joined the tree mod log to get a consistent view of b+trees91* while we do backref walking, or BTRFS_SEQ_LAST.92* When using BTRFS_SEQ_LAST, delayed refs are not checked and it uses93* commit roots when searching b+trees - this is a special case for94* qgroups used during a transaction commit.95*/96u64 time_seq;97/*98* Used to collect the bytenr of metadata extents that point to the99* target extent.100*/101struct ulist *refs;102/*103* List used to collect the IDs of the roots from which the target104* extent is accessible. Can be NULL in case the caller does not care105* about collecting root IDs.106*/107struct ulist *roots;108/*109* Used by iterate_extent_inodes() and the main backref walk code110* (find_parent_nodes()). Lookup and store functions for an optional111* cache which maps the logical address (bytenr) of leaves to an array112* of root IDs.113*/114bool (*cache_lookup)(u64 leaf_bytenr, void *user_ctx,115const u64 **root_ids_ret, int *root_count_ret);116void (*cache_store)(u64 leaf_bytenr, const struct ulist *root_ids,117void *user_ctx);118/*119* If this is not NULL, then the backref walking code will call this120* for each indirect data extent reference as soon as it finds one,121* before collecting all the remaining backrefs and before resolving122* indirect backrefs. This allows for the caller to terminate backref123* walking as soon as it finds one backref that matches some specific124* criteria. The @cache_lookup and @cache_store callbacks should not125* be NULL in order to use this callback.126*/127iterate_extent_inodes_t *indirect_ref_iterator;128/*129* If this is not NULL, then the backref walking code will call this for130* each extent item it's meant to process before it actually starts131* processing it. If this returns anything other than 0, then it stops132* the backref walking code immediately.133*/134int (*check_extent_item)(u64 bytenr, const struct btrfs_extent_item *ei,135const struct extent_buffer *leaf, void *user_ctx);136/*137* If this is not NULL, then the backref walking code will call this for138* each extent data ref it finds (BTRFS_EXTENT_DATA_REF_KEY keys) before139* processing that data ref. If this callback return false, then it will140* ignore this data ref and it will never resolve the indirect data ref,141* saving time searching for leaves in a fs tree with file extent items142* matching the data ref.143*/144bool (*skip_data_ref)(u64 root, u64 ino, u64 offset, void *user_ctx);145/* Context object to pass to the callbacks defined above. */146void *user_ctx;147};148149struct inode_fs_paths {150struct btrfs_path *btrfs_path;151struct btrfs_root *fs_root;152struct btrfs_data_container *fspath;153};154155struct btrfs_backref_shared_cache_entry {156u64 bytenr;157u64 gen;158bool is_shared;159};160161#define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8162163struct btrfs_backref_share_check_ctx {164/* Ulists used during backref walking. */165struct ulist refs;166/*167* The current leaf the caller of btrfs_is_data_extent_shared() is at.168* Typically the caller (at the moment only fiemap) tries to determine169* the sharedness of data extents point by file extent items from entire170* leaves.171*/172u64 curr_leaf_bytenr;173/*174* The previous leaf the caller was at in the previous call to175* btrfs_is_data_extent_shared(). This may be the same as the current176* leaf. On the first call it must be 0.177*/178u64 prev_leaf_bytenr;179/*180* A path from a root to a leaf that has a file extent item pointing to181* a given data extent should never exceed the maximum b+tree height.182*/183struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL];184bool use_path_cache;185/*186* Cache the sharedness result for the last few extents we have found,187* but only for extents for which we have multiple file extent items188* that point to them.189* It's very common to have several file extent items that point to the190* same extent (bytenr) but with different offsets and lengths. This191* typically happens for COW writes, partial writes into prealloc192* extents, NOCOW writes after snapshoting a root, hole punching or193* reflinking within the same file (less common perhaps).194* So keep a small cache with the lookup results for the extent pointed195* by the last few file extent items. This cache is checked, with a196* linear scan, whenever btrfs_is_data_extent_shared() is called, so197* it must be small so that it does not negatively affect performance in198* case we don't have multiple file extent items that point to the same199* data extent.200*/201struct {202u64 bytenr;203bool is_shared;204} prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE];205/*206* The slot in the prev_extents_cache array that will be used for207* storing the sharedness result of a new data extent.208*/209int prev_extents_cache_slot;210};211212struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void);213void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx);214215int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,216struct btrfs_path *path, struct btrfs_key *found_key,217u64 *flags);218219int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,220struct btrfs_key *key, struct btrfs_extent_item *ei,221u32 item_size, u64 *out_root, u8 *out_level);222223int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx,224bool search_commit_root,225iterate_extent_inodes_t *iterate, void *user_ctx);226227int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,228void *ctx, bool ignore_offset);229230int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);231232int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx);233int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx,234bool skip_commit_root_sem);235char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,236u32 name_len, unsigned long name_off,237struct extent_buffer *eb_in, u64 parent,238char *dest, u32 size);239240struct btrfs_data_container *init_data_container(u32 total_bytes);241struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,242struct btrfs_path *path);243void free_ipath(struct inode_fs_paths *ipath);244245int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,246u64 start_off, struct btrfs_path *path,247struct btrfs_inode_extref **ret_extref,248u64 *found_off);249int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr,250u64 extent_gen,251struct btrfs_backref_share_check_ctx *ctx);252253int __init btrfs_prelim_ref_init(void);254void __cold btrfs_prelim_ref_exit(void);255256struct prelim_ref {257struct rb_node rbnode;258u64 root_id;259struct btrfs_key key_for_search;260u8 level;261int count;262struct extent_inode_elem *inode_list;263u64 parent;264u64 wanted_disk_byte;265};266267/*268* Iterate backrefs of one extent.269*270* Now it only supports iteration of tree block in commit root.271*/272struct btrfs_backref_iter {273u64 bytenr;274struct btrfs_path *path;275struct btrfs_fs_info *fs_info;276struct btrfs_key cur_key;277u32 item_ptr;278u32 cur_ptr;279u32 end_ptr;280};281282struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info);283284/*285* For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data286* is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.287*288* This helper determines if that's the case.289*/290static inline bool btrfs_backref_has_tree_block_info(291struct btrfs_backref_iter *iter)292{293if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&294iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))295return true;296return false;297}298299int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);300301int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);302303/*304* Backref cache related structures305*306* The whole objective of backref_cache is to build a bi-directional map307* of tree blocks (represented by backref_node) and all their parents.308*/309310/*311* Represent a tree block in the backref cache312*/313struct btrfs_backref_node {314union{315/* Use rb_simple_node for search/insert */316struct {317struct rb_node rb_node;318u64 bytenr;319};320321struct rb_simple_node simple_node;322};323324/*325* This is a sanity check, whenever we COW a block we will update326* new_bytenr with it's current location, and we will check this in327* various places to validate that the cache makes sense, it shouldn't328* be used for anything else.329*/330u64 new_bytenr;331/* Objectid of tree block owner, can be not uptodate */332u64 owner;333/* Link to pending, changed or detached list */334struct list_head list;335336/* List of upper level edges, which link this node to its parents */337struct list_head upper;338/* List of lower level edges, which link this node to its children */339struct list_head lower;340341/* NULL if this node is not tree root */342struct btrfs_root *root;343/* Extent buffer got by COWing the block */344struct extent_buffer *eb;345/* Level of the tree block */346unsigned int level:8;347/* Is the extent buffer locked */348unsigned int locked:1;349/* Has the block been processed */350unsigned int processed:1;351/* Have backrefs of this block been checked */352unsigned int checked:1;353/*354* 1 if corresponding block has been COWed but some upper level block355* pointers may not point to the new location356*/357unsigned int pending:1;358/* 1 if the backref node isn't connected to any other backref node */359unsigned int detached:1;360361/*362* For generic purpose backref cache, where we only care if it's a reloc363* root, doesn't care the source subvolid.364*/365unsigned int is_reloc_root:1;366};367368#define LOWER 0369#define UPPER 1370371/*372* Represent an edge connecting upper and lower backref nodes.373*/374struct btrfs_backref_edge {375/*376* list[LOWER] is linked to btrfs_backref_node::upper of lower level377* node, and list[UPPER] is linked to btrfs_backref_node::lower of378* upper level node.379*380* Also, build_backref_tree() uses list[UPPER] for pending edges, before381* linking list[UPPER] to its upper level nodes.382*/383struct list_head list[2];384385/* Two related nodes */386struct btrfs_backref_node *node[2];387};388389struct btrfs_backref_cache {390/* Red black tree of all backref nodes in the cache */391struct rb_root rb_root;392/* For passing backref nodes to btrfs_reloc_cow_block */393struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];394/*395* List of blocks that have been COWed but some block pointers in upper396* level blocks may not reflect the new location397*/398struct list_head pending[BTRFS_MAX_LEVEL];399400u64 last_trans;401402int nr_nodes;403int nr_edges;404405/* List of unchecked backref edges during backref cache build */406struct list_head pending_edge;407408/* List of useless backref nodes during backref cache build */409struct list_head useless_node;410411struct btrfs_fs_info *fs_info;412413/*414* Whether this cache is for relocation415*416* Reloction backref cache require more info for reloc root compared417* to generic backref cache.418*/419bool is_reloc;420};421422void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,423struct btrfs_backref_cache *cache, bool is_reloc);424struct btrfs_backref_node *btrfs_backref_alloc_node(425struct btrfs_backref_cache *cache, u64 bytenr, int level);426struct btrfs_backref_edge *btrfs_backref_alloc_edge(427struct btrfs_backref_cache *cache);428429void btrfs_backref_free_node(struct btrfs_backref_cache *cache,430struct btrfs_backref_node *node);431void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,432struct btrfs_backref_edge *edge);433void btrfs_backref_unlock_node_buffer(struct btrfs_backref_node *node);434void btrfs_backref_drop_node_buffer(struct btrfs_backref_node *node);435436void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,437struct btrfs_backref_node *node);438void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,439struct btrfs_backref_node *node);440441void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);442443static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,444u64 bytenr, int error)445{446btrfs_panic(fs_info, error,447"Inconsistency in backref cache found at offset %llu",448bytenr);449}450451int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,452struct btrfs_backref_cache *cache,453struct btrfs_path *path,454struct btrfs_backref_iter *iter,455struct btrfs_key *node_key,456struct btrfs_backref_node *cur);457458int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,459struct btrfs_backref_node *start);460461void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,462struct btrfs_backref_node *node);463464#endif465466467