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*88ffb665SFilipe Manana * Used by implementations of iterate_extent_inodes_t (see definition below) to 17*88ffb665SFilipe Manana * signal that backref iteration can stop immediately and no error happened. 18*88ffb665SFilipe Manana * The value must be non-negative and must not be 0, 1 (which is a common return 19*88ffb665SFilipe Manana * value from things like btrfs_search_slot() and used internally in the backref 20*88ffb665SFilipe Manana * walking code) and different from BACKREF_FOUND_SHARED and 21*88ffb665SFilipe Manana * BACKREF_FOUND_NOT_SHARED 22*88ffb665SFilipe Manana */ 23*88ffb665SFilipe Manana #define BTRFS_ITERATE_EXTENT_INODES_STOP 5 24*88ffb665SFilipe Manana 25*88ffb665SFilipe Manana /* 26*88ffb665SFilipe Manana * Should return 0 if no errors happened and iteration of backrefs should 27*88ffb665SFilipe Manana * continue. Can return BTRFS_ITERATE_EXTENT_INODES_STOP or any other non-zero 28*88ffb665SFilipe Manana * value to immediately stop iteration and possibly signal an error back to 29*88ffb665SFilipe Manana * the caller. 30*88ffb665SFilipe Manana */ 31*88ffb665SFilipe Manana typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 num_bytes, 32*88ffb665SFilipe Manana u64 root, void *ctx); 33*88ffb665SFilipe Manana 34*88ffb665SFilipe 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; 63a2c8d27eSFilipe Manana /* A valid transaction handle or NULL. */ 64a2c8d27eSFilipe Manana struct btrfs_trans_handle *trans; 65a2c8d27eSFilipe Manana /* 66a2c8d27eSFilipe Manana * The file system's info object, can not be NULL. 67a2c8d27eSFilipe Manana * 68a2c8d27eSFilipe Manana * Must always be set by the top level caller. 69a2c8d27eSFilipe Manana */ 70a2c8d27eSFilipe Manana struct btrfs_fs_info *fs_info; 71a2c8d27eSFilipe Manana /* 72a2c8d27eSFilipe Manana * Time sequence acquired from btrfs_get_tree_mod_seq(), in case the 73a2c8d27eSFilipe Manana * caller joined the tree mod log to get a consistent view of b+trees 74a2c8d27eSFilipe Manana * while we do backref walking, or BTRFS_SEQ_LAST. 75a2c8d27eSFilipe Manana * When using BTRFS_SEQ_LAST, delayed refs are not checked and it uses 76a2c8d27eSFilipe Manana * commit roots when searching b+trees - this is a special case for 77a2c8d27eSFilipe Manana * qgroups used during a transaction commit. 78a2c8d27eSFilipe Manana */ 79a2c8d27eSFilipe Manana u64 time_seq; 80a2c8d27eSFilipe Manana /* 81a2c8d27eSFilipe Manana * Used to collect the bytenr of metadata extents that point to the 82a2c8d27eSFilipe Manana * target extent. 83a2c8d27eSFilipe Manana */ 84a2c8d27eSFilipe Manana struct ulist *refs; 85a2c8d27eSFilipe Manana /* 86a2c8d27eSFilipe Manana * List used to collect the IDs of the roots from which the target 87a2c8d27eSFilipe Manana * extent is accessible. Can be NULL in case the caller does not care 88a2c8d27eSFilipe Manana * about collecting root IDs. 89a2c8d27eSFilipe Manana */ 90a2c8d27eSFilipe Manana struct ulist *roots; 9166d04209SFilipe Manana /* 92*88ffb665SFilipe Manana * Used by iterate_extent_inodes() and the main backref walk code 93*88ffb665SFilipe Manana * (find_parent_nodes()). Lookup and store functions for an optional 94*88ffb665SFilipe Manana * cache which maps the logical address (bytenr) of leaves to an array 95*88ffb665SFilipe Manana * of root IDs. 9666d04209SFilipe Manana */ 9766d04209SFilipe Manana bool (*cache_lookup)(u64 leaf_bytenr, void *user_ctx, 9866d04209SFilipe Manana const u64 **root_ids_ret, int *root_count_ret); 9966d04209SFilipe Manana void (*cache_store)(u64 leaf_bytenr, const struct ulist *root_ids, 10066d04209SFilipe Manana void *user_ctx); 101*88ffb665SFilipe Manana /* 102*88ffb665SFilipe Manana * If this is not NULL, then the backref walking code will call this 103*88ffb665SFilipe Manana * for each indirect data extent reference as soon as it finds one, 104*88ffb665SFilipe Manana * before collecting all the remaining backrefs and before resolving 105*88ffb665SFilipe Manana * indirect backrefs. This allows for the caller to terminate backref 106*88ffb665SFilipe Manana * walking as soon as it finds one backref that matches some specific 107*88ffb665SFilipe Manana * criteria. The @cache_lookup and @cache_store callbacks should not 108*88ffb665SFilipe Manana * be NULL in order to use this callback. 109*88ffb665SFilipe Manana */ 110*88ffb665SFilipe Manana iterate_extent_inodes_t *indirect_ref_iterator; 111*88ffb665SFilipe Manana /* 112*88ffb665SFilipe Manana * Context object to pass to the @cache_lookup, @cache_store and 113*88ffb665SFilipe Manana * @indirect_ref_iterator callbacks. 114*88ffb665SFilipe Manana */ 11566d04209SFilipe Manana void *user_ctx; 116a2c8d27eSFilipe Manana }; 1176ce6ba53SFilipe Manana 118a542ad1bSJan Schmidt struct inode_fs_paths { 119a542ad1bSJan Schmidt struct btrfs_path *btrfs_path; 120a542ad1bSJan Schmidt struct btrfs_root *fs_root; 121a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 122a542ad1bSJan Schmidt }; 123a542ad1bSJan Schmidt 12412a824dcSFilipe Manana struct btrfs_backref_shared_cache_entry { 12512a824dcSFilipe Manana u64 bytenr; 12612a824dcSFilipe Manana u64 gen; 12712a824dcSFilipe Manana bool is_shared; 12812a824dcSFilipe Manana }; 12912a824dcSFilipe Manana 13073e339e6SFilipe Manana #define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8 13173e339e6SFilipe Manana 13261dbb952SFilipe Manana struct btrfs_backref_share_check_ctx { 13384a7949dSFilipe Manana /* Ulists used during backref walking. */ 13484a7949dSFilipe Manana struct ulist refs; 13512a824dcSFilipe Manana /* 136877c1476SFilipe Manana * The current leaf the caller of btrfs_is_data_extent_shared() is at. 137877c1476SFilipe Manana * Typically the caller (at the moment only fiemap) tries to determine 138877c1476SFilipe Manana * the sharedness of data extents point by file extent items from entire 139877c1476SFilipe Manana * leaves. 140877c1476SFilipe Manana */ 141877c1476SFilipe Manana u64 curr_leaf_bytenr; 142877c1476SFilipe Manana /* 143877c1476SFilipe Manana * The previous leaf the caller was at in the previous call to 144877c1476SFilipe Manana * btrfs_is_data_extent_shared(). This may be the same as the current 145877c1476SFilipe Manana * leaf. On the first call it must be 0. 146877c1476SFilipe Manana */ 147877c1476SFilipe Manana u64 prev_leaf_bytenr; 148877c1476SFilipe Manana /* 14912a824dcSFilipe Manana * A path from a root to a leaf that has a file extent item pointing to 15012a824dcSFilipe Manana * a given data extent should never exceed the maximum b+tree height. 15112a824dcSFilipe Manana */ 15261dbb952SFilipe Manana struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL]; 15361dbb952SFilipe Manana bool use_path_cache; 15473e339e6SFilipe Manana /* 15573e339e6SFilipe Manana * Cache the sharedness result for the last few extents we have found, 15673e339e6SFilipe Manana * but only for extents for which we have multiple file extent items 15773e339e6SFilipe Manana * that point to them. 15873e339e6SFilipe Manana * It's very common to have several file extent items that point to the 15973e339e6SFilipe Manana * same extent (bytenr) but with different offsets and lengths. This 16073e339e6SFilipe Manana * typically happens for COW writes, partial writes into prealloc 16173e339e6SFilipe Manana * extents, NOCOW writes after snapshoting a root, hole punching or 16273e339e6SFilipe Manana * reflinking within the same file (less common perhaps). 16373e339e6SFilipe Manana * So keep a small cache with the lookup results for the extent pointed 16473e339e6SFilipe Manana * by the last few file extent items. This cache is checked, with a 16573e339e6SFilipe Manana * linear scan, whenever btrfs_is_data_extent_shared() is called, so 16673e339e6SFilipe Manana * it must be small so that it does not negatively affect performance in 16773e339e6SFilipe Manana * case we don't have multiple file extent items that point to the same 16873e339e6SFilipe Manana * data extent. 16973e339e6SFilipe Manana */ 17073e339e6SFilipe Manana struct { 17173e339e6SFilipe Manana u64 bytenr; 17273e339e6SFilipe Manana bool is_shared; 17373e339e6SFilipe Manana } prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE]; 17473e339e6SFilipe Manana /* 17573e339e6SFilipe Manana * The slot in the prev_extents_cache array that will be used for 17673e339e6SFilipe Manana * storing the sharedness result of a new data extent. 17773e339e6SFilipe Manana */ 17873e339e6SFilipe Manana int prev_extents_cache_slot; 17912a824dcSFilipe Manana }; 18012a824dcSFilipe Manana 18184a7949dSFilipe Manana struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void); 18284a7949dSFilipe Manana void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx); 18384a7949dSFilipe Manana 184a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 18569917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 18669917e43SLiu Bo u64 *flags); 187a542ad1bSJan Schmidt 188a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 1896eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 1906eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level); 191a542ad1bSJan Schmidt 192a2c8d27eSFilipe Manana int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx, 193a2c8d27eSFilipe Manana bool search_commit_root, 194a2c8d27eSFilipe Manana iterate_extent_inodes_t *iterate, void *user_ctx); 195a542ad1bSJan Schmidt 196a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 197e3059ec0SDavid Sterba struct btrfs_path *path, void *ctx, 198c995ab3cSZygo Blaxell bool ignore_offset); 199a542ad1bSJan Schmidt 200a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath); 201a542ad1bSJan Schmidt 202a2c8d27eSFilipe Manana int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx); 203a2c8d27eSFilipe Manana int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx, 2048949b9a1SFilipe Manana bool skip_commit_root_sem); 20596b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 20696b5bd77SJan Schmidt u32 name_len, unsigned long name_off, 20796b5bd77SJan Schmidt struct extent_buffer *eb_in, u64 parent, 20896b5bd77SJan Schmidt char *dest, u32 size); 2098da6d581SJan Schmidt 210a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes); 211a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 212a542ad1bSJan Schmidt struct btrfs_path *path); 213a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath); 214a542ad1bSJan Schmidt 215f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 216f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 217f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 218f186373fSMark Fasheh u64 *found_off); 219ceb707daSFilipe Manana int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, 220b8f164e3SFilipe Manana u64 extent_gen, 22161dbb952SFilipe Manana struct btrfs_backref_share_check_ctx *ctx); 222f186373fSMark Fasheh 223b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void); 224e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void); 22500142756SJeff Mahoney 22600142756SJeff Mahoney struct prelim_ref { 22700142756SJeff Mahoney struct rb_node rbnode; 22800142756SJeff Mahoney u64 root_id; 22900142756SJeff Mahoney struct btrfs_key key_for_search; 23000142756SJeff Mahoney int level; 23100142756SJeff Mahoney int count; 23200142756SJeff Mahoney struct extent_inode_elem *inode_list; 23300142756SJeff Mahoney u64 parent; 23400142756SJeff Mahoney u64 wanted_disk_byte; 23500142756SJeff Mahoney }; 23600142756SJeff Mahoney 237a37f232bSQu Wenruo /* 238a37f232bSQu Wenruo * Iterate backrefs of one extent. 239a37f232bSQu Wenruo * 240a37f232bSQu Wenruo * Now it only supports iteration of tree block in commit root. 241a37f232bSQu Wenruo */ 242a37f232bSQu Wenruo struct btrfs_backref_iter { 243a37f232bSQu Wenruo u64 bytenr; 244a37f232bSQu Wenruo struct btrfs_path *path; 245a37f232bSQu Wenruo struct btrfs_fs_info *fs_info; 246a37f232bSQu Wenruo struct btrfs_key cur_key; 247a37f232bSQu Wenruo u32 item_ptr; 248a37f232bSQu Wenruo u32 cur_ptr; 249a37f232bSQu Wenruo u32 end_ptr; 250a37f232bSQu Wenruo }; 251a37f232bSQu Wenruo 252d68194b2SDavid Sterba struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info); 253a37f232bSQu Wenruo 254a37f232bSQu Wenruo static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter) 255a37f232bSQu Wenruo { 256a37f232bSQu Wenruo if (!iter) 257a37f232bSQu Wenruo return; 258a37f232bSQu Wenruo btrfs_free_path(iter->path); 259a37f232bSQu Wenruo kfree(iter); 260a37f232bSQu Wenruo } 261a37f232bSQu Wenruo 262c39c2ddcSQu Wenruo static inline struct extent_buffer *btrfs_backref_get_eb( 263c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 264c39c2ddcSQu Wenruo { 265c39c2ddcSQu Wenruo if (!iter) 266c39c2ddcSQu Wenruo return NULL; 267c39c2ddcSQu Wenruo return iter->path->nodes[0]; 268c39c2ddcSQu Wenruo } 269c39c2ddcSQu Wenruo 270c39c2ddcSQu Wenruo /* 271c39c2ddcSQu Wenruo * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data 272c39c2ddcSQu Wenruo * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header. 273c39c2ddcSQu Wenruo * 274c39c2ddcSQu Wenruo * This helper determines if that's the case. 275c39c2ddcSQu Wenruo */ 276c39c2ddcSQu Wenruo static inline bool btrfs_backref_has_tree_block_info( 277c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 278c39c2ddcSQu Wenruo { 279c39c2ddcSQu Wenruo if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY && 280c39c2ddcSQu Wenruo iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item)) 281c39c2ddcSQu Wenruo return true; 282c39c2ddcSQu Wenruo return false; 283c39c2ddcSQu Wenruo } 284c39c2ddcSQu Wenruo 285a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr); 286a37f232bSQu Wenruo 287c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter); 288c39c2ddcSQu Wenruo 289c39c2ddcSQu Wenruo static inline bool btrfs_backref_iter_is_inline_ref( 290c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 291c39c2ddcSQu Wenruo { 292c39c2ddcSQu Wenruo if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY || 293c39c2ddcSQu Wenruo iter->cur_key.type == BTRFS_METADATA_ITEM_KEY) 294c39c2ddcSQu Wenruo return true; 295c39c2ddcSQu Wenruo return false; 296c39c2ddcSQu Wenruo } 297c39c2ddcSQu Wenruo 298a37f232bSQu Wenruo static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter) 299a37f232bSQu Wenruo { 300a37f232bSQu Wenruo iter->bytenr = 0; 301a37f232bSQu Wenruo iter->item_ptr = 0; 302a37f232bSQu Wenruo iter->cur_ptr = 0; 303a37f232bSQu Wenruo iter->end_ptr = 0; 304a37f232bSQu Wenruo btrfs_release_path(iter->path); 305a37f232bSQu Wenruo memset(&iter->cur_key, 0, sizeof(iter->cur_key)); 306a37f232bSQu Wenruo } 307a37f232bSQu Wenruo 30870535441SQu Wenruo /* 30970535441SQu Wenruo * Backref cache related structures 31070535441SQu Wenruo * 31170535441SQu Wenruo * The whole objective of backref_cache is to build a bi-directional map 31270535441SQu Wenruo * of tree blocks (represented by backref_node) and all their parents. 31370535441SQu Wenruo */ 31470535441SQu Wenruo 31570535441SQu Wenruo /* 31670535441SQu Wenruo * Represent a tree block in the backref cache 31770535441SQu Wenruo */ 31870535441SQu Wenruo struct btrfs_backref_node { 319e9a28dc5SQu Wenruo struct { 32070535441SQu Wenruo struct rb_node rb_node; 32170535441SQu Wenruo u64 bytenr; 322e9a28dc5SQu Wenruo }; /* Use rb_simple_node for search/insert */ 32370535441SQu Wenruo 32470535441SQu Wenruo u64 new_bytenr; 32570535441SQu Wenruo /* Objectid of tree block owner, can be not uptodate */ 32670535441SQu Wenruo u64 owner; 32770535441SQu Wenruo /* Link to pending, changed or detached list */ 32870535441SQu Wenruo struct list_head list; 32970535441SQu Wenruo 33070535441SQu Wenruo /* List of upper level edges, which link this node to its parents */ 33170535441SQu Wenruo struct list_head upper; 33270535441SQu Wenruo /* List of lower level edges, which link this node to its children */ 33370535441SQu Wenruo struct list_head lower; 33470535441SQu Wenruo 33570535441SQu Wenruo /* NULL if this node is not tree root */ 33670535441SQu Wenruo struct btrfs_root *root; 33770535441SQu Wenruo /* Extent buffer got by COWing the block */ 33870535441SQu Wenruo struct extent_buffer *eb; 33970535441SQu Wenruo /* Level of the tree block */ 34070535441SQu Wenruo unsigned int level:8; 34192a7cc42SQu Wenruo /* Is the block in a non-shareable tree */ 34270535441SQu Wenruo unsigned int cowonly:1; 34370535441SQu Wenruo /* 1 if no child node is in the cache */ 34470535441SQu Wenruo unsigned int lowest:1; 34570535441SQu Wenruo /* Is the extent buffer locked */ 34670535441SQu Wenruo unsigned int locked:1; 34770535441SQu Wenruo /* Has the block been processed */ 34870535441SQu Wenruo unsigned int processed:1; 34970535441SQu Wenruo /* Have backrefs of this block been checked */ 35070535441SQu Wenruo unsigned int checked:1; 35170535441SQu Wenruo /* 35270535441SQu Wenruo * 1 if corresponding block has been COWed but some upper level block 35370535441SQu Wenruo * pointers may not point to the new location 35470535441SQu Wenruo */ 35570535441SQu Wenruo unsigned int pending:1; 35670535441SQu Wenruo /* 1 if the backref node isn't connected to any other backref node */ 35770535441SQu Wenruo unsigned int detached:1; 35870535441SQu Wenruo 35970535441SQu Wenruo /* 36070535441SQu Wenruo * For generic purpose backref cache, where we only care if it's a reloc 36170535441SQu Wenruo * root, doesn't care the source subvolid. 36270535441SQu Wenruo */ 36370535441SQu Wenruo unsigned int is_reloc_root:1; 36470535441SQu Wenruo }; 36570535441SQu Wenruo 36670535441SQu Wenruo #define LOWER 0 36770535441SQu Wenruo #define UPPER 1 36870535441SQu Wenruo 36970535441SQu Wenruo /* 37070535441SQu Wenruo * Represent an edge connecting upper and lower backref nodes. 37170535441SQu Wenruo */ 37270535441SQu Wenruo struct btrfs_backref_edge { 37370535441SQu Wenruo /* 37470535441SQu Wenruo * list[LOWER] is linked to btrfs_backref_node::upper of lower level 37570535441SQu Wenruo * node, and list[UPPER] is linked to btrfs_backref_node::lower of 37670535441SQu Wenruo * upper level node. 37770535441SQu Wenruo * 37870535441SQu Wenruo * Also, build_backref_tree() uses list[UPPER] for pending edges, before 37970535441SQu Wenruo * linking list[UPPER] to its upper level nodes. 38070535441SQu Wenruo */ 38170535441SQu Wenruo struct list_head list[2]; 38270535441SQu Wenruo 38370535441SQu Wenruo /* Two related nodes */ 38470535441SQu Wenruo struct btrfs_backref_node *node[2]; 38570535441SQu Wenruo }; 38670535441SQu Wenruo 38770535441SQu Wenruo struct btrfs_backref_cache { 38870535441SQu Wenruo /* Red black tree of all backref nodes in the cache */ 38970535441SQu Wenruo struct rb_root rb_root; 39070535441SQu Wenruo /* For passing backref nodes to btrfs_reloc_cow_block */ 39170535441SQu Wenruo struct btrfs_backref_node *path[BTRFS_MAX_LEVEL]; 39270535441SQu Wenruo /* 39370535441SQu Wenruo * List of blocks that have been COWed but some block pointers in upper 39470535441SQu Wenruo * level blocks may not reflect the new location 39570535441SQu Wenruo */ 39670535441SQu Wenruo struct list_head pending[BTRFS_MAX_LEVEL]; 39770535441SQu Wenruo /* List of backref nodes with no child node */ 39870535441SQu Wenruo struct list_head leaves; 39970535441SQu Wenruo /* List of blocks that have been COWed in current transaction */ 40070535441SQu Wenruo struct list_head changed; 40170535441SQu Wenruo /* List of detached backref node. */ 40270535441SQu Wenruo struct list_head detached; 40370535441SQu Wenruo 40470535441SQu Wenruo u64 last_trans; 40570535441SQu Wenruo 40670535441SQu Wenruo int nr_nodes; 40770535441SQu Wenruo int nr_edges; 40870535441SQu Wenruo 40970535441SQu Wenruo /* List of unchecked backref edges during backref cache build */ 41070535441SQu Wenruo struct list_head pending_edge; 41170535441SQu Wenruo 41270535441SQu Wenruo /* List of useless backref nodes during backref cache build */ 41370535441SQu Wenruo struct list_head useless_node; 41470535441SQu Wenruo 41570535441SQu Wenruo struct btrfs_fs_info *fs_info; 41670535441SQu Wenruo 41770535441SQu Wenruo /* 41870535441SQu Wenruo * Whether this cache is for relocation 41970535441SQu Wenruo * 42070535441SQu Wenruo * Reloction backref cache require more info for reloc root compared 42170535441SQu Wenruo * to generic backref cache. 42270535441SQu Wenruo */ 42370535441SQu Wenruo unsigned int is_reloc; 42470535441SQu Wenruo }; 42570535441SQu Wenruo 426584fb121SQu Wenruo void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 427584fb121SQu Wenruo struct btrfs_backref_cache *cache, int is_reloc); 428b1818dabSQu Wenruo struct btrfs_backref_node *btrfs_backref_alloc_node( 429b1818dabSQu Wenruo struct btrfs_backref_cache *cache, u64 bytenr, int level); 43047254d07SQu Wenruo struct btrfs_backref_edge *btrfs_backref_alloc_edge( 43147254d07SQu Wenruo struct btrfs_backref_cache *cache); 432584fb121SQu Wenruo 433f39911e5SQu Wenruo #define LINK_LOWER (1 << 0) 434f39911e5SQu Wenruo #define LINK_UPPER (1 << 1) 435f39911e5SQu Wenruo static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge, 436f39911e5SQu Wenruo struct btrfs_backref_node *lower, 437f39911e5SQu Wenruo struct btrfs_backref_node *upper, 438f39911e5SQu Wenruo int link_which) 439f39911e5SQu Wenruo { 440f39911e5SQu Wenruo ASSERT(upper && lower && upper->level == lower->level + 1); 441f39911e5SQu Wenruo edge->node[LOWER] = lower; 442f39911e5SQu Wenruo edge->node[UPPER] = upper; 443f39911e5SQu Wenruo if (link_which & LINK_LOWER) 444f39911e5SQu Wenruo list_add_tail(&edge->list[LOWER], &lower->upper); 445f39911e5SQu Wenruo if (link_which & LINK_UPPER) 446f39911e5SQu Wenruo list_add_tail(&edge->list[UPPER], &upper->lower); 447f39911e5SQu Wenruo } 448f39911e5SQu Wenruo 449741188d3SQu Wenruo static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache, 450741188d3SQu Wenruo struct btrfs_backref_node *node) 451741188d3SQu Wenruo { 452741188d3SQu Wenruo if (node) { 453eddda68dSJosef Bacik ASSERT(list_empty(&node->list)); 454eddda68dSJosef Bacik ASSERT(list_empty(&node->lower)); 455eddda68dSJosef Bacik ASSERT(node->eb == NULL); 456741188d3SQu Wenruo cache->nr_nodes--; 457741188d3SQu Wenruo btrfs_put_root(node->root); 458741188d3SQu Wenruo kfree(node); 459741188d3SQu Wenruo } 460741188d3SQu Wenruo } 461741188d3SQu Wenruo 462741188d3SQu Wenruo static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache, 463741188d3SQu Wenruo struct btrfs_backref_edge *edge) 464741188d3SQu Wenruo { 465741188d3SQu Wenruo if (edge) { 466741188d3SQu Wenruo cache->nr_edges--; 467741188d3SQu Wenruo kfree(edge); 468741188d3SQu Wenruo } 469741188d3SQu Wenruo } 470741188d3SQu Wenruo 471b0fe7078SQu Wenruo static inline void btrfs_backref_unlock_node_buffer( 472b0fe7078SQu Wenruo struct btrfs_backref_node *node) 473b0fe7078SQu Wenruo { 474b0fe7078SQu Wenruo if (node->locked) { 475b0fe7078SQu Wenruo btrfs_tree_unlock(node->eb); 476b0fe7078SQu Wenruo node->locked = 0; 477b0fe7078SQu Wenruo } 478b0fe7078SQu Wenruo } 479b0fe7078SQu Wenruo 480b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node_buffer( 481b0fe7078SQu Wenruo struct btrfs_backref_node *node) 482b0fe7078SQu Wenruo { 483b0fe7078SQu Wenruo if (node->eb) { 484b0fe7078SQu Wenruo btrfs_backref_unlock_node_buffer(node); 485b0fe7078SQu Wenruo free_extent_buffer(node->eb); 486b0fe7078SQu Wenruo node->eb = NULL; 487b0fe7078SQu Wenruo } 488b0fe7078SQu Wenruo } 489b0fe7078SQu Wenruo 490b0fe7078SQu Wenruo /* 491b0fe7078SQu Wenruo * Drop the backref node from cache without cleaning up its children 492b0fe7078SQu Wenruo * edges. 493b0fe7078SQu Wenruo * 494b0fe7078SQu Wenruo * This can only be called on node without parent edges. 495b0fe7078SQu Wenruo * The children edges are still kept as is. 496b0fe7078SQu Wenruo */ 497b0fe7078SQu Wenruo static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree, 498b0fe7078SQu Wenruo struct btrfs_backref_node *node) 499b0fe7078SQu Wenruo { 500eddda68dSJosef Bacik ASSERT(list_empty(&node->upper)); 501b0fe7078SQu Wenruo 502b0fe7078SQu Wenruo btrfs_backref_drop_node_buffer(node); 503eddda68dSJosef Bacik list_del_init(&node->list); 504eddda68dSJosef Bacik list_del_init(&node->lower); 505b0fe7078SQu Wenruo if (!RB_EMPTY_NODE(&node->rb_node)) 506b0fe7078SQu Wenruo rb_erase(&node->rb_node, &tree->rb_root); 507b0fe7078SQu Wenruo btrfs_backref_free_node(tree, node); 508b0fe7078SQu Wenruo } 509b0fe7078SQu Wenruo 510023acb07SQu Wenruo void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 511023acb07SQu Wenruo struct btrfs_backref_node *node); 512023acb07SQu Wenruo 51313fe1bdbSQu Wenruo void btrfs_backref_release_cache(struct btrfs_backref_cache *cache); 51413fe1bdbSQu Wenruo 515982c92cbSQu Wenruo static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info, 516982c92cbSQu Wenruo u64 bytenr, int errno) 517982c92cbSQu Wenruo { 518982c92cbSQu Wenruo btrfs_panic(fs_info, errno, 519982c92cbSQu Wenruo "Inconsistency in backref cache found at offset %llu", 520982c92cbSQu Wenruo bytenr); 521982c92cbSQu Wenruo } 522982c92cbSQu Wenruo 5231b60d2ecSQu Wenruo int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, 5241b60d2ecSQu Wenruo struct btrfs_path *path, 5251b60d2ecSQu Wenruo struct btrfs_backref_iter *iter, 5261b60d2ecSQu Wenruo struct btrfs_key *node_key, 5271b60d2ecSQu Wenruo struct btrfs_backref_node *cur); 5281b60d2ecSQu Wenruo 529fc997ed0SQu Wenruo int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 530fc997ed0SQu Wenruo struct btrfs_backref_node *start); 531fc997ed0SQu Wenruo 5321b23ea18SQu Wenruo void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 5331b23ea18SQu Wenruo struct btrfs_backref_node *node); 5341b23ea18SQu Wenruo 535a542ad1bSJan Schmidt #endif 536