blob: 87f60a48f7500a023621cf959a4152a3f945b1eb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
David Sterba9888c342018-04-03 19:16:55 +02002
3#ifndef BTRFS_EXTENT_IO_H
4#define BTRFS_EXTENT_IO_H
Chris Masond1310b22008-01-24 16:13:08 -05005
6#include <linux/rbtree.h>
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02007#include <linux/refcount.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +02008#include <linux/fiemap.h>
Qu Wenruoac467772015-10-12 12:08:16 +08009#include "ulist.h"
Chris Masond1310b22008-01-24 16:13:08 -050010
Li Zefan261507a02010-12-17 14:21:50 +080011/*
12 * flags for bio submission. The high bits indicate the compression
13 * type for this bio
14 */
Chris Masonc8b97812008-10-29 14:49:59 -040015#define EXTENT_BIO_COMPRESSED 1
Li Zefan261507a02010-12-17 14:21:50 +080016#define EXTENT_BIO_FLAG_SHIFT 16
Chris Masonc8b97812008-10-29 14:49:59 -040017
David Sterba80cb3832018-11-27 15:03:20 +010018enum {
19 EXTENT_BUFFER_UPTODATE,
20 EXTENT_BUFFER_DIRTY,
21 EXTENT_BUFFER_CORRUPT,
22 /* this got triggered by readahead */
23 EXTENT_BUFFER_READAHEAD,
24 EXTENT_BUFFER_TREE_REF,
25 EXTENT_BUFFER_STALE,
26 EXTENT_BUFFER_WRITEBACK,
27 /* read IO error */
28 EXTENT_BUFFER_READ_ERR,
29 EXTENT_BUFFER_UNMAPPED,
30 EXTENT_BUFFER_IN_TREE,
31 /* write IO error */
32 EXTENT_BUFFER_WRITE_ERR,
33};
Chris Masonb4ce94d2009-02-04 09:25:08 -050034
Liu Boda2c7002017-02-10 16:41:05 +010035/* these are flags for __process_pages_contig */
Josef Bacikc2790a22013-07-29 11:20:47 -040036#define PAGE_UNLOCK (1 << 0)
37#define PAGE_CLEAR_DIRTY (1 << 1)
38#define PAGE_SET_WRITEBACK (1 << 2)
39#define PAGE_END_WRITEBACK (1 << 3)
40#define PAGE_SET_PRIVATE2 (1 << 4)
Filipe Manana704de492014-10-06 22:14:22 +010041#define PAGE_SET_ERROR (1 << 5)
Liu Boda2c7002017-02-10 16:41:05 +010042#define PAGE_LOCK (1 << 6)
Chris Masona791e352009-10-08 11:27:10 -040043
Chris Masond1310b22008-01-24 16:13:08 -050044/*
45 * page->private values. Every page that is controlled by the extent
46 * map has page->private set to one.
47 */
48#define EXTENT_PAGE_PRIVATE 1
Chris Masond1310b22008-01-24 16:13:08 -050049
Omar Sandoval2fe1d552016-09-22 17:24:20 -070050/*
51 * The extent buffer bitmap operations are done with byte granularity instead of
52 * word granularity for two reasons:
53 * 1. The bitmaps must be little-endian on disk.
54 * 2. Bitmap items are not guaranteed to be aligned to a word and therefore a
55 * single word in a bitmap may straddle two pages in the extent buffer.
56 */
57#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
58#define BYTE_MASK ((1 << BITS_PER_BYTE) - 1)
59#define BITMAP_FIRST_BYTE_MASK(start) \
60 ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
61#define BITMAP_LAST_BYTE_MASK(nbits) \
62 (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
63
Josef Bacikea466792012-03-26 21:57:36 -040064struct btrfs_root;
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +020065struct btrfs_inode;
Miao Xiefacc8a222013-07-25 19:22:34 +080066struct btrfs_io_bio;
David Sterba47dc1962016-02-11 13:24:13 +010067struct io_failure_record;
Josef Bacik9c7d3a52019-09-23 10:05:19 -040068struct extent_io_tree;
David Sterbaa7587812017-06-23 03:05:23 +020069
Omar Sandoval77d5d682020-04-16 14:46:25 -070070typedef blk_status_t (submit_bio_hook_t)(struct inode *inode, struct bio *bio,
71 int mirror_num,
72 unsigned long bio_flags);
73
David Sterbaa7587812017-06-23 03:05:23 +020074typedef blk_status_t (extent_submit_bio_start_t)(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +010075 struct bio *bio, u64 bio_offset);
David Sterbaa7587812017-06-23 03:05:23 +020076
Chris Masond1310b22008-01-24 16:13:08 -050077struct extent_io_ops {
David Sterba4d53ddd2017-02-17 15:27:44 +010078 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +010079 * The following callbacks must be always defined, the function
David Sterba4d53ddd2017-02-17 15:27:44 +010080 * pointer will be called unconditionally.
81 */
Omar Sandoval77d5d682020-04-16 14:46:25 -070082 submit_bio_hook_t *submit_bio_hook;
Miao Xiefacc8a222013-07-25 19:22:34 +080083 int (*readpage_end_io_hook)(struct btrfs_io_bio *io_bio, u64 phy_offset,
84 struct page *page, u64 start, u64 end,
85 int mirror);
Chris Masond1310b22008-01-24 16:13:08 -050086};
87
Chris Masond1310b22008-01-24 16:13:08 -050088
Chris Mason727011e2010-08-06 13:21:20 -040089#define INLINE_EXTENT_BUFFER_PAGES 16
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030090#define MAX_INLINE_EXTENT_BUFFER_SIZE (INLINE_EXTENT_BUFFER_PAGES * PAGE_SIZE)
Chris Masond1310b22008-01-24 16:13:08 -050091struct extent_buffer {
92 u64 start;
93 unsigned long len;
Chris Masonb4ce94d2009-02-04 09:25:08 -050094 unsigned long bflags;
Josef Bacikf28491e2013-12-16 13:24:27 -050095 struct btrfs_fs_info *fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -050096 spinlock_t refs_lock;
Chris Mason727011e2010-08-06 13:21:20 -040097 atomic_t refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -040098 atomic_t io_pages;
Josef Bacik5cf1ab52012-04-16 09:42:26 -040099 int read_mirror;
Miao Xie19fe0a82010-10-26 20:57:29 -0400100 struct rcu_head rcu_head;
Arne Jansen5b25f702011-09-13 10:55:48 +0200101 pid_t lock_owner;
Chris Masonb4ce94d2009-02-04 09:25:08 -0500102
David Sterba06297d82019-05-02 16:47:23 +0200103 int blocking_writers;
Chris Masonbd681512011-07-16 15:23:14 -0400104 atomic_t blocking_readers;
David Sterbaed1b4ed2018-08-24 16:31:17 +0200105 bool lock_nested;
Filipe Manana656f30d2014-09-26 12:25:56 +0100106 /* >= 0 if eb belongs to a log tree, -1 otherwise */
107 short log_index;
Chris Masonb4ce94d2009-02-04 09:25:08 -0500108
Chris Masonbd681512011-07-16 15:23:14 -0400109 /* protects write locks */
110 rwlock_t lock;
111
112 /* readers use lock_wq while they wait for the write
113 * lock holders to unlock
Chris Masonb4ce94d2009-02-04 09:25:08 -0500114 */
Chris Masonbd681512011-07-16 15:23:14 -0400115 wait_queue_head_t write_lock_wq;
116
117 /* writers use read_lock_wq while they wait for readers
118 * to unlock
119 */
120 wait_queue_head_t read_lock_wq;
David Sterbab8dae312013-02-28 14:54:18 +0000121 struct page *pages[INLINE_EXTENT_BUFFER_PAGES];
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000122#ifdef CONFIG_BTRFS_DEBUG
David Sterbaf3dc24c2019-05-02 16:51:53 +0200123 int spinning_writers;
David Sterbaafd495a2018-08-24 15:57:38 +0200124 atomic_t spinning_readers;
David Sterba5c9c7992018-08-24 16:15:51 +0200125 atomic_t read_locks;
David Sterba00801ae2019-05-02 16:53:47 +0200126 int write_locks;
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000127 struct list_head leak_list;
128#endif
Chris Masond1310b22008-01-24 16:13:08 -0500129};
130
Qu Wenruoac467772015-10-12 12:08:16 +0800131/*
132 * Structure to record how many bytes and which ranges are set/cleared
133 */
134struct extent_changeset {
135 /* How many bytes are set/cleared in this operation */
Qu Wenruo7bc329c2017-02-27 15:10:36 +0800136 unsigned int bytes_changed;
Qu Wenruoac467772015-10-12 12:08:16 +0800137
138 /* Changed ranges */
David Sterba53d32352017-02-13 13:42:29 +0100139 struct ulist range_changed;
Qu Wenruoac467772015-10-12 12:08:16 +0800140};
141
Qu Wenruo364ecf32017-02-27 15:10:38 +0800142static inline void extent_changeset_init(struct extent_changeset *changeset)
143{
144 changeset->bytes_changed = 0;
145 ulist_init(&changeset->range_changed);
146}
147
148static inline struct extent_changeset *extent_changeset_alloc(void)
149{
150 struct extent_changeset *ret;
151
152 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
153 if (!ret)
154 return NULL;
155
156 extent_changeset_init(ret);
157 return ret;
158}
159
160static inline void extent_changeset_release(struct extent_changeset *changeset)
161{
162 if (!changeset)
163 return;
164 changeset->bytes_changed = 0;
165 ulist_release(&changeset->range_changed);
166}
167
168static inline void extent_changeset_free(struct extent_changeset *changeset)
169{
170 if (!changeset)
171 return;
172 extent_changeset_release(changeset);
173 kfree(changeset);
174}
175
Li Zefan261507a02010-12-17 14:21:50 +0800176static inline void extent_set_compress_type(unsigned long *bio_flags,
177 int compress_type)
178{
179 *bio_flags |= compress_type << EXTENT_BIO_FLAG_SHIFT;
180}
181
182static inline int extent_compress_type(unsigned long bio_flags)
183{
184 return bio_flags >> EXTENT_BIO_FLAG_SHIFT;
185}
186
Chris Masond1310b22008-01-24 16:13:08 -0500187struct extent_map_tree;
188
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +0200189typedef struct extent_map *(get_extent_t)(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -0800190 struct page *page, size_t pg_offset,
191 u64 start, u64 len);
Chris Masond1310b22008-01-24 16:13:08 -0500192
Nikolay Borisov477a30b2018-04-19 10:46:34 +0300193int try_release_extent_mapping(struct page *page, gfp_t mask);
David Sterbaf7a52a42013-04-26 14:56:29 +0000194int try_release_extent_buffer(struct page *page);
David Sterbacd716d82015-12-03 14:41:30 +0100195
David Sterba71ad38b2020-02-05 19:09:35 +0100196int extent_read_full_page(struct page *page, get_extent_t *get_extent,
197 int mirror_num);
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +0200198int extent_write_full_page(struct page *page, struct writeback_control *wbc);
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200199int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -0500200 int mode);
Nikolay Borisov8ae225a2018-04-19 10:46:38 +0300201int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -0500202 struct writeback_control *wbc);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400203int btree_write_cache_pages(struct address_space *mapping,
204 struct writeback_control *wbc);
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -0700205void extent_readahead(struct readahead_control *rac);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -0500206int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +0200207 __u64 start, __u64 len);
Chris Masond1310b22008-01-24 16:13:08 -0500208void set_page_extent_mapped(struct page *page);
209
Josef Bacikf28491e2013-12-16 13:24:27 -0500210struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +0200211 u64 start);
Omar Sandoval0f331222015-09-29 20:50:31 -0700212struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
213 u64 start, unsigned long len);
David Sterba3f556f72014-06-15 03:20:26 +0200214struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -0400215 u64 start);
David Sterba2b489662020-04-29 03:04:10 +0200216struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src);
Josef Bacikf28491e2013-12-16 13:24:27 -0500217struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
Chandra Seetharaman452c75c2013-10-07 10:45:25 -0500218 u64 start);
Chris Masond1310b22008-01-24 16:13:08 -0500219void free_extent_buffer(struct extent_buffer *eb);
Josef Bacik3083ee22012-03-09 16:01:49 -0500220void free_extent_buffer_stale(struct extent_buffer *eb);
Arne Jansenbb82ab82011-06-10 14:06:53 +0200221#define WAIT_NONE 0
222#define WAIT_COMPLETE 1
223#define WAIT_PAGE_LOCK 2
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +0300224int read_extent_buffer_pages(struct extent_buffer *eb, int wait,
David Sterba6af49db2017-06-23 04:09:57 +0200225 int mirror_num);
Josef Bacikfd8b2b62013-04-24 16:41:19 -0400226void wait_on_extent_buffer_writeback(struct extent_buffer *eb);
Robin Dong479ed9a2012-09-29 02:07:47 -0600227
David Sterbacc5e31a2018-03-01 18:20:27 +0100228static inline int num_extent_pages(const struct extent_buffer *eb)
Robin Dong479ed9a2012-09-29 02:07:47 -0600229{
David Sterba8791d432018-07-04 17:49:31 +0200230 return (round_up(eb->start + eb->len, PAGE_SIZE) >> PAGE_SHIFT) -
231 (eb->start >> PAGE_SHIFT);
Robin Dong479ed9a2012-09-29 02:07:47 -0600232}
233
David Sterba2b489662020-04-29 03:04:10 +0200234static inline int extent_buffer_uptodate(const struct extent_buffer *eb)
Anand Jainba020492018-02-13 12:35:44 +0800235{
236 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
237}
238
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600239int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
240 unsigned long start, unsigned long len);
241void read_extent_buffer(const struct extent_buffer *eb, void *dst,
Chris Masond1310b22008-01-24 16:13:08 -0500242 unsigned long start,
243 unsigned long len);
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600244int read_extent_buffer_to_user(const struct extent_buffer *eb,
245 void __user *dst, unsigned long start,
Gerhard Heift550ac1d2014-01-30 16:24:01 +0100246 unsigned long len);
David Sterba2b489662020-04-29 03:04:10 +0200247void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *src);
248void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb,
David Sterbaf157bf72016-11-09 17:43:38 +0100249 const void *src);
David Sterba2b489662020-04-29 03:04:10 +0200250void write_extent_buffer(const struct extent_buffer *eb, const void *src,
Chris Masond1310b22008-01-24 16:13:08 -0500251 unsigned long start, unsigned long len);
David Sterba2b489662020-04-29 03:04:10 +0200252void copy_extent_buffer_full(const struct extent_buffer *dst,
253 const struct extent_buffer *src);
254void copy_extent_buffer(const struct extent_buffer *dst,
255 const struct extent_buffer *src,
Chris Masond1310b22008-01-24 16:13:08 -0500256 unsigned long dst_offset, unsigned long src_offset,
257 unsigned long len);
David Sterba2b489662020-04-29 03:04:10 +0200258void memcpy_extent_buffer(const struct extent_buffer *dst,
259 unsigned long dst_offset, unsigned long src_offset,
260 unsigned long len);
261void memmove_extent_buffer(const struct extent_buffer *dst,
262 unsigned long dst_offset, unsigned long src_offset,
David Sterbab159fa22016-11-08 18:09:03 +0100263 unsigned long len);
David Sterba2b489662020-04-29 03:04:10 +0200264void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
265 unsigned long len);
266int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -0700267 unsigned long pos);
David Sterba2b489662020-04-29 03:04:10 +0200268void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -0700269 unsigned long pos, unsigned long len);
David Sterba2b489662020-04-29 03:04:10 +0200270void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
271 unsigned long start, unsigned long pos,
272 unsigned long len);
273void clear_extent_buffer_dirty(const struct extent_buffer *eb);
Liu Boabb57ef2018-09-14 01:44:42 +0800274bool set_extent_buffer_dirty(struct extent_buffer *eb);
David Sterba09c25a82015-12-03 13:08:59 +0100275void set_extent_buffer_uptodate(struct extent_buffer *eb);
David Sterba69ba3922015-12-03 13:08:59 +0100276void clear_extent_buffer_uptodate(struct extent_buffer *eb);
David Sterba2b489662020-04-29 03:04:10 +0200277int extent_buffer_under_io(const struct extent_buffer *eb);
David Sterbabd1fa4f2015-12-03 13:08:59 +0100278void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
David Sterbaf6311572015-12-03 13:08:59 +0100279void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
David Sterbaa9d93e12015-12-03 13:08:59 +0100280void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300281 struct page *locked_page,
282 unsigned bits_to_clear,
283 unsigned long page_ops);
David Sterbae749af442019-06-18 20:00:16 +0200284struct bio *btrfs_bio_alloc(u64 first_byte);
David Sterbac5e4c3d2017-06-12 17:29:41 +0200285struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs);
David Sterba8b6c1d52017-06-02 17:48:13 +0200286struct bio *btrfs_bio_clone(struct bio *bio);
Liu Boe4770942017-05-16 10:57:14 -0700287struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +0200288
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100289struct btrfs_fs_info;
Nikolay Borisov9d4f7f82017-02-20 13:50:55 +0200290struct btrfs_inode;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +0200291
Josef Bacik6ec656b2017-05-05 11:57:14 -0400292int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
293 u64 length, u64 logical, struct page *page,
294 unsigned int pg_offset, int mirror_num);
David Sterbab5227c02015-12-03 13:08:59 +0100295void end_extent_writepage(struct page *page, int err, u64 start, u64 end);
David Sterba2b489662020-04-29 03:04:10 +0200296int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num);
Miao Xie2fe63032014-09-12 18:43:59 +0800297
298/*
299 * When IO fails, either with EIO or csum verification fails, we
300 * try other mirrors that might have a good copy of the data. This
301 * io_failure_record is used to record state as we go through all the
302 * mirrors. If another mirror has good data, the page is set up to date
303 * and things continue. If a good mirror can't be found, the original
304 * bio end_io callback is called to indicate things have failed.
305 */
306struct io_failure_record {
307 struct page *page;
308 u64 start;
309 u64 len;
310 u64 logical;
311 unsigned long bio_flags;
312 int this_mirror;
313 int failed_mirror;
314 int in_validation;
315};
316
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +0200317
Omar Sandoval77d5d682020-04-16 14:46:25 -0700318blk_status_t btrfs_submit_read_repair(struct inode *inode,
319 struct bio *failed_bio, u64 phy_offset,
320 struct page *page, unsigned int pgoff,
321 u64 start, u64 end, int failed_mirror,
322 submit_bio_hook_t *submit_bio_hook);
323
Josef Bacik294e30f2013-10-09 12:00:56 -0400324#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Goldwyn Rodrigues99780592019-06-21 10:02:54 -0500325bool find_lock_delalloc_range(struct inode *inode,
Johannes Thumshirnce9f9672018-11-19 10:38:17 +0100326 struct page *locked_page, u64 *start,
327 u64 *end);
Chris Mason0d4cf4e2014-10-07 13:24:20 -0700328#endif
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400329struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -0400330 u64 start);
Josef Bacik3fd63722020-02-14 16:11:40 -0500331
332#ifdef CONFIG_BTRFS_DEBUG
333void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info);
334#else
335#define btrfs_extent_buffer_leak_debug_check(fs_info) do {} while (0)
336#endif
337
Josef Bacik294e30f2013-10-09 12:00:56 -0400338#endif