• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __EXTENTIO__
2 #define __EXTENTIO__
3 
4 #include <linux/rbtree.h>
5 
6 /* bits for the extent state */
7 #define EXTENT_DIRTY 1
8 #define EXTENT_WRITEBACK (1 << 1)
9 #define EXTENT_UPTODATE (1 << 2)
10 #define EXTENT_LOCKED (1 << 3)
11 #define EXTENT_NEW (1 << 4)
12 #define EXTENT_DELALLOC (1 << 5)
13 #define EXTENT_DEFRAG (1 << 6)
14 #define EXTENT_BOUNDARY (1 << 9)
15 #define EXTENT_NODATASUM (1 << 10)
16 #define EXTENT_DO_ACCOUNTING (1 << 11)
17 #define EXTENT_FIRST_DELALLOC (1 << 12)
18 #define EXTENT_NEED_WAIT (1 << 13)
19 #define EXTENT_DAMAGED (1 << 14)
20 #define EXTENT_NORESERVE (1 << 15)
21 #define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
22 #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | EXTENT_FIRST_DELALLOC)
23 
24 /*
25  * flags for bio submission. The high bits indicate the compression
26  * type for this bio
27  */
28 #define EXTENT_BIO_COMPRESSED 1
29 #define EXTENT_BIO_TREE_LOG 2
30 #define EXTENT_BIO_PARENT_LOCKED 4
31 #define EXTENT_BIO_FLAG_SHIFT 16
32 
33 /* these are bit numbers for test/set bit */
34 #define EXTENT_BUFFER_UPTODATE 0
35 #define EXTENT_BUFFER_DIRTY 2
36 #define EXTENT_BUFFER_CORRUPT 3
37 #define EXTENT_BUFFER_READAHEAD 4	/* this got triggered by readahead */
38 #define EXTENT_BUFFER_TREE_REF 5
39 #define EXTENT_BUFFER_STALE 6
40 #define EXTENT_BUFFER_WRITEBACK 7
41 #define EXTENT_BUFFER_READ_ERR 8        /* read IO error */
42 #define EXTENT_BUFFER_DUMMY 9
43 #define EXTENT_BUFFER_IN_TREE 10
44 #define EXTENT_BUFFER_WRITE_ERR 11    /* write IO error */
45 
46 /* these are flags for extent_clear_unlock_delalloc */
47 #define PAGE_UNLOCK		(1 << 0)
48 #define PAGE_CLEAR_DIRTY	(1 << 1)
49 #define PAGE_SET_WRITEBACK	(1 << 2)
50 #define PAGE_END_WRITEBACK	(1 << 3)
51 #define PAGE_SET_PRIVATE2	(1 << 4)
52 
53 /*
54  * page->private values.  Every page that is controlled by the extent
55  * map has page->private set to one.
56  */
57 #define EXTENT_PAGE_PRIVATE 1
58 
59 struct extent_state;
60 struct btrfs_root;
61 struct btrfs_io_bio;
62 
63 typedef	int (extent_submit_bio_hook_t)(struct inode *inode, int rw,
64 				       struct bio *bio, int mirror_num,
65 				       unsigned long bio_flags, u64 bio_offset);
66 struct extent_io_ops {
67 	int (*fill_delalloc)(struct inode *inode, struct page *locked_page,
68 			     u64 start, u64 end, int *page_started,
69 			     unsigned long *nr_written);
70 	int (*writepage_start_hook)(struct page *page, u64 start, u64 end);
71 	int (*writepage_io_hook)(struct page *page, u64 start, u64 end);
72 	extent_submit_bio_hook_t *submit_bio_hook;
73 	int (*merge_bio_hook)(int rw, struct page *page, unsigned long offset,
74 			      size_t size, struct bio *bio,
75 			      unsigned long bio_flags);
76 	int (*readpage_io_failed_hook)(struct page *page, int failed_mirror);
77 	int (*readpage_end_io_hook)(struct btrfs_io_bio *io_bio, u64 phy_offset,
78 				    struct page *page, u64 start, u64 end,
79 				    int mirror);
80 	int (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
81 				      struct extent_state *state, int uptodate);
82 	void (*set_bit_hook)(struct inode *inode, struct extent_state *state,
83 			     unsigned long *bits);
84 	void (*clear_bit_hook)(struct inode *inode, struct extent_state *state,
85 			       unsigned long *bits);
86 	void (*merge_extent_hook)(struct inode *inode,
87 				  struct extent_state *new,
88 				  struct extent_state *other);
89 	void (*split_extent_hook)(struct inode *inode,
90 				  struct extent_state *orig, u64 split);
91 };
92 
93 struct extent_io_tree {
94 	struct rb_root state;
95 	struct address_space *mapping;
96 	u64 dirty_bytes;
97 	int track_uptodate;
98 	spinlock_t lock;
99 	struct extent_io_ops *ops;
100 };
101 
102 struct extent_state {
103 	u64 start;
104 	u64 end; /* inclusive */
105 	struct rb_node rb_node;
106 
107 	/* ADD NEW ELEMENTS AFTER THIS */
108 	wait_queue_head_t wq;
109 	atomic_t refs;
110 	unsigned long state;
111 
112 	/* for use by the FS */
113 	u64 private;
114 
115 #ifdef CONFIG_BTRFS_DEBUG
116 	struct list_head leak_list;
117 #endif
118 };
119 
120 #define INLINE_EXTENT_BUFFER_PAGES 16
121 #define MAX_INLINE_EXTENT_BUFFER_SIZE (INLINE_EXTENT_BUFFER_PAGES * PAGE_CACHE_SIZE)
122 struct extent_buffer {
123 	u64 start;
124 	unsigned long len;
125 	unsigned long bflags;
126 	struct btrfs_fs_info *fs_info;
127 	spinlock_t refs_lock;
128 	atomic_t refs;
129 	atomic_t io_pages;
130 	int read_mirror;
131 	struct rcu_head rcu_head;
132 	pid_t lock_owner;
133 
134 	/* count of read lock holders on the extent buffer */
135 	atomic_t write_locks;
136 	atomic_t read_locks;
137 	atomic_t blocking_writers;
138 	atomic_t blocking_readers;
139 	atomic_t spinning_readers;
140 	atomic_t spinning_writers;
141 	short lock_nested;
142 	/* >= 0 if eb belongs to a log tree, -1 otherwise */
143 	short log_index;
144 
145 	/* protects write locks */
146 	rwlock_t lock;
147 
148 	/* readers use lock_wq while they wait for the write
149 	 * lock holders to unlock
150 	 */
151 	wait_queue_head_t write_lock_wq;
152 
153 	/* writers use read_lock_wq while they wait for readers
154 	 * to unlock
155 	 */
156 	wait_queue_head_t read_lock_wq;
157 	struct page *pages[INLINE_EXTENT_BUFFER_PAGES];
158 #ifdef CONFIG_BTRFS_DEBUG
159 	struct list_head leak_list;
160 #endif
161 };
162 
extent_set_compress_type(unsigned long * bio_flags,int compress_type)163 static inline void extent_set_compress_type(unsigned long *bio_flags,
164 					    int compress_type)
165 {
166 	*bio_flags |= compress_type << EXTENT_BIO_FLAG_SHIFT;
167 }
168 
extent_compress_type(unsigned long bio_flags)169 static inline int extent_compress_type(unsigned long bio_flags)
170 {
171 	return bio_flags >> EXTENT_BIO_FLAG_SHIFT;
172 }
173 
174 struct extent_map_tree;
175 
176 typedef struct extent_map *(get_extent_t)(struct inode *inode,
177 					  struct page *page,
178 					  size_t pg_offset,
179 					  u64 start, u64 len,
180 					  int create);
181 
182 void extent_io_tree_init(struct extent_io_tree *tree,
183 			 struct address_space *mapping);
184 int try_release_extent_mapping(struct extent_map_tree *map,
185 			       struct extent_io_tree *tree, struct page *page,
186 			       gfp_t mask);
187 int try_release_extent_buffer(struct page *page);
188 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
189 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
190 		     unsigned long bits, struct extent_state **cached);
191 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end);
192 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
193 			 struct extent_state **cached, gfp_t mask);
194 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
195 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
196 			  get_extent_t *get_extent, int mirror_num);
197 int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
198 				 get_extent_t *get_extent, int mirror_num);
199 int __init extent_io_init(void);
200 void extent_io_exit(void);
201 
202 u64 count_range_bits(struct extent_io_tree *tree,
203 		     u64 *start, u64 search_end,
204 		     u64 max_bytes, unsigned long bits, int contig);
205 
206 void free_extent_state(struct extent_state *state);
207 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
208 		   unsigned long bits, int filled,
209 		   struct extent_state *cached_state);
210 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
211 		      unsigned long bits, gfp_t mask);
212 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
213 		     unsigned long bits, int wake, int delete,
214 		     struct extent_state **cached, gfp_t mask);
215 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
216 		    unsigned long bits, gfp_t mask);
217 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
218 		   unsigned long bits, u64 *failed_start,
219 		   struct extent_state **cached_state, gfp_t mask);
220 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
221 			struct extent_state **cached_state, gfp_t mask);
222 int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
223 			  struct extent_state **cached_state, gfp_t mask);
224 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
225 		   gfp_t mask);
226 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
227 		     gfp_t mask);
228 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
229 		       gfp_t mask);
230 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
231 		       unsigned long bits, unsigned long clear_bits,
232 		       struct extent_state **cached_state, gfp_t mask);
233 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
234 			struct extent_state **cached_state, gfp_t mask);
235 int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
236 		      struct extent_state **cached_state, gfp_t mask);
237 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
238 			  u64 *start_ret, u64 *end_ret, unsigned long bits,
239 			  struct extent_state **cached_state);
240 int extent_invalidatepage(struct extent_io_tree *tree,
241 			  struct page *page, unsigned long offset);
242 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
243 			  get_extent_t *get_extent,
244 			  struct writeback_control *wbc);
245 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
246 			      u64 start, u64 end, get_extent_t *get_extent,
247 			      int mode);
248 int extent_writepages(struct extent_io_tree *tree,
249 		      struct address_space *mapping,
250 		      get_extent_t *get_extent,
251 		      struct writeback_control *wbc);
252 int btree_write_cache_pages(struct address_space *mapping,
253 			    struct writeback_control *wbc);
254 int extent_readpages(struct extent_io_tree *tree,
255 		     struct address_space *mapping,
256 		     struct list_head *pages, unsigned nr_pages,
257 		     get_extent_t get_extent);
258 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
259 		__u64 start, __u64 len, get_extent_t *get_extent);
260 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private);
261 void set_page_extent_mapped(struct page *page);
262 
263 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
264 					  u64 start, unsigned long len);
265 struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len);
266 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src);
267 struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
268 					 u64 start);
269 void free_extent_buffer(struct extent_buffer *eb);
270 void free_extent_buffer_stale(struct extent_buffer *eb);
271 #define WAIT_NONE	0
272 #define WAIT_COMPLETE	1
273 #define WAIT_PAGE_LOCK	2
274 int read_extent_buffer_pages(struct extent_io_tree *tree,
275 			     struct extent_buffer *eb, u64 start, int wait,
276 			     get_extent_t *get_extent, int mirror_num);
277 void wait_on_extent_buffer_writeback(struct extent_buffer *eb);
278 
num_extent_pages(u64 start,u64 len)279 static inline unsigned long num_extent_pages(u64 start, u64 len)
280 {
281 	return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
282 		(start >> PAGE_CACHE_SHIFT);
283 }
284 
extent_buffer_get(struct extent_buffer * eb)285 static inline void extent_buffer_get(struct extent_buffer *eb)
286 {
287 	atomic_inc(&eb->refs);
288 }
289 
290 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
291 			  unsigned long start,
292 			  unsigned long len);
293 void read_extent_buffer(struct extent_buffer *eb, void *dst,
294 			unsigned long start,
295 			unsigned long len);
296 int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dst,
297 			       unsigned long start,
298 			       unsigned long len);
299 void write_extent_buffer(struct extent_buffer *eb, const void *src,
300 			 unsigned long start, unsigned long len);
301 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
302 			unsigned long dst_offset, unsigned long src_offset,
303 			unsigned long len);
304 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
305 			   unsigned long src_offset, unsigned long len);
306 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
307 			   unsigned long src_offset, unsigned long len);
308 void memset_extent_buffer(struct extent_buffer *eb, char c,
309 			  unsigned long start, unsigned long len);
310 void clear_extent_buffer_dirty(struct extent_buffer *eb);
311 int set_extent_buffer_dirty(struct extent_buffer *eb);
312 int set_extent_buffer_uptodate(struct extent_buffer *eb);
313 int clear_extent_buffer_uptodate(struct extent_buffer *eb);
314 int extent_buffer_uptodate(struct extent_buffer *eb);
315 int extent_buffer_under_io(struct extent_buffer *eb);
316 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
317 		      unsigned long min_len, char **map,
318 		      unsigned long *map_start,
319 		      unsigned long *map_len);
320 int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
321 int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
322 int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
323 				 struct page *locked_page,
324 				 unsigned long bits_to_clear,
325 				 unsigned long page_ops);
326 struct bio *
327 btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
328 		gfp_t gfp_flags);
329 struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs);
330 struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask);
331 
332 struct btrfs_fs_info;
333 
334 int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
335 		      struct page *page, unsigned int pg_offset,
336 		      int mirror_num);
337 int clean_io_failure(struct inode *inode, u64 start, struct page *page,
338 		     unsigned int pg_offset);
339 int end_extent_writepage(struct page *page, int err, u64 start, u64 end);
340 int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
341 			 int mirror_num);
342 
343 /*
344  * When IO fails, either with EIO or csum verification fails, we
345  * try other mirrors that might have a good copy of the data.  This
346  * io_failure_record is used to record state as we go through all the
347  * mirrors.  If another mirror has good data, the page is set up to date
348  * and things continue.  If a good mirror can't be found, the original
349  * bio end_io callback is called to indicate things have failed.
350  */
351 struct io_failure_record {
352 	struct page *page;
353 	u64 start;
354 	u64 len;
355 	u64 logical;
356 	unsigned long bio_flags;
357 	int this_mirror;
358 	int failed_mirror;
359 	int in_validation;
360 };
361 
362 void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end);
363 int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
364 				struct io_failure_record **failrec_ret);
365 int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
366 			   struct io_failure_record *failrec, int fail_mirror);
367 struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
368 				    struct io_failure_record *failrec,
369 				    struct page *page, int pg_offset, int icsum,
370 				    bio_end_io_t *endio_func, void *data);
371 int free_io_failure(struct inode *inode, struct io_failure_record *rec);
372 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
373 noinline u64 find_lock_delalloc_range(struct inode *inode,
374 				      struct extent_io_tree *tree,
375 				      struct page *locked_page, u64 *start,
376 				      u64 *end, u64 max_bytes);
377 #endif
378 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
379 					       u64 start, unsigned long len);
380 #endif
381