blob: 0c9b11924f7457863a4dec3217c0014dab49742e [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/spinlock.h>
8#include <linux/blkdev.h>
9#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050010#include <linux/writeback.h>
11#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070012#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020018#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040020#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040021#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080022#include "backref.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
Chris Mason9be33952013-05-17 18:30:14 -040026static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050027
Filipe Manana27a35072014-07-06 20:09:59 +010028static inline bool extent_state_in_tree(const struct extent_state *state)
29{
30 return !RB_EMPTY_NODE(&state->rb_node);
31}
32
Eric Sandeen6d49ba12013-04-22 16:12:31 +000033#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050034static LIST_HEAD(buffers);
35static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040036
Chris Masond3977122009-01-05 21:25:51 -050037static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000038
39static inline
40void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
41{
42 unsigned long flags;
43
44 spin_lock_irqsave(&leak_lock, flags);
45 list_add(new, head);
46 spin_unlock_irqrestore(&leak_lock, flags);
47}
48
49static inline
50void btrfs_leak_debug_del(struct list_head *entry)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&leak_lock, flags);
55 list_del(entry);
56 spin_unlock_irqrestore(&leak_lock, flags);
57}
58
59static inline
60void btrfs_leak_debug_check(void)
61{
62 struct extent_state *state;
63 struct extent_buffer *eb;
64
65 while (!list_empty(&states)) {
66 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010067 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010068 state->start, state->end, state->state,
69 extent_state_in_tree(state),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020070 atomic_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000071 list_del(&state->leak_list);
72 kmem_cache_free(extent_state_cache, state);
73 }
74
75 while (!list_empty(&buffers)) {
76 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Frank Holtonefe120a2013-12-20 11:37:06 -050077 printk(KERN_ERR "BTRFS: buffer leak start %llu len %lu "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020078 "refs %d\n",
79 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]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200101 caller, btrfs_ino(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
130 /* tells the submit_bio code to use a WRITE_SYNC */
131 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,
166 SLAB_RECLAIM_ACCOUNT | 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,
172 SLAB_RECLAIM_ACCOUNT | 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
229 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400230 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500231 return state;
232 state->state = 0;
David Sterba47dc1962016-02-11 13:24:13 +0100233 state->failrec = NULL;
Filipe Manana27a35072014-07-06 20:09:59 +0100234 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000235 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500236 atomic_set(&state->refs, 1);
237 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100238 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500239 return state;
240}
Chris Masond1310b22008-01-24 16:13:08 -0500241
Chris Mason4845e442010-05-25 20:56:50 -0400242void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500243{
Chris Masond1310b22008-01-24 16:13:08 -0500244 if (!state)
245 return;
246 if (atomic_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100247 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000248 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100249 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500250 kmem_cache_free(extent_state_cache, state);
251 }
252}
Chris Masond1310b22008-01-24 16:13:08 -0500253
Filipe Mananaf2071b22014-02-12 15:05:53 +0000254static struct rb_node *tree_insert(struct rb_root *root,
255 struct rb_node *search_start,
256 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000257 struct rb_node *node,
258 struct rb_node ***p_in,
259 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500260{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000261 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500262 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500263 struct tree_entry *entry;
264
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000265 if (p_in && parent_in) {
266 p = *p_in;
267 parent = *parent_in;
268 goto do_insert;
269 }
270
Filipe Mananaf2071b22014-02-12 15:05:53 +0000271 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500272 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500273 parent = *p;
274 entry = rb_entry(parent, struct tree_entry, rb_node);
275
276 if (offset < entry->start)
277 p = &(*p)->rb_left;
278 else if (offset > entry->end)
279 p = &(*p)->rb_right;
280 else
281 return parent;
282 }
283
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000284do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500285 rb_link_node(node, parent, p);
286 rb_insert_color(node, root);
287 return NULL;
288}
289
Chris Mason80ea96b2008-02-01 14:51:59 -0500290static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000291 struct rb_node **prev_ret,
292 struct rb_node **next_ret,
293 struct rb_node ***p_ret,
294 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500295{
Chris Mason80ea96b2008-02-01 14:51:59 -0500296 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000297 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500298 struct rb_node *prev = NULL;
299 struct rb_node *orig_prev = NULL;
300 struct tree_entry *entry;
301 struct tree_entry *prev_entry = NULL;
302
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000303 while (*n) {
304 prev = *n;
305 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500306 prev_entry = entry;
307
308 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000309 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500310 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000311 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500312 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000313 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500314 }
315
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000316 if (p_ret)
317 *p_ret = n;
318 if (parent_ret)
319 *parent_ret = prev;
320
Chris Masond1310b22008-01-24 16:13:08 -0500321 if (prev_ret) {
322 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500323 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500324 prev = rb_next(prev);
325 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
326 }
327 *prev_ret = prev;
328 prev = orig_prev;
329 }
330
331 if (next_ret) {
332 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500333 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500334 prev = rb_prev(prev);
335 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
336 }
337 *next_ret = prev;
338 }
339 return NULL;
340}
341
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000342static inline struct rb_node *
343tree_search_for_insert(struct extent_io_tree *tree,
344 u64 offset,
345 struct rb_node ***p_ret,
346 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500347{
Chris Mason70dec802008-01-29 09:59:12 -0500348 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500349 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500350
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000351 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500352 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500353 return prev;
354 return ret;
355}
356
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000357static inline struct rb_node *tree_search(struct extent_io_tree *tree,
358 u64 offset)
359{
360 return tree_search_for_insert(tree, offset, NULL, NULL);
361}
362
Josef Bacik9ed74f22009-09-11 16:12:44 -0400363static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
364 struct extent_state *other)
365{
366 if (tree->ops && tree->ops->merge_extent_hook)
367 tree->ops->merge_extent_hook(tree->mapping->host, new,
368 other);
369}
370
Chris Masond1310b22008-01-24 16:13:08 -0500371/*
372 * utility function to look for merge candidates inside a given range.
373 * Any extents with matching state are merged together into a single
374 * extent in the tree. Extents with EXTENT_IO in their state field
375 * are not merged because the end_io handlers need to be able to do
376 * operations on them without sleeping (or doing allocations/splits).
377 *
378 * This should be called with the tree lock held.
379 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000380static void merge_state(struct extent_io_tree *tree,
381 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500382{
383 struct extent_state *other;
384 struct rb_node *other_node;
385
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400386 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000387 return;
Chris Masond1310b22008-01-24 16:13:08 -0500388
389 other_node = rb_prev(&state->rb_node);
390 if (other_node) {
391 other = rb_entry(other_node, struct extent_state, rb_node);
392 if (other->end == state->start - 1 &&
393 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400394 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500395 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500396 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100397 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500398 free_extent_state(other);
399 }
400 }
401 other_node = rb_next(&state->rb_node);
402 if (other_node) {
403 other = rb_entry(other_node, struct extent_state, rb_node);
404 if (other->start == state->end + 1 &&
405 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400406 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400407 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400408 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100409 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400410 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500411 }
412 }
Chris Masond1310b22008-01-24 16:13:08 -0500413}
414
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000415static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100416 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500417{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000418 if (tree->ops && tree->ops->set_bit_hook)
419 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500420}
421
422static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100423 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500424{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400425 if (tree->ops && tree->ops->clear_bit_hook)
426 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500427}
428
Xiao Guangrong3150b692011-07-14 03:19:08 +0000429static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800430 struct extent_state *state, unsigned *bits,
431 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000432
Chris Masond1310b22008-01-24 16:13:08 -0500433/*
434 * insert an extent_state struct into the tree. 'bits' are set on the
435 * struct before it is inserted.
436 *
437 * This may return -EEXIST if the extent is already there, in which case the
438 * state struct is freed.
439 *
440 * The tree lock is not taken internally. This is a utility function and
441 * probably isn't what you want to call (see set/clear_extent_bit).
442 */
443static int insert_state(struct extent_io_tree *tree,
444 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000445 struct rb_node ***p,
446 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800447 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500448{
449 struct rb_node *node;
450
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000451 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500452 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200453 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500454 state->start = start;
455 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400456
Qu Wenruod38ed272015-10-12 14:53:37 +0800457 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000458
Filipe Mananaf2071b22014-02-12 15:05:53 +0000459 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500460 if (node) {
461 struct extent_state *found;
462 found = rb_entry(node, struct extent_state, rb_node);
Frank Holtonefe120a2013-12-20 11:37:06 -0500463 printk(KERN_ERR "BTRFS: found node %llu %llu on insert of "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200464 "%llu %llu\n",
465 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500466 return -EEXIST;
467 }
468 merge_state(tree, state);
469 return 0;
470}
471
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000472static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400473 u64 split)
474{
475 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000476 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400477}
478
Chris Masond1310b22008-01-24 16:13:08 -0500479/*
480 * split a given extent state struct in two, inserting the preallocated
481 * struct 'prealloc' as the newly created second half. 'split' indicates an
482 * offset inside 'orig' where it should be split.
483 *
484 * Before calling,
485 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
486 * are two extent state structs in the tree:
487 * prealloc: [orig->start, split - 1]
488 * orig: [ split, orig->end ]
489 *
490 * The tree locks are not taken by this function. They need to be held
491 * by the caller.
492 */
493static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
494 struct extent_state *prealloc, u64 split)
495{
496 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400497
498 split_cb(tree, orig, split);
499
Chris Masond1310b22008-01-24 16:13:08 -0500500 prealloc->start = orig->start;
501 prealloc->end = split - 1;
502 prealloc->state = orig->state;
503 orig->start = split;
504
Filipe Mananaf2071b22014-02-12 15:05:53 +0000505 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
506 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500507 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500508 free_extent_state(prealloc);
509 return -EEXIST;
510 }
511 return 0;
512}
513
Li Zefancdc6a392012-03-12 16:39:48 +0800514static struct extent_state *next_state(struct extent_state *state)
515{
516 struct rb_node *next = rb_next(&state->rb_node);
517 if (next)
518 return rb_entry(next, struct extent_state, rb_node);
519 else
520 return NULL;
521}
522
Chris Masond1310b22008-01-24 16:13:08 -0500523/*
524 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800525 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500526 *
527 * If no bits are set on the state struct after clearing things, the
528 * struct is freed and removed from the tree
529 */
Li Zefancdc6a392012-03-12 16:39:48 +0800530static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
531 struct extent_state *state,
Qu Wenruofefdc552015-10-12 15:35:38 +0800532 unsigned *bits, int wake,
533 struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500534{
Li Zefancdc6a392012-03-12 16:39:48 +0800535 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100536 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500537
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400538 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500539 u64 range = state->end - state->start + 1;
540 WARN_ON(range > tree->dirty_bytes);
541 tree->dirty_bytes -= range;
542 }
Chris Mason291d6732008-01-29 15:55:23 -0500543 clear_state_cb(tree, state, bits);
Qu Wenruofefdc552015-10-12 15:35:38 +0800544 add_extent_changeset(state, bits_to_clear, changeset, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -0400545 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500546 if (wake)
547 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400548 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800549 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100550 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500551 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100552 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500553 free_extent_state(state);
554 } else {
555 WARN_ON(1);
556 }
557 } else {
558 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800559 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500560 }
Li Zefancdc6a392012-03-12 16:39:48 +0800561 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500562}
563
Xiao Guangrong82337672011-04-20 06:44:57 +0000564static struct extent_state *
565alloc_extent_state_atomic(struct extent_state *prealloc)
566{
567 if (!prealloc)
568 prealloc = alloc_extent_state(GFP_ATOMIC);
569
570 return prealloc;
571}
572
Eric Sandeen48a3b632013-04-25 20:41:01 +0000573static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400574{
575 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
576 "Extent tree was modified by another "
577 "thread while locked.");
578}
579
Chris Masond1310b22008-01-24 16:13:08 -0500580/*
581 * clear some bits on a range in the tree. This may require splitting
582 * or inserting elements in the tree, so the gfp mask is used to
583 * indicate which allocations or sleeping are allowed.
584 *
585 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
586 * the given range from the tree regardless of state (ie for truncate).
587 *
588 * the range [start, end] is inclusive.
589 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100590 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500591 */
Qu Wenruofefdc552015-10-12 15:35:38 +0800592static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
593 unsigned bits, int wake, int delete,
594 struct extent_state **cached_state,
595 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500596{
597 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400598 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500599 struct extent_state *prealloc = NULL;
600 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400601 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500602 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000603 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500604
Josef Bacika5dee372013-12-13 10:02:44 -0500605 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000606
Josef Bacik7ee9e442013-06-21 16:37:03 -0400607 if (bits & EXTENT_DELALLOC)
608 bits |= EXTENT_NORESERVE;
609
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400610 if (delete)
611 bits |= ~EXTENT_CTLBITS;
612 bits |= EXTENT_FIRST_DELALLOC;
613
Josef Bacik2ac55d42010-02-03 19:33:23 +0000614 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
615 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500616again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800617 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000618 /*
619 * Don't care for allocation failure here because we might end
620 * up not needing the pre-allocated extent state at all, which
621 * is the case if we only have in the tree extent states that
622 * cover our input range and don't cover too any other range.
623 * If we end up needing a new extent state we allocate it later.
624 */
Chris Masond1310b22008-01-24 16:13:08 -0500625 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500626 }
627
Chris Masoncad321a2008-12-17 14:51:42 -0500628 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400629 if (cached_state) {
630 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000631
632 if (clear) {
633 *cached_state = NULL;
634 cached_state = NULL;
635 }
636
Filipe Manana27a35072014-07-06 20:09:59 +0100637 if (cached && extent_state_in_tree(cached) &&
638 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000639 if (clear)
640 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400641 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400642 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400643 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000644 if (clear)
645 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400646 }
Chris Masond1310b22008-01-24 16:13:08 -0500647 /*
648 * this search will find the extents that end after
649 * our range starts
650 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500651 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500652 if (!node)
653 goto out;
654 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400655hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500656 if (state->start > end)
657 goto out;
658 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400659 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500660
Liu Bo04493142012-02-16 18:34:37 +0800661 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800662 if (!(state->state & bits)) {
663 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800664 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800665 }
Liu Bo04493142012-02-16 18:34:37 +0800666
Chris Masond1310b22008-01-24 16:13:08 -0500667 /*
668 * | ---- desired range ---- |
669 * | state | or
670 * | ------------- state -------------- |
671 *
672 * We need to split the extent we found, and may flip
673 * bits on second half.
674 *
675 * If the extent we found extends past our range, we
676 * just split and search again. It'll get split again
677 * the next time though.
678 *
679 * If the extent we found is inside our range, we clear
680 * the desired bit on it.
681 */
682
683 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000684 prealloc = alloc_extent_state_atomic(prealloc);
685 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500686 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400687 if (err)
688 extent_io_tree_panic(tree, err);
689
Chris Masond1310b22008-01-24 16:13:08 -0500690 prealloc = NULL;
691 if (err)
692 goto out;
693 if (state->end <= end) {
Qu Wenruofefdc552015-10-12 15:35:38 +0800694 state = clear_state_bit(tree, state, &bits, wake,
695 changeset);
Liu Bod1ac6e42012-05-10 18:10:39 +0800696 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500697 }
698 goto search_again;
699 }
700 /*
701 * | ---- desired range ---- |
702 * | state |
703 * We need to split the extent, and clear the bit
704 * on the first half
705 */
706 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000707 prealloc = alloc_extent_state_atomic(prealloc);
708 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500709 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400710 if (err)
711 extent_io_tree_panic(tree, err);
712
Chris Masond1310b22008-01-24 16:13:08 -0500713 if (wake)
714 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400715
Qu Wenruofefdc552015-10-12 15:35:38 +0800716 clear_state_bit(tree, prealloc, &bits, wake, changeset);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400717
Chris Masond1310b22008-01-24 16:13:08 -0500718 prealloc = NULL;
719 goto out;
720 }
Chris Mason42daec22009-09-23 19:51:09 -0400721
Qu Wenruofefdc552015-10-12 15:35:38 +0800722 state = clear_state_bit(tree, state, &bits, wake, changeset);
Liu Bo04493142012-02-16 18:34:37 +0800723next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400724 if (last_end == (u64)-1)
725 goto out;
726 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800727 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800728 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500729 goto search_again;
730
731out:
Chris Masoncad321a2008-12-17 14:51:42 -0500732 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500733 if (prealloc)
734 free_extent_state(prealloc);
735
Jeff Mahoney6763af82012-03-01 14:56:29 +0100736 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500737
738search_again:
739 if (start > end)
740 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500741 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -0800742 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -0500743 cond_resched();
744 goto again;
745}
Chris Masond1310b22008-01-24 16:13:08 -0500746
Jeff Mahoney143bede2012-03-01 14:56:26 +0100747static void wait_on_state(struct extent_io_tree *tree,
748 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500749 __releases(tree->lock)
750 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500751{
752 DEFINE_WAIT(wait);
753 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500754 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500755 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500756 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500757 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500758}
759
760/*
761 * waits for one or more bits to clear on a range in the state tree.
762 * The range [start, end] is inclusive.
763 * The tree lock is taken by this function
764 */
David Sterba41074882013-04-29 13:38:46 +0000765static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
766 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500767{
768 struct extent_state *state;
769 struct rb_node *node;
770
Josef Bacika5dee372013-12-13 10:02:44 -0500771 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000772
Chris Masoncad321a2008-12-17 14:51:42 -0500773 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500774again:
775 while (1) {
776 /*
777 * this search will find all the extents that end after
778 * our range starts
779 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500780 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100781process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500782 if (!node)
783 break;
784
785 state = rb_entry(node, struct extent_state, rb_node);
786
787 if (state->start > end)
788 goto out;
789
790 if (state->state & bits) {
791 start = state->start;
792 atomic_inc(&state->refs);
793 wait_on_state(tree, state);
794 free_extent_state(state);
795 goto again;
796 }
797 start = state->end + 1;
798
799 if (start > end)
800 break;
801
Filipe Mananac50d3e72014-03-31 14:53:25 +0100802 if (!cond_resched_lock(&tree->lock)) {
803 node = rb_next(node);
804 goto process_node;
805 }
Chris Masond1310b22008-01-24 16:13:08 -0500806 }
807out:
Chris Masoncad321a2008-12-17 14:51:42 -0500808 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500809}
Chris Masond1310b22008-01-24 16:13:08 -0500810
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000811static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500812 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800813 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500814{
David Sterba9ee49a042015-01-14 19:52:13 +0100815 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400816
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000817 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400818 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500819 u64 range = state->end - state->start + 1;
820 tree->dirty_bytes += range;
821 }
Qu Wenruod38ed272015-10-12 14:53:37 +0800822 add_extent_changeset(state, bits_to_set, changeset, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400823 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500824}
825
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100826static void cache_state_if_flags(struct extent_state *state,
827 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100828 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400829{
830 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100831 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400832 *cached_ptr = state;
833 atomic_inc(&state->refs);
834 }
835 }
836}
837
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100838static void cache_state(struct extent_state *state,
839 struct extent_state **cached_ptr)
840{
841 return cache_state_if_flags(state, cached_ptr,
842 EXTENT_IOBITS | EXTENT_BOUNDARY);
843}
844
Chris Masond1310b22008-01-24 16:13:08 -0500845/*
Chris Mason1edbb732009-09-02 13:24:36 -0400846 * set some bits on a range in the tree. This may require allocations or
847 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500848 *
Chris Mason1edbb732009-09-02 13:24:36 -0400849 * If any of the exclusive bits are set, this will fail with -EEXIST if some
850 * part of the range already has the desired bits set. The start of the
851 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500852 *
Chris Mason1edbb732009-09-02 13:24:36 -0400853 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500854 */
Chris Mason1edbb732009-09-02 13:24:36 -0400855
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100856static int __must_check
857__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100858 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000859 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800860 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500861{
862 struct extent_state *state;
863 struct extent_state *prealloc = NULL;
864 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000865 struct rb_node **p;
866 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500867 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500868 u64 last_start;
869 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400870
Josef Bacika5dee372013-12-13 10:02:44 -0500871 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000872
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400873 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500874again:
Mel Gormand0164ad2015-11-06 16:28:21 -0800875 if (!prealloc && gfpflags_allow_blocking(mask)) {
Chris Masond1310b22008-01-24 16:13:08 -0500876 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000877 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500878 }
879
Chris Masoncad321a2008-12-17 14:51:42 -0500880 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400881 if (cached_state && *cached_state) {
882 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400883 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100884 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400885 node = &state->rb_node;
886 goto hit_next;
887 }
888 }
Chris Masond1310b22008-01-24 16:13:08 -0500889 /*
890 * this search will find all the extents that end after
891 * our range starts.
892 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000893 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500894 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000895 prealloc = alloc_extent_state_atomic(prealloc);
896 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000897 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800898 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400899 if (err)
900 extent_io_tree_panic(tree, err);
901
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000902 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500903 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500904 goto out;
905 }
Chris Masond1310b22008-01-24 16:13:08 -0500906 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400907hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500908 last_start = state->start;
909 last_end = state->end;
910
911 /*
912 * | ---- desired range ---- |
913 * | state |
914 *
915 * Just lock what we found and keep going
916 */
917 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400918 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500919 *failed_start = state->start;
920 err = -EEXIST;
921 goto out;
922 }
Chris Mason42daec22009-09-23 19:51:09 -0400923
Qu Wenruod38ed272015-10-12 14:53:37 +0800924 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400925 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500926 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400927 if (last_end == (u64)-1)
928 goto out;
929 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800930 state = next_state(state);
931 if (start < end && state && state->start == start &&
932 !need_resched())
933 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500934 goto search_again;
935 }
936
937 /*
938 * | ---- desired range ---- |
939 * | state |
940 * or
941 * | ------------- state -------------- |
942 *
943 * We need to split the extent we found, and may flip bits on
944 * second half.
945 *
946 * If the extent we found extends past our
947 * range, we just split and search again. It'll get split
948 * again the next time though.
949 *
950 * If the extent we found is inside our range, we set the
951 * desired bit on it.
952 */
953 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400954 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500955 *failed_start = start;
956 err = -EEXIST;
957 goto out;
958 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000959
960 prealloc = alloc_extent_state_atomic(prealloc);
961 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500962 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400963 if (err)
964 extent_io_tree_panic(tree, err);
965
Chris Masond1310b22008-01-24 16:13:08 -0500966 prealloc = NULL;
967 if (err)
968 goto out;
969 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800970 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400971 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500972 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400973 if (last_end == (u64)-1)
974 goto out;
975 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800976 state = next_state(state);
977 if (start < end && state && state->start == start &&
978 !need_resched())
979 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500980 }
981 goto search_again;
982 }
983 /*
984 * | ---- desired range ---- |
985 * | state | or | state |
986 *
987 * There's a hole, we need to insert something in it and
988 * ignore the extent we found.
989 */
990 if (state->start > start) {
991 u64 this_end;
992 if (end < last_start)
993 this_end = end;
994 else
Chris Masond3977122009-01-05 21:25:51 -0500995 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000996
997 prealloc = alloc_extent_state_atomic(prealloc);
998 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000999
1000 /*
1001 * Avoid to free 'prealloc' if it can be merged with
1002 * the later extent.
1003 */
Chris Masond1310b22008-01-24 16:13:08 -05001004 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001005 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001006 if (err)
1007 extent_io_tree_panic(tree, err);
1008
Chris Mason2c64c532009-09-02 15:04:12 -04001009 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001010 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001011 start = this_end + 1;
1012 goto search_again;
1013 }
1014 /*
1015 * | ---- desired range ---- |
1016 * | state |
1017 * We need to split the extent, and set the bit
1018 * on the first half
1019 */
1020 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001021 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001022 *failed_start = start;
1023 err = -EEXIST;
1024 goto out;
1025 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001026
1027 prealloc = alloc_extent_state_atomic(prealloc);
1028 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001029 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001030 if (err)
1031 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001032
Qu Wenruod38ed272015-10-12 14:53:37 +08001033 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001034 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001035 merge_state(tree, prealloc);
1036 prealloc = NULL;
1037 goto out;
1038 }
1039
1040 goto search_again;
1041
1042out:
Chris Masoncad321a2008-12-17 14:51:42 -05001043 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001044 if (prealloc)
1045 free_extent_state(prealloc);
1046
1047 return err;
1048
1049search_again:
1050 if (start > end)
1051 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -05001052 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -08001053 if (gfpflags_allow_blocking(mask))
Chris Masond1310b22008-01-24 16:13:08 -05001054 cond_resched();
1055 goto again;
1056}
Chris Masond1310b22008-01-24 16:13:08 -05001057
David Sterba41074882013-04-29 13:38:46 +00001058int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001059 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001060 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001061{
1062 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001063 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001064}
1065
1066
Josef Bacik462d6fa2011-09-26 13:56:12 -04001067/**
Liu Bo10983f22012-07-11 15:26:19 +08001068 * convert_extent_bit - convert all bits in a given range from one bit to
1069 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070 * @tree: the io tree to search
1071 * @start: the start offset in bytes
1072 * @end: the end offset in bytes (inclusive)
1073 * @bits: the bits to set in this range
1074 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001075 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001076 * @mask: the allocation mask
1077 *
1078 * This will go through and set bits for the given range. If any states exist
1079 * already in this range they are set with the given bit and cleared of the
1080 * clear_bits. This is only meant to be used by things that are mergeable, ie
1081 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1082 * boundary bits like LOCK.
1083 */
1084int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001085 unsigned bits, unsigned clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -04001086 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001087{
1088 struct extent_state *state;
1089 struct extent_state *prealloc = NULL;
1090 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001091 struct rb_node **p;
1092 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001093 int err = 0;
1094 u64 last_start;
1095 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001096 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097
Josef Bacika5dee372013-12-13 10:02:44 -05001098 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001099
Josef Bacik462d6fa2011-09-26 13:56:12 -04001100again:
Mel Gormand0164ad2015-11-06 16:28:21 -08001101 if (!prealloc && gfpflags_allow_blocking(mask)) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001102 /*
1103 * Best effort, don't worry if extent state allocation fails
1104 * here for the first iteration. We might have a cached state
1105 * that matches exactly the target range, in which case no
1106 * extent state allocations are needed. We'll only know this
1107 * after locking the tree.
1108 */
Josef Bacik462d6fa2011-09-26 13:56:12 -04001109 prealloc = alloc_extent_state(mask);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001110 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001111 return -ENOMEM;
1112 }
1113
1114 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001115 if (cached_state && *cached_state) {
1116 state = *cached_state;
1117 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001118 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001119 node = &state->rb_node;
1120 goto hit_next;
1121 }
1122 }
1123
Josef Bacik462d6fa2011-09-26 13:56:12 -04001124 /*
1125 * this search will find all the extents that end after
1126 * our range starts.
1127 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001128 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001129 if (!node) {
1130 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001131 if (!prealloc) {
1132 err = -ENOMEM;
1133 goto out;
1134 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001135 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001136 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001137 if (err)
1138 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001139 cache_state(prealloc, cached_state);
1140 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001141 goto out;
1142 }
1143 state = rb_entry(node, struct extent_state, rb_node);
1144hit_next:
1145 last_start = state->start;
1146 last_end = state->end;
1147
1148 /*
1149 * | ---- desired range ---- |
1150 * | state |
1151 *
1152 * Just lock what we found and keep going
1153 */
1154 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001155 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001156 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001157 state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001158 if (last_end == (u64)-1)
1159 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001160 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001161 if (start < end && state && state->start == start &&
1162 !need_resched())
1163 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001164 goto search_again;
1165 }
1166
1167 /*
1168 * | ---- desired range ---- |
1169 * | state |
1170 * or
1171 * | ------------- state -------------- |
1172 *
1173 * We need to split the extent we found, and may flip bits on
1174 * second half.
1175 *
1176 * If the extent we found extends past our
1177 * range, we just split and search again. It'll get split
1178 * again the next time though.
1179 *
1180 * If the extent we found is inside our range, we set the
1181 * desired bit on it.
1182 */
1183 if (state->start < start) {
1184 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001185 if (!prealloc) {
1186 err = -ENOMEM;
1187 goto out;
1188 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001189 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001190 if (err)
1191 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001192 prealloc = NULL;
1193 if (err)
1194 goto out;
1195 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001196 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001197 cache_state(state, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001198 state = clear_state_bit(tree, state, &clear_bits, 0,
1199 NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001200 if (last_end == (u64)-1)
1201 goto out;
1202 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001203 if (start < end && state && state->start == start &&
1204 !need_resched())
1205 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001206 }
1207 goto search_again;
1208 }
1209 /*
1210 * | ---- desired range ---- |
1211 * | state | or | state |
1212 *
1213 * There's a hole, we need to insert something in it and
1214 * ignore the extent we found.
1215 */
1216 if (state->start > start) {
1217 u64 this_end;
1218 if (end < last_start)
1219 this_end = end;
1220 else
1221 this_end = last_start - 1;
1222
1223 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001224 if (!prealloc) {
1225 err = -ENOMEM;
1226 goto out;
1227 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001228
1229 /*
1230 * Avoid to free 'prealloc' if it can be merged with
1231 * the later extent.
1232 */
1233 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001234 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001235 if (err)
1236 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001237 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001238 prealloc = NULL;
1239 start = this_end + 1;
1240 goto search_again;
1241 }
1242 /*
1243 * | ---- desired range ---- |
1244 * | state |
1245 * We need to split the extent, and set the bit
1246 * on the first half
1247 */
1248 if (state->start <= end && state->end > end) {
1249 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001250 if (!prealloc) {
1251 err = -ENOMEM;
1252 goto out;
1253 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001254
1255 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001256 if (err)
1257 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001258
Qu Wenruod38ed272015-10-12 14:53:37 +08001259 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001260 cache_state(prealloc, cached_state);
Qu Wenruofefdc552015-10-12 15:35:38 +08001261 clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001262 prealloc = NULL;
1263 goto out;
1264 }
1265
1266 goto search_again;
1267
1268out:
1269 spin_unlock(&tree->lock);
1270 if (prealloc)
1271 free_extent_state(prealloc);
1272
1273 return err;
1274
1275search_again:
1276 if (start > end)
1277 goto out;
1278 spin_unlock(&tree->lock);
Mel Gormand0164ad2015-11-06 16:28:21 -08001279 if (gfpflags_allow_blocking(mask))
Josef Bacik462d6fa2011-09-26 13:56:12 -04001280 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001281 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001282 goto again;
1283}
1284
Chris Masond1310b22008-01-24 16:13:08 -05001285/* wrappers around set/clear extent bit */
Qu Wenruod38ed272015-10-12 14:53:37 +08001286int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba2c53b912016-04-26 23:54:39 +02001287 unsigned bits, struct extent_changeset *changeset)
Qu Wenruod38ed272015-10-12 14:53:37 +08001288{
1289 /*
1290 * We don't support EXTENT_LOCKED yet, as current changeset will
1291 * record any bits changed, so for EXTENT_LOCKED case, it will
1292 * either fail with -EEXIST or changeset will record the whole
1293 * range.
1294 */
1295 BUG_ON(bits & EXTENT_LOCKED);
1296
David Sterba2c53b912016-04-26 23:54:39 +02001297 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
Qu Wenruod38ed272015-10-12 14:53:37 +08001298 changeset);
1299}
1300
Qu Wenruofefdc552015-10-12 15:35:38 +08001301int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1302 unsigned bits, int wake, int delete,
1303 struct extent_state **cached, gfp_t mask)
1304{
1305 return __clear_extent_bit(tree, start, end, bits, wake, delete,
1306 cached, mask, NULL);
1307}
1308
Qu Wenruofefdc552015-10-12 15:35:38 +08001309int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaf734c442016-04-26 23:54:39 +02001310 unsigned bits, struct extent_changeset *changeset)
Qu Wenruofefdc552015-10-12 15:35:38 +08001311{
1312 /*
1313 * Don't support EXTENT_LOCKED case, same reason as
1314 * set_record_extent_bits().
1315 */
1316 BUG_ON(bits & EXTENT_LOCKED);
1317
David Sterbaf734c442016-04-26 23:54:39 +02001318 return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
Qu Wenruofefdc552015-10-12 15:35:38 +08001319 changeset);
1320}
1321
Chris Masond352ac62008-09-29 15:18:18 -04001322/*
1323 * either insert or lock state struct between start and end use mask to tell
1324 * us if waiting is desired.
1325 */
Chris Mason1edbb732009-09-02 13:24:36 -04001326int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterbaff13db42015-12-03 14:30:40 +01001327 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001328{
1329 int err;
1330 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001331
Chris Masond1310b22008-01-24 16:13:08 -05001332 while (1) {
David Sterbaff13db42015-12-03 14:30:40 +01001333 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001334 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001335 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001336 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001337 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1338 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001339 } else
Chris Masond1310b22008-01-24 16:13:08 -05001340 break;
Chris Masond1310b22008-01-24 16:13:08 -05001341 WARN_ON(start > end);
1342 }
1343 return err;
1344}
Chris Masond1310b22008-01-24 16:13:08 -05001345
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001346int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001347{
1348 int err;
1349 u64 failed_start;
1350
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001351 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001352 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001353 if (err == -EEXIST) {
1354 if (failed_start > start)
1355 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001356 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001357 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001358 }
Josef Bacik25179202008-10-29 14:49:05 -04001359 return 1;
1360}
Josef Bacik25179202008-10-29 14:49:05 -04001361
David Sterbabd1fa4f2015-12-03 13:08:59 +01001362void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001363{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001364 unsigned long index = start >> PAGE_SHIFT;
1365 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001366 struct page *page;
1367
1368 while (index <= end_index) {
1369 page = find_get_page(inode->i_mapping, index);
1370 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1371 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001372 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001373 index++;
1374 }
Chris Mason4adaa612013-03-26 13:07:00 -04001375}
1376
David Sterbaf6311572015-12-03 13:08:59 +01001377void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
Chris Mason4adaa612013-03-26 13:07:00 -04001378{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 unsigned long index = start >> PAGE_SHIFT;
1380 unsigned long end_index = end >> PAGE_SHIFT;
Chris Mason4adaa612013-03-26 13:07:00 -04001381 struct page *page;
1382
1383 while (index <= end_index) {
1384 page = find_get_page(inode->i_mapping, index);
1385 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001386 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001387 account_page_redirty(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001388 put_page(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001389 index++;
1390 }
Chris Mason4adaa612013-03-26 13:07:00 -04001391}
1392
Chris Masond1310b22008-01-24 16:13:08 -05001393/*
Chris Masond1310b22008-01-24 16:13:08 -05001394 * helper function to set both pages and extents in the tree writeback
1395 */
David Sterba35de6db2015-12-03 13:08:59 +01001396static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001397{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001398 unsigned long index = start >> PAGE_SHIFT;
1399 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05001400 struct page *page;
1401
1402 while (index <= end_index) {
1403 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001404 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001405 set_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001406 put_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001407 index++;
1408 }
Chris Masond1310b22008-01-24 16:13:08 -05001409}
Chris Masond1310b22008-01-24 16:13:08 -05001410
Chris Masond352ac62008-09-29 15:18:18 -04001411/* find the first state struct with 'bits' set after 'start', and
1412 * return it. tree->lock must be held. NULL will returned if
1413 * nothing was found after 'start'
1414 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001415static struct extent_state *
1416find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001417 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001418{
1419 struct rb_node *node;
1420 struct extent_state *state;
1421
1422 /*
1423 * this search will find all the extents that end after
1424 * our range starts.
1425 */
1426 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001427 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001428 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001429
Chris Masond3977122009-01-05 21:25:51 -05001430 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001431 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001432 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001433 return state;
Chris Masond3977122009-01-05 21:25:51 -05001434
Chris Masond7fc6402008-02-18 12:12:38 -05001435 node = rb_next(node);
1436 if (!node)
1437 break;
1438 }
1439out:
1440 return NULL;
1441}
Chris Masond7fc6402008-02-18 12:12:38 -05001442
Chris Masond352ac62008-09-29 15:18:18 -04001443/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001444 * find the first offset in the io tree with 'bits' set. zero is
1445 * returned if we find something, and *start_ret and *end_ret are
1446 * set to reflect the state struct that was found.
1447 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001448 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001449 */
1450int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001451 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001452 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001453{
1454 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001455 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001456 int ret = 1;
1457
1458 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001459 if (cached_state && *cached_state) {
1460 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001461 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001462 n = rb_next(&state->rb_node);
1463 while (n) {
1464 state = rb_entry(n, struct extent_state,
1465 rb_node);
1466 if (state->state & bits)
1467 goto got_it;
1468 n = rb_next(n);
1469 }
1470 free_extent_state(*cached_state);
1471 *cached_state = NULL;
1472 goto out;
1473 }
1474 free_extent_state(*cached_state);
1475 *cached_state = NULL;
1476 }
1477
Xiao Guangrong69261c42011-07-14 03:19:45 +00001478 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001479got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001480 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001481 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001482 *start_ret = state->start;
1483 *end_ret = state->end;
1484 ret = 0;
1485 }
Josef Bacike6138872012-09-27 17:07:30 -04001486out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001487 spin_unlock(&tree->lock);
1488 return ret;
1489}
1490
1491/*
Chris Masond352ac62008-09-29 15:18:18 -04001492 * find a contiguous range of bytes in the file marked as delalloc, not
1493 * more than 'max_bytes'. start and end are used to return the range,
1494 *
1495 * 1 is returned if we find something, 0 if nothing was in the tree
1496 */
Chris Masonc8b97812008-10-29 14:49:59 -04001497static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001498 u64 *start, u64 *end, u64 max_bytes,
1499 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001500{
1501 struct rb_node *node;
1502 struct extent_state *state;
1503 u64 cur_start = *start;
1504 u64 found = 0;
1505 u64 total_bytes = 0;
1506
Chris Masoncad321a2008-12-17 14:51:42 -05001507 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001508
Chris Masond1310b22008-01-24 16:13:08 -05001509 /*
1510 * this search will find all the extents that end after
1511 * our range starts.
1512 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001513 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001514 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001515 if (!found)
1516 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001517 goto out;
1518 }
1519
Chris Masond3977122009-01-05 21:25:51 -05001520 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001521 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001522 if (found && (state->start != cur_start ||
1523 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001524 goto out;
1525 }
1526 if (!(state->state & EXTENT_DELALLOC)) {
1527 if (!found)
1528 *end = state->end;
1529 goto out;
1530 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001531 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001532 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001533 *cached_state = state;
1534 atomic_inc(&state->refs);
1535 }
Chris Masond1310b22008-01-24 16:13:08 -05001536 found++;
1537 *end = state->end;
1538 cur_start = state->end + 1;
1539 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001540 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001541 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001542 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001543 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001544 break;
1545 }
1546out:
Chris Masoncad321a2008-12-17 14:51:42 -05001547 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001548 return found;
1549}
1550
Jeff Mahoney143bede2012-03-01 14:56:26 +01001551static noinline void __unlock_for_delalloc(struct inode *inode,
1552 struct page *locked_page,
1553 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001554{
1555 int ret;
1556 struct page *pages[16];
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001557 unsigned long index = start >> PAGE_SHIFT;
1558 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001559 unsigned long nr_pages = end_index - index + 1;
1560 int i;
1561
1562 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001563 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001564
Chris Masond3977122009-01-05 21:25:51 -05001565 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001566 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001567 min_t(unsigned long, nr_pages,
1568 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001569 for (i = 0; i < ret; i++) {
1570 if (pages[i] != locked_page)
1571 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001572 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001573 }
1574 nr_pages -= ret;
1575 index += ret;
1576 cond_resched();
1577 }
Chris Masonc8b97812008-10-29 14:49:59 -04001578}
1579
1580static noinline int lock_delalloc_pages(struct inode *inode,
1581 struct page *locked_page,
1582 u64 delalloc_start,
1583 u64 delalloc_end)
1584{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001585 unsigned long index = delalloc_start >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001586 unsigned long start_index = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001587 unsigned long end_index = delalloc_end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001588 unsigned long pages_locked = 0;
1589 struct page *pages[16];
1590 unsigned long nrpages;
1591 int ret;
1592 int i;
1593
1594 /* the caller is responsible for locking the start index */
1595 if (index == locked_page->index && index == end_index)
1596 return 0;
1597
1598 /* skip the page at the start index */
1599 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001600 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001601 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001602 min_t(unsigned long,
1603 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001604 if (ret == 0) {
1605 ret = -EAGAIN;
1606 goto done;
1607 }
1608 /* now we have an array of pages, lock them all */
1609 for (i = 0; i < ret; i++) {
1610 /*
1611 * the caller is taking responsibility for
1612 * locked_page
1613 */
Chris Mason771ed682008-11-06 22:02:51 -05001614 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001615 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001616 if (!PageDirty(pages[i]) ||
1617 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001618 ret = -EAGAIN;
1619 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001620 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001621 goto done;
1622 }
1623 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001624 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001625 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001626 }
Chris Masonc8b97812008-10-29 14:49:59 -04001627 nrpages -= ret;
1628 index += ret;
1629 cond_resched();
1630 }
1631 ret = 0;
1632done:
1633 if (ret && pages_locked) {
1634 __unlock_for_delalloc(inode, locked_page,
1635 delalloc_start,
1636 ((u64)(start_index + pages_locked - 1)) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001637 PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -04001638 }
1639 return ret;
1640}
1641
1642/*
1643 * find a contiguous range of bytes in the file marked as delalloc, not
1644 * more than 'max_bytes'. start and end are used to return the range,
1645 *
1646 * 1 is returned if we find something, 0 if nothing was in the tree
1647 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001648STATIC u64 find_lock_delalloc_range(struct inode *inode,
1649 struct extent_io_tree *tree,
1650 struct page *locked_page, u64 *start,
1651 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001652{
1653 u64 delalloc_start;
1654 u64 delalloc_end;
1655 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001656 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001657 int ret;
1658 int loops = 0;
1659
1660again:
1661 /* step one, find a bunch of delalloc bytes starting at start */
1662 delalloc_start = *start;
1663 delalloc_end = 0;
1664 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001665 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001666 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001667 *start = delalloc_start;
1668 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001669 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001670 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001671 }
1672
1673 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001674 * start comes from the offset of locked_page. We have to lock
1675 * pages in order, so we can't process delalloc bytes before
1676 * locked_page
1677 */
Chris Masond3977122009-01-05 21:25:51 -05001678 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001679 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001680
1681 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001682 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001683 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001684 if (delalloc_end + 1 - delalloc_start > max_bytes)
1685 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001686
Chris Masonc8b97812008-10-29 14:49:59 -04001687 /* step two, lock all the pages after the page that has start */
1688 ret = lock_delalloc_pages(inode, locked_page,
1689 delalloc_start, delalloc_end);
1690 if (ret == -EAGAIN) {
1691 /* some of the pages are gone, lets avoid looping by
1692 * shortening the size of the delalloc range we're searching
1693 */
Chris Mason9655d292009-09-02 15:22:30 -04001694 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001695 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001696 if (!loops) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001697 max_bytes = PAGE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001698 loops = 1;
1699 goto again;
1700 } else {
1701 found = 0;
1702 goto out_failed;
1703 }
1704 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001705 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001706
1707 /* step three, lock the state bits for the whole range */
David Sterbaff13db42015-12-03 14:30:40 +01001708 lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001709
1710 /* then test to make sure it is all still delalloc */
1711 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001712 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001713 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001714 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1715 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001716 __unlock_for_delalloc(inode, locked_page,
1717 delalloc_start, delalloc_end);
1718 cond_resched();
1719 goto again;
1720 }
Chris Mason9655d292009-09-02 15:22:30 -04001721 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001722 *start = delalloc_start;
1723 *end = delalloc_end;
1724out_failed:
1725 return found;
1726}
1727
David Sterbaa9d93e12015-12-03 13:08:59 +01001728void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001729 struct page *locked_page,
David Sterba9ee49a042015-01-14 19:52:13 +01001730 unsigned clear_bits,
Josef Bacikc2790a22013-07-29 11:20:47 -04001731 unsigned long page_ops)
Chris Masonc8b97812008-10-29 14:49:59 -04001732{
Josef Bacikc2790a22013-07-29 11:20:47 -04001733 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Masonc8b97812008-10-29 14:49:59 -04001734 int ret;
1735 struct page *pages[16];
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001736 unsigned long index = start >> PAGE_SHIFT;
1737 unsigned long end_index = end >> PAGE_SHIFT;
Chris Masonc8b97812008-10-29 14:49:59 -04001738 unsigned long nr_pages = end_index - index + 1;
1739 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001740
Chris Mason2c64c532009-09-02 15:04:12 -04001741 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacikc2790a22013-07-29 11:20:47 -04001742 if (page_ops == 0)
David Sterbaa9d93e12015-12-03 13:08:59 +01001743 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001744
Filipe Manana704de492014-10-06 22:14:22 +01001745 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1746 mapping_set_error(inode->i_mapping, -EIO);
1747
Chris Masond3977122009-01-05 21:25:51 -05001748 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001749 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001750 min_t(unsigned long,
1751 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001752 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001753
Josef Bacikc2790a22013-07-29 11:20:47 -04001754 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001755 SetPagePrivate2(pages[i]);
1756
Chris Masonc8b97812008-10-29 14:49:59 -04001757 if (pages[i] == locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001758 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001759 continue;
1760 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001761 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001762 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001763 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001764 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001765 if (page_ops & PAGE_SET_ERROR)
1766 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001767 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001768 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001769 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001770 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001771 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001772 }
1773 nr_pages -= ret;
1774 index += ret;
1775 cond_resched();
1776 }
Chris Masonc8b97812008-10-29 14:49:59 -04001777}
Chris Masonc8b97812008-10-29 14:49:59 -04001778
Chris Masond352ac62008-09-29 15:18:18 -04001779/*
1780 * count the number of bytes in the tree that have a given bit(s)
1781 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1782 * cached. The total number found is returned.
1783 */
Chris Masond1310b22008-01-24 16:13:08 -05001784u64 count_range_bits(struct extent_io_tree *tree,
1785 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001786 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001787{
1788 struct rb_node *node;
1789 struct extent_state *state;
1790 u64 cur_start = *start;
1791 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001792 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001793 int found = 0;
1794
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301795 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001796 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001797
Chris Masoncad321a2008-12-17 14:51:42 -05001798 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001799 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1800 total_bytes = tree->dirty_bytes;
1801 goto out;
1802 }
1803 /*
1804 * this search will find all the extents that end after
1805 * our range starts.
1806 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001807 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001808 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001809 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001810
Chris Masond3977122009-01-05 21:25:51 -05001811 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001812 state = rb_entry(node, struct extent_state, rb_node);
1813 if (state->start > search_end)
1814 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001815 if (contig && found && state->start > last + 1)
1816 break;
1817 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001818 total_bytes += min(search_end, state->end) + 1 -
1819 max(cur_start, state->start);
1820 if (total_bytes >= max_bytes)
1821 break;
1822 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001823 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001824 found = 1;
1825 }
Chris Masonec29ed52011-02-23 16:23:20 -05001826 last = state->end;
1827 } else if (contig && found) {
1828 break;
Chris Masond1310b22008-01-24 16:13:08 -05001829 }
1830 node = rb_next(node);
1831 if (!node)
1832 break;
1833 }
1834out:
Chris Masoncad321a2008-12-17 14:51:42 -05001835 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001836 return total_bytes;
1837}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001838
Chris Masond352ac62008-09-29 15:18:18 -04001839/*
1840 * set the private field for a given byte offset in the tree. If there isn't
1841 * an extent_state there already, this does nothing.
1842 */
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001843static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001844 struct io_failure_record *failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001845{
1846 struct rb_node *node;
1847 struct extent_state *state;
1848 int ret = 0;
1849
Chris Masoncad321a2008-12-17 14:51:42 -05001850 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001851 /*
1852 * this search will find all the extents that end after
1853 * our range starts.
1854 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001855 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001856 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001857 ret = -ENOENT;
1858 goto out;
1859 }
1860 state = rb_entry(node, struct extent_state, rb_node);
1861 if (state->start != start) {
1862 ret = -ENOENT;
1863 goto out;
1864 }
David Sterba47dc1962016-02-11 13:24:13 +01001865 state->failrec = failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001866out:
Chris Masoncad321a2008-12-17 14:51:42 -05001867 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001868 return ret;
1869}
1870
Arnd Bergmannf827ba92016-02-22 22:53:20 +01001871static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
David Sterba47dc1962016-02-11 13:24:13 +01001872 struct io_failure_record **failrec)
Chris Masond1310b22008-01-24 16:13:08 -05001873{
1874 struct rb_node *node;
1875 struct extent_state *state;
1876 int ret = 0;
1877
Chris Masoncad321a2008-12-17 14:51:42 -05001878 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001879 /*
1880 * this search will find all the extents that end after
1881 * our range starts.
1882 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001883 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001884 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001885 ret = -ENOENT;
1886 goto out;
1887 }
1888 state = rb_entry(node, struct extent_state, rb_node);
1889 if (state->start != start) {
1890 ret = -ENOENT;
1891 goto out;
1892 }
David Sterba47dc1962016-02-11 13:24:13 +01001893 *failrec = state->failrec;
Chris Masond1310b22008-01-24 16:13:08 -05001894out:
Chris Masoncad321a2008-12-17 14:51:42 -05001895 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001896 return ret;
1897}
1898
1899/*
1900 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001901 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001902 * has the bits set. Otherwise, 1 is returned if any bit in the
1903 * range is found set.
1904 */
1905int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001906 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001907{
1908 struct extent_state *state = NULL;
1909 struct rb_node *node;
1910 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001911
Chris Masoncad321a2008-12-17 14:51:42 -05001912 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001913 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001914 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001915 node = &cached->rb_node;
1916 else
1917 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001918 while (node && start <= end) {
1919 state = rb_entry(node, struct extent_state, rb_node);
1920
1921 if (filled && state->start > start) {
1922 bitset = 0;
1923 break;
1924 }
1925
1926 if (state->start > end)
1927 break;
1928
1929 if (state->state & bits) {
1930 bitset = 1;
1931 if (!filled)
1932 break;
1933 } else if (filled) {
1934 bitset = 0;
1935 break;
1936 }
Chris Mason46562cec2009-09-23 20:23:16 -04001937
1938 if (state->end == (u64)-1)
1939 break;
1940
Chris Masond1310b22008-01-24 16:13:08 -05001941 start = state->end + 1;
1942 if (start > end)
1943 break;
1944 node = rb_next(node);
1945 if (!node) {
1946 if (filled)
1947 bitset = 0;
1948 break;
1949 }
1950 }
Chris Masoncad321a2008-12-17 14:51:42 -05001951 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001952 return bitset;
1953}
Chris Masond1310b22008-01-24 16:13:08 -05001954
1955/*
1956 * helper function to set a given page up to date if all the
1957 * extents in the tree for that page are up to date
1958 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001959static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001960{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001961 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001962 u64 end = start + PAGE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001963 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001964 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001965}
1966
Miao Xie8b110e32014-09-12 18:44:03 +08001967int free_io_failure(struct inode *inode, struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001968{
1969 int ret;
1970 int err = 0;
1971 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1972
David Sterba47dc1962016-02-11 13:24:13 +01001973 set_state_failrec(failure_tree, rec->start, NULL);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974 ret = clear_extent_bits(failure_tree, rec->start,
1975 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001976 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001977 if (ret)
1978 err = ret;
1979
David Woodhouse53b381b2013-01-29 18:40:14 -05001980 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1981 rec->start + rec->len - 1,
David Sterba91166212016-04-26 23:54:39 +02001982 EXTENT_DAMAGED);
David Woodhouse53b381b2013-01-29 18:40:14 -05001983 if (ret && !err)
1984 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001985
1986 kfree(rec);
1987 return err;
1988}
1989
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001990/*
1991 * this bypasses the standard btrfs submit functions deliberately, as
1992 * the standard behavior is to write all copies in a raid setup. here we only
1993 * want to write the one bad copy. so we do the mapping for ourselves and issue
1994 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001995 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001996 * actually prevents the read that triggered the error from finishing.
1997 * currently, there can be no more than two copies of every data bit. thus,
1998 * exactly one rewrite is required.
1999 */
Miao Xie1203b682014-09-12 18:44:01 +08002000int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
2001 struct page *page, unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002002{
Miao Xie1203b682014-09-12 18:44:01 +08002003 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002004 struct bio *bio;
2005 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002006 u64 map_length = 0;
2007 u64 sector;
2008 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002009 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002010 int ret;
2011
Ilya Dryomov908960c2013-11-03 19:06:39 +02002012 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002013 BUG_ON(!mirror_num);
2014
David Woodhouse53b381b2013-01-29 18:40:14 -05002015 /* we can't repair anything in raid56 yet */
2016 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2017 return 0;
2018
Chris Mason9be33952013-05-17 18:30:14 -04002019 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002020 if (!bio)
2021 return -EIO;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002022 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002023 map_length = length;
2024
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002025 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002026 &map_length, &bbio, mirror_num);
2027 if (ret) {
2028 bio_put(bio);
2029 return -EIO;
2030 }
2031 BUG_ON(mirror_num != bbio->mirror_num);
2032 sector = bbio->stripes[mirror_num-1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002033 bio->bi_iter.bi_sector = sector;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002034 dev = bbio->stripes[mirror_num-1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002035 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 if (!dev || !dev->bdev || !dev->writeable) {
2037 bio_put(bio);
2038 return -EIO;
2039 }
2040 bio->bi_bdev = dev->bdev;
Miao Xieffdd2012014-09-12 18:44:00 +08002041 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002042
Kent Overstreet33879d42013-11-23 22:33:32 -08002043 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002044 /* try to remap that extent elsewhere? */
2045 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002046 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002047 return -EIO;
2048 }
2049
David Sterbab14af3b2015-10-08 10:43:10 +02002050 btrfs_info_rl_in_rcu(fs_info,
2051 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Miao Xie1203b682014-09-12 18:44:01 +08002052 btrfs_ino(inode), start,
2053 rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002054 bio_put(bio);
2055 return 0;
2056}
2057
Josef Bacikea466792012-03-26 21:57:36 -04002058int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2059 int mirror_num)
2060{
Josef Bacikea466792012-03-26 21:57:36 -04002061 u64 start = eb->start;
2062 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002063 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002064
Ilya Dryomov908960c2013-11-03 19:06:39 +02002065 if (root->fs_info->sb->s_flags & MS_RDONLY)
2066 return -EROFS;
2067
Josef Bacikea466792012-03-26 21:57:36 -04002068 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002069 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002070
2071 ret = repair_io_failure(root->fs_info->btree_inode, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 PAGE_SIZE, start, p,
Miao Xie1203b682014-09-12 18:44:01 +08002073 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002074 if (ret)
2075 break;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002076 start += PAGE_SIZE;
Josef Bacikea466792012-03-26 21:57:36 -04002077 }
2078
2079 return ret;
2080}
2081
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002082/*
2083 * each time an IO finishes, we do a fast check in the IO failure tree
2084 * to see if we need to process or clean up an io_failure_record
2085 */
Miao Xie8b110e32014-09-12 18:44:03 +08002086int clean_io_failure(struct inode *inode, u64 start, struct page *page,
2087 unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002088{
2089 u64 private;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002090 struct io_failure_record *failrec;
Ilya Dryomov908960c2013-11-03 19:06:39 +02002091 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092 struct extent_state *state;
2093 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002094 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002095
2096 private = 0;
2097 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2098 (u64)-1, 1, EXTENT_DIRTY, 0);
2099 if (!ret)
2100 return 0;
2101
David Sterba47dc1962016-02-11 13:24:13 +01002102 ret = get_state_failrec(&BTRFS_I(inode)->io_failure_tree, start,
2103 &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002104 if (ret)
2105 return 0;
2106
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002107 BUG_ON(!failrec->this_mirror);
2108
2109 if (failrec->in_validation) {
2110 /* there was no real error, just free the record */
2111 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2112 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 goto out;
2114 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002115 if (fs_info->sb->s_flags & MS_RDONLY)
2116 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002117
2118 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2119 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2120 failrec->start,
2121 EXTENT_LOCKED);
2122 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2123
Miao Xie883d0de2013-07-25 19:22:35 +08002124 if (state && state->start <= failrec->start &&
2125 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002126 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2127 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002128 if (num_copies > 1) {
Miao Xie1203b682014-09-12 18:44:01 +08002129 repair_io_failure(inode, start, failrec->len,
Miao Xie454ff3d2014-09-12 18:43:58 +08002130 failrec->logical, page,
Miao Xie1203b682014-09-12 18:44:01 +08002131 pg_offset, failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002132 }
2133 }
2134
2135out:
Miao Xie454ff3d2014-09-12 18:43:58 +08002136 free_io_failure(inode, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002137
Miao Xie454ff3d2014-09-12 18:43:58 +08002138 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139}
2140
Miao Xief6124962014-09-12 18:44:04 +08002141/*
2142 * Can be called when
2143 * - hold extent lock
2144 * - under ordered extent
2145 * - the inode is freeing
2146 */
2147void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
2148{
2149 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2150 struct io_failure_record *failrec;
2151 struct extent_state *state, *next;
2152
2153 if (RB_EMPTY_ROOT(&failure_tree->state))
2154 return;
2155
2156 spin_lock(&failure_tree->lock);
2157 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2158 while (state) {
2159 if (state->start > end)
2160 break;
2161
2162 ASSERT(state->end <= end);
2163
2164 next = next_state(state);
2165
David Sterba47dc1962016-02-11 13:24:13 +01002166 failrec = state->failrec;
Miao Xief6124962014-09-12 18:44:04 +08002167 free_extent_state(state);
2168 kfree(failrec);
2169
2170 state = next;
2171 }
2172 spin_unlock(&failure_tree->lock);
2173}
2174
Miao Xie2fe63032014-09-12 18:43:59 +08002175int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
David Sterba47dc1962016-02-11 13:24:13 +01002176 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177{
Miao Xie2fe63032014-09-12 18:43:59 +08002178 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002179 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002180 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2181 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2182 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002183 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002184 u64 logical;
2185
David Sterba47dc1962016-02-11 13:24:13 +01002186 ret = get_state_failrec(failure_tree, start, &failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 if (ret) {
2188 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2189 if (!failrec)
2190 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002191
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002192 failrec->start = start;
2193 failrec->len = end - start + 1;
2194 failrec->this_mirror = 0;
2195 failrec->bio_flags = 0;
2196 failrec->in_validation = 0;
2197
2198 read_lock(&em_tree->lock);
2199 em = lookup_extent_mapping(em_tree, start, failrec->len);
2200 if (!em) {
2201 read_unlock(&em_tree->lock);
2202 kfree(failrec);
2203 return -EIO;
2204 }
2205
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002206 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002207 free_extent_map(em);
2208 em = NULL;
2209 }
2210 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002211 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002212 kfree(failrec);
2213 return -EIO;
2214 }
Miao Xie2fe63032014-09-12 18:43:59 +08002215
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002216 logical = start - em->start;
2217 logical = em->block_start + logical;
2218 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2219 logical = em->block_start;
2220 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2221 extent_set_compress_type(&failrec->bio_flags,
2222 em->compress_type);
2223 }
Miao Xie2fe63032014-09-12 18:43:59 +08002224
2225 pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n",
2226 logical, start, failrec->len);
2227
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002228 failrec->logical = logical;
2229 free_extent_map(em);
2230
2231 /* set the bits in the private failure tree */
2232 ret = set_extent_bits(failure_tree, start, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +02002233 EXTENT_LOCKED | EXTENT_DIRTY);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002234 if (ret >= 0)
David Sterba47dc1962016-02-11 13:24:13 +01002235 ret = set_state_failrec(failure_tree, start, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002236 /* set the bits in the inode's tree */
2237 if (ret >= 0)
David Sterbaceeb0ae2016-04-26 23:54:39 +02002238 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002239 if (ret < 0) {
2240 kfree(failrec);
2241 return ret;
2242 }
2243 } else {
Miao Xie2fe63032014-09-12 18:43:59 +08002244 pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002245 failrec->logical, failrec->start, failrec->len,
2246 failrec->in_validation);
2247 /*
2248 * when data can be on disk more than twice, add to failrec here
2249 * (e.g. with a list for failed_mirror) to make
2250 * clean_io_failure() clean all those errors at once.
2251 */
2252 }
Miao Xie2fe63032014-09-12 18:43:59 +08002253
2254 *failrec_ret = failrec;
2255
2256 return 0;
2257}
2258
2259int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
2260 struct io_failure_record *failrec, int failed_mirror)
2261{
2262 int num_copies;
2263
Stefan Behrens5d964052012-11-05 14:59:07 +01002264 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2265 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002266 if (num_copies == 1) {
2267 /*
2268 * we only have a single copy of the data, so don't bother with
2269 * all the retry and error correction code that follows. no
2270 * matter what the error is, it is very likely to persist.
2271 */
Miao Xie2fe63032014-09-12 18:43:59 +08002272 pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
Miao Xie09a7f7a2013-07-25 19:22:32 +08002273 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002274 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002275 }
2276
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002277 /*
2278 * there are two premises:
2279 * a) deliver good data to the caller
2280 * b) correct the bad sectors on disk
2281 */
2282 if (failed_bio->bi_vcnt > 1) {
2283 /*
2284 * to fulfill b), we need to know the exact failing sectors, as
2285 * we don't want to rewrite any more than the failed ones. thus,
2286 * we need separate read requests for the failed bio
2287 *
2288 * if the following BUG_ON triggers, our validation request got
2289 * merged. we need separate requests for our algorithm to work.
2290 */
2291 BUG_ON(failrec->in_validation);
2292 failrec->in_validation = 1;
2293 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002294 } else {
2295 /*
2296 * we're ready to fulfill a) and b) alongside. get a good copy
2297 * of the failed sector and if we succeed, we have setup
2298 * everything for repair_io_failure to do the rest for us.
2299 */
2300 if (failrec->in_validation) {
2301 BUG_ON(failrec->this_mirror != failed_mirror);
2302 failrec->in_validation = 0;
2303 failrec->this_mirror = 0;
2304 }
2305 failrec->failed_mirror = failed_mirror;
2306 failrec->this_mirror++;
2307 if (failrec->this_mirror == failed_mirror)
2308 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002309 }
2310
Miao Xiefacc8a22013-07-25 19:22:34 +08002311 if (failrec->this_mirror > num_copies) {
Miao Xie2fe63032014-09-12 18:43:59 +08002312 pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002313 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002314 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002315 }
2316
Miao Xie2fe63032014-09-12 18:43:59 +08002317 return 1;
2318}
2319
2320
2321struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2322 struct io_failure_record *failrec,
2323 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002324 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002325{
2326 struct bio *bio;
2327 struct btrfs_io_bio *btrfs_failed_bio;
2328 struct btrfs_io_bio *btrfs_bio;
2329
Chris Mason9be33952013-05-17 18:30:14 -04002330 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Miao Xie2fe63032014-09-12 18:43:59 +08002331 if (!bio)
2332 return NULL;
2333
2334 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002335 bio->bi_iter.bi_sector = failrec->logical >> 9;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002336 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002337 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002338 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002339
Miao Xiefacc8a22013-07-25 19:22:34 +08002340 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2341 if (btrfs_failed_bio->csum) {
2342 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2343 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2344
2345 btrfs_bio = btrfs_io_bio(bio);
2346 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002347 icsum *= csum_size;
2348 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a22013-07-25 19:22:34 +08002349 csum_size);
2350 }
2351
Miao Xie2fe63032014-09-12 18:43:59 +08002352 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002353
Miao Xie2fe63032014-09-12 18:43:59 +08002354 return bio;
2355}
2356
2357/*
2358 * this is a generic handler for readpage errors (default
2359 * readpage_io_failed_hook). if other copies exist, read those and write back
2360 * good data to the failed position. does not investigate in remapping the
2361 * failed extent elsewhere, hoping the device will be smart enough to do this as
2362 * needed
2363 */
2364
2365static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2366 struct page *page, u64 start, u64 end,
2367 int failed_mirror)
2368{
2369 struct io_failure_record *failrec;
2370 struct inode *inode = page->mapping->host;
2371 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2372 struct bio *bio;
2373 int read_mode;
2374 int ret;
2375
2376 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2377
2378 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2379 if (ret)
2380 return ret;
2381
2382 ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
2383 if (!ret) {
2384 free_io_failure(inode, failrec);
2385 return -EIO;
2386 }
2387
2388 if (failed_bio->bi_vcnt > 1)
2389 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2390 else
2391 read_mode = READ_SYNC;
2392
2393 phy_offset >>= inode->i_sb->s_blocksize_bits;
2394 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2395 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002396 (int)phy_offset, failed_bio->bi_end_io,
2397 NULL);
Miao Xie2fe63032014-09-12 18:43:59 +08002398 if (!bio) {
2399 free_io_failure(inode, failrec);
2400 return -EIO;
2401 }
2402
2403 pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n",
2404 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002405
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002406 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2407 failrec->this_mirror,
2408 failrec->bio_flags, 0);
Miao Xie6c387ab2014-09-12 18:43:57 +08002409 if (ret) {
Miao Xie454ff3d2014-09-12 18:43:58 +08002410 free_io_failure(inode, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002411 bio_put(bio);
2412 }
2413
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002414 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002415}
2416
Chris Masond1310b22008-01-24 16:13:08 -05002417/* lots and lots of room for performance fixes in the end_bio funcs */
2418
David Sterbab5227c02015-12-03 13:08:59 +01002419void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
Jeff Mahoney87826df2012-02-15 16:23:57 +01002420{
2421 int uptodate = (err == 0);
2422 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002423 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002424
2425 tree = &BTRFS_I(page->mapping->host)->io_tree;
2426
2427 if (tree->ops && tree->ops->writepage_end_io_hook) {
2428 ret = tree->ops->writepage_end_io_hook(page, start,
2429 end, NULL, uptodate);
2430 if (ret)
2431 uptodate = 0;
2432 }
2433
Jeff Mahoney87826df2012-02-15 16:23:57 +01002434 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002435 ClearPageUptodate(page);
2436 SetPageError(page);
Liu Bo5dca6ee2014-05-12 12:47:36 +08002437 ret = ret < 0 ? ret : -EIO;
2438 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002439 }
Jeff Mahoney87826df2012-02-15 16:23:57 +01002440}
2441
Chris Masond1310b22008-01-24 16:13:08 -05002442/*
2443 * after a writepage IO is done, we need to:
2444 * clear the uptodate bits on error
2445 * clear the writeback bits in the extent tree for this IO
2446 * end_page_writeback if the page has no more pending IO
2447 *
2448 * Scheduling is not allowed, so the extent state tree is expected
2449 * to have one and only one object corresponding to this IO.
2450 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002451static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002452{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002453 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002454 u64 start;
2455 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002456 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002457
Kent Overstreet2c30c712013-11-07 12:20:26 -08002458 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002459 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002460
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002461 /* We always issue full-page reads, but if some block
2462 * in a page fails to read, blk_update_request() will
2463 * advance bv_offset and adjust bv_len to compensate.
2464 * Print a warning for nonzero offsets, and an error
2465 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002466 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2467 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Frank Holtonefe120a2013-12-20 11:37:06 -05002468 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
2469 "partial page write in btrfs with offset %u and length %u",
2470 bvec->bv_offset, bvec->bv_len);
2471 else
2472 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
2473 "incomplete page write in btrfs with offset %u and "
2474 "length %u",
2475 bvec->bv_offset, bvec->bv_len);
2476 }
Chris Masond1310b22008-01-24 16:13:08 -05002477
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002478 start = page_offset(page);
2479 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002480
David Sterbab5227c02015-12-03 13:08:59 +01002481 end_extent_writepage(page, bio->bi_error, start, end);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002482 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002483 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002484
Chris Masond1310b22008-01-24 16:13:08 -05002485 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002486}
2487
Miao Xie883d0de2013-07-25 19:22:35 +08002488static void
2489endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2490 int uptodate)
2491{
2492 struct extent_state *cached = NULL;
2493 u64 end = start + len - 1;
2494
2495 if (uptodate && tree->track_uptodate)
2496 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2497 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2498}
2499
Chris Masond1310b22008-01-24 16:13:08 -05002500/*
2501 * after a readpage IO is done, we need to:
2502 * clear the uptodate bits on error
2503 * set the uptodate bits if things worked
2504 * set the page up to date if all extents in the tree are uptodate
2505 * clear the lock bit in the extent tree
2506 * unlock the page if there are no other extents locked for it
2507 *
2508 * Scheduling is not allowed, so the extent state tree is expected
2509 * to have one and only one object corresponding to this IO.
2510 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002511static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002512{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002513 struct bio_vec *bvec;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002514 int uptodate = !bio->bi_error;
Miao Xiefacc8a22013-07-25 19:22:34 +08002515 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002516 struct extent_io_tree *tree;
Miao Xiefacc8a22013-07-25 19:22:34 +08002517 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002518 u64 start;
2519 u64 end;
Miao Xiefacc8a22013-07-25 19:22:34 +08002520 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002521 u64 extent_start = 0;
2522 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002523 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002524 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002525 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002526
Kent Overstreet2c30c712013-11-07 12:20:26 -08002527 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002528 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002529 struct inode *inode = page->mapping->host;
Arne Jansen507903b2011-04-06 10:02:20 +00002530
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002531 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002532 "mirror=%u\n", (u64)bio->bi_iter.bi_sector,
2533 bio->bi_error, io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002534 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002535
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002536 /* We always issue full-page reads, but if some block
2537 * in a page fails to read, blk_update_request() will
2538 * advance bv_offset and adjust bv_len to compensate.
2539 * Print a warning for nonzero offsets, and an error
2540 * if they don't add up to a full page. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002541 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2542 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
Frank Holtonefe120a2013-12-20 11:37:06 -05002543 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
2544 "partial page read in btrfs with offset %u and length %u",
2545 bvec->bv_offset, bvec->bv_len);
2546 else
2547 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
2548 "incomplete page read in btrfs with offset %u and "
2549 "length %u",
2550 bvec->bv_offset, bvec->bv_len);
2551 }
Chris Masond1310b22008-01-24 16:13:08 -05002552
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002553 start = page_offset(page);
2554 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a22013-07-25 19:22:34 +08002555 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002556
Chris Mason9be33952013-05-17 18:30:14 -04002557 mirror = io_bio->mirror_num;
Miao Xief2a09da2013-07-25 19:22:33 +08002558 if (likely(uptodate && tree->ops &&
2559 tree->ops->readpage_end_io_hook)) {
Miao Xiefacc8a22013-07-25 19:22:34 +08002560 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2561 page, start, end,
2562 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002563 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002564 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002565 else
Miao Xie1203b682014-09-12 18:44:01 +08002566 clean_io_failure(inode, start, page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002567 }
Josef Bacikea466792012-03-26 21:57:36 -04002568
Miao Xief2a09da2013-07-25 19:22:33 +08002569 if (likely(uptodate))
2570 goto readpage_ok;
2571
2572 if (tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002573 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002574 if (!ret && !bio->bi_error)
Josef Bacikea466792012-03-26 21:57:36 -04002575 uptodate = 1;
Miao Xief2a09da2013-07-25 19:22:33 +08002576 } else {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002577 /*
2578 * The generic bio_readpage_error handles errors the
2579 * following way: If possible, new read requests are
2580 * created and submitted and will end up in
2581 * end_bio_extent_readpage as well (if we're lucky, not
2582 * in the !uptodate case). In that case it returns 0 and
2583 * we just go on with the next page in our bio. If it
2584 * can't handle the error it will return -EIO and we
2585 * remain responsible for that page.
2586 */
Miao Xiefacc8a22013-07-25 19:22:34 +08002587 ret = bio_readpage_error(bio, offset, page, start, end,
2588 mirror);
Chris Mason7e383262008-04-09 16:28:12 -04002589 if (ret == 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002590 uptodate = !bio->bi_error;
Liu Bo38c1c2e2014-08-19 23:33:13 +08002591 offset += len;
Chris Mason7e383262008-04-09 16:28:12 -04002592 continue;
2593 }
2594 }
Miao Xief2a09da2013-07-25 19:22:33 +08002595readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002596 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002597 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002598 pgoff_t end_index = i_size >> PAGE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002599 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002600
2601 /* Zero out the end if this page straddles i_size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002602 off = i_size & (PAGE_SIZE-1);
Liu Boa583c022014-08-19 23:32:22 +08002603 if (page->index == end_index && off)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002604 zero_user_segment(page, off, PAGE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002605 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002606 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002607 ClearPageUptodate(page);
2608 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002609 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002610 unlock_page(page);
Miao Xiefacc8a22013-07-25 19:22:34 +08002611 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002612
2613 if (unlikely(!uptodate)) {
2614 if (extent_len) {
2615 endio_readpage_release_extent(tree,
2616 extent_start,
2617 extent_len, 1);
2618 extent_start = 0;
2619 extent_len = 0;
2620 }
2621 endio_readpage_release_extent(tree, start,
2622 end - start + 1, 0);
2623 } else if (!extent_len) {
2624 extent_start = start;
2625 extent_len = end + 1 - start;
2626 } else if (extent_start + extent_len == start) {
2627 extent_len += end + 1 - start;
2628 } else {
2629 endio_readpage_release_extent(tree, extent_start,
2630 extent_len, uptodate);
2631 extent_start = start;
2632 extent_len = end + 1 - start;
2633 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002634 }
Chris Masond1310b22008-01-24 16:13:08 -05002635
Miao Xie883d0de2013-07-25 19:22:35 +08002636 if (extent_len)
2637 endio_readpage_release_extent(tree, extent_start, extent_len,
2638 uptodate);
Miao Xiefacc8a22013-07-25 19:22:34 +08002639 if (io_bio->end_io)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002640 io_bio->end_io(io_bio, bio->bi_error);
Chris Masond1310b22008-01-24 16:13:08 -05002641 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002642}
2643
Chris Mason9be33952013-05-17 18:30:14 -04002644/*
2645 * this allocates from the btrfs_bioset. We're returning a bio right now
2646 * but you can call btrfs_io_bio for the appropriate container_of magic
2647 */
Miao Xie88f794e2010-11-22 03:02:55 +00002648struct bio *
2649btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2650 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002651{
Miao Xiefacc8a22013-07-25 19:22:34 +08002652 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002653 struct bio *bio;
2654
Chris Mason9be33952013-05-17 18:30:14 -04002655 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002656
2657 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002658 while (!bio && (nr_vecs /= 2)) {
2659 bio = bio_alloc_bioset(gfp_flags,
2660 nr_vecs, btrfs_bioset);
2661 }
Chris Masond1310b22008-01-24 16:13:08 -05002662 }
2663
2664 if (bio) {
2665 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002666 bio->bi_iter.bi_sector = first_sector;
Miao Xiefacc8a22013-07-25 19:22:34 +08002667 btrfs_bio = btrfs_io_bio(bio);
2668 btrfs_bio->csum = NULL;
2669 btrfs_bio->csum_allocated = NULL;
2670 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002671 }
2672 return bio;
2673}
2674
Chris Mason9be33952013-05-17 18:30:14 -04002675struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2676{
Miao Xie23ea8e52014-09-12 18:43:54 +08002677 struct btrfs_io_bio *btrfs_bio;
2678 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002679
Miao Xie23ea8e52014-09-12 18:43:54 +08002680 new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2681 if (new) {
2682 btrfs_bio = btrfs_io_bio(new);
2683 btrfs_bio->csum = NULL;
2684 btrfs_bio->csum_allocated = NULL;
2685 btrfs_bio->end_io = NULL;
Chris Mason3a9508b2015-08-21 10:05:39 -07002686
2687#ifdef CONFIG_BLK_CGROUP
Chris Masonda2f0f72015-07-02 13:57:22 -07002688 /* FIXME, put this into bio_clone_bioset */
2689 if (bio->bi_css)
2690 bio_associate_blkcg(new, bio->bi_css);
Chris Mason3a9508b2015-08-21 10:05:39 -07002691#endif
Miao Xie23ea8e52014-09-12 18:43:54 +08002692 }
2693 return new;
2694}
Chris Mason9be33952013-05-17 18:30:14 -04002695
2696/* this also allocates from the btrfs_bioset */
2697struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2698{
Miao Xiefacc8a22013-07-25 19:22:34 +08002699 struct btrfs_io_bio *btrfs_bio;
2700 struct bio *bio;
2701
2702 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2703 if (bio) {
2704 btrfs_bio = btrfs_io_bio(bio);
2705 btrfs_bio->csum = NULL;
2706 btrfs_bio->csum_allocated = NULL;
2707 btrfs_bio->end_io = NULL;
2708 }
2709 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002710}
2711
2712
Jeff Mahoney355808c2011-10-03 23:23:14 -04002713static int __must_check submit_one_bio(int rw, struct bio *bio,
2714 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002715{
Chris Masond1310b22008-01-24 16:13:08 -05002716 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002717 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2718 struct page *page = bvec->bv_page;
2719 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002720 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002721
Miao Xie4eee4fa2012-12-21 09:17:45 +00002722 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002723
David Woodhouse902b22f2008-08-20 08:51:49 -04002724 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002725
2726 bio_get(bio);
2727
Chris Mason065631f2008-02-20 12:07:25 -05002728 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002729 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002730 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002731 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002732 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002733
Chris Masond1310b22008-01-24 16:13:08 -05002734 bio_put(bio);
2735 return ret;
2736}
2737
David Woodhouse64a16702009-07-15 23:29:37 +01002738static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002739 unsigned long offset, size_t size, struct bio *bio,
2740 unsigned long bio_flags)
2741{
2742 int ret = 0;
2743 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002744 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002745 bio_flags);
2746 BUG_ON(ret < 0);
2747 return ret;
2748
2749}
2750
Chris Masond1310b22008-01-24 16:13:08 -05002751static int submit_extent_page(int rw, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002752 struct writeback_control *wbc,
Chris Masond1310b22008-01-24 16:13:08 -05002753 struct page *page, sector_t sector,
2754 size_t size, unsigned long offset,
2755 struct block_device *bdev,
2756 struct bio **bio_ret,
2757 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002758 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002759 int mirror_num,
2760 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002761 unsigned long bio_flags,
2762 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002763{
2764 int ret = 0;
2765 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002766 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002767 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002768 size_t page_size = min_t(size_t, size, PAGE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002769
2770 if (bio_ret && *bio_ret) {
2771 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002772 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002773 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002774 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002775 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002776
2777 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002778 force_bio_submit ||
David Woodhouse64a16702009-07-15 23:29:37 +01002779 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002780 bio_add_page(bio, page, page_size, offset) < page_size) {
2781 ret = submit_one_bio(rw, bio, mirror_num,
2782 prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002783 if (ret < 0) {
2784 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002785 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002786 }
Chris Masond1310b22008-01-24 16:13:08 -05002787 bio = NULL;
2788 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002789 if (wbc)
2790 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002791 return 0;
2792 }
2793 }
Chris Masonc8b97812008-10-29 14:49:59 -04002794
Kent Overstreetb54ffb72015-05-19 14:31:01 +02002795 bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
2796 GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002797 if (!bio)
2798 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002799
Chris Masonc8b97812008-10-29 14:49:59 -04002800 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002801 bio->bi_end_io = end_io_func;
2802 bio->bi_private = tree;
Chris Masonda2f0f72015-07-02 13:57:22 -07002803 if (wbc) {
2804 wbc_init_bio(wbc, bio);
2805 wbc_account_io(wbc, page, page_size);
2806 }
Chris Mason70dec802008-01-29 09:59:12 -05002807
Chris Masond3977122009-01-05 21:25:51 -05002808 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002809 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002810 else
Chris Masonc8b97812008-10-29 14:49:59 -04002811 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002812
2813 return ret;
2814}
2815
Eric Sandeen48a3b632013-04-25 20:41:01 +00002816static void attach_extent_buffer_page(struct extent_buffer *eb,
2817 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002818{
2819 if (!PagePrivate(page)) {
2820 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002821 get_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002822 set_page_private(page, (unsigned long)eb);
2823 } else {
2824 WARN_ON(page->private != (unsigned long)eb);
2825 }
2826}
2827
Chris Masond1310b22008-01-24 16:13:08 -05002828void set_page_extent_mapped(struct page *page)
2829{
2830 if (!PagePrivate(page)) {
2831 SetPagePrivate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002832 get_page(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002833 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002834 }
2835}
2836
Miao Xie125bac012013-07-25 19:22:37 +08002837static struct extent_map *
2838__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2839 u64 start, u64 len, get_extent_t *get_extent,
2840 struct extent_map **em_cached)
2841{
2842 struct extent_map *em;
2843
2844 if (em_cached && *em_cached) {
2845 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002846 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002847 start < extent_map_end(em)) {
2848 atomic_inc(&em->refs);
2849 return em;
2850 }
2851
2852 free_extent_map(em);
2853 *em_cached = NULL;
2854 }
2855
2856 em = get_extent(inode, page, pg_offset, start, len, 0);
2857 if (em_cached && !IS_ERR_OR_NULL(em)) {
2858 BUG_ON(*em_cached);
2859 atomic_inc(&em->refs);
2860 *em_cached = em;
2861 }
2862 return em;
2863}
Chris Masond1310b22008-01-24 16:13:08 -05002864/*
2865 * basic readpage implementation. Locked extent state structs are inserted
2866 * into the tree that are removed when the IO is done (by the end_io
2867 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002868 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002869 */
Miao Xie99740902013-07-25 19:22:36 +08002870static int __do_readpage(struct extent_io_tree *tree,
2871 struct page *page,
2872 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002873 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002874 struct bio **bio, int mirror_num,
Filipe Manana005efed2015-09-14 09:09:31 +01002875 unsigned long *bio_flags, int rw,
2876 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002877{
2878 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002879 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002880 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002881 u64 end;
2882 u64 cur = start;
2883 u64 extent_offset;
2884 u64 last_byte = i_size_read(inode);
2885 u64 block_start;
2886 u64 cur_end;
2887 sector_t sector;
2888 struct extent_map *em;
2889 struct block_device *bdev;
2890 int ret;
2891 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002892 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002893 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002894 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002895 size_t blocksize = inode->i_sb->s_blocksize;
Filipe Manana7f042a82016-01-27 19:17:20 +00002896 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002897
2898 set_page_extent_mapped(page);
2899
Miao Xie99740902013-07-25 19:22:36 +08002900 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002901 if (!PageUptodate(page)) {
2902 if (cleancache_get_page(page) == 0) {
2903 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002904 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002905 goto out;
2906 }
2907 }
2908
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002909 if (page->index == last_byte >> PAGE_SHIFT) {
Chris Masonc8b97812008-10-29 14:49:59 -04002910 char *userpage;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002911 size_t zero_offset = last_byte & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002912
2913 if (zero_offset) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 iosize = PAGE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002915 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002916 memset(userpage + zero_offset, 0, iosize);
2917 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002918 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002919 }
2920 }
Chris Masond1310b22008-01-24 16:13:08 -05002921 while (cur <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002922 unsigned long pnr = (last_byte >> PAGE_SHIFT) + 1;
Filipe Manana005efed2015-09-14 09:09:31 +01002923 bool force_bio_submit = false;
Josef Bacikc8f2f242013-02-11 11:33:00 -05002924
Chris Masond1310b22008-01-24 16:13:08 -05002925 if (cur >= last_byte) {
2926 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002927 struct extent_state *cached = NULL;
2928
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002929 iosize = PAGE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002930 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002931 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002932 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002933 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002934 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002935 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00002936 unlock_extent_cached(tree, cur,
2937 cur + iosize - 1,
2938 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002939 break;
2940 }
Miao Xie125bac012013-07-25 19:22:37 +08002941 em = __get_extent_map(inode, page, pg_offset, cur,
2942 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002943 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002944 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00002945 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002946 break;
2947 }
Chris Masond1310b22008-01-24 16:13:08 -05002948 extent_offset = cur - em->start;
2949 BUG_ON(extent_map_end(em) <= cur);
2950 BUG_ON(end < cur);
2951
Li Zefan261507a02010-12-17 14:21:50 +08002952 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002953 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002954 extent_set_compress_type(&this_bio_flag,
2955 em->compress_type);
2956 }
Chris Masonc8b97812008-10-29 14:49:59 -04002957
Chris Masond1310b22008-01-24 16:13:08 -05002958 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2959 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002960 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002961 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2962 disk_io_size = em->block_len;
2963 sector = em->block_start >> 9;
2964 } else {
2965 sector = (em->block_start + extent_offset) >> 9;
2966 disk_io_size = iosize;
2967 }
Chris Masond1310b22008-01-24 16:13:08 -05002968 bdev = em->bdev;
2969 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002970 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2971 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01002972
2973 /*
2974 * If we have a file range that points to a compressed extent
2975 * and it's followed by a consecutive file range that points to
2976 * to the same compressed extent (possibly with a different
2977 * offset and/or length, so it either points to the whole extent
2978 * or only part of it), we must make sure we do not submit a
2979 * single bio to populate the pages for the 2 ranges because
2980 * this makes the compressed extent read zero out the pages
2981 * belonging to the 2nd range. Imagine the following scenario:
2982 *
2983 * File layout
2984 * [0 - 8K] [8K - 24K]
2985 * | |
2986 * | |
2987 * points to extent X, points to extent X,
2988 * offset 4K, length of 8K offset 0, length 16K
2989 *
2990 * [extent X, compressed length = 4K uncompressed length = 16K]
2991 *
2992 * If the bio to read the compressed extent covers both ranges,
2993 * it will decompress extent X into the pages belonging to the
2994 * first range and then it will stop, zeroing out the remaining
2995 * pages that belong to the other range that points to extent X.
2996 * So here we make sure we submit 2 bios, one for the first
2997 * range and another one for the third range. Both will target
2998 * the same physical extent from disk, but we can't currently
2999 * make the compressed bio endio callback populate the pages
3000 * for both ranges because each compressed bio is tightly
3001 * coupled with a single extent map, and each range can have
3002 * an extent map with a different offset value relative to the
3003 * uncompressed data of our extent and different lengths. This
3004 * is a corner case so we prioritize correctness over
3005 * non-optimal behavior (submitting 2 bios for the same extent).
3006 */
3007 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3008 prev_em_start && *prev_em_start != (u64)-1 &&
3009 *prev_em_start != em->orig_start)
3010 force_bio_submit = true;
3011
3012 if (prev_em_start)
3013 *prev_em_start = em->orig_start;
3014
Chris Masond1310b22008-01-24 16:13:08 -05003015 free_extent_map(em);
3016 em = NULL;
3017
3018 /* we've found a hole, just zero and go on */
3019 if (block_start == EXTENT_MAP_HOLE) {
3020 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003021 struct extent_state *cached = NULL;
3022
Cong Wang7ac687d2011-11-25 23:14:28 +08003023 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003024 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003025 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003026 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003027
3028 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003029 &cached, GFP_NOFS);
Filipe Manana7f042a82016-01-27 19:17:20 +00003030 unlock_extent_cached(tree, cur,
3031 cur + iosize - 1,
3032 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003033 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003034 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003035 continue;
3036 }
3037 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003038 if (test_range_bit(tree, cur, cur_end,
3039 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003040 check_page_uptodate(tree, page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003041 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003042 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003043 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003044 continue;
3045 }
Chris Mason70dec802008-01-29 09:59:12 -05003046 /* we have an inline extent but it didn't get marked up
3047 * to date. Error out
3048 */
3049 if (block_start == EXTENT_MAP_INLINE) {
3050 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003051 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003052 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003053 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003054 continue;
3055 }
Chris Masond1310b22008-01-24 16:13:08 -05003056
Josef Bacikc8f2f242013-02-11 11:33:00 -05003057 pnr -= page->index;
Chris Masonda2f0f72015-07-02 13:57:22 -07003058 ret = submit_extent_page(rw, tree, NULL, page,
David Sterba306e16c2011-04-19 14:29:38 +02003059 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04003060 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04003061 end_bio_extent_readpage, mirror_num,
3062 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003063 this_bio_flag,
3064 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003065 if (!ret) {
3066 nr++;
3067 *bio_flags = this_bio_flag;
3068 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003069 SetPageError(page);
Filipe Manana7f042a82016-01-27 19:17:20 +00003070 unlock_extent(tree, cur, cur + iosize - 1);
Josef Bacikedd33c92012-10-05 16:40:32 -04003071 }
Chris Masond1310b22008-01-24 16:13:08 -05003072 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003073 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003074 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003075out:
Chris Masond1310b22008-01-24 16:13:08 -05003076 if (!nr) {
3077 if (!PageError(page))
3078 SetPageUptodate(page);
3079 unlock_page(page);
3080 }
3081 return 0;
3082}
3083
Miao Xie99740902013-07-25 19:22:36 +08003084static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3085 struct page *pages[], int nr_pages,
3086 u64 start, u64 end,
3087 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003088 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003089 struct bio **bio, int mirror_num,
Filipe Manana808f80b2015-09-28 09:56:26 +01003090 unsigned long *bio_flags, int rw,
3091 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003092{
3093 struct inode *inode;
3094 struct btrfs_ordered_extent *ordered;
3095 int index;
3096
3097 inode = pages[0]->mapping->host;
3098 while (1) {
3099 lock_extent(tree, start, end);
3100 ordered = btrfs_lookup_ordered_range(inode, start,
3101 end - start + 1);
3102 if (!ordered)
3103 break;
3104 unlock_extent(tree, start, end);
3105 btrfs_start_ordered_extent(inode, ordered, 1);
3106 btrfs_put_ordered_extent(ordered);
3107 }
3108
3109 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003110 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Filipe Manana808f80b2015-09-28 09:56:26 +01003111 mirror_num, bio_flags, rw, prev_em_start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003112 put_page(pages[index]);
Miao Xie99740902013-07-25 19:22:36 +08003113 }
3114}
3115
3116static void __extent_readpages(struct extent_io_tree *tree,
3117 struct page *pages[],
3118 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003119 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003120 struct bio **bio, int mirror_num,
Filipe Manana808f80b2015-09-28 09:56:26 +01003121 unsigned long *bio_flags, int rw,
3122 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003123{
Stefan Behrens35a36212013-08-14 18:12:25 +02003124 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003125 u64 end = 0;
3126 u64 page_start;
3127 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003128 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003129
3130 for (index = 0; index < nr_pages; index++) {
3131 page_start = page_offset(pages[index]);
3132 if (!end) {
3133 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003134 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003135 first_index = index;
3136 } else if (end + 1 == page_start) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003137 end += PAGE_SIZE;
Miao Xie99740902013-07-25 19:22:36 +08003138 } else {
3139 __do_contiguous_readpages(tree, &pages[first_index],
3140 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003141 end, get_extent, em_cached,
3142 bio, mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003143 rw, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003144 start = page_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003145 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003146 first_index = index;
3147 }
3148 }
3149
3150 if (end)
3151 __do_contiguous_readpages(tree, &pages[first_index],
3152 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003153 end, get_extent, em_cached, bio,
Filipe Manana808f80b2015-09-28 09:56:26 +01003154 mirror_num, bio_flags, rw,
3155 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003156}
3157
3158static int __extent_read_full_page(struct extent_io_tree *tree,
3159 struct page *page,
3160 get_extent_t *get_extent,
3161 struct bio **bio, int mirror_num,
3162 unsigned long *bio_flags, int rw)
3163{
3164 struct inode *inode = page->mapping->host;
3165 struct btrfs_ordered_extent *ordered;
3166 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003167 u64 end = start + PAGE_SIZE - 1;
Miao Xie99740902013-07-25 19:22:36 +08003168 int ret;
3169
3170 while (1) {
3171 lock_extent(tree, start, end);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05303172 ordered = btrfs_lookup_ordered_range(inode, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003173 PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08003174 if (!ordered)
3175 break;
3176 unlock_extent(tree, start, end);
3177 btrfs_start_ordered_extent(inode, ordered, 1);
3178 btrfs_put_ordered_extent(ordered);
3179 }
3180
Miao Xie125bac012013-07-25 19:22:37 +08003181 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Filipe Manana005efed2015-09-14 09:09:31 +01003182 bio_flags, rw, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003183 return ret;
3184}
3185
Chris Masond1310b22008-01-24 16:13:08 -05003186int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003187 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003188{
3189 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003190 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003191 int ret;
3192
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003193 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003194 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05003195 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003196 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003197 return ret;
3198}
Chris Masond1310b22008-01-24 16:13:08 -05003199
Chris Mason11c83492009-04-20 15:50:09 -04003200static noinline void update_nr_written(struct page *page,
3201 struct writeback_control *wbc,
3202 unsigned long nr_written)
3203{
3204 wbc->nr_to_write -= nr_written;
3205 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
3206 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
3207 page->mapping->writeback_index = page->index + nr_written;
3208}
3209
Chris Masond1310b22008-01-24 16:13:08 -05003210/*
Chris Mason40f76582014-05-21 13:35:51 -07003211 * helper for __extent_writepage, doing all of the delayed allocation setup.
3212 *
3213 * This returns 1 if our fill_delalloc function did all the work required
3214 * to write the page (copy into inline extent). In this case the IO has
3215 * been started and the page is already unlocked.
3216 *
3217 * This returns 0 if all went well (page still locked)
3218 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003219 */
Chris Mason40f76582014-05-21 13:35:51 -07003220static noinline_for_stack int writepage_delalloc(struct inode *inode,
3221 struct page *page, struct writeback_control *wbc,
3222 struct extent_page_data *epd,
3223 u64 delalloc_start,
3224 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003225{
Chris Mason40f76582014-05-21 13:35:51 -07003226 struct extent_io_tree *tree = epd->tree;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003227 u64 page_end = delalloc_start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003228 u64 nr_delalloc;
3229 u64 delalloc_to_write = 0;
3230 u64 delalloc_end = 0;
3231 int ret;
3232 int page_started = 0;
3233
3234 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3235 return 0;
3236
3237 while (delalloc_end < page_end) {
3238 nr_delalloc = find_lock_delalloc_range(inode, tree,
3239 page,
3240 &delalloc_start,
3241 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003242 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003243 if (nr_delalloc == 0) {
3244 delalloc_start = delalloc_end + 1;
3245 continue;
3246 }
3247 ret = tree->ops->fill_delalloc(inode, page,
3248 delalloc_start,
3249 delalloc_end,
3250 &page_started,
3251 nr_written);
3252 /* File system has been set read-only */
3253 if (ret) {
3254 SetPageError(page);
3255 /* fill_delalloc should be return < 0 for error
3256 * but just in case, we use > 0 here meaning the
3257 * IO is started, so we don't want to return > 0
3258 * unless things are going well.
3259 */
3260 ret = ret < 0 ? ret : -EIO;
3261 goto done;
3262 }
3263 /*
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003264 * delalloc_end is already one less than the total length, so
3265 * we don't subtract one from PAGE_SIZE
Chris Mason40f76582014-05-21 13:35:51 -07003266 */
3267 delalloc_to_write += (delalloc_end - delalloc_start +
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03003268 PAGE_SIZE) >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003269 delalloc_start = delalloc_end + 1;
3270 }
3271 if (wbc->nr_to_write < delalloc_to_write) {
3272 int thresh = 8192;
3273
3274 if (delalloc_to_write < thresh * 2)
3275 thresh = delalloc_to_write;
3276 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3277 thresh);
3278 }
3279
3280 /* did the fill delalloc function already unlock and start
3281 * the IO?
3282 */
3283 if (page_started) {
3284 /*
3285 * we've unlocked the page, so we can't update
3286 * the mapping's writeback index, just update
3287 * nr_to_write.
3288 */
3289 wbc->nr_to_write -= *nr_written;
3290 return 1;
3291 }
3292
3293 ret = 0;
3294
3295done:
3296 return ret;
3297}
3298
3299/*
3300 * helper for __extent_writepage. This calls the writepage start hooks,
3301 * and does the loop to map the page into extents and bios.
3302 *
3303 * We return 1 if the IO is started and the page is unlocked,
3304 * 0 if all went well (page still locked)
3305 * < 0 if there were errors (page still locked)
3306 */
3307static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3308 struct page *page,
3309 struct writeback_control *wbc,
3310 struct extent_page_data *epd,
3311 loff_t i_size,
3312 unsigned long nr_written,
3313 int write_flags, int *nr_ret)
3314{
Chris Masond1310b22008-01-24 16:13:08 -05003315 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003316 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003317 u64 page_end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003318 u64 end;
3319 u64 cur = start;
3320 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003321 u64 block_start;
3322 u64 iosize;
3323 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04003324 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003325 struct extent_map *em;
3326 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003327 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003328 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003329 int ret = 0;
3330 int nr = 0;
3331 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003332
Chris Mason247e7432008-07-17 12:53:51 -04003333 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003334 ret = tree->ops->writepage_start_hook(page, start,
3335 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003336 if (ret) {
3337 /* Fixup worker will requeue */
3338 if (ret == -EBUSY)
3339 wbc->pages_skipped++;
3340 else
3341 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003342
Chris Mason11c83492009-04-20 15:50:09 -04003343 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003344 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003345 ret = 1;
Chris Mason11c83492009-04-20 15:50:09 -04003346 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003347 }
3348 }
3349
Chris Mason11c83492009-04-20 15:50:09 -04003350 /*
3351 * we don't want to touch the inode after unlocking the page,
3352 * so we update the mapping writeback index now
3353 */
3354 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003355
Chris Masond1310b22008-01-24 16:13:08 -05003356 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003357 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003358 if (tree->ops && tree->ops->writepage_end_io_hook)
3359 tree->ops->writepage_end_io_hook(page, start,
3360 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003361 goto done;
3362 }
3363
Chris Masond1310b22008-01-24 16:13:08 -05003364 blocksize = inode->i_sb->s_blocksize;
3365
3366 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003367 u64 em_end;
3368 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003369 if (tree->ops && tree->ops->writepage_end_io_hook)
3370 tree->ops->writepage_end_io_hook(page, cur,
3371 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003372 break;
3373 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003374 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003375 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003376 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003377 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003378 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003379 break;
3380 }
3381
3382 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003383 em_end = extent_map_end(em);
3384 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003385 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003386 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003387 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003388 sector = (em->block_start + extent_offset) >> 9;
3389 bdev = em->bdev;
3390 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003391 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003392 free_extent_map(em);
3393 em = NULL;
3394
Chris Masonc8b97812008-10-29 14:49:59 -04003395 /*
3396 * compressed and inline extents are written through other
3397 * paths in the FS
3398 */
3399 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003400 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003401 /*
3402 * end_io notification does not happen here for
3403 * compressed extents
3404 */
3405 if (!compressed && tree->ops &&
3406 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003407 tree->ops->writepage_end_io_hook(page, cur,
3408 cur + iosize - 1,
3409 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003410 else if (compressed) {
3411 /* we don't want to end_page_writeback on
3412 * a compressed extent. this happens
3413 * elsewhere
3414 */
3415 nr++;
3416 }
3417
3418 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003419 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003420 continue;
3421 }
Chris Masonc8b97812008-10-29 14:49:59 -04003422
Chris Masond1310b22008-01-24 16:13:08 -05003423 if (tree->ops && tree->ops->writepage_io_hook) {
3424 ret = tree->ops->writepage_io_hook(page, cur,
3425 cur + iosize - 1);
3426 } else {
3427 ret = 0;
3428 }
Chris Mason1259ab72008-05-12 13:39:03 -04003429 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003430 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003431 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003432 unsigned long max_nr = (i_size >> PAGE_SHIFT) + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003433
Chris Masond1310b22008-01-24 16:13:08 -05003434 set_range_writeback(tree, cur, cur + iosize - 1);
3435 if (!PageWriteback(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003436 btrfs_err(BTRFS_I(inode)->root->fs_info,
3437 "page %lu not writeback, cur %llu end %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003438 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003439 }
3440
Chris Masonda2f0f72015-07-02 13:57:22 -07003441 ret = submit_extent_page(write_flags, tree, wbc, page,
Chris Masonffbd5172009-04-20 15:50:09 -04003442 sector, iosize, pg_offset,
3443 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003444 end_bio_extent_writepage,
Filipe Manana005efed2015-09-14 09:09:31 +01003445 0, 0, 0, false);
Chris Masond1310b22008-01-24 16:13:08 -05003446 if (ret)
3447 SetPageError(page);
3448 }
3449 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003450 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003451 nr++;
3452 }
3453done:
Chris Mason40f76582014-05-21 13:35:51 -07003454 *nr_ret = nr;
Chris Mason771ed682008-11-06 22:02:51 -05003455
Chris Mason11c83492009-04-20 15:50:09 -04003456done_unlocked:
3457
Chris Mason2c64c532009-09-02 15:04:12 -04003458 /* drop our reference on any cached states */
3459 free_extent_state(cached_state);
Chris Mason40f76582014-05-21 13:35:51 -07003460 return ret;
3461}
3462
3463/*
3464 * the writepage semantics are similar to regular writepage. extent
3465 * records are inserted to lock ranges in the tree, and as dirty areas
3466 * are found, they are marked writeback. Then the lock bits are removed
3467 * and the end_io handler clears the writeback ranges
3468 */
3469static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3470 void *data)
3471{
3472 struct inode *inode = page->mapping->host;
3473 struct extent_page_data *epd = data;
3474 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003475 u64 page_end = start + PAGE_SIZE - 1;
Chris Mason40f76582014-05-21 13:35:51 -07003476 int ret;
3477 int nr = 0;
3478 size_t pg_offset = 0;
3479 loff_t i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003480 unsigned long end_index = i_size >> PAGE_SHIFT;
Chris Mason40f76582014-05-21 13:35:51 -07003481 int write_flags;
3482 unsigned long nr_written = 0;
3483
3484 if (wbc->sync_mode == WB_SYNC_ALL)
3485 write_flags = WRITE_SYNC;
3486 else
3487 write_flags = WRITE;
3488
3489 trace___extent_writepage(page, inode, wbc);
3490
3491 WARN_ON(!PageLocked(page));
3492
3493 ClearPageError(page);
3494
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003495 pg_offset = i_size & (PAGE_SIZE - 1);
Chris Mason40f76582014-05-21 13:35:51 -07003496 if (page->index > end_index ||
3497 (page->index == end_index && !pg_offset)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003498 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003499 unlock_page(page);
3500 return 0;
3501 }
3502
3503 if (page->index == end_index) {
3504 char *userpage;
3505
3506 userpage = kmap_atomic(page);
3507 memset(userpage + pg_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003508 PAGE_SIZE - pg_offset);
Chris Mason40f76582014-05-21 13:35:51 -07003509 kunmap_atomic(userpage);
3510 flush_dcache_page(page);
3511 }
3512
3513 pg_offset = 0;
3514
3515 set_page_extent_mapped(page);
3516
3517 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3518 if (ret == 1)
3519 goto done_unlocked;
3520 if (ret)
3521 goto done;
3522
3523 ret = __extent_writepage_io(inode, page, wbc, epd,
3524 i_size, nr_written, write_flags, &nr);
3525 if (ret == 1)
3526 goto done_unlocked;
3527
3528done:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003529 if (nr == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003530 /* make sure the mapping tag for page dirty gets cleared */
Chris Mason771ed682008-11-06 22:02:51 -05003531 set_page_writeback(page);
3532 end_page_writeback(page);
3533 }
Filipe Manana61391d52014-05-09 17:17:40 +01003534 if (PageError(page)) {
3535 ret = ret < 0 ? ret : -EIO;
3536 end_extent_writepage(page, ret, start, page_end);
3537 }
Christoph Hellwigb2950862008-12-02 09:54:17 -05003538 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003539 return ret;
Chris Mason4bef0842008-09-08 11:18:08 -04003540
3541done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003542 return 0;
3543}
3544
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003545void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003546{
NeilBrown74316202014-07-07 15:16:04 +10003547 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3548 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003549}
3550
Chris Mason0e378df2014-05-19 20:55:27 -07003551static noinline_for_stack int
3552lock_extent_buffer_for_io(struct extent_buffer *eb,
3553 struct btrfs_fs_info *fs_info,
3554 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003555{
3556 unsigned long i, num_pages;
3557 int flush = 0;
3558 int ret = 0;
3559
3560 if (!btrfs_try_tree_write_lock(eb)) {
3561 flush = 1;
3562 flush_write_bio(epd);
3563 btrfs_tree_lock(eb);
3564 }
3565
3566 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3567 btrfs_tree_unlock(eb);
3568 if (!epd->sync_io)
3569 return 0;
3570 if (!flush) {
3571 flush_write_bio(epd);
3572 flush = 1;
3573 }
Chris Masona098d8e2012-03-21 12:09:56 -04003574 while (1) {
3575 wait_on_extent_buffer_writeback(eb);
3576 btrfs_tree_lock(eb);
3577 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3578 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003579 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003580 }
3581 }
3582
Josef Bacik51561ff2012-07-20 16:25:24 -04003583 /*
3584 * We need to do this to prevent races in people who check if the eb is
3585 * under IO since we can end up having no IO bits set for a short period
3586 * of time.
3587 */
3588 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003589 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3590 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003591 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003592 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003593 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3594 -eb->len,
3595 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003596 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003597 } else {
3598 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003599 }
3600
3601 btrfs_tree_unlock(eb);
3602
3603 if (!ret)
3604 return ret;
3605
3606 num_pages = num_extent_pages(eb->start, eb->len);
3607 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003608 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003609
3610 if (!trylock_page(p)) {
3611 if (!flush) {
3612 flush_write_bio(epd);
3613 flush = 1;
3614 }
3615 lock_page(p);
3616 }
3617 }
3618
3619 return ret;
3620}
3621
3622static void end_extent_buffer_writeback(struct extent_buffer *eb)
3623{
3624 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003625 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003626 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3627}
3628
Filipe Manana656f30d2014-09-26 12:25:56 +01003629static void set_btree_ioerr(struct page *page)
3630{
3631 struct extent_buffer *eb = (struct extent_buffer *)page->private;
3632 struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode);
3633
3634 SetPageError(page);
3635 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3636 return;
3637
3638 /*
3639 * If writeback for a btree extent that doesn't belong to a log tree
3640 * failed, increment the counter transaction->eb_write_errors.
3641 * We do this because while the transaction is running and before it's
3642 * committing (when we call filemap_fdata[write|wait]_range against
3643 * the btree inode), we might have
3644 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3645 * returns an error or an error happens during writeback, when we're
3646 * committing the transaction we wouldn't know about it, since the pages
3647 * can be no longer dirty nor marked anymore for writeback (if a
3648 * subsequent modification to the extent buffer didn't happen before the
3649 * transaction commit), which makes filemap_fdata[write|wait]_range not
3650 * able to find the pages tagged with SetPageError at transaction
3651 * commit time. So if this happens we must abort the transaction,
3652 * otherwise we commit a super block with btree roots that point to
3653 * btree nodes/leafs whose content on disk is invalid - either garbage
3654 * or the content of some node/leaf from a past generation that got
3655 * cowed or deleted and is no longer valid.
3656 *
3657 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3658 * not be enough - we need to distinguish between log tree extents vs
3659 * non-log tree extents, and the next filemap_fdatawait_range() call
3660 * will catch and clear such errors in the mapping - and that call might
3661 * be from a log sync and not from a transaction commit. Also, checking
3662 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3663 * not done and would not be reliable - the eb might have been released
3664 * from memory and reading it back again means that flag would not be
3665 * set (since it's a runtime flag, not persisted on disk).
3666 *
3667 * Using the flags below in the btree inode also makes us achieve the
3668 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3669 * writeback for all dirty pages and before filemap_fdatawait_range()
3670 * is called, the writeback for all dirty pages had already finished
3671 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3672 * filemap_fdatawait_range() would return success, as it could not know
3673 * that writeback errors happened (the pages were no longer tagged for
3674 * writeback).
3675 */
3676 switch (eb->log_index) {
3677 case -1:
3678 set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags);
3679 break;
3680 case 0:
3681 set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
3682 break;
3683 case 1:
3684 set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
3685 break;
3686 default:
3687 BUG(); /* unexpected, logic error */
3688 }
3689}
3690
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003691static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003692{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003693 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003694 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003695 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003696
Kent Overstreet2c30c712013-11-07 12:20:26 -08003697 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003698 struct page *page = bvec->bv_page;
3699
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003700 eb = (struct extent_buffer *)page->private;
3701 BUG_ON(!eb);
3702 done = atomic_dec_and_test(&eb->io_pages);
3703
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003704 if (bio->bi_error ||
3705 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003706 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003707 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003708 }
3709
3710 end_page_writeback(page);
3711
3712 if (!done)
3713 continue;
3714
3715 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003716 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003717
3718 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003719}
3720
Chris Mason0e378df2014-05-19 20:55:27 -07003721static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003722 struct btrfs_fs_info *fs_info,
3723 struct writeback_control *wbc,
3724 struct extent_page_data *epd)
3725{
3726 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003727 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003728 u64 offset = eb->start;
3729 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003730 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003731 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003732 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003733
Filipe Manana656f30d2014-09-26 12:25:56 +01003734 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003735 num_pages = num_extent_pages(eb->start, eb->len);
3736 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003737 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3738 bio_flags = EXTENT_BIO_TREE_LOG;
3739
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003740 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003741 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003742
3743 clear_page_dirty_for_io(p);
3744 set_page_writeback(p);
Chris Masonda2f0f72015-07-02 13:57:22 -07003745 ret = submit_extent_page(rw, tree, wbc, p, offset >> 9,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003746 PAGE_SIZE, 0, bdev, &epd->bio,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003747 -1, end_bio_extent_buffer_writepage,
Filipe Manana005efed2015-09-14 09:09:31 +01003748 0, epd->bio_flags, bio_flags, false);
Josef Bacikde0022b2012-09-25 14:25:58 -04003749 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003750 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003751 set_btree_ioerr(p);
Filipe Manana55e3bd22014-09-22 17:41:04 +01003752 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003753 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3754 end_extent_buffer_writeback(eb);
3755 ret = -EIO;
3756 break;
3757 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003758 offset += PAGE_SIZE;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003759 update_nr_written(p, wbc, 1);
3760 unlock_page(p);
3761 }
3762
3763 if (unlikely(ret)) {
3764 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003765 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003766 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003767 unlock_page(p);
3768 }
3769 }
3770
3771 return ret;
3772}
3773
3774int btree_write_cache_pages(struct address_space *mapping,
3775 struct writeback_control *wbc)
3776{
3777 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3778 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3779 struct extent_buffer *eb, *prev_eb = NULL;
3780 struct extent_page_data epd = {
3781 .bio = NULL,
3782 .tree = tree,
3783 .extent_locked = 0,
3784 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003785 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003786 };
3787 int ret = 0;
3788 int done = 0;
3789 int nr_to_write_done = 0;
3790 struct pagevec pvec;
3791 int nr_pages;
3792 pgoff_t index;
3793 pgoff_t end; /* Inclusive */
3794 int scanned = 0;
3795 int tag;
3796
3797 pagevec_init(&pvec, 0);
3798 if (wbc->range_cyclic) {
3799 index = mapping->writeback_index; /* Start from prev offset */
3800 end = -1;
3801 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003802 index = wbc->range_start >> PAGE_SHIFT;
3803 end = wbc->range_end >> PAGE_SHIFT;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003804 scanned = 1;
3805 }
3806 if (wbc->sync_mode == WB_SYNC_ALL)
3807 tag = PAGECACHE_TAG_TOWRITE;
3808 else
3809 tag = PAGECACHE_TAG_DIRTY;
3810retry:
3811 if (wbc->sync_mode == WB_SYNC_ALL)
3812 tag_pages_for_writeback(mapping, index, end);
3813 while (!done && !nr_to_write_done && (index <= end) &&
3814 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3815 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3816 unsigned i;
3817
3818 scanned = 1;
3819 for (i = 0; i < nr_pages; i++) {
3820 struct page *page = pvec.pages[i];
3821
3822 if (!PagePrivate(page))
3823 continue;
3824
3825 if (!wbc->range_cyclic && page->index > end) {
3826 done = 1;
3827 break;
3828 }
3829
Josef Bacikb5bae262012-09-14 13:43:01 -04003830 spin_lock(&mapping->private_lock);
3831 if (!PagePrivate(page)) {
3832 spin_unlock(&mapping->private_lock);
3833 continue;
3834 }
3835
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003836 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003837
3838 /*
3839 * Shouldn't happen and normally this would be a BUG_ON
3840 * but no sense in crashing the users box for something
3841 * we can survive anyway.
3842 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303843 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003844 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003845 continue;
3846 }
3847
Josef Bacikb5bae262012-09-14 13:43:01 -04003848 if (eb == prev_eb) {
3849 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003850 continue;
3851 }
3852
Josef Bacikb5bae262012-09-14 13:43:01 -04003853 ret = atomic_inc_not_zero(&eb->refs);
3854 spin_unlock(&mapping->private_lock);
3855 if (!ret)
3856 continue;
3857
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003858 prev_eb = eb;
3859 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3860 if (!ret) {
3861 free_extent_buffer(eb);
3862 continue;
3863 }
3864
3865 ret = write_one_eb(eb, fs_info, wbc, &epd);
3866 if (ret) {
3867 done = 1;
3868 free_extent_buffer(eb);
3869 break;
3870 }
3871 free_extent_buffer(eb);
3872
3873 /*
3874 * the filesystem may choose to bump up nr_to_write.
3875 * We have to make sure to honor the new nr_to_write
3876 * at any time
3877 */
3878 nr_to_write_done = wbc->nr_to_write <= 0;
3879 }
3880 pagevec_release(&pvec);
3881 cond_resched();
3882 }
3883 if (!scanned && !done) {
3884 /*
3885 * We hit the last page and there is more work to be done: wrap
3886 * back to the start of the file
3887 */
3888 scanned = 1;
3889 index = 0;
3890 goto retry;
3891 }
3892 flush_write_bio(&epd);
3893 return ret;
3894}
3895
Chris Masond1310b22008-01-24 16:13:08 -05003896/**
3897 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3898 * @mapping: address space structure to write
3899 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3900 * @writepage: function called for each page
3901 * @data: data passed to writepage function
3902 *
3903 * If a page is already under I/O, write_cache_pages() skips it, even
3904 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3905 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3906 * and msync() need to guarantee that all the data which was dirty at the time
3907 * the call was made get new I/O started against them. If wbc->sync_mode is
3908 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3909 * existing IO to complete.
3910 */
Chris Mason4bef0842008-09-08 11:18:08 -04003911static int extent_write_cache_pages(struct extent_io_tree *tree,
3912 struct address_space *mapping,
3913 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003914 writepage_t writepage, void *data,
3915 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003916{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003917 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003918 int ret = 0;
3919 int done = 0;
Filipe Manana61391d52014-05-09 17:17:40 +01003920 int err = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003921 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003922 struct pagevec pvec;
3923 int nr_pages;
3924 pgoff_t index;
3925 pgoff_t end; /* Inclusive */
3926 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003927 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003928
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003929 /*
3930 * We have to hold onto the inode so that ordered extents can do their
3931 * work when the IO finishes. The alternative to this is failing to add
3932 * an ordered extent if the igrab() fails there and that is a huge pain
3933 * to deal with, so instead just hold onto the inode throughout the
3934 * writepages operation. If it fails here we are freeing up the inode
3935 * anyway and we'd rather not waste our time writing out stuff that is
3936 * going to be truncated anyway.
3937 */
3938 if (!igrab(inode))
3939 return 0;
3940
Chris Masond1310b22008-01-24 16:13:08 -05003941 pagevec_init(&pvec, 0);
3942 if (wbc->range_cyclic) {
3943 index = mapping->writeback_index; /* Start from prev offset */
3944 end = -1;
3945 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003946 index = wbc->range_start >> PAGE_SHIFT;
3947 end = wbc->range_end >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003948 scanned = 1;
3949 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003950 if (wbc->sync_mode == WB_SYNC_ALL)
3951 tag = PAGECACHE_TAG_TOWRITE;
3952 else
3953 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003954retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003955 if (wbc->sync_mode == WB_SYNC_ALL)
3956 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003957 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003958 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3959 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003960 unsigned i;
3961
3962 scanned = 1;
3963 for (i = 0; i < nr_pages; i++) {
3964 struct page *page = pvec.pages[i];
3965
3966 /*
3967 * At this point we hold neither mapping->tree_lock nor
3968 * lock on the page itself: the page may be truncated or
3969 * invalidated (changing page->mapping to NULL), or even
3970 * swizzled back from swapper_space to tmpfs file
3971 * mapping
3972 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003973 if (!trylock_page(page)) {
3974 flush_fn(data);
3975 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003976 }
Chris Masond1310b22008-01-24 16:13:08 -05003977
3978 if (unlikely(page->mapping != mapping)) {
3979 unlock_page(page);
3980 continue;
3981 }
3982
3983 if (!wbc->range_cyclic && page->index > end) {
3984 done = 1;
3985 unlock_page(page);
3986 continue;
3987 }
3988
Chris Masond2c3f4f2008-11-19 12:44:22 -05003989 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003990 if (PageWriteback(page))
3991 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003992 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003993 }
Chris Masond1310b22008-01-24 16:13:08 -05003994
3995 if (PageWriteback(page) ||
3996 !clear_page_dirty_for_io(page)) {
3997 unlock_page(page);
3998 continue;
3999 }
4000
4001 ret = (*writepage)(page, wbc, data);
4002
4003 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4004 unlock_page(page);
4005 ret = 0;
4006 }
Filipe Manana61391d52014-05-09 17:17:40 +01004007 if (!err && ret < 0)
4008 err = ret;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004009
4010 /*
4011 * the filesystem may choose to bump up nr_to_write.
4012 * We have to make sure to honor the new nr_to_write
4013 * at any time
4014 */
4015 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004016 }
4017 pagevec_release(&pvec);
4018 cond_resched();
4019 }
Filipe Manana61391d52014-05-09 17:17:40 +01004020 if (!scanned && !done && !err) {
Chris Masond1310b22008-01-24 16:13:08 -05004021 /*
4022 * We hit the last page and there is more work to be done: wrap
4023 * back to the start of the file
4024 */
4025 scanned = 1;
4026 index = 0;
4027 goto retry;
4028 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004029 btrfs_add_delayed_iput(inode);
Filipe Manana61391d52014-05-09 17:17:40 +01004030 return err;
Chris Masond1310b22008-01-24 16:13:08 -05004031}
Chris Masond1310b22008-01-24 16:13:08 -05004032
Chris Masonffbd5172009-04-20 15:50:09 -04004033static void flush_epd_write_bio(struct extent_page_data *epd)
4034{
4035 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004036 int rw = WRITE;
4037 int ret;
4038
Chris Masonffbd5172009-04-20 15:50:09 -04004039 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04004040 rw = WRITE_SYNC;
4041
Josef Bacikde0022b2012-09-25 14:25:58 -04004042 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004043 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004044 epd->bio = NULL;
4045 }
4046}
4047
Chris Masond2c3f4f2008-11-19 12:44:22 -05004048static noinline void flush_write_bio(void *data)
4049{
4050 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004051 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004052}
4053
Chris Masond1310b22008-01-24 16:13:08 -05004054int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4055 get_extent_t *get_extent,
4056 struct writeback_control *wbc)
4057{
4058 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004059 struct extent_page_data epd = {
4060 .bio = NULL,
4061 .tree = tree,
4062 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004063 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004064 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004065 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004066 };
Chris Masond1310b22008-01-24 16:13:08 -05004067
Chris Masond1310b22008-01-24 16:13:08 -05004068 ret = __extent_writepage(page, wbc, &epd);
4069
Chris Masonffbd5172009-04-20 15:50:09 -04004070 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004071 return ret;
4072}
Chris Masond1310b22008-01-24 16:13:08 -05004073
Chris Mason771ed682008-11-06 22:02:51 -05004074int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4075 u64 start, u64 end, get_extent_t *get_extent,
4076 int mode)
4077{
4078 int ret = 0;
4079 struct address_space *mapping = inode->i_mapping;
4080 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004081 unsigned long nr_pages = (end - start + PAGE_SIZE) >>
4082 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05004083
4084 struct extent_page_data epd = {
4085 .bio = NULL,
4086 .tree = tree,
4087 .get_extent = get_extent,
4088 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004089 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004090 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05004091 };
4092 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004093 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004094 .nr_to_write = nr_pages * 2,
4095 .range_start = start,
4096 .range_end = end + 1,
4097 };
4098
Chris Masond3977122009-01-05 21:25:51 -05004099 while (start <= end) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004100 page = find_get_page(mapping, start >> PAGE_SHIFT);
Chris Mason771ed682008-11-06 22:02:51 -05004101 if (clear_page_dirty_for_io(page))
4102 ret = __extent_writepage(page, &wbc_writepages, &epd);
4103 else {
4104 if (tree->ops && tree->ops->writepage_end_io_hook)
4105 tree->ops->writepage_end_io_hook(page, start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004106 start + PAGE_SIZE - 1,
Chris Mason771ed682008-11-06 22:02:51 -05004107 NULL, 1);
4108 unlock_page(page);
4109 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004110 put_page(page);
4111 start += PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05004112 }
4113
Chris Masonffbd5172009-04-20 15:50:09 -04004114 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004115 return ret;
4116}
Chris Masond1310b22008-01-24 16:13:08 -05004117
4118int extent_writepages(struct extent_io_tree *tree,
4119 struct address_space *mapping,
4120 get_extent_t *get_extent,
4121 struct writeback_control *wbc)
4122{
4123 int ret = 0;
4124 struct extent_page_data epd = {
4125 .bio = NULL,
4126 .tree = tree,
4127 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004128 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004129 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004130 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004131 };
4132
Chris Mason4bef0842008-09-08 11:18:08 -04004133 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004134 __extent_writepage, &epd,
4135 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004136 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004137 return ret;
4138}
Chris Masond1310b22008-01-24 16:13:08 -05004139
4140int extent_readpages(struct extent_io_tree *tree,
4141 struct address_space *mapping,
4142 struct list_head *pages, unsigned nr_pages,
4143 get_extent_t get_extent)
4144{
4145 struct bio *bio = NULL;
4146 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004147 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004148 struct page *pagepool[16];
4149 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004150 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004151 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004152 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004153
Chris Masond1310b22008-01-24 16:13:08 -05004154 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004155 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004156
4157 prefetchw(&page->flags);
4158 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004159 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04004160 page->index, GFP_NOFS)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004161 put_page(page);
Liu Bo67c96842012-07-20 21:43:09 -06004162 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004163 }
Liu Bo67c96842012-07-20 21:43:09 -06004164
4165 pagepool[nr++] = page;
4166 if (nr < ARRAY_SIZE(pagepool))
4167 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004168 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Filipe Manana808f80b2015-09-28 09:56:26 +01004169 &bio, 0, &bio_flags, READ, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004170 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004171 }
Miao Xie99740902013-07-25 19:22:36 +08004172 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004173 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Filipe Manana808f80b2015-09-28 09:56:26 +01004174 &bio, 0, &bio_flags, READ, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004175
Miao Xie125bac012013-07-25 19:22:37 +08004176 if (em_cached)
4177 free_extent_map(em_cached);
4178
Chris Masond1310b22008-01-24 16:13:08 -05004179 BUG_ON(!list_empty(pages));
4180 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004181 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004182 return 0;
4183}
Chris Masond1310b22008-01-24 16:13:08 -05004184
4185/*
4186 * basic invalidatepage code, this waits on any locked or writeback
4187 * ranges corresponding to the page, and then deletes any extent state
4188 * records from the tree
4189 */
4190int extent_invalidatepage(struct extent_io_tree *tree,
4191 struct page *page, unsigned long offset)
4192{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004193 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004194 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004195 u64 end = start + PAGE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004196 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4197
Qu Wenruofda28322013-02-26 08:10:22 +00004198 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004199 if (start > end)
4200 return 0;
4201
David Sterbaff13db42015-12-03 14:30:40 +01004202 lock_extent_bits(tree, start, end, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004203 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004204 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004205 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4206 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004207 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004208 return 0;
4209}
Chris Masond1310b22008-01-24 16:13:08 -05004210
4211/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004212 * a helper for releasepage, this tests for areas of the page that
4213 * are locked or under IO and drops the related state bits if it is safe
4214 * to drop the page.
4215 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004216static int try_release_extent_state(struct extent_map_tree *map,
4217 struct extent_io_tree *tree,
4218 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004219{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004220 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004221 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004222 int ret = 1;
4223
Chris Mason211f90e2008-07-18 11:56:15 -04004224 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004225 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004226 ret = 0;
4227 else {
4228 if ((mask & GFP_NOFS) == GFP_NOFS)
4229 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04004230 /*
4231 * at this point we can safely clear everything except the
4232 * locked bit and the nodatasum bit
4233 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004234 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004235 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4236 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004237
4238 /* if clear_extent_bit failed for enomem reasons,
4239 * we can't allow the release to continue.
4240 */
4241 if (ret < 0)
4242 ret = 0;
4243 else
4244 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004245 }
4246 return ret;
4247}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004248
4249/*
Chris Masond1310b22008-01-24 16:13:08 -05004250 * a helper for releasepage. As long as there are no locked extents
4251 * in the range corresponding to the page, both state records and extent
4252 * map records are removed
4253 */
4254int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004255 struct extent_io_tree *tree, struct page *page,
4256 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004257{
4258 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004259 u64 start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004260 u64 end = start + PAGE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004261
Mel Gormand0164ad2015-11-06 16:28:21 -08004262 if (gfpflags_allow_blocking(mask) &&
Byongho Leeee221842015-12-15 01:42:10 +09004263 page->mapping->host->i_size > SZ_16M) {
Yan39b56372008-02-15 10:40:50 -05004264 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004265 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004266 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004267 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004268 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004269 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004270 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004271 break;
4272 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004273 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4274 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004275 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004276 free_extent_map(em);
4277 break;
4278 }
4279 if (!test_range_bit(tree, em->start,
4280 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004281 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004282 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004283 remove_extent_mapping(map, em);
4284 /* once for the rb tree */
4285 free_extent_map(em);
4286 }
4287 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004288 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004289
4290 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004291 free_extent_map(em);
4292 }
Chris Masond1310b22008-01-24 16:13:08 -05004293 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004294 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004295}
Chris Masond1310b22008-01-24 16:13:08 -05004296
Chris Masonec29ed52011-02-23 16:23:20 -05004297/*
4298 * helper function for fiemap, which doesn't want to see any holes.
4299 * This maps until we find something past 'last'
4300 */
4301static struct extent_map *get_extent_skip_holes(struct inode *inode,
4302 u64 offset,
4303 u64 last,
4304 get_extent_t *get_extent)
4305{
4306 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
4307 struct extent_map *em;
4308 u64 len;
4309
4310 if (offset >= last)
4311 return NULL;
4312
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304313 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004314 len = last - offset;
4315 if (len == 0)
4316 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004317 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05004318 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004319 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004320 return em;
4321
4322 /* if this isn't a hole return it */
4323 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4324 em->block_start != EXTENT_MAP_HOLE) {
4325 return em;
4326 }
4327
4328 /* this is a hole, advance to the next extent */
4329 offset = extent_map_end(em);
4330 free_extent_map(em);
4331 if (offset >= last)
4332 break;
4333 }
4334 return NULL;
4335}
4336
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004337int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4338 __u64 start, __u64 len, get_extent_t *get_extent)
4339{
Josef Bacik975f84f2010-11-23 19:36:57 +00004340 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004341 u64 off = start;
4342 u64 max = start + len;
4343 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004344 u32 found_type;
4345 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004346 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004347 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004348 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004349 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004350 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004351 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004352 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004353 struct btrfs_root *root = BTRFS_I(inode)->root;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004354 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004355 u64 em_start = 0;
4356 u64 em_len = 0;
4357 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004358
4359 if (len == 0)
4360 return -EINVAL;
4361
Josef Bacik975f84f2010-11-23 19:36:57 +00004362 path = btrfs_alloc_path();
4363 if (!path)
4364 return -ENOMEM;
4365 path->leave_spinning = 1;
4366
Qu Wenruo2c919432014-07-18 09:55:43 +08004367 start = round_down(start, BTRFS_I(inode)->root->sectorsize);
4368 len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004369
Chris Masonec29ed52011-02-23 16:23:20 -05004370 /*
4371 * lookup the last file extent. We're not using i_size here
4372 * because there might be preallocation past i_size
4373 */
Josef Bacikdc046b12014-09-10 16:20:45 -04004374 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
4375 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004376 if (ret < 0) {
4377 btrfs_free_path(path);
4378 return ret;
4379 }
4380 WARN_ON(!ret);
4381 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004382 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004383 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004384
Chris Masonec29ed52011-02-23 16:23:20 -05004385 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08004386 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004387 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004388 /* have to trust i_size as the end */
4389 last = (u64)-1;
4390 last_for_get_extent = isize;
4391 } else {
4392 /*
4393 * remember the start of the last extent. There are a
4394 * bunch of different factors that go into the length of the
4395 * extent, so its much less complex to remember where it started
4396 */
4397 last = found_key.offset;
4398 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004399 }
Liu Bofe09e162013-09-22 12:54:23 +08004400 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004401
Chris Masonec29ed52011-02-23 16:23:20 -05004402 /*
4403 * we might have some extents allocated but more delalloc past those
4404 * extents. so, we trust isize unless the start of the last extent is
4405 * beyond isize
4406 */
4407 if (last < isize) {
4408 last = (u64)-1;
4409 last_for_get_extent = isize;
4410 }
4411
David Sterbaff13db42015-12-03 14:30:40 +01004412 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004413 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004414
Josef Bacik4d479cf2011-11-17 11:34:31 -05004415 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004416 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004417 if (!em)
4418 goto out;
4419 if (IS_ERR(em)) {
4420 ret = PTR_ERR(em);
4421 goto out;
4422 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004423
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004424 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004425 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004426
Chris Masonea8efc72011-03-08 11:54:40 -05004427 /* break if the extent we found is outside the range */
4428 if (em->start >= max || extent_map_end(em) < off)
4429 break;
4430
4431 /*
4432 * get_extent may return an extent that starts before our
4433 * requested range. We have to make sure the ranges
4434 * we return to fiemap always move forward and don't
4435 * overlap, so adjust the offsets here
4436 */
4437 em_start = max(em->start, off);
4438
4439 /*
4440 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004441 * for adjusting the disk offset below. Only do this if the
4442 * extent isn't compressed since our in ram offset may be past
4443 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004444 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004445 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4446 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004447 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004448 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004449 disko = 0;
4450 flags = 0;
4451
Chris Masonea8efc72011-03-08 11:54:40 -05004452 /*
4453 * bump off for our next call to get_extent
4454 */
4455 off = extent_map_end(em);
4456 if (off >= max)
4457 end = 1;
4458
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004459 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004460 end = 1;
4461 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004462 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004463 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4464 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004465 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004466 flags |= (FIEMAP_EXTENT_DELALLOC |
4467 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004468 } else if (fieinfo->fi_extents_max) {
4469 u64 bytenr = em->block_start -
4470 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004471
Chris Masonea8efc72011-03-08 11:54:40 -05004472 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004473
4474 /*
4475 * As btrfs supports shared space, this information
4476 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004477 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4478 * then we're just getting a count and we can skip the
4479 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004480 */
Josef Bacikdc046b12014-09-10 16:20:45 -04004481 ret = btrfs_check_shared(NULL, root->fs_info,
4482 root->objectid,
4483 btrfs_ino(inode), bytenr);
4484 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004485 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004486 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004487 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004488 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004489 }
4490 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4491 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004492 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4493 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004494
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004495 free_extent_map(em);
4496 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004497 if ((em_start >= last) || em_len == (u64)-1 ||
4498 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004499 flags |= FIEMAP_EXTENT_LAST;
4500 end = 1;
4501 }
4502
Chris Masonec29ed52011-02-23 16:23:20 -05004503 /* now scan forward to see if this is really the last extent. */
4504 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4505 get_extent);
4506 if (IS_ERR(em)) {
4507 ret = PTR_ERR(em);
4508 goto out;
4509 }
4510 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004511 flags |= FIEMAP_EXTENT_LAST;
4512 end = 1;
4513 }
Chris Masonec29ed52011-02-23 16:23:20 -05004514 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4515 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004516 if (ret) {
4517 if (ret == 1)
4518 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004519 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004520 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004521 }
4522out_free:
4523 free_extent_map(em);
4524out:
Liu Bofe09e162013-09-22 12:54:23 +08004525 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004526 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004527 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004528 return ret;
4529}
4530
Chris Mason727011e2010-08-06 13:21:20 -04004531static void __free_extent_buffer(struct extent_buffer *eb)
4532{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004533 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004534 kmem_cache_free(extent_buffer_cache, eb);
4535}
4536
Josef Bacika26e8c92014-03-28 17:07:27 -04004537int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004538{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004539 return (atomic_read(&eb->io_pages) ||
4540 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4541 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004542}
4543
Miao Xie897ca6e2010-10-26 20:57:29 -04004544/*
4545 * Helper for releasing extent buffer page.
4546 */
David Sterbaa50924e2014-07-31 00:51:36 +02004547static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e2010-10-26 20:57:29 -04004548{
4549 unsigned long index;
4550 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004551 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004552
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004553 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004554
David Sterbaa50924e2014-07-31 00:51:36 +02004555 index = num_extent_pages(eb->start, eb->len);
4556 if (index == 0)
Miao Xie897ca6e2010-10-26 20:57:29 -04004557 return;
4558
4559 do {
4560 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004561 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004562 if (!page)
4563 continue;
4564 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004565 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004566 /*
4567 * We do this since we'll remove the pages after we've
4568 * removed the eb from the radix tree, so we could race
4569 * and have this page now attached to the new eb. So
4570 * only clear page_private if it's still connected to
4571 * this eb.
4572 */
4573 if (PagePrivate(page) &&
4574 page->private == (unsigned long)eb) {
4575 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4576 BUG_ON(PageDirty(page));
4577 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004578 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004579 * We need to make sure we haven't be attached
4580 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004581 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004582 ClearPagePrivate(page);
4583 set_page_private(page, 0);
4584 /* One for the page private */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004585 put_page(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004586 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004587
4588 if (mapped)
4589 spin_unlock(&page->mapping->private_lock);
4590
4591 /* One for when we alloced the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004592 put_page(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004593 } while (index != 0);
Miao Xie897ca6e2010-10-26 20:57:29 -04004594}
4595
4596/*
4597 * Helper for releasing the extent buffer.
4598 */
4599static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4600{
David Sterbaa50924e2014-07-31 00:51:36 +02004601 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e2010-10-26 20:57:29 -04004602 __free_extent_buffer(eb);
4603}
4604
Josef Bacikf28491e2013-12-16 13:24:27 -05004605static struct extent_buffer *
4606__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004607 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004608{
4609 struct extent_buffer *eb = NULL;
4610
Michal Hockod1b5c562015-08-19 14:17:40 +02004611 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004612 eb->start = start;
4613 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004614 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004615 eb->bflags = 0;
4616 rwlock_init(&eb->lock);
4617 atomic_set(&eb->write_locks, 0);
4618 atomic_set(&eb->read_locks, 0);
4619 atomic_set(&eb->blocking_readers, 0);
4620 atomic_set(&eb->blocking_writers, 0);
4621 atomic_set(&eb->spinning_readers, 0);
4622 atomic_set(&eb->spinning_writers, 0);
4623 eb->lock_nested = 0;
4624 init_waitqueue_head(&eb->write_lock_wq);
4625 init_waitqueue_head(&eb->read_lock_wq);
4626
4627 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4628
4629 spin_lock_init(&eb->refs_lock);
4630 atomic_set(&eb->refs, 1);
4631 atomic_set(&eb->io_pages, 0);
4632
4633 /*
4634 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4635 */
4636 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4637 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4638 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4639
4640 return eb;
4641}
4642
4643struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4644{
4645 unsigned long i;
4646 struct page *p;
4647 struct extent_buffer *new;
4648 unsigned long num_pages = num_extent_pages(src->start, src->len);
4649
David Sterba3f556f72014-06-15 03:20:26 +02004650 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004651 if (new == NULL)
4652 return NULL;
4653
4654 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004655 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004656 if (!p) {
4657 btrfs_release_extent_buffer(new);
4658 return NULL;
4659 }
4660 attach_extent_buffer_page(new, p);
4661 WARN_ON(PageDirty(p));
4662 SetPageUptodate(p);
4663 new->pages[i] = p;
4664 }
4665
4666 copy_extent_buffer(new, src, 0, 0, src->len);
4667 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4668 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4669
4670 return new;
4671}
4672
Omar Sandoval0f331222015-09-29 20:50:31 -07004673struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4674 u64 start, unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004675{
4676 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004677 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004678 unsigned long i;
4679
Omar Sandoval0f331222015-09-29 20:50:31 -07004680 num_pages = num_extent_pages(start, len);
David Sterba3f556f72014-06-15 03:20:26 +02004681
4682 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004683 if (!eb)
4684 return NULL;
4685
4686 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004687 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004688 if (!eb->pages[i])
4689 goto err;
4690 }
4691 set_extent_buffer_uptodate(eb);
4692 btrfs_set_header_nritems(eb, 0);
4693 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4694
4695 return eb;
4696err:
4697 for (; i > 0; i--)
4698 __free_page(eb->pages[i - 1]);
4699 __free_extent_buffer(eb);
4700 return NULL;
4701}
4702
Omar Sandoval0f331222015-09-29 20:50:31 -07004703struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4704 u64 start)
4705{
4706 unsigned long len;
4707
4708 if (!fs_info) {
4709 /*
4710 * Called only from tests that don't always have a fs_info
4711 * available, but we know that nodesize is 4096
4712 */
4713 len = 4096;
4714 } else {
4715 len = fs_info->tree_root->nodesize;
4716 }
4717
4718 return __alloc_dummy_extent_buffer(fs_info, start, len);
4719}
4720
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004721static void check_buffer_tree_ref(struct extent_buffer *eb)
4722{
Chris Mason242e18c2013-01-29 17:49:37 -05004723 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004724 /* the ref bit is tricky. We have to make sure it is set
4725 * if we have the buffer dirty. Otherwise the
4726 * code to free a buffer can end up dropping a dirty
4727 * page
4728 *
4729 * Once the ref bit is set, it won't go away while the
4730 * buffer is dirty or in writeback, and it also won't
4731 * go away while we have the reference count on the
4732 * eb bumped.
4733 *
4734 * We can't just set the ref bit without bumping the
4735 * ref on the eb because free_extent_buffer might
4736 * see the ref bit and try to clear it. If this happens
4737 * free_extent_buffer might end up dropping our original
4738 * ref by mistake and freeing the page before we are able
4739 * to add one more ref.
4740 *
4741 * So bump the ref count first, then set the bit. If someone
4742 * beat us to it, drop the ref we added.
4743 */
Chris Mason242e18c2013-01-29 17:49:37 -05004744 refs = atomic_read(&eb->refs);
4745 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4746 return;
4747
Josef Bacik594831c2012-07-20 16:11:08 -04004748 spin_lock(&eb->refs_lock);
4749 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004750 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004751 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004752}
4753
Mel Gorman2457aec2014-06-04 16:10:31 -07004754static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4755 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004756{
4757 unsigned long num_pages, i;
4758
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004759 check_buffer_tree_ref(eb);
4760
Josef Bacik5df42352012-03-15 18:24:42 -04004761 num_pages = num_extent_pages(eb->start, eb->len);
4762 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004763 struct page *p = eb->pages[i];
4764
Mel Gorman2457aec2014-06-04 16:10:31 -07004765 if (p != accessed)
4766 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004767 }
4768}
4769
Josef Bacikf28491e2013-12-16 13:24:27 -05004770struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4771 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004772{
4773 struct extent_buffer *eb;
4774
4775 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004776 eb = radix_tree_lookup(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004777 start >> PAGE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004778 if (eb && atomic_inc_not_zero(&eb->refs)) {
4779 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004780 /*
4781 * Lock our eb's refs_lock to avoid races with
4782 * free_extent_buffer. When we get our eb it might be flagged
4783 * with EXTENT_BUFFER_STALE and another task running
4784 * free_extent_buffer might have seen that flag set,
4785 * eb->refs == 2, that the buffer isn't under IO (dirty and
4786 * writeback flags not set) and it's still in the tree (flag
4787 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4788 * of decrementing the extent buffer's reference count twice.
4789 * So here we could race and increment the eb's reference count,
4790 * clear its stale flag, mark it as dirty and drop our reference
4791 * before the other task finishes executing free_extent_buffer,
4792 * which would later result in an attempt to free an extent
4793 * buffer that is dirty.
4794 */
4795 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4796 spin_lock(&eb->refs_lock);
4797 spin_unlock(&eb->refs_lock);
4798 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004799 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004800 return eb;
4801 }
4802 rcu_read_unlock();
4803
4804 return NULL;
4805}
4806
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004807#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4808struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004809 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004810{
4811 struct extent_buffer *eb, *exists = NULL;
4812 int ret;
4813
4814 eb = find_extent_buffer(fs_info, start);
4815 if (eb)
4816 return eb;
David Sterba3f556f72014-06-15 03:20:26 +02004817 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004818 if (!eb)
4819 return NULL;
4820 eb->fs_info = fs_info;
4821again:
4822 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4823 if (ret)
4824 goto free_eb;
4825 spin_lock(&fs_info->buffer_lock);
4826 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004827 start >> PAGE_SHIFT, eb);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004828 spin_unlock(&fs_info->buffer_lock);
4829 radix_tree_preload_end();
4830 if (ret == -EEXIST) {
4831 exists = find_extent_buffer(fs_info, start);
4832 if (exists)
4833 goto free_eb;
4834 else
4835 goto again;
4836 }
4837 check_buffer_tree_ref(eb);
4838 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4839
4840 /*
4841 * We will free dummy extent buffer's if they come into
4842 * free_extent_buffer with a ref count of 2, but if we are using this we
4843 * want the buffers to stay in memory until we're done with them, so
4844 * bump the ref count again.
4845 */
4846 atomic_inc(&eb->refs);
4847 return eb;
4848free_eb:
4849 btrfs_release_extent_buffer(eb);
4850 return exists;
4851}
4852#endif
4853
Josef Bacikf28491e2013-12-16 13:24:27 -05004854struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004855 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004856{
David Sterbace3e6982014-06-15 03:00:04 +02004857 unsigned long len = fs_info->tree_root->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004858 unsigned long num_pages = num_extent_pages(start, len);
4859 unsigned long i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004860 unsigned long index = start >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004861 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004862 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004863 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004864 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004865 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004866 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004867
Josef Bacikf28491e2013-12-16 13:24:27 -05004868 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004869 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004870 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004871
David Sterba23d79d82014-06-15 02:55:29 +02004872 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004873 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004874 return NULL;
4875
Chris Mason727011e2010-08-06 13:21:20 -04004876 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004877 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Josef Bacik4804b382012-10-05 16:43:45 -04004878 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004879 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004880
4881 spin_lock(&mapping->private_lock);
4882 if (PagePrivate(p)) {
4883 /*
4884 * We could have already allocated an eb for this page
4885 * and attached one so lets see if we can get a ref on
4886 * the existing eb, and if we can we know it's good and
4887 * we can just return that one, else we know we can just
4888 * overwrite page->private.
4889 */
4890 exists = (struct extent_buffer *)p->private;
4891 if (atomic_inc_not_zero(&exists->refs)) {
4892 spin_unlock(&mapping->private_lock);
4893 unlock_page(p);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004894 put_page(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004895 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004896 goto free_eb;
4897 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004898 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004899
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004900 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004901 * Do this so attach doesn't complain and we need to
4902 * drop the ref the old guy had.
4903 */
4904 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004905 WARN_ON(PageDirty(p));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004906 put_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05004907 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004908 attach_extent_buffer_page(eb, p);
4909 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004910 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004911 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004912 if (!PageUptodate(p))
4913 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004914
4915 /*
4916 * see below about how we avoid a nasty race with release page
4917 * and why we unlock later
4918 */
Chris Masond1310b22008-01-24 16:13:08 -05004919 }
4920 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004921 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004922again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004923 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4924 if (ret)
4925 goto free_eb;
4926
Josef Bacikf28491e2013-12-16 13:24:27 -05004927 spin_lock(&fs_info->buffer_lock);
4928 ret = radix_tree_insert(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004929 start >> PAGE_SHIFT, eb);
Josef Bacikf28491e2013-12-16 13:24:27 -05004930 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004931 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004932 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004933 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004934 if (exists)
4935 goto free_eb;
4936 else
Josef Bacik115391d2012-03-09 09:51:43 -05004937 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004938 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004939 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004940 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004941 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05004942
4943 /*
4944 * there is a race where release page may have
4945 * tried to find this extent buffer in the radix
4946 * but failed. It will tell the VM it is safe to
4947 * reclaim the, and it will clear the page private bit.
4948 * We must make sure to set the page private bit properly
4949 * after the extent buffer is in the radix tree so
4950 * it doesn't get lost
4951 */
Chris Mason727011e2010-08-06 13:21:20 -04004952 SetPageChecked(eb->pages[0]);
4953 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004954 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04004955 ClearPageChecked(p);
4956 unlock_page(p);
4957 }
4958 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004959 return eb;
4960
Chris Mason6af118ce2008-07-22 11:18:07 -04004961free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004962 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04004963 for (i = 0; i < num_pages; i++) {
4964 if (eb->pages[i])
4965 unlock_page(eb->pages[i]);
4966 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004967
Miao Xie897ca6e2010-10-26 20:57:29 -04004968 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004969 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004970}
Chris Masond1310b22008-01-24 16:13:08 -05004971
Josef Bacik3083ee22012-03-09 16:01:49 -05004972static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4973{
4974 struct extent_buffer *eb =
4975 container_of(head, struct extent_buffer, rcu_head);
4976
4977 __free_extent_buffer(eb);
4978}
4979
Josef Bacik3083ee22012-03-09 16:01:49 -05004980/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004981static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004982{
4983 WARN_ON(atomic_read(&eb->refs) == 0);
4984 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05004985 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004986 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05004987
Jan Schmidt815a51c2012-05-16 17:00:02 +02004988 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004989
Josef Bacikf28491e2013-12-16 13:24:27 -05004990 spin_lock(&fs_info->buffer_lock);
4991 radix_tree_delete(&fs_info->buffer_radix,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004992 eb->start >> PAGE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05004993 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004994 } else {
4995 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004996 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004997
4998 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02004999 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005000#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5001 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5002 __free_extent_buffer(eb);
5003 return 1;
5004 }
5005#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005006 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005007 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005008 }
5009 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005010
5011 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005012}
5013
Chris Masond1310b22008-01-24 16:13:08 -05005014void free_extent_buffer(struct extent_buffer *eb)
5015{
Chris Mason242e18c2013-01-29 17:49:37 -05005016 int refs;
5017 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005018 if (!eb)
5019 return;
5020
Chris Mason242e18c2013-01-29 17:49:37 -05005021 while (1) {
5022 refs = atomic_read(&eb->refs);
5023 if (refs <= 3)
5024 break;
5025 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5026 if (old == refs)
5027 return;
5028 }
5029
Josef Bacik3083ee22012-03-09 16:01:49 -05005030 spin_lock(&eb->refs_lock);
5031 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005032 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5033 atomic_dec(&eb->refs);
5034
5035 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005036 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005037 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005038 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5039 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005040
Josef Bacik3083ee22012-03-09 16:01:49 -05005041 /*
5042 * I know this is terrible, but it's temporary until we stop tracking
5043 * the uptodate bits and such for the extent buffers.
5044 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005045 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005046}
Chris Masond1310b22008-01-24 16:13:08 -05005047
Josef Bacik3083ee22012-03-09 16:01:49 -05005048void free_extent_buffer_stale(struct extent_buffer *eb)
5049{
5050 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005051 return;
5052
Josef Bacik3083ee22012-03-09 16:01:49 -05005053 spin_lock(&eb->refs_lock);
5054 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5055
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005056 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005057 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5058 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005059 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005060}
5061
Chris Mason1d4284b2012-03-28 20:31:37 -04005062void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005063{
Chris Masond1310b22008-01-24 16:13:08 -05005064 unsigned long i;
5065 unsigned long num_pages;
5066 struct page *page;
5067
Chris Masond1310b22008-01-24 16:13:08 -05005068 num_pages = num_extent_pages(eb->start, eb->len);
5069
5070 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005071 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005072 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005073 continue;
5074
Chris Masona61e6f22008-07-22 11:18:08 -04005075 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005076 WARN_ON(!PagePrivate(page));
5077
Chris Masond1310b22008-01-24 16:13:08 -05005078 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005079 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005080 if (!PageDirty(page)) {
5081 radix_tree_tag_clear(&page->mapping->page_tree,
5082 page_index(page),
5083 PAGECACHE_TAG_DIRTY);
5084 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005085 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005086 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005087 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005088 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005089 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005090}
Chris Masond1310b22008-01-24 16:13:08 -05005091
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005092int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005093{
5094 unsigned long i;
5095 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005096 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005097
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005098 check_buffer_tree_ref(eb);
5099
Chris Masonb9473432009-03-13 11:00:37 -04005100 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005101
Chris Masond1310b22008-01-24 16:13:08 -05005102 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005103 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005104 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5105
Chris Masonb9473432009-03-13 11:00:37 -04005106 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005107 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005108 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005109}
Chris Masond1310b22008-01-24 16:13:08 -05005110
David Sterba69ba3922015-12-03 13:08:59 +01005111void clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005112{
5113 unsigned long i;
5114 struct page *page;
5115 unsigned long num_pages;
5116
Chris Masonb4ce94d2009-02-04 09:25:08 -05005117 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005118 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005119 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005120 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005121 if (page)
5122 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005123 }
Chris Mason1259ab72008-05-12 13:39:03 -04005124}
5125
David Sterba09c25a82015-12-03 13:08:59 +01005126void set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005127{
5128 unsigned long i;
5129 struct page *page;
5130 unsigned long num_pages;
5131
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005132 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005133 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005134 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005135 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005136 SetPageUptodate(page);
5137 }
Chris Masond1310b22008-01-24 16:13:08 -05005138}
Chris Masond1310b22008-01-24 16:13:08 -05005139
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005140int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005141{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005142 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005143}
Chris Masond1310b22008-01-24 16:13:08 -05005144
5145int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02005146 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005147 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005148{
5149 unsigned long i;
5150 unsigned long start_i;
5151 struct page *page;
5152 int err;
5153 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005154 int locked_pages = 0;
5155 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005156 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005157 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005158 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005159 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005160
Chris Masonb4ce94d2009-02-04 09:25:08 -05005161 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005162 return 0;
5163
Chris Masond1310b22008-01-24 16:13:08 -05005164 if (start) {
5165 WARN_ON(start < eb->start);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005166 start_i = (start >> PAGE_SHIFT) -
5167 (eb->start >> PAGE_SHIFT);
Chris Masond1310b22008-01-24 16:13:08 -05005168 } else {
5169 start_i = 0;
5170 }
5171
5172 num_pages = num_extent_pages(eb->start, eb->len);
5173 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005174 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005175 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005176 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005177 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005178 } else {
5179 lock_page(page);
5180 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005181 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04005182 if (!PageUptodate(page)) {
5183 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005184 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005185 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005186 }
5187 if (all_uptodate) {
5188 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005189 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005190 goto unlock_exit;
5191 }
5192
Filipe Manana656f30d2014-09-26 12:25:56 +01005193 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005194 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005195 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04005196 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005197 page = eb->pages[i];
Chris Masonce9adaa2008-04-09 16:28:12 -04005198 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04005199 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005200 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005201 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005202 mirror_num, &bio_flags,
5203 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05005204 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05005205 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05005206 } else {
5207 unlock_page(page);
5208 }
5209 }
5210
Jeff Mahoney355808c2011-10-03 23:23:14 -04005211 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005212 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
5213 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005214 if (err)
5215 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005216 }
Chris Masona86c12c2008-02-07 10:50:54 -05005217
Arne Jansenbb82ab82011-06-10 14:06:53 +02005218 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005219 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005220
Chris Masond1310b22008-01-24 16:13:08 -05005221 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005222 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005223 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005224 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005225 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005226 }
Chris Masond3977122009-01-05 21:25:51 -05005227
Chris Masond1310b22008-01-24 16:13:08 -05005228 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005229
5230unlock_exit:
5231 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05005232 while (locked_pages > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005233 page = eb->pages[i];
Chris Masonce9adaa2008-04-09 16:28:12 -04005234 i++;
5235 unlock_page(page);
5236 locked_pages--;
5237 }
5238 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005239}
Chris Masond1310b22008-01-24 16:13:08 -05005240
5241void read_extent_buffer(struct extent_buffer *eb, void *dstv,
5242 unsigned long start,
5243 unsigned long len)
5244{
5245 size_t cur;
5246 size_t offset;
5247 struct page *page;
5248 char *kaddr;
5249 char *dst = (char *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005250 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5251 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005252
5253 WARN_ON(start > eb->len);
5254 WARN_ON(start + len > eb->start + eb->len);
5255
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005256 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005257
Chris Masond3977122009-01-05 21:25:51 -05005258 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005259 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005260
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005261 cur = min(len, (PAGE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005262 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005263 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005264
5265 dst += cur;
5266 len -= cur;
5267 offset = 0;
5268 i++;
5269 }
5270}
Chris Masond1310b22008-01-24 16:13:08 -05005271
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005272int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
5273 unsigned long start,
5274 unsigned long len)
5275{
5276 size_t cur;
5277 size_t offset;
5278 struct page *page;
5279 char *kaddr;
5280 char __user *dst = (char __user *)dstv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005281 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5282 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005283 int ret = 0;
5284
5285 WARN_ON(start > eb->len);
5286 WARN_ON(start + len > eb->start + eb->len);
5287
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005288 offset = (start_offset + start) & (PAGE_SIZE - 1);
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005289
5290 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005291 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005292
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005293 cur = min(len, (PAGE_SIZE - offset));
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005294 kaddr = page_address(page);
5295 if (copy_to_user(dst, kaddr + offset, cur)) {
5296 ret = -EFAULT;
5297 break;
5298 }
5299
5300 dst += cur;
5301 len -= cur;
5302 offset = 0;
5303 i++;
5304 }
5305
5306 return ret;
5307}
5308
Chris Masond1310b22008-01-24 16:13:08 -05005309int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04005310 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05005311 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04005312 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005313{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005314 size_t offset = start & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005315 char *kaddr;
5316 struct page *p;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005317 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5318 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005319 unsigned long end_i = (start_offset + start + min_len - 1) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005320 PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005321
5322 if (i != end_i)
5323 return -EINVAL;
5324
5325 if (i == 0) {
5326 offset = start_offset;
5327 *map_start = 0;
5328 } else {
5329 offset = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005330 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
Chris Masond1310b22008-01-24 16:13:08 -05005331 }
Chris Masond3977122009-01-05 21:25:51 -05005332
Chris Masond1310b22008-01-24 16:13:08 -05005333 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005334 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005335 "wanted %lu %lu\n",
5336 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04005337 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005338 }
5339
David Sterbafb85fc92014-07-31 01:03:53 +02005340 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005341 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005342 *map = kaddr + offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005343 *map_len = PAGE_SIZE - offset;
Chris Masond1310b22008-01-24 16:13:08 -05005344 return 0;
5345}
Chris Masond1310b22008-01-24 16:13:08 -05005346
Chris Masond1310b22008-01-24 16:13:08 -05005347int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
5348 unsigned long start,
5349 unsigned long len)
5350{
5351 size_t cur;
5352 size_t offset;
5353 struct page *page;
5354 char *kaddr;
5355 char *ptr = (char *)ptrv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005356 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5357 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005358 int ret = 0;
5359
5360 WARN_ON(start > eb->len);
5361 WARN_ON(start + len > eb->start + eb->len);
5362
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005363 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005364
Chris Masond3977122009-01-05 21:25:51 -05005365 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005366 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005367
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005368 cur = min(len, (PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005369
Chris Masona6591712011-07-19 12:04:14 -04005370 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005371 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005372 if (ret)
5373 break;
5374
5375 ptr += cur;
5376 len -= cur;
5377 offset = 0;
5378 i++;
5379 }
5380 return ret;
5381}
Chris Masond1310b22008-01-24 16:13:08 -05005382
5383void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5384 unsigned long start, unsigned long len)
5385{
5386 size_t cur;
5387 size_t offset;
5388 struct page *page;
5389 char *kaddr;
5390 char *src = (char *)srcv;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005391 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5392 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005393
5394 WARN_ON(start > eb->len);
5395 WARN_ON(start + len > eb->start + eb->len);
5396
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005397 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005398
Chris Masond3977122009-01-05 21:25:51 -05005399 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005400 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005401 WARN_ON(!PageUptodate(page));
5402
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005403 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005404 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005405 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005406
5407 src += cur;
5408 len -= cur;
5409 offset = 0;
5410 i++;
5411 }
5412}
Chris Masond1310b22008-01-24 16:13:08 -05005413
5414void memset_extent_buffer(struct extent_buffer *eb, char c,
5415 unsigned long start, unsigned long len)
5416{
5417 size_t cur;
5418 size_t offset;
5419 struct page *page;
5420 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005421 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5422 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005423
5424 WARN_ON(start > eb->len);
5425 WARN_ON(start + len > eb->start + eb->len);
5426
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005427 offset = (start_offset + start) & (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005428
Chris Masond3977122009-01-05 21:25:51 -05005429 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005430 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005431 WARN_ON(!PageUptodate(page));
5432
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005433 cur = min(len, PAGE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005434 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005435 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005436
5437 len -= cur;
5438 offset = 0;
5439 i++;
5440 }
5441}
Chris Masond1310b22008-01-24 16:13:08 -05005442
5443void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5444 unsigned long dst_offset, unsigned long src_offset,
5445 unsigned long len)
5446{
5447 u64 dst_len = dst->len;
5448 size_t cur;
5449 size_t offset;
5450 struct page *page;
5451 char *kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005452 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
5453 unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005454
5455 WARN_ON(src->len != dst_len);
5456
5457 offset = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005458 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005459
Chris Masond3977122009-01-05 21:25:51 -05005460 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005461 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005462 WARN_ON(!PageUptodate(page));
5463
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005464 cur = min(len, (unsigned long)(PAGE_SIZE - offset));
Chris Masond1310b22008-01-24 16:13:08 -05005465
Chris Masona6591712011-07-19 12:04:14 -04005466 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005467 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005468
5469 src_offset += cur;
5470 len -= cur;
5471 offset = 0;
5472 i++;
5473 }
5474}
Chris Masond1310b22008-01-24 16:13:08 -05005475
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005476/*
5477 * The extent buffer bitmap operations are done with byte granularity because
5478 * bitmap items are not guaranteed to be aligned to a word and therefore a
5479 * single word in a bitmap may straddle two pages in the extent buffer.
5480 */
5481#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
5482#define BYTE_MASK ((1 << BITS_PER_BYTE) - 1)
5483#define BITMAP_FIRST_BYTE_MASK(start) \
5484 ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
5485#define BITMAP_LAST_BYTE_MASK(nbits) \
5486 (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
5487
5488/*
5489 * eb_bitmap_offset() - calculate the page and offset of the byte containing the
5490 * given bit number
5491 * @eb: the extent buffer
5492 * @start: offset of the bitmap item in the extent buffer
5493 * @nr: bit number
5494 * @page_index: return index of the page in the extent buffer that contains the
5495 * given bit number
5496 * @page_offset: return offset into the page given by page_index
5497 *
5498 * This helper hides the ugliness of finding the byte in an extent buffer which
5499 * contains a given bit.
5500 */
5501static inline void eb_bitmap_offset(struct extent_buffer *eb,
5502 unsigned long start, unsigned long nr,
5503 unsigned long *page_index,
5504 size_t *page_offset)
5505{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005506 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005507 size_t byte_offset = BIT_BYTE(nr);
5508 size_t offset;
5509
5510 /*
5511 * The byte we want is the offset of the extent buffer + the offset of
5512 * the bitmap item in the extent buffer + the offset of the byte in the
5513 * bitmap item.
5514 */
5515 offset = start_offset + start + byte_offset;
5516
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005517 *page_index = offset >> PAGE_SHIFT;
5518 *page_offset = offset & (PAGE_SIZE - 1);
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005519}
5520
5521/**
5522 * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
5523 * @eb: the extent buffer
5524 * @start: offset of the bitmap item in the extent buffer
5525 * @nr: bit number to test
5526 */
5527int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
5528 unsigned long nr)
5529{
5530 char *kaddr;
5531 struct page *page;
5532 unsigned long i;
5533 size_t offset;
5534
5535 eb_bitmap_offset(eb, start, nr, &i, &offset);
5536 page = eb->pages[i];
5537 WARN_ON(!PageUptodate(page));
5538 kaddr = page_address(page);
5539 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
5540}
5541
5542/**
5543 * extent_buffer_bitmap_set - set an area of a bitmap
5544 * @eb: the extent buffer
5545 * @start: offset of the bitmap item in the extent buffer
5546 * @pos: bit number of the first bit
5547 * @len: number of bits to set
5548 */
5549void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
5550 unsigned long pos, unsigned long len)
5551{
5552 char *kaddr;
5553 struct page *page;
5554 unsigned long i;
5555 size_t offset;
5556 const unsigned int size = pos + len;
5557 int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
5558 unsigned int mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
5559
5560 eb_bitmap_offset(eb, start, pos, &i, &offset);
5561 page = eb->pages[i];
5562 WARN_ON(!PageUptodate(page));
5563 kaddr = page_address(page);
5564
5565 while (len >= bits_to_set) {
5566 kaddr[offset] |= mask_to_set;
5567 len -= bits_to_set;
5568 bits_to_set = BITS_PER_BYTE;
5569 mask_to_set = ~0U;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005570 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005571 offset = 0;
5572 page = eb->pages[++i];
5573 WARN_ON(!PageUptodate(page));
5574 kaddr = page_address(page);
5575 }
5576 }
5577 if (len) {
5578 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
5579 kaddr[offset] |= mask_to_set;
5580 }
5581}
5582
5583
5584/**
5585 * extent_buffer_bitmap_clear - clear an area of a bitmap
5586 * @eb: the extent buffer
5587 * @start: offset of the bitmap item in the extent buffer
5588 * @pos: bit number of the first bit
5589 * @len: number of bits to clear
5590 */
5591void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
5592 unsigned long pos, unsigned long len)
5593{
5594 char *kaddr;
5595 struct page *page;
5596 unsigned long i;
5597 size_t offset;
5598 const unsigned int size = pos + len;
5599 int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
5600 unsigned int mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
5601
5602 eb_bitmap_offset(eb, start, pos, &i, &offset);
5603 page = eb->pages[i];
5604 WARN_ON(!PageUptodate(page));
5605 kaddr = page_address(page);
5606
5607 while (len >= bits_to_clear) {
5608 kaddr[offset] &= ~mask_to_clear;
5609 len -= bits_to_clear;
5610 bits_to_clear = BITS_PER_BYTE;
5611 mask_to_clear = ~0U;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005612 if (++offset >= PAGE_SIZE && len > 0) {
Omar Sandoval3e1e8bb2015-09-29 20:50:30 -07005613 offset = 0;
5614 page = eb->pages[++i];
5615 WARN_ON(!PageUptodate(page));
5616 kaddr = page_address(page);
5617 }
5618 }
5619 if (len) {
5620 mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
5621 kaddr[offset] &= ~mask_to_clear;
5622 }
5623}
5624
Sergei Trofimovich33872062011-04-11 21:52:52 +00005625static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5626{
5627 unsigned long distance = (src > dst) ? src - dst : dst - src;
5628 return distance < len;
5629}
5630
Chris Masond1310b22008-01-24 16:13:08 -05005631static void copy_pages(struct page *dst_page, struct page *src_page,
5632 unsigned long dst_off, unsigned long src_off,
5633 unsigned long len)
5634{
Chris Masona6591712011-07-19 12:04:14 -04005635 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005636 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005637 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005638
Sergei Trofimovich33872062011-04-11 21:52:52 +00005639 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005640 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005641 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005642 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005643 if (areas_overlap(src_off, dst_off, len))
5644 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005645 }
Chris Masond1310b22008-01-24 16:13:08 -05005646
Chris Mason727011e2010-08-06 13:21:20 -04005647 if (must_memmove)
5648 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5649 else
5650 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005651}
5652
5653void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5654 unsigned long src_offset, unsigned long len)
5655{
5656 size_t cur;
5657 size_t dst_off_in_page;
5658 size_t src_off_in_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005659 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005660 unsigned long dst_i;
5661 unsigned long src_i;
5662
5663 if (src_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005664 btrfs_err(dst->fs_info,
5665 "memmove bogus src_offset %lu move "
5666 "len %lu dst len %lu", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005667 BUG_ON(1);
5668 }
5669 if (dst_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005670 btrfs_err(dst->fs_info,
5671 "memmove bogus dst_offset %lu move "
5672 "len %lu dst len %lu", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005673 BUG_ON(1);
5674 }
5675
Chris Masond3977122009-01-05 21:25:51 -05005676 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005677 dst_off_in_page = (start_offset + dst_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005678 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005679 src_off_in_page = (start_offset + src_offset) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005680 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005681
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005682 dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
5683 src_i = (start_offset + src_offset) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005684
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005685 cur = min(len, (unsigned long)(PAGE_SIZE -
Chris Masond1310b22008-01-24 16:13:08 -05005686 src_off_in_page));
5687 cur = min_t(unsigned long, cur,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005688 (unsigned long)(PAGE_SIZE - dst_off_in_page));
Chris Masond1310b22008-01-24 16:13:08 -05005689
David Sterbafb85fc92014-07-31 01:03:53 +02005690 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005691 dst_off_in_page, src_off_in_page, cur);
5692
5693 src_offset += cur;
5694 dst_offset += cur;
5695 len -= cur;
5696 }
5697}
Chris Masond1310b22008-01-24 16:13:08 -05005698
5699void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5700 unsigned long src_offset, unsigned long len)
5701{
5702 size_t cur;
5703 size_t dst_off_in_page;
5704 size_t src_off_in_page;
5705 unsigned long dst_end = dst_offset + len - 1;
5706 unsigned long src_end = src_offset + len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005707 size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005708 unsigned long dst_i;
5709 unsigned long src_i;
5710
5711 if (src_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005712 btrfs_err(dst->fs_info, "memmove bogus src_offset %lu move "
5713 "len %lu len %lu", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005714 BUG_ON(1);
5715 }
5716 if (dst_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005717 btrfs_err(dst->fs_info, "memmove bogus dst_offset %lu move "
5718 "len %lu len %lu", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005719 BUG_ON(1);
5720 }
Chris Mason727011e2010-08-06 13:21:20 -04005721 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005722 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5723 return;
5724 }
Chris Masond3977122009-01-05 21:25:51 -05005725 while (len > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005726 dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
5727 src_i = (start_offset + src_end) >> PAGE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005728
5729 dst_off_in_page = (start_offset + dst_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005730 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005731 src_off_in_page = (start_offset + src_end) &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005732 (PAGE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005733
5734 cur = min_t(unsigned long, len, src_off_in_page + 1);
5735 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005736 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005737 dst_off_in_page - cur + 1,
5738 src_off_in_page - cur + 1, cur);
5739
5740 dst_end -= cur;
5741 src_end -= cur;
5742 len -= cur;
5743 }
5744}
Chris Mason6af118ce2008-07-22 11:18:07 -04005745
David Sterbaf7a52a42013-04-26 14:56:29 +00005746int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005747{
Chris Mason6af118ce2008-07-22 11:18:07 -04005748 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005749
Miao Xie19fe0a82010-10-26 20:57:29 -04005750 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005751 * We need to make sure noboody is attaching this page to an eb right
5752 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005753 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005754 spin_lock(&page->mapping->private_lock);
5755 if (!PagePrivate(page)) {
5756 spin_unlock(&page->mapping->private_lock);
5757 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005758 }
5759
Josef Bacik3083ee22012-03-09 16:01:49 -05005760 eb = (struct extent_buffer *)page->private;
5761 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005762
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005763 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005764 * This is a little awful but should be ok, we need to make sure that
5765 * the eb doesn't disappear out from under us while we're looking at
5766 * this page.
5767 */
5768 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005769 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005770 spin_unlock(&eb->refs_lock);
5771 spin_unlock(&page->mapping->private_lock);
5772 return 0;
5773 }
5774 spin_unlock(&page->mapping->private_lock);
5775
Josef Bacik3083ee22012-03-09 16:01:49 -05005776 /*
5777 * If tree ref isn't set then we know the ref on this eb is a real ref,
5778 * so just return, this page will likely be freed soon anyway.
5779 */
5780 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5781 spin_unlock(&eb->refs_lock);
5782 return 0;
5783 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005784
David Sterbaf7a52a42013-04-26 14:56:29 +00005785 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005786}