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> 108da6d581SJan Schmidt #include "ulist.h" 1191cb916cSAlexander Block #include "extent_io.h" 12a542ad1bSJan Schmidt 13a542ad1bSJan Schmidt struct inode_fs_paths { 14a542ad1bSJan Schmidt struct btrfs_path *btrfs_path; 15a542ad1bSJan Schmidt struct btrfs_root *fs_root; 16a542ad1bSJan Schmidt struct btrfs_data_container *fspath; 17a542ad1bSJan Schmidt }; 18a542ad1bSJan Schmidt 19a542ad1bSJan Schmidt typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 root, 20a542ad1bSJan Schmidt void *ctx); 21a542ad1bSJan Schmidt 22a542ad1bSJan Schmidt int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 2369917e43SLiu Bo struct btrfs_path *path, struct btrfs_key *found_key, 2469917e43SLiu Bo u64 *flags); 25a542ad1bSJan Schmidt 26a542ad1bSJan Schmidt int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 276eda71d0SLiu Bo struct btrfs_key *key, struct btrfs_extent_item *ei, 286eda71d0SLiu Bo u32 item_size, u64 *out_root, u8 *out_level); 29a542ad1bSJan Schmidt 30a542ad1bSJan Schmidt int iterate_extent_inodes(struct btrfs_fs_info *fs_info, 31a542ad1bSJan Schmidt u64 extent_item_objectid, 327a3ae2f8SJan Schmidt u64 extent_offset, int search_commit_root, 33c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 34c995ab3cSZygo Blaxell bool ignore_offset); 35a542ad1bSJan Schmidt 36a542ad1bSJan Schmidt int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 37a542ad1bSJan Schmidt struct btrfs_path *path, 38c995ab3cSZygo Blaxell iterate_extent_inodes_t *iterate, void *ctx, 39c995ab3cSZygo Blaxell bool ignore_offset); 40a542ad1bSJan Schmidt 41a542ad1bSJan Schmidt int paths_from_inode(u64 inum, struct inode_fs_paths *ipath); 42a542ad1bSJan Schmidt 4319b546d7SQu Wenruo int btrfs_find_all_leafs(struct btrfs_trans_handle *trans, 4419b546d7SQu Wenruo struct btrfs_fs_info *fs_info, u64 bytenr, 4519b546d7SQu Wenruo u64 time_seq, struct ulist **leafs, 4619b546d7SQu Wenruo const u64 *extent_item_pos, bool ignore_offset); 478da6d581SJan Schmidt int btrfs_find_all_roots(struct btrfs_trans_handle *trans, 488da6d581SJan Schmidt struct btrfs_fs_info *fs_info, u64 bytenr, 49c995ab3cSZygo Blaxell u64 time_seq, struct ulist **roots, bool ignore_offset); 5096b5bd77SJan Schmidt char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 5196b5bd77SJan Schmidt u32 name_len, unsigned long name_off, 5296b5bd77SJan Schmidt struct extent_buffer *eb_in, u64 parent, 5396b5bd77SJan Schmidt char *dest, u32 size); 548da6d581SJan Schmidt 55a542ad1bSJan Schmidt struct btrfs_data_container *init_data_container(u32 total_bytes); 56a542ad1bSJan Schmidt struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 57a542ad1bSJan Schmidt struct btrfs_path *path); 58a542ad1bSJan Schmidt void free_ipath(struct inode_fs_paths *ipath); 59a542ad1bSJan Schmidt 60f186373fSMark Fasheh int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 61f186373fSMark Fasheh u64 start_off, struct btrfs_path *path, 62f186373fSMark Fasheh struct btrfs_inode_extref **ret_extref, 63f186373fSMark Fasheh u64 *found_off); 645911c8feSDavid Sterba int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr, 655911c8feSDavid Sterba struct ulist *roots, struct ulist *tmp_ulist); 66f186373fSMark Fasheh 67b9e9a6cbSWang Shilong int __init btrfs_prelim_ref_init(void); 68e67c718bSDavid Sterba void __cold btrfs_prelim_ref_exit(void); 6900142756SJeff Mahoney 7000142756SJeff Mahoney struct prelim_ref { 7100142756SJeff Mahoney struct rb_node rbnode; 7200142756SJeff Mahoney u64 root_id; 7300142756SJeff Mahoney struct btrfs_key key_for_search; 7400142756SJeff Mahoney int level; 7500142756SJeff Mahoney int count; 7600142756SJeff Mahoney struct extent_inode_elem *inode_list; 7700142756SJeff Mahoney u64 parent; 7800142756SJeff Mahoney u64 wanted_disk_byte; 7900142756SJeff Mahoney }; 8000142756SJeff Mahoney 81a37f232bSQu Wenruo /* 82a37f232bSQu Wenruo * Iterate backrefs of one extent. 83a37f232bSQu Wenruo * 84a37f232bSQu Wenruo * Now it only supports iteration of tree block in commit root. 85a37f232bSQu Wenruo */ 86a37f232bSQu Wenruo struct btrfs_backref_iter { 87a37f232bSQu Wenruo u64 bytenr; 88a37f232bSQu Wenruo struct btrfs_path *path; 89a37f232bSQu Wenruo struct btrfs_fs_info *fs_info; 90a37f232bSQu Wenruo struct btrfs_key cur_key; 91a37f232bSQu Wenruo u32 item_ptr; 92a37f232bSQu Wenruo u32 cur_ptr; 93a37f232bSQu Wenruo u32 end_ptr; 94a37f232bSQu Wenruo }; 95a37f232bSQu Wenruo 96a37f232bSQu Wenruo struct btrfs_backref_iter *btrfs_backref_iter_alloc( 97a37f232bSQu Wenruo struct btrfs_fs_info *fs_info, gfp_t gfp_flag); 98a37f232bSQu Wenruo 99a37f232bSQu Wenruo static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter) 100a37f232bSQu Wenruo { 101a37f232bSQu Wenruo if (!iter) 102a37f232bSQu Wenruo return; 103a37f232bSQu Wenruo btrfs_free_path(iter->path); 104a37f232bSQu Wenruo kfree(iter); 105a37f232bSQu Wenruo } 106a37f232bSQu Wenruo 107c39c2ddcSQu Wenruo static inline struct extent_buffer *btrfs_backref_get_eb( 108c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 109c39c2ddcSQu Wenruo { 110c39c2ddcSQu Wenruo if (!iter) 111c39c2ddcSQu Wenruo return NULL; 112c39c2ddcSQu Wenruo return iter->path->nodes[0]; 113c39c2ddcSQu Wenruo } 114c39c2ddcSQu Wenruo 115c39c2ddcSQu Wenruo /* 116c39c2ddcSQu Wenruo * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data 117c39c2ddcSQu Wenruo * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header. 118c39c2ddcSQu Wenruo * 119c39c2ddcSQu Wenruo * This helper determines if that's the case. 120c39c2ddcSQu Wenruo */ 121c39c2ddcSQu Wenruo static inline bool btrfs_backref_has_tree_block_info( 122c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 123c39c2ddcSQu Wenruo { 124c39c2ddcSQu Wenruo if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY && 125c39c2ddcSQu Wenruo iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item)) 126c39c2ddcSQu Wenruo return true; 127c39c2ddcSQu Wenruo return false; 128c39c2ddcSQu Wenruo } 129c39c2ddcSQu Wenruo 130a37f232bSQu Wenruo int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr); 131a37f232bSQu Wenruo 132c39c2ddcSQu Wenruo int btrfs_backref_iter_next(struct btrfs_backref_iter *iter); 133c39c2ddcSQu Wenruo 134c39c2ddcSQu Wenruo static inline bool btrfs_backref_iter_is_inline_ref( 135c39c2ddcSQu Wenruo struct btrfs_backref_iter *iter) 136c39c2ddcSQu Wenruo { 137c39c2ddcSQu Wenruo if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY || 138c39c2ddcSQu Wenruo iter->cur_key.type == BTRFS_METADATA_ITEM_KEY) 139c39c2ddcSQu Wenruo return true; 140c39c2ddcSQu Wenruo return false; 141c39c2ddcSQu Wenruo } 142c39c2ddcSQu Wenruo 143a37f232bSQu Wenruo static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter) 144a37f232bSQu Wenruo { 145a37f232bSQu Wenruo iter->bytenr = 0; 146a37f232bSQu Wenruo iter->item_ptr = 0; 147a37f232bSQu Wenruo iter->cur_ptr = 0; 148a37f232bSQu Wenruo iter->end_ptr = 0; 149a37f232bSQu Wenruo btrfs_release_path(iter->path); 150a37f232bSQu Wenruo memset(&iter->cur_key, 0, sizeof(iter->cur_key)); 151a37f232bSQu Wenruo } 152a37f232bSQu Wenruo 153a542ad1bSJan Schmidt #endif 154