19c7d3a54SJosef Bacik /* SPDX-License-Identifier: GPL-2.0 */ 29c7d3a54SJosef Bacik 39c7d3a54SJosef Bacik #ifndef BTRFS_EXTENT_IO_TREE_H 49c7d3a54SJosef Bacik #define BTRFS_EXTENT_IO_TREE_H 59c7d3a54SJosef Bacik 6*d3b4d0fdSDavid Sterba #include "misc.h" 7*d3b4d0fdSDavid Sterba 89c7d3a54SJosef Bacik struct extent_changeset; 9b3f167aaSJosef Bacik struct io_failure_record; 109c7d3a54SJosef Bacik 119c7d3a54SJosef Bacik /* Bits for the extent state */ 12*d3b4d0fdSDavid Sterba enum { 13*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DIRTY), 14*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_UPTODATE), 15*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_LOCKED), 16*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NEW), 17*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DELALLOC), 18*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DEFRAG), 19*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_BOUNDARY), 20*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NODATASUM), 21*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_CLEAR_META_RESV), 22*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NEED_WAIT), 23*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NORESERVE), 24*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_QGROUP_RESERVED), 25*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_CLEAR_DATA_RESV), 262766ff61SFilipe Manana /* 27*d3b4d0fdSDavid Sterba * Must be cleared only during ordered extent completion or on error 28*d3b4d0fdSDavid Sterba * paths if we did not manage to submit bios and create the ordered 29*d3b4d0fdSDavid Sterba * extents for the range. Should not be cleared during page release 30*d3b4d0fdSDavid Sterba * and page invalidation (if there is an ordered extent in flight), 31*d3b4d0fdSDavid Sterba * that is left for the ordered extent completion. 322766ff61SFilipe Manana */ 33*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DELALLOC_NEW), 342766ff61SFilipe Manana /* 35*d3b4d0fdSDavid Sterba * When an ordered extent successfully completes for a region marked as 36*d3b4d0fdSDavid Sterba * a new delalloc range, use this flag when clearing a new delalloc 37*d3b4d0fdSDavid Sterba * range to indicate that the VFS' inode number of bytes should be 38*d3b4d0fdSDavid Sterba * incremented and the inode's new delalloc bytes decremented, in an 39*d3b4d0fdSDavid Sterba * atomic way to prevent races with stat(2). 402766ff61SFilipe Manana */ 41*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_ADD_INODE_BYTES), 42bd015294SJosef Bacik /* 43*d3b4d0fdSDavid Sterba * Set during truncate when we're clearing an entire range and we just 44*d3b4d0fdSDavid Sterba * want the extent states to go away. 45bd015294SJosef Bacik */ 46*d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_CLEAR_ALL_BITS), 47*d3b4d0fdSDavid Sterba }; 48bd015294SJosef Bacik 499c7d3a54SJosef Bacik #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \ 509c7d3a54SJosef Bacik EXTENT_CLEAR_DATA_RESV) 512766ff61SFilipe Manana #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \ 52bd015294SJosef Bacik EXTENT_ADD_INODE_BYTES | \ 53bd015294SJosef Bacik EXTENT_CLEAR_ALL_BITS) 549c7d3a54SJosef Bacik 559c7d3a54SJosef Bacik /* 569c7d3a54SJosef Bacik * Redefined bits above which are used only in the device allocation tree, 579c7d3a54SJosef Bacik * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV 589c7d3a54SJosef Bacik * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit 599c7d3a54SJosef Bacik * manipulation functions 609c7d3a54SJosef Bacik */ 619c7d3a54SJosef Bacik #define CHUNK_ALLOCATED EXTENT_DIRTY 629c7d3a54SJosef Bacik #define CHUNK_TRIMMED EXTENT_DEFRAG 63c57dd1f2SQu Wenruo #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \ 64c57dd1f2SQu Wenruo CHUNK_TRIMMED) 659c7d3a54SJosef Bacik 669c7d3a54SJosef Bacik enum { 67fe119a6eSNikolay Borisov IO_TREE_FS_PINNED_EXTENTS, 68fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, 692c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, 709c7d3a54SJosef Bacik IO_TREE_INODE_IO, 719c7d3a54SJosef Bacik IO_TREE_RELOC_BLOCKS, 729c7d3a54SJosef Bacik IO_TREE_TRANS_DIRTY_PAGES, 739c7d3a54SJosef Bacik IO_TREE_ROOT_DIRTY_LOG_PAGES, 7441a2ee75SJosef Bacik IO_TREE_INODE_FILE_EXTENT, 75e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, 769c7d3a54SJosef Bacik IO_TREE_SELFTEST, 77154f7cb8SQu Wenruo IO_TREE_DEVICE_ALLOC_STATE, 789c7d3a54SJosef Bacik }; 799c7d3a54SJosef Bacik 809c7d3a54SJosef Bacik struct extent_io_tree { 819c7d3a54SJosef Bacik struct rb_root state; 829c7d3a54SJosef Bacik struct btrfs_fs_info *fs_info; 839c7d3a54SJosef Bacik void *private_data; 849c7d3a54SJosef Bacik 859c7d3a54SJosef Bacik /* Who owns this io tree, should be one of IO_TREE_* */ 869c7d3a54SJosef Bacik u8 owner; 879c7d3a54SJosef Bacik 889c7d3a54SJosef Bacik spinlock_t lock; 899c7d3a54SJosef Bacik }; 909c7d3a54SJosef Bacik 919c7d3a54SJosef Bacik struct extent_state { 929c7d3a54SJosef Bacik u64 start; 939c7d3a54SJosef Bacik u64 end; /* inclusive */ 949c7d3a54SJosef Bacik struct rb_node rb_node; 959c7d3a54SJosef Bacik 969c7d3a54SJosef Bacik /* ADD NEW ELEMENTS AFTER THIS */ 979c7d3a54SJosef Bacik wait_queue_head_t wq; 989c7d3a54SJosef Bacik refcount_t refs; 99f97e27e9SQu Wenruo u32 state; 1009c7d3a54SJosef Bacik 1019c7d3a54SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1029c7d3a54SJosef Bacik struct list_head leak_list; 1039c7d3a54SJosef Bacik #endif 1049c7d3a54SJosef Bacik }; 1059c7d3a54SJosef Bacik 1069c7d3a54SJosef Bacik void extent_io_tree_init(struct btrfs_fs_info *fs_info, 1079c7d3a54SJosef Bacik struct extent_io_tree *tree, unsigned int owner, 1089c7d3a54SJosef Bacik void *private_data); 1099c7d3a54SJosef Bacik void extent_io_tree_release(struct extent_io_tree *tree); 1109c7d3a54SJosef Bacik 111570eb97bSJosef Bacik int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 1129c7d3a54SJosef Bacik struct extent_state **cached); 1139c7d3a54SJosef Bacik 11483ae4133SJosef Bacik int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 11583ae4133SJosef Bacik struct extent_state **cached); 1169c7d3a54SJosef Bacik 117a62a3bd9SJosef Bacik int __init extent_state_init_cachep(void); 118a62a3bd9SJosef Bacik void __cold extent_state_free_cachep(void); 1199c7d3a54SJosef Bacik 1209c7d3a54SJosef Bacik u64 count_range_bits(struct extent_io_tree *tree, 1219c7d3a54SJosef Bacik u64 *start, u64 search_end, 122f97e27e9SQu Wenruo u64 max_bytes, u32 bits, int contig); 1239c7d3a54SJosef Bacik 1249c7d3a54SJosef Bacik void free_extent_state(struct extent_state *state); 1259c7d3a54SJosef Bacik int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, 126f97e27e9SQu Wenruo u32 bits, int filled, struct extent_state *cached_state); 1279c7d3a54SJosef Bacik int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 128f97e27e9SQu Wenruo u32 bits, struct extent_changeset *changeset); 1299c7d3a54SJosef Bacik int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 130bd015294SJosef Bacik u32 bits, struct extent_state **cached, gfp_t mask, 131bd015294SJosef Bacik struct extent_changeset *changeset); 1329c7d3a54SJosef Bacik 133a6631887SJosef Bacik static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start, 134bd015294SJosef Bacik u64 end, u32 bits, 135a6631887SJosef Bacik struct extent_state **cached) 136a6631887SJosef Bacik { 137bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, bits, cached, 138dbbf4992SJosef Bacik GFP_NOFS, NULL); 139a6631887SJosef Bacik } 140a6631887SJosef Bacik 141570eb97bSJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, 142570eb97bSJosef Bacik struct extent_state **cached) 1439c7d3a54SJosef Bacik { 144bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, 1459c7d3a54SJosef Bacik GFP_NOFS, NULL); 1469c7d3a54SJosef Bacik } 1479c7d3a54SJosef Bacik 148570eb97bSJosef Bacik static inline int unlock_extent_atomic(struct extent_io_tree *tree, u64 start, 149570eb97bSJosef Bacik u64 end, struct extent_state **cached) 1509c7d3a54SJosef Bacik { 151bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, 1529c7d3a54SJosef Bacik GFP_ATOMIC, NULL); 1539c7d3a54SJosef Bacik } 1549c7d3a54SJosef Bacik 1559c7d3a54SJosef Bacik static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start, 156f97e27e9SQu Wenruo u64 end, u32 bits) 1579c7d3a54SJosef Bacik { 158bd015294SJosef Bacik return clear_extent_bit(tree, start, end, bits, NULL); 1599c7d3a54SJosef Bacik } 1609c7d3a54SJosef Bacik 1619c7d3a54SJosef Bacik int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 162f97e27e9SQu Wenruo u32 bits, struct extent_changeset *changeset); 1639c7d3a54SJosef Bacik int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 164291bbb1eSJosef Bacik u32 bits, struct extent_state **cached_state, gfp_t mask); 165a6631887SJosef Bacik 166a6631887SJosef Bacik static inline int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, 167a6631887SJosef Bacik u64 end, u32 bits) 168a6631887SJosef Bacik { 169291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, bits, NULL, GFP_NOWAIT); 170a6631887SJosef Bacik } 1719c7d3a54SJosef Bacik 1729c7d3a54SJosef Bacik static inline int set_extent_bits(struct extent_io_tree *tree, u64 start, 173f97e27e9SQu Wenruo u64 end, u32 bits) 1749c7d3a54SJosef Bacik { 175291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, bits, NULL, GFP_NOFS); 1769c7d3a54SJosef Bacik } 1779c7d3a54SJosef Bacik 1789c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, 1799c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state) 1809c7d3a54SJosef Bacik { 181bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 1829c7d3a54SJosef Bacik cached_state, GFP_NOFS, NULL); 1839c7d3a54SJosef Bacik } 1849c7d3a54SJosef Bacik 1859c7d3a54SJosef Bacik static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start, 1869c7d3a54SJosef Bacik u64 end, gfp_t mask) 1879c7d3a54SJosef Bacik { 188291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, mask); 1899c7d3a54SJosef Bacik } 1909c7d3a54SJosef Bacik 1919c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start, 1929c7d3a54SJosef Bacik u64 end, struct extent_state **cached) 1939c7d3a54SJosef Bacik { 1949c7d3a54SJosef Bacik return clear_extent_bit(tree, start, end, 1959c7d3a54SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC | 196bd015294SJosef Bacik EXTENT_DO_ACCOUNTING, cached); 1979c7d3a54SJosef Bacik } 1989c7d3a54SJosef Bacik 1999c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 200f97e27e9SQu Wenruo u32 bits, u32 clear_bits, 2019c7d3a54SJosef Bacik struct extent_state **cached_state); 2029c7d3a54SJosef Bacik 2039c7d3a54SJosef Bacik static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start, 204f97e27e9SQu Wenruo u64 end, u32 extra_bits, 2059c7d3a54SJosef Bacik struct extent_state **cached_state) 2069c7d3a54SJosef Bacik { 2079c7d3a54SJosef Bacik return set_extent_bit(tree, start, end, 20852b029f4SEthan Lien EXTENT_DELALLOC | extra_bits, 209291bbb1eSJosef Bacik cached_state, GFP_NOFS); 2109c7d3a54SJosef Bacik } 2119c7d3a54SJosef Bacik 2129c7d3a54SJosef Bacik static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start, 2139c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state) 2149c7d3a54SJosef Bacik { 2159c7d3a54SJosef Bacik return set_extent_bit(tree, start, end, 21652b029f4SEthan Lien EXTENT_DELALLOC | EXTENT_DEFRAG, 217291bbb1eSJosef Bacik cached_state, GFP_NOFS); 2189c7d3a54SJosef Bacik } 2199c7d3a54SJosef Bacik 2209c7d3a54SJosef Bacik static inline int set_extent_new(struct extent_io_tree *tree, u64 start, 2219c7d3a54SJosef Bacik u64 end) 2229c7d3a54SJosef Bacik { 223291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, GFP_NOFS); 2249c7d3a54SJosef Bacik } 2259c7d3a54SJosef Bacik 2269c7d3a54SJosef Bacik static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start, 2279c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state, gfp_t mask) 2289c7d3a54SJosef Bacik { 229994bcd1eSJosef Bacik return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 230291bbb1eSJosef Bacik cached_state, mask); 2319c7d3a54SJosef Bacik } 2329c7d3a54SJosef Bacik 2339c7d3a54SJosef Bacik int find_first_extent_bit(struct extent_io_tree *tree, u64 start, 234f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits, 2359c7d3a54SJosef Bacik struct extent_state **cached_state); 2369c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, 237f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits); 23841a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, 239f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits); 240083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, 241083e75e7SJosef Bacik u64 *end, u64 max_bytes, 242083e75e7SJosef Bacik struct extent_state **cached_state); 243123a7f00SJosef Bacik void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, 244123a7f00SJosef Bacik struct extent_state **cached_state); 2459c7d3a54SJosef Bacik 2469c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */ 247