18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci#ifndef BTRFS_EXTENT_MAP_H 48c2ecf20Sopenharmony_ci#define BTRFS_EXTENT_MAP_H 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/rbtree.h> 78c2ecf20Sopenharmony_ci#include <linux/refcount.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#define EXTENT_MAP_LAST_BYTE ((u64)-4) 108c2ecf20Sopenharmony_ci#define EXTENT_MAP_HOLE ((u64)-3) 118c2ecf20Sopenharmony_ci#define EXTENT_MAP_INLINE ((u64)-2) 128c2ecf20Sopenharmony_ci/* used only during fiemap calls */ 138c2ecf20Sopenharmony_ci#define EXTENT_MAP_DELALLOC ((u64)-1) 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* bits for the extent_map::flags field */ 168c2ecf20Sopenharmony_cienum { 178c2ecf20Sopenharmony_ci /* this entry not yet on disk, don't free it */ 188c2ecf20Sopenharmony_ci EXTENT_FLAG_PINNED, 198c2ecf20Sopenharmony_ci EXTENT_FLAG_COMPRESSED, 208c2ecf20Sopenharmony_ci /* pre-allocated extent */ 218c2ecf20Sopenharmony_ci EXTENT_FLAG_PREALLOC, 228c2ecf20Sopenharmony_ci /* Logging this extent */ 238c2ecf20Sopenharmony_ci EXTENT_FLAG_LOGGING, 248c2ecf20Sopenharmony_ci /* Filling in a preallocated extent */ 258c2ecf20Sopenharmony_ci EXTENT_FLAG_FILLING, 268c2ecf20Sopenharmony_ci /* filesystem extent mapping type */ 278c2ecf20Sopenharmony_ci EXTENT_FLAG_FS_MAPPING, 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct extent_map { 318c2ecf20Sopenharmony_ci struct rb_node rb_node; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci /* all of these are in bytes */ 348c2ecf20Sopenharmony_ci u64 start; 358c2ecf20Sopenharmony_ci u64 len; 368c2ecf20Sopenharmony_ci u64 mod_start; 378c2ecf20Sopenharmony_ci u64 mod_len; 388c2ecf20Sopenharmony_ci u64 orig_start; 398c2ecf20Sopenharmony_ci u64 orig_block_len; 408c2ecf20Sopenharmony_ci u64 ram_bytes; 418c2ecf20Sopenharmony_ci u64 block_start; 428c2ecf20Sopenharmony_ci u64 block_len; 438c2ecf20Sopenharmony_ci u64 generation; 448c2ecf20Sopenharmony_ci unsigned long flags; 458c2ecf20Sopenharmony_ci /* Used for chunk mappings, flag EXTENT_FLAG_FS_MAPPING must be set */ 468c2ecf20Sopenharmony_ci struct map_lookup *map_lookup; 478c2ecf20Sopenharmony_ci refcount_t refs; 488c2ecf20Sopenharmony_ci unsigned int compress_type; 498c2ecf20Sopenharmony_ci struct list_head list; 508c2ecf20Sopenharmony_ci}; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistruct extent_map_tree { 538c2ecf20Sopenharmony_ci struct rb_root_cached map; 548c2ecf20Sopenharmony_ci struct list_head modified_extents; 558c2ecf20Sopenharmony_ci rwlock_t lock; 568c2ecf20Sopenharmony_ci}; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic inline int extent_map_in_tree(const struct extent_map *em) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci return !RB_EMPTY_NODE(&em->rb_node); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic inline u64 extent_map_end(struct extent_map *em) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci if (em->start + em->len < em->start) 668c2ecf20Sopenharmony_ci return (u64)-1; 678c2ecf20Sopenharmony_ci return em->start + em->len; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic inline u64 extent_map_block_end(struct extent_map *em) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci if (em->block_start + em->block_len < em->block_start) 738c2ecf20Sopenharmony_ci return (u64)-1; 748c2ecf20Sopenharmony_ci return em->block_start + em->block_len; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_civoid extent_map_tree_init(struct extent_map_tree *tree); 788c2ecf20Sopenharmony_cistruct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, 798c2ecf20Sopenharmony_ci u64 start, u64 len); 808c2ecf20Sopenharmony_ciint add_extent_mapping(struct extent_map_tree *tree, 818c2ecf20Sopenharmony_ci struct extent_map *em, int modified); 828c2ecf20Sopenharmony_civoid remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em); 838c2ecf20Sopenharmony_civoid replace_extent_mapping(struct extent_map_tree *tree, 848c2ecf20Sopenharmony_ci struct extent_map *cur, 858c2ecf20Sopenharmony_ci struct extent_map *new, 868c2ecf20Sopenharmony_ci int modified); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistruct extent_map *alloc_extent_map(void); 898c2ecf20Sopenharmony_civoid free_extent_map(struct extent_map *em); 908c2ecf20Sopenharmony_ciint __init extent_map_init(void); 918c2ecf20Sopenharmony_civoid __cold extent_map_exit(void); 928c2ecf20Sopenharmony_ciint unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen); 938c2ecf20Sopenharmony_civoid clear_em_logging(struct extent_map_tree *tree, struct extent_map *em); 948c2ecf20Sopenharmony_cistruct extent_map *search_extent_mapping(struct extent_map_tree *tree, 958c2ecf20Sopenharmony_ci u64 start, u64 len); 968c2ecf20Sopenharmony_ciint btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, 978c2ecf20Sopenharmony_ci struct extent_map_tree *em_tree, 988c2ecf20Sopenharmony_ci struct extent_map **em_in, u64 start, u64 len); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci#endif 101