blob: d61b526dc8561ebc7a8174f4894b82d4e09ec073 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Sterbac1d7c512018-04-03 19:23:33 +02002
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/bitops.h>
4#include <linux/slab.h>
5#include <linux/bio.h>
6#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/pagemap.h>
8#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05009#include <linux/spinlock.h>
10#include <linux/blkdev.h>
11#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050012#include <linux/writeback.h>
13#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070014#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060015#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050016#include "extent_io.h"
17#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080024#include "backref.h"
David Sterba6af49db2017-06-23 04:09:57 +020025#include "disk-io.h"
Chris Masond1310b22008-01-24 16:13:08 -050026
Chris Masond1310b22008-01-24 16:13:08 -050027static struct kmem_cache *extent_state_cache;
28static struct kmem_cache *extent_buffer_cache;
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -040029static struct bio_set btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050030
Filipe Manana27a35072014-07-06 20:09:59 +010031static inline bool extent_state_in_tree(const struct extent_state *state)
32{
33 return !RB_EMPTY_NODE(&state->rb_node);
34}
35
Eric Sandeen6d49ba12013-04-22 16:12:31 +000036#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050037static LIST_HEAD(buffers);
38static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040039
Chris Masond3977122009-01-05 21:25:51 -050040static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000041
42static inline
43void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
44{
45 unsigned long flags;
46
47 spin_lock_irqsave(&leak_lock, flags);
48 list_add(new, head);
49 spin_unlock_irqrestore(&leak_lock, flags);
50}
51
52static inline
53void btrfs_leak_debug_del(struct list_head *entry)
54{
55 unsigned long flags;
56
57 spin_lock_irqsave(&leak_lock, flags);
58 list_del(entry);
59 spin_unlock_irqrestore(&leak_lock, flags);
60}
61
62static inline
63void btrfs_leak_debug_check(void)
64{
65 struct extent_state *state;
66 struct extent_buffer *eb;
67
68 while (!list_empty(&states)) {
69 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010070 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010071 state->start, state->end, state->state,
72 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020073 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000074 list_del(&state->leak_list);
75 kmem_cache_free(extent_state_cache, state);
76 }
77
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Liu Boaf2679e2018-01-25 11:02:48 -070080 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
81 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000082 list_del(&eb->leak_list);
83 kmem_cache_free(extent_buffer_cache, eb);
84 }
85}
David Sterba8d599ae2013-04-30 15:22:23 +000086
Josef Bacika5dee372013-12-13 10:02:44 -050087#define btrfs_debug_check_extent_io_range(tree, start, end) \
88 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000089static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050090 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000091{
Nikolay Borisov65a680f2018-11-01 14:09:49 +020092 struct inode *inode = tree->private_data;
93 u64 isize;
94
95 if (!inode || !is_data_inode(inode))
96 return;
97
98 isize = i_size_read(inode);
99 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
100 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
101 "%s: ino %llu isize %llu odd range [%llu,%llu]",
102 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
103 }
David Sterba8d599ae2013-04-30 15:22:23 +0000104}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000105#else
106#define btrfs_leak_debug_add(new, head) do {} while (0)
107#define btrfs_leak_debug_del(entry) do {} while (0)
108#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000109#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400110#endif
Chris Masond1310b22008-01-24 16:13:08 -0500111
Chris Masond1310b22008-01-24 16:13:08 -0500112#define BUFFER_LRU_MAX 64
113
114struct tree_entry {
115 u64 start;
116 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500117 struct rb_node rb_node;
118};
119
120struct extent_page_data {
121 struct bio *bio;
122 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500123 /* tells writepage not to lock the state bits for this range
124 * it still does the unlocking
125 */
Chris Masonffbd5172009-04-20 15:50:09 -0400126 unsigned int extent_locked:1;
127
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600128 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400129 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500130};
131
David Sterba57599c72018-03-01 17:56:34 +0100132static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800133 struct extent_changeset *changeset,
134 int set)
135{
136 int ret;
137
138 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100139 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800140 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100141 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800142 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100143 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800144 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100145 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800146 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100147 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800148}
149
Qu Wenruobb58eb92019-01-25 13:09:15 +0800150static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
151 unsigned long bio_flags)
152{
153 blk_status_t ret = 0;
154 struct bio_vec *bvec = bio_last_bvec_all(bio);
Linus Torvalds80201fe2019-03-08 14:12:17 -0800155 struct bio_vec bv;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800156 struct extent_io_tree *tree = bio->bi_private;
157 u64 start;
158
Linus Torvalds80201fe2019-03-08 14:12:17 -0800159 mp_bvec_last_segment(bvec, &bv);
160 start = page_offset(bv.bv_page) + bv.bv_offset;
Qu Wenruobb58eb92019-01-25 13:09:15 +0800161
162 bio->bi_private = NULL;
163
164 if (tree->ops)
165 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
166 mirror_num, bio_flags, start);
167 else
168 btrfsic_submit_bio(bio);
169
170 return blk_status_to_errno(ret);
171}
172
173static void flush_write_bio(struct extent_page_data *epd)
174{
175 if (epd->bio) {
176 int ret;
177
178 ret = submit_one_bio(epd->bio, 0, 0);
179 BUG_ON(ret < 0); /* -ENOMEM */
180 epd->bio = NULL;
181 }
182}
David Sterbae2932ee2017-06-23 04:16:17 +0200183
Chris Masond1310b22008-01-24 16:13:08 -0500184int __init extent_io_init(void)
185{
David Sterba837e1972012-09-07 03:00:48 -0600186 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200187 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300188 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500189 if (!extent_state_cache)
190 return -ENOMEM;
191
David Sterba837e1972012-09-07 03:00:48 -0600192 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200193 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300194 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500195 if (!extent_buffer_cache)
196 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400197
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400198 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
199 offsetof(struct btrfs_io_bio, bio),
200 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400201 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700202
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400203 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700204 goto free_bioset;
205
Chris Masond1310b22008-01-24 16:13:08 -0500206 return 0;
207
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700208free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400209 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700210
Chris Mason9be33952013-05-17 18:30:14 -0400211free_buffer_cache:
212 kmem_cache_destroy(extent_buffer_cache);
213 extent_buffer_cache = NULL;
214
Chris Masond1310b22008-01-24 16:13:08 -0500215free_state_cache:
216 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400217 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500218 return -ENOMEM;
219}
220
David Sterbae67c7182018-02-19 17:24:18 +0100221void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500222{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000223 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000224
225 /*
226 * Make sure all delayed rcu free are flushed before we
227 * destroy caches.
228 */
229 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800230 kmem_cache_destroy(extent_state_cache);
231 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400232 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500233}
234
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800235void extent_io_tree_init(struct btrfs_fs_info *fs_info,
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800236 struct extent_io_tree *tree, unsigned int owner,
237 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500238{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800239 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000240 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500241 tree->ops = NULL;
242 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500243 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400244 tree->private_data = private_data;
Qu Wenruo43eb5f22019-03-01 10:47:59 +0800245 tree->owner = owner;
Chris Masond1310b22008-01-24 16:13:08 -0500246}
Chris Masond1310b22008-01-24 16:13:08 -0500247
Christoph Hellwigb2950862008-12-02 09:54:17 -0500248static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500249{
250 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500251
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100252 /*
253 * The given mask might be not appropriate for the slab allocator,
254 * drop the unsupported bits
255 */
256 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500257 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400258 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500259 return state;
260 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100261 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100262 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000263 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200264 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500265 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100266 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500267 return state;
268}
Chris Masond1310b22008-01-24 16:13:08 -0500269
Chris Mason4845e442010-05-25 20:56:50 -0400270void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500271{
Chris Masond1310b22008-01-24 16:13:08 -0500272 if (!state)
273 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200274 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100275 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000276 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100277 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500278 kmem_cache_free(extent_state_cache, state);
279 }
280}
Chris Masond1310b22008-01-24 16:13:08 -0500281
Filipe Mananaf2071b22014-02-12 15:05:53 +0000282static struct rb_node *tree_insert(struct rb_root *root,
283 struct rb_node *search_start,
284 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000285 struct rb_node *node,
286 struct rb_node ***p_in,
287 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500288{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000289 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500290 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500291 struct tree_entry *entry;
292
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000293 if (p_in && parent_in) {
294 p = *p_in;
295 parent = *parent_in;
296 goto do_insert;
297 }
298
Filipe Mananaf2071b22014-02-12 15:05:53 +0000299 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500300 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500301 parent = *p;
302 entry = rb_entry(parent, struct tree_entry, rb_node);
303
304 if (offset < entry->start)
305 p = &(*p)->rb_left;
306 else if (offset > entry->end)
307 p = &(*p)->rb_right;
308 else
309 return parent;
310 }
311
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000312do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500313 rb_link_node(node, parent, p);
314 rb_insert_color(node, root);
315 return NULL;
316}
317
Chris Mason80ea96b2008-02-01 14:51:59 -0500318static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000319 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200320 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000321 struct rb_node ***p_ret,
322 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500323{
Chris Mason80ea96b2008-02-01 14:51:59 -0500324 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000325 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500326 struct rb_node *prev = NULL;
327 struct rb_node *orig_prev = NULL;
328 struct tree_entry *entry;
329 struct tree_entry *prev_entry = NULL;
330
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000331 while (*n) {
332 prev = *n;
333 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500334 prev_entry = entry;
335
336 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000337 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500338 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000339 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500340 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000341 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500342 }
343
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000344 if (p_ret)
345 *p_ret = n;
346 if (parent_ret)
347 *parent_ret = prev;
348
Nikolay Borisov352646c2019-01-30 16:51:00 +0200349 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500350 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500351 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500352 prev = rb_next(prev);
353 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
354 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200355 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500356 prev = orig_prev;
357 }
358
Nikolay Borisov352646c2019-01-30 16:51:00 +0200359 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500360 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500361 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500362 prev = rb_prev(prev);
363 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
364 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200365 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500366 }
367 return NULL;
368}
369
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000370static inline struct rb_node *
371tree_search_for_insert(struct extent_io_tree *tree,
372 u64 offset,
373 struct rb_node ***p_ret,
374 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500375{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200376 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500377 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500378
Nikolay Borisov352646c2019-01-30 16:51:00 +0200379 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500380 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200381 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500382 return ret;
383}
384
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000385static inline struct rb_node *tree_search(struct extent_io_tree *tree,
386 u64 offset)
387{
388 return tree_search_for_insert(tree, offset, NULL, NULL);
389}
390
Chris Masond1310b22008-01-24 16:13:08 -0500391/*
392 * utility function to look for merge candidates inside a given range.
393 * Any extents with matching state are merged together into a single
394 * extent in the tree. Extents with EXTENT_IO in their state field
395 * are not merged because the end_io handlers need to be able to do
396 * operations on them without sleeping (or doing allocations/splits).
397 *
398 * This should be called with the tree lock held.
399 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000400static void merge_state(struct extent_io_tree *tree,
401 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500402{
403 struct extent_state *other;
404 struct rb_node *other_node;
405
Nikolay Borisov88826792019-03-14 15:28:31 +0200406 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000407 return;
Chris Masond1310b22008-01-24 16:13:08 -0500408
409 other_node = rb_prev(&state->rb_node);
410 if (other_node) {
411 other = rb_entry(other_node, struct extent_state, rb_node);
412 if (other->end == state->start - 1 &&
413 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200414 if (tree->private_data &&
415 is_data_inode(tree->private_data))
416 btrfs_merge_delalloc_extent(tree->private_data,
417 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500418 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500419 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100420 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500421 free_extent_state(other);
422 }
423 }
424 other_node = rb_next(&state->rb_node);
425 if (other_node) {
426 other = rb_entry(other_node, struct extent_state, rb_node);
427 if (other->start == state->end + 1 &&
428 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200429 if (tree->private_data &&
430 is_data_inode(tree->private_data))
431 btrfs_merge_delalloc_extent(tree->private_data,
432 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400433 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400434 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100435 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400436 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500437 }
438 }
Chris Masond1310b22008-01-24 16:13:08 -0500439}
440
Xiao Guangrong3150b692011-07-14 03:19:08 +0000441static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800442 struct extent_state *state, unsigned *bits,
443 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000444
Chris Masond1310b22008-01-24 16:13:08 -0500445/*
446 * insert an extent_state struct into the tree. 'bits' are set on the
447 * struct before it is inserted.
448 *
449 * This may return -EEXIST if the extent is already there, in which case the
450 * state struct is freed.
451 *
452 * The tree lock is not taken internally. This is a utility function and
453 * probably isn't what you want to call (see set/clear_extent_bit).
454 */
455static int insert_state(struct extent_io_tree *tree,
456 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000457 struct rb_node ***p,
458 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800459 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500460{
461 struct rb_node *node;
462
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000463 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500464 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200465 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500466 state->start = start;
467 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400468
Qu Wenruod38ed272015-10-12 14:53:37 +0800469 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000470
Filipe Mananaf2071b22014-02-12 15:05:53 +0000471 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500472 if (node) {
473 struct extent_state *found;
474 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400475 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200476 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500477 return -EEXIST;
478 }
479 merge_state(tree, state);
480 return 0;
481}
482
483/*
484 * split a given extent state struct in two, inserting the preallocated
485 * struct 'prealloc' as the newly created second half. 'split' indicates an
486 * offset inside 'orig' where it should be split.
487 *
488 * Before calling,
489 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
490 * are two extent state structs in the tree:
491 * prealloc: [orig->start, split - 1]
492 * orig: [ split, orig->end ]
493 *
494 * The tree locks are not taken by this function. They need to be held
495 * by the caller.
496 */
497static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
498 struct extent_state *prealloc, u64 split)
499{
500 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400501
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200502 if (tree->private_data && is_data_inode(tree->private_data))
503 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400504
Chris Masond1310b22008-01-24 16:13:08 -0500505 prealloc->start = orig->start;
506 prealloc->end = split - 1;
507 prealloc->state = orig->state;
508 orig->start = split;
509
Filipe Mananaf2071b22014-02-12 15:05:53 +0000510 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
511 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500512 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500513 free_extent_state(prealloc);
514 return -EEXIST;
515 }
516 return 0;
517}
518
Li Zefancdc6a392012-03-12 16:39:48 +0800519static struct extent_state *next_state(struct extent_state *state)
520{
521 struct rb_node *next = rb_next(&state->rb_node);
522 if (next)
523 return rb_entry(next, struct extent_state, rb_node);
524 else
525 return NULL;
526}
527
Chris Masond1310b22008-01-24 16:13:08 -0500528/*
529 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100530 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500531 *
532 * If no bits are set on the state struct after clearing things, the
533 * struct is freed and removed from the tree
534 */
Li Zefancdc6a392012-03-12 16:39:48 +0800535static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
536 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800537 unsigned *bits, int wake,
538 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500539{
Li Zefancdc6a392012-03-12 16:39:48 +0800540 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100541 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100542 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500543
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400544 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500545 u64 range = state->end - state->start + 1;
546 WARN_ON(range > tree->dirty_bytes);
547 tree->dirty_bytes -= range;
548 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200549
550 if (tree->private_data && is_data_inode(tree->private_data))
551 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
552
David Sterba57599c72018-03-01 17:56:34 +0100553 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
554 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400555 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500556 if (wake)
557 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400558 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800559 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100560 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500561 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100562 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500563 free_extent_state(state);
564 } else {
565 WARN_ON(1);
566 }
567 } else {
568 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800569 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500570 }
Li Zefancdc6a392012-03-12 16:39:48 +0800571 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500572}
573
Xiao Guangrong82337672011-04-20 06:44:57 +0000574static struct extent_state *
575alloc_extent_state_atomic(struct extent_state *prealloc)
576{
577 if (!prealloc)
578 prealloc = alloc_extent_state(GFP_ATOMIC);
579
580 return prealloc;
581}
582
Eric Sandeen48a3b632013-04-25 20:41:01 +0000583static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400584{
David Sterba05912a32018-07-18 19:23:45 +0200585 struct inode *inode = tree->private_data;
586
587 btrfs_panic(btrfs_sb(inode->i_sb), err,
588 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400589}
590
Chris Masond1310b22008-01-24 16:13:08 -0500591/*
592 * clear some bits on a range in the tree. This may require splitting
593 * or inserting elements in the tree, so the gfp mask is used to
594 * indicate which allocations or sleeping are allowed.
595 *
596 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
597 * the given range from the tree regardless of state (ie for truncate).
598 *
599 * the range [start, end] is inclusive.
600 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100601 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500602 */
David Sterba66b0c882017-10-31 16:30:47 +0100603int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800604 unsigned bits, int wake, int delete,
605 struct extent_state **cached_state,
606 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500607{
608 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400609 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500610 struct extent_state *prealloc = NULL;
611 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400612 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500613 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000614 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500615
Josef Bacika5dee372013-12-13 10:02:44 -0500616 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800617 trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000618
Josef Bacik7ee9e442013-06-21 16:37:03 -0400619 if (bits & EXTENT_DELALLOC)
620 bits |= EXTENT_NORESERVE;
621
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400622 if (delete)
623 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400624
Nikolay Borisov88826792019-03-14 15:28:31 +0200625 if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY))
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500627again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800628 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000629 /*
630 * Don't care for allocation failure here because we might end
631 * up not needing the pre-allocated extent state at all, which
632 * is the case if we only have in the tree extent states that
633 * cover our input range and don't cover too any other range.
634 * If we end up needing a new extent state we allocate it later.
635 */
Chris Masond1310b22008-01-24 16:13:08 -0500636 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500637 }
638
Chris Masoncad321a2008-12-17 14:51:42 -0500639 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400640 if (cached_state) {
641 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000642
643 if (clear) {
644 *cached_state = NULL;
645 cached_state = NULL;
646 }
647
Filipe Manana27a35072014-07-06 20:09:59 +0100648 if (cached && extent_state_in_tree(cached) &&
649 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000650 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200651 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400652 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400653 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400654 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000655 if (clear)
656 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400657 }
Chris Masond1310b22008-01-24 16:13:08 -0500658 /*
659 * this search will find the extents that end after
660 * our range starts
661 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500662 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500663 if (!node)
664 goto out;
665 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400666hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500667 if (state->start > end)
668 goto out;
669 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400670 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500671
Liu Bo04493142012-02-16 18:34:37 +0800672 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800673 if (!(state->state & bits)) {
674 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800675 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800676 }
Liu Bo04493142012-02-16 18:34:37 +0800677
Chris Masond1310b22008-01-24 16:13:08 -0500678 /*
679 * | ---- desired range ---- |
680 * | state | or
681 * | ------------- state -------------- |
682 *
683 * We need to split the extent we found, and may flip
684 * bits on second half.
685 *
686 * If the extent we found extends past our range, we
687 * just split and search again. It'll get split again
688 * the next time though.
689 *
690 * If the extent we found is inside our range, we clear
691 * the desired bit on it.
692 */
693
694 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000695 prealloc = alloc_extent_state_atomic(prealloc);
696 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500697 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400698 if (err)
699 extent_io_tree_panic(tree, err);
700
Chris Masond1310b22008-01-24 16:13:08 -0500701 prealloc = NULL;
702 if (err)
703 goto out;
704 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800705 state = clear_state_bit(tree, state, &bits, wake,
706 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800707 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500708 }
709 goto search_again;
710 }
711 /*
712 * | ---- desired range ---- |
713 * | state |
714 * We need to split the extent, and clear the bit
715 * on the first half
716 */
717 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000718 prealloc = alloc_extent_state_atomic(prealloc);
719 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500720 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400721 if (err)
722 extent_io_tree_panic(tree, err);
723
Chris Masond1310b22008-01-24 16:13:08 -0500724 if (wake)
725 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400726
Qu Wenruofefdc552015-10-12 15:35:38 +0800727 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400728
Chris Masond1310b22008-01-24 16:13:08 -0500729 prealloc = NULL;
730 goto out;
731 }
Chris Mason42daec22009-09-23 19:51:09 -0400732
Qu Wenruofefdc552015-10-12 15:35:38 +0800733 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800734next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400735 if (last_end == (u64)-1)
736 goto out;
737 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800738 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800739 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500740
741search_again:
742 if (start > end)
743 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500744 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800745 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500746 cond_resched();
747 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200748
749out:
750 spin_unlock(&tree->lock);
751 if (prealloc)
752 free_extent_state(prealloc);
753
754 return 0;
755
Chris Masond1310b22008-01-24 16:13:08 -0500756}
Chris Masond1310b22008-01-24 16:13:08 -0500757
Jeff Mahoney143bede2012-03-01 14:56:26 +0100758static void wait_on_state(struct extent_io_tree *tree,
759 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500760 __releases(tree->lock)
761 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500762{
763 DEFINE_WAIT(wait);
764 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500765 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500766 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500767 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500768 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500769}
770
771/*
772 * waits for one or more bits to clear on a range in the state tree.
773 * The range [start, end] is inclusive.
774 * The tree lock is taken by this function
775 */
David Sterba41074882013-04-29 13:38:46 +0000776static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
777 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500778{
779 struct extent_state *state;
780 struct rb_node *node;
781
Josef Bacika5dee372013-12-13 10:02:44 -0500782 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000783
Chris Masoncad321a2008-12-17 14:51:42 -0500784 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500785again:
786 while (1) {
787 /*
788 * this search will find all the extents that end after
789 * our range starts
790 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500791 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100792process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500793 if (!node)
794 break;
795
796 state = rb_entry(node, struct extent_state, rb_node);
797
798 if (state->start > end)
799 goto out;
800
801 if (state->state & bits) {
802 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200803 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500804 wait_on_state(tree, state);
805 free_extent_state(state);
806 goto again;
807 }
808 start = state->end + 1;
809
810 if (start > end)
811 break;
812
Filipe Mananac50d3e72014-03-31 14:53:25 +0100813 if (!cond_resched_lock(&tree->lock)) {
814 node = rb_next(node);
815 goto process_node;
816 }
Chris Masond1310b22008-01-24 16:13:08 -0500817 }
818out:
Chris Masoncad321a2008-12-17 14:51:42 -0500819 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500820}
Chris Masond1310b22008-01-24 16:13:08 -0500821
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000822static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500823 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800824 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500825{
David Sterba9ee49a042015-01-14 19:52:13 +0100826 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100827 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400828
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200829 if (tree->private_data && is_data_inode(tree->private_data))
830 btrfs_set_delalloc_extent(tree->private_data, state, bits);
831
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400832 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500833 u64 range = state->end - state->start + 1;
834 tree->dirty_bytes += range;
835 }
David Sterba57599c72018-03-01 17:56:34 +0100836 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
837 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400838 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500839}
840
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100841static void cache_state_if_flags(struct extent_state *state,
842 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100843 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400844{
845 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100846 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400847 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200848 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400849 }
850 }
851}
852
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100853static void cache_state(struct extent_state *state,
854 struct extent_state **cached_ptr)
855{
856 return cache_state_if_flags(state, cached_ptr,
Nikolay Borisov88826792019-03-14 15:28:31 +0200857 EXTENT_LOCKED | EXTENT_BOUNDARY);
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100858}
859
Chris Masond1310b22008-01-24 16:13:08 -0500860/*
Chris Mason1edbb732009-09-02 13:24:36 -0400861 * set some bits on a range in the tree. This may require allocations or
862 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500863 *
Chris Mason1edbb732009-09-02 13:24:36 -0400864 * If any of the exclusive bits are set, this will fail with -EEXIST if some
865 * part of the range already has the desired bits set. The start of the
866 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500867 *
Chris Mason1edbb732009-09-02 13:24:36 -0400868 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500869 */
Chris Mason1edbb732009-09-02 13:24:36 -0400870
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100871static int __must_check
872__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100873 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000874 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800875 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500876{
877 struct extent_state *state;
878 struct extent_state *prealloc = NULL;
879 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000880 struct rb_node **p;
881 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500882 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500883 u64 last_start;
884 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400885
Josef Bacika5dee372013-12-13 10:02:44 -0500886 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +0800887 trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
David Sterba8d599ae2013-04-30 15:22:23 +0000888
Chris Masond1310b22008-01-24 16:13:08 -0500889again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800890 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200891 /*
892 * Don't care for allocation failure here because we might end
893 * up not needing the pre-allocated extent state at all, which
894 * is the case if we only have in the tree extent states that
895 * cover our input range and don't cover too any other range.
896 * If we end up needing a new extent state we allocate it later.
897 */
Chris Masond1310b22008-01-24 16:13:08 -0500898 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500899 }
900
Chris Masoncad321a2008-12-17 14:51:42 -0500901 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400902 if (cached_state && *cached_state) {
903 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400904 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100905 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400906 node = &state->rb_node;
907 goto hit_next;
908 }
909 }
Chris Masond1310b22008-01-24 16:13:08 -0500910 /*
911 * this search will find all the extents that end after
912 * our range starts.
913 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000914 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500915 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000916 prealloc = alloc_extent_state_atomic(prealloc);
917 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000918 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800919 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400920 if (err)
921 extent_io_tree_panic(tree, err);
922
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000923 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500924 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500925 goto out;
926 }
Chris Masond1310b22008-01-24 16:13:08 -0500927 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400928hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500929 last_start = state->start;
930 last_end = state->end;
931
932 /*
933 * | ---- desired range ---- |
934 * | state |
935 *
936 * Just lock what we found and keep going
937 */
938 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400939 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500940 *failed_start = state->start;
941 err = -EEXIST;
942 goto out;
943 }
Chris Mason42daec22009-09-23 19:51:09 -0400944
Qu Wenruod38ed272015-10-12 14:53:37 +0800945 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400946 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500947 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400948 if (last_end == (u64)-1)
949 goto out;
950 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800951 state = next_state(state);
952 if (start < end && state && state->start == start &&
953 !need_resched())
954 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500955 goto search_again;
956 }
957
958 /*
959 * | ---- desired range ---- |
960 * | state |
961 * or
962 * | ------------- state -------------- |
963 *
964 * We need to split the extent we found, and may flip bits on
965 * second half.
966 *
967 * If the extent we found extends past our
968 * range, we just split and search again. It'll get split
969 * again the next time though.
970 *
971 * If the extent we found is inside our range, we set the
972 * desired bit on it.
973 */
974 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400975 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500976 *failed_start = start;
977 err = -EEXIST;
978 goto out;
979 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000980
981 prealloc = alloc_extent_state_atomic(prealloc);
982 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500983 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400984 if (err)
985 extent_io_tree_panic(tree, err);
986
Chris Masond1310b22008-01-24 16:13:08 -0500987 prealloc = NULL;
988 if (err)
989 goto out;
990 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800991 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400992 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500993 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400994 if (last_end == (u64)-1)
995 goto out;
996 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800997 state = next_state(state);
998 if (start < end && state && state->start == start &&
999 !need_resched())
1000 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -05001001 }
1002 goto search_again;
1003 }
1004 /*
1005 * | ---- desired range ---- |
1006 * | state | or | state |
1007 *
1008 * There's a hole, we need to insert something in it and
1009 * ignore the extent we found.
1010 */
1011 if (state->start > start) {
1012 u64 this_end;
1013 if (end < last_start)
1014 this_end = end;
1015 else
Chris Masond3977122009-01-05 21:25:51 -05001016 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001017
1018 prealloc = alloc_extent_state_atomic(prealloc);
1019 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001020
1021 /*
1022 * Avoid to free 'prealloc' if it can be merged with
1023 * the later extent.
1024 */
Chris Masond1310b22008-01-24 16:13:08 -05001025 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001026 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001027 if (err)
1028 extent_io_tree_panic(tree, err);
1029
Chris Mason2c64c532009-09-02 15:04:12 -04001030 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001031 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001032 start = this_end + 1;
1033 goto search_again;
1034 }
1035 /*
1036 * | ---- desired range ---- |
1037 * | state |
1038 * We need to split the extent, and set the bit
1039 * on the first half
1040 */
1041 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001042 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001043 *failed_start = start;
1044 err = -EEXIST;
1045 goto out;
1046 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001047
1048 prealloc = alloc_extent_state_atomic(prealloc);
1049 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001050 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001051 if (err)
1052 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001053
Qu Wenruod38ed272015-10-12 14:53:37 +08001054 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001055 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001056 merge_state(tree, prealloc);
1057 prealloc = NULL;
1058 goto out;
1059 }
1060
David Sterbab5a4ba142016-04-27 01:02:15 +02001061search_again:
1062 if (start > end)
1063 goto out;
1064 spin_unlock(&tree->lock);
1065 if (gfpflags_allow_blocking(mask))
1066 cond_resched();
1067 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001068
1069out:
Chris Masoncad321a2008-12-17 14:51:42 -05001070 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001071 if (prealloc)
1072 free_extent_state(prealloc);
1073
1074 return err;
1075
Chris Masond1310b22008-01-24 16:13:08 -05001076}
Chris Masond1310b22008-01-24 16:13:08 -05001077
David Sterba41074882013-04-29 13:38:46 +00001078int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001079 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001080 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001081{
1082 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001083 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001084}
1085
1086
Josef Bacik462d6fa2011-09-26 13:56:12 -04001087/**
Liu Bo10983f22012-07-11 15:26:19 +08001088 * convert_extent_bit - convert all bits in a given range from one bit to
1089 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090 * @tree: the io tree to search
1091 * @start: the start offset in bytes
1092 * @end: the end offset in bytes (inclusive)
1093 * @bits: the bits to set in this range
1094 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001095 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001096 *
1097 * This will go through and set bits for the given range. If any states exist
1098 * already in this range they are set with the given bit and cleared of the
1099 * clear_bits. This is only meant to be used by things that are mergeable, ie
1100 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1101 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001102 *
1103 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001104 */
1105int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001106 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001107 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108{
1109 struct extent_state *state;
1110 struct extent_state *prealloc = NULL;
1111 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001112 struct rb_node **p;
1113 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001114 int err = 0;
1115 u64 last_start;
1116 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001117 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001118
Josef Bacika5dee372013-12-13 10:02:44 -05001119 btrfs_debug_check_extent_io_range(tree, start, end);
Qu Wenruoa1d19842019-03-01 10:48:00 +08001120 trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
1121 clear_bits);
David Sterba8d599ae2013-04-30 15:22:23 +00001122
Josef Bacik462d6fa2011-09-26 13:56:12 -04001123again:
David Sterba210aa272016-04-26 23:54:39 +02001124 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001125 /*
1126 * Best effort, don't worry if extent state allocation fails
1127 * here for the first iteration. We might have a cached state
1128 * that matches exactly the target range, in which case no
1129 * extent state allocations are needed. We'll only know this
1130 * after locking the tree.
1131 */
David Sterba210aa272016-04-26 23:54:39 +02001132 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001133 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001134 return -ENOMEM;
1135 }
1136
1137 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001138 if (cached_state && *cached_state) {
1139 state = *cached_state;
1140 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001141 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001142 node = &state->rb_node;
1143 goto hit_next;
1144 }
1145 }
1146
Josef Bacik462d6fa2011-09-26 13:56:12 -04001147 /*
1148 * this search will find all the extents that end after
1149 * our range starts.
1150 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001151 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001152 if (!node) {
1153 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001154 if (!prealloc) {
1155 err = -ENOMEM;
1156 goto out;
1157 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001158 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001159 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001160 if (err)
1161 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001162 cache_state(prealloc, cached_state);
1163 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001164 goto out;
1165 }
1166 state = rb_entry(node, struct extent_state, rb_node);
1167hit_next:
1168 last_start = state->start;
1169 last_end = state->end;
1170
1171 /*
1172 * | ---- desired range ---- |
1173 * | state |
1174 *
1175 * Just lock what we found and keep going
1176 */
1177 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001178 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001179 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001180 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001181 if (last_end == (u64)-1)
1182 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001183 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001184 if (start < end && state && state->start == start &&
1185 !need_resched())
1186 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001187 goto search_again;
1188 }
1189
1190 /*
1191 * | ---- desired range ---- |
1192 * | state |
1193 * or
1194 * | ------------- state -------------- |
1195 *
1196 * We need to split the extent we found, and may flip bits on
1197 * second half.
1198 *
1199 * If the extent we found extends past our
1200 * range, we just split and search again. It'll get split
1201 * again the next time though.
1202 *
1203 * If the extent we found is inside our range, we set the
1204 * desired bit on it.
1205 */
1206 if (state->start < start) {
1207 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001208 if (!prealloc) {
1209 err = -ENOMEM;
1210 goto out;
1211 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001212 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001213 if (err)
1214 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001215 prealloc = NULL;
1216 if (err)
1217 goto out;
1218 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001219 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001220 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001221 state = clear_state_bit(tree, state, &clear_bits, 0,
1222 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001223 if (last_end == (u64)-1)
1224 goto out;
1225 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001226 if (start < end && state && state->start == start &&
1227 !need_resched())
1228 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001229 }
1230 goto search_again;
1231 }
1232 /*
1233 * | ---- desired range ---- |
1234 * | state | or | state |
1235 *
1236 * There's a hole, we need to insert something in it and
1237 * ignore the extent we found.
1238 */
1239 if (state->start > start) {
1240 u64 this_end;
1241 if (end < last_start)
1242 this_end = end;
1243 else
1244 this_end = last_start - 1;
1245
1246 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001247 if (!prealloc) {
1248 err = -ENOMEM;
1249 goto out;
1250 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001251
1252 /*
1253 * Avoid to free 'prealloc' if it can be merged with
1254 * the later extent.
1255 */
1256 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001257 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001258 if (err)
1259 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001260 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001261 prealloc = NULL;
1262 start = this_end + 1;
1263 goto search_again;
1264 }
1265 /*
1266 * | ---- desired range ---- |
1267 * | state |
1268 * We need to split the extent, and set the bit
1269 * on the first half
1270 */
1271 if (state->start <= end && state->end > end) {
1272 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001273 if (!prealloc) {
1274 err = -ENOMEM;
1275 goto out;
1276 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001277
1278 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001279 if (err)
1280 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001281
Qu Wenruod38ed272015-10-12 14:53:37 +08001282 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001283 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001284 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001285 prealloc = NULL;
1286 goto out;
1287 }
1288
Josef Bacik462d6fa2011-09-26 13:56:12 -04001289search_again:
1290 if (start > end)
1291 goto out;
1292 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001293 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001294 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001295 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001296
1297out:
1298 spin_unlock(&tree->lock);
1299 if (prealloc)
1300 free_extent_state(prealloc);
1301
1302 return err;
1303}
1304
Chris Masond1310b22008-01-24 16:13:08 -05001305/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001306int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001307 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001308{
1309 /*
1310 * We don't support EXTENT_LOCKED yet, as current changeset will
1311 * record any bits changed, so for EXTENT_LOCKED case, it will
1312 * either fail with -EEXIST or changeset will record the whole
1313 * range.
1314 */
1315 BUG_ON(bits & EXTENT_LOCKED);
1316
David Sterba2c53b912016-04-26 23:54:39 +02001317 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001318 changeset);
1319}
1320
Qu Wenruofefdc552015-10-12 15:35:38 +08001321int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1322 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001323 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001324{
1325 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001326 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001327}
1328
Qu Wenruofefdc552015-10-12 15:35:38 +08001329int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001330 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001331{
1332 /*
1333 * Don't support EXTENT_LOCKED case, same reason as
1334 * set_record_extent_bits().
1335 */
1336 BUG_ON(bits & EXTENT_LOCKED);
1337
David Sterbaf734c442016-04-26 23:54:39 +02001338 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001339 changeset);
1340}
1341
Chris Masond352ac62008-09-29 15:18:18 -04001342/*
1343 * either insert or lock state struct between start and end use mask to tell
1344 * us if waiting is desired.
1345 */
Chris Mason1edbb732009-09-02 13:24:36 -04001346int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001347 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001348{
1349 int err;
1350 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001351
Chris Masond1310b22008-01-24 16:13:08 -05001352 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001353 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001354 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001355 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001356 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001357 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1358 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001359 } else
Chris Masond1310b22008-01-24 16:13:08 -05001360 break;
Chris Masond1310b22008-01-24 16:13:08 -05001361 WARN_ON(start > end);
1362 }
1363 return err;
1364}
Chris Masond1310b22008-01-24 16:13:08 -05001365
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001366int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001367{
1368 int err;
1369 u64 failed_start;
1370
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001371 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001372 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001373 if (err == -EEXIST) {
1374 if (failed_start > start)
1375 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001376 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001377 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001378 }
Josef Bacik25179202008-10-29 14:49:05 -04001379 return 1;
1380}
Josef Bacik25179202008-10-29 14:49:05 -04001381
David Sterbabd1fa4f2015-12-03 13:08:59 +01001382void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001383{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001384 unsigned long index = start >> PAGE_SHIFT;
1385 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001386 struct page *page;
1387
1388 while (index <= end_index) {
1389 page = find_get_page(inode->i_mapping, index);
1390 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1391 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001392 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001393 index++;
1394 }
Chris Mason4adaa612013-03-26 13:07:00 -04001395}
1396
David Sterbaf6311572015-12-03 13:08:59 +01001397void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001398{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001399 unsigned long index = start >> PAGE_SHIFT;
1400 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001401 struct page *page;
1402
1403 while (index <= end_index) {
1404 page = find_get_page(inode->i_mapping, index);
1405 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001406 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001407 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001408 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001409 index++;
1410 }
Chris Mason4adaa612013-03-26 13:07:00 -04001411}
1412
Chris Masond352ac62008-09-29 15:18:18 -04001413/* find the first state struct with 'bits' set after 'start', and
1414 * return it. tree->lock must be held. NULL will returned if
1415 * nothing was found after 'start'
1416 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001417static struct extent_state *
1418find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001419 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001420{
1421 struct rb_node *node;
1422 struct extent_state *state;
1423
1424 /*
1425 * this search will find all the extents that end after
1426 * our range starts.
1427 */
1428 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001429 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001430 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001431
Chris Masond3977122009-01-05 21:25:51 -05001432 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001433 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001434 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001435 return state;
Chris Masond3977122009-01-05 21:25:51 -05001436
Chris Masond7fc6402008-02-18 12:12:38 -05001437 node = rb_next(node);
1438 if (!node)
1439 break;
1440 }
1441out:
1442 return NULL;
1443}
Chris Masond7fc6402008-02-18 12:12:38 -05001444
Chris Masond352ac62008-09-29 15:18:18 -04001445/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001446 * find the first offset in the io tree with 'bits' set. zero is
1447 * returned if we find something, and *start_ret and *end_ret are
1448 * set to reflect the state struct that was found.
1449 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001450 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001451 */
1452int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001453 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001454 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001455{
1456 struct extent_state *state;
1457 int ret = 1;
1458
1459 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001460 if (cached_state && *cached_state) {
1461 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001462 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001463 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001464 if (state->state & bits)
1465 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001466 }
1467 free_extent_state(*cached_state);
1468 *cached_state = NULL;
1469 goto out;
1470 }
1471 free_extent_state(*cached_state);
1472 *cached_state = NULL;
1473 }
1474
Xiao Guangrong69261c42011-07-14 03:19:45 +00001475 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001476got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001477 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001478 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001479 *start_ret = state->start;
1480 *end_ret = state->end;
1481 ret = 0;
1482 }
Josef Bacike6138872012-09-27 17:07:30 -04001483out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001484 spin_unlock(&tree->lock);
1485 return ret;
1486}
1487
1488/*
Chris Masond352ac62008-09-29 15:18:18 -04001489 * find a contiguous range of bytes in the file marked as delalloc, not
1490 * more than 'max_bytes'. start and end are used to return the range,
1491 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001492 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001493 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001494static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001495 u64 *start, u64 *end, u64 max_bytes,
1496 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001497{
1498 struct rb_node *node;
1499 struct extent_state *state;
1500 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001501 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001502 u64 total_bytes = 0;
1503
Chris Masoncad321a2008-12-17 14:51:42 -05001504 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001505
Chris Masond1310b22008-01-24 16:13:08 -05001506 /*
1507 * this search will find all the extents that end after
1508 * our range starts.
1509 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001510 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001511 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001512 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001513 goto out;
1514 }
1515
Chris Masond3977122009-01-05 21:25:51 -05001516 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001517 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001518 if (found && (state->start != cur_start ||
1519 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001520 goto out;
1521 }
1522 if (!(state->state & EXTENT_DELALLOC)) {
1523 if (!found)
1524 *end = state->end;
1525 goto out;
1526 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001527 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001528 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001529 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001530 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001531 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001532 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001533 *end = state->end;
1534 cur_start = state->end + 1;
1535 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001536 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001537 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001538 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001539 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001540 break;
1541 }
1542out:
Chris Masoncad321a2008-12-17 14:51:42 -05001543 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001544 return found;
1545}
1546
Liu Boda2c7002017-02-10 16:41:05 +01001547static int __process_pages_contig(struct address_space *mapping,
1548 struct page *locked_page,
1549 pgoff_t start_index, pgoff_t end_index,
1550 unsigned long page_ops, pgoff_t *index_ret);
1551
Jeff Mahoney143bede2012-03-01 14:56:26 +01001552static noinline void __unlock_for_delalloc(struct inode *inode,
1553 struct page *locked_page,
1554 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001555{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001556 unsigned long index = start >> PAGE_SHIFT;
1557 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001558
Liu Bo76c00212017-02-10 16:42:14 +01001559 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001560 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001561 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001562
Liu Bo76c00212017-02-10 16:42:14 +01001563 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1564 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001565}
1566
1567static noinline int lock_delalloc_pages(struct inode *inode,
1568 struct page *locked_page,
1569 u64 delalloc_start,
1570 u64 delalloc_end)
1571{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001572 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001573 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001574 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001575 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001576
Liu Bo76c00212017-02-10 16:42:14 +01001577 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001578 if (index == locked_page->index && index == end_index)
1579 return 0;
1580
Liu Bo76c00212017-02-10 16:42:14 +01001581 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1582 end_index, PAGE_LOCK, &index_ret);
1583 if (ret == -EAGAIN)
1584 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1585 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001586 return ret;
1587}
1588
1589/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001590 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1591 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001592 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001593 * Return: true if we find something
1594 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001595 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001596EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001597noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001598 struct extent_io_tree *tree,
1599 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001600 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001601{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001602 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001603 u64 delalloc_start;
1604 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001605 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001606 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001607 int ret;
1608 int loops = 0;
1609
1610again:
1611 /* step one, find a bunch of delalloc bytes starting at start */
1612 delalloc_start = *start;
1613 delalloc_end = 0;
1614 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001615 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001616 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001617 *start = delalloc_start;
1618 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001619 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001620 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001621 }
1622
1623 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001624 * start comes from the offset of locked_page. We have to lock
1625 * pages in order, so we can't process delalloc bytes before
1626 * locked_page
1627 */
Chris Masond3977122009-01-05 21:25:51 -05001628 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001629 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001630
1631 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001632 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001633 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001634 if (delalloc_end + 1 - delalloc_start > max_bytes)
1635 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001636
Chris Masonc8b97812008-10-29 14:49:59 -04001637 /* step two, lock all the pages after the page that has start */
1638 ret = lock_delalloc_pages(inode, locked_page,
1639 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001640 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001641 if (ret == -EAGAIN) {
1642 /* some of the pages are gone, lets avoid looping by
1643 * shortening the size of the delalloc range we're searching
1644 */
Chris Mason9655d292009-09-02 15:22:30 -04001645 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001646 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001647 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001648 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001649 loops = 1;
1650 goto again;
1651 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001652 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001653 goto out_failed;
1654 }
1655 }
Chris Masonc8b97812008-10-29 14:49:59 -04001656
1657 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001658 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001659
1660 /* then test to make sure it is all still delalloc */
1661 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001662 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001663 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001664 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001665 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001666 __unlock_for_delalloc(inode, locked_page,
1667 delalloc_start, delalloc_end);
1668 cond_resched();
1669 goto again;
1670 }
Chris Mason9655d292009-09-02 15:22:30 -04001671 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001672 *start = delalloc_start;
1673 *end = delalloc_end;
1674out_failed:
1675 return found;
1676}
1677
Liu Boda2c7002017-02-10 16:41:05 +01001678static int __process_pages_contig(struct address_space *mapping,
1679 struct page *locked_page,
1680 pgoff_t start_index, pgoff_t end_index,
1681 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001682{
Liu Bo873695b2017-02-02 17:49:22 -08001683 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001684 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001685 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001686 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001687 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001688 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001689 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001690
Liu Boda2c7002017-02-10 16:41:05 +01001691 if (page_ops & PAGE_LOCK) {
1692 ASSERT(page_ops == PAGE_LOCK);
1693 ASSERT(index_ret && *index_ret == start_index);
1694 }
1695
Filipe Manana704de492014-10-06 22:14:22 +01001696 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001697 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001698
Chris Masond3977122009-01-05 21:25:51 -05001699 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001700 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001701 min_t(unsigned long,
1702 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001703 if (ret == 0) {
1704 /*
1705 * Only if we're going to lock these pages,
1706 * can we find nothing at @index.
1707 */
1708 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001709 err = -EAGAIN;
1710 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001711 }
Chris Mason8b62b722009-09-02 16:53:46 -04001712
Liu Boda2c7002017-02-10 16:41:05 +01001713 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001714 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001715 SetPagePrivate2(pages[i]);
1716
Chris Masonc8b97812008-10-29 14:49:59 -04001717 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001718 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001719 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001720 continue;
1721 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001722 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001723 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001724 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001725 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001726 if (page_ops & PAGE_SET_ERROR)
1727 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001728 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001729 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001730 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001731 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001732 if (page_ops & PAGE_LOCK) {
1733 lock_page(pages[i]);
1734 if (!PageDirty(pages[i]) ||
1735 pages[i]->mapping != mapping) {
1736 unlock_page(pages[i]);
1737 put_page(pages[i]);
1738 err = -EAGAIN;
1739 goto out;
1740 }
1741 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001742 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001743 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001744 }
1745 nr_pages -= ret;
1746 index += ret;
1747 cond_resched();
1748 }
Liu Boda2c7002017-02-10 16:41:05 +01001749out:
1750 if (err && index_ret)
1751 *index_ret = start_index + pages_locked - 1;
1752 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001753}
Chris Masonc8b97812008-10-29 14:49:59 -04001754
Liu Bo873695b2017-02-02 17:49:22 -08001755void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1756 u64 delalloc_end, struct page *locked_page,
1757 unsigned clear_bits,
1758 unsigned long page_ops)
1759{
1760 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001761 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001762
1763 __process_pages_contig(inode->i_mapping, locked_page,
1764 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001765 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001766}
1767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
1769 * count the number of bytes in the tree that have a given bit(s)
1770 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1771 * cached. The total number found is returned.
1772 */
Chris Masond1310b22008-01-24 16:13:08 -05001773u64 count_range_bits(struct extent_io_tree *tree,
1774 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001775 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001776{
1777 struct rb_node *node;
1778 struct extent_state *state;
1779 u64 cur_start = *start;
1780 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001781 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001782 int found = 0;
1783
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301784 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001785 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001786
Chris Masoncad321a2008-12-17 14:51:42 -05001787 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001788 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1789 total_bytes = tree->dirty_bytes;
1790 goto out;
1791 }
1792 /*
1793 * this search will find all the extents that end after
1794 * our range starts.
1795 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001796 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001797 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001798 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001799
Chris Masond3977122009-01-05 21:25:51 -05001800 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001801 state = rb_entry(node, struct extent_state, rb_node);
1802 if (state->start > search_end)
1803 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001804 if (contig && found && state->start > last + 1)
1805 break;
1806 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001807 total_bytes += min(search_end, state->end) + 1 -
1808 max(cur_start, state->start);
1809 if (total_bytes >= max_bytes)
1810 break;
1811 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001812 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001813 found = 1;
1814 }
Chris Masonec29ed52011-02-23 16:23:20 -05001815 last = state->end;
1816 } else if (contig && found) {
1817 break;
Chris Masond1310b22008-01-24 16:13:08 -05001818 }
1819 node = rb_next(node);
1820 if (!node)
1821 break;
1822 }
1823out:
Chris Masoncad321a2008-12-17 14:51:42 -05001824 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001825 return total_bytes;
1826}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001827
Chris Masond352ac62008-09-29 15:18:18 -04001828/*
1829 * set the private field for a given byte offset in the tree. If there isn't
1830 * an extent_state there already, this does nothing.
1831 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001832static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001833 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001834{
1835 struct rb_node *node;
1836 struct extent_state *state;
1837 int ret = 0;
1838
Chris Masoncad321a2008-12-17 14:51:42 -05001839 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001840 /*
1841 * this search will find all the extents that end after
1842 * our range starts.
1843 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001844 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001845 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001846 ret = -ENOENT;
1847 goto out;
1848 }
1849 state = rb_entry(node, struct extent_state, rb_node);
1850 if (state->start != start) {
1851 ret = -ENOENT;
1852 goto out;
1853 }
David Sterba47dc1962016-02-11 13:24:13 +01001854 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001855out:
Chris Masoncad321a2008-12-17 14:51:42 -05001856 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001857 return ret;
1858}
1859
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001860static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001861 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001862{
1863 struct rb_node *node;
1864 struct extent_state *state;
1865 int ret = 0;
1866
Chris Masoncad321a2008-12-17 14:51:42 -05001867 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001868 /*
1869 * this search will find all the extents that end after
1870 * our range starts.
1871 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001872 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001873 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001874 ret = -ENOENT;
1875 goto out;
1876 }
1877 state = rb_entry(node, struct extent_state, rb_node);
1878 if (state->start != start) {
1879 ret = -ENOENT;
1880 goto out;
1881 }
David Sterba47dc1962016-02-11 13:24:13 +01001882 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001883out:
Chris Masoncad321a2008-12-17 14:51:42 -05001884 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001885 return ret;
1886}
1887
1888/*
1889 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001890 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001891 * has the bits set. Otherwise, 1 is returned if any bit in the
1892 * range is found set.
1893 */
1894int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001895 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001896{
1897 struct extent_state *state = NULL;
1898 struct rb_node *node;
1899 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001900
Chris Masoncad321a2008-12-17 14:51:42 -05001901 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001902 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001903 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001904 node = &cached->rb_node;
1905 else
1906 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001907 while (node && start <= end) {
1908 state = rb_entry(node, struct extent_state, rb_node);
1909
1910 if (filled && state->start > start) {
1911 bitset = 0;
1912 break;
1913 }
1914
1915 if (state->start > end)
1916 break;
1917
1918 if (state->state & bits) {
1919 bitset = 1;
1920 if (!filled)
1921 break;
1922 } else if (filled) {
1923 bitset = 0;
1924 break;
1925 }
Chris Mason46562ce2009-09-23 20:23:16 -04001926
1927 if (state->end == (u64)-1)
1928 break;
1929
Chris Masond1310b22008-01-24 16:13:08 -05001930 start = state->end + 1;
1931 if (start > end)
1932 break;
1933 node = rb_next(node);
1934 if (!node) {
1935 if (filled)
1936 bitset = 0;
1937 break;
1938 }
1939 }
Chris Masoncad321a2008-12-17 14:51:42 -05001940 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001941 return bitset;
1942}
Chris Masond1310b22008-01-24 16:13:08 -05001943
1944/*
1945 * helper function to set a given page up to date if all the
1946 * extents in the tree for that page are up to date
1947 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001948static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001949{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001950 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001951 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001952 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001953 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001954}
1955
Josef Bacik7870d082017-05-05 11:57:15 -04001956int free_io_failure(struct extent_io_tree *failure_tree,
1957 struct extent_io_tree *io_tree,
1958 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001959{
1960 int ret;
1961 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001962
David Sterba47dc1962016-02-11 13:24:13 +01001963 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001964 ret = clear_extent_bits(failure_tree, rec->start,
1965 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001966 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001967 if (ret)
1968 err = ret;
1969
Josef Bacik7870d082017-05-05 11:57:15 -04001970 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001971 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001972 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001973 if (ret && !err)
1974 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001975
1976 kfree(rec);
1977 return err;
1978}
1979
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980/*
1981 * this bypasses the standard btrfs submit functions deliberately, as
1982 * the standard behavior is to write all copies in a raid setup. here we only
1983 * want to write the one bad copy. so we do the mapping for ourselves and issue
1984 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001985 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001986 * actually prevents the read that triggered the error from finishing.
1987 * currently, there can be no more than two copies of every data bit. thus,
1988 * exactly one rewrite is required.
1989 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001990int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1991 u64 length, u64 logical, struct page *page,
1992 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001993{
1994 struct bio *bio;
1995 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001996 u64 map_length = 0;
1997 u64 sector;
1998 struct btrfs_bio *bbio = NULL;
1999 int ret;
2000
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002001 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002002 BUG_ON(!mirror_num);
2003
David Sterbac5e4c3d2017-06-12 17:29:41 +02002004 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002005 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002006 map_length = length;
2007
Filipe Mananab5de8d02016-05-27 22:21:27 +01002008 /*
2009 * Avoid races with device replace and make sure our bbio has devices
2010 * associated to its stripes that don't go away while we are doing the
2011 * read repair operation.
2012 */
2013 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002014 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002015 /*
2016 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2017 * to update all raid stripes, but here we just want to correct
2018 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2019 * stripe's dev and sector.
2020 */
2021 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2022 &map_length, &bbio, 0);
2023 if (ret) {
2024 btrfs_bio_counter_dec(fs_info);
2025 bio_put(bio);
2026 return -EIO;
2027 }
2028 ASSERT(bbio->mirror_num == 1);
2029 } else {
2030 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2031 &map_length, &bbio, mirror_num);
2032 if (ret) {
2033 btrfs_bio_counter_dec(fs_info);
2034 bio_put(bio);
2035 return -EIO;
2036 }
2037 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002038 }
Liu Boc7253282017-03-29 10:53:58 -07002039
2040 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002041 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002042 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002043 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002044 if (!dev || !dev->bdev ||
2045 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002046 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002047 bio_put(bio);
2048 return -EIO;
2049 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002050 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002051 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002052 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002053
Mike Christie4e49ea42016-06-05 14:31:41 -05002054 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002055 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002056 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002057 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002058 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 return -EIO;
2060 }
2061
David Sterbab14af3b2015-10-08 10:43:10 +02002062 btrfs_info_rl_in_rcu(fs_info,
2063 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002064 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002065 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002066 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002067 bio_put(bio);
2068 return 0;
2069}
2070
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002071int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2072 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002073{
Josef Bacikea466792012-03-26 21:57:36 -04002074 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002075 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002076 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002077
David Howellsbc98a422017-07-17 08:45:34 +01002078 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002079 return -EROFS;
2080
Josef Bacikea466792012-03-26 21:57:36 -04002081 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002082 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002083
Josef Bacik6ec656b2017-05-05 11:57:14 -04002084 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002085 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002086 if (ret)
2087 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002088 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002089 }
2090
2091 return ret;
2092}
2093
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002094/*
2095 * each time an IO finishes, we do a fast check in the IO failure tree
2096 * to see if we need to process or clean up an io_failure_record
2097 */
Josef Bacik7870d082017-05-05 11:57:15 -04002098int clean_io_failure(struct btrfs_fs_info *fs_info,
2099 struct extent_io_tree *failure_tree,
2100 struct extent_io_tree *io_tree, u64 start,
2101 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002102{
2103 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002104 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105 struct extent_state *state;
2106 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002107 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108
2109 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002110 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2111 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002112 if (!ret)
2113 return 0;
2114
Josef Bacik7870d082017-05-05 11:57:15 -04002115 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002116 if (ret)
2117 return 0;
2118
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002119 BUG_ON(!failrec->this_mirror);
2120
2121 if (failrec->in_validation) {
2122 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002123 btrfs_debug(fs_info,
2124 "clean_io_failure: freeing dummy error at %llu",
2125 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002126 goto out;
2127 }
David Howellsbc98a422017-07-17 08:45:34 +01002128 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002129 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002130
Josef Bacik7870d082017-05-05 11:57:15 -04002131 spin_lock(&io_tree->lock);
2132 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002133 failrec->start,
2134 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002135 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002136
Miao Xie883d0de2013-07-25 19:22:35 +08002137 if (state && state->start <= failrec->start &&
2138 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002139 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2140 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002141 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002142 repair_io_failure(fs_info, ino, start, failrec->len,
2143 failrec->logical, page, pg_offset,
2144 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002145 }
2146 }
2147
2148out:
Josef Bacik7870d082017-05-05 11:57:15 -04002149 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002150
Miao Xie454ff3d2014-09-12 18:43:58 +08002151 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002152}
2153
Miao Xief6124962014-09-12 18:44:04 +08002154/*
2155 * Can be called when
2156 * - hold extent lock
2157 * - under ordered extent
2158 * - the inode is freeing
2159 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002160void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002161{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002162 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002163 struct io_failure_record *failrec;
2164 struct extent_state *state, *next;
2165
2166 if (RB_EMPTY_ROOT(&failure_tree->state))
2167 return;
2168
2169 spin_lock(&failure_tree->lock);
2170 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2171 while (state) {
2172 if (state->start > end)
2173 break;
2174
2175 ASSERT(state->end <= end);
2176
2177 next = next_state(state);
2178
David Sterba47dc1962016-02-11 13:24:13 +01002179 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002180 free_extent_state(state);
2181 kfree(failrec);
2182
2183 state = next;
2184 }
2185 spin_unlock(&failure_tree->lock);
2186}
2187
Miao Xie2fe63032014-09-12 18:43:59 +08002188int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002189 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002190{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002192 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002193 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002194 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2195 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2196 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002197 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002198 u64 logical;
2199
David Sterba47dc1962016-02-11 13:24:13 +01002200 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002201 if (ret) {
2202 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2203 if (!failrec)
2204 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002205
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002206 failrec->start = start;
2207 failrec->len = end - start + 1;
2208 failrec->this_mirror = 0;
2209 failrec->bio_flags = 0;
2210 failrec->in_validation = 0;
2211
2212 read_lock(&em_tree->lock);
2213 em = lookup_extent_mapping(em_tree, start, failrec->len);
2214 if (!em) {
2215 read_unlock(&em_tree->lock);
2216 kfree(failrec);
2217 return -EIO;
2218 }
2219
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002220 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002221 free_extent_map(em);
2222 em = NULL;
2223 }
2224 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002225 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002226 kfree(failrec);
2227 return -EIO;
2228 }
Miao Xie2fe63032014-09-12 18:43:59 +08002229
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002230 logical = start - em->start;
2231 logical = em->block_start + logical;
2232 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2233 logical = em->block_start;
2234 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2235 extent_set_compress_type(&failrec->bio_flags,
2236 em->compress_type);
2237 }
Miao Xie2fe63032014-09-12 18:43:59 +08002238
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002239 btrfs_debug(fs_info,
2240 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2241 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002242
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 failrec->logical = logical;
2244 free_extent_map(em);
2245
2246 /* set the bits in the private failure tree */
2247 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002248 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002249 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002250 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002251 /* set the bits in the inode's tree */
2252 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002253 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 if (ret < 0) {
2255 kfree(failrec);
2256 return ret;
2257 }
2258 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002259 btrfs_debug(fs_info,
2260 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2261 failrec->logical, failrec->start, failrec->len,
2262 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002263 /*
2264 * when data can be on disk more than twice, add to failrec here
2265 * (e.g. with a list for failed_mirror) to make
2266 * clean_io_failure() clean all those errors at once.
2267 */
2268 }
Miao Xie2fe63032014-09-12 18:43:59 +08002269
2270 *failrec_ret = failrec;
2271
2272 return 0;
2273}
2274
Ming Leia0b60d72017-12-18 20:22:11 +08002275bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002276 struct io_failure_record *failrec, int failed_mirror)
2277{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002279 int num_copies;
2280
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002281 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002282 if (num_copies == 1) {
2283 /*
2284 * we only have a single copy of the data, so don't bother with
2285 * all the retry and error correction code that follows. no
2286 * matter what the error is, it is very likely to persist.
2287 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002288 btrfs_debug(fs_info,
2289 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2290 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002291 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002292 }
2293
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002294 /*
2295 * there are two premises:
2296 * a) deliver good data to the caller
2297 * b) correct the bad sectors on disk
2298 */
Ming Leia0b60d72017-12-18 20:22:11 +08002299 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002300 /*
2301 * to fulfill b), we need to know the exact failing sectors, as
2302 * we don't want to rewrite any more than the failed ones. thus,
2303 * we need separate read requests for the failed bio
2304 *
2305 * if the following BUG_ON triggers, our validation request got
2306 * merged. we need separate requests for our algorithm to work.
2307 */
2308 BUG_ON(failrec->in_validation);
2309 failrec->in_validation = 1;
2310 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002311 } else {
2312 /*
2313 * we're ready to fulfill a) and b) alongside. get a good copy
2314 * of the failed sector and if we succeed, we have setup
2315 * everything for repair_io_failure to do the rest for us.
2316 */
2317 if (failrec->in_validation) {
2318 BUG_ON(failrec->this_mirror != failed_mirror);
2319 failrec->in_validation = 0;
2320 failrec->this_mirror = 0;
2321 }
2322 failrec->failed_mirror = failed_mirror;
2323 failrec->this_mirror++;
2324 if (failrec->this_mirror == failed_mirror)
2325 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002326 }
2327
Miao Xiefacc8a222013-07-25 19:22:34 +08002328 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002329 btrfs_debug(fs_info,
2330 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2331 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002332 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002333 }
2334
Liu Boc3cfb652017-07-13 15:00:50 -07002335 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002336}
2337
2338
2339struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2340 struct io_failure_record *failrec,
2341 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002342 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002344 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002345 struct bio *bio;
2346 struct btrfs_io_bio *btrfs_failed_bio;
2347 struct btrfs_io_bio *btrfs_bio;
2348
David Sterbac5e4c3d2017-06-12 17:29:41 +02002349 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002350 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002351 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002352 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002353 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002354 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002355
Miao Xiefacc8a222013-07-25 19:22:34 +08002356 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2357 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002358 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2359
2360 btrfs_bio = btrfs_io_bio(bio);
2361 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002362 icsum *= csum_size;
2363 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002364 csum_size);
2365 }
2366
Miao Xie2fe63032014-09-12 18:43:59 +08002367 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002368
Miao Xie2fe63032014-09-12 18:43:59 +08002369 return bio;
2370}
2371
2372/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002373 * This is a generic handler for readpage errors. If other copies exist, read
2374 * those and write back good data to the failed position. Does not investigate
2375 * in remapping the failed extent elsewhere, hoping the device will be smart
2376 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002377 */
Miao Xie2fe63032014-09-12 18:43:59 +08002378static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2379 struct page *page, u64 start, u64 end,
2380 int failed_mirror)
2381{
2382 struct io_failure_record *failrec;
2383 struct inode *inode = page->mapping->host;
2384 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002385 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002386 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002387 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002388 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002389 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002390 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002391
Mike Christie1f7ad752016-06-05 14:31:51 -05002392 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002393
2394 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2395 if (ret)
2396 return ret;
2397
Ming Leia0b60d72017-12-18 20:22:11 +08002398 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002399 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002400 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002401 return -EIO;
2402 }
2403
Ming Leia0b60d72017-12-18 20:22:11 +08002404 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002405 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002406
2407 phy_offset >>= inode->i_sb->s_blocksize_bits;
2408 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2409 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002410 (int)phy_offset, failed_bio->bi_end_io,
2411 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002412 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002413
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002414 btrfs_debug(btrfs_sb(inode->i_sb),
2415 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2416 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002417
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002418 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002419 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002420 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002421 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002422 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002423 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002424 }
2425
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002426 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002427}
2428
Chris Masond1310b22008-01-24 16:13:08 -05002429/* lots and lots of room for performance fixes in the end_bio funcs */
2430
David Sterbab5227c02015-12-03 13:08:59 +01002431void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002432{
2433 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002434 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002435
Nikolay Borisovc6297322018-11-08 10:18:08 +02002436 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002437
Jeff Mahoney87826df2012-02-15 16:23:57 +01002438 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002439 ClearPageUptodate(page);
2440 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002441 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002442 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002443 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002444}
2445
Chris Masond1310b22008-01-24 16:13:08 -05002446/*
2447 * after a writepage IO is done, we need to:
2448 * clear the uptodate bits on error
2449 * clear the writeback bits in the extent tree for this IO
2450 * end_page_writeback if the page has no more pending IO
2451 *
2452 * Scheduling is not allowed, so the extent state tree is expected
2453 * to have one and only one object corresponding to this IO.
2454 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002455static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002456{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002457 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002458 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002459 u64 start;
2460 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002461 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002462 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002463
David Sterbac09abff2017-07-13 18:10:07 +02002464 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002465 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002466 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002467 struct inode *inode = page->mapping->host;
2468 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002469
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002470 /* We always issue full-page reads, but if some block
2471 * in a page fails to read, blk_update_request() will
2472 * advance bv_offset and adjust bv_len to compensate.
2473 * Print a warning for nonzero offsets, and an error
2474 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002475 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2476 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002477 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002478 "partial page write in btrfs with offset %u and length %u",
2479 bvec->bv_offset, bvec->bv_len);
2480 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002481 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002482 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002483 bvec->bv_offset, bvec->bv_len);
2484 }
Chris Masond1310b22008-01-24 16:13:08 -05002485
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002486 start = page_offset(page);
2487 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002488
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002489 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002490 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002491 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002492
Chris Masond1310b22008-01-24 16:13:08 -05002493 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002494}
2495
Miao Xie883d0de2013-07-25 19:22:35 +08002496static void
2497endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2498 int uptodate)
2499{
2500 struct extent_state *cached = NULL;
2501 u64 end = start + len - 1;
2502
2503 if (uptodate && tree->track_uptodate)
2504 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002505 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002506}
2507
Chris Masond1310b22008-01-24 16:13:08 -05002508/*
2509 * after a readpage IO is done, we need to:
2510 * clear the uptodate bits on error
2511 * set the uptodate bits if things worked
2512 * set the page up to date if all extents in the tree are uptodate
2513 * clear the lock bit in the extent tree
2514 * unlock the page if there are no other extents locked for it
2515 *
2516 * Scheduling is not allowed, so the extent state tree is expected
2517 * to have one and only one object corresponding to this IO.
2518 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002519static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002520{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002521 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002522 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002523 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002524 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002525 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002526 u64 start;
2527 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002528 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002529 u64 extent_start = 0;
2530 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002531 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002532 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002533 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002534 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002535
David Sterbac09abff2017-07-13 18:10:07 +02002536 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002537 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002538 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002539 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002540 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002541 bool data_inode = btrfs_ino(BTRFS_I(inode))
2542 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002543
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002544 btrfs_debug(fs_info,
2545 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002546 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002547 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002548 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002549 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002550
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002551 /* We always issue full-page reads, but if some block
2552 * in a page fails to read, blk_update_request() will
2553 * advance bv_offset and adjust bv_len to compensate.
2554 * Print a warning for nonzero offsets, and an error
2555 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002556 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2557 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002558 btrfs_err(fs_info,
2559 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002560 bvec->bv_offset, bvec->bv_len);
2561 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002562 btrfs_info(fs_info,
2563 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002564 bvec->bv_offset, bvec->bv_len);
2565 }
Chris Masond1310b22008-01-24 16:13:08 -05002566
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002567 start = page_offset(page);
2568 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002569 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002570
Chris Mason9be33952013-05-17 18:30:14 -04002571 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002572 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002573 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2574 page, start, end,
2575 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002576 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002577 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002578 else
Josef Bacik7870d082017-05-05 11:57:15 -04002579 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2580 failure_tree, tree, start,
2581 page,
2582 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002583 }
Josef Bacikea466792012-03-26 21:57:36 -04002584
Miao Xief2a09da2013-07-25 19:22:33 +08002585 if (likely(uptodate))
2586 goto readpage_ok;
2587
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002588 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002589
2590 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002591 * The generic bio_readpage_error handles errors the
2592 * following way: If possible, new read requests are
2593 * created and submitted and will end up in
2594 * end_bio_extent_readpage as well (if we're lucky,
2595 * not in the !uptodate case). In that case it returns
2596 * 0 and we just go on with the next page in our bio.
2597 * If it can't handle the error it will return -EIO and
2598 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002599 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002600 ret = bio_readpage_error(bio, offset, page, start, end,
2601 mirror);
2602 if (ret == 0) {
2603 uptodate = !bio->bi_status;
2604 offset += len;
2605 continue;
2606 }
2607 } else {
2608 struct extent_buffer *eb;
2609
2610 eb = (struct extent_buffer *)page->private;
2611 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2612 eb->read_mirror = mirror;
2613 atomic_dec(&eb->io_pages);
2614 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2615 &eb->bflags))
2616 btree_readahead_hook(eb, -EIO);
2617
2618 ret = -EIO;
Chris Mason7e383262008-04-09 16:28:12 -04002619 }
Miao Xief2a09da2013-07-25 19:22:33 +08002620readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002621 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002622 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002623 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002624 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002625
2626 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002627 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002628 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002629 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002630 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002631 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002632 ClearPageUptodate(page);
2633 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002634 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002635 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002636 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002637
2638 if (unlikely(!uptodate)) {
2639 if (extent_len) {
2640 endio_readpage_release_extent(tree,
2641 extent_start,
2642 extent_len, 1);
2643 extent_start = 0;
2644 extent_len = 0;
2645 }
2646 endio_readpage_release_extent(tree, start,
2647 end - start + 1, 0);
2648 } else if (!extent_len) {
2649 extent_start = start;
2650 extent_len = end + 1 - start;
2651 } else if (extent_start + extent_len == start) {
2652 extent_len += end + 1 - start;
2653 } else {
2654 endio_readpage_release_extent(tree, extent_start,
2655 extent_len, uptodate);
2656 extent_start = start;
2657 extent_len = end + 1 - start;
2658 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002659 }
Chris Masond1310b22008-01-24 16:13:08 -05002660
Miao Xie883d0de2013-07-25 19:22:35 +08002661 if (extent_len)
2662 endio_readpage_release_extent(tree, extent_start, extent_len,
2663 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002664 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002665 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002666}
2667
Chris Mason9be33952013-05-17 18:30:14 -04002668/*
David Sterba184f9992017-06-12 17:29:39 +02002669 * Initialize the members up to but not including 'bio'. Use after allocating a
2670 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2671 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002672 */
David Sterba184f9992017-06-12 17:29:39 +02002673static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002674{
David Sterba184f9992017-06-12 17:29:39 +02002675 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2676}
2677
2678/*
David Sterba6e707bc2017-06-02 17:26:26 +02002679 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2680 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2681 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002682 */
David Sterbac821e7f32017-06-02 18:35:36 +02002683struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002684{
2685 struct bio *bio;
2686
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002687 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002688 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002689 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002690 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002691 return bio;
2692}
2693
David Sterba8b6c1d52017-06-02 17:48:13 +02002694struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002695{
Miao Xie23ea8e52014-09-12 18:43:54 +08002696 struct btrfs_io_bio *btrfs_bio;
2697 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002698
David Sterba6e707bc2017-06-02 17:26:26 +02002699 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002700 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002701 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002702 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002703 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002704 return new;
2705}
Chris Mason9be33952013-05-17 18:30:14 -04002706
David Sterbac5e4c3d2017-06-12 17:29:41 +02002707struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002708{
Miao Xiefacc8a222013-07-25 19:22:34 +08002709 struct bio *bio;
2710
David Sterba6e707bc2017-06-02 17:26:26 +02002711 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002712 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002713 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002714 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002715}
2716
Liu Boe4770942017-05-16 10:57:14 -07002717struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002718{
2719 struct bio *bio;
2720 struct btrfs_io_bio *btrfs_bio;
2721
2722 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002723 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002724 ASSERT(bio);
2725
2726 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002727 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002728
2729 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002730 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002731 return bio;
2732}
Chris Mason9be33952013-05-17 18:30:14 -04002733
David Sterba4b81ba42017-06-06 19:14:26 +02002734/*
2735 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002736 * @tree: tree so we can call our merge_bio hook
2737 * @wbc: optional writeback control for io accounting
2738 * @page: page to add to the bio
2739 * @pg_offset: offset of the new bio or to check whether we are adding
2740 * a contiguous page to the previous one
2741 * @size: portion of page that we want to write
2742 * @offset: starting offset in the page
2743 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002744 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002745 * @end_io_func: end_io callback for new bio
2746 * @mirror_num: desired mirror to read/write
2747 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2748 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002749 */
2750static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002751 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002752 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002753 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002754 struct block_device *bdev,
2755 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002756 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002757 int mirror_num,
2758 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002759 unsigned long bio_flags,
2760 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002761{
2762 int ret = 0;
2763 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002764 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002765 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002766
David Sterba5c2b1fd2017-06-06 19:22:55 +02002767 ASSERT(bio_ret);
2768
2769 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002770 bool contig;
2771 bool can_merge = true;
2772
Chris Masond1310b22008-01-24 16:13:08 -05002773 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002774 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002775 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002776 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002777 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002778
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002779 ASSERT(tree->ops);
2780 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002781 can_merge = false;
2782
2783 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002784 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002785 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002786 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002787 if (ret < 0) {
2788 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002789 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002790 }
Chris Masond1310b22008-01-24 16:13:08 -05002791 bio = NULL;
2792 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002793 if (wbc)
2794 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002795 return 0;
2796 }
2797 }
Chris Masonc8b97812008-10-29 14:49:59 -04002798
David Sterba6273b7f2017-10-04 17:30:11 +02002799 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002800 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002801 bio->bi_end_io = end_io_func;
2802 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002803 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002804 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002805 if (wbc) {
2806 wbc_init_bio(wbc, bio);
2807 wbc_account_io(wbc, page, page_size);
2808 }
Chris Mason70dec802008-01-29 09:59:12 -05002809
David Sterba5c2b1fd2017-06-06 19:22:55 +02002810 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002811
2812 return ret;
2813}
2814
Eric Sandeen48a3b632013-04-25 20:41:01 +00002815static void attach_extent_buffer_page(struct extent_buffer *eb,
2816 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002817{
2818 if (!PagePrivate(page)) {
2819 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002820 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002821 set_page_private(page, (unsigned long)eb);
2822 } else {
2823 WARN_ON(page->private != (unsigned long)eb);
2824 }
2825}
2826
Chris Masond1310b22008-01-24 16:13:08 -05002827void set_page_extent_mapped(struct page *page)
2828{
2829 if (!PagePrivate(page)) {
2830 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002831 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002832 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002833 }
2834}
2835
Miao Xie125bac012013-07-25 19:22:37 +08002836static struct extent_map *
2837__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2838 u64 start, u64 len, get_extent_t *get_extent,
2839 struct extent_map **em_cached)
2840{
2841 struct extent_map *em;
2842
2843 if (em_cached && *em_cached) {
2844 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002845 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002846 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002847 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002848 return em;
2849 }
2850
2851 free_extent_map(em);
2852 *em_cached = NULL;
2853 }
2854
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002855 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002856 if (em_cached && !IS_ERR_OR_NULL(em)) {
2857 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002858 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002859 *em_cached = em;
2860 }
2861 return em;
2862}
Chris Masond1310b22008-01-24 16:13:08 -05002863/*
2864 * basic readpage implementation. Locked extent state structs are inserted
2865 * into the tree that are removed when the IO is done (by the end_io
2866 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002867 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002868 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002869 */
Miao Xie99740902013-07-25 19:22:36 +08002870static int __do_readpage(struct extent_io_tree *tree,
2871 struct page *page,
2872 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002873 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002874 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002875 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002876 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002877{
2878 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002879 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002880 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002881 u64 cur = start;
2882 u64 extent_offset;
2883 u64 last_byte = i_size_read(inode);
2884 u64 block_start;
2885 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002886 struct extent_map *em;
2887 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002888 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002889 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002890 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002891 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002892 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002893 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002894 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002895
2896 set_page_extent_mapped(page);
2897
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002898 if (!PageUptodate(page)) {
2899 if (cleancache_get_page(page) == 0) {
2900 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002901 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002902 goto out;
2903 }
2904 }
2905
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002906 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002907 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01002908 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04002909
2910 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002911 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002912 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002913 memset(userpage + zero_offset, 0, iosize);
2914 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002915 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002916 }
2917 }
Chris Masond1310b22008-01-24 16:13:08 -05002918 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002919 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002920 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002921
Chris Masond1310b22008-01-24 16:13:08 -05002922 if (cur >= last_byte) {
2923 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002924 struct extent_state *cached = NULL;
2925
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002926 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002927 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002928 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002929 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002930 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002931 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002932 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002933 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002934 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002935 break;
2936 }
Miao Xie125bac012013-07-25 19:22:37 +08002937 em = __get_extent_map(inode, page, pg_offset, cur,
2938 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002939 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002940 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002941 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002942 break;
2943 }
Chris Masond1310b22008-01-24 16:13:08 -05002944 extent_offset = cur - em->start;
2945 BUG_ON(extent_map_end(em) <= cur);
2946 BUG_ON(end < cur);
2947
Li Zefan261507a02010-12-17 14:21:50 +08002948 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002949 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002950 extent_set_compress_type(&this_bio_flag,
2951 em->compress_type);
2952 }
Chris Masonc8b97812008-10-29 14:49:59 -04002953
Chris Masond1310b22008-01-24 16:13:08 -05002954 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2955 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002956 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002957 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2958 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002959 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002960 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002961 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002962 disk_io_size = iosize;
2963 }
Chris Masond1310b22008-01-24 16:13:08 -05002964 bdev = em->bdev;
2965 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002966 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2967 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002968
2969 /*
2970 * If we have a file range that points to a compressed extent
2971 * and it's followed by a consecutive file range that points to
2972 * to the same compressed extent (possibly with a different
2973 * offset and/or length, so it either points to the whole extent
2974 * or only part of it), we must make sure we do not submit a
2975 * single bio to populate the pages for the 2 ranges because
2976 * this makes the compressed extent read zero out the pages
2977 * belonging to the 2nd range. Imagine the following scenario:
2978 *
2979 * File layout
2980 * [0 - 8K] [8K - 24K]
2981 * | |
2982 * | |
2983 * points to extent X, points to extent X,
2984 * offset 4K, length of 8K offset 0, length 16K
2985 *
2986 * [extent X, compressed length = 4K uncompressed length = 16K]
2987 *
2988 * If the bio to read the compressed extent covers both ranges,
2989 * it will decompress extent X into the pages belonging to the
2990 * first range and then it will stop, zeroing out the remaining
2991 * pages that belong to the other range that points to extent X.
2992 * So here we make sure we submit 2 bios, one for the first
2993 * range and another one for the third range. Both will target
2994 * the same physical extent from disk, but we can't currently
2995 * make the compressed bio endio callback populate the pages
2996 * for both ranges because each compressed bio is tightly
2997 * coupled with a single extent map, and each range can have
2998 * an extent map with a different offset value relative to the
2999 * uncompressed data of our extent and different lengths. This
3000 * is a corner case so we prioritize correctness over
3001 * non-optimal behavior (submitting 2 bios for the same extent).
3002 */
3003 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3004 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00003005 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003006 force_bio_submit = true;
3007
3008 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003009 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003010
Chris Masond1310b22008-01-24 16:13:08 -05003011 free_extent_map(em);
3012 em = NULL;
3013
3014 /* we've found a hole, just zero and go on */
3015 if (block_start == EXTENT_MAP_HOLE) {
3016 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003017 struct extent_state *cached = NULL;
3018
Cong Wang7ac687d2011-11-25 23:14:28 +08003019 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003020 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003021 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003022 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003023
3024 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003025 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003026 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003027 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003028 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003029 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003030 continue;
3031 }
3032 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003033 if (test_range_bit(tree, cur, cur_end,
3034 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003035 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003036 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003037 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003038 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003039 continue;
3040 }
Chris Mason70dec802008-01-29 09:59:12 -05003041 /* we have an inline extent but it didn't get marked up
3042 * to date. Error out
3043 */
3044 if (block_start == EXTENT_MAP_INLINE) {
3045 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003046 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003047 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003048 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003049 continue;
3050 }
Chris Masond1310b22008-01-24 16:13:08 -05003051
David Sterba4b81ba42017-06-06 19:14:26 +02003052 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003053 page, offset, disk_io_size,
3054 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003055 end_bio_extent_readpage, mirror_num,
3056 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003057 this_bio_flag,
3058 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003059 if (!ret) {
3060 nr++;
3061 *bio_flags = this_bio_flag;
3062 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003063 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003064 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003065 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003066 }
Chris Masond1310b22008-01-24 16:13:08 -05003067 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003068 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003069 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003070out:
Chris Masond1310b22008-01-24 16:13:08 -05003071 if (!nr) {
3072 if (!PageError(page))
3073 SetPageUptodate(page);
3074 unlock_page(page);
3075 }
Liu Bobaf863b2016-07-11 10:39:07 -07003076 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003077}
3078
Miao Xie99740902013-07-25 19:22:36 +08003079static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3080 struct page *pages[], int nr_pages,
3081 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003082 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003083 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003084 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003085 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003086{
3087 struct inode *inode;
3088 struct btrfs_ordered_extent *ordered;
3089 int index;
3090
3091 inode = pages[0]->mapping->host;
3092 while (1) {
3093 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003094 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003095 end - start + 1);
3096 if (!ordered)
3097 break;
3098 unlock_extent(tree, start, end);
3099 btrfs_start_ordered_extent(inode, ordered, 1);
3100 btrfs_put_ordered_extent(ordered);
3101 }
3102
3103 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003104 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003105 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003106 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003107 }
3108}
3109
3110static void __extent_readpages(struct extent_io_tree *tree,
3111 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003112 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003113 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003114 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003115 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003116{
Stefan Behrens35a36212013-08-14 18:12:25 +02003117 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003118 u64 end = 0;
3119 u64 page_start;
3120 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003121 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003122
3123 for (index = 0; index < nr_pages; index++) {
3124 page_start = page_offset(pages[index]);
3125 if (!end) {
3126 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003127 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003128 first_index = index;
3129 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003130 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003131 } else {
3132 __do_contiguous_readpages(tree, &pages[first_index],
3133 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003134 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003135 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003136 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003137 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003139 first_index = index;
3140 }
3141 }
3142
3143 if (end)
3144 __do_contiguous_readpages(tree, &pages[first_index],
3145 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003146 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003147 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003148}
3149
3150static int __extent_read_full_page(struct extent_io_tree *tree,
3151 struct page *page,
3152 get_extent_t *get_extent,
3153 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003154 unsigned long *bio_flags,
3155 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003156{
3157 struct inode *inode = page->mapping->host;
3158 struct btrfs_ordered_extent *ordered;
3159 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003160 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003161 int ret;
3162
3163 while (1) {
3164 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003165 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003166 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003167 if (!ordered)
3168 break;
3169 unlock_extent(tree, start, end);
3170 btrfs_start_ordered_extent(inode, ordered, 1);
3171 btrfs_put_ordered_extent(ordered);
3172 }
3173
Miao Xie125bac012013-07-25 19:22:37 +08003174 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003175 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003176 return ret;
3177}
3178
Chris Masond1310b22008-01-24 16:13:08 -05003179int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003180 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003181{
3182 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003183 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003184 int ret;
3185
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003186 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003187 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003188 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003189 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003190 return ret;
3191}
Chris Masond1310b22008-01-24 16:13:08 -05003192
David Sterba3d4b9492017-02-10 19:33:41 +01003193static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003194 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003195{
3196 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003197}
3198
Chris Masond1310b22008-01-24 16:13:08 -05003199/*
Chris Mason40f76582014-05-21 13:35:51 -07003200 * helper for __extent_writepage, doing all of the delayed allocation setup.
3201 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003202 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003203 * to write the page (copy into inline extent). In this case the IO has
3204 * been started and the page is already unlocked.
3205 *
3206 * This returns 0 if all went well (page still locked)
3207 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003208 */
Chris Mason40f76582014-05-21 13:35:51 -07003209static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003210 struct page *page, struct writeback_control *wbc,
3211 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003212{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003213 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003214 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003215 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003216 u64 delalloc_to_write = 0;
3217 u64 delalloc_end = 0;
3218 int ret;
3219 int page_started = 0;
3220
Chris Mason40f76582014-05-21 13:35:51 -07003221
3222 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003223 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003224 page,
3225 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003226 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003227 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003228 delalloc_start = delalloc_end + 1;
3229 continue;
3230 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003231 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3232 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003233 /* File system has been set read-only */
3234 if (ret) {
3235 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003236 /*
3237 * btrfs_run_delalloc_range should return < 0 for error
3238 * but just in case, we use > 0 here meaning the IO is
3239 * started, so we don't want to return > 0 unless
3240 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003241 */
3242 ret = ret < 0 ? ret : -EIO;
3243 goto done;
3244 }
3245 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003246 * delalloc_end is already one less than the total length, so
3247 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003248 */
3249 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003250 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003251 delalloc_start = delalloc_end + 1;
3252 }
3253 if (wbc->nr_to_write < delalloc_to_write) {
3254 int thresh = 8192;
3255
3256 if (delalloc_to_write < thresh * 2)
3257 thresh = delalloc_to_write;
3258 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3259 thresh);
3260 }
3261
3262 /* did the fill delalloc function already unlock and start
3263 * the IO?
3264 */
3265 if (page_started) {
3266 /*
3267 * we've unlocked the page, so we can't update
3268 * the mapping's writeback index, just update
3269 * nr_to_write.
3270 */
3271 wbc->nr_to_write -= *nr_written;
3272 return 1;
3273 }
3274
3275 ret = 0;
3276
3277done:
3278 return ret;
3279}
3280
3281/*
3282 * helper for __extent_writepage. This calls the writepage start hooks,
3283 * and does the loop to map the page into extents and bios.
3284 *
3285 * We return 1 if the IO is started and the page is unlocked,
3286 * 0 if all went well (page still locked)
3287 * < 0 if there were errors (page still locked)
3288 */
3289static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3290 struct page *page,
3291 struct writeback_control *wbc,
3292 struct extent_page_data *epd,
3293 loff_t i_size,
3294 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003295 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003296{
Chris Masond1310b22008-01-24 16:13:08 -05003297 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003298 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003299 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003300 u64 end;
3301 u64 cur = start;
3302 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003303 u64 block_start;
3304 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003305 struct extent_map *em;
3306 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003307 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003308 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003309 int ret = 0;
3310 int nr = 0;
3311 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003312
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003313 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3314 if (ret) {
3315 /* Fixup worker will requeue */
3316 if (ret == -EBUSY)
3317 wbc->pages_skipped++;
3318 else
3319 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003320
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003321 update_nr_written(wbc, nr_written);
3322 unlock_page(page);
3323 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003324 }
3325
Chris Mason11c83492009-04-20 15:50:09 -04003326 /*
3327 * we don't want to touch the inode after unlocking the page,
3328 * so we update the mapping writeback index now
3329 */
David Sterba3d4b9492017-02-10 19:33:41 +01003330 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003331
Chris Masond1310b22008-01-24 16:13:08 -05003332 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003333 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003334 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003335 goto done;
3336 }
3337
Chris Masond1310b22008-01-24 16:13:08 -05003338 blocksize = inode->i_sb->s_blocksize;
3339
3340 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003341 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003342 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003343
Chris Mason40f76582014-05-21 13:35:51 -07003344 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003345 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003346 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003347 break;
3348 }
David Sterba3c98c622017-06-23 04:01:08 +02003349 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003350 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003351 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003352 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003353 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003354 break;
3355 }
3356
3357 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003358 em_end = extent_map_end(em);
3359 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003360 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003361 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003362 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003363 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003364 bdev = em->bdev;
3365 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003366 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003367 free_extent_map(em);
3368 em = NULL;
3369
Chris Masonc8b97812008-10-29 14:49:59 -04003370 /*
3371 * compressed and inline extents are written through other
3372 * paths in the FS
3373 */
3374 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003375 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003376 /*
3377 * end_io notification does not happen here for
3378 * compressed extents
3379 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003380 if (!compressed)
3381 btrfs_writepage_endio_finish_ordered(page, cur,
3382 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003383 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003384 else if (compressed) {
3385 /* we don't want to end_page_writeback on
3386 * a compressed extent. this happens
3387 * elsewhere
3388 */
3389 nr++;
3390 }
3391
3392 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003393 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003394 continue;
3395 }
Chris Masonc8b97812008-10-29 14:49:59 -04003396
David Sterba5cdc84b2018-07-18 20:32:52 +02003397 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003398 if (!PageWriteback(page)) {
3399 btrfs_err(BTRFS_I(inode)->root->fs_info,
3400 "page %lu not writeback, cur %llu end %llu",
3401 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003402 }
David Sterba58409ed2016-05-04 11:46:10 +02003403
David Sterba4b81ba42017-06-06 19:14:26 +02003404 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003405 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003406 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003407 end_bio_extent_writepage,
3408 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003409 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003410 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003411 if (PageWriteback(page))
3412 end_page_writeback(page);
3413 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003414
Chris Masond1310b22008-01-24 16:13:08 -05003415 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003416 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003417 nr++;
3418 }
3419done:
Chris Mason40f76582014-05-21 13:35:51 -07003420 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003421 return ret;
3422}
3423
3424/*
3425 * the writepage semantics are similar to regular writepage. extent
3426 * records are inserted to lock ranges in the tree, and as dirty areas
3427 * are found, they are marked writeback. Then the lock bits are removed
3428 * and the end_io handler clears the writeback ranges
3429 */
3430static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003431 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003432{
3433 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003434 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003435 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003436 int ret;
3437 int nr = 0;
3438 size_t pg_offset = 0;
3439 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003440 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003441 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003442 unsigned long nr_written = 0;
3443
Liu Boff40adf2017-08-24 18:19:48 -06003444 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003445
3446 trace___extent_writepage(page, inode, wbc);
3447
3448 WARN_ON(!PageLocked(page));
3449
3450 ClearPageError(page);
3451
Johannes Thumshirn70730172018-12-05 15:23:03 +01003452 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003453 if (page->index > end_index ||
3454 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003455 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003456 unlock_page(page);
3457 return 0;
3458 }
3459
3460 if (page->index == end_index) {
3461 char *userpage;
3462
3463 userpage = kmap_atomic(page);
3464 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003465 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003466 kunmap_atomic(userpage);
3467 flush_dcache_page(page);
3468 }
3469
3470 pg_offset = 0;
3471
3472 set_page_extent_mapped(page);
3473
Nikolay Borisov7789a552018-11-08 10:18:06 +02003474 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003475 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003476 if (ret == 1)
3477 goto done_unlocked;
3478 if (ret)
3479 goto done;
3480 }
Chris Mason40f76582014-05-21 13:35:51 -07003481
3482 ret = __extent_writepage_io(inode, page, wbc, epd,
3483 i_size, nr_written, write_flags, &nr);
3484 if (ret == 1)
3485 goto done_unlocked;
3486
3487done:
Chris Masond1310b22008-01-24 16:13:08 -05003488 if (nr == 0) {
3489 /* make sure the mapping tag for page dirty gets cleared */
3490 set_page_writeback(page);
3491 end_page_writeback(page);
3492 }
Filipe Manana61391d52014-05-09 17:17:40 +01003493 if (PageError(page)) {
3494 ret = ret < 0 ? ret : -EIO;
3495 end_extent_writepage(page, ret, start, page_end);
3496 }
Chris Masond1310b22008-01-24 16:13:08 -05003497 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003498 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003499
Chris Mason11c83492009-04-20 15:50:09 -04003500done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003501 return 0;
3502}
3503
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003504void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003505{
NeilBrown74316202014-07-07 15:16:04 +10003506 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3507 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003508}
3509
Chris Mason0e378df2014-05-19 20:55:27 -07003510static noinline_for_stack int
3511lock_extent_buffer_for_io(struct extent_buffer *eb,
3512 struct btrfs_fs_info *fs_info,
3513 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003514{
David Sterbacc5e31a2018-03-01 18:20:27 +01003515 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003516 int flush = 0;
3517 int ret = 0;
3518
3519 if (!btrfs_try_tree_write_lock(eb)) {
3520 flush = 1;
3521 flush_write_bio(epd);
3522 btrfs_tree_lock(eb);
3523 }
3524
3525 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3526 btrfs_tree_unlock(eb);
3527 if (!epd->sync_io)
3528 return 0;
3529 if (!flush) {
3530 flush_write_bio(epd);
3531 flush = 1;
3532 }
Chris Masona098d8e82012-03-21 12:09:56 -04003533 while (1) {
3534 wait_on_extent_buffer_writeback(eb);
3535 btrfs_tree_lock(eb);
3536 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3537 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003538 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003539 }
3540 }
3541
Josef Bacik51561ff2012-07-20 16:25:24 -04003542 /*
3543 * We need to do this to prevent races in people who check if the eb is
3544 * under IO since we can end up having no IO bits set for a short period
3545 * of time.
3546 */
3547 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003548 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3549 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003550 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003551 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003552 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3553 -eb->len,
3554 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003555 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003556 } else {
3557 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003558 }
3559
3560 btrfs_tree_unlock(eb);
3561
3562 if (!ret)
3563 return ret;
3564
David Sterba65ad0102018-06-29 10:56:49 +02003565 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003566 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003567 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003568
3569 if (!trylock_page(p)) {
3570 if (!flush) {
3571 flush_write_bio(epd);
3572 flush = 1;
3573 }
3574 lock_page(p);
3575 }
3576 }
3577
3578 return ret;
3579}
3580
3581static void end_extent_buffer_writeback(struct extent_buffer *eb)
3582{
3583 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003584 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003585 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3586}
3587
Filipe Manana656f30d2014-09-26 12:25:56 +01003588static void set_btree_ioerr(struct page *page)
3589{
3590 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003591
3592 SetPageError(page);
3593 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3594 return;
3595
3596 /*
3597 * If writeback for a btree extent that doesn't belong to a log tree
3598 * failed, increment the counter transaction->eb_write_errors.
3599 * We do this because while the transaction is running and before it's
3600 * committing (when we call filemap_fdata[write|wait]_range against
3601 * the btree inode), we might have
3602 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3603 * returns an error or an error happens during writeback, when we're
3604 * committing the transaction we wouldn't know about it, since the pages
3605 * can be no longer dirty nor marked anymore for writeback (if a
3606 * subsequent modification to the extent buffer didn't happen before the
3607 * transaction commit), which makes filemap_fdata[write|wait]_range not
3608 * able to find the pages tagged with SetPageError at transaction
3609 * commit time. So if this happens we must abort the transaction,
3610 * otherwise we commit a super block with btree roots that point to
3611 * btree nodes/leafs whose content on disk is invalid - either garbage
3612 * or the content of some node/leaf from a past generation that got
3613 * cowed or deleted and is no longer valid.
3614 *
3615 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3616 * not be enough - we need to distinguish between log tree extents vs
3617 * non-log tree extents, and the next filemap_fdatawait_range() call
3618 * will catch and clear such errors in the mapping - and that call might
3619 * be from a log sync and not from a transaction commit. Also, checking
3620 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3621 * not done and would not be reliable - the eb might have been released
3622 * from memory and reading it back again means that flag would not be
3623 * set (since it's a runtime flag, not persisted on disk).
3624 *
3625 * Using the flags below in the btree inode also makes us achieve the
3626 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3627 * writeback for all dirty pages and before filemap_fdatawait_range()
3628 * is called, the writeback for all dirty pages had already finished
3629 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3630 * filemap_fdatawait_range() would return success, as it could not know
3631 * that writeback errors happened (the pages were no longer tagged for
3632 * writeback).
3633 */
3634 switch (eb->log_index) {
3635 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003636 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003637 break;
3638 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003639 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003640 break;
3641 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003642 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003643 break;
3644 default:
3645 BUG(); /* unexpected, logic error */
3646 }
3647}
3648
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003649static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003650{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003651 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003652 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003653 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003654 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003655
David Sterbac09abff2017-07-13 18:10:07 +02003656 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003657 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003658 struct page *page = bvec->bv_page;
3659
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003660 eb = (struct extent_buffer *)page->private;
3661 BUG_ON(!eb);
3662 done = atomic_dec_and_test(&eb->io_pages);
3663
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003664 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003665 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003666 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003667 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003668 }
3669
3670 end_page_writeback(page);
3671
3672 if (!done)
3673 continue;
3674
3675 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003676 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003677
3678 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679}
3680
Chris Mason0e378df2014-05-19 20:55:27 -07003681static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682 struct btrfs_fs_info *fs_info,
3683 struct writeback_control *wbc,
3684 struct extent_page_data *epd)
3685{
3686 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003687 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003688 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003689 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003690 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003691 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003692 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003693 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003694
Filipe Manana656f30d2014-09-26 12:25:56 +01003695 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003696 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003697 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003698
Liu Bo851cd172016-09-23 13:44:44 -07003699 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3700 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003701 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003702 end = btrfs_node_key_ptr_offset(nritems);
3703
David Sterbab159fa22016-11-08 18:09:03 +01003704 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003705 } else {
3706 /*
3707 * leaf:
3708 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3709 */
3710 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003711 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003712 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003713 }
3714
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003715 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003716 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717
3718 clear_page_dirty_for_io(p);
3719 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003720 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003721 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003722 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003723 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003724 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003725 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003726 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003727 if (PageWriteback(p))
3728 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003729 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3730 end_extent_buffer_writeback(eb);
3731 ret = -EIO;
3732 break;
3733 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003734 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003735 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003736 unlock_page(p);
3737 }
3738
3739 if (unlikely(ret)) {
3740 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003741 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003742 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003743 unlock_page(p);
3744 }
3745 }
3746
3747 return ret;
3748}
3749
3750int btree_write_cache_pages(struct address_space *mapping,
3751 struct writeback_control *wbc)
3752{
3753 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3754 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3755 struct extent_buffer *eb, *prev_eb = NULL;
3756 struct extent_page_data epd = {
3757 .bio = NULL,
3758 .tree = tree,
3759 .extent_locked = 0,
3760 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3761 };
3762 int ret = 0;
3763 int done = 0;
3764 int nr_to_write_done = 0;
3765 struct pagevec pvec;
3766 int nr_pages;
3767 pgoff_t index;
3768 pgoff_t end; /* Inclusive */
3769 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003770 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003771
Mel Gorman86679822017-11-15 17:37:52 -08003772 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003773 if (wbc->range_cyclic) {
3774 index = mapping->writeback_index; /* Start from prev offset */
3775 end = -1;
3776 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003777 index = wbc->range_start >> PAGE_SHIFT;
3778 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003779 scanned = 1;
3780 }
3781 if (wbc->sync_mode == WB_SYNC_ALL)
3782 tag = PAGECACHE_TAG_TOWRITE;
3783 else
3784 tag = PAGECACHE_TAG_DIRTY;
3785retry:
3786 if (wbc->sync_mode == WB_SYNC_ALL)
3787 tag_pages_for_writeback(mapping, index, end);
3788 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003789 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003790 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003791 unsigned i;
3792
3793 scanned = 1;
3794 for (i = 0; i < nr_pages; i++) {
3795 struct page *page = pvec.pages[i];
3796
3797 if (!PagePrivate(page))
3798 continue;
3799
Josef Bacikb5bae262012-09-14 13:43:01 -04003800 spin_lock(&mapping->private_lock);
3801 if (!PagePrivate(page)) {
3802 spin_unlock(&mapping->private_lock);
3803 continue;
3804 }
3805
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003806 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003807
3808 /*
3809 * Shouldn't happen and normally this would be a BUG_ON
3810 * but no sense in crashing the users box for something
3811 * we can survive anyway.
3812 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303813 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003814 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003815 continue;
3816 }
3817
Josef Bacikb5bae262012-09-14 13:43:01 -04003818 if (eb == prev_eb) {
3819 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003820 continue;
3821 }
3822
Josef Bacikb5bae262012-09-14 13:43:01 -04003823 ret = atomic_inc_not_zero(&eb->refs);
3824 spin_unlock(&mapping->private_lock);
3825 if (!ret)
3826 continue;
3827
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003828 prev_eb = eb;
3829 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3830 if (!ret) {
3831 free_extent_buffer(eb);
3832 continue;
3833 }
3834
3835 ret = write_one_eb(eb, fs_info, wbc, &epd);
3836 if (ret) {
3837 done = 1;
3838 free_extent_buffer(eb);
3839 break;
3840 }
3841 free_extent_buffer(eb);
3842
3843 /*
3844 * the filesystem may choose to bump up nr_to_write.
3845 * We have to make sure to honor the new nr_to_write
3846 * at any time
3847 */
3848 nr_to_write_done = wbc->nr_to_write <= 0;
3849 }
3850 pagevec_release(&pvec);
3851 cond_resched();
3852 }
3853 if (!scanned && !done) {
3854 /*
3855 * We hit the last page and there is more work to be done: wrap
3856 * back to the start of the file
3857 */
3858 scanned = 1;
3859 index = 0;
3860 goto retry;
3861 }
3862 flush_write_bio(&epd);
3863 return ret;
3864}
3865
Chris Masond1310b22008-01-24 16:13:08 -05003866/**
Chris Mason4bef0842008-09-08 11:18:08 -04003867 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05003868 * @mapping: address space structure to write
3869 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003870 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003871 *
3872 * If a page is already under I/O, write_cache_pages() skips it, even
3873 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3874 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3875 * and msync() need to guarantee that all the data which was dirty at the time
3876 * the call was made get new I/O started against them. If wbc->sync_mode is
3877 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3878 * existing IO to complete.
3879 */
David Sterba4242b642017-02-10 19:38:24 +01003880static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003881 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003882 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003883{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003884 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003885 int ret = 0;
3886 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003887 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003888 struct pagevec pvec;
3889 int nr_pages;
3890 pgoff_t index;
3891 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003892 pgoff_t done_index;
3893 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003894 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003895 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003896
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003897 /*
3898 * We have to hold onto the inode so that ordered extents can do their
3899 * work when the IO finishes. The alternative to this is failing to add
3900 * an ordered extent if the igrab() fails there and that is a huge pain
3901 * to deal with, so instead just hold onto the inode throughout the
3902 * writepages operation. If it fails here we are freeing up the inode
3903 * anyway and we'd rather not waste our time writing out stuff that is
3904 * going to be truncated anyway.
3905 */
3906 if (!igrab(inode))
3907 return 0;
3908
Mel Gorman86679822017-11-15 17:37:52 -08003909 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003910 if (wbc->range_cyclic) {
3911 index = mapping->writeback_index; /* Start from prev offset */
3912 end = -1;
3913 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003914 index = wbc->range_start >> PAGE_SHIFT;
3915 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003916 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3917 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003918 scanned = 1;
3919 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003920
3921 /*
3922 * We do the tagged writepage as long as the snapshot flush bit is set
3923 * and we are the first one who do the filemap_flush() on this inode.
3924 *
3925 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3926 * not race in and drop the bit.
3927 */
3928 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3929 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3930 &BTRFS_I(inode)->runtime_flags))
3931 wbc->tagged_writepages = 1;
3932
3933 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003934 tag = PAGECACHE_TAG_TOWRITE;
3935 else
3936 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003937retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003938 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003939 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003940 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003941 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003942 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3943 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003944 unsigned i;
3945
3946 scanned = 1;
3947 for (i = 0; i < nr_pages; i++) {
3948 struct page *page = pvec.pages[i];
3949
Liu Boa91326672016-03-07 16:56:21 -08003950 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003951 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003952 * At this point we hold neither the i_pages lock nor
3953 * the page lock: the page may be truncated or
3954 * invalidated (changing page->mapping to NULL),
3955 * or even swizzled back from swapper_space to
3956 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003957 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003958 if (!trylock_page(page)) {
David Sterbaaab6e9e2017-11-30 18:00:02 +01003959 flush_write_bio(epd);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003960 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003961 }
Chris Masond1310b22008-01-24 16:13:08 -05003962
3963 if (unlikely(page->mapping != mapping)) {
3964 unlock_page(page);
3965 continue;
3966 }
3967
Chris Masond2c3f4f2008-11-19 12:44:22 -05003968 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003969 if (PageWriteback(page))
David Sterbaaab6e9e2017-11-30 18:00:02 +01003970 flush_write_bio(epd);
Chris Masond1310b22008-01-24 16:13:08 -05003971 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003972 }
Chris Masond1310b22008-01-24 16:13:08 -05003973
3974 if (PageWriteback(page) ||
3975 !clear_page_dirty_for_io(page)) {
3976 unlock_page(page);
3977 continue;
3978 }
3979
David Sterbaaab6e9e2017-11-30 18:00:02 +01003980 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003981
3982 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3983 unlock_page(page);
3984 ret = 0;
3985 }
Liu Boa91326672016-03-07 16:56:21 -08003986 if (ret < 0) {
3987 /*
3988 * done_index is set past this page,
3989 * so media errors will not choke
3990 * background writeout for the entire
3991 * file. This has consequences for
3992 * range_cyclic semantics (ie. it may
3993 * not be suitable for data integrity
3994 * writeout).
3995 */
3996 done_index = page->index + 1;
3997 done = 1;
3998 break;
3999 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004000
4001 /*
4002 * the filesystem may choose to bump up nr_to_write.
4003 * We have to make sure to honor the new nr_to_write
4004 * at any time
4005 */
4006 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004007 }
4008 pagevec_release(&pvec);
4009 cond_resched();
4010 }
Liu Bo894b36e2016-03-07 16:56:22 -08004011 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004012 /*
4013 * We hit the last page and there is more work to be done: wrap
4014 * back to the start of the file
4015 */
4016 scanned = 1;
4017 index = 0;
4018 goto retry;
4019 }
Liu Boa91326672016-03-07 16:56:21 -08004020
4021 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4022 mapping->writeback_index = done_index;
4023
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004024 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004025 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004026}
Chris Masond1310b22008-01-24 16:13:08 -05004027
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004028int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004029{
4030 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004031 struct extent_page_data epd = {
4032 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004033 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004034 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004035 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004036 };
Chris Masond1310b22008-01-24 16:13:08 -05004037
Chris Masond1310b22008-01-24 16:13:08 -05004038 ret = __extent_writepage(page, wbc, &epd);
4039
David Sterbae2932ee2017-06-23 04:16:17 +02004040 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004041 return ret;
4042}
Chris Masond1310b22008-01-24 16:13:08 -05004043
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004044int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004045 int mode)
4046{
4047 int ret = 0;
4048 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004049 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004050 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004051 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4052 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004053
4054 struct extent_page_data epd = {
4055 .bio = NULL,
4056 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004057 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004058 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004059 };
4060 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004061 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004062 .nr_to_write = nr_pages * 2,
4063 .range_start = start,
4064 .range_end = end + 1,
4065 };
4066
Chris Masond3977122009-01-05 21:25:51 -05004067 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004068 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004069 if (clear_page_dirty_for_io(page))
4070 ret = __extent_writepage(page, &wbc_writepages, &epd);
4071 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004072 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004073 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004074 unlock_page(page);
4075 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004076 put_page(page);
4077 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004078 }
4079
David Sterbae2932ee2017-06-23 04:16:17 +02004080 flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004081 return ret;
4082}
Chris Masond1310b22008-01-24 16:13:08 -05004083
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004084int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004085 struct writeback_control *wbc)
4086{
4087 int ret = 0;
4088 struct extent_page_data epd = {
4089 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004090 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004091 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004092 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004093 };
4094
David Sterba935db852017-06-23 04:30:28 +02004095 ret = extent_write_cache_pages(mapping, wbc, &epd);
David Sterbae2932ee2017-06-23 04:16:17 +02004096 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004097 return ret;
4098}
Chris Masond1310b22008-01-24 16:13:08 -05004099
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004100int extent_readpages(struct address_space *mapping, struct list_head *pages,
4101 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004102{
4103 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004104 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004105 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004106 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004107 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004108 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004109 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004110
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004111 while (!list_empty(pages)) {
4112 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004113 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004114
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004115 prefetchw(&page->flags);
4116 list_del(&page->lru);
4117 if (add_to_page_cache_lru(page, mapping, page->index,
4118 readahead_gfp_mask(mapping))) {
4119 put_page(page);
4120 continue;
4121 }
4122
4123 pagepool[nr++] = page;
Chris Masond1310b22008-01-24 16:13:08 -05004124 }
Liu Bo67c96842012-07-20 21:43:09 -06004125
David Sterbae4d17ef2017-06-23 04:09:57 +02004126 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004127 &bio_flags, &prev_em_start);
Chris Masond1310b22008-01-24 16:13:08 -05004128 }
Liu Bo67c96842012-07-20 21:43:09 -06004129
Miao Xie125bac012013-07-25 19:22:37 +08004130 if (em_cached)
4131 free_extent_map(em_cached);
4132
Chris Masond1310b22008-01-24 16:13:08 -05004133 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004134 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004135 return 0;
4136}
Chris Masond1310b22008-01-24 16:13:08 -05004137
4138/*
4139 * basic invalidatepage code, this waits on any locked or writeback
4140 * ranges corresponding to the page, and then deletes any extent state
4141 * records from the tree
4142 */
4143int extent_invalidatepage(struct extent_io_tree *tree,
4144 struct page *page, unsigned long offset)
4145{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004146 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004147 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004148 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004149 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4150
Qu Wenruofda28322013-02-26 08:10:22 +00004151 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004152 if (start > end)
4153 return 0;
4154
David Sterbaff13db42015-12-03 14:30:40 +01004155 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004156 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004157 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004158 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4159 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004160 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004161 return 0;
4162}
Chris Masond1310b22008-01-24 16:13:08 -05004163
4164/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004165 * a helper for releasepage, this tests for areas of the page that
4166 * are locked or under IO and drops the related state bits if it is safe
4167 * to drop the page.
4168 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004169static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004170 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004171{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004172 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004173 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004174 int ret = 1;
4175
Nikolay Borisov88826792019-03-14 15:28:31 +02004176 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
Chris Mason7b13b7b2008-04-18 10:29:50 -04004177 ret = 0;
Nikolay Borisov88826792019-03-14 15:28:31 +02004178 } else {
Chris Mason11ef1602009-09-23 20:28:46 -04004179 /*
4180 * at this point we can safely clear everything except the
4181 * locked bit and the nodatasum bit
4182 */
David Sterba66b0c882017-10-31 16:30:47 +01004183 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004184 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004185 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004186
4187 /* if clear_extent_bit failed for enomem reasons,
4188 * we can't allow the release to continue.
4189 */
4190 if (ret < 0)
4191 ret = 0;
4192 else
4193 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004194 }
4195 return ret;
4196}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004197
4198/*
Chris Masond1310b22008-01-24 16:13:08 -05004199 * a helper for releasepage. As long as there are no locked extents
4200 * in the range corresponding to the page, both state records and extent
4201 * map records are removed
4202 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004203int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004204{
4205 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004206 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004207 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004208 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4209 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4210 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004211
Mel Gormand0164ad2015-11-06 16:28:21 -08004212 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004213 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004214 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004215 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004216 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004217 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004218 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004219 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004220 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004221 break;
4222 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004223 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4224 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004225 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004226 free_extent_map(em);
4227 break;
4228 }
4229 if (!test_range_bit(tree, em->start,
4230 extent_map_end(em) - 1,
Nikolay Borisov4e586ca2019-03-14 15:28:30 +02004231 EXTENT_LOCKED, 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004232 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4233 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004234 remove_extent_mapping(map, em);
4235 /* once for the rb tree */
4236 free_extent_map(em);
4237 }
4238 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004239 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004240
4241 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004242 free_extent_map(em);
4243 }
Chris Masond1310b22008-01-24 16:13:08 -05004244 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004245 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004246}
Chris Masond1310b22008-01-24 16:13:08 -05004247
Chris Masonec29ed52011-02-23 16:23:20 -05004248/*
4249 * helper function for fiemap, which doesn't want to see any holes.
4250 * This maps until we find something past 'last'
4251 */
4252static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004253 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004254{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004255 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004256 struct extent_map *em;
4257 u64 len;
4258
4259 if (offset >= last)
4260 return NULL;
4261
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304262 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004263 len = last - offset;
4264 if (len == 0)
4265 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004266 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004267 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004268 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004269 return em;
4270
4271 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004272 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004273 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004274
4275 /* this is a hole, advance to the next extent */
4276 offset = extent_map_end(em);
4277 free_extent_map(em);
4278 if (offset >= last)
4279 break;
4280 }
4281 return NULL;
4282}
4283
Qu Wenruo47518322017-04-07 10:43:15 +08004284/*
4285 * To cache previous fiemap extent
4286 *
4287 * Will be used for merging fiemap extent
4288 */
4289struct fiemap_cache {
4290 u64 offset;
4291 u64 phys;
4292 u64 len;
4293 u32 flags;
4294 bool cached;
4295};
4296
4297/*
4298 * Helper to submit fiemap extent.
4299 *
4300 * Will try to merge current fiemap extent specified by @offset, @phys,
4301 * @len and @flags with cached one.
4302 * And only when we fails to merge, cached one will be submitted as
4303 * fiemap extent.
4304 *
4305 * Return value is the same as fiemap_fill_next_extent().
4306 */
4307static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4308 struct fiemap_cache *cache,
4309 u64 offset, u64 phys, u64 len, u32 flags)
4310{
4311 int ret = 0;
4312
4313 if (!cache->cached)
4314 goto assign;
4315
4316 /*
4317 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004318 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004319 * Not recoverable.
4320 *
4321 * NOTE: Physical address can overlap, due to compression
4322 */
4323 if (cache->offset + cache->len > offset) {
4324 WARN_ON(1);
4325 return -EINVAL;
4326 }
4327
4328 /*
4329 * Only merges fiemap extents if
4330 * 1) Their logical addresses are continuous
4331 *
4332 * 2) Their physical addresses are continuous
4333 * So truly compressed (physical size smaller than logical size)
4334 * extents won't get merged with each other
4335 *
4336 * 3) Share same flags except FIEMAP_EXTENT_LAST
4337 * So regular extent won't get merged with prealloc extent
4338 */
4339 if (cache->offset + cache->len == offset &&
4340 cache->phys + cache->len == phys &&
4341 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4342 (flags & ~FIEMAP_EXTENT_LAST)) {
4343 cache->len += len;
4344 cache->flags |= flags;
4345 goto try_submit_last;
4346 }
4347
4348 /* Not mergeable, need to submit cached one */
4349 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4350 cache->len, cache->flags);
4351 cache->cached = false;
4352 if (ret)
4353 return ret;
4354assign:
4355 cache->cached = true;
4356 cache->offset = offset;
4357 cache->phys = phys;
4358 cache->len = len;
4359 cache->flags = flags;
4360try_submit_last:
4361 if (cache->flags & FIEMAP_EXTENT_LAST) {
4362 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4363 cache->phys, cache->len, cache->flags);
4364 cache->cached = false;
4365 }
4366 return ret;
4367}
4368
4369/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004370 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004371 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004372 * The last fiemap cache may still be cached in the following case:
4373 * 0 4k 8k
4374 * |<- Fiemap range ->|
4375 * |<------------ First extent ----------->|
4376 *
4377 * In this case, the first extent range will be cached but not emitted.
4378 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004379 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004380static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4381 struct fiemap_extent_info *fieinfo,
4382 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004383{
4384 int ret;
4385
4386 if (!cache->cached)
4387 return 0;
4388
Qu Wenruo47518322017-04-07 10:43:15 +08004389 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4390 cache->len, cache->flags);
4391 cache->cached = false;
4392 if (ret > 0)
4393 ret = 0;
4394 return ret;
4395}
4396
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004397int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004398 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004399{
Josef Bacik975f84f2010-11-23 19:36:57 +00004400 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004401 u64 off = start;
4402 u64 max = start + len;
4403 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004404 u32 found_type;
4405 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004406 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004407 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004408 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004409 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004410 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004411 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004412 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004413 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004414 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004415 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004416 u64 em_start = 0;
4417 u64 em_len = 0;
4418 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004419
4420 if (len == 0)
4421 return -EINVAL;
4422
Josef Bacik975f84f2010-11-23 19:36:57 +00004423 path = btrfs_alloc_path();
4424 if (!path)
4425 return -ENOMEM;
4426 path->leave_spinning = 1;
4427
Jeff Mahoneyda170662016-06-15 09:22:56 -04004428 start = round_down(start, btrfs_inode_sectorsize(inode));
4429 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004430
Chris Masonec29ed52011-02-23 16:23:20 -05004431 /*
4432 * lookup the last file extent. We're not using i_size here
4433 * because there might be preallocation past i_size
4434 */
David Sterbaf85b7372017-01-20 14:54:07 +01004435 ret = btrfs_lookup_file_extent(NULL, root, path,
4436 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004437 if (ret < 0) {
4438 btrfs_free_path(path);
4439 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004440 } else {
4441 WARN_ON(!ret);
4442 if (ret == 1)
4443 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004444 }
Liu Bo2d324f52016-05-17 17:21:48 -07004445
Josef Bacik975f84f2010-11-23 19:36:57 +00004446 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004447 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004448 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004449
Chris Masonec29ed52011-02-23 16:23:20 -05004450 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004451 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004452 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004453 /* have to trust i_size as the end */
4454 last = (u64)-1;
4455 last_for_get_extent = isize;
4456 } else {
4457 /*
4458 * remember the start of the last extent. There are a
4459 * bunch of different factors that go into the length of the
4460 * extent, so its much less complex to remember where it started
4461 */
4462 last = found_key.offset;
4463 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004464 }
Liu Bofe09e162013-09-22 12:54:23 +08004465 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004466
Chris Masonec29ed52011-02-23 16:23:20 -05004467 /*
4468 * we might have some extents allocated but more delalloc past those
4469 * extents. so, we trust isize unless the start of the last extent is
4470 * beyond isize
4471 */
4472 if (last < isize) {
4473 last = (u64)-1;
4474 last_for_get_extent = isize;
4475 }
4476
David Sterbaff13db42015-12-03 14:30:40 +01004477 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004478 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004479
David Sterbae3350e12017-06-23 04:09:57 +02004480 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004481 if (!em)
4482 goto out;
4483 if (IS_ERR(em)) {
4484 ret = PTR_ERR(em);
4485 goto out;
4486 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004487
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004488 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004489 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004490
Chris Masonea8efc72011-03-08 11:54:40 -05004491 /* break if the extent we found is outside the range */
4492 if (em->start >= max || extent_map_end(em) < off)
4493 break;
4494
4495 /*
4496 * get_extent may return an extent that starts before our
4497 * requested range. We have to make sure the ranges
4498 * we return to fiemap always move forward and don't
4499 * overlap, so adjust the offsets here
4500 */
4501 em_start = max(em->start, off);
4502
4503 /*
4504 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004505 * for adjusting the disk offset below. Only do this if the
4506 * extent isn't compressed since our in ram offset may be past
4507 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004508 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004509 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4510 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004511 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004512 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004513 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004514 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4515 disko = em->block_start + offset_in_extent;
4516 else
4517 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004518
Chris Masonea8efc72011-03-08 11:54:40 -05004519 /*
4520 * bump off for our next call to get_extent
4521 */
4522 off = extent_map_end(em);
4523 if (off >= max)
4524 end = 1;
4525
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004526 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004527 end = 1;
4528 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004529 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004530 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4531 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004532 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004533 flags |= (FIEMAP_EXTENT_DELALLOC |
4534 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004535 } else if (fieinfo->fi_extents_max) {
4536 u64 bytenr = em->block_start -
4537 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004538
Liu Bofe09e162013-09-22 12:54:23 +08004539 /*
4540 * As btrfs supports shared space, this information
4541 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004542 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4543 * then we're just getting a count and we can skip the
4544 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004545 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004546 ret = btrfs_check_shared(root,
4547 btrfs_ino(BTRFS_I(inode)),
4548 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004549 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004550 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004551 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004552 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004553 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004554 }
4555 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4556 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004557 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4558 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004559
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004560 free_extent_map(em);
4561 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004562 if ((em_start >= last) || em_len == (u64)-1 ||
4563 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004564 flags |= FIEMAP_EXTENT_LAST;
4565 end = 1;
4566 }
4567
Chris Masonec29ed52011-02-23 16:23:20 -05004568 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004569 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004570 if (IS_ERR(em)) {
4571 ret = PTR_ERR(em);
4572 goto out;
4573 }
4574 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004575 flags |= FIEMAP_EXTENT_LAST;
4576 end = 1;
4577 }
Qu Wenruo47518322017-04-07 10:43:15 +08004578 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4579 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004580 if (ret) {
4581 if (ret == 1)
4582 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004583 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004584 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004585 }
4586out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004587 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004588 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 free_extent_map(em);
4590out:
Liu Bofe09e162013-09-22 12:54:23 +08004591 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004592 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004593 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004594 return ret;
4595}
4596
Chris Mason727011e2010-08-06 13:21:20 -04004597static void __free_extent_buffer(struct extent_buffer *eb)
4598{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004599 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004600 kmem_cache_free(extent_buffer_cache, eb);
4601}
4602
Josef Bacika26e8c92014-03-28 17:07:27 -04004603int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004604{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004605 return (atomic_read(&eb->io_pages) ||
4606 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4607 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004608}
4609
Miao Xie897ca6e92010-10-26 20:57:29 -04004610/*
David Sterba55ac0132018-07-19 17:24:32 +02004611 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004612 */
David Sterba55ac0132018-07-19 17:24:32 +02004613static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004614{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004615 int i;
4616 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004617 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004618
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004619 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004620
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004621 num_pages = num_extent_pages(eb);
4622 for (i = 0; i < num_pages; i++) {
4623 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004624
Forrest Liu5d2361d2015-02-09 17:31:45 +08004625 if (!page)
4626 continue;
4627 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004628 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004629 /*
4630 * We do this since we'll remove the pages after we've
4631 * removed the eb from the radix tree, so we could race
4632 * and have this page now attached to the new eb. So
4633 * only clear page_private if it's still connected to
4634 * this eb.
4635 */
4636 if (PagePrivate(page) &&
4637 page->private == (unsigned long)eb) {
4638 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4639 BUG_ON(PageDirty(page));
4640 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004641 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004642 * We need to make sure we haven't be attached
4643 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004644 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004645 ClearPagePrivate(page);
4646 set_page_private(page, 0);
4647 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004648 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004649 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004650
4651 if (mapped)
4652 spin_unlock(&page->mapping->private_lock);
4653
Nicholas D Steeves01327612016-05-19 21:18:45 -04004654 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004655 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004656 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004657}
4658
4659/*
4660 * Helper for releasing the extent buffer.
4661 */
4662static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4663{
David Sterba55ac0132018-07-19 17:24:32 +02004664 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004665 __free_extent_buffer(eb);
4666}
4667
Josef Bacikf28491e2013-12-16 13:24:27 -05004668static struct extent_buffer *
4669__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004670 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004671{
4672 struct extent_buffer *eb = NULL;
4673
Michal Hockod1b5c562015-08-19 14:17:40 +02004674 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004675 eb->start = start;
4676 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004677 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004678 eb->bflags = 0;
4679 rwlock_init(&eb->lock);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004680 atomic_set(&eb->blocking_readers, 0);
4681 atomic_set(&eb->blocking_writers, 0);
David Sterbaed1b4ed2018-08-24 16:31:17 +02004682 eb->lock_nested = false;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004683 init_waitqueue_head(&eb->write_lock_wq);
4684 init_waitqueue_head(&eb->read_lock_wq);
4685
4686 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4687
4688 spin_lock_init(&eb->refs_lock);
4689 atomic_set(&eb->refs, 1);
4690 atomic_set(&eb->io_pages, 0);
4691
4692 /*
4693 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4694 */
4695 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4696 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4697 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4698
David Sterba843ccf92018-08-24 14:56:28 +02004699#ifdef CONFIG_BTRFS_DEBUG
4700 atomic_set(&eb->spinning_writers, 0);
David Sterbaafd495a2018-08-24 15:57:38 +02004701 atomic_set(&eb->spinning_readers, 0);
David Sterba5c9c7992018-08-24 16:15:51 +02004702 atomic_set(&eb->read_locks, 0);
David Sterbac79adfc2018-08-24 16:24:26 +02004703 atomic_set(&eb->write_locks, 0);
David Sterba843ccf92018-08-24 14:56:28 +02004704#endif
4705
Josef Bacikdb7f3432013-08-07 14:54:37 -04004706 return eb;
4707}
4708
4709struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4710{
David Sterbacc5e31a2018-03-01 18:20:27 +01004711 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004712 struct page *p;
4713 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004714 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004715
David Sterba3f556f72014-06-15 03:20:26 +02004716 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004717 if (new == NULL)
4718 return NULL;
4719
4720 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004721 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004722 if (!p) {
4723 btrfs_release_extent_buffer(new);
4724 return NULL;
4725 }
4726 attach_extent_buffer_page(new, p);
4727 WARN_ON(PageDirty(p));
4728 SetPageUptodate(p);
4729 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004730 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004731 }
4732
Josef Bacikdb7f3432013-08-07 14:54:37 -04004733 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004734 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004735
4736 return new;
4737}
4738
Omar Sandoval0f331222015-09-29 20:50:31 -07004739struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4740 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004741{
4742 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004743 int num_pages;
4744 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004745
David Sterba3f556f72014-06-15 03:20:26 +02004746 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004747 if (!eb)
4748 return NULL;
4749
David Sterba65ad0102018-06-29 10:56:49 +02004750 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004751 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004752 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004753 if (!eb->pages[i])
4754 goto err;
4755 }
4756 set_extent_buffer_uptodate(eb);
4757 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004758 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004759
4760 return eb;
4761err:
4762 for (; i > 0; i--)
4763 __free_page(eb->pages[i - 1]);
4764 __free_extent_buffer(eb);
4765 return NULL;
4766}
4767
Omar Sandoval0f331222015-09-29 20:50:31 -07004768struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004769 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004770{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004771 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004772}
4773
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004774static void check_buffer_tree_ref(struct extent_buffer *eb)
4775{
Chris Mason242e18c2013-01-29 17:49:37 -05004776 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004777 /* the ref bit is tricky. We have to make sure it is set
4778 * if we have the buffer dirty. Otherwise the
4779 * code to free a buffer can end up dropping a dirty
4780 * page
4781 *
4782 * Once the ref bit is set, it won't go away while the
4783 * buffer is dirty or in writeback, and it also won't
4784 * go away while we have the reference count on the
4785 * eb bumped.
4786 *
4787 * We can't just set the ref bit without bumping the
4788 * ref on the eb because free_extent_buffer might
4789 * see the ref bit and try to clear it. If this happens
4790 * free_extent_buffer might end up dropping our original
4791 * ref by mistake and freeing the page before we are able
4792 * to add one more ref.
4793 *
4794 * So bump the ref count first, then set the bit. If someone
4795 * beat us to it, drop the ref we added.
4796 */
Chris Mason242e18c2013-01-29 17:49:37 -05004797 refs = atomic_read(&eb->refs);
4798 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4799 return;
4800
Josef Bacik594831c2012-07-20 16:11:08 -04004801 spin_lock(&eb->refs_lock);
4802 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004803 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004804 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004805}
4806
Mel Gorman2457aec2014-06-04 16:10:31 -07004807static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4808 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004809{
David Sterbacc5e31a2018-03-01 18:20:27 +01004810 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004811
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004812 check_buffer_tree_ref(eb);
4813
David Sterba65ad0102018-06-29 10:56:49 +02004814 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004815 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004816 struct page *p = eb->pages[i];
4817
Mel Gorman2457aec2014-06-04 16:10:31 -07004818 if (p != accessed)
4819 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004820 }
4821}
4822
Josef Bacikf28491e2013-12-16 13:24:27 -05004823struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4824 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004825{
4826 struct extent_buffer *eb;
4827
4828 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004829 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004830 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004831 if (eb && atomic_inc_not_zero(&eb->refs)) {
4832 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004833 /*
4834 * Lock our eb's refs_lock to avoid races with
4835 * free_extent_buffer. When we get our eb it might be flagged
4836 * with EXTENT_BUFFER_STALE and another task running
4837 * free_extent_buffer might have seen that flag set,
4838 * eb->refs == 2, that the buffer isn't under IO (dirty and
4839 * writeback flags not set) and it's still in the tree (flag
4840 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4841 * of decrementing the extent buffer's reference count twice.
4842 * So here we could race and increment the eb's reference count,
4843 * clear its stale flag, mark it as dirty and drop our reference
4844 * before the other task finishes executing free_extent_buffer,
4845 * which would later result in an attempt to free an extent
4846 * buffer that is dirty.
4847 */
4848 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4849 spin_lock(&eb->refs_lock);
4850 spin_unlock(&eb->refs_lock);
4851 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004852 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004853 return eb;
4854 }
4855 rcu_read_unlock();
4856
4857 return NULL;
4858}
4859
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004860#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4861struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004862 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004863{
4864 struct extent_buffer *eb, *exists = NULL;
4865 int ret;
4866
4867 eb = find_extent_buffer(fs_info, start);
4868 if (eb)
4869 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004870 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004871 if (!eb)
4872 return NULL;
4873 eb->fs_info = fs_info;
4874again:
David Sterbae1860a72016-05-09 14:11:38 +02004875 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004876 if (ret)
4877 goto free_eb;
4878 spin_lock(&fs_info->buffer_lock);
4879 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004880 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004881 spin_unlock(&fs_info->buffer_lock);
4882 radix_tree_preload_end();
4883 if (ret == -EEXIST) {
4884 exists = find_extent_buffer(fs_info, start);
4885 if (exists)
4886 goto free_eb;
4887 else
4888 goto again;
4889 }
4890 check_buffer_tree_ref(eb);
4891 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4892
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004893 return eb;
4894free_eb:
4895 btrfs_release_extent_buffer(eb);
4896 return exists;
4897}
4898#endif
4899
Josef Bacikf28491e2013-12-16 13:24:27 -05004900struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004901 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004902{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004903 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004904 int num_pages;
4905 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004906 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004907 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004908 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004909 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004910 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004911 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004912 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004913
Jeff Mahoneyda170662016-06-15 09:22:56 -04004914 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004915 btrfs_err(fs_info, "bad tree block start %llu", start);
4916 return ERR_PTR(-EINVAL);
4917 }
4918
Josef Bacikf28491e2013-12-16 13:24:27 -05004919 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004920 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004921 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004922
David Sterba23d79d82014-06-15 02:55:29 +02004923 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004924 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004925 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004926
David Sterba65ad0102018-06-29 10:56:49 +02004927 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004928 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004929 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004930 if (!p) {
4931 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004932 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004933 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004934
4935 spin_lock(&mapping->private_lock);
4936 if (PagePrivate(p)) {
4937 /*
4938 * We could have already allocated an eb for this page
4939 * and attached one so lets see if we can get a ref on
4940 * the existing eb, and if we can we know it's good and
4941 * we can just return that one, else we know we can just
4942 * overwrite page->private.
4943 */
4944 exists = (struct extent_buffer *)p->private;
4945 if (atomic_inc_not_zero(&exists->refs)) {
4946 spin_unlock(&mapping->private_lock);
4947 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004948 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004949 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004950 goto free_eb;
4951 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004952 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004953
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004954 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004955 * Do this so attach doesn't complain and we need to
4956 * drop the ref the old guy had.
4957 */
4958 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004959 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004960 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004961 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004962 attach_extent_buffer_page(eb, p);
4963 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004964 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004965 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004966 if (!PageUptodate(p))
4967 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004968
4969 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004970 * We can't unlock the pages just yet since the extent buffer
4971 * hasn't been properly inserted in the radix tree, this
4972 * opens a race with btree_releasepage which can free a page
4973 * while we are still filling in all pages for the buffer and
4974 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05004975 */
Chris Masond1310b22008-01-24 16:13:08 -05004976 }
4977 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004978 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004979again:
David Sterbae1860a72016-05-09 14:11:38 +02004980 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004981 if (ret) {
4982 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004983 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004984 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004985
Josef Bacikf28491e2013-12-16 13:24:27 -05004986 spin_lock(&fs_info->buffer_lock);
4987 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004988 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05004989 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004990 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004991 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004992 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004993 if (exists)
4994 goto free_eb;
4995 else
Josef Bacik115391d2012-03-09 09:51:43 -05004996 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004997 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004998 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004999 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005000 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005001
5002 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005003 * Now it's safe to unlock the pages because any calls to
5004 * btree_releasepage will correctly detect that a page belongs to a
5005 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005006 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005007 for (i = 0; i < num_pages; i++)
5008 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005009 return eb;
5010
Chris Mason6af118ce2008-07-22 11:18:07 -04005011free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005012 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005013 for (i = 0; i < num_pages; i++) {
5014 if (eb->pages[i])
5015 unlock_page(eb->pages[i]);
5016 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005017
Miao Xie897ca6e92010-10-26 20:57:29 -04005018 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005019 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005020}
Chris Masond1310b22008-01-24 16:13:08 -05005021
Josef Bacik3083ee22012-03-09 16:01:49 -05005022static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5023{
5024 struct extent_buffer *eb =
5025 container_of(head, struct extent_buffer, rcu_head);
5026
5027 __free_extent_buffer(eb);
5028}
5029
David Sterbaf7a52a42013-04-26 14:56:29 +00005030static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005031{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005032 lockdep_assert_held(&eb->refs_lock);
5033
Josef Bacik3083ee22012-03-09 16:01:49 -05005034 WARN_ON(atomic_read(&eb->refs) == 0);
5035 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005036 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005037 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005038
Jan Schmidt815a51c2012-05-16 17:00:02 +02005039 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005040
Josef Bacikf28491e2013-12-16 13:24:27 -05005041 spin_lock(&fs_info->buffer_lock);
5042 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005043 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005044 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005045 } else {
5046 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005047 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005048
5049 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005050 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005051#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005052 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005053 __free_extent_buffer(eb);
5054 return 1;
5055 }
5056#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005057 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005058 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005059 }
5060 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005061
5062 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005063}
5064
Chris Masond1310b22008-01-24 16:13:08 -05005065void free_extent_buffer(struct extent_buffer *eb)
5066{
Chris Mason242e18c2013-01-29 17:49:37 -05005067 int refs;
5068 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005069 if (!eb)
5070 return;
5071
Chris Mason242e18c2013-01-29 17:49:37 -05005072 while (1) {
5073 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005074 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5075 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5076 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005077 break;
5078 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5079 if (old == refs)
5080 return;
5081 }
5082
Josef Bacik3083ee22012-03-09 16:01:49 -05005083 spin_lock(&eb->refs_lock);
5084 if (atomic_read(&eb->refs) == 2 &&
5085 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005086 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005087 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5088 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005089
Josef Bacik3083ee22012-03-09 16:01:49 -05005090 /*
5091 * I know this is terrible, but it's temporary until we stop tracking
5092 * the uptodate bits and such for the extent buffers.
5093 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005094 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005095}
Chris Masond1310b22008-01-24 16:13:08 -05005096
Josef Bacik3083ee22012-03-09 16:01:49 -05005097void free_extent_buffer_stale(struct extent_buffer *eb)
5098{
5099 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005100 return;
5101
Josef Bacik3083ee22012-03-09 16:01:49 -05005102 spin_lock(&eb->refs_lock);
5103 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5104
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005105 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005106 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5107 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005108 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005109}
5110
Chris Mason1d4284b2012-03-28 20:31:37 -04005111void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005112{
David Sterbacc5e31a2018-03-01 18:20:27 +01005113 int i;
5114 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005115 struct page *page;
5116
David Sterba65ad0102018-06-29 10:56:49 +02005117 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005118
5119 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005120 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005121 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005122 continue;
5123
Chris Masona61e6f22008-07-22 11:18:08 -04005124 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005125 WARN_ON(!PagePrivate(page));
5126
Chris Masond1310b22008-01-24 16:13:08 -05005127 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005128 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005129 if (!PageDirty(page))
5130 __xa_clear_mark(&page->mapping->i_pages,
5131 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005132 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005133 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005134 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005135 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005136 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005137}
Chris Masond1310b22008-01-24 16:13:08 -05005138
Liu Boabb57ef2018-09-14 01:44:42 +08005139bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005140{
David Sterbacc5e31a2018-03-01 18:20:27 +01005141 int i;
5142 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005143 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005144
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005145 check_buffer_tree_ref(eb);
5146
Chris Masonb9473432009-03-13 11:00:37 -04005147 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005148
David Sterba65ad0102018-06-29 10:56:49 +02005149 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005150 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005151 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5152
Liu Boabb57ef2018-09-14 01:44:42 +08005153 if (!was_dirty)
5154 for (i = 0; i < num_pages; i++)
5155 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005156
5157#ifdef CONFIG_BTRFS_DEBUG
5158 for (i = 0; i < num_pages; i++)
5159 ASSERT(PageDirty(eb->pages[i]));
5160#endif
5161
Chris Masonb9473432009-03-13 11:00:37 -04005162 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005163}
Chris Masond1310b22008-01-24 16:13:08 -05005164
David Sterba69ba3922015-12-03 13:08:59 +01005165void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005166{
David Sterbacc5e31a2018-03-01 18:20:27 +01005167 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005168 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005169 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005170
Chris Masonb4ce94d2009-02-04 09:25:08 -05005171 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005172 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005173 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005174 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005175 if (page)
5176 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005177 }
Chris Mason1259ab72008-05-12 13:39:03 -04005178}
5179
David Sterba09c25a82015-12-03 13:08:59 +01005180void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005181{
David Sterbacc5e31a2018-03-01 18:20:27 +01005182 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005183 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005184 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005185
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005186 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005187 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005188 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005189 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005190 SetPageUptodate(page);
5191 }
Chris Masond1310b22008-01-24 16:13:08 -05005192}
Chris Masond1310b22008-01-24 16:13:08 -05005193
Chris Masond1310b22008-01-24 16:13:08 -05005194int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005195 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005196{
David Sterbacc5e31a2018-03-01 18:20:27 +01005197 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005198 struct page *page;
5199 int err;
5200 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005201 int locked_pages = 0;
5202 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005203 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005204 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005205 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005206 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005207
Chris Masonb4ce94d2009-02-04 09:25:08 -05005208 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005209 return 0;
5210
David Sterba65ad0102018-06-29 10:56:49 +02005211 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005212 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005213 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005214 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005215 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005216 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005217 } else {
5218 lock_page(page);
5219 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005220 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005221 }
5222 /*
5223 * We need to firstly lock all pages to make sure that
5224 * the uptodate bit of our pages won't be affected by
5225 * clear_extent_buffer_uptodate().
5226 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005227 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005228 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005229 if (!PageUptodate(page)) {
5230 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005231 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005232 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005233 }
Liu Bo2571e732016-08-03 12:33:01 -07005234
Chris Masonce9adaa2008-04-09 16:28:12 -04005235 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005236 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005237 goto unlock_exit;
5238 }
5239
Filipe Manana656f30d2014-09-26 12:25:56 +01005240 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005241 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005242 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005243 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005244 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005245
Chris Masonce9adaa2008-04-09 16:28:12 -04005246 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005247 if (ret) {
5248 atomic_dec(&eb->io_pages);
5249 unlock_page(page);
5250 continue;
5251 }
5252
Chris Masonf1885912008-04-09 16:28:12 -04005253 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005254 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005255 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005256 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005257 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005258 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005259 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005260 /*
5261 * We use &bio in above __extent_read_full_page,
5262 * so we ensure that if it returns error, the
5263 * current page fails to add itself to bio and
5264 * it's been unlocked.
5265 *
5266 * We must dec io_pages by ourselves.
5267 */
5268 atomic_dec(&eb->io_pages);
5269 }
Chris Masond1310b22008-01-24 16:13:08 -05005270 } else {
5271 unlock_page(page);
5272 }
5273 }
5274
Jeff Mahoney355808c2011-10-03 23:23:14 -04005275 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005276 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005277 if (err)
5278 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005279 }
Chris Masona86c12c2008-02-07 10:50:54 -05005280
Arne Jansenbb82ab82011-06-10 14:06:53 +02005281 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005282 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005283
Josef Bacik8436ea912016-09-02 15:40:03 -04005284 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005285 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005286 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005287 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005288 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005289 }
Chris Masond3977122009-01-05 21:25:51 -05005290
Chris Masond1310b22008-01-24 16:13:08 -05005291 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005292
5293unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005294 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005295 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005296 page = eb->pages[locked_pages];
5297 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005298 }
5299 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005300}
Chris Masond1310b22008-01-24 16:13:08 -05005301
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005302void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5303 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005304{
5305 size_t cur;
5306 size_t offset;
5307 struct page *page;
5308 char *kaddr;
5309 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005310 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005311 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005312
Liu Bof716abd2017-08-09 11:10:16 -06005313 if (start + len > eb->len) {
5314 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5315 eb->start, eb->len, start, len);
5316 memset(dst, 0, len);
5317 return;
5318 }
Chris Masond1310b22008-01-24 16:13:08 -05005319
Johannes Thumshirn70730172018-12-05 15:23:03 +01005320 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005321
Chris Masond3977122009-01-05 21:25:51 -05005322 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005323 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005324
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005325 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005326 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005327 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005328
5329 dst += cur;
5330 len -= cur;
5331 offset = 0;
5332 i++;
5333 }
5334}
Chris Masond1310b22008-01-24 16:13:08 -05005335
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005336int read_extent_buffer_to_user(const struct extent_buffer *eb,
5337 void __user *dstv,
5338 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005339{
5340 size_t cur;
5341 size_t offset;
5342 struct page *page;
5343 char *kaddr;
5344 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005345 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005346 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005347 int ret = 0;
5348
5349 WARN_ON(start > eb->len);
5350 WARN_ON(start + len > eb->start + eb->len);
5351
Johannes Thumshirn70730172018-12-05 15:23:03 +01005352 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005353
5354 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005355 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005356
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005357 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005358 kaddr = page_address(page);
5359 if (copy_to_user(dst, kaddr + offset, cur)) {
5360 ret = -EFAULT;
5361 break;
5362 }
5363
5364 dst += cur;
5365 len -= cur;
5366 offset = 0;
5367 i++;
5368 }
5369
5370 return ret;
5371}
5372
Liu Bo415b35a2016-06-17 19:16:21 -07005373/*
5374 * return 0 if the item is found within a page.
5375 * return 1 if the item spans two pages.
5376 * return -EINVAL otherwise.
5377 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005378int map_private_extent_buffer(const struct extent_buffer *eb,
5379 unsigned long start, unsigned long min_len,
5380 char **map, unsigned long *map_start,
5381 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005382{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005383 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005384 char *kaddr;
5385 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005386 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005387 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005388 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005389 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005390
Liu Bof716abd2017-08-09 11:10:16 -06005391 if (start + min_len > eb->len) {
5392 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5393 eb->start, eb->len, start, min_len);
5394 return -EINVAL;
5395 }
5396
Chris Masond1310b22008-01-24 16:13:08 -05005397 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005398 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005399
5400 if (i == 0) {
5401 offset = start_offset;
5402 *map_start = 0;
5403 } else {
5404 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005405 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005406 }
Chris Masond3977122009-01-05 21:25:51 -05005407
David Sterbafb85fc92014-07-31 01:03:53 +02005408 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005409 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005410 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005411 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005412 return 0;
5413}
Chris Masond1310b22008-01-24 16:13:08 -05005414
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005415int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5416 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005417{
5418 size_t cur;
5419 size_t offset;
5420 struct page *page;
5421 char *kaddr;
5422 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005423 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005424 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005425 int ret = 0;
5426
5427 WARN_ON(start > eb->len);
5428 WARN_ON(start + len > eb->start + eb->len);
5429
Johannes Thumshirn70730172018-12-05 15:23:03 +01005430 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005431
Chris Masond3977122009-01-05 21:25:51 -05005432 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005433 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005434
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005435 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005436
Chris Masona6591712011-07-19 12:04:14 -04005437 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005438 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005439 if (ret)
5440 break;
5441
5442 ptr += cur;
5443 len -= cur;
5444 offset = 0;
5445 i++;
5446 }
5447 return ret;
5448}
Chris Masond1310b22008-01-24 16:13:08 -05005449
David Sterbaf157bf72016-11-09 17:43:38 +01005450void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5451 const void *srcv)
5452{
5453 char *kaddr;
5454
5455 WARN_ON(!PageUptodate(eb->pages[0]));
5456 kaddr = page_address(eb->pages[0]);
5457 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5458 BTRFS_FSID_SIZE);
5459}
5460
5461void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5462{
5463 char *kaddr;
5464
5465 WARN_ON(!PageUptodate(eb->pages[0]));
5466 kaddr = page_address(eb->pages[0]);
5467 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5468 BTRFS_FSID_SIZE);
5469}
5470
Chris Masond1310b22008-01-24 16:13:08 -05005471void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5472 unsigned long start, unsigned long len)
5473{
5474 size_t cur;
5475 size_t offset;
5476 struct page *page;
5477 char *kaddr;
5478 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005479 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005480 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005481
5482 WARN_ON(start > eb->len);
5483 WARN_ON(start + len > eb->start + eb->len);
5484
Johannes Thumshirn70730172018-12-05 15:23:03 +01005485 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005486
Chris Masond3977122009-01-05 21:25:51 -05005487 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005488 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005489 WARN_ON(!PageUptodate(page));
5490
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005491 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005492 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005493 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005494
5495 src += cur;
5496 len -= cur;
5497 offset = 0;
5498 i++;
5499 }
5500}
Chris Masond1310b22008-01-24 16:13:08 -05005501
David Sterbab159fa22016-11-08 18:09:03 +01005502void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5503 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005504{
5505 size_t cur;
5506 size_t offset;
5507 struct page *page;
5508 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005509 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005510 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005511
5512 WARN_ON(start > eb->len);
5513 WARN_ON(start + len > eb->start + eb->len);
5514
Johannes Thumshirn70730172018-12-05 15:23:03 +01005515 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005516
Chris Masond3977122009-01-05 21:25:51 -05005517 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005518 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005519 WARN_ON(!PageUptodate(page));
5520
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005521 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005522 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005523 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005524
5525 len -= cur;
5526 offset = 0;
5527 i++;
5528 }
5529}
Chris Masond1310b22008-01-24 16:13:08 -05005530
David Sterba58e80122016-11-08 18:30:31 +01005531void copy_extent_buffer_full(struct extent_buffer *dst,
5532 struct extent_buffer *src)
5533{
5534 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005535 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005536
5537 ASSERT(dst->len == src->len);
5538
David Sterba65ad0102018-06-29 10:56:49 +02005539 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005540 for (i = 0; i < num_pages; i++)
5541 copy_page(page_address(dst->pages[i]),
5542 page_address(src->pages[i]));
5543}
5544
Chris Masond1310b22008-01-24 16:13:08 -05005545void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5546 unsigned long dst_offset, unsigned long src_offset,
5547 unsigned long len)
5548{
5549 u64 dst_len = dst->len;
5550 size_t cur;
5551 size_t offset;
5552 struct page *page;
5553 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005554 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005555 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005556
5557 WARN_ON(src->len != dst_len);
5558
Johannes Thumshirn70730172018-12-05 15:23:03 +01005559 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005560
Chris Masond3977122009-01-05 21:25:51 -05005561 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005562 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005563 WARN_ON(!PageUptodate(page));
5564
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005565 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005566
Chris Masona6591712011-07-19 12:04:14 -04005567 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005568 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005569
5570 src_offset += cur;
5571 len -= cur;
5572 offset = 0;
5573 i++;
5574 }
5575}
Chris Masond1310b22008-01-24 16:13:08 -05005576
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005577/*
5578 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5579 * given bit number
5580 * @eb: the extent buffer
5581 * @start: offset of the bitmap item in the extent buffer
5582 * @nr: bit number
5583 * @page_index: return index of the page in the extent buffer that contains the
5584 * given bit number
5585 * @page_offset: return offset into the page given by page_index
5586 *
5587 * This helper hides the ugliness of finding the byte in an extent buffer which
5588 * contains a given bit.
5589 */
5590static inline void eb_bitmap_offset(struct extent_buffer *eb,
5591 unsigned long start, unsigned long nr,
5592 unsigned long *page_index,
5593 size_t *page_offset)
5594{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005595 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005596 size_t byte_offset = BIT_BYTE(nr);
5597 size_t offset;
5598
5599 /*
5600 * The byte we want is the offset of the extent buffer + the offset of
5601 * the bitmap item in the extent buffer + the offset of the byte in the
5602 * bitmap item.
5603 */
5604 offset = start_offset + start + byte_offset;
5605
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005606 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005607 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005608}
5609
5610/**
5611 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5612 * @eb: the extent buffer
5613 * @start: offset of the bitmap item in the extent buffer
5614 * @nr: bit number to test
5615 */
5616int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5617 unsigned long nr)
5618{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005619 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005620 struct page *page;
5621 unsigned long i;
5622 size_t offset;
5623
5624 eb_bitmap_offset(eb, start, nr, &i, &offset);
5625 page = eb->pages[i];
5626 WARN_ON(!PageUptodate(page));
5627 kaddr = page_address(page);
5628 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5629}
5630
5631/**
5632 * extent_buffer_bitmap_set - set an area of a bitmap
5633 * @eb: the extent buffer
5634 * @start: offset of the bitmap item in the extent buffer
5635 * @pos: bit number of the first bit
5636 * @len: number of bits to set
5637 */
5638void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5639 unsigned long pos, unsigned long len)
5640{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005641 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005642 struct page *page;
5643 unsigned long i;
5644 size_t offset;
5645 const unsigned int size = pos + len;
5646 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005647 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005648
5649 eb_bitmap_offset(eb, start, pos, &i, &offset);
5650 page = eb->pages[i];
5651 WARN_ON(!PageUptodate(page));
5652 kaddr = page_address(page);
5653
5654 while (len >= bits_to_set) {
5655 kaddr[offset] |= mask_to_set;
5656 len -= bits_to_set;
5657 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005658 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005659 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005660 offset = 0;
5661 page = eb->pages[++i];
5662 WARN_ON(!PageUptodate(page));
5663 kaddr = page_address(page);
5664 }
5665 }
5666 if (len) {
5667 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5668 kaddr[offset] |= mask_to_set;
5669 }
5670}
5671
5672
5673/**
5674 * extent_buffer_bitmap_clear - clear an area of a bitmap
5675 * @eb: the extent buffer
5676 * @start: offset of the bitmap item in the extent buffer
5677 * @pos: bit number of the first bit
5678 * @len: number of bits to clear
5679 */
5680void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5681 unsigned long pos, unsigned long len)
5682{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005683 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005684 struct page *page;
5685 unsigned long i;
5686 size_t offset;
5687 const unsigned int size = pos + len;
5688 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005689 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005690
5691 eb_bitmap_offset(eb, start, pos, &i, &offset);
5692 page = eb->pages[i];
5693 WARN_ON(!PageUptodate(page));
5694 kaddr = page_address(page);
5695
5696 while (len >= bits_to_clear) {
5697 kaddr[offset] &= ~mask_to_clear;
5698 len -= bits_to_clear;
5699 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005700 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005701 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005702 offset = 0;
5703 page = eb->pages[++i];
5704 WARN_ON(!PageUptodate(page));
5705 kaddr = page_address(page);
5706 }
5707 }
5708 if (len) {
5709 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5710 kaddr[offset] &= ~mask_to_clear;
5711 }
5712}
5713
Sergei Trofimovich33872062011-04-11 21:52:52 +00005714static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5715{
5716 unsigned long distance = (src > dst) ? src - dst : dst - src;
5717 return distance < len;
5718}
5719
Chris Masond1310b22008-01-24 16:13:08 -05005720static void copy_pages(struct page *dst_page, struct page *src_page,
5721 unsigned long dst_off, unsigned long src_off,
5722 unsigned long len)
5723{
Chris Masona6591712011-07-19 12:04:14 -04005724 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005725 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005726 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005727
Sergei Trofimovich33872062011-04-11 21:52:52 +00005728 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005729 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005730 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005731 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005732 if (areas_overlap(src_off, dst_off, len))
5733 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005734 }
Chris Masond1310b22008-01-24 16:13:08 -05005735
Chris Mason727011e2010-08-06 13:21:20 -04005736 if (must_memmove)
5737 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5738 else
5739 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005740}
5741
5742void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5743 unsigned long src_offset, unsigned long len)
5744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005745 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005746 size_t cur;
5747 size_t dst_off_in_page;
5748 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005749 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005750 unsigned long dst_i;
5751 unsigned long src_i;
5752
5753 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005754 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005755 "memmove bogus src_offset %lu move len %lu dst len %lu",
5756 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005757 BUG_ON(1);
5758 }
5759 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005760 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005761 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5762 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005763 BUG_ON(1);
5764 }
5765
Chris Masond3977122009-01-05 21:25:51 -05005766 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005767 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5768 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005769
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005770 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5771 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005772
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005773 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005774 src_off_in_page));
5775 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005776 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005777
David Sterbafb85fc92014-07-31 01:03:53 +02005778 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005779 dst_off_in_page, src_off_in_page, cur);
5780
5781 src_offset += cur;
5782 dst_offset += cur;
5783 len -= cur;
5784 }
5785}
Chris Masond1310b22008-01-24 16:13:08 -05005786
5787void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5788 unsigned long src_offset, unsigned long len)
5789{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005790 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005791 size_t cur;
5792 size_t dst_off_in_page;
5793 size_t src_off_in_page;
5794 unsigned long dst_end = dst_offset + len - 1;
5795 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005796 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005797 unsigned long dst_i;
5798 unsigned long src_i;
5799
5800 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005801 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005802 "memmove bogus src_offset %lu move len %lu len %lu",
5803 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005804 BUG_ON(1);
5805 }
5806 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005807 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005808 "memmove bogus dst_offset %lu move len %lu len %lu",
5809 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005810 BUG_ON(1);
5811 }
Chris Mason727011e2010-08-06 13:21:20 -04005812 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005813 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5814 return;
5815 }
Chris Masond3977122009-01-05 21:25:51 -05005816 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005817 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5818 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005819
Johannes Thumshirn70730172018-12-05 15:23:03 +01005820 dst_off_in_page = offset_in_page(start_offset + dst_end);
5821 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005822
5823 cur = min_t(unsigned long, len, src_off_in_page + 1);
5824 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005825 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005826 dst_off_in_page - cur + 1,
5827 src_off_in_page - cur + 1, cur);
5828
5829 dst_end -= cur;
5830 src_end -= cur;
5831 len -= cur;
5832 }
5833}
Chris Mason6af118ce2008-07-22 11:18:07 -04005834
David Sterbaf7a52a42013-04-26 14:56:29 +00005835int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005836{
Chris Mason6af118ce2008-07-22 11:18:07 -04005837 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005838
Miao Xie19fe0a82010-10-26 20:57:29 -04005839 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005840 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005841 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005842 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005843 spin_lock(&page->mapping->private_lock);
5844 if (!PagePrivate(page)) {
5845 spin_unlock(&page->mapping->private_lock);
5846 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005847 }
5848
Josef Bacik3083ee22012-03-09 16:01:49 -05005849 eb = (struct extent_buffer *)page->private;
5850 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005851
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005852 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005853 * This is a little awful but should be ok, we need to make sure that
5854 * the eb doesn't disappear out from under us while we're looking at
5855 * this page.
5856 */
5857 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005858 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005859 spin_unlock(&eb->refs_lock);
5860 spin_unlock(&page->mapping->private_lock);
5861 return 0;
5862 }
5863 spin_unlock(&page->mapping->private_lock);
5864
Josef Bacik3083ee22012-03-09 16:01:49 -05005865 /*
5866 * If tree ref isn't set then we know the ref on this eb is a real ref,
5867 * so just return, this page will likely be freed soon anyway.
5868 */
5869 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5870 spin_unlock(&eb->refs_lock);
5871 return 0;
5872 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005873
David Sterbaf7a52a42013-04-26 14:56:29 +00005874 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005875}