blob: 47a8fe9d22e890bddd2d3df7bf9abb027db4647a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Chris Masond1310b22008-01-24 16:13:08 -05002#include <linux/bitops.h>
3#include <linux/slab.h>
4#include <linux/bio.h>
5#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05006#include <linux/pagemap.h>
7#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05008#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020019#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010020#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040021#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040022#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080023#include "backref.h"
David Sterba6af49db2017-06-23 04:09:57 +020024#include "disk-io.h"
Chris Masond1310b22008-01-24 16:13:08 -050025
Chris Masond1310b22008-01-24 16:13:08 -050026static struct kmem_cache *extent_state_cache;
27static struct kmem_cache *extent_buffer_cache;
Chris Mason9be33952013-05-17 18:30:14 -040028static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050029
Filipe Manana27a35072014-07-06 20:09:59 +010030static inline bool extent_state_in_tree(const struct extent_state *state)
31{
32 return !RB_EMPTY_NODE(&state->rb_node);
33}
34
Eric Sandeen6d49ba12013-04-22 16:12:31 +000035#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050036static LIST_HEAD(buffers);
37static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040038
Chris Masond3977122009-01-05 21:25:51 -050039static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000040
41static inline
42void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
43{
44 unsigned long flags;
45
46 spin_lock_irqsave(&leak_lock, flags);
47 list_add(new, head);
48 spin_unlock_irqrestore(&leak_lock, flags);
49}
50
51static inline
52void btrfs_leak_debug_del(struct list_head *entry)
53{
54 unsigned long flags;
55
56 spin_lock_irqsave(&leak_lock, flags);
57 list_del(entry);
58 spin_unlock_irqrestore(&leak_lock, flags);
59}
60
61static inline
62void btrfs_leak_debug_check(void)
63{
64 struct extent_state *state;
65 struct extent_buffer *eb;
66
67 while (!list_empty(&states)) {
68 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010069 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010070 state->start, state->end, state->state,
71 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020072 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000073 list_del(&state->leak_list);
74 kmem_cache_free(extent_state_cache, state);
75 }
76
77 while (!list_empty(&buffers)) {
78 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Liu Boaf2679e2018-01-25 11:02:48 -070079 pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
80 eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000081 list_del(&eb->leak_list);
82 kmem_cache_free(extent_buffer_cache, eb);
83 }
84}
David Sterba8d599ae2013-04-30 15:22:23 +000085
Josef Bacika5dee372013-12-13 10:02:44 -050086#define btrfs_debug_check_extent_io_range(tree, start, end) \
87 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000088static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050089 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000090{
Josef Bacikc6100a42017-05-05 11:57:13 -040091 if (tree->ops && tree->ops->check_extent_io_range)
92 tree->ops->check_extent_io_range(tree->private_data, caller,
93 start, end);
David Sterba8d599ae2013-04-30 15:22:23 +000094}
Eric Sandeen6d49ba12013-04-22 16:12:31 +000095#else
96#define btrfs_leak_debug_add(new, head) do {} while (0)
97#define btrfs_leak_debug_del(entry) do {} while (0)
98#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +000099#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400100#endif
Chris Masond1310b22008-01-24 16:13:08 -0500101
Chris Masond1310b22008-01-24 16:13:08 -0500102#define BUFFER_LRU_MAX 64
103
104struct tree_entry {
105 u64 start;
106 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500107 struct rb_node rb_node;
108};
109
110struct extent_page_data {
111 struct bio *bio;
112 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -0500113 /* tells writepage not to lock the state bits for this range
114 * it still does the unlocking
115 */
Chris Masonffbd5172009-04-20 15:50:09 -0400116 unsigned int extent_locked:1;
117
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600118 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400119 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500120};
121
David Sterba57599c72018-03-01 17:56:34 +0100122static int add_extent_changeset(struct extent_state *state, unsigned bits,
Qu Wenruod38ed272015-10-12 14:53:37 +0800123 struct extent_changeset *changeset,
124 int set)
125{
126 int ret;
127
128 if (!changeset)
David Sterba57599c72018-03-01 17:56:34 +0100129 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800130 if (set && (state->state & bits) == bits)
David Sterba57599c72018-03-01 17:56:34 +0100131 return 0;
Qu Wenruofefdc552015-10-12 15:35:38 +0800132 if (!set && (state->state & bits) == 0)
David Sterba57599c72018-03-01 17:56:34 +0100133 return 0;
Qu Wenruod38ed272015-10-12 14:53:37 +0800134 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100135 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800136 GFP_ATOMIC);
David Sterba57599c72018-03-01 17:56:34 +0100137 return ret;
Qu Wenruod38ed272015-10-12 14:53:37 +0800138}
139
David Sterbaaab6e9e2017-11-30 18:00:02 +0100140static void flush_write_bio(struct extent_page_data *epd);
David Sterbae2932ee2017-06-23 04:16:17 +0200141
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400142static inline struct btrfs_fs_info *
143tree_fs_info(struct extent_io_tree *tree)
144{
Josef Bacikc6100a42017-05-05 11:57:13 -0400145 if (tree->ops)
146 return tree->ops->tree_fs_info(tree->private_data);
147 return NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400148}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400149
Chris Masond1310b22008-01-24 16:13:08 -0500150int __init extent_io_init(void)
151{
David Sterba837e1972012-09-07 03:00:48 -0600152 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200153 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300154 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500155 if (!extent_state_cache)
156 return -ENOMEM;
157
David Sterba837e1972012-09-07 03:00:48 -0600158 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200159 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300160 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500161 if (!extent_buffer_cache)
162 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400163
164 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
NeilBrown011067b2017-06-18 14:38:57 +1000165 offsetof(struct btrfs_io_bio, bio),
166 BIOSET_NEED_BVECS);
Chris Mason9be33952013-05-17 18:30:14 -0400167 if (!btrfs_bioset)
168 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700169
170 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
171 goto free_bioset;
172
Chris Masond1310b22008-01-24 16:13:08 -0500173 return 0;
174
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700175free_bioset:
176 bioset_free(btrfs_bioset);
177 btrfs_bioset = NULL;
178
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);
Chris Mason9be33952013-05-17 18:30:14 -0400200 if (btrfs_bioset)
201 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500202}
203
204void extent_io_tree_init(struct extent_io_tree *tree,
Josef Bacikc6100a42017-05-05 11:57:13 -0400205 void *private_data)
Chris Masond1310b22008-01-24 16:13:08 -0500206{
Eric Paris6bef4d32010-02-23 19:43:04 +0000207 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500208 tree->ops = NULL;
209 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500210 spin_lock_init(&tree->lock);
Josef Bacikc6100a42017-05-05 11:57:13 -0400211 tree->private_data = private_data;
Chris Masond1310b22008-01-24 16:13:08 -0500212}
Chris Masond1310b22008-01-24 16:13:08 -0500213
Christoph Hellwigb2950862008-12-02 09:54:17 -0500214static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500215{
216 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500217
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100218 /*
219 * The given mask might be not appropriate for the slab allocator,
220 * drop the unsupported bits
221 */
222 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500223 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400224 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500225 return state;
226 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100227 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100228 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000229 btrfs_leak_debug_add(&state->leak_list, &states);
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200230 refcount_set(&state->refs, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500231 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100232 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500233 return state;
234}
Chris Masond1310b22008-01-24 16:13:08 -0500235
Chris Mason4845e442010-05-25 20:56:50 -0400236void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500237{
Chris Masond1310b22008-01-24 16:13:08 -0500238 if (!state)
239 return;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200240 if (refcount_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100241 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000242 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100243 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500244 kmem_cache_free(extent_state_cache, state);
245 }
246}
Chris Masond1310b22008-01-24 16:13:08 -0500247
Filipe Mananaf2071b22014-02-12 15:05:53 +0000248static struct rb_node *tree_insert(struct rb_root *root,
249 struct rb_node *search_start,
250 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000251 struct rb_node *node,
252 struct rb_node ***p_in,
253 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500254{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000255 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500256 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500257 struct tree_entry *entry;
258
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000259 if (p_in && parent_in) {
260 p = *p_in;
261 parent = *parent_in;
262 goto do_insert;
263 }
264
Filipe Mananaf2071b22014-02-12 15:05:53 +0000265 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500266 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500267 parent = *p;
268 entry = rb_entry(parent, struct tree_entry, rb_node);
269
270 if (offset < entry->start)
271 p = &(*p)->rb_left;
272 else if (offset > entry->end)
273 p = &(*p)->rb_right;
274 else
275 return parent;
276 }
277
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000278do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500279 rb_link_node(node, parent, p);
280 rb_insert_color(node, root);
281 return NULL;
282}
283
Chris Mason80ea96b2008-02-01 14:51:59 -0500284static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000285 struct rb_node **prev_ret,
286 struct rb_node **next_ret,
287 struct rb_node ***p_ret,
288 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500289{
Chris Mason80ea96b2008-02-01 14:51:59 -0500290 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000291 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500292 struct rb_node *prev = NULL;
293 struct rb_node *orig_prev = NULL;
294 struct tree_entry *entry;
295 struct tree_entry *prev_entry = NULL;
296
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000297 while (*n) {
298 prev = *n;
299 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500300 prev_entry = entry;
301
302 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000303 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500304 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000305 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500306 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000307 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500308 }
309
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000310 if (p_ret)
311 *p_ret = n;
312 if (parent_ret)
313 *parent_ret = prev;
314
Chris Masond1310b22008-01-24 16:13:08 -0500315 if (prev_ret) {
316 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500317 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500318 prev = rb_next(prev);
319 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
320 }
321 *prev_ret = prev;
322 prev = orig_prev;
323 }
324
325 if (next_ret) {
326 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500327 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500328 prev = rb_prev(prev);
329 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
330 }
331 *next_ret = prev;
332 }
333 return NULL;
334}
335
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000336static inline struct rb_node *
337tree_search_for_insert(struct extent_io_tree *tree,
338 u64 offset,
339 struct rb_node ***p_ret,
340 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500341{
Chris Mason70dec802008-01-29 09:59:12 -0500342 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500343 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500344
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000345 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500346 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500347 return prev;
348 return ret;
349}
350
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000351static inline struct rb_node *tree_search(struct extent_io_tree *tree,
352 u64 offset)
353{
354 return tree_search_for_insert(tree, offset, NULL, NULL);
355}
356
Josef Bacik9ed74f22009-09-11 16:12:44 -0400357static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
358 struct extent_state *other)
359{
360 if (tree->ops && tree->ops->merge_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400361 tree->ops->merge_extent_hook(tree->private_data, new, other);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400362}
363
Chris Masond1310b22008-01-24 16:13:08 -0500364/*
365 * utility function to look for merge candidates inside a given range.
366 * Any extents with matching state are merged together into a single
367 * extent in the tree. Extents with EXTENT_IO in their state field
368 * are not merged because the end_io handlers need to be able to do
369 * operations on them without sleeping (or doing allocations/splits).
370 *
371 * This should be called with the tree lock held.
372 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000373static void merge_state(struct extent_io_tree *tree,
374 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500375{
376 struct extent_state *other;
377 struct rb_node *other_node;
378
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400379 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000380 return;
Chris Masond1310b22008-01-24 16:13:08 -0500381
382 other_node = rb_prev(&state->rb_node);
383 if (other_node) {
384 other = rb_entry(other_node, struct extent_state, rb_node);
385 if (other->end == state->start - 1 &&
386 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500388 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500389 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100390 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500391 free_extent_state(other);
392 }
393 }
394 other_node = rb_next(&state->rb_node);
395 if (other_node) {
396 other = rb_entry(other_node, struct extent_state, rb_node);
397 if (other->start == state->end + 1 &&
398 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400399 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400400 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400401 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100402 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400403 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500404 }
405 }
Chris Masond1310b22008-01-24 16:13:08 -0500406}
407
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000408static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100409 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500410{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000411 if (tree->ops && tree->ops->set_bit_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400412 tree->ops->set_bit_hook(tree->private_data, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500413}
414
415static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100416 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500417{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400418 if (tree->ops && tree->ops->clear_bit_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400419 tree->ops->clear_bit_hook(tree->private_data, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500420}
421
Xiao Guangrong3150b692011-07-14 03:19:08 +0000422static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800423 struct extent_state *state, unsigned *bits,
424 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000425
Chris Masond1310b22008-01-24 16:13:08 -0500426/*
427 * insert an extent_state struct into the tree. 'bits' are set on the
428 * struct before it is inserted.
429 *
430 * This may return -EEXIST if the extent is already there, in which case the
431 * state struct is freed.
432 *
433 * The tree lock is not taken internally. This is a utility function and
434 * probably isn't what you want to call (see set/clear_extent_bit).
435 */
436static int insert_state(struct extent_io_tree *tree,
437 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000438 struct rb_node ***p,
439 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800440 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500441{
442 struct rb_node *node;
443
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000444 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500445 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200446 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500447 state->start = start;
448 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400449
Qu Wenruod38ed272015-10-12 14:53:37 +0800450 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000451
Filipe Mananaf2071b22014-02-12 15:05:53 +0000452 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500453 if (node) {
454 struct extent_state *found;
455 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400456 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200457 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500458 return -EEXIST;
459 }
460 merge_state(tree, state);
461 return 0;
462}
463
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000464static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400465 u64 split)
466{
467 if (tree->ops && tree->ops->split_extent_hook)
Josef Bacikc6100a42017-05-05 11:57:13 -0400468 tree->ops->split_extent_hook(tree->private_data, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400469}
470
Chris Masond1310b22008-01-24 16:13:08 -0500471/*
472 * split a given extent state struct in two, inserting the preallocated
473 * struct 'prealloc' as the newly created second half. 'split' indicates an
474 * offset inside 'orig' where it should be split.
475 *
476 * Before calling,
477 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
478 * are two extent state structs in the tree:
479 * prealloc: [orig->start, split - 1]
480 * orig: [ split, orig->end ]
481 *
482 * The tree locks are not taken by this function. They need to be held
483 * by the caller.
484 */
485static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
486 struct extent_state *prealloc, u64 split)
487{
488 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400489
490 split_cb(tree, orig, split);
491
Chris Masond1310b22008-01-24 16:13:08 -0500492 prealloc->start = orig->start;
493 prealloc->end = split - 1;
494 prealloc->state = orig->state;
495 orig->start = split;
496
Filipe Mananaf2071b22014-02-12 15:05:53 +0000497 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
498 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500499 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500500 free_extent_state(prealloc);
501 return -EEXIST;
502 }
503 return 0;
504}
505
Li Zefancdc6a392012-03-12 16:39:48 +0800506static struct extent_state *next_state(struct extent_state *state)
507{
508 struct rb_node *next = rb_next(&state->rb_node);
509 if (next)
510 return rb_entry(next, struct extent_state, rb_node);
511 else
512 return NULL;
513}
514
Chris Masond1310b22008-01-24 16:13:08 -0500515/*
516 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800517 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500518 *
519 * If no bits are set on the state struct after clearing things, the
520 * struct is freed and removed from the tree
521 */
Li Zefancdc6a392012-03-12 16:39:48 +0800522static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
523 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800524 unsigned *bits, int wake,
525 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500526{
Li Zefancdc6a392012-03-12 16:39:48 +0800527 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100528 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100529 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500530
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400531 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500532 u64 range = state->end - state->start + 1;
533 WARN_ON(range > tree->dirty_bytes);
534 tree->dirty_bytes -= range;
535 }
Chris Mason291d6732008-01-29 15:55:23 -0500536 clear_state_cb(tree, state, bits);
David Sterba57599c72018-03-01 17:56:34 +0100537 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
538 BUG_ON(ret < 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400539 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500540 if (wake)
541 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400542 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800543 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100544 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500545 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100546 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500547 free_extent_state(state);
548 } else {
549 WARN_ON(1);
550 }
551 } else {
552 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800553 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500554 }
Li Zefancdc6a392012-03-12 16:39:48 +0800555 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500556}
557
Xiao Guangrong82337672011-04-20 06:44:57 +0000558static struct extent_state *
559alloc_extent_state_atomic(struct extent_state *prealloc)
560{
561 if (!prealloc)
562 prealloc = alloc_extent_state(GFP_ATOMIC);
563
564 return prealloc;
565}
566
Eric Sandeen48a3b632013-04-25 20:41:01 +0000567static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400568{
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400569 btrfs_panic(tree_fs_info(tree), err,
570 "Locking error: Extent tree was modified by another thread while locked.");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400571}
572
Chris Masond1310b22008-01-24 16:13:08 -0500573/*
574 * clear some bits on a range in the tree. This may require splitting
575 * or inserting elements in the tree, so the gfp mask is used to
576 * indicate which allocations or sleeping are allowed.
577 *
578 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
579 * the given range from the tree regardless of state (ie for truncate).
580 *
581 * the range [start, end] is inclusive.
582 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100583 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500584 */
David Sterba66b0c882017-10-31 16:30:47 +0100585int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800586 unsigned bits, int wake, int delete,
587 struct extent_state **cached_state,
588 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500589{
590 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400591 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500592 struct extent_state *prealloc = NULL;
593 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400594 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500595 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000596 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500597
Josef Bacika5dee372013-12-13 10:02:44 -0500598 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000599
Josef Bacik7ee9e442013-06-21 16:37:03 -0400600 if (bits & EXTENT_DELALLOC)
601 bits |= EXTENT_NORESERVE;
602
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400603 if (delete)
604 bits |= ~EXTENT_CTLBITS;
605 bits |= EXTENT_FIRST_DELALLOC;
606
Josef Bacik2ac55d42010-02-03 19:33:23 +0000607 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
608 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500609again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800610 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000611 /*
612 * Don't care for allocation failure here because we might end
613 * up not needing the pre-allocated extent state at all, which
614 * is the case if we only have in the tree extent states that
615 * cover our input range and don't cover too any other range.
616 * If we end up needing a new extent state we allocate it later.
617 */
Chris Masond1310b22008-01-24 16:13:08 -0500618 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500619 }
620
Chris Masoncad321a2008-12-17 14:51:42 -0500621 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400622 if (cached_state) {
623 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000624
625 if (clear) {
626 *cached_state = NULL;
627 cached_state = NULL;
628 }
629
Filipe Manana27a35072014-07-06 20:09:59 +0100630 if (cached && extent_state_in_tree(cached) &&
631 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000632 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200633 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400634 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400635 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400636 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000637 if (clear)
638 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400639 }
Chris Masond1310b22008-01-24 16:13:08 -0500640 /*
641 * this search will find the extents that end after
642 * our range starts
643 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500644 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500645 if (!node)
646 goto out;
647 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400648hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500649 if (state->start > end)
650 goto out;
651 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400652 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500653
Liu Bo04493142012-02-16 18:34:37 +0800654 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800655 if (!(state->state & bits)) {
656 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800657 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800658 }
Liu Bo04493142012-02-16 18:34:37 +0800659
Chris Masond1310b22008-01-24 16:13:08 -0500660 /*
661 * | ---- desired range ---- |
662 * | state | or
663 * | ------------- state -------------- |
664 *
665 * We need to split the extent we found, and may flip
666 * bits on second half.
667 *
668 * If the extent we found extends past our range, we
669 * just split and search again. It'll get split again
670 * the next time though.
671 *
672 * If the extent we found is inside our range, we clear
673 * the desired bit on it.
674 */
675
676 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000677 prealloc = alloc_extent_state_atomic(prealloc);
678 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500679 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400680 if (err)
681 extent_io_tree_panic(tree, err);
682
Chris Masond1310b22008-01-24 16:13:08 -0500683 prealloc = NULL;
684 if (err)
685 goto out;
686 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800687 state = clear_state_bit(tree, state, &bits, wake,
688 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800689 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500690 }
691 goto search_again;
692 }
693 /*
694 * | ---- desired range ---- |
695 * | state |
696 * We need to split the extent, and clear the bit
697 * on the first half
698 */
699 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000700 prealloc = alloc_extent_state_atomic(prealloc);
701 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500702 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400703 if (err)
704 extent_io_tree_panic(tree, err);
705
Chris Masond1310b22008-01-24 16:13:08 -0500706 if (wake)
707 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400708
Qu Wenruofefdc552015-10-12 15:35:38 +0800709 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400710
Chris Masond1310b22008-01-24 16:13:08 -0500711 prealloc = NULL;
712 goto out;
713 }
Chris Mason42daec22009-09-23 19:51:09 -0400714
Qu Wenruofefdc552015-10-12 15:35:38 +0800715 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800716next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400717 if (last_end == (u64)-1)
718 goto out;
719 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800720 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800721 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500722
723search_again:
724 if (start > end)
725 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500726 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800727 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500728 cond_resched();
729 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200730
731out:
732 spin_unlock(&tree->lock);
733 if (prealloc)
734 free_extent_state(prealloc);
735
736 return 0;
737
Chris Masond1310b22008-01-24 16:13:08 -0500738}
Chris Masond1310b22008-01-24 16:13:08 -0500739
Jeff Mahoney143bede2012-03-01 14:56:26 +0100740static void wait_on_state(struct extent_io_tree *tree,
741 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500742 __releases(tree->lock)
743 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500744{
745 DEFINE_WAIT(wait);
746 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500747 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500748 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500749 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500750 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500751}
752
753/*
754 * waits for one or more bits to clear on a range in the state tree.
755 * The range [start, end] is inclusive.
756 * The tree lock is taken by this function
757 */
David Sterba41074882013-04-29 13:38:46 +0000758static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
759 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500760{
761 struct extent_state *state;
762 struct rb_node *node;
763
Josef Bacika5dee372013-12-13 10:02:44 -0500764 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000765
Chris Masoncad321a2008-12-17 14:51:42 -0500766 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500767again:
768 while (1) {
769 /*
770 * this search will find all the extents that end after
771 * our range starts
772 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500773 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100774process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500775 if (!node)
776 break;
777
778 state = rb_entry(node, struct extent_state, rb_node);
779
780 if (state->start > end)
781 goto out;
782
783 if (state->state & bits) {
784 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200785 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500786 wait_on_state(tree, state);
787 free_extent_state(state);
788 goto again;
789 }
790 start = state->end + 1;
791
792 if (start > end)
793 break;
794
Filipe Mananac50d3e72014-03-31 14:53:25 +0100795 if (!cond_resched_lock(&tree->lock)) {
796 node = rb_next(node);
797 goto process_node;
798 }
Chris Masond1310b22008-01-24 16:13:08 -0500799 }
800out:
Chris Masoncad321a2008-12-17 14:51:42 -0500801 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500802}
Chris Masond1310b22008-01-24 16:13:08 -0500803
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000804static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500805 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800806 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500807{
David Sterba9ee49a042015-01-14 19:52:13 +0100808 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
David Sterba57599c72018-03-01 17:56:34 +0100809 int ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400810
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000811 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400812 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500813 u64 range = state->end - state->start + 1;
814 tree->dirty_bytes += range;
815 }
David Sterba57599c72018-03-01 17:56:34 +0100816 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
817 BUG_ON(ret < 0);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400818 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500819}
820
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100821static void cache_state_if_flags(struct extent_state *state,
822 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100823 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400824{
825 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100826 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400827 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200828 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400829 }
830 }
831}
832
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100833static void cache_state(struct extent_state *state,
834 struct extent_state **cached_ptr)
835{
836 return cache_state_if_flags(state, cached_ptr,
837 EXTENT_IOBITS | EXTENT_BOUNDARY);
838}
839
Chris Masond1310b22008-01-24 16:13:08 -0500840/*
Chris Mason1edbb732009-09-02 13:24:36 -0400841 * set some bits on a range in the tree. This may require allocations or
842 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500843 *
Chris Mason1edbb732009-09-02 13:24:36 -0400844 * If any of the exclusive bits are set, this will fail with -EEXIST if some
845 * part of the range already has the desired bits set. The start of the
846 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500847 *
Chris Mason1edbb732009-09-02 13:24:36 -0400848 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500849 */
Chris Mason1edbb732009-09-02 13:24:36 -0400850
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100851static int __must_check
852__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100853 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000854 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800855 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500856{
857 struct extent_state *state;
858 struct extent_state *prealloc = NULL;
859 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000860 struct rb_node **p;
861 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500862 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500863 u64 last_start;
864 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400865
Josef Bacika5dee372013-12-13 10:02:44 -0500866 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000867
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400868 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500869again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800870 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200871 /*
872 * Don't care for allocation failure here because we might end
873 * up not needing the pre-allocated extent state at all, which
874 * is the case if we only have in the tree extent states that
875 * cover our input range and don't cover too any other range.
876 * If we end up needing a new extent state we allocate it later.
877 */
Chris Masond1310b22008-01-24 16:13:08 -0500878 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500879 }
880
Chris Masoncad321a2008-12-17 14:51:42 -0500881 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400882 if (cached_state && *cached_state) {
883 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400884 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100885 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400886 node = &state->rb_node;
887 goto hit_next;
888 }
889 }
Chris Masond1310b22008-01-24 16:13:08 -0500890 /*
891 * this search will find all the extents that end after
892 * our range starts.
893 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000894 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500895 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000896 prealloc = alloc_extent_state_atomic(prealloc);
897 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000898 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800899 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400900 if (err)
901 extent_io_tree_panic(tree, err);
902
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000903 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500904 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500905 goto out;
906 }
Chris Masond1310b22008-01-24 16:13:08 -0500907 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400908hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500909 last_start = state->start;
910 last_end = state->end;
911
912 /*
913 * | ---- desired range ---- |
914 * | state |
915 *
916 * Just lock what we found and keep going
917 */
918 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400919 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500920 *failed_start = state->start;
921 err = -EEXIST;
922 goto out;
923 }
Chris Mason42daec22009-09-23 19:51:09 -0400924
Qu Wenruod38ed272015-10-12 14:53:37 +0800925 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400926 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500927 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400928 if (last_end == (u64)-1)
929 goto out;
930 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800931 state = next_state(state);
932 if (start < end && state && state->start == start &&
933 !need_resched())
934 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500935 goto search_again;
936 }
937
938 /*
939 * | ---- desired range ---- |
940 * | state |
941 * or
942 * | ------------- state -------------- |
943 *
944 * We need to split the extent we found, and may flip bits on
945 * second half.
946 *
947 * If the extent we found extends past our
948 * range, we just split and search again. It'll get split
949 * again the next time though.
950 *
951 * If the extent we found is inside our range, we set the
952 * desired bit on it.
953 */
954 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400955 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500956 *failed_start = start;
957 err = -EEXIST;
958 goto out;
959 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000960
961 prealloc = alloc_extent_state_atomic(prealloc);
962 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500963 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400964 if (err)
965 extent_io_tree_panic(tree, err);
966
Chris Masond1310b22008-01-24 16:13:08 -0500967 prealloc = NULL;
968 if (err)
969 goto out;
970 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800971 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400972 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500973 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400974 if (last_end == (u64)-1)
975 goto out;
976 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800977 state = next_state(state);
978 if (start < end && state && state->start == start &&
979 !need_resched())
980 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500981 }
982 goto search_again;
983 }
984 /*
985 * | ---- desired range ---- |
986 * | state | or | state |
987 *
988 * There's a hole, we need to insert something in it and
989 * ignore the extent we found.
990 */
991 if (state->start > start) {
992 u64 this_end;
993 if (end < last_start)
994 this_end = end;
995 else
Chris Masond3977122009-01-05 21:25:51 -0500996 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000997
998 prealloc = alloc_extent_state_atomic(prealloc);
999 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001000
1001 /*
1002 * Avoid to free 'prealloc' if it can be merged with
1003 * the later extent.
1004 */
Chris Masond1310b22008-01-24 16:13:08 -05001005 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001006 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001007 if (err)
1008 extent_io_tree_panic(tree, err);
1009
Chris Mason2c64c532009-09-02 15:04:12 -04001010 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001011 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001012 start = this_end + 1;
1013 goto search_again;
1014 }
1015 /*
1016 * | ---- desired range ---- |
1017 * | state |
1018 * We need to split the extent, and set the bit
1019 * on the first half
1020 */
1021 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001022 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001023 *failed_start = start;
1024 err = -EEXIST;
1025 goto out;
1026 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001027
1028 prealloc = alloc_extent_state_atomic(prealloc);
1029 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001030 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001031 if (err)
1032 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001033
Qu Wenruod38ed272015-10-12 14:53:37 +08001034 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001035 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001036 merge_state(tree, prealloc);
1037 prealloc = NULL;
1038 goto out;
1039 }
1040
David Sterbab5a4ba142016-04-27 01:02:15 +02001041search_again:
1042 if (start > end)
1043 goto out;
1044 spin_unlock(&tree->lock);
1045 if (gfpflags_allow_blocking(mask))
1046 cond_resched();
1047 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001048
1049out:
Chris Masoncad321a2008-12-17 14:51:42 -05001050 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001051 if (prealloc)
1052 free_extent_state(prealloc);
1053
1054 return err;
1055
Chris Masond1310b22008-01-24 16:13:08 -05001056}
Chris Masond1310b22008-01-24 16:13:08 -05001057
David Sterba41074882013-04-29 13:38:46 +00001058int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001059 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001060 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001061{
1062 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001063 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001064}
1065
1066
Josef Bacik462d6fa2011-09-26 13:56:12 -04001067/**
Liu Bo10983f22012-07-11 15:26:19 +08001068 * convert_extent_bit - convert all bits in a given range from one bit to
1069 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070 * @tree: the io tree to search
1071 * @start: the start offset in bytes
1072 * @end: the end offset in bytes (inclusive)
1073 * @bits: the bits to set in this range
1074 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001075 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001076 *
1077 * This will go through and set bits for the given range. If any states exist
1078 * already in this range they are set with the given bit and cleared of the
1079 * clear_bits. This is only meant to be used by things that are mergeable, ie
1080 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1081 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001082 *
1083 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084 */
1085int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001086 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001087 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001088{
1089 struct extent_state *state;
1090 struct extent_state *prealloc = NULL;
1091 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001092 struct rb_node **p;
1093 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094 int err = 0;
1095 u64 last_start;
1096 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001097 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001098
Josef Bacika5dee372013-12-13 10:02:44 -05001099 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001100
Josef Bacik462d6fa2011-09-26 13:56:12 -04001101again:
David Sterba210aa272016-04-26 23:54:39 +02001102 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001103 /*
1104 * Best effort, don't worry if extent state allocation fails
1105 * here for the first iteration. We might have a cached state
1106 * that matches exactly the target range, in which case no
1107 * extent state allocations are needed. We'll only know this
1108 * after locking the tree.
1109 */
David Sterba210aa272016-04-26 23:54:39 +02001110 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001111 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001112 return -ENOMEM;
1113 }
1114
1115 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001116 if (cached_state && *cached_state) {
1117 state = *cached_state;
1118 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001119 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001120 node = &state->rb_node;
1121 goto hit_next;
1122 }
1123 }
1124
Josef Bacik462d6fa2011-09-26 13:56:12 -04001125 /*
1126 * this search will find all the extents that end after
1127 * our range starts.
1128 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001129 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001130 if (!node) {
1131 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001132 if (!prealloc) {
1133 err = -ENOMEM;
1134 goto out;
1135 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001136 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001137 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001138 if (err)
1139 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001140 cache_state(prealloc, cached_state);
1141 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001142 goto out;
1143 }
1144 state = rb_entry(node, struct extent_state, rb_node);
1145hit_next:
1146 last_start = state->start;
1147 last_end = state->end;
1148
1149 /*
1150 * | ---- desired range ---- |
1151 * | state |
1152 *
1153 * Just lock what we found and keep going
1154 */
1155 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001156 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001157 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001158 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001159 if (last_end == (u64)-1)
1160 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001161 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001162 if (start < end && state && state->start == start &&
1163 !need_resched())
1164 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001165 goto search_again;
1166 }
1167
1168 /*
1169 * | ---- desired range ---- |
1170 * | state |
1171 * or
1172 * | ------------- state -------------- |
1173 *
1174 * We need to split the extent we found, and may flip bits on
1175 * second half.
1176 *
1177 * If the extent we found extends past our
1178 * range, we just split and search again. It'll get split
1179 * again the next time though.
1180 *
1181 * If the extent we found is inside our range, we set the
1182 * desired bit on it.
1183 */
1184 if (state->start < start) {
1185 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001186 if (!prealloc) {
1187 err = -ENOMEM;
1188 goto out;
1189 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001190 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001191 if (err)
1192 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001193 prealloc = NULL;
1194 if (err)
1195 goto out;
1196 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001197 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001198 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001199 state = clear_state_bit(tree, state, &clear_bits, 0,
1200 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001201 if (last_end == (u64)-1)
1202 goto out;
1203 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001204 if (start < end && state && state->start == start &&
1205 !need_resched())
1206 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001207 }
1208 goto search_again;
1209 }
1210 /*
1211 * | ---- desired range ---- |
1212 * | state | or | state |
1213 *
1214 * There's a hole, we need to insert something in it and
1215 * ignore the extent we found.
1216 */
1217 if (state->start > start) {
1218 u64 this_end;
1219 if (end < last_start)
1220 this_end = end;
1221 else
1222 this_end = last_start - 1;
1223
1224 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001225 if (!prealloc) {
1226 err = -ENOMEM;
1227 goto out;
1228 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001229
1230 /*
1231 * Avoid to free 'prealloc' if it can be merged with
1232 * the later extent.
1233 */
1234 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001235 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001236 if (err)
1237 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001238 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001239 prealloc = NULL;
1240 start = this_end + 1;
1241 goto search_again;
1242 }
1243 /*
1244 * | ---- desired range ---- |
1245 * | state |
1246 * We need to split the extent, and set the bit
1247 * on the first half
1248 */
1249 if (state->start <= end && state->end > end) {
1250 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001251 if (!prealloc) {
1252 err = -ENOMEM;
1253 goto out;
1254 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001255
1256 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001257 if (err)
1258 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001259
Qu Wenruod38ed272015-10-12 14:53:37 +08001260 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001261 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001262 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001263 prealloc = NULL;
1264 goto out;
1265 }
1266
Josef Bacik462d6fa2011-09-26 13:56:12 -04001267search_again:
1268 if (start > end)
1269 goto out;
1270 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001271 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001272 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001273 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001274
1275out:
1276 spin_unlock(&tree->lock);
1277 if (prealloc)
1278 free_extent_state(prealloc);
1279
1280 return err;
1281}
1282
Chris Masond1310b22008-01-24 16:13:08 -05001283/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001284int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001285 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001286{
1287 /*
1288 * We don't support EXTENT_LOCKED yet, as current changeset will
1289 * record any bits changed, so for EXTENT_LOCKED case, it will
1290 * either fail with -EEXIST or changeset will record the whole
1291 * range.
1292 */
1293 BUG_ON(bits & EXTENT_LOCKED);
1294
David Sterba2c53b912016-04-26 23:54:39 +02001295 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001296 changeset);
1297}
1298
Qu Wenruofefdc552015-10-12 15:35:38 +08001299int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1300 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001301 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001302{
1303 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001304 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001305}
1306
Qu Wenruofefdc552015-10-12 15:35:38 +08001307int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001308 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001309{
1310 /*
1311 * Don't support EXTENT_LOCKED case, same reason as
1312 * set_record_extent_bits().
1313 */
1314 BUG_ON(bits & EXTENT_LOCKED);
1315
David Sterbaf734c442016-04-26 23:54:39 +02001316 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001317 changeset);
1318}
1319
Chris Masond352ac62008-09-29 15:18:18 -04001320/*
1321 * either insert or lock state struct between start and end use mask to tell
1322 * us if waiting is desired.
1323 */
Chris Mason1edbb732009-09-02 13:24:36 -04001324int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001325 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001326{
1327 int err;
1328 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001329
Chris Masond1310b22008-01-24 16:13:08 -05001330 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001331 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001332 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001333 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001334 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001335 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1336 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001337 } else
Chris Masond1310b22008-01-24 16:13:08 -05001338 break;
Chris Masond1310b22008-01-24 16:13:08 -05001339 WARN_ON(start > end);
1340 }
1341 return err;
1342}
Chris Masond1310b22008-01-24 16:13:08 -05001343
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001344int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001345{
1346 int err;
1347 u64 failed_start;
1348
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001349 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001350 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001351 if (err == -EEXIST) {
1352 if (failed_start > start)
1353 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001354 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001355 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001356 }
Josef Bacik25179202008-10-29 14:49:05 -04001357 return 1;
1358}
Josef Bacik25179202008-10-29 14:49:05 -04001359
David Sterbabd1fa4f2015-12-03 13:08:59 +01001360void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001361{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001362 unsigned long index = start >> PAGE_SHIFT;
1363 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001364 struct page *page;
1365
1366 while (index <= end_index) {
1367 page = find_get_page(inode->i_mapping, index);
1368 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1369 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001370 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001371 index++;
1372 }
Chris Mason4adaa612013-03-26 13:07:00 -04001373}
1374
David Sterbaf6311572015-12-03 13:08:59 +01001375void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001376{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001377 unsigned long index = start >> PAGE_SHIFT;
1378 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001379 struct page *page;
1380
1381 while (index <= end_index) {
1382 page = find_get_page(inode->i_mapping, index);
1383 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001384 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001385 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001386 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001387 index++;
1388 }
Chris Mason4adaa612013-03-26 13:07:00 -04001389}
1390
Chris Masond1310b22008-01-24 16:13:08 -05001391/*
Chris Masond1310b22008-01-24 16:13:08 -05001392 * helper function to set both pages and extents in the tree writeback
1393 */
David Sterba35de6db2015-12-03 13:08:59 +01001394static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001395{
Josef Bacikc6100a42017-05-05 11:57:13 -04001396 tree->ops->set_range_writeback(tree->private_data, start, end);
Chris Masond1310b22008-01-24 16:13:08 -05001397}
Chris Masond1310b22008-01-24 16:13:08 -05001398
Chris Masond352ac62008-09-29 15:18:18 -04001399/* find the first state struct with 'bits' set after 'start', and
1400 * return it. tree->lock must be held. NULL will returned if
1401 * nothing was found after 'start'
1402 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001403static struct extent_state *
1404find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001405 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001406{
1407 struct rb_node *node;
1408 struct extent_state *state;
1409
1410 /*
1411 * this search will find all the extents that end after
1412 * our range starts.
1413 */
1414 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001415 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001416 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001417
Chris Masond3977122009-01-05 21:25:51 -05001418 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001419 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001420 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001421 return state;
Chris Masond3977122009-01-05 21:25:51 -05001422
Chris Masond7fc6402008-02-18 12:12:38 -05001423 node = rb_next(node);
1424 if (!node)
1425 break;
1426 }
1427out:
1428 return NULL;
1429}
Chris Masond7fc6402008-02-18 12:12:38 -05001430
Chris Masond352ac62008-09-29 15:18:18 -04001431/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001432 * find the first offset in the io tree with 'bits' set. zero is
1433 * returned if we find something, and *start_ret and *end_ret are
1434 * set to reflect the state struct that was found.
1435 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001436 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001437 */
1438int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001439 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001440 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001441{
1442 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001443 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001444 int ret = 1;
1445
1446 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001447 if (cached_state && *cached_state) {
1448 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001449 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001450 n = rb_next(&state->rb_node);
1451 while (n) {
1452 state = rb_entry(n, struct extent_state,
1453 rb_node);
1454 if (state->state & bits)
1455 goto got_it;
1456 n = rb_next(n);
1457 }
1458 free_extent_state(*cached_state);
1459 *cached_state = NULL;
1460 goto out;
1461 }
1462 free_extent_state(*cached_state);
1463 *cached_state = NULL;
1464 }
1465
Xiao Guangrong69261c42011-07-14 03:19:45 +00001466 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001467got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001468 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001469 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001470 *start_ret = state->start;
1471 *end_ret = state->end;
1472 ret = 0;
1473 }
Josef Bacike6138872012-09-27 17:07:30 -04001474out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001475 spin_unlock(&tree->lock);
1476 return ret;
1477}
1478
1479/*
Chris Masond352ac62008-09-29 15:18:18 -04001480 * find a contiguous range of bytes in the file marked as delalloc, not
1481 * more than 'max_bytes'. start and end are used to return the range,
1482 *
1483 * 1 is returned if we find something, 0 if nothing was in the tree
1484 */
Chris Masonc8b97812008-10-29 14:49:59 -04001485static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001486 u64 *start, u64 *end, u64 max_bytes,
1487 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001488{
1489 struct rb_node *node;
1490 struct extent_state *state;
1491 u64 cur_start = *start;
1492 u64 found = 0;
1493 u64 total_bytes = 0;
1494
Chris Masoncad321a2008-12-17 14:51:42 -05001495 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001496
Chris Masond1310b22008-01-24 16:13:08 -05001497 /*
1498 * this search will find all the extents that end after
1499 * our range starts.
1500 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001501 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001502 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001503 if (!found)
1504 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001505 goto out;
1506 }
1507
Chris Masond3977122009-01-05 21:25:51 -05001508 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001509 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001510 if (found && (state->start != cur_start ||
1511 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001512 goto out;
1513 }
1514 if (!(state->state & EXTENT_DELALLOC)) {
1515 if (!found)
1516 *end = state->end;
1517 goto out;
1518 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001519 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001520 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001521 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001522 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001523 }
Chris Masond1310b22008-01-24 16:13:08 -05001524 found++;
1525 *end = state->end;
1526 cur_start = state->end + 1;
1527 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001528 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001529 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001530 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001531 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001532 break;
1533 }
1534out:
Chris Masoncad321a2008-12-17 14:51:42 -05001535 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001536 return found;
1537}
1538
Liu Boda2c7002017-02-10 16:41:05 +01001539static int __process_pages_contig(struct address_space *mapping,
1540 struct page *locked_page,
1541 pgoff_t start_index, pgoff_t end_index,
1542 unsigned long page_ops, pgoff_t *index_ret);
1543
Jeff Mahoney143bede2012-03-01 14:56:26 +01001544static noinline void __unlock_for_delalloc(struct inode *inode,
1545 struct page *locked_page,
1546 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001547{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001548 unsigned long index = start >> PAGE_SHIFT;
1549 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001550
Liu Bo76c00212017-02-10 16:42:14 +01001551 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001552 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001553 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001554
Liu Bo76c00212017-02-10 16:42:14 +01001555 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1556 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001557}
1558
1559static noinline int lock_delalloc_pages(struct inode *inode,
1560 struct page *locked_page,
1561 u64 delalloc_start,
1562 u64 delalloc_end)
1563{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001564 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001565 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001566 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001567 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001568
Liu Bo76c00212017-02-10 16:42:14 +01001569 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001570 if (index == locked_page->index && index == end_index)
1571 return 0;
1572
Liu Bo76c00212017-02-10 16:42:14 +01001573 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1574 end_index, PAGE_LOCK, &index_ret);
1575 if (ret == -EAGAIN)
1576 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1577 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001578 return ret;
1579}
1580
1581/*
1582 * find a contiguous range of bytes in the file marked as delalloc, not
1583 * more than 'max_bytes'. start and end are used to return the range,
1584 *
1585 * 1 is returned if we find something, 0 if nothing was in the tree
1586 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001587STATIC u64 find_lock_delalloc_range(struct inode *inode,
1588 struct extent_io_tree *tree,
1589 struct page *locked_page, u64 *start,
1590 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001591{
1592 u64 delalloc_start;
1593 u64 delalloc_end;
1594 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001595 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001596 int ret;
1597 int loops = 0;
1598
1599again:
1600 /* step one, find a bunch of delalloc bytes starting at start */
1601 delalloc_start = *start;
1602 delalloc_end = 0;
1603 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001604 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001605 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001606 *start = delalloc_start;
1607 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001608 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001609 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001610 }
1611
1612 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001613 * start comes from the offset of locked_page. We have to lock
1614 * pages in order, so we can't process delalloc bytes before
1615 * locked_page
1616 */
Chris Masond3977122009-01-05 21:25:51 -05001617 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001618 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001619
1620 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001621 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001622 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001623 if (delalloc_end + 1 - delalloc_start > max_bytes)
1624 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001625
Chris Masonc8b97812008-10-29 14:49:59 -04001626 /* step two, lock all the pages after the page that has start */
1627 ret = lock_delalloc_pages(inode, locked_page,
1628 delalloc_start, delalloc_end);
1629 if (ret == -EAGAIN) {
1630 /* some of the pages are gone, lets avoid looping by
1631 * shortening the size of the delalloc range we're searching
1632 */
Chris Mason9655d292009-09-02 15:22:30 -04001633 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001634 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001635 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001636 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001637 loops = 1;
1638 goto again;
1639 } else {
1640 found = 0;
1641 goto out_failed;
1642 }
1643 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001644 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001645
1646 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001647 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001648
1649 /* then test to make sure it is all still delalloc */
1650 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001651 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001652 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001653 unlock_extent_cached(tree, delalloc_start, delalloc_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001654 &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001655 __unlock_for_delalloc(inode, locked_page,
1656 delalloc_start, delalloc_end);
1657 cond_resched();
1658 goto again;
1659 }
Chris Mason9655d292009-09-02 15:22:30 -04001660 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001661 *start = delalloc_start;
1662 *end = delalloc_end;
1663out_failed:
1664 return found;
1665}
1666
Liu Boda2c7002017-02-10 16:41:05 +01001667static int __process_pages_contig(struct address_space *mapping,
1668 struct page *locked_page,
1669 pgoff_t start_index, pgoff_t end_index,
1670 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001671{
Liu Bo873695b2017-02-02 17:49:22 -08001672 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001673 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001674 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001675 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001676 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001677 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001678 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001679
Liu Boda2c7002017-02-10 16:41:05 +01001680 if (page_ops & PAGE_LOCK) {
1681 ASSERT(page_ops == PAGE_LOCK);
1682 ASSERT(index_ret && *index_ret == start_index);
1683 }
1684
Filipe Manana704de492014-10-06 22:14:22 +01001685 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001686 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001687
Chris Masond3977122009-01-05 21:25:51 -05001688 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001689 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001690 min_t(unsigned long,
1691 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001692 if (ret == 0) {
1693 /*
1694 * Only if we're going to lock these pages,
1695 * can we find nothing at @index.
1696 */
1697 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001698 err = -EAGAIN;
1699 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001700 }
Chris Mason8b62b722009-09-02 16:53:46 -04001701
Liu Boda2c7002017-02-10 16:41:05 +01001702 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001703 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001704 SetPagePrivate2(pages[i]);
1705
Chris Masonc8b97812008-10-29 14:49:59 -04001706 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001707 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001708 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001709 continue;
1710 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001711 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001712 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001713 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001714 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001715 if (page_ops & PAGE_SET_ERROR)
1716 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001717 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001718 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001719 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001720 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001721 if (page_ops & PAGE_LOCK) {
1722 lock_page(pages[i]);
1723 if (!PageDirty(pages[i]) ||
1724 pages[i]->mapping != mapping) {
1725 unlock_page(pages[i]);
1726 put_page(pages[i]);
1727 err = -EAGAIN;
1728 goto out;
1729 }
1730 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001731 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001732 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001733 }
1734 nr_pages -= ret;
1735 index += ret;
1736 cond_resched();
1737 }
Liu Boda2c7002017-02-10 16:41:05 +01001738out:
1739 if (err && index_ret)
1740 *index_ret = start_index + pages_locked - 1;
1741 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001742}
Chris Masonc8b97812008-10-29 14:49:59 -04001743
Liu Bo873695b2017-02-02 17:49:22 -08001744void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1745 u64 delalloc_end, struct page *locked_page,
1746 unsigned clear_bits,
1747 unsigned long page_ops)
1748{
1749 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001750 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001751
1752 __process_pages_contig(inode->i_mapping, locked_page,
1753 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001754 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001755}
1756
Chris Masond352ac62008-09-29 15:18:18 -04001757/*
1758 * count the number of bytes in the tree that have a given bit(s)
1759 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1760 * cached. The total number found is returned.
1761 */
Chris Masond1310b22008-01-24 16:13:08 -05001762u64 count_range_bits(struct extent_io_tree *tree,
1763 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001764 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001765{
1766 struct rb_node *node;
1767 struct extent_state *state;
1768 u64 cur_start = *start;
1769 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001770 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001771 int found = 0;
1772
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301773 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001774 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001775
Chris Masoncad321a2008-12-17 14:51:42 -05001776 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001777 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1778 total_bytes = tree->dirty_bytes;
1779 goto out;
1780 }
1781 /*
1782 * this search will find all the extents that end after
1783 * our range starts.
1784 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001785 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001786 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001787 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001788
Chris Masond3977122009-01-05 21:25:51 -05001789 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001790 state = rb_entry(node, struct extent_state, rb_node);
1791 if (state->start > search_end)
1792 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001793 if (contig && found && state->start > last + 1)
1794 break;
1795 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001796 total_bytes += min(search_end, state->end) + 1 -
1797 max(cur_start, state->start);
1798 if (total_bytes >= max_bytes)
1799 break;
1800 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001801 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001802 found = 1;
1803 }
Chris Masonec29ed52011-02-23 16:23:20 -05001804 last = state->end;
1805 } else if (contig && found) {
1806 break;
Chris Masond1310b22008-01-24 16:13:08 -05001807 }
1808 node = rb_next(node);
1809 if (!node)
1810 break;
1811 }
1812out:
Chris Masoncad321a2008-12-17 14:51:42 -05001813 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001814 return total_bytes;
1815}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001816
Chris Masond352ac62008-09-29 15:18:18 -04001817/*
1818 * set the private field for a given byte offset in the tree. If there isn't
1819 * an extent_state there already, this does nothing.
1820 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001821static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001822 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001823{
1824 struct rb_node *node;
1825 struct extent_state *state;
1826 int ret = 0;
1827
Chris Masoncad321a2008-12-17 14:51:42 -05001828 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001829 /*
1830 * this search will find all the extents that end after
1831 * our range starts.
1832 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001833 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001834 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001835 ret = -ENOENT;
1836 goto out;
1837 }
1838 state = rb_entry(node, struct extent_state, rb_node);
1839 if (state->start != start) {
1840 ret = -ENOENT;
1841 goto out;
1842 }
David Sterba47dc1962016-02-11 13:24:13 +01001843 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001844out:
Chris Masoncad321a2008-12-17 14:51:42 -05001845 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001846 return ret;
1847}
1848
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001849static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001850 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001851{
1852 struct rb_node *node;
1853 struct extent_state *state;
1854 int ret = 0;
1855
Chris Masoncad321a2008-12-17 14:51:42 -05001856 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001857 /*
1858 * this search will find all the extents that end after
1859 * our range starts.
1860 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001861 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001862 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001863 ret = -ENOENT;
1864 goto out;
1865 }
1866 state = rb_entry(node, struct extent_state, rb_node);
1867 if (state->start != start) {
1868 ret = -ENOENT;
1869 goto out;
1870 }
David Sterba47dc1962016-02-11 13:24:13 +01001871 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001872out:
Chris Masoncad321a2008-12-17 14:51:42 -05001873 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001874 return ret;
1875}
1876
1877/*
1878 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001879 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001880 * has the bits set. Otherwise, 1 is returned if any bit in the
1881 * range is found set.
1882 */
1883int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001884 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001885{
1886 struct extent_state *state = NULL;
1887 struct rb_node *node;
1888 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001889
Chris Masoncad321a2008-12-17 14:51:42 -05001890 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001891 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001892 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001893 node = &cached->rb_node;
1894 else
1895 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001896 while (node && start <= end) {
1897 state = rb_entry(node, struct extent_state, rb_node);
1898
1899 if (filled && state->start > start) {
1900 bitset = 0;
1901 break;
1902 }
1903
1904 if (state->start > end)
1905 break;
1906
1907 if (state->state & bits) {
1908 bitset = 1;
1909 if (!filled)
1910 break;
1911 } else if (filled) {
1912 bitset = 0;
1913 break;
1914 }
Chris Mason46562ce2009-09-23 20:23:16 -04001915
1916 if (state->end == (u64)-1)
1917 break;
1918
Chris Masond1310b22008-01-24 16:13:08 -05001919 start = state->end + 1;
1920 if (start > end)
1921 break;
1922 node = rb_next(node);
1923 if (!node) {
1924 if (filled)
1925 bitset = 0;
1926 break;
1927 }
1928 }
Chris Masoncad321a2008-12-17 14:51:42 -05001929 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001930 return bitset;
1931}
Chris Masond1310b22008-01-24 16:13:08 -05001932
1933/*
1934 * helper function to set a given page up to date if all the
1935 * extents in the tree for that page are up to date
1936 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001937static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001938{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001939 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001940 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001941 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001942 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001943}
1944
Josef Bacik7870d082017-05-05 11:57:15 -04001945int free_io_failure(struct extent_io_tree *failure_tree,
1946 struct extent_io_tree *io_tree,
1947 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001948{
1949 int ret;
1950 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001951
David Sterba47dc1962016-02-11 13:24:13 +01001952 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001953 ret = clear_extent_bits(failure_tree, rec->start,
1954 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001955 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001956 if (ret)
1957 err = ret;
1958
Josef Bacik7870d082017-05-05 11:57:15 -04001959 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001960 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001961 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001962 if (ret && !err)
1963 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001964
1965 kfree(rec);
1966 return err;
1967}
1968
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001969/*
1970 * this bypasses the standard btrfs submit functions deliberately, as
1971 * the standard behavior is to write all copies in a raid setup. here we only
1972 * want to write the one bad copy. so we do the mapping for ourselves and issue
1973 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001974 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001975 * actually prevents the read that triggered the error from finishing.
1976 * currently, there can be no more than two copies of every data bit. thus,
1977 * exactly one rewrite is required.
1978 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001979int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1980 u64 length, u64 logical, struct page *page,
1981 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001982{
1983 struct bio *bio;
1984 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001985 u64 map_length = 0;
1986 u64 sector;
1987 struct btrfs_bio *bbio = NULL;
1988 int ret;
1989
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001990 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991 BUG_ON(!mirror_num);
1992
David Sterbac5e4c3d2017-06-12 17:29:41 +02001993 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001994 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001995 map_length = length;
1996
Filipe Mananab5de8d02016-05-27 22:21:27 +01001997 /*
1998 * Avoid races with device replace and make sure our bbio has devices
1999 * associated to its stripes that don't go away while we are doing the
2000 * read repair operation.
2001 */
2002 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03002003 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002004 /*
2005 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2006 * to update all raid stripes, but here we just want to correct
2007 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2008 * stripe's dev and sector.
2009 */
2010 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2011 &map_length, &bbio, 0);
2012 if (ret) {
2013 btrfs_bio_counter_dec(fs_info);
2014 bio_put(bio);
2015 return -EIO;
2016 }
2017 ASSERT(bbio->mirror_num == 1);
2018 } else {
2019 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2020 &map_length, &bbio, mirror_num);
2021 if (ret) {
2022 btrfs_bio_counter_dec(fs_info);
2023 bio_put(bio);
2024 return -EIO;
2025 }
2026 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002027 }
Liu Boc7253282017-03-29 10:53:58 -07002028
2029 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002030 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002031 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002032 btrfs_put_bbio(bbio);
Anand Jainebbede42017-12-04 12:54:52 +08002033 if (!dev || !dev->bdev ||
2034 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002035 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 bio_put(bio);
2037 return -EIO;
2038 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002039 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002040 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002041 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002042
Mike Christie4e49ea42016-06-05 14:31:41 -05002043 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002044 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002045 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002047 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002048 return -EIO;
2049 }
2050
David Sterbab14af3b2015-10-08 10:43:10 +02002051 btrfs_info_rl_in_rcu(fs_info,
2052 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002053 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002054 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002055 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002056 bio_put(bio);
2057 return 0;
2058}
2059
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002060int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2061 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002062{
Josef Bacikea466792012-03-26 21:57:36 -04002063 u64 start = eb->start;
2064 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002065 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002066
David Howellsbc98a422017-07-17 08:45:34 +01002067 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002068 return -EROFS;
2069
Josef Bacikea466792012-03-26 21:57:36 -04002070 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002071 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002072
Josef Bacik6ec656b2017-05-05 11:57:14 -04002073 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002074 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002075 if (ret)
2076 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002077 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002078 }
2079
2080 return ret;
2081}
2082
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002083/*
2084 * each time an IO finishes, we do a fast check in the IO failure tree
2085 * to see if we need to process or clean up an io_failure_record
2086 */
Josef Bacik7870d082017-05-05 11:57:15 -04002087int clean_io_failure(struct btrfs_fs_info *fs_info,
2088 struct extent_io_tree *failure_tree,
2089 struct extent_io_tree *io_tree, u64 start,
2090 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002091{
2092 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002093 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002094 struct extent_state *state;
2095 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002097
2098 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002099 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2100 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002101 if (!ret)
2102 return 0;
2103
Josef Bacik7870d082017-05-05 11:57:15 -04002104 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105 if (ret)
2106 return 0;
2107
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108 BUG_ON(!failrec->this_mirror);
2109
2110 if (failrec->in_validation) {
2111 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002112 btrfs_debug(fs_info,
2113 "clean_io_failure: freeing dummy error at %llu",
2114 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002115 goto out;
2116 }
David Howellsbc98a422017-07-17 08:45:34 +01002117 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002118 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002119
Josef Bacik7870d082017-05-05 11:57:15 -04002120 spin_lock(&io_tree->lock);
2121 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002122 failrec->start,
2123 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002124 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002125
Miao Xie883d0de2013-07-25 19:22:35 +08002126 if (state && state->start <= failrec->start &&
2127 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002128 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2129 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002130 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002131 repair_io_failure(fs_info, ino, start, failrec->len,
2132 failrec->logical, page, pg_offset,
2133 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002134 }
2135 }
2136
2137out:
Josef Bacik7870d082017-05-05 11:57:15 -04002138 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139
Miao Xie454ff3d2014-09-12 18:43:58 +08002140 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002141}
2142
Miao Xief6124962014-09-12 18:44:04 +08002143/*
2144 * Can be called when
2145 * - hold extent lock
2146 * - under ordered extent
2147 * - the inode is freeing
2148 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002149void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002150{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002151 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002152 struct io_failure_record *failrec;
2153 struct extent_state *state, *next;
2154
2155 if (RB_EMPTY_ROOT(&failure_tree->state))
2156 return;
2157
2158 spin_lock(&failure_tree->lock);
2159 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2160 while (state) {
2161 if (state->start > end)
2162 break;
2163
2164 ASSERT(state->end <= end);
2165
2166 next = next_state(state);
2167
David Sterba47dc1962016-02-11 13:24:13 +01002168 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002169 free_extent_state(state);
2170 kfree(failrec);
2171
2172 state = next;
2173 }
2174 spin_unlock(&failure_tree->lock);
2175}
2176
Miao Xie2fe63032014-09-12 18:43:59 +08002177int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002178 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002179{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002181 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002182 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002183 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2184 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2185 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002186 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 u64 logical;
2188
David Sterba47dc1962016-02-11 13:24:13 +01002189 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002190 if (ret) {
2191 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2192 if (!failrec)
2193 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002194
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002195 failrec->start = start;
2196 failrec->len = end - start + 1;
2197 failrec->this_mirror = 0;
2198 failrec->bio_flags = 0;
2199 failrec->in_validation = 0;
2200
2201 read_lock(&em_tree->lock);
2202 em = lookup_extent_mapping(em_tree, start, failrec->len);
2203 if (!em) {
2204 read_unlock(&em_tree->lock);
2205 kfree(failrec);
2206 return -EIO;
2207 }
2208
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002209 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002210 free_extent_map(em);
2211 em = NULL;
2212 }
2213 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002214 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002215 kfree(failrec);
2216 return -EIO;
2217 }
Miao Xie2fe63032014-09-12 18:43:59 +08002218
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002219 logical = start - em->start;
2220 logical = em->block_start + logical;
2221 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2222 logical = em->block_start;
2223 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2224 extent_set_compress_type(&failrec->bio_flags,
2225 em->compress_type);
2226 }
Miao Xie2fe63032014-09-12 18:43:59 +08002227
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002228 btrfs_debug(fs_info,
2229 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2230 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002231
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002232 failrec->logical = logical;
2233 free_extent_map(em);
2234
2235 /* set the bits in the private failure tree */
2236 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002237 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002238 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002239 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002240 /* set the bits in the inode's tree */
2241 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002242 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 if (ret < 0) {
2244 kfree(failrec);
2245 return ret;
2246 }
2247 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002248 btrfs_debug(fs_info,
2249 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2250 failrec->logical, failrec->start, failrec->len,
2251 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002252 /*
2253 * when data can be on disk more than twice, add to failrec here
2254 * (e.g. with a list for failed_mirror) to make
2255 * clean_io_failure() clean all those errors at once.
2256 */
2257 }
Miao Xie2fe63032014-09-12 18:43:59 +08002258
2259 *failrec_ret = failrec;
2260
2261 return 0;
2262}
2263
Ming Leia0b60d72017-12-18 20:22:11 +08002264bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages,
Miao Xie2fe63032014-09-12 18:43:59 +08002265 struct io_failure_record *failrec, int failed_mirror)
2266{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002267 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002268 int num_copies;
2269
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002270 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 if (num_copies == 1) {
2272 /*
2273 * we only have a single copy of the data, so don't bother with
2274 * all the retry and error correction code that follows. no
2275 * matter what the error is, it is very likely to persist.
2276 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002277 btrfs_debug(fs_info,
2278 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2279 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002280 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002281 }
2282
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002283 /*
2284 * there are two premises:
2285 * a) deliver good data to the caller
2286 * b) correct the bad sectors on disk
2287 */
Ming Leia0b60d72017-12-18 20:22:11 +08002288 if (failed_bio_pages > 1) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002289 /*
2290 * to fulfill b), we need to know the exact failing sectors, as
2291 * we don't want to rewrite any more than the failed ones. thus,
2292 * we need separate read requests for the failed bio
2293 *
2294 * if the following BUG_ON triggers, our validation request got
2295 * merged. we need separate requests for our algorithm to work.
2296 */
2297 BUG_ON(failrec->in_validation);
2298 failrec->in_validation = 1;
2299 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002300 } else {
2301 /*
2302 * we're ready to fulfill a) and b) alongside. get a good copy
2303 * of the failed sector and if we succeed, we have setup
2304 * everything for repair_io_failure to do the rest for us.
2305 */
2306 if (failrec->in_validation) {
2307 BUG_ON(failrec->this_mirror != failed_mirror);
2308 failrec->in_validation = 0;
2309 failrec->this_mirror = 0;
2310 }
2311 failrec->failed_mirror = failed_mirror;
2312 failrec->this_mirror++;
2313 if (failrec->this_mirror == failed_mirror)
2314 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002315 }
2316
Miao Xiefacc8a222013-07-25 19:22:34 +08002317 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002318 btrfs_debug(fs_info,
2319 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2320 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002321 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002322 }
2323
Liu Boc3cfb652017-07-13 15:00:50 -07002324 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002325}
2326
2327
2328struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2329 struct io_failure_record *failrec,
2330 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002331 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002332{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002333 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002334 struct bio *bio;
2335 struct btrfs_io_bio *btrfs_failed_bio;
2336 struct btrfs_io_bio *btrfs_bio;
2337
David Sterbac5e4c3d2017-06-12 17:29:41 +02002338 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002339 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002340 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002341 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002342 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002343 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002344
Miao Xiefacc8a222013-07-25 19:22:34 +08002345 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2346 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002347 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2348
2349 btrfs_bio = btrfs_io_bio(bio);
2350 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002351 icsum *= csum_size;
2352 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002353 csum_size);
2354 }
2355
Miao Xie2fe63032014-09-12 18:43:59 +08002356 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002357
Miao Xie2fe63032014-09-12 18:43:59 +08002358 return bio;
2359}
2360
2361/*
2362 * this is a generic handler for readpage errors (default
2363 * readpage_io_failed_hook). if other copies exist, read those and write back
2364 * good data to the failed position. does not investigate in remapping the
2365 * failed extent elsewhere, hoping the device will be smart enough to do this as
2366 * needed
2367 */
2368
2369static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2370 struct page *page, u64 start, u64 end,
2371 int failed_mirror)
2372{
2373 struct io_failure_record *failrec;
2374 struct inode *inode = page->mapping->host;
2375 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002376 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002377 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002378 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002379 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002380 int ret;
Ming Leia0b60d72017-12-18 20:22:11 +08002381 unsigned failed_bio_pages = bio_pages_all(failed_bio);
Miao Xie2fe63032014-09-12 18:43:59 +08002382
Mike Christie1f7ad752016-06-05 14:31:51 -05002383 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002384
2385 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2386 if (ret)
2387 return ret;
2388
Ming Leia0b60d72017-12-18 20:22:11 +08002389 if (!btrfs_check_repairable(inode, failed_bio_pages, failrec,
Liu Boc3cfb652017-07-13 15:00:50 -07002390 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002391 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002392 return -EIO;
2393 }
2394
Ming Leia0b60d72017-12-18 20:22:11 +08002395 if (failed_bio_pages > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002396 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002397
2398 phy_offset >>= inode->i_sb->s_blocksize_bits;
2399 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2400 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002401 (int)phy_offset, failed_bio->bi_end_io,
2402 NULL);
Mike Christie1f7ad752016-06-05 14:31:51 -05002403 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie2fe63032014-09-12 18:43:59 +08002404
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002405 btrfs_debug(btrfs_sb(inode->i_sb),
2406 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2407 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002408
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002409 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002410 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002411 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002412 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002413 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002414 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002415 }
2416
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002417 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002418}
2419
Chris Masond1310b22008-01-24 16:13:08 -05002420/* lots and lots of room for performance fixes in the end_bio funcs */
2421
David Sterbab5227c02015-12-03 13:08:59 +01002422void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002423{
2424 int uptodate = (err == 0);
2425 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002426 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002427
2428 tree = &BTRFS_I(page->mapping->host)->io_tree;
2429
David Sterbac3988d62017-02-17 15:18:32 +01002430 if (tree->ops && tree->ops->writepage_end_io_hook)
2431 tree->ops->writepage_end_io_hook(page, start, end, NULL,
2432 uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002433
Jeff Mahoney87826df2012-02-15 16:23:57 +01002434 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002435 ClearPageUptodate(page);
2436 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002437 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002438 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002439 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002440}
2441
Chris Masond1310b22008-01-24 16:13:08 -05002442/*
2443 * after a writepage IO is done, we need to:
2444 * clear the uptodate bits on error
2445 * clear the writeback bits in the extent tree for this IO
2446 * end_page_writeback if the page has no more pending IO
2447 *
2448 * Scheduling is not allowed, so the extent state tree is expected
2449 * to have one and only one object corresponding to this IO.
2450 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002451static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002452{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002453 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002454 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002455 u64 start;
2456 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002457 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002458
David Sterbac09abff2017-07-13 18:10:07 +02002459 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002460 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002461 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002462 struct inode *inode = page->mapping->host;
2463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002464
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002465 /* We always issue full-page reads, but if some block
2466 * in a page fails to read, blk_update_request() will
2467 * advance bv_offset and adjust bv_len to compensate.
2468 * Print a warning for nonzero offsets, and an error
2469 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002470 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2471 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002472 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002473 "partial page write in btrfs with offset %u and length %u",
2474 bvec->bv_offset, bvec->bv_len);
2475 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002477 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002478 bvec->bv_offset, bvec->bv_len);
2479 }
Chris Masond1310b22008-01-24 16:13:08 -05002480
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002481 start = page_offset(page);
2482 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002483
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002484 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002485 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002486 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002487
Chris Masond1310b22008-01-24 16:13:08 -05002488 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002489}
2490
Miao Xie883d0de2013-07-25 19:22:35 +08002491static void
2492endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2493 int uptodate)
2494{
2495 struct extent_state *cached = NULL;
2496 u64 end = start + len - 1;
2497
2498 if (uptodate && tree->track_uptodate)
2499 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
David Sterbad810a4b2017-12-07 18:52:54 +01002500 unlock_extent_cached_atomic(tree, start, end, &cached);
Miao Xie883d0de2013-07-25 19:22:35 +08002501}
2502
Chris Masond1310b22008-01-24 16:13:08 -05002503/*
2504 * after a readpage IO is done, we need to:
2505 * clear the uptodate bits on error
2506 * set the uptodate bits if things worked
2507 * set the page up to date if all extents in the tree are uptodate
2508 * clear the lock bit in the extent tree
2509 * unlock the page if there are no other extents locked for it
2510 *
2511 * Scheduling is not allowed, so the extent state tree is expected
2512 * to have one and only one object corresponding to this IO.
2513 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002514static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002515{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002516 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002517 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002518 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002519 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002520 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002521 u64 start;
2522 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002523 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002524 u64 extent_start = 0;
2525 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002526 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002527 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002528 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002529
David Sterbac09abff2017-07-13 18:10:07 +02002530 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002531 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002532 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002533 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002534 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002535
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002536 btrfs_debug(fs_info,
2537 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002538 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002539 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002540 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002541 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002542
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002543 /* We always issue full-page reads, but if some block
2544 * in a page fails to read, blk_update_request() will
2545 * advance bv_offset and adjust bv_len to compensate.
2546 * Print a warning for nonzero offsets, and an error
2547 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002548 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2549 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002550 btrfs_err(fs_info,
2551 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002552 bvec->bv_offset, bvec->bv_len);
2553 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002554 btrfs_info(fs_info,
2555 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002556 bvec->bv_offset, bvec->bv_len);
2557 }
Chris Masond1310b22008-01-24 16:13:08 -05002558
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002559 start = page_offset(page);
2560 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002561 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002562
Chris Mason9be33952013-05-17 18:30:14 -04002563 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002564 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002565 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2566 page, start, end,
2567 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002568 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002569 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002570 else
Josef Bacik7870d082017-05-05 11:57:15 -04002571 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2572 failure_tree, tree, start,
2573 page,
2574 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002575 }
Josef Bacikea466792012-03-26 21:57:36 -04002576
Miao Xief2a09da2013-07-25 19:22:33 +08002577 if (likely(uptodate))
2578 goto readpage_ok;
2579
David Sterba20a7db82017-02-17 16:24:29 +01002580 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002581 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002582 if (ret == -EAGAIN) {
2583 /*
2584 * Data inode's readpage_io_failed_hook() always
2585 * returns -EAGAIN.
2586 *
2587 * The generic bio_readpage_error handles errors
2588 * the following way: If possible, new read
2589 * requests are created and submitted and will
2590 * end up in end_bio_extent_readpage as well (if
2591 * we're lucky, not in the !uptodate case). In
2592 * that case it returns 0 and we just go on with
2593 * the next page in our bio. If it can't handle
2594 * the error it will return -EIO and we remain
2595 * responsible for that page.
2596 */
2597 ret = bio_readpage_error(bio, offset, page,
2598 start, end, mirror);
2599 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002600 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002601 offset += len;
2602 continue;
2603 }
Chris Mason7e383262008-04-09 16:28:12 -04002604 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002605
2606 /*
2607 * metadata's readpage_io_failed_hook() always returns
2608 * -EIO and fixes nothing. -EIO is also returned if
2609 * data inode error could not be fixed.
2610 */
2611 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002612 }
Miao Xief2a09da2013-07-25 19:22:33 +08002613readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002614 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002615 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002616 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002617 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002618
2619 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002620 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002621 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002622 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002623 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002624 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002625 ClearPageUptodate(page);
2626 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002627 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002628 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002629 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002630
2631 if (unlikely(!uptodate)) {
2632 if (extent_len) {
2633 endio_readpage_release_extent(tree,
2634 extent_start,
2635 extent_len, 1);
2636 extent_start = 0;
2637 extent_len = 0;
2638 }
2639 endio_readpage_release_extent(tree, start,
2640 end - start + 1, 0);
2641 } else if (!extent_len) {
2642 extent_start = start;
2643 extent_len = end + 1 - start;
2644 } else if (extent_start + extent_len == start) {
2645 extent_len += end + 1 - start;
2646 } else {
2647 endio_readpage_release_extent(tree, extent_start,
2648 extent_len, uptodate);
2649 extent_start = start;
2650 extent_len = end + 1 - start;
2651 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002652 }
Chris Masond1310b22008-01-24 16:13:08 -05002653
Miao Xie883d0de2013-07-25 19:22:35 +08002654 if (extent_len)
2655 endio_readpage_release_extent(tree, extent_start, extent_len,
2656 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002657 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002658 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002659 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002660}
2661
Chris Mason9be33952013-05-17 18:30:14 -04002662/*
David Sterba184f9992017-06-12 17:29:39 +02002663 * Initialize the members up to but not including 'bio'. Use after allocating a
2664 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2665 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002666 */
David Sterba184f9992017-06-12 17:29:39 +02002667static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002668{
David Sterba184f9992017-06-12 17:29:39 +02002669 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2670}
2671
2672/*
David Sterba6e707bc2017-06-02 17:26:26 +02002673 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2674 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2675 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002676 */
David Sterbac821e7f32017-06-02 18:35:36 +02002677struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002678{
2679 struct bio *bio;
2680
David Sterba9f2179a2017-06-02 17:55:44 +02002681 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002682 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002683 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002684 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002685 return bio;
2686}
2687
David Sterba8b6c1d52017-06-02 17:48:13 +02002688struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002689{
Miao Xie23ea8e52014-09-12 18:43:54 +08002690 struct btrfs_io_bio *btrfs_bio;
2691 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002692
David Sterba6e707bc2017-06-02 17:26:26 +02002693 /* Bio allocation backed by a bioset does not fail */
David Sterba8b6c1d52017-06-02 17:48:13 +02002694 new = bio_clone_fast(bio, GFP_NOFS, btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002695 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002696 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002697 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002698 return new;
2699}
Chris Mason9be33952013-05-17 18:30:14 -04002700
David Sterbac5e4c3d2017-06-12 17:29:41 +02002701struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002702{
Miao Xiefacc8a222013-07-25 19:22:34 +08002703 struct bio *bio;
2704
David Sterba6e707bc2017-06-02 17:26:26 +02002705 /* Bio allocation backed by a bioset does not fail */
David Sterbac5e4c3d2017-06-12 17:29:41 +02002706 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002707 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002708 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002709}
2710
Liu Boe4770942017-05-16 10:57:14 -07002711struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002712{
2713 struct bio *bio;
2714 struct btrfs_io_bio *btrfs_bio;
2715
2716 /* this will never fail when it's backed by a bioset */
Liu Boe4770942017-05-16 10:57:14 -07002717 bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002718 ASSERT(bio);
2719
2720 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002721 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002722
2723 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002724 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002725 return bio;
2726}
Chris Mason9be33952013-05-17 18:30:14 -04002727
Mike Christie1f7ad752016-06-05 14:31:51 -05002728static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2729 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002730{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002731 blk_status_t ret = 0;
Ming Leic45a8f22017-12-18 20:22:05 +08002732 struct bio_vec *bvec = bio_last_bvec_all(bio);
Chris Mason70dec802008-01-29 09:59:12 -05002733 struct page *page = bvec->bv_page;
2734 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002735 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002736
Miao Xie4eee4fa2012-12-21 09:17:45 +00002737 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002738
David Woodhouse902b22f2008-08-20 08:51:49 -04002739 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002740
David Sterba20c98012017-02-17 15:59:35 +01002741 if (tree->ops)
Josef Bacikc6100a42017-05-05 11:57:13 -04002742 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002743 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002744 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002745 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002746
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002747 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002748}
2749
David Sterba4b81ba42017-06-06 19:14:26 +02002750/*
2751 * @opf: bio REQ_OP_* and REQ_* flags as one value
David Sterbab8b3d622017-06-12 19:50:41 +02002752 * @tree: tree so we can call our merge_bio hook
2753 * @wbc: optional writeback control for io accounting
2754 * @page: page to add to the bio
2755 * @pg_offset: offset of the new bio or to check whether we are adding
2756 * a contiguous page to the previous one
2757 * @size: portion of page that we want to write
2758 * @offset: starting offset in the page
2759 * @bdev: attach newly created bios to this bdev
David Sterba5c2b1fd2017-06-06 19:22:55 +02002760 * @bio_ret: must be valid pointer, newly allocated bio will be stored there
David Sterbab8b3d622017-06-12 19:50:41 +02002761 * @end_io_func: end_io callback for new bio
2762 * @mirror_num: desired mirror to read/write
2763 * @prev_bio_flags: flags of previous bio to see if we can merge the current one
2764 * @bio_flags: flags of the current bio to see if we can merge them
David Sterba4b81ba42017-06-06 19:14:26 +02002765 */
2766static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002767 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002768 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002769 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002770 struct block_device *bdev,
2771 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002772 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002773 int mirror_num,
2774 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002775 unsigned long bio_flags,
2776 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002777{
2778 int ret = 0;
2779 struct bio *bio;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002780 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002781 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002782
David Sterba5c2b1fd2017-06-06 19:22:55 +02002783 ASSERT(bio_ret);
2784
2785 if (*bio_ret) {
David Sterba0c8508a2017-06-12 20:00:43 +02002786 bool contig;
2787 bool can_merge = true;
2788
Chris Masond1310b22008-01-24 16:13:08 -05002789 bio = *bio_ret;
David Sterba0c8508a2017-06-12 20:00:43 +02002790 if (prev_bio_flags & EXTENT_BIO_COMPRESSED)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002791 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002792 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002793 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002794
David Sterba0c8508a2017-06-12 20:00:43 +02002795 if (tree->ops && tree->ops->merge_bio_hook(page, offset,
2796 page_size, bio, bio_flags))
2797 can_merge = false;
2798
2799 if (prev_bio_flags != bio_flags || !contig || !can_merge ||
Filipe Manana005efed2015-09-14 09:09:31 +01002800 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002801 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002802 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002803 if (ret < 0) {
2804 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002805 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002806 }
Chris Masond1310b22008-01-24 16:13:08 -05002807 bio = NULL;
2808 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002809 if (wbc)
2810 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002811 return 0;
2812 }
2813 }
Chris Masonc8b97812008-10-29 14:49:59 -04002814
David Sterba6273b7f2017-10-04 17:30:11 +02002815 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002816 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002817 bio->bi_end_io = end_io_func;
2818 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002819 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002820 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002821 if (wbc) {
2822 wbc_init_bio(wbc, bio);
2823 wbc_account_io(wbc, page, page_size);
2824 }
Chris Mason70dec802008-01-29 09:59:12 -05002825
David Sterba5c2b1fd2017-06-06 19:22:55 +02002826 *bio_ret = bio;
Chris Masond1310b22008-01-24 16:13:08 -05002827
2828 return ret;
2829}
2830
Eric Sandeen48a3b632013-04-25 20:41:01 +00002831static void attach_extent_buffer_page(struct extent_buffer *eb,
2832 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002833{
2834 if (!PagePrivate(page)) {
2835 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002836 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002837 set_page_private(page, (unsigned long)eb);
2838 } else {
2839 WARN_ON(page->private != (unsigned long)eb);
2840 }
2841}
2842
Chris Masond1310b22008-01-24 16:13:08 -05002843void set_page_extent_mapped(struct page *page)
2844{
2845 if (!PagePrivate(page)) {
2846 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002847 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002848 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002849 }
2850}
2851
Miao Xie125bac012013-07-25 19:22:37 +08002852static struct extent_map *
2853__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2854 u64 start, u64 len, get_extent_t *get_extent,
2855 struct extent_map **em_cached)
2856{
2857 struct extent_map *em;
2858
2859 if (em_cached && *em_cached) {
2860 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002861 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002862 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002863 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002864 return em;
2865 }
2866
2867 free_extent_map(em);
2868 *em_cached = NULL;
2869 }
2870
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002871 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002872 if (em_cached && !IS_ERR_OR_NULL(em)) {
2873 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002874 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002875 *em_cached = em;
2876 }
2877 return em;
2878}
Chris Masond1310b22008-01-24 16:13:08 -05002879/*
2880 * basic readpage implementation. Locked extent state structs are inserted
2881 * into the tree that are removed when the IO is done (by the end_io
2882 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002883 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002884 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002885 */
Miao Xie99740902013-07-25 19:22:36 +08002886static int __do_readpage(struct extent_io_tree *tree,
2887 struct page *page,
2888 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002889 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002890 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002891 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002892 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002893{
2894 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002895 u64 start = page_offset(page);
David Sterba8eec8292017-06-06 19:50:13 +02002896 const u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002897 u64 cur = start;
2898 u64 extent_offset;
2899 u64 last_byte = i_size_read(inode);
2900 u64 block_start;
2901 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002902 struct extent_map *em;
2903 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002904 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002905 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002906 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002907 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002908 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002909 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002910 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002911
2912 set_page_extent_mapped(page);
2913
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002914 if (!PageUptodate(page)) {
2915 if (cleancache_get_page(page) == 0) {
2916 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002917 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002918 goto out;
2919 }
2920 }
2921
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002922 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002923 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002924 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002925
2926 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002927 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002928 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002929 memset(userpage + zero_offset, 0, iosize);
2930 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002931 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002932 }
2933 }
Chris Masond1310b22008-01-24 16:13:08 -05002934 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002935 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002936 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002937
Chris Masond1310b22008-01-24 16:13:08 -05002938 if (cur >= last_byte) {
2939 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002940 struct extent_state *cached = NULL;
2941
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002942 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002943 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002944 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002945 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002946 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002947 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002948 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002949 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01002950 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05002951 break;
2952 }
Miao Xie125bac012013-07-25 19:22:37 +08002953 em = __get_extent_map(inode, page, pg_offset, cur,
2954 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002955 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002956 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002957 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002958 break;
2959 }
Chris Masond1310b22008-01-24 16:13:08 -05002960 extent_offset = cur - em->start;
2961 BUG_ON(extent_map_end(em) <= cur);
2962 BUG_ON(end < cur);
2963
Li Zefan261507a02010-12-17 14:21:50 +08002964 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002965 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002966 extent_set_compress_type(&this_bio_flag,
2967 em->compress_type);
2968 }
Chris Masonc8b97812008-10-29 14:49:59 -04002969
Chris Masond1310b22008-01-24 16:13:08 -05002970 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2971 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002972 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002973 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2974 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002975 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002976 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002977 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002978 disk_io_size = iosize;
2979 }
Chris Masond1310b22008-01-24 16:13:08 -05002980 bdev = em->bdev;
2981 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002982 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2983 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002984
2985 /*
2986 * If we have a file range that points to a compressed extent
2987 * and it's followed by a consecutive file range that points to
2988 * to the same compressed extent (possibly with a different
2989 * offset and/or length, so it either points to the whole extent
2990 * or only part of it), we must make sure we do not submit a
2991 * single bio to populate the pages for the 2 ranges because
2992 * this makes the compressed extent read zero out the pages
2993 * belonging to the 2nd range. Imagine the following scenario:
2994 *
2995 * File layout
2996 * [0 - 8K] [8K - 24K]
2997 * | |
2998 * | |
2999 * points to extent X, points to extent X,
3000 * offset 4K, length of 8K offset 0, length 16K
3001 *
3002 * [extent X, compressed length = 4K uncompressed length = 16K]
3003 *
3004 * If the bio to read the compressed extent covers both ranges,
3005 * it will decompress extent X into the pages belonging to the
3006 * first range and then it will stop, zeroing out the remaining
3007 * pages that belong to the other range that points to extent X.
3008 * So here we make sure we submit 2 bios, one for the first
3009 * range and another one for the third range. Both will target
3010 * the same physical extent from disk, but we can't currently
3011 * make the compressed bio endio callback populate the pages
3012 * for both ranges because each compressed bio is tightly
3013 * coupled with a single extent map, and each range can have
3014 * an extent map with a different offset value relative to the
3015 * uncompressed data of our extent and different lengths. This
3016 * is a corner case so we prioritize correctness over
3017 * non-optimal behavior (submitting 2 bios for the same extent).
3018 */
3019 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3020 prev_em_start && *prev_em_start != (u64)-1 &&
3021 *prev_em_start != em->orig_start)
3022 force_bio_submit = true;
3023
3024 if (prev_em_start)
3025 *prev_em_start = em->orig_start;
3026
Chris Masond1310b22008-01-24 16:13:08 -05003027 free_extent_map(em);
3028 em = NULL;
3029
3030 /* we've found a hole, just zero and go on */
3031 if (block_start == EXTENT_MAP_HOLE) {
3032 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003033 struct extent_state *cached = NULL;
3034
Cong Wang7ac687d2011-11-25 23:14:28 +08003035 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003036 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003037 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003038 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003039
3040 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003041 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003042 unlock_extent_cached(tree, cur,
David Sterbae43bbe52017-12-12 21:43:52 +01003043 cur + iosize - 1, &cached);
Chris Masond1310b22008-01-24 16:13:08 -05003044 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003045 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003046 continue;
3047 }
3048 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003049 if (test_range_bit(tree, cur, cur_end,
3050 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003051 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003052 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003053 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003054 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003055 continue;
3056 }
Chris Mason70dec802008-01-29 09:59:12 -05003057 /* we have an inline extent but it didn't get marked up
3058 * to date. Error out
3059 */
3060 if (block_start == EXTENT_MAP_INLINE) {
3061 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003062 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003063 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003064 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003065 continue;
3066 }
Chris Masond1310b22008-01-24 16:13:08 -05003067
David Sterba4b81ba42017-06-06 19:14:26 +02003068 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003069 page, offset, disk_io_size,
3070 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003071 end_bio_extent_readpage, mirror_num,
3072 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003073 this_bio_flag,
3074 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003075 if (!ret) {
3076 nr++;
3077 *bio_flags = this_bio_flag;
3078 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003079 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003080 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003081 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003082 }
Chris Masond1310b22008-01-24 16:13:08 -05003083 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003084 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003085 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003086out:
Chris Masond1310b22008-01-24 16:13:08 -05003087 if (!nr) {
3088 if (!PageError(page))
3089 SetPageUptodate(page);
3090 unlock_page(page);
3091 }
Liu Bobaf863b2016-07-11 10:39:07 -07003092 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003093}
3094
Miao Xie99740902013-07-25 19:22:36 +08003095static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3096 struct page *pages[], int nr_pages,
3097 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003098 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003099 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003100 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003101 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003102{
3103 struct inode *inode;
3104 struct btrfs_ordered_extent *ordered;
3105 int index;
3106
3107 inode = pages[0]->mapping->host;
3108 while (1) {
3109 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003110 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003111 end - start + 1);
3112 if (!ordered)
3113 break;
3114 unlock_extent(tree, start, end);
3115 btrfs_start_ordered_extent(inode, ordered, 1);
3116 btrfs_put_ordered_extent(ordered);
3117 }
3118
3119 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003120 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
3121 bio, 0, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003122 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003123 }
3124}
3125
3126static void __extent_readpages(struct extent_io_tree *tree,
3127 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003128 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003129 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003130 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003131 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003132{
Stefan Behrens35a36212013-08-14 18:12:25 +02003133 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003134 u64 end = 0;
3135 u64 page_start;
3136 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003137 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003138
3139 for (index = 0; index < nr_pages; index++) {
3140 page_start = page_offset(pages[index]);
3141 if (!end) {
3142 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003143 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003144 first_index = index;
3145 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003146 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003147 } else {
3148 __do_contiguous_readpages(tree, &pages[first_index],
3149 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003150 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003151 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003152 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003153 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003154 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003155 first_index = index;
3156 }
3157 }
3158
3159 if (end)
3160 __do_contiguous_readpages(tree, &pages[first_index],
3161 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003162 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003163 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003164}
3165
3166static int __extent_read_full_page(struct extent_io_tree *tree,
3167 struct page *page,
3168 get_extent_t *get_extent,
3169 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003170 unsigned long *bio_flags,
3171 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003172{
3173 struct inode *inode = page->mapping->host;
3174 struct btrfs_ordered_extent *ordered;
3175 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003176 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003177 int ret;
3178
3179 while (1) {
3180 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003181 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003182 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003183 if (!ordered)
3184 break;
3185 unlock_extent(tree, start, end);
3186 btrfs_start_ordered_extent(inode, ordered, 1);
3187 btrfs_put_ordered_extent(ordered);
3188 }
3189
Miao Xie125bac012013-07-25 19:22:37 +08003190 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003191 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003192 return ret;
3193}
3194
Chris Masond1310b22008-01-24 16:13:08 -05003195int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003196 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003197{
3198 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003199 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003200 int ret;
3201
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003202 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003203 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003204 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003205 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003206 return ret;
3207}
Chris Masond1310b22008-01-24 16:13:08 -05003208
David Sterba3d4b9492017-02-10 19:33:41 +01003209static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003210 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003211{
3212 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003213}
3214
Chris Masond1310b22008-01-24 16:13:08 -05003215/*
Chris Mason40f76582014-05-21 13:35:51 -07003216 * helper for __extent_writepage, doing all of the delayed allocation setup.
3217 *
3218 * This returns 1 if our fill_delalloc function did all the work required
3219 * to write the page (copy into inline extent). In this case the IO has
3220 * been started and the page is already unlocked.
3221 *
3222 * This returns 0 if all went well (page still locked)
3223 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003224 */
Chris Mason40f76582014-05-21 13:35:51 -07003225static noinline_for_stack int writepage_delalloc(struct inode *inode,
3226 struct page *page, struct writeback_control *wbc,
3227 struct extent_page_data *epd,
3228 u64 delalloc_start,
3229 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003230{
Chris Mason40f76582014-05-21 13:35:51 -07003231 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003232 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003233 u64 nr_delalloc;
3234 u64 delalloc_to_write = 0;
3235 u64 delalloc_end = 0;
3236 int ret;
3237 int page_started = 0;
3238
3239 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3240 return 0;
3241
3242 while (delalloc_end < page_end) {
3243 nr_delalloc = find_lock_delalloc_range(inode, tree,
3244 page,
3245 &delalloc_start,
3246 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003247 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003248 if (nr_delalloc == 0) {
3249 delalloc_start = delalloc_end + 1;
3250 continue;
3251 }
3252 ret = tree->ops->fill_delalloc(inode, page,
3253 delalloc_start,
3254 delalloc_end,
3255 &page_started,
Liu Bof82b7352017-10-23 23:18:16 -06003256 nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003257 /* File system has been set read-only */
3258 if (ret) {
3259 SetPageError(page);
3260 /* fill_delalloc should be return < 0 for error
3261 * but just in case, we use > 0 here meaning the
3262 * IO is started, so we don't want to return > 0
3263 * unless things are going well.
3264 */
3265 ret = ret < 0 ? ret : -EIO;
3266 goto done;
3267 }
3268 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003269 * delalloc_end is already one less than the total length, so
3270 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003271 */
3272 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003273 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003274 delalloc_start = delalloc_end + 1;
3275 }
3276 if (wbc->nr_to_write < delalloc_to_write) {
3277 int thresh = 8192;
3278
3279 if (delalloc_to_write < thresh * 2)
3280 thresh = delalloc_to_write;
3281 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3282 thresh);
3283 }
3284
3285 /* did the fill delalloc function already unlock and start
3286 * the IO?
3287 */
3288 if (page_started) {
3289 /*
3290 * we've unlocked the page, so we can't update
3291 * the mapping's writeback index, just update
3292 * nr_to_write.
3293 */
3294 wbc->nr_to_write -= *nr_written;
3295 return 1;
3296 }
3297
3298 ret = 0;
3299
3300done:
3301 return ret;
3302}
3303
3304/*
3305 * helper for __extent_writepage. This calls the writepage start hooks,
3306 * and does the loop to map the page into extents and bios.
3307 *
3308 * We return 1 if the IO is started and the page is unlocked,
3309 * 0 if all went well (page still locked)
3310 * < 0 if there were errors (page still locked)
3311 */
3312static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3313 struct page *page,
3314 struct writeback_control *wbc,
3315 struct extent_page_data *epd,
3316 loff_t i_size,
3317 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003318 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003319{
Chris Masond1310b22008-01-24 16:13:08 -05003320 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003321 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003322 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003323 u64 end;
3324 u64 cur = start;
3325 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003326 u64 block_start;
3327 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003328 struct extent_map *em;
3329 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003330 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003331 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003332 int ret = 0;
3333 int nr = 0;
3334 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003335
Chris Mason247e7432008-07-17 12:53:51 -04003336 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003337 ret = tree->ops->writepage_start_hook(page, start,
3338 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003339 if (ret) {
3340 /* Fixup worker will requeue */
3341 if (ret == -EBUSY)
3342 wbc->pages_skipped++;
3343 else
3344 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003345
David Sterba3d4b9492017-02-10 19:33:41 +01003346 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003347 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003348 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003349 }
3350 }
3351
Chris Mason11c83492009-04-20 15:50:09 -04003352 /*
3353 * we don't want to touch the inode after unlocking the page,
3354 * so we update the mapping writeback index now
3355 */
David Sterba3d4b9492017-02-10 19:33:41 +01003356 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003357
Chris Masond1310b22008-01-24 16:13:08 -05003358 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003359 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003360 if (tree->ops && tree->ops->writepage_end_io_hook)
3361 tree->ops->writepage_end_io_hook(page, start,
3362 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003363 goto done;
3364 }
3365
Chris Masond1310b22008-01-24 16:13:08 -05003366 blocksize = inode->i_sb->s_blocksize;
3367
3368 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003369 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003370 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003371
Chris Mason40f76582014-05-21 13:35:51 -07003372 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003373 if (tree->ops && tree->ops->writepage_end_io_hook)
3374 tree->ops->writepage_end_io_hook(page, cur,
3375 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003376 break;
3377 }
David Sterba3c98c622017-06-23 04:01:08 +02003378 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003379 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003380 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003381 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003382 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003383 break;
3384 }
3385
3386 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003387 em_end = extent_map_end(em);
3388 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003389 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003390 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003391 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003392 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003393 bdev = em->bdev;
3394 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003395 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003396 free_extent_map(em);
3397 em = NULL;
3398
Chris Masonc8b97812008-10-29 14:49:59 -04003399 /*
3400 * compressed and inline extents are written through other
3401 * paths in the FS
3402 */
3403 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003404 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003405 /*
3406 * end_io notification does not happen here for
3407 * compressed extents
3408 */
3409 if (!compressed && tree->ops &&
3410 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003411 tree->ops->writepage_end_io_hook(page, cur,
3412 cur + iosize - 1,
3413 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003414 else if (compressed) {
3415 /* we don't want to end_page_writeback on
3416 * a compressed extent. this happens
3417 * elsewhere
3418 */
3419 nr++;
3420 }
3421
3422 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003423 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003424 continue;
3425 }
Chris Masonc8b97812008-10-29 14:49:59 -04003426
David Sterba58409ed2016-05-04 11:46:10 +02003427 set_range_writeback(tree, cur, cur + iosize - 1);
3428 if (!PageWriteback(page)) {
3429 btrfs_err(BTRFS_I(inode)->root->fs_info,
3430 "page %lu not writeback, cur %llu end %llu",
3431 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003432 }
David Sterba58409ed2016-05-04 11:46:10 +02003433
David Sterba4b81ba42017-06-06 19:14:26 +02003434 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003435 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003436 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003437 end_bio_extent_writepage,
3438 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003439 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003440 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003441 if (PageWriteback(page))
3442 end_page_writeback(page);
3443 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003444
Chris Masond1310b22008-01-24 16:13:08 -05003445 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003446 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003447 nr++;
3448 }
3449done:
Chris Mason40f76582014-05-21 13:35:51 -07003450 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003451 return ret;
3452}
3453
3454/*
3455 * the writepage semantics are similar to regular writepage. extent
3456 * records are inserted to lock ranges in the tree, and as dirty areas
3457 * are found, they are marked writeback. Then the lock bits are removed
3458 * and the end_io handler clears the writeback ranges
3459 */
3460static int __extent_writepage(struct page *page, struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003461 struct extent_page_data *epd)
Chris Mason40f76582014-05-21 13:35:51 -07003462{
3463 struct inode *inode = page->mapping->host;
Chris Mason40f76582014-05-21 13:35:51 -07003464 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003465 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003466 int ret;
3467 int nr = 0;
3468 size_t pg_offset = 0;
3469 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003470 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003471 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003472 unsigned long nr_written = 0;
3473
Liu Boff40adf2017-08-24 18:19:48 -06003474 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003475
3476 trace___extent_writepage(page, inode, wbc);
3477
3478 WARN_ON(!PageLocked(page));
3479
3480 ClearPageError(page);
3481
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003482 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003483 if (page->index > end_index ||
3484 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003485 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003486 unlock_page(page);
3487 return 0;
3488 }
3489
3490 if (page->index == end_index) {
3491 char *userpage;
3492
3493 userpage = kmap_atomic(page);
3494 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003495 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003496 kunmap_atomic(userpage);
3497 flush_dcache_page(page);
3498 }
3499
3500 pg_offset = 0;
3501
3502 set_page_extent_mapped(page);
3503
3504 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3505 if (ret == 1)
3506 goto done_unlocked;
3507 if (ret)
3508 goto done;
3509
3510 ret = __extent_writepage_io(inode, page, wbc, epd,
3511 i_size, nr_written, write_flags, &nr);
3512 if (ret == 1)
3513 goto done_unlocked;
3514
3515done:
Chris Masond1310b22008-01-24 16:13:08 -05003516 if (nr == 0) {
3517 /* make sure the mapping tag for page dirty gets cleared */
3518 set_page_writeback(page);
3519 end_page_writeback(page);
3520 }
Filipe Manana61391d52014-05-09 17:17:40 +01003521 if (PageError(page)) {
3522 ret = ret < 0 ? ret : -EIO;
3523 end_extent_writepage(page, ret, start, page_end);
3524 }
Chris Masond1310b22008-01-24 16:13:08 -05003525 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003526 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003527
Chris Mason11c83492009-04-20 15:50:09 -04003528done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003529 return 0;
3530}
3531
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003532void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003533{
NeilBrown74316202014-07-07 15:16:04 +10003534 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3535 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003536}
3537
Chris Mason0e378df2014-05-19 20:55:27 -07003538static noinline_for_stack int
3539lock_extent_buffer_for_io(struct extent_buffer *eb,
3540 struct btrfs_fs_info *fs_info,
3541 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003542{
3543 unsigned long i, num_pages;
3544 int flush = 0;
3545 int ret = 0;
3546
3547 if (!btrfs_try_tree_write_lock(eb)) {
3548 flush = 1;
3549 flush_write_bio(epd);
3550 btrfs_tree_lock(eb);
3551 }
3552
3553 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3554 btrfs_tree_unlock(eb);
3555 if (!epd->sync_io)
3556 return 0;
3557 if (!flush) {
3558 flush_write_bio(epd);
3559 flush = 1;
3560 }
Chris Masona098d8e82012-03-21 12:09:56 -04003561 while (1) {
3562 wait_on_extent_buffer_writeback(eb);
3563 btrfs_tree_lock(eb);
3564 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3565 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003566 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003567 }
3568 }
3569
Josef Bacik51561ff2012-07-20 16:25:24 -04003570 /*
3571 * We need to do this to prevent races in people who check if the eb is
3572 * under IO since we can end up having no IO bits set for a short period
3573 * of time.
3574 */
3575 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003576 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3577 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003578 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003579 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003580 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3581 -eb->len,
3582 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003583 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003584 } else {
3585 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003586 }
3587
3588 btrfs_tree_unlock(eb);
3589
3590 if (!ret)
3591 return ret;
3592
3593 num_pages = num_extent_pages(eb->start, eb->len);
3594 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003595 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003596
3597 if (!trylock_page(p)) {
3598 if (!flush) {
3599 flush_write_bio(epd);
3600 flush = 1;
3601 }
3602 lock_page(p);
3603 }
3604 }
3605
3606 return ret;
3607}
3608
3609static void end_extent_buffer_writeback(struct extent_buffer *eb)
3610{
3611 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003612 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003613 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3614}
3615
Filipe Manana656f30d2014-09-26 12:25:56 +01003616static void set_btree_ioerr(struct page *page)
3617{
3618 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003619
3620 SetPageError(page);
3621 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3622 return;
3623
3624 /*
3625 * If writeback for a btree extent that doesn't belong to a log tree
3626 * failed, increment the counter transaction->eb_write_errors.
3627 * We do this because while the transaction is running and before it's
3628 * committing (when we call filemap_fdata[write|wait]_range against
3629 * the btree inode), we might have
3630 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3631 * returns an error or an error happens during writeback, when we're
3632 * committing the transaction we wouldn't know about it, since the pages
3633 * can be no longer dirty nor marked anymore for writeback (if a
3634 * subsequent modification to the extent buffer didn't happen before the
3635 * transaction commit), which makes filemap_fdata[write|wait]_range not
3636 * able to find the pages tagged with SetPageError at transaction
3637 * commit time. So if this happens we must abort the transaction,
3638 * otherwise we commit a super block with btree roots that point to
3639 * btree nodes/leafs whose content on disk is invalid - either garbage
3640 * or the content of some node/leaf from a past generation that got
3641 * cowed or deleted and is no longer valid.
3642 *
3643 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3644 * not be enough - we need to distinguish between log tree extents vs
3645 * non-log tree extents, and the next filemap_fdatawait_range() call
3646 * will catch and clear such errors in the mapping - and that call might
3647 * be from a log sync and not from a transaction commit. Also, checking
3648 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3649 * not done and would not be reliable - the eb might have been released
3650 * from memory and reading it back again means that flag would not be
3651 * set (since it's a runtime flag, not persisted on disk).
3652 *
3653 * Using the flags below in the btree inode also makes us achieve the
3654 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3655 * writeback for all dirty pages and before filemap_fdatawait_range()
3656 * is called, the writeback for all dirty pages had already finished
3657 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3658 * filemap_fdatawait_range() would return success, as it could not know
3659 * that writeback errors happened (the pages were no longer tagged for
3660 * writeback).
3661 */
3662 switch (eb->log_index) {
3663 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003664 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003665 break;
3666 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003667 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003668 break;
3669 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003670 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003671 break;
3672 default:
3673 BUG(); /* unexpected, logic error */
3674 }
3675}
3676
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003677static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003678{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003679 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003680 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003681 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682
David Sterbac09abff2017-07-13 18:10:07 +02003683 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003684 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003685 struct page *page = bvec->bv_page;
3686
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003687 eb = (struct extent_buffer *)page->private;
3688 BUG_ON(!eb);
3689 done = atomic_dec_and_test(&eb->io_pages);
3690
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003691 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003692 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003693 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003694 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003695 }
3696
3697 end_page_writeback(page);
3698
3699 if (!done)
3700 continue;
3701
3702 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003703 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003704
3705 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706}
3707
Chris Mason0e378df2014-05-19 20:55:27 -07003708static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003709 struct btrfs_fs_info *fs_info,
3710 struct writeback_control *wbc,
3711 struct extent_page_data *epd)
3712{
3713 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003714 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003715 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003716 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717 unsigned long i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003718 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003719 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003720 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003721
Filipe Manana656f30d2014-09-26 12:25:56 +01003722 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003723 num_pages = num_extent_pages(eb->start, eb->len);
3724 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003725
Liu Bo851cd172016-09-23 13:44:44 -07003726 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3727 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003728 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003729 end = btrfs_node_key_ptr_offset(nritems);
3730
David Sterbab159fa22016-11-08 18:09:03 +01003731 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003732 } else {
3733 /*
3734 * leaf:
3735 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3736 */
3737 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003738 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003739 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003740 }
3741
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003742 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003743 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003744
3745 clear_page_dirty_for_io(p);
3746 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003747 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003748 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003749 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003750 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003751 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003752 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003753 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003754 if (PageWriteback(p))
3755 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003756 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3757 end_extent_buffer_writeback(eb);
3758 ret = -EIO;
3759 break;
3760 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003761 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003762 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 unlock_page(p);
3764 }
3765
3766 if (unlikely(ret)) {
3767 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003768 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003769 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003770 unlock_page(p);
3771 }
3772 }
3773
3774 return ret;
3775}
3776
3777int btree_write_cache_pages(struct address_space *mapping,
3778 struct writeback_control *wbc)
3779{
3780 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3781 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3782 struct extent_buffer *eb, *prev_eb = NULL;
3783 struct extent_page_data epd = {
3784 .bio = NULL,
3785 .tree = tree,
3786 .extent_locked = 0,
3787 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3788 };
3789 int ret = 0;
3790 int done = 0;
3791 int nr_to_write_done = 0;
3792 struct pagevec pvec;
3793 int nr_pages;
3794 pgoff_t index;
3795 pgoff_t end; /* Inclusive */
3796 int scanned = 0;
3797 int tag;
3798
Mel Gorman86679822017-11-15 17:37:52 -08003799 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003800 if (wbc->range_cyclic) {
3801 index = mapping->writeback_index; /* Start from prev offset */
3802 end = -1;
3803 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003804 index = wbc->range_start >> PAGE_SHIFT;
3805 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003806 scanned = 1;
3807 }
3808 if (wbc->sync_mode == WB_SYNC_ALL)
3809 tag = PAGECACHE_TAG_TOWRITE;
3810 else
3811 tag = PAGECACHE_TAG_DIRTY;
3812retry:
3813 if (wbc->sync_mode == WB_SYNC_ALL)
3814 tag_pages_for_writeback(mapping, index, end);
3815 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003816 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003817 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003818 unsigned i;
3819
3820 scanned = 1;
3821 for (i = 0; i < nr_pages; i++) {
3822 struct page *page = pvec.pages[i];
3823
3824 if (!PagePrivate(page))
3825 continue;
3826
Josef Bacikb5bae262012-09-14 13:43:01 -04003827 spin_lock(&mapping->private_lock);
3828 if (!PagePrivate(page)) {
3829 spin_unlock(&mapping->private_lock);
3830 continue;
3831 }
3832
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003833 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003834
3835 /*
3836 * Shouldn't happen and normally this would be a BUG_ON
3837 * but no sense in crashing the users box for something
3838 * we can survive anyway.
3839 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303840 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003841 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003842 continue;
3843 }
3844
Josef Bacikb5bae262012-09-14 13:43:01 -04003845 if (eb == prev_eb) {
3846 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003847 continue;
3848 }
3849
Josef Bacikb5bae262012-09-14 13:43:01 -04003850 ret = atomic_inc_not_zero(&eb->refs);
3851 spin_unlock(&mapping->private_lock);
3852 if (!ret)
3853 continue;
3854
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003855 prev_eb = eb;
3856 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3857 if (!ret) {
3858 free_extent_buffer(eb);
3859 continue;
3860 }
3861
3862 ret = write_one_eb(eb, fs_info, wbc, &epd);
3863 if (ret) {
3864 done = 1;
3865 free_extent_buffer(eb);
3866 break;
3867 }
3868 free_extent_buffer(eb);
3869
3870 /*
3871 * the filesystem may choose to bump up nr_to_write.
3872 * We have to make sure to honor the new nr_to_write
3873 * at any time
3874 */
3875 nr_to_write_done = wbc->nr_to_write <= 0;
3876 }
3877 pagevec_release(&pvec);
3878 cond_resched();
3879 }
3880 if (!scanned && !done) {
3881 /*
3882 * We hit the last page and there is more work to be done: wrap
3883 * back to the start of the file
3884 */
3885 scanned = 1;
3886 index = 0;
3887 goto retry;
3888 }
3889 flush_write_bio(&epd);
3890 return ret;
3891}
3892
Chris Masond1310b22008-01-24 16:13:08 -05003893/**
Chris Mason4bef0842008-09-08 11:18:08 -04003894 * 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 -05003895 * @mapping: address space structure to write
3896 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
David Sterba935db852017-06-23 04:30:28 +02003897 * @data: data passed to __extent_writepage function
Chris Masond1310b22008-01-24 16:13:08 -05003898 *
3899 * If a page is already under I/O, write_cache_pages() skips it, even
3900 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3901 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3902 * and msync() need to guarantee that all the data which was dirty at the time
3903 * the call was made get new I/O started against them. If wbc->sync_mode is
3904 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3905 * existing IO to complete.
3906 */
David Sterba4242b642017-02-10 19:38:24 +01003907static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003908 struct writeback_control *wbc,
David Sterbaaab6e9e2017-11-30 18:00:02 +01003909 struct extent_page_data *epd)
Chris Masond1310b22008-01-24 16:13:08 -05003910{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003911 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003912 int ret = 0;
3913 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003914 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003915 struct pagevec pvec;
3916 int nr_pages;
3917 pgoff_t index;
3918 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003919 pgoff_t done_index;
3920 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003921 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003922 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003923
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003924 /*
3925 * We have to hold onto the inode so that ordered extents can do their
3926 * work when the IO finishes. The alternative to this is failing to add
3927 * an ordered extent if the igrab() fails there and that is a huge pain
3928 * to deal with, so instead just hold onto the inode throughout the
3929 * writepages operation. If it fails here we are freeing up the inode
3930 * anyway and we'd rather not waste our time writing out stuff that is
3931 * going to be truncated anyway.
3932 */
3933 if (!igrab(inode))
3934 return 0;
3935
Mel Gorman86679822017-11-15 17:37:52 -08003936 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003937 if (wbc->range_cyclic) {
3938 index = mapping->writeback_index; /* Start from prev offset */
3939 end = -1;
3940 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003941 index = wbc->range_start >> PAGE_SHIFT;
3942 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003943 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3944 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003945 scanned = 1;
3946 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003947 if (wbc->sync_mode == WB_SYNC_ALL)
3948 tag = PAGECACHE_TAG_TOWRITE;
3949 else
3950 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003951retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003952 if (wbc->sync_mode == WB_SYNC_ALL)
3953 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003954 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003955 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003956 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3957 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003958 unsigned i;
3959
3960 scanned = 1;
3961 for (i = 0; i < nr_pages; i++) {
3962 struct page *page = pvec.pages[i];
3963
Liu Boa91326672016-03-07 16:56:21 -08003964 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003965 /*
3966 * At this point we hold neither mapping->tree_lock nor
3967 * lock on the page itself: the page may be truncated or
3968 * invalidated (changing page->mapping to NULL), or even
3969 * swizzled back from swapper_space to tmpfs file
3970 * mapping
3971 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003972 if (!trylock_page(page)) {
David Sterbaaab6e9e2017-11-30 18:00:02 +01003973 flush_write_bio(epd);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003974 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003975 }
Chris Masond1310b22008-01-24 16:13:08 -05003976
3977 if (unlikely(page->mapping != mapping)) {
3978 unlock_page(page);
3979 continue;
3980 }
3981
Chris Masond2c3f4f2008-11-19 12:44:22 -05003982 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003983 if (PageWriteback(page))
David Sterbaaab6e9e2017-11-30 18:00:02 +01003984 flush_write_bio(epd);
Chris Masond1310b22008-01-24 16:13:08 -05003985 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003986 }
Chris Masond1310b22008-01-24 16:13:08 -05003987
3988 if (PageWriteback(page) ||
3989 !clear_page_dirty_for_io(page)) {
3990 unlock_page(page);
3991 continue;
3992 }
3993
David Sterbaaab6e9e2017-11-30 18:00:02 +01003994 ret = __extent_writepage(page, wbc, epd);
Chris Masond1310b22008-01-24 16:13:08 -05003995
3996 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3997 unlock_page(page);
3998 ret = 0;
3999 }
Liu Boa91326672016-03-07 16:56:21 -08004000 if (ret < 0) {
4001 /*
4002 * done_index is set past this page,
4003 * so media errors will not choke
4004 * background writeout for the entire
4005 * file. This has consequences for
4006 * range_cyclic semantics (ie. it may
4007 * not be suitable for data integrity
4008 * writeout).
4009 */
4010 done_index = page->index + 1;
4011 done = 1;
4012 break;
4013 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004014
4015 /*
4016 * the filesystem may choose to bump up nr_to_write.
4017 * We have to make sure to honor the new nr_to_write
4018 * at any time
4019 */
4020 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004021 }
4022 pagevec_release(&pvec);
4023 cond_resched();
4024 }
Liu Bo894b36e2016-03-07 16:56:22 -08004025 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004026 /*
4027 * We hit the last page and there is more work to be done: wrap
4028 * back to the start of the file
4029 */
4030 scanned = 1;
4031 index = 0;
4032 goto retry;
4033 }
Liu Boa91326672016-03-07 16:56:21 -08004034
4035 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4036 mapping->writeback_index = done_index;
4037
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004038 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004039 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004040}
Chris Masond1310b22008-01-24 16:13:08 -05004041
David Sterbaaab6e9e2017-11-30 18:00:02 +01004042static void flush_write_bio(struct extent_page_data *epd)
Chris Masonffbd5172009-04-20 15:50:09 -04004043{
4044 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004045 int ret;
4046
Liu Bo18fdc672017-09-13 12:18:22 -06004047 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004048 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004049 epd->bio = NULL;
4050 }
4051}
4052
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004053int extent_write_full_page(struct page *page, struct writeback_control *wbc)
Chris Masond1310b22008-01-24 16:13:08 -05004054{
4055 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004056 struct extent_page_data epd = {
4057 .bio = NULL,
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02004058 .tree = &BTRFS_I(page->mapping->host)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -05004059 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004060 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004061 };
Chris Masond1310b22008-01-24 16:13:08 -05004062
Chris Masond1310b22008-01-24 16:13:08 -05004063 ret = __extent_writepage(page, wbc, &epd);
4064
David Sterbae2932ee2017-06-23 04:16:17 +02004065 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004066 return ret;
4067}
Chris Masond1310b22008-01-24 16:13:08 -05004068
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004069int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
Chris Mason771ed682008-11-06 22:02:51 -05004070 int mode)
4071{
4072 int ret = 0;
4073 struct address_space *mapping = inode->i_mapping;
Nikolay Borisov5e3ee232017-12-08 15:55:58 +02004074 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Mason771ed682008-11-06 22:02:51 -05004075 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004076 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4077 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004078
4079 struct extent_page_data epd = {
4080 .bio = NULL,
4081 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004082 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004083 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004084 };
4085 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004086 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004087 .nr_to_write = nr_pages * 2,
4088 .range_start = start,
4089 .range_end = end + 1,
4090 };
4091
Chris Masond3977122009-01-05 21:25:51 -05004092 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004093 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004094 if (clear_page_dirty_for_io(page))
4095 ret = __extent_writepage(page, &wbc_writepages, &epd);
4096 else {
4097 if (tree->ops && tree->ops->writepage_end_io_hook)
4098 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004099 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004100 NULL, 1);
4101 unlock_page(page);
4102 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004103 put_page(page);
4104 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004105 }
4106
David Sterbae2932ee2017-06-23 04:16:17 +02004107 flush_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004108 return ret;
4109}
Chris Masond1310b22008-01-24 16:13:08 -05004110
4111int extent_writepages(struct extent_io_tree *tree,
4112 struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004113 struct writeback_control *wbc)
4114{
4115 int ret = 0;
4116 struct extent_page_data epd = {
4117 .bio = NULL,
4118 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004119 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004120 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004121 };
4122
David Sterba935db852017-06-23 04:30:28 +02004123 ret = extent_write_cache_pages(mapping, wbc, &epd);
David Sterbae2932ee2017-06-23 04:16:17 +02004124 flush_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004125 return ret;
4126}
Chris Masond1310b22008-01-24 16:13:08 -05004127
4128int extent_readpages(struct extent_io_tree *tree,
4129 struct address_space *mapping,
David Sterba09325842017-06-23 04:09:57 +02004130 struct list_head *pages, unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004131{
4132 struct bio *bio = NULL;
4133 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004134 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004135 struct page *pagepool[16];
4136 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004137 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004138 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004139 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004140
Chris Masond1310b22008-01-24 16:13:08 -05004141 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004142 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004143
4144 prefetchw(&page->flags);
4145 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004146 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004147 page->index,
4148 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004149 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004150 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004151 }
Liu Bo67c96842012-07-20 21:43:09 -06004152
4153 pagepool[nr++] = page;
4154 if (nr < ARRAY_SIZE(pagepool))
4155 continue;
David Sterbae4d17ef2017-06-23 04:09:57 +02004156 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4157 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004158 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004159 }
Miao Xie99740902013-07-25 19:22:36 +08004160 if (nr)
David Sterbae4d17ef2017-06-23 04:09:57 +02004161 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4162 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004163
Miao Xie125bac012013-07-25 19:22:37 +08004164 if (em_cached)
4165 free_extent_map(em_cached);
4166
Chris Masond1310b22008-01-24 16:13:08 -05004167 BUG_ON(!list_empty(pages));
4168 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004169 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004170 return 0;
4171}
Chris Masond1310b22008-01-24 16:13:08 -05004172
4173/*
4174 * basic invalidatepage code, this waits on any locked or writeback
4175 * ranges corresponding to the page, and then deletes any extent state
4176 * records from the tree
4177 */
4178int extent_invalidatepage(struct extent_io_tree *tree,
4179 struct page *page, unsigned long offset)
4180{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004181 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004182 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004183 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004184 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4185
Qu Wenruofda28322013-02-26 08:10:22 +00004186 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004187 if (start > end)
4188 return 0;
4189
David Sterbaff13db42015-12-03 14:30:40 +01004190 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004191 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004192 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004193 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4194 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004195 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004196 return 0;
4197}
Chris Masond1310b22008-01-24 16:13:08 -05004198
4199/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004200 * a helper for releasepage, this tests for areas of the page that
4201 * are locked or under IO and drops the related state bits if it is safe
4202 * to drop the page.
4203 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004204static int try_release_extent_state(struct extent_map_tree *map,
4205 struct extent_io_tree *tree,
4206 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004207{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004208 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004209 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004210 int ret = 1;
4211
Chris Mason211f90e2008-07-18 11:56:15 -04004212 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004213 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004214 ret = 0;
4215 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004216 /*
4217 * at this point we can safely clear everything except the
4218 * locked bit and the nodatasum bit
4219 */
David Sterba66b0c882017-10-31 16:30:47 +01004220 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004221 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004222 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004223
4224 /* if clear_extent_bit failed for enomem reasons,
4225 * we can't allow the release to continue.
4226 */
4227 if (ret < 0)
4228 ret = 0;
4229 else
4230 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004231 }
4232 return ret;
4233}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004234
4235/*
Chris Masond1310b22008-01-24 16:13:08 -05004236 * a helper for releasepage. As long as there are no locked extents
4237 * in the range corresponding to the page, both state records and extent
4238 * map records are removed
4239 */
4240int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004241 struct extent_io_tree *tree, struct page *page,
4242 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004243{
4244 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004245 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004246 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004247
Mel Gormand0164ad2015-11-06 16:28:21 -08004248 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004249 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004250 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004251 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004252 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004253 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004254 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004255 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004256 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004257 break;
4258 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004259 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4260 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004261 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004262 free_extent_map(em);
4263 break;
4264 }
4265 if (!test_range_bit(tree, em->start,
4266 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004267 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004268 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004269 remove_extent_mapping(map, em);
4270 /* once for the rb tree */
4271 free_extent_map(em);
4272 }
4273 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004274 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004275
4276 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004277 free_extent_map(em);
4278 }
Chris Masond1310b22008-01-24 16:13:08 -05004279 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004280 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004281}
Chris Masond1310b22008-01-24 16:13:08 -05004282
Chris Masonec29ed52011-02-23 16:23:20 -05004283/*
4284 * helper function for fiemap, which doesn't want to see any holes.
4285 * This maps until we find something past 'last'
4286 */
4287static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004288 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004289{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004290 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004291 struct extent_map *em;
4292 u64 len;
4293
4294 if (offset >= last)
4295 return NULL;
4296
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304297 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004298 len = last - offset;
4299 if (len == 0)
4300 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004301 len = ALIGN(len, sectorsize);
David Sterbae3350e12017-06-23 04:09:57 +02004302 em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, offset,
4303 len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004304 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004305 return em;
4306
4307 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004308 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004309 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004310
4311 /* this is a hole, advance to the next extent */
4312 offset = extent_map_end(em);
4313 free_extent_map(em);
4314 if (offset >= last)
4315 break;
4316 }
4317 return NULL;
4318}
4319
Qu Wenruo47518322017-04-07 10:43:15 +08004320/*
4321 * To cache previous fiemap extent
4322 *
4323 * Will be used for merging fiemap extent
4324 */
4325struct fiemap_cache {
4326 u64 offset;
4327 u64 phys;
4328 u64 len;
4329 u32 flags;
4330 bool cached;
4331};
4332
4333/*
4334 * Helper to submit fiemap extent.
4335 *
4336 * Will try to merge current fiemap extent specified by @offset, @phys,
4337 * @len and @flags with cached one.
4338 * And only when we fails to merge, cached one will be submitted as
4339 * fiemap extent.
4340 *
4341 * Return value is the same as fiemap_fill_next_extent().
4342 */
4343static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4344 struct fiemap_cache *cache,
4345 u64 offset, u64 phys, u64 len, u32 flags)
4346{
4347 int ret = 0;
4348
4349 if (!cache->cached)
4350 goto assign;
4351
4352 /*
4353 * Sanity check, extent_fiemap() should have ensured that new
4354 * fiemap extent won't overlap with cahced one.
4355 * Not recoverable.
4356 *
4357 * NOTE: Physical address can overlap, due to compression
4358 */
4359 if (cache->offset + cache->len > offset) {
4360 WARN_ON(1);
4361 return -EINVAL;
4362 }
4363
4364 /*
4365 * Only merges fiemap extents if
4366 * 1) Their logical addresses are continuous
4367 *
4368 * 2) Their physical addresses are continuous
4369 * So truly compressed (physical size smaller than logical size)
4370 * extents won't get merged with each other
4371 *
4372 * 3) Share same flags except FIEMAP_EXTENT_LAST
4373 * So regular extent won't get merged with prealloc extent
4374 */
4375 if (cache->offset + cache->len == offset &&
4376 cache->phys + cache->len == phys &&
4377 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4378 (flags & ~FIEMAP_EXTENT_LAST)) {
4379 cache->len += len;
4380 cache->flags |= flags;
4381 goto try_submit_last;
4382 }
4383
4384 /* Not mergeable, need to submit cached one */
4385 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4386 cache->len, cache->flags);
4387 cache->cached = false;
4388 if (ret)
4389 return ret;
4390assign:
4391 cache->cached = true;
4392 cache->offset = offset;
4393 cache->phys = phys;
4394 cache->len = len;
4395 cache->flags = flags;
4396try_submit_last:
4397 if (cache->flags & FIEMAP_EXTENT_LAST) {
4398 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4399 cache->phys, cache->len, cache->flags);
4400 cache->cached = false;
4401 }
4402 return ret;
4403}
4404
4405/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004406 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004407 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004408 * The last fiemap cache may still be cached in the following case:
4409 * 0 4k 8k
4410 * |<- Fiemap range ->|
4411 * |<------------ First extent ----------->|
4412 *
4413 * In this case, the first extent range will be cached but not emitted.
4414 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004415 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004416static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4417 struct fiemap_extent_info *fieinfo,
4418 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004419{
4420 int ret;
4421
4422 if (!cache->cached)
4423 return 0;
4424
Qu Wenruo47518322017-04-07 10:43:15 +08004425 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4426 cache->len, cache->flags);
4427 cache->cached = false;
4428 if (ret > 0)
4429 ret = 0;
4430 return ret;
4431}
4432
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004433int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004434 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004435{
Josef Bacik975f84f2010-11-23 19:36:57 +00004436 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004437 u64 off = start;
4438 u64 max = start + len;
4439 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004440 u32 found_type;
4441 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004442 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004443 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004444 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004445 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004446 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004447 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004448 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004449 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004450 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004451 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004452 u64 em_start = 0;
4453 u64 em_len = 0;
4454 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004455
4456 if (len == 0)
4457 return -EINVAL;
4458
Josef Bacik975f84f2010-11-23 19:36:57 +00004459 path = btrfs_alloc_path();
4460 if (!path)
4461 return -ENOMEM;
4462 path->leave_spinning = 1;
4463
Jeff Mahoneyda170662016-06-15 09:22:56 -04004464 start = round_down(start, btrfs_inode_sectorsize(inode));
4465 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004466
Chris Masonec29ed52011-02-23 16:23:20 -05004467 /*
4468 * lookup the last file extent. We're not using i_size here
4469 * because there might be preallocation past i_size
4470 */
David Sterbaf85b7372017-01-20 14:54:07 +01004471 ret = btrfs_lookup_file_extent(NULL, root, path,
4472 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004473 if (ret < 0) {
4474 btrfs_free_path(path);
4475 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004476 } else {
4477 WARN_ON(!ret);
4478 if (ret == 1)
4479 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004480 }
Liu Bo2d324f52016-05-17 17:21:48 -07004481
Josef Bacik975f84f2010-11-23 19:36:57 +00004482 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004483 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004484 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004485
Chris Masonec29ed52011-02-23 16:23:20 -05004486 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004487 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004488 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004489 /* have to trust i_size as the end */
4490 last = (u64)-1;
4491 last_for_get_extent = isize;
4492 } else {
4493 /*
4494 * remember the start of the last extent. There are a
4495 * bunch of different factors that go into the length of the
4496 * extent, so its much less complex to remember where it started
4497 */
4498 last = found_key.offset;
4499 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004500 }
Liu Bofe09e162013-09-22 12:54:23 +08004501 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004502
Chris Masonec29ed52011-02-23 16:23:20 -05004503 /*
4504 * we might have some extents allocated but more delalloc past those
4505 * extents. so, we trust isize unless the start of the last extent is
4506 * beyond isize
4507 */
4508 if (last < isize) {
4509 last = (u64)-1;
4510 last_for_get_extent = isize;
4511 }
4512
David Sterbaff13db42015-12-03 14:30:40 +01004513 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004514 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004515
David Sterbae3350e12017-06-23 04:09:57 +02004516 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004517 if (!em)
4518 goto out;
4519 if (IS_ERR(em)) {
4520 ret = PTR_ERR(em);
4521 goto out;
4522 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004523
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004524 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004525 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004526
Chris Masonea8efc72011-03-08 11:54:40 -05004527 /* break if the extent we found is outside the range */
4528 if (em->start >= max || extent_map_end(em) < off)
4529 break;
4530
4531 /*
4532 * get_extent may return an extent that starts before our
4533 * requested range. We have to make sure the ranges
4534 * we return to fiemap always move forward and don't
4535 * overlap, so adjust the offsets here
4536 */
4537 em_start = max(em->start, off);
4538
4539 /*
4540 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004541 * for adjusting the disk offset below. Only do this if the
4542 * extent isn't compressed since our in ram offset may be past
4543 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004544 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004545 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4546 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004547 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004548 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004549 disko = 0;
4550 flags = 0;
4551
Chris Masonea8efc72011-03-08 11:54:40 -05004552 /*
4553 * bump off for our next call to get_extent
4554 */
4555 off = extent_map_end(em);
4556 if (off >= max)
4557 end = 1;
4558
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004559 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004560 end = 1;
4561 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004562 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004563 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4564 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004565 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004566 flags |= (FIEMAP_EXTENT_DELALLOC |
4567 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004568 } else if (fieinfo->fi_extents_max) {
4569 u64 bytenr = em->block_start -
4570 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004571
Chris Masonea8efc72011-03-08 11:54:40 -05004572 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004573
4574 /*
4575 * As btrfs supports shared space, this information
4576 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004577 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4578 * then we're just getting a count and we can skip the
4579 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004580 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004581 ret = btrfs_check_shared(root,
4582 btrfs_ino(BTRFS_I(inode)),
4583 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004584 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004585 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004586 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004587 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004588 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 }
4590 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4591 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004592 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4593 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004594
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004595 free_extent_map(em);
4596 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004597 if ((em_start >= last) || em_len == (u64)-1 ||
4598 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004599 flags |= FIEMAP_EXTENT_LAST;
4600 end = 1;
4601 }
4602
Chris Masonec29ed52011-02-23 16:23:20 -05004603 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004604 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004605 if (IS_ERR(em)) {
4606 ret = PTR_ERR(em);
4607 goto out;
4608 }
4609 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004610 flags |= FIEMAP_EXTENT_LAST;
4611 end = 1;
4612 }
Qu Wenruo47518322017-04-07 10:43:15 +08004613 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4614 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004615 if (ret) {
4616 if (ret == 1)
4617 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004618 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004619 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004620 }
4621out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004622 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004623 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004624 free_extent_map(em);
4625out:
Liu Bofe09e162013-09-22 12:54:23 +08004626 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004627 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004628 &cached_state);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004629 return ret;
4630}
4631
Chris Mason727011e2010-08-06 13:21:20 -04004632static void __free_extent_buffer(struct extent_buffer *eb)
4633{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004634 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004635 kmem_cache_free(extent_buffer_cache, eb);
4636}
4637
Josef Bacika26e8c92014-03-28 17:07:27 -04004638int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004639{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004640 return (atomic_read(&eb->io_pages) ||
4641 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4642 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004643}
4644
Miao Xie897ca6e92010-10-26 20:57:29 -04004645/*
4646 * Helper for releasing extent buffer page.
4647 */
David Sterbaa50924e2014-07-31 00:51:36 +02004648static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004649{
4650 unsigned long index;
4651 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004652 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004653
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004654 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004655
David Sterbaa50924e2014-07-31 00:51:36 +02004656 index = num_extent_pages(eb->start, eb->len);
4657 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004658 return;
4659
4660 do {
4661 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004662 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004663 if (!page)
4664 continue;
4665 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004666 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004667 /*
4668 * We do this since we'll remove the pages after we've
4669 * removed the eb from the radix tree, so we could race
4670 * and have this page now attached to the new eb. So
4671 * only clear page_private if it's still connected to
4672 * this eb.
4673 */
4674 if (PagePrivate(page) &&
4675 page->private == (unsigned long)eb) {
4676 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4677 BUG_ON(PageDirty(page));
4678 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004679 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004680 * We need to make sure we haven't be attached
4681 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004682 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004683 ClearPagePrivate(page);
4684 set_page_private(page, 0);
4685 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004686 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004687 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004688
4689 if (mapped)
4690 spin_unlock(&page->mapping->private_lock);
4691
Nicholas D Steeves01327612016-05-19 21:18:45 -04004692 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004693 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004694 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004695}
4696
4697/*
4698 * Helper for releasing the extent buffer.
4699 */
4700static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4701{
David Sterbaa50924e2014-07-31 00:51:36 +02004702 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004703 __free_extent_buffer(eb);
4704}
4705
Josef Bacikf28491e2013-12-16 13:24:27 -05004706static struct extent_buffer *
4707__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004708 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004709{
4710 struct extent_buffer *eb = NULL;
4711
Michal Hockod1b5c562015-08-19 14:17:40 +02004712 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004713 eb->start = start;
4714 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004715 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004716 eb->bflags = 0;
4717 rwlock_init(&eb->lock);
4718 atomic_set(&eb->write_locks, 0);
4719 atomic_set(&eb->read_locks, 0);
4720 atomic_set(&eb->blocking_readers, 0);
4721 atomic_set(&eb->blocking_writers, 0);
4722 atomic_set(&eb->spinning_readers, 0);
4723 atomic_set(&eb->spinning_writers, 0);
4724 eb->lock_nested = 0;
4725 init_waitqueue_head(&eb->write_lock_wq);
4726 init_waitqueue_head(&eb->read_lock_wq);
4727
4728 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4729
4730 spin_lock_init(&eb->refs_lock);
4731 atomic_set(&eb->refs, 1);
4732 atomic_set(&eb->io_pages, 0);
4733
4734 /*
4735 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4736 */
4737 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4738 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4739 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4740
4741 return eb;
4742}
4743
4744struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4745{
4746 unsigned long i;
4747 struct page *p;
4748 struct extent_buffer *new;
4749 unsigned long num_pages = num_extent_pages(src->start, src->len);
4750
David Sterba3f556f72014-06-15 03:20:26 +02004751 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004752 if (new == NULL)
4753 return NULL;
4754
4755 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004756 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004757 if (!p) {
4758 btrfs_release_extent_buffer(new);
4759 return NULL;
4760 }
4761 attach_extent_buffer_page(new, p);
4762 WARN_ON(PageDirty(p));
4763 SetPageUptodate(p);
4764 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004765 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004766 }
4767
Josef Bacikdb7f3432013-08-07 14:54:37 -04004768 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4769 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4770
4771 return new;
4772}
4773
Omar Sandoval0f331222015-09-29 20:50:31 -07004774struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4775 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004776{
4777 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004778 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004779 unsigned long i;
4780
Omar Sandoval0f331222015-09-29 20:50:31 -07004781 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004782
4783 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004784 if (!eb)
4785 return NULL;
4786
4787 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004788 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004789 if (!eb->pages[i])
4790 goto err;
4791 }
4792 set_extent_buffer_uptodate(eb);
4793 btrfs_set_header_nritems(eb, 0);
4794 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4795
4796 return eb;
4797err:
4798 for (; i > 0; i--)
4799 __free_page(eb->pages[i - 1]);
4800 __free_extent_buffer(eb);
4801 return NULL;
4802}
4803
Omar Sandoval0f331222015-09-29 20:50:31 -07004804struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004805 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004806{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004807 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004808}
4809
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004810static void check_buffer_tree_ref(struct extent_buffer *eb)
4811{
Chris Mason242e18c2013-01-29 17:49:37 -05004812 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004813 /* the ref bit is tricky. We have to make sure it is set
4814 * if we have the buffer dirty. Otherwise the
4815 * code to free a buffer can end up dropping a dirty
4816 * page
4817 *
4818 * Once the ref bit is set, it won't go away while the
4819 * buffer is dirty or in writeback, and it also won't
4820 * go away while we have the reference count on the
4821 * eb bumped.
4822 *
4823 * We can't just set the ref bit without bumping the
4824 * ref on the eb because free_extent_buffer might
4825 * see the ref bit and try to clear it. If this happens
4826 * free_extent_buffer might end up dropping our original
4827 * ref by mistake and freeing the page before we are able
4828 * to add one more ref.
4829 *
4830 * So bump the ref count first, then set the bit. If someone
4831 * beat us to it, drop the ref we added.
4832 */
Chris Mason242e18c2013-01-29 17:49:37 -05004833 refs = atomic_read(&eb->refs);
4834 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4835 return;
4836
Josef Bacik594831c2012-07-20 16:11:08 -04004837 spin_lock(&eb->refs_lock);
4838 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004839 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004840 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004841}
4842
Mel Gorman2457aec2014-06-04 16:10:31 -07004843static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4844 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004845{
4846 unsigned long num_pages, i;
4847
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004848 check_buffer_tree_ref(eb);
4849
Josef Bacik5df42352012-03-15 18:24:42 -04004850 num_pages = num_extent_pages(eb->start, eb->len);
4851 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004852 struct page *p = eb->pages[i];
4853
Mel Gorman2457aec2014-06-04 16:10:31 -07004854 if (p != accessed)
4855 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004856 }
4857}
4858
Josef Bacikf28491e2013-12-16 13:24:27 -05004859struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4860 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004861{
4862 struct extent_buffer *eb;
4863
4864 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004865 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004866 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004867 if (eb && atomic_inc_not_zero(&eb->refs)) {
4868 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004869 /*
4870 * Lock our eb's refs_lock to avoid races with
4871 * free_extent_buffer. When we get our eb it might be flagged
4872 * with EXTENT_BUFFER_STALE and another task running
4873 * free_extent_buffer might have seen that flag set,
4874 * eb->refs == 2, that the buffer isn't under IO (dirty and
4875 * writeback flags not set) and it's still in the tree (flag
4876 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4877 * of decrementing the extent buffer's reference count twice.
4878 * So here we could race and increment the eb's reference count,
4879 * clear its stale flag, mark it as dirty and drop our reference
4880 * before the other task finishes executing free_extent_buffer,
4881 * which would later result in an attempt to free an extent
4882 * buffer that is dirty.
4883 */
4884 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4885 spin_lock(&eb->refs_lock);
4886 spin_unlock(&eb->refs_lock);
4887 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004888 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004889 return eb;
4890 }
4891 rcu_read_unlock();
4892
4893 return NULL;
4894}
4895
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004896#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4897struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004898 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004899{
4900 struct extent_buffer *eb, *exists = NULL;
4901 int ret;
4902
4903 eb = find_extent_buffer(fs_info, start);
4904 if (eb)
4905 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004906 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004907 if (!eb)
4908 return NULL;
4909 eb->fs_info = fs_info;
4910again:
David Sterbae1860a72016-05-09 14:11:38 +02004911 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004912 if (ret)
4913 goto free_eb;
4914 spin_lock(&fs_info->buffer_lock);
4915 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004916 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004917 spin_unlock(&fs_info->buffer_lock);
4918 radix_tree_preload_end();
4919 if (ret == -EEXIST) {
4920 exists = find_extent_buffer(fs_info, start);
4921 if (exists)
4922 goto free_eb;
4923 else
4924 goto again;
4925 }
4926 check_buffer_tree_ref(eb);
4927 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4928
4929 /*
4930 * We will free dummy extent buffer's if they come into
4931 * free_extent_buffer with a ref count of 2, but if we are using this we
4932 * want the buffers to stay in memory until we're done with them, so
4933 * bump the ref count again.
4934 */
4935 atomic_inc(&eb->refs);
4936 return eb;
4937free_eb:
4938 btrfs_release_extent_buffer(eb);
4939 return exists;
4940}
4941#endif
4942
Josef Bacikf28491e2013-12-16 13:24:27 -05004943struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004944 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004945{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004946 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004947 unsigned long num_pages = num_extent_pages(start, len);
4948 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004949 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004950 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004951 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004952 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004953 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004954 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004955 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004956
Jeff Mahoneyda170662016-06-15 09:22:56 -04004957 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004958 btrfs_err(fs_info, "bad tree block start %llu", start);
4959 return ERR_PTR(-EINVAL);
4960 }
4961
Josef Bacikf28491e2013-12-16 13:24:27 -05004962 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004963 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004964 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004965
David Sterba23d79d82014-06-15 02:55:29 +02004966 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004967 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004968 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004969
Chris Mason727011e2010-08-06 13:21:20 -04004970 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004971 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004972 if (!p) {
4973 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004974 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004975 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004976
4977 spin_lock(&mapping->private_lock);
4978 if (PagePrivate(p)) {
4979 /*
4980 * We could have already allocated an eb for this page
4981 * and attached one so lets see if we can get a ref on
4982 * the existing eb, and if we can we know it's good and
4983 * we can just return that one, else we know we can just
4984 * overwrite page->private.
4985 */
4986 exists = (struct extent_buffer *)p->private;
4987 if (atomic_inc_not_zero(&exists->refs)) {
4988 spin_unlock(&mapping->private_lock);
4989 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004990 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004991 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004992 goto free_eb;
4993 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004994 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004995
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004996 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004997 * Do this so attach doesn't complain and we need to
4998 * drop the ref the old guy had.
4999 */
5000 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005001 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005002 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005003 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005004 attach_extent_buffer_page(eb, p);
5005 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005006 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005007 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005008 if (!PageUptodate(p))
5009 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005010
5011 /*
5012 * see below about how we avoid a nasty race with release page
5013 * and why we unlock later
5014 */
Chris Masond1310b22008-01-24 16:13:08 -05005015 }
5016 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005017 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005018again:
David Sterbae1860a72016-05-09 14:11:38 +02005019 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005020 if (ret) {
5021 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005022 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005023 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005024
Josef Bacikf28491e2013-12-16 13:24:27 -05005025 spin_lock(&fs_info->buffer_lock);
5026 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005027 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005028 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005029 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005030 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005031 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005032 if (exists)
5033 goto free_eb;
5034 else
Josef Bacik115391d2012-03-09 09:51:43 -05005035 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005036 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005037 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005038 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005039 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005040
5041 /*
5042 * there is a race where release page may have
5043 * tried to find this extent buffer in the radix
5044 * but failed. It will tell the VM it is safe to
5045 * reclaim the, and it will clear the page private bit.
5046 * We must make sure to set the page private bit properly
5047 * after the extent buffer is in the radix tree so
5048 * it doesn't get lost
5049 */
Chris Mason727011e2010-08-06 13:21:20 -04005050 SetPageChecked(eb->pages[0]);
5051 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005052 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005053 ClearPageChecked(p);
5054 unlock_page(p);
5055 }
5056 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005057 return eb;
5058
Chris Mason6af118ce2008-07-22 11:18:07 -04005059free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005060 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005061 for (i = 0; i < num_pages; i++) {
5062 if (eb->pages[i])
5063 unlock_page(eb->pages[i]);
5064 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005065
Miao Xie897ca6e92010-10-26 20:57:29 -04005066 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005067 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005068}
Chris Masond1310b22008-01-24 16:13:08 -05005069
Josef Bacik3083ee22012-03-09 16:01:49 -05005070static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5071{
5072 struct extent_buffer *eb =
5073 container_of(head, struct extent_buffer, rcu_head);
5074
5075 __free_extent_buffer(eb);
5076}
5077
Josef Bacik3083ee22012-03-09 16:01:49 -05005078/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005079static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005080{
5081 WARN_ON(atomic_read(&eb->refs) == 0);
5082 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005083 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005084 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005085
Jan Schmidt815a51c2012-05-16 17:00:02 +02005086 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005087
Josef Bacikf28491e2013-12-16 13:24:27 -05005088 spin_lock(&fs_info->buffer_lock);
5089 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005090 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005091 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005092 } else {
5093 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005094 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005095
5096 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005097 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005098#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5099 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5100 __free_extent_buffer(eb);
5101 return 1;
5102 }
5103#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005104 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005105 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005106 }
5107 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005108
5109 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005110}
5111
Chris Masond1310b22008-01-24 16:13:08 -05005112void free_extent_buffer(struct extent_buffer *eb)
5113{
Chris Mason242e18c2013-01-29 17:49:37 -05005114 int refs;
5115 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005116 if (!eb)
5117 return;
5118
Chris Mason242e18c2013-01-29 17:49:37 -05005119 while (1) {
5120 refs = atomic_read(&eb->refs);
5121 if (refs <= 3)
5122 break;
5123 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5124 if (old == refs)
5125 return;
5126 }
5127
Josef Bacik3083ee22012-03-09 16:01:49 -05005128 spin_lock(&eb->refs_lock);
5129 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005130 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5131 atomic_dec(&eb->refs);
5132
5133 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005134 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005135 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005136 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5137 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005138
Josef Bacik3083ee22012-03-09 16:01:49 -05005139 /*
5140 * I know this is terrible, but it's temporary until we stop tracking
5141 * the uptodate bits and such for the extent buffers.
5142 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005143 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005144}
Chris Masond1310b22008-01-24 16:13:08 -05005145
Josef Bacik3083ee22012-03-09 16:01:49 -05005146void free_extent_buffer_stale(struct extent_buffer *eb)
5147{
5148 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005149 return;
5150
Josef Bacik3083ee22012-03-09 16:01:49 -05005151 spin_lock(&eb->refs_lock);
5152 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5153
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005154 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005155 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5156 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005157 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005158}
5159
Chris Mason1d4284b2012-03-28 20:31:37 -04005160void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005161{
Chris Masond1310b22008-01-24 16:13:08 -05005162 unsigned long i;
5163 unsigned long num_pages;
5164 struct page *page;
5165
Chris Masond1310b22008-01-24 16:13:08 -05005166 num_pages = num_extent_pages(eb->start, eb->len);
5167
5168 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005169 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005170 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005171 continue;
5172
Chris Masona61e6f22008-07-22 11:18:08 -04005173 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005174 WARN_ON(!PagePrivate(page));
5175
Chris Masond1310b22008-01-24 16:13:08 -05005176 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005177 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005178 if (!PageDirty(page)) {
5179 radix_tree_tag_clear(&page->mapping->page_tree,
5180 page_index(page),
5181 PAGECACHE_TAG_DIRTY);
5182 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005183 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005184 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005185 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005186 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005187 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005188}
Chris Masond1310b22008-01-24 16:13:08 -05005189
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005190int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005191{
5192 unsigned long i;
5193 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005194 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005195
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005196 check_buffer_tree_ref(eb);
5197
Chris Masonb9473432009-03-13 11:00:37 -04005198 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005199
Chris Masond1310b22008-01-24 16:13:08 -05005200 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005201 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005202 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5203
Chris Masonb9473432009-03-13 11:00:37 -04005204 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005205 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005206 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005207}
Chris Masond1310b22008-01-24 16:13:08 -05005208
David Sterba69ba3922015-12-03 13:08:59 +01005209void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005210{
5211 unsigned long i;
5212 struct page *page;
5213 unsigned long num_pages;
5214
Chris Masonb4ce94d2009-02-04 09:25:08 -05005215 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005216 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005217 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005218 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005219 if (page)
5220 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005221 }
Chris Mason1259ab72008-05-12 13:39:03 -04005222}
5223
David Sterba09c25a82015-12-03 13:08:59 +01005224void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005225{
5226 unsigned long i;
5227 struct page *page;
5228 unsigned long num_pages;
5229
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005230 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005231 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005232 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005233 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005234 SetPageUptodate(page);
5235 }
Chris Masond1310b22008-01-24 16:13:08 -05005236}
Chris Masond1310b22008-01-24 16:13:08 -05005237
Chris Masond1310b22008-01-24 16:13:08 -05005238int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005239 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005240{
5241 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005242 struct page *page;
5243 int err;
5244 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005245 int locked_pages = 0;
5246 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005247 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005248 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005249 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005250 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005251
Chris Masonb4ce94d2009-02-04 09:25:08 -05005252 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005253 return 0;
5254
Chris Masond1310b22008-01-24 16:13:08 -05005255 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005256 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005257 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005258 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005259 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005260 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005261 } else {
5262 lock_page(page);
5263 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005264 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005265 }
5266 /*
5267 * We need to firstly lock all pages to make sure that
5268 * the uptodate bit of our pages won't be affected by
5269 * clear_extent_buffer_uptodate().
5270 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005271 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005272 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005273 if (!PageUptodate(page)) {
5274 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005275 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005276 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005277 }
Liu Bo2571e732016-08-03 12:33:01 -07005278
Chris Masonce9adaa2008-04-09 16:28:12 -04005279 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005280 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005281 goto unlock_exit;
5282 }
5283
Filipe Manana656f30d2014-09-26 12:25:56 +01005284 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005285 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005286 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005287 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005288 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005289
Chris Masonce9adaa2008-04-09 16:28:12 -04005290 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005291 if (ret) {
5292 atomic_dec(&eb->io_pages);
5293 unlock_page(page);
5294 continue;
5295 }
5296
Chris Masonf1885912008-04-09 16:28:12 -04005297 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005298 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005299 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005300 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005301 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005302 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005303 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005304 /*
5305 * We use &bio in above __extent_read_full_page,
5306 * so we ensure that if it returns error, the
5307 * current page fails to add itself to bio and
5308 * it's been unlocked.
5309 *
5310 * We must dec io_pages by ourselves.
5311 */
5312 atomic_dec(&eb->io_pages);
5313 }
Chris Masond1310b22008-01-24 16:13:08 -05005314 } else {
5315 unlock_page(page);
5316 }
5317 }
5318
Jeff Mahoney355808c2011-10-03 23:23:14 -04005319 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005320 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005321 if (err)
5322 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005323 }
Chris Masona86c12c2008-02-07 10:50:54 -05005324
Arne Jansenbb82ab82011-06-10 14:06:53 +02005325 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005326 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005327
Josef Bacik8436ea912016-09-02 15:40:03 -04005328 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005329 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005330 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005331 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005332 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005333 }
Chris Masond3977122009-01-05 21:25:51 -05005334
Chris Masond1310b22008-01-24 16:13:08 -05005335 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005336
5337unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005338 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005339 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005340 page = eb->pages[locked_pages];
5341 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005342 }
5343 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005344}
Chris Masond1310b22008-01-24 16:13:08 -05005345
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005346void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5347 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005348{
5349 size_t cur;
5350 size_t offset;
5351 struct page *page;
5352 char *kaddr;
5353 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005354 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5355 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005356
Liu Bof716abd2017-08-09 11:10:16 -06005357 if (start + len > eb->len) {
5358 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5359 eb->start, eb->len, start, len);
5360 memset(dst, 0, len);
5361 return;
5362 }
Chris Masond1310b22008-01-24 16:13:08 -05005363
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005364 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005365
Chris Masond3977122009-01-05 21:25:51 -05005366 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005367 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005368
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005369 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005370 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005371 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005372
5373 dst += cur;
5374 len -= cur;
5375 offset = 0;
5376 i++;
5377 }
5378}
Chris Masond1310b22008-01-24 16:13:08 -05005379
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005380int read_extent_buffer_to_user(const struct extent_buffer *eb,
5381 void __user *dstv,
5382 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005383{
5384 size_t cur;
5385 size_t offset;
5386 struct page *page;
5387 char *kaddr;
5388 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005389 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5390 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005391 int ret = 0;
5392
5393 WARN_ON(start > eb->len);
5394 WARN_ON(start + len > eb->start + eb->len);
5395
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005396 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005397
5398 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005399 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005400
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005401 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005402 kaddr = page_address(page);
5403 if (copy_to_user(dst, kaddr + offset, cur)) {
5404 ret = -EFAULT;
5405 break;
5406 }
5407
5408 dst += cur;
5409 len -= cur;
5410 offset = 0;
5411 i++;
5412 }
5413
5414 return ret;
5415}
5416
Liu Bo415b35a2016-06-17 19:16:21 -07005417/*
5418 * return 0 if the item is found within a page.
5419 * return 1 if the item spans two pages.
5420 * return -EINVAL otherwise.
5421 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005422int map_private_extent_buffer(const struct extent_buffer *eb,
5423 unsigned long start, unsigned long min_len,
5424 char **map, unsigned long *map_start,
5425 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005426{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005427 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005428 char *kaddr;
5429 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005430 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5431 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005432 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005433 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005434
Liu Bof716abd2017-08-09 11:10:16 -06005435 if (start + min_len > eb->len) {
5436 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5437 eb->start, eb->len, start, min_len);
5438 return -EINVAL;
5439 }
5440
Chris Masond1310b22008-01-24 16:13:08 -05005441 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005442 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005443
5444 if (i == 0) {
5445 offset = start_offset;
5446 *map_start = 0;
5447 } else {
5448 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005449 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005450 }
Chris Masond3977122009-01-05 21:25:51 -05005451
David Sterbafb85fc92014-07-31 01:03:53 +02005452 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005453 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005454 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005455 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005456 return 0;
5457}
Chris Masond1310b22008-01-24 16:13:08 -05005458
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005459int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5460 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005461{
5462 size_t cur;
5463 size_t offset;
5464 struct page *page;
5465 char *kaddr;
5466 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005467 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5468 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005469 int ret = 0;
5470
5471 WARN_ON(start > eb->len);
5472 WARN_ON(start + len > eb->start + eb->len);
5473
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005474 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005475
Chris Masond3977122009-01-05 21:25:51 -05005476 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005477 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005479 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005480
Chris Masona6591712011-07-19 12:04:14 -04005481 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005482 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005483 if (ret)
5484 break;
5485
5486 ptr += cur;
5487 len -= cur;
5488 offset = 0;
5489 i++;
5490 }
5491 return ret;
5492}
Chris Masond1310b22008-01-24 16:13:08 -05005493
David Sterbaf157bf72016-11-09 17:43:38 +01005494void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5495 const void *srcv)
5496{
5497 char *kaddr;
5498
5499 WARN_ON(!PageUptodate(eb->pages[0]));
5500 kaddr = page_address(eb->pages[0]);
5501 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5502 BTRFS_FSID_SIZE);
5503}
5504
5505void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5506{
5507 char *kaddr;
5508
5509 WARN_ON(!PageUptodate(eb->pages[0]));
5510 kaddr = page_address(eb->pages[0]);
5511 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5512 BTRFS_FSID_SIZE);
5513}
5514
Chris Masond1310b22008-01-24 16:13:08 -05005515void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5516 unsigned long start, unsigned long len)
5517{
5518 size_t cur;
5519 size_t offset;
5520 struct page *page;
5521 char *kaddr;
5522 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005523 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5524 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005525
5526 WARN_ON(start > eb->len);
5527 WARN_ON(start + len > eb->start + eb->len);
5528
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005529 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005530
Chris Masond3977122009-01-05 21:25:51 -05005531 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005532 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005533 WARN_ON(!PageUptodate(page));
5534
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005535 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005536 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005537 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005538
5539 src += cur;
5540 len -= cur;
5541 offset = 0;
5542 i++;
5543 }
5544}
Chris Masond1310b22008-01-24 16:13:08 -05005545
David Sterbab159fa22016-11-08 18:09:03 +01005546void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5547 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005548{
5549 size_t cur;
5550 size_t offset;
5551 struct page *page;
5552 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005553 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5554 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005555
5556 WARN_ON(start > eb->len);
5557 WARN_ON(start + len > eb->start + eb->len);
5558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005559 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005560
Chris Masond3977122009-01-05 21:25:51 -05005561 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005562 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005563 WARN_ON(!PageUptodate(page));
5564
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005565 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005566 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005567 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005568
5569 len -= cur;
5570 offset = 0;
5571 i++;
5572 }
5573}
Chris Masond1310b22008-01-24 16:13:08 -05005574
David Sterba58e80122016-11-08 18:30:31 +01005575void copy_extent_buffer_full(struct extent_buffer *dst,
5576 struct extent_buffer *src)
5577{
5578 int i;
5579 unsigned num_pages;
5580
5581 ASSERT(dst->len == src->len);
5582
5583 num_pages = num_extent_pages(dst->start, dst->len);
5584 for (i = 0; i < num_pages; i++)
5585 copy_page(page_address(dst->pages[i]),
5586 page_address(src->pages[i]));
5587}
5588
Chris Masond1310b22008-01-24 16:13:08 -05005589void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5590 unsigned long dst_offset, unsigned long src_offset,
5591 unsigned long len)
5592{
5593 u64 dst_len = dst->len;
5594 size_t cur;
5595 size_t offset;
5596 struct page *page;
5597 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005598 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5599 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005600
5601 WARN_ON(src->len != dst_len);
5602
5603 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005604 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005605
Chris Masond3977122009-01-05 21:25:51 -05005606 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005607 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005608 WARN_ON(!PageUptodate(page));
5609
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005610 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005611
Chris Masona6591712011-07-19 12:04:14 -04005612 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005613 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005614
5615 src_offset += cur;
5616 len -= cur;
5617 offset = 0;
5618 i++;
5619 }
5620}
Chris Masond1310b22008-01-24 16:13:08 -05005621
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005622void le_bitmap_set(u8 *map, unsigned int start, int len)
5623{
5624 u8 *p = map + BIT_BYTE(start);
5625 const unsigned int size = start + len;
5626 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5627 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5628
5629 while (len - bits_to_set >= 0) {
5630 *p |= mask_to_set;
5631 len -= bits_to_set;
5632 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005633 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005634 p++;
5635 }
5636 if (len) {
5637 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5638 *p |= mask_to_set;
5639 }
5640}
5641
5642void le_bitmap_clear(u8 *map, unsigned int start, int len)
5643{
5644 u8 *p = map + BIT_BYTE(start);
5645 const unsigned int size = start + len;
5646 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5647 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5648
5649 while (len - bits_to_clear >= 0) {
5650 *p &= ~mask_to_clear;
5651 len -= bits_to_clear;
5652 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005653 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005654 p++;
5655 }
5656 if (len) {
5657 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5658 *p &= ~mask_to_clear;
5659 }
5660}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005661
5662/*
5663 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5664 * given bit number
5665 * @eb: the extent buffer
5666 * @start: offset of the bitmap item in the extent buffer
5667 * @nr: bit number
5668 * @page_index: return index of the page in the extent buffer that contains the
5669 * given bit number
5670 * @page_offset: return offset into the page given by page_index
5671 *
5672 * This helper hides the ugliness of finding the byte in an extent buffer which
5673 * contains a given bit.
5674 */
5675static inline void eb_bitmap_offset(struct extent_buffer *eb,
5676 unsigned long start, unsigned long nr,
5677 unsigned long *page_index,
5678 size_t *page_offset)
5679{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005680 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005681 size_t byte_offset = BIT_BYTE(nr);
5682 size_t offset;
5683
5684 /*
5685 * The byte we want is the offset of the extent buffer + the offset of
5686 * the bitmap item in the extent buffer + the offset of the byte in the
5687 * bitmap item.
5688 */
5689 offset = start_offset + start + byte_offset;
5690
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005691 *page_index = offset >> PAGE_SHIFT;
5692 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005693}
5694
5695/**
5696 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5697 * @eb: the extent buffer
5698 * @start: offset of the bitmap item in the extent buffer
5699 * @nr: bit number to test
5700 */
5701int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5702 unsigned long nr)
5703{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005704 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005705 struct page *page;
5706 unsigned long i;
5707 size_t offset;
5708
5709 eb_bitmap_offset(eb, start, nr, &i, &offset);
5710 page = eb->pages[i];
5711 WARN_ON(!PageUptodate(page));
5712 kaddr = page_address(page);
5713 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5714}
5715
5716/**
5717 * extent_buffer_bitmap_set - set an area of a bitmap
5718 * @eb: the extent buffer
5719 * @start: offset of the bitmap item in the extent buffer
5720 * @pos: bit number of the first bit
5721 * @len: number of bits to set
5722 */
5723void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5724 unsigned long pos, unsigned long len)
5725{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005726 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005727 struct page *page;
5728 unsigned long i;
5729 size_t offset;
5730 const unsigned int size = pos + len;
5731 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005732 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005733
5734 eb_bitmap_offset(eb, start, pos, &i, &offset);
5735 page = eb->pages[i];
5736 WARN_ON(!PageUptodate(page));
5737 kaddr = page_address(page);
5738
5739 while (len >= bits_to_set) {
5740 kaddr[offset] |= mask_to_set;
5741 len -= bits_to_set;
5742 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005743 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005744 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005745 offset = 0;
5746 page = eb->pages[++i];
5747 WARN_ON(!PageUptodate(page));
5748 kaddr = page_address(page);
5749 }
5750 }
5751 if (len) {
5752 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5753 kaddr[offset] |= mask_to_set;
5754 }
5755}
5756
5757
5758/**
5759 * extent_buffer_bitmap_clear - clear an area of a bitmap
5760 * @eb: the extent buffer
5761 * @start: offset of the bitmap item in the extent buffer
5762 * @pos: bit number of the first bit
5763 * @len: number of bits to clear
5764 */
5765void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5766 unsigned long pos, unsigned long len)
5767{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005768 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005769 struct page *page;
5770 unsigned long i;
5771 size_t offset;
5772 const unsigned int size = pos + len;
5773 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005774 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005775
5776 eb_bitmap_offset(eb, start, pos, &i, &offset);
5777 page = eb->pages[i];
5778 WARN_ON(!PageUptodate(page));
5779 kaddr = page_address(page);
5780
5781 while (len >= bits_to_clear) {
5782 kaddr[offset] &= ~mask_to_clear;
5783 len -= bits_to_clear;
5784 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005785 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005786 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005787 offset = 0;
5788 page = eb->pages[++i];
5789 WARN_ON(!PageUptodate(page));
5790 kaddr = page_address(page);
5791 }
5792 }
5793 if (len) {
5794 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5795 kaddr[offset] &= ~mask_to_clear;
5796 }
5797}
5798
Sergei Trofimovich33872062011-04-11 21:52:52 +00005799static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5800{
5801 unsigned long distance = (src > dst) ? src - dst : dst - src;
5802 return distance < len;
5803}
5804
Chris Masond1310b22008-01-24 16:13:08 -05005805static void copy_pages(struct page *dst_page, struct page *src_page,
5806 unsigned long dst_off, unsigned long src_off,
5807 unsigned long len)
5808{
Chris Masona6591712011-07-19 12:04:14 -04005809 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005810 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005811 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005812
Sergei Trofimovich33872062011-04-11 21:52:52 +00005813 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005814 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005815 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005816 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005817 if (areas_overlap(src_off, dst_off, len))
5818 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005819 }
Chris Masond1310b22008-01-24 16:13:08 -05005820
Chris Mason727011e2010-08-06 13:21:20 -04005821 if (must_memmove)
5822 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5823 else
5824 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005825}
5826
5827void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5828 unsigned long src_offset, unsigned long len)
5829{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005830 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005831 size_t cur;
5832 size_t dst_off_in_page;
5833 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005834 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005835 unsigned long dst_i;
5836 unsigned long src_i;
5837
5838 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005839 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005840 "memmove bogus src_offset %lu move len %lu dst len %lu",
5841 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005842 BUG_ON(1);
5843 }
5844 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005845 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005846 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5847 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005848 BUG_ON(1);
5849 }
5850
Chris Masond3977122009-01-05 21:25:51 -05005851 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005852 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005853 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005854 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005855 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005856
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005857 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5858 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005859
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005860 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005861 src_off_in_page));
5862 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005863 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005864
David Sterbafb85fc92014-07-31 01:03:53 +02005865 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005866 dst_off_in_page, src_off_in_page, cur);
5867
5868 src_offset += cur;
5869 dst_offset += cur;
5870 len -= cur;
5871 }
5872}
Chris Masond1310b22008-01-24 16:13:08 -05005873
5874void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5875 unsigned long src_offset, unsigned long len)
5876{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005877 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005878 size_t cur;
5879 size_t dst_off_in_page;
5880 size_t src_off_in_page;
5881 unsigned long dst_end = dst_offset + len - 1;
5882 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005883 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005884 unsigned long dst_i;
5885 unsigned long src_i;
5886
5887 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005888 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005889 "memmove bogus src_offset %lu move len %lu len %lu",
5890 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005891 BUG_ON(1);
5892 }
5893 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005894 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005895 "memmove bogus dst_offset %lu move len %lu len %lu",
5896 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005897 BUG_ON(1);
5898 }
Chris Mason727011e2010-08-06 13:21:20 -04005899 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005900 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5901 return;
5902 }
Chris Masond3977122009-01-05 21:25:51 -05005903 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005904 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5905 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005906
5907 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005908 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005909 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005910 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005911
5912 cur = min_t(unsigned long, len, src_off_in_page + 1);
5913 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005914 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005915 dst_off_in_page - cur + 1,
5916 src_off_in_page - cur + 1, cur);
5917
5918 dst_end -= cur;
5919 src_end -= cur;
5920 len -= cur;
5921 }
5922}
Chris Mason6af118ce2008-07-22 11:18:07 -04005923
David Sterbaf7a52a42013-04-26 14:56:29 +00005924int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005925{
Chris Mason6af118ce2008-07-22 11:18:07 -04005926 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005927
Miao Xie19fe0a82010-10-26 20:57:29 -04005928 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005929 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005930 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005931 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005932 spin_lock(&page->mapping->private_lock);
5933 if (!PagePrivate(page)) {
5934 spin_unlock(&page->mapping->private_lock);
5935 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005936 }
5937
Josef Bacik3083ee22012-03-09 16:01:49 -05005938 eb = (struct extent_buffer *)page->private;
5939 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005940
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005941 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005942 * This is a little awful but should be ok, we need to make sure that
5943 * the eb doesn't disappear out from under us while we're looking at
5944 * this page.
5945 */
5946 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005947 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005948 spin_unlock(&eb->refs_lock);
5949 spin_unlock(&page->mapping->private_lock);
5950 return 0;
5951 }
5952 spin_unlock(&page->mapping->private_lock);
5953
Josef Bacik3083ee22012-03-09 16:01:49 -05005954 /*
5955 * If tree ref isn't set then we know the ref on this eb is a real ref,
5956 * so just return, this page will likely be freed soon anyway.
5957 */
5958 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5959 spin_unlock(&eb->refs_lock);
5960 return 0;
5961 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005962
David Sterbaf7a52a42013-04-26 14:56:29 +00005963 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005964}