Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / fs / btrfs / extent_io.h
CommitLineData
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>
602035d7
DS
10#include <linux/spinlock.h>
11#include <linux/atomic.h>
12#include <linux/rwsem.h>
13#include <linux/list.h>
14#include <linux/slab.h>
0ff40013 15#include "compression.h"
84cda1a6 16#include "messages.h"
ac467772 17#include "ulist.h"
fd8d2951 18#include "misc.h"
d1310b2e 19
602035d7
DS
20struct page;
21struct file;
22struct folio;
23struct inode;
24struct fiemap_extent_info;
25struct readahead_control;
26struct address_space;
27struct writeback_control;
28struct extent_io_tree;
29struct extent_map_tree;
6b0a63a4 30struct extent_state;
602035d7
DS
31struct btrfs_block_group;
32struct btrfs_fs_info;
33struct btrfs_inode;
34struct btrfs_root;
98c8d683 35struct btrfs_trans_handle;
602035d7 36struct btrfs_tree_parent_check;
98c8d683 37
80cb3836
DS
38enum {
39 EXTENT_BUFFER_UPTODATE,
40 EXTENT_BUFFER_DIRTY,
80cb3836
DS
41 EXTENT_BUFFER_TREE_REF,
42 EXTENT_BUFFER_STALE,
43 EXTENT_BUFFER_WRITEBACK,
80cb3836 44 EXTENT_BUFFER_UNMAPPED,
80cb3836
DS
45 /* write IO error */
46 EXTENT_BUFFER_WRITE_ERR,
cbf44cd9
JT
47 /* Indicate the extent buffer is written zeroed out (for zoned) */
48 EXTENT_BUFFER_ZONED_ZEROOUT,
d7172f52
CH
49 /* Indicate that extent buffer pages a being read */
50 EXTENT_BUFFER_READING,
80cb3836 51};
b4ce94de 52
da2c7009 53/* these are flags for __process_pages_contig */
fd8d2951
DS
54enum {
55 ENUM_BIT(PAGE_UNLOCK),
56 /* Page starts writeback, clear dirty bit and set writeback bit */
57 ENUM_BIT(PAGE_START_WRITEBACK),
58 ENUM_BIT(PAGE_END_WRITEBACK),
59 ENUM_BIT(PAGE_SET_ORDERED),
fd8d2951 60};
a791e35e 61
d1310b2e 62/*
cfbf07e2
QW
63 * Folio private values. Every page that is controlled by the extent map has
64 * folio private set to this value.
d1310b2e 65 */
cfbf07e2 66#define EXTENT_FOLIO_PRIVATE 1
d1310b2e 67
2fe1d551
OS
68/*
69 * The extent buffer bitmap operations are done with byte granularity instead of
70 * word granularity for two reasons:
71 * 1. The bitmaps must be little-endian on disk.
72 * 2. Bitmap items are not guaranteed to be aligned to a word and therefore a
73 * single word in a bitmap may straddle two pages in the extent buffer.
74 */
75#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
05a6ec86 76#define BYTE_MASK ((1U << BITS_PER_BYTE) - 1)
2fe1d551
OS
77#define BITMAP_FIRST_BYTE_MASK(start) \
78 ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
79#define BITMAP_LAST_BYTE_MASK(nbits) \
80 (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
81
a758781d 82
a62a3bd9
JB
83int __init extent_buffer_init_cachep(void);
84void __cold extent_buffer_free_cachep(void);
85
deb67895 86#define INLINE_EXTENT_BUFFER_PAGES (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE)
d1310b2e
CM
87struct extent_buffer {
88 u64 start;
84cda1a6
QW
89 u32 len;
90 u32 folio_size;
b4ce94de 91 unsigned long bflags;
f28491e0 92 struct btrfs_fs_info *fs_info;
397239ed
QW
93
94 /*
95 * The address where the eb can be accessed without any cross-page handling.
96 * This can be NULL if not possible.
97 */
98 void *addr;
99
3083ee2e 100 spinlock_t refs_lock;
727011e0 101 atomic_t refs;
5cf1ab56 102 int read_mirror;
656f30db 103 /* >= 0 if eb belongs to a log tree, -1 otherwise */
dc516164 104 s8 log_index;
84cda1a6 105 u8 folio_shift;
150cce2d 106 struct rcu_head rcu_head;
dc516164
DS
107
108 struct rw_semaphore lock;
bd681513 109
082d5bb9
QW
110 /*
111 * Pointers to all the folios of the extent buffer.
112 *
113 * For now the folio is always order 0 (aka, a single page).
114 */
115 struct folio *folios[INLINE_EXTENT_BUFFER_PAGES];
6d49ba1b
ES
116#ifdef CONFIG_BTRFS_DEBUG
117 struct list_head leak_list;
150cce2d 118 pid_t lock_owner;
6d49ba1b 119#endif
d1310b2e
CM
120};
121
861093ef
NA
122struct btrfs_eb_write_context {
123 struct writeback_control *wbc;
124 struct extent_buffer *eb;
7db94301
NA
125 /* Block group @eb resides in. Only used for zoned mode. */
126 struct btrfs_block_group *zoned_bg;
861093ef
NA
127};
128
84cda1a6
QW
129static inline unsigned long offset_in_eb_folio(const struct extent_buffer *eb,
130 u64 start)
131{
132 ASSERT(eb->folio_size);
133 return start & (eb->folio_size - 1);
134}
135
9b48adda
JB
136/*
137 * Get the correct offset inside the page of extent buffer.
138 *
139 * @eb: target extent buffer
140 * @start: offset inside the extent buffer
141 *
142 * Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.
143 */
8d993618
QW
144static inline size_t get_eb_offset_in_folio(const struct extent_buffer *eb,
145 unsigned long offset)
9b48adda
JB
146{
147 /*
8d993618
QW
148 * 1) sectorsize == PAGE_SIZE and nodesize >= PAGE_SIZE case
149 * 1.1) One large folio covering the whole eb
150 * The eb->start is aligned to folio size, thus adding it
151 * won't cause any difference.
152 * 1.2) Several page sized folios
153 * The eb->start is aligned to folio (page) size, thus
154 * adding it won't cause any difference.
9b48adda 155 *
8d993618
QW
156 * 2) sectorsize < PAGE_SIZE and nodesize < PAGE_SIZE case
157 * In this case there would only be one page sized folio, and there
158 * may be several different extent buffers in the page/folio.
159 * We need to add eb->start to properly access the offset inside
160 * that eb.
9b48adda 161 */
8d993618 162 return offset_in_folio(eb->folios[0], offset + eb->start);
9b48adda
JB
163}
164
8d993618
QW
165static inline unsigned long get_eb_folio_index(const struct extent_buffer *eb,
166 unsigned long offset)
9b48adda
JB
167{
168 /*
8d993618
QW
169 * 1) sectorsize == PAGE_SIZE and nodesize >= PAGE_SIZE case
170 * 1.1) One large folio covering the whole eb.
171 * the folio_shift would be large enough to always make us
172 * return 0 as index.
173 * 1.2) Several page sized folios
84cda1a6 174 * The folio_shift would be PAGE_SHIFT, giving us the correct
8d993618 175 * index.
9b48adda 176 *
8d993618
QW
177 * 2) sectorsize < PAGE_SIZE and nodesize < PAGE_SIZE case
178 * The folio would only be page sized, and always give us 0 as index.
9b48adda 179 */
84cda1a6 180 return offset >> eb->folio_shift;
9b48adda
JB
181}
182
ac467772
QW
183/*
184 * Structure to record how many bytes and which ranges are set/cleared
185 */
186struct extent_changeset {
187 /* How many bytes are set/cleared in this operation */
b642b52d 188 u64 bytes_changed;
ac467772
QW
189
190 /* Changed ranges */
53d32359 191 struct ulist range_changed;
ac467772
QW
192};
193
364ecf36
QW
194static inline void extent_changeset_init(struct extent_changeset *changeset)
195{
196 changeset->bytes_changed = 0;
197 ulist_init(&changeset->range_changed);
198}
199
200static inline struct extent_changeset *extent_changeset_alloc(void)
201{
202 struct extent_changeset *ret;
203
204 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
205 if (!ret)
206 return NULL;
207
208 extent_changeset_init(ret);
209 return ret;
210}
211
33336c18
BB
212static inline void extent_changeset_prealloc(struct extent_changeset *changeset, gfp_t gfp_mask)
213{
214 ulist_prealloc(&changeset->range_changed, gfp_mask);
215}
216
364ecf36
QW
217static inline void extent_changeset_release(struct extent_changeset *changeset)
218{
219 if (!changeset)
220 return;
221 changeset->bytes_changed = 0;
222 ulist_release(&changeset->range_changed);
223}
224
225static inline void extent_changeset_free(struct extent_changeset *changeset)
226{
227 if (!changeset)
228 return;
229 extent_changeset_release(changeset);
230 kfree(changeset);
231}
232
046c0d65 233bool try_release_extent_mapping(struct folio *folio, gfp_t mask);
b8ae2bfa 234int try_release_extent_buffer(struct folio *folio);
cd716d8f 235
fdaf9a58 236int btrfs_read_folio(struct file *file, struct folio *folio);
01e11841 237void extent_write_locked_range(struct inode *inode, const struct folio *locked_folio,
778b8785
CH
238 u64 start, u64 end, struct writeback_control *wbc,
239 bool pages_dirty);
c66f2afc 240int btrfs_writepages(struct address_space *mapping, struct writeback_control *wbc);
0b32f4bb
JB
241int btree_write_cache_pages(struct address_space *mapping,
242 struct writeback_control *wbc);
5e121ae6 243void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
7938d38b 244void btrfs_readahead(struct readahead_control *rac);
dfba9f47 245int set_folio_extent_mapped(struct folio *folio);
266a9361 246void clear_folio_extent_mapped(struct folio *folio);
d1310b2e 247
f28491e0 248struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
3fbaf258 249 u64 start, u64 owner_root, int level);
3f556f78 250struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
da17066c 251 u64 start);
2b48966a 252struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src);
f28491e0 253struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
452c75c3 254 u64 start);
d1310b2e 255void free_extent_buffer(struct extent_buffer *eb);
3083ee2e 256void free_extent_buffer_stale(struct extent_buffer *eb);
248c4ff3 257int read_extent_buffer_pages(struct extent_buffer *eb, int mirror_num,
2917f741 258 const struct btrfs_tree_parent_check *parent_check);
248c4ff3
DS
259int read_extent_buffer_pages_nowait(struct extent_buffer *eb, int mirror_num,
260 const struct btrfs_tree_parent_check *parent_check);
261
075adeeb
DS
262static inline void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
263{
264 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
265 TASK_UNINTERRUPTIBLE);
266}
267
bfb484d9 268void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
3fbaf258 269 u64 bytenr, u64 owner_root, u64 gen, int level);
bfb484d9 270void btrfs_readahead_node_child(struct extent_buffer *node, int slot);
479ed9ab 271
6e7c2838
DS
272/* Note: this can be used in for loops without caching the value in a variable. */
273static inline int __pure num_extent_pages(const struct extent_buffer *eb)
479ed9ab 274{
4a3dc938
QW
275 /*
276 * For sectorsize == PAGE_SIZE case, since nodesize is always aligned to
277 * sectorsize, it's just eb->len >> PAGE_SHIFT.
278 *
279 * For sectorsize < PAGE_SIZE case, we could have nodesize < PAGE_SIZE,
280 * thus have to ensure we get at least one page.
281 */
282 return (eb->len >> PAGE_SHIFT) ?: 1;
479ed9ab
RD
283}
284
13df3775
QW
285/*
286 * This can only be determined at runtime by checking eb::folios[0].
287 *
288 * As we can have either one large folio covering the whole eb
289 * (either nodesize <= PAGE_SIZE, or high order folio), or multiple
290 * single-paged folios.
6e7c2838
DS
291 *
292 * Note: this can be used in for loops without caching the value in a variable.
13df3775 293 */
6e7c2838 294static inline int __pure num_extent_folios(const struct extent_buffer *eb)
13df3775 295{
d6fe0c69
BB
296 if (!eb->folios[0])
297 return 0;
13df3775
QW
298 if (folio_order(eb->folios[0]))
299 return 1;
300 return num_extent_pages(eb);
301}
302
2b48966a 303static inline int extent_buffer_uptodate(const struct extent_buffer *eb)
ba020491
AJ
304{
305 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
306}
307
1cbb1f45
JM
308int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
309 unsigned long start, unsigned long len);
310void read_extent_buffer(const struct extent_buffer *eb, void *dst,
d1310b2e
CM
311 unsigned long start,
312 unsigned long len);
a48b73ec
JB
313int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
314 void __user *dst, unsigned long start,
315 unsigned long len);
2b48966a 316void write_extent_buffer(const struct extent_buffer *eb, const void *src,
d1310b2e 317 unsigned long start, unsigned long len);
730c374e
QW
318
319static inline void write_extent_buffer_chunk_tree_uuid(
320 const struct extent_buffer *eb, const void *chunk_tree_uuid)
321{
322 write_extent_buffer(eb, chunk_tree_uuid,
323 offsetof(struct btrfs_header, chunk_tree_uuid),
324 BTRFS_FSID_SIZE);
325}
326
327static inline void write_extent_buffer_fsid(const struct extent_buffer *eb,
328 const void *fsid)
329{
330 write_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
331 BTRFS_FSID_SIZE);
332}
333
2b48966a
DS
334void copy_extent_buffer_full(const struct extent_buffer *dst,
335 const struct extent_buffer *src);
336void copy_extent_buffer(const struct extent_buffer *dst,
337 const struct extent_buffer *src,
d1310b2e
CM
338 unsigned long dst_offset, unsigned long src_offset,
339 unsigned long len);
2b48966a
DS
340void memcpy_extent_buffer(const struct extent_buffer *dst,
341 unsigned long dst_offset, unsigned long src_offset,
342 unsigned long len);
343void memmove_extent_buffer(const struct extent_buffer *dst,
344 unsigned long dst_offset, unsigned long src_offset,
b159fa28 345 unsigned long len);
2b48966a 346void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
b159fa28 347 unsigned long len);
2b48966a 348int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
3e1e8bb7 349 unsigned long pos);
2b48966a 350void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
3e1e8bb7 351 unsigned long pos, unsigned long len);
2b48966a
DS
352void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
353 unsigned long start, unsigned long pos,
354 unsigned long len);
f18cc978 355void set_extent_buffer_dirty(struct extent_buffer *eb);
09c25a8c 356void set_extent_buffer_uptodate(struct extent_buffer *eb);
69ba3927 357void clear_extent_buffer_uptodate(struct extent_buffer *eb);
ad7ff17b 358void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
a67f5405 359 const struct folio *locked_folio,
6b0a63a4 360 struct extent_state **cached,
f97e27e9 361 u32 bits_to_clear, unsigned long page_ops);
d6f65c27
JB
362int extent_invalidate_folio(struct extent_io_tree *tree,
363 struct folio *folio, size_t offset);
98c8d683
JB
364void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
365 struct extent_buffer *buf);
dd137dd1 366
09e6cef1 367int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
0fbf6cbd 368 bool nofail);
fea91134 369int btrfs_alloc_folio_array(unsigned int nr_folios, struct folio **folio_array);
4a54c8c1 370
294e30fe 371#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9978059b 372bool find_lock_delalloc_range(struct inode *inode,
c987f1e6 373 struct folio *locked_folio, u64 *start,
ce9f967f 374 u64 *end);
0d4cf4e6 375#endif
faa2dbf0 376struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
da17066c 377 u64 start);
3fd63727
JB
378
379#ifdef CONFIG_BTRFS_DEBUG
380void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info);
381#else
382#define btrfs_extent_buffer_leak_debug_check(fs_info) do {} while (0)
383#endif
384
294e30fe 385#endif