xref: /linux/fs/btrfs/extent-io-tree.h (revision 602035d7)
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_UPTODATE),
21 	ENUM_BIT(EXTENT_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 	 * When an ordered extent successfully completes for a region marked as
42 	 * a new delalloc range, use this flag when clearing a new delalloc
43 	 * range to indicate that the VFS' inode number of bytes should be
44 	 * incremented and the inode's new delalloc bytes decremented, in an
45 	 * atomic way to prevent races with stat(2).
46 	 */
47 	ENUM_BIT(EXTENT_ADD_INODE_BYTES),
48 	/*
49 	 * Set during truncate when we're clearing an entire range and we just
50 	 * want the extent states to go away.
51 	 */
52 	ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
53 
54 	/*
55 	 * This must be last.
56 	 *
57 	 * Bit not representing a state but a request for NOWAIT semantics,
58 	 * e.g. when allocating memory, and must be masked out from the other
59 	 * bits.
60 	 */
61 	ENUM_BIT(EXTENT_NOWAIT)
62 };
63 
64 #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
65 				 EXTENT_CLEAR_DATA_RESV)
66 #define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING | \
67 				 EXTENT_ADD_INODE_BYTES | \
68 				 EXTENT_CLEAR_ALL_BITS)
69 
70 /*
71  * Redefined bits above which are used only in the device allocation tree,
72  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
73  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
74  * manipulation functions
75  */
76 #define CHUNK_ALLOCATED				EXTENT_DIRTY
77 #define CHUNK_TRIMMED				EXTENT_DEFRAG
78 #define CHUNK_STATE_MASK			(CHUNK_ALLOCATED |		\
79 						 CHUNK_TRIMMED)
80 
81 enum {
82 	IO_TREE_FS_PINNED_EXTENTS,
83 	IO_TREE_FS_EXCLUDED_EXTENTS,
84 	IO_TREE_BTREE_INODE_IO,
85 	IO_TREE_INODE_IO,
86 	IO_TREE_RELOC_BLOCKS,
87 	IO_TREE_TRANS_DIRTY_PAGES,
88 	IO_TREE_ROOT_DIRTY_LOG_PAGES,
89 	IO_TREE_INODE_FILE_EXTENT,
90 	IO_TREE_LOG_CSUM_RANGE,
91 	IO_TREE_SELFTEST,
92 	IO_TREE_DEVICE_ALLOC_STATE,
93 };
94 
95 struct extent_io_tree {
96 	struct rb_root state;
97 	/*
98 	 * The fs_info is needed for trace points, a tree attached to an inode
99 	 * needs the inode.
100 	 *
101 	 * owner == IO_TREE_INODE_IO - then inode is valid and fs_info can be
102 	 *                             accessed as inode->root->fs_info
103 	 */
104 	union {
105 		struct btrfs_fs_info *fs_info;
106 		struct btrfs_inode *inode;
107 	};
108 
109 	/* Who owns this io tree, should be one of IO_TREE_* */
110 	u8 owner;
111 
112 	spinlock_t lock;
113 };
114 
115 struct extent_state {
116 	u64 start;
117 	u64 end; /* inclusive */
118 	struct rb_node rb_node;
119 
120 	/* ADD NEW ELEMENTS AFTER THIS */
121 	wait_queue_head_t wq;
122 	refcount_t refs;
123 	u32 state;
124 
125 #ifdef CONFIG_BTRFS_DEBUG
126 	struct list_head leak_list;
127 #endif
128 };
129 
130 struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree);
131 const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree);
132 const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
133 
134 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
135 			 struct extent_io_tree *tree, unsigned int owner);
136 void extent_io_tree_release(struct extent_io_tree *tree);
137 
138 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
139 		struct extent_state **cached);
140 
141 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
142 		    struct extent_state **cached);
143 
144 int __init extent_state_init_cachep(void);
145 void __cold extent_state_free_cachep(void);
146 
147 u64 count_range_bits(struct extent_io_tree *tree,
148 		     u64 *start, u64 search_end,
149 		     u64 max_bytes, u32 bits, int contig,
150 		     struct extent_state **cached_state);
151 
152 void free_extent_state(struct extent_state *state);
153 bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
154 		    struct extent_state *cached_state);
155 bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
156 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
157 			     u32 bits, struct extent_changeset *changeset);
158 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
159 		       u32 bits, struct extent_state **cached,
160 		       struct extent_changeset *changeset);
161 
clear_extent_bit(struct extent_io_tree * tree,u64 start,u64 end,u32 bits,struct extent_state ** cached)162 static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start,
163 				   u64 end, u32 bits,
164 				   struct extent_state **cached)
165 {
166 	return __clear_extent_bit(tree, start, end, bits, cached, NULL);
167 }
168 
unlock_extent(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)169 static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
170 				struct extent_state **cached)
171 {
172 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, cached, NULL);
173 }
174 
clear_extent_bits(struct extent_io_tree * tree,u64 start,u64 end,u32 bits)175 static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
176 				    u64 end, u32 bits)
177 {
178 	return clear_extent_bit(tree, start, end, bits, NULL);
179 }
180 
181 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
182 			   u32 bits, struct extent_changeset *changeset);
183 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
184 		   u32 bits, struct extent_state **cached_state);
185 
clear_extent_uptodate(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached_state)186 static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
187 		u64 end, struct extent_state **cached_state)
188 {
189 	return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
190 				  cached_state, NULL);
191 }
192 
clear_extent_dirty(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)193 static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
194 				     u64 end, struct extent_state **cached)
195 {
196 	return clear_extent_bit(tree, start, end,
197 				EXTENT_DIRTY | EXTENT_DELALLOC |
198 				EXTENT_DO_ACCOUNTING, cached);
199 }
200 
201 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
202 		       u32 bits, u32 clear_bits,
203 		       struct extent_state **cached_state);
204 
205 bool find_first_extent_bit(struct extent_io_tree *tree, u64 start,
206 			   u64 *start_ret, u64 *end_ret, u32 bits,
207 			   struct extent_state **cached_state);
208 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
209 				 u64 *start_ret, u64 *end_ret, u32 bits);
210 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
211 			       u64 *start_ret, u64 *end_ret, u32 bits);
212 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
213 			       u64 *end, u64 max_bytes,
214 			       struct extent_state **cached_state);
215 
216 #endif /* BTRFS_EXTENT_IO_TREE_H */
217