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