blob: 139f2fe3092f8acfa86d46b92c75c979d435ebfc [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,
236 struct extent_io_tree *tree, void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500237{
Qu Wenruoc258d6e2019-03-01 10:47:58 +0800238 tree->fs_info = fs_info;
Eric Paris6bef4d32010-02-23 19:43:04 +0000239 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500240 tree->ops = NULL;
241 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500242 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400243 tree->private_data = private_data;
Chris Masond1310b22008-01-24 16:13:08 -0500244}
Chris Masond1310b22008-01-24 16:13:08 -0500245
Christoph Hellwigb2950862008-12-02 09:54:17 -0500246static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500247{
248 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500249
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100250 /*
251 * The given mask might be not appropriate for the slab allocator,
252 * drop the unsupported bits
253 */
254 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500255 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400256 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500257 return state;
258 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100259 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100260 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000261 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200262 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500263 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100264 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500265 return state;
266}
Chris Masond1310b22008-01-24 16:13:08 -0500267
Chris Mason4845e442010-05-25 20:56:50 -0400268void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500269{
Chris Masond1310b22008-01-24 16:13:08 -0500270 if (!state)
271 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200272 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100273 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000274 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100275 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500276 kmem_cache_free(extent_state_cache, state);
277 }
278}
Chris Masond1310b22008-01-24 16:13:08 -0500279
Filipe Mananaf2071b22014-02-12 15:05:53 +0000280static struct rb_node *tree_insert(struct rb_root *root,
281 struct rb_node *search_start,
282 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000283 struct rb_node *node,
284 struct rb_node ***p_in,
285 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500286{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000287 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500288 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500289 struct tree_entry *entry;
290
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000291 if (p_in && parent_in) {
292 p = *p_in;
293 parent = *parent_in;
294 goto do_insert;
295 }
296
Filipe Mananaf2071b22014-02-12 15:05:53 +0000297 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500298 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500299 parent = *p;
300 entry = rb_entry(parent, struct tree_entry, rb_node);
301
302 if (offset < entry->start)
303 p = &(*p)->rb_left;
304 else if (offset > entry->end)
305 p = &(*p)->rb_right;
306 else
307 return parent;
308 }
309
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000310do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500311 rb_link_node(node, parent, p);
312 rb_insert_color(node, root);
313 return NULL;
314}
315
Chris Mason80ea96b2008-02-01 14:51:59 -0500316static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000317 struct rb_node **next_ret,
Nikolay Borisov352646c2019-01-30 16:51:00 +0200318 struct rb_node **prev_ret,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000319 struct rb_node ***p_ret,
320 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500321{
Chris Mason80ea96b2008-02-01 14:51:59 -0500322 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000323 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500324 struct rb_node *prev = NULL;
325 struct rb_node *orig_prev = NULL;
326 struct tree_entry *entry;
327 struct tree_entry *prev_entry = NULL;
328
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000329 while (*n) {
330 prev = *n;
331 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500332 prev_entry = entry;
333
334 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000335 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500336 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000337 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500338 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000339 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500340 }
341
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000342 if (p_ret)
343 *p_ret = n;
344 if (parent_ret)
345 *parent_ret = prev;
346
Nikolay Borisov352646c2019-01-30 16:51:00 +0200347 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500348 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500349 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500350 prev = rb_next(prev);
351 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
352 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200353 *next_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500354 prev = orig_prev;
355 }
356
Nikolay Borisov352646c2019-01-30 16:51:00 +0200357 if (prev_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500358 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500359 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500360 prev = rb_prev(prev);
361 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
362 }
Nikolay Borisov352646c2019-01-30 16:51:00 +0200363 *prev_ret = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500364 }
365 return NULL;
366}
367
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000368static inline struct rb_node *
369tree_search_for_insert(struct extent_io_tree *tree,
370 u64 offset,
371 struct rb_node ***p_ret,
372 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500373{
Nikolay Borisov352646c2019-01-30 16:51:00 +0200374 struct rb_node *next= NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500375 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500376
Nikolay Borisov352646c2019-01-30 16:51:00 +0200377 ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500378 if (!ret)
Nikolay Borisov352646c2019-01-30 16:51:00 +0200379 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500380 return ret;
381}
382
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000383static inline struct rb_node *tree_search(struct extent_io_tree *tree,
384 u64 offset)
385{
386 return tree_search_for_insert(tree, offset, NULL, NULL);
387}
388
Chris Masond1310b22008-01-24 16:13:08 -0500389/*
390 * utility function to look for merge candidates inside a given range.
391 * Any extents with matching state are merged together into a single
392 * extent in the tree. Extents with EXTENT_IO in their state field
393 * are not merged because the end_io handlers need to be able to do
394 * operations on them without sleeping (or doing allocations/splits).
395 *
396 * This should be called with the tree lock held.
397 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000398static void merge_state(struct extent_io_tree *tree,
399 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500400{
401 struct extent_state *other;
402 struct rb_node *other_node;
403
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400404 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000405 return;
Chris Masond1310b22008-01-24 16:13:08 -0500406
407 other_node = rb_prev(&state->rb_node);
408 if (other_node) {
409 other = rb_entry(other_node, struct extent_state, rb_node);
410 if (other->end == state->start - 1 &&
411 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200412 if (tree->private_data &&
413 is_data_inode(tree->private_data))
414 btrfs_merge_delalloc_extent(tree->private_data,
415 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500416 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500417 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100418 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500419 free_extent_state(other);
420 }
421 }
422 other_node = rb_next(&state->rb_node);
423 if (other_node) {
424 other = rb_entry(other_node, struct extent_state, rb_node);
425 if (other->start == state->end + 1 &&
426 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200427 if (tree->private_data &&
428 is_data_inode(tree->private_data))
429 btrfs_merge_delalloc_extent(tree->private_data,
430 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400431 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400432 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100433 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400434 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500435 }
436 }
Chris Masond1310b22008-01-24 16:13:08 -0500437}
438
Xiao Guangrong3150b692011-07-14 03:19:08 +0000439static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800440 struct extent_state *state, unsigned *bits,
441 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000442
Chris Masond1310b22008-01-24 16:13:08 -0500443/*
444 * insert an extent_state struct into the tree. 'bits' are set on the
445 * struct before it is inserted.
446 *
447 * This may return -EEXIST if the extent is already there, in which case the
448 * state struct is freed.
449 *
450 * The tree lock is not taken internally. This is a utility function and
451 * probably isn't what you want to call (see set/clear_extent_bit).
452 */
453static int insert_state(struct extent_io_tree *tree,
454 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000455 struct rb_node ***p,
456 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800457 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500458{
459 struct rb_node *node;
460
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000461 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500462 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200463 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500464 state->start = start;
465 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400466
Qu Wenruod38ed272015-10-12 14:53:37 +0800467 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000468
Filipe Mananaf2071b22014-02-12 15:05:53 +0000469 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500470 if (node) {
471 struct extent_state *found;
472 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400473 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200474 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500475 return -EEXIST;
476 }
477 merge_state(tree, state);
478 return 0;
479}
480
481/*
482 * split a given extent state struct in two, inserting the preallocated
483 * struct 'prealloc' as the newly created second half. 'split' indicates an
484 * offset inside 'orig' where it should be split.
485 *
486 * Before calling,
487 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
488 * are two extent state structs in the tree:
489 * prealloc: [orig->start, split - 1]
490 * orig: [ split, orig->end ]
491 *
492 * The tree locks are not taken by this function. They need to be held
493 * by the caller.
494 */
495static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
496 struct extent_state *prealloc, u64 split)
497{
498 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400499
Nikolay Borisovabbb55f2018-11-01 14:09:53 +0200500 if (tree->private_data && is_data_inode(tree->private_data))
501 btrfs_split_delalloc_extent(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400502
Chris Masond1310b22008-01-24 16:13:08 -0500503 prealloc->start = orig->start;
504 prealloc->end = split - 1;
505 prealloc->state = orig->state;
506 orig->start = split;
507
Filipe Mananaf2071b22014-02-12 15:05:53 +0000508 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
509 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500510 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500511 free_extent_state(prealloc);
512 return -EEXIST;
513 }
514 return 0;
515}
516
Li Zefancdc6a392012-03-12 16:39:48 +0800517static struct extent_state *next_state(struct extent_state *state)
518{
519 struct rb_node *next = rb_next(&state->rb_node);
520 if (next)
521 return rb_entry(next, struct extent_state, rb_node);
522 else
523 return NULL;
524}
525
Chris Masond1310b22008-01-24 16:13:08 -0500526/*
527 * utility function to clear some bits in an extent state struct.
Andrea Gelmini52042d82018-11-28 12:05:13 +0100528 * it will optionally wake up anyone waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500529 *
530 * If no bits are set on the state struct after clearing things, the
531 * struct is freed and removed from the tree
532 */
Li Zefancdc6a392012-03-12 16:39:48 +0800533static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
534 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800535 unsigned *bits, int wake,
536 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500537{
Li Zefancdc6a392012-03-12 16:39:48 +0800538 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100539 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100540 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500541
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400542 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500543 u64 range = state->end - state->start + 1;
544 WARN_ON(range > tree->dirty_bytes);
545 tree->dirty_bytes -= range;
546 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200547
548 if (tree->private_data && is_data_inode(tree->private_data))
549 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
550
David Sterba57599c72018-03-01 17:56:34 +0100551 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
552 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400553 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500554 if (wake)
555 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400556 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800557 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100558 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500559 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100560 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500561 free_extent_state(state);
562 } else {
563 WARN_ON(1);
564 }
565 } else {
566 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800567 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500568 }
Li Zefancdc6a392012-03-12 16:39:48 +0800569 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500570}
571
Xiao Guangrong82337672011-04-20 06:44:57 +0000572static struct extent_state *
573alloc_extent_state_atomic(struct extent_state *prealloc)
574{
575 if (!prealloc)
576 prealloc = alloc_extent_state(GFP_ATOMIC);
577
578 return prealloc;
579}
580
Eric Sandeen48a3b632013-04-25 20:41:01 +0000581static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400582{
David Sterba05912a32018-07-18 19:23:45 +0200583 struct inode *inode = tree->private_data;
584
585 btrfs_panic(btrfs_sb(inode->i_sb), err,
586 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400587}
588
Chris Masond1310b22008-01-24 16:13:08 -0500589/*
590 * clear some bits on a range in the tree. This may require splitting
591 * or inserting elements in the tree, so the gfp mask is used to
592 * indicate which allocations or sleeping are allowed.
593 *
594 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
595 * the given range from the tree regardless of state (ie for truncate).
596 *
597 * the range [start, end] is inclusive.
598 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100599 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500600 */
David Sterba66b0c882017-10-31 16:30:47 +0100601int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800602 unsigned bits, int wake, int delete,
603 struct extent_state **cached_state,
604 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500605{
606 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400607 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500608 struct extent_state *prealloc = NULL;
609 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400610 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500611 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000612 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500613
Josef Bacika5dee372013-12-13 10:02:44 -0500614 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000615
Josef Bacik7ee9e442013-06-21 16:37:03 -0400616 if (bits & EXTENT_DELALLOC)
617 bits |= EXTENT_NORESERVE;
618
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400619 if (delete)
620 bits |= ~EXTENT_CTLBITS;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400621
Josef Bacik2ac55d42010-02-03 19:33:23 +0000622 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
623 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500624again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800625 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000626 /*
627 * Don't care for allocation failure here because we might end
628 * up not needing the pre-allocated extent state at all, which
629 * is the case if we only have in the tree extent states that
630 * cover our input range and don't cover too any other range.
631 * If we end up needing a new extent state we allocate it later.
632 */
Chris Masond1310b22008-01-24 16:13:08 -0500633 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500634 }
635
Chris Masoncad321a2008-12-17 14:51:42 -0500636 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400637 if (cached_state) {
638 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000639
640 if (clear) {
641 *cached_state = NULL;
642 cached_state = NULL;
643 }
644
Filipe Manana27a35072014-07-06 20:09:59 +0100645 if (cached && extent_state_in_tree(cached) &&
646 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000647 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200648 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400649 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400650 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400651 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000652 if (clear)
653 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400654 }
Chris Masond1310b22008-01-24 16:13:08 -0500655 /*
656 * this search will find the extents that end after
657 * our range starts
658 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500659 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500660 if (!node)
661 goto out;
662 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400663hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500664 if (state->start > end)
665 goto out;
666 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400667 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500668
Liu Bo04493142012-02-16 18:34:37 +0800669 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800670 if (!(state->state & bits)) {
671 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800672 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800673 }
Liu Bo04493142012-02-16 18:34:37 +0800674
Chris Masond1310b22008-01-24 16:13:08 -0500675 /*
676 * | ---- desired range ---- |
677 * | state | or
678 * | ------------- state -------------- |
679 *
680 * We need to split the extent we found, and may flip
681 * bits on second half.
682 *
683 * If the extent we found extends past our range, we
684 * just split and search again. It'll get split again
685 * the next time though.
686 *
687 * If the extent we found is inside our range, we clear
688 * the desired bit on it.
689 */
690
691 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000692 prealloc = alloc_extent_state_atomic(prealloc);
693 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500694 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400695 if (err)
696 extent_io_tree_panic(tree, err);
697
Chris Masond1310b22008-01-24 16:13:08 -0500698 prealloc = NULL;
699 if (err)
700 goto out;
701 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800702 state = clear_state_bit(tree, state, &bits, wake,
703 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800704 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500705 }
706 goto search_again;
707 }
708 /*
709 * | ---- desired range ---- |
710 * | state |
711 * We need to split the extent, and clear the bit
712 * on the first half
713 */
714 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000715 prealloc = alloc_extent_state_atomic(prealloc);
716 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500717 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400718 if (err)
719 extent_io_tree_panic(tree, err);
720
Chris Masond1310b22008-01-24 16:13:08 -0500721 if (wake)
722 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400723
Qu Wenruofefdc552015-10-12 15:35:38 +0800724 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400725
Chris Masond1310b22008-01-24 16:13:08 -0500726 prealloc = NULL;
727 goto out;
728 }
Chris Mason42daec22009-09-23 19:51:09 -0400729
Qu Wenruofefdc552015-10-12 15:35:38 +0800730 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800731next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400732 if (last_end == (u64)-1)
733 goto out;
734 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800735 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800736 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500737
738search_again:
739 if (start > end)
740 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500741 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800742 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500743 cond_resched();
744 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200745
746out:
747 spin_unlock(&tree->lock);
748 if (prealloc)
749 free_extent_state(prealloc);
750
751 return 0;
752
Chris Masond1310b22008-01-24 16:13:08 -0500753}
Chris Masond1310b22008-01-24 16:13:08 -0500754
Jeff Mahoney143bede2012-03-01 14:56:26 +0100755static void wait_on_state(struct extent_io_tree *tree,
756 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500757 __releases(tree->lock)
758 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500759{
760 DEFINE_WAIT(wait);
761 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500762 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500763 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500764 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500765 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500766}
767
768/*
769 * waits for one or more bits to clear on a range in the state tree.
770 * The range [start, end] is inclusive.
771 * The tree lock is taken by this function
772 */
David Sterba41074882013-04-29 13:38:46 +0000773static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
774 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500775{
776 struct extent_state *state;
777 struct rb_node *node;
778
Josef Bacika5dee372013-12-13 10:02:44 -0500779 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000780
Chris Masoncad321a2008-12-17 14:51:42 -0500781 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500782again:
783 while (1) {
784 /*
785 * this search will find all the extents that end after
786 * our range starts
787 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500788 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100789process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500790 if (!node)
791 break;
792
793 state = rb_entry(node, struct extent_state, rb_node);
794
795 if (state->start > end)
796 goto out;
797
798 if (state->state & bits) {
799 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200800 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500801 wait_on_state(tree, state);
802 free_extent_state(state);
803 goto again;
804 }
805 start = state->end + 1;
806
807 if (start > end)
808 break;
809
Filipe Mananac50d3e72014-03-31 14:53:25 +0100810 if (!cond_resched_lock(&tree->lock)) {
811 node = rb_next(node);
812 goto process_node;
813 }
Chris Masond1310b22008-01-24 16:13:08 -0500814 }
815out:
Chris Masoncad321a2008-12-17 14:51:42 -0500816 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500817}
Chris Masond1310b22008-01-24 16:13:08 -0500818
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000819static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500820 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800821 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500822{
David Sterba9ee49a042015-01-14 19:52:13 +0100823 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100824 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400825
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200826 if (tree->private_data && is_data_inode(tree->private_data))
827 btrfs_set_delalloc_extent(tree->private_data, state, bits);
828
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400829 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500830 u64 range = state->end - state->start + 1;
831 tree->dirty_bytes += range;
832 }
David Sterba57599c72018-03-01 17:56:34 +0100833 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
834 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400835 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500836}
837
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100838static void cache_state_if_flags(struct extent_state *state,
839 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100840 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400841{
842 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100843 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400844 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200845 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400846 }
847 }
848}
849
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100850static void cache_state(struct extent_state *state,
851 struct extent_state **cached_ptr)
852{
853 return cache_state_if_flags(state, cached_ptr,
854 EXTENT_IOBITS | EXTENT_BOUNDARY);
855}
856
Chris Masond1310b22008-01-24 16:13:08 -0500857/*
Chris Mason1edbb732009-09-02 13:24:36 -0400858 * set some bits on a range in the tree. This may require allocations or
859 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500860 *
Chris Mason1edbb732009-09-02 13:24:36 -0400861 * If any of the exclusive bits are set, this will fail with -EEXIST if some
862 * part of the range already has the desired bits set. The start of the
863 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500864 *
Chris Mason1edbb732009-09-02 13:24:36 -0400865 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500866 */
Chris Mason1edbb732009-09-02 13:24:36 -0400867
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100868static int __must_check
869__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100870 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000871 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800872 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500873{
874 struct extent_state *state;
875 struct extent_state *prealloc = NULL;
876 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000877 struct rb_node **p;
878 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500879 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500880 u64 last_start;
881 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400882
Josef Bacika5dee372013-12-13 10:02:44 -0500883 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000884
Chris Masond1310b22008-01-24 16:13:08 -0500885again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800886 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200887 /*
888 * Don't care for allocation failure here because we might end
889 * up not needing the pre-allocated extent state at all, which
890 * is the case if we only have in the tree extent states that
891 * cover our input range and don't cover too any other range.
892 * If we end up needing a new extent state we allocate it later.
893 */
Chris Masond1310b22008-01-24 16:13:08 -0500894 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500895 }
896
Chris Masoncad321a2008-12-17 14:51:42 -0500897 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400898 if (cached_state && *cached_state) {
899 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400900 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100901 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400902 node = &state->rb_node;
903 goto hit_next;
904 }
905 }
Chris Masond1310b22008-01-24 16:13:08 -0500906 /*
907 * this search will find all the extents that end after
908 * our range starts.
909 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000910 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500911 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000912 prealloc = alloc_extent_state_atomic(prealloc);
913 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000914 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800915 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400916 if (err)
917 extent_io_tree_panic(tree, err);
918
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000919 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500920 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500921 goto out;
922 }
Chris Masond1310b22008-01-24 16:13:08 -0500923 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400924hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500925 last_start = state->start;
926 last_end = state->end;
927
928 /*
929 * | ---- desired range ---- |
930 * | state |
931 *
932 * Just lock what we found and keep going
933 */
934 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400935 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500936 *failed_start = state->start;
937 err = -EEXIST;
938 goto out;
939 }
Chris Mason42daec22009-09-23 19:51:09 -0400940
Qu Wenruod38ed272015-10-12 14:53:37 +0800941 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400942 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500943 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400944 if (last_end == (u64)-1)
945 goto out;
946 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800947 state = next_state(state);
948 if (start < end && state && state->start == start &&
949 !need_resched())
950 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500951 goto search_again;
952 }
953
954 /*
955 * | ---- desired range ---- |
956 * | state |
957 * or
958 * | ------------- state -------------- |
959 *
960 * We need to split the extent we found, and may flip bits on
961 * second half.
962 *
963 * If the extent we found extends past our
964 * range, we just split and search again. It'll get split
965 * again the next time though.
966 *
967 * If the extent we found is inside our range, we set the
968 * desired bit on it.
969 */
970 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400971 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500972 *failed_start = start;
973 err = -EEXIST;
974 goto out;
975 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000976
977 prealloc = alloc_extent_state_atomic(prealloc);
978 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500979 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400980 if (err)
981 extent_io_tree_panic(tree, err);
982
Chris Masond1310b22008-01-24 16:13:08 -0500983 prealloc = NULL;
984 if (err)
985 goto out;
986 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800987 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400988 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500989 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400990 if (last_end == (u64)-1)
991 goto out;
992 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800993 state = next_state(state);
994 if (start < end && state && state->start == start &&
995 !need_resched())
996 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500997 }
998 goto search_again;
999 }
1000 /*
1001 * | ---- desired range ---- |
1002 * | state | or | state |
1003 *
1004 * There's a hole, we need to insert something in it and
1005 * ignore the extent we found.
1006 */
1007 if (state->start > start) {
1008 u64 this_end;
1009 if (end < last_start)
1010 this_end = end;
1011 else
Chris Masond3977122009-01-05 21:25:51 -05001012 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001013
1014 prealloc = alloc_extent_state_atomic(prealloc);
1015 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001016
1017 /*
1018 * Avoid to free 'prealloc' if it can be merged with
1019 * the later extent.
1020 */
Chris Masond1310b22008-01-24 16:13:08 -05001021 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001022 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001023 if (err)
1024 extent_io_tree_panic(tree, err);
1025
Chris Mason2c64c532009-09-02 15:04:12 -04001026 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001027 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001028 start = this_end + 1;
1029 goto search_again;
1030 }
1031 /*
1032 * | ---- desired range ---- |
1033 * | state |
1034 * We need to split the extent, and set the bit
1035 * on the first half
1036 */
1037 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001038 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001039 *failed_start = start;
1040 err = -EEXIST;
1041 goto out;
1042 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001043
1044 prealloc = alloc_extent_state_atomic(prealloc);
1045 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001046 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001047 if (err)
1048 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001049
Qu Wenruod38ed272015-10-12 14:53:37 +08001050 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001051 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001052 merge_state(tree, prealloc);
1053 prealloc = NULL;
1054 goto out;
1055 }
1056
David Sterbab5a4ba142016-04-27 01:02:15 +02001057search_again:
1058 if (start > end)
1059 goto out;
1060 spin_unlock(&tree->lock);
1061 if (gfpflags_allow_blocking(mask))
1062 cond_resched();
1063 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001064
1065out:
Chris Masoncad321a2008-12-17 14:51:42 -05001066 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001067 if (prealloc)
1068 free_extent_state(prealloc);
1069
1070 return err;
1071
Chris Masond1310b22008-01-24 16:13:08 -05001072}
Chris Masond1310b22008-01-24 16:13:08 -05001073
David Sterba41074882013-04-29 13:38:46 +00001074int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001075 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001076 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001077{
1078 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001079 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001080}
1081
1082
Josef Bacik462d6fa2011-09-26 13:56:12 -04001083/**
Liu Bo10983f22012-07-11 15:26:19 +08001084 * convert_extent_bit - convert all bits in a given range from one bit to
1085 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001086 * @tree: the io tree to search
1087 * @start: the start offset in bytes
1088 * @end: the end offset in bytes (inclusive)
1089 * @bits: the bits to set in this range
1090 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001091 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001092 *
1093 * This will go through and set bits for the given range. If any states exist
1094 * already in this range they are set with the given bit and cleared of the
1095 * clear_bits. This is only meant to be used by things that are mergeable, ie
1096 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1097 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001098 *
1099 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001100 */
1101int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001102 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001103 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001104{
1105 struct extent_state *state;
1106 struct extent_state *prealloc = NULL;
1107 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001108 struct rb_node **p;
1109 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001110 int err = 0;
1111 u64 last_start;
1112 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001113 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001114
Josef Bacika5dee372013-12-13 10:02:44 -05001115 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001116
Josef Bacik462d6fa2011-09-26 13:56:12 -04001117again:
David Sterba210aa272016-04-26 23:54:39 +02001118 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001119 /*
1120 * Best effort, don't worry if extent state allocation fails
1121 * here for the first iteration. We might have a cached state
1122 * that matches exactly the target range, in which case no
1123 * extent state allocations are needed. We'll only know this
1124 * after locking the tree.
1125 */
David Sterba210aa272016-04-26 23:54:39 +02001126 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001127 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001128 return -ENOMEM;
1129 }
1130
1131 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001132 if (cached_state && *cached_state) {
1133 state = *cached_state;
1134 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001135 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001136 node = &state->rb_node;
1137 goto hit_next;
1138 }
1139 }
1140
Josef Bacik462d6fa2011-09-26 13:56:12 -04001141 /*
1142 * this search will find all the extents that end after
1143 * our range starts.
1144 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001145 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001146 if (!node) {
1147 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001148 if (!prealloc) {
1149 err = -ENOMEM;
1150 goto out;
1151 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001152 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001153 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001154 if (err)
1155 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001156 cache_state(prealloc, cached_state);
1157 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001158 goto out;
1159 }
1160 state = rb_entry(node, struct extent_state, rb_node);
1161hit_next:
1162 last_start = state->start;
1163 last_end = state->end;
1164
1165 /*
1166 * | ---- desired range ---- |
1167 * | state |
1168 *
1169 * Just lock what we found and keep going
1170 */
1171 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001172 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001173 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001174 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001175 if (last_end == (u64)-1)
1176 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001177 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001178 if (start < end && state && state->start == start &&
1179 !need_resched())
1180 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001181 goto search_again;
1182 }
1183
1184 /*
1185 * | ---- desired range ---- |
1186 * | state |
1187 * or
1188 * | ------------- state -------------- |
1189 *
1190 * We need to split the extent we found, and may flip bits on
1191 * second half.
1192 *
1193 * If the extent we found extends past our
1194 * range, we just split and search again. It'll get split
1195 * again the next time though.
1196 *
1197 * If the extent we found is inside our range, we set the
1198 * desired bit on it.
1199 */
1200 if (state->start < start) {
1201 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001202 if (!prealloc) {
1203 err = -ENOMEM;
1204 goto out;
1205 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001206 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001207 if (err)
1208 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001209 prealloc = NULL;
1210 if (err)
1211 goto out;
1212 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001213 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001214 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001215 state = clear_state_bit(tree, state, &clear_bits, 0,
1216 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001217 if (last_end == (u64)-1)
1218 goto out;
1219 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001220 if (start < end && state && state->start == start &&
1221 !need_resched())
1222 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001223 }
1224 goto search_again;
1225 }
1226 /*
1227 * | ---- desired range ---- |
1228 * | state | or | state |
1229 *
1230 * There's a hole, we need to insert something in it and
1231 * ignore the extent we found.
1232 */
1233 if (state->start > start) {
1234 u64 this_end;
1235 if (end < last_start)
1236 this_end = end;
1237 else
1238 this_end = last_start - 1;
1239
1240 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001241 if (!prealloc) {
1242 err = -ENOMEM;
1243 goto out;
1244 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001245
1246 /*
1247 * Avoid to free 'prealloc' if it can be merged with
1248 * the later extent.
1249 */
1250 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001251 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001252 if (err)
1253 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001254 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001255 prealloc = NULL;
1256 start = this_end + 1;
1257 goto search_again;
1258 }
1259 /*
1260 * | ---- desired range ---- |
1261 * | state |
1262 * We need to split the extent, and set the bit
1263 * on the first half
1264 */
1265 if (state->start <= end && state->end > end) {
1266 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001267 if (!prealloc) {
1268 err = -ENOMEM;
1269 goto out;
1270 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001271
1272 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001273 if (err)
1274 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001275
Qu Wenruod38ed272015-10-12 14:53:37 +08001276 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001277 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001278 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001279 prealloc = NULL;
1280 goto out;
1281 }
1282
Josef Bacik462d6fa2011-09-26 13:56:12 -04001283search_again:
1284 if (start > end)
1285 goto out;
1286 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001287 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001288 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001289 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001290
1291out:
1292 spin_unlock(&tree->lock);
1293 if (prealloc)
1294 free_extent_state(prealloc);
1295
1296 return err;
1297}
1298
Chris Masond1310b22008-01-24 16:13:08 -05001299/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001300int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001301 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001302{
1303 /*
1304 * We don't support EXTENT_LOCKED yet, as current changeset will
1305 * record any bits changed, so for EXTENT_LOCKED case, it will
1306 * either fail with -EEXIST or changeset will record the whole
1307 * range.
1308 */
1309 BUG_ON(bits & EXTENT_LOCKED);
1310
David Sterba2c53b912016-04-26 23:54:39 +02001311 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001312 changeset);
1313}
1314
Qu Wenruofefdc552015-10-12 15:35:38 +08001315int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1316 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001317 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001318{
1319 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001320 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001321}
1322
Qu Wenruofefdc552015-10-12 15:35:38 +08001323int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001324 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001325{
1326 /*
1327 * Don't support EXTENT_LOCKED case, same reason as
1328 * set_record_extent_bits().
1329 */
1330 BUG_ON(bits & EXTENT_LOCKED);
1331
David Sterbaf734c442016-04-26 23:54:39 +02001332 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001333 changeset);
1334}
1335
Chris Masond352ac62008-09-29 15:18:18 -04001336/*
1337 * either insert or lock state struct between start and end use mask to tell
1338 * us if waiting is desired.
1339 */
Chris Mason1edbb732009-09-02 13:24:36 -04001340int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001341 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001342{
1343 int err;
1344 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001345
Chris Masond1310b22008-01-24 16:13:08 -05001346 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001347 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001348 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001349 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001350 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001351 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1352 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001353 } else
Chris Masond1310b22008-01-24 16:13:08 -05001354 break;
Chris Masond1310b22008-01-24 16:13:08 -05001355 WARN_ON(start > end);
1356 }
1357 return err;
1358}
Chris Masond1310b22008-01-24 16:13:08 -05001359
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001360int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik251792012008-10-29 14:49:05 -04001361{
1362 int err;
1363 u64 failed_start;
1364
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001365 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001366 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001367 if (err == -EEXIST) {
1368 if (failed_start > start)
1369 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001370 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik251792012008-10-29 14:49:05 -04001371 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001372 }
Josef Bacik251792012008-10-29 14:49:05 -04001373 return 1;
1374}
Josef Bacik251792012008-10-29 14:49:05 -04001375
David Sterbabd1fa4f2015-12-03 13:08:59 +01001376void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001377{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001378 unsigned long index = start >> PAGE_SHIFT;
1379 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001380 struct page *page;
1381
1382 while (index <= end_index) {
1383 page = find_get_page(inode->i_mapping, index);
1384 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1385 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001386 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001387 index++;
1388 }
Chris Mason4adaa612013-03-26 13:07:00 -04001389}
1390
David Sterbaf6311572015-12-03 13:08:59 +01001391void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001392{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001393 unsigned long index = start >> PAGE_SHIFT;
1394 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001395 struct page *page;
1396
1397 while (index <= end_index) {
1398 page = find_get_page(inode->i_mapping, index);
1399 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001400 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001401 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001402 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001403 index++;
1404 }
Chris Mason4adaa612013-03-26 13:07:00 -04001405}
1406
Chris Masond352ac62008-09-29 15:18:18 -04001407/* find the first state struct with 'bits' set after 'start', and
1408 * return it. tree->lock must be held. NULL will returned if
1409 * nothing was found after 'start'
1410 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001411static struct extent_state *
1412find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001413 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001414{
1415 struct rb_node *node;
1416 struct extent_state *state;
1417
1418 /*
1419 * this search will find all the extents that end after
1420 * our range starts.
1421 */
1422 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001423 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001424 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001425
Chris Masond3977122009-01-05 21:25:51 -05001426 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001427 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001428 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001429 return state;
Chris Masond3977122009-01-05 21:25:51 -05001430
Chris Masond7fc6402008-02-18 12:12:38 -05001431 node = rb_next(node);
1432 if (!node)
1433 break;
1434 }
1435out:
1436 return NULL;
1437}
Chris Masond7fc6402008-02-18 12:12:38 -05001438
Chris Masond352ac62008-09-29 15:18:18 -04001439/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001440 * find the first offset in the io tree with 'bits' set. zero is
1441 * returned if we find something, and *start_ret and *end_ret are
1442 * set to reflect the state struct that was found.
1443 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001444 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001445 */
1446int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001447 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001448 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001449{
1450 struct extent_state *state;
1451 int ret = 1;
1452
1453 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001454 if (cached_state && *cached_state) {
1455 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001456 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001457 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001458 if (state->state & bits)
1459 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001460 }
1461 free_extent_state(*cached_state);
1462 *cached_state = NULL;
1463 goto out;
1464 }
1465 free_extent_state(*cached_state);
1466 *cached_state = NULL;
1467 }
1468
Xiao Guangrong69261c42011-07-14 03:19:45 +00001469 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001470got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001471 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001472 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001473 *start_ret = state->start;
1474 *end_ret = state->end;
1475 ret = 0;
1476 }
Josef Bacike6138872012-09-27 17:07:30 -04001477out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001478 spin_unlock(&tree->lock);
1479 return ret;
1480}
1481
1482/*
Chris Masond352ac62008-09-29 15:18:18 -04001483 * find a contiguous range of bytes in the file marked as delalloc, not
1484 * more than 'max_bytes'. start and end are used to return the range,
1485 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001486 * true is returned if we find something, false if nothing was in the tree
Chris Masond352ac62008-09-29 15:18:18 -04001487 */
Lu Fengqi3522e902018-11-29 11:33:38 +08001488static noinline bool find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001489 u64 *start, u64 *end, u64 max_bytes,
1490 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001491{
1492 struct rb_node *node;
1493 struct extent_state *state;
1494 u64 cur_start = *start;
Lu Fengqi3522e902018-11-29 11:33:38 +08001495 bool found = false;
Chris Masond1310b22008-01-24 16:13:08 -05001496 u64 total_bytes = 0;
1497
Chris Masoncad321a2008-12-17 14:51:42 -05001498 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001499
Chris Masond1310b22008-01-24 16:13:08 -05001500 /*
1501 * this search will find all the extents that end after
1502 * our range starts.
1503 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001504 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001505 if (!node) {
Lu Fengqi3522e902018-11-29 11:33:38 +08001506 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001507 goto out;
1508 }
1509
Chris Masond3977122009-01-05 21:25:51 -05001510 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001511 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001512 if (found && (state->start != cur_start ||
1513 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001514 goto out;
1515 }
1516 if (!(state->state & EXTENT_DELALLOC)) {
1517 if (!found)
1518 *end = state->end;
1519 goto out;
1520 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001521 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001522 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001523 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001524 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001525 }
Lu Fengqi3522e902018-11-29 11:33:38 +08001526 found = true;
Chris Masond1310b22008-01-24 16:13:08 -05001527 *end = state->end;
1528 cur_start = state->end + 1;
1529 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001530 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001531 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001532 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001533 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001534 break;
1535 }
1536out:
Chris Masoncad321a2008-12-17 14:51:42 -05001537 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001538 return found;
1539}
1540
Liu Boda2c7002017-02-10 16:41:05 +01001541static int __process_pages_contig(struct address_space *mapping,
1542 struct page *locked_page,
1543 pgoff_t start_index, pgoff_t end_index,
1544 unsigned long page_ops, pgoff_t *index_ret);
1545
Jeff Mahoney143bede2012-03-01 14:56:26 +01001546static noinline void __unlock_for_delalloc(struct inode *inode,
1547 struct page *locked_page,
1548 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001549{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001550 unsigned long index = start >> PAGE_SHIFT;
1551 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001552
Liu Bo76c00212017-02-10 16:42:14 +01001553 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001554 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001555 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001556
Liu Bo76c00212017-02-10 16:42:14 +01001557 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1558 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001559}
1560
1561static noinline int lock_delalloc_pages(struct inode *inode,
1562 struct page *locked_page,
1563 u64 delalloc_start,
1564 u64 delalloc_end)
1565{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001566 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001567 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001568 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001569 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001570
Liu Bo76c00212017-02-10 16:42:14 +01001571 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001572 if (index == locked_page->index && index == end_index)
1573 return 0;
1574
Liu Bo76c00212017-02-10 16:42:14 +01001575 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1576 end_index, PAGE_LOCK, &index_ret);
1577 if (ret == -EAGAIN)
1578 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1579 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001580 return ret;
1581}
1582
1583/*
Lu Fengqi3522e902018-11-29 11:33:38 +08001584 * Find and lock a contiguous range of bytes in the file marked as delalloc, no
1585 * more than @max_bytes. @Start and @end are used to return the range,
Chris Masonc8b97812008-10-29 14:49:59 -04001586 *
Lu Fengqi3522e902018-11-29 11:33:38 +08001587 * Return: true if we find something
1588 * false if nothing was in the tree
Chris Masonc8b97812008-10-29 14:49:59 -04001589 */
Johannes Thumshirnce9f9672018-11-19 10:38:17 +01001590EXPORT_FOR_TESTS
Lu Fengqi3522e902018-11-29 11:33:38 +08001591noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001592 struct extent_io_tree *tree,
1593 struct page *locked_page, u64 *start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03001594 u64 *end)
Chris Masonc8b97812008-10-29 14:49:59 -04001595{
Nikolay Borisov917aace2018-10-26 14:43:20 +03001596 u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001597 u64 delalloc_start;
1598 u64 delalloc_end;
Lu Fengqi3522e902018-11-29 11:33:38 +08001599 bool found;
Chris Mason9655d292009-09-02 15:22:30 -04001600 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001601 int ret;
1602 int loops = 0;
1603
1604again:
1605 /* step one, find a bunch of delalloc bytes starting at start */
1606 delalloc_start = *start;
1607 delalloc_end = 0;
1608 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001609 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001610 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001611 *start = delalloc_start;
1612 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001613 free_extent_state(cached_state);
Lu Fengqi3522e902018-11-29 11:33:38 +08001614 return false;
Chris Masonc8b97812008-10-29 14:49:59 -04001615 }
1616
1617 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001618 * start comes from the offset of locked_page. We have to lock
1619 * pages in order, so we can't process delalloc bytes before
1620 * locked_page
1621 */
Chris Masond3977122009-01-05 21:25:51 -05001622 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001623 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001624
1625 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001626 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001627 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001628 if (delalloc_end + 1 - delalloc_start > max_bytes)
1629 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001630
Chris Masonc8b97812008-10-29 14:49:59 -04001631 /* step two, lock all the pages after the page that has start */
1632 ret = lock_delalloc_pages(inode, locked_page,
1633 delalloc_start, delalloc_end);
Nikolay Borisov9bfd61d2018-10-26 14:43:21 +03001634 ASSERT(!ret || ret == -EAGAIN);
Chris Masonc8b97812008-10-29 14:49:59 -04001635 if (ret == -EAGAIN) {
1636 /* some of the pages are gone, lets avoid looping by
1637 * shortening the size of the delalloc range we're searching
1638 */
Chris Mason9655d292009-09-02 15:22:30 -04001639 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001640 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001641 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001642 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001643 loops = 1;
1644 goto again;
1645 } else {
Lu Fengqi3522e902018-11-29 11:33:38 +08001646 found = false;
Chris Masonc8b97812008-10-29 14:49:59 -04001647 goto out_failed;
1648 }
1649 }
Chris Masonc8b97812008-10-29 14:49:59 -04001650
1651 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001652 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001653
1654 /* then test to make sure it is all still delalloc */
1655 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001656 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001657 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001658 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001659 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001660 __unlock_for_delalloc(inode, locked_page,
1661 delalloc_start, delalloc_end);
1662 cond_resched();
1663 goto again;
1664 }
Chris Mason9655d292009-09-02 15:22:30 -04001665 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001666 *start = delalloc_start;
1667 *end = delalloc_end;
1668out_failed:
1669 return found;
1670}
1671
Liu Boda2c7002017-02-10 16:41:05 +01001672static int __process_pages_contig(struct address_space *mapping,
1673 struct page *locked_page,
1674 pgoff_t start_index, pgoff_t end_index,
1675 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001676{
Liu Bo873695b2017-02-02 17:49:22 -08001677 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001678 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001679 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001680 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001681 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001682 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001683 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001684
Liu Boda2c7002017-02-10 16:41:05 +01001685 if (page_ops & PAGE_LOCK) {
1686 ASSERT(page_ops == PAGE_LOCK);
1687 ASSERT(index_ret && *index_ret == start_index);
1688 }
1689
Filipe Manana704de492014-10-06 22:14:22 +01001690 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001691 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001692
Chris Masond3977122009-01-05 21:25:51 -05001693 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001694 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001695 min_t(unsigned long,
1696 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001697 if (ret == 0) {
1698 /*
1699 * Only if we're going to lock these pages,
1700 * can we find nothing at @index.
1701 */
1702 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001703 err = -EAGAIN;
1704 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001705 }
Chris Mason8b62b722009-09-02 16:53:46 -04001706
Liu Boda2c7002017-02-10 16:41:05 +01001707 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001708 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001709 SetPagePrivate2(pages[i]);
1710
Chris Masonc8b97812008-10-29 14:49:59 -04001711 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001712 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001713 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001714 continue;
1715 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001716 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001717 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001718 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001719 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001720 if (page_ops & PAGE_SET_ERROR)
1721 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001722 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001723 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001724 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001725 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001726 if (page_ops & PAGE_LOCK) {
1727 lock_page(pages[i]);
1728 if (!PageDirty(pages[i]) ||
1729 pages[i]->mapping != mapping) {
1730 unlock_page(pages[i]);
1731 put_page(pages[i]);
1732 err = -EAGAIN;
1733 goto out;
1734 }
1735 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001736 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001737 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001738 }
1739 nr_pages -= ret;
1740 index += ret;
1741 cond_resched();
1742 }
Liu Boda2c7002017-02-10 16:41:05 +01001743out:
1744 if (err && index_ret)
1745 *index_ret = start_index + pages_locked - 1;
1746 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001747}
Chris Masonc8b97812008-10-29 14:49:59 -04001748
Liu Bo873695b2017-02-02 17:49:22 -08001749void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1750 u64 delalloc_end, struct page *locked_page,
1751 unsigned clear_bits,
1752 unsigned long page_ops)
1753{
1754 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001755 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001756
1757 __process_pages_contig(inode->i_mapping, locked_page,
1758 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001759 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001760}
1761
Chris Masond352ac62008-09-29 15:18:18 -04001762/*
1763 * count the number of bytes in the tree that have a given bit(s)
1764 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1765 * cached. The total number found is returned.
1766 */
Chris Masond1310b22008-01-24 16:13:08 -05001767u64 count_range_bits(struct extent_io_tree *tree,
1768 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001769 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001770{
1771 struct rb_node *node;
1772 struct extent_state *state;
1773 u64 cur_start = *start;
1774 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001775 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001776 int found = 0;
1777
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301778 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001779 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001780
Chris Masoncad321a2008-12-17 14:51:42 -05001781 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001782 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1783 total_bytes = tree->dirty_bytes;
1784 goto out;
1785 }
1786 /*
1787 * this search will find all the extents that end after
1788 * our range starts.
1789 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001790 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001791 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001792 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001793
Chris Masond3977122009-01-05 21:25:51 -05001794 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001795 state = rb_entry(node, struct extent_state, rb_node);
1796 if (state->start > search_end)
1797 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001798 if (contig && found && state->start > last + 1)
1799 break;
1800 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001801 total_bytes += min(search_end, state->end) + 1 -
1802 max(cur_start, state->start);
1803 if (total_bytes >= max_bytes)
1804 break;
1805 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001806 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001807 found = 1;
1808 }
Chris Masonec29ed52011-02-23 16:23:20 -05001809 last = state->end;
1810 } else if (contig && found) {
1811 break;
Chris Masond1310b22008-01-24 16:13:08 -05001812 }
1813 node = rb_next(node);
1814 if (!node)
1815 break;
1816 }
1817out:
Chris Masoncad321a2008-12-17 14:51:42 -05001818 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001819 return total_bytes;
1820}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001821
Chris Masond352ac62008-09-29 15:18:18 -04001822/*
1823 * set the private field for a given byte offset in the tree. If there isn't
1824 * an extent_state there already, this does nothing.
1825 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001826static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001827 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001828{
1829 struct rb_node *node;
1830 struct extent_state *state;
1831 int ret = 0;
1832
Chris Masoncad321a2008-12-17 14:51:42 -05001833 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001834 /*
1835 * this search will find all the extents that end after
1836 * our range starts.
1837 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001838 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001839 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001840 ret = -ENOENT;
1841 goto out;
1842 }
1843 state = rb_entry(node, struct extent_state, rb_node);
1844 if (state->start != start) {
1845 ret = -ENOENT;
1846 goto out;
1847 }
David Sterba47dc1962016-02-11 13:24:13 +01001848 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001849out:
Chris Masoncad321a2008-12-17 14:51:42 -05001850 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001851 return ret;
1852}
1853
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001854static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001855 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001856{
1857 struct rb_node *node;
1858 struct extent_state *state;
1859 int ret = 0;
1860
Chris Masoncad321a2008-12-17 14:51:42 -05001861 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001862 /*
1863 * this search will find all the extents that end after
1864 * our range starts.
1865 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001866 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001867 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001868 ret = -ENOENT;
1869 goto out;
1870 }
1871 state = rb_entry(node, struct extent_state, rb_node);
1872 if (state->start != start) {
1873 ret = -ENOENT;
1874 goto out;
1875 }
David Sterba47dc1962016-02-11 13:24:13 +01001876 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001877out:
Chris Masoncad321a2008-12-17 14:51:42 -05001878 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001879 return ret;
1880}
1881
1882/*
1883 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001884 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001885 * has the bits set. Otherwise, 1 is returned if any bit in the
1886 * range is found set.
1887 */
1888int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001889 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001890{
1891 struct extent_state *state = NULL;
1892 struct rb_node *node;
1893 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001894
Chris Masoncad321a2008-12-17 14:51:42 -05001895 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001896 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001897 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001898 node = &cached->rb_node;
1899 else
1900 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001901 while (node && start <= end) {
1902 state = rb_entry(node, struct extent_state, rb_node);
1903
1904 if (filled && state->start > start) {
1905 bitset = 0;
1906 break;
1907 }
1908
1909 if (state->start > end)
1910 break;
1911
1912 if (state->state & bits) {
1913 bitset = 1;
1914 if (!filled)
1915 break;
1916 } else if (filled) {
1917 bitset = 0;
1918 break;
1919 }
Chris Mason46562cec2009-09-23 20:23:16 -04001920
1921 if (state->end == (u64)-1)
1922 break;
1923
Chris Masond1310b22008-01-24 16:13:08 -05001924 start = state->end + 1;
1925 if (start > end)
1926 break;
1927 node = rb_next(node);
1928 if (!node) {
1929 if (filled)
1930 bitset = 0;
1931 break;
1932 }
1933 }
Chris Masoncad321a2008-12-17 14:51:42 -05001934 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001935 return bitset;
1936}
Chris Masond1310b22008-01-24 16:13:08 -05001937
1938/*
1939 * helper function to set a given page up to date if all the
1940 * extents in the tree for that page are up to date
1941 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001942static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001943{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001944 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001945 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001946 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001947 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001948}
1949
Josef Bacik7870d082017-05-05 11:57:15 -04001950int free_io_failure(struct extent_io_tree *failure_tree,
1951 struct extent_io_tree *io_tree,
1952 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001953{
1954 int ret;
1955 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001956
David Sterba47dc1962016-02-11 13:24:13 +01001957 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001958 ret = clear_extent_bits(failure_tree, rec->start,
1959 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001960 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001961 if (ret)
1962 err = ret;
1963
Josef Bacik7870d082017-05-05 11:57:15 -04001964 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001965 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001966 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001967 if (ret && !err)
1968 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001969
1970 kfree(rec);
1971 return err;
1972}
1973
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974/*
1975 * this bypasses the standard btrfs submit functions deliberately, as
1976 * the standard behavior is to write all copies in a raid setup. here we only
1977 * want to write the one bad copy. so we do the mapping for ourselves and issue
1978 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001979 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980 * actually prevents the read that triggered the error from finishing.
1981 * currently, there can be no more than two copies of every data bit. thus,
1982 * exactly one rewrite is required.
1983 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001984int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1985 u64 length, u64 logical, struct page *page,
1986 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001987{
1988 struct bio *bio;
1989 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001990 u64 map_length = 0;
1991 u64 sector;
1992 struct btrfs_bio *bbio = NULL;
1993 int ret;
1994
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001995 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001996 BUG_ON(!mirror_num);
1997
David Sterbac5e4c3d2017-06-12 17:29:41 +02001998 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001999 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002000 map_length = length;
2001
Filipe Mananab5de8d02016-05-27 22:21:27 +01002002 /*
2003 * Avoid races with device replace and make sure our bbio has devices
2004 * associated to its stripes that don't go away while we are doing the
2005 * read repair operation.
2006 */
2007 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002008 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002009 /*
2010 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2011 * to update all raid stripes, but here we just want to correct
2012 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2013 * stripe's dev and sector.
2014 */
2015 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2016 &map_length, &bbio, 0);
2017 if (ret) {
2018 btrfs_bio_counter_dec(fs_info);
2019 bio_put(bio);
2020 return -EIO;
2021 }
2022 ASSERT(bbio->mirror_num == 1);
2023 } else {
2024 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2025 &map_length, &bbio, mirror_num);
2026 if (ret) {
2027 btrfs_bio_counter_dec(fs_info);
2028 bio_put(bio);
2029 return -EIO;
2030 }
2031 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002032 }
Liu Boc7253282017-03-29 10:53:58 -07002033
2034 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002035 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002036 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002037 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002038 if (!dev || !dev->bdev ||
2039 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002040 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002041 bio_put(bio);
2042 return -EIO;
2043 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002044 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002045 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002046 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002047
Mike Christie4e49ea42016-06-05 14:31:41 -05002048 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002049 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002050 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002052 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002053 return -EIO;
2054 }
2055
David Sterbab14af3b2015-10-08 10:43:10 +02002056 btrfs_info_rl_in_rcu(fs_info,
2057 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002058 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002059 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002060 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002061 bio_put(bio);
2062 return 0;
2063}
2064
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002065int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2066 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002067{
Josef Bacikea466792012-03-26 21:57:36 -04002068 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002069 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002070 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002071
David Howellsbc98a422017-07-17 08:45:34 +01002072 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002073 return -EROFS;
2074
Josef Bacikea466792012-03-26 21:57:36 -04002075 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002076 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002077
Josef Bacik6ec656b2017-05-05 11:57:14 -04002078 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002079 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002080 if (ret)
2081 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002083 }
2084
2085 return ret;
2086}
2087
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088/*
2089 * each time an IO finishes, we do a fast check in the IO failure tree
2090 * to see if we need to process or clean up an io_failure_record
2091 */
Josef Bacik7870d082017-05-05 11:57:15 -04002092int clean_io_failure(struct btrfs_fs_info *fs_info,
2093 struct extent_io_tree *failure_tree,
2094 struct extent_io_tree *io_tree, u64 start,
2095 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096{
2097 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002098 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002099 struct extent_state *state;
2100 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002101 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002102
2103 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002104 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2105 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002106 if (!ret)
2107 return 0;
2108
Josef Bacik7870d082017-05-05 11:57:15 -04002109 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 if (ret)
2111 return 0;
2112
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 BUG_ON(!failrec->this_mirror);
2114
2115 if (failrec->in_validation) {
2116 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002117 btrfs_debug(fs_info,
2118 "clean_io_failure: freeing dummy error at %llu",
2119 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 goto out;
2121 }
David Howellsbc98a422017-07-17 08:45:34 +01002122 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002123 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002124
Josef Bacik7870d082017-05-05 11:57:15 -04002125 spin_lock(&io_tree->lock);
2126 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002127 failrec->start,
2128 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002129 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002130
Miao Xie883d0de2013-07-25 19:22:35 +08002131 if (state && state->start <= failrec->start &&
2132 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002133 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2134 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002135 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002136 repair_io_failure(fs_info, ino, start, failrec->len,
2137 failrec->logical, page, pg_offset,
2138 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139 }
2140 }
2141
2142out:
Josef Bacik7870d082017-05-05 11:57:15 -04002143 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002144
Miao Xie454ff3d2014-09-12 18:43:58 +08002145 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002146}
2147
Miao Xief6124962014-09-12 18:44:04 +08002148/*
2149 * Can be called when
2150 * - hold extent lock
2151 * - under ordered extent
2152 * - the inode is freeing
2153 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002154void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002155{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002156 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002157 struct io_failure_record *failrec;
2158 struct extent_state *state, *next;
2159
2160 if (RB_EMPTY_ROOT(&failure_tree->state))
2161 return;
2162
2163 spin_lock(&failure_tree->lock);
2164 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2165 while (state) {
2166 if (state->start > end)
2167 break;
2168
2169 ASSERT(state->end <= end);
2170
2171 next = next_state(state);
2172
David Sterba47dc1962016-02-11 13:24:13 +01002173 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002174 free_extent_state(state);
2175 kfree(failrec);
2176
2177 state = next;
2178 }
2179 spin_unlock(&failure_tree->lock);
2180}
2181
Miao Xie2fe63032014-09-12 18:43:59 +08002182int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002183 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002184{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002185 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002186 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002188 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2189 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2190 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002191 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002192 u64 logical;
2193
David Sterba47dc1962016-02-11 13:24:13 +01002194 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002195 if (ret) {
2196 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2197 if (!failrec)
2198 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002199
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002200 failrec->start = start;
2201 failrec->len = end - start + 1;
2202 failrec->this_mirror = 0;
2203 failrec->bio_flags = 0;
2204 failrec->in_validation = 0;
2205
2206 read_lock(&em_tree->lock);
2207 em = lookup_extent_mapping(em_tree, start, failrec->len);
2208 if (!em) {
2209 read_unlock(&em_tree->lock);
2210 kfree(failrec);
2211 return -EIO;
2212 }
2213
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002214 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002215 free_extent_map(em);
2216 em = NULL;
2217 }
2218 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002219 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002220 kfree(failrec);
2221 return -EIO;
2222 }
Miao Xie2fe63032014-09-12 18:43:59 +08002223
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224 logical = start - em->start;
2225 logical = em->block_start + logical;
2226 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2227 logical = em->block_start;
2228 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2229 extent_set_compress_type(&failrec->bio_flags,
2230 em->compress_type);
2231 }
Miao Xie2fe63032014-09-12 18:43:59 +08002232
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002233 btrfs_debug(fs_info,
2234 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2235 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002236
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237 failrec->logical = logical;
2238 free_extent_map(em);
2239
2240 /* set the bits in the private failure tree */
2241 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002242 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002244 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002245 /* set the bits in the inode's tree */
2246 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002247 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002248 if (ret < 0) {
2249 kfree(failrec);
2250 return ret;
2251 }
2252 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002253 btrfs_debug(fs_info,
2254 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2255 failrec->logical, failrec->start, failrec->len,
2256 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002257 /*
2258 * when data can be on disk more than twice, add to failrec here
2259 * (e.g. with a list for failed_mirror) to make
2260 * clean_io_failure() clean all those errors at once.
2261 */
2262 }
Miao Xie2fe63032014-09-12 18:43:59 +08002263
2264 *failrec_ret = failrec;
2265
2266 return 0;
2267}
2268
Ming Leia0b60d72017-12-18 20:22:11 +08002269bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002270 struct io_failure_record *failrec, int failed_mirror)
2271{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002273 int num_copies;
2274
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002275 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 if (num_copies == 1) {
2277 /*
2278 * we only have a single copy of the data, so don't bother with
2279 * all the retry and error correction code that follows. no
2280 * matter what the error is, it is very likely to persist.
2281 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002282 btrfs_debug(fs_info,
2283 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2284 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002285 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002286 }
2287
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002288 /*
2289 * there are two premises:
2290 * a) deliver good data to the caller
2291 * b) correct the bad sectors on disk
2292 */
Ming Leia0b60d72017-12-18 20:22:11 +08002293 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002294 /*
2295 * to fulfill b), we need to know the exact failing sectors, as
2296 * we don't want to rewrite any more than the failed ones. thus,
2297 * we need separate read requests for the failed bio
2298 *
2299 * if the following BUG_ON triggers, our validation request got
2300 * merged. we need separate requests for our algorithm to work.
2301 */
2302 BUG_ON(failrec->in_validation);
2303 failrec->in_validation = 1;
2304 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 } else {
2306 /*
2307 * we're ready to fulfill a) and b) alongside. get a good copy
2308 * of the failed sector and if we succeed, we have setup
2309 * everything for repair_io_failure to do the rest for us.
2310 */
2311 if (failrec->in_validation) {
2312 BUG_ON(failrec->this_mirror != failed_mirror);
2313 failrec->in_validation = 0;
2314 failrec->this_mirror = 0;
2315 }
2316 failrec->failed_mirror = failed_mirror;
2317 failrec->this_mirror++;
2318 if (failrec->this_mirror == failed_mirror)
2319 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002320 }
2321
Miao Xiefacc8a222013-07-25 19:22:34 +08002322 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002323 btrfs_debug(fs_info,
2324 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2325 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002326 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002327 }
2328
Liu Boc3cfb652017-07-13 15:00:50 -07002329 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002330}
2331
2332
2333struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2334 struct io_failure_record *failrec,
2335 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002336 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002339 struct bio *bio;
2340 struct btrfs_io_bio *btrfs_failed_bio;
2341 struct btrfs_io_bio *btrfs_bio;
2342
David Sterbac5e4c3d2017-06-12 17:29:41 +02002343 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002344 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002345 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002346 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002347 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002348 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002349
Miao Xiefacc8a222013-07-25 19:22:34 +08002350 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2351 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002352 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2353
2354 btrfs_bio = btrfs_io_bio(bio);
2355 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002356 icsum *= csum_size;
2357 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002358 csum_size);
2359 }
2360
Miao Xie2fe63032014-09-12 18:43:59 +08002361 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002362
Miao Xie2fe63032014-09-12 18:43:59 +08002363 return bio;
2364}
2365
2366/*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002367 * This is a generic handler for readpage errors. If other copies exist, read
2368 * those and write back good data to the failed position. Does not investigate
2369 * in remapping the failed extent elsewhere, hoping the device will be smart
2370 * enough to do this as needed
Miao Xie2fe63032014-09-12 18:43:59 +08002371 */
Miao Xie2fe63032014-09-12 18:43:59 +08002372static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2373 struct page *page, u64 start, u64 end,
2374 int failed_mirror)
2375{
2376 struct io_failure_record *failrec;
2377 struct inode *inode = page->mapping->host;
2378 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002379 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002380 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002381 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002382 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002383 int ret;
Christoph Hellwig8a2ee442019-02-15 19:13:07 +08002384 unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT;
Miao Xie2fe63032014-09-12 18:43:59 +08002385
Mike Christie1f7ad752016-06-05 14:31:51 -05002386 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002387
2388 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2389 if (ret)
2390 return ret;
2391
Ming Leia0b60d72017-12-18 20:22:11 +08002392 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002393 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002394 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002395 return -EIO;
2396 }
2397
Ming Leia0b60d72017-12-18 20:22:11 +08002398 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002399 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002400
2401 phy_offset >>= inode->i_sb->s_blocksize_bits;
2402 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2403 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002404 (int)phy_offset, failed_bio->bi_end_io,
2405 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002406 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002407
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002408 btrfs_debug(btrfs_sb(inode->i_sb),
2409 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2410 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002411
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002412 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002413 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002414 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002415 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002416 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002417 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002418 }
2419
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002420 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002421}
2422
Chris Masond1310b22008-01-24 16:13:08 -05002423/* lots and lots of room for performance fixes in the end_bio funcs */
2424
David Sterbab5227c02015-12-03 13:08:59 +01002425void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002426{
2427 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002428 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002429
Nikolay Borisovc6297322018-11-08 10:18:08 +02002430 btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002431
Jeff Mahoney87826df2012-02-15 16:23:57 +01002432 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002433 ClearPageUptodate(page);
2434 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002435 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002436 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002437 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002438}
2439
Chris Masond1310b22008-01-24 16:13:08 -05002440/*
2441 * after a writepage IO is done, we need to:
2442 * clear the uptodate bits on error
2443 * clear the writeback bits in the extent tree for this IO
2444 * end_page_writeback if the page has no more pending IO
2445 *
2446 * Scheduling is not allowed, so the extent state tree is expected
2447 * to have one and only one object corresponding to this IO.
2448 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002449static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002450{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002451 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002452 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002453 u64 start;
2454 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002455 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002456 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002457
David Sterbac09abff2017-07-13 18:10:07 +02002458 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002459 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002460 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002461 struct inode *inode = page->mapping->host;
2462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002463
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002464 /* We always issue full-page reads, but if some block
2465 * in a page fails to read, blk_update_request() will
2466 * advance bv_offset and adjust bv_len to compensate.
2467 * Print a warning for nonzero offsets, and an error
2468 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002469 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2470 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002471 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002472 "partial page write in btrfs with offset %u and length %u",
2473 bvec->bv_offset, bvec->bv_len);
2474 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002475 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002476 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002477 bvec->bv_offset, bvec->bv_len);
2478 }
Chris Masond1310b22008-01-24 16:13:08 -05002479
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002480 start = page_offset(page);
2481 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002482
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002483 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002484 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002485 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002486
Chris Masond1310b22008-01-24 16:13:08 -05002487 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002488}
2489
Miao Xie883d0de2013-07-25 19:22:35 +08002490static void
2491endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2492 int uptodate)
2493{
2494 struct extent_state *cached = NULL;
2495 u64 end = start + len - 1;
2496
2497 if (uptodate && tree->track_uptodate)
2498 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002499 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002500}
2501
Chris Masond1310b22008-01-24 16:13:08 -05002502/*
2503 * after a readpage IO is done, we need to:
2504 * clear the uptodate bits on error
2505 * set the uptodate bits if things worked
2506 * set the page up to date if all extents in the tree are uptodate
2507 * clear the lock bit in the extent tree
2508 * unlock the page if there are no other extents locked for it
2509 *
2510 * Scheduling is not allowed, so the extent state tree is expected
2511 * to have one and only one object corresponding to this IO.
2512 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002513static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002514{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002515 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002516 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002517 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002518 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002519 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002520 u64 start;
2521 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002522 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002523 u64 extent_start = 0;
2524 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002525 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002526 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002527 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08002528 struct bvec_iter_all iter_all;
Chris Masond1310b22008-01-24 16:13:08 -05002529
David Sterbac09abff2017-07-13 18:10:07 +02002530 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08002531 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Chris Masond1310b22008-01-24 16:13:08 -05002532 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002533 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002534 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002535 bool data_inode = btrfs_ino(BTRFS_I(inode))
2536 != BTRFS_BTREE_INODE_OBJECTID;
Arne Jansen507903b2011-04-06 10:02:20 +00002537
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002538 btrfs_debug(fs_info,
2539 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002540 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002541 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002542 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002543 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002544
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002545 /* We always issue full-page reads, but if some block
2546 * in a page fails to read, blk_update_request() will
2547 * advance bv_offset and adjust bv_len to compensate.
2548 * Print a warning for nonzero offsets, and an error
2549 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002550 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2551 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002552 btrfs_err(fs_info,
2553 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002554 bvec->bv_offset, bvec->bv_len);
2555 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002556 btrfs_info(fs_info,
2557 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002558 bvec->bv_offset, bvec->bv_len);
2559 }
Chris Masond1310b22008-01-24 16:13:08 -05002560
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002561 start = page_offset(page);
2562 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002563 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002564
Chris Mason9be33952013-05-17 18:30:14 -04002565 mirror = io_bio->mirror_num;
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002566 if (likely(uptodate)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002567 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2568 page, start, end,
2569 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002570 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002571 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002572 else
Josef Bacik7870d082017-05-05 11:57:15 -04002573 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2574 failure_tree, tree, start,
2575 page,
2576 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002577 }
Josef Bacikea466792012-03-26 21:57:36 -04002578
Miao Xief2a09da2013-07-25 19:22:33 +08002579 if (likely(uptodate))
2580 goto readpage_ok;
2581
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002582 if (data_inode) {
Liu Bo9d0d1c82017-03-24 15:04:50 -07002583
2584 /*
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002585 * The generic bio_readpage_error handles errors the
2586 * following way: If possible, new read requests are
2587 * created and submitted and will end up in
2588 * end_bio_extent_readpage as well (if we're lucky,
2589 * not in the !uptodate case). In that case it returns
2590 * 0 and we just go on with the next page in our bio.
2591 * If it can't handle the error it will return -EIO and
2592 * we remain responsible for that page.
Liu Bo9d0d1c82017-03-24 15:04:50 -07002593 */
Nikolay Borisov78e62c02018-11-22 10:17:49 +02002594 ret = bio_readpage_error(bio, offset, page, start, end,
2595 mirror);
2596 if (ret == 0) {
2597 uptodate = !bio->bi_status;
2598 offset += len;
2599 continue;
2600 }
2601 } else {
2602 struct extent_buffer *eb;
2603
2604 eb = (struct extent_buffer *)page->private;
2605 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
2606 eb->read_mirror = mirror;
2607 atomic_dec(&eb->io_pages);
2608 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
2609 &eb->bflags))
2610 btree_readahead_hook(eb, -EIO);
2611
2612 ret = -EIO;
Chris Mason7e383262008-04-09 16:28:12 -04002613 }
Miao Xief2a09da2013-07-25 19:22:33 +08002614readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002615 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002616 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002617 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002618 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002619
2620 /* Zero out the end if this page straddles i_size */
Johannes Thumshirn70730172018-12-05 15:23:03 +01002621 off = offset_in_page(i_size);
Liu Boa583c022014-08-19 23:32:22 +08002622 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002623 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002624 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002625 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002626 ClearPageUptodate(page);
2627 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002628 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002629 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002630 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002631
2632 if (unlikely(!uptodate)) {
2633 if (extent_len) {
2634 endio_readpage_release_extent(tree,
2635 extent_start,
2636 extent_len, 1);
2637 extent_start = 0;
2638 extent_len = 0;
2639 }
2640 endio_readpage_release_extent(tree, start,
2641 end - start + 1, 0);
2642 } else if (!extent_len) {
2643 extent_start = start;
2644 extent_len = end + 1 - start;
2645 } else if (extent_start + extent_len == start) {
2646 extent_len += end + 1 - start;
2647 } else {
2648 endio_readpage_release_extent(tree, extent_start,
2649 extent_len, uptodate);
2650 extent_start = start;
2651 extent_len = end + 1 - start;
2652 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002653 }
Chris Masond1310b22008-01-24 16:13:08 -05002654
Miao Xie883d0de2013-07-25 19:22:35 +08002655 if (extent_len)
2656 endio_readpage_release_extent(tree, extent_start, extent_len,
2657 uptodate);
David Sterbab3a0dd52018-11-22 17:16:49 +01002658 btrfs_io_bio_free_csum(io_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002659 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002660}
2661
Chris Mason9be33952013-05-17 18:30:14 -04002662/*
David Sterba184f9992017-06-12 17:29:39 +02002663 * Initialize the members up to but not including 'bio'. Use after allocating a
2664 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2665 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002666 */
David Sterba184f9992017-06-12 17:29:39 +02002667static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002668{
David Sterba184f9992017-06-12 17:29:39 +02002669 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2670}
2671
2672/*
David Sterba6e707bc2017-06-02 17:26:26 +02002673 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2674 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2675 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002676 */
David Sterbac821e7f32017-06-02 18:35:36 +02002677struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002678{
2679 struct bio *bio;
2680
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002681 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002682 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002683 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002684 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002685 return bio;
2686}
2687
David Sterba8b6c1d52017-06-02 17:48:13 +02002688struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002689{
Miao Xie23ea8e52014-09-12 18:43:54 +08002690 struct btrfs_io_bio *btrfs_bio;
2691 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002692
David Sterba6e707bc2017-06-02 17:26:26 +02002693 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002694 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002695 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002696 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002697 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002698 return new;
2699}
Chris Mason9be33952013-05-17 18:30:14 -04002700
David Sterbac5e4c3d2017-06-12 17:29:41 +02002701struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002702{
Miao Xiefacc8a222013-07-25 19:22:34 +08002703 struct bio *bio;
2704
David Sterba6e707bc2017-06-02 17:26:26 +02002705 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002706 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002707 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002708 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002709}
2710
Liu Boe4770942017-05-16 10:57:14 -07002711struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002712{
2713 struct bio *bio;
2714 struct btrfs_io_bio *btrfs_bio;
2715
2716 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002717 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002718 ASSERT(bio);
2719
2720 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002721 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002722
2723 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002724 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002725 return bio;
2726}
Chris Mason9be33952013-05-17 18:30:14 -04002727
David Sterba4b81ba42017-06-06 19:14:26 +02002728/*
2729 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002730 * @tree: tree so we can call our merge_bio hook
2731 * @wbc: optional writeback control for io accounting
2732 * @page: page to add to the bio
2733 * @pg_offset: offset of the new bio or to check whether we are adding
2734 * a contiguous page to the previous one
2735 * @size: portion of page that we want to write
2736 * @offset: starting offset in the page
2737 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002738 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002739 * @end_io_func: end_io callback for new bio
2740 * @mirror_num: desired mirror to read/write
2741 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2742 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002743 */
2744static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002745 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002746 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002747 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002748 struct block_device *bdev,
2749 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002750 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002751 int mirror_num,
2752 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002753 unsigned long bio_flags,
2754 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002755{
2756 int ret = 0;
2757 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002758 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002759 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002760
David Sterba5c2b1fd2017-06-06 19:22:55 +02002761 ASSERT(bio_ret);
2762
2763 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002764 bool contig;
2765 bool can_merge = true;
2766
Chris Masond1310b22008-01-24 16:13:08 -05002767 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002768 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002769 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002770 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002771 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002772
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002773 ASSERT(tree->ops);
2774 if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002775 can_merge = false;
2776
2777 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002778 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002779 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002780 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002781 if (ret < 0) {
2782 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002783 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002784 }
Chris Masond1310b22008-01-24 16:13:08 -05002785 bio = NULL;
2786 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002787 if (wbc)
2788 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002789 return 0;
2790 }
2791 }
Chris Masonc8b97812008-10-29 14:49:59 -04002792
David Sterba6273b7f2017-10-04 17:30:11 +02002793 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002794 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002795 bio->bi_end_io = end_io_func;
2796 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002797 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002798 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002799 if (wbc) {
2800 wbc_init_bio(wbc, bio);
2801 wbc_account_io(wbc, page, page_size);
2802 }
Chris Mason70dec802008-01-29 09:59:12 -05002803
David Sterba5c2b1fd2017-06-06 19:22:55 +02002804 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002805
2806 return ret;
2807}
2808
Eric Sandeen48a3b632013-04-25 20:41:01 +00002809static void attach_extent_buffer_page(struct extent_buffer *eb,
2810 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002811{
2812 if (!PagePrivate(page)) {
2813 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002814 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002815 set_page_private(page, (unsigned long)eb);
2816 } else {
2817 WARN_ON(page->private != (unsigned long)eb);
2818 }
2819}
2820
Chris Masond1310b22008-01-24 16:13:08 -05002821void set_page_extent_mapped(struct page *page)
2822{
2823 if (!PagePrivate(page)) {
2824 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002825 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002826 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002827 }
2828}
2829
Miao Xie125bac012013-07-25 19:22:37 +08002830static struct extent_map *
2831__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2832 u64 start, u64 len, get_extent_t *get_extent,
2833 struct extent_map **em_cached)
2834{
2835 struct extent_map *em;
2836
2837 if (em_cached && *em_cached) {
2838 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002839 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002840 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002841 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002842 return em;
2843 }
2844
2845 free_extent_map(em);
2846 *em_cached = NULL;
2847 }
2848
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002849 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002850 if (em_cached && !IS_ERR_OR_NULL(em)) {
2851 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002852 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002853 *em_cached = em;
2854 }
2855 return em;
2856}
Chris Masond1310b22008-01-24 16:13:08 -05002857/*
2858 * basic readpage implementation. Locked extent state structs are inserted
2859 * into the tree that are removed when the IO is done (by the end_io
2860 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002861 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002862 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002863 */
Miao Xie99740902013-07-25 19:22:36 +08002864static int __do_readpage(struct extent_io_tree *tree,
2865 struct page *page,
2866 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002867 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002868 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002869 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002870 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002871{
2872 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002873 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002874 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002875 u64 cur = start;
2876 u64 extent_offset;
2877 u64 last_byte = i_size_read(inode);
2878 u64 block_start;
2879 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002880 struct extent_map *em;
2881 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002882 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002883 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002884 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002885 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002886 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002887 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002888 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002889
2890 set_page_extent_mapped(page);
2891
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002892 if (!PageUptodate(page)) {
2893 if (cleancache_get_page(page) == 0) {
2894 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002895 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002896 goto out;
2897 }
2898 }
2899
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002900 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002901 char *userpage;
Johannes Thumshirn70730172018-12-05 15:23:03 +01002902 size_t zero_offset = offset_in_page(last_byte);
Chris Masonc8b97812008-10-29 14:49:59 -04002903
2904 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002905 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002906 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002907 memset(userpage + zero_offset, 0, iosize);
2908 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002909 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002910 }
2911 }
Chris Masond1310b22008-01-24 16:13:08 -05002912 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002913 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002914 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002915
Chris Masond1310b22008-01-24 16:13:08 -05002916 if (cur >= last_byte) {
2917 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002918 struct extent_state *cached = NULL;
2919
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002920 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002921 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002922 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002923 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002924 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002925 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002926 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002927 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002928 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002929 break;
2930 }
Miao Xie125bac012013-07-25 19:22:37 +08002931 em = __get_extent_map(inode, page, pg_offset, cur,
2932 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002933 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002934 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002935 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002936 break;
2937 }
Chris Masond1310b22008-01-24 16:13:08 -05002938 extent_offset = cur - em->start;
2939 BUG_ON(extent_map_end(em) <= cur);
2940 BUG_ON(end < cur);
2941
Li Zefan261507a02010-12-17 14:21:50 +08002942 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002943 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002944 extent_set_compress_type(&this_bio_flag,
2945 em->compress_type);
2946 }
Chris Masonc8b97812008-10-29 14:49:59 -04002947
Chris Masond1310b22008-01-24 16:13:08 -05002948 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2949 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002950 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002951 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2952 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002953 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002954 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002955 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002956 disk_io_size = iosize;
2957 }
Chris Masond1310b22008-01-24 16:13:08 -05002958 bdev = em->bdev;
2959 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002960 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2961 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002962
2963 /*
2964 * If we have a file range that points to a compressed extent
2965 * and it's followed by a consecutive file range that points to
2966 * to the same compressed extent (possibly with a different
2967 * offset and/or length, so it either points to the whole extent
2968 * or only part of it), we must make sure we do not submit a
2969 * single bio to populate the pages for the 2 ranges because
2970 * this makes the compressed extent read zero out the pages
2971 * belonging to the 2nd range. Imagine the following scenario:
2972 *
2973 * File layout
2974 * [0 - 8K] [8K - 24K]
2975 * | |
2976 * | |
2977 * points to extent X, points to extent X,
2978 * offset 4K, length of 8K offset 0, length 16K
2979 *
2980 * [extent X, compressed length = 4K uncompressed length = 16K]
2981 *
2982 * If the bio to read the compressed extent covers both ranges,
2983 * it will decompress extent X into the pages belonging to the
2984 * first range and then it will stop, zeroing out the remaining
2985 * pages that belong to the other range that points to extent X.
2986 * So here we make sure we submit 2 bios, one for the first
2987 * range and another one for the third range. Both will target
2988 * the same physical extent from disk, but we can't currently
2989 * make the compressed bio endio callback populate the pages
2990 * for both ranges because each compressed bio is tightly
2991 * coupled with a single extent map, and each range can have
2992 * an extent map with a different offset value relative to the
2993 * uncompressed data of our extent and different lengths. This
2994 * is a corner case so we prioritize correctness over
2995 * non-optimal behavior (submitting 2 bios for the same extent).
2996 */
2997 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
2998 prev_em_start && *prev_em_start != (u64)-1 &&
Filipe Manana8e928212019-02-14 15:17:20 +00002999 *prev_em_start != em->start)
Filipe Manana005efed2015-09-14 09:09:31 +01003000 force_bio_submit = true;
3001
3002 if (prev_em_start)
Filipe Manana8e928212019-02-14 15:17:20 +00003003 *prev_em_start = em->start;
Filipe Manana005efed2015-09-14 09:09:31 +01003004
Chris Masond1310b22008-01-24 16:13:08 -05003005 free_extent_map(em);
3006 em = NULL;
3007
3008 /* we've found a hole, just zero and go on */
3009 if (block_start == EXTENT_MAP_HOLE) {
3010 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003011 struct extent_state *cached = NULL;
3012
Cong Wang7ac687d2011-11-25 23:14:28 +08003013 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003014 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003015 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003016 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003017
3018 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003019 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003020 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003021 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003022 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003023 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003024 continue;
3025 }
3026 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003027 if (test_range_bit(tree, cur, cur_end,
3028 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003029 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003030 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003031 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003032 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003033 continue;
3034 }
Chris Mason70dec802008-01-29 09:59:12 -05003035 /* we have an inline extent but it didn't get marked up
3036 * to date. Error out
3037 */
3038 if (block_start == EXTENT_MAP_INLINE) {
3039 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003040 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003041 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003042 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003043 continue;
3044 }
Chris Masond1310b22008-01-24 16:13:08 -05003045
David Sterba4b81ba42017-06-06 19:14:26 +02003046 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003047 page, offset, disk_io_size,
3048 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003049 end_bio_extent_readpage, mirror_num,
3050 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003051 this_bio_flag,
3052 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003053 if (!ret) {
3054 nr++;
3055 *bio_flags = this_bio_flag;
3056 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003057 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003058 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003059 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003060 }
Chris Masond1310b22008-01-24 16:13:08 -05003061 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003062 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003063 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003064out:
Chris Masond1310b22008-01-24 16:13:08 -05003065 if (!nr) {
3066 if (!PageError(page))
3067 SetPageUptodate(page);
3068 unlock_page(page);
3069 }
Liu Bobaf863b2016-07-11 10:39:07 -07003070 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003071}
3072
Miao Xie99740902013-07-25 19:22:36 +08003073static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3074 struct page *pages[], int nr_pages,
3075 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003076 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003077 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003078 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003079 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003080{
3081 struct inode *inode;
3082 struct btrfs_ordered_extent *ordered;
3083 int index;
3084
3085 inode = pages[0]->mapping->host;
3086 while (1) {
3087 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003088 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003089 end - start + 1);
3090 if (!ordered)
3091 break;
3092 unlock_extent(tree, start, end);
3093 btrfs_start_ordered_extent(inode, ordered, 1);
3094 btrfs_put_ordered_extent(ordered);
3095 }
3096
3097 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003098 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003099 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003100 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003101 }
3102}
3103
3104static void __extent_readpages(struct extent_io_tree *tree,
3105 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003106 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003107 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003108 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003109 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003110{
Stefan Behrens35a36212013-08-14 18:12:25 +02003111 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003112 u64 end = 0;
3113 u64 page_start;
3114 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003115 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003116
3117 for (index = 0; index < nr_pages; index++) {
3118 page_start = page_offset(pages[index]);
3119 if (!end) {
3120 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003121 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003122 first_index = index;
3123 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003124 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003125 } else {
3126 __do_contiguous_readpages(tree, &pages[first_index],
3127 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003128 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003129 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003130 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003131 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003132 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003133 first_index = index;
3134 }
3135 }
3136
3137 if (end)
3138 __do_contiguous_readpages(tree, &pages[first_index],
3139 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003140 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003141 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003142}
3143
3144static int __extent_read_full_page(struct extent_io_tree *tree,
3145 struct page *page,
3146 get_extent_t *get_extent,
3147 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003148 unsigned long *bio_flags,
3149 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003150{
3151 struct inode *inode = page->mapping->host;
3152 struct btrfs_ordered_extent *ordered;
3153 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003154 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003155 int ret;
3156
3157 while (1) {
3158 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003159 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003160 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003161 if (!ordered)
3162 break;
3163 unlock_extent(tree, start, end);
3164 btrfs_start_ordered_extent(inode, ordered, 1);
3165 btrfs_put_ordered_extent(ordered);
3166 }
3167
Miao Xie125bac012013-07-25 19:22:37 +08003168 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003169 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003170 return ret;
3171}
3172
Chris Masond1310b22008-01-24 16:13:08 -05003173int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003174 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003175{
3176 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003177 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003178 int ret;
3179
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003180 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003181 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003182 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003183 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003184 return ret;
3185}
Chris Masond1310b22008-01-24 16:13:08 -05003186
David Sterba3d4b9492017-02-10 19:33:41 +01003187static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003188 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003189{
3190 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003191}
3192
Chris Masond1310b22008-01-24 16:13:08 -05003193/*
Chris Mason40f76582014-05-21 13:35:51 -07003194 * helper for __extent_writepage, doing all of the delayed allocation setup.
3195 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003196 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003197 * to write the page (copy into inline extent). In this case the IO has
3198 * been started and the page is already unlocked.
3199 *
3200 * This returns 0 if all went well (page still locked)
3201 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003202 */
Chris Mason40f76582014-05-21 13:35:51 -07003203static noinline_for_stack int writepage_delalloc(struct inode *inode,
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003204 struct page *page, struct writeback_control *wbc,
3205 u64 delalloc_start, unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003206{
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003207 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003208 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Lu Fengqi3522e902018-11-29 11:33:38 +08003209 bool found;
Chris Mason40f76582014-05-21 13:35:51 -07003210 u64 delalloc_to_write = 0;
3211 u64 delalloc_end = 0;
3212 int ret;
3213 int page_started = 0;
3214
Chris Mason40f76582014-05-21 13:35:51 -07003215
3216 while (delalloc_end < page_end) {
Lu Fengqi3522e902018-11-29 11:33:38 +08003217 found = find_lock_delalloc_range(inode, tree,
Chris Mason40f76582014-05-21 13:35:51 -07003218 page,
3219 &delalloc_start,
Nikolay Borisov917aace2018-10-26 14:43:20 +03003220 &delalloc_end);
Lu Fengqi3522e902018-11-29 11:33:38 +08003221 if (!found) {
Chris Mason40f76582014-05-21 13:35:51 -07003222 delalloc_start = delalloc_end + 1;
3223 continue;
3224 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003225 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3226 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003227 /* File system has been set read-only */
3228 if (ret) {
3229 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003230 /*
3231 * btrfs_run_delalloc_range should return < 0 for error
3232 * but just in case, we use > 0 here meaning the IO is
3233 * started, so we don't want to return > 0 unless
3234 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003235 */
3236 ret = ret < 0 ? ret : -EIO;
3237 goto done;
3238 }
3239 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003240 * delalloc_end is already one less than the total length, so
3241 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003242 */
3243 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003244 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003245 delalloc_start = delalloc_end + 1;
3246 }
3247 if (wbc->nr_to_write < delalloc_to_write) {
3248 int thresh = 8192;
3249
3250 if (delalloc_to_write < thresh * 2)
3251 thresh = delalloc_to_write;
3252 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3253 thresh);
3254 }
3255
3256 /* did the fill delalloc function already unlock and start
3257 * the IO?
3258 */
3259 if (page_started) {
3260 /*
3261 * we've unlocked the page, so we can't update
3262 * the mapping's writeback index, just update
3263 * nr_to_write.
3264 */
3265 wbc->nr_to_write -= *nr_written;
3266 return 1;
3267 }
3268
3269 ret = 0;
3270
3271done:
3272 return ret;
3273}
3274
3275/*
3276 * helper for __extent_writepage. This calls the writepage start hooks,
3277 * and does the loop to map the page into extents and bios.
3278 *
3279 * We return 1 if the IO is started and the page is unlocked,
3280 * 0 if all went well (page still locked)
3281 * < 0 if there were errors (page still locked)
3282 */
3283static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3284 struct page *page,
3285 struct writeback_control *wbc,
3286 struct extent_page_data *epd,
3287 loff_t i_size,
3288 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003289 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003290{
Chris Masond1310b22008-01-24 16:13:08 -05003291 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003292 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003293 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003294 u64 end;
3295 u64 cur = start;
3296 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003297 u64 block_start;
3298 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003299 struct extent_map *em;
3300 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003301 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003302 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003303 int ret = 0;
3304 int nr = 0;
3305 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003306
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003307 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3308 if (ret) {
3309 /* Fixup worker will requeue */
3310 if (ret == -EBUSY)
3311 wbc->pages_skipped++;
3312 else
3313 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003314
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003315 update_nr_written(wbc, nr_written);
3316 unlock_page(page);
3317 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003318 }
3319
Chris Mason11c83492009-04-20 15:50:09 -04003320 /*
3321 * we don't want to touch the inode after unlocking the page,
3322 * so we update the mapping writeback index now
3323 */
David Sterba3d4b9492017-02-10 19:33:41 +01003324 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003325
Chris Masond1310b22008-01-24 16:13:08 -05003326 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003327 if (i_size <= start) {
Nikolay Borisovc6297322018-11-08 10:18:08 +02003328 btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003329 goto done;
3330 }
3331
Chris Masond1310b22008-01-24 16:13:08 -05003332 blocksize = inode->i_sb->s_blocksize;
3333
3334 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003335 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003336 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003337
Chris Mason40f76582014-05-21 13:35:51 -07003338 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003339 btrfs_writepage_endio_finish_ordered(page, cur,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003340 page_end, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003341 break;
3342 }
David Sterba3c98c622017-06-23 04:01:08 +02003343 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003344 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003345 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003346 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003347 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003348 break;
3349 }
3350
3351 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003352 em_end = extent_map_end(em);
3353 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003354 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003355 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003356 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003357 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003358 bdev = em->bdev;
3359 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003360 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003361 free_extent_map(em);
3362 em = NULL;
3363
Chris Masonc8b97812008-10-29 14:49:59 -04003364 /*
3365 * compressed and inline extents are written through other
3366 * paths in the FS
3367 */
3368 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003369 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003370 /*
3371 * end_io notification does not happen here for
3372 * compressed extents
3373 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003374 if (!compressed)
3375 btrfs_writepage_endio_finish_ordered(page, cur,
3376 cur + iosize - 1,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003377 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003378 else if (compressed) {
3379 /* we don't want to end_page_writeback on
3380 * a compressed extent. this happens
3381 * elsewhere
3382 */
3383 nr++;
3384 }
3385
3386 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003387 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003388 continue;
3389 }
Chris Masonc8b97812008-10-29 14:49:59 -04003390
David Sterba5cdc84b2018-07-18 20:32:52 +02003391 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003392 if (!PageWriteback(page)) {
3393 btrfs_err(BTRFS_I(inode)->root->fs_info,
3394 "page %lu not writeback, cur %llu end %llu",
3395 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003396 }
David Sterba58409ed2016-05-04 11:46:10 +02003397
David Sterba4b81ba42017-06-06 19:14:26 +02003398 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003399 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003400 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003401 end_bio_extent_writepage,
3402 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003403 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003404 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003405 if (PageWriteback(page))
3406 end_page_writeback(page);
3407 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003408
Chris Masond1310b22008-01-24 16:13:08 -05003409 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003410 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003411 nr++;
3412 }
3413done:
Chris Mason40f76582014-05-21 13:35:51 -07003414 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003415 return ret;
3416}
3417
3418/*
3419 * the writepage semantics are similar to regular writepage. extent
3420 * records are inserted to lock ranges in the tree, and as dirty areas
3421 * are found, they are marked writeback. Then the lock bits are removed
3422 * and the end_io handler clears the writeback ranges
3423 */
3424static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003425 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003426{
3427 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003428 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003429 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003430 int ret;
3431 int nr = 0;
3432 size_t pg_offset = 0;
3433 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003434 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003435 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003436 unsigned long nr_written = 0;
3437
Liu Boff40adf2017-08-24 18:19:48 -06003438 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003439
3440 trace___extent_writepage(page, inode, wbc);
3441
3442 WARN_ON(!PageLocked(page));
3443
3444 ClearPageError(page);
3445
Johannes Thumshirn70730172018-12-05 15:23:03 +01003446 pg_offset = offset_in_page(i_size);
Chris Mason40f76582014-05-21 13:35:51 -07003447 if (page->index > end_index ||
3448 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003449 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003450 unlock_page(page);
3451 return 0;
3452 }
3453
3454 if (page->index == end_index) {
3455 char *userpage;
3456
3457 userpage = kmap_atomic(page);
3458 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003459 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003460 kunmap_atomic(userpage);
3461 flush_dcache_page(page);
3462 }
3463
3464 pg_offset = 0;
3465
3466 set_page_extent_mapped(page);
3467
Nikolay Borisov7789a552018-11-08 10:18:06 +02003468 if (!epd->extent_locked) {
Nikolay Borisov8cc02372018-11-08 10:18:07 +02003469 ret = writepage_delalloc(inode, page, wbc, start, &nr_written);
Nikolay Borisov7789a552018-11-08 10:18:06 +02003470 if (ret == 1)
3471 goto done_unlocked;
3472 if (ret)
3473 goto done;
3474 }
Chris Mason40f76582014-05-21 13:35:51 -07003475
3476 ret = __extent_writepage_io(inode, page, wbc, epd,
3477 i_size, nr_written, write_flags, &nr);
3478 if (ret == 1)
3479 goto done_unlocked;
3480
3481done:
Chris Masond1310b22008-01-24 16:13:08 -05003482 if (nr == 0) {
3483 /* make sure the mapping tag for page dirty gets cleared */
3484 set_page_writeback(page);
3485 end_page_writeback(page);
3486 }
Filipe Manana61391d52014-05-09 17:17:40 +01003487 if (PageError(page)) {
3488 ret = ret < 0 ? ret : -EIO;
3489 end_extent_writepage(page, ret, start, page_end);
3490 }
Chris Masond1310b22008-01-24 16:13:08 -05003491 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003492 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003493
Chris Mason11c83492009-04-20 15:50:09 -04003494done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003495 return 0;
3496}
3497
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003498void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003499{
NeilBrown74316202014-07-07 15:16:04 +10003500 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3501 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003502}
3503
Chris Mason0e378df2014-05-19 20:55:27 -07003504static noinline_for_stack int
3505lock_extent_buffer_for_io(struct extent_buffer *eb,
3506 struct btrfs_fs_info *fs_info,
3507 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003508{
David Sterbacc5e31a2018-03-01 18:20:27 +01003509 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003510 int flush = 0;
3511 int ret = 0;
3512
3513 if (!btrfs_try_tree_write_lock(eb)) {
3514 flush = 1;
3515 flush_write_bio(epd);
3516 btrfs_tree_lock(eb);
3517 }
3518
3519 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3520 btrfs_tree_unlock(eb);
3521 if (!epd->sync_io)
3522 return 0;
3523 if (!flush) {
3524 flush_write_bio(epd);
3525 flush = 1;
3526 }
Chris Masona098d8e82012-03-21 12:09:56 -04003527 while (1) {
3528 wait_on_extent_buffer_writeback(eb);
3529 btrfs_tree_lock(eb);
3530 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3531 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003532 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003533 }
3534 }
3535
Josef Bacik51561ff2012-07-20 16:25:24 -04003536 /*
3537 * We need to do this to prevent races in people who check if the eb is
3538 * under IO since we can end up having no IO bits set for a short period
3539 * of time.
3540 */
3541 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003542 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3543 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003544 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003545 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003546 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3547 -eb->len,
3548 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003549 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003550 } else {
3551 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003552 }
3553
3554 btrfs_tree_unlock(eb);
3555
3556 if (!ret)
3557 return ret;
3558
David Sterba65ad0102018-06-29 10:56:49 +02003559 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003561 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003562
3563 if (!trylock_page(p)) {
3564 if (!flush) {
3565 flush_write_bio(epd);
3566 flush = 1;
3567 }
3568 lock_page(p);
3569 }
3570 }
3571
3572 return ret;
3573}
3574
3575static void end_extent_buffer_writeback(struct extent_buffer *eb)
3576{
3577 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003578 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003579 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3580}
3581
Filipe Manana656f30d2014-09-26 12:25:56 +01003582static void set_btree_ioerr(struct page *page)
3583{
3584 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003585
3586 SetPageError(page);
3587 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3588 return;
3589
3590 /*
3591 * If writeback for a btree extent that doesn't belong to a log tree
3592 * failed, increment the counter transaction->eb_write_errors.
3593 * We do this because while the transaction is running and before it's
3594 * committing (when we call filemap_fdata[write|wait]_range against
3595 * the btree inode), we might have
3596 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3597 * returns an error or an error happens during writeback, when we're
3598 * committing the transaction we wouldn't know about it, since the pages
3599 * can be no longer dirty nor marked anymore for writeback (if a
3600 * subsequent modification to the extent buffer didn't happen before the
3601 * transaction commit), which makes filemap_fdata[write|wait]_range not
3602 * able to find the pages tagged with SetPageError at transaction
3603 * commit time. So if this happens we must abort the transaction,
3604 * otherwise we commit a super block with btree roots that point to
3605 * btree nodes/leafs whose content on disk is invalid - either garbage
3606 * or the content of some node/leaf from a past generation that got
3607 * cowed or deleted and is no longer valid.
3608 *
3609 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3610 * not be enough - we need to distinguish between log tree extents vs
3611 * non-log tree extents, and the next filemap_fdatawait_range() call
3612 * will catch and clear such errors in the mapping - and that call might
3613 * be from a log sync and not from a transaction commit. Also, checking
3614 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3615 * not done and would not be reliable - the eb might have been released
3616 * from memory and reading it back again means that flag would not be
3617 * set (since it's a runtime flag, not persisted on disk).
3618 *
3619 * Using the flags below in the btree inode also makes us achieve the
3620 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3621 * writeback for all dirty pages and before filemap_fdatawait_range()
3622 * is called, the writeback for all dirty pages had already finished
3623 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3624 * filemap_fdatawait_range() would return success, as it could not know
3625 * that writeback errors happened (the pages were no longer tagged for
3626 * writeback).
3627 */
3628 switch (eb->log_index) {
3629 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003630 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003631 break;
3632 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003633 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003634 break;
3635 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003636 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003637 break;
3638 default:
3639 BUG(); /* unexpected, logic error */
3640 }
3641}
3642
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003643static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003644{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003645 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003646 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003647 int i, done;
Ming Lei6dc4f102019-02-15 19:13:19 +08003648 struct bvec_iter_all iter_all;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003649
David Sterbac09abff2017-07-13 18:10:07 +02003650 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08003651 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003652 struct page *page = bvec->bv_page;
3653
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003654 eb = (struct extent_buffer *)page->private;
3655 BUG_ON(!eb);
3656 done = atomic_dec_and_test(&eb->io_pages);
3657
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003658 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003659 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003660 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003661 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003662 }
3663
3664 end_page_writeback(page);
3665
3666 if (!done)
3667 continue;
3668
3669 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003670 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003671
3672 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003673}
3674
Chris Mason0e378df2014-05-19 20:55:27 -07003675static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003676 struct btrfs_fs_info *fs_info,
3677 struct writeback_control *wbc,
3678 struct extent_page_data *epd)
3679{
3680 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003681 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003683 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003684 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003685 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003686 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003687 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003688
Filipe Manana656f30d2014-09-26 12:25:56 +01003689 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003690 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003692
Liu Bo851cd172016-09-23 13:44:44 -07003693 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3694 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003695 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003696 end = btrfs_node_key_ptr_offset(nritems);
3697
David Sterbab159fa22016-11-08 18:09:03 +01003698 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003699 } else {
3700 /*
3701 * leaf:
3702 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3703 */
3704 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003705 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003706 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003707 }
3708
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003709 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003710 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003711
3712 clear_page_dirty_for_io(p);
3713 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003714 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003715 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003716 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003717 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003718 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003719 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003720 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003721 if (PageWriteback(p))
3722 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003723 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3724 end_extent_buffer_writeback(eb);
3725 ret = -EIO;
3726 break;
3727 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003728 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003729 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003730 unlock_page(p);
3731 }
3732
3733 if (unlikely(ret)) {
3734 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003735 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003736 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003737 unlock_page(p);
3738 }
3739 }
3740
3741 return ret;
3742}
3743
3744int btree_write_cache_pages(struct address_space *mapping,
3745 struct writeback_control *wbc)
3746{
3747 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3748 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3749 struct extent_buffer *eb, *prev_eb = NULL;
3750 struct extent_page_data epd = {
3751 .bio = NULL,
3752 .tree = tree,
3753 .extent_locked = 0,
3754 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3755 };
3756 int ret = 0;
3757 int done = 0;
3758 int nr_to_write_done = 0;
3759 struct pagevec pvec;
3760 int nr_pages;
3761 pgoff_t index;
3762 pgoff_t end; /* Inclusive */
3763 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003764 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003765
Mel Gorman86679822017-11-15 17:37:52 -08003766 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003767 if (wbc->range_cyclic) {
3768 index = mapping->writeback_index; /* Start from prev offset */
3769 end = -1;
3770 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003771 index = wbc->range_start >> PAGE_SHIFT;
3772 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003773 scanned = 1;
3774 }
3775 if (wbc->sync_mode == WB_SYNC_ALL)
3776 tag = PAGECACHE_TAG_TOWRITE;
3777 else
3778 tag = PAGECACHE_TAG_DIRTY;
3779retry:
3780 if (wbc->sync_mode == WB_SYNC_ALL)
3781 tag_pages_for_writeback(mapping, index, end);
3782 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003783 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003784 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003785 unsigned i;
3786
3787 scanned = 1;
3788 for (i = 0; i < nr_pages; i++) {
3789 struct page *page = pvec.pages[i];
3790
3791 if (!PagePrivate(page))
3792 continue;
3793
Josef Bacikb5bae262012-09-14 13:43:01 -04003794 spin_lock(&mapping->private_lock);
3795 if (!PagePrivate(page)) {
3796 spin_unlock(&mapping->private_lock);
3797 continue;
3798 }
3799
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003800 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003801
3802 /*
3803 * Shouldn't happen and normally this would be a BUG_ON
3804 * but no sense in crashing the users box for something
3805 * we can survive anyway.
3806 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303807 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003808 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003809 continue;
3810 }
3811
Josef Bacikb5bae262012-09-14 13:43:01 -04003812 if (eb == prev_eb) {
3813 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003814 continue;
3815 }
3816
Josef Bacikb5bae262012-09-14 13:43:01 -04003817 ret = atomic_inc_not_zero(&eb->refs);
3818 spin_unlock(&mapping->private_lock);
3819 if (!ret)
3820 continue;
3821
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003822 prev_eb = eb;
3823 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3824 if (!ret) {
3825 free_extent_buffer(eb);
3826 continue;
3827 }
3828
3829 ret = write_one_eb(eb, fs_info, wbc, &epd);
3830 if (ret) {
3831 done = 1;
3832 free_extent_buffer(eb);
3833 break;
3834 }
3835 free_extent_buffer(eb);
3836
3837 /*
3838 * the filesystem may choose to bump up nr_to_write.
3839 * We have to make sure to honor the new nr_to_write
3840 * at any time
3841 */
3842 nr_to_write_done = wbc->nr_to_write <= 0;
3843 }
3844 pagevec_release(&pvec);
3845 cond_resched();
3846 }
3847 if (!scanned && !done) {
3848 /*
3849 * We hit the last page and there is more work to be done: wrap
3850 * back to the start of the file
3851 */
3852 scanned = 1;
3853 index = 0;
3854 goto retry;
3855 }
3856 flush_write_bio(&epd);
3857 return ret;
3858}
3859
Chris Masond1310b22008-01-24 16:13:08 -05003860/**
Chris Mason4bef0842008-09-08 11:18:08 -04003861 * 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 -05003862 * @mapping: address space structure to write
3863 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003864 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003865 *
3866 * If a page is already under I/O, write_cache_pages() skips it, even
3867 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3868 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3869 * and msync() need to guarantee that all the data which was dirty at the time
3870 * the call was made get new I/O started against them. If wbc->sync_mode is
3871 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3872 * existing IO to complete.
3873 */
David Sterba4242b642017-02-10 19:38:24 +01003874static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003875 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003876 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003877{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003878 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003879 int ret = 0;
3880 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003881 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003882 struct pagevec pvec;
3883 int nr_pages;
3884 pgoff_t index;
3885 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003886 pgoff_t done_index;
3887 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003888 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003889 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003890
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003891 /*
3892 * We have to hold onto the inode so that ordered extents can do their
3893 * work when the IO finishes. The alternative to this is failing to add
3894 * an ordered extent if the igrab() fails there and that is a huge pain
3895 * to deal with, so instead just hold onto the inode throughout the
3896 * writepages operation. If it fails here we are freeing up the inode
3897 * anyway and we'd rather not waste our time writing out stuff that is
3898 * going to be truncated anyway.
3899 */
3900 if (!igrab(inode))
3901 return 0;
3902
Mel Gorman86679822017-11-15 17:37:52 -08003903 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003904 if (wbc->range_cyclic) {
3905 index = mapping->writeback_index; /* Start from prev offset */
3906 end = -1;
3907 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003908 index = wbc->range_start >> PAGE_SHIFT;
3909 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003910 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3911 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003912 scanned = 1;
3913 }
Ethan Lien3cd24c62018-11-01 14:49:03 +08003914
3915 /*
3916 * We do the tagged writepage as long as the snapshot flush bit is set
3917 * and we are the first one who do the filemap_flush() on this inode.
3918 *
3919 * The nr_to_write == LONG_MAX is needed to make sure other flushers do
3920 * not race in and drop the bit.
3921 */
3922 if (range_whole && wbc->nr_to_write == LONG_MAX &&
3923 test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
3924 &BTRFS_I(inode)->runtime_flags))
3925 wbc->tagged_writepages = 1;
3926
3927 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003928 tag = PAGECACHE_TAG_TOWRITE;
3929 else
3930 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003931retry:
Ethan Lien3cd24c62018-11-01 14:49:03 +08003932 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Josef Bacikf7aaa062011-07-15 21:26:38 +00003933 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003934 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003935 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003936 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3937 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003938 unsigned i;
3939
3940 scanned = 1;
3941 for (i = 0; i < nr_pages; i++) {
3942 struct page *page = pvec.pages[i];
3943
Liu Boa91326672016-03-07 16:56:21 -08003944 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003945 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003946 * At this point we hold neither the i_pages lock nor
3947 * the page lock: the page may be truncated or
3948 * invalidated (changing page->mapping to NULL),
3949 * or even swizzled back from swapper_space to
3950 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003951 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003952 if (!trylock_page(page)) {
David Sterbaaab6e9e2017-11-30 18:00:02 +01003953 flush_write_bio(epd);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003954 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003955 }
Chris Masond1310b22008-01-24 16:13:08 -05003956
3957 if (unlikely(page->mapping != mapping)) {
3958 unlock_page(page);
3959 continue;
3960 }
3961
Chris Masond2c3f4f2008-11-19 12:44:22 -05003962 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003963 if (PageWriteback(page))
David Sterbaaab6e9e2017-11-30 18:00:02 +01003964 flush_write_bio(epd);
Chris Masond1310b22008-01-24 16:13:08 -05003965 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003966 }
Chris Masond1310b22008-01-24 16:13:08 -05003967
3968 if (PageWriteback(page) ||
3969 !clear_page_dirty_for_io(page)) {
3970 unlock_page(page);
3971 continue;
3972 }
3973
David Sterbaaab6e9e2017-11-30 18:00:02 +01003974 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003975
3976 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3977 unlock_page(page);
3978 ret = 0;
3979 }
Liu Boa91326672016-03-07 16:56:21 -08003980 if (ret < 0) {
3981 /*
3982 * done_index is set past this page,
3983 * so media errors will not choke
3984 * background writeout for the entire
3985 * file. This has consequences for
3986 * range_cyclic semantics (ie. it may
3987 * not be suitable for data integrity
3988 * writeout).
3989 */
3990 done_index = page->index + 1;
3991 done = 1;
3992 break;
3993 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003994
3995 /*
3996 * the filesystem may choose to bump up nr_to_write.
3997 * We have to make sure to honor the new nr_to_write
3998 * at any time
3999 */
4000 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004001 }
4002 pagevec_release(&pvec);
4003 cond_resched();
4004 }
Liu Bo894b36e2016-03-07 16:56:22 -08004005 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004006 /*
4007 * We hit the last page and there is more work to be done: wrap
4008 * back to the start of the file
4009 */
4010 scanned = 1;
4011 index = 0;
4012 goto retry;
4013 }
Liu Boa91326672016-03-07 16:56:21 -08004014
4015 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4016 mapping->writeback_index = done_index;
4017
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004018 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004019 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004020}
Chris Masond1310b22008-01-24 16:13:08 -05004021
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004022int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004023{
4024 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004025 struct extent_page_data epd = {
4026 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004027 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004028 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004029 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004030 };
Chris Masond1310b22008-01-24 16:13:08 -05004031
Chris Masond1310b22008-01-24 16:13:08 -05004032 ret = __extent_writepage(page, wbc, &epd);
4033
David Sterbae2932ee2017-06-23 04:16:17 +02004034 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004035 return ret;
4036}
Chris Masond1310b22008-01-24 16:13:08 -05004037
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004038int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004039 int mode)
4040{
4041 int ret = 0;
4042 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004043 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004044 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004045 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4046 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004047
4048 struct extent_page_data epd = {
4049 .bio = NULL,
4050 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004051 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004052 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004053 };
4054 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004055 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004056 .nr_to_write = nr_pages * 2,
4057 .range_start = start,
4058 .range_end = end + 1,
4059 };
4060
Chris Masond3977122009-01-05 21:25:51 -05004061 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004062 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004063 if (clear_page_dirty_for_io(page))
4064 ret = __extent_writepage(page, &wbc_writepages, &epd);
4065 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004066 btrfs_writepage_endio_finish_ordered(page, start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02004067 start + PAGE_SIZE - 1, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004068 unlock_page(page);
4069 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004070 put_page(page);
4071 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004072 }
4073
David Sterbae2932ee2017-06-23 04:16:17 +02004074 flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004075 return ret;
4076}
Chris Masond1310b22008-01-24 16:13:08 -05004077
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004078int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004079 struct writeback_control *wbc)
4080{
4081 int ret = 0;
4082 struct extent_page_data epd = {
4083 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004084 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004085 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004086 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004087 };
4088
David Sterba935db852017-06-23 04:30:28 +02004089 ret = extent_write_cache_pages(mapping, wbc, &epd);
David Sterbae2932ee2017-06-23 04:16:17 +02004090 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004091 return ret;
4092}
Chris Masond1310b22008-01-24 16:13:08 -05004093
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004094int extent_readpages(struct address_space *mapping, struct list_head *pages,
4095 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004096{
4097 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004098 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004099 struct page *pagepool[16];
Miao Xie125bac012013-07-25 19:22:37 +08004100 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004101 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004102 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004103 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004104
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004105 while (!list_empty(pages)) {
4106 for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) {
Nikolay Borisovf86196e2019-01-03 15:29:02 -08004107 struct page *page = lru_to_page(pages);
Chris Masond1310b22008-01-24 16:13:08 -05004108
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004109 prefetchw(&page->flags);
4110 list_del(&page->lru);
4111 if (add_to_page_cache_lru(page, mapping, page->index,
4112 readahead_gfp_mask(mapping))) {
4113 put_page(page);
4114 continue;
4115 }
4116
4117 pagepool[nr++] = page;
Chris Masond1310b22008-01-24 16:13:08 -05004118 }
Liu Bo67c96842012-07-20 21:43:09 -06004119
David Sterbae4d17ef2017-06-23 04:09:57 +02004120 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
Nikolay Borisov61ed3a12018-11-29 18:41:31 +02004121 &bio_flags, &prev_em_start);
Chris Masond1310b22008-01-24 16:13:08 -05004122 }
Liu Bo67c96842012-07-20 21:43:09 -06004123
Miao Xie125bac012013-07-25 19:22:37 +08004124 if (em_cached)
4125 free_extent_map(em_cached);
4126
Chris Masond1310b22008-01-24 16:13:08 -05004127 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004128 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004129 return 0;
4130}
Chris Masond1310b22008-01-24 16:13:08 -05004131
4132/*
4133 * basic invalidatepage code, this waits on any locked or writeback
4134 * ranges corresponding to the page, and then deletes any extent state
4135 * records from the tree
4136 */
4137int extent_invalidatepage(struct extent_io_tree *tree,
4138 struct page *page, unsigned long offset)
4139{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004140 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004141 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004142 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004143 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4144
Qu Wenruofda28322013-02-26 08:10:22 +00004145 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004146 if (start > end)
4147 return 0;
4148
David Sterbaff13db42015-12-03 14:30:40 +01004149 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004150 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004151 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004152 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4153 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004154 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004155 return 0;
4156}
Chris Masond1310b22008-01-24 16:13:08 -05004157
4158/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004159 * a helper for releasepage, this tests for areas of the page that
4160 * are locked or under IO and drops the related state bits if it is safe
4161 * to drop the page.
4162 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004163static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004164 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004165{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004166 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004167 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004168 int ret = 1;
4169
Chris Mason211f90e2008-07-18 11:56:15 -04004170 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004171 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004172 ret = 0;
4173 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004174 /*
4175 * at this point we can safely clear everything except the
4176 * locked bit and the nodatasum bit
4177 */
David Sterba66b0c882017-10-31 16:30:47 +01004178 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004179 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004180 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004181
4182 /* if clear_extent_bit failed for enomem reasons,
4183 * we can't allow the release to continue.
4184 */
4185 if (ret < 0)
4186 ret = 0;
4187 else
4188 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004189 }
4190 return ret;
4191}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004192
4193/*
Chris Masond1310b22008-01-24 16:13:08 -05004194 * a helper for releasepage. As long as there are no locked extents
4195 * in the range corresponding to the page, both state records and extent
4196 * map records are removed
4197 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004198int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004199{
4200 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004201 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004202 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004203 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4204 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4205 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004206
Mel Gormand0164ad2015-11-06 16:28:21 -08004207 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004208 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004209 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004210 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004211 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004212 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004213 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004214 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004215 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004216 break;
4217 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004218 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4219 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004220 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004221 free_extent_map(em);
4222 break;
4223 }
4224 if (!test_range_bit(tree, em->start,
4225 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004226 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004227 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004228 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4229 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004230 remove_extent_mapping(map, em);
4231 /* once for the rb tree */
4232 free_extent_map(em);
4233 }
4234 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004235 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004236
4237 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004238 free_extent_map(em);
4239 }
Chris Masond1310b22008-01-24 16:13:08 -05004240 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004241 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004242}
Chris Masond1310b22008-01-24 16:13:08 -05004243
Chris Masonec29ed52011-02-23 16:23:20 -05004244/*
4245 * helper function for fiemap, which doesn't want to see any holes.
4246 * This maps until we find something past 'last'
4247 */
4248static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004249 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004250{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004251 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004252 struct extent_map *em;
4253 u64 len;
4254
4255 if (offset >= last)
4256 return NULL;
4257
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304258 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004259 len = last - offset;
4260 if (len == 0)
4261 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004262 len = ALIGN(len, sectorsize);
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02004263 em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len);
David Sterbac7040052011-04-19 18:00:01 +02004264 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004265 return em;
4266
4267 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004268 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004269 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004270
4271 /* this is a hole, advance to the next extent */
4272 offset = extent_map_end(em);
4273 free_extent_map(em);
4274 if (offset >= last)
4275 break;
4276 }
4277 return NULL;
4278}
4279
Qu Wenruo47518322017-04-07 10:43:15 +08004280/*
4281 * To cache previous fiemap extent
4282 *
4283 * Will be used for merging fiemap extent
4284 */
4285struct fiemap_cache {
4286 u64 offset;
4287 u64 phys;
4288 u64 len;
4289 u32 flags;
4290 bool cached;
4291};
4292
4293/*
4294 * Helper to submit fiemap extent.
4295 *
4296 * Will try to merge current fiemap extent specified by @offset, @phys,
4297 * @len and @flags with cached one.
4298 * And only when we fails to merge, cached one will be submitted as
4299 * fiemap extent.
4300 *
4301 * Return value is the same as fiemap_fill_next_extent().
4302 */
4303static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4304 struct fiemap_cache *cache,
4305 u64 offset, u64 phys, u64 len, u32 flags)
4306{
4307 int ret = 0;
4308
4309 if (!cache->cached)
4310 goto assign;
4311
4312 /*
4313 * Sanity check, extent_fiemap() should have ensured that new
Andrea Gelmini52042d82018-11-28 12:05:13 +01004314 * fiemap extent won't overlap with cached one.
Qu Wenruo47518322017-04-07 10:43:15 +08004315 * Not recoverable.
4316 *
4317 * NOTE: Physical address can overlap, due to compression
4318 */
4319 if (cache->offset + cache->len > offset) {
4320 WARN_ON(1);
4321 return -EINVAL;
4322 }
4323
4324 /*
4325 * Only merges fiemap extents if
4326 * 1) Their logical addresses are continuous
4327 *
4328 * 2) Their physical addresses are continuous
4329 * So truly compressed (physical size smaller than logical size)
4330 * extents won't get merged with each other
4331 *
4332 * 3) Share same flags except FIEMAP_EXTENT_LAST
4333 * So regular extent won't get merged with prealloc extent
4334 */
4335 if (cache->offset + cache->len == offset &&
4336 cache->phys + cache->len == phys &&
4337 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4338 (flags & ~FIEMAP_EXTENT_LAST)) {
4339 cache->len += len;
4340 cache->flags |= flags;
4341 goto try_submit_last;
4342 }
4343
4344 /* Not mergeable, need to submit cached one */
4345 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4346 cache->len, cache->flags);
4347 cache->cached = false;
4348 if (ret)
4349 return ret;
4350assign:
4351 cache->cached = true;
4352 cache->offset = offset;
4353 cache->phys = phys;
4354 cache->len = len;
4355 cache->flags = flags;
4356try_submit_last:
4357 if (cache->flags & FIEMAP_EXTENT_LAST) {
4358 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4359 cache->phys, cache->len, cache->flags);
4360 cache->cached = false;
4361 }
4362 return ret;
4363}
4364
4365/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004366 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004367 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004368 * The last fiemap cache may still be cached in the following case:
4369 * 0 4k 8k
4370 * |<- Fiemap range ->|
4371 * |<------------ First extent ----------->|
4372 *
4373 * In this case, the first extent range will be cached but not emitted.
4374 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004375 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004376static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4377 struct fiemap_extent_info *fieinfo,
4378 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004379{
4380 int ret;
4381
4382 if (!cache->cached)
4383 return 0;
4384
Qu Wenruo47518322017-04-07 10:43:15 +08004385 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4386 cache->len, cache->flags);
4387 cache->cached = false;
4388 if (ret > 0)
4389 ret = 0;
4390 return ret;
4391}
4392
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004393int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004394 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004395{
Josef Bacik975f84f2010-11-23 19:36:57 +00004396 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004397 u64 off = start;
4398 u64 max = start + len;
4399 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004400 u32 found_type;
4401 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004402 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004403 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004404 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004405 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004406 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004407 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004408 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004409 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004410 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004411 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004412 u64 em_start = 0;
4413 u64 em_len = 0;
4414 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004415
4416 if (len == 0)
4417 return -EINVAL;
4418
Josef Bacik975f84f2010-11-23 19:36:57 +00004419 path = btrfs_alloc_path();
4420 if (!path)
4421 return -ENOMEM;
4422 path->leave_spinning = 1;
4423
Jeff Mahoneyda170662016-06-15 09:22:56 -04004424 start = round_down(start, btrfs_inode_sectorsize(inode));
4425 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004426
Chris Masonec29ed52011-02-23 16:23:20 -05004427 /*
4428 * lookup the last file extent. We're not using i_size here
4429 * because there might be preallocation past i_size
4430 */
David Sterbaf85b7372017-01-20 14:54:07 +01004431 ret = btrfs_lookup_file_extent(NULL, root, path,
4432 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004433 if (ret < 0) {
4434 btrfs_free_path(path);
4435 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004436 } else {
4437 WARN_ON(!ret);
4438 if (ret == 1)
4439 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004440 }
Liu Bo2d324f52016-05-17 17:21:48 -07004441
Josef Bacik975f84f2010-11-23 19:36:57 +00004442 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004443 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004444 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004445
Chris Masonec29ed52011-02-23 16:23:20 -05004446 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004447 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004448 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004449 /* have to trust i_size as the end */
4450 last = (u64)-1;
4451 last_for_get_extent = isize;
4452 } else {
4453 /*
4454 * remember the start of the last extent. There are a
4455 * bunch of different factors that go into the length of the
4456 * extent, so its much less complex to remember where it started
4457 */
4458 last = found_key.offset;
4459 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004460 }
Liu Bofe09e162013-09-22 12:54:23 +08004461 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004462
Chris Masonec29ed52011-02-23 16:23:20 -05004463 /*
4464 * we might have some extents allocated but more delalloc past those
4465 * extents. so, we trust isize unless the start of the last extent is
4466 * beyond isize
4467 */
4468 if (last < isize) {
4469 last = (u64)-1;
4470 last_for_get_extent = isize;
4471 }
4472
David Sterbaff13db42015-12-03 14:30:40 +01004473 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004474 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004475
David Sterbae3350e12017-06-23 04:09:57 +02004476 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004477 if (!em)
4478 goto out;
4479 if (IS_ERR(em)) {
4480 ret = PTR_ERR(em);
4481 goto out;
4482 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004483
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004484 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004485 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004486
Chris Masonea8efc72011-03-08 11:54:40 -05004487 /* break if the extent we found is outside the range */
4488 if (em->start >= max || extent_map_end(em) < off)
4489 break;
4490
4491 /*
4492 * get_extent may return an extent that starts before our
4493 * requested range. We have to make sure the ranges
4494 * we return to fiemap always move forward and don't
4495 * overlap, so adjust the offsets here
4496 */
4497 em_start = max(em->start, off);
4498
4499 /*
4500 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004501 * for adjusting the disk offset below. Only do this if the
4502 * extent isn't compressed since our in ram offset may be past
4503 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004504 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004505 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4506 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004507 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004508 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004509 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004510 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4511 disko = em->block_start + offset_in_extent;
4512 else
4513 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004514
Chris Masonea8efc72011-03-08 11:54:40 -05004515 /*
4516 * bump off for our next call to get_extent
4517 */
4518 off = extent_map_end(em);
4519 if (off >= max)
4520 end = 1;
4521
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004522 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004523 end = 1;
4524 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004525 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004526 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4527 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004528 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004529 flags |= (FIEMAP_EXTENT_DELALLOC |
4530 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004531 } else if (fieinfo->fi_extents_max) {
4532 u64 bytenr = em->block_start -
4533 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004534
Liu Bofe09e162013-09-22 12:54:23 +08004535 /*
4536 * As btrfs supports shared space, this information
4537 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004538 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4539 * then we're just getting a count and we can skip the
4540 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004541 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004542 ret = btrfs_check_shared(root,
4543 btrfs_ino(BTRFS_I(inode)),
4544 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004545 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004546 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004547 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004548 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004549 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004550 }
4551 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4552 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004553 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4554 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004555
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004556 free_extent_map(em);
4557 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004558 if ((em_start >= last) || em_len == (u64)-1 ||
4559 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004560 flags |= FIEMAP_EXTENT_LAST;
4561 end = 1;
4562 }
4563
Chris Masonec29ed52011-02-23 16:23:20 -05004564 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004565 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004566 if (IS_ERR(em)) {
4567 ret = PTR_ERR(em);
4568 goto out;
4569 }
4570 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004571 flags |= FIEMAP_EXTENT_LAST;
4572 end = 1;
4573 }
Qu Wenruo47518322017-04-07 10:43:15 +08004574 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4575 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004576 if (ret) {
4577 if (ret == 1)
4578 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004579 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004580 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004581 }
4582out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004583 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004584 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004585 free_extent_map(em);
4586out:
Liu Bofe09e162013-09-22 12:54:23 +08004587 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004588 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004589 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004590 return ret;
4591}
4592
Chris Mason727011e2010-08-06 13:21:20 -04004593static void __free_extent_buffer(struct extent_buffer *eb)
4594{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004595 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004596 kmem_cache_free(extent_buffer_cache, eb);
4597}
4598
Josef Bacika26e8c92014-03-28 17:07:27 -04004599int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004600{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004601 return (atomic_read(&eb->io_pages) ||
4602 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4603 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004604}
4605
Miao Xie897ca6e92010-10-26 20:57:29 -04004606/*
David Sterba55ac0132018-07-19 17:24:32 +02004607 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004608 */
David Sterba55ac0132018-07-19 17:24:32 +02004609static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004610{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004611 int i;
4612 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004613 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004614
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004615 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004616
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004617 num_pages = num_extent_pages(eb);
4618 for (i = 0; i < num_pages; i++) {
4619 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004620
Forrest Liu5d2361d2015-02-09 17:31:45 +08004621 if (!page)
4622 continue;
4623 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004624 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004625 /*
4626 * We do this since we'll remove the pages after we've
4627 * removed the eb from the radix tree, so we could race
4628 * and have this page now attached to the new eb. So
4629 * only clear page_private if it's still connected to
4630 * this eb.
4631 */
4632 if (PagePrivate(page) &&
4633 page->private == (unsigned long)eb) {
4634 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4635 BUG_ON(PageDirty(page));
4636 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004637 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004638 * We need to make sure we haven't be attached
4639 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004640 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004641 ClearPagePrivate(page);
4642 set_page_private(page, 0);
4643 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004644 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004645 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004646
4647 if (mapped)
4648 spin_unlock(&page->mapping->private_lock);
4649
Nicholas D Steeves01327612016-05-19 21:18:45 -04004650 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004651 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004652 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004653}
4654
4655/*
4656 * Helper for releasing the extent buffer.
4657 */
4658static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4659{
David Sterba55ac0132018-07-19 17:24:32 +02004660 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004661 __free_extent_buffer(eb);
4662}
4663
Josef Bacikf28491e2013-12-16 13:24:27 -05004664static struct extent_buffer *
4665__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004666 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004667{
4668 struct extent_buffer *eb = NULL;
4669
Michal Hockod1b5c562015-08-19 14:17:40 +02004670 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004671 eb->start = start;
4672 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004673 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004674 eb->bflags = 0;
4675 rwlock_init(&eb->lock);
4676 atomic_set(&eb->write_locks, 0);
4677 atomic_set(&eb->read_locks, 0);
4678 atomic_set(&eb->blocking_readers, 0);
4679 atomic_set(&eb->blocking_writers, 0);
4680 atomic_set(&eb->spinning_readers, 0);
4681 atomic_set(&eb->spinning_writers, 0);
4682 eb->lock_nested = 0;
4683 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
4699 return eb;
4700}
4701
4702struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4703{
David Sterbacc5e31a2018-03-01 18:20:27 +01004704 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004705 struct page *p;
4706 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004707 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004708
David Sterba3f556f72014-06-15 03:20:26 +02004709 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004710 if (new == NULL)
4711 return NULL;
4712
4713 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004714 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004715 if (!p) {
4716 btrfs_release_extent_buffer(new);
4717 return NULL;
4718 }
4719 attach_extent_buffer_page(new, p);
4720 WARN_ON(PageDirty(p));
4721 SetPageUptodate(p);
4722 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004723 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004724 }
4725
Josef Bacikdb7f3432013-08-07 14:54:37 -04004726 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004727 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004728
4729 return new;
4730}
4731
Omar Sandoval0f331222015-09-29 20:50:31 -07004732struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4733 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004734{
4735 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004736 int num_pages;
4737 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004738
David Sterba3f556f72014-06-15 03:20:26 +02004739 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004740 if (!eb)
4741 return NULL;
4742
David Sterba65ad0102018-06-29 10:56:49 +02004743 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004744 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004745 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004746 if (!eb->pages[i])
4747 goto err;
4748 }
4749 set_extent_buffer_uptodate(eb);
4750 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004751 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004752
4753 return eb;
4754err:
4755 for (; i > 0; i--)
4756 __free_page(eb->pages[i - 1]);
4757 __free_extent_buffer(eb);
4758 return NULL;
4759}
4760
Omar Sandoval0f331222015-09-29 20:50:31 -07004761struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004762 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004763{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004764 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004765}
4766
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004767static void check_buffer_tree_ref(struct extent_buffer *eb)
4768{
Chris Mason242e18c2013-01-29 17:49:37 -05004769 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004770 /* the ref bit is tricky. We have to make sure it is set
4771 * if we have the buffer dirty. Otherwise the
4772 * code to free a buffer can end up dropping a dirty
4773 * page
4774 *
4775 * Once the ref bit is set, it won't go away while the
4776 * buffer is dirty or in writeback, and it also won't
4777 * go away while we have the reference count on the
4778 * eb bumped.
4779 *
4780 * We can't just set the ref bit without bumping the
4781 * ref on the eb because free_extent_buffer might
4782 * see the ref bit and try to clear it. If this happens
4783 * free_extent_buffer might end up dropping our original
4784 * ref by mistake and freeing the page before we are able
4785 * to add one more ref.
4786 *
4787 * So bump the ref count first, then set the bit. If someone
4788 * beat us to it, drop the ref we added.
4789 */
Chris Mason242e18c2013-01-29 17:49:37 -05004790 refs = atomic_read(&eb->refs);
4791 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4792 return;
4793
Josef Bacik594831c2012-07-20 16:11:08 -04004794 spin_lock(&eb->refs_lock);
4795 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004796 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004797 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004798}
4799
Mel Gorman2457aec2014-06-04 16:10:31 -07004800static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4801 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004802{
David Sterbacc5e31a2018-03-01 18:20:27 +01004803 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004804
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004805 check_buffer_tree_ref(eb);
4806
David Sterba65ad0102018-06-29 10:56:49 +02004807 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004808 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004809 struct page *p = eb->pages[i];
4810
Mel Gorman2457aec2014-06-04 16:10:31 -07004811 if (p != accessed)
4812 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004813 }
4814}
4815
Josef Bacikf28491e2013-12-16 13:24:27 -05004816struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4817 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004818{
4819 struct extent_buffer *eb;
4820
4821 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004822 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004823 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004824 if (eb && atomic_inc_not_zero(&eb->refs)) {
4825 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004826 /*
4827 * Lock our eb's refs_lock to avoid races with
4828 * free_extent_buffer. When we get our eb it might be flagged
4829 * with EXTENT_BUFFER_STALE and another task running
4830 * free_extent_buffer might have seen that flag set,
4831 * eb->refs == 2, that the buffer isn't under IO (dirty and
4832 * writeback flags not set) and it's still in the tree (flag
4833 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4834 * of decrementing the extent buffer's reference count twice.
4835 * So here we could race and increment the eb's reference count,
4836 * clear its stale flag, mark it as dirty and drop our reference
4837 * before the other task finishes executing free_extent_buffer,
4838 * which would later result in an attempt to free an extent
4839 * buffer that is dirty.
4840 */
4841 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4842 spin_lock(&eb->refs_lock);
4843 spin_unlock(&eb->refs_lock);
4844 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004845 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004846 return eb;
4847 }
4848 rcu_read_unlock();
4849
4850 return NULL;
4851}
4852
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004853#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4854struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004855 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004856{
4857 struct extent_buffer *eb, *exists = NULL;
4858 int ret;
4859
4860 eb = find_extent_buffer(fs_info, start);
4861 if (eb)
4862 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004863 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004864 if (!eb)
4865 return NULL;
4866 eb->fs_info = fs_info;
4867again:
David Sterbae1860a72016-05-09 14:11:38 +02004868 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004869 if (ret)
4870 goto free_eb;
4871 spin_lock(&fs_info->buffer_lock);
4872 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004873 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004874 spin_unlock(&fs_info->buffer_lock);
4875 radix_tree_preload_end();
4876 if (ret == -EEXIST) {
4877 exists = find_extent_buffer(fs_info, start);
4878 if (exists)
4879 goto free_eb;
4880 else
4881 goto again;
4882 }
4883 check_buffer_tree_ref(eb);
4884 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4885
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004886 return eb;
4887free_eb:
4888 btrfs_release_extent_buffer(eb);
4889 return exists;
4890}
4891#endif
4892
Josef Bacikf28491e2013-12-16 13:24:27 -05004893struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004894 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004895{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004896 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004897 int num_pages;
4898 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004899 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004900 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004901 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004902 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004903 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004904 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004905 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004906
Jeff Mahoneyda170662016-06-15 09:22:56 -04004907 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004908 btrfs_err(fs_info, "bad tree block start %llu", start);
4909 return ERR_PTR(-EINVAL);
4910 }
4911
Josef Bacikf28491e2013-12-16 13:24:27 -05004912 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004913 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004914 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004915
David Sterba23d79d82014-06-15 02:55:29 +02004916 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004917 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004918 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004919
David Sterba65ad0102018-06-29 10:56:49 +02004920 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004921 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004922 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004923 if (!p) {
4924 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004925 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004926 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004927
4928 spin_lock(&mapping->private_lock);
4929 if (PagePrivate(p)) {
4930 /*
4931 * We could have already allocated an eb for this page
4932 * and attached one so lets see if we can get a ref on
4933 * the existing eb, and if we can we know it's good and
4934 * we can just return that one, else we know we can just
4935 * overwrite page->private.
4936 */
4937 exists = (struct extent_buffer *)p->private;
4938 if (atomic_inc_not_zero(&exists->refs)) {
4939 spin_unlock(&mapping->private_lock);
4940 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004941 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004942 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004943 goto free_eb;
4944 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004945 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004946
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004947 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004948 * Do this so attach doesn't complain and we need to
4949 * drop the ref the old guy had.
4950 */
4951 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004952 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004953 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004954 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004955 attach_extent_buffer_page(eb, p);
4956 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004957 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004958 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004959 if (!PageUptodate(p))
4960 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004961
4962 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004963 * We can't unlock the pages just yet since the extent buffer
4964 * hasn't been properly inserted in the radix tree, this
4965 * opens a race with btree_releasepage which can free a page
4966 * while we are still filling in all pages for the buffer and
4967 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05004968 */
Chris Masond1310b22008-01-24 16:13:08 -05004969 }
4970 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004971 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004972again:
David Sterbae1860a72016-05-09 14:11:38 +02004973 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004974 if (ret) {
4975 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004976 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004977 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004978
Josef Bacikf28491e2013-12-16 13:24:27 -05004979 spin_lock(&fs_info->buffer_lock);
4980 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004981 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05004982 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004983 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004984 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004985 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004986 if (exists)
4987 goto free_eb;
4988 else
Josef Bacik115391d2012-03-09 09:51:43 -05004989 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004990 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004991 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004992 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004993 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05004994
4995 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004996 * Now it's safe to unlock the pages because any calls to
4997 * btree_releasepage will correctly detect that a page belongs to a
4998 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05004999 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005000 for (i = 0; i < num_pages; i++)
5001 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005002 return eb;
5003
Chris Mason6af118ce2008-07-22 11:18:07 -04005004free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005005 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005006 for (i = 0; i < num_pages; i++) {
5007 if (eb->pages[i])
5008 unlock_page(eb->pages[i]);
5009 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005010
Miao Xie897ca6e92010-10-26 20:57:29 -04005011 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005012 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005013}
Chris Masond1310b22008-01-24 16:13:08 -05005014
Josef Bacik3083ee22012-03-09 16:01:49 -05005015static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5016{
5017 struct extent_buffer *eb =
5018 container_of(head, struct extent_buffer, rcu_head);
5019
5020 __free_extent_buffer(eb);
5021}
5022
David Sterbaf7a52a42013-04-26 14:56:29 +00005023static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005024{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005025 lockdep_assert_held(&eb->refs_lock);
5026
Josef Bacik3083ee22012-03-09 16:01:49 -05005027 WARN_ON(atomic_read(&eb->refs) == 0);
5028 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005029 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005030 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005031
Jan Schmidt815a51c2012-05-16 17:00:02 +02005032 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005033
Josef Bacikf28491e2013-12-16 13:24:27 -05005034 spin_lock(&fs_info->buffer_lock);
5035 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005036 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005037 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005038 } else {
5039 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005040 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005041
5042 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005043 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005044#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005045 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005046 __free_extent_buffer(eb);
5047 return 1;
5048 }
5049#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005050 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005051 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005052 }
5053 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005054
5055 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005056}
5057
Chris Masond1310b22008-01-24 16:13:08 -05005058void free_extent_buffer(struct extent_buffer *eb)
5059{
Chris Mason242e18c2013-01-29 17:49:37 -05005060 int refs;
5061 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005062 if (!eb)
5063 return;
5064
Chris Mason242e18c2013-01-29 17:49:37 -05005065 while (1) {
5066 refs = atomic_read(&eb->refs);
Nikolay Borisov46cc7752018-10-15 17:04:01 +03005067 if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3)
5068 || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) &&
5069 refs == 1))
Chris Mason242e18c2013-01-29 17:49:37 -05005070 break;
5071 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5072 if (old == refs)
5073 return;
5074 }
5075
Josef Bacik3083ee22012-03-09 16:01:49 -05005076 spin_lock(&eb->refs_lock);
5077 if (atomic_read(&eb->refs) == 2 &&
5078 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005079 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005080 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5081 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005082
Josef Bacik3083ee22012-03-09 16:01:49 -05005083 /*
5084 * I know this is terrible, but it's temporary until we stop tracking
5085 * the uptodate bits and such for the extent buffers.
5086 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005087 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005088}
Chris Masond1310b22008-01-24 16:13:08 -05005089
Josef Bacik3083ee22012-03-09 16:01:49 -05005090void free_extent_buffer_stale(struct extent_buffer *eb)
5091{
5092 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005093 return;
5094
Josef Bacik3083ee22012-03-09 16:01:49 -05005095 spin_lock(&eb->refs_lock);
5096 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5097
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005098 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005099 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5100 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005101 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005102}
5103
Chris Mason1d4284b2012-03-28 20:31:37 -04005104void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005105{
David Sterbacc5e31a2018-03-01 18:20:27 +01005106 int i;
5107 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005108 struct page *page;
5109
David Sterba65ad0102018-06-29 10:56:49 +02005110 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005111
5112 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005113 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005114 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005115 continue;
5116
Chris Masona61e6f22008-07-22 11:18:08 -04005117 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005118 WARN_ON(!PagePrivate(page));
5119
Chris Masond1310b22008-01-24 16:13:08 -05005120 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005121 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005122 if (!PageDirty(page))
5123 __xa_clear_mark(&page->mapping->i_pages,
5124 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005125 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005126 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005127 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005128 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005129 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005130}
Chris Masond1310b22008-01-24 16:13:08 -05005131
Liu Boabb57ef2018-09-14 01:44:42 +08005132bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005133{
David Sterbacc5e31a2018-03-01 18:20:27 +01005134 int i;
5135 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005136 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005137
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005138 check_buffer_tree_ref(eb);
5139
Chris Masonb9473432009-03-13 11:00:37 -04005140 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005141
David Sterba65ad0102018-06-29 10:56:49 +02005142 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005143 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005144 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5145
Liu Boabb57ef2018-09-14 01:44:42 +08005146 if (!was_dirty)
5147 for (i = 0; i < num_pages; i++)
5148 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005149
5150#ifdef CONFIG_BTRFS_DEBUG
5151 for (i = 0; i < num_pages; i++)
5152 ASSERT(PageDirty(eb->pages[i]));
5153#endif
5154
Chris Masonb9473432009-03-13 11:00:37 -04005155 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005156}
Chris Masond1310b22008-01-24 16:13:08 -05005157
David Sterba69ba3922015-12-03 13:08:59 +01005158void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005159{
David Sterbacc5e31a2018-03-01 18:20:27 +01005160 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005161 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005162 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005163
Chris Masonb4ce94d2009-02-04 09:25:08 -05005164 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005165 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005166 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005167 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005168 if (page)
5169 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005170 }
Chris Mason1259ab72008-05-12 13:39:03 -04005171}
5172
David Sterba09c25a82015-12-03 13:08:59 +01005173void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005174{
David Sterbacc5e31a2018-03-01 18:20:27 +01005175 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005176 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005177 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005178
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005179 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005180 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005181 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005182 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005183 SetPageUptodate(page);
5184 }
Chris Masond1310b22008-01-24 16:13:08 -05005185}
Chris Masond1310b22008-01-24 16:13:08 -05005186
Chris Masond1310b22008-01-24 16:13:08 -05005187int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005188 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005189{
David Sterbacc5e31a2018-03-01 18:20:27 +01005190 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005191 struct page *page;
5192 int err;
5193 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005194 int locked_pages = 0;
5195 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005196 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005197 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005198 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005199 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005200
Chris Masonb4ce94d2009-02-04 09:25:08 -05005201 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005202 return 0;
5203
David Sterba65ad0102018-06-29 10:56:49 +02005204 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005205 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005206 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005207 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005208 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005209 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005210 } else {
5211 lock_page(page);
5212 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005213 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005214 }
5215 /*
5216 * We need to firstly lock all pages to make sure that
5217 * the uptodate bit of our pages won't be affected by
5218 * clear_extent_buffer_uptodate().
5219 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005220 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005221 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005222 if (!PageUptodate(page)) {
5223 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005224 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005225 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005226 }
Liu Bo2571e732016-08-03 12:33:01 -07005227
Chris Masonce9adaa2008-04-09 16:28:12 -04005228 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005229 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005230 goto unlock_exit;
5231 }
5232
Filipe Manana656f30d2014-09-26 12:25:56 +01005233 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005234 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005235 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005236 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005237 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005238
Chris Masonce9adaa2008-04-09 16:28:12 -04005239 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005240 if (ret) {
5241 atomic_dec(&eb->io_pages);
5242 unlock_page(page);
5243 continue;
5244 }
5245
Chris Masonf1885912008-04-09 16:28:12 -04005246 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005247 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005248 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005249 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005250 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005251 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005252 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005253 /*
5254 * We use &bio in above __extent_read_full_page,
5255 * so we ensure that if it returns error, the
5256 * current page fails to add itself to bio and
5257 * it's been unlocked.
5258 *
5259 * We must dec io_pages by ourselves.
5260 */
5261 atomic_dec(&eb->io_pages);
5262 }
Chris Masond1310b22008-01-24 16:13:08 -05005263 } else {
5264 unlock_page(page);
5265 }
5266 }
5267
Jeff Mahoney355808c2011-10-03 23:23:14 -04005268 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005269 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005270 if (err)
5271 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005272 }
Chris Masona86c12c2008-02-07 10:50:54 -05005273
Arne Jansenbb82ab82011-06-10 14:06:53 +02005274 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005275 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005276
Josef Bacik8436ea912016-09-02 15:40:03 -04005277 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005278 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005279 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005280 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005281 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005282 }
Chris Masond3977122009-01-05 21:25:51 -05005283
Chris Masond1310b22008-01-24 16:13:08 -05005284 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005285
5286unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005287 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005288 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005289 page = eb->pages[locked_pages];
5290 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005291 }
5292 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005293}
Chris Masond1310b22008-01-24 16:13:08 -05005294
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005295void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5296 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005297{
5298 size_t cur;
5299 size_t offset;
5300 struct page *page;
5301 char *kaddr;
5302 char *dst = (char *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005303 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005304 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005305
Liu Bof716abd2017-08-09 11:10:16 -06005306 if (start + len > eb->len) {
5307 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5308 eb->start, eb->len, start, len);
5309 memset(dst, 0, len);
5310 return;
5311 }
Chris Masond1310b22008-01-24 16:13:08 -05005312
Johannes Thumshirn70730172018-12-05 15:23:03 +01005313 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005314
Chris Masond3977122009-01-05 21:25:51 -05005315 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005316 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005317
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005318 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005319 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005320 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005321
5322 dst += cur;
5323 len -= cur;
5324 offset = 0;
5325 i++;
5326 }
5327}
Chris Masond1310b22008-01-24 16:13:08 -05005328
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005329int read_extent_buffer_to_user(const struct extent_buffer *eb,
5330 void __user *dstv,
5331 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005332{
5333 size_t cur;
5334 size_t offset;
5335 struct page *page;
5336 char *kaddr;
5337 char __user *dst = (char __user *)dstv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005338 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005339 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005340 int ret = 0;
5341
5342 WARN_ON(start > eb->len);
5343 WARN_ON(start + len > eb->start + eb->len);
5344
Johannes Thumshirn70730172018-12-05 15:23:03 +01005345 offset = offset_in_page(start_offset + start);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005346
5347 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005348 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005349
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005350 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005351 kaddr = page_address(page);
5352 if (copy_to_user(dst, kaddr + offset, cur)) {
5353 ret = -EFAULT;
5354 break;
5355 }
5356
5357 dst += cur;
5358 len -= cur;
5359 offset = 0;
5360 i++;
5361 }
5362
5363 return ret;
5364}
5365
Liu Bo415b35a2016-06-17 19:16:21 -07005366/*
5367 * return 0 if the item is found within a page.
5368 * return 1 if the item spans two pages.
5369 * return -EINVAL otherwise.
5370 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005371int map_private_extent_buffer(const struct extent_buffer *eb,
5372 unsigned long start, unsigned long min_len,
5373 char **map, unsigned long *map_start,
5374 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005375{
Johannes Thumshirncc2c39d2018-11-28 09:54:54 +01005376 size_t offset;
Chris Masond1310b22008-01-24 16:13:08 -05005377 char *kaddr;
5378 struct page *p;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005379 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005380 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005381 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005382 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005383
Liu Bof716abd2017-08-09 11:10:16 -06005384 if (start + min_len > eb->len) {
5385 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5386 eb->start, eb->len, start, min_len);
5387 return -EINVAL;
5388 }
5389
Chris Masond1310b22008-01-24 16:13:08 -05005390 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005391 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005392
5393 if (i == 0) {
5394 offset = start_offset;
5395 *map_start = 0;
5396 } else {
5397 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005398 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005399 }
Chris Masond3977122009-01-05 21:25:51 -05005400
David Sterbafb85fc92014-07-31 01:03:53 +02005401 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005402 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005403 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005404 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005405 return 0;
5406}
Chris Masond1310b22008-01-24 16:13:08 -05005407
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005408int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5409 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005410{
5411 size_t cur;
5412 size_t offset;
5413 struct page *page;
5414 char *kaddr;
5415 char *ptr = (char *)ptrv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005416 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005417 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005418 int ret = 0;
5419
5420 WARN_ON(start > eb->len);
5421 WARN_ON(start + len > eb->start + eb->len);
5422
Johannes Thumshirn70730172018-12-05 15:23:03 +01005423 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005424
Chris Masond3977122009-01-05 21:25:51 -05005425 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005426 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005427
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005428 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005429
Chris Masona6591712011-07-19 12:04:14 -04005430 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005431 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005432 if (ret)
5433 break;
5434
5435 ptr += cur;
5436 len -= cur;
5437 offset = 0;
5438 i++;
5439 }
5440 return ret;
5441}
Chris Masond1310b22008-01-24 16:13:08 -05005442
David Sterbaf157bf72016-11-09 17:43:38 +01005443void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5444 const void *srcv)
5445{
5446 char *kaddr;
5447
5448 WARN_ON(!PageUptodate(eb->pages[0]));
5449 kaddr = page_address(eb->pages[0]);
5450 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5451 BTRFS_FSID_SIZE);
5452}
5453
5454void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5455{
5456 char *kaddr;
5457
5458 WARN_ON(!PageUptodate(eb->pages[0]));
5459 kaddr = page_address(eb->pages[0]);
5460 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5461 BTRFS_FSID_SIZE);
5462}
5463
Chris Masond1310b22008-01-24 16:13:08 -05005464void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5465 unsigned long start, unsigned long len)
5466{
5467 size_t cur;
5468 size_t offset;
5469 struct page *page;
5470 char *kaddr;
5471 char *src = (char *)srcv;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005472 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005473 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005474
5475 WARN_ON(start > eb->len);
5476 WARN_ON(start + len > eb->start + eb->len);
5477
Johannes Thumshirn70730172018-12-05 15:23:03 +01005478 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005479
Chris Masond3977122009-01-05 21:25:51 -05005480 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005481 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005482 WARN_ON(!PageUptodate(page));
5483
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005484 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005485 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005486 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005487
5488 src += cur;
5489 len -= cur;
5490 offset = 0;
5491 i++;
5492 }
5493}
Chris Masond1310b22008-01-24 16:13:08 -05005494
David Sterbab159fa22016-11-08 18:09:03 +01005495void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5496 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005497{
5498 size_t cur;
5499 size_t offset;
5500 struct page *page;
5501 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005502 size_t start_offset = offset_in_page(eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005503 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005504
5505 WARN_ON(start > eb->len);
5506 WARN_ON(start + len > eb->start + eb->len);
5507
Johannes Thumshirn70730172018-12-05 15:23:03 +01005508 offset = offset_in_page(start_offset + start);
Chris Masond1310b22008-01-24 16:13:08 -05005509
Chris Masond3977122009-01-05 21:25:51 -05005510 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005511 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005512 WARN_ON(!PageUptodate(page));
5513
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005514 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005515 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005516 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005517
5518 len -= cur;
5519 offset = 0;
5520 i++;
5521 }
5522}
Chris Masond1310b22008-01-24 16:13:08 -05005523
David Sterba58e80122016-11-08 18:30:31 +01005524void copy_extent_buffer_full(struct extent_buffer *dst,
5525 struct extent_buffer *src)
5526{
5527 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005528 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005529
5530 ASSERT(dst->len == src->len);
5531
David Sterba65ad0102018-06-29 10:56:49 +02005532 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005533 for (i = 0; i < num_pages; i++)
5534 copy_page(page_address(dst->pages[i]),
5535 page_address(src->pages[i]));
5536}
5537
Chris Masond1310b22008-01-24 16:13:08 -05005538void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5539 unsigned long dst_offset, unsigned long src_offset,
5540 unsigned long len)
5541{
5542 u64 dst_len = dst->len;
5543 size_t cur;
5544 size_t offset;
5545 struct page *page;
5546 char *kaddr;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005547 size_t start_offset = offset_in_page(dst->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005548 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005549
5550 WARN_ON(src->len != dst_len);
5551
Johannes Thumshirn70730172018-12-05 15:23:03 +01005552 offset = offset_in_page(start_offset + dst_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005553
Chris Masond3977122009-01-05 21:25:51 -05005554 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005555 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005556 WARN_ON(!PageUptodate(page));
5557
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005558 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005559
Chris Masona6591712011-07-19 12:04:14 -04005560 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005561 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005562
5563 src_offset += cur;
5564 len -= cur;
5565 offset = 0;
5566 i++;
5567 }
5568}
Chris Masond1310b22008-01-24 16:13:08 -05005569
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005570/*
5571 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5572 * given bit number
5573 * @eb: the extent buffer
5574 * @start: offset of the bitmap item in the extent buffer
5575 * @nr: bit number
5576 * @page_index: return index of the page in the extent buffer that contains the
5577 * given bit number
5578 * @page_offset: return offset into the page given by page_index
5579 *
5580 * This helper hides the ugliness of finding the byte in an extent buffer which
5581 * contains a given bit.
5582 */
5583static inline void eb_bitmap_offset(struct extent_buffer *eb,
5584 unsigned long start, unsigned long nr,
5585 unsigned long *page_index,
5586 size_t *page_offset)
5587{
Johannes Thumshirn70730172018-12-05 15:23:03 +01005588 size_t start_offset = offset_in_page(eb->start);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005589 size_t byte_offset = BIT_BYTE(nr);
5590 size_t offset;
5591
5592 /*
5593 * The byte we want is the offset of the extent buffer + the offset of
5594 * the bitmap item in the extent buffer + the offset of the byte in the
5595 * bitmap item.
5596 */
5597 offset = start_offset + start + byte_offset;
5598
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005599 *page_index = offset >> PAGE_SHIFT;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005600 *page_offset = offset_in_page(offset);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005601}
5602
5603/**
5604 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5605 * @eb: the extent buffer
5606 * @start: offset of the bitmap item in the extent buffer
5607 * @nr: bit number to test
5608 */
5609int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5610 unsigned long nr)
5611{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005612 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005613 struct page *page;
5614 unsigned long i;
5615 size_t offset;
5616
5617 eb_bitmap_offset(eb, start, nr, &i, &offset);
5618 page = eb->pages[i];
5619 WARN_ON(!PageUptodate(page));
5620 kaddr = page_address(page);
5621 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5622}
5623
5624/**
5625 * extent_buffer_bitmap_set - set an area of a bitmap
5626 * @eb: the extent buffer
5627 * @start: offset of the bitmap item in the extent buffer
5628 * @pos: bit number of the first bit
5629 * @len: number of bits to set
5630 */
5631void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5632 unsigned long pos, unsigned long len)
5633{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005634 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005635 struct page *page;
5636 unsigned long i;
5637 size_t offset;
5638 const unsigned int size = pos + len;
5639 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005640 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005641
5642 eb_bitmap_offset(eb, start, pos, &i, &offset);
5643 page = eb->pages[i];
5644 WARN_ON(!PageUptodate(page));
5645 kaddr = page_address(page);
5646
5647 while (len >= bits_to_set) {
5648 kaddr[offset] |= mask_to_set;
5649 len -= bits_to_set;
5650 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005651 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005652 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005653 offset = 0;
5654 page = eb->pages[++i];
5655 WARN_ON(!PageUptodate(page));
5656 kaddr = page_address(page);
5657 }
5658 }
5659 if (len) {
5660 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5661 kaddr[offset] |= mask_to_set;
5662 }
5663}
5664
5665
5666/**
5667 * extent_buffer_bitmap_clear - clear an area of a bitmap
5668 * @eb: the extent buffer
5669 * @start: offset of the bitmap item in the extent buffer
5670 * @pos: bit number of the first bit
5671 * @len: number of bits to clear
5672 */
5673void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5674 unsigned long pos, unsigned long len)
5675{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005676 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005677 struct page *page;
5678 unsigned long i;
5679 size_t offset;
5680 const unsigned int size = pos + len;
5681 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005682 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005683
5684 eb_bitmap_offset(eb, start, pos, &i, &offset);
5685 page = eb->pages[i];
5686 WARN_ON(!PageUptodate(page));
5687 kaddr = page_address(page);
5688
5689 while (len >= bits_to_clear) {
5690 kaddr[offset] &= ~mask_to_clear;
5691 len -= bits_to_clear;
5692 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005693 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005694 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005695 offset = 0;
5696 page = eb->pages[++i];
5697 WARN_ON(!PageUptodate(page));
5698 kaddr = page_address(page);
5699 }
5700 }
5701 if (len) {
5702 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5703 kaddr[offset] &= ~mask_to_clear;
5704 }
5705}
5706
Sergei Trofimovich33872062011-04-11 21:52:52 +00005707static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5708{
5709 unsigned long distance = (src > dst) ? src - dst : dst - src;
5710 return distance < len;
5711}
5712
Chris Masond1310b22008-01-24 16:13:08 -05005713static void copy_pages(struct page *dst_page, struct page *src_page,
5714 unsigned long dst_off, unsigned long src_off,
5715 unsigned long len)
5716{
Chris Masona6591712011-07-19 12:04:14 -04005717 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005718 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005719 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005720
Sergei Trofimovich33872062011-04-11 21:52:52 +00005721 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005722 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005723 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005724 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005725 if (areas_overlap(src_off, dst_off, len))
5726 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005727 }
Chris Masond1310b22008-01-24 16:13:08 -05005728
Chris Mason727011e2010-08-06 13:21:20 -04005729 if (must_memmove)
5730 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5731 else
5732 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005733}
5734
5735void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5736 unsigned long src_offset, unsigned long len)
5737{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005738 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005739 size_t cur;
5740 size_t dst_off_in_page;
5741 size_t src_off_in_page;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005742 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005743 unsigned long dst_i;
5744 unsigned long src_i;
5745
5746 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005747 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005748 "memmove bogus src_offset %lu move len %lu dst len %lu",
5749 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005750 BUG_ON(1);
5751 }
5752 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005753 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005754 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5755 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005756 BUG_ON(1);
5757 }
5758
Chris Masond3977122009-01-05 21:25:51 -05005759 while (len > 0) {
Johannes Thumshirn70730172018-12-05 15:23:03 +01005760 dst_off_in_page = offset_in_page(start_offset + dst_offset);
5761 src_off_in_page = offset_in_page(start_offset + src_offset);
Chris Masond1310b22008-01-24 16:13:08 -05005762
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005763 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5764 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005765
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005766 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005767 src_off_in_page));
5768 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005769 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005770
David Sterbafb85fc92014-07-31 01:03:53 +02005771 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005772 dst_off_in_page, src_off_in_page, cur);
5773
5774 src_offset += cur;
5775 dst_offset += cur;
5776 len -= cur;
5777 }
5778}
Chris Masond1310b22008-01-24 16:13:08 -05005779
5780void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5781 unsigned long src_offset, unsigned long len)
5782{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005783 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005784 size_t cur;
5785 size_t dst_off_in_page;
5786 size_t src_off_in_page;
5787 unsigned long dst_end = dst_offset + len - 1;
5788 unsigned long src_end = src_offset + len - 1;
Johannes Thumshirn70730172018-12-05 15:23:03 +01005789 size_t start_offset = offset_in_page(dst->start);
Chris Masond1310b22008-01-24 16:13:08 -05005790 unsigned long dst_i;
5791 unsigned long src_i;
5792
5793 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005794 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005795 "memmove bogus src_offset %lu move len %lu len %lu",
5796 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005797 BUG_ON(1);
5798 }
5799 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005800 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005801 "memmove bogus dst_offset %lu move len %lu len %lu",
5802 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005803 BUG_ON(1);
5804 }
Chris Mason727011e2010-08-06 13:21:20 -04005805 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005806 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5807 return;
5808 }
Chris Masond3977122009-01-05 21:25:51 -05005809 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005810 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5811 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005812
Johannes Thumshirn70730172018-12-05 15:23:03 +01005813 dst_off_in_page = offset_in_page(start_offset + dst_end);
5814 src_off_in_page = offset_in_page(start_offset + src_end);
Chris Masond1310b22008-01-24 16:13:08 -05005815
5816 cur = min_t(unsigned long, len, src_off_in_page + 1);
5817 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005818 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005819 dst_off_in_page - cur + 1,
5820 src_off_in_page - cur + 1, cur);
5821
5822 dst_end -= cur;
5823 src_end -= cur;
5824 len -= cur;
5825 }
5826}
Chris Mason6af118ce2008-07-22 11:18:07 -04005827
David Sterbaf7a52a42013-04-26 14:56:29 +00005828int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005829{
Chris Mason6af118ce2008-07-22 11:18:07 -04005830 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005831
Miao Xie19fe0a82010-10-26 20:57:29 -04005832 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005833 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005834 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005835 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005836 spin_lock(&page->mapping->private_lock);
5837 if (!PagePrivate(page)) {
5838 spin_unlock(&page->mapping->private_lock);
5839 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005840 }
5841
Josef Bacik3083ee22012-03-09 16:01:49 -05005842 eb = (struct extent_buffer *)page->private;
5843 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005844
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005845 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005846 * This is a little awful but should be ok, we need to make sure that
5847 * the eb doesn't disappear out from under us while we're looking at
5848 * this page.
5849 */
5850 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005851 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005852 spin_unlock(&eb->refs_lock);
5853 spin_unlock(&page->mapping->private_lock);
5854 return 0;
5855 }
5856 spin_unlock(&page->mapping->private_lock);
5857
Josef Bacik3083ee22012-03-09 16:01:49 -05005858 /*
5859 * If tree ref isn't set then we know the ref on this eb is a real ref,
5860 * so just return, this page will likely be freed soon anyway.
5861 */
5862 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5863 spin_unlock(&eb->refs_lock);
5864 return 0;
5865 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005866
David Sterbaf7a52a42013-04-26 14:56:29 +00005867 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005868}