blob: b6a4974ecc23801bdefe3b59ead3600affd1f059 [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,
Chris Mason3e9fd942007-11-20 10:47:25 -05001036 u64 *start, u64 *end, u64 max_bytes)
Chris Masonb888db22007-08-27 16:49:44 -04001037{
1038 struct rb_node *node;
1039 struct extent_state *state;
Chris Mason3e9fd942007-11-20 10:47:25 -05001040 u64 cur_start = *start;
Chris Masonb888db22007-08-27 16:49:44 -04001041 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);
Chris Mason3e9fd942007-11-20 10:47:25 -05001057 if (found && state->start != cur_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001058 goto out;
1059 }
1060 if (!(state->state & EXTENT_DELALLOC)) {
1061 goto out;
1062 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001063 if (!found) {
1064 struct extent_state *prev_state;
1065 struct rb_node *prev_node = node;
1066 while(1) {
1067 prev_node = rb_prev(prev_node);
1068 if (!prev_node)
1069 break;
1070 prev_state = rb_entry(prev_node,
1071 struct extent_state,
1072 rb_node);
1073 if (!(prev_state->state & EXTENT_DELALLOC))
1074 break;
1075 state = prev_state;
1076 node = prev_node;
Chris Masonb888db22007-08-27 16:49:44 -04001077 }
Chris Masonb888db22007-08-27 16:49:44 -04001078 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001079 if (state->state & EXTENT_LOCKED) {
1080 DEFINE_WAIT(wait);
1081 atomic_inc(&state->refs);
1082 prepare_to_wait(&state->wq, &wait,
1083 TASK_UNINTERRUPTIBLE);
1084 write_unlock_irq(&tree->lock);
1085 schedule();
1086 write_lock_irq(&tree->lock);
1087 finish_wait(&state->wq, &wait);
1088 free_extent_state(state);
1089 goto search_again;
1090 }
1091 state->state |= EXTENT_LOCKED;
1092 if (!found)
1093 *start = state->start;
Chris Masonb888db22007-08-27 16:49:44 -04001094 found++;
1095 *end = state->end;
1096 cur_start = state->end + 1;
1097 node = rb_next(node);
1098 if (!node)
1099 break;
Yan944746e2007-11-01 11:28:42 -04001100 total_bytes += state->end - state->start + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001101 if (total_bytes >= max_bytes)
1102 break;
1103 }
1104out:
1105 write_unlock_irq(&tree->lock);
1106 return found;
1107}
1108
Chris Mason793955b2007-11-26 16:34:41 -08001109u64 count_range_bits(struct extent_map_tree *tree,
1110 u64 *start, u64 max_bytes, unsigned long bits)
1111{
1112 struct rb_node *node;
1113 struct extent_state *state;
1114 u64 cur_start = *start;
1115 u64 total_bytes = 0;
1116 int found = 0;
1117
1118 write_lock_irq(&tree->lock);
1119 /*
1120 * this search will find all the extents that end after
1121 * our range starts.
1122 */
1123 node = tree_search(&tree->state, cur_start);
1124 if (!node || IS_ERR(node)) {
1125 goto out;
1126 }
1127
1128 while(1) {
1129 state = rb_entry(node, struct extent_state, rb_node);
1130 if ((state->state & bits)) {
1131 total_bytes += state->end - state->start + 1;
1132 if (total_bytes >= max_bytes)
1133 break;
1134 if (!found) {
1135 *start = state->start;
1136 found = 1;
1137 }
1138 }
1139 node = rb_next(node);
1140 if (!node)
1141 break;
1142 }
1143out:
1144 write_unlock_irq(&tree->lock);
1145 return total_bytes;
1146}
1147
Chris Masona52d9a82007-08-27 16:49:44 -04001148/*
1149 * helper function to lock both pages and extents in the tree.
1150 * pages must be locked first.
1151 */
1152int lock_range(struct extent_map_tree *tree, u64 start, u64 end)
1153{
1154 unsigned long index = start >> PAGE_CACHE_SHIFT;
1155 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1156 struct page *page;
1157 int err;
1158
1159 while (index <= end_index) {
1160 page = grab_cache_page(tree->mapping, index);
1161 if (!page) {
1162 err = -ENOMEM;
1163 goto failed;
1164 }
1165 if (IS_ERR(page)) {
1166 err = PTR_ERR(page);
1167 goto failed;
1168 }
1169 index++;
1170 }
1171 lock_extent(tree, start, end, GFP_NOFS);
1172 return 0;
1173
1174failed:
1175 /*
1176 * we failed above in getting the page at 'index', so we undo here
1177 * up to but not including the page at 'index'
1178 */
1179 end_index = index;
1180 index = start >> PAGE_CACHE_SHIFT;
1181 while (index < end_index) {
1182 page = find_get_page(tree->mapping, index);
1183 unlock_page(page);
1184 page_cache_release(page);
1185 index++;
1186 }
1187 return err;
1188}
1189EXPORT_SYMBOL(lock_range);
1190
1191/*
1192 * helper function to unlock both pages and extents in the tree.
1193 */
1194int unlock_range(struct extent_map_tree *tree, u64 start, u64 end)
1195{
1196 unsigned long index = start >> PAGE_CACHE_SHIFT;
1197 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1198 struct page *page;
1199
1200 while (index <= end_index) {
1201 page = find_get_page(tree->mapping, index);
1202 unlock_page(page);
1203 page_cache_release(page);
1204 index++;
1205 }
1206 unlock_extent(tree, start, end, GFP_NOFS);
1207 return 0;
1208}
1209EXPORT_SYMBOL(unlock_range);
1210
Chris Mason07157aa2007-08-30 08:50:51 -04001211int set_state_private(struct extent_map_tree *tree, u64 start, u64 private)
1212{
1213 struct rb_node *node;
1214 struct extent_state *state;
1215 int ret = 0;
1216
1217 write_lock_irq(&tree->lock);
1218 /*
1219 * this search will find all the extents that end after
1220 * our range starts.
1221 */
1222 node = tree_search(&tree->state, start);
1223 if (!node || IS_ERR(node)) {
1224 ret = -ENOENT;
1225 goto out;
1226 }
1227 state = rb_entry(node, struct extent_state, rb_node);
1228 if (state->start != start) {
1229 ret = -ENOENT;
1230 goto out;
1231 }
1232 state->private = private;
1233out:
1234 write_unlock_irq(&tree->lock);
1235 return ret;
Chris Mason07157aa2007-08-30 08:50:51 -04001236}
1237
1238int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private)
1239{
1240 struct rb_node *node;
1241 struct extent_state *state;
1242 int ret = 0;
1243
1244 read_lock_irq(&tree->lock);
1245 /*
1246 * this search will find all the extents that end after
1247 * our range starts.
1248 */
1249 node = tree_search(&tree->state, start);
1250 if (!node || IS_ERR(node)) {
1251 ret = -ENOENT;
1252 goto out;
1253 }
1254 state = rb_entry(node, struct extent_state, rb_node);
1255 if (state->start != start) {
1256 ret = -ENOENT;
1257 goto out;
1258 }
1259 *private = state->private;
1260out:
1261 read_unlock_irq(&tree->lock);
1262 return ret;
1263}
1264
Chris Masona52d9a82007-08-27 16:49:44 -04001265/*
1266 * searches a range in the state tree for a given mask.
1267 * If 'filled' == 1, this returns 1 only if ever extent in the tree
1268 * has the bits set. Otherwise, 1 is returned if any bit in the
1269 * range is found set.
1270 */
Chris Mason1a5bc1672007-10-15 16:15:26 -04001271int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
1272 int bits, int filled)
Chris Masona52d9a82007-08-27 16:49:44 -04001273{
1274 struct extent_state *state = NULL;
1275 struct rb_node *node;
1276 int bitset = 0;
1277
1278 read_lock_irq(&tree->lock);
1279 node = tree_search(&tree->state, start);
1280 while (node && start <= end) {
1281 state = rb_entry(node, struct extent_state, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -04001282
1283 if (filled && state->start > start) {
1284 bitset = 0;
1285 break;
1286 }
Chris Mason0591fb52007-11-11 08:22:00 -05001287
1288 if (state->start > end)
1289 break;
1290
Chris Masona52d9a82007-08-27 16:49:44 -04001291 if (state->state & bits) {
1292 bitset = 1;
1293 if (!filled)
1294 break;
1295 } else if (filled) {
1296 bitset = 0;
1297 break;
1298 }
1299 start = state->end + 1;
1300 if (start > end)
1301 break;
1302 node = rb_next(node);
1303 }
1304 read_unlock_irq(&tree->lock);
1305 return bitset;
1306}
Chris Mason1a5bc1672007-10-15 16:15:26 -04001307EXPORT_SYMBOL(test_range_bit);
Chris Masona52d9a82007-08-27 16:49:44 -04001308
1309/*
1310 * helper function to set a given page up to date if all the
1311 * extents in the tree for that page are up to date
1312 */
1313static int check_page_uptodate(struct extent_map_tree *tree,
1314 struct page *page)
1315{
Chris Mason35ebb932007-10-30 16:56:53 -04001316 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001317 u64 end = start + PAGE_CACHE_SIZE - 1;
1318 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1319 SetPageUptodate(page);
1320 return 0;
1321}
1322
1323/*
1324 * helper function to unlock a page if all the extents in the tree
1325 * for that page are unlocked
1326 */
1327static int check_page_locked(struct extent_map_tree *tree,
1328 struct page *page)
1329{
Chris Mason35ebb932007-10-30 16:56:53 -04001330 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001331 u64 end = start + PAGE_CACHE_SIZE - 1;
1332 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1333 unlock_page(page);
1334 return 0;
1335}
1336
1337/*
1338 * helper function to end page writeback if all the extents
1339 * in the tree for that page are done with writeback
1340 */
1341static int check_page_writeback(struct extent_map_tree *tree,
1342 struct page *page)
1343{
Chris Mason35ebb932007-10-30 16:56:53 -04001344 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001345 u64 end = start + PAGE_CACHE_SIZE - 1;
1346 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1347 end_page_writeback(page);
1348 return 0;
1349}
1350
1351/* lots and lots of room for performance fixes in the end_bio funcs */
1352
1353/*
1354 * after a writepage IO is done, we need to:
1355 * clear the uptodate bits on error
1356 * clear the writeback bits in the extent tree for this IO
1357 * end_page_writeback if the page has no more pending IO
1358 *
1359 * Scheduling is not allowed, so the extent state tree is expected
1360 * to have one and only one object corresponding to this IO.
1361 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001362#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1363static void end_bio_extent_writepage(struct bio *bio, int err)
1364#else
Chris Masona52d9a82007-08-27 16:49:44 -04001365static int end_bio_extent_writepage(struct bio *bio,
1366 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001367#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001368{
1369 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1370 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1371 struct extent_map_tree *tree = bio->bi_private;
1372 u64 start;
1373 u64 end;
1374 int whole_page;
1375
Jens Axboe0a2118d2007-10-19 09:23:05 -04001376#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001377 if (bio->bi_size)
1378 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001379#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001380
1381 do {
1382 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001383 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1384 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001385 end = start + bvec->bv_len - 1;
1386
1387 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1388 whole_page = 1;
1389 else
1390 whole_page = 0;
1391
1392 if (--bvec >= bio->bi_io_vec)
1393 prefetchw(&bvec->bv_page->flags);
1394
1395 if (!uptodate) {
1396 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1397 ClearPageUptodate(page);
1398 SetPageError(page);
1399 }
1400 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1401
1402 if (whole_page)
1403 end_page_writeback(page);
1404 else
1405 check_page_writeback(tree, page);
Christoph Hellwig0e2752a2007-09-10 20:02:33 -04001406 if (tree->ops && tree->ops->writepage_end_io_hook)
1407 tree->ops->writepage_end_io_hook(page, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001408 } while (bvec >= bio->bi_io_vec);
1409
1410 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001411#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001412 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001413#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001414}
1415
1416/*
1417 * after a readpage IO is done, we need to:
1418 * clear the uptodate bits on error
1419 * set the uptodate bits if things worked
1420 * set the page up to date if all extents in the tree are uptodate
1421 * clear the lock bit in the extent tree
1422 * unlock the page if there are no other extents locked for it
1423 *
1424 * Scheduling is not allowed, so the extent state tree is expected
1425 * to have one and only one object corresponding to this IO.
1426 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001427#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1428static void end_bio_extent_readpage(struct bio *bio, int err)
1429#else
Chris Masona52d9a82007-08-27 16:49:44 -04001430static int end_bio_extent_readpage(struct bio *bio,
1431 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001432#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001433{
Chris Mason07157aa2007-08-30 08:50:51 -04001434 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04001435 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1436 struct extent_map_tree *tree = bio->bi_private;
1437 u64 start;
1438 u64 end;
1439 int whole_page;
Chris Mason07157aa2007-08-30 08:50:51 -04001440 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04001441
Jens Axboe0a2118d2007-10-19 09:23:05 -04001442#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001443 if (bio->bi_size)
1444 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001445#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001446
1447 do {
1448 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001449 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1450 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001451 end = start + bvec->bv_len - 1;
1452
1453 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1454 whole_page = 1;
1455 else
1456 whole_page = 0;
1457
1458 if (--bvec >= bio->bi_io_vec)
1459 prefetchw(&bvec->bv_page->flags);
1460
Chris Mason07157aa2007-08-30 08:50:51 -04001461 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1462 ret = tree->ops->readpage_end_io_hook(page, start, end);
1463 if (ret)
1464 uptodate = 0;
1465 }
Chris Masona52d9a82007-08-27 16:49:44 -04001466 if (uptodate) {
1467 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1468 if (whole_page)
1469 SetPageUptodate(page);
1470 else
1471 check_page_uptodate(tree, page);
1472 } else {
1473 ClearPageUptodate(page);
1474 SetPageError(page);
1475 }
1476
1477 unlock_extent(tree, start, end, GFP_ATOMIC);
1478
1479 if (whole_page)
1480 unlock_page(page);
1481 else
1482 check_page_locked(tree, page);
1483 } while (bvec >= bio->bi_io_vec);
1484
1485 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001486#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001487 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001488#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001489}
1490
1491/*
1492 * IO done from prepare_write is pretty simple, we just unlock
1493 * the structs in the extent tree when done, and set the uptodate bits
1494 * as appropriate.
1495 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001496#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1497static void end_bio_extent_preparewrite(struct bio *bio, int err)
1498#else
Chris Masona52d9a82007-08-27 16:49:44 -04001499static int end_bio_extent_preparewrite(struct bio *bio,
1500 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001501#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001502{
1503 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1504 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1505 struct extent_map_tree *tree = bio->bi_private;
1506 u64 start;
1507 u64 end;
1508
Jens Axboe0a2118d2007-10-19 09:23:05 -04001509#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001510 if (bio->bi_size)
1511 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001512#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001513
1514 do {
1515 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001516 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1517 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001518 end = start + bvec->bv_len - 1;
1519
1520 if (--bvec >= bio->bi_io_vec)
1521 prefetchw(&bvec->bv_page->flags);
1522
1523 if (uptodate) {
1524 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1525 } else {
1526 ClearPageUptodate(page);
1527 SetPageError(page);
1528 }
1529
1530 unlock_extent(tree, start, end, GFP_ATOMIC);
1531
1532 } while (bvec >= bio->bi_io_vec);
1533
1534 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001535#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001536 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001537#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001538}
1539
Chris Masonb293f022007-11-01 19:45:34 -04001540static struct bio *
1541extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1542 gfp_t gfp_flags)
1543{
1544 struct bio *bio;
1545
1546 bio = bio_alloc(gfp_flags, nr_vecs);
1547
1548 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1549 while (!bio && (nr_vecs /= 2))
1550 bio = bio_alloc(gfp_flags, nr_vecs);
1551 }
1552
1553 if (bio) {
1554 bio->bi_bdev = bdev;
1555 bio->bi_sector = first_sector;
1556 }
1557 return bio;
1558}
1559
1560static int submit_one_bio(int rw, struct bio *bio)
1561{
1562 int ret = 0;
1563 bio_get(bio);
1564 submit_bio(rw, bio);
1565 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1566 ret = -EOPNOTSUPP;
1567 bio_put(bio);
1568 return ret;
1569}
1570
Chris Masona52d9a82007-08-27 16:49:44 -04001571static int submit_extent_page(int rw, struct extent_map_tree *tree,
1572 struct page *page, sector_t sector,
1573 size_t size, unsigned long offset,
1574 struct block_device *bdev,
Chris Masonb293f022007-11-01 19:45:34 -04001575 struct bio **bio_ret,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001576 unsigned long max_pages,
Chris Masona52d9a82007-08-27 16:49:44 -04001577 bio_end_io_t end_io_func)
1578{
Chris Masona52d9a82007-08-27 16:49:44 -04001579 int ret = 0;
Chris Masonb293f022007-11-01 19:45:34 -04001580 struct bio *bio;
1581 int nr;
Chris Masona52d9a82007-08-27 16:49:44 -04001582
Chris Masonb293f022007-11-01 19:45:34 -04001583 if (bio_ret && *bio_ret) {
1584 bio = *bio_ret;
1585 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1586 bio_add_page(bio, page, size, offset) < size) {
1587 ret = submit_one_bio(rw, bio);
1588 bio = NULL;
1589 } else {
1590 return 0;
1591 }
1592 }
Chris Mason3ab2fb52007-11-08 10:59:22 -05001593 nr = min_t(int, max_pages, bio_get_nr_vecs(bdev));
Chris Masonb293f022007-11-01 19:45:34 -04001594 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1595 if (!bio) {
1596 printk("failed to allocate bio nr %d\n", nr);
1597 }
1598 bio_add_page(bio, page, size, offset);
Chris Masona52d9a82007-08-27 16:49:44 -04001599 bio->bi_end_io = end_io_func;
1600 bio->bi_private = tree;
Chris Masonb293f022007-11-01 19:45:34 -04001601 if (bio_ret) {
1602 *bio_ret = bio;
1603 } else {
1604 ret = submit_one_bio(rw, bio);
1605 }
Chris Masona52d9a82007-08-27 16:49:44 -04001606
Chris Masona52d9a82007-08-27 16:49:44 -04001607 return ret;
1608}
1609
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001610void set_page_extent_mapped(struct page *page)
1611{
1612 if (!PagePrivate(page)) {
1613 SetPagePrivate(page);
1614 WARN_ON(!page->mapping->a_ops->invalidatepage);
Chris Mason19c00dd2007-10-15 16:19:22 -04001615 set_page_private(page, EXTENT_PAGE_PRIVATE);
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001616 page_cache_get(page);
1617 }
1618}
1619
Chris Masona52d9a82007-08-27 16:49:44 -04001620/*
1621 * basic readpage implementation. Locked extent state structs are inserted
1622 * into the tree that are removed when the IO is done (by the end_io
1623 * handlers)
1624 */
Chris Mason3ab2fb52007-11-08 10:59:22 -05001625static int __extent_read_full_page(struct extent_map_tree *tree,
1626 struct page *page,
1627 get_extent_t *get_extent,
1628 struct bio **bio)
Chris Masona52d9a82007-08-27 16:49:44 -04001629{
1630 struct inode *inode = page->mapping->host;
Chris Mason35ebb932007-10-30 16:56:53 -04001631 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001632 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1633 u64 end;
1634 u64 cur = start;
1635 u64 extent_offset;
1636 u64 last_byte = i_size_read(inode);
1637 u64 block_start;
1638 u64 cur_end;
1639 sector_t sector;
1640 struct extent_map *em;
1641 struct block_device *bdev;
1642 int ret;
1643 int nr = 0;
1644 size_t page_offset = 0;
1645 size_t iosize;
1646 size_t blocksize = inode->i_sb->s_blocksize;
1647
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001648 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001649
1650 end = page_end;
1651 lock_extent(tree, start, end, GFP_NOFS);
1652
1653 while (cur <= end) {
1654 if (cur >= last_byte) {
1655 iosize = PAGE_CACHE_SIZE - page_offset;
1656 zero_user_page(page, page_offset, iosize, KM_USER0);
1657 set_extent_uptodate(tree, cur, cur + iosize - 1,
1658 GFP_NOFS);
1659 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1660 break;
1661 }
1662 em = get_extent(inode, page, page_offset, cur, end, 0);
1663 if (IS_ERR(em) || !em) {
1664 SetPageError(page);
1665 unlock_extent(tree, cur, end, GFP_NOFS);
1666 break;
1667 }
1668
1669 extent_offset = cur - em->start;
1670 BUG_ON(em->end < cur);
1671 BUG_ON(end < cur);
1672
1673 iosize = min(em->end - cur, end - cur) + 1;
1674 cur_end = min(em->end, end);
1675 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1676 sector = (em->block_start + extent_offset) >> 9;
1677 bdev = em->bdev;
1678 block_start = em->block_start;
1679 free_extent_map(em);
1680 em = NULL;
1681
1682 /* we've found a hole, just zero and go on */
Chris Mason5f39d392007-10-15 16:14:19 -04001683 if (block_start == EXTENT_MAP_HOLE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001684 zero_user_page(page, page_offset, iosize, KM_USER0);
1685 set_extent_uptodate(tree, cur, cur + iosize - 1,
1686 GFP_NOFS);
1687 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1688 cur = cur + iosize;
1689 page_offset += iosize;
1690 continue;
1691 }
1692 /* the get_extent function already copied into the page */
1693 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1694 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1695 cur = cur + iosize;
1696 page_offset += iosize;
1697 continue;
1698 }
1699
Chris Mason07157aa2007-08-30 08:50:51 -04001700 ret = 0;
1701 if (tree->ops && tree->ops->readpage_io_hook) {
1702 ret = tree->ops->readpage_io_hook(page, cur,
1703 cur + iosize - 1);
1704 }
1705 if (!ret) {
Chris Mason3ab2fb52007-11-08 10:59:22 -05001706 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1707 nr -= page->index;
Chris Mason07157aa2007-08-30 08:50:51 -04001708 ret = submit_extent_page(READ, tree, page,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001709 sector, iosize, page_offset,
1710 bdev, bio, nr,
1711 end_bio_extent_readpage);
Chris Mason07157aa2007-08-30 08:50:51 -04001712 }
Chris Masona52d9a82007-08-27 16:49:44 -04001713 if (ret)
1714 SetPageError(page);
1715 cur = cur + iosize;
1716 page_offset += iosize;
1717 nr++;
1718 }
1719 if (!nr) {
1720 if (!PageError(page))
1721 SetPageUptodate(page);
1722 unlock_page(page);
1723 }
1724 return 0;
1725}
Chris Mason3ab2fb52007-11-08 10:59:22 -05001726
1727int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
1728 get_extent_t *get_extent)
1729{
1730 struct bio *bio = NULL;
1731 int ret;
1732
1733 ret = __extent_read_full_page(tree, page, get_extent, &bio);
1734 if (bio)
1735 submit_one_bio(READ, bio);
1736 return ret;
1737}
Chris Masona52d9a82007-08-27 16:49:44 -04001738EXPORT_SYMBOL(extent_read_full_page);
1739
1740/*
1741 * the writepage semantics are similar to regular writepage. extent
1742 * records are inserted to lock ranges in the tree, and as dirty areas
1743 * are found, they are marked writeback. Then the lock bits are removed
1744 * and the end_io handler clears the writeback ranges
1745 */
Chris Masonb293f022007-11-01 19:45:34 -04001746static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1747 void *data)
Chris Masona52d9a82007-08-27 16:49:44 -04001748{
1749 struct inode *inode = page->mapping->host;
Chris Masonb293f022007-11-01 19:45:34 -04001750 struct extent_page_data *epd = data;
1751 struct extent_map_tree *tree = epd->tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001752 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason3e9fd942007-11-20 10:47:25 -05001753 u64 delalloc_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001754 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1755 u64 end;
1756 u64 cur = start;
1757 u64 extent_offset;
1758 u64 last_byte = i_size_read(inode);
1759 u64 block_start;
Chris Mason179e29e2007-11-01 11:28:41 -04001760 u64 iosize;
Chris Masona52d9a82007-08-27 16:49:44 -04001761 sector_t sector;
1762 struct extent_map *em;
1763 struct block_device *bdev;
1764 int ret;
1765 int nr = 0;
1766 size_t page_offset = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04001767 size_t blocksize;
1768 loff_t i_size = i_size_read(inode);
1769 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001770 u64 nr_delalloc;
1771 u64 delalloc_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001772
Chris Masonb888db22007-08-27 16:49:44 -04001773 WARN_ON(!PageLocked(page));
Chris Masona52d9a82007-08-27 16:49:44 -04001774 if (page->index > end_index) {
1775 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1776 unlock_page(page);
1777 return 0;
1778 }
1779
1780 if (page->index == end_index) {
1781 size_t offset = i_size & (PAGE_CACHE_SIZE - 1);
1782 zero_user_page(page, offset,
1783 PAGE_CACHE_SIZE - offset, KM_USER0);
1784 }
1785
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001786 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001787
Chris Mason3e9fd942007-11-20 10:47:25 -05001788 delalloc_start = start;
1789 delalloc_end = 0;
1790 while(delalloc_end < page_end) {
1791 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
1792 &delalloc_end,
1793 128 * 1024 * 1024);
1794 if (nr_delalloc <= 0)
1795 break;
1796 tree->ops->fill_delalloc(inode, delalloc_start,
1797 delalloc_end);
1798 clear_extent_bit(tree, delalloc_start,
1799 delalloc_end,
1800 EXTENT_LOCKED | EXTENT_DELALLOC,
1801 1, 0, GFP_NOFS);
1802 delalloc_start = delalloc_end + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001803 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001804 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Masonb888db22007-08-27 16:49:44 -04001805
1806 end = page_end;
1807 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1808 printk("found delalloc bits after lock_extent\n");
1809 }
Chris Masona52d9a82007-08-27 16:49:44 -04001810
1811 if (last_byte <= start) {
1812 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1813 goto done;
1814 }
1815
1816 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
1817 blocksize = inode->i_sb->s_blocksize;
1818
1819 while (cur <= end) {
1820 if (cur >= last_byte) {
1821 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
1822 break;
1823 }
Chris Masonb293f022007-11-01 19:45:34 -04001824 em = epd->get_extent(inode, page, page_offset, cur, end, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04001825 if (IS_ERR(em) || !em) {
1826 SetPageError(page);
1827 break;
1828 }
1829
1830 extent_offset = cur - em->start;
1831 BUG_ON(em->end < cur);
1832 BUG_ON(end < cur);
1833 iosize = min(em->end - cur, end - cur) + 1;
1834 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1835 sector = (em->block_start + extent_offset) >> 9;
1836 bdev = em->bdev;
1837 block_start = em->block_start;
1838 free_extent_map(em);
1839 em = NULL;
1840
Chris Mason5f39d392007-10-15 16:14:19 -04001841 if (block_start == EXTENT_MAP_HOLE ||
1842 block_start == EXTENT_MAP_INLINE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001843 clear_extent_dirty(tree, cur,
1844 cur + iosize - 1, GFP_NOFS);
1845 cur = cur + iosize;
1846 page_offset += iosize;
1847 continue;
1848 }
1849
1850 /* leave this out until we have a page_mkwrite call */
1851 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
1852 EXTENT_DIRTY, 0)) {
1853 cur = cur + iosize;
1854 page_offset += iosize;
1855 continue;
1856 }
1857 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
Christoph Hellwigb06355f2007-09-10 20:02:32 -04001858 if (tree->ops && tree->ops->writepage_io_hook) {
1859 ret = tree->ops->writepage_io_hook(page, cur,
1860 cur + iosize - 1);
1861 } else {
1862 ret = 0;
1863 }
Chris Masona52d9a82007-08-27 16:49:44 -04001864 if (ret)
1865 SetPageError(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001866 else {
Chris Mason7073c8e2007-11-20 13:44:45 -05001867 unsigned long max_nr = end_index + 1;
Chris Mason07157aa2007-08-30 08:50:51 -04001868 set_range_writeback(tree, cur, cur + iosize - 1);
Chris Mason7073c8e2007-11-20 13:44:45 -05001869 if (!PageWriteback(page)) {
1870 printk("warning page %lu not writeback, "
1871 "cur %llu end %llu\n", page->index,
1872 (unsigned long long)cur,
1873 (unsigned long long)end);
1874 }
Chris Masonb293f022007-11-01 19:45:34 -04001875
Chris Mason07157aa2007-08-30 08:50:51 -04001876 ret = submit_extent_page(WRITE, tree, page, sector,
1877 iosize, page_offset, bdev,
Chris Mason7073c8e2007-11-20 13:44:45 -05001878 &epd->bio, max_nr,
Chris Mason07157aa2007-08-30 08:50:51 -04001879 end_bio_extent_writepage);
1880 if (ret)
1881 SetPageError(page);
1882 }
Chris Masona52d9a82007-08-27 16:49:44 -04001883 cur = cur + iosize;
1884 page_offset += iosize;
1885 nr++;
1886 }
1887done:
Chris Mason7073c8e2007-11-20 13:44:45 -05001888 if (nr == 0) {
1889 /* make sure the mapping tag for page dirty gets cleared */
1890 set_page_writeback(page);
1891 end_page_writeback(page);
1892 }
Chris Masona52d9a82007-08-27 16:49:44 -04001893 unlock_extent(tree, start, page_end, GFP_NOFS);
1894 unlock_page(page);
1895 return 0;
1896}
Chris Masonb293f022007-11-01 19:45:34 -04001897
1898int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
1899 get_extent_t *get_extent,
1900 struct writeback_control *wbc)
1901{
1902 int ret;
Chris Mason015a739c2007-11-26 16:15:16 -08001903 struct address_space *mapping = page->mapping;
Chris Masonb293f022007-11-01 19:45:34 -04001904 struct extent_page_data epd = {
1905 .bio = NULL,
1906 .tree = tree,
1907 .get_extent = get_extent,
1908 };
Chris Mason015a739c2007-11-26 16:15:16 -08001909 struct writeback_control wbc_writepages = {
1910 .bdi = wbc->bdi,
1911 .sync_mode = WB_SYNC_NONE,
1912 .older_than_this = NULL,
1913 .nr_to_write = 64,
1914 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
1915 .range_end = (loff_t)-1,
1916 };
1917
Chris Masonb293f022007-11-01 19:45:34 -04001918
1919 ret = __extent_writepage(page, wbc, &epd);
Chris Mason015a739c2007-11-26 16:15:16 -08001920
1921 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
Chris Masonb293f022007-11-01 19:45:34 -04001922 if (epd.bio)
1923 submit_one_bio(WRITE, epd.bio);
1924 return ret;
1925}
Chris Masona52d9a82007-08-27 16:49:44 -04001926EXPORT_SYMBOL(extent_write_full_page);
1927
Chris Masonb293f022007-11-01 19:45:34 -04001928int extent_writepages(struct extent_map_tree *tree,
1929 struct address_space *mapping,
1930 get_extent_t *get_extent,
1931 struct writeback_control *wbc)
1932{
1933 int ret;
1934 struct extent_page_data epd = {
1935 .bio = NULL,
1936 .tree = tree,
1937 .get_extent = get_extent,
1938 };
1939
1940 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
1941 if (epd.bio)
1942 submit_one_bio(WRITE, epd.bio);
1943 return ret;
1944}
1945EXPORT_SYMBOL(extent_writepages);
1946
Chris Mason3ab2fb52007-11-08 10:59:22 -05001947int extent_readpages(struct extent_map_tree *tree,
1948 struct address_space *mapping,
1949 struct list_head *pages, unsigned nr_pages,
1950 get_extent_t get_extent)
1951{
1952 struct bio *bio = NULL;
1953 unsigned page_idx;
1954 struct pagevec pvec;
1955
1956 pagevec_init(&pvec, 0);
1957 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
1958 struct page *page = list_entry(pages->prev, struct page, lru);
1959
1960 prefetchw(&page->flags);
1961 list_del(&page->lru);
1962 /*
1963 * what we want to do here is call add_to_page_cache_lru,
1964 * but that isn't exported, so we reproduce it here
1965 */
1966 if (!add_to_page_cache(page, mapping,
1967 page->index, GFP_KERNEL)) {
1968
1969 /* open coding of lru_cache_add, also not exported */
1970 page_cache_get(page);
1971 if (!pagevec_add(&pvec, page))
1972 __pagevec_lru_add(&pvec);
1973 __extent_read_full_page(tree, page, get_extent, &bio);
1974 }
1975 page_cache_release(page);
1976 }
1977 if (pagevec_count(&pvec))
1978 __pagevec_lru_add(&pvec);
1979 BUG_ON(!list_empty(pages));
1980 if (bio)
1981 submit_one_bio(READ, bio);
1982 return 0;
1983}
1984EXPORT_SYMBOL(extent_readpages);
1985
Chris Masona52d9a82007-08-27 16:49:44 -04001986/*
1987 * basic invalidatepage code, this waits on any locked or writeback
1988 * ranges corresponding to the page, and then deletes any extent state
1989 * records from the tree
1990 */
1991int extent_invalidatepage(struct extent_map_tree *tree,
1992 struct page *page, unsigned long offset)
1993{
Chris Mason35ebb932007-10-30 16:56:53 -04001994 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Masona52d9a82007-08-27 16:49:44 -04001995 u64 end = start + PAGE_CACHE_SIZE - 1;
1996 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
1997
1998 start += (offset + blocksize -1) & ~(blocksize - 1);
1999 if (start > end)
2000 return 0;
2001
2002 lock_extent(tree, start, end, GFP_NOFS);
2003 wait_on_extent_writeback(tree, start, end);
Chris Mason2bf5a722007-08-30 11:54:02 -04002004 clear_extent_bit(tree, start, end,
2005 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Masona52d9a82007-08-27 16:49:44 -04002006 1, 1, GFP_NOFS);
2007 return 0;
2008}
2009EXPORT_SYMBOL(extent_invalidatepage);
2010
2011/*
2012 * simple commit_write call, set_range_dirty is used to mark both
2013 * the pages and the extent records as dirty
2014 */
2015int extent_commit_write(struct extent_map_tree *tree,
2016 struct inode *inode, struct page *page,
2017 unsigned from, unsigned to)
2018{
2019 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2020
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04002021 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04002022 set_page_dirty(page);
2023
2024 if (pos > inode->i_size) {
2025 i_size_write(inode, pos);
2026 mark_inode_dirty(inode);
2027 }
2028 return 0;
2029}
2030EXPORT_SYMBOL(extent_commit_write);
2031
2032int extent_prepare_write(struct extent_map_tree *tree,
2033 struct inode *inode, struct page *page,
2034 unsigned from, unsigned to, get_extent_t *get_extent)
2035{
Chris Mason35ebb932007-10-30 16:56:53 -04002036 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002037 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2038 u64 block_start;
2039 u64 orig_block_start;
2040 u64 block_end;
2041 u64 cur_end;
2042 struct extent_map *em;
2043 unsigned blocksize = 1 << inode->i_blkbits;
2044 size_t page_offset = 0;
2045 size_t block_off_start;
2046 size_t block_off_end;
2047 int err = 0;
2048 int iocount = 0;
2049 int ret = 0;
2050 int isnew;
2051
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04002052 set_page_extent_mapped(page);
2053
Chris Masona52d9a82007-08-27 16:49:44 -04002054 block_start = (page_start + from) & ~((u64)blocksize - 1);
2055 block_end = (page_start + to - 1) | (blocksize - 1);
2056 orig_block_start = block_start;
2057
2058 lock_extent(tree, page_start, page_end, GFP_NOFS);
2059 while(block_start <= block_end) {
2060 em = get_extent(inode, page, page_offset, block_start,
2061 block_end, 1);
2062 if (IS_ERR(em) || !em) {
2063 goto err;
2064 }
2065 cur_end = min(block_end, em->end);
2066 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2067 block_off_end = block_off_start + blocksize;
2068 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2069
2070 if (!PageUptodate(page) && isnew &&
2071 (block_off_end > to || block_off_start < from)) {
2072 void *kaddr;
2073
2074 kaddr = kmap_atomic(page, KM_USER0);
2075 if (block_off_end > to)
2076 memset(kaddr + to, 0, block_off_end - to);
2077 if (block_off_start < from)
2078 memset(kaddr + block_off_start, 0,
2079 from - block_off_start);
2080 flush_dcache_page(page);
2081 kunmap_atomic(kaddr, KM_USER0);
2082 }
2083 if (!isnew && !PageUptodate(page) &&
2084 (block_off_end > to || block_off_start < from) &&
2085 !test_range_bit(tree, block_start, cur_end,
2086 EXTENT_UPTODATE, 1)) {
2087 u64 sector;
2088 u64 extent_offset = block_start - em->start;
2089 size_t iosize;
2090 sector = (em->block_start + extent_offset) >> 9;
2091 iosize = (cur_end - block_start + blocksize - 1) &
2092 ~((u64)blocksize - 1);
2093 /*
2094 * we've already got the extent locked, but we
2095 * need to split the state such that our end_bio
2096 * handler can clear the lock.
2097 */
2098 set_extent_bit(tree, block_start,
2099 block_start + iosize - 1,
2100 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2101 ret = submit_extent_page(READ, tree, page,
2102 sector, iosize, page_offset, em->bdev,
Chris Masonb293f022007-11-01 19:45:34 -04002103 NULL, 1,
Chris Masona52d9a82007-08-27 16:49:44 -04002104 end_bio_extent_preparewrite);
2105 iocount++;
2106 block_start = block_start + iosize;
2107 } else {
2108 set_extent_uptodate(tree, block_start, cur_end,
2109 GFP_NOFS);
2110 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2111 block_start = cur_end + 1;
2112 }
2113 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2114 free_extent_map(em);
2115 }
2116 if (iocount) {
2117 wait_extent_bit(tree, orig_block_start,
2118 block_end, EXTENT_LOCKED);
2119 }
2120 check_page_uptodate(tree, page);
2121err:
2122 /* FIXME, zero out newly allocated blocks on error */
2123 return err;
2124}
2125EXPORT_SYMBOL(extent_prepare_write);
2126
2127/*
2128 * a helper for releasepage. As long as there are no locked extents
2129 * in the range corresponding to the page, both state records and extent
2130 * map records are removed
2131 */
2132int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page)
2133{
2134 struct extent_map *em;
Chris Mason35ebb932007-10-30 16:56:53 -04002135 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002136 u64 end = start + PAGE_CACHE_SIZE - 1;
2137 u64 orig_start = start;
Chris Masonb888db22007-08-27 16:49:44 -04002138 int ret = 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002139
2140 while (start <= end) {
2141 em = lookup_extent_mapping(tree, start, end);
2142 if (!em || IS_ERR(em))
2143 break;
Chris Masonb888db22007-08-27 16:49:44 -04002144 if (!test_range_bit(tree, em->start, em->end,
2145 EXTENT_LOCKED, 0)) {
2146 remove_extent_mapping(tree, em);
2147 /* once for the rb tree */
Chris Masona52d9a82007-08-27 16:49:44 -04002148 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04002149 }
Chris Masona52d9a82007-08-27 16:49:44 -04002150 start = em->end + 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002151 /* once for us */
2152 free_extent_map(em);
2153 }
Chris Masonb888db22007-08-27 16:49:44 -04002154 if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0))
2155 ret = 0;
2156 else
2157 clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
2158 1, 1, GFP_NOFS);
2159 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002160}
2161EXPORT_SYMBOL(try_release_extent_mapping);
2162
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002163sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2164 get_extent_t *get_extent)
2165{
2166 struct inode *inode = mapping->host;
2167 u64 start = iblock << inode->i_blkbits;
2168 u64 end = start + (1 << inode->i_blkbits) - 1;
Yanc67cda12007-10-29 11:41:05 -04002169 sector_t sector = 0;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002170 struct extent_map *em;
2171
2172 em = get_extent(inode, NULL, 0, start, end, 0);
2173 if (!em || IS_ERR(em))
2174 return 0;
2175
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002176 if (em->block_start == EXTENT_MAP_INLINE ||
Chris Mason5f39d392007-10-15 16:14:19 -04002177 em->block_start == EXTENT_MAP_HOLE)
Yanc67cda12007-10-29 11:41:05 -04002178 goto out;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002179
Yanc67cda12007-10-29 11:41:05 -04002180 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2181out:
2182 free_extent_map(em);
2183 return sector;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002184}
Chris Mason5f39d392007-10-15 16:14:19 -04002185
Chris Mason4dc119042007-10-15 16:18:14 -04002186static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002187{
Chris Mason4dc119042007-10-15 16:18:14 -04002188 if (list_empty(&eb->lru)) {
2189 extent_buffer_get(eb);
2190 list_add(&eb->lru, &tree->buffer_lru);
2191 tree->lru_size++;
2192 if (tree->lru_size >= BUFFER_LRU_MAX) {
2193 struct extent_buffer *rm;
2194 rm = list_entry(tree->buffer_lru.prev,
2195 struct extent_buffer, lru);
2196 tree->lru_size--;
Chris Mason856bf3e2007-11-08 10:59:05 -05002197 list_del_init(&rm->lru);
Chris Mason4dc119042007-10-15 16:18:14 -04002198 free_extent_buffer(rm);
2199 }
2200 } else
2201 list_move(&eb->lru, &tree->buffer_lru);
2202 return 0;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002203}
Chris Mason4dc119042007-10-15 16:18:14 -04002204static struct extent_buffer *find_lru(struct extent_map_tree *tree,
2205 u64 start, unsigned long len)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002206{
Chris Mason4dc119042007-10-15 16:18:14 -04002207 struct list_head *lru = &tree->buffer_lru;
2208 struct list_head *cur = lru->next;
2209 struct extent_buffer *eb;
Chris Masonf510cfe2007-10-15 16:14:48 -04002210
Chris Mason4dc119042007-10-15 16:18:14 -04002211 if (list_empty(lru))
2212 return NULL;
Chris Masonf510cfe2007-10-15 16:14:48 -04002213
Chris Mason4dc119042007-10-15 16:18:14 -04002214 do {
2215 eb = list_entry(cur, struct extent_buffer, lru);
2216 if (eb->start == start && eb->len == len) {
2217 extent_buffer_get(eb);
2218 return eb;
2219 }
2220 cur = cur->next;
2221 } while (cur != lru);
2222 return NULL;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002223}
2224
Chris Masondb945352007-10-15 16:15:53 -04002225static inline unsigned long num_extent_pages(u64 start, u64 len)
2226{
2227 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2228 (start >> PAGE_CACHE_SHIFT);
2229}
Chris Mason4dc119042007-10-15 16:18:14 -04002230
2231static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2232 unsigned long i)
2233{
2234 struct page *p;
Chris Mason3685f792007-10-19 09:23:27 -04002235 struct address_space *mapping;
Chris Mason4dc119042007-10-15 16:18:14 -04002236
2237 if (i == 0)
Chris Mason810191f2007-10-15 16:18:55 -04002238 return eb->first_page;
Chris Mason4dc119042007-10-15 16:18:14 -04002239 i += eb->start >> PAGE_CACHE_SHIFT;
Chris Mason3685f792007-10-19 09:23:27 -04002240 mapping = eb->first_page->mapping;
2241 read_lock_irq(&mapping->tree_lock);
2242 p = radix_tree_lookup(&mapping->page_tree, i);
2243 read_unlock_irq(&mapping->tree_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002244 return p;
2245}
2246
2247static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
2248 u64 start,
2249 unsigned long len,
2250 gfp_t mask)
2251{
2252 struct extent_buffer *eb = NULL;
2253
2254 spin_lock(&tree->lru_lock);
2255 eb = find_lru(tree, start, len);
Chris Mason4dc119042007-10-15 16:18:14 -04002256 spin_unlock(&tree->lru_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002257 if (eb) {
Chris Mason09be2072007-11-07 21:08:16 -05002258 return eb;
Chris Mason4dc119042007-10-15 16:18:14 -04002259 }
Chris Mason09be2072007-11-07 21:08:16 -05002260
2261 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Mason4dc119042007-10-15 16:18:14 -04002262 INIT_LIST_HEAD(&eb->lru);
2263 eb->start = start;
2264 eb->len = len;
2265 atomic_set(&eb->refs, 1);
2266
Chris Mason4dc119042007-10-15 16:18:14 -04002267 return eb;
2268}
2269
2270static void __free_extent_buffer(struct extent_buffer *eb)
2271{
2272 kmem_cache_free(extent_buffer_cache, eb);
2273}
2274
Chris Mason5f39d392007-10-15 16:14:19 -04002275struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
2276 u64 start, unsigned long len,
Chris Mason19c00dd2007-10-15 16:19:22 -04002277 struct page *page0,
Chris Mason5f39d392007-10-15 16:14:19 -04002278 gfp_t mask)
2279{
Chris Masondb945352007-10-15 16:15:53 -04002280 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002281 unsigned long i;
2282 unsigned long index = start >> PAGE_CACHE_SHIFT;
2283 struct extent_buffer *eb;
2284 struct page *p;
2285 struct address_space *mapping = tree->mapping;
Yan65555a02007-10-25 15:42:57 -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
Chris Mason19c00dd2007-10-15 16:19:22 -04002295 if (page0) {
2296 eb->first_page = page0;
2297 i = 1;
2298 index++;
2299 page_cache_get(page0);
Chris Masonff79f812007-10-15 16:22:25 -04002300 mark_page_accessed(page0);
Chris Mason19c00dd2007-10-15 16:19:22 -04002301 set_page_extent_mapped(page0);
Chris Mason0591fb52007-11-11 08:22:00 -05002302 WARN_ON(!PageUptodate(page0));
Chris Mason19c00dd2007-10-15 16:19:22 -04002303 set_page_private(page0, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2304 len << 2);
2305 } else {
2306 i = 0;
2307 }
2308 for (; i < num_pages; i++, index++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002309 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002310 if (!p) {
Chris Masondb945352007-10-15 16:15:53 -04002311 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002312 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002313 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002314 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002315 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002316 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002317 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002318 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2319 len << 2);
2320 } else {
2321 set_page_private(p, EXTENT_PAGE_PRIVATE);
2322 }
Chris Mason5f39d392007-10-15 16:14:19 -04002323 if (!PageUptodate(p))
2324 uptodate = 0;
2325 unlock_page(p);
2326 }
2327 if (uptodate)
2328 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002329 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002330
2331lru_add:
2332 spin_lock(&tree->lru_lock);
2333 add_lru(tree, eb);
2334 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002335 return eb;
Chris Mason09be2072007-11-07 21:08:16 -05002336
Chris Mason5f39d392007-10-15 16:14:19 -04002337fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002338 spin_lock(&tree->lru_lock);
2339 list_del_init(&eb->lru);
2340 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002341 if (!atomic_dec_and_test(&eb->refs))
2342 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002343 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002344 page_cache_release(extent_buffer_page(eb, index));
2345 }
Chris Mason0591fb52007-11-11 08:22:00 -05002346 if (i > 0)
2347 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002348 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002349 return NULL;
2350}
2351EXPORT_SYMBOL(alloc_extent_buffer);
2352
2353struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
2354 u64 start, unsigned long len,
2355 gfp_t mask)
2356{
Chris Masondb945352007-10-15 16:15:53 -04002357 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason09be2072007-11-07 21:08:16 -05002358 unsigned long i;
2359 unsigned long index = start >> PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002360 struct extent_buffer *eb;
2361 struct page *p;
2362 struct address_space *mapping = tree->mapping;
Chris Mason14048ed2007-10-15 16:16:28 -04002363 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002364
Chris Mason4dc119042007-10-15 16:18:14 -04002365 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002366 if (!eb || IS_ERR(eb))
2367 return NULL;
2368
Chris Mason4dc119042007-10-15 16:18:14 -04002369 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002370 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002371
2372 for (i = 0; i < num_pages; i++, index++) {
Chris Mason14048ed2007-10-15 16:16:28 -04002373 p = find_lock_page(mapping, index);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002374 if (!p) {
Chris Mason5f39d392007-10-15 16:14:19 -04002375 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002376 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002377 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002378 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002379
2380 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002381 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002382 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2383 len << 2);
2384 } else {
2385 set_page_private(p, EXTENT_PAGE_PRIVATE);
2386 }
2387
Chris Mason14048ed2007-10-15 16:16:28 -04002388 if (!PageUptodate(p))
2389 uptodate = 0;
2390 unlock_page(p);
Chris Mason5f39d392007-10-15 16:14:19 -04002391 }
Chris Mason14048ed2007-10-15 16:16:28 -04002392 if (uptodate)
2393 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002394 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002395
2396lru_add:
2397 spin_lock(&tree->lru_lock);
2398 add_lru(tree, eb);
2399 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002400 return eb;
2401fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002402 spin_lock(&tree->lru_lock);
2403 list_del_init(&eb->lru);
2404 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002405 if (!atomic_dec_and_test(&eb->refs))
2406 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002407 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002408 page_cache_release(extent_buffer_page(eb, index));
2409 }
Chris Mason0591fb52007-11-11 08:22:00 -05002410 if (i > 0)
2411 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002412 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002413 return NULL;
2414}
2415EXPORT_SYMBOL(find_extent_buffer);
2416
2417void free_extent_buffer(struct extent_buffer *eb)
2418{
2419 unsigned long i;
2420 unsigned long num_pages;
2421
2422 if (!eb)
2423 return;
2424
2425 if (!atomic_dec_and_test(&eb->refs))
2426 return;
2427
Chris Mason0591fb52007-11-11 08:22:00 -05002428 WARN_ON(!list_empty(&eb->lru));
Chris Masondb945352007-10-15 16:15:53 -04002429 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002430
Chris Mason0591fb52007-11-11 08:22:00 -05002431 for (i = 1; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002432 page_cache_release(extent_buffer_page(eb, i));
Chris Mason5f39d392007-10-15 16:14:19 -04002433 }
Chris Mason0591fb52007-11-11 08:22:00 -05002434 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason6d36dcd2007-10-15 16:14:37 -04002435 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002436}
2437EXPORT_SYMBOL(free_extent_buffer);
2438
2439int clear_extent_buffer_dirty(struct extent_map_tree *tree,
2440 struct extent_buffer *eb)
2441{
2442 int set;
2443 unsigned long i;
2444 unsigned long num_pages;
2445 struct page *page;
2446
2447 u64 start = eb->start;
2448 u64 end = start + eb->len - 1;
2449
2450 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
Chris Masondb945352007-10-15 16:15:53 -04002451 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002452
2453 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002454 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002455 lock_page(page);
2456 /*
2457 * if we're on the last page or the first page and the
2458 * block isn't aligned on a page boundary, do extra checks
2459 * to make sure we don't clean page that is partially dirty
2460 */
2461 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2462 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002463 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason35ebb932007-10-30 16:56:53 -04002464 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002465 end = start + PAGE_CACHE_SIZE - 1;
2466 if (test_range_bit(tree, start, end,
2467 EXTENT_DIRTY, 0)) {
2468 unlock_page(page);
2469 continue;
2470 }
2471 }
2472 clear_page_dirty_for_io(page);
Chris Mason7073c8e2007-11-20 13:44:45 -05002473 write_lock_irq(&page->mapping->tree_lock);
2474 if (!PageDirty(page)) {
2475 radix_tree_tag_clear(&page->mapping->page_tree,
2476 page_index(page),
2477 PAGECACHE_TAG_DIRTY);
2478 }
2479 write_unlock_irq(&page->mapping->tree_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002480 unlock_page(page);
2481 }
2482 return 0;
2483}
2484EXPORT_SYMBOL(clear_extent_buffer_dirty);
2485
2486int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
2487 struct extent_buffer *eb)
2488{
2489 return wait_on_extent_writeback(tree, eb->start,
2490 eb->start + eb->len - 1);
2491}
2492EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2493
2494int set_extent_buffer_dirty(struct extent_map_tree *tree,
2495 struct extent_buffer *eb)
2496{
Chris Mason810191f2007-10-15 16:18:55 -04002497 unsigned long i;
2498 unsigned long num_pages;
2499
2500 num_pages = num_extent_pages(eb->start, eb->len);
2501 for (i = 0; i < num_pages; i++) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002502 struct page *page = extent_buffer_page(eb, i);
2503 /* writepage may need to do something special for the
2504 * first page, we have to make sure page->private is
2505 * properly set. releasepage may drop page->private
2506 * on us if the page isn't already dirty.
2507 */
2508 if (i == 0) {
2509 lock_page(page);
2510 set_page_private(page,
2511 EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2512 eb->len << 2);
2513 }
Chris Mason810191f2007-10-15 16:18:55 -04002514 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Mason19c00dd2007-10-15 16:19:22 -04002515 if (i == 0)
2516 unlock_page(page);
Chris Mason810191f2007-10-15 16:18:55 -04002517 }
2518 return set_extent_dirty(tree, eb->start,
2519 eb->start + eb->len - 1, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002520}
2521EXPORT_SYMBOL(set_extent_buffer_dirty);
2522
2523int set_extent_buffer_uptodate(struct extent_map_tree *tree,
2524 struct extent_buffer *eb)
2525{
2526 unsigned long i;
2527 struct page *page;
2528 unsigned long num_pages;
2529
Chris Masondb945352007-10-15 16:15:53 -04002530 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002531
2532 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2533 GFP_NOFS);
2534 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002535 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002536 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2537 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002538 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason5f39d392007-10-15 16:14:19 -04002539 check_page_uptodate(tree, page);
2540 continue;
2541 }
2542 SetPageUptodate(page);
2543 }
2544 return 0;
2545}
2546EXPORT_SYMBOL(set_extent_buffer_uptodate);
2547
2548int extent_buffer_uptodate(struct extent_map_tree *tree,
2549 struct extent_buffer *eb)
2550{
2551 if (eb->flags & EXTENT_UPTODATE)
2552 return 1;
2553 return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
2554 EXTENT_UPTODATE, 1);
2555}
2556EXPORT_SYMBOL(extent_buffer_uptodate);
2557
2558int read_extent_buffer_pages(struct extent_map_tree *tree,
Chris Mason19c00dd2007-10-15 16:19:22 -04002559 struct extent_buffer *eb,
2560 u64 start,
2561 int wait)
Chris Mason5f39d392007-10-15 16:14:19 -04002562{
2563 unsigned long i;
Chris Mason19c00dd2007-10-15 16:19:22 -04002564 unsigned long start_i;
Chris Mason5f39d392007-10-15 16:14:19 -04002565 struct page *page;
2566 int err;
2567 int ret = 0;
2568 unsigned long num_pages;
2569
2570 if (eb->flags & EXTENT_UPTODATE)
2571 return 0;
2572
Chris Mason14048ed2007-10-15 16:16:28 -04002573 if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason5f39d392007-10-15 16:14:19 -04002574 EXTENT_UPTODATE, 1)) {
2575 return 0;
2576 }
Chris Mason0591fb52007-11-11 08:22:00 -05002577
Chris Mason19c00dd2007-10-15 16:19:22 -04002578 if (start) {
2579 WARN_ON(start < eb->start);
2580 start_i = (start >> PAGE_CACHE_SHIFT) -
2581 (eb->start >> PAGE_CACHE_SHIFT);
2582 } else {
2583 start_i = 0;
2584 }
Chris Mason5f39d392007-10-15 16:14:19 -04002585
Chris Masondb945352007-10-15 16:15:53 -04002586 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason19c00dd2007-10-15 16:19:22 -04002587 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002588 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002589 if (PageUptodate(page)) {
2590 continue;
2591 }
2592 if (!wait) {
2593 if (TestSetPageLocked(page)) {
2594 continue;
2595 }
2596 } else {
2597 lock_page(page);
2598 }
2599 if (!PageUptodate(page)) {
2600 err = page->mapping->a_ops->readpage(NULL, page);
2601 if (err) {
2602 ret = err;
2603 }
2604 } else {
2605 unlock_page(page);
2606 }
2607 }
2608
2609 if (ret || !wait) {
2610 return ret;
2611 }
2612
Chris Mason19c00dd2007-10-15 16:19:22 -04002613 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002614 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002615 wait_on_page_locked(page);
2616 if (!PageUptodate(page)) {
2617 ret = -EIO;
2618 }
2619 }
Chris Mason4dc119042007-10-15 16:18:14 -04002620 if (!ret)
2621 eb->flags |= EXTENT_UPTODATE;
Chris Mason5f39d392007-10-15 16:14:19 -04002622 return ret;
2623}
2624EXPORT_SYMBOL(read_extent_buffer_pages);
2625
2626void read_extent_buffer(struct extent_buffer *eb, void *dstv,
2627 unsigned long start,
2628 unsigned long len)
2629{
2630 size_t cur;
2631 size_t offset;
2632 struct page *page;
2633 char *kaddr;
2634 char *dst = (char *)dstv;
2635 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2636 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Mason14048ed2007-10-15 16:16:28 -04002637 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002638
2639 WARN_ON(start > eb->len);
2640 WARN_ON(start + len > eb->start + eb->len);
2641
Chris Mason3685f792007-10-19 09:23:27 -04002642 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002643
2644 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002645 page = extent_buffer_page(eb, i);
Chris Mason14048ed2007-10-15 16:16:28 -04002646 if (!PageUptodate(page)) {
2647 printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len);
2648 WARN_ON(1);
2649 }
Chris Mason5f39d392007-10-15 16:14:19 -04002650 WARN_ON(!PageUptodate(page));
2651
2652 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Mason59d169e2007-10-19 09:23:09 -04002653 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002654 memcpy(dst, kaddr + offset, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002655 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002656
2657 dst += cur;
2658 len -= cur;
2659 offset = 0;
2660 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002661 }
2662}
2663EXPORT_SYMBOL(read_extent_buffer);
2664
Chris Mason19c00dd2007-10-15 16:19:22 -04002665int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masondb945352007-10-15 16:15:53 -04002666 unsigned long min_len, char **token, char **map,
2667 unsigned long *map_start,
2668 unsigned long *map_len, int km)
Chris Mason5f39d392007-10-15 16:14:19 -04002669{
Chris Mason479965d2007-10-15 16:14:27 -04002670 size_t offset = start & (PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002672 struct page *p;
Chris Mason5f39d392007-10-15 16:14:19 -04002673 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2674 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Yan65555a02007-10-25 15:42:57 -04002675 unsigned long end_i = (start_offset + start + min_len - 1) >>
Chris Mason810191f2007-10-15 16:18:55 -04002676 PAGE_CACHE_SHIFT;
Chris Mason479965d2007-10-15 16:14:27 -04002677
2678 if (i != end_i)
2679 return -EINVAL;
Chris Mason5f39d392007-10-15 16:14:19 -04002680
Chris Mason5f39d392007-10-15 16:14:19 -04002681 if (i == 0) {
2682 offset = start_offset;
2683 *map_start = 0;
2684 } else {
Chris Masondb945352007-10-15 16:15:53 -04002685 offset = 0;
Chris Mason0591fb52007-11-11 08:22:00 -05002686 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
Chris Mason5f39d392007-10-15 16:14:19 -04002687 }
Yan65555a02007-10-25 15:42:57 -04002688 if (start + min_len > eb->len) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002689printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
2690 WARN_ON(1);
2691 }
Chris Mason5f39d392007-10-15 16:14:19 -04002692
Chris Masondb945352007-10-15 16:15:53 -04002693 p = extent_buffer_page(eb, i);
2694 WARN_ON(!PageUptodate(p));
2695 kaddr = kmap_atomic(p, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002696 *token = kaddr;
2697 *map = kaddr + offset;
2698 *map_len = PAGE_CACHE_SIZE - offset;
2699 return 0;
2700}
Chris Mason19c00dd2007-10-15 16:19:22 -04002701EXPORT_SYMBOL(map_private_extent_buffer);
Chris Masondb945352007-10-15 16:15:53 -04002702
2703int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
2704 unsigned long min_len,
2705 char **token, char **map,
2706 unsigned long *map_start,
2707 unsigned long *map_len, int km)
2708{
2709 int err;
2710 int save = 0;
2711 if (eb->map_token) {
Chris Masondb945352007-10-15 16:15:53 -04002712 unmap_extent_buffer(eb, eb->map_token, km);
2713 eb->map_token = NULL;
2714 save = 1;
2715 }
Chris Mason19c00dd2007-10-15 16:19:22 -04002716 err = map_private_extent_buffer(eb, start, min_len, token, map,
2717 map_start, map_len, km);
Chris Masondb945352007-10-15 16:15:53 -04002718 if (!err && save) {
2719 eb->map_token = *token;
2720 eb->kaddr = *map;
2721 eb->map_start = *map_start;
2722 eb->map_len = *map_len;
2723 }
2724 return err;
2725}
Chris Mason5f39d392007-10-15 16:14:19 -04002726EXPORT_SYMBOL(map_extent_buffer);
2727
2728void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
2729{
Chris Masonae5252b2007-10-15 16:14:41 -04002730 kunmap_atomic(token, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002731}
2732EXPORT_SYMBOL(unmap_extent_buffer);
2733
2734int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
2735 unsigned long start,
2736 unsigned long len)
2737{
2738 size_t cur;
2739 size_t offset;
2740 struct page *page;
2741 char *kaddr;
2742 char *ptr = (char *)ptrv;
2743 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2744 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2745 int ret = 0;
2746
2747 WARN_ON(start > eb->len);
2748 WARN_ON(start + len > eb->start + eb->len);
2749
Chris Mason3685f792007-10-19 09:23:27 -04002750 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002751
2752 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002753 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002754 WARN_ON(!PageUptodate(page));
2755
2756 cur = min(len, (PAGE_CACHE_SIZE - offset));
2757
Chris Masonae5252b2007-10-15 16:14:41 -04002758 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002759 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002760 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002761 if (ret)
2762 break;
2763
2764 ptr += cur;
2765 len -= cur;
2766 offset = 0;
2767 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002768 }
2769 return ret;
2770}
2771EXPORT_SYMBOL(memcmp_extent_buffer);
2772
2773void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
2774 unsigned long start, unsigned long len)
2775{
2776 size_t cur;
2777 size_t offset;
2778 struct page *page;
2779 char *kaddr;
2780 char *src = (char *)srcv;
2781 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2782 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2783
2784 WARN_ON(start > eb->len);
2785 WARN_ON(start + len > eb->start + eb->len);
2786
Chris Mason3685f792007-10-19 09:23:27 -04002787 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002788
2789 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002790 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002791 WARN_ON(!PageUptodate(page));
2792
2793 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Mason59d169e2007-10-19 09:23:09 -04002794 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002795 memcpy(kaddr + offset, src, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002796 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002797
2798 src += cur;
2799 len -= cur;
2800 offset = 0;
2801 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002802 }
2803}
2804EXPORT_SYMBOL(write_extent_buffer);
2805
2806void memset_extent_buffer(struct extent_buffer *eb, char c,
2807 unsigned long start, unsigned long len)
2808{
2809 size_t cur;
2810 size_t offset;
2811 struct page *page;
2812 char *kaddr;
2813 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2814 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2815
2816 WARN_ON(start > eb->len);
2817 WARN_ON(start + len > eb->start + eb->len);
2818
Chris Mason3685f792007-10-19 09:23:27 -04002819 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002820
2821 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002822 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 WARN_ON(!PageUptodate(page));
2824
2825 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masonae5252b2007-10-15 16:14:41 -04002826 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002827 memset(kaddr + offset, c, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002828 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002829
2830 len -= cur;
2831 offset = 0;
2832 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002833 }
2834}
2835EXPORT_SYMBOL(memset_extent_buffer);
2836
2837void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
2838 unsigned long dst_offset, unsigned long src_offset,
2839 unsigned long len)
2840{
2841 u64 dst_len = dst->len;
2842 size_t cur;
2843 size_t offset;
2844 struct page *page;
2845 char *kaddr;
2846 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2847 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2848
2849 WARN_ON(src->len != dst_len);
2850
Chris Mason3685f792007-10-19 09:23:27 -04002851 offset = (start_offset + dst_offset) &
2852 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002853
2854 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002855 page = extent_buffer_page(dst, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002856 WARN_ON(!PageUptodate(page));
2857
2858 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
2859
Chris Masonff190c02007-10-19 10:39:41 -04002860 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002861 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masonff190c02007-10-19 10:39:41 -04002862 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002863
2864 src_offset += cur;
2865 len -= cur;
2866 offset = 0;
2867 i++;
2868 }
2869}
2870EXPORT_SYMBOL(copy_extent_buffer);
2871
2872static void move_pages(struct page *dst_page, struct page *src_page,
2873 unsigned long dst_off, unsigned long src_off,
2874 unsigned long len)
2875{
Chris Masonae5252b2007-10-15 16:14:41 -04002876 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002877 if (dst_page == src_page) {
2878 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
2879 } else {
Chris Masonae5252b2007-10-15 16:14:41 -04002880 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002881 char *p = dst_kaddr + dst_off + len;
2882 char *s = src_kaddr + src_off + len;
2883
2884 while (len--)
2885 *--p = *--s;
2886
Chris Masonae5252b2007-10-15 16:14:41 -04002887 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002888 }
Chris Masonae5252b2007-10-15 16:14:41 -04002889 kunmap_atomic(dst_kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002890}
2891
2892static void copy_pages(struct page *dst_page, struct page *src_page,
2893 unsigned long dst_off, unsigned long src_off,
2894 unsigned long len)
2895{
Chris Masonae5252b2007-10-15 16:14:41 -04002896 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002897 char *src_kaddr;
2898
2899 if (dst_page != src_page)
Chris Masonae5252b2007-10-15 16:14:41 -04002900 src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002901 else
2902 src_kaddr = dst_kaddr;
2903
2904 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002905 kunmap_atomic(dst_kaddr, KM_USER0);
2906 if (dst_page != src_page)
2907 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002908}
2909
2910void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2911 unsigned long src_offset, unsigned long len)
2912{
2913 size_t cur;
2914 size_t dst_off_in_page;
2915 size_t src_off_in_page;
2916 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2917 unsigned long dst_i;
2918 unsigned long src_i;
2919
2920 if (src_offset + len > dst->len) {
2921 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2922 src_offset, len, dst->len);
2923 BUG_ON(1);
2924 }
2925 if (dst_offset + len > dst->len) {
2926 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2927 dst_offset, len, dst->len);
2928 BUG_ON(1);
2929 }
2930
2931 while(len > 0) {
Chris Mason3685f792007-10-19 09:23:27 -04002932 dst_off_in_page = (start_offset + dst_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002933 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002934 src_off_in_page = (start_offset + src_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002935 ((unsigned long)PAGE_CACHE_SIZE - 1);
2936
2937 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2938 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
2939
Chris Mason5f39d392007-10-15 16:14:19 -04002940 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
2941 src_off_in_page));
Jens Axboeae2f5412007-10-19 09:22:59 -04002942 cur = min_t(unsigned long, cur,
2943 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
Chris Mason5f39d392007-10-15 16:14:19 -04002944
Chris Mason6d36dcd2007-10-15 16:14:37 -04002945 copy_pages(extent_buffer_page(dst, dst_i),
2946 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002947 dst_off_in_page, src_off_in_page, cur);
2948
2949 src_offset += cur;
2950 dst_offset += cur;
2951 len -= cur;
2952 }
2953}
2954EXPORT_SYMBOL(memcpy_extent_buffer);
2955
2956void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2957 unsigned long src_offset, unsigned long len)
2958{
2959 size_t cur;
2960 size_t dst_off_in_page;
2961 size_t src_off_in_page;
2962 unsigned long dst_end = dst_offset + len - 1;
2963 unsigned long src_end = src_offset + len - 1;
2964 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2965 unsigned long dst_i;
2966 unsigned long src_i;
2967
2968 if (src_offset + len > dst->len) {
2969 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2970 src_offset, len, dst->len);
2971 BUG_ON(1);
2972 }
2973 if (dst_offset + len > dst->len) {
2974 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2975 dst_offset, len, dst->len);
2976 BUG_ON(1);
2977 }
2978 if (dst_offset < src_offset) {
2979 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
2980 return;
2981 }
2982 while(len > 0) {
2983 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
2984 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
2985
Chris Mason3685f792007-10-19 09:23:27 -04002986 dst_off_in_page = (start_offset + dst_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04002987 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002988 src_off_in_page = (start_offset + src_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04002989 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002990
Jens Axboeae2f5412007-10-19 09:22:59 -04002991 cur = min_t(unsigned long, len, src_off_in_page + 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002992 cur = min(cur, dst_off_in_page + 1);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002993 move_pages(extent_buffer_page(dst, dst_i),
2994 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002995 dst_off_in_page - cur + 1,
2996 src_off_in_page - cur + 1, cur);
2997
Chris Masondb945352007-10-15 16:15:53 -04002998 dst_end -= cur;
2999 src_end -= cur;
Chris Mason5f39d392007-10-15 16:14:19 -04003000 len -= cur;
3001 }
3002}
3003EXPORT_SYMBOL(memmove_extent_buffer);