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 /* 16*a2c8d27eSFilipe Manana * Context and arguments for backref walking functions. Some of the fields are 17*a2c8d27eSFilipe Manana * to be filled by the caller of such functions while other are filled by the 18*a2c8d27eSFilipe Manana * functions themselves, as described below. 196ce6ba53SFilipe Manana */ 20*a2c8d27eSFilipe Manana struct btrfs_backref_walk_ctx { 21*a2c8d27eSFilipe Manana /* 22*a2c8d27eSFilipe Manana * The address of the extent for which we are doing backref walking. 23*a2c8d27eSFilipe Manana * Can be either a data extent or a metadata extent. 24*a2c8d27eSFilipe Manana * 25*a2c8d27eSFilipe Manana * Must always be set by the top level caller. 26*a2c8d27eSFilipe Manana */ 27*a2c8d27eSFilipe Manana u64 bytenr; 28*a2c8d27eSFilipe Manana /* 29*a2c8d27eSFilipe Manana * Offset relative to the target extent. This is only used for data 30*a2c8d27eSFilipe Manana * extents, and it's meaningful because we can have file extent items 31*a2c8d27eSFilipe Manana * that point only to a section of a data extent ("bookend" extents), 32*a2c8d27eSFilipe Manana * and we want to filter out any that don't point to a section of the 33*a2c8d27eSFilipe Manana * data extent containing the given offset. 34*a2c8d27eSFilipe Manana * 35*a2c8d27eSFilipe Manana * Must always be set by the top level caller. 36*a2c8d27eSFilipe Manana */ 37*a2c8d27eSFilipe Manana u64 extent_item_pos; 38*a2c8d27eSFilipe Manana /* 39*a2c8d27eSFilipe Manana * If true and bytenr corresponds to a data extent, then references from 40*a2c8d27eSFilipe Manana * all file extent items that point to the data extent are considered, 41*a2c8d27eSFilipe Manana * @extent_item_pos is ignored. 42*a2c8d27eSFilipe Manana */ 43*a2c8d27eSFilipe Manana bool ignore_extent_item_pos; 44*a2c8d27eSFilipe Manana /* A valid transaction handle or NULL. */ 45*a2c8d27eSFilipe Manana struct btrfs_trans_handle *trans; 46*a2c8d27eSFilipe Manana /* 47*a2c8d27eSFilipe Manana * The file system's info object, can not be NULL. 48*a2c8d27eSFilipe Manana * 49*a2c8d27eSFilipe Manana * Must always be set by the top level caller. 50*a2c8d27eSFilipe Manana */ 51*a2c8d27eSFilipe Manana struct btrfs_fs_info *fs_info; 52*a2c8d27eSFilipe Manana /* 53*a2c8d27eSFilipe Manana * Time sequence acquired from btrfs_get_tree_mod_seq(), in case the 54*a2c8d27eSFilipe Manana * caller joined the tree mod log to get a consistent view of b+trees 55*a2c8d27eSFilipe Manana * while we do backref walking, or BTRFS_SEQ_LAST. 56*a2c8d27eSFilipe Manana * When using BTRFS_SEQ_LAST, delayed refs are not checked and it uses 57*a2c8d27eSFilipe Manana * commit roots when searching b+trees - this is a special case for 58*a2c8d27eSFilipe Manana * qgroups used during a transaction commit. 59*a2c8d27eSFilipe Manana */ 60*a2c8d27eSFilipe Manana u64 time_seq; 61*a2c8d27eSFilipe Manana /* 62*a2c8d27eSFilipe Manana * Used to collect the bytenr of metadata extents that point to the 63*a2c8d27eSFilipe Manana * target extent. 64*a2c8d27eSFilipe Manana */ 65*a2c8d27eSFilipe Manana struct ulist *refs; 66*a2c8d27eSFilipe Manana /* 67*a2c8d27eSFilipe Manana * List used to collect the IDs of the roots from which the target 68*a2c8d27eSFilipe Manana * extent is accessible. Can be NULL in case the caller does not care 69*a2c8d27eSFilipe Manana * about collecting root IDs. 70*a2c8d27eSFilipe Manana */ 71*a2c8d27eSFilipe Manana struct ulist *roots; 72*a2c8d27eSFilipe Manana }; 736ce6ba53SFilipe Manana 74a542ad1bSJan Schmidt struct inode_fs_paths { 75a542ad1bSJan Schmidt struct btrfs_path *btrfs_path; 76a542ad1bSJan Schmidt struct btrfs_root *fs_root; 77a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 78a542ad1bSJan Schmidt }; 79a542ad1bSJan Schmidt 8012a824dcSFilipe Manana struct btrfs_backref_shared_cache_entry { 8112a824dcSFilipe Manana u64 bytenr; 8212a824dcSFilipe Manana u64 gen; 8312a824dcSFilipe Manana bool is_shared; 8412a824dcSFilipe Manana }; 8512a824dcSFilipe Manana 8673e339e6SFilipe Manana #define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8 8773e339e6SFilipe Manana 8861dbb952SFilipe Manana struct btrfs_backref_share_check_ctx { 8984a7949dSFilipe Manana /* Ulists used during backref walking. */ 9084a7949dSFilipe Manana struct ulist refs; 9112a824dcSFilipe Manana /* 92877c1476SFilipe Manana * The current leaf the caller of btrfs_is_data_extent_shared() is at. 93877c1476SFilipe Manana * Typically the caller (at the moment only fiemap) tries to determine 94877c1476SFilipe Manana * the sharedness of data extents point by file extent items from entire 95877c1476SFilipe Manana * leaves. 96877c1476SFilipe Manana */ 97877c1476SFilipe Manana u64 curr_leaf_bytenr; 98877c1476SFilipe Manana /* 99877c1476SFilipe Manana * The previous leaf the caller was at in the previous call to 100877c1476SFilipe Manana * btrfs_is_data_extent_shared(). This may be the same as the current 101877c1476SFilipe Manana * leaf. On the first call it must be 0. 102877c1476SFilipe Manana */ 103877c1476SFilipe Manana u64 prev_leaf_bytenr; 104877c1476SFilipe Manana /* 10512a824dcSFilipe Manana * A path from a root to a leaf that has a file extent item pointing to 10612a824dcSFilipe Manana * a given data extent should never exceed the maximum b+tree height. 10712a824dcSFilipe Manana */ 10861dbb952SFilipe Manana struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL]; 10961dbb952SFilipe Manana bool use_path_cache; 11073e339e6SFilipe Manana /* 11173e339e6SFilipe Manana * Cache the sharedness result for the last few extents we have found, 11273e339e6SFilipe Manana * but only for extents for which we have multiple file extent items 11373e339e6SFilipe Manana * that point to them. 11473e339e6SFilipe Manana * It's very common to have several file extent items that point to the 11573e339e6SFilipe Manana * same extent (bytenr) but with different offsets and lengths. This 11673e339e6SFilipe Manana * typically happens for COW writes, partial writes into prealloc 11773e339e6SFilipe Manana * extents, NOCOW writes after snapshoting a root, hole punching or 11873e339e6SFilipe Manana * reflinking within the same file (less common perhaps). 11973e339e6SFilipe Manana * So keep a small cache with the lookup results for the extent pointed 12073e339e6SFilipe Manana * by the last few file extent items. This cache is checked, with a 12173e339e6SFilipe Manana * linear scan, whenever btrfs_is_data_extent_shared() is called, so 12273e339e6SFilipe Manana * it must be small so that it does not negatively affect performance in 12373e339e6SFilipe Manana * case we don't have multiple file extent items that point to the same 12473e339e6SFilipe Manana * data extent. 12573e339e6SFilipe Manana */ 12673e339e6SFilipe Manana struct { 12773e339e6SFilipe Manana u64 bytenr; 12873e339e6SFilipe Manana bool is_shared; 12973e339e6SFilipe Manana } prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE]; 13073e339e6SFilipe Manana /* 13173e339e6SFilipe Manana * The slot in the prev_extents_cache array that will be used for 13273e339e6SFilipe Manana * storing the sharedness result of a new data extent. 13373e339e6SFilipe Manana */ 13473e339e6SFilipe Manana int prev_extents_cache_slot; 13512a824dcSFilipe Manana }; 13612a824dcSFilipe Manana 137c7499a64SFilipe Manana typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 num_bytes, 138c7499a64SFilipe Manana u64 root, void *ctx); 139a542ad1bSJan Schmidt 14084a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void); 14184a7949dSFilipe Manana void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx); 14284a7949dSFilipe Manana 143a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 14469917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 14569917e43SLiu Bo u64 *flags); 146a542ad1bSJan Schmidt 147a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1486eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 1496eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level); 150a542ad1bSJan Schmidt 151*a2c8d27eSFilipe Manana int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx, 152*a2c8d27eSFilipe Manana bool search_commit_root, 153*a2c8d27eSFilipe Manana iterate_extent_inodes_t *iterate, void *user_ctx); 154a542ad1bSJan Schmidt 155a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 156e3059ec0SDavid Sterba struct btrfs_path *path, void *ctx, 157c995ab3cSZygo Blaxell bool ignore_offset); 158a542ad1bSJan Schmidt 159a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath); 160a542ad1bSJan Schmidt 161*a2c8d27eSFilipe Manana int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx); 162*a2c8d27eSFilipe Manana int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx, 1638949b9a1SFilipe Manana bool skip_commit_root_sem); 16496b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 16596b5bd77SJan Schmidt u32 name_len, unsigned long name_off, 16696b5bd77SJan Schmidt struct extent_buffer *eb_in, u64 parent, 16796b5bd77SJan Schmidt char *dest, u32 size); 1688da6d581SJan Schmidt 169a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes); 170a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 171a542ad1bSJan Schmidt struct btrfs_path *path); 172a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath); 173a542ad1bSJan Schmidt 174f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 175f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 176f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 177f186373fSMark Fasheh u64 *found_off); 178ceb707daSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, 179b8f164e3SFilipe Manana u64 extent_gen, 18061dbb952SFilipe Manana struct btrfs_backref_share_check_ctx *ctx); 181f186373fSMark Fasheh 182b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void); 183e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void); 18400142756SJeff Mahoney 18500142756SJeff Mahoney struct prelim_ref { 18600142756SJeff Mahoney struct rb_node rbnode; 18700142756SJeff Mahoney u64 root_id; 18800142756SJeff Mahoney struct btrfs_key key_for_search; 18900142756SJeff Mahoney int level; 19000142756SJeff Mahoney int count; 19100142756SJeff Mahoney struct extent_inode_elem *inode_list; 19200142756SJeff Mahoney u64 parent; 19300142756SJeff Mahoney u64 wanted_disk_byte; 19400142756SJeff Mahoney }; 19500142756SJeff Mahoney 196a37f232bSQu Wenruo /* 197a37f232bSQu Wenruo * Iterate backrefs of one extent. 198a37f232bSQu Wenruo * 199a37f232bSQu Wenruo * Now it only supports iteration of tree block in commit root. 200a37f232bSQu Wenruo */ 201a37f232bSQu Wenruo struct btrfs_backref_iter { 202a37f232bSQu Wenruo u64 bytenr; 203a37f232bSQu Wenruo struct btrfs_path *path; 204a37f232bSQu Wenruo struct btrfs_fs_info *fs_info; 205a37f232bSQu Wenruo struct btrfs_key cur_key; 206a37f232bSQu Wenruo u32 item_ptr; 207a37f232bSQu Wenruo u32 cur_ptr; 208a37f232bSQu Wenruo u32 end_ptr; 209a37f232bSQu Wenruo }; 210a37f232bSQu Wenruo 211d68194b2SDavid Sterba struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info); 212a37f232bSQu Wenruo 213a37f232bSQu Wenruo static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter) 214a37f232bSQu Wenruo { 215a37f232bSQu Wenruo if (!iter) 216a37f232bSQu Wenruo return; 217a37f232bSQu Wenruo btrfs_free_path(iter->path); 218a37f232bSQu Wenruo kfree(iter); 219a37f232bSQu Wenruo } 220a37f232bSQu Wenruo 221c39c2ddcSQu Wenruo static inline struct extent_buffer *btrfs_backref_get_eb( 222c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 223c39c2ddcSQu Wenruo { 224c39c2ddcSQu Wenruo if (!iter) 225c39c2ddcSQu Wenruo return NULL; 226c39c2ddcSQu Wenruo return iter->path->nodes[0]; 227c39c2ddcSQu Wenruo } 228c39c2ddcSQu Wenruo 229c39c2ddcSQu Wenruo /* 230c39c2ddcSQu Wenruo * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data 231c39c2ddcSQu Wenruo * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header. 232c39c2ddcSQu Wenruo * 233c39c2ddcSQu Wenruo * This helper determines if that's the case. 234c39c2ddcSQu Wenruo */ 235c39c2ddcSQu Wenruo static inline bool btrfs_backref_has_tree_block_info( 236c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 237c39c2ddcSQu Wenruo { 238c39c2ddcSQu Wenruo if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY && 239c39c2ddcSQu Wenruo iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item)) 240c39c2ddcSQu Wenruo return true; 241c39c2ddcSQu Wenruo return false; 242c39c2ddcSQu Wenruo } 243c39c2ddcSQu Wenruo 244a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr); 245a37f232bSQu Wenruo 246c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter); 247c39c2ddcSQu Wenruo 248c39c2ddcSQu Wenruo static inline bool btrfs_backref_iter_is_inline_ref( 249c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 250c39c2ddcSQu Wenruo { 251c39c2ddcSQu Wenruo if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY || 252c39c2ddcSQu Wenruo iter->cur_key.type == BTRFS_METADATA_ITEM_KEY) 253c39c2ddcSQu Wenruo return true; 254c39c2ddcSQu Wenruo return false; 255c39c2ddcSQu Wenruo } 256c39c2ddcSQu Wenruo 257a37f232bSQu Wenruo static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter) 258a37f232bSQu Wenruo { 259a37f232bSQu Wenruo iter->bytenr = 0; 260a37f232bSQu Wenruo iter->item_ptr = 0; 261a37f232bSQu Wenruo iter->cur_ptr = 0; 262a37f232bSQu Wenruo iter->end_ptr = 0; 263a37f232bSQu Wenruo btrfs_release_path(iter->path); 264a37f232bSQu Wenruo memset(&iter->cur_key, 0, sizeof(iter->cur_key)); 265a37f232bSQu Wenruo } 266a37f232bSQu Wenruo 26770535441SQu Wenruo /* 26870535441SQu Wenruo * Backref cache related structures 26970535441SQu Wenruo * 27070535441SQu Wenruo * The whole objective of backref_cache is to build a bi-directional map 27170535441SQu Wenruo * of tree blocks (represented by backref_node) and all their parents. 27270535441SQu Wenruo */ 27370535441SQu Wenruo 27470535441SQu Wenruo /* 27570535441SQu Wenruo * Represent a tree block in the backref cache 27670535441SQu Wenruo */ 27770535441SQu Wenruo struct btrfs_backref_node { 278e9a28dc5SQu Wenruo struct { 27970535441SQu Wenruo struct rb_node rb_node; 28070535441SQu Wenruo u64 bytenr; 281e9a28dc5SQu Wenruo }; /* Use rb_simple_node for search/insert */ 28270535441SQu Wenruo 28370535441SQu Wenruo u64 new_bytenr; 28470535441SQu Wenruo /* Objectid of tree block owner, can be not uptodate */ 28570535441SQu Wenruo u64 owner; 28670535441SQu Wenruo /* Link to pending, changed or detached list */ 28770535441SQu Wenruo struct list_head list; 28870535441SQu Wenruo 28970535441SQu Wenruo /* List of upper level edges, which link this node to its parents */ 29070535441SQu Wenruo struct list_head upper; 29170535441SQu Wenruo /* List of lower level edges, which link this node to its children */ 29270535441SQu Wenruo struct list_head lower; 29370535441SQu Wenruo 29470535441SQu Wenruo /* NULL if this node is not tree root */ 29570535441SQu Wenruo struct btrfs_root *root; 29670535441SQu Wenruo /* Extent buffer got by COWing the block */ 29770535441SQu Wenruo struct extent_buffer *eb; 29870535441SQu Wenruo /* Level of the tree block */ 29970535441SQu Wenruo unsigned int level:8; 30092a7cc42SQu Wenruo /* Is the block in a non-shareable tree */ 30170535441SQu Wenruo unsigned int cowonly:1; 30270535441SQu Wenruo /* 1 if no child node is in the cache */ 30370535441SQu Wenruo unsigned int lowest:1; 30470535441SQu Wenruo /* Is the extent buffer locked */ 30570535441SQu Wenruo unsigned int locked:1; 30670535441SQu Wenruo /* Has the block been processed */ 30770535441SQu Wenruo unsigned int processed:1; 30870535441SQu Wenruo /* Have backrefs of this block been checked */ 30970535441SQu Wenruo unsigned int checked:1; 31070535441SQu Wenruo /* 31170535441SQu Wenruo * 1 if corresponding block has been COWed but some upper level block 31270535441SQu Wenruo * pointers may not point to the new location 31370535441SQu Wenruo */ 31470535441SQu Wenruo unsigned int pending:1; 31570535441SQu Wenruo /* 1 if the backref node isn't connected to any other backref node */ 31670535441SQu Wenruo unsigned int detached:1; 31770535441SQu Wenruo 31870535441SQu Wenruo /* 31970535441SQu Wenruo * For generic purpose backref cache, where we only care if it's a reloc 32070535441SQu Wenruo * root, doesn't care the source subvolid. 32170535441SQu Wenruo */ 32270535441SQu Wenruo unsigned int is_reloc_root:1; 32370535441SQu Wenruo }; 32470535441SQu Wenruo 32570535441SQu Wenruo #define LOWER 0 32670535441SQu Wenruo #define UPPER 1 32770535441SQu Wenruo 32870535441SQu Wenruo /* 32970535441SQu Wenruo * Represent an edge connecting upper and lower backref nodes. 33070535441SQu Wenruo */ 33170535441SQu Wenruo struct btrfs_backref_edge { 33270535441SQu Wenruo /* 33370535441SQu Wenruo * list[LOWER] is linked to btrfs_backref_node::upper of lower level 33470535441SQu Wenruo * node, and list[UPPER] is linked to btrfs_backref_node::lower of 33570535441SQu Wenruo * upper level node. 33670535441SQu Wenruo * 33770535441SQu Wenruo * Also, build_backref_tree() uses list[UPPER] for pending edges, before 33870535441SQu Wenruo * linking list[UPPER] to its upper level nodes. 33970535441SQu Wenruo */ 34070535441SQu Wenruo struct list_head list[2]; 34170535441SQu Wenruo 34270535441SQu Wenruo /* Two related nodes */ 34370535441SQu Wenruo struct btrfs_backref_node *node[2]; 34470535441SQu Wenruo }; 34570535441SQu Wenruo 34670535441SQu Wenruo struct btrfs_backref_cache { 34770535441SQu Wenruo /* Red black tree of all backref nodes in the cache */ 34870535441SQu Wenruo struct rb_root rb_root; 34970535441SQu Wenruo /* For passing backref nodes to btrfs_reloc_cow_block */ 35070535441SQu Wenruo struct btrfs_backref_node *path[BTRFS_MAX_LEVEL]; 35170535441SQu Wenruo /* 35270535441SQu Wenruo * List of blocks that have been COWed but some block pointers in upper 35370535441SQu Wenruo * level blocks may not reflect the new location 35470535441SQu Wenruo */ 35570535441SQu Wenruo struct list_head pending[BTRFS_MAX_LEVEL]; 35670535441SQu Wenruo /* List of backref nodes with no child node */ 35770535441SQu Wenruo struct list_head leaves; 35870535441SQu Wenruo /* List of blocks that have been COWed in current transaction */ 35970535441SQu Wenruo struct list_head changed; 36070535441SQu Wenruo /* List of detached backref node. */ 36170535441SQu Wenruo struct list_head detached; 36270535441SQu Wenruo 36370535441SQu Wenruo u64 last_trans; 36470535441SQu Wenruo 36570535441SQu Wenruo int nr_nodes; 36670535441SQu Wenruo int nr_edges; 36770535441SQu Wenruo 36870535441SQu Wenruo /* List of unchecked backref edges during backref cache build */ 36970535441SQu Wenruo struct list_head pending_edge; 37070535441SQu Wenruo 37170535441SQu Wenruo /* List of useless backref nodes during backref cache build */ 37270535441SQu Wenruo struct list_head useless_node; 37370535441SQu Wenruo 37470535441SQu Wenruo struct btrfs_fs_info *fs_info; 37570535441SQu Wenruo 37670535441SQu Wenruo /* 37770535441SQu Wenruo * Whether this cache is for relocation 37870535441SQu Wenruo * 37970535441SQu Wenruo * Reloction backref cache require more info for reloc root compared 38070535441SQu Wenruo * to generic backref cache. 38170535441SQu Wenruo */ 38270535441SQu Wenruo unsigned int is_reloc; 38370535441SQu Wenruo }; 38470535441SQu Wenruo 385584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 386584fb121SQu Wenruo struct btrfs_backref_cache *cache, int is_reloc); 387b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node( 388b1818dabSQu Wenruo struct btrfs_backref_cache *cache, u64 bytenr, int level); 38947254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge( 39047254d07SQu Wenruo struct btrfs_backref_cache *cache); 391584fb121SQu Wenruo 392f39911e5SQu Wenruo #define LINK_LOWER (1 << 0) 393f39911e5SQu Wenruo #define LINK_UPPER (1 << 1) 394f39911e5SQu Wenruo static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge, 395f39911e5SQu Wenruo struct btrfs_backref_node *lower, 396f39911e5SQu Wenruo struct btrfs_backref_node *upper, 397f39911e5SQu Wenruo int link_which) 398f39911e5SQu Wenruo { 399f39911e5SQu Wenruo ASSERT(upper && lower && upper->level == lower->level + 1); 400f39911e5SQu Wenruo edge->node[LOWER] = lower; 401f39911e5SQu Wenruo edge->node[UPPER] = upper; 402f39911e5SQu Wenruo if (link_which & LINK_LOWER) 403f39911e5SQu Wenruo list_add_tail(&edge->list[LOWER], &lower->upper); 404f39911e5SQu Wenruo if (link_which & LINK_UPPER) 405f39911e5SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 406f39911e5SQu Wenruo } 407f39911e5SQu Wenruo 408741188d3SQu Wenruo static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache, 409741188d3SQu Wenruo struct btrfs_backref_node *node) 410741188d3SQu Wenruo { 411741188d3SQu Wenruo if (node) { 412eddda68dSJosef Bacik ASSERT(list_empty(&node->list)); 413eddda68dSJosef Bacik ASSERT(list_empty(&node->lower)); 414eddda68dSJosef Bacik ASSERT(node->eb == NULL); 415741188d3SQu Wenruo cache->nr_nodes--; 416741188d3SQu Wenruo btrfs_put_root(node->root); 417741188d3SQu Wenruo kfree(node); 418741188d3SQu Wenruo } 419741188d3SQu Wenruo } 420741188d3SQu Wenruo 421741188d3SQu Wenruo static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache, 422741188d3SQu Wenruo struct btrfs_backref_edge *edge) 423741188d3SQu Wenruo { 424741188d3SQu Wenruo if (edge) { 425741188d3SQu Wenruo cache->nr_edges--; 426741188d3SQu Wenruo kfree(edge); 427741188d3SQu Wenruo } 428741188d3SQu Wenruo } 429741188d3SQu Wenruo 430b0fe7078SQu Wenruo static inline void btrfs_backref_unlock_node_buffer( 431b0fe7078SQu Wenruo struct btrfs_backref_node *node) 432b0fe7078SQu Wenruo { 433b0fe7078SQu Wenruo if (node->locked) { 434b0fe7078SQu Wenruo btrfs_tree_unlock(node->eb); 435b0fe7078SQu Wenruo node->locked = 0; 436b0fe7078SQu Wenruo } 437b0fe7078SQu Wenruo } 438b0fe7078SQu Wenruo 439b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node_buffer( 440b0fe7078SQu Wenruo struct btrfs_backref_node *node) 441b0fe7078SQu Wenruo { 442b0fe7078SQu Wenruo if (node->eb) { 443b0fe7078SQu Wenruo btrfs_backref_unlock_node_buffer(node); 444b0fe7078SQu Wenruo free_extent_buffer(node->eb); 445b0fe7078SQu Wenruo node->eb = NULL; 446b0fe7078SQu Wenruo } 447b0fe7078SQu Wenruo } 448b0fe7078SQu Wenruo 449b0fe7078SQu Wenruo /* 450b0fe7078SQu Wenruo * Drop the backref node from cache without cleaning up its children 451b0fe7078SQu Wenruo * edges. 452b0fe7078SQu Wenruo * 453b0fe7078SQu Wenruo * This can only be called on node without parent edges. 454b0fe7078SQu Wenruo * The children edges are still kept as is. 455b0fe7078SQu Wenruo */ 456b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree, 457b0fe7078SQu Wenruo struct btrfs_backref_node *node) 458b0fe7078SQu Wenruo { 459eddda68dSJosef Bacik ASSERT(list_empty(&node->upper)); 460b0fe7078SQu Wenruo 461b0fe7078SQu Wenruo btrfs_backref_drop_node_buffer(node); 462eddda68dSJosef Bacik list_del_init(&node->list); 463eddda68dSJosef Bacik list_del_init(&node->lower); 464b0fe7078SQu Wenruo if (!RB_EMPTY_NODE(&node->rb_node)) 465b0fe7078SQu Wenruo rb_erase(&node->rb_node, &tree->rb_root); 466b0fe7078SQu Wenruo btrfs_backref_free_node(tree, node); 467b0fe7078SQu Wenruo } 468b0fe7078SQu Wenruo 469023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 470023acb07SQu Wenruo struct btrfs_backref_node *node); 471023acb07SQu Wenruo 47213fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache); 47313fe1bdbSQu Wenruo 474982c92cbSQu Wenruo static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info, 475982c92cbSQu Wenruo u64 bytenr, int errno) 476982c92cbSQu Wenruo { 477982c92cbSQu Wenruo btrfs_panic(fs_info, errno, 478982c92cbSQu Wenruo "Inconsistency in backref cache found at offset %llu", 479982c92cbSQu Wenruo bytenr); 480982c92cbSQu Wenruo } 481982c92cbSQu Wenruo 4821b60d2ecSQu Wenruo int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, 4831b60d2ecSQu Wenruo struct btrfs_path *path, 4841b60d2ecSQu Wenruo struct btrfs_backref_iter *iter, 4851b60d2ecSQu Wenruo struct btrfs_key *node_key, 4861b60d2ecSQu Wenruo struct btrfs_backref_node *cur); 4871b60d2ecSQu Wenruo 488fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 489fc997ed0SQu Wenruo struct btrfs_backref_node *start); 490fc997ed0SQu Wenruo 4911b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 4921b23ea18SQu Wenruo struct btrfs_backref_node *node); 4931b23ea18SQu Wenruo 494a542ad1bSJan Schmidt #endif 495