1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_EXTENT_IO_TREE_H
4 #define BTRFS_EXTENT_IO_TREE_H
5 
6 #include <linux/rbtree.h>
7 #include <linux/spinlock.h>
8 #include <linux/refcount.h>
9 #include <linux/list.h>
10 #include <linux/wait.h>
11 #include "misc.h"
12 
13 struct extent_changeset;
14 struct btrfs_fs_info;
15 struct btrfs_inode;
16 
17 /* Bits for the extent state */
18 enum {
19 	ENUM_BIT(EXTENT_DIRTY),
20 	ENUM_BIT(EXTENT_LOCKED),
21 	ENUM_BIT(EXTENT_DIO_LOCKED),
22 	ENUM_BIT(EXTENT_NEW),
23 	ENUM_BIT(EXTENT_DELALLOC),
24 	ENUM_BIT(EXTENT_DEFRAG),
25 	ENUM_BIT(EXTENT_BOUNDARY),
26 	ENUM_BIT(EXTENT_NODATASUM),
27 	ENUM_BIT(EXTENT_CLEAR_META_RESV),
28 	ENUM_BIT(EXTENT_NEED_WAIT),
29 	ENUM_BIT(EXTENT_NORESERVE),
30 	ENUM_BIT(EXTENT_QGROUP_RESERVED),
31 	ENUM_BIT(EXTENT_CLEAR_DATA_RESV),
32 	/*
33 	 * Must be cleared only during ordered extent completion or on error
34 	 * paths if we did not manage to submit bios and create the ordered
35 	 * extents for the range.  Should not be cleared during page release
36 	 * and page invalidation (if there is an ordered extent in flight),
37 	 * that is left for the ordered extent completion.
38 	 */
39 	ENUM_BIT(EXTENT_DELALLOC_NEW),
40 	/*
41 	 * Mark that a range is being locked for finishing an ordered extent.
42 	 * Used together with EXTENT_LOCKED.
43 	 */
44 	ENUM_BIT(EXTENT_FINISHING_ORDERED),
45 	/*
46 	 * When an ordered extent successfully completes for a region marked as
47 	 * a new delalloc range, use this flag when clearing a new delalloc
48 	 * range to indicate that the VFS' inode number of bytes should be
49 	 * incremented and the inode's new delalloc bytes decremented, in an
50 	 * atomic way to prevent races with stat(2).
51 	 */
52 	ENUM_BIT(EXTENT_ADD_INODE_BYTES),
53 	/*
54 	 * Set during truncate when we're clearing an entire range and we just
55 	 * want the extent states to go away.
56 	 */
57 	ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
58 
59 	/*
60 	 * This must be last.
61 	 *
62 	 * Bit not representing a state but a request for NOWAIT semantics,
63 	 * e.g. when allocating memory, and must be masked out from the other
64 	 * bits.
65 	 */
66 	ENUM_BIT(EXTENT_NOWAIT)
67 };
68 
69 #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
70 				 EXTENT_CLEAR_DATA_RESV)
71 #define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING | \
72 				 EXTENT_ADD_INODE_BYTES | \
73 				 EXTENT_CLEAR_ALL_BITS)
74 
75 #define EXTENT_LOCK_BITS	(EXTENT_LOCKED | EXTENT_DIO_LOCKED)
76 
77 /*
78  * Redefined bits above which are used only in the device allocation tree,
79  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
80  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
81  * manipulation functions
82  */
83 #define CHUNK_ALLOCATED				EXTENT_DIRTY
84 #define CHUNK_TRIMMED				EXTENT_DEFRAG
85 #define CHUNK_STATE_MASK			(CHUNK_ALLOCATED |		\
86 						 CHUNK_TRIMMED)
87 
88 enum {
89 	IO_TREE_FS_PINNED_EXTENTS,
90 	IO_TREE_FS_EXCLUDED_EXTENTS,
91 	IO_TREE_BTREE_INODE_IO,
92 	IO_TREE_INODE_IO,
93 	IO_TREE_RELOC_BLOCKS,
94 	IO_TREE_TRANS_DIRTY_PAGES,
95 	IO_TREE_ROOT_DIRTY_LOG_PAGES,
96 	IO_TREE_INODE_FILE_EXTENT,
97 	IO_TREE_LOG_CSUM_RANGE,
98 	IO_TREE_SELFTEST,
99 	IO_TREE_DEVICE_ALLOC_STATE,
100 };
101 
102 struct extent_io_tree {
103 	struct rb_root state;
104 	/*
105 	 * The fs_info is needed for trace points, a tree attached to an inode
106 	 * needs the inode.
107 	 *
108 	 * owner == IO_TREE_INODE_IO - then inode is valid and fs_info can be
109 	 *                             accessed as inode->root->fs_info
110 	 */
111 	union {
112 		struct btrfs_fs_info *fs_info;
113 		struct btrfs_inode *inode;
114 	};
115 
116 	/* Who owns this io tree, should be one of IO_TREE_* */
117 	u8 owner;
118 
119 	spinlock_t lock;
120 };
121 
122 struct extent_state {
123 	u64 start;
124 	u64 end; /* inclusive */
125 	struct rb_node rb_node;
126 
127 	/* ADD NEW ELEMENTS AFTER THIS */
128 	wait_queue_head_t wq;
129 	refcount_t refs;
130 	u32 state;
131 
132 #ifdef CONFIG_BTRFS_DEBUG
133 	struct list_head leak_list;
134 #endif
135 };
136 
137 const struct btrfs_inode *btrfs_extent_io_tree_to_inode(const struct extent_io_tree *tree);
138 const struct btrfs_fs_info *btrfs_extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
139 
140 void btrfs_extent_io_tree_init(struct btrfs_fs_info *fs_info,
141 			       struct extent_io_tree *tree, unsigned int owner);
142 void btrfs_extent_io_tree_release(struct extent_io_tree *tree);
143 int btrfs_lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
144 			   struct extent_state **cached);
145 bool btrfs_try_lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
146 				u32 bits, struct extent_state **cached);
147 
148 static inline int btrfs_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
149 				    struct extent_state **cached)
150 {
151 	return btrfs_lock_extent_bits(tree, start, end, EXTENT_LOCKED, cached);
152 }
153 
154 static inline bool btrfs_try_lock_extent(struct extent_io_tree *tree, u64 start,
155 					 u64 end, struct extent_state **cached)
156 {
157 	return btrfs_try_lock_extent_bits(tree, start, end, EXTENT_LOCKED, cached);
158 }
159 
160 int __init btrfs_extent_state_init_cachep(void);
161 void __cold btrfs_extent_state_free_cachep(void);
162 
163 u64 btrfs_count_range_bits(struct extent_io_tree *tree,
164 			   u64 *start, u64 search_end,
165 			   u64 max_bytes, u32 bits, int contig,
166 			   struct extent_state **cached_state);
167 
168 void btrfs_free_extent_state(struct extent_state *state);
169 bool btrfs_test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
170 			  struct extent_state *cached_state);
171 bool btrfs_test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
172 void btrfs_get_range_bits(struct extent_io_tree *tree, u64 start, u64 end, u32 *bits,
173 			  struct extent_state **cached_state);
174 int btrfs_clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
175 				   u32 bits, struct extent_changeset *changeset);
176 int btrfs_clear_extent_bit_changeset(struct extent_io_tree *tree, u64 start, u64 end,
177 				     u32 bits, struct extent_state **cached,
178 				     struct extent_changeset *changeset);
179 
180 static inline int btrfs_clear_extent_bit(struct extent_io_tree *tree, u64 start,
181 					 u64 end, u32 bits,
182 					 struct extent_state **cached)
183 {
184 	return btrfs_clear_extent_bit_changeset(tree, start, end, bits, cached, NULL);
185 }
186 
187 static inline int btrfs_unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
188 				      struct extent_state **cached)
189 {
190 	return btrfs_clear_extent_bit_changeset(tree, start, end, EXTENT_LOCKED,
191 						cached, NULL);
192 }
193 
194 static inline int btrfs_clear_extent_bits(struct extent_io_tree *tree, u64 start,
195 					  u64 end, u32 bits)
196 {
197 	return btrfs_clear_extent_bit(tree, start, end, bits, NULL);
198 }
199 
200 int btrfs_set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
201 				 u32 bits, struct extent_changeset *changeset);
202 int btrfs_set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
203 			 u32 bits, struct extent_state **cached_state);
204 
205 static inline int btrfs_clear_extent_dirty(struct extent_io_tree *tree, u64 start,
206 					   u64 end, struct extent_state **cached)
207 {
208 	return btrfs_clear_extent_bit(tree, start, end,
209 				      EXTENT_DIRTY | EXTENT_DELALLOC |
210 				      EXTENT_DO_ACCOUNTING, cached);
211 }
212 
213 int btrfs_convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
214 			     u32 bits, u32 clear_bits,
215 			     struct extent_state **cached_state);
216 
217 bool btrfs_find_first_extent_bit(struct extent_io_tree *tree, u64 start,
218 				 u64 *start_ret, u64 *end_ret, u32 bits,
219 				 struct extent_state **cached_state);
220 void btrfs_find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
221 				       u64 *start_ret, u64 *end_ret, u32 bits);
222 bool btrfs_find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
223 				      u64 *start_ret, u64 *end_ret, u32 bits);
224 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
225 			       u64 *end, u64 max_bytes,
226 			       struct extent_state **cached_state);
227 static inline int btrfs_lock_dio_extent(struct extent_io_tree *tree, u64 start,
228 					u64 end, struct extent_state **cached)
229 {
230 	return btrfs_lock_extent_bits(tree, start, end, EXTENT_DIO_LOCKED, cached);
231 }
232 
233 static inline bool btrfs_try_lock_dio_extent(struct extent_io_tree *tree, u64 start,
234 					     u64 end, struct extent_state **cached)
235 {
236 	return btrfs_try_lock_extent_bits(tree, start, end, EXTENT_DIO_LOCKED, cached);
237 }
238 
239 static inline int btrfs_unlock_dio_extent(struct extent_io_tree *tree, u64 start,
240 					  u64 end, struct extent_state **cached)
241 {
242 	return btrfs_clear_extent_bit_changeset(tree, start, end, EXTENT_DIO_LOCKED,
243 						cached, NULL);
244 }
245 
246 struct extent_state *btrfs_next_extent_state(struct extent_io_tree *tree,
247 					     struct extent_state *state);
248 
249 #endif /* BTRFS_EXTENT_IO_TREE_H */
250