1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef BTRFS_EXTENT_IO_TREE_H
4 #define BTRFS_EXTENT_IO_TREE_H
8 struct extent_changeset;
10 /* Bits for the extent state */
12 ENUM_BIT(EXTENT_DIRTY),
13 ENUM_BIT(EXTENT_UPTODATE),
14 ENUM_BIT(EXTENT_LOCKED),
16 ENUM_BIT(EXTENT_DELALLOC),
17 ENUM_BIT(EXTENT_DEFRAG),
18 ENUM_BIT(EXTENT_BOUNDARY),
19 ENUM_BIT(EXTENT_NODATASUM),
20 ENUM_BIT(EXTENT_CLEAR_META_RESV),
21 ENUM_BIT(EXTENT_NEED_WAIT),
22 ENUM_BIT(EXTENT_NORESERVE),
23 ENUM_BIT(EXTENT_QGROUP_RESERVED),
24 ENUM_BIT(EXTENT_CLEAR_DATA_RESV),
26 * Must be cleared only during ordered extent completion or on error
27 * paths if we did not manage to submit bios and create the ordered
28 * extents for the range. Should not be cleared during page release
29 * and page invalidation (if there is an ordered extent in flight),
30 * that is left for the ordered extent completion.
32 ENUM_BIT(EXTENT_DELALLOC_NEW),
34 * When an ordered extent successfully completes for a region marked as
35 * a new delalloc range, use this flag when clearing a new delalloc
36 * range to indicate that the VFS' inode number of bytes should be
37 * incremented and the inode's new delalloc bytes decremented, in an
38 * atomic way to prevent races with stat(2).
40 ENUM_BIT(EXTENT_ADD_INODE_BYTES),
42 * Set during truncate when we're clearing an entire range and we just
43 * want the extent states to go away.
45 ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
50 * Bit not representing a state but a request for NOWAIT semantics,
51 * e.g. when allocating memory, and must be masked out from the other
54 ENUM_BIT(EXTENT_NOWAIT)
57 #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \
58 EXTENT_CLEAR_DATA_RESV)
59 #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \
60 EXTENT_ADD_INODE_BYTES | \
61 EXTENT_CLEAR_ALL_BITS)
64 * Redefined bits above which are used only in the device allocation tree,
65 * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
66 * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
67 * manipulation functions
69 #define CHUNK_ALLOCATED EXTENT_DIRTY
70 #define CHUNK_TRIMMED EXTENT_DEFRAG
71 #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \
75 IO_TREE_FS_PINNED_EXTENTS,
76 IO_TREE_FS_EXCLUDED_EXTENTS,
77 IO_TREE_BTREE_INODE_IO,
80 IO_TREE_TRANS_DIRTY_PAGES,
81 IO_TREE_ROOT_DIRTY_LOG_PAGES,
82 IO_TREE_INODE_FILE_EXTENT,
83 IO_TREE_LOG_CSUM_RANGE,
85 IO_TREE_DEVICE_ALLOC_STATE,
88 struct extent_io_tree {
91 * The fs_info is needed for trace points, a tree attached to an inode
94 * owner == IO_TREE_INODE_IO - then inode is valid and fs_info can be
95 * accessed as inode->root->fs_info
98 struct btrfs_fs_info *fs_info;
99 struct btrfs_inode *inode;
102 /* Who owns this io tree, should be one of IO_TREE_* */
108 struct extent_state {
110 u64 end; /* inclusive */
111 struct rb_node rb_node;
113 /* ADD NEW ELEMENTS AFTER THIS */
114 wait_queue_head_t wq;
118 #ifdef CONFIG_BTRFS_DEBUG
119 struct list_head leak_list;
123 struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree);
124 const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree);
125 const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
127 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
128 struct extent_io_tree *tree, unsigned int owner);
129 void extent_io_tree_release(struct extent_io_tree *tree);
131 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
132 struct extent_state **cached);
134 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
135 struct extent_state **cached);
137 int __init extent_state_init_cachep(void);
138 void __cold extent_state_free_cachep(void);
140 u64 count_range_bits(struct extent_io_tree *tree,
141 u64 *start, u64 search_end,
142 u64 max_bytes, u32 bits, int contig,
143 struct extent_state **cached_state);
145 void free_extent_state(struct extent_state *state);
146 bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
147 struct extent_state *cached_state);
148 bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
149 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
150 u32 bits, struct extent_changeset *changeset);
151 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
152 u32 bits, struct extent_state **cached,
153 struct extent_changeset *changeset);
155 static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start,
157 struct extent_state **cached)
159 return __clear_extent_bit(tree, start, end, bits, cached, NULL);
162 static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
163 struct extent_state **cached)
165 return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, NULL);
168 static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
171 return clear_extent_bit(tree, start, end, bits, NULL);
174 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
175 u32 bits, struct extent_changeset *changeset);
176 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
177 u32 bits, struct extent_state **cached_state);
179 static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
180 u64 end, struct extent_state **cached_state)
182 return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
186 static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
187 u64 end, struct extent_state **cached)
189 return clear_extent_bit(tree, start, end,
190 EXTENT_DIRTY | EXTENT_DELALLOC |
191 EXTENT_DO_ACCOUNTING, cached);
194 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
195 u32 bits, u32 clear_bits,
196 struct extent_state **cached_state);
198 bool find_first_extent_bit(struct extent_io_tree *tree, u64 start,
199 u64 *start_ret, u64 *end_ret, u32 bits,
200 struct extent_state **cached_state);
201 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
202 u64 *start_ret, u64 *end_ret, u32 bits);
203 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
204 u64 *start_ret, u64 *end_ret, u32 bits);
205 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
206 u64 *end, u64 max_bytes,
207 struct extent_state **cached_state);
209 #endif /* BTRFS_EXTENT_IO_TREE_H */