blob: 996306d322de300174181033bd13ed3d159e7f3e [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"
Lu Fengqiafce7722016-06-13 09:36:46 +080023#include "transaction.h"
Chris Masond1310b22008-01-24 16:13:08 -050024
Chris Masond1310b22008-01-24 16:13:08 -050025static struct kmem_cache *extent_state_cache;
26static struct kmem_cache *extent_buffer_cache;
Chris Mason9be33952013-05-17 18:30:14 -040027static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050028
Filipe Manana27a35072014-07-06 20:09:59 +010029static inline bool extent_state_in_tree(const struct extent_state *state)
30{
31 return !RB_EMPTY_NODE(&state->rb_node);
32}
33
Eric Sandeen6d49ba12013-04-22 16:12:31 +000034#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050035static LIST_HEAD(buffers);
36static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040037
Chris Masond3977122009-01-05 21:25:51 -050038static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000039
40static inline
41void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
42{
43 unsigned long flags;
44
45 spin_lock_irqsave(&leak_lock, flags);
46 list_add(new, head);
47 spin_unlock_irqrestore(&leak_lock, flags);
48}
49
50static inline
51void btrfs_leak_debug_del(struct list_head *entry)
52{
53 unsigned long flags;
54
55 spin_lock_irqsave(&leak_lock, flags);
56 list_del(entry);
57 spin_unlock_irqrestore(&leak_lock, flags);
58}
59
60static inline
61void btrfs_leak_debug_check(void)
62{
63 struct extent_state *state;
64 struct extent_buffer *eb;
65
66 while (!list_empty(&states)) {
67 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010068 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010069 state->start, state->end, state->state,
70 extent_state_in_tree(state),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020071 atomic_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000072 list_del(&state->leak_list);
73 kmem_cache_free(extent_state_cache, state);
74 }
75
76 while (!list_empty(&buffers)) {
77 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Jeff Mahoney62e85572016-09-20 10:05:01 -040078 pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020079 eb->start, eb->len, atomic_read(&eb->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000080 list_del(&eb->leak_list);
81 kmem_cache_free(extent_buffer_cache, eb);
82 }
83}
David Sterba8d599ae2013-04-30 15:22:23 +000084
Josef Bacika5dee372013-12-13 10:02:44 -050085#define btrfs_debug_check_extent_io_range(tree, start, end) \
86 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000087static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050088 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000089{
Josef Bacika5dee372013-12-13 10:02:44 -050090 struct inode *inode;
91 u64 isize;
David Sterba8d599ae2013-04-30 15:22:23 +000092
Josef Bacika5dee372013-12-13 10:02:44 -050093 if (!tree->mapping)
94 return;
95
96 inode = tree->mapping->host;
97 isize = i_size_read(inode);
David Sterba8d599ae2013-04-30 15:22:23 +000098 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
David Sterba94647322015-10-08 11:01:36 +020099 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
100 "%s: ino %llu isize %llu odd range [%llu,%llu]",
David Sterbaf85b7372017-01-20 14:54:07 +0100101 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000102 }
103}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000104#else
105#define btrfs_leak_debug_add(new, head) do {} while (0)
106#define btrfs_leak_debug_del(entry) do {} while (0)
107#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000108#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400109#endif
Chris Masond1310b22008-01-24 16:13:08 -0500110
Chris Masond1310b22008-01-24 16:13:08 -0500111#define BUFFER_LRU_MAX 64
112
113struct tree_entry {
114 u64 start;
115 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500116 struct rb_node rb_node;
117};
118
119struct extent_page_data {
120 struct bio *bio;
121 struct extent_io_tree *tree;
122 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -0400123 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500124
125 /* tells writepage not to lock the state bits for this range
126 * it still does the unlocking
127 */
Chris Masonffbd5172009-04-20 15:50:09 -0400128 unsigned int extent_locked:1;
129
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600130 /* tells the submit_bio code to use REQ_SYNC */
Chris Masonffbd5172009-04-20 15:50:09 -0400131 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500132};
133
Qu Wenruod38ed272015-10-12 14:53:37 +0800134static void add_extent_changeset(struct extent_state *state, unsigned bits,
135 struct extent_changeset *changeset,
136 int set)
137{
138 int ret;
139
140 if (!changeset)
141 return;
142 if (set && (state->state & bits) == bits)
143 return;
Qu Wenruofefdc552015-10-12 15:35:38 +0800144 if (!set && (state->state & bits) == 0)
145 return;
Qu Wenruod38ed272015-10-12 14:53:37 +0800146 changeset->bytes_changed += state->end - state->start + 1;
David Sterba53d32352017-02-13 13:42:29 +0100147 ret = ulist_add(&changeset->range_changed, state->start, state->end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800148 GFP_ATOMIC);
149 /* ENOMEM */
150 BUG_ON(ret < 0);
151}
152
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400153static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400154static inline struct btrfs_fs_info *
155tree_fs_info(struct extent_io_tree *tree)
156{
Josef Bacika5dee372013-12-13 10:02:44 -0500157 if (!tree->mapping)
158 return NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400159 return btrfs_sb(tree->mapping->host->i_sb);
160}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400161
Chris Masond1310b22008-01-24 16:13:08 -0500162int __init extent_io_init(void)
163{
David Sterba837e1972012-09-07 03:00:48 -0600164 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200165 sizeof(struct extent_state), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300166 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500167 if (!extent_state_cache)
168 return -ENOMEM;
169
David Sterba837e1972012-09-07 03:00:48 -0600170 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200171 sizeof(struct extent_buffer), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300172 SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500173 if (!extent_buffer_cache)
174 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400175
176 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
177 offsetof(struct btrfs_io_bio, bio));
178 if (!btrfs_bioset)
179 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700180
181 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
182 goto free_bioset;
183
Chris Masond1310b22008-01-24 16:13:08 -0500184 return 0;
185
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700186free_bioset:
187 bioset_free(btrfs_bioset);
188 btrfs_bioset = NULL;
189
Chris Mason9be33952013-05-17 18:30:14 -0400190free_buffer_cache:
191 kmem_cache_destroy(extent_buffer_cache);
192 extent_buffer_cache = NULL;
193
Chris Masond1310b22008-01-24 16:13:08 -0500194free_state_cache:
195 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400196 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500197 return -ENOMEM;
198}
199
200void extent_io_exit(void)
201{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000202 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000203
204 /*
205 * Make sure all delayed rcu free are flushed before we
206 * destroy caches.
207 */
208 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +0800209 kmem_cache_destroy(extent_state_cache);
210 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400211 if (btrfs_bioset)
212 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500213}
214
215void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200216 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500217{
Eric Paris6bef4d32010-02-23 19:43:04 +0000218 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500219 tree->ops = NULL;
220 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500221 spin_lock_init(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500222 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500223}
Chris Masond1310b22008-01-24 16:13:08 -0500224
Christoph Hellwigb2950862008-12-02 09:54:17 -0500225static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500226{
227 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500228
Michal Hocko3ba7ab22017-01-09 15:39:02 +0100229 /*
230 * The given mask might be not appropriate for the slab allocator,
231 * drop the unsupported bits
232 */
233 mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -0500234 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400235 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500236 return state;
237 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100238 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100239 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000240 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500241 atomic_set(&state->refs, 1);
242 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100243 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500244 return state;
245}
Chris Masond1310b22008-01-24 16:13:08 -0500246
Chris Mason4845e442010-05-25 20:56:50 -0400247void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500248{
Chris Masond1310b22008-01-24 16:13:08 -0500249 if (!state)
250 return;
251 if (atomic_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100252 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000253 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100254 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500255 kmem_cache_free(extent_state_cache, state);
256 }
257}
Chris Masond1310b22008-01-24 16:13:08 -0500258
Filipe Mananaf2071b22014-02-12 15:05:53 +0000259static struct rb_node *tree_insert(struct rb_root *root,
260 struct rb_node *search_start,
261 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000262 struct rb_node *node,
263 struct rb_node ***p_in,
264 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500265{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000266 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500267 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500268 struct tree_entry *entry;
269
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000270 if (p_in && parent_in) {
271 p = *p_in;
272 parent = *parent_in;
273 goto do_insert;
274 }
275
Filipe Mananaf2071b22014-02-12 15:05:53 +0000276 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500277 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500278 parent = *p;
279 entry = rb_entry(parent, struct tree_entry, rb_node);
280
281 if (offset < entry->start)
282 p = &(*p)->rb_left;
283 else if (offset > entry->end)
284 p = &(*p)->rb_right;
285 else
286 return parent;
287 }
288
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000289do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500290 rb_link_node(node, parent, p);
291 rb_insert_color(node, root);
292 return NULL;
293}
294
Chris Mason80ea96b2008-02-01 14:51:59 -0500295static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000296 struct rb_node **prev_ret,
297 struct rb_node **next_ret,
298 struct rb_node ***p_ret,
299 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500300{
Chris Mason80ea96b2008-02-01 14:51:59 -0500301 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000302 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500303 struct rb_node *prev = NULL;
304 struct rb_node *orig_prev = NULL;
305 struct tree_entry *entry;
306 struct tree_entry *prev_entry = NULL;
307
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000308 while (*n) {
309 prev = *n;
310 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500311 prev_entry = entry;
312
313 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000314 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500315 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000316 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500317 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000318 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500319 }
320
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000321 if (p_ret)
322 *p_ret = n;
323 if (parent_ret)
324 *parent_ret = prev;
325
Chris Masond1310b22008-01-24 16:13:08 -0500326 if (prev_ret) {
327 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500328 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500329 prev = rb_next(prev);
330 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
331 }
332 *prev_ret = prev;
333 prev = orig_prev;
334 }
335
336 if (next_ret) {
337 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500338 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500339 prev = rb_prev(prev);
340 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
341 }
342 *next_ret = prev;
343 }
344 return NULL;
345}
346
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000347static inline struct rb_node *
348tree_search_for_insert(struct extent_io_tree *tree,
349 u64 offset,
350 struct rb_node ***p_ret,
351 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500352{
Chris Mason70dec802008-01-29 09:59:12 -0500353 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500354 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500355
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000356 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500357 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500358 return prev;
359 return ret;
360}
361
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000362static inline struct rb_node *tree_search(struct extent_io_tree *tree,
363 u64 offset)
364{
365 return tree_search_for_insert(tree, offset, NULL, NULL);
366}
367
Josef Bacik9ed74f22009-09-11 16:12:44 -0400368static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
369 struct extent_state *other)
370{
371 if (tree->ops && tree->ops->merge_extent_hook)
372 tree->ops->merge_extent_hook(tree->mapping->host, new,
373 other);
374}
375
Chris Masond1310b22008-01-24 16:13:08 -0500376/*
377 * utility function to look for merge candidates inside a given range.
378 * Any extents with matching state are merged together into a single
379 * extent in the tree. Extents with EXTENT_IO in their state field
380 * are not merged because the end_io handlers need to be able to do
381 * operations on them without sleeping (or doing allocations/splits).
382 *
383 * This should be called with the tree lock held.
384 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000385static void merge_state(struct extent_io_tree *tree,
386 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500387{
388 struct extent_state *other;
389 struct rb_node *other_node;
390
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400391 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000392 return;
Chris Masond1310b22008-01-24 16:13:08 -0500393
394 other_node = rb_prev(&state->rb_node);
395 if (other_node) {
396 other = rb_entry(other_node, struct extent_state, rb_node);
397 if (other->end == state->start - 1 &&
398 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400399 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500400 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500401 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100402 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500403 free_extent_state(other);
404 }
405 }
406 other_node = rb_next(&state->rb_node);
407 if (other_node) {
408 other = rb_entry(other_node, struct extent_state, rb_node);
409 if (other->start == state->end + 1 &&
410 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400411 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400412 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400413 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100414 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400415 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500416 }
417 }
Chris Masond1310b22008-01-24 16:13:08 -0500418}
419
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000420static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100421 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500422{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000423 if (tree->ops && tree->ops->set_bit_hook)
424 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500425}
426
427static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100428 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500429{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400430 if (tree->ops && tree->ops->clear_bit_hook)
431 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500432}
433
Xiao Guangrong3150b692011-07-14 03:19:08 +0000434static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800435 struct extent_state *state, unsigned *bits,
436 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000437
Chris Masond1310b22008-01-24 16:13:08 -0500438/*
439 * insert an extent_state struct into the tree. 'bits' are set on the
440 * struct before it is inserted.
441 *
442 * This may return -EEXIST if the extent is already there, in which case the
443 * state struct is freed.
444 *
445 * The tree lock is not taken internally. This is a utility function and
446 * probably isn't what you want to call (see set/clear_extent_bit).
447 */
448static int insert_state(struct extent_io_tree *tree,
449 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000450 struct rb_node ***p,
451 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800452 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500453{
454 struct rb_node *node;
455
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000456 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500457 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200458 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500459 state->start = start;
460 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400461
Qu Wenruod38ed272015-10-12 14:53:37 +0800462 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000463
Filipe Mananaf2071b22014-02-12 15:05:53 +0000464 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500465 if (node) {
466 struct extent_state *found;
467 found = rb_entry(node, struct extent_state, rb_node);
Jeff Mahoney62e85572016-09-20 10:05:01 -0400468 pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200469 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500470 return -EEXIST;
471 }
472 merge_state(tree, state);
473 return 0;
474}
475
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000476static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400477 u64 split)
478{
479 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000480 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400481}
482
Chris Masond1310b22008-01-24 16:13:08 -0500483/*
484 * split a given extent state struct in two, inserting the preallocated
485 * struct 'prealloc' as the newly created second half. 'split' indicates an
486 * offset inside 'orig' where it should be split.
487 *
488 * Before calling,
489 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
490 * are two extent state structs in the tree:
491 * prealloc: [orig->start, split - 1]
492 * orig: [ split, orig->end ]
493 *
494 * The tree locks are not taken by this function. They need to be held
495 * by the caller.
496 */
497static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
498 struct extent_state *prealloc, u64 split)
499{
500 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400501
502 split_cb(tree, orig, split);
503
Chris Masond1310b22008-01-24 16:13:08 -0500504 prealloc->start = orig->start;
505 prealloc->end = split - 1;
506 prealloc->state = orig->state;
507 orig->start = split;
508
Filipe Mananaf2071b22014-02-12 15:05:53 +0000509 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
510 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500511 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500512 free_extent_state(prealloc);
513 return -EEXIST;
514 }
515 return 0;
516}
517
Li Zefancdc6a392012-03-12 16:39:48 +0800518static struct extent_state *next_state(struct extent_state *state)
519{
520 struct rb_node *next = rb_next(&state->rb_node);
521 if (next)
522 return rb_entry(next, struct extent_state, rb_node);
523 else
524 return NULL;
525}
526
Chris Masond1310b22008-01-24 16:13:08 -0500527/*
528 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800529 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500530 *
531 * If no bits are set on the state struct after clearing things, the
532 * struct is freed and removed from the tree
533 */
Li Zefancdc6a392012-03-12 16:39:48 +0800534static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
535 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800536 unsigned *bits, int wake,
537 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500538{
Li Zefancdc6a392012-03-12 16:39:48 +0800539 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100540 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500541
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400542 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500543 u64 range = state->end - state->start + 1;
544 WARN_ON(range > tree->dirty_bytes);
545 tree->dirty_bytes -= range;
546 }
Chris Mason291d6732008-01-29 15:55:23 -0500547 clear_state_cb(tree, state, bits);
Qu Wenruofefdc552015-10-12 15:35:38 +0800548 add_extent_changeset(state, bits_to_clear, changeset, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400549 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500550 if (wake)
551 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400552 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800553 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100554 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500555 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100556 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500557 free_extent_state(state);
558 } else {
559 WARN_ON(1);
560 }
561 } else {
562 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800563 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500564 }
Li Zefancdc6a392012-03-12 16:39:48 +0800565 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500566}
567
Xiao Guangrong82337672011-04-20 06:44:57 +0000568static struct extent_state *
569alloc_extent_state_atomic(struct extent_state *prealloc)
570{
571 if (!prealloc)
572 prealloc = alloc_extent_state(GFP_ATOMIC);
573
574 return prealloc;
575}
576
Eric Sandeen48a3b632013-04-25 20:41:01 +0000577static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400578{
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400579 btrfs_panic(tree_fs_info(tree), err,
580 "Locking error: Extent tree was modified by another thread while locked.");
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400581}
582
Chris Masond1310b22008-01-24 16:13:08 -0500583/*
584 * clear some bits on a range in the tree. This may require splitting
585 * or inserting elements in the tree, so the gfp mask is used to
586 * indicate which allocations or sleeping are allowed.
587 *
588 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
589 * the given range from the tree regardless of state (ie for truncate).
590 *
591 * the range [start, end] is inclusive.
592 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100593 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500594 */
Qu Wenruofefdc552015-10-12 15:35:38 +0800595static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
596 unsigned bits, int wake, int delete,
597 struct extent_state **cached_state,
598 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500599{
600 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400601 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500602 struct extent_state *prealloc = NULL;
603 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400604 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500605 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000606 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500607
Josef Bacika5dee372013-12-13 10:02:44 -0500608 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000609
Josef Bacik7ee9e442013-06-21 16:37:03 -0400610 if (bits & EXTENT_DELALLOC)
611 bits |= EXTENT_NORESERVE;
612
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400613 if (delete)
614 bits |= ~EXTENT_CTLBITS;
615 bits |= EXTENT_FIRST_DELALLOC;
616
Josef Bacik2ac55d42010-02-03 19:33:23 +0000617 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
618 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500619again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800620 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000621 /*
622 * Don't care for allocation failure here because we might end
623 * up not needing the pre-allocated extent state at all, which
624 * is the case if we only have in the tree extent states that
625 * cover our input range and don't cover too any other range.
626 * If we end up needing a new extent state we allocate it later.
627 */
Chris Masond1310b22008-01-24 16:13:08 -0500628 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500629 }
630
Chris Masoncad321a2008-12-17 14:51:42 -0500631 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400632 if (cached_state) {
633 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000634
635 if (clear) {
636 *cached_state = NULL;
637 cached_state = NULL;
638 }
639
Filipe Manana27a35072014-07-06 20:09:59 +0100640 if (cached && extent_state_in_tree(cached) &&
641 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000642 if (clear)
643 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400644 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400645 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400646 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000647 if (clear)
648 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400649 }
Chris Masond1310b22008-01-24 16:13:08 -0500650 /*
651 * this search will find the extents that end after
652 * our range starts
653 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500654 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500655 if (!node)
656 goto out;
657 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400658hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500659 if (state->start > end)
660 goto out;
661 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400662 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500663
Liu Bo04493142012-02-16 18:34:37 +0800664 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800665 if (!(state->state & bits)) {
666 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800667 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800668 }
Liu Bo04493142012-02-16 18:34:37 +0800669
Chris Masond1310b22008-01-24 16:13:08 -0500670 /*
671 * | ---- desired range ---- |
672 * | state | or
673 * | ------------- state -------------- |
674 *
675 * We need to split the extent we found, and may flip
676 * bits on second half.
677 *
678 * If the extent we found extends past our range, we
679 * just split and search again. It'll get split again
680 * the next time though.
681 *
682 * If the extent we found is inside our range, we clear
683 * the desired bit on it.
684 */
685
686 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000687 prealloc = alloc_extent_state_atomic(prealloc);
688 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500689 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400690 if (err)
691 extent_io_tree_panic(tree, err);
692
Chris Masond1310b22008-01-24 16:13:08 -0500693 prealloc = NULL;
694 if (err)
695 goto out;
696 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800697 state = clear_state_bit(tree, state, &bits, wake,
698 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800699 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500700 }
701 goto search_again;
702 }
703 /*
704 * | ---- desired range ---- |
705 * | state |
706 * We need to split the extent, and clear the bit
707 * on the first half
708 */
709 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000710 prealloc = alloc_extent_state_atomic(prealloc);
711 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500712 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400713 if (err)
714 extent_io_tree_panic(tree, err);
715
Chris Masond1310b22008-01-24 16:13:08 -0500716 if (wake)
717 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400718
Qu Wenruofefdc552015-10-12 15:35:38 +0800719 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400720
Chris Masond1310b22008-01-24 16:13:08 -0500721 prealloc = NULL;
722 goto out;
723 }
Chris Mason42daec22009-09-23 19:51:09 -0400724
Qu Wenruofefdc552015-10-12 15:35:38 +0800725 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800726next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400727 if (last_end == (u64)-1)
728 goto out;
729 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800730 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800731 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500732
733search_again:
734 if (start > end)
735 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500736 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800737 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500738 cond_resched();
739 goto again;
David Sterba7ab5cb22016-04-27 01:02:15 +0200740
741out:
742 spin_unlock(&tree->lock);
743 if (prealloc)
744 free_extent_state(prealloc);
745
746 return 0;
747
Chris Masond1310b22008-01-24 16:13:08 -0500748}
Chris Masond1310b22008-01-24 16:13:08 -0500749
Jeff Mahoney143bede2012-03-01 14:56:26 +0100750static void wait_on_state(struct extent_io_tree *tree,
751 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500752 __releases(tree->lock)
753 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500754{
755 DEFINE_WAIT(wait);
756 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500757 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500758 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500759 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500760 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500761}
762
763/*
764 * waits for one or more bits to clear on a range in the state tree.
765 * The range [start, end] is inclusive.
766 * The tree lock is taken by this function
767 */
David Sterba41074882013-04-29 13:38:46 +0000768static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
769 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500770{
771 struct extent_state *state;
772 struct rb_node *node;
773
Josef Bacika5dee372013-12-13 10:02:44 -0500774 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000775
Chris Masoncad321a2008-12-17 14:51:42 -0500776 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500777again:
778 while (1) {
779 /*
780 * this search will find all the extents that end after
781 * our range starts
782 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500783 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100784process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500785 if (!node)
786 break;
787
788 state = rb_entry(node, struct extent_state, rb_node);
789
790 if (state->start > end)
791 goto out;
792
793 if (state->state & bits) {
794 start = state->start;
795 atomic_inc(&state->refs);
796 wait_on_state(tree, state);
797 free_extent_state(state);
798 goto again;
799 }
800 start = state->end + 1;
801
802 if (start > end)
803 break;
804
Filipe Mananac50d3e72014-03-31 14:53:25 +0100805 if (!cond_resched_lock(&tree->lock)) {
806 node = rb_next(node);
807 goto process_node;
808 }
Chris Masond1310b22008-01-24 16:13:08 -0500809 }
810out:
Chris Masoncad321a2008-12-17 14:51:42 -0500811 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500812}
Chris Masond1310b22008-01-24 16:13:08 -0500813
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000814static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500815 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800816 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500817{
David Sterba9ee49a042015-01-14 19:52:13 +0100818 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400819
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000820 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400821 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500822 u64 range = state->end - state->start + 1;
823 tree->dirty_bytes += range;
824 }
Qu Wenruod38ed272015-10-12 14:53:37 +0800825 add_extent_changeset(state, bits_to_set, changeset, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400826 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500827}
828
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100829static void cache_state_if_flags(struct extent_state *state,
830 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100831 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400832{
833 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100834 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400835 *cached_ptr = state;
836 atomic_inc(&state->refs);
837 }
838 }
839}
840
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100841static void cache_state(struct extent_state *state,
842 struct extent_state **cached_ptr)
843{
844 return cache_state_if_flags(state, cached_ptr,
845 EXTENT_IOBITS | EXTENT_BOUNDARY);
846}
847
Chris Masond1310b22008-01-24 16:13:08 -0500848/*
Chris Mason1edbb732009-09-02 13:24:36 -0400849 * set some bits on a range in the tree. This may require allocations or
850 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500851 *
Chris Mason1edbb732009-09-02 13:24:36 -0400852 * If any of the exclusive bits are set, this will fail with -EEXIST if some
853 * part of the range already has the desired bits set. The start of the
854 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500855 *
Chris Mason1edbb732009-09-02 13:24:36 -0400856 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500857 */
Chris Mason1edbb732009-09-02 13:24:36 -0400858
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100859static int __must_check
860__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100861 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000862 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800863 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500864{
865 struct extent_state *state;
866 struct extent_state *prealloc = NULL;
867 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000868 struct rb_node **p;
869 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500870 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500871 u64 last_start;
872 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400873
Josef Bacika5dee372013-12-13 10:02:44 -0500874 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000875
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400876 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500877again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800878 if (!prealloc && gfpflags_allow_blocking(mask)) {
David Sterba059f7912016-04-27 01:03:45 +0200879 /*
880 * Don't care for allocation failure here because we might end
881 * up not needing the pre-allocated extent state at all, which
882 * is the case if we only have in the tree extent states that
883 * cover our input range and don't cover too any other range.
884 * If we end up needing a new extent state we allocate it later.
885 */
Chris Masond1310b22008-01-24 16:13:08 -0500886 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500887 }
888
Chris Masoncad321a2008-12-17 14:51:42 -0500889 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400890 if (cached_state && *cached_state) {
891 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400892 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100893 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400894 node = &state->rb_node;
895 goto hit_next;
896 }
897 }
Chris Masond1310b22008-01-24 16:13:08 -0500898 /*
899 * this search will find all the extents that end after
900 * our range starts.
901 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000902 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500903 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000904 prealloc = alloc_extent_state_atomic(prealloc);
905 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000906 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800907 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400908 if (err)
909 extent_io_tree_panic(tree, err);
910
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000911 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500912 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500913 goto out;
914 }
Chris Masond1310b22008-01-24 16:13:08 -0500915 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400916hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500917 last_start = state->start;
918 last_end = state->end;
919
920 /*
921 * | ---- desired range ---- |
922 * | state |
923 *
924 * Just lock what we found and keep going
925 */
926 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400927 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500928 *failed_start = state->start;
929 err = -EEXIST;
930 goto out;
931 }
Chris Mason42daec22009-09-23 19:51:09 -0400932
Qu Wenruod38ed272015-10-12 14:53:37 +0800933 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400934 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500935 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400936 if (last_end == (u64)-1)
937 goto out;
938 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800939 state = next_state(state);
940 if (start < end && state && state->start == start &&
941 !need_resched())
942 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500943 goto search_again;
944 }
945
946 /*
947 * | ---- desired range ---- |
948 * | state |
949 * or
950 * | ------------- state -------------- |
951 *
952 * We need to split the extent we found, and may flip bits on
953 * second half.
954 *
955 * If the extent we found extends past our
956 * range, we just split and search again. It'll get split
957 * again the next time though.
958 *
959 * If the extent we found is inside our range, we set the
960 * desired bit on it.
961 */
962 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400963 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500964 *failed_start = start;
965 err = -EEXIST;
966 goto out;
967 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000968
969 prealloc = alloc_extent_state_atomic(prealloc);
970 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500971 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400972 if (err)
973 extent_io_tree_panic(tree, err);
974
Chris Masond1310b22008-01-24 16:13:08 -0500975 prealloc = NULL;
976 if (err)
977 goto out;
978 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800979 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400980 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500981 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400982 if (last_end == (u64)-1)
983 goto out;
984 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800985 state = next_state(state);
986 if (start < end && state && state->start == start &&
987 !need_resched())
988 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500989 }
990 goto search_again;
991 }
992 /*
993 * | ---- desired range ---- |
994 * | state | or | state |
995 *
996 * There's a hole, we need to insert something in it and
997 * ignore the extent we found.
998 */
999 if (state->start > start) {
1000 u64 this_end;
1001 if (end < last_start)
1002 this_end = end;
1003 else
Chris Masond3977122009-01-05 21:25:51 -05001004 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +00001005
1006 prealloc = alloc_extent_state_atomic(prealloc);
1007 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +00001008
1009 /*
1010 * Avoid to free 'prealloc' if it can be merged with
1011 * the later extent.
1012 */
Chris Masond1310b22008-01-24 16:13:08 -05001013 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001014 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001015 if (err)
1016 extent_io_tree_panic(tree, err);
1017
Chris Mason2c64c532009-09-02 15:04:12 -04001018 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001019 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001020 start = this_end + 1;
1021 goto search_again;
1022 }
1023 /*
1024 * | ---- desired range ---- |
1025 * | state |
1026 * We need to split the extent, and set the bit
1027 * on the first half
1028 */
1029 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001030 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001031 *failed_start = start;
1032 err = -EEXIST;
1033 goto out;
1034 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001035
1036 prealloc = alloc_extent_state_atomic(prealloc);
1037 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001038 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001039 if (err)
1040 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001041
Qu Wenruod38ed272015-10-12 14:53:37 +08001042 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001043 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001044 merge_state(tree, prealloc);
1045 prealloc = NULL;
1046 goto out;
1047 }
1048
David Sterbab5a4ba142016-04-27 01:02:15 +02001049search_again:
1050 if (start > end)
1051 goto out;
1052 spin_unlock(&tree->lock);
1053 if (gfpflags_allow_blocking(mask))
1054 cond_resched();
1055 goto again;
Chris Masond1310b22008-01-24 16:13:08 -05001056
1057out:
Chris Masoncad321a2008-12-17 14:51:42 -05001058 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001059 if (prealloc)
1060 free_extent_state(prealloc);
1061
1062 return err;
1063
Chris Masond1310b22008-01-24 16:13:08 -05001064}
Chris Masond1310b22008-01-24 16:13:08 -05001065
David Sterba41074882013-04-29 13:38:46 +00001066int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001067 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001068 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001069{
1070 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001071 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001072}
1073
1074
Josef Bacik462d6fa2011-09-26 13:56:12 -04001075/**
Liu Bo10983f22012-07-11 15:26:19 +08001076 * convert_extent_bit - convert all bits in a given range from one bit to
1077 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001078 * @tree: the io tree to search
1079 * @start: the start offset in bytes
1080 * @end: the end offset in bytes (inclusive)
1081 * @bits: the bits to set in this range
1082 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001083 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084 *
1085 * This will go through and set bits for the given range. If any states exist
1086 * already in this range they are set with the given bit and cleared of the
1087 * clear_bits. This is only meant to be used by things that are mergeable, ie
1088 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1089 * boundary bits like LOCK.
David Sterba210aa272016-04-26 23:54:39 +02001090 *
1091 * All allocations are done with GFP_NOFS.
Josef Bacik462d6fa2011-09-26 13:56:12 -04001092 */
1093int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001094 unsigned bits, unsigned clear_bits,
David Sterba210aa272016-04-26 23:54:39 +02001095 struct extent_state **cached_state)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001096{
1097 struct extent_state *state;
1098 struct extent_state *prealloc = NULL;
1099 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001100 struct rb_node **p;
1101 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001102 int err = 0;
1103 u64 last_start;
1104 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001105 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001106
Josef Bacika5dee372013-12-13 10:02:44 -05001107 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001108
Josef Bacik462d6fa2011-09-26 13:56:12 -04001109again:
David Sterba210aa272016-04-26 23:54:39 +02001110 if (!prealloc) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001111 /*
1112 * Best effort, don't worry if extent state allocation fails
1113 * here for the first iteration. We might have a cached state
1114 * that matches exactly the target range, in which case no
1115 * extent state allocations are needed. We'll only know this
1116 * after locking the tree.
1117 */
David Sterba210aa272016-04-26 23:54:39 +02001118 prealloc = alloc_extent_state(GFP_NOFS);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001119 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001120 return -ENOMEM;
1121 }
1122
1123 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001124 if (cached_state && *cached_state) {
1125 state = *cached_state;
1126 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001127 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001128 node = &state->rb_node;
1129 goto hit_next;
1130 }
1131 }
1132
Josef Bacik462d6fa2011-09-26 13:56:12 -04001133 /*
1134 * this search will find all the extents that end after
1135 * our range starts.
1136 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001137 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001138 if (!node) {
1139 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001140 if (!prealloc) {
1141 err = -ENOMEM;
1142 goto out;
1143 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001144 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001145 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001146 if (err)
1147 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001148 cache_state(prealloc, cached_state);
1149 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001150 goto out;
1151 }
1152 state = rb_entry(node, struct extent_state, rb_node);
1153hit_next:
1154 last_start = state->start;
1155 last_end = state->end;
1156
1157 /*
1158 * | ---- desired range ---- |
1159 * | state |
1160 *
1161 * Just lock what we found and keep going
1162 */
1163 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001164 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001165 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001166 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001167 if (last_end == (u64)-1)
1168 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001169 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001170 if (start < end && state && state->start == start &&
1171 !need_resched())
1172 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001173 goto search_again;
1174 }
1175
1176 /*
1177 * | ---- desired range ---- |
1178 * | state |
1179 * or
1180 * | ------------- state -------------- |
1181 *
1182 * We need to split the extent we found, and may flip bits on
1183 * second half.
1184 *
1185 * If the extent we found extends past our
1186 * range, we just split and search again. It'll get split
1187 * again the next time though.
1188 *
1189 * If the extent we found is inside our range, we set the
1190 * desired bit on it.
1191 */
1192 if (state->start < start) {
1193 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001194 if (!prealloc) {
1195 err = -ENOMEM;
1196 goto out;
1197 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001198 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001199 if (err)
1200 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001201 prealloc = NULL;
1202 if (err)
1203 goto out;
1204 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001205 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001206 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001207 state = clear_state_bit(tree, state, &clear_bits, 0,
1208 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001209 if (last_end == (u64)-1)
1210 goto out;
1211 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001212 if (start < end && state && state->start == start &&
1213 !need_resched())
1214 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001215 }
1216 goto search_again;
1217 }
1218 /*
1219 * | ---- desired range ---- |
1220 * | state | or | state |
1221 *
1222 * There's a hole, we need to insert something in it and
1223 * ignore the extent we found.
1224 */
1225 if (state->start > start) {
1226 u64 this_end;
1227 if (end < last_start)
1228 this_end = end;
1229 else
1230 this_end = last_start - 1;
1231
1232 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001233 if (!prealloc) {
1234 err = -ENOMEM;
1235 goto out;
1236 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001237
1238 /*
1239 * Avoid to free 'prealloc' if it can be merged with
1240 * the later extent.
1241 */
1242 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001243 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001244 if (err)
1245 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001246 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001247 prealloc = NULL;
1248 start = this_end + 1;
1249 goto search_again;
1250 }
1251 /*
1252 * | ---- desired range ---- |
1253 * | state |
1254 * We need to split the extent, and set the bit
1255 * on the first half
1256 */
1257 if (state->start <= end && state->end > end) {
1258 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001259 if (!prealloc) {
1260 err = -ENOMEM;
1261 goto out;
1262 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001263
1264 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001265 if (err)
1266 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001267
Qu Wenruod38ed272015-10-12 14:53:37 +08001268 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001269 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001270 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001271 prealloc = NULL;
1272 goto out;
1273 }
1274
Josef Bacik462d6fa2011-09-26 13:56:12 -04001275search_again:
1276 if (start > end)
1277 goto out;
1278 spin_unlock(&tree->lock);
David Sterba210aa272016-04-26 23:54:39 +02001279 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001280 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001281 goto again;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001282
1283out:
1284 spin_unlock(&tree->lock);
1285 if (prealloc)
1286 free_extent_state(prealloc);
1287
1288 return err;
1289}
1290
Chris Masond1310b22008-01-24 16:13:08 -05001291/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001292int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001293 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001294{
1295 /*
1296 * We don't support EXTENT_LOCKED yet, as current changeset will
1297 * record any bits changed, so for EXTENT_LOCKED case, it will
1298 * either fail with -EEXIST or changeset will record the whole
1299 * range.
1300 */
1301 BUG_ON(bits & EXTENT_LOCKED);
1302
David Sterba2c53b912016-04-26 23:54:39 +02001303 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001304 changeset);
1305}
1306
Qu Wenruofefdc552015-10-12 15:35:38 +08001307int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1308 unsigned bits, int wake, int delete,
1309 struct extent_state **cached, gfp_t mask)
1310{
1311 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1312 cached, mask, NULL);
1313}
1314
Qu Wenruofefdc552015-10-12 15:35:38 +08001315int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001316 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001317{
1318 /*
1319 * Don't support EXTENT_LOCKED case, same reason as
1320 * set_record_extent_bits().
1321 */
1322 BUG_ON(bits & EXTENT_LOCKED);
1323
David Sterbaf734c442016-04-26 23:54:39 +02001324 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001325 changeset);
1326}
1327
Chris Masond352ac62008-09-29 15:18:18 -04001328/*
1329 * either insert or lock state struct between start and end use mask to tell
1330 * us if waiting is desired.
1331 */
Chris Mason1edbb732009-09-02 13:24:36 -04001332int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001333 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001334{
1335 int err;
1336 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001337
Chris Masond1310b22008-01-24 16:13:08 -05001338 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001339 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001340 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001341 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001342 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001343 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1344 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001345 } else
Chris Masond1310b22008-01-24 16:13:08 -05001346 break;
Chris Masond1310b22008-01-24 16:13:08 -05001347 WARN_ON(start > end);
1348 }
1349 return err;
1350}
Chris Masond1310b22008-01-24 16:13:08 -05001351
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001352int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001353{
1354 int err;
1355 u64 failed_start;
1356
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001357 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001358 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001359 if (err == -EEXIST) {
1360 if (failed_start > start)
1361 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001362 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001363 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001364 }
Josef Bacik25179202008-10-29 14:49:05 -04001365 return 1;
1366}
Josef Bacik25179202008-10-29 14:49:05 -04001367
David Sterbabd1fa4f2015-12-03 13:08:59 +01001368void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001369{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001370 unsigned long index = start >> PAGE_SHIFT;
1371 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001372 struct page *page;
1373
1374 while (index <= end_index) {
1375 page = find_get_page(inode->i_mapping, index);
1376 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1377 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001378 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001379 index++;
1380 }
Chris Mason4adaa612013-03-26 13:07:00 -04001381}
1382
David Sterbaf6311572015-12-03 13:08:59 +01001383void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001384{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001385 unsigned long index = start >> PAGE_SHIFT;
1386 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001387 struct page *page;
1388
1389 while (index <= end_index) {
1390 page = find_get_page(inode->i_mapping, index);
1391 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001392 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001393 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001394 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001395 index++;
1396 }
Chris Mason4adaa612013-03-26 13:07:00 -04001397}
1398
Chris Masond1310b22008-01-24 16:13:08 -05001399/*
Chris Masond1310b22008-01-24 16:13:08 -05001400 * helper function to set both pages and extents in the tree writeback
1401 */
David Sterba35de6db2015-12-03 13:08:59 +01001402static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001403{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001404 unsigned long index = start >> PAGE_SHIFT;
1405 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05001406 struct page *page;
1407
1408 while (index <= end_index) {
1409 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001411 set_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001412 put_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001413 index++;
1414 }
Chris Masond1310b22008-01-24 16:13:08 -05001415}
Chris Masond1310b22008-01-24 16:13:08 -05001416
Chris Masond352ac62008-09-29 15:18:18 -04001417/* find the first state struct with 'bits' set after 'start', and
1418 * return it. tree->lock must be held. NULL will returned if
1419 * nothing was found after 'start'
1420 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001421static struct extent_state *
1422find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001423 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001424{
1425 struct rb_node *node;
1426 struct extent_state *state;
1427
1428 /*
1429 * this search will find all the extents that end after
1430 * our range starts.
1431 */
1432 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001433 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001434 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001435
Chris Masond3977122009-01-05 21:25:51 -05001436 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001437 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001438 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001439 return state;
Chris Masond3977122009-01-05 21:25:51 -05001440
Chris Masond7fc6402008-02-18 12:12:38 -05001441 node = rb_next(node);
1442 if (!node)
1443 break;
1444 }
1445out:
1446 return NULL;
1447}
Chris Masond7fc6402008-02-18 12:12:38 -05001448
Chris Masond352ac62008-09-29 15:18:18 -04001449/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001450 * find the first offset in the io tree with 'bits' set. zero is
1451 * returned if we find something, and *start_ret and *end_ret are
1452 * set to reflect the state struct that was found.
1453 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001454 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001455 */
1456int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001457 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001458 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001459{
1460 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001461 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001462 int ret = 1;
1463
1464 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001465 if (cached_state && *cached_state) {
1466 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001467 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001468 n = rb_next(&state->rb_node);
1469 while (n) {
1470 state = rb_entry(n, struct extent_state,
1471 rb_node);
1472 if (state->state & bits)
1473 goto got_it;
1474 n = rb_next(n);
1475 }
1476 free_extent_state(*cached_state);
1477 *cached_state = NULL;
1478 goto out;
1479 }
1480 free_extent_state(*cached_state);
1481 *cached_state = NULL;
1482 }
1483
Xiao Guangrong69261c42011-07-14 03:19:45 +00001484 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001485got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001486 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001487 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001488 *start_ret = state->start;
1489 *end_ret = state->end;
1490 ret = 0;
1491 }
Josef Bacike6138872012-09-27 17:07:30 -04001492out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001493 spin_unlock(&tree->lock);
1494 return ret;
1495}
1496
1497/*
Chris Masond352ac62008-09-29 15:18:18 -04001498 * find a contiguous range of bytes in the file marked as delalloc, not
1499 * more than 'max_bytes'. start and end are used to return the range,
1500 *
1501 * 1 is returned if we find something, 0 if nothing was in the tree
1502 */
Chris Masonc8b97812008-10-29 14:49:59 -04001503static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001504 u64 *start, u64 *end, u64 max_bytes,
1505 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001506{
1507 struct rb_node *node;
1508 struct extent_state *state;
1509 u64 cur_start = *start;
1510 u64 found = 0;
1511 u64 total_bytes = 0;
1512
Chris Masoncad321a2008-12-17 14:51:42 -05001513 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001514
Chris Masond1310b22008-01-24 16:13:08 -05001515 /*
1516 * this search will find all the extents that end after
1517 * our range starts.
1518 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001519 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001520 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001521 if (!found)
1522 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001523 goto out;
1524 }
1525
Chris Masond3977122009-01-05 21:25:51 -05001526 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001527 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001528 if (found && (state->start != cur_start ||
1529 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001530 goto out;
1531 }
1532 if (!(state->state & EXTENT_DELALLOC)) {
1533 if (!found)
1534 *end = state->end;
1535 goto out;
1536 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001537 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001538 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001539 *cached_state = state;
1540 atomic_inc(&state->refs);
1541 }
Chris Masond1310b22008-01-24 16:13:08 -05001542 found++;
1543 *end = state->end;
1544 cur_start = state->end + 1;
1545 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001546 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001547 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001548 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001549 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001550 break;
1551 }
1552out:
Chris Masoncad321a2008-12-17 14:51:42 -05001553 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001554 return found;
1555}
1556
Liu Boda2c7002017-02-10 16:41:05 +01001557static int __process_pages_contig(struct address_space *mapping,
1558 struct page *locked_page,
1559 pgoff_t start_index, pgoff_t end_index,
1560 unsigned long page_ops, pgoff_t *index_ret);
1561
Jeff Mahoney143bede2012-03-01 14:56:26 +01001562static noinline void __unlock_for_delalloc(struct inode *inode,
1563 struct page *locked_page,
1564 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001565{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001566 unsigned long index = start >> PAGE_SHIFT;
1567 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001568
Liu Bo76c00212017-02-10 16:42:14 +01001569 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001570 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001571 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001572
Liu Bo76c00212017-02-10 16:42:14 +01001573 __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
1574 PAGE_UNLOCK, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -04001575}
1576
1577static noinline int lock_delalloc_pages(struct inode *inode,
1578 struct page *locked_page,
1579 u64 delalloc_start,
1580 u64 delalloc_end)
1581{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001582 unsigned long index = delalloc_start >> PAGE_SHIFT;
Liu Bo76c00212017-02-10 16:42:14 +01001583 unsigned long index_ret = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001584 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001585 int ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001586
Liu Bo76c00212017-02-10 16:42:14 +01001587 ASSERT(locked_page);
Chris Masonc8b97812008-10-29 14:49:59 -04001588 if (index == locked_page->index && index == end_index)
1589 return 0;
1590
Liu Bo76c00212017-02-10 16:42:14 +01001591 ret = __process_pages_contig(inode->i_mapping, locked_page, index,
1592 end_index, PAGE_LOCK, &index_ret);
1593 if (ret == -EAGAIN)
1594 __unlock_for_delalloc(inode, locked_page, delalloc_start,
1595 (u64)index_ret << PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001596 return ret;
1597}
1598
1599/*
1600 * find a contiguous range of bytes in the file marked as delalloc, not
1601 * more than 'max_bytes'. start and end are used to return the range,
1602 *
1603 * 1 is returned if we find something, 0 if nothing was in the tree
1604 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001605STATIC u64 find_lock_delalloc_range(struct inode *inode,
1606 struct extent_io_tree *tree,
1607 struct page *locked_page, u64 *start,
1608 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001609{
1610 u64 delalloc_start;
1611 u64 delalloc_end;
1612 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001613 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001614 int ret;
1615 int loops = 0;
1616
1617again:
1618 /* step one, find a bunch of delalloc bytes starting at start */
1619 delalloc_start = *start;
1620 delalloc_end = 0;
1621 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001622 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001623 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001624 *start = delalloc_start;
1625 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001626 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001627 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001628 }
1629
1630 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001631 * start comes from the offset of locked_page. We have to lock
1632 * pages in order, so we can't process delalloc bytes before
1633 * locked_page
1634 */
Chris Masond3977122009-01-05 21:25:51 -05001635 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001636 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001637
1638 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001639 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001640 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001641 if (delalloc_end + 1 - delalloc_start > max_bytes)
1642 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001643
Chris Masonc8b97812008-10-29 14:49:59 -04001644 /* step two, lock all the pages after the page that has start */
1645 ret = lock_delalloc_pages(inode, locked_page,
1646 delalloc_start, delalloc_end);
1647 if (ret == -EAGAIN) {
1648 /* some of the pages are gone, lets avoid looping by
1649 * shortening the size of the delalloc range we're searching
1650 */
Chris Mason9655d292009-09-02 15:22:30 -04001651 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001652 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001653 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001654 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001655 loops = 1;
1656 goto again;
1657 } else {
1658 found = 0;
1659 goto out_failed;
1660 }
1661 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001662 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001663
1664 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001665 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001666
1667 /* then test to make sure it is all still delalloc */
1668 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001669 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001670 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001671 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1672 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001673 __unlock_for_delalloc(inode, locked_page,
1674 delalloc_start, delalloc_end);
1675 cond_resched();
1676 goto again;
1677 }
Chris Mason9655d292009-09-02 15:22:30 -04001678 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001679 *start = delalloc_start;
1680 *end = delalloc_end;
1681out_failed:
1682 return found;
1683}
1684
Liu Boda2c7002017-02-10 16:41:05 +01001685static int __process_pages_contig(struct address_space *mapping,
1686 struct page *locked_page,
1687 pgoff_t start_index, pgoff_t end_index,
1688 unsigned long page_ops, pgoff_t *index_ret)
Chris Masonc8b97812008-10-29 14:49:59 -04001689{
Liu Bo873695b2017-02-02 17:49:22 -08001690 unsigned long nr_pages = end_index - start_index + 1;
Liu Boda2c7002017-02-10 16:41:05 +01001691 unsigned long pages_locked = 0;
Liu Bo873695b2017-02-02 17:49:22 -08001692 pgoff_t index = start_index;
Chris Masonc8b97812008-10-29 14:49:59 -04001693 struct page *pages[16];
Liu Bo873695b2017-02-02 17:49:22 -08001694 unsigned ret;
Liu Boda2c7002017-02-10 16:41:05 +01001695 int err = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001696 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001697
Liu Boda2c7002017-02-10 16:41:05 +01001698 if (page_ops & PAGE_LOCK) {
1699 ASSERT(page_ops == PAGE_LOCK);
1700 ASSERT(index_ret && *index_ret == start_index);
1701 }
1702
Filipe Manana704de492014-10-06 22:14:22 +01001703 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
Liu Bo873695b2017-02-02 17:49:22 -08001704 mapping_set_error(mapping, -EIO);
Filipe Manana704de492014-10-06 22:14:22 +01001705
Chris Masond3977122009-01-05 21:25:51 -05001706 while (nr_pages > 0) {
Liu Bo873695b2017-02-02 17:49:22 -08001707 ret = find_get_pages_contig(mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001708 min_t(unsigned long,
1709 nr_pages, ARRAY_SIZE(pages)), pages);
Liu Boda2c7002017-02-10 16:41:05 +01001710 if (ret == 0) {
1711 /*
1712 * Only if we're going to lock these pages,
1713 * can we find nothing at @index.
1714 */
1715 ASSERT(page_ops & PAGE_LOCK);
1716 return ret;
1717 }
Chris Mason8b62b722009-09-02 16:53:46 -04001718
Liu Boda2c7002017-02-10 16:41:05 +01001719 for (i = 0; i < ret; i++) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001720 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001721 SetPagePrivate2(pages[i]);
1722
Chris Masonc8b97812008-10-29 14:49:59 -04001723 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001724 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001725 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001726 continue;
1727 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001728 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001729 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001730 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001731 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001732 if (page_ops & PAGE_SET_ERROR)
1733 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001734 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001735 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001736 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001737 unlock_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001738 if (page_ops & PAGE_LOCK) {
1739 lock_page(pages[i]);
1740 if (!PageDirty(pages[i]) ||
1741 pages[i]->mapping != mapping) {
1742 unlock_page(pages[i]);
1743 put_page(pages[i]);
1744 err = -EAGAIN;
1745 goto out;
1746 }
1747 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001748 put_page(pages[i]);
Liu Boda2c7002017-02-10 16:41:05 +01001749 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001750 }
1751 nr_pages -= ret;
1752 index += ret;
1753 cond_resched();
1754 }
Liu Boda2c7002017-02-10 16:41:05 +01001755out:
1756 if (err && index_ret)
1757 *index_ret = start_index + pages_locked - 1;
1758 return err;
Chris Masonc8b97812008-10-29 14:49:59 -04001759}
Chris Masonc8b97812008-10-29 14:49:59 -04001760
Liu Bo873695b2017-02-02 17:49:22 -08001761void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1762 u64 delalloc_end, struct page *locked_page,
1763 unsigned clear_bits,
1764 unsigned long page_ops)
1765{
1766 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
1767 NULL, GFP_NOFS);
1768
1769 __process_pages_contig(inode->i_mapping, locked_page,
1770 start >> PAGE_SHIFT, end >> PAGE_SHIFT,
Liu Boda2c7002017-02-10 16:41:05 +01001771 page_ops, NULL);
Liu Bo873695b2017-02-02 17:49:22 -08001772}
1773
Chris Masond352ac62008-09-29 15:18:18 -04001774/*
1775 * count the number of bytes in the tree that have a given bit(s)
1776 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1777 * cached. The total number found is returned.
1778 */
Chris Masond1310b22008-01-24 16:13:08 -05001779u64 count_range_bits(struct extent_io_tree *tree,
1780 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001781 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001782{
1783 struct rb_node *node;
1784 struct extent_state *state;
1785 u64 cur_start = *start;
1786 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001787 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001788 int found = 0;
1789
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301790 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001791 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001792
Chris Masoncad321a2008-12-17 14:51:42 -05001793 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001794 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1795 total_bytes = tree->dirty_bytes;
1796 goto out;
1797 }
1798 /*
1799 * this search will find all the extents that end after
1800 * our range starts.
1801 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001802 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001803 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001804 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001805
Chris Masond3977122009-01-05 21:25:51 -05001806 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001807 state = rb_entry(node, struct extent_state, rb_node);
1808 if (state->start > search_end)
1809 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001810 if (contig && found && state->start > last + 1)
1811 break;
1812 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001813 total_bytes += min(search_end, state->end) + 1 -
1814 max(cur_start, state->start);
1815 if (total_bytes >= max_bytes)
1816 break;
1817 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001818 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001819 found = 1;
1820 }
Chris Masonec29ed52011-02-23 16:23:20 -05001821 last = state->end;
1822 } else if (contig && found) {
1823 break;
Chris Masond1310b22008-01-24 16:13:08 -05001824 }
1825 node = rb_next(node);
1826 if (!node)
1827 break;
1828 }
1829out:
Chris Masoncad321a2008-12-17 14:51:42 -05001830 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001831 return total_bytes;
1832}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001833
Chris Masond352ac62008-09-29 15:18:18 -04001834/*
1835 * set the private field for a given byte offset in the tree. If there isn't
1836 * an extent_state there already, this does nothing.
1837 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001838static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001839 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001840{
1841 struct rb_node *node;
1842 struct extent_state *state;
1843 int ret = 0;
1844
Chris Masoncad321a2008-12-17 14:51:42 -05001845 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001846 /*
1847 * this search will find all the extents that end after
1848 * our range starts.
1849 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001850 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001851 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001852 ret = -ENOENT;
1853 goto out;
1854 }
1855 state = rb_entry(node, struct extent_state, rb_node);
1856 if (state->start != start) {
1857 ret = -ENOENT;
1858 goto out;
1859 }
David Sterba47dc1962016-02-11 13:24:13 +01001860 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001861out:
Chris Masoncad321a2008-12-17 14:51:42 -05001862 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001863 return ret;
1864}
1865
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001866static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001867 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001868{
1869 struct rb_node *node;
1870 struct extent_state *state;
1871 int ret = 0;
1872
Chris Masoncad321a2008-12-17 14:51:42 -05001873 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001874 /*
1875 * this search will find all the extents that end after
1876 * our range starts.
1877 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001878 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001879 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001880 ret = -ENOENT;
1881 goto out;
1882 }
1883 state = rb_entry(node, struct extent_state, rb_node);
1884 if (state->start != start) {
1885 ret = -ENOENT;
1886 goto out;
1887 }
David Sterba47dc1962016-02-11 13:24:13 +01001888 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001889out:
Chris Masoncad321a2008-12-17 14:51:42 -05001890 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001891 return ret;
1892}
1893
1894/*
1895 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001896 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001897 * has the bits set. Otherwise, 1 is returned if any bit in the
1898 * range is found set.
1899 */
1900int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001901 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001902{
1903 struct extent_state *state = NULL;
1904 struct rb_node *node;
1905 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001906
Chris Masoncad321a2008-12-17 14:51:42 -05001907 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001908 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001909 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001910 node = &cached->rb_node;
1911 else
1912 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001913 while (node && start <= end) {
1914 state = rb_entry(node, struct extent_state, rb_node);
1915
1916 if (filled && state->start > start) {
1917 bitset = 0;
1918 break;
1919 }
1920
1921 if (state->start > end)
1922 break;
1923
1924 if (state->state & bits) {
1925 bitset = 1;
1926 if (!filled)
1927 break;
1928 } else if (filled) {
1929 bitset = 0;
1930 break;
1931 }
Chris Mason46562ce2009-09-23 20:23:16 -04001932
1933 if (state->end == (u64)-1)
1934 break;
1935
Chris Masond1310b22008-01-24 16:13:08 -05001936 start = state->end + 1;
1937 if (start > end)
1938 break;
1939 node = rb_next(node);
1940 if (!node) {
1941 if (filled)
1942 bitset = 0;
1943 break;
1944 }
1945 }
Chris Masoncad321a2008-12-17 14:51:42 -05001946 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001947 return bitset;
1948}
Chris Masond1310b22008-01-24 16:13:08 -05001949
1950/*
1951 * helper function to set a given page up to date if all the
1952 * extents in the tree for that page are up to date
1953 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001954static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001955{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001956 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001957 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001958 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001959 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001960}
1961
Miao Xie8b110e32014-09-12 18:44:03 +08001962int free_io_failure(struct inode *inode, struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001963{
1964 int ret;
1965 int err = 0;
1966 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1967
David Sterba47dc1962016-02-11 13:24:13 +01001968 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001969 ret = clear_extent_bits(failure_tree, rec->start,
1970 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001971 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001972 if (ret)
1973 err = ret;
1974
David Woodhouse53b381b2013-01-29 18:40:14 -05001975 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1976 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001977 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001978 if (ret && !err)
1979 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980
1981 kfree(rec);
1982 return err;
1983}
1984
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001985/*
1986 * this bypasses the standard btrfs submit functions deliberately, as
1987 * the standard behavior is to write all copies in a raid setup. here we only
1988 * want to write the one bad copy. so we do the mapping for ourselves and issue
1989 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001990 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991 * actually prevents the read that triggered the error from finishing.
1992 * currently, there can be no more than two copies of every data bit. thus,
1993 * exactly one rewrite is required.
1994 */
Miao Xie1203b682014-09-12 18:44:01 +08001995int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
1996 struct page *page, unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001997{
Miao Xie1203b682014-09-12 18:44:01 +08001998 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001999 struct bio *bio;
2000 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002001 u64 map_length = 0;
2002 u64 sector;
2003 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002004 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002005 int ret;
2006
Ilya Dryomov908960c2013-11-03 19:06:39 +02002007 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002008 BUG_ON(!mirror_num);
2009
David Woodhouse53b381b2013-01-29 18:40:14 -05002010 /* we can't repair anything in raid56 yet */
2011 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2012 return 0;
2013
Chris Mason9be33952013-05-17 18:30:14 -04002014 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002015 if (!bio)
2016 return -EIO;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002017 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002018 map_length = length;
2019
Filipe Mananab5de8d02016-05-27 22:21:27 +01002020 /*
2021 * Avoid races with device replace and make sure our bbio has devices
2022 * associated to its stripes that don't go away while we are doing the
2023 * read repair operation.
2024 */
2025 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002026 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002027 &map_length, &bbio, mirror_num);
2028 if (ret) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002029 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002030 bio_put(bio);
2031 return -EIO;
2032 }
2033 BUG_ON(mirror_num != bbio->mirror_num);
2034 sector = bbio->stripes[mirror_num-1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002035 bio->bi_iter.bi_sector = sector;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 dev = bbio->stripes[mirror_num-1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002037 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002038 if (!dev || !dev->bdev || !dev->writeable) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002039 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002040 bio_put(bio);
2041 return -EIO;
2042 }
2043 bio->bi_bdev = dev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002044 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002045 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046
Mike Christie4e49ea42016-06-05 14:31:41 -05002047 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002048 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002049 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002050 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002051 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002052 return -EIO;
2053 }
2054
David Sterbab14af3b2015-10-08 10:43:10 +02002055 btrfs_info_rl_in_rcu(fs_info,
2056 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002057 btrfs_ino(BTRFS_I(inode)), start,
Miao Xie1203b682014-09-12 18:44:01 +08002058 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002059 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002060 bio_put(bio);
2061 return 0;
2062}
2063
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002064int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2065 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002066{
Josef Bacikea466792012-03-26 21:57:36 -04002067 u64 start = eb->start;
2068 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002069 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002070
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002071 if (fs_info->sb->s_flags & MS_RDONLY)
Ilya Dryomov908960c2013-11-03 19:06:39 +02002072 return -EROFS;
2073
Josef Bacikea466792012-03-26 21:57:36 -04002074 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002075 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002076
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002077 ret = repair_io_failure(fs_info->btree_inode, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002078 PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002079 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002080 if (ret)
2081 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002083 }
2084
2085 return ret;
2086}
2087
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088/*
2089 * each time an IO finishes, we do a fast check in the IO failure tree
2090 * to see if we need to process or clean up an io_failure_record
2091 */
Miao Xie8b110e32014-09-12 18:44:03 +08002092int clean_io_failure(struct inode *inode, u64 start, struct page *page,
2093 unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002094{
2095 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096 struct io_failure_record *failrec;
Ilya Dryomov908960c2013-11-03 19:06:39 +02002097 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002098 struct extent_state *state;
2099 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002100 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002101
2102 private = 0;
2103 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2104 (u64)-1, 1, EXTENT_DIRTY, 0);
2105 if (!ret)
2106 return 0;
2107
David Sterba47dc1962016-02-11 13:24:13 +01002108 ret = get_state_failrec(&BTRFS_I(inode)->io_failure_tree, start,
2109 &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 if (ret)
2111 return 0;
2112
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 BUG_ON(!failrec->this_mirror);
2114
2115 if (failrec->in_validation) {
2116 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002117 btrfs_debug(fs_info,
2118 "clean_io_failure: freeing dummy error at %llu",
2119 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 goto out;
2121 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002122 if (fs_info->sb->s_flags & MS_RDONLY)
2123 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002124
2125 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2126 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2127 failrec->start,
2128 EXTENT_LOCKED);
2129 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2130
Miao Xie883d0de2013-07-25 19:22:35 +08002131 if (state && state->start <= failrec->start &&
2132 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002133 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2134 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002135 if (num_copies > 1) {
Miao Xie1203b682014-09-12 18:44:01 +08002136 repair_io_failure(inode, start, failrec->len,
Miao Xie454ff3d2014-09-12 18:43:58 +08002137 failrec->logical, page,
Miao Xie1203b682014-09-12 18:44:01 +08002138 pg_offset, failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139 }
2140 }
2141
2142out:
Miao Xie454ff3d2014-09-12 18:43:58 +08002143 free_io_failure(inode, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002144
Miao Xie454ff3d2014-09-12 18:43:58 +08002145 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002146}
2147
Miao Xief6124962014-09-12 18:44:04 +08002148/*
2149 * Can be called when
2150 * - hold extent lock
2151 * - under ordered extent
2152 * - the inode is freeing
2153 */
2154void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
2155{
2156 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2157 struct io_failure_record *failrec;
2158 struct extent_state *state, *next;
2159
2160 if (RB_EMPTY_ROOT(&failure_tree->state))
2161 return;
2162
2163 spin_lock(&failure_tree->lock);
2164 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2165 while (state) {
2166 if (state->start > end)
2167 break;
2168
2169 ASSERT(state->end <= end);
2170
2171 next = next_state(state);
2172
David Sterba47dc1962016-02-11 13:24:13 +01002173 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002174 free_extent_state(state);
2175 kfree(failrec);
2176
2177 state = next;
2178 }
2179 spin_unlock(&failure_tree->lock);
2180}
2181
Miao Xie2fe63032014-09-12 18:43:59 +08002182int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002183 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002184{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002185 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002186 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002188 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2189 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2190 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002191 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002192 u64 logical;
2193
David Sterba47dc1962016-02-11 13:24:13 +01002194 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002195 if (ret) {
2196 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2197 if (!failrec)
2198 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002199
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002200 failrec->start = start;
2201 failrec->len = end - start + 1;
2202 failrec->this_mirror = 0;
2203 failrec->bio_flags = 0;
2204 failrec->in_validation = 0;
2205
2206 read_lock(&em_tree->lock);
2207 em = lookup_extent_mapping(em_tree, start, failrec->len);
2208 if (!em) {
2209 read_unlock(&em_tree->lock);
2210 kfree(failrec);
2211 return -EIO;
2212 }
2213
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002214 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002215 free_extent_map(em);
2216 em = NULL;
2217 }
2218 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002219 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002220 kfree(failrec);
2221 return -EIO;
2222 }
Miao Xie2fe63032014-09-12 18:43:59 +08002223
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224 logical = start - em->start;
2225 logical = em->block_start + logical;
2226 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2227 logical = em->block_start;
2228 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2229 extent_set_compress_type(&failrec->bio_flags,
2230 em->compress_type);
2231 }
Miao Xie2fe63032014-09-12 18:43:59 +08002232
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002233 btrfs_debug(fs_info,
2234 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2235 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002236
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237 failrec->logical = logical;
2238 free_extent_map(em);
2239
2240 /* set the bits in the private failure tree */
2241 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002242 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002243 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002244 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002245 /* set the bits in the inode's tree */
2246 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002247 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002248 if (ret < 0) {
2249 kfree(failrec);
2250 return ret;
2251 }
2252 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002253 btrfs_debug(fs_info,
2254 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2255 failrec->logical, failrec->start, failrec->len,
2256 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002257 /*
2258 * when data can be on disk more than twice, add to failrec here
2259 * (e.g. with a list for failed_mirror) to make
2260 * clean_io_failure() clean all those errors at once.
2261 */
2262 }
Miao Xie2fe63032014-09-12 18:43:59 +08002263
2264 *failrec_ret = failrec;
2265
2266 return 0;
2267}
2268
2269int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
2270 struct io_failure_record *failrec, int failed_mirror)
2271{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002273 int num_copies;
2274
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002275 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 if (num_copies == 1) {
2277 /*
2278 * we only have a single copy of the data, so don't bother with
2279 * all the retry and error correction code that follows. no
2280 * matter what the error is, it is very likely to persist.
2281 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002282 btrfs_debug(fs_info,
2283 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2284 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002285 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002286 }
2287
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002288 /*
2289 * there are two premises:
2290 * a) deliver good data to the caller
2291 * b) correct the bad sectors on disk
2292 */
2293 if (failed_bio->bi_vcnt > 1) {
2294 /*
2295 * to fulfill b), we need to know the exact failing sectors, as
2296 * we don't want to rewrite any more than the failed ones. thus,
2297 * we need separate read requests for the failed bio
2298 *
2299 * if the following BUG_ON triggers, our validation request got
2300 * merged. we need separate requests for our algorithm to work.
2301 */
2302 BUG_ON(failrec->in_validation);
2303 failrec->in_validation = 1;
2304 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 } else {
2306 /*
2307 * we're ready to fulfill a) and b) alongside. get a good copy
2308 * of the failed sector and if we succeed, we have setup
2309 * everything for repair_io_failure to do the rest for us.
2310 */
2311 if (failrec->in_validation) {
2312 BUG_ON(failrec->this_mirror != failed_mirror);
2313 failrec->in_validation = 0;
2314 failrec->this_mirror = 0;
2315 }
2316 failrec->failed_mirror = failed_mirror;
2317 failrec->this_mirror++;
2318 if (failrec->this_mirror == failed_mirror)
2319 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002320 }
2321
Miao Xiefacc8a222013-07-25 19:22:34 +08002322 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002323 btrfs_debug(fs_info,
2324 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2325 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002326 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002327 }
2328
Miao Xie2fe63032014-09-12 18:43:59 +08002329 return 1;
2330}
2331
2332
2333struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2334 struct io_failure_record *failrec,
2335 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002336 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002339 struct bio *bio;
2340 struct btrfs_io_bio *btrfs_failed_bio;
2341 struct btrfs_io_bio *btrfs_bio;
2342
Chris Mason9be33952013-05-17 18:30:14 -04002343 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Miao Xie2fe63032014-09-12 18:43:59 +08002344 if (!bio)
2345 return NULL;
2346
2347 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002348 bio->bi_iter.bi_sector = failrec->logical >> 9;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002349 bio->bi_bdev = fs_info->fs_devices->latest_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002350 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002351 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002352
Miao Xiefacc8a222013-07-25 19:22:34 +08002353 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2354 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002355 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2356
2357 btrfs_bio = btrfs_io_bio(bio);
2358 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002359 icsum *= csum_size;
2360 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002361 csum_size);
2362 }
2363
Miao Xie2fe63032014-09-12 18:43:59 +08002364 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002365
Miao Xie2fe63032014-09-12 18:43:59 +08002366 return bio;
2367}
2368
2369/*
2370 * this is a generic handler for readpage errors (default
2371 * readpage_io_failed_hook). if other copies exist, read those and write back
2372 * good data to the failed position. does not investigate in remapping the
2373 * failed extent elsewhere, hoping the device will be smart enough to do this as
2374 * needed
2375 */
2376
2377static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2378 struct page *page, u64 start, u64 end,
2379 int failed_mirror)
2380{
2381 struct io_failure_record *failrec;
2382 struct inode *inode = page->mapping->host;
2383 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2384 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002385 int read_mode = 0;
Miao Xie2fe63032014-09-12 18:43:59 +08002386 int ret;
2387
Mike Christie1f7ad752016-06-05 14:31:51 -05002388 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002389
2390 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2391 if (ret)
2392 return ret;
2393
2394 ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
2395 if (!ret) {
2396 free_io_failure(inode, failrec);
2397 return -EIO;
2398 }
2399
2400 if (failed_bio->bi_vcnt > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002401 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002402
2403 phy_offset >>= inode->i_sb->s_blocksize_bits;
2404 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2405 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002406 (int)phy_offset, failed_bio->bi_end_io,
2407 NULL);
Miao Xie2fe63032014-09-12 18:43:59 +08002408 if (!bio) {
2409 free_io_failure(inode, failrec);
2410 return -EIO;
2411 }
Mike Christie1f7ad752016-06-05 14:31:51 -05002412 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie2fe63032014-09-12 18:43:59 +08002413
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002414 btrfs_debug(btrfs_sb(inode->i_sb),
2415 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2416 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002417
Mike Christie81a75f672016-06-05 14:31:54 -05002418 ret = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002419 failrec->bio_flags, 0);
Miao Xie6c387ab2014-09-12 18:43:57 +08002420 if (ret) {
Miao Xie454ff3d2014-09-12 18:43:58 +08002421 free_io_failure(inode, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002422 bio_put(bio);
2423 }
2424
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002425 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002426}
2427
Chris Masond1310b22008-01-24 16:13:08 -05002428/* lots and lots of room for performance fixes in the end_bio funcs */
2429
David Sterbab5227c02015-12-03 13:08:59 +01002430void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002431{
2432 int uptodate = (err == 0);
2433 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002434 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002435
2436 tree = &BTRFS_I(page->mapping->host)->io_tree;
2437
2438 if (tree->ops && tree->ops->writepage_end_io_hook) {
2439 ret = tree->ops->writepage_end_io_hook(page, start,
2440 end, NULL, uptodate);
2441 if (ret)
2442 uptodate = 0;
2443 }
2444
Jeff Mahoney87826df2012-02-15 16:23:57 +01002445 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002446 ClearPageUptodate(page);
2447 SetPageError(page);
Liu Bo5dca6ee2014-05-12 12:47:36 +08002448 ret = ret < 0 ? ret : -EIO;
2449 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002450 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002451}
2452
Chris Masond1310b22008-01-24 16:13:08 -05002453/*
2454 * after a writepage IO is done, we need to:
2455 * clear the uptodate bits on error
2456 * clear the writeback bits in the extent tree for this IO
2457 * end_page_writeback if the page has no more pending IO
2458 *
2459 * Scheduling is not allowed, so the extent state tree is expected
2460 * to have one and only one object corresponding to this IO.
2461 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002462static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002463{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002464 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002465 u64 start;
2466 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002467 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002468
Kent Overstreet2c30c712013-11-07 12:20:26 -08002469 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002470 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002471 struct inode *inode = page->mapping->host;
2472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002473
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002474 /* We always issue full-page reads, but if some block
2475 * in a page fails to read, blk_update_request() will
2476 * advance bv_offset and adjust bv_len to compensate.
2477 * Print a warning for nonzero offsets, and an error
2478 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002479 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2480 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002481 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002482 "partial page write in btrfs with offset %u and length %u",
2483 bvec->bv_offset, bvec->bv_len);
2484 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002485 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002486 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002487 bvec->bv_offset, bvec->bv_len);
2488 }
Chris Masond1310b22008-01-24 16:13:08 -05002489
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002490 start = page_offset(page);
2491 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002492
David Sterbab5227c02015-12-03 13:08:59 +01002493 end_extent_writepage(page, bio->bi_error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002494 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002495 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002496
Chris Masond1310b22008-01-24 16:13:08 -05002497 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002498}
2499
Miao Xie883d0de2013-07-25 19:22:35 +08002500static void
2501endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2502 int uptodate)
2503{
2504 struct extent_state *cached = NULL;
2505 u64 end = start + len - 1;
2506
2507 if (uptodate && tree->track_uptodate)
2508 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2509 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2510}
2511
Chris Masond1310b22008-01-24 16:13:08 -05002512/*
2513 * after a readpage IO is done, we need to:
2514 * clear the uptodate bits on error
2515 * set the uptodate bits if things worked
2516 * set the page up to date if all extents in the tree are uptodate
2517 * clear the lock bit in the extent tree
2518 * unlock the page if there are no other extents locked for it
2519 *
2520 * Scheduling is not allowed, so the extent state tree is expected
2521 * to have one and only one object corresponding to this IO.
2522 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002523static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002524{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002525 struct bio_vec *bvec;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002526 int uptodate = !bio->bi_error;
Miao Xiefacc8a222013-07-25 19:22:34 +08002527 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002528 struct extent_io_tree *tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002529 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002530 u64 start;
2531 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002532 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002533 u64 extent_start = 0;
2534 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002535 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002536 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002537 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002538
Kent Overstreet2c30c712013-11-07 12:20:26 -08002539 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002540 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002541 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002543
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002544 btrfs_debug(fs_info,
2545 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
2546 (u64)bio->bi_iter.bi_sector, bio->bi_error,
2547 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002548 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002549
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002550 /* We always issue full-page reads, but if some block
2551 * in a page fails to read, blk_update_request() will
2552 * advance bv_offset and adjust bv_len to compensate.
2553 * Print a warning for nonzero offsets, and an error
2554 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002555 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2556 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002557 btrfs_err(fs_info,
2558 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002559 bvec->bv_offset, bvec->bv_len);
2560 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002561 btrfs_info(fs_info,
2562 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002563 bvec->bv_offset, bvec->bv_len);
2564 }
Chris Masond1310b22008-01-24 16:13:08 -05002565
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002566 start = page_offset(page);
2567 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002568 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002569
Chris Mason9be33952013-05-17 18:30:14 -04002570 mirror = io_bio->mirror_num;
Miao Xief2a09da2013-07-25 19:22:33 +08002571 if (likely(uptodate && tree->ops &&
2572 tree->ops->readpage_end_io_hook)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002573 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2574 page, start, end,
2575 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002576 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002577 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002578 else
Miao Xie1203b682014-09-12 18:44:01 +08002579 clean_io_failure(inode, start, page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002580 }
Josef Bacikea466792012-03-26 21:57:36 -04002581
Miao Xief2a09da2013-07-25 19:22:33 +08002582 if (likely(uptodate))
2583 goto readpage_ok;
2584
2585 if (tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002586 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002587 if (!ret && !bio->bi_error)
Josef Bacikea466792012-03-26 21:57:36 -04002588 uptodate = 1;
Miao Xief2a09da2013-07-25 19:22:33 +08002589 } else {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002590 /*
2591 * The generic bio_readpage_error handles errors the
2592 * following way: If possible, new read requests are
2593 * created and submitted and will end up in
2594 * end_bio_extent_readpage as well (if we're lucky, not
2595 * in the !uptodate case). In that case it returns 0 and
2596 * we just go on with the next page in our bio. If it
2597 * can't handle the error it will return -EIO and we
2598 * remain responsible for that page.
2599 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002600 ret = bio_readpage_error(bio, offset, page, start, end,
2601 mirror);
Chris Mason7e383262008-04-09 16:28:12 -04002602 if (ret == 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002603 uptodate = !bio->bi_error;
Liu Bo38c1c2e2014-08-19 23:33:13 +08002604 offset += len;
Chris Mason7e383262008-04-09 16:28:12 -04002605 continue;
2606 }
2607 }
Miao Xief2a09da2013-07-25 19:22:33 +08002608readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002609 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002610 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002611 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002612 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002613
2614 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002615 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002616 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002617 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002618 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002619 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002620 ClearPageUptodate(page);
2621 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002622 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002623 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002624 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002625
2626 if (unlikely(!uptodate)) {
2627 if (extent_len) {
2628 endio_readpage_release_extent(tree,
2629 extent_start,
2630 extent_len, 1);
2631 extent_start = 0;
2632 extent_len = 0;
2633 }
2634 endio_readpage_release_extent(tree, start,
2635 end - start + 1, 0);
2636 } else if (!extent_len) {
2637 extent_start = start;
2638 extent_len = end + 1 - start;
2639 } else if (extent_start + extent_len == start) {
2640 extent_len += end + 1 - start;
2641 } else {
2642 endio_readpage_release_extent(tree, extent_start,
2643 extent_len, uptodate);
2644 extent_start = start;
2645 extent_len = end + 1 - start;
2646 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002647 }
Chris Masond1310b22008-01-24 16:13:08 -05002648
Miao Xie883d0de2013-07-25 19:22:35 +08002649 if (extent_len)
2650 endio_readpage_release_extent(tree, extent_start, extent_len,
2651 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002652 if (io_bio->end_io)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002653 io_bio->end_io(io_bio, bio->bi_error);
Chris Masond1310b22008-01-24 16:13:08 -05002654 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002655}
2656
Chris Mason9be33952013-05-17 18:30:14 -04002657/*
2658 * this allocates from the btrfs_bioset. We're returning a bio right now
2659 * but you can call btrfs_io_bio for the appropriate container_of magic
2660 */
Miao Xie88f794e2010-11-22 03:02:55 +00002661struct bio *
2662btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2663 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002664{
Miao Xiefacc8a222013-07-25 19:22:34 +08002665 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002666 struct bio *bio;
2667
Chris Mason9be33952013-05-17 18:30:14 -04002668 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002669
2670 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002671 while (!bio && (nr_vecs /= 2)) {
2672 bio = bio_alloc_bioset(gfp_flags,
2673 nr_vecs, btrfs_bioset);
2674 }
Chris Masond1310b22008-01-24 16:13:08 -05002675 }
2676
2677 if (bio) {
2678 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002679 bio->bi_iter.bi_sector = first_sector;
Miao Xiefacc8a222013-07-25 19:22:34 +08002680 btrfs_bio = btrfs_io_bio(bio);
2681 btrfs_bio->csum = NULL;
2682 btrfs_bio->csum_allocated = NULL;
2683 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002684 }
2685 return bio;
2686}
2687
Chris Mason9be33952013-05-17 18:30:14 -04002688struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2689{
Miao Xie23ea8e52014-09-12 18:43:54 +08002690 struct btrfs_io_bio *btrfs_bio;
2691 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002692
Miao Xie23ea8e52014-09-12 18:43:54 +08002693 new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2694 if (new) {
2695 btrfs_bio = btrfs_io_bio(new);
2696 btrfs_bio->csum = NULL;
2697 btrfs_bio->csum_allocated = NULL;
2698 btrfs_bio->end_io = NULL;
2699 }
2700 return new;
2701}
Chris Mason9be33952013-05-17 18:30:14 -04002702
2703/* this also allocates from the btrfs_bioset */
2704struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2705{
Miao Xiefacc8a222013-07-25 19:22:34 +08002706 struct btrfs_io_bio *btrfs_bio;
2707 struct bio *bio;
2708
2709 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2710 if (bio) {
2711 btrfs_bio = btrfs_io_bio(bio);
2712 btrfs_bio->csum = NULL;
2713 btrfs_bio->csum_allocated = NULL;
2714 btrfs_bio->end_io = NULL;
2715 }
2716 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002717}
2718
2719
Mike Christie1f7ad752016-06-05 14:31:51 -05002720static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2721 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002722{
Chris Masond1310b22008-01-24 16:13:08 -05002723 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002724 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2725 struct page *page = bvec->bv_page;
2726 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002727 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002728
Miao Xie4eee4fa2012-12-21 09:17:45 +00002729 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002730
David Woodhouse902b22f2008-08-20 08:51:49 -04002731 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002732 bio_get(bio);
2733
Chris Mason065631f2008-02-20 12:07:25 -05002734 if (tree->ops && tree->ops->submit_bio_hook)
Mike Christie81a75f672016-06-05 14:31:54 -05002735 ret = tree->ops->submit_bio_hook(page->mapping->host, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002736 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002737 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002738 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002739
Chris Masond1310b22008-01-24 16:13:08 -05002740 bio_put(bio);
2741 return ret;
2742}
2743
Mike Christie1f7ad752016-06-05 14:31:51 -05002744static int merge_bio(struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002745 unsigned long offset, size_t size, struct bio *bio,
2746 unsigned long bio_flags)
2747{
2748 int ret = 0;
2749 if (tree->ops && tree->ops->merge_bio_hook)
Mike Christie81a75f672016-06-05 14:31:54 -05002750 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002751 bio_flags);
Jeff Mahoney3444a972011-10-03 23:23:13 -04002752 return ret;
2753
2754}
2755
Mike Christie1f7ad752016-06-05 14:31:51 -05002756static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002757 struct writeback_control *wbc,
Chris Masond1310b22008-01-24 16:13:08 -05002758 struct page *page, sector_t sector,
2759 size_t size, unsigned long offset,
2760 struct block_device *bdev,
2761 struct bio **bio_ret,
Chris Masonf1885912008-04-09 16:28:12 -04002762 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002763 int mirror_num,
2764 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002765 unsigned long bio_flags,
2766 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002767{
2768 int ret = 0;
2769 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002770 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002771 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002772 size_t page_size = min_t(size_t, size, PAGE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002773
2774 if (bio_ret && *bio_ret) {
2775 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002776 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002777 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002778 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002779 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002780
2781 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002782 force_bio_submit ||
Mike Christie1f7ad752016-06-05 14:31:51 -05002783 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002784 bio_add_page(bio, page, page_size, offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002785 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002786 if (ret < 0) {
2787 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002788 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002789 }
Chris Masond1310b22008-01-24 16:13:08 -05002790 bio = NULL;
2791 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002792 if (wbc)
2793 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002794 return 0;
2795 }
2796 }
Chris Masonc8b97812008-10-29 14:49:59 -04002797
Kent Overstreetb54ffb72015-05-19 14:31:01 +02002798 bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
2799 GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002800 if (!bio)
2801 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002802
Chris Masonc8b97812008-10-29 14:49:59 -04002803 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002804 bio->bi_end_io = end_io_func;
2805 bio->bi_private = tree;
Mike Christie1f7ad752016-06-05 14:31:51 -05002806 bio_set_op_attrs(bio, op, op_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07002807 if (wbc) {
2808 wbc_init_bio(wbc, bio);
2809 wbc_account_io(wbc, page, page_size);
2810 }
Chris Mason70dec802008-01-29 09:59:12 -05002811
Chris Masond3977122009-01-05 21:25:51 -05002812 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002813 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002814 else
Mike Christie1f7ad752016-06-05 14:31:51 -05002815 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002816
2817 return ret;
2818}
2819
Eric Sandeen48a3b632013-04-25 20:41:01 +00002820static void attach_extent_buffer_page(struct extent_buffer *eb,
2821 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002822{
2823 if (!PagePrivate(page)) {
2824 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002825 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002826 set_page_private(page, (unsigned long)eb);
2827 } else {
2828 WARN_ON(page->private != (unsigned long)eb);
2829 }
2830}
2831
Chris Masond1310b22008-01-24 16:13:08 -05002832void set_page_extent_mapped(struct page *page)
2833{
2834 if (!PagePrivate(page)) {
2835 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002836 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002837 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002838 }
2839}
2840
Miao Xie125bac012013-07-25 19:22:37 +08002841static struct extent_map *
2842__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2843 u64 start, u64 len, get_extent_t *get_extent,
2844 struct extent_map **em_cached)
2845{
2846 struct extent_map *em;
2847
2848 if (em_cached && *em_cached) {
2849 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002850 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002851 start < extent_map_end(em)) {
2852 atomic_inc(&em->refs);
2853 return em;
2854 }
2855
2856 free_extent_map(em);
2857 *em_cached = NULL;
2858 }
2859
2860 em = get_extent(inode, page, pg_offset, start, len, 0);
2861 if (em_cached && !IS_ERR_OR_NULL(em)) {
2862 BUG_ON(*em_cached);
2863 atomic_inc(&em->refs);
2864 *em_cached = em;
2865 }
2866 return em;
2867}
Chris Masond1310b22008-01-24 16:13:08 -05002868/*
2869 * basic readpage implementation. Locked extent state structs are inserted
2870 * into the tree that are removed when the IO is done (by the end_io
2871 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002872 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002873 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002874 */
Miao Xie99740902013-07-25 19:22:36 +08002875static int __do_readpage(struct extent_io_tree *tree,
2876 struct page *page,
2877 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002878 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002879 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05002880 unsigned long *bio_flags, int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002881 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002882{
2883 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002884 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002885 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002886 u64 end;
2887 u64 cur = start;
2888 u64 extent_offset;
2889 u64 last_byte = i_size_read(inode);
2890 u64 block_start;
2891 u64 cur_end;
2892 sector_t sector;
2893 struct extent_map *em;
2894 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002895 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002896 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002897 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002898 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002899 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002900 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002901 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002902
2903 set_page_extent_mapped(page);
2904
Miao Xie99740902013-07-25 19:22:36 +08002905 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002906 if (!PageUptodate(page)) {
2907 if (cleancache_get_page(page) == 0) {
2908 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002909 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002910 goto out;
2911 }
2912 }
2913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002915 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002916 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002917
2918 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002919 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002920 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002921 memset(userpage + zero_offset, 0, iosize);
2922 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002923 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002924 }
2925 }
Chris Masond1310b22008-01-24 16:13:08 -05002926 while (cur <= end) {
Filipe Manana005efed2015-09-14 09:09:31 +01002927 bool force_bio_submit = false;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002928
Chris Masond1310b22008-01-24 16:13:08 -05002929 if (cur >= last_byte) {
2930 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002931 struct extent_state *cached = NULL;
2932
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002933 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002934 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002935 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002936 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002937 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002938 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002939 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002940 unlock_extent_cached(tree, cur,
2941 cur + iosize - 1,
2942 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002943 break;
2944 }
Miao Xie125bac012013-07-25 19:22:37 +08002945 em = __get_extent_map(inode, page, pg_offset, cur,
2946 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002947 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002948 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002949 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002950 break;
2951 }
Chris Masond1310b22008-01-24 16:13:08 -05002952 extent_offset = cur - em->start;
2953 BUG_ON(extent_map_end(em) <= cur);
2954 BUG_ON(end < cur);
2955
Li Zefan261507a02010-12-17 14:21:50 +08002956 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002957 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002958 extent_set_compress_type(&this_bio_flag,
2959 em->compress_type);
2960 }
Chris Masonc8b97812008-10-29 14:49:59 -04002961
Chris Masond1310b22008-01-24 16:13:08 -05002962 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2963 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002964 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002965 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2966 disk_io_size = em->block_len;
2967 sector = em->block_start >> 9;
2968 } else {
2969 sector = (em->block_start + extent_offset) >> 9;
2970 disk_io_size = iosize;
2971 }
Chris Masond1310b22008-01-24 16:13:08 -05002972 bdev = em->bdev;
2973 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002974 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2975 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002976
2977 /*
2978 * If we have a file range that points to a compressed extent
2979 * and it's followed by a consecutive file range that points to
2980 * to the same compressed extent (possibly with a different
2981 * offset and/or length, so it either points to the whole extent
2982 * or only part of it), we must make sure we do not submit a
2983 * single bio to populate the pages for the 2 ranges because
2984 * this makes the compressed extent read zero out the pages
2985 * belonging to the 2nd range. Imagine the following scenario:
2986 *
2987 * File layout
2988 * [0 - 8K] [8K - 24K]
2989 * | |
2990 * | |
2991 * points to extent X, points to extent X,
2992 * offset 4K, length of 8K offset 0, length 16K
2993 *
2994 * [extent X, compressed length = 4K uncompressed length = 16K]
2995 *
2996 * If the bio to read the compressed extent covers both ranges,
2997 * it will decompress extent X into the pages belonging to the
2998 * first range and then it will stop, zeroing out the remaining
2999 * pages that belong to the other range that points to extent X.
3000 * So here we make sure we submit 2 bios, one for the first
3001 * range and another one for the third range. Both will target
3002 * the same physical extent from disk, but we can't currently
3003 * make the compressed bio endio callback populate the pages
3004 * for both ranges because each compressed bio is tightly
3005 * coupled with a single extent map, and each range can have
3006 * an extent map with a different offset value relative to the
3007 * uncompressed data of our extent and different lengths. This
3008 * is a corner case so we prioritize correctness over
3009 * non-optimal behavior (submitting 2 bios for the same extent).
3010 */
3011 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3012 prev_em_start && *prev_em_start != (u64)-1 &&
3013 *prev_em_start != em->orig_start)
3014 force_bio_submit = true;
3015
3016 if (prev_em_start)
3017 *prev_em_start = em->orig_start;
3018
Chris Masond1310b22008-01-24 16:13:08 -05003019 free_extent_map(em);
3020 em = NULL;
3021
3022 /* we've found a hole, just zero and go on */
3023 if (block_start == EXTENT_MAP_HOLE) {
3024 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003025 struct extent_state *cached = NULL;
3026
Cong Wang7ac687d2011-11-25 23:14:28 +08003027 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003028 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003029 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003030 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003031
3032 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003033 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003034 unlock_extent_cached(tree, cur,
3035 cur + iosize - 1,
3036 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003037 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003038 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003039 continue;
3040 }
3041 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003042 if (test_range_bit(tree, cur, cur_end,
3043 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003044 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003045 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003046 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003047 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003048 continue;
3049 }
Chris Mason70dec802008-01-29 09:59:12 -05003050 /* we have an inline extent but it didn't get marked up
3051 * to date. Error out
3052 */
3053 if (block_start == EXTENT_MAP_INLINE) {
3054 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003055 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003056 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003057 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003058 continue;
3059 }
Chris Masond1310b22008-01-24 16:13:08 -05003060
Mike Christie1f7ad752016-06-05 14:31:51 -05003061 ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL,
3062 page, sector, disk_io_size, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003063 bdev, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003064 end_bio_extent_readpage, mirror_num,
3065 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003066 this_bio_flag,
3067 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003068 if (!ret) {
3069 nr++;
3070 *bio_flags = this_bio_flag;
3071 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003072 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003073 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003074 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003075 }
Chris Masond1310b22008-01-24 16:13:08 -05003076 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003077 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003078 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003079out:
Chris Masond1310b22008-01-24 16:13:08 -05003080 if (!nr) {
3081 if (!PageError(page))
3082 SetPageUptodate(page);
3083 unlock_page(page);
3084 }
Liu Bobaf863b2016-07-11 10:39:07 -07003085 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003086}
3087
Miao Xie99740902013-07-25 19:22:36 +08003088static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3089 struct page *pages[], int nr_pages,
3090 u64 start, u64 end,
3091 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003092 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003093 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003094 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003095 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003096{
3097 struct inode *inode;
3098 struct btrfs_ordered_extent *ordered;
3099 int index;
3100
3101 inode = pages[0]->mapping->host;
3102 while (1) {
3103 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003104 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Miao Xie99740902013-07-25 19:22:36 +08003105 end - start + 1);
3106 if (!ordered)
3107 break;
3108 unlock_extent(tree, start, end);
3109 btrfs_start_ordered_extent(inode, ordered, 1);
3110 btrfs_put_ordered_extent(ordered);
3111 }
3112
3113 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003114 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003115 mirror_num, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003116 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003117 }
3118}
3119
3120static void __extent_readpages(struct extent_io_tree *tree,
3121 struct page *pages[],
3122 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003123 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003124 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003125 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003126 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003127{
Stefan Behrens35a36212013-08-14 18:12:25 +02003128 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003129 u64 end = 0;
3130 u64 page_start;
3131 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003132 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003133
3134 for (index = 0; index < nr_pages; index++) {
3135 page_start = page_offset(pages[index]);
3136 if (!end) {
3137 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003139 first_index = index;
3140 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003141 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003142 } else {
3143 __do_contiguous_readpages(tree, &pages[first_index],
3144 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003145 end, get_extent, em_cached,
3146 bio, mirror_num, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003147 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003148 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003149 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003150 first_index = index;
3151 }
3152 }
3153
3154 if (end)
3155 __do_contiguous_readpages(tree, &pages[first_index],
3156 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003157 end, get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003158 mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003159 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003160}
3161
3162static int __extent_read_full_page(struct extent_io_tree *tree,
3163 struct page *page,
3164 get_extent_t *get_extent,
3165 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003166 unsigned long *bio_flags, int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003167{
3168 struct inode *inode = page->mapping->host;
3169 struct btrfs_ordered_extent *ordered;
3170 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003171 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003172 int ret;
3173
3174 while (1) {
3175 lock_extent(tree, start, end);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02003176 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003177 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003178 if (!ordered)
3179 break;
3180 unlock_extent(tree, start, end);
3181 btrfs_start_ordered_extent(inode, ordered, 1);
3182 btrfs_put_ordered_extent(ordered);
3183 }
3184
Miao Xie125bac012013-07-25 19:22:37 +08003185 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003186 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003187 return ret;
3188}
3189
Chris Masond1310b22008-01-24 16:13:08 -05003190int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003191 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003192{
3193 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003194 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003195 int ret;
3196
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003197 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003198 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003199 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003200 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003201 return ret;
3202}
Chris Masond1310b22008-01-24 16:13:08 -05003203
David Sterba3d4b9492017-02-10 19:33:41 +01003204static void update_nr_written(struct writeback_control *wbc,
Liu Boa91326672016-03-07 16:56:21 -08003205 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003206{
3207 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003208}
3209
Chris Masond1310b22008-01-24 16:13:08 -05003210/*
Chris Mason40f76582014-05-21 13:35:51 -07003211 * helper for __extent_writepage, doing all of the delayed allocation setup.
3212 *
3213 * This returns 1 if our fill_delalloc function did all the work required
3214 * to write the page (copy into inline extent). In this case the IO has
3215 * been started and the page is already unlocked.
3216 *
3217 * This returns 0 if all went well (page still locked)
3218 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003219 */
Chris Mason40f76582014-05-21 13:35:51 -07003220static noinline_for_stack int writepage_delalloc(struct inode *inode,
3221 struct page *page, struct writeback_control *wbc,
3222 struct extent_page_data *epd,
3223 u64 delalloc_start,
3224 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003225{
Chris Mason40f76582014-05-21 13:35:51 -07003226 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003227 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003228 u64 nr_delalloc;
3229 u64 delalloc_to_write = 0;
3230 u64 delalloc_end = 0;
3231 int ret;
3232 int page_started = 0;
3233
3234 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3235 return 0;
3236
3237 while (delalloc_end < page_end) {
3238 nr_delalloc = find_lock_delalloc_range(inode, tree,
3239 page,
3240 &delalloc_start,
3241 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003242 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003243 if (nr_delalloc == 0) {
3244 delalloc_start = delalloc_end + 1;
3245 continue;
3246 }
3247 ret = tree->ops->fill_delalloc(inode, page,
3248 delalloc_start,
3249 delalloc_end,
3250 &page_started,
3251 nr_written);
3252 /* File system has been set read-only */
3253 if (ret) {
3254 SetPageError(page);
3255 /* fill_delalloc should be return < 0 for error
3256 * but just in case, we use > 0 here meaning the
3257 * IO is started, so we don't want to return > 0
3258 * unless things are going well.
3259 */
3260 ret = ret < 0 ? ret : -EIO;
3261 goto done;
3262 }
3263 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003264 * delalloc_end is already one less than the total length, so
3265 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003266 */
3267 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003268 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003269 delalloc_start = delalloc_end + 1;
3270 }
3271 if (wbc->nr_to_write < delalloc_to_write) {
3272 int thresh = 8192;
3273
3274 if (delalloc_to_write < thresh * 2)
3275 thresh = delalloc_to_write;
3276 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3277 thresh);
3278 }
3279
3280 /* did the fill delalloc function already unlock and start
3281 * the IO?
3282 */
3283 if (page_started) {
3284 /*
3285 * we've unlocked the page, so we can't update
3286 * the mapping's writeback index, just update
3287 * nr_to_write.
3288 */
3289 wbc->nr_to_write -= *nr_written;
3290 return 1;
3291 }
3292
3293 ret = 0;
3294
3295done:
3296 return ret;
3297}
3298
3299/*
3300 * helper for __extent_writepage. This calls the writepage start hooks,
3301 * and does the loop to map the page into extents and bios.
3302 *
3303 * We return 1 if the IO is started and the page is unlocked,
3304 * 0 if all went well (page still locked)
3305 * < 0 if there were errors (page still locked)
3306 */
3307static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3308 struct page *page,
3309 struct writeback_control *wbc,
3310 struct extent_page_data *epd,
3311 loff_t i_size,
3312 unsigned long nr_written,
3313 int write_flags, int *nr_ret)
3314{
Chris Masond1310b22008-01-24 16:13:08 -05003315 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003316 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003317 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003318 u64 end;
3319 u64 cur = start;
3320 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003321 u64 block_start;
3322 u64 iosize;
3323 sector_t sector;
3324 struct extent_map *em;
3325 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003326 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003327 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003328 int ret = 0;
3329 int nr = 0;
3330 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003331
Chris Mason247e7432008-07-17 12:53:51 -04003332 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003333 ret = tree->ops->writepage_start_hook(page, start,
3334 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003335 if (ret) {
3336 /* Fixup worker will requeue */
3337 if (ret == -EBUSY)
3338 wbc->pages_skipped++;
3339 else
3340 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003341
David Sterba3d4b9492017-02-10 19:33:41 +01003342 update_nr_written(wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003343 unlock_page(page);
Liu Bobcf93482017-01-25 17:15:54 -08003344 return 1;
Chris Mason247e7432008-07-17 12:53:51 -04003345 }
3346 }
3347
Chris Mason11c83492009-04-20 15:50:09 -04003348 /*
3349 * we don't want to touch the inode after unlocking the page,
3350 * so we update the mapping writeback index now
3351 */
David Sterba3d4b9492017-02-10 19:33:41 +01003352 update_nr_written(wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003353
Chris Masond1310b22008-01-24 16:13:08 -05003354 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003355 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003356 if (tree->ops && tree->ops->writepage_end_io_hook)
3357 tree->ops->writepage_end_io_hook(page, start,
3358 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003359 goto done;
3360 }
3361
Chris Masond1310b22008-01-24 16:13:08 -05003362 blocksize = inode->i_sb->s_blocksize;
3363
3364 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003365 u64 em_end;
David Sterba58409ed2016-05-04 11:46:10 +02003366
Chris Mason40f76582014-05-21 13:35:51 -07003367 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003368 if (tree->ops && tree->ops->writepage_end_io_hook)
3369 tree->ops->writepage_end_io_hook(page, cur,
3370 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003371 break;
3372 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003373 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003374 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003375 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003376 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003377 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003378 break;
3379 }
3380
3381 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003382 em_end = extent_map_end(em);
3383 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003384 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003385 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003386 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003387 sector = (em->block_start + extent_offset) >> 9;
3388 bdev = em->bdev;
3389 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003390 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003391 free_extent_map(em);
3392 em = NULL;
3393
Chris Masonc8b97812008-10-29 14:49:59 -04003394 /*
3395 * compressed and inline extents are written through other
3396 * paths in the FS
3397 */
3398 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003399 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003400 /*
3401 * end_io notification does not happen here for
3402 * compressed extents
3403 */
3404 if (!compressed && tree->ops &&
3405 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003406 tree->ops->writepage_end_io_hook(page, cur,
3407 cur + iosize - 1,
3408 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003409 else if (compressed) {
3410 /* we don't want to end_page_writeback on
3411 * a compressed extent. this happens
3412 * elsewhere
3413 */
3414 nr++;
3415 }
3416
3417 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003418 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003419 continue;
3420 }
Chris Masonc8b97812008-10-29 14:49:59 -04003421
David Sterba58409ed2016-05-04 11:46:10 +02003422 set_range_writeback(tree, cur, cur + iosize - 1);
3423 if (!PageWriteback(page)) {
3424 btrfs_err(BTRFS_I(inode)->root->fs_info,
3425 "page %lu not writeback, cur %llu end %llu",
3426 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003427 }
David Sterba58409ed2016-05-04 11:46:10 +02003428
Mike Christie1f7ad752016-06-05 14:31:51 -05003429 ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
3430 page, sector, iosize, pg_offset,
David Sterbac2df8bb2017-02-10 19:29:38 +01003431 bdev, &epd->bio,
David Sterba58409ed2016-05-04 11:46:10 +02003432 end_bio_extent_writepage,
3433 0, 0, 0, false);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003434 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003435 SetPageError(page);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003436 if (PageWriteback(page))
3437 end_page_writeback(page);
3438 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003439
Chris Masond1310b22008-01-24 16:13:08 -05003440 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003441 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003442 nr++;
3443 }
3444done:
Chris Mason40f76582014-05-21 13:35:51 -07003445 *nr_ret = nr;
Chris Mason40f76582014-05-21 13:35:51 -07003446 return ret;
3447}
3448
3449/*
3450 * the writepage semantics are similar to regular writepage. extent
3451 * records are inserted to lock ranges in the tree, and as dirty areas
3452 * are found, they are marked writeback. Then the lock bits are removed
3453 * and the end_io handler clears the writeback ranges
3454 */
3455static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3456 void *data)
3457{
3458 struct inode *inode = page->mapping->host;
3459 struct extent_page_data *epd = data;
3460 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003461 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003462 int ret;
3463 int nr = 0;
3464 size_t pg_offset = 0;
3465 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003466 unsigned long end_index = i_size >> PAGE_SHIFT;
Mike Christie1f7ad752016-06-05 14:31:51 -05003467 int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003468 unsigned long nr_written = 0;
3469
3470 if (wbc->sync_mode == WB_SYNC_ALL)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003471 write_flags = REQ_SYNC;
Chris Mason40f76582014-05-21 13:35:51 -07003472
3473 trace___extent_writepage(page, inode, wbc);
3474
3475 WARN_ON(!PageLocked(page));
3476
3477 ClearPageError(page);
3478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003479 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003480 if (page->index > end_index ||
3481 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003482 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003483 unlock_page(page);
3484 return 0;
3485 }
3486
3487 if (page->index == end_index) {
3488 char *userpage;
3489
3490 userpage = kmap_atomic(page);
3491 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003492 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003493 kunmap_atomic(userpage);
3494 flush_dcache_page(page);
3495 }
3496
3497 pg_offset = 0;
3498
3499 set_page_extent_mapped(page);
3500
3501 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3502 if (ret == 1)
3503 goto done_unlocked;
3504 if (ret)
3505 goto done;
3506
3507 ret = __extent_writepage_io(inode, page, wbc, epd,
3508 i_size, nr_written, write_flags, &nr);
3509 if (ret == 1)
3510 goto done_unlocked;
3511
3512done:
Chris Masond1310b22008-01-24 16:13:08 -05003513 if (nr == 0) {
3514 /* make sure the mapping tag for page dirty gets cleared */
3515 set_page_writeback(page);
3516 end_page_writeback(page);
3517 }
Filipe Manana61391d52014-05-09 17:17:40 +01003518 if (PageError(page)) {
3519 ret = ret < 0 ? ret : -EIO;
3520 end_extent_writepage(page, ret, start, page_end);
3521 }
Chris Masond1310b22008-01-24 16:13:08 -05003522 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003523 return ret;
Chris Mason771ed682008-11-06 22:02:51 -05003524
Chris Mason11c83492009-04-20 15:50:09 -04003525done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003526 return 0;
3527}
3528
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003529void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003530{
NeilBrown74316202014-07-07 15:16:04 +10003531 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3532 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003533}
3534
Chris Mason0e378df2014-05-19 20:55:27 -07003535static noinline_for_stack int
3536lock_extent_buffer_for_io(struct extent_buffer *eb,
3537 struct btrfs_fs_info *fs_info,
3538 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003539{
3540 unsigned long i, num_pages;
3541 int flush = 0;
3542 int ret = 0;
3543
3544 if (!btrfs_try_tree_write_lock(eb)) {
3545 flush = 1;
3546 flush_write_bio(epd);
3547 btrfs_tree_lock(eb);
3548 }
3549
3550 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3551 btrfs_tree_unlock(eb);
3552 if (!epd->sync_io)
3553 return 0;
3554 if (!flush) {
3555 flush_write_bio(epd);
3556 flush = 1;
3557 }
Chris Masona098d8e82012-03-21 12:09:56 -04003558 while (1) {
3559 wait_on_extent_buffer_writeback(eb);
3560 btrfs_tree_lock(eb);
3561 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3562 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003563 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003564 }
3565 }
3566
Josef Bacik51561ff2012-07-20 16:25:24 -04003567 /*
3568 * We need to do this to prevent races in people who check if the eb is
3569 * under IO since we can end up having no IO bits set for a short period
3570 * of time.
3571 */
3572 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003573 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3574 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003575 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003576 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003577 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3578 -eb->len,
3579 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003580 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003581 } else {
3582 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003583 }
3584
3585 btrfs_tree_unlock(eb);
3586
3587 if (!ret)
3588 return ret;
3589
3590 num_pages = num_extent_pages(eb->start, eb->len);
3591 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003592 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003593
3594 if (!trylock_page(p)) {
3595 if (!flush) {
3596 flush_write_bio(epd);
3597 flush = 1;
3598 }
3599 lock_page(p);
3600 }
3601 }
3602
3603 return ret;
3604}
3605
3606static void end_extent_buffer_writeback(struct extent_buffer *eb)
3607{
3608 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003609 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003610 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3611}
3612
Filipe Manana656f30d2014-09-26 12:25:56 +01003613static void set_btree_ioerr(struct page *page)
3614{
3615 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003616
3617 SetPageError(page);
3618 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3619 return;
3620
3621 /*
3622 * If writeback for a btree extent that doesn't belong to a log tree
3623 * failed, increment the counter transaction->eb_write_errors.
3624 * We do this because while the transaction is running and before it's
3625 * committing (when we call filemap_fdata[write|wait]_range against
3626 * the btree inode), we might have
3627 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3628 * returns an error or an error happens during writeback, when we're
3629 * committing the transaction we wouldn't know about it, since the pages
3630 * can be no longer dirty nor marked anymore for writeback (if a
3631 * subsequent modification to the extent buffer didn't happen before the
3632 * transaction commit), which makes filemap_fdata[write|wait]_range not
3633 * able to find the pages tagged with SetPageError at transaction
3634 * commit time. So if this happens we must abort the transaction,
3635 * otherwise we commit a super block with btree roots that point to
3636 * btree nodes/leafs whose content on disk is invalid - either garbage
3637 * or the content of some node/leaf from a past generation that got
3638 * cowed or deleted and is no longer valid.
3639 *
3640 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3641 * not be enough - we need to distinguish between log tree extents vs
3642 * non-log tree extents, and the next filemap_fdatawait_range() call
3643 * will catch and clear such errors in the mapping - and that call might
3644 * be from a log sync and not from a transaction commit. Also, checking
3645 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3646 * not done and would not be reliable - the eb might have been released
3647 * from memory and reading it back again means that flag would not be
3648 * set (since it's a runtime flag, not persisted on disk).
3649 *
3650 * Using the flags below in the btree inode also makes us achieve the
3651 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3652 * writeback for all dirty pages and before filemap_fdatawait_range()
3653 * is called, the writeback for all dirty pages had already finished
3654 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3655 * filemap_fdatawait_range() would return success, as it could not know
3656 * that writeback errors happened (the pages were no longer tagged for
3657 * writeback).
3658 */
3659 switch (eb->log_index) {
3660 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003661 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003662 break;
3663 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003664 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003665 break;
3666 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003667 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003668 break;
3669 default:
3670 BUG(); /* unexpected, logic error */
3671 }
3672}
3673
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003674static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003675{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003676 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003677 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003678 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679
Kent Overstreet2c30c712013-11-07 12:20:26 -08003680 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003681 struct page *page = bvec->bv_page;
3682
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003683 eb = (struct extent_buffer *)page->private;
3684 BUG_ON(!eb);
3685 done = atomic_dec_and_test(&eb->io_pages);
3686
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003687 if (bio->bi_error ||
3688 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003689 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003690 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691 }
3692
3693 end_page_writeback(page);
3694
3695 if (!done)
3696 continue;
3697
3698 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003699 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003700
3701 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003702}
3703
Chris Mason0e378df2014-05-19 20:55:27 -07003704static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003705 struct btrfs_fs_info *fs_info,
3706 struct writeback_control *wbc,
3707 struct extent_page_data *epd)
3708{
3709 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003710 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003711 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003712 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003713 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003714 unsigned long bio_flags = 0;
Liu Bo851cd172016-09-23 13:44:44 -07003715 unsigned long start, end;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003716 int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003717 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003718
Filipe Manana656f30d2014-09-26 12:25:56 +01003719 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003720 num_pages = num_extent_pages(eb->start, eb->len);
3721 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003722 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3723 bio_flags = EXTENT_BIO_TREE_LOG;
3724
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);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003737 end = btrfs_leaf_data(eb) + 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);
Mike Christie1f7ad752016-06-05 14:31:51 -05003746 ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
3747 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,
Filipe Manana005efed2015-09-14 09:09:31 +01003750 0, epd->bio_flags, bio_flags, false);
Josef Bacikde0022b2012-09-25 14:25:58 -04003751 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003752 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003753 set_btree_ioerr(p);
Takafumi Kubotafe01aa62017-02-09 17:24:33 +09003754 if (PageWriteback(p))
3755 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003756 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3757 end_extent_buffer_writeback(eb);
3758 ret = -EIO;
3759 break;
3760 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003761 offset += PAGE_SIZE;
David Sterba3d4b9492017-02-10 19:33:41 +01003762 update_nr_written(wbc, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 unlock_page(p);
3764 }
3765
3766 if (unlikely(ret)) {
3767 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003768 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003769 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003770 unlock_page(p);
3771 }
3772 }
3773
3774 return ret;
3775}
3776
3777int btree_write_cache_pages(struct address_space *mapping,
3778 struct writeback_control *wbc)
3779{
3780 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3781 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3782 struct extent_buffer *eb, *prev_eb = NULL;
3783 struct extent_page_data epd = {
3784 .bio = NULL,
3785 .tree = tree,
3786 .extent_locked = 0,
3787 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003788 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003789 };
3790 int ret = 0;
3791 int done = 0;
3792 int nr_to_write_done = 0;
3793 struct pagevec pvec;
3794 int nr_pages;
3795 pgoff_t index;
3796 pgoff_t end; /* Inclusive */
3797 int scanned = 0;
3798 int tag;
3799
3800 pagevec_init(&pvec, 0);
3801 if (wbc->range_cyclic) {
3802 index = mapping->writeback_index; /* Start from prev offset */
3803 end = -1;
3804 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003805 index = wbc->range_start >> PAGE_SHIFT;
3806 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003807 scanned = 1;
3808 }
3809 if (wbc->sync_mode == WB_SYNC_ALL)
3810 tag = PAGECACHE_TAG_TOWRITE;
3811 else
3812 tag = PAGECACHE_TAG_DIRTY;
3813retry:
3814 if (wbc->sync_mode == WB_SYNC_ALL)
3815 tag_pages_for_writeback(mapping, index, end);
3816 while (!done && !nr_to_write_done && (index <= end) &&
3817 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3818 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3819 unsigned i;
3820
3821 scanned = 1;
3822 for (i = 0; i < nr_pages; i++) {
3823 struct page *page = pvec.pages[i];
3824
3825 if (!PagePrivate(page))
3826 continue;
3827
3828 if (!wbc->range_cyclic && page->index > end) {
3829 done = 1;
3830 break;
3831 }
3832
Josef Bacikb5bae262012-09-14 13:43:01 -04003833 spin_lock(&mapping->private_lock);
3834 if (!PagePrivate(page)) {
3835 spin_unlock(&mapping->private_lock);
3836 continue;
3837 }
3838
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003839 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003840
3841 /*
3842 * Shouldn't happen and normally this would be a BUG_ON
3843 * but no sense in crashing the users box for something
3844 * we can survive anyway.
3845 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303846 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003847 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003848 continue;
3849 }
3850
Josef Bacikb5bae262012-09-14 13:43:01 -04003851 if (eb == prev_eb) {
3852 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003853 continue;
3854 }
3855
Josef Bacikb5bae262012-09-14 13:43:01 -04003856 ret = atomic_inc_not_zero(&eb->refs);
3857 spin_unlock(&mapping->private_lock);
3858 if (!ret)
3859 continue;
3860
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003861 prev_eb = eb;
3862 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3863 if (!ret) {
3864 free_extent_buffer(eb);
3865 continue;
3866 }
3867
3868 ret = write_one_eb(eb, fs_info, wbc, &epd);
3869 if (ret) {
3870 done = 1;
3871 free_extent_buffer(eb);
3872 break;
3873 }
3874 free_extent_buffer(eb);
3875
3876 /*
3877 * the filesystem may choose to bump up nr_to_write.
3878 * We have to make sure to honor the new nr_to_write
3879 * at any time
3880 */
3881 nr_to_write_done = wbc->nr_to_write <= 0;
3882 }
3883 pagevec_release(&pvec);
3884 cond_resched();
3885 }
3886 if (!scanned && !done) {
3887 /*
3888 * We hit the last page and there is more work to be done: wrap
3889 * back to the start of the file
3890 */
3891 scanned = 1;
3892 index = 0;
3893 goto retry;
3894 }
3895 flush_write_bio(&epd);
3896 return ret;
3897}
3898
Chris Masond1310b22008-01-24 16:13:08 -05003899/**
Chris Mason4bef0842008-09-08 11:18:08 -04003900 * 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 -05003901 * @mapping: address space structure to write
3902 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3903 * @writepage: function called for each page
3904 * @data: data passed to writepage function
3905 *
3906 * If a page is already under I/O, write_cache_pages() skips it, even
3907 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3908 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3909 * and msync() need to guarantee that all the data which was dirty at the time
3910 * the call was made get new I/O started against them. If wbc->sync_mode is
3911 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3912 * existing IO to complete.
3913 */
David Sterba4242b642017-02-10 19:38:24 +01003914static int extent_write_cache_pages(struct address_space *mapping,
Chris Mason4bef0842008-09-08 11:18:08 -04003915 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003916 writepage_t writepage, void *data,
3917 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003918{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003919 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003920 int ret = 0;
3921 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003922 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003923 struct pagevec pvec;
3924 int nr_pages;
3925 pgoff_t index;
3926 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003927 pgoff_t done_index;
3928 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003929 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003930 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003931
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003932 /*
3933 * We have to hold onto the inode so that ordered extents can do their
3934 * work when the IO finishes. The alternative to this is failing to add
3935 * an ordered extent if the igrab() fails there and that is a huge pain
3936 * to deal with, so instead just hold onto the inode throughout the
3937 * writepages operation. If it fails here we are freeing up the inode
3938 * anyway and we'd rather not waste our time writing out stuff that is
3939 * going to be truncated anyway.
3940 */
3941 if (!igrab(inode))
3942 return 0;
3943
Chris Masond1310b22008-01-24 16:13:08 -05003944 pagevec_init(&pvec, 0);
3945 if (wbc->range_cyclic) {
3946 index = mapping->writeback_index; /* Start from prev offset */
3947 end = -1;
3948 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003949 index = wbc->range_start >> PAGE_SHIFT;
3950 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003951 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3952 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003953 scanned = 1;
3954 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003955 if (wbc->sync_mode == WB_SYNC_ALL)
3956 tag = PAGECACHE_TAG_TOWRITE;
3957 else
3958 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003959retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003960 if (wbc->sync_mode == WB_SYNC_ALL)
3961 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003962 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003963 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003964 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3965 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003966 unsigned i;
3967
3968 scanned = 1;
3969 for (i = 0; i < nr_pages; i++) {
3970 struct page *page = pvec.pages[i];
3971
Liu Boa91326672016-03-07 16:56:21 -08003972 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003973 /*
3974 * At this point we hold neither mapping->tree_lock nor
3975 * lock on the page itself: the page may be truncated or
3976 * invalidated (changing page->mapping to NULL), or even
3977 * swizzled back from swapper_space to tmpfs file
3978 * mapping
3979 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003980 if (!trylock_page(page)) {
3981 flush_fn(data);
3982 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003983 }
Chris Masond1310b22008-01-24 16:13:08 -05003984
3985 if (unlikely(page->mapping != mapping)) {
3986 unlock_page(page);
3987 continue;
3988 }
3989
3990 if (!wbc->range_cyclic && page->index > end) {
3991 done = 1;
3992 unlock_page(page);
3993 continue;
3994 }
3995
Chris Masond2c3f4f2008-11-19 12:44:22 -05003996 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003997 if (PageWriteback(page))
3998 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003999 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004000 }
Chris Masond1310b22008-01-24 16:13:08 -05004001
4002 if (PageWriteback(page) ||
4003 !clear_page_dirty_for_io(page)) {
4004 unlock_page(page);
4005 continue;
4006 }
4007
4008 ret = (*writepage)(page, wbc, data);
4009
4010 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4011 unlock_page(page);
4012 ret = 0;
4013 }
Liu Boa91326672016-03-07 16:56:21 -08004014 if (ret < 0) {
4015 /*
4016 * done_index is set past this page,
4017 * so media errors will not choke
4018 * background writeout for the entire
4019 * file. This has consequences for
4020 * range_cyclic semantics (ie. it may
4021 * not be suitable for data integrity
4022 * writeout).
4023 */
4024 done_index = page->index + 1;
4025 done = 1;
4026 break;
4027 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004028
4029 /*
4030 * the filesystem may choose to bump up nr_to_write.
4031 * We have to make sure to honor the new nr_to_write
4032 * at any time
4033 */
4034 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004035 }
4036 pagevec_release(&pvec);
4037 cond_resched();
4038 }
Liu Bo894b36e2016-03-07 16:56:22 -08004039 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004040 /*
4041 * We hit the last page and there is more work to be done: wrap
4042 * back to the start of the file
4043 */
4044 scanned = 1;
4045 index = 0;
4046 goto retry;
4047 }
Liu Boa91326672016-03-07 16:56:21 -08004048
4049 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4050 mapping->writeback_index = done_index;
4051
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004052 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004053 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004054}
Chris Masond1310b22008-01-24 16:13:08 -05004055
Chris Masonffbd5172009-04-20 15:50:09 -04004056static void flush_epd_write_bio(struct extent_page_data *epd)
4057{
4058 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004059 int ret;
4060
Mike Christie1f7ad752016-06-05 14:31:51 -05004061 bio_set_op_attrs(epd->bio, REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004062 epd->sync_io ? REQ_SYNC : 0);
Jeff Mahoney355808c2011-10-03 23:23:14 -04004063
Mike Christie1f7ad752016-06-05 14:31:51 -05004064 ret = submit_one_bio(epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004066 epd->bio = NULL;
4067 }
4068}
4069
Chris Masond2c3f4f2008-11-19 12:44:22 -05004070static noinline void flush_write_bio(void *data)
4071{
4072 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004073 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004074}
4075
Chris Masond1310b22008-01-24 16:13:08 -05004076int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4077 get_extent_t *get_extent,
4078 struct writeback_control *wbc)
4079{
4080 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004081 struct extent_page_data epd = {
4082 .bio = NULL,
4083 .tree = tree,
4084 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004085 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004086 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004087 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004088 };
Chris Masond1310b22008-01-24 16:13:08 -05004089
Chris Masond1310b22008-01-24 16:13:08 -05004090 ret = __extent_writepage(page, wbc, &epd);
4091
Chris Masonffbd5172009-04-20 15:50:09 -04004092 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004093 return ret;
4094}
Chris Masond1310b22008-01-24 16:13:08 -05004095
Chris Mason771ed682008-11-06 22:02:51 -05004096int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4097 u64 start, u64 end, get_extent_t *get_extent,
4098 int mode)
4099{
4100 int ret = 0;
4101 struct address_space *mapping = inode->i_mapping;
4102 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004103 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4104 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004105
4106 struct extent_page_data epd = {
4107 .bio = NULL,
4108 .tree = tree,
4109 .get_extent = get_extent,
4110 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004111 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004112 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05004113 };
4114 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004115 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004116 .nr_to_write = nr_pages * 2,
4117 .range_start = start,
4118 .range_end = end + 1,
4119 };
4120
Chris Masond3977122009-01-05 21:25:51 -05004121 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004122 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004123 if (clear_page_dirty_for_io(page))
4124 ret = __extent_writepage(page, &wbc_writepages, &epd);
4125 else {
4126 if (tree->ops && tree->ops->writepage_end_io_hook)
4127 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004128 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004129 NULL, 1);
4130 unlock_page(page);
4131 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004132 put_page(page);
4133 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004134 }
4135
Chris Masonffbd5172009-04-20 15:50:09 -04004136 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004137 return ret;
4138}
Chris Masond1310b22008-01-24 16:13:08 -05004139
4140int extent_writepages(struct extent_io_tree *tree,
4141 struct address_space *mapping,
4142 get_extent_t *get_extent,
4143 struct writeback_control *wbc)
4144{
4145 int ret = 0;
4146 struct extent_page_data epd = {
4147 .bio = NULL,
4148 .tree = tree,
4149 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004150 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004151 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004152 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004153 };
4154
David Sterba4242b642017-02-10 19:38:24 +01004155 ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004156 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004157 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004158 return ret;
4159}
Chris Masond1310b22008-01-24 16:13:08 -05004160
4161int extent_readpages(struct extent_io_tree *tree,
4162 struct address_space *mapping,
4163 struct list_head *pages, unsigned nr_pages,
4164 get_extent_t get_extent)
4165{
4166 struct bio *bio = NULL;
4167 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004168 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004169 struct page *pagepool[16];
4170 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004171 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004172 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004173 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004174
Chris Masond1310b22008-01-24 16:13:08 -05004175 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004176 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004177
4178 prefetchw(&page->flags);
4179 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004180 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004181 page->index,
4182 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004183 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004184 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004185 }
Liu Bo67c96842012-07-20 21:43:09 -06004186
4187 pagepool[nr++] = page;
4188 if (nr < ARRAY_SIZE(pagepool))
4189 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004190 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004191 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004192 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004193 }
Miao Xie99740902013-07-25 19:22:36 +08004194 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004195 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004196 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004197
Miao Xie125bac012013-07-25 19:22:37 +08004198 if (em_cached)
4199 free_extent_map(em_cached);
4200
Chris Masond1310b22008-01-24 16:13:08 -05004201 BUG_ON(!list_empty(pages));
4202 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004203 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004204 return 0;
4205}
Chris Masond1310b22008-01-24 16:13:08 -05004206
4207/*
4208 * basic invalidatepage code, this waits on any locked or writeback
4209 * ranges corresponding to the page, and then deletes any extent state
4210 * records from the tree
4211 */
4212int extent_invalidatepage(struct extent_io_tree *tree,
4213 struct page *page, unsigned long offset)
4214{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004215 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004216 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004217 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004218 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4219
Qu Wenruofda28322013-02-26 08:10:22 +00004220 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004221 if (start > end)
4222 return 0;
4223
David Sterbaff13db42015-12-03 14:30:40 +01004224 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004225 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004226 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004227 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4228 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004229 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004230 return 0;
4231}
Chris Masond1310b22008-01-24 16:13:08 -05004232
4233/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004234 * a helper for releasepage, this tests for areas of the page that
4235 * are locked or under IO and drops the related state bits if it is safe
4236 * to drop the page.
4237 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004238static int try_release_extent_state(struct extent_map_tree *map,
4239 struct extent_io_tree *tree,
4240 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004241{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004242 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004243 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004244 int ret = 1;
4245
Chris Mason211f90e2008-07-18 11:56:15 -04004246 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004247 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004248 ret = 0;
4249 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004250 /*
4251 * at this point we can safely clear everything except the
4252 * locked bit and the nodatasum bit
4253 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004254 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004255 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4256 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004257
4258 /* if clear_extent_bit failed for enomem reasons,
4259 * we can't allow the release to continue.
4260 */
4261 if (ret < 0)
4262 ret = 0;
4263 else
4264 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004265 }
4266 return ret;
4267}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004268
4269/*
Chris Masond1310b22008-01-24 16:13:08 -05004270 * a helper for releasepage. As long as there are no locked extents
4271 * in the range corresponding to the page, both state records and extent
4272 * map records are removed
4273 */
4274int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004275 struct extent_io_tree *tree, struct page *page,
4276 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004277{
4278 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004279 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004280 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004281
Mel Gormand0164ad2015-11-06 16:28:21 -08004282 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004283 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004284 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004285 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004286 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004287 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004288 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004289 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004290 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004291 break;
4292 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004293 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4294 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004295 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004296 free_extent_map(em);
4297 break;
4298 }
4299 if (!test_range_bit(tree, em->start,
4300 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004301 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004302 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004303 remove_extent_mapping(map, em);
4304 /* once for the rb tree */
4305 free_extent_map(em);
4306 }
4307 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004308 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004309
4310 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004311 free_extent_map(em);
4312 }
Chris Masond1310b22008-01-24 16:13:08 -05004313 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004314 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004315}
Chris Masond1310b22008-01-24 16:13:08 -05004316
Chris Masonec29ed52011-02-23 16:23:20 -05004317/*
4318 * helper function for fiemap, which doesn't want to see any holes.
4319 * This maps until we find something past 'last'
4320 */
4321static struct extent_map *get_extent_skip_holes(struct inode *inode,
4322 u64 offset,
4323 u64 last,
4324 get_extent_t *get_extent)
4325{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004326 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004327 struct extent_map *em;
4328 u64 len;
4329
4330 if (offset >= last)
4331 return NULL;
4332
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304333 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004334 len = last - offset;
4335 if (len == 0)
4336 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004337 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05004338 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004339 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004340 return em;
4341
4342 /* if this isn't a hole return it */
4343 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4344 em->block_start != EXTENT_MAP_HOLE) {
4345 return em;
4346 }
4347
4348 /* this is a hole, advance to the next extent */
4349 offset = extent_map_end(em);
4350 free_extent_map(em);
4351 if (offset >= last)
4352 break;
4353 }
4354 return NULL;
4355}
4356
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004357int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4358 __u64 start, __u64 len, get_extent_t *get_extent)
4359{
Josef Bacik975f84f2010-11-23 19:36:57 +00004360 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004361 u64 off = start;
4362 u64 max = start + len;
4363 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004364 u32 found_type;
4365 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004366 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004367 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004368 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004369 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004370 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004371 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004372 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004373 struct btrfs_root *root = BTRFS_I(inode)->root;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004374 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004375 u64 em_start = 0;
4376 u64 em_len = 0;
4377 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004378
4379 if (len == 0)
4380 return -EINVAL;
4381
Josef Bacik975f84f2010-11-23 19:36:57 +00004382 path = btrfs_alloc_path();
4383 if (!path)
4384 return -ENOMEM;
4385 path->leave_spinning = 1;
4386
Jeff Mahoneyda170662016-06-15 09:22:56 -04004387 start = round_down(start, btrfs_inode_sectorsize(inode));
4388 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004389
Chris Masonec29ed52011-02-23 16:23:20 -05004390 /*
4391 * lookup the last file extent. We're not using i_size here
4392 * because there might be preallocation past i_size
4393 */
David Sterbaf85b7372017-01-20 14:54:07 +01004394 ret = btrfs_lookup_file_extent(NULL, root, path,
4395 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004396 if (ret < 0) {
4397 btrfs_free_path(path);
4398 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004399 } else {
4400 WARN_ON(!ret);
4401 if (ret == 1)
4402 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004403 }
Liu Bo2d324f52016-05-17 17:21:48 -07004404
Josef Bacik975f84f2010-11-23 19:36:57 +00004405 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004406 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004407 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004408
Chris Masonec29ed52011-02-23 16:23:20 -05004409 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004410 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004411 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004412 /* have to trust i_size as the end */
4413 last = (u64)-1;
4414 last_for_get_extent = isize;
4415 } else {
4416 /*
4417 * remember the start of the last extent. There are a
4418 * bunch of different factors that go into the length of the
4419 * extent, so its much less complex to remember where it started
4420 */
4421 last = found_key.offset;
4422 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004423 }
Liu Bofe09e162013-09-22 12:54:23 +08004424 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004425
Chris Masonec29ed52011-02-23 16:23:20 -05004426 /*
4427 * we might have some extents allocated but more delalloc past those
4428 * extents. so, we trust isize unless the start of the last extent is
4429 * beyond isize
4430 */
4431 if (last < isize) {
4432 last = (u64)-1;
4433 last_for_get_extent = isize;
4434 }
4435
David Sterbaff13db42015-12-03 14:30:40 +01004436 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004437 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004438
Josef Bacik4d479cf2011-11-17 11:34:31 -05004439 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004440 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004441 if (!em)
4442 goto out;
4443 if (IS_ERR(em)) {
4444 ret = PTR_ERR(em);
4445 goto out;
4446 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004447
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004448 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004449 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004450
Chris Masonea8efc72011-03-08 11:54:40 -05004451 /* break if the extent we found is outside the range */
4452 if (em->start >= max || extent_map_end(em) < off)
4453 break;
4454
4455 /*
4456 * get_extent may return an extent that starts before our
4457 * requested range. We have to make sure the ranges
4458 * we return to fiemap always move forward and don't
4459 * overlap, so adjust the offsets here
4460 */
4461 em_start = max(em->start, off);
4462
4463 /*
4464 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004465 * for adjusting the disk offset below. Only do this if the
4466 * extent isn't compressed since our in ram offset may be past
4467 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004468 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004469 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4470 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004471 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004472 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004473 disko = 0;
4474 flags = 0;
4475
Chris Masonea8efc72011-03-08 11:54:40 -05004476 /*
4477 * bump off for our next call to get_extent
4478 */
4479 off = extent_map_end(em);
4480 if (off >= max)
4481 end = 1;
4482
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004483 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004484 end = 1;
4485 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004486 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004487 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4488 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004489 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004490 flags |= (FIEMAP_EXTENT_DELALLOC |
4491 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004492 } else if (fieinfo->fi_extents_max) {
Lu Fengqiafce7722016-06-13 09:36:46 +08004493 struct btrfs_trans_handle *trans;
4494
Josef Bacikdc046b12014-09-10 16:20:45 -04004495 u64 bytenr = em->block_start -
4496 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004497
Chris Masonea8efc72011-03-08 11:54:40 -05004498 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004499
4500 /*
Lu Fengqiafce7722016-06-13 09:36:46 +08004501 * We need a trans handle to get delayed refs
4502 */
4503 trans = btrfs_join_transaction(root);
4504 /*
4505 * It's OK if we can't start a trans we can still check
4506 * from commit_root
4507 */
4508 if (IS_ERR(trans))
4509 trans = NULL;
4510
4511 /*
Liu Bofe09e162013-09-22 12:54:23 +08004512 * As btrfs supports shared space, this information
4513 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004514 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4515 * then we're just getting a count and we can skip the
4516 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004517 */
Lu Fengqiafce7722016-06-13 09:36:46 +08004518 ret = btrfs_check_shared(trans, root->fs_info,
David Sterbaf85b7372017-01-20 14:54:07 +01004519 root->objectid,
4520 btrfs_ino(BTRFS_I(inode)), bytenr);
Lu Fengqiafce7722016-06-13 09:36:46 +08004521 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004522 btrfs_end_transaction(trans);
Josef Bacikdc046b12014-09-10 16:20:45 -04004523 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004524 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004525 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004526 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004527 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004528 }
4529 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4530 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004531 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4532 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004533
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004534 free_extent_map(em);
4535 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004536 if ((em_start >= last) || em_len == (u64)-1 ||
4537 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004538 flags |= FIEMAP_EXTENT_LAST;
4539 end = 1;
4540 }
4541
Chris Masonec29ed52011-02-23 16:23:20 -05004542 /* now scan forward to see if this is really the last extent. */
4543 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4544 get_extent);
4545 if (IS_ERR(em)) {
4546 ret = PTR_ERR(em);
4547 goto out;
4548 }
4549 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004550 flags |= FIEMAP_EXTENT_LAST;
4551 end = 1;
4552 }
Chris Masonec29ed52011-02-23 16:23:20 -05004553 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4554 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004555 if (ret) {
4556 if (ret == 1)
4557 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004558 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004559 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004560 }
4561out_free:
4562 free_extent_map(em);
4563out:
Liu Bofe09e162013-09-22 12:54:23 +08004564 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004565 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004566 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004567 return ret;
4568}
4569
Chris Mason727011e2010-08-06 13:21:20 -04004570static void __free_extent_buffer(struct extent_buffer *eb)
4571{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004572 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004573 kmem_cache_free(extent_buffer_cache, eb);
4574}
4575
Josef Bacika26e8c92014-03-28 17:07:27 -04004576int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004577{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004578 return (atomic_read(&eb->io_pages) ||
4579 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4580 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004581}
4582
Miao Xie897ca6e92010-10-26 20:57:29 -04004583/*
4584 * Helper for releasing extent buffer page.
4585 */
David Sterbaa50924e2014-07-31 00:51:36 +02004586static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004587{
4588 unsigned long index;
4589 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004590 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004591
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004592 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004593
David Sterbaa50924e2014-07-31 00:51:36 +02004594 index = num_extent_pages(eb->start, eb->len);
4595 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004596 return;
4597
4598 do {
4599 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004600 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004601 if (!page)
4602 continue;
4603 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004604 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004605 /*
4606 * We do this since we'll remove the pages after we've
4607 * removed the eb from the radix tree, so we could race
4608 * and have this page now attached to the new eb. So
4609 * only clear page_private if it's still connected to
4610 * this eb.
4611 */
4612 if (PagePrivate(page) &&
4613 page->private == (unsigned long)eb) {
4614 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4615 BUG_ON(PageDirty(page));
4616 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004617 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004618 * We need to make sure we haven't be attached
4619 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004620 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004621 ClearPagePrivate(page);
4622 set_page_private(page, 0);
4623 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004624 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004625 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004626
4627 if (mapped)
4628 spin_unlock(&page->mapping->private_lock);
4629
Nicholas D Steeves01327612016-05-19 21:18:45 -04004630 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004631 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004632 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004633}
4634
4635/*
4636 * Helper for releasing the extent buffer.
4637 */
4638static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4639{
David Sterbaa50924e2014-07-31 00:51:36 +02004640 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004641 __free_extent_buffer(eb);
4642}
4643
Josef Bacikf28491e2013-12-16 13:24:27 -05004644static struct extent_buffer *
4645__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004646 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004647{
4648 struct extent_buffer *eb = NULL;
4649
Michal Hockod1b5c562015-08-19 14:17:40 +02004650 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004651 eb->start = start;
4652 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004653 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004654 eb->bflags = 0;
4655 rwlock_init(&eb->lock);
4656 atomic_set(&eb->write_locks, 0);
4657 atomic_set(&eb->read_locks, 0);
4658 atomic_set(&eb->blocking_readers, 0);
4659 atomic_set(&eb->blocking_writers, 0);
4660 atomic_set(&eb->spinning_readers, 0);
4661 atomic_set(&eb->spinning_writers, 0);
4662 eb->lock_nested = 0;
4663 init_waitqueue_head(&eb->write_lock_wq);
4664 init_waitqueue_head(&eb->read_lock_wq);
4665
4666 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4667
4668 spin_lock_init(&eb->refs_lock);
4669 atomic_set(&eb->refs, 1);
4670 atomic_set(&eb->io_pages, 0);
4671
4672 /*
4673 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4674 */
4675 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4676 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4677 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4678
4679 return eb;
4680}
4681
4682struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4683{
4684 unsigned long i;
4685 struct page *p;
4686 struct extent_buffer *new;
4687 unsigned long num_pages = num_extent_pages(src->start, src->len);
4688
David Sterba3f556f72014-06-15 03:20:26 +02004689 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004690 if (new == NULL)
4691 return NULL;
4692
4693 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004694 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004695 if (!p) {
4696 btrfs_release_extent_buffer(new);
4697 return NULL;
4698 }
4699 attach_extent_buffer_page(new, p);
4700 WARN_ON(PageDirty(p));
4701 SetPageUptodate(p);
4702 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004703 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004704 }
4705
Josef Bacikdb7f3432013-08-07 14:54:37 -04004706 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4707 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4708
4709 return new;
4710}
4711
Omar Sandoval0f331222015-09-29 20:50:31 -07004712struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4713 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004714{
4715 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004716 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004717 unsigned long i;
4718
Omar Sandoval0f331222015-09-29 20:50:31 -07004719 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004720
4721 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004722 if (!eb)
4723 return NULL;
4724
4725 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004726 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004727 if (!eb->pages[i])
4728 goto err;
4729 }
4730 set_extent_buffer_uptodate(eb);
4731 btrfs_set_header_nritems(eb, 0);
4732 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4733
4734 return eb;
4735err:
4736 for (; i > 0; i--)
4737 __free_page(eb->pages[i - 1]);
4738 __free_extent_buffer(eb);
4739 return NULL;
4740}
4741
Omar Sandoval0f331222015-09-29 20:50:31 -07004742struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004743 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004744{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004745 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004746}
4747
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004748static void check_buffer_tree_ref(struct extent_buffer *eb)
4749{
Chris Mason242e18c2013-01-29 17:49:37 -05004750 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004751 /* the ref bit is tricky. We have to make sure it is set
4752 * if we have the buffer dirty. Otherwise the
4753 * code to free a buffer can end up dropping a dirty
4754 * page
4755 *
4756 * Once the ref bit is set, it won't go away while the
4757 * buffer is dirty or in writeback, and it also won't
4758 * go away while we have the reference count on the
4759 * eb bumped.
4760 *
4761 * We can't just set the ref bit without bumping the
4762 * ref on the eb because free_extent_buffer might
4763 * see the ref bit and try to clear it. If this happens
4764 * free_extent_buffer might end up dropping our original
4765 * ref by mistake and freeing the page before we are able
4766 * to add one more ref.
4767 *
4768 * So bump the ref count first, then set the bit. If someone
4769 * beat us to it, drop the ref we added.
4770 */
Chris Mason242e18c2013-01-29 17:49:37 -05004771 refs = atomic_read(&eb->refs);
4772 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4773 return;
4774
Josef Bacik594831c2012-07-20 16:11:08 -04004775 spin_lock(&eb->refs_lock);
4776 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004777 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004778 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004779}
4780
Mel Gorman2457aec2014-06-04 16:10:31 -07004781static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4782 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004783{
4784 unsigned long num_pages, i;
4785
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004786 check_buffer_tree_ref(eb);
4787
Josef Bacik5df42352012-03-15 18:24:42 -04004788 num_pages = num_extent_pages(eb->start, eb->len);
4789 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004790 struct page *p = eb->pages[i];
4791
Mel Gorman2457aec2014-06-04 16:10:31 -07004792 if (p != accessed)
4793 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004794 }
4795}
4796
Josef Bacikf28491e2013-12-16 13:24:27 -05004797struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4798 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004799{
4800 struct extent_buffer *eb;
4801
4802 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004803 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004804 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004805 if (eb && atomic_inc_not_zero(&eb->refs)) {
4806 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004807 /*
4808 * Lock our eb's refs_lock to avoid races with
4809 * free_extent_buffer. When we get our eb it might be flagged
4810 * with EXTENT_BUFFER_STALE and another task running
4811 * free_extent_buffer might have seen that flag set,
4812 * eb->refs == 2, that the buffer isn't under IO (dirty and
4813 * writeback flags not set) and it's still in the tree (flag
4814 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4815 * of decrementing the extent buffer's reference count twice.
4816 * So here we could race and increment the eb's reference count,
4817 * clear its stale flag, mark it as dirty and drop our reference
4818 * before the other task finishes executing free_extent_buffer,
4819 * which would later result in an attempt to free an extent
4820 * buffer that is dirty.
4821 */
4822 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4823 spin_lock(&eb->refs_lock);
4824 spin_unlock(&eb->refs_lock);
4825 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004826 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004827 return eb;
4828 }
4829 rcu_read_unlock();
4830
4831 return NULL;
4832}
4833
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004834#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4835struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004836 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004837{
4838 struct extent_buffer *eb, *exists = NULL;
4839 int ret;
4840
4841 eb = find_extent_buffer(fs_info, start);
4842 if (eb)
4843 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004844 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004845 if (!eb)
4846 return NULL;
4847 eb->fs_info = fs_info;
4848again:
David Sterbae1860a72016-05-09 14:11:38 +02004849 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004850 if (ret)
4851 goto free_eb;
4852 spin_lock(&fs_info->buffer_lock);
4853 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004854 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004855 spin_unlock(&fs_info->buffer_lock);
4856 radix_tree_preload_end();
4857 if (ret == -EEXIST) {
4858 exists = find_extent_buffer(fs_info, start);
4859 if (exists)
4860 goto free_eb;
4861 else
4862 goto again;
4863 }
4864 check_buffer_tree_ref(eb);
4865 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4866
4867 /*
4868 * We will free dummy extent buffer's if they come into
4869 * free_extent_buffer with a ref count of 2, but if we are using this we
4870 * want the buffers to stay in memory until we're done with them, so
4871 * bump the ref count again.
4872 */
4873 atomic_inc(&eb->refs);
4874 return eb;
4875free_eb:
4876 btrfs_release_extent_buffer(eb);
4877 return exists;
4878}
4879#endif
4880
Josef Bacikf28491e2013-12-16 13:24:27 -05004881struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004882 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004883{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004884 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004885 unsigned long num_pages = num_extent_pages(start, len);
4886 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004887 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004888 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004889 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004890 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004891 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004892 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004893 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004894
Jeff Mahoneyda170662016-06-15 09:22:56 -04004895 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004896 btrfs_err(fs_info, "bad tree block start %llu", start);
4897 return ERR_PTR(-EINVAL);
4898 }
4899
Josef Bacikf28491e2013-12-16 13:24:27 -05004900 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004901 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004902 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004903
David Sterba23d79d82014-06-15 02:55:29 +02004904 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004905 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004906 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004907
Chris Mason727011e2010-08-06 13:21:20 -04004908 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004909 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004910 if (!p) {
4911 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004912 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004913 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004914
4915 spin_lock(&mapping->private_lock);
4916 if (PagePrivate(p)) {
4917 /*
4918 * We could have already allocated an eb for this page
4919 * and attached one so lets see if we can get a ref on
4920 * the existing eb, and if we can we know it's good and
4921 * we can just return that one, else we know we can just
4922 * overwrite page->private.
4923 */
4924 exists = (struct extent_buffer *)p->private;
4925 if (atomic_inc_not_zero(&exists->refs)) {
4926 spin_unlock(&mapping->private_lock);
4927 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004928 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004929 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004930 goto free_eb;
4931 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004932 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004933
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004934 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004935 * Do this so attach doesn't complain and we need to
4936 * drop the ref the old guy had.
4937 */
4938 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004939 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004940 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004941 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004942 attach_extent_buffer_page(eb, p);
4943 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004944 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004945 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004946 if (!PageUptodate(p))
4947 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004948
4949 /*
4950 * see below about how we avoid a nasty race with release page
4951 * and why we unlock later
4952 */
Chris Masond1310b22008-01-24 16:13:08 -05004953 }
4954 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004955 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004956again:
David Sterbae1860a72016-05-09 14:11:38 +02004957 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004958 if (ret) {
4959 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004960 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004961 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004962
Josef Bacikf28491e2013-12-16 13:24:27 -05004963 spin_lock(&fs_info->buffer_lock);
4964 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004965 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05004966 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004967 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004968 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004969 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004970 if (exists)
4971 goto free_eb;
4972 else
Josef Bacik115391d2012-03-09 09:51:43 -05004973 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004974 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004975 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004976 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004977 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05004978
4979 /*
4980 * there is a race where release page may have
4981 * tried to find this extent buffer in the radix
4982 * but failed. It will tell the VM it is safe to
4983 * reclaim the, and it will clear the page private bit.
4984 * We must make sure to set the page private bit properly
4985 * after the extent buffer is in the radix tree so
4986 * it doesn't get lost
4987 */
Chris Mason727011e2010-08-06 13:21:20 -04004988 SetPageChecked(eb->pages[0]);
4989 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004990 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04004991 ClearPageChecked(p);
4992 unlock_page(p);
4993 }
4994 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004995 return eb;
4996
Chris Mason6af118ce2008-07-22 11:18:07 -04004997free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004998 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04004999 for (i = 0; i < num_pages; i++) {
5000 if (eb->pages[i])
5001 unlock_page(eb->pages[i]);
5002 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005003
Miao Xie897ca6e92010-10-26 20:57:29 -04005004 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005005 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005006}
Chris Masond1310b22008-01-24 16:13:08 -05005007
Josef Bacik3083ee22012-03-09 16:01:49 -05005008static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5009{
5010 struct extent_buffer *eb =
5011 container_of(head, struct extent_buffer, rcu_head);
5012
5013 __free_extent_buffer(eb);
5014}
5015
Josef Bacik3083ee22012-03-09 16:01:49 -05005016/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005017static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005018{
5019 WARN_ON(atomic_read(&eb->refs) == 0);
5020 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005021 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005022 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005023
Jan Schmidt815a51c2012-05-16 17:00:02 +02005024 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005025
Josef Bacikf28491e2013-12-16 13:24:27 -05005026 spin_lock(&fs_info->buffer_lock);
5027 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005028 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005029 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005030 } else {
5031 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005032 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005033
5034 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005035 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005036#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5037 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5038 __free_extent_buffer(eb);
5039 return 1;
5040 }
5041#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005042 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005043 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005044 }
5045 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005046
5047 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005048}
5049
Chris Masond1310b22008-01-24 16:13:08 -05005050void free_extent_buffer(struct extent_buffer *eb)
5051{
Chris Mason242e18c2013-01-29 17:49:37 -05005052 int refs;
5053 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005054 if (!eb)
5055 return;
5056
Chris Mason242e18c2013-01-29 17:49:37 -05005057 while (1) {
5058 refs = atomic_read(&eb->refs);
5059 if (refs <= 3)
5060 break;
5061 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5062 if (old == refs)
5063 return;
5064 }
5065
Josef Bacik3083ee22012-03-09 16:01:49 -05005066 spin_lock(&eb->refs_lock);
5067 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005068 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5069 atomic_dec(&eb->refs);
5070
5071 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005072 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005073 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005074 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5075 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005076
Josef Bacik3083ee22012-03-09 16:01:49 -05005077 /*
5078 * I know this is terrible, but it's temporary until we stop tracking
5079 * the uptodate bits and such for the extent buffers.
5080 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005081 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005082}
Chris Masond1310b22008-01-24 16:13:08 -05005083
Josef Bacik3083ee22012-03-09 16:01:49 -05005084void free_extent_buffer_stale(struct extent_buffer *eb)
5085{
5086 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005087 return;
5088
Josef Bacik3083ee22012-03-09 16:01:49 -05005089 spin_lock(&eb->refs_lock);
5090 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5091
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005092 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005093 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5094 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005095 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005096}
5097
Chris Mason1d4284b2012-03-28 20:31:37 -04005098void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005099{
Chris Masond1310b22008-01-24 16:13:08 -05005100 unsigned long i;
5101 unsigned long num_pages;
5102 struct page *page;
5103
Chris Masond1310b22008-01-24 16:13:08 -05005104 num_pages = num_extent_pages(eb->start, eb->len);
5105
5106 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005107 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005108 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005109 continue;
5110
Chris Masona61e6f22008-07-22 11:18:08 -04005111 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005112 WARN_ON(!PagePrivate(page));
5113
Chris Masond1310b22008-01-24 16:13:08 -05005114 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005115 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005116 if (!PageDirty(page)) {
5117 radix_tree_tag_clear(&page->mapping->page_tree,
5118 page_index(page),
5119 PAGECACHE_TAG_DIRTY);
5120 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005121 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005122 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005123 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005124 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005125 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005126}
Chris Masond1310b22008-01-24 16:13:08 -05005127
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005128int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005129{
5130 unsigned long i;
5131 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005132 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005133
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005134 check_buffer_tree_ref(eb);
5135
Chris Masonb9473432009-03-13 11:00:37 -04005136 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005137
Chris Masond1310b22008-01-24 16:13:08 -05005138 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005139 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005140 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5141
Chris Masonb9473432009-03-13 11:00:37 -04005142 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005143 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005144 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005145}
Chris Masond1310b22008-01-24 16:13:08 -05005146
David Sterba69ba3922015-12-03 13:08:59 +01005147void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005148{
5149 unsigned long i;
5150 struct page *page;
5151 unsigned long num_pages;
5152
Chris Masonb4ce94d2009-02-04 09:25:08 -05005153 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005154 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005155 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005156 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005157 if (page)
5158 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005159 }
Chris Mason1259ab72008-05-12 13:39:03 -04005160}
5161
David Sterba09c25a82015-12-03 13:08:59 +01005162void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005163{
5164 unsigned long i;
5165 struct page *page;
5166 unsigned long num_pages;
5167
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005168 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005169 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005170 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005171 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005172 SetPageUptodate(page);
5173 }
Chris Masond1310b22008-01-24 16:13:08 -05005174}
Chris Masond1310b22008-01-24 16:13:08 -05005175
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005176int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005177{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005178 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005179}
Chris Masond1310b22008-01-24 16:13:08 -05005180
5181int read_extent_buffer_pages(struct extent_io_tree *tree,
Josef Bacik8436ea912016-09-02 15:40:03 -04005182 struct extent_buffer *eb, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005183 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005184{
5185 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005186 struct page *page;
5187 int err;
5188 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005189 int locked_pages = 0;
5190 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005191 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005192 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005193 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005194 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005195
Chris Masonb4ce94d2009-02-04 09:25:08 -05005196 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005197 return 0;
5198
Chris Masond1310b22008-01-24 16:13:08 -05005199 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005200 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005201 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005202 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005203 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005204 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005205 } else {
5206 lock_page(page);
5207 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005208 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005209 }
5210 /*
5211 * We need to firstly lock all pages to make sure that
5212 * the uptodate bit of our pages won't be affected by
5213 * clear_extent_buffer_uptodate().
5214 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005215 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005216 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005217 if (!PageUptodate(page)) {
5218 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005219 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005220 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005221 }
Liu Bo2571e732016-08-03 12:33:01 -07005222
Chris Masonce9adaa2008-04-09 16:28:12 -04005223 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005224 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005225 goto unlock_exit;
5226 }
5227
Filipe Manana656f30d2014-09-26 12:25:56 +01005228 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005229 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005230 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005231 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005232 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005233
Chris Masonce9adaa2008-04-09 16:28:12 -04005234 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005235 if (ret) {
5236 atomic_dec(&eb->io_pages);
5237 unlock_page(page);
5238 continue;
5239 }
5240
Chris Masonf1885912008-04-09 16:28:12 -04005241 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005242 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005243 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005244 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005245 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005246 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005247 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005248 /*
5249 * We use &bio in above __extent_read_full_page,
5250 * so we ensure that if it returns error, the
5251 * current page fails to add itself to bio and
5252 * it's been unlocked.
5253 *
5254 * We must dec io_pages by ourselves.
5255 */
5256 atomic_dec(&eb->io_pages);
5257 }
Chris Masond1310b22008-01-24 16:13:08 -05005258 } else {
5259 unlock_page(page);
5260 }
5261 }
5262
Jeff Mahoney355808c2011-10-03 23:23:14 -04005263 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005264 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005265 if (err)
5266 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005267 }
Chris Masona86c12c2008-02-07 10:50:54 -05005268
Arne Jansenbb82ab82011-06-10 14:06:53 +02005269 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005270 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005271
Josef Bacik8436ea912016-09-02 15:40:03 -04005272 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005273 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005274 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005275 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005276 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005277 }
Chris Masond3977122009-01-05 21:25:51 -05005278
Chris Masond1310b22008-01-24 16:13:08 -05005279 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005280
5281unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005282 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005283 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005284 page = eb->pages[locked_pages];
5285 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005286 }
5287 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005288}
Chris Masond1310b22008-01-24 16:13:08 -05005289
5290void read_extent_buffer(struct extent_buffer *eb, void *dstv,
5291 unsigned long start,
5292 unsigned long len)
5293{
5294 size_t cur;
5295 size_t offset;
5296 struct page *page;
5297 char *kaddr;
5298 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005299 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5300 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005301
5302 WARN_ON(start > eb->len);
5303 WARN_ON(start + len > eb->start + eb->len);
5304
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005305 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005306
Chris Masond3977122009-01-05 21:25:51 -05005307 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005308 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005309
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005310 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005311 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005312 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005313
5314 dst += cur;
5315 len -= cur;
5316 offset = 0;
5317 i++;
5318 }
5319}
Chris Masond1310b22008-01-24 16:13:08 -05005320
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005321int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
5322 unsigned long start,
5323 unsigned long len)
5324{
5325 size_t cur;
5326 size_t offset;
5327 struct page *page;
5328 char *kaddr;
5329 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005330 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5331 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005332 int ret = 0;
5333
5334 WARN_ON(start > eb->len);
5335 WARN_ON(start + len > eb->start + eb->len);
5336
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005337 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005338
5339 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005340 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005341
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005342 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005343 kaddr = page_address(page);
5344 if (copy_to_user(dst, kaddr + offset, cur)) {
5345 ret = -EFAULT;
5346 break;
5347 }
5348
5349 dst += cur;
5350 len -= cur;
5351 offset = 0;
5352 i++;
5353 }
5354
5355 return ret;
5356}
5357
Liu Bo415b35a2016-06-17 19:16:21 -07005358/*
5359 * return 0 if the item is found within a page.
5360 * return 1 if the item spans two pages.
5361 * return -EINVAL otherwise.
5362 */
Chris Masond1310b22008-01-24 16:13:08 -05005363int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04005364 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05005365 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04005366 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005367{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005368 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005369 char *kaddr;
5370 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005371 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5372 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005373 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005374 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005375
5376 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005377 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005378
5379 if (i == 0) {
5380 offset = start_offset;
5381 *map_start = 0;
5382 } else {
5383 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005384 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005385 }
Chris Masond3977122009-01-05 21:25:51 -05005386
Chris Masond1310b22008-01-24 16:13:08 -05005387 if (start + min_len > eb->len) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005388 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005389 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04005390 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005391 }
5392
David Sterbafb85fc92014-07-31 01:03:53 +02005393 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005394 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005395 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005396 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005397 return 0;
5398}
Chris Masond1310b22008-01-24 16:13:08 -05005399
Chris Masond1310b22008-01-24 16:13:08 -05005400int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
5401 unsigned long start,
5402 unsigned long len)
5403{
5404 size_t cur;
5405 size_t offset;
5406 struct page *page;
5407 char *kaddr;
5408 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005409 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5410 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005411 int ret = 0;
5412
5413 WARN_ON(start > eb->len);
5414 WARN_ON(start + len > eb->start + eb->len);
5415
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005416 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005417
Chris Masond3977122009-01-05 21:25:51 -05005418 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005419 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005420
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005421 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005422
Chris Masona6591712011-07-19 12:04:14 -04005423 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005424 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005425 if (ret)
5426 break;
5427
5428 ptr += cur;
5429 len -= cur;
5430 offset = 0;
5431 i++;
5432 }
5433 return ret;
5434}
Chris Masond1310b22008-01-24 16:13:08 -05005435
David Sterbaf157bf72016-11-09 17:43:38 +01005436void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5437 const void *srcv)
5438{
5439 char *kaddr;
5440
5441 WARN_ON(!PageUptodate(eb->pages[0]));
5442 kaddr = page_address(eb->pages[0]);
5443 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5444 BTRFS_FSID_SIZE);
5445}
5446
5447void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5448{
5449 char *kaddr;
5450
5451 WARN_ON(!PageUptodate(eb->pages[0]));
5452 kaddr = page_address(eb->pages[0]);
5453 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5454 BTRFS_FSID_SIZE);
5455}
5456
Chris Masond1310b22008-01-24 16:13:08 -05005457void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5458 unsigned long start, unsigned long len)
5459{
5460 size_t cur;
5461 size_t offset;
5462 struct page *page;
5463 char *kaddr;
5464 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005465 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5466 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005467
5468 WARN_ON(start > eb->len);
5469 WARN_ON(start + len > eb->start + eb->len);
5470
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005471 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005472
Chris Masond3977122009-01-05 21:25:51 -05005473 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005474 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005475 WARN_ON(!PageUptodate(page));
5476
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005477 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005478 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005479 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005480
5481 src += cur;
5482 len -= cur;
5483 offset = 0;
5484 i++;
5485 }
5486}
Chris Masond1310b22008-01-24 16:13:08 -05005487
David Sterbab159fa22016-11-08 18:09:03 +01005488void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5489 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005490{
5491 size_t cur;
5492 size_t offset;
5493 struct page *page;
5494 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005495 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5496 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005497
5498 WARN_ON(start > eb->len);
5499 WARN_ON(start + len > eb->start + eb->len);
5500
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005501 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005502
Chris Masond3977122009-01-05 21:25:51 -05005503 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005504 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005505 WARN_ON(!PageUptodate(page));
5506
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005507 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005508 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005509 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005510
5511 len -= cur;
5512 offset = 0;
5513 i++;
5514 }
5515}
Chris Masond1310b22008-01-24 16:13:08 -05005516
David Sterba58e80122016-11-08 18:30:31 +01005517void copy_extent_buffer_full(struct extent_buffer *dst,
5518 struct extent_buffer *src)
5519{
5520 int i;
5521 unsigned num_pages;
5522
5523 ASSERT(dst->len == src->len);
5524
5525 num_pages = num_extent_pages(dst->start, dst->len);
5526 for (i = 0; i < num_pages; i++)
5527 copy_page(page_address(dst->pages[i]),
5528 page_address(src->pages[i]));
5529}
5530
Chris Masond1310b22008-01-24 16:13:08 -05005531void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5532 unsigned long dst_offset, unsigned long src_offset,
5533 unsigned long len)
5534{
5535 u64 dst_len = dst->len;
5536 size_t cur;
5537 size_t offset;
5538 struct page *page;
5539 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005540 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5541 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005542
5543 WARN_ON(src->len != dst_len);
5544
5545 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005546 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005547
Chris Masond3977122009-01-05 21:25:51 -05005548 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005549 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005550 WARN_ON(!PageUptodate(page));
5551
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005552 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005553
Chris Masona6591712011-07-19 12:04:14 -04005554 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005555 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005556
5557 src_offset += cur;
5558 len -= cur;
5559 offset = 0;
5560 i++;
5561 }
5562}
Chris Masond1310b22008-01-24 16:13:08 -05005563
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005564void le_bitmap_set(u8 *map, unsigned int start, int len)
5565{
5566 u8 *p = map + BIT_BYTE(start);
5567 const unsigned int size = start + len;
5568 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5569 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5570
5571 while (len - bits_to_set >= 0) {
5572 *p |= mask_to_set;
5573 len -= bits_to_set;
5574 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005575 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005576 p++;
5577 }
5578 if (len) {
5579 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5580 *p |= mask_to_set;
5581 }
5582}
5583
5584void le_bitmap_clear(u8 *map, unsigned int start, int len)
5585{
5586 u8 *p = map + BIT_BYTE(start);
5587 const unsigned int size = start + len;
5588 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5589 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5590
5591 while (len - bits_to_clear >= 0) {
5592 *p &= ~mask_to_clear;
5593 len -= bits_to_clear;
5594 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005595 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005596 p++;
5597 }
5598 if (len) {
5599 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5600 *p &= ~mask_to_clear;
5601 }
5602}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005603
5604/*
5605 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5606 * given bit number
5607 * @eb: the extent buffer
5608 * @start: offset of the bitmap item in the extent buffer
5609 * @nr: bit number
5610 * @page_index: return index of the page in the extent buffer that contains the
5611 * given bit number
5612 * @page_offset: return offset into the page given by page_index
5613 *
5614 * This helper hides the ugliness of finding the byte in an extent buffer which
5615 * contains a given bit.
5616 */
5617static inline void eb_bitmap_offset(struct extent_buffer *eb,
5618 unsigned long start, unsigned long nr,
5619 unsigned long *page_index,
5620 size_t *page_offset)
5621{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005622 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005623 size_t byte_offset = BIT_BYTE(nr);
5624 size_t offset;
5625
5626 /*
5627 * The byte we want is the offset of the extent buffer + the offset of
5628 * the bitmap item in the extent buffer + the offset of the byte in the
5629 * bitmap item.
5630 */
5631 offset = start_offset + start + byte_offset;
5632
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005633 *page_index = offset >> PAGE_SHIFT;
5634 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005635}
5636
5637/**
5638 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5639 * @eb: the extent buffer
5640 * @start: offset of the bitmap item in the extent buffer
5641 * @nr: bit number to test
5642 */
5643int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5644 unsigned long nr)
5645{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005646 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005647 struct page *page;
5648 unsigned long i;
5649 size_t offset;
5650
5651 eb_bitmap_offset(eb, start, nr, &i, &offset);
5652 page = eb->pages[i];
5653 WARN_ON(!PageUptodate(page));
5654 kaddr = page_address(page);
5655 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5656}
5657
5658/**
5659 * extent_buffer_bitmap_set - set an area of a bitmap
5660 * @eb: the extent buffer
5661 * @start: offset of the bitmap item in the extent buffer
5662 * @pos: bit number of the first bit
5663 * @len: number of bits to set
5664 */
5665void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5666 unsigned long pos, unsigned long len)
5667{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005668 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005669 struct page *page;
5670 unsigned long i;
5671 size_t offset;
5672 const unsigned int size = pos + len;
5673 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005674 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005675
5676 eb_bitmap_offset(eb, start, pos, &i, &offset);
5677 page = eb->pages[i];
5678 WARN_ON(!PageUptodate(page));
5679 kaddr = page_address(page);
5680
5681 while (len >= bits_to_set) {
5682 kaddr[offset] |= mask_to_set;
5683 len -= bits_to_set;
5684 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005685 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005686 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005687 offset = 0;
5688 page = eb->pages[++i];
5689 WARN_ON(!PageUptodate(page));
5690 kaddr = page_address(page);
5691 }
5692 }
5693 if (len) {
5694 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5695 kaddr[offset] |= mask_to_set;
5696 }
5697}
5698
5699
5700/**
5701 * extent_buffer_bitmap_clear - clear an area of a bitmap
5702 * @eb: the extent buffer
5703 * @start: offset of the bitmap item in the extent buffer
5704 * @pos: bit number of the first bit
5705 * @len: number of bits to clear
5706 */
5707void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5708 unsigned long pos, unsigned long len)
5709{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005710 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005711 struct page *page;
5712 unsigned long i;
5713 size_t offset;
5714 const unsigned int size = pos + len;
5715 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005716 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005717
5718 eb_bitmap_offset(eb, start, pos, &i, &offset);
5719 page = eb->pages[i];
5720 WARN_ON(!PageUptodate(page));
5721 kaddr = page_address(page);
5722
5723 while (len >= bits_to_clear) {
5724 kaddr[offset] &= ~mask_to_clear;
5725 len -= bits_to_clear;
5726 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005727 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005728 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005729 offset = 0;
5730 page = eb->pages[++i];
5731 WARN_ON(!PageUptodate(page));
5732 kaddr = page_address(page);
5733 }
5734 }
5735 if (len) {
5736 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5737 kaddr[offset] &= ~mask_to_clear;
5738 }
5739}
5740
Sergei Trofimovich33872062011-04-11 21:52:52 +00005741static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5742{
5743 unsigned long distance = (src > dst) ? src - dst : dst - src;
5744 return distance < len;
5745}
5746
Chris Masond1310b22008-01-24 16:13:08 -05005747static void copy_pages(struct page *dst_page, struct page *src_page,
5748 unsigned long dst_off, unsigned long src_off,
5749 unsigned long len)
5750{
Chris Masona6591712011-07-19 12:04:14 -04005751 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005752 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005753 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005754
Sergei Trofimovich33872062011-04-11 21:52:52 +00005755 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005756 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005757 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005758 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005759 if (areas_overlap(src_off, dst_off, len))
5760 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005761 }
Chris Masond1310b22008-01-24 16:13:08 -05005762
Chris Mason727011e2010-08-06 13:21:20 -04005763 if (must_memmove)
5764 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5765 else
5766 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005767}
5768
5769void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5770 unsigned long src_offset, unsigned long len)
5771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005772 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005773 size_t cur;
5774 size_t dst_off_in_page;
5775 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005776 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005777 unsigned long dst_i;
5778 unsigned long src_i;
5779
5780 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005781 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005782 "memmove bogus src_offset %lu move len %lu dst len %lu",
5783 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005784 BUG_ON(1);
5785 }
5786 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005787 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005788 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5789 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005790 BUG_ON(1);
5791 }
5792
Chris Masond3977122009-01-05 21:25:51 -05005793 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005794 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005795 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005796 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005797 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005798
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005799 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5800 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005801
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005802 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005803 src_off_in_page));
5804 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005805 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005806
David Sterbafb85fc92014-07-31 01:03:53 +02005807 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005808 dst_off_in_page, src_off_in_page, cur);
5809
5810 src_offset += cur;
5811 dst_offset += cur;
5812 len -= cur;
5813 }
5814}
Chris Masond1310b22008-01-24 16:13:08 -05005815
5816void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5817 unsigned long src_offset, unsigned long len)
5818{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005819 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005820 size_t cur;
5821 size_t dst_off_in_page;
5822 size_t src_off_in_page;
5823 unsigned long dst_end = dst_offset + len - 1;
5824 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005825 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005826 unsigned long dst_i;
5827 unsigned long src_i;
5828
5829 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005830 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005831 "memmove bogus src_offset %lu move len %lu len %lu",
5832 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005833 BUG_ON(1);
5834 }
5835 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005836 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005837 "memmove bogus dst_offset %lu move len %lu len %lu",
5838 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005839 BUG_ON(1);
5840 }
Chris Mason727011e2010-08-06 13:21:20 -04005841 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005842 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5843 return;
5844 }
Chris Masond3977122009-01-05 21:25:51 -05005845 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005846 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5847 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005848
5849 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005850 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005851 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005852 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005853
5854 cur = min_t(unsigned long, len, src_off_in_page + 1);
5855 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005856 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005857 dst_off_in_page - cur + 1,
5858 src_off_in_page - cur + 1, cur);
5859
5860 dst_end -= cur;
5861 src_end -= cur;
5862 len -= cur;
5863 }
5864}
Chris Mason6af118ce2008-07-22 11:18:07 -04005865
David Sterbaf7a52a42013-04-26 14:56:29 +00005866int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005867{
Chris Mason6af118ce2008-07-22 11:18:07 -04005868 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005869
Miao Xie19fe0a82010-10-26 20:57:29 -04005870 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005871 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005872 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005873 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005874 spin_lock(&page->mapping->private_lock);
5875 if (!PagePrivate(page)) {
5876 spin_unlock(&page->mapping->private_lock);
5877 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005878 }
5879
Josef Bacik3083ee22012-03-09 16:01:49 -05005880 eb = (struct extent_buffer *)page->private;
5881 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005882
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005883 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005884 * This is a little awful but should be ok, we need to make sure that
5885 * the eb doesn't disappear out from under us while we're looking at
5886 * this page.
5887 */
5888 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005889 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005890 spin_unlock(&eb->refs_lock);
5891 spin_unlock(&page->mapping->private_lock);
5892 return 0;
5893 }
5894 spin_unlock(&page->mapping->private_lock);
5895
Josef Bacik3083ee22012-03-09 16:01:49 -05005896 /*
5897 * If tree ref isn't set then we know the ref on this eb is a real ref,
5898 * so just return, this page will likely be freed soon anyway.
5899 */
5900 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5901 spin_unlock(&eb->refs_lock);
5902 return 0;
5903 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005904
David Sterbaf7a52a42013-04-26 14:56:29 +00005905 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005906}