blob: b0677c84bb751fb7a0838860117fe74c61274d5c [file] [log] [blame]
Chris Masona52d9a82007-08-27 16:49:44 -04001#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>
Chris Mason4dc119042007-10-15 16:18:14 -040011#include <linux/swap.h>
Jens Axboe0a2118d2007-10-19 09:23:05 -040012#include <linux/version.h>
Chris Masonb293f02e2007-11-01 19:45:34 -040013#include <linux/writeback.h>
Chris Masona52d9a82007-08-27 16:49:44 -040014#include "extent_map.h"
15
Chris Mason86479a02007-09-10 19:58:16 -040016/* temporary define until extent_map moves out of btrfs */
17struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
18 unsigned long extra_flags,
19 void (*ctor)(void *, struct kmem_cache *,
20 unsigned long));
21
Chris Masona52d9a82007-08-27 16:49:44 -040022static struct kmem_cache *extent_map_cache;
23static struct kmem_cache *extent_state_cache;
Chris Mason6d36dcd2007-10-15 16:14:37 -040024static struct kmem_cache *extent_buffer_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -040025
Chris Masonf510cfe2007-10-15 16:14:48 -040026static LIST_HEAD(buffers);
27static LIST_HEAD(states);
28
Chris Masonf510cfe2007-10-15 16:14:48 -040029static spinlock_t state_lock = SPIN_LOCK_UNLOCKED;
Chris Mason4dc119042007-10-15 16:18:14 -040030#define BUFFER_LRU_MAX 64
Chris Masona52d9a82007-08-27 16:49:44 -040031
32struct tree_entry {
33 u64 start;
34 u64 end;
35 int in_tree;
36 struct rb_node rb_node;
37};
38
Chris Masonb293f02e2007-11-01 19:45:34 -040039struct extent_page_data {
40 struct bio *bio;
41 struct extent_map_tree *tree;
42 get_extent_t *get_extent;
43};
44
Chris Masona52d9a82007-08-27 16:49:44 -040045void __init extent_map_init(void)
46{
Chris Mason86479a02007-09-10 19:58:16 -040047 extent_map_cache = btrfs_cache_create("extent_map",
Chris Mason6d36dcd2007-10-15 16:14:37 -040048 sizeof(struct extent_map), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040049 NULL);
Chris Mason86479a02007-09-10 19:58:16 -040050 extent_state_cache = btrfs_cache_create("extent_state",
Chris Mason6d36dcd2007-10-15 16:14:37 -040051 sizeof(struct extent_state), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040052 NULL);
Chris Mason6d36dcd2007-10-15 16:14:37 -040053 extent_buffer_cache = btrfs_cache_create("extent_buffers",
54 sizeof(struct extent_buffer), 0,
55 NULL);
Chris Masona52d9a82007-08-27 16:49:44 -040056}
57
58void __exit extent_map_exit(void)
59{
Chris Masonf510cfe2007-10-15 16:14:48 -040060 struct extent_state *state;
Chris Mason6d36dcd2007-10-15 16:14:37 -040061
Chris Masonf510cfe2007-10-15 16:14:48 -040062 while (!list_empty(&states)) {
63 state = list_entry(states.next, struct extent_state, list);
64 printk("state leak: start %Lu end %Lu state %lu in tree %d refs %d\n", state->start, state->end, state->state, state->in_tree, atomic_read(&state->refs));
65 list_del(&state->list);
66 kmem_cache_free(extent_state_cache, state);
67
68 }
Chris Masonf510cfe2007-10-15 16:14:48 -040069
Chris Masona52d9a82007-08-27 16:49:44 -040070 if (extent_map_cache)
71 kmem_cache_destroy(extent_map_cache);
72 if (extent_state_cache)
73 kmem_cache_destroy(extent_state_cache);
Chris Mason6d36dcd2007-10-15 16:14:37 -040074 if (extent_buffer_cache)
75 kmem_cache_destroy(extent_buffer_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040076}
77
78void extent_map_tree_init(struct extent_map_tree *tree,
79 struct address_space *mapping, gfp_t mask)
80{
81 tree->map.rb_node = NULL;
82 tree->state.rb_node = NULL;
Chris Mason07157aa2007-08-30 08:50:51 -040083 tree->ops = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -040084 rwlock_init(&tree->lock);
Chris Mason4dc119042007-10-15 16:18:14 -040085 spin_lock_init(&tree->lru_lock);
Chris Masona52d9a82007-08-27 16:49:44 -040086 tree->mapping = mapping;
Chris Mason4dc119042007-10-15 16:18:14 -040087 INIT_LIST_HEAD(&tree->buffer_lru);
88 tree->lru_size = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040089}
90EXPORT_SYMBOL(extent_map_tree_init);
91
Chris Mason19c00dd2007-10-15 16:19:22 -040092void extent_map_tree_empty_lru(struct extent_map_tree *tree)
Chris Mason4dc119042007-10-15 16:18:14 -040093{
94 struct extent_buffer *eb;
95 while(!list_empty(&tree->buffer_lru)) {
96 eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
97 lru);
98 list_del(&eb->lru);
99 free_extent_buffer(eb);
100 }
101}
Chris Mason19c00dd2007-10-15 16:19:22 -0400102EXPORT_SYMBOL(extent_map_tree_empty_lru);
Chris Mason4dc119042007-10-15 16:18:14 -0400103
Chris Masona52d9a82007-08-27 16:49:44 -0400104struct extent_map *alloc_extent_map(gfp_t mask)
105{
106 struct extent_map *em;
107 em = kmem_cache_alloc(extent_map_cache, mask);
108 if (!em || IS_ERR(em))
109 return em;
110 em->in_tree = 0;
111 atomic_set(&em->refs, 1);
112 return em;
113}
114EXPORT_SYMBOL(alloc_extent_map);
115
116void free_extent_map(struct extent_map *em)
117{
Chris Mason2bf5a722007-08-30 11:54:02 -0400118 if (!em)
119 return;
Chris Masona52d9a82007-08-27 16:49:44 -0400120 if (atomic_dec_and_test(&em->refs)) {
121 WARN_ON(em->in_tree);
122 kmem_cache_free(extent_map_cache, em);
123 }
124}
125EXPORT_SYMBOL(free_extent_map);
126
127
128struct extent_state *alloc_extent_state(gfp_t mask)
129{
130 struct extent_state *state;
Chris Masonf510cfe2007-10-15 16:14:48 -0400131 unsigned long flags;
132
Chris Masona52d9a82007-08-27 16:49:44 -0400133 state = kmem_cache_alloc(extent_state_cache, mask);
134 if (!state || IS_ERR(state))
135 return state;
136 state->state = 0;
137 state->in_tree = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400138 state->private = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400139
140 spin_lock_irqsave(&state_lock, flags);
141 list_add(&state->list, &states);
142 spin_unlock_irqrestore(&state_lock, flags);
143
Chris Masona52d9a82007-08-27 16:49:44 -0400144 atomic_set(&state->refs, 1);
145 init_waitqueue_head(&state->wq);
Chris Masona52d9a82007-08-27 16:49:44 -0400146 return state;
147}
148EXPORT_SYMBOL(alloc_extent_state);
149
150void free_extent_state(struct extent_state *state)
151{
Chris Masonf510cfe2007-10-15 16:14:48 -0400152 unsigned long flags;
Chris Mason2bf5a722007-08-30 11:54:02 -0400153 if (!state)
154 return;
Chris Masona52d9a82007-08-27 16:49:44 -0400155 if (atomic_dec_and_test(&state->refs)) {
156 WARN_ON(state->in_tree);
Chris Masonf510cfe2007-10-15 16:14:48 -0400157 spin_lock_irqsave(&state_lock, flags);
158 list_del(&state->list);
159 spin_unlock_irqrestore(&state_lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400160 kmem_cache_free(extent_state_cache, state);
161 }
162}
163EXPORT_SYMBOL(free_extent_state);
164
165static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
166 struct rb_node *node)
167{
168 struct rb_node ** p = &root->rb_node;
169 struct rb_node * parent = NULL;
170 struct tree_entry *entry;
171
172 while(*p) {
173 parent = *p;
174 entry = rb_entry(parent, struct tree_entry, rb_node);
175
176 if (offset < entry->start)
177 p = &(*p)->rb_left;
178 else if (offset > entry->end)
179 p = &(*p)->rb_right;
180 else
181 return parent;
182 }
183
184 entry = rb_entry(node, struct tree_entry, rb_node);
185 entry->in_tree = 1;
186 rb_link_node(node, parent, p);
187 rb_insert_color(node, root);
188 return NULL;
189}
190
191static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
192 struct rb_node **prev_ret)
193{
194 struct rb_node * n = root->rb_node;
195 struct rb_node *prev = NULL;
196 struct tree_entry *entry;
197 struct tree_entry *prev_entry = NULL;
198
199 while(n) {
200 entry = rb_entry(n, struct tree_entry, rb_node);
201 prev = n;
202 prev_entry = entry;
203
204 if (offset < entry->start)
205 n = n->rb_left;
206 else if (offset > entry->end)
207 n = n->rb_right;
208 else
209 return n;
210 }
211 if (!prev_ret)
212 return NULL;
213 while(prev && offset > prev_entry->end) {
214 prev = rb_next(prev);
215 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
216 }
217 *prev_ret = prev;
218 return NULL;
219}
220
221static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
222{
223 struct rb_node *prev;
224 struct rb_node *ret;
225 ret = __tree_search(root, offset, &prev);
226 if (!ret)
227 return prev;
228 return ret;
229}
230
231static int tree_delete(struct rb_root *root, u64 offset)
232{
233 struct rb_node *node;
234 struct tree_entry *entry;
235
236 node = __tree_search(root, offset, NULL);
237 if (!node)
238 return -ENOENT;
239 entry = rb_entry(node, struct tree_entry, rb_node);
240 entry->in_tree = 0;
241 rb_erase(node, root);
242 return 0;
243}
244
245/*
246 * add_extent_mapping tries a simple backward merge with existing
247 * mappings. The extent_map struct passed in will be inserted into
248 * the tree directly (no copies made, just a reference taken).
249 */
250int add_extent_mapping(struct extent_map_tree *tree,
251 struct extent_map *em)
252{
253 int ret = 0;
254 struct extent_map *prev = NULL;
255 struct rb_node *rb;
256
257 write_lock_irq(&tree->lock);
258 rb = tree_insert(&tree->map, em->end, &em->rb_node);
259 if (rb) {
260 prev = rb_entry(rb, struct extent_map, rb_node);
261 printk("found extent map %Lu %Lu on insert of %Lu %Lu\n", prev->start, prev->end, em->start, em->end);
262 ret = -EEXIST;
263 goto out;
264 }
265 atomic_inc(&em->refs);
266 if (em->start != 0) {
267 rb = rb_prev(&em->rb_node);
268 if (rb)
269 prev = rb_entry(rb, struct extent_map, rb_node);
270 if (prev && prev->end + 1 == em->start &&
Chris Mason5f39d392007-10-15 16:14:19 -0400271 ((em->block_start == EXTENT_MAP_HOLE &&
272 prev->block_start == EXTENT_MAP_HOLE) ||
Chris Mason179e29e2007-11-01 11:28:41 -0400273 (em->block_start == EXTENT_MAP_INLINE &&
274 prev->block_start == EXTENT_MAP_INLINE) ||
275 (em->block_start == EXTENT_MAP_DELALLOC &&
276 prev->block_start == EXTENT_MAP_DELALLOC) ||
277 (em->block_start < EXTENT_MAP_DELALLOC - 1 &&
278 em->block_start == prev->block_end + 1))) {
Chris Masona52d9a82007-08-27 16:49:44 -0400279 em->start = prev->start;
280 em->block_start = prev->block_start;
281 rb_erase(&prev->rb_node, &tree->map);
282 prev->in_tree = 0;
283 free_extent_map(prev);
284 }
285 }
286out:
287 write_unlock_irq(&tree->lock);
288 return ret;
289}
290EXPORT_SYMBOL(add_extent_mapping);
291
292/*
293 * lookup_extent_mapping returns the first extent_map struct in the
294 * tree that intersects the [start, end] (inclusive) range. There may
295 * be additional objects in the tree that intersect, so check the object
296 * returned carefully to make sure you don't need additional lookups.
297 */
298struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
299 u64 start, u64 end)
300{
301 struct extent_map *em;
302 struct rb_node *rb_node;
303
304 read_lock_irq(&tree->lock);
305 rb_node = tree_search(&tree->map, start);
306 if (!rb_node) {
307 em = NULL;
308 goto out;
309 }
310 if (IS_ERR(rb_node)) {
311 em = ERR_PTR(PTR_ERR(rb_node));
312 goto out;
313 }
314 em = rb_entry(rb_node, struct extent_map, rb_node);
315 if (em->end < start || em->start > end) {
316 em = NULL;
317 goto out;
318 }
319 atomic_inc(&em->refs);
320out:
321 read_unlock_irq(&tree->lock);
322 return em;
323}
324EXPORT_SYMBOL(lookup_extent_mapping);
325
326/*
327 * removes an extent_map struct from the tree. No reference counts are
328 * dropped, and no checks are done to see if the range is in use
329 */
330int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
331{
332 int ret;
333
334 write_lock_irq(&tree->lock);
335 ret = tree_delete(&tree->map, em->end);
336 write_unlock_irq(&tree->lock);
337 return ret;
338}
339EXPORT_SYMBOL(remove_extent_mapping);
340
341/*
342 * utility function to look for merge candidates inside a given range.
343 * Any extents with matching state are merged together into a single
344 * extent in the tree. Extents with EXTENT_IO in their state field
345 * are not merged because the end_io handlers need to be able to do
346 * operations on them without sleeping (or doing allocations/splits).
347 *
348 * This should be called with the tree lock held.
349 */
350static int merge_state(struct extent_map_tree *tree,
351 struct extent_state *state)
352{
353 struct extent_state *other;
354 struct rb_node *other_node;
355
356 if (state->state & EXTENT_IOBITS)
357 return 0;
358
359 other_node = rb_prev(&state->rb_node);
360 if (other_node) {
361 other = rb_entry(other_node, struct extent_state, rb_node);
362 if (other->end == state->start - 1 &&
363 other->state == state->state) {
364 state->start = other->start;
365 other->in_tree = 0;
366 rb_erase(&other->rb_node, &tree->state);
367 free_extent_state(other);
368 }
369 }
370 other_node = rb_next(&state->rb_node);
371 if (other_node) {
372 other = rb_entry(other_node, struct extent_state, rb_node);
373 if (other->start == state->end + 1 &&
374 other->state == state->state) {
375 other->start = state->start;
376 state->in_tree = 0;
377 rb_erase(&state->rb_node, &tree->state);
378 free_extent_state(state);
379 }
380 }
381 return 0;
382}
383
384/*
385 * insert an extent_state struct into the tree. 'bits' are set on the
386 * struct before it is inserted.
387 *
388 * This may return -EEXIST if the extent is already there, in which case the
389 * state struct is freed.
390 *
391 * The tree lock is not taken internally. This is a utility function and
392 * probably isn't what you want to call (see set/clear_extent_bit).
393 */
394static int insert_state(struct extent_map_tree *tree,
395 struct extent_state *state, u64 start, u64 end,
396 int bits)
397{
398 struct rb_node *node;
399
400 if (end < start) {
401 printk("end < start %Lu %Lu\n", end, start);
402 WARN_ON(1);
403 }
404 state->state |= bits;
405 state->start = start;
406 state->end = end;
Chris Masona52d9a82007-08-27 16:49:44 -0400407 node = tree_insert(&tree->state, end, &state->rb_node);
408 if (node) {
409 struct extent_state *found;
410 found = rb_entry(node, struct extent_state, rb_node);
Chris Masonb888db2b2007-08-27 16:49:44 -0400411 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -0400412 free_extent_state(state);
413 return -EEXIST;
414 }
415 merge_state(tree, state);
416 return 0;
417}
418
419/*
420 * split a given extent state struct in two, inserting the preallocated
421 * struct 'prealloc' as the newly created second half. 'split' indicates an
422 * offset inside 'orig' where it should be split.
423 *
424 * Before calling,
425 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
426 * are two extent state structs in the tree:
427 * prealloc: [orig->start, split - 1]
428 * orig: [ split, orig->end ]
429 *
430 * The tree locks are not taken by this function. They need to be held
431 * by the caller.
432 */
433static int split_state(struct extent_map_tree *tree, struct extent_state *orig,
434 struct extent_state *prealloc, u64 split)
435{
436 struct rb_node *node;
437 prealloc->start = orig->start;
438 prealloc->end = split - 1;
439 prealloc->state = orig->state;
440 orig->start = split;
Chris Masonf510cfe2007-10-15 16:14:48 -0400441
Chris Masona52d9a82007-08-27 16:49:44 -0400442 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
443 if (node) {
444 struct extent_state *found;
445 found = rb_entry(node, struct extent_state, rb_node);
Chris Masonb888db2b2007-08-27 16:49:44 -0400446 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
Chris Masona52d9a82007-08-27 16:49:44 -0400447 free_extent_state(prealloc);
448 return -EEXIST;
449 }
450 return 0;
451}
452
453/*
454 * utility function to clear some bits in an extent state struct.
455 * it will optionally wake up any one waiting on this state (wake == 1), or
456 * forcibly remove the state from the tree (delete == 1).
457 *
458 * If no bits are set on the state struct after clearing things, the
459 * struct is freed and removed from the tree
460 */
461static int clear_state_bit(struct extent_map_tree *tree,
462 struct extent_state *state, int bits, int wake,
463 int delete)
464{
465 int ret = state->state & bits;
466 state->state &= ~bits;
467 if (wake)
468 wake_up(&state->wq);
469 if (delete || state->state == 0) {
470 if (state->in_tree) {
471 rb_erase(&state->rb_node, &tree->state);
472 state->in_tree = 0;
473 free_extent_state(state);
474 } else {
475 WARN_ON(1);
476 }
477 } else {
478 merge_state(tree, state);
479 }
480 return ret;
481}
482
483/*
484 * clear some bits on a range in the tree. This may require splitting
485 * or inserting elements in the tree, so the gfp mask is used to
486 * indicate which allocations or sleeping are allowed.
487 *
488 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
489 * the given range from the tree regardless of state (ie for truncate).
490 *
491 * the range [start, end] is inclusive.
492 *
493 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
494 * bits were already set, or zero if none of the bits were already set.
495 */
496int clear_extent_bit(struct extent_map_tree *tree, u64 start, u64 end,
497 int bits, int wake, int delete, gfp_t mask)
498{
499 struct extent_state *state;
500 struct extent_state *prealloc = NULL;
501 struct rb_node *node;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400502 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400503 int err;
504 int set = 0;
505
506again:
507 if (!prealloc && (mask & __GFP_WAIT)) {
508 prealloc = alloc_extent_state(mask);
509 if (!prealloc)
510 return -ENOMEM;
511 }
512
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400513 write_lock_irqsave(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400514 /*
515 * this search will find the extents that end after
516 * our range starts
517 */
518 node = tree_search(&tree->state, start);
519 if (!node)
520 goto out;
521 state = rb_entry(node, struct extent_state, rb_node);
522 if (state->start > end)
523 goto out;
524 WARN_ON(state->end < start);
525
526 /*
527 * | ---- desired range ---- |
528 * | state | or
529 * | ------------- state -------------- |
530 *
531 * We need to split the extent we found, and may flip
532 * bits on second half.
533 *
534 * If the extent we found extends past our range, we
535 * just split and search again. It'll get split again
536 * the next time though.
537 *
538 * If the extent we found is inside our range, we clear
539 * the desired bit on it.
540 */
541
542 if (state->start < start) {
543 err = split_state(tree, state, prealloc, start);
544 BUG_ON(err == -EEXIST);
545 prealloc = NULL;
546 if (err)
547 goto out;
548 if (state->end <= end) {
549 start = state->end + 1;
550 set |= clear_state_bit(tree, state, bits,
551 wake, delete);
552 } else {
553 start = state->start;
554 }
555 goto search_again;
556 }
557 /*
558 * | ---- desired range ---- |
559 * | state |
560 * We need to split the extent, and clear the bit
561 * on the first half
562 */
563 if (state->start <= end && state->end > end) {
564 err = split_state(tree, state, prealloc, end + 1);
565 BUG_ON(err == -EEXIST);
566
567 if (wake)
568 wake_up(&state->wq);
569 set |= clear_state_bit(tree, prealloc, bits,
570 wake, delete);
571 prealloc = NULL;
572 goto out;
573 }
574
575 start = state->end + 1;
576 set |= clear_state_bit(tree, state, bits, wake, delete);
577 goto search_again;
578
579out:
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400580 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400581 if (prealloc)
582 free_extent_state(prealloc);
583
584 return set;
585
586search_again:
Chris Mason96b51792007-10-15 16:15:19 -0400587 if (start > end)
Chris Masona52d9a82007-08-27 16:49:44 -0400588 goto out;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400589 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400590 if (mask & __GFP_WAIT)
591 cond_resched();
592 goto again;
593}
594EXPORT_SYMBOL(clear_extent_bit);
595
596static int wait_on_state(struct extent_map_tree *tree,
597 struct extent_state *state)
598{
599 DEFINE_WAIT(wait);
600 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
601 read_unlock_irq(&tree->lock);
602 schedule();
603 read_lock_irq(&tree->lock);
604 finish_wait(&state->wq, &wait);
605 return 0;
606}
607
608/*
609 * waits for one or more bits to clear on a range in the state tree.
610 * The range [start, end] is inclusive.
611 * The tree lock is taken by this function
612 */
613int wait_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits)
614{
615 struct extent_state *state;
616 struct rb_node *node;
617
618 read_lock_irq(&tree->lock);
619again:
620 while (1) {
621 /*
622 * this search will find all the extents that end after
623 * our range starts
624 */
625 node = tree_search(&tree->state, start);
626 if (!node)
627 break;
628
629 state = rb_entry(node, struct extent_state, rb_node);
630
631 if (state->start > end)
632 goto out;
633
634 if (state->state & bits) {
635 start = state->start;
636 atomic_inc(&state->refs);
637 wait_on_state(tree, state);
638 free_extent_state(state);
639 goto again;
640 }
641 start = state->end + 1;
642
643 if (start > end)
644 break;
645
646 if (need_resched()) {
647 read_unlock_irq(&tree->lock);
648 cond_resched();
649 read_lock_irq(&tree->lock);
650 }
651 }
652out:
653 read_unlock_irq(&tree->lock);
654 return 0;
655}
656EXPORT_SYMBOL(wait_extent_bit);
657
658/*
659 * set some bits on a range in the tree. This may require allocations
660 * or sleeping, so the gfp mask is used to indicate what is allowed.
661 *
662 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
663 * range already has the desired bits set. The start of the existing
664 * range is returned in failed_start in this case.
665 *
666 * [start, end] is inclusive
667 * This takes the tree lock.
668 */
669int set_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits,
670 int exclusive, u64 *failed_start, gfp_t mask)
671{
672 struct extent_state *state;
673 struct extent_state *prealloc = NULL;
674 struct rb_node *node;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400675 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400676 int err = 0;
677 int set;
678 u64 last_start;
679 u64 last_end;
680again:
681 if (!prealloc && (mask & __GFP_WAIT)) {
682 prealloc = alloc_extent_state(mask);
683 if (!prealloc)
684 return -ENOMEM;
685 }
686
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400687 write_lock_irqsave(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400688 /*
689 * this search will find all the extents that end after
690 * our range starts.
691 */
692 node = tree_search(&tree->state, start);
693 if (!node) {
694 err = insert_state(tree, prealloc, start, end, bits);
695 prealloc = NULL;
696 BUG_ON(err == -EEXIST);
697 goto out;
698 }
699
700 state = rb_entry(node, struct extent_state, rb_node);
701 last_start = state->start;
702 last_end = state->end;
703
704 /*
705 * | ---- desired range ---- |
706 * | state |
707 *
708 * Just lock what we found and keep going
709 */
710 if (state->start == start && state->end <= end) {
711 set = state->state & bits;
712 if (set && exclusive) {
713 *failed_start = state->start;
714 err = -EEXIST;
715 goto out;
716 }
717 state->state |= bits;
718 start = state->end + 1;
719 merge_state(tree, state);
720 goto search_again;
721 }
722
723 /*
724 * | ---- desired range ---- |
725 * | state |
726 * or
727 * | ------------- state -------------- |
728 *
729 * We need to split the extent we found, and may flip bits on
730 * second half.
731 *
732 * If the extent we found extends past our
733 * range, we just split and search again. It'll get split
734 * again the next time though.
735 *
736 * If the extent we found is inside our range, we set the
737 * desired bit on it.
738 */
739 if (state->start < start) {
740 set = state->state & bits;
741 if (exclusive && set) {
742 *failed_start = start;
743 err = -EEXIST;
744 goto out;
745 }
746 err = split_state(tree, state, prealloc, start);
747 BUG_ON(err == -EEXIST);
748 prealloc = NULL;
749 if (err)
750 goto out;
751 if (state->end <= end) {
752 state->state |= bits;
753 start = state->end + 1;
754 merge_state(tree, state);
755 } else {
756 start = state->start;
757 }
758 goto search_again;
759 }
760 /*
761 * | ---- desired range ---- |
Chris Masona52d9a82007-08-27 16:49:44 -0400762 * | state | or | state |
763 *
764 * There's a hole, we need to insert something in it and
765 * ignore the extent we found.
766 */
767 if (state->start > start) {
768 u64 this_end;
769 if (end < last_start)
770 this_end = end;
771 else
772 this_end = last_start -1;
773 err = insert_state(tree, prealloc, start, this_end,
774 bits);
775 prealloc = NULL;
776 BUG_ON(err == -EEXIST);
777 if (err)
778 goto out;
779 start = this_end + 1;
780 goto search_again;
781 }
Chris Masona8c450b2007-09-10 20:00:27 -0400782 /*
783 * | ---- desired range ---- |
784 * | state |
785 * We need to split the extent, and set the bit
786 * on the first half
787 */
788 if (state->start <= end && state->end > end) {
789 set = state->state & bits;
790 if (exclusive && set) {
791 *failed_start = start;
792 err = -EEXIST;
793 goto out;
794 }
795 err = split_state(tree, state, prealloc, end + 1);
796 BUG_ON(err == -EEXIST);
797
798 prealloc->state |= bits;
799 merge_state(tree, prealloc);
800 prealloc = NULL;
801 goto out;
802 }
803
Chris Masona52d9a82007-08-27 16:49:44 -0400804 goto search_again;
805
806out:
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400807 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400808 if (prealloc)
809 free_extent_state(prealloc);
810
811 return err;
812
813search_again:
814 if (start > end)
815 goto out;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400816 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400817 if (mask & __GFP_WAIT)
818 cond_resched();
819 goto again;
820}
821EXPORT_SYMBOL(set_extent_bit);
822
823/* wrappers around set/clear extent bit */
824int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
825 gfp_t mask)
826{
827 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
828 mask);
829}
830EXPORT_SYMBOL(set_extent_dirty);
831
Chris Mason96b51792007-10-15 16:15:19 -0400832int set_extent_bits(struct extent_map_tree *tree, u64 start, u64 end,
833 int bits, gfp_t mask)
834{
835 return set_extent_bit(tree, start, end, bits, 0, NULL,
836 mask);
837}
838EXPORT_SYMBOL(set_extent_bits);
839
840int clear_extent_bits(struct extent_map_tree *tree, u64 start, u64 end,
841 int bits, gfp_t mask)
842{
843 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
844}
845EXPORT_SYMBOL(clear_extent_bits);
846
Chris Masonb888db2b2007-08-27 16:49:44 -0400847int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
848 gfp_t mask)
849{
850 return set_extent_bit(tree, start, end,
851 EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
852 mask);
853}
854EXPORT_SYMBOL(set_extent_delalloc);
855
Chris Masona52d9a82007-08-27 16:49:44 -0400856int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
857 gfp_t mask)
858{
Chris Masonb888db2b2007-08-27 16:49:44 -0400859 return clear_extent_bit(tree, start, end,
860 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
Chris Masona52d9a82007-08-27 16:49:44 -0400861}
862EXPORT_SYMBOL(clear_extent_dirty);
863
864int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
865 gfp_t mask)
866{
867 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
868 mask);
869}
870EXPORT_SYMBOL(set_extent_new);
871
872int clear_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
873 gfp_t mask)
874{
875 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
876}
877EXPORT_SYMBOL(clear_extent_new);
878
879int set_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end,
880 gfp_t mask)
881{
882 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
883 mask);
884}
885EXPORT_SYMBOL(set_extent_uptodate);
886
887int clear_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end,
888 gfp_t mask)
889{
890 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
891}
892EXPORT_SYMBOL(clear_extent_uptodate);
893
894int set_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end,
895 gfp_t mask)
896{
897 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
898 0, NULL, mask);
899}
900EXPORT_SYMBOL(set_extent_writeback);
901
902int clear_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end,
903 gfp_t mask)
904{
905 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
906}
907EXPORT_SYMBOL(clear_extent_writeback);
908
909int wait_on_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end)
910{
911 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
912}
913EXPORT_SYMBOL(wait_on_extent_writeback);
914
915/*
916 * locks a range in ascending order, waiting for any locked regions
917 * it hits on the way. [start,end] are inclusive, and this will sleep.
918 */
919int lock_extent(struct extent_map_tree *tree, u64 start, u64 end, gfp_t mask)
920{
921 int err;
922 u64 failed_start;
923 while (1) {
924 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
925 &failed_start, mask);
926 if (err == -EEXIST && (mask & __GFP_WAIT)) {
927 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
928 start = failed_start;
929 } else {
930 break;
931 }
932 WARN_ON(start > end);
933 }
934 return err;
935}
936EXPORT_SYMBOL(lock_extent);
937
938int unlock_extent(struct extent_map_tree *tree, u64 start, u64 end,
939 gfp_t mask)
940{
941 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
942}
943EXPORT_SYMBOL(unlock_extent);
944
945/*
946 * helper function to set pages and extents in the tree dirty
947 */
948int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end)
949{
950 unsigned long index = start >> PAGE_CACHE_SHIFT;
951 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
952 struct page *page;
953
954 while (index <= end_index) {
955 page = find_get_page(tree->mapping, index);
956 BUG_ON(!page);
957 __set_page_dirty_nobuffers(page);
958 page_cache_release(page);
959 index++;
960 }
961 set_extent_dirty(tree, start, end, GFP_NOFS);
962 return 0;
963}
964EXPORT_SYMBOL(set_range_dirty);
965
966/*
967 * helper function to set both pages and extents in the tree writeback
968 */
969int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end)
970{
971 unsigned long index = start >> PAGE_CACHE_SHIFT;
972 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
973 struct page *page;
974
975 while (index <= end_index) {
976 page = find_get_page(tree->mapping, index);
977 BUG_ON(!page);
978 set_page_writeback(page);
979 page_cache_release(page);
980 index++;
981 }
982 set_extent_writeback(tree, start, end, GFP_NOFS);
983 return 0;
984}
985EXPORT_SYMBOL(set_range_writeback);
986
Chris Mason5f39d392007-10-15 16:14:19 -0400987int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
988 u64 *start_ret, u64 *end_ret, int bits)
989{
990 struct rb_node *node;
991 struct extent_state *state;
992 int ret = 1;
993
Chris Masone19caa52007-10-15 16:17:44 -0400994 read_lock_irq(&tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -0400995 /*
996 * this search will find all the extents that end after
997 * our range starts.
998 */
999 node = tree_search(&tree->state, start);
1000 if (!node || IS_ERR(node)) {
1001 goto out;
1002 }
1003
1004 while(1) {
1005 state = rb_entry(node, struct extent_state, rb_node);
Chris Masone19caa52007-10-15 16:17:44 -04001006 if (state->end >= start && (state->state & bits)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001007 *start_ret = state->start;
1008 *end_ret = state->end;
1009 ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -04001010 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001011 }
1012 node = rb_next(node);
1013 if (!node)
1014 break;
1015 }
1016out:
Chris Masone19caa52007-10-15 16:17:44 -04001017 read_unlock_irq(&tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001018 return ret;
1019}
1020EXPORT_SYMBOL(find_first_extent_bit);
1021
Chris Masonb888db2b2007-08-27 16:49:44 -04001022u64 find_lock_delalloc_range(struct extent_map_tree *tree,
1023 u64 start, u64 lock_start, u64 *end, u64 max_bytes)
1024{
1025 struct rb_node *node;
1026 struct extent_state *state;
1027 u64 cur_start = start;
1028 u64 found = 0;
1029 u64 total_bytes = 0;
1030
1031 write_lock_irq(&tree->lock);
1032 /*
1033 * this search will find all the extents that end after
1034 * our range starts.
1035 */
1036search_again:
1037 node = tree_search(&tree->state, cur_start);
1038 if (!node || IS_ERR(node)) {
1039 goto out;
1040 }
1041
1042 while(1) {
1043 state = rb_entry(node, struct extent_state, rb_node);
1044 if (state->start != cur_start) {
1045 goto out;
1046 }
1047 if (!(state->state & EXTENT_DELALLOC)) {
1048 goto out;
1049 }
1050 if (state->start >= lock_start) {
1051 if (state->state & EXTENT_LOCKED) {
1052 DEFINE_WAIT(wait);
1053 atomic_inc(&state->refs);
Yan944746e2007-11-01 11:28:42 -04001054 prepare_to_wait(&state->wq, &wait,
1055 TASK_UNINTERRUPTIBLE);
Chris Masonb888db2b2007-08-27 16:49:44 -04001056 write_unlock_irq(&tree->lock);
1057 schedule();
1058 write_lock_irq(&tree->lock);
1059 finish_wait(&state->wq, &wait);
1060 free_extent_state(state);
1061 goto search_again;
1062 }
1063 state->state |= EXTENT_LOCKED;
1064 }
1065 found++;
1066 *end = state->end;
1067 cur_start = state->end + 1;
1068 node = rb_next(node);
1069 if (!node)
1070 break;
Yan944746e2007-11-01 11:28:42 -04001071 total_bytes += state->end - state->start + 1;
Chris Masonb888db2b2007-08-27 16:49:44 -04001072 if (total_bytes >= max_bytes)
1073 break;
1074 }
1075out:
1076 write_unlock_irq(&tree->lock);
1077 return found;
1078}
1079
Chris Masona52d9a82007-08-27 16:49:44 -04001080/*
1081 * helper function to lock both pages and extents in the tree.
1082 * pages must be locked first.
1083 */
1084int lock_range(struct extent_map_tree *tree, u64 start, u64 end)
1085{
1086 unsigned long index = start >> PAGE_CACHE_SHIFT;
1087 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1088 struct page *page;
1089 int err;
1090
1091 while (index <= end_index) {
1092 page = grab_cache_page(tree->mapping, index);
1093 if (!page) {
1094 err = -ENOMEM;
1095 goto failed;
1096 }
1097 if (IS_ERR(page)) {
1098 err = PTR_ERR(page);
1099 goto failed;
1100 }
1101 index++;
1102 }
1103 lock_extent(tree, start, end, GFP_NOFS);
1104 return 0;
1105
1106failed:
1107 /*
1108 * we failed above in getting the page at 'index', so we undo here
1109 * up to but not including the page at 'index'
1110 */
1111 end_index = index;
1112 index = start >> PAGE_CACHE_SHIFT;
1113 while (index < end_index) {
1114 page = find_get_page(tree->mapping, index);
1115 unlock_page(page);
1116 page_cache_release(page);
1117 index++;
1118 }
1119 return err;
1120}
1121EXPORT_SYMBOL(lock_range);
1122
1123/*
1124 * helper function to unlock both pages and extents in the tree.
1125 */
1126int unlock_range(struct extent_map_tree *tree, u64 start, u64 end)
1127{
1128 unsigned long index = start >> PAGE_CACHE_SHIFT;
1129 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1130 struct page *page;
1131
1132 while (index <= end_index) {
1133 page = find_get_page(tree->mapping, index);
1134 unlock_page(page);
1135 page_cache_release(page);
1136 index++;
1137 }
1138 unlock_extent(tree, start, end, GFP_NOFS);
1139 return 0;
1140}
1141EXPORT_SYMBOL(unlock_range);
1142
Chris Mason07157aa2007-08-30 08:50:51 -04001143int set_state_private(struct extent_map_tree *tree, u64 start, u64 private)
1144{
1145 struct rb_node *node;
1146 struct extent_state *state;
1147 int ret = 0;
1148
1149 write_lock_irq(&tree->lock);
1150 /*
1151 * this search will find all the extents that end after
1152 * our range starts.
1153 */
1154 node = tree_search(&tree->state, start);
1155 if (!node || IS_ERR(node)) {
1156 ret = -ENOENT;
1157 goto out;
1158 }
1159 state = rb_entry(node, struct extent_state, rb_node);
1160 if (state->start != start) {
1161 ret = -ENOENT;
1162 goto out;
1163 }
1164 state->private = private;
1165out:
1166 write_unlock_irq(&tree->lock);
1167 return ret;
Chris Mason07157aa2007-08-30 08:50:51 -04001168}
1169
1170int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private)
1171{
1172 struct rb_node *node;
1173 struct extent_state *state;
1174 int ret = 0;
1175
1176 read_lock_irq(&tree->lock);
1177 /*
1178 * this search will find all the extents that end after
1179 * our range starts.
1180 */
1181 node = tree_search(&tree->state, start);
1182 if (!node || IS_ERR(node)) {
1183 ret = -ENOENT;
1184 goto out;
1185 }
1186 state = rb_entry(node, struct extent_state, rb_node);
1187 if (state->start != start) {
1188 ret = -ENOENT;
1189 goto out;
1190 }
1191 *private = state->private;
1192out:
1193 read_unlock_irq(&tree->lock);
1194 return ret;
1195}
1196
Chris Masona52d9a82007-08-27 16:49:44 -04001197/*
1198 * searches a range in the state tree for a given mask.
1199 * If 'filled' == 1, this returns 1 only if ever extent in the tree
1200 * has the bits set. Otherwise, 1 is returned if any bit in the
1201 * range is found set.
1202 */
Chris Mason1a5bc162007-10-15 16:15:26 -04001203int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
1204 int bits, int filled)
Chris Masona52d9a82007-08-27 16:49:44 -04001205{
1206 struct extent_state *state = NULL;
1207 struct rb_node *node;
1208 int bitset = 0;
1209
1210 read_lock_irq(&tree->lock);
1211 node = tree_search(&tree->state, start);
1212 while (node && start <= end) {
1213 state = rb_entry(node, struct extent_state, rb_node);
1214 if (state->start > end)
1215 break;
1216
1217 if (filled && state->start > start) {
1218 bitset = 0;
1219 break;
1220 }
1221 if (state->state & bits) {
1222 bitset = 1;
1223 if (!filled)
1224 break;
1225 } else if (filled) {
1226 bitset = 0;
1227 break;
1228 }
1229 start = state->end + 1;
1230 if (start > end)
1231 break;
1232 node = rb_next(node);
1233 }
1234 read_unlock_irq(&tree->lock);
1235 return bitset;
1236}
Chris Mason1a5bc162007-10-15 16:15:26 -04001237EXPORT_SYMBOL(test_range_bit);
Chris Masona52d9a82007-08-27 16:49:44 -04001238
1239/*
1240 * helper function to set a given page up to date if all the
1241 * extents in the tree for that page are up to date
1242 */
1243static int check_page_uptodate(struct extent_map_tree *tree,
1244 struct page *page)
1245{
Chris Mason35ebb932007-10-30 16:56:53 -04001246 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001247 u64 end = start + PAGE_CACHE_SIZE - 1;
1248 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1249 SetPageUptodate(page);
1250 return 0;
1251}
1252
1253/*
1254 * helper function to unlock a page if all the extents in the tree
1255 * for that page are unlocked
1256 */
1257static int check_page_locked(struct extent_map_tree *tree,
1258 struct page *page)
1259{
Chris Mason35ebb932007-10-30 16:56:53 -04001260 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001261 u64 end = start + PAGE_CACHE_SIZE - 1;
1262 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1263 unlock_page(page);
1264 return 0;
1265}
1266
1267/*
1268 * helper function to end page writeback if all the extents
1269 * in the tree for that page are done with writeback
1270 */
1271static int check_page_writeback(struct extent_map_tree *tree,
1272 struct page *page)
1273{
Chris Mason35ebb932007-10-30 16:56:53 -04001274 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001275 u64 end = start + PAGE_CACHE_SIZE - 1;
1276 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1277 end_page_writeback(page);
1278 return 0;
1279}
1280
1281/* lots and lots of room for performance fixes in the end_bio funcs */
1282
1283/*
1284 * after a writepage IO is done, we need to:
1285 * clear the uptodate bits on error
1286 * clear the writeback bits in the extent tree for this IO
1287 * end_page_writeback if the page has no more pending IO
1288 *
1289 * Scheduling is not allowed, so the extent state tree is expected
1290 * to have one and only one object corresponding to this IO.
1291 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001292#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1293static void end_bio_extent_writepage(struct bio *bio, int err)
1294#else
Chris Masona52d9a82007-08-27 16:49:44 -04001295static int end_bio_extent_writepage(struct bio *bio,
1296 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001297#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001298{
1299 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1300 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1301 struct extent_map_tree *tree = bio->bi_private;
1302 u64 start;
1303 u64 end;
1304 int whole_page;
1305
Jens Axboe0a2118d2007-10-19 09:23:05 -04001306#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001307 if (bio->bi_size)
1308 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001309#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001310
1311 do {
1312 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001313 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1314 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001315 end = start + bvec->bv_len - 1;
1316
1317 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1318 whole_page = 1;
1319 else
1320 whole_page = 0;
1321
1322 if (--bvec >= bio->bi_io_vec)
1323 prefetchw(&bvec->bv_page->flags);
1324
1325 if (!uptodate) {
1326 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1327 ClearPageUptodate(page);
1328 SetPageError(page);
1329 }
1330 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1331
1332 if (whole_page)
1333 end_page_writeback(page);
1334 else
1335 check_page_writeback(tree, page);
Christoph Hellwig0e2752a2007-09-10 20:02:33 -04001336 if (tree->ops && tree->ops->writepage_end_io_hook)
1337 tree->ops->writepage_end_io_hook(page, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001338 } while (bvec >= bio->bi_io_vec);
1339
1340 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001341#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001342 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001343#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001344}
1345
1346/*
1347 * after a readpage IO is done, we need to:
1348 * clear the uptodate bits on error
1349 * set the uptodate bits if things worked
1350 * set the page up to date if all extents in the tree are uptodate
1351 * clear the lock bit in the extent tree
1352 * unlock the page if there are no other extents locked for it
1353 *
1354 * Scheduling is not allowed, so the extent state tree is expected
1355 * to have one and only one object corresponding to this IO.
1356 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001357#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1358static void end_bio_extent_readpage(struct bio *bio, int err)
1359#else
Chris Masona52d9a82007-08-27 16:49:44 -04001360static int end_bio_extent_readpage(struct bio *bio,
1361 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001362#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001363{
Chris Mason07157aa2007-08-30 08:50:51 -04001364 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04001365 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1366 struct extent_map_tree *tree = bio->bi_private;
1367 u64 start;
1368 u64 end;
1369 int whole_page;
Chris Mason07157aa2007-08-30 08:50:51 -04001370 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04001371
Jens Axboe0a2118d2007-10-19 09:23:05 -04001372#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001373 if (bio->bi_size)
1374 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001375#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001376
1377 do {
1378 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001379 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1380 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001381 end = start + bvec->bv_len - 1;
1382
1383 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1384 whole_page = 1;
1385 else
1386 whole_page = 0;
1387
1388 if (--bvec >= bio->bi_io_vec)
1389 prefetchw(&bvec->bv_page->flags);
1390
Chris Mason07157aa2007-08-30 08:50:51 -04001391 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1392 ret = tree->ops->readpage_end_io_hook(page, start, end);
1393 if (ret)
1394 uptodate = 0;
1395 }
Chris Masona52d9a82007-08-27 16:49:44 -04001396 if (uptodate) {
1397 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1398 if (whole_page)
1399 SetPageUptodate(page);
1400 else
1401 check_page_uptodate(tree, page);
1402 } else {
1403 ClearPageUptodate(page);
1404 SetPageError(page);
1405 }
1406
1407 unlock_extent(tree, start, end, GFP_ATOMIC);
1408
1409 if (whole_page)
1410 unlock_page(page);
1411 else
1412 check_page_locked(tree, page);
1413 } while (bvec >= bio->bi_io_vec);
1414
1415 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001416#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001417 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001418#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001419}
1420
1421/*
1422 * IO done from prepare_write is pretty simple, we just unlock
1423 * the structs in the extent tree when done, and set the uptodate bits
1424 * as appropriate.
1425 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001426#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1427static void end_bio_extent_preparewrite(struct bio *bio, int err)
1428#else
Chris Masona52d9a82007-08-27 16:49:44 -04001429static int end_bio_extent_preparewrite(struct bio *bio,
1430 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001431#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001432{
1433 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1434 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1435 struct extent_map_tree *tree = bio->bi_private;
1436 u64 start;
1437 u64 end;
1438
Jens Axboe0a2118d2007-10-19 09:23:05 -04001439#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001440 if (bio->bi_size)
1441 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001442#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001443
1444 do {
1445 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001446 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1447 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001448 end = start + bvec->bv_len - 1;
1449
1450 if (--bvec >= bio->bi_io_vec)
1451 prefetchw(&bvec->bv_page->flags);
1452
1453 if (uptodate) {
1454 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1455 } else {
1456 ClearPageUptodate(page);
1457 SetPageError(page);
1458 }
1459
1460 unlock_extent(tree, start, end, GFP_ATOMIC);
1461
1462 } while (bvec >= bio->bi_io_vec);
1463
1464 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001465#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001466 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001467#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001468}
1469
Chris Masonb293f02e2007-11-01 19:45:34 -04001470static struct bio *
1471extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1472 gfp_t gfp_flags)
1473{
1474 struct bio *bio;
1475
1476 bio = bio_alloc(gfp_flags, nr_vecs);
1477
1478 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1479 while (!bio && (nr_vecs /= 2))
1480 bio = bio_alloc(gfp_flags, nr_vecs);
1481 }
1482
1483 if (bio) {
1484 bio->bi_bdev = bdev;
1485 bio->bi_sector = first_sector;
1486 }
1487 return bio;
1488}
1489
1490static int submit_one_bio(int rw, struct bio *bio)
1491{
1492 int ret = 0;
1493 bio_get(bio);
1494 submit_bio(rw, bio);
1495 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1496 ret = -EOPNOTSUPP;
1497 bio_put(bio);
1498 return ret;
1499}
1500
Chris Masona52d9a82007-08-27 16:49:44 -04001501static int submit_extent_page(int rw, struct extent_map_tree *tree,
1502 struct page *page, sector_t sector,
1503 size_t size, unsigned long offset,
1504 struct block_device *bdev,
Chris Masonb293f02e2007-11-01 19:45:34 -04001505 struct bio **bio_ret,
1506 int max_pages,
Chris Masona52d9a82007-08-27 16:49:44 -04001507 bio_end_io_t end_io_func)
1508{
Chris Masona52d9a82007-08-27 16:49:44 -04001509 int ret = 0;
Chris Masonb293f02e2007-11-01 19:45:34 -04001510 struct bio *bio;
1511 int nr;
Chris Masona52d9a82007-08-27 16:49:44 -04001512
Chris Masonb293f02e2007-11-01 19:45:34 -04001513 if (bio_ret && *bio_ret) {
1514 bio = *bio_ret;
1515 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1516 bio_add_page(bio, page, size, offset) < size) {
1517 ret = submit_one_bio(rw, bio);
1518 bio = NULL;
1519 } else {
1520 return 0;
1521 }
1522 }
1523 nr = min(max_pages, bio_get_nr_vecs(bdev));
1524 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1525 if (!bio) {
1526 printk("failed to allocate bio nr %d\n", nr);
1527 }
1528 bio_add_page(bio, page, size, offset);
Chris Masona52d9a82007-08-27 16:49:44 -04001529 bio->bi_end_io = end_io_func;
1530 bio->bi_private = tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04001531 if (bio_ret) {
1532 *bio_ret = bio;
1533 } else {
1534 ret = submit_one_bio(rw, bio);
1535 }
Chris Masona52d9a82007-08-27 16:49:44 -04001536
Chris Masona52d9a82007-08-27 16:49:44 -04001537 return ret;
1538}
1539
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001540void set_page_extent_mapped(struct page *page)
1541{
1542 if (!PagePrivate(page)) {
1543 SetPagePrivate(page);
1544 WARN_ON(!page->mapping->a_ops->invalidatepage);
Chris Mason19c00dd2007-10-15 16:19:22 -04001545 set_page_private(page, EXTENT_PAGE_PRIVATE);
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001546 page_cache_get(page);
1547 }
1548}
1549
Chris Masona52d9a82007-08-27 16:49:44 -04001550/*
1551 * basic readpage implementation. Locked extent state structs are inserted
1552 * into the tree that are removed when the IO is done (by the end_io
1553 * handlers)
1554 */
1555int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
1556 get_extent_t *get_extent)
1557{
1558 struct inode *inode = page->mapping->host;
Chris Mason35ebb932007-10-30 16:56:53 -04001559 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001560 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1561 u64 end;
1562 u64 cur = start;
1563 u64 extent_offset;
1564 u64 last_byte = i_size_read(inode);
1565 u64 block_start;
1566 u64 cur_end;
1567 sector_t sector;
1568 struct extent_map *em;
1569 struct block_device *bdev;
1570 int ret;
1571 int nr = 0;
1572 size_t page_offset = 0;
1573 size_t iosize;
1574 size_t blocksize = inode->i_sb->s_blocksize;
1575
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001576 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001577
1578 end = page_end;
1579 lock_extent(tree, start, end, GFP_NOFS);
1580
1581 while (cur <= end) {
1582 if (cur >= last_byte) {
1583 iosize = PAGE_CACHE_SIZE - page_offset;
1584 zero_user_page(page, page_offset, iosize, KM_USER0);
1585 set_extent_uptodate(tree, cur, cur + iosize - 1,
1586 GFP_NOFS);
1587 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1588 break;
1589 }
1590 em = get_extent(inode, page, page_offset, cur, end, 0);
1591 if (IS_ERR(em) || !em) {
1592 SetPageError(page);
1593 unlock_extent(tree, cur, end, GFP_NOFS);
1594 break;
1595 }
1596
1597 extent_offset = cur - em->start;
1598 BUG_ON(em->end < cur);
1599 BUG_ON(end < cur);
1600
1601 iosize = min(em->end - cur, end - cur) + 1;
1602 cur_end = min(em->end, end);
1603 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1604 sector = (em->block_start + extent_offset) >> 9;
1605 bdev = em->bdev;
1606 block_start = em->block_start;
1607 free_extent_map(em);
1608 em = NULL;
1609
1610 /* we've found a hole, just zero and go on */
Chris Mason5f39d392007-10-15 16:14:19 -04001611 if (block_start == EXTENT_MAP_HOLE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001612 zero_user_page(page, page_offset, iosize, KM_USER0);
1613 set_extent_uptodate(tree, cur, cur + iosize - 1,
1614 GFP_NOFS);
1615 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1616 cur = cur + iosize;
1617 page_offset += iosize;
1618 continue;
1619 }
1620 /* the get_extent function already copied into the page */
1621 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1622 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1623 cur = cur + iosize;
1624 page_offset += iosize;
1625 continue;
1626 }
1627
Chris Mason07157aa2007-08-30 08:50:51 -04001628 ret = 0;
1629 if (tree->ops && tree->ops->readpage_io_hook) {
1630 ret = tree->ops->readpage_io_hook(page, cur,
1631 cur + iosize - 1);
1632 }
1633 if (!ret) {
1634 ret = submit_extent_page(READ, tree, page,
1635 sector, iosize, page_offset,
Chris Masonb293f02e2007-11-01 19:45:34 -04001636 bdev, NULL, 1,
1637 end_bio_extent_readpage);
Chris Mason07157aa2007-08-30 08:50:51 -04001638 }
Chris Masona52d9a82007-08-27 16:49:44 -04001639 if (ret)
1640 SetPageError(page);
1641 cur = cur + iosize;
1642 page_offset += iosize;
1643 nr++;
1644 }
1645 if (!nr) {
1646 if (!PageError(page))
1647 SetPageUptodate(page);
1648 unlock_page(page);
1649 }
1650 return 0;
1651}
1652EXPORT_SYMBOL(extent_read_full_page);
1653
1654/*
1655 * the writepage semantics are similar to regular writepage. extent
1656 * records are inserted to lock ranges in the tree, and as dirty areas
1657 * are found, they are marked writeback. Then the lock bits are removed
1658 * and the end_io handler clears the writeback ranges
1659 */
Chris Masonb293f02e2007-11-01 19:45:34 -04001660static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1661 void *data)
Chris Masona52d9a82007-08-27 16:49:44 -04001662{
1663 struct inode *inode = page->mapping->host;
Chris Masonb293f02e2007-11-01 19:45:34 -04001664 struct extent_page_data *epd = data;
1665 struct extent_map_tree *tree = epd->tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001666 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001667 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1668 u64 end;
1669 u64 cur = start;
1670 u64 extent_offset;
1671 u64 last_byte = i_size_read(inode);
1672 u64 block_start;
Chris Mason179e29e2007-11-01 11:28:41 -04001673 u64 iosize;
Chris Masona52d9a82007-08-27 16:49:44 -04001674 sector_t sector;
1675 struct extent_map *em;
1676 struct block_device *bdev;
1677 int ret;
1678 int nr = 0;
1679 size_t page_offset = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04001680 size_t blocksize;
1681 loff_t i_size = i_size_read(inode);
1682 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
Chris Masonb888db2b2007-08-27 16:49:44 -04001683 u64 nr_delalloc;
1684 u64 delalloc_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001685
Chris Masonb888db2b2007-08-27 16:49:44 -04001686 WARN_ON(!PageLocked(page));
Chris Masona52d9a82007-08-27 16:49:44 -04001687 if (page->index > end_index) {
1688 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1689 unlock_page(page);
1690 return 0;
1691 }
1692
1693 if (page->index == end_index) {
1694 size_t offset = i_size & (PAGE_CACHE_SIZE - 1);
1695 zero_user_page(page, offset,
1696 PAGE_CACHE_SIZE - offset, KM_USER0);
1697 }
1698
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001699 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001700
Chris Masona52d9a82007-08-27 16:49:44 -04001701 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Masonb888db2b2007-08-27 16:49:44 -04001702 nr_delalloc = find_lock_delalloc_range(tree, start, page_end + 1,
1703 &delalloc_end,
1704 128 * 1024 * 1024);
1705 if (nr_delalloc) {
Chris Mason07157aa2007-08-30 08:50:51 -04001706 tree->ops->fill_delalloc(inode, start, delalloc_end);
Chris Masonb888db2b2007-08-27 16:49:44 -04001707 if (delalloc_end >= page_end + 1) {
1708 clear_extent_bit(tree, page_end + 1, delalloc_end,
1709 EXTENT_LOCKED | EXTENT_DELALLOC,
1710 1, 0, GFP_NOFS);
1711 }
1712 clear_extent_bit(tree, start, page_end, EXTENT_DELALLOC,
1713 0, 0, GFP_NOFS);
1714 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1715 printk("found delalloc bits after clear extent_bit\n");
1716 }
1717 } else if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1718 printk("found delalloc bits after find_delalloc_range returns 0\n");
1719 }
1720
1721 end = page_end;
1722 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1723 printk("found delalloc bits after lock_extent\n");
1724 }
Chris Masona52d9a82007-08-27 16:49:44 -04001725
1726 if (last_byte <= start) {
1727 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1728 goto done;
1729 }
1730
1731 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
1732 blocksize = inode->i_sb->s_blocksize;
1733
1734 while (cur <= end) {
1735 if (cur >= last_byte) {
1736 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
1737 break;
1738 }
Chris Masonb293f02e2007-11-01 19:45:34 -04001739 em = epd->get_extent(inode, page, page_offset, cur, end, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04001740 if (IS_ERR(em) || !em) {
1741 SetPageError(page);
1742 break;
1743 }
1744
1745 extent_offset = cur - em->start;
1746 BUG_ON(em->end < cur);
1747 BUG_ON(end < cur);
1748 iosize = min(em->end - cur, end - cur) + 1;
1749 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1750 sector = (em->block_start + extent_offset) >> 9;
1751 bdev = em->bdev;
1752 block_start = em->block_start;
1753 free_extent_map(em);
1754 em = NULL;
1755
Chris Mason5f39d392007-10-15 16:14:19 -04001756 if (block_start == EXTENT_MAP_HOLE ||
1757 block_start == EXTENT_MAP_INLINE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001758 clear_extent_dirty(tree, cur,
1759 cur + iosize - 1, GFP_NOFS);
1760 cur = cur + iosize;
1761 page_offset += iosize;
1762 continue;
1763 }
1764
1765 /* leave this out until we have a page_mkwrite call */
1766 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
1767 EXTENT_DIRTY, 0)) {
1768 cur = cur + iosize;
1769 page_offset += iosize;
1770 continue;
1771 }
1772 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
Christoph Hellwigb06355f2007-09-10 20:02:32 -04001773 if (tree->ops && tree->ops->writepage_io_hook) {
1774 ret = tree->ops->writepage_io_hook(page, cur,
1775 cur + iosize - 1);
1776 } else {
1777 ret = 0;
1778 }
Chris Masona52d9a82007-08-27 16:49:44 -04001779 if (ret)
1780 SetPageError(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001781 else {
Chris Masonb293f02e2007-11-01 19:45:34 -04001782 unsigned long nr = end_index + 1;
Chris Mason07157aa2007-08-30 08:50:51 -04001783 set_range_writeback(tree, cur, cur + iosize - 1);
Chris Masonb293f02e2007-11-01 19:45:34 -04001784
Chris Mason07157aa2007-08-30 08:50:51 -04001785 ret = submit_extent_page(WRITE, tree, page, sector,
1786 iosize, page_offset, bdev,
Chris Masonb293f02e2007-11-01 19:45:34 -04001787 &epd->bio, nr,
Chris Mason07157aa2007-08-30 08:50:51 -04001788 end_bio_extent_writepage);
1789 if (ret)
1790 SetPageError(page);
1791 }
Chris Masona52d9a82007-08-27 16:49:44 -04001792 cur = cur + iosize;
1793 page_offset += iosize;
1794 nr++;
1795 }
1796done:
Chris Masona52d9a82007-08-27 16:49:44 -04001797 unlock_extent(tree, start, page_end, GFP_NOFS);
1798 unlock_page(page);
1799 return 0;
1800}
Chris Masonb293f02e2007-11-01 19:45:34 -04001801
1802int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
1803 get_extent_t *get_extent,
1804 struct writeback_control *wbc)
1805{
1806 int ret;
1807 struct extent_page_data epd = {
1808 .bio = NULL,
1809 .tree = tree,
1810 .get_extent = get_extent,
1811 };
1812
1813 ret = __extent_writepage(page, wbc, &epd);
1814 if (epd.bio)
1815 submit_one_bio(WRITE, epd.bio);
1816 return ret;
1817}
Chris Masona52d9a82007-08-27 16:49:44 -04001818EXPORT_SYMBOL(extent_write_full_page);
1819
Chris Masonb293f02e2007-11-01 19:45:34 -04001820int extent_writepages(struct extent_map_tree *tree,
1821 struct address_space *mapping,
1822 get_extent_t *get_extent,
1823 struct writeback_control *wbc)
1824{
1825 int ret;
1826 struct extent_page_data epd = {
1827 .bio = NULL,
1828 .tree = tree,
1829 .get_extent = get_extent,
1830 };
1831
1832 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
1833 if (epd.bio)
1834 submit_one_bio(WRITE, epd.bio);
1835 return ret;
1836}
1837EXPORT_SYMBOL(extent_writepages);
1838
Chris Masona52d9a82007-08-27 16:49:44 -04001839/*
1840 * basic invalidatepage code, this waits on any locked or writeback
1841 * ranges corresponding to the page, and then deletes any extent state
1842 * records from the tree
1843 */
1844int extent_invalidatepage(struct extent_map_tree *tree,
1845 struct page *page, unsigned long offset)
1846{
Chris Mason35ebb932007-10-30 16:56:53 -04001847 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Masona52d9a82007-08-27 16:49:44 -04001848 u64 end = start + PAGE_CACHE_SIZE - 1;
1849 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
1850
1851 start += (offset + blocksize -1) & ~(blocksize - 1);
1852 if (start > end)
1853 return 0;
1854
1855 lock_extent(tree, start, end, GFP_NOFS);
1856 wait_on_extent_writeback(tree, start, end);
Chris Mason2bf5a722007-08-30 11:54:02 -04001857 clear_extent_bit(tree, start, end,
1858 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Masona52d9a82007-08-27 16:49:44 -04001859 1, 1, GFP_NOFS);
1860 return 0;
1861}
1862EXPORT_SYMBOL(extent_invalidatepage);
1863
1864/*
1865 * simple commit_write call, set_range_dirty is used to mark both
1866 * the pages and the extent records as dirty
1867 */
1868int extent_commit_write(struct extent_map_tree *tree,
1869 struct inode *inode, struct page *page,
1870 unsigned from, unsigned to)
1871{
1872 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1873
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001874 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001875 set_page_dirty(page);
1876
1877 if (pos > inode->i_size) {
1878 i_size_write(inode, pos);
1879 mark_inode_dirty(inode);
1880 }
1881 return 0;
1882}
1883EXPORT_SYMBOL(extent_commit_write);
1884
1885int extent_prepare_write(struct extent_map_tree *tree,
1886 struct inode *inode, struct page *page,
1887 unsigned from, unsigned to, get_extent_t *get_extent)
1888{
Chris Mason35ebb932007-10-30 16:56:53 -04001889 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001890 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1891 u64 block_start;
1892 u64 orig_block_start;
1893 u64 block_end;
1894 u64 cur_end;
1895 struct extent_map *em;
1896 unsigned blocksize = 1 << inode->i_blkbits;
1897 size_t page_offset = 0;
1898 size_t block_off_start;
1899 size_t block_off_end;
1900 int err = 0;
1901 int iocount = 0;
1902 int ret = 0;
1903 int isnew;
1904
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001905 set_page_extent_mapped(page);
1906
Chris Masona52d9a82007-08-27 16:49:44 -04001907 block_start = (page_start + from) & ~((u64)blocksize - 1);
1908 block_end = (page_start + to - 1) | (blocksize - 1);
1909 orig_block_start = block_start;
1910
1911 lock_extent(tree, page_start, page_end, GFP_NOFS);
1912 while(block_start <= block_end) {
1913 em = get_extent(inode, page, page_offset, block_start,
1914 block_end, 1);
1915 if (IS_ERR(em) || !em) {
1916 goto err;
1917 }
1918 cur_end = min(block_end, em->end);
1919 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
1920 block_off_end = block_off_start + blocksize;
1921 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
1922
1923 if (!PageUptodate(page) && isnew &&
1924 (block_off_end > to || block_off_start < from)) {
1925 void *kaddr;
1926
1927 kaddr = kmap_atomic(page, KM_USER0);
1928 if (block_off_end > to)
1929 memset(kaddr + to, 0, block_off_end - to);
1930 if (block_off_start < from)
1931 memset(kaddr + block_off_start, 0,
1932 from - block_off_start);
1933 flush_dcache_page(page);
1934 kunmap_atomic(kaddr, KM_USER0);
1935 }
1936 if (!isnew && !PageUptodate(page) &&
1937 (block_off_end > to || block_off_start < from) &&
1938 !test_range_bit(tree, block_start, cur_end,
1939 EXTENT_UPTODATE, 1)) {
1940 u64 sector;
1941 u64 extent_offset = block_start - em->start;
1942 size_t iosize;
1943 sector = (em->block_start + extent_offset) >> 9;
1944 iosize = (cur_end - block_start + blocksize - 1) &
1945 ~((u64)blocksize - 1);
1946 /*
1947 * we've already got the extent locked, but we
1948 * need to split the state such that our end_bio
1949 * handler can clear the lock.
1950 */
1951 set_extent_bit(tree, block_start,
1952 block_start + iosize - 1,
1953 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
1954 ret = submit_extent_page(READ, tree, page,
1955 sector, iosize, page_offset, em->bdev,
Chris Masonb293f02e2007-11-01 19:45:34 -04001956 NULL, 1,
Chris Masona52d9a82007-08-27 16:49:44 -04001957 end_bio_extent_preparewrite);
1958 iocount++;
1959 block_start = block_start + iosize;
1960 } else {
1961 set_extent_uptodate(tree, block_start, cur_end,
1962 GFP_NOFS);
1963 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
1964 block_start = cur_end + 1;
1965 }
1966 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
1967 free_extent_map(em);
1968 }
1969 if (iocount) {
1970 wait_extent_bit(tree, orig_block_start,
1971 block_end, EXTENT_LOCKED);
1972 }
1973 check_page_uptodate(tree, page);
1974err:
1975 /* FIXME, zero out newly allocated blocks on error */
1976 return err;
1977}
1978EXPORT_SYMBOL(extent_prepare_write);
1979
1980/*
1981 * a helper for releasepage. As long as there are no locked extents
1982 * in the range corresponding to the page, both state records and extent
1983 * map records are removed
1984 */
1985int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page)
1986{
1987 struct extent_map *em;
Chris Mason35ebb932007-10-30 16:56:53 -04001988 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001989 u64 end = start + PAGE_CACHE_SIZE - 1;
1990 u64 orig_start = start;
Chris Masonb888db2b2007-08-27 16:49:44 -04001991 int ret = 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001992
1993 while (start <= end) {
1994 em = lookup_extent_mapping(tree, start, end);
1995 if (!em || IS_ERR(em))
1996 break;
Chris Masonb888db2b2007-08-27 16:49:44 -04001997 if (!test_range_bit(tree, em->start, em->end,
1998 EXTENT_LOCKED, 0)) {
1999 remove_extent_mapping(tree, em);
2000 /* once for the rb tree */
Chris Masona52d9a82007-08-27 16:49:44 -04002001 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04002002 }
Chris Masona52d9a82007-08-27 16:49:44 -04002003 start = em->end + 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002004 /* once for us */
2005 free_extent_map(em);
2006 }
Chris Masonb888db2b2007-08-27 16:49:44 -04002007 if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0))
2008 ret = 0;
2009 else
2010 clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
2011 1, 1, GFP_NOFS);
2012 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002013}
2014EXPORT_SYMBOL(try_release_extent_mapping);
2015
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002016sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2017 get_extent_t *get_extent)
2018{
2019 struct inode *inode = mapping->host;
2020 u64 start = iblock << inode->i_blkbits;
2021 u64 end = start + (1 << inode->i_blkbits) - 1;
Yanc67cda12007-10-29 11:41:05 -04002022 sector_t sector = 0;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002023 struct extent_map *em;
2024
2025 em = get_extent(inode, NULL, 0, start, end, 0);
2026 if (!em || IS_ERR(em))
2027 return 0;
2028
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002029 if (em->block_start == EXTENT_MAP_INLINE ||
Chris Mason5f39d392007-10-15 16:14:19 -04002030 em->block_start == EXTENT_MAP_HOLE)
Yanc67cda12007-10-29 11:41:05 -04002031 goto out;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002032
Yanc67cda12007-10-29 11:41:05 -04002033 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2034out:
2035 free_extent_map(em);
2036 return sector;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002037}
Chris Mason5f39d392007-10-15 16:14:19 -04002038
Chris Mason4dc119042007-10-15 16:18:14 -04002039static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002040{
Chris Mason4dc119042007-10-15 16:18:14 -04002041 if (list_empty(&eb->lru)) {
2042 extent_buffer_get(eb);
2043 list_add(&eb->lru, &tree->buffer_lru);
2044 tree->lru_size++;
2045 if (tree->lru_size >= BUFFER_LRU_MAX) {
2046 struct extent_buffer *rm;
2047 rm = list_entry(tree->buffer_lru.prev,
2048 struct extent_buffer, lru);
2049 tree->lru_size--;
2050 list_del(&rm->lru);
2051 free_extent_buffer(rm);
2052 }
2053 } else
2054 list_move(&eb->lru, &tree->buffer_lru);
2055 return 0;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002056}
Chris Mason4dc119042007-10-15 16:18:14 -04002057static struct extent_buffer *find_lru(struct extent_map_tree *tree,
2058 u64 start, unsigned long len)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002059{
Chris Mason4dc119042007-10-15 16:18:14 -04002060 struct list_head *lru = &tree->buffer_lru;
2061 struct list_head *cur = lru->next;
2062 struct extent_buffer *eb;
Chris Masonf510cfe2007-10-15 16:14:48 -04002063
Chris Mason4dc119042007-10-15 16:18:14 -04002064 if (list_empty(lru))
2065 return NULL;
Chris Masonf510cfe2007-10-15 16:14:48 -04002066
Chris Mason4dc119042007-10-15 16:18:14 -04002067 do {
2068 eb = list_entry(cur, struct extent_buffer, lru);
2069 if (eb->start == start && eb->len == len) {
2070 extent_buffer_get(eb);
2071 return eb;
2072 }
2073 cur = cur->next;
2074 } while (cur != lru);
2075 return NULL;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002076}
2077
Chris Masondb945352007-10-15 16:15:53 -04002078static inline unsigned long num_extent_pages(u64 start, u64 len)
2079{
2080 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2081 (start >> PAGE_CACHE_SHIFT);
2082}
Chris Mason4dc119042007-10-15 16:18:14 -04002083
2084static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2085 unsigned long i)
2086{
2087 struct page *p;
Chris Mason3685f792007-10-19 09:23:27 -04002088 struct address_space *mapping;
Chris Mason4dc119042007-10-15 16:18:14 -04002089
2090 if (i == 0)
Chris Mason810191f2007-10-15 16:18:55 -04002091 return eb->first_page;
Chris Mason4dc119042007-10-15 16:18:14 -04002092 i += eb->start >> PAGE_CACHE_SHIFT;
Chris Mason3685f792007-10-19 09:23:27 -04002093 mapping = eb->first_page->mapping;
2094 read_lock_irq(&mapping->tree_lock);
2095 p = radix_tree_lookup(&mapping->page_tree, i);
2096 read_unlock_irq(&mapping->tree_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002097 return p;
2098}
2099
2100static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
2101 u64 start,
2102 unsigned long len,
2103 gfp_t mask)
2104{
2105 struct extent_buffer *eb = NULL;
2106
2107 spin_lock(&tree->lru_lock);
2108 eb = find_lru(tree, start, len);
Chris Mason19c00dd2007-10-15 16:19:22 -04002109 if (eb) {
Chris Mason4dc119042007-10-15 16:18:14 -04002110 goto lru_add;
Chris Mason19c00dd2007-10-15 16:19:22 -04002111 }
Chris Mason4dc119042007-10-15 16:18:14 -04002112 spin_unlock(&tree->lru_lock);
2113
2114 if (eb) {
2115 memset(eb, 0, sizeof(*eb));
2116 } else {
2117 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2118 }
2119 INIT_LIST_HEAD(&eb->lru);
2120 eb->start = start;
2121 eb->len = len;
2122 atomic_set(&eb->refs, 1);
2123
2124 spin_lock(&tree->lru_lock);
2125lru_add:
2126 add_lru(tree, eb);
2127 spin_unlock(&tree->lru_lock);
2128 return eb;
2129}
2130
2131static void __free_extent_buffer(struct extent_buffer *eb)
2132{
2133 kmem_cache_free(extent_buffer_cache, eb);
2134}
2135
Chris Mason5f39d392007-10-15 16:14:19 -04002136struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
2137 u64 start, unsigned long len,
Chris Mason19c00dd2007-10-15 16:19:22 -04002138 struct page *page0,
Chris Mason5f39d392007-10-15 16:14:19 -04002139 gfp_t mask)
2140{
Chris Masondb945352007-10-15 16:15:53 -04002141 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 unsigned long i;
2143 unsigned long index = start >> PAGE_CACHE_SHIFT;
2144 struct extent_buffer *eb;
2145 struct page *p;
2146 struct address_space *mapping = tree->mapping;
Yan65555a02007-10-25 15:42:57 -04002147 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002148
Chris Mason4dc119042007-10-15 16:18:14 -04002149 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002150 if (!eb || IS_ERR(eb))
2151 return NULL;
2152
Chris Mason4dc119042007-10-15 16:18:14 -04002153 if (eb->flags & EXTENT_BUFFER_FILLED)
2154 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -04002155
Chris Mason19c00dd2007-10-15 16:19:22 -04002156 if (page0) {
2157 eb->first_page = page0;
2158 i = 1;
2159 index++;
2160 page_cache_get(page0);
Chris Masonff79f812007-10-15 16:22:25 -04002161 mark_page_accessed(page0);
Chris Mason19c00dd2007-10-15 16:19:22 -04002162 set_page_extent_mapped(page0);
2163 set_page_private(page0, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2164 len << 2);
2165 } else {
2166 i = 0;
2167 }
2168 for (; i < num_pages; i++, index++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002169 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002170 if (!p) {
Chris Masondb945352007-10-15 16:15:53 -04002171 WARN_ON(1);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002172 /* make sure the free only frees the pages we've
2173 * grabbed a reference on
2174 */
2175 eb->len = i << PAGE_CACHE_SHIFT;
2176 eb->start &= ~((u64)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002177 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002178 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002179 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002180 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002181 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002182 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002183 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2184 len << 2);
2185 } else {
2186 set_page_private(p, EXTENT_PAGE_PRIVATE);
2187 }
Chris Mason5f39d392007-10-15 16:14:19 -04002188 if (!PageUptodate(p))
2189 uptodate = 0;
2190 unlock_page(p);
2191 }
2192 if (uptodate)
2193 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002194 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason5f39d392007-10-15 16:14:19 -04002195 return eb;
2196fail:
2197 free_extent_buffer(eb);
2198 return NULL;
2199}
2200EXPORT_SYMBOL(alloc_extent_buffer);
2201
2202struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
2203 u64 start, unsigned long len,
2204 gfp_t mask)
2205{
Chris Masondb945352007-10-15 16:15:53 -04002206 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason19c00dd2007-10-15 16:19:22 -04002207 unsigned long i; unsigned long index = start >> PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002208 struct extent_buffer *eb;
2209 struct page *p;
2210 struct address_space *mapping = tree->mapping;
Chris Mason14048ed2007-10-15 16:16:28 -04002211 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002212
Chris Mason4dc119042007-10-15 16:18:14 -04002213 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 if (!eb || IS_ERR(eb))
2215 return NULL;
2216
Chris Mason4dc119042007-10-15 16:18:14 -04002217 if (eb->flags & EXTENT_BUFFER_FILLED)
2218 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -04002219
2220 for (i = 0; i < num_pages; i++, index++) {
Chris Mason14048ed2007-10-15 16:16:28 -04002221 p = find_lock_page(mapping, index);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002222 if (!p) {
2223 /* make sure the free only frees the pages we've
2224 * grabbed a reference on
2225 */
2226 eb->len = i << PAGE_CACHE_SHIFT;
2227 eb->start &= ~((u64)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002228 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002229 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002230 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002231 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002232
2233 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002234 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002235 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2236 len << 2);
2237 } else {
2238 set_page_private(p, EXTENT_PAGE_PRIVATE);
2239 }
2240
Chris Mason14048ed2007-10-15 16:16:28 -04002241 if (!PageUptodate(p))
2242 uptodate = 0;
2243 unlock_page(p);
Chris Mason5f39d392007-10-15 16:14:19 -04002244 }
Chris Mason14048ed2007-10-15 16:16:28 -04002245 if (uptodate)
2246 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002247 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason5f39d392007-10-15 16:14:19 -04002248 return eb;
2249fail:
2250 free_extent_buffer(eb);
2251 return NULL;
2252}
2253EXPORT_SYMBOL(find_extent_buffer);
2254
2255void free_extent_buffer(struct extent_buffer *eb)
2256{
2257 unsigned long i;
2258 unsigned long num_pages;
2259
2260 if (!eb)
2261 return;
2262
2263 if (!atomic_dec_and_test(&eb->refs))
2264 return;
2265
Chris Masondb945352007-10-15 16:15:53 -04002266 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002267
Chris Mason09e71a32007-10-15 16:17:04 -04002268 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002269 page_cache_release(extent_buffer_page(eb, i));
Chris Mason5f39d392007-10-15 16:14:19 -04002270 }
Chris Mason6d36dcd2007-10-15 16:14:37 -04002271 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002272}
2273EXPORT_SYMBOL(free_extent_buffer);
2274
2275int clear_extent_buffer_dirty(struct extent_map_tree *tree,
2276 struct extent_buffer *eb)
2277{
2278 int set;
2279 unsigned long i;
2280 unsigned long num_pages;
2281 struct page *page;
2282
2283 u64 start = eb->start;
2284 u64 end = start + eb->len - 1;
2285
2286 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
Chris Masondb945352007-10-15 16:15:53 -04002287 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002288
2289 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002290 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002291 lock_page(page);
2292 /*
2293 * if we're on the last page or the first page and the
2294 * block isn't aligned on a page boundary, do extra checks
2295 * to make sure we don't clean page that is partially dirty
2296 */
2297 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2298 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002299 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason35ebb932007-10-30 16:56:53 -04002300 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002301 end = start + PAGE_CACHE_SIZE - 1;
2302 if (test_range_bit(tree, start, end,
2303 EXTENT_DIRTY, 0)) {
2304 unlock_page(page);
2305 continue;
2306 }
2307 }
2308 clear_page_dirty_for_io(page);
2309 unlock_page(page);
2310 }
2311 return 0;
2312}
2313EXPORT_SYMBOL(clear_extent_buffer_dirty);
2314
2315int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
2316 struct extent_buffer *eb)
2317{
2318 return wait_on_extent_writeback(tree, eb->start,
2319 eb->start + eb->len - 1);
2320}
2321EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2322
2323int set_extent_buffer_dirty(struct extent_map_tree *tree,
2324 struct extent_buffer *eb)
2325{
Chris Mason810191f2007-10-15 16:18:55 -04002326 unsigned long i;
2327 unsigned long num_pages;
2328
2329 num_pages = num_extent_pages(eb->start, eb->len);
2330 for (i = 0; i < num_pages; i++) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002331 struct page *page = extent_buffer_page(eb, i);
2332 /* writepage may need to do something special for the
2333 * first page, we have to make sure page->private is
2334 * properly set. releasepage may drop page->private
2335 * on us if the page isn't already dirty.
2336 */
2337 if (i == 0) {
2338 lock_page(page);
2339 set_page_private(page,
2340 EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2341 eb->len << 2);
2342 }
Chris Mason810191f2007-10-15 16:18:55 -04002343 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Mason19c00dd2007-10-15 16:19:22 -04002344 if (i == 0)
2345 unlock_page(page);
Chris Mason810191f2007-10-15 16:18:55 -04002346 }
2347 return set_extent_dirty(tree, eb->start,
2348 eb->start + eb->len - 1, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002349}
2350EXPORT_SYMBOL(set_extent_buffer_dirty);
2351
2352int set_extent_buffer_uptodate(struct extent_map_tree *tree,
2353 struct extent_buffer *eb)
2354{
2355 unsigned long i;
2356 struct page *page;
2357 unsigned long num_pages;
2358
Chris Masondb945352007-10-15 16:15:53 -04002359 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002360
2361 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2362 GFP_NOFS);
2363 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002364 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002365 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2366 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002367 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason5f39d392007-10-15 16:14:19 -04002368 check_page_uptodate(tree, page);
2369 continue;
2370 }
2371 SetPageUptodate(page);
2372 }
2373 return 0;
2374}
2375EXPORT_SYMBOL(set_extent_buffer_uptodate);
2376
2377int extent_buffer_uptodate(struct extent_map_tree *tree,
2378 struct extent_buffer *eb)
2379{
2380 if (eb->flags & EXTENT_UPTODATE)
2381 return 1;
2382 return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
2383 EXTENT_UPTODATE, 1);
2384}
2385EXPORT_SYMBOL(extent_buffer_uptodate);
2386
2387int read_extent_buffer_pages(struct extent_map_tree *tree,
Chris Mason19c00dd2007-10-15 16:19:22 -04002388 struct extent_buffer *eb,
2389 u64 start,
2390 int wait)
Chris Mason5f39d392007-10-15 16:14:19 -04002391{
2392 unsigned long i;
Chris Mason19c00dd2007-10-15 16:19:22 -04002393 unsigned long start_i;
Chris Mason5f39d392007-10-15 16:14:19 -04002394 struct page *page;
2395 int err;
2396 int ret = 0;
2397 unsigned long num_pages;
2398
2399 if (eb->flags & EXTENT_UPTODATE)
2400 return 0;
2401
Chris Mason14048ed2007-10-15 16:16:28 -04002402 if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason5f39d392007-10-15 16:14:19 -04002403 EXTENT_UPTODATE, 1)) {
2404 return 0;
2405 }
Chris Mason19c00dd2007-10-15 16:19:22 -04002406 if (start) {
2407 WARN_ON(start < eb->start);
2408 start_i = (start >> PAGE_CACHE_SHIFT) -
2409 (eb->start >> PAGE_CACHE_SHIFT);
2410 } else {
2411 start_i = 0;
2412 }
Chris Mason5f39d392007-10-15 16:14:19 -04002413
Chris Masondb945352007-10-15 16:15:53 -04002414 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason19c00dd2007-10-15 16:19:22 -04002415 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002416 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002417 if (PageUptodate(page)) {
2418 continue;
2419 }
2420 if (!wait) {
2421 if (TestSetPageLocked(page)) {
2422 continue;
2423 }
2424 } else {
2425 lock_page(page);
2426 }
2427 if (!PageUptodate(page)) {
2428 err = page->mapping->a_ops->readpage(NULL, page);
2429 if (err) {
2430 ret = err;
2431 }
2432 } else {
2433 unlock_page(page);
2434 }
2435 }
2436
2437 if (ret || !wait) {
2438 return ret;
2439 }
2440
Chris Mason19c00dd2007-10-15 16:19:22 -04002441 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002442 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002443 wait_on_page_locked(page);
2444 if (!PageUptodate(page)) {
2445 ret = -EIO;
2446 }
2447 }
Chris Mason4dc119042007-10-15 16:18:14 -04002448 if (!ret)
2449 eb->flags |= EXTENT_UPTODATE;
Chris Mason5f39d392007-10-15 16:14:19 -04002450 return ret;
2451}
2452EXPORT_SYMBOL(read_extent_buffer_pages);
2453
2454void read_extent_buffer(struct extent_buffer *eb, void *dstv,
2455 unsigned long start,
2456 unsigned long len)
2457{
2458 size_t cur;
2459 size_t offset;
2460 struct page *page;
2461 char *kaddr;
2462 char *dst = (char *)dstv;
2463 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2464 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Mason14048ed2007-10-15 16:16:28 -04002465 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002466
2467 WARN_ON(start > eb->len);
2468 WARN_ON(start + len > eb->start + eb->len);
2469
Chris Mason3685f792007-10-19 09:23:27 -04002470 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002471
2472 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002473 page = extent_buffer_page(eb, i);
Chris Mason14048ed2007-10-15 16:16:28 -04002474 if (!PageUptodate(page)) {
2475 printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len);
2476 WARN_ON(1);
2477 }
Chris Mason5f39d392007-10-15 16:14:19 -04002478 WARN_ON(!PageUptodate(page));
2479
2480 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Mason59d169e2007-10-19 09:23:09 -04002481 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002482 memcpy(dst, kaddr + offset, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002483 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002484
2485 dst += cur;
2486 len -= cur;
2487 offset = 0;
2488 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002489 }
2490}
2491EXPORT_SYMBOL(read_extent_buffer);
2492
Chris Mason19c00dd2007-10-15 16:19:22 -04002493int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masondb945352007-10-15 16:15:53 -04002494 unsigned long min_len, char **token, char **map,
2495 unsigned long *map_start,
2496 unsigned long *map_len, int km)
Chris Mason5f39d392007-10-15 16:14:19 -04002497{
Chris Mason479965d2007-10-15 16:14:27 -04002498 size_t offset = start & (PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002499 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002500 struct page *p;
Chris Mason5f39d392007-10-15 16:14:19 -04002501 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2502 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Yan65555a02007-10-25 15:42:57 -04002503 unsigned long end_i = (start_offset + start + min_len - 1) >>
Chris Mason810191f2007-10-15 16:18:55 -04002504 PAGE_CACHE_SHIFT;
Chris Mason479965d2007-10-15 16:14:27 -04002505
2506 if (i != end_i)
2507 return -EINVAL;
Chris Mason5f39d392007-10-15 16:14:19 -04002508
Chris Mason5f39d392007-10-15 16:14:19 -04002509 if (i == 0) {
2510 offset = start_offset;
2511 *map_start = 0;
2512 } else {
Chris Masondb945352007-10-15 16:15:53 -04002513 offset = 0;
Chris Mason479965d2007-10-15 16:14:27 -04002514 *map_start = (i << PAGE_CACHE_SHIFT) - start_offset;
Chris Mason5f39d392007-10-15 16:14:19 -04002515 }
Yan65555a02007-10-25 15:42:57 -04002516 if (start + min_len > eb->len) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002517printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
2518 WARN_ON(1);
2519 }
Chris Mason5f39d392007-10-15 16:14:19 -04002520
Chris Masondb945352007-10-15 16:15:53 -04002521 p = extent_buffer_page(eb, i);
2522 WARN_ON(!PageUptodate(p));
2523 kaddr = kmap_atomic(p, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002524 *token = kaddr;
2525 *map = kaddr + offset;
2526 *map_len = PAGE_CACHE_SIZE - offset;
2527 return 0;
2528}
Chris Mason19c00dd2007-10-15 16:19:22 -04002529EXPORT_SYMBOL(map_private_extent_buffer);
Chris Masondb945352007-10-15 16:15:53 -04002530
2531int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
2532 unsigned long min_len,
2533 char **token, char **map,
2534 unsigned long *map_start,
2535 unsigned long *map_len, int km)
2536{
2537 int err;
2538 int save = 0;
2539 if (eb->map_token) {
Chris Masondb945352007-10-15 16:15:53 -04002540 unmap_extent_buffer(eb, eb->map_token, km);
2541 eb->map_token = NULL;
2542 save = 1;
2543 }
Chris Mason19c00dd2007-10-15 16:19:22 -04002544 err = map_private_extent_buffer(eb, start, min_len, token, map,
2545 map_start, map_len, km);
Chris Masondb945352007-10-15 16:15:53 -04002546 if (!err && save) {
2547 eb->map_token = *token;
2548 eb->kaddr = *map;
2549 eb->map_start = *map_start;
2550 eb->map_len = *map_len;
2551 }
2552 return err;
2553}
Chris Mason5f39d392007-10-15 16:14:19 -04002554EXPORT_SYMBOL(map_extent_buffer);
2555
2556void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
2557{
Chris Masonae5252b2007-10-15 16:14:41 -04002558 kunmap_atomic(token, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002559}
2560EXPORT_SYMBOL(unmap_extent_buffer);
2561
2562int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
2563 unsigned long start,
2564 unsigned long len)
2565{
2566 size_t cur;
2567 size_t offset;
2568 struct page *page;
2569 char *kaddr;
2570 char *ptr = (char *)ptrv;
2571 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2572 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2573 int ret = 0;
2574
2575 WARN_ON(start > eb->len);
2576 WARN_ON(start + len > eb->start + eb->len);
2577
Chris Mason3685f792007-10-19 09:23:27 -04002578 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002579
2580 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002581 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002582 WARN_ON(!PageUptodate(page));
2583
2584 cur = min(len, (PAGE_CACHE_SIZE - offset));
2585
Chris Masonae5252b2007-10-15 16:14:41 -04002586 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002587 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002588 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002589 if (ret)
2590 break;
2591
2592 ptr += cur;
2593 len -= cur;
2594 offset = 0;
2595 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002596 }
2597 return ret;
2598}
2599EXPORT_SYMBOL(memcmp_extent_buffer);
2600
2601void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
2602 unsigned long start, unsigned long len)
2603{
2604 size_t cur;
2605 size_t offset;
2606 struct page *page;
2607 char *kaddr;
2608 char *src = (char *)srcv;
2609 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2610 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2611
2612 WARN_ON(start > eb->len);
2613 WARN_ON(start + len > eb->start + eb->len);
2614
Chris Mason3685f792007-10-19 09:23:27 -04002615 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002616
2617 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002618 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 WARN_ON(!PageUptodate(page));
2620
2621 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Mason59d169e2007-10-19 09:23:09 -04002622 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002623 memcpy(kaddr + offset, src, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002624 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002625
2626 src += cur;
2627 len -= cur;
2628 offset = 0;
2629 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002630 }
2631}
2632EXPORT_SYMBOL(write_extent_buffer);
2633
2634void memset_extent_buffer(struct extent_buffer *eb, char c,
2635 unsigned long start, unsigned long len)
2636{
2637 size_t cur;
2638 size_t offset;
2639 struct page *page;
2640 char *kaddr;
2641 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2642 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2643
2644 WARN_ON(start > eb->len);
2645 WARN_ON(start + len > eb->start + eb->len);
2646
Chris Mason3685f792007-10-19 09:23:27 -04002647 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002648
2649 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002650 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002651 WARN_ON(!PageUptodate(page));
2652
2653 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masonae5252b2007-10-15 16:14:41 -04002654 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002655 memset(kaddr + offset, c, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002656 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002657
2658 len -= cur;
2659 offset = 0;
2660 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002661 }
2662}
2663EXPORT_SYMBOL(memset_extent_buffer);
2664
2665void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
2666 unsigned long dst_offset, unsigned long src_offset,
2667 unsigned long len)
2668{
2669 u64 dst_len = dst->len;
2670 size_t cur;
2671 size_t offset;
2672 struct page *page;
2673 char *kaddr;
2674 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2675 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2676
2677 WARN_ON(src->len != dst_len);
2678
Chris Mason3685f792007-10-19 09:23:27 -04002679 offset = (start_offset + dst_offset) &
2680 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002681
2682 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002683 page = extent_buffer_page(dst, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002684 WARN_ON(!PageUptodate(page));
2685
2686 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
2687
Chris Masonff190c02007-10-19 10:39:41 -04002688 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002689 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masonff190c02007-10-19 10:39:41 -04002690 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002691
2692 src_offset += cur;
2693 len -= cur;
2694 offset = 0;
2695 i++;
2696 }
2697}
2698EXPORT_SYMBOL(copy_extent_buffer);
2699
2700static void move_pages(struct page *dst_page, struct page *src_page,
2701 unsigned long dst_off, unsigned long src_off,
2702 unsigned long len)
2703{
Chris Masonae5252b2007-10-15 16:14:41 -04002704 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002705 if (dst_page == src_page) {
2706 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
2707 } else {
Chris Masonae5252b2007-10-15 16:14:41 -04002708 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002709 char *p = dst_kaddr + dst_off + len;
2710 char *s = src_kaddr + src_off + len;
2711
2712 while (len--)
2713 *--p = *--s;
2714
Chris Masonae5252b2007-10-15 16:14:41 -04002715 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002716 }
Chris Masonae5252b2007-10-15 16:14:41 -04002717 kunmap_atomic(dst_kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002718}
2719
2720static void copy_pages(struct page *dst_page, struct page *src_page,
2721 unsigned long dst_off, unsigned long src_off,
2722 unsigned long len)
2723{
Chris Masonae5252b2007-10-15 16:14:41 -04002724 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 char *src_kaddr;
2726
2727 if (dst_page != src_page)
Chris Masonae5252b2007-10-15 16:14:41 -04002728 src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002729 else
2730 src_kaddr = dst_kaddr;
2731
2732 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002733 kunmap_atomic(dst_kaddr, KM_USER0);
2734 if (dst_page != src_page)
2735 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002736}
2737
2738void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2739 unsigned long src_offset, unsigned long len)
2740{
2741 size_t cur;
2742 size_t dst_off_in_page;
2743 size_t src_off_in_page;
2744 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2745 unsigned long dst_i;
2746 unsigned long src_i;
2747
2748 if (src_offset + len > dst->len) {
2749 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2750 src_offset, len, dst->len);
2751 BUG_ON(1);
2752 }
2753 if (dst_offset + len > dst->len) {
2754 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2755 dst_offset, len, dst->len);
2756 BUG_ON(1);
2757 }
2758
2759 while(len > 0) {
Chris Mason3685f792007-10-19 09:23:27 -04002760 dst_off_in_page = (start_offset + dst_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002761 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002762 src_off_in_page = (start_offset + src_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002763 ((unsigned long)PAGE_CACHE_SIZE - 1);
2764
2765 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2766 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
2767
Chris Mason5f39d392007-10-15 16:14:19 -04002768 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
2769 src_off_in_page));
Jens Axboeae2f5412007-10-19 09:22:59 -04002770 cur = min_t(unsigned long, cur,
2771 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
Chris Mason5f39d392007-10-15 16:14:19 -04002772
Chris Mason6d36dcd2007-10-15 16:14:37 -04002773 copy_pages(extent_buffer_page(dst, dst_i),
2774 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002775 dst_off_in_page, src_off_in_page, cur);
2776
2777 src_offset += cur;
2778 dst_offset += cur;
2779 len -= cur;
2780 }
2781}
2782EXPORT_SYMBOL(memcpy_extent_buffer);
2783
2784void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2785 unsigned long src_offset, unsigned long len)
2786{
2787 size_t cur;
2788 size_t dst_off_in_page;
2789 size_t src_off_in_page;
2790 unsigned long dst_end = dst_offset + len - 1;
2791 unsigned long src_end = src_offset + len - 1;
2792 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2793 unsigned long dst_i;
2794 unsigned long src_i;
2795
2796 if (src_offset + len > dst->len) {
2797 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2798 src_offset, len, dst->len);
2799 BUG_ON(1);
2800 }
2801 if (dst_offset + len > dst->len) {
2802 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2803 dst_offset, len, dst->len);
2804 BUG_ON(1);
2805 }
2806 if (dst_offset < src_offset) {
2807 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
2808 return;
2809 }
2810 while(len > 0) {
2811 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
2812 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
2813
Chris Mason3685f792007-10-19 09:23:27 -04002814 dst_off_in_page = (start_offset + dst_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04002815 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002816 src_off_in_page = (start_offset + src_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04002817 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002818
Jens Axboeae2f5412007-10-19 09:22:59 -04002819 cur = min_t(unsigned long, len, src_off_in_page + 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002820 cur = min(cur, dst_off_in_page + 1);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002821 move_pages(extent_buffer_page(dst, dst_i),
2822 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002823 dst_off_in_page - cur + 1,
2824 src_off_in_page - cur + 1, cur);
2825
Chris Masondb945352007-10-15 16:15:53 -04002826 dst_end -= cur;
2827 src_end -= cur;
Chris Mason5f39d392007-10-15 16:14:19 -04002828 len -= cur;
2829 }
2830}
2831EXPORT_SYMBOL(memmove_extent_buffer);