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),
4662bc6047SDavid Sterba
4762bc6047SDavid Sterba /*
4862bc6047SDavid Sterba * This must be last.
4962bc6047SDavid Sterba *
5062bc6047SDavid Sterba * Bit not representing a state but a request for NOWAIT semantics,
5162bc6047SDavid Sterba * e.g. when allocating memory, and must be masked out from the other
5262bc6047SDavid Sterba * bits.
5362bc6047SDavid Sterba */
5462bc6047SDavid 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,
1391d126800SDavid Sterba u32 bits, struct extent_state **cached,
140bd015294SJosef Bacik struct extent_changeset *changeset);
1419c7d3a54SJosef Bacik
clear_extent_bit(struct extent_io_tree * tree,u64 start,u64 end,u32 bits,struct extent_state ** cached)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 {
1461d126800SDavid Sterba return __clear_extent_bit(tree, start, end, bits, cached, NULL);
147a6631887SJosef Bacik }
148a6631887SJosef Bacik
unlock_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)149570eb97bSJosef Bacik static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
150570eb97bSJosef Bacik struct extent_state **cached)
1519c7d3a54SJosef Bacik {
1521d126800SDavid Sterba return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, NULL);
1539c7d3a54SJosef Bacik }
1549c7d3a54SJosef Bacik
clear_extent_bits(struct extent_io_tree * tree,u64 start,u64 end,u32 bits)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,
1641d126800SDavid Sterba u32 bits, struct extent_state **cached_state);
165a6631887SJosef Bacik
clear_extent_uptodate(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached_state)1669c7d3a54SJosef Bacik static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
1679c7d3a54SJosef Bacik u64 end, struct extent_state **cached_state)
1689c7d3a54SJosef Bacik {
169bd015294SJosef Bacik return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
1701d126800SDavid Sterba cached_state, NULL);
1719c7d3a54SJosef Bacik }
1729c7d3a54SJosef Bacik
clear_extent_dirty(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)1739c7d3a54SJosef Bacik static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
1749c7d3a54SJosef Bacik u64 end, struct extent_state **cached)
1759c7d3a54SJosef Bacik {
1769c7d3a54SJosef Bacik return clear_extent_bit(tree, start, end,
1779c7d3a54SJosef Bacik EXTENT_DIRTY | EXTENT_DELALLOC |
178bd015294SJosef Bacik EXTENT_DO_ACCOUNTING, cached);
1799c7d3a54SJosef Bacik }
1809c7d3a54SJosef Bacik
1819c7d3a54SJosef Bacik int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
182f97e27e9SQu Wenruo u32 bits, u32 clear_bits,
1839c7d3a54SJosef Bacik struct extent_state **cached_state);
1849c7d3a54SJosef Bacik
185*e5860f82SFilipe Manana bool find_first_extent_bit(struct extent_io_tree *tree, u64 start,
186f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits,
1879c7d3a54SJosef Bacik struct extent_state **cached_state);
1889c7d3a54SJosef Bacik void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
189f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits);
19041a2ee75SJosef Bacik int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
191f97e27e9SQu Wenruo u64 *start_ret, u64 *end_ret, u32 bits);
192083e75e7SJosef Bacik bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
193083e75e7SJosef Bacik u64 *end, u64 max_bytes,
194083e75e7SJosef Bacik struct extent_state **cached_state);
195123a7f00SJosef Bacik void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
196123a7f00SJosef Bacik struct extent_state **cached_state);
1979c7d3a54SJosef Bacik
1989c7d3a54SJosef Bacik #endif /* BTRFS_EXTENT_IO_TREE_H */
199