blob: 654ed3de005461a5f1829a5d432401a9035bb1fa [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);
Filipe Manana27a35072014-07-06 20:09:59 +010067 pr_err("BTRFS: state leak: start %llu end %llu state %lu in tree %d refs %d\n",
68 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) {
99 printk_ratelimited(KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -0500100 "BTRFS: %s: ino %llu isize %llu odd range [%llu,%llu]\n",
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
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400134static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400135static inline struct btrfs_fs_info *
136tree_fs_info(struct extent_io_tree *tree)
137{
Josef Bacika5dee372013-12-13 10:02:44 -0500138 if (!tree->mapping)
139 return NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400140 return btrfs_sb(tree->mapping->host->i_sb);
141}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400142
Chris Masond1310b22008-01-24 16:13:08 -0500143int __init extent_io_init(void)
144{
David Sterba837e1972012-09-07 03:00:48 -0600145 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200146 sizeof(struct extent_state), 0,
147 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500148 if (!extent_state_cache)
149 return -ENOMEM;
150
David Sterba837e1972012-09-07 03:00:48 -0600151 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200152 sizeof(struct extent_buffer), 0,
153 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500154 if (!extent_buffer_cache)
155 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400156
157 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
158 offsetof(struct btrfs_io_bio, bio));
159 if (!btrfs_bioset)
160 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700161
162 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
163 goto free_bioset;
164
Chris Masond1310b22008-01-24 16:13:08 -0500165 return 0;
166
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700167free_bioset:
168 bioset_free(btrfs_bioset);
169 btrfs_bioset = NULL;
170
Chris Mason9be33952013-05-17 18:30:14 -0400171free_buffer_cache:
172 kmem_cache_destroy(extent_buffer_cache);
173 extent_buffer_cache = NULL;
174
Chris Masond1310b22008-01-24 16:13:08 -0500175free_state_cache:
176 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400177 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500178 return -ENOMEM;
179}
180
181void extent_io_exit(void)
182{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000183 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000184
185 /*
186 * Make sure all delayed rcu free are flushed before we
187 * destroy caches.
188 */
189 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500190 if (extent_state_cache)
191 kmem_cache_destroy(extent_state_cache);
192 if (extent_buffer_cache)
193 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400194 if (btrfs_bioset)
195 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500196}
197
198void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200199 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500200{
Eric Paris6bef4d32010-02-23 19:43:04 +0000201 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500202 tree->ops = NULL;
203 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500204 spin_lock_init(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500205 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500206}
Chris Masond1310b22008-01-24 16:13:08 -0500207
Christoph Hellwigb2950862008-12-02 09:54:17 -0500208static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500209{
210 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500211
212 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400213 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500214 return state;
215 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500216 state->private = 0;
Filipe Manana27a35072014-07-06 20:09:59 +0100217 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000218 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500219 atomic_set(&state->refs, 1);
220 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100221 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500222 return state;
223}
Chris Masond1310b22008-01-24 16:13:08 -0500224
Chris Mason4845e442010-05-25 20:56:50 -0400225void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500226{
Chris Masond1310b22008-01-24 16:13:08 -0500227 if (!state)
228 return;
229 if (atomic_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100230 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000231 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100232 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500233 kmem_cache_free(extent_state_cache, state);
234 }
235}
Chris Masond1310b22008-01-24 16:13:08 -0500236
Filipe Mananaf2071b22014-02-12 15:05:53 +0000237static struct rb_node *tree_insert(struct rb_root *root,
238 struct rb_node *search_start,
239 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000240 struct rb_node *node,
241 struct rb_node ***p_in,
242 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500243{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000244 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500245 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500246 struct tree_entry *entry;
247
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000248 if (p_in && parent_in) {
249 p = *p_in;
250 parent = *parent_in;
251 goto do_insert;
252 }
253
Filipe Mananaf2071b22014-02-12 15:05:53 +0000254 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500255 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500256 parent = *p;
257 entry = rb_entry(parent, struct tree_entry, rb_node);
258
259 if (offset < entry->start)
260 p = &(*p)->rb_left;
261 else if (offset > entry->end)
262 p = &(*p)->rb_right;
263 else
264 return parent;
265 }
266
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000267do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500268 rb_link_node(node, parent, p);
269 rb_insert_color(node, root);
270 return NULL;
271}
272
Chris Mason80ea96b2008-02-01 14:51:59 -0500273static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000274 struct rb_node **prev_ret,
275 struct rb_node **next_ret,
276 struct rb_node ***p_ret,
277 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500278{
Chris Mason80ea96b2008-02-01 14:51:59 -0500279 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000280 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500281 struct rb_node *prev = NULL;
282 struct rb_node *orig_prev = NULL;
283 struct tree_entry *entry;
284 struct tree_entry *prev_entry = NULL;
285
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000286 while (*n) {
287 prev = *n;
288 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500289 prev_entry = entry;
290
291 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000292 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500293 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000294 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500295 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000296 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500297 }
298
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000299 if (p_ret)
300 *p_ret = n;
301 if (parent_ret)
302 *parent_ret = prev;
303
Chris Masond1310b22008-01-24 16:13:08 -0500304 if (prev_ret) {
305 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500306 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500307 prev = rb_next(prev);
308 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
309 }
310 *prev_ret = prev;
311 prev = orig_prev;
312 }
313
314 if (next_ret) {
315 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500316 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500317 prev = rb_prev(prev);
318 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
319 }
320 *next_ret = prev;
321 }
322 return NULL;
323}
324
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000325static inline struct rb_node *
326tree_search_for_insert(struct extent_io_tree *tree,
327 u64 offset,
328 struct rb_node ***p_ret,
329 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500330{
Chris Mason70dec802008-01-29 09:59:12 -0500331 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500332 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500333
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000334 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500335 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500336 return prev;
337 return ret;
338}
339
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000340static inline struct rb_node *tree_search(struct extent_io_tree *tree,
341 u64 offset)
342{
343 return tree_search_for_insert(tree, offset, NULL, NULL);
344}
345
Josef Bacik9ed74f22009-09-11 16:12:44 -0400346static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
347 struct extent_state *other)
348{
349 if (tree->ops && tree->ops->merge_extent_hook)
350 tree->ops->merge_extent_hook(tree->mapping->host, new,
351 other);
352}
353
Chris Masond1310b22008-01-24 16:13:08 -0500354/*
355 * utility function to look for merge candidates inside a given range.
356 * Any extents with matching state are merged together into a single
357 * extent in the tree. Extents with EXTENT_IO in their state field
358 * are not merged because the end_io handlers need to be able to do
359 * operations on them without sleeping (or doing allocations/splits).
360 *
361 * This should be called with the tree lock held.
362 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000363static void merge_state(struct extent_io_tree *tree,
364 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500365{
366 struct extent_state *other;
367 struct rb_node *other_node;
368
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400369 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000370 return;
Chris Masond1310b22008-01-24 16:13:08 -0500371
372 other_node = rb_prev(&state->rb_node);
373 if (other_node) {
374 other = rb_entry(other_node, struct extent_state, rb_node);
375 if (other->end == state->start - 1 &&
376 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400377 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500378 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500379 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100380 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500381 free_extent_state(other);
382 }
383 }
384 other_node = rb_next(&state->rb_node);
385 if (other_node) {
386 other = rb_entry(other_node, struct extent_state, rb_node);
387 if (other->start == state->end + 1 &&
388 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400389 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400390 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400391 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100392 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400393 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500394 }
395 }
Chris Masond1310b22008-01-24 16:13:08 -0500396}
397
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000398static void set_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000399 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500400{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000401 if (tree->ops && tree->ops->set_bit_hook)
402 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500403}
404
405static void clear_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000406 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500407{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400408 if (tree->ops && tree->ops->clear_bit_hook)
409 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500410}
411
Xiao Guangrong3150b692011-07-14 03:19:08 +0000412static void set_state_bits(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000413 struct extent_state *state, unsigned long *bits);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000414
Chris Masond1310b22008-01-24 16:13:08 -0500415/*
416 * insert an extent_state struct into the tree. 'bits' are set on the
417 * struct before it is inserted.
418 *
419 * This may return -EEXIST if the extent is already there, in which case the
420 * state struct is freed.
421 *
422 * The tree lock is not taken internally. This is a utility function and
423 * probably isn't what you want to call (see set/clear_extent_bit).
424 */
425static int insert_state(struct extent_io_tree *tree,
426 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000427 struct rb_node ***p,
428 struct rb_node **parent,
David Sterba41074882013-04-29 13:38:46 +0000429 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500430{
431 struct rb_node *node;
432
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000433 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500434 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200435 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500436 state->start = start;
437 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400438
Xiao Guangrong3150b692011-07-14 03:19:08 +0000439 set_state_bits(tree, state, bits);
440
Filipe Mananaf2071b22014-02-12 15:05:53 +0000441 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500442 if (node) {
443 struct extent_state *found;
444 found = rb_entry(node, struct extent_state, rb_node);
Frank Holtonefe120a2013-12-20 11:37:06 -0500445 printk(KERN_ERR "BTRFS: found node %llu %llu on insert of "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200446 "%llu %llu\n",
447 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500448 return -EEXIST;
449 }
450 merge_state(tree, state);
451 return 0;
452}
453
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000454static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400455 u64 split)
456{
457 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000458 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400459}
460
Chris Masond1310b22008-01-24 16:13:08 -0500461/*
462 * split a given extent state struct in two, inserting the preallocated
463 * struct 'prealloc' as the newly created second half. 'split' indicates an
464 * offset inside 'orig' where it should be split.
465 *
466 * Before calling,
467 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
468 * are two extent state structs in the tree:
469 * prealloc: [orig->start, split - 1]
470 * orig: [ split, orig->end ]
471 *
472 * The tree locks are not taken by this function. They need to be held
473 * by the caller.
474 */
475static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
476 struct extent_state *prealloc, u64 split)
477{
478 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400479
480 split_cb(tree, orig, split);
481
Chris Masond1310b22008-01-24 16:13:08 -0500482 prealloc->start = orig->start;
483 prealloc->end = split - 1;
484 prealloc->state = orig->state;
485 orig->start = split;
486
Filipe Mananaf2071b22014-02-12 15:05:53 +0000487 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
488 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500489 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500490 free_extent_state(prealloc);
491 return -EEXIST;
492 }
493 return 0;
494}
495
Li Zefancdc6a392012-03-12 16:39:48 +0800496static struct extent_state *next_state(struct extent_state *state)
497{
498 struct rb_node *next = rb_next(&state->rb_node);
499 if (next)
500 return rb_entry(next, struct extent_state, rb_node);
501 else
502 return NULL;
503}
504
Chris Masond1310b22008-01-24 16:13:08 -0500505/*
506 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800507 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500508 *
509 * If no bits are set on the state struct after clearing things, the
510 * struct is freed and removed from the tree
511 */
Li Zefancdc6a392012-03-12 16:39:48 +0800512static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
513 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000514 unsigned long *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500515{
Li Zefancdc6a392012-03-12 16:39:48 +0800516 struct extent_state *next;
David Sterba41074882013-04-29 13:38:46 +0000517 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500518
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400519 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500520 u64 range = state->end - state->start + 1;
521 WARN_ON(range > tree->dirty_bytes);
522 tree->dirty_bytes -= range;
523 }
Chris Mason291d6732008-01-29 15:55:23 -0500524 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400525 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500526 if (wake)
527 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400528 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800529 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100530 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500531 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100532 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500533 free_extent_state(state);
534 } else {
535 WARN_ON(1);
536 }
537 } else {
538 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800539 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500540 }
Li Zefancdc6a392012-03-12 16:39:48 +0800541 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500542}
543
Xiao Guangrong82337672011-04-20 06:44:57 +0000544static struct extent_state *
545alloc_extent_state_atomic(struct extent_state *prealloc)
546{
547 if (!prealloc)
548 prealloc = alloc_extent_state(GFP_ATOMIC);
549
550 return prealloc;
551}
552
Eric Sandeen48a3b632013-04-25 20:41:01 +0000553static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400554{
555 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
556 "Extent tree was modified by another "
557 "thread while locked.");
558}
559
Chris Masond1310b22008-01-24 16:13:08 -0500560/*
561 * clear some bits on a range in the tree. This may require splitting
562 * or inserting elements in the tree, so the gfp mask is used to
563 * indicate which allocations or sleeping are allowed.
564 *
565 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
566 * the given range from the tree regardless of state (ie for truncate).
567 *
568 * the range [start, end] is inclusive.
569 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100570 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500571 */
572int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000573 unsigned long bits, int wake, int delete,
Chris Mason2c64c532009-09-02 15:04:12 -0400574 struct extent_state **cached_state,
575 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500576{
577 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400578 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500579 struct extent_state *prealloc = NULL;
580 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400581 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500582 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000583 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500584
Josef Bacika5dee372013-12-13 10:02:44 -0500585 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000586
Josef Bacik7ee9e442013-06-21 16:37:03 -0400587 if (bits & EXTENT_DELALLOC)
588 bits |= EXTENT_NORESERVE;
589
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400590 if (delete)
591 bits |= ~EXTENT_CTLBITS;
592 bits |= EXTENT_FIRST_DELALLOC;
593
Josef Bacik2ac55d42010-02-03 19:33:23 +0000594 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
595 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500596again:
597 if (!prealloc && (mask & __GFP_WAIT)) {
598 prealloc = alloc_extent_state(mask);
599 if (!prealloc)
600 return -ENOMEM;
601 }
602
Chris Masoncad321a2008-12-17 14:51:42 -0500603 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400604 if (cached_state) {
605 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000606
607 if (clear) {
608 *cached_state = NULL;
609 cached_state = NULL;
610 }
611
Filipe Manana27a35072014-07-06 20:09:59 +0100612 if (cached && extent_state_in_tree(cached) &&
613 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000614 if (clear)
615 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400616 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400617 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400618 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000619 if (clear)
620 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400621 }
Chris Masond1310b22008-01-24 16:13:08 -0500622 /*
623 * this search will find the extents that end after
624 * our range starts
625 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500626 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500627 if (!node)
628 goto out;
629 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400630hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500631 if (state->start > end)
632 goto out;
633 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400634 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500635
Liu Bo04493142012-02-16 18:34:37 +0800636 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800637 if (!(state->state & bits)) {
638 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800639 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800640 }
Liu Bo04493142012-02-16 18:34:37 +0800641
Chris Masond1310b22008-01-24 16:13:08 -0500642 /*
643 * | ---- desired range ---- |
644 * | state | or
645 * | ------------- state -------------- |
646 *
647 * We need to split the extent we found, and may flip
648 * bits on second half.
649 *
650 * If the extent we found extends past our range, we
651 * just split and search again. It'll get split again
652 * the next time though.
653 *
654 * If the extent we found is inside our range, we clear
655 * the desired bit on it.
656 */
657
658 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000659 prealloc = alloc_extent_state_atomic(prealloc);
660 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500661 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400662 if (err)
663 extent_io_tree_panic(tree, err);
664
Chris Masond1310b22008-01-24 16:13:08 -0500665 prealloc = NULL;
666 if (err)
667 goto out;
668 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800669 state = clear_state_bit(tree, state, &bits, wake);
670 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500671 }
672 goto search_again;
673 }
674 /*
675 * | ---- desired range ---- |
676 * | state |
677 * We need to split the extent, and clear the bit
678 * on the first half
679 */
680 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000681 prealloc = alloc_extent_state_atomic(prealloc);
682 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500683 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400684 if (err)
685 extent_io_tree_panic(tree, err);
686
Chris Masond1310b22008-01-24 16:13:08 -0500687 if (wake)
688 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400689
Jeff Mahoney6763af82012-03-01 14:56:29 +0100690 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400691
Chris Masond1310b22008-01-24 16:13:08 -0500692 prealloc = NULL;
693 goto out;
694 }
Chris Mason42daec22009-09-23 19:51:09 -0400695
Li Zefancdc6a392012-03-12 16:39:48 +0800696 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800697next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400698 if (last_end == (u64)-1)
699 goto out;
700 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800701 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800702 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500703 goto search_again;
704
705out:
Chris Masoncad321a2008-12-17 14:51:42 -0500706 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500707 if (prealloc)
708 free_extent_state(prealloc);
709
Jeff Mahoney6763af82012-03-01 14:56:29 +0100710 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500711
712search_again:
713 if (start > end)
714 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500715 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500716 if (mask & __GFP_WAIT)
717 cond_resched();
718 goto again;
719}
Chris Masond1310b22008-01-24 16:13:08 -0500720
Jeff Mahoney143bede2012-03-01 14:56:26 +0100721static void wait_on_state(struct extent_io_tree *tree,
722 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500723 __releases(tree->lock)
724 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500725{
726 DEFINE_WAIT(wait);
727 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500728 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500729 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500730 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500731 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500732}
733
734/*
735 * waits for one or more bits to clear on a range in the state tree.
736 * The range [start, end] is inclusive.
737 * The tree lock is taken by this function
738 */
David Sterba41074882013-04-29 13:38:46 +0000739static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
740 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500741{
742 struct extent_state *state;
743 struct rb_node *node;
744
Josef Bacika5dee372013-12-13 10:02:44 -0500745 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000746
Chris Masoncad321a2008-12-17 14:51:42 -0500747 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500748again:
749 while (1) {
750 /*
751 * this search will find all the extents that end after
752 * our range starts
753 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500754 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100755process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500756 if (!node)
757 break;
758
759 state = rb_entry(node, struct extent_state, rb_node);
760
761 if (state->start > end)
762 goto out;
763
764 if (state->state & bits) {
765 start = state->start;
766 atomic_inc(&state->refs);
767 wait_on_state(tree, state);
768 free_extent_state(state);
769 goto again;
770 }
771 start = state->end + 1;
772
773 if (start > end)
774 break;
775
Filipe Mananac50d3e72014-03-31 14:53:25 +0100776 if (!cond_resched_lock(&tree->lock)) {
777 node = rb_next(node);
778 goto process_node;
779 }
Chris Masond1310b22008-01-24 16:13:08 -0500780 }
781out:
Chris Masoncad321a2008-12-17 14:51:42 -0500782 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500783}
Chris Masond1310b22008-01-24 16:13:08 -0500784
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000785static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500786 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000787 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500788{
David Sterba41074882013-04-29 13:38:46 +0000789 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400790
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000791 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400792 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500793 u64 range = state->end - state->start + 1;
794 tree->dirty_bytes += range;
795 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400796 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500797}
798
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100799static void cache_state_if_flags(struct extent_state *state,
800 struct extent_state **cached_ptr,
801 const u64 flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400802{
803 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100804 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400805 *cached_ptr = state;
806 atomic_inc(&state->refs);
807 }
808 }
809}
810
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100811static void cache_state(struct extent_state *state,
812 struct extent_state **cached_ptr)
813{
814 return cache_state_if_flags(state, cached_ptr,
815 EXTENT_IOBITS | EXTENT_BOUNDARY);
816}
817
Chris Masond1310b22008-01-24 16:13:08 -0500818/*
Chris Mason1edbb732009-09-02 13:24:36 -0400819 * set some bits on a range in the tree. This may require allocations or
820 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500821 *
Chris Mason1edbb732009-09-02 13:24:36 -0400822 * If any of the exclusive bits are set, this will fail with -EEXIST if some
823 * part of the range already has the desired bits set. The start of the
824 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500825 *
Chris Mason1edbb732009-09-02 13:24:36 -0400826 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500827 */
Chris Mason1edbb732009-09-02 13:24:36 -0400828
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100829static int __must_check
830__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000831 unsigned long bits, unsigned long exclusive_bits,
832 u64 *failed_start, struct extent_state **cached_state,
833 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500834{
835 struct extent_state *state;
836 struct extent_state *prealloc = NULL;
837 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000838 struct rb_node **p;
839 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500840 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500841 u64 last_start;
842 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400843
Josef Bacika5dee372013-12-13 10:02:44 -0500844 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000845
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400846 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500847again:
848 if (!prealloc && (mask & __GFP_WAIT)) {
849 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000850 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500851 }
852
Chris Masoncad321a2008-12-17 14:51:42 -0500853 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400854 if (cached_state && *cached_state) {
855 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400856 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100857 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400858 node = &state->rb_node;
859 goto hit_next;
860 }
861 }
Chris Masond1310b22008-01-24 16:13:08 -0500862 /*
863 * this search will find all the extents that end after
864 * our range starts.
865 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000866 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500867 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000868 prealloc = alloc_extent_state_atomic(prealloc);
869 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000870 err = insert_state(tree, prealloc, start, end,
871 &p, &parent, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400872 if (err)
873 extent_io_tree_panic(tree, err);
874
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000875 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500876 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500877 goto out;
878 }
Chris Masond1310b22008-01-24 16:13:08 -0500879 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400880hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500881 last_start = state->start;
882 last_end = state->end;
883
884 /*
885 * | ---- desired range ---- |
886 * | state |
887 *
888 * Just lock what we found and keep going
889 */
890 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400891 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500892 *failed_start = state->start;
893 err = -EEXIST;
894 goto out;
895 }
Chris Mason42daec22009-09-23 19:51:09 -0400896
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000897 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400898 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500899 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400900 if (last_end == (u64)-1)
901 goto out;
902 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800903 state = next_state(state);
904 if (start < end && state && state->start == start &&
905 !need_resched())
906 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500907 goto search_again;
908 }
909
910 /*
911 * | ---- desired range ---- |
912 * | state |
913 * or
914 * | ------------- state -------------- |
915 *
916 * We need to split the extent we found, and may flip bits on
917 * second half.
918 *
919 * If the extent we found extends past our
920 * range, we just split and search again. It'll get split
921 * again the next time though.
922 *
923 * If the extent we found is inside our range, we set the
924 * desired bit on it.
925 */
926 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400927 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500928 *failed_start = start;
929 err = -EEXIST;
930 goto out;
931 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000932
933 prealloc = alloc_extent_state_atomic(prealloc);
934 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500935 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400936 if (err)
937 extent_io_tree_panic(tree, err);
938
Chris Masond1310b22008-01-24 16:13:08 -0500939 prealloc = NULL;
940 if (err)
941 goto out;
942 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000943 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400944 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500945 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400946 if (last_end == (u64)-1)
947 goto out;
948 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800949 state = next_state(state);
950 if (start < end && state && state->start == start &&
951 !need_resched())
952 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500953 }
954 goto search_again;
955 }
956 /*
957 * | ---- desired range ---- |
958 * | state | or | state |
959 *
960 * There's a hole, we need to insert something in it and
961 * ignore the extent we found.
962 */
963 if (state->start > start) {
964 u64 this_end;
965 if (end < last_start)
966 this_end = end;
967 else
Chris Masond3977122009-01-05 21:25:51 -0500968 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000969
970 prealloc = alloc_extent_state_atomic(prealloc);
971 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000972
973 /*
974 * Avoid to free 'prealloc' if it can be merged with
975 * the later extent.
976 */
Chris Masond1310b22008-01-24 16:13:08 -0500977 err = insert_state(tree, prealloc, start, this_end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000978 NULL, NULL, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400979 if (err)
980 extent_io_tree_panic(tree, err);
981
Chris Mason2c64c532009-09-02 15:04:12 -0400982 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500983 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500984 start = this_end + 1;
985 goto search_again;
986 }
987 /*
988 * | ---- desired range ---- |
989 * | state |
990 * We need to split the extent, and set the bit
991 * on the first half
992 */
993 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400994 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500995 *failed_start = start;
996 err = -EEXIST;
997 goto out;
998 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000999
1000 prealloc = alloc_extent_state_atomic(prealloc);
1001 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001002 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001003 if (err)
1004 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001005
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001006 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -04001007 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001008 merge_state(tree, prealloc);
1009 prealloc = NULL;
1010 goto out;
1011 }
1012
1013 goto search_again;
1014
1015out:
Chris Masoncad321a2008-12-17 14:51:42 -05001016 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001017 if (prealloc)
1018 free_extent_state(prealloc);
1019
1020 return err;
1021
1022search_again:
1023 if (start > end)
1024 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -05001025 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001026 if (mask & __GFP_WAIT)
1027 cond_resched();
1028 goto again;
1029}
Chris Masond1310b22008-01-24 16:13:08 -05001030
David Sterba41074882013-04-29 13:38:46 +00001031int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1032 unsigned long bits, u64 * failed_start,
1033 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001034{
1035 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
1036 cached_state, mask);
1037}
1038
1039
Josef Bacik462d6fa2011-09-26 13:56:12 -04001040/**
Liu Bo10983f22012-07-11 15:26:19 +08001041 * convert_extent_bit - convert all bits in a given range from one bit to
1042 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001043 * @tree: the io tree to search
1044 * @start: the start offset in bytes
1045 * @end: the end offset in bytes (inclusive)
1046 * @bits: the bits to set in this range
1047 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001048 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001049 * @mask: the allocation mask
1050 *
1051 * This will go through and set bits for the given range. If any states exist
1052 * already in this range they are set with the given bit and cleared of the
1053 * clear_bits. This is only meant to be used by things that are mergeable, ie
1054 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1055 * boundary bits like LOCK.
1056 */
1057int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001058 unsigned long bits, unsigned long clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -04001059 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001060{
1061 struct extent_state *state;
1062 struct extent_state *prealloc = NULL;
1063 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001064 struct rb_node **p;
1065 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001066 int err = 0;
1067 u64 last_start;
1068 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001069 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070
Josef Bacika5dee372013-12-13 10:02:44 -05001071 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001072
Josef Bacik462d6fa2011-09-26 13:56:12 -04001073again:
1074 if (!prealloc && (mask & __GFP_WAIT)) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001075 /*
1076 * Best effort, don't worry if extent state allocation fails
1077 * here for the first iteration. We might have a cached state
1078 * that matches exactly the target range, in which case no
1079 * extent state allocations are needed. We'll only know this
1080 * after locking the tree.
1081 */
Josef Bacik462d6fa2011-09-26 13:56:12 -04001082 prealloc = alloc_extent_state(mask);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001083 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084 return -ENOMEM;
1085 }
1086
1087 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001088 if (cached_state && *cached_state) {
1089 state = *cached_state;
1090 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001091 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001092 node = &state->rb_node;
1093 goto hit_next;
1094 }
1095 }
1096
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097 /*
1098 * this search will find all the extents that end after
1099 * our range starts.
1100 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001101 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001102 if (!node) {
1103 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001104 if (!prealloc) {
1105 err = -ENOMEM;
1106 goto out;
1107 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001108 err = insert_state(tree, prealloc, start, end,
1109 &p, &parent, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001110 if (err)
1111 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001112 cache_state(prealloc, cached_state);
1113 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001114 goto out;
1115 }
1116 state = rb_entry(node, struct extent_state, rb_node);
1117hit_next:
1118 last_start = state->start;
1119 last_end = state->end;
1120
1121 /*
1122 * | ---- desired range ---- |
1123 * | state |
1124 *
1125 * Just lock what we found and keep going
1126 */
1127 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001128 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001129 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001130 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001131 if (last_end == (u64)-1)
1132 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001133 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001134 if (start < end && state && state->start == start &&
1135 !need_resched())
1136 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001137 goto search_again;
1138 }
1139
1140 /*
1141 * | ---- desired range ---- |
1142 * | state |
1143 * or
1144 * | ------------- state -------------- |
1145 *
1146 * We need to split the extent we found, and may flip bits on
1147 * second half.
1148 *
1149 * If the extent we found extends past our
1150 * range, we just split and search again. It'll get split
1151 * again the next time though.
1152 *
1153 * If the extent we found is inside our range, we set the
1154 * desired bit on it.
1155 */
1156 if (state->start < start) {
1157 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001158 if (!prealloc) {
1159 err = -ENOMEM;
1160 goto out;
1161 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001162 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001163 if (err)
1164 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001165 prealloc = NULL;
1166 if (err)
1167 goto out;
1168 if (state->end <= end) {
1169 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001170 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001171 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001172 if (last_end == (u64)-1)
1173 goto out;
1174 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001175 if (start < end && state && state->start == start &&
1176 !need_resched())
1177 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001178 }
1179 goto search_again;
1180 }
1181 /*
1182 * | ---- desired range ---- |
1183 * | state | or | state |
1184 *
1185 * There's a hole, we need to insert something in it and
1186 * ignore the extent we found.
1187 */
1188 if (state->start > start) {
1189 u64 this_end;
1190 if (end < last_start)
1191 this_end = end;
1192 else
1193 this_end = last_start - 1;
1194
1195 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001196 if (!prealloc) {
1197 err = -ENOMEM;
1198 goto out;
1199 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001200
1201 /*
1202 * Avoid to free 'prealloc' if it can be merged with
1203 * the later extent.
1204 */
1205 err = insert_state(tree, prealloc, start, this_end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001206 NULL, NULL, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001207 if (err)
1208 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001209 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001210 prealloc = NULL;
1211 start = this_end + 1;
1212 goto search_again;
1213 }
1214 /*
1215 * | ---- desired range ---- |
1216 * | state |
1217 * We need to split the extent, and set the bit
1218 * on the first half
1219 */
1220 if (state->start <= end && state->end > end) {
1221 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001222 if (!prealloc) {
1223 err = -ENOMEM;
1224 goto out;
1225 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001226
1227 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001228 if (err)
1229 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001230
1231 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001232 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001233 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001234 prealloc = NULL;
1235 goto out;
1236 }
1237
1238 goto search_again;
1239
1240out:
1241 spin_unlock(&tree->lock);
1242 if (prealloc)
1243 free_extent_state(prealloc);
1244
1245 return err;
1246
1247search_again:
1248 if (start > end)
1249 goto out;
1250 spin_unlock(&tree->lock);
1251 if (mask & __GFP_WAIT)
1252 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001253 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001254 goto again;
1255}
1256
Chris Masond1310b22008-01-24 16:13:08 -05001257/* wrappers around set/clear extent bit */
1258int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1259 gfp_t mask)
1260{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001261 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001262 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001263}
Chris Masond1310b22008-01-24 16:13:08 -05001264
1265int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001266 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001267{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001268 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001269 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001270}
Chris Masond1310b22008-01-24 16:13:08 -05001271
1272int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001273 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001274{
Chris Mason2c64c532009-09-02 15:04:12 -04001275 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001276}
Chris Masond1310b22008-01-24 16:13:08 -05001277
1278int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001279 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001280{
1281 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001282 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001283 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001284}
Chris Masond1310b22008-01-24 16:13:08 -05001285
Liu Bo9e8a4a82012-09-05 19:10:51 -06001286int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1287 struct extent_state **cached_state, gfp_t mask)
1288{
1289 return set_extent_bit(tree, start, end,
1290 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1291 NULL, cached_state, mask);
1292}
1293
Chris Masond1310b22008-01-24 16:13:08 -05001294int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1295 gfp_t mask)
1296{
1297 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001298 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001299 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001300}
Chris Masond1310b22008-01-24 16:13:08 -05001301
1302int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1303 gfp_t mask)
1304{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001305 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001306 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001307}
Chris Masond1310b22008-01-24 16:13:08 -05001308
Chris Masond1310b22008-01-24 16:13:08 -05001309int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001310 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001311{
Liu Bo6b67a322013-03-28 08:30:28 +00001312 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001313 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001314}
Chris Masond1310b22008-01-24 16:13:08 -05001315
Josef Bacik5fd02042012-05-02 14:00:54 -04001316int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1317 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001318{
Chris Mason2c64c532009-09-02 15:04:12 -04001319 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001320 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001321}
Chris Masond1310b22008-01-24 16:13:08 -05001322
Chris Masond352ac62008-09-29 15:18:18 -04001323/*
1324 * either insert or lock state struct between start and end use mask to tell
1325 * us if waiting is desired.
1326 */
Chris Mason1edbb732009-09-02 13:24:36 -04001327int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001328 unsigned long bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001329{
1330 int err;
1331 u64 failed_start;
1332 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001333 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1334 EXTENT_LOCKED, &failed_start,
1335 cached_state, GFP_NOFS);
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 lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001347{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001348 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001349}
1350
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001351int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001352{
1353 int err;
1354 u64 failed_start;
1355
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001356 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1357 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001358 if (err == -EEXIST) {
1359 if (failed_start > start)
1360 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001361 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001362 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001363 }
Josef Bacik25179202008-10-29 14:49:05 -04001364 return 1;
1365}
Josef Bacik25179202008-10-29 14:49:05 -04001366
Chris Mason2c64c532009-09-02 15:04:12 -04001367int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1368 struct extent_state **cached, gfp_t mask)
1369{
1370 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1371 mask);
1372}
1373
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001374int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001375{
Chris Mason2c64c532009-09-02 15:04:12 -04001376 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001377 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001378}
Chris Masond1310b22008-01-24 16:13:08 -05001379
Chris Mason4adaa612013-03-26 13:07:00 -04001380int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1381{
1382 unsigned long index = start >> PAGE_CACHE_SHIFT;
1383 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1384 struct page *page;
1385
1386 while (index <= end_index) {
1387 page = find_get_page(inode->i_mapping, index);
1388 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1389 clear_page_dirty_for_io(page);
1390 page_cache_release(page);
1391 index++;
1392 }
1393 return 0;
1394}
1395
1396int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1397{
1398 unsigned long index = start >> PAGE_CACHE_SHIFT;
1399 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1400 struct page *page;
1401
1402 while (index <= end_index) {
1403 page = find_get_page(inode->i_mapping, index);
1404 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1405 account_page_redirty(page);
1406 __set_page_dirty_nobuffers(page);
1407 page_cache_release(page);
1408 index++;
1409 }
1410 return 0;
1411}
1412
Chris Masond1310b22008-01-24 16:13:08 -05001413/*
Chris Masond1310b22008-01-24 16:13:08 -05001414 * helper function to set both pages and extents in the tree writeback
1415 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001416static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001417{
1418 unsigned long index = start >> PAGE_CACHE_SHIFT;
1419 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1420 struct page *page;
1421
1422 while (index <= end_index) {
1423 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001424 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001425 set_page_writeback(page);
1426 page_cache_release(page);
1427 index++;
1428 }
Chris Masond1310b22008-01-24 16:13:08 -05001429 return 0;
1430}
Chris Masond1310b22008-01-24 16:13:08 -05001431
Chris Masond352ac62008-09-29 15:18:18 -04001432/* find the first state struct with 'bits' set after 'start', and
1433 * return it. tree->lock must be held. NULL will returned if
1434 * nothing was found after 'start'
1435 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001436static struct extent_state *
1437find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +00001438 u64 start, unsigned long bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001439{
1440 struct rb_node *node;
1441 struct extent_state *state;
1442
1443 /*
1444 * this search will find all the extents that end after
1445 * our range starts.
1446 */
1447 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001448 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001449 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001450
Chris Masond3977122009-01-05 21:25:51 -05001451 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001452 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001453 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001454 return state;
Chris Masond3977122009-01-05 21:25:51 -05001455
Chris Masond7fc6402008-02-18 12:12:38 -05001456 node = rb_next(node);
1457 if (!node)
1458 break;
1459 }
1460out:
1461 return NULL;
1462}
Chris Masond7fc6402008-02-18 12:12:38 -05001463
Chris Masond352ac62008-09-29 15:18:18 -04001464/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001465 * find the first offset in the io tree with 'bits' set. zero is
1466 * returned if we find something, and *start_ret and *end_ret are
1467 * set to reflect the state struct that was found.
1468 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001469 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001470 */
1471int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba41074882013-04-29 13:38:46 +00001472 u64 *start_ret, u64 *end_ret, unsigned long bits,
Josef Bacike6138872012-09-27 17:07:30 -04001473 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001474{
1475 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001476 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001477 int ret = 1;
1478
1479 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001480 if (cached_state && *cached_state) {
1481 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001482 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001483 n = rb_next(&state->rb_node);
1484 while (n) {
1485 state = rb_entry(n, struct extent_state,
1486 rb_node);
1487 if (state->state & bits)
1488 goto got_it;
1489 n = rb_next(n);
1490 }
1491 free_extent_state(*cached_state);
1492 *cached_state = NULL;
1493 goto out;
1494 }
1495 free_extent_state(*cached_state);
1496 *cached_state = NULL;
1497 }
1498
Xiao Guangrong69261c42011-07-14 03:19:45 +00001499 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001500got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001501 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001502 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001503 *start_ret = state->start;
1504 *end_ret = state->end;
1505 ret = 0;
1506 }
Josef Bacike6138872012-09-27 17:07:30 -04001507out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001508 spin_unlock(&tree->lock);
1509 return ret;
1510}
1511
1512/*
Chris Masond352ac62008-09-29 15:18:18 -04001513 * find a contiguous range of bytes in the file marked as delalloc, not
1514 * more than 'max_bytes'. start and end are used to return the range,
1515 *
1516 * 1 is returned if we find something, 0 if nothing was in the tree
1517 */
Chris Masonc8b97812008-10-29 14:49:59 -04001518static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001519 u64 *start, u64 *end, u64 max_bytes,
1520 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001521{
1522 struct rb_node *node;
1523 struct extent_state *state;
1524 u64 cur_start = *start;
1525 u64 found = 0;
1526 u64 total_bytes = 0;
1527
Chris Masoncad321a2008-12-17 14:51:42 -05001528 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001529
Chris Masond1310b22008-01-24 16:13:08 -05001530 /*
1531 * this search will find all the extents that end after
1532 * our range starts.
1533 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001534 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001535 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001536 if (!found)
1537 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001538 goto out;
1539 }
1540
Chris Masond3977122009-01-05 21:25:51 -05001541 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001542 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001543 if (found && (state->start != cur_start ||
1544 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001545 goto out;
1546 }
1547 if (!(state->state & EXTENT_DELALLOC)) {
1548 if (!found)
1549 *end = state->end;
1550 goto out;
1551 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001552 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001553 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001554 *cached_state = state;
1555 atomic_inc(&state->refs);
1556 }
Chris Masond1310b22008-01-24 16:13:08 -05001557 found++;
1558 *end = state->end;
1559 cur_start = state->end + 1;
1560 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001561 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001562 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001563 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001564 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001565 break;
1566 }
1567out:
Chris Masoncad321a2008-12-17 14:51:42 -05001568 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001569 return found;
1570}
1571
Jeff Mahoney143bede2012-03-01 14:56:26 +01001572static noinline void __unlock_for_delalloc(struct inode *inode,
1573 struct page *locked_page,
1574 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001575{
1576 int ret;
1577 struct page *pages[16];
1578 unsigned long index = start >> PAGE_CACHE_SHIFT;
1579 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1580 unsigned long nr_pages = end_index - index + 1;
1581 int i;
1582
1583 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001584 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001585
Chris Masond3977122009-01-05 21:25:51 -05001586 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001587 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001588 min_t(unsigned long, nr_pages,
1589 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001590 for (i = 0; i < ret; i++) {
1591 if (pages[i] != locked_page)
1592 unlock_page(pages[i]);
1593 page_cache_release(pages[i]);
1594 }
1595 nr_pages -= ret;
1596 index += ret;
1597 cond_resched();
1598 }
Chris Masonc8b97812008-10-29 14:49:59 -04001599}
1600
1601static noinline int lock_delalloc_pages(struct inode *inode,
1602 struct page *locked_page,
1603 u64 delalloc_start,
1604 u64 delalloc_end)
1605{
1606 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1607 unsigned long start_index = index;
1608 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1609 unsigned long pages_locked = 0;
1610 struct page *pages[16];
1611 unsigned long nrpages;
1612 int ret;
1613 int i;
1614
1615 /* the caller is responsible for locking the start index */
1616 if (index == locked_page->index && index == end_index)
1617 return 0;
1618
1619 /* skip the page at the start index */
1620 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001621 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001622 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001623 min_t(unsigned long,
1624 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001625 if (ret == 0) {
1626 ret = -EAGAIN;
1627 goto done;
1628 }
1629 /* now we have an array of pages, lock them all */
1630 for (i = 0; i < ret; i++) {
1631 /*
1632 * the caller is taking responsibility for
1633 * locked_page
1634 */
Chris Mason771ed682008-11-06 22:02:51 -05001635 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001636 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001637 if (!PageDirty(pages[i]) ||
1638 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001639 ret = -EAGAIN;
1640 unlock_page(pages[i]);
1641 page_cache_release(pages[i]);
1642 goto done;
1643 }
1644 }
Chris Masonc8b97812008-10-29 14:49:59 -04001645 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001646 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001647 }
Chris Masonc8b97812008-10-29 14:49:59 -04001648 nrpages -= ret;
1649 index += ret;
1650 cond_resched();
1651 }
1652 ret = 0;
1653done:
1654 if (ret && pages_locked) {
1655 __unlock_for_delalloc(inode, locked_page,
1656 delalloc_start,
1657 ((u64)(start_index + pages_locked - 1)) <<
1658 PAGE_CACHE_SHIFT);
1659 }
1660 return ret;
1661}
1662
1663/*
1664 * find a contiguous range of bytes in the file marked as delalloc, not
1665 * more than 'max_bytes'. start and end are used to return the range,
1666 *
1667 * 1 is returned if we find something, 0 if nothing was in the tree
1668 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001669STATIC u64 find_lock_delalloc_range(struct inode *inode,
1670 struct extent_io_tree *tree,
1671 struct page *locked_page, u64 *start,
1672 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001673{
1674 u64 delalloc_start;
1675 u64 delalloc_end;
1676 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001677 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001678 int ret;
1679 int loops = 0;
1680
1681again:
1682 /* step one, find a bunch of delalloc bytes starting at start */
1683 delalloc_start = *start;
1684 delalloc_end = 0;
1685 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001686 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001687 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001688 *start = delalloc_start;
1689 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001690 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001691 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001692 }
1693
1694 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001695 * start comes from the offset of locked_page. We have to lock
1696 * pages in order, so we can't process delalloc bytes before
1697 * locked_page
1698 */
Chris Masond3977122009-01-05 21:25:51 -05001699 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001700 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001701
1702 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001703 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001704 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001705 if (delalloc_end + 1 - delalloc_start > max_bytes)
1706 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001707
Chris Masonc8b97812008-10-29 14:49:59 -04001708 /* step two, lock all the pages after the page that has start */
1709 ret = lock_delalloc_pages(inode, locked_page,
1710 delalloc_start, delalloc_end);
1711 if (ret == -EAGAIN) {
1712 /* some of the pages are gone, lets avoid looping by
1713 * shortening the size of the delalloc range we're searching
1714 */
Chris Mason9655d292009-09-02 15:22:30 -04001715 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001716 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001717 if (!loops) {
Josef Bacik7bf811a52013-10-07 22:11:09 -04001718 max_bytes = PAGE_CACHE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001719 loops = 1;
1720 goto again;
1721 } else {
1722 found = 0;
1723 goto out_failed;
1724 }
1725 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001726 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001727
1728 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001729 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001730
1731 /* then test to make sure it is all still delalloc */
1732 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001733 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001734 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001735 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1736 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001737 __unlock_for_delalloc(inode, locked_page,
1738 delalloc_start, delalloc_end);
1739 cond_resched();
1740 goto again;
1741 }
Chris Mason9655d292009-09-02 15:22:30 -04001742 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001743 *start = delalloc_start;
1744 *end = delalloc_end;
1745out_failed:
1746 return found;
1747}
1748
Josef Bacikc2790a22013-07-29 11:20:47 -04001749int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1750 struct page *locked_page,
1751 unsigned long clear_bits,
1752 unsigned long page_ops)
Chris Masonc8b97812008-10-29 14:49:59 -04001753{
Josef Bacikc2790a22013-07-29 11:20:47 -04001754 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Masonc8b97812008-10-29 14:49:59 -04001755 int ret;
1756 struct page *pages[16];
1757 unsigned long index = start >> PAGE_CACHE_SHIFT;
1758 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1759 unsigned long nr_pages = end_index - index + 1;
1760 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001761
Chris Mason2c64c532009-09-02 15:04:12 -04001762 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacikc2790a22013-07-29 11:20:47 -04001763 if (page_ops == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001764 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001765
Filipe Manana704de492014-10-06 22:14:22 +01001766 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1767 mapping_set_error(inode->i_mapping, -EIO);
1768
Chris Masond3977122009-01-05 21:25:51 -05001769 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001770 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001771 min_t(unsigned long,
1772 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001773 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001774
Josef Bacikc2790a22013-07-29 11:20:47 -04001775 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001776 SetPagePrivate2(pages[i]);
1777
Chris Masonc8b97812008-10-29 14:49:59 -04001778 if (pages[i] == locked_page) {
1779 page_cache_release(pages[i]);
1780 continue;
1781 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001782 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001783 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001784 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001785 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001786 if (page_ops & PAGE_SET_ERROR)
1787 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001788 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001789 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001790 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001791 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001792 page_cache_release(pages[i]);
1793 }
1794 nr_pages -= ret;
1795 index += ret;
1796 cond_resched();
1797 }
1798 return 0;
1799}
Chris Masonc8b97812008-10-29 14:49:59 -04001800
Chris Masond352ac62008-09-29 15:18:18 -04001801/*
1802 * count the number of bytes in the tree that have a given bit(s)
1803 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1804 * cached. The total number found is returned.
1805 */
Chris Masond1310b22008-01-24 16:13:08 -05001806u64 count_range_bits(struct extent_io_tree *tree,
1807 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001808 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001809{
1810 struct rb_node *node;
1811 struct extent_state *state;
1812 u64 cur_start = *start;
1813 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001814 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001815 int found = 0;
1816
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301817 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001818 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001819
Chris Masoncad321a2008-12-17 14:51:42 -05001820 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001821 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1822 total_bytes = tree->dirty_bytes;
1823 goto out;
1824 }
1825 /*
1826 * this search will find all the extents that end after
1827 * our range starts.
1828 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001829 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001830 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001831 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001832
Chris Masond3977122009-01-05 21:25:51 -05001833 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001834 state = rb_entry(node, struct extent_state, rb_node);
1835 if (state->start > search_end)
1836 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001837 if (contig && found && state->start > last + 1)
1838 break;
1839 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001840 total_bytes += min(search_end, state->end) + 1 -
1841 max(cur_start, state->start);
1842 if (total_bytes >= max_bytes)
1843 break;
1844 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001845 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001846 found = 1;
1847 }
Chris Masonec29ed52011-02-23 16:23:20 -05001848 last = state->end;
1849 } else if (contig && found) {
1850 break;
Chris Masond1310b22008-01-24 16:13:08 -05001851 }
1852 node = rb_next(node);
1853 if (!node)
1854 break;
1855 }
1856out:
Chris Masoncad321a2008-12-17 14:51:42 -05001857 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001858 return total_bytes;
1859}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001860
Chris Masond352ac62008-09-29 15:18:18 -04001861/*
1862 * set the private field for a given byte offset in the tree. If there isn't
1863 * an extent_state there already, this does nothing.
1864 */
Sergei Trofimovich171170c2013-08-14 23:27:46 +03001865static int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
Chris Masond1310b22008-01-24 16:13:08 -05001866{
1867 struct rb_node *node;
1868 struct extent_state *state;
1869 int ret = 0;
1870
Chris Masoncad321a2008-12-17 14:51:42 -05001871 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001872 /*
1873 * this search will find all the extents that end after
1874 * our range starts.
1875 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001876 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001877 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001878 ret = -ENOENT;
1879 goto out;
1880 }
1881 state = rb_entry(node, struct extent_state, rb_node);
1882 if (state->start != start) {
1883 ret = -ENOENT;
1884 goto out;
1885 }
1886 state->private = private;
1887out:
Chris Masoncad321a2008-12-17 14:51:42 -05001888 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001889 return ret;
1890}
1891
1892int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1893{
1894 struct rb_node *node;
1895 struct extent_state *state;
1896 int ret = 0;
1897
Chris Masoncad321a2008-12-17 14:51:42 -05001898 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001899 /*
1900 * this search will find all the extents that end after
1901 * our range starts.
1902 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001903 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001904 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001905 ret = -ENOENT;
1906 goto out;
1907 }
1908 state = rb_entry(node, struct extent_state, rb_node);
1909 if (state->start != start) {
1910 ret = -ENOENT;
1911 goto out;
1912 }
1913 *private = state->private;
1914out:
Chris Masoncad321a2008-12-17 14:51:42 -05001915 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001916 return ret;
1917}
1918
1919/*
1920 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001921 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001922 * has the bits set. Otherwise, 1 is returned if any bit in the
1923 * range is found set.
1924 */
1925int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001926 unsigned long bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001927{
1928 struct extent_state *state = NULL;
1929 struct rb_node *node;
1930 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001931
Chris Masoncad321a2008-12-17 14:51:42 -05001932 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001933 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001934 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001935 node = &cached->rb_node;
1936 else
1937 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001938 while (node && start <= end) {
1939 state = rb_entry(node, struct extent_state, rb_node);
1940
1941 if (filled && state->start > start) {
1942 bitset = 0;
1943 break;
1944 }
1945
1946 if (state->start > end)
1947 break;
1948
1949 if (state->state & bits) {
1950 bitset = 1;
1951 if (!filled)
1952 break;
1953 } else if (filled) {
1954 bitset = 0;
1955 break;
1956 }
Chris Mason46562ce2009-09-23 20:23:16 -04001957
1958 if (state->end == (u64)-1)
1959 break;
1960
Chris Masond1310b22008-01-24 16:13:08 -05001961 start = state->end + 1;
1962 if (start > end)
1963 break;
1964 node = rb_next(node);
1965 if (!node) {
1966 if (filled)
1967 bitset = 0;
1968 break;
1969 }
1970 }
Chris Masoncad321a2008-12-17 14:51:42 -05001971 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001972 return bitset;
1973}
Chris Masond1310b22008-01-24 16:13:08 -05001974
1975/*
1976 * helper function to set a given page up to date if all the
1977 * extents in the tree for that page are up to date
1978 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001979static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001980{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001981 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001982 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001983 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001984 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001985}
1986
Miao Xie8b110e32014-09-12 18:44:03 +08001987int free_io_failure(struct inode *inode, struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001988{
1989 int ret;
1990 int err = 0;
1991 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1992
1993 set_state_private(failure_tree, rec->start, 0);
1994 ret = clear_extent_bits(failure_tree, rec->start,
1995 rec->start + rec->len - 1,
1996 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1997 if (ret)
1998 err = ret;
1999
David Woodhouse53b381b2013-01-29 18:40:14 -05002000 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2001 rec->start + rec->len - 1,
2002 EXTENT_DAMAGED, GFP_NOFS);
2003 if (ret && !err)
2004 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002005
2006 kfree(rec);
2007 return err;
2008}
2009
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002010/*
2011 * this bypasses the standard btrfs submit functions deliberately, as
2012 * the standard behavior is to write all copies in a raid setup. here we only
2013 * want to write the one bad copy. so we do the mapping for ourselves and issue
2014 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002015 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002016 * actually prevents the read that triggered the error from finishing.
2017 * currently, there can be no more than two copies of every data bit. thus,
2018 * exactly one rewrite is required.
2019 */
Miao Xie1203b682014-09-12 18:44:01 +08002020int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
2021 struct page *page, unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002022{
Miao Xie1203b682014-09-12 18:44:01 +08002023 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002024 struct bio *bio;
2025 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002026 u64 map_length = 0;
2027 u64 sector;
2028 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002029 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002030 int ret;
2031
Ilya Dryomov908960c2013-11-03 19:06:39 +02002032 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002033 BUG_ON(!mirror_num);
2034
David Woodhouse53b381b2013-01-29 18:40:14 -05002035 /* we can't repair anything in raid56 yet */
2036 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2037 return 0;
2038
Chris Mason9be33952013-05-17 18:30:14 -04002039 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002040 if (!bio)
2041 return -EIO;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002042 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002043 map_length = length;
2044
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002045 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046 &map_length, &bbio, mirror_num);
2047 if (ret) {
2048 bio_put(bio);
2049 return -EIO;
2050 }
2051 BUG_ON(mirror_num != bbio->mirror_num);
2052 sector = bbio->stripes[mirror_num-1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002053 bio->bi_iter.bi_sector = sector;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002054 dev = bbio->stripes[mirror_num-1].dev;
2055 kfree(bbio);
2056 if (!dev || !dev->bdev || !dev->writeable) {
2057 bio_put(bio);
2058 return -EIO;
2059 }
2060 bio->bi_bdev = dev->bdev;
Miao Xieffdd2012014-09-12 18:44:00 +08002061 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002062
Kent Overstreet33879d42013-11-23 22:33:32 -08002063 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002064 /* try to remap that extent elsewhere? */
2065 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002066 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002067 return -EIO;
2068 }
2069
Frank Holtonefe120a2013-12-20 11:37:06 -05002070 printk_ratelimited_in_rcu(KERN_INFO
Miao Xie1203b682014-09-12 18:44:01 +08002071 "BTRFS: read error corrected: ino %llu off %llu (dev %s sector %llu)\n",
2072 btrfs_ino(inode), start,
2073 rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002074 bio_put(bio);
2075 return 0;
2076}
2077
Josef Bacikea466792012-03-26 21:57:36 -04002078int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2079 int mirror_num)
2080{
Josef Bacikea466792012-03-26 21:57:36 -04002081 u64 start = eb->start;
2082 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002083 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002084
Ilya Dryomov908960c2013-11-03 19:06:39 +02002085 if (root->fs_info->sb->s_flags & MS_RDONLY)
2086 return -EROFS;
2087
Josef Bacikea466792012-03-26 21:57:36 -04002088 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002089 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002090
2091 ret = repair_io_failure(root->fs_info->btree_inode, start,
2092 PAGE_CACHE_SIZE, start, p,
2093 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002094 if (ret)
2095 break;
2096 start += PAGE_CACHE_SIZE;
2097 }
2098
2099 return ret;
2100}
2101
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002102/*
2103 * each time an IO finishes, we do a fast check in the IO failure tree
2104 * to see if we need to process or clean up an io_failure_record
2105 */
Miao Xie8b110e32014-09-12 18:44:03 +08002106int clean_io_failure(struct inode *inode, u64 start, struct page *page,
2107 unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108{
2109 u64 private;
2110 u64 private_failure;
2111 struct io_failure_record *failrec;
Ilya Dryomov908960c2013-11-03 19:06:39 +02002112 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 struct extent_state *state;
2114 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002115 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002116
2117 private = 0;
2118 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2119 (u64)-1, 1, EXTENT_DIRTY, 0);
2120 if (!ret)
2121 return 0;
2122
2123 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2124 &private_failure);
2125 if (ret)
2126 return 0;
2127
2128 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2129 BUG_ON(!failrec->this_mirror);
2130
2131 if (failrec->in_validation) {
2132 /* there was no real error, just free the record */
2133 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2134 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002135 goto out;
2136 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002137 if (fs_info->sb->s_flags & MS_RDONLY)
2138 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139
2140 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2141 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2142 failrec->start,
2143 EXTENT_LOCKED);
2144 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2145
Miao Xie883d0de2013-07-25 19:22:35 +08002146 if (state && state->start <= failrec->start &&
2147 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002148 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2149 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002150 if (num_copies > 1) {
Miao Xie1203b682014-09-12 18:44:01 +08002151 repair_io_failure(inode, start, failrec->len,
Miao Xie454ff3d2014-09-12 18:43:58 +08002152 failrec->logical, page,
Miao Xie1203b682014-09-12 18:44:01 +08002153 pg_offset, failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002154 }
2155 }
2156
2157out:
Miao Xie454ff3d2014-09-12 18:43:58 +08002158 free_io_failure(inode, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002159
Miao Xie454ff3d2014-09-12 18:43:58 +08002160 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002161}
2162
Miao Xief6124962014-09-12 18:44:04 +08002163/*
2164 * Can be called when
2165 * - hold extent lock
2166 * - under ordered extent
2167 * - the inode is freeing
2168 */
2169void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
2170{
2171 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2172 struct io_failure_record *failrec;
2173 struct extent_state *state, *next;
2174
2175 if (RB_EMPTY_ROOT(&failure_tree->state))
2176 return;
2177
2178 spin_lock(&failure_tree->lock);
2179 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2180 while (state) {
2181 if (state->start > end)
2182 break;
2183
2184 ASSERT(state->end <= end);
2185
2186 next = next_state(state);
2187
2188 failrec = (struct io_failure_record *)state->private;
2189 free_extent_state(state);
2190 kfree(failrec);
2191
2192 state = next;
2193 }
2194 spin_unlock(&failure_tree->lock);
2195}
2196
Miao Xie2fe63032014-09-12 18:43:59 +08002197int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
2198 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002199{
Miao Xie2fe63032014-09-12 18:43:59 +08002200 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002201 u64 private;
2202 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002203 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2204 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2205 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002206 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002207 u64 logical;
2208
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002209 ret = get_state_private(failure_tree, start, &private);
2210 if (ret) {
2211 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2212 if (!failrec)
2213 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002214
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002215 failrec->start = start;
2216 failrec->len = end - start + 1;
2217 failrec->this_mirror = 0;
2218 failrec->bio_flags = 0;
2219 failrec->in_validation = 0;
2220
2221 read_lock(&em_tree->lock);
2222 em = lookup_extent_mapping(em_tree, start, failrec->len);
2223 if (!em) {
2224 read_unlock(&em_tree->lock);
2225 kfree(failrec);
2226 return -EIO;
2227 }
2228
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002229 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002230 free_extent_map(em);
2231 em = NULL;
2232 }
2233 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002234 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002235 kfree(failrec);
2236 return -EIO;
2237 }
Miao Xie2fe63032014-09-12 18:43:59 +08002238
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002239 logical = start - em->start;
2240 logical = em->block_start + logical;
2241 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2242 logical = em->block_start;
2243 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2244 extent_set_compress_type(&failrec->bio_flags,
2245 em->compress_type);
2246 }
Miao Xie2fe63032014-09-12 18:43:59 +08002247
2248 pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n",
2249 logical, start, failrec->len);
2250
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002251 failrec->logical = logical;
2252 free_extent_map(em);
2253
2254 /* set the bits in the private failure tree */
2255 ret = set_extent_bits(failure_tree, start, end,
2256 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2257 if (ret >= 0)
2258 ret = set_state_private(failure_tree, start,
2259 (u64)(unsigned long)failrec);
2260 /* set the bits in the inode's tree */
2261 if (ret >= 0)
2262 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2263 GFP_NOFS);
2264 if (ret < 0) {
2265 kfree(failrec);
2266 return ret;
2267 }
2268 } else {
2269 failrec = (struct io_failure_record *)(unsigned long)private;
Miao Xie2fe63032014-09-12 18:43:59 +08002270 pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 failrec->logical, failrec->start, failrec->len,
2272 failrec->in_validation);
2273 /*
2274 * when data can be on disk more than twice, add to failrec here
2275 * (e.g. with a list for failed_mirror) to make
2276 * clean_io_failure() clean all those errors at once.
2277 */
2278 }
Miao Xie2fe63032014-09-12 18:43:59 +08002279
2280 *failrec_ret = failrec;
2281
2282 return 0;
2283}
2284
2285int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
2286 struct io_failure_record *failrec, int failed_mirror)
2287{
2288 int num_copies;
2289
Stefan Behrens5d964052012-11-05 14:59:07 +01002290 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2291 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002292 if (num_copies == 1) {
2293 /*
2294 * we only have a single copy of the data, so don't bother with
2295 * all the retry and error correction code that follows. no
2296 * matter what the error is, it is very likely to persist.
2297 */
Miao Xie2fe63032014-09-12 18:43:59 +08002298 pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
Miao Xie09a7f7a2013-07-25 19:22:32 +08002299 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002300 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002301 }
2302
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002303 /*
2304 * there are two premises:
2305 * a) deliver good data to the caller
2306 * b) correct the bad sectors on disk
2307 */
2308 if (failed_bio->bi_vcnt > 1) {
2309 /*
2310 * to fulfill b), we need to know the exact failing sectors, as
2311 * we don't want to rewrite any more than the failed ones. thus,
2312 * we need separate read requests for the failed bio
2313 *
2314 * if the following BUG_ON triggers, our validation request got
2315 * merged. we need separate requests for our algorithm to work.
2316 */
2317 BUG_ON(failrec->in_validation);
2318 failrec->in_validation = 1;
2319 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002320 } else {
2321 /*
2322 * we're ready to fulfill a) and b) alongside. get a good copy
2323 * of the failed sector and if we succeed, we have setup
2324 * everything for repair_io_failure to do the rest for us.
2325 */
2326 if (failrec->in_validation) {
2327 BUG_ON(failrec->this_mirror != failed_mirror);
2328 failrec->in_validation = 0;
2329 failrec->this_mirror = 0;
2330 }
2331 failrec->failed_mirror = failed_mirror;
2332 failrec->this_mirror++;
2333 if (failrec->this_mirror == failed_mirror)
2334 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002335 }
2336
Miao Xiefacc8a222013-07-25 19:22:34 +08002337 if (failrec->this_mirror > num_copies) {
Miao Xie2fe63032014-09-12 18:43:59 +08002338 pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002339 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002340 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002341 }
2342
Miao Xie2fe63032014-09-12 18:43:59 +08002343 return 1;
2344}
2345
2346
2347struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2348 struct io_failure_record *failrec,
2349 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002350 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002351{
2352 struct bio *bio;
2353 struct btrfs_io_bio *btrfs_failed_bio;
2354 struct btrfs_io_bio *btrfs_bio;
2355
Chris Mason9be33952013-05-17 18:30:14 -04002356 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Miao Xie2fe63032014-09-12 18:43:59 +08002357 if (!bio)
2358 return NULL;
2359
2360 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002361 bio->bi_iter.bi_sector = failrec->logical >> 9;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002362 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002363 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002364 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002365
Miao Xiefacc8a222013-07-25 19:22:34 +08002366 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2367 if (btrfs_failed_bio->csum) {
2368 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2369 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2370
2371 btrfs_bio = btrfs_io_bio(bio);
2372 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002373 icsum *= csum_size;
2374 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002375 csum_size);
2376 }
2377
Miao Xie2fe63032014-09-12 18:43:59 +08002378 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002379
Miao Xie2fe63032014-09-12 18:43:59 +08002380 return bio;
2381}
2382
2383/*
2384 * this is a generic handler for readpage errors (default
2385 * readpage_io_failed_hook). if other copies exist, read those and write back
2386 * good data to the failed position. does not investigate in remapping the
2387 * failed extent elsewhere, hoping the device will be smart enough to do this as
2388 * needed
2389 */
2390
2391static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2392 struct page *page, u64 start, u64 end,
2393 int failed_mirror)
2394{
2395 struct io_failure_record *failrec;
2396 struct inode *inode = page->mapping->host;
2397 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2398 struct bio *bio;
2399 int read_mode;
2400 int ret;
2401
2402 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2403
2404 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2405 if (ret)
2406 return ret;
2407
2408 ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
2409 if (!ret) {
2410 free_io_failure(inode, failrec);
2411 return -EIO;
2412 }
2413
2414 if (failed_bio->bi_vcnt > 1)
2415 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2416 else
2417 read_mode = READ_SYNC;
2418
2419 phy_offset >>= inode->i_sb->s_blocksize_bits;
2420 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2421 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002422 (int)phy_offset, failed_bio->bi_end_io,
2423 NULL);
Miao Xie2fe63032014-09-12 18:43:59 +08002424 if (!bio) {
2425 free_io_failure(inode, failrec);
2426 return -EIO;
2427 }
2428
2429 pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n",
2430 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002431
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002432 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2433 failrec->this_mirror,
2434 failrec->bio_flags, 0);
Miao Xie6c387ab2014-09-12 18:43:57 +08002435 if (ret) {
Miao Xie454ff3d2014-09-12 18:43:58 +08002436 free_io_failure(inode, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002437 bio_put(bio);
2438 }
2439
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002440 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002441}
2442
Chris Masond1310b22008-01-24 16:13:08 -05002443/* lots and lots of room for performance fixes in the end_bio funcs */
2444
Jeff Mahoney87826df2012-02-15 16:23:57 +01002445int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2446{
2447 int uptodate = (err == 0);
2448 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002449 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002450
2451 tree = &BTRFS_I(page->mapping->host)->io_tree;
2452
2453 if (tree->ops && tree->ops->writepage_end_io_hook) {
2454 ret = tree->ops->writepage_end_io_hook(page, start,
2455 end, NULL, uptodate);
2456 if (ret)
2457 uptodate = 0;
2458 }
2459
Jeff Mahoney87826df2012-02-15 16:23:57 +01002460 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002461 ClearPageUptodate(page);
2462 SetPageError(page);
Liu Bo5dca6ee2014-05-12 12:47:36 +08002463 ret = ret < 0 ? ret : -EIO;
2464 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002465 }
2466 return 0;
2467}
2468
Chris Masond1310b22008-01-24 16:13:08 -05002469/*
2470 * after a writepage IO is done, we need to:
2471 * clear the uptodate bits on error
2472 * clear the writeback bits in the extent tree for this IO
2473 * end_page_writeback if the page has no more pending IO
2474 *
2475 * Scheduling is not allowed, so the extent state tree is expected
2476 * to have one and only one object corresponding to this IO.
2477 */
Chris Masond1310b22008-01-24 16:13:08 -05002478static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002479{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002480 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002481 u64 start;
2482 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002483 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002484
Kent Overstreet2c30c712013-11-07 12:20:26 -08002485 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002486 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002487
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002488 /* We always issue full-page reads, but if some block
2489 * in a page fails to read, blk_update_request() will
2490 * advance bv_offset and adjust bv_len to compensate.
2491 * Print a warning for nonzero offsets, and an error
2492 * if they don't add up to a full page. */
Frank Holtonefe120a2013-12-20 11:37:06 -05002493 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
2494 if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
2495 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
2496 "partial page write in btrfs with offset %u and length %u",
2497 bvec->bv_offset, bvec->bv_len);
2498 else
2499 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
2500 "incomplete page write in btrfs with offset %u and "
2501 "length %u",
2502 bvec->bv_offset, bvec->bv_len);
2503 }
Chris Masond1310b22008-01-24 16:13:08 -05002504
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002505 start = page_offset(page);
2506 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002507
Jeff Mahoney87826df2012-02-15 16:23:57 +01002508 if (end_extent_writepage(page, err, start, end))
2509 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002510
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002511 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002512 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002513
Chris Masond1310b22008-01-24 16:13:08 -05002514 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002515}
2516
Miao Xie883d0de2013-07-25 19:22:35 +08002517static void
2518endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2519 int uptodate)
2520{
2521 struct extent_state *cached = NULL;
2522 u64 end = start + len - 1;
2523
2524 if (uptodate && tree->track_uptodate)
2525 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2526 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2527}
2528
Chris Masond1310b22008-01-24 16:13:08 -05002529/*
2530 * after a readpage IO is done, we need to:
2531 * clear the uptodate bits on error
2532 * set the uptodate bits if things worked
2533 * set the page up to date if all extents in the tree are uptodate
2534 * clear the lock bit in the extent tree
2535 * unlock the page if there are no other extents locked for it
2536 *
2537 * Scheduling is not allowed, so the extent state tree is expected
2538 * to have one and only one object corresponding to this IO.
2539 */
Chris Masond1310b22008-01-24 16:13:08 -05002540static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002541{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002542 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002543 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Miao Xiefacc8a222013-07-25 19:22:34 +08002544 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002545 struct extent_io_tree *tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002546 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002547 u64 start;
2548 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002549 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002550 u64 extent_start = 0;
2551 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002552 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002553 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002554 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002555
Chris Masond20f7042008-12-08 16:58:54 -05002556 if (err)
2557 uptodate = 0;
2558
Kent Overstreet2c30c712013-11-07 12:20:26 -08002559 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002560 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002561 struct inode *inode = page->mapping->host;
Arne Jansen507903b2011-04-06 10:02:20 +00002562
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002563 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
Miao Xiec1dc0892014-09-12 18:43:56 +08002564 "mirror=%u\n", (u64)bio->bi_iter.bi_sector, err,
Chris Mason9be33952013-05-17 18:30:14 -04002565 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002566 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002567
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002568 /* We always issue full-page reads, but if some block
2569 * in a page fails to read, blk_update_request() will
2570 * advance bv_offset and adjust bv_len to compensate.
2571 * Print a warning for nonzero offsets, and an error
2572 * if they don't add up to a full page. */
Frank Holtonefe120a2013-12-20 11:37:06 -05002573 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
2574 if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
2575 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
2576 "partial page read in btrfs with offset %u and length %u",
2577 bvec->bv_offset, bvec->bv_len);
2578 else
2579 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
2580 "incomplete page read in btrfs with offset %u and "
2581 "length %u",
2582 bvec->bv_offset, bvec->bv_len);
2583 }
Chris Masond1310b22008-01-24 16:13:08 -05002584
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002585 start = page_offset(page);
2586 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002587 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002588
Chris Mason9be33952013-05-17 18:30:14 -04002589 mirror = io_bio->mirror_num;
Miao Xief2a09da2013-07-25 19:22:33 +08002590 if (likely(uptodate && tree->ops &&
2591 tree->ops->readpage_end_io_hook)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002592 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2593 page, start, end,
2594 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002595 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002596 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002597 else
Miao Xie1203b682014-09-12 18:44:01 +08002598 clean_io_failure(inode, start, page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002599 }
Josef Bacikea466792012-03-26 21:57:36 -04002600
Miao Xief2a09da2013-07-25 19:22:33 +08002601 if (likely(uptodate))
2602 goto readpage_ok;
2603
2604 if (tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002605 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002606 if (!ret && !err &&
2607 test_bit(BIO_UPTODATE, &bio->bi_flags))
2608 uptodate = 1;
Miao Xief2a09da2013-07-25 19:22:33 +08002609 } else {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002610 /*
2611 * The generic bio_readpage_error handles errors the
2612 * following way: If possible, new read requests are
2613 * created and submitted and will end up in
2614 * end_bio_extent_readpage as well (if we're lucky, not
2615 * in the !uptodate case). In that case it returns 0 and
2616 * we just go on with the next page in our bio. If it
2617 * can't handle the error it will return -EIO and we
2618 * remain responsible for that page.
2619 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002620 ret = bio_readpage_error(bio, offset, page, start, end,
2621 mirror);
Chris Mason7e383262008-04-09 16:28:12 -04002622 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002623 uptodate =
2624 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002625 if (err)
2626 uptodate = 0;
Liu Bo38c1c2e2014-08-19 23:33:13 +08002627 offset += len;
Chris Mason7e383262008-04-09 16:28:12 -04002628 continue;
2629 }
2630 }
Miao Xief2a09da2013-07-25 19:22:33 +08002631readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002632 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002633 loff_t i_size = i_size_read(inode);
2634 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002635 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002636
2637 /* Zero out the end if this page straddles i_size */
Liu Boa583c022014-08-19 23:32:22 +08002638 off = i_size & (PAGE_CACHE_SIZE-1);
2639 if (page->index == end_index && off)
2640 zero_user_segment(page, off, PAGE_CACHE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002641 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002642 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002643 ClearPageUptodate(page);
2644 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002645 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002646 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002647 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002648
2649 if (unlikely(!uptodate)) {
2650 if (extent_len) {
2651 endio_readpage_release_extent(tree,
2652 extent_start,
2653 extent_len, 1);
2654 extent_start = 0;
2655 extent_len = 0;
2656 }
2657 endio_readpage_release_extent(tree, start,
2658 end - start + 1, 0);
2659 } else if (!extent_len) {
2660 extent_start = start;
2661 extent_len = end + 1 - start;
2662 } else if (extent_start + extent_len == start) {
2663 extent_len += end + 1 - start;
2664 } else {
2665 endio_readpage_release_extent(tree, extent_start,
2666 extent_len, uptodate);
2667 extent_start = start;
2668 extent_len = end + 1 - start;
2669 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002670 }
Chris Masond1310b22008-01-24 16:13:08 -05002671
Miao Xie883d0de2013-07-25 19:22:35 +08002672 if (extent_len)
2673 endio_readpage_release_extent(tree, extent_start, extent_len,
2674 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002675 if (io_bio->end_io)
2676 io_bio->end_io(io_bio, err);
Chris Masond1310b22008-01-24 16:13:08 -05002677 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002678}
2679
Chris Mason9be33952013-05-17 18:30:14 -04002680/*
2681 * this allocates from the btrfs_bioset. We're returning a bio right now
2682 * but you can call btrfs_io_bio for the appropriate container_of magic
2683 */
Miao Xie88f794e2010-11-22 03:02:55 +00002684struct bio *
2685btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2686 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002687{
Miao Xiefacc8a222013-07-25 19:22:34 +08002688 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002689 struct bio *bio;
2690
Chris Mason9be33952013-05-17 18:30:14 -04002691 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002692
2693 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002694 while (!bio && (nr_vecs /= 2)) {
2695 bio = bio_alloc_bioset(gfp_flags,
2696 nr_vecs, btrfs_bioset);
2697 }
Chris Masond1310b22008-01-24 16:13:08 -05002698 }
2699
2700 if (bio) {
2701 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002702 bio->bi_iter.bi_sector = first_sector;
Miao Xiefacc8a222013-07-25 19:22:34 +08002703 btrfs_bio = btrfs_io_bio(bio);
2704 btrfs_bio->csum = NULL;
2705 btrfs_bio->csum_allocated = NULL;
2706 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002707 }
2708 return bio;
2709}
2710
Chris Mason9be33952013-05-17 18:30:14 -04002711struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2712{
Miao Xie23ea8e52014-09-12 18:43:54 +08002713 struct btrfs_io_bio *btrfs_bio;
2714 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002715
Miao Xie23ea8e52014-09-12 18:43:54 +08002716 new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2717 if (new) {
2718 btrfs_bio = btrfs_io_bio(new);
2719 btrfs_bio->csum = NULL;
2720 btrfs_bio->csum_allocated = NULL;
2721 btrfs_bio->end_io = NULL;
2722 }
2723 return new;
2724}
Chris Mason9be33952013-05-17 18:30:14 -04002725
2726/* this also allocates from the btrfs_bioset */
2727struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2728{
Miao Xiefacc8a222013-07-25 19:22:34 +08002729 struct btrfs_io_bio *btrfs_bio;
2730 struct bio *bio;
2731
2732 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2733 if (bio) {
2734 btrfs_bio = btrfs_io_bio(bio);
2735 btrfs_bio->csum = NULL;
2736 btrfs_bio->csum_allocated = NULL;
2737 btrfs_bio->end_io = NULL;
2738 }
2739 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002740}
2741
2742
Jeff Mahoney355808c2011-10-03 23:23:14 -04002743static int __must_check submit_one_bio(int rw, struct bio *bio,
2744 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002745{
Chris Masond1310b22008-01-24 16:13:08 -05002746 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002747 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2748 struct page *page = bvec->bv_page;
2749 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002750 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002751
Miao Xie4eee4fa2012-12-21 09:17:45 +00002752 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002753
David Woodhouse902b22f2008-08-20 08:51:49 -04002754 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002755
2756 bio_get(bio);
2757
Chris Mason065631f2008-02-20 12:07:25 -05002758 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002759 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002760 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002761 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002762 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002763
Chris Masond1310b22008-01-24 16:13:08 -05002764 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2765 ret = -EOPNOTSUPP;
2766 bio_put(bio);
2767 return ret;
2768}
2769
David Woodhouse64a16702009-07-15 23:29:37 +01002770static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002771 unsigned long offset, size_t size, struct bio *bio,
2772 unsigned long bio_flags)
2773{
2774 int ret = 0;
2775 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002776 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002777 bio_flags);
2778 BUG_ON(ret < 0);
2779 return ret;
2780
2781}
2782
Chris Masond1310b22008-01-24 16:13:08 -05002783static int submit_extent_page(int rw, struct extent_io_tree *tree,
2784 struct page *page, sector_t sector,
2785 size_t size, unsigned long offset,
2786 struct block_device *bdev,
2787 struct bio **bio_ret,
2788 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002789 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002790 int mirror_num,
2791 unsigned long prev_bio_flags,
2792 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002793{
2794 int ret = 0;
2795 struct bio *bio;
2796 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002797 int contig = 0;
2798 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2799 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002800 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002801
2802 if (bio_ret && *bio_ret) {
2803 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002804 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002805 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002806 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002807 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002808
2809 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002810 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002811 bio_add_page(bio, page, page_size, offset) < page_size) {
2812 ret = submit_one_bio(rw, bio, mirror_num,
2813 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002814 if (ret < 0)
2815 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002816 bio = NULL;
2817 } else {
2818 return 0;
2819 }
2820 }
Chris Masonc8b97812008-10-29 14:49:59 -04002821 if (this_compressed)
2822 nr = BIO_MAX_PAGES;
2823 else
2824 nr = bio_get_nr_vecs(bdev);
2825
Miao Xie88f794e2010-11-22 03:02:55 +00002826 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002827 if (!bio)
2828 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002829
Chris Masonc8b97812008-10-29 14:49:59 -04002830 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002831 bio->bi_end_io = end_io_func;
2832 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002833
Chris Masond3977122009-01-05 21:25:51 -05002834 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002835 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002836 else
Chris Masonc8b97812008-10-29 14:49:59 -04002837 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002838
2839 return ret;
2840}
2841
Eric Sandeen48a3b632013-04-25 20:41:01 +00002842static void attach_extent_buffer_page(struct extent_buffer *eb,
2843 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002844{
2845 if (!PagePrivate(page)) {
2846 SetPagePrivate(page);
2847 page_cache_get(page);
2848 set_page_private(page, (unsigned long)eb);
2849 } else {
2850 WARN_ON(page->private != (unsigned long)eb);
2851 }
2852}
2853
Chris Masond1310b22008-01-24 16:13:08 -05002854void set_page_extent_mapped(struct page *page)
2855{
2856 if (!PagePrivate(page)) {
2857 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002858 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002859 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002860 }
2861}
2862
Miao Xie125bac012013-07-25 19:22:37 +08002863static struct extent_map *
2864__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2865 u64 start, u64 len, get_extent_t *get_extent,
2866 struct extent_map **em_cached)
2867{
2868 struct extent_map *em;
2869
2870 if (em_cached && *em_cached) {
2871 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002872 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002873 start < extent_map_end(em)) {
2874 atomic_inc(&em->refs);
2875 return em;
2876 }
2877
2878 free_extent_map(em);
2879 *em_cached = NULL;
2880 }
2881
2882 em = get_extent(inode, page, pg_offset, start, len, 0);
2883 if (em_cached && !IS_ERR_OR_NULL(em)) {
2884 BUG_ON(*em_cached);
2885 atomic_inc(&em->refs);
2886 *em_cached = em;
2887 }
2888 return em;
2889}
Chris Masond1310b22008-01-24 16:13:08 -05002890/*
2891 * basic readpage implementation. Locked extent state structs are inserted
2892 * into the tree that are removed when the IO is done (by the end_io
2893 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002894 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002895 */
Miao Xie99740902013-07-25 19:22:36 +08002896static int __do_readpage(struct extent_io_tree *tree,
2897 struct page *page,
2898 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002899 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002900 struct bio **bio, int mirror_num,
2901 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002902{
2903 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002904 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002905 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2906 u64 end;
2907 u64 cur = start;
2908 u64 extent_offset;
2909 u64 last_byte = i_size_read(inode);
2910 u64 block_start;
2911 u64 cur_end;
2912 sector_t sector;
2913 struct extent_map *em;
2914 struct block_device *bdev;
2915 int ret;
2916 int nr = 0;
Mark Fasheh4b384312013-08-06 11:42:50 -07002917 int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
David Sterba306e16c2011-04-19 14:29:38 +02002918 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002919 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002920 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002921 size_t blocksize = inode->i_sb->s_blocksize;
Mark Fasheh4b384312013-08-06 11:42:50 -07002922 unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
Chris Masond1310b22008-01-24 16:13:08 -05002923
2924 set_page_extent_mapped(page);
2925
Miao Xie99740902013-07-25 19:22:36 +08002926 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002927 if (!PageUptodate(page)) {
2928 if (cleancache_get_page(page) == 0) {
2929 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002930 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002931 goto out;
2932 }
2933 }
2934
Chris Masonc8b97812008-10-29 14:49:59 -04002935 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2936 char *userpage;
2937 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2938
2939 if (zero_offset) {
2940 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002941 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002942 memset(userpage + zero_offset, 0, iosize);
2943 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002944 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002945 }
2946 }
Chris Masond1310b22008-01-24 16:13:08 -05002947 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002948 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2949
Chris Masond1310b22008-01-24 16:13:08 -05002950 if (cur >= last_byte) {
2951 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002952 struct extent_state *cached = NULL;
2953
David Sterba306e16c2011-04-19 14:29:38 +02002954 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002955 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002956 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002957 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002958 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002959 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002960 &cached, GFP_NOFS);
Mark Fasheh4b384312013-08-06 11:42:50 -07002961 if (!parent_locked)
2962 unlock_extent_cached(tree, cur,
2963 cur + iosize - 1,
2964 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002965 break;
2966 }
Miao Xie125bac012013-07-25 19:22:37 +08002967 em = __get_extent_map(inode, page, pg_offset, cur,
2968 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002969 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002970 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07002971 if (!parent_locked)
2972 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002973 break;
2974 }
Chris Masond1310b22008-01-24 16:13:08 -05002975 extent_offset = cur - em->start;
2976 BUG_ON(extent_map_end(em) <= cur);
2977 BUG_ON(end < cur);
2978
Li Zefan261507a02010-12-17 14:21:50 +08002979 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002980 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002981 extent_set_compress_type(&this_bio_flag,
2982 em->compress_type);
2983 }
Chris Masonc8b97812008-10-29 14:49:59 -04002984
Chris Masond1310b22008-01-24 16:13:08 -05002985 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2986 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002987 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002988 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2989 disk_io_size = em->block_len;
2990 sector = em->block_start >> 9;
2991 } else {
2992 sector = (em->block_start + extent_offset) >> 9;
2993 disk_io_size = iosize;
2994 }
Chris Masond1310b22008-01-24 16:13:08 -05002995 bdev = em->bdev;
2996 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002997 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2998 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002999 free_extent_map(em);
3000 em = NULL;
3001
3002 /* we've found a hole, just zero and go on */
3003 if (block_start == EXTENT_MAP_HOLE) {
3004 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003005 struct extent_state *cached = NULL;
3006
Cong Wang7ac687d2011-11-25 23:14:28 +08003007 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003008 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003009 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003010 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003011
3012 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003013 &cached, GFP_NOFS);
3014 unlock_extent_cached(tree, cur, cur + iosize - 1,
3015 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003016 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003017 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003018 continue;
3019 }
3020 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003021 if (test_range_bit(tree, cur, cur_end,
3022 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003023 check_page_uptodate(tree, page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003024 if (!parent_locked)
3025 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003026 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003027 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003028 continue;
3029 }
Chris Mason70dec802008-01-29 09:59:12 -05003030 /* we have an inline extent but it didn't get marked up
3031 * to date. Error out
3032 */
3033 if (block_start == EXTENT_MAP_INLINE) {
3034 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003035 if (!parent_locked)
3036 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003037 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003038 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003039 continue;
3040 }
Chris Masond1310b22008-01-24 16:13:08 -05003041
Josef Bacikc8f2f242013-02-11 11:33:00 -05003042 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003043 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02003044 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04003045 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04003046 end_bio_extent_readpage, mirror_num,
3047 *bio_flags,
3048 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003049 if (!ret) {
3050 nr++;
3051 *bio_flags = this_bio_flag;
3052 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003053 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003054 if (!parent_locked)
3055 unlock_extent(tree, cur, cur + iosize - 1);
Josef Bacikedd33c92012-10-05 16:40:32 -04003056 }
Chris Masond1310b22008-01-24 16:13:08 -05003057 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003058 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003059 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003060out:
Chris Masond1310b22008-01-24 16:13:08 -05003061 if (!nr) {
3062 if (!PageError(page))
3063 SetPageUptodate(page);
3064 unlock_page(page);
3065 }
3066 return 0;
3067}
3068
Miao Xie99740902013-07-25 19:22:36 +08003069static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3070 struct page *pages[], int nr_pages,
3071 u64 start, u64 end,
3072 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003073 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003074 struct bio **bio, int mirror_num,
3075 unsigned long *bio_flags, int rw)
3076{
3077 struct inode *inode;
3078 struct btrfs_ordered_extent *ordered;
3079 int index;
3080
3081 inode = pages[0]->mapping->host;
3082 while (1) {
3083 lock_extent(tree, start, end);
3084 ordered = btrfs_lookup_ordered_range(inode, start,
3085 end - start + 1);
3086 if (!ordered)
3087 break;
3088 unlock_extent(tree, start, end);
3089 btrfs_start_ordered_extent(inode, ordered, 1);
3090 btrfs_put_ordered_extent(ordered);
3091 }
3092
3093 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003094 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
3095 mirror_num, bio_flags, rw);
Miao Xie99740902013-07-25 19:22:36 +08003096 page_cache_release(pages[index]);
3097 }
3098}
3099
3100static void __extent_readpages(struct extent_io_tree *tree,
3101 struct page *pages[],
3102 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003103 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003104 struct bio **bio, int mirror_num,
3105 unsigned long *bio_flags, int rw)
3106{
Stefan Behrens35a36212013-08-14 18:12:25 +02003107 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003108 u64 end = 0;
3109 u64 page_start;
3110 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003111 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003112
3113 for (index = 0; index < nr_pages; index++) {
3114 page_start = page_offset(pages[index]);
3115 if (!end) {
3116 start = page_start;
3117 end = start + PAGE_CACHE_SIZE - 1;
3118 first_index = index;
3119 } else if (end + 1 == page_start) {
3120 end += PAGE_CACHE_SIZE;
3121 } else {
3122 __do_contiguous_readpages(tree, &pages[first_index],
3123 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003124 end, get_extent, em_cached,
3125 bio, mirror_num, bio_flags,
3126 rw);
Miao Xie99740902013-07-25 19:22:36 +08003127 start = page_start;
3128 end = start + PAGE_CACHE_SIZE - 1;
3129 first_index = index;
3130 }
3131 }
3132
3133 if (end)
3134 __do_contiguous_readpages(tree, &pages[first_index],
3135 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003136 end, get_extent, em_cached, bio,
Miao Xie99740902013-07-25 19:22:36 +08003137 mirror_num, bio_flags, rw);
3138}
3139
3140static int __extent_read_full_page(struct extent_io_tree *tree,
3141 struct page *page,
3142 get_extent_t *get_extent,
3143 struct bio **bio, int mirror_num,
3144 unsigned long *bio_flags, int rw)
3145{
3146 struct inode *inode = page->mapping->host;
3147 struct btrfs_ordered_extent *ordered;
3148 u64 start = page_offset(page);
3149 u64 end = start + PAGE_CACHE_SIZE - 1;
3150 int ret;
3151
3152 while (1) {
3153 lock_extent(tree, start, end);
3154 ordered = btrfs_lookup_ordered_extent(inode, start);
3155 if (!ordered)
3156 break;
3157 unlock_extent(tree, start, end);
3158 btrfs_start_ordered_extent(inode, ordered, 1);
3159 btrfs_put_ordered_extent(ordered);
3160 }
3161
Miao Xie125bac012013-07-25 19:22:37 +08003162 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
3163 bio_flags, rw);
Miao Xie99740902013-07-25 19:22:36 +08003164 return ret;
3165}
3166
Chris Masond1310b22008-01-24 16:13:08 -05003167int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003168 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003169{
3170 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003171 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003172 int ret;
3173
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003174 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003175 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05003176 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003177 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003178 return ret;
3179}
Chris Masond1310b22008-01-24 16:13:08 -05003180
Mark Fasheh4b384312013-08-06 11:42:50 -07003181int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
3182 get_extent_t *get_extent, int mirror_num)
3183{
3184 struct bio *bio = NULL;
3185 unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED;
3186 int ret;
3187
3188 ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num,
3189 &bio_flags, READ);
3190 if (bio)
3191 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
3192 return ret;
3193}
3194
Chris Mason11c83492009-04-20 15:50:09 -04003195static noinline void update_nr_written(struct page *page,
3196 struct writeback_control *wbc,
3197 unsigned long nr_written)
3198{
3199 wbc->nr_to_write -= nr_written;
3200 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
3201 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
3202 page->mapping->writeback_index = page->index + nr_written;
3203}
3204
Chris Masond1310b22008-01-24 16:13:08 -05003205/*
Chris Mason40f76582014-05-21 13:35:51 -07003206 * helper for __extent_writepage, doing all of the delayed allocation setup.
3207 *
3208 * This returns 1 if our fill_delalloc function did all the work required
3209 * to write the page (copy into inline extent). In this case the IO has
3210 * been started and the page is already unlocked.
3211 *
3212 * This returns 0 if all went well (page still locked)
3213 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003214 */
Chris Mason40f76582014-05-21 13:35:51 -07003215static noinline_for_stack int writepage_delalloc(struct inode *inode,
3216 struct page *page, struct writeback_control *wbc,
3217 struct extent_page_data *epd,
3218 u64 delalloc_start,
3219 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003220{
Chris Mason40f76582014-05-21 13:35:51 -07003221 struct extent_io_tree *tree = epd->tree;
3222 u64 page_end = delalloc_start + PAGE_CACHE_SIZE - 1;
3223 u64 nr_delalloc;
3224 u64 delalloc_to_write = 0;
3225 u64 delalloc_end = 0;
3226 int ret;
3227 int page_started = 0;
3228
3229 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3230 return 0;
3231
3232 while (delalloc_end < page_end) {
3233 nr_delalloc = find_lock_delalloc_range(inode, tree,
3234 page,
3235 &delalloc_start,
3236 &delalloc_end,
3237 128 * 1024 * 1024);
3238 if (nr_delalloc == 0) {
3239 delalloc_start = delalloc_end + 1;
3240 continue;
3241 }
3242 ret = tree->ops->fill_delalloc(inode, page,
3243 delalloc_start,
3244 delalloc_end,
3245 &page_started,
3246 nr_written);
3247 /* File system has been set read-only */
3248 if (ret) {
3249 SetPageError(page);
3250 /* fill_delalloc should be return < 0 for error
3251 * but just in case, we use > 0 here meaning the
3252 * IO is started, so we don't want to return > 0
3253 * unless things are going well.
3254 */
3255 ret = ret < 0 ? ret : -EIO;
3256 goto done;
3257 }
3258 /*
3259 * delalloc_end is already one less than the total
3260 * length, so we don't subtract one from
3261 * PAGE_CACHE_SIZE
3262 */
3263 delalloc_to_write += (delalloc_end - delalloc_start +
3264 PAGE_CACHE_SIZE) >>
3265 PAGE_CACHE_SHIFT;
3266 delalloc_start = delalloc_end + 1;
3267 }
3268 if (wbc->nr_to_write < delalloc_to_write) {
3269 int thresh = 8192;
3270
3271 if (delalloc_to_write < thresh * 2)
3272 thresh = delalloc_to_write;
3273 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3274 thresh);
3275 }
3276
3277 /* did the fill delalloc function already unlock and start
3278 * the IO?
3279 */
3280 if (page_started) {
3281 /*
3282 * we've unlocked the page, so we can't update
3283 * the mapping's writeback index, just update
3284 * nr_to_write.
3285 */
3286 wbc->nr_to_write -= *nr_written;
3287 return 1;
3288 }
3289
3290 ret = 0;
3291
3292done:
3293 return ret;
3294}
3295
3296/*
3297 * helper for __extent_writepage. This calls the writepage start hooks,
3298 * and does the loop to map the page into extents and bios.
3299 *
3300 * We return 1 if the IO is started and the page is unlocked,
3301 * 0 if all went well (page still locked)
3302 * < 0 if there were errors (page still locked)
3303 */
3304static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3305 struct page *page,
3306 struct writeback_control *wbc,
3307 struct extent_page_data *epd,
3308 loff_t i_size,
3309 unsigned long nr_written,
3310 int write_flags, int *nr_ret)
3311{
Chris Masond1310b22008-01-24 16:13:08 -05003312 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003313 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003314 u64 page_end = start + PAGE_CACHE_SIZE - 1;
3315 u64 end;
3316 u64 cur = start;
3317 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003318 u64 block_start;
3319 u64 iosize;
3320 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04003321 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003322 struct extent_map *em;
3323 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003324 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003325 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003326 int ret = 0;
3327 int nr = 0;
3328 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003329
Chris Mason247e7432008-07-17 12:53:51 -04003330 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003331 ret = tree->ops->writepage_start_hook(page, start,
3332 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003333 if (ret) {
3334 /* Fixup worker will requeue */
3335 if (ret == -EBUSY)
3336 wbc->pages_skipped++;
3337 else
3338 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003339
Chris Mason11c83492009-04-20 15:50:09 -04003340 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003341 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003342 ret = 1;
Chris Mason11c83492009-04-20 15:50:09 -04003343 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003344 }
3345 }
3346
Chris Mason11c83492009-04-20 15:50:09 -04003347 /*
3348 * we don't want to touch the inode after unlocking the page,
3349 * so we update the mapping writeback index now
3350 */
3351 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003352
Chris Masond1310b22008-01-24 16:13:08 -05003353 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003354 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003355 if (tree->ops && tree->ops->writepage_end_io_hook)
3356 tree->ops->writepage_end_io_hook(page, start,
3357 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003358 goto done;
3359 }
3360
Chris Masond1310b22008-01-24 16:13:08 -05003361 blocksize = inode->i_sb->s_blocksize;
3362
3363 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003364 u64 em_end;
3365 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003366 if (tree->ops && tree->ops->writepage_end_io_hook)
3367 tree->ops->writepage_end_io_hook(page, cur,
3368 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003369 break;
3370 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003371 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003372 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003373 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003374 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003375 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003376 break;
3377 }
3378
3379 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003380 em_end = extent_map_end(em);
3381 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003382 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003383 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003384 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003385 sector = (em->block_start + extent_offset) >> 9;
3386 bdev = em->bdev;
3387 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003388 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003389 free_extent_map(em);
3390 em = NULL;
3391
Chris Masonc8b97812008-10-29 14:49:59 -04003392 /*
3393 * compressed and inline extents are written through other
3394 * paths in the FS
3395 */
3396 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003397 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003398 /*
3399 * end_io notification does not happen here for
3400 * compressed extents
3401 */
3402 if (!compressed && tree->ops &&
3403 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003404 tree->ops->writepage_end_io_hook(page, cur,
3405 cur + iosize - 1,
3406 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003407 else if (compressed) {
3408 /* we don't want to end_page_writeback on
3409 * a compressed extent. this happens
3410 * elsewhere
3411 */
3412 nr++;
3413 }
3414
3415 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003416 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003417 continue;
3418 }
Chris Masonc8b97812008-10-29 14:49:59 -04003419
Chris Masond1310b22008-01-24 16:13:08 -05003420 if (tree->ops && tree->ops->writepage_io_hook) {
3421 ret = tree->ops->writepage_io_hook(page, cur,
3422 cur + iosize - 1);
3423 } else {
3424 ret = 0;
3425 }
Chris Mason1259ab72008-05-12 13:39:03 -04003426 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003427 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003428 } else {
Chris Mason40f76582014-05-21 13:35:51 -07003429 unsigned long max_nr = (i_size >> PAGE_CACHE_SHIFT) + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003430
Chris Masond1310b22008-01-24 16:13:08 -05003431 set_range_writeback(tree, cur, cur + iosize - 1);
3432 if (!PageWriteback(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003433 btrfs_err(BTRFS_I(inode)->root->fs_info,
3434 "page %lu not writeback, cur %llu end %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003435 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003436 }
3437
Chris Masonffbd5172009-04-20 15:50:09 -04003438 ret = submit_extent_page(write_flags, tree, page,
3439 sector, iosize, pg_offset,
3440 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003441 end_bio_extent_writepage,
3442 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003443 if (ret)
3444 SetPageError(page);
3445 }
3446 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003447 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003448 nr++;
3449 }
3450done:
Chris Mason40f76582014-05-21 13:35:51 -07003451 *nr_ret = nr;
Chris Mason771ed682008-11-06 22:02:51 -05003452
Chris Mason11c83492009-04-20 15:50:09 -04003453done_unlocked:
3454
Chris Mason2c64c532009-09-02 15:04:12 -04003455 /* drop our reference on any cached states */
3456 free_extent_state(cached_state);
Chris Mason40f76582014-05-21 13:35:51 -07003457 return ret;
3458}
3459
3460/*
3461 * the writepage semantics are similar to regular writepage. extent
3462 * records are inserted to lock ranges in the tree, and as dirty areas
3463 * are found, they are marked writeback. Then the lock bits are removed
3464 * and the end_io handler clears the writeback ranges
3465 */
3466static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3467 void *data)
3468{
3469 struct inode *inode = page->mapping->host;
3470 struct extent_page_data *epd = data;
3471 u64 start = page_offset(page);
3472 u64 page_end = start + PAGE_CACHE_SIZE - 1;
3473 int ret;
3474 int nr = 0;
3475 size_t pg_offset = 0;
3476 loff_t i_size = i_size_read(inode);
3477 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
3478 int write_flags;
3479 unsigned long nr_written = 0;
3480
3481 if (wbc->sync_mode == WB_SYNC_ALL)
3482 write_flags = WRITE_SYNC;
3483 else
3484 write_flags = WRITE;
3485
3486 trace___extent_writepage(page, inode, wbc);
3487
3488 WARN_ON(!PageLocked(page));
3489
3490 ClearPageError(page);
3491
3492 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
3493 if (page->index > end_index ||
3494 (page->index == end_index && !pg_offset)) {
3495 page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
3496 unlock_page(page);
3497 return 0;
3498 }
3499
3500 if (page->index == end_index) {
3501 char *userpage;
3502
3503 userpage = kmap_atomic(page);
3504 memset(userpage + pg_offset, 0,
3505 PAGE_CACHE_SIZE - pg_offset);
3506 kunmap_atomic(userpage);
3507 flush_dcache_page(page);
3508 }
3509
3510 pg_offset = 0;
3511
3512 set_page_extent_mapped(page);
3513
3514 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3515 if (ret == 1)
3516 goto done_unlocked;
3517 if (ret)
3518 goto done;
3519
3520 ret = __extent_writepage_io(inode, page, wbc, epd,
3521 i_size, nr_written, write_flags, &nr);
3522 if (ret == 1)
3523 goto done_unlocked;
3524
3525done:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003526 if (nr == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003527 /* make sure the mapping tag for page dirty gets cleared */
Chris Mason771ed682008-11-06 22:02:51 -05003528 set_page_writeback(page);
3529 end_page_writeback(page);
3530 }
Filipe Manana61391d52014-05-09 17:17:40 +01003531 if (PageError(page)) {
3532 ret = ret < 0 ? ret : -EIO;
3533 end_extent_writepage(page, ret, start, page_end);
3534 }
Christoph Hellwigb2950862008-12-02 09:54:17 -05003535 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003536 return ret;
Chris Mason4bef0842008-09-08 11:18:08 -04003537
3538done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003539 return 0;
3540}
3541
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003542void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003543{
NeilBrown74316202014-07-07 15:16:04 +10003544 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3545 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003546}
3547
Chris Mason0e378df2014-05-19 20:55:27 -07003548static noinline_for_stack int
3549lock_extent_buffer_for_io(struct extent_buffer *eb,
3550 struct btrfs_fs_info *fs_info,
3551 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003552{
3553 unsigned long i, num_pages;
3554 int flush = 0;
3555 int ret = 0;
3556
3557 if (!btrfs_try_tree_write_lock(eb)) {
3558 flush = 1;
3559 flush_write_bio(epd);
3560 btrfs_tree_lock(eb);
3561 }
3562
3563 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3564 btrfs_tree_unlock(eb);
3565 if (!epd->sync_io)
3566 return 0;
3567 if (!flush) {
3568 flush_write_bio(epd);
3569 flush = 1;
3570 }
Chris Masona098d8e2012-03-21 12:09:56 -04003571 while (1) {
3572 wait_on_extent_buffer_writeback(eb);
3573 btrfs_tree_lock(eb);
3574 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3575 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003576 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003577 }
3578 }
3579
Josef Bacik51561ff2012-07-20 16:25:24 -04003580 /*
3581 * We need to do this to prevent races in people who check if the eb is
3582 * under IO since we can end up having no IO bits set for a short period
3583 * of time.
3584 */
3585 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003586 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3587 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003588 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003589 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003590 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3591 -eb->len,
3592 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003593 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003594 } else {
3595 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003596 }
3597
3598 btrfs_tree_unlock(eb);
3599
3600 if (!ret)
3601 return ret;
3602
3603 num_pages = num_extent_pages(eb->start, eb->len);
3604 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003605 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003606
3607 if (!trylock_page(p)) {
3608 if (!flush) {
3609 flush_write_bio(epd);
3610 flush = 1;
3611 }
3612 lock_page(p);
3613 }
3614 }
3615
3616 return ret;
3617}
3618
3619static void end_extent_buffer_writeback(struct extent_buffer *eb)
3620{
3621 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003622 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003623 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3624}
3625
Filipe Manana656f30d2014-09-26 12:25:56 +01003626static void set_btree_ioerr(struct page *page)
3627{
3628 struct extent_buffer *eb = (struct extent_buffer *)page->private;
3629 struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode);
3630
3631 SetPageError(page);
3632 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3633 return;
3634
3635 /*
3636 * If writeback for a btree extent that doesn't belong to a log tree
3637 * failed, increment the counter transaction->eb_write_errors.
3638 * We do this because while the transaction is running and before it's
3639 * committing (when we call filemap_fdata[write|wait]_range against
3640 * the btree inode), we might have
3641 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3642 * returns an error or an error happens during writeback, when we're
3643 * committing the transaction we wouldn't know about it, since the pages
3644 * can be no longer dirty nor marked anymore for writeback (if a
3645 * subsequent modification to the extent buffer didn't happen before the
3646 * transaction commit), which makes filemap_fdata[write|wait]_range not
3647 * able to find the pages tagged with SetPageError at transaction
3648 * commit time. So if this happens we must abort the transaction,
3649 * otherwise we commit a super block with btree roots that point to
3650 * btree nodes/leafs whose content on disk is invalid - either garbage
3651 * or the content of some node/leaf from a past generation that got
3652 * cowed or deleted and is no longer valid.
3653 *
3654 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3655 * not be enough - we need to distinguish between log tree extents vs
3656 * non-log tree extents, and the next filemap_fdatawait_range() call
3657 * will catch and clear such errors in the mapping - and that call might
3658 * be from a log sync and not from a transaction commit. Also, checking
3659 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3660 * not done and would not be reliable - the eb might have been released
3661 * from memory and reading it back again means that flag would not be
3662 * set (since it's a runtime flag, not persisted on disk).
3663 *
3664 * Using the flags below in the btree inode also makes us achieve the
3665 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3666 * writeback for all dirty pages and before filemap_fdatawait_range()
3667 * is called, the writeback for all dirty pages had already finished
3668 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3669 * filemap_fdatawait_range() would return success, as it could not know
3670 * that writeback errors happened (the pages were no longer tagged for
3671 * writeback).
3672 */
3673 switch (eb->log_index) {
3674 case -1:
3675 set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags);
3676 break;
3677 case 0:
3678 set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
3679 break;
3680 case 1:
3681 set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
3682 break;
3683 default:
3684 BUG(); /* unexpected, logic error */
3685 }
3686}
3687
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003688static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3689{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003690 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003692 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003693
Kent Overstreet2c30c712013-11-07 12:20:26 -08003694 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003695 struct page *page = bvec->bv_page;
3696
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003697 eb = (struct extent_buffer *)page->private;
3698 BUG_ON(!eb);
3699 done = atomic_dec_and_test(&eb->io_pages);
3700
Filipe Manana656f30d2014-09-26 12:25:56 +01003701 if (err || test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003702 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003703 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003704 }
3705
3706 end_page_writeback(page);
3707
3708 if (!done)
3709 continue;
3710
3711 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003712 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003713
3714 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003715}
3716
Chris Mason0e378df2014-05-19 20:55:27 -07003717static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003718 struct btrfs_fs_info *fs_info,
3719 struct writeback_control *wbc,
3720 struct extent_page_data *epd)
3721{
3722 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003723 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003724 u64 offset = eb->start;
3725 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003726 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003727 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003728 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003729
Filipe Manana656f30d2014-09-26 12:25:56 +01003730 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003731 num_pages = num_extent_pages(eb->start, eb->len);
3732 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003733 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3734 bio_flags = EXTENT_BIO_TREE_LOG;
3735
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003736 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003737 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003738
3739 clear_page_dirty_for_io(p);
3740 set_page_writeback(p);
Josef Bacikf28491e2013-12-16 13:24:27 -05003741 ret = submit_extent_page(rw, tree, p, offset >> 9,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003742 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3743 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003744 0, epd->bio_flags, bio_flags);
3745 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003746 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003747 set_btree_ioerr(p);
Filipe Manana55e3bd22014-09-22 17:41:04 +01003748 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003749 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3750 end_extent_buffer_writeback(eb);
3751 ret = -EIO;
3752 break;
3753 }
3754 offset += PAGE_CACHE_SIZE;
3755 update_nr_written(p, wbc, 1);
3756 unlock_page(p);
3757 }
3758
3759 if (unlikely(ret)) {
3760 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003761 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003762 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003763 unlock_page(p);
3764 }
3765 }
3766
3767 return ret;
3768}
3769
3770int btree_write_cache_pages(struct address_space *mapping,
3771 struct writeback_control *wbc)
3772{
3773 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3774 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3775 struct extent_buffer *eb, *prev_eb = NULL;
3776 struct extent_page_data epd = {
3777 .bio = NULL,
3778 .tree = tree,
3779 .extent_locked = 0,
3780 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003781 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003782 };
3783 int ret = 0;
3784 int done = 0;
3785 int nr_to_write_done = 0;
3786 struct pagevec pvec;
3787 int nr_pages;
3788 pgoff_t index;
3789 pgoff_t end; /* Inclusive */
3790 int scanned = 0;
3791 int tag;
3792
3793 pagevec_init(&pvec, 0);
3794 if (wbc->range_cyclic) {
3795 index = mapping->writeback_index; /* Start from prev offset */
3796 end = -1;
3797 } else {
3798 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3799 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3800 scanned = 1;
3801 }
3802 if (wbc->sync_mode == WB_SYNC_ALL)
3803 tag = PAGECACHE_TAG_TOWRITE;
3804 else
3805 tag = PAGECACHE_TAG_DIRTY;
3806retry:
3807 if (wbc->sync_mode == WB_SYNC_ALL)
3808 tag_pages_for_writeback(mapping, index, end);
3809 while (!done && !nr_to_write_done && (index <= end) &&
3810 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3811 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3812 unsigned i;
3813
3814 scanned = 1;
3815 for (i = 0; i < nr_pages; i++) {
3816 struct page *page = pvec.pages[i];
3817
3818 if (!PagePrivate(page))
3819 continue;
3820
3821 if (!wbc->range_cyclic && page->index > end) {
3822 done = 1;
3823 break;
3824 }
3825
Josef Bacikb5bae262012-09-14 13:43:01 -04003826 spin_lock(&mapping->private_lock);
3827 if (!PagePrivate(page)) {
3828 spin_unlock(&mapping->private_lock);
3829 continue;
3830 }
3831
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003832 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003833
3834 /*
3835 * Shouldn't happen and normally this would be a BUG_ON
3836 * but no sense in crashing the users box for something
3837 * we can survive anyway.
3838 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303839 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003840 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003841 continue;
3842 }
3843
Josef Bacikb5bae262012-09-14 13:43:01 -04003844 if (eb == prev_eb) {
3845 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003846 continue;
3847 }
3848
Josef Bacikb5bae262012-09-14 13:43:01 -04003849 ret = atomic_inc_not_zero(&eb->refs);
3850 spin_unlock(&mapping->private_lock);
3851 if (!ret)
3852 continue;
3853
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003854 prev_eb = eb;
3855 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3856 if (!ret) {
3857 free_extent_buffer(eb);
3858 continue;
3859 }
3860
3861 ret = write_one_eb(eb, fs_info, wbc, &epd);
3862 if (ret) {
3863 done = 1;
3864 free_extent_buffer(eb);
3865 break;
3866 }
3867 free_extent_buffer(eb);
3868
3869 /*
3870 * the filesystem may choose to bump up nr_to_write.
3871 * We have to make sure to honor the new nr_to_write
3872 * at any time
3873 */
3874 nr_to_write_done = wbc->nr_to_write <= 0;
3875 }
3876 pagevec_release(&pvec);
3877 cond_resched();
3878 }
3879 if (!scanned && !done) {
3880 /*
3881 * We hit the last page and there is more work to be done: wrap
3882 * back to the start of the file
3883 */
3884 scanned = 1;
3885 index = 0;
3886 goto retry;
3887 }
3888 flush_write_bio(&epd);
3889 return ret;
3890}
3891
Chris Masond1310b22008-01-24 16:13:08 -05003892/**
3893 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3894 * @mapping: address space structure to write
3895 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3896 * @writepage: function called for each page
3897 * @data: data passed to writepage function
3898 *
3899 * If a page is already under I/O, write_cache_pages() skips it, even
3900 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3901 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3902 * and msync() need to guarantee that all the data which was dirty at the time
3903 * the call was made get new I/O started against them. If wbc->sync_mode is
3904 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3905 * existing IO to complete.
3906 */
Chris Mason4bef0842008-09-08 11:18:08 -04003907static int extent_write_cache_pages(struct extent_io_tree *tree,
3908 struct address_space *mapping,
3909 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003910 writepage_t writepage, void *data,
3911 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003912{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003913 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003914 int ret = 0;
3915 int done = 0;
Filipe Manana61391d52014-05-09 17:17:40 +01003916 int err = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003917 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003918 struct pagevec pvec;
3919 int nr_pages;
3920 pgoff_t index;
3921 pgoff_t end; /* Inclusive */
3922 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003923 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003924
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003925 /*
3926 * We have to hold onto the inode so that ordered extents can do their
3927 * work when the IO finishes. The alternative to this is failing to add
3928 * an ordered extent if the igrab() fails there and that is a huge pain
3929 * to deal with, so instead just hold onto the inode throughout the
3930 * writepages operation. If it fails here we are freeing up the inode
3931 * anyway and we'd rather not waste our time writing out stuff that is
3932 * going to be truncated anyway.
3933 */
3934 if (!igrab(inode))
3935 return 0;
3936
Chris Masond1310b22008-01-24 16:13:08 -05003937 pagevec_init(&pvec, 0);
3938 if (wbc->range_cyclic) {
3939 index = mapping->writeback_index; /* Start from prev offset */
3940 end = -1;
3941 } else {
3942 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3943 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003944 scanned = 1;
3945 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003946 if (wbc->sync_mode == WB_SYNC_ALL)
3947 tag = PAGECACHE_TAG_TOWRITE;
3948 else
3949 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003950retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003951 if (wbc->sync_mode == WB_SYNC_ALL)
3952 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003953 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003954 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3955 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003956 unsigned i;
3957
3958 scanned = 1;
3959 for (i = 0; i < nr_pages; i++) {
3960 struct page *page = pvec.pages[i];
3961
3962 /*
3963 * At this point we hold neither mapping->tree_lock nor
3964 * lock on the page itself: the page may be truncated or
3965 * invalidated (changing page->mapping to NULL), or even
3966 * swizzled back from swapper_space to tmpfs file
3967 * mapping
3968 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003969 if (!trylock_page(page)) {
3970 flush_fn(data);
3971 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003972 }
Chris Masond1310b22008-01-24 16:13:08 -05003973
3974 if (unlikely(page->mapping != mapping)) {
3975 unlock_page(page);
3976 continue;
3977 }
3978
3979 if (!wbc->range_cyclic && page->index > end) {
3980 done = 1;
3981 unlock_page(page);
3982 continue;
3983 }
3984
Chris Masond2c3f4f2008-11-19 12:44:22 -05003985 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003986 if (PageWriteback(page))
3987 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003988 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003989 }
Chris Masond1310b22008-01-24 16:13:08 -05003990
3991 if (PageWriteback(page) ||
3992 !clear_page_dirty_for_io(page)) {
3993 unlock_page(page);
3994 continue;
3995 }
3996
3997 ret = (*writepage)(page, wbc, data);
3998
3999 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4000 unlock_page(page);
4001 ret = 0;
4002 }
Filipe Manana61391d52014-05-09 17:17:40 +01004003 if (!err && ret < 0)
4004 err = ret;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004005
4006 /*
4007 * the filesystem may choose to bump up nr_to_write.
4008 * We have to make sure to honor the new nr_to_write
4009 * at any time
4010 */
4011 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004012 }
4013 pagevec_release(&pvec);
4014 cond_resched();
4015 }
Filipe Manana61391d52014-05-09 17:17:40 +01004016 if (!scanned && !done && !err) {
Chris Masond1310b22008-01-24 16:13:08 -05004017 /*
4018 * We hit the last page and there is more work to be done: wrap
4019 * back to the start of the file
4020 */
4021 scanned = 1;
4022 index = 0;
4023 goto retry;
4024 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004025 btrfs_add_delayed_iput(inode);
Filipe Manana61391d52014-05-09 17:17:40 +01004026 return err;
Chris Masond1310b22008-01-24 16:13:08 -05004027}
Chris Masond1310b22008-01-24 16:13:08 -05004028
Chris Masonffbd5172009-04-20 15:50:09 -04004029static void flush_epd_write_bio(struct extent_page_data *epd)
4030{
4031 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004032 int rw = WRITE;
4033 int ret;
4034
Chris Masonffbd5172009-04-20 15:50:09 -04004035 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04004036 rw = WRITE_SYNC;
4037
Josef Bacikde0022b2012-09-25 14:25:58 -04004038 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004039 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004040 epd->bio = NULL;
4041 }
4042}
4043
Chris Masond2c3f4f2008-11-19 12:44:22 -05004044static noinline void flush_write_bio(void *data)
4045{
4046 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004047 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004048}
4049
Chris Masond1310b22008-01-24 16:13:08 -05004050int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4051 get_extent_t *get_extent,
4052 struct writeback_control *wbc)
4053{
4054 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004055 struct extent_page_data epd = {
4056 .bio = NULL,
4057 .tree = tree,
4058 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004059 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004060 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004061 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004062 };
Chris Masond1310b22008-01-24 16:13:08 -05004063
Chris Masond1310b22008-01-24 16:13:08 -05004064 ret = __extent_writepage(page, wbc, &epd);
4065
Chris Masonffbd5172009-04-20 15:50:09 -04004066 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004067 return ret;
4068}
Chris Masond1310b22008-01-24 16:13:08 -05004069
Chris Mason771ed682008-11-06 22:02:51 -05004070int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4071 u64 start, u64 end, get_extent_t *get_extent,
4072 int mode)
4073{
4074 int ret = 0;
4075 struct address_space *mapping = inode->i_mapping;
4076 struct page *page;
4077 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
4078 PAGE_CACHE_SHIFT;
4079
4080 struct extent_page_data epd = {
4081 .bio = NULL,
4082 .tree = tree,
4083 .get_extent = get_extent,
4084 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004085 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004086 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05004087 };
4088 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004089 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004090 .nr_to_write = nr_pages * 2,
4091 .range_start = start,
4092 .range_end = end + 1,
4093 };
4094
Chris Masond3977122009-01-05 21:25:51 -05004095 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05004096 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
4097 if (clear_page_dirty_for_io(page))
4098 ret = __extent_writepage(page, &wbc_writepages, &epd);
4099 else {
4100 if (tree->ops && tree->ops->writepage_end_io_hook)
4101 tree->ops->writepage_end_io_hook(page, start,
4102 start + PAGE_CACHE_SIZE - 1,
4103 NULL, 1);
4104 unlock_page(page);
4105 }
4106 page_cache_release(page);
4107 start += PAGE_CACHE_SIZE;
4108 }
4109
Chris Masonffbd5172009-04-20 15:50:09 -04004110 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004111 return ret;
4112}
Chris Masond1310b22008-01-24 16:13:08 -05004113
4114int extent_writepages(struct extent_io_tree *tree,
4115 struct address_space *mapping,
4116 get_extent_t *get_extent,
4117 struct writeback_control *wbc)
4118{
4119 int ret = 0;
4120 struct extent_page_data epd = {
4121 .bio = NULL,
4122 .tree = tree,
4123 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004124 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004125 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004126 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004127 };
4128
Chris Mason4bef0842008-09-08 11:18:08 -04004129 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004130 __extent_writepage, &epd,
4131 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004132 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004133 return ret;
4134}
Chris Masond1310b22008-01-24 16:13:08 -05004135
4136int extent_readpages(struct extent_io_tree *tree,
4137 struct address_space *mapping,
4138 struct list_head *pages, unsigned nr_pages,
4139 get_extent_t get_extent)
4140{
4141 struct bio *bio = NULL;
4142 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004143 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004144 struct page *pagepool[16];
4145 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004146 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004147 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004148
Chris Masond1310b22008-01-24 16:13:08 -05004149 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004150 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004151
4152 prefetchw(&page->flags);
4153 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004154 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04004155 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06004156 page_cache_release(page);
4157 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004158 }
Liu Bo67c96842012-07-20 21:43:09 -06004159
4160 pagepool[nr++] = page;
4161 if (nr < ARRAY_SIZE(pagepool))
4162 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004163 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Miao Xie99740902013-07-25 19:22:36 +08004164 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06004165 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004166 }
Miao Xie99740902013-07-25 19:22:36 +08004167 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004168 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Miao Xie99740902013-07-25 19:22:36 +08004169 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06004170
Miao Xie125bac012013-07-25 19:22:37 +08004171 if (em_cached)
4172 free_extent_map(em_cached);
4173
Chris Masond1310b22008-01-24 16:13:08 -05004174 BUG_ON(!list_empty(pages));
4175 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004177 return 0;
4178}
Chris Masond1310b22008-01-24 16:13:08 -05004179
4180/*
4181 * basic invalidatepage code, this waits on any locked or writeback
4182 * ranges corresponding to the page, and then deletes any extent state
4183 * records from the tree
4184 */
4185int extent_invalidatepage(struct extent_io_tree *tree,
4186 struct page *page, unsigned long offset)
4187{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004188 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004189 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05004190 u64 end = start + PAGE_CACHE_SIZE - 1;
4191 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4192
Qu Wenruofda28322013-02-26 08:10:22 +00004193 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004194 if (start > end)
4195 return 0;
4196
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004197 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004198 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004199 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004200 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4201 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004202 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004203 return 0;
4204}
Chris Masond1310b22008-01-24 16:13:08 -05004205
4206/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004207 * a helper for releasepage, this tests for areas of the page that
4208 * are locked or under IO and drops the related state bits if it is safe
4209 * to drop the page.
4210 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004211static int try_release_extent_state(struct extent_map_tree *map,
4212 struct extent_io_tree *tree,
4213 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004214{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004215 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04004216 u64 end = start + PAGE_CACHE_SIZE - 1;
4217 int ret = 1;
4218
Chris Mason211f90e2008-07-18 11:56:15 -04004219 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004220 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004221 ret = 0;
4222 else {
4223 if ((mask & GFP_NOFS) == GFP_NOFS)
4224 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04004225 /*
4226 * at this point we can safely clear everything except the
4227 * locked bit and the nodatasum bit
4228 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004229 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004230 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4231 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004232
4233 /* if clear_extent_bit failed for enomem reasons,
4234 * we can't allow the release to continue.
4235 */
4236 if (ret < 0)
4237 ret = 0;
4238 else
4239 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004240 }
4241 return ret;
4242}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004243
4244/*
Chris Masond1310b22008-01-24 16:13:08 -05004245 * a helper for releasepage. As long as there are no locked extents
4246 * in the range corresponding to the page, both state records and extent
4247 * map records are removed
4248 */
4249int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004250 struct extent_io_tree *tree, struct page *page,
4251 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004252{
4253 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004254 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05004255 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004256
Chris Mason70dec802008-01-29 09:59:12 -05004257 if ((mask & __GFP_WAIT) &&
4258 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05004259 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004260 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004261 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004262 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004263 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004264 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004265 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004266 break;
4267 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004268 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4269 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004270 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004271 free_extent_map(em);
4272 break;
4273 }
4274 if (!test_range_bit(tree, em->start,
4275 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004276 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004277 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004278 remove_extent_mapping(map, em);
4279 /* once for the rb tree */
4280 free_extent_map(em);
4281 }
4282 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004283 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004284
4285 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004286 free_extent_map(em);
4287 }
Chris Masond1310b22008-01-24 16:13:08 -05004288 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004289 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004290}
Chris Masond1310b22008-01-24 16:13:08 -05004291
Chris Masonec29ed52011-02-23 16:23:20 -05004292/*
4293 * helper function for fiemap, which doesn't want to see any holes.
4294 * This maps until we find something past 'last'
4295 */
4296static struct extent_map *get_extent_skip_holes(struct inode *inode,
4297 u64 offset,
4298 u64 last,
4299 get_extent_t *get_extent)
4300{
4301 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
4302 struct extent_map *em;
4303 u64 len;
4304
4305 if (offset >= last)
4306 return NULL;
4307
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304308 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004309 len = last - offset;
4310 if (len == 0)
4311 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004312 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05004313 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004314 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004315 return em;
4316
4317 /* if this isn't a hole return it */
4318 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4319 em->block_start != EXTENT_MAP_HOLE) {
4320 return em;
4321 }
4322
4323 /* this is a hole, advance to the next extent */
4324 offset = extent_map_end(em);
4325 free_extent_map(em);
4326 if (offset >= last)
4327 break;
4328 }
4329 return NULL;
4330}
4331
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004332int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4333 __u64 start, __u64 len, get_extent_t *get_extent)
4334{
Josef Bacik975f84f2010-11-23 19:36:57 +00004335 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004336 u64 off = start;
4337 u64 max = start + len;
4338 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004339 u32 found_type;
4340 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004341 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004342 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004343 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004344 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004345 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004346 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004347 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004348 struct btrfs_root *root = BTRFS_I(inode)->root;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004349 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004350 u64 em_start = 0;
4351 u64 em_len = 0;
4352 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004353
4354 if (len == 0)
4355 return -EINVAL;
4356
Josef Bacik975f84f2010-11-23 19:36:57 +00004357 path = btrfs_alloc_path();
4358 if (!path)
4359 return -ENOMEM;
4360 path->leave_spinning = 1;
4361
Qu Wenruo2c919432014-07-18 09:55:43 +08004362 start = round_down(start, BTRFS_I(inode)->root->sectorsize);
4363 len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004364
Chris Masonec29ed52011-02-23 16:23:20 -05004365 /*
4366 * lookup the last file extent. We're not using i_size here
4367 * because there might be preallocation past i_size
4368 */
Josef Bacikdc046b12014-09-10 16:20:45 -04004369 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
4370 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004371 if (ret < 0) {
4372 btrfs_free_path(path);
4373 return ret;
4374 }
4375 WARN_ON(!ret);
4376 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004377 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004378 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004379
Chris Masonec29ed52011-02-23 16:23:20 -05004380 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08004381 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004382 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004383 /* have to trust i_size as the end */
4384 last = (u64)-1;
4385 last_for_get_extent = isize;
4386 } else {
4387 /*
4388 * remember the start of the last extent. There are a
4389 * bunch of different factors that go into the length of the
4390 * extent, so its much less complex to remember where it started
4391 */
4392 last = found_key.offset;
4393 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004394 }
Liu Bofe09e162013-09-22 12:54:23 +08004395 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004396
Chris Masonec29ed52011-02-23 16:23:20 -05004397 /*
4398 * we might have some extents allocated but more delalloc past those
4399 * extents. so, we trust isize unless the start of the last extent is
4400 * beyond isize
4401 */
4402 if (last < isize) {
4403 last = (u64)-1;
4404 last_for_get_extent = isize;
4405 }
4406
Liu Boa52f4cd2013-05-01 16:23:41 +00004407 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004408 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004409
Josef Bacik4d479cf2011-11-17 11:34:31 -05004410 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004411 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004412 if (!em)
4413 goto out;
4414 if (IS_ERR(em)) {
4415 ret = PTR_ERR(em);
4416 goto out;
4417 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004418
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004419 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004420 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004421
Chris Masonea8efc72011-03-08 11:54:40 -05004422 /* break if the extent we found is outside the range */
4423 if (em->start >= max || extent_map_end(em) < off)
4424 break;
4425
4426 /*
4427 * get_extent may return an extent that starts before our
4428 * requested range. We have to make sure the ranges
4429 * we return to fiemap always move forward and don't
4430 * overlap, so adjust the offsets here
4431 */
4432 em_start = max(em->start, off);
4433
4434 /*
4435 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004436 * for adjusting the disk offset below. Only do this if the
4437 * extent isn't compressed since our in ram offset may be past
4438 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004439 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004440 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4441 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004442 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004443 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004444 disko = 0;
4445 flags = 0;
4446
Chris Masonea8efc72011-03-08 11:54:40 -05004447 /*
4448 * bump off for our next call to get_extent
4449 */
4450 off = extent_map_end(em);
4451 if (off >= max)
4452 end = 1;
4453
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004454 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004455 end = 1;
4456 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004457 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004458 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4459 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004460 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004461 flags |= (FIEMAP_EXTENT_DELALLOC |
4462 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004463 } else if (fieinfo->fi_extents_max) {
4464 u64 bytenr = em->block_start -
4465 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004466
Chris Masonea8efc72011-03-08 11:54:40 -05004467 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004468
4469 /*
4470 * As btrfs supports shared space, this information
4471 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004472 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4473 * then we're just getting a count and we can skip the
4474 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004475 */
Josef Bacikdc046b12014-09-10 16:20:45 -04004476 ret = btrfs_check_shared(NULL, root->fs_info,
4477 root->objectid,
4478 btrfs_ino(inode), bytenr);
4479 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004480 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004481 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004482 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004483 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004484 }
4485 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4486 flags |= FIEMAP_EXTENT_ENCODED;
4487
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004488 free_extent_map(em);
4489 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004490 if ((em_start >= last) || em_len == (u64)-1 ||
4491 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004492 flags |= FIEMAP_EXTENT_LAST;
4493 end = 1;
4494 }
4495
Chris Masonec29ed52011-02-23 16:23:20 -05004496 /* now scan forward to see if this is really the last extent. */
4497 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4498 get_extent);
4499 if (IS_ERR(em)) {
4500 ret = PTR_ERR(em);
4501 goto out;
4502 }
4503 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004504 flags |= FIEMAP_EXTENT_LAST;
4505 end = 1;
4506 }
Chris Masonec29ed52011-02-23 16:23:20 -05004507 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4508 em_len, flags);
4509 if (ret)
4510 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004511 }
4512out_free:
4513 free_extent_map(em);
4514out:
Liu Bofe09e162013-09-22 12:54:23 +08004515 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004516 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004517 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004518 return ret;
4519}
4520
Chris Mason727011e2010-08-06 13:21:20 -04004521static void __free_extent_buffer(struct extent_buffer *eb)
4522{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004523 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004524 kmem_cache_free(extent_buffer_cache, eb);
4525}
4526
Josef Bacika26e8c92014-03-28 17:07:27 -04004527int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004528{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004529 return (atomic_read(&eb->io_pages) ||
4530 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4531 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004532}
4533
Miao Xie897ca6e92010-10-26 20:57:29 -04004534/*
4535 * Helper for releasing extent buffer page.
4536 */
David Sterbaa50924e2014-07-31 00:51:36 +02004537static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e92010-10-26 20:57:29 -04004538{
4539 unsigned long index;
4540 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004541 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004542
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004543 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004544
David Sterbaa50924e2014-07-31 00:51:36 +02004545 index = num_extent_pages(eb->start, eb->len);
4546 if (index == 0)
Miao Xie897ca6e92010-10-26 20:57:29 -04004547 return;
4548
4549 do {
4550 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004551 page = eb->pages[index];
Jan Schmidt815a51c2012-05-16 17:00:02 +02004552 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004553 spin_lock(&page->mapping->private_lock);
4554 /*
4555 * We do this since we'll remove the pages after we've
4556 * removed the eb from the radix tree, so we could race
4557 * and have this page now attached to the new eb. So
4558 * only clear page_private if it's still connected to
4559 * this eb.
4560 */
4561 if (PagePrivate(page) &&
4562 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004563 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004564 BUG_ON(PageDirty(page));
4565 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004566 /*
4567 * We need to make sure we haven't be attached
4568 * to a new eb.
4569 */
4570 ClearPagePrivate(page);
4571 set_page_private(page, 0);
4572 /* One for the page private */
4573 page_cache_release(page);
4574 }
4575 spin_unlock(&page->mapping->private_lock);
4576
Jan Schmidt815a51c2012-05-16 17:00:02 +02004577 }
4578 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004579 /* One for when we alloced the page */
Miao Xie897ca6e92010-10-26 20:57:29 -04004580 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004581 }
David Sterbaa50924e2014-07-31 00:51:36 +02004582 } while (index != 0);
Miao Xie897ca6e92010-10-26 20:57:29 -04004583}
4584
4585/*
4586 * Helper for releasing the extent buffer.
4587 */
4588static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4589{
David Sterbaa50924e2014-07-31 00:51:36 +02004590 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e92010-10-26 20:57:29 -04004591 __free_extent_buffer(eb);
4592}
4593
Josef Bacikf28491e2013-12-16 13:24:27 -05004594static struct extent_buffer *
4595__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
4596 unsigned long len, gfp_t mask)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004597{
4598 struct extent_buffer *eb = NULL;
4599
4600 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4601 if (eb == NULL)
4602 return NULL;
4603 eb->start = start;
4604 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004605 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004606 eb->bflags = 0;
4607 rwlock_init(&eb->lock);
4608 atomic_set(&eb->write_locks, 0);
4609 atomic_set(&eb->read_locks, 0);
4610 atomic_set(&eb->blocking_readers, 0);
4611 atomic_set(&eb->blocking_writers, 0);
4612 atomic_set(&eb->spinning_readers, 0);
4613 atomic_set(&eb->spinning_writers, 0);
4614 eb->lock_nested = 0;
4615 init_waitqueue_head(&eb->write_lock_wq);
4616 init_waitqueue_head(&eb->read_lock_wq);
4617
4618 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4619
4620 spin_lock_init(&eb->refs_lock);
4621 atomic_set(&eb->refs, 1);
4622 atomic_set(&eb->io_pages, 0);
4623
4624 /*
4625 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4626 */
4627 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4628 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4629 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4630
4631 return eb;
4632}
4633
4634struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4635{
4636 unsigned long i;
4637 struct page *p;
4638 struct extent_buffer *new;
4639 unsigned long num_pages = num_extent_pages(src->start, src->len);
4640
Josef Bacik9ec72672013-08-07 16:57:23 -04004641 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004642 if (new == NULL)
4643 return NULL;
4644
4645 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004646 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004647 if (!p) {
4648 btrfs_release_extent_buffer(new);
4649 return NULL;
4650 }
4651 attach_extent_buffer_page(new, p);
4652 WARN_ON(PageDirty(p));
4653 SetPageUptodate(p);
4654 new->pages[i] = p;
4655 }
4656
4657 copy_extent_buffer(new, src, 0, 0, src->len);
4658 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4659 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4660
4661 return new;
4662}
4663
4664struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4665{
4666 struct extent_buffer *eb;
4667 unsigned long num_pages = num_extent_pages(0, len);
4668 unsigned long i;
4669
Josef Bacik9ec72672013-08-07 16:57:23 -04004670 eb = __alloc_extent_buffer(NULL, start, len, GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004671 if (!eb)
4672 return NULL;
4673
4674 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004675 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004676 if (!eb->pages[i])
4677 goto err;
4678 }
4679 set_extent_buffer_uptodate(eb);
4680 btrfs_set_header_nritems(eb, 0);
4681 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4682
4683 return eb;
4684err:
4685 for (; i > 0; i--)
4686 __free_page(eb->pages[i - 1]);
4687 __free_extent_buffer(eb);
4688 return NULL;
4689}
4690
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004691static void check_buffer_tree_ref(struct extent_buffer *eb)
4692{
Chris Mason242e18c2013-01-29 17:49:37 -05004693 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004694 /* the ref bit is tricky. We have to make sure it is set
4695 * if we have the buffer dirty. Otherwise the
4696 * code to free a buffer can end up dropping a dirty
4697 * page
4698 *
4699 * Once the ref bit is set, it won't go away while the
4700 * buffer is dirty or in writeback, and it also won't
4701 * go away while we have the reference count on the
4702 * eb bumped.
4703 *
4704 * We can't just set the ref bit without bumping the
4705 * ref on the eb because free_extent_buffer might
4706 * see the ref bit and try to clear it. If this happens
4707 * free_extent_buffer might end up dropping our original
4708 * ref by mistake and freeing the page before we are able
4709 * to add one more ref.
4710 *
4711 * So bump the ref count first, then set the bit. If someone
4712 * beat us to it, drop the ref we added.
4713 */
Chris Mason242e18c2013-01-29 17:49:37 -05004714 refs = atomic_read(&eb->refs);
4715 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4716 return;
4717
Josef Bacik594831c2012-07-20 16:11:08 -04004718 spin_lock(&eb->refs_lock);
4719 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004720 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004721 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004722}
4723
Mel Gorman2457aec2014-06-04 16:10:31 -07004724static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4725 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004726{
4727 unsigned long num_pages, i;
4728
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004729 check_buffer_tree_ref(eb);
4730
Josef Bacik5df42352012-03-15 18:24:42 -04004731 num_pages = num_extent_pages(eb->start, eb->len);
4732 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004733 struct page *p = eb->pages[i];
4734
Mel Gorman2457aec2014-06-04 16:10:31 -07004735 if (p != accessed)
4736 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004737 }
4738}
4739
Josef Bacikf28491e2013-12-16 13:24:27 -05004740struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4741 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004742{
4743 struct extent_buffer *eb;
4744
4745 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004746 eb = radix_tree_lookup(&fs_info->buffer_radix,
4747 start >> PAGE_CACHE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004748 if (eb && atomic_inc_not_zero(&eb->refs)) {
4749 rcu_read_unlock();
Mel Gorman2457aec2014-06-04 16:10:31 -07004750 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004751 return eb;
4752 }
4753 rcu_read_unlock();
4754
4755 return NULL;
4756}
4757
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004758#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4759struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
4760 u64 start, unsigned long len)
4761{
4762 struct extent_buffer *eb, *exists = NULL;
4763 int ret;
4764
4765 eb = find_extent_buffer(fs_info, start);
4766 if (eb)
4767 return eb;
4768 eb = alloc_dummy_extent_buffer(start, len);
4769 if (!eb)
4770 return NULL;
4771 eb->fs_info = fs_info;
4772again:
4773 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4774 if (ret)
4775 goto free_eb;
4776 spin_lock(&fs_info->buffer_lock);
4777 ret = radix_tree_insert(&fs_info->buffer_radix,
4778 start >> PAGE_CACHE_SHIFT, eb);
4779 spin_unlock(&fs_info->buffer_lock);
4780 radix_tree_preload_end();
4781 if (ret == -EEXIST) {
4782 exists = find_extent_buffer(fs_info, start);
4783 if (exists)
4784 goto free_eb;
4785 else
4786 goto again;
4787 }
4788 check_buffer_tree_ref(eb);
4789 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4790
4791 /*
4792 * We will free dummy extent buffer's if they come into
4793 * free_extent_buffer with a ref count of 2, but if we are using this we
4794 * want the buffers to stay in memory until we're done with them, so
4795 * bump the ref count again.
4796 */
4797 atomic_inc(&eb->refs);
4798 return eb;
4799free_eb:
4800 btrfs_release_extent_buffer(eb);
4801 return exists;
4802}
4803#endif
4804
Josef Bacikf28491e2013-12-16 13:24:27 -05004805struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
Chris Mason727011e2010-08-06 13:21:20 -04004806 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004807{
4808 unsigned long num_pages = num_extent_pages(start, len);
4809 unsigned long i;
4810 unsigned long index = start >> PAGE_CACHE_SHIFT;
4811 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004812 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004813 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004814 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004815 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004816 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004817
Josef Bacikf28491e2013-12-16 13:24:27 -05004818 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004819 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004820 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004821
Josef Bacikf28491e2013-12-16 13:24:27 -05004822 eb = __alloc_extent_buffer(fs_info, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004823 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004824 return NULL;
4825
Chris Mason727011e2010-08-06 13:21:20 -04004826 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004827 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004828 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004829 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004830
4831 spin_lock(&mapping->private_lock);
4832 if (PagePrivate(p)) {
4833 /*
4834 * We could have already allocated an eb for this page
4835 * and attached one so lets see if we can get a ref on
4836 * the existing eb, and if we can we know it's good and
4837 * we can just return that one, else we know we can just
4838 * overwrite page->private.
4839 */
4840 exists = (struct extent_buffer *)p->private;
4841 if (atomic_inc_not_zero(&exists->refs)) {
4842 spin_unlock(&mapping->private_lock);
4843 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004844 page_cache_release(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004845 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004846 goto free_eb;
4847 }
4848
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004849 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004850 * Do this so attach doesn't complain and we need to
4851 * drop the ref the old guy had.
4852 */
4853 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004854 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004855 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004856 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004857 attach_extent_buffer_page(eb, p);
4858 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004859 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04004860 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004861 if (!PageUptodate(p))
4862 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004863
4864 /*
4865 * see below about how we avoid a nasty race with release page
4866 * and why we unlock later
4867 */
Chris Masond1310b22008-01-24 16:13:08 -05004868 }
4869 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004870 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004871again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004872 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4873 if (ret)
4874 goto free_eb;
4875
Josef Bacikf28491e2013-12-16 13:24:27 -05004876 spin_lock(&fs_info->buffer_lock);
4877 ret = radix_tree_insert(&fs_info->buffer_radix,
4878 start >> PAGE_CACHE_SHIFT, eb);
4879 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004880 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004881 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004882 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004883 if (exists)
4884 goto free_eb;
4885 else
Josef Bacik115391d2012-03-09 09:51:43 -05004886 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004887 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004888 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004889 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004890 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05004891
4892 /*
4893 * there is a race where release page may have
4894 * tried to find this extent buffer in the radix
4895 * but failed. It will tell the VM it is safe to
4896 * reclaim the, and it will clear the page private bit.
4897 * We must make sure to set the page private bit properly
4898 * after the extent buffer is in the radix tree so
4899 * it doesn't get lost
4900 */
Chris Mason727011e2010-08-06 13:21:20 -04004901 SetPageChecked(eb->pages[0]);
4902 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004903 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04004904 ClearPageChecked(p);
4905 unlock_page(p);
4906 }
4907 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004908 return eb;
4909
Chris Mason6af118ce2008-07-22 11:18:07 -04004910free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004911 for (i = 0; i < num_pages; i++) {
4912 if (eb->pages[i])
4913 unlock_page(eb->pages[i]);
4914 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004915
Josef Bacik17de39a2012-05-04 15:16:06 -04004916 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e92010-10-26 20:57:29 -04004917 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004918 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004919}
Chris Masond1310b22008-01-24 16:13:08 -05004920
Josef Bacik3083ee22012-03-09 16:01:49 -05004921static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4922{
4923 struct extent_buffer *eb =
4924 container_of(head, struct extent_buffer, rcu_head);
4925
4926 __free_extent_buffer(eb);
4927}
4928
Josef Bacik3083ee22012-03-09 16:01:49 -05004929/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004930static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004931{
4932 WARN_ON(atomic_read(&eb->refs) == 0);
4933 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05004934 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05004935 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05004936
Jan Schmidt815a51c2012-05-16 17:00:02 +02004937 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004938
Josef Bacikf28491e2013-12-16 13:24:27 -05004939 spin_lock(&fs_info->buffer_lock);
4940 radix_tree_delete(&fs_info->buffer_radix,
Jan Schmidt815a51c2012-05-16 17:00:02 +02004941 eb->start >> PAGE_CACHE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05004942 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05004943 } else {
4944 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004945 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004946
4947 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02004948 btrfs_release_extent_buffer_page(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004949 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004950 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004951 }
4952 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004953
4954 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004955}
4956
Chris Masond1310b22008-01-24 16:13:08 -05004957void free_extent_buffer(struct extent_buffer *eb)
4958{
Chris Mason242e18c2013-01-29 17:49:37 -05004959 int refs;
4960 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004961 if (!eb)
4962 return;
4963
Chris Mason242e18c2013-01-29 17:49:37 -05004964 while (1) {
4965 refs = atomic_read(&eb->refs);
4966 if (refs <= 3)
4967 break;
4968 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4969 if (old == refs)
4970 return;
4971 }
4972
Josef Bacik3083ee22012-03-09 16:01:49 -05004973 spin_lock(&eb->refs_lock);
4974 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004975 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4976 atomic_dec(&eb->refs);
4977
4978 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004979 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004980 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004981 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4982 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004983
Josef Bacik3083ee22012-03-09 16:01:49 -05004984 /*
4985 * I know this is terrible, but it's temporary until we stop tracking
4986 * the uptodate bits and such for the extent buffers.
4987 */
David Sterbaf7a52a42013-04-26 14:56:29 +00004988 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004989}
Chris Masond1310b22008-01-24 16:13:08 -05004990
Josef Bacik3083ee22012-03-09 16:01:49 -05004991void free_extent_buffer_stale(struct extent_buffer *eb)
4992{
4993 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004994 return;
4995
Josef Bacik3083ee22012-03-09 16:01:49 -05004996 spin_lock(&eb->refs_lock);
4997 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4998
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004999 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005000 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5001 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005002 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005003}
5004
Chris Mason1d4284b2012-03-28 20:31:37 -04005005void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005006{
Chris Masond1310b22008-01-24 16:13:08 -05005007 unsigned long i;
5008 unsigned long num_pages;
5009 struct page *page;
5010
Chris Masond1310b22008-01-24 16:13:08 -05005011 num_pages = num_extent_pages(eb->start, eb->len);
5012
5013 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005014 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005015 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005016 continue;
5017
Chris Masona61e6f22008-07-22 11:18:08 -04005018 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005019 WARN_ON(!PagePrivate(page));
5020
Chris Masond1310b22008-01-24 16:13:08 -05005021 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005022 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005023 if (!PageDirty(page)) {
5024 radix_tree_tag_clear(&page->mapping->page_tree,
5025 page_index(page),
5026 PAGECACHE_TAG_DIRTY);
5027 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005028 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005029 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005030 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005031 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005032 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005033}
Chris Masond1310b22008-01-24 16:13:08 -05005034
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005035int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005036{
5037 unsigned long i;
5038 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005039 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005040
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005041 check_buffer_tree_ref(eb);
5042
Chris Masonb9473432009-03-13 11:00:37 -04005043 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005044
Chris Masond1310b22008-01-24 16:13:08 -05005045 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005046 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005047 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5048
Chris Masonb9473432009-03-13 11:00:37 -04005049 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005050 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005051 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005052}
Chris Masond1310b22008-01-24 16:13:08 -05005053
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005054int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005055{
5056 unsigned long i;
5057 struct page *page;
5058 unsigned long num_pages;
5059
Chris Masonb4ce94d2009-02-04 09:25:08 -05005060 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005061 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005062 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005063 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005064 if (page)
5065 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005066 }
5067 return 0;
5068}
5069
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005070int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005071{
5072 unsigned long i;
5073 struct page *page;
5074 unsigned long num_pages;
5075
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005076 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005077 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005078 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005079 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005080 SetPageUptodate(page);
5081 }
5082 return 0;
5083}
Chris Masond1310b22008-01-24 16:13:08 -05005084
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005085int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005086{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005087 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005088}
Chris Masond1310b22008-01-24 16:13:08 -05005089
5090int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02005091 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005092 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005093{
5094 unsigned long i;
5095 unsigned long start_i;
5096 struct page *page;
5097 int err;
5098 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005099 int locked_pages = 0;
5100 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005101 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005102 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005103 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005104 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005105
Chris Masonb4ce94d2009-02-04 09:25:08 -05005106 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005107 return 0;
5108
Chris Masond1310b22008-01-24 16:13:08 -05005109 if (start) {
5110 WARN_ON(start < eb->start);
5111 start_i = (start >> PAGE_CACHE_SHIFT) -
5112 (eb->start >> PAGE_CACHE_SHIFT);
5113 } else {
5114 start_i = 0;
5115 }
5116
5117 num_pages = num_extent_pages(eb->start, eb->len);
5118 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005119 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005120 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005121 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005122 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005123 } else {
5124 lock_page(page);
5125 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005126 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04005127 if (!PageUptodate(page)) {
5128 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005129 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005130 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005131 }
5132 if (all_uptodate) {
5133 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005134 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005135 goto unlock_exit;
5136 }
5137
Filipe Manana656f30d2014-09-26 12:25:56 +01005138 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005139 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005140 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04005141 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005142 page = eb->pages[i];
Chris Masonce9adaa2008-04-09 16:28:12 -04005143 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04005144 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005145 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005146 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005147 mirror_num, &bio_flags,
5148 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05005149 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05005150 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05005151 } else {
5152 unlock_page(page);
5153 }
5154 }
5155
Jeff Mahoney355808c2011-10-03 23:23:14 -04005156 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005157 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
5158 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005159 if (err)
5160 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005161 }
Chris Masona86c12c2008-02-07 10:50:54 -05005162
Arne Jansenbb82ab82011-06-10 14:06:53 +02005163 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005164 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005165
Chris Masond1310b22008-01-24 16:13:08 -05005166 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005167 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005168 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005169 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005170 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005171 }
Chris Masond3977122009-01-05 21:25:51 -05005172
Chris Masond1310b22008-01-24 16:13:08 -05005173 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005174
5175unlock_exit:
5176 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05005177 while (locked_pages > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005178 page = eb->pages[i];
Chris Masonce9adaa2008-04-09 16:28:12 -04005179 i++;
5180 unlock_page(page);
5181 locked_pages--;
5182 }
5183 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005184}
Chris Masond1310b22008-01-24 16:13:08 -05005185
5186void read_extent_buffer(struct extent_buffer *eb, void *dstv,
5187 unsigned long start,
5188 unsigned long len)
5189{
5190 size_t cur;
5191 size_t offset;
5192 struct page *page;
5193 char *kaddr;
5194 char *dst = (char *)dstv;
5195 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5196 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005197
5198 WARN_ON(start > eb->len);
5199 WARN_ON(start + len > eb->start + eb->len);
5200
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005201 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005202
Chris Masond3977122009-01-05 21:25:51 -05005203 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005204 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005205
5206 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005207 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005208 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005209
5210 dst += cur;
5211 len -= cur;
5212 offset = 0;
5213 i++;
5214 }
5215}
Chris Masond1310b22008-01-24 16:13:08 -05005216
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005217int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
5218 unsigned long start,
5219 unsigned long len)
5220{
5221 size_t cur;
5222 size_t offset;
5223 struct page *page;
5224 char *kaddr;
5225 char __user *dst = (char __user *)dstv;
5226 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5227 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5228 int ret = 0;
5229
5230 WARN_ON(start > eb->len);
5231 WARN_ON(start + len > eb->start + eb->len);
5232
5233 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
5234
5235 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005236 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005237
5238 cur = min(len, (PAGE_CACHE_SIZE - offset));
5239 kaddr = page_address(page);
5240 if (copy_to_user(dst, kaddr + offset, cur)) {
5241 ret = -EFAULT;
5242 break;
5243 }
5244
5245 dst += cur;
5246 len -= cur;
5247 offset = 0;
5248 i++;
5249 }
5250
5251 return ret;
5252}
5253
Chris Masond1310b22008-01-24 16:13:08 -05005254int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04005255 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05005256 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04005257 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005258{
5259 size_t offset = start & (PAGE_CACHE_SIZE - 1);
5260 char *kaddr;
5261 struct page *p;
5262 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5263 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5264 unsigned long end_i = (start_offset + start + min_len - 1) >>
5265 PAGE_CACHE_SHIFT;
5266
5267 if (i != end_i)
5268 return -EINVAL;
5269
5270 if (i == 0) {
5271 offset = start_offset;
5272 *map_start = 0;
5273 } else {
5274 offset = 0;
5275 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
5276 }
Chris Masond3977122009-01-05 21:25:51 -05005277
Chris Masond1310b22008-01-24 16:13:08 -05005278 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005279 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005280 "wanted %lu %lu\n",
5281 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04005282 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005283 }
5284
David Sterbafb85fc92014-07-31 01:03:53 +02005285 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005286 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005287 *map = kaddr + offset;
5288 *map_len = PAGE_CACHE_SIZE - offset;
5289 return 0;
5290}
Chris Masond1310b22008-01-24 16:13:08 -05005291
Chris Masond1310b22008-01-24 16:13:08 -05005292int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
5293 unsigned long start,
5294 unsigned long len)
5295{
5296 size_t cur;
5297 size_t offset;
5298 struct page *page;
5299 char *kaddr;
5300 char *ptr = (char *)ptrv;
5301 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5302 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5303 int ret = 0;
5304
5305 WARN_ON(start > eb->len);
5306 WARN_ON(start + len > eb->start + eb->len);
5307
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005308 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005309
Chris Masond3977122009-01-05 21:25:51 -05005310 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005311 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005312
5313 cur = min(len, (PAGE_CACHE_SIZE - offset));
5314
Chris Masona6591712011-07-19 12:04:14 -04005315 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005316 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005317 if (ret)
5318 break;
5319
5320 ptr += cur;
5321 len -= cur;
5322 offset = 0;
5323 i++;
5324 }
5325 return ret;
5326}
Chris Masond1310b22008-01-24 16:13:08 -05005327
5328void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5329 unsigned long start, unsigned long len)
5330{
5331 size_t cur;
5332 size_t offset;
5333 struct page *page;
5334 char *kaddr;
5335 char *src = (char *)srcv;
5336 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5337 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5338
5339 WARN_ON(start > eb->len);
5340 WARN_ON(start + len > eb->start + eb->len);
5341
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005342 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005343
Chris Masond3977122009-01-05 21:25:51 -05005344 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005345 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005346 WARN_ON(!PageUptodate(page));
5347
5348 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005349 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005350 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005351
5352 src += cur;
5353 len -= cur;
5354 offset = 0;
5355 i++;
5356 }
5357}
Chris Masond1310b22008-01-24 16:13:08 -05005358
5359void memset_extent_buffer(struct extent_buffer *eb, char c,
5360 unsigned long start, unsigned long len)
5361{
5362 size_t cur;
5363 size_t offset;
5364 struct page *page;
5365 char *kaddr;
5366 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5367 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5368
5369 WARN_ON(start > eb->len);
5370 WARN_ON(start + len > eb->start + eb->len);
5371
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005372 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005373
Chris Masond3977122009-01-05 21:25:51 -05005374 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005375 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005376 WARN_ON(!PageUptodate(page));
5377
5378 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005379 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005380 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005381
5382 len -= cur;
5383 offset = 0;
5384 i++;
5385 }
5386}
Chris Masond1310b22008-01-24 16:13:08 -05005387
5388void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5389 unsigned long dst_offset, unsigned long src_offset,
5390 unsigned long len)
5391{
5392 u64 dst_len = dst->len;
5393 size_t cur;
5394 size_t offset;
5395 struct page *page;
5396 char *kaddr;
5397 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5398 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5399
5400 WARN_ON(src->len != dst_len);
5401
5402 offset = (start_offset + dst_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005403 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005404
Chris Masond3977122009-01-05 21:25:51 -05005405 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005406 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005407 WARN_ON(!PageUptodate(page));
5408
5409 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
5410
Chris Masona6591712011-07-19 12:04:14 -04005411 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005412 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005413
5414 src_offset += cur;
5415 len -= cur;
5416 offset = 0;
5417 i++;
5418 }
5419}
Chris Masond1310b22008-01-24 16:13:08 -05005420
Sergei Trofimovich33872062011-04-11 21:52:52 +00005421static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5422{
5423 unsigned long distance = (src > dst) ? src - dst : dst - src;
5424 return distance < len;
5425}
5426
Chris Masond1310b22008-01-24 16:13:08 -05005427static void copy_pages(struct page *dst_page, struct page *src_page,
5428 unsigned long dst_off, unsigned long src_off,
5429 unsigned long len)
5430{
Chris Masona6591712011-07-19 12:04:14 -04005431 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005432 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005433 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005434
Sergei Trofimovich33872062011-04-11 21:52:52 +00005435 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005436 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005437 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005438 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005439 if (areas_overlap(src_off, dst_off, len))
5440 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005441 }
Chris Masond1310b22008-01-24 16:13:08 -05005442
Chris Mason727011e2010-08-06 13:21:20 -04005443 if (must_memmove)
5444 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5445 else
5446 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005447}
5448
5449void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5450 unsigned long src_offset, unsigned long len)
5451{
5452 size_t cur;
5453 size_t dst_off_in_page;
5454 size_t src_off_in_page;
5455 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5456 unsigned long dst_i;
5457 unsigned long src_i;
5458
5459 if (src_offset + len > dst->len) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005460 printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move "
Chris Masond3977122009-01-05 21:25:51 -05005461 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005462 BUG_ON(1);
5463 }
5464 if (dst_offset + len > dst->len) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005465 printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move "
Chris Masond3977122009-01-05 21:25:51 -05005466 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005467 BUG_ON(1);
5468 }
5469
Chris Masond3977122009-01-05 21:25:51 -05005470 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005471 dst_off_in_page = (start_offset + dst_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005472 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005473 src_off_in_page = (start_offset + src_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005474 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005475
5476 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5477 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5478
5479 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5480 src_off_in_page));
5481 cur = min_t(unsigned long, cur,
5482 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5483
David Sterbafb85fc92014-07-31 01:03:53 +02005484 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005485 dst_off_in_page, src_off_in_page, cur);
5486
5487 src_offset += cur;
5488 dst_offset += cur;
5489 len -= cur;
5490 }
5491}
Chris Masond1310b22008-01-24 16:13:08 -05005492
5493void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5494 unsigned long src_offset, unsigned long len)
5495{
5496 size_t cur;
5497 size_t dst_off_in_page;
5498 size_t src_off_in_page;
5499 unsigned long dst_end = dst_offset + len - 1;
5500 unsigned long src_end = src_offset + len - 1;
5501 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5502 unsigned long dst_i;
5503 unsigned long src_i;
5504
5505 if (src_offset + len > dst->len) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005506 printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move "
Chris Masond3977122009-01-05 21:25:51 -05005507 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005508 BUG_ON(1);
5509 }
5510 if (dst_offset + len > dst->len) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005511 printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move "
Chris Masond3977122009-01-05 21:25:51 -05005512 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005513 BUG_ON(1);
5514 }
Chris Mason727011e2010-08-06 13:21:20 -04005515 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005516 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5517 return;
5518 }
Chris Masond3977122009-01-05 21:25:51 -05005519 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005520 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5521 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5522
5523 dst_off_in_page = (start_offset + dst_end) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005524 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005525 src_off_in_page = (start_offset + src_end) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005526 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005527
5528 cur = min_t(unsigned long, len, src_off_in_page + 1);
5529 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005530 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005531 dst_off_in_page - cur + 1,
5532 src_off_in_page - cur + 1, cur);
5533
5534 dst_end -= cur;
5535 src_end -= cur;
5536 len -= cur;
5537 }
5538}
Chris Mason6af118ce2008-07-22 11:18:07 -04005539
David Sterbaf7a52a42013-04-26 14:56:29 +00005540int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005541{
Chris Mason6af118ce2008-07-22 11:18:07 -04005542 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005543
Miao Xie19fe0a82010-10-26 20:57:29 -04005544 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005545 * We need to make sure noboody is attaching this page to an eb right
5546 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005547 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005548 spin_lock(&page->mapping->private_lock);
5549 if (!PagePrivate(page)) {
5550 spin_unlock(&page->mapping->private_lock);
5551 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005552 }
5553
Josef Bacik3083ee22012-03-09 16:01:49 -05005554 eb = (struct extent_buffer *)page->private;
5555 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005556
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005557 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005558 * This is a little awful but should be ok, we need to make sure that
5559 * the eb doesn't disappear out from under us while we're looking at
5560 * this page.
5561 */
5562 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005563 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005564 spin_unlock(&eb->refs_lock);
5565 spin_unlock(&page->mapping->private_lock);
5566 return 0;
5567 }
5568 spin_unlock(&page->mapping->private_lock);
5569
Josef Bacik3083ee22012-03-09 16:01:49 -05005570 /*
5571 * If tree ref isn't set then we know the ref on this eb is a real ref,
5572 * so just return, this page will likely be freed soon anyway.
5573 */
5574 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5575 spin_unlock(&eb->refs_lock);
5576 return 0;
5577 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005578
David Sterbaf7a52a42013-04-26 14:56:29 +00005579 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005580}