xref: /OK3568_Linux_fs/kernel/fs/btrfs/extent-io-tree.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #ifndef BTRFS_EXTENT_IO_TREE_H
4*4882a593Smuzhiyun #define BTRFS_EXTENT_IO_TREE_H
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun struct extent_changeset;
7*4882a593Smuzhiyun struct io_failure_record;
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /* Bits for the extent state */
10*4882a593Smuzhiyun #define EXTENT_DIRTY		(1U << 0)
11*4882a593Smuzhiyun #define EXTENT_UPTODATE		(1U << 1)
12*4882a593Smuzhiyun #define EXTENT_LOCKED		(1U << 2)
13*4882a593Smuzhiyun #define EXTENT_NEW		(1U << 3)
14*4882a593Smuzhiyun #define EXTENT_DELALLOC		(1U << 4)
15*4882a593Smuzhiyun #define EXTENT_DEFRAG		(1U << 5)
16*4882a593Smuzhiyun #define EXTENT_BOUNDARY		(1U << 6)
17*4882a593Smuzhiyun #define EXTENT_NODATASUM	(1U << 7)
18*4882a593Smuzhiyun #define EXTENT_CLEAR_META_RESV	(1U << 8)
19*4882a593Smuzhiyun #define EXTENT_NEED_WAIT	(1U << 9)
20*4882a593Smuzhiyun #define EXTENT_DAMAGED		(1U << 10)
21*4882a593Smuzhiyun #define EXTENT_NORESERVE	(1U << 11)
22*4882a593Smuzhiyun #define EXTENT_QGROUP_RESERVED	(1U << 12)
23*4882a593Smuzhiyun #define EXTENT_CLEAR_DATA_RESV	(1U << 13)
24*4882a593Smuzhiyun #define EXTENT_DELALLOC_NEW	(1U << 14)
25*4882a593Smuzhiyun #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
26*4882a593Smuzhiyun 				 EXTENT_CLEAR_DATA_RESV)
27*4882a593Smuzhiyun #define EXTENT_CTLBITS		(EXTENT_DO_ACCOUNTING)
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * Redefined bits above which are used only in the device allocation tree,
31*4882a593Smuzhiyun  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
32*4882a593Smuzhiyun  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
33*4882a593Smuzhiyun  * manipulation functions
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun #define CHUNK_ALLOCATED				EXTENT_DIRTY
36*4882a593Smuzhiyun #define CHUNK_TRIMMED				EXTENT_DEFRAG
37*4882a593Smuzhiyun #define CHUNK_STATE_MASK			(CHUNK_ALLOCATED |		\
38*4882a593Smuzhiyun 						 CHUNK_TRIMMED)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun enum {
41*4882a593Smuzhiyun 	IO_TREE_FS_PINNED_EXTENTS,
42*4882a593Smuzhiyun 	IO_TREE_FS_EXCLUDED_EXTENTS,
43*4882a593Smuzhiyun 	IO_TREE_BTREE_INODE_IO,
44*4882a593Smuzhiyun 	IO_TREE_INODE_IO,
45*4882a593Smuzhiyun 	IO_TREE_INODE_IO_FAILURE,
46*4882a593Smuzhiyun 	IO_TREE_RELOC_BLOCKS,
47*4882a593Smuzhiyun 	IO_TREE_TRANS_DIRTY_PAGES,
48*4882a593Smuzhiyun 	IO_TREE_ROOT_DIRTY_LOG_PAGES,
49*4882a593Smuzhiyun 	IO_TREE_INODE_FILE_EXTENT,
50*4882a593Smuzhiyun 	IO_TREE_LOG_CSUM_RANGE,
51*4882a593Smuzhiyun 	IO_TREE_SELFTEST,
52*4882a593Smuzhiyun 	IO_TREE_DEVICE_ALLOC_STATE,
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun struct extent_io_tree {
56*4882a593Smuzhiyun 	struct rb_root state;
57*4882a593Smuzhiyun 	struct btrfs_fs_info *fs_info;
58*4882a593Smuzhiyun 	void *private_data;
59*4882a593Smuzhiyun 	u64 dirty_bytes;
60*4882a593Smuzhiyun 	bool track_uptodate;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/* Who owns this io tree, should be one of IO_TREE_* */
63*4882a593Smuzhiyun 	u8 owner;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	spinlock_t lock;
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun struct extent_state {
69*4882a593Smuzhiyun 	u64 start;
70*4882a593Smuzhiyun 	u64 end; /* inclusive */
71*4882a593Smuzhiyun 	struct rb_node rb_node;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* ADD NEW ELEMENTS AFTER THIS */
74*4882a593Smuzhiyun 	wait_queue_head_t wq;
75*4882a593Smuzhiyun 	refcount_t refs;
76*4882a593Smuzhiyun 	unsigned state;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	struct io_failure_record *failrec;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun #ifdef CONFIG_BTRFS_DEBUG
81*4882a593Smuzhiyun 	struct list_head leak_list;
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun int __init extent_state_cache_init(void);
86*4882a593Smuzhiyun void __cold extent_state_cache_exit(void);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun void extent_io_tree_init(struct btrfs_fs_info *fs_info,
89*4882a593Smuzhiyun 			 struct extent_io_tree *tree, unsigned int owner,
90*4882a593Smuzhiyun 			 void *private_data);
91*4882a593Smuzhiyun void extent_io_tree_release(struct extent_io_tree *tree);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
94*4882a593Smuzhiyun 		     struct extent_state **cached);
95*4882a593Smuzhiyun 
lock_extent(struct extent_io_tree * tree,u64 start,u64 end)96*4882a593Smuzhiyun static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	return lock_extent_bits(tree, start, end, NULL);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun int __init extent_io_init(void);
104*4882a593Smuzhiyun void __cold extent_io_exit(void);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun u64 count_range_bits(struct extent_io_tree *tree,
107*4882a593Smuzhiyun 		     u64 *start, u64 search_end,
108*4882a593Smuzhiyun 		     u64 max_bytes, unsigned bits, int contig);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun void free_extent_state(struct extent_state *state);
111*4882a593Smuzhiyun int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
112*4882a593Smuzhiyun 		   unsigned bits, int filled,
113*4882a593Smuzhiyun 		   struct extent_state *cached_state);
114*4882a593Smuzhiyun int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
115*4882a593Smuzhiyun 		unsigned bits, struct extent_changeset *changeset);
116*4882a593Smuzhiyun int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
117*4882a593Smuzhiyun 		     unsigned bits, int wake, int delete,
118*4882a593Smuzhiyun 		     struct extent_state **cached);
119*4882a593Smuzhiyun int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
120*4882a593Smuzhiyun 		     unsigned bits, int wake, int delete,
121*4882a593Smuzhiyun 		     struct extent_state **cached, gfp_t mask,
122*4882a593Smuzhiyun 		     struct extent_changeset *changeset);
123*4882a593Smuzhiyun 
unlock_extent(struct extent_io_tree * tree,u64 start,u64 end)124*4882a593Smuzhiyun static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
unlock_extent_cached(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)129*4882a593Smuzhiyun static inline int unlock_extent_cached(struct extent_io_tree *tree, u64 start,
130*4882a593Smuzhiyun 		u64 end, struct extent_state **cached)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
133*4882a593Smuzhiyun 				GFP_NOFS, NULL);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
unlock_extent_cached_atomic(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)136*4882a593Smuzhiyun static inline int unlock_extent_cached_atomic(struct extent_io_tree *tree,
137*4882a593Smuzhiyun 		u64 start, u64 end, struct extent_state **cached)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
140*4882a593Smuzhiyun 				GFP_ATOMIC, NULL);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
clear_extent_bits(struct extent_io_tree * tree,u64 start,u64 end,unsigned bits)143*4882a593Smuzhiyun static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
144*4882a593Smuzhiyun 		u64 end, unsigned bits)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	int wake = 0;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	if (bits & EXTENT_LOCKED)
149*4882a593Smuzhiyun 		wake = 1;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	return clear_extent_bit(tree, start, end, bits, wake, 0, NULL);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
155*4882a593Smuzhiyun 			   unsigned bits, struct extent_changeset *changeset);
156*4882a593Smuzhiyun int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
157*4882a593Smuzhiyun 		   unsigned bits, u64 *failed_start,
158*4882a593Smuzhiyun 		   struct extent_state **cached_state, gfp_t mask);
159*4882a593Smuzhiyun int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
160*4882a593Smuzhiyun 			   unsigned bits);
161*4882a593Smuzhiyun 
set_extent_bits(struct extent_io_tree * tree,u64 start,u64 end,unsigned bits)162*4882a593Smuzhiyun static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
163*4882a593Smuzhiyun 		u64 end, unsigned bits)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	return set_extent_bit(tree, start, end, bits, NULL, NULL, GFP_NOFS);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
clear_extent_uptodate(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached_state)168*4882a593Smuzhiyun static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
169*4882a593Smuzhiyun 		u64 end, struct extent_state **cached_state)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
172*4882a593Smuzhiyun 				cached_state, GFP_NOFS, NULL);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
set_extent_dirty(struct extent_io_tree * tree,u64 start,u64 end,gfp_t mask)175*4882a593Smuzhiyun static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
176*4882a593Smuzhiyun 		u64 end, gfp_t mask)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
179*4882a593Smuzhiyun 			      NULL, mask);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
clear_extent_dirty(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached)182*4882a593Smuzhiyun static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
183*4882a593Smuzhiyun 				     u64 end, struct extent_state **cached)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	return clear_extent_bit(tree, start, end,
186*4882a593Smuzhiyun 				EXTENT_DIRTY | EXTENT_DELALLOC |
187*4882a593Smuzhiyun 				EXTENT_DO_ACCOUNTING, 0, 0, cached);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
191*4882a593Smuzhiyun 		       unsigned bits, unsigned clear_bits,
192*4882a593Smuzhiyun 		       struct extent_state **cached_state);
193*4882a593Smuzhiyun 
set_extent_delalloc(struct extent_io_tree * tree,u64 start,u64 end,unsigned int extra_bits,struct extent_state ** cached_state)194*4882a593Smuzhiyun static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
195*4882a593Smuzhiyun 				      u64 end, unsigned int extra_bits,
196*4882a593Smuzhiyun 				      struct extent_state **cached_state)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	return set_extent_bit(tree, start, end,
199*4882a593Smuzhiyun 			      EXTENT_DELALLOC | EXTENT_UPTODATE | extra_bits,
200*4882a593Smuzhiyun 			      NULL, cached_state, GFP_NOFS);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
set_extent_defrag(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached_state)203*4882a593Smuzhiyun static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
204*4882a593Smuzhiyun 		u64 end, struct extent_state **cached_state)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	return set_extent_bit(tree, start, end,
207*4882a593Smuzhiyun 			      EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
208*4882a593Smuzhiyun 			      NULL, cached_state, GFP_NOFS);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
set_extent_new(struct extent_io_tree * tree,u64 start,u64 end)211*4882a593Smuzhiyun static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
212*4882a593Smuzhiyun 		u64 end)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, NULL,
215*4882a593Smuzhiyun 			GFP_NOFS);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
set_extent_uptodate(struct extent_io_tree * tree,u64 start,u64 end,struct extent_state ** cached_state,gfp_t mask)218*4882a593Smuzhiyun static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start,
219*4882a593Smuzhiyun 		u64 end, struct extent_state **cached_state, gfp_t mask)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
222*4882a593Smuzhiyun 			      cached_state, mask);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
226*4882a593Smuzhiyun 			  u64 *start_ret, u64 *end_ret, unsigned bits,
227*4882a593Smuzhiyun 			  struct extent_state **cached_state);
228*4882a593Smuzhiyun void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
229*4882a593Smuzhiyun 				 u64 *start_ret, u64 *end_ret, unsigned bits);
230*4882a593Smuzhiyun int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
231*4882a593Smuzhiyun 			       u64 *start_ret, u64 *end_ret, unsigned bits);
232*4882a593Smuzhiyun int extent_invalidatepage(struct extent_io_tree *tree,
233*4882a593Smuzhiyun 			  struct page *page, unsigned long offset);
234*4882a593Smuzhiyun bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
235*4882a593Smuzhiyun 			       u64 *end, u64 max_bytes,
236*4882a593Smuzhiyun 			       struct extent_state **cached_state);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /* This should be reworked in the future and put elsewhere. */
239*4882a593Smuzhiyun struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start);
240*4882a593Smuzhiyun int set_state_failrec(struct extent_io_tree *tree, u64 start,
241*4882a593Smuzhiyun 		      struct io_failure_record *failrec);
242*4882a593Smuzhiyun void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,
243*4882a593Smuzhiyun 		u64 end);
244*4882a593Smuzhiyun int free_io_failure(struct extent_io_tree *failure_tree,
245*4882a593Smuzhiyun 		    struct extent_io_tree *io_tree,
246*4882a593Smuzhiyun 		    struct io_failure_record *rec);
247*4882a593Smuzhiyun int clean_io_failure(struct btrfs_fs_info *fs_info,
248*4882a593Smuzhiyun 		     struct extent_io_tree *failure_tree,
249*4882a593Smuzhiyun 		     struct extent_io_tree *io_tree, u64 start,
250*4882a593Smuzhiyun 		     struct page *page, u64 ino, unsigned int pg_offset);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun #endif /* BTRFS_EXTENT_IO_TREE_H */
253