blob: 3e7c9ed6505b5c174d877e7233f9427be87fa851 [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>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Chris Masond1310b22008-01-24 16:13:08 -050024
Chris Masond1310b22008-01-24 16:13:08 -050025static struct kmem_cache *extent_state_cache;
26static struct kmem_cache *extent_buffer_cache;
27
28static LIST_HEAD(buffers);
29static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040030
Chris Masonb47eda82008-11-10 12:34:40 -050031#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050032#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050033static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040034#endif
Chris Masond1310b22008-01-24 16:13:08 -050035
Chris Masond1310b22008-01-24 16:13:08 -050036#define BUFFER_LRU_MAX 64
37
38struct tree_entry {
39 u64 start;
40 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050041 struct rb_node rb_node;
42};
43
44struct extent_page_data {
45 struct bio *bio;
46 struct extent_io_tree *tree;
47 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050048
49 /* tells writepage not to lock the state bits for this range
50 * it still does the unlocking
51 */
Chris Masonffbd5172009-04-20 15:50:09 -040052 unsigned int extent_locked:1;
53
54 /* tells the submit_bio code to use a WRITE_SYNC */
55 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050056};
57
Josef Bacik0b32f4b2012-03-13 09:38:00 -040058static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040059static inline struct btrfs_fs_info *
60tree_fs_info(struct extent_io_tree *tree)
61{
62 return btrfs_sb(tree->mapping->host->i_sb);
63}
Josef Bacik0b32f4b2012-03-13 09:38:00 -040064
Chris Masond1310b22008-01-24 16:13:08 -050065int __init extent_io_init(void)
66{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020067 extent_state_cache = kmem_cache_create("extent_state",
68 sizeof(struct extent_state), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050070 if (!extent_state_cache)
71 return -ENOMEM;
72
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020073 extent_buffer_cache = kmem_cache_create("extent_buffers",
74 sizeof(struct extent_buffer), 0,
75 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050076 if (!extent_buffer_cache)
77 goto free_state_cache;
78 return 0;
79
80free_state_cache:
81 kmem_cache_destroy(extent_state_cache);
82 return -ENOMEM;
83}
84
85void extent_io_exit(void)
86{
87 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040088 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050089
90 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040091 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050092 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
93 "state %lu in tree %p refs %d\n",
94 (unsigned long long)state->start,
95 (unsigned long long)state->end,
96 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040097 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050098 kmem_cache_free(extent_state_cache, state);
99
100 }
101
Chris Mason2d2ae542008-03-26 16:24:23 -0400102 while (!list_empty(&buffers)) {
103 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500104 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
105 "refs %d\n", (unsigned long long)eb->start,
106 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400107 list_del(&eb->leak_list);
108 kmem_cache_free(extent_buffer_cache, eb);
109 }
Chris Masond1310b22008-01-24 16:13:08 -0500110 if (extent_state_cache)
111 kmem_cache_destroy(extent_state_cache);
112 if (extent_buffer_cache)
113 kmem_cache_destroy(extent_buffer_cache);
114}
115
116void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200117 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500118{
Eric Paris6bef4d32010-02-23 19:43:04 +0000119 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400120 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500121 tree->ops = NULL;
122 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500123 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400124 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500125 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500126}
Chris Masond1310b22008-01-24 16:13:08 -0500127
Christoph Hellwigb2950862008-12-02 09:54:17 -0500128static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500129{
130 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500131#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400132 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400133#endif
Chris Masond1310b22008-01-24 16:13:08 -0500134
135 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400136 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500137 return state;
138 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500139 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500140 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500141#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400142 spin_lock_irqsave(&leak_lock, flags);
143 list_add(&state->leak_list, &states);
144 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Masond1310b22008-01-24 16:13:08 -0500146 atomic_set(&state->refs, 1);
147 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100148 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500149 return state;
150}
Chris Masond1310b22008-01-24 16:13:08 -0500151
Chris Mason4845e442010-05-25 20:56:50 -0400152void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500153{
Chris Masond1310b22008-01-24 16:13:08 -0500154 if (!state)
155 return;
156 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500157#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400158 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400159#endif
Chris Mason70dec802008-01-29 09:59:12 -0500160 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500161#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400162 spin_lock_irqsave(&leak_lock, flags);
163 list_del(&state->leak_list);
164 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400165#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100166 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500167 kmem_cache_free(extent_state_cache, state);
168 }
169}
Chris Masond1310b22008-01-24 16:13:08 -0500170
171static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
172 struct rb_node *node)
173{
Chris Masond3977122009-01-05 21:25:51 -0500174 struct rb_node **p = &root->rb_node;
175 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500176 struct tree_entry *entry;
177
Chris Masond3977122009-01-05 21:25:51 -0500178 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500179 parent = *p;
180 entry = rb_entry(parent, struct tree_entry, rb_node);
181
182 if (offset < entry->start)
183 p = &(*p)->rb_left;
184 else if (offset > entry->end)
185 p = &(*p)->rb_right;
186 else
187 return parent;
188 }
189
Chris Masond1310b22008-01-24 16:13:08 -0500190 rb_link_node(node, parent, p);
191 rb_insert_color(node, root);
192 return NULL;
193}
194
Chris Mason80ea96b2008-02-01 14:51:59 -0500195static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500196 struct rb_node **prev_ret,
197 struct rb_node **next_ret)
198{
Chris Mason80ea96b2008-02-01 14:51:59 -0500199 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500200 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500201 struct rb_node *prev = NULL;
202 struct rb_node *orig_prev = NULL;
203 struct tree_entry *entry;
204 struct tree_entry *prev_entry = NULL;
205
Chris Masond3977122009-01-05 21:25:51 -0500206 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500207 entry = rb_entry(n, struct tree_entry, rb_node);
208 prev = n;
209 prev_entry = entry;
210
211 if (offset < entry->start)
212 n = n->rb_left;
213 else if (offset > entry->end)
214 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500215 else
Chris Masond1310b22008-01-24 16:13:08 -0500216 return n;
217 }
218
219 if (prev_ret) {
220 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500221 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500222 prev = rb_next(prev);
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
224 }
225 *prev_ret = prev;
226 prev = orig_prev;
227 }
228
229 if (next_ret) {
230 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500231 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500232 prev = rb_prev(prev);
233 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
234 }
235 *next_ret = prev;
236 }
237 return NULL;
238}
239
Chris Mason80ea96b2008-02-01 14:51:59 -0500240static inline struct rb_node *tree_search(struct extent_io_tree *tree,
241 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500242{
Chris Mason70dec802008-01-29 09:59:12 -0500243 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500244 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500245
Chris Mason80ea96b2008-02-01 14:51:59 -0500246 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500247 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500248 return prev;
249 return ret;
250}
251
Josef Bacik9ed74f22009-09-11 16:12:44 -0400252static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
253 struct extent_state *other)
254{
255 if (tree->ops && tree->ops->merge_extent_hook)
256 tree->ops->merge_extent_hook(tree->mapping->host, new,
257 other);
258}
259
Chris Masond1310b22008-01-24 16:13:08 -0500260/*
261 * utility function to look for merge candidates inside a given range.
262 * Any extents with matching state are merged together into a single
263 * extent in the tree. Extents with EXTENT_IO in their state field
264 * are not merged because the end_io handlers need to be able to do
265 * operations on them without sleeping (or doing allocations/splits).
266 *
267 * This should be called with the tree lock held.
268 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000269static void merge_state(struct extent_io_tree *tree,
270 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500271{
272 struct extent_state *other;
273 struct rb_node *other_node;
274
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400275 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000276 return;
Chris Masond1310b22008-01-24 16:13:08 -0500277
278 other_node = rb_prev(&state->rb_node);
279 if (other_node) {
280 other = rb_entry(other_node, struct extent_state, rb_node);
281 if (other->end == state->start - 1 &&
282 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400283 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500284 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500285 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500286 rb_erase(&other->rb_node, &tree->state);
287 free_extent_state(other);
288 }
289 }
290 other_node = rb_next(&state->rb_node);
291 if (other_node) {
292 other = rb_entry(other_node, struct extent_state, rb_node);
293 if (other->start == state->end + 1 &&
294 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400295 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400296 state->end = other->end;
297 other->tree = NULL;
298 rb_erase(&other->rb_node, &tree->state);
299 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500300 }
301 }
Chris Masond1310b22008-01-24 16:13:08 -0500302}
303
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000304static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000307 if (tree->ops && tree->ops->set_bit_hook)
308 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
311static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400312 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400314 if (tree->ops && tree->ops->clear_bit_hook)
315 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500316}
317
Xiao Guangrong3150b692011-07-14 03:19:08 +0000318static void set_state_bits(struct extent_io_tree *tree,
319 struct extent_state *state, int *bits);
320
Chris Masond1310b22008-01-24 16:13:08 -0500321/*
322 * insert an extent_state struct into the tree. 'bits' are set on the
323 * struct before it is inserted.
324 *
325 * This may return -EEXIST if the extent is already there, in which case the
326 * state struct is freed.
327 *
328 * The tree lock is not taken internally. This is a utility function and
329 * probably isn't what you want to call (see set/clear_extent_bit).
330 */
331static int insert_state(struct extent_io_tree *tree,
332 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400333 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500334{
335 struct rb_node *node;
336
337 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500338 printk(KERN_ERR "btrfs end < start %llu %llu\n",
339 (unsigned long long)end,
340 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500341 WARN_ON(1);
342 }
Chris Masond1310b22008-01-24 16:13:08 -0500343 state->start = start;
344 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400345
Xiao Guangrong3150b692011-07-14 03:19:08 +0000346 set_state_bits(tree, state, bits);
347
Chris Masond1310b22008-01-24 16:13:08 -0500348 node = tree_insert(&tree->state, end, &state->rb_node);
349 if (node) {
350 struct extent_state *found;
351 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500352 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
353 "%llu %llu\n", (unsigned long long)found->start,
354 (unsigned long long)found->end,
355 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500356 return -EEXIST;
357 }
Chris Mason70dec802008-01-29 09:59:12 -0500358 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500359 merge_state(tree, state);
360 return 0;
361}
362
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000363static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400364 u64 split)
365{
366 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000367 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400368}
369
Chris Masond1310b22008-01-24 16:13:08 -0500370/*
371 * split a given extent state struct in two, inserting the preallocated
372 * struct 'prealloc' as the newly created second half. 'split' indicates an
373 * offset inside 'orig' where it should be split.
374 *
375 * Before calling,
376 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
377 * are two extent state structs in the tree:
378 * prealloc: [orig->start, split - 1]
379 * orig: [ split, orig->end ]
380 *
381 * The tree locks are not taken by this function. They need to be held
382 * by the caller.
383 */
384static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
385 struct extent_state *prealloc, u64 split)
386{
387 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400388
389 split_cb(tree, orig, split);
390
Chris Masond1310b22008-01-24 16:13:08 -0500391 prealloc->start = orig->start;
392 prealloc->end = split - 1;
393 prealloc->state = orig->state;
394 orig->start = split;
395
396 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
397 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500398 free_extent_state(prealloc);
399 return -EEXIST;
400 }
Chris Mason70dec802008-01-29 09:59:12 -0500401 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500402 return 0;
403}
404
Li Zefancdc6a392012-03-12 16:39:48 +0800405static struct extent_state *next_state(struct extent_state *state)
406{
407 struct rb_node *next = rb_next(&state->rb_node);
408 if (next)
409 return rb_entry(next, struct extent_state, rb_node);
410 else
411 return NULL;
412}
413
Chris Masond1310b22008-01-24 16:13:08 -0500414/*
415 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800416 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500417 *
418 * If no bits are set on the state struct after clearing things, the
419 * struct is freed and removed from the tree
420 */
Li Zefancdc6a392012-03-12 16:39:48 +0800421static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
422 struct extent_state *state,
423 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500424{
Li Zefancdc6a392012-03-12 16:39:48 +0800425 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400426 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500427
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400428 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500429 u64 range = state->end - state->start + 1;
430 WARN_ON(range > tree->dirty_bytes);
431 tree->dirty_bytes -= range;
432 }
Chris Mason291d6732008-01-29 15:55:23 -0500433 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400434 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500435 if (wake)
436 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400437 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800438 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500439 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500440 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500441 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500442 free_extent_state(state);
443 } else {
444 WARN_ON(1);
445 }
446 } else {
447 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800448 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500449 }
Li Zefancdc6a392012-03-12 16:39:48 +0800450 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500451}
452
Xiao Guangrong82337672011-04-20 06:44:57 +0000453static struct extent_state *
454alloc_extent_state_atomic(struct extent_state *prealloc)
455{
456 if (!prealloc)
457 prealloc = alloc_extent_state(GFP_ATOMIC);
458
459 return prealloc;
460}
461
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400462void extent_io_tree_panic(struct extent_io_tree *tree, int err)
463{
464 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
465 "Extent tree was modified by another "
466 "thread while locked.");
467}
468
Chris Masond1310b22008-01-24 16:13:08 -0500469/*
470 * clear some bits on a range in the tree. This may require splitting
471 * or inserting elements in the tree, so the gfp mask is used to
472 * indicate which allocations or sleeping are allowed.
473 *
474 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
475 * the given range from the tree regardless of state (ie for truncate).
476 *
477 * the range [start, end] is inclusive.
478 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100479 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500480 */
481int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400482 int bits, int wake, int delete,
483 struct extent_state **cached_state,
484 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500485{
486 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400487 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500488 struct extent_state *prealloc = NULL;
489 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400490 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500491 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000492 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500493
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400494 if (delete)
495 bits |= ~EXTENT_CTLBITS;
496 bits |= EXTENT_FIRST_DELALLOC;
497
Josef Bacik2ac55d42010-02-03 19:33:23 +0000498 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
499 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500500again:
501 if (!prealloc && (mask & __GFP_WAIT)) {
502 prealloc = alloc_extent_state(mask);
503 if (!prealloc)
504 return -ENOMEM;
505 }
506
Chris Masoncad321a2008-12-17 14:51:42 -0500507 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400508 if (cached_state) {
509 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000510
511 if (clear) {
512 *cached_state = NULL;
513 cached_state = NULL;
514 }
515
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400516 if (cached && cached->tree && cached->start <= start &&
517 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000518 if (clear)
519 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400520 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400521 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400522 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000523 if (clear)
524 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400525 }
Chris Masond1310b22008-01-24 16:13:08 -0500526 /*
527 * this search will find the extents that end after
528 * our range starts
529 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500530 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500531 if (!node)
532 goto out;
533 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400534hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500535 if (state->start > end)
536 goto out;
537 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400538 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500539
Liu Bo04493142012-02-16 18:34:37 +0800540 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800541 if (!(state->state & bits)) {
542 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800543 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800544 }
Liu Bo04493142012-02-16 18:34:37 +0800545
Chris Masond1310b22008-01-24 16:13:08 -0500546 /*
547 * | ---- desired range ---- |
548 * | state | or
549 * | ------------- state -------------- |
550 *
551 * We need to split the extent we found, and may flip
552 * bits on second half.
553 *
554 * If the extent we found extends past our range, we
555 * just split and search again. It'll get split again
556 * the next time though.
557 *
558 * If the extent we found is inside our range, we clear
559 * the desired bit on it.
560 */
561
562 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000563 prealloc = alloc_extent_state_atomic(prealloc);
564 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500565 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400566 if (err)
567 extent_io_tree_panic(tree, err);
568
Chris Masond1310b22008-01-24 16:13:08 -0500569 prealloc = NULL;
570 if (err)
571 goto out;
572 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800573 state = clear_state_bit(tree, state, &bits, wake);
574 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500575 }
576 goto search_again;
577 }
578 /*
579 * | ---- desired range ---- |
580 * | state |
581 * We need to split the extent, and clear the bit
582 * on the first half
583 */
584 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000585 prealloc = alloc_extent_state_atomic(prealloc);
586 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500587 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400588 if (err)
589 extent_io_tree_panic(tree, err);
590
Chris Masond1310b22008-01-24 16:13:08 -0500591 if (wake)
592 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400593
Jeff Mahoney6763af82012-03-01 14:56:29 +0100594 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400595
Chris Masond1310b22008-01-24 16:13:08 -0500596 prealloc = NULL;
597 goto out;
598 }
Chris Mason42daec22009-09-23 19:51:09 -0400599
Li Zefancdc6a392012-03-12 16:39:48 +0800600 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800601next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400602 if (last_end == (u64)-1)
603 goto out;
604 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800605 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800606 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500607 goto search_again;
608
609out:
Chris Masoncad321a2008-12-17 14:51:42 -0500610 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500611 if (prealloc)
612 free_extent_state(prealloc);
613
Jeff Mahoney6763af82012-03-01 14:56:29 +0100614 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500615
616search_again:
617 if (start > end)
618 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500619 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500620 if (mask & __GFP_WAIT)
621 cond_resched();
622 goto again;
623}
Chris Masond1310b22008-01-24 16:13:08 -0500624
Jeff Mahoney143bede2012-03-01 14:56:26 +0100625static void wait_on_state(struct extent_io_tree *tree,
626 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500627 __releases(tree->lock)
628 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500629{
630 DEFINE_WAIT(wait);
631 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500632 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500633 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500634 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500635 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500636}
637
638/*
639 * waits for one or more bits to clear on a range in the state tree.
640 * The range [start, end] is inclusive.
641 * The tree lock is taken by this function
642 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100643void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500644{
645 struct extent_state *state;
646 struct rb_node *node;
647
Chris Masoncad321a2008-12-17 14:51:42 -0500648 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500649again:
650 while (1) {
651 /*
652 * this search will find all the extents that end after
653 * our range starts
654 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500655 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500656 if (!node)
657 break;
658
659 state = rb_entry(node, struct extent_state, rb_node);
660
661 if (state->start > end)
662 goto out;
663
664 if (state->state & bits) {
665 start = state->start;
666 atomic_inc(&state->refs);
667 wait_on_state(tree, state);
668 free_extent_state(state);
669 goto again;
670 }
671 start = state->end + 1;
672
673 if (start > end)
674 break;
675
Xiao Guangrongded91f02011-07-14 03:19:27 +0000676 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500677 }
678out:
Chris Masoncad321a2008-12-17 14:51:42 -0500679 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500680}
Chris Masond1310b22008-01-24 16:13:08 -0500681
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000682static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500683 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400684 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500685{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400686 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400687
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000688 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400689 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500690 u64 range = state->end - state->start + 1;
691 tree->dirty_bytes += range;
692 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400693 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500694}
695
Chris Mason2c64c532009-09-02 15:04:12 -0400696static void cache_state(struct extent_state *state,
697 struct extent_state **cached_ptr)
698{
699 if (cached_ptr && !(*cached_ptr)) {
700 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
701 *cached_ptr = state;
702 atomic_inc(&state->refs);
703 }
704 }
705}
706
Arne Jansen507903b2011-04-06 10:02:20 +0000707static void uncache_state(struct extent_state **cached_ptr)
708{
709 if (cached_ptr && (*cached_ptr)) {
710 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400711 *cached_ptr = NULL;
712 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000713 }
714}
715
Chris Masond1310b22008-01-24 16:13:08 -0500716/*
Chris Mason1edbb732009-09-02 13:24:36 -0400717 * set some bits on a range in the tree. This may require allocations or
718 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500719 *
Chris Mason1edbb732009-09-02 13:24:36 -0400720 * If any of the exclusive bits are set, this will fail with -EEXIST if some
721 * part of the range already has the desired bits set. The start of the
722 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500723 *
Chris Mason1edbb732009-09-02 13:24:36 -0400724 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500725 */
Chris Mason1edbb732009-09-02 13:24:36 -0400726
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100727static int __must_check
728__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
729 int bits, int exclusive_bits, u64 *failed_start,
730 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500731{
732 struct extent_state *state;
733 struct extent_state *prealloc = NULL;
734 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500735 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500736 u64 last_start;
737 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400738
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400739 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500740again:
741 if (!prealloc && (mask & __GFP_WAIT)) {
742 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000743 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500744 }
745
Chris Masoncad321a2008-12-17 14:51:42 -0500746 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400747 if (cached_state && *cached_state) {
748 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400749 if (state->start <= start && state->end > start &&
750 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400751 node = &state->rb_node;
752 goto hit_next;
753 }
754 }
Chris Masond1310b22008-01-24 16:13:08 -0500755 /*
756 * this search will find all the extents that end after
757 * our range starts.
758 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500759 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500760 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000761 prealloc = alloc_extent_state_atomic(prealloc);
762 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400763 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400764 if (err)
765 extent_io_tree_panic(tree, err);
766
Chris Masond1310b22008-01-24 16:13:08 -0500767 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500768 goto out;
769 }
Chris Masond1310b22008-01-24 16:13:08 -0500770 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400771hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500772 last_start = state->start;
773 last_end = state->end;
774
775 /*
776 * | ---- desired range ---- |
777 * | state |
778 *
779 * Just lock what we found and keep going
780 */
781 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400782 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500783 *failed_start = state->start;
784 err = -EEXIST;
785 goto out;
786 }
Chris Mason42daec22009-09-23 19:51:09 -0400787
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000788 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400789 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500790 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400791 if (last_end == (u64)-1)
792 goto out;
793 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800794 state = next_state(state);
795 if (start < end && state && state->start == start &&
796 !need_resched())
797 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500798 goto search_again;
799 }
800
801 /*
802 * | ---- desired range ---- |
803 * | state |
804 * or
805 * | ------------- state -------------- |
806 *
807 * We need to split the extent we found, and may flip bits on
808 * second half.
809 *
810 * If the extent we found extends past our
811 * range, we just split and search again. It'll get split
812 * again the next time though.
813 *
814 * If the extent we found is inside our range, we set the
815 * desired bit on it.
816 */
817 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400818 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500819 *failed_start = start;
820 err = -EEXIST;
821 goto out;
822 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000823
824 prealloc = alloc_extent_state_atomic(prealloc);
825 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500826 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400827 if (err)
828 extent_io_tree_panic(tree, err);
829
Chris Masond1310b22008-01-24 16:13:08 -0500830 prealloc = NULL;
831 if (err)
832 goto out;
833 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000834 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400835 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500836 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400837 if (last_end == (u64)-1)
838 goto out;
839 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800840 state = next_state(state);
841 if (start < end && state && state->start == start &&
842 !need_resched())
843 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500844 }
845 goto search_again;
846 }
847 /*
848 * | ---- desired range ---- |
849 * | state | or | state |
850 *
851 * There's a hole, we need to insert something in it and
852 * ignore the extent we found.
853 */
854 if (state->start > start) {
855 u64 this_end;
856 if (end < last_start)
857 this_end = end;
858 else
Chris Masond3977122009-01-05 21:25:51 -0500859 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000860
861 prealloc = alloc_extent_state_atomic(prealloc);
862 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000863
864 /*
865 * Avoid to free 'prealloc' if it can be merged with
866 * the later extent.
867 */
Chris Masond1310b22008-01-24 16:13:08 -0500868 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400869 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400870 if (err)
871 extent_io_tree_panic(tree, err);
872
Chris Mason2c64c532009-09-02 15:04:12 -0400873 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500874 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500875 start = this_end + 1;
876 goto search_again;
877 }
878 /*
879 * | ---- desired range ---- |
880 * | state |
881 * We need to split the extent, and set the bit
882 * on the first half
883 */
884 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400885 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500886 *failed_start = start;
887 err = -EEXIST;
888 goto out;
889 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000890
891 prealloc = alloc_extent_state_atomic(prealloc);
892 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500893 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400894 if (err)
895 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500896
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000897 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400898 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500899 merge_state(tree, prealloc);
900 prealloc = NULL;
901 goto out;
902 }
903
904 goto search_again;
905
906out:
Chris Masoncad321a2008-12-17 14:51:42 -0500907 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500908 if (prealloc)
909 free_extent_state(prealloc);
910
911 return err;
912
913search_again:
914 if (start > end)
915 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500916 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500917 if (mask & __GFP_WAIT)
918 cond_resched();
919 goto again;
920}
Chris Masond1310b22008-01-24 16:13:08 -0500921
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100922int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
923 u64 *failed_start, struct extent_state **cached_state,
924 gfp_t mask)
925{
926 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
927 cached_state, mask);
928}
929
930
Josef Bacik462d6fa2011-09-26 13:56:12 -0400931/**
932 * convert_extent - convert all bits in a given range from one bit to another
933 * @tree: the io tree to search
934 * @start: the start offset in bytes
935 * @end: the end offset in bytes (inclusive)
936 * @bits: the bits to set in this range
937 * @clear_bits: the bits to clear in this range
938 * @mask: the allocation mask
939 *
940 * This will go through and set bits for the given range. If any states exist
941 * already in this range they are set with the given bit and cleared of the
942 * clear_bits. This is only meant to be used by things that are mergeable, ie
943 * converting from say DELALLOC to DIRTY. This is not meant to be used with
944 * boundary bits like LOCK.
945 */
946int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
947 int bits, int clear_bits, gfp_t mask)
948{
949 struct extent_state *state;
950 struct extent_state *prealloc = NULL;
951 struct rb_node *node;
952 int err = 0;
953 u64 last_start;
954 u64 last_end;
955
956again:
957 if (!prealloc && (mask & __GFP_WAIT)) {
958 prealloc = alloc_extent_state(mask);
959 if (!prealloc)
960 return -ENOMEM;
961 }
962
963 spin_lock(&tree->lock);
964 /*
965 * this search will find all the extents that end after
966 * our range starts.
967 */
968 node = tree_search(tree, start);
969 if (!node) {
970 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500971 if (!prealloc) {
972 err = -ENOMEM;
973 goto out;
974 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400975 err = insert_state(tree, prealloc, start, end, &bits);
976 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400977 if (err)
978 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400979 goto out;
980 }
981 state = rb_entry(node, struct extent_state, rb_node);
982hit_next:
983 last_start = state->start;
984 last_end = state->end;
985
986 /*
987 * | ---- desired range ---- |
988 * | state |
989 *
990 * Just lock what we found and keep going
991 */
992 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -0400993 set_state_bits(tree, state, &bits);
Liu Bod1ac6e42012-05-10 18:10:39 +0800994 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400995 if (last_end == (u64)-1)
996 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -0400997 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800998 if (start < end && state && state->start == start &&
999 !need_resched())
1000 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001001 goto search_again;
1002 }
1003
1004 /*
1005 * | ---- desired range ---- |
1006 * | state |
1007 * or
1008 * | ------------- state -------------- |
1009 *
1010 * We need to split the extent we found, and may flip bits on
1011 * second half.
1012 *
1013 * If the extent we found extends past our
1014 * range, we just split and search again. It'll get split
1015 * again the next time though.
1016 *
1017 * If the extent we found is inside our range, we set the
1018 * desired bit on it.
1019 */
1020 if (state->start < start) {
1021 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001022 if (!prealloc) {
1023 err = -ENOMEM;
1024 goto out;
1025 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001026 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001027 if (err)
1028 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001029 prealloc = NULL;
1030 if (err)
1031 goto out;
1032 if (state->end <= end) {
1033 set_state_bits(tree, state, &bits);
Liu Bod1ac6e42012-05-10 18:10:39 +08001034 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001035 if (last_end == (u64)-1)
1036 goto out;
1037 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001038 if (start < end && state && state->start == start &&
1039 !need_resched())
1040 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001041 }
1042 goto search_again;
1043 }
1044 /*
1045 * | ---- desired range ---- |
1046 * | state | or | state |
1047 *
1048 * There's a hole, we need to insert something in it and
1049 * ignore the extent we found.
1050 */
1051 if (state->start > start) {
1052 u64 this_end;
1053 if (end < last_start)
1054 this_end = end;
1055 else
1056 this_end = last_start - 1;
1057
1058 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001059 if (!prealloc) {
1060 err = -ENOMEM;
1061 goto out;
1062 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001063
1064 /*
1065 * Avoid to free 'prealloc' if it can be merged with
1066 * the later extent.
1067 */
1068 err = insert_state(tree, prealloc, start, this_end,
1069 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001070 if (err)
1071 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001072 prealloc = NULL;
1073 start = this_end + 1;
1074 goto search_again;
1075 }
1076 /*
1077 * | ---- desired range ---- |
1078 * | state |
1079 * We need to split the extent, and set the bit
1080 * on the first half
1081 */
1082 if (state->start <= end && state->end > end) {
1083 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001084 if (!prealloc) {
1085 err = -ENOMEM;
1086 goto out;
1087 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001088
1089 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001090 if (err)
1091 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001092
1093 set_state_bits(tree, prealloc, &bits);
1094 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001095 prealloc = NULL;
1096 goto out;
1097 }
1098
1099 goto search_again;
1100
1101out:
1102 spin_unlock(&tree->lock);
1103 if (prealloc)
1104 free_extent_state(prealloc);
1105
1106 return err;
1107
1108search_again:
1109 if (start > end)
1110 goto out;
1111 spin_unlock(&tree->lock);
1112 if (mask & __GFP_WAIT)
1113 cond_resched();
1114 goto again;
1115}
1116
Chris Masond1310b22008-01-24 16:13:08 -05001117/* wrappers around set/clear extent bit */
1118int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1119 gfp_t mask)
1120{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001121 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001122 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001123}
Chris Masond1310b22008-01-24 16:13:08 -05001124
1125int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1126 int bits, gfp_t mask)
1127{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001128 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001129 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001130}
Chris Masond1310b22008-01-24 16:13:08 -05001131
1132int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1133 int bits, gfp_t mask)
1134{
Chris Mason2c64c532009-09-02 15:04:12 -04001135 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001136}
Chris Masond1310b22008-01-24 16:13:08 -05001137
1138int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001139 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001140{
1141 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001142 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001143 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001144}
Chris Masond1310b22008-01-24 16:13:08 -05001145
1146int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1147 gfp_t mask)
1148{
1149 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001150 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001151 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001152}
Chris Masond1310b22008-01-24 16:13:08 -05001153
1154int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1155 gfp_t mask)
1156{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001157 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001158 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001159}
Chris Masond1310b22008-01-24 16:13:08 -05001160
Chris Masond1310b22008-01-24 16:13:08 -05001161int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001162 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001163{
Arne Jansen507903b2011-04-06 10:02:20 +00001164 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001165 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001166}
Chris Masond1310b22008-01-24 16:13:08 -05001167
Josef Bacik5fd02042012-05-02 14:00:54 -04001168int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1169 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001170{
Chris Mason2c64c532009-09-02 15:04:12 -04001171 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001172 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001173}
Chris Masond1310b22008-01-24 16:13:08 -05001174
Chris Masond352ac62008-09-29 15:18:18 -04001175/*
1176 * either insert or lock state struct between start and end use mask to tell
1177 * us if waiting is desired.
1178 */
Chris Mason1edbb732009-09-02 13:24:36 -04001179int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001180 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001181{
1182 int err;
1183 u64 failed_start;
1184 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001185 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1186 EXTENT_LOCKED, &failed_start,
1187 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001188 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001189 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1190 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001191 } else
Chris Masond1310b22008-01-24 16:13:08 -05001192 break;
Chris Masond1310b22008-01-24 16:13:08 -05001193 WARN_ON(start > end);
1194 }
1195 return err;
1196}
Chris Masond1310b22008-01-24 16:13:08 -05001197
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001198int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001199{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001200 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001201}
1202
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001203int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001204{
1205 int err;
1206 u64 failed_start;
1207
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001208 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1209 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001210 if (err == -EEXIST) {
1211 if (failed_start > start)
1212 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001213 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001214 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001215 }
Josef Bacik25179202008-10-29 14:49:05 -04001216 return 1;
1217}
Josef Bacik25179202008-10-29 14:49:05 -04001218
Chris Mason2c64c532009-09-02 15:04:12 -04001219int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1220 struct extent_state **cached, gfp_t mask)
1221{
1222 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1223 mask);
1224}
1225
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001226int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001227{
Chris Mason2c64c532009-09-02 15:04:12 -04001228 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001229 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001230}
Chris Masond1310b22008-01-24 16:13:08 -05001231
1232/*
Chris Masond1310b22008-01-24 16:13:08 -05001233 * helper function to set both pages and extents in the tree writeback
1234 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001235static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001236{
1237 unsigned long index = start >> PAGE_CACHE_SHIFT;
1238 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1239 struct page *page;
1240
1241 while (index <= end_index) {
1242 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001243 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001244 set_page_writeback(page);
1245 page_cache_release(page);
1246 index++;
1247 }
Chris Masond1310b22008-01-24 16:13:08 -05001248 return 0;
1249}
Chris Masond1310b22008-01-24 16:13:08 -05001250
Chris Masond352ac62008-09-29 15:18:18 -04001251/* find the first state struct with 'bits' set after 'start', and
1252 * return it. tree->lock must be held. NULL will returned if
1253 * nothing was found after 'start'
1254 */
Chris Masond7fc6402008-02-18 12:12:38 -05001255struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1256 u64 start, int bits)
1257{
1258 struct rb_node *node;
1259 struct extent_state *state;
1260
1261 /*
1262 * this search will find all the extents that end after
1263 * our range starts.
1264 */
1265 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001266 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001267 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001268
Chris Masond3977122009-01-05 21:25:51 -05001269 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001270 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001271 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001272 return state;
Chris Masond3977122009-01-05 21:25:51 -05001273
Chris Masond7fc6402008-02-18 12:12:38 -05001274 node = rb_next(node);
1275 if (!node)
1276 break;
1277 }
1278out:
1279 return NULL;
1280}
Chris Masond7fc6402008-02-18 12:12:38 -05001281
Chris Masond352ac62008-09-29 15:18:18 -04001282/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001283 * find the first offset in the io tree with 'bits' set. zero is
1284 * returned if we find something, and *start_ret and *end_ret are
1285 * set to reflect the state struct that was found.
1286 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001287 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001288 */
1289int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1290 u64 *start_ret, u64 *end_ret, int bits)
1291{
1292 struct extent_state *state;
1293 int ret = 1;
1294
1295 spin_lock(&tree->lock);
1296 state = find_first_extent_bit_state(tree, start, bits);
1297 if (state) {
1298 *start_ret = state->start;
1299 *end_ret = state->end;
1300 ret = 0;
1301 }
1302 spin_unlock(&tree->lock);
1303 return ret;
1304}
1305
1306/*
Chris Masond352ac62008-09-29 15:18:18 -04001307 * find a contiguous range of bytes in the file marked as delalloc, not
1308 * more than 'max_bytes'. start and end are used to return the range,
1309 *
1310 * 1 is returned if we find something, 0 if nothing was in the tree
1311 */
Chris Masonc8b97812008-10-29 14:49:59 -04001312static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001313 u64 *start, u64 *end, u64 max_bytes,
1314 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001315{
1316 struct rb_node *node;
1317 struct extent_state *state;
1318 u64 cur_start = *start;
1319 u64 found = 0;
1320 u64 total_bytes = 0;
1321
Chris Masoncad321a2008-12-17 14:51:42 -05001322 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001323
Chris Masond1310b22008-01-24 16:13:08 -05001324 /*
1325 * this search will find all the extents that end after
1326 * our range starts.
1327 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001328 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001329 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001330 if (!found)
1331 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001332 goto out;
1333 }
1334
Chris Masond3977122009-01-05 21:25:51 -05001335 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001336 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001337 if (found && (state->start != cur_start ||
1338 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001339 goto out;
1340 }
1341 if (!(state->state & EXTENT_DELALLOC)) {
1342 if (!found)
1343 *end = state->end;
1344 goto out;
1345 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001346 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001347 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001348 *cached_state = state;
1349 atomic_inc(&state->refs);
1350 }
Chris Masond1310b22008-01-24 16:13:08 -05001351 found++;
1352 *end = state->end;
1353 cur_start = state->end + 1;
1354 node = rb_next(node);
1355 if (!node)
1356 break;
1357 total_bytes += state->end - state->start + 1;
1358 if (total_bytes >= max_bytes)
1359 break;
1360 }
1361out:
Chris Masoncad321a2008-12-17 14:51:42 -05001362 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001363 return found;
1364}
1365
Jeff Mahoney143bede2012-03-01 14:56:26 +01001366static noinline void __unlock_for_delalloc(struct inode *inode,
1367 struct page *locked_page,
1368 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001369{
1370 int ret;
1371 struct page *pages[16];
1372 unsigned long index = start >> PAGE_CACHE_SHIFT;
1373 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1374 unsigned long nr_pages = end_index - index + 1;
1375 int i;
1376
1377 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001378 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001379
Chris Masond3977122009-01-05 21:25:51 -05001380 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001381 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001382 min_t(unsigned long, nr_pages,
1383 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001384 for (i = 0; i < ret; i++) {
1385 if (pages[i] != locked_page)
1386 unlock_page(pages[i]);
1387 page_cache_release(pages[i]);
1388 }
1389 nr_pages -= ret;
1390 index += ret;
1391 cond_resched();
1392 }
Chris Masonc8b97812008-10-29 14:49:59 -04001393}
1394
1395static noinline int lock_delalloc_pages(struct inode *inode,
1396 struct page *locked_page,
1397 u64 delalloc_start,
1398 u64 delalloc_end)
1399{
1400 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1401 unsigned long start_index = index;
1402 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1403 unsigned long pages_locked = 0;
1404 struct page *pages[16];
1405 unsigned long nrpages;
1406 int ret;
1407 int i;
1408
1409 /* the caller is responsible for locking the start index */
1410 if (index == locked_page->index && index == end_index)
1411 return 0;
1412
1413 /* skip the page at the start index */
1414 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001415 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001416 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001417 min_t(unsigned long,
1418 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001419 if (ret == 0) {
1420 ret = -EAGAIN;
1421 goto done;
1422 }
1423 /* now we have an array of pages, lock them all */
1424 for (i = 0; i < ret; i++) {
1425 /*
1426 * the caller is taking responsibility for
1427 * locked_page
1428 */
Chris Mason771ed682008-11-06 22:02:51 -05001429 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001430 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001431 if (!PageDirty(pages[i]) ||
1432 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001433 ret = -EAGAIN;
1434 unlock_page(pages[i]);
1435 page_cache_release(pages[i]);
1436 goto done;
1437 }
1438 }
Chris Masonc8b97812008-10-29 14:49:59 -04001439 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001440 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001441 }
Chris Masonc8b97812008-10-29 14:49:59 -04001442 nrpages -= ret;
1443 index += ret;
1444 cond_resched();
1445 }
1446 ret = 0;
1447done:
1448 if (ret && pages_locked) {
1449 __unlock_for_delalloc(inode, locked_page,
1450 delalloc_start,
1451 ((u64)(start_index + pages_locked - 1)) <<
1452 PAGE_CACHE_SHIFT);
1453 }
1454 return ret;
1455}
1456
1457/*
1458 * find a contiguous range of bytes in the file marked as delalloc, not
1459 * more than 'max_bytes'. start and end are used to return the range,
1460 *
1461 * 1 is returned if we find something, 0 if nothing was in the tree
1462 */
1463static noinline u64 find_lock_delalloc_range(struct inode *inode,
1464 struct extent_io_tree *tree,
1465 struct page *locked_page,
1466 u64 *start, u64 *end,
1467 u64 max_bytes)
1468{
1469 u64 delalloc_start;
1470 u64 delalloc_end;
1471 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001472 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001473 int ret;
1474 int loops = 0;
1475
1476again:
1477 /* step one, find a bunch of delalloc bytes starting at start */
1478 delalloc_start = *start;
1479 delalloc_end = 0;
1480 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001481 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001482 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001483 *start = delalloc_start;
1484 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001485 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001486 return found;
1487 }
1488
1489 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001490 * start comes from the offset of locked_page. We have to lock
1491 * pages in order, so we can't process delalloc bytes before
1492 * locked_page
1493 */
Chris Masond3977122009-01-05 21:25:51 -05001494 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001495 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001496
1497 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001498 * make sure to limit the number of pages we try to lock down
1499 * if we're looping.
1500 */
Chris Masond3977122009-01-05 21:25:51 -05001501 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001502 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001503
Chris Masonc8b97812008-10-29 14:49:59 -04001504 /* step two, lock all the pages after the page that has start */
1505 ret = lock_delalloc_pages(inode, locked_page,
1506 delalloc_start, delalloc_end);
1507 if (ret == -EAGAIN) {
1508 /* some of the pages are gone, lets avoid looping by
1509 * shortening the size of the delalloc range we're searching
1510 */
Chris Mason9655d292009-09-02 15:22:30 -04001511 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001512 if (!loops) {
1513 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1514 max_bytes = PAGE_CACHE_SIZE - offset;
1515 loops = 1;
1516 goto again;
1517 } else {
1518 found = 0;
1519 goto out_failed;
1520 }
1521 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001522 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001523
1524 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001525 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001526
1527 /* then test to make sure it is all still delalloc */
1528 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001529 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001530 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001531 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1532 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001533 __unlock_for_delalloc(inode, locked_page,
1534 delalloc_start, delalloc_end);
1535 cond_resched();
1536 goto again;
1537 }
Chris Mason9655d292009-09-02 15:22:30 -04001538 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001539 *start = delalloc_start;
1540 *end = delalloc_end;
1541out_failed:
1542 return found;
1543}
1544
1545int extent_clear_unlock_delalloc(struct inode *inode,
1546 struct extent_io_tree *tree,
1547 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001548 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001549{
1550 int ret;
1551 struct page *pages[16];
1552 unsigned long index = start >> PAGE_CACHE_SHIFT;
1553 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1554 unsigned long nr_pages = end_index - index + 1;
1555 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001556 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001557
Chris Masona791e352009-10-08 11:27:10 -04001558 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001559 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001560 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001561 clear_bits |= EXTENT_DIRTY;
1562
Chris Masona791e352009-10-08 11:27:10 -04001563 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001564 clear_bits |= EXTENT_DELALLOC;
1565
Chris Mason2c64c532009-09-02 15:04:12 -04001566 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001567 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1568 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1569 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001570 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001571
Chris Masond3977122009-01-05 21:25:51 -05001572 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001573 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001574 min_t(unsigned long,
1575 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001576 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001577
Chris Masona791e352009-10-08 11:27:10 -04001578 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001579 SetPagePrivate2(pages[i]);
1580
Chris Masonc8b97812008-10-29 14:49:59 -04001581 if (pages[i] == locked_page) {
1582 page_cache_release(pages[i]);
1583 continue;
1584 }
Chris Masona791e352009-10-08 11:27:10 -04001585 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001586 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001587 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001588 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001589 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001590 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001591 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001592 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001593 page_cache_release(pages[i]);
1594 }
1595 nr_pages -= ret;
1596 index += ret;
1597 cond_resched();
1598 }
1599 return 0;
1600}
Chris Masonc8b97812008-10-29 14:49:59 -04001601
Chris Masond352ac62008-09-29 15:18:18 -04001602/*
1603 * count the number of bytes in the tree that have a given bit(s)
1604 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1605 * cached. The total number found is returned.
1606 */
Chris Masond1310b22008-01-24 16:13:08 -05001607u64 count_range_bits(struct extent_io_tree *tree,
1608 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001609 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001610{
1611 struct rb_node *node;
1612 struct extent_state *state;
1613 u64 cur_start = *start;
1614 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001615 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001616 int found = 0;
1617
1618 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001619 WARN_ON(1);
1620 return 0;
1621 }
1622
Chris Masoncad321a2008-12-17 14:51:42 -05001623 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001624 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1625 total_bytes = tree->dirty_bytes;
1626 goto out;
1627 }
1628 /*
1629 * this search will find all the extents that end after
1630 * our range starts.
1631 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001632 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001633 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001634 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001635
Chris Masond3977122009-01-05 21:25:51 -05001636 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001637 state = rb_entry(node, struct extent_state, rb_node);
1638 if (state->start > search_end)
1639 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001640 if (contig && found && state->start > last + 1)
1641 break;
1642 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001643 total_bytes += min(search_end, state->end) + 1 -
1644 max(cur_start, state->start);
1645 if (total_bytes >= max_bytes)
1646 break;
1647 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001648 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001649 found = 1;
1650 }
Chris Masonec29ed52011-02-23 16:23:20 -05001651 last = state->end;
1652 } else if (contig && found) {
1653 break;
Chris Masond1310b22008-01-24 16:13:08 -05001654 }
1655 node = rb_next(node);
1656 if (!node)
1657 break;
1658 }
1659out:
Chris Masoncad321a2008-12-17 14:51:42 -05001660 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001661 return total_bytes;
1662}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001663
Chris Masond352ac62008-09-29 15:18:18 -04001664/*
1665 * set the private field for a given byte offset in the tree. If there isn't
1666 * an extent_state there already, this does nothing.
1667 */
Chris Masond1310b22008-01-24 16:13:08 -05001668int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1669{
1670 struct rb_node *node;
1671 struct extent_state *state;
1672 int ret = 0;
1673
Chris Masoncad321a2008-12-17 14:51:42 -05001674 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001675 /*
1676 * this search will find all the extents that end after
1677 * our range starts.
1678 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001679 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001680 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001681 ret = -ENOENT;
1682 goto out;
1683 }
1684 state = rb_entry(node, struct extent_state, rb_node);
1685 if (state->start != start) {
1686 ret = -ENOENT;
1687 goto out;
1688 }
1689 state->private = private;
1690out:
Chris Masoncad321a2008-12-17 14:51:42 -05001691 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001692 return ret;
1693}
1694
1695int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1696{
1697 struct rb_node *node;
1698 struct extent_state *state;
1699 int ret = 0;
1700
Chris Masoncad321a2008-12-17 14:51:42 -05001701 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001702 /*
1703 * this search will find all the extents that end after
1704 * our range starts.
1705 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001706 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001707 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001708 ret = -ENOENT;
1709 goto out;
1710 }
1711 state = rb_entry(node, struct extent_state, rb_node);
1712 if (state->start != start) {
1713 ret = -ENOENT;
1714 goto out;
1715 }
1716 *private = state->private;
1717out:
Chris Masoncad321a2008-12-17 14:51:42 -05001718 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001719 return ret;
1720}
1721
1722/*
1723 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001724 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001725 * has the bits set. Otherwise, 1 is returned if any bit in the
1726 * range is found set.
1727 */
1728int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001729 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001730{
1731 struct extent_state *state = NULL;
1732 struct rb_node *node;
1733 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001734
Chris Masoncad321a2008-12-17 14:51:42 -05001735 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001736 if (cached && cached->tree && cached->start <= start &&
1737 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001738 node = &cached->rb_node;
1739 else
1740 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001741 while (node && start <= end) {
1742 state = rb_entry(node, struct extent_state, rb_node);
1743
1744 if (filled && state->start > start) {
1745 bitset = 0;
1746 break;
1747 }
1748
1749 if (state->start > end)
1750 break;
1751
1752 if (state->state & bits) {
1753 bitset = 1;
1754 if (!filled)
1755 break;
1756 } else if (filled) {
1757 bitset = 0;
1758 break;
1759 }
Chris Mason46562ce2009-09-23 20:23:16 -04001760
1761 if (state->end == (u64)-1)
1762 break;
1763
Chris Masond1310b22008-01-24 16:13:08 -05001764 start = state->end + 1;
1765 if (start > end)
1766 break;
1767 node = rb_next(node);
1768 if (!node) {
1769 if (filled)
1770 bitset = 0;
1771 break;
1772 }
1773 }
Chris Masoncad321a2008-12-17 14:51:42 -05001774 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001775 return bitset;
1776}
Chris Masond1310b22008-01-24 16:13:08 -05001777
1778/*
1779 * helper function to set a given page up to date if all the
1780 * extents in the tree for that page are up to date
1781 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001782static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001783{
1784 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1785 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001786 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001787 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001788}
1789
1790/*
1791 * helper function to unlock a page if all the extents in the tree
1792 * for that page are unlocked
1793 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001794static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001795{
1796 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1797 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001798 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001799 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001800}
1801
1802/*
1803 * helper function to end page writeback if all the extents
1804 * in the tree for that page are done with writeback
1805 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001806static void check_page_writeback(struct extent_io_tree *tree,
1807 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001808{
Chris Mason1edbb732009-09-02 13:24:36 -04001809 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001810}
1811
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001812/*
1813 * When IO fails, either with EIO or csum verification fails, we
1814 * try other mirrors that might have a good copy of the data. This
1815 * io_failure_record is used to record state as we go through all the
1816 * mirrors. If another mirror has good data, the page is set up to date
1817 * and things continue. If a good mirror can't be found, the original
1818 * bio end_io callback is called to indicate things have failed.
1819 */
1820struct io_failure_record {
1821 struct page *page;
1822 u64 start;
1823 u64 len;
1824 u64 logical;
1825 unsigned long bio_flags;
1826 int this_mirror;
1827 int failed_mirror;
1828 int in_validation;
1829};
1830
1831static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1832 int did_repair)
1833{
1834 int ret;
1835 int err = 0;
1836 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1837
1838 set_state_private(failure_tree, rec->start, 0);
1839 ret = clear_extent_bits(failure_tree, rec->start,
1840 rec->start + rec->len - 1,
1841 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1842 if (ret)
1843 err = ret;
1844
1845 if (did_repair) {
1846 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1847 rec->start + rec->len - 1,
1848 EXTENT_DAMAGED, GFP_NOFS);
1849 if (ret && !err)
1850 err = ret;
1851 }
1852
1853 kfree(rec);
1854 return err;
1855}
1856
1857static void repair_io_failure_callback(struct bio *bio, int err)
1858{
1859 complete(bio->bi_private);
1860}
1861
1862/*
1863 * this bypasses the standard btrfs submit functions deliberately, as
1864 * the standard behavior is to write all copies in a raid setup. here we only
1865 * want to write the one bad copy. so we do the mapping for ourselves and issue
1866 * submit_bio directly.
1867 * to avoid any synchonization issues, wait for the data after writing, which
1868 * actually prevents the read that triggered the error from finishing.
1869 * currently, there can be no more than two copies of every data bit. thus,
1870 * exactly one rewrite is required.
1871 */
1872int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1873 u64 length, u64 logical, struct page *page,
1874 int mirror_num)
1875{
1876 struct bio *bio;
1877 struct btrfs_device *dev;
1878 DECLARE_COMPLETION_ONSTACK(compl);
1879 u64 map_length = 0;
1880 u64 sector;
1881 struct btrfs_bio *bbio = NULL;
1882 int ret;
1883
1884 BUG_ON(!mirror_num);
1885
1886 bio = bio_alloc(GFP_NOFS, 1);
1887 if (!bio)
1888 return -EIO;
1889 bio->bi_private = &compl;
1890 bio->bi_end_io = repair_io_failure_callback;
1891 bio->bi_size = 0;
1892 map_length = length;
1893
1894 ret = btrfs_map_block(map_tree, WRITE, logical,
1895 &map_length, &bbio, mirror_num);
1896 if (ret) {
1897 bio_put(bio);
1898 return -EIO;
1899 }
1900 BUG_ON(mirror_num != bbio->mirror_num);
1901 sector = bbio->stripes[mirror_num-1].physical >> 9;
1902 bio->bi_sector = sector;
1903 dev = bbio->stripes[mirror_num-1].dev;
1904 kfree(bbio);
1905 if (!dev || !dev->bdev || !dev->writeable) {
1906 bio_put(bio);
1907 return -EIO;
1908 }
1909 bio->bi_bdev = dev->bdev;
1910 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001911 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001912 wait_for_completion(&compl);
1913
1914 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1915 /* try to remap that extent elsewhere? */
1916 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001917 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001918 return -EIO;
1919 }
1920
Anand Jaind5b025d2012-07-02 22:05:21 -06001921 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04001922 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1923 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001924
1925 bio_put(bio);
1926 return 0;
1927}
1928
Josef Bacikea466792012-03-26 21:57:36 -04001929int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1930 int mirror_num)
1931{
1932 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1933 u64 start = eb->start;
1934 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001935 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001936
1937 for (i = 0; i < num_pages; i++) {
1938 struct page *p = extent_buffer_page(eb, i);
1939 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1940 start, p, mirror_num);
1941 if (ret)
1942 break;
1943 start += PAGE_CACHE_SIZE;
1944 }
1945
1946 return ret;
1947}
1948
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001949/*
1950 * each time an IO finishes, we do a fast check in the IO failure tree
1951 * to see if we need to process or clean up an io_failure_record
1952 */
1953static int clean_io_failure(u64 start, struct page *page)
1954{
1955 u64 private;
1956 u64 private_failure;
1957 struct io_failure_record *failrec;
1958 struct btrfs_mapping_tree *map_tree;
1959 struct extent_state *state;
1960 int num_copies;
1961 int did_repair = 0;
1962 int ret;
1963 struct inode *inode = page->mapping->host;
1964
1965 private = 0;
1966 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1967 (u64)-1, 1, EXTENT_DIRTY, 0);
1968 if (!ret)
1969 return 0;
1970
1971 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1972 &private_failure);
1973 if (ret)
1974 return 0;
1975
1976 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1977 BUG_ON(!failrec->this_mirror);
1978
1979 if (failrec->in_validation) {
1980 /* there was no real error, just free the record */
1981 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1982 failrec->start);
1983 did_repair = 1;
1984 goto out;
1985 }
1986
1987 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1988 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1989 failrec->start,
1990 EXTENT_LOCKED);
1991 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1992
1993 if (state && state->start == failrec->start) {
1994 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1995 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1996 failrec->len);
1997 if (num_copies > 1) {
1998 ret = repair_io_failure(map_tree, start, failrec->len,
1999 failrec->logical, page,
2000 failrec->failed_mirror);
2001 did_repair = !ret;
2002 }
2003 }
2004
2005out:
2006 if (!ret)
2007 ret = free_io_failure(inode, failrec, did_repair);
2008
2009 return ret;
2010}
2011
2012/*
2013 * this is a generic handler for readpage errors (default
2014 * readpage_io_failed_hook). if other copies exist, read those and write back
2015 * good data to the failed position. does not investigate in remapping the
2016 * failed extent elsewhere, hoping the device will be smart enough to do this as
2017 * needed
2018 */
2019
2020static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2021 u64 start, u64 end, int failed_mirror,
2022 struct extent_state *state)
2023{
2024 struct io_failure_record *failrec = NULL;
2025 u64 private;
2026 struct extent_map *em;
2027 struct inode *inode = page->mapping->host;
2028 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2029 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2030 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2031 struct bio *bio;
2032 int num_copies;
2033 int ret;
2034 int read_mode;
2035 u64 logical;
2036
2037 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2038
2039 ret = get_state_private(failure_tree, start, &private);
2040 if (ret) {
2041 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2042 if (!failrec)
2043 return -ENOMEM;
2044 failrec->start = start;
2045 failrec->len = end - start + 1;
2046 failrec->this_mirror = 0;
2047 failrec->bio_flags = 0;
2048 failrec->in_validation = 0;
2049
2050 read_lock(&em_tree->lock);
2051 em = lookup_extent_mapping(em_tree, start, failrec->len);
2052 if (!em) {
2053 read_unlock(&em_tree->lock);
2054 kfree(failrec);
2055 return -EIO;
2056 }
2057
2058 if (em->start > start || em->start + em->len < start) {
2059 free_extent_map(em);
2060 em = NULL;
2061 }
2062 read_unlock(&em_tree->lock);
2063
2064 if (!em || IS_ERR(em)) {
2065 kfree(failrec);
2066 return -EIO;
2067 }
2068 logical = start - em->start;
2069 logical = em->block_start + logical;
2070 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2071 logical = em->block_start;
2072 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2073 extent_set_compress_type(&failrec->bio_flags,
2074 em->compress_type);
2075 }
2076 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2077 "len=%llu\n", logical, start, failrec->len);
2078 failrec->logical = logical;
2079 free_extent_map(em);
2080
2081 /* set the bits in the private failure tree */
2082 ret = set_extent_bits(failure_tree, start, end,
2083 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2084 if (ret >= 0)
2085 ret = set_state_private(failure_tree, start,
2086 (u64)(unsigned long)failrec);
2087 /* set the bits in the inode's tree */
2088 if (ret >= 0)
2089 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2090 GFP_NOFS);
2091 if (ret < 0) {
2092 kfree(failrec);
2093 return ret;
2094 }
2095 } else {
2096 failrec = (struct io_failure_record *)(unsigned long)private;
2097 pr_debug("bio_readpage_error: (found) logical=%llu, "
2098 "start=%llu, len=%llu, validation=%d\n",
2099 failrec->logical, failrec->start, failrec->len,
2100 failrec->in_validation);
2101 /*
2102 * when data can be on disk more than twice, add to failrec here
2103 * (e.g. with a list for failed_mirror) to make
2104 * clean_io_failure() clean all those errors at once.
2105 */
2106 }
2107 num_copies = btrfs_num_copies(
2108 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2109 failrec->logical, failrec->len);
2110 if (num_copies == 1) {
2111 /*
2112 * we only have a single copy of the data, so don't bother with
2113 * all the retry and error correction code that follows. no
2114 * matter what the error is, it is very likely to persist.
2115 */
2116 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2117 "state=%p, num_copies=%d, next_mirror %d, "
2118 "failed_mirror %d\n", state, num_copies,
2119 failrec->this_mirror, failed_mirror);
2120 free_io_failure(inode, failrec, 0);
2121 return -EIO;
2122 }
2123
2124 if (!state) {
2125 spin_lock(&tree->lock);
2126 state = find_first_extent_bit_state(tree, failrec->start,
2127 EXTENT_LOCKED);
2128 if (state && state->start != failrec->start)
2129 state = NULL;
2130 spin_unlock(&tree->lock);
2131 }
2132
2133 /*
2134 * there are two premises:
2135 * a) deliver good data to the caller
2136 * b) correct the bad sectors on disk
2137 */
2138 if (failed_bio->bi_vcnt > 1) {
2139 /*
2140 * to fulfill b), we need to know the exact failing sectors, as
2141 * we don't want to rewrite any more than the failed ones. thus,
2142 * we need separate read requests for the failed bio
2143 *
2144 * if the following BUG_ON triggers, our validation request got
2145 * merged. we need separate requests for our algorithm to work.
2146 */
2147 BUG_ON(failrec->in_validation);
2148 failrec->in_validation = 1;
2149 failrec->this_mirror = failed_mirror;
2150 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2151 } else {
2152 /*
2153 * we're ready to fulfill a) and b) alongside. get a good copy
2154 * of the failed sector and if we succeed, we have setup
2155 * everything for repair_io_failure to do the rest for us.
2156 */
2157 if (failrec->in_validation) {
2158 BUG_ON(failrec->this_mirror != failed_mirror);
2159 failrec->in_validation = 0;
2160 failrec->this_mirror = 0;
2161 }
2162 failrec->failed_mirror = failed_mirror;
2163 failrec->this_mirror++;
2164 if (failrec->this_mirror == failed_mirror)
2165 failrec->this_mirror++;
2166 read_mode = READ_SYNC;
2167 }
2168
2169 if (!state || failrec->this_mirror > num_copies) {
2170 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2171 "next_mirror %d, failed_mirror %d\n", state,
2172 num_copies, failrec->this_mirror, failed_mirror);
2173 free_io_failure(inode, failrec, 0);
2174 return -EIO;
2175 }
2176
2177 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002178 if (!bio) {
2179 free_io_failure(inode, failrec, 0);
2180 return -EIO;
2181 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002182 bio->bi_private = state;
2183 bio->bi_end_io = failed_bio->bi_end_io;
2184 bio->bi_sector = failrec->logical >> 9;
2185 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2186 bio->bi_size = 0;
2187
2188 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2189
2190 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2191 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2192 failrec->this_mirror, num_copies, failrec->in_validation);
2193
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002194 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2195 failrec->this_mirror,
2196 failrec->bio_flags, 0);
2197 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002198}
2199
Chris Masond1310b22008-01-24 16:13:08 -05002200/* lots and lots of room for performance fixes in the end_bio funcs */
2201
Jeff Mahoney87826df2012-02-15 16:23:57 +01002202int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2203{
2204 int uptodate = (err == 0);
2205 struct extent_io_tree *tree;
2206 int ret;
2207
2208 tree = &BTRFS_I(page->mapping->host)->io_tree;
2209
2210 if (tree->ops && tree->ops->writepage_end_io_hook) {
2211 ret = tree->ops->writepage_end_io_hook(page, start,
2212 end, NULL, uptodate);
2213 if (ret)
2214 uptodate = 0;
2215 }
2216
Jeff Mahoney87826df2012-02-15 16:23:57 +01002217 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002218 ClearPageUptodate(page);
2219 SetPageError(page);
2220 }
2221 return 0;
2222}
2223
Chris Masond1310b22008-01-24 16:13:08 -05002224/*
2225 * after a writepage IO is done, we need to:
2226 * clear the uptodate bits on error
2227 * clear the writeback bits in the extent tree for this IO
2228 * end_page_writeback if the page has no more pending IO
2229 *
2230 * Scheduling is not allowed, so the extent state tree is expected
2231 * to have one and only one object corresponding to this IO.
2232 */
Chris Masond1310b22008-01-24 16:13:08 -05002233static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002234{
Chris Masond1310b22008-01-24 16:13:08 -05002235 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002236 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002237 u64 start;
2238 u64 end;
2239 int whole_page;
2240
Chris Masond1310b22008-01-24 16:13:08 -05002241 do {
2242 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002243 tree = &BTRFS_I(page->mapping->host)->io_tree;
2244
Chris Masond1310b22008-01-24 16:13:08 -05002245 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2246 bvec->bv_offset;
2247 end = start + bvec->bv_len - 1;
2248
2249 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2250 whole_page = 1;
2251 else
2252 whole_page = 0;
2253
2254 if (--bvec >= bio->bi_io_vec)
2255 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002256
Jeff Mahoney87826df2012-02-15 16:23:57 +01002257 if (end_extent_writepage(page, err, start, end))
2258 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002259
Chris Masond1310b22008-01-24 16:13:08 -05002260 if (whole_page)
2261 end_page_writeback(page);
2262 else
2263 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002264 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002265
Chris Masond1310b22008-01-24 16:13:08 -05002266 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002267}
2268
2269/*
2270 * after a readpage IO is done, we need to:
2271 * clear the uptodate bits on error
2272 * set the uptodate bits if things worked
2273 * set the page up to date if all extents in the tree are uptodate
2274 * clear the lock bit in the extent tree
2275 * unlock the page if there are no other extents locked for it
2276 *
2277 * Scheduling is not allowed, so the extent state tree is expected
2278 * to have one and only one object corresponding to this IO.
2279 */
Chris Masond1310b22008-01-24 16:13:08 -05002280static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002281{
2282 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002283 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2284 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002285 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002286 u64 start;
2287 u64 end;
2288 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002289 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002290 int ret;
2291
Chris Masond20f7042008-12-08 16:58:54 -05002292 if (err)
2293 uptodate = 0;
2294
Chris Masond1310b22008-01-24 16:13:08 -05002295 do {
2296 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002297 struct extent_state *cached = NULL;
2298 struct extent_state *state;
2299
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002300 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2301 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2302 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002303 tree = &BTRFS_I(page->mapping->host)->io_tree;
2304
Chris Masond1310b22008-01-24 16:13:08 -05002305 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2306 bvec->bv_offset;
2307 end = start + bvec->bv_len - 1;
2308
2309 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2310 whole_page = 1;
2311 else
2312 whole_page = 0;
2313
Chris Mason4125bf72010-02-03 18:18:45 +00002314 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002315 prefetchw(&bvec->bv_page->flags);
2316
Arne Jansen507903b2011-04-06 10:02:20 +00002317 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002318 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002319 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002320 /*
2321 * take a reference on the state, unlock will drop
2322 * the ref
2323 */
2324 cache_state(state, &cached);
2325 }
2326 spin_unlock(&tree->lock);
2327
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002328 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002329 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002330 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002331 state, mirror);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002332 if (ret) {
2333 /* no IO indicated but software detected errors
2334 * in the block, either checksum errors or
2335 * issues with the contents */
2336 struct btrfs_root *root =
2337 BTRFS_I(page->mapping->host)->root;
2338 struct btrfs_device *device;
2339
Chris Masond1310b22008-01-24 16:13:08 -05002340 uptodate = 0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002341 device = btrfs_find_device_for_logical(
2342 root, start, mirror);
2343 if (device)
2344 btrfs_dev_stat_inc_and_print(device,
2345 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2346 } else {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002347 clean_io_failure(start, page);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002348 }
Chris Masond1310b22008-01-24 16:13:08 -05002349 }
Josef Bacikea466792012-03-26 21:57:36 -04002350
Josef Bacikea466792012-03-26 21:57:36 -04002351 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002352 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002353 if (!ret && !err &&
2354 test_bit(BIO_UPTODATE, &bio->bi_flags))
2355 uptodate = 1;
2356 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002357 /*
2358 * The generic bio_readpage_error handles errors the
2359 * following way: If possible, new read requests are
2360 * created and submitted and will end up in
2361 * end_bio_extent_readpage as well (if we're lucky, not
2362 * in the !uptodate case). In that case it returns 0 and
2363 * we just go on with the next page in our bio. If it
2364 * can't handle the error it will return -EIO and we
2365 * remain responsible for that page.
2366 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002367 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002368 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002369 uptodate =
2370 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002371 if (err)
2372 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002373 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002374 continue;
2375 }
2376 }
Chris Mason70dec802008-01-29 09:59:12 -05002377
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002378 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002379 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002380 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002381 }
Arne Jansen507903b2011-04-06 10:02:20 +00002382 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002383
Chris Mason70dec802008-01-29 09:59:12 -05002384 if (whole_page) {
2385 if (uptodate) {
2386 SetPageUptodate(page);
2387 } else {
2388 ClearPageUptodate(page);
2389 SetPageError(page);
2390 }
Chris Masond1310b22008-01-24 16:13:08 -05002391 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002392 } else {
2393 if (uptodate) {
2394 check_page_uptodate(tree, page);
2395 } else {
2396 ClearPageUptodate(page);
2397 SetPageError(page);
2398 }
Chris Masond1310b22008-01-24 16:13:08 -05002399 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002400 }
Chris Mason4125bf72010-02-03 18:18:45 +00002401 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002402
2403 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002404}
2405
Miao Xie88f794e2010-11-22 03:02:55 +00002406struct bio *
2407btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2408 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002409{
2410 struct bio *bio;
2411
2412 bio = bio_alloc(gfp_flags, nr_vecs);
2413
2414 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2415 while (!bio && (nr_vecs /= 2))
2416 bio = bio_alloc(gfp_flags, nr_vecs);
2417 }
2418
2419 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002420 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002421 bio->bi_bdev = bdev;
2422 bio->bi_sector = first_sector;
2423 }
2424 return bio;
2425}
2426
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002427/*
2428 * Since writes are async, they will only return -ENOMEM.
2429 * Reads can return the full range of I/O error conditions.
2430 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002431static int __must_check submit_one_bio(int rw, struct bio *bio,
2432 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002433{
Chris Masond1310b22008-01-24 16:13:08 -05002434 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002435 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2436 struct page *page = bvec->bv_page;
2437 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002438 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002439
2440 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002441
David Woodhouse902b22f2008-08-20 08:51:49 -04002442 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002443
2444 bio_get(bio);
2445
Chris Mason065631f2008-02-20 12:07:25 -05002446 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002447 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002448 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002449 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002450 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002451
Chris Masond1310b22008-01-24 16:13:08 -05002452 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2453 ret = -EOPNOTSUPP;
2454 bio_put(bio);
2455 return ret;
2456}
2457
Jeff Mahoney3444a972011-10-03 23:23:13 -04002458static int merge_bio(struct extent_io_tree *tree, struct page *page,
2459 unsigned long offset, size_t size, struct bio *bio,
2460 unsigned long bio_flags)
2461{
2462 int ret = 0;
2463 if (tree->ops && tree->ops->merge_bio_hook)
2464 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2465 bio_flags);
2466 BUG_ON(ret < 0);
2467 return ret;
2468
2469}
2470
Chris Masond1310b22008-01-24 16:13:08 -05002471static int submit_extent_page(int rw, struct extent_io_tree *tree,
2472 struct page *page, sector_t sector,
2473 size_t size, unsigned long offset,
2474 struct block_device *bdev,
2475 struct bio **bio_ret,
2476 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002477 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002478 int mirror_num,
2479 unsigned long prev_bio_flags,
2480 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002481{
2482 int ret = 0;
2483 struct bio *bio;
2484 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002485 int contig = 0;
2486 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2487 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002488 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002489
2490 if (bio_ret && *bio_ret) {
2491 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002492 if (old_compressed)
2493 contig = bio->bi_sector == sector;
2494 else
2495 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2496 sector;
2497
2498 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002499 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002500 bio_add_page(bio, page, page_size, offset) < page_size) {
2501 ret = submit_one_bio(rw, bio, mirror_num,
2502 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002503 if (ret < 0)
2504 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002505 bio = NULL;
2506 } else {
2507 return 0;
2508 }
2509 }
Chris Masonc8b97812008-10-29 14:49:59 -04002510 if (this_compressed)
2511 nr = BIO_MAX_PAGES;
2512 else
2513 nr = bio_get_nr_vecs(bdev);
2514
Miao Xie88f794e2010-11-22 03:02:55 +00002515 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002516 if (!bio)
2517 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002518
Chris Masonc8b97812008-10-29 14:49:59 -04002519 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002520 bio->bi_end_io = end_io_func;
2521 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002522
Chris Masond3977122009-01-05 21:25:51 -05002523 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002524 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002525 else
Chris Masonc8b97812008-10-29 14:49:59 -04002526 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002527
2528 return ret;
2529}
2530
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002531void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2532{
2533 if (!PagePrivate(page)) {
2534 SetPagePrivate(page);
2535 page_cache_get(page);
2536 set_page_private(page, (unsigned long)eb);
2537 } else {
2538 WARN_ON(page->private != (unsigned long)eb);
2539 }
2540}
2541
Chris Masond1310b22008-01-24 16:13:08 -05002542void set_page_extent_mapped(struct page *page)
2543{
2544 if (!PagePrivate(page)) {
2545 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002546 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002547 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002548 }
2549}
2550
Chris Masond1310b22008-01-24 16:13:08 -05002551/*
2552 * basic readpage implementation. Locked extent state structs are inserted
2553 * into the tree that are removed when the IO is done (by the end_io
2554 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002555 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002556 */
2557static int __extent_read_full_page(struct extent_io_tree *tree,
2558 struct page *page,
2559 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002560 struct bio **bio, int mirror_num,
2561 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002562{
2563 struct inode *inode = page->mapping->host;
2564 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2565 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2566 u64 end;
2567 u64 cur = start;
2568 u64 extent_offset;
2569 u64 last_byte = i_size_read(inode);
2570 u64 block_start;
2571 u64 cur_end;
2572 sector_t sector;
2573 struct extent_map *em;
2574 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002575 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002576 int ret;
2577 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002578 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002579 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002580 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002581 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002582 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002583
2584 set_page_extent_mapped(page);
2585
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002586 if (!PageUptodate(page)) {
2587 if (cleancache_get_page(page) == 0) {
2588 BUG_ON(blocksize != PAGE_SIZE);
2589 goto out;
2590 }
2591 }
2592
Chris Masond1310b22008-01-24 16:13:08 -05002593 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002594 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002595 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002596 ordered = btrfs_lookup_ordered_extent(inode, start);
2597 if (!ordered)
2598 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002599 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002600 btrfs_start_ordered_extent(inode, ordered, 1);
2601 btrfs_put_ordered_extent(ordered);
2602 }
Chris Masond1310b22008-01-24 16:13:08 -05002603
Chris Masonc8b97812008-10-29 14:49:59 -04002604 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2605 char *userpage;
2606 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2607
2608 if (zero_offset) {
2609 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002610 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002611 memset(userpage + zero_offset, 0, iosize);
2612 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002613 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002614 }
2615 }
Chris Masond1310b22008-01-24 16:13:08 -05002616 while (cur <= end) {
2617 if (cur >= last_byte) {
2618 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002619 struct extent_state *cached = NULL;
2620
David Sterba306e16c2011-04-19 14:29:38 +02002621 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002622 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002623 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002624 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002625 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002626 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002627 &cached, GFP_NOFS);
2628 unlock_extent_cached(tree, cur, cur + iosize - 1,
2629 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002630 break;
2631 }
David Sterba306e16c2011-04-19 14:29:38 +02002632 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002633 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002634 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002635 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002636 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002637 break;
2638 }
Chris Masond1310b22008-01-24 16:13:08 -05002639 extent_offset = cur - em->start;
2640 BUG_ON(extent_map_end(em) <= cur);
2641 BUG_ON(end < cur);
2642
Li Zefan261507a02010-12-17 14:21:50 +08002643 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002644 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002645 extent_set_compress_type(&this_bio_flag,
2646 em->compress_type);
2647 }
Chris Masonc8b97812008-10-29 14:49:59 -04002648
Chris Masond1310b22008-01-24 16:13:08 -05002649 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2650 cur_end = min(extent_map_end(em) - 1, end);
2651 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002652 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2653 disk_io_size = em->block_len;
2654 sector = em->block_start >> 9;
2655 } else {
2656 sector = (em->block_start + extent_offset) >> 9;
2657 disk_io_size = iosize;
2658 }
Chris Masond1310b22008-01-24 16:13:08 -05002659 bdev = em->bdev;
2660 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002661 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2662 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002663 free_extent_map(em);
2664 em = NULL;
2665
2666 /* we've found a hole, just zero and go on */
2667 if (block_start == EXTENT_MAP_HOLE) {
2668 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002669 struct extent_state *cached = NULL;
2670
Cong Wang7ac687d2011-11-25 23:14:28 +08002671 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002672 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002673 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002674 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002675
2676 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002677 &cached, GFP_NOFS);
2678 unlock_extent_cached(tree, cur, cur + iosize - 1,
2679 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002680 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002681 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002682 continue;
2683 }
2684 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002685 if (test_range_bit(tree, cur, cur_end,
2686 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002687 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002688 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002689 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002690 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002691 continue;
2692 }
Chris Mason70dec802008-01-29 09:59:12 -05002693 /* we have an inline extent but it didn't get marked up
2694 * to date. Error out
2695 */
2696 if (block_start == EXTENT_MAP_INLINE) {
2697 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002698 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002699 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002700 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002701 continue;
2702 }
Chris Masond1310b22008-01-24 16:13:08 -05002703
2704 ret = 0;
2705 if (tree->ops && tree->ops->readpage_io_hook) {
2706 ret = tree->ops->readpage_io_hook(page, cur,
2707 cur + iosize - 1);
2708 }
2709 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002710 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2711 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002712 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002713 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002714 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002715 end_bio_extent_readpage, mirror_num,
2716 *bio_flags,
2717 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002718 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002719 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002720 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002721 }
2722 if (ret)
2723 SetPageError(page);
2724 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002725 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002726 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002727out:
Chris Masond1310b22008-01-24 16:13:08 -05002728 if (!nr) {
2729 if (!PageError(page))
2730 SetPageUptodate(page);
2731 unlock_page(page);
2732 }
2733 return 0;
2734}
2735
2736int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002737 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002738{
2739 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002740 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002741 int ret;
2742
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002743 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002744 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002745 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002746 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002747 return ret;
2748}
Chris Masond1310b22008-01-24 16:13:08 -05002749
Chris Mason11c83492009-04-20 15:50:09 -04002750static noinline void update_nr_written(struct page *page,
2751 struct writeback_control *wbc,
2752 unsigned long nr_written)
2753{
2754 wbc->nr_to_write -= nr_written;
2755 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2756 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2757 page->mapping->writeback_index = page->index + nr_written;
2758}
2759
Chris Masond1310b22008-01-24 16:13:08 -05002760/*
2761 * the writepage semantics are similar to regular writepage. extent
2762 * records are inserted to lock ranges in the tree, and as dirty areas
2763 * are found, they are marked writeback. Then the lock bits are removed
2764 * and the end_io handler clears the writeback ranges
2765 */
2766static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2767 void *data)
2768{
2769 struct inode *inode = page->mapping->host;
2770 struct extent_page_data *epd = data;
2771 struct extent_io_tree *tree = epd->tree;
2772 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2773 u64 delalloc_start;
2774 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2775 u64 end;
2776 u64 cur = start;
2777 u64 extent_offset;
2778 u64 last_byte = i_size_read(inode);
2779 u64 block_start;
2780 u64 iosize;
2781 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002782 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002783 struct extent_map *em;
2784 struct block_device *bdev;
2785 int ret;
2786 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002787 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002788 size_t blocksize;
2789 loff_t i_size = i_size_read(inode);
2790 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2791 u64 nr_delalloc;
2792 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002793 int page_started;
2794 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002795 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002796 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002797 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002798
Chris Masonffbd5172009-04-20 15:50:09 -04002799 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002800 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002801 else
2802 write_flags = WRITE;
2803
liubo1abe9b82011-03-24 11:18:59 +00002804 trace___extent_writepage(page, inode, wbc);
2805
Chris Masond1310b22008-01-24 16:13:08 -05002806 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002807
2808 ClearPageError(page);
2809
Chris Mason7f3c74f2008-07-18 12:01:11 -04002810 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002811 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002812 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002813 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002814 unlock_page(page);
2815 return 0;
2816 }
2817
2818 if (page->index == end_index) {
2819 char *userpage;
2820
Cong Wang7ac687d2011-11-25 23:14:28 +08002821 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002822 memset(userpage + pg_offset, 0,
2823 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002824 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002825 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002826 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002827 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002828
2829 set_page_extent_mapped(page);
2830
Josef Bacik9e487102011-08-01 12:08:18 -04002831 if (!tree->ops || !tree->ops->fill_delalloc)
2832 fill_delalloc = false;
2833
Chris Masond1310b22008-01-24 16:13:08 -05002834 delalloc_start = start;
2835 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002836 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002837 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002838 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002839 /*
2840 * make sure the wbc mapping index is at least updated
2841 * to this page.
2842 */
2843 update_nr_written(page, wbc, 0);
2844
Chris Masond3977122009-01-05 21:25:51 -05002845 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002846 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002847 page,
2848 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002849 &delalloc_end,
2850 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002851 if (nr_delalloc == 0) {
2852 delalloc_start = delalloc_end + 1;
2853 continue;
2854 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002855 ret = tree->ops->fill_delalloc(inode, page,
2856 delalloc_start,
2857 delalloc_end,
2858 &page_started,
2859 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002860 /* File system has been set read-only */
2861 if (ret) {
2862 SetPageError(page);
2863 goto done;
2864 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002865 /*
2866 * delalloc_end is already one less than the total
2867 * length, so we don't subtract one from
2868 * PAGE_CACHE_SIZE
2869 */
2870 delalloc_to_write += (delalloc_end - delalloc_start +
2871 PAGE_CACHE_SIZE) >>
2872 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002873 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002874 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002875 if (wbc->nr_to_write < delalloc_to_write) {
2876 int thresh = 8192;
2877
2878 if (delalloc_to_write < thresh * 2)
2879 thresh = delalloc_to_write;
2880 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2881 thresh);
2882 }
Chris Masonc8b97812008-10-29 14:49:59 -04002883
Chris Mason771ed682008-11-06 22:02:51 -05002884 /* did the fill delalloc function already unlock and start
2885 * the IO?
2886 */
2887 if (page_started) {
2888 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002889 /*
2890 * we've unlocked the page, so we can't update
2891 * the mapping's writeback index, just update
2892 * nr_to_write.
2893 */
2894 wbc->nr_to_write -= nr_written;
2895 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002896 }
Chris Masonc8b97812008-10-29 14:49:59 -04002897 }
Chris Mason247e7432008-07-17 12:53:51 -04002898 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002899 ret = tree->ops->writepage_start_hook(page, start,
2900 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002901 if (ret) {
2902 /* Fixup worker will requeue */
2903 if (ret == -EBUSY)
2904 wbc->pages_skipped++;
2905 else
2906 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002907 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002908 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002909 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002910 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002911 }
2912 }
2913
Chris Mason11c83492009-04-20 15:50:09 -04002914 /*
2915 * we don't want to touch the inode after unlocking the page,
2916 * so we update the mapping writeback index now
2917 */
2918 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002919
Chris Masond1310b22008-01-24 16:13:08 -05002920 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002921 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922 if (tree->ops && tree->ops->writepage_end_io_hook)
2923 tree->ops->writepage_end_io_hook(page, start,
2924 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002925 goto done;
2926 }
2927
Chris Masond1310b22008-01-24 16:13:08 -05002928 blocksize = inode->i_sb->s_blocksize;
2929
2930 while (cur <= end) {
2931 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002932 if (tree->ops && tree->ops->writepage_end_io_hook)
2933 tree->ops->writepage_end_io_hook(page, cur,
2934 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002935 break;
2936 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002937 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002938 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002939 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002940 SetPageError(page);
2941 break;
2942 }
2943
2944 extent_offset = cur - em->start;
2945 BUG_ON(extent_map_end(em) <= cur);
2946 BUG_ON(end < cur);
2947 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2948 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2949 sector = (em->block_start + extent_offset) >> 9;
2950 bdev = em->bdev;
2951 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002952 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002953 free_extent_map(em);
2954 em = NULL;
2955
Chris Masonc8b97812008-10-29 14:49:59 -04002956 /*
2957 * compressed and inline extents are written through other
2958 * paths in the FS
2959 */
2960 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002961 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002962 /*
2963 * end_io notification does not happen here for
2964 * compressed extents
2965 */
2966 if (!compressed && tree->ops &&
2967 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002968 tree->ops->writepage_end_io_hook(page, cur,
2969 cur + iosize - 1,
2970 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002971 else if (compressed) {
2972 /* we don't want to end_page_writeback on
2973 * a compressed extent. this happens
2974 * elsewhere
2975 */
2976 nr++;
2977 }
2978
2979 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002980 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002981 continue;
2982 }
Chris Masond1310b22008-01-24 16:13:08 -05002983 /* leave this out until we have a page_mkwrite call */
2984 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002985 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002986 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002987 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002988 continue;
2989 }
Chris Masonc8b97812008-10-29 14:49:59 -04002990
Chris Masond1310b22008-01-24 16:13:08 -05002991 if (tree->ops && tree->ops->writepage_io_hook) {
2992 ret = tree->ops->writepage_io_hook(page, cur,
2993 cur + iosize - 1);
2994 } else {
2995 ret = 0;
2996 }
Chris Mason1259ab72008-05-12 13:39:03 -04002997 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002998 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002999 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003000 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003001
Chris Masond1310b22008-01-24 16:13:08 -05003002 set_range_writeback(tree, cur, cur + iosize - 1);
3003 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003004 printk(KERN_ERR "btrfs warning page %lu not "
3005 "writeback, cur %llu end %llu\n",
3006 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003007 (unsigned long long)end);
3008 }
3009
Chris Masonffbd5172009-04-20 15:50:09 -04003010 ret = submit_extent_page(write_flags, tree, page,
3011 sector, iosize, pg_offset,
3012 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003013 end_bio_extent_writepage,
3014 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003015 if (ret)
3016 SetPageError(page);
3017 }
3018 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003019 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003020 nr++;
3021 }
3022done:
3023 if (nr == 0) {
3024 /* make sure the mapping tag for page dirty gets cleared */
3025 set_page_writeback(page);
3026 end_page_writeback(page);
3027 }
Chris Masond1310b22008-01-24 16:13:08 -05003028 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003029
Chris Mason11c83492009-04-20 15:50:09 -04003030done_unlocked:
3031
Chris Mason2c64c532009-09-02 15:04:12 -04003032 /* drop our reference on any cached states */
3033 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003034 return 0;
3035}
3036
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003037static int eb_wait(void *word)
3038{
3039 io_schedule();
3040 return 0;
3041}
3042
3043static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3044{
3045 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3046 TASK_UNINTERRUPTIBLE);
3047}
3048
3049static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3050 struct btrfs_fs_info *fs_info,
3051 struct extent_page_data *epd)
3052{
3053 unsigned long i, num_pages;
3054 int flush = 0;
3055 int ret = 0;
3056
3057 if (!btrfs_try_tree_write_lock(eb)) {
3058 flush = 1;
3059 flush_write_bio(epd);
3060 btrfs_tree_lock(eb);
3061 }
3062
3063 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3064 btrfs_tree_unlock(eb);
3065 if (!epd->sync_io)
3066 return 0;
3067 if (!flush) {
3068 flush_write_bio(epd);
3069 flush = 1;
3070 }
Chris Masona098d8e2012-03-21 12:09:56 -04003071 while (1) {
3072 wait_on_extent_buffer_writeback(eb);
3073 btrfs_tree_lock(eb);
3074 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3075 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003076 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003077 }
3078 }
3079
Josef Bacik51561ff2012-07-20 16:25:24 -04003080 /*
3081 * We need to do this to prevent races in people who check if the eb is
3082 * under IO since we can end up having no IO bits set for a short period
3083 * of time.
3084 */
3085 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003086 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3087 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003088 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003089 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3090 spin_lock(&fs_info->delalloc_lock);
3091 if (fs_info->dirty_metadata_bytes >= eb->len)
3092 fs_info->dirty_metadata_bytes -= eb->len;
3093 else
3094 WARN_ON(1);
3095 spin_unlock(&fs_info->delalloc_lock);
3096 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003097 } else {
3098 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003099 }
3100
3101 btrfs_tree_unlock(eb);
3102
3103 if (!ret)
3104 return ret;
3105
3106 num_pages = num_extent_pages(eb->start, eb->len);
3107 for (i = 0; i < num_pages; i++) {
3108 struct page *p = extent_buffer_page(eb, i);
3109
3110 if (!trylock_page(p)) {
3111 if (!flush) {
3112 flush_write_bio(epd);
3113 flush = 1;
3114 }
3115 lock_page(p);
3116 }
3117 }
3118
3119 return ret;
3120}
3121
3122static void end_extent_buffer_writeback(struct extent_buffer *eb)
3123{
3124 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3125 smp_mb__after_clear_bit();
3126 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3127}
3128
3129static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3130{
3131 int uptodate = err == 0;
3132 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3133 struct extent_buffer *eb;
3134 int done;
3135
3136 do {
3137 struct page *page = bvec->bv_page;
3138
3139 bvec--;
3140 eb = (struct extent_buffer *)page->private;
3141 BUG_ON(!eb);
3142 done = atomic_dec_and_test(&eb->io_pages);
3143
3144 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3145 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3146 ClearPageUptodate(page);
3147 SetPageError(page);
3148 }
3149
3150 end_page_writeback(page);
3151
3152 if (!done)
3153 continue;
3154
3155 end_extent_buffer_writeback(eb);
3156 } while (bvec >= bio->bi_io_vec);
3157
3158 bio_put(bio);
3159
3160}
3161
3162static int write_one_eb(struct extent_buffer *eb,
3163 struct btrfs_fs_info *fs_info,
3164 struct writeback_control *wbc,
3165 struct extent_page_data *epd)
3166{
3167 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3168 u64 offset = eb->start;
3169 unsigned long i, num_pages;
3170 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003171 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003172
3173 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3174 num_pages = num_extent_pages(eb->start, eb->len);
3175 atomic_set(&eb->io_pages, num_pages);
3176 for (i = 0; i < num_pages; i++) {
3177 struct page *p = extent_buffer_page(eb, i);
3178
3179 clear_page_dirty_for_io(p);
3180 set_page_writeback(p);
3181 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3182 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3183 -1, end_bio_extent_buffer_writepage,
3184 0, 0, 0);
3185 if (ret) {
3186 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3187 SetPageError(p);
3188 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3189 end_extent_buffer_writeback(eb);
3190 ret = -EIO;
3191 break;
3192 }
3193 offset += PAGE_CACHE_SIZE;
3194 update_nr_written(p, wbc, 1);
3195 unlock_page(p);
3196 }
3197
3198 if (unlikely(ret)) {
3199 for (; i < num_pages; i++) {
3200 struct page *p = extent_buffer_page(eb, i);
3201 unlock_page(p);
3202 }
3203 }
3204
3205 return ret;
3206}
3207
3208int btree_write_cache_pages(struct address_space *mapping,
3209 struct writeback_control *wbc)
3210{
3211 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3212 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3213 struct extent_buffer *eb, *prev_eb = NULL;
3214 struct extent_page_data epd = {
3215 .bio = NULL,
3216 .tree = tree,
3217 .extent_locked = 0,
3218 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3219 };
3220 int ret = 0;
3221 int done = 0;
3222 int nr_to_write_done = 0;
3223 struct pagevec pvec;
3224 int nr_pages;
3225 pgoff_t index;
3226 pgoff_t end; /* Inclusive */
3227 int scanned = 0;
3228 int tag;
3229
3230 pagevec_init(&pvec, 0);
3231 if (wbc->range_cyclic) {
3232 index = mapping->writeback_index; /* Start from prev offset */
3233 end = -1;
3234 } else {
3235 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3236 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3237 scanned = 1;
3238 }
3239 if (wbc->sync_mode == WB_SYNC_ALL)
3240 tag = PAGECACHE_TAG_TOWRITE;
3241 else
3242 tag = PAGECACHE_TAG_DIRTY;
3243retry:
3244 if (wbc->sync_mode == WB_SYNC_ALL)
3245 tag_pages_for_writeback(mapping, index, end);
3246 while (!done && !nr_to_write_done && (index <= end) &&
3247 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3248 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3249 unsigned i;
3250
3251 scanned = 1;
3252 for (i = 0; i < nr_pages; i++) {
3253 struct page *page = pvec.pages[i];
3254
3255 if (!PagePrivate(page))
3256 continue;
3257
3258 if (!wbc->range_cyclic && page->index > end) {
3259 done = 1;
3260 break;
3261 }
3262
3263 eb = (struct extent_buffer *)page->private;
3264 if (!eb) {
3265 WARN_ON(1);
3266 continue;
3267 }
3268
3269 if (eb == prev_eb)
3270 continue;
3271
3272 if (!atomic_inc_not_zero(&eb->refs)) {
3273 WARN_ON(1);
3274 continue;
3275 }
3276
3277 prev_eb = eb;
3278 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3279 if (!ret) {
3280 free_extent_buffer(eb);
3281 continue;
3282 }
3283
3284 ret = write_one_eb(eb, fs_info, wbc, &epd);
3285 if (ret) {
3286 done = 1;
3287 free_extent_buffer(eb);
3288 break;
3289 }
3290 free_extent_buffer(eb);
3291
3292 /*
3293 * the filesystem may choose to bump up nr_to_write.
3294 * We have to make sure to honor the new nr_to_write
3295 * at any time
3296 */
3297 nr_to_write_done = wbc->nr_to_write <= 0;
3298 }
3299 pagevec_release(&pvec);
3300 cond_resched();
3301 }
3302 if (!scanned && !done) {
3303 /*
3304 * We hit the last page and there is more work to be done: wrap
3305 * back to the start of the file
3306 */
3307 scanned = 1;
3308 index = 0;
3309 goto retry;
3310 }
3311 flush_write_bio(&epd);
3312 return ret;
3313}
3314
Chris Masond1310b22008-01-24 16:13:08 -05003315/**
Chris Mason4bef0842008-09-08 11:18:08 -04003316 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05003317 * @mapping: address space structure to write
3318 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3319 * @writepage: function called for each page
3320 * @data: data passed to writepage function
3321 *
3322 * If a page is already under I/O, write_cache_pages() skips it, even
3323 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3324 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3325 * and msync() need to guarantee that all the data which was dirty at the time
3326 * the call was made get new I/O started against them. If wbc->sync_mode is
3327 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3328 * existing IO to complete.
3329 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003330static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003331 struct address_space *mapping,
3332 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003333 writepage_t writepage, void *data,
3334 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003335{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003336 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003337 int ret = 0;
3338 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003339 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003340 struct pagevec pvec;
3341 int nr_pages;
3342 pgoff_t index;
3343 pgoff_t end; /* Inclusive */
3344 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003345 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003346
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003347 /*
3348 * We have to hold onto the inode so that ordered extents can do their
3349 * work when the IO finishes. The alternative to this is failing to add
3350 * an ordered extent if the igrab() fails there and that is a huge pain
3351 * to deal with, so instead just hold onto the inode throughout the
3352 * writepages operation. If it fails here we are freeing up the inode
3353 * anyway and we'd rather not waste our time writing out stuff that is
3354 * going to be truncated anyway.
3355 */
3356 if (!igrab(inode))
3357 return 0;
3358
Chris Masond1310b22008-01-24 16:13:08 -05003359 pagevec_init(&pvec, 0);
3360 if (wbc->range_cyclic) {
3361 index = mapping->writeback_index; /* Start from prev offset */
3362 end = -1;
3363 } else {
3364 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3365 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003366 scanned = 1;
3367 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003368 if (wbc->sync_mode == WB_SYNC_ALL)
3369 tag = PAGECACHE_TAG_TOWRITE;
3370 else
3371 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003372retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003373 if (wbc->sync_mode == WB_SYNC_ALL)
3374 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003375 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003376 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3377 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003378 unsigned i;
3379
3380 scanned = 1;
3381 for (i = 0; i < nr_pages; i++) {
3382 struct page *page = pvec.pages[i];
3383
3384 /*
3385 * At this point we hold neither mapping->tree_lock nor
3386 * lock on the page itself: the page may be truncated or
3387 * invalidated (changing page->mapping to NULL), or even
3388 * swizzled back from swapper_space to tmpfs file
3389 * mapping
3390 */
Chris Mason01d658f2011-11-01 10:08:06 -04003391 if (tree->ops &&
3392 tree->ops->write_cache_pages_lock_hook) {
3393 tree->ops->write_cache_pages_lock_hook(page,
3394 data, flush_fn);
3395 } else {
3396 if (!trylock_page(page)) {
3397 flush_fn(data);
3398 lock_page(page);
3399 }
3400 }
Chris Masond1310b22008-01-24 16:13:08 -05003401
3402 if (unlikely(page->mapping != mapping)) {
3403 unlock_page(page);
3404 continue;
3405 }
3406
3407 if (!wbc->range_cyclic && page->index > end) {
3408 done = 1;
3409 unlock_page(page);
3410 continue;
3411 }
3412
Chris Masond2c3f4f2008-11-19 12:44:22 -05003413 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003414 if (PageWriteback(page))
3415 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003416 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003417 }
Chris Masond1310b22008-01-24 16:13:08 -05003418
3419 if (PageWriteback(page) ||
3420 !clear_page_dirty_for_io(page)) {
3421 unlock_page(page);
3422 continue;
3423 }
3424
3425 ret = (*writepage)(page, wbc, data);
3426
3427 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3428 unlock_page(page);
3429 ret = 0;
3430 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003431 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003432 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003433
3434 /*
3435 * the filesystem may choose to bump up nr_to_write.
3436 * We have to make sure to honor the new nr_to_write
3437 * at any time
3438 */
3439 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003440 }
3441 pagevec_release(&pvec);
3442 cond_resched();
3443 }
3444 if (!scanned && !done) {
3445 /*
3446 * We hit the last page and there is more work to be done: wrap
3447 * back to the start of the file
3448 */
3449 scanned = 1;
3450 index = 0;
3451 goto retry;
3452 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003453 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003454 return ret;
3455}
Chris Masond1310b22008-01-24 16:13:08 -05003456
Chris Masonffbd5172009-04-20 15:50:09 -04003457static void flush_epd_write_bio(struct extent_page_data *epd)
3458{
3459 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003460 int rw = WRITE;
3461 int ret;
3462
Chris Masonffbd5172009-04-20 15:50:09 -04003463 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003464 rw = WRITE_SYNC;
3465
3466 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003467 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003468 epd->bio = NULL;
3469 }
3470}
3471
Chris Masond2c3f4f2008-11-19 12:44:22 -05003472static noinline void flush_write_bio(void *data)
3473{
3474 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003475 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003476}
3477
Chris Masond1310b22008-01-24 16:13:08 -05003478int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3479 get_extent_t *get_extent,
3480 struct writeback_control *wbc)
3481{
3482 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003483 struct extent_page_data epd = {
3484 .bio = NULL,
3485 .tree = tree,
3486 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003487 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003488 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003489 };
Chris Masond1310b22008-01-24 16:13:08 -05003490
Chris Masond1310b22008-01-24 16:13:08 -05003491 ret = __extent_writepage(page, wbc, &epd);
3492
Chris Masonffbd5172009-04-20 15:50:09 -04003493 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003494 return ret;
3495}
Chris Masond1310b22008-01-24 16:13:08 -05003496
Chris Mason771ed682008-11-06 22:02:51 -05003497int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3498 u64 start, u64 end, get_extent_t *get_extent,
3499 int mode)
3500{
3501 int ret = 0;
3502 struct address_space *mapping = inode->i_mapping;
3503 struct page *page;
3504 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3505 PAGE_CACHE_SHIFT;
3506
3507 struct extent_page_data epd = {
3508 .bio = NULL,
3509 .tree = tree,
3510 .get_extent = get_extent,
3511 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003512 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003513 };
3514 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003515 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003516 .nr_to_write = nr_pages * 2,
3517 .range_start = start,
3518 .range_end = end + 1,
3519 };
3520
Chris Masond3977122009-01-05 21:25:51 -05003521 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003522 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3523 if (clear_page_dirty_for_io(page))
3524 ret = __extent_writepage(page, &wbc_writepages, &epd);
3525 else {
3526 if (tree->ops && tree->ops->writepage_end_io_hook)
3527 tree->ops->writepage_end_io_hook(page, start,
3528 start + PAGE_CACHE_SIZE - 1,
3529 NULL, 1);
3530 unlock_page(page);
3531 }
3532 page_cache_release(page);
3533 start += PAGE_CACHE_SIZE;
3534 }
3535
Chris Masonffbd5172009-04-20 15:50:09 -04003536 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003537 return ret;
3538}
Chris Masond1310b22008-01-24 16:13:08 -05003539
3540int extent_writepages(struct extent_io_tree *tree,
3541 struct address_space *mapping,
3542 get_extent_t *get_extent,
3543 struct writeback_control *wbc)
3544{
3545 int ret = 0;
3546 struct extent_page_data epd = {
3547 .bio = NULL,
3548 .tree = tree,
3549 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003550 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003551 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003552 };
3553
Chris Mason4bef0842008-09-08 11:18:08 -04003554 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003555 __extent_writepage, &epd,
3556 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003557 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003558 return ret;
3559}
Chris Masond1310b22008-01-24 16:13:08 -05003560
3561int extent_readpages(struct extent_io_tree *tree,
3562 struct address_space *mapping,
3563 struct list_head *pages, unsigned nr_pages,
3564 get_extent_t get_extent)
3565{
3566 struct bio *bio = NULL;
3567 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003568 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003569 struct page *pagepool[16];
3570 struct page *page;
3571 int i = 0;
3572 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003573
Chris Masond1310b22008-01-24 16:13:08 -05003574 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003575 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003576
3577 prefetchw(&page->flags);
3578 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003579 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003580 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003581 page_cache_release(page);
3582 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003583 }
Liu Bo67c96842012-07-20 21:43:09 -06003584
3585 pagepool[nr++] = page;
3586 if (nr < ARRAY_SIZE(pagepool))
3587 continue;
3588 for (i = 0; i < nr; i++) {
3589 __extent_read_full_page(tree, pagepool[i], get_extent,
3590 &bio, 0, &bio_flags);
3591 page_cache_release(pagepool[i]);
3592 }
3593 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003594 }
Liu Bo67c96842012-07-20 21:43:09 -06003595 for (i = 0; i < nr; i++) {
3596 __extent_read_full_page(tree, pagepool[i], get_extent,
3597 &bio, 0, &bio_flags);
3598 page_cache_release(pagepool[i]);
3599 }
3600
Chris Masond1310b22008-01-24 16:13:08 -05003601 BUG_ON(!list_empty(pages));
3602 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003603 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003604 return 0;
3605}
Chris Masond1310b22008-01-24 16:13:08 -05003606
3607/*
3608 * basic invalidatepage code, this waits on any locked or writeback
3609 * ranges corresponding to the page, and then deletes any extent state
3610 * records from the tree
3611 */
3612int extent_invalidatepage(struct extent_io_tree *tree,
3613 struct page *page, unsigned long offset)
3614{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003615 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003616 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3617 u64 end = start + PAGE_CACHE_SIZE - 1;
3618 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3619
Chris Masond3977122009-01-05 21:25:51 -05003620 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003621 if (start > end)
3622 return 0;
3623
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003624 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003625 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003626 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003627 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3628 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003629 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003630 return 0;
3631}
Chris Masond1310b22008-01-24 16:13:08 -05003632
3633/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003634 * a helper for releasepage, this tests for areas of the page that
3635 * are locked or under IO and drops the related state bits if it is safe
3636 * to drop the page.
3637 */
3638int try_release_extent_state(struct extent_map_tree *map,
3639 struct extent_io_tree *tree, struct page *page,
3640 gfp_t mask)
3641{
3642 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3643 u64 end = start + PAGE_CACHE_SIZE - 1;
3644 int ret = 1;
3645
Chris Mason211f90e2008-07-18 11:56:15 -04003646 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003647 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003648 ret = 0;
3649 else {
3650 if ((mask & GFP_NOFS) == GFP_NOFS)
3651 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003652 /*
3653 * at this point we can safely clear everything except the
3654 * locked bit and the nodatasum bit
3655 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003656 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003657 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3658 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003659
3660 /* if clear_extent_bit failed for enomem reasons,
3661 * we can't allow the release to continue.
3662 */
3663 if (ret < 0)
3664 ret = 0;
3665 else
3666 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003667 }
3668 return ret;
3669}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003670
3671/*
Chris Masond1310b22008-01-24 16:13:08 -05003672 * a helper for releasepage. As long as there are no locked extents
3673 * in the range corresponding to the page, both state records and extent
3674 * map records are removed
3675 */
3676int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003677 struct extent_io_tree *tree, struct page *page,
3678 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003679{
3680 struct extent_map *em;
3681 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3682 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003683
Chris Mason70dec802008-01-29 09:59:12 -05003684 if ((mask & __GFP_WAIT) &&
3685 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003686 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003687 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003688 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003689 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003690 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003691 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003692 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003693 break;
3694 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003695 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3696 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003697 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003698 free_extent_map(em);
3699 break;
3700 }
3701 if (!test_range_bit(tree, em->start,
3702 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003703 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003704 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003705 remove_extent_mapping(map, em);
3706 /* once for the rb tree */
3707 free_extent_map(em);
3708 }
3709 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003710 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003711
3712 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003713 free_extent_map(em);
3714 }
Chris Masond1310b22008-01-24 16:13:08 -05003715 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003716 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003717}
Chris Masond1310b22008-01-24 16:13:08 -05003718
Chris Masonec29ed52011-02-23 16:23:20 -05003719/*
3720 * helper function for fiemap, which doesn't want to see any holes.
3721 * This maps until we find something past 'last'
3722 */
3723static struct extent_map *get_extent_skip_holes(struct inode *inode,
3724 u64 offset,
3725 u64 last,
3726 get_extent_t *get_extent)
3727{
3728 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3729 struct extent_map *em;
3730 u64 len;
3731
3732 if (offset >= last)
3733 return NULL;
3734
3735 while(1) {
3736 len = last - offset;
3737 if (len == 0)
3738 break;
3739 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3740 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003741 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003742 return em;
3743
3744 /* if this isn't a hole return it */
3745 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3746 em->block_start != EXTENT_MAP_HOLE) {
3747 return em;
3748 }
3749
3750 /* this is a hole, advance to the next extent */
3751 offset = extent_map_end(em);
3752 free_extent_map(em);
3753 if (offset >= last)
3754 break;
3755 }
3756 return NULL;
3757}
3758
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003759int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3760 __u64 start, __u64 len, get_extent_t *get_extent)
3761{
Josef Bacik975f84f2010-11-23 19:36:57 +00003762 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003763 u64 off = start;
3764 u64 max = start + len;
3765 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003766 u32 found_type;
3767 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003768 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003769 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003770 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003771 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003772 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003773 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003774 struct btrfs_path *path;
3775 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003776 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003777 u64 em_start = 0;
3778 u64 em_len = 0;
3779 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003780 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003781
3782 if (len == 0)
3783 return -EINVAL;
3784
Josef Bacik975f84f2010-11-23 19:36:57 +00003785 path = btrfs_alloc_path();
3786 if (!path)
3787 return -ENOMEM;
3788 path->leave_spinning = 1;
3789
Josef Bacik4d479cf2011-11-17 11:34:31 -05003790 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3791 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3792
Chris Masonec29ed52011-02-23 16:23:20 -05003793 /*
3794 * lookup the last file extent. We're not using i_size here
3795 * because there might be preallocation past i_size
3796 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003797 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003798 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003799 if (ret < 0) {
3800 btrfs_free_path(path);
3801 return ret;
3802 }
3803 WARN_ON(!ret);
3804 path->slots[0]--;
3805 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3806 struct btrfs_file_extent_item);
3807 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3808 found_type = btrfs_key_type(&found_key);
3809
Chris Masonec29ed52011-02-23 16:23:20 -05003810 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003811 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003812 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003813 /* have to trust i_size as the end */
3814 last = (u64)-1;
3815 last_for_get_extent = isize;
3816 } else {
3817 /*
3818 * remember the start of the last extent. There are a
3819 * bunch of different factors that go into the length of the
3820 * extent, so its much less complex to remember where it started
3821 */
3822 last = found_key.offset;
3823 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003824 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003825 btrfs_free_path(path);
3826
Chris Masonec29ed52011-02-23 16:23:20 -05003827 /*
3828 * we might have some extents allocated but more delalloc past those
3829 * extents. so, we trust isize unless the start of the last extent is
3830 * beyond isize
3831 */
3832 if (last < isize) {
3833 last = (u64)-1;
3834 last_for_get_extent = isize;
3835 }
3836
Josef Bacik2ac55d42010-02-03 19:33:23 +00003837 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003838 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003839
Josef Bacik4d479cf2011-11-17 11:34:31 -05003840 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003841 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003842 if (!em)
3843 goto out;
3844 if (IS_ERR(em)) {
3845 ret = PTR_ERR(em);
3846 goto out;
3847 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003848
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003849 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003850 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003851
Chris Masonea8efc72011-03-08 11:54:40 -05003852 /* break if the extent we found is outside the range */
3853 if (em->start >= max || extent_map_end(em) < off)
3854 break;
3855
3856 /*
3857 * get_extent may return an extent that starts before our
3858 * requested range. We have to make sure the ranges
3859 * we return to fiemap always move forward and don't
3860 * overlap, so adjust the offsets here
3861 */
3862 em_start = max(em->start, off);
3863
3864 /*
3865 * record the offset from the start of the extent
3866 * for adjusting the disk offset below
3867 */
3868 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003869 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003870 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003871 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003872 disko = 0;
3873 flags = 0;
3874
Chris Masonea8efc72011-03-08 11:54:40 -05003875 /*
3876 * bump off for our next call to get_extent
3877 */
3878 off = extent_map_end(em);
3879 if (off >= max)
3880 end = 1;
3881
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003882 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003883 end = 1;
3884 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003885 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003886 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3887 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003888 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003889 flags |= (FIEMAP_EXTENT_DELALLOC |
3890 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003891 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003892 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003893 }
3894 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3895 flags |= FIEMAP_EXTENT_ENCODED;
3896
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003897 free_extent_map(em);
3898 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003899 if ((em_start >= last) || em_len == (u64)-1 ||
3900 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003901 flags |= FIEMAP_EXTENT_LAST;
3902 end = 1;
3903 }
3904
Chris Masonec29ed52011-02-23 16:23:20 -05003905 /* now scan forward to see if this is really the last extent. */
3906 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3907 get_extent);
3908 if (IS_ERR(em)) {
3909 ret = PTR_ERR(em);
3910 goto out;
3911 }
3912 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003913 flags |= FIEMAP_EXTENT_LAST;
3914 end = 1;
3915 }
Chris Masonec29ed52011-02-23 16:23:20 -05003916 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3917 em_len, flags);
3918 if (ret)
3919 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003920 }
3921out_free:
3922 free_extent_map(em);
3923out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003924 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3925 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003926 return ret;
3927}
3928
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003929inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003930 unsigned long i)
3931{
Chris Mason727011e2010-08-06 13:21:20 -04003932 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003933}
3934
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003935inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003936{
Chris Mason6af118ce2008-07-22 11:18:07 -04003937 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3938 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003939}
3940
Chris Mason727011e2010-08-06 13:21:20 -04003941static void __free_extent_buffer(struct extent_buffer *eb)
3942{
3943#if LEAK_DEBUG
3944 unsigned long flags;
3945 spin_lock_irqsave(&leak_lock, flags);
3946 list_del(&eb->leak_list);
3947 spin_unlock_irqrestore(&leak_lock, flags);
3948#endif
3949 if (eb->pages && eb->pages != eb->inline_pages)
3950 kfree(eb->pages);
3951 kmem_cache_free(extent_buffer_cache, eb);
3952}
3953
Chris Masond1310b22008-01-24 16:13:08 -05003954static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3955 u64 start,
3956 unsigned long len,
3957 gfp_t mask)
3958{
3959 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003960#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003961 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003962#endif
Chris Masond1310b22008-01-24 16:13:08 -05003963
Chris Masond1310b22008-01-24 16:13:08 -05003964 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003965 if (eb == NULL)
3966 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003967 eb->start = start;
3968 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003969 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02003970 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003971 rwlock_init(&eb->lock);
3972 atomic_set(&eb->write_locks, 0);
3973 atomic_set(&eb->read_locks, 0);
3974 atomic_set(&eb->blocking_readers, 0);
3975 atomic_set(&eb->blocking_writers, 0);
3976 atomic_set(&eb->spinning_readers, 0);
3977 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003978 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003979 init_waitqueue_head(&eb->write_lock_wq);
3980 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003981
Chris Mason39351272009-02-04 09:24:05 -05003982#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003983 spin_lock_irqsave(&leak_lock, flags);
3984 list_add(&eb->leak_list, &buffers);
3985 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003986#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003987 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003988 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003989 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003990
3991 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3992 struct page **pages;
3993 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3994 PAGE_CACHE_SHIFT;
3995 pages = kzalloc(num_pages, mask);
3996 if (!pages) {
3997 __free_extent_buffer(eb);
3998 return NULL;
3999 }
4000 eb->pages = pages;
4001 } else {
4002 eb->pages = eb->inline_pages;
4003 }
Chris Masond1310b22008-01-24 16:13:08 -05004004
4005 return eb;
4006}
4007
Jan Schmidt815a51c2012-05-16 17:00:02 +02004008struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4009{
4010 unsigned long i;
4011 struct page *p;
4012 struct extent_buffer *new;
4013 unsigned long num_pages = num_extent_pages(src->start, src->len);
4014
4015 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4016 if (new == NULL)
4017 return NULL;
4018
4019 for (i = 0; i < num_pages; i++) {
4020 p = alloc_page(GFP_ATOMIC);
4021 BUG_ON(!p);
4022 attach_extent_buffer_page(new, p);
4023 WARN_ON(PageDirty(p));
4024 SetPageUptodate(p);
4025 new->pages[i] = p;
4026 }
4027
4028 copy_extent_buffer(new, src, 0, 0, src->len);
4029 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4030 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4031
4032 return new;
4033}
4034
4035struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4036{
4037 struct extent_buffer *eb;
4038 unsigned long num_pages = num_extent_pages(0, len);
4039 unsigned long i;
4040
4041 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4042 if (!eb)
4043 return NULL;
4044
4045 for (i = 0; i < num_pages; i++) {
4046 eb->pages[i] = alloc_page(GFP_ATOMIC);
4047 if (!eb->pages[i])
4048 goto err;
4049 }
4050 set_extent_buffer_uptodate(eb);
4051 btrfs_set_header_nritems(eb, 0);
4052 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4053
4054 return eb;
4055err:
4056 for (i--; i > 0; i--)
4057 __free_page(eb->pages[i]);
4058 __free_extent_buffer(eb);
4059 return NULL;
4060}
4061
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004062static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004063{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004064 return (atomic_read(&eb->io_pages) ||
4065 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4066 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004067}
4068
Miao Xie897ca6e92010-10-26 20:57:29 -04004069/*
4070 * Helper for releasing extent buffer page.
4071 */
4072static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4073 unsigned long start_idx)
4074{
4075 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004076 unsigned long num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004077 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004078 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004079
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004080 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004081
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004082 num_pages = num_extent_pages(eb->start, eb->len);
4083 index = start_idx + num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004084 if (start_idx >= index)
4085 return;
4086
4087 do {
4088 index--;
4089 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004090 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004091 spin_lock(&page->mapping->private_lock);
4092 /*
4093 * We do this since we'll remove the pages after we've
4094 * removed the eb from the radix tree, so we could race
4095 * and have this page now attached to the new eb. So
4096 * only clear page_private if it's still connected to
4097 * this eb.
4098 */
4099 if (PagePrivate(page) &&
4100 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004101 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004102 BUG_ON(PageDirty(page));
4103 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004104 /*
4105 * We need to make sure we haven't be attached
4106 * to a new eb.
4107 */
4108 ClearPagePrivate(page);
4109 set_page_private(page, 0);
4110 /* One for the page private */
4111 page_cache_release(page);
4112 }
4113 spin_unlock(&page->mapping->private_lock);
4114
Jan Schmidt815a51c2012-05-16 17:00:02 +02004115 }
4116 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004117 /* One for when we alloced the page */
Miao Xie897ca6e92010-10-26 20:57:29 -04004118 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004119 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004120 } while (index != start_idx);
4121}
4122
4123/*
4124 * Helper for releasing the extent buffer.
4125 */
4126static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4127{
4128 btrfs_release_extent_buffer_page(eb, 0);
4129 __free_extent_buffer(eb);
4130}
4131
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004132static void check_buffer_tree_ref(struct extent_buffer *eb)
4133{
4134 /* the ref bit is tricky. We have to make sure it is set
4135 * if we have the buffer dirty. Otherwise the
4136 * code to free a buffer can end up dropping a dirty
4137 * page
4138 *
4139 * Once the ref bit is set, it won't go away while the
4140 * buffer is dirty or in writeback, and it also won't
4141 * go away while we have the reference count on the
4142 * eb bumped.
4143 *
4144 * We can't just set the ref bit without bumping the
4145 * ref on the eb because free_extent_buffer might
4146 * see the ref bit and try to clear it. If this happens
4147 * free_extent_buffer might end up dropping our original
4148 * ref by mistake and freeing the page before we are able
4149 * to add one more ref.
4150 *
4151 * So bump the ref count first, then set the bit. If someone
4152 * beat us to it, drop the ref we added.
4153 */
Josef Bacik594831c2012-07-20 16:11:08 -04004154 spin_lock(&eb->refs_lock);
4155 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004156 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004157 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004158}
4159
Josef Bacik5df42352012-03-15 18:24:42 -04004160static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4161{
4162 unsigned long num_pages, i;
4163
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004164 check_buffer_tree_ref(eb);
4165
Josef Bacik5df42352012-03-15 18:24:42 -04004166 num_pages = num_extent_pages(eb->start, eb->len);
4167 for (i = 0; i < num_pages; i++) {
4168 struct page *p = extent_buffer_page(eb, i);
4169 mark_page_accessed(p);
4170 }
4171}
4172
Chris Masond1310b22008-01-24 16:13:08 -05004173struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004174 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004175{
4176 unsigned long num_pages = num_extent_pages(start, len);
4177 unsigned long i;
4178 unsigned long index = start >> PAGE_CACHE_SHIFT;
4179 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004180 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004181 struct page *p;
4182 struct address_space *mapping = tree->mapping;
4183 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004184 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004185
Miao Xie19fe0a82010-10-26 20:57:29 -04004186 rcu_read_lock();
4187 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4188 if (eb && atomic_inc_not_zero(&eb->refs)) {
4189 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004190 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004191 return eb;
4192 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004193 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004194
David Sterbaba144192011-04-21 01:12:06 +02004195 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004196 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004197 return NULL;
4198
Chris Mason727011e2010-08-06 13:21:20 -04004199 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004200 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004201 if (!p) {
4202 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04004203 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004204 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004205
4206 spin_lock(&mapping->private_lock);
4207 if (PagePrivate(p)) {
4208 /*
4209 * We could have already allocated an eb for this page
4210 * and attached one so lets see if we can get a ref on
4211 * the existing eb, and if we can we know it's good and
4212 * we can just return that one, else we know we can just
4213 * overwrite page->private.
4214 */
4215 exists = (struct extent_buffer *)p->private;
4216 if (atomic_inc_not_zero(&exists->refs)) {
4217 spin_unlock(&mapping->private_lock);
4218 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004219 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004220 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004221 goto free_eb;
4222 }
4223
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004224 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004225 * Do this so attach doesn't complain and we need to
4226 * drop the ref the old guy had.
4227 */
4228 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004229 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004230 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004231 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004232 attach_extent_buffer_page(eb, p);
4233 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004234 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004235 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004236 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004237 if (!PageUptodate(p))
4238 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004239
4240 /*
4241 * see below about how we avoid a nasty race with release page
4242 * and why we unlock later
4243 */
Chris Masond1310b22008-01-24 16:13:08 -05004244 }
4245 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004246 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004247again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004248 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4249 if (ret)
4250 goto free_eb;
4251
Chris Mason6af118ce2008-07-22 11:18:07 -04004252 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004253 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4254 if (ret == -EEXIST) {
4255 exists = radix_tree_lookup(&tree->buffer,
4256 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004257 if (!atomic_inc_not_zero(&exists->refs)) {
4258 spin_unlock(&tree->buffer_lock);
4259 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004260 exists = NULL;
4261 goto again;
4262 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004263 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004264 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004265 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004266 goto free_eb;
4267 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004268 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004269 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004270 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004271 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004272
4273 /*
4274 * there is a race where release page may have
4275 * tried to find this extent buffer in the radix
4276 * but failed. It will tell the VM it is safe to
4277 * reclaim the, and it will clear the page private bit.
4278 * We must make sure to set the page private bit properly
4279 * after the extent buffer is in the radix tree so
4280 * it doesn't get lost
4281 */
Chris Mason727011e2010-08-06 13:21:20 -04004282 SetPageChecked(eb->pages[0]);
4283 for (i = 1; i < num_pages; i++) {
4284 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004285 ClearPageChecked(p);
4286 unlock_page(p);
4287 }
4288 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004289 return eb;
4290
Chris Mason6af118ce2008-07-22 11:18:07 -04004291free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004292 for (i = 0; i < num_pages; i++) {
4293 if (eb->pages[i])
4294 unlock_page(eb->pages[i]);
4295 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004296
Josef Bacik17de39a2012-05-04 15:16:06 -04004297 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e92010-10-26 20:57:29 -04004298 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004299 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004300}
Chris Masond1310b22008-01-24 16:13:08 -05004301
4302struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004303 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004304{
Chris Masond1310b22008-01-24 16:13:08 -05004305 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004306
Miao Xie19fe0a82010-10-26 20:57:29 -04004307 rcu_read_lock();
4308 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4309 if (eb && atomic_inc_not_zero(&eb->refs)) {
4310 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004311 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004312 return eb;
4313 }
4314 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004315
Miao Xie19fe0a82010-10-26 20:57:29 -04004316 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004317}
Chris Masond1310b22008-01-24 16:13:08 -05004318
Josef Bacik3083ee22012-03-09 16:01:49 -05004319static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4320{
4321 struct extent_buffer *eb =
4322 container_of(head, struct extent_buffer, rcu_head);
4323
4324 __free_extent_buffer(eb);
4325}
4326
Josef Bacik3083ee22012-03-09 16:01:49 -05004327/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004328static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004329{
4330 WARN_ON(atomic_read(&eb->refs) == 0);
4331 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004332 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4333 spin_unlock(&eb->refs_lock);
4334 } else {
4335 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004336
Jan Schmidt815a51c2012-05-16 17:00:02 +02004337 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004338
Jan Schmidt815a51c2012-05-16 17:00:02 +02004339 spin_lock(&tree->buffer_lock);
4340 radix_tree_delete(&tree->buffer,
4341 eb->start >> PAGE_CACHE_SHIFT);
4342 spin_unlock(&tree->buffer_lock);
4343 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004344
4345 /* Should be safe to release our pages at this point */
4346 btrfs_release_extent_buffer_page(eb, 0);
4347
4348 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004349 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004350 }
4351 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004352
4353 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004354}
4355
Chris Masond1310b22008-01-24 16:13:08 -05004356void free_extent_buffer(struct extent_buffer *eb)
4357{
Chris Masond1310b22008-01-24 16:13:08 -05004358 if (!eb)
4359 return;
4360
Josef Bacik3083ee22012-03-09 16:01:49 -05004361 spin_lock(&eb->refs_lock);
4362 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004363 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4364 atomic_dec(&eb->refs);
4365
4366 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004367 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004368 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004369 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4370 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004371
Josef Bacik3083ee22012-03-09 16:01:49 -05004372 /*
4373 * I know this is terrible, but it's temporary until we stop tracking
4374 * the uptodate bits and such for the extent buffers.
4375 */
4376 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004377}
Chris Masond1310b22008-01-24 16:13:08 -05004378
Josef Bacik3083ee22012-03-09 16:01:49 -05004379void free_extent_buffer_stale(struct extent_buffer *eb)
4380{
4381 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004382 return;
4383
Josef Bacik3083ee22012-03-09 16:01:49 -05004384 spin_lock(&eb->refs_lock);
4385 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4386
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004387 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004388 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4389 atomic_dec(&eb->refs);
4390 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004391}
4392
Chris Mason1d4284b2012-03-28 20:31:37 -04004393void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004394{
Chris Masond1310b22008-01-24 16:13:08 -05004395 unsigned long i;
4396 unsigned long num_pages;
4397 struct page *page;
4398
Chris Masond1310b22008-01-24 16:13:08 -05004399 num_pages = num_extent_pages(eb->start, eb->len);
4400
4401 for (i = 0; i < num_pages; i++) {
4402 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004403 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004404 continue;
4405
Chris Masona61e6f22008-07-22 11:18:08 -04004406 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004407 WARN_ON(!PagePrivate(page));
4408
Chris Masond1310b22008-01-24 16:13:08 -05004409 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004410 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004411 if (!PageDirty(page)) {
4412 radix_tree_tag_clear(&page->mapping->page_tree,
4413 page_index(page),
4414 PAGECACHE_TAG_DIRTY);
4415 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004416 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004417 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004418 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004419 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004420 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004421}
Chris Masond1310b22008-01-24 16:13:08 -05004422
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004423int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004424{
4425 unsigned long i;
4426 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004427 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004428
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004429 check_buffer_tree_ref(eb);
4430
Chris Masonb9473432009-03-13 11:00:37 -04004431 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004432
Chris Masond1310b22008-01-24 16:13:08 -05004433 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004434 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004435 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4436
Chris Masonb9473432009-03-13 11:00:37 -04004437 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004438 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004439 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004440}
Chris Masond1310b22008-01-24 16:13:08 -05004441
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004442static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004443{
4444 if (len < PAGE_CACHE_SIZE)
4445 return 1;
4446 if (start & (PAGE_CACHE_SIZE - 1))
4447 return 1;
4448 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4449 return 1;
4450 return 0;
4451}
4452
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004453int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004454{
4455 unsigned long i;
4456 struct page *page;
4457 unsigned long num_pages;
4458
Chris Masonb4ce94d2009-02-04 09:25:08 -05004459 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004460 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004461 for (i = 0; i < num_pages; i++) {
4462 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004463 if (page)
4464 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004465 }
4466 return 0;
4467}
4468
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004469int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004470{
4471 unsigned long i;
4472 struct page *page;
4473 unsigned long num_pages;
4474
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004475 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004476 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004477 for (i = 0; i < num_pages; i++) {
4478 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004479 SetPageUptodate(page);
4480 }
4481 return 0;
4482}
Chris Masond1310b22008-01-24 16:13:08 -05004483
Chris Masonce9adaa2008-04-09 16:28:12 -04004484int extent_range_uptodate(struct extent_io_tree *tree,
4485 u64 start, u64 end)
4486{
4487 struct page *page;
4488 int ret;
4489 int pg_uptodate = 1;
4490 int uptodate;
4491 unsigned long index;
4492
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004493 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004494 ret = test_range_bit(tree, start, end,
4495 EXTENT_UPTODATE, 1, NULL);
4496 if (ret)
4497 return 1;
4498 }
Chris Masond3977122009-01-05 21:25:51 -05004499 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004500 index = start >> PAGE_CACHE_SHIFT;
4501 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004502 if (!page)
4503 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004504 uptodate = PageUptodate(page);
4505 page_cache_release(page);
4506 if (!uptodate) {
4507 pg_uptodate = 0;
4508 break;
4509 }
4510 start += PAGE_CACHE_SIZE;
4511 }
4512 return pg_uptodate;
4513}
4514
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004515int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004516{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004517 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004518}
Chris Masond1310b22008-01-24 16:13:08 -05004519
4520int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004521 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004522 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004523{
4524 unsigned long i;
4525 unsigned long start_i;
4526 struct page *page;
4527 int err;
4528 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004529 int locked_pages = 0;
4530 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004531 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004532 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004533 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004534 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004535
Chris Masonb4ce94d2009-02-04 09:25:08 -05004536 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004537 return 0;
4538
Chris Masond1310b22008-01-24 16:13:08 -05004539 if (start) {
4540 WARN_ON(start < eb->start);
4541 start_i = (start >> PAGE_CACHE_SHIFT) -
4542 (eb->start >> PAGE_CACHE_SHIFT);
4543 } else {
4544 start_i = 0;
4545 }
4546
4547 num_pages = num_extent_pages(eb->start, eb->len);
4548 for (i = start_i; i < num_pages; i++) {
4549 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004550 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004551 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004552 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004553 } else {
4554 lock_page(page);
4555 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004556 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004557 if (!PageUptodate(page)) {
4558 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004559 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004560 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004561 }
4562 if (all_uptodate) {
4563 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004564 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004565 goto unlock_exit;
4566 }
4567
Josef Bacikea466792012-03-26 21:57:36 -04004568 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004569 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004570 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004571 for (i = start_i; i < num_pages; i++) {
4572 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004573 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004574 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004575 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004576 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004577 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004578 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004579 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004580 } else {
4581 unlock_page(page);
4582 }
4583 }
4584
Jeff Mahoney355808c2011-10-03 23:23:14 -04004585 if (bio) {
4586 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004587 if (err)
4588 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004589 }
Chris Masona86c12c2008-02-07 10:50:54 -05004590
Arne Jansenbb82ab82011-06-10 14:06:53 +02004591 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004592 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004593
Chris Masond1310b22008-01-24 16:13:08 -05004594 for (i = start_i; i < num_pages; i++) {
4595 page = extent_buffer_page(eb, i);
4596 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004597 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004598 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004599 }
Chris Masond3977122009-01-05 21:25:51 -05004600
Chris Masond1310b22008-01-24 16:13:08 -05004601 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004602
4603unlock_exit:
4604 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004605 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004606 page = extent_buffer_page(eb, i);
4607 i++;
4608 unlock_page(page);
4609 locked_pages--;
4610 }
4611 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004612}
Chris Masond1310b22008-01-24 16:13:08 -05004613
4614void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4615 unsigned long start,
4616 unsigned long len)
4617{
4618 size_t cur;
4619 size_t offset;
4620 struct page *page;
4621 char *kaddr;
4622 char *dst = (char *)dstv;
4623 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4624 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004625
4626 WARN_ON(start > eb->len);
4627 WARN_ON(start + len > eb->start + eb->len);
4628
4629 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4630
Chris Masond3977122009-01-05 21:25:51 -05004631 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004632 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004633
4634 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004635 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004636 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004637
4638 dst += cur;
4639 len -= cur;
4640 offset = 0;
4641 i++;
4642 }
4643}
Chris Masond1310b22008-01-24 16:13:08 -05004644
4645int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004646 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004647 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004648 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004649{
4650 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4651 char *kaddr;
4652 struct page *p;
4653 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4654 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4655 unsigned long end_i = (start_offset + start + min_len - 1) >>
4656 PAGE_CACHE_SHIFT;
4657
4658 if (i != end_i)
4659 return -EINVAL;
4660
4661 if (i == 0) {
4662 offset = start_offset;
4663 *map_start = 0;
4664 } else {
4665 offset = 0;
4666 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4667 }
Chris Masond3977122009-01-05 21:25:51 -05004668
Chris Masond1310b22008-01-24 16:13:08 -05004669 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004670 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4671 "wanted %lu %lu\n", (unsigned long long)eb->start,
4672 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004673 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004674 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004675 }
4676
4677 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004678 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004679 *map = kaddr + offset;
4680 *map_len = PAGE_CACHE_SIZE - offset;
4681 return 0;
4682}
Chris Masond1310b22008-01-24 16:13:08 -05004683
Chris Masond1310b22008-01-24 16:13:08 -05004684int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4685 unsigned long start,
4686 unsigned long len)
4687{
4688 size_t cur;
4689 size_t offset;
4690 struct page *page;
4691 char *kaddr;
4692 char *ptr = (char *)ptrv;
4693 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4694 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4695 int ret = 0;
4696
4697 WARN_ON(start > eb->len);
4698 WARN_ON(start + len > eb->start + eb->len);
4699
4700 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4701
Chris Masond3977122009-01-05 21:25:51 -05004702 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004703 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004704
4705 cur = min(len, (PAGE_CACHE_SIZE - offset));
4706
Chris Masona6591712011-07-19 12:04:14 -04004707 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004708 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004709 if (ret)
4710 break;
4711
4712 ptr += cur;
4713 len -= cur;
4714 offset = 0;
4715 i++;
4716 }
4717 return ret;
4718}
Chris Masond1310b22008-01-24 16:13:08 -05004719
4720void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4721 unsigned long start, unsigned long len)
4722{
4723 size_t cur;
4724 size_t offset;
4725 struct page *page;
4726 char *kaddr;
4727 char *src = (char *)srcv;
4728 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4729 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4730
4731 WARN_ON(start > eb->len);
4732 WARN_ON(start + len > eb->start + eb->len);
4733
4734 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4735
Chris Masond3977122009-01-05 21:25:51 -05004736 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004737 page = extent_buffer_page(eb, i);
4738 WARN_ON(!PageUptodate(page));
4739
4740 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004741 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004742 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004743
4744 src += cur;
4745 len -= cur;
4746 offset = 0;
4747 i++;
4748 }
4749}
Chris Masond1310b22008-01-24 16:13:08 -05004750
4751void memset_extent_buffer(struct extent_buffer *eb, char c,
4752 unsigned long start, unsigned long len)
4753{
4754 size_t cur;
4755 size_t offset;
4756 struct page *page;
4757 char *kaddr;
4758 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4759 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4760
4761 WARN_ON(start > eb->len);
4762 WARN_ON(start + len > eb->start + eb->len);
4763
4764 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4765
Chris Masond3977122009-01-05 21:25:51 -05004766 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004767 page = extent_buffer_page(eb, i);
4768 WARN_ON(!PageUptodate(page));
4769
4770 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004771 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004772 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004773
4774 len -= cur;
4775 offset = 0;
4776 i++;
4777 }
4778}
Chris Masond1310b22008-01-24 16:13:08 -05004779
4780void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4781 unsigned long dst_offset, unsigned long src_offset,
4782 unsigned long len)
4783{
4784 u64 dst_len = dst->len;
4785 size_t cur;
4786 size_t offset;
4787 struct page *page;
4788 char *kaddr;
4789 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4790 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4791
4792 WARN_ON(src->len != dst_len);
4793
4794 offset = (start_offset + dst_offset) &
4795 ((unsigned long)PAGE_CACHE_SIZE - 1);
4796
Chris Masond3977122009-01-05 21:25:51 -05004797 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004798 page = extent_buffer_page(dst, i);
4799 WARN_ON(!PageUptodate(page));
4800
4801 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4802
Chris Masona6591712011-07-19 12:04:14 -04004803 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004804 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004805
4806 src_offset += cur;
4807 len -= cur;
4808 offset = 0;
4809 i++;
4810 }
4811}
Chris Masond1310b22008-01-24 16:13:08 -05004812
4813static void move_pages(struct page *dst_page, struct page *src_page,
4814 unsigned long dst_off, unsigned long src_off,
4815 unsigned long len)
4816{
Chris Masona6591712011-07-19 12:04:14 -04004817 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004818 if (dst_page == src_page) {
4819 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4820 } else {
Chris Masona6591712011-07-19 12:04:14 -04004821 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004822 char *p = dst_kaddr + dst_off + len;
4823 char *s = src_kaddr + src_off + len;
4824
4825 while (len--)
4826 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004827 }
Chris Masond1310b22008-01-24 16:13:08 -05004828}
4829
Sergei Trofimovich33872062011-04-11 21:52:52 +00004830static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4831{
4832 unsigned long distance = (src > dst) ? src - dst : dst - src;
4833 return distance < len;
4834}
4835
Chris Masond1310b22008-01-24 16:13:08 -05004836static void copy_pages(struct page *dst_page, struct page *src_page,
4837 unsigned long dst_off, unsigned long src_off,
4838 unsigned long len)
4839{
Chris Masona6591712011-07-19 12:04:14 -04004840 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004841 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004842 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004843
Sergei Trofimovich33872062011-04-11 21:52:52 +00004844 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004845 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004846 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004847 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004848 if (areas_overlap(src_off, dst_off, len))
4849 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004850 }
Chris Masond1310b22008-01-24 16:13:08 -05004851
Chris Mason727011e2010-08-06 13:21:20 -04004852 if (must_memmove)
4853 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4854 else
4855 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004856}
4857
4858void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4859 unsigned long src_offset, unsigned long len)
4860{
4861 size_t cur;
4862 size_t dst_off_in_page;
4863 size_t src_off_in_page;
4864 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4865 unsigned long dst_i;
4866 unsigned long src_i;
4867
4868 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004869 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4870 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004871 BUG_ON(1);
4872 }
4873 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004874 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4875 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004876 BUG_ON(1);
4877 }
4878
Chris Masond3977122009-01-05 21:25:51 -05004879 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004880 dst_off_in_page = (start_offset + dst_offset) &
4881 ((unsigned long)PAGE_CACHE_SIZE - 1);
4882 src_off_in_page = (start_offset + src_offset) &
4883 ((unsigned long)PAGE_CACHE_SIZE - 1);
4884
4885 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4886 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4887
4888 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4889 src_off_in_page));
4890 cur = min_t(unsigned long, cur,
4891 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4892
4893 copy_pages(extent_buffer_page(dst, dst_i),
4894 extent_buffer_page(dst, src_i),
4895 dst_off_in_page, src_off_in_page, cur);
4896
4897 src_offset += cur;
4898 dst_offset += cur;
4899 len -= cur;
4900 }
4901}
Chris Masond1310b22008-01-24 16:13:08 -05004902
4903void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4904 unsigned long src_offset, unsigned long len)
4905{
4906 size_t cur;
4907 size_t dst_off_in_page;
4908 size_t src_off_in_page;
4909 unsigned long dst_end = dst_offset + len - 1;
4910 unsigned long src_end = src_offset + len - 1;
4911 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4912 unsigned long dst_i;
4913 unsigned long src_i;
4914
4915 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004916 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4917 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004918 BUG_ON(1);
4919 }
4920 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004921 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4922 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004923 BUG_ON(1);
4924 }
Chris Mason727011e2010-08-06 13:21:20 -04004925 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004926 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4927 return;
4928 }
Chris Masond3977122009-01-05 21:25:51 -05004929 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004930 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4931 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4932
4933 dst_off_in_page = (start_offset + dst_end) &
4934 ((unsigned long)PAGE_CACHE_SIZE - 1);
4935 src_off_in_page = (start_offset + src_end) &
4936 ((unsigned long)PAGE_CACHE_SIZE - 1);
4937
4938 cur = min_t(unsigned long, len, src_off_in_page + 1);
4939 cur = min(cur, dst_off_in_page + 1);
4940 move_pages(extent_buffer_page(dst, dst_i),
4941 extent_buffer_page(dst, src_i),
4942 dst_off_in_page - cur + 1,
4943 src_off_in_page - cur + 1, cur);
4944
4945 dst_end -= cur;
4946 src_end -= cur;
4947 len -= cur;
4948 }
4949}
Chris Mason6af118ce2008-07-22 11:18:07 -04004950
Josef Bacik3083ee22012-03-09 16:01:49 -05004951int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004952{
Chris Mason6af118ce2008-07-22 11:18:07 -04004953 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04004954
Miao Xie19fe0a82010-10-26 20:57:29 -04004955 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004956 * We need to make sure noboody is attaching this page to an eb right
4957 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004958 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004959 spin_lock(&page->mapping->private_lock);
4960 if (!PagePrivate(page)) {
4961 spin_unlock(&page->mapping->private_lock);
4962 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004963 }
4964
Josef Bacik3083ee22012-03-09 16:01:49 -05004965 eb = (struct extent_buffer *)page->private;
4966 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004967
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004968 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004969 * This is a little awful but should be ok, we need to make sure that
4970 * the eb doesn't disappear out from under us while we're looking at
4971 * this page.
4972 */
4973 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004974 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004975 spin_unlock(&eb->refs_lock);
4976 spin_unlock(&page->mapping->private_lock);
4977 return 0;
4978 }
4979 spin_unlock(&page->mapping->private_lock);
4980
4981 if ((mask & GFP_NOFS) == GFP_NOFS)
4982 mask = GFP_NOFS;
4983
4984 /*
4985 * If tree ref isn't set then we know the ref on this eb is a real ref,
4986 * so just return, this page will likely be freed soon anyway.
4987 */
4988 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4989 spin_unlock(&eb->refs_lock);
4990 return 0;
4991 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004992
Josef Bacike64860a2012-07-20 16:05:36 -04004993 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04004994}