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