blob: 9359c1b7ccb8e214b2612a8afe4a8bb0fcc56fb5 [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;
147 ret = ulist_add(changeset->range_changed, state->start, state->end,
148 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 Sterbab5a4ba12016-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
Jeff Mahoney143bede2012-03-01 14:56:26 +01001557static noinline void __unlock_for_delalloc(struct inode *inode,
1558 struct page *locked_page,
1559 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001560{
1561 int ret;
1562 struct page *pages[16];
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001563 unsigned long index = start >> PAGE_SHIFT;
1564 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001565 unsigned long nr_pages = end_index - index + 1;
1566 int i;
1567
1568 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001569 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001570
Chris Masond3977122009-01-05 21:25:51 -05001571 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001572 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001573 min_t(unsigned long, nr_pages,
1574 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001575 for (i = 0; i < ret; i++) {
1576 if (pages[i] != locked_page)
1577 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001578 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001579 }
1580 nr_pages -= ret;
1581 index += ret;
1582 cond_resched();
1583 }
Chris Masonc8b97812008-10-29 14:49:59 -04001584}
1585
1586static noinline int lock_delalloc_pages(struct inode *inode,
1587 struct page *locked_page,
1588 u64 delalloc_start,
1589 u64 delalloc_end)
1590{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001591 unsigned long index = delalloc_start >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001592 unsigned long start_index = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001593 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001594 unsigned long pages_locked = 0;
1595 struct page *pages[16];
1596 unsigned long nrpages;
1597 int ret;
1598 int i;
1599
1600 /* the caller is responsible for locking the start index */
1601 if (index == locked_page->index && index == end_index)
1602 return 0;
1603
1604 /* skip the page at the start index */
1605 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001606 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001607 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001608 min_t(unsigned long,
1609 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001610 if (ret == 0) {
1611 ret = -EAGAIN;
1612 goto done;
1613 }
1614 /* now we have an array of pages, lock them all */
1615 for (i = 0; i < ret; i++) {
1616 /*
1617 * the caller is taking responsibility for
1618 * locked_page
1619 */
Chris Mason771ed682008-11-06 22:02:51 -05001620 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001621 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001622 if (!PageDirty(pages[i]) ||
1623 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001624 ret = -EAGAIN;
1625 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001626 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001627 goto done;
1628 }
1629 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001631 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001632 }
Chris Masonc8b97812008-10-29 14:49:59 -04001633 nrpages -= ret;
1634 index += ret;
1635 cond_resched();
1636 }
1637 ret = 0;
1638done:
1639 if (ret && pages_locked) {
1640 __unlock_for_delalloc(inode, locked_page,
1641 delalloc_start,
1642 ((u64)(start_index + pages_locked - 1)) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001643 PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001644 }
1645 return ret;
1646}
1647
1648/*
1649 * find a contiguous range of bytes in the file marked as delalloc, not
1650 * more than 'max_bytes'. start and end are used to return the range,
1651 *
1652 * 1 is returned if we find something, 0 if nothing was in the tree
1653 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001654STATIC u64 find_lock_delalloc_range(struct inode *inode,
1655 struct extent_io_tree *tree,
1656 struct page *locked_page, u64 *start,
1657 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001658{
1659 u64 delalloc_start;
1660 u64 delalloc_end;
1661 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001662 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001663 int ret;
1664 int loops = 0;
1665
1666again:
1667 /* step one, find a bunch of delalloc bytes starting at start */
1668 delalloc_start = *start;
1669 delalloc_end = 0;
1670 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001671 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001672 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001673 *start = delalloc_start;
1674 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001675 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001676 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001677 }
1678
1679 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001680 * start comes from the offset of locked_page. We have to lock
1681 * pages in order, so we can't process delalloc bytes before
1682 * locked_page
1683 */
Chris Masond3977122009-01-05 21:25:51 -05001684 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001685 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001686
1687 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001688 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001689 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001690 if (delalloc_end + 1 - delalloc_start > max_bytes)
1691 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001692
Chris Masonc8b97812008-10-29 14:49:59 -04001693 /* step two, lock all the pages after the page that has start */
1694 ret = lock_delalloc_pages(inode, locked_page,
1695 delalloc_start, delalloc_end);
1696 if (ret == -EAGAIN) {
1697 /* some of the pages are gone, lets avoid looping by
1698 * shortening the size of the delalloc range we're searching
1699 */
Chris Mason9655d292009-09-02 15:22:30 -04001700 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001701 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001702 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001703 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001704 loops = 1;
1705 goto again;
1706 } else {
1707 found = 0;
1708 goto out_failed;
1709 }
1710 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001711 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001712
1713 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001714 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001715
1716 /* then test to make sure it is all still delalloc */
1717 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001718 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001719 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001720 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1721 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001722 __unlock_for_delalloc(inode, locked_page,
1723 delalloc_start, delalloc_end);
1724 cond_resched();
1725 goto again;
1726 }
Chris Mason9655d292009-09-02 15:22:30 -04001727 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001728 *start = delalloc_start;
1729 *end = delalloc_end;
1730out_failed:
1731 return found;
1732}
1733
David Sterbaa9d93e12015-12-03 13:08:59 +01001734void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001735 u64 delalloc_end, struct page *locked_page,
David Sterba9ee49a042015-01-14 19:52:13 +01001736 unsigned clear_bits,
Josef Bacikc2790a22013-07-29 11:20:47 -04001737 unsigned long page_ops)
Chris Masonc8b97812008-10-29 14:49:59 -04001738{
Josef Bacikc2790a22013-07-29 11:20:47 -04001739 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Masonc8b97812008-10-29 14:49:59 -04001740 int ret;
1741 struct page *pages[16];
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001742 unsigned long index = start >> PAGE_SHIFT;
1743 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001744 unsigned long nr_pages = end_index - index + 1;
1745 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001746
Chris Mason2c64c532009-09-02 15:04:12 -04001747 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacikc2790a22013-07-29 11:20:47 -04001748 if (page_ops == 0)
David Sterbaa9d93e12015-12-03 13:08:59 +01001749 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001750
Filipe Manana704de492014-10-06 22:14:22 +01001751 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1752 mapping_set_error(inode->i_mapping, -EIO);
1753
Chris Masond3977122009-01-05 21:25:51 -05001754 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001755 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001756 min_t(unsigned long,
1757 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001758 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001759
Josef Bacikc2790a22013-07-29 11:20:47 -04001760 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001761 SetPagePrivate2(pages[i]);
1762
Chris Masonc8b97812008-10-29 14:49:59 -04001763 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001764 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001765 continue;
1766 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001767 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001768 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001769 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001770 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001771 if (page_ops & PAGE_SET_ERROR)
1772 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001773 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001774 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001775 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001776 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001777 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001778 }
1779 nr_pages -= ret;
1780 index += ret;
1781 cond_resched();
1782 }
Chris Masonc8b97812008-10-29 14:49:59 -04001783}
Chris Masonc8b97812008-10-29 14:49:59 -04001784
Chris Masond352ac62008-09-29 15:18:18 -04001785/*
1786 * count the number of bytes in the tree that have a given bit(s)
1787 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1788 * cached. The total number found is returned.
1789 */
Chris Masond1310b22008-01-24 16:13:08 -05001790u64 count_range_bits(struct extent_io_tree *tree,
1791 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001792 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001793{
1794 struct rb_node *node;
1795 struct extent_state *state;
1796 u64 cur_start = *start;
1797 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001798 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001799 int found = 0;
1800
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301801 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001802 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001803
Chris Masoncad321a2008-12-17 14:51:42 -05001804 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001805 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1806 total_bytes = tree->dirty_bytes;
1807 goto out;
1808 }
1809 /*
1810 * this search will find all the extents that end after
1811 * our range starts.
1812 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001813 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001814 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001815 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001816
Chris Masond3977122009-01-05 21:25:51 -05001817 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001818 state = rb_entry(node, struct extent_state, rb_node);
1819 if (state->start > search_end)
1820 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001821 if (contig && found && state->start > last + 1)
1822 break;
1823 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001824 total_bytes += min(search_end, state->end) + 1 -
1825 max(cur_start, state->start);
1826 if (total_bytes >= max_bytes)
1827 break;
1828 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001829 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001830 found = 1;
1831 }
Chris Masonec29ed52011-02-23 16:23:20 -05001832 last = state->end;
1833 } else if (contig && found) {
1834 break;
Chris Masond1310b22008-01-24 16:13:08 -05001835 }
1836 node = rb_next(node);
1837 if (!node)
1838 break;
1839 }
1840out:
Chris Masoncad321a2008-12-17 14:51:42 -05001841 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001842 return total_bytes;
1843}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001844
Chris Masond352ac62008-09-29 15:18:18 -04001845/*
1846 * set the private field for a given byte offset in the tree. If there isn't
1847 * an extent_state there already, this does nothing.
1848 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001849static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001850 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001851{
1852 struct rb_node *node;
1853 struct extent_state *state;
1854 int ret = 0;
1855
Chris Masoncad321a2008-12-17 14:51:42 -05001856 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001857 /*
1858 * this search will find all the extents that end after
1859 * our range starts.
1860 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001861 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001862 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001863 ret = -ENOENT;
1864 goto out;
1865 }
1866 state = rb_entry(node, struct extent_state, rb_node);
1867 if (state->start != start) {
1868 ret = -ENOENT;
1869 goto out;
1870 }
David Sterba47dc1962016-02-11 13:24:13 +01001871 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001872out:
Chris Masoncad321a2008-12-17 14:51:42 -05001873 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001874 return ret;
1875}
1876
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001877static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001878 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001879{
1880 struct rb_node *node;
1881 struct extent_state *state;
1882 int ret = 0;
1883
Chris Masoncad321a2008-12-17 14:51:42 -05001884 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001885 /*
1886 * this search will find all the extents that end after
1887 * our range starts.
1888 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001889 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001890 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001891 ret = -ENOENT;
1892 goto out;
1893 }
1894 state = rb_entry(node, struct extent_state, rb_node);
1895 if (state->start != start) {
1896 ret = -ENOENT;
1897 goto out;
1898 }
David Sterba47dc1962016-02-11 13:24:13 +01001899 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001900out:
Chris Masoncad321a2008-12-17 14:51:42 -05001901 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001902 return ret;
1903}
1904
1905/*
1906 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001907 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001908 * has the bits set. Otherwise, 1 is returned if any bit in the
1909 * range is found set.
1910 */
1911int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001912 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001913{
1914 struct extent_state *state = NULL;
1915 struct rb_node *node;
1916 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001917
Chris Masoncad321a2008-12-17 14:51:42 -05001918 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001919 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001920 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001921 node = &cached->rb_node;
1922 else
1923 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001924 while (node && start <= end) {
1925 state = rb_entry(node, struct extent_state, rb_node);
1926
1927 if (filled && state->start > start) {
1928 bitset = 0;
1929 break;
1930 }
1931
1932 if (state->start > end)
1933 break;
1934
1935 if (state->state & bits) {
1936 bitset = 1;
1937 if (!filled)
1938 break;
1939 } else if (filled) {
1940 bitset = 0;
1941 break;
1942 }
Chris Mason46562ce2009-09-23 20:23:16 -04001943
1944 if (state->end == (u64)-1)
1945 break;
1946
Chris Masond1310b22008-01-24 16:13:08 -05001947 start = state->end + 1;
1948 if (start > end)
1949 break;
1950 node = rb_next(node);
1951 if (!node) {
1952 if (filled)
1953 bitset = 0;
1954 break;
1955 }
1956 }
Chris Masoncad321a2008-12-17 14:51:42 -05001957 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001958 return bitset;
1959}
Chris Masond1310b22008-01-24 16:13:08 -05001960
1961/*
1962 * helper function to set a given page up to date if all the
1963 * extents in the tree for that page are up to date
1964 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001965static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001966{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001967 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001968 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001969 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001970 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001971}
1972
Miao Xie8b110e32014-09-12 18:44:03 +08001973int free_io_failure(struct inode *inode, struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974{
1975 int ret;
1976 int err = 0;
1977 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1978
David Sterba47dc1962016-02-11 13:24:13 +01001979 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980 ret = clear_extent_bits(failure_tree, rec->start,
1981 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001982 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001983 if (ret)
1984 err = ret;
1985
David Woodhouse53b381b2013-01-29 18:40:14 -05001986 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1987 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001988 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001989 if (ret && !err)
1990 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991
1992 kfree(rec);
1993 return err;
1994}
1995
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001996/*
1997 * this bypasses the standard btrfs submit functions deliberately, as
1998 * the standard behavior is to write all copies in a raid setup. here we only
1999 * want to write the one bad copy. so we do the mapping for ourselves and issue
2000 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002001 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002002 * actually prevents the read that triggered the error from finishing.
2003 * currently, there can be no more than two copies of every data bit. thus,
2004 * exactly one rewrite is required.
2005 */
Miao Xie1203b682014-09-12 18:44:01 +08002006int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
2007 struct page *page, unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002008{
Miao Xie1203b682014-09-12 18:44:01 +08002009 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002010 struct bio *bio;
2011 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002012 u64 map_length = 0;
2013 u64 sector;
2014 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002015 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002016 int ret;
2017
Ilya Dryomov908960c2013-11-03 19:06:39 +02002018 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002019 BUG_ON(!mirror_num);
2020
David Woodhouse53b381b2013-01-29 18:40:14 -05002021 /* we can't repair anything in raid56 yet */
2022 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2023 return 0;
2024
Chris Mason9be33952013-05-17 18:30:14 -04002025 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002026 if (!bio)
2027 return -EIO;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002028 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002029 map_length = length;
2030
Filipe Mananab5de8d02016-05-27 22:21:27 +01002031 /*
2032 * Avoid races with device replace and make sure our bbio has devices
2033 * associated to its stripes that don't go away while we are doing the
2034 * read repair operation.
2035 */
2036 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002037 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002038 &map_length, &bbio, mirror_num);
2039 if (ret) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002040 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002041 bio_put(bio);
2042 return -EIO;
2043 }
2044 BUG_ON(mirror_num != bbio->mirror_num);
2045 sector = bbio->stripes[mirror_num-1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002046 bio->bi_iter.bi_sector = sector;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002047 dev = bbio->stripes[mirror_num-1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002048 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002049 if (!dev || !dev->bdev || !dev->writeable) {
Filipe Mananab5de8d02016-05-27 22:21:27 +01002050 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051 bio_put(bio);
2052 return -EIO;
2053 }
2054 bio->bi_bdev = dev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002055 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Miao Xieffdd2012014-09-12 18:44:00 +08002056 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002057
Mike Christie4e49ea42016-06-05 14:31:41 -05002058 if (btrfsic_submit_bio_wait(bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 /* try to remap that extent elsewhere? */
Filipe Mananab5de8d02016-05-27 22:21:27 +01002060 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002061 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002062 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002063 return -EIO;
2064 }
2065
David Sterbab14af3b2015-10-08 10:43:10 +02002066 btrfs_info_rl_in_rcu(fs_info,
2067 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002068 btrfs_ino(BTRFS_I(inode)), start,
Miao Xie1203b682014-09-12 18:44:01 +08002069 rcu_str_deref(dev->name), sector);
Filipe Mananab5de8d02016-05-27 22:21:27 +01002070 btrfs_bio_counter_dec(fs_info);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002071 bio_put(bio);
2072 return 0;
2073}
2074
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002075int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2076 struct extent_buffer *eb, int mirror_num)
Josef Bacikea466792012-03-26 21:57:36 -04002077{
Josef Bacikea466792012-03-26 21:57:36 -04002078 u64 start = eb->start;
2079 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002080 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002081
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002082 if (fs_info->sb->s_flags & MS_RDONLY)
Ilya Dryomov908960c2013-11-03 19:06:39 +02002083 return -EROFS;
2084
Josef Bacikea466792012-03-26 21:57:36 -04002085 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002086 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002087
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002088 ret = repair_io_failure(fs_info->btree_inode, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002089 PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002090 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002091 if (ret)
2092 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002093 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002094 }
2095
2096 return ret;
2097}
2098
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002099/*
2100 * each time an IO finishes, we do a fast check in the IO failure tree
2101 * to see if we need to process or clean up an io_failure_record
2102 */
Miao Xie8b110e32014-09-12 18:44:03 +08002103int clean_io_failure(struct inode *inode, u64 start, struct page *page,
2104 unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105{
2106 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002107 struct io_failure_record *failrec;
Ilya Dryomov908960c2013-11-03 19:06:39 +02002108 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002109 struct extent_state *state;
2110 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002111 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002112
2113 private = 0;
2114 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2115 (u64)-1, 1, EXTENT_DIRTY, 0);
2116 if (!ret)
2117 return 0;
2118
David Sterba47dc1962016-02-11 13:24:13 +01002119 ret = get_state_failrec(&BTRFS_I(inode)->io_failure_tree, start,
2120 &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002121 if (ret)
2122 return 0;
2123
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002124 BUG_ON(!failrec->this_mirror);
2125
2126 if (failrec->in_validation) {
2127 /* there was no real error, just free the record */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002128 btrfs_debug(fs_info,
2129 "clean_io_failure: freeing dummy error at %llu",
2130 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002131 goto out;
2132 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002133 if (fs_info->sb->s_flags & MS_RDONLY)
2134 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002135
2136 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2137 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2138 failrec->start,
2139 EXTENT_LOCKED);
2140 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2141
Miao Xie883d0de2013-07-25 19:22:35 +08002142 if (state && state->start <= failrec->start &&
2143 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002144 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2145 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002146 if (num_copies > 1) {
Miao Xie1203b682014-09-12 18:44:01 +08002147 repair_io_failure(inode, start, failrec->len,
Miao Xie454ff3d2014-09-12 18:43:58 +08002148 failrec->logical, page,
Miao Xie1203b682014-09-12 18:44:01 +08002149 pg_offset, failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002150 }
2151 }
2152
2153out:
Miao Xie454ff3d2014-09-12 18:43:58 +08002154 free_io_failure(inode, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002155
Miao Xie454ff3d2014-09-12 18:43:58 +08002156 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002157}
2158
Miao Xief6124962014-09-12 18:44:04 +08002159/*
2160 * Can be called when
2161 * - hold extent lock
2162 * - under ordered extent
2163 * - the inode is freeing
2164 */
2165void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
2166{
2167 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2168 struct io_failure_record *failrec;
2169 struct extent_state *state, *next;
2170
2171 if (RB_EMPTY_ROOT(&failure_tree->state))
2172 return;
2173
2174 spin_lock(&failure_tree->lock);
2175 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2176 while (state) {
2177 if (state->start > end)
2178 break;
2179
2180 ASSERT(state->end <= end);
2181
2182 next = next_state(state);
2183
David Sterba47dc1962016-02-11 13:24:13 +01002184 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002185 free_extent_state(state);
2186 kfree(failrec);
2187
2188 state = next;
2189 }
2190 spin_unlock(&failure_tree->lock);
2191}
2192
Miao Xie2fe63032014-09-12 18:43:59 +08002193int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002194 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002195{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002196 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002197 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002198 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002199 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2200 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2201 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002202 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002203 u64 logical;
2204
David Sterba47dc1962016-02-11 13:24:13 +01002205 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002206 if (ret) {
2207 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2208 if (!failrec)
2209 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002210
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002211 failrec->start = start;
2212 failrec->len = end - start + 1;
2213 failrec->this_mirror = 0;
2214 failrec->bio_flags = 0;
2215 failrec->in_validation = 0;
2216
2217 read_lock(&em_tree->lock);
2218 em = lookup_extent_mapping(em_tree, start, failrec->len);
2219 if (!em) {
2220 read_unlock(&em_tree->lock);
2221 kfree(failrec);
2222 return -EIO;
2223 }
2224
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002225 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002226 free_extent_map(em);
2227 em = NULL;
2228 }
2229 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002230 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002231 kfree(failrec);
2232 return -EIO;
2233 }
Miao Xie2fe63032014-09-12 18:43:59 +08002234
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002235 logical = start - em->start;
2236 logical = em->block_start + logical;
2237 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2238 logical = em->block_start;
2239 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2240 extent_set_compress_type(&failrec->bio_flags,
2241 em->compress_type);
2242 }
Miao Xie2fe63032014-09-12 18:43:59 +08002243
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002244 btrfs_debug(fs_info,
2245 "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
2246 logical, start, failrec->len);
Miao Xie2fe63032014-09-12 18:43:59 +08002247
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002248 failrec->logical = logical;
2249 free_extent_map(em);
2250
2251 /* set the bits in the private failure tree */
2252 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002253 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002255 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002256 /* set the bits in the inode's tree */
2257 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002258 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002259 if (ret < 0) {
2260 kfree(failrec);
2261 return ret;
2262 }
2263 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002264 btrfs_debug(fs_info,
2265 "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
2266 failrec->logical, failrec->start, failrec->len,
2267 failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002268 /*
2269 * when data can be on disk more than twice, add to failrec here
2270 * (e.g. with a list for failed_mirror) to make
2271 * clean_io_failure() clean all those errors at once.
2272 */
2273 }
Miao Xie2fe63032014-09-12 18:43:59 +08002274
2275 *failrec_ret = failrec;
2276
2277 return 0;
2278}
2279
2280int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
2281 struct io_failure_record *failrec, int failed_mirror)
2282{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002284 int num_copies;
2285
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002286 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002287 if (num_copies == 1) {
2288 /*
2289 * we only have a single copy of the data, so don't bother with
2290 * all the retry and error correction code that follows. no
2291 * matter what the error is, it is very likely to persist.
2292 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002293 btrfs_debug(fs_info,
2294 "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
2295 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002296 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002297 }
2298
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002299 /*
2300 * there are two premises:
2301 * a) deliver good data to the caller
2302 * b) correct the bad sectors on disk
2303 */
2304 if (failed_bio->bi_vcnt > 1) {
2305 /*
2306 * to fulfill b), we need to know the exact failing sectors, as
2307 * we don't want to rewrite any more than the failed ones. thus,
2308 * we need separate read requests for the failed bio
2309 *
2310 * if the following BUG_ON triggers, our validation request got
2311 * merged. we need separate requests for our algorithm to work.
2312 */
2313 BUG_ON(failrec->in_validation);
2314 failrec->in_validation = 1;
2315 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002316 } else {
2317 /*
2318 * we're ready to fulfill a) and b) alongside. get a good copy
2319 * of the failed sector and if we succeed, we have setup
2320 * everything for repair_io_failure to do the rest for us.
2321 */
2322 if (failrec->in_validation) {
2323 BUG_ON(failrec->this_mirror != failed_mirror);
2324 failrec->in_validation = 0;
2325 failrec->this_mirror = 0;
2326 }
2327 failrec->failed_mirror = failed_mirror;
2328 failrec->this_mirror++;
2329 if (failrec->this_mirror == failed_mirror)
2330 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002331 }
2332
Miao Xiefacc8a222013-07-25 19:22:34 +08002333 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002334 btrfs_debug(fs_info,
2335 "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
2336 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002337 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002338 }
2339
Miao Xie2fe63032014-09-12 18:43:59 +08002340 return 1;
2341}
2342
2343
2344struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2345 struct io_failure_record *failrec,
2346 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002347 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002348{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002349 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie2fe63032014-09-12 18:43:59 +08002350 struct bio *bio;
2351 struct btrfs_io_bio *btrfs_failed_bio;
2352 struct btrfs_io_bio *btrfs_bio;
2353
Chris Mason9be33952013-05-17 18:30:14 -04002354 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Miao Xie2fe63032014-09-12 18:43:59 +08002355 if (!bio)
2356 return NULL;
2357
2358 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002359 bio->bi_iter.bi_sector = failrec->logical >> 9;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002360 bio->bi_bdev = fs_info->fs_devices->latest_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002361 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002362 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002363
Miao Xiefacc8a222013-07-25 19:22:34 +08002364 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2365 if (btrfs_failed_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002366 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2367
2368 btrfs_bio = btrfs_io_bio(bio);
2369 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002370 icsum *= csum_size;
2371 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002372 csum_size);
2373 }
2374
Miao Xie2fe63032014-09-12 18:43:59 +08002375 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002376
Miao Xie2fe63032014-09-12 18:43:59 +08002377 return bio;
2378}
2379
2380/*
2381 * this is a generic handler for readpage errors (default
2382 * readpage_io_failed_hook). if other copies exist, read those and write back
2383 * good data to the failed position. does not investigate in remapping the
2384 * failed extent elsewhere, hoping the device will be smart enough to do this as
2385 * needed
2386 */
2387
2388static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2389 struct page *page, u64 start, u64 end,
2390 int failed_mirror)
2391{
2392 struct io_failure_record *failrec;
2393 struct inode *inode = page->mapping->host;
2394 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2395 struct bio *bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002396 int read_mode = 0;
Miao Xie2fe63032014-09-12 18:43:59 +08002397 int ret;
2398
Mike Christie1f7ad752016-06-05 14:31:51 -05002399 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie2fe63032014-09-12 18:43:59 +08002400
2401 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2402 if (ret)
2403 return ret;
2404
2405 ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
2406 if (!ret) {
2407 free_io_failure(inode, failrec);
2408 return -EIO;
2409 }
2410
2411 if (failed_bio->bi_vcnt > 1)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06002412 read_mode |= REQ_FAILFAST_DEV;
Miao Xie2fe63032014-09-12 18:43:59 +08002413
2414 phy_offset >>= inode->i_sb->s_blocksize_bits;
2415 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2416 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002417 (int)phy_offset, failed_bio->bi_end_io,
2418 NULL);
Miao Xie2fe63032014-09-12 18:43:59 +08002419 if (!bio) {
2420 free_io_failure(inode, failrec);
2421 return -EIO;
2422 }
Mike Christie1f7ad752016-06-05 14:31:51 -05002423 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie2fe63032014-09-12 18:43:59 +08002424
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002425 btrfs_debug(btrfs_sb(inode->i_sb),
2426 "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
2427 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002428
Mike Christie81a75f672016-06-05 14:31:54 -05002429 ret = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror,
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002430 failrec->bio_flags, 0);
Miao Xie6c387ab2014-09-12 18:43:57 +08002431 if (ret) {
Miao Xie454ff3d2014-09-12 18:43:58 +08002432 free_io_failure(inode, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002433 bio_put(bio);
2434 }
2435
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002436 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002437}
2438
Chris Masond1310b22008-01-24 16:13:08 -05002439/* lots and lots of room for performance fixes in the end_bio funcs */
2440
David Sterbab5227c02015-12-03 13:08:59 +01002441void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002442{
2443 int uptodate = (err == 0);
2444 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002445 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002446
2447 tree = &BTRFS_I(page->mapping->host)->io_tree;
2448
2449 if (tree->ops && tree->ops->writepage_end_io_hook) {
2450 ret = tree->ops->writepage_end_io_hook(page, start,
2451 end, NULL, uptodate);
2452 if (ret)
2453 uptodate = 0;
2454 }
2455
Jeff Mahoney87826df2012-02-15 16:23:57 +01002456 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002457 ClearPageUptodate(page);
2458 SetPageError(page);
Liu Bo5dca6ee2014-05-12 12:47:36 +08002459 ret = ret < 0 ? ret : -EIO;
2460 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002461 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002462}
2463
Chris Masond1310b22008-01-24 16:13:08 -05002464/*
2465 * after a writepage IO is done, we need to:
2466 * clear the uptodate bits on error
2467 * clear the writeback bits in the extent tree for this IO
2468 * end_page_writeback if the page has no more pending IO
2469 *
2470 * Scheduling is not allowed, so the extent state tree is expected
2471 * to have one and only one object corresponding to this IO.
2472 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002473static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002474{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002475 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002476 u64 start;
2477 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002478 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002479
Kent Overstreet2c30c712013-11-07 12:20:26 -08002480 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002481 struct page *page = bvec->bv_page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002482 struct inode *inode = page->mapping->host;
2483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Woodhouse902b22f2008-08-20 08:51:49 -04002484
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002485 /* We always issue full-page reads, but if some block
2486 * in a page fails to read, blk_update_request() will
2487 * advance bv_offset and adjust bv_len to compensate.
2488 * Print a warning for nonzero offsets, and an error
2489 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002490 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2491 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002492 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002493 "partial page write in btrfs with offset %u and length %u",
2494 bvec->bv_offset, bvec->bv_len);
2495 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002496 btrfs_info(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002497 "incomplete page write in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002498 bvec->bv_offset, bvec->bv_len);
2499 }
Chris Masond1310b22008-01-24 16:13:08 -05002500
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002501 start = page_offset(page);
2502 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002503
David Sterbab5227c02015-12-03 13:08:59 +01002504 end_extent_writepage(page, bio->bi_error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002505 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002506 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002507
Chris Masond1310b22008-01-24 16:13:08 -05002508 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002509}
2510
Miao Xie883d0de2013-07-25 19:22:35 +08002511static void
2512endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2513 int uptodate)
2514{
2515 struct extent_state *cached = NULL;
2516 u64 end = start + len - 1;
2517
2518 if (uptodate && tree->track_uptodate)
2519 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2520 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2521}
2522
Chris Masond1310b22008-01-24 16:13:08 -05002523/*
2524 * after a readpage IO is done, we need to:
2525 * clear the uptodate bits on error
2526 * set the uptodate bits if things worked
2527 * set the page up to date if all extents in the tree are uptodate
2528 * clear the lock bit in the extent tree
2529 * unlock the page if there are no other extents locked for it
2530 *
2531 * Scheduling is not allowed, so the extent state tree is expected
2532 * to have one and only one object corresponding to this IO.
2533 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002534static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002535{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002536 struct bio_vec *bvec;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002537 int uptodate = !bio->bi_error;
Miao Xiefacc8a222013-07-25 19:22:34 +08002538 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002539 struct extent_io_tree *tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002540 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002541 u64 start;
2542 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002543 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002544 u64 extent_start = 0;
2545 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002546 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002547 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002548 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002549
Kent Overstreet2c30c712013-11-07 12:20:26 -08002550 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002551 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002552 struct inode *inode = page->mapping->host;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002553 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen507903b2011-04-06 10:02:20 +00002554
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002555 btrfs_debug(fs_info,
2556 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
2557 (u64)bio->bi_iter.bi_sector, bio->bi_error,
2558 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002559 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002560
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002561 /* We always issue full-page reads, but if some block
2562 * in a page fails to read, blk_update_request() will
2563 * advance bv_offset and adjust bv_len to compensate.
2564 * Print a warning for nonzero offsets, and an error
2565 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002566 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2567 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002568 btrfs_err(fs_info,
2569 "partial page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002570 bvec->bv_offset, bvec->bv_len);
2571 else
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002572 btrfs_info(fs_info,
2573 "incomplete page read in btrfs with offset %u and length %u",
Frank Holtonefe120a2013-12-20 11:37:06 -05002574 bvec->bv_offset, bvec->bv_len);
2575 }
Chris Masond1310b22008-01-24 16:13:08 -05002576
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002577 start = page_offset(page);
2578 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002579 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002580
Chris Mason9be33952013-05-17 18:30:14 -04002581 mirror = io_bio->mirror_num;
Miao Xief2a09da2013-07-25 19:22:33 +08002582 if (likely(uptodate && tree->ops &&
2583 tree->ops->readpage_end_io_hook)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002584 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2585 page, start, end,
2586 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002587 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002588 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002589 else
Miao Xie1203b682014-09-12 18:44:01 +08002590 clean_io_failure(inode, start, page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002591 }
Josef Bacikea466792012-03-26 21:57:36 -04002592
Miao Xief2a09da2013-07-25 19:22:33 +08002593 if (likely(uptodate))
2594 goto readpage_ok;
2595
2596 if (tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002597 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002598 if (!ret && !bio->bi_error)
Josef Bacikea466792012-03-26 21:57:36 -04002599 uptodate = 1;
Miao Xief2a09da2013-07-25 19:22:33 +08002600 } else {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002601 /*
2602 * The generic bio_readpage_error handles errors the
2603 * following way: If possible, new read requests are
2604 * created and submitted and will end up in
2605 * end_bio_extent_readpage as well (if we're lucky, not
2606 * in the !uptodate case). In that case it returns 0 and
2607 * we just go on with the next page in our bio. If it
2608 * can't handle the error it will return -EIO and we
2609 * remain responsible for that page.
2610 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002611 ret = bio_readpage_error(bio, offset, page, start, end,
2612 mirror);
Chris Mason7e383262008-04-09 16:28:12 -04002613 if (ret == 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002614 uptodate = !bio->bi_error;
Liu Bo38c1c2e2014-08-19 23:33:13 +08002615 offset += len;
Chris Mason7e383262008-04-09 16:28:12 -04002616 continue;
2617 }
2618 }
Miao Xief2a09da2013-07-25 19:22:33 +08002619readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002620 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002621 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002622 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002623 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002624
2625 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002626 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002627 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002628 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002629 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002630 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002631 ClearPageUptodate(page);
2632 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002633 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002634 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002635 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002636
2637 if (unlikely(!uptodate)) {
2638 if (extent_len) {
2639 endio_readpage_release_extent(tree,
2640 extent_start,
2641 extent_len, 1);
2642 extent_start = 0;
2643 extent_len = 0;
2644 }
2645 endio_readpage_release_extent(tree, start,
2646 end - start + 1, 0);
2647 } else if (!extent_len) {
2648 extent_start = start;
2649 extent_len = end + 1 - start;
2650 } else if (extent_start + extent_len == start) {
2651 extent_len += end + 1 - start;
2652 } else {
2653 endio_readpage_release_extent(tree, extent_start,
2654 extent_len, uptodate);
2655 extent_start = start;
2656 extent_len = end + 1 - start;
2657 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002658 }
Chris Masond1310b22008-01-24 16:13:08 -05002659
Miao Xie883d0de2013-07-25 19:22:35 +08002660 if (extent_len)
2661 endio_readpage_release_extent(tree, extent_start, extent_len,
2662 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002663 if (io_bio->end_io)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002664 io_bio->end_io(io_bio, bio->bi_error);
Chris Masond1310b22008-01-24 16:13:08 -05002665 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002666}
2667
Chris Mason9be33952013-05-17 18:30:14 -04002668/*
2669 * this allocates from the btrfs_bioset. We're returning a bio right now
2670 * but you can call btrfs_io_bio for the appropriate container_of magic
2671 */
Miao Xie88f794e2010-11-22 03:02:55 +00002672struct bio *
2673btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2674 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002675{
Miao Xiefacc8a222013-07-25 19:22:34 +08002676 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002677 struct bio *bio;
2678
Chris Mason9be33952013-05-17 18:30:14 -04002679 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002680
2681 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002682 while (!bio && (nr_vecs /= 2)) {
2683 bio = bio_alloc_bioset(gfp_flags,
2684 nr_vecs, btrfs_bioset);
2685 }
Chris Masond1310b22008-01-24 16:13:08 -05002686 }
2687
2688 if (bio) {
2689 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002690 bio->bi_iter.bi_sector = first_sector;
Miao Xiefacc8a222013-07-25 19:22:34 +08002691 btrfs_bio = btrfs_io_bio(bio);
2692 btrfs_bio->csum = NULL;
2693 btrfs_bio->csum_allocated = NULL;
2694 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002695 }
2696 return bio;
2697}
2698
Chris Mason9be33952013-05-17 18:30:14 -04002699struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2700{
Miao Xie23ea8e52014-09-12 18:43:54 +08002701 struct btrfs_io_bio *btrfs_bio;
2702 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002703
Miao Xie23ea8e52014-09-12 18:43:54 +08002704 new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2705 if (new) {
2706 btrfs_bio = btrfs_io_bio(new);
2707 btrfs_bio->csum = NULL;
2708 btrfs_bio->csum_allocated = NULL;
2709 btrfs_bio->end_io = NULL;
2710 }
2711 return new;
2712}
Chris Mason9be33952013-05-17 18:30:14 -04002713
2714/* this also allocates from the btrfs_bioset */
2715struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2716{
Miao Xiefacc8a222013-07-25 19:22:34 +08002717 struct btrfs_io_bio *btrfs_bio;
2718 struct bio *bio;
2719
2720 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2721 if (bio) {
2722 btrfs_bio = btrfs_io_bio(bio);
2723 btrfs_bio->csum = NULL;
2724 btrfs_bio->csum_allocated = NULL;
2725 btrfs_bio->end_io = NULL;
2726 }
2727 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002728}
2729
2730
Mike Christie1f7ad752016-06-05 14:31:51 -05002731static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
2732 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002733{
Chris Masond1310b22008-01-24 16:13:08 -05002734 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002735 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2736 struct page *page = bvec->bv_page;
2737 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002738 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002739
Miao Xie4eee4fa2012-12-21 09:17:45 +00002740 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002741
David Woodhouse902b22f2008-08-20 08:51:49 -04002742 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002743 bio_get(bio);
2744
Chris Mason065631f2008-02-20 12:07:25 -05002745 if (tree->ops && tree->ops->submit_bio_hook)
Mike Christie81a75f672016-06-05 14:31:54 -05002746 ret = tree->ops->submit_bio_hook(page->mapping->host, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002747 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002748 else
Mike Christie4e49ea42016-06-05 14:31:41 -05002749 btrfsic_submit_bio(bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002750
Chris Masond1310b22008-01-24 16:13:08 -05002751 bio_put(bio);
2752 return ret;
2753}
2754
Mike Christie1f7ad752016-06-05 14:31:51 -05002755static int merge_bio(struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002756 unsigned long offset, size_t size, struct bio *bio,
2757 unsigned long bio_flags)
2758{
2759 int ret = 0;
2760 if (tree->ops && tree->ops->merge_bio_hook)
Mike Christie81a75f672016-06-05 14:31:54 -05002761 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002762 bio_flags);
Jeff Mahoney3444a972011-10-03 23:23:13 -04002763 return ret;
2764
2765}
2766
Mike Christie1f7ad752016-06-05 14:31:51 -05002767static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002768 struct writeback_control *wbc,
Chris Masond1310b22008-01-24 16:13:08 -05002769 struct page *page, sector_t sector,
2770 size_t size, unsigned long offset,
2771 struct block_device *bdev,
2772 struct bio **bio_ret,
2773 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002774 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002775 int mirror_num,
2776 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002777 unsigned long bio_flags,
2778 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002779{
2780 int ret = 0;
2781 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002782 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002783 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002784 size_t page_size = min_t(size_t, size, PAGE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002785
2786 if (bio_ret && *bio_ret) {
2787 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002788 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002789 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002790 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002791 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002792
2793 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002794 force_bio_submit ||
Mike Christie1f7ad752016-06-05 14:31:51 -05002795 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002796 bio_add_page(bio, page, page_size, offset) < page_size) {
Mike Christie1f7ad752016-06-05 14:31:51 -05002797 ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002798 if (ret < 0) {
2799 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002800 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002801 }
Chris Masond1310b22008-01-24 16:13:08 -05002802 bio = NULL;
2803 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002804 if (wbc)
2805 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002806 return 0;
2807 }
2808 }
Chris Masonc8b97812008-10-29 14:49:59 -04002809
Kent Overstreetb54ffb72015-05-19 14:31:01 +02002810 bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
2811 GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002812 if (!bio)
2813 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002814
Chris Masonc8b97812008-10-29 14:49:59 -04002815 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002816 bio->bi_end_io = end_io_func;
2817 bio->bi_private = tree;
Mike Christie1f7ad752016-06-05 14:31:51 -05002818 bio_set_op_attrs(bio, op, op_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07002819 if (wbc) {
2820 wbc_init_bio(wbc, bio);
2821 wbc_account_io(wbc, page, page_size);
2822 }
Chris Mason70dec802008-01-29 09:59:12 -05002823
Chris Masond3977122009-01-05 21:25:51 -05002824 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002825 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002826 else
Mike Christie1f7ad752016-06-05 14:31:51 -05002827 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002828
2829 return ret;
2830}
2831
Eric Sandeen48a3b632013-04-25 20:41:01 +00002832static void attach_extent_buffer_page(struct extent_buffer *eb,
2833 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002834{
2835 if (!PagePrivate(page)) {
2836 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002837 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002838 set_page_private(page, (unsigned long)eb);
2839 } else {
2840 WARN_ON(page->private != (unsigned long)eb);
2841 }
2842}
2843
Chris Masond1310b22008-01-24 16:13:08 -05002844void set_page_extent_mapped(struct page *page)
2845{
2846 if (!PagePrivate(page)) {
2847 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002848 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002849 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002850 }
2851}
2852
Miao Xie125bac012013-07-25 19:22:37 +08002853static struct extent_map *
2854__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2855 u64 start, u64 len, get_extent_t *get_extent,
2856 struct extent_map **em_cached)
2857{
2858 struct extent_map *em;
2859
2860 if (em_cached && *em_cached) {
2861 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002862 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002863 start < extent_map_end(em)) {
2864 atomic_inc(&em->refs);
2865 return em;
2866 }
2867
2868 free_extent_map(em);
2869 *em_cached = NULL;
2870 }
2871
2872 em = get_extent(inode, page, pg_offset, start, len, 0);
2873 if (em_cached && !IS_ERR_OR_NULL(em)) {
2874 BUG_ON(*em_cached);
2875 atomic_inc(&em->refs);
2876 *em_cached = em;
2877 }
2878 return em;
2879}
Chris Masond1310b22008-01-24 16:13:08 -05002880/*
2881 * basic readpage implementation. Locked extent state structs are inserted
2882 * into the tree that are removed when the IO is done (by the end_io
2883 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002884 * XXX JDM: This needs looking at to ensure proper page locking
Liu Bobaf863b2016-07-11 10:39:07 -07002885 * return 0 on success, otherwise return error
Chris Masond1310b22008-01-24 16:13:08 -05002886 */
Miao Xie99740902013-07-25 19:22:36 +08002887static int __do_readpage(struct extent_io_tree *tree,
2888 struct page *page,
2889 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002890 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002891 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05002892 unsigned long *bio_flags, int read_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002893 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002894{
2895 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002896 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002897 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002898 u64 end;
2899 u64 cur = start;
2900 u64 extent_offset;
2901 u64 last_byte = i_size_read(inode);
2902 u64 block_start;
2903 u64 cur_end;
2904 sector_t sector;
2905 struct extent_map *em;
2906 struct block_device *bdev;
Liu Bobaf863b2016-07-11 10:39:07 -07002907 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002908 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002909 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002910 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002911 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002912 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002913 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002914
2915 set_page_extent_mapped(page);
2916
Miao Xie99740902013-07-25 19:22:36 +08002917 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002918 if (!PageUptodate(page)) {
2919 if (cleancache_get_page(page) == 0) {
2920 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002921 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002922 goto out;
2923 }
2924 }
2925
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002926 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002927 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002928 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002929
2930 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002931 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002932 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002933 memset(userpage + zero_offset, 0, iosize);
2934 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002935 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002936 }
2937 }
Chris Masond1310b22008-01-24 16:13:08 -05002938 while (cur <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002939 unsigned long pnr = (last_byte >> PAGE_SHIFT) + 1;
Filipe Manana005efed2015-09-14 09:09:31 +01002940 bool force_bio_submit = false;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002941
Chris Masond1310b22008-01-24 16:13:08 -05002942 if (cur >= last_byte) {
2943 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002944 struct extent_state *cached = NULL;
2945
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002946 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002947 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002948 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002949 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002950 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002951 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002952 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002953 unlock_extent_cached(tree, cur,
2954 cur + iosize - 1,
2955 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002956 break;
2957 }
Miao Xie125bac012013-07-25 19:22:37 +08002958 em = __get_extent_map(inode, page, pg_offset, cur,
2959 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002960 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002961 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002962 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002963 break;
2964 }
Chris Masond1310b22008-01-24 16:13:08 -05002965 extent_offset = cur - em->start;
2966 BUG_ON(extent_map_end(em) <= cur);
2967 BUG_ON(end < cur);
2968
Li Zefan261507a02010-12-17 14:21:50 +08002969 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002970 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002971 extent_set_compress_type(&this_bio_flag,
2972 em->compress_type);
2973 }
Chris Masonc8b97812008-10-29 14:49:59 -04002974
Chris Masond1310b22008-01-24 16:13:08 -05002975 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2976 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002977 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002978 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2979 disk_io_size = em->block_len;
2980 sector = em->block_start >> 9;
2981 } else {
2982 sector = (em->block_start + extent_offset) >> 9;
2983 disk_io_size = iosize;
2984 }
Chris Masond1310b22008-01-24 16:13:08 -05002985 bdev = em->bdev;
2986 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002987 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2988 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002989
2990 /*
2991 * If we have a file range that points to a compressed extent
2992 * and it's followed by a consecutive file range that points to
2993 * to the same compressed extent (possibly with a different
2994 * offset and/or length, so it either points to the whole extent
2995 * or only part of it), we must make sure we do not submit a
2996 * single bio to populate the pages for the 2 ranges because
2997 * this makes the compressed extent read zero out the pages
2998 * belonging to the 2nd range. Imagine the following scenario:
2999 *
3000 * File layout
3001 * [0 - 8K] [8K - 24K]
3002 * | |
3003 * | |
3004 * points to extent X, points to extent X,
3005 * offset 4K, length of 8K offset 0, length 16K
3006 *
3007 * [extent X, compressed length = 4K uncompressed length = 16K]
3008 *
3009 * If the bio to read the compressed extent covers both ranges,
3010 * it will decompress extent X into the pages belonging to the
3011 * first range and then it will stop, zeroing out the remaining
3012 * pages that belong to the other range that points to extent X.
3013 * So here we make sure we submit 2 bios, one for the first
3014 * range and another one for the third range. Both will target
3015 * the same physical extent from disk, but we can't currently
3016 * make the compressed bio endio callback populate the pages
3017 * for both ranges because each compressed bio is tightly
3018 * coupled with a single extent map, and each range can have
3019 * an extent map with a different offset value relative to the
3020 * uncompressed data of our extent and different lengths. This
3021 * is a corner case so we prioritize correctness over
3022 * non-optimal behavior (submitting 2 bios for the same extent).
3023 */
3024 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3025 prev_em_start && *prev_em_start != (u64)-1 &&
3026 *prev_em_start != em->orig_start)
3027 force_bio_submit = true;
3028
3029 if (prev_em_start)
3030 *prev_em_start = em->orig_start;
3031
Chris Masond1310b22008-01-24 16:13:08 -05003032 free_extent_map(em);
3033 em = NULL;
3034
3035 /* we've found a hole, just zero and go on */
3036 if (block_start == EXTENT_MAP_HOLE) {
3037 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003038 struct extent_state *cached = NULL;
3039
Cong Wang7ac687d2011-11-25 23:14:28 +08003040 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003041 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003042 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003043 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003044
3045 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003046 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003047 unlock_extent_cached(tree, cur,
3048 cur + iosize - 1,
3049 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003050 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003051 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003052 continue;
3053 }
3054 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003055 if (test_range_bit(tree, cur, cur_end,
3056 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003057 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003058 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003059 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003060 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003061 continue;
3062 }
Chris Mason70dec802008-01-29 09:59:12 -05003063 /* we have an inline extent but it didn't get marked up
3064 * to date. Error out
3065 */
3066 if (block_start == EXTENT_MAP_INLINE) {
3067 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003068 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003069 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003070 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003071 continue;
3072 }
Chris Masond1310b22008-01-24 16:13:08 -05003073
Josef Bacikc8f2f242013-02-11 11:33:00 -05003074 pnr -= page->index;
Mike Christie1f7ad752016-06-05 14:31:51 -05003075 ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL,
3076 page, sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04003077 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04003078 end_bio_extent_readpage, mirror_num,
3079 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003080 this_bio_flag,
3081 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003082 if (!ret) {
3083 nr++;
3084 *bio_flags = this_bio_flag;
3085 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003086 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003087 unlock_extent(tree, cur, cur + iosize - 1);
Liu Bobaf863b2016-07-11 10:39:07 -07003088 goto out;
Josef Bacikedd33c92012-10-05 16:40:32 -04003089 }
Chris Masond1310b22008-01-24 16:13:08 -05003090 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003091 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003092 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003093out:
Chris Masond1310b22008-01-24 16:13:08 -05003094 if (!nr) {
3095 if (!PageError(page))
3096 SetPageUptodate(page);
3097 unlock_page(page);
3098 }
Liu Bobaf863b2016-07-11 10:39:07 -07003099 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003100}
3101
Miao Xie99740902013-07-25 19:22:36 +08003102static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3103 struct page *pages[], int nr_pages,
3104 u64 start, u64 end,
3105 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003106 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003107 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003108 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003109 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003110{
3111 struct inode *inode;
3112 struct btrfs_ordered_extent *ordered;
3113 int index;
3114
3115 inode = pages[0]->mapping->host;
3116 while (1) {
3117 lock_extent(tree, start, end);
3118 ordered = btrfs_lookup_ordered_range(inode, start,
3119 end - start + 1);
3120 if (!ordered)
3121 break;
3122 unlock_extent(tree, start, end);
3123 btrfs_start_ordered_extent(inode, ordered, 1);
3124 btrfs_put_ordered_extent(ordered);
3125 }
3126
3127 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003128 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003129 mirror_num, bio_flags, 0, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003130 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003131 }
3132}
3133
3134static void __extent_readpages(struct extent_io_tree *tree,
3135 struct page *pages[],
3136 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003137 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003138 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003139 unsigned long *bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003140 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003141{
Stefan Behrens35a36212013-08-14 18:12:25 +02003142 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003143 u64 end = 0;
3144 u64 page_start;
3145 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003146 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003147
3148 for (index = 0; index < nr_pages; index++) {
3149 page_start = page_offset(pages[index]);
3150 if (!end) {
3151 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003152 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003153 first_index = index;
3154 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003155 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003156 } else {
3157 __do_contiguous_readpages(tree, &pages[first_index],
3158 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003159 end, get_extent, em_cached,
3160 bio, mirror_num, bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05003161 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003162 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003163 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003164 first_index = index;
3165 }
3166 }
3167
3168 if (end)
3169 __do_contiguous_readpages(tree, &pages[first_index],
3170 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003171 end, get_extent, em_cached, bio,
Mike Christie1f7ad752016-06-05 14:31:51 -05003172 mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003173 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003174}
3175
3176static int __extent_read_full_page(struct extent_io_tree *tree,
3177 struct page *page,
3178 get_extent_t *get_extent,
3179 struct bio **bio, int mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003180 unsigned long *bio_flags, int read_flags)
Miao Xie99740902013-07-25 19:22:36 +08003181{
3182 struct inode *inode = page->mapping->host;
3183 struct btrfs_ordered_extent *ordered;
3184 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003185 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003186 int ret;
3187
3188 while (1) {
3189 lock_extent(tree, start, end);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05303190 ordered = btrfs_lookup_ordered_range(inode, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003191 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003192 if (!ordered)
3193 break;
3194 unlock_extent(tree, start, end);
3195 btrfs_start_ordered_extent(inode, ordered, 1);
3196 btrfs_put_ordered_extent(ordered);
3197 }
3198
Miao Xie125bac012013-07-25 19:22:37 +08003199 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003200 bio_flags, read_flags, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003201 return ret;
3202}
3203
Chris Masond1310b22008-01-24 16:13:08 -05003204int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003205 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003206{
3207 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003208 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003209 int ret;
3210
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003211 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Mike Christie1f7ad752016-06-05 14:31:51 -05003212 &bio_flags, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003213 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05003214 ret = submit_one_bio(bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003215 return ret;
3216}
Chris Masond1310b22008-01-24 16:13:08 -05003217
Liu Boa91326672016-03-07 16:56:21 -08003218static void update_nr_written(struct page *page, struct writeback_control *wbc,
3219 unsigned long nr_written)
Chris Mason11c83492009-04-20 15:50:09 -04003220{
3221 wbc->nr_to_write -= nr_written;
Chris Mason11c83492009-04-20 15:50:09 -04003222}
3223
Chris Masond1310b22008-01-24 16:13:08 -05003224/*
Chris Mason40f76582014-05-21 13:35:51 -07003225 * helper for __extent_writepage, doing all of the delayed allocation setup.
3226 *
3227 * This returns 1 if our fill_delalloc function did all the work required
3228 * to write the page (copy into inline extent). In this case the IO has
3229 * been started and the page is already unlocked.
3230 *
3231 * This returns 0 if all went well (page still locked)
3232 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003233 */
Chris Mason40f76582014-05-21 13:35:51 -07003234static noinline_for_stack int writepage_delalloc(struct inode *inode,
3235 struct page *page, struct writeback_control *wbc,
3236 struct extent_page_data *epd,
3237 u64 delalloc_start,
3238 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003239{
Chris Mason40f76582014-05-21 13:35:51 -07003240 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003241 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003242 u64 nr_delalloc;
3243 u64 delalloc_to_write = 0;
3244 u64 delalloc_end = 0;
3245 int ret;
3246 int page_started = 0;
3247
3248 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3249 return 0;
3250
3251 while (delalloc_end < page_end) {
3252 nr_delalloc = find_lock_delalloc_range(inode, tree,
3253 page,
3254 &delalloc_start,
3255 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003256 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003257 if (nr_delalloc == 0) {
3258 delalloc_start = delalloc_end + 1;
3259 continue;
3260 }
3261 ret = tree->ops->fill_delalloc(inode, page,
3262 delalloc_start,
3263 delalloc_end,
3264 &page_started,
3265 nr_written);
3266 /* File system has been set read-only */
3267 if (ret) {
3268 SetPageError(page);
3269 /* fill_delalloc should be return < 0 for error
3270 * but just in case, we use > 0 here meaning the
3271 * IO is started, so we don't want to return > 0
3272 * unless things are going well.
3273 */
3274 ret = ret < 0 ? ret : -EIO;
3275 goto done;
3276 }
3277 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003278 * delalloc_end is already one less than the total length, so
3279 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003280 */
3281 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003282 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003283 delalloc_start = delalloc_end + 1;
3284 }
3285 if (wbc->nr_to_write < delalloc_to_write) {
3286 int thresh = 8192;
3287
3288 if (delalloc_to_write < thresh * 2)
3289 thresh = delalloc_to_write;
3290 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3291 thresh);
3292 }
3293
3294 /* did the fill delalloc function already unlock and start
3295 * the IO?
3296 */
3297 if (page_started) {
3298 /*
3299 * we've unlocked the page, so we can't update
3300 * the mapping's writeback index, just update
3301 * nr_to_write.
3302 */
3303 wbc->nr_to_write -= *nr_written;
3304 return 1;
3305 }
3306
3307 ret = 0;
3308
3309done:
3310 return ret;
3311}
3312
3313/*
3314 * helper for __extent_writepage. This calls the writepage start hooks,
3315 * and does the loop to map the page into extents and bios.
3316 *
3317 * We return 1 if the IO is started and the page is unlocked,
3318 * 0 if all went well (page still locked)
3319 * < 0 if there were errors (page still locked)
3320 */
3321static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3322 struct page *page,
3323 struct writeback_control *wbc,
3324 struct extent_page_data *epd,
3325 loff_t i_size,
3326 unsigned long nr_written,
3327 int write_flags, int *nr_ret)
3328{
Chris Masond1310b22008-01-24 16:13:08 -05003329 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003330 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003331 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003332 u64 end;
3333 u64 cur = start;
3334 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003335 u64 block_start;
3336 u64 iosize;
3337 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04003338 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003339 struct extent_map *em;
3340 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003341 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003342 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003343 int ret = 0;
3344 int nr = 0;
3345 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003346
Chris Mason247e7432008-07-17 12:53:51 -04003347 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003348 ret = tree->ops->writepage_start_hook(page, start,
3349 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003350 if (ret) {
3351 /* Fixup worker will requeue */
3352 if (ret == -EBUSY)
3353 wbc->pages_skipped++;
3354 else
3355 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003356
Chris Mason11c83492009-04-20 15:50:09 -04003357 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003358 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003359 ret = 1;
Chris Mason11c83492009-04-20 15:50:09 -04003360 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003361 }
3362 }
3363
Chris Mason11c83492009-04-20 15:50:09 -04003364 /*
3365 * we don't want to touch the inode after unlocking the page,
3366 * so we update the mapping writeback index now
3367 */
3368 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003369
Chris Masond1310b22008-01-24 16:13:08 -05003370 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003371 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003372 if (tree->ops && tree->ops->writepage_end_io_hook)
3373 tree->ops->writepage_end_io_hook(page, start,
3374 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003375 goto done;
3376 }
3377
Chris Masond1310b22008-01-24 16:13:08 -05003378 blocksize = inode->i_sb->s_blocksize;
3379
3380 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003381 u64 em_end;
David Sterba58409ed2016-05-04 11:46:10 +02003382 unsigned long max_nr;
3383
Chris Mason40f76582014-05-21 13:35:51 -07003384 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003385 if (tree->ops && tree->ops->writepage_end_io_hook)
3386 tree->ops->writepage_end_io_hook(page, cur,
3387 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003388 break;
3389 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003390 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003391 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003392 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003393 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003394 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003395 break;
3396 }
3397
3398 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003399 em_end = extent_map_end(em);
3400 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003401 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003402 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003403 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003404 sector = (em->block_start + extent_offset) >> 9;
3405 bdev = em->bdev;
3406 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003407 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003408 free_extent_map(em);
3409 em = NULL;
3410
Chris Masonc8b97812008-10-29 14:49:59 -04003411 /*
3412 * compressed and inline extents are written through other
3413 * paths in the FS
3414 */
3415 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003416 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003417 /*
3418 * end_io notification does not happen here for
3419 * compressed extents
3420 */
3421 if (!compressed && tree->ops &&
3422 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003423 tree->ops->writepage_end_io_hook(page, cur,
3424 cur + iosize - 1,
3425 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003426 else if (compressed) {
3427 /* we don't want to end_page_writeback on
3428 * a compressed extent. this happens
3429 * elsewhere
3430 */
3431 nr++;
3432 }
3433
3434 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003435 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003436 continue;
3437 }
Chris Masonc8b97812008-10-29 14:49:59 -04003438
David Sterba58409ed2016-05-04 11:46:10 +02003439 max_nr = (i_size >> PAGE_SHIFT) + 1;
3440
3441 set_range_writeback(tree, cur, cur + iosize - 1);
3442 if (!PageWriteback(page)) {
3443 btrfs_err(BTRFS_I(inode)->root->fs_info,
3444 "page %lu not writeback, cur %llu end %llu",
3445 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003446 }
David Sterba58409ed2016-05-04 11:46:10 +02003447
Mike Christie1f7ad752016-06-05 14:31:51 -05003448 ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
3449 page, sector, iosize, pg_offset,
David Sterba58409ed2016-05-04 11:46:10 +02003450 bdev, &epd->bio, max_nr,
3451 end_bio_extent_writepage,
3452 0, 0, 0, false);
3453 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003454 SetPageError(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04003455
Chris Masond1310b22008-01-24 16:13:08 -05003456 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003457 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003458 nr++;
3459 }
3460done:
Chris Mason40f76582014-05-21 13:35:51 -07003461 *nr_ret = nr;
Chris Mason771ed682008-11-06 22:02:51 -05003462
Chris Mason11c83492009-04-20 15:50:09 -04003463done_unlocked:
3464
Chris Mason2c64c532009-09-02 15:04:12 -04003465 /* drop our reference on any cached states */
3466 free_extent_state(cached_state);
Chris Mason40f76582014-05-21 13:35:51 -07003467 return ret;
3468}
3469
3470/*
3471 * the writepage semantics are similar to regular writepage. extent
3472 * records are inserted to lock ranges in the tree, and as dirty areas
3473 * are found, they are marked writeback. Then the lock bits are removed
3474 * and the end_io handler clears the writeback ranges
3475 */
3476static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3477 void *data)
3478{
3479 struct inode *inode = page->mapping->host;
3480 struct extent_page_data *epd = data;
3481 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003482 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003483 int ret;
3484 int nr = 0;
3485 size_t pg_offset = 0;
3486 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003487 unsigned long end_index = i_size >> PAGE_SHIFT;
Mike Christie1f7ad752016-06-05 14:31:51 -05003488 int write_flags = 0;
Chris Mason40f76582014-05-21 13:35:51 -07003489 unsigned long nr_written = 0;
3490
3491 if (wbc->sync_mode == WB_SYNC_ALL)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003492 write_flags = REQ_SYNC;
Chris Mason40f76582014-05-21 13:35:51 -07003493
3494 trace___extent_writepage(page, inode, wbc);
3495
3496 WARN_ON(!PageLocked(page));
3497
3498 ClearPageError(page);
3499
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003500 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003501 if (page->index > end_index ||
3502 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003503 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003504 unlock_page(page);
3505 return 0;
3506 }
3507
3508 if (page->index == end_index) {
3509 char *userpage;
3510
3511 userpage = kmap_atomic(page);
3512 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003513 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003514 kunmap_atomic(userpage);
3515 flush_dcache_page(page);
3516 }
3517
3518 pg_offset = 0;
3519
3520 set_page_extent_mapped(page);
3521
3522 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3523 if (ret == 1)
3524 goto done_unlocked;
3525 if (ret)
3526 goto done;
3527
3528 ret = __extent_writepage_io(inode, page, wbc, epd,
3529 i_size, nr_written, write_flags, &nr);
3530 if (ret == 1)
3531 goto done_unlocked;
3532
3533done:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003534 if (nr == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003535 /* make sure the mapping tag for page dirty gets cleared */
Chris Mason771ed682008-11-06 22:02:51 -05003536 set_page_writeback(page);
3537 end_page_writeback(page);
3538 }
Filipe Manana61391d52014-05-09 17:17:40 +01003539 if (PageError(page)) {
3540 ret = ret < 0 ? ret : -EIO;
3541 end_extent_writepage(page, ret, start, page_end);
3542 }
Christoph Hellwigb2950862008-12-02 09:54:17 -05003543 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003544 return ret;
Chris Mason4bef0842008-09-08 11:18:08 -04003545
3546done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003547 return 0;
3548}
3549
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003550void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003551{
NeilBrown74316202014-07-07 15:16:04 +10003552 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3553 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003554}
3555
Chris Mason0e378df2014-05-19 20:55:27 -07003556static noinline_for_stack int
3557lock_extent_buffer_for_io(struct extent_buffer *eb,
3558 struct btrfs_fs_info *fs_info,
3559 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003560{
3561 unsigned long i, num_pages;
3562 int flush = 0;
3563 int ret = 0;
3564
3565 if (!btrfs_try_tree_write_lock(eb)) {
3566 flush = 1;
3567 flush_write_bio(epd);
3568 btrfs_tree_lock(eb);
3569 }
3570
3571 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3572 btrfs_tree_unlock(eb);
3573 if (!epd->sync_io)
3574 return 0;
3575 if (!flush) {
3576 flush_write_bio(epd);
3577 flush = 1;
3578 }
Chris Masona098d8e2012-03-21 12:09:56 -04003579 while (1) {
3580 wait_on_extent_buffer_writeback(eb);
3581 btrfs_tree_lock(eb);
3582 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3583 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003584 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003585 }
3586 }
3587
Josef Bacik51561ff2012-07-20 16:25:24 -04003588 /*
3589 * We need to do this to prevent races in people who check if the eb is
3590 * under IO since we can end up having no IO bits set for a short period
3591 * of time.
3592 */
3593 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003594 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3595 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003596 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003597 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003598 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3599 -eb->len,
3600 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003601 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003602 } else {
3603 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003604 }
3605
3606 btrfs_tree_unlock(eb);
3607
3608 if (!ret)
3609 return ret;
3610
3611 num_pages = num_extent_pages(eb->start, eb->len);
3612 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003613 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003614
3615 if (!trylock_page(p)) {
3616 if (!flush) {
3617 flush_write_bio(epd);
3618 flush = 1;
3619 }
3620 lock_page(p);
3621 }
3622 }
3623
3624 return ret;
3625}
3626
3627static void end_extent_buffer_writeback(struct extent_buffer *eb)
3628{
3629 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003630 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003631 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3632}
3633
Filipe Manana656f30d2014-09-26 12:25:56 +01003634static void set_btree_ioerr(struct page *page)
3635{
3636 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Filipe Manana656f30d2014-09-26 12:25:56 +01003637
3638 SetPageError(page);
3639 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3640 return;
3641
3642 /*
3643 * If writeback for a btree extent that doesn't belong to a log tree
3644 * failed, increment the counter transaction->eb_write_errors.
3645 * We do this because while the transaction is running and before it's
3646 * committing (when we call filemap_fdata[write|wait]_range against
3647 * the btree inode), we might have
3648 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3649 * returns an error or an error happens during writeback, when we're
3650 * committing the transaction we wouldn't know about it, since the pages
3651 * can be no longer dirty nor marked anymore for writeback (if a
3652 * subsequent modification to the extent buffer didn't happen before the
3653 * transaction commit), which makes filemap_fdata[write|wait]_range not
3654 * able to find the pages tagged with SetPageError at transaction
3655 * commit time. So if this happens we must abort the transaction,
3656 * otherwise we commit a super block with btree roots that point to
3657 * btree nodes/leafs whose content on disk is invalid - either garbage
3658 * or the content of some node/leaf from a past generation that got
3659 * cowed or deleted and is no longer valid.
3660 *
3661 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3662 * not be enough - we need to distinguish between log tree extents vs
3663 * non-log tree extents, and the next filemap_fdatawait_range() call
3664 * will catch and clear such errors in the mapping - and that call might
3665 * be from a log sync and not from a transaction commit. Also, checking
3666 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3667 * not done and would not be reliable - the eb might have been released
3668 * from memory and reading it back again means that flag would not be
3669 * set (since it's a runtime flag, not persisted on disk).
3670 *
3671 * Using the flags below in the btree inode also makes us achieve the
3672 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3673 * writeback for all dirty pages and before filemap_fdatawait_range()
3674 * is called, the writeback for all dirty pages had already finished
3675 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3676 * filemap_fdatawait_range() would return success, as it could not know
3677 * that writeback errors happened (the pages were no longer tagged for
3678 * writeback).
3679 */
3680 switch (eb->log_index) {
3681 case -1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003682 set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003683 break;
3684 case 0:
Josef Bacikafcdd122016-09-02 15:40:02 -04003685 set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003686 break;
3687 case 1:
Josef Bacikafcdd122016-09-02 15:40:02 -04003688 set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
Filipe Manana656f30d2014-09-26 12:25:56 +01003689 break;
3690 default:
3691 BUG(); /* unexpected, logic error */
3692 }
3693}
3694
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003695static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003696{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003697 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003698 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003699 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003700
Kent Overstreet2c30c712013-11-07 12:20:26 -08003701 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003702 struct page *page = bvec->bv_page;
3703
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003704 eb = (struct extent_buffer *)page->private;
3705 BUG_ON(!eb);
3706 done = atomic_dec_and_test(&eb->io_pages);
3707
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003708 if (bio->bi_error ||
3709 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003710 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003711 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003712 }
3713
3714 end_page_writeback(page);
3715
3716 if (!done)
3717 continue;
3718
3719 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003720 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003721
3722 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003723}
3724
Chris Mason0e378df2014-05-19 20:55:27 -07003725static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003726 struct btrfs_fs_info *fs_info,
3727 struct writeback_control *wbc,
3728 struct extent_page_data *epd)
3729{
3730 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003731 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003732 u64 offset = eb->start;
Liu Bo851cd172016-09-23 13:44:44 -07003733 u32 nritems;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003734 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003735 unsigned long bio_flags = 0;
Liu Bo851cd172016-09-23 13:44:44 -07003736 unsigned long start, end;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06003737 int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003738 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003739
Filipe Manana656f30d2014-09-26 12:25:56 +01003740 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003741 num_pages = num_extent_pages(eb->start, eb->len);
3742 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003743 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3744 bio_flags = EXTENT_BIO_TREE_LOG;
3745
Liu Bo851cd172016-09-23 13:44:44 -07003746 /* set btree blocks beyond nritems with 0 to avoid stale content. */
3747 nritems = btrfs_header_nritems(eb);
Liu Bo3eb548e2016-09-14 17:22:57 -07003748 if (btrfs_header_level(eb) > 0) {
Liu Bo3eb548e2016-09-14 17:22:57 -07003749 end = btrfs_node_key_ptr_offset(nritems);
3750
David Sterbab159fa22016-11-08 18:09:03 +01003751 memzero_extent_buffer(eb, end, eb->len - end);
Liu Bo851cd172016-09-23 13:44:44 -07003752 } else {
3753 /*
3754 * leaf:
3755 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3756 */
3757 start = btrfs_item_nr_offset(nritems);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003758 end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb);
David Sterbab159fa22016-11-08 18:09:03 +01003759 memzero_extent_buffer(eb, start, end - start);
Liu Bo3eb548e2016-09-14 17:22:57 -07003760 }
3761
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003762 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003763 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003764
3765 clear_page_dirty_for_io(p);
3766 set_page_writeback(p);
Mike Christie1f7ad752016-06-05 14:31:51 -05003767 ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
3768 p, offset >> 9, PAGE_SIZE, 0, bdev,
3769 &epd->bio, -1,
3770 end_bio_extent_buffer_writepage,
Filipe Manana005efed2015-09-14 09:09:31 +01003771 0, epd->bio_flags, bio_flags, false);
Josef Bacikde0022b2012-09-25 14:25:58 -04003772 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003773 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003774 set_btree_ioerr(p);
Filipe Manana55e3bd22014-09-22 17:41:04 +01003775 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003776 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3777 end_extent_buffer_writeback(eb);
3778 ret = -EIO;
3779 break;
3780 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003781 offset += PAGE_SIZE;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003782 update_nr_written(p, wbc, 1);
3783 unlock_page(p);
3784 }
3785
3786 if (unlikely(ret)) {
3787 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003788 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003789 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003790 unlock_page(p);
3791 }
3792 }
3793
3794 return ret;
3795}
3796
3797int btree_write_cache_pages(struct address_space *mapping,
3798 struct writeback_control *wbc)
3799{
3800 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3801 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3802 struct extent_buffer *eb, *prev_eb = NULL;
3803 struct extent_page_data epd = {
3804 .bio = NULL,
3805 .tree = tree,
3806 .extent_locked = 0,
3807 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003808 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003809 };
3810 int ret = 0;
3811 int done = 0;
3812 int nr_to_write_done = 0;
3813 struct pagevec pvec;
3814 int nr_pages;
3815 pgoff_t index;
3816 pgoff_t end; /* Inclusive */
3817 int scanned = 0;
3818 int tag;
3819
3820 pagevec_init(&pvec, 0);
3821 if (wbc->range_cyclic) {
3822 index = mapping->writeback_index; /* Start from prev offset */
3823 end = -1;
3824 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003825 index = wbc->range_start >> PAGE_SHIFT;
3826 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003827 scanned = 1;
3828 }
3829 if (wbc->sync_mode == WB_SYNC_ALL)
3830 tag = PAGECACHE_TAG_TOWRITE;
3831 else
3832 tag = PAGECACHE_TAG_DIRTY;
3833retry:
3834 if (wbc->sync_mode == WB_SYNC_ALL)
3835 tag_pages_for_writeback(mapping, index, end);
3836 while (!done && !nr_to_write_done && (index <= end) &&
3837 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3838 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3839 unsigned i;
3840
3841 scanned = 1;
3842 for (i = 0; i < nr_pages; i++) {
3843 struct page *page = pvec.pages[i];
3844
3845 if (!PagePrivate(page))
3846 continue;
3847
3848 if (!wbc->range_cyclic && page->index > end) {
3849 done = 1;
3850 break;
3851 }
3852
Josef Bacikb5bae262012-09-14 13:43:01 -04003853 spin_lock(&mapping->private_lock);
3854 if (!PagePrivate(page)) {
3855 spin_unlock(&mapping->private_lock);
3856 continue;
3857 }
3858
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003859 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003860
3861 /*
3862 * Shouldn't happen and normally this would be a BUG_ON
3863 * but no sense in crashing the users box for something
3864 * we can survive anyway.
3865 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303866 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003867 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003868 continue;
3869 }
3870
Josef Bacikb5bae262012-09-14 13:43:01 -04003871 if (eb == prev_eb) {
3872 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003873 continue;
3874 }
3875
Josef Bacikb5bae262012-09-14 13:43:01 -04003876 ret = atomic_inc_not_zero(&eb->refs);
3877 spin_unlock(&mapping->private_lock);
3878 if (!ret)
3879 continue;
3880
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003881 prev_eb = eb;
3882 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3883 if (!ret) {
3884 free_extent_buffer(eb);
3885 continue;
3886 }
3887
3888 ret = write_one_eb(eb, fs_info, wbc, &epd);
3889 if (ret) {
3890 done = 1;
3891 free_extent_buffer(eb);
3892 break;
3893 }
3894 free_extent_buffer(eb);
3895
3896 /*
3897 * the filesystem may choose to bump up nr_to_write.
3898 * We have to make sure to honor the new nr_to_write
3899 * at any time
3900 */
3901 nr_to_write_done = wbc->nr_to_write <= 0;
3902 }
3903 pagevec_release(&pvec);
3904 cond_resched();
3905 }
3906 if (!scanned && !done) {
3907 /*
3908 * We hit the last page and there is more work to be done: wrap
3909 * back to the start of the file
3910 */
3911 scanned = 1;
3912 index = 0;
3913 goto retry;
3914 }
3915 flush_write_bio(&epd);
3916 return ret;
3917}
3918
Chris Masond1310b22008-01-24 16:13:08 -05003919/**
3920 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3921 * @mapping: address space structure to write
3922 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3923 * @writepage: function called for each page
3924 * @data: data passed to writepage function
3925 *
3926 * If a page is already under I/O, write_cache_pages() skips it, even
3927 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3928 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3929 * and msync() need to guarantee that all the data which was dirty at the time
3930 * the call was made get new I/O started against them. If wbc->sync_mode is
3931 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3932 * existing IO to complete.
3933 */
Chris Mason4bef0842008-09-08 11:18:08 -04003934static int extent_write_cache_pages(struct extent_io_tree *tree,
3935 struct address_space *mapping,
3936 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003937 writepage_t writepage, void *data,
3938 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003939{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003940 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003941 int ret = 0;
3942 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003943 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003944 struct pagevec pvec;
3945 int nr_pages;
3946 pgoff_t index;
3947 pgoff_t end; /* Inclusive */
Liu Boa91326672016-03-07 16:56:21 -08003948 pgoff_t done_index;
3949 int range_whole = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003950 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003951 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003952
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003953 /*
3954 * We have to hold onto the inode so that ordered extents can do their
3955 * work when the IO finishes. The alternative to this is failing to add
3956 * an ordered extent if the igrab() fails there and that is a huge pain
3957 * to deal with, so instead just hold onto the inode throughout the
3958 * writepages operation. If it fails here we are freeing up the inode
3959 * anyway and we'd rather not waste our time writing out stuff that is
3960 * going to be truncated anyway.
3961 */
3962 if (!igrab(inode))
3963 return 0;
3964
Chris Masond1310b22008-01-24 16:13:08 -05003965 pagevec_init(&pvec, 0);
3966 if (wbc->range_cyclic) {
3967 index = mapping->writeback_index; /* Start from prev offset */
3968 end = -1;
3969 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003970 index = wbc->range_start >> PAGE_SHIFT;
3971 end = wbc->range_end >> PAGE_SHIFT;
Liu Boa91326672016-03-07 16:56:21 -08003972 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3973 range_whole = 1;
Chris Masond1310b22008-01-24 16:13:08 -05003974 scanned = 1;
3975 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003976 if (wbc->sync_mode == WB_SYNC_ALL)
3977 tag = PAGECACHE_TAG_TOWRITE;
3978 else
3979 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003980retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003981 if (wbc->sync_mode == WB_SYNC_ALL)
3982 tag_pages_for_writeback(mapping, index, end);
Liu Boa91326672016-03-07 16:56:21 -08003983 done_index = index;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003984 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003985 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3986 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003987 unsigned i;
3988
3989 scanned = 1;
3990 for (i = 0; i < nr_pages; i++) {
3991 struct page *page = pvec.pages[i];
3992
Liu Boa91326672016-03-07 16:56:21 -08003993 done_index = page->index;
Chris Masond1310b22008-01-24 16:13:08 -05003994 /*
3995 * At this point we hold neither mapping->tree_lock nor
3996 * lock on the page itself: the page may be truncated or
3997 * invalidated (changing page->mapping to NULL), or even
3998 * swizzled back from swapper_space to tmpfs file
3999 * mapping
4000 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004001 if (!trylock_page(page)) {
4002 flush_fn(data);
4003 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004004 }
Chris Masond1310b22008-01-24 16:13:08 -05004005
4006 if (unlikely(page->mapping != mapping)) {
4007 unlock_page(page);
4008 continue;
4009 }
4010
4011 if (!wbc->range_cyclic && page->index > end) {
4012 done = 1;
4013 unlock_page(page);
4014 continue;
4015 }
4016
Chris Masond2c3f4f2008-11-19 12:44:22 -05004017 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05004018 if (PageWriteback(page))
4019 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05004020 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004021 }
Chris Masond1310b22008-01-24 16:13:08 -05004022
4023 if (PageWriteback(page) ||
4024 !clear_page_dirty_for_io(page)) {
4025 unlock_page(page);
4026 continue;
4027 }
4028
4029 ret = (*writepage)(page, wbc, data);
4030
4031 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4032 unlock_page(page);
4033 ret = 0;
4034 }
Liu Boa91326672016-03-07 16:56:21 -08004035 if (ret < 0) {
4036 /*
4037 * done_index is set past this page,
4038 * so media errors will not choke
4039 * background writeout for the entire
4040 * file. This has consequences for
4041 * range_cyclic semantics (ie. it may
4042 * not be suitable for data integrity
4043 * writeout).
4044 */
4045 done_index = page->index + 1;
4046 done = 1;
4047 break;
4048 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004049
4050 /*
4051 * the filesystem may choose to bump up nr_to_write.
4052 * We have to make sure to honor the new nr_to_write
4053 * at any time
4054 */
4055 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004056 }
4057 pagevec_release(&pvec);
4058 cond_resched();
4059 }
Liu Bo894b36e2016-03-07 16:56:22 -08004060 if (!scanned && !done) {
Chris Masond1310b22008-01-24 16:13:08 -05004061 /*
4062 * We hit the last page and there is more work to be done: wrap
4063 * back to the start of the file
4064 */
4065 scanned = 1;
4066 index = 0;
4067 goto retry;
4068 }
Liu Boa91326672016-03-07 16:56:21 -08004069
4070 if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
4071 mapping->writeback_index = done_index;
4072
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004073 btrfs_add_delayed_iput(inode);
Liu Bo894b36e2016-03-07 16:56:22 -08004074 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004075}
Chris Masond1310b22008-01-24 16:13:08 -05004076
Chris Masonffbd5172009-04-20 15:50:09 -04004077static void flush_epd_write_bio(struct extent_page_data *epd)
4078{
4079 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004080 int ret;
4081
Mike Christie1f7ad752016-06-05 14:31:51 -05004082 bio_set_op_attrs(epd->bio, REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004083 epd->sync_io ? REQ_SYNC : 0);
Jeff Mahoney355808c2011-10-03 23:23:14 -04004084
Mike Christie1f7ad752016-06-05 14:31:51 -05004085 ret = submit_one_bio(epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004087 epd->bio = NULL;
4088 }
4089}
4090
Chris Masond2c3f4f2008-11-19 12:44:22 -05004091static noinline void flush_write_bio(void *data)
4092{
4093 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004094 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004095}
4096
Chris Masond1310b22008-01-24 16:13:08 -05004097int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4098 get_extent_t *get_extent,
4099 struct writeback_control *wbc)
4100{
4101 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004102 struct extent_page_data epd = {
4103 .bio = NULL,
4104 .tree = tree,
4105 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004106 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004107 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004108 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004109 };
Chris Masond1310b22008-01-24 16:13:08 -05004110
Chris Masond1310b22008-01-24 16:13:08 -05004111 ret = __extent_writepage(page, wbc, &epd);
4112
Chris Masonffbd5172009-04-20 15:50:09 -04004113 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004114 return ret;
4115}
Chris Masond1310b22008-01-24 16:13:08 -05004116
Chris Mason771ed682008-11-06 22:02:51 -05004117int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4118 u64 start, u64 end, get_extent_t *get_extent,
4119 int mode)
4120{
4121 int ret = 0;
4122 struct address_space *mapping = inode->i_mapping;
4123 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004124 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4125 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004126
4127 struct extent_page_data epd = {
4128 .bio = NULL,
4129 .tree = tree,
4130 .get_extent = get_extent,
4131 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004132 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004133 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05004134 };
4135 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004136 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004137 .nr_to_write = nr_pages * 2,
4138 .range_start = start,
4139 .range_end = end + 1,
4140 };
4141
Chris Masond3977122009-01-05 21:25:51 -05004142 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004143 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004144 if (clear_page_dirty_for_io(page))
4145 ret = __extent_writepage(page, &wbc_writepages, &epd);
4146 else {
4147 if (tree->ops && tree->ops->writepage_end_io_hook)
4148 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004149 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004150 NULL, 1);
4151 unlock_page(page);
4152 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004153 put_page(page);
4154 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004155 }
4156
Chris Masonffbd5172009-04-20 15:50:09 -04004157 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004158 return ret;
4159}
Chris Masond1310b22008-01-24 16:13:08 -05004160
4161int extent_writepages(struct extent_io_tree *tree,
4162 struct address_space *mapping,
4163 get_extent_t *get_extent,
4164 struct writeback_control *wbc)
4165{
4166 int ret = 0;
4167 struct extent_page_data epd = {
4168 .bio = NULL,
4169 .tree = tree,
4170 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004171 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004172 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004173 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004174 };
4175
Chris Mason4bef0842008-09-08 11:18:08 -04004176 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004177 __extent_writepage, &epd,
4178 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004179 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004180 return ret;
4181}
Chris Masond1310b22008-01-24 16:13:08 -05004182
4183int extent_readpages(struct extent_io_tree *tree,
4184 struct address_space *mapping,
4185 struct list_head *pages, unsigned nr_pages,
4186 get_extent_t get_extent)
4187{
4188 struct bio *bio = NULL;
4189 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004190 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004191 struct page *pagepool[16];
4192 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004193 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004194 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004195 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004196
Chris Masond1310b22008-01-24 16:13:08 -05004197 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004198 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004199
4200 prefetchw(&page->flags);
4201 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004202 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07004203 page->index,
4204 readahead_gfp_mask(mapping))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004205 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004206 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004207 }
Liu Bo67c96842012-07-20 21:43:09 -06004208
4209 pagepool[nr++] = page;
4210 if (nr < ARRAY_SIZE(pagepool))
4211 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004212 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004213 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004214 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004215 }
Miao Xie99740902013-07-25 19:22:36 +08004216 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004217 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Mike Christie1f7ad752016-06-05 14:31:51 -05004218 &bio, 0, &bio_flags, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004219
Miao Xie125bac012013-07-25 19:22:37 +08004220 if (em_cached)
4221 free_extent_map(em_cached);
4222
Chris Masond1310b22008-01-24 16:13:08 -05004223 BUG_ON(!list_empty(pages));
4224 if (bio)
Mike Christie1f7ad752016-06-05 14:31:51 -05004225 return submit_one_bio(bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004226 return 0;
4227}
Chris Masond1310b22008-01-24 16:13:08 -05004228
4229/*
4230 * basic invalidatepage code, this waits on any locked or writeback
4231 * ranges corresponding to the page, and then deletes any extent state
4232 * records from the tree
4233 */
4234int extent_invalidatepage(struct extent_io_tree *tree,
4235 struct page *page, unsigned long offset)
4236{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004237 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004238 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004239 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004240 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4241
Qu Wenruofda28322013-02-26 08:10:22 +00004242 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004243 if (start > end)
4244 return 0;
4245
David Sterbaff13db42015-12-03 14:30:40 +01004246 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004247 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004248 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004249 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4250 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004251 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004252 return 0;
4253}
Chris Masond1310b22008-01-24 16:13:08 -05004254
4255/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004256 * a helper for releasepage, this tests for areas of the page that
4257 * are locked or under IO and drops the related state bits if it is safe
4258 * to drop the page.
4259 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004260static int try_release_extent_state(struct extent_map_tree *map,
4261 struct extent_io_tree *tree,
4262 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004263{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004264 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004265 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004266 int ret = 1;
4267
Chris Mason211f90e2008-07-18 11:56:15 -04004268 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004269 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004270 ret = 0;
4271 else {
Chris Mason11ef1602009-09-23 20:28:46 -04004272 /*
4273 * at this point we can safely clear everything except the
4274 * locked bit and the nodatasum bit
4275 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004276 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004277 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4278 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004279
4280 /* if clear_extent_bit failed for enomem reasons,
4281 * we can't allow the release to continue.
4282 */
4283 if (ret < 0)
4284 ret = 0;
4285 else
4286 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004287 }
4288 return ret;
4289}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004290
4291/*
Chris Masond1310b22008-01-24 16:13:08 -05004292 * a helper for releasepage. As long as there are no locked extents
4293 * in the range corresponding to the page, both state records and extent
4294 * map records are removed
4295 */
4296int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004297 struct extent_io_tree *tree, struct page *page,
4298 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004299{
4300 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004301 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004302 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004303
Mel Gormand0164ad2015-11-06 16:28:21 -08004304 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004305 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004306 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004307 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004308 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004309 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004310 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004311 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004312 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004313 break;
4314 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004315 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4316 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004317 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004318 free_extent_map(em);
4319 break;
4320 }
4321 if (!test_range_bit(tree, em->start,
4322 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004323 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004324 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004325 remove_extent_mapping(map, em);
4326 /* once for the rb tree */
4327 free_extent_map(em);
4328 }
4329 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004330 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004331
4332 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004333 free_extent_map(em);
4334 }
Chris Masond1310b22008-01-24 16:13:08 -05004335 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004336 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004337}
Chris Masond1310b22008-01-24 16:13:08 -05004338
Chris Masonec29ed52011-02-23 16:23:20 -05004339/*
4340 * helper function for fiemap, which doesn't want to see any holes.
4341 * This maps until we find something past 'last'
4342 */
4343static struct extent_map *get_extent_skip_holes(struct inode *inode,
4344 u64 offset,
4345 u64 last,
4346 get_extent_t *get_extent)
4347{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004348 u64 sectorsize = btrfs_inode_sectorsize(inode);
Chris Masonec29ed52011-02-23 16:23:20 -05004349 struct extent_map *em;
4350 u64 len;
4351
4352 if (offset >= last)
4353 return NULL;
4354
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304355 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004356 len = last - offset;
4357 if (len == 0)
4358 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004359 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05004360 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004361 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004362 return em;
4363
4364 /* if this isn't a hole return it */
4365 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4366 em->block_start != EXTENT_MAP_HOLE) {
4367 return em;
4368 }
4369
4370 /* this is a hole, advance to the next extent */
4371 offset = extent_map_end(em);
4372 free_extent_map(em);
4373 if (offset >= last)
4374 break;
4375 }
4376 return NULL;
4377}
4378
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004379int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4380 __u64 start, __u64 len, get_extent_t *get_extent)
4381{
Josef Bacik975f84f2010-11-23 19:36:57 +00004382 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004383 u64 off = start;
4384 u64 max = start + len;
4385 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004386 u32 found_type;
4387 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004388 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004389 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004390 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004391 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004392 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004393 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004394 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004395 struct btrfs_root *root = BTRFS_I(inode)->root;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004396 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004397 u64 em_start = 0;
4398 u64 em_len = 0;
4399 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004400
4401 if (len == 0)
4402 return -EINVAL;
4403
Josef Bacik975f84f2010-11-23 19:36:57 +00004404 path = btrfs_alloc_path();
4405 if (!path)
4406 return -ENOMEM;
4407 path->leave_spinning = 1;
4408
Jeff Mahoneyda170662016-06-15 09:22:56 -04004409 start = round_down(start, btrfs_inode_sectorsize(inode));
4410 len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004411
Chris Masonec29ed52011-02-23 16:23:20 -05004412 /*
4413 * lookup the last file extent. We're not using i_size here
4414 * because there might be preallocation past i_size
4415 */
David Sterbaf85b7372017-01-20 14:54:07 +01004416 ret = btrfs_lookup_file_extent(NULL, root, path,
4417 btrfs_ino(BTRFS_I(inode)), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004418 if (ret < 0) {
4419 btrfs_free_path(path);
4420 return ret;
Liu Bo2d324f52016-05-17 17:21:48 -07004421 } else {
4422 WARN_ON(!ret);
4423 if (ret == 1)
4424 ret = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004425 }
Liu Bo2d324f52016-05-17 17:21:48 -07004426
Josef Bacik975f84f2010-11-23 19:36:57 +00004427 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004428 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004429 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004430
Chris Masonec29ed52011-02-23 16:23:20 -05004431 /* No extents, but there might be delalloc bits */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004432 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004433 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004434 /* have to trust i_size as the end */
4435 last = (u64)-1;
4436 last_for_get_extent = isize;
4437 } else {
4438 /*
4439 * remember the start of the last extent. There are a
4440 * bunch of different factors that go into the length of the
4441 * extent, so its much less complex to remember where it started
4442 */
4443 last = found_key.offset;
4444 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004445 }
Liu Bofe09e162013-09-22 12:54:23 +08004446 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004447
Chris Masonec29ed52011-02-23 16:23:20 -05004448 /*
4449 * we might have some extents allocated but more delalloc past those
4450 * extents. so, we trust isize unless the start of the last extent is
4451 * beyond isize
4452 */
4453 if (last < isize) {
4454 last = (u64)-1;
4455 last_for_get_extent = isize;
4456 }
4457
David Sterbaff13db42015-12-03 14:30:40 +01004458 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004459 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004460
Josef Bacik4d479cf2011-11-17 11:34:31 -05004461 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004462 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004463 if (!em)
4464 goto out;
4465 if (IS_ERR(em)) {
4466 ret = PTR_ERR(em);
4467 goto out;
4468 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004469
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004470 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004471 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004472
Chris Masonea8efc72011-03-08 11:54:40 -05004473 /* break if the extent we found is outside the range */
4474 if (em->start >= max || extent_map_end(em) < off)
4475 break;
4476
4477 /*
4478 * get_extent may return an extent that starts before our
4479 * requested range. We have to make sure the ranges
4480 * we return to fiemap always move forward and don't
4481 * overlap, so adjust the offsets here
4482 */
4483 em_start = max(em->start, off);
4484
4485 /*
4486 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004487 * for adjusting the disk offset below. Only do this if the
4488 * extent isn't compressed since our in ram offset may be past
4489 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004490 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004491 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4492 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004493 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004494 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004495 disko = 0;
4496 flags = 0;
4497
Chris Masonea8efc72011-03-08 11:54:40 -05004498 /*
4499 * bump off for our next call to get_extent
4500 */
4501 off = extent_map_end(em);
4502 if (off >= max)
4503 end = 1;
4504
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004505 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004506 end = 1;
4507 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004508 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004509 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4510 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004511 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004512 flags |= (FIEMAP_EXTENT_DELALLOC |
4513 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004514 } else if (fieinfo->fi_extents_max) {
Lu Fengqiafce7722016-06-13 09:36:46 +08004515 struct btrfs_trans_handle *trans;
4516
Josef Bacikdc046b12014-09-10 16:20:45 -04004517 u64 bytenr = em->block_start -
4518 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004519
Chris Masonea8efc72011-03-08 11:54:40 -05004520 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004521
4522 /*
Lu Fengqiafce7722016-06-13 09:36:46 +08004523 * We need a trans handle to get delayed refs
4524 */
4525 trans = btrfs_join_transaction(root);
4526 /*
4527 * It's OK if we can't start a trans we can still check
4528 * from commit_root
4529 */
4530 if (IS_ERR(trans))
4531 trans = NULL;
4532
4533 /*
Liu Bofe09e162013-09-22 12:54:23 +08004534 * As btrfs supports shared space, this information
4535 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004536 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4537 * then we're just getting a count and we can skip the
4538 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004539 */
Lu Fengqiafce7722016-06-13 09:36:46 +08004540 ret = btrfs_check_shared(trans, root->fs_info,
David Sterbaf85b7372017-01-20 14:54:07 +01004541 root->objectid,
4542 btrfs_ino(BTRFS_I(inode)), bytenr);
Lu Fengqiafce7722016-06-13 09:36:46 +08004543 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004544 btrfs_end_transaction(trans);
Josef Bacikdc046b12014-09-10 16:20:45 -04004545 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004546 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004547 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004548 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004549 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004550 }
4551 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4552 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004553 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4554 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004555
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004556 free_extent_map(em);
4557 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004558 if ((em_start >= last) || em_len == (u64)-1 ||
4559 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004560 flags |= FIEMAP_EXTENT_LAST;
4561 end = 1;
4562 }
4563
Chris Masonec29ed52011-02-23 16:23:20 -05004564 /* now scan forward to see if this is really the last extent. */
4565 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4566 get_extent);
4567 if (IS_ERR(em)) {
4568 ret = PTR_ERR(em);
4569 goto out;
4570 }
4571 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004572 flags |= FIEMAP_EXTENT_LAST;
4573 end = 1;
4574 }
Chris Masonec29ed52011-02-23 16:23:20 -05004575 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4576 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004577 if (ret) {
4578 if (ret == 1)
4579 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004580 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004581 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004582 }
4583out_free:
4584 free_extent_map(em);
4585out:
Liu Bofe09e162013-09-22 12:54:23 +08004586 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004587 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004588 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004589 return ret;
4590}
4591
Chris Mason727011e2010-08-06 13:21:20 -04004592static void __free_extent_buffer(struct extent_buffer *eb)
4593{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004594 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004595 kmem_cache_free(extent_buffer_cache, eb);
4596}
4597
Josef Bacika26e8c92014-03-28 17:07:27 -04004598int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004599{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004600 return (atomic_read(&eb->io_pages) ||
4601 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4602 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004603}
4604
Miao Xie897ca6e92010-10-26 20:57:29 -04004605/*
4606 * Helper for releasing extent buffer page.
4607 */
David Sterbaa50924e2014-07-31 00:51:36 +02004608static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004609{
4610 unsigned long index;
4611 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004612 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004613
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004614 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004615
David Sterbaa50924e2014-07-31 00:51:36 +02004616 index = num_extent_pages(eb->start, eb->len);
4617 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004618 return;
4619
4620 do {
4621 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004622 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004623 if (!page)
4624 continue;
4625 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004626 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004627 /*
4628 * We do this since we'll remove the pages after we've
4629 * removed the eb from the radix tree, so we could race
4630 * and have this page now attached to the new eb. So
4631 * only clear page_private if it's still connected to
4632 * this eb.
4633 */
4634 if (PagePrivate(page) &&
4635 page->private == (unsigned long)eb) {
4636 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4637 BUG_ON(PageDirty(page));
4638 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004639 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004640 * We need to make sure we haven't be attached
4641 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004642 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004643 ClearPagePrivate(page);
4644 set_page_private(page, 0);
4645 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004646 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004647 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004648
4649 if (mapped)
4650 spin_unlock(&page->mapping->private_lock);
4651
Nicholas D Steeves01327612016-05-19 21:18:45 -04004652 /* One for when we allocated the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004653 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004654 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004655}
4656
4657/*
4658 * Helper for releasing the extent buffer.
4659 */
4660static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4661{
David Sterbaa50924e2014-07-31 00:51:36 +02004662 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004663 __free_extent_buffer(eb);
4664}
4665
Josef Bacikf28491e2013-12-16 13:24:27 -05004666static struct extent_buffer *
4667__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004668 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004669{
4670 struct extent_buffer *eb = NULL;
4671
Michal Hockod1b5c562015-08-19 14:17:40 +02004672 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004673 eb->start = start;
4674 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004675 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004676 eb->bflags = 0;
4677 rwlock_init(&eb->lock);
4678 atomic_set(&eb->write_locks, 0);
4679 atomic_set(&eb->read_locks, 0);
4680 atomic_set(&eb->blocking_readers, 0);
4681 atomic_set(&eb->blocking_writers, 0);
4682 atomic_set(&eb->spinning_readers, 0);
4683 atomic_set(&eb->spinning_writers, 0);
4684 eb->lock_nested = 0;
4685 init_waitqueue_head(&eb->write_lock_wq);
4686 init_waitqueue_head(&eb->read_lock_wq);
4687
4688 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4689
4690 spin_lock_init(&eb->refs_lock);
4691 atomic_set(&eb->refs, 1);
4692 atomic_set(&eb->io_pages, 0);
4693
4694 /*
4695 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4696 */
4697 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4698 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4699 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4700
4701 return eb;
4702}
4703
4704struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4705{
4706 unsigned long i;
4707 struct page *p;
4708 struct extent_buffer *new;
4709 unsigned long num_pages = num_extent_pages(src->start, src->len);
4710
David Sterba3f556f72014-06-15 03:20:26 +02004711 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004712 if (new == NULL)
4713 return NULL;
4714
4715 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004716 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004717 if (!p) {
4718 btrfs_release_extent_buffer(new);
4719 return NULL;
4720 }
4721 attach_extent_buffer_page(new, p);
4722 WARN_ON(PageDirty(p));
4723 SetPageUptodate(p);
4724 new->pages[i] = p;
David Sterbafba1acf2016-11-08 17:56:24 +01004725 copy_page(page_address(p), page_address(src->pages[i]));
Josef Bacikdb7f3432013-08-07 14:54:37 -04004726 }
4727
Josef Bacikdb7f3432013-08-07 14:54:37 -04004728 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4729 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4730
4731 return new;
4732}
4733
Omar Sandoval0f331222015-09-29 20:50:31 -07004734struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4735 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004736{
4737 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004738 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004739 unsigned long i;
4740
Omar Sandoval0f331222015-09-29 20:50:31 -07004741 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004742
4743 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004744 if (!eb)
4745 return NULL;
4746
4747 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004748 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004749 if (!eb->pages[i])
4750 goto err;
4751 }
4752 set_extent_buffer_uptodate(eb);
4753 btrfs_set_header_nritems(eb, 0);
4754 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4755
4756 return eb;
4757err:
4758 for (; i > 0; i--)
4759 __free_page(eb->pages[i - 1]);
4760 __free_extent_buffer(eb);
4761 return NULL;
4762}
4763
Omar Sandoval0f331222015-09-29 20:50:31 -07004764struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004765 u64 start)
Omar Sandoval0f331222015-09-29 20:50:31 -07004766{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004767 return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
Omar Sandoval0f331222015-09-29 20:50:31 -07004768}
4769
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004770static void check_buffer_tree_ref(struct extent_buffer *eb)
4771{
Chris Mason242e18c2013-01-29 17:49:37 -05004772 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004773 /* the ref bit is tricky. We have to make sure it is set
4774 * if we have the buffer dirty. Otherwise the
4775 * code to free a buffer can end up dropping a dirty
4776 * page
4777 *
4778 * Once the ref bit is set, it won't go away while the
4779 * buffer is dirty or in writeback, and it also won't
4780 * go away while we have the reference count on the
4781 * eb bumped.
4782 *
4783 * We can't just set the ref bit without bumping the
4784 * ref on the eb because free_extent_buffer might
4785 * see the ref bit and try to clear it. If this happens
4786 * free_extent_buffer might end up dropping our original
4787 * ref by mistake and freeing the page before we are able
4788 * to add one more ref.
4789 *
4790 * So bump the ref count first, then set the bit. If someone
4791 * beat us to it, drop the ref we added.
4792 */
Chris Mason242e18c2013-01-29 17:49:37 -05004793 refs = atomic_read(&eb->refs);
4794 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4795 return;
4796
Josef Bacik594831c2012-07-20 16:11:08 -04004797 spin_lock(&eb->refs_lock);
4798 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004799 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004800 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004801}
4802
Mel Gorman2457aec2014-06-04 16:10:31 -07004803static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4804 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004805{
4806 unsigned long num_pages, i;
4807
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004808 check_buffer_tree_ref(eb);
4809
Josef Bacik5df42352012-03-15 18:24:42 -04004810 num_pages = num_extent_pages(eb->start, eb->len);
4811 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004812 struct page *p = eb->pages[i];
4813
Mel Gorman2457aec2014-06-04 16:10:31 -07004814 if (p != accessed)
4815 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004816 }
4817}
4818
Josef Bacikf28491e2013-12-16 13:24:27 -05004819struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4820 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004821{
4822 struct extent_buffer *eb;
4823
4824 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004825 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004826 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004827 if (eb && atomic_inc_not_zero(&eb->refs)) {
4828 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004829 /*
4830 * Lock our eb's refs_lock to avoid races with
4831 * free_extent_buffer. When we get our eb it might be flagged
4832 * with EXTENT_BUFFER_STALE and another task running
4833 * free_extent_buffer might have seen that flag set,
4834 * eb->refs == 2, that the buffer isn't under IO (dirty and
4835 * writeback flags not set) and it's still in the tree (flag
4836 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4837 * of decrementing the extent buffer's reference count twice.
4838 * So here we could race and increment the eb's reference count,
4839 * clear its stale flag, mark it as dirty and drop our reference
4840 * before the other task finishes executing free_extent_buffer,
4841 * which would later result in an attempt to free an extent
4842 * buffer that is dirty.
4843 */
4844 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4845 spin_lock(&eb->refs_lock);
4846 spin_unlock(&eb->refs_lock);
4847 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004848 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004849 return eb;
4850 }
4851 rcu_read_unlock();
4852
4853 return NULL;
4854}
4855
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004856#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4857struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004858 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004859{
4860 struct extent_buffer *eb, *exists = NULL;
4861 int ret;
4862
4863 eb = find_extent_buffer(fs_info, start);
4864 if (eb)
4865 return eb;
Jeff Mahoneyda170662016-06-15 09:22:56 -04004866 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004867 if (!eb)
4868 return NULL;
4869 eb->fs_info = fs_info;
4870again:
David Sterbae1860a72016-05-09 14:11:38 +02004871 ret = radix_tree_preload(GFP_NOFS);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004872 if (ret)
4873 goto free_eb;
4874 spin_lock(&fs_info->buffer_lock);
4875 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004876 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004877 spin_unlock(&fs_info->buffer_lock);
4878 radix_tree_preload_end();
4879 if (ret == -EEXIST) {
4880 exists = find_extent_buffer(fs_info, start);
4881 if (exists)
4882 goto free_eb;
4883 else
4884 goto again;
4885 }
4886 check_buffer_tree_ref(eb);
4887 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4888
4889 /*
4890 * We will free dummy extent buffer's if they come into
4891 * free_extent_buffer with a ref count of 2, but if we are using this we
4892 * want the buffers to stay in memory until we're done with them, so
4893 * bump the ref count again.
4894 */
4895 atomic_inc(&eb->refs);
4896 return eb;
4897free_eb:
4898 btrfs_release_extent_buffer(eb);
4899 return exists;
4900}
4901#endif
4902
Josef Bacikf28491e2013-12-16 13:24:27 -05004903struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004904 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004905{
Jeff Mahoneyda170662016-06-15 09:22:56 -04004906 unsigned long len = fs_info->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004907 unsigned long num_pages = num_extent_pages(start, len);
4908 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004909 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004910 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004911 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004912 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004913 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004914 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004915 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004916
Jeff Mahoneyda170662016-06-15 09:22:56 -04004917 if (!IS_ALIGNED(start, fs_info->sectorsize)) {
Liu Boc871b0f2016-06-06 12:01:23 -07004918 btrfs_err(fs_info, "bad tree block start %llu", start);
4919 return ERR_PTR(-EINVAL);
4920 }
4921
Josef Bacikf28491e2013-12-16 13:24:27 -05004922 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004923 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004924 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004925
David Sterba23d79d82014-06-15 02:55:29 +02004926 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004927 if (!eb)
Liu Boc871b0f2016-06-06 12:01:23 -07004928 return ERR_PTR(-ENOMEM);
Chris Masond1310b22008-01-24 16:13:08 -05004929
Chris Mason727011e2010-08-06 13:21:20 -04004930 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004931 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Liu Boc871b0f2016-06-06 12:01:23 -07004932 if (!p) {
4933 exists = ERR_PTR(-ENOMEM);
Chris Mason6af118ce2008-07-22 11:18:07 -04004934 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004935 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004936
4937 spin_lock(&mapping->private_lock);
4938 if (PagePrivate(p)) {
4939 /*
4940 * We could have already allocated an eb for this page
4941 * and attached one so lets see if we can get a ref on
4942 * the existing eb, and if we can we know it's good and
4943 * we can just return that one, else we know we can just
4944 * overwrite page->private.
4945 */
4946 exists = (struct extent_buffer *)p->private;
4947 if (atomic_inc_not_zero(&exists->refs)) {
4948 spin_unlock(&mapping->private_lock);
4949 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004950 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004951 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004952 goto free_eb;
4953 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004954 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004955
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004956 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004957 * Do this so attach doesn't complain and we need to
4958 * drop the ref the old guy had.
4959 */
4960 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004961 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004962 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004963 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004964 attach_extent_buffer_page(eb, p);
4965 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004966 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004967 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004968 if (!PageUptodate(p))
4969 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004970
4971 /*
4972 * see below about how we avoid a nasty race with release page
4973 * and why we unlock later
4974 */
Chris Masond1310b22008-01-24 16:13:08 -05004975 }
4976 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004977 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004978again:
David Sterbae1860a72016-05-09 14:11:38 +02004979 ret = radix_tree_preload(GFP_NOFS);
Liu Boc871b0f2016-06-06 12:01:23 -07004980 if (ret) {
4981 exists = ERR_PTR(ret);
Miao Xie19fe0a82010-10-26 20:57:29 -04004982 goto free_eb;
Liu Boc871b0f2016-06-06 12:01:23 -07004983 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004984
Josef Bacikf28491e2013-12-16 13:24:27 -05004985 spin_lock(&fs_info->buffer_lock);
4986 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004987 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05004988 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004989 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004990 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004991 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004992 if (exists)
4993 goto free_eb;
4994 else
Josef Bacik115391d2012-03-09 09:51:43 -05004995 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004996 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004997 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004998 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004999 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005000
5001 /*
5002 * there is a race where release page may have
5003 * tried to find this extent buffer in the radix
5004 * but failed. It will tell the VM it is safe to
5005 * reclaim the, and it will clear the page private bit.
5006 * We must make sure to set the page private bit properly
5007 * after the extent buffer is in the radix tree so
5008 * it doesn't get lost
5009 */
Chris Mason727011e2010-08-06 13:21:20 -04005010 SetPageChecked(eb->pages[0]);
5011 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005012 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005013 ClearPageChecked(p);
5014 unlock_page(p);
5015 }
5016 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005017 return eb;
5018
Chris Mason6af118ce2008-07-22 11:18:07 -04005019free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005020 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005021 for (i = 0; i < num_pages; i++) {
5022 if (eb->pages[i])
5023 unlock_page(eb->pages[i]);
5024 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005025
Miao Xie897ca6e92010-10-26 20:57:29 -04005026 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005027 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005028}
Chris Masond1310b22008-01-24 16:13:08 -05005029
Josef Bacik3083ee22012-03-09 16:01:49 -05005030static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5031{
5032 struct extent_buffer *eb =
5033 container_of(head, struct extent_buffer, rcu_head);
5034
5035 __free_extent_buffer(eb);
5036}
5037
Josef Bacik3083ee22012-03-09 16:01:49 -05005038/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005039static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005040{
5041 WARN_ON(atomic_read(&eb->refs) == 0);
5042 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005043 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005044 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005045
Jan Schmidt815a51c2012-05-16 17:00:02 +02005046 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005047
Josef Bacikf28491e2013-12-16 13:24:27 -05005048 spin_lock(&fs_info->buffer_lock);
5049 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005050 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005051 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005052 } else {
5053 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005054 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005055
5056 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005057 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005058#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5059 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5060 __free_extent_buffer(eb);
5061 return 1;
5062 }
5063#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005064 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005065 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005066 }
5067 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005068
5069 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005070}
5071
Chris Masond1310b22008-01-24 16:13:08 -05005072void free_extent_buffer(struct extent_buffer *eb)
5073{
Chris Mason242e18c2013-01-29 17:49:37 -05005074 int refs;
5075 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005076 if (!eb)
5077 return;
5078
Chris Mason242e18c2013-01-29 17:49:37 -05005079 while (1) {
5080 refs = atomic_read(&eb->refs);
5081 if (refs <= 3)
5082 break;
5083 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5084 if (old == refs)
5085 return;
5086 }
5087
Josef Bacik3083ee22012-03-09 16:01:49 -05005088 spin_lock(&eb->refs_lock);
5089 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005090 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5091 atomic_dec(&eb->refs);
5092
5093 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005094 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005095 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005096 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5097 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005098
Josef Bacik3083ee22012-03-09 16:01:49 -05005099 /*
5100 * I know this is terrible, but it's temporary until we stop tracking
5101 * the uptodate bits and such for the extent buffers.
5102 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005103 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005104}
Chris Masond1310b22008-01-24 16:13:08 -05005105
Josef Bacik3083ee22012-03-09 16:01:49 -05005106void free_extent_buffer_stale(struct extent_buffer *eb)
5107{
5108 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005109 return;
5110
Josef Bacik3083ee22012-03-09 16:01:49 -05005111 spin_lock(&eb->refs_lock);
5112 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5113
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005114 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005115 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5116 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005117 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005118}
5119
Chris Mason1d4284b2012-03-28 20:31:37 -04005120void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005121{
Chris Masond1310b22008-01-24 16:13:08 -05005122 unsigned long i;
5123 unsigned long num_pages;
5124 struct page *page;
5125
Chris Masond1310b22008-01-24 16:13:08 -05005126 num_pages = num_extent_pages(eb->start, eb->len);
5127
5128 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005129 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005130 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005131 continue;
5132
Chris Masona61e6f22008-07-22 11:18:08 -04005133 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005134 WARN_ON(!PagePrivate(page));
5135
Chris Masond1310b22008-01-24 16:13:08 -05005136 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005137 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005138 if (!PageDirty(page)) {
5139 radix_tree_tag_clear(&page->mapping->page_tree,
5140 page_index(page),
5141 PAGECACHE_TAG_DIRTY);
5142 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005143 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005144 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005145 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005146 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005147 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005148}
Chris Masond1310b22008-01-24 16:13:08 -05005149
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005150int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005151{
5152 unsigned long i;
5153 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005154 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005155
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005156 check_buffer_tree_ref(eb);
5157
Chris Masonb9473432009-03-13 11:00:37 -04005158 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005159
Chris Masond1310b22008-01-24 16:13:08 -05005160 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005161 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005162 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5163
Chris Masonb9473432009-03-13 11:00:37 -04005164 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005165 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005166 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005167}
Chris Masond1310b22008-01-24 16:13:08 -05005168
David Sterba69ba3922015-12-03 13:08:59 +01005169void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005170{
5171 unsigned long i;
5172 struct page *page;
5173 unsigned long num_pages;
5174
Chris Masonb4ce94d2009-02-04 09:25:08 -05005175 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005176 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005177 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005178 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005179 if (page)
5180 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005181 }
Chris Mason1259ab72008-05-12 13:39:03 -04005182}
5183
David Sterba09c25a82015-12-03 13:08:59 +01005184void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005185{
5186 unsigned long i;
5187 struct page *page;
5188 unsigned long num_pages;
5189
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005190 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005191 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005192 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005193 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005194 SetPageUptodate(page);
5195 }
Chris Masond1310b22008-01-24 16:13:08 -05005196}
Chris Masond1310b22008-01-24 16:13:08 -05005197
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005198int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005199{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005200 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005201}
Chris Masond1310b22008-01-24 16:13:08 -05005202
5203int read_extent_buffer_pages(struct extent_io_tree *tree,
Josef Bacik8436ea912016-09-02 15:40:03 -04005204 struct extent_buffer *eb, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005205 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005206{
5207 unsigned long i;
Chris Masond1310b22008-01-24 16:13:08 -05005208 struct page *page;
5209 int err;
5210 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005211 int locked_pages = 0;
5212 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005213 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005214 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005215 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005216 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005217
Chris Masonb4ce94d2009-02-04 09:25:08 -05005218 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005219 return 0;
5220
Chris Masond1310b22008-01-24 16:13:08 -05005221 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik8436ea912016-09-02 15:40:03 -04005222 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005223 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005224 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005225 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005226 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005227 } else {
5228 lock_page(page);
5229 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005230 locked_pages++;
Liu Bo2571e732016-08-03 12:33:01 -07005231 }
5232 /*
5233 * We need to firstly lock all pages to make sure that
5234 * the uptodate bit of our pages won't be affected by
5235 * clear_extent_buffer_uptodate().
5236 */
Josef Bacik8436ea912016-09-02 15:40:03 -04005237 for (i = 0; i < num_pages; i++) {
Liu Bo2571e732016-08-03 12:33:01 -07005238 page = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005239 if (!PageUptodate(page)) {
5240 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005241 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005242 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005243 }
Liu Bo2571e732016-08-03 12:33:01 -07005244
Chris Masonce9adaa2008-04-09 16:28:12 -04005245 if (all_uptodate) {
Josef Bacik8436ea912016-09-02 15:40:03 -04005246 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005247 goto unlock_exit;
5248 }
5249
Filipe Manana656f30d2014-09-26 12:25:56 +01005250 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005251 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005252 atomic_set(&eb->io_pages, num_reads);
Josef Bacik8436ea912016-09-02 15:40:03 -04005253 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005254 page = eb->pages[i];
Liu Bobaf863b2016-07-11 10:39:07 -07005255
Chris Masonce9adaa2008-04-09 16:28:12 -04005256 if (!PageUptodate(page)) {
Liu Bobaf863b2016-07-11 10:39:07 -07005257 if (ret) {
5258 atomic_dec(&eb->io_pages);
5259 unlock_page(page);
5260 continue;
5261 }
5262
Chris Masonf1885912008-04-09 16:28:12 -04005263 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005264 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005265 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005266 mirror_num, &bio_flags,
Mike Christie1f7ad752016-06-05 14:31:51 -05005267 REQ_META);
Liu Bobaf863b2016-07-11 10:39:07 -07005268 if (err) {
Chris Masond1310b22008-01-24 16:13:08 -05005269 ret = err;
Liu Bobaf863b2016-07-11 10:39:07 -07005270 /*
5271 * We use &bio in above __extent_read_full_page,
5272 * so we ensure that if it returns error, the
5273 * current page fails to add itself to bio and
5274 * it's been unlocked.
5275 *
5276 * We must dec io_pages by ourselves.
5277 */
5278 atomic_dec(&eb->io_pages);
5279 }
Chris Masond1310b22008-01-24 16:13:08 -05005280 } else {
5281 unlock_page(page);
5282 }
5283 }
5284
Jeff Mahoney355808c2011-10-03 23:23:14 -04005285 if (bio) {
Mike Christie1f7ad752016-06-05 14:31:51 -05005286 err = submit_one_bio(bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005287 if (err)
5288 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005289 }
Chris Masona86c12c2008-02-07 10:50:54 -05005290
Arne Jansenbb82ab82011-06-10 14:06:53 +02005291 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005292 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005293
Josef Bacik8436ea912016-09-02 15:40:03 -04005294 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005295 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005296 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005297 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005298 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005299 }
Chris Masond3977122009-01-05 21:25:51 -05005300
Chris Masond1310b22008-01-24 16:13:08 -05005301 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005302
5303unlock_exit:
Chris Masond3977122009-01-05 21:25:51 -05005304 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04005305 locked_pages--;
Josef Bacik8436ea912016-09-02 15:40:03 -04005306 page = eb->pages[locked_pages];
5307 unlock_page(page);
Chris Masonce9adaa2008-04-09 16:28:12 -04005308 }
5309 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005310}
Chris Masond1310b22008-01-24 16:13:08 -05005311
5312void read_extent_buffer(struct extent_buffer *eb, void *dstv,
5313 unsigned long start,
5314 unsigned long len)
5315{
5316 size_t cur;
5317 size_t offset;
5318 struct page *page;
5319 char *kaddr;
5320 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005321 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5322 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005323
5324 WARN_ON(start > eb->len);
5325 WARN_ON(start + len > eb->start + eb->len);
5326
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005327 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005328
Chris Masond3977122009-01-05 21:25:51 -05005329 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005330 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005331
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005332 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005333 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005334 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005335
5336 dst += cur;
5337 len -= cur;
5338 offset = 0;
5339 i++;
5340 }
5341}
Chris Masond1310b22008-01-24 16:13:08 -05005342
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005343int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
5344 unsigned long start,
5345 unsigned long len)
5346{
5347 size_t cur;
5348 size_t offset;
5349 struct page *page;
5350 char *kaddr;
5351 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005352 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5353 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005354 int ret = 0;
5355
5356 WARN_ON(start > eb->len);
5357 WARN_ON(start + len > eb->start + eb->len);
5358
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005359 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005360
5361 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005362 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005363
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005364 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005365 kaddr = page_address(page);
5366 if (copy_to_user(dst, kaddr + offset, cur)) {
5367 ret = -EFAULT;
5368 break;
5369 }
5370
5371 dst += cur;
5372 len -= cur;
5373 offset = 0;
5374 i++;
5375 }
5376
5377 return ret;
5378}
5379
Liu Bo415b35a2016-06-17 19:16:21 -07005380/*
5381 * return 0 if the item is found within a page.
5382 * return 1 if the item spans two pages.
5383 * return -EINVAL otherwise.
5384 */
Chris Masond1310b22008-01-24 16:13:08 -05005385int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04005386 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05005387 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04005388 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005389{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005390 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005391 char *kaddr;
5392 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005393 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5394 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005395 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005396 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005397
5398 if (i != end_i)
Liu Bo415b35a2016-06-17 19:16:21 -07005399 return 1;
Chris Masond1310b22008-01-24 16:13:08 -05005400
5401 if (i == 0) {
5402 offset = start_offset;
5403 *map_start = 0;
5404 } else {
5405 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005406 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005407 }
Chris Masond3977122009-01-05 21:25:51 -05005408
Chris Masond1310b22008-01-24 16:13:08 -05005409 if (start + min_len > eb->len) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005410 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005411 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04005412 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005413 }
5414
David Sterbafb85fc92014-07-31 01:03:53 +02005415 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005416 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005417 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005418 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005419 return 0;
5420}
Chris Masond1310b22008-01-24 16:13:08 -05005421
Chris Masond1310b22008-01-24 16:13:08 -05005422int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
5423 unsigned long start,
5424 unsigned long len)
5425{
5426 size_t cur;
5427 size_t offset;
5428 struct page *page;
5429 char *kaddr;
5430 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005431 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5432 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005433 int ret = 0;
5434
5435 WARN_ON(start > eb->len);
5436 WARN_ON(start + len > eb->start + eb->len);
5437
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005438 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005439
Chris Masond3977122009-01-05 21:25:51 -05005440 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005441 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005442
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005443 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005444
Chris Masona6591712011-07-19 12:04:14 -04005445 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005446 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005447 if (ret)
5448 break;
5449
5450 ptr += cur;
5451 len -= cur;
5452 offset = 0;
5453 i++;
5454 }
5455 return ret;
5456}
Chris Masond1310b22008-01-24 16:13:08 -05005457
David Sterbaf157bf72016-11-09 17:43:38 +01005458void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
5459 const void *srcv)
5460{
5461 char *kaddr;
5462
5463 WARN_ON(!PageUptodate(eb->pages[0]));
5464 kaddr = page_address(eb->pages[0]);
5465 memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
5466 BTRFS_FSID_SIZE);
5467}
5468
5469void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
5470{
5471 char *kaddr;
5472
5473 WARN_ON(!PageUptodate(eb->pages[0]));
5474 kaddr = page_address(eb->pages[0]);
5475 memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
5476 BTRFS_FSID_SIZE);
5477}
5478
Chris Masond1310b22008-01-24 16:13:08 -05005479void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5480 unsigned long start, unsigned long len)
5481{
5482 size_t cur;
5483 size_t offset;
5484 struct page *page;
5485 char *kaddr;
5486 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005487 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5488 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005489
5490 WARN_ON(start > eb->len);
5491 WARN_ON(start + len > eb->start + eb->len);
5492
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005493 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005494
Chris Masond3977122009-01-05 21:25:51 -05005495 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005496 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005497 WARN_ON(!PageUptodate(page));
5498
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005499 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005500 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005501 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005502
5503 src += cur;
5504 len -= cur;
5505 offset = 0;
5506 i++;
5507 }
5508}
Chris Masond1310b22008-01-24 16:13:08 -05005509
David Sterbab159fa22016-11-08 18:09:03 +01005510void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
5511 unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05005512{
5513 size_t cur;
5514 size_t offset;
5515 struct page *page;
5516 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005517 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5518 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005519
5520 WARN_ON(start > eb->len);
5521 WARN_ON(start + len > eb->start + eb->len);
5522
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005523 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005524
Chris Masond3977122009-01-05 21:25:51 -05005525 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005526 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005527 WARN_ON(!PageUptodate(page));
5528
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005529 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005530 kaddr = page_address(page);
David Sterbab159fa22016-11-08 18:09:03 +01005531 memset(kaddr + offset, 0, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005532
5533 len -= cur;
5534 offset = 0;
5535 i++;
5536 }
5537}
Chris Masond1310b22008-01-24 16:13:08 -05005538
David Sterba58e80122016-11-08 18:30:31 +01005539void copy_extent_buffer_full(struct extent_buffer *dst,
5540 struct extent_buffer *src)
5541{
5542 int i;
5543 unsigned num_pages;
5544
5545 ASSERT(dst->len == src->len);
5546
5547 num_pages = num_extent_pages(dst->start, dst->len);
5548 for (i = 0; i < num_pages; i++)
5549 copy_page(page_address(dst->pages[i]),
5550 page_address(src->pages[i]));
5551}
5552
Chris Masond1310b22008-01-24 16:13:08 -05005553void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5554 unsigned long dst_offset, unsigned long src_offset,
5555 unsigned long len)
5556{
5557 u64 dst_len = dst->len;
5558 size_t cur;
5559 size_t offset;
5560 struct page *page;
5561 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005562 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5563 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005564
5565 WARN_ON(src->len != dst_len);
5566
5567 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005568 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005569
Chris Masond3977122009-01-05 21:25:51 -05005570 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005571 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005572 WARN_ON(!PageUptodate(page));
5573
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005574 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005575
Chris Masona6591712011-07-19 12:04:14 -04005576 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005577 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005578
5579 src_offset += cur;
5580 len -= cur;
5581 offset = 0;
5582 i++;
5583 }
5584}
Chris Masond1310b22008-01-24 16:13:08 -05005585
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005586void le_bitmap_set(u8 *map, unsigned int start, int len)
5587{
5588 u8 *p = map + BIT_BYTE(start);
5589 const unsigned int size = start + len;
5590 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5591 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
5592
5593 while (len - bits_to_set >= 0) {
5594 *p |= mask_to_set;
5595 len -= bits_to_set;
5596 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005597 mask_to_set = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005598 p++;
5599 }
5600 if (len) {
5601 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5602 *p |= mask_to_set;
5603 }
5604}
5605
5606void le_bitmap_clear(u8 *map, unsigned int start, int len)
5607{
5608 u8 *p = map + BIT_BYTE(start);
5609 const unsigned int size = start + len;
5610 int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
5611 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
5612
5613 while (len - bits_to_clear >= 0) {
5614 *p &= ~mask_to_clear;
5615 len -= bits_to_clear;
5616 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005617 mask_to_clear = ~0;
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005618 p++;
5619 }
5620 if (len) {
5621 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5622 *p &= ~mask_to_clear;
5623 }
5624}
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005625
5626/*
5627 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5628 * given bit number
5629 * @eb: the extent buffer
5630 * @start: offset of the bitmap item in the extent buffer
5631 * @nr: bit number
5632 * @page_index: return index of the page in the extent buffer that contains the
5633 * given bit number
5634 * @page_offset: return offset into the page given by page_index
5635 *
5636 * This helper hides the ugliness of finding the byte in an extent buffer which
5637 * contains a given bit.
5638 */
5639static inline void eb_bitmap_offset(struct extent_buffer *eb,
5640 unsigned long start, unsigned long nr,
5641 unsigned long *page_index,
5642 size_t *page_offset)
5643{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005644 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005645 size_t byte_offset = BIT_BYTE(nr);
5646 size_t offset;
5647
5648 /*
5649 * The byte we want is the offset of the extent buffer + the offset of
5650 * the bitmap item in the extent buffer + the offset of the byte in the
5651 * bitmap item.
5652 */
5653 offset = start_offset + start + byte_offset;
5654
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005655 *page_index = offset >> PAGE_SHIFT;
5656 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005657}
5658
5659/**
5660 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5661 * @eb: the extent buffer
5662 * @start: offset of the bitmap item in the extent buffer
5663 * @nr: bit number to test
5664 */
5665int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5666 unsigned long nr)
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
5673 eb_bitmap_offset(eb, start, nr, &i, &offset);
5674 page = eb->pages[i];
5675 WARN_ON(!PageUptodate(page));
5676 kaddr = page_address(page);
5677 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5678}
5679
5680/**
5681 * extent_buffer_bitmap_set - set an area of a bitmap
5682 * @eb: the extent buffer
5683 * @start: offset of the bitmap item in the extent buffer
5684 * @pos: bit number of the first bit
5685 * @len: number of bits to set
5686 */
5687void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5688 unsigned long pos, unsigned long len)
5689{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005690 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005691 struct page *page;
5692 unsigned long i;
5693 size_t offset;
5694 const unsigned int size = pos + len;
5695 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005696 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005697
5698 eb_bitmap_offset(eb, start, pos, &i, &offset);
5699 page = eb->pages[i];
5700 WARN_ON(!PageUptodate(page));
5701 kaddr = page_address(page);
5702
5703 while (len >= bits_to_set) {
5704 kaddr[offset] |= mask_to_set;
5705 len -= bits_to_set;
5706 bits_to_set = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005707 mask_to_set = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005708 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005709 offset = 0;
5710 page = eb->pages[++i];
5711 WARN_ON(!PageUptodate(page));
5712 kaddr = page_address(page);
5713 }
5714 }
5715 if (len) {
5716 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5717 kaddr[offset] |= mask_to_set;
5718 }
5719}
5720
5721
5722/**
5723 * extent_buffer_bitmap_clear - clear an area of a bitmap
5724 * @eb: the extent buffer
5725 * @start: offset of the bitmap item in the extent buffer
5726 * @pos: bit number of the first bit
5727 * @len: number of bits to clear
5728 */
5729void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5730 unsigned long pos, unsigned long len)
5731{
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005732 u8 *kaddr;
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005733 struct page *page;
5734 unsigned long i;
5735 size_t offset;
5736 const unsigned int size = pos + len;
5737 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
Omar Sandoval2fe1d552016-09-22 17:24:20 -07005738 u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005739
5740 eb_bitmap_offset(eb, start, pos, &i, &offset);
5741 page = eb->pages[i];
5742 WARN_ON(!PageUptodate(page));
5743 kaddr = page_address(page);
5744
5745 while (len >= bits_to_clear) {
5746 kaddr[offset] &= ~mask_to_clear;
5747 len -= bits_to_clear;
5748 bits_to_clear = BITS_PER_BYTE;
Dan Carpenter9c894692016-10-12 11:33:21 +03005749 mask_to_clear = ~0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005750 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005751 offset = 0;
5752 page = eb->pages[++i];
5753 WARN_ON(!PageUptodate(page));
5754 kaddr = page_address(page);
5755 }
5756 }
5757 if (len) {
5758 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5759 kaddr[offset] &= ~mask_to_clear;
5760 }
5761}
5762
Sergei Trofimovich33872062011-04-11 21:52:52 +00005763static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5764{
5765 unsigned long distance = (src > dst) ? src - dst : dst - src;
5766 return distance < len;
5767}
5768
Chris Masond1310b22008-01-24 16:13:08 -05005769static void copy_pages(struct page *dst_page, struct page *src_page,
5770 unsigned long dst_off, unsigned long src_off,
5771 unsigned long len)
5772{
Chris Masona6591712011-07-19 12:04:14 -04005773 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005774 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005775 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005776
Sergei Trofimovich33872062011-04-11 21:52:52 +00005777 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005778 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005779 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005780 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005781 if (areas_overlap(src_off, dst_off, len))
5782 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005783 }
Chris Masond1310b22008-01-24 16:13:08 -05005784
Chris Mason727011e2010-08-06 13:21:20 -04005785 if (must_memmove)
5786 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5787 else
5788 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005789}
5790
5791void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5792 unsigned long src_offset, unsigned long len)
5793{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005794 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005795 size_t cur;
5796 size_t dst_off_in_page;
5797 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005798 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005799 unsigned long dst_i;
5800 unsigned long src_i;
5801
5802 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005803 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005804 "memmove bogus src_offset %lu move len %lu dst len %lu",
5805 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005806 BUG_ON(1);
5807 }
5808 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005809 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005810 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5811 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005812 BUG_ON(1);
5813 }
5814
Chris Masond3977122009-01-05 21:25:51 -05005815 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005816 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005817 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005818 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005819 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005820
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005821 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5822 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005823
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005824 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005825 src_off_in_page));
5826 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005827 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005828
David Sterbafb85fc92014-07-31 01:03:53 +02005829 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005830 dst_off_in_page, src_off_in_page, cur);
5831
5832 src_offset += cur;
5833 dst_offset += cur;
5834 len -= cur;
5835 }
5836}
Chris Masond1310b22008-01-24 16:13:08 -05005837
5838void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5839 unsigned long src_offset, unsigned long len)
5840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005841 struct btrfs_fs_info *fs_info = dst->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05005842 size_t cur;
5843 size_t dst_off_in_page;
5844 size_t src_off_in_page;
5845 unsigned long dst_end = dst_offset + len - 1;
5846 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005847 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005848 unsigned long dst_i;
5849 unsigned long src_i;
5850
5851 if (src_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005852 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005853 "memmove bogus src_offset %lu move len %lu len %lu",
5854 src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005855 BUG_ON(1);
5856 }
5857 if (dst_offset + len > dst->len) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005858 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005859 "memmove bogus dst_offset %lu move len %lu len %lu",
5860 dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005861 BUG_ON(1);
5862 }
Chris Mason727011e2010-08-06 13:21:20 -04005863 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005864 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5865 return;
5866 }
Chris Masond3977122009-01-05 21:25:51 -05005867 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005868 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5869 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005870
5871 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005872 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005873 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005874 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005875
5876 cur = min_t(unsigned long, len, src_off_in_page + 1);
5877 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005878 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005879 dst_off_in_page - cur + 1,
5880 src_off_in_page - cur + 1, cur);
5881
5882 dst_end -= cur;
5883 src_end -= cur;
5884 len -= cur;
5885 }
5886}
Chris Mason6af118ce2008-07-22 11:18:07 -04005887
David Sterbaf7a52a42013-04-26 14:56:29 +00005888int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005889{
Chris Mason6af118ce2008-07-22 11:18:07 -04005890 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005891
Miao Xie19fe0a82010-10-26 20:57:29 -04005892 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005893 * We need to make sure nobody is attaching this page to an eb right
Josef Bacik3083ee22012-03-09 16:01:49 -05005894 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005895 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005896 spin_lock(&page->mapping->private_lock);
5897 if (!PagePrivate(page)) {
5898 spin_unlock(&page->mapping->private_lock);
5899 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005900 }
5901
Josef Bacik3083ee22012-03-09 16:01:49 -05005902 eb = (struct extent_buffer *)page->private;
5903 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005904
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005905 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005906 * This is a little awful but should be ok, we need to make sure that
5907 * the eb doesn't disappear out from under us while we're looking at
5908 * this page.
5909 */
5910 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005911 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005912 spin_unlock(&eb->refs_lock);
5913 spin_unlock(&page->mapping->private_lock);
5914 return 0;
5915 }
5916 spin_unlock(&page->mapping->private_lock);
5917
Josef Bacik3083ee22012-03-09 16:01:49 -05005918 /*
5919 * If tree ref isn't set then we know the ref on this eb is a real ref,
5920 * so just return, this page will likely be freed soon anyway.
5921 */
5922 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5923 spin_unlock(&eb->refs_lock);
5924 return 0;
5925 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005926
David Sterbaf7a52a42013-04-26 14:56:29 +00005927 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005928}