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),
48 #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \
49 EXTENT_CLEAR_DATA_RESV)
50 #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \
51 EXTENT_ADD_INODE_BYTES | \
52 EXTENT_CLEAR_ALL_BITS)
55 * Redefined bits above which are used only in the device allocation tree,
56 * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
57 * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
58 * manipulation functions
60 #define CHUNK_ALLOCATED EXTENT_DIRTY
61 #define CHUNK_TRIMMED EXTENT_DEFRAG
62 #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \
66 IO_TREE_FS_PINNED_EXTENTS,
67 IO_TREE_FS_EXCLUDED_EXTENTS,
68 IO_TREE_BTREE_INODE_IO,
71 IO_TREE_TRANS_DIRTY_PAGES,
72 IO_TREE_ROOT_DIRTY_LOG_PAGES,
73 IO_TREE_INODE_FILE_EXTENT,
74 IO_TREE_LOG_CSUM_RANGE,
76 IO_TREE_DEVICE_ALLOC_STATE,
79 struct extent_io_tree {
81 struct btrfs_fs_info *fs_info;
82 /* Inode associated with this tree, or NULL. */
83 struct btrfs_inode *inode;
85 /* Who owns this io tree, should be one of IO_TREE_* */
93 u64 end; /* inclusive */
94 struct rb_node rb_node;
96 /* ADD NEW ELEMENTS AFTER THIS */
101 #ifdef CONFIG_BTRFS_DEBUG
102 struct list_head leak_list;
106 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
107 struct extent_io_tree *tree, unsigned int owner);
108 void extent_io_tree_release(struct extent_io_tree *tree);
110 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
111 struct extent_state **cached);
113 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
114 struct extent_state **cached);
116 int __init extent_state_init_cachep(void);
117 void __cold extent_state_free_cachep(void);
119 u64 count_range_bits(struct extent_io_tree *tree,
120 u64 *start, u64 search_end,
121 u64 max_bytes, u32 bits, int contig,
122 struct extent_state **cached_state);
124 void free_extent_state(struct extent_state *state);
125 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
126 u32 bits, int filled, struct extent_state *cached_state);
127 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
128 u32 bits, struct extent_changeset *changeset);
129 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
130 u32 bits, struct extent_state **cached, gfp_t mask,
131 struct extent_changeset *changeset);
133 static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start,
135 struct extent_state **cached)
137 return __clear_extent_bit(tree, start, end, bits, cached,
141 static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
142 struct extent_state **cached)
144 return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached,
148 static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
151 return clear_extent_bit(tree, start, end, bits, NULL);
154 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
155 u32 bits, struct extent_changeset *changeset);
156 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
157 u32 bits, struct extent_state **cached_state, gfp_t mask);
159 static inline int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start,
162 return set_extent_bit(tree, start, end, bits, NULL, GFP_NOWAIT);
165 static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
168 return set_extent_bit(tree, start, end, bits, NULL, GFP_NOFS);
171 static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
172 u64 end, struct extent_state **cached_state)
174 return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
175 cached_state, GFP_NOFS, NULL);
178 static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
181 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, mask);
184 static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
185 u64 end, struct extent_state **cached)
187 return clear_extent_bit(tree, start, end,
188 EXTENT_DIRTY | EXTENT_DELALLOC |
189 EXTENT_DO_ACCOUNTING, cached);
192 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
193 u32 bits, u32 clear_bits,
194 struct extent_state **cached_state);
196 static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
197 u64 end, u32 extra_bits,
198 struct extent_state **cached_state)
200 return set_extent_bit(tree, start, end,
201 EXTENT_DELALLOC | extra_bits,
202 cached_state, GFP_NOFS);
205 static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
206 u64 end, struct extent_state **cached_state)
208 return set_extent_bit(tree, start, end,
209 EXTENT_DELALLOC | EXTENT_DEFRAG,
210 cached_state, GFP_NOFS);
213 static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
216 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, GFP_NOFS);
219 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
220 u64 *start_ret, u64 *end_ret, u32 bits,
221 struct extent_state **cached_state);
222 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
223 u64 *start_ret, u64 *end_ret, u32 bits);
224 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
225 u64 *start_ret, u64 *end_ret, u32 bits);
226 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
227 u64 *end, u64 max_bytes,
228 struct extent_state **cached_state);
229 void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
230 struct extent_state **cached_state);
232 #endif /* BTRFS_EXTENT_IO_TREE_H */