^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #ifndef BTRFS_EXTENT_MAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #define BTRFS_EXTENT_MAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define EXTENT_MAP_LAST_BYTE ((u64)-4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define EXTENT_MAP_HOLE ((u64)-3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define EXTENT_MAP_INLINE ((u64)-2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* used only during fiemap calls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define EXTENT_MAP_DELALLOC ((u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* bits for the extent_map::flags field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* this entry not yet on disk, don't free it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) EXTENT_FLAG_PINNED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) EXTENT_FLAG_COMPRESSED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* pre-allocated extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) EXTENT_FLAG_PREALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* Logging this extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) EXTENT_FLAG_LOGGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* Filling in a preallocated extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) EXTENT_FLAG_FILLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* filesystem extent mapping type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) EXTENT_FLAG_FS_MAPPING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct extent_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct rb_node rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* all of these are in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u64 mod_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u64 mod_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) u64 orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u64 orig_block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) u64 ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u64 block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u64 block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u64 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Used for chunk mappings, flag EXTENT_FLAG_FS_MAPPING must be set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct map_lookup *map_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) refcount_t refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct extent_map_tree {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct rb_root_cached map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct list_head modified_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) rwlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static inline int extent_map_in_tree(const struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return !RB_EMPTY_NODE(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static inline u64 extent_map_end(struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (em->start + em->len < em->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return em->start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static inline u64 extent_map_block_end(struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (em->block_start + em->block_len < em->block_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return em->block_start + em->block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) void extent_map_tree_init(struct extent_map_tree *tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u64 start, u64 len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int add_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct extent_map *em, int modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) void replace_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct extent_map *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct extent_map *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct extent_map *alloc_extent_map(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void free_extent_map(struct extent_map *em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int __init extent_map_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) void __cold extent_map_exit(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) u64 start, u64 len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct extent_map_tree *em_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct extent_map **em_in, u64 start, u64 len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #endif