blob: 861a087025a9b07747a2f4681c7179303cd0d754 [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
David Sterbaaab6e9e2017-11-30 18:00:02 +0100150static void flush_write_bio(struct extent_page_data *epd);
David Sterbae2932ee2017-06-23 04:16:17 +0200151
Chris Masond1310b22008-01-24 16:13:08 -0500152int __init extent_io_init(void)
153{
David Sterba837e1972012-09-07 03:00:48 -0600154 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200155 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300156 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500157 if (!extent_state_cache)
158 return -ENOMEM;
159
David Sterba837e1972012-09-07 03:00:48 -0600160 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200161 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300162 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500163 if (!extent_buffer_cache)
164 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400165
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400166 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
167 offsetof(struct btrfs_io_bio, bio),
168 BIOSET_NEED_BVECS))
Chris Mason9be33952013-05-17 18:30:14 -0400169 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700170
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400171 if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE))
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700172 goto free_bioset;
173
Chris Masond1310b22008-01-24 16:13:08 -0500174 return 0;
175
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700176free_bioset:
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400177 bioset_exit(&btrfs_bioset);
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700178
Chris Mason9be33952013-05-17 18:30:14 -0400179free_buffer_cache:
180 kmem_cache_destroy(extent_buffer_cache);
181 extent_buffer_cache = NULL;
182
Chris Masond1310b22008-01-24 16:13:08 -0500183free_state_cache:
184 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400185 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500186 return -ENOMEM;
187}
188
David Sterbae67c7182018-02-19 17:24:18 +0100189void __cold extent_io_exit(void)
Chris Masond1310b22008-01-24 16:13:08 -0500190{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000191 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000192
193 /*
194 * Make sure all delayed rcu free are flushed before we
195 * destroy caches.
196 */
197 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800198 kmem_cache_destroy(extent_state_cache);
199 kmem_cache_destroy(extent_buffer_cache);
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -0400200 bioset_exit(&btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500201}
202
203void extent_io_tree_init(struct extent_io_tree *tree,
Josef Bacikc6100a42017-05-05 11:57:13 -0400204 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500205{
Eric Paris6bef4d32010-02-23 19:43:04 +0000206 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500207 tree->ops = NULL;
208 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500209 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400210 tree->private_data = private_data;
Chris Masond1310b22008-01-24 16:13:08 -0500211}
Chris Masond1310b22008-01-24 16:13:08 -0500212
Christoph Hellwigb2950862008-12-02 09:54:17 -0500213static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500214{
215 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500216
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100217 /*
218 * The given mask might be not appropriate for the slab allocator,
219 * drop the unsupported bits
220 */
221 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500222 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400223 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500224 return state;
225 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100226 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100227 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000228 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200229 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500230 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100231 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500232 return state;
233}
Chris Masond1310b22008-01-24 16:13:08 -0500234
Chris Mason4845e442010-05-25 20:56:50 -0400235void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500236{
Chris Masond1310b22008-01-24 16:13:08 -0500237 if (!state)
238 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200239 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100240 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000241 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100242 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500243 kmem_cache_free(extent_state_cache, state);
244 }
245}
Chris Masond1310b22008-01-24 16:13:08 -0500246
Filipe Mananaf2071b22014-02-12 15:05:53 +0000247static struct rb_node *tree_insert(struct rb_root *root,
248 struct rb_node *search_start,
249 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000250 struct rb_node *node,
251 struct rb_node ***p_in,
252 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500253{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000254 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500255 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500256 struct tree_entry *entry;
257
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000258 if (p_in && parent_in) {
259 p = *p_in;
260 parent = *parent_in;
261 goto do_insert;
262 }
263
Filipe Mananaf2071b22014-02-12 15:05:53 +0000264 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500265 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500266 parent = *p;
267 entry = rb_entry(parent, struct tree_entry, rb_node);
268
269 if (offset < entry->start)
270 p = &(*p)->rb_left;
271 else if (offset > entry->end)
272 p = &(*p)->rb_right;
273 else
274 return parent;
275 }
276
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000277do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500278 rb_link_node(node, parent, p);
279 rb_insert_color(node, root);
280 return NULL;
281}
282
Chris Mason80ea96b2008-02-01 14:51:59 -0500283static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000284 struct rb_node **prev_ret,
285 struct rb_node **next_ret,
286 struct rb_node ***p_ret,
287 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500288{
Chris Mason80ea96b2008-02-01 14:51:59 -0500289 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000290 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500291 struct rb_node *prev = NULL;
292 struct rb_node *orig_prev = NULL;
293 struct tree_entry *entry;
294 struct tree_entry *prev_entry = NULL;
295
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000296 while (*n) {
297 prev = *n;
298 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500299 prev_entry = entry;
300
301 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000302 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500303 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000304 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500305 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000306 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500307 }
308
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000309 if (p_ret)
310 *p_ret = n;
311 if (parent_ret)
312 *parent_ret = prev;
313
Chris Masond1310b22008-01-24 16:13:08 -0500314 if (prev_ret) {
315 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500316 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500317 prev = rb_next(prev);
318 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
319 }
320 *prev_ret = prev;
321 prev = orig_prev;
322 }
323
324 if (next_ret) {
325 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500326 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500327 prev = rb_prev(prev);
328 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
329 }
330 *next_ret = prev;
331 }
332 return NULL;
333}
334
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000335static inline struct rb_node *
336tree_search_for_insert(struct extent_io_tree *tree,
337 u64 offset,
338 struct rb_node ***p_ret,
339 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500340{
Chris Mason70dec802008-01-29 09:59:12 -0500341 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500342 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500343
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000344 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500345 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500346 return prev;
347 return ret;
348}
349
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000350static inline struct rb_node *tree_search(struct extent_io_tree *tree,
351 u64 offset)
352{
353 return tree_search_for_insert(tree, offset, NULL, NULL);
354}
355
Chris Masond1310b22008-01-24 16:13:08 -0500356/*
357 * utility function to look for merge candidates inside a given range.
358 * Any extents with matching state are merged together into a single
359 * extent in the tree. Extents with EXTENT_IO in their state field
360 * are not merged because the end_io handlers need to be able to do
361 * operations on them without sleeping (or doing allocations/splits).
362 *
363 * This should be called with the tree lock held.
364 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000365static void merge_state(struct extent_io_tree *tree,
366 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500367{
368 struct extent_state *other;
369 struct rb_node *other_node;
370
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400371 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000372 return;
Chris Masond1310b22008-01-24 16:13:08 -0500373
374 other_node = rb_prev(&state->rb_node);
375 if (other_node) {
376 other = rb_entry(other_node, struct extent_state, rb_node);
377 if (other->end == state->start - 1 &&
378 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200379 if (tree->private_data &&
380 is_data_inode(tree->private_data))
381 btrfs_merge_delalloc_extent(tree->private_data,
382 state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500383 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500384 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100385 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500386 free_extent_state(other);
387 }
388 }
389 other_node = rb_next(&state->rb_node);
390 if (other_node) {
391 other = rb_entry(other_node, struct extent_state, rb_node);
392 if (other->start == state->end + 1 &&
393 other->state == state->state) {
Nikolay Borisov5c848192018-11-01 14:09:52 +0200394 if (tree->private_data &&
395 is_data_inode(tree->private_data))
396 btrfs_merge_delalloc_extent(tree->private_data,
397 state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400398 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400399 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100400 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400401 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500402 }
403 }
Chris Masond1310b22008-01-24 16:13:08 -0500404}
405
Xiao Guangrong3150b692011-07-14 03:19:08 +0000406static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800407 struct extent_state *state, unsigned *bits,
408 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000409
Chris Masond1310b22008-01-24 16:13:08 -0500410/*
411 * insert an extent_state struct into the tree. 'bits' are set on the
412 * struct before it is inserted.
413 *
414 * This may return -EEXIST if the extent is already there, in which case the
415 * state struct is freed.
416 *
417 * The tree lock is not taken internally. This is a utility function and
418 * probably isn't what you want to call (see set/clear_extent_bit).
419 */
420static int insert_state(struct extent_io_tree *tree,
421 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000422 struct rb_node ***p,
423 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800424 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500425{
426 struct rb_node *node;
427
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000428 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500429 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200430 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500431 state->start = start;
432 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400433
Qu Wenruod38ed272015-10-12 14:53:37 +0800434 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000435
Filipe Mananaf2071b22014-02-12 15:05:53 +0000436 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500437 if (node) {
438 struct extent_state *found;
439 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400440 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200441 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500442 return -EEXIST;
443 }
444 merge_state(tree, state);
445 return 0;
446}
447
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000448static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400449 u64 split)
450{
451 if (tree->ops && tree->ops->split_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400452 tree->ops->split_extent_hook(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400453}
454
Chris Masond1310b22008-01-24 16:13:08 -0500455/*
456 * split a given extent state struct in two, inserting the preallocated
457 * struct 'prealloc' as the newly created second half. 'split' indicates an
458 * offset inside 'orig' where it should be split.
459 *
460 * Before calling,
461 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
462 * are two extent state structs in the tree:
463 * prealloc: [orig->start, split - 1]
464 * orig: [ split, orig->end ]
465 *
466 * The tree locks are not taken by this function. They need to be held
467 * by the caller.
468 */
469static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
470 struct extent_state *prealloc, u64 split)
471{
472 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400473
474 split_cb(tree, orig, split);
475
Chris Masond1310b22008-01-24 16:13:08 -0500476 prealloc->start = orig->start;
477 prealloc->end = split - 1;
478 prealloc->state = orig->state;
479 orig->start = split;
480
Filipe Mananaf2071b22014-02-12 15:05:53 +0000481 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
482 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500483 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500484 free_extent_state(prealloc);
485 return -EEXIST;
486 }
487 return 0;
488}
489
Li Zefancdc6a392012-03-12 16:39:48 +0800490static struct extent_state *next_state(struct extent_state *state)
491{
492 struct rb_node *next = rb_next(&state->rb_node);
493 if (next)
494 return rb_entry(next, struct extent_state, rb_node);
495 else
496 return NULL;
497}
498
Chris Masond1310b22008-01-24 16:13:08 -0500499/*
500 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800501 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500502 *
503 * If no bits are set on the state struct after clearing things, the
504 * struct is freed and removed from the tree
505 */
Li Zefancdc6a392012-03-12 16:39:48 +0800506static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
507 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800508 unsigned *bits, int wake,
509 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500510{
Li Zefancdc6a392012-03-12 16:39:48 +0800511 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100512 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100513 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500514
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400515 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500516 u64 range = state->end - state->start + 1;
517 WARN_ON(range > tree->dirty_bytes);
518 tree->dirty_bytes -= range;
519 }
Nikolay Borisova36bb5f2018-11-01 14:09:51 +0200520
521 if (tree->private_data && is_data_inode(tree->private_data))
522 btrfs_clear_delalloc_extent(tree->private_data, state, bits);
523
David Sterba57599c72018-03-01 17:56:34 +0100524 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
525 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400526 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500527 if (wake)
528 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400529 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800530 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100531 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500532 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100533 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500534 free_extent_state(state);
535 } else {
536 WARN_ON(1);
537 }
538 } else {
539 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800540 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500541 }
Li Zefancdc6a392012-03-12 16:39:48 +0800542 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500543}
544
Xiao Guangrong82337672011-04-20 06:44:57 +0000545static struct extent_state *
546alloc_extent_state_atomic(struct extent_state *prealloc)
547{
548 if (!prealloc)
549 prealloc = alloc_extent_state(GFP_ATOMIC);
550
551 return prealloc;
552}
553
Eric Sandeen48a3b632013-04-25 20:41:01 +0000554static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400555{
David Sterba05912a32018-07-18 19:23:45 +0200556 struct inode *inode = tree->private_data;
557
558 btrfs_panic(btrfs_sb(inode->i_sb), err,
559 "locking error: extent tree was modified by another thread while locked");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400560}
561
Chris Masond1310b22008-01-24 16:13:08 -0500562/*
563 * clear some bits on a range in the tree. This may require splitting
564 * or inserting elements in the tree, so the gfp mask is used to
565 * indicate which allocations or sleeping are allowed.
566 *
567 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
568 * the given range from the tree regardless of state (ie for truncate).
569 *
570 * the range [start, end] is inclusive.
571 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100572 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500573 */
David Sterba66b0c882017-10-31 16:30:47 +0100574int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800575 unsigned bits, int wake, int delete,
576 struct extent_state **cached_state,
577 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500578{
579 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400580 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500581 struct extent_state *prealloc = NULL;
582 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400583 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500584 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000585 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500586
Josef Bacika5dee372013-12-13 10:02:44 -0500587 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000588
Josef Bacik7ee9e442013-06-21 16:37:03 -0400589 if (bits & EXTENT_DELALLOC)
590 bits |= EXTENT_NORESERVE;
591
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400592 if (delete)
593 bits |= ~EXTENT_CTLBITS;
594 bits |= EXTENT_FIRST_DELALLOC;
595
Josef Bacik2ac55d42010-02-03 19:33:23 +0000596 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
597 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500598again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800599 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000600 /*
601 * Don't care for allocation failure here because we might end
602 * up not needing the pre-allocated extent state at all, which
603 * is the case if we only have in the tree extent states that
604 * cover our input range and don't cover too any other range.
605 * If we end up needing a new extent state we allocate it later.
606 */
Chris Masond1310b22008-01-24 16:13:08 -0500607 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500608 }
609
Chris Masoncad321a2008-12-17 14:51:42 -0500610 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400611 if (cached_state) {
612 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000613
614 if (clear) {
615 *cached_state = NULL;
616 cached_state = NULL;
617 }
618
Filipe Manana27a35072014-07-06 20:09:59 +0100619 if (cached && extent_state_in_tree(cached) &&
620 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000621 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200622 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400623 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400624 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400625 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 if (clear)
627 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400628 }
Chris Masond1310b22008-01-24 16:13:08 -0500629 /*
630 * this search will find the extents that end after
631 * our range starts
632 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500633 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500634 if (!node)
635 goto out;
636 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400637hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500638 if (state->start > end)
639 goto out;
640 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400641 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500642
Liu Bo04493142012-02-16 18:34:37 +0800643 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800644 if (!(state->state & bits)) {
645 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800646 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800647 }
Liu Bo04493142012-02-16 18:34:37 +0800648
Chris Masond1310b22008-01-24 16:13:08 -0500649 /*
650 * | ---- desired range ---- |
651 * | state | or
652 * | ------------- state -------------- |
653 *
654 * We need to split the extent we found, and may flip
655 * bits on second half.
656 *
657 * If the extent we found extends past our range, we
658 * just split and search again. It'll get split again
659 * the next time though.
660 *
661 * If the extent we found is inside our range, we clear
662 * the desired bit on it.
663 */
664
665 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000666 prealloc = alloc_extent_state_atomic(prealloc);
667 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500668 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400669 if (err)
670 extent_io_tree_panic(tree, err);
671
Chris Masond1310b22008-01-24 16:13:08 -0500672 prealloc = NULL;
673 if (err)
674 goto out;
675 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800676 state = clear_state_bit(tree, state, &bits, wake,
677 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800678 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500679 }
680 goto search_again;
681 }
682 /*
683 * | ---- desired range ---- |
684 * | state |
685 * We need to split the extent, and clear the bit
686 * on the first half
687 */
688 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000689 prealloc = alloc_extent_state_atomic(prealloc);
690 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500691 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400692 if (err)
693 extent_io_tree_panic(tree, err);
694
Chris Masond1310b22008-01-24 16:13:08 -0500695 if (wake)
696 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400697
Qu Wenruofefdc552015-10-12 15:35:38 +0800698 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400699
Chris Masond1310b22008-01-24 16:13:08 -0500700 prealloc = NULL;
701 goto out;
702 }
Chris Mason42daec22009-09-23 19:51:09 -0400703
Qu Wenruofefdc552015-10-12 15:35:38 +0800704 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800705next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400706 if (last_end == (u64)-1)
707 goto out;
708 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800709 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800710 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500711
712search_again:
713 if (start > end)
714 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500715 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800716 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500717 cond_resched();
718 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200719
720out:
721 spin_unlock(&tree->lock);
722 if (prealloc)
723 free_extent_state(prealloc);
724
725 return 0;
726
Chris Masond1310b22008-01-24 16:13:08 -0500727}
Chris Masond1310b22008-01-24 16:13:08 -0500728
Jeff Mahoney143bede2012-03-01 14:56:26 +0100729static void wait_on_state(struct extent_io_tree *tree,
730 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500731 __releases(tree->lock)
732 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500733{
734 DEFINE_WAIT(wait);
735 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500736 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500737 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500738 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500739 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500740}
741
742/*
743 * waits for one or more bits to clear on a range in the state tree.
744 * The range [start, end] is inclusive.
745 * The tree lock is taken by this function
746 */
David Sterba41074882013-04-29 13:38:46 +0000747static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
748 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500749{
750 struct extent_state *state;
751 struct rb_node *node;
752
Josef Bacika5dee372013-12-13 10:02:44 -0500753 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000754
Chris Masoncad321a2008-12-17 14:51:42 -0500755 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500756again:
757 while (1) {
758 /*
759 * this search will find all the extents that end after
760 * our range starts
761 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500762 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100763process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500764 if (!node)
765 break;
766
767 state = rb_entry(node, struct extent_state, rb_node);
768
769 if (state->start > end)
770 goto out;
771
772 if (state->state & bits) {
773 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200774 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500775 wait_on_state(tree, state);
776 free_extent_state(state);
777 goto again;
778 }
779 start = state->end + 1;
780
781 if (start > end)
782 break;
783
Filipe Mananac50d3e72014-03-31 14:53:25 +0100784 if (!cond_resched_lock(&tree->lock)) {
785 node = rb_next(node);
786 goto process_node;
787 }
Chris Masond1310b22008-01-24 16:13:08 -0500788 }
789out:
Chris Masoncad321a2008-12-17 14:51:42 -0500790 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500791}
Chris Masond1310b22008-01-24 16:13:08 -0500792
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000793static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500794 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800795 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500796{
David Sterba9ee49a042015-01-14 19:52:13 +0100797 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100798 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400799
Nikolay Borisove06a1fc2018-11-01 14:09:50 +0200800 if (tree->private_data && is_data_inode(tree->private_data))
801 btrfs_set_delalloc_extent(tree->private_data, state, bits);
802
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400803 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500804 u64 range = state->end - state->start + 1;
805 tree->dirty_bytes += range;
806 }
David Sterba57599c72018-03-01 17:56:34 +0100807 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
808 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400809 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500810}
811
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100812static void cache_state_if_flags(struct extent_state *state,
813 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100814 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400815{
816 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100817 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400818 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200819 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400820 }
821 }
822}
823
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100824static void cache_state(struct extent_state *state,
825 struct extent_state **cached_ptr)
826{
827 return cache_state_if_flags(state, cached_ptr,
828 EXTENT_IOBITS | EXTENT_BOUNDARY);
829}
830
Chris Masond1310b22008-01-24 16:13:08 -0500831/*
Chris Mason1edbb732009-09-02 13:24:36 -0400832 * set some bits on a range in the tree. This may require allocations or
833 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500834 *
Chris Mason1edbb732009-09-02 13:24:36 -0400835 * If any of the exclusive bits are set, this will fail with -EEXIST if some
836 * part of the range already has the desired bits set. The start of the
837 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500838 *
Chris Mason1edbb732009-09-02 13:24:36 -0400839 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500840 */
Chris Mason1edbb732009-09-02 13:24:36 -0400841
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100842static int __must_check
843__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100844 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000845 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800846 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500847{
848 struct extent_state *state;
849 struct extent_state *prealloc = NULL;
850 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000851 struct rb_node **p;
852 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500853 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500854 u64 last_start;
855 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400856
Josef Bacika5dee372013-12-13 10:02:44 -0500857 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000858
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400859 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500860again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800861 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200862 /*
863 * Don't care for allocation failure here because we might end
864 * up not needing the pre-allocated extent state at all, which
865 * is the case if we only have in the tree extent states that
866 * cover our input range and don't cover too any other range.
867 * If we end up needing a new extent state we allocate it later.
868 */
Chris Masond1310b22008-01-24 16:13:08 -0500869 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500870 }
871
Chris Masoncad321a2008-12-17 14:51:42 -0500872 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400873 if (cached_state && *cached_state) {
874 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400875 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100876 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400877 node = &state->rb_node;
878 goto hit_next;
879 }
880 }
Chris Masond1310b22008-01-24 16:13:08 -0500881 /*
882 * this search will find all the extents that end after
883 * our range starts.
884 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000885 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500886 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000887 prealloc = alloc_extent_state_atomic(prealloc);
888 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000889 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800890 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400891 if (err)
892 extent_io_tree_panic(tree, err);
893
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000894 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500895 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500896 goto out;
897 }
Chris Masond1310b22008-01-24 16:13:08 -0500898 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400899hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500900 last_start = state->start;
901 last_end = state->end;
902
903 /*
904 * | ---- desired range ---- |
905 * | state |
906 *
907 * Just lock what we found and keep going
908 */
909 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400910 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500911 *failed_start = state->start;
912 err = -EEXIST;
913 goto out;
914 }
Chris Mason42daec22009-09-23 19:51:09 -0400915
Qu Wenruod38ed272015-10-12 14:53:37 +0800916 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400917 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500918 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400919 if (last_end == (u64)-1)
920 goto out;
921 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800922 state = next_state(state);
923 if (start < end && state && state->start == start &&
924 !need_resched())
925 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500926 goto search_again;
927 }
928
929 /*
930 * | ---- desired range ---- |
931 * | state |
932 * or
933 * | ------------- state -------------- |
934 *
935 * We need to split the extent we found, and may flip bits on
936 * second half.
937 *
938 * If the extent we found extends past our
939 * range, we just split and search again. It'll get split
940 * again the next time though.
941 *
942 * If the extent we found is inside our range, we set the
943 * desired bit on it.
944 */
945 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400946 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500947 *failed_start = start;
948 err = -EEXIST;
949 goto out;
950 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000951
952 prealloc = alloc_extent_state_atomic(prealloc);
953 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500954 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400955 if (err)
956 extent_io_tree_panic(tree, err);
957
Chris Masond1310b22008-01-24 16:13:08 -0500958 prealloc = NULL;
959 if (err)
960 goto out;
961 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800962 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400963 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500964 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400965 if (last_end == (u64)-1)
966 goto out;
967 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800968 state = next_state(state);
969 if (start < end && state && state->start == start &&
970 !need_resched())
971 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500972 }
973 goto search_again;
974 }
975 /*
976 * | ---- desired range ---- |
977 * | state | or | state |
978 *
979 * There's a hole, we need to insert something in it and
980 * ignore the extent we found.
981 */
982 if (state->start > start) {
983 u64 this_end;
984 if (end < last_start)
985 this_end = end;
986 else
Chris Masond3977122009-01-05 21:25:51 -0500987 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000988
989 prealloc = alloc_extent_state_atomic(prealloc);
990 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000991
992 /*
993 * Avoid to free 'prealloc' if it can be merged with
994 * the later extent.
995 */
Chris Masond1310b22008-01-24 16:13:08 -0500996 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800997 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400998 if (err)
999 extent_io_tree_panic(tree, err);
1000
Chris Mason2c64c532009-09-02 15:04:12 -04001001 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001002 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001003 start = this_end + 1;
1004 goto search_again;
1005 }
1006 /*
1007 * | ---- desired range ---- |
1008 * | state |
1009 * We need to split the extent, and set the bit
1010 * on the first half
1011 */
1012 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001013 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001014 *failed_start = start;
1015 err = -EEXIST;
1016 goto out;
1017 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001018
1019 prealloc = alloc_extent_state_atomic(prealloc);
1020 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001021 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001022 if (err)
1023 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001024
Qu Wenruod38ed272015-10-12 14:53:37 +08001025 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001026 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001027 merge_state(tree, prealloc);
1028 prealloc = NULL;
1029 goto out;
1030 }
1031
David Sterbab5a4ba142016-04-27 01:02:15 +02001032search_again:
1033 if (start > end)
1034 goto out;
1035 spin_unlock(&tree->lock);
1036 if (gfpflags_allow_blocking(mask))
1037 cond_resched();
1038 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001039
1040out:
Chris Masoncad321a2008-12-17 14:51:42 -05001041 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001042 if (prealloc)
1043 free_extent_state(prealloc);
1044
1045 return err;
1046
Chris Masond1310b22008-01-24 16:13:08 -05001047}
Chris Masond1310b22008-01-24 16:13:08 -05001048
David Sterba41074882013-04-29 13:38:46 +00001049int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001050 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001051 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001052{
1053 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001054 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001055}
1056
1057
Josef Bacik462d6fa2011-09-26 13:56:12 -04001058/**
Liu Bo10983f22012-07-11 15:26:19 +08001059 * convert_extent_bit - convert all bits in a given range from one bit to
1060 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001061 * @tree: the io tree to search
1062 * @start: the start offset in bytes
1063 * @end: the end offset in bytes (inclusive)
1064 * @bits: the bits to set in this range
1065 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001066 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001067 *
1068 * This will go through and set bits for the given range. If any states exist
1069 * already in this range they are set with the given bit and cleared of the
1070 * clear_bits. This is only meant to be used by things that are mergeable, ie
1071 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1072 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001073 *
1074 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001075 */
1076int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001077 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001078 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001079{
1080 struct extent_state *state;
1081 struct extent_state *prealloc = NULL;
1082 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001083 struct rb_node **p;
1084 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001085 int err = 0;
1086 u64 last_start;
1087 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001088 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001089
Josef Bacika5dee372013-12-13 10:02:44 -05001090 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001091
Josef Bacik462d6fa2011-09-26 13:56:12 -04001092again:
David Sterba210aa272016-04-26 23:54:39 +02001093 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001094 /*
1095 * Best effort, don't worry if extent state allocation fails
1096 * here for the first iteration. We might have a cached state
1097 * that matches exactly the target range, in which case no
1098 * extent state allocations are needed. We'll only know this
1099 * after locking the tree.
1100 */
David Sterba210aa272016-04-26 23:54:39 +02001101 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001102 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001103 return -ENOMEM;
1104 }
1105
1106 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001107 if (cached_state && *cached_state) {
1108 state = *cached_state;
1109 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001110 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001111 node = &state->rb_node;
1112 goto hit_next;
1113 }
1114 }
1115
Josef Bacik462d6fa2011-09-26 13:56:12 -04001116 /*
1117 * this search will find all the extents that end after
1118 * our range starts.
1119 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001120 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001121 if (!node) {
1122 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001123 if (!prealloc) {
1124 err = -ENOMEM;
1125 goto out;
1126 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001127 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001128 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001129 if (err)
1130 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001131 cache_state(prealloc, cached_state);
1132 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001133 goto out;
1134 }
1135 state = rb_entry(node, struct extent_state, rb_node);
1136hit_next:
1137 last_start = state->start;
1138 last_end = state->end;
1139
1140 /*
1141 * | ---- desired range ---- |
1142 * | state |
1143 *
1144 * Just lock what we found and keep going
1145 */
1146 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001147 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001148 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001149 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001150 if (last_end == (u64)-1)
1151 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001152 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001153 if (start < end && state && state->start == start &&
1154 !need_resched())
1155 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001156 goto search_again;
1157 }
1158
1159 /*
1160 * | ---- desired range ---- |
1161 * | state |
1162 * or
1163 * | ------------- state -------------- |
1164 *
1165 * We need to split the extent we found, and may flip bits on
1166 * second half.
1167 *
1168 * If the extent we found extends past our
1169 * range, we just split and search again. It'll get split
1170 * again the next time though.
1171 *
1172 * If the extent we found is inside our range, we set the
1173 * desired bit on it.
1174 */
1175 if (state->start < start) {
1176 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001177 if (!prealloc) {
1178 err = -ENOMEM;
1179 goto out;
1180 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001181 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001182 if (err)
1183 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001184 prealloc = NULL;
1185 if (err)
1186 goto out;
1187 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001188 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001189 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001190 state = clear_state_bit(tree, state, &clear_bits, 0,
1191 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001192 if (last_end == (u64)-1)
1193 goto out;
1194 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001195 if (start < end && state && state->start == start &&
1196 !need_resched())
1197 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001198 }
1199 goto search_again;
1200 }
1201 /*
1202 * | ---- desired range ---- |
1203 * | state | or | state |
1204 *
1205 * There's a hole, we need to insert something in it and
1206 * ignore the extent we found.
1207 */
1208 if (state->start > start) {
1209 u64 this_end;
1210 if (end < last_start)
1211 this_end = end;
1212 else
1213 this_end = last_start - 1;
1214
1215 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001216 if (!prealloc) {
1217 err = -ENOMEM;
1218 goto out;
1219 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001220
1221 /*
1222 * Avoid to free 'prealloc' if it can be merged with
1223 * the later extent.
1224 */
1225 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001226 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001227 if (err)
1228 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001229 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001230 prealloc = NULL;
1231 start = this_end + 1;
1232 goto search_again;
1233 }
1234 /*
1235 * | ---- desired range ---- |
1236 * | state |
1237 * We need to split the extent, and set the bit
1238 * on the first half
1239 */
1240 if (state->start <= end && state->end > end) {
1241 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001242 if (!prealloc) {
1243 err = -ENOMEM;
1244 goto out;
1245 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001246
1247 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001248 if (err)
1249 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001250
Qu Wenruod38ed272015-10-12 14:53:37 +08001251 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001252 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001253 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001254 prealloc = NULL;
1255 goto out;
1256 }
1257
Josef Bacik462d6fa2011-09-26 13:56:12 -04001258search_again:
1259 if (start > end)
1260 goto out;
1261 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001262 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001263 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001264 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001265
1266out:
1267 spin_unlock(&tree->lock);
1268 if (prealloc)
1269 free_extent_state(prealloc);
1270
1271 return err;
1272}
1273
Chris Masond1310b22008-01-24 16:13:08 -05001274/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001275int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001276 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001277{
1278 /*
1279 * We don't support EXTENT_LOCKED yet, as current changeset will
1280 * record any bits changed, so for EXTENT_LOCKED case, it will
1281 * either fail with -EEXIST or changeset will record the whole
1282 * range.
1283 */
1284 BUG_ON(bits & EXTENT_LOCKED);
1285
David Sterba2c53b912016-04-26 23:54:39 +02001286 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001287 changeset);
1288}
1289
Qu Wenruofefdc552015-10-12 15:35:38 +08001290int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1291 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001292 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001293{
1294 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001295 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001296}
1297
Qu Wenruofefdc552015-10-12 15:35:38 +08001298int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001299 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001300{
1301 /*
1302 * Don't support EXTENT_LOCKED case, same reason as
1303 * set_record_extent_bits().
1304 */
1305 BUG_ON(bits & EXTENT_LOCKED);
1306
David Sterbaf734c442016-04-26 23:54:39 +02001307 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001308 changeset);
1309}
1310
Chris Masond352ac62008-09-29 15:18:18 -04001311/*
1312 * either insert or lock state struct between start and end use mask to tell
1313 * us if waiting is desired.
1314 */
Chris Mason1edbb732009-09-02 13:24:36 -04001315int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001316 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001317{
1318 int err;
1319 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001320
Chris Masond1310b22008-01-24 16:13:08 -05001321 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001322 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001323 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001324 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001325 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001326 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1327 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001328 } else
Chris Masond1310b22008-01-24 16:13:08 -05001329 break;
Chris Masond1310b22008-01-24 16:13:08 -05001330 WARN_ON(start > end);
1331 }
1332 return err;
1333}
Chris Masond1310b22008-01-24 16:13:08 -05001334
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001335int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik251792012008-10-29 14:49:05 -04001336{
1337 int err;
1338 u64 failed_start;
1339
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001340 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001341 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001342 if (err == -EEXIST) {
1343 if (failed_start > start)
1344 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001345 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik251792012008-10-29 14:49:05 -04001346 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001347 }
Josef Bacik251792012008-10-29 14:49:05 -04001348 return 1;
1349}
Josef Bacik251792012008-10-29 14:49:05 -04001350
David Sterbabd1fa4f2015-12-03 13:08:59 +01001351void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001352{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001353 unsigned long index = start >> PAGE_SHIFT;
1354 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001355 struct page *page;
1356
1357 while (index <= end_index) {
1358 page = find_get_page(inode->i_mapping, index);
1359 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1360 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001361 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001362 index++;
1363 }
Chris Mason4adaa612013-03-26 13:07:00 -04001364}
1365
David Sterbaf6311572015-12-03 13:08:59 +01001366void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001367{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001368 unsigned long index = start >> PAGE_SHIFT;
1369 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001370 struct page *page;
1371
1372 while (index <= end_index) {
1373 page = find_get_page(inode->i_mapping, index);
1374 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001375 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001376 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001377 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001378 index++;
1379 }
Chris Mason4adaa612013-03-26 13:07:00 -04001380}
1381
Chris Masond352ac62008-09-29 15:18:18 -04001382/* find the first state struct with 'bits' set after 'start', and
1383 * return it. tree->lock must be held. NULL will returned if
1384 * nothing was found after 'start'
1385 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001386static struct extent_state *
1387find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001388 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001389{
1390 struct rb_node *node;
1391 struct extent_state *state;
1392
1393 /*
1394 * this search will find all the extents that end after
1395 * our range starts.
1396 */
1397 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001398 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001399 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001400
Chris Masond3977122009-01-05 21:25:51 -05001401 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001402 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001403 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001404 return state;
Chris Masond3977122009-01-05 21:25:51 -05001405
Chris Masond7fc6402008-02-18 12:12:38 -05001406 node = rb_next(node);
1407 if (!node)
1408 break;
1409 }
1410out:
1411 return NULL;
1412}
Chris Masond7fc6402008-02-18 12:12:38 -05001413
Chris Masond352ac62008-09-29 15:18:18 -04001414/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001415 * find the first offset in the io tree with 'bits' set. zero is
1416 * returned if we find something, and *start_ret and *end_ret are
1417 * set to reflect the state struct that was found.
1418 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001419 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001420 */
1421int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001422 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001423 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001424{
1425 struct extent_state *state;
1426 int ret = 1;
1427
1428 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001429 if (cached_state && *cached_state) {
1430 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001431 if (state->end == start - 1 && extent_state_in_tree(state)) {
Liu Bo9688e9a2018-08-23 03:14:53 +08001432 while ((state = next_state(state)) != NULL) {
Josef Bacike6138872012-09-27 17:07:30 -04001433 if (state->state & bits)
1434 goto got_it;
Josef Bacike6138872012-09-27 17:07:30 -04001435 }
1436 free_extent_state(*cached_state);
1437 *cached_state = NULL;
1438 goto out;
1439 }
1440 free_extent_state(*cached_state);
1441 *cached_state = NULL;
1442 }
1443
Xiao Guangrong69261c42011-07-14 03:19:45 +00001444 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001445got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001446 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001447 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001448 *start_ret = state->start;
1449 *end_ret = state->end;
1450 ret = 0;
1451 }
Josef Bacike6138872012-09-27 17:07:30 -04001452out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001453 spin_unlock(&tree->lock);
1454 return ret;
1455}
1456
1457/*
Chris Masond352ac62008-09-29 15:18:18 -04001458 * find a contiguous range of bytes in the file marked as delalloc, not
1459 * more than 'max_bytes'. start and end are used to return the range,
1460 *
1461 * 1 is returned if we find something, 0 if nothing was in the tree
1462 */
Chris Masonc8b97812008-10-29 14:49:59 -04001463static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001464 u64 *start, u64 *end, u64 max_bytes,
1465 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001466{
1467 struct rb_node *node;
1468 struct extent_state *state;
1469 u64 cur_start = *start;
1470 u64 found = 0;
1471 u64 total_bytes = 0;
1472
Chris Masoncad321a2008-12-17 14:51:42 -05001473 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001474
Chris Masond1310b22008-01-24 16:13:08 -05001475 /*
1476 * this search will find all the extents that end after
1477 * our range starts.
1478 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001479 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001480 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001481 if (!found)
1482 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001483 goto out;
1484 }
1485
Chris Masond3977122009-01-05 21:25:51 -05001486 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001487 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001488 if (found && (state->start != cur_start ||
1489 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001490 goto out;
1491 }
1492 if (!(state->state & EXTENT_DELALLOC)) {
1493 if (!found)
1494 *end = state->end;
1495 goto out;
1496 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001497 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001498 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001499 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001500 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001501 }
Chris Masond1310b22008-01-24 16:13:08 -05001502 found++;
1503 *end = state->end;
1504 cur_start = state->end + 1;
1505 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001506 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001507 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001508 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001509 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001510 break;
1511 }
1512out:
Chris Masoncad321a2008-12-17 14:51:42 -05001513 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001514 return found;
1515}
1516
Liu Boda2c7002017-02-10 16:41:05 +01001517static int __process_pages_contig(struct address_space *mapping,
1518 struct page *locked_page,
1519 pgoff_t start_index, pgoff_t end_index,
1520 unsigned long page_ops, pgoff_t *index_ret);
1521
Jeff Mahoney143bede2012-03-01 14:56:26 +01001522static noinline void __unlock_for_delalloc(struct inode *inode,
1523 struct page *locked_page,
1524 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001525{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001526 unsigned long index = start >> PAGE_SHIFT;
1527 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001528
Liu Bo76c00212017-02-10 16:42:14 +01001529 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001530 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001531 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001532
Liu Bo76c00212017-02-10 16:42:14 +01001533 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1534 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001535}
1536
1537static noinline int lock_delalloc_pages(struct inode *inode,
1538 struct page *locked_page,
1539 u64 delalloc_start,
1540 u64 delalloc_end)
1541{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001542 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001543 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001544 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001545 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001546
Liu Bo76c00212017-02-10 16:42:14 +01001547 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001548 if (index == locked_page->index && index == end_index)
1549 return 0;
1550
Liu Bo76c00212017-02-10 16:42:14 +01001551 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1552 end_index, PAGE_LOCK, &index_ret);
1553 if (ret == -EAGAIN)
1554 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1555 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001556 return ret;
1557}
1558
1559/*
1560 * find a contiguous range of bytes in the file marked as delalloc, not
1561 * more than 'max_bytes'. start and end are used to return the range,
1562 *
1563 * 1 is returned if we find something, 0 if nothing was in the tree
1564 */
David Sterba9c363962018-08-17 17:28:06 +02001565static noinline_for_stack u64 find_lock_delalloc_range(struct inode *inode,
Josef Bacik294e30f2013-10-09 12:00:56 -04001566 struct extent_io_tree *tree,
1567 struct page *locked_page, u64 *start,
1568 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001569{
1570 u64 delalloc_start;
1571 u64 delalloc_end;
1572 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001573 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001574 int ret;
1575 int loops = 0;
1576
1577again:
1578 /* step one, find a bunch of delalloc bytes starting at start */
1579 delalloc_start = *start;
1580 delalloc_end = 0;
1581 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001582 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001583 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001584 *start = delalloc_start;
1585 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001586 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001587 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001588 }
1589
1590 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001591 * start comes from the offset of locked_page. We have to lock
1592 * pages in order, so we can't process delalloc bytes before
1593 * locked_page
1594 */
Chris Masond3977122009-01-05 21:25:51 -05001595 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001596 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001597
1598 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001599 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001600 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001601 if (delalloc_end + 1 - delalloc_start > max_bytes)
1602 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001603
Chris Masonc8b97812008-10-29 14:49:59 -04001604 /* step two, lock all the pages after the page that has start */
1605 ret = lock_delalloc_pages(inode, locked_page,
1606 delalloc_start, delalloc_end);
1607 if (ret == -EAGAIN) {
1608 /* some of the pages are gone, lets avoid looping by
1609 * shortening the size of the delalloc range we're searching
1610 */
Chris Mason9655d292009-09-02 15:22:30 -04001611 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001612 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001613 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001614 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001615 loops = 1;
1616 goto again;
1617 } else {
1618 found = 0;
1619 goto out_failed;
1620 }
1621 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001622 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001623
1624 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001625 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001626
1627 /* then test to make sure it is all still delalloc */
1628 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001629 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001630 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001631 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001632 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001633 __unlock_for_delalloc(inode, locked_page,
1634 delalloc_start, delalloc_end);
1635 cond_resched();
1636 goto again;
1637 }
Chris Mason9655d292009-09-02 15:22:30 -04001638 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001639 *start = delalloc_start;
1640 *end = delalloc_end;
1641out_failed:
1642 return found;
1643}
1644
David Sterba9c363962018-08-17 17:28:06 +02001645#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1646u64 btrfs_find_lock_delalloc_range(struct inode *inode,
1647 struct extent_io_tree *tree,
1648 struct page *locked_page, u64 *start,
1649 u64 *end, u64 max_bytes)
1650{
1651 return find_lock_delalloc_range(inode, tree, locked_page, start, end,
1652 max_bytes);
1653}
1654#endif
1655
Liu Boda2c7002017-02-10 16:41:05 +01001656static int __process_pages_contig(struct address_space *mapping,
1657 struct page *locked_page,
1658 pgoff_t start_index, pgoff_t end_index,
1659 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001660{
Liu Bo873695b2017-02-02 17:49:22 -08001661 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001662 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001663 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001664 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001665 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001666 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001667 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001668
Liu Boda2c7002017-02-10 16:41:05 +01001669 if (page_ops & PAGE_LOCK) {
1670 ASSERT(page_ops == PAGE_LOCK);
1671 ASSERT(index_ret && *index_ret == start_index);
1672 }
1673
Filipe Manana704de492014-10-06 22:14:22 +01001674 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001675 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001676
Chris Masond3977122009-01-05 21:25:51 -05001677 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001678 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001679 min_t(unsigned long,
1680 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001681 if (ret == 0) {
1682 /*
1683 * Only if we're going to lock these pages,
1684 * can we find nothing at @index.
1685 */
1686 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001687 err = -EAGAIN;
1688 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001689 }
Chris Mason8b62b722009-09-02 16:53:46 -04001690
Liu Boda2c7002017-02-10 16:41:05 +01001691 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001692 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001693 SetPagePrivate2(pages[i]);
1694
Chris Masonc8b97812008-10-29 14:49:59 -04001695 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001696 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001697 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001698 continue;
1699 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001700 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001701 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001702 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001703 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001704 if (page_ops & PAGE_SET_ERROR)
1705 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001706 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001707 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001708 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001709 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001710 if (page_ops & PAGE_LOCK) {
1711 lock_page(pages[i]);
1712 if (!PageDirty(pages[i]) ||
1713 pages[i]->mapping != mapping) {
1714 unlock_page(pages[i]);
1715 put_page(pages[i]);
1716 err = -EAGAIN;
1717 goto out;
1718 }
1719 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001720 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001721 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001722 }
1723 nr_pages -= ret;
1724 index += ret;
1725 cond_resched();
1726 }
Liu Boda2c7002017-02-10 16:41:05 +01001727out:
1728 if (err && index_ret)
1729 *index_ret = start_index + pages_locked - 1;
1730 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001731}
Chris Masonc8b97812008-10-29 14:49:59 -04001732
Liu Bo873695b2017-02-02 17:49:22 -08001733void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1734 u64 delalloc_end, struct page *locked_page,
1735 unsigned clear_bits,
1736 unsigned long page_ops)
1737{
1738 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001739 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001740
1741 __process_pages_contig(inode->i_mapping, locked_page,
1742 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001743 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001744}
1745
Chris Masond352ac62008-09-29 15:18:18 -04001746/*
1747 * count the number of bytes in the tree that have a given bit(s)
1748 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1749 * cached. The total number found is returned.
1750 */
Chris Masond1310b22008-01-24 16:13:08 -05001751u64 count_range_bits(struct extent_io_tree *tree,
1752 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001753 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001754{
1755 struct rb_node *node;
1756 struct extent_state *state;
1757 u64 cur_start = *start;
1758 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001759 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001760 int found = 0;
1761
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301762 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001763 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001764
Chris Masoncad321a2008-12-17 14:51:42 -05001765 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001766 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1767 total_bytes = tree->dirty_bytes;
1768 goto out;
1769 }
1770 /*
1771 * this search will find all the extents that end after
1772 * our range starts.
1773 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001774 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001775 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001776 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001777
Chris Masond3977122009-01-05 21:25:51 -05001778 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001779 state = rb_entry(node, struct extent_state, rb_node);
1780 if (state->start > search_end)
1781 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001782 if (contig && found && state->start > last + 1)
1783 break;
1784 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001785 total_bytes += min(search_end, state->end) + 1 -
1786 max(cur_start, state->start);
1787 if (total_bytes >= max_bytes)
1788 break;
1789 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001790 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001791 found = 1;
1792 }
Chris Masonec29ed52011-02-23 16:23:20 -05001793 last = state->end;
1794 } else if (contig && found) {
1795 break;
Chris Masond1310b22008-01-24 16:13:08 -05001796 }
1797 node = rb_next(node);
1798 if (!node)
1799 break;
1800 }
1801out:
Chris Masoncad321a2008-12-17 14:51:42 -05001802 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001803 return total_bytes;
1804}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001805
Chris Masond352ac62008-09-29 15:18:18 -04001806/*
1807 * set the private field for a given byte offset in the tree. If there isn't
1808 * an extent_state there already, this does nothing.
1809 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001810static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001811 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001812{
1813 struct rb_node *node;
1814 struct extent_state *state;
1815 int ret = 0;
1816
Chris Masoncad321a2008-12-17 14:51:42 -05001817 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001818 /*
1819 * this search will find all the extents that end after
1820 * our range starts.
1821 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001822 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001823 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001824 ret = -ENOENT;
1825 goto out;
1826 }
1827 state = rb_entry(node, struct extent_state, rb_node);
1828 if (state->start != start) {
1829 ret = -ENOENT;
1830 goto out;
1831 }
David Sterba47dc1962016-02-11 13:24:13 +01001832 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001833out:
Chris Masoncad321a2008-12-17 14:51:42 -05001834 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001835 return ret;
1836}
1837
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001838static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001839 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001840{
1841 struct rb_node *node;
1842 struct extent_state *state;
1843 int ret = 0;
1844
Chris Masoncad321a2008-12-17 14:51:42 -05001845 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001846 /*
1847 * this search will find all the extents that end after
1848 * our range starts.
1849 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001850 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001851 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001852 ret = -ENOENT;
1853 goto out;
1854 }
1855 state = rb_entry(node, struct extent_state, rb_node);
1856 if (state->start != start) {
1857 ret = -ENOENT;
1858 goto out;
1859 }
David Sterba47dc1962016-02-11 13:24:13 +01001860 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001861out:
Chris Masoncad321a2008-12-17 14:51:42 -05001862 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001863 return ret;
1864}
1865
1866/*
1867 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001868 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001869 * has the bits set. Otherwise, 1 is returned if any bit in the
1870 * range is found set.
1871 */
1872int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001873 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001874{
1875 struct extent_state *state = NULL;
1876 struct rb_node *node;
1877 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001878
Chris Masoncad321a2008-12-17 14:51:42 -05001879 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001880 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001881 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001882 node = &cached->rb_node;
1883 else
1884 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001885 while (node && start <= end) {
1886 state = rb_entry(node, struct extent_state, rb_node);
1887
1888 if (filled && state->start > start) {
1889 bitset = 0;
1890 break;
1891 }
1892
1893 if (state->start > end)
1894 break;
1895
1896 if (state->state & bits) {
1897 bitset = 1;
1898 if (!filled)
1899 break;
1900 } else if (filled) {
1901 bitset = 0;
1902 break;
1903 }
Chris Mason46562cec2009-09-23 20:23:16 -04001904
1905 if (state->end == (u64)-1)
1906 break;
1907
Chris Masond1310b22008-01-24 16:13:08 -05001908 start = state->end + 1;
1909 if (start > end)
1910 break;
1911 node = rb_next(node);
1912 if (!node) {
1913 if (filled)
1914 bitset = 0;
1915 break;
1916 }
1917 }
Chris Masoncad321a2008-12-17 14:51:42 -05001918 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001919 return bitset;
1920}
Chris Masond1310b22008-01-24 16:13:08 -05001921
1922/*
1923 * helper function to set a given page up to date if all the
1924 * extents in the tree for that page are up to date
1925 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001926static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001927{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001928 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001929 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001930 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001931 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001932}
1933
Josef Bacik7870d082017-05-05 11:57:15 -04001934int free_io_failure(struct extent_io_tree *failure_tree,
1935 struct extent_io_tree *io_tree,
1936 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001937{
1938 int ret;
1939 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001940
David Sterba47dc1962016-02-11 13:24:13 +01001941 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001942 ret = clear_extent_bits(failure_tree, rec->start,
1943 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001944 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001945 if (ret)
1946 err = ret;
1947
Josef Bacik7870d082017-05-05 11:57:15 -04001948 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001949 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001950 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001951 if (ret && !err)
1952 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001953
1954 kfree(rec);
1955 return err;
1956}
1957
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001958/*
1959 * this bypasses the standard btrfs submit functions deliberately, as
1960 * the standard behavior is to write all copies in a raid setup. here we only
1961 * want to write the one bad copy. so we do the mapping for ourselves and issue
1962 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001963 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001964 * actually prevents the read that triggered the error from finishing.
1965 * currently, there can be no more than two copies of every data bit. thus,
1966 * exactly one rewrite is required.
1967 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001968int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1969 u64 length, u64 logical, struct page *page,
1970 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001971{
1972 struct bio *bio;
1973 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974 u64 map_length = 0;
1975 u64 sector;
1976 struct btrfs_bio *bbio = NULL;
1977 int ret;
1978
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001979 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980 BUG_ON(!mirror_num);
1981
David Sterbac5e4c3d2017-06-12 17:29:41 +02001982 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001983 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001984 map_length = length;
1985
Filipe Mananab5de8d02016-05-27 22:21:27 +01001986 /*
1987 * Avoid races with device replace and make sure our bbio has devices
1988 * associated to its stripes that don't go away while we are doing the
1989 * read repair operation.
1990 */
1991 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03001992 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07001993 /*
1994 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
1995 * to update all raid stripes, but here we just want to correct
1996 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
1997 * stripe's dev and sector.
1998 */
1999 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2000 &map_length, &bbio, 0);
2001 if (ret) {
2002 btrfs_bio_counter_dec(fs_info);
2003 bio_put(bio);
2004 return -EIO;
2005 }
2006 ASSERT(bbio->mirror_num == 1);
2007 } else {
2008 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2009 &map_length, &bbio, mirror_num);
2010 if (ret) {
2011 btrfs_bio_counter_dec(fs_info);
2012 bio_put(bio);
2013 return -EIO;
2014 }
2015 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002016 }
Liu Boc7253282017-03-29 10:53:58 -07002017
2018 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002019 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002020 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002021 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002022 if (!dev || !dev->bdev ||
2023 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002024 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002025 bio_put(bio);
2026 return -EIO;
2027 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002028 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002029 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002030 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002031
Mike Christie4e49ea42016-06-05 14:31:41 -05002032 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002033 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002034 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002035 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002036 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002037 return -EIO;
2038 }
2039
David Sterbab14af3b2015-10-08 10:43:10 +02002040 btrfs_info_rl_in_rcu(fs_info,
2041 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002042 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002043 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002044 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002045 bio_put(bio);
2046 return 0;
2047}
2048
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002049int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2050 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002051{
Josef Bacikea466792012-03-26 21:57:36 -04002052 u64 start = eb->start;
David Sterbacc5e31a2018-03-01 18:20:27 +01002053 int i, num_pages = num_extent_pages(eb);
Chris Masond95603b2012-04-12 15:55:15 -04002054 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002055
David Howellsbc98a422017-07-17 08:45:34 +01002056 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002057 return -EROFS;
2058
Josef Bacikea466792012-03-26 21:57:36 -04002059 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002060 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002061
Josef Bacik6ec656b2017-05-05 11:57:14 -04002062 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002063 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002064 if (ret)
2065 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002067 }
2068
2069 return ret;
2070}
2071
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002072/*
2073 * each time an IO finishes, we do a fast check in the IO failure tree
2074 * to see if we need to process or clean up an io_failure_record
2075 */
Josef Bacik7870d082017-05-05 11:57:15 -04002076int clean_io_failure(struct btrfs_fs_info *fs_info,
2077 struct extent_io_tree *failure_tree,
2078 struct extent_io_tree *io_tree, u64 start,
2079 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002080{
2081 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002082 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002083 struct extent_state *state;
2084 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002085 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086
2087 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002088 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2089 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002090 if (!ret)
2091 return 0;
2092
Josef Bacik7870d082017-05-05 11:57:15 -04002093 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002094 if (ret)
2095 return 0;
2096
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002097 BUG_ON(!failrec->this_mirror);
2098
2099 if (failrec->in_validation) {
2100 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002101 btrfs_debug(fs_info,
2102 "clean_io_failure: freeing dummy error at %llu",
2103 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002104 goto out;
2105 }
David Howellsbc98a422017-07-17 08:45:34 +01002106 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002107 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108
Josef Bacik7870d082017-05-05 11:57:15 -04002109 spin_lock(&io_tree->lock);
2110 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002111 failrec->start,
2112 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002113 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002114
Miao Xie883d0de2013-07-25 19:22:35 +08002115 if (state && state->start <= failrec->start &&
2116 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002117 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2118 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002119 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002120 repair_io_failure(fs_info, ino, start, failrec->len,
2121 failrec->logical, page, pg_offset,
2122 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002123 }
2124 }
2125
2126out:
Josef Bacik7870d082017-05-05 11:57:15 -04002127 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002128
Miao Xie454ff3d2014-09-12 18:43:58 +08002129 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002130}
2131
Miao Xief6124962014-09-12 18:44:04 +08002132/*
2133 * Can be called when
2134 * - hold extent lock
2135 * - under ordered extent
2136 * - the inode is freeing
2137 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002138void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002139{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002140 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002141 struct io_failure_record *failrec;
2142 struct extent_state *state, *next;
2143
2144 if (RB_EMPTY_ROOT(&failure_tree->state))
2145 return;
2146
2147 spin_lock(&failure_tree->lock);
2148 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2149 while (state) {
2150 if (state->start > end)
2151 break;
2152
2153 ASSERT(state->end <= end);
2154
2155 next = next_state(state);
2156
David Sterba47dc1962016-02-11 13:24:13 +01002157 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002158 free_extent_state(state);
2159 kfree(failrec);
2160
2161 state = next;
2162 }
2163 spin_unlock(&failure_tree->lock);
2164}
2165
Miao Xie2fe63032014-09-12 18:43:59 +08002166int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002167 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002168{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002169 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002170 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002171 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002172 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2173 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2174 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002175 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002176 u64 logical;
2177
David Sterba47dc1962016-02-11 13:24:13 +01002178 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002179 if (ret) {
2180 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2181 if (!failrec)
2182 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002183
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002184 failrec->start = start;
2185 failrec->len = end - start + 1;
2186 failrec->this_mirror = 0;
2187 failrec->bio_flags = 0;
2188 failrec->in_validation = 0;
2189
2190 read_lock(&em_tree->lock);
2191 em = lookup_extent_mapping(em_tree, start, failrec->len);
2192 if (!em) {
2193 read_unlock(&em_tree->lock);
2194 kfree(failrec);
2195 return -EIO;
2196 }
2197
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002198 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002199 free_extent_map(em);
2200 em = NULL;
2201 }
2202 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002203 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002204 kfree(failrec);
2205 return -EIO;
2206 }
Miao Xie2fe63032014-09-12 18:43:59 +08002207
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002208 logical = start - em->start;
2209 logical = em->block_start + logical;
2210 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2211 logical = em->block_start;
2212 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2213 extent_set_compress_type(&failrec->bio_flags,
2214 em->compress_type);
2215 }
Miao Xie2fe63032014-09-12 18:43:59 +08002216
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002217 btrfs_debug(fs_info,
2218 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2219 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002220
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002221 failrec->logical = logical;
2222 free_extent_map(em);
2223
2224 /* set the bits in the private failure tree */
2225 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002226 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002227 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002228 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002229 /* set the bits in the inode's tree */
2230 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002231 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002232 if (ret < 0) {
2233 kfree(failrec);
2234 return ret;
2235 }
2236 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002237 btrfs_debug(fs_info,
2238 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2239 failrec->logical, failrec->start, failrec->len,
2240 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002241 /*
2242 * when data can be on disk more than twice, add to failrec here
2243 * (e.g. with a list for failed_mirror) to make
2244 * clean_io_failure() clean all those errors at once.
2245 */
2246 }
Miao Xie2fe63032014-09-12 18:43:59 +08002247
2248 *failrec_ret = failrec;
2249
2250 return 0;
2251}
2252
Ming Leia0b60d72017-12-18 20:22:11 +08002253bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002254 struct io_failure_record *failrec, int failed_mirror)
2255{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002257 int num_copies;
2258
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002259 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002260 if (num_copies == 1) {
2261 /*
2262 * we only have a single copy of the data, so don't bother with
2263 * all the retry and error correction code that follows. no
2264 * matter what the error is, it is very likely to persist.
2265 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002266 btrfs_debug(fs_info,
2267 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2268 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002269 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002270 }
2271
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002272 /*
2273 * there are two premises:
2274 * a) deliver good data to the caller
2275 * b) correct the bad sectors on disk
2276 */
Ming Leia0b60d72017-12-18 20:22:11 +08002277 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002278 /*
2279 * to fulfill b), we need to know the exact failing sectors, as
2280 * we don't want to rewrite any more than the failed ones. thus,
2281 * we need separate read requests for the failed bio
2282 *
2283 * if the following BUG_ON triggers, our validation request got
2284 * merged. we need separate requests for our algorithm to work.
2285 */
2286 BUG_ON(failrec->in_validation);
2287 failrec->in_validation = 1;
2288 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002289 } else {
2290 /*
2291 * we're ready to fulfill a) and b) alongside. get a good copy
2292 * of the failed sector and if we succeed, we have setup
2293 * everything for repair_io_failure to do the rest for us.
2294 */
2295 if (failrec->in_validation) {
2296 BUG_ON(failrec->this_mirror != failed_mirror);
2297 failrec->in_validation = 0;
2298 failrec->this_mirror = 0;
2299 }
2300 failrec->failed_mirror = failed_mirror;
2301 failrec->this_mirror++;
2302 if (failrec->this_mirror == failed_mirror)
2303 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002304 }
2305
Miao Xiefacc8a222013-07-25 19:22:34 +08002306 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002307 btrfs_debug(fs_info,
2308 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2309 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002310 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002311 }
2312
Liu Boc3cfb652017-07-13 15:00:50 -07002313 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002314}
2315
2316
2317struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2318 struct io_failure_record *failrec,
2319 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002320 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002321{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002323 struct bio *bio;
2324 struct btrfs_io_bio *btrfs_failed_bio;
2325 struct btrfs_io_bio *btrfs_bio;
2326
David Sterbac5e4c3d2017-06-12 17:29:41 +02002327 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002328 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002329 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002330 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002331 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002332 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002333
Miao Xiefacc8a222013-07-25 19:22:34 +08002334 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2335 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002336 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2337
2338 btrfs_bio = btrfs_io_bio(bio);
2339 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002340 icsum *= csum_size;
2341 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002342 csum_size);
2343 }
2344
Miao Xie2fe63032014-09-12 18:43:59 +08002345 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002346
Miao Xie2fe63032014-09-12 18:43:59 +08002347 return bio;
2348}
2349
2350/*
2351 * this is a generic handler for readpage errors (default
2352 * readpage_io_failed_hook). if other copies exist, read those and write back
2353 * good data to the failed position. does not investigate in remapping the
2354 * failed extent elsewhere, hoping the device will be smart enough to do this as
2355 * needed
2356 */
2357
2358static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2359 struct page *page, u64 start, u64 end,
2360 int failed_mirror)
2361{
2362 struct io_failure_record *failrec;
2363 struct inode *inode = page->mapping->host;
2364 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002365 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002366 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002367 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002368 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002369 int ret;
Ming Leia0b60d72017-12-18 20:22:11 +08002370 unsigned failed_bio_pages = bio_pages_all(failed_bio);
Miao Xie2fe63032014-09-12 18:43:59 +08002371
Mike Christie1f7ad752016-06-05 14:31:51 -05002372 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002373
2374 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2375 if (ret)
2376 return ret;
2377
Ming Leia0b60d72017-12-18 20:22:11 +08002378 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002379 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002380 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002381 return -EIO;
2382 }
2383
Ming Leia0b60d72017-12-18 20:22:11 +08002384 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002385 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002386
2387 phy_offset >>= inode->i_sb->s_blocksize_bits;
2388 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2389 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002390 (int)phy_offset, failed_bio->bi_end_io,
2391 NULL);
David Sterbaebcc3262018-06-29 10:56:53 +02002392 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie2fe63032014-09-12 18:43:59 +08002393
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002394 btrfs_debug(btrfs_sb(inode->i_sb),
2395 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2396 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002397
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002398 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002399 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002400 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002401 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002402 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002403 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002404 }
2405
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002406 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002407}
2408
Chris Masond1310b22008-01-24 16:13:08 -05002409/* lots and lots of room for performance fixes in the end_bio funcs */
2410
David Sterbab5227c02015-12-03 13:08:59 +01002411void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002412{
2413 int uptodate = (err == 0);
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002414 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002415
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02002416 btrfs_writepage_endio_finish_ordered(page, start, end, NULL, uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002417
Jeff Mahoney87826df2012-02-15 16:23:57 +01002418 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002419 ClearPageUptodate(page);
2420 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002421 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002422 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002423 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002424}
2425
Chris Masond1310b22008-01-24 16:13:08 -05002426/*
2427 * after a writepage IO is done, we need to:
2428 * clear the uptodate bits on error
2429 * clear the writeback bits in the extent tree for this IO
2430 * end_page_writeback if the page has no more pending IO
2431 *
2432 * Scheduling is not allowed, so the extent state tree is expected
2433 * to have one and only one object corresponding to this IO.
2434 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002435static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002436{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002437 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002438 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002439 u64 start;
2440 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002441 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002442
David Sterbac09abff2017-07-13 18:10:07 +02002443 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002444 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002445 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002446 struct inode *inode = page->mapping->host;
2447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002448
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002449 /* We always issue full-page reads, but if some block
2450 * in a page fails to read, blk_update_request() will
2451 * advance bv_offset and adjust bv_len to compensate.
2452 * Print a warning for nonzero offsets, and an error
2453 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002454 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2455 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002457 "partial page write in btrfs with offset %u and length %u",
2458 bvec->bv_offset, bvec->bv_len);
2459 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002460 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002461 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002462 bvec->bv_offset, bvec->bv_len);
2463 }
Chris Masond1310b22008-01-24 16:13:08 -05002464
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002465 start = page_offset(page);
2466 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002467
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002468 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002469 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002470 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002471
Chris Masond1310b22008-01-24 16:13:08 -05002472 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002473}
2474
Miao Xie883d0de2013-07-25 19:22:35 +08002475static void
2476endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2477 int uptodate)
2478{
2479 struct extent_state *cached = NULL;
2480 u64 end = start + len - 1;
2481
2482 if (uptodate && tree->track_uptodate)
2483 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002484 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002485}
2486
Chris Masond1310b22008-01-24 16:13:08 -05002487/*
2488 * after a readpage IO is done, we need to:
2489 * clear the uptodate bits on error
2490 * set the uptodate bits if things worked
2491 * set the page up to date if all extents in the tree are uptodate
2492 * clear the lock bit in the extent tree
2493 * unlock the page if there are no other extents locked for it
2494 *
2495 * Scheduling is not allowed, so the extent state tree is expected
2496 * to have one and only one object corresponding to this IO.
2497 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002498static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002499{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002500 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002501 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002502 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002503 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002504 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002505 u64 start;
2506 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002507 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002508 u64 extent_start = 0;
2509 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002510 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002511 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002512 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002513
David Sterbac09abff2017-07-13 18:10:07 +02002514 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002515 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002516 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002517 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002518 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002519
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002520 btrfs_debug(fs_info,
2521 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002522 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002523 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002524 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002525 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002526
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002527 /* We always issue full-page reads, but if some block
2528 * in a page fails to read, blk_update_request() will
2529 * advance bv_offset and adjust bv_len to compensate.
2530 * Print a warning for nonzero offsets, and an error
2531 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002532 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2533 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002534 btrfs_err(fs_info,
2535 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002536 bvec->bv_offset, bvec->bv_len);
2537 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002538 btrfs_info(fs_info,
2539 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002540 bvec->bv_offset, bvec->bv_len);
2541 }
Chris Masond1310b22008-01-24 16:13:08 -05002542
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002543 start = page_offset(page);
2544 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002545 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002546
Chris Mason9be33952013-05-17 18:30:14 -04002547 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002548 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002549 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2550 page, start, end,
2551 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002552 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002553 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002554 else
Josef Bacik7870d082017-05-05 11:57:15 -04002555 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2556 failure_tree, tree, start,
2557 page,
2558 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002559 }
Josef Bacikea466792012-03-26 21:57:36 -04002560
Miao Xief2a09da2013-07-25 19:22:33 +08002561 if (likely(uptodate))
2562 goto readpage_ok;
2563
David Sterba20a7db82017-02-17 16:24:29 +01002564 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002565 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002566 if (ret == -EAGAIN) {
2567 /*
2568 * Data inode's readpage_io_failed_hook() always
2569 * returns -EAGAIN.
2570 *
2571 * The generic bio_readpage_error handles errors
2572 * the following way: If possible, new read
2573 * requests are created and submitted and will
2574 * end up in end_bio_extent_readpage as well (if
2575 * we're lucky, not in the !uptodate case). In
2576 * that case it returns 0 and we just go on with
2577 * the next page in our bio. If it can't handle
2578 * the error it will return -EIO and we remain
2579 * responsible for that page.
2580 */
2581 ret = bio_readpage_error(bio, offset, page,
2582 start, end, mirror);
2583 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002584 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002585 offset += len;
2586 continue;
2587 }
Chris Mason7e383262008-04-09 16:28:12 -04002588 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002589
2590 /*
2591 * metadata's readpage_io_failed_hook() always returns
2592 * -EIO and fixes nothing. -EIO is also returned if
2593 * data inode error could not be fixed.
2594 */
2595 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002596 }
Miao Xief2a09da2013-07-25 19:22:33 +08002597readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002598 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002599 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002600 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002601 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002602
2603 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002604 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002605 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002606 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002607 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002608 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002609 ClearPageUptodate(page);
2610 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002611 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002612 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002613 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002614
2615 if (unlikely(!uptodate)) {
2616 if (extent_len) {
2617 endio_readpage_release_extent(tree,
2618 extent_start,
2619 extent_len, 1);
2620 extent_start = 0;
2621 extent_len = 0;
2622 }
2623 endio_readpage_release_extent(tree, start,
2624 end - start + 1, 0);
2625 } else if (!extent_len) {
2626 extent_start = start;
2627 extent_len = end + 1 - start;
2628 } else if (extent_start + extent_len == start) {
2629 extent_len += end + 1 - start;
2630 } else {
2631 endio_readpage_release_extent(tree, extent_start,
2632 extent_len, uptodate);
2633 extent_start = start;
2634 extent_len = end + 1 - start;
2635 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002636 }
Chris Masond1310b22008-01-24 16:13:08 -05002637
Miao Xie883d0de2013-07-25 19:22:35 +08002638 if (extent_len)
2639 endio_readpage_release_extent(tree, extent_start, extent_len,
2640 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002641 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002642 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002643 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002644}
2645
Chris Mason9be33952013-05-17 18:30:14 -04002646/*
David Sterba184f9992017-06-12 17:29:39 +02002647 * Initialize the members up to but not including 'bio'. Use after allocating a
2648 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2649 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002650 */
David Sterba184f9992017-06-12 17:29:39 +02002651static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002652{
David Sterba184f9992017-06-12 17:29:39 +02002653 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2654}
2655
2656/*
David Sterba6e707bc2017-06-02 17:26:26 +02002657 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2658 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2659 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002660 */
David Sterbac821e7f32017-06-02 18:35:36 +02002661struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002662{
2663 struct bio *bio;
2664
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002665 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002666 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002667 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002668 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002669 return bio;
2670}
2671
David Sterba8b6c1d52017-06-02 17:48:13 +02002672struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002673{
Miao Xie23ea8e52014-09-12 18:43:54 +08002674 struct btrfs_io_bio *btrfs_bio;
2675 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002676
David Sterba6e707bc2017-06-02 17:26:26 +02002677 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002678 new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002679 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002680 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002681 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002682 return new;
2683}
Chris Mason9be33952013-05-17 18:30:14 -04002684
David Sterbac5e4c3d2017-06-12 17:29:41 +02002685struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002686{
Miao Xiefacc8a222013-07-25 19:22:34 +08002687 struct bio *bio;
2688
David Sterba6e707bc2017-06-02 17:26:26 +02002689 /* Bio allocation backed by a bioset does not fail */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002690 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002691 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002692 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002693}
2694
Liu Boe4770942017-05-16 10:57:14 -07002695struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002696{
2697 struct bio *bio;
2698 struct btrfs_io_bio *btrfs_bio;
2699
2700 /* this will never fail when it's backed by a bioset */
Kent Overstreet8ac9f7c2018-05-20 18:25:56 -04002701 bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002702 ASSERT(bio);
2703
2704 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002705 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002706
2707 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002708 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002709 return bio;
2710}
Chris Mason9be33952013-05-17 18:30:14 -04002711
Mike Christie1f7ad752016-06-05 14:31:51 -05002712static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2713 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002714{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002715 blk_status_t ret = 0;
Ming Leic45a8f22017-12-18 20:22:05 +08002716 struct bio_vec *bvec = bio_last_bvec_all(bio);
Chris Mason70dec802008-01-29 09:59:12 -05002717 struct page *page = bvec->bv_page;
2718 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002719 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002720
Miao Xie4eee4fa2012-12-21 09:17:45 +00002721 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002722
David Woodhouse902b22f2008-08-20 08:51:49 -04002723 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002724
David Sterba20c98012017-02-17 15:59:35 +01002725 if (tree->ops)
Josef Bacikc6100a42017-05-05 11:57:13 -04002726 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002727 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002728 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002729 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002730
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002731 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002732}
2733
David Sterba4b81ba42017-06-06 19:14:26 +02002734/*
2735 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002736 * @tree: tree so we can call our merge_bio hook
2737 * @wbc: optional writeback control for io accounting
2738 * @page: page to add to the bio
2739 * @pg_offset: offset of the new bio or to check whether we are adding
2740 * a contiguous page to the previous one
2741 * @size: portion of page that we want to write
2742 * @offset: starting offset in the page
2743 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002744 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002745 * @end_io_func: end_io callback for new bio
2746 * @mirror_num: desired mirror to read/write
2747 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2748 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002749 */
2750static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002751 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002752 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002753 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002754 struct block_device *bdev,
2755 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002756 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002757 int mirror_num,
2758 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002759 unsigned long bio_flags,
2760 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002761{
2762 int ret = 0;
2763 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002764 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002765 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002766
David Sterba5c2b1fd2017-06-06 19:22:55 +02002767 ASSERT(bio_ret);
2768
2769 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002770 bool contig;
2771 bool can_merge = true;
2772
Chris Masond1310b22008-01-24 16:13:08 -05002773 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002774 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002775 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002776 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002777 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002778
David Sterba00032d32018-07-18 19:28:09 +02002779 if (tree->ops && btrfs_merge_bio_hook(page, offset, page_size,
2780 bio, bio_flags))
David Sterba0c8508a2017-06-12 20:00:43 +02002781 can_merge = false;
2782
2783 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002784 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002785 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002786 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002787 if (ret < 0) {
2788 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002789 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002790 }
Chris Masond1310b22008-01-24 16:13:08 -05002791 bio = NULL;
2792 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002793 if (wbc)
2794 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002795 return 0;
2796 }
2797 }
Chris Masonc8b97812008-10-29 14:49:59 -04002798
David Sterba6273b7f2017-10-04 17:30:11 +02002799 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002800 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002801 bio->bi_end_io = end_io_func;
2802 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002803 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002804 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002805 if (wbc) {
2806 wbc_init_bio(wbc, bio);
2807 wbc_account_io(wbc, page, page_size);
2808 }
Chris Mason70dec802008-01-29 09:59:12 -05002809
David Sterba5c2b1fd2017-06-06 19:22:55 +02002810 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002811
2812 return ret;
2813}
2814
Eric Sandeen48a3b632013-04-25 20:41:01 +00002815static void attach_extent_buffer_page(struct extent_buffer *eb,
2816 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002817{
2818 if (!PagePrivate(page)) {
2819 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002820 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002821 set_page_private(page, (unsigned long)eb);
2822 } else {
2823 WARN_ON(page->private != (unsigned long)eb);
2824 }
2825}
2826
Chris Masond1310b22008-01-24 16:13:08 -05002827void set_page_extent_mapped(struct page *page)
2828{
2829 if (!PagePrivate(page)) {
2830 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002831 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002832 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002833 }
2834}
2835
Miao Xie125bac012013-07-25 19:22:37 +08002836static struct extent_map *
2837__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2838 u64 start, u64 len, get_extent_t *get_extent,
2839 struct extent_map **em_cached)
2840{
2841 struct extent_map *em;
2842
2843 if (em_cached && *em_cached) {
2844 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002845 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002846 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002847 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002848 return em;
2849 }
2850
2851 free_extent_map(em);
2852 *em_cached = NULL;
2853 }
2854
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002855 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002856 if (em_cached && !IS_ERR_OR_NULL(em)) {
2857 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002858 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002859 *em_cached = em;
2860 }
2861 return em;
2862}
Chris Masond1310b22008-01-24 16:13:08 -05002863/*
2864 * basic readpage implementation. Locked extent state structs are inserted
2865 * into the tree that are removed when the IO is done (by the end_io
2866 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002867 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002868 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002869 */
Miao Xie99740902013-07-25 19:22:36 +08002870static int __do_readpage(struct extent_io_tree *tree,
2871 struct page *page,
2872 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002873 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002874 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002875 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002876 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002877{
2878 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002879 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002880 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002881 u64 cur = start;
2882 u64 extent_offset;
2883 u64 last_byte = i_size_read(inode);
2884 u64 block_start;
2885 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002886 struct extent_map *em;
2887 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002888 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002889 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002890 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002891 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002892 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002893 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002894 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002895
2896 set_page_extent_mapped(page);
2897
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002898 if (!PageUptodate(page)) {
2899 if (cleancache_get_page(page) == 0) {
2900 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002901 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002902 goto out;
2903 }
2904 }
2905
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002906 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002907 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002908 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002909
2910 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002911 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002912 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002913 memset(userpage + zero_offset, 0, iosize);
2914 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002915 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002916 }
2917 }
Chris Masond1310b22008-01-24 16:13:08 -05002918 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002919 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002920 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002921
Chris Masond1310b22008-01-24 16:13:08 -05002922 if (cur >= last_byte) {
2923 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002924 struct extent_state *cached = NULL;
2925
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002926 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002927 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002928 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002929 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002930 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002931 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002932 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002933 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002934 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002935 break;
2936 }
Miao Xie125bac012013-07-25 19:22:37 +08002937 em = __get_extent_map(inode, page, pg_offset, cur,
2938 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002939 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002940 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002941 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002942 break;
2943 }
Chris Masond1310b22008-01-24 16:13:08 -05002944 extent_offset = cur - em->start;
2945 BUG_ON(extent_map_end(em) <= cur);
2946 BUG_ON(end < cur);
2947
Li Zefan261507a02010-12-17 14:21:50 +08002948 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002949 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002950 extent_set_compress_type(&this_bio_flag,
2951 em->compress_type);
2952 }
Chris Masonc8b97812008-10-29 14:49:59 -04002953
Chris Masond1310b22008-01-24 16:13:08 -05002954 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2955 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002956 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002957 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2958 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002959 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002960 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002961 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002962 disk_io_size = iosize;
2963 }
Chris Masond1310b22008-01-24 16:13:08 -05002964 bdev = em->bdev;
2965 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002966 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2967 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002968
2969 /*
2970 * If we have a file range that points to a compressed extent
2971 * and it's followed by a consecutive file range that points to
2972 * to the same compressed extent (possibly with a different
2973 * offset and/or length, so it either points to the whole extent
2974 * or only part of it), we must make sure we do not submit a
2975 * single bio to populate the pages for the 2 ranges because
2976 * this makes the compressed extent read zero out the pages
2977 * belonging to the 2nd range. Imagine the following scenario:
2978 *
2979 * File layout
2980 * [0 - 8K] [8K - 24K]
2981 * | |
2982 * | |
2983 * points to extent X, points to extent X,
2984 * offset 4K, length of 8K offset 0, length 16K
2985 *
2986 * [extent X, compressed length = 4K uncompressed length = 16K]
2987 *
2988 * If the bio to read the compressed extent covers both ranges,
2989 * it will decompress extent X into the pages belonging to the
2990 * first range and then it will stop, zeroing out the remaining
2991 * pages that belong to the other range that points to extent X.
2992 * So here we make sure we submit 2 bios, one for the first
2993 * range and another one for the third range. Both will target
2994 * the same physical extent from disk, but we can't currently
2995 * make the compressed bio endio callback populate the pages
2996 * for both ranges because each compressed bio is tightly
2997 * coupled with a single extent map, and each range can have
2998 * an extent map with a different offset value relative to the
2999 * uncompressed data of our extent and different lengths. This
3000 * is a corner case so we prioritize correctness over
3001 * non-optimal behavior (submitting 2 bios for the same extent).
3002 */
3003 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3004 prev_em_start && *prev_em_start != (u64)-1 &&
3005 *prev_em_start != em->orig_start)
3006 force_bio_submit = true;
3007
3008 if (prev_em_start)
3009 *prev_em_start = em->orig_start;
3010
Chris Masond1310b22008-01-24 16:13:08 -05003011 free_extent_map(em);
3012 em = NULL;
3013
3014 /* we've found a hole, just zero and go on */
3015 if (block_start == EXTENT_MAP_HOLE) {
3016 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003017 struct extent_state *cached = NULL;
3018
Cong Wang7ac687d2011-11-25 23:14:28 +08003019 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003020 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003021 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003022 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003023
3024 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003025 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003026 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003027 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003028 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003029 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003030 continue;
3031 }
3032 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003033 if (test_range_bit(tree, cur, cur_end,
3034 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003035 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003036 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003037 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003038 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003039 continue;
3040 }
Chris Mason70dec802008-01-29 09:59:12 -05003041 /* we have an inline extent but it didn't get marked up
3042 * to date. Error out
3043 */
3044 if (block_start == EXTENT_MAP_INLINE) {
3045 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003046 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003047 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003048 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003049 continue;
3050 }
Chris Masond1310b22008-01-24 16:13:08 -05003051
David Sterba4b81ba42017-06-06 19:14:26 +02003052 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003053 page, offset, disk_io_size,
3054 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003055 end_bio_extent_readpage, mirror_num,
3056 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003057 this_bio_flag,
3058 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003059 if (!ret) {
3060 nr++;
3061 *bio_flags = this_bio_flag;
3062 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003063 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003064 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003065 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003066 }
Chris Masond1310b22008-01-24 16:13:08 -05003067 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003068 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003069 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003070out:
Chris Masond1310b22008-01-24 16:13:08 -05003071 if (!nr) {
3072 if (!PageError(page))
3073 SetPageUptodate(page);
3074 unlock_page(page);
3075 }
Liu Bobaf863b2016-07-11 10:39:07 -07003076 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003077}
3078
Miao Xie99740902013-07-25 19:22:36 +08003079static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3080 struct page *pages[], int nr_pages,
3081 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003082 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003083 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003084 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003085 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003086{
3087 struct inode *inode;
3088 struct btrfs_ordered_extent *ordered;
3089 int index;
3090
3091 inode = pages[0]->mapping->host;
3092 while (1) {
3093 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003094 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003095 end - start + 1);
3096 if (!ordered)
3097 break;
3098 unlock_extent(tree, start, end);
3099 btrfs_start_ordered_extent(inode, ordered, 1);
3100 btrfs_put_ordered_extent(ordered);
3101 }
3102
3103 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003104 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
Jens Axboe5e9d3982018-08-17 15:45:39 -07003105 bio, 0, bio_flags, REQ_RAHEAD, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003106 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003107 }
3108}
3109
3110static void __extent_readpages(struct extent_io_tree *tree,
3111 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003112 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003113 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003114 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003115 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003116{
Stefan Behrens35a36212013-08-14 18:12:25 +02003117 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003118 u64 end = 0;
3119 u64 page_start;
3120 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003121 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003122
3123 for (index = 0; index < nr_pages; index++) {
3124 page_start = page_offset(pages[index]);
3125 if (!end) {
3126 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003127 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003128 first_index = index;
3129 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003130 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003131 } else {
3132 __do_contiguous_readpages(tree, &pages[first_index],
3133 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003134 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003135 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003136 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003137 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003139 first_index = index;
3140 }
3141 }
3142
3143 if (end)
3144 __do_contiguous_readpages(tree, &pages[first_index],
3145 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003146 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003147 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003148}
3149
3150static int __extent_read_full_page(struct extent_io_tree *tree,
3151 struct page *page,
3152 get_extent_t *get_extent,
3153 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003154 unsigned long *bio_flags,
3155 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003156{
3157 struct inode *inode = page->mapping->host;
3158 struct btrfs_ordered_extent *ordered;
3159 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003160 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003161 int ret;
3162
3163 while (1) {
3164 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003165 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003166 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003167 if (!ordered)
3168 break;
3169 unlock_extent(tree, start, end);
3170 btrfs_start_ordered_extent(inode, ordered, 1);
3171 btrfs_put_ordered_extent(ordered);
3172 }
3173
Miao Xie125bac012013-07-25 19:22:37 +08003174 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003175 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003176 return ret;
3177}
3178
Chris Masond1310b22008-01-24 16:13:08 -05003179int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003180 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003181{
3182 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003183 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003184 int ret;
3185
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003186 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003187 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003188 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003189 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003190 return ret;
3191}
Chris Masond1310b22008-01-24 16:13:08 -05003192
David Sterba3d4b9492017-02-10 19:33:41 +01003193static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003194 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003195{
3196 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003197}
3198
Chris Masond1310b22008-01-24 16:13:08 -05003199/*
Chris Mason40f76582014-05-21 13:35:51 -07003200 * helper for __extent_writepage, doing all of the delayed allocation setup.
3201 *
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003202 * This returns 1 if btrfs_run_delalloc_range function did all the work required
Chris Mason40f76582014-05-21 13:35:51 -07003203 * to write the page (copy into inline extent). In this case the IO has
3204 * been started and the page is already unlocked.
3205 *
3206 * This returns 0 if all went well (page still locked)
3207 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003208 */
Chris Mason40f76582014-05-21 13:35:51 -07003209static noinline_for_stack int writepage_delalloc(struct inode *inode,
3210 struct page *page, struct writeback_control *wbc,
3211 struct extent_page_data *epd,
3212 u64 delalloc_start,
3213 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003214{
Chris Mason40f76582014-05-21 13:35:51 -07003215 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003216 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003217 u64 nr_delalloc;
3218 u64 delalloc_to_write = 0;
3219 u64 delalloc_end = 0;
3220 int ret;
3221 int page_started = 0;
3222
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003223 if (epd->extent_locked)
Chris Mason40f76582014-05-21 13:35:51 -07003224 return 0;
3225
3226 while (delalloc_end < page_end) {
3227 nr_delalloc = find_lock_delalloc_range(inode, tree,
3228 page,
3229 &delalloc_start,
3230 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003231 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003232 if (nr_delalloc == 0) {
3233 delalloc_start = delalloc_end + 1;
3234 continue;
3235 }
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003236 ret = btrfs_run_delalloc_range(inode, page, delalloc_start,
3237 delalloc_end, &page_started, nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003238 /* File system has been set read-only */
3239 if (ret) {
3240 SetPageError(page);
Nikolay Borisov5eaad972018-11-01 14:09:46 +02003241 /*
3242 * btrfs_run_delalloc_range should return < 0 for error
3243 * but just in case, we use > 0 here meaning the IO is
3244 * started, so we don't want to return > 0 unless
3245 * things are going well.
Chris Mason40f76582014-05-21 13:35:51 -07003246 */
3247 ret = ret < 0 ? ret : -EIO;
3248 goto done;
3249 }
3250 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003251 * delalloc_end is already one less than the total length, so
3252 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003253 */
3254 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003255 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003256 delalloc_start = delalloc_end + 1;
3257 }
3258 if (wbc->nr_to_write < delalloc_to_write) {
3259 int thresh = 8192;
3260
3261 if (delalloc_to_write < thresh * 2)
3262 thresh = delalloc_to_write;
3263 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3264 thresh);
3265 }
3266
3267 /* did the fill delalloc function already unlock and start
3268 * the IO?
3269 */
3270 if (page_started) {
3271 /*
3272 * we've unlocked the page, so we can't update
3273 * the mapping's writeback index, just update
3274 * nr_to_write.
3275 */
3276 wbc->nr_to_write -= *nr_written;
3277 return 1;
3278 }
3279
3280 ret = 0;
3281
3282done:
3283 return ret;
3284}
3285
3286/*
3287 * helper for __extent_writepage. This calls the writepage start hooks,
3288 * and does the loop to map the page into extents and bios.
3289 *
3290 * We return 1 if the IO is started and the page is unlocked,
3291 * 0 if all went well (page still locked)
3292 * < 0 if there were errors (page still locked)
3293 */
3294static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3295 struct page *page,
3296 struct writeback_control *wbc,
3297 struct extent_page_data *epd,
3298 loff_t i_size,
3299 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003300 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003301{
Chris Masond1310b22008-01-24 16:13:08 -05003302 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003303 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003304 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003305 u64 end;
3306 u64 cur = start;
3307 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003308 u64 block_start;
3309 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003310 struct extent_map *em;
3311 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003312 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003313 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003314 int ret = 0;
3315 int nr = 0;
3316 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003317
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003318 ret = btrfs_writepage_cow_fixup(page, start, page_end);
3319 if (ret) {
3320 /* Fixup worker will requeue */
3321 if (ret == -EBUSY)
3322 wbc->pages_skipped++;
3323 else
3324 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003325
Nikolay Borisovd75855b2018-11-01 14:09:47 +02003326 update_nr_written(wbc, nr_written);
3327 unlock_page(page);
3328 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003329 }
3330
Chris Mason11c83492009-04-20 15:50:09 -04003331 /*
3332 * we don't want to touch the inode after unlocking the page,
3333 * so we update the mapping writeback index now
3334 */
David Sterba3d4b9492017-02-10 19:33:41 +01003335 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003336
Chris Masond1310b22008-01-24 16:13:08 -05003337 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003338 if (i_size <= start) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003339 btrfs_writepage_endio_finish_ordered(page, start, page_end,
3340 NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003341 goto done;
3342 }
3343
Chris Masond1310b22008-01-24 16:13:08 -05003344 blocksize = inode->i_sb->s_blocksize;
3345
3346 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003347 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003348 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003349
Chris Mason40f76582014-05-21 13:35:51 -07003350 if (cur >= i_size) {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003351 btrfs_writepage_endio_finish_ordered(page, cur,
3352 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003353 break;
3354 }
David Sterba3c98c622017-06-23 04:01:08 +02003355 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003356 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003357 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003358 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003359 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003360 break;
3361 }
3362
3363 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003364 em_end = extent_map_end(em);
3365 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003366 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003367 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003368 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003369 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003370 bdev = em->bdev;
3371 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003372 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003373 free_extent_map(em);
3374 em = NULL;
3375
Chris Masonc8b97812008-10-29 14:49:59 -04003376 /*
3377 * compressed and inline extents are written through other
3378 * paths in the FS
3379 */
3380 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003381 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003382 /*
3383 * end_io notification does not happen here for
3384 * compressed extents
3385 */
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02003386 if (!compressed)
3387 btrfs_writepage_endio_finish_ordered(page, cur,
3388 cur + iosize - 1,
3389 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003390 else if (compressed) {
3391 /* we don't want to end_page_writeback on
3392 * a compressed extent. this happens
3393 * elsewhere
3394 */
3395 nr++;
3396 }
3397
3398 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003399 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003400 continue;
3401 }
Chris Masonc8b97812008-10-29 14:49:59 -04003402
David Sterba5cdc84b2018-07-18 20:32:52 +02003403 btrfs_set_range_writeback(tree, cur, cur + iosize - 1);
David Sterba58409ed2016-05-04 11:46:10 +02003404 if (!PageWriteback(page)) {
3405 btrfs_err(BTRFS_I(inode)->root->fs_info,
3406 "page %lu not writeback, cur %llu end %llu",
3407 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003408 }
David Sterba58409ed2016-05-04 11:46:10 +02003409
David Sterba4b81ba42017-06-06 19:14:26 +02003410 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003411 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003412 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003413 end_bio_extent_writepage,
3414 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003415 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003416 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003417 if (PageWriteback(page))
3418 end_page_writeback(page);
3419 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003420
Chris Masond1310b22008-01-24 16:13:08 -05003421 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003422 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003423 nr++;
3424 }
3425done:
Chris Mason40f76582014-05-21 13:35:51 -07003426 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003427 return ret;
3428}
3429
3430/*
3431 * the writepage semantics are similar to regular writepage. extent
3432 * records are inserted to lock ranges in the tree, and as dirty areas
3433 * are found, they are marked writeback. Then the lock bits are removed
3434 * and the end_io handler clears the writeback ranges
3435 */
3436static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003437 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003438{
3439 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003440 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003441 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003442 int ret;
3443 int nr = 0;
3444 size_t pg_offset = 0;
3445 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003446 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003447 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003448 unsigned long nr_written = 0;
3449
Liu Boff40adf2017-08-24 18:19:48 -06003450 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003451
3452 trace___extent_writepage(page, inode, wbc);
3453
3454 WARN_ON(!PageLocked(page));
3455
3456 ClearPageError(page);
3457
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003458 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003459 if (page->index > end_index ||
3460 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003461 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003462 unlock_page(page);
3463 return 0;
3464 }
3465
3466 if (page->index == end_index) {
3467 char *userpage;
3468
3469 userpage = kmap_atomic(page);
3470 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003471 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003472 kunmap_atomic(userpage);
3473 flush_dcache_page(page);
3474 }
3475
3476 pg_offset = 0;
3477
3478 set_page_extent_mapped(page);
3479
3480 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3481 if (ret == 1)
3482 goto done_unlocked;
3483 if (ret)
3484 goto done;
3485
3486 ret = __extent_writepage_io(inode, page, wbc, epd,
3487 i_size, nr_written, write_flags, &nr);
3488 if (ret == 1)
3489 goto done_unlocked;
3490
3491done:
Chris Masond1310b22008-01-24 16:13:08 -05003492 if (nr == 0) {
3493 /* make sure the mapping tag for page dirty gets cleared */
3494 set_page_writeback(page);
3495 end_page_writeback(page);
3496 }
Filipe Manana61391d52014-05-09 17:17:40 +01003497 if (PageError(page)) {
3498 ret = ret < 0 ? ret : -EIO;
3499 end_extent_writepage(page, ret, start, page_end);
3500 }
Chris Masond1310b22008-01-24 16:13:08 -05003501 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003502 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003503
Chris Mason11c83492009-04-20 15:50:09 -04003504done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003505 return 0;
3506}
3507
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003508void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003509{
NeilBrown74316202014-07-07 15:16:04 +10003510 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3511 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003512}
3513
Chris Mason0e378df2014-05-19 20:55:27 -07003514static noinline_for_stack int
3515lock_extent_buffer_for_io(struct extent_buffer *eb,
3516 struct btrfs_fs_info *fs_info,
3517 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003518{
David Sterbacc5e31a2018-03-01 18:20:27 +01003519 int i, num_pages;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003520 int flush = 0;
3521 int ret = 0;
3522
3523 if (!btrfs_try_tree_write_lock(eb)) {
3524 flush = 1;
3525 flush_write_bio(epd);
3526 btrfs_tree_lock(eb);
3527 }
3528
3529 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3530 btrfs_tree_unlock(eb);
3531 if (!epd->sync_io)
3532 return 0;
3533 if (!flush) {
3534 flush_write_bio(epd);
3535 flush = 1;
3536 }
Chris Masona098d8e82012-03-21 12:09:56 -04003537 while (1) {
3538 wait_on_extent_buffer_writeback(eb);
3539 btrfs_tree_lock(eb);
3540 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3541 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003542 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003543 }
3544 }
3545
Josef Bacik51561ff2012-07-20 16:25:24 -04003546 /*
3547 * We need to do this to prevent races in people who check if the eb is
3548 * under IO since we can end up having no IO bits set for a short period
3549 * of time.
3550 */
3551 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003552 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3553 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003554 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003555 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003556 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3557 -eb->len,
3558 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003559 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003560 } else {
3561 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003562 }
3563
3564 btrfs_tree_unlock(eb);
3565
3566 if (!ret)
3567 return ret;
3568
David Sterba65ad0102018-06-29 10:56:49 +02003569 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003570 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003571 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003572
3573 if (!trylock_page(p)) {
3574 if (!flush) {
3575 flush_write_bio(epd);
3576 flush = 1;
3577 }
3578 lock_page(p);
3579 }
3580 }
3581
3582 return ret;
3583}
3584
3585static void end_extent_buffer_writeback(struct extent_buffer *eb)
3586{
3587 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003588 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003589 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3590}
3591
Filipe Manana656f30d2014-09-26 12:25:56 +01003592static void set_btree_ioerr(struct page *page)
3593{
3594 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003595
3596 SetPageError(page);
3597 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3598 return;
3599
3600 /*
3601 * If writeback for a btree extent that doesn't belong to a log tree
3602 * failed, increment the counter transaction->eb_write_errors.
3603 * We do this because while the transaction is running and before it's
3604 * committing (when we call filemap_fdata[write|wait]_range against
3605 * the btree inode), we might have
3606 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3607 * returns an error or an error happens during writeback, when we're
3608 * committing the transaction we wouldn't know about it, since the pages
3609 * can be no longer dirty nor marked anymore for writeback (if a
3610 * subsequent modification to the extent buffer didn't happen before the
3611 * transaction commit), which makes filemap_fdata[write|wait]_range not
3612 * able to find the pages tagged with SetPageError at transaction
3613 * commit time. So if this happens we must abort the transaction,
3614 * otherwise we commit a super block with btree roots that point to
3615 * btree nodes/leafs whose content on disk is invalid - either garbage
3616 * or the content of some node/leaf from a past generation that got
3617 * cowed or deleted and is no longer valid.
3618 *
3619 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3620 * not be enough - we need to distinguish between log tree extents vs
3621 * non-log tree extents, and the next filemap_fdatawait_range() call
3622 * will catch and clear such errors in the mapping - and that call might
3623 * be from a log sync and not from a transaction commit. Also, checking
3624 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3625 * not done and would not be reliable - the eb might have been released
3626 * from memory and reading it back again means that flag would not be
3627 * set (since it's a runtime flag, not persisted on disk).
3628 *
3629 * Using the flags below in the btree inode also makes us achieve the
3630 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3631 * writeback for all dirty pages and before filemap_fdatawait_range()
3632 * is called, the writeback for all dirty pages had already finished
3633 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3634 * filemap_fdatawait_range() would return success, as it could not know
3635 * that writeback errors happened (the pages were no longer tagged for
3636 * writeback).
3637 */
3638 switch (eb->log_index) {
3639 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003640 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003641 break;
3642 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003643 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003644 break;
3645 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003646 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003647 break;
3648 default:
3649 BUG(); /* unexpected, logic error */
3650 }
3651}
3652
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003653static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003654{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003655 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003656 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003657 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003658
David Sterbac09abff2017-07-13 18:10:07 +02003659 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003660 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003661 struct page *page = bvec->bv_page;
3662
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003663 eb = (struct extent_buffer *)page->private;
3664 BUG_ON(!eb);
3665 done = atomic_dec_and_test(&eb->io_pages);
3666
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003667 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003668 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003669 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003670 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003671 }
3672
3673 end_page_writeback(page);
3674
3675 if (!done)
3676 continue;
3677
3678 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003679 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003680
3681 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682}
3683
Chris Mason0e378df2014-05-19 20:55:27 -07003684static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003685 struct btrfs_fs_info *fs_info,
3686 struct writeback_control *wbc,
3687 struct extent_page_data *epd)
3688{
3689 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003690 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003692 u32 nritems;
David Sterbacc5e31a2018-03-01 18:20:27 +01003693 int i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003694 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003695 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003696 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003697
Filipe Manana656f30d2014-09-26 12:25:56 +01003698 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02003699 num_pages = num_extent_pages(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003700 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003701
Liu Bo851cd172016-09-23 13:44:44 -07003702 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3703 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003704 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003705 end = btrfs_node_key_ptr_offset(nritems);
3706
David Sterbab159fa22016-11-08 18:09:03 +01003707 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003708 } else {
3709 /*
3710 * leaf:
3711 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3712 */
3713 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003714 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003715 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003716 }
3717
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003718 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003719 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003720
3721 clear_page_dirty_for_io(p);
3722 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003723 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003724 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003725 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003726 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003727 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003728 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003729 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003730 if (PageWriteback(p))
3731 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003732 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3733 end_extent_buffer_writeback(eb);
3734 ret = -EIO;
3735 break;
3736 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003737 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003738 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003739 unlock_page(p);
3740 }
3741
3742 if (unlikely(ret)) {
3743 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003744 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003745 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003746 unlock_page(p);
3747 }
3748 }
3749
3750 return ret;
3751}
3752
3753int btree_write_cache_pages(struct address_space *mapping,
3754 struct writeback_control *wbc)
3755{
3756 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3757 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3758 struct extent_buffer *eb, *prev_eb = NULL;
3759 struct extent_page_data epd = {
3760 .bio = NULL,
3761 .tree = tree,
3762 .extent_locked = 0,
3763 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3764 };
3765 int ret = 0;
3766 int done = 0;
3767 int nr_to_write_done = 0;
3768 struct pagevec pvec;
3769 int nr_pages;
3770 pgoff_t index;
3771 pgoff_t end; /* Inclusive */
3772 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003773 xa_mark_t tag;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003774
Mel Gorman86679822017-11-15 17:37:52 -08003775 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003776 if (wbc->range_cyclic) {
3777 index = mapping->writeback_index; /* Start from prev offset */
3778 end = -1;
3779 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003780 index = wbc->range_start >> PAGE_SHIFT;
3781 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003782 scanned = 1;
3783 }
3784 if (wbc->sync_mode == WB_SYNC_ALL)
3785 tag = PAGECACHE_TAG_TOWRITE;
3786 else
3787 tag = PAGECACHE_TAG_DIRTY;
3788retry:
3789 if (wbc->sync_mode == WB_SYNC_ALL)
3790 tag_pages_for_writeback(mapping, index, end);
3791 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003792 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003793 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003794 unsigned i;
3795
3796 scanned = 1;
3797 for (i = 0; i < nr_pages; i++) {
3798 struct page *page = pvec.pages[i];
3799
3800 if (!PagePrivate(page))
3801 continue;
3802
Josef Bacikb5bae262012-09-14 13:43:01 -04003803 spin_lock(&mapping->private_lock);
3804 if (!PagePrivate(page)) {
3805 spin_unlock(&mapping->private_lock);
3806 continue;
3807 }
3808
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003809 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003810
3811 /*
3812 * Shouldn't happen and normally this would be a BUG_ON
3813 * but no sense in crashing the users box for something
3814 * we can survive anyway.
3815 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303816 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003817 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003818 continue;
3819 }
3820
Josef Bacikb5bae262012-09-14 13:43:01 -04003821 if (eb == prev_eb) {
3822 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003823 continue;
3824 }
3825
Josef Bacikb5bae262012-09-14 13:43:01 -04003826 ret = atomic_inc_not_zero(&eb->refs);
3827 spin_unlock(&mapping->private_lock);
3828 if (!ret)
3829 continue;
3830
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003831 prev_eb = eb;
3832 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3833 if (!ret) {
3834 free_extent_buffer(eb);
3835 continue;
3836 }
3837
3838 ret = write_one_eb(eb, fs_info, wbc, &epd);
3839 if (ret) {
3840 done = 1;
3841 free_extent_buffer(eb);
3842 break;
3843 }
3844 free_extent_buffer(eb);
3845
3846 /*
3847 * the filesystem may choose to bump up nr_to_write.
3848 * We have to make sure to honor the new nr_to_write
3849 * at any time
3850 */
3851 nr_to_write_done = wbc->nr_to_write <= 0;
3852 }
3853 pagevec_release(&pvec);
3854 cond_resched();
3855 }
3856 if (!scanned && !done) {
3857 /*
3858 * We hit the last page and there is more work to be done: wrap
3859 * back to the start of the file
3860 */
3861 scanned = 1;
3862 index = 0;
3863 goto retry;
3864 }
3865 flush_write_bio(&epd);
3866 return ret;
3867}
3868
Chris Masond1310b22008-01-24 16:13:08 -05003869/**
Chris Mason4bef0842008-09-08 11:18:08 -04003870 * 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 -05003871 * @mapping: address space structure to write
3872 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003873 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003874 *
3875 * If a page is already under I/O, write_cache_pages() skips it, even
3876 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3877 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3878 * and msync() need to guarantee that all the data which was dirty at the time
3879 * the call was made get new I/O started against them. If wbc->sync_mode is
3880 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3881 * existing IO to complete.
3882 */
David Sterba4242b642017-02-10 19:38:24 +01003883static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003884 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003885 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003886{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003887 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003888 int ret = 0;
3889 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003890 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003891 struct pagevec pvec;
3892 int nr_pages;
3893 pgoff_t index;
3894 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003895 pgoff_t done_index;
3896 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003897 int scanned = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05003898 xa_mark_t tag;
Chris Masond1310b22008-01-24 16:13:08 -05003899
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003900 /*
3901 * We have to hold onto the inode so that ordered extents can do their
3902 * work when the IO finishes. The alternative to this is failing to add
3903 * an ordered extent if the igrab() fails there and that is a huge pain
3904 * to deal with, so instead just hold onto the inode throughout the
3905 * writepages operation. If it fails here we are freeing up the inode
3906 * anyway and we'd rather not waste our time writing out stuff that is
3907 * going to be truncated anyway.
3908 */
3909 if (!igrab(inode))
3910 return 0;
3911
Mel Gorman86679822017-11-15 17:37:52 -08003912 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003913 if (wbc->range_cyclic) {
3914 index = mapping->writeback_index; /* Start from prev offset */
3915 end = -1;
3916 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003917 index = wbc->range_start >> PAGE_SHIFT;
3918 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003919 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3920 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003921 scanned = 1;
3922 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003923 if (wbc->sync_mode == WB_SYNC_ALL)
3924 tag = PAGECACHE_TAG_TOWRITE;
3925 else
3926 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003927retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003928 if (wbc->sync_mode == WB_SYNC_ALL)
3929 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003930 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003931 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003932 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3933 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003934 unsigned i;
3935
3936 scanned = 1;
3937 for (i = 0; i < nr_pages; i++) {
3938 struct page *page = pvec.pages[i];
3939
Liu Boa91326672016-03-07 16:56:21 -08003940 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003941 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07003942 * At this point we hold neither the i_pages lock nor
3943 * the page lock: the page may be truncated or
3944 * invalidated (changing page->mapping to NULL),
3945 * or even swizzled back from swapper_space to
3946 * tmpfs file mapping
Chris Masond1310b22008-01-24 16:13:08 -05003947 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003948 if (!trylock_page(page)) {
David Sterbaaab6e9e2017-11-30 18:00:02 +01003949 flush_write_bio(epd);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003950 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003951 }
Chris Masond1310b22008-01-24 16:13:08 -05003952
3953 if (unlikely(page->mapping != mapping)) {
3954 unlock_page(page);
3955 continue;
3956 }
3957
Chris Masond2c3f4f2008-11-19 12:44:22 -05003958 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003959 if (PageWriteback(page))
David Sterbaaab6e9e2017-11-30 18:00:02 +01003960 flush_write_bio(epd);
Chris Masond1310b22008-01-24 16:13:08 -05003961 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003962 }
Chris Masond1310b22008-01-24 16:13:08 -05003963
3964 if (PageWriteback(page) ||
3965 !clear_page_dirty_for_io(page)) {
3966 unlock_page(page);
3967 continue;
3968 }
3969
David Sterbaaab6e9e2017-11-30 18:00:02 +01003970 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003971
3972 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3973 unlock_page(page);
3974 ret = 0;
3975 }
Liu Boa91326672016-03-07 16:56:21 -08003976 if (ret < 0) {
3977 /*
3978 * done_index is set past this page,
3979 * so media errors will not choke
3980 * background writeout for the entire
3981 * file. This has consequences for
3982 * range_cyclic semantics (ie. it may
3983 * not be suitable for data integrity
3984 * writeout).
3985 */
3986 done_index = page->index + 1;
3987 done = 1;
3988 break;
3989 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003990
3991 /*
3992 * the filesystem may choose to bump up nr_to_write.
3993 * We have to make sure to honor the new nr_to_write
3994 * at any time
3995 */
3996 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003997 }
3998 pagevec_release(&pvec);
3999 cond_resched();
4000 }
Liu Bo894b36e2016-03-07 16:56:22 -08004001 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004002 /*
4003 * We hit the last page and there is more work to be done: wrap
4004 * back to the start of the file
4005 */
4006 scanned = 1;
4007 index = 0;
4008 goto retry;
4009 }
Liu Boa91326672016-03-07 16:56:21 -08004010
4011 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4012 mapping->writeback_index = done_index;
4013
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004014 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004015 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004016}
Chris Masond1310b22008-01-24 16:13:08 -05004017
David Sterbaaab6e9e2017-11-30 18:00:02 +01004018static void flush_write_bio(struct extent_page_data *epd)
Chris Masonffbd5172009-04-20 15:50:09 -04004019{
4020 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004021 int ret;
4022
Liu Bo18fdc672017-09-13 12:18:22 -06004023 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004024 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004025 epd->bio = NULL;
4026 }
4027}
4028
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004029int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004030{
4031 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004032 struct extent_page_data epd = {
4033 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004034 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004035 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004036 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004037 };
Chris Masond1310b22008-01-24 16:13:08 -05004038
Chris Masond1310b22008-01-24 16:13:08 -05004039 ret = __extent_writepage(page, wbc, &epd);
4040
David Sterbae2932ee2017-06-23 04:16:17 +02004041 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004042 return ret;
4043}
Chris Masond1310b22008-01-24 16:13:08 -05004044
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004045int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004046 int mode)
4047{
4048 int ret = 0;
4049 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004050 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004051 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004052 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4053 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004054
4055 struct extent_page_data epd = {
4056 .bio = NULL,
4057 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004058 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004059 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004060 };
4061 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004062 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004063 .nr_to_write = nr_pages * 2,
4064 .range_start = start,
4065 .range_end = end + 1,
4066 };
4067
Chris Masond3977122009-01-05 21:25:51 -05004068 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004069 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004070 if (clear_page_dirty_for_io(page))
4071 ret = __extent_writepage(page, &wbc_writepages, &epd);
4072 else {
Nikolay Borisov7087a9d2018-11-01 14:09:48 +02004073 btrfs_writepage_endio_finish_ordered(page, start,
4074 start + PAGE_SIZE - 1,
4075 NULL, 1);
Chris Mason771ed682008-11-06 22:02:51 -05004076 unlock_page(page);
4077 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004078 put_page(page);
4079 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004080 }
4081
David Sterbae2932ee2017-06-23 04:16:17 +02004082 flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004083 return ret;
4084}
Chris Masond1310b22008-01-24 16:13:08 -05004085
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004086int extent_writepages(struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004087 struct writeback_control *wbc)
4088{
4089 int ret = 0;
4090 struct extent_page_data epd = {
4091 .bio = NULL,
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03004092 .tree = &BTRFS_I(mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004093 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004094 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004095 };
4096
David Sterba935db852017-06-23 04:30:28 +02004097 ret = extent_write_cache_pages(mapping, wbc, &epd);
David Sterbae2932ee2017-06-23 04:16:17 +02004098 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004099 return ret;
4100}
Chris Masond1310b22008-01-24 16:13:08 -05004101
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004102int extent_readpages(struct address_space *mapping, struct list_head *pages,
4103 unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004104{
4105 struct bio *bio = NULL;
4106 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004107 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004108 struct page *pagepool[16];
4109 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004110 struct extent_map *em_cached = NULL;
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03004111 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
Liu Bo67c96842012-07-20 21:43:09 -06004112 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004113 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004114
Chris Masond1310b22008-01-24 16:13:08 -05004115 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004116 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004117
4118 prefetchw(&page->flags);
4119 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004120 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004121 page->index,
4122 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004123 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004124 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004125 }
Liu Bo67c96842012-07-20 21:43:09 -06004126
4127 pagepool[nr++] = page;
4128 if (nr < ARRAY_SIZE(pagepool))
4129 continue;
David Sterbae4d17ef2017-06-23 04:09:57 +02004130 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4131 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004132 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004133 }
Miao Xie99740902013-07-25 19:22:36 +08004134 if (nr)
David Sterbae4d17ef2017-06-23 04:09:57 +02004135 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4136 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004137
Miao Xie125bac012013-07-25 19:22:37 +08004138 if (em_cached)
4139 free_extent_map(em_cached);
4140
Chris Masond1310b22008-01-24 16:13:08 -05004141 BUG_ON(!list_empty(pages));
4142 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004143 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004144 return 0;
4145}
Chris Masond1310b22008-01-24 16:13:08 -05004146
4147/*
4148 * basic invalidatepage code, this waits on any locked or writeback
4149 * ranges corresponding to the page, and then deletes any extent state
4150 * records from the tree
4151 */
4152int extent_invalidatepage(struct extent_io_tree *tree,
4153 struct page *page, unsigned long offset)
4154{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004155 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004156 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004157 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004158 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4159
Qu Wenruofda28322013-02-26 08:10:22 +00004160 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004161 if (start > end)
4162 return 0;
4163
David Sterbaff13db42015-12-03 14:30:40 +01004164 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004165 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004166 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004167 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4168 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004169 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004170 return 0;
4171}
Chris Masond1310b22008-01-24 16:13:08 -05004172
4173/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004174 * a helper for releasepage, this tests for areas of the page that
4175 * are locked or under IO and drops the related state bits if it is safe
4176 * to drop the page.
4177 */
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004178static int try_release_extent_state(struct extent_io_tree *tree,
Eric Sandeen48a3b632013-04-25 20:41:01 +00004179 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004180{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004181 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004182 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004183 int ret = 1;
4184
Chris Mason211f90e2008-07-18 11:56:15 -04004185 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004186 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004187 ret = 0;
4188 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004189 /*
4190 * at this point we can safely clear everything except the
4191 * locked bit and the nodatasum bit
4192 */
David Sterba66b0c882017-10-31 16:30:47 +01004193 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004194 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004195 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004196
4197 /* if clear_extent_bit failed for enomem reasons,
4198 * we can't allow the release to continue.
4199 */
4200 if (ret < 0)
4201 ret = 0;
4202 else
4203 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004204 }
4205 return ret;
4206}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004207
4208/*
Chris Masond1310b22008-01-24 16:13:08 -05004209 * a helper for releasepage. As long as there are no locked extents
4210 * in the range corresponding to the page, both state records and extent
4211 * map records are removed
4212 */
Nikolay Borisov477a30b2018-04-19 10:46:34 +03004213int try_release_extent_mapping(struct page *page, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004214{
4215 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004216 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004217 u64 end = start + PAGE_SIZE - 1;
Filipe Mananabd3599a2018-07-12 01:36:43 +01004218 struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host);
4219 struct extent_io_tree *tree = &btrfs_inode->io_tree;
4220 struct extent_map_tree *map = &btrfs_inode->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004221
Mel Gormand0164ad2015-11-06 16:28:21 -08004222 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004223 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004224 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004225 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004226 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004227 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004228 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004229 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004230 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004231 break;
4232 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004233 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4234 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004235 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004236 free_extent_map(em);
4237 break;
4238 }
4239 if (!test_range_bit(tree, em->start,
4240 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004241 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004242 0, NULL)) {
Filipe Mananabd3599a2018-07-12 01:36:43 +01004243 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4244 &btrfs_inode->runtime_flags);
Chris Mason70dec802008-01-29 09:59:12 -05004245 remove_extent_mapping(map, em);
4246 /* once for the rb tree */
4247 free_extent_map(em);
4248 }
4249 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004250 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004251
4252 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004253 free_extent_map(em);
4254 }
Chris Masond1310b22008-01-24 16:13:08 -05004255 }
Nikolay Borisov29c68b2d2018-04-19 10:46:35 +03004256 return try_release_extent_state(tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004257}
Chris Masond1310b22008-01-24 16:13:08 -05004258
Chris Masonec29ed52011-02-23 16:23:20 -05004259/*
4260 * helper function for fiemap, which doesn't want to see any holes.
4261 * This maps until we find something past 'last'
4262 */
4263static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004264 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004265{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004266 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004267 struct extent_map *em;
4268 u64 len;
4269
4270 if (offset >= last)
4271 return NULL;
4272
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304273 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004274 len = last - offset;
4275 if (len == 0)
4276 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004277 len = ALIGN(len, sectorsize);
David Sterbae3350e12017-06-23 04:09:57 +02004278 em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, offset,
4279 len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004280 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004281 return em;
4282
4283 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004284 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004285 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004286
4287 /* this is a hole, advance to the next extent */
4288 offset = extent_map_end(em);
4289 free_extent_map(em);
4290 if (offset >= last)
4291 break;
4292 }
4293 return NULL;
4294}
4295
Qu Wenruo47518322017-04-07 10:43:15 +08004296/*
4297 * To cache previous fiemap extent
4298 *
4299 * Will be used for merging fiemap extent
4300 */
4301struct fiemap_cache {
4302 u64 offset;
4303 u64 phys;
4304 u64 len;
4305 u32 flags;
4306 bool cached;
4307};
4308
4309/*
4310 * Helper to submit fiemap extent.
4311 *
4312 * Will try to merge current fiemap extent specified by @offset, @phys,
4313 * @len and @flags with cached one.
4314 * And only when we fails to merge, cached one will be submitted as
4315 * fiemap extent.
4316 *
4317 * Return value is the same as fiemap_fill_next_extent().
4318 */
4319static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4320 struct fiemap_cache *cache,
4321 u64 offset, u64 phys, u64 len, u32 flags)
4322{
4323 int ret = 0;
4324
4325 if (!cache->cached)
4326 goto assign;
4327
4328 /*
4329 * Sanity check, extent_fiemap() should have ensured that new
4330 * fiemap extent won't overlap with cahced one.
4331 * Not recoverable.
4332 *
4333 * NOTE: Physical address can overlap, due to compression
4334 */
4335 if (cache->offset + cache->len > offset) {
4336 WARN_ON(1);
4337 return -EINVAL;
4338 }
4339
4340 /*
4341 * Only merges fiemap extents if
4342 * 1) Their logical addresses are continuous
4343 *
4344 * 2) Their physical addresses are continuous
4345 * So truly compressed (physical size smaller than logical size)
4346 * extents won't get merged with each other
4347 *
4348 * 3) Share same flags except FIEMAP_EXTENT_LAST
4349 * So regular extent won't get merged with prealloc extent
4350 */
4351 if (cache->offset + cache->len == offset &&
4352 cache->phys + cache->len == phys &&
4353 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4354 (flags & ~FIEMAP_EXTENT_LAST)) {
4355 cache->len += len;
4356 cache->flags |= flags;
4357 goto try_submit_last;
4358 }
4359
4360 /* Not mergeable, need to submit cached one */
4361 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4362 cache->len, cache->flags);
4363 cache->cached = false;
4364 if (ret)
4365 return ret;
4366assign:
4367 cache->cached = true;
4368 cache->offset = offset;
4369 cache->phys = phys;
4370 cache->len = len;
4371 cache->flags = flags;
4372try_submit_last:
4373 if (cache->flags & FIEMAP_EXTENT_LAST) {
4374 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4375 cache->phys, cache->len, cache->flags);
4376 cache->cached = false;
4377 }
4378 return ret;
4379}
4380
4381/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004382 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004383 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004384 * The last fiemap cache may still be cached in the following case:
4385 * 0 4k 8k
4386 * |<- Fiemap range ->|
4387 * |<------------ First extent ----------->|
4388 *
4389 * In this case, the first extent range will be cached but not emitted.
4390 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004391 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004392static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4393 struct fiemap_extent_info *fieinfo,
4394 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004395{
4396 int ret;
4397
4398 if (!cache->cached)
4399 return 0;
4400
Qu Wenruo47518322017-04-07 10:43:15 +08004401 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4402 cache->len, cache->flags);
4403 cache->cached = false;
4404 if (ret > 0)
4405 ret = 0;
4406 return ret;
4407}
4408
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004409int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004410 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004411{
Josef Bacik975f84f2010-11-23 19:36:57 +00004412 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004413 u64 off = start;
4414 u64 max = start + len;
4415 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004416 u32 found_type;
4417 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004418 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004419 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004420 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004421 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004422 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004423 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004424 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004425 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004426 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004427 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004428 u64 em_start = 0;
4429 u64 em_len = 0;
4430 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004431
4432 if (len == 0)
4433 return -EINVAL;
4434
Josef Bacik975f84f2010-11-23 19:36:57 +00004435 path = btrfs_alloc_path();
4436 if (!path)
4437 return -ENOMEM;
4438 path->leave_spinning = 1;
4439
Jeff Mahoneyda170662016-06-15 09:22:56 -04004440 start = round_down(start, btrfs_inode_sectorsize(inode));
4441 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004442
Chris Masonec29ed52011-02-23 16:23:20 -05004443 /*
4444 * lookup the last file extent. We're not using i_size here
4445 * because there might be preallocation past i_size
4446 */
David Sterbaf85b7372017-01-20 14:54:07 +01004447 ret = btrfs_lookup_file_extent(NULL, root, path,
4448 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004449 if (ret < 0) {
4450 btrfs_free_path(path);
4451 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004452 } else {
4453 WARN_ON(!ret);
4454 if (ret == 1)
4455 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004456 }
Liu Bo2d324f52016-05-17 17:21:48 -07004457
Josef Bacik975f84f2010-11-23 19:36:57 +00004458 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004459 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004460 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004461
Chris Masonec29ed52011-02-23 16:23:20 -05004462 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004463 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004464 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004465 /* have to trust i_size as the end */
4466 last = (u64)-1;
4467 last_for_get_extent = isize;
4468 } else {
4469 /*
4470 * remember the start of the last extent. There are a
4471 * bunch of different factors that go into the length of the
4472 * extent, so its much less complex to remember where it started
4473 */
4474 last = found_key.offset;
4475 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004476 }
Liu Bofe09e162013-09-22 12:54:23 +08004477 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004478
Chris Masonec29ed52011-02-23 16:23:20 -05004479 /*
4480 * we might have some extents allocated but more delalloc past those
4481 * extents. so, we trust isize unless the start of the last extent is
4482 * beyond isize
4483 */
4484 if (last < isize) {
4485 last = (u64)-1;
4486 last_for_get_extent = isize;
4487 }
4488
David Sterbaff13db42015-12-03 14:30:40 +01004489 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004490 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004491
David Sterbae3350e12017-06-23 04:09:57 +02004492 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004493 if (!em)
4494 goto out;
4495 if (IS_ERR(em)) {
4496 ret = PTR_ERR(em);
4497 goto out;
4498 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004499
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004500 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004501 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004502
Chris Masonea8efc72011-03-08 11:54:40 -05004503 /* break if the extent we found is outside the range */
4504 if (em->start >= max || extent_map_end(em) < off)
4505 break;
4506
4507 /*
4508 * get_extent may return an extent that starts before our
4509 * requested range. We have to make sure the ranges
4510 * we return to fiemap always move forward and don't
4511 * overlap, so adjust the offsets here
4512 */
4513 em_start = max(em->start, off);
4514
4515 /*
4516 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004517 * for adjusting the disk offset below. Only do this if the
4518 * extent isn't compressed since our in ram offset may be past
4519 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004520 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004521 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4522 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004523 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004524 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004525 flags = 0;
Filipe Mananaf0986312018-06-20 10:02:30 +01004526 if (em->block_start < EXTENT_MAP_LAST_BYTE)
4527 disko = em->block_start + offset_in_extent;
4528 else
4529 disko = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004530
Chris Masonea8efc72011-03-08 11:54:40 -05004531 /*
4532 * bump off for our next call to get_extent
4533 */
4534 off = extent_map_end(em);
4535 if (off >= max)
4536 end = 1;
4537
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004538 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004539 end = 1;
4540 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004541 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004542 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4543 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004544 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004545 flags |= (FIEMAP_EXTENT_DELALLOC |
4546 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004547 } else if (fieinfo->fi_extents_max) {
4548 u64 bytenr = em->block_start -
4549 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004550
Liu Bofe09e162013-09-22 12:54:23 +08004551 /*
4552 * As btrfs supports shared space, this information
4553 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004554 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4555 * then we're just getting a count and we can skip the
4556 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004557 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004558 ret = btrfs_check_shared(root,
4559 btrfs_ino(BTRFS_I(inode)),
4560 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004561 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004562 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004563 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004564 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004565 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004566 }
4567 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4568 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004569 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4570 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004571
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004572 free_extent_map(em);
4573 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004574 if ((em_start >= last) || em_len == (u64)-1 ||
4575 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004576 flags |= FIEMAP_EXTENT_LAST;
4577 end = 1;
4578 }
4579
Chris Masonec29ed52011-02-23 16:23:20 -05004580 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004581 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004582 if (IS_ERR(em)) {
4583 ret = PTR_ERR(em);
4584 goto out;
4585 }
4586 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004587 flags |= FIEMAP_EXTENT_LAST;
4588 end = 1;
4589 }
Qu Wenruo47518322017-04-07 10:43:15 +08004590 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4591 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004592 if (ret) {
4593 if (ret == 1)
4594 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004595 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004596 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004597 }
4598out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004599 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004600 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004601 free_extent_map(em);
4602out:
Liu Bofe09e162013-09-22 12:54:23 +08004603 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004604 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004605 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004606 return ret;
4607}
4608
Chris Mason727011e2010-08-06 13:21:20 -04004609static void __free_extent_buffer(struct extent_buffer *eb)
4610{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004611 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004612 kmem_cache_free(extent_buffer_cache, eb);
4613}
4614
Josef Bacika26e8c92014-03-28 17:07:27 -04004615int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004616{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004617 return (atomic_read(&eb->io_pages) ||
4618 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4619 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004620}
4621
Miao Xie897ca6e92010-10-26 20:57:29 -04004622/*
David Sterba55ac0132018-07-19 17:24:32 +02004623 * Release all pages attached to the extent buffer.
Miao Xie897ca6e92010-10-26 20:57:29 -04004624 */
David Sterba55ac0132018-07-19 17:24:32 +02004625static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004626{
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004627 int i;
4628 int num_pages;
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004629 int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004630
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004631 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004632
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004633 num_pages = num_extent_pages(eb);
4634 for (i = 0; i < num_pages; i++) {
4635 struct page *page = eb->pages[i];
Miao Xie897ca6e92010-10-26 20:57:29 -04004636
Forrest Liu5d2361d2015-02-09 17:31:45 +08004637 if (!page)
4638 continue;
4639 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004640 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004641 /*
4642 * We do this since we'll remove the pages after we've
4643 * removed the eb from the radix tree, so we could race
4644 * and have this page now attached to the new eb. So
4645 * only clear page_private if it's still connected to
4646 * this eb.
4647 */
4648 if (PagePrivate(page) &&
4649 page->private == (unsigned long)eb) {
4650 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4651 BUG_ON(PageDirty(page));
4652 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004653 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004654 * We need to make sure we haven't be attached
4655 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004656 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004657 ClearPagePrivate(page);
4658 set_page_private(page, 0);
4659 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004660 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004661 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004662
4663 if (mapped)
4664 spin_unlock(&page->mapping->private_lock);
4665
Nicholas D Steeves01327612016-05-19 21:18:45 -04004666 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004667 put_page(page);
Nikolay Borisovd64766f2018-06-27 16:38:22 +03004668 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004669}
4670
4671/*
4672 * Helper for releasing the extent buffer.
4673 */
4674static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4675{
David Sterba55ac0132018-07-19 17:24:32 +02004676 btrfs_release_extent_buffer_pages(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004677 __free_extent_buffer(eb);
4678}
4679
Josef Bacikf28491e2013-12-16 13:24:27 -05004680static struct extent_buffer *
4681__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004682 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004683{
4684 struct extent_buffer *eb = NULL;
4685
Michal Hockod1b5c562015-08-19 14:17:40 +02004686 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004687 eb->start = start;
4688 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004689 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004690 eb->bflags = 0;
4691 rwlock_init(&eb->lock);
4692 atomic_set(&eb->write_locks, 0);
4693 atomic_set(&eb->read_locks, 0);
4694 atomic_set(&eb->blocking_readers, 0);
4695 atomic_set(&eb->blocking_writers, 0);
4696 atomic_set(&eb->spinning_readers, 0);
4697 atomic_set(&eb->spinning_writers, 0);
4698 eb->lock_nested = 0;
4699 init_waitqueue_head(&eb->write_lock_wq);
4700 init_waitqueue_head(&eb->read_lock_wq);
4701
4702 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4703
4704 spin_lock_init(&eb->refs_lock);
4705 atomic_set(&eb->refs, 1);
4706 atomic_set(&eb->io_pages, 0);
4707
4708 /*
4709 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4710 */
4711 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4712 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4713 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4714
4715 return eb;
4716}
4717
4718struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4719{
David Sterbacc5e31a2018-03-01 18:20:27 +01004720 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004721 struct page *p;
4722 struct extent_buffer *new;
David Sterbacc5e31a2018-03-01 18:20:27 +01004723 int num_pages = num_extent_pages(src);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004724
David Sterba3f556f72014-06-15 03:20:26 +02004725 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004726 if (new == NULL)
4727 return NULL;
4728
4729 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004730 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004731 if (!p) {
4732 btrfs_release_extent_buffer(new);
4733 return NULL;
4734 }
4735 attach_extent_buffer_page(new, p);
4736 WARN_ON(PageDirty(p));
4737 SetPageUptodate(p);
4738 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004739 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004740 }
4741
Josef Bacikdb7f3432013-08-07 14:54:37 -04004742 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004743 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004744
4745 return new;
4746}
4747
Omar Sandoval0f331222015-09-29 20:50:31 -07004748struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4749 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004750{
4751 struct extent_buffer *eb;
David Sterbacc5e31a2018-03-01 18:20:27 +01004752 int num_pages;
4753 int i;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004754
David Sterba3f556f72014-06-15 03:20:26 +02004755 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004756 if (!eb)
4757 return NULL;
4758
David Sterba65ad0102018-06-29 10:56:49 +02004759 num_pages = num_extent_pages(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004760 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004761 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004762 if (!eb->pages[i])
4763 goto err;
4764 }
4765 set_extent_buffer_uptodate(eb);
4766 btrfs_set_header_nritems(eb, 0);
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004767 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004768
4769 return eb;
4770err:
4771 for (; i > 0; i--)
4772 __free_page(eb->pages[i - 1]);
4773 __free_extent_buffer(eb);
4774 return NULL;
4775}
4776
Omar Sandoval0f331222015-09-29 20:50:31 -07004777struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004778 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004779{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004780 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004781}
4782
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004783static void check_buffer_tree_ref(struct extent_buffer *eb)
4784{
Chris Mason242e18c2013-01-29 17:49:37 -05004785 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004786 /* the ref bit is tricky. We have to make sure it is set
4787 * if we have the buffer dirty. Otherwise the
4788 * code to free a buffer can end up dropping a dirty
4789 * page
4790 *
4791 * Once the ref bit is set, it won't go away while the
4792 * buffer is dirty or in writeback, and it also won't
4793 * go away while we have the reference count on the
4794 * eb bumped.
4795 *
4796 * We can't just set the ref bit without bumping the
4797 * ref on the eb because free_extent_buffer might
4798 * see the ref bit and try to clear it. If this happens
4799 * free_extent_buffer might end up dropping our original
4800 * ref by mistake and freeing the page before we are able
4801 * to add one more ref.
4802 *
4803 * So bump the ref count first, then set the bit. If someone
4804 * beat us to it, drop the ref we added.
4805 */
Chris Mason242e18c2013-01-29 17:49:37 -05004806 refs = atomic_read(&eb->refs);
4807 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4808 return;
4809
Josef Bacik594831c2012-07-20 16:11:08 -04004810 spin_lock(&eb->refs_lock);
4811 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004812 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004813 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004814}
4815
Mel Gorman2457aec2014-06-04 16:10:31 -07004816static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4817 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004818{
David Sterbacc5e31a2018-03-01 18:20:27 +01004819 int num_pages, i;
Josef Bacik5df42352012-03-15 18:24:42 -04004820
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004821 check_buffer_tree_ref(eb);
4822
David Sterba65ad0102018-06-29 10:56:49 +02004823 num_pages = num_extent_pages(eb);
Josef Bacik5df42352012-03-15 18:24:42 -04004824 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004825 struct page *p = eb->pages[i];
4826
Mel Gorman2457aec2014-06-04 16:10:31 -07004827 if (p != accessed)
4828 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004829 }
4830}
4831
Josef Bacikf28491e2013-12-16 13:24:27 -05004832struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4833 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004834{
4835 struct extent_buffer *eb;
4836
4837 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004838 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004839 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004840 if (eb && atomic_inc_not_zero(&eb->refs)) {
4841 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004842 /*
4843 * Lock our eb's refs_lock to avoid races with
4844 * free_extent_buffer. When we get our eb it might be flagged
4845 * with EXTENT_BUFFER_STALE and another task running
4846 * free_extent_buffer might have seen that flag set,
4847 * eb->refs == 2, that the buffer isn't under IO (dirty and
4848 * writeback flags not set) and it's still in the tree (flag
4849 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4850 * of decrementing the extent buffer's reference count twice.
4851 * So here we could race and increment the eb's reference count,
4852 * clear its stale flag, mark it as dirty and drop our reference
4853 * before the other task finishes executing free_extent_buffer,
4854 * which would later result in an attempt to free an extent
4855 * buffer that is dirty.
4856 */
4857 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4858 spin_lock(&eb->refs_lock);
4859 spin_unlock(&eb->refs_lock);
4860 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004861 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004862 return eb;
4863 }
4864 rcu_read_unlock();
4865
4866 return NULL;
4867}
4868
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004869#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4870struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004871 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004872{
4873 struct extent_buffer *eb, *exists = NULL;
4874 int ret;
4875
4876 eb = find_extent_buffer(fs_info, start);
4877 if (eb)
4878 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004879 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004880 if (!eb)
4881 return NULL;
4882 eb->fs_info = fs_info;
4883again:
David Sterbae1860a72016-05-09 14:11:38 +02004884 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004885 if (ret)
4886 goto free_eb;
4887 spin_lock(&fs_info->buffer_lock);
4888 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004889 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004890 spin_unlock(&fs_info->buffer_lock);
4891 radix_tree_preload_end();
4892 if (ret == -EEXIST) {
4893 exists = find_extent_buffer(fs_info, start);
4894 if (exists)
4895 goto free_eb;
4896 else
4897 goto again;
4898 }
4899 check_buffer_tree_ref(eb);
4900 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4901
4902 /*
4903 * We will free dummy extent buffer's if they come into
4904 * free_extent_buffer with a ref count of 2, but if we are using this we
4905 * want the buffers to stay in memory until we're done with them, so
4906 * bump the ref count again.
4907 */
4908 atomic_inc(&eb->refs);
4909 return eb;
4910free_eb:
4911 btrfs_release_extent_buffer(eb);
4912 return exists;
4913}
4914#endif
4915
Josef Bacikf28491e2013-12-16 13:24:27 -05004916struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004917 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004918{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004919 unsigned long len = fs_info->nodesize;
David Sterbacc5e31a2018-03-01 18:20:27 +01004920 int num_pages;
4921 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004922 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004923 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004924 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004925 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004926 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004927 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004928 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004929
Jeff Mahoneyda170662016-06-15 09:22:56 -04004930 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004931 btrfs_err(fs_info, "bad tree block start %llu", start);
4932 return ERR_PTR(-EINVAL);
4933 }
4934
Josef Bacikf28491e2013-12-16 13:24:27 -05004935 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004936 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004937 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004938
David Sterba23d79d82014-06-15 02:55:29 +02004939 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004940 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004941 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004942
David Sterba65ad0102018-06-29 10:56:49 +02004943 num_pages = num_extent_pages(eb);
Chris Mason727011e2010-08-06 13:21:20 -04004944 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004945 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004946 if (!p) {
4947 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004948 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004949 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004950
4951 spin_lock(&mapping->private_lock);
4952 if (PagePrivate(p)) {
4953 /*
4954 * We could have already allocated an eb for this page
4955 * and attached one so lets see if we can get a ref on
4956 * the existing eb, and if we can we know it's good and
4957 * we can just return that one, else we know we can just
4958 * overwrite page->private.
4959 */
4960 exists = (struct extent_buffer *)p->private;
4961 if (atomic_inc_not_zero(&exists->refs)) {
4962 spin_unlock(&mapping->private_lock);
4963 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004964 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004965 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004966 goto free_eb;
4967 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004968 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004969
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004970 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004971 * Do this so attach doesn't complain and we need to
4972 * drop the ref the old guy had.
4973 */
4974 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004975 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004976 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004977 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004978 attach_extent_buffer_page(eb, p);
4979 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004980 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004981 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004982 if (!PageUptodate(p))
4983 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004984
4985 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03004986 * We can't unlock the pages just yet since the extent buffer
4987 * hasn't been properly inserted in the radix tree, this
4988 * opens a race with btree_releasepage which can free a page
4989 * while we are still filling in all pages for the buffer and
4990 * we could crash.
Chris Masoneb14ab82011-02-10 12:35:00 -05004991 */
Chris Masond1310b22008-01-24 16:13:08 -05004992 }
4993 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004994 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004995again:
David Sterbae1860a72016-05-09 14:11:38 +02004996 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004997 if (ret) {
4998 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004999 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005000 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005001
Josef Bacikf28491e2013-12-16 13:24:27 -05005002 spin_lock(&fs_info->buffer_lock);
5003 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005004 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005005 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005006 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005007 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005008 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005009 if (exists)
5010 goto free_eb;
5011 else
Josef Bacik115391d2012-03-09 09:51:43 -05005012 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005013 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005014 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005015 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005016 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005017
5018 /*
Nikolay Borisovb16d0112018-07-04 10:24:52 +03005019 * Now it's safe to unlock the pages because any calls to
5020 * btree_releasepage will correctly detect that a page belongs to a
5021 * live buffer and won't free them prematurely.
Chris Masoneb14ab82011-02-10 12:35:00 -05005022 */
Nikolay Borisov28187ae2018-07-04 10:24:51 +03005023 for (i = 0; i < num_pages; i++)
5024 unlock_page(eb->pages[i]);
Chris Masond1310b22008-01-24 16:13:08 -05005025 return eb;
5026
Chris Mason6af118ce2008-07-22 11:18:07 -04005027free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005028 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005029 for (i = 0; i < num_pages; i++) {
5030 if (eb->pages[i])
5031 unlock_page(eb->pages[i]);
5032 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005033
Miao Xie897ca6e92010-10-26 20:57:29 -04005034 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005035 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005036}
Chris Masond1310b22008-01-24 16:13:08 -05005037
Josef Bacik3083ee22012-03-09 16:01:49 -05005038static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5039{
5040 struct extent_buffer *eb =
5041 container_of(head, struct extent_buffer, rcu_head);
5042
5043 __free_extent_buffer(eb);
5044}
5045
David Sterbaf7a52a42013-04-26 14:56:29 +00005046static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005047{
Nikolay Borisov07e21c42018-06-27 16:38:23 +03005048 lockdep_assert_held(&eb->refs_lock);
5049
Josef Bacik3083ee22012-03-09 16:01:49 -05005050 WARN_ON(atomic_read(&eb->refs) == 0);
5051 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005052 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005053 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005054
Jan Schmidt815a51c2012-05-16 17:00:02 +02005055 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005056
Josef Bacikf28491e2013-12-16 13:24:27 -05005057 spin_lock(&fs_info->buffer_lock);
5058 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005059 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005060 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005061 } else {
5062 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005063 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005064
5065 /* Should be safe to release our pages at this point */
David Sterba55ac0132018-07-19 17:24:32 +02005066 btrfs_release_extent_buffer_pages(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005067#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005068 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
Josef Bacikbcb7e442015-03-16 17:38:02 -04005069 __free_extent_buffer(eb);
5070 return 1;
5071 }
5072#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005073 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005074 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005075 }
5076 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005077
5078 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005079}
5080
Chris Masond1310b22008-01-24 16:13:08 -05005081void free_extent_buffer(struct extent_buffer *eb)
5082{
Chris Mason242e18c2013-01-29 17:49:37 -05005083 int refs;
5084 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005085 if (!eb)
5086 return;
5087
Chris Mason242e18c2013-01-29 17:49:37 -05005088 while (1) {
5089 refs = atomic_read(&eb->refs);
5090 if (refs <= 3)
5091 break;
5092 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5093 if (old == refs)
5094 return;
5095 }
5096
Josef Bacik3083ee22012-03-09 16:01:49 -05005097 spin_lock(&eb->refs_lock);
5098 if (atomic_read(&eb->refs) == 2 &&
Nikolay Borisovb0132a32018-06-27 16:38:24 +03005099 test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))
Jan Schmidt815a51c2012-05-16 17:00:02 +02005100 atomic_dec(&eb->refs);
5101
5102 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005103 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005104 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005105 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5106 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005107
Josef Bacik3083ee22012-03-09 16:01:49 -05005108 /*
5109 * I know this is terrible, but it's temporary until we stop tracking
5110 * the uptodate bits and such for the extent buffers.
5111 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005112 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005113}
Chris Masond1310b22008-01-24 16:13:08 -05005114
Josef Bacik3083ee22012-03-09 16:01:49 -05005115void free_extent_buffer_stale(struct extent_buffer *eb)
5116{
5117 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005118 return;
5119
Josef Bacik3083ee22012-03-09 16:01:49 -05005120 spin_lock(&eb->refs_lock);
5121 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5122
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005123 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005124 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5125 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005126 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005127}
5128
Chris Mason1d4284b2012-03-28 20:31:37 -04005129void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005130{
David Sterbacc5e31a2018-03-01 18:20:27 +01005131 int i;
5132 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005133 struct page *page;
5134
David Sterba65ad0102018-06-29 10:56:49 +02005135 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005136
5137 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005138 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005139 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005140 continue;
5141
Chris Masona61e6f22008-07-22 11:18:08 -04005142 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005143 WARN_ON(!PagePrivate(page));
5144
Chris Masond1310b22008-01-24 16:13:08 -05005145 clear_page_dirty_for_io(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005146 xa_lock_irq(&page->mapping->i_pages);
Matthew Wilcox0a943c62017-12-04 10:37:22 -05005147 if (!PageDirty(page))
5148 __xa_clear_mark(&page->mapping->i_pages,
5149 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07005150 xa_unlock_irq(&page->mapping->i_pages);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005151 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005152 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005153 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005154 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005155}
Chris Masond1310b22008-01-24 16:13:08 -05005156
Liu Boabb57ef2018-09-14 01:44:42 +08005157bool set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005158{
David Sterbacc5e31a2018-03-01 18:20:27 +01005159 int i;
5160 int num_pages;
Liu Boabb57ef2018-09-14 01:44:42 +08005161 bool was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005162
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005163 check_buffer_tree_ref(eb);
5164
Chris Masonb9473432009-03-13 11:00:37 -04005165 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005166
David Sterba65ad0102018-06-29 10:56:49 +02005167 num_pages = num_extent_pages(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05005168 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005169 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5170
Liu Boabb57ef2018-09-14 01:44:42 +08005171 if (!was_dirty)
5172 for (i = 0; i < num_pages; i++)
5173 set_page_dirty(eb->pages[i]);
Liu Bo51995c32018-09-14 01:46:08 +08005174
5175#ifdef CONFIG_BTRFS_DEBUG
5176 for (i = 0; i < num_pages; i++)
5177 ASSERT(PageDirty(eb->pages[i]));
5178#endif
5179
Chris Masonb9473432009-03-13 11:00:37 -04005180 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005181}
Chris Masond1310b22008-01-24 16:13:08 -05005182
David Sterba69ba3922015-12-03 13:08:59 +01005183void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005184{
David Sterbacc5e31a2018-03-01 18:20:27 +01005185 int i;
Chris Mason1259ab72008-05-12 13:39:03 -04005186 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005187 int num_pages;
Chris Mason1259ab72008-05-12 13:39:03 -04005188
Chris Masonb4ce94d2009-02-04 09:25:08 -05005189 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005190 num_pages = num_extent_pages(eb);
Chris Mason1259ab72008-05-12 13:39:03 -04005191 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005192 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005193 if (page)
5194 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005195 }
Chris Mason1259ab72008-05-12 13:39:03 -04005196}
5197
David Sterba09c25a82015-12-03 13:08:59 +01005198void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005199{
David Sterbacc5e31a2018-03-01 18:20:27 +01005200 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005201 struct page *page;
David Sterbacc5e31a2018-03-01 18:20:27 +01005202 int num_pages;
Chris Masond1310b22008-01-24 16:13:08 -05005203
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005204 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
David Sterba65ad0102018-06-29 10:56:49 +02005205 num_pages = num_extent_pages(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005206 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005207 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005208 SetPageUptodate(page);
5209 }
Chris Masond1310b22008-01-24 16:13:08 -05005210}
Chris Masond1310b22008-01-24 16:13:08 -05005211
Chris Masond1310b22008-01-24 16:13:08 -05005212int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005213 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005214{
David Sterbacc5e31a2018-03-01 18:20:27 +01005215 int i;
Chris Masond1310b22008-01-24 16:13:08 -05005216 struct page *page;
5217 int err;
5218 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005219 int locked_pages = 0;
5220 int all_uptodate = 1;
David Sterbacc5e31a2018-03-01 18:20:27 +01005221 int num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005222 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005223 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005224 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005225
Chris Masonb4ce94d2009-02-04 09:25:08 -05005226 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005227 return 0;
5228
David Sterba65ad0102018-06-29 10:56:49 +02005229 num_pages = num_extent_pages(eb);
Josef Bacik8436ea912016-09-02 15:40:03 -04005230 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005231 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005232 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005233 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005234 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005235 } else {
5236 lock_page(page);
5237 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005238 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005239 }
5240 /*
5241 * We need to firstly lock all pages to make sure that
5242 * the uptodate bit of our pages won't be affected by
5243 * clear_extent_buffer_uptodate().
5244 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005245 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005246 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005247 if (!PageUptodate(page)) {
5248 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005249 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005250 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005251 }
Liu Bo2571e732016-08-03 12:33:01 -07005252
Chris Masonce9adaa2008-04-09 16:28:12 -04005253 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005254 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005255 goto unlock_exit;
5256 }
5257
Filipe Manana656f30d2014-09-26 12:25:56 +01005258 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005259 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005260 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005261 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005262 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005263
Chris Masonce9adaa2008-04-09 16:28:12 -04005264 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005265 if (ret) {
5266 atomic_dec(&eb->io_pages);
5267 unlock_page(page);
5268 continue;
5269 }
5270
Chris Masonf1885912008-04-09 16:28:12 -04005271 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005272 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005273 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005274 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005275 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005276 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005277 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005278 /*
5279 * We use &bio in above __extent_read_full_page,
5280 * so we ensure that if it returns error, the
5281 * current page fails to add itself to bio and
5282 * it's been unlocked.
5283 *
5284 * We must dec io_pages by ourselves.
5285 */
5286 atomic_dec(&eb->io_pages);
5287 }
Chris Masond1310b22008-01-24 16:13:08 -05005288 } else {
5289 unlock_page(page);
5290 }
5291 }
5292
Jeff Mahoney355808c2011-10-03 23:23:14 -04005293 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005294 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005295 if (err)
5296 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005297 }
Chris Masona86c12c2008-02-07 10:50:54 -05005298
Arne Jansenbb82ab82011-06-10 14:06:53 +02005299 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005300 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005301
Josef Bacik8436ea912016-09-02 15:40:03 -04005302 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005303 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005304 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005305 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005306 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005307 }
Chris Masond3977122009-01-05 21:25:51 -05005308
Chris Masond1310b22008-01-24 16:13:08 -05005309 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005310
5311unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005312 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005313 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005314 page = eb->pages[locked_pages];
5315 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005316 }
5317 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005318}
Chris Masond1310b22008-01-24 16:13:08 -05005319
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005320void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5321 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005322{
5323 size_t cur;
5324 size_t offset;
5325 struct page *page;
5326 char *kaddr;
5327 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005328 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5329 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005330
Liu Bof716abd2017-08-09 11:10:16 -06005331 if (start + len > eb->len) {
5332 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5333 eb->start, eb->len, start, len);
5334 memset(dst, 0, len);
5335 return;
5336 }
Chris Masond1310b22008-01-24 16:13:08 -05005337
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005338 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005339
Chris Masond3977122009-01-05 21:25:51 -05005340 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005341 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005342
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005343 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005344 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005345 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005346
5347 dst += cur;
5348 len -= cur;
5349 offset = 0;
5350 i++;
5351 }
5352}
Chris Masond1310b22008-01-24 16:13:08 -05005353
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005354int read_extent_buffer_to_user(const struct extent_buffer *eb,
5355 void __user *dstv,
5356 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005357{
5358 size_t cur;
5359 size_t offset;
5360 struct page *page;
5361 char *kaddr;
5362 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005363 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5364 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005365 int ret = 0;
5366
5367 WARN_ON(start > eb->len);
5368 WARN_ON(start + len > eb->start + eb->len);
5369
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005370 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005371
5372 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005373 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005374
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005375 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005376 kaddr = page_address(page);
5377 if (copy_to_user(dst, kaddr + offset, cur)) {
5378 ret = -EFAULT;
5379 break;
5380 }
5381
5382 dst += cur;
5383 len -= cur;
5384 offset = 0;
5385 i++;
5386 }
5387
5388 return ret;
5389}
5390
Liu Bo415b35a2016-06-17 19:16:21 -07005391/*
5392 * return 0 if the item is found within a page.
5393 * return 1 if the item spans two pages.
5394 * return -EINVAL otherwise.
5395 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005396int map_private_extent_buffer(const struct extent_buffer *eb,
5397 unsigned long start, unsigned long min_len,
5398 char **map, unsigned long *map_start,
5399 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005400{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005401 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005402 char *kaddr;
5403 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005404 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5405 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005406 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005407 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005408
Liu Bof716abd2017-08-09 11:10:16 -06005409 if (start + min_len > eb->len) {
5410 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5411 eb->start, eb->len, start, min_len);
5412 return -EINVAL;
5413 }
5414
Chris Masond1310b22008-01-24 16:13:08 -05005415 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005416 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005417
5418 if (i == 0) {
5419 offset = start_offset;
5420 *map_start = 0;
5421 } else {
5422 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005423 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005424 }
Chris Masond3977122009-01-05 21:25:51 -05005425
David Sterbafb85fc92014-07-31 01:03:53 +02005426 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005427 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005428 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005429 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005430 return 0;
5431}
Chris Masond1310b22008-01-24 16:13:08 -05005432
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005433int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5434 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005435{
5436 size_t cur;
5437 size_t offset;
5438 struct page *page;
5439 char *kaddr;
5440 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005441 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5442 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005443 int ret = 0;
5444
5445 WARN_ON(start > eb->len);
5446 WARN_ON(start + len > eb->start + eb->len);
5447
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005448 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005449
Chris Masond3977122009-01-05 21:25:51 -05005450 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005451 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005452
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005453 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005454
Chris Masona6591712011-07-19 12:04:14 -04005455 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005456 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005457 if (ret)
5458 break;
5459
5460 ptr += cur;
5461 len -= cur;
5462 offset = 0;
5463 i++;
5464 }
5465 return ret;
5466}
Chris Masond1310b22008-01-24 16:13:08 -05005467
David Sterbaf157bf72016-11-09 17:43:38 +01005468void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5469 const void *srcv)
5470{
5471 char *kaddr;
5472
5473 WARN_ON(!PageUptodate(eb->pages[0]));
5474 kaddr = page_address(eb->pages[0]);
5475 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5476 BTRFS_FSID_SIZE);
5477}
5478
5479void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5480{
5481 char *kaddr;
5482
5483 WARN_ON(!PageUptodate(eb->pages[0]));
5484 kaddr = page_address(eb->pages[0]);
5485 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5486 BTRFS_FSID_SIZE);
5487}
5488
Chris Masond1310b22008-01-24 16:13:08 -05005489void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5490 unsigned long start, unsigned long len)
5491{
5492 size_t cur;
5493 size_t offset;
5494 struct page *page;
5495 char *kaddr;
5496 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005497 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5498 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005499
5500 WARN_ON(start > eb->len);
5501 WARN_ON(start + len > eb->start + eb->len);
5502
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005503 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005504
Chris Masond3977122009-01-05 21:25:51 -05005505 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005506 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005507 WARN_ON(!PageUptodate(page));
5508
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005509 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005510 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005511 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005512
5513 src += cur;
5514 len -= cur;
5515 offset = 0;
5516 i++;
5517 }
5518}
Chris Masond1310b22008-01-24 16:13:08 -05005519
David Sterbab159fa22016-11-08 18:09:03 +01005520void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5521 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005522{
5523 size_t cur;
5524 size_t offset;
5525 struct page *page;
5526 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005527 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5528 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005529
5530 WARN_ON(start > eb->len);
5531 WARN_ON(start + len > eb->start + eb->len);
5532
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005533 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005534
Chris Masond3977122009-01-05 21:25:51 -05005535 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005536 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005537 WARN_ON(!PageUptodate(page));
5538
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005539 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005540 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005541 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005542
5543 len -= cur;
5544 offset = 0;
5545 i++;
5546 }
5547}
Chris Masond1310b22008-01-24 16:13:08 -05005548
David Sterba58e80122016-11-08 18:30:31 +01005549void copy_extent_buffer_full(struct extent_buffer *dst,
5550 struct extent_buffer *src)
5551{
5552 int i;
David Sterbacc5e31a2018-03-01 18:20:27 +01005553 int num_pages;
David Sterba58e80122016-11-08 18:30:31 +01005554
5555 ASSERT(dst->len == src->len);
5556
David Sterba65ad0102018-06-29 10:56:49 +02005557 num_pages = num_extent_pages(dst);
David Sterba58e80122016-11-08 18:30:31 +01005558 for (i = 0; i < num_pages; i++)
5559 copy_page(page_address(dst->pages[i]),
5560 page_address(src->pages[i]));
5561}
5562
Chris Masond1310b22008-01-24 16:13:08 -05005563void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5564 unsigned long dst_offset, unsigned long src_offset,
5565 unsigned long len)
5566{
5567 u64 dst_len = dst->len;
5568 size_t cur;
5569 size_t offset;
5570 struct page *page;
5571 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005572 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5573 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005574
5575 WARN_ON(src->len != dst_len);
5576
5577 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005578 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005579
Chris Masond3977122009-01-05 21:25:51 -05005580 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005581 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005582 WARN_ON(!PageUptodate(page));
5583
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005584 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005585
Chris Masona6591712011-07-19 12:04:14 -04005586 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005587 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005588
5589 src_offset += cur;
5590 len -= cur;
5591 offset = 0;
5592 i++;
5593 }
5594}
Chris Masond1310b22008-01-24 16:13:08 -05005595
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005596/*
5597 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5598 * given bit number
5599 * @eb: the extent buffer
5600 * @start: offset of the bitmap item in the extent buffer
5601 * @nr: bit number
5602 * @page_index: return index of the page in the extent buffer that contains the
5603 * given bit number
5604 * @page_offset: return offset into the page given by page_index
5605 *
5606 * This helper hides the ugliness of finding the byte in an extent buffer which
5607 * contains a given bit.
5608 */
5609static inline void eb_bitmap_offset(struct extent_buffer *eb,
5610 unsigned long start, unsigned long nr,
5611 unsigned long *page_index,
5612 size_t *page_offset)
5613{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005614 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005615 size_t byte_offset = BIT_BYTE(nr);
5616 size_t offset;
5617
5618 /*
5619 * The byte we want is the offset of the extent buffer + the offset of
5620 * the bitmap item in the extent buffer + the offset of the byte in the
5621 * bitmap item.
5622 */
5623 offset = start_offset + start + byte_offset;
5624
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005625 *page_index = offset >> PAGE_SHIFT;
5626 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005627}
5628
5629/**
5630 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5631 * @eb: the extent buffer
5632 * @start: offset of the bitmap item in the extent buffer
5633 * @nr: bit number to test
5634 */
5635int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5636 unsigned long nr)
5637{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005638 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005639 struct page *page;
5640 unsigned long i;
5641 size_t offset;
5642
5643 eb_bitmap_offset(eb, start, nr, &i, &offset);
5644 page = eb->pages[i];
5645 WARN_ON(!PageUptodate(page));
5646 kaddr = page_address(page);
5647 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5648}
5649
5650/**
5651 * extent_buffer_bitmap_set - set an area of a bitmap
5652 * @eb: the extent buffer
5653 * @start: offset of the bitmap item in the extent buffer
5654 * @pos: bit number of the first bit
5655 * @len: number of bits to set
5656 */
5657void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5658 unsigned long pos, unsigned long len)
5659{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005660 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005661 struct page *page;
5662 unsigned long i;
5663 size_t offset;
5664 const unsigned int size = pos + len;
5665 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005666 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005667
5668 eb_bitmap_offset(eb, start, pos, &i, &offset);
5669 page = eb->pages[i];
5670 WARN_ON(!PageUptodate(page));
5671 kaddr = page_address(page);
5672
5673 while (len >= bits_to_set) {
5674 kaddr[offset] |= mask_to_set;
5675 len -= bits_to_set;
5676 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005677 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005678 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005679 offset = 0;
5680 page = eb->pages[++i];
5681 WARN_ON(!PageUptodate(page));
5682 kaddr = page_address(page);
5683 }
5684 }
5685 if (len) {
5686 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5687 kaddr[offset] |= mask_to_set;
5688 }
5689}
5690
5691
5692/**
5693 * extent_buffer_bitmap_clear - clear an area of a bitmap
5694 * @eb: the extent buffer
5695 * @start: offset of the bitmap item in the extent buffer
5696 * @pos: bit number of the first bit
5697 * @len: number of bits to clear
5698 */
5699void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5700 unsigned long pos, unsigned long len)
5701{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005702 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005703 struct page *page;
5704 unsigned long i;
5705 size_t offset;
5706 const unsigned int size = pos + len;
5707 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005708 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005709
5710 eb_bitmap_offset(eb, start, pos, &i, &offset);
5711 page = eb->pages[i];
5712 WARN_ON(!PageUptodate(page));
5713 kaddr = page_address(page);
5714
5715 while (len >= bits_to_clear) {
5716 kaddr[offset] &= ~mask_to_clear;
5717 len -= bits_to_clear;
5718 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005719 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005720 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005721 offset = 0;
5722 page = eb->pages[++i];
5723 WARN_ON(!PageUptodate(page));
5724 kaddr = page_address(page);
5725 }
5726 }
5727 if (len) {
5728 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5729 kaddr[offset] &= ~mask_to_clear;
5730 }
5731}
5732
Sergei Trofimovich33872062011-04-11 21:52:52 +00005733static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5734{
5735 unsigned long distance = (src > dst) ? src - dst : dst - src;
5736 return distance < len;
5737}
5738
Chris Masond1310b22008-01-24 16:13:08 -05005739static void copy_pages(struct page *dst_page, struct page *src_page,
5740 unsigned long dst_off, unsigned long src_off,
5741 unsigned long len)
5742{
Chris Masona6591712011-07-19 12:04:14 -04005743 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005744 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005745 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005746
Sergei Trofimovich33872062011-04-11 21:52:52 +00005747 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005748 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005749 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005750 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005751 if (areas_overlap(src_off, dst_off, len))
5752 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005753 }
Chris Masond1310b22008-01-24 16:13:08 -05005754
Chris Mason727011e2010-08-06 13:21:20 -04005755 if (must_memmove)
5756 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5757 else
5758 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005759}
5760
5761void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5762 unsigned long src_offset, unsigned long len)
5763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005764 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005765 size_t cur;
5766 size_t dst_off_in_page;
5767 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005768 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005769 unsigned long dst_i;
5770 unsigned long src_i;
5771
5772 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005773 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005774 "memmove bogus src_offset %lu move len %lu dst len %lu",
5775 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005776 BUG_ON(1);
5777 }
5778 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005779 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005780 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5781 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005782 BUG_ON(1);
5783 }
5784
Chris Masond3977122009-01-05 21:25:51 -05005785 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005786 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005787 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005788 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005789 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005790
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005791 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5792 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005793
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005794 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005795 src_off_in_page));
5796 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005797 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005798
David Sterbafb85fc92014-07-31 01:03:53 +02005799 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005800 dst_off_in_page, src_off_in_page, cur);
5801
5802 src_offset += cur;
5803 dst_offset += cur;
5804 len -= cur;
5805 }
5806}
Chris Masond1310b22008-01-24 16:13:08 -05005807
5808void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5809 unsigned long src_offset, unsigned long len)
5810{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005811 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005812 size_t cur;
5813 size_t dst_off_in_page;
5814 size_t src_off_in_page;
5815 unsigned long dst_end = dst_offset + len - 1;
5816 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005817 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005818 unsigned long dst_i;
5819 unsigned long src_i;
5820
5821 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005822 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005823 "memmove bogus src_offset %lu move len %lu len %lu",
5824 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005825 BUG_ON(1);
5826 }
5827 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005828 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005829 "memmove bogus dst_offset %lu move len %lu len %lu",
5830 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005831 BUG_ON(1);
5832 }
Chris Mason727011e2010-08-06 13:21:20 -04005833 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005834 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5835 return;
5836 }
Chris Masond3977122009-01-05 21:25:51 -05005837 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005838 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5839 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005840
5841 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005842 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005843 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005844 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005845
5846 cur = min_t(unsigned long, len, src_off_in_page + 1);
5847 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005848 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005849 dst_off_in_page - cur + 1,
5850 src_off_in_page - cur + 1, cur);
5851
5852 dst_end -= cur;
5853 src_end -= cur;
5854 len -= cur;
5855 }
5856}
Chris Mason6af118ce2008-07-22 11:18:07 -04005857
David Sterbaf7a52a42013-04-26 14:56:29 +00005858int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005859{
Chris Mason6af118ce2008-07-22 11:18:07 -04005860 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005861
Miao Xie19fe0a82010-10-26 20:57:29 -04005862 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005863 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005864 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005865 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005866 spin_lock(&page->mapping->private_lock);
5867 if (!PagePrivate(page)) {
5868 spin_unlock(&page->mapping->private_lock);
5869 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005870 }
5871
Josef Bacik3083ee22012-03-09 16:01:49 -05005872 eb = (struct extent_buffer *)page->private;
5873 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005874
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005875 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005876 * This is a little awful but should be ok, we need to make sure that
5877 * the eb doesn't disappear out from under us while we're looking at
5878 * this page.
5879 */
5880 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005881 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005882 spin_unlock(&eb->refs_lock);
5883 spin_unlock(&page->mapping->private_lock);
5884 return 0;
5885 }
5886 spin_unlock(&page->mapping->private_lock);
5887
Josef Bacik3083ee22012-03-09 16:01:49 -05005888 /*
5889 * If tree ref isn't set then we know the ref on this eb is a real ref,
5890 * so just return, this page will likely be freed soon anyway.
5891 */
5892 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5893 spin_unlock(&eb->refs_lock);
5894 return 0;
5895 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005896
David Sterbaf7a52a42013-04-26 14:56:29 +00005897 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005898}