]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/extent_io.c
Btrfs: Start btree concurrency work.
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / extent_io.c
CommitLineData
d1310b2e
CM
1#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>
12#include <linux/version.h>
13#include <linux/writeback.h>
14#include <linux/pagevec.h>
15#include "extent_io.h"
16#include "extent_map.h"
17
18/* temporary define until extent_map moves out of btrfs */
19struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
20 unsigned long extra_flags,
21 void (*ctor)(void *, struct kmem_cache *,
22 unsigned long));
23
24static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
27static LIST_HEAD(buffers);
28static LIST_HEAD(states);
2d2ae547 29static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
d1310b2e 30
d1310b2e
CM
31#define BUFFER_LRU_MAX 64
32
33struct tree_entry {
34 u64 start;
35 u64 end;
d1310b2e
CM
36 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;
43};
44
45int __init extent_io_init(void)
46{
47 extent_state_cache = btrfs_cache_create("extent_state",
48 sizeof(struct extent_state), 0,
49 NULL);
50 if (!extent_state_cache)
51 return -ENOMEM;
52
53 extent_buffer_cache = btrfs_cache_create("extent_buffers",
54 sizeof(struct extent_buffer), 0,
55 NULL);
56 if (!extent_buffer_cache)
57 goto free_state_cache;
58 return 0;
59
60free_state_cache:
61 kmem_cache_destroy(extent_state_cache);
62 return -ENOMEM;
63}
64
65void extent_io_exit(void)
66{
67 struct extent_state *state;
2d2ae547 68 struct extent_buffer *eb;
d1310b2e
CM
69
70 while (!list_empty(&states)) {
2d2ae547 71 state = list_entry(states.next, struct extent_state, leak_list);
70dec807 72 printk("state leak: start %Lu end %Lu state %lu in tree %p refs %d\n", state->start, state->end, state->state, state->tree, atomic_read(&state->refs));
2d2ae547 73 list_del(&state->leak_list);
d1310b2e
CM
74 kmem_cache_free(extent_state_cache, state);
75
76 }
77
2d2ae547
CM
78 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
80 printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs));
81 list_del(&eb->leak_list);
82 kmem_cache_free(extent_buffer_cache, eb);
83 }
d1310b2e
CM
84 if (extent_state_cache)
85 kmem_cache_destroy(extent_state_cache);
86 if (extent_buffer_cache)
87 kmem_cache_destroy(extent_buffer_cache);
88}
89
90void extent_io_tree_init(struct extent_io_tree *tree,
91 struct address_space *mapping, gfp_t mask)
92{
93 tree->state.rb_node = NULL;
94 tree->ops = NULL;
95 tree->dirty_bytes = 0;
70dec807 96 spin_lock_init(&tree->lock);
d1310b2e
CM
97 spin_lock_init(&tree->lru_lock);
98 tree->mapping = mapping;
99 INIT_LIST_HEAD(&tree->buffer_lru);
100 tree->lru_size = 0;
80ea96b1 101 tree->last = NULL;
d1310b2e
CM
102}
103EXPORT_SYMBOL(extent_io_tree_init);
104
105void extent_io_tree_empty_lru(struct extent_io_tree *tree)
106{
107 struct extent_buffer *eb;
108 while(!list_empty(&tree->buffer_lru)) {
109 eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
110 lru);
111 list_del_init(&eb->lru);
112 free_extent_buffer(eb);
113 }
114}
115EXPORT_SYMBOL(extent_io_tree_empty_lru);
116
117struct extent_state *alloc_extent_state(gfp_t mask)
118{
119 struct extent_state *state;
2d2ae547 120 unsigned long flags;
d1310b2e
CM
121
122 state = kmem_cache_alloc(extent_state_cache, mask);
2b114d1d 123 if (!state)
d1310b2e
CM
124 return state;
125 state->state = 0;
d1310b2e 126 state->private = 0;
70dec807 127 state->tree = NULL;
2d2ae547
CM
128 spin_lock_irqsave(&leak_lock, flags);
129 list_add(&state->leak_list, &states);
130 spin_unlock_irqrestore(&leak_lock, flags);
d1310b2e
CM
131
132 atomic_set(&state->refs, 1);
133 init_waitqueue_head(&state->wq);
134 return state;
135}
136EXPORT_SYMBOL(alloc_extent_state);
137
138void free_extent_state(struct extent_state *state)
139{
d1310b2e
CM
140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
2d2ae547 143 unsigned long flags;
70dec807 144 WARN_ON(state->tree);
2d2ae547
CM
145 spin_lock_irqsave(&leak_lock, flags);
146 list_del(&state->leak_list);
147 spin_unlock_irqrestore(&leak_lock, flags);
d1310b2e
CM
148 kmem_cache_free(extent_state_cache, state);
149 }
150}
151EXPORT_SYMBOL(free_extent_state);
152
153static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
154 struct rb_node *node)
155{
156 struct rb_node ** p = &root->rb_node;
157 struct rb_node * parent = NULL;
158 struct tree_entry *entry;
159
160 while(*p) {
161 parent = *p;
162 entry = rb_entry(parent, struct tree_entry, rb_node);
163
164 if (offset < entry->start)
165 p = &(*p)->rb_left;
166 else if (offset > entry->end)
167 p = &(*p)->rb_right;
168 else
169 return parent;
170 }
171
172 entry = rb_entry(node, struct tree_entry, rb_node);
d1310b2e
CM
173 rb_link_node(node, parent, p);
174 rb_insert_color(node, root);
175 return NULL;
176}
177
80ea96b1 178static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
d1310b2e
CM
179 struct rb_node **prev_ret,
180 struct rb_node **next_ret)
181{
80ea96b1 182 struct rb_root *root = &tree->state;
d1310b2e
CM
183 struct rb_node * n = root->rb_node;
184 struct rb_node *prev = NULL;
185 struct rb_node *orig_prev = NULL;
186 struct tree_entry *entry;
187 struct tree_entry *prev_entry = NULL;
188
80ea96b1
CM
189 if (tree->last) {
190 struct extent_state *state;
191 state = tree->last;
192 if (state->start <= offset && offset <= state->end)
193 return &tree->last->rb_node;
194 }
d1310b2e
CM
195 while(n) {
196 entry = rb_entry(n, struct tree_entry, rb_node);
197 prev = n;
198 prev_entry = entry;
199
200 if (offset < entry->start)
201 n = n->rb_left;
202 else if (offset > entry->end)
203 n = n->rb_right;
80ea96b1
CM
204 else {
205 tree->last = rb_entry(n, struct extent_state, rb_node);
d1310b2e 206 return n;
80ea96b1 207 }
d1310b2e
CM
208 }
209
210 if (prev_ret) {
211 orig_prev = prev;
212 while(prev && offset > prev_entry->end) {
213 prev = rb_next(prev);
214 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
215 }
216 *prev_ret = prev;
217 prev = orig_prev;
218 }
219
220 if (next_ret) {
221 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
222 while(prev && offset < prev_entry->start) {
223 prev = rb_prev(prev);
224 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
225 }
226 *next_ret = prev;
227 }
228 return NULL;
229}
230
80ea96b1
CM
231static inline struct rb_node *tree_search(struct extent_io_tree *tree,
232 u64 offset)
d1310b2e 233{
70dec807 234 struct rb_node *prev = NULL;
d1310b2e 235 struct rb_node *ret;
70dec807 236
80ea96b1
CM
237 ret = __etree_search(tree, offset, &prev, NULL);
238 if (!ret) {
239 if (prev) {
240 tree->last = rb_entry(prev, struct extent_state,
241 rb_node);
242 }
d1310b2e 243 return prev;
80ea96b1 244 }
d1310b2e
CM
245 return ret;
246}
247
248/*
249 * utility function to look for merge candidates inside a given range.
250 * Any extents with matching state are merged together into a single
251 * extent in the tree. Extents with EXTENT_IO in their state field
252 * are not merged because the end_io handlers need to be able to do
253 * operations on them without sleeping (or doing allocations/splits).
254 *
255 * This should be called with the tree lock held.
256 */
257static int merge_state(struct extent_io_tree *tree,
258 struct extent_state *state)
259{
260 struct extent_state *other;
261 struct rb_node *other_node;
262
263 if (state->state & EXTENT_IOBITS)
264 return 0;
265
266 other_node = rb_prev(&state->rb_node);
267 if (other_node) {
268 other = rb_entry(other_node, struct extent_state, rb_node);
269 if (other->end == state->start - 1 &&
270 other->state == state->state) {
271 state->start = other->start;
70dec807 272 other->tree = NULL;
80ea96b1 273 if (tree->last == other)
d7fc640e 274 tree->last = state;
d1310b2e
CM
275 rb_erase(&other->rb_node, &tree->state);
276 free_extent_state(other);
277 }
278 }
279 other_node = rb_next(&state->rb_node);
280 if (other_node) {
281 other = rb_entry(other_node, struct extent_state, rb_node);
282 if (other->start == state->end + 1 &&
283 other->state == state->state) {
284 other->start = state->start;
70dec807 285 state->tree = NULL;
80ea96b1 286 if (tree->last == state)
d7fc640e 287 tree->last = other;
d1310b2e
CM
288 rb_erase(&state->rb_node, &tree->state);
289 free_extent_state(state);
290 }
291 }
292 return 0;
293}
294
291d673e
CM
295static void set_state_cb(struct extent_io_tree *tree,
296 struct extent_state *state,
297 unsigned long bits)
298{
299 if (tree->ops && tree->ops->set_bit_hook) {
300 tree->ops->set_bit_hook(tree->mapping->host, state->start,
b0c68f8b 301 state->end, state->state, bits);
291d673e
CM
302 }
303}
304
305static void clear_state_cb(struct extent_io_tree *tree,
306 struct extent_state *state,
307 unsigned long bits)
308{
309 if (tree->ops && tree->ops->set_bit_hook) {
310 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
b0c68f8b 311 state->end, state->state, bits);
291d673e
CM
312 }
313}
314
d1310b2e
CM
315/*
316 * insert an extent_state struct into the tree. 'bits' are set on the
317 * struct before it is inserted.
318 *
319 * This may return -EEXIST if the extent is already there, in which case the
320 * state struct is freed.
321 *
322 * The tree lock is not taken internally. This is a utility function and
323 * probably isn't what you want to call (see set/clear_extent_bit).
324 */
325static int insert_state(struct extent_io_tree *tree,
326 struct extent_state *state, u64 start, u64 end,
327 int bits)
328{
329 struct rb_node *node;
330
331 if (end < start) {
332 printk("end < start %Lu %Lu\n", end, start);
333 WARN_ON(1);
334 }
335 if (bits & EXTENT_DIRTY)
336 tree->dirty_bytes += end - start + 1;
b0c68f8b 337 set_state_cb(tree, state, bits);
d1310b2e
CM
338 state->state |= bits;
339 state->start = start;
340 state->end = end;
341 node = tree_insert(&tree->state, end, &state->rb_node);
342 if (node) {
343 struct extent_state *found;
344 found = rb_entry(node, struct extent_state, rb_node);
345 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
346 free_extent_state(state);
347 return -EEXIST;
348 }
70dec807 349 state->tree = tree;
80ea96b1 350 tree->last = state;
d1310b2e
CM
351 merge_state(tree, state);
352 return 0;
353}
354
355/*
356 * split a given extent state struct in two, inserting the preallocated
357 * struct 'prealloc' as the newly created second half. 'split' indicates an
358 * offset inside 'orig' where it should be split.
359 *
360 * Before calling,
361 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
362 * are two extent state structs in the tree:
363 * prealloc: [orig->start, split - 1]
364 * orig: [ split, orig->end ]
365 *
366 * The tree locks are not taken by this function. They need to be held
367 * by the caller.
368 */
369static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
370 struct extent_state *prealloc, u64 split)
371{
372 struct rb_node *node;
373 prealloc->start = orig->start;
374 prealloc->end = split - 1;
375 prealloc->state = orig->state;
376 orig->start = split;
377
378 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
379 if (node) {
380 struct extent_state *found;
381 found = rb_entry(node, struct extent_state, rb_node);
382 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
383 free_extent_state(prealloc);
384 return -EEXIST;
385 }
70dec807 386 prealloc->tree = tree;
d1310b2e
CM
387 return 0;
388}
389
390/*
391 * utility function to clear some bits in an extent state struct.
392 * it will optionally wake up any one waiting on this state (wake == 1), or
393 * forcibly remove the state from the tree (delete == 1).
394 *
395 * If no bits are set on the state struct after clearing things, the
396 * struct is freed and removed from the tree
397 */
398static int clear_state_bit(struct extent_io_tree *tree,
399 struct extent_state *state, int bits, int wake,
400 int delete)
401{
402 int ret = state->state & bits;
403
404 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
405 u64 range = state->end - state->start + 1;
406 WARN_ON(range > tree->dirty_bytes);
407 tree->dirty_bytes -= range;
408 }
291d673e 409 clear_state_cb(tree, state, bits);
b0c68f8b 410 state->state &= ~bits;
d1310b2e
CM
411 if (wake)
412 wake_up(&state->wq);
413 if (delete || state->state == 0) {
70dec807 414 if (state->tree) {
ae9d1285 415 clear_state_cb(tree, state, state->state);
d7fc640e
CM
416 if (tree->last == state) {
417 tree->last = extent_state_next(state);
418 }
d1310b2e 419 rb_erase(&state->rb_node, &tree->state);
70dec807 420 state->tree = NULL;
d1310b2e
CM
421 free_extent_state(state);
422 } else {
423 WARN_ON(1);
424 }
425 } else {
426 merge_state(tree, state);
427 }
428 return ret;
429}
430
431/*
432 * clear some bits on a range in the tree. This may require splitting
433 * or inserting elements in the tree, so the gfp mask is used to
434 * indicate which allocations or sleeping are allowed.
435 *
436 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
437 * the given range from the tree regardless of state (ie for truncate).
438 *
439 * the range [start, end] is inclusive.
440 *
441 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
442 * bits were already set, or zero if none of the bits were already set.
443 */
444int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
445 int bits, int wake, int delete, gfp_t mask)
446{
447 struct extent_state *state;
448 struct extent_state *prealloc = NULL;
449 struct rb_node *node;
450 unsigned long flags;
451 int err;
452 int set = 0;
453
454again:
455 if (!prealloc && (mask & __GFP_WAIT)) {
456 prealloc = alloc_extent_state(mask);
457 if (!prealloc)
458 return -ENOMEM;
459 }
460
70dec807 461 spin_lock_irqsave(&tree->lock, flags);
d1310b2e
CM
462 /*
463 * this search will find the extents that end after
464 * our range starts
465 */
80ea96b1 466 node = tree_search(tree, start);
d1310b2e
CM
467 if (!node)
468 goto out;
469 state = rb_entry(node, struct extent_state, rb_node);
470 if (state->start > end)
471 goto out;
472 WARN_ON(state->end < start);
473
474 /*
475 * | ---- desired range ---- |
476 * | state | or
477 * | ------------- state -------------- |
478 *
479 * We need to split the extent we found, and may flip
480 * bits on second half.
481 *
482 * If the extent we found extends past our range, we
483 * just split and search again. It'll get split again
484 * the next time though.
485 *
486 * If the extent we found is inside our range, we clear
487 * the desired bit on it.
488 */
489
490 if (state->start < start) {
70dec807
CM
491 if (!prealloc)
492 prealloc = alloc_extent_state(GFP_ATOMIC);
d1310b2e
CM
493 err = split_state(tree, state, prealloc, start);
494 BUG_ON(err == -EEXIST);
495 prealloc = NULL;
496 if (err)
497 goto out;
498 if (state->end <= end) {
499 start = state->end + 1;
500 set |= clear_state_bit(tree, state, bits,
501 wake, delete);
502 } else {
503 start = state->start;
504 }
505 goto search_again;
506 }
507 /*
508 * | ---- desired range ---- |
509 * | state |
510 * We need to split the extent, and clear the bit
511 * on the first half
512 */
513 if (state->start <= end && state->end > end) {
70dec807
CM
514 if (!prealloc)
515 prealloc = alloc_extent_state(GFP_ATOMIC);
d1310b2e
CM
516 err = split_state(tree, state, prealloc, end + 1);
517 BUG_ON(err == -EEXIST);
518
519 if (wake)
520 wake_up(&state->wq);
521 set |= clear_state_bit(tree, prealloc, bits,
522 wake, delete);
523 prealloc = NULL;
524 goto out;
525 }
526
527 start = state->end + 1;
528 set |= clear_state_bit(tree, state, bits, wake, delete);
529 goto search_again;
530
531out:
70dec807 532 spin_unlock_irqrestore(&tree->lock, flags);
d1310b2e
CM
533 if (prealloc)
534 free_extent_state(prealloc);
535
536 return set;
537
538search_again:
539 if (start > end)
540 goto out;
70dec807 541 spin_unlock_irqrestore(&tree->lock, flags);
d1310b2e
CM
542 if (mask & __GFP_WAIT)
543 cond_resched();
544 goto again;
545}
546EXPORT_SYMBOL(clear_extent_bit);
547
548static int wait_on_state(struct extent_io_tree *tree,
549 struct extent_state *state)
550{
551 DEFINE_WAIT(wait);
552 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
70dec807 553 spin_unlock_irq(&tree->lock);
d1310b2e 554 schedule();
70dec807 555 spin_lock_irq(&tree->lock);
d1310b2e
CM
556 finish_wait(&state->wq, &wait);
557 return 0;
558}
559
560/*
561 * waits for one or more bits to clear on a range in the state tree.
562 * The range [start, end] is inclusive.
563 * The tree lock is taken by this function
564 */
565int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
566{
567 struct extent_state *state;
568 struct rb_node *node;
569
70dec807 570 spin_lock_irq(&tree->lock);
d1310b2e
CM
571again:
572 while (1) {
573 /*
574 * this search will find all the extents that end after
575 * our range starts
576 */
80ea96b1 577 node = tree_search(tree, start);
d1310b2e
CM
578 if (!node)
579 break;
580
581 state = rb_entry(node, struct extent_state, rb_node);
582
583 if (state->start > end)
584 goto out;
585
586 if (state->state & bits) {
587 start = state->start;
588 atomic_inc(&state->refs);
589 wait_on_state(tree, state);
590 free_extent_state(state);
591 goto again;
592 }
593 start = state->end + 1;
594
595 if (start > end)
596 break;
597
598 if (need_resched()) {
70dec807 599 spin_unlock_irq(&tree->lock);
d1310b2e 600 cond_resched();
70dec807 601 spin_lock_irq(&tree->lock);
d1310b2e
CM
602 }
603 }
604out:
70dec807 605 spin_unlock_irq(&tree->lock);
d1310b2e
CM
606 return 0;
607}
608EXPORT_SYMBOL(wait_extent_bit);
609
610static void set_state_bits(struct extent_io_tree *tree,
611 struct extent_state *state,
612 int bits)
613{
614 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
615 u64 range = state->end - state->start + 1;
616 tree->dirty_bytes += range;
617 }
291d673e 618 set_state_cb(tree, state, bits);
b0c68f8b 619 state->state |= bits;
d1310b2e
CM
620}
621
622/*
623 * set some bits on a range in the tree. This may require allocations
624 * or sleeping, so the gfp mask is used to indicate what is allowed.
625 *
626 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
627 * range already has the desired bits set. The start of the existing
628 * range is returned in failed_start in this case.
629 *
630 * [start, end] is inclusive
631 * This takes the tree lock.
632 */
633int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
634 int exclusive, u64 *failed_start, gfp_t mask)
635{
636 struct extent_state *state;
637 struct extent_state *prealloc = NULL;
638 struct rb_node *node;
639 unsigned long flags;
640 int err = 0;
641 int set;
642 u64 last_start;
643 u64 last_end;
644again:
645 if (!prealloc && (mask & __GFP_WAIT)) {
646 prealloc = alloc_extent_state(mask);
647 if (!prealloc)
648 return -ENOMEM;
649 }
650
70dec807 651 spin_lock_irqsave(&tree->lock, flags);
d1310b2e
CM
652 /*
653 * this search will find all the extents that end after
654 * our range starts.
655 */
80ea96b1 656 node = tree_search(tree, start);
d1310b2e
CM
657 if (!node) {
658 err = insert_state(tree, prealloc, start, end, bits);
659 prealloc = NULL;
660 BUG_ON(err == -EEXIST);
661 goto out;
662 }
663
664 state = rb_entry(node, struct extent_state, rb_node);
665 last_start = state->start;
666 last_end = state->end;
667
668 /*
669 * | ---- desired range ---- |
670 * | state |
671 *
672 * Just lock what we found and keep going
673 */
674 if (state->start == start && state->end <= end) {
675 set = state->state & bits;
676 if (set && exclusive) {
677 *failed_start = state->start;
678 err = -EEXIST;
679 goto out;
680 }
681 set_state_bits(tree, state, bits);
682 start = state->end + 1;
683 merge_state(tree, state);
684 goto search_again;
685 }
686
687 /*
688 * | ---- desired range ---- |
689 * | state |
690 * or
691 * | ------------- state -------------- |
692 *
693 * We need to split the extent we found, and may flip bits on
694 * second half.
695 *
696 * If the extent we found extends past our
697 * range, we just split and search again. It'll get split
698 * again the next time though.
699 *
700 * If the extent we found is inside our range, we set the
701 * desired bit on it.
702 */
703 if (state->start < start) {
704 set = state->state & bits;
705 if (exclusive && set) {
706 *failed_start = start;
707 err = -EEXIST;
708 goto out;
709 }
710 err = split_state(tree, state, prealloc, start);
711 BUG_ON(err == -EEXIST);
712 prealloc = NULL;
713 if (err)
714 goto out;
715 if (state->end <= end) {
716 set_state_bits(tree, state, bits);
717 start = state->end + 1;
718 merge_state(tree, state);
719 } else {
720 start = state->start;
721 }
722 goto search_again;
723 }
724 /*
725 * | ---- desired range ---- |
726 * | state | or | state |
727 *
728 * There's a hole, we need to insert something in it and
729 * ignore the extent we found.
730 */
731 if (state->start > start) {
732 u64 this_end;
733 if (end < last_start)
734 this_end = end;
735 else
736 this_end = last_start -1;
737 err = insert_state(tree, prealloc, start, this_end,
738 bits);
739 prealloc = NULL;
740 BUG_ON(err == -EEXIST);
741 if (err)
742 goto out;
743 start = this_end + 1;
744 goto search_again;
745 }
746 /*
747 * | ---- desired range ---- |
748 * | state |
749 * We need to split the extent, and set the bit
750 * on the first half
751 */
752 if (state->start <= end && state->end > end) {
753 set = state->state & bits;
754 if (exclusive && set) {
755 *failed_start = start;
756 err = -EEXIST;
757 goto out;
758 }
759 err = split_state(tree, state, prealloc, end + 1);
760 BUG_ON(err == -EEXIST);
761
762 set_state_bits(tree, prealloc, bits);
763 merge_state(tree, prealloc);
764 prealloc = NULL;
765 goto out;
766 }
767
768 goto search_again;
769
770out:
70dec807 771 spin_unlock_irqrestore(&tree->lock, flags);
d1310b2e
CM
772 if (prealloc)
773 free_extent_state(prealloc);
774
775 return err;
776
777search_again:
778 if (start > end)
779 goto out;
70dec807 780 spin_unlock_irqrestore(&tree->lock, flags);
d1310b2e
CM
781 if (mask & __GFP_WAIT)
782 cond_resched();
783 goto again;
784}
785EXPORT_SYMBOL(set_extent_bit);
786
787/* wrappers around set/clear extent bit */
788int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
789 gfp_t mask)
790{
791 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
792 mask);
793}
794EXPORT_SYMBOL(set_extent_dirty);
795
796int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
797 int bits, gfp_t mask)
798{
799 return set_extent_bit(tree, start, end, bits, 0, NULL,
800 mask);
801}
802EXPORT_SYMBOL(set_extent_bits);
803
804int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
805 int bits, gfp_t mask)
806{
807 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
808}
809EXPORT_SYMBOL(clear_extent_bits);
810
811int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
812 gfp_t mask)
813{
814 return set_extent_bit(tree, start, end,
815 EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
816 mask);
817}
818EXPORT_SYMBOL(set_extent_delalloc);
819
820int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
821 gfp_t mask)
822{
823 return clear_extent_bit(tree, start, end,
824 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
825}
826EXPORT_SYMBOL(clear_extent_dirty);
827
828int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
829 gfp_t mask)
830{
831 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
832 mask);
833}
834EXPORT_SYMBOL(set_extent_new);
835
836int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
837 gfp_t mask)
838{
839 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
840}
841EXPORT_SYMBOL(clear_extent_new);
842
843int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
844 gfp_t mask)
845{
846 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
847 mask);
848}
849EXPORT_SYMBOL(set_extent_uptodate);
850
851int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
852 gfp_t mask)
853{
854 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
855}
856EXPORT_SYMBOL(clear_extent_uptodate);
857
858int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
859 gfp_t mask)
860{
861 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
862 0, NULL, mask);
863}
864EXPORT_SYMBOL(set_extent_writeback);
865
866int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
867 gfp_t mask)
868{
869 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
870}
871EXPORT_SYMBOL(clear_extent_writeback);
872
873int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
874{
875 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
876}
877EXPORT_SYMBOL(wait_on_extent_writeback);
878
d1310b2e
CM
879int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
880{
881 int err;
882 u64 failed_start;
883 while (1) {
884 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
885 &failed_start, mask);
886 if (err == -EEXIST && (mask & __GFP_WAIT)) {
887 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
888 start = failed_start;
889 } else {
890 break;
891 }
892 WARN_ON(start > end);
893 }
894 return err;
895}
896EXPORT_SYMBOL(lock_extent);
897
898int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
899 gfp_t mask)
900{
901 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
902}
903EXPORT_SYMBOL(unlock_extent);
904
905/*
906 * helper function to set pages and extents in the tree dirty
907 */
908int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
909{
910 unsigned long index = start >> PAGE_CACHE_SHIFT;
911 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
912 struct page *page;
913
914 while (index <= end_index) {
915 page = find_get_page(tree->mapping, index);
916 BUG_ON(!page);
917 __set_page_dirty_nobuffers(page);
918 page_cache_release(page);
919 index++;
920 }
921 set_extent_dirty(tree, start, end, GFP_NOFS);
922 return 0;
923}
924EXPORT_SYMBOL(set_range_dirty);
925
926/*
927 * helper function to set both pages and extents in the tree writeback
928 */
929int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
930{
931 unsigned long index = start >> PAGE_CACHE_SHIFT;
932 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
933 struct page *page;
934
935 while (index <= end_index) {
936 page = find_get_page(tree->mapping, index);
937 BUG_ON(!page);
938 set_page_writeback(page);
939 page_cache_release(page);
940 index++;
941 }
942 set_extent_writeback(tree, start, end, GFP_NOFS);
943 return 0;
944}
945EXPORT_SYMBOL(set_range_writeback);
946
947int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
948 u64 *start_ret, u64 *end_ret, int bits)
949{
950 struct rb_node *node;
951 struct extent_state *state;
952 int ret = 1;
953
70dec807 954 spin_lock_irq(&tree->lock);
d1310b2e
CM
955 /*
956 * this search will find all the extents that end after
957 * our range starts.
958 */
80ea96b1 959 node = tree_search(tree, start);
2b114d1d 960 if (!node) {
d1310b2e
CM
961 goto out;
962 }
963
964 while(1) {
965 state = rb_entry(node, struct extent_state, rb_node);
966 if (state->end >= start && (state->state & bits)) {
967 *start_ret = state->start;
968 *end_ret = state->end;
969 ret = 0;
970 break;
971 }
972 node = rb_next(node);
973 if (!node)
974 break;
975 }
976out:
70dec807 977 spin_unlock_irq(&tree->lock);
d1310b2e
CM
978 return ret;
979}
980EXPORT_SYMBOL(find_first_extent_bit);
981
d7fc640e
CM
982struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
983 u64 start, int bits)
984{
985 struct rb_node *node;
986 struct extent_state *state;
987
988 /*
989 * this search will find all the extents that end after
990 * our range starts.
991 */
992 node = tree_search(tree, start);
2b114d1d 993 if (!node) {
d7fc640e
CM
994 goto out;
995 }
996
997 while(1) {
998 state = rb_entry(node, struct extent_state, rb_node);
999 if (state->end >= start && (state->state & bits)) {
1000 return state;
1001 }
1002 node = rb_next(node);
1003 if (!node)
1004 break;
1005 }
1006out:
1007 return NULL;
1008}
1009EXPORT_SYMBOL(find_first_extent_bit_state);
1010
d1310b2e
CM
1011u64 find_lock_delalloc_range(struct extent_io_tree *tree,
1012 u64 *start, u64 *end, u64 max_bytes)
1013{
1014 struct rb_node *node;
1015 struct extent_state *state;
1016 u64 cur_start = *start;
1017 u64 found = 0;
1018 u64 total_bytes = 0;
1019
70dec807 1020 spin_lock_irq(&tree->lock);
d1310b2e
CM
1021 /*
1022 * this search will find all the extents that end after
1023 * our range starts.
1024 */
1025search_again:
80ea96b1 1026 node = tree_search(tree, cur_start);
2b114d1d 1027 if (!node) {
3b951516
CM
1028 if (!found)
1029 *end = (u64)-1;
d1310b2e
CM
1030 goto out;
1031 }
1032
1033 while(1) {
1034 state = rb_entry(node, struct extent_state, rb_node);
1035 if (found && state->start != cur_start) {
1036 goto out;
1037 }
1038 if (!(state->state & EXTENT_DELALLOC)) {
1039 if (!found)
1040 *end = state->end;
1041 goto out;
1042 }
1043 if (!found) {
1044 struct extent_state *prev_state;
1045 struct rb_node *prev_node = node;
1046 while(1) {
1047 prev_node = rb_prev(prev_node);
1048 if (!prev_node)
1049 break;
1050 prev_state = rb_entry(prev_node,
1051 struct extent_state,
1052 rb_node);
1053 if (!(prev_state->state & EXTENT_DELALLOC))
1054 break;
1055 state = prev_state;
1056 node = prev_node;
1057 }
1058 }
1059 if (state->state & EXTENT_LOCKED) {
1060 DEFINE_WAIT(wait);
1061 atomic_inc(&state->refs);
1062 prepare_to_wait(&state->wq, &wait,
1063 TASK_UNINTERRUPTIBLE);
70dec807 1064 spin_unlock_irq(&tree->lock);
d1310b2e 1065 schedule();
70dec807 1066 spin_lock_irq(&tree->lock);
d1310b2e
CM
1067 finish_wait(&state->wq, &wait);
1068 free_extent_state(state);
1069 goto search_again;
1070 }
291d673e 1071 set_state_cb(tree, state, EXTENT_LOCKED);
b0c68f8b 1072 state->state |= EXTENT_LOCKED;
d1310b2e
CM
1073 if (!found)
1074 *start = state->start;
1075 found++;
1076 *end = state->end;
1077 cur_start = state->end + 1;
1078 node = rb_next(node);
1079 if (!node)
1080 break;
1081 total_bytes += state->end - state->start + 1;
1082 if (total_bytes >= max_bytes)
1083 break;
1084 }
1085out:
70dec807 1086 spin_unlock_irq(&tree->lock);
d1310b2e
CM
1087 return found;
1088}
1089
1090u64 count_range_bits(struct extent_io_tree *tree,
1091 u64 *start, u64 search_end, u64 max_bytes,
1092 unsigned long bits)
1093{
1094 struct rb_node *node;
1095 struct extent_state *state;
1096 u64 cur_start = *start;
1097 u64 total_bytes = 0;
1098 int found = 0;
1099
1100 if (search_end <= cur_start) {
1101 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1102 WARN_ON(1);
1103 return 0;
1104 }
1105
70dec807 1106 spin_lock_irq(&tree->lock);
d1310b2e
CM
1107 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1108 total_bytes = tree->dirty_bytes;
1109 goto out;
1110 }
1111 /*
1112 * this search will find all the extents that end after
1113 * our range starts.
1114 */
80ea96b1 1115 node = tree_search(tree, cur_start);
2b114d1d 1116 if (!node) {
d1310b2e
CM
1117 goto out;
1118 }
1119
1120 while(1) {
1121 state = rb_entry(node, struct extent_state, rb_node);
1122 if (state->start > search_end)
1123 break;
1124 if (state->end >= cur_start && (state->state & bits)) {
1125 total_bytes += min(search_end, state->end) + 1 -
1126 max(cur_start, state->start);
1127 if (total_bytes >= max_bytes)
1128 break;
1129 if (!found) {
1130 *start = state->start;
1131 found = 1;
1132 }
1133 }
1134 node = rb_next(node);
1135 if (!node)
1136 break;
1137 }
1138out:
70dec807 1139 spin_unlock_irq(&tree->lock);
d1310b2e
CM
1140 return total_bytes;
1141}
1142/*
1143 * helper function to lock both pages and extents in the tree.
1144 * pages must be locked first.
1145 */
1146int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1147{
1148 unsigned long index = start >> PAGE_CACHE_SHIFT;
1149 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1150 struct page *page;
1151 int err;
1152
1153 while (index <= end_index) {
1154 page = grab_cache_page(tree->mapping, index);
1155 if (!page) {
1156 err = -ENOMEM;
1157 goto failed;
1158 }
1159 if (IS_ERR(page)) {
1160 err = PTR_ERR(page);
1161 goto failed;
1162 }
1163 index++;
1164 }
1165 lock_extent(tree, start, end, GFP_NOFS);
1166 return 0;
1167
1168failed:
1169 /*
1170 * we failed above in getting the page at 'index', so we undo here
1171 * up to but not including the page at 'index'
1172 */
1173 end_index = index;
1174 index = start >> PAGE_CACHE_SHIFT;
1175 while (index < end_index) {
1176 page = find_get_page(tree->mapping, index);
1177 unlock_page(page);
1178 page_cache_release(page);
1179 index++;
1180 }
1181 return err;
1182}
1183EXPORT_SYMBOL(lock_range);
1184
1185/*
1186 * helper function to unlock both pages and extents in the tree.
1187 */
1188int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1189{
1190 unsigned long index = start >> PAGE_CACHE_SHIFT;
1191 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1192 struct page *page;
1193
1194 while (index <= end_index) {
1195 page = find_get_page(tree->mapping, index);
1196 unlock_page(page);
1197 page_cache_release(page);
1198 index++;
1199 }
1200 unlock_extent(tree, start, end, GFP_NOFS);
1201 return 0;
1202}
1203EXPORT_SYMBOL(unlock_range);
1204
1205int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1206{
1207 struct rb_node *node;
1208 struct extent_state *state;
1209 int ret = 0;
1210
70dec807 1211 spin_lock_irq(&tree->lock);
d1310b2e
CM
1212 /*
1213 * this search will find all the extents that end after
1214 * our range starts.
1215 */
80ea96b1 1216 node = tree_search(tree, start);
2b114d1d 1217 if (!node) {
d1310b2e
CM
1218 ret = -ENOENT;
1219 goto out;
1220 }
1221 state = rb_entry(node, struct extent_state, rb_node);
1222 if (state->start != start) {
1223 ret = -ENOENT;
1224 goto out;
1225 }
1226 state->private = private;
1227out:
70dec807 1228 spin_unlock_irq(&tree->lock);
d1310b2e
CM
1229 return ret;
1230}
1231
1232int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1233{
1234 struct rb_node *node;
1235 struct extent_state *state;
1236 int ret = 0;
1237
70dec807 1238 spin_lock_irq(&tree->lock);
d1310b2e
CM
1239 /*
1240 * this search will find all the extents that end after
1241 * our range starts.
1242 */
80ea96b1 1243 node = tree_search(tree, start);
2b114d1d 1244 if (!node) {
d1310b2e
CM
1245 ret = -ENOENT;
1246 goto out;
1247 }
1248 state = rb_entry(node, struct extent_state, rb_node);
1249 if (state->start != start) {
1250 ret = -ENOENT;
1251 goto out;
1252 }
1253 *private = state->private;
1254out:
70dec807 1255 spin_unlock_irq(&tree->lock);
d1310b2e
CM
1256 return ret;
1257}
1258
1259/*
1260 * searches a range in the state tree for a given mask.
70dec807 1261 * If 'filled' == 1, this returns 1 only if every extent in the tree
d1310b2e
CM
1262 * has the bits set. Otherwise, 1 is returned if any bit in the
1263 * range is found set.
1264 */
1265int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1266 int bits, int filled)
1267{
1268 struct extent_state *state = NULL;
1269 struct rb_node *node;
1270 int bitset = 0;
1271 unsigned long flags;
1272
70dec807 1273 spin_lock_irqsave(&tree->lock, flags);
80ea96b1 1274 node = tree_search(tree, start);
d1310b2e
CM
1275 while (node && start <= end) {
1276 state = rb_entry(node, struct extent_state, rb_node);
1277
1278 if (filled && state->start > start) {
1279 bitset = 0;
1280 break;
1281 }
1282
1283 if (state->start > end)
1284 break;
1285
1286 if (state->state & bits) {
1287 bitset = 1;
1288 if (!filled)
1289 break;
1290 } else if (filled) {
1291 bitset = 0;
1292 break;
1293 }
1294 start = state->end + 1;
1295 if (start > end)
1296 break;
1297 node = rb_next(node);
1298 if (!node) {
1299 if (filled)
1300 bitset = 0;
1301 break;
1302 }
1303 }
70dec807 1304 spin_unlock_irqrestore(&tree->lock, flags);
d1310b2e
CM
1305 return bitset;
1306}
1307EXPORT_SYMBOL(test_range_bit);
1308
1309/*
1310 * helper function to set a given page up to date if all the
1311 * extents in the tree for that page are up to date
1312 */
1313static int check_page_uptodate(struct extent_io_tree *tree,
1314 struct page *page)
1315{
1316 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1317 u64 end = start + PAGE_CACHE_SIZE - 1;
1318 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1319 SetPageUptodate(page);
1320 return 0;
1321}
1322
1323/*
1324 * helper function to unlock a page if all the extents in the tree
1325 * for that page are unlocked
1326 */
1327static int check_page_locked(struct extent_io_tree *tree,
1328 struct page *page)
1329{
1330 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1331 u64 end = start + PAGE_CACHE_SIZE - 1;
1332 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1333 unlock_page(page);
1334 return 0;
1335}
1336
1337/*
1338 * helper function to end page writeback if all the extents
1339 * in the tree for that page are done with writeback
1340 */
1341static int check_page_writeback(struct extent_io_tree *tree,
1342 struct page *page)
1343{
1344 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1345 u64 end = start + PAGE_CACHE_SIZE - 1;
1346 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1347 end_page_writeback(page);
1348 return 0;
1349}
1350
1351/* lots and lots of room for performance fixes in the end_bio funcs */
1352
1353/*
1354 * after a writepage IO is done, we need to:
1355 * clear the uptodate bits on error
1356 * clear the writeback bits in the extent tree for this IO
1357 * end_page_writeback if the page has no more pending IO
1358 *
1359 * Scheduling is not allowed, so the extent state tree is expected
1360 * to have one and only one object corresponding to this IO.
1361 */
1362#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1363static void end_bio_extent_writepage(struct bio *bio, int err)
1364#else
1365static int end_bio_extent_writepage(struct bio *bio,
1366 unsigned int bytes_done, int err)
1367#endif
1368{
1259ab75 1369 int uptodate = err == 0;
d1310b2e 1370 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
70dec807
CM
1371 struct extent_state *state = bio->bi_private;
1372 struct extent_io_tree *tree = state->tree;
1373 struct rb_node *node;
d1310b2e
CM
1374 u64 start;
1375 u64 end;
70dec807 1376 u64 cur;
d1310b2e 1377 int whole_page;
1259ab75 1378 int ret;
70dec807 1379 unsigned long flags;
d1310b2e
CM
1380
1381#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1382 if (bio->bi_size)
1383 return 1;
1384#endif
d1310b2e
CM
1385 do {
1386 struct page *page = bvec->bv_page;
1387 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1388 bvec->bv_offset;
1389 end = start + bvec->bv_len - 1;
1390
1391 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1392 whole_page = 1;
1393 else
1394 whole_page = 0;
1395
1396 if (--bvec >= bio->bi_io_vec)
1397 prefetchw(&bvec->bv_page->flags);
1398
1259ab75
CM
1399 if (tree->ops && tree->ops->writepage_end_io_hook) {
1400 ret = tree->ops->writepage_end_io_hook(page, start,
1401 end, state);
1402 if (ret)
1403 uptodate = 0;
1404 }
1405
1406 if (!uptodate && tree->ops &&
1407 tree->ops->writepage_io_failed_hook) {
1408 ret = tree->ops->writepage_io_failed_hook(bio, page,
1409 start, end, state);
1410 if (ret == 0) {
1411 state = NULL;
1412 uptodate = (err == 0);
1413 continue;
1414 }
1415 }
1416
d1310b2e
CM
1417 if (!uptodate) {
1418 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1419 ClearPageUptodate(page);
1420 SetPageError(page);
1421 }
70dec807 1422
70dec807
CM
1423 /*
1424 * bios can get merged in funny ways, and so we need to
1425 * be careful with the state variable. We know the
1426 * state won't be merged with others because it has
1427 * WRITEBACK set, but we can't be sure each biovec is
1428 * sequential in the file. So, if our cached state
1429 * doesn't match the expected end, search the tree
1430 * for the correct one.
1431 */
1432
1433 spin_lock_irqsave(&tree->lock, flags);
1434 if (!state || state->end != end) {
1435 state = NULL;
80ea96b1 1436 node = __etree_search(tree, start, NULL, NULL);
70dec807
CM
1437 if (node) {
1438 state = rb_entry(node, struct extent_state,
1439 rb_node);
1440 if (state->end != end ||
1441 !(state->state & EXTENT_WRITEBACK))
1442 state = NULL;
1443 }
1444 if (!state) {
1445 spin_unlock_irqrestore(&tree->lock, flags);
1446 clear_extent_writeback(tree, start,
1447 end, GFP_ATOMIC);
1448 goto next_io;
1449 }
1450 }
1451 cur = end;
1452 while(1) {
1453 struct extent_state *clear = state;
1454 cur = state->start;
1455 node = rb_prev(&state->rb_node);
1456 if (node) {
1457 state = rb_entry(node,
1458 struct extent_state,
1459 rb_node);
1460 } else {
1461 state = NULL;
1462 }
1463
1464 clear_state_bit(tree, clear, EXTENT_WRITEBACK,
1465 1, 0);
1466 if (cur == start)
1467 break;
1468 if (cur < start) {
1469 WARN_ON(1);
1470 break;
1471 }
1472 if (!node)
1473 break;
1474 }
1475 /* before releasing the lock, make sure the next state
1476 * variable has the expected bits set and corresponds
1477 * to the correct offsets in the file
1478 */
1479 if (state && (state->end + 1 != start ||
c2e639f0 1480 !(state->state & EXTENT_WRITEBACK))) {
70dec807
CM
1481 state = NULL;
1482 }
1483 spin_unlock_irqrestore(&tree->lock, flags);
1484next_io:
d1310b2e
CM
1485
1486 if (whole_page)
1487 end_page_writeback(page);
1488 else
1489 check_page_writeback(tree, page);
d1310b2e 1490 } while (bvec >= bio->bi_io_vec);
d1310b2e
CM
1491 bio_put(bio);
1492#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1493 return 0;
1494#endif
1495}
1496
1497/*
1498 * after a readpage IO is done, we need to:
1499 * clear the uptodate bits on error
1500 * set the uptodate bits if things worked
1501 * set the page up to date if all extents in the tree are uptodate
1502 * clear the lock bit in the extent tree
1503 * unlock the page if there are no other extents locked for it
1504 *
1505 * Scheduling is not allowed, so the extent state tree is expected
1506 * to have one and only one object corresponding to this IO.
1507 */
1508#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1509static void end_bio_extent_readpage(struct bio *bio, int err)
1510#else
1511static int end_bio_extent_readpage(struct bio *bio,
1512 unsigned int bytes_done, int err)
1513#endif
1514{
1515 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1516 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
70dec807
CM
1517 struct extent_state *state = bio->bi_private;
1518 struct extent_io_tree *tree = state->tree;
1519 struct rb_node *node;
d1310b2e
CM
1520 u64 start;
1521 u64 end;
70dec807
CM
1522 u64 cur;
1523 unsigned long flags;
d1310b2e
CM
1524 int whole_page;
1525 int ret;
1526
1527#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1528 if (bio->bi_size)
1529 return 1;
1530#endif
1531
1532 do {
1533 struct page *page = bvec->bv_page;
1534 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1535 bvec->bv_offset;
1536 end = start + bvec->bv_len - 1;
1537
1538 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1539 whole_page = 1;
1540 else
1541 whole_page = 0;
1542
1543 if (--bvec >= bio->bi_io_vec)
1544 prefetchw(&bvec->bv_page->flags);
1545
1546 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
70dec807
CM
1547 ret = tree->ops->readpage_end_io_hook(page, start, end,
1548 state);
d1310b2e
CM
1549 if (ret)
1550 uptodate = 0;
1551 }
7e38326f
CM
1552 if (!uptodate && tree->ops &&
1553 tree->ops->readpage_io_failed_hook) {
1554 ret = tree->ops->readpage_io_failed_hook(bio, page,
1555 start, end, state);
1556 if (ret == 0) {
1557 state = NULL;
3b951516
CM
1558 uptodate =
1559 test_bit(BIO_UPTODATE, &bio->bi_flags);
7e38326f
CM
1560 continue;
1561 }
1562 }
d1310b2e 1563
70dec807
CM
1564 spin_lock_irqsave(&tree->lock, flags);
1565 if (!state || state->end != end) {
1566 state = NULL;
80ea96b1 1567 node = __etree_search(tree, start, NULL, NULL);
70dec807
CM
1568 if (node) {
1569 state = rb_entry(node, struct extent_state,
1570 rb_node);
1571 if (state->end != end ||
1572 !(state->state & EXTENT_LOCKED))
1573 state = NULL;
1574 }
3b951516 1575 if (!state) {
70dec807 1576 spin_unlock_irqrestore(&tree->lock, flags);
3b951516
CM
1577 if (uptodate)
1578 set_extent_uptodate(tree, start, end,
1579 GFP_ATOMIC);
70dec807
CM
1580 unlock_extent(tree, start, end, GFP_ATOMIC);
1581 goto next_io;
1582 }
1583 }
d1310b2e 1584
70dec807
CM
1585 cur = end;
1586 while(1) {
1587 struct extent_state *clear = state;
1588 cur = state->start;
1589 node = rb_prev(&state->rb_node);
1590 if (node) {
1591 state = rb_entry(node,
1592 struct extent_state,
1593 rb_node);
1594 } else {
1595 state = NULL;
1596 }
f188591e
CM
1597 if (uptodate) {
1598 set_state_cb(tree, clear, EXTENT_UPTODATE);
1599 clear->state |= EXTENT_UPTODATE;
1600 }
70dec807
CM
1601 clear_state_bit(tree, clear, EXTENT_LOCKED,
1602 1, 0);
1603 if (cur == start)
1604 break;
1605 if (cur < start) {
1606 WARN_ON(1);
1607 break;
1608 }
1609 if (!node)
1610 break;
1611 }
1612 /* before releasing the lock, make sure the next state
1613 * variable has the expected bits set and corresponds
1614 * to the correct offsets in the file
1615 */
1616 if (state && (state->end + 1 != start ||
c2e639f0 1617 !(state->state & EXTENT_LOCKED))) {
70dec807
CM
1618 state = NULL;
1619 }
1620 spin_unlock_irqrestore(&tree->lock, flags);
1621next_io:
1622 if (whole_page) {
1623 if (uptodate) {
1624 SetPageUptodate(page);
1625 } else {
1626 ClearPageUptodate(page);
1627 SetPageError(page);
1628 }
d1310b2e 1629 unlock_page(page);
70dec807
CM
1630 } else {
1631 if (uptodate) {
1632 check_page_uptodate(tree, page);
1633 } else {
1634 ClearPageUptodate(page);
1635 SetPageError(page);
1636 }
d1310b2e 1637 check_page_locked(tree, page);
70dec807 1638 }
d1310b2e
CM
1639 } while (bvec >= bio->bi_io_vec);
1640
1641 bio_put(bio);
1642#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1643 return 0;
1644#endif
1645}
1646
1647/*
1648 * IO done from prepare_write is pretty simple, we just unlock
1649 * the structs in the extent tree when done, and set the uptodate bits
1650 * as appropriate.
1651 */
1652#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1653static void end_bio_extent_preparewrite(struct bio *bio, int err)
1654#else
1655static int end_bio_extent_preparewrite(struct bio *bio,
1656 unsigned int bytes_done, int err)
1657#endif
1658{
1659 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1660 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
70dec807
CM
1661 struct extent_state *state = bio->bi_private;
1662 struct extent_io_tree *tree = state->tree;
d1310b2e
CM
1663 u64 start;
1664 u64 end;
1665
1666#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1667 if (bio->bi_size)
1668 return 1;
1669#endif
1670
1671 do {
1672 struct page *page = bvec->bv_page;
1673 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1674 bvec->bv_offset;
1675 end = start + bvec->bv_len - 1;
1676
1677 if (--bvec >= bio->bi_io_vec)
1678 prefetchw(&bvec->bv_page->flags);
1679
1680 if (uptodate) {
1681 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1682 } else {
1683 ClearPageUptodate(page);
1684 SetPageError(page);
1685 }
1686
1687 unlock_extent(tree, start, end, GFP_ATOMIC);
1688
1689 } while (bvec >= bio->bi_io_vec);
1690
1691 bio_put(bio);
1692#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1693 return 0;
1694#endif
1695}
1696
1697static struct bio *
1698extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1699 gfp_t gfp_flags)
1700{
1701 struct bio *bio;
1702
1703 bio = bio_alloc(gfp_flags, nr_vecs);
1704
1705 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1706 while (!bio && (nr_vecs /= 2))
1707 bio = bio_alloc(gfp_flags, nr_vecs);
1708 }
1709
1710 if (bio) {
e1c4b745 1711 bio->bi_size = 0;
d1310b2e
CM
1712 bio->bi_bdev = bdev;
1713 bio->bi_sector = first_sector;
1714 }
1715 return bio;
1716}
1717
f188591e 1718static int submit_one_bio(int rw, struct bio *bio, int mirror_num)
d1310b2e 1719{
d1310b2e 1720 int ret = 0;
70dec807
CM
1721 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1722 struct page *page = bvec->bv_page;
1723 struct extent_io_tree *tree = bio->bi_private;
1724 struct rb_node *node;
1725 struct extent_state *state;
1726 u64 start;
1727 u64 end;
1728
1729 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1730 end = start + bvec->bv_len - 1;
1731
1732 spin_lock_irq(&tree->lock);
80ea96b1 1733 node = __etree_search(tree, start, NULL, NULL);
70dec807
CM
1734 BUG_ON(!node);
1735 state = rb_entry(node, struct extent_state, rb_node);
1736 while(state->end < end) {
1737 node = rb_next(node);
1738 state = rb_entry(node, struct extent_state, rb_node);
1739 }
1740 BUG_ON(state->end != end);
1741 spin_unlock_irq(&tree->lock);
1742
1743 bio->bi_private = state;
d1310b2e
CM
1744
1745 bio_get(bio);
1746
065631f6 1747 if (tree->ops && tree->ops->submit_bio_hook)
f188591e
CM
1748 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1749 mirror_num);
0b86a832
CM
1750 else
1751 submit_bio(rw, bio);
d1310b2e
CM
1752 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1753 ret = -EOPNOTSUPP;
1754 bio_put(bio);
1755 return ret;
1756}
1757
1758static int submit_extent_page(int rw, struct extent_io_tree *tree,
1759 struct page *page, sector_t sector,
1760 size_t size, unsigned long offset,
1761 struct block_device *bdev,
1762 struct bio **bio_ret,
1763 unsigned long max_pages,
f188591e
CM
1764 bio_end_io_t end_io_func,
1765 int mirror_num)
d1310b2e
CM
1766{
1767 int ret = 0;
1768 struct bio *bio;
1769 int nr;
1770
1771 if (bio_ret && *bio_ret) {
1772 bio = *bio_ret;
1773 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
239b14b3
CM
1774 (tree->ops && tree->ops->merge_bio_hook &&
1775 tree->ops->merge_bio_hook(page, offset, size, bio)) ||
d1310b2e 1776 bio_add_page(bio, page, size, offset) < size) {
f188591e 1777 ret = submit_one_bio(rw, bio, mirror_num);
d1310b2e
CM
1778 bio = NULL;
1779 } else {
1780 return 0;
1781 }
1782 }
961d0232 1783 nr = bio_get_nr_vecs(bdev);
d1310b2e
CM
1784 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1785 if (!bio) {
1786 printk("failed to allocate bio nr %d\n", nr);
1787 }
70dec807
CM
1788
1789
d1310b2e
CM
1790 bio_add_page(bio, page, size, offset);
1791 bio->bi_end_io = end_io_func;
1792 bio->bi_private = tree;
70dec807 1793
d1310b2e
CM
1794 if (bio_ret) {
1795 *bio_ret = bio;
1796 } else {
f188591e 1797 ret = submit_one_bio(rw, bio, mirror_num);
d1310b2e
CM
1798 }
1799
1800 return ret;
1801}
1802
1803void set_page_extent_mapped(struct page *page)
1804{
1805 if (!PagePrivate(page)) {
1806 SetPagePrivate(page);
1807 WARN_ON(!page->mapping->a_ops->invalidatepage);
1808 set_page_private(page, EXTENT_PAGE_PRIVATE);
1809 page_cache_get(page);
1810 }
1811}
1812
1813void set_page_extent_head(struct page *page, unsigned long len)
1814{
1815 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1816}
1817
1818/*
1819 * basic readpage implementation. Locked extent state structs are inserted
1820 * into the tree that are removed when the IO is done (by the end_io
1821 * handlers)
1822 */
1823static int __extent_read_full_page(struct extent_io_tree *tree,
1824 struct page *page,
1825 get_extent_t *get_extent,
f188591e 1826 struct bio **bio, int mirror_num)
d1310b2e
CM
1827{
1828 struct inode *inode = page->mapping->host;
1829 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1830 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1831 u64 end;
1832 u64 cur = start;
1833 u64 extent_offset;
1834 u64 last_byte = i_size_read(inode);
1835 u64 block_start;
1836 u64 cur_end;
1837 sector_t sector;
1838 struct extent_map *em;
1839 struct block_device *bdev;
1840 int ret;
1841 int nr = 0;
1842 size_t page_offset = 0;
1843 size_t iosize;
1844 size_t blocksize = inode->i_sb->s_blocksize;
1845
1846 set_page_extent_mapped(page);
1847
1848 end = page_end;
1849 lock_extent(tree, start, end, GFP_NOFS);
1850
1851 while (cur <= end) {
1852 if (cur >= last_byte) {
1853 char *userpage;
1854 iosize = PAGE_CACHE_SIZE - page_offset;
1855 userpage = kmap_atomic(page, KM_USER0);
1856 memset(userpage + page_offset, 0, iosize);
1857 flush_dcache_page(page);
1858 kunmap_atomic(userpage, KM_USER0);
1859 set_extent_uptodate(tree, cur, cur + iosize - 1,
1860 GFP_NOFS);
1861 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1862 break;
1863 }
1864 em = get_extent(inode, page, page_offset, cur,
1865 end - cur + 1, 0);
1866 if (IS_ERR(em) || !em) {
1867 SetPageError(page);
1868 unlock_extent(tree, cur, end, GFP_NOFS);
1869 break;
1870 }
1871
1872 extent_offset = cur - em->start;
1873 BUG_ON(extent_map_end(em) <= cur);
1874 BUG_ON(end < cur);
1875
1876 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1877 cur_end = min(extent_map_end(em) - 1, end);
1878 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1879 sector = (em->block_start + extent_offset) >> 9;
1880 bdev = em->bdev;
1881 block_start = em->block_start;
1882 free_extent_map(em);
1883 em = NULL;
1884
1885 /* we've found a hole, just zero and go on */
1886 if (block_start == EXTENT_MAP_HOLE) {
1887 char *userpage;
1888 userpage = kmap_atomic(page, KM_USER0);
1889 memset(userpage + page_offset, 0, iosize);
1890 flush_dcache_page(page);
1891 kunmap_atomic(userpage, KM_USER0);
1892
1893 set_extent_uptodate(tree, cur, cur + iosize - 1,
1894 GFP_NOFS);
1895 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1896 cur = cur + iosize;
1897 page_offset += iosize;
1898 continue;
1899 }
1900 /* the get_extent function already copied into the page */
1901 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1902 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1903 cur = cur + iosize;
1904 page_offset += iosize;
1905 continue;
1906 }
70dec807
CM
1907 /* we have an inline extent but it didn't get marked up
1908 * to date. Error out
1909 */
1910 if (block_start == EXTENT_MAP_INLINE) {
1911 SetPageError(page);
1912 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1913 cur = cur + iosize;
1914 page_offset += iosize;
1915 continue;
1916 }
d1310b2e
CM
1917
1918 ret = 0;
1919 if (tree->ops && tree->ops->readpage_io_hook) {
1920 ret = tree->ops->readpage_io_hook(page, cur,
1921 cur + iosize - 1);
1922 }
1923 if (!ret) {
1924 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1925 nr -= page->index;
1926 ret = submit_extent_page(READ, tree, page,
1927 sector, iosize, page_offset,
1928 bdev, bio, nr,
f188591e 1929 end_bio_extent_readpage, mirror_num);
d1310b2e
CM
1930 }
1931 if (ret)
1932 SetPageError(page);
1933 cur = cur + iosize;
1934 page_offset += iosize;
1935 nr++;
1936 }
1937 if (!nr) {
1938 if (!PageError(page))
1939 SetPageUptodate(page);
1940 unlock_page(page);
1941 }
1942 return 0;
1943}
1944
1945int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
1946 get_extent_t *get_extent)
1947{
1948 struct bio *bio = NULL;
1949 int ret;
1950
f188591e 1951 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0);
d1310b2e 1952 if (bio)
f188591e 1953 submit_one_bio(READ, bio, 0);
d1310b2e
CM
1954 return ret;
1955}
1956EXPORT_SYMBOL(extent_read_full_page);
1957
1958/*
1959 * the writepage semantics are similar to regular writepage. extent
1960 * records are inserted to lock ranges in the tree, and as dirty areas
1961 * are found, they are marked writeback. Then the lock bits are removed
1962 * and the end_io handler clears the writeback ranges
1963 */
1964static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1965 void *data)
1966{
1967 struct inode *inode = page->mapping->host;
1968 struct extent_page_data *epd = data;
1969 struct extent_io_tree *tree = epd->tree;
1970 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1971 u64 delalloc_start;
1972 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1973 u64 end;
1974 u64 cur = start;
1975 u64 extent_offset;
1976 u64 last_byte = i_size_read(inode);
1977 u64 block_start;
1978 u64 iosize;
1979 sector_t sector;
1980 struct extent_map *em;
1981 struct block_device *bdev;
1982 int ret;
1983 int nr = 0;
1984 size_t page_offset = 0;
1985 size_t blocksize;
1986 loff_t i_size = i_size_read(inode);
1987 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
1988 u64 nr_delalloc;
1989 u64 delalloc_end;
1990
211c17f5 1991
d1310b2e 1992 WARN_ON(!PageLocked(page));
211c17f5
CM
1993 page_offset = i_size & (PAGE_CACHE_SIZE - 1);
1994 if (page->index > end_index ||
1995 (page->index == end_index && !page_offset)) {
1996 page->mapping->a_ops->invalidatepage(page, 0);
d1310b2e
CM
1997 unlock_page(page);
1998 return 0;
1999 }
2000
2001 if (page->index == end_index) {
2002 char *userpage;
2003
d1310b2e 2004 userpage = kmap_atomic(page, KM_USER0);
211c17f5
CM
2005 memset(userpage + page_offset, 0,
2006 PAGE_CACHE_SIZE - page_offset);
d1310b2e 2007 kunmap_atomic(userpage, KM_USER0);
211c17f5 2008 flush_dcache_page(page);
d1310b2e 2009 }
211c17f5 2010 page_offset = 0;
d1310b2e
CM
2011
2012 set_page_extent_mapped(page);
2013
2014 delalloc_start = start;
2015 delalloc_end = 0;
2016 while(delalloc_end < page_end) {
2017 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
2018 &delalloc_end,
2019 128 * 1024 * 1024);
2020 if (nr_delalloc == 0) {
2021 delalloc_start = delalloc_end + 1;
2022 continue;
2023 }
2024 tree->ops->fill_delalloc(inode, delalloc_start,
2025 delalloc_end);
2026 clear_extent_bit(tree, delalloc_start,
2027 delalloc_end,
2028 EXTENT_LOCKED | EXTENT_DELALLOC,
2029 1, 0, GFP_NOFS);
2030 delalloc_start = delalloc_end + 1;
2031 }
2032 lock_extent(tree, start, page_end, GFP_NOFS);
2033
2034 end = page_end;
2035 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2036 printk("found delalloc bits after lock_extent\n");
2037 }
2038
2039 if (last_byte <= start) {
2040 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2041 goto done;
2042 }
2043
2044 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2045 blocksize = inode->i_sb->s_blocksize;
2046
2047 while (cur <= end) {
2048 if (cur >= last_byte) {
2049 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2050 break;
2051 }
2052 em = epd->get_extent(inode, page, page_offset, cur,
2053 end - cur + 1, 1);
2054 if (IS_ERR(em) || !em) {
2055 SetPageError(page);
2056 break;
2057 }
2058
2059 extent_offset = cur - em->start;
2060 BUG_ON(extent_map_end(em) <= cur);
2061 BUG_ON(end < cur);
2062 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2063 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2064 sector = (em->block_start + extent_offset) >> 9;
2065 bdev = em->bdev;
2066 block_start = em->block_start;
2067 free_extent_map(em);
2068 em = NULL;
2069
2070 if (block_start == EXTENT_MAP_HOLE ||
2071 block_start == EXTENT_MAP_INLINE) {
2072 clear_extent_dirty(tree, cur,
2073 cur + iosize - 1, GFP_NOFS);
2074 cur = cur + iosize;
2075 page_offset += iosize;
2076 continue;
2077 }
2078
2079 /* leave this out until we have a page_mkwrite call */
2080 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2081 EXTENT_DIRTY, 0)) {
2082 cur = cur + iosize;
2083 page_offset += iosize;
2084 continue;
2085 }
2086 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2087 if (tree->ops && tree->ops->writepage_io_hook) {
2088 ret = tree->ops->writepage_io_hook(page, cur,
2089 cur + iosize - 1);
2090 } else {
2091 ret = 0;
2092 }
1259ab75 2093 if (ret) {
d1310b2e 2094 SetPageError(page);
1259ab75 2095 } else {
d1310b2e
CM
2096 unsigned long max_nr = end_index + 1;
2097 set_range_writeback(tree, cur, cur + iosize - 1);
2098 if (!PageWriteback(page)) {
2099 printk("warning page %lu not writeback, "
2100 "cur %llu end %llu\n", page->index,
2101 (unsigned long long)cur,
2102 (unsigned long long)end);
2103 }
2104
2105 ret = submit_extent_page(WRITE, tree, page, sector,
2106 iosize, page_offset, bdev,
2107 &epd->bio, max_nr,
f188591e 2108 end_bio_extent_writepage, 0);
d1310b2e
CM
2109 if (ret)
2110 SetPageError(page);
2111 }
2112 cur = cur + iosize;
2113 page_offset += iosize;
2114 nr++;
2115 }
2116done:
2117 if (nr == 0) {
2118 /* make sure the mapping tag for page dirty gets cleared */
2119 set_page_writeback(page);
2120 end_page_writeback(page);
2121 }
2122 unlock_extent(tree, start, page_end, GFP_NOFS);
2123 unlock_page(page);
2124 return 0;
2125}
2126
5e478dc9 2127#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
d1310b2e
CM
2128/* Taken directly from 2.6.23 for 2.6.18 back port */
2129typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
2130 void *data);
2131
2132/**
2133 * write_cache_pages - walk the list of dirty pages of the given address space
2134 * and write all of them.
2135 * @mapping: address space structure to write
2136 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2137 * @writepage: function called for each page
2138 * @data: data passed to writepage function
2139 *
2140 * If a page is already under I/O, write_cache_pages() skips it, even
2141 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2142 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2143 * and msync() need to guarantee that all the data which was dirty at the time
2144 * the call was made get new I/O started against them. If wbc->sync_mode is
2145 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2146 * existing IO to complete.
2147 */
2148static int write_cache_pages(struct address_space *mapping,
2149 struct writeback_control *wbc, writepage_t writepage,
2150 void *data)
2151{
2152 struct backing_dev_info *bdi = mapping->backing_dev_info;
2153 int ret = 0;
2154 int done = 0;
2155 struct pagevec pvec;
2156 int nr_pages;
2157 pgoff_t index;
2158 pgoff_t end; /* Inclusive */
2159 int scanned = 0;
2160 int range_whole = 0;
2161
2162 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2163 wbc->encountered_congestion = 1;
2164 return 0;
2165 }
2166
2167 pagevec_init(&pvec, 0);
2168 if (wbc->range_cyclic) {
2169 index = mapping->writeback_index; /* Start from prev offset */
2170 end = -1;
2171 } else {
2172 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2173 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2174 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2175 range_whole = 1;
2176 scanned = 1;
2177 }
2178retry:
2179 while (!done && (index <= end) &&
2180 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2181 PAGECACHE_TAG_DIRTY,
2182 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2183 unsigned i;
2184
2185 scanned = 1;
2186 for (i = 0; i < nr_pages; i++) {
2187 struct page *page = pvec.pages[i];
2188
2189 /*
2190 * At this point we hold neither mapping->tree_lock nor
2191 * lock on the page itself: the page may be truncated or
2192 * invalidated (changing page->mapping to NULL), or even
2193 * swizzled back from swapper_space to tmpfs file
2194 * mapping
2195 */
2196 lock_page(page);
2197
2198 if (unlikely(page->mapping != mapping)) {
2199 unlock_page(page);
2200 continue;
2201 }
2202
2203 if (!wbc->range_cyclic && page->index > end) {
2204 done = 1;
2205 unlock_page(page);
2206 continue;
2207 }
2208
2209 if (wbc->sync_mode != WB_SYNC_NONE)
2210 wait_on_page_writeback(page);
2211
2212 if (PageWriteback(page) ||
2213 !clear_page_dirty_for_io(page)) {
2214 unlock_page(page);
2215 continue;
2216 }
2217
2218 ret = (*writepage)(page, wbc, data);
2219
2220 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2221 unlock_page(page);
2222 ret = 0;
2223 }
2224 if (ret || (--(wbc->nr_to_write) <= 0))
2225 done = 1;
2226 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2227 wbc->encountered_congestion = 1;
2228 done = 1;
2229 }
2230 }
2231 pagevec_release(&pvec);
2232 cond_resched();
2233 }
2234 if (!scanned && !done) {
2235 /*
2236 * We hit the last page and there is more work to be done: wrap
2237 * back to the start of the file
2238 */
2239 scanned = 1;
2240 index = 0;
2241 goto retry;
2242 }
2243 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2244 mapping->writeback_index = index;
2245 return ret;
2246}
2247#endif
2248
2249int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2250 get_extent_t *get_extent,
2251 struct writeback_control *wbc)
2252{
2253 int ret;
2254 struct address_space *mapping = page->mapping;
2255 struct extent_page_data epd = {
2256 .bio = NULL,
2257 .tree = tree,
2258 .get_extent = get_extent,
2259 };
2260 struct writeback_control wbc_writepages = {
2261 .bdi = wbc->bdi,
2262 .sync_mode = WB_SYNC_NONE,
2263 .older_than_this = NULL,
2264 .nr_to_write = 64,
2265 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2266 .range_end = (loff_t)-1,
2267 };
2268
2269
2270 ret = __extent_writepage(page, wbc, &epd);
2271
2272 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
2273 if (epd.bio) {
f188591e 2274 submit_one_bio(WRITE, epd.bio, 0);
d1310b2e
CM
2275 }
2276 return ret;
2277}
2278EXPORT_SYMBOL(extent_write_full_page);
2279
2280
2281int extent_writepages(struct extent_io_tree *tree,
2282 struct address_space *mapping,
2283 get_extent_t *get_extent,
2284 struct writeback_control *wbc)
2285{
2286 int ret = 0;
2287 struct extent_page_data epd = {
2288 .bio = NULL,
2289 .tree = tree,
2290 .get_extent = get_extent,
2291 };
2292
2293 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
2294 if (epd.bio) {
f188591e 2295 submit_one_bio(WRITE, epd.bio, 0);
d1310b2e
CM
2296 }
2297 return ret;
2298}
2299EXPORT_SYMBOL(extent_writepages);
2300
2301int extent_readpages(struct extent_io_tree *tree,
2302 struct address_space *mapping,
2303 struct list_head *pages, unsigned nr_pages,
2304 get_extent_t get_extent)
2305{
2306 struct bio *bio = NULL;
2307 unsigned page_idx;
2308 struct pagevec pvec;
2309
2310 pagevec_init(&pvec, 0);
2311 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2312 struct page *page = list_entry(pages->prev, struct page, lru);
2313
2314 prefetchw(&page->flags);
2315 list_del(&page->lru);
2316 /*
2317 * what we want to do here is call add_to_page_cache_lru,
2318 * but that isn't exported, so we reproduce it here
2319 */
2320 if (!add_to_page_cache(page, mapping,
2321 page->index, GFP_KERNEL)) {
2322
2323 /* open coding of lru_cache_add, also not exported */
2324 page_cache_get(page);
2325 if (!pagevec_add(&pvec, page))
2326 __pagevec_lru_add(&pvec);
f188591e
CM
2327 __extent_read_full_page(tree, page, get_extent,
2328 &bio, 0);
d1310b2e
CM
2329 }
2330 page_cache_release(page);
2331 }
2332 if (pagevec_count(&pvec))
2333 __pagevec_lru_add(&pvec);
2334 BUG_ON(!list_empty(pages));
2335 if (bio)
f188591e 2336 submit_one_bio(READ, bio, 0);
d1310b2e
CM
2337 return 0;
2338}
2339EXPORT_SYMBOL(extent_readpages);
2340
2341/*
2342 * basic invalidatepage code, this waits on any locked or writeback
2343 * ranges corresponding to the page, and then deletes any extent state
2344 * records from the tree
2345 */
2346int extent_invalidatepage(struct extent_io_tree *tree,
2347 struct page *page, unsigned long offset)
2348{
2349 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2350 u64 end = start + PAGE_CACHE_SIZE - 1;
2351 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2352
2353 start += (offset + blocksize -1) & ~(blocksize - 1);
2354 if (start > end)
2355 return 0;
2356
2357 lock_extent(tree, start, end, GFP_NOFS);
2358 wait_on_extent_writeback(tree, start, end);
2359 clear_extent_bit(tree, start, end,
2360 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2361 1, 1, GFP_NOFS);
2362 return 0;
2363}
2364EXPORT_SYMBOL(extent_invalidatepage);
2365
2366/*
2367 * simple commit_write call, set_range_dirty is used to mark both
2368 * the pages and the extent records as dirty
2369 */
2370int extent_commit_write(struct extent_io_tree *tree,
2371 struct inode *inode, struct page *page,
2372 unsigned from, unsigned to)
2373{
2374 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2375
2376 set_page_extent_mapped(page);
2377 set_page_dirty(page);
2378
2379 if (pos > inode->i_size) {
2380 i_size_write(inode, pos);
2381 mark_inode_dirty(inode);
2382 }
2383 return 0;
2384}
2385EXPORT_SYMBOL(extent_commit_write);
2386
2387int extent_prepare_write(struct extent_io_tree *tree,
2388 struct inode *inode, struct page *page,
2389 unsigned from, unsigned to, get_extent_t *get_extent)
2390{
2391 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2392 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2393 u64 block_start;
2394 u64 orig_block_start;
2395 u64 block_end;
2396 u64 cur_end;
2397 struct extent_map *em;
2398 unsigned blocksize = 1 << inode->i_blkbits;
2399 size_t page_offset = 0;
2400 size_t block_off_start;
2401 size_t block_off_end;
2402 int err = 0;
2403 int iocount = 0;
2404 int ret = 0;
2405 int isnew;
2406
2407 set_page_extent_mapped(page);
2408
2409 block_start = (page_start + from) & ~((u64)blocksize - 1);
2410 block_end = (page_start + to - 1) | (blocksize - 1);
2411 orig_block_start = block_start;
2412
2413 lock_extent(tree, page_start, page_end, GFP_NOFS);
2414 while(block_start <= block_end) {
2415 em = get_extent(inode, page, page_offset, block_start,
2416 block_end - block_start + 1, 1);
2417 if (IS_ERR(em) || !em) {
2418 goto err;
2419 }
2420 cur_end = min(block_end, extent_map_end(em) - 1);
2421 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2422 block_off_end = block_off_start + blocksize;
2423 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2424
2425 if (!PageUptodate(page) && isnew &&
2426 (block_off_end > to || block_off_start < from)) {
2427 void *kaddr;
2428
2429 kaddr = kmap_atomic(page, KM_USER0);
2430 if (block_off_end > to)
2431 memset(kaddr + to, 0, block_off_end - to);
2432 if (block_off_start < from)
2433 memset(kaddr + block_off_start, 0,
2434 from - block_off_start);
2435 flush_dcache_page(page);
2436 kunmap_atomic(kaddr, KM_USER0);
2437 }
2438 if ((em->block_start != EXTENT_MAP_HOLE &&
2439 em->block_start != EXTENT_MAP_INLINE) &&
2440 !isnew && !PageUptodate(page) &&
2441 (block_off_end > to || block_off_start < from) &&
2442 !test_range_bit(tree, block_start, cur_end,
2443 EXTENT_UPTODATE, 1)) {
2444 u64 sector;
2445 u64 extent_offset = block_start - em->start;
2446 size_t iosize;
2447 sector = (em->block_start + extent_offset) >> 9;
2448 iosize = (cur_end - block_start + blocksize) &
2449 ~((u64)blocksize - 1);
2450 /*
2451 * we've already got the extent locked, but we
2452 * need to split the state such that our end_bio
2453 * handler can clear the lock.
2454 */
2455 set_extent_bit(tree, block_start,
2456 block_start + iosize - 1,
2457 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2458 ret = submit_extent_page(READ, tree, page,
2459 sector, iosize, page_offset, em->bdev,
2460 NULL, 1,
f188591e 2461 end_bio_extent_preparewrite, 0);
d1310b2e
CM
2462 iocount++;
2463 block_start = block_start + iosize;
2464 } else {
2465 set_extent_uptodate(tree, block_start, cur_end,
2466 GFP_NOFS);
2467 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2468 block_start = cur_end + 1;
2469 }
2470 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2471 free_extent_map(em);
2472 }
2473 if (iocount) {
2474 wait_extent_bit(tree, orig_block_start,
2475 block_end, EXTENT_LOCKED);
2476 }
2477 check_page_uptodate(tree, page);
2478err:
2479 /* FIXME, zero out newly allocated blocks on error */
2480 return err;
2481}
2482EXPORT_SYMBOL(extent_prepare_write);
2483
7b13b7b1
CM
2484/*
2485 * a helper for releasepage, this tests for areas of the page that
2486 * are locked or under IO and drops the related state bits if it is safe
2487 * to drop the page.
2488 */
2489int try_release_extent_state(struct extent_map_tree *map,
2490 struct extent_io_tree *tree, struct page *page,
2491 gfp_t mask)
2492{
2493 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2494 u64 end = start + PAGE_CACHE_SIZE - 1;
2495 int ret = 1;
2496
2497 if (test_range_bit(tree, start, end, EXTENT_IOBITS, 0))
2498 ret = 0;
2499 else {
2500 if ((mask & GFP_NOFS) == GFP_NOFS)
2501 mask = GFP_NOFS;
2502 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2503 1, 1, mask);
2504 }
2505 return ret;
2506}
2507EXPORT_SYMBOL(try_release_extent_state);
2508
d1310b2e
CM
2509/*
2510 * a helper for releasepage. As long as there are no locked extents
2511 * in the range corresponding to the page, both state records and extent
2512 * map records are removed
2513 */
2514int try_release_extent_mapping(struct extent_map_tree *map,
70dec807
CM
2515 struct extent_io_tree *tree, struct page *page,
2516 gfp_t mask)
d1310b2e
CM
2517{
2518 struct extent_map *em;
2519 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2520 u64 end = start + PAGE_CACHE_SIZE - 1;
7b13b7b1 2521
70dec807
CM
2522 if ((mask & __GFP_WAIT) &&
2523 page->mapping->host->i_size > 16 * 1024 * 1024) {
39b5637f 2524 u64 len;
70dec807 2525 while (start <= end) {
39b5637f 2526 len = end - start + 1;
70dec807 2527 spin_lock(&map->lock);
39b5637f 2528 em = lookup_extent_mapping(map, start, len);
70dec807
CM
2529 if (!em || IS_ERR(em)) {
2530 spin_unlock(&map->lock);
2531 break;
2532 }
2533 if (em->start != start) {
2534 spin_unlock(&map->lock);
2535 free_extent_map(em);
2536 break;
2537 }
2538 if (!test_range_bit(tree, em->start,
2539 extent_map_end(em) - 1,
2540 EXTENT_LOCKED, 0)) {
2541 remove_extent_mapping(map, em);
2542 /* once for the rb tree */
2543 free_extent_map(em);
2544 }
2545 start = extent_map_end(em);
d1310b2e 2546 spin_unlock(&map->lock);
70dec807
CM
2547
2548 /* once for us */
d1310b2e
CM
2549 free_extent_map(em);
2550 }
d1310b2e 2551 }
7b13b7b1 2552 return try_release_extent_state(map, tree, page, mask);
d1310b2e
CM
2553}
2554EXPORT_SYMBOL(try_release_extent_mapping);
2555
2556sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2557 get_extent_t *get_extent)
2558{
2559 struct inode *inode = mapping->host;
2560 u64 start = iblock << inode->i_blkbits;
2561 sector_t sector = 0;
2562 struct extent_map *em;
2563
2564 em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0);
2565 if (!em || IS_ERR(em))
2566 return 0;
2567
2568 if (em->block_start == EXTENT_MAP_INLINE ||
2569 em->block_start == EXTENT_MAP_HOLE)
2570 goto out;
2571
2572 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
d1310b2e
CM
2573out:
2574 free_extent_map(em);
2575 return sector;
2576}
2577
2578static int add_lru(struct extent_io_tree *tree, struct extent_buffer *eb)
2579{
2580 if (list_empty(&eb->lru)) {
2581 extent_buffer_get(eb);
2582 list_add(&eb->lru, &tree->buffer_lru);
2583 tree->lru_size++;
2584 if (tree->lru_size >= BUFFER_LRU_MAX) {
2585 struct extent_buffer *rm;
2586 rm = list_entry(tree->buffer_lru.prev,
2587 struct extent_buffer, lru);
2588 tree->lru_size--;
2589 list_del_init(&rm->lru);
2590 free_extent_buffer(rm);
2591 }
2592 } else
2593 list_move(&eb->lru, &tree->buffer_lru);
2594 return 0;
2595}
2596static struct extent_buffer *find_lru(struct extent_io_tree *tree,
2597 u64 start, unsigned long len)
2598{
2599 struct list_head *lru = &tree->buffer_lru;
2600 struct list_head *cur = lru->next;
2601 struct extent_buffer *eb;
2602
2603 if (list_empty(lru))
2604 return NULL;
2605
2606 do {
2607 eb = list_entry(cur, struct extent_buffer, lru);
2608 if (eb->start == start && eb->len == len) {
2609 extent_buffer_get(eb);
2610 return eb;
2611 }
2612 cur = cur->next;
2613 } while (cur != lru);
2614 return NULL;
2615}
2616
2617static inline unsigned long num_extent_pages(u64 start, u64 len)
2618{
2619 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2620 (start >> PAGE_CACHE_SHIFT);
2621}
2622
2623static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2624 unsigned long i)
2625{
2626 struct page *p;
2627 struct address_space *mapping;
2628
2629 if (i == 0)
2630 return eb->first_page;
2631 i += eb->start >> PAGE_CACHE_SHIFT;
2632 mapping = eb->first_page->mapping;
2633 read_lock_irq(&mapping->tree_lock);
2634 p = radix_tree_lookup(&mapping->page_tree, i);
2635 read_unlock_irq(&mapping->tree_lock);
2636 return p;
2637}
2638
ce9adaa5
CM
2639int release_extent_buffer_tail_pages(struct extent_buffer *eb)
2640{
2641 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
2642 struct page *page;
2643 unsigned long i;
2644
2645 if (num_pages == 1)
2646 return 0;
2647 for (i = 1; i < num_pages; i++) {
2648 page = extent_buffer_page(eb, i);
2649 page_cache_release(page);
2650 }
2651 return 0;
2652}
2653
2654
728131d8
CM
2655int invalidate_extent_lru(struct extent_io_tree *tree, u64 start,
2656 unsigned long len)
2657{
2658 struct list_head *lru = &tree->buffer_lru;
2659 struct list_head *cur = lru->next;
2660 struct extent_buffer *eb;
2661 int found = 0;
2662
2663 spin_lock(&tree->lru_lock);
2664 if (list_empty(lru))
2665 goto out;
2666
2667 do {
2668 eb = list_entry(cur, struct extent_buffer, lru);
2669 if (eb->start <= start && eb->start + eb->len > start) {
2670 eb->flags &= ~EXTENT_UPTODATE;
2671 }
728131d8
CM
2672 cur = cur->next;
2673 } while (cur != lru);
2674out:
2675 spin_unlock(&tree->lru_lock);
2676 return found;
2677}
2678
d1310b2e
CM
2679static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2680 u64 start,
2681 unsigned long len,
2682 gfp_t mask)
2683{
2684 struct extent_buffer *eb = NULL;
2d2ae547 2685 unsigned long flags;
d1310b2e
CM
2686
2687 spin_lock(&tree->lru_lock);
2688 eb = find_lru(tree, start, len);
2689 spin_unlock(&tree->lru_lock);
2690 if (eb) {
2691 return eb;
2692 }
2693
2694 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2695 INIT_LIST_HEAD(&eb->lru);
2696 eb->start = start;
2697 eb->len = len;
2d2ae547
CM
2698 spin_lock_irqsave(&leak_lock, flags);
2699 list_add(&eb->leak_list, &buffers);
2700 spin_unlock_irqrestore(&leak_lock, flags);
d1310b2e
CM
2701 atomic_set(&eb->refs, 1);
2702
2703 return eb;
2704}
2705
2706static void __free_extent_buffer(struct extent_buffer *eb)
2707{
2d2ae547
CM
2708 unsigned long flags;
2709 spin_lock_irqsave(&leak_lock, flags);
2710 list_del(&eb->leak_list);
2711 spin_unlock_irqrestore(&leak_lock, flags);
d1310b2e
CM
2712 kmem_cache_free(extent_buffer_cache, eb);
2713}
2714
2715struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2716 u64 start, unsigned long len,
2717 struct page *page0,
2718 gfp_t mask)
2719{
2720 unsigned long num_pages = num_extent_pages(start, len);
2721 unsigned long i;
2722 unsigned long index = start >> PAGE_CACHE_SHIFT;
2723 struct extent_buffer *eb;
2724 struct page *p;
2725 struct address_space *mapping = tree->mapping;
2726 int uptodate = 1;
2727
2728 eb = __alloc_extent_buffer(tree, start, len, mask);
2b114d1d 2729 if (!eb)
d1310b2e
CM
2730 return NULL;
2731
2732 if (eb->flags & EXTENT_BUFFER_FILLED)
2733 goto lru_add;
2734
2735 if (page0) {
2736 eb->first_page = page0;
2737 i = 1;
2738 index++;
2739 page_cache_get(page0);
2740 mark_page_accessed(page0);
2741 set_page_extent_mapped(page0);
d1310b2e 2742 set_page_extent_head(page0, len);
f188591e 2743 uptodate = PageUptodate(page0);
d1310b2e
CM
2744 } else {
2745 i = 0;
2746 }
2747 for (; i < num_pages; i++, index++) {
2748 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
2749 if (!p) {
2750 WARN_ON(1);
2751 goto fail;
2752 }
2753 set_page_extent_mapped(p);
2754 mark_page_accessed(p);
2755 if (i == 0) {
2756 eb->first_page = p;
2757 set_page_extent_head(p, len);
2758 } else {
2759 set_page_private(p, EXTENT_PAGE_PRIVATE);
2760 }
2761 if (!PageUptodate(p))
2762 uptodate = 0;
2763 unlock_page(p);
2764 }
2765 if (uptodate)
2766 eb->flags |= EXTENT_UPTODATE;
2767 eb->flags |= EXTENT_BUFFER_FILLED;
2768
2769lru_add:
2770 spin_lock(&tree->lru_lock);
2771 add_lru(tree, eb);
2772 spin_unlock(&tree->lru_lock);
2773 return eb;
2774
2775fail:
2776 spin_lock(&tree->lru_lock);
2777 list_del_init(&eb->lru);
2778 spin_unlock(&tree->lru_lock);
2779 if (!atomic_dec_and_test(&eb->refs))
2780 return NULL;
2781 for (index = 1; index < i; index++) {
2782 page_cache_release(extent_buffer_page(eb, index));
2783 }
2784 if (i > 0)
2785 page_cache_release(extent_buffer_page(eb, 0));
2786 __free_extent_buffer(eb);
2787 return NULL;
2788}
2789EXPORT_SYMBOL(alloc_extent_buffer);
2790
2791struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
2792 u64 start, unsigned long len,
2793 gfp_t mask)
2794{
2795 unsigned long num_pages = num_extent_pages(start, len);
2796 unsigned long i;
2797 unsigned long index = start >> PAGE_CACHE_SHIFT;
2798 struct extent_buffer *eb;
2799 struct page *p;
2800 struct address_space *mapping = tree->mapping;
2801 int uptodate = 1;
2802
2803 eb = __alloc_extent_buffer(tree, start, len, mask);
2b114d1d 2804 if (!eb)
d1310b2e
CM
2805 return NULL;
2806
2807 if (eb->flags & EXTENT_BUFFER_FILLED)
2808 goto lru_add;
2809
2810 for (i = 0; i < num_pages; i++, index++) {
2811 p = find_lock_page(mapping, index);
2812 if (!p) {
2813 goto fail;
2814 }
2815 set_page_extent_mapped(p);
2816 mark_page_accessed(p);
2817
2818 if (i == 0) {
2819 eb->first_page = p;
2820 set_page_extent_head(p, len);
2821 } else {
2822 set_page_private(p, EXTENT_PAGE_PRIVATE);
2823 }
2824
2825 if (!PageUptodate(p))
2826 uptodate = 0;
2827 unlock_page(p);
2828 }
2829 if (uptodate)
2830 eb->flags |= EXTENT_UPTODATE;
2831 eb->flags |= EXTENT_BUFFER_FILLED;
2832
2833lru_add:
2834 spin_lock(&tree->lru_lock);
2835 add_lru(tree, eb);
2836 spin_unlock(&tree->lru_lock);
2837 return eb;
2838fail:
2839 spin_lock(&tree->lru_lock);
2840 list_del_init(&eb->lru);
2841 spin_unlock(&tree->lru_lock);
2842 if (!atomic_dec_and_test(&eb->refs))
2843 return NULL;
2844 for (index = 1; index < i; index++) {
2845 page_cache_release(extent_buffer_page(eb, index));
2846 }
2847 if (i > 0)
2848 page_cache_release(extent_buffer_page(eb, 0));
2849 __free_extent_buffer(eb);
2850 return NULL;
2851}
2852EXPORT_SYMBOL(find_extent_buffer);
2853
2854void free_extent_buffer(struct extent_buffer *eb)
2855{
2856 unsigned long i;
2857 unsigned long num_pages;
2858
2859 if (!eb)
2860 return;
2861
2862 if (!atomic_dec_and_test(&eb->refs))
2863 return;
2864
2865 WARN_ON(!list_empty(&eb->lru));
2866 num_pages = num_extent_pages(eb->start, eb->len);
2867
2868 for (i = 1; i < num_pages; i++) {
2869 page_cache_release(extent_buffer_page(eb, i));
2870 }
2871 page_cache_release(extent_buffer_page(eb, 0));
2872 __free_extent_buffer(eb);
2873}
2874EXPORT_SYMBOL(free_extent_buffer);
2875
2876int clear_extent_buffer_dirty(struct extent_io_tree *tree,
2877 struct extent_buffer *eb)
2878{
2879 int set;
2880 unsigned long i;
2881 unsigned long num_pages;
2882 struct page *page;
2883
2884 u64 start = eb->start;
2885 u64 end = start + eb->len - 1;
2886
2887 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
2888 num_pages = num_extent_pages(eb->start, eb->len);
2889
2890 for (i = 0; i < num_pages; i++) {
2891 page = extent_buffer_page(eb, i);
d1310b2e
CM
2892 if (i == 0)
2893 set_page_extent_head(page, eb->len);
2894 else
2895 set_page_private(page, EXTENT_PAGE_PRIVATE);
2896
2897 /*
2898 * if we're on the last page or the first page and the
2899 * block isn't aligned on a page boundary, do extra checks
2900 * to make sure we don't clean page that is partially dirty
2901 */
2902 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2903 ((i == num_pages - 1) &&
2904 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2905 start = (u64)page->index << PAGE_CACHE_SHIFT;
2906 end = start + PAGE_CACHE_SIZE - 1;
2907 if (test_range_bit(tree, start, end,
2908 EXTENT_DIRTY, 0)) {
d1310b2e
CM
2909 continue;
2910 }
2911 }
2912 clear_page_dirty_for_io(page);
70dec807 2913 read_lock_irq(&page->mapping->tree_lock);
d1310b2e
CM
2914 if (!PageDirty(page)) {
2915 radix_tree_tag_clear(&page->mapping->page_tree,
2916 page_index(page),
2917 PAGECACHE_TAG_DIRTY);
2918 }
70dec807 2919 read_unlock_irq(&page->mapping->tree_lock);
d1310b2e
CM
2920 }
2921 return 0;
2922}
2923EXPORT_SYMBOL(clear_extent_buffer_dirty);
2924
2925int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
2926 struct extent_buffer *eb)
2927{
2928 return wait_on_extent_writeback(tree, eb->start,
2929 eb->start + eb->len - 1);
2930}
2931EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2932
2933int set_extent_buffer_dirty(struct extent_io_tree *tree,
2934 struct extent_buffer *eb)
2935{
2936 unsigned long i;
2937 unsigned long num_pages;
2938
2939 num_pages = num_extent_pages(eb->start, eb->len);
2940 for (i = 0; i < num_pages; i++) {
2941 struct page *page = extent_buffer_page(eb, i);
2942 /* writepage may need to do something special for the
2943 * first page, we have to make sure page->private is
2944 * properly set. releasepage may drop page->private
2945 * on us if the page isn't already dirty.
2946 */
2947 if (i == 0) {
d1310b2e
CM
2948 set_page_extent_head(page, eb->len);
2949 } else if (PagePrivate(page) &&
2950 page->private != EXTENT_PAGE_PRIVATE) {
d1310b2e 2951 set_page_extent_mapped(page);
d1310b2e
CM
2952 }
2953 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
d1310b2e
CM
2954 }
2955 return set_extent_dirty(tree, eb->start,
2956 eb->start + eb->len - 1, GFP_NOFS);
2957}
2958EXPORT_SYMBOL(set_extent_buffer_dirty);
2959
1259ab75
CM
2960int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
2961 struct extent_buffer *eb)
2962{
2963 unsigned long i;
2964 struct page *page;
2965 unsigned long num_pages;
2966
2967 num_pages = num_extent_pages(eb->start, eb->len);
2968 eb->flags &= ~EXTENT_UPTODATE;
2969
2970 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2971 GFP_NOFS);
2972 for (i = 0; i < num_pages; i++) {
2973 page = extent_buffer_page(eb, i);
2974 ClearPageUptodate(page);
2975 }
2976 return 0;
2977}
2978
d1310b2e
CM
2979int set_extent_buffer_uptodate(struct extent_io_tree *tree,
2980 struct extent_buffer *eb)
2981{
2982 unsigned long i;
2983 struct page *page;
2984 unsigned long num_pages;
2985
2986 num_pages = num_extent_pages(eb->start, eb->len);
2987
2988 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2989 GFP_NOFS);
2990 for (i = 0; i < num_pages; i++) {
2991 page = extent_buffer_page(eb, i);
2992 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2993 ((i == num_pages - 1) &&
2994 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2995 check_page_uptodate(tree, page);
2996 continue;
2997 }
2998 SetPageUptodate(page);
2999 }
3000 return 0;
3001}
3002EXPORT_SYMBOL(set_extent_buffer_uptodate);
3003
ce9adaa5
CM
3004int extent_range_uptodate(struct extent_io_tree *tree,
3005 u64 start, u64 end)
3006{
3007 struct page *page;
3008 int ret;
3009 int pg_uptodate = 1;
3010 int uptodate;
3011 unsigned long index;
3012
3013 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3014 if (ret)
3015 return 1;
3016 while(start <= end) {
3017 index = start >> PAGE_CACHE_SHIFT;
3018 page = find_get_page(tree->mapping, index);
3019 uptodate = PageUptodate(page);
3020 page_cache_release(page);
3021 if (!uptodate) {
3022 pg_uptodate = 0;
3023 break;
3024 }
3025 start += PAGE_CACHE_SIZE;
3026 }
3027 return pg_uptodate;
3028}
3029
d1310b2e 3030int extent_buffer_uptodate(struct extent_io_tree *tree,
ce9adaa5 3031 struct extent_buffer *eb)
d1310b2e 3032{
728131d8 3033 int ret = 0;
ce9adaa5
CM
3034 unsigned long num_pages;
3035 unsigned long i;
728131d8
CM
3036 struct page *page;
3037 int pg_uptodate = 1;
3038
d1310b2e 3039 if (eb->flags & EXTENT_UPTODATE)
4235298e 3040 return 1;
728131d8 3041
4235298e 3042 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
d1310b2e 3043 EXTENT_UPTODATE, 1);
4235298e
CM
3044 if (ret)
3045 return ret;
728131d8
CM
3046
3047 num_pages = num_extent_pages(eb->start, eb->len);
3048 for (i = 0; i < num_pages; i++) {
3049 page = extent_buffer_page(eb, i);
3050 if (!PageUptodate(page)) {
3051 pg_uptodate = 0;
3052 break;
3053 }
3054 }
4235298e 3055 return pg_uptodate;
d1310b2e
CM
3056}
3057EXPORT_SYMBOL(extent_buffer_uptodate);
3058
3059int read_extent_buffer_pages(struct extent_io_tree *tree,
3060 struct extent_buffer *eb,
a86c12c7 3061 u64 start, int wait,
f188591e 3062 get_extent_t *get_extent, int mirror_num)
d1310b2e
CM
3063{
3064 unsigned long i;
3065 unsigned long start_i;
3066 struct page *page;
3067 int err;
3068 int ret = 0;
ce9adaa5
CM
3069 int locked_pages = 0;
3070 int all_uptodate = 1;
3071 int inc_all_pages = 0;
d1310b2e 3072 unsigned long num_pages;
a86c12c7
CM
3073 struct bio *bio = NULL;
3074
d1310b2e
CM
3075 if (eb->flags & EXTENT_UPTODATE)
3076 return 0;
3077
ce9adaa5 3078 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
d1310b2e
CM
3079 EXTENT_UPTODATE, 1)) {
3080 return 0;
3081 }
3082
3083 if (start) {
3084 WARN_ON(start < eb->start);
3085 start_i = (start >> PAGE_CACHE_SHIFT) -
3086 (eb->start >> PAGE_CACHE_SHIFT);
3087 } else {
3088 start_i = 0;
3089 }
3090
3091 num_pages = num_extent_pages(eb->start, eb->len);
3092 for (i = start_i; i < num_pages; i++) {
3093 page = extent_buffer_page(eb, i);
d1310b2e 3094 if (!wait) {
ce9adaa5
CM
3095 if (TestSetPageLocked(page))
3096 goto unlock_exit;
d1310b2e
CM
3097 } else {
3098 lock_page(page);
3099 }
ce9adaa5 3100 locked_pages++;
d1310b2e 3101 if (!PageUptodate(page)) {
ce9adaa5
CM
3102 all_uptodate = 0;
3103 }
3104 }
3105 if (all_uptodate) {
3106 if (start_i == 0)
3107 eb->flags |= EXTENT_UPTODATE;
3108 goto unlock_exit;
3109 }
3110
3111 for (i = start_i; i < num_pages; i++) {
3112 page = extent_buffer_page(eb, i);
3113 if (inc_all_pages)
3114 page_cache_get(page);
3115 if (!PageUptodate(page)) {
3116 if (start_i == 0)
3117 inc_all_pages = 1;
f188591e 3118 ClearPageError(page);
a86c12c7 3119 err = __extent_read_full_page(tree, page,
f188591e
CM
3120 get_extent, &bio,
3121 mirror_num);
d1310b2e
CM
3122 if (err) {
3123 ret = err;
3124 }
3125 } else {
3126 unlock_page(page);
3127 }
3128 }
3129
a86c12c7 3130 if (bio)
f188591e 3131 submit_one_bio(READ, bio, mirror_num);
a86c12c7 3132
d1310b2e
CM
3133 if (ret || !wait) {
3134 return ret;
3135 }
d1310b2e
CM
3136 for (i = start_i; i < num_pages; i++) {
3137 page = extent_buffer_page(eb, i);
3138 wait_on_page_locked(page);
3139 if (!PageUptodate(page)) {
3140 ret = -EIO;
3141 }
3142 }
3143 if (!ret)
3144 eb->flags |= EXTENT_UPTODATE;
3145 return ret;
ce9adaa5
CM
3146
3147unlock_exit:
3148 i = start_i;
3149 while(locked_pages > 0) {
3150 page = extent_buffer_page(eb, i);
3151 i++;
3152 unlock_page(page);
3153 locked_pages--;
3154 }
3155 return ret;
d1310b2e
CM
3156}
3157EXPORT_SYMBOL(read_extent_buffer_pages);
3158
3159void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3160 unsigned long start,
3161 unsigned long len)
3162{
3163 size_t cur;
3164 size_t offset;
3165 struct page *page;
3166 char *kaddr;
3167 char *dst = (char *)dstv;
3168 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3169 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
d1310b2e
CM
3170
3171 WARN_ON(start > eb->len);
3172 WARN_ON(start + len > eb->start + eb->len);
3173
3174 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3175
3176 while(len > 0) {
3177 page = extent_buffer_page(eb, i);
d1310b2e
CM
3178
3179 cur = min(len, (PAGE_CACHE_SIZE - offset));
3180 kaddr = kmap_atomic(page, KM_USER1);
3181 memcpy(dst, kaddr + offset, cur);
3182 kunmap_atomic(kaddr, KM_USER1);
3183
3184 dst += cur;
3185 len -= cur;
3186 offset = 0;
3187 i++;
3188 }
3189}
3190EXPORT_SYMBOL(read_extent_buffer);
3191
3192int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3193 unsigned long min_len, char **token, char **map,
3194 unsigned long *map_start,
3195 unsigned long *map_len, int km)
3196{
3197 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3198 char *kaddr;
3199 struct page *p;
3200 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3201 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3202 unsigned long end_i = (start_offset + start + min_len - 1) >>
3203 PAGE_CACHE_SHIFT;
3204
3205 if (i != end_i)
3206 return -EINVAL;
3207
3208 if (i == 0) {
3209 offset = start_offset;
3210 *map_start = 0;
3211 } else {
3212 offset = 0;
3213 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3214 }
3215 if (start + min_len > eb->len) {
3216printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3217 WARN_ON(1);
3218 }
3219
3220 p = extent_buffer_page(eb, i);
d1310b2e
CM
3221 kaddr = kmap_atomic(p, km);
3222 *token = kaddr;
3223 *map = kaddr + offset;
3224 *map_len = PAGE_CACHE_SIZE - offset;
3225 return 0;
3226}
3227EXPORT_SYMBOL(map_private_extent_buffer);
3228
3229int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3230 unsigned long min_len,
3231 char **token, char **map,
3232 unsigned long *map_start,
3233 unsigned long *map_len, int km)
3234{
3235 int err;
3236 int save = 0;
3237 if (eb->map_token) {
3238 unmap_extent_buffer(eb, eb->map_token, km);
3239 eb->map_token = NULL;
3240 save = 1;
3241 }
3242 err = map_private_extent_buffer(eb, start, min_len, token, map,
3243 map_start, map_len, km);
3244 if (!err && save) {
3245 eb->map_token = *token;
3246 eb->kaddr = *map;
3247 eb->map_start = *map_start;
3248 eb->map_len = *map_len;
3249 }
3250 return err;
3251}
3252EXPORT_SYMBOL(map_extent_buffer);
3253
3254void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3255{
3256 kunmap_atomic(token, km);
3257}
3258EXPORT_SYMBOL(unmap_extent_buffer);
3259
3260int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3261 unsigned long start,
3262 unsigned long len)
3263{
3264 size_t cur;
3265 size_t offset;
3266 struct page *page;
3267 char *kaddr;
3268 char *ptr = (char *)ptrv;
3269 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3270 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3271 int ret = 0;
3272
3273 WARN_ON(start > eb->len);
3274 WARN_ON(start + len > eb->start + eb->len);
3275
3276 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3277
3278 while(len > 0) {
3279 page = extent_buffer_page(eb, i);
d1310b2e
CM
3280
3281 cur = min(len, (PAGE_CACHE_SIZE - offset));
3282
3283 kaddr = kmap_atomic(page, KM_USER0);
3284 ret = memcmp(ptr, kaddr + offset, cur);
3285 kunmap_atomic(kaddr, KM_USER0);
3286 if (ret)
3287 break;
3288
3289 ptr += cur;
3290 len -= cur;
3291 offset = 0;
3292 i++;
3293 }
3294 return ret;
3295}
3296EXPORT_SYMBOL(memcmp_extent_buffer);
3297
3298void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3299 unsigned long start, unsigned long len)
3300{
3301 size_t cur;
3302 size_t offset;
3303 struct page *page;
3304 char *kaddr;
3305 char *src = (char *)srcv;
3306 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3307 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3308
3309 WARN_ON(start > eb->len);
3310 WARN_ON(start + len > eb->start + eb->len);
3311
3312 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3313
3314 while(len > 0) {
3315 page = extent_buffer_page(eb, i);
3316 WARN_ON(!PageUptodate(page));
3317
3318 cur = min(len, PAGE_CACHE_SIZE - offset);
3319 kaddr = kmap_atomic(page, KM_USER1);
3320 memcpy(kaddr + offset, src, cur);
3321 kunmap_atomic(kaddr, KM_USER1);
3322
3323 src += cur;
3324 len -= cur;
3325 offset = 0;
3326 i++;
3327 }
3328}
3329EXPORT_SYMBOL(write_extent_buffer);
3330
3331void memset_extent_buffer(struct extent_buffer *eb, char c,
3332 unsigned long start, unsigned long len)
3333{
3334 size_t cur;
3335 size_t offset;
3336 struct page *page;
3337 char *kaddr;
3338 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3339 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3340
3341 WARN_ON(start > eb->len);
3342 WARN_ON(start + len > eb->start + eb->len);
3343
3344 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3345
3346 while(len > 0) {
3347 page = extent_buffer_page(eb, i);
3348 WARN_ON(!PageUptodate(page));
3349
3350 cur = min(len, PAGE_CACHE_SIZE - offset);
3351 kaddr = kmap_atomic(page, KM_USER0);
3352 memset(kaddr + offset, c, cur);
3353 kunmap_atomic(kaddr, KM_USER0);
3354
3355 len -= cur;
3356 offset = 0;
3357 i++;
3358 }
3359}
3360EXPORT_SYMBOL(memset_extent_buffer);
3361
3362void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3363 unsigned long dst_offset, unsigned long src_offset,
3364 unsigned long len)
3365{
3366 u64 dst_len = dst->len;
3367 size_t cur;
3368 size_t offset;
3369 struct page *page;
3370 char *kaddr;
3371 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3372 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3373
3374 WARN_ON(src->len != dst_len);
3375
3376 offset = (start_offset + dst_offset) &
3377 ((unsigned long)PAGE_CACHE_SIZE - 1);
3378
3379 while(len > 0) {
3380 page = extent_buffer_page(dst, i);
3381 WARN_ON(!PageUptodate(page));
3382
3383 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3384
3385 kaddr = kmap_atomic(page, KM_USER0);
3386 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3387 kunmap_atomic(kaddr, KM_USER0);
3388
3389 src_offset += cur;
3390 len -= cur;
3391 offset = 0;
3392 i++;
3393 }
3394}
3395EXPORT_SYMBOL(copy_extent_buffer);
3396
3397static void move_pages(struct page *dst_page, struct page *src_page,
3398 unsigned long dst_off, unsigned long src_off,
3399 unsigned long len)
3400{
3401 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3402 if (dst_page == src_page) {
3403 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3404 } else {
3405 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3406 char *p = dst_kaddr + dst_off + len;
3407 char *s = src_kaddr + src_off + len;
3408
3409 while (len--)
3410 *--p = *--s;
3411
3412 kunmap_atomic(src_kaddr, KM_USER1);
3413 }
3414 kunmap_atomic(dst_kaddr, KM_USER0);
3415}
3416
3417static void copy_pages(struct page *dst_page, struct page *src_page,
3418 unsigned long dst_off, unsigned long src_off,
3419 unsigned long len)
3420{
3421 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3422 char *src_kaddr;
3423
3424 if (dst_page != src_page)
3425 src_kaddr = kmap_atomic(src_page, KM_USER1);
3426 else
3427 src_kaddr = dst_kaddr;
3428
3429 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3430 kunmap_atomic(dst_kaddr, KM_USER0);
3431 if (dst_page != src_page)
3432 kunmap_atomic(src_kaddr, KM_USER1);
3433}
3434
3435void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3436 unsigned long src_offset, unsigned long len)
3437{
3438 size_t cur;
3439 size_t dst_off_in_page;
3440 size_t src_off_in_page;
3441 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3442 unsigned long dst_i;
3443 unsigned long src_i;
3444
3445 if (src_offset + len > dst->len) {
3446 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3447 src_offset, len, dst->len);
3448 BUG_ON(1);
3449 }
3450 if (dst_offset + len > dst->len) {
3451 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3452 dst_offset, len, dst->len);
3453 BUG_ON(1);
3454 }
3455
3456 while(len > 0) {
3457 dst_off_in_page = (start_offset + dst_offset) &
3458 ((unsigned long)PAGE_CACHE_SIZE - 1);
3459 src_off_in_page = (start_offset + src_offset) &
3460 ((unsigned long)PAGE_CACHE_SIZE - 1);
3461
3462 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3463 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3464
3465 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3466 src_off_in_page));
3467 cur = min_t(unsigned long, cur,
3468 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3469
3470 copy_pages(extent_buffer_page(dst, dst_i),
3471 extent_buffer_page(dst, src_i),
3472 dst_off_in_page, src_off_in_page, cur);
3473
3474 src_offset += cur;
3475 dst_offset += cur;
3476 len -= cur;
3477 }
3478}
3479EXPORT_SYMBOL(memcpy_extent_buffer);
3480
3481void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3482 unsigned long src_offset, unsigned long len)
3483{
3484 size_t cur;
3485 size_t dst_off_in_page;
3486 size_t src_off_in_page;
3487 unsigned long dst_end = dst_offset + len - 1;
3488 unsigned long src_end = src_offset + len - 1;
3489 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3490 unsigned long dst_i;
3491 unsigned long src_i;
3492
3493 if (src_offset + len > dst->len) {
3494 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3495 src_offset, len, dst->len);
3496 BUG_ON(1);
3497 }
3498 if (dst_offset + len > dst->len) {
3499 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3500 dst_offset, len, dst->len);
3501 BUG_ON(1);
3502 }
3503 if (dst_offset < src_offset) {
3504 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3505 return;
3506 }
3507 while(len > 0) {
3508 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3509 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3510
3511 dst_off_in_page = (start_offset + dst_end) &
3512 ((unsigned long)PAGE_CACHE_SIZE - 1);
3513 src_off_in_page = (start_offset + src_end) &
3514 ((unsigned long)PAGE_CACHE_SIZE - 1);
3515
3516 cur = min_t(unsigned long, len, src_off_in_page + 1);
3517 cur = min(cur, dst_off_in_page + 1);
3518 move_pages(extent_buffer_page(dst, dst_i),
3519 extent_buffer_page(dst, src_i),
3520 dst_off_in_page - cur + 1,
3521 src_off_in_page - cur + 1, cur);
3522
3523 dst_end -= cur;
3524 src_end -= cur;
3525 len -= cur;
3526 }
3527}
3528EXPORT_SYMBOL(memmove_extent_buffer);