blob: f91f28efdb59c59bd569139ae712fbef83bc0c15 [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 Masonb293f022007-11-01 19:45:34 -040013#include <linux/writeback.h>
Chris Mason3ab2fb52007-11-08 10:59:22 -050014#include <linux/pagevec.h>
Chris Masona52d9a82007-08-27 16:49:44 -040015#include "extent_map.h"
16
Chris Mason86479a02007-09-10 19:58:16 -040017/* temporary define until extent_map moves out of btrfs */
18struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
19 unsigned long extra_flags,
20 void (*ctor)(void *, struct kmem_cache *,
21 unsigned long));
22
Chris Masona52d9a82007-08-27 16:49:44 -040023static struct kmem_cache *extent_map_cache;
24static struct kmem_cache *extent_state_cache;
Chris Mason6d36dcd2007-10-15 16:14:37 -040025static struct kmem_cache *extent_buffer_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -040026
Chris Masonf510cfe2007-10-15 16:14:48 -040027static LIST_HEAD(buffers);
28static LIST_HEAD(states);
29
Chris Masonf510cfe2007-10-15 16:14:48 -040030static spinlock_t state_lock = SPIN_LOCK_UNLOCKED;
Chris Mason4dc119042007-10-15 16:18:14 -040031#define BUFFER_LRU_MAX 64
Chris Masona52d9a82007-08-27 16:49:44 -040032
33struct tree_entry {
34 u64 start;
35 u64 end;
36 int in_tree;
37 struct rb_node rb_node;
38};
39
Chris Masonb293f022007-11-01 19:45:34 -040040struct extent_page_data {
41 struct bio *bio;
42 struct extent_map_tree *tree;
43 get_extent_t *get_extent;
44};
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050045int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040046{
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);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050050 if (!extent_map_cache)
51 return -ENOMEM;
Chris Mason86479a02007-09-10 19:58:16 -040052 extent_state_cache = btrfs_cache_create("extent_state",
Chris Mason6d36dcd2007-10-15 16:14:37 -040053 sizeof(struct extent_state), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040054 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050055 if (!extent_state_cache)
56 goto free_map_cache;
Chris Mason6d36dcd2007-10-15 16:14:37 -040057 extent_buffer_cache = btrfs_cache_create("extent_buffers",
58 sizeof(struct extent_buffer), 0,
59 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050060 if (!extent_buffer_cache)
61 goto free_state_cache;
62 return 0;
63
64free_state_cache:
65 kmem_cache_destroy(extent_state_cache);
66free_map_cache:
67 kmem_cache_destroy(extent_map_cache);
68 return -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -040069}
70
71void __exit extent_map_exit(void)
72{
Chris Masonf510cfe2007-10-15 16:14:48 -040073 struct extent_state *state;
Chris Mason6d36dcd2007-10-15 16:14:37 -040074
Chris Masonf510cfe2007-10-15 16:14:48 -040075 while (!list_empty(&states)) {
76 state = list_entry(states.next, struct extent_state, list);
77 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));
78 list_del(&state->list);
79 kmem_cache_free(extent_state_cache, state);
80
81 }
Chris Masonf510cfe2007-10-15 16:14:48 -040082
Chris Masona52d9a82007-08-27 16:49:44 -040083 if (extent_map_cache)
84 kmem_cache_destroy(extent_map_cache);
85 if (extent_state_cache)
86 kmem_cache_destroy(extent_state_cache);
Chris Mason6d36dcd2007-10-15 16:14:37 -040087 if (extent_buffer_cache)
88 kmem_cache_destroy(extent_buffer_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040089}
90
91void extent_map_tree_init(struct extent_map_tree *tree,
92 struct address_space *mapping, gfp_t mask)
93{
94 tree->map.rb_node = NULL;
95 tree->state.rb_node = NULL;
Chris Mason07157aa2007-08-30 08:50:51 -040096 tree->ops = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -040097 rwlock_init(&tree->lock);
Chris Mason4dc119042007-10-15 16:18:14 -040098 spin_lock_init(&tree->lru_lock);
Chris Masona52d9a82007-08-27 16:49:44 -040099 tree->mapping = mapping;
Chris Mason4dc119042007-10-15 16:18:14 -0400100 INIT_LIST_HEAD(&tree->buffer_lru);
101 tree->lru_size = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400102}
103EXPORT_SYMBOL(extent_map_tree_init);
104
Chris Mason19c00dd2007-10-15 16:19:22 -0400105void extent_map_tree_empty_lru(struct extent_map_tree *tree)
Chris Mason4dc119042007-10-15 16:18:14 -0400106{
107 struct extent_buffer *eb;
108 while(!list_empty(&tree->buffer_lru)) {
109 eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
110 lru);
Chris Mason0591fb52007-11-11 08:22:00 -0500111 list_del_init(&eb->lru);
Chris Mason4dc119042007-10-15 16:18:14 -0400112 free_extent_buffer(eb);
113 }
114}
Chris Mason19c00dd2007-10-15 16:19:22 -0400115EXPORT_SYMBOL(extent_map_tree_empty_lru);
Chris Mason4dc119042007-10-15 16:18:14 -0400116
Chris Masona52d9a82007-08-27 16:49:44 -0400117struct extent_map *alloc_extent_map(gfp_t mask)
118{
119 struct extent_map *em;
120 em = kmem_cache_alloc(extent_map_cache, mask);
121 if (!em || IS_ERR(em))
122 return em;
123 em->in_tree = 0;
124 atomic_set(&em->refs, 1);
125 return em;
126}
127EXPORT_SYMBOL(alloc_extent_map);
128
129void free_extent_map(struct extent_map *em)
130{
Chris Mason2bf5a722007-08-30 11:54:02 -0400131 if (!em)
132 return;
Chris Masona52d9a82007-08-27 16:49:44 -0400133 if (atomic_dec_and_test(&em->refs)) {
134 WARN_ON(em->in_tree);
135 kmem_cache_free(extent_map_cache, em);
136 }
137}
138EXPORT_SYMBOL(free_extent_map);
139
140
141struct extent_state *alloc_extent_state(gfp_t mask)
142{
143 struct extent_state *state;
Chris Masonf510cfe2007-10-15 16:14:48 -0400144 unsigned long flags;
145
Chris Masona52d9a82007-08-27 16:49:44 -0400146 state = kmem_cache_alloc(extent_state_cache, mask);
147 if (!state || IS_ERR(state))
148 return state;
149 state->state = 0;
150 state->in_tree = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400151 state->private = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400152
153 spin_lock_irqsave(&state_lock, flags);
154 list_add(&state->list, &states);
155 spin_unlock_irqrestore(&state_lock, flags);
156
Chris Masona52d9a82007-08-27 16:49:44 -0400157 atomic_set(&state->refs, 1);
158 init_waitqueue_head(&state->wq);
Chris Masona52d9a82007-08-27 16:49:44 -0400159 return state;
160}
161EXPORT_SYMBOL(alloc_extent_state);
162
163void free_extent_state(struct extent_state *state)
164{
Chris Masonf510cfe2007-10-15 16:14:48 -0400165 unsigned long flags;
Chris Mason2bf5a722007-08-30 11:54:02 -0400166 if (!state)
167 return;
Chris Masona52d9a82007-08-27 16:49:44 -0400168 if (atomic_dec_and_test(&state->refs)) {
169 WARN_ON(state->in_tree);
Chris Masonf510cfe2007-10-15 16:14:48 -0400170 spin_lock_irqsave(&state_lock, flags);
171 list_del(&state->list);
172 spin_unlock_irqrestore(&state_lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400173 kmem_cache_free(extent_state_cache, state);
174 }
175}
176EXPORT_SYMBOL(free_extent_state);
177
178static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
179 struct rb_node *node)
180{
181 struct rb_node ** p = &root->rb_node;
182 struct rb_node * parent = NULL;
183 struct tree_entry *entry;
184
185 while(*p) {
186 parent = *p;
187 entry = rb_entry(parent, struct tree_entry, rb_node);
188
189 if (offset < entry->start)
190 p = &(*p)->rb_left;
191 else if (offset > entry->end)
192 p = &(*p)->rb_right;
193 else
194 return parent;
195 }
196
197 entry = rb_entry(node, struct tree_entry, rb_node);
198 entry->in_tree = 1;
199 rb_link_node(node, parent, p);
200 rb_insert_color(node, root);
201 return NULL;
202}
203
204static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
205 struct rb_node **prev_ret)
206{
207 struct rb_node * n = root->rb_node;
208 struct rb_node *prev = NULL;
209 struct tree_entry *entry;
210 struct tree_entry *prev_entry = NULL;
211
212 while(n) {
213 entry = rb_entry(n, struct tree_entry, rb_node);
214 prev = n;
215 prev_entry = entry;
216
217 if (offset < entry->start)
218 n = n->rb_left;
219 else if (offset > entry->end)
220 n = n->rb_right;
221 else
222 return n;
223 }
224 if (!prev_ret)
225 return NULL;
226 while(prev && offset > prev_entry->end) {
227 prev = rb_next(prev);
228 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
229 }
230 *prev_ret = prev;
231 return NULL;
232}
233
234static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
235{
236 struct rb_node *prev;
237 struct rb_node *ret;
238 ret = __tree_search(root, offset, &prev);
239 if (!ret)
240 return prev;
241 return ret;
242}
243
244static int tree_delete(struct rb_root *root, u64 offset)
245{
246 struct rb_node *node;
247 struct tree_entry *entry;
248
249 node = __tree_search(root, offset, NULL);
250 if (!node)
251 return -ENOENT;
252 entry = rb_entry(node, struct tree_entry, rb_node);
253 entry->in_tree = 0;
254 rb_erase(node, root);
255 return 0;
256}
257
258/*
259 * add_extent_mapping tries a simple backward merge with existing
260 * mappings. The extent_map struct passed in will be inserted into
261 * the tree directly (no copies made, just a reference taken).
262 */
263int add_extent_mapping(struct extent_map_tree *tree,
264 struct extent_map *em)
265{
266 int ret = 0;
267 struct extent_map *prev = NULL;
268 struct rb_node *rb;
269
270 write_lock_irq(&tree->lock);
271 rb = tree_insert(&tree->map, em->end, &em->rb_node);
272 if (rb) {
273 prev = rb_entry(rb, struct extent_map, rb_node);
274 printk("found extent map %Lu %Lu on insert of %Lu %Lu\n", prev->start, prev->end, em->start, em->end);
275 ret = -EEXIST;
276 goto out;
277 }
278 atomic_inc(&em->refs);
279 if (em->start != 0) {
280 rb = rb_prev(&em->rb_node);
281 if (rb)
282 prev = rb_entry(rb, struct extent_map, rb_node);
283 if (prev && prev->end + 1 == em->start &&
Chris Mason5f39d392007-10-15 16:14:19 -0400284 ((em->block_start == EXTENT_MAP_HOLE &&
285 prev->block_start == EXTENT_MAP_HOLE) ||
Chris Mason179e29e2007-11-01 11:28:41 -0400286 (em->block_start == EXTENT_MAP_INLINE &&
287 prev->block_start == EXTENT_MAP_INLINE) ||
288 (em->block_start == EXTENT_MAP_DELALLOC &&
289 prev->block_start == EXTENT_MAP_DELALLOC) ||
290 (em->block_start < EXTENT_MAP_DELALLOC - 1 &&
291 em->block_start == prev->block_end + 1))) {
Chris Masona52d9a82007-08-27 16:49:44 -0400292 em->start = prev->start;
293 em->block_start = prev->block_start;
294 rb_erase(&prev->rb_node, &tree->map);
295 prev->in_tree = 0;
296 free_extent_map(prev);
297 }
298 }
299out:
300 write_unlock_irq(&tree->lock);
301 return ret;
302}
303EXPORT_SYMBOL(add_extent_mapping);
304
305/*
306 * lookup_extent_mapping returns the first extent_map struct in the
307 * tree that intersects the [start, end] (inclusive) range. There may
308 * be additional objects in the tree that intersect, so check the object
309 * returned carefully to make sure you don't need additional lookups.
310 */
311struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
312 u64 start, u64 end)
313{
314 struct extent_map *em;
315 struct rb_node *rb_node;
316
317 read_lock_irq(&tree->lock);
318 rb_node = tree_search(&tree->map, start);
319 if (!rb_node) {
320 em = NULL;
321 goto out;
322 }
323 if (IS_ERR(rb_node)) {
324 em = ERR_PTR(PTR_ERR(rb_node));
325 goto out;
326 }
327 em = rb_entry(rb_node, struct extent_map, rb_node);
328 if (em->end < start || em->start > end) {
329 em = NULL;
330 goto out;
331 }
332 atomic_inc(&em->refs);
333out:
334 read_unlock_irq(&tree->lock);
335 return em;
336}
337EXPORT_SYMBOL(lookup_extent_mapping);
338
339/*
340 * removes an extent_map struct from the tree. No reference counts are
341 * dropped, and no checks are done to see if the range is in use
342 */
343int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
344{
345 int ret;
346
347 write_lock_irq(&tree->lock);
348 ret = tree_delete(&tree->map, em->end);
349 write_unlock_irq(&tree->lock);
350 return ret;
351}
352EXPORT_SYMBOL(remove_extent_mapping);
353
354/*
355 * utility function to look for merge candidates inside a given range.
356 * Any extents with matching state are merged together into a single
357 * extent in the tree. Extents with EXTENT_IO in their state field
358 * are not merged because the end_io handlers need to be able to do
359 * operations on them without sleeping (or doing allocations/splits).
360 *
361 * This should be called with the tree lock held.
362 */
363static int merge_state(struct extent_map_tree *tree,
364 struct extent_state *state)
365{
366 struct extent_state *other;
367 struct rb_node *other_node;
368
369 if (state->state & EXTENT_IOBITS)
370 return 0;
371
372 other_node = rb_prev(&state->rb_node);
373 if (other_node) {
374 other = rb_entry(other_node, struct extent_state, rb_node);
375 if (other->end == state->start - 1 &&
376 other->state == state->state) {
377 state->start = other->start;
378 other->in_tree = 0;
379 rb_erase(&other->rb_node, &tree->state);
380 free_extent_state(other);
381 }
382 }
383 other_node = rb_next(&state->rb_node);
384 if (other_node) {
385 other = rb_entry(other_node, struct extent_state, rb_node);
386 if (other->start == state->end + 1 &&
387 other->state == state->state) {
388 other->start = state->start;
389 state->in_tree = 0;
390 rb_erase(&state->rb_node, &tree->state);
391 free_extent_state(state);
392 }
393 }
394 return 0;
395}
396
397/*
398 * insert an extent_state struct into the tree. 'bits' are set on the
399 * struct before it is inserted.
400 *
401 * This may return -EEXIST if the extent is already there, in which case the
402 * state struct is freed.
403 *
404 * The tree lock is not taken internally. This is a utility function and
405 * probably isn't what you want to call (see set/clear_extent_bit).
406 */
407static int insert_state(struct extent_map_tree *tree,
408 struct extent_state *state, u64 start, u64 end,
409 int bits)
410{
411 struct rb_node *node;
412
413 if (end < start) {
414 printk("end < start %Lu %Lu\n", end, start);
415 WARN_ON(1);
416 }
417 state->state |= bits;
418 state->start = start;
419 state->end = end;
Chris Masona52d9a82007-08-27 16:49:44 -0400420 node = tree_insert(&tree->state, end, &state->rb_node);
421 if (node) {
422 struct extent_state *found;
423 found = rb_entry(node, struct extent_state, rb_node);
Chris Masonb888db22007-08-27 16:49:44 -0400424 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -0400425 free_extent_state(state);
426 return -EEXIST;
427 }
428 merge_state(tree, state);
429 return 0;
430}
431
432/*
433 * split a given extent state struct in two, inserting the preallocated
434 * struct 'prealloc' as the newly created second half. 'split' indicates an
435 * offset inside 'orig' where it should be split.
436 *
437 * Before calling,
438 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
439 * are two extent state structs in the tree:
440 * prealloc: [orig->start, split - 1]
441 * orig: [ split, orig->end ]
442 *
443 * The tree locks are not taken by this function. They need to be held
444 * by the caller.
445 */
446static int split_state(struct extent_map_tree *tree, struct extent_state *orig,
447 struct extent_state *prealloc, u64 split)
448{
449 struct rb_node *node;
450 prealloc->start = orig->start;
451 prealloc->end = split - 1;
452 prealloc->state = orig->state;
453 orig->start = split;
Chris Masonf510cfe2007-10-15 16:14:48 -0400454
Chris Masona52d9a82007-08-27 16:49:44 -0400455 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
456 if (node) {
457 struct extent_state *found;
458 found = rb_entry(node, struct extent_state, rb_node);
Chris Masonb888db22007-08-27 16:49:44 -0400459 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 -0400460 free_extent_state(prealloc);
461 return -EEXIST;
462 }
463 return 0;
464}
465
466/*
467 * utility function to clear some bits in an extent state struct.
468 * it will optionally wake up any one waiting on this state (wake == 1), or
469 * forcibly remove the state from the tree (delete == 1).
470 *
471 * If no bits are set on the state struct after clearing things, the
472 * struct is freed and removed from the tree
473 */
474static int clear_state_bit(struct extent_map_tree *tree,
475 struct extent_state *state, int bits, int wake,
476 int delete)
477{
478 int ret = state->state & bits;
479 state->state &= ~bits;
480 if (wake)
481 wake_up(&state->wq);
482 if (delete || state->state == 0) {
483 if (state->in_tree) {
484 rb_erase(&state->rb_node, &tree->state);
485 state->in_tree = 0;
486 free_extent_state(state);
487 } else {
488 WARN_ON(1);
489 }
490 } else {
491 merge_state(tree, state);
492 }
493 return ret;
494}
495
496/*
497 * clear some bits on a range in the tree. This may require splitting
498 * or inserting elements in the tree, so the gfp mask is used to
499 * indicate which allocations or sleeping are allowed.
500 *
501 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
502 * the given range from the tree regardless of state (ie for truncate).
503 *
504 * the range [start, end] is inclusive.
505 *
506 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
507 * bits were already set, or zero if none of the bits were already set.
508 */
509int clear_extent_bit(struct extent_map_tree *tree, u64 start, u64 end,
510 int bits, int wake, int delete, gfp_t mask)
511{
512 struct extent_state *state;
513 struct extent_state *prealloc = NULL;
514 struct rb_node *node;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400515 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400516 int err;
517 int set = 0;
518
519again:
520 if (!prealloc && (mask & __GFP_WAIT)) {
521 prealloc = alloc_extent_state(mask);
522 if (!prealloc)
523 return -ENOMEM;
524 }
525
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400526 write_lock_irqsave(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400527 /*
528 * this search will find the extents that end after
529 * our range starts
530 */
531 node = tree_search(&tree->state, start);
532 if (!node)
533 goto out;
534 state = rb_entry(node, struct extent_state, rb_node);
535 if (state->start > end)
536 goto out;
537 WARN_ON(state->end < start);
538
539 /*
540 * | ---- desired range ---- |
541 * | state | or
542 * | ------------- state -------------- |
543 *
544 * We need to split the extent we found, and may flip
545 * bits on second half.
546 *
547 * If the extent we found extends past our range, we
548 * just split and search again. It'll get split again
549 * the next time though.
550 *
551 * If the extent we found is inside our range, we clear
552 * the desired bit on it.
553 */
554
555 if (state->start < start) {
556 err = split_state(tree, state, prealloc, start);
557 BUG_ON(err == -EEXIST);
558 prealloc = NULL;
559 if (err)
560 goto out;
561 if (state->end <= end) {
562 start = state->end + 1;
563 set |= clear_state_bit(tree, state, bits,
564 wake, delete);
565 } else {
566 start = state->start;
567 }
568 goto search_again;
569 }
570 /*
571 * | ---- desired range ---- |
572 * | state |
573 * We need to split the extent, and clear the bit
574 * on the first half
575 */
576 if (state->start <= end && state->end > end) {
577 err = split_state(tree, state, prealloc, end + 1);
578 BUG_ON(err == -EEXIST);
579
580 if (wake)
581 wake_up(&state->wq);
582 set |= clear_state_bit(tree, prealloc, bits,
583 wake, delete);
584 prealloc = NULL;
585 goto out;
586 }
587
588 start = state->end + 1;
589 set |= clear_state_bit(tree, state, bits, wake, delete);
590 goto search_again;
591
592out:
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400593 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400594 if (prealloc)
595 free_extent_state(prealloc);
596
597 return set;
598
599search_again:
Chris Mason96b51792007-10-15 16:15:19 -0400600 if (start > end)
Chris Masona52d9a82007-08-27 16:49:44 -0400601 goto out;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400602 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400603 if (mask & __GFP_WAIT)
604 cond_resched();
605 goto again;
606}
607EXPORT_SYMBOL(clear_extent_bit);
608
609static int wait_on_state(struct extent_map_tree *tree,
610 struct extent_state *state)
611{
612 DEFINE_WAIT(wait);
613 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
614 read_unlock_irq(&tree->lock);
615 schedule();
616 read_lock_irq(&tree->lock);
617 finish_wait(&state->wq, &wait);
618 return 0;
619}
620
621/*
622 * waits for one or more bits to clear on a range in the state tree.
623 * The range [start, end] is inclusive.
624 * The tree lock is taken by this function
625 */
626int wait_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits)
627{
628 struct extent_state *state;
629 struct rb_node *node;
630
631 read_lock_irq(&tree->lock);
632again:
633 while (1) {
634 /*
635 * this search will find all the extents that end after
636 * our range starts
637 */
638 node = tree_search(&tree->state, start);
639 if (!node)
640 break;
641
642 state = rb_entry(node, struct extent_state, rb_node);
643
644 if (state->start > end)
645 goto out;
646
647 if (state->state & bits) {
648 start = state->start;
649 atomic_inc(&state->refs);
650 wait_on_state(tree, state);
651 free_extent_state(state);
652 goto again;
653 }
654 start = state->end + 1;
655
656 if (start > end)
657 break;
658
659 if (need_resched()) {
660 read_unlock_irq(&tree->lock);
661 cond_resched();
662 read_lock_irq(&tree->lock);
663 }
664 }
665out:
666 read_unlock_irq(&tree->lock);
667 return 0;
668}
669EXPORT_SYMBOL(wait_extent_bit);
670
671/*
672 * set some bits on a range in the tree. This may require allocations
673 * or sleeping, so the gfp mask is used to indicate what is allowed.
674 *
675 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
676 * range already has the desired bits set. The start of the existing
677 * range is returned in failed_start in this case.
678 *
679 * [start, end] is inclusive
680 * This takes the tree lock.
681 */
682int set_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits,
683 int exclusive, u64 *failed_start, gfp_t mask)
684{
685 struct extent_state *state;
686 struct extent_state *prealloc = NULL;
687 struct rb_node *node;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400688 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400689 int err = 0;
690 int set;
691 u64 last_start;
692 u64 last_end;
693again:
694 if (!prealloc && (mask & __GFP_WAIT)) {
695 prealloc = alloc_extent_state(mask);
696 if (!prealloc)
697 return -ENOMEM;
698 }
699
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400700 write_lock_irqsave(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400701 /*
702 * this search will find all the extents that end after
703 * our range starts.
704 */
705 node = tree_search(&tree->state, start);
706 if (!node) {
707 err = insert_state(tree, prealloc, start, end, bits);
708 prealloc = NULL;
709 BUG_ON(err == -EEXIST);
710 goto out;
711 }
712
713 state = rb_entry(node, struct extent_state, rb_node);
714 last_start = state->start;
715 last_end = state->end;
716
717 /*
718 * | ---- desired range ---- |
719 * | state |
720 *
721 * Just lock what we found and keep going
722 */
723 if (state->start == start && state->end <= end) {
724 set = state->state & bits;
725 if (set && exclusive) {
726 *failed_start = state->start;
727 err = -EEXIST;
728 goto out;
729 }
730 state->state |= bits;
731 start = state->end + 1;
732 merge_state(tree, state);
733 goto search_again;
734 }
735
736 /*
737 * | ---- desired range ---- |
738 * | state |
739 * or
740 * | ------------- state -------------- |
741 *
742 * We need to split the extent we found, and may flip bits on
743 * second half.
744 *
745 * If the extent we found extends past our
746 * range, we just split and search again. It'll get split
747 * again the next time though.
748 *
749 * If the extent we found is inside our range, we set the
750 * desired bit on it.
751 */
752 if (state->start < start) {
753 set = state->state & bits;
754 if (exclusive && set) {
755 *failed_start = start;
756 err = -EEXIST;
757 goto out;
758 }
759 err = split_state(tree, state, prealloc, start);
760 BUG_ON(err == -EEXIST);
761 prealloc = NULL;
762 if (err)
763 goto out;
764 if (state->end <= end) {
765 state->state |= bits;
766 start = state->end + 1;
767 merge_state(tree, state);
768 } else {
769 start = state->start;
770 }
771 goto search_again;
772 }
773 /*
774 * | ---- desired range ---- |
Chris Masona52d9a82007-08-27 16:49:44 -0400775 * | state | or | state |
776 *
777 * There's a hole, we need to insert something in it and
778 * ignore the extent we found.
779 */
780 if (state->start > start) {
781 u64 this_end;
782 if (end < last_start)
783 this_end = end;
784 else
785 this_end = last_start -1;
786 err = insert_state(tree, prealloc, start, this_end,
787 bits);
788 prealloc = NULL;
789 BUG_ON(err == -EEXIST);
790 if (err)
791 goto out;
792 start = this_end + 1;
793 goto search_again;
794 }
Chris Masona8c450b2007-09-10 20:00:27 -0400795 /*
796 * | ---- desired range ---- |
797 * | state |
798 * We need to split the extent, and set the bit
799 * on the first half
800 */
801 if (state->start <= end && state->end > end) {
802 set = state->state & bits;
803 if (exclusive && set) {
804 *failed_start = start;
805 err = -EEXIST;
806 goto out;
807 }
808 err = split_state(tree, state, prealloc, end + 1);
809 BUG_ON(err == -EEXIST);
810
811 prealloc->state |= bits;
812 merge_state(tree, prealloc);
813 prealloc = NULL;
814 goto out;
815 }
816
Chris Masona52d9a82007-08-27 16:49:44 -0400817 goto search_again;
818
819out:
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400820 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400821 if (prealloc)
822 free_extent_state(prealloc);
823
824 return err;
825
826search_again:
827 if (start > end)
828 goto out;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400829 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400830 if (mask & __GFP_WAIT)
831 cond_resched();
832 goto again;
833}
834EXPORT_SYMBOL(set_extent_bit);
835
836/* wrappers around set/clear extent bit */
837int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
838 gfp_t mask)
839{
840 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
841 mask);
842}
843EXPORT_SYMBOL(set_extent_dirty);
844
Chris Mason96b51792007-10-15 16:15:19 -0400845int set_extent_bits(struct extent_map_tree *tree, u64 start, u64 end,
846 int bits, gfp_t mask)
847{
848 return set_extent_bit(tree, start, end, bits, 0, NULL,
849 mask);
850}
851EXPORT_SYMBOL(set_extent_bits);
852
853int clear_extent_bits(struct extent_map_tree *tree, u64 start, u64 end,
854 int bits, gfp_t mask)
855{
856 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
857}
858EXPORT_SYMBOL(clear_extent_bits);
859
Chris Masonb888db22007-08-27 16:49:44 -0400860int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
861 gfp_t mask)
862{
863 return set_extent_bit(tree, start, end,
864 EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
865 mask);
866}
867EXPORT_SYMBOL(set_extent_delalloc);
868
Chris Masona52d9a82007-08-27 16:49:44 -0400869int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
870 gfp_t mask)
871{
Chris Masonb888db22007-08-27 16:49:44 -0400872 return clear_extent_bit(tree, start, end,
873 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
Chris Masona52d9a82007-08-27 16:49:44 -0400874}
875EXPORT_SYMBOL(clear_extent_dirty);
876
877int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
878 gfp_t mask)
879{
880 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
881 mask);
882}
883EXPORT_SYMBOL(set_extent_new);
884
885int clear_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
886 gfp_t mask)
887{
888 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
889}
890EXPORT_SYMBOL(clear_extent_new);
891
892int set_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end,
893 gfp_t mask)
894{
895 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
896 mask);
897}
898EXPORT_SYMBOL(set_extent_uptodate);
899
900int clear_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end,
901 gfp_t mask)
902{
903 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
904}
905EXPORT_SYMBOL(clear_extent_uptodate);
906
907int set_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end,
908 gfp_t mask)
909{
910 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
911 0, NULL, mask);
912}
913EXPORT_SYMBOL(set_extent_writeback);
914
915int clear_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end,
916 gfp_t mask)
917{
918 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
919}
920EXPORT_SYMBOL(clear_extent_writeback);
921
922int wait_on_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end)
923{
924 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
925}
926EXPORT_SYMBOL(wait_on_extent_writeback);
927
928/*
929 * locks a range in ascending order, waiting for any locked regions
930 * it hits on the way. [start,end] are inclusive, and this will sleep.
931 */
932int lock_extent(struct extent_map_tree *tree, u64 start, u64 end, gfp_t mask)
933{
934 int err;
935 u64 failed_start;
936 while (1) {
937 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
938 &failed_start, mask);
939 if (err == -EEXIST && (mask & __GFP_WAIT)) {
940 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
941 start = failed_start;
942 } else {
943 break;
944 }
945 WARN_ON(start > end);
946 }
947 return err;
948}
949EXPORT_SYMBOL(lock_extent);
950
951int unlock_extent(struct extent_map_tree *tree, u64 start, u64 end,
952 gfp_t mask)
953{
954 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
955}
956EXPORT_SYMBOL(unlock_extent);
957
958/*
959 * helper function to set pages and extents in the tree dirty
960 */
961int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end)
962{
963 unsigned long index = start >> PAGE_CACHE_SHIFT;
964 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
965 struct page *page;
966
967 while (index <= end_index) {
968 page = find_get_page(tree->mapping, index);
969 BUG_ON(!page);
970 __set_page_dirty_nobuffers(page);
971 page_cache_release(page);
972 index++;
973 }
974 set_extent_dirty(tree, start, end, GFP_NOFS);
975 return 0;
976}
977EXPORT_SYMBOL(set_range_dirty);
978
979/*
980 * helper function to set both pages and extents in the tree writeback
981 */
982int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end)
983{
984 unsigned long index = start >> PAGE_CACHE_SHIFT;
985 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
986 struct page *page;
987
988 while (index <= end_index) {
989 page = find_get_page(tree->mapping, index);
990 BUG_ON(!page);
991 set_page_writeback(page);
992 page_cache_release(page);
993 index++;
994 }
995 set_extent_writeback(tree, start, end, GFP_NOFS);
996 return 0;
997}
998EXPORT_SYMBOL(set_range_writeback);
999
Chris Mason5f39d392007-10-15 16:14:19 -04001000int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
1001 u64 *start_ret, u64 *end_ret, int bits)
1002{
1003 struct rb_node *node;
1004 struct extent_state *state;
1005 int ret = 1;
1006
Chris Masone19caa52007-10-15 16:17:44 -04001007 read_lock_irq(&tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001008 /*
1009 * this search will find all the extents that end after
1010 * our range starts.
1011 */
1012 node = tree_search(&tree->state, start);
1013 if (!node || IS_ERR(node)) {
1014 goto out;
1015 }
1016
1017 while(1) {
1018 state = rb_entry(node, struct extent_state, rb_node);
Chris Masone19caa52007-10-15 16:17:44 -04001019 if (state->end >= start && (state->state & bits)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001020 *start_ret = state->start;
1021 *end_ret = state->end;
1022 ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -04001023 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001024 }
1025 node = rb_next(node);
1026 if (!node)
1027 break;
1028 }
1029out:
Chris Masone19caa52007-10-15 16:17:44 -04001030 read_unlock_irq(&tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001031 return ret;
1032}
1033EXPORT_SYMBOL(find_first_extent_bit);
1034
Chris Masonb888db22007-08-27 16:49:44 -04001035u64 find_lock_delalloc_range(struct extent_map_tree *tree,
1036 u64 start, u64 lock_start, u64 *end, u64 max_bytes)
1037{
1038 struct rb_node *node;
1039 struct extent_state *state;
1040 u64 cur_start = start;
1041 u64 found = 0;
1042 u64 total_bytes = 0;
1043
1044 write_lock_irq(&tree->lock);
1045 /*
1046 * this search will find all the extents that end after
1047 * our range starts.
1048 */
1049search_again:
1050 node = tree_search(&tree->state, cur_start);
1051 if (!node || IS_ERR(node)) {
1052 goto out;
1053 }
1054
1055 while(1) {
1056 state = rb_entry(node, struct extent_state, rb_node);
1057 if (state->start != cur_start) {
1058 goto out;
1059 }
1060 if (!(state->state & EXTENT_DELALLOC)) {
1061 goto out;
1062 }
1063 if (state->start >= lock_start) {
1064 if (state->state & EXTENT_LOCKED) {
1065 DEFINE_WAIT(wait);
1066 atomic_inc(&state->refs);
Yan944746e2007-11-01 11:28:42 -04001067 prepare_to_wait(&state->wq, &wait,
1068 TASK_UNINTERRUPTIBLE);
Chris Masonb888db22007-08-27 16:49:44 -04001069 write_unlock_irq(&tree->lock);
1070 schedule();
1071 write_lock_irq(&tree->lock);
1072 finish_wait(&state->wq, &wait);
1073 free_extent_state(state);
1074 goto search_again;
1075 }
1076 state->state |= EXTENT_LOCKED;
1077 }
1078 found++;
1079 *end = state->end;
1080 cur_start = state->end + 1;
1081 node = rb_next(node);
1082 if (!node)
1083 break;
Yan944746e2007-11-01 11:28:42 -04001084 total_bytes += state->end - state->start + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001085 if (total_bytes >= max_bytes)
1086 break;
1087 }
1088out:
1089 write_unlock_irq(&tree->lock);
1090 return found;
1091}
1092
Chris Masona52d9a82007-08-27 16:49:44 -04001093/*
1094 * helper function to lock both pages and extents in the tree.
1095 * pages must be locked first.
1096 */
1097int lock_range(struct extent_map_tree *tree, u64 start, u64 end)
1098{
1099 unsigned long index = start >> PAGE_CACHE_SHIFT;
1100 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1101 struct page *page;
1102 int err;
1103
1104 while (index <= end_index) {
1105 page = grab_cache_page(tree->mapping, index);
1106 if (!page) {
1107 err = -ENOMEM;
1108 goto failed;
1109 }
1110 if (IS_ERR(page)) {
1111 err = PTR_ERR(page);
1112 goto failed;
1113 }
1114 index++;
1115 }
1116 lock_extent(tree, start, end, GFP_NOFS);
1117 return 0;
1118
1119failed:
1120 /*
1121 * we failed above in getting the page at 'index', so we undo here
1122 * up to but not including the page at 'index'
1123 */
1124 end_index = index;
1125 index = start >> PAGE_CACHE_SHIFT;
1126 while (index < end_index) {
1127 page = find_get_page(tree->mapping, index);
1128 unlock_page(page);
1129 page_cache_release(page);
1130 index++;
1131 }
1132 return err;
1133}
1134EXPORT_SYMBOL(lock_range);
1135
1136/*
1137 * helper function to unlock both pages and extents in the tree.
1138 */
1139int unlock_range(struct extent_map_tree *tree, u64 start, u64 end)
1140{
1141 unsigned long index = start >> PAGE_CACHE_SHIFT;
1142 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1143 struct page *page;
1144
1145 while (index <= end_index) {
1146 page = find_get_page(tree->mapping, index);
1147 unlock_page(page);
1148 page_cache_release(page);
1149 index++;
1150 }
1151 unlock_extent(tree, start, end, GFP_NOFS);
1152 return 0;
1153}
1154EXPORT_SYMBOL(unlock_range);
1155
Chris Mason07157aa2007-08-30 08:50:51 -04001156int set_state_private(struct extent_map_tree *tree, u64 start, u64 private)
1157{
1158 struct rb_node *node;
1159 struct extent_state *state;
1160 int ret = 0;
1161
1162 write_lock_irq(&tree->lock);
1163 /*
1164 * this search will find all the extents that end after
1165 * our range starts.
1166 */
1167 node = tree_search(&tree->state, start);
1168 if (!node || IS_ERR(node)) {
1169 ret = -ENOENT;
1170 goto out;
1171 }
1172 state = rb_entry(node, struct extent_state, rb_node);
1173 if (state->start != start) {
1174 ret = -ENOENT;
1175 goto out;
1176 }
1177 state->private = private;
1178out:
1179 write_unlock_irq(&tree->lock);
1180 return ret;
Chris Mason07157aa2007-08-30 08:50:51 -04001181}
1182
1183int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private)
1184{
1185 struct rb_node *node;
1186 struct extent_state *state;
1187 int ret = 0;
1188
1189 read_lock_irq(&tree->lock);
1190 /*
1191 * this search will find all the extents that end after
1192 * our range starts.
1193 */
1194 node = tree_search(&tree->state, start);
1195 if (!node || IS_ERR(node)) {
1196 ret = -ENOENT;
1197 goto out;
1198 }
1199 state = rb_entry(node, struct extent_state, rb_node);
1200 if (state->start != start) {
1201 ret = -ENOENT;
1202 goto out;
1203 }
1204 *private = state->private;
1205out:
1206 read_unlock_irq(&tree->lock);
1207 return ret;
1208}
1209
Chris Masona52d9a82007-08-27 16:49:44 -04001210/*
1211 * searches a range in the state tree for a given mask.
1212 * If 'filled' == 1, this returns 1 only if ever extent in the tree
1213 * has the bits set. Otherwise, 1 is returned if any bit in the
1214 * range is found set.
1215 */
Chris Mason1a5bc162007-10-15 16:15:26 -04001216int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
1217 int bits, int filled)
Chris Masona52d9a82007-08-27 16:49:44 -04001218{
1219 struct extent_state *state = NULL;
1220 struct rb_node *node;
1221 int bitset = 0;
1222
1223 read_lock_irq(&tree->lock);
1224 node = tree_search(&tree->state, start);
1225 while (node && start <= end) {
1226 state = rb_entry(node, struct extent_state, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -04001227
1228 if (filled && state->start > start) {
1229 bitset = 0;
1230 break;
1231 }
Chris Mason0591fb52007-11-11 08:22:00 -05001232
1233 if (state->start > end)
1234 break;
1235
Chris Masona52d9a82007-08-27 16:49:44 -04001236 if (state->state & bits) {
1237 bitset = 1;
1238 if (!filled)
1239 break;
1240 } else if (filled) {
1241 bitset = 0;
1242 break;
1243 }
1244 start = state->end + 1;
1245 if (start > end)
1246 break;
1247 node = rb_next(node);
1248 }
1249 read_unlock_irq(&tree->lock);
1250 return bitset;
1251}
Chris Mason1a5bc162007-10-15 16:15:26 -04001252EXPORT_SYMBOL(test_range_bit);
Chris Masona52d9a82007-08-27 16:49:44 -04001253
1254/*
1255 * helper function to set a given page up to date if all the
1256 * extents in the tree for that page are up to date
1257 */
1258static int check_page_uptodate(struct extent_map_tree *tree,
1259 struct page *page)
1260{
Chris Mason35ebb932007-10-30 16:56:53 -04001261 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001262 u64 end = start + PAGE_CACHE_SIZE - 1;
1263 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1264 SetPageUptodate(page);
1265 return 0;
1266}
1267
1268/*
1269 * helper function to unlock a page if all the extents in the tree
1270 * for that page are unlocked
1271 */
1272static int check_page_locked(struct extent_map_tree *tree,
1273 struct page *page)
1274{
Chris Mason35ebb932007-10-30 16:56:53 -04001275 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001276 u64 end = start + PAGE_CACHE_SIZE - 1;
1277 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1278 unlock_page(page);
1279 return 0;
1280}
1281
1282/*
1283 * helper function to end page writeback if all the extents
1284 * in the tree for that page are done with writeback
1285 */
1286static int check_page_writeback(struct extent_map_tree *tree,
1287 struct page *page)
1288{
Chris Mason35ebb932007-10-30 16:56:53 -04001289 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001290 u64 end = start + PAGE_CACHE_SIZE - 1;
1291 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1292 end_page_writeback(page);
1293 return 0;
1294}
1295
1296/* lots and lots of room for performance fixes in the end_bio funcs */
1297
1298/*
1299 * after a writepage IO is done, we need to:
1300 * clear the uptodate bits on error
1301 * clear the writeback bits in the extent tree for this IO
1302 * end_page_writeback if the page has no more pending IO
1303 *
1304 * Scheduling is not allowed, so the extent state tree is expected
1305 * to have one and only one object corresponding to this IO.
1306 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001307#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1308static void end_bio_extent_writepage(struct bio *bio, int err)
1309#else
Chris Masona52d9a82007-08-27 16:49:44 -04001310static int end_bio_extent_writepage(struct bio *bio,
1311 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001312#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001313{
1314 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1315 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1316 struct extent_map_tree *tree = bio->bi_private;
1317 u64 start;
1318 u64 end;
1319 int whole_page;
1320
Jens Axboe0a2118d2007-10-19 09:23:05 -04001321#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001322 if (bio->bi_size)
1323 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001324#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001325
1326 do {
1327 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001328 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1329 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001330 end = start + bvec->bv_len - 1;
1331
1332 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1333 whole_page = 1;
1334 else
1335 whole_page = 0;
1336
1337 if (--bvec >= bio->bi_io_vec)
1338 prefetchw(&bvec->bv_page->flags);
1339
1340 if (!uptodate) {
1341 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1342 ClearPageUptodate(page);
1343 SetPageError(page);
1344 }
1345 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1346
1347 if (whole_page)
1348 end_page_writeback(page);
1349 else
1350 check_page_writeback(tree, page);
Christoph Hellwig0e2752a2007-09-10 20:02:33 -04001351 if (tree->ops && tree->ops->writepage_end_io_hook)
1352 tree->ops->writepage_end_io_hook(page, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001353 } while (bvec >= bio->bi_io_vec);
1354
1355 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001356#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001357 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001358#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001359}
1360
1361/*
1362 * after a readpage IO is done, we need to:
1363 * clear the uptodate bits on error
1364 * set the uptodate bits if things worked
1365 * set the page up to date if all extents in the tree are uptodate
1366 * clear the lock bit in the extent tree
1367 * unlock the page if there are no other extents locked for it
1368 *
1369 * Scheduling is not allowed, so the extent state tree is expected
1370 * to have one and only one object corresponding to this IO.
1371 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001372#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1373static void end_bio_extent_readpage(struct bio *bio, int err)
1374#else
Chris Masona52d9a82007-08-27 16:49:44 -04001375static int end_bio_extent_readpage(struct bio *bio,
1376 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001377#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001378{
Chris Mason07157aa2007-08-30 08:50:51 -04001379 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04001380 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1381 struct extent_map_tree *tree = bio->bi_private;
1382 u64 start;
1383 u64 end;
1384 int whole_page;
Chris Mason07157aa2007-08-30 08:50:51 -04001385 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04001386
Jens Axboe0a2118d2007-10-19 09:23:05 -04001387#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001388 if (bio->bi_size)
1389 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001390#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001391
1392 do {
1393 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001394 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1395 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001396 end = start + bvec->bv_len - 1;
1397
1398 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1399 whole_page = 1;
1400 else
1401 whole_page = 0;
1402
1403 if (--bvec >= bio->bi_io_vec)
1404 prefetchw(&bvec->bv_page->flags);
1405
Chris Mason07157aa2007-08-30 08:50:51 -04001406 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1407 ret = tree->ops->readpage_end_io_hook(page, start, end);
1408 if (ret)
1409 uptodate = 0;
1410 }
Chris Masona52d9a82007-08-27 16:49:44 -04001411 if (uptodate) {
1412 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1413 if (whole_page)
1414 SetPageUptodate(page);
1415 else
1416 check_page_uptodate(tree, page);
1417 } else {
1418 ClearPageUptodate(page);
1419 SetPageError(page);
1420 }
1421
1422 unlock_extent(tree, start, end, GFP_ATOMIC);
1423
1424 if (whole_page)
1425 unlock_page(page);
1426 else
1427 check_page_locked(tree, page);
1428 } while (bvec >= bio->bi_io_vec);
1429
1430 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001431#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001432 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001433#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001434}
1435
1436/*
1437 * IO done from prepare_write is pretty simple, we just unlock
1438 * the structs in the extent tree when done, and set the uptodate bits
1439 * as appropriate.
1440 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001441#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1442static void end_bio_extent_preparewrite(struct bio *bio, int err)
1443#else
Chris Masona52d9a82007-08-27 16:49:44 -04001444static int end_bio_extent_preparewrite(struct bio *bio,
1445 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001446#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001447{
1448 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1449 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1450 struct extent_map_tree *tree = bio->bi_private;
1451 u64 start;
1452 u64 end;
1453
Jens Axboe0a2118d2007-10-19 09:23:05 -04001454#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001455 if (bio->bi_size)
1456 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001457#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001458
1459 do {
1460 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001461 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1462 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001463 end = start + bvec->bv_len - 1;
1464
1465 if (--bvec >= bio->bi_io_vec)
1466 prefetchw(&bvec->bv_page->flags);
1467
1468 if (uptodate) {
1469 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1470 } else {
1471 ClearPageUptodate(page);
1472 SetPageError(page);
1473 }
1474
1475 unlock_extent(tree, start, end, GFP_ATOMIC);
1476
1477 } while (bvec >= bio->bi_io_vec);
1478
1479 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001480#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001481 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001482#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001483}
1484
Chris Masonb293f022007-11-01 19:45:34 -04001485static struct bio *
1486extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1487 gfp_t gfp_flags)
1488{
1489 struct bio *bio;
1490
1491 bio = bio_alloc(gfp_flags, nr_vecs);
1492
1493 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1494 while (!bio && (nr_vecs /= 2))
1495 bio = bio_alloc(gfp_flags, nr_vecs);
1496 }
1497
1498 if (bio) {
1499 bio->bi_bdev = bdev;
1500 bio->bi_sector = first_sector;
1501 }
1502 return bio;
1503}
1504
1505static int submit_one_bio(int rw, struct bio *bio)
1506{
1507 int ret = 0;
1508 bio_get(bio);
1509 submit_bio(rw, bio);
1510 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1511 ret = -EOPNOTSUPP;
1512 bio_put(bio);
1513 return ret;
1514}
1515
Chris Masona52d9a82007-08-27 16:49:44 -04001516static int submit_extent_page(int rw, struct extent_map_tree *tree,
1517 struct page *page, sector_t sector,
1518 size_t size, unsigned long offset,
1519 struct block_device *bdev,
Chris Masonb293f022007-11-01 19:45:34 -04001520 struct bio **bio_ret,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001521 unsigned long max_pages,
Chris Masona52d9a82007-08-27 16:49:44 -04001522 bio_end_io_t end_io_func)
1523{
Chris Masona52d9a82007-08-27 16:49:44 -04001524 int ret = 0;
Chris Masonb293f022007-11-01 19:45:34 -04001525 struct bio *bio;
1526 int nr;
Chris Masona52d9a82007-08-27 16:49:44 -04001527
Chris Masonb293f022007-11-01 19:45:34 -04001528 if (bio_ret && *bio_ret) {
1529 bio = *bio_ret;
1530 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1531 bio_add_page(bio, page, size, offset) < size) {
1532 ret = submit_one_bio(rw, bio);
1533 bio = NULL;
1534 } else {
1535 return 0;
1536 }
1537 }
Chris Mason3ab2fb52007-11-08 10:59:22 -05001538 nr = min_t(int, max_pages, bio_get_nr_vecs(bdev));
Chris Masonb293f022007-11-01 19:45:34 -04001539 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1540 if (!bio) {
1541 printk("failed to allocate bio nr %d\n", nr);
1542 }
1543 bio_add_page(bio, page, size, offset);
Chris Masona52d9a82007-08-27 16:49:44 -04001544 bio->bi_end_io = end_io_func;
1545 bio->bi_private = tree;
Chris Masonb293f022007-11-01 19:45:34 -04001546 if (bio_ret) {
1547 *bio_ret = bio;
1548 } else {
1549 ret = submit_one_bio(rw, bio);
1550 }
Chris Masona52d9a82007-08-27 16:49:44 -04001551
Chris Masona52d9a82007-08-27 16:49:44 -04001552 return ret;
1553}
1554
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001555void set_page_extent_mapped(struct page *page)
1556{
1557 if (!PagePrivate(page)) {
1558 SetPagePrivate(page);
1559 WARN_ON(!page->mapping->a_ops->invalidatepage);
Chris Mason19c00dd2007-10-15 16:19:22 -04001560 set_page_private(page, EXTENT_PAGE_PRIVATE);
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001561 page_cache_get(page);
1562 }
1563}
1564
Chris Masona52d9a82007-08-27 16:49:44 -04001565/*
1566 * basic readpage implementation. Locked extent state structs are inserted
1567 * into the tree that are removed when the IO is done (by the end_io
1568 * handlers)
1569 */
Chris Mason3ab2fb52007-11-08 10:59:22 -05001570static int __extent_read_full_page(struct extent_map_tree *tree,
1571 struct page *page,
1572 get_extent_t *get_extent,
1573 struct bio **bio)
Chris Masona52d9a82007-08-27 16:49:44 -04001574{
1575 struct inode *inode = page->mapping->host;
Chris Mason35ebb932007-10-30 16:56:53 -04001576 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001577 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1578 u64 end;
1579 u64 cur = start;
1580 u64 extent_offset;
1581 u64 last_byte = i_size_read(inode);
1582 u64 block_start;
1583 u64 cur_end;
1584 sector_t sector;
1585 struct extent_map *em;
1586 struct block_device *bdev;
1587 int ret;
1588 int nr = 0;
1589 size_t page_offset = 0;
1590 size_t iosize;
1591 size_t blocksize = inode->i_sb->s_blocksize;
1592
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001593 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001594
1595 end = page_end;
1596 lock_extent(tree, start, end, GFP_NOFS);
1597
1598 while (cur <= end) {
1599 if (cur >= last_byte) {
1600 iosize = PAGE_CACHE_SIZE - page_offset;
1601 zero_user_page(page, page_offset, iosize, KM_USER0);
1602 set_extent_uptodate(tree, cur, cur + iosize - 1,
1603 GFP_NOFS);
1604 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1605 break;
1606 }
1607 em = get_extent(inode, page, page_offset, cur, end, 0);
1608 if (IS_ERR(em) || !em) {
1609 SetPageError(page);
1610 unlock_extent(tree, cur, end, GFP_NOFS);
1611 break;
1612 }
1613
1614 extent_offset = cur - em->start;
1615 BUG_ON(em->end < cur);
1616 BUG_ON(end < cur);
1617
1618 iosize = min(em->end - cur, end - cur) + 1;
1619 cur_end = min(em->end, end);
1620 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1621 sector = (em->block_start + extent_offset) >> 9;
1622 bdev = em->bdev;
1623 block_start = em->block_start;
1624 free_extent_map(em);
1625 em = NULL;
1626
1627 /* we've found a hole, just zero and go on */
Chris Mason5f39d392007-10-15 16:14:19 -04001628 if (block_start == EXTENT_MAP_HOLE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001629 zero_user_page(page, page_offset, iosize, KM_USER0);
1630 set_extent_uptodate(tree, cur, cur + iosize - 1,
1631 GFP_NOFS);
1632 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1633 cur = cur + iosize;
1634 page_offset += iosize;
1635 continue;
1636 }
1637 /* the get_extent function already copied into the page */
1638 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1639 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1640 cur = cur + iosize;
1641 page_offset += iosize;
1642 continue;
1643 }
1644
Chris Mason07157aa2007-08-30 08:50:51 -04001645 ret = 0;
1646 if (tree->ops && tree->ops->readpage_io_hook) {
1647 ret = tree->ops->readpage_io_hook(page, cur,
1648 cur + iosize - 1);
1649 }
1650 if (!ret) {
Chris Mason3ab2fb52007-11-08 10:59:22 -05001651 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1652 nr -= page->index;
Chris Mason07157aa2007-08-30 08:50:51 -04001653 ret = submit_extent_page(READ, tree, page,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001654 sector, iosize, page_offset,
1655 bdev, bio, nr,
1656 end_bio_extent_readpage);
Chris Mason07157aa2007-08-30 08:50:51 -04001657 }
Chris Masona52d9a82007-08-27 16:49:44 -04001658 if (ret)
1659 SetPageError(page);
1660 cur = cur + iosize;
1661 page_offset += iosize;
1662 nr++;
1663 }
1664 if (!nr) {
1665 if (!PageError(page))
1666 SetPageUptodate(page);
1667 unlock_page(page);
1668 }
1669 return 0;
1670}
Chris Mason3ab2fb52007-11-08 10:59:22 -05001671
1672int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
1673 get_extent_t *get_extent)
1674{
1675 struct bio *bio = NULL;
1676 int ret;
1677
1678 ret = __extent_read_full_page(tree, page, get_extent, &bio);
1679 if (bio)
1680 submit_one_bio(READ, bio);
1681 return ret;
1682}
Chris Masona52d9a82007-08-27 16:49:44 -04001683EXPORT_SYMBOL(extent_read_full_page);
1684
1685/*
1686 * the writepage semantics are similar to regular writepage. extent
1687 * records are inserted to lock ranges in the tree, and as dirty areas
1688 * are found, they are marked writeback. Then the lock bits are removed
1689 * and the end_io handler clears the writeback ranges
1690 */
Chris Masonb293f022007-11-01 19:45:34 -04001691static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1692 void *data)
Chris Masona52d9a82007-08-27 16:49:44 -04001693{
1694 struct inode *inode = page->mapping->host;
Chris Masonb293f022007-11-01 19:45:34 -04001695 struct extent_page_data *epd = data;
1696 struct extent_map_tree *tree = epd->tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001697 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001698 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1699 u64 end;
1700 u64 cur = start;
1701 u64 extent_offset;
1702 u64 last_byte = i_size_read(inode);
1703 u64 block_start;
Chris Mason179e29e2007-11-01 11:28:41 -04001704 u64 iosize;
Chris Masona52d9a82007-08-27 16:49:44 -04001705 sector_t sector;
1706 struct extent_map *em;
1707 struct block_device *bdev;
1708 int ret;
1709 int nr = 0;
1710 size_t page_offset = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04001711 size_t blocksize;
1712 loff_t i_size = i_size_read(inode);
1713 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001714 u64 nr_delalloc;
1715 u64 delalloc_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001716
Chris Masonb888db22007-08-27 16:49:44 -04001717 WARN_ON(!PageLocked(page));
Chris Masona52d9a82007-08-27 16:49:44 -04001718 if (page->index > end_index) {
1719 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1720 unlock_page(page);
1721 return 0;
1722 }
1723
1724 if (page->index == end_index) {
1725 size_t offset = i_size & (PAGE_CACHE_SIZE - 1);
1726 zero_user_page(page, offset,
1727 PAGE_CACHE_SIZE - offset, KM_USER0);
1728 }
1729
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001730 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001731
Chris Masona52d9a82007-08-27 16:49:44 -04001732 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Masonb888db22007-08-27 16:49:44 -04001733 nr_delalloc = find_lock_delalloc_range(tree, start, page_end + 1,
1734 &delalloc_end,
1735 128 * 1024 * 1024);
1736 if (nr_delalloc) {
Chris Mason07157aa2007-08-30 08:50:51 -04001737 tree->ops->fill_delalloc(inode, start, delalloc_end);
Chris Masonb888db22007-08-27 16:49:44 -04001738 if (delalloc_end >= page_end + 1) {
1739 clear_extent_bit(tree, page_end + 1, delalloc_end,
1740 EXTENT_LOCKED | EXTENT_DELALLOC,
1741 1, 0, GFP_NOFS);
1742 }
1743 clear_extent_bit(tree, start, page_end, EXTENT_DELALLOC,
1744 0, 0, GFP_NOFS);
1745 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1746 printk("found delalloc bits after clear extent_bit\n");
1747 }
1748 } else if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1749 printk("found delalloc bits after find_delalloc_range returns 0\n");
1750 }
1751
1752 end = page_end;
1753 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1754 printk("found delalloc bits after lock_extent\n");
1755 }
Chris Masona52d9a82007-08-27 16:49:44 -04001756
1757 if (last_byte <= start) {
1758 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1759 goto done;
1760 }
1761
1762 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
1763 blocksize = inode->i_sb->s_blocksize;
1764
1765 while (cur <= end) {
1766 if (cur >= last_byte) {
1767 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
1768 break;
1769 }
Chris Masonb293f022007-11-01 19:45:34 -04001770 em = epd->get_extent(inode, page, page_offset, cur, end, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04001771 if (IS_ERR(em) || !em) {
1772 SetPageError(page);
1773 break;
1774 }
1775
1776 extent_offset = cur - em->start;
1777 BUG_ON(em->end < cur);
1778 BUG_ON(end < cur);
1779 iosize = min(em->end - cur, end - cur) + 1;
1780 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1781 sector = (em->block_start + extent_offset) >> 9;
1782 bdev = em->bdev;
1783 block_start = em->block_start;
1784 free_extent_map(em);
1785 em = NULL;
1786
Chris Mason5f39d392007-10-15 16:14:19 -04001787 if (block_start == EXTENT_MAP_HOLE ||
1788 block_start == EXTENT_MAP_INLINE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001789 clear_extent_dirty(tree, cur,
1790 cur + iosize - 1, GFP_NOFS);
1791 cur = cur + iosize;
1792 page_offset += iosize;
1793 continue;
1794 }
1795
1796 /* leave this out until we have a page_mkwrite call */
1797 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
1798 EXTENT_DIRTY, 0)) {
1799 cur = cur + iosize;
1800 page_offset += iosize;
1801 continue;
1802 }
1803 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
Christoph Hellwigb06355f2007-09-10 20:02:32 -04001804 if (tree->ops && tree->ops->writepage_io_hook) {
1805 ret = tree->ops->writepage_io_hook(page, cur,
1806 cur + iosize - 1);
1807 } else {
1808 ret = 0;
1809 }
Chris Masona52d9a82007-08-27 16:49:44 -04001810 if (ret)
1811 SetPageError(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001812 else {
Chris Masonb293f022007-11-01 19:45:34 -04001813 unsigned long nr = end_index + 1;
Chris Mason07157aa2007-08-30 08:50:51 -04001814 set_range_writeback(tree, cur, cur + iosize - 1);
Chris Masonb293f022007-11-01 19:45:34 -04001815
Chris Mason07157aa2007-08-30 08:50:51 -04001816 ret = submit_extent_page(WRITE, tree, page, sector,
1817 iosize, page_offset, bdev,
Chris Masonb293f022007-11-01 19:45:34 -04001818 &epd->bio, nr,
Chris Mason07157aa2007-08-30 08:50:51 -04001819 end_bio_extent_writepage);
1820 if (ret)
1821 SetPageError(page);
1822 }
Chris Masona52d9a82007-08-27 16:49:44 -04001823 cur = cur + iosize;
1824 page_offset += iosize;
1825 nr++;
1826 }
1827done:
Chris Masona52d9a82007-08-27 16:49:44 -04001828 unlock_extent(tree, start, page_end, GFP_NOFS);
1829 unlock_page(page);
1830 return 0;
1831}
Chris Masonb293f022007-11-01 19:45:34 -04001832
1833int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
1834 get_extent_t *get_extent,
1835 struct writeback_control *wbc)
1836{
1837 int ret;
1838 struct extent_page_data epd = {
1839 .bio = NULL,
1840 .tree = tree,
1841 .get_extent = get_extent,
1842 };
1843
1844 ret = __extent_writepage(page, wbc, &epd);
1845 if (epd.bio)
1846 submit_one_bio(WRITE, epd.bio);
1847 return ret;
1848}
Chris Masona52d9a82007-08-27 16:49:44 -04001849EXPORT_SYMBOL(extent_write_full_page);
1850
Chris Masonb293f022007-11-01 19:45:34 -04001851int extent_writepages(struct extent_map_tree *tree,
1852 struct address_space *mapping,
1853 get_extent_t *get_extent,
1854 struct writeback_control *wbc)
1855{
1856 int ret;
1857 struct extent_page_data epd = {
1858 .bio = NULL,
1859 .tree = tree,
1860 .get_extent = get_extent,
1861 };
1862
1863 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
1864 if (epd.bio)
1865 submit_one_bio(WRITE, epd.bio);
1866 return ret;
1867}
1868EXPORT_SYMBOL(extent_writepages);
1869
Chris Mason3ab2fb52007-11-08 10:59:22 -05001870int extent_readpages(struct extent_map_tree *tree,
1871 struct address_space *mapping,
1872 struct list_head *pages, unsigned nr_pages,
1873 get_extent_t get_extent)
1874{
1875 struct bio *bio = NULL;
1876 unsigned page_idx;
1877 struct pagevec pvec;
1878
1879 pagevec_init(&pvec, 0);
1880 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
1881 struct page *page = list_entry(pages->prev, struct page, lru);
1882
1883 prefetchw(&page->flags);
1884 list_del(&page->lru);
1885 /*
1886 * what we want to do here is call add_to_page_cache_lru,
1887 * but that isn't exported, so we reproduce it here
1888 */
1889 if (!add_to_page_cache(page, mapping,
1890 page->index, GFP_KERNEL)) {
1891
1892 /* open coding of lru_cache_add, also not exported */
1893 page_cache_get(page);
1894 if (!pagevec_add(&pvec, page))
1895 __pagevec_lru_add(&pvec);
1896 __extent_read_full_page(tree, page, get_extent, &bio);
1897 }
1898 page_cache_release(page);
1899 }
1900 if (pagevec_count(&pvec))
1901 __pagevec_lru_add(&pvec);
1902 BUG_ON(!list_empty(pages));
1903 if (bio)
1904 submit_one_bio(READ, bio);
1905 return 0;
1906}
1907EXPORT_SYMBOL(extent_readpages);
1908
Chris Masona52d9a82007-08-27 16:49:44 -04001909/*
1910 * basic invalidatepage code, this waits on any locked or writeback
1911 * ranges corresponding to the page, and then deletes any extent state
1912 * records from the tree
1913 */
1914int extent_invalidatepage(struct extent_map_tree *tree,
1915 struct page *page, unsigned long offset)
1916{
Chris Mason35ebb932007-10-30 16:56:53 -04001917 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Masona52d9a82007-08-27 16:49:44 -04001918 u64 end = start + PAGE_CACHE_SIZE - 1;
1919 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
1920
1921 start += (offset + blocksize -1) & ~(blocksize - 1);
1922 if (start > end)
1923 return 0;
1924
1925 lock_extent(tree, start, end, GFP_NOFS);
1926 wait_on_extent_writeback(tree, start, end);
Chris Mason2bf5a722007-08-30 11:54:02 -04001927 clear_extent_bit(tree, start, end,
1928 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Masona52d9a82007-08-27 16:49:44 -04001929 1, 1, GFP_NOFS);
1930 return 0;
1931}
1932EXPORT_SYMBOL(extent_invalidatepage);
1933
1934/*
1935 * simple commit_write call, set_range_dirty is used to mark both
1936 * the pages and the extent records as dirty
1937 */
1938int extent_commit_write(struct extent_map_tree *tree,
1939 struct inode *inode, struct page *page,
1940 unsigned from, unsigned to)
1941{
1942 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1943
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001944 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001945 set_page_dirty(page);
1946
1947 if (pos > inode->i_size) {
1948 i_size_write(inode, pos);
1949 mark_inode_dirty(inode);
1950 }
1951 return 0;
1952}
1953EXPORT_SYMBOL(extent_commit_write);
1954
1955int extent_prepare_write(struct extent_map_tree *tree,
1956 struct inode *inode, struct page *page,
1957 unsigned from, unsigned to, get_extent_t *get_extent)
1958{
Chris Mason35ebb932007-10-30 16:56:53 -04001959 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001960 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1961 u64 block_start;
1962 u64 orig_block_start;
1963 u64 block_end;
1964 u64 cur_end;
1965 struct extent_map *em;
1966 unsigned blocksize = 1 << inode->i_blkbits;
1967 size_t page_offset = 0;
1968 size_t block_off_start;
1969 size_t block_off_end;
1970 int err = 0;
1971 int iocount = 0;
1972 int ret = 0;
1973 int isnew;
1974
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001975 set_page_extent_mapped(page);
1976
Chris Masona52d9a82007-08-27 16:49:44 -04001977 block_start = (page_start + from) & ~((u64)blocksize - 1);
1978 block_end = (page_start + to - 1) | (blocksize - 1);
1979 orig_block_start = block_start;
1980
1981 lock_extent(tree, page_start, page_end, GFP_NOFS);
1982 while(block_start <= block_end) {
1983 em = get_extent(inode, page, page_offset, block_start,
1984 block_end, 1);
1985 if (IS_ERR(em) || !em) {
1986 goto err;
1987 }
1988 cur_end = min(block_end, em->end);
1989 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
1990 block_off_end = block_off_start + blocksize;
1991 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
1992
1993 if (!PageUptodate(page) && isnew &&
1994 (block_off_end > to || block_off_start < from)) {
1995 void *kaddr;
1996
1997 kaddr = kmap_atomic(page, KM_USER0);
1998 if (block_off_end > to)
1999 memset(kaddr + to, 0, block_off_end - to);
2000 if (block_off_start < from)
2001 memset(kaddr + block_off_start, 0,
2002 from - block_off_start);
2003 flush_dcache_page(page);
2004 kunmap_atomic(kaddr, KM_USER0);
2005 }
2006 if (!isnew && !PageUptodate(page) &&
2007 (block_off_end > to || block_off_start < from) &&
2008 !test_range_bit(tree, block_start, cur_end,
2009 EXTENT_UPTODATE, 1)) {
2010 u64 sector;
2011 u64 extent_offset = block_start - em->start;
2012 size_t iosize;
2013 sector = (em->block_start + extent_offset) >> 9;
2014 iosize = (cur_end - block_start + blocksize - 1) &
2015 ~((u64)blocksize - 1);
2016 /*
2017 * we've already got the extent locked, but we
2018 * need to split the state such that our end_bio
2019 * handler can clear the lock.
2020 */
2021 set_extent_bit(tree, block_start,
2022 block_start + iosize - 1,
2023 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2024 ret = submit_extent_page(READ, tree, page,
2025 sector, iosize, page_offset, em->bdev,
Chris Masonb293f022007-11-01 19:45:34 -04002026 NULL, 1,
Chris Masona52d9a82007-08-27 16:49:44 -04002027 end_bio_extent_preparewrite);
2028 iocount++;
2029 block_start = block_start + iosize;
2030 } else {
2031 set_extent_uptodate(tree, block_start, cur_end,
2032 GFP_NOFS);
2033 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2034 block_start = cur_end + 1;
2035 }
2036 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2037 free_extent_map(em);
2038 }
2039 if (iocount) {
2040 wait_extent_bit(tree, orig_block_start,
2041 block_end, EXTENT_LOCKED);
2042 }
2043 check_page_uptodate(tree, page);
2044err:
2045 /* FIXME, zero out newly allocated blocks on error */
2046 return err;
2047}
2048EXPORT_SYMBOL(extent_prepare_write);
2049
2050/*
2051 * a helper for releasepage. As long as there are no locked extents
2052 * in the range corresponding to the page, both state records and extent
2053 * map records are removed
2054 */
2055int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page)
2056{
2057 struct extent_map *em;
Chris Mason35ebb932007-10-30 16:56:53 -04002058 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002059 u64 end = start + PAGE_CACHE_SIZE - 1;
2060 u64 orig_start = start;
Chris Masonb888db22007-08-27 16:49:44 -04002061 int ret = 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002062
2063 while (start <= end) {
2064 em = lookup_extent_mapping(tree, start, end);
2065 if (!em || IS_ERR(em))
2066 break;
Chris Masonb888db22007-08-27 16:49:44 -04002067 if (!test_range_bit(tree, em->start, em->end,
2068 EXTENT_LOCKED, 0)) {
2069 remove_extent_mapping(tree, em);
2070 /* once for the rb tree */
Chris Masona52d9a82007-08-27 16:49:44 -04002071 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04002072 }
Chris Masona52d9a82007-08-27 16:49:44 -04002073 start = em->end + 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002074 /* once for us */
2075 free_extent_map(em);
2076 }
Chris Masonb888db22007-08-27 16:49:44 -04002077 if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0))
2078 ret = 0;
2079 else
2080 clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
2081 1, 1, GFP_NOFS);
2082 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002083}
2084EXPORT_SYMBOL(try_release_extent_mapping);
2085
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002086sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2087 get_extent_t *get_extent)
2088{
2089 struct inode *inode = mapping->host;
2090 u64 start = iblock << inode->i_blkbits;
2091 u64 end = start + (1 << inode->i_blkbits) - 1;
Yanc67cda12007-10-29 11:41:05 -04002092 sector_t sector = 0;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002093 struct extent_map *em;
2094
2095 em = get_extent(inode, NULL, 0, start, end, 0);
2096 if (!em || IS_ERR(em))
2097 return 0;
2098
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002099 if (em->block_start == EXTENT_MAP_INLINE ||
Chris Mason5f39d392007-10-15 16:14:19 -04002100 em->block_start == EXTENT_MAP_HOLE)
Yanc67cda12007-10-29 11:41:05 -04002101 goto out;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002102
Yanc67cda12007-10-29 11:41:05 -04002103 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2104out:
2105 free_extent_map(em);
2106 return sector;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002107}
Chris Mason5f39d392007-10-15 16:14:19 -04002108
Chris Mason4dc119042007-10-15 16:18:14 -04002109static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002110{
Chris Mason4dc119042007-10-15 16:18:14 -04002111 if (list_empty(&eb->lru)) {
2112 extent_buffer_get(eb);
2113 list_add(&eb->lru, &tree->buffer_lru);
2114 tree->lru_size++;
2115 if (tree->lru_size >= BUFFER_LRU_MAX) {
2116 struct extent_buffer *rm;
2117 rm = list_entry(tree->buffer_lru.prev,
2118 struct extent_buffer, lru);
2119 tree->lru_size--;
Chris Mason856bf3e2007-11-08 10:59:05 -05002120 list_del_init(&rm->lru);
Chris Mason4dc119042007-10-15 16:18:14 -04002121 free_extent_buffer(rm);
2122 }
2123 } else
2124 list_move(&eb->lru, &tree->buffer_lru);
2125 return 0;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002126}
Chris Mason4dc119042007-10-15 16:18:14 -04002127static struct extent_buffer *find_lru(struct extent_map_tree *tree,
2128 u64 start, unsigned long len)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002129{
Chris Mason4dc119042007-10-15 16:18:14 -04002130 struct list_head *lru = &tree->buffer_lru;
2131 struct list_head *cur = lru->next;
2132 struct extent_buffer *eb;
Chris Masonf510cfe2007-10-15 16:14:48 -04002133
Chris Mason4dc119042007-10-15 16:18:14 -04002134 if (list_empty(lru))
2135 return NULL;
Chris Masonf510cfe2007-10-15 16:14:48 -04002136
Chris Mason4dc119042007-10-15 16:18:14 -04002137 do {
2138 eb = list_entry(cur, struct extent_buffer, lru);
2139 if (eb->start == start && eb->len == len) {
2140 extent_buffer_get(eb);
2141 return eb;
2142 }
2143 cur = cur->next;
2144 } while (cur != lru);
2145 return NULL;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002146}
2147
Chris Masondb945352007-10-15 16:15:53 -04002148static inline unsigned long num_extent_pages(u64 start, u64 len)
2149{
2150 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2151 (start >> PAGE_CACHE_SHIFT);
2152}
Chris Mason4dc119042007-10-15 16:18:14 -04002153
2154static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2155 unsigned long i)
2156{
2157 struct page *p;
Chris Mason3685f792007-10-19 09:23:27 -04002158 struct address_space *mapping;
Chris Mason4dc119042007-10-15 16:18:14 -04002159
2160 if (i == 0)
Chris Mason810191f2007-10-15 16:18:55 -04002161 return eb->first_page;
Chris Mason4dc119042007-10-15 16:18:14 -04002162 i += eb->start >> PAGE_CACHE_SHIFT;
Chris Mason3685f792007-10-19 09:23:27 -04002163 mapping = eb->first_page->mapping;
2164 read_lock_irq(&mapping->tree_lock);
2165 p = radix_tree_lookup(&mapping->page_tree, i);
2166 read_unlock_irq(&mapping->tree_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002167 return p;
2168}
2169
2170static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
2171 u64 start,
2172 unsigned long len,
2173 gfp_t mask)
2174{
2175 struct extent_buffer *eb = NULL;
2176
2177 spin_lock(&tree->lru_lock);
2178 eb = find_lru(tree, start, len);
Chris Mason4dc119042007-10-15 16:18:14 -04002179 spin_unlock(&tree->lru_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002180 if (eb) {
Chris Mason09be2072007-11-07 21:08:16 -05002181 return eb;
Chris Mason4dc119042007-10-15 16:18:14 -04002182 }
Chris Mason09be2072007-11-07 21:08:16 -05002183
2184 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Mason4dc119042007-10-15 16:18:14 -04002185 INIT_LIST_HEAD(&eb->lru);
2186 eb->start = start;
2187 eb->len = len;
2188 atomic_set(&eb->refs, 1);
2189
Chris Mason4dc119042007-10-15 16:18:14 -04002190 return eb;
2191}
2192
2193static void __free_extent_buffer(struct extent_buffer *eb)
2194{
2195 kmem_cache_free(extent_buffer_cache, eb);
2196}
2197
Chris Mason5f39d392007-10-15 16:14:19 -04002198struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
2199 u64 start, unsigned long len,
Chris Mason19c00dd2007-10-15 16:19:22 -04002200 struct page *page0,
Chris Mason5f39d392007-10-15 16:14:19 -04002201 gfp_t mask)
2202{
Chris Masondb945352007-10-15 16:15:53 -04002203 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002204 unsigned long i;
2205 unsigned long index = start >> PAGE_CACHE_SHIFT;
2206 struct extent_buffer *eb;
2207 struct page *p;
2208 struct address_space *mapping = tree->mapping;
Yan65555a02007-10-25 15:42:57 -04002209 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002210
Chris Mason4dc119042007-10-15 16:18:14 -04002211 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002212 if (!eb || IS_ERR(eb))
2213 return NULL;
2214
Chris Mason4dc119042007-10-15 16:18:14 -04002215 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002216 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002217
Chris Mason19c00dd2007-10-15 16:19:22 -04002218 if (page0) {
2219 eb->first_page = page0;
2220 i = 1;
2221 index++;
2222 page_cache_get(page0);
Chris Masonff79f812007-10-15 16:22:25 -04002223 mark_page_accessed(page0);
Chris Mason19c00dd2007-10-15 16:19:22 -04002224 set_page_extent_mapped(page0);
Chris Mason0591fb52007-11-11 08:22:00 -05002225 WARN_ON(!PageUptodate(page0));
Chris Mason19c00dd2007-10-15 16:19:22 -04002226 set_page_private(page0, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2227 len << 2);
2228 } else {
2229 i = 0;
2230 }
2231 for (; i < num_pages; i++, index++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002232 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002233 if (!p) {
Chris Masondb945352007-10-15 16:15:53 -04002234 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002235 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002236 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002237 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002238 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002239 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002240 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002241 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2242 len << 2);
2243 } else {
2244 set_page_private(p, EXTENT_PAGE_PRIVATE);
2245 }
Chris Mason5f39d392007-10-15 16:14:19 -04002246 if (!PageUptodate(p))
2247 uptodate = 0;
2248 unlock_page(p);
2249 }
2250 if (uptodate)
2251 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002252 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002253
2254lru_add:
2255 spin_lock(&tree->lru_lock);
2256 add_lru(tree, eb);
2257 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002258 return eb;
Chris Mason09be2072007-11-07 21:08:16 -05002259
Chris Mason5f39d392007-10-15 16:14:19 -04002260fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002261 spin_lock(&tree->lru_lock);
2262 list_del_init(&eb->lru);
2263 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002264 if (!atomic_dec_and_test(&eb->refs))
2265 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002266 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002267 page_cache_release(extent_buffer_page(eb, index));
2268 }
Chris Mason0591fb52007-11-11 08:22:00 -05002269 if (i > 0)
2270 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002271 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002272 return NULL;
2273}
2274EXPORT_SYMBOL(alloc_extent_buffer);
2275
2276struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
2277 u64 start, unsigned long len,
2278 gfp_t mask)
2279{
Chris Masondb945352007-10-15 16:15:53 -04002280 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason09be2072007-11-07 21:08:16 -05002281 unsigned long i;
2282 unsigned long index = start >> PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002283 struct extent_buffer *eb;
2284 struct page *p;
2285 struct address_space *mapping = tree->mapping;
Chris Mason14048ed2007-10-15 16:16:28 -04002286 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002287
Chris Mason4dc119042007-10-15 16:18:14 -04002288 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002289 if (!eb || IS_ERR(eb))
2290 return NULL;
2291
Chris Mason4dc119042007-10-15 16:18:14 -04002292 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002293 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002294
2295 for (i = 0; i < num_pages; i++, index++) {
Chris Mason14048ed2007-10-15 16:16:28 -04002296 p = find_lock_page(mapping, index);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002297 if (!p) {
Chris Mason5f39d392007-10-15 16:14:19 -04002298 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002299 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002300 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002301 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002302
2303 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002304 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002305 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2306 len << 2);
2307 } else {
2308 set_page_private(p, EXTENT_PAGE_PRIVATE);
2309 }
2310
Chris Mason14048ed2007-10-15 16:16:28 -04002311 if (!PageUptodate(p))
2312 uptodate = 0;
2313 unlock_page(p);
Chris Mason5f39d392007-10-15 16:14:19 -04002314 }
Chris Mason14048ed2007-10-15 16:16:28 -04002315 if (uptodate)
2316 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002317 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002318
2319lru_add:
2320 spin_lock(&tree->lru_lock);
2321 add_lru(tree, eb);
2322 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002323 return eb;
2324fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002325 spin_lock(&tree->lru_lock);
2326 list_del_init(&eb->lru);
2327 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002328 if (!atomic_dec_and_test(&eb->refs))
2329 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002330 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002331 page_cache_release(extent_buffer_page(eb, index));
2332 }
Chris Mason0591fb52007-11-11 08:22:00 -05002333 if (i > 0)
2334 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002335 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002336 return NULL;
2337}
2338EXPORT_SYMBOL(find_extent_buffer);
2339
2340void free_extent_buffer(struct extent_buffer *eb)
2341{
2342 unsigned long i;
2343 unsigned long num_pages;
2344
2345 if (!eb)
2346 return;
2347
2348 if (!atomic_dec_and_test(&eb->refs))
2349 return;
2350
Chris Mason0591fb52007-11-11 08:22:00 -05002351 WARN_ON(!list_empty(&eb->lru));
Chris Masondb945352007-10-15 16:15:53 -04002352 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002353
Chris Mason0591fb52007-11-11 08:22:00 -05002354 for (i = 1; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002355 page_cache_release(extent_buffer_page(eb, i));
Chris Mason5f39d392007-10-15 16:14:19 -04002356 }
Chris Mason0591fb52007-11-11 08:22:00 -05002357 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason6d36dcd2007-10-15 16:14:37 -04002358 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002359}
2360EXPORT_SYMBOL(free_extent_buffer);
2361
2362int clear_extent_buffer_dirty(struct extent_map_tree *tree,
2363 struct extent_buffer *eb)
2364{
2365 int set;
2366 unsigned long i;
2367 unsigned long num_pages;
2368 struct page *page;
2369
2370 u64 start = eb->start;
2371 u64 end = start + eb->len - 1;
2372
2373 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
Chris Masondb945352007-10-15 16:15:53 -04002374 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002375
2376 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002377 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002378 lock_page(page);
2379 /*
2380 * if we're on the last page or the first page and the
2381 * block isn't aligned on a page boundary, do extra checks
2382 * to make sure we don't clean page that is partially dirty
2383 */
2384 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2385 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002386 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason35ebb932007-10-30 16:56:53 -04002387 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002388 end = start + PAGE_CACHE_SIZE - 1;
2389 if (test_range_bit(tree, start, end,
2390 EXTENT_DIRTY, 0)) {
2391 unlock_page(page);
2392 continue;
2393 }
2394 }
2395 clear_page_dirty_for_io(page);
2396 unlock_page(page);
2397 }
2398 return 0;
2399}
2400EXPORT_SYMBOL(clear_extent_buffer_dirty);
2401
2402int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
2403 struct extent_buffer *eb)
2404{
2405 return wait_on_extent_writeback(tree, eb->start,
2406 eb->start + eb->len - 1);
2407}
2408EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2409
2410int set_extent_buffer_dirty(struct extent_map_tree *tree,
2411 struct extent_buffer *eb)
2412{
Chris Mason810191f2007-10-15 16:18:55 -04002413 unsigned long i;
2414 unsigned long num_pages;
2415
2416 num_pages = num_extent_pages(eb->start, eb->len);
2417 for (i = 0; i < num_pages; i++) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002418 struct page *page = extent_buffer_page(eb, i);
2419 /* writepage may need to do something special for the
2420 * first page, we have to make sure page->private is
2421 * properly set. releasepage may drop page->private
2422 * on us if the page isn't already dirty.
2423 */
2424 if (i == 0) {
2425 lock_page(page);
2426 set_page_private(page,
2427 EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2428 eb->len << 2);
2429 }
Chris Mason810191f2007-10-15 16:18:55 -04002430 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Mason19c00dd2007-10-15 16:19:22 -04002431 if (i == 0)
2432 unlock_page(page);
Chris Mason810191f2007-10-15 16:18:55 -04002433 }
2434 return set_extent_dirty(tree, eb->start,
2435 eb->start + eb->len - 1, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002436}
2437EXPORT_SYMBOL(set_extent_buffer_dirty);
2438
2439int set_extent_buffer_uptodate(struct extent_map_tree *tree,
2440 struct extent_buffer *eb)
2441{
2442 unsigned long i;
2443 struct page *page;
2444 unsigned long num_pages;
2445
Chris Masondb945352007-10-15 16:15:53 -04002446 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002447
2448 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2449 GFP_NOFS);
2450 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002451 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002452 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2453 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002454 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason5f39d392007-10-15 16:14:19 -04002455 check_page_uptodate(tree, page);
2456 continue;
2457 }
2458 SetPageUptodate(page);
2459 }
2460 return 0;
2461}
2462EXPORT_SYMBOL(set_extent_buffer_uptodate);
2463
2464int extent_buffer_uptodate(struct extent_map_tree *tree,
2465 struct extent_buffer *eb)
2466{
2467 if (eb->flags & EXTENT_UPTODATE)
2468 return 1;
2469 return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
2470 EXTENT_UPTODATE, 1);
2471}
2472EXPORT_SYMBOL(extent_buffer_uptodate);
2473
2474int read_extent_buffer_pages(struct extent_map_tree *tree,
Chris Mason19c00dd2007-10-15 16:19:22 -04002475 struct extent_buffer *eb,
2476 u64 start,
2477 int wait)
Chris Mason5f39d392007-10-15 16:14:19 -04002478{
2479 unsigned long i;
Chris Mason19c00dd2007-10-15 16:19:22 -04002480 unsigned long start_i;
Chris Mason5f39d392007-10-15 16:14:19 -04002481 struct page *page;
2482 int err;
2483 int ret = 0;
2484 unsigned long num_pages;
2485
2486 if (eb->flags & EXTENT_UPTODATE)
2487 return 0;
2488
Chris Mason14048ed2007-10-15 16:16:28 -04002489 if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason5f39d392007-10-15 16:14:19 -04002490 EXTENT_UPTODATE, 1)) {
2491 return 0;
2492 }
Chris Mason0591fb52007-11-11 08:22:00 -05002493
Chris Mason19c00dd2007-10-15 16:19:22 -04002494 if (start) {
2495 WARN_ON(start < eb->start);
2496 start_i = (start >> PAGE_CACHE_SHIFT) -
2497 (eb->start >> PAGE_CACHE_SHIFT);
2498 } else {
2499 start_i = 0;
2500 }
Chris Mason5f39d392007-10-15 16:14:19 -04002501
Chris Masondb945352007-10-15 16:15:53 -04002502 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason19c00dd2007-10-15 16:19:22 -04002503 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002504 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002505 if (PageUptodate(page)) {
2506 continue;
2507 }
2508 if (!wait) {
2509 if (TestSetPageLocked(page)) {
2510 continue;
2511 }
2512 } else {
2513 lock_page(page);
2514 }
2515 if (!PageUptodate(page)) {
2516 err = page->mapping->a_ops->readpage(NULL, page);
2517 if (err) {
2518 ret = err;
2519 }
2520 } else {
2521 unlock_page(page);
2522 }
2523 }
2524
2525 if (ret || !wait) {
2526 return ret;
2527 }
2528
Chris Mason19c00dd2007-10-15 16:19:22 -04002529 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002530 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002531 wait_on_page_locked(page);
2532 if (!PageUptodate(page)) {
2533 ret = -EIO;
2534 }
2535 }
Chris Mason4dc119042007-10-15 16:18:14 -04002536 if (!ret)
2537 eb->flags |= EXTENT_UPTODATE;
Chris Mason5f39d392007-10-15 16:14:19 -04002538 return ret;
2539}
2540EXPORT_SYMBOL(read_extent_buffer_pages);
2541
2542void read_extent_buffer(struct extent_buffer *eb, void *dstv,
2543 unsigned long start,
2544 unsigned long len)
2545{
2546 size_t cur;
2547 size_t offset;
2548 struct page *page;
2549 char *kaddr;
2550 char *dst = (char *)dstv;
2551 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2552 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Mason14048ed2007-10-15 16:16:28 -04002553 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002554
2555 WARN_ON(start > eb->len);
2556 WARN_ON(start + len > eb->start + eb->len);
2557
Chris Mason3685f792007-10-19 09:23:27 -04002558 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002559
2560 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002561 page = extent_buffer_page(eb, i);
Chris Mason14048ed2007-10-15 16:16:28 -04002562 if (!PageUptodate(page)) {
2563 printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len);
2564 WARN_ON(1);
2565 }
Chris Mason5f39d392007-10-15 16:14:19 -04002566 WARN_ON(!PageUptodate(page));
2567
2568 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Mason59d169e2007-10-19 09:23:09 -04002569 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002570 memcpy(dst, kaddr + offset, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002571 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002572
2573 dst += cur;
2574 len -= cur;
2575 offset = 0;
2576 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002577 }
2578}
2579EXPORT_SYMBOL(read_extent_buffer);
2580
Chris Mason19c00dd2007-10-15 16:19:22 -04002581int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masondb945352007-10-15 16:15:53 -04002582 unsigned long min_len, char **token, char **map,
2583 unsigned long *map_start,
2584 unsigned long *map_len, int km)
Chris Mason5f39d392007-10-15 16:14:19 -04002585{
Chris Mason479965d2007-10-15 16:14:27 -04002586 size_t offset = start & (PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002587 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002588 struct page *p;
Chris Mason5f39d392007-10-15 16:14:19 -04002589 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2590 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Yan65555a02007-10-25 15:42:57 -04002591 unsigned long end_i = (start_offset + start + min_len - 1) >>
Chris Mason810191f2007-10-15 16:18:55 -04002592 PAGE_CACHE_SHIFT;
Chris Mason479965d2007-10-15 16:14:27 -04002593
2594 if (i != end_i)
2595 return -EINVAL;
Chris Mason5f39d392007-10-15 16:14:19 -04002596
Chris Mason5f39d392007-10-15 16:14:19 -04002597 if (i == 0) {
2598 offset = start_offset;
2599 *map_start = 0;
2600 } else {
Chris Masondb945352007-10-15 16:15:53 -04002601 offset = 0;
Chris Mason0591fb52007-11-11 08:22:00 -05002602 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
Chris Mason5f39d392007-10-15 16:14:19 -04002603 }
Yan65555a02007-10-25 15:42:57 -04002604 if (start + min_len > eb->len) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002605printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
2606 WARN_ON(1);
2607 }
Chris Mason5f39d392007-10-15 16:14:19 -04002608
Chris Masondb945352007-10-15 16:15:53 -04002609 p = extent_buffer_page(eb, i);
2610 WARN_ON(!PageUptodate(p));
2611 kaddr = kmap_atomic(p, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002612 *token = kaddr;
2613 *map = kaddr + offset;
2614 *map_len = PAGE_CACHE_SIZE - offset;
2615 return 0;
2616}
Chris Mason19c00dd2007-10-15 16:19:22 -04002617EXPORT_SYMBOL(map_private_extent_buffer);
Chris Masondb945352007-10-15 16:15:53 -04002618
2619int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
2620 unsigned long min_len,
2621 char **token, char **map,
2622 unsigned long *map_start,
2623 unsigned long *map_len, int km)
2624{
2625 int err;
2626 int save = 0;
2627 if (eb->map_token) {
Chris Masondb945352007-10-15 16:15:53 -04002628 unmap_extent_buffer(eb, eb->map_token, km);
2629 eb->map_token = NULL;
2630 save = 1;
2631 }
Chris Mason19c00dd2007-10-15 16:19:22 -04002632 err = map_private_extent_buffer(eb, start, min_len, token, map,
2633 map_start, map_len, km);
Chris Masondb945352007-10-15 16:15:53 -04002634 if (!err && save) {
2635 eb->map_token = *token;
2636 eb->kaddr = *map;
2637 eb->map_start = *map_start;
2638 eb->map_len = *map_len;
2639 }
2640 return err;
2641}
Chris Mason5f39d392007-10-15 16:14:19 -04002642EXPORT_SYMBOL(map_extent_buffer);
2643
2644void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
2645{
Chris Masonae5252b2007-10-15 16:14:41 -04002646 kunmap_atomic(token, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002647}
2648EXPORT_SYMBOL(unmap_extent_buffer);
2649
2650int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
2651 unsigned long start,
2652 unsigned long len)
2653{
2654 size_t cur;
2655 size_t offset;
2656 struct page *page;
2657 char *kaddr;
2658 char *ptr = (char *)ptrv;
2659 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2660 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2661 int ret = 0;
2662
2663 WARN_ON(start > eb->len);
2664 WARN_ON(start + len > eb->start + eb->len);
2665
Chris Mason3685f792007-10-19 09:23:27 -04002666 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002667
2668 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002669 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002670 WARN_ON(!PageUptodate(page));
2671
2672 cur = min(len, (PAGE_CACHE_SIZE - offset));
2673
Chris Masonae5252b2007-10-15 16:14:41 -04002674 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002675 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002676 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 if (ret)
2678 break;
2679
2680 ptr += cur;
2681 len -= cur;
2682 offset = 0;
2683 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002684 }
2685 return ret;
2686}
2687EXPORT_SYMBOL(memcmp_extent_buffer);
2688
2689void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
2690 unsigned long start, unsigned long len)
2691{
2692 size_t cur;
2693 size_t offset;
2694 struct page *page;
2695 char *kaddr;
2696 char *src = (char *)srcv;
2697 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2698 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2699
2700 WARN_ON(start > eb->len);
2701 WARN_ON(start + len > eb->start + eb->len);
2702
Chris Mason3685f792007-10-19 09:23:27 -04002703 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002704
2705 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002706 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002707 WARN_ON(!PageUptodate(page));
2708
2709 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Mason59d169e2007-10-19 09:23:09 -04002710 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002711 memcpy(kaddr + offset, src, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002712 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002713
2714 src += cur;
2715 len -= cur;
2716 offset = 0;
2717 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002718 }
2719}
2720EXPORT_SYMBOL(write_extent_buffer);
2721
2722void memset_extent_buffer(struct extent_buffer *eb, char c,
2723 unsigned long start, unsigned long len)
2724{
2725 size_t cur;
2726 size_t offset;
2727 struct page *page;
2728 char *kaddr;
2729 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2730 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2731
2732 WARN_ON(start > eb->len);
2733 WARN_ON(start + len > eb->start + eb->len);
2734
Chris Mason3685f792007-10-19 09:23:27 -04002735 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002736
2737 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002738 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002739 WARN_ON(!PageUptodate(page));
2740
2741 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masonae5252b2007-10-15 16:14:41 -04002742 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002743 memset(kaddr + offset, c, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002744 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002745
2746 len -= cur;
2747 offset = 0;
2748 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002749 }
2750}
2751EXPORT_SYMBOL(memset_extent_buffer);
2752
2753void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
2754 unsigned long dst_offset, unsigned long src_offset,
2755 unsigned long len)
2756{
2757 u64 dst_len = dst->len;
2758 size_t cur;
2759 size_t offset;
2760 struct page *page;
2761 char *kaddr;
2762 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2763 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2764
2765 WARN_ON(src->len != dst_len);
2766
Chris Mason3685f792007-10-19 09:23:27 -04002767 offset = (start_offset + dst_offset) &
2768 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002769
2770 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002771 page = extent_buffer_page(dst, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002772 WARN_ON(!PageUptodate(page));
2773
2774 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
2775
Chris Masonff190c02007-10-19 10:39:41 -04002776 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002777 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masonff190c02007-10-19 10:39:41 -04002778 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002779
2780 src_offset += cur;
2781 len -= cur;
2782 offset = 0;
2783 i++;
2784 }
2785}
2786EXPORT_SYMBOL(copy_extent_buffer);
2787
2788static void move_pages(struct page *dst_page, struct page *src_page,
2789 unsigned long dst_off, unsigned long src_off,
2790 unsigned long len)
2791{
Chris Masonae5252b2007-10-15 16:14:41 -04002792 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002793 if (dst_page == src_page) {
2794 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
2795 } else {
Chris Masonae5252b2007-10-15 16:14:41 -04002796 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002797 char *p = dst_kaddr + dst_off + len;
2798 char *s = src_kaddr + src_off + len;
2799
2800 while (len--)
2801 *--p = *--s;
2802
Chris Masonae5252b2007-10-15 16:14:41 -04002803 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002804 }
Chris Masonae5252b2007-10-15 16:14:41 -04002805 kunmap_atomic(dst_kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002806}
2807
2808static void copy_pages(struct page *dst_page, struct page *src_page,
2809 unsigned long dst_off, unsigned long src_off,
2810 unsigned long len)
2811{
Chris Masonae5252b2007-10-15 16:14:41 -04002812 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002813 char *src_kaddr;
2814
2815 if (dst_page != src_page)
Chris Masonae5252b2007-10-15 16:14:41 -04002816 src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002817 else
2818 src_kaddr = dst_kaddr;
2819
2820 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002821 kunmap_atomic(dst_kaddr, KM_USER0);
2822 if (dst_page != src_page)
2823 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002824}
2825
2826void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2827 unsigned long src_offset, unsigned long len)
2828{
2829 size_t cur;
2830 size_t dst_off_in_page;
2831 size_t src_off_in_page;
2832 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2833 unsigned long dst_i;
2834 unsigned long src_i;
2835
2836 if (src_offset + len > dst->len) {
2837 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2838 src_offset, len, dst->len);
2839 BUG_ON(1);
2840 }
2841 if (dst_offset + len > dst->len) {
2842 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2843 dst_offset, len, dst->len);
2844 BUG_ON(1);
2845 }
2846
2847 while(len > 0) {
Chris Mason3685f792007-10-19 09:23:27 -04002848 dst_off_in_page = (start_offset + dst_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002849 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002850 src_off_in_page = (start_offset + src_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002851 ((unsigned long)PAGE_CACHE_SIZE - 1);
2852
2853 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2854 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
2855
Chris Mason5f39d392007-10-15 16:14:19 -04002856 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
2857 src_off_in_page));
Jens Axboeae2f5412007-10-19 09:22:59 -04002858 cur = min_t(unsigned long, cur,
2859 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
Chris Mason5f39d392007-10-15 16:14:19 -04002860
Chris Mason6d36dcd2007-10-15 16:14:37 -04002861 copy_pages(extent_buffer_page(dst, dst_i),
2862 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002863 dst_off_in_page, src_off_in_page, cur);
2864
2865 src_offset += cur;
2866 dst_offset += cur;
2867 len -= cur;
2868 }
2869}
2870EXPORT_SYMBOL(memcpy_extent_buffer);
2871
2872void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2873 unsigned long src_offset, unsigned long len)
2874{
2875 size_t cur;
2876 size_t dst_off_in_page;
2877 size_t src_off_in_page;
2878 unsigned long dst_end = dst_offset + len - 1;
2879 unsigned long src_end = src_offset + len - 1;
2880 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2881 unsigned long dst_i;
2882 unsigned long src_i;
2883
2884 if (src_offset + len > dst->len) {
2885 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2886 src_offset, len, dst->len);
2887 BUG_ON(1);
2888 }
2889 if (dst_offset + len > dst->len) {
2890 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2891 dst_offset, len, dst->len);
2892 BUG_ON(1);
2893 }
2894 if (dst_offset < src_offset) {
2895 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
2896 return;
2897 }
2898 while(len > 0) {
2899 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
2900 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
2901
Chris Mason3685f792007-10-19 09:23:27 -04002902 dst_off_in_page = (start_offset + dst_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04002903 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002904 src_off_in_page = (start_offset + src_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04002905 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002906
Jens Axboeae2f5412007-10-19 09:22:59 -04002907 cur = min_t(unsigned long, len, src_off_in_page + 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002908 cur = min(cur, dst_off_in_page + 1);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002909 move_pages(extent_buffer_page(dst, dst_i),
2910 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002911 dst_off_in_page - cur + 1,
2912 src_off_in_page - cur + 1, cur);
2913
Chris Masondb945352007-10-15 16:15:53 -04002914 dst_end -= cur;
2915 src_end -= cur;
Chris Mason5f39d392007-10-15 16:14:19 -04002916 len -= cur;
2917 }
2918}
2919EXPORT_SYMBOL(memmove_extent_buffer);