blob: 0dfcef37352b0127d25fbe561acafe7f139be950 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/spinlock.h>
8#include <linux/blkdev.h>
9#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050010#include <linux/writeback.h>
11#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070012#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020018#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040020#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040021#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080022#include "backref.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
Chris Mason9be33952013-05-17 18:30:14 -040026static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050027
Filipe Manana27a35072014-07-06 20:09:59 +010028static inline bool extent_state_in_tree(const struct extent_state *state)
29{
30 return !RB_EMPTY_NODE(&state->rb_node);
31}
32
Eric Sandeen6d49ba12013-04-22 16:12:31 +000033#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050034static LIST_HEAD(buffers);
35static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040036
Chris Masond3977122009-01-05 21:25:51 -050037static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000038
39static inline
40void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
41{
42 unsigned long flags;
43
44 spin_lock_irqsave(&leak_lock, flags);
45 list_add(new, head);
46 spin_unlock_irqrestore(&leak_lock, flags);
47}
48
49static inline
50void btrfs_leak_debug_del(struct list_head *entry)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&leak_lock, flags);
55 list_del(entry);
56 spin_unlock_irqrestore(&leak_lock, flags);
57}
58
59static inline
60void btrfs_leak_debug_check(void)
61{
62 struct extent_state *state;
63 struct extent_buffer *eb;
64
65 while (!list_empty(&states)) {
66 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010067 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010068 state->start, state->end, state->state,
69 extent_state_in_tree(state),
Elena Reshetovab7ac31b2017-03-03 10:55:19 +020070 refcount_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000071 list_del(&state->leak_list);
72 kmem_cache_free(extent_state_cache, state);
73 }
74
75 while (!list_empty(&buffers)) {
76 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Jeff Mahoney62e85572016-09-20 10:05:01 -040077 pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020078 eb->start, eb->len, atomic_read(&eb->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000079 list_del(&eb->leak_list);
80 kmem_cache_free(extent_buffer_cache, eb);
81 }
82}
David Sterba8d599ae2013-04-30 15:22:23 +000083
Josef Bacika5dee372013-12-13 10:02:44 -050084#define btrfs_debug_check_extent_io_range(tree, start, end) \
85 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000086static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050087 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000088{
Josef Bacikc6100a42017-05-05 11:57:13 -040089 if (tree->ops && tree->ops->check_extent_io_range)
90 tree->ops->check_extent_io_range(tree->private_data, caller,
91 start, end);
David Sterba8d599ae2013-04-30 15:22:23 +000092}
Eric Sandeen6d49ba12013-04-22 16:12:31 +000093#else
94#define btrfs_leak_debug_add(new, head) do {} while (0)
95#define btrfs_leak_debug_del(entry) do {} while (0)
96#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +000097#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -040098#endif
Chris Masond1310b22008-01-24 16:13:08 -050099
Chris Masond1310b22008-01-24 16:13:08 -0500100#define BUFFER_LRU_MAX 64
101
102struct tree_entry {
103 u64 start;
104 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500105 struct rb_node rb_node;
106};
107
108struct extent_page_data {
109 struct bio *bio;
110 struct extent_io_tree *tree;
111 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -0500112
113 /* 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 */
Qu Wenruofefdc552015-10-12 15:35:38 +0800583static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
584 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 Sterbab5a4ba142016-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,
1297 struct extent_state **cached, gfp_t mask)
1298{
1299 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1300 cached, mask, NULL);
1301}
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,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001350 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
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,
1746 NULL, GFP_NOFS);
1747
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
Ilya Dryomov908960c2013-11-03 19:06:39 +02001986 ASSERT(!(fs_info->sb->s_flags & MS_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 Christie81a75f672016-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,
Chris Masond1310b22008-01-24 16:13:08 -05002763 struct page *page, sector_t sector,
2764 size_t size, unsigned long offset,
2765 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);
Chris Masond1310b22008-01-24 16:13:08 -05002778
2779 if (bio_ret && *bio_ret) {
2780 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002781 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002782 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002783 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002784 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002785
2786 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002787 force_bio_submit ||
Mike Christie1f7ad752016-06-05 14:31:51 -05002788 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002789 bio_add_page(bio, page, page_size, offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002790 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002791 if (ret < 0) {
2792 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002793 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002794 }
Chris Masond1310b22008-01-24 16:13:08 -05002795 bio = NULL;
2796 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002797 if (wbc)
2798 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002799 return 0;
2800 }
2801 }
Chris Masonc8b97812008-10-29 14:49:59 -04002802
Goffredo Baroncelli2d8ce702017-10-03 19:31:10 +02002803 bio = btrfs_bio_alloc(bdev, (u64)sector << 9);
Chris Masonc8b97812008-10-29 14:49:59 -04002804 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002805 bio->bi_end_io = end_io_func;
2806 bio->bi_private = tree;
Jens Axboee6959b92017-06-27 11:51:28 -06002807 bio->bi_write_hint = page->mapping->host->i_write_hint;
David Sterba4b81ba42017-06-06 19:14:26 +02002808 bio->bi_opf = opf;
Chris Masonda2f0f72015-07-02 13:57:22 -07002809 if (wbc) {
2810 wbc_init_bio(wbc, bio);
2811 wbc_account_io(wbc, page, page_size);
2812 }
Chris Mason70dec802008-01-29 09:59:12 -05002813
Chris Masond3977122009-01-05 21:25:51 -05002814 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002815 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002816 else
Mike Christie1f7ad752016-06-05 14:31:51 -05002817 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002818
2819 return ret;
2820}
2821
Eric Sandeen48a3b632013-04-25 20:41:01 +00002822static void attach_extent_buffer_page(struct extent_buffer *eb,
2823 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002824{
2825 if (!PagePrivate(page)) {
2826 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002827 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002828 set_page_private(page, (unsigned long)eb);
2829 } else {
2830 WARN_ON(page->private != (unsigned long)eb);
2831 }
2832}
2833
Chris Masond1310b22008-01-24 16:13:08 -05002834void set_page_extent_mapped(struct page *page)
2835{
2836 if (!PagePrivate(page)) {
2837 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002838 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002839 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002840 }
2841}
2842
Miao Xie125bac012013-07-25 19:22:37 +08002843static struct extent_map *
2844__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2845 u64 start, u64 len, get_extent_t *get_extent,
2846 struct extent_map **em_cached)
2847{
2848 struct extent_map *em;
2849
2850 if (em_cached && *em_cached) {
2851 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002852 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002853 start < extent_map_end(em)) {
Elena Reshetova490b54d2017-03-03 10:55:12 +02002854 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002855 return em;
2856 }
2857
2858 free_extent_map(em);
2859 *em_cached = NULL;
2860 }
2861
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02002862 em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
Miao Xie125bac012013-07-25 19:22:37 +08002863 if (em_cached && !IS_ERR_OR_NULL(em)) {
2864 BUG_ON(*em_cached);
Elena Reshetova490b54d2017-03-03 10:55:12 +02002865 refcount_inc(&em->refs);
Miao Xie125bac012013-07-25 19:22:37 +08002866 *em_cached = em;
2867 }
2868 return em;
2869}
Chris Masond1310b22008-01-24 16:13:08 -05002870/*
2871 * basic readpage implementation. Locked extent state structs are inserted
2872 * into the tree that are removed when the IO is done (by the end_io
2873 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002874 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002875 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002876 */
Miao Xie99740902013-07-25 19:22:36 +08002877static int __do_readpage(struct extent_io_tree *tree,
2878 struct page *page,
2879 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002880 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002881 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02002882 unsigned long *bio_flags, unsigned int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002883 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002884{
2885 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002886 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002887 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002888 u64 end;
2889 u64 cur = start;
2890 u64 extent_offset;
2891 u64 last_byte = i_size_read(inode);
2892 u64 block_start;
2893 u64 cur_end;
2894 sector_t sector;
2895 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;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002930
Chris Masond1310b22008-01-24 16:13:08 -05002931 if (cur >= last_byte) {
2932 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002933 struct extent_state *cached = NULL;
2934
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002935 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002936 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002937 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002938 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002939 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002940 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002941 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002942 unlock_extent_cached(tree, cur,
2943 cur + iosize - 1,
2944 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002945 break;
2946 }
Miao Xie125bac012013-07-25 19:22:37 +08002947 em = __get_extent_map(inode, page, pg_offset, cur,
2948 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002949 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002950 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002951 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002952 break;
2953 }
Chris Masond1310b22008-01-24 16:13:08 -05002954 extent_offset = cur - em->start;
2955 BUG_ON(extent_map_end(em) <= cur);
2956 BUG_ON(end < cur);
2957
Li Zefan261507a02010-12-17 14:21:50 +08002958 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002959 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002960 extent_set_compress_type(&this_bio_flag,
2961 em->compress_type);
2962 }
Chris Masonc8b97812008-10-29 14:49:59 -04002963
Chris Masond1310b22008-01-24 16:13:08 -05002964 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2965 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002966 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002967 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2968 disk_io_size = em->block_len;
2969 sector = em->block_start >> 9;
2970 } else {
2971 sector = (em->block_start + extent_offset) >> 9;
2972 disk_io_size = iosize;
2973 }
Chris Masond1310b22008-01-24 16:13:08 -05002974 bdev = em->bdev;
2975 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002976 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2977 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002978
2979 /*
2980 * If we have a file range that points to a compressed extent
2981 * and it's followed by a consecutive file range that points to
2982 * to the same compressed extent (possibly with a different
2983 * offset and/or length, so it either points to the whole extent
2984 * or only part of it), we must make sure we do not submit a
2985 * single bio to populate the pages for the 2 ranges because
2986 * this makes the compressed extent read zero out the pages
2987 * belonging to the 2nd range. Imagine the following scenario:
2988 *
2989 * File layout
2990 * [0 - 8K] [8K - 24K]
2991 * | |
2992 * | |
2993 * points to extent X, points to extent X,
2994 * offset 4K, length of 8K offset 0, length 16K
2995 *
2996 * [extent X, compressed length = 4K uncompressed length = 16K]
2997 *
2998 * If the bio to read the compressed extent covers both ranges,
2999 * it will decompress extent X into the pages belonging to the
3000 * first range and then it will stop, zeroing out the remaining
3001 * pages that belong to the other range that points to extent X.
3002 * So here we make sure we submit 2 bios, one for the first
3003 * range and another one for the third range. Both will target
3004 * the same physical extent from disk, but we can't currently
3005 * make the compressed bio endio callback populate the pages
3006 * for both ranges because each compressed bio is tightly
3007 * coupled with a single extent map, and each range can have
3008 * an extent map with a different offset value relative to the
3009 * uncompressed data of our extent and different lengths. This
3010 * is a corner case so we prioritize correctness over
3011 * non-optimal behavior (submitting 2 bios for the same extent).
3012 */
3013 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3014 prev_em_start && *prev_em_start != (u64)-1 &&
3015 *prev_em_start != em->orig_start)
3016 force_bio_submit = true;
3017
3018 if (prev_em_start)
3019 *prev_em_start = em->orig_start;
3020
Chris Masond1310b22008-01-24 16:13:08 -05003021 free_extent_map(em);
3022 em = NULL;
3023
3024 /* we've found a hole, just zero and go on */
3025 if (block_start == EXTENT_MAP_HOLE) {
3026 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003027 struct extent_state *cached = NULL;
3028
Cong Wang7ac687d2011-11-25 23:14:28 +08003029 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003030 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003031 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003032 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003033
3034 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003035 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003036 unlock_extent_cached(tree, cur,
3037 cur + iosize - 1,
3038 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003039 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003040 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003041 continue;
3042 }
3043 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003044 if (test_range_bit(tree, cur, cur_end,
3045 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003046 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003047 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003048 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003049 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003050 continue;
3051 }
Chris Mason70dec802008-01-29 09:59:12 -05003052 /* we have an inline extent but it didn't get marked up
3053 * to date. Error out
3054 */
3055 if (block_start == EXTENT_MAP_INLINE) {
3056 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003057 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003058 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003059 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003060 continue;
3061 }
Chris Masond1310b22008-01-24 16:13:08 -05003062
David Sterba4b81ba42017-06-06 19:14:26 +02003063 ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL,
Mike Christie1f7ad752016-06-05 14:31:51 -05003064 page, sector, disk_io_size, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003065 bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003066 end_bio_extent_readpage, mirror_num,
3067 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003068 this_bio_flag,
3069 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003070 if (!ret) {
3071 nr++;
3072 *bio_flags = this_bio_flag;
3073 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003074 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003075 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003076 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003077 }
Chris Masond1310b22008-01-24 16:13:08 -05003078 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003079 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003080 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003081out:
Chris Masond1310b22008-01-24 16:13:08 -05003082 if (!nr) {
3083 if (!PageError(page))
3084 SetPageUptodate(page);
3085 unlock_page(page);
3086 }
Liu Bobaf863b2016-07-11 10:39:07 -07003087 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003088}
3089
Miao Xie99740902013-07-25 19:22:36 +08003090static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3091 struct page *pages[], int nr_pages,
3092 u64 start, u64 end,
3093 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003094 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003095 struct bio **bio, int mirror_num,
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++) {
Miao Xie125bac012013-07-25 19:22:37 +08003116 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003117 mirror_num, 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[],
3124 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003125 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003126 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003127 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003128 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003129{
Stefan Behrens35a36212013-08-14 18:12:25 +02003130 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003131 u64 end = 0;
3132 u64 page_start;
3133 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003134 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003135
3136 for (index = 0; index < nr_pages; index++) {
3137 page_start = page_offset(pages[index]);
3138 if (!end) {
3139 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003140 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003141 first_index = index;
3142 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003143 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003144 } else {
3145 __do_contiguous_readpages(tree, &pages[first_index],
3146 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003147 end, get_extent, em_cached,
3148 bio, mirror_num, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003149 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003150 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003151 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003152 first_index = index;
3153 }
3154 }
3155
3156 if (end)
3157 __do_contiguous_readpages(tree, &pages[first_index],
3158 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003159 end, get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003160 mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003161 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003162}
3163
3164static int __extent_read_full_page(struct extent_io_tree *tree,
3165 struct page *page,
3166 get_extent_t *get_extent,
3167 struct bio **bio, int mirror_num,
David Sterbaf1c77c52017-06-06 19:03:49 +02003168 unsigned long *bio_flags,
3169 unsigned int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003170{
3171 struct inode *inode = page->mapping->host;
3172 struct btrfs_ordered_extent *ordered;
3173 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003174 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003175 int ret;
3176
3177 while (1) {
3178 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003179 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003180 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003181 if (!ordered)
3182 break;
3183 unlock_extent(tree, start, end);
3184 btrfs_start_ordered_extent(inode, ordered, 1);
3185 btrfs_put_ordered_extent(ordered);
3186 }
3187
Miao Xie125bac012013-07-25 19:22:37 +08003188 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003189 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003190 return ret;
3191}
3192
Chris Masond1310b22008-01-24 16:13:08 -05003193int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003194 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003195{
3196 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003197 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003198 int ret;
3199
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003200 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003201 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003202 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003203 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003204 return ret;
3205}
Chris Masond1310b22008-01-24 16:13:08 -05003206
David Sterba3d4b9492017-02-10 19:33:41 +01003207static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003208 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003209{
3210 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003211}
3212
Chris Masond1310b22008-01-24 16:13:08 -05003213/*
Chris Mason40f76582014-05-21 13:35:51 -07003214 * helper for __extent_writepage, doing all of the delayed allocation setup.
3215 *
3216 * This returns 1 if our fill_delalloc function did all the work required
3217 * to write the page (copy into inline extent). In this case the IO has
3218 * been started and the page is already unlocked.
3219 *
3220 * This returns 0 if all went well (page still locked)
3221 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003222 */
Chris Mason40f76582014-05-21 13:35:51 -07003223static noinline_for_stack int writepage_delalloc(struct inode *inode,
3224 struct page *page, struct writeback_control *wbc,
3225 struct extent_page_data *epd,
3226 u64 delalloc_start,
3227 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003228{
Chris Mason40f76582014-05-21 13:35:51 -07003229 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003230 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003231 u64 nr_delalloc;
3232 u64 delalloc_to_write = 0;
3233 u64 delalloc_end = 0;
3234 int ret;
3235 int page_started = 0;
3236
3237 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3238 return 0;
3239
3240 while (delalloc_end < page_end) {
3241 nr_delalloc = find_lock_delalloc_range(inode, tree,
3242 page,
3243 &delalloc_start,
3244 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003245 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003246 if (nr_delalloc == 0) {
3247 delalloc_start = delalloc_end + 1;
3248 continue;
3249 }
3250 ret = tree->ops->fill_delalloc(inode, page,
3251 delalloc_start,
3252 delalloc_end,
3253 &page_started,
3254 nr_written);
3255 /* File system has been set read-only */
3256 if (ret) {
3257 SetPageError(page);
3258 /* fill_delalloc should be return < 0 for error
3259 * but just in case, we use > 0 here meaning the
3260 * IO is started, so we don't want to return > 0
3261 * unless things are going well.
3262 */
3263 ret = ret < 0 ? ret : -EIO;
3264 goto done;
3265 }
3266 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003267 * delalloc_end is already one less than the total length, so
3268 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003269 */
3270 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003271 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003272 delalloc_start = delalloc_end + 1;
3273 }
3274 if (wbc->nr_to_write < delalloc_to_write) {
3275 int thresh = 8192;
3276
3277 if (delalloc_to_write < thresh * 2)
3278 thresh = delalloc_to_write;
3279 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3280 thresh);
3281 }
3282
3283 /* did the fill delalloc function already unlock and start
3284 * the IO?
3285 */
3286 if (page_started) {
3287 /*
3288 * we've unlocked the page, so we can't update
3289 * the mapping's writeback index, just update
3290 * nr_to_write.
3291 */
3292 wbc->nr_to_write -= *nr_written;
3293 return 1;
3294 }
3295
3296 ret = 0;
3297
3298done:
3299 return ret;
3300}
3301
3302/*
3303 * helper for __extent_writepage. This calls the writepage start hooks,
3304 * and does the loop to map the page into extents and bios.
3305 *
3306 * We return 1 if the IO is started and the page is unlocked,
3307 * 0 if all went well (page still locked)
3308 * < 0 if there were errors (page still locked)
3309 */
3310static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3311 struct page *page,
3312 struct writeback_control *wbc,
3313 struct extent_page_data *epd,
3314 loff_t i_size,
3315 unsigned long nr_written,
David Sterbaf1c77c52017-06-06 19:03:49 +02003316 unsigned int write_flags, int *nr_ret)
Chris Mason40f76582014-05-21 13:35:51 -07003317{
Chris Masond1310b22008-01-24 16:13:08 -05003318 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003319 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003320 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003321 u64 end;
3322 u64 cur = start;
3323 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003324 u64 block_start;
3325 u64 iosize;
3326 sector_t sector;
3327 struct extent_map *em;
3328 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003329 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003330 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003331 int ret = 0;
3332 int nr = 0;
3333 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003334
Chris Mason247e7432008-07-17 12:53:51 -04003335 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003336 ret = tree->ops->writepage_start_hook(page, start,
3337 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003338 if (ret) {
3339 /* Fixup worker will requeue */
3340 if (ret == -EBUSY)
3341 wbc->pages_skipped++;
3342 else
3343 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003344
David Sterba3d4b9492017-02-10 19:33:41 +01003345 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003346 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003347 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003348 }
3349 }
3350
Chris Mason11c83492009-04-20 15:50:09 -04003351 /*
3352 * we don't want to touch the inode after unlocking the page,
3353 * so we update the mapping writeback index now
3354 */
David Sterba3d4b9492017-02-10 19:33:41 +01003355 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003356
Chris Masond1310b22008-01-24 16:13:08 -05003357 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003358 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003359 if (tree->ops && tree->ops->writepage_end_io_hook)
3360 tree->ops->writepage_end_io_hook(page, start,
3361 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003362 goto done;
3363 }
3364
Chris Masond1310b22008-01-24 16:13:08 -05003365 blocksize = inode->i_sb->s_blocksize;
3366
3367 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003368 u64 em_end;
David Sterba58409ed2016-05-04 11:46:10 +02003369
Chris Mason40f76582014-05-21 13:35:51 -07003370 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003371 if (tree->ops && tree->ops->writepage_end_io_hook)
3372 tree->ops->writepage_end_io_hook(page, cur,
3373 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003374 break;
3375 }
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02003376 em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003377 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003378 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003379 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003380 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003381 break;
3382 }
3383
3384 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003385 em_end = extent_map_end(em);
3386 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003387 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003388 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003389 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003390 sector = (em->block_start + extent_offset) >> 9;
3391 bdev = em->bdev;
3392 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003393 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003394 free_extent_map(em);
3395 em = NULL;
3396
Chris Masonc8b97812008-10-29 14:49:59 -04003397 /*
3398 * compressed and inline extents are written through other
3399 * paths in the FS
3400 */
3401 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003402 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003403 /*
3404 * end_io notification does not happen here for
3405 * compressed extents
3406 */
3407 if (!compressed && tree->ops &&
3408 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003409 tree->ops->writepage_end_io_hook(page, cur,
3410 cur + iosize - 1,
3411 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003412 else if (compressed) {
3413 /* we don't want to end_page_writeback on
3414 * a compressed extent. this happens
3415 * elsewhere
3416 */
3417 nr++;
3418 }
3419
3420 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003421 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003422 continue;
3423 }
Chris Masonc8b97812008-10-29 14:49:59 -04003424
David Sterba58409ed2016-05-04 11:46:10 +02003425 set_range_writeback(tree, cur, cur + iosize - 1);
3426 if (!PageWriteback(page)) {
3427 btrfs_err(BTRFS_I(inode)->root->fs_info,
3428 "page %lu not writeback, cur %llu end %llu",
3429 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003430 }
David Sterba58409ed2016-05-04 11:46:10 +02003431
David Sterba4b81ba42017-06-06 19:14:26 +02003432 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
Mike Christie1f7ad752016-06-05 14:31:51 -05003433 page, sector, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003434 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003435 end_bio_extent_writepage,
3436 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003437 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003438 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003439 if (PageWriteback(page))
3440 end_page_writeback(page);
3441 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003442
Chris Masond1310b22008-01-24 16:13:08 -05003443 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003444 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003445 nr++;
3446 }
3447done:
Chris Mason40f76582014-05-21 13:35:51 -07003448 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003449 return ret;
3450}
3451
3452/*
3453 * the writepage semantics are similar to regular writepage. extent
3454 * records are inserted to lock ranges in the tree, and as dirty areas
3455 * are found, they are marked writeback. Then the lock bits are removed
3456 * and the end_io handler clears the writeback ranges
3457 */
3458static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3459 void *data)
3460{
3461 struct inode *inode = page->mapping->host;
3462 struct extent_page_data *epd = data;
3463 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003464 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003465 int ret;
3466 int nr = 0;
3467 size_t pg_offset = 0;
3468 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003469 unsigned long end_index = i_size >> PAGE_SHIFT;
David Sterbaf1c77c52017-06-06 19:03:49 +02003470 unsigned int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003471 unsigned long nr_written = 0;
3472
Liu Boff40adf2017-08-24 18:19:48 -06003473 write_flags = wbc_to_write_flags(wbc);
Chris Mason40f76582014-05-21 13:35:51 -07003474
3475 trace___extent_writepage(page, inode, wbc);
3476
3477 WARN_ON(!PageLocked(page));
3478
3479 ClearPageError(page);
3480
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003481 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003482 if (page->index > end_index ||
3483 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003484 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003485 unlock_page(page);
3486 return 0;
3487 }
3488
3489 if (page->index == end_index) {
3490 char *userpage;
3491
3492 userpage = kmap_atomic(page);
3493 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003494 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003495 kunmap_atomic(userpage);
3496 flush_dcache_page(page);
3497 }
3498
3499 pg_offset = 0;
3500
3501 set_page_extent_mapped(page);
3502
3503 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3504 if (ret == 1)
3505 goto done_unlocked;
3506 if (ret)
3507 goto done;
3508
3509 ret = __extent_writepage_io(inode, page, wbc, epd,
3510 i_size, nr_written, write_flags, &nr);
3511 if (ret == 1)
3512 goto done_unlocked;
3513
3514done:
Chris Masond1310b22008-01-24 16:13:08 -05003515 if (nr == 0) {
3516 /* make sure the mapping tag for page dirty gets cleared */
3517 set_page_writeback(page);
3518 end_page_writeback(page);
3519 }
Filipe Manana61391d52014-05-09 17:17:40 +01003520 if (PageError(page)) {
3521 ret = ret < 0 ? ret : -EIO;
3522 end_extent_writepage(page, ret, start, page_end);
3523 }
Chris Masond1310b22008-01-24 16:13:08 -05003524 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003525 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003526
Chris Mason11c83492009-04-20 15:50:09 -04003527done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003528 return 0;
3529}
3530
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003531void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003532{
NeilBrown74316202014-07-07 15:16:04 +10003533 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3534 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003535}
3536
Chris Mason0e378df2014-05-19 20:55:27 -07003537static noinline_for_stack int
3538lock_extent_buffer_for_io(struct extent_buffer *eb,
3539 struct btrfs_fs_info *fs_info,
3540 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003541{
3542 unsigned long i, num_pages;
3543 int flush = 0;
3544 int ret = 0;
3545
3546 if (!btrfs_try_tree_write_lock(eb)) {
3547 flush = 1;
3548 flush_write_bio(epd);
3549 btrfs_tree_lock(eb);
3550 }
3551
3552 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3553 btrfs_tree_unlock(eb);
3554 if (!epd->sync_io)
3555 return 0;
3556 if (!flush) {
3557 flush_write_bio(epd);
3558 flush = 1;
3559 }
Chris Masona098d8e82012-03-21 12:09:56 -04003560 while (1) {
3561 wait_on_extent_buffer_writeback(eb);
3562 btrfs_tree_lock(eb);
3563 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3564 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003565 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003566 }
3567 }
3568
Josef Bacik51561ff2012-07-20 16:25:24 -04003569 /*
3570 * We need to do this to prevent races in people who check if the eb is
3571 * under IO since we can end up having no IO bits set for a short period
3572 * of time.
3573 */
3574 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003575 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3576 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003577 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003578 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Nikolay Borisov104b4e52017-06-20 21:01:20 +03003579 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
3580 -eb->len,
3581 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003582 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003583 } else {
3584 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003585 }
3586
3587 btrfs_tree_unlock(eb);
3588
3589 if (!ret)
3590 return ret;
3591
3592 num_pages = num_extent_pages(eb->start, eb->len);
3593 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003594 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003595
3596 if (!trylock_page(p)) {
3597 if (!flush) {
3598 flush_write_bio(epd);
3599 flush = 1;
3600 }
3601 lock_page(p);
3602 }
3603 }
3604
3605 return ret;
3606}
3607
3608static void end_extent_buffer_writeback(struct extent_buffer *eb)
3609{
3610 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003611 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003612 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3613}
3614
Filipe Manana656f30d2014-09-26 12:25:56 +01003615static void set_btree_ioerr(struct page *page)
3616{
3617 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003618
3619 SetPageError(page);
3620 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3621 return;
3622
3623 /*
3624 * If writeback for a btree extent that doesn't belong to a log tree
3625 * failed, increment the counter transaction->eb_write_errors.
3626 * We do this because while the transaction is running and before it's
3627 * committing (when we call filemap_fdata[write|wait]_range against
3628 * the btree inode), we might have
3629 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3630 * returns an error or an error happens during writeback, when we're
3631 * committing the transaction we wouldn't know about it, since the pages
3632 * can be no longer dirty nor marked anymore for writeback (if a
3633 * subsequent modification to the extent buffer didn't happen before the
3634 * transaction commit), which makes filemap_fdata[write|wait]_range not
3635 * able to find the pages tagged with SetPageError at transaction
3636 * commit time. So if this happens we must abort the transaction,
3637 * otherwise we commit a super block with btree roots that point to
3638 * btree nodes/leafs whose content on disk is invalid - either garbage
3639 * or the content of some node/leaf from a past generation that got
3640 * cowed or deleted and is no longer valid.
3641 *
3642 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3643 * not be enough - we need to distinguish between log tree extents vs
3644 * non-log tree extents, and the next filemap_fdatawait_range() call
3645 * will catch and clear such errors in the mapping - and that call might
3646 * be from a log sync and not from a transaction commit. Also, checking
3647 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3648 * not done and would not be reliable - the eb might have been released
3649 * from memory and reading it back again means that flag would not be
3650 * set (since it's a runtime flag, not persisted on disk).
3651 *
3652 * Using the flags below in the btree inode also makes us achieve the
3653 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3654 * writeback for all dirty pages and before filemap_fdatawait_range()
3655 * is called, the writeback for all dirty pages had already finished
3656 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3657 * filemap_fdatawait_range() would return success, as it could not know
3658 * that writeback errors happened (the pages were no longer tagged for
3659 * writeback).
3660 */
3661 switch (eb->log_index) {
3662 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003663 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003664 break;
3665 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003666 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003667 break;
3668 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003669 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003670 break;
3671 default:
3672 BUG(); /* unexpected, logic error */
3673 }
3674}
3675
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003676static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003677{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003678 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003680 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003681
David Sterbac09abff2017-07-13 18:10:07 +02003682 ASSERT(!bio_flagged(bio, BIO_CLONED));
Kent Overstreet2c30c712013-11-07 12:20:26 -08003683 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003684 struct page *page = bvec->bv_page;
3685
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003686 eb = (struct extent_buffer *)page->private;
3687 BUG_ON(!eb);
3688 done = atomic_dec_and_test(&eb->io_pages);
3689
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003690 if (bio->bi_status ||
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003691 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003692 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003693 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003694 }
3695
3696 end_page_writeback(page);
3697
3698 if (!done)
3699 continue;
3700
3701 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003702 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003703
3704 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003705}
3706
Chris Mason0e378df2014-05-19 20:55:27 -07003707static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003708 struct btrfs_fs_info *fs_info,
3709 struct writeback_control *wbc,
3710 struct extent_page_data *epd)
3711{
3712 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003713 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003714 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003715 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003716 unsigned long i, num_pages;
Liu Bo851cd172016-09-23 13:44:44 -07003717 unsigned long start, end;
Liu Boff40adf2017-08-24 18:19:48 -06003718 unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003719 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003720
Filipe Manana656f30d2014-09-26 12:25:56 +01003721 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003722 num_pages = num_extent_pages(eb->start, eb->len);
3723 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003724
Liu Bo851cd172016-09-23 13:44:44 -07003725 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3726 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003727 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003728 end = btrfs_node_key_ptr_offset(nritems);
3729
David Sterbab159fa22016-11-08 18:09:03 +01003730 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003731 } else {
3732 /*
3733 * leaf:
3734 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3735 */
3736 start = btrfs_item_nr_offset(nritems);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003737 end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003738 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003739 }
3740
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003741 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003742 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003743
3744 clear_page_dirty_for_io(p);
3745 set_page_writeback(p);
David Sterba4b81ba42017-06-06 19:14:26 +02003746 ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc,
Mike Christie1f7ad752016-06-05 14:31:51 -05003747 p, offset >> 9, PAGE_SIZE, 0, bdev,
David Sterbac2df8bb2017-02-10 19:29:38 +01003748 &epd->bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003749 end_bio_extent_buffer_writepage,
Liu Bo18fdc672017-09-13 12:18:22 -06003750 0, 0, 0, false);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003751 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003752 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003753 if (PageWriteback(p))
3754 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003755 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3756 end_extent_buffer_writeback(eb);
3757 ret = -EIO;
3758 break;
3759 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003760 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003761 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003762 unlock_page(p);
3763 }
3764
3765 if (unlikely(ret)) {
3766 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003767 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003768 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003769 unlock_page(p);
3770 }
3771 }
3772
3773 return ret;
3774}
3775
3776int btree_write_cache_pages(struct address_space *mapping,
3777 struct writeback_control *wbc)
3778{
3779 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3780 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3781 struct extent_buffer *eb, *prev_eb = NULL;
3782 struct extent_page_data epd = {
3783 .bio = NULL,
3784 .tree = tree,
3785 .extent_locked = 0,
3786 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3787 };
3788 int ret = 0;
3789 int done = 0;
3790 int nr_to_write_done = 0;
3791 struct pagevec pvec;
3792 int nr_pages;
3793 pgoff_t index;
3794 pgoff_t end; /* Inclusive */
3795 int scanned = 0;
3796 int tag;
3797
3798 pagevec_init(&pvec, 0);
3799 if (wbc->range_cyclic) {
3800 index = mapping->writeback_index; /* Start from prev offset */
3801 end = -1;
3802 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003803 index = wbc->range_start >> PAGE_SHIFT;
3804 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003805 scanned = 1;
3806 }
3807 if (wbc->sync_mode == WB_SYNC_ALL)
3808 tag = PAGECACHE_TAG_TOWRITE;
3809 else
3810 tag = PAGECACHE_TAG_DIRTY;
3811retry:
3812 if (wbc->sync_mode == WB_SYNC_ALL)
3813 tag_pages_for_writeback(mapping, index, end);
3814 while (!done && !nr_to_write_done && (index <= end) &&
3815 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3816 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3817 unsigned i;
3818
3819 scanned = 1;
3820 for (i = 0; i < nr_pages; i++) {
3821 struct page *page = pvec.pages[i];
3822
3823 if (!PagePrivate(page))
3824 continue;
3825
3826 if (!wbc->range_cyclic && page->index > end) {
3827 done = 1;
3828 break;
3829 }
3830
Josef Bacikb5bae262012-09-14 13:43:01 -04003831 spin_lock(&mapping->private_lock);
3832 if (!PagePrivate(page)) {
3833 spin_unlock(&mapping->private_lock);
3834 continue;
3835 }
3836
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003837 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003838
3839 /*
3840 * Shouldn't happen and normally this would be a BUG_ON
3841 * but no sense in crashing the users box for something
3842 * we can survive anyway.
3843 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303844 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003845 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003846 continue;
3847 }
3848
Josef Bacikb5bae262012-09-14 13:43:01 -04003849 if (eb == prev_eb) {
3850 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003851 continue;
3852 }
3853
Josef Bacikb5bae262012-09-14 13:43:01 -04003854 ret = atomic_inc_not_zero(&eb->refs);
3855 spin_unlock(&mapping->private_lock);
3856 if (!ret)
3857 continue;
3858
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003859 prev_eb = eb;
3860 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3861 if (!ret) {
3862 free_extent_buffer(eb);
3863 continue;
3864 }
3865
3866 ret = write_one_eb(eb, fs_info, wbc, &epd);
3867 if (ret) {
3868 done = 1;
3869 free_extent_buffer(eb);
3870 break;
3871 }
3872 free_extent_buffer(eb);
3873
3874 /*
3875 * the filesystem may choose to bump up nr_to_write.
3876 * We have to make sure to honor the new nr_to_write
3877 * at any time
3878 */
3879 nr_to_write_done = wbc->nr_to_write <= 0;
3880 }
3881 pagevec_release(&pvec);
3882 cond_resched();
3883 }
3884 if (!scanned && !done) {
3885 /*
3886 * We hit the last page and there is more work to be done: wrap
3887 * back to the start of the file
3888 */
3889 scanned = 1;
3890 index = 0;
3891 goto retry;
3892 }
3893 flush_write_bio(&epd);
3894 return ret;
3895}
3896
Chris Masond1310b22008-01-24 16:13:08 -05003897/**
Chris Mason4bef0842008-09-08 11:18:08 -04003898 * 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 -05003899 * @mapping: address space structure to write
3900 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3901 * @writepage: function called for each page
3902 * @data: data passed to writepage function
3903 *
3904 * If a page is already under I/O, write_cache_pages() skips it, even
3905 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3906 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3907 * and msync() need to guarantee that all the data which was dirty at the time
3908 * the call was made get new I/O started against them. If wbc->sync_mode is
3909 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3910 * existing IO to complete.
3911 */
David Sterba4242b642017-02-10 19:38:24 +01003912static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003913 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003914 writepage_t writepage, void *data,
3915 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003916{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003917 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003918 int ret = 0;
3919 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003920 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003921 struct pagevec pvec;
3922 int nr_pages;
3923 pgoff_t index;
3924 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003925 pgoff_t done_index;
3926 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003927 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003928 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003929
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003930 /*
3931 * We have to hold onto the inode so that ordered extents can do their
3932 * work when the IO finishes. The alternative to this is failing to add
3933 * an ordered extent if the igrab() fails there and that is a huge pain
3934 * to deal with, so instead just hold onto the inode throughout the
3935 * writepages operation. If it fails here we are freeing up the inode
3936 * anyway and we'd rather not waste our time writing out stuff that is
3937 * going to be truncated anyway.
3938 */
3939 if (!igrab(inode))
3940 return 0;
3941
Chris Masond1310b22008-01-24 16:13:08 -05003942 pagevec_init(&pvec, 0);
3943 if (wbc->range_cyclic) {
3944 index = mapping->writeback_index; /* Start from prev offset */
3945 end = -1;
3946 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003947 index = wbc->range_start >> PAGE_SHIFT;
3948 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003949 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3950 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003951 scanned = 1;
3952 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003953 if (wbc->sync_mode == WB_SYNC_ALL)
3954 tag = PAGECACHE_TAG_TOWRITE;
3955 else
3956 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003957retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003958 if (wbc->sync_mode == WB_SYNC_ALL)
3959 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003960 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003961 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003962 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3963 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003964 unsigned i;
3965
3966 scanned = 1;
3967 for (i = 0; i < nr_pages; i++) {
3968 struct page *page = pvec.pages[i];
3969
Liu Boa91326672016-03-07 16:56:21 -08003970 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003971 /*
3972 * At this point we hold neither mapping->tree_lock nor
3973 * lock on the page itself: the page may be truncated or
3974 * invalidated (changing page->mapping to NULL), or even
3975 * swizzled back from swapper_space to tmpfs file
3976 * mapping
3977 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003978 if (!trylock_page(page)) {
3979 flush_fn(data);
3980 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003981 }
Chris Masond1310b22008-01-24 16:13:08 -05003982
3983 if (unlikely(page->mapping != mapping)) {
3984 unlock_page(page);
3985 continue;
3986 }
3987
3988 if (!wbc->range_cyclic && page->index > end) {
3989 done = 1;
3990 unlock_page(page);
3991 continue;
3992 }
3993
Chris Masond2c3f4f2008-11-19 12:44:22 -05003994 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003995 if (PageWriteback(page))
3996 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003997 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003998 }
Chris Masond1310b22008-01-24 16:13:08 -05003999
4000 if (PageWriteback(page) ||
4001 !clear_page_dirty_for_io(page)) {
4002 unlock_page(page);
4003 continue;
4004 }
4005
4006 ret = (*writepage)(page, wbc, data);
4007
4008 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4009 unlock_page(page);
4010 ret = 0;
4011 }
Liu Boa91326672016-03-07 16:56:21 -08004012 if (ret < 0) {
4013 /*
4014 * done_index is set past this page,
4015 * so media errors will not choke
4016 * background writeout for the entire
4017 * file. This has consequences for
4018 * range_cyclic semantics (ie. it may
4019 * not be suitable for data integrity
4020 * writeout).
4021 */
4022 done_index = page->index + 1;
4023 done = 1;
4024 break;
4025 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004026
4027 /*
4028 * the filesystem may choose to bump up nr_to_write.
4029 * We have to make sure to honor the new nr_to_write
4030 * at any time
4031 */
4032 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004033 }
4034 pagevec_release(&pvec);
4035 cond_resched();
4036 }
Liu Bo894b36e2016-03-07 16:56:22 -08004037 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004038 /*
4039 * We hit the last page and there is more work to be done: wrap
4040 * back to the start of the file
4041 */
4042 scanned = 1;
4043 index = 0;
4044 goto retry;
4045 }
Liu Boa91326672016-03-07 16:56:21 -08004046
4047 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4048 mapping->writeback_index = done_index;
4049
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004050 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004051 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004052}
Chris Masond1310b22008-01-24 16:13:08 -05004053
Chris Masonffbd5172009-04-20 15:50:09 -04004054static void flush_epd_write_bio(struct extent_page_data *epd)
4055{
4056 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004057 int ret;
4058
Liu Bo18fdc672017-09-13 12:18:22 -06004059 ret = submit_one_bio(epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004060 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004061 epd->bio = NULL;
4062 }
4063}
4064
Chris Masond2c3f4f2008-11-19 12:44:22 -05004065static noinline void flush_write_bio(void *data)
4066{
4067 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004068 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004069}
4070
Chris Masond1310b22008-01-24 16:13:08 -05004071int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4072 get_extent_t *get_extent,
4073 struct writeback_control *wbc)
4074{
4075 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004076 struct extent_page_data epd = {
4077 .bio = NULL,
4078 .tree = tree,
4079 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004080 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004081 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004082 };
Chris Masond1310b22008-01-24 16:13:08 -05004083
Chris Masond1310b22008-01-24 16:13:08 -05004084 ret = __extent_writepage(page, wbc, &epd);
4085
Chris Masonffbd5172009-04-20 15:50:09 -04004086 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004087 return ret;
4088}
Chris Masond1310b22008-01-24 16:13:08 -05004089
Chris Mason771ed682008-11-06 22:02:51 -05004090int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4091 u64 start, u64 end, get_extent_t *get_extent,
4092 int mode)
4093{
4094 int ret = 0;
4095 struct address_space *mapping = inode->i_mapping;
4096 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004097 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4098 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004099
4100 struct extent_page_data epd = {
4101 .bio = NULL,
4102 .tree = tree,
4103 .get_extent = get_extent,
4104 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004105 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05004106 };
4107 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004108 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004109 .nr_to_write = nr_pages * 2,
4110 .range_start = start,
4111 .range_end = end + 1,
4112 };
4113
Chris Masond3977122009-01-05 21:25:51 -05004114 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004115 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004116 if (clear_page_dirty_for_io(page))
4117 ret = __extent_writepage(page, &wbc_writepages, &epd);
4118 else {
4119 if (tree->ops && tree->ops->writepage_end_io_hook)
4120 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004121 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004122 NULL, 1);
4123 unlock_page(page);
4124 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004125 put_page(page);
4126 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004127 }
4128
Chris Masonffbd5172009-04-20 15:50:09 -04004129 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004130 return ret;
4131}
Chris Masond1310b22008-01-24 16:13:08 -05004132
4133int extent_writepages(struct extent_io_tree *tree,
4134 struct address_space *mapping,
4135 get_extent_t *get_extent,
4136 struct writeback_control *wbc)
4137{
4138 int ret = 0;
4139 struct extent_page_data epd = {
4140 .bio = NULL,
4141 .tree = tree,
4142 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004143 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004144 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05004145 };
4146
David Sterba4242b642017-02-10 19:38:24 +01004147 ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004148 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004149 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004150 return ret;
4151}
Chris Masond1310b22008-01-24 16:13:08 -05004152
4153int extent_readpages(struct extent_io_tree *tree,
4154 struct address_space *mapping,
4155 struct list_head *pages, unsigned nr_pages,
4156 get_extent_t get_extent)
4157{
4158 struct bio *bio = NULL;
4159 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004160 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004161 struct page *pagepool[16];
4162 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004163 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004164 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004165 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004166
Chris Masond1310b22008-01-24 16:13:08 -05004167 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004168 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004169
4170 prefetchw(&page->flags);
4171 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004172 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004173 page->index,
4174 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004175 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004176 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004177 }
Liu Bo67c96842012-07-20 21:43:09 -06004178
4179 pagepool[nr++] = page;
4180 if (nr < ARRAY_SIZE(pagepool))
4181 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004182 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004183 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004184 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004185 }
Miao Xie99740902013-07-25 19:22:36 +08004186 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004187 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004188 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004189
Miao Xie125bac012013-07-25 19:22:37 +08004190 if (em_cached)
4191 free_extent_map(em_cached);
4192
Chris Masond1310b22008-01-24 16:13:08 -05004193 BUG_ON(!list_empty(pages));
4194 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004195 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004196 return 0;
4197}
Chris Masond1310b22008-01-24 16:13:08 -05004198
4199/*
4200 * basic invalidatepage code, this waits on any locked or writeback
4201 * ranges corresponding to the page, and then deletes any extent state
4202 * records from the tree
4203 */
4204int extent_invalidatepage(struct extent_io_tree *tree,
4205 struct page *page, unsigned long offset)
4206{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004207 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004208 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004209 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004210 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4211
Qu Wenruofda28322013-02-26 08:10:22 +00004212 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004213 if (start > end)
4214 return 0;
4215
David Sterbaff13db42015-12-03 14:30:40 +01004216 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004217 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004218 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004219 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4220 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004221 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004222 return 0;
4223}
Chris Masond1310b22008-01-24 16:13:08 -05004224
4225/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004226 * a helper for releasepage, this tests for areas of the page that
4227 * are locked or under IO and drops the related state bits if it is safe
4228 * to drop the page.
4229 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004230static int try_release_extent_state(struct extent_map_tree *map,
4231 struct extent_io_tree *tree,
4232 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004233{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004234 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004235 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004236 int ret = 1;
4237
Chris Mason211f90e2008-07-18 11:56:15 -04004238 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004239 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004240 ret = 0;
4241 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004242 /*
4243 * at this point we can safely clear everything except the
4244 * locked bit and the nodatasum bit
4245 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004246 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004247 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4248 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004249
4250 /* if clear_extent_bit failed for enomem reasons,
4251 * we can't allow the release to continue.
4252 */
4253 if (ret < 0)
4254 ret = 0;
4255 else
4256 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004257 }
4258 return ret;
4259}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004260
4261/*
Chris Masond1310b22008-01-24 16:13:08 -05004262 * a helper for releasepage. As long as there are no locked extents
4263 * in the range corresponding to the page, both state records and extent
4264 * map records are removed
4265 */
4266int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004267 struct extent_io_tree *tree, struct page *page,
4268 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004269{
4270 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004271 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004272 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004273
Mel Gormand0164ad2015-11-06 16:28:21 -08004274 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004275 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004276 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004277 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004278 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004279 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004280 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004281 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004282 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004283 break;
4284 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004285 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4286 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004287 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004288 free_extent_map(em);
4289 break;
4290 }
4291 if (!test_range_bit(tree, em->start,
4292 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004293 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004294 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004295 remove_extent_mapping(map, em);
4296 /* once for the rb tree */
4297 free_extent_map(em);
4298 }
4299 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004300 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004301
4302 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004303 free_extent_map(em);
4304 }
Chris Masond1310b22008-01-24 16:13:08 -05004305 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004306 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004307}
Chris Masond1310b22008-01-24 16:13:08 -05004308
Chris Masonec29ed52011-02-23 16:23:20 -05004309/*
4310 * helper function for fiemap, which doesn't want to see any holes.
4311 * This maps until we find something past 'last'
4312 */
4313static struct extent_map *get_extent_skip_holes(struct inode *inode,
4314 u64 offset,
4315 u64 last,
4316 get_extent_t *get_extent)
4317{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004318 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004319 struct extent_map *em;
4320 u64 len;
4321
4322 if (offset >= last)
4323 return NULL;
4324
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304325 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004326 len = last - offset;
4327 if (len == 0)
4328 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004329 len = ALIGN(len, sectorsize);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004330 em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004331 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004332 return em;
4333
4334 /* if this isn't a hole return it */
4335 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4336 em->block_start != EXTENT_MAP_HOLE) {
4337 return em;
4338 }
4339
4340 /* this is a hole, advance to the next extent */
4341 offset = extent_map_end(em);
4342 free_extent_map(em);
4343 if (offset >= last)
4344 break;
4345 }
4346 return NULL;
4347}
4348
Qu Wenruo47518322017-04-07 10:43:15 +08004349/*
4350 * To cache previous fiemap extent
4351 *
4352 * Will be used for merging fiemap extent
4353 */
4354struct fiemap_cache {
4355 u64 offset;
4356 u64 phys;
4357 u64 len;
4358 u32 flags;
4359 bool cached;
4360};
4361
4362/*
4363 * Helper to submit fiemap extent.
4364 *
4365 * Will try to merge current fiemap extent specified by @offset, @phys,
4366 * @len and @flags with cached one.
4367 * And only when we fails to merge, cached one will be submitted as
4368 * fiemap extent.
4369 *
4370 * Return value is the same as fiemap_fill_next_extent().
4371 */
4372static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
4373 struct fiemap_cache *cache,
4374 u64 offset, u64 phys, u64 len, u32 flags)
4375{
4376 int ret = 0;
4377
4378 if (!cache->cached)
4379 goto assign;
4380
4381 /*
4382 * Sanity check, extent_fiemap() should have ensured that new
4383 * fiemap extent won't overlap with cahced one.
4384 * Not recoverable.
4385 *
4386 * NOTE: Physical address can overlap, due to compression
4387 */
4388 if (cache->offset + cache->len > offset) {
4389 WARN_ON(1);
4390 return -EINVAL;
4391 }
4392
4393 /*
4394 * Only merges fiemap extents if
4395 * 1) Their logical addresses are continuous
4396 *
4397 * 2) Their physical addresses are continuous
4398 * So truly compressed (physical size smaller than logical size)
4399 * extents won't get merged with each other
4400 *
4401 * 3) Share same flags except FIEMAP_EXTENT_LAST
4402 * So regular extent won't get merged with prealloc extent
4403 */
4404 if (cache->offset + cache->len == offset &&
4405 cache->phys + cache->len == phys &&
4406 (cache->flags & ~FIEMAP_EXTENT_LAST) ==
4407 (flags & ~FIEMAP_EXTENT_LAST)) {
4408 cache->len += len;
4409 cache->flags |= flags;
4410 goto try_submit_last;
4411 }
4412
4413 /* Not mergeable, need to submit cached one */
4414 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4415 cache->len, cache->flags);
4416 cache->cached = false;
4417 if (ret)
4418 return ret;
4419assign:
4420 cache->cached = true;
4421 cache->offset = offset;
4422 cache->phys = phys;
4423 cache->len = len;
4424 cache->flags = flags;
4425try_submit_last:
4426 if (cache->flags & FIEMAP_EXTENT_LAST) {
4427 ret = fiemap_fill_next_extent(fieinfo, cache->offset,
4428 cache->phys, cache->len, cache->flags);
4429 cache->cached = false;
4430 }
4431 return ret;
4432}
4433
4434/*
Qu Wenruo848c23b2017-06-22 10:01:21 +08004435 * Emit last fiemap cache
Qu Wenruo47518322017-04-07 10:43:15 +08004436 *
Qu Wenruo848c23b2017-06-22 10:01:21 +08004437 * The last fiemap cache may still be cached in the following case:
4438 * 0 4k 8k
4439 * |<- Fiemap range ->|
4440 * |<------------ First extent ----------->|
4441 *
4442 * In this case, the first extent range will be cached but not emitted.
4443 * So we must emit it before ending extent_fiemap().
Qu Wenruo47518322017-04-07 10:43:15 +08004444 */
Qu Wenruo848c23b2017-06-22 10:01:21 +08004445static int emit_last_fiemap_cache(struct btrfs_fs_info *fs_info,
4446 struct fiemap_extent_info *fieinfo,
4447 struct fiemap_cache *cache)
Qu Wenruo47518322017-04-07 10:43:15 +08004448{
4449 int ret;
4450
4451 if (!cache->cached)
4452 return 0;
4453
Qu Wenruo47518322017-04-07 10:43:15 +08004454 ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
4455 cache->len, cache->flags);
4456 cache->cached = false;
4457 if (ret > 0)
4458 ret = 0;
4459 return ret;
4460}
4461
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004462int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4463 __u64 start, __u64 len, get_extent_t *get_extent)
4464{
Josef Bacik975f84f2010-11-23 19:36:57 +00004465 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004466 u64 off = start;
4467 u64 max = start + len;
4468 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004469 u32 found_type;
4470 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004471 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004472 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004473 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004474 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004475 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004476 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004477 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004478 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo47518322017-04-07 10:43:15 +08004479 struct fiemap_cache cache = { 0 };
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004480 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004481 u64 em_start = 0;
4482 u64 em_len = 0;
4483 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004484
4485 if (len == 0)
4486 return -EINVAL;
4487
Josef Bacik975f84f2010-11-23 19:36:57 +00004488 path = btrfs_alloc_path();
4489 if (!path)
4490 return -ENOMEM;
4491 path->leave_spinning = 1;
4492
Jeff Mahoneyda170662016-06-15 09:22:56 -04004493 start = round_down(start, btrfs_inode_sectorsize(inode));
4494 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004495
Chris Masonec29ed52011-02-23 16:23:20 -05004496 /*
4497 * lookup the last file extent. We're not using i_size here
4498 * because there might be preallocation past i_size
4499 */
David Sterbaf85b7372017-01-20 14:54:07 +01004500 ret = btrfs_lookup_file_extent(NULL, root, path,
4501 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004502 if (ret < 0) {
4503 btrfs_free_path(path);
4504 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004505 } else {
4506 WARN_ON(!ret);
4507 if (ret == 1)
4508 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004509 }
Liu Bo2d324f52016-05-17 17:21:48 -07004510
Josef Bacik975f84f2010-11-23 19:36:57 +00004511 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004512 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004513 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004514
Chris Masonec29ed52011-02-23 16:23:20 -05004515 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004516 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004517 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004518 /* have to trust i_size as the end */
4519 last = (u64)-1;
4520 last_for_get_extent = isize;
4521 } else {
4522 /*
4523 * remember the start of the last extent. There are a
4524 * bunch of different factors that go into the length of the
4525 * extent, so its much less complex to remember where it started
4526 */
4527 last = found_key.offset;
4528 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004529 }
Liu Bofe09e162013-09-22 12:54:23 +08004530 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004531
Chris Masonec29ed52011-02-23 16:23:20 -05004532 /*
4533 * we might have some extents allocated but more delalloc past those
4534 * extents. so, we trust isize unless the start of the last extent is
4535 * beyond isize
4536 */
4537 if (last < isize) {
4538 last = (u64)-1;
4539 last_for_get_extent = isize;
4540 }
4541
David Sterbaff13db42015-12-03 14:30:40 +01004542 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004543 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004544
Josef Bacik4d479cf2011-11-17 11:34:31 -05004545 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004546 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004547 if (!em)
4548 goto out;
4549 if (IS_ERR(em)) {
4550 ret = PTR_ERR(em);
4551 goto out;
4552 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004553
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004554 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004555 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004556
Chris Masonea8efc72011-03-08 11:54:40 -05004557 /* break if the extent we found is outside the range */
4558 if (em->start >= max || extent_map_end(em) < off)
4559 break;
4560
4561 /*
4562 * get_extent may return an extent that starts before our
4563 * requested range. We have to make sure the ranges
4564 * we return to fiemap always move forward and don't
4565 * overlap, so adjust the offsets here
4566 */
4567 em_start = max(em->start, off);
4568
4569 /*
4570 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004571 * for adjusting the disk offset below. Only do this if the
4572 * extent isn't compressed since our in ram offset may be past
4573 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004574 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004575 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4576 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004577 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004578 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004579 disko = 0;
4580 flags = 0;
4581
Chris Masonea8efc72011-03-08 11:54:40 -05004582 /*
4583 * bump off for our next call to get_extent
4584 */
4585 off = extent_map_end(em);
4586 if (off >= max)
4587 end = 1;
4588
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004589 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004590 end = 1;
4591 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004592 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004593 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4594 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004595 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004596 flags |= (FIEMAP_EXTENT_DELALLOC |
4597 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004598 } else if (fieinfo->fi_extents_max) {
4599 u64 bytenr = em->block_start -
4600 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004601
Chris Masonea8efc72011-03-08 11:54:40 -05004602 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004603
4604 /*
4605 * As btrfs supports shared space, this information
4606 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004607 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4608 * then we're just getting a count and we can skip the
4609 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004610 */
Edmund Nadolskibb739cf2017-06-28 21:56:58 -06004611 ret = btrfs_check_shared(root,
4612 btrfs_ino(BTRFS_I(inode)),
4613 bytenr);
Josef Bacikdc046b12014-09-10 16:20:45 -04004614 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004615 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004616 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004617 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004618 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004619 }
4620 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4621 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004622 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4623 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004624
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004625 free_extent_map(em);
4626 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004627 if ((em_start >= last) || em_len == (u64)-1 ||
4628 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004629 flags |= FIEMAP_EXTENT_LAST;
4630 end = 1;
4631 }
4632
Chris Masonec29ed52011-02-23 16:23:20 -05004633 /* now scan forward to see if this is really the last extent. */
4634 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4635 get_extent);
4636 if (IS_ERR(em)) {
4637 ret = PTR_ERR(em);
4638 goto out;
4639 }
4640 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004641 flags |= FIEMAP_EXTENT_LAST;
4642 end = 1;
4643 }
Qu Wenruo47518322017-04-07 10:43:15 +08004644 ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
4645 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004646 if (ret) {
4647 if (ret == 1)
4648 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004649 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004650 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004651 }
4652out_free:
Qu Wenruo47518322017-04-07 10:43:15 +08004653 if (!ret)
Qu Wenruo848c23b2017-06-22 10:01:21 +08004654 ret = emit_last_fiemap_cache(root->fs_info, fieinfo, &cache);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004655 free_extent_map(em);
4656out:
Liu Bofe09e162013-09-22 12:54:23 +08004657 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004658 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004659 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004660 return ret;
4661}
4662
Chris Mason727011e2010-08-06 13:21:20 -04004663static void __free_extent_buffer(struct extent_buffer *eb)
4664{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004665 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004666 kmem_cache_free(extent_buffer_cache, eb);
4667}
4668
Josef Bacika26e8c92014-03-28 17:07:27 -04004669int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004670{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004671 return (atomic_read(&eb->io_pages) ||
4672 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4673 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004674}
4675
Miao Xie897ca6e92010-10-26 20:57:29 -04004676/*
4677 * Helper for releasing extent buffer page.
4678 */
David Sterbaa50924e2014-07-31 00:51:36 +02004679static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004680{
4681 unsigned long index;
4682 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004683 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004684
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004685 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004686
David Sterbaa50924e2014-07-31 00:51:36 +02004687 index = num_extent_pages(eb->start, eb->len);
4688 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004689 return;
4690
4691 do {
4692 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004693 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004694 if (!page)
4695 continue;
4696 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004697 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004698 /*
4699 * We do this since we'll remove the pages after we've
4700 * removed the eb from the radix tree, so we could race
4701 * and have this page now attached to the new eb. So
4702 * only clear page_private if it's still connected to
4703 * this eb.
4704 */
4705 if (PagePrivate(page) &&
4706 page->private == (unsigned long)eb) {
4707 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4708 BUG_ON(PageDirty(page));
4709 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004710 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004711 * We need to make sure we haven't be attached
4712 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004713 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004714 ClearPagePrivate(page);
4715 set_page_private(page, 0);
4716 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004717 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004718 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004719
4720 if (mapped)
4721 spin_unlock(&page->mapping->private_lock);
4722
Nicholas D Steeves01327612016-05-19 21:18:45 -04004723 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004724 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004725 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004726}
4727
4728/*
4729 * Helper for releasing the extent buffer.
4730 */
4731static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4732{
David Sterbaa50924e2014-07-31 00:51:36 +02004733 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004734 __free_extent_buffer(eb);
4735}
4736
Josef Bacikf28491e2013-12-16 13:24:27 -05004737static struct extent_buffer *
4738__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004739 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004740{
4741 struct extent_buffer *eb = NULL;
4742
Michal Hockod1b5c562015-08-19 14:17:40 +02004743 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004744 eb->start = start;
4745 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004746 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004747 eb->bflags = 0;
4748 rwlock_init(&eb->lock);
4749 atomic_set(&eb->write_locks, 0);
4750 atomic_set(&eb->read_locks, 0);
4751 atomic_set(&eb->blocking_readers, 0);
4752 atomic_set(&eb->blocking_writers, 0);
4753 atomic_set(&eb->spinning_readers, 0);
4754 atomic_set(&eb->spinning_writers, 0);
4755 eb->lock_nested = 0;
4756 init_waitqueue_head(&eb->write_lock_wq);
4757 init_waitqueue_head(&eb->read_lock_wq);
4758
4759 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4760
4761 spin_lock_init(&eb->refs_lock);
4762 atomic_set(&eb->refs, 1);
4763 atomic_set(&eb->io_pages, 0);
4764
4765 /*
4766 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4767 */
4768 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4769 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4770 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4771
4772 return eb;
4773}
4774
4775struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4776{
4777 unsigned long i;
4778 struct page *p;
4779 struct extent_buffer *new;
4780 unsigned long num_pages = num_extent_pages(src->start, src->len);
4781
David Sterba3f556f72014-06-15 03:20:26 +02004782 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004783 if (new == NULL)
4784 return NULL;
4785
4786 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004787 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004788 if (!p) {
4789 btrfs_release_extent_buffer(new);
4790 return NULL;
4791 }
4792 attach_extent_buffer_page(new, p);
4793 WARN_ON(PageDirty(p));
4794 SetPageUptodate(p);
4795 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004796 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004797 }
4798
Josef Bacikdb7f3432013-08-07 14:54:37 -04004799 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4800 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4801
4802 return new;
4803}
4804
Omar Sandoval0f331222015-09-29 20:50:31 -07004805struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4806 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004807{
4808 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004809 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004810 unsigned long i;
4811
Omar Sandoval0f331222015-09-29 20:50:31 -07004812 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004813
4814 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004815 if (!eb)
4816 return NULL;
4817
4818 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004819 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004820 if (!eb->pages[i])
4821 goto err;
4822 }
4823 set_extent_buffer_uptodate(eb);
4824 btrfs_set_header_nritems(eb, 0);
4825 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4826
4827 return eb;
4828err:
4829 for (; i > 0; i--)
4830 __free_page(eb->pages[i - 1]);
4831 __free_extent_buffer(eb);
4832 return NULL;
4833}
4834
Omar Sandoval0f331222015-09-29 20:50:31 -07004835struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004836 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004837{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004838 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004839}
4840
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004841static void check_buffer_tree_ref(struct extent_buffer *eb)
4842{
Chris Mason242e18c2013-01-29 17:49:37 -05004843 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004844 /* the ref bit is tricky. We have to make sure it is set
4845 * if we have the buffer dirty. Otherwise the
4846 * code to free a buffer can end up dropping a dirty
4847 * page
4848 *
4849 * Once the ref bit is set, it won't go away while the
4850 * buffer is dirty or in writeback, and it also won't
4851 * go away while we have the reference count on the
4852 * eb bumped.
4853 *
4854 * We can't just set the ref bit without bumping the
4855 * ref on the eb because free_extent_buffer might
4856 * see the ref bit and try to clear it. If this happens
4857 * free_extent_buffer might end up dropping our original
4858 * ref by mistake and freeing the page before we are able
4859 * to add one more ref.
4860 *
4861 * So bump the ref count first, then set the bit. If someone
4862 * beat us to it, drop the ref we added.
4863 */
Chris Mason242e18c2013-01-29 17:49:37 -05004864 refs = atomic_read(&eb->refs);
4865 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4866 return;
4867
Josef Bacik594831c2012-07-20 16:11:08 -04004868 spin_lock(&eb->refs_lock);
4869 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004870 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004871 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004872}
4873
Mel Gorman2457aec2014-06-04 16:10:31 -07004874static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4875 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004876{
4877 unsigned long num_pages, i;
4878
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004879 check_buffer_tree_ref(eb);
4880
Josef Bacik5df42352012-03-15 18:24:42 -04004881 num_pages = num_extent_pages(eb->start, eb->len);
4882 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004883 struct page *p = eb->pages[i];
4884
Mel Gorman2457aec2014-06-04 16:10:31 -07004885 if (p != accessed)
4886 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004887 }
4888}
4889
Josef Bacikf28491e2013-12-16 13:24:27 -05004890struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4891 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004892{
4893 struct extent_buffer *eb;
4894
4895 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004896 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004897 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004898 if (eb && atomic_inc_not_zero(&eb->refs)) {
4899 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004900 /*
4901 * Lock our eb's refs_lock to avoid races with
4902 * free_extent_buffer. When we get our eb it might be flagged
4903 * with EXTENT_BUFFER_STALE and another task running
4904 * free_extent_buffer might have seen that flag set,
4905 * eb->refs == 2, that the buffer isn't under IO (dirty and
4906 * writeback flags not set) and it's still in the tree (flag
4907 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4908 * of decrementing the extent buffer's reference count twice.
4909 * So here we could race and increment the eb's reference count,
4910 * clear its stale flag, mark it as dirty and drop our reference
4911 * before the other task finishes executing free_extent_buffer,
4912 * which would later result in an attempt to free an extent
4913 * buffer that is dirty.
4914 */
4915 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4916 spin_lock(&eb->refs_lock);
4917 spin_unlock(&eb->refs_lock);
4918 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004919 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004920 return eb;
4921 }
4922 rcu_read_unlock();
4923
4924 return NULL;
4925}
4926
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004927#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4928struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004929 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004930{
4931 struct extent_buffer *eb, *exists = NULL;
4932 int ret;
4933
4934 eb = find_extent_buffer(fs_info, start);
4935 if (eb)
4936 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004937 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004938 if (!eb)
4939 return NULL;
4940 eb->fs_info = fs_info;
4941again:
David Sterbae1860a72016-05-09 14:11:38 +02004942 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004943 if (ret)
4944 goto free_eb;
4945 spin_lock(&fs_info->buffer_lock);
4946 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004947 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004948 spin_unlock(&fs_info->buffer_lock);
4949 radix_tree_preload_end();
4950 if (ret == -EEXIST) {
4951 exists = find_extent_buffer(fs_info, start);
4952 if (exists)
4953 goto free_eb;
4954 else
4955 goto again;
4956 }
4957 check_buffer_tree_ref(eb);
4958 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4959
4960 /*
4961 * We will free dummy extent buffer's if they come into
4962 * free_extent_buffer with a ref count of 2, but if we are using this we
4963 * want the buffers to stay in memory until we're done with them, so
4964 * bump the ref count again.
4965 */
4966 atomic_inc(&eb->refs);
4967 return eb;
4968free_eb:
4969 btrfs_release_extent_buffer(eb);
4970 return exists;
4971}
4972#endif
4973
Josef Bacikf28491e2013-12-16 13:24:27 -05004974struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004975 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004976{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004977 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004978 unsigned long num_pages = num_extent_pages(start, len);
4979 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004980 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004981 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004982 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004983 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004984 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004985 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004986 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004987
Jeff Mahoneyda170662016-06-15 09:22:56 -04004988 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004989 btrfs_err(fs_info, "bad tree block start %llu", start);
4990 return ERR_PTR(-EINVAL);
4991 }
4992
Josef Bacikf28491e2013-12-16 13:24:27 -05004993 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004994 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004995 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004996
David Sterba23d79d82014-06-15 02:55:29 +02004997 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004998 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004999 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05005000
Chris Mason727011e2010-08-06 13:21:20 -04005001 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02005002 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07005003 if (!p) {
5004 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04005005 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005006 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005007
5008 spin_lock(&mapping->private_lock);
5009 if (PagePrivate(p)) {
5010 /*
5011 * We could have already allocated an eb for this page
5012 * and attached one so lets see if we can get a ref on
5013 * the existing eb, and if we can we know it's good and
5014 * we can just return that one, else we know we can just
5015 * overwrite page->private.
5016 */
5017 exists = (struct extent_buffer *)p->private;
5018 if (atomic_inc_not_zero(&exists->refs)) {
5019 spin_unlock(&mapping->private_lock);
5020 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005021 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07005022 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005023 goto free_eb;
5024 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005025 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005026
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005027 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005028 * Do this so attach doesn't complain and we need to
5029 * drop the ref the old guy had.
5030 */
5031 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005032 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005033 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05005034 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05005035 attach_extent_buffer_page(eb, p);
5036 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005037 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005038 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005039 if (!PageUptodate(p))
5040 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005041
5042 /*
5043 * see below about how we avoid a nasty race with release page
5044 * and why we unlock later
5045 */
Chris Masond1310b22008-01-24 16:13:08 -05005046 }
5047 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005048 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005049again:
David Sterbae1860a72016-05-09 14:11:38 +02005050 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07005051 if (ret) {
5052 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04005053 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07005054 }
Miao Xie19fe0a82010-10-26 20:57:29 -04005055
Josef Bacikf28491e2013-12-16 13:24:27 -05005056 spin_lock(&fs_info->buffer_lock);
5057 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005058 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05005059 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005060 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005061 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005062 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005063 if (exists)
5064 goto free_eb;
5065 else
Josef Bacik115391d2012-03-09 09:51:43 -05005066 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005067 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005068 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005069 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005070 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005071
5072 /*
5073 * there is a race where release page may have
5074 * tried to find this extent buffer in the radix
5075 * but failed. It will tell the VM it is safe to
5076 * reclaim the, and it will clear the page private bit.
5077 * We must make sure to set the page private bit properly
5078 * after the extent buffer is in the radix tree so
5079 * it doesn't get lost
5080 */
Chris Mason727011e2010-08-06 13:21:20 -04005081 SetPageChecked(eb->pages[0]);
5082 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005083 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005084 ClearPageChecked(p);
5085 unlock_page(p);
5086 }
5087 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005088 return eb;
5089
Chris Mason6af118ce2008-07-22 11:18:07 -04005090free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005091 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005092 for (i = 0; i < num_pages; i++) {
5093 if (eb->pages[i])
5094 unlock_page(eb->pages[i]);
5095 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005096
Miao Xie897ca6e92010-10-26 20:57:29 -04005097 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005098 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005099}
Chris Masond1310b22008-01-24 16:13:08 -05005100
Josef Bacik3083ee22012-03-09 16:01:49 -05005101static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5102{
5103 struct extent_buffer *eb =
5104 container_of(head, struct extent_buffer, rcu_head);
5105
5106 __free_extent_buffer(eb);
5107}
5108
Josef Bacik3083ee22012-03-09 16:01:49 -05005109/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005110static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005111{
5112 WARN_ON(atomic_read(&eb->refs) == 0);
5113 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005114 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005115 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005116
Jan Schmidt815a51c2012-05-16 17:00:02 +02005117 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005118
Josef Bacikf28491e2013-12-16 13:24:27 -05005119 spin_lock(&fs_info->buffer_lock);
5120 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005121 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005122 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005123 } else {
5124 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005125 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005126
5127 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005128 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005129#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5130 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5131 __free_extent_buffer(eb);
5132 return 1;
5133 }
5134#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005135 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005136 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005137 }
5138 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005139
5140 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005141}
5142
Chris Masond1310b22008-01-24 16:13:08 -05005143void free_extent_buffer(struct extent_buffer *eb)
5144{
Chris Mason242e18c2013-01-29 17:49:37 -05005145 int refs;
5146 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005147 if (!eb)
5148 return;
5149
Chris Mason242e18c2013-01-29 17:49:37 -05005150 while (1) {
5151 refs = atomic_read(&eb->refs);
5152 if (refs <= 3)
5153 break;
5154 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5155 if (old == refs)
5156 return;
5157 }
5158
Josef Bacik3083ee22012-03-09 16:01:49 -05005159 spin_lock(&eb->refs_lock);
5160 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005161 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5162 atomic_dec(&eb->refs);
5163
5164 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005165 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005166 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005167 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5168 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005169
Josef Bacik3083ee22012-03-09 16:01:49 -05005170 /*
5171 * I know this is terrible, but it's temporary until we stop tracking
5172 * the uptodate bits and such for the extent buffers.
5173 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005174 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005175}
Chris Masond1310b22008-01-24 16:13:08 -05005176
Josef Bacik3083ee22012-03-09 16:01:49 -05005177void free_extent_buffer_stale(struct extent_buffer *eb)
5178{
5179 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005180 return;
5181
Josef Bacik3083ee22012-03-09 16:01:49 -05005182 spin_lock(&eb->refs_lock);
5183 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5184
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005185 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005186 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5187 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005188 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005189}
5190
Chris Mason1d4284b2012-03-28 20:31:37 -04005191void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005192{
Chris Masond1310b22008-01-24 16:13:08 -05005193 unsigned long i;
5194 unsigned long num_pages;
5195 struct page *page;
5196
Chris Masond1310b22008-01-24 16:13:08 -05005197 num_pages = num_extent_pages(eb->start, eb->len);
5198
5199 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005200 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005201 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005202 continue;
5203
Chris Masona61e6f22008-07-22 11:18:08 -04005204 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005205 WARN_ON(!PagePrivate(page));
5206
Chris Masond1310b22008-01-24 16:13:08 -05005207 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005208 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005209 if (!PageDirty(page)) {
5210 radix_tree_tag_clear(&page->mapping->page_tree,
5211 page_index(page),
5212 PAGECACHE_TAG_DIRTY);
5213 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005214 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005215 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005216 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005217 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005218 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005219}
Chris Masond1310b22008-01-24 16:13:08 -05005220
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005221int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005222{
5223 unsigned long i;
5224 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005225 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005226
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005227 check_buffer_tree_ref(eb);
5228
Chris Masonb9473432009-03-13 11:00:37 -04005229 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005230
Chris Masond1310b22008-01-24 16:13:08 -05005231 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005232 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005233 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5234
Chris Masonb9473432009-03-13 11:00:37 -04005235 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005236 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005237 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005238}
Chris Masond1310b22008-01-24 16:13:08 -05005239
David Sterba69ba3922015-12-03 13:08:59 +01005240void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005241{
5242 unsigned long i;
5243 struct page *page;
5244 unsigned long num_pages;
5245
Chris Masonb4ce94d2009-02-04 09:25:08 -05005246 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005247 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005248 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005249 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005250 if (page)
5251 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005252 }
Chris Mason1259ab72008-05-12 13:39:03 -04005253}
5254
David Sterba09c25a82015-12-03 13:08:59 +01005255void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005256{
5257 unsigned long i;
5258 struct page *page;
5259 unsigned long num_pages;
5260
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005261 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005262 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005263 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005264 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005265 SetPageUptodate(page);
5266 }
Chris Masond1310b22008-01-24 16:13:08 -05005267}
Chris Masond1310b22008-01-24 16:13:08 -05005268
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005269int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005270{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005271 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005272}
Chris Masond1310b22008-01-24 16:13:08 -05005273
5274int read_extent_buffer_pages(struct extent_io_tree *tree,
Josef Bacik8436ea912016-09-02 15:40:03 -04005275 struct extent_buffer *eb, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005276 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005277{
5278 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005279 struct page *page;
5280 int err;
5281 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005282 int locked_pages = 0;
5283 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005284 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005285 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005286 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005287 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005288
Chris Masonb4ce94d2009-02-04 09:25:08 -05005289 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005290 return 0;
5291
Chris Masond1310b22008-01-24 16:13:08 -05005292 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005293 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005294 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005295 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005296 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005297 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005298 } else {
5299 lock_page(page);
5300 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005301 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005302 }
5303 /*
5304 * We need to firstly lock all pages to make sure that
5305 * the uptodate bit of our pages won't be affected by
5306 * clear_extent_buffer_uptodate().
5307 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005308 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005309 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005310 if (!PageUptodate(page)) {
5311 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005312 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005313 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005314 }
Liu Bo2571e732016-08-03 12:33:01 -07005315
Chris Masonce9adaa2008-04-09 16:28:12 -04005316 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005317 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005318 goto unlock_exit;
5319 }
5320
Filipe Manana656f30d2014-09-26 12:25:56 +01005321 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005322 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005323 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005324 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005325 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005326
Chris Masonce9adaa2008-04-09 16:28:12 -04005327 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005328 if (ret) {
5329 atomic_dec(&eb->io_pages);
5330 unlock_page(page);
5331 continue;
5332 }
5333
Chris Masonf1885912008-04-09 16:28:12 -04005334 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005335 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005336 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005337 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005338 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005339 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005340 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005341 /*
5342 * We use &bio in above __extent_read_full_page,
5343 * so we ensure that if it returns error, the
5344 * current page fails to add itself to bio and
5345 * it's been unlocked.
5346 *
5347 * We must dec io_pages by ourselves.
5348 */
5349 atomic_dec(&eb->io_pages);
5350 }
Chris Masond1310b22008-01-24 16:13:08 -05005351 } else {
5352 unlock_page(page);
5353 }
5354 }
5355
Jeff Mahoney355808c2011-10-03 23:23:14 -04005356 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005357 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005358 if (err)
5359 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005360 }
Chris Masona86c12c2008-02-07 10:50:54 -05005361
Arne Jansenbb82ab82011-06-10 14:06:53 +02005362 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005363 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005364
Josef Bacik8436ea912016-09-02 15:40:03 -04005365 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005366 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005367 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005368 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005369 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005370 }
Chris Masond3977122009-01-05 21:25:51 -05005371
Chris Masond1310b22008-01-24 16:13:08 -05005372 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005373
5374unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005375 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005376 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005377 page = eb->pages[locked_pages];
5378 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005379 }
5380 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005381}
Chris Masond1310b22008-01-24 16:13:08 -05005382
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005383void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5384 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005385{
5386 size_t cur;
5387 size_t offset;
5388 struct page *page;
5389 char *kaddr;
5390 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005391 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5392 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005393
Liu Bof716abd2017-08-09 11:10:16 -06005394 if (start + len > eb->len) {
5395 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5396 eb->start, eb->len, start, len);
5397 memset(dst, 0, len);
5398 return;
5399 }
Chris Masond1310b22008-01-24 16:13:08 -05005400
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005401 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005402
Chris Masond3977122009-01-05 21:25:51 -05005403 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005404 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005405
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005406 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005407 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005408 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005409
5410 dst += cur;
5411 len -= cur;
5412 offset = 0;
5413 i++;
5414 }
5415}
Chris Masond1310b22008-01-24 16:13:08 -05005416
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005417int read_extent_buffer_to_user(const struct extent_buffer *eb,
5418 void __user *dstv,
5419 unsigned long start, unsigned long len)
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005420{
5421 size_t cur;
5422 size_t offset;
5423 struct page *page;
5424 char *kaddr;
5425 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005426 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5427 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005428 int ret = 0;
5429
5430 WARN_ON(start > eb->len);
5431 WARN_ON(start + len > eb->start + eb->len);
5432
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005433 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005434
5435 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005436 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005437
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005438 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005439 kaddr = page_address(page);
5440 if (copy_to_user(dst, kaddr + offset, cur)) {
5441 ret = -EFAULT;
5442 break;
5443 }
5444
5445 dst += cur;
5446 len -= cur;
5447 offset = 0;
5448 i++;
5449 }
5450
5451 return ret;
5452}
5453
Liu Bo415b35a2016-06-17 19:16:21 -07005454/*
5455 * return 0 if the item is found within a page.
5456 * return 1 if the item spans two pages.
5457 * return -EINVAL otherwise.
5458 */
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005459int map_private_extent_buffer(const struct extent_buffer *eb,
5460 unsigned long start, unsigned long min_len,
5461 char **map, unsigned long *map_start,
5462 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005463{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005464 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005465 char *kaddr;
5466 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005467 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5468 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005469 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005470 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005471
Liu Bof716abd2017-08-09 11:10:16 -06005472 if (start + min_len > eb->len) {
5473 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5474 eb->start, eb->len, start, min_len);
5475 return -EINVAL;
5476 }
5477
Chris Masond1310b22008-01-24 16:13:08 -05005478 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005479 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005480
5481 if (i == 0) {
5482 offset = start_offset;
5483 *map_start = 0;
5484 } else {
5485 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005486 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005487 }
Chris Masond3977122009-01-05 21:25:51 -05005488
David Sterbafb85fc92014-07-31 01:03:53 +02005489 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005490 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005491 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005492 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005493 return 0;
5494}
Chris Masond1310b22008-01-24 16:13:08 -05005495
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -06005496int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
5497 unsigned long start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005498{
5499 size_t cur;
5500 size_t offset;
5501 struct page *page;
5502 char *kaddr;
5503 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005504 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5505 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005506 int ret = 0;
5507
5508 WARN_ON(start > eb->len);
5509 WARN_ON(start + len > eb->start + eb->len);
5510
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005511 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005512
Chris Masond3977122009-01-05 21:25:51 -05005513 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005514 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005515
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005516 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005517
Chris Masona6591712011-07-19 12:04:14 -04005518 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005519 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005520 if (ret)
5521 break;
5522
5523 ptr += cur;
5524 len -= cur;
5525 offset = 0;
5526 i++;
5527 }
5528 return ret;
5529}
Chris Masond1310b22008-01-24 16:13:08 -05005530
David Sterbaf157bf72016-11-09 17:43:38 +01005531void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5532 const void *srcv)
5533{
5534 char *kaddr;
5535
5536 WARN_ON(!PageUptodate(eb->pages[0]));
5537 kaddr = page_address(eb->pages[0]);
5538 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5539 BTRFS_FSID_SIZE);
5540}
5541
5542void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5543{
5544 char *kaddr;
5545
5546 WARN_ON(!PageUptodate(eb->pages[0]));
5547 kaddr = page_address(eb->pages[0]);
5548 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5549 BTRFS_FSID_SIZE);
5550}
5551
Chris Masond1310b22008-01-24 16:13:08 -05005552void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5553 unsigned long start, unsigned long len)
5554{
5555 size_t cur;
5556 size_t offset;
5557 struct page *page;
5558 char *kaddr;
5559 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005560 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5561 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005562
5563 WARN_ON(start > eb->len);
5564 WARN_ON(start + len > eb->start + eb->len);
5565
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005566 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005567
Chris Masond3977122009-01-05 21:25:51 -05005568 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005569 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005570 WARN_ON(!PageUptodate(page));
5571
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005572 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005573 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005574 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005575
5576 src += cur;
5577 len -= cur;
5578 offset = 0;
5579 i++;
5580 }
5581}
Chris Masond1310b22008-01-24 16:13:08 -05005582
David Sterbab159fa22016-11-08 18:09:03 +01005583void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5584 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005585{
5586 size_t cur;
5587 size_t offset;
5588 struct page *page;
5589 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005590 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5591 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005592
5593 WARN_ON(start > eb->len);
5594 WARN_ON(start + len > eb->start + eb->len);
5595
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005596 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005597
Chris Masond3977122009-01-05 21:25:51 -05005598 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005599 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005600 WARN_ON(!PageUptodate(page));
5601
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005602 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005603 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005604 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005605
5606 len -= cur;
5607 offset = 0;
5608 i++;
5609 }
5610}
Chris Masond1310b22008-01-24 16:13:08 -05005611
David Sterba58e80122016-11-08 18:30:31 +01005612void copy_extent_buffer_full(struct extent_buffer *dst,
5613 struct extent_buffer *src)
5614{
5615 int i;
5616 unsigned num_pages;
5617
5618 ASSERT(dst->len == src->len);
5619
5620 num_pages = num_extent_pages(dst->start, dst->len);
5621 for (i = 0; i < num_pages; i++)
5622 copy_page(page_address(dst->pages[i]),
5623 page_address(src->pages[i]));
5624}
5625
Chris Masond1310b22008-01-24 16:13:08 -05005626void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5627 unsigned long dst_offset, unsigned long src_offset,
5628 unsigned long len)
5629{
5630 u64 dst_len = dst->len;
5631 size_t cur;
5632 size_t offset;
5633 struct page *page;
5634 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005635 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5636 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005637
5638 WARN_ON(src->len != dst_len);
5639
5640 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005641 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005642
Chris Masond3977122009-01-05 21:25:51 -05005643 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005644 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005645 WARN_ON(!PageUptodate(page));
5646
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005647 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005648
Chris Masona6591712011-07-19 12:04:14 -04005649 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005650 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005651
5652 src_offset += cur;
5653 len -= cur;
5654 offset = 0;
5655 i++;
5656 }
5657}
Chris Masond1310b22008-01-24 16:13:08 -05005658
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005659void le_bitmap_set(u8 *map, unsigned int start, int len)
5660{
5661 u8 *p = map + BIT_BYTE(start);
5662 const unsigned int size = start + len;
5663 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5664 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5665
5666 while (len - bits_to_set >= 0) {
5667 *p |= mask_to_set;
5668 len -= bits_to_set;
5669 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005670 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005671 p++;
5672 }
5673 if (len) {
5674 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5675 *p |= mask_to_set;
5676 }
5677}
5678
5679void le_bitmap_clear(u8 *map, unsigned int start, int len)
5680{
5681 u8 *p = map + BIT_BYTE(start);
5682 const unsigned int size = start + len;
5683 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5684 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5685
5686 while (len - bits_to_clear >= 0) {
5687 *p &= ~mask_to_clear;
5688 len -= bits_to_clear;
5689 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005690 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005691 p++;
5692 }
5693 if (len) {
5694 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5695 *p &= ~mask_to_clear;
5696 }
5697}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005698
5699/*
5700 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5701 * given bit number
5702 * @eb: the extent buffer
5703 * @start: offset of the bitmap item in the extent buffer
5704 * @nr: bit number
5705 * @page_index: return index of the page in the extent buffer that contains the
5706 * given bit number
5707 * @page_offset: return offset into the page given by page_index
5708 *
5709 * This helper hides the ugliness of finding the byte in an extent buffer which
5710 * contains a given bit.
5711 */
5712static inline void eb_bitmap_offset(struct extent_buffer *eb,
5713 unsigned long start, unsigned long nr,
5714 unsigned long *page_index,
5715 size_t *page_offset)
5716{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005717 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005718 size_t byte_offset = BIT_BYTE(nr);
5719 size_t offset;
5720
5721 /*
5722 * The byte we want is the offset of the extent buffer + the offset of
5723 * the bitmap item in the extent buffer + the offset of the byte in the
5724 * bitmap item.
5725 */
5726 offset = start_offset + start + byte_offset;
5727
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005728 *page_index = offset >> PAGE_SHIFT;
5729 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005730}
5731
5732/**
5733 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5734 * @eb: the extent buffer
5735 * @start: offset of the bitmap item in the extent buffer
5736 * @nr: bit number to test
5737 */
5738int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5739 unsigned long nr)
5740{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005741 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005742 struct page *page;
5743 unsigned long i;
5744 size_t offset;
5745
5746 eb_bitmap_offset(eb, start, nr, &i, &offset);
5747 page = eb->pages[i];
5748 WARN_ON(!PageUptodate(page));
5749 kaddr = page_address(page);
5750 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5751}
5752
5753/**
5754 * extent_buffer_bitmap_set - set an area of a bitmap
5755 * @eb: the extent buffer
5756 * @start: offset of the bitmap item in the extent buffer
5757 * @pos: bit number of the first bit
5758 * @len: number of bits to set
5759 */
5760void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5761 unsigned long pos, unsigned long len)
5762{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005763 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005764 struct page *page;
5765 unsigned long i;
5766 size_t offset;
5767 const unsigned int size = pos + len;
5768 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005769 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005770
5771 eb_bitmap_offset(eb, start, pos, &i, &offset);
5772 page = eb->pages[i];
5773 WARN_ON(!PageUptodate(page));
5774 kaddr = page_address(page);
5775
5776 while (len >= bits_to_set) {
5777 kaddr[offset] |= mask_to_set;
5778 len -= bits_to_set;
5779 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005780 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005781 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005782 offset = 0;
5783 page = eb->pages[++i];
5784 WARN_ON(!PageUptodate(page));
5785 kaddr = page_address(page);
5786 }
5787 }
5788 if (len) {
5789 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5790 kaddr[offset] |= mask_to_set;
5791 }
5792}
5793
5794
5795/**
5796 * extent_buffer_bitmap_clear - clear an area of a bitmap
5797 * @eb: the extent buffer
5798 * @start: offset of the bitmap item in the extent buffer
5799 * @pos: bit number of the first bit
5800 * @len: number of bits to clear
5801 */
5802void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5803 unsigned long pos, unsigned long len)
5804{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005805 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005806 struct page *page;
5807 unsigned long i;
5808 size_t offset;
5809 const unsigned int size = pos + len;
5810 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005811 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005812
5813 eb_bitmap_offset(eb, start, pos, &i, &offset);
5814 page = eb->pages[i];
5815 WARN_ON(!PageUptodate(page));
5816 kaddr = page_address(page);
5817
5818 while (len >= bits_to_clear) {
5819 kaddr[offset] &= ~mask_to_clear;
5820 len -= bits_to_clear;
5821 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005822 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005823 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005824 offset = 0;
5825 page = eb->pages[++i];
5826 WARN_ON(!PageUptodate(page));
5827 kaddr = page_address(page);
5828 }
5829 }
5830 if (len) {
5831 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5832 kaddr[offset] &= ~mask_to_clear;
5833 }
5834}
5835
Sergei Trofimovich33872062011-04-11 21:52:52 +00005836static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5837{
5838 unsigned long distance = (src > dst) ? src - dst : dst - src;
5839 return distance < len;
5840}
5841
Chris Masond1310b22008-01-24 16:13:08 -05005842static void copy_pages(struct page *dst_page, struct page *src_page,
5843 unsigned long dst_off, unsigned long src_off,
5844 unsigned long len)
5845{
Chris Masona6591712011-07-19 12:04:14 -04005846 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005847 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005848 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005849
Sergei Trofimovich33872062011-04-11 21:52:52 +00005850 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005851 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005852 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005853 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005854 if (areas_overlap(src_off, dst_off, len))
5855 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005856 }
Chris Masond1310b22008-01-24 16:13:08 -05005857
Chris Mason727011e2010-08-06 13:21:20 -04005858 if (must_memmove)
5859 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5860 else
5861 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005862}
5863
5864void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5865 unsigned long src_offset, unsigned long len)
5866{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005867 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005868 size_t cur;
5869 size_t dst_off_in_page;
5870 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005871 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005872 unsigned long dst_i;
5873 unsigned long src_i;
5874
5875 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005876 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005877 "memmove bogus src_offset %lu move len %lu dst len %lu",
5878 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005879 BUG_ON(1);
5880 }
5881 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005882 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005883 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5884 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005885 BUG_ON(1);
5886 }
5887
Chris Masond3977122009-01-05 21:25:51 -05005888 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005889 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005890 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005891 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005892 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005893
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005894 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5895 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005896
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005897 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005898 src_off_in_page));
5899 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005900 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005901
David Sterbafb85fc92014-07-31 01:03:53 +02005902 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005903 dst_off_in_page, src_off_in_page, cur);
5904
5905 src_offset += cur;
5906 dst_offset += cur;
5907 len -= cur;
5908 }
5909}
Chris Masond1310b22008-01-24 16:13:08 -05005910
5911void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5912 unsigned long src_offset, unsigned long len)
5913{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005914 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005915 size_t cur;
5916 size_t dst_off_in_page;
5917 size_t src_off_in_page;
5918 unsigned long dst_end = dst_offset + len - 1;
5919 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005920 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005921 unsigned long dst_i;
5922 unsigned long src_i;
5923
5924 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005925 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005926 "memmove bogus src_offset %lu move len %lu len %lu",
5927 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005928 BUG_ON(1);
5929 }
5930 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005931 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005932 "memmove bogus dst_offset %lu move len %lu len %lu",
5933 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005934 BUG_ON(1);
5935 }
Chris Mason727011e2010-08-06 13:21:20 -04005936 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005937 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5938 return;
5939 }
Chris Masond3977122009-01-05 21:25:51 -05005940 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005941 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5942 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005943
5944 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005945 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005946 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005947 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005948
5949 cur = min_t(unsigned long, len, src_off_in_page + 1);
5950 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005951 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005952 dst_off_in_page - cur + 1,
5953 src_off_in_page - cur + 1, cur);
5954
5955 dst_end -= cur;
5956 src_end -= cur;
5957 len -= cur;
5958 }
5959}
Chris Mason6af118ce2008-07-22 11:18:07 -04005960
David Sterbaf7a52a42013-04-26 14:56:29 +00005961int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005962{
Chris Mason6af118ce2008-07-22 11:18:07 -04005963 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005964
Miao Xie19fe0a82010-10-26 20:57:29 -04005965 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005966 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005967 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005968 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005969 spin_lock(&page->mapping->private_lock);
5970 if (!PagePrivate(page)) {
5971 spin_unlock(&page->mapping->private_lock);
5972 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005973 }
5974
Josef Bacik3083ee22012-03-09 16:01:49 -05005975 eb = (struct extent_buffer *)page->private;
5976 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005977
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005978 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005979 * This is a little awful but should be ok, we need to make sure that
5980 * the eb doesn't disappear out from under us while we're looking at
5981 * this page.
5982 */
5983 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005984 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005985 spin_unlock(&eb->refs_lock);
5986 spin_unlock(&page->mapping->private_lock);
5987 return 0;
5988 }
5989 spin_unlock(&page->mapping->private_lock);
5990
Josef Bacik3083ee22012-03-09 16:01:49 -05005991 /*
5992 * If tree ref isn't set then we know the ref on this eb is a real ref,
5993 * so just return, this page will likely be freed soon anyway.
5994 */
5995 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5996 spin_unlock(&eb->refs_lock);
5997 return 0;
5998 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005999
David Sterbaf7a52a42013-04-26 14:56:29 +00006000 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04006001}