Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
9888c340 DS |
2 | |
3 | #ifndef BTRFS_EXTENT_IO_H | |
4 | #define BTRFS_EXTENT_IO_H | |
d1310b2e CM |
5 | |
6 | #include <linux/rbtree.h> | |
b7ac31b7 | 7 | #include <linux/refcount.h> |
10c5db28 | 8 | #include <linux/fiemap.h> |
deb67895 | 9 | #include <linux/btrfs_tree.h> |
0ff40013 | 10 | #include "compression.h" |
ac467772 | 11 | #include "ulist.h" |
fd8d2951 | 12 | #include "misc.h" |
d1310b2e | 13 | |
80cb3836 DS |
14 | enum { |
15 | EXTENT_BUFFER_UPTODATE, | |
16 | EXTENT_BUFFER_DIRTY, | |
17 | EXTENT_BUFFER_CORRUPT, | |
18 | /* this got triggered by readahead */ | |
19 | EXTENT_BUFFER_READAHEAD, | |
20 | EXTENT_BUFFER_TREE_REF, | |
21 | EXTENT_BUFFER_STALE, | |
22 | EXTENT_BUFFER_WRITEBACK, | |
23 | /* read IO error */ | |
24 | EXTENT_BUFFER_READ_ERR, | |
25 | EXTENT_BUFFER_UNMAPPED, | |
26 | EXTENT_BUFFER_IN_TREE, | |
27 | /* write IO error */ | |
28 | EXTENT_BUFFER_WRITE_ERR, | |
d3575156 | 29 | EXTENT_BUFFER_NO_CHECK, |
80cb3836 | 30 | }; |
b4ce94de | 31 | |
da2c7009 | 32 | /* these are flags for __process_pages_contig */ |
fd8d2951 DS |
33 | enum { |
34 | ENUM_BIT(PAGE_UNLOCK), | |
35 | /* Page starts writeback, clear dirty bit and set writeback bit */ | |
36 | ENUM_BIT(PAGE_START_WRITEBACK), | |
37 | ENUM_BIT(PAGE_END_WRITEBACK), | |
38 | ENUM_BIT(PAGE_SET_ORDERED), | |
39 | ENUM_BIT(PAGE_SET_ERROR), | |
40 | ENUM_BIT(PAGE_LOCK), | |
41 | }; | |
a791e35e | 42 | |
d1310b2e CM |
43 | /* |
44 | * page->private values. Every page that is controlled by the extent | |
45 | * map has page->private set to one. | |
46 | */ | |
47 | #define EXTENT_PAGE_PRIVATE 1 | |
d1310b2e | 48 | |
2fe1d551 OS |
49 | /* |
50 | * The extent buffer bitmap operations are done with byte granularity instead of | |
51 | * word granularity for two reasons: | |
52 | * 1. The bitmaps must be little-endian on disk. | |
53 | * 2. Bitmap items are not guaranteed to be aligned to a word and therefore a | |
54 | * single word in a bitmap may straddle two pages in the extent buffer. | |
55 | */ | |
56 | #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE) | |
57 | #define BYTE_MASK ((1 << BITS_PER_BYTE) - 1) | |
58 | #define BITMAP_FIRST_BYTE_MASK(start) \ | |
59 | ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK) | |
60 | #define BITMAP_LAST_BYTE_MASK(nbits) \ | |
61 | (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1))) | |
62 | ||
7aa51232 | 63 | struct btrfs_bio; |
ea466794 | 64 | struct btrfs_root; |
6fc0ef68 | 65 | struct btrfs_inode; |
183ebab7 | 66 | struct btrfs_fs_info; |
47dc196a | 67 | struct io_failure_record; |
9c7d3a54 | 68 | struct extent_io_tree; |
a758781d | 69 | |
a62a3bd9 JB |
70 | int __init extent_buffer_init_cachep(void); |
71 | void __cold extent_buffer_free_cachep(void); | |
72 | ||
deb67895 | 73 | #define INLINE_EXTENT_BUFFER_PAGES (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE) |
d1310b2e CM |
74 | struct extent_buffer { |
75 | u64 start; | |
76 | unsigned long len; | |
b4ce94de | 77 | unsigned long bflags; |
f28491e0 | 78 | struct btrfs_fs_info *fs_info; |
3083ee2e | 79 | spinlock_t refs_lock; |
727011e0 | 80 | atomic_t refs; |
0b32f4bb | 81 | atomic_t io_pages; |
5cf1ab56 | 82 | int read_mirror; |
19fe0a8b | 83 | struct rcu_head rcu_head; |
5b25f70f | 84 | pid_t lock_owner; |
656f30db | 85 | /* >= 0 if eb belongs to a log tree, -1 otherwise */ |
dc516164 DS |
86 | s8 log_index; |
87 | ||
88 | struct rw_semaphore lock; | |
bd681513 | 89 | |
b8dae313 | 90 | struct page *pages[INLINE_EXTENT_BUFFER_PAGES]; |
d3575156 | 91 | struct list_head release_list; |
6d49ba1b ES |
92 | #ifdef CONFIG_BTRFS_DEBUG |
93 | struct list_head leak_list; | |
94 | #endif | |
d1310b2e CM |
95 | }; |
96 | ||
ac467772 QW |
97 | /* |
98 | * Structure to record how many bytes and which ranges are set/cleared | |
99 | */ | |
100 | struct extent_changeset { | |
101 | /* How many bytes are set/cleared in this operation */ | |
b642b52d | 102 | u64 bytes_changed; |
ac467772 QW |
103 | |
104 | /* Changed ranges */ | |
53d32359 | 105 | struct ulist range_changed; |
ac467772 QW |
106 | }; |
107 | ||
364ecf36 QW |
108 | static inline void extent_changeset_init(struct extent_changeset *changeset) |
109 | { | |
110 | changeset->bytes_changed = 0; | |
111 | ulist_init(&changeset->range_changed); | |
112 | } | |
113 | ||
114 | static inline struct extent_changeset *extent_changeset_alloc(void) | |
115 | { | |
116 | struct extent_changeset *ret; | |
117 | ||
118 | ret = kmalloc(sizeof(*ret), GFP_KERNEL); | |
119 | if (!ret) | |
120 | return NULL; | |
121 | ||
122 | extent_changeset_init(ret); | |
123 | return ret; | |
124 | } | |
125 | ||
126 | static inline void extent_changeset_release(struct extent_changeset *changeset) | |
127 | { | |
128 | if (!changeset) | |
129 | return; | |
130 | changeset->bytes_changed = 0; | |
131 | ulist_release(&changeset->range_changed); | |
132 | } | |
133 | ||
134 | static inline void extent_changeset_free(struct extent_changeset *changeset) | |
135 | { | |
136 | if (!changeset) | |
137 | return; | |
138 | extent_changeset_release(changeset); | |
139 | kfree(changeset); | |
140 | } | |
141 | ||
d1310b2e CM |
142 | struct extent_map_tree; |
143 | ||
477a30ba | 144 | int try_release_extent_mapping(struct page *page, gfp_t mask); |
f7a52a40 | 145 | int try_release_extent_buffer(struct page *page); |
cd716d8f | 146 | |
fdaf9a58 | 147 | int btrfs_read_folio(struct file *file, struct folio *folio); |
2bd0fc93 | 148 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end); |
8ae225a8 | 149 | int extent_writepages(struct address_space *mapping, |
d1310b2e | 150 | struct writeback_control *wbc); |
0b32f4bb JB |
151 | int btree_write_cache_pages(struct address_space *mapping, |
152 | struct writeback_control *wbc); | |
ba206a02 | 153 | void extent_readahead(struct readahead_control *rac); |
facee0a0 | 154 | int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, |
bab16e21 | 155 | u64 start, u64 len); |
32443de3 QW |
156 | int set_page_extent_mapped(struct page *page); |
157 | void clear_page_extent_mapped(struct page *page); | |
d1310b2e | 158 | |
f28491e0 | 159 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
3fbaf258 | 160 | u64 start, u64 owner_root, int level); |
0f331229 OS |
161 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
162 | u64 start, unsigned long len); | |
3f556f78 | 163 | struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
da17066c | 164 | u64 start); |
2b48966a | 165 | struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src); |
f28491e0 | 166 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
452c75c3 | 167 | u64 start); |
d1310b2e | 168 | void free_extent_buffer(struct extent_buffer *eb); |
3083ee2e | 169 | void free_extent_buffer_stale(struct extent_buffer *eb); |
bb82ab88 AJ |
170 | #define WAIT_NONE 0 |
171 | #define WAIT_COMPLETE 1 | |
172 | #define WAIT_PAGE_LOCK 2 | |
c2ccfbc6 | 173 | int read_extent_buffer_pages(struct extent_buffer *eb, int wait, |
6af49dbd | 174 | int mirror_num); |
fd8b2b61 | 175 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb); |
bfb484d9 | 176 | void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, |
3fbaf258 | 177 | u64 bytenr, u64 owner_root, u64 gen, int level); |
bfb484d9 | 178 | void btrfs_readahead_node_child(struct extent_buffer *node, int slot); |
479ed9ab | 179 | |
cc5e31a4 | 180 | static inline int num_extent_pages(const struct extent_buffer *eb) |
479ed9ab | 181 | { |
4a3dc938 QW |
182 | /* |
183 | * For sectorsize == PAGE_SIZE case, since nodesize is always aligned to | |
184 | * sectorsize, it's just eb->len >> PAGE_SHIFT. | |
185 | * | |
186 | * For sectorsize < PAGE_SIZE case, we could have nodesize < PAGE_SIZE, | |
187 | * thus have to ensure we get at least one page. | |
188 | */ | |
189 | return (eb->len >> PAGE_SHIFT) ?: 1; | |
479ed9ab RD |
190 | } |
191 | ||
2b48966a | 192 | static inline int extent_buffer_uptodate(const struct extent_buffer *eb) |
ba020491 AJ |
193 | { |
194 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); | |
195 | } | |
196 | ||
1cbb1f45 JM |
197 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
198 | unsigned long start, unsigned long len); | |
199 | void read_extent_buffer(const struct extent_buffer *eb, void *dst, | |
d1310b2e CM |
200 | unsigned long start, |
201 | unsigned long len); | |
a48b73ec JB |
202 | int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, |
203 | void __user *dst, unsigned long start, | |
204 | unsigned long len); | |
2b48966a DS |
205 | void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *src); |
206 | void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb, | |
f157bf76 | 207 | const void *src); |
2b48966a | 208 | void write_extent_buffer(const struct extent_buffer *eb, const void *src, |
d1310b2e | 209 | unsigned long start, unsigned long len); |
2b48966a DS |
210 | void copy_extent_buffer_full(const struct extent_buffer *dst, |
211 | const struct extent_buffer *src); | |
212 | void copy_extent_buffer(const struct extent_buffer *dst, | |
213 | const struct extent_buffer *src, | |
d1310b2e CM |
214 | unsigned long dst_offset, unsigned long src_offset, |
215 | unsigned long len); | |
2b48966a DS |
216 | void memcpy_extent_buffer(const struct extent_buffer *dst, |
217 | unsigned long dst_offset, unsigned long src_offset, | |
218 | unsigned long len); | |
219 | void memmove_extent_buffer(const struct extent_buffer *dst, | |
220 | unsigned long dst_offset, unsigned long src_offset, | |
b159fa28 | 221 | unsigned long len); |
2b48966a | 222 | void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, |
b159fa28 | 223 | unsigned long len); |
2b48966a | 224 | int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, |
3e1e8bb7 | 225 | unsigned long pos); |
2b48966a | 226 | void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, |
3e1e8bb7 | 227 | unsigned long pos, unsigned long len); |
2b48966a DS |
228 | void extent_buffer_bitmap_clear(const struct extent_buffer *eb, |
229 | unsigned long start, unsigned long pos, | |
230 | unsigned long len); | |
231 | void clear_extent_buffer_dirty(const struct extent_buffer *eb); | |
abb57ef3 | 232 | bool set_extent_buffer_dirty(struct extent_buffer *eb); |
09c25a8c | 233 | void set_extent_buffer_uptodate(struct extent_buffer *eb); |
69ba3927 | 234 | void clear_extent_buffer_uptodate(struct extent_buffer *eb); |
2b48966a | 235 | int extent_buffer_under_io(const struct extent_buffer *eb); |
bd1fa4f0 | 236 | void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end); |
f6311572 | 237 | void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end); |
ad7ff17b | 238 | void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
74e9194a | 239 | struct page *locked_page, |
f97e27e9 | 240 | u32 bits_to_clear, unsigned long page_ops); |
d6f65c27 JB |
241 | int extent_invalidate_folio(struct extent_io_tree *tree, |
242 | struct folio *folio, size_t offset); | |
dd137dd1 STD |
243 | |
244 | int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array); | |
4a54c8c1 | 245 | |
b5227c07 | 246 | void end_extent_writepage(struct page *page, int err, u64 start, u64 end); |
2b48966a | 247 | int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num); |
2fe6303e MX |
248 | |
249 | /* | |
250 | * When IO fails, either with EIO or csum verification fails, we | |
251 | * try other mirrors that might have a good copy of the data. This | |
252 | * io_failure_record is used to record state as we go through all the | |
1245835d | 253 | * mirrors. If another mirror has good data, the sector is set up to date |
2fe6303e MX |
254 | * and things continue. If a good mirror can't be found, the original |
255 | * bio end_io callback is called to indicate things have failed. | |
256 | */ | |
257 | struct io_failure_record { | |
87c11705 JB |
258 | /* Use rb_simple_node for search/insert */ |
259 | struct { | |
260 | struct rb_node rb_node; | |
261 | u64 bytenr; | |
262 | }; | |
2fe6303e | 263 | struct page *page; |
2fe6303e MX |
264 | u64 len; |
265 | u64 logical; | |
2fe6303e MX |
266 | int this_mirror; |
267 | int failed_mirror; | |
c144c63f | 268 | int num_copies; |
2fe6303e MX |
269 | }; |
270 | ||
d8f9268e | 271 | int btrfs_repair_one_sector(struct btrfs_inode *inode, struct btrfs_bio *failed_bbio, |
7aa51232 | 272 | u32 bio_offset, struct page *page, unsigned int pgoff, |
19af6a7d | 273 | bool submit_buffered); |
d6f65c27 JB |
274 | void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end); |
275 | int btrfs_clean_io_failure(struct btrfs_inode *inode, u64 start, | |
276 | struct page *page, unsigned int pg_offset); | |
77d5d689 | 277 | |
294e30fe | 278 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9978059b | 279 | bool find_lock_delalloc_range(struct inode *inode, |
ce9f967f JT |
280 | struct page *locked_page, u64 *start, |
281 | u64 *end); | |
0d4cf4e6 | 282 | #endif |
faa2dbf0 | 283 | struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, |
da17066c | 284 | u64 start); |
3fd63727 JB |
285 | |
286 | #ifdef CONFIG_BTRFS_DEBUG | |
287 | void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info); | |
288 | #else | |
289 | #define btrfs_extent_buffer_leak_debug_check(fs_info) do {} while (0) | |
290 | #endif | |
291 | ||
294e30fe | 292 | #endif |