blob: ef72efef8b39c6b166071e5c93ccd62d30323189 [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);
Jeff Mahoney62e85572016-09-20 10:05:01 -040079 pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020080 eb->start, eb->len, atomic_read(&eb->refs));
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
Qu Wenruod38ed272015-10-12 14:53:37 +0800122static void add_extent_changeset(struct extent_state *state, unsigned bits,
123 struct extent_changeset *changeset,
124 int set)
125{
126 int ret;
127
128 if (!changeset)
129 return;
130 if (set && (state->state & bits) == bits)
131 return;
Qu Wenruofefdc552015-10-12 15:35:38 +0800132 if (!set && (state->state & bits) == 0)
133 return;
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);
137 /* ENOMEM */
138 BUG_ON(ret < 0);
139}
140
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400141static noinline void flush_write_bio(void *data);
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
189void extent_io_exit(void)
190{
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;
Chris Masond1310b22008-01-24 16:13:08 -0500529
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400530 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500531 u64 range = state->end - state->start + 1;
532 WARN_ON(range > tree->dirty_bytes);
533 tree->dirty_bytes -= range;
534 }
Chris Mason291d6732008-01-29 15:55:23 -0500535 clear_state_cb(tree, state, bits);
Qu Wenruofefdc552015-10-12 15:35:38 +0800536 add_extent_changeset(state, bits_to_clear, changeset, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400537 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500538 if (wake)
539 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400540 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800541 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100542 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500543 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100544 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500545 free_extent_state(state);
546 } else {
547 WARN_ON(1);
548 }
549 } else {
550 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800551 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500552 }
Li Zefancdc6a392012-03-12 16:39:48 +0800553 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500554}
555
Xiao Guangrong82337672011-04-20 06:44:57 +0000556static struct extent_state *
557alloc_extent_state_atomic(struct extent_state *prealloc)
558{
559 if (!prealloc)
560 prealloc = alloc_extent_state(GFP_ATOMIC);
561
562 return prealloc;
563}
564
Eric Sandeen48a3b632013-04-25 20:41:01 +0000565static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400566{
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400567 btrfs_panic(tree_fs_info(tree), err,
568 "Locking error: Extent tree was modified by another thread while locked.");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400569}
570
Chris Masond1310b22008-01-24 16:13:08 -0500571/*
572 * clear some bits on a range in the tree. This may require splitting
573 * or inserting elements in the tree, so the gfp mask is used to
574 * indicate which allocations or sleeping are allowed.
575 *
576 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
577 * the given range from the tree regardless of state (ie for truncate).
578 *
579 * the range [start, end] is inclusive.
580 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100581 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500582 */
David Sterba66b0c882017-10-31 16:30:47 +0100583int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Qu Wenruofefdc552015-10-12 15:35:38 +0800584 unsigned bits, int wake, int delete,
585 struct extent_state **cached_state,
586 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500587{
588 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400589 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500590 struct extent_state *prealloc = NULL;
591 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400592 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500593 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000594 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500595
Josef Bacika5dee372013-12-13 10:02:44 -0500596 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000597
Josef Bacik7ee9e442013-06-21 16:37:03 -0400598 if (bits & EXTENT_DELALLOC)
599 bits |= EXTENT_NORESERVE;
600
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400601 if (delete)
602 bits |= ~EXTENT_CTLBITS;
603 bits |= EXTENT_FIRST_DELALLOC;
604
Josef Bacik2ac55d42010-02-03 19:33:23 +0000605 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
606 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500607again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800608 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000609 /*
610 * Don't care for allocation failure here because we might end
611 * up not needing the pre-allocated extent state at all, which
612 * is the case if we only have in the tree extent states that
613 * cover our input range and don't cover too any other range.
614 * If we end up needing a new extent state we allocate it later.
615 */
Chris Masond1310b22008-01-24 16:13:08 -0500616 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500617 }
618
Chris Masoncad321a2008-12-17 14:51:42 -0500619 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400620 if (cached_state) {
621 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000622
623 if (clear) {
624 *cached_state = NULL;
625 cached_state = NULL;
626 }
627
Filipe Manana27a35072014-07-06 20:09:59 +0100628 if (cached && extent_state_in_tree(cached) &&
629 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000630 if (clear)
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200631 refcount_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400632 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400633 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400634 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000635 if (clear)
636 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400637 }
Chris Masond1310b22008-01-24 16:13:08 -0500638 /*
639 * this search will find the extents that end after
640 * our range starts
641 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500642 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500643 if (!node)
644 goto out;
645 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400646hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500647 if (state->start > end)
648 goto out;
649 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400650 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500651
Liu Bo04493142012-02-16 18:34:37 +0800652 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800653 if (!(state->state & bits)) {
654 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800655 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800656 }
Liu Bo04493142012-02-16 18:34:37 +0800657
Chris Masond1310b22008-01-24 16:13:08 -0500658 /*
659 * | ---- desired range ---- |
660 * | state | or
661 * | ------------- state -------------- |
662 *
663 * We need to split the extent we found, and may flip
664 * bits on second half.
665 *
666 * If the extent we found extends past our range, we
667 * just split and search again. It'll get split again
668 * the next time though.
669 *
670 * If the extent we found is inside our range, we clear
671 * the desired bit on it.
672 */
673
674 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000675 prealloc = alloc_extent_state_atomic(prealloc);
676 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500677 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400678 if (err)
679 extent_io_tree_panic(tree, err);
680
Chris Masond1310b22008-01-24 16:13:08 -0500681 prealloc = NULL;
682 if (err)
683 goto out;
684 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800685 state = clear_state_bit(tree, state, &bits, wake,
686 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800687 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500688 }
689 goto search_again;
690 }
691 /*
692 * | ---- desired range ---- |
693 * | state |
694 * We need to split the extent, and clear the bit
695 * on the first half
696 */
697 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000698 prealloc = alloc_extent_state_atomic(prealloc);
699 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500700 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400701 if (err)
702 extent_io_tree_panic(tree, err);
703
Chris Masond1310b22008-01-24 16:13:08 -0500704 if (wake)
705 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400706
Qu Wenruofefdc552015-10-12 15:35:38 +0800707 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400708
Chris Masond1310b22008-01-24 16:13:08 -0500709 prealloc = NULL;
710 goto out;
711 }
Chris Mason42daec22009-09-23 19:51:09 -0400712
Qu Wenruofefdc552015-10-12 15:35:38 +0800713 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800714next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400715 if (last_end == (u64)-1)
716 goto out;
717 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800718 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800719 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500720
721search_again:
722 if (start > end)
723 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500724 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800725 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500726 cond_resched();
727 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200728
729out:
730 spin_unlock(&tree->lock);
731 if (prealloc)
732 free_extent_state(prealloc);
733
734 return 0;
735
Chris Masond1310b22008-01-24 16:13:08 -0500736}
Chris Masond1310b22008-01-24 16:13:08 -0500737
Jeff Mahoney143bede2012-03-01 14:56:26 +0100738static void wait_on_state(struct extent_io_tree *tree,
739 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500740 __releases(tree->lock)
741 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500742{
743 DEFINE_WAIT(wait);
744 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500745 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500746 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500747 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500748 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500749}
750
751/*
752 * waits for one or more bits to clear on a range in the state tree.
753 * The range [start, end] is inclusive.
754 * The tree lock is taken by this function
755 */
David Sterba41074882013-04-29 13:38:46 +0000756static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
757 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500758{
759 struct extent_state *state;
760 struct rb_node *node;
761
Josef Bacika5dee372013-12-13 10:02:44 -0500762 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000763
Chris Masoncad321a2008-12-17 14:51:42 -0500764 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500765again:
766 while (1) {
767 /*
768 * this search will find all the extents that end after
769 * our range starts
770 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500771 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100772process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500773 if (!node)
774 break;
775
776 state = rb_entry(node, struct extent_state, rb_node);
777
778 if (state->start > end)
779 goto out;
780
781 if (state->state & bits) {
782 start = state->start;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200783 refcount_inc(&state->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500784 wait_on_state(tree, state);
785 free_extent_state(state);
786 goto again;
787 }
788 start = state->end + 1;
789
790 if (start > end)
791 break;
792
Filipe Mananac50d3e72014-03-31 14:53:25 +0100793 if (!cond_resched_lock(&tree->lock)) {
794 node = rb_next(node);
795 goto process_node;
796 }
Chris Masond1310b22008-01-24 16:13:08 -0500797 }
798out:
Chris Masoncad321a2008-12-17 14:51:42 -0500799 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500800}
Chris Masond1310b22008-01-24 16:13:08 -0500801
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000802static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500803 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800804 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500805{
David Sterba9ee49a042015-01-14 19:52:13 +0100806 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400807
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000808 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400809 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500810 u64 range = state->end - state->start + 1;
811 tree->dirty_bytes += range;
812 }
Qu Wenruod38ed272015-10-12 14:53:37 +0800813 add_extent_changeset(state, bits_to_set, changeset, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400814 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500815}
816
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100817static void cache_state_if_flags(struct extent_state *state,
818 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100819 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400820{
821 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100822 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400823 *cached_ptr = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +0200824 refcount_inc(&state->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400825 }
826 }
827}
828
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100829static void cache_state(struct extent_state *state,
830 struct extent_state **cached_ptr)
831{
832 return cache_state_if_flags(state, cached_ptr,
833 EXTENT_IOBITS | EXTENT_BOUNDARY);
834}
835
Chris Masond1310b22008-01-24 16:13:08 -0500836/*
Chris Mason1edbb732009-09-02 13:24:36 -0400837 * set some bits on a range in the tree. This may require allocations or
838 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500839 *
Chris Mason1edbb732009-09-02 13:24:36 -0400840 * If any of the exclusive bits are set, this will fail with -EEXIST if some
841 * part of the range already has the desired bits set. The start of the
842 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500843 *
Chris Mason1edbb732009-09-02 13:24:36 -0400844 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500845 */
Chris Mason1edbb732009-09-02 13:24:36 -0400846
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100847static int __must_check
848__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100849 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000850 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800851 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500852{
853 struct extent_state *state;
854 struct extent_state *prealloc = NULL;
855 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000856 struct rb_node **p;
857 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500858 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500859 u64 last_start;
860 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400861
Josef Bacika5dee372013-12-13 10:02:44 -0500862 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000863
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400864 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500865again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800866 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200867 /*
868 * Don't care for allocation failure here because we might end
869 * up not needing the pre-allocated extent state at all, which
870 * is the case if we only have in the tree extent states that
871 * cover our input range and don't cover too any other range.
872 * If we end up needing a new extent state we allocate it later.
873 */
Chris Masond1310b22008-01-24 16:13:08 -0500874 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500875 }
876
Chris Masoncad321a2008-12-17 14:51:42 -0500877 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400878 if (cached_state && *cached_state) {
879 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400880 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100881 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400882 node = &state->rb_node;
883 goto hit_next;
884 }
885 }
Chris Masond1310b22008-01-24 16:13:08 -0500886 /*
887 * this search will find all the extents that end after
888 * our range starts.
889 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000890 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500891 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000892 prealloc = alloc_extent_state_atomic(prealloc);
893 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000894 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800895 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400896 if (err)
897 extent_io_tree_panic(tree, err);
898
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000899 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500900 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500901 goto out;
902 }
Chris Masond1310b22008-01-24 16:13:08 -0500903 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400904hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500905 last_start = state->start;
906 last_end = state->end;
907
908 /*
909 * | ---- desired range ---- |
910 * | state |
911 *
912 * Just lock what we found and keep going
913 */
914 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400915 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500916 *failed_start = state->start;
917 err = -EEXIST;
918 goto out;
919 }
Chris Mason42daec22009-09-23 19:51:09 -0400920
Qu Wenruod38ed272015-10-12 14:53:37 +0800921 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400922 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500923 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400924 if (last_end == (u64)-1)
925 goto out;
926 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800927 state = next_state(state);
928 if (start < end && state && state->start == start &&
929 !need_resched())
930 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500931 goto search_again;
932 }
933
934 /*
935 * | ---- desired range ---- |
936 * | state |
937 * or
938 * | ------------- state -------------- |
939 *
940 * We need to split the extent we found, and may flip bits on
941 * second half.
942 *
943 * If the extent we found extends past our
944 * range, we just split and search again. It'll get split
945 * again the next time though.
946 *
947 * If the extent we found is inside our range, we set the
948 * desired bit on it.
949 */
950 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400951 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500952 *failed_start = start;
953 err = -EEXIST;
954 goto out;
955 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000956
957 prealloc = alloc_extent_state_atomic(prealloc);
958 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500959 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400960 if (err)
961 extent_io_tree_panic(tree, err);
962
Chris Masond1310b22008-01-24 16:13:08 -0500963 prealloc = NULL;
964 if (err)
965 goto out;
966 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800967 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400968 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500969 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400970 if (last_end == (u64)-1)
971 goto out;
972 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800973 state = next_state(state);
974 if (start < end && state && state->start == start &&
975 !need_resched())
976 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500977 }
978 goto search_again;
979 }
980 /*
981 * | ---- desired range ---- |
982 * | state | or | state |
983 *
984 * There's a hole, we need to insert something in it and
985 * ignore the extent we found.
986 */
987 if (state->start > start) {
988 u64 this_end;
989 if (end < last_start)
990 this_end = end;
991 else
Chris Masond3977122009-01-05 21:25:51 -0500992 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000993
994 prealloc = alloc_extent_state_atomic(prealloc);
995 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000996
997 /*
998 * Avoid to free 'prealloc' if it can be merged with
999 * the later extent.
1000 */
Chris Masond1310b22008-01-24 16:13:08 -05001001 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001002 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001003 if (err)
1004 extent_io_tree_panic(tree, err);
1005
Chris Mason2c64c532009-09-02 15:04:12 -04001006 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001007 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001008 start = this_end + 1;
1009 goto search_again;
1010 }
1011 /*
1012 * | ---- desired range ---- |
1013 * | state |
1014 * We need to split the extent, and set the bit
1015 * on the first half
1016 */
1017 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001018 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001019 *failed_start = start;
1020 err = -EEXIST;
1021 goto out;
1022 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001023
1024 prealloc = alloc_extent_state_atomic(prealloc);
1025 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001026 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001027 if (err)
1028 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001029
Qu Wenruod38ed272015-10-12 14:53:37 +08001030 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001031 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001032 merge_state(tree, prealloc);
1033 prealloc = NULL;
1034 goto out;
1035 }
1036
David Sterbab5a4ba12016-04-27 01:02:15 +02001037search_again:
1038 if (start > end)
1039 goto out;
1040 spin_unlock(&tree->lock);
1041 if (gfpflags_allow_blocking(mask))
1042 cond_resched();
1043 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001044
1045out:
Chris Masoncad321a2008-12-17 14:51:42 -05001046 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001047 if (prealloc)
1048 free_extent_state(prealloc);
1049
1050 return err;
1051
Chris Masond1310b22008-01-24 16:13:08 -05001052}
Chris Masond1310b22008-01-24 16:13:08 -05001053
David Sterba41074882013-04-29 13:38:46 +00001054int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001055 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001056 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001057{
1058 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001059 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001060}
1061
1062
Josef Bacik462d6fa2011-09-26 13:56:12 -04001063/**
Liu Bo10983f22012-07-11 15:26:19 +08001064 * convert_extent_bit - convert all bits in a given range from one bit to
1065 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001066 * @tree: the io tree to search
1067 * @start: the start offset in bytes
1068 * @end: the end offset in bytes (inclusive)
1069 * @bits: the bits to set in this range
1070 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001071 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001072 *
1073 * This will go through and set bits for the given range. If any states exist
1074 * already in this range they are set with the given bit and cleared of the
1075 * clear_bits. This is only meant to be used by things that are mergeable, ie
1076 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1077 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001078 *
1079 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001080 */
1081int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001082 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001083 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084{
1085 struct extent_state *state;
1086 struct extent_state *prealloc = NULL;
1087 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001088 struct rb_node **p;
1089 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090 int err = 0;
1091 u64 last_start;
1092 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001093 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094
Josef Bacika5dee372013-12-13 10:02:44 -05001095 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001096
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097again:
David Sterba210aa272016-04-26 23:54:39 +02001098 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001099 /*
1100 * Best effort, don't worry if extent state allocation fails
1101 * here for the first iteration. We might have a cached state
1102 * that matches exactly the target range, in which case no
1103 * extent state allocations are needed. We'll only know this
1104 * after locking the tree.
1105 */
David Sterba210aa272016-04-26 23:54:39 +02001106 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001107 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108 return -ENOMEM;
1109 }
1110
1111 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001112 if (cached_state && *cached_state) {
1113 state = *cached_state;
1114 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001115 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001116 node = &state->rb_node;
1117 goto hit_next;
1118 }
1119 }
1120
Josef Bacik462d6fa2011-09-26 13:56:12 -04001121 /*
1122 * this search will find all the extents that end after
1123 * our range starts.
1124 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001125 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001126 if (!node) {
1127 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001128 if (!prealloc) {
1129 err = -ENOMEM;
1130 goto out;
1131 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001132 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001133 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001134 if (err)
1135 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001136 cache_state(prealloc, cached_state);
1137 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001138 goto out;
1139 }
1140 state = rb_entry(node, struct extent_state, rb_node);
1141hit_next:
1142 last_start = state->start;
1143 last_end = state->end;
1144
1145 /*
1146 * | ---- desired range ---- |
1147 * | state |
1148 *
1149 * Just lock what we found and keep going
1150 */
1151 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001152 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001153 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001154 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001155 if (last_end == (u64)-1)
1156 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001157 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001158 if (start < end && state && state->start == start &&
1159 !need_resched())
1160 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001161 goto search_again;
1162 }
1163
1164 /*
1165 * | ---- desired range ---- |
1166 * | state |
1167 * or
1168 * | ------------- state -------------- |
1169 *
1170 * We need to split the extent we found, and may flip bits on
1171 * second half.
1172 *
1173 * If the extent we found extends past our
1174 * range, we just split and search again. It'll get split
1175 * again the next time though.
1176 *
1177 * If the extent we found is inside our range, we set the
1178 * desired bit on it.
1179 */
1180 if (state->start < start) {
1181 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001182 if (!prealloc) {
1183 err = -ENOMEM;
1184 goto out;
1185 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001186 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001187 if (err)
1188 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001189 prealloc = NULL;
1190 if (err)
1191 goto out;
1192 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001193 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001194 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001195 state = clear_state_bit(tree, state, &clear_bits, 0,
1196 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001197 if (last_end == (u64)-1)
1198 goto out;
1199 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001200 if (start < end && state && state->start == start &&
1201 !need_resched())
1202 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001203 }
1204 goto search_again;
1205 }
1206 /*
1207 * | ---- desired range ---- |
1208 * | state | or | state |
1209 *
1210 * There's a hole, we need to insert something in it and
1211 * ignore the extent we found.
1212 */
1213 if (state->start > start) {
1214 u64 this_end;
1215 if (end < last_start)
1216 this_end = end;
1217 else
1218 this_end = last_start - 1;
1219
1220 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001221 if (!prealloc) {
1222 err = -ENOMEM;
1223 goto out;
1224 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001225
1226 /*
1227 * Avoid to free 'prealloc' if it can be merged with
1228 * the later extent.
1229 */
1230 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001231 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001232 if (err)
1233 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001234 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001235 prealloc = NULL;
1236 start = this_end + 1;
1237 goto search_again;
1238 }
1239 /*
1240 * | ---- desired range ---- |
1241 * | state |
1242 * We need to split the extent, and set the bit
1243 * on the first half
1244 */
1245 if (state->start <= end && state->end > end) {
1246 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001247 if (!prealloc) {
1248 err = -ENOMEM;
1249 goto out;
1250 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001251
1252 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001253 if (err)
1254 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001255
Qu Wenruod38ed272015-10-12 14:53:37 +08001256 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001257 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001258 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001259 prealloc = NULL;
1260 goto out;
1261 }
1262
Josef Bacik462d6fa2011-09-26 13:56:12 -04001263search_again:
1264 if (start > end)
1265 goto out;
1266 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001267 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001268 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001269 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001270
1271out:
1272 spin_unlock(&tree->lock);
1273 if (prealloc)
1274 free_extent_state(prealloc);
1275
1276 return err;
1277}
1278
Chris Masond1310b22008-01-24 16:13:08 -05001279/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001280int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001281 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001282{
1283 /*
1284 * We don't support EXTENT_LOCKED yet, as current changeset will
1285 * record any bits changed, so for EXTENT_LOCKED case, it will
1286 * either fail with -EEXIST or changeset will record the whole
1287 * range.
1288 */
1289 BUG_ON(bits & EXTENT_LOCKED);
1290
David Sterba2c53b912016-04-26 23:54:39 +02001291 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001292 changeset);
1293}
1294
Qu Wenruofefdc552015-10-12 15:35:38 +08001295int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1296 unsigned bits, int wake, int delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001297 struct extent_state **cached)
Qu Wenruofefdc552015-10-12 15:35:38 +08001298{
1299 return __clear_extent_bit(tree, start, end, bits, wake, delete,
David Sterbaae0f1622017-10-31 16:37:52 +01001300 cached, GFP_NOFS, NULL);
Qu Wenruofefdc552015-10-12 15:35:38 +08001301}
1302
Qu Wenruofefdc552015-10-12 15:35:38 +08001303int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001304 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001305{
1306 /*
1307 * Don't support EXTENT_LOCKED case, same reason as
1308 * set_record_extent_bits().
1309 */
1310 BUG_ON(bits & EXTENT_LOCKED);
1311
David Sterbaf734c442016-04-26 23:54:39 +02001312 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001313 changeset);
1314}
1315
Chris Masond352ac62008-09-29 15:18:18 -04001316/*
1317 * either insert or lock state struct between start and end use mask to tell
1318 * us if waiting is desired.
1319 */
Chris Mason1edbb732009-09-02 13:24:36 -04001320int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001321 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001322{
1323 int err;
1324 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001325
Chris Masond1310b22008-01-24 16:13:08 -05001326 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001327 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001328 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001329 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001330 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001331 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1332 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001333 } else
Chris Masond1310b22008-01-24 16:13:08 -05001334 break;
Chris Masond1310b22008-01-24 16:13:08 -05001335 WARN_ON(start > end);
1336 }
1337 return err;
1338}
Chris Masond1310b22008-01-24 16:13:08 -05001339
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001340int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001341{
1342 int err;
1343 u64 failed_start;
1344
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001345 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001346 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001347 if (err == -EEXIST) {
1348 if (failed_start > start)
1349 clear_extent_bit(tree, start, failed_start - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001350 EXTENT_LOCKED, 1, 0, NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001351 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001352 }
Josef Bacik25179202008-10-29 14:49:05 -04001353 return 1;
1354}
Josef Bacik25179202008-10-29 14:49:05 -04001355
David Sterbabd1fa4f2015-12-03 13:08:59 +01001356void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001357{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001358 unsigned long index = start >> PAGE_SHIFT;
1359 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001360 struct page *page;
1361
1362 while (index <= end_index) {
1363 page = find_get_page(inode->i_mapping, index);
1364 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1365 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001366 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001367 index++;
1368 }
Chris Mason4adaa612013-03-26 13:07:00 -04001369}
1370
David Sterbaf6311572015-12-03 13:08:59 +01001371void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001372{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001373 unsigned long index = start >> PAGE_SHIFT;
1374 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001375 struct page *page;
1376
1377 while (index <= end_index) {
1378 page = find_get_page(inode->i_mapping, index);
1379 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001380 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001381 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001383 index++;
1384 }
Chris Mason4adaa612013-03-26 13:07:00 -04001385}
1386
Chris Masond1310b22008-01-24 16:13:08 -05001387/*
Chris Masond1310b22008-01-24 16:13:08 -05001388 * helper function to set both pages and extents in the tree writeback
1389 */
David Sterba35de6db2015-12-03 13:08:59 +01001390static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001391{
Josef Bacikc6100a42017-05-05 11:57:13 -04001392 tree->ops->set_range_writeback(tree->private_data, start, end);
Chris Masond1310b22008-01-24 16:13:08 -05001393}
Chris Masond1310b22008-01-24 16:13:08 -05001394
Chris Masond352ac62008-09-29 15:18:18 -04001395/* find the first state struct with 'bits' set after 'start', and
1396 * return it. tree->lock must be held. NULL will returned if
1397 * nothing was found after 'start'
1398 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001399static struct extent_state *
1400find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001401 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001402{
1403 struct rb_node *node;
1404 struct extent_state *state;
1405
1406 /*
1407 * this search will find all the extents that end after
1408 * our range starts.
1409 */
1410 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001411 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001412 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001413
Chris Masond3977122009-01-05 21:25:51 -05001414 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001415 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001416 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001417 return state;
Chris Masond3977122009-01-05 21:25:51 -05001418
Chris Masond7fc6402008-02-18 12:12:38 -05001419 node = rb_next(node);
1420 if (!node)
1421 break;
1422 }
1423out:
1424 return NULL;
1425}
Chris Masond7fc6402008-02-18 12:12:38 -05001426
Chris Masond352ac62008-09-29 15:18:18 -04001427/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001428 * find the first offset in the io tree with 'bits' set. zero is
1429 * returned if we find something, and *start_ret and *end_ret are
1430 * set to reflect the state struct that was found.
1431 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001432 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001433 */
1434int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001435 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001436 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001437{
1438 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001439 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001440 int ret = 1;
1441
1442 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001443 if (cached_state && *cached_state) {
1444 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001445 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001446 n = rb_next(&state->rb_node);
1447 while (n) {
1448 state = rb_entry(n, struct extent_state,
1449 rb_node);
1450 if (state->state & bits)
1451 goto got_it;
1452 n = rb_next(n);
1453 }
1454 free_extent_state(*cached_state);
1455 *cached_state = NULL;
1456 goto out;
1457 }
1458 free_extent_state(*cached_state);
1459 *cached_state = NULL;
1460 }
1461
Xiao Guangrong69261c42011-07-14 03:19:45 +00001462 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001463got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001464 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001465 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001466 *start_ret = state->start;
1467 *end_ret = state->end;
1468 ret = 0;
1469 }
Josef Bacike6138872012-09-27 17:07:30 -04001470out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001471 spin_unlock(&tree->lock);
1472 return ret;
1473}
1474
1475/*
Chris Masond352ac62008-09-29 15:18:18 -04001476 * find a contiguous range of bytes in the file marked as delalloc, not
1477 * more than 'max_bytes'. start and end are used to return the range,
1478 *
1479 * 1 is returned if we find something, 0 if nothing was in the tree
1480 */
Chris Masonc8b97812008-10-29 14:49:59 -04001481static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001482 u64 *start, u64 *end, u64 max_bytes,
1483 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001484{
1485 struct rb_node *node;
1486 struct extent_state *state;
1487 u64 cur_start = *start;
1488 u64 found = 0;
1489 u64 total_bytes = 0;
1490
Chris Masoncad321a2008-12-17 14:51:42 -05001491 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001492
Chris Masond1310b22008-01-24 16:13:08 -05001493 /*
1494 * this search will find all the extents that end after
1495 * our range starts.
1496 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001497 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001498 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001499 if (!found)
1500 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001501 goto out;
1502 }
1503
Chris Masond3977122009-01-05 21:25:51 -05001504 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001505 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001506 if (found && (state->start != cur_start ||
1507 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001508 goto out;
1509 }
1510 if (!(state->state & EXTENT_DELALLOC)) {
1511 if (!found)
1512 *end = state->end;
1513 goto out;
1514 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001515 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001516 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001517 *cached_state = state;
Elena Reshetovab7ac31b2017-03-03 10:55:19 +02001518 refcount_inc(&state->refs);
Josef Bacikc2a128d2010-02-02 21:19:11 +00001519 }
Chris Masond1310b22008-01-24 16:13:08 -05001520 found++;
1521 *end = state->end;
1522 cur_start = state->end + 1;
1523 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001524 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001525 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001526 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001527 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001528 break;
1529 }
1530out:
Chris Masoncad321a2008-12-17 14:51:42 -05001531 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001532 return found;
1533}
1534
Liu Boda2c7002017-02-10 16:41:05 +01001535static int __process_pages_contig(struct address_space *mapping,
1536 struct page *locked_page,
1537 pgoff_t start_index, pgoff_t end_index,
1538 unsigned long page_ops, pgoff_t *index_ret);
1539
Jeff Mahoney143bede2012-03-01 14:56:26 +01001540static noinline void __unlock_for_delalloc(struct inode *inode,
1541 struct page *locked_page,
1542 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001543{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001544 unsigned long index = start >> PAGE_SHIFT;
1545 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001546
Liu Bo76c00212017-02-10 16:42:14 +01001547 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001548 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001549 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001550
Liu Bo76c00212017-02-10 16:42:14 +01001551 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1552 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001553}
1554
1555static noinline int lock_delalloc_pages(struct inode *inode,
1556 struct page *locked_page,
1557 u64 delalloc_start,
1558 u64 delalloc_end)
1559{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001561 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001562 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001563 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001564
Liu Bo76c00212017-02-10 16:42:14 +01001565 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001566 if (index == locked_page->index && index == end_index)
1567 return 0;
1568
Liu Bo76c00212017-02-10 16:42:14 +01001569 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1570 end_index, PAGE_LOCK, &index_ret);
1571 if (ret == -EAGAIN)
1572 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1573 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001574 return ret;
1575}
1576
1577/*
1578 * find a contiguous range of bytes in the file marked as delalloc, not
1579 * more than 'max_bytes'. start and end are used to return the range,
1580 *
1581 * 1 is returned if we find something, 0 if nothing was in the tree
1582 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001583STATIC u64 find_lock_delalloc_range(struct inode *inode,
1584 struct extent_io_tree *tree,
1585 struct page *locked_page, u64 *start,
1586 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001587{
1588 u64 delalloc_start;
1589 u64 delalloc_end;
1590 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001591 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001592 int ret;
1593 int loops = 0;
1594
1595again:
1596 /* step one, find a bunch of delalloc bytes starting at start */
1597 delalloc_start = *start;
1598 delalloc_end = 0;
1599 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001600 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001601 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001602 *start = delalloc_start;
1603 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001604 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001605 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001606 }
1607
1608 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001609 * start comes from the offset of locked_page. We have to lock
1610 * pages in order, so we can't process delalloc bytes before
1611 * locked_page
1612 */
Chris Masond3977122009-01-05 21:25:51 -05001613 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001614 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001615
1616 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001617 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001618 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001619 if (delalloc_end + 1 - delalloc_start > max_bytes)
1620 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001621
Chris Masonc8b97812008-10-29 14:49:59 -04001622 /* step two, lock all the pages after the page that has start */
1623 ret = lock_delalloc_pages(inode, locked_page,
1624 delalloc_start, delalloc_end);
1625 if (ret == -EAGAIN) {
1626 /* some of the pages are gone, lets avoid looping by
1627 * shortening the size of the delalloc range we're searching
1628 */
Chris Mason9655d292009-09-02 15:22:30 -04001629 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001630 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001631 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001632 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001633 loops = 1;
1634 goto again;
1635 } else {
1636 found = 0;
1637 goto out_failed;
1638 }
1639 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001640 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001641
1642 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001643 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001644
1645 /* then test to make sure it is all still delalloc */
1646 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001647 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001648 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001649 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1650 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001651 __unlock_for_delalloc(inode, locked_page,
1652 delalloc_start, delalloc_end);
1653 cond_resched();
1654 goto again;
1655 }
Chris Mason9655d292009-09-02 15:22:30 -04001656 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001657 *start = delalloc_start;
1658 *end = delalloc_end;
1659out_failed:
1660 return found;
1661}
1662
Liu Boda2c7002017-02-10 16:41:05 +01001663static int __process_pages_contig(struct address_space *mapping,
1664 struct page *locked_page,
1665 pgoff_t start_index, pgoff_t end_index,
1666 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001667{
Liu Bo873695b2017-02-02 17:49:22 -08001668 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001669 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001670 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001671 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001672 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001673 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001674 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001675
Liu Boda2c7002017-02-10 16:41:05 +01001676 if (page_ops & PAGE_LOCK) {
1677 ASSERT(page_ops == PAGE_LOCK);
1678 ASSERT(index_ret && *index_ret == start_index);
1679 }
1680
Filipe Manana704de492014-10-06 22:14:22 +01001681 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001682 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001683
Chris Masond3977122009-01-05 21:25:51 -05001684 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001685 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001686 min_t(unsigned long,
1687 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001688 if (ret == 0) {
1689 /*
1690 * Only if we're going to lock these pages,
1691 * can we find nothing at @index.
1692 */
1693 ASSERT(page_ops & PAGE_LOCK);
Liu Bo49d4a332017-03-06 18:20:56 -08001694 err = -EAGAIN;
1695 goto out;
Liu Boda2c7002017-02-10 16:41:05 +01001696 }
Chris Mason8b62b722009-09-02 16:53:46 -04001697
Liu Boda2c7002017-02-10 16:41:05 +01001698 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001699 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001700 SetPagePrivate2(pages[i]);
1701
Chris Masonc8b97812008-10-29 14:49:59 -04001702 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001703 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001704 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001705 continue;
1706 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001707 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001708 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001709 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001710 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001711 if (page_ops & PAGE_SET_ERROR)
1712 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001713 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001714 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001715 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001716 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001717 if (page_ops & PAGE_LOCK) {
1718 lock_page(pages[i]);
1719 if (!PageDirty(pages[i]) ||
1720 pages[i]->mapping != mapping) {
1721 unlock_page(pages[i]);
1722 put_page(pages[i]);
1723 err = -EAGAIN;
1724 goto out;
1725 }
1726 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001727 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001728 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001729 }
1730 nr_pages -= ret;
1731 index += ret;
1732 cond_resched();
1733 }
Liu Boda2c7002017-02-10 16:41:05 +01001734out:
1735 if (err && index_ret)
1736 *index_ret = start_index + pages_locked - 1;
1737 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001738}
Chris Masonc8b97812008-10-29 14:49:59 -04001739
Liu Bo873695b2017-02-02 17:49:22 -08001740void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1741 u64 delalloc_end, struct page *locked_page,
1742 unsigned clear_bits,
1743 unsigned long page_ops)
1744{
1745 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001746 NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001747
1748 __process_pages_contig(inode->i_mapping, locked_page,
1749 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001750 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001751}
1752
Chris Masond352ac62008-09-29 15:18:18 -04001753/*
1754 * count the number of bytes in the tree that have a given bit(s)
1755 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1756 * cached. The total number found is returned.
1757 */
Chris Masond1310b22008-01-24 16:13:08 -05001758u64 count_range_bits(struct extent_io_tree *tree,
1759 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001760 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001761{
1762 struct rb_node *node;
1763 struct extent_state *state;
1764 u64 cur_start = *start;
1765 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001766 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001767 int found = 0;
1768
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301769 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001770 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001771
Chris Masoncad321a2008-12-17 14:51:42 -05001772 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001773 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1774 total_bytes = tree->dirty_bytes;
1775 goto out;
1776 }
1777 /*
1778 * this search will find all the extents that end after
1779 * our range starts.
1780 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001781 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001782 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001783 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001784
Chris Masond3977122009-01-05 21:25:51 -05001785 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001786 state = rb_entry(node, struct extent_state, rb_node);
1787 if (state->start > search_end)
1788 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001789 if (contig && found && state->start > last + 1)
1790 break;
1791 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001792 total_bytes += min(search_end, state->end) + 1 -
1793 max(cur_start, state->start);
1794 if (total_bytes >= max_bytes)
1795 break;
1796 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001797 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001798 found = 1;
1799 }
Chris Masonec29ed52011-02-23 16:23:20 -05001800 last = state->end;
1801 } else if (contig && found) {
1802 break;
Chris Masond1310b22008-01-24 16:13:08 -05001803 }
1804 node = rb_next(node);
1805 if (!node)
1806 break;
1807 }
1808out:
Chris Masoncad321a2008-12-17 14:51:42 -05001809 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001810 return total_bytes;
1811}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001812
Chris Masond352ac62008-09-29 15:18:18 -04001813/*
1814 * set the private field for a given byte offset in the tree. If there isn't
1815 * an extent_state there already, this does nothing.
1816 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001817static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001818 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001819{
1820 struct rb_node *node;
1821 struct extent_state *state;
1822 int ret = 0;
1823
Chris Masoncad321a2008-12-17 14:51:42 -05001824 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001825 /*
1826 * this search will find all the extents that end after
1827 * our range starts.
1828 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001829 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001830 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001831 ret = -ENOENT;
1832 goto out;
1833 }
1834 state = rb_entry(node, struct extent_state, rb_node);
1835 if (state->start != start) {
1836 ret = -ENOENT;
1837 goto out;
1838 }
David Sterba47dc1962016-02-11 13:24:13 +01001839 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001840out:
Chris Masoncad321a2008-12-17 14:51:42 -05001841 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001842 return ret;
1843}
1844
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001845static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001846 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001847{
1848 struct rb_node *node;
1849 struct extent_state *state;
1850 int ret = 0;
1851
Chris Masoncad321a2008-12-17 14:51:42 -05001852 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001853 /*
1854 * this search will find all the extents that end after
1855 * our range starts.
1856 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001857 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001858 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001859 ret = -ENOENT;
1860 goto out;
1861 }
1862 state = rb_entry(node, struct extent_state, rb_node);
1863 if (state->start != start) {
1864 ret = -ENOENT;
1865 goto out;
1866 }
David Sterba47dc1962016-02-11 13:24:13 +01001867 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001868out:
Chris Masoncad321a2008-12-17 14:51:42 -05001869 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001870 return ret;
1871}
1872
1873/*
1874 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001875 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001876 * has the bits set. Otherwise, 1 is returned if any bit in the
1877 * range is found set.
1878 */
1879int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001880 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001881{
1882 struct extent_state *state = NULL;
1883 struct rb_node *node;
1884 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001885
Chris Masoncad321a2008-12-17 14:51:42 -05001886 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001887 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001888 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001889 node = &cached->rb_node;
1890 else
1891 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001892 while (node && start <= end) {
1893 state = rb_entry(node, struct extent_state, rb_node);
1894
1895 if (filled && state->start > start) {
1896 bitset = 0;
1897 break;
1898 }
1899
1900 if (state->start > end)
1901 break;
1902
1903 if (state->state & bits) {
1904 bitset = 1;
1905 if (!filled)
1906 break;
1907 } else if (filled) {
1908 bitset = 0;
1909 break;
1910 }
Chris Mason46562ce2009-09-23 20:23:16 -04001911
1912 if (state->end == (u64)-1)
1913 break;
1914
Chris Masond1310b22008-01-24 16:13:08 -05001915 start = state->end + 1;
1916 if (start > end)
1917 break;
1918 node = rb_next(node);
1919 if (!node) {
1920 if (filled)
1921 bitset = 0;
1922 break;
1923 }
1924 }
Chris Masoncad321a2008-12-17 14:51:42 -05001925 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001926 return bitset;
1927}
Chris Masond1310b22008-01-24 16:13:08 -05001928
1929/*
1930 * helper function to set a given page up to date if all the
1931 * extents in the tree for that page are up to date
1932 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001933static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001934{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001935 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001936 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001937 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001938 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001939}
1940
Josef Bacik7870d082017-05-05 11:57:15 -04001941int free_io_failure(struct extent_io_tree *failure_tree,
1942 struct extent_io_tree *io_tree,
1943 struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001944{
1945 int ret;
1946 int err = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001947
David Sterba47dc1962016-02-11 13:24:13 +01001948 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001949 ret = clear_extent_bits(failure_tree, rec->start,
1950 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001951 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001952 if (ret)
1953 err = ret;
1954
Josef Bacik7870d082017-05-05 11:57:15 -04001955 ret = clear_extent_bits(io_tree, rec->start,
David Woodhouse53b381b2013-01-29 18:40:14 -05001956 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001957 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001958 if (ret && !err)
1959 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001960
1961 kfree(rec);
1962 return err;
1963}
1964
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001965/*
1966 * this bypasses the standard btrfs submit functions deliberately, as
1967 * the standard behavior is to write all copies in a raid setup. here we only
1968 * want to write the one bad copy. so we do the mapping for ourselves and issue
1969 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001970 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001971 * actually prevents the read that triggered the error from finishing.
1972 * currently, there can be no more than two copies of every data bit. thus,
1973 * exactly one rewrite is required.
1974 */
Josef Bacik6ec656b2017-05-05 11:57:14 -04001975int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
1976 u64 length, u64 logical, struct page *page,
1977 unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001978{
1979 struct bio *bio;
1980 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001981 u64 map_length = 0;
1982 u64 sector;
1983 struct btrfs_bio *bbio = NULL;
1984 int ret;
1985
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001986 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001987 BUG_ON(!mirror_num);
1988
David Sterbac5e4c3d2017-06-12 17:29:41 +02001989 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001990 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991 map_length = length;
1992
Filipe Mananab5de8d02016-05-27 22:21:27 +01001993 /*
1994 * Avoid races with device replace and make sure our bbio has devices
1995 * associated to its stripes that don't go away while we are doing the
1996 * read repair operation.
1997 */
1998 btrfs_bio_counter_inc_blocked(fs_info);
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03001999 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
Liu Boc7253282017-03-29 10:53:58 -07002000 /*
2001 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
2002 * to update all raid stripes, but here we just want to correct
2003 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
2004 * stripe's dev and sector.
2005 */
2006 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
2007 &map_length, &bbio, 0);
2008 if (ret) {
2009 btrfs_bio_counter_dec(fs_info);
2010 bio_put(bio);
2011 return -EIO;
2012 }
2013 ASSERT(bbio->mirror_num == 1);
2014 } else {
2015 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
2016 &map_length, &bbio, mirror_num);
2017 if (ret) {
2018 btrfs_bio_counter_dec(fs_info);
2019 bio_put(bio);
2020 return -EIO;
2021 }
2022 BUG_ON(mirror_num != bbio->mirror_num);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002023 }
Liu Boc7253282017-03-29 10:53:58 -07002024
2025 sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002026 bio->bi_iter.bi_sector = sector;
Liu Boc7253282017-03-29 10:53:58 -07002027 dev = bbio->stripes[bbio->mirror_num - 1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002028 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002029 if (!dev || !dev->bdev || !dev->writeable) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002030 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002031 bio_put(bio);
2032 return -EIO;
2033 }
Christoph Hellwig74d46992017-08-23 19:10:32 +02002034 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002035 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002036 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002037
Mike Christie4e49ea42016-06-05 14:31:41 -05002038 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002039 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002040 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002041 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002042 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002043 return -EIO;
2044 }
2045
David Sterbab14af3b2015-10-08 10:43:10 +02002046 btrfs_info_rl_in_rcu(fs_info,
2047 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Josef Bacik6ec656b2017-05-05 11:57:14 -04002048 ino, start,
Miao Xie1203b682014-09-12 18:44:01 +08002049 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002050 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051 bio_put(bio);
2052 return 0;
2053}
2054
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002055int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2056 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002057{
Josef Bacikea466792012-03-26 21:57:36 -04002058 u64 start = eb->start;
2059 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002060 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002061
David Howellsbc98a422017-07-17 08:45:34 +01002062 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002063 return -EROFS;
2064
Josef Bacikea466792012-03-26 21:57:36 -04002065 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002066 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002067
Josef Bacik6ec656b2017-05-05 11:57:14 -04002068 ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002069 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002070 if (ret)
2071 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002073 }
2074
2075 return ret;
2076}
2077
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002078/*
2079 * each time an IO finishes, we do a fast check in the IO failure tree
2080 * to see if we need to process or clean up an io_failure_record
2081 */
Josef Bacik7870d082017-05-05 11:57:15 -04002082int clean_io_failure(struct btrfs_fs_info *fs_info,
2083 struct extent_io_tree *failure_tree,
2084 struct extent_io_tree *io_tree, u64 start,
2085 struct page *page, u64 ino, unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086{
2087 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002089 struct extent_state *state;
2090 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002091 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092
2093 private = 0;
Josef Bacik7870d082017-05-05 11:57:15 -04002094 ret = count_range_bits(failure_tree, &private, (u64)-1, 1,
2095 EXTENT_DIRTY, 0);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096 if (!ret)
2097 return 0;
2098
Josef Bacik7870d082017-05-05 11:57:15 -04002099 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002100 if (ret)
2101 return 0;
2102
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002103 BUG_ON(!failrec->this_mirror);
2104
2105 if (failrec->in_validation) {
2106 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002107 btrfs_debug(fs_info,
2108 "clean_io_failure: freeing dummy error at %llu",
2109 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 goto out;
2111 }
David Howellsbc98a422017-07-17 08:45:34 +01002112 if (sb_rdonly(fs_info->sb))
Ilya Dryomov908960c2013-11-03 19:06:39 +02002113 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002114
Josef Bacik7870d082017-05-05 11:57:15 -04002115 spin_lock(&io_tree->lock);
2116 state = find_first_extent_bit_state(io_tree,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117 failrec->start,
2118 EXTENT_LOCKED);
Josef Bacik7870d082017-05-05 11:57:15 -04002119 spin_unlock(&io_tree->lock);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120
Miao Xie883d0de2013-07-25 19:22:35 +08002121 if (state && state->start <= failrec->start &&
2122 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002123 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2124 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002125 if (num_copies > 1) {
Josef Bacik7870d082017-05-05 11:57:15 -04002126 repair_io_failure(fs_info, ino, start, failrec->len,
2127 failrec->logical, page, pg_offset,
2128 failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002129 }
2130 }
2131
2132out:
Josef Bacik7870d082017-05-05 11:57:15 -04002133 free_io_failure(failure_tree, io_tree, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002134
Miao Xie454ff3d2014-09-12 18:43:58 +08002135 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002136}
2137
Miao Xief6124962014-09-12 18:44:04 +08002138/*
2139 * Can be called when
2140 * - hold extent lock
2141 * - under ordered extent
2142 * - the inode is freeing
2143 */
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002144void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
Miao Xief6124962014-09-12 18:44:04 +08002145{
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002146 struct extent_io_tree *failure_tree = &inode->io_failure_tree;
Miao Xief6124962014-09-12 18:44:04 +08002147 struct io_failure_record *failrec;
2148 struct extent_state *state, *next;
2149
2150 if (RB_EMPTY_ROOT(&failure_tree->state))
2151 return;
2152
2153 spin_lock(&failure_tree->lock);
2154 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2155 while (state) {
2156 if (state->start > end)
2157 break;
2158
2159 ASSERT(state->end <= end);
2160
2161 next = next_state(state);
2162
David Sterba47dc1962016-02-11 13:24:13 +01002163 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002164 free_extent_state(state);
2165 kfree(failrec);
2166
2167 state = next;
2168 }
2169 spin_unlock(&failure_tree->lock);
2170}
2171
Miao Xie2fe63032014-09-12 18:43:59 +08002172int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002173 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002174{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002175 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002176 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002178 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2179 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2180 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002181 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002182 u64 logical;
2183
David Sterba47dc1962016-02-11 13:24:13 +01002184 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002185 if (ret) {
2186 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2187 if (!failrec)
2188 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002189
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002190 failrec->start = start;
2191 failrec->len = end - start + 1;
2192 failrec->this_mirror = 0;
2193 failrec->bio_flags = 0;
2194 failrec->in_validation = 0;
2195
2196 read_lock(&em_tree->lock);
2197 em = lookup_extent_mapping(em_tree, start, failrec->len);
2198 if (!em) {
2199 read_unlock(&em_tree->lock);
2200 kfree(failrec);
2201 return -EIO;
2202 }
2203
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002204 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205 free_extent_map(em);
2206 em = NULL;
2207 }
2208 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002209 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002210 kfree(failrec);
2211 return -EIO;
2212 }
Miao Xie2fe63032014-09-12 18:43:59 +08002213
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002214 logical = start - em->start;
2215 logical = em->block_start + logical;
2216 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2217 logical = em->block_start;
2218 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2219 extent_set_compress_type(&failrec->bio_flags,
2220 em->compress_type);
2221 }
Miao Xie2fe63032014-09-12 18:43:59 +08002222
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002223 btrfs_debug(fs_info,
2224 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2225 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002226
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002227 failrec->logical = logical;
2228 free_extent_map(em);
2229
2230 /* set the bits in the private failure tree */
2231 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002232 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002233 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002234 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002235 /* set the bits in the inode's tree */
2236 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002237 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002238 if (ret < 0) {
2239 kfree(failrec);
2240 return ret;
2241 }
2242 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002243 btrfs_debug(fs_info,
2244 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2245 failrec->logical, failrec->start, failrec->len,
2246 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002247 /*
2248 * when data can be on disk more than twice, add to failrec here
2249 * (e.g. with a list for failed_mirror) to make
2250 * clean_io_failure() clean all those errors at once.
2251 */
2252 }
Miao Xie2fe63032014-09-12 18:43:59 +08002253
2254 *failrec_ret = failrec;
2255
2256 return 0;
2257}
2258
Liu Boc3cfb652017-07-13 15:00:50 -07002259bool btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
Miao Xie2fe63032014-09-12 18:43:59 +08002260 struct io_failure_record *failrec, int failed_mirror)
2261{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002263 int num_copies;
2264
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002265 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002266 if (num_copies == 1) {
2267 /*
2268 * we only have a single copy of the data, so don't bother with
2269 * all the retry and error correction code that follows. no
2270 * matter what the error is, it is very likely to persist.
2271 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002272 btrfs_debug(fs_info,
2273 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2274 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002275 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 }
2277
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002278 /*
2279 * there are two premises:
2280 * a) deliver good data to the caller
2281 * b) correct the bad sectors on disk
2282 */
2283 if (failed_bio->bi_vcnt > 1) {
2284 /*
2285 * to fulfill b), we need to know the exact failing sectors, as
2286 * we don't want to rewrite any more than the failed ones. thus,
2287 * we need separate read requests for the failed bio
2288 *
2289 * if the following BUG_ON triggers, our validation request got
2290 * merged. we need separate requests for our algorithm to work.
2291 */
2292 BUG_ON(failrec->in_validation);
2293 failrec->in_validation = 1;
2294 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002295 } else {
2296 /*
2297 * we're ready to fulfill a) and b) alongside. get a good copy
2298 * of the failed sector and if we succeed, we have setup
2299 * everything for repair_io_failure to do the rest for us.
2300 */
2301 if (failrec->in_validation) {
2302 BUG_ON(failrec->this_mirror != failed_mirror);
2303 failrec->in_validation = 0;
2304 failrec->this_mirror = 0;
2305 }
2306 failrec->failed_mirror = failed_mirror;
2307 failrec->this_mirror++;
2308 if (failrec->this_mirror == failed_mirror)
2309 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002310 }
2311
Miao Xiefacc8a222013-07-25 19:22:34 +08002312 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002313 btrfs_debug(fs_info,
2314 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2315 num_copies, failrec->this_mirror, failed_mirror);
Liu Boc3cfb652017-07-13 15:00:50 -07002316 return false;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002317 }
2318
Liu Boc3cfb652017-07-13 15:00:50 -07002319 return true;
Miao Xie2fe63032014-09-12 18:43:59 +08002320}
2321
2322
2323struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2324 struct io_failure_record *failrec,
2325 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002326 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002327{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002329 struct bio *bio;
2330 struct btrfs_io_bio *btrfs_failed_bio;
2331 struct btrfs_io_bio *btrfs_bio;
2332
David Sterbac5e4c3d2017-06-12 17:29:41 +02002333 bio = btrfs_io_bio_alloc(1);
Miao Xie2fe63032014-09-12 18:43:59 +08002334 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002335 bio->bi_iter.bi_sector = failrec->logical >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002336 bio_set_dev(bio, fs_info->fs_devices->latest_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002337 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002338 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002339
Miao Xiefacc8a222013-07-25 19:22:34 +08002340 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2341 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002342 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2343
2344 btrfs_bio = btrfs_io_bio(bio);
2345 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002346 icsum *= csum_size;
2347 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002348 csum_size);
2349 }
2350
Miao Xie2fe63032014-09-12 18:43:59 +08002351 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002352
Miao Xie2fe63032014-09-12 18:43:59 +08002353 return bio;
2354}
2355
2356/*
2357 * this is a generic handler for readpage errors (default
2358 * readpage_io_failed_hook). if other copies exist, read those and write back
2359 * good data to the failed position. does not investigate in remapping the
2360 * failed extent elsewhere, hoping the device will be smart enough to do this as
2361 * needed
2362 */
2363
2364static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2365 struct page *page, u64 start, u64 end,
2366 int failed_mirror)
2367{
2368 struct io_failure_record *failrec;
2369 struct inode *inode = page->mapping->host;
2370 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002371 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie2fe63032014-09-12 18:43:59 +08002372 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002373 int read_mode = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002374 blk_status_t status;
Miao Xie2fe63032014-09-12 18:43:59 +08002375 int ret;
2376
Mike Christie1f7ad752016-06-05 14:31:51 -05002377 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002378
2379 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2380 if (ret)
2381 return ret;
2382
Liu Boc3cfb652017-07-13 15:00:50 -07002383 if (!btrfs_check_repairable(inode, failed_bio, failrec,
2384 failed_mirror)) {
Josef Bacik7870d082017-05-05 11:57:15 -04002385 free_io_failure(failure_tree, tree, failrec);
Miao Xie2fe63032014-09-12 18:43:59 +08002386 return -EIO;
2387 }
2388
2389 if (failed_bio->bi_vcnt > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002390 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002391
2392 phy_offset >>= inode->i_sb->s_blocksize_bits;
2393 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2394 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002395 (int)phy_offset, failed_bio->bi_end_io,
2396 NULL);
Mike Christie1f7ad752016-06-05 14:31:51 -05002397 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie2fe63032014-09-12 18:43:59 +08002398
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002399 btrfs_debug(btrfs_sb(inode->i_sb),
2400 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2401 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002402
Linus Torvalds8c27cb32017-07-05 16:41:23 -07002403 status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002404 failrec->bio_flags, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002405 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04002406 free_io_failure(failure_tree, tree, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002407 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002408 ret = blk_status_to_errno(status);
Miao Xie6c387ab2014-09-12 18:43:57 +08002409 }
2410
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002411 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002412}
2413
Chris Masond1310b22008-01-24 16:13:08 -05002414/* lots and lots of room for performance fixes in the end_bio funcs */
2415
David Sterbab5227c02015-12-03 13:08:59 +01002416void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002417{
2418 int uptodate = (err == 0);
2419 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002420 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002421
2422 tree = &BTRFS_I(page->mapping->host)->io_tree;
2423
David Sterbac3988d62017-02-17 15:18:32 +01002424 if (tree->ops && tree->ops->writepage_end_io_hook)
2425 tree->ops->writepage_end_io_hook(page, start, end, NULL,
2426 uptodate);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002427
Jeff Mahoney87826df2012-02-15 16:23:57 +01002428 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002429 ClearPageUptodate(page);
2430 SetPageError(page);
Colin Ian Kingbff5baf2017-05-09 18:14:01 +01002431 ret = err < 0 ? err : -EIO;
Liu Bo5dca6ee2014-05-12 12:47:36 +08002432 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002433 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002434}
2435
Chris Masond1310b22008-01-24 16:13:08 -05002436/*
2437 * after a writepage IO is done, we need to:
2438 * clear the uptodate bits on error
2439 * clear the writeback bits in the extent tree for this IO
2440 * end_page_writeback if the page has no more pending IO
2441 *
2442 * Scheduling is not allowed, so the extent state tree is expected
2443 * to have one and only one object corresponding to this IO.
2444 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002445static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002446{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002447 int error = blk_status_to_errno(bio->bi_status);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002448 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002449 u64 start;
2450 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002451 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002452
David Sterbac09abff2017-07-13 18:10:07 +02002453 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002454 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002455 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 struct inode *inode = page->mapping->host;
2457 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002458
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002459 /* We always issue full-page reads, but if some block
2460 * in a page fails to read, blk_update_request() will
2461 * advance bv_offset and adjust bv_len to compensate.
2462 * Print a warning for nonzero offsets, and an error
2463 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002464 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2465 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002466 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002467 "partial page write in btrfs with offset %u and length %u",
2468 bvec->bv_offset, bvec->bv_len);
2469 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002471 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002472 bvec->bv_offset, bvec->bv_len);
2473 }
Chris Masond1310b22008-01-24 16:13:08 -05002474
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002475 start = page_offset(page);
2476 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002477
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002478 end_extent_writepage(page, error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002479 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002480 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002481
Chris Masond1310b22008-01-24 16:13:08 -05002482 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002483}
2484
Miao Xie883d0de2013-07-25 19:22:35 +08002485static void
2486endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2487 int uptodate)
2488{
2489 struct extent_state *cached = NULL;
2490 u64 end = start + len - 1;
2491
2492 if (uptodate && tree->track_uptodate)
2493 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2494 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2495}
2496
Chris Masond1310b22008-01-24 16:13:08 -05002497/*
2498 * after a readpage IO is done, we need to:
2499 * clear the uptodate bits on error
2500 * set the uptodate bits if things worked
2501 * set the page up to date if all extents in the tree are uptodate
2502 * clear the lock bit in the extent tree
2503 * unlock the page if there are no other extents locked for it
2504 *
2505 * Scheduling is not allowed, so the extent state tree is expected
2506 * to have one and only one object corresponding to this IO.
2507 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002508static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002509{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002510 struct bio_vec *bvec;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002511 int uptodate = !bio->bi_status;
Miao Xiefacc8a222013-07-25 19:22:34 +08002512 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04002513 struct extent_io_tree *tree, *failure_tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002514 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002515 u64 start;
2516 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002517 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002518 u64 extent_start = 0;
2519 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002520 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002521 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002522 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002523
David Sterbac09abff2017-07-13 18:10:07 +02002524 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08002525 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002526 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002527 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002528 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002529
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002530 btrfs_debug(fs_info,
2531 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002532 (u64)bio->bi_iter.bi_sector, bio->bi_status,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002533 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002534 tree = &BTRFS_I(inode)->io_tree;
Josef Bacik7870d082017-05-05 11:57:15 -04002535 failure_tree = &BTRFS_I(inode)->io_failure_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002536
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002537 /* We always issue full-page reads, but if some block
2538 * in a page fails to read, blk_update_request() will
2539 * advance bv_offset and adjust bv_len to compensate.
2540 * Print a warning for nonzero offsets, and an error
2541 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002542 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2543 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002544 btrfs_err(fs_info,
2545 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002546 bvec->bv_offset, bvec->bv_len);
2547 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002548 btrfs_info(fs_info,
2549 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002550 bvec->bv_offset, bvec->bv_len);
2551 }
Chris Masond1310b22008-01-24 16:13:08 -05002552
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002553 start = page_offset(page);
2554 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002555 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002556
Chris Mason9be33952013-05-17 18:30:14 -04002557 mirror = io_bio->mirror_num;
David Sterba20c98012017-02-17 15:59:35 +01002558 if (likely(uptodate && tree->ops)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002559 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2560 page, start, end,
2561 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002562 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002563 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002564 else
Josef Bacik7870d082017-05-05 11:57:15 -04002565 clean_io_failure(BTRFS_I(inode)->root->fs_info,
2566 failure_tree, tree, start,
2567 page,
2568 btrfs_ino(BTRFS_I(inode)), 0);
Chris Masond1310b22008-01-24 16:13:08 -05002569 }
Josef Bacikea466792012-03-26 21:57:36 -04002570
Miao Xief2a09da2013-07-25 19:22:33 +08002571 if (likely(uptodate))
2572 goto readpage_ok;
2573
David Sterba20a7db82017-02-17 16:24:29 +01002574 if (tree->ops) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002575 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Liu Bo9d0d1c82017-03-24 15:04:50 -07002576 if (ret == -EAGAIN) {
2577 /*
2578 * Data inode's readpage_io_failed_hook() always
2579 * returns -EAGAIN.
2580 *
2581 * The generic bio_readpage_error handles errors
2582 * the following way: If possible, new read
2583 * requests are created and submitted and will
2584 * end up in end_bio_extent_readpage as well (if
2585 * we're lucky, not in the !uptodate case). In
2586 * that case it returns 0 and we just go on with
2587 * the next page in our bio. If it can't handle
2588 * the error it will return -EIO and we remain
2589 * responsible for that page.
2590 */
2591 ret = bio_readpage_error(bio, offset, page,
2592 start, end, mirror);
2593 if (ret == 0) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002594 uptodate = !bio->bi_status;
Liu Bo9d0d1c82017-03-24 15:04:50 -07002595 offset += len;
2596 continue;
2597 }
Chris Mason7e383262008-04-09 16:28:12 -04002598 }
Liu Bo9d0d1c82017-03-24 15:04:50 -07002599
2600 /*
2601 * metadata's readpage_io_failed_hook() always returns
2602 * -EIO and fixes nothing. -EIO is also returned if
2603 * data inode error could not be fixed.
2604 */
2605 ASSERT(ret == -EIO);
Chris Mason7e383262008-04-09 16:28:12 -04002606 }
Miao Xief2a09da2013-07-25 19:22:33 +08002607readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002608 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002609 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002610 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002611 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002612
2613 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002614 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002615 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002616 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002617 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002618 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002619 ClearPageUptodate(page);
2620 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002621 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002622 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002623 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002624
2625 if (unlikely(!uptodate)) {
2626 if (extent_len) {
2627 endio_readpage_release_extent(tree,
2628 extent_start,
2629 extent_len, 1);
2630 extent_start = 0;
2631 extent_len = 0;
2632 }
2633 endio_readpage_release_extent(tree, start,
2634 end - start + 1, 0);
2635 } else if (!extent_len) {
2636 extent_start = start;
2637 extent_len = end + 1 - start;
2638 } else if (extent_start + extent_len == start) {
2639 extent_len += end + 1 - start;
2640 } else {
2641 endio_readpage_release_extent(tree, extent_start,
2642 extent_len, uptodate);
2643 extent_start = start;
2644 extent_len = end + 1 - start;
2645 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002646 }
Chris Masond1310b22008-01-24 16:13:08 -05002647
Miao Xie883d0de2013-07-25 19:22:35 +08002648 if (extent_len)
2649 endio_readpage_release_extent(tree, extent_start, extent_len,
2650 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002651 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002652 io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
Chris Masond1310b22008-01-24 16:13:08 -05002653 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002654}
2655
Chris Mason9be33952013-05-17 18:30:14 -04002656/*
David Sterba184f9992017-06-12 17:29:39 +02002657 * Initialize the members up to but not including 'bio'. Use after allocating a
2658 * new bio by bio_alloc_bioset as it does not initialize the bytes outside of
2659 * 'bio' because use of __GFP_ZERO is not supported.
Chris Mason9be33952013-05-17 18:30:14 -04002660 */
David Sterba184f9992017-06-12 17:29:39 +02002661static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
Chris Masond1310b22008-01-24 16:13:08 -05002662{
David Sterba184f9992017-06-12 17:29:39 +02002663 memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio));
2664}
2665
2666/*
David Sterba6e707bc2017-06-02 17:26:26 +02002667 * The following helpers allocate a bio. As it's backed by a bioset, it'll
2668 * never fail. We're returning a bio right now but you can call btrfs_io_bio
2669 * for the appropriate container_of magic
Chris Masond1310b22008-01-24 16:13:08 -05002670 */
David Sterbac821e7f32017-06-02 18:35:36 +02002671struct bio *btrfs_bio_alloc(struct block_device *bdev, u64 first_byte)
Chris Masond1310b22008-01-24 16:13:08 -05002672{
2673 struct bio *bio;
2674
David Sterba9f2179a2017-06-02 17:55:44 +02002675 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, btrfs_bioset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002676 bio_set_dev(bio, bdev);
David Sterbac821e7f32017-06-02 18:35:36 +02002677 bio->bi_iter.bi_sector = first_byte >> 9;
David Sterba184f9992017-06-12 17:29:39 +02002678 btrfs_io_bio_init(btrfs_io_bio(bio));
Chris Masond1310b22008-01-24 16:13:08 -05002679 return bio;
2680}
2681
David Sterba8b6c1d52017-06-02 17:48:13 +02002682struct bio *btrfs_bio_clone(struct bio *bio)
Chris Mason9be33952013-05-17 18:30:14 -04002683{
Miao Xie23ea8e52014-09-12 18:43:54 +08002684 struct btrfs_io_bio *btrfs_bio;
2685 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002686
David Sterba6e707bc2017-06-02 17:26:26 +02002687 /* Bio allocation backed by a bioset does not fail */
David Sterba8b6c1d52017-06-02 17:48:13 +02002688 new = bio_clone_fast(bio, GFP_NOFS, btrfs_bioset);
David Sterba6e707bc2017-06-02 17:26:26 +02002689 btrfs_bio = btrfs_io_bio(new);
David Sterba184f9992017-06-12 17:29:39 +02002690 btrfs_io_bio_init(btrfs_bio);
David Sterba6e707bc2017-06-02 17:26:26 +02002691 btrfs_bio->iter = bio->bi_iter;
Miao Xie23ea8e52014-09-12 18:43:54 +08002692 return new;
2693}
Chris Mason9be33952013-05-17 18:30:14 -04002694
David Sterbac5e4c3d2017-06-12 17:29:41 +02002695struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
Chris Mason9be33952013-05-17 18:30:14 -04002696{
Miao Xiefacc8a222013-07-25 19:22:34 +08002697 struct bio *bio;
2698
David Sterba6e707bc2017-06-02 17:26:26 +02002699 /* Bio allocation backed by a bioset does not fail */
David Sterbac5e4c3d2017-06-12 17:29:41 +02002700 bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, btrfs_bioset);
David Sterba184f9992017-06-12 17:29:39 +02002701 btrfs_io_bio_init(btrfs_io_bio(bio));
Miao Xiefacc8a222013-07-25 19:22:34 +08002702 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002703}
2704
Liu Boe4770942017-05-16 10:57:14 -07002705struct bio *btrfs_bio_clone_partial(struct bio *orig, int offset, int size)
Liu Bo2f8e9142017-05-15 17:43:31 -07002706{
2707 struct bio *bio;
2708 struct btrfs_io_bio *btrfs_bio;
2709
2710 /* this will never fail when it's backed by a bioset */
Liu Boe4770942017-05-16 10:57:14 -07002711 bio = bio_clone_fast(orig, GFP_NOFS, btrfs_bioset);
Liu Bo2f8e9142017-05-15 17:43:31 -07002712 ASSERT(bio);
2713
2714 btrfs_bio = btrfs_io_bio(bio);
David Sterba184f9992017-06-12 17:29:39 +02002715 btrfs_io_bio_init(btrfs_bio);
Liu Bo2f8e9142017-05-15 17:43:31 -07002716
2717 bio_trim(bio, offset >> 9, size >> 9);
Liu Bo17347ce2017-05-15 15:33:27 -07002718 btrfs_bio->iter = bio->bi_iter;
Liu Bo2f8e9142017-05-15 17:43:31 -07002719 return bio;
2720}
Chris Mason9be33952013-05-17 18:30:14 -04002721
Mike Christie1f7ad752016-06-05 14:31:51 -05002722static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2723 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002724{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002725 blk_status_t ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002726 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2727 struct page *page = bvec->bv_page;
2728 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002729 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002730
Miao Xie4eee4fa2012-12-21 09:17:45 +00002731 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002732
David Woodhouse902b22f2008-08-20 08:51:49 -04002733 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002734 bio_get(bio);
2735
David Sterba20c98012017-02-17 15:59:35 +01002736 if (tree->ops)
Josef Bacikc6100a42017-05-05 11:57:13 -04002737 ret = tree->ops->submit_bio_hook(tree->private_data, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002738 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002739 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002740 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002741
Chris Masond1310b22008-01-24 16:13:08 -05002742 bio_put(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002743 return blk_status_to_errno(ret);
Chris Masond1310b22008-01-24 16:13:08 -05002744}
2745
Mike Christie1f7ad752016-06-05 14:31:51 -05002746static int merge_bio(struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002747 unsigned long offset, size_t size, struct bio *bio,
2748 unsigned long bio_flags)
2749{
2750 int ret = 0;
David Sterba20c98012017-02-17 15:59:35 +01002751 if (tree->ops)
Mike Christie81a75f62016-06-05 14:31:54 -05002752 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002753 bio_flags);
Jeff Mahoney3444a972011-10-03 23:23:13 -04002754 return ret;
2755
2756}
2757
David Sterba4b81ba42017-06-06 19:14:26 +02002758/*
2759 * @opf: bio REQ_OP_* and REQ_* flags as one value
2760 */
2761static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002762 struct writeback_control *wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02002763 struct page *page, u64 offset,
David Sterba6c5a4e22017-10-04 17:10:34 +02002764 size_t size, unsigned long pg_offset,
Chris Masond1310b22008-01-24 16:13:08 -05002765 struct block_device *bdev,
2766 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002767 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002768 int mirror_num,
2769 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002770 unsigned long bio_flags,
2771 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002772{
2773 int ret = 0;
2774 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002775 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002776 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002777 size_t page_size = min_t(size_t, size, PAGE_SIZE);
David Sterba6273b7f2017-10-04 17:30:11 +02002778 sector_t sector = offset >> 9;
Chris Masond1310b22008-01-24 16:13:08 -05002779
2780 if (bio_ret && *bio_ret) {
2781 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002782 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002783 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002784 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002785 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002786
2787 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002788 force_bio_submit ||
David Sterba6c5a4e22017-10-04 17:10:34 +02002789 merge_bio(tree, page, pg_offset, page_size, bio, bio_flags) ||
2790 bio_add_page(bio, page, page_size, pg_offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002791 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002792 if (ret < 0) {
2793 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002794 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002795 }
Chris Masond1310b22008-01-24 16:13:08 -05002796 bio = NULL;
2797 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002798 if (wbc)
2799 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002800 return 0;
2801 }
2802 }
Chris Masonc8b97812008-10-29 14:49:59 -04002803
David Sterba6273b7f2017-10-04 17:30:11 +02002804 bio = btrfs_bio_alloc(bdev, offset);
David Sterba6c5a4e22017-10-04 17:10:34 +02002805 bio_add_page(bio, page, page_size, pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002806 bio->bi_end_io = end_io_func;
2807 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002808 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002809 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002810 if (wbc) {
2811 wbc_init_bio(wbc, bio);
2812 wbc_account_io(wbc, page, page_size);
2813 }
Chris Mason70dec802008-01-29 09:59:12 -05002814
Chris Masond3977122009-01-05 21:25:51 -05002815 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002816 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002817 else
Mike Christie1f7ad752016-06-05 14:31:51 -05002818 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002819
2820 return ret;
2821}
2822
Eric Sandeen48a3b632013-04-25 20:41:01 +00002823static void attach_extent_buffer_page(struct extent_buffer *eb,
2824 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002825{
2826 if (!PagePrivate(page)) {
2827 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002828 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002829 set_page_private(page, (unsigned long)eb);
2830 } else {
2831 WARN_ON(page->private != (unsigned long)eb);
2832 }
2833}
2834
Chris Masond1310b22008-01-24 16:13:08 -05002835void set_page_extent_mapped(struct page *page)
2836{
2837 if (!PagePrivate(page)) {
2838 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002839 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002840 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002841 }
2842}
2843
Miao Xie125bac012013-07-25 19:22:37 +08002844static struct extent_map *
2845__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2846 u64 start, u64 len, get_extent_t *get_extent,
2847 struct extent_map **em_cached)
2848{
2849 struct extent_map *em;
2850
2851 if (em_cached && *em_cached) {
2852 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002853 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002854 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002855 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002856 return em;
2857 }
2858
2859 free_extent_map(em);
2860 *em_cached = NULL;
2861 }
2862
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02002863 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002864 if (em_cached && !IS_ERR_OR_NULL(em)) {
2865 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002866 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002867 *em_cached = em;
2868 }
2869 return em;
2870}
Chris Masond1310b22008-01-24 16:13:08 -05002871/*
2872 * basic readpage implementation. Locked extent state structs are inserted
2873 * into the tree that are removed when the IO is done (by the end_io
2874 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002875 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002876 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002877 */
Miao Xie99740902013-07-25 19:22:36 +08002878static int __do_readpage(struct extent_io_tree *tree,
2879 struct page *page,
2880 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002881 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002882 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002883 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002884 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002885{
2886 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002887 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002888 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002889 u64 end;
2890 u64 cur = start;
2891 u64 extent_offset;
2892 u64 last_byte = i_size_read(inode);
2893 u64 block_start;
2894 u64 cur_end;
Chris Masond1310b22008-01-24 16:13:08 -05002895 struct extent_map *em;
2896 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002897 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002898 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002899 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002900 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002901 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002902 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002903 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002904
2905 set_page_extent_mapped(page);
2906
Miao Xie99740902013-07-25 19:22:36 +08002907 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002908 if (!PageUptodate(page)) {
2909 if (cleancache_get_page(page) == 0) {
2910 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002911 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002912 goto out;
2913 }
2914 }
2915
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002916 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002917 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002918 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002919
2920 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002921 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002922 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002923 memset(userpage + zero_offset, 0, iosize);
2924 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002925 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002926 }
2927 }
Chris Masond1310b22008-01-24 16:13:08 -05002928 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002929 bool force_bio_submit = false;
David Sterba6273b7f2017-10-04 17:30:11 +02002930 u64 offset;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002931
Chris Masond1310b22008-01-24 16:13:08 -05002932 if (cur >= last_byte) {
2933 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002934 struct extent_state *cached = NULL;
2935
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002936 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002937 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002938 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002939 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002940 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002941 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002942 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002943 unlock_extent_cached(tree, cur,
2944 cur + iosize - 1,
2945 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002946 break;
2947 }
Miao Xie125bac012013-07-25 19:22:37 +08002948 em = __get_extent_map(inode, page, pg_offset, cur,
2949 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002950 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002951 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002952 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002953 break;
2954 }
Chris Masond1310b22008-01-24 16:13:08 -05002955 extent_offset = cur - em->start;
2956 BUG_ON(extent_map_end(em) <= cur);
2957 BUG_ON(end < cur);
2958
Li Zefan261507a02010-12-17 14:21:50 +08002959 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002960 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002961 extent_set_compress_type(&this_bio_flag,
2962 em->compress_type);
2963 }
Chris Masonc8b97812008-10-29 14:49:59 -04002964
Chris Masond1310b22008-01-24 16:13:08 -05002965 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2966 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002967 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002968 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2969 disk_io_size = em->block_len;
David Sterba6273b7f2017-10-04 17:30:11 +02002970 offset = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002971 } else {
David Sterba6273b7f2017-10-04 17:30:11 +02002972 offset = em->block_start + extent_offset;
Chris Masonc8b97812008-10-29 14:49:59 -04002973 disk_io_size = iosize;
2974 }
Chris Masond1310b22008-01-24 16:13:08 -05002975 bdev = em->bdev;
2976 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002977 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2978 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002979
2980 /*
2981 * If we have a file range that points to a compressed extent
2982 * and it's followed by a consecutive file range that points to
2983 * to the same compressed extent (possibly with a different
2984 * offset and/or length, so it either points to the whole extent
2985 * or only part of it), we must make sure we do not submit a
2986 * single bio to populate the pages for the 2 ranges because
2987 * this makes the compressed extent read zero out the pages
2988 * belonging to the 2nd range. Imagine the following scenario:
2989 *
2990 * File layout
2991 * [0 - 8K] [8K - 24K]
2992 * | |
2993 * | |
2994 * points to extent X, points to extent X,
2995 * offset 4K, length of 8K offset 0, length 16K
2996 *
2997 * [extent X, compressed length = 4K uncompressed length = 16K]
2998 *
2999 * If the bio to read the compressed extent covers both ranges,
3000 * it will decompress extent X into the pages belonging to the
3001 * first range and then it will stop, zeroing out the remaining
3002 * pages that belong to the other range that points to extent X.
3003 * So here we make sure we submit 2 bios, one for the first
3004 * range and another one for the third range. Both will target
3005 * the same physical extent from disk, but we can't currently
3006 * make the compressed bio endio callback populate the pages
3007 * for both ranges because each compressed bio is tightly
3008 * coupled with a single extent map, and each range can have
3009 * an extent map with a different offset value relative to the
3010 * uncompressed data of our extent and different lengths. This
3011 * is a corner case so we prioritize correctness over
3012 * non-optimal behavior (submitting 2 bios for the same extent).
3013 */
3014 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3015 prev_em_start && *prev_em_start != (u64)-1 &&
3016 *prev_em_start != em->orig_start)
3017 force_bio_submit = true;
3018
3019 if (prev_em_start)
3020 *prev_em_start = em->orig_start;
3021
Chris Masond1310b22008-01-24 16:13:08 -05003022 free_extent_map(em);
3023 em = NULL;
3024
3025 /* we've found a hole, just zero and go on */
3026 if (block_start == EXTENT_MAP_HOLE) {
3027 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003028 struct extent_state *cached = NULL;
3029
Cong Wang7ac687d2011-11-25 23:14:28 +08003030 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003031 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003032 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003033 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003034
3035 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003036 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003037 unlock_extent_cached(tree, cur,
3038 cur + iosize - 1,
3039 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003040 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003041 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003042 continue;
3043 }
3044 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003045 if (test_range_bit(tree, cur, cur_end,
3046 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003047 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003048 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003049 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003050 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003051 continue;
3052 }
Chris Mason70dec802008-01-29 09:59:12 -05003053 /* we have an inline extent but it didn't get marked up
3054 * to date. Error out
3055 */
3056 if (block_start == EXTENT_MAP_INLINE) {
3057 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003058 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003059 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003060 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003061 continue;
3062 }
Chris Masond1310b22008-01-24 16:13:08 -05003063
David Sterba4b81ba42017-06-06 19:14:26 +02003064 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
David Sterba6273b7f2017-10-04 17:30:11 +02003065 page, offset, disk_io_size,
3066 pg_offset, bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003067 end_bio_extent_readpage, mirror_num,
3068 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003069 this_bio_flag,
3070 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003071 if (!ret) {
3072 nr++;
3073 *bio_flags = this_bio_flag;
3074 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003075 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003076 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003077 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003078 }
Chris Masond1310b22008-01-24 16:13:08 -05003079 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003080 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003081 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003082out:
Chris Masond1310b22008-01-24 16:13:08 -05003083 if (!nr) {
3084 if (!PageError(page))
3085 SetPageUptodate(page);
3086 unlock_page(page);
3087 }
Liu Bobaf863b2016-07-11 10:39:07 -07003088 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003089}
3090
Miao Xie99740902013-07-25 19:22:36 +08003091static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3092 struct page *pages[], int nr_pages,
3093 u64 start, u64 end,
Miao Xie125bac012013-07-25 19:22:37 +08003094 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003095 struct bio **bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003096 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003097 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003098{
3099 struct inode *inode;
3100 struct btrfs_ordered_extent *ordered;
3101 int index;
3102
3103 inode = pages[0]->mapping->host;
3104 while (1) {
3105 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003106 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003107 end - start + 1);
3108 if (!ordered)
3109 break;
3110 unlock_extent(tree, start, end);
3111 btrfs_start_ordered_extent(inode, ordered, 1);
3112 btrfs_put_ordered_extent(ordered);
3113 }
3114
3115 for (index = 0; index < nr_pages; index++) {
David Sterba4ef77692017-06-23 04:09:57 +02003116 __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
3117 bio, 0, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003118 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003119 }
3120}
3121
3122static void __extent_readpages(struct extent_io_tree *tree,
3123 struct page *pages[],
David Sterbae4d17ef2017-06-23 04:09:57 +02003124 int nr_pages,
Miao Xie125bac012013-07-25 19:22:37 +08003125 struct extent_map **em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003126 struct bio **bio, unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003127 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003128{
Stefan Behrens35a36212013-08-14 18:12:25 +02003129 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003130 u64 end = 0;
3131 u64 page_start;
3132 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003133 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003134
3135 for (index = 0; index < nr_pages; index++) {
3136 page_start = page_offset(pages[index]);
3137 if (!end) {
3138 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003139 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003140 first_index = index;
3141 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003142 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003143 } else {
3144 __do_contiguous_readpages(tree, &pages[first_index],
3145 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003146 end, em_cached,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003147 bio, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003148 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003149 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003150 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003151 first_index = index;
3152 }
3153 }
3154
3155 if (end)
3156 __do_contiguous_readpages(tree, &pages[first_index],
3157 index - first_index, start,
David Sterba4ef77692017-06-23 04:09:57 +02003158 end, em_cached, bio,
Nikolay Borisovd3fac6b2017-10-24 11:50:39 +03003159 bio_flags, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003160}
3161
3162static int __extent_read_full_page(struct extent_io_tree *tree,
3163 struct page *page,
3164 get_extent_t *get_extent,
3165 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003166 unsigned long *bio_flags,
3167 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003168{
3169 struct inode *inode = page->mapping->host;
3170 struct btrfs_ordered_extent *ordered;
3171 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003172 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003173 int ret;
3174
3175 while (1) {
3176 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003177 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003178 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003179 if (!ordered)
3180 break;
3181 unlock_extent(tree, start, end);
3182 btrfs_start_ordered_extent(inode, ordered, 1);
3183 btrfs_put_ordered_extent(ordered);
3184 }
3185
Miao Xie125bac012013-07-25 19:22:37 +08003186 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003187 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003188 return ret;
3189}
3190
Chris Masond1310b22008-01-24 16:13:08 -05003191int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003192 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003193{
3194 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003195 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003196 int ret;
3197
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003198 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003199 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003200 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003201 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003202 return ret;
3203}
Chris Masond1310b22008-01-24 16:13:08 -05003204
David Sterba3d4b9492017-02-10 19:33:41 +01003205static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003206 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003207{
3208 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003209}
3210
Chris Masond1310b22008-01-24 16:13:08 -05003211/*
Chris Mason40f76582014-05-21 13:35:51 -07003212 * helper for __extent_writepage, doing all of the delayed allocation setup.
3213 *
3214 * This returns 1 if our fill_delalloc function did all the work required
3215 * to write the page (copy into inline extent). In this case the IO has
3216 * been started and the page is already unlocked.
3217 *
3218 * This returns 0 if all went well (page still locked)
3219 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003220 */
Chris Mason40f76582014-05-21 13:35:51 -07003221static noinline_for_stack int writepage_delalloc(struct inode *inode,
3222 struct page *page, struct writeback_control *wbc,
3223 struct extent_page_data *epd,
3224 u64 delalloc_start,
3225 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003226{
Chris Mason40f76582014-05-21 13:35:51 -07003227 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003228 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003229 u64 nr_delalloc;
3230 u64 delalloc_to_write = 0;
3231 u64 delalloc_end = 0;
3232 int ret;
3233 int page_started = 0;
3234
3235 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3236 return 0;
3237
3238 while (delalloc_end < page_end) {
3239 nr_delalloc = find_lock_delalloc_range(inode, tree,
3240 page,
3241 &delalloc_start,
3242 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003243 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003244 if (nr_delalloc == 0) {
3245 delalloc_start = delalloc_end + 1;
3246 continue;
3247 }
3248 ret = tree->ops->fill_delalloc(inode, page,
3249 delalloc_start,
3250 delalloc_end,
3251 &page_started,
Liu Bof82b7352017-10-23 23:18:16 -06003252 nr_written, wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003253 /* File system has been set read-only */
3254 if (ret) {
3255 SetPageError(page);
3256 /* fill_delalloc should be return < 0 for error
3257 * but just in case, we use > 0 here meaning the
3258 * IO is started, so we don't want to return > 0
3259 * unless things are going well.
3260 */
3261 ret = ret < 0 ? ret : -EIO;
3262 goto done;
3263 }
3264 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003265 * delalloc_end is already one less than the total length, so
3266 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003267 */
3268 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003269 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003270 delalloc_start = delalloc_end + 1;
3271 }
3272 if (wbc->nr_to_write < delalloc_to_write) {
3273 int thresh = 8192;
3274
3275 if (delalloc_to_write < thresh * 2)
3276 thresh = delalloc_to_write;
3277 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3278 thresh);
3279 }
3280
3281 /* did the fill delalloc function already unlock and start
3282 * the IO?
3283 */
3284 if (page_started) {
3285 /*
3286 * we've unlocked the page, so we can't update
3287 * the mapping's writeback index, just update
3288 * nr_to_write.
3289 */
3290 wbc->nr_to_write -= *nr_written;
3291 return 1;
3292 }
3293
3294 ret = 0;
3295
3296done:
3297 return ret;
3298}
3299
3300/*
3301 * helper for __extent_writepage. This calls the writepage start hooks,
3302 * and does the loop to map the page into extents and bios.
3303 *
3304 * We return 1 if the IO is started and the page is unlocked,
3305 * 0 if all went well (page still locked)
3306 * < 0 if there were errors (page still locked)
3307 */
3308static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3309 struct page *page,
3310 struct writeback_control *wbc,
3311 struct extent_page_data *epd,
3312 loff_t i_size,
3313 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003314 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003315{
Chris Masond1310b22008-01-24 16:13:08 -05003316 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003317 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003318 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003319 u64 end;
3320 u64 cur = start;
3321 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003322 u64 block_start;
3323 u64 iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003324 struct extent_map *em;
3325 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003326 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003327 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003328 int ret = 0;
3329 int nr = 0;
3330 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003331
Chris Mason247e7432008-07-17 12:53:51 -04003332 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003333 ret = tree->ops->writepage_start_hook(page, start,
3334 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003335 if (ret) {
3336 /* Fixup worker will requeue */
3337 if (ret == -EBUSY)
3338 wbc->pages_skipped++;
3339 else
3340 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003341
David Sterba3d4b9492017-02-10 19:33:41 +01003342 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003343 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003344 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003345 }
3346 }
3347
Chris Mason11c83492009-04-20 15:50:09 -04003348 /*
3349 * we don't want to touch the inode after unlocking the page,
3350 * so we update the mapping writeback index now
3351 */
David Sterba3d4b9492017-02-10 19:33:41 +01003352 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003353
Chris Masond1310b22008-01-24 16:13:08 -05003354 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003355 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003356 if (tree->ops && tree->ops->writepage_end_io_hook)
3357 tree->ops->writepage_end_io_hook(page, start,
3358 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003359 goto done;
3360 }
3361
Chris Masond1310b22008-01-24 16:13:08 -05003362 blocksize = inode->i_sb->s_blocksize;
3363
3364 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003365 u64 em_end;
David Sterba6273b7f2017-10-04 17:30:11 +02003366 u64 offset;
David Sterba58409ed2016-05-04 11:46:10 +02003367
Chris Mason40f76582014-05-21 13:35:51 -07003368 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003369 if (tree->ops && tree->ops->writepage_end_io_hook)
3370 tree->ops->writepage_end_io_hook(page, cur,
3371 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003372 break;
3373 }
David Sterba3c98c622017-06-23 04:01:08 +02003374 em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003375 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003376 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003377 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003378 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003379 break;
3380 }
3381
3382 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003383 em_end = extent_map_end(em);
3384 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003385 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003386 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003387 iosize = ALIGN(iosize, blocksize);
David Sterba6273b7f2017-10-04 17:30:11 +02003388 offset = em->block_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003389 bdev = em->bdev;
3390 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003391 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003392 free_extent_map(em);
3393 em = NULL;
3394
Chris Masonc8b97812008-10-29 14:49:59 -04003395 /*
3396 * compressed and inline extents are written through other
3397 * paths in the FS
3398 */
3399 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003400 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003401 /*
3402 * end_io notification does not happen here for
3403 * compressed extents
3404 */
3405 if (!compressed && tree->ops &&
3406 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003407 tree->ops->writepage_end_io_hook(page, cur,
3408 cur + iosize - 1,
3409 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003410 else if (compressed) {
3411 /* we don't want to end_page_writeback on
3412 * a compressed extent. this happens
3413 * elsewhere
3414 */
3415 nr++;
3416 }
3417
3418 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003419 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003420 continue;
3421 }
Chris Masonc8b97812008-10-29 14:49:59 -04003422
David Sterba58409ed2016-05-04 11:46:10 +02003423 set_range_writeback(tree, cur, cur + iosize - 1);
3424 if (!PageWriteback(page)) {
3425 btrfs_err(BTRFS_I(inode)->root->fs_info,
3426 "page %lu not writeback, cur %llu end %llu",
3427 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003428 }
David Sterba58409ed2016-05-04 11:46:10 +02003429
David Sterba4b81ba42017-06-06 19:14:26 +02003430 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003431 page, offset, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003432 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003433 end_bio_extent_writepage,
3434 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003435 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003436 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003437 if (PageWriteback(page))
3438 end_page_writeback(page);
3439 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003440
Chris Masond1310b22008-01-24 16:13:08 -05003441 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003442 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003443 nr++;
3444 }
3445done:
Chris Mason40f76582014-05-21 13:35:51 -07003446 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003447 return ret;
3448}
3449
3450/*
3451 * the writepage semantics are similar to regular writepage. extent
3452 * records are inserted to lock ranges in the tree, and as dirty areas
3453 * are found, they are marked writeback. Then the lock bits are removed
3454 * and the end_io handler clears the writeback ranges
3455 */
3456static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3457 void *data)
3458{
3459 struct inode *inode = page->mapping->host;
3460 struct extent_page_data *epd = data;
3461 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003462 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003463 int ret;
3464 int nr = 0;
3465 size_t pg_offset = 0;
3466 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003467 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003468 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003469 unsigned long nr_written = 0;
3470
Liu Boff40adf2017-08-24 18:19:48 -06003471 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003472
3473 trace___extent_writepage(page, inode, wbc);
3474
3475 WARN_ON(!PageLocked(page));
3476
3477 ClearPageError(page);
3478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003479 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003480 if (page->index > end_index ||
3481 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003482 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003483 unlock_page(page);
3484 return 0;
3485 }
3486
3487 if (page->index == end_index) {
3488 char *userpage;
3489
3490 userpage = kmap_atomic(page);
3491 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003492 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003493 kunmap_atomic(userpage);
3494 flush_dcache_page(page);
3495 }
3496
3497 pg_offset = 0;
3498
3499 set_page_extent_mapped(page);
3500
3501 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3502 if (ret == 1)
3503 goto done_unlocked;
3504 if (ret)
3505 goto done;
3506
3507 ret = __extent_writepage_io(inode, page, wbc, epd,
3508 i_size, nr_written, write_flags, &nr);
3509 if (ret == 1)
3510 goto done_unlocked;
3511
3512done:
Chris Masond1310b22008-01-24 16:13:08 -05003513 if (nr == 0) {
3514 /* make sure the mapping tag for page dirty gets cleared */
3515 set_page_writeback(page);
3516 end_page_writeback(page);
3517 }
Filipe Manana61391d52014-05-09 17:17:40 +01003518 if (PageError(page)) {
3519 ret = ret < 0 ? ret : -EIO;
3520 end_extent_writepage(page, ret, start, page_end);
3521 }
Chris Masond1310b22008-01-24 16:13:08 -05003522 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003523 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003524
Chris Mason11c83492009-04-20 15:50:09 -04003525done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003526 return 0;
3527}
3528
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003529void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003530{
NeilBrown74316202014-07-07 15:16:04 +10003531 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3532 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003533}
3534
Chris Mason0e378df2014-05-19 20:55:27 -07003535static noinline_for_stack int
3536lock_extent_buffer_for_io(struct extent_buffer *eb,
3537 struct btrfs_fs_info *fs_info,
3538 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003539{
3540 unsigned long i, num_pages;
3541 int flush = 0;
3542 int ret = 0;
3543
3544 if (!btrfs_try_tree_write_lock(eb)) {
3545 flush = 1;
3546 flush_write_bio(epd);
3547 btrfs_tree_lock(eb);
3548 }
3549
3550 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3551 btrfs_tree_unlock(eb);
3552 if (!epd->sync_io)
3553 return 0;
3554 if (!flush) {
3555 flush_write_bio(epd);
3556 flush = 1;
3557 }
Chris Masona098d8e2012-03-21 12:09:56 -04003558 while (1) {
3559 wait_on_extent_buffer_writeback(eb);
3560 btrfs_tree_lock(eb);
3561 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3562 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003563 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003564 }
3565 }
3566
Josef Bacik51561ff2012-07-20 16:25:24 -04003567 /*
3568 * We need to do this to prevent races in people who check if the eb is
3569 * under IO since we can end up having no IO bits set for a short period
3570 * of time.
3571 */
3572 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003573 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3574 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003575 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003576 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003577 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3578 -eb->len,
3579 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003580 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003581 } else {
3582 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003583 }
3584
3585 btrfs_tree_unlock(eb);
3586
3587 if (!ret)
3588 return ret;
3589
3590 num_pages = num_extent_pages(eb->start, eb->len);
3591 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003592 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003593
3594 if (!trylock_page(p)) {
3595 if (!flush) {
3596 flush_write_bio(epd);
3597 flush = 1;
3598 }
3599 lock_page(p);
3600 }
3601 }
3602
3603 return ret;
3604}
3605
3606static void end_extent_buffer_writeback(struct extent_buffer *eb)
3607{
3608 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003609 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003610 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3611}
3612
Filipe Manana656f30d2014-09-26 12:25:56 +01003613static void set_btree_ioerr(struct page *page)
3614{
3615 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003616
3617 SetPageError(page);
3618 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3619 return;
3620
3621 /*
3622 * If writeback for a btree extent that doesn't belong to a log tree
3623 * failed, increment the counter transaction->eb_write_errors.
3624 * We do this because while the transaction is running and before it's
3625 * committing (when we call filemap_fdata[write|wait]_range against
3626 * the btree inode), we might have
3627 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3628 * returns an error or an error happens during writeback, when we're
3629 * committing the transaction we wouldn't know about it, since the pages
3630 * can be no longer dirty nor marked anymore for writeback (if a
3631 * subsequent modification to the extent buffer didn't happen before the
3632 * transaction commit), which makes filemap_fdata[write|wait]_range not
3633 * able to find the pages tagged with SetPageError at transaction
3634 * commit time. So if this happens we must abort the transaction,
3635 * otherwise we commit a super block with btree roots that point to
3636 * btree nodes/leafs whose content on disk is invalid - either garbage
3637 * or the content of some node/leaf from a past generation that got
3638 * cowed or deleted and is no longer valid.
3639 *
3640 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3641 * not be enough - we need to distinguish between log tree extents vs
3642 * non-log tree extents, and the next filemap_fdatawait_range() call
3643 * will catch and clear such errors in the mapping - and that call might
3644 * be from a log sync and not from a transaction commit. Also, checking
3645 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3646 * not done and would not be reliable - the eb might have been released
3647 * from memory and reading it back again means that flag would not be
3648 * set (since it's a runtime flag, not persisted on disk).
3649 *
3650 * Using the flags below in the btree inode also makes us achieve the
3651 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3652 * writeback for all dirty pages and before filemap_fdatawait_range()
3653 * is called, the writeback for all dirty pages had already finished
3654 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3655 * filemap_fdatawait_range() would return success, as it could not know
3656 * that writeback errors happened (the pages were no longer tagged for
3657 * writeback).
3658 */
3659 switch (eb->log_index) {
3660 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003661 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003662 break;
3663 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003664 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003665 break;
3666 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003667 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003668 break;
3669 default:
3670 BUG(); /* unexpected, logic error */
3671 }
3672}
3673
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003674static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003675{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003676 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003677 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003678 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679
David Sterbac09abff2017-07-13 18:10:07 +02003680 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003681 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003682 struct page *page = bvec->bv_page;
3683
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003684 eb = (struct extent_buffer *)page->private;
3685 BUG_ON(!eb);
3686 done = atomic_dec_and_test(&eb->io_pages);
3687
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003688 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003689 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003690 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003691 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003692 }
3693
3694 end_page_writeback(page);
3695
3696 if (!done)
3697 continue;
3698
3699 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003700 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003701
3702 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003703}
3704
Chris Mason0e378df2014-05-19 20:55:27 -07003705static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706 struct btrfs_fs_info *fs_info,
3707 struct writeback_control *wbc,
3708 struct extent_page_data *epd)
3709{
3710 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003711 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003712 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003713 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003714 unsigned long i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003715 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003716 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003717 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003718
Filipe Manana656f30d2014-09-26 12:25:56 +01003719 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003720 num_pages = num_extent_pages(eb->start, eb->len);
3721 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003722
Liu Bo851cd172016-09-23 13:44:44 -07003723 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3724 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003725 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003726 end = btrfs_node_key_ptr_offset(nritems);
3727
David Sterbab159fa22016-11-08 18:09:03 +01003728 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003729 } else {
3730 /*
3731 * leaf:
3732 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3733 */
3734 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003735 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003736 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003737 }
3738
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003739 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003740 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003741
3742 clear_page_dirty_for_io(p);
3743 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003744 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
David Sterba6273b7f2017-10-04 17:30:11 +02003745 p, offset, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003746 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003747 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003748 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003749 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003750 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003751 if (PageWriteback(p))
3752 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003753 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3754 end_extent_buffer_writeback(eb);
3755 ret = -EIO;
3756 break;
3757 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003758 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003759 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003760 unlock_page(p);
3761 }
3762
3763 if (unlikely(ret)) {
3764 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003765 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003766 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003767 unlock_page(p);
3768 }
3769 }
3770
3771 return ret;
3772}
3773
3774int btree_write_cache_pages(struct address_space *mapping,
3775 struct writeback_control *wbc)
3776{
3777 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3778 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3779 struct extent_buffer *eb, *prev_eb = NULL;
3780 struct extent_page_data epd = {
3781 .bio = NULL,
3782 .tree = tree,
3783 .extent_locked = 0,
3784 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3785 };
3786 int ret = 0;
3787 int done = 0;
3788 int nr_to_write_done = 0;
3789 struct pagevec pvec;
3790 int nr_pages;
3791 pgoff_t index;
3792 pgoff_t end; /* Inclusive */
3793 int scanned = 0;
3794 int tag;
3795
Mel Gorman86679822017-11-15 17:37:52 -08003796 pagevec_init(&pvec);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003797 if (wbc->range_cyclic) {
3798 index = mapping->writeback_index; /* Start from prev offset */
3799 end = -1;
3800 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003801 index = wbc->range_start >> PAGE_SHIFT;
3802 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003803 scanned = 1;
3804 }
3805 if (wbc->sync_mode == WB_SYNC_ALL)
3806 tag = PAGECACHE_TAG_TOWRITE;
3807 else
3808 tag = PAGECACHE_TAG_DIRTY;
3809retry:
3810 if (wbc->sync_mode == WB_SYNC_ALL)
3811 tag_pages_for_writeback(mapping, index, end);
3812 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara4006f432017-11-15 17:34:37 -08003813 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08003814 tag))) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003815 unsigned i;
3816
3817 scanned = 1;
3818 for (i = 0; i < nr_pages; i++) {
3819 struct page *page = pvec.pages[i];
3820
3821 if (!PagePrivate(page))
3822 continue;
3823
Josef Bacikb5bae262012-09-14 13:43:01 -04003824 spin_lock(&mapping->private_lock);
3825 if (!PagePrivate(page)) {
3826 spin_unlock(&mapping->private_lock);
3827 continue;
3828 }
3829
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003830 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003831
3832 /*
3833 * Shouldn't happen and normally this would be a BUG_ON
3834 * but no sense in crashing the users box for something
3835 * we can survive anyway.
3836 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303837 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003838 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003839 continue;
3840 }
3841
Josef Bacikb5bae262012-09-14 13:43:01 -04003842 if (eb == prev_eb) {
3843 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003844 continue;
3845 }
3846
Josef Bacikb5bae262012-09-14 13:43:01 -04003847 ret = atomic_inc_not_zero(&eb->refs);
3848 spin_unlock(&mapping->private_lock);
3849 if (!ret)
3850 continue;
3851
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003852 prev_eb = eb;
3853 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3854 if (!ret) {
3855 free_extent_buffer(eb);
3856 continue;
3857 }
3858
3859 ret = write_one_eb(eb, fs_info, wbc, &epd);
3860 if (ret) {
3861 done = 1;
3862 free_extent_buffer(eb);
3863 break;
3864 }
3865 free_extent_buffer(eb);
3866
3867 /*
3868 * the filesystem may choose to bump up nr_to_write.
3869 * We have to make sure to honor the new nr_to_write
3870 * at any time
3871 */
3872 nr_to_write_done = wbc->nr_to_write <= 0;
3873 }
3874 pagevec_release(&pvec);
3875 cond_resched();
3876 }
3877 if (!scanned && !done) {
3878 /*
3879 * We hit the last page and there is more work to be done: wrap
3880 * back to the start of the file
3881 */
3882 scanned = 1;
3883 index = 0;
3884 goto retry;
3885 }
3886 flush_write_bio(&epd);
3887 return ret;
3888}
3889
Chris Masond1310b22008-01-24 16:13:08 -05003890/**
Chris Mason4bef0842008-09-08 11:18:08 -04003891 * 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 -05003892 * @mapping: address space structure to write
3893 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3894 * @writepage: function called for each page
3895 * @data: data passed to writepage function
3896 *
3897 * If a page is already under I/O, write_cache_pages() skips it, even
3898 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3899 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3900 * and msync() need to guarantee that all the data which was dirty at the time
3901 * the call was made get new I/O started against them. If wbc->sync_mode is
3902 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3903 * existing IO to complete.
3904 */
David Sterba4242b642017-02-10 19:38:24 +01003905static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003906 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003907 writepage_t writepage, void *data,
3908 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003909{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003910 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003911 int ret = 0;
3912 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003913 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003914 struct pagevec pvec;
3915 int nr_pages;
3916 pgoff_t index;
3917 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003918 pgoff_t done_index;
3919 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003920 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003921 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003922
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003923 /*
3924 * We have to hold onto the inode so that ordered extents can do their
3925 * work when the IO finishes. The alternative to this is failing to add
3926 * an ordered extent if the igrab() fails there and that is a huge pain
3927 * to deal with, so instead just hold onto the inode throughout the
3928 * writepages operation. If it fails here we are freeing up the inode
3929 * anyway and we'd rather not waste our time writing out stuff that is
3930 * going to be truncated anyway.
3931 */
3932 if (!igrab(inode))
3933 return 0;
3934
Mel Gorman86679822017-11-15 17:37:52 -08003935 pagevec_init(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05003936 if (wbc->range_cyclic) {
3937 index = mapping->writeback_index; /* Start from prev offset */
3938 end = -1;
3939 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003940 index = wbc->range_start >> PAGE_SHIFT;
3941 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003942 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3943 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003944 scanned = 1;
3945 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003946 if (wbc->sync_mode == WB_SYNC_ALL)
3947 tag = PAGECACHE_TAG_TOWRITE;
3948 else
3949 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003950retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003951 if (wbc->sync_mode == WB_SYNC_ALL)
3952 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003953 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003954 while (!done && !nr_to_write_done && (index <= end) &&
Jan Kara67fd7072017-11-15 17:35:19 -08003955 (nr_pages = pagevec_lookup_range_tag(&pvec, mapping,
3956 &index, end, tag))) {
Chris Masond1310b22008-01-24 16:13:08 -05003957 unsigned i;
3958
3959 scanned = 1;
3960 for (i = 0; i < nr_pages; i++) {
3961 struct page *page = pvec.pages[i];
3962
Liu Boa91326672016-03-07 16:56:21 -08003963 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003964 /*
3965 * At this point we hold neither mapping->tree_lock nor
3966 * lock on the page itself: the page may be truncated or
3967 * invalidated (changing page->mapping to NULL), or even
3968 * swizzled back from swapper_space to tmpfs file
3969 * mapping
3970 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003971 if (!trylock_page(page)) {
3972 flush_fn(data);
3973 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003974 }
Chris Masond1310b22008-01-24 16:13:08 -05003975
3976 if (unlikely(page->mapping != mapping)) {
3977 unlock_page(page);
3978 continue;
3979 }
3980
Chris Masond2c3f4f2008-11-19 12:44:22 -05003981 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003982 if (PageWriteback(page))
3983 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003984 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003985 }
Chris Masond1310b22008-01-24 16:13:08 -05003986
3987 if (PageWriteback(page) ||
3988 !clear_page_dirty_for_io(page)) {
3989 unlock_page(page);
3990 continue;
3991 }
3992
3993 ret = (*writepage)(page, wbc, data);
3994
3995 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3996 unlock_page(page);
3997 ret = 0;
3998 }
Liu Boa91326672016-03-07 16:56:21 -08003999 if (ret < 0) {
4000 /*
4001 * done_index is set past this page,
4002 * so media errors will not choke
4003 * background writeout for the entire
4004 * file. This has consequences for
4005 * range_cyclic semantics (ie. it may
4006 * not be suitable for data integrity
4007 * writeout).
4008 */
4009 done_index = page->index + 1;
4010 done = 1;
4011 break;
4012 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004013
4014 /*
4015 * the filesystem may choose to bump up nr_to_write.
4016 * We have to make sure to honor the new nr_to_write
4017 * at any time
4018 */
4019 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004020 }
4021 pagevec_release(&pvec);
4022 cond_resched();
4023 }
Liu Bo894b36e2016-03-07 16:56:22 -08004024 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004025 /*
4026 * We hit the last page and there is more work to be done: wrap
4027 * back to the start of the file
4028 */
4029 scanned = 1;
4030 index = 0;
4031 goto retry;
4032 }
Liu Boa91326672016-03-07 16:56:21 -08004033
4034 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4035 mapping->writeback_index = done_index;
4036
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004037 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004038 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004039}
Chris Masond1310b22008-01-24 16:13:08 -05004040
Chris Masonffbd5172009-04-20 15:50:09 -04004041static void flush_epd_write_bio(struct extent_page_data *epd)
4042{
4043 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004044 int ret;
4045
Liu Bo18fdc672017-09-13 12:18:22 -06004046 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004047 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004048 epd->bio = NULL;
4049 }
4050}
4051
Chris Masond2c3f4f2008-11-19 12:44:22 -05004052static noinline void flush_write_bio(void *data)
4053{
4054 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004055 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004056}
4057
Chris Masond1310b22008-01-24 16:13:08 -05004058int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -05004059 struct writeback_control *wbc)
4060{
4061 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004062 struct extent_page_data epd = {
4063 .bio = NULL,
4064 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004065 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004066 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004067 };
Chris Masond1310b22008-01-24 16:13:08 -05004068
Chris Masond1310b22008-01-24 16:13:08 -05004069 ret = __extent_writepage(page, wbc, &epd);
4070
Chris Masonffbd5172009-04-20 15:50:09 -04004071 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004072 return ret;
4073}
Chris Masond1310b22008-01-24 16:13:08 -05004074
Chris Mason771ed682008-11-06 22:02:51 -05004075int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
David Sterba916b9292017-06-23 03:47:28 +02004076 u64 start, u64 end, int mode)
Chris Mason771ed682008-11-06 22:02:51 -05004077{
4078 int ret = 0;
4079 struct address_space *mapping = inode->i_mapping;
4080 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004081 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4082 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004083
4084 struct extent_page_data epd = {
4085 .bio = NULL,
4086 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004087 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004088 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004089 };
4090 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004091 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004092 .nr_to_write = nr_pages * 2,
4093 .range_start = start,
4094 .range_end = end + 1,
4095 };
4096
Chris Masond3977122009-01-05 21:25:51 -05004097 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004098 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004099 if (clear_page_dirty_for_io(page))
4100 ret = __extent_writepage(page, &wbc_writepages, &epd);
4101 else {
4102 if (tree->ops && tree->ops->writepage_end_io_hook)
4103 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004104 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004105 NULL, 1);
4106 unlock_page(page);
4107 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004108 put_page(page);
4109 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004110 }
4111
Chris Masonffbd5172009-04-20 15:50:09 -04004112 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004113 return ret;
4114}
Chris Masond1310b22008-01-24 16:13:08 -05004115
4116int extent_writepages(struct extent_io_tree *tree,
4117 struct address_space *mapping,
Chris Masond1310b22008-01-24 16:13:08 -05004118 struct writeback_control *wbc)
4119{
4120 int ret = 0;
4121 struct extent_page_data epd = {
4122 .bio = NULL,
4123 .tree = tree,
Chris Mason771ed682008-11-06 22:02:51 -05004124 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004125 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004126 };
4127
David Sterba4242b642017-02-10 19:38:24 +01004128 ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004129 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004130 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004131 return ret;
4132}
Chris Masond1310b22008-01-24 16:13:08 -05004133
4134int extent_readpages(struct extent_io_tree *tree,
4135 struct address_space *mapping,
David Sterba09325842017-06-23 04:09:57 +02004136 struct list_head *pages, unsigned nr_pages)
Chris Masond1310b22008-01-24 16:13:08 -05004137{
4138 struct bio *bio = NULL;
4139 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004140 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004141 struct page *pagepool[16];
4142 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004143 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004144 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004145 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004146
Chris Masond1310b22008-01-24 16:13:08 -05004147 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004148 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004149
4150 prefetchw(&page->flags);
4151 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004152 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004153 page->index,
4154 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004155 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004156 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004157 }
Liu Bo67c96842012-07-20 21:43:09 -06004158
4159 pagepool[nr++] = page;
4160 if (nr < ARRAY_SIZE(pagepool))
4161 continue;
David Sterbae4d17ef2017-06-23 04:09:57 +02004162 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4163 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004164 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004165 }
Miao Xie99740902013-07-25 19:22:36 +08004166 if (nr)
David Sterbae4d17ef2017-06-23 04:09:57 +02004167 __extent_readpages(tree, pagepool, nr, &em_cached, &bio,
4168 &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004169
Miao Xie125bac012013-07-25 19:22:37 +08004170 if (em_cached)
4171 free_extent_map(em_cached);
4172
Chris Masond1310b22008-01-24 16:13:08 -05004173 BUG_ON(!list_empty(pages));
4174 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004175 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004176 return 0;
4177}
Chris Masond1310b22008-01-24 16:13:08 -05004178
4179/*
4180 * basic invalidatepage code, this waits on any locked or writeback
4181 * ranges corresponding to the page, and then deletes any extent state
4182 * records from the tree
4183 */
4184int extent_invalidatepage(struct extent_io_tree *tree,
4185 struct page *page, unsigned long offset)
4186{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004187 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004188 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004189 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004190 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4191
Qu Wenruofda28322013-02-26 08:10:22 +00004192 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004193 if (start > end)
4194 return 0;
4195
David Sterbaff13db42015-12-03 14:30:40 +01004196 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004197 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004198 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004199 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4200 EXTENT_DO_ACCOUNTING,
David Sterbaae0f1622017-10-31 16:37:52 +01004201 1, 1, &cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05004202 return 0;
4203}
Chris Masond1310b22008-01-24 16:13:08 -05004204
4205/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004206 * a helper for releasepage, this tests for areas of the page that
4207 * are locked or under IO and drops the related state bits if it is safe
4208 * to drop the page.
4209 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004210static int try_release_extent_state(struct extent_map_tree *map,
4211 struct extent_io_tree *tree,
4212 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004213{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004214 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004215 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004216 int ret = 1;
4217
Chris Mason211f90e2008-07-18 11:56:15 -04004218 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004219 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004220 ret = 0;
4221 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004222 /*
4223 * at this point we can safely clear everything except the
4224 * locked bit and the nodatasum bit
4225 */
David Sterba66b0c882017-10-31 16:30:47 +01004226 ret = __clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004227 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
David Sterba66b0c882017-10-31 16:30:47 +01004228 0, 0, NULL, mask, NULL);
Chris Masone3f24cc2011-02-14 12:52:08 -05004229
4230 /* if clear_extent_bit failed for enomem reasons,
4231 * we can't allow the release to continue.
4232 */
4233 if (ret < 0)
4234 ret = 0;
4235 else
4236 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004237 }
4238 return ret;
4239}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004240
4241/*
Chris Masond1310b22008-01-24 16:13:08 -05004242 * a helper for releasepage. As long as there are no locked extents
4243 * in the range corresponding to the page, both state records and extent
4244 * map records are removed
4245 */
4246int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004247 struct extent_io_tree *tree, struct page *page,
4248 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004249{
4250 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004251 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004252 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004253
Mel Gormand0164ad2015-11-06 16:28:21 -08004254 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004255 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004256 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004257 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004258 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004259 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004260 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004261 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004262 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004263 break;
4264 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004265 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4266 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004267 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004268 free_extent_map(em);
4269 break;
4270 }
4271 if (!test_range_bit(tree, em->start,
4272 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004273 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004274 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004275 remove_extent_mapping(map, em);
4276 /* once for the rb tree */
4277 free_extent_map(em);
4278 }
4279 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004280 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004281
4282 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004283 free_extent_map(em);
4284 }
Chris Masond1310b22008-01-24 16:13:08 -05004285 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004286 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004287}
Chris Masond1310b22008-01-24 16:13:08 -05004288
Chris Masonec29ed52011-02-23 16:23:20 -05004289/*
4290 * helper function for fiemap, which doesn't want to see any holes.
4291 * This maps until we find something past 'last'
4292 */
4293static struct extent_map *get_extent_skip_holes(struct inode *inode,
David Sterbae3350e12017-06-23 04:09:57 +02004294 u64 offset, u64 last)
Chris Masonec29ed52011-02-23 16:23:20 -05004295{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004296 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004297 struct extent_map *em;
4298 u64 len;
4299
4300 if (offset >= last)
4301 return NULL;
4302
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304303 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004304 len = last - offset;
4305 if (len == 0)
4306 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004307 len = ALIGN(len, sectorsize);
David Sterbae3350e12017-06-23 04:09:57 +02004308 em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, offset,
4309 len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004310 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004311 return em;
4312
4313 /* if this isn't a hole return it */
Nikolay Borisov4a2d25c2017-11-23 10:51:43 +02004314 if (em->block_start != EXTENT_MAP_HOLE)
Chris Masonec29ed52011-02-23 16:23:20 -05004315 return em;
Chris Masonec29ed52011-02-23 16:23:20 -05004316
4317 /* this is a hole, advance to the next extent */
4318 offset = extent_map_end(em);
4319 free_extent_map(em);
4320 if (offset >= last)
4321 break;
4322 }
4323 return NULL;
4324}
4325
Qu Wenruo47518322017-04-07 10:43:15 +08004326/*
4327 * To cache previous fiemap extent
4328 *
4329 * Will be used for merging fiemap extent
4330 */
4331struct fiemap_cache {
4332 u64 offset;
4333 u64 phys;
4334 u64 len;
4335 u32 flags;
4336 bool cached;
4337};
4338
4339/*
4340 * Helper to submit fiemap extent.
4341 *
4342 * Will try to merge current fiemap extent specified by @offset, @phys,
4343 * @len and @flags with cached one.
4344 * And only when we fails to merge, cached one will be submitted as
4345 * fiemap extent.
4346 *
4347 * Return value is the same as fiemap_fill_next_extent().
4348 */
4349static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4350 struct fiemap_cache *cache,
4351 u64 offset, u64 phys, u64 len, u32 flags)
4352{
4353 int ret = 0;
4354
4355 if (!cache->cached)
4356 goto assign;
4357
4358 /*
4359 * Sanity check, extent_fiemap() should have ensured that new
4360 * fiemap extent won't overlap with cahced one.
4361 * Not recoverable.
4362 *
4363 * NOTE: Physical address can overlap, due to compression
4364 */
4365 if (cache->offset + cache->len > offset) {
4366 WARN_ON(1);
4367 return -EINVAL;
4368 }
4369
4370 /*
4371 * Only merges fiemap extents if
4372 * 1) Their logical addresses are continuous
4373 *
4374 * 2) Their physical addresses are continuous
4375 * So truly compressed (physical size smaller than logical size)
4376 * extents won't get merged with each other
4377 *
4378 * 3) Share same flags except FIEMAP_EXTENT_LAST
4379 * So regular extent won't get merged with prealloc extent
4380 */
4381 if (cache->offset + cache->len == offset &&
4382 cache->phys + cache->len == phys &&
4383 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4384 (flags & ~FIEMAP_EXTENT_LAST)) {
4385 cache->len += len;
4386 cache->flags |= flags;
4387 goto try_submit_last;
4388 }
4389
4390 /* Not mergeable, need to submit cached one */
4391 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4392 cache->len, cache->flags);
4393 cache->cached = false;
4394 if (ret)
4395 return ret;
4396assign:
4397 cache->cached = true;
4398 cache->offset = offset;
4399 cache->phys = phys;
4400 cache->len = len;
4401 cache->flags = flags;
4402try_submit_last:
4403 if (cache->flags & FIEMAP_EXTENT_LAST) {
4404 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4405 cache->phys, cache->len, cache->flags);
4406 cache->cached = false;
4407 }
4408 return ret;
4409}
4410
4411/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004412 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004413 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004414 * The last fiemap cache may still be cached in the following case:
4415 * 0 4k 8k
4416 * |<- Fiemap range ->|
4417 * |<------------ First extent ----------->|
4418 *
4419 * In this case, the first extent range will be cached but not emitted.
4420 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004421 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004422static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4423 struct fiemap_extent_info *fieinfo,
4424 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004425{
4426 int ret;
4427
4428 if (!cache->cached)
4429 return 0;
4430
Qu Wenruo47518322017-04-07 10:43:15 +08004431 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4432 cache->len, cache->flags);
4433 cache->cached = false;
4434 if (ret > 0)
4435 ret = 0;
4436 return ret;
4437}
4438
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004439int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterba2135fb92017-06-23 04:09:57 +02004440 __u64 start, __u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004441{
Josef Bacik975f84f2010-11-23 19:36:57 +00004442 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004443 u64 off = start;
4444 u64 max = start + len;
4445 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004446 u32 found_type;
4447 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004448 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004449 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004450 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004451 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004452 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004453 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004454 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004455 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004456 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004457 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004458 u64 em_start = 0;
4459 u64 em_len = 0;
4460 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004461
4462 if (len == 0)
4463 return -EINVAL;
4464
Josef Bacik975f84f2010-11-23 19:36:57 +00004465 path = btrfs_alloc_path();
4466 if (!path)
4467 return -ENOMEM;
4468 path->leave_spinning = 1;
4469
Jeff Mahoneyda170662016-06-15 09:22:56 -04004470 start = round_down(start, btrfs_inode_sectorsize(inode));
4471 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004472
Chris Masonec29ed52011-02-23 16:23:20 -05004473 /*
4474 * lookup the last file extent. We're not using i_size here
4475 * because there might be preallocation past i_size
4476 */
David Sterbaf85b7372017-01-20 14:54:07 +01004477 ret = btrfs_lookup_file_extent(NULL, root, path,
4478 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004479 if (ret < 0) {
4480 btrfs_free_path(path);
4481 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004482 } else {
4483 WARN_ON(!ret);
4484 if (ret == 1)
4485 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004486 }
Liu Bo2d324f52016-05-17 17:21:48 -07004487
Josef Bacik975f84f2010-11-23 19:36:57 +00004488 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004489 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004490 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004491
Chris Masonec29ed52011-02-23 16:23:20 -05004492 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004493 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004494 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004495 /* have to trust i_size as the end */
4496 last = (u64)-1;
4497 last_for_get_extent = isize;
4498 } else {
4499 /*
4500 * remember the start of the last extent. There are a
4501 * bunch of different factors that go into the length of the
4502 * extent, so its much less complex to remember where it started
4503 */
4504 last = found_key.offset;
4505 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004506 }
Liu Bofe09e162013-09-22 12:54:23 +08004507 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004508
Chris Masonec29ed52011-02-23 16:23:20 -05004509 /*
4510 * we might have some extents allocated but more delalloc past those
4511 * extents. so, we trust isize unless the start of the last extent is
4512 * beyond isize
4513 */
4514 if (last < isize) {
4515 last = (u64)-1;
4516 last_for_get_extent = isize;
4517 }
4518
David Sterbaff13db42015-12-03 14:30:40 +01004519 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004520 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004521
David Sterbae3350e12017-06-23 04:09:57 +02004522 em = get_extent_skip_holes(inode, start, last_for_get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004523 if (!em)
4524 goto out;
4525 if (IS_ERR(em)) {
4526 ret = PTR_ERR(em);
4527 goto out;
4528 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004529
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004530 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004531 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004532
Chris Masonea8efc72011-03-08 11:54:40 -05004533 /* break if the extent we found is outside the range */
4534 if (em->start >= max || extent_map_end(em) < off)
4535 break;
4536
4537 /*
4538 * get_extent may return an extent that starts before our
4539 * requested range. We have to make sure the ranges
4540 * we return to fiemap always move forward and don't
4541 * overlap, so adjust the offsets here
4542 */
4543 em_start = max(em->start, off);
4544
4545 /*
4546 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004547 * for adjusting the disk offset below. Only do this if the
4548 * extent isn't compressed since our in ram offset may be past
4549 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004550 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004551 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4552 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004553 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004554 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004555 disko = 0;
4556 flags = 0;
4557
Chris Masonea8efc72011-03-08 11:54:40 -05004558 /*
4559 * bump off for our next call to get_extent
4560 */
4561 off = extent_map_end(em);
4562 if (off >= max)
4563 end = 1;
4564
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004565 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004566 end = 1;
4567 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004568 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004569 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4570 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004571 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004572 flags |= (FIEMAP_EXTENT_DELALLOC |
4573 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004574 } else if (fieinfo->fi_extents_max) {
4575 u64 bytenr = em->block_start -
4576 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004577
Chris Masonea8efc72011-03-08 11:54:40 -05004578 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004579
4580 /*
4581 * As btrfs supports shared space, this information
4582 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004583 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4584 * then we're just getting a count and we can skip the
4585 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004586 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004587 ret = btrfs_check_shared(root,
4588 btrfs_ino(BTRFS_I(inode)),
4589 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004590 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004591 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004592 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004593 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004594 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004595 }
4596 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4597 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004598 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4599 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004600
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004601 free_extent_map(em);
4602 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004603 if ((em_start >= last) || em_len == (u64)-1 ||
4604 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004605 flags |= FIEMAP_EXTENT_LAST;
4606 end = 1;
4607 }
4608
Chris Masonec29ed52011-02-23 16:23:20 -05004609 /* now scan forward to see if this is really the last extent. */
David Sterbae3350e12017-06-23 04:09:57 +02004610 em = get_extent_skip_holes(inode, off, last_for_get_extent);
Chris Masonec29ed52011-02-23 16:23:20 -05004611 if (IS_ERR(em)) {
4612 ret = PTR_ERR(em);
4613 goto out;
4614 }
4615 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004616 flags |= FIEMAP_EXTENT_LAST;
4617 end = 1;
4618 }
Qu Wenruo47518322017-04-07 10:43:15 +08004619 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4620 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004621 if (ret) {
4622 if (ret == 1)
4623 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004624 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004625 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004626 }
4627out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004628 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004629 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004630 free_extent_map(em);
4631out:
Liu Bofe09e162013-09-22 12:54:23 +08004632 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004633 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004634 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004635 return ret;
4636}
4637
Chris Mason727011e2010-08-06 13:21:20 -04004638static void __free_extent_buffer(struct extent_buffer *eb)
4639{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004640 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004641 kmem_cache_free(extent_buffer_cache, eb);
4642}
4643
Josef Bacika26e8c92014-03-28 17:07:27 -04004644int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004645{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004646 return (atomic_read(&eb->io_pages) ||
4647 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4648 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004649}
4650
Miao Xie897ca6e92010-10-26 20:57:29 -04004651/*
4652 * Helper for releasing extent buffer page.
4653 */
David Sterbaa50924e2014-07-31 00:51:36 +02004654static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004655{
4656 unsigned long index;
4657 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004658 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004659
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004660 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004661
David Sterbaa50924e2014-07-31 00:51:36 +02004662 index = num_extent_pages(eb->start, eb->len);
4663 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004664 return;
4665
4666 do {
4667 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004668 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004669 if (!page)
4670 continue;
4671 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004672 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004673 /*
4674 * We do this since we'll remove the pages after we've
4675 * removed the eb from the radix tree, so we could race
4676 * and have this page now attached to the new eb. So
4677 * only clear page_private if it's still connected to
4678 * this eb.
4679 */
4680 if (PagePrivate(page) &&
4681 page->private == (unsigned long)eb) {
4682 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4683 BUG_ON(PageDirty(page));
4684 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004685 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004686 * We need to make sure we haven't be attached
4687 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004688 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004689 ClearPagePrivate(page);
4690 set_page_private(page, 0);
4691 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004692 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004693 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004694
4695 if (mapped)
4696 spin_unlock(&page->mapping->private_lock);
4697
Nicholas D Steeves01327612016-05-19 21:18:45 -04004698 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004699 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004700 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004701}
4702
4703/*
4704 * Helper for releasing the extent buffer.
4705 */
4706static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4707{
David Sterbaa50924e2014-07-31 00:51:36 +02004708 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004709 __free_extent_buffer(eb);
4710}
4711
Josef Bacikf28491e2013-12-16 13:24:27 -05004712static struct extent_buffer *
4713__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004714 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004715{
4716 struct extent_buffer *eb = NULL;
4717
Michal Hockod1b5c562015-08-19 14:17:40 +02004718 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004719 eb->start = start;
4720 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004721 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004722 eb->bflags = 0;
4723 rwlock_init(&eb->lock);
4724 atomic_set(&eb->write_locks, 0);
4725 atomic_set(&eb->read_locks, 0);
4726 atomic_set(&eb->blocking_readers, 0);
4727 atomic_set(&eb->blocking_writers, 0);
4728 atomic_set(&eb->spinning_readers, 0);
4729 atomic_set(&eb->spinning_writers, 0);
4730 eb->lock_nested = 0;
4731 init_waitqueue_head(&eb->write_lock_wq);
4732 init_waitqueue_head(&eb->read_lock_wq);
4733
4734 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4735
4736 spin_lock_init(&eb->refs_lock);
4737 atomic_set(&eb->refs, 1);
4738 atomic_set(&eb->io_pages, 0);
4739
4740 /*
4741 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4742 */
4743 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4744 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4745 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4746
4747 return eb;
4748}
4749
4750struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4751{
4752 unsigned long i;
4753 struct page *p;
4754 struct extent_buffer *new;
4755 unsigned long num_pages = num_extent_pages(src->start, src->len);
4756
David Sterba3f556f72014-06-15 03:20:26 +02004757 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004758 if (new == NULL)
4759 return NULL;
4760
4761 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004762 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004763 if (!p) {
4764 btrfs_release_extent_buffer(new);
4765 return NULL;
4766 }
4767 attach_extent_buffer_page(new, p);
4768 WARN_ON(PageDirty(p));
4769 SetPageUptodate(p);
4770 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004771 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004772 }
4773
Josef Bacikdb7f3432013-08-07 14:54:37 -04004774 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4775 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4776
4777 return new;
4778}
4779
Omar Sandoval0f331222015-09-29 20:50:31 -07004780struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4781 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004782{
4783 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004784 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004785 unsigned long i;
4786
Omar Sandoval0f331222015-09-29 20:50:31 -07004787 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004788
4789 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004790 if (!eb)
4791 return NULL;
4792
4793 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004794 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004795 if (!eb->pages[i])
4796 goto err;
4797 }
4798 set_extent_buffer_uptodate(eb);
4799 btrfs_set_header_nritems(eb, 0);
4800 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4801
4802 return eb;
4803err:
4804 for (; i > 0; i--)
4805 __free_page(eb->pages[i - 1]);
4806 __free_extent_buffer(eb);
4807 return NULL;
4808}
4809
Omar Sandoval0f331222015-09-29 20:50:31 -07004810struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004811 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004812{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004813 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004814}
4815
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004816static void check_buffer_tree_ref(struct extent_buffer *eb)
4817{
Chris Mason242e18c2013-01-29 17:49:37 -05004818 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004819 /* the ref bit is tricky. We have to make sure it is set
4820 * if we have the buffer dirty. Otherwise the
4821 * code to free a buffer can end up dropping a dirty
4822 * page
4823 *
4824 * Once the ref bit is set, it won't go away while the
4825 * buffer is dirty or in writeback, and it also won't
4826 * go away while we have the reference count on the
4827 * eb bumped.
4828 *
4829 * We can't just set the ref bit without bumping the
4830 * ref on the eb because free_extent_buffer might
4831 * see the ref bit and try to clear it. If this happens
4832 * free_extent_buffer might end up dropping our original
4833 * ref by mistake and freeing the page before we are able
4834 * to add one more ref.
4835 *
4836 * So bump the ref count first, then set the bit. If someone
4837 * beat us to it, drop the ref we added.
4838 */
Chris Mason242e18c2013-01-29 17:49:37 -05004839 refs = atomic_read(&eb->refs);
4840 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4841 return;
4842
Josef Bacik594831c2012-07-20 16:11:08 -04004843 spin_lock(&eb->refs_lock);
4844 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004845 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004846 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004847}
4848
Mel Gorman2457aec2014-06-04 16:10:31 -07004849static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4850 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004851{
4852 unsigned long num_pages, i;
4853
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004854 check_buffer_tree_ref(eb);
4855
Josef Bacik5df42352012-03-15 18:24:42 -04004856 num_pages = num_extent_pages(eb->start, eb->len);
4857 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004858 struct page *p = eb->pages[i];
4859
Mel Gorman2457aec2014-06-04 16:10:31 -07004860 if (p != accessed)
4861 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004862 }
4863}
4864
Josef Bacikf28491e2013-12-16 13:24:27 -05004865struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4866 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004867{
4868 struct extent_buffer *eb;
4869
4870 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004871 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004872 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004873 if (eb && atomic_inc_not_zero(&eb->refs)) {
4874 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004875 /*
4876 * Lock our eb's refs_lock to avoid races with
4877 * free_extent_buffer. When we get our eb it might be flagged
4878 * with EXTENT_BUFFER_STALE and another task running
4879 * free_extent_buffer might have seen that flag set,
4880 * eb->refs == 2, that the buffer isn't under IO (dirty and
4881 * writeback flags not set) and it's still in the tree (flag
4882 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4883 * of decrementing the extent buffer's reference count twice.
4884 * So here we could race and increment the eb's reference count,
4885 * clear its stale flag, mark it as dirty and drop our reference
4886 * before the other task finishes executing free_extent_buffer,
4887 * which would later result in an attempt to free an extent
4888 * buffer that is dirty.
4889 */
4890 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4891 spin_lock(&eb->refs_lock);
4892 spin_unlock(&eb->refs_lock);
4893 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004894 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004895 return eb;
4896 }
4897 rcu_read_unlock();
4898
4899 return NULL;
4900}
4901
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004902#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4903struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004904 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004905{
4906 struct extent_buffer *eb, *exists = NULL;
4907 int ret;
4908
4909 eb = find_extent_buffer(fs_info, start);
4910 if (eb)
4911 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004912 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004913 if (!eb)
4914 return NULL;
4915 eb->fs_info = fs_info;
4916again:
David Sterbae1860a72016-05-09 14:11:38 +02004917 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004918 if (ret)
4919 goto free_eb;
4920 spin_lock(&fs_info->buffer_lock);
4921 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004922 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004923 spin_unlock(&fs_info->buffer_lock);
4924 radix_tree_preload_end();
4925 if (ret == -EEXIST) {
4926 exists = find_extent_buffer(fs_info, start);
4927 if (exists)
4928 goto free_eb;
4929 else
4930 goto again;
4931 }
4932 check_buffer_tree_ref(eb);
4933 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4934
4935 /*
4936 * We will free dummy extent buffer's if they come into
4937 * free_extent_buffer with a ref count of 2, but if we are using this we
4938 * want the buffers to stay in memory until we're done with them, so
4939 * bump the ref count again.
4940 */
4941 atomic_inc(&eb->refs);
4942 return eb;
4943free_eb:
4944 btrfs_release_extent_buffer(eb);
4945 return exists;
4946}
4947#endif
4948
Josef Bacikf28491e2013-12-16 13:24:27 -05004949struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004950 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004951{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004952 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004953 unsigned long num_pages = num_extent_pages(start, len);
4954 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004955 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004956 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004957 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004958 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004959 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004960 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004961 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004962
Jeff Mahoneyda170662016-06-15 09:22:56 -04004963 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004964 btrfs_err(fs_info, "bad tree block start %llu", start);
4965 return ERR_PTR(-EINVAL);
4966 }
4967
Josef Bacikf28491e2013-12-16 13:24:27 -05004968 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004969 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004970 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004971
David Sterba23d79d82014-06-15 02:55:29 +02004972 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004973 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004974 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004975
Chris Mason727011e2010-08-06 13:21:20 -04004976 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004977 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004978 if (!p) {
4979 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004980 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004981 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004982
4983 spin_lock(&mapping->private_lock);
4984 if (PagePrivate(p)) {
4985 /*
4986 * We could have already allocated an eb for this page
4987 * and attached one so lets see if we can get a ref on
4988 * the existing eb, and if we can we know it's good and
4989 * we can just return that one, else we know we can just
4990 * overwrite page->private.
4991 */
4992 exists = (struct extent_buffer *)p->private;
4993 if (atomic_inc_not_zero(&exists->refs)) {
4994 spin_unlock(&mapping->private_lock);
4995 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004996 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004997 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004998 goto free_eb;
4999 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005000 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005001
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005002 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005003 * Do this so attach doesn't complain and we need to
5004 * drop the ref the old guy had.
5005 */
5006 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005007 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005008 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005009 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005010 attach_extent_buffer_page(eb, p);
5011 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005012 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005013 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005014 if (!PageUptodate(p))
5015 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005016
5017 /*
5018 * see below about how we avoid a nasty race with release page
5019 * and why we unlock later
5020 */
Chris Masond1310b22008-01-24 16:13:08 -05005021 }
5022 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005023 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005024again:
David Sterbae1860a72016-05-09 14:11:38 +02005025 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005026 if (ret) {
5027 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005028 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005029 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005030
Josef Bacikf28491e2013-12-16 13:24:27 -05005031 spin_lock(&fs_info->buffer_lock);
5032 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005033 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005034 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005035 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005036 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005037 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005038 if (exists)
5039 goto free_eb;
5040 else
Josef Bacik115391d2012-03-09 09:51:43 -05005041 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005042 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005043 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005044 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005045 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005046
5047 /*
5048 * there is a race where release page may have
5049 * tried to find this extent buffer in the radix
5050 * but failed. It will tell the VM it is safe to
5051 * reclaim the, and it will clear the page private bit.
5052 * We must make sure to set the page private bit properly
5053 * after the extent buffer is in the radix tree so
5054 * it doesn't get lost
5055 */
Chris Mason727011e2010-08-06 13:21:20 -04005056 SetPageChecked(eb->pages[0]);
5057 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005058 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005059 ClearPageChecked(p);
5060 unlock_page(p);
5061 }
5062 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005063 return eb;
5064
Chris Mason6af118ce2008-07-22 11:18:07 -04005065free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005066 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005067 for (i = 0; i < num_pages; i++) {
5068 if (eb->pages[i])
5069 unlock_page(eb->pages[i]);
5070 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005071
Miao Xie897ca6e92010-10-26 20:57:29 -04005072 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005073 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005074}
Chris Masond1310b22008-01-24 16:13:08 -05005075
Josef Bacik3083ee22012-03-09 16:01:49 -05005076static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5077{
5078 struct extent_buffer *eb =
5079 container_of(head, struct extent_buffer, rcu_head);
5080
5081 __free_extent_buffer(eb);
5082}
5083
Josef Bacik3083ee22012-03-09 16:01:49 -05005084/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005085static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005086{
5087 WARN_ON(atomic_read(&eb->refs) == 0);
5088 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005089 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005090 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005091
Jan Schmidt815a51c2012-05-16 17:00:02 +02005092 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005093
Josef Bacikf28491e2013-12-16 13:24:27 -05005094 spin_lock(&fs_info->buffer_lock);
5095 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005096 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005097 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005098 } else {
5099 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005100 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005101
5102 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005103 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005104#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5105 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5106 __free_extent_buffer(eb);
5107 return 1;
5108 }
5109#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005110 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005111 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005112 }
5113 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005114
5115 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005116}
5117
Chris Masond1310b22008-01-24 16:13:08 -05005118void free_extent_buffer(struct extent_buffer *eb)
5119{
Chris Mason242e18c2013-01-29 17:49:37 -05005120 int refs;
5121 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005122 if (!eb)
5123 return;
5124
Chris Mason242e18c2013-01-29 17:49:37 -05005125 while (1) {
5126 refs = atomic_read(&eb->refs);
5127 if (refs <= 3)
5128 break;
5129 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5130 if (old == refs)
5131 return;
5132 }
5133
Josef Bacik3083ee22012-03-09 16:01:49 -05005134 spin_lock(&eb->refs_lock);
5135 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005136 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5137 atomic_dec(&eb->refs);
5138
5139 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005140 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005141 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005142 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5143 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005144
Josef Bacik3083ee22012-03-09 16:01:49 -05005145 /*
5146 * I know this is terrible, but it's temporary until we stop tracking
5147 * the uptodate bits and such for the extent buffers.
5148 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005149 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005150}
Chris Masond1310b22008-01-24 16:13:08 -05005151
Josef Bacik3083ee22012-03-09 16:01:49 -05005152void free_extent_buffer_stale(struct extent_buffer *eb)
5153{
5154 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005155 return;
5156
Josef Bacik3083ee22012-03-09 16:01:49 -05005157 spin_lock(&eb->refs_lock);
5158 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5159
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005160 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005161 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5162 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005163 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005164}
5165
Chris Mason1d4284b2012-03-28 20:31:37 -04005166void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005167{
Chris Masond1310b22008-01-24 16:13:08 -05005168 unsigned long i;
5169 unsigned long num_pages;
5170 struct page *page;
5171
Chris Masond1310b22008-01-24 16:13:08 -05005172 num_pages = num_extent_pages(eb->start, eb->len);
5173
5174 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005175 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005176 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005177 continue;
5178
Chris Masona61e6f22008-07-22 11:18:08 -04005179 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005180 WARN_ON(!PagePrivate(page));
5181
Chris Masond1310b22008-01-24 16:13:08 -05005182 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005183 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005184 if (!PageDirty(page)) {
5185 radix_tree_tag_clear(&page->mapping->page_tree,
5186 page_index(page),
5187 PAGECACHE_TAG_DIRTY);
5188 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005189 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005190 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005191 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005192 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005193 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005194}
Chris Masond1310b22008-01-24 16:13:08 -05005195
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005196int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005197{
5198 unsigned long i;
5199 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005200 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005201
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005202 check_buffer_tree_ref(eb);
5203
Chris Masonb9473432009-03-13 11:00:37 -04005204 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005205
Chris Masond1310b22008-01-24 16:13:08 -05005206 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005207 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005208 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5209
Chris Masonb9473432009-03-13 11:00:37 -04005210 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005211 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005212 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005213}
Chris Masond1310b22008-01-24 16:13:08 -05005214
David Sterba69ba3922015-12-03 13:08:59 +01005215void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005216{
5217 unsigned long i;
5218 struct page *page;
5219 unsigned long num_pages;
5220
Chris Masonb4ce94d2009-02-04 09:25:08 -05005221 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005222 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005223 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005224 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005225 if (page)
5226 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005227 }
Chris Mason1259ab72008-05-12 13:39:03 -04005228}
5229
David Sterba09c25a82015-12-03 13:08:59 +01005230void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005231{
5232 unsigned long i;
5233 struct page *page;
5234 unsigned long num_pages;
5235
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005236 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005237 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005238 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005239 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005240 SetPageUptodate(page);
5241 }
Chris Masond1310b22008-01-24 16:13:08 -05005242}
Chris Masond1310b22008-01-24 16:13:08 -05005243
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005244int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005245{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005246 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005247}
Chris Masond1310b22008-01-24 16:13:08 -05005248
5249int read_extent_buffer_pages(struct extent_io_tree *tree,
David Sterba6af49db2017-06-23 04:09:57 +02005250 struct extent_buffer *eb, int wait, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005251{
5252 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005253 struct page *page;
5254 int err;
5255 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005256 int locked_pages = 0;
5257 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005258 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005259 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005260 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005261 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005262
Chris Masonb4ce94d2009-02-04 09:25:08 -05005263 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005264 return 0;
5265
Chris Masond1310b22008-01-24 16:13:08 -05005266 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005267 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005268 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005269 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005270 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005271 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005272 } else {
5273 lock_page(page);
5274 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005275 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005276 }
5277 /*
5278 * We need to firstly lock all pages to make sure that
5279 * the uptodate bit of our pages won't be affected by
5280 * clear_extent_buffer_uptodate().
5281 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005282 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005283 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005284 if (!PageUptodate(page)) {
5285 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005286 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005287 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005288 }
Liu Bo2571e732016-08-03 12:33:01 -07005289
Chris Masonce9adaa2008-04-09 16:28:12 -04005290 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005291 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005292 goto unlock_exit;
5293 }
5294
Filipe Manana656f30d2014-09-26 12:25:56 +01005295 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005296 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005297 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005298 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005299 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005300
Chris Masonce9adaa2008-04-09 16:28:12 -04005301 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005302 if (ret) {
5303 atomic_dec(&eb->io_pages);
5304 unlock_page(page);
5305 continue;
5306 }
5307
Chris Masonf1885912008-04-09 16:28:12 -04005308 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005309 err = __extent_read_full_page(tree, page,
David Sterba6af49db2017-06-23 04:09:57 +02005310 btree_get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005311 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005312 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005313 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005314 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005315 /*
5316 * We use &bio in above __extent_read_full_page,
5317 * so we ensure that if it returns error, the
5318 * current page fails to add itself to bio and
5319 * it's been unlocked.
5320 *
5321 * We must dec io_pages by ourselves.
5322 */
5323 atomic_dec(&eb->io_pages);
5324 }
Chris Masond1310b22008-01-24 16:13:08 -05005325 } else {
5326 unlock_page(page);
5327 }
5328 }
5329
Jeff Mahoney355808c2011-10-03 23:23:14 -04005330 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005331 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005332 if (err)
5333 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005334 }
Chris Masona86c12c2008-02-07 10:50:54 -05005335
Arne Jansenbb82ab82011-06-10 14:06:53 +02005336 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005337 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005338
Josef Bacik8436ea912016-09-02 15:40:03 -04005339 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005340 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005341 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005342 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005343 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005344 }
Chris Masond3977122009-01-05 21:25:51 -05005345
Chris Masond1310b22008-01-24 16:13:08 -05005346 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005347
5348unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005349 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005350 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005351 page = eb->pages[locked_pages];
5352 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005353 }
5354 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005355}
Chris Masond1310b22008-01-24 16:13:08 -05005356
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005357void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5358 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005359{
5360 size_t cur;
5361 size_t offset;
5362 struct page *page;
5363 char *kaddr;
5364 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005365 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5366 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005367
Liu Bof716abd2017-08-09 11:10:16 -06005368 if (start + len > eb->len) {
5369 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5370 eb->start, eb->len, start, len);
5371 memset(dst, 0, len);
5372 return;
5373 }
Chris Masond1310b22008-01-24 16:13:08 -05005374
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005375 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005376
Chris Masond3977122009-01-05 21:25:51 -05005377 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005378 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005379
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005380 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005381 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005382 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005383
5384 dst += cur;
5385 len -= cur;
5386 offset = 0;
5387 i++;
5388 }
5389}
Chris Masond1310b22008-01-24 16:13:08 -05005390
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005391int read_extent_buffer_to_user(const struct extent_buffer *eb,
5392 void __user *dstv,
5393 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005394{
5395 size_t cur;
5396 size_t offset;
5397 struct page *page;
5398 char *kaddr;
5399 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005400 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5401 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005402 int ret = 0;
5403
5404 WARN_ON(start > eb->len);
5405 WARN_ON(start + len > eb->start + eb->len);
5406
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005407 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005408
5409 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005410 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005411
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005412 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005413 kaddr = page_address(page);
5414 if (copy_to_user(dst, kaddr + offset, cur)) {
5415 ret = -EFAULT;
5416 break;
5417 }
5418
5419 dst += cur;
5420 len -= cur;
5421 offset = 0;
5422 i++;
5423 }
5424
5425 return ret;
5426}
5427
Liu Bo415b35a2016-06-17 19:16:21 -07005428/*
5429 * return 0 if the item is found within a page.
5430 * return 1 if the item spans two pages.
5431 * return -EINVAL otherwise.
5432 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005433int map_private_extent_buffer(const struct extent_buffer *eb,
5434 unsigned long start, unsigned long min_len,
5435 char **map, unsigned long *map_start,
5436 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005437{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005438 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005439 char *kaddr;
5440 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005441 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5442 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005443 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005444 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005445
Liu Bof716abd2017-08-09 11:10:16 -06005446 if (start + min_len > eb->len) {
5447 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5448 eb->start, eb->len, start, min_len);
5449 return -EINVAL;
5450 }
5451
Chris Masond1310b22008-01-24 16:13:08 -05005452 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005453 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005454
5455 if (i == 0) {
5456 offset = start_offset;
5457 *map_start = 0;
5458 } else {
5459 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005460 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005461 }
Chris Masond3977122009-01-05 21:25:51 -05005462
David Sterbafb85fc92014-07-31 01:03:53 +02005463 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005464 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005465 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005466 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005467 return 0;
5468}
Chris Masond1310b22008-01-24 16:13:08 -05005469
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005470int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5471 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005472{
5473 size_t cur;
5474 size_t offset;
5475 struct page *page;
5476 char *kaddr;
5477 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005478 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5479 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005480 int ret = 0;
5481
5482 WARN_ON(start > eb->len);
5483 WARN_ON(start + len > eb->start + eb->len);
5484
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005485 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005486
Chris Masond3977122009-01-05 21:25:51 -05005487 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005488 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005489
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005490 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005491
Chris Masona6591712011-07-19 12:04:14 -04005492 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005493 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005494 if (ret)
5495 break;
5496
5497 ptr += cur;
5498 len -= cur;
5499 offset = 0;
5500 i++;
5501 }
5502 return ret;
5503}
Chris Masond1310b22008-01-24 16:13:08 -05005504
David Sterbaf157bf72016-11-09 17:43:38 +01005505void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5506 const void *srcv)
5507{
5508 char *kaddr;
5509
5510 WARN_ON(!PageUptodate(eb->pages[0]));
5511 kaddr = page_address(eb->pages[0]);
5512 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5513 BTRFS_FSID_SIZE);
5514}
5515
5516void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5517{
5518 char *kaddr;
5519
5520 WARN_ON(!PageUptodate(eb->pages[0]));
5521 kaddr = page_address(eb->pages[0]);
5522 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5523 BTRFS_FSID_SIZE);
5524}
5525
Chris Masond1310b22008-01-24 16:13:08 -05005526void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5527 unsigned long start, unsigned long len)
5528{
5529 size_t cur;
5530 size_t offset;
5531 struct page *page;
5532 char *kaddr;
5533 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005534 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5535 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005536
5537 WARN_ON(start > eb->len);
5538 WARN_ON(start + len > eb->start + eb->len);
5539
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005540 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005541
Chris Masond3977122009-01-05 21:25:51 -05005542 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005543 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005544 WARN_ON(!PageUptodate(page));
5545
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005546 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005547 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005548 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005549
5550 src += cur;
5551 len -= cur;
5552 offset = 0;
5553 i++;
5554 }
5555}
Chris Masond1310b22008-01-24 16:13:08 -05005556
David Sterbab159fa22016-11-08 18:09:03 +01005557void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5558 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005559{
5560 size_t cur;
5561 size_t offset;
5562 struct page *page;
5563 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005564 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5565 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005566
5567 WARN_ON(start > eb->len);
5568 WARN_ON(start + len > eb->start + eb->len);
5569
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005570 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005571
Chris Masond3977122009-01-05 21:25:51 -05005572 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005573 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005574 WARN_ON(!PageUptodate(page));
5575
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005576 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005577 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005578 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005579
5580 len -= cur;
5581 offset = 0;
5582 i++;
5583 }
5584}
Chris Masond1310b22008-01-24 16:13:08 -05005585
David Sterba58e80122016-11-08 18:30:31 +01005586void copy_extent_buffer_full(struct extent_buffer *dst,
5587 struct extent_buffer *src)
5588{
5589 int i;
5590 unsigned num_pages;
5591
5592 ASSERT(dst->len == src->len);
5593
5594 num_pages = num_extent_pages(dst->start, dst->len);
5595 for (i = 0; i < num_pages; i++)
5596 copy_page(page_address(dst->pages[i]),
5597 page_address(src->pages[i]));
5598}
5599
Chris Masond1310b22008-01-24 16:13:08 -05005600void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5601 unsigned long dst_offset, unsigned long src_offset,
5602 unsigned long len)
5603{
5604 u64 dst_len = dst->len;
5605 size_t cur;
5606 size_t offset;
5607 struct page *page;
5608 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005609 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5610 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005611
5612 WARN_ON(src->len != dst_len);
5613
5614 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005615 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005616
Chris Masond3977122009-01-05 21:25:51 -05005617 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005618 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005619 WARN_ON(!PageUptodate(page));
5620
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005621 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005622
Chris Masona6591712011-07-19 12:04:14 -04005623 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005624 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005625
5626 src_offset += cur;
5627 len -= cur;
5628 offset = 0;
5629 i++;
5630 }
5631}
Chris Masond1310b22008-01-24 16:13:08 -05005632
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005633void le_bitmap_set(u8 *map, unsigned int start, int len)
5634{
5635 u8 *p = map + BIT_BYTE(start);
5636 const unsigned int size = start + len;
5637 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5638 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5639
5640 while (len - bits_to_set >= 0) {
5641 *p |= mask_to_set;
5642 len -= bits_to_set;
5643 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005644 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005645 p++;
5646 }
5647 if (len) {
5648 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5649 *p |= mask_to_set;
5650 }
5651}
5652
5653void le_bitmap_clear(u8 *map, unsigned int start, int len)
5654{
5655 u8 *p = map + BIT_BYTE(start);
5656 const unsigned int size = start + len;
5657 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5658 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5659
5660 while (len - bits_to_clear >= 0) {
5661 *p &= ~mask_to_clear;
5662 len -= bits_to_clear;
5663 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005664 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005665 p++;
5666 }
5667 if (len) {
5668 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5669 *p &= ~mask_to_clear;
5670 }
5671}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005672
5673/*
5674 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5675 * given bit number
5676 * @eb: the extent buffer
5677 * @start: offset of the bitmap item in the extent buffer
5678 * @nr: bit number
5679 * @page_index: return index of the page in the extent buffer that contains the
5680 * given bit number
5681 * @page_offset: return offset into the page given by page_index
5682 *
5683 * This helper hides the ugliness of finding the byte in an extent buffer which
5684 * contains a given bit.
5685 */
5686static inline void eb_bitmap_offset(struct extent_buffer *eb,
5687 unsigned long start, unsigned long nr,
5688 unsigned long *page_index,
5689 size_t *page_offset)
5690{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005691 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005692 size_t byte_offset = BIT_BYTE(nr);
5693 size_t offset;
5694
5695 /*
5696 * The byte we want is the offset of the extent buffer + the offset of
5697 * the bitmap item in the extent buffer + the offset of the byte in the
5698 * bitmap item.
5699 */
5700 offset = start_offset + start + byte_offset;
5701
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005702 *page_index = offset >> PAGE_SHIFT;
5703 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005704}
5705
5706/**
5707 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5708 * @eb: the extent buffer
5709 * @start: offset of the bitmap item in the extent buffer
5710 * @nr: bit number to test
5711 */
5712int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5713 unsigned long nr)
5714{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005715 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005716 struct page *page;
5717 unsigned long i;
5718 size_t offset;
5719
5720 eb_bitmap_offset(eb, start, nr, &i, &offset);
5721 page = eb->pages[i];
5722 WARN_ON(!PageUptodate(page));
5723 kaddr = page_address(page);
5724 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5725}
5726
5727/**
5728 * extent_buffer_bitmap_set - set an area of a bitmap
5729 * @eb: the extent buffer
5730 * @start: offset of the bitmap item in the extent buffer
5731 * @pos: bit number of the first bit
5732 * @len: number of bits to set
5733 */
5734void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5735 unsigned long pos, unsigned long len)
5736{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005737 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005738 struct page *page;
5739 unsigned long i;
5740 size_t offset;
5741 const unsigned int size = pos + len;
5742 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005743 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005744
5745 eb_bitmap_offset(eb, start, pos, &i, &offset);
5746 page = eb->pages[i];
5747 WARN_ON(!PageUptodate(page));
5748 kaddr = page_address(page);
5749
5750 while (len >= bits_to_set) {
5751 kaddr[offset] |= mask_to_set;
5752 len -= bits_to_set;
5753 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005754 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005755 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005756 offset = 0;
5757 page = eb->pages[++i];
5758 WARN_ON(!PageUptodate(page));
5759 kaddr = page_address(page);
5760 }
5761 }
5762 if (len) {
5763 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5764 kaddr[offset] |= mask_to_set;
5765 }
5766}
5767
5768
5769/**
5770 * extent_buffer_bitmap_clear - clear an area of a bitmap
5771 * @eb: the extent buffer
5772 * @start: offset of the bitmap item in the extent buffer
5773 * @pos: bit number of the first bit
5774 * @len: number of bits to clear
5775 */
5776void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5777 unsigned long pos, unsigned long len)
5778{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005779 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005780 struct page *page;
5781 unsigned long i;
5782 size_t offset;
5783 const unsigned int size = pos + len;
5784 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005785 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005786
5787 eb_bitmap_offset(eb, start, pos, &i, &offset);
5788 page = eb->pages[i];
5789 WARN_ON(!PageUptodate(page));
5790 kaddr = page_address(page);
5791
5792 while (len >= bits_to_clear) {
5793 kaddr[offset] &= ~mask_to_clear;
5794 len -= bits_to_clear;
5795 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005796 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005797 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005798 offset = 0;
5799 page = eb->pages[++i];
5800 WARN_ON(!PageUptodate(page));
5801 kaddr = page_address(page);
5802 }
5803 }
5804 if (len) {
5805 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5806 kaddr[offset] &= ~mask_to_clear;
5807 }
5808}
5809
Sergei Trofimovich33872062011-04-11 21:52:52 +00005810static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5811{
5812 unsigned long distance = (src > dst) ? src - dst : dst - src;
5813 return distance < len;
5814}
5815
Chris Masond1310b22008-01-24 16:13:08 -05005816static void copy_pages(struct page *dst_page, struct page *src_page,
5817 unsigned long dst_off, unsigned long src_off,
5818 unsigned long len)
5819{
Chris Masona6591712011-07-19 12:04:14 -04005820 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005821 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005822 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005823
Sergei Trofimovich33872062011-04-11 21:52:52 +00005824 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005825 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005826 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005827 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005828 if (areas_overlap(src_off, dst_off, len))
5829 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005830 }
Chris Masond1310b22008-01-24 16:13:08 -05005831
Chris Mason727011e2010-08-06 13:21:20 -04005832 if (must_memmove)
5833 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5834 else
5835 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005836}
5837
5838void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5839 unsigned long src_offset, unsigned long len)
5840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005841 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005842 size_t cur;
5843 size_t dst_off_in_page;
5844 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005845 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005846 unsigned long dst_i;
5847 unsigned long src_i;
5848
5849 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005850 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005851 "memmove bogus src_offset %lu move len %lu dst len %lu",
5852 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005853 BUG_ON(1);
5854 }
5855 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005856 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005857 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5858 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005859 BUG_ON(1);
5860 }
5861
Chris Masond3977122009-01-05 21:25:51 -05005862 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005863 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005864 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005865 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005866 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005867
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005868 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5869 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005870
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005871 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005872 src_off_in_page));
5873 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005874 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005875
David Sterbafb85fc92014-07-31 01:03:53 +02005876 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005877 dst_off_in_page, src_off_in_page, cur);
5878
5879 src_offset += cur;
5880 dst_offset += cur;
5881 len -= cur;
5882 }
5883}
Chris Masond1310b22008-01-24 16:13:08 -05005884
5885void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5886 unsigned long src_offset, unsigned long len)
5887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005888 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005889 size_t cur;
5890 size_t dst_off_in_page;
5891 size_t src_off_in_page;
5892 unsigned long dst_end = dst_offset + len - 1;
5893 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005894 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005895 unsigned long dst_i;
5896 unsigned long src_i;
5897
5898 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005899 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005900 "memmove bogus src_offset %lu move len %lu len %lu",
5901 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005902 BUG_ON(1);
5903 }
5904 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005905 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005906 "memmove bogus dst_offset %lu move len %lu len %lu",
5907 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005908 BUG_ON(1);
5909 }
Chris Mason727011e2010-08-06 13:21:20 -04005910 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005911 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5912 return;
5913 }
Chris Masond3977122009-01-05 21:25:51 -05005914 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005915 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5916 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005917
5918 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005919 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005920 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005921 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005922
5923 cur = min_t(unsigned long, len, src_off_in_page + 1);
5924 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005925 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005926 dst_off_in_page - cur + 1,
5927 src_off_in_page - cur + 1, cur);
5928
5929 dst_end -= cur;
5930 src_end -= cur;
5931 len -= cur;
5932 }
5933}
Chris Mason6af118ce2008-07-22 11:18:07 -04005934
David Sterbaf7a52a42013-04-26 14:56:29 +00005935int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005936{
Chris Mason6af118ce2008-07-22 11:18:07 -04005937 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005938
Miao Xie19fe0a82010-10-26 20:57:29 -04005939 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005940 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005941 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005942 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005943 spin_lock(&page->mapping->private_lock);
5944 if (!PagePrivate(page)) {
5945 spin_unlock(&page->mapping->private_lock);
5946 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005947 }
5948
Josef Bacik3083ee22012-03-09 16:01:49 -05005949 eb = (struct extent_buffer *)page->private;
5950 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005951
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005952 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005953 * This is a little awful but should be ok, we need to make sure that
5954 * the eb doesn't disappear out from under us while we're looking at
5955 * this page.
5956 */
5957 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005958 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005959 spin_unlock(&eb->refs_lock);
5960 spin_unlock(&page->mapping->private_lock);
5961 return 0;
5962 }
5963 spin_unlock(&page->mapping->private_lock);
5964
Josef Bacik3083ee22012-03-09 16:01:49 -05005965 /*
5966 * If tree ref isn't set then we know the ref on this eb is a real ref,
5967 * so just return, this page will likely be freed soon anyway.
5968 */
5969 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5970 spin_unlock(&eb->refs_lock);
5971 return 0;
5972 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005973
David Sterbaf7a52a42013-04-26 14:56:29 +00005974 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005975}