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 6d3b4d0fdSDavid Sterba #include "misc.h" 7d3b4d0fdSDavid Sterba 89c7d3a54SJosef Bacik struct extent_changeset; 99c7d3a54SJosef Bacik 109c7d3a54SJosef Bacik /* Bits for the extent state */ 11d3b4d0fdSDavid Sterba enum { 12d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DIRTY), 13d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_UPTODATE), 14d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_LOCKED), 15d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NEW), 16d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DELALLOC), 17d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DEFRAG), 18d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_BOUNDARY), 19d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NODATASUM), 20d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_CLEAR_META_RESV), 21d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NEED_WAIT), 22d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_NORESERVE), 23d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_QGROUP_RESERVED), 24d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_CLEAR_DATA_RESV), 252766ff61SFilipe Manana /* 26d3b4d0fdSDavid Sterba * Must be cleared only during ordered extent completion or on error 27d3b4d0fdSDavid Sterba * paths if we did not manage to submit bios and create the ordered 28d3b4d0fdSDavid Sterba * extents for the range. Should not be cleared during page release 29d3b4d0fdSDavid Sterba * and page invalidation (if there is an ordered extent in flight), 30d3b4d0fdSDavid Sterba * that is left for the ordered extent completion. 312766ff61SFilipe Manana */ 32d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_DELALLOC_NEW), 332766ff61SFilipe Manana /* 34d3b4d0fdSDavid Sterba * When an ordered extent successfully completes for a region marked as 35d3b4d0fdSDavid Sterba * a new delalloc range, use this flag when clearing a new delalloc 36d3b4d0fdSDavid Sterba * range to indicate that the VFS' inode number of bytes should be 37d3b4d0fdSDavid Sterba * incremented and the inode's new delalloc bytes decremented, in an 38d3b4d0fdSDavid Sterba * atomic way to prevent races with stat(2). 392766ff61SFilipe Manana */ 40d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_ADD_INODE_BYTES), 41bd015294SJosef Bacik /* 42d3b4d0fdSDavid Sterba * Set during truncate when we're clearing an entire range and we just 43d3b4d0fdSDavid Sterba * want the extent states to go away. 44bd015294SJosef Bacik */ 45d3b4d0fdSDavid Sterba ENUM_BIT(EXTENT_CLEAR_ALL_BITS), 46*62bc6047SDavid Sterba 47*62bc6047SDavid Sterba /* 48*62bc6047SDavid Sterba * This must be last. 49*62bc6047SDavid Sterba * 50*62bc6047SDavid Sterba * Bit not representing a state but a request for NOWAIT semantics, 51*62bc6047SDavid Sterba * e.g. when allocating memory, and must be masked out from the other 52*62bc6047SDavid Sterba * bits. 53*62bc6047SDavid Sterba */ 54*62bc6047SDavid Sterba ENUM_BIT(EXTENT_NOWAIT) 55d3b4d0fdSDavid Sterba }; 56bd015294SJosef Bacik 579c7d3a54SJosef Bacik #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \ 589c7d3a54SJosef Bacik EXTENT_CLEAR_DATA_RESV) 592766ff61SFilipe Manana #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \ 60bd015294SJosef Bacik EXTENT_ADD_INODE_BYTES | \ 61bd015294SJosef Bacik EXTENT_CLEAR_ALL_BITS) 629c7d3a54SJosef Bacik 639c7d3a54SJosef Bacik /* 649c7d3a54SJosef Bacik * Redefined bits above which are used only in the device allocation tree, 659c7d3a54SJosef Bacik * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV 669c7d3a54SJosef Bacik * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit 679c7d3a54SJosef Bacik * manipulation functions 689c7d3a54SJosef Bacik */ 699c7d3a54SJosef Bacik #define CHUNK_ALLOCATED EXTENT_DIRTY 709c7d3a54SJosef Bacik #define CHUNK_TRIMMED EXTENT_DEFRAG 71c57dd1f2SQu Wenruo #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \ 72c57dd1f2SQu Wenruo CHUNK_TRIMMED) 739c7d3a54SJosef Bacik 749c7d3a54SJosef Bacik enum { 75fe119a6eSNikolay Borisov IO_TREE_FS_PINNED_EXTENTS, 76fe119a6eSNikolay Borisov IO_TREE_FS_EXCLUDED_EXTENTS, 772c53a14dSQu Wenruo IO_TREE_BTREE_INODE_IO, 789c7d3a54SJosef Bacik IO_TREE_INODE_IO, 799c7d3a54SJosef Bacik IO_TREE_RELOC_BLOCKS, 809c7d3a54SJosef Bacik IO_TREE_TRANS_DIRTY_PAGES, 819c7d3a54SJosef Bacik IO_TREE_ROOT_DIRTY_LOG_PAGES, 8241a2ee75SJosef Bacik IO_TREE_INODE_FILE_EXTENT, 83e289f03eSFilipe Manana IO_TREE_LOG_CSUM_RANGE, 849c7d3a54SJosef Bacik IO_TREE_SELFTEST, 85154f7cb8SQu Wenruo IO_TREE_DEVICE_ALLOC_STATE, 869c7d3a54SJosef Bacik }; 879c7d3a54SJosef Bacik 889c7d3a54SJosef Bacik struct extent_io_tree { 899c7d3a54SJosef Bacik struct rb_root state; 909c7d3a54SJosef Bacik struct btrfs_fs_info *fs_info; 910988fc7bSDavid Sterba /* Inode associated with this tree, or NULL. */ 920988fc7bSDavid Sterba struct btrfs_inode *inode; 939c7d3a54SJosef Bacik 949c7d3a54SJosef Bacik /* Who owns this io tree, should be one of IO_TREE_* */ 959c7d3a54SJosef Bacik u8 owner; 969c7d3a54SJosef Bacik 979c7d3a54SJosef Bacik spinlock_t lock; 989c7d3a54SJosef Bacik }; 999c7d3a54SJosef Bacik 1009c7d3a54SJosef Bacik struct extent_state { 1019c7d3a54SJosef Bacik u64 start; 1029c7d3a54SJosef Bacik u64 end; /* inclusive */ 1039c7d3a54SJosef Bacik struct rb_node rb_node; 1049c7d3a54SJosef Bacik 1059c7d3a54SJosef Bacik /* ADD NEW ELEMENTS AFTER THIS */ 1069c7d3a54SJosef Bacik wait_queue_head_t wq; 1079c7d3a54SJosef Bacik refcount_t refs; 108f97e27e9SQu Wenruo u32 state; 1099c7d3a54SJosef Bacik 1109c7d3a54SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 1119c7d3a54SJosef Bacik struct list_head leak_list; 1129c7d3a54SJosef Bacik #endif 1139c7d3a54SJosef Bacik }; 1149c7d3a54SJosef Bacik 1159c7d3a54SJosef Bacik void extent_io_tree_init(struct btrfs_fs_info *fs_info, 11635da5a7eSDavid Sterba struct extent_io_tree *tree, unsigned int owner); 1179c7d3a54SJosef Bacik void extent_io_tree_release(struct extent_io_tree *tree); 1189c7d3a54SJosef Bacik 119570eb97bSJosef Bacik int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 1209c7d3a54SJosef Bacik struct extent_state **cached); 1219c7d3a54SJosef Bacik 12283ae4133SJosef Bacik int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 12383ae4133SJosef Bacik struct extent_state **cached); 1249c7d3a54SJosef Bacik 125a62a3bd9SJosef Bacik int __init extent_state_init_cachep(void); 126a62a3bd9SJosef Bacik void __cold extent_state_free_cachep(void); 1279c7d3a54SJosef Bacik 1289c7d3a54SJosef Bacik u64 count_range_bits(struct extent_io_tree *tree, 1299c7d3a54SJosef Bacik u64 *start, u64 search_end, 1308c6e53a7SFilipe Manana u64 max_bytes, u32 bits, int contig, 1318c6e53a7SFilipe Manana struct extent_state **cached_state); 1329c7d3a54SJosef Bacik 1339c7d3a54SJosef Bacik void free_extent_state(struct extent_state *state); 1349c7d3a54SJosef Bacik int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, 135f97e27e9SQu Wenruo u32 bits, int filled, struct extent_state *cached_state); 1369c7d3a54SJosef Bacik int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 137f97e27e9SQu Wenruo u32 bits, struct extent_changeset *changeset); 1389c7d3a54SJosef Bacik int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 139bd015294SJosef Bacik u32 bits, struct extent_state **cached, gfp_t mask, 140bd015294SJosef Bacik struct extent_changeset *changeset); 1419c7d3a54SJosef Bacik 142a6631887SJosef Bacik static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start, 143bd015294SJosef Bacik u64 end, u32 bits, 144a6631887SJosef Bacik struct extent_state **cached) 145a6631887SJosef Bacik { 146bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, bits, cached, 147dbbf4992SJosef Bacik GFP_NOFS, NULL); 148a6631887SJosef Bacik } 149a6631887SJosef Bacik 150570eb97bSJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, 151570eb97bSJosef Bacik struct extent_state **cached) 1529c7d3a54SJosef Bacik { 153bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, 1549c7d3a54SJosef Bacik GFP_NOFS, NULL); 1559c7d3a54SJosef Bacik } 1569c7d3a54SJosef Bacik 1579c7d3a54SJosef Bacik static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start, 158f97e27e9SQu Wenruo u64 end, u32 bits) 1599c7d3a54SJosef Bacik { 160bd015294SJosef Bacik return clear_extent_bit(tree, start, end, bits, NULL); 1619c7d3a54SJosef Bacik } 1629c7d3a54SJosef Bacik 1639c7d3a54SJosef Bacik int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 164f97e27e9SQu Wenruo u32 bits, struct extent_changeset *changeset); 1659c7d3a54SJosef Bacik int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 166291bbb1eSJosef Bacik u32 bits, struct extent_state **cached_state, gfp_t mask); 167a6631887SJosef Bacik 1689c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, 1699c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state) 1709c7d3a54SJosef Bacik { 171bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 1729c7d3a54SJosef Bacik cached_state, GFP_NOFS, NULL); 1739c7d3a54SJosef Bacik } 1749c7d3a54SJosef Bacik 1759c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start, 1769c7d3a54SJosef Bacik u64 end, struct extent_state **cached) 1779c7d3a54SJosef Bacik { 1789c7d3a54SJosef Bacik return clear_extent_bit(tree, start, end, 1799c7d3a54SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC | 180bd015294SJosef Bacik EXTENT_DO_ACCOUNTING, cached); 1819c7d3a54SJosef Bacik } 1829c7d3a54SJosef Bacik 1839c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, 184f97e27e9SQu Wenruo u32 bits, u32 clear_bits, 1859c7d3a54SJosef Bacik struct extent_state **cached_state); 1869c7d3a54SJosef Bacik 1879c7d3a54SJosef Bacik int find_first_extent_bit(struct extent_io_tree *tree, u64 start, 188f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits, 1899c7d3a54SJosef Bacik struct extent_state **cached_state); 1909c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, 191f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits); 19241a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, 193f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits); 194083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, 195083e75e7SJosef Bacik u64 *end, u64 max_bytes, 196083e75e7SJosef Bacik struct extent_state **cached_state); 197123a7f00SJosef Bacik void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, 198123a7f00SJosef Bacik struct extent_state **cached_state); 1999c7d3a54SJosef Bacik 2009c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */ 201