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 69c7d3a54SJosef Bacik struct extent_changeset; 7b3f167aaSJosef Bacik struct io_failure_record; 89c7d3a54SJosef Bacik 99c7d3a54SJosef Bacik /* Bits for the extent state */ 109c7d3a54SJosef Bacik #define EXTENT_DIRTY (1U << 0) 119c7d3a54SJosef Bacik #define EXTENT_UPTODATE (1U << 1) 129c7d3a54SJosef Bacik #define EXTENT_LOCKED (1U << 2) 139c7d3a54SJosef Bacik #define EXTENT_NEW (1U << 3) 149c7d3a54SJosef Bacik #define EXTENT_DELALLOC (1U << 4) 159c7d3a54SJosef Bacik #define EXTENT_DEFRAG (1U << 5) 169c7d3a54SJosef Bacik #define EXTENT_BOUNDARY (1U << 6) 179c7d3a54SJosef Bacik #define EXTENT_NODATASUM (1U << 7) 189c7d3a54SJosef Bacik #define EXTENT_CLEAR_META_RESV (1U << 8) 199c7d3a54SJosef Bacik #define EXTENT_NEED_WAIT (1U << 9) 209c7d3a54SJosef Bacik #define EXTENT_NORESERVE (1U << 11) 219c7d3a54SJosef Bacik #define EXTENT_QGROUP_RESERVED (1U << 12) 229c7d3a54SJosef Bacik #define EXTENT_CLEAR_DATA_RESV (1U << 13) 232766ff61SFilipe Manana /* 242766ff61SFilipe Manana * Must be cleared only during ordered extent completion or on error paths if we 252766ff61SFilipe Manana * did not manage to submit bios and create the ordered extents for the range. 262766ff61SFilipe Manana * Should not be cleared during page release and page invalidation (if there is 272766ff61SFilipe Manana * an ordered extent in flight), that is left for the ordered extent completion. 282766ff61SFilipe Manana */ 299c7d3a54SJosef Bacik #define EXTENT_DELALLOC_NEW (1U << 14) 302766ff61SFilipe Manana /* 312766ff61SFilipe Manana * When an ordered extent successfully completes for a region marked as a new 322766ff61SFilipe Manana * delalloc range, use this flag when clearing a new delalloc range to indicate 332766ff61SFilipe Manana * that the VFS' inode number of bytes should be incremented and the inode's new 342766ff61SFilipe Manana * delalloc bytes decremented, in an atomic way to prevent races with stat(2). 352766ff61SFilipe Manana */ 362766ff61SFilipe Manana #define EXTENT_ADD_INODE_BYTES (1U << 15) 37bd015294SJosef Bacik 38bd015294SJosef Bacik /* 39bd015294SJosef Bacik * Set during truncate when we're clearing an entire range and we just want the 40bd015294SJosef Bacik * extent states to go away. 41bd015294SJosef Bacik */ 42bd015294SJosef Bacik #define EXTENT_CLEAR_ALL_BITS (1U << 16) 43bd015294SJosef Bacik 449c7d3a54SJosef Bacik #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \ 459c7d3a54SJosef Bacik EXTENT_CLEAR_DATA_RESV) 462766ff61SFilipe Manana #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \ 47bd015294SJosef Bacik EXTENT_ADD_INODE_BYTES | \ 48bd015294SJosef Bacik EXTENT_CLEAR_ALL_BITS) 499c7d3a54SJosef Bacik 509c7d3a54SJosef Bacik /* 519c7d3a54SJosef Bacik * Redefined bits above which are used only in the device allocation tree, 529c7d3a54SJosef Bacik * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV 539c7d3a54SJosef Bacik * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit 549c7d3a54SJosef Bacik * manipulation functions 559c7d3a54SJosef Bacik */ 569c7d3a54SJosef Bacik #define CHUNK_ALLOCATED EXTENT_DIRTY 579c7d3a54SJosef Bacik #define CHUNK_TRIMMED EXTENT_DEFRAG 58c57dd1f2SQu Wenruo #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \ 59c57dd1f2SQu Wenruo CHUNK_TRIMMED) 609c7d3a54SJosef Bacik 619c7d3a54SJosef Bacik enum { 62fe119a6eSNikolay Borisov IO_TREE_FS_PINNED_EXTENTS, 63fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, 642c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, 659c7d3a54SJosef Bacik IO_TREE_INODE_IO, 669c7d3a54SJosef Bacik IO_TREE_RELOC_BLOCKS, 679c7d3a54SJosef Bacik IO_TREE_TRANS_DIRTY_PAGES, 689c7d3a54SJosef Bacik IO_TREE_ROOT_DIRTY_LOG_PAGES, 6941a2ee75SJosef Bacik IO_TREE_INODE_FILE_EXTENT, 70e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, 719c7d3a54SJosef Bacik IO_TREE_SELFTEST, 72154f7cb8SQu Wenruo IO_TREE_DEVICE_ALLOC_STATE, 739c7d3a54SJosef Bacik }; 749c7d3a54SJosef Bacik 759c7d3a54SJosef Bacik struct extent_io_tree { 769c7d3a54SJosef Bacik struct rb_root state; 779c7d3a54SJosef Bacik struct btrfs_fs_info *fs_info; 789c7d3a54SJosef Bacik void *private_data; 799c7d3a54SJosef Bacik 809c7d3a54SJosef Bacik /* Who owns this io tree, should be one of IO_TREE_* */ 819c7d3a54SJosef Bacik u8 owner; 829c7d3a54SJosef Bacik 839c7d3a54SJosef Bacik spinlock_t lock; 849c7d3a54SJosef Bacik }; 859c7d3a54SJosef Bacik 869c7d3a54SJosef Bacik struct extent_state { 879c7d3a54SJosef Bacik u64 start; 889c7d3a54SJosef Bacik u64 end; /* inclusive */ 899c7d3a54SJosef Bacik struct rb_node rb_node; 909c7d3a54SJosef Bacik 919c7d3a54SJosef Bacik /* ADD NEW ELEMENTS AFTER THIS */ 929c7d3a54SJosef Bacik wait_queue_head_t wq; 939c7d3a54SJosef Bacik refcount_t refs; 94f97e27e9SQu Wenruo u32 state; 959c7d3a54SJosef Bacik 969c7d3a54SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 979c7d3a54SJosef Bacik struct list_head leak_list; 989c7d3a54SJosef Bacik #endif 999c7d3a54SJosef Bacik }; 1009c7d3a54SJosef Bacik 1019c7d3a54SJosef Bacik void extent_io_tree_init(struct btrfs_fs_info *fs_info, 1029c7d3a54SJosef Bacik struct extent_io_tree *tree, unsigned int owner, 1039c7d3a54SJosef Bacik void *private_data); 1049c7d3a54SJosef Bacik void extent_io_tree_release(struct extent_io_tree *tree); 1059c7d3a54SJosef Bacik 106570eb97bSJosef Bacik int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 1079c7d3a54SJosef Bacik struct extent_state **cached); 1089c7d3a54SJosef Bacik 109*83ae4133SJosef Bacik int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 110*83ae4133SJosef Bacik struct extent_state **cached); 1119c7d3a54SJosef Bacik 112a62a3bd9SJosef Bacik int __init extent_state_init_cachep(void); 113a62a3bd9SJosef Bacik void __cold extent_state_free_cachep(void); 1149c7d3a54SJosef Bacik 1159c7d3a54SJosef Bacik u64 count_range_bits(struct extent_io_tree *tree, 1169c7d3a54SJosef Bacik u64 *start, u64 search_end, 117f97e27e9SQu Wenruo u64 max_bytes, u32 bits, int contig); 1189c7d3a54SJosef Bacik 1199c7d3a54SJosef Bacik void free_extent_state(struct extent_state *state); 1209c7d3a54SJosef Bacik int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, 121f97e27e9SQu Wenruo u32 bits, int filled, struct extent_state *cached_state); 1229c7d3a54SJosef Bacik int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 123f97e27e9SQu Wenruo u32 bits, struct extent_changeset *changeset); 1249c7d3a54SJosef Bacik int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 125bd015294SJosef Bacik u32 bits, struct extent_state **cached, gfp_t mask, 126bd015294SJosef Bacik struct extent_changeset *changeset); 1279c7d3a54SJosef Bacik 128a6631887SJosef Bacik static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start, 129bd015294SJosef Bacik u64 end, u32 bits, 130a6631887SJosef Bacik struct extent_state **cached) 131a6631887SJosef Bacik { 132bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, bits, cached, 133dbbf4992SJosef Bacik GFP_NOFS, NULL); 134a6631887SJosef Bacik } 135a6631887SJosef Bacik 136570eb97bSJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, 137570eb97bSJosef Bacik struct extent_state **cached) 1389c7d3a54SJosef Bacik { 139bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, 1409c7d3a54SJosef Bacik GFP_NOFS, NULL); 1419c7d3a54SJosef Bacik } 1429c7d3a54SJosef Bacik 143570eb97bSJosef Bacik static inline int unlock_extent_atomic(struct extent_io_tree *tree, u64 start, 144570eb97bSJosef Bacik u64 end, struct extent_state **cached) 1459c7d3a54SJosef Bacik { 146bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, 1479c7d3a54SJosef Bacik GFP_ATOMIC, NULL); 1489c7d3a54SJosef Bacik } 1499c7d3a54SJosef Bacik 1509c7d3a54SJosef Bacik static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start, 151f97e27e9SQu Wenruo u64 end, u32 bits) 1529c7d3a54SJosef Bacik { 153bd015294SJosef Bacik return clear_extent_bit(tree, start, end, bits, NULL); 1549c7d3a54SJosef Bacik } 1559c7d3a54SJosef Bacik 1569c7d3a54SJosef Bacik int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 157f97e27e9SQu Wenruo u32 bits, struct extent_changeset *changeset); 1589c7d3a54SJosef Bacik int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 159291bbb1eSJosef Bacik u32 bits, struct extent_state **cached_state, gfp_t mask); 160a6631887SJosef Bacik 161a6631887SJosef Bacik static inline int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, 162a6631887SJosef Bacik u64 end, u32 bits) 163a6631887SJosef Bacik { 164291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, bits, NULL, GFP_NOWAIT); 165a6631887SJosef Bacik } 1669c7d3a54SJosef Bacik 1679c7d3a54SJosef Bacik static inline int set_extent_bits(struct extent_io_tree *tree, u64 start, 168f97e27e9SQu Wenruo u64 end, u32 bits) 1699c7d3a54SJosef Bacik { 170291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, bits, NULL, GFP_NOFS); 1719c7d3a54SJosef Bacik } 1729c7d3a54SJosef Bacik 1739c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, 1749c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state) 1759c7d3a54SJosef Bacik { 176bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 1779c7d3a54SJosef Bacik cached_state, GFP_NOFS, NULL); 1789c7d3a54SJosef Bacik } 1799c7d3a54SJosef Bacik 1809c7d3a54SJosef Bacik static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start, 1819c7d3a54SJosef Bacik u64 end, gfp_t mask) 1829c7d3a54SJosef Bacik { 183291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, mask); 1849c7d3a54SJosef Bacik } 1859c7d3a54SJosef Bacik 1869c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start, 1879c7d3a54SJosef Bacik u64 end, struct extent_state **cached) 1889c7d3a54SJosef Bacik { 1899c7d3a54SJosef Bacik return clear_extent_bit(tree, start, end, 1909c7d3a54SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC | 191bd015294SJosef Bacik EXTENT_DO_ACCOUNTING, cached); 1929c7d3a54SJosef Bacik } 1939c7d3a54SJosef Bacik 1949c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 195f97e27e9SQu Wenruo u32 bits, u32 clear_bits, 1969c7d3a54SJosef Bacik struct extent_state **cached_state); 1979c7d3a54SJosef Bacik 1989c7d3a54SJosef Bacik static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start, 199f97e27e9SQu Wenruo u64 end, u32 extra_bits, 2009c7d3a54SJosef Bacik struct extent_state **cached_state) 2019c7d3a54SJosef Bacik { 2029c7d3a54SJosef Bacik return set_extent_bit(tree, start, end, 20352b029f4SEthan Lien EXTENT_DELALLOC | extra_bits, 204291bbb1eSJosef Bacik cached_state, GFP_NOFS); 2059c7d3a54SJosef Bacik } 2069c7d3a54SJosef Bacik 2079c7d3a54SJosef Bacik static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start, 2089c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state) 2099c7d3a54SJosef Bacik { 2109c7d3a54SJosef Bacik return set_extent_bit(tree, start, end, 21152b029f4SEthan Lien EXTENT_DELALLOC | EXTENT_DEFRAG, 212291bbb1eSJosef Bacik cached_state, GFP_NOFS); 2139c7d3a54SJosef Bacik } 2149c7d3a54SJosef Bacik 2159c7d3a54SJosef Bacik static inline int set_extent_new(struct extent_io_tree *tree, u64 start, 2169c7d3a54SJosef Bacik u64 end) 2179c7d3a54SJosef Bacik { 218291bbb1eSJosef Bacik return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, GFP_NOFS); 2199c7d3a54SJosef Bacik } 2209c7d3a54SJosef Bacik 2219c7d3a54SJosef Bacik static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start, 2229c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state, gfp_t mask) 2239c7d3a54SJosef Bacik { 224994bcd1eSJosef Bacik return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 225291bbb1eSJosef Bacik cached_state, mask); 2269c7d3a54SJosef Bacik } 2279c7d3a54SJosef Bacik 2289c7d3a54SJosef Bacik int find_first_extent_bit(struct extent_io_tree *tree, u64 start, 229f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits, 2309c7d3a54SJosef Bacik struct extent_state **cached_state); 2319c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, 232f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits); 23341a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, 234f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits); 235083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, 236083e75e7SJosef Bacik u64 *end, u64 max_bytes, 237083e75e7SJosef Bacik struct extent_state **cached_state); 238ec39e39bSJosef Bacik void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits); 2399c7d3a54SJosef Bacik 2409c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */ 241