blob: de1793ba004aa472afc8c983521cea7380ef4720 [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>
5#include <linux/gfp.h>
6#include <linux/pagemap.h>
7#include <linux/page-flags.h>
8#include <linux/module.h>
9#include <linux/spinlock.h>
10#include <linux/blkdev.h>
11#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050012#include <linux/writeback.h>
13#include <linux/pagevec.h>
14#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040016#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050019
Chris Masond1310b22008-01-24 16:13:08 -050020static struct kmem_cache *extent_state_cache;
21static struct kmem_cache *extent_buffer_cache;
22
23static LIST_HEAD(buffers);
24static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040025
Chris Masonb47eda82008-11-10 12:34:40 -050026#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050027#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050028static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040029#endif
Chris Masond1310b22008-01-24 16:13:08 -050030
Chris Masond1310b22008-01-24 16:13:08 -050031#define BUFFER_LRU_MAX 64
32
33struct tree_entry {
34 u64 start;
35 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050036 struct rb_node rb_node;
37};
38
39struct extent_page_data {
40 struct bio *bio;
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050043
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
46 */
Chris Masonffbd5172009-04-20 15:50:09 -040047 unsigned int extent_locked:1;
48
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050051};
52
53int __init extent_io_init(void)
54{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020055 extent_state_cache = kmem_cache_create("extent_state",
56 sizeof(struct extent_state), 0,
57 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050058 if (!extent_state_cache)
59 return -ENOMEM;
60
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020061 extent_buffer_cache = kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050064 if (!extent_buffer_cache)
65 goto free_state_cache;
66 return 0;
67
68free_state_cache:
69 kmem_cache_destroy(extent_state_cache);
70 return -ENOMEM;
71}
72
73void extent_io_exit(void)
74{
75 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040076 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050077
78 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040079 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050080 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state->start,
83 (unsigned long long)state->end,
84 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040085 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050086 kmem_cache_free(extent_state_cache, state);
87
88 }
89
Chris Mason2d2ae542008-03-26 16:24:23 -040090 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050092 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb->start,
94 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040095 list_del(&eb->leak_list);
96 kmem_cache_free(extent_buffer_cache, eb);
97 }
Chris Masond1310b22008-01-24 16:13:08 -050098 if (extent_state_cache)
99 kmem_cache_destroy(extent_state_cache);
100 if (extent_buffer_cache)
101 kmem_cache_destroy(extent_buffer_cache);
102}
103
104void extent_io_tree_init(struct extent_io_tree *tree,
105 struct address_space *mapping, gfp_t mask)
106{
107 tree->state.rb_node = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400108 tree->buffer.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500109 tree->ops = NULL;
110 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500111 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400112 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500113 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500114}
Chris Masond1310b22008-01-24 16:13:08 -0500115
Christoph Hellwigb2950862008-12-02 09:54:17 -0500116static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500117{
118 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500119#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400120 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400121#endif
Chris Masond1310b22008-01-24 16:13:08 -0500122
123 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400124 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500125 return state;
126 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500127 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500128 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500129#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400133#endif
Chris Masond1310b22008-01-24 16:13:08 -0500134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
136 return state;
137}
Chris Masond1310b22008-01-24 16:13:08 -0500138
Christoph Hellwigb2950862008-12-02 09:54:17 -0500139static void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500140{
Chris Masond1310b22008-01-24 16:13:08 -0500141 if (!state)
142 return;
143 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500144#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400145 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400146#endif
Chris Mason70dec802008-01-29 09:59:12 -0500147 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500148#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Masond1310b22008-01-24 16:13:08 -0500153 kmem_cache_free(extent_state_cache, state);
154 }
155}
Chris Masond1310b22008-01-24 16:13:08 -0500156
157static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
159{
Chris Masond3977122009-01-05 21:25:51 -0500160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500162 struct tree_entry *entry;
163
Chris Masond3977122009-01-05 21:25:51 -0500164 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500165 parent = *p;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
167
168 if (offset < entry->start)
169 p = &(*p)->rb_left;
170 else if (offset > entry->end)
171 p = &(*p)->rb_right;
172 else
173 return parent;
174 }
175
176 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
179 return NULL;
180}
181
Chris Mason80ea96b2008-02-01 14:51:59 -0500182static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
185{
Chris Mason80ea96b2008-02-01 14:51:59 -0500186 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500187 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
192
Chris Masond3977122009-01-05 21:25:51 -0500193 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500194 entry = rb_entry(n, struct tree_entry, rb_node);
195 prev = n;
196 prev_entry = entry;
197
198 if (offset < entry->start)
199 n = n->rb_left;
200 else if (offset > entry->end)
201 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500202 else
Chris Masond1310b22008-01-24 16:13:08 -0500203 return n;
204 }
205
206 if (prev_ret) {
207 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500208 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
211 }
212 *prev_ret = prev;
213 prev = orig_prev;
214 }
215
216 if (next_ret) {
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500218 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
221 }
222 *next_ret = prev;
223 }
224 return NULL;
225}
226
Chris Mason80ea96b2008-02-01 14:51:59 -0500227static inline struct rb_node *tree_search(struct extent_io_tree *tree,
228 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500229{
Chris Mason70dec802008-01-29 09:59:12 -0500230 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500231 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500232
Chris Mason80ea96b2008-02-01 14:51:59 -0500233 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500234 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500235 return prev;
236 return ret;
237}
238
Chris Mason6af118ce2008-07-22 11:18:07 -0400239static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
240 u64 offset, struct rb_node *node)
241{
242 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500243 struct rb_node **p = &root->rb_node;
244 struct rb_node *parent = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400245 struct extent_buffer *eb;
246
Chris Masond3977122009-01-05 21:25:51 -0500247 while (*p) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400248 parent = *p;
249 eb = rb_entry(parent, struct extent_buffer, rb_node);
250
251 if (offset < eb->start)
252 p = &(*p)->rb_left;
253 else if (offset > eb->start)
254 p = &(*p)->rb_right;
255 else
256 return eb;
257 }
258
259 rb_link_node(node, parent, p);
260 rb_insert_color(node, root);
261 return NULL;
262}
263
264static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
265 u64 offset)
266{
267 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500268 struct rb_node *n = root->rb_node;
Chris Mason6af118ce2008-07-22 11:18:07 -0400269 struct extent_buffer *eb;
270
Chris Masond3977122009-01-05 21:25:51 -0500271 while (n) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400272 eb = rb_entry(n, struct extent_buffer, rb_node);
273 if (offset < eb->start)
274 n = n->rb_left;
275 else if (offset > eb->start)
276 n = n->rb_right;
277 else
278 return eb;
279 }
280 return NULL;
281}
282
Josef Bacik9ed74f22009-09-11 16:12:44 -0400283static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
284 struct extent_state *other)
285{
286 if (tree->ops && tree->ops->merge_extent_hook)
287 tree->ops->merge_extent_hook(tree->mapping->host, new,
288 other);
289}
290
Chris Masond1310b22008-01-24 16:13:08 -0500291/*
292 * utility function to look for merge candidates inside a given range.
293 * Any extents with matching state are merged together into a single
294 * extent in the tree. Extents with EXTENT_IO in their state field
295 * are not merged because the end_io handlers need to be able to do
296 * operations on them without sleeping (or doing allocations/splits).
297 *
298 * This should be called with the tree lock held.
299 */
300static int merge_state(struct extent_io_tree *tree,
301 struct extent_state *state)
302{
303 struct extent_state *other;
304 struct rb_node *other_node;
305
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400306 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500307 return 0;
308
309 other_node = rb_prev(&state->rb_node);
310 if (other_node) {
311 other = rb_entry(other_node, struct extent_state, rb_node);
312 if (other->end == state->start - 1 &&
313 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400314 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500315 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500316 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500317 rb_erase(&other->rb_node, &tree->state);
318 free_extent_state(other);
319 }
320 }
321 other_node = rb_next(&state->rb_node);
322 if (other_node) {
323 other = rb_entry(other_node, struct extent_state, rb_node);
324 if (other->start == state->end + 1 &&
325 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400326 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500327 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500328 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500329 rb_erase(&state->rb_node, &tree->state);
330 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400331 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500332 }
333 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400334
Chris Masond1310b22008-01-24 16:13:08 -0500335 return 0;
336}
337
Josef Bacik9ed74f22009-09-11 16:12:44 -0400338static int set_state_cb(struct extent_io_tree *tree,
Chris Mason291d6732008-01-29 15:55:23 -0500339 struct extent_state *state,
340 unsigned long bits)
341{
342 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400343 return tree->ops->set_bit_hook(tree->mapping->host,
344 state->start, state->end,
345 state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500346 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400347
348 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500349}
350
351static void clear_state_cb(struct extent_io_tree *tree,
352 struct extent_state *state,
353 unsigned long bits)
354{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400355 if (tree->ops && tree->ops->clear_bit_hook)
356 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500357}
358
Chris Masond1310b22008-01-24 16:13:08 -0500359/*
360 * insert an extent_state struct into the tree. 'bits' are set on the
361 * struct before it is inserted.
362 *
363 * This may return -EEXIST if the extent is already there, in which case the
364 * state struct is freed.
365 *
366 * The tree lock is not taken internally. This is a utility function and
367 * probably isn't what you want to call (see set/clear_extent_bit).
368 */
369static int insert_state(struct extent_io_tree *tree,
370 struct extent_state *state, u64 start, u64 end,
371 int bits)
372{
373 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400374 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500375
376 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500377 printk(KERN_ERR "btrfs end < start %llu %llu\n",
378 (unsigned long long)end,
379 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500380 WARN_ON(1);
381 }
Chris Masond1310b22008-01-24 16:13:08 -0500382 state->start = start;
383 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400384 ret = set_state_cb(tree, state, bits);
385 if (ret)
386 return ret;
387
388 if (bits & EXTENT_DIRTY)
389 tree->dirty_bytes += end - start + 1;
Chris Masone48c4652009-09-11 11:25:02 -0400390 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500391 node = tree_insert(&tree->state, end, &state->rb_node);
392 if (node) {
393 struct extent_state *found;
394 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500395 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
396 "%llu %llu\n", (unsigned long long)found->start,
397 (unsigned long long)found->end,
398 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500399 free_extent_state(state);
400 return -EEXIST;
401 }
Chris Mason70dec802008-01-29 09:59:12 -0500402 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500403 merge_state(tree, state);
404 return 0;
405}
406
Josef Bacik9ed74f22009-09-11 16:12:44 -0400407static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
408 u64 split)
409{
410 if (tree->ops && tree->ops->split_extent_hook)
411 return tree->ops->split_extent_hook(tree->mapping->host,
412 orig, split);
413 return 0;
414}
415
Chris Masond1310b22008-01-24 16:13:08 -0500416/*
417 * split a given extent state struct in two, inserting the preallocated
418 * struct 'prealloc' as the newly created second half. 'split' indicates an
419 * offset inside 'orig' where it should be split.
420 *
421 * Before calling,
422 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
423 * are two extent state structs in the tree:
424 * prealloc: [orig->start, split - 1]
425 * orig: [ split, orig->end ]
426 *
427 * The tree locks are not taken by this function. They need to be held
428 * by the caller.
429 */
430static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
431 struct extent_state *prealloc, u64 split)
432{
433 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400434
435 split_cb(tree, orig, split);
436
Chris Masond1310b22008-01-24 16:13:08 -0500437 prealloc->start = orig->start;
438 prealloc->end = split - 1;
439 prealloc->state = orig->state;
440 orig->start = split;
441
442 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
443 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500444 free_extent_state(prealloc);
445 return -EEXIST;
446 }
Chris Mason70dec802008-01-29 09:59:12 -0500447 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500448 return 0;
449}
450
451/*
452 * utility function to clear some bits in an extent state struct.
453 * it will optionally wake up any one waiting on this state (wake == 1), or
454 * forcibly remove the state from the tree (delete == 1).
455 *
456 * If no bits are set on the state struct after clearing things, the
457 * struct is freed and removed from the tree
458 */
459static int clear_state_bit(struct extent_io_tree *tree,
460 struct extent_state *state, int bits, int wake,
461 int delete)
462{
463 int ret = state->state & bits;
464
465 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
466 u64 range = state->end - state->start + 1;
467 WARN_ON(range > tree->dirty_bytes);
468 tree->dirty_bytes -= range;
469 }
Chris Mason291d6732008-01-29 15:55:23 -0500470 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500471 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500472 if (wake)
473 wake_up(&state->wq);
474 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500475 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500476 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500477 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500478 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500479 free_extent_state(state);
480 } else {
481 WARN_ON(1);
482 }
483 } else {
484 merge_state(tree, state);
485 }
486 return ret;
487}
488
489/*
490 * clear some bits on a range in the tree. This may require splitting
491 * or inserting elements in the tree, so the gfp mask is used to
492 * indicate which allocations or sleeping are allowed.
493 *
494 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
495 * the given range from the tree regardless of state (ie for truncate).
496 *
497 * the range [start, end] is inclusive.
498 *
499 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
500 * bits were already set, or zero if none of the bits were already set.
501 */
502int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400503 int bits, int wake, int delete,
504 struct extent_state **cached_state,
505 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500506{
507 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400508 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500509 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400510 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500511 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400512 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500513 int err;
514 int set = 0;
515
516again:
517 if (!prealloc && (mask & __GFP_WAIT)) {
518 prealloc = alloc_extent_state(mask);
519 if (!prealloc)
520 return -ENOMEM;
521 }
522
Chris Masoncad321a2008-12-17 14:51:42 -0500523 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400524 if (cached_state) {
525 cached = *cached_state;
526 *cached_state = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400527 cached_state = NULL;
528 if (cached && cached->tree && cached->start == start) {
Chris Mason2c64c532009-09-02 15:04:12 -0400529 atomic_dec(&cached->refs);
530 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400531 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400532 }
533 free_extent_state(cached);
534 }
Chris Masond1310b22008-01-24 16:13:08 -0500535 /*
536 * this search will find the extents that end after
537 * our range starts
538 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500539 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500540 if (!node)
541 goto out;
542 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400543hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500544 if (state->start > end)
545 goto out;
546 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400547 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500548
549 /*
550 * | ---- desired range ---- |
551 * | state | or
552 * | ------------- state -------------- |
553 *
554 * We need to split the extent we found, and may flip
555 * bits on second half.
556 *
557 * If the extent we found extends past our range, we
558 * just split and search again. It'll get split again
559 * the next time though.
560 *
561 * If the extent we found is inside our range, we clear
562 * the desired bit on it.
563 */
564
565 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500566 if (!prealloc)
567 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500568 err = split_state(tree, state, prealloc, start);
569 BUG_ON(err == -EEXIST);
570 prealloc = NULL;
571 if (err)
572 goto out;
573 if (state->end <= end) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400574 set |= clear_state_bit(tree, state, bits, wake,
575 delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400576 if (last_end == (u64)-1)
577 goto out;
578 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500579 }
580 goto search_again;
581 }
582 /*
583 * | ---- desired range ---- |
584 * | state |
585 * We need to split the extent, and clear the bit
586 * on the first half
587 */
588 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500589 if (!prealloc)
590 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500591 err = split_state(tree, state, prealloc, end + 1);
592 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500593 if (wake)
594 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400595
Josef Bacik9ed74f22009-09-11 16:12:44 -0400596 set |= clear_state_bit(tree, prealloc, bits, wake, delete);
597
Chris Masond1310b22008-01-24 16:13:08 -0500598 prealloc = NULL;
599 goto out;
600 }
Chris Mason42daec22009-09-23 19:51:09 -0400601
Chris Mason2c64c532009-09-02 15:04:12 -0400602 if (state->end < end && prealloc && !need_resched())
603 next_node = rb_next(&state->rb_node);
604 else
605 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400606
Chris Masond1310b22008-01-24 16:13:08 -0500607 set |= clear_state_bit(tree, state, bits, wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400608 if (last_end == (u64)-1)
609 goto out;
610 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400611 if (start <= end && next_node) {
612 state = rb_entry(next_node, struct extent_state,
613 rb_node);
614 if (state->start == start)
615 goto hit_next;
616 }
Chris Masond1310b22008-01-24 16:13:08 -0500617 goto search_again;
618
619out:
Chris Masoncad321a2008-12-17 14:51:42 -0500620 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500621 if (prealloc)
622 free_extent_state(prealloc);
623
624 return set;
625
626search_again:
627 if (start > end)
628 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500629 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630 if (mask & __GFP_WAIT)
631 cond_resched();
632 goto again;
633}
Chris Masond1310b22008-01-24 16:13:08 -0500634
635static int wait_on_state(struct extent_io_tree *tree,
636 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500637 __releases(tree->lock)
638 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500639{
640 DEFINE_WAIT(wait);
641 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500642 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500643 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500644 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500645 finish_wait(&state->wq, &wait);
646 return 0;
647}
648
649/*
650 * waits for one or more bits to clear on a range in the state tree.
651 * The range [start, end] is inclusive.
652 * The tree lock is taken by this function
653 */
654int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
655{
656 struct extent_state *state;
657 struct rb_node *node;
658
Chris Masoncad321a2008-12-17 14:51:42 -0500659 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500660again:
661 while (1) {
662 /*
663 * this search will find all the extents that end after
664 * our range starts
665 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500666 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500667 if (!node)
668 break;
669
670 state = rb_entry(node, struct extent_state, rb_node);
671
672 if (state->start > end)
673 goto out;
674
675 if (state->state & bits) {
676 start = state->start;
677 atomic_inc(&state->refs);
678 wait_on_state(tree, state);
679 free_extent_state(state);
680 goto again;
681 }
682 start = state->end + 1;
683
684 if (start > end)
685 break;
686
687 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500688 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500689 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500690 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500691 }
692 }
693out:
Chris Masoncad321a2008-12-17 14:51:42 -0500694 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500695 return 0;
696}
Chris Masond1310b22008-01-24 16:13:08 -0500697
Josef Bacik9ed74f22009-09-11 16:12:44 -0400698static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500699 struct extent_state *state,
700 int bits)
701{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400702 int ret;
703
704 ret = set_state_cb(tree, state, bits);
705 if (ret)
706 return ret;
707
Chris Masond1310b22008-01-24 16:13:08 -0500708 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
709 u64 range = state->end - state->start + 1;
710 tree->dirty_bytes += range;
711 }
Chris Masonb0c68f82008-01-31 11:05:37 -0500712 state->state |= bits;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400713
714 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500715}
716
Chris Mason2c64c532009-09-02 15:04:12 -0400717static void cache_state(struct extent_state *state,
718 struct extent_state **cached_ptr)
719{
720 if (cached_ptr && !(*cached_ptr)) {
721 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
722 *cached_ptr = state;
723 atomic_inc(&state->refs);
724 }
725 }
726}
727
Chris Masond1310b22008-01-24 16:13:08 -0500728/*
Chris Mason1edbb732009-09-02 13:24:36 -0400729 * set some bits on a range in the tree. This may require allocations or
730 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500731 *
Chris Mason1edbb732009-09-02 13:24:36 -0400732 * If any of the exclusive bits are set, this will fail with -EEXIST if some
733 * part of the range already has the desired bits set. The start of the
734 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500735 *
Chris Mason1edbb732009-09-02 13:24:36 -0400736 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500737 */
Chris Mason1edbb732009-09-02 13:24:36 -0400738
Chris Masond3977122009-01-05 21:25:51 -0500739static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason1edbb732009-09-02 13:24:36 -0400740 int bits, int exclusive_bits, u64 *failed_start,
Chris Mason2c64c532009-09-02 15:04:12 -0400741 struct extent_state **cached_state,
Chris Masond3977122009-01-05 21:25:51 -0500742 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500743{
744 struct extent_state *state;
745 struct extent_state *prealloc = NULL;
746 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500747 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500748 u64 last_start;
749 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400750
Chris Masond1310b22008-01-24 16:13:08 -0500751again:
752 if (!prealloc && (mask & __GFP_WAIT)) {
753 prealloc = alloc_extent_state(mask);
754 if (!prealloc)
755 return -ENOMEM;
756 }
757
Chris Masoncad321a2008-12-17 14:51:42 -0500758 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400759 if (cached_state && *cached_state) {
760 state = *cached_state;
761 if (state->start == start && state->tree) {
762 node = &state->rb_node;
763 goto hit_next;
764 }
765 }
Chris Masond1310b22008-01-24 16:13:08 -0500766 /*
767 * this search will find all the extents that end after
768 * our range starts.
769 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500770 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500771 if (!node) {
772 err = insert_state(tree, prealloc, start, end, bits);
773 prealloc = NULL;
774 BUG_ON(err == -EEXIST);
775 goto out;
776 }
Chris Masond1310b22008-01-24 16:13:08 -0500777 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400778hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500779 last_start = state->start;
780 last_end = state->end;
781
782 /*
783 * | ---- desired range ---- |
784 * | state |
785 *
786 * Just lock what we found and keep going
787 */
788 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400789 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400790 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500791 *failed_start = state->start;
792 err = -EEXIST;
793 goto out;
794 }
Chris Mason42daec22009-09-23 19:51:09 -0400795
Josef Bacik9ed74f22009-09-11 16:12:44 -0400796 err = set_state_bits(tree, state, bits);
797 if (err)
798 goto out;
799
Chris Mason2c64c532009-09-02 15:04:12 -0400800 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500801 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400802 if (last_end == (u64)-1)
803 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400804
Yan Zheng5c939df2009-05-27 09:16:03 -0400805 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400806 if (start < end && prealloc && !need_resched()) {
807 next_node = rb_next(node);
808 if (next_node) {
809 state = rb_entry(next_node, struct extent_state,
810 rb_node);
811 if (state->start == start)
812 goto hit_next;
813 }
814 }
Chris Masond1310b22008-01-24 16:13:08 -0500815 goto search_again;
816 }
817
818 /*
819 * | ---- desired range ---- |
820 * | state |
821 * or
822 * | ------------- state -------------- |
823 *
824 * We need to split the extent we found, and may flip bits on
825 * second half.
826 *
827 * If the extent we found extends past our
828 * range, we just split and search again. It'll get split
829 * again the next time though.
830 *
831 * If the extent we found is inside our range, we set the
832 * desired bit on it.
833 */
834 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400835 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500836 *failed_start = start;
837 err = -EEXIST;
838 goto out;
839 }
840 err = split_state(tree, state, prealloc, start);
841 BUG_ON(err == -EEXIST);
842 prealloc = NULL;
843 if (err)
844 goto out;
845 if (state->end <= end) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400846 err = set_state_bits(tree, state, bits);
847 if (err)
848 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400849 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500850 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400851 if (last_end == (u64)-1)
852 goto out;
853 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500854 }
855 goto search_again;
856 }
857 /*
858 * | ---- desired range ---- |
859 * | state | or | state |
860 *
861 * There's a hole, we need to insert something in it and
862 * ignore the extent we found.
863 */
864 if (state->start > start) {
865 u64 this_end;
866 if (end < last_start)
867 this_end = end;
868 else
Chris Masond3977122009-01-05 21:25:51 -0500869 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500870 err = insert_state(tree, prealloc, start, this_end,
871 bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400872 BUG_ON(err == -EEXIST);
873 if (err) {
874 prealloc = NULL;
875 goto out;
876 }
Chris Mason2c64c532009-09-02 15:04:12 -0400877 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500878 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500879 start = this_end + 1;
880 goto search_again;
881 }
882 /*
883 * | ---- desired range ---- |
884 * | state |
885 * We need to split the extent, and set the bit
886 * on the first half
887 */
888 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400889 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500890 *failed_start = start;
891 err = -EEXIST;
892 goto out;
893 }
894 err = split_state(tree, state, prealloc, end + 1);
895 BUG_ON(err == -EEXIST);
896
Josef Bacik9ed74f22009-09-11 16:12:44 -0400897 err = set_state_bits(tree, prealloc, bits);
898 if (err) {
899 prealloc = NULL;
900 goto out;
901 }
Chris Mason2c64c532009-09-02 15:04:12 -0400902 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500903 merge_state(tree, prealloc);
904 prealloc = NULL;
905 goto out;
906 }
907
908 goto search_again;
909
910out:
Chris Masoncad321a2008-12-17 14:51:42 -0500911 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500912 if (prealloc)
913 free_extent_state(prealloc);
914
915 return err;
916
917search_again:
918 if (start > end)
919 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500920 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500921 if (mask & __GFP_WAIT)
922 cond_resched();
923 goto again;
924}
Chris Masond1310b22008-01-24 16:13:08 -0500925
926/* wrappers around set/clear extent bit */
927int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
928 gfp_t mask)
929{
930 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400931 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500932}
Chris Masond1310b22008-01-24 16:13:08 -0500933
934int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
935 int bits, gfp_t mask)
936{
937 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400938 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500939}
Chris Masond1310b22008-01-24 16:13:08 -0500940
941int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
942 int bits, gfp_t mask)
943{
Chris Mason2c64c532009-09-02 15:04:12 -0400944 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500945}
Chris Masond1310b22008-01-24 16:13:08 -0500946
947int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
948 gfp_t mask)
949{
950 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400951 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Chris Mason2c64c532009-09-02 15:04:12 -0400952 0, NULL, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500953}
Chris Masond1310b22008-01-24 16:13:08 -0500954
955int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
956 gfp_t mask)
957{
958 return clear_extent_bit(tree, start, end,
Chris Mason2c64c532009-09-02 15:04:12 -0400959 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
960 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500961}
Chris Masond1310b22008-01-24 16:13:08 -0500962
963int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
964 gfp_t mask)
965{
966 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400967 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500968}
Chris Masond1310b22008-01-24 16:13:08 -0500969
Christoph Hellwigb2950862008-12-02 09:54:17 -0500970static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500971 gfp_t mask)
972{
Chris Mason2c64c532009-09-02 15:04:12 -0400973 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
974 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500975}
Chris Masond1310b22008-01-24 16:13:08 -0500976
977int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
978 gfp_t mask)
979{
980 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400981 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500982}
Chris Masond1310b22008-01-24 16:13:08 -0500983
Chris Masond3977122009-01-05 21:25:51 -0500984static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
985 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500986{
Chris Mason2c64c532009-09-02 15:04:12 -0400987 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
988 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500989}
Chris Masond1310b22008-01-24 16:13:08 -0500990
Chris Masond1310b22008-01-24 16:13:08 -0500991int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
992{
993 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
994}
Chris Masond1310b22008-01-24 16:13:08 -0500995
Chris Masond352ac62008-09-29 15:18:18 -0400996/*
997 * either insert or lock state struct between start and end use mask to tell
998 * us if waiting is desired.
999 */
Chris Mason1edbb732009-09-02 13:24:36 -04001000int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001001 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001002{
1003 int err;
1004 u64 failed_start;
1005 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001006 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001007 EXTENT_LOCKED, &failed_start,
1008 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001009 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1010 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1011 start = failed_start;
1012 } else {
1013 break;
1014 }
1015 WARN_ON(start > end);
1016 }
1017 return err;
1018}
Chris Masond1310b22008-01-24 16:13:08 -05001019
Chris Mason1edbb732009-09-02 13:24:36 -04001020int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1021{
Chris Mason2c64c532009-09-02 15:04:12 -04001022 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001023}
1024
Josef Bacik25179202008-10-29 14:49:05 -04001025int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1026 gfp_t mask)
1027{
1028 int err;
1029 u64 failed_start;
1030
Chris Mason2c64c532009-09-02 15:04:12 -04001031 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1032 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001033 if (err == -EEXIST) {
1034 if (failed_start > start)
1035 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001036 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001037 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001038 }
Josef Bacik25179202008-10-29 14:49:05 -04001039 return 1;
1040}
Josef Bacik25179202008-10-29 14:49:05 -04001041
Chris Mason2c64c532009-09-02 15:04:12 -04001042int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1043 struct extent_state **cached, gfp_t mask)
1044{
1045 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1046 mask);
1047}
1048
Chris Masond1310b22008-01-24 16:13:08 -05001049int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1050 gfp_t mask)
1051{
Chris Mason2c64c532009-09-02 15:04:12 -04001052 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1053 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001054}
Chris Masond1310b22008-01-24 16:13:08 -05001055
1056/*
1057 * helper function to set pages and extents in the tree dirty
1058 */
1059int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1060{
1061 unsigned long index = start >> PAGE_CACHE_SHIFT;
1062 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1063 struct page *page;
1064
1065 while (index <= end_index) {
1066 page = find_get_page(tree->mapping, index);
1067 BUG_ON(!page);
1068 __set_page_dirty_nobuffers(page);
1069 page_cache_release(page);
1070 index++;
1071 }
Chris Masond1310b22008-01-24 16:13:08 -05001072 return 0;
1073}
Chris Masond1310b22008-01-24 16:13:08 -05001074
1075/*
1076 * helper function to set both pages and extents in the tree writeback
1077 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001078static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001079{
1080 unsigned long index = start >> PAGE_CACHE_SHIFT;
1081 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1082 struct page *page;
1083
1084 while (index <= end_index) {
1085 page = find_get_page(tree->mapping, index);
1086 BUG_ON(!page);
1087 set_page_writeback(page);
1088 page_cache_release(page);
1089 index++;
1090 }
Chris Masond1310b22008-01-24 16:13:08 -05001091 return 0;
1092}
Chris Masond1310b22008-01-24 16:13:08 -05001093
Chris Masond352ac62008-09-29 15:18:18 -04001094/*
1095 * find the first offset in the io tree with 'bits' set. zero is
1096 * returned if we find something, and *start_ret and *end_ret are
1097 * set to reflect the state struct that was found.
1098 *
1099 * If nothing was found, 1 is returned, < 0 on error
1100 */
Chris Masond1310b22008-01-24 16:13:08 -05001101int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1102 u64 *start_ret, u64 *end_ret, int bits)
1103{
1104 struct rb_node *node;
1105 struct extent_state *state;
1106 int ret = 1;
1107
Chris Masoncad321a2008-12-17 14:51:42 -05001108 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001109 /*
1110 * this search will find all the extents that end after
1111 * our range starts.
1112 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001113 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001114 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001115 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001116
Chris Masond3977122009-01-05 21:25:51 -05001117 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001118 state = rb_entry(node, struct extent_state, rb_node);
1119 if (state->end >= start && (state->state & bits)) {
1120 *start_ret = state->start;
1121 *end_ret = state->end;
1122 ret = 0;
1123 break;
1124 }
1125 node = rb_next(node);
1126 if (!node)
1127 break;
1128 }
1129out:
Chris Masoncad321a2008-12-17 14:51:42 -05001130 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001131 return ret;
1132}
Chris Masond1310b22008-01-24 16:13:08 -05001133
Chris Masond352ac62008-09-29 15:18:18 -04001134/* find the first state struct with 'bits' set after 'start', and
1135 * return it. tree->lock must be held. NULL will returned if
1136 * nothing was found after 'start'
1137 */
Chris Masond7fc6402008-02-18 12:12:38 -05001138struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1139 u64 start, int bits)
1140{
1141 struct rb_node *node;
1142 struct extent_state *state;
1143
1144 /*
1145 * this search will find all the extents that end after
1146 * our range starts.
1147 */
1148 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001149 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001150 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001151
Chris Masond3977122009-01-05 21:25:51 -05001152 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001153 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001154 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001155 return state;
Chris Masond3977122009-01-05 21:25:51 -05001156
Chris Masond7fc6402008-02-18 12:12:38 -05001157 node = rb_next(node);
1158 if (!node)
1159 break;
1160 }
1161out:
1162 return NULL;
1163}
Chris Masond7fc6402008-02-18 12:12:38 -05001164
Chris Masond352ac62008-09-29 15:18:18 -04001165/*
1166 * find a contiguous range of bytes in the file marked as delalloc, not
1167 * more than 'max_bytes'. start and end are used to return the range,
1168 *
1169 * 1 is returned if we find something, 0 if nothing was in the tree
1170 */
Chris Masonc8b97812008-10-29 14:49:59 -04001171static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1172 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001173{
1174 struct rb_node *node;
1175 struct extent_state *state;
1176 u64 cur_start = *start;
1177 u64 found = 0;
1178 u64 total_bytes = 0;
1179
Chris Masoncad321a2008-12-17 14:51:42 -05001180 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001181
Chris Masond1310b22008-01-24 16:13:08 -05001182 /*
1183 * this search will find all the extents that end after
1184 * our range starts.
1185 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001186 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001187 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001188 if (!found)
1189 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001190 goto out;
1191 }
1192
Chris Masond3977122009-01-05 21:25:51 -05001193 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001194 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001195 if (found && (state->start != cur_start ||
1196 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001197 goto out;
1198 }
1199 if (!(state->state & EXTENT_DELALLOC)) {
1200 if (!found)
1201 *end = state->end;
1202 goto out;
1203 }
Chris Masond1310b22008-01-24 16:13:08 -05001204 if (!found)
1205 *start = state->start;
1206 found++;
1207 *end = state->end;
1208 cur_start = state->end + 1;
1209 node = rb_next(node);
1210 if (!node)
1211 break;
1212 total_bytes += state->end - state->start + 1;
1213 if (total_bytes >= max_bytes)
1214 break;
1215 }
1216out:
Chris Masoncad321a2008-12-17 14:51:42 -05001217 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001218 return found;
1219}
1220
Chris Masonc8b97812008-10-29 14:49:59 -04001221static noinline int __unlock_for_delalloc(struct inode *inode,
1222 struct page *locked_page,
1223 u64 start, u64 end)
1224{
1225 int ret;
1226 struct page *pages[16];
1227 unsigned long index = start >> PAGE_CACHE_SHIFT;
1228 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1229 unsigned long nr_pages = end_index - index + 1;
1230 int i;
1231
1232 if (index == locked_page->index && end_index == index)
1233 return 0;
1234
Chris Masond3977122009-01-05 21:25:51 -05001235 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001236 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001237 min_t(unsigned long, nr_pages,
1238 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001239 for (i = 0; i < ret; i++) {
1240 if (pages[i] != locked_page)
1241 unlock_page(pages[i]);
1242 page_cache_release(pages[i]);
1243 }
1244 nr_pages -= ret;
1245 index += ret;
1246 cond_resched();
1247 }
1248 return 0;
1249}
1250
1251static noinline int lock_delalloc_pages(struct inode *inode,
1252 struct page *locked_page,
1253 u64 delalloc_start,
1254 u64 delalloc_end)
1255{
1256 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1257 unsigned long start_index = index;
1258 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1259 unsigned long pages_locked = 0;
1260 struct page *pages[16];
1261 unsigned long nrpages;
1262 int ret;
1263 int i;
1264
1265 /* the caller is responsible for locking the start index */
1266 if (index == locked_page->index && index == end_index)
1267 return 0;
1268
1269 /* skip the page at the start index */
1270 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001271 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001272 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001273 min_t(unsigned long,
1274 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001275 if (ret == 0) {
1276 ret = -EAGAIN;
1277 goto done;
1278 }
1279 /* now we have an array of pages, lock them all */
1280 for (i = 0; i < ret; i++) {
1281 /*
1282 * the caller is taking responsibility for
1283 * locked_page
1284 */
Chris Mason771ed682008-11-06 22:02:51 -05001285 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001286 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001287 if (!PageDirty(pages[i]) ||
1288 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001289 ret = -EAGAIN;
1290 unlock_page(pages[i]);
1291 page_cache_release(pages[i]);
1292 goto done;
1293 }
1294 }
Chris Masonc8b97812008-10-29 14:49:59 -04001295 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001296 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001297 }
Chris Masonc8b97812008-10-29 14:49:59 -04001298 nrpages -= ret;
1299 index += ret;
1300 cond_resched();
1301 }
1302 ret = 0;
1303done:
1304 if (ret && pages_locked) {
1305 __unlock_for_delalloc(inode, locked_page,
1306 delalloc_start,
1307 ((u64)(start_index + pages_locked - 1)) <<
1308 PAGE_CACHE_SHIFT);
1309 }
1310 return ret;
1311}
1312
1313/*
1314 * 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 */
1319static noinline u64 find_lock_delalloc_range(struct inode *inode,
1320 struct extent_io_tree *tree,
1321 struct page *locked_page,
1322 u64 *start, u64 *end,
1323 u64 max_bytes)
1324{
1325 u64 delalloc_start;
1326 u64 delalloc_end;
1327 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001328 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001329 int ret;
1330 int loops = 0;
1331
1332again:
1333 /* step one, find a bunch of delalloc bytes starting at start */
1334 delalloc_start = *start;
1335 delalloc_end = 0;
1336 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1337 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001338 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001339 *start = delalloc_start;
1340 *end = delalloc_end;
1341 return found;
1342 }
1343
1344 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001345 * start comes from the offset of locked_page. We have to lock
1346 * pages in order, so we can't process delalloc bytes before
1347 * locked_page
1348 */
Chris Masond3977122009-01-05 21:25:51 -05001349 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001350 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001351
1352 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001353 * make sure to limit the number of pages we try to lock down
1354 * if we're looping.
1355 */
Chris Masond3977122009-01-05 21:25:51 -05001356 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001357 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001358
Chris Masonc8b97812008-10-29 14:49:59 -04001359 /* step two, lock all the pages after the page that has start */
1360 ret = lock_delalloc_pages(inode, locked_page,
1361 delalloc_start, delalloc_end);
1362 if (ret == -EAGAIN) {
1363 /* some of the pages are gone, lets avoid looping by
1364 * shortening the size of the delalloc range we're searching
1365 */
Chris Mason9655d292009-09-02 15:22:30 -04001366 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001367 if (!loops) {
1368 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1369 max_bytes = PAGE_CACHE_SIZE - offset;
1370 loops = 1;
1371 goto again;
1372 } else {
1373 found = 0;
1374 goto out_failed;
1375 }
1376 }
1377 BUG_ON(ret);
1378
1379 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001380 lock_extent_bits(tree, delalloc_start, delalloc_end,
1381 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001382
1383 /* then test to make sure it is all still delalloc */
1384 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001385 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001386 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001387 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1388 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001389 __unlock_for_delalloc(inode, locked_page,
1390 delalloc_start, delalloc_end);
1391 cond_resched();
1392 goto again;
1393 }
Chris Mason9655d292009-09-02 15:22:30 -04001394 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001395 *start = delalloc_start;
1396 *end = delalloc_end;
1397out_failed:
1398 return found;
1399}
1400
1401int extent_clear_unlock_delalloc(struct inode *inode,
1402 struct extent_io_tree *tree,
1403 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001404 int unlock_pages,
1405 int clear_unlock,
1406 int clear_delalloc, int clear_dirty,
1407 int set_writeback,
Chris Mason8b62b722009-09-02 16:53:46 -04001408 int end_writeback,
1409 int set_private2)
Chris Masonc8b97812008-10-29 14:49:59 -04001410{
1411 int ret;
1412 struct page *pages[16];
1413 unsigned long index = start >> PAGE_CACHE_SHIFT;
1414 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1415 unsigned long nr_pages = end_index - index + 1;
1416 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001417 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001418
Chris Mason771ed682008-11-06 22:02:51 -05001419 if (clear_unlock)
1420 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001421 if (clear_dirty)
1422 clear_bits |= EXTENT_DIRTY;
1423
Chris Mason771ed682008-11-06 22:02:51 -05001424 if (clear_delalloc)
1425 clear_bits |= EXTENT_DELALLOC;
1426
Chris Mason2c64c532009-09-02 15:04:12 -04001427 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04001428 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback ||
1429 set_private2))
Chris Mason771ed682008-11-06 22:02:51 -05001430 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001431
Chris Masond3977122009-01-05 21:25:51 -05001432 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001433 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001434 min_t(unsigned long,
1435 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001436 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001437
1438 if (set_private2)
1439 SetPagePrivate2(pages[i]);
1440
Chris Masonc8b97812008-10-29 14:49:59 -04001441 if (pages[i] == locked_page) {
1442 page_cache_release(pages[i]);
1443 continue;
1444 }
1445 if (clear_dirty)
1446 clear_page_dirty_for_io(pages[i]);
1447 if (set_writeback)
1448 set_page_writeback(pages[i]);
1449 if (end_writeback)
1450 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001451 if (unlock_pages)
1452 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001453 page_cache_release(pages[i]);
1454 }
1455 nr_pages -= ret;
1456 index += ret;
1457 cond_resched();
1458 }
1459 return 0;
1460}
Chris Masonc8b97812008-10-29 14:49:59 -04001461
Chris Masond352ac62008-09-29 15:18:18 -04001462/*
1463 * count the number of bytes in the tree that have a given bit(s)
1464 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1465 * cached. The total number found is returned.
1466 */
Chris Masond1310b22008-01-24 16:13:08 -05001467u64 count_range_bits(struct extent_io_tree *tree,
1468 u64 *start, u64 search_end, u64 max_bytes,
1469 unsigned long bits)
1470{
1471 struct rb_node *node;
1472 struct extent_state *state;
1473 u64 cur_start = *start;
1474 u64 total_bytes = 0;
1475 int found = 0;
1476
1477 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001478 WARN_ON(1);
1479 return 0;
1480 }
1481
Chris Masoncad321a2008-12-17 14:51:42 -05001482 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001483 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1484 total_bytes = tree->dirty_bytes;
1485 goto out;
1486 }
1487 /*
1488 * this search will find all the extents that end after
1489 * our range starts.
1490 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001491 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001492 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001493 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001494
Chris Masond3977122009-01-05 21:25:51 -05001495 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001496 state = rb_entry(node, struct extent_state, rb_node);
1497 if (state->start > search_end)
1498 break;
1499 if (state->end >= cur_start && (state->state & bits)) {
1500 total_bytes += min(search_end, state->end) + 1 -
1501 max(cur_start, state->start);
1502 if (total_bytes >= max_bytes)
1503 break;
1504 if (!found) {
1505 *start = state->start;
1506 found = 1;
1507 }
1508 }
1509 node = rb_next(node);
1510 if (!node)
1511 break;
1512 }
1513out:
Chris Masoncad321a2008-12-17 14:51:42 -05001514 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001515 return total_bytes;
1516}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001517
Chris Masond352ac62008-09-29 15:18:18 -04001518/*
1519 * set the private field for a given byte offset in the tree. If there isn't
1520 * an extent_state there already, this does nothing.
1521 */
Chris Masond1310b22008-01-24 16:13:08 -05001522int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1523{
1524 struct rb_node *node;
1525 struct extent_state *state;
1526 int ret = 0;
1527
Chris Masoncad321a2008-12-17 14:51:42 -05001528 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001529 /*
1530 * this search will find all the extents that end after
1531 * our range starts.
1532 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001533 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001534 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001535 ret = -ENOENT;
1536 goto out;
1537 }
1538 state = rb_entry(node, struct extent_state, rb_node);
1539 if (state->start != start) {
1540 ret = -ENOENT;
1541 goto out;
1542 }
1543 state->private = private;
1544out:
Chris Masoncad321a2008-12-17 14:51:42 -05001545 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001546 return ret;
1547}
1548
1549int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1550{
1551 struct rb_node *node;
1552 struct extent_state *state;
1553 int ret = 0;
1554
Chris Masoncad321a2008-12-17 14:51:42 -05001555 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001556 /*
1557 * this search will find all the extents that end after
1558 * our range starts.
1559 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001560 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001561 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001562 ret = -ENOENT;
1563 goto out;
1564 }
1565 state = rb_entry(node, struct extent_state, rb_node);
1566 if (state->start != start) {
1567 ret = -ENOENT;
1568 goto out;
1569 }
1570 *private = state->private;
1571out:
Chris Masoncad321a2008-12-17 14:51:42 -05001572 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001573 return ret;
1574}
1575
1576/*
1577 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001578 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001579 * has the bits set. Otherwise, 1 is returned if any bit in the
1580 * range is found set.
1581 */
1582int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001583 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001584{
1585 struct extent_state *state = NULL;
1586 struct rb_node *node;
1587 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001588
Chris Masoncad321a2008-12-17 14:51:42 -05001589 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001590 if (cached && cached->tree && cached->start == start)
1591 node = &cached->rb_node;
1592 else
1593 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001594 while (node && start <= end) {
1595 state = rb_entry(node, struct extent_state, rb_node);
1596
1597 if (filled && state->start > start) {
1598 bitset = 0;
1599 break;
1600 }
1601
1602 if (state->start > end)
1603 break;
1604
1605 if (state->state & bits) {
1606 bitset = 1;
1607 if (!filled)
1608 break;
1609 } else if (filled) {
1610 bitset = 0;
1611 break;
1612 }
Chris Mason46562cec2009-09-23 20:23:16 -04001613
1614 if (state->end == (u64)-1)
1615 break;
1616
Chris Masond1310b22008-01-24 16:13:08 -05001617 start = state->end + 1;
1618 if (start > end)
1619 break;
1620 node = rb_next(node);
1621 if (!node) {
1622 if (filled)
1623 bitset = 0;
1624 break;
1625 }
1626 }
Chris Masoncad321a2008-12-17 14:51:42 -05001627 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001628 return bitset;
1629}
Chris Masond1310b22008-01-24 16:13:08 -05001630
1631/*
1632 * helper function to set a given page up to date if all the
1633 * extents in the tree for that page are up to date
1634 */
1635static int check_page_uptodate(struct extent_io_tree *tree,
1636 struct page *page)
1637{
1638 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1639 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001640 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001641 SetPageUptodate(page);
1642 return 0;
1643}
1644
1645/*
1646 * helper function to unlock a page if all the extents in the tree
1647 * for that page are unlocked
1648 */
1649static int check_page_locked(struct extent_io_tree *tree,
1650 struct page *page)
1651{
1652 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1653 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001654 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001655 unlock_page(page);
1656 return 0;
1657}
1658
1659/*
1660 * helper function to end page writeback if all the extents
1661 * in the tree for that page are done with writeback
1662 */
1663static int check_page_writeback(struct extent_io_tree *tree,
1664 struct page *page)
1665{
Chris Mason1edbb732009-09-02 13:24:36 -04001666 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001667 return 0;
1668}
1669
1670/* lots and lots of room for performance fixes in the end_bio funcs */
1671
1672/*
1673 * after a writepage IO is done, we need to:
1674 * clear the uptodate bits on error
1675 * clear the writeback bits in the extent tree for this IO
1676 * end_page_writeback if the page has no more pending IO
1677 *
1678 * Scheduling is not allowed, so the extent state tree is expected
1679 * to have one and only one object corresponding to this IO.
1680 */
Chris Masond1310b22008-01-24 16:13:08 -05001681static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001682{
Chris Mason1259ab72008-05-12 13:39:03 -04001683 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001684 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001685 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001686 u64 start;
1687 u64 end;
1688 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001689 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001690
Chris Masond1310b22008-01-24 16:13:08 -05001691 do {
1692 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001693 tree = &BTRFS_I(page->mapping->host)->io_tree;
1694
Chris Masond1310b22008-01-24 16:13:08 -05001695 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1696 bvec->bv_offset;
1697 end = start + bvec->bv_len - 1;
1698
1699 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1700 whole_page = 1;
1701 else
1702 whole_page = 0;
1703
1704 if (--bvec >= bio->bi_io_vec)
1705 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001706 if (tree->ops && tree->ops->writepage_end_io_hook) {
1707 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001708 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001709 if (ret)
1710 uptodate = 0;
1711 }
1712
1713 if (!uptodate && tree->ops &&
1714 tree->ops->writepage_io_failed_hook) {
1715 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001716 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001717 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001718 uptodate = (err == 0);
1719 continue;
1720 }
1721 }
1722
Chris Masond1310b22008-01-24 16:13:08 -05001723 if (!uptodate) {
Chris Mason1edbb732009-09-02 13:24:36 -04001724 clear_extent_uptodate(tree, start, end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001725 ClearPageUptodate(page);
1726 SetPageError(page);
1727 }
Chris Mason70dec802008-01-29 09:59:12 -05001728
Chris Masond1310b22008-01-24 16:13:08 -05001729 if (whole_page)
1730 end_page_writeback(page);
1731 else
1732 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001733 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001734
Chris Masond1310b22008-01-24 16:13:08 -05001735 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001736}
1737
1738/*
1739 * after a readpage IO is done, we need to:
1740 * clear the uptodate bits on error
1741 * set the uptodate bits if things worked
1742 * set the page up to date if all extents in the tree are uptodate
1743 * clear the lock bit in the extent tree
1744 * unlock the page if there are no other extents locked for it
1745 *
1746 * Scheduling is not allowed, so the extent state tree is expected
1747 * to have one and only one object corresponding to this IO.
1748 */
Chris Masond1310b22008-01-24 16:13:08 -05001749static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001750{
1751 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1752 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001753 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001754 u64 start;
1755 u64 end;
1756 int whole_page;
1757 int ret;
1758
Chris Masond20f7042008-12-08 16:58:54 -05001759 if (err)
1760 uptodate = 0;
1761
Chris Masond1310b22008-01-24 16:13:08 -05001762 do {
1763 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001764 tree = &BTRFS_I(page->mapping->host)->io_tree;
1765
Chris Masond1310b22008-01-24 16:13:08 -05001766 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1767 bvec->bv_offset;
1768 end = start + bvec->bv_len - 1;
1769
1770 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1771 whole_page = 1;
1772 else
1773 whole_page = 0;
1774
1775 if (--bvec >= bio->bi_io_vec)
1776 prefetchw(&bvec->bv_page->flags);
1777
1778 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001779 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001780 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001781 if (ret)
1782 uptodate = 0;
1783 }
Chris Mason7e383262008-04-09 16:28:12 -04001784 if (!uptodate && tree->ops &&
1785 tree->ops->readpage_io_failed_hook) {
1786 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001787 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001788 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001789 uptodate =
1790 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001791 if (err)
1792 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001793 continue;
1794 }
1795 }
Chris Mason70dec802008-01-29 09:59:12 -05001796
Chris Mason771ed682008-11-06 22:02:51 -05001797 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001798 set_extent_uptodate(tree, start, end,
1799 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001800 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001801 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001802
Chris Mason70dec802008-01-29 09:59:12 -05001803 if (whole_page) {
1804 if (uptodate) {
1805 SetPageUptodate(page);
1806 } else {
1807 ClearPageUptodate(page);
1808 SetPageError(page);
1809 }
Chris Masond1310b22008-01-24 16:13:08 -05001810 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001811 } else {
1812 if (uptodate) {
1813 check_page_uptodate(tree, page);
1814 } else {
1815 ClearPageUptodate(page);
1816 SetPageError(page);
1817 }
Chris Masond1310b22008-01-24 16:13:08 -05001818 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001819 }
Chris Masond1310b22008-01-24 16:13:08 -05001820 } while (bvec >= bio->bi_io_vec);
1821
1822 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001823}
1824
1825/*
1826 * IO done from prepare_write is pretty simple, we just unlock
1827 * the structs in the extent tree when done, and set the uptodate bits
1828 * as appropriate.
1829 */
Chris Masond1310b22008-01-24 16:13:08 -05001830static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001831{
1832 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1833 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001834 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001835 u64 start;
1836 u64 end;
1837
Chris Masond1310b22008-01-24 16:13:08 -05001838 do {
1839 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001840 tree = &BTRFS_I(page->mapping->host)->io_tree;
1841
Chris Masond1310b22008-01-24 16:13:08 -05001842 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1843 bvec->bv_offset;
1844 end = start + bvec->bv_len - 1;
1845
1846 if (--bvec >= bio->bi_io_vec)
1847 prefetchw(&bvec->bv_page->flags);
1848
1849 if (uptodate) {
1850 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1851 } else {
1852 ClearPageUptodate(page);
1853 SetPageError(page);
1854 }
1855
1856 unlock_extent(tree, start, end, GFP_ATOMIC);
1857
1858 } while (bvec >= bio->bi_io_vec);
1859
1860 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001861}
1862
1863static struct bio *
1864extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1865 gfp_t gfp_flags)
1866{
1867 struct bio *bio;
1868
1869 bio = bio_alloc(gfp_flags, nr_vecs);
1870
1871 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1872 while (!bio && (nr_vecs /= 2))
1873 bio = bio_alloc(gfp_flags, nr_vecs);
1874 }
1875
1876 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001877 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001878 bio->bi_bdev = bdev;
1879 bio->bi_sector = first_sector;
1880 }
1881 return bio;
1882}
1883
Chris Masonc8b97812008-10-29 14:49:59 -04001884static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1885 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001886{
Chris Masond1310b22008-01-24 16:13:08 -05001887 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001888 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1889 struct page *page = bvec->bv_page;
1890 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001891 u64 start;
1892 u64 end;
1893
1894 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1895 end = start + bvec->bv_len - 1;
1896
David Woodhouse902b22f2008-08-20 08:51:49 -04001897 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001898
1899 bio_get(bio);
1900
Chris Mason065631f2008-02-20 12:07:25 -05001901 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001902 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001903 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001904 else
1905 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001906 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1907 ret = -EOPNOTSUPP;
1908 bio_put(bio);
1909 return ret;
1910}
1911
1912static int submit_extent_page(int rw, struct extent_io_tree *tree,
1913 struct page *page, sector_t sector,
1914 size_t size, unsigned long offset,
1915 struct block_device *bdev,
1916 struct bio **bio_ret,
1917 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001918 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001919 int mirror_num,
1920 unsigned long prev_bio_flags,
1921 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001922{
1923 int ret = 0;
1924 struct bio *bio;
1925 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001926 int contig = 0;
1927 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1928 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001929 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001930
1931 if (bio_ret && *bio_ret) {
1932 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001933 if (old_compressed)
1934 contig = bio->bi_sector == sector;
1935 else
1936 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1937 sector;
1938
1939 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001940 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001941 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1942 bio_flags)) ||
1943 bio_add_page(bio, page, page_size, offset) < page_size) {
1944 ret = submit_one_bio(rw, bio, mirror_num,
1945 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001946 bio = NULL;
1947 } else {
1948 return 0;
1949 }
1950 }
Chris Masonc8b97812008-10-29 14:49:59 -04001951 if (this_compressed)
1952 nr = BIO_MAX_PAGES;
1953 else
1954 nr = bio_get_nr_vecs(bdev);
1955
Chris Masond1310b22008-01-24 16:13:08 -05001956 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001957
Chris Masonc8b97812008-10-29 14:49:59 -04001958 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001959 bio->bi_end_io = end_io_func;
1960 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001961
Chris Masond3977122009-01-05 21:25:51 -05001962 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001963 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001964 else
Chris Masonc8b97812008-10-29 14:49:59 -04001965 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001966
1967 return ret;
1968}
1969
1970void set_page_extent_mapped(struct page *page)
1971{
1972 if (!PagePrivate(page)) {
1973 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001974 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001975 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001976 }
1977}
1978
Christoph Hellwigb2950862008-12-02 09:54:17 -05001979static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001980{
1981 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1982}
1983
1984/*
1985 * basic readpage implementation. Locked extent state structs are inserted
1986 * into the tree that are removed when the IO is done (by the end_io
1987 * handlers)
1988 */
1989static int __extent_read_full_page(struct extent_io_tree *tree,
1990 struct page *page,
1991 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001992 struct bio **bio, int mirror_num,
1993 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001994{
1995 struct inode *inode = page->mapping->host;
1996 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1997 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1998 u64 end;
1999 u64 cur = start;
2000 u64 extent_offset;
2001 u64 last_byte = i_size_read(inode);
2002 u64 block_start;
2003 u64 cur_end;
2004 sector_t sector;
2005 struct extent_map *em;
2006 struct block_device *bdev;
2007 int ret;
2008 int nr = 0;
2009 size_t page_offset = 0;
2010 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002011 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002012 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002013 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002014
2015 set_page_extent_mapped(page);
2016
2017 end = page_end;
2018 lock_extent(tree, start, end, GFP_NOFS);
2019
Chris Masonc8b97812008-10-29 14:49:59 -04002020 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2021 char *userpage;
2022 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2023
2024 if (zero_offset) {
2025 iosize = PAGE_CACHE_SIZE - zero_offset;
2026 userpage = kmap_atomic(page, KM_USER0);
2027 memset(userpage + zero_offset, 0, iosize);
2028 flush_dcache_page(page);
2029 kunmap_atomic(userpage, KM_USER0);
2030 }
2031 }
Chris Masond1310b22008-01-24 16:13:08 -05002032 while (cur <= end) {
2033 if (cur >= last_byte) {
2034 char *userpage;
2035 iosize = PAGE_CACHE_SIZE - page_offset;
2036 userpage = kmap_atomic(page, KM_USER0);
2037 memset(userpage + page_offset, 0, iosize);
2038 flush_dcache_page(page);
2039 kunmap_atomic(userpage, KM_USER0);
2040 set_extent_uptodate(tree, cur, cur + iosize - 1,
2041 GFP_NOFS);
2042 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2043 break;
2044 }
2045 em = get_extent(inode, page, page_offset, cur,
2046 end - cur + 1, 0);
2047 if (IS_ERR(em) || !em) {
2048 SetPageError(page);
2049 unlock_extent(tree, cur, end, GFP_NOFS);
2050 break;
2051 }
Chris Masond1310b22008-01-24 16:13:08 -05002052 extent_offset = cur - em->start;
2053 BUG_ON(extent_map_end(em) <= cur);
2054 BUG_ON(end < cur);
2055
Chris Masonc8b97812008-10-29 14:49:59 -04002056 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2057 this_bio_flag = EXTENT_BIO_COMPRESSED;
2058
Chris Masond1310b22008-01-24 16:13:08 -05002059 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2060 cur_end = min(extent_map_end(em) - 1, end);
2061 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002062 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2063 disk_io_size = em->block_len;
2064 sector = em->block_start >> 9;
2065 } else {
2066 sector = (em->block_start + extent_offset) >> 9;
2067 disk_io_size = iosize;
2068 }
Chris Masond1310b22008-01-24 16:13:08 -05002069 bdev = em->bdev;
2070 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002071 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2072 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002073 free_extent_map(em);
2074 em = NULL;
2075
2076 /* we've found a hole, just zero and go on */
2077 if (block_start == EXTENT_MAP_HOLE) {
2078 char *userpage;
2079 userpage = kmap_atomic(page, KM_USER0);
2080 memset(userpage + page_offset, 0, iosize);
2081 flush_dcache_page(page);
2082 kunmap_atomic(userpage, KM_USER0);
2083
2084 set_extent_uptodate(tree, cur, cur + iosize - 1,
2085 GFP_NOFS);
2086 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2087 cur = cur + iosize;
2088 page_offset += iosize;
2089 continue;
2090 }
2091 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002092 if (test_range_bit(tree, cur, cur_end,
2093 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002094 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002095 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2096 cur = cur + iosize;
2097 page_offset += iosize;
2098 continue;
2099 }
Chris Mason70dec802008-01-29 09:59:12 -05002100 /* we have an inline extent but it didn't get marked up
2101 * to date. Error out
2102 */
2103 if (block_start == EXTENT_MAP_INLINE) {
2104 SetPageError(page);
2105 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2106 cur = cur + iosize;
2107 page_offset += iosize;
2108 continue;
2109 }
Chris Masond1310b22008-01-24 16:13:08 -05002110
2111 ret = 0;
2112 if (tree->ops && tree->ops->readpage_io_hook) {
2113 ret = tree->ops->readpage_io_hook(page, cur,
2114 cur + iosize - 1);
2115 }
2116 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002117 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2118 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002119 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002120 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002121 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002122 end_bio_extent_readpage, mirror_num,
2123 *bio_flags,
2124 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002125 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002126 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002127 }
2128 if (ret)
2129 SetPageError(page);
2130 cur = cur + iosize;
2131 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002132 }
2133 if (!nr) {
2134 if (!PageError(page))
2135 SetPageUptodate(page);
2136 unlock_page(page);
2137 }
2138 return 0;
2139}
2140
2141int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2142 get_extent_t *get_extent)
2143{
2144 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002145 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002146 int ret;
2147
Chris Masonc8b97812008-10-29 14:49:59 -04002148 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2149 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002150 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002151 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002152 return ret;
2153}
Chris Masond1310b22008-01-24 16:13:08 -05002154
Chris Mason11c83492009-04-20 15:50:09 -04002155static noinline void update_nr_written(struct page *page,
2156 struct writeback_control *wbc,
2157 unsigned long nr_written)
2158{
2159 wbc->nr_to_write -= nr_written;
2160 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2161 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2162 page->mapping->writeback_index = page->index + nr_written;
2163}
2164
Chris Masond1310b22008-01-24 16:13:08 -05002165/*
2166 * the writepage semantics are similar to regular writepage. extent
2167 * records are inserted to lock ranges in the tree, and as dirty areas
2168 * are found, they are marked writeback. Then the lock bits are removed
2169 * and the end_io handler clears the writeback ranges
2170 */
2171static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2172 void *data)
2173{
2174 struct inode *inode = page->mapping->host;
2175 struct extent_page_data *epd = data;
2176 struct extent_io_tree *tree = epd->tree;
2177 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2178 u64 delalloc_start;
2179 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2180 u64 end;
2181 u64 cur = start;
2182 u64 extent_offset;
2183 u64 last_byte = i_size_read(inode);
2184 u64 block_start;
2185 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002186 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002187 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002188 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002189 struct extent_map *em;
2190 struct block_device *bdev;
2191 int ret;
2192 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002193 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002194 size_t blocksize;
2195 loff_t i_size = i_size_read(inode);
2196 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2197 u64 nr_delalloc;
2198 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002199 int page_started;
2200 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002201 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002202 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002203
Chris Masonffbd5172009-04-20 15:50:09 -04002204 if (wbc->sync_mode == WB_SYNC_ALL)
2205 write_flags = WRITE_SYNC_PLUG;
2206 else
2207 write_flags = WRITE;
2208
Chris Masond1310b22008-01-24 16:13:08 -05002209 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002210 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002211 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002212 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002213 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002214 unlock_page(page);
2215 return 0;
2216 }
2217
2218 if (page->index == end_index) {
2219 char *userpage;
2220
Chris Masond1310b22008-01-24 16:13:08 -05002221 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002222 memset(userpage + pg_offset, 0,
2223 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002224 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002225 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002226 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002227 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002228
2229 set_page_extent_mapped(page);
2230
2231 delalloc_start = start;
2232 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002233 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002234 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002235 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002236 /*
2237 * make sure the wbc mapping index is at least updated
2238 * to this page.
2239 */
2240 update_nr_written(page, wbc, 0);
2241
Chris Masond3977122009-01-05 21:25:51 -05002242 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002243 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002244 page,
2245 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002246 &delalloc_end,
2247 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002248 if (nr_delalloc == 0) {
2249 delalloc_start = delalloc_end + 1;
2250 continue;
2251 }
2252 tree->ops->fill_delalloc(inode, page, delalloc_start,
2253 delalloc_end, &page_started,
2254 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002255 /*
2256 * delalloc_end is already one less than the total
2257 * length, so we don't subtract one from
2258 * PAGE_CACHE_SIZE
2259 */
2260 delalloc_to_write += (delalloc_end - delalloc_start +
2261 PAGE_CACHE_SIZE) >>
2262 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002263 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002264 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002265 if (wbc->nr_to_write < delalloc_to_write) {
2266 int thresh = 8192;
2267
2268 if (delalloc_to_write < thresh * 2)
2269 thresh = delalloc_to_write;
2270 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2271 thresh);
2272 }
Chris Masonc8b97812008-10-29 14:49:59 -04002273
Chris Mason771ed682008-11-06 22:02:51 -05002274 /* did the fill delalloc function already unlock and start
2275 * the IO?
2276 */
2277 if (page_started) {
2278 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002279 /*
2280 * we've unlocked the page, so we can't update
2281 * the mapping's writeback index, just update
2282 * nr_to_write.
2283 */
2284 wbc->nr_to_write -= nr_written;
2285 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002286 }
Chris Masonc8b97812008-10-29 14:49:59 -04002287 }
Chris Mason247e7432008-07-17 12:53:51 -04002288 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002289 ret = tree->ops->writepage_start_hook(page, start,
2290 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002291 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002292 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002293 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002294 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002295 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002296 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002297 }
2298 }
2299
Chris Mason11c83492009-04-20 15:50:09 -04002300 /*
2301 * we don't want to touch the inode after unlocking the page,
2302 * so we update the mapping writeback index now
2303 */
2304 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002305
Chris Masond1310b22008-01-24 16:13:08 -05002306 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002307 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002308 if (tree->ops && tree->ops->writepage_end_io_hook)
2309 tree->ops->writepage_end_io_hook(page, start,
2310 page_end, NULL, 1);
2311 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002312 goto done;
2313 }
2314
Chris Masond1310b22008-01-24 16:13:08 -05002315 blocksize = inode->i_sb->s_blocksize;
2316
2317 while (cur <= end) {
2318 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002319 if (tree->ops && tree->ops->writepage_end_io_hook)
2320 tree->ops->writepage_end_io_hook(page, cur,
2321 page_end, NULL, 1);
2322 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002323 break;
2324 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002325 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002326 end - cur + 1, 1);
2327 if (IS_ERR(em) || !em) {
2328 SetPageError(page);
2329 break;
2330 }
2331
2332 extent_offset = cur - em->start;
2333 BUG_ON(extent_map_end(em) <= cur);
2334 BUG_ON(end < cur);
2335 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2336 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2337 sector = (em->block_start + extent_offset) >> 9;
2338 bdev = em->bdev;
2339 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002340 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002341 free_extent_map(em);
2342 em = NULL;
2343
Chris Masonc8b97812008-10-29 14:49:59 -04002344 /*
2345 * compressed and inline extents are written through other
2346 * paths in the FS
2347 */
2348 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002349 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002350 /*
2351 * end_io notification does not happen here for
2352 * compressed extents
2353 */
2354 if (!compressed && tree->ops &&
2355 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002356 tree->ops->writepage_end_io_hook(page, cur,
2357 cur + iosize - 1,
2358 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002359 else if (compressed) {
2360 /* we don't want to end_page_writeback on
2361 * a compressed extent. this happens
2362 * elsewhere
2363 */
2364 nr++;
2365 }
2366
2367 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002368 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002369 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002370 continue;
2371 }
Chris Masond1310b22008-01-24 16:13:08 -05002372 /* leave this out until we have a page_mkwrite call */
2373 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002374 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002375 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002376 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002377 continue;
2378 }
Chris Masonc8b97812008-10-29 14:49:59 -04002379
Chris Masond1310b22008-01-24 16:13:08 -05002380 if (tree->ops && tree->ops->writepage_io_hook) {
2381 ret = tree->ops->writepage_io_hook(page, cur,
2382 cur + iosize - 1);
2383 } else {
2384 ret = 0;
2385 }
Chris Mason1259ab72008-05-12 13:39:03 -04002386 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002387 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002388 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002389 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002390
Chris Masond1310b22008-01-24 16:13:08 -05002391 set_range_writeback(tree, cur, cur + iosize - 1);
2392 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002393 printk(KERN_ERR "btrfs warning page %lu not "
2394 "writeback, cur %llu end %llu\n",
2395 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002396 (unsigned long long)end);
2397 }
2398
Chris Masonffbd5172009-04-20 15:50:09 -04002399 ret = submit_extent_page(write_flags, tree, page,
2400 sector, iosize, pg_offset,
2401 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002402 end_bio_extent_writepage,
2403 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002404 if (ret)
2405 SetPageError(page);
2406 }
2407 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002408 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002409 nr++;
2410 }
2411done:
2412 if (nr == 0) {
2413 /* make sure the mapping tag for page dirty gets cleared */
2414 set_page_writeback(page);
2415 end_page_writeback(page);
2416 }
Chris Masond1310b22008-01-24 16:13:08 -05002417 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002418
Chris Mason11c83492009-04-20 15:50:09 -04002419done_unlocked:
2420
Chris Mason2c64c532009-09-02 15:04:12 -04002421 /* drop our reference on any cached states */
2422 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002423 return 0;
2424}
2425
Chris Masond1310b22008-01-24 16:13:08 -05002426/**
Chris Mason4bef0842008-09-08 11:18:08 -04002427 * 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 -05002428 * @mapping: address space structure to write
2429 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2430 * @writepage: function called for each page
2431 * @data: data passed to writepage function
2432 *
2433 * If a page is already under I/O, write_cache_pages() skips it, even
2434 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2435 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2436 * and msync() need to guarantee that all the data which was dirty at the time
2437 * the call was made get new I/O started against them. If wbc->sync_mode is
2438 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2439 * existing IO to complete.
2440 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002441static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002442 struct address_space *mapping,
2443 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002444 writepage_t writepage, void *data,
2445 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002446{
Chris Masond1310b22008-01-24 16:13:08 -05002447 int ret = 0;
2448 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002449 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002450 struct pagevec pvec;
2451 int nr_pages;
2452 pgoff_t index;
2453 pgoff_t end; /* Inclusive */
2454 int scanned = 0;
2455 int range_whole = 0;
2456
Chris Masond1310b22008-01-24 16:13:08 -05002457 pagevec_init(&pvec, 0);
2458 if (wbc->range_cyclic) {
2459 index = mapping->writeback_index; /* Start from prev offset */
2460 end = -1;
2461 } else {
2462 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2463 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2464 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2465 range_whole = 1;
2466 scanned = 1;
2467 }
2468retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002469 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002470 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002471 PAGECACHE_TAG_DIRTY, min(end - index,
2472 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002473 unsigned i;
2474
2475 scanned = 1;
2476 for (i = 0; i < nr_pages; i++) {
2477 struct page *page = pvec.pages[i];
2478
2479 /*
2480 * At this point we hold neither mapping->tree_lock nor
2481 * lock on the page itself: the page may be truncated or
2482 * invalidated (changing page->mapping to NULL), or even
2483 * swizzled back from swapper_space to tmpfs file
2484 * mapping
2485 */
Chris Mason4bef0842008-09-08 11:18:08 -04002486 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2487 tree->ops->write_cache_pages_lock_hook(page);
2488 else
2489 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002490
2491 if (unlikely(page->mapping != mapping)) {
2492 unlock_page(page);
2493 continue;
2494 }
2495
2496 if (!wbc->range_cyclic && page->index > end) {
2497 done = 1;
2498 unlock_page(page);
2499 continue;
2500 }
2501
Chris Masond2c3f4f2008-11-19 12:44:22 -05002502 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002503 if (PageWriteback(page))
2504 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002505 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002506 }
Chris Masond1310b22008-01-24 16:13:08 -05002507
2508 if (PageWriteback(page) ||
2509 !clear_page_dirty_for_io(page)) {
2510 unlock_page(page);
2511 continue;
2512 }
2513
2514 ret = (*writepage)(page, wbc, data);
2515
2516 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2517 unlock_page(page);
2518 ret = 0;
2519 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002520 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002521 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002522
2523 /*
2524 * the filesystem may choose to bump up nr_to_write.
2525 * We have to make sure to honor the new nr_to_write
2526 * at any time
2527 */
2528 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002529 }
2530 pagevec_release(&pvec);
2531 cond_resched();
2532 }
2533 if (!scanned && !done) {
2534 /*
2535 * We hit the last page and there is more work to be done: wrap
2536 * back to the start of the file
2537 */
2538 scanned = 1;
2539 index = 0;
2540 goto retry;
2541 }
Chris Masond1310b22008-01-24 16:13:08 -05002542 return ret;
2543}
Chris Masond1310b22008-01-24 16:13:08 -05002544
Chris Masonffbd5172009-04-20 15:50:09 -04002545static void flush_epd_write_bio(struct extent_page_data *epd)
2546{
2547 if (epd->bio) {
2548 if (epd->sync_io)
2549 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2550 else
2551 submit_one_bio(WRITE, epd->bio, 0, 0);
2552 epd->bio = NULL;
2553 }
2554}
2555
Chris Masond2c3f4f2008-11-19 12:44:22 -05002556static noinline void flush_write_bio(void *data)
2557{
2558 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002559 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002560}
2561
Chris Masond1310b22008-01-24 16:13:08 -05002562int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2563 get_extent_t *get_extent,
2564 struct writeback_control *wbc)
2565{
2566 int ret;
2567 struct address_space *mapping = page->mapping;
2568 struct extent_page_data epd = {
2569 .bio = NULL,
2570 .tree = tree,
2571 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002572 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002573 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002574 };
2575 struct writeback_control wbc_writepages = {
2576 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002577 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002578 .older_than_this = NULL,
2579 .nr_to_write = 64,
2580 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2581 .range_end = (loff_t)-1,
2582 };
2583
Chris Masond1310b22008-01-24 16:13:08 -05002584 ret = __extent_writepage(page, wbc, &epd);
2585
Chris Mason4bef0842008-09-08 11:18:08 -04002586 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002587 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002588 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002589 return ret;
2590}
Chris Masond1310b22008-01-24 16:13:08 -05002591
Chris Mason771ed682008-11-06 22:02:51 -05002592int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2593 u64 start, u64 end, get_extent_t *get_extent,
2594 int mode)
2595{
2596 int ret = 0;
2597 struct address_space *mapping = inode->i_mapping;
2598 struct page *page;
2599 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2600 PAGE_CACHE_SHIFT;
2601
2602 struct extent_page_data epd = {
2603 .bio = NULL,
2604 .tree = tree,
2605 .get_extent = get_extent,
2606 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002607 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002608 };
2609 struct writeback_control wbc_writepages = {
2610 .bdi = inode->i_mapping->backing_dev_info,
2611 .sync_mode = mode,
2612 .older_than_this = NULL,
2613 .nr_to_write = nr_pages * 2,
2614 .range_start = start,
2615 .range_end = end + 1,
2616 };
2617
Chris Masond3977122009-01-05 21:25:51 -05002618 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002619 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2620 if (clear_page_dirty_for_io(page))
2621 ret = __extent_writepage(page, &wbc_writepages, &epd);
2622 else {
2623 if (tree->ops && tree->ops->writepage_end_io_hook)
2624 tree->ops->writepage_end_io_hook(page, start,
2625 start + PAGE_CACHE_SIZE - 1,
2626 NULL, 1);
2627 unlock_page(page);
2628 }
2629 page_cache_release(page);
2630 start += PAGE_CACHE_SIZE;
2631 }
2632
Chris Masonffbd5172009-04-20 15:50:09 -04002633 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002634 return ret;
2635}
Chris Masond1310b22008-01-24 16:13:08 -05002636
2637int extent_writepages(struct extent_io_tree *tree,
2638 struct address_space *mapping,
2639 get_extent_t *get_extent,
2640 struct writeback_control *wbc)
2641{
2642 int ret = 0;
2643 struct extent_page_data epd = {
2644 .bio = NULL,
2645 .tree = tree,
2646 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002647 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002648 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002649 };
2650
Chris Mason4bef0842008-09-08 11:18:08 -04002651 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002652 __extent_writepage, &epd,
2653 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002654 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002655 return ret;
2656}
Chris Masond1310b22008-01-24 16:13:08 -05002657
2658int extent_readpages(struct extent_io_tree *tree,
2659 struct address_space *mapping,
2660 struct list_head *pages, unsigned nr_pages,
2661 get_extent_t get_extent)
2662{
2663 struct bio *bio = NULL;
2664 unsigned page_idx;
2665 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002666 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002667
2668 pagevec_init(&pvec, 0);
2669 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2670 struct page *page = list_entry(pages->prev, struct page, lru);
2671
2672 prefetchw(&page->flags);
2673 list_del(&page->lru);
2674 /*
2675 * what we want to do here is call add_to_page_cache_lru,
2676 * but that isn't exported, so we reproduce it here
2677 */
2678 if (!add_to_page_cache(page, mapping,
2679 page->index, GFP_KERNEL)) {
2680
2681 /* open coding of lru_cache_add, also not exported */
2682 page_cache_get(page);
2683 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002684 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002685 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002686 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002687 }
2688 page_cache_release(page);
2689 }
2690 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002691 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002692 BUG_ON(!list_empty(pages));
2693 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002694 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002695 return 0;
2696}
Chris Masond1310b22008-01-24 16:13:08 -05002697
2698/*
2699 * basic invalidatepage code, this waits on any locked or writeback
2700 * ranges corresponding to the page, and then deletes any extent state
2701 * records from the tree
2702 */
2703int extent_invalidatepage(struct extent_io_tree *tree,
2704 struct page *page, unsigned long offset)
2705{
2706 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2707 u64 end = start + PAGE_CACHE_SIZE - 1;
2708 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2709
Chris Masond3977122009-01-05 21:25:51 -05002710 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002711 if (start > end)
2712 return 0;
2713
2714 lock_extent(tree, start, end, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002715 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002716 clear_extent_bit(tree, start, end,
2717 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Mason2c64c532009-09-02 15:04:12 -04002718 1, 1, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002719 return 0;
2720}
Chris Masond1310b22008-01-24 16:13:08 -05002721
2722/*
2723 * simple commit_write call, set_range_dirty is used to mark both
2724 * the pages and the extent records as dirty
2725 */
2726int extent_commit_write(struct extent_io_tree *tree,
2727 struct inode *inode, struct page *page,
2728 unsigned from, unsigned to)
2729{
2730 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2731
2732 set_page_extent_mapped(page);
2733 set_page_dirty(page);
2734
2735 if (pos > inode->i_size) {
2736 i_size_write(inode, pos);
2737 mark_inode_dirty(inode);
2738 }
2739 return 0;
2740}
Chris Masond1310b22008-01-24 16:13:08 -05002741
2742int extent_prepare_write(struct extent_io_tree *tree,
2743 struct inode *inode, struct page *page,
2744 unsigned from, unsigned to, get_extent_t *get_extent)
2745{
2746 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2747 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2748 u64 block_start;
2749 u64 orig_block_start;
2750 u64 block_end;
2751 u64 cur_end;
2752 struct extent_map *em;
2753 unsigned blocksize = 1 << inode->i_blkbits;
2754 size_t page_offset = 0;
2755 size_t block_off_start;
2756 size_t block_off_end;
2757 int err = 0;
2758 int iocount = 0;
2759 int ret = 0;
2760 int isnew;
2761
2762 set_page_extent_mapped(page);
2763
2764 block_start = (page_start + from) & ~((u64)blocksize - 1);
2765 block_end = (page_start + to - 1) | (blocksize - 1);
2766 orig_block_start = block_start;
2767
2768 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002769 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002770 em = get_extent(inode, page, page_offset, block_start,
2771 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002772 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002773 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002774
Chris Masond1310b22008-01-24 16:13:08 -05002775 cur_end = min(block_end, extent_map_end(em) - 1);
2776 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2777 block_off_end = block_off_start + blocksize;
2778 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2779
2780 if (!PageUptodate(page) && isnew &&
2781 (block_off_end > to || block_off_start < from)) {
2782 void *kaddr;
2783
2784 kaddr = kmap_atomic(page, KM_USER0);
2785 if (block_off_end > to)
2786 memset(kaddr + to, 0, block_off_end - to);
2787 if (block_off_start < from)
2788 memset(kaddr + block_off_start, 0,
2789 from - block_off_start);
2790 flush_dcache_page(page);
2791 kunmap_atomic(kaddr, KM_USER0);
2792 }
2793 if ((em->block_start != EXTENT_MAP_HOLE &&
2794 em->block_start != EXTENT_MAP_INLINE) &&
2795 !isnew && !PageUptodate(page) &&
2796 (block_off_end > to || block_off_start < from) &&
2797 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002798 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002799 u64 sector;
2800 u64 extent_offset = block_start - em->start;
2801 size_t iosize;
2802 sector = (em->block_start + extent_offset) >> 9;
2803 iosize = (cur_end - block_start + blocksize) &
2804 ~((u64)blocksize - 1);
2805 /*
2806 * we've already got the extent locked, but we
2807 * need to split the state such that our end_bio
2808 * handler can clear the lock.
2809 */
2810 set_extent_bit(tree, block_start,
2811 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002812 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002813 ret = submit_extent_page(READ, tree, page,
2814 sector, iosize, page_offset, em->bdev,
2815 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002816 end_bio_extent_preparewrite, 0,
2817 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002818 iocount++;
2819 block_start = block_start + iosize;
2820 } else {
2821 set_extent_uptodate(tree, block_start, cur_end,
2822 GFP_NOFS);
2823 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2824 block_start = cur_end + 1;
2825 }
2826 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2827 free_extent_map(em);
2828 }
2829 if (iocount) {
2830 wait_extent_bit(tree, orig_block_start,
2831 block_end, EXTENT_LOCKED);
2832 }
2833 check_page_uptodate(tree, page);
2834err:
2835 /* FIXME, zero out newly allocated blocks on error */
2836 return err;
2837}
Chris Masond1310b22008-01-24 16:13:08 -05002838
2839/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002840 * a helper for releasepage, this tests for areas of the page that
2841 * are locked or under IO and drops the related state bits if it is safe
2842 * to drop the page.
2843 */
2844int try_release_extent_state(struct extent_map_tree *map,
2845 struct extent_io_tree *tree, struct page *page,
2846 gfp_t mask)
2847{
2848 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2849 u64 end = start + PAGE_CACHE_SIZE - 1;
2850 int ret = 1;
2851
Chris Mason211f90e2008-07-18 11:56:15 -04002852 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002853 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002854 ret = 0;
2855 else {
2856 if ((mask & GFP_NOFS) == GFP_NOFS)
2857 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002858 /*
2859 * at this point we can safely clear everything except the
2860 * locked bit and the nodatasum bit
2861 */
2862 clear_extent_bit(tree, start, end,
2863 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2864 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002865 }
2866 return ret;
2867}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002868
2869/*
Chris Masond1310b22008-01-24 16:13:08 -05002870 * a helper for releasepage. As long as there are no locked extents
2871 * in the range corresponding to the page, both state records and extent
2872 * map records are removed
2873 */
2874int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002875 struct extent_io_tree *tree, struct page *page,
2876 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002877{
2878 struct extent_map *em;
2879 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2880 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002881
Chris Mason70dec802008-01-29 09:59:12 -05002882 if ((mask & __GFP_WAIT) &&
2883 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002884 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002885 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002886 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002887 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002888 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002889 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002890 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002891 break;
2892 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002893 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2894 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002895 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002896 free_extent_map(em);
2897 break;
2898 }
2899 if (!test_range_bit(tree, em->start,
2900 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002901 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002902 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002903 remove_extent_mapping(map, em);
2904 /* once for the rb tree */
2905 free_extent_map(em);
2906 }
2907 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002908 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002909
2910 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002911 free_extent_map(em);
2912 }
Chris Masond1310b22008-01-24 16:13:08 -05002913 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002914 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002915}
Chris Masond1310b22008-01-24 16:13:08 -05002916
2917sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2918 get_extent_t *get_extent)
2919{
2920 struct inode *inode = mapping->host;
2921 u64 start = iblock << inode->i_blkbits;
2922 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002923 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002924 struct extent_map *em;
2925
Yan Zhengd899e052008-10-30 14:25:28 -04002926 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2927 GFP_NOFS);
2928 em = get_extent(inode, NULL, 0, start, blksize, 0);
2929 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2930 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002931 if (!em || IS_ERR(em))
2932 return 0;
2933
Yan Zhengd899e052008-10-30 14:25:28 -04002934 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002935 goto out;
2936
2937 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002938out:
2939 free_extent_map(em);
2940 return sector;
2941}
2942
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002943int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2944 __u64 start, __u64 len, get_extent_t *get_extent)
2945{
2946 int ret;
2947 u64 off = start;
2948 u64 max = start + len;
2949 u32 flags = 0;
2950 u64 disko = 0;
2951 struct extent_map *em = NULL;
2952 int end = 0;
2953 u64 em_start = 0, em_len = 0;
2954 unsigned long emflags;
2955 ret = 0;
2956
2957 if (len == 0)
2958 return -EINVAL;
2959
2960 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2961 GFP_NOFS);
2962 em = get_extent(inode, NULL, 0, off, max - off, 0);
2963 if (!em)
2964 goto out;
2965 if (IS_ERR(em)) {
2966 ret = PTR_ERR(em);
2967 goto out;
2968 }
2969 while (!end) {
2970 off = em->start + em->len;
2971 if (off >= max)
2972 end = 1;
2973
2974 em_start = em->start;
2975 em_len = em->len;
2976
2977 disko = 0;
2978 flags = 0;
2979
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002980 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002981 end = 1;
2982 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002983 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002984 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002985 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002986 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2987 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002988 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002989 flags |= (FIEMAP_EXTENT_DELALLOC |
2990 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002991 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002992 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002993 }
2994 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2995 flags |= FIEMAP_EXTENT_ENCODED;
2996
2997 emflags = em->flags;
2998 free_extent_map(em);
2999 em = NULL;
3000
3001 if (!end) {
3002 em = get_extent(inode, NULL, 0, off, max - off, 0);
3003 if (!em)
3004 goto out;
3005 if (IS_ERR(em)) {
3006 ret = PTR_ERR(em);
3007 goto out;
3008 }
3009 emflags = em->flags;
3010 }
3011 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3012 flags |= FIEMAP_EXTENT_LAST;
3013 end = 1;
3014 }
3015
3016 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3017 em_len, flags);
3018 if (ret)
3019 goto out_free;
3020 }
3021out_free:
3022 free_extent_map(em);
3023out:
3024 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
3025 GFP_NOFS);
3026 return ret;
3027}
3028
Chris Masond1310b22008-01-24 16:13:08 -05003029static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3030 unsigned long i)
3031{
3032 struct page *p;
3033 struct address_space *mapping;
3034
3035 if (i == 0)
3036 return eb->first_page;
3037 i += eb->start >> PAGE_CACHE_SHIFT;
3038 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003039 if (!mapping)
3040 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003041
3042 /*
3043 * extent_buffer_page is only called after pinning the page
3044 * by increasing the reference count. So we know the page must
3045 * be in the radix tree.
3046 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003047 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003048 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003049 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003050
Chris Masond1310b22008-01-24 16:13:08 -05003051 return p;
3052}
3053
Chris Mason6af118ce2008-07-22 11:18:07 -04003054static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003055{
Chris Mason6af118ce2008-07-22 11:18:07 -04003056 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3057 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003058}
3059
Chris Masond1310b22008-01-24 16:13:08 -05003060static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3061 u64 start,
3062 unsigned long len,
3063 gfp_t mask)
3064{
3065 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003066#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003067 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003068#endif
Chris Masond1310b22008-01-24 16:13:08 -05003069
Chris Masond1310b22008-01-24 16:13:08 -05003070 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003071 eb->start = start;
3072 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003073 spin_lock_init(&eb->lock);
3074 init_waitqueue_head(&eb->lock_wq);
3075
Chris Mason39351272009-02-04 09:24:05 -05003076#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003077 spin_lock_irqsave(&leak_lock, flags);
3078 list_add(&eb->leak_list, &buffers);
3079 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003080#endif
Chris Masond1310b22008-01-24 16:13:08 -05003081 atomic_set(&eb->refs, 1);
3082
3083 return eb;
3084}
3085
3086static void __free_extent_buffer(struct extent_buffer *eb)
3087{
Chris Mason39351272009-02-04 09:24:05 -05003088#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003089 unsigned long flags;
3090 spin_lock_irqsave(&leak_lock, flags);
3091 list_del(&eb->leak_list);
3092 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003093#endif
Chris Masond1310b22008-01-24 16:13:08 -05003094 kmem_cache_free(extent_buffer_cache, eb);
3095}
3096
3097struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3098 u64 start, unsigned long len,
3099 struct page *page0,
3100 gfp_t mask)
3101{
3102 unsigned long num_pages = num_extent_pages(start, len);
3103 unsigned long i;
3104 unsigned long index = start >> PAGE_CACHE_SHIFT;
3105 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003106 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003107 struct page *p;
3108 struct address_space *mapping = tree->mapping;
3109 int uptodate = 1;
3110
Chris Mason6af118ce2008-07-22 11:18:07 -04003111 spin_lock(&tree->buffer_lock);
3112 eb = buffer_search(tree, start);
3113 if (eb) {
3114 atomic_inc(&eb->refs);
3115 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003116 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003117 return eb;
3118 }
3119 spin_unlock(&tree->buffer_lock);
3120
Chris Masond1310b22008-01-24 16:13:08 -05003121 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003122 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003123 return NULL;
3124
Chris Masond1310b22008-01-24 16:13:08 -05003125 if (page0) {
3126 eb->first_page = page0;
3127 i = 1;
3128 index++;
3129 page_cache_get(page0);
3130 mark_page_accessed(page0);
3131 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003132 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003133 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003134 } else {
3135 i = 0;
3136 }
3137 for (; i < num_pages; i++, index++) {
3138 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3139 if (!p) {
3140 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003141 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003142 }
3143 set_page_extent_mapped(p);
3144 mark_page_accessed(p);
3145 if (i == 0) {
3146 eb->first_page = p;
3147 set_page_extent_head(p, len);
3148 } else {
3149 set_page_private(p, EXTENT_PAGE_PRIVATE);
3150 }
3151 if (!PageUptodate(p))
3152 uptodate = 0;
3153 unlock_page(p);
3154 }
3155 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003156 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003157
Chris Mason6af118ce2008-07-22 11:18:07 -04003158 spin_lock(&tree->buffer_lock);
3159 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3160 if (exists) {
3161 /* add one reference for the caller */
3162 atomic_inc(&exists->refs);
3163 spin_unlock(&tree->buffer_lock);
3164 goto free_eb;
3165 }
3166 spin_unlock(&tree->buffer_lock);
3167
3168 /* add one reference for the tree */
3169 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003170 return eb;
3171
Chris Mason6af118ce2008-07-22 11:18:07 -04003172free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003173 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003174 return exists;
3175 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003176 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003177 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003178 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003179 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003180}
Chris Masond1310b22008-01-24 16:13:08 -05003181
3182struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3183 u64 start, unsigned long len,
3184 gfp_t mask)
3185{
Chris Masond1310b22008-01-24 16:13:08 -05003186 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003187
Chris Mason6af118ce2008-07-22 11:18:07 -04003188 spin_lock(&tree->buffer_lock);
3189 eb = buffer_search(tree, start);
3190 if (eb)
3191 atomic_inc(&eb->refs);
3192 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003193
Josef Bacik0f9dd462008-09-23 13:14:11 -04003194 if (eb)
3195 mark_page_accessed(eb->first_page);
3196
Chris Masond1310b22008-01-24 16:13:08 -05003197 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003198}
Chris Masond1310b22008-01-24 16:13:08 -05003199
3200void free_extent_buffer(struct extent_buffer *eb)
3201{
Chris Masond1310b22008-01-24 16:13:08 -05003202 if (!eb)
3203 return;
3204
3205 if (!atomic_dec_and_test(&eb->refs))
3206 return;
3207
Chris Mason6af118ce2008-07-22 11:18:07 -04003208 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003209}
Chris Masond1310b22008-01-24 16:13:08 -05003210
3211int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3212 struct extent_buffer *eb)
3213{
Chris Masond1310b22008-01-24 16:13:08 -05003214 unsigned long i;
3215 unsigned long num_pages;
3216 struct page *page;
3217
Chris Masond1310b22008-01-24 16:13:08 -05003218 num_pages = num_extent_pages(eb->start, eb->len);
3219
3220 for (i = 0; i < num_pages; i++) {
3221 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003222 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003223 continue;
3224
Chris Masona61e6f22008-07-22 11:18:08 -04003225 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003226 if (i == 0)
3227 set_page_extent_head(page, eb->len);
3228 else
3229 set_page_private(page, EXTENT_PAGE_PRIVATE);
3230
Chris Masond1310b22008-01-24 16:13:08 -05003231 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003232 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003233 if (!PageDirty(page)) {
3234 radix_tree_tag_clear(&page->mapping->page_tree,
3235 page_index(page),
3236 PAGECACHE_TAG_DIRTY);
3237 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003238 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003239 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003240 }
3241 return 0;
3242}
Chris Masond1310b22008-01-24 16:13:08 -05003243
3244int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3245 struct extent_buffer *eb)
3246{
3247 return wait_on_extent_writeback(tree, eb->start,
3248 eb->start + eb->len - 1);
3249}
Chris Masond1310b22008-01-24 16:13:08 -05003250
3251int set_extent_buffer_dirty(struct extent_io_tree *tree,
3252 struct extent_buffer *eb)
3253{
3254 unsigned long i;
3255 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003256 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003257
Chris Masonb9473432009-03-13 11:00:37 -04003258 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003259 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003260 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003261 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003262 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003263}
Chris Masond1310b22008-01-24 16:13:08 -05003264
Chris Mason1259ab72008-05-12 13:39:03 -04003265int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3266 struct extent_buffer *eb)
3267{
3268 unsigned long i;
3269 struct page *page;
3270 unsigned long num_pages;
3271
3272 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003273 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003274
3275 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3276 GFP_NOFS);
3277 for (i = 0; i < num_pages; i++) {
3278 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003279 if (page)
3280 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003281 }
3282 return 0;
3283}
3284
Chris Masond1310b22008-01-24 16:13:08 -05003285int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3286 struct extent_buffer *eb)
3287{
3288 unsigned long i;
3289 struct page *page;
3290 unsigned long num_pages;
3291
3292 num_pages = num_extent_pages(eb->start, eb->len);
3293
3294 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3295 GFP_NOFS);
3296 for (i = 0; i < num_pages; i++) {
3297 page = extent_buffer_page(eb, i);
3298 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3299 ((i == num_pages - 1) &&
3300 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3301 check_page_uptodate(tree, page);
3302 continue;
3303 }
3304 SetPageUptodate(page);
3305 }
3306 return 0;
3307}
Chris Masond1310b22008-01-24 16:13:08 -05003308
Chris Masonce9adaa2008-04-09 16:28:12 -04003309int extent_range_uptodate(struct extent_io_tree *tree,
3310 u64 start, u64 end)
3311{
3312 struct page *page;
3313 int ret;
3314 int pg_uptodate = 1;
3315 int uptodate;
3316 unsigned long index;
3317
Chris Mason9655d292009-09-02 15:22:30 -04003318 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003319 if (ret)
3320 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003321 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003322 index = start >> PAGE_CACHE_SHIFT;
3323 page = find_get_page(tree->mapping, index);
3324 uptodate = PageUptodate(page);
3325 page_cache_release(page);
3326 if (!uptodate) {
3327 pg_uptodate = 0;
3328 break;
3329 }
3330 start += PAGE_CACHE_SIZE;
3331 }
3332 return pg_uptodate;
3333}
3334
Chris Masond1310b22008-01-24 16:13:08 -05003335int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003336 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003337{
Chris Mason728131d2008-04-09 16:28:12 -04003338 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003339 unsigned long num_pages;
3340 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003341 struct page *page;
3342 int pg_uptodate = 1;
3343
Chris Masonb4ce94d2009-02-04 09:25:08 -05003344 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003345 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003346
Chris Mason42352982008-04-28 16:40:52 -04003347 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003348 EXTENT_UPTODATE, 1, NULL);
Chris Mason42352982008-04-28 16:40:52 -04003349 if (ret)
3350 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003351
3352 num_pages = num_extent_pages(eb->start, eb->len);
3353 for (i = 0; i < num_pages; i++) {
3354 page = extent_buffer_page(eb, i);
3355 if (!PageUptodate(page)) {
3356 pg_uptodate = 0;
3357 break;
3358 }
3359 }
Chris Mason42352982008-04-28 16:40:52 -04003360 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003361}
Chris Masond1310b22008-01-24 16:13:08 -05003362
3363int read_extent_buffer_pages(struct extent_io_tree *tree,
3364 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003365 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003366 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003367{
3368 unsigned long i;
3369 unsigned long start_i;
3370 struct page *page;
3371 int err;
3372 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003373 int locked_pages = 0;
3374 int all_uptodate = 1;
3375 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003376 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003377 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003378 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003379
Chris Masonb4ce94d2009-02-04 09:25:08 -05003380 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003381 return 0;
3382
Chris Masonce9adaa2008-04-09 16:28:12 -04003383 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003384 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003385 return 0;
3386 }
3387
3388 if (start) {
3389 WARN_ON(start < eb->start);
3390 start_i = (start >> PAGE_CACHE_SHIFT) -
3391 (eb->start >> PAGE_CACHE_SHIFT);
3392 } else {
3393 start_i = 0;
3394 }
3395
3396 num_pages = num_extent_pages(eb->start, eb->len);
3397 for (i = start_i; i < num_pages; i++) {
3398 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003399 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003400 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003401 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003402 } else {
3403 lock_page(page);
3404 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003405 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003406 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003407 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003408 }
3409 if (all_uptodate) {
3410 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003411 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003412 goto unlock_exit;
3413 }
3414
3415 for (i = start_i; i < num_pages; i++) {
3416 page = extent_buffer_page(eb, i);
3417 if (inc_all_pages)
3418 page_cache_get(page);
3419 if (!PageUptodate(page)) {
3420 if (start_i == 0)
3421 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003422 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003423 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003424 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003425 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003426 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003427 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003428 } else {
3429 unlock_page(page);
3430 }
3431 }
3432
Chris Masona86c12c2008-02-07 10:50:54 -05003433 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003434 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003435
Chris Masond3977122009-01-05 21:25:51 -05003436 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003437 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003438
Chris Masond1310b22008-01-24 16:13:08 -05003439 for (i = start_i; i < num_pages; i++) {
3440 page = extent_buffer_page(eb, i);
3441 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003442 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003443 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003444 }
Chris Masond3977122009-01-05 21:25:51 -05003445
Chris Masond1310b22008-01-24 16:13:08 -05003446 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003447 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003448 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003449
3450unlock_exit:
3451 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003452 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003453 page = extent_buffer_page(eb, i);
3454 i++;
3455 unlock_page(page);
3456 locked_pages--;
3457 }
3458 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003459}
Chris Masond1310b22008-01-24 16:13:08 -05003460
3461void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3462 unsigned long start,
3463 unsigned long len)
3464{
3465 size_t cur;
3466 size_t offset;
3467 struct page *page;
3468 char *kaddr;
3469 char *dst = (char *)dstv;
3470 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3471 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003472
3473 WARN_ON(start > eb->len);
3474 WARN_ON(start + len > eb->start + eb->len);
3475
3476 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3477
Chris Masond3977122009-01-05 21:25:51 -05003478 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003479 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003480
3481 cur = min(len, (PAGE_CACHE_SIZE - offset));
3482 kaddr = kmap_atomic(page, KM_USER1);
3483 memcpy(dst, kaddr + offset, cur);
3484 kunmap_atomic(kaddr, KM_USER1);
3485
3486 dst += cur;
3487 len -= cur;
3488 offset = 0;
3489 i++;
3490 }
3491}
Chris Masond1310b22008-01-24 16:13:08 -05003492
3493int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3494 unsigned long min_len, char **token, char **map,
3495 unsigned long *map_start,
3496 unsigned long *map_len, int km)
3497{
3498 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3499 char *kaddr;
3500 struct page *p;
3501 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3502 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3503 unsigned long end_i = (start_offset + start + min_len - 1) >>
3504 PAGE_CACHE_SHIFT;
3505
3506 if (i != end_i)
3507 return -EINVAL;
3508
3509 if (i == 0) {
3510 offset = start_offset;
3511 *map_start = 0;
3512 } else {
3513 offset = 0;
3514 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3515 }
Chris Masond3977122009-01-05 21:25:51 -05003516
Chris Masond1310b22008-01-24 16:13:08 -05003517 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003518 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3519 "wanted %lu %lu\n", (unsigned long long)eb->start,
3520 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003521 WARN_ON(1);
3522 }
3523
3524 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003525 kaddr = kmap_atomic(p, km);
3526 *token = kaddr;
3527 *map = kaddr + offset;
3528 *map_len = PAGE_CACHE_SIZE - offset;
3529 return 0;
3530}
Chris Masond1310b22008-01-24 16:13:08 -05003531
3532int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3533 unsigned long min_len,
3534 char **token, char **map,
3535 unsigned long *map_start,
3536 unsigned long *map_len, int km)
3537{
3538 int err;
3539 int save = 0;
3540 if (eb->map_token) {
3541 unmap_extent_buffer(eb, eb->map_token, km);
3542 eb->map_token = NULL;
3543 save = 1;
3544 }
3545 err = map_private_extent_buffer(eb, start, min_len, token, map,
3546 map_start, map_len, km);
3547 if (!err && save) {
3548 eb->map_token = *token;
3549 eb->kaddr = *map;
3550 eb->map_start = *map_start;
3551 eb->map_len = *map_len;
3552 }
3553 return err;
3554}
Chris Masond1310b22008-01-24 16:13:08 -05003555
3556void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3557{
3558 kunmap_atomic(token, km);
3559}
Chris Masond1310b22008-01-24 16:13:08 -05003560
3561int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3562 unsigned long start,
3563 unsigned long len)
3564{
3565 size_t cur;
3566 size_t offset;
3567 struct page *page;
3568 char *kaddr;
3569 char *ptr = (char *)ptrv;
3570 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3571 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3572 int ret = 0;
3573
3574 WARN_ON(start > eb->len);
3575 WARN_ON(start + len > eb->start + eb->len);
3576
3577 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3578
Chris Masond3977122009-01-05 21:25:51 -05003579 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003580 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003581
3582 cur = min(len, (PAGE_CACHE_SIZE - offset));
3583
3584 kaddr = kmap_atomic(page, KM_USER0);
3585 ret = memcmp(ptr, kaddr + offset, cur);
3586 kunmap_atomic(kaddr, KM_USER0);
3587 if (ret)
3588 break;
3589
3590 ptr += cur;
3591 len -= cur;
3592 offset = 0;
3593 i++;
3594 }
3595 return ret;
3596}
Chris Masond1310b22008-01-24 16:13:08 -05003597
3598void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3599 unsigned long start, unsigned long len)
3600{
3601 size_t cur;
3602 size_t offset;
3603 struct page *page;
3604 char *kaddr;
3605 char *src = (char *)srcv;
3606 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3607 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3608
3609 WARN_ON(start > eb->len);
3610 WARN_ON(start + len > eb->start + eb->len);
3611
3612 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3613
Chris Masond3977122009-01-05 21:25:51 -05003614 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003615 page = extent_buffer_page(eb, i);
3616 WARN_ON(!PageUptodate(page));
3617
3618 cur = min(len, PAGE_CACHE_SIZE - offset);
3619 kaddr = kmap_atomic(page, KM_USER1);
3620 memcpy(kaddr + offset, src, cur);
3621 kunmap_atomic(kaddr, KM_USER1);
3622
3623 src += cur;
3624 len -= cur;
3625 offset = 0;
3626 i++;
3627 }
3628}
Chris Masond1310b22008-01-24 16:13:08 -05003629
3630void memset_extent_buffer(struct extent_buffer *eb, char c,
3631 unsigned long start, unsigned long len)
3632{
3633 size_t cur;
3634 size_t offset;
3635 struct page *page;
3636 char *kaddr;
3637 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3638 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3639
3640 WARN_ON(start > eb->len);
3641 WARN_ON(start + len > eb->start + eb->len);
3642
3643 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3644
Chris Masond3977122009-01-05 21:25:51 -05003645 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003646 page = extent_buffer_page(eb, i);
3647 WARN_ON(!PageUptodate(page));
3648
3649 cur = min(len, PAGE_CACHE_SIZE - offset);
3650 kaddr = kmap_atomic(page, KM_USER0);
3651 memset(kaddr + offset, c, cur);
3652 kunmap_atomic(kaddr, KM_USER0);
3653
3654 len -= cur;
3655 offset = 0;
3656 i++;
3657 }
3658}
Chris Masond1310b22008-01-24 16:13:08 -05003659
3660void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3661 unsigned long dst_offset, unsigned long src_offset,
3662 unsigned long len)
3663{
3664 u64 dst_len = dst->len;
3665 size_t cur;
3666 size_t offset;
3667 struct page *page;
3668 char *kaddr;
3669 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3670 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3671
3672 WARN_ON(src->len != dst_len);
3673
3674 offset = (start_offset + dst_offset) &
3675 ((unsigned long)PAGE_CACHE_SIZE - 1);
3676
Chris Masond3977122009-01-05 21:25:51 -05003677 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003678 page = extent_buffer_page(dst, i);
3679 WARN_ON(!PageUptodate(page));
3680
3681 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3682
3683 kaddr = kmap_atomic(page, KM_USER0);
3684 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3685 kunmap_atomic(kaddr, KM_USER0);
3686
3687 src_offset += cur;
3688 len -= cur;
3689 offset = 0;
3690 i++;
3691 }
3692}
Chris Masond1310b22008-01-24 16:13:08 -05003693
3694static void move_pages(struct page *dst_page, struct page *src_page,
3695 unsigned long dst_off, unsigned long src_off,
3696 unsigned long len)
3697{
3698 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3699 if (dst_page == src_page) {
3700 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3701 } else {
3702 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3703 char *p = dst_kaddr + dst_off + len;
3704 char *s = src_kaddr + src_off + len;
3705
3706 while (len--)
3707 *--p = *--s;
3708
3709 kunmap_atomic(src_kaddr, KM_USER1);
3710 }
3711 kunmap_atomic(dst_kaddr, KM_USER0);
3712}
3713
3714static void copy_pages(struct page *dst_page, struct page *src_page,
3715 unsigned long dst_off, unsigned long src_off,
3716 unsigned long len)
3717{
3718 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3719 char *src_kaddr;
3720
3721 if (dst_page != src_page)
3722 src_kaddr = kmap_atomic(src_page, KM_USER1);
3723 else
3724 src_kaddr = dst_kaddr;
3725
3726 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3727 kunmap_atomic(dst_kaddr, KM_USER0);
3728 if (dst_page != src_page)
3729 kunmap_atomic(src_kaddr, KM_USER1);
3730}
3731
3732void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3733 unsigned long src_offset, unsigned long len)
3734{
3735 size_t cur;
3736 size_t dst_off_in_page;
3737 size_t src_off_in_page;
3738 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3739 unsigned long dst_i;
3740 unsigned long src_i;
3741
3742 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003743 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3744 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003745 BUG_ON(1);
3746 }
3747 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003748 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3749 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003750 BUG_ON(1);
3751 }
3752
Chris Masond3977122009-01-05 21:25:51 -05003753 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003754 dst_off_in_page = (start_offset + dst_offset) &
3755 ((unsigned long)PAGE_CACHE_SIZE - 1);
3756 src_off_in_page = (start_offset + src_offset) &
3757 ((unsigned long)PAGE_CACHE_SIZE - 1);
3758
3759 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3760 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3761
3762 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3763 src_off_in_page));
3764 cur = min_t(unsigned long, cur,
3765 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3766
3767 copy_pages(extent_buffer_page(dst, dst_i),
3768 extent_buffer_page(dst, src_i),
3769 dst_off_in_page, src_off_in_page, cur);
3770
3771 src_offset += cur;
3772 dst_offset += cur;
3773 len -= cur;
3774 }
3775}
Chris Masond1310b22008-01-24 16:13:08 -05003776
3777void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3778 unsigned long src_offset, unsigned long len)
3779{
3780 size_t cur;
3781 size_t dst_off_in_page;
3782 size_t src_off_in_page;
3783 unsigned long dst_end = dst_offset + len - 1;
3784 unsigned long src_end = src_offset + len - 1;
3785 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3786 unsigned long dst_i;
3787 unsigned long src_i;
3788
3789 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003790 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3791 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003792 BUG_ON(1);
3793 }
3794 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003795 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3796 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003797 BUG_ON(1);
3798 }
3799 if (dst_offset < src_offset) {
3800 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3801 return;
3802 }
Chris Masond3977122009-01-05 21:25:51 -05003803 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003804 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3805 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3806
3807 dst_off_in_page = (start_offset + dst_end) &
3808 ((unsigned long)PAGE_CACHE_SIZE - 1);
3809 src_off_in_page = (start_offset + src_end) &
3810 ((unsigned long)PAGE_CACHE_SIZE - 1);
3811
3812 cur = min_t(unsigned long, len, src_off_in_page + 1);
3813 cur = min(cur, dst_off_in_page + 1);
3814 move_pages(extent_buffer_page(dst, dst_i),
3815 extent_buffer_page(dst, src_i),
3816 dst_off_in_page - cur + 1,
3817 src_off_in_page - cur + 1, cur);
3818
3819 dst_end -= cur;
3820 src_end -= cur;
3821 len -= cur;
3822 }
3823}
Chris Mason6af118ce2008-07-22 11:18:07 -04003824
3825int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3826{
3827 u64 start = page_offset(page);
3828 struct extent_buffer *eb;
3829 int ret = 1;
3830 unsigned long i;
3831 unsigned long num_pages;
3832
3833 spin_lock(&tree->buffer_lock);
3834 eb = buffer_search(tree, start);
3835 if (!eb)
3836 goto out;
3837
3838 if (atomic_read(&eb->refs) > 1) {
3839 ret = 0;
3840 goto out;
3841 }
Chris Masonb9473432009-03-13 11:00:37 -04003842 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3843 ret = 0;
3844 goto out;
3845 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003846 /* at this point we can safely release the extent buffer */
3847 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003848 for (i = 0; i < num_pages; i++)
3849 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003850 rb_erase(&eb->rb_node, &tree->buffer);
3851 __free_extent_buffer(eb);
3852out:
3853 spin_unlock(&tree->buffer_lock);
3854 return ret;
3855}