blob: b08ea4717e9d70ef7967fe33c1658b669eee16ac [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 }
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000110
111 /*
112 * Make sure all delayed rcu free are flushed before we
113 * destroy caches.
114 */
115 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500116 if (extent_state_cache)
117 kmem_cache_destroy(extent_state_cache);
118 if (extent_buffer_cache)
119 kmem_cache_destroy(extent_buffer_cache);
120}
121
122void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200123 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500124{
Eric Paris6bef4d32010-02-23 19:43:04 +0000125 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400126 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500127 tree->ops = NULL;
128 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500129 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400130 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500131 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500132}
Chris Masond1310b22008-01-24 16:13:08 -0500133
Christoph Hellwigb2950862008-12-02 09:54:17 -0500134static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500135{
136 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500137#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400138 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400139#endif
Chris Masond1310b22008-01-24 16:13:08 -0500140
141 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400142 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500143 return state;
144 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500145 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500146 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_add(&state->leak_list, &states);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 atomic_set(&state->refs, 1);
153 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100154 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500155 return state;
156}
Chris Masond1310b22008-01-24 16:13:08 -0500157
Chris Mason4845e442010-05-25 20:56:50 -0400158void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500159{
Chris Masond1310b22008-01-24 16:13:08 -0500160 if (!state)
161 return;
162 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500163#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400164 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400165#endif
Chris Mason70dec802008-01-29 09:59:12 -0500166 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500167#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400168 spin_lock_irqsave(&leak_lock, flags);
169 list_del(&state->leak_list);
170 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400171#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100172 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500173 kmem_cache_free(extent_state_cache, state);
174 }
175}
Chris Masond1310b22008-01-24 16:13:08 -0500176
177static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
178 struct rb_node *node)
179{
Chris Masond3977122009-01-05 21:25:51 -0500180 struct rb_node **p = &root->rb_node;
181 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct tree_entry *entry;
183
Chris Masond3977122009-01-05 21:25:51 -0500184 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500185 parent = *p;
186 entry = rb_entry(parent, struct tree_entry, rb_node);
187
188 if (offset < entry->start)
189 p = &(*p)->rb_left;
190 else if (offset > entry->end)
191 p = &(*p)->rb_right;
192 else
193 return parent;
194 }
195
Chris Masond1310b22008-01-24 16:13:08 -0500196 rb_link_node(node, parent, p);
197 rb_insert_color(node, root);
198 return NULL;
199}
200
Chris Mason80ea96b2008-02-01 14:51:59 -0500201static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500202 struct rb_node **prev_ret,
203 struct rb_node **next_ret)
204{
Chris Mason80ea96b2008-02-01 14:51:59 -0500205 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500206 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500207 struct rb_node *prev = NULL;
208 struct rb_node *orig_prev = NULL;
209 struct tree_entry *entry;
210 struct tree_entry *prev_entry = NULL;
211
Chris Masond3977122009-01-05 21:25:51 -0500212 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500213 entry = rb_entry(n, struct tree_entry, rb_node);
214 prev = n;
215 prev_entry = entry;
216
217 if (offset < entry->start)
218 n = n->rb_left;
219 else if (offset > entry->end)
220 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500221 else
Chris Masond1310b22008-01-24 16:13:08 -0500222 return n;
223 }
224
225 if (prev_ret) {
226 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500227 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500228 prev = rb_next(prev);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 }
231 *prev_ret = prev;
232 prev = orig_prev;
233 }
234
235 if (next_ret) {
236 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500237 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500238 prev = rb_prev(prev);
239 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
240 }
241 *next_ret = prev;
242 }
243 return NULL;
244}
245
Chris Mason80ea96b2008-02-01 14:51:59 -0500246static inline struct rb_node *tree_search(struct extent_io_tree *tree,
247 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500248{
Chris Mason70dec802008-01-29 09:59:12 -0500249 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500250 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500251
Chris Mason80ea96b2008-02-01 14:51:59 -0500252 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500253 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500254 return prev;
255 return ret;
256}
257
Josef Bacik9ed74f22009-09-11 16:12:44 -0400258static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
259 struct extent_state *other)
260{
261 if (tree->ops && tree->ops->merge_extent_hook)
262 tree->ops->merge_extent_hook(tree->mapping->host, new,
263 other);
264}
265
Chris Masond1310b22008-01-24 16:13:08 -0500266/*
267 * utility function to look for merge candidates inside a given range.
268 * Any extents with matching state are merged together into a single
269 * extent in the tree. Extents with EXTENT_IO in their state field
270 * are not merged because the end_io handlers need to be able to do
271 * operations on them without sleeping (or doing allocations/splits).
272 *
273 * This should be called with the tree lock held.
274 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000275static void merge_state(struct extent_io_tree *tree,
276 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500277{
278 struct extent_state *other;
279 struct rb_node *other_node;
280
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400281 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000282 return;
Chris Masond1310b22008-01-24 16:13:08 -0500283
284 other_node = rb_prev(&state->rb_node);
285 if (other_node) {
286 other = rb_entry(other_node, struct extent_state, rb_node);
287 if (other->end == state->start - 1 &&
288 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500290 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500291 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500292 rb_erase(&other->rb_node, &tree->state);
293 free_extent_state(other);
294 }
295 }
296 other_node = rb_next(&state->rb_node);
297 if (other_node) {
298 other = rb_entry(other_node, struct extent_state, rb_node);
299 if (other->start == state->end + 1 &&
300 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400301 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400302 state->end = other->end;
303 other->tree = NULL;
304 rb_erase(&other->rb_node, &tree->state);
305 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500306 }
307 }
Chris Masond1310b22008-01-24 16:13:08 -0500308}
309
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000310static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400311 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500312{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000313 if (tree->ops && tree->ops->set_bit_hook)
314 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500315}
316
317static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500319{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400320 if (tree->ops && tree->ops->clear_bit_hook)
321 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500322}
323
Xiao Guangrong3150b692011-07-14 03:19:08 +0000324static void set_state_bits(struct extent_io_tree *tree,
325 struct extent_state *state, int *bits);
326
Chris Masond1310b22008-01-24 16:13:08 -0500327/*
328 * insert an extent_state struct into the tree. 'bits' are set on the
329 * struct before it is inserted.
330 *
331 * This may return -EEXIST if the extent is already there, in which case the
332 * state struct is freed.
333 *
334 * The tree lock is not taken internally. This is a utility function and
335 * probably isn't what you want to call (see set/clear_extent_bit).
336 */
337static int insert_state(struct extent_io_tree *tree,
338 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400339 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500340{
341 struct rb_node *node;
342
343 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500344 printk(KERN_ERR "btrfs end < start %llu %llu\n",
345 (unsigned long long)end,
346 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500347 WARN_ON(1);
348 }
Chris Masond1310b22008-01-24 16:13:08 -0500349 state->start = start;
350 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400351
Xiao Guangrong3150b692011-07-14 03:19:08 +0000352 set_state_bits(tree, state, bits);
353
Chris Masond1310b22008-01-24 16:13:08 -0500354 node = tree_insert(&tree->state, end, &state->rb_node);
355 if (node) {
356 struct extent_state *found;
357 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500358 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
359 "%llu %llu\n", (unsigned long long)found->start,
360 (unsigned long long)found->end,
361 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500362 return -EEXIST;
363 }
Chris Mason70dec802008-01-29 09:59:12 -0500364 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500365 merge_state(tree, state);
366 return 0;
367}
368
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000369static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400370 u64 split)
371{
372 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000373 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400374}
375
Chris Masond1310b22008-01-24 16:13:08 -0500376/*
377 * split a given extent state struct in two, inserting the preallocated
378 * struct 'prealloc' as the newly created second half. 'split' indicates an
379 * offset inside 'orig' where it should be split.
380 *
381 * Before calling,
382 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
383 * are two extent state structs in the tree:
384 * prealloc: [orig->start, split - 1]
385 * orig: [ split, orig->end ]
386 *
387 * The tree locks are not taken by this function. They need to be held
388 * by the caller.
389 */
390static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
391 struct extent_state *prealloc, u64 split)
392{
393 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400394
395 split_cb(tree, orig, split);
396
Chris Masond1310b22008-01-24 16:13:08 -0500397 prealloc->start = orig->start;
398 prealloc->end = split - 1;
399 prealloc->state = orig->state;
400 orig->start = split;
401
402 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
403 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500404 free_extent_state(prealloc);
405 return -EEXIST;
406 }
Chris Mason70dec802008-01-29 09:59:12 -0500407 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500408 return 0;
409}
410
Li Zefancdc6a392012-03-12 16:39:48 +0800411static struct extent_state *next_state(struct extent_state *state)
412{
413 struct rb_node *next = rb_next(&state->rb_node);
414 if (next)
415 return rb_entry(next, struct extent_state, rb_node);
416 else
417 return NULL;
418}
419
Chris Masond1310b22008-01-24 16:13:08 -0500420/*
421 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800422 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500423 *
424 * If no bits are set on the state struct after clearing things, the
425 * struct is freed and removed from the tree
426 */
Li Zefancdc6a392012-03-12 16:39:48 +0800427static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
428 struct extent_state *state,
429 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500430{
Li Zefancdc6a392012-03-12 16:39:48 +0800431 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400432 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500433
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400434 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500435 u64 range = state->end - state->start + 1;
436 WARN_ON(range > tree->dirty_bytes);
437 tree->dirty_bytes -= range;
438 }
Chris Mason291d6732008-01-29 15:55:23 -0500439 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400440 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500441 if (wake)
442 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400443 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800444 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500445 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500446 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500447 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500448 free_extent_state(state);
449 } else {
450 WARN_ON(1);
451 }
452 } else {
453 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800454 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500455 }
Li Zefancdc6a392012-03-12 16:39:48 +0800456 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500457}
458
Xiao Guangrong82337672011-04-20 06:44:57 +0000459static struct extent_state *
460alloc_extent_state_atomic(struct extent_state *prealloc)
461{
462 if (!prealloc)
463 prealloc = alloc_extent_state(GFP_ATOMIC);
464
465 return prealloc;
466}
467
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400468void extent_io_tree_panic(struct extent_io_tree *tree, int err)
469{
470 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
471 "Extent tree was modified by another "
472 "thread while locked.");
473}
474
Chris Masond1310b22008-01-24 16:13:08 -0500475/*
476 * clear some bits on a range in the tree. This may require splitting
477 * or inserting elements in the tree, so the gfp mask is used to
478 * indicate which allocations or sleeping are allowed.
479 *
480 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
481 * the given range from the tree regardless of state (ie for truncate).
482 *
483 * the range [start, end] is inclusive.
484 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100485 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500486 */
487int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400488 int bits, int wake, int delete,
489 struct extent_state **cached_state,
490 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500491{
492 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400493 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500494 struct extent_state *prealloc = NULL;
495 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400496 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500497 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000498 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500499
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400500 if (delete)
501 bits |= ~EXTENT_CTLBITS;
502 bits |= EXTENT_FIRST_DELALLOC;
503
Josef Bacik2ac55d42010-02-03 19:33:23 +0000504 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
505 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500506again:
507 if (!prealloc && (mask & __GFP_WAIT)) {
508 prealloc = alloc_extent_state(mask);
509 if (!prealloc)
510 return -ENOMEM;
511 }
512
Chris Masoncad321a2008-12-17 14:51:42 -0500513 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400514 if (cached_state) {
515 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000516
517 if (clear) {
518 *cached_state = NULL;
519 cached_state = NULL;
520 }
521
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400522 if (cached && cached->tree && cached->start <= start &&
523 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000524 if (clear)
525 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400526 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400527 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400528 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000529 if (clear)
530 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400531 }
Chris Masond1310b22008-01-24 16:13:08 -0500532 /*
533 * this search will find the extents that end after
534 * our range starts
535 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500536 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500537 if (!node)
538 goto out;
539 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400540hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500541 if (state->start > end)
542 goto out;
543 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400544 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500545
Liu Bo04493142012-02-16 18:34:37 +0800546 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800547 if (!(state->state & bits)) {
548 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800549 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800550 }
Liu Bo04493142012-02-16 18:34:37 +0800551
Chris Masond1310b22008-01-24 16:13:08 -0500552 /*
553 * | ---- desired range ---- |
554 * | state | or
555 * | ------------- state -------------- |
556 *
557 * We need to split the extent we found, and may flip
558 * bits on second half.
559 *
560 * If the extent we found extends past our range, we
561 * just split and search again. It'll get split again
562 * the next time though.
563 *
564 * If the extent we found is inside our range, we clear
565 * the desired bit on it.
566 */
567
568 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000569 prealloc = alloc_extent_state_atomic(prealloc);
570 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500571 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400572 if (err)
573 extent_io_tree_panic(tree, err);
574
Chris Masond1310b22008-01-24 16:13:08 -0500575 prealloc = NULL;
576 if (err)
577 goto out;
578 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800579 state = clear_state_bit(tree, state, &bits, wake);
580 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500581 }
582 goto search_again;
583 }
584 /*
585 * | ---- desired range ---- |
586 * | state |
587 * We need to split the extent, and clear the bit
588 * on the first half
589 */
590 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000591 prealloc = alloc_extent_state_atomic(prealloc);
592 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500593 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400594 if (err)
595 extent_io_tree_panic(tree, err);
596
Chris Masond1310b22008-01-24 16:13:08 -0500597 if (wake)
598 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400599
Jeff Mahoney6763af82012-03-01 14:56:29 +0100600 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400601
Chris Masond1310b22008-01-24 16:13:08 -0500602 prealloc = NULL;
603 goto out;
604 }
Chris Mason42daec22009-09-23 19:51:09 -0400605
Li Zefancdc6a392012-03-12 16:39:48 +0800606 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800607next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400608 if (last_end == (u64)-1)
609 goto out;
610 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800611 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800612 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500613 goto search_again;
614
615out:
Chris Masoncad321a2008-12-17 14:51:42 -0500616 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500617 if (prealloc)
618 free_extent_state(prealloc);
619
Jeff Mahoney6763af82012-03-01 14:56:29 +0100620 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500621
622search_again:
623 if (start > end)
624 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500625 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500626 if (mask & __GFP_WAIT)
627 cond_resched();
628 goto again;
629}
Chris Masond1310b22008-01-24 16:13:08 -0500630
Jeff Mahoney143bede2012-03-01 14:56:26 +0100631static void wait_on_state(struct extent_io_tree *tree,
632 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500633 __releases(tree->lock)
634 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500635{
636 DEFINE_WAIT(wait);
637 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500638 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500639 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500640 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500641 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500642}
643
644/*
645 * waits for one or more bits to clear on a range in the state tree.
646 * The range [start, end] is inclusive.
647 * The tree lock is taken by this function
648 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100649void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500650{
651 struct extent_state *state;
652 struct rb_node *node;
653
Chris Masoncad321a2008-12-17 14:51:42 -0500654 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500655again:
656 while (1) {
657 /*
658 * this search will find all the extents that end after
659 * our range starts
660 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500661 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500662 if (!node)
663 break;
664
665 state = rb_entry(node, struct extent_state, rb_node);
666
667 if (state->start > end)
668 goto out;
669
670 if (state->state & bits) {
671 start = state->start;
672 atomic_inc(&state->refs);
673 wait_on_state(tree, state);
674 free_extent_state(state);
675 goto again;
676 }
677 start = state->end + 1;
678
679 if (start > end)
680 break;
681
Xiao Guangrongded91f02011-07-14 03:19:27 +0000682 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500683 }
684out:
Chris Masoncad321a2008-12-17 14:51:42 -0500685 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500686}
Chris Masond1310b22008-01-24 16:13:08 -0500687
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000688static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500689 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500691{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400692 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400693
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000694 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400695 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500696 u64 range = state->end - state->start + 1;
697 tree->dirty_bytes += range;
698 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400699 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500700}
701
Chris Mason2c64c532009-09-02 15:04:12 -0400702static void cache_state(struct extent_state *state,
703 struct extent_state **cached_ptr)
704{
705 if (cached_ptr && !(*cached_ptr)) {
706 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
707 *cached_ptr = state;
708 atomic_inc(&state->refs);
709 }
710 }
711}
712
Arne Jansen507903b2011-04-06 10:02:20 +0000713static void uncache_state(struct extent_state **cached_ptr)
714{
715 if (cached_ptr && (*cached_ptr)) {
716 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400717 *cached_ptr = NULL;
718 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000719 }
720}
721
Chris Masond1310b22008-01-24 16:13:08 -0500722/*
Chris Mason1edbb732009-09-02 13:24:36 -0400723 * set some bits on a range in the tree. This may require allocations or
724 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500725 *
Chris Mason1edbb732009-09-02 13:24:36 -0400726 * If any of the exclusive bits are set, this will fail with -EEXIST if some
727 * part of the range already has the desired bits set. The start of the
728 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500729 *
Chris Mason1edbb732009-09-02 13:24:36 -0400730 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500731 */
Chris Mason1edbb732009-09-02 13:24:36 -0400732
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100733static int __must_check
734__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
735 int bits, int exclusive_bits, u64 *failed_start,
736 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500737{
738 struct extent_state *state;
739 struct extent_state *prealloc = NULL;
740 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500741 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500742 u64 last_start;
743 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400744
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400745 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500746again:
747 if (!prealloc && (mask & __GFP_WAIT)) {
748 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000749 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500750 }
751
Chris Masoncad321a2008-12-17 14:51:42 -0500752 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400753 if (cached_state && *cached_state) {
754 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400755 if (state->start <= start && state->end > start &&
756 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400757 node = &state->rb_node;
758 goto hit_next;
759 }
760 }
Chris Masond1310b22008-01-24 16:13:08 -0500761 /*
762 * this search will find all the extents that end after
763 * our range starts.
764 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500765 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500766 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000767 prealloc = alloc_extent_state_atomic(prealloc);
768 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400769 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400770 if (err)
771 extent_io_tree_panic(tree, err);
772
Chris Masond1310b22008-01-24 16:13:08 -0500773 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500774 goto out;
775 }
Chris Masond1310b22008-01-24 16:13:08 -0500776 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400777hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500778 last_start = state->start;
779 last_end = state->end;
780
781 /*
782 * | ---- desired range ---- |
783 * | state |
784 *
785 * Just lock what we found and keep going
786 */
787 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400788 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500789 *failed_start = state->start;
790 err = -EEXIST;
791 goto out;
792 }
Chris Mason42daec22009-09-23 19:51:09 -0400793
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000794 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400795 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500796 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400797 if (last_end == (u64)-1)
798 goto out;
799 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800800 state = next_state(state);
801 if (start < end && state && state->start == start &&
802 !need_resched())
803 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500804 goto search_again;
805 }
806
807 /*
808 * | ---- desired range ---- |
809 * | state |
810 * or
811 * | ------------- state -------------- |
812 *
813 * We need to split the extent we found, and may flip bits on
814 * second half.
815 *
816 * If the extent we found extends past our
817 * range, we just split and search again. It'll get split
818 * again the next time though.
819 *
820 * If the extent we found is inside our range, we set the
821 * desired bit on it.
822 */
823 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400824 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500825 *failed_start = start;
826 err = -EEXIST;
827 goto out;
828 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000829
830 prealloc = alloc_extent_state_atomic(prealloc);
831 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500832 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400833 if (err)
834 extent_io_tree_panic(tree, err);
835
Chris Masond1310b22008-01-24 16:13:08 -0500836 prealloc = NULL;
837 if (err)
838 goto out;
839 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000840 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400841 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500842 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400843 if (last_end == (u64)-1)
844 goto out;
845 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800846 state = next_state(state);
847 if (start < end && state && state->start == start &&
848 !need_resched())
849 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500850 }
851 goto search_again;
852 }
853 /*
854 * | ---- desired range ---- |
855 * | state | or | state |
856 *
857 * There's a hole, we need to insert something in it and
858 * ignore the extent we found.
859 */
860 if (state->start > start) {
861 u64 this_end;
862 if (end < last_start)
863 this_end = end;
864 else
Chris Masond3977122009-01-05 21:25:51 -0500865 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000866
867 prealloc = alloc_extent_state_atomic(prealloc);
868 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000869
870 /*
871 * Avoid to free 'prealloc' if it can be merged with
872 * the later extent.
873 */
Chris Masond1310b22008-01-24 16:13:08 -0500874 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400875 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400876 if (err)
877 extent_io_tree_panic(tree, err);
878
Chris Mason2c64c532009-09-02 15:04:12 -0400879 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500880 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500881 start = this_end + 1;
882 goto search_again;
883 }
884 /*
885 * | ---- desired range ---- |
886 * | state |
887 * We need to split the extent, and set the bit
888 * on the first half
889 */
890 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400891 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500892 *failed_start = start;
893 err = -EEXIST;
894 goto out;
895 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000896
897 prealloc = alloc_extent_state_atomic(prealloc);
898 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500899 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400900 if (err)
901 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500902
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000903 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400904 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500905 merge_state(tree, prealloc);
906 prealloc = NULL;
907 goto out;
908 }
909
910 goto search_again;
911
912out:
Chris Masoncad321a2008-12-17 14:51:42 -0500913 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500914 if (prealloc)
915 free_extent_state(prealloc);
916
917 return err;
918
919search_again:
920 if (start > end)
921 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500922 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500923 if (mask & __GFP_WAIT)
924 cond_resched();
925 goto again;
926}
Chris Masond1310b22008-01-24 16:13:08 -0500927
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100928int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
929 u64 *failed_start, struct extent_state **cached_state,
930 gfp_t mask)
931{
932 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
933 cached_state, mask);
934}
935
936
Josef Bacik462d6fa2011-09-26 13:56:12 -0400937/**
Liu Bo10983f22012-07-11 15:26:19 +0800938 * convert_extent_bit - convert all bits in a given range from one bit to
939 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400940 * @tree: the io tree to search
941 * @start: the start offset in bytes
942 * @end: the end offset in bytes (inclusive)
943 * @bits: the bits to set in this range
944 * @clear_bits: the bits to clear in this range
945 * @mask: the allocation mask
946 *
947 * This will go through and set bits for the given range. If any states exist
948 * already in this range they are set with the given bit and cleared of the
949 * clear_bits. This is only meant to be used by things that are mergeable, ie
950 * converting from say DELALLOC to DIRTY. This is not meant to be used with
951 * boundary bits like LOCK.
952 */
953int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
954 int bits, int clear_bits, gfp_t mask)
955{
956 struct extent_state *state;
957 struct extent_state *prealloc = NULL;
958 struct rb_node *node;
959 int err = 0;
960 u64 last_start;
961 u64 last_end;
962
963again:
964 if (!prealloc && (mask & __GFP_WAIT)) {
965 prealloc = alloc_extent_state(mask);
966 if (!prealloc)
967 return -ENOMEM;
968 }
969
970 spin_lock(&tree->lock);
971 /*
972 * this search will find all the extents that end after
973 * our range starts.
974 */
975 node = tree_search(tree, start);
976 if (!node) {
977 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500978 if (!prealloc) {
979 err = -ENOMEM;
980 goto out;
981 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400982 err = insert_state(tree, prealloc, start, end, &bits);
983 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400984 if (err)
985 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400986 goto out;
987 }
988 state = rb_entry(node, struct extent_state, rb_node);
989hit_next:
990 last_start = state->start;
991 last_end = state->end;
992
993 /*
994 * | ---- desired range ---- |
995 * | state |
996 *
997 * Just lock what we found and keep going
998 */
999 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001000 set_state_bits(tree, state, &bits);
Liu Bod1ac6e42012-05-10 18:10:39 +08001001 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001002 if (last_end == (u64)-1)
1003 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001004 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001005 if (start < end && state && state->start == start &&
1006 !need_resched())
1007 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001008 goto search_again;
1009 }
1010
1011 /*
1012 * | ---- desired range ---- |
1013 * | state |
1014 * or
1015 * | ------------- state -------------- |
1016 *
1017 * We need to split the extent we found, and may flip bits on
1018 * second half.
1019 *
1020 * If the extent we found extends past our
1021 * range, we just split and search again. It'll get split
1022 * again the next time though.
1023 *
1024 * If the extent we found is inside our range, we set the
1025 * desired bit on it.
1026 */
1027 if (state->start < start) {
1028 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001029 if (!prealloc) {
1030 err = -ENOMEM;
1031 goto out;
1032 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001033 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001034 if (err)
1035 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001036 prealloc = NULL;
1037 if (err)
1038 goto out;
1039 if (state->end <= end) {
1040 set_state_bits(tree, state, &bits);
Liu Bod1ac6e42012-05-10 18:10:39 +08001041 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001042 if (last_end == (u64)-1)
1043 goto out;
1044 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001045 if (start < end && state && state->start == start &&
1046 !need_resched())
1047 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001048 }
1049 goto search_again;
1050 }
1051 /*
1052 * | ---- desired range ---- |
1053 * | state | or | state |
1054 *
1055 * There's a hole, we need to insert something in it and
1056 * ignore the extent we found.
1057 */
1058 if (state->start > start) {
1059 u64 this_end;
1060 if (end < last_start)
1061 this_end = end;
1062 else
1063 this_end = last_start - 1;
1064
1065 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001066 if (!prealloc) {
1067 err = -ENOMEM;
1068 goto out;
1069 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070
1071 /*
1072 * Avoid to free 'prealloc' if it can be merged with
1073 * the later extent.
1074 */
1075 err = insert_state(tree, prealloc, start, this_end,
1076 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001077 if (err)
1078 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001079 prealloc = NULL;
1080 start = this_end + 1;
1081 goto search_again;
1082 }
1083 /*
1084 * | ---- desired range ---- |
1085 * | state |
1086 * We need to split the extent, and set the bit
1087 * on the first half
1088 */
1089 if (state->start <= end && state->end > end) {
1090 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001091 if (!prealloc) {
1092 err = -ENOMEM;
1093 goto out;
1094 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001095
1096 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001097 if (err)
1098 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001099
1100 set_state_bits(tree, prealloc, &bits);
1101 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001102 prealloc = NULL;
1103 goto out;
1104 }
1105
1106 goto search_again;
1107
1108out:
1109 spin_unlock(&tree->lock);
1110 if (prealloc)
1111 free_extent_state(prealloc);
1112
1113 return err;
1114
1115search_again:
1116 if (start > end)
1117 goto out;
1118 spin_unlock(&tree->lock);
1119 if (mask & __GFP_WAIT)
1120 cond_resched();
1121 goto again;
1122}
1123
Chris Masond1310b22008-01-24 16:13:08 -05001124/* wrappers around set/clear extent bit */
1125int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1126 gfp_t mask)
1127{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001128 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 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 set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1133 int bits, gfp_t mask)
1134{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001135 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001136 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001137}
Chris Masond1310b22008-01-24 16:13:08 -05001138
1139int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1140 int bits, gfp_t mask)
1141{
Chris Mason2c64c532009-09-02 15:04:12 -04001142 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
1145int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001146 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001147{
1148 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001149 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001150 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001151}
Chris Masond1310b22008-01-24 16:13:08 -05001152
1153int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1154 gfp_t mask)
1155{
1156 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001157 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001158 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001159}
Chris Masond1310b22008-01-24 16:13:08 -05001160
1161int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1162 gfp_t mask)
1163{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001164 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001165 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001166}
Chris Masond1310b22008-01-24 16:13:08 -05001167
Chris Masond1310b22008-01-24 16:13:08 -05001168int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001169 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001170{
Arne Jansen507903b2011-04-06 10:02:20 +00001171 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001172 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001173}
Chris Masond1310b22008-01-24 16:13:08 -05001174
Josef Bacik5fd02042012-05-02 14:00:54 -04001175int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1176 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001177{
Chris Mason2c64c532009-09-02 15:04:12 -04001178 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001179 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001180}
Chris Masond1310b22008-01-24 16:13:08 -05001181
Chris Masond352ac62008-09-29 15:18:18 -04001182/*
1183 * either insert or lock state struct between start and end use mask to tell
1184 * us if waiting is desired.
1185 */
Chris Mason1edbb732009-09-02 13:24:36 -04001186int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001187 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001188{
1189 int err;
1190 u64 failed_start;
1191 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001192 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1193 EXTENT_LOCKED, &failed_start,
1194 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001195 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001196 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1197 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001198 } else
Chris Masond1310b22008-01-24 16:13:08 -05001199 break;
Chris Masond1310b22008-01-24 16:13:08 -05001200 WARN_ON(start > end);
1201 }
1202 return err;
1203}
Chris Masond1310b22008-01-24 16:13:08 -05001204
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001205int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001206{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001207 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001208}
1209
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001210int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001211{
1212 int err;
1213 u64 failed_start;
1214
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001215 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1216 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001217 if (err == -EEXIST) {
1218 if (failed_start > start)
1219 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001220 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001221 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001222 }
Josef Bacik25179202008-10-29 14:49:05 -04001223 return 1;
1224}
Josef Bacik25179202008-10-29 14:49:05 -04001225
Chris Mason2c64c532009-09-02 15:04:12 -04001226int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1227 struct extent_state **cached, gfp_t mask)
1228{
1229 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1230 mask);
1231}
1232
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001233int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001234{
Chris Mason2c64c532009-09-02 15:04:12 -04001235 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001236 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001237}
Chris Masond1310b22008-01-24 16:13:08 -05001238
1239/*
Chris Masond1310b22008-01-24 16:13:08 -05001240 * helper function to set both pages and extents in the tree writeback
1241 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001242static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001243{
1244 unsigned long index = start >> PAGE_CACHE_SHIFT;
1245 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1246 struct page *page;
1247
1248 while (index <= end_index) {
1249 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001251 set_page_writeback(page);
1252 page_cache_release(page);
1253 index++;
1254 }
Chris Masond1310b22008-01-24 16:13:08 -05001255 return 0;
1256}
Chris Masond1310b22008-01-24 16:13:08 -05001257
Chris Masond352ac62008-09-29 15:18:18 -04001258/* find the first state struct with 'bits' set after 'start', and
1259 * return it. tree->lock must be held. NULL will returned if
1260 * nothing was found after 'start'
1261 */
Chris Masond7fc6402008-02-18 12:12:38 -05001262struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1263 u64 start, int bits)
1264{
1265 struct rb_node *node;
1266 struct extent_state *state;
1267
1268 /*
1269 * this search will find all the extents that end after
1270 * our range starts.
1271 */
1272 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001273 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001274 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001275
Chris Masond3977122009-01-05 21:25:51 -05001276 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001277 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001278 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001279 return state;
Chris Masond3977122009-01-05 21:25:51 -05001280
Chris Masond7fc6402008-02-18 12:12:38 -05001281 node = rb_next(node);
1282 if (!node)
1283 break;
1284 }
1285out:
1286 return NULL;
1287}
Chris Masond7fc6402008-02-18 12:12:38 -05001288
Chris Masond352ac62008-09-29 15:18:18 -04001289/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001290 * find the first offset in the io tree with 'bits' set. zero is
1291 * returned if we find something, and *start_ret and *end_ret are
1292 * set to reflect the state struct that was found.
1293 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001294 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001295 */
1296int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1297 u64 *start_ret, u64 *end_ret, int bits)
1298{
1299 struct extent_state *state;
1300 int ret = 1;
1301
1302 spin_lock(&tree->lock);
1303 state = find_first_extent_bit_state(tree, start, bits);
1304 if (state) {
1305 *start_ret = state->start;
1306 *end_ret = state->end;
1307 ret = 0;
1308 }
1309 spin_unlock(&tree->lock);
1310 return ret;
1311}
1312
1313/*
Chris Masond352ac62008-09-29 15:18:18 -04001314 * find a contiguous range of bytes in the file marked as delalloc, not
1315 * more than 'max_bytes'. start and end are used to return the range,
1316 *
1317 * 1 is returned if we find something, 0 if nothing was in the tree
1318 */
Chris Masonc8b97812008-10-29 14:49:59 -04001319static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001320 u64 *start, u64 *end, u64 max_bytes,
1321 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001322{
1323 struct rb_node *node;
1324 struct extent_state *state;
1325 u64 cur_start = *start;
1326 u64 found = 0;
1327 u64 total_bytes = 0;
1328
Chris Masoncad321a2008-12-17 14:51:42 -05001329 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001330
Chris Masond1310b22008-01-24 16:13:08 -05001331 /*
1332 * this search will find all the extents that end after
1333 * our range starts.
1334 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001335 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001336 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001337 if (!found)
1338 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001339 goto out;
1340 }
1341
Chris Masond3977122009-01-05 21:25:51 -05001342 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001343 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001344 if (found && (state->start != cur_start ||
1345 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001346 goto out;
1347 }
1348 if (!(state->state & EXTENT_DELALLOC)) {
1349 if (!found)
1350 *end = state->end;
1351 goto out;
1352 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001353 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001354 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001355 *cached_state = state;
1356 atomic_inc(&state->refs);
1357 }
Chris Masond1310b22008-01-24 16:13:08 -05001358 found++;
1359 *end = state->end;
1360 cur_start = state->end + 1;
1361 node = rb_next(node);
1362 if (!node)
1363 break;
1364 total_bytes += state->end - state->start + 1;
1365 if (total_bytes >= max_bytes)
1366 break;
1367 }
1368out:
Chris Masoncad321a2008-12-17 14:51:42 -05001369 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001370 return found;
1371}
1372
Jeff Mahoney143bede2012-03-01 14:56:26 +01001373static noinline void __unlock_for_delalloc(struct inode *inode,
1374 struct page *locked_page,
1375 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001376{
1377 int ret;
1378 struct page *pages[16];
1379 unsigned long index = start >> PAGE_CACHE_SHIFT;
1380 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1381 unsigned long nr_pages = end_index - index + 1;
1382 int i;
1383
1384 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001385 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001386
Chris Masond3977122009-01-05 21:25:51 -05001387 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001388 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001389 min_t(unsigned long, nr_pages,
1390 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001391 for (i = 0; i < ret; i++) {
1392 if (pages[i] != locked_page)
1393 unlock_page(pages[i]);
1394 page_cache_release(pages[i]);
1395 }
1396 nr_pages -= ret;
1397 index += ret;
1398 cond_resched();
1399 }
Chris Masonc8b97812008-10-29 14:49:59 -04001400}
1401
1402static noinline int lock_delalloc_pages(struct inode *inode,
1403 struct page *locked_page,
1404 u64 delalloc_start,
1405 u64 delalloc_end)
1406{
1407 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1408 unsigned long start_index = index;
1409 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1410 unsigned long pages_locked = 0;
1411 struct page *pages[16];
1412 unsigned long nrpages;
1413 int ret;
1414 int i;
1415
1416 /* the caller is responsible for locking the start index */
1417 if (index == locked_page->index && index == end_index)
1418 return 0;
1419
1420 /* skip the page at the start index */
1421 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001422 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001423 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001424 min_t(unsigned long,
1425 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001426 if (ret == 0) {
1427 ret = -EAGAIN;
1428 goto done;
1429 }
1430 /* now we have an array of pages, lock them all */
1431 for (i = 0; i < ret; i++) {
1432 /*
1433 * the caller is taking responsibility for
1434 * locked_page
1435 */
Chris Mason771ed682008-11-06 22:02:51 -05001436 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001437 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001438 if (!PageDirty(pages[i]) ||
1439 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001440 ret = -EAGAIN;
1441 unlock_page(pages[i]);
1442 page_cache_release(pages[i]);
1443 goto done;
1444 }
1445 }
Chris Masonc8b97812008-10-29 14:49:59 -04001446 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001447 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001448 }
Chris Masonc8b97812008-10-29 14:49:59 -04001449 nrpages -= ret;
1450 index += ret;
1451 cond_resched();
1452 }
1453 ret = 0;
1454done:
1455 if (ret && pages_locked) {
1456 __unlock_for_delalloc(inode, locked_page,
1457 delalloc_start,
1458 ((u64)(start_index + pages_locked - 1)) <<
1459 PAGE_CACHE_SHIFT);
1460 }
1461 return ret;
1462}
1463
1464/*
1465 * find a contiguous range of bytes in the file marked as delalloc, not
1466 * more than 'max_bytes'. start and end are used to return the range,
1467 *
1468 * 1 is returned if we find something, 0 if nothing was in the tree
1469 */
1470static noinline u64 find_lock_delalloc_range(struct inode *inode,
1471 struct extent_io_tree *tree,
1472 struct page *locked_page,
1473 u64 *start, u64 *end,
1474 u64 max_bytes)
1475{
1476 u64 delalloc_start;
1477 u64 delalloc_end;
1478 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001479 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001480 int ret;
1481 int loops = 0;
1482
1483again:
1484 /* step one, find a bunch of delalloc bytes starting at start */
1485 delalloc_start = *start;
1486 delalloc_end = 0;
1487 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001488 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001489 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001490 *start = delalloc_start;
1491 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001492 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001493 return found;
1494 }
1495
1496 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001497 * start comes from the offset of locked_page. We have to lock
1498 * pages in order, so we can't process delalloc bytes before
1499 * locked_page
1500 */
Chris Masond3977122009-01-05 21:25:51 -05001501 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001502 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001503
1504 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001505 * make sure to limit the number of pages we try to lock down
1506 * if we're looping.
1507 */
Chris Masond3977122009-01-05 21:25:51 -05001508 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001509 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001510
Chris Masonc8b97812008-10-29 14:49:59 -04001511 /* step two, lock all the pages after the page that has start */
1512 ret = lock_delalloc_pages(inode, locked_page,
1513 delalloc_start, delalloc_end);
1514 if (ret == -EAGAIN) {
1515 /* some of the pages are gone, lets avoid looping by
1516 * shortening the size of the delalloc range we're searching
1517 */
Chris Mason9655d292009-09-02 15:22:30 -04001518 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001519 if (!loops) {
1520 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1521 max_bytes = PAGE_CACHE_SIZE - offset;
1522 loops = 1;
1523 goto again;
1524 } else {
1525 found = 0;
1526 goto out_failed;
1527 }
1528 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001529 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001530
1531 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001532 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001533
1534 /* then test to make sure it is all still delalloc */
1535 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001536 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001537 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001538 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1539 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001540 __unlock_for_delalloc(inode, locked_page,
1541 delalloc_start, delalloc_end);
1542 cond_resched();
1543 goto again;
1544 }
Chris Mason9655d292009-09-02 15:22:30 -04001545 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001546 *start = delalloc_start;
1547 *end = delalloc_end;
1548out_failed:
1549 return found;
1550}
1551
1552int extent_clear_unlock_delalloc(struct inode *inode,
1553 struct extent_io_tree *tree,
1554 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001555 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001556{
1557 int ret;
1558 struct page *pages[16];
1559 unsigned long index = start >> PAGE_CACHE_SHIFT;
1560 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1561 unsigned long nr_pages = end_index - index + 1;
1562 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001563 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001564
Chris Masona791e352009-10-08 11:27:10 -04001565 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001566 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001567 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001568 clear_bits |= EXTENT_DIRTY;
1569
Chris Masona791e352009-10-08 11:27:10 -04001570 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001571 clear_bits |= EXTENT_DELALLOC;
1572
Chris Mason2c64c532009-09-02 15:04:12 -04001573 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001574 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1575 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1576 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001577 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001578
Chris Masond3977122009-01-05 21:25:51 -05001579 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001580 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001581 min_t(unsigned long,
1582 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001583 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001584
Chris Masona791e352009-10-08 11:27:10 -04001585 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001586 SetPagePrivate2(pages[i]);
1587
Chris Masonc8b97812008-10-29 14:49:59 -04001588 if (pages[i] == locked_page) {
1589 page_cache_release(pages[i]);
1590 continue;
1591 }
Chris Masona791e352009-10-08 11:27:10 -04001592 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001593 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001594 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001595 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001596 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001597 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001598 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001599 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001600 page_cache_release(pages[i]);
1601 }
1602 nr_pages -= ret;
1603 index += ret;
1604 cond_resched();
1605 }
1606 return 0;
1607}
Chris Masonc8b97812008-10-29 14:49:59 -04001608
Chris Masond352ac62008-09-29 15:18:18 -04001609/*
1610 * count the number of bytes in the tree that have a given bit(s)
1611 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1612 * cached. The total number found is returned.
1613 */
Chris Masond1310b22008-01-24 16:13:08 -05001614u64 count_range_bits(struct extent_io_tree *tree,
1615 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001616 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001617{
1618 struct rb_node *node;
1619 struct extent_state *state;
1620 u64 cur_start = *start;
1621 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001622 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001623 int found = 0;
1624
1625 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001626 WARN_ON(1);
1627 return 0;
1628 }
1629
Chris Masoncad321a2008-12-17 14:51:42 -05001630 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001631 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1632 total_bytes = tree->dirty_bytes;
1633 goto out;
1634 }
1635 /*
1636 * this search will find all the extents that end after
1637 * our range starts.
1638 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001639 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001640 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001641 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001642
Chris Masond3977122009-01-05 21:25:51 -05001643 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001644 state = rb_entry(node, struct extent_state, rb_node);
1645 if (state->start > search_end)
1646 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001647 if (contig && found && state->start > last + 1)
1648 break;
1649 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001650 total_bytes += min(search_end, state->end) + 1 -
1651 max(cur_start, state->start);
1652 if (total_bytes >= max_bytes)
1653 break;
1654 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001655 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001656 found = 1;
1657 }
Chris Masonec29ed52011-02-23 16:23:20 -05001658 last = state->end;
1659 } else if (contig && found) {
1660 break;
Chris Masond1310b22008-01-24 16:13:08 -05001661 }
1662 node = rb_next(node);
1663 if (!node)
1664 break;
1665 }
1666out:
Chris Masoncad321a2008-12-17 14:51:42 -05001667 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001668 return total_bytes;
1669}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001670
Chris Masond352ac62008-09-29 15:18:18 -04001671/*
1672 * set the private field for a given byte offset in the tree. If there isn't
1673 * an extent_state there already, this does nothing.
1674 */
Chris Masond1310b22008-01-24 16:13:08 -05001675int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1676{
1677 struct rb_node *node;
1678 struct extent_state *state;
1679 int ret = 0;
1680
Chris Masoncad321a2008-12-17 14:51:42 -05001681 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001682 /*
1683 * this search will find all the extents that end after
1684 * our range starts.
1685 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001686 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001687 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001688 ret = -ENOENT;
1689 goto out;
1690 }
1691 state = rb_entry(node, struct extent_state, rb_node);
1692 if (state->start != start) {
1693 ret = -ENOENT;
1694 goto out;
1695 }
1696 state->private = private;
1697out:
Chris Masoncad321a2008-12-17 14:51:42 -05001698 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001699 return ret;
1700}
1701
1702int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1703{
1704 struct rb_node *node;
1705 struct extent_state *state;
1706 int ret = 0;
1707
Chris Masoncad321a2008-12-17 14:51:42 -05001708 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001709 /*
1710 * this search will find all the extents that end after
1711 * our range starts.
1712 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001713 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001714 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001715 ret = -ENOENT;
1716 goto out;
1717 }
1718 state = rb_entry(node, struct extent_state, rb_node);
1719 if (state->start != start) {
1720 ret = -ENOENT;
1721 goto out;
1722 }
1723 *private = state->private;
1724out:
Chris Masoncad321a2008-12-17 14:51:42 -05001725 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001726 return ret;
1727}
1728
1729/*
1730 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001731 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001732 * has the bits set. Otherwise, 1 is returned if any bit in the
1733 * range is found set.
1734 */
1735int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001736 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001737{
1738 struct extent_state *state = NULL;
1739 struct rb_node *node;
1740 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001741
Chris Masoncad321a2008-12-17 14:51:42 -05001742 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001743 if (cached && cached->tree && cached->start <= start &&
1744 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001745 node = &cached->rb_node;
1746 else
1747 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001748 while (node && start <= end) {
1749 state = rb_entry(node, struct extent_state, rb_node);
1750
1751 if (filled && state->start > start) {
1752 bitset = 0;
1753 break;
1754 }
1755
1756 if (state->start > end)
1757 break;
1758
1759 if (state->state & bits) {
1760 bitset = 1;
1761 if (!filled)
1762 break;
1763 } else if (filled) {
1764 bitset = 0;
1765 break;
1766 }
Chris Mason46562ce2009-09-23 20:23:16 -04001767
1768 if (state->end == (u64)-1)
1769 break;
1770
Chris Masond1310b22008-01-24 16:13:08 -05001771 start = state->end + 1;
1772 if (start > end)
1773 break;
1774 node = rb_next(node);
1775 if (!node) {
1776 if (filled)
1777 bitset = 0;
1778 break;
1779 }
1780 }
Chris Masoncad321a2008-12-17 14:51:42 -05001781 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001782 return bitset;
1783}
Chris Masond1310b22008-01-24 16:13:08 -05001784
1785/*
1786 * helper function to set a given page up to date if all the
1787 * extents in the tree for that page are up to date
1788 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001789static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001790{
1791 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1792 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001793 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001794 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001795}
1796
1797/*
1798 * helper function to unlock a page if all the extents in the tree
1799 * for that page are unlocked
1800 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001801static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001802{
1803 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1804 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001805 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001806 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001807}
1808
1809/*
1810 * helper function to end page writeback if all the extents
1811 * in the tree for that page are done with writeback
1812 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001813static void check_page_writeback(struct extent_io_tree *tree,
1814 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001815{
Chris Mason1edbb732009-09-02 13:24:36 -04001816 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001817}
1818
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001819/*
1820 * When IO fails, either with EIO or csum verification fails, we
1821 * try other mirrors that might have a good copy of the data. This
1822 * io_failure_record is used to record state as we go through all the
1823 * mirrors. If another mirror has good data, the page is set up to date
1824 * and things continue. If a good mirror can't be found, the original
1825 * bio end_io callback is called to indicate things have failed.
1826 */
1827struct io_failure_record {
1828 struct page *page;
1829 u64 start;
1830 u64 len;
1831 u64 logical;
1832 unsigned long bio_flags;
1833 int this_mirror;
1834 int failed_mirror;
1835 int in_validation;
1836};
1837
1838static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1839 int did_repair)
1840{
1841 int ret;
1842 int err = 0;
1843 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1844
1845 set_state_private(failure_tree, rec->start, 0);
1846 ret = clear_extent_bits(failure_tree, rec->start,
1847 rec->start + rec->len - 1,
1848 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1849 if (ret)
1850 err = ret;
1851
1852 if (did_repair) {
1853 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1854 rec->start + rec->len - 1,
1855 EXTENT_DAMAGED, GFP_NOFS);
1856 if (ret && !err)
1857 err = ret;
1858 }
1859
1860 kfree(rec);
1861 return err;
1862}
1863
1864static void repair_io_failure_callback(struct bio *bio, int err)
1865{
1866 complete(bio->bi_private);
1867}
1868
1869/*
1870 * this bypasses the standard btrfs submit functions deliberately, as
1871 * the standard behavior is to write all copies in a raid setup. here we only
1872 * want to write the one bad copy. so we do the mapping for ourselves and issue
1873 * submit_bio directly.
1874 * to avoid any synchonization issues, wait for the data after writing, which
1875 * actually prevents the read that triggered the error from finishing.
1876 * currently, there can be no more than two copies of every data bit. thus,
1877 * exactly one rewrite is required.
1878 */
1879int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1880 u64 length, u64 logical, struct page *page,
1881 int mirror_num)
1882{
1883 struct bio *bio;
1884 struct btrfs_device *dev;
1885 DECLARE_COMPLETION_ONSTACK(compl);
1886 u64 map_length = 0;
1887 u64 sector;
1888 struct btrfs_bio *bbio = NULL;
1889 int ret;
1890
1891 BUG_ON(!mirror_num);
1892
1893 bio = bio_alloc(GFP_NOFS, 1);
1894 if (!bio)
1895 return -EIO;
1896 bio->bi_private = &compl;
1897 bio->bi_end_io = repair_io_failure_callback;
1898 bio->bi_size = 0;
1899 map_length = length;
1900
1901 ret = btrfs_map_block(map_tree, WRITE, logical,
1902 &map_length, &bbio, mirror_num);
1903 if (ret) {
1904 bio_put(bio);
1905 return -EIO;
1906 }
1907 BUG_ON(mirror_num != bbio->mirror_num);
1908 sector = bbio->stripes[mirror_num-1].physical >> 9;
1909 bio->bi_sector = sector;
1910 dev = bbio->stripes[mirror_num-1].dev;
1911 kfree(bbio);
1912 if (!dev || !dev->bdev || !dev->writeable) {
1913 bio_put(bio);
1914 return -EIO;
1915 }
1916 bio->bi_bdev = dev->bdev;
1917 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001918 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001919 wait_for_completion(&compl);
1920
1921 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1922 /* try to remap that extent elsewhere? */
1923 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001924 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001925 return -EIO;
1926 }
1927
Anand Jaind5b025d2012-07-02 22:05:21 -06001928 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04001929 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1930 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001931
1932 bio_put(bio);
1933 return 0;
1934}
1935
Josef Bacikea466792012-03-26 21:57:36 -04001936int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1937 int mirror_num)
1938{
1939 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1940 u64 start = eb->start;
1941 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001942 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001943
1944 for (i = 0; i < num_pages; i++) {
1945 struct page *p = extent_buffer_page(eb, i);
1946 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1947 start, p, mirror_num);
1948 if (ret)
1949 break;
1950 start += PAGE_CACHE_SIZE;
1951 }
1952
1953 return ret;
1954}
1955
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001956/*
1957 * each time an IO finishes, we do a fast check in the IO failure tree
1958 * to see if we need to process or clean up an io_failure_record
1959 */
1960static int clean_io_failure(u64 start, struct page *page)
1961{
1962 u64 private;
1963 u64 private_failure;
1964 struct io_failure_record *failrec;
1965 struct btrfs_mapping_tree *map_tree;
1966 struct extent_state *state;
1967 int num_copies;
1968 int did_repair = 0;
1969 int ret;
1970 struct inode *inode = page->mapping->host;
1971
1972 private = 0;
1973 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1974 (u64)-1, 1, EXTENT_DIRTY, 0);
1975 if (!ret)
1976 return 0;
1977
1978 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1979 &private_failure);
1980 if (ret)
1981 return 0;
1982
1983 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1984 BUG_ON(!failrec->this_mirror);
1985
1986 if (failrec->in_validation) {
1987 /* there was no real error, just free the record */
1988 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1989 failrec->start);
1990 did_repair = 1;
1991 goto out;
1992 }
1993
1994 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1995 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1996 failrec->start,
1997 EXTENT_LOCKED);
1998 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1999
2000 if (state && state->start == failrec->start) {
2001 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2002 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2003 failrec->len);
2004 if (num_copies > 1) {
2005 ret = repair_io_failure(map_tree, start, failrec->len,
2006 failrec->logical, page,
2007 failrec->failed_mirror);
2008 did_repair = !ret;
2009 }
2010 }
2011
2012out:
2013 if (!ret)
2014 ret = free_io_failure(inode, failrec, did_repair);
2015
2016 return ret;
2017}
2018
2019/*
2020 * this is a generic handler for readpage errors (default
2021 * readpage_io_failed_hook). if other copies exist, read those and write back
2022 * good data to the failed position. does not investigate in remapping the
2023 * failed extent elsewhere, hoping the device will be smart enough to do this as
2024 * needed
2025 */
2026
2027static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2028 u64 start, u64 end, int failed_mirror,
2029 struct extent_state *state)
2030{
2031 struct io_failure_record *failrec = NULL;
2032 u64 private;
2033 struct extent_map *em;
2034 struct inode *inode = page->mapping->host;
2035 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2036 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2037 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2038 struct bio *bio;
2039 int num_copies;
2040 int ret;
2041 int read_mode;
2042 u64 logical;
2043
2044 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2045
2046 ret = get_state_private(failure_tree, start, &private);
2047 if (ret) {
2048 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2049 if (!failrec)
2050 return -ENOMEM;
2051 failrec->start = start;
2052 failrec->len = end - start + 1;
2053 failrec->this_mirror = 0;
2054 failrec->bio_flags = 0;
2055 failrec->in_validation = 0;
2056
2057 read_lock(&em_tree->lock);
2058 em = lookup_extent_mapping(em_tree, start, failrec->len);
2059 if (!em) {
2060 read_unlock(&em_tree->lock);
2061 kfree(failrec);
2062 return -EIO;
2063 }
2064
2065 if (em->start > start || em->start + em->len < start) {
2066 free_extent_map(em);
2067 em = NULL;
2068 }
2069 read_unlock(&em_tree->lock);
2070
2071 if (!em || IS_ERR(em)) {
2072 kfree(failrec);
2073 return -EIO;
2074 }
2075 logical = start - em->start;
2076 logical = em->block_start + logical;
2077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2078 logical = em->block_start;
2079 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2080 extent_set_compress_type(&failrec->bio_flags,
2081 em->compress_type);
2082 }
2083 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2084 "len=%llu\n", logical, start, failrec->len);
2085 failrec->logical = logical;
2086 free_extent_map(em);
2087
2088 /* set the bits in the private failure tree */
2089 ret = set_extent_bits(failure_tree, start, end,
2090 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2091 if (ret >= 0)
2092 ret = set_state_private(failure_tree, start,
2093 (u64)(unsigned long)failrec);
2094 /* set the bits in the inode's tree */
2095 if (ret >= 0)
2096 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2097 GFP_NOFS);
2098 if (ret < 0) {
2099 kfree(failrec);
2100 return ret;
2101 }
2102 } else {
2103 failrec = (struct io_failure_record *)(unsigned long)private;
2104 pr_debug("bio_readpage_error: (found) logical=%llu, "
2105 "start=%llu, len=%llu, validation=%d\n",
2106 failrec->logical, failrec->start, failrec->len,
2107 failrec->in_validation);
2108 /*
2109 * when data can be on disk more than twice, add to failrec here
2110 * (e.g. with a list for failed_mirror) to make
2111 * clean_io_failure() clean all those errors at once.
2112 */
2113 }
2114 num_copies = btrfs_num_copies(
2115 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2116 failrec->logical, failrec->len);
2117 if (num_copies == 1) {
2118 /*
2119 * we only have a single copy of the data, so don't bother with
2120 * all the retry and error correction code that follows. no
2121 * matter what the error is, it is very likely to persist.
2122 */
2123 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2124 "state=%p, num_copies=%d, next_mirror %d, "
2125 "failed_mirror %d\n", state, num_copies,
2126 failrec->this_mirror, failed_mirror);
2127 free_io_failure(inode, failrec, 0);
2128 return -EIO;
2129 }
2130
2131 if (!state) {
2132 spin_lock(&tree->lock);
2133 state = find_first_extent_bit_state(tree, failrec->start,
2134 EXTENT_LOCKED);
2135 if (state && state->start != failrec->start)
2136 state = NULL;
2137 spin_unlock(&tree->lock);
2138 }
2139
2140 /*
2141 * there are two premises:
2142 * a) deliver good data to the caller
2143 * b) correct the bad sectors on disk
2144 */
2145 if (failed_bio->bi_vcnt > 1) {
2146 /*
2147 * to fulfill b), we need to know the exact failing sectors, as
2148 * we don't want to rewrite any more than the failed ones. thus,
2149 * we need separate read requests for the failed bio
2150 *
2151 * if the following BUG_ON triggers, our validation request got
2152 * merged. we need separate requests for our algorithm to work.
2153 */
2154 BUG_ON(failrec->in_validation);
2155 failrec->in_validation = 1;
2156 failrec->this_mirror = failed_mirror;
2157 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2158 } else {
2159 /*
2160 * we're ready to fulfill a) and b) alongside. get a good copy
2161 * of the failed sector and if we succeed, we have setup
2162 * everything for repair_io_failure to do the rest for us.
2163 */
2164 if (failrec->in_validation) {
2165 BUG_ON(failrec->this_mirror != failed_mirror);
2166 failrec->in_validation = 0;
2167 failrec->this_mirror = 0;
2168 }
2169 failrec->failed_mirror = failed_mirror;
2170 failrec->this_mirror++;
2171 if (failrec->this_mirror == failed_mirror)
2172 failrec->this_mirror++;
2173 read_mode = READ_SYNC;
2174 }
2175
2176 if (!state || failrec->this_mirror > num_copies) {
2177 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2178 "next_mirror %d, failed_mirror %d\n", state,
2179 num_copies, failrec->this_mirror, failed_mirror);
2180 free_io_failure(inode, failrec, 0);
2181 return -EIO;
2182 }
2183
2184 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002185 if (!bio) {
2186 free_io_failure(inode, failrec, 0);
2187 return -EIO;
2188 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002189 bio->bi_private = state;
2190 bio->bi_end_io = failed_bio->bi_end_io;
2191 bio->bi_sector = failrec->logical >> 9;
2192 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2193 bio->bi_size = 0;
2194
2195 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2196
2197 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2198 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2199 failrec->this_mirror, num_copies, failrec->in_validation);
2200
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002201 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2202 failrec->this_mirror,
2203 failrec->bio_flags, 0);
2204 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205}
2206
Chris Masond1310b22008-01-24 16:13:08 -05002207/* lots and lots of room for performance fixes in the end_bio funcs */
2208
Jeff Mahoney87826df2012-02-15 16:23:57 +01002209int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2210{
2211 int uptodate = (err == 0);
2212 struct extent_io_tree *tree;
2213 int ret;
2214
2215 tree = &BTRFS_I(page->mapping->host)->io_tree;
2216
2217 if (tree->ops && tree->ops->writepage_end_io_hook) {
2218 ret = tree->ops->writepage_end_io_hook(page, start,
2219 end, NULL, uptodate);
2220 if (ret)
2221 uptodate = 0;
2222 }
2223
Jeff Mahoney87826df2012-02-15 16:23:57 +01002224 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002225 ClearPageUptodate(page);
2226 SetPageError(page);
2227 }
2228 return 0;
2229}
2230
Chris Masond1310b22008-01-24 16:13:08 -05002231/*
2232 * after a writepage IO is done, we need to:
2233 * clear the uptodate bits on error
2234 * clear the writeback bits in the extent tree for this IO
2235 * end_page_writeback if the page has no more pending IO
2236 *
2237 * Scheduling is not allowed, so the extent state tree is expected
2238 * to have one and only one object corresponding to this IO.
2239 */
Chris Masond1310b22008-01-24 16:13:08 -05002240static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002241{
Chris Masond1310b22008-01-24 16:13:08 -05002242 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002243 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002244 u64 start;
2245 u64 end;
2246 int whole_page;
2247
Chris Masond1310b22008-01-24 16:13:08 -05002248 do {
2249 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002250 tree = &BTRFS_I(page->mapping->host)->io_tree;
2251
Chris Masond1310b22008-01-24 16:13:08 -05002252 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2253 bvec->bv_offset;
2254 end = start + bvec->bv_len - 1;
2255
2256 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2257 whole_page = 1;
2258 else
2259 whole_page = 0;
2260
2261 if (--bvec >= bio->bi_io_vec)
2262 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002263
Jeff Mahoney87826df2012-02-15 16:23:57 +01002264 if (end_extent_writepage(page, err, start, end))
2265 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002266
Chris Masond1310b22008-01-24 16:13:08 -05002267 if (whole_page)
2268 end_page_writeback(page);
2269 else
2270 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002271 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002272
Chris Masond1310b22008-01-24 16:13:08 -05002273 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002274}
2275
2276/*
2277 * after a readpage IO is done, we need to:
2278 * clear the uptodate bits on error
2279 * set the uptodate bits if things worked
2280 * set the page up to date if all extents in the tree are uptodate
2281 * clear the lock bit in the extent tree
2282 * unlock the page if there are no other extents locked for it
2283 *
2284 * Scheduling is not allowed, so the extent state tree is expected
2285 * to have one and only one object corresponding to this IO.
2286 */
Chris Masond1310b22008-01-24 16:13:08 -05002287static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002288{
2289 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002290 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2291 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002292 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002293 u64 start;
2294 u64 end;
2295 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002296 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002297 int ret;
2298
Chris Masond20f7042008-12-08 16:58:54 -05002299 if (err)
2300 uptodate = 0;
2301
Chris Masond1310b22008-01-24 16:13:08 -05002302 do {
2303 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002304 struct extent_state *cached = NULL;
2305 struct extent_state *state;
2306
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2308 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2309 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002310 tree = &BTRFS_I(page->mapping->host)->io_tree;
2311
Chris Masond1310b22008-01-24 16:13:08 -05002312 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2313 bvec->bv_offset;
2314 end = start + bvec->bv_len - 1;
2315
2316 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2317 whole_page = 1;
2318 else
2319 whole_page = 0;
2320
Chris Mason4125bf72010-02-03 18:18:45 +00002321 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002322 prefetchw(&bvec->bv_page->flags);
2323
Arne Jansen507903b2011-04-06 10:02:20 +00002324 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002325 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002326 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002327 /*
2328 * take a reference on the state, unlock will drop
2329 * the ref
2330 */
2331 cache_state(state, &cached);
2332 }
2333 spin_unlock(&tree->lock);
2334
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002335 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002336 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002337 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002338 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002339 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002340 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002341 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002342 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002343 }
Josef Bacikea466792012-03-26 21:57:36 -04002344
Josef Bacikea466792012-03-26 21:57:36 -04002345 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002346 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002347 if (!ret && !err &&
2348 test_bit(BIO_UPTODATE, &bio->bi_flags))
2349 uptodate = 1;
2350 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002351 /*
2352 * The generic bio_readpage_error handles errors the
2353 * following way: If possible, new read requests are
2354 * created and submitted and will end up in
2355 * end_bio_extent_readpage as well (if we're lucky, not
2356 * in the !uptodate case). In that case it returns 0 and
2357 * we just go on with the next page in our bio. If it
2358 * can't handle the error it will return -EIO and we
2359 * remain responsible for that page.
2360 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002361 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002362 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002363 uptodate =
2364 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002365 if (err)
2366 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002367 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002368 continue;
2369 }
2370 }
Chris Mason70dec802008-01-29 09:59:12 -05002371
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002372 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002373 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002374 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002375 }
Arne Jansen507903b2011-04-06 10:02:20 +00002376 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002377
Chris Mason70dec802008-01-29 09:59:12 -05002378 if (whole_page) {
2379 if (uptodate) {
2380 SetPageUptodate(page);
2381 } else {
2382 ClearPageUptodate(page);
2383 SetPageError(page);
2384 }
Chris Masond1310b22008-01-24 16:13:08 -05002385 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002386 } else {
2387 if (uptodate) {
2388 check_page_uptodate(tree, page);
2389 } else {
2390 ClearPageUptodate(page);
2391 SetPageError(page);
2392 }
Chris Masond1310b22008-01-24 16:13:08 -05002393 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002394 }
Chris Mason4125bf72010-02-03 18:18:45 +00002395 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002396
2397 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002398}
2399
Miao Xie88f794e2010-11-22 03:02:55 +00002400struct bio *
2401btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2402 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002403{
2404 struct bio *bio;
2405
2406 bio = bio_alloc(gfp_flags, nr_vecs);
2407
2408 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2409 while (!bio && (nr_vecs /= 2))
2410 bio = bio_alloc(gfp_flags, nr_vecs);
2411 }
2412
2413 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002414 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002415 bio->bi_bdev = bdev;
2416 bio->bi_sector = first_sector;
2417 }
2418 return bio;
2419}
2420
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002421/*
2422 * Since writes are async, they will only return -ENOMEM.
2423 * Reads can return the full range of I/O error conditions.
2424 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002425static int __must_check submit_one_bio(int rw, struct bio *bio,
2426 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002427{
Chris Masond1310b22008-01-24 16:13:08 -05002428 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002429 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2430 struct page *page = bvec->bv_page;
2431 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002432 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002433
2434 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002435
David Woodhouse902b22f2008-08-20 08:51:49 -04002436 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002437
2438 bio_get(bio);
2439
Chris Mason065631f2008-02-20 12:07:25 -05002440 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002441 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002442 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002443 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002444 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002445
Chris Masond1310b22008-01-24 16:13:08 -05002446 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2447 ret = -EOPNOTSUPP;
2448 bio_put(bio);
2449 return ret;
2450}
2451
Jeff Mahoney3444a972011-10-03 23:23:13 -04002452static int merge_bio(struct extent_io_tree *tree, struct page *page,
2453 unsigned long offset, size_t size, struct bio *bio,
2454 unsigned long bio_flags)
2455{
2456 int ret = 0;
2457 if (tree->ops && tree->ops->merge_bio_hook)
2458 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2459 bio_flags);
2460 BUG_ON(ret < 0);
2461 return ret;
2462
2463}
2464
Chris Masond1310b22008-01-24 16:13:08 -05002465static int submit_extent_page(int rw, struct extent_io_tree *tree,
2466 struct page *page, sector_t sector,
2467 size_t size, unsigned long offset,
2468 struct block_device *bdev,
2469 struct bio **bio_ret,
2470 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002471 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002472 int mirror_num,
2473 unsigned long prev_bio_flags,
2474 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002475{
2476 int ret = 0;
2477 struct bio *bio;
2478 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002479 int contig = 0;
2480 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2481 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002482 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002483
2484 if (bio_ret && *bio_ret) {
2485 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002486 if (old_compressed)
2487 contig = bio->bi_sector == sector;
2488 else
2489 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2490 sector;
2491
2492 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002493 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002494 bio_add_page(bio, page, page_size, offset) < page_size) {
2495 ret = submit_one_bio(rw, bio, mirror_num,
2496 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002497 if (ret < 0)
2498 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002499 bio = NULL;
2500 } else {
2501 return 0;
2502 }
2503 }
Chris Masonc8b97812008-10-29 14:49:59 -04002504 if (this_compressed)
2505 nr = BIO_MAX_PAGES;
2506 else
2507 nr = bio_get_nr_vecs(bdev);
2508
Miao Xie88f794e2010-11-22 03:02:55 +00002509 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002510 if (!bio)
2511 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002512
Chris Masonc8b97812008-10-29 14:49:59 -04002513 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002514 bio->bi_end_io = end_io_func;
2515 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002516
Chris Masond3977122009-01-05 21:25:51 -05002517 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002518 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002519 else
Chris Masonc8b97812008-10-29 14:49:59 -04002520 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002521
2522 return ret;
2523}
2524
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002525void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2526{
2527 if (!PagePrivate(page)) {
2528 SetPagePrivate(page);
2529 page_cache_get(page);
2530 set_page_private(page, (unsigned long)eb);
2531 } else {
2532 WARN_ON(page->private != (unsigned long)eb);
2533 }
2534}
2535
Chris Masond1310b22008-01-24 16:13:08 -05002536void set_page_extent_mapped(struct page *page)
2537{
2538 if (!PagePrivate(page)) {
2539 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002540 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002541 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002542 }
2543}
2544
Chris Masond1310b22008-01-24 16:13:08 -05002545/*
2546 * basic readpage implementation. Locked extent state structs are inserted
2547 * into the tree that are removed when the IO is done (by the end_io
2548 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002549 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002550 */
2551static int __extent_read_full_page(struct extent_io_tree *tree,
2552 struct page *page,
2553 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002554 struct bio **bio, int mirror_num,
2555 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002556{
2557 struct inode *inode = page->mapping->host;
2558 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2559 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2560 u64 end;
2561 u64 cur = start;
2562 u64 extent_offset;
2563 u64 last_byte = i_size_read(inode);
2564 u64 block_start;
2565 u64 cur_end;
2566 sector_t sector;
2567 struct extent_map *em;
2568 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002569 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002570 int ret;
2571 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002572 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002573 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002574 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002575 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002576 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002577
2578 set_page_extent_mapped(page);
2579
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002580 if (!PageUptodate(page)) {
2581 if (cleancache_get_page(page) == 0) {
2582 BUG_ON(blocksize != PAGE_SIZE);
2583 goto out;
2584 }
2585 }
2586
Chris Masond1310b22008-01-24 16:13:08 -05002587 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002588 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002589 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002590 ordered = btrfs_lookup_ordered_extent(inode, start);
2591 if (!ordered)
2592 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002593 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002594 btrfs_start_ordered_extent(inode, ordered, 1);
2595 btrfs_put_ordered_extent(ordered);
2596 }
Chris Masond1310b22008-01-24 16:13:08 -05002597
Chris Masonc8b97812008-10-29 14:49:59 -04002598 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2599 char *userpage;
2600 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2601
2602 if (zero_offset) {
2603 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002604 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002605 memset(userpage + zero_offset, 0, iosize);
2606 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002607 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002608 }
2609 }
Chris Masond1310b22008-01-24 16:13:08 -05002610 while (cur <= end) {
2611 if (cur >= last_byte) {
2612 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002613 struct extent_state *cached = NULL;
2614
David Sterba306e16c2011-04-19 14:29:38 +02002615 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002616 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002617 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002618 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002619 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002620 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002621 &cached, GFP_NOFS);
2622 unlock_extent_cached(tree, cur, cur + iosize - 1,
2623 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002624 break;
2625 }
David Sterba306e16c2011-04-19 14:29:38 +02002626 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002627 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002628 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002629 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002630 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002631 break;
2632 }
Chris Masond1310b22008-01-24 16:13:08 -05002633 extent_offset = cur - em->start;
2634 BUG_ON(extent_map_end(em) <= cur);
2635 BUG_ON(end < cur);
2636
Li Zefan261507a02010-12-17 14:21:50 +08002637 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002638 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002639 extent_set_compress_type(&this_bio_flag,
2640 em->compress_type);
2641 }
Chris Masonc8b97812008-10-29 14:49:59 -04002642
Chris Masond1310b22008-01-24 16:13:08 -05002643 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2644 cur_end = min(extent_map_end(em) - 1, end);
2645 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002646 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2647 disk_io_size = em->block_len;
2648 sector = em->block_start >> 9;
2649 } else {
2650 sector = (em->block_start + extent_offset) >> 9;
2651 disk_io_size = iosize;
2652 }
Chris Masond1310b22008-01-24 16:13:08 -05002653 bdev = em->bdev;
2654 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002655 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2656 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002657 free_extent_map(em);
2658 em = NULL;
2659
2660 /* we've found a hole, just zero and go on */
2661 if (block_start == EXTENT_MAP_HOLE) {
2662 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002663 struct extent_state *cached = NULL;
2664
Cong Wang7ac687d2011-11-25 23:14:28 +08002665 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002666 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002667 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002668 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002669
2670 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002671 &cached, GFP_NOFS);
2672 unlock_extent_cached(tree, cur, cur + iosize - 1,
2673 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002674 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002675 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002676 continue;
2677 }
2678 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002679 if (test_range_bit(tree, cur, cur_end,
2680 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002681 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002682 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002683 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002684 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002685 continue;
2686 }
Chris Mason70dec802008-01-29 09:59:12 -05002687 /* we have an inline extent but it didn't get marked up
2688 * to date. Error out
2689 */
2690 if (block_start == EXTENT_MAP_INLINE) {
2691 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002692 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002693 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002694 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002695 continue;
2696 }
Chris Masond1310b22008-01-24 16:13:08 -05002697
2698 ret = 0;
2699 if (tree->ops && tree->ops->readpage_io_hook) {
2700 ret = tree->ops->readpage_io_hook(page, cur,
2701 cur + iosize - 1);
2702 }
2703 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002704 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2705 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002706 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002707 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002708 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002709 end_bio_extent_readpage, mirror_num,
2710 *bio_flags,
2711 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002712 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002713 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002714 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002715 }
2716 if (ret)
2717 SetPageError(page);
2718 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002719 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002720 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002721out:
Chris Masond1310b22008-01-24 16:13:08 -05002722 if (!nr) {
2723 if (!PageError(page))
2724 SetPageUptodate(page);
2725 unlock_page(page);
2726 }
2727 return 0;
2728}
2729
2730int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002731 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002732{
2733 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002734 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002735 int ret;
2736
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002737 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002738 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002739 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002740 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002741 return ret;
2742}
Chris Masond1310b22008-01-24 16:13:08 -05002743
Chris Mason11c83492009-04-20 15:50:09 -04002744static noinline void update_nr_written(struct page *page,
2745 struct writeback_control *wbc,
2746 unsigned long nr_written)
2747{
2748 wbc->nr_to_write -= nr_written;
2749 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2750 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2751 page->mapping->writeback_index = page->index + nr_written;
2752}
2753
Chris Masond1310b22008-01-24 16:13:08 -05002754/*
2755 * the writepage semantics are similar to regular writepage. extent
2756 * records are inserted to lock ranges in the tree, and as dirty areas
2757 * are found, they are marked writeback. Then the lock bits are removed
2758 * and the end_io handler clears the writeback ranges
2759 */
2760static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2761 void *data)
2762{
2763 struct inode *inode = page->mapping->host;
2764 struct extent_page_data *epd = data;
2765 struct extent_io_tree *tree = epd->tree;
2766 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2767 u64 delalloc_start;
2768 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2769 u64 end;
2770 u64 cur = start;
2771 u64 extent_offset;
2772 u64 last_byte = i_size_read(inode);
2773 u64 block_start;
2774 u64 iosize;
2775 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002776 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002777 struct extent_map *em;
2778 struct block_device *bdev;
2779 int ret;
2780 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002781 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002782 size_t blocksize;
2783 loff_t i_size = i_size_read(inode);
2784 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2785 u64 nr_delalloc;
2786 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002787 int page_started;
2788 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002789 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002790 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002791 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002792
Chris Masonffbd5172009-04-20 15:50:09 -04002793 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002794 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002795 else
2796 write_flags = WRITE;
2797
liubo1abe9b82011-03-24 11:18:59 +00002798 trace___extent_writepage(page, inode, wbc);
2799
Chris Masond1310b22008-01-24 16:13:08 -05002800 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002801
2802 ClearPageError(page);
2803
Chris Mason7f3c74f2008-07-18 12:01:11 -04002804 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002805 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002806 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002807 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002808 unlock_page(page);
2809 return 0;
2810 }
2811
2812 if (page->index == end_index) {
2813 char *userpage;
2814
Cong Wang7ac687d2011-11-25 23:14:28 +08002815 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002816 memset(userpage + pg_offset, 0,
2817 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002818 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002819 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002820 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002821 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002822
2823 set_page_extent_mapped(page);
2824
Josef Bacik9e487102011-08-01 12:08:18 -04002825 if (!tree->ops || !tree->ops->fill_delalloc)
2826 fill_delalloc = false;
2827
Chris Masond1310b22008-01-24 16:13:08 -05002828 delalloc_start = start;
2829 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002830 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002831 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002832 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002833 /*
2834 * make sure the wbc mapping index is at least updated
2835 * to this page.
2836 */
2837 update_nr_written(page, wbc, 0);
2838
Chris Masond3977122009-01-05 21:25:51 -05002839 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002840 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002841 page,
2842 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002843 &delalloc_end,
2844 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002845 if (nr_delalloc == 0) {
2846 delalloc_start = delalloc_end + 1;
2847 continue;
2848 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002849 ret = tree->ops->fill_delalloc(inode, page,
2850 delalloc_start,
2851 delalloc_end,
2852 &page_started,
2853 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002854 /* File system has been set read-only */
2855 if (ret) {
2856 SetPageError(page);
2857 goto done;
2858 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002859 /*
2860 * delalloc_end is already one less than the total
2861 * length, so we don't subtract one from
2862 * PAGE_CACHE_SIZE
2863 */
2864 delalloc_to_write += (delalloc_end - delalloc_start +
2865 PAGE_CACHE_SIZE) >>
2866 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002867 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002868 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002869 if (wbc->nr_to_write < delalloc_to_write) {
2870 int thresh = 8192;
2871
2872 if (delalloc_to_write < thresh * 2)
2873 thresh = delalloc_to_write;
2874 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2875 thresh);
2876 }
Chris Masonc8b97812008-10-29 14:49:59 -04002877
Chris Mason771ed682008-11-06 22:02:51 -05002878 /* did the fill delalloc function already unlock and start
2879 * the IO?
2880 */
2881 if (page_started) {
2882 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002883 /*
2884 * we've unlocked the page, so we can't update
2885 * the mapping's writeback index, just update
2886 * nr_to_write.
2887 */
2888 wbc->nr_to_write -= nr_written;
2889 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002890 }
Chris Masonc8b97812008-10-29 14:49:59 -04002891 }
Chris Mason247e7432008-07-17 12:53:51 -04002892 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002893 ret = tree->ops->writepage_start_hook(page, start,
2894 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002895 if (ret) {
2896 /* Fixup worker will requeue */
2897 if (ret == -EBUSY)
2898 wbc->pages_skipped++;
2899 else
2900 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002901 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002902 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002903 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002904 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002905 }
2906 }
2907
Chris Mason11c83492009-04-20 15:50:09 -04002908 /*
2909 * we don't want to touch the inode after unlocking the page,
2910 * so we update the mapping writeback index now
2911 */
2912 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002913
Chris Masond1310b22008-01-24 16:13:08 -05002914 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002915 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916 if (tree->ops && tree->ops->writepage_end_io_hook)
2917 tree->ops->writepage_end_io_hook(page, start,
2918 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002919 goto done;
2920 }
2921
Chris Masond1310b22008-01-24 16:13:08 -05002922 blocksize = inode->i_sb->s_blocksize;
2923
2924 while (cur <= end) {
2925 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926 if (tree->ops && tree->ops->writepage_end_io_hook)
2927 tree->ops->writepage_end_io_hook(page, cur,
2928 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002929 break;
2930 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002931 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002932 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002933 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002934 SetPageError(page);
2935 break;
2936 }
2937
2938 extent_offset = cur - em->start;
2939 BUG_ON(extent_map_end(em) <= cur);
2940 BUG_ON(end < cur);
2941 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2942 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2943 sector = (em->block_start + extent_offset) >> 9;
2944 bdev = em->bdev;
2945 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002946 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002947 free_extent_map(em);
2948 em = NULL;
2949
Chris Masonc8b97812008-10-29 14:49:59 -04002950 /*
2951 * compressed and inline extents are written through other
2952 * paths in the FS
2953 */
2954 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002955 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002956 /*
2957 * end_io notification does not happen here for
2958 * compressed extents
2959 */
2960 if (!compressed && tree->ops &&
2961 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002962 tree->ops->writepage_end_io_hook(page, cur,
2963 cur + iosize - 1,
2964 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002965 else if (compressed) {
2966 /* we don't want to end_page_writeback on
2967 * a compressed extent. this happens
2968 * elsewhere
2969 */
2970 nr++;
2971 }
2972
2973 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002974 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002975 continue;
2976 }
Chris Masond1310b22008-01-24 16:13:08 -05002977 /* leave this out until we have a page_mkwrite call */
2978 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002979 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002980 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002981 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002982 continue;
2983 }
Chris Masonc8b97812008-10-29 14:49:59 -04002984
Chris Masond1310b22008-01-24 16:13:08 -05002985 if (tree->ops && tree->ops->writepage_io_hook) {
2986 ret = tree->ops->writepage_io_hook(page, cur,
2987 cur + iosize - 1);
2988 } else {
2989 ret = 0;
2990 }
Chris Mason1259ab72008-05-12 13:39:03 -04002991 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002992 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002993 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002994 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002995
Chris Masond1310b22008-01-24 16:13:08 -05002996 set_range_writeback(tree, cur, cur + iosize - 1);
2997 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002998 printk(KERN_ERR "btrfs warning page %lu not "
2999 "writeback, cur %llu end %llu\n",
3000 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003001 (unsigned long long)end);
3002 }
3003
Chris Masonffbd5172009-04-20 15:50:09 -04003004 ret = submit_extent_page(write_flags, tree, page,
3005 sector, iosize, pg_offset,
3006 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003007 end_bio_extent_writepage,
3008 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003009 if (ret)
3010 SetPageError(page);
3011 }
3012 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003013 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003014 nr++;
3015 }
3016done:
3017 if (nr == 0) {
3018 /* make sure the mapping tag for page dirty gets cleared */
3019 set_page_writeback(page);
3020 end_page_writeback(page);
3021 }
Chris Masond1310b22008-01-24 16:13:08 -05003022 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003023
Chris Mason11c83492009-04-20 15:50:09 -04003024done_unlocked:
3025
Chris Mason2c64c532009-09-02 15:04:12 -04003026 /* drop our reference on any cached states */
3027 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003028 return 0;
3029}
3030
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003031static int eb_wait(void *word)
3032{
3033 io_schedule();
3034 return 0;
3035}
3036
3037static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3038{
3039 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3040 TASK_UNINTERRUPTIBLE);
3041}
3042
3043static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3044 struct btrfs_fs_info *fs_info,
3045 struct extent_page_data *epd)
3046{
3047 unsigned long i, num_pages;
3048 int flush = 0;
3049 int ret = 0;
3050
3051 if (!btrfs_try_tree_write_lock(eb)) {
3052 flush = 1;
3053 flush_write_bio(epd);
3054 btrfs_tree_lock(eb);
3055 }
3056
3057 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3058 btrfs_tree_unlock(eb);
3059 if (!epd->sync_io)
3060 return 0;
3061 if (!flush) {
3062 flush_write_bio(epd);
3063 flush = 1;
3064 }
Chris Masona098d8e2012-03-21 12:09:56 -04003065 while (1) {
3066 wait_on_extent_buffer_writeback(eb);
3067 btrfs_tree_lock(eb);
3068 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3069 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003070 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003071 }
3072 }
3073
Josef Bacik51561ff2012-07-20 16:25:24 -04003074 /*
3075 * We need to do this to prevent races in people who check if the eb is
3076 * under IO since we can end up having no IO bits set for a short period
3077 * of time.
3078 */
3079 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003080 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3081 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003082 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003083 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3084 spin_lock(&fs_info->delalloc_lock);
3085 if (fs_info->dirty_metadata_bytes >= eb->len)
3086 fs_info->dirty_metadata_bytes -= eb->len;
3087 else
3088 WARN_ON(1);
3089 spin_unlock(&fs_info->delalloc_lock);
3090 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003091 } else {
3092 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003093 }
3094
3095 btrfs_tree_unlock(eb);
3096
3097 if (!ret)
3098 return ret;
3099
3100 num_pages = num_extent_pages(eb->start, eb->len);
3101 for (i = 0; i < num_pages; i++) {
3102 struct page *p = extent_buffer_page(eb, i);
3103
3104 if (!trylock_page(p)) {
3105 if (!flush) {
3106 flush_write_bio(epd);
3107 flush = 1;
3108 }
3109 lock_page(p);
3110 }
3111 }
3112
3113 return ret;
3114}
3115
3116static void end_extent_buffer_writeback(struct extent_buffer *eb)
3117{
3118 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3119 smp_mb__after_clear_bit();
3120 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3121}
3122
3123static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3124{
3125 int uptodate = err == 0;
3126 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3127 struct extent_buffer *eb;
3128 int done;
3129
3130 do {
3131 struct page *page = bvec->bv_page;
3132
3133 bvec--;
3134 eb = (struct extent_buffer *)page->private;
3135 BUG_ON(!eb);
3136 done = atomic_dec_and_test(&eb->io_pages);
3137
3138 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3139 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3140 ClearPageUptodate(page);
3141 SetPageError(page);
3142 }
3143
3144 end_page_writeback(page);
3145
3146 if (!done)
3147 continue;
3148
3149 end_extent_buffer_writeback(eb);
3150 } while (bvec >= bio->bi_io_vec);
3151
3152 bio_put(bio);
3153
3154}
3155
3156static int write_one_eb(struct extent_buffer *eb,
3157 struct btrfs_fs_info *fs_info,
3158 struct writeback_control *wbc,
3159 struct extent_page_data *epd)
3160{
3161 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3162 u64 offset = eb->start;
3163 unsigned long i, num_pages;
3164 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003165 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003166
3167 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3168 num_pages = num_extent_pages(eb->start, eb->len);
3169 atomic_set(&eb->io_pages, num_pages);
3170 for (i = 0; i < num_pages; i++) {
3171 struct page *p = extent_buffer_page(eb, i);
3172
3173 clear_page_dirty_for_io(p);
3174 set_page_writeback(p);
3175 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3176 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3177 -1, end_bio_extent_buffer_writepage,
3178 0, 0, 0);
3179 if (ret) {
3180 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3181 SetPageError(p);
3182 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3183 end_extent_buffer_writeback(eb);
3184 ret = -EIO;
3185 break;
3186 }
3187 offset += PAGE_CACHE_SIZE;
3188 update_nr_written(p, wbc, 1);
3189 unlock_page(p);
3190 }
3191
3192 if (unlikely(ret)) {
3193 for (; i < num_pages; i++) {
3194 struct page *p = extent_buffer_page(eb, i);
3195 unlock_page(p);
3196 }
3197 }
3198
3199 return ret;
3200}
3201
3202int btree_write_cache_pages(struct address_space *mapping,
3203 struct writeback_control *wbc)
3204{
3205 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3206 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3207 struct extent_buffer *eb, *prev_eb = NULL;
3208 struct extent_page_data epd = {
3209 .bio = NULL,
3210 .tree = tree,
3211 .extent_locked = 0,
3212 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3213 };
3214 int ret = 0;
3215 int done = 0;
3216 int nr_to_write_done = 0;
3217 struct pagevec pvec;
3218 int nr_pages;
3219 pgoff_t index;
3220 pgoff_t end; /* Inclusive */
3221 int scanned = 0;
3222 int tag;
3223
3224 pagevec_init(&pvec, 0);
3225 if (wbc->range_cyclic) {
3226 index = mapping->writeback_index; /* Start from prev offset */
3227 end = -1;
3228 } else {
3229 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3230 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3231 scanned = 1;
3232 }
3233 if (wbc->sync_mode == WB_SYNC_ALL)
3234 tag = PAGECACHE_TAG_TOWRITE;
3235 else
3236 tag = PAGECACHE_TAG_DIRTY;
3237retry:
3238 if (wbc->sync_mode == WB_SYNC_ALL)
3239 tag_pages_for_writeback(mapping, index, end);
3240 while (!done && !nr_to_write_done && (index <= end) &&
3241 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3242 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3243 unsigned i;
3244
3245 scanned = 1;
3246 for (i = 0; i < nr_pages; i++) {
3247 struct page *page = pvec.pages[i];
3248
3249 if (!PagePrivate(page))
3250 continue;
3251
3252 if (!wbc->range_cyclic && page->index > end) {
3253 done = 1;
3254 break;
3255 }
3256
3257 eb = (struct extent_buffer *)page->private;
3258 if (!eb) {
3259 WARN_ON(1);
3260 continue;
3261 }
3262
3263 if (eb == prev_eb)
3264 continue;
3265
3266 if (!atomic_inc_not_zero(&eb->refs)) {
3267 WARN_ON(1);
3268 continue;
3269 }
3270
3271 prev_eb = eb;
3272 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3273 if (!ret) {
3274 free_extent_buffer(eb);
3275 continue;
3276 }
3277
3278 ret = write_one_eb(eb, fs_info, wbc, &epd);
3279 if (ret) {
3280 done = 1;
3281 free_extent_buffer(eb);
3282 break;
3283 }
3284 free_extent_buffer(eb);
3285
3286 /*
3287 * the filesystem may choose to bump up nr_to_write.
3288 * We have to make sure to honor the new nr_to_write
3289 * at any time
3290 */
3291 nr_to_write_done = wbc->nr_to_write <= 0;
3292 }
3293 pagevec_release(&pvec);
3294 cond_resched();
3295 }
3296 if (!scanned && !done) {
3297 /*
3298 * We hit the last page and there is more work to be done: wrap
3299 * back to the start of the file
3300 */
3301 scanned = 1;
3302 index = 0;
3303 goto retry;
3304 }
3305 flush_write_bio(&epd);
3306 return ret;
3307}
3308
Chris Masond1310b22008-01-24 16:13:08 -05003309/**
Chris Mason4bef0842008-09-08 11:18:08 -04003310 * 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 -05003311 * @mapping: address space structure to write
3312 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3313 * @writepage: function called for each page
3314 * @data: data passed to writepage function
3315 *
3316 * If a page is already under I/O, write_cache_pages() skips it, even
3317 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3318 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3319 * and msync() need to guarantee that all the data which was dirty at the time
3320 * the call was made get new I/O started against them. If wbc->sync_mode is
3321 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3322 * existing IO to complete.
3323 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003324static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003325 struct address_space *mapping,
3326 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003327 writepage_t writepage, void *data,
3328 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003329{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003330 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003331 int ret = 0;
3332 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003333 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003334 struct pagevec pvec;
3335 int nr_pages;
3336 pgoff_t index;
3337 pgoff_t end; /* Inclusive */
3338 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003339 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003340
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003341 /*
3342 * We have to hold onto the inode so that ordered extents can do their
3343 * work when the IO finishes. The alternative to this is failing to add
3344 * an ordered extent if the igrab() fails there and that is a huge pain
3345 * to deal with, so instead just hold onto the inode throughout the
3346 * writepages operation. If it fails here we are freeing up the inode
3347 * anyway and we'd rather not waste our time writing out stuff that is
3348 * going to be truncated anyway.
3349 */
3350 if (!igrab(inode))
3351 return 0;
3352
Chris Masond1310b22008-01-24 16:13:08 -05003353 pagevec_init(&pvec, 0);
3354 if (wbc->range_cyclic) {
3355 index = mapping->writeback_index; /* Start from prev offset */
3356 end = -1;
3357 } else {
3358 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3359 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003360 scanned = 1;
3361 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003362 if (wbc->sync_mode == WB_SYNC_ALL)
3363 tag = PAGECACHE_TAG_TOWRITE;
3364 else
3365 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003366retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003367 if (wbc->sync_mode == WB_SYNC_ALL)
3368 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003369 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003370 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3371 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003372 unsigned i;
3373
3374 scanned = 1;
3375 for (i = 0; i < nr_pages; i++) {
3376 struct page *page = pvec.pages[i];
3377
3378 /*
3379 * At this point we hold neither mapping->tree_lock nor
3380 * lock on the page itself: the page may be truncated or
3381 * invalidated (changing page->mapping to NULL), or even
3382 * swizzled back from swapper_space to tmpfs file
3383 * mapping
3384 */
Chris Mason01d658f2011-11-01 10:08:06 -04003385 if (tree->ops &&
3386 tree->ops->write_cache_pages_lock_hook) {
3387 tree->ops->write_cache_pages_lock_hook(page,
3388 data, flush_fn);
3389 } else {
3390 if (!trylock_page(page)) {
3391 flush_fn(data);
3392 lock_page(page);
3393 }
3394 }
Chris Masond1310b22008-01-24 16:13:08 -05003395
3396 if (unlikely(page->mapping != mapping)) {
3397 unlock_page(page);
3398 continue;
3399 }
3400
3401 if (!wbc->range_cyclic && page->index > end) {
3402 done = 1;
3403 unlock_page(page);
3404 continue;
3405 }
3406
Chris Masond2c3f4f2008-11-19 12:44:22 -05003407 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003408 if (PageWriteback(page))
3409 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003410 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003411 }
Chris Masond1310b22008-01-24 16:13:08 -05003412
3413 if (PageWriteback(page) ||
3414 !clear_page_dirty_for_io(page)) {
3415 unlock_page(page);
3416 continue;
3417 }
3418
3419 ret = (*writepage)(page, wbc, data);
3420
3421 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3422 unlock_page(page);
3423 ret = 0;
3424 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003425 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003426 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003427
3428 /*
3429 * the filesystem may choose to bump up nr_to_write.
3430 * We have to make sure to honor the new nr_to_write
3431 * at any time
3432 */
3433 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003434 }
3435 pagevec_release(&pvec);
3436 cond_resched();
3437 }
3438 if (!scanned && !done) {
3439 /*
3440 * We hit the last page and there is more work to be done: wrap
3441 * back to the start of the file
3442 */
3443 scanned = 1;
3444 index = 0;
3445 goto retry;
3446 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003447 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003448 return ret;
3449}
Chris Masond1310b22008-01-24 16:13:08 -05003450
Chris Masonffbd5172009-04-20 15:50:09 -04003451static void flush_epd_write_bio(struct extent_page_data *epd)
3452{
3453 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003454 int rw = WRITE;
3455 int ret;
3456
Chris Masonffbd5172009-04-20 15:50:09 -04003457 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003458 rw = WRITE_SYNC;
3459
3460 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003461 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003462 epd->bio = NULL;
3463 }
3464}
3465
Chris Masond2c3f4f2008-11-19 12:44:22 -05003466static noinline void flush_write_bio(void *data)
3467{
3468 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003469 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003470}
3471
Chris Masond1310b22008-01-24 16:13:08 -05003472int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3473 get_extent_t *get_extent,
3474 struct writeback_control *wbc)
3475{
3476 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003477 struct extent_page_data epd = {
3478 .bio = NULL,
3479 .tree = tree,
3480 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003481 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003482 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003483 };
Chris Masond1310b22008-01-24 16:13:08 -05003484
Chris Masond1310b22008-01-24 16:13:08 -05003485 ret = __extent_writepage(page, wbc, &epd);
3486
Chris Masonffbd5172009-04-20 15:50:09 -04003487 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003488 return ret;
3489}
Chris Masond1310b22008-01-24 16:13:08 -05003490
Chris Mason771ed682008-11-06 22:02:51 -05003491int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3492 u64 start, u64 end, get_extent_t *get_extent,
3493 int mode)
3494{
3495 int ret = 0;
3496 struct address_space *mapping = inode->i_mapping;
3497 struct page *page;
3498 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3499 PAGE_CACHE_SHIFT;
3500
3501 struct extent_page_data epd = {
3502 .bio = NULL,
3503 .tree = tree,
3504 .get_extent = get_extent,
3505 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003506 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003507 };
3508 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003509 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003510 .nr_to_write = nr_pages * 2,
3511 .range_start = start,
3512 .range_end = end + 1,
3513 };
3514
Chris Masond3977122009-01-05 21:25:51 -05003515 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003516 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3517 if (clear_page_dirty_for_io(page))
3518 ret = __extent_writepage(page, &wbc_writepages, &epd);
3519 else {
3520 if (tree->ops && tree->ops->writepage_end_io_hook)
3521 tree->ops->writepage_end_io_hook(page, start,
3522 start + PAGE_CACHE_SIZE - 1,
3523 NULL, 1);
3524 unlock_page(page);
3525 }
3526 page_cache_release(page);
3527 start += PAGE_CACHE_SIZE;
3528 }
3529
Chris Masonffbd5172009-04-20 15:50:09 -04003530 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003531 return ret;
3532}
Chris Masond1310b22008-01-24 16:13:08 -05003533
3534int extent_writepages(struct extent_io_tree *tree,
3535 struct address_space *mapping,
3536 get_extent_t *get_extent,
3537 struct writeback_control *wbc)
3538{
3539 int ret = 0;
3540 struct extent_page_data epd = {
3541 .bio = NULL,
3542 .tree = tree,
3543 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003544 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003545 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003546 };
3547
Chris Mason4bef0842008-09-08 11:18:08 -04003548 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003549 __extent_writepage, &epd,
3550 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003551 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003552 return ret;
3553}
Chris Masond1310b22008-01-24 16:13:08 -05003554
3555int extent_readpages(struct extent_io_tree *tree,
3556 struct address_space *mapping,
3557 struct list_head *pages, unsigned nr_pages,
3558 get_extent_t get_extent)
3559{
3560 struct bio *bio = NULL;
3561 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003562 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003563 struct page *pagepool[16];
3564 struct page *page;
3565 int i = 0;
3566 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003567
Chris Masond1310b22008-01-24 16:13:08 -05003568 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003569 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003570
3571 prefetchw(&page->flags);
3572 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003573 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003574 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003575 page_cache_release(page);
3576 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003577 }
Liu Bo67c96842012-07-20 21:43:09 -06003578
3579 pagepool[nr++] = page;
3580 if (nr < ARRAY_SIZE(pagepool))
3581 continue;
3582 for (i = 0; i < nr; i++) {
3583 __extent_read_full_page(tree, pagepool[i], get_extent,
3584 &bio, 0, &bio_flags);
3585 page_cache_release(pagepool[i]);
3586 }
3587 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003588 }
Liu Bo67c96842012-07-20 21:43:09 -06003589 for (i = 0; i < nr; i++) {
3590 __extent_read_full_page(tree, pagepool[i], get_extent,
3591 &bio, 0, &bio_flags);
3592 page_cache_release(pagepool[i]);
3593 }
3594
Chris Masond1310b22008-01-24 16:13:08 -05003595 BUG_ON(!list_empty(pages));
3596 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003597 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003598 return 0;
3599}
Chris Masond1310b22008-01-24 16:13:08 -05003600
3601/*
3602 * basic invalidatepage code, this waits on any locked or writeback
3603 * ranges corresponding to the page, and then deletes any extent state
3604 * records from the tree
3605 */
3606int extent_invalidatepage(struct extent_io_tree *tree,
3607 struct page *page, unsigned long offset)
3608{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003609 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003610 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3611 u64 end = start + PAGE_CACHE_SIZE - 1;
3612 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3613
Chris Masond3977122009-01-05 21:25:51 -05003614 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003615 if (start > end)
3616 return 0;
3617
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003618 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003619 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003620 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003621 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3622 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003623 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003624 return 0;
3625}
Chris Masond1310b22008-01-24 16:13:08 -05003626
3627/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003628 * a helper for releasepage, this tests for areas of the page that
3629 * are locked or under IO and drops the related state bits if it is safe
3630 * to drop the page.
3631 */
3632int try_release_extent_state(struct extent_map_tree *map,
3633 struct extent_io_tree *tree, struct page *page,
3634 gfp_t mask)
3635{
3636 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3637 u64 end = start + PAGE_CACHE_SIZE - 1;
3638 int ret = 1;
3639
Chris Mason211f90e2008-07-18 11:56:15 -04003640 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003641 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003642 ret = 0;
3643 else {
3644 if ((mask & GFP_NOFS) == GFP_NOFS)
3645 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003646 /*
3647 * at this point we can safely clear everything except the
3648 * locked bit and the nodatasum bit
3649 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003650 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003651 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3652 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003653
3654 /* if clear_extent_bit failed for enomem reasons,
3655 * we can't allow the release to continue.
3656 */
3657 if (ret < 0)
3658 ret = 0;
3659 else
3660 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003661 }
3662 return ret;
3663}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003664
3665/*
Chris Masond1310b22008-01-24 16:13:08 -05003666 * a helper for releasepage. As long as there are no locked extents
3667 * in the range corresponding to the page, both state records and extent
3668 * map records are removed
3669 */
3670int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003671 struct extent_io_tree *tree, struct page *page,
3672 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003673{
3674 struct extent_map *em;
3675 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3676 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003677
Chris Mason70dec802008-01-29 09:59:12 -05003678 if ((mask & __GFP_WAIT) &&
3679 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003680 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003681 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003682 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003683 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003684 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003685 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003686 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003687 break;
3688 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003689 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3690 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003691 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003692 free_extent_map(em);
3693 break;
3694 }
3695 if (!test_range_bit(tree, em->start,
3696 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003697 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003698 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003699 remove_extent_mapping(map, em);
3700 /* once for the rb tree */
3701 free_extent_map(em);
3702 }
3703 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003704 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003705
3706 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003707 free_extent_map(em);
3708 }
Chris Masond1310b22008-01-24 16:13:08 -05003709 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003710 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003711}
Chris Masond1310b22008-01-24 16:13:08 -05003712
Chris Masonec29ed52011-02-23 16:23:20 -05003713/*
3714 * helper function for fiemap, which doesn't want to see any holes.
3715 * This maps until we find something past 'last'
3716 */
3717static struct extent_map *get_extent_skip_holes(struct inode *inode,
3718 u64 offset,
3719 u64 last,
3720 get_extent_t *get_extent)
3721{
3722 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3723 struct extent_map *em;
3724 u64 len;
3725
3726 if (offset >= last)
3727 return NULL;
3728
3729 while(1) {
3730 len = last - offset;
3731 if (len == 0)
3732 break;
3733 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3734 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003735 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003736 return em;
3737
3738 /* if this isn't a hole return it */
3739 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3740 em->block_start != EXTENT_MAP_HOLE) {
3741 return em;
3742 }
3743
3744 /* this is a hole, advance to the next extent */
3745 offset = extent_map_end(em);
3746 free_extent_map(em);
3747 if (offset >= last)
3748 break;
3749 }
3750 return NULL;
3751}
3752
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003753int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3754 __u64 start, __u64 len, get_extent_t *get_extent)
3755{
Josef Bacik975f84f2010-11-23 19:36:57 +00003756 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003757 u64 off = start;
3758 u64 max = start + len;
3759 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003760 u32 found_type;
3761 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003762 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003763 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003764 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003765 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003766 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003767 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003768 struct btrfs_path *path;
3769 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003770 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003771 u64 em_start = 0;
3772 u64 em_len = 0;
3773 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003774 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003775
3776 if (len == 0)
3777 return -EINVAL;
3778
Josef Bacik975f84f2010-11-23 19:36:57 +00003779 path = btrfs_alloc_path();
3780 if (!path)
3781 return -ENOMEM;
3782 path->leave_spinning = 1;
3783
Josef Bacik4d479cf2011-11-17 11:34:31 -05003784 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3785 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3786
Chris Masonec29ed52011-02-23 16:23:20 -05003787 /*
3788 * lookup the last file extent. We're not using i_size here
3789 * because there might be preallocation past i_size
3790 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003791 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003792 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003793 if (ret < 0) {
3794 btrfs_free_path(path);
3795 return ret;
3796 }
3797 WARN_ON(!ret);
3798 path->slots[0]--;
3799 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3800 struct btrfs_file_extent_item);
3801 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3802 found_type = btrfs_key_type(&found_key);
3803
Chris Masonec29ed52011-02-23 16:23:20 -05003804 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003805 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003806 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003807 /* have to trust i_size as the end */
3808 last = (u64)-1;
3809 last_for_get_extent = isize;
3810 } else {
3811 /*
3812 * remember the start of the last extent. There are a
3813 * bunch of different factors that go into the length of the
3814 * extent, so its much less complex to remember where it started
3815 */
3816 last = found_key.offset;
3817 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003818 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003819 btrfs_free_path(path);
3820
Chris Masonec29ed52011-02-23 16:23:20 -05003821 /*
3822 * we might have some extents allocated but more delalloc past those
3823 * extents. so, we trust isize unless the start of the last extent is
3824 * beyond isize
3825 */
3826 if (last < isize) {
3827 last = (u64)-1;
3828 last_for_get_extent = isize;
3829 }
3830
Josef Bacik2ac55d42010-02-03 19:33:23 +00003831 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003832 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003833
Josef Bacik4d479cf2011-11-17 11:34:31 -05003834 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003835 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003836 if (!em)
3837 goto out;
3838 if (IS_ERR(em)) {
3839 ret = PTR_ERR(em);
3840 goto out;
3841 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003842
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003843 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003844 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003845
Chris Masonea8efc72011-03-08 11:54:40 -05003846 /* break if the extent we found is outside the range */
3847 if (em->start >= max || extent_map_end(em) < off)
3848 break;
3849
3850 /*
3851 * get_extent may return an extent that starts before our
3852 * requested range. We have to make sure the ranges
3853 * we return to fiemap always move forward and don't
3854 * overlap, so adjust the offsets here
3855 */
3856 em_start = max(em->start, off);
3857
3858 /*
3859 * record the offset from the start of the extent
3860 * for adjusting the disk offset below
3861 */
3862 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003863 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003864 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003865 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003866 disko = 0;
3867 flags = 0;
3868
Chris Masonea8efc72011-03-08 11:54:40 -05003869 /*
3870 * bump off for our next call to get_extent
3871 */
3872 off = extent_map_end(em);
3873 if (off >= max)
3874 end = 1;
3875
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003876 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003877 end = 1;
3878 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003879 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003880 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3881 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003882 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003883 flags |= (FIEMAP_EXTENT_DELALLOC |
3884 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003885 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003886 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003887 }
3888 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3889 flags |= FIEMAP_EXTENT_ENCODED;
3890
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003891 free_extent_map(em);
3892 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003893 if ((em_start >= last) || em_len == (u64)-1 ||
3894 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003895 flags |= FIEMAP_EXTENT_LAST;
3896 end = 1;
3897 }
3898
Chris Masonec29ed52011-02-23 16:23:20 -05003899 /* now scan forward to see if this is really the last extent. */
3900 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3901 get_extent);
3902 if (IS_ERR(em)) {
3903 ret = PTR_ERR(em);
3904 goto out;
3905 }
3906 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003907 flags |= FIEMAP_EXTENT_LAST;
3908 end = 1;
3909 }
Chris Masonec29ed52011-02-23 16:23:20 -05003910 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3911 em_len, flags);
3912 if (ret)
3913 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003914 }
3915out_free:
3916 free_extent_map(em);
3917out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003918 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3919 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003920 return ret;
3921}
3922
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003923inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003924 unsigned long i)
3925{
Chris Mason727011e2010-08-06 13:21:20 -04003926 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003927}
3928
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003929inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003930{
Chris Mason6af118ce2008-07-22 11:18:07 -04003931 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3932 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003933}
3934
Chris Mason727011e2010-08-06 13:21:20 -04003935static void __free_extent_buffer(struct extent_buffer *eb)
3936{
3937#if LEAK_DEBUG
3938 unsigned long flags;
3939 spin_lock_irqsave(&leak_lock, flags);
3940 list_del(&eb->leak_list);
3941 spin_unlock_irqrestore(&leak_lock, flags);
3942#endif
3943 if (eb->pages && eb->pages != eb->inline_pages)
3944 kfree(eb->pages);
3945 kmem_cache_free(extent_buffer_cache, eb);
3946}
3947
Chris Masond1310b22008-01-24 16:13:08 -05003948static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3949 u64 start,
3950 unsigned long len,
3951 gfp_t mask)
3952{
3953 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003954#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003955 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003956#endif
Chris Masond1310b22008-01-24 16:13:08 -05003957
Chris Masond1310b22008-01-24 16:13:08 -05003958 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003959 if (eb == NULL)
3960 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003961 eb->start = start;
3962 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003963 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02003964 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003965 rwlock_init(&eb->lock);
3966 atomic_set(&eb->write_locks, 0);
3967 atomic_set(&eb->read_locks, 0);
3968 atomic_set(&eb->blocking_readers, 0);
3969 atomic_set(&eb->blocking_writers, 0);
3970 atomic_set(&eb->spinning_readers, 0);
3971 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003972 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003973 init_waitqueue_head(&eb->write_lock_wq);
3974 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003975
Chris Mason39351272009-02-04 09:24:05 -05003976#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003977 spin_lock_irqsave(&leak_lock, flags);
3978 list_add(&eb->leak_list, &buffers);
3979 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003980#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003981 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003982 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003983 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003984
3985 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3986 struct page **pages;
3987 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3988 PAGE_CACHE_SHIFT;
3989 pages = kzalloc(num_pages, mask);
3990 if (!pages) {
3991 __free_extent_buffer(eb);
3992 return NULL;
3993 }
3994 eb->pages = pages;
3995 } else {
3996 eb->pages = eb->inline_pages;
3997 }
Chris Masond1310b22008-01-24 16:13:08 -05003998
3999 return eb;
4000}
4001
Jan Schmidt815a51c2012-05-16 17:00:02 +02004002struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4003{
4004 unsigned long i;
4005 struct page *p;
4006 struct extent_buffer *new;
4007 unsigned long num_pages = num_extent_pages(src->start, src->len);
4008
4009 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4010 if (new == NULL)
4011 return NULL;
4012
4013 for (i = 0; i < num_pages; i++) {
4014 p = alloc_page(GFP_ATOMIC);
4015 BUG_ON(!p);
4016 attach_extent_buffer_page(new, p);
4017 WARN_ON(PageDirty(p));
4018 SetPageUptodate(p);
4019 new->pages[i] = p;
4020 }
4021
4022 copy_extent_buffer(new, src, 0, 0, src->len);
4023 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4024 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4025
4026 return new;
4027}
4028
4029struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4030{
4031 struct extent_buffer *eb;
4032 unsigned long num_pages = num_extent_pages(0, len);
4033 unsigned long i;
4034
4035 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4036 if (!eb)
4037 return NULL;
4038
4039 for (i = 0; i < num_pages; i++) {
4040 eb->pages[i] = alloc_page(GFP_ATOMIC);
4041 if (!eb->pages[i])
4042 goto err;
4043 }
4044 set_extent_buffer_uptodate(eb);
4045 btrfs_set_header_nritems(eb, 0);
4046 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4047
4048 return eb;
4049err:
4050 for (i--; i > 0; i--)
4051 __free_page(eb->pages[i]);
4052 __free_extent_buffer(eb);
4053 return NULL;
4054}
4055
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004056static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004057{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004058 return (atomic_read(&eb->io_pages) ||
4059 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4060 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004061}
4062
Miao Xie897ca6e92010-10-26 20:57:29 -04004063/*
4064 * Helper for releasing extent buffer page.
4065 */
4066static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4067 unsigned long start_idx)
4068{
4069 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004070 unsigned long num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004071 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004072 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004073
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004074 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004075
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004076 num_pages = num_extent_pages(eb->start, eb->len);
4077 index = start_idx + num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004078 if (start_idx >= index)
4079 return;
4080
4081 do {
4082 index--;
4083 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004084 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004085 spin_lock(&page->mapping->private_lock);
4086 /*
4087 * We do this since we'll remove the pages after we've
4088 * removed the eb from the radix tree, so we could race
4089 * and have this page now attached to the new eb. So
4090 * only clear page_private if it's still connected to
4091 * this eb.
4092 */
4093 if (PagePrivate(page) &&
4094 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004095 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004096 BUG_ON(PageDirty(page));
4097 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004098 /*
4099 * We need to make sure we haven't be attached
4100 * to a new eb.
4101 */
4102 ClearPagePrivate(page);
4103 set_page_private(page, 0);
4104 /* One for the page private */
4105 page_cache_release(page);
4106 }
4107 spin_unlock(&page->mapping->private_lock);
4108
Jan Schmidt815a51c2012-05-16 17:00:02 +02004109 }
4110 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004111 /* One for when we alloced the page */
Miao Xie897ca6e92010-10-26 20:57:29 -04004112 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004113 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004114 } while (index != start_idx);
4115}
4116
4117/*
4118 * Helper for releasing the extent buffer.
4119 */
4120static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4121{
4122 btrfs_release_extent_buffer_page(eb, 0);
4123 __free_extent_buffer(eb);
4124}
4125
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004126static void check_buffer_tree_ref(struct extent_buffer *eb)
4127{
4128 /* the ref bit is tricky. We have to make sure it is set
4129 * if we have the buffer dirty. Otherwise the
4130 * code to free a buffer can end up dropping a dirty
4131 * page
4132 *
4133 * Once the ref bit is set, it won't go away while the
4134 * buffer is dirty or in writeback, and it also won't
4135 * go away while we have the reference count on the
4136 * eb bumped.
4137 *
4138 * We can't just set the ref bit without bumping the
4139 * ref on the eb because free_extent_buffer might
4140 * see the ref bit and try to clear it. If this happens
4141 * free_extent_buffer might end up dropping our original
4142 * ref by mistake and freeing the page before we are able
4143 * to add one more ref.
4144 *
4145 * So bump the ref count first, then set the bit. If someone
4146 * beat us to it, drop the ref we added.
4147 */
Josef Bacik594831c2012-07-20 16:11:08 -04004148 spin_lock(&eb->refs_lock);
4149 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004150 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004151 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004152}
4153
Josef Bacik5df42352012-03-15 18:24:42 -04004154static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4155{
4156 unsigned long num_pages, i;
4157
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004158 check_buffer_tree_ref(eb);
4159
Josef Bacik5df42352012-03-15 18:24:42 -04004160 num_pages = num_extent_pages(eb->start, eb->len);
4161 for (i = 0; i < num_pages; i++) {
4162 struct page *p = extent_buffer_page(eb, i);
4163 mark_page_accessed(p);
4164 }
4165}
4166
Chris Masond1310b22008-01-24 16:13:08 -05004167struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004168 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004169{
4170 unsigned long num_pages = num_extent_pages(start, len);
4171 unsigned long i;
4172 unsigned long index = start >> PAGE_CACHE_SHIFT;
4173 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004174 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004175 struct page *p;
4176 struct address_space *mapping = tree->mapping;
4177 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004178 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004179
Miao Xie19fe0a82010-10-26 20:57:29 -04004180 rcu_read_lock();
4181 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4182 if (eb && atomic_inc_not_zero(&eb->refs)) {
4183 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004184 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004185 return eb;
4186 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004187 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004188
David Sterbaba144192011-04-21 01:12:06 +02004189 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004190 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004191 return NULL;
4192
Chris Mason727011e2010-08-06 13:21:20 -04004193 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004194 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004195 if (!p) {
4196 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04004197 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004198 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004199
4200 spin_lock(&mapping->private_lock);
4201 if (PagePrivate(p)) {
4202 /*
4203 * We could have already allocated an eb for this page
4204 * and attached one so lets see if we can get a ref on
4205 * the existing eb, and if we can we know it's good and
4206 * we can just return that one, else we know we can just
4207 * overwrite page->private.
4208 */
4209 exists = (struct extent_buffer *)p->private;
4210 if (atomic_inc_not_zero(&exists->refs)) {
4211 spin_unlock(&mapping->private_lock);
4212 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004213 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004214 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004215 goto free_eb;
4216 }
4217
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004218 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004219 * Do this so attach doesn't complain and we need to
4220 * drop the ref the old guy had.
4221 */
4222 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004223 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004224 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004225 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004226 attach_extent_buffer_page(eb, p);
4227 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004228 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004229 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004230 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004231 if (!PageUptodate(p))
4232 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004233
4234 /*
4235 * see below about how we avoid a nasty race with release page
4236 * and why we unlock later
4237 */
Chris Masond1310b22008-01-24 16:13:08 -05004238 }
4239 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004240 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004241again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004242 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4243 if (ret)
4244 goto free_eb;
4245
Chris Mason6af118ce2008-07-22 11:18:07 -04004246 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004247 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4248 if (ret == -EEXIST) {
4249 exists = radix_tree_lookup(&tree->buffer,
4250 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004251 if (!atomic_inc_not_zero(&exists->refs)) {
4252 spin_unlock(&tree->buffer_lock);
4253 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004254 exists = NULL;
4255 goto again;
4256 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004257 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004258 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004259 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004260 goto free_eb;
4261 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004262 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004263 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004264 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004265 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004266
4267 /*
4268 * there is a race where release page may have
4269 * tried to find this extent buffer in the radix
4270 * but failed. It will tell the VM it is safe to
4271 * reclaim the, and it will clear the page private bit.
4272 * We must make sure to set the page private bit properly
4273 * after the extent buffer is in the radix tree so
4274 * it doesn't get lost
4275 */
Chris Mason727011e2010-08-06 13:21:20 -04004276 SetPageChecked(eb->pages[0]);
4277 for (i = 1; i < num_pages; i++) {
4278 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004279 ClearPageChecked(p);
4280 unlock_page(p);
4281 }
4282 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004283 return eb;
4284
Chris Mason6af118ce2008-07-22 11:18:07 -04004285free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004286 for (i = 0; i < num_pages; i++) {
4287 if (eb->pages[i])
4288 unlock_page(eb->pages[i]);
4289 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004290
Josef Bacik17de39a2012-05-04 15:16:06 -04004291 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e92010-10-26 20:57:29 -04004292 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004293 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004294}
Chris Masond1310b22008-01-24 16:13:08 -05004295
4296struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004297 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004298{
Chris Masond1310b22008-01-24 16:13:08 -05004299 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004300
Miao Xie19fe0a82010-10-26 20:57:29 -04004301 rcu_read_lock();
4302 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4303 if (eb && atomic_inc_not_zero(&eb->refs)) {
4304 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004305 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004306 return eb;
4307 }
4308 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004309
Miao Xie19fe0a82010-10-26 20:57:29 -04004310 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004311}
Chris Masond1310b22008-01-24 16:13:08 -05004312
Josef Bacik3083ee22012-03-09 16:01:49 -05004313static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4314{
4315 struct extent_buffer *eb =
4316 container_of(head, struct extent_buffer, rcu_head);
4317
4318 __free_extent_buffer(eb);
4319}
4320
Josef Bacik3083ee22012-03-09 16:01:49 -05004321/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004322static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004323{
4324 WARN_ON(atomic_read(&eb->refs) == 0);
4325 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004326 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4327 spin_unlock(&eb->refs_lock);
4328 } else {
4329 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004330
Jan Schmidt815a51c2012-05-16 17:00:02 +02004331 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004332
Jan Schmidt815a51c2012-05-16 17:00:02 +02004333 spin_lock(&tree->buffer_lock);
4334 radix_tree_delete(&tree->buffer,
4335 eb->start >> PAGE_CACHE_SHIFT);
4336 spin_unlock(&tree->buffer_lock);
4337 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004338
4339 /* Should be safe to release our pages at this point */
4340 btrfs_release_extent_buffer_page(eb, 0);
4341
4342 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004343 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004344 }
4345 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004346
4347 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004348}
4349
Chris Masond1310b22008-01-24 16:13:08 -05004350void free_extent_buffer(struct extent_buffer *eb)
4351{
Chris Masond1310b22008-01-24 16:13:08 -05004352 if (!eb)
4353 return;
4354
Josef Bacik3083ee22012-03-09 16:01:49 -05004355 spin_lock(&eb->refs_lock);
4356 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004357 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4358 atomic_dec(&eb->refs);
4359
4360 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004361 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004362 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004363 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4364 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004365
Josef Bacik3083ee22012-03-09 16:01:49 -05004366 /*
4367 * I know this is terrible, but it's temporary until we stop tracking
4368 * the uptodate bits and such for the extent buffers.
4369 */
4370 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004371}
Chris Masond1310b22008-01-24 16:13:08 -05004372
Josef Bacik3083ee22012-03-09 16:01:49 -05004373void free_extent_buffer_stale(struct extent_buffer *eb)
4374{
4375 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004376 return;
4377
Josef Bacik3083ee22012-03-09 16:01:49 -05004378 spin_lock(&eb->refs_lock);
4379 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4380
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004381 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004382 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4383 atomic_dec(&eb->refs);
4384 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004385}
4386
Chris Mason1d4284b2012-03-28 20:31:37 -04004387void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004388{
Chris Masond1310b22008-01-24 16:13:08 -05004389 unsigned long i;
4390 unsigned long num_pages;
4391 struct page *page;
4392
Chris Masond1310b22008-01-24 16:13:08 -05004393 num_pages = num_extent_pages(eb->start, eb->len);
4394
4395 for (i = 0; i < num_pages; i++) {
4396 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004397 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004398 continue;
4399
Chris Masona61e6f22008-07-22 11:18:08 -04004400 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004401 WARN_ON(!PagePrivate(page));
4402
Chris Masond1310b22008-01-24 16:13:08 -05004403 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004404 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004405 if (!PageDirty(page)) {
4406 radix_tree_tag_clear(&page->mapping->page_tree,
4407 page_index(page),
4408 PAGECACHE_TAG_DIRTY);
4409 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004410 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004411 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004412 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004413 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004414 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004415}
Chris Masond1310b22008-01-24 16:13:08 -05004416
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004417int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004418{
4419 unsigned long i;
4420 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004421 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004422
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004423 check_buffer_tree_ref(eb);
4424
Chris Masonb9473432009-03-13 11:00:37 -04004425 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004426
Chris Masond1310b22008-01-24 16:13:08 -05004427 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004428 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004429 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4430
Chris Masonb9473432009-03-13 11:00:37 -04004431 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004432 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004433 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004434}
Chris Masond1310b22008-01-24 16:13:08 -05004435
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004436static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004437{
4438 if (len < PAGE_CACHE_SIZE)
4439 return 1;
4440 if (start & (PAGE_CACHE_SIZE - 1))
4441 return 1;
4442 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4443 return 1;
4444 return 0;
4445}
4446
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004447int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004448{
4449 unsigned long i;
4450 struct page *page;
4451 unsigned long num_pages;
4452
Chris Masonb4ce94d2009-02-04 09:25:08 -05004453 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004454 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004455 for (i = 0; i < num_pages; i++) {
4456 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004457 if (page)
4458 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004459 }
4460 return 0;
4461}
4462
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004463int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004464{
4465 unsigned long i;
4466 struct page *page;
4467 unsigned long num_pages;
4468
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004469 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004470 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004471 for (i = 0; i < num_pages; i++) {
4472 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004473 SetPageUptodate(page);
4474 }
4475 return 0;
4476}
Chris Masond1310b22008-01-24 16:13:08 -05004477
Chris Masonce9adaa2008-04-09 16:28:12 -04004478int extent_range_uptodate(struct extent_io_tree *tree,
4479 u64 start, u64 end)
4480{
4481 struct page *page;
4482 int ret;
4483 int pg_uptodate = 1;
4484 int uptodate;
4485 unsigned long index;
4486
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004487 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004488 ret = test_range_bit(tree, start, end,
4489 EXTENT_UPTODATE, 1, NULL);
4490 if (ret)
4491 return 1;
4492 }
Chris Masond3977122009-01-05 21:25:51 -05004493 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004494 index = start >> PAGE_CACHE_SHIFT;
4495 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004496 if (!page)
4497 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004498 uptodate = PageUptodate(page);
4499 page_cache_release(page);
4500 if (!uptodate) {
4501 pg_uptodate = 0;
4502 break;
4503 }
4504 start += PAGE_CACHE_SIZE;
4505 }
4506 return pg_uptodate;
4507}
4508
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004509int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004510{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004511 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004512}
Chris Masond1310b22008-01-24 16:13:08 -05004513
4514int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004515 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004516 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004517{
4518 unsigned long i;
4519 unsigned long start_i;
4520 struct page *page;
4521 int err;
4522 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004523 int locked_pages = 0;
4524 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004525 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004526 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004527 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004528 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004529
Chris Masonb4ce94d2009-02-04 09:25:08 -05004530 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004531 return 0;
4532
Chris Masond1310b22008-01-24 16:13:08 -05004533 if (start) {
4534 WARN_ON(start < eb->start);
4535 start_i = (start >> PAGE_CACHE_SHIFT) -
4536 (eb->start >> PAGE_CACHE_SHIFT);
4537 } else {
4538 start_i = 0;
4539 }
4540
4541 num_pages = num_extent_pages(eb->start, eb->len);
4542 for (i = start_i; i < num_pages; i++) {
4543 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004544 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004545 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004546 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004547 } else {
4548 lock_page(page);
4549 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004550 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004551 if (!PageUptodate(page)) {
4552 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004553 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004554 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004555 }
4556 if (all_uptodate) {
4557 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004558 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004559 goto unlock_exit;
4560 }
4561
Josef Bacikea466792012-03-26 21:57:36 -04004562 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004563 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004564 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004565 for (i = start_i; i < num_pages; i++) {
4566 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004567 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004568 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004569 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004570 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004571 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004572 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004573 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004574 } else {
4575 unlock_page(page);
4576 }
4577 }
4578
Jeff Mahoney355808c2011-10-03 23:23:14 -04004579 if (bio) {
4580 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004581 if (err)
4582 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004583 }
Chris Masona86c12c2008-02-07 10:50:54 -05004584
Arne Jansenbb82ab82011-06-10 14:06:53 +02004585 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004586 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004587
Chris Masond1310b22008-01-24 16:13:08 -05004588 for (i = start_i; i < num_pages; i++) {
4589 page = extent_buffer_page(eb, i);
4590 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004591 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004592 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004593 }
Chris Masond3977122009-01-05 21:25:51 -05004594
Chris Masond1310b22008-01-24 16:13:08 -05004595 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004596
4597unlock_exit:
4598 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004599 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004600 page = extent_buffer_page(eb, i);
4601 i++;
4602 unlock_page(page);
4603 locked_pages--;
4604 }
4605 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004606}
Chris Masond1310b22008-01-24 16:13:08 -05004607
4608void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4609 unsigned long start,
4610 unsigned long len)
4611{
4612 size_t cur;
4613 size_t offset;
4614 struct page *page;
4615 char *kaddr;
4616 char *dst = (char *)dstv;
4617 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4618 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004619
4620 WARN_ON(start > eb->len);
4621 WARN_ON(start + len > eb->start + eb->len);
4622
4623 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4624
Chris Masond3977122009-01-05 21:25:51 -05004625 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004626 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004627
4628 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004629 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004630 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004631
4632 dst += cur;
4633 len -= cur;
4634 offset = 0;
4635 i++;
4636 }
4637}
Chris Masond1310b22008-01-24 16:13:08 -05004638
4639int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004640 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004641 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004642 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004643{
4644 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4645 char *kaddr;
4646 struct page *p;
4647 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4648 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4649 unsigned long end_i = (start_offset + start + min_len - 1) >>
4650 PAGE_CACHE_SHIFT;
4651
4652 if (i != end_i)
4653 return -EINVAL;
4654
4655 if (i == 0) {
4656 offset = start_offset;
4657 *map_start = 0;
4658 } else {
4659 offset = 0;
4660 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4661 }
Chris Masond3977122009-01-05 21:25:51 -05004662
Chris Masond1310b22008-01-24 16:13:08 -05004663 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004664 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4665 "wanted %lu %lu\n", (unsigned long long)eb->start,
4666 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004667 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004668 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004669 }
4670
4671 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004672 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004673 *map = kaddr + offset;
4674 *map_len = PAGE_CACHE_SIZE - offset;
4675 return 0;
4676}
Chris Masond1310b22008-01-24 16:13:08 -05004677
Chris Masond1310b22008-01-24 16:13:08 -05004678int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4679 unsigned long start,
4680 unsigned long len)
4681{
4682 size_t cur;
4683 size_t offset;
4684 struct page *page;
4685 char *kaddr;
4686 char *ptr = (char *)ptrv;
4687 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4688 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4689 int ret = 0;
4690
4691 WARN_ON(start > eb->len);
4692 WARN_ON(start + len > eb->start + eb->len);
4693
4694 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4695
Chris Masond3977122009-01-05 21:25:51 -05004696 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004697 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004698
4699 cur = min(len, (PAGE_CACHE_SIZE - offset));
4700
Chris Masona6591712011-07-19 12:04:14 -04004701 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004702 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004703 if (ret)
4704 break;
4705
4706 ptr += cur;
4707 len -= cur;
4708 offset = 0;
4709 i++;
4710 }
4711 return ret;
4712}
Chris Masond1310b22008-01-24 16:13:08 -05004713
4714void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4715 unsigned long start, unsigned long len)
4716{
4717 size_t cur;
4718 size_t offset;
4719 struct page *page;
4720 char *kaddr;
4721 char *src = (char *)srcv;
4722 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4723 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4724
4725 WARN_ON(start > eb->len);
4726 WARN_ON(start + len > eb->start + eb->len);
4727
4728 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4729
Chris Masond3977122009-01-05 21:25:51 -05004730 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004731 page = extent_buffer_page(eb, i);
4732 WARN_ON(!PageUptodate(page));
4733
4734 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004735 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004736 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004737
4738 src += cur;
4739 len -= cur;
4740 offset = 0;
4741 i++;
4742 }
4743}
Chris Masond1310b22008-01-24 16:13:08 -05004744
4745void memset_extent_buffer(struct extent_buffer *eb, char c,
4746 unsigned long start, unsigned long len)
4747{
4748 size_t cur;
4749 size_t offset;
4750 struct page *page;
4751 char *kaddr;
4752 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4753 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4754
4755 WARN_ON(start > eb->len);
4756 WARN_ON(start + len > eb->start + eb->len);
4757
4758 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4759
Chris Masond3977122009-01-05 21:25:51 -05004760 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004761 page = extent_buffer_page(eb, i);
4762 WARN_ON(!PageUptodate(page));
4763
4764 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004765 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004766 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004767
4768 len -= cur;
4769 offset = 0;
4770 i++;
4771 }
4772}
Chris Masond1310b22008-01-24 16:13:08 -05004773
4774void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4775 unsigned long dst_offset, unsigned long src_offset,
4776 unsigned long len)
4777{
4778 u64 dst_len = dst->len;
4779 size_t cur;
4780 size_t offset;
4781 struct page *page;
4782 char *kaddr;
4783 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4784 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4785
4786 WARN_ON(src->len != dst_len);
4787
4788 offset = (start_offset + dst_offset) &
4789 ((unsigned long)PAGE_CACHE_SIZE - 1);
4790
Chris Masond3977122009-01-05 21:25:51 -05004791 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004792 page = extent_buffer_page(dst, i);
4793 WARN_ON(!PageUptodate(page));
4794
4795 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4796
Chris Masona6591712011-07-19 12:04:14 -04004797 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004798 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004799
4800 src_offset += cur;
4801 len -= cur;
4802 offset = 0;
4803 i++;
4804 }
4805}
Chris Masond1310b22008-01-24 16:13:08 -05004806
4807static void move_pages(struct page *dst_page, struct page *src_page,
4808 unsigned long dst_off, unsigned long src_off,
4809 unsigned long len)
4810{
Chris Masona6591712011-07-19 12:04:14 -04004811 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004812 if (dst_page == src_page) {
4813 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4814 } else {
Chris Masona6591712011-07-19 12:04:14 -04004815 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004816 char *p = dst_kaddr + dst_off + len;
4817 char *s = src_kaddr + src_off + len;
4818
4819 while (len--)
4820 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004821 }
Chris Masond1310b22008-01-24 16:13:08 -05004822}
4823
Sergei Trofimovich33872062011-04-11 21:52:52 +00004824static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4825{
4826 unsigned long distance = (src > dst) ? src - dst : dst - src;
4827 return distance < len;
4828}
4829
Chris Masond1310b22008-01-24 16:13:08 -05004830static void copy_pages(struct page *dst_page, struct page *src_page,
4831 unsigned long dst_off, unsigned long src_off,
4832 unsigned long len)
4833{
Chris Masona6591712011-07-19 12:04:14 -04004834 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004835 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004836 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004837
Sergei Trofimovich33872062011-04-11 21:52:52 +00004838 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004839 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004840 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004841 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004842 if (areas_overlap(src_off, dst_off, len))
4843 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004844 }
Chris Masond1310b22008-01-24 16:13:08 -05004845
Chris Mason727011e2010-08-06 13:21:20 -04004846 if (must_memmove)
4847 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4848 else
4849 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004850}
4851
4852void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4853 unsigned long src_offset, unsigned long len)
4854{
4855 size_t cur;
4856 size_t dst_off_in_page;
4857 size_t src_off_in_page;
4858 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4859 unsigned long dst_i;
4860 unsigned long src_i;
4861
4862 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004863 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4864 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004865 BUG_ON(1);
4866 }
4867 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004868 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4869 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004870 BUG_ON(1);
4871 }
4872
Chris Masond3977122009-01-05 21:25:51 -05004873 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004874 dst_off_in_page = (start_offset + dst_offset) &
4875 ((unsigned long)PAGE_CACHE_SIZE - 1);
4876 src_off_in_page = (start_offset + src_offset) &
4877 ((unsigned long)PAGE_CACHE_SIZE - 1);
4878
4879 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4880 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4881
4882 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4883 src_off_in_page));
4884 cur = min_t(unsigned long, cur,
4885 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4886
4887 copy_pages(extent_buffer_page(dst, dst_i),
4888 extent_buffer_page(dst, src_i),
4889 dst_off_in_page, src_off_in_page, cur);
4890
4891 src_offset += cur;
4892 dst_offset += cur;
4893 len -= cur;
4894 }
4895}
Chris Masond1310b22008-01-24 16:13:08 -05004896
4897void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4898 unsigned long src_offset, unsigned long len)
4899{
4900 size_t cur;
4901 size_t dst_off_in_page;
4902 size_t src_off_in_page;
4903 unsigned long dst_end = dst_offset + len - 1;
4904 unsigned long src_end = src_offset + len - 1;
4905 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4906 unsigned long dst_i;
4907 unsigned long src_i;
4908
4909 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004910 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4911 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004912 BUG_ON(1);
4913 }
4914 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004915 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4916 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004917 BUG_ON(1);
4918 }
Chris Mason727011e2010-08-06 13:21:20 -04004919 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004920 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4921 return;
4922 }
Chris Masond3977122009-01-05 21:25:51 -05004923 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004924 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4925 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4926
4927 dst_off_in_page = (start_offset + dst_end) &
4928 ((unsigned long)PAGE_CACHE_SIZE - 1);
4929 src_off_in_page = (start_offset + src_end) &
4930 ((unsigned long)PAGE_CACHE_SIZE - 1);
4931
4932 cur = min_t(unsigned long, len, src_off_in_page + 1);
4933 cur = min(cur, dst_off_in_page + 1);
4934 move_pages(extent_buffer_page(dst, dst_i),
4935 extent_buffer_page(dst, src_i),
4936 dst_off_in_page - cur + 1,
4937 src_off_in_page - cur + 1, cur);
4938
4939 dst_end -= cur;
4940 src_end -= cur;
4941 len -= cur;
4942 }
4943}
Chris Mason6af118ce2008-07-22 11:18:07 -04004944
Josef Bacik3083ee22012-03-09 16:01:49 -05004945int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004946{
Chris Mason6af118ce2008-07-22 11:18:07 -04004947 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04004948
Miao Xie19fe0a82010-10-26 20:57:29 -04004949 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004950 * We need to make sure noboody is attaching this page to an eb right
4951 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004952 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004953 spin_lock(&page->mapping->private_lock);
4954 if (!PagePrivate(page)) {
4955 spin_unlock(&page->mapping->private_lock);
4956 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004957 }
4958
Josef Bacik3083ee22012-03-09 16:01:49 -05004959 eb = (struct extent_buffer *)page->private;
4960 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004961
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004962 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004963 * This is a little awful but should be ok, we need to make sure that
4964 * the eb doesn't disappear out from under us while we're looking at
4965 * this page.
4966 */
4967 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004968 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004969 spin_unlock(&eb->refs_lock);
4970 spin_unlock(&page->mapping->private_lock);
4971 return 0;
4972 }
4973 spin_unlock(&page->mapping->private_lock);
4974
4975 if ((mask & GFP_NOFS) == GFP_NOFS)
4976 mask = GFP_NOFS;
4977
4978 /*
4979 * If tree ref isn't set then we know the ref on this eb is a real ref,
4980 * so just return, this page will likely be freed soon anyway.
4981 */
4982 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4983 spin_unlock(&eb->refs_lock);
4984 return 0;
4985 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004986
Josef Bacike64860a2012-07-20 16:05:36 -04004987 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04004988}