blob: 06e437723dc367940619a258a405413d1190b463 [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};
Chris Masonca664622007-11-27 11:16:35 -050045
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050046int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040047{
Chris Mason86479a02007-09-10 19:58:16 -040048 extent_map_cache = btrfs_cache_create("extent_map",
Chris Mason6d36dcd2007-10-15 16:14:37 -040049 sizeof(struct extent_map), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040050 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050051 if (!extent_map_cache)
52 return -ENOMEM;
Chris Mason86479a02007-09-10 19:58:16 -040053 extent_state_cache = btrfs_cache_create("extent_state",
Chris Mason6d36dcd2007-10-15 16:14:37 -040054 sizeof(struct extent_state), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040055 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050056 if (!extent_state_cache)
57 goto free_map_cache;
Chris Mason6d36dcd2007-10-15 16:14:37 -040058 extent_buffer_cache = btrfs_cache_create("extent_buffers",
59 sizeof(struct extent_buffer), 0,
60 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050061 if (!extent_buffer_cache)
62 goto free_state_cache;
63 return 0;
64
65free_state_cache:
66 kmem_cache_destroy(extent_state_cache);
67free_map_cache:
68 kmem_cache_destroy(extent_map_cache);
69 return -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -040070}
71
72void __exit extent_map_exit(void)
73{
Chris Masonf510cfe2007-10-15 16:14:48 -040074 struct extent_state *state;
Chris Mason6d36dcd2007-10-15 16:14:37 -040075
Chris Masonf510cfe2007-10-15 16:14:48 -040076 while (!list_empty(&states)) {
77 state = list_entry(states.next, struct extent_state, list);
78 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));
79 list_del(&state->list);
80 kmem_cache_free(extent_state_cache, state);
81
82 }
Chris Masonf510cfe2007-10-15 16:14:48 -040083
Chris Masona52d9a82007-08-27 16:49:44 -040084 if (extent_map_cache)
85 kmem_cache_destroy(extent_map_cache);
86 if (extent_state_cache)
87 kmem_cache_destroy(extent_state_cache);
Chris Mason6d36dcd2007-10-15 16:14:37 -040088 if (extent_buffer_cache)
89 kmem_cache_destroy(extent_buffer_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040090}
91
92void extent_map_tree_init(struct extent_map_tree *tree,
93 struct address_space *mapping, gfp_t mask)
94{
95 tree->map.rb_node = NULL;
96 tree->state.rb_node = NULL;
Chris Mason07157aa2007-08-30 08:50:51 -040097 tree->ops = NULL;
Chris Masonca664622007-11-27 11:16:35 -050098 tree->dirty_bytes = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040099 rwlock_init(&tree->lock);
Chris Mason4dc119042007-10-15 16:18:14 -0400100 spin_lock_init(&tree->lru_lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400101 tree->mapping = mapping;
Chris Mason4dc119042007-10-15 16:18:14 -0400102 INIT_LIST_HEAD(&tree->buffer_lru);
103 tree->lru_size = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400104}
105EXPORT_SYMBOL(extent_map_tree_init);
106
Chris Mason19c00dd2007-10-15 16:19:22 -0400107void extent_map_tree_empty_lru(struct extent_map_tree *tree)
Chris Mason4dc119042007-10-15 16:18:14 -0400108{
109 struct extent_buffer *eb;
110 while(!list_empty(&tree->buffer_lru)) {
111 eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
112 lru);
Chris Mason0591fb52007-11-11 08:22:00 -0500113 list_del_init(&eb->lru);
Chris Mason4dc119042007-10-15 16:18:14 -0400114 free_extent_buffer(eb);
115 }
116}
Chris Mason19c00dd2007-10-15 16:19:22 -0400117EXPORT_SYMBOL(extent_map_tree_empty_lru);
Chris Mason4dc119042007-10-15 16:18:14 -0400118
Chris Masona52d9a82007-08-27 16:49:44 -0400119struct extent_map *alloc_extent_map(gfp_t mask)
120{
121 struct extent_map *em;
122 em = kmem_cache_alloc(extent_map_cache, mask);
123 if (!em || IS_ERR(em))
124 return em;
125 em->in_tree = 0;
126 atomic_set(&em->refs, 1);
127 return em;
128}
129EXPORT_SYMBOL(alloc_extent_map);
130
131void free_extent_map(struct extent_map *em)
132{
Chris Mason2bf5a722007-08-30 11:54:02 -0400133 if (!em)
134 return;
Chris Masona52d9a82007-08-27 16:49:44 -0400135 if (atomic_dec_and_test(&em->refs)) {
136 WARN_ON(em->in_tree);
137 kmem_cache_free(extent_map_cache, em);
138 }
139}
140EXPORT_SYMBOL(free_extent_map);
141
142
143struct extent_state *alloc_extent_state(gfp_t mask)
144{
145 struct extent_state *state;
Chris Masonf510cfe2007-10-15 16:14:48 -0400146 unsigned long flags;
147
Chris Masona52d9a82007-08-27 16:49:44 -0400148 state = kmem_cache_alloc(extent_state_cache, mask);
149 if (!state || IS_ERR(state))
150 return state;
151 state->state = 0;
152 state->in_tree = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400153 state->private = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400154
155 spin_lock_irqsave(&state_lock, flags);
156 list_add(&state->list, &states);
157 spin_unlock_irqrestore(&state_lock, flags);
158
Chris Masona52d9a82007-08-27 16:49:44 -0400159 atomic_set(&state->refs, 1);
160 init_waitqueue_head(&state->wq);
Chris Masona52d9a82007-08-27 16:49:44 -0400161 return state;
162}
163EXPORT_SYMBOL(alloc_extent_state);
164
165void free_extent_state(struct extent_state *state)
166{
Chris Masonf510cfe2007-10-15 16:14:48 -0400167 unsigned long flags;
Chris Mason2bf5a722007-08-30 11:54:02 -0400168 if (!state)
169 return;
Chris Masona52d9a82007-08-27 16:49:44 -0400170 if (atomic_dec_and_test(&state->refs)) {
171 WARN_ON(state->in_tree);
Chris Masonf510cfe2007-10-15 16:14:48 -0400172 spin_lock_irqsave(&state_lock, flags);
173 list_del(&state->list);
174 spin_unlock_irqrestore(&state_lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400175 kmem_cache_free(extent_state_cache, state);
176 }
177}
178EXPORT_SYMBOL(free_extent_state);
179
180static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
181 struct rb_node *node)
182{
183 struct rb_node ** p = &root->rb_node;
184 struct rb_node * parent = NULL;
185 struct tree_entry *entry;
186
187 while(*p) {
188 parent = *p;
189 entry = rb_entry(parent, struct tree_entry, rb_node);
190
191 if (offset < entry->start)
192 p = &(*p)->rb_left;
193 else if (offset > entry->end)
194 p = &(*p)->rb_right;
195 else
196 return parent;
197 }
198
199 entry = rb_entry(node, struct tree_entry, rb_node);
200 entry->in_tree = 1;
201 rb_link_node(node, parent, p);
202 rb_insert_color(node, root);
203 return NULL;
204}
205
206static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
207 struct rb_node **prev_ret)
208{
209 struct rb_node * n = root->rb_node;
210 struct rb_node *prev = NULL;
211 struct tree_entry *entry;
212 struct tree_entry *prev_entry = NULL;
213
214 while(n) {
215 entry = rb_entry(n, struct tree_entry, rb_node);
216 prev = n;
217 prev_entry = entry;
218
219 if (offset < entry->start)
220 n = n->rb_left;
221 else if (offset > entry->end)
222 n = n->rb_right;
223 else
224 return n;
225 }
226 if (!prev_ret)
227 return NULL;
228 while(prev && offset > prev_entry->end) {
229 prev = rb_next(prev);
230 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
231 }
232 *prev_ret = prev;
233 return NULL;
234}
235
236static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
237{
238 struct rb_node *prev;
239 struct rb_node *ret;
240 ret = __tree_search(root, offset, &prev);
241 if (!ret)
242 return prev;
243 return ret;
244}
245
246static int tree_delete(struct rb_root *root, u64 offset)
247{
248 struct rb_node *node;
249 struct tree_entry *entry;
250
251 node = __tree_search(root, offset, NULL);
252 if (!node)
253 return -ENOENT;
254 entry = rb_entry(node, struct tree_entry, rb_node);
255 entry->in_tree = 0;
256 rb_erase(node, root);
257 return 0;
258}
259
260/*
261 * add_extent_mapping tries a simple backward merge with existing
262 * mappings. The extent_map struct passed in will be inserted into
263 * the tree directly (no copies made, just a reference taken).
264 */
265int add_extent_mapping(struct extent_map_tree *tree,
266 struct extent_map *em)
267{
268 int ret = 0;
269 struct extent_map *prev = NULL;
270 struct rb_node *rb;
271
272 write_lock_irq(&tree->lock);
273 rb = tree_insert(&tree->map, em->end, &em->rb_node);
274 if (rb) {
275 prev = rb_entry(rb, struct extent_map, rb_node);
276 printk("found extent map %Lu %Lu on insert of %Lu %Lu\n", prev->start, prev->end, em->start, em->end);
277 ret = -EEXIST;
278 goto out;
279 }
280 atomic_inc(&em->refs);
281 if (em->start != 0) {
282 rb = rb_prev(&em->rb_node);
283 if (rb)
284 prev = rb_entry(rb, struct extent_map, rb_node);
285 if (prev && prev->end + 1 == em->start &&
Chris Mason5f39d392007-10-15 16:14:19 -0400286 ((em->block_start == EXTENT_MAP_HOLE &&
287 prev->block_start == EXTENT_MAP_HOLE) ||
Chris Mason179e29e2007-11-01 11:28:41 -0400288 (em->block_start == EXTENT_MAP_INLINE &&
289 prev->block_start == EXTENT_MAP_INLINE) ||
290 (em->block_start == EXTENT_MAP_DELALLOC &&
291 prev->block_start == EXTENT_MAP_DELALLOC) ||
292 (em->block_start < EXTENT_MAP_DELALLOC - 1 &&
293 em->block_start == prev->block_end + 1))) {
Chris Masona52d9a82007-08-27 16:49:44 -0400294 em->start = prev->start;
295 em->block_start = prev->block_start;
296 rb_erase(&prev->rb_node, &tree->map);
297 prev->in_tree = 0;
298 free_extent_map(prev);
299 }
300 }
301out:
302 write_unlock_irq(&tree->lock);
303 return ret;
304}
305EXPORT_SYMBOL(add_extent_mapping);
306
307/*
308 * lookup_extent_mapping returns the first extent_map struct in the
309 * tree that intersects the [start, end] (inclusive) range. There may
310 * be additional objects in the tree that intersect, so check the object
311 * returned carefully to make sure you don't need additional lookups.
312 */
313struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
314 u64 start, u64 end)
315{
316 struct extent_map *em;
317 struct rb_node *rb_node;
318
319 read_lock_irq(&tree->lock);
320 rb_node = tree_search(&tree->map, start);
321 if (!rb_node) {
322 em = NULL;
323 goto out;
324 }
325 if (IS_ERR(rb_node)) {
326 em = ERR_PTR(PTR_ERR(rb_node));
327 goto out;
328 }
329 em = rb_entry(rb_node, struct extent_map, rb_node);
330 if (em->end < start || em->start > end) {
331 em = NULL;
332 goto out;
333 }
334 atomic_inc(&em->refs);
335out:
336 read_unlock_irq(&tree->lock);
337 return em;
338}
339EXPORT_SYMBOL(lookup_extent_mapping);
340
341/*
342 * removes an extent_map struct from the tree. No reference counts are
343 * dropped, and no checks are done to see if the range is in use
344 */
345int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
346{
347 int ret;
348
349 write_lock_irq(&tree->lock);
350 ret = tree_delete(&tree->map, em->end);
351 write_unlock_irq(&tree->lock);
352 return ret;
353}
354EXPORT_SYMBOL(remove_extent_mapping);
355
356/*
357 * utility function to look for merge candidates inside a given range.
358 * Any extents with matching state are merged together into a single
359 * extent in the tree. Extents with EXTENT_IO in their state field
360 * are not merged because the end_io handlers need to be able to do
361 * operations on them without sleeping (or doing allocations/splits).
362 *
363 * This should be called with the tree lock held.
364 */
365static int merge_state(struct extent_map_tree *tree,
366 struct extent_state *state)
367{
368 struct extent_state *other;
369 struct rb_node *other_node;
370
371 if (state->state & EXTENT_IOBITS)
372 return 0;
373
374 other_node = rb_prev(&state->rb_node);
375 if (other_node) {
376 other = rb_entry(other_node, struct extent_state, rb_node);
377 if (other->end == state->start - 1 &&
378 other->state == state->state) {
379 state->start = other->start;
380 other->in_tree = 0;
381 rb_erase(&other->rb_node, &tree->state);
382 free_extent_state(other);
383 }
384 }
385 other_node = rb_next(&state->rb_node);
386 if (other_node) {
387 other = rb_entry(other_node, struct extent_state, rb_node);
388 if (other->start == state->end + 1 &&
389 other->state == state->state) {
390 other->start = state->start;
391 state->in_tree = 0;
392 rb_erase(&state->rb_node, &tree->state);
393 free_extent_state(state);
394 }
395 }
396 return 0;
397}
398
399/*
400 * insert an extent_state struct into the tree. 'bits' are set on the
401 * struct before it is inserted.
402 *
403 * This may return -EEXIST if the extent is already there, in which case the
404 * state struct is freed.
405 *
406 * The tree lock is not taken internally. This is a utility function and
407 * probably isn't what you want to call (see set/clear_extent_bit).
408 */
409static int insert_state(struct extent_map_tree *tree,
410 struct extent_state *state, u64 start, u64 end,
411 int bits)
412{
413 struct rb_node *node;
414
415 if (end < start) {
416 printk("end < start %Lu %Lu\n", end, start);
417 WARN_ON(1);
418 }
Chris Masonca664622007-11-27 11:16:35 -0500419 if (bits & EXTENT_DIRTY)
420 tree->dirty_bytes += end - start + 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400421 state->state |= bits;
422 state->start = start;
423 state->end = end;
Chris Masona52d9a82007-08-27 16:49:44 -0400424 node = tree_insert(&tree->state, end, &state->rb_node);
425 if (node) {
426 struct extent_state *found;
427 found = rb_entry(node, struct extent_state, rb_node);
Chris Masonb888db22007-08-27 16:49:44 -0400428 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -0400429 free_extent_state(state);
430 return -EEXIST;
431 }
432 merge_state(tree, state);
433 return 0;
434}
435
436/*
437 * split a given extent state struct in two, inserting the preallocated
438 * struct 'prealloc' as the newly created second half. 'split' indicates an
439 * offset inside 'orig' where it should be split.
440 *
441 * Before calling,
442 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
443 * are two extent state structs in the tree:
444 * prealloc: [orig->start, split - 1]
445 * orig: [ split, orig->end ]
446 *
447 * The tree locks are not taken by this function. They need to be held
448 * by the caller.
449 */
450static int split_state(struct extent_map_tree *tree, struct extent_state *orig,
451 struct extent_state *prealloc, u64 split)
452{
453 struct rb_node *node;
454 prealloc->start = orig->start;
455 prealloc->end = split - 1;
456 prealloc->state = orig->state;
457 orig->start = split;
Chris Masonf510cfe2007-10-15 16:14:48 -0400458
Chris Masona52d9a82007-08-27 16:49:44 -0400459 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
460 if (node) {
461 struct extent_state *found;
462 found = rb_entry(node, struct extent_state, rb_node);
Chris Masonb888db22007-08-27 16:49:44 -0400463 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 -0400464 free_extent_state(prealloc);
465 return -EEXIST;
466 }
467 return 0;
468}
469
470/*
471 * utility function to clear some bits in an extent state struct.
472 * it will optionally wake up any one waiting on this state (wake == 1), or
473 * forcibly remove the state from the tree (delete == 1).
474 *
475 * If no bits are set on the state struct after clearing things, the
476 * struct is freed and removed from the tree
477 */
478static int clear_state_bit(struct extent_map_tree *tree,
479 struct extent_state *state, int bits, int wake,
480 int delete)
481{
482 int ret = state->state & bits;
Chris Masonca664622007-11-27 11:16:35 -0500483
484 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
485 u64 range = state->end - state->start + 1;
486 WARN_ON(range > tree->dirty_bytes);
487 tree->dirty_bytes -= range;
488 }
Chris Masona52d9a82007-08-27 16:49:44 -0400489 state->state &= ~bits;
490 if (wake)
491 wake_up(&state->wq);
492 if (delete || state->state == 0) {
493 if (state->in_tree) {
494 rb_erase(&state->rb_node, &tree->state);
495 state->in_tree = 0;
496 free_extent_state(state);
497 } else {
498 WARN_ON(1);
499 }
500 } else {
501 merge_state(tree, state);
502 }
503 return ret;
504}
505
506/*
507 * clear some bits on a range in the tree. This may require splitting
508 * or inserting elements in the tree, so the gfp mask is used to
509 * indicate which allocations or sleeping are allowed.
510 *
511 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
512 * the given range from the tree regardless of state (ie for truncate).
513 *
514 * the range [start, end] is inclusive.
515 *
516 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
517 * bits were already set, or zero if none of the bits were already set.
518 */
519int clear_extent_bit(struct extent_map_tree *tree, u64 start, u64 end,
520 int bits, int wake, int delete, gfp_t mask)
521{
522 struct extent_state *state;
523 struct extent_state *prealloc = NULL;
524 struct rb_node *node;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400525 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400526 int err;
527 int set = 0;
528
529again:
530 if (!prealloc && (mask & __GFP_WAIT)) {
531 prealloc = alloc_extent_state(mask);
532 if (!prealloc)
533 return -ENOMEM;
534 }
535
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400536 write_lock_irqsave(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400537 /*
538 * this search will find the extents that end after
539 * our range starts
540 */
541 node = tree_search(&tree->state, start);
542 if (!node)
543 goto out;
544 state = rb_entry(node, struct extent_state, rb_node);
545 if (state->start > end)
546 goto out;
547 WARN_ON(state->end < start);
548
549 /*
550 * | ---- desired range ---- |
551 * | state | or
552 * | ------------- state -------------- |
553 *
554 * We need to split the extent we found, and may flip
555 * bits on second half.
556 *
557 * If the extent we found extends past our range, we
558 * just split and search again. It'll get split again
559 * the next time though.
560 *
561 * If the extent we found is inside our range, we clear
562 * the desired bit on it.
563 */
564
565 if (state->start < start) {
566 err = split_state(tree, state, prealloc, start);
567 BUG_ON(err == -EEXIST);
568 prealloc = NULL;
569 if (err)
570 goto out;
571 if (state->end <= end) {
572 start = state->end + 1;
573 set |= clear_state_bit(tree, state, bits,
574 wake, delete);
575 } else {
576 start = state->start;
577 }
578 goto search_again;
579 }
580 /*
581 * | ---- desired range ---- |
582 * | state |
583 * We need to split the extent, and clear the bit
584 * on the first half
585 */
586 if (state->start <= end && state->end > end) {
587 err = split_state(tree, state, prealloc, end + 1);
588 BUG_ON(err == -EEXIST);
589
590 if (wake)
591 wake_up(&state->wq);
592 set |= clear_state_bit(tree, prealloc, bits,
593 wake, delete);
594 prealloc = NULL;
595 goto out;
596 }
597
598 start = state->end + 1;
599 set |= clear_state_bit(tree, state, bits, wake, delete);
600 goto search_again;
601
602out:
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400603 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400604 if (prealloc)
605 free_extent_state(prealloc);
606
607 return set;
608
609search_again:
Chris Mason96b51792007-10-15 16:15:19 -0400610 if (start > end)
Chris Masona52d9a82007-08-27 16:49:44 -0400611 goto out;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400612 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400613 if (mask & __GFP_WAIT)
614 cond_resched();
615 goto again;
616}
617EXPORT_SYMBOL(clear_extent_bit);
618
619static int wait_on_state(struct extent_map_tree *tree,
620 struct extent_state *state)
621{
622 DEFINE_WAIT(wait);
623 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
624 read_unlock_irq(&tree->lock);
625 schedule();
626 read_lock_irq(&tree->lock);
627 finish_wait(&state->wq, &wait);
628 return 0;
629}
630
631/*
632 * waits for one or more bits to clear on a range in the state tree.
633 * The range [start, end] is inclusive.
634 * The tree lock is taken by this function
635 */
636int wait_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits)
637{
638 struct extent_state *state;
639 struct rb_node *node;
640
641 read_lock_irq(&tree->lock);
642again:
643 while (1) {
644 /*
645 * this search will find all the extents that end after
646 * our range starts
647 */
648 node = tree_search(&tree->state, start);
649 if (!node)
650 break;
651
652 state = rb_entry(node, struct extent_state, rb_node);
653
654 if (state->start > end)
655 goto out;
656
657 if (state->state & bits) {
658 start = state->start;
659 atomic_inc(&state->refs);
660 wait_on_state(tree, state);
661 free_extent_state(state);
662 goto again;
663 }
664 start = state->end + 1;
665
666 if (start > end)
667 break;
668
669 if (need_resched()) {
670 read_unlock_irq(&tree->lock);
671 cond_resched();
672 read_lock_irq(&tree->lock);
673 }
674 }
675out:
676 read_unlock_irq(&tree->lock);
677 return 0;
678}
679EXPORT_SYMBOL(wait_extent_bit);
680
Chris Masonca664622007-11-27 11:16:35 -0500681static void set_state_bits(struct extent_map_tree *tree,
682 struct extent_state *state,
683 int bits)
684{
685 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
686 u64 range = state->end - state->start + 1;
687 tree->dirty_bytes += range;
688 }
689 state->state |= bits;
690}
691
Chris Masona52d9a82007-08-27 16:49:44 -0400692/*
693 * set some bits on a range in the tree. This may require allocations
694 * or sleeping, so the gfp mask is used to indicate what is allowed.
695 *
696 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
697 * range already has the desired bits set. The start of the existing
698 * range is returned in failed_start in this case.
699 *
700 * [start, end] is inclusive
701 * This takes the tree lock.
702 */
703int set_extent_bit(struct extent_map_tree *tree, u64 start, u64 end, int bits,
704 int exclusive, u64 *failed_start, gfp_t mask)
705{
706 struct extent_state *state;
707 struct extent_state *prealloc = NULL;
708 struct rb_node *node;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400709 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400710 int err = 0;
711 int set;
712 u64 last_start;
713 u64 last_end;
714again:
715 if (!prealloc && (mask & __GFP_WAIT)) {
716 prealloc = alloc_extent_state(mask);
717 if (!prealloc)
718 return -ENOMEM;
719 }
720
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400721 write_lock_irqsave(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400722 /*
723 * this search will find all the extents that end after
724 * our range starts.
725 */
726 node = tree_search(&tree->state, start);
727 if (!node) {
728 err = insert_state(tree, prealloc, start, end, bits);
729 prealloc = NULL;
730 BUG_ON(err == -EEXIST);
731 goto out;
732 }
733
734 state = rb_entry(node, struct extent_state, rb_node);
735 last_start = state->start;
736 last_end = state->end;
737
738 /*
739 * | ---- desired range ---- |
740 * | state |
741 *
742 * Just lock what we found and keep going
743 */
744 if (state->start == start && state->end <= end) {
745 set = state->state & bits;
746 if (set && exclusive) {
747 *failed_start = state->start;
748 err = -EEXIST;
749 goto out;
750 }
Chris Masonca664622007-11-27 11:16:35 -0500751 set_state_bits(tree, state, bits);
Chris Masona52d9a82007-08-27 16:49:44 -0400752 start = state->end + 1;
753 merge_state(tree, state);
754 goto search_again;
755 }
756
757 /*
758 * | ---- desired range ---- |
759 * | state |
760 * or
761 * | ------------- state -------------- |
762 *
763 * We need to split the extent we found, and may flip bits on
764 * second half.
765 *
766 * If the extent we found extends past our
767 * range, we just split and search again. It'll get split
768 * again the next time though.
769 *
770 * If the extent we found is inside our range, we set the
771 * desired bit on it.
772 */
773 if (state->start < start) {
774 set = state->state & bits;
775 if (exclusive && set) {
776 *failed_start = start;
777 err = -EEXIST;
778 goto out;
779 }
780 err = split_state(tree, state, prealloc, start);
781 BUG_ON(err == -EEXIST);
782 prealloc = NULL;
783 if (err)
784 goto out;
785 if (state->end <= end) {
Chris Masonca664622007-11-27 11:16:35 -0500786 set_state_bits(tree, state, bits);
Chris Masona52d9a82007-08-27 16:49:44 -0400787 start = state->end + 1;
788 merge_state(tree, state);
789 } else {
790 start = state->start;
791 }
792 goto search_again;
793 }
794 /*
795 * | ---- desired range ---- |
Chris Masona52d9a82007-08-27 16:49:44 -0400796 * | state | or | state |
797 *
798 * There's a hole, we need to insert something in it and
799 * ignore the extent we found.
800 */
801 if (state->start > start) {
802 u64 this_end;
803 if (end < last_start)
804 this_end = end;
805 else
806 this_end = last_start -1;
807 err = insert_state(tree, prealloc, start, this_end,
808 bits);
809 prealloc = NULL;
810 BUG_ON(err == -EEXIST);
811 if (err)
812 goto out;
813 start = this_end + 1;
814 goto search_again;
815 }
Chris Masona8c450b2007-09-10 20:00:27 -0400816 /*
817 * | ---- desired range ---- |
818 * | state |
819 * We need to split the extent, and set the bit
820 * on the first half
821 */
822 if (state->start <= end && state->end > end) {
823 set = state->state & bits;
824 if (exclusive && set) {
825 *failed_start = start;
826 err = -EEXIST;
827 goto out;
828 }
829 err = split_state(tree, state, prealloc, end + 1);
830 BUG_ON(err == -EEXIST);
831
Chris Masonca664622007-11-27 11:16:35 -0500832 set_state_bits(tree, prealloc, bits);
Chris Masona8c450b2007-09-10 20:00:27 -0400833 merge_state(tree, prealloc);
834 prealloc = NULL;
835 goto out;
836 }
837
Chris Masona52d9a82007-08-27 16:49:44 -0400838 goto search_again;
839
840out:
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400841 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400842 if (prealloc)
843 free_extent_state(prealloc);
844
845 return err;
846
847search_again:
848 if (start > end)
849 goto out;
Christoph Hellwig90f1c192007-09-10 20:02:27 -0400850 write_unlock_irqrestore(&tree->lock, flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400851 if (mask & __GFP_WAIT)
852 cond_resched();
853 goto again;
854}
855EXPORT_SYMBOL(set_extent_bit);
856
857/* wrappers around set/clear extent bit */
858int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
859 gfp_t mask)
860{
861 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
862 mask);
863}
864EXPORT_SYMBOL(set_extent_dirty);
865
Chris Mason96b51792007-10-15 16:15:19 -0400866int set_extent_bits(struct extent_map_tree *tree, u64 start, u64 end,
867 int bits, gfp_t mask)
868{
869 return set_extent_bit(tree, start, end, bits, 0, NULL,
870 mask);
871}
872EXPORT_SYMBOL(set_extent_bits);
873
874int clear_extent_bits(struct extent_map_tree *tree, u64 start, u64 end,
875 int bits, gfp_t mask)
876{
877 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
878}
879EXPORT_SYMBOL(clear_extent_bits);
880
Chris Masonb888db22007-08-27 16:49:44 -0400881int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
882 gfp_t mask)
883{
884 return set_extent_bit(tree, start, end,
885 EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
886 mask);
887}
888EXPORT_SYMBOL(set_extent_delalloc);
889
Chris Masona52d9a82007-08-27 16:49:44 -0400890int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
891 gfp_t mask)
892{
Chris Masonb888db22007-08-27 16:49:44 -0400893 return clear_extent_bit(tree, start, end,
894 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
Chris Masona52d9a82007-08-27 16:49:44 -0400895}
896EXPORT_SYMBOL(clear_extent_dirty);
897
898int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
899 gfp_t mask)
900{
901 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
902 mask);
903}
904EXPORT_SYMBOL(set_extent_new);
905
906int clear_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
907 gfp_t mask)
908{
909 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
910}
911EXPORT_SYMBOL(clear_extent_new);
912
913int set_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end,
914 gfp_t mask)
915{
916 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
917 mask);
918}
919EXPORT_SYMBOL(set_extent_uptodate);
920
921int clear_extent_uptodate(struct extent_map_tree *tree, u64 start, u64 end,
922 gfp_t mask)
923{
924 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
925}
926EXPORT_SYMBOL(clear_extent_uptodate);
927
928int set_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end,
929 gfp_t mask)
930{
931 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
932 0, NULL, mask);
933}
934EXPORT_SYMBOL(set_extent_writeback);
935
936int clear_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end,
937 gfp_t mask)
938{
939 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
940}
941EXPORT_SYMBOL(clear_extent_writeback);
942
943int wait_on_extent_writeback(struct extent_map_tree *tree, u64 start, u64 end)
944{
945 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
946}
947EXPORT_SYMBOL(wait_on_extent_writeback);
948
949/*
950 * locks a range in ascending order, waiting for any locked regions
951 * it hits on the way. [start,end] are inclusive, and this will sleep.
952 */
953int lock_extent(struct extent_map_tree *tree, u64 start, u64 end, gfp_t mask)
954{
955 int err;
956 u64 failed_start;
957 while (1) {
958 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
959 &failed_start, mask);
960 if (err == -EEXIST && (mask & __GFP_WAIT)) {
961 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
962 start = failed_start;
963 } else {
964 break;
965 }
966 WARN_ON(start > end);
967 }
968 return err;
969}
970EXPORT_SYMBOL(lock_extent);
971
972int unlock_extent(struct extent_map_tree *tree, u64 start, u64 end,
973 gfp_t mask)
974{
975 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
976}
977EXPORT_SYMBOL(unlock_extent);
978
979/*
980 * helper function to set pages and extents in the tree dirty
981 */
982int set_range_dirty(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_dirty_nobuffers(page);
992 page_cache_release(page);
993 index++;
994 }
995 set_extent_dirty(tree, start, end, GFP_NOFS);
996 return 0;
997}
998EXPORT_SYMBOL(set_range_dirty);
999
1000/*
1001 * helper function to set both pages and extents in the tree writeback
1002 */
1003int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end)
1004{
1005 unsigned long index = start >> PAGE_CACHE_SHIFT;
1006 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1007 struct page *page;
1008
1009 while (index <= end_index) {
1010 page = find_get_page(tree->mapping, index);
1011 BUG_ON(!page);
1012 set_page_writeback(page);
1013 page_cache_release(page);
1014 index++;
1015 }
1016 set_extent_writeback(tree, start, end, GFP_NOFS);
1017 return 0;
1018}
1019EXPORT_SYMBOL(set_range_writeback);
1020
Chris Mason5f39d392007-10-15 16:14:19 -04001021int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
1022 u64 *start_ret, u64 *end_ret, int bits)
1023{
1024 struct rb_node *node;
1025 struct extent_state *state;
1026 int ret = 1;
1027
Chris Masone19caa52007-10-15 16:17:44 -04001028 read_lock_irq(&tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001029 /*
1030 * this search will find all the extents that end after
1031 * our range starts.
1032 */
1033 node = tree_search(&tree->state, start);
1034 if (!node || IS_ERR(node)) {
1035 goto out;
1036 }
1037
1038 while(1) {
1039 state = rb_entry(node, struct extent_state, rb_node);
Chris Masone19caa52007-10-15 16:17:44 -04001040 if (state->end >= start && (state->state & bits)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001041 *start_ret = state->start;
1042 *end_ret = state->end;
1043 ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -04001044 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001045 }
1046 node = rb_next(node);
1047 if (!node)
1048 break;
1049 }
1050out:
Chris Masone19caa52007-10-15 16:17:44 -04001051 read_unlock_irq(&tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001052 return ret;
1053}
1054EXPORT_SYMBOL(find_first_extent_bit);
1055
Chris Masonb888db22007-08-27 16:49:44 -04001056u64 find_lock_delalloc_range(struct extent_map_tree *tree,
Chris Mason3e9fd942007-11-20 10:47:25 -05001057 u64 *start, u64 *end, u64 max_bytes)
Chris Masonb888db22007-08-27 16:49:44 -04001058{
1059 struct rb_node *node;
1060 struct extent_state *state;
Chris Mason3e9fd942007-11-20 10:47:25 -05001061 u64 cur_start = *start;
Chris Masonb888db22007-08-27 16:49:44 -04001062 u64 found = 0;
1063 u64 total_bytes = 0;
1064
1065 write_lock_irq(&tree->lock);
1066 /*
1067 * this search will find all the extents that end after
1068 * our range starts.
1069 */
1070search_again:
1071 node = tree_search(&tree->state, cur_start);
1072 if (!node || IS_ERR(node)) {
1073 goto out;
1074 }
1075
1076 while(1) {
1077 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason3e9fd942007-11-20 10:47:25 -05001078 if (found && state->start != cur_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001079 goto out;
1080 }
1081 if (!(state->state & EXTENT_DELALLOC)) {
1082 goto out;
1083 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001084 if (!found) {
1085 struct extent_state *prev_state;
1086 struct rb_node *prev_node = node;
1087 while(1) {
1088 prev_node = rb_prev(prev_node);
1089 if (!prev_node)
1090 break;
1091 prev_state = rb_entry(prev_node,
1092 struct extent_state,
1093 rb_node);
1094 if (!(prev_state->state & EXTENT_DELALLOC))
1095 break;
1096 state = prev_state;
1097 node = prev_node;
Chris Masonb888db22007-08-27 16:49:44 -04001098 }
Chris Masonb888db22007-08-27 16:49:44 -04001099 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001100 if (state->state & EXTENT_LOCKED) {
1101 DEFINE_WAIT(wait);
1102 atomic_inc(&state->refs);
1103 prepare_to_wait(&state->wq, &wait,
1104 TASK_UNINTERRUPTIBLE);
1105 write_unlock_irq(&tree->lock);
1106 schedule();
1107 write_lock_irq(&tree->lock);
1108 finish_wait(&state->wq, &wait);
1109 free_extent_state(state);
1110 goto search_again;
1111 }
1112 state->state |= EXTENT_LOCKED;
1113 if (!found)
1114 *start = state->start;
Chris Masonb888db22007-08-27 16:49:44 -04001115 found++;
1116 *end = state->end;
1117 cur_start = state->end + 1;
1118 node = rb_next(node);
1119 if (!node)
1120 break;
Yan944746e2007-11-01 11:28:42 -04001121 total_bytes += state->end - state->start + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001122 if (total_bytes >= max_bytes)
1123 break;
1124 }
1125out:
1126 write_unlock_irq(&tree->lock);
1127 return found;
1128}
1129
Chris Mason793955b2007-11-26 16:34:41 -08001130u64 count_range_bits(struct extent_map_tree *tree,
1131 u64 *start, u64 max_bytes, unsigned long bits)
1132{
1133 struct rb_node *node;
1134 struct extent_state *state;
1135 u64 cur_start = *start;
1136 u64 total_bytes = 0;
1137 int found = 0;
1138
1139 write_lock_irq(&tree->lock);
Chris Masonca664622007-11-27 11:16:35 -05001140 if (bits == EXTENT_DIRTY) {
1141 *start = 0;
1142 total_bytes = tree->dirty_bytes;
1143 goto out;
1144 }
Chris Mason793955b2007-11-26 16:34:41 -08001145 /*
1146 * this search will find all the extents that end after
1147 * our range starts.
1148 */
1149 node = tree_search(&tree->state, cur_start);
1150 if (!node || IS_ERR(node)) {
1151 goto out;
1152 }
1153
1154 while(1) {
1155 state = rb_entry(node, struct extent_state, rb_node);
1156 if ((state->state & bits)) {
1157 total_bytes += state->end - state->start + 1;
1158 if (total_bytes >= max_bytes)
1159 break;
1160 if (!found) {
1161 *start = state->start;
1162 found = 1;
1163 }
1164 }
1165 node = rb_next(node);
1166 if (!node)
1167 break;
1168 }
1169out:
1170 write_unlock_irq(&tree->lock);
1171 return total_bytes;
1172}
1173
Chris Masona52d9a82007-08-27 16:49:44 -04001174/*
1175 * helper function to lock both pages and extents in the tree.
1176 * pages must be locked first.
1177 */
1178int lock_range(struct extent_map_tree *tree, u64 start, u64 end)
1179{
1180 unsigned long index = start >> PAGE_CACHE_SHIFT;
1181 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1182 struct page *page;
1183 int err;
1184
1185 while (index <= end_index) {
1186 page = grab_cache_page(tree->mapping, index);
1187 if (!page) {
1188 err = -ENOMEM;
1189 goto failed;
1190 }
1191 if (IS_ERR(page)) {
1192 err = PTR_ERR(page);
1193 goto failed;
1194 }
1195 index++;
1196 }
1197 lock_extent(tree, start, end, GFP_NOFS);
1198 return 0;
1199
1200failed:
1201 /*
1202 * we failed above in getting the page at 'index', so we undo here
1203 * up to but not including the page at 'index'
1204 */
1205 end_index = index;
1206 index = start >> PAGE_CACHE_SHIFT;
1207 while (index < end_index) {
1208 page = find_get_page(tree->mapping, index);
1209 unlock_page(page);
1210 page_cache_release(page);
1211 index++;
1212 }
1213 return err;
1214}
1215EXPORT_SYMBOL(lock_range);
1216
1217/*
1218 * helper function to unlock both pages and extents in the tree.
1219 */
1220int unlock_range(struct extent_map_tree *tree, u64 start, u64 end)
1221{
1222 unsigned long index = start >> PAGE_CACHE_SHIFT;
1223 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1224 struct page *page;
1225
1226 while (index <= end_index) {
1227 page = find_get_page(tree->mapping, index);
1228 unlock_page(page);
1229 page_cache_release(page);
1230 index++;
1231 }
1232 unlock_extent(tree, start, end, GFP_NOFS);
1233 return 0;
1234}
1235EXPORT_SYMBOL(unlock_range);
1236
Chris Mason07157aa2007-08-30 08:50:51 -04001237int set_state_private(struct extent_map_tree *tree, u64 start, u64 private)
1238{
1239 struct rb_node *node;
1240 struct extent_state *state;
1241 int ret = 0;
1242
1243 write_lock_irq(&tree->lock);
1244 /*
1245 * this search will find all the extents that end after
1246 * our range starts.
1247 */
1248 node = tree_search(&tree->state, start);
1249 if (!node || IS_ERR(node)) {
1250 ret = -ENOENT;
1251 goto out;
1252 }
1253 state = rb_entry(node, struct extent_state, rb_node);
1254 if (state->start != start) {
1255 ret = -ENOENT;
1256 goto out;
1257 }
1258 state->private = private;
1259out:
1260 write_unlock_irq(&tree->lock);
1261 return ret;
Chris Mason07157aa2007-08-30 08:50:51 -04001262}
1263
1264int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private)
1265{
1266 struct rb_node *node;
1267 struct extent_state *state;
1268 int ret = 0;
1269
1270 read_lock_irq(&tree->lock);
1271 /*
1272 * this search will find all the extents that end after
1273 * our range starts.
1274 */
1275 node = tree_search(&tree->state, start);
1276 if (!node || IS_ERR(node)) {
1277 ret = -ENOENT;
1278 goto out;
1279 }
1280 state = rb_entry(node, struct extent_state, rb_node);
1281 if (state->start != start) {
1282 ret = -ENOENT;
1283 goto out;
1284 }
1285 *private = state->private;
1286out:
1287 read_unlock_irq(&tree->lock);
1288 return ret;
1289}
1290
Chris Masona52d9a82007-08-27 16:49:44 -04001291/*
1292 * searches a range in the state tree for a given mask.
1293 * If 'filled' == 1, this returns 1 only if ever extent in the tree
1294 * has the bits set. Otherwise, 1 is returned if any bit in the
1295 * range is found set.
1296 */
Chris Mason1a5bc162007-10-15 16:15:26 -04001297int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
1298 int bits, int filled)
Chris Masona52d9a82007-08-27 16:49:44 -04001299{
1300 struct extent_state *state = NULL;
1301 struct rb_node *node;
1302 int bitset = 0;
1303
1304 read_lock_irq(&tree->lock);
1305 node = tree_search(&tree->state, start);
1306 while (node && start <= end) {
1307 state = rb_entry(node, struct extent_state, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -04001308
1309 if (filled && state->start > start) {
1310 bitset = 0;
1311 break;
1312 }
Chris Mason0591fb52007-11-11 08:22:00 -05001313
1314 if (state->start > end)
1315 break;
1316
Chris Masona52d9a82007-08-27 16:49:44 -04001317 if (state->state & bits) {
1318 bitset = 1;
1319 if (!filled)
1320 break;
1321 } else if (filled) {
1322 bitset = 0;
1323 break;
1324 }
1325 start = state->end + 1;
1326 if (start > end)
1327 break;
1328 node = rb_next(node);
1329 }
1330 read_unlock_irq(&tree->lock);
1331 return bitset;
1332}
Chris Mason1a5bc162007-10-15 16:15:26 -04001333EXPORT_SYMBOL(test_range_bit);
Chris Masona52d9a82007-08-27 16:49:44 -04001334
1335/*
1336 * helper function to set a given page up to date if all the
1337 * extents in the tree for that page are up to date
1338 */
1339static int check_page_uptodate(struct extent_map_tree *tree,
1340 struct page *page)
1341{
Chris Mason35ebb932007-10-30 16:56:53 -04001342 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001343 u64 end = start + PAGE_CACHE_SIZE - 1;
1344 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1345 SetPageUptodate(page);
1346 return 0;
1347}
1348
1349/*
1350 * helper function to unlock a page if all the extents in the tree
1351 * for that page are unlocked
1352 */
1353static int check_page_locked(struct extent_map_tree *tree,
1354 struct page *page)
1355{
Chris Mason35ebb932007-10-30 16:56:53 -04001356 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001357 u64 end = start + PAGE_CACHE_SIZE - 1;
1358 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1359 unlock_page(page);
1360 return 0;
1361}
1362
1363/*
1364 * helper function to end page writeback if all the extents
1365 * in the tree for that page are done with writeback
1366 */
1367static int check_page_writeback(struct extent_map_tree *tree,
1368 struct page *page)
1369{
Chris Mason35ebb932007-10-30 16:56:53 -04001370 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001371 u64 end = start + PAGE_CACHE_SIZE - 1;
1372 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1373 end_page_writeback(page);
1374 return 0;
1375}
1376
1377/* lots and lots of room for performance fixes in the end_bio funcs */
1378
1379/*
1380 * after a writepage IO is done, we need to:
1381 * clear the uptodate bits on error
1382 * clear the writeback bits in the extent tree for this IO
1383 * end_page_writeback if the page has no more pending IO
1384 *
1385 * Scheduling is not allowed, so the extent state tree is expected
1386 * to have one and only one object corresponding to this IO.
1387 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001388#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1389static void end_bio_extent_writepage(struct bio *bio, int err)
1390#else
Chris Masona52d9a82007-08-27 16:49:44 -04001391static int end_bio_extent_writepage(struct bio *bio,
1392 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001393#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001394{
1395 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1396 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1397 struct extent_map_tree *tree = bio->bi_private;
1398 u64 start;
1399 u64 end;
1400 int whole_page;
1401
Jens Axboe0a2118d2007-10-19 09:23:05 -04001402#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001403 if (bio->bi_size)
1404 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001405#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001406
1407 do {
1408 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001409 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1410 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001411 end = start + bvec->bv_len - 1;
1412
1413 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1414 whole_page = 1;
1415 else
1416 whole_page = 0;
1417
1418 if (--bvec >= bio->bi_io_vec)
1419 prefetchw(&bvec->bv_page->flags);
1420
1421 if (!uptodate) {
1422 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1423 ClearPageUptodate(page);
1424 SetPageError(page);
1425 }
1426 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1427
1428 if (whole_page)
1429 end_page_writeback(page);
1430 else
1431 check_page_writeback(tree, page);
Christoph Hellwig0e2752a2007-09-10 20:02:33 -04001432 if (tree->ops && tree->ops->writepage_end_io_hook)
1433 tree->ops->writepage_end_io_hook(page, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001434 } while (bvec >= bio->bi_io_vec);
1435
1436 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001437#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001438 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001439#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001440}
1441
1442/*
1443 * after a readpage IO is done, we need to:
1444 * clear the uptodate bits on error
1445 * set the uptodate bits if things worked
1446 * set the page up to date if all extents in the tree are uptodate
1447 * clear the lock bit in the extent tree
1448 * unlock the page if there are no other extents locked for it
1449 *
1450 * Scheduling is not allowed, so the extent state tree is expected
1451 * to have one and only one object corresponding to this IO.
1452 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001453#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1454static void end_bio_extent_readpage(struct bio *bio, int err)
1455#else
Chris Masona52d9a82007-08-27 16:49:44 -04001456static int end_bio_extent_readpage(struct bio *bio,
1457 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001458#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001459{
Chris Mason07157aa2007-08-30 08:50:51 -04001460 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04001461 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1462 struct extent_map_tree *tree = bio->bi_private;
1463 u64 start;
1464 u64 end;
1465 int whole_page;
Chris Mason07157aa2007-08-30 08:50:51 -04001466 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04001467
Jens Axboe0a2118d2007-10-19 09:23:05 -04001468#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001469 if (bio->bi_size)
1470 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001471#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001472
1473 do {
1474 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001475 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1476 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001477 end = start + bvec->bv_len - 1;
1478
1479 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1480 whole_page = 1;
1481 else
1482 whole_page = 0;
1483
1484 if (--bvec >= bio->bi_io_vec)
1485 prefetchw(&bvec->bv_page->flags);
1486
Chris Mason07157aa2007-08-30 08:50:51 -04001487 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1488 ret = tree->ops->readpage_end_io_hook(page, start, end);
1489 if (ret)
1490 uptodate = 0;
1491 }
Chris Masona52d9a82007-08-27 16:49:44 -04001492 if (uptodate) {
1493 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1494 if (whole_page)
1495 SetPageUptodate(page);
1496 else
1497 check_page_uptodate(tree, page);
1498 } else {
1499 ClearPageUptodate(page);
1500 SetPageError(page);
1501 }
1502
1503 unlock_extent(tree, start, end, GFP_ATOMIC);
1504
1505 if (whole_page)
1506 unlock_page(page);
1507 else
1508 check_page_locked(tree, page);
1509 } while (bvec >= bio->bi_io_vec);
1510
1511 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001512#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001513 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001514#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001515}
1516
1517/*
1518 * IO done from prepare_write is pretty simple, we just unlock
1519 * the structs in the extent tree when done, and set the uptodate bits
1520 * as appropriate.
1521 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001522#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1523static void end_bio_extent_preparewrite(struct bio *bio, int err)
1524#else
Chris Masona52d9a82007-08-27 16:49:44 -04001525static int end_bio_extent_preparewrite(struct bio *bio,
1526 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001527#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001528{
1529 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1530 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1531 struct extent_map_tree *tree = bio->bi_private;
1532 u64 start;
1533 u64 end;
1534
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 if (bio->bi_size)
1537 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001538#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001539
1540 do {
1541 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001542 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1543 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001544 end = start + bvec->bv_len - 1;
1545
1546 if (--bvec >= bio->bi_io_vec)
1547 prefetchw(&bvec->bv_page->flags);
1548
1549 if (uptodate) {
1550 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1551 } else {
1552 ClearPageUptodate(page);
1553 SetPageError(page);
1554 }
1555
1556 unlock_extent(tree, start, end, GFP_ATOMIC);
1557
1558 } while (bvec >= bio->bi_io_vec);
1559
1560 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001561#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001562 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001563#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001564}
1565
Chris Masonb293f022007-11-01 19:45:34 -04001566static struct bio *
1567extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1568 gfp_t gfp_flags)
1569{
1570 struct bio *bio;
1571
1572 bio = bio_alloc(gfp_flags, nr_vecs);
1573
1574 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1575 while (!bio && (nr_vecs /= 2))
1576 bio = bio_alloc(gfp_flags, nr_vecs);
1577 }
1578
1579 if (bio) {
1580 bio->bi_bdev = bdev;
1581 bio->bi_sector = first_sector;
1582 }
1583 return bio;
1584}
1585
1586static int submit_one_bio(int rw, struct bio *bio)
1587{
1588 int ret = 0;
1589 bio_get(bio);
1590 submit_bio(rw, bio);
1591 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1592 ret = -EOPNOTSUPP;
1593 bio_put(bio);
1594 return ret;
1595}
1596
Chris Masona52d9a82007-08-27 16:49:44 -04001597static int submit_extent_page(int rw, struct extent_map_tree *tree,
1598 struct page *page, sector_t sector,
1599 size_t size, unsigned long offset,
1600 struct block_device *bdev,
Chris Masonb293f022007-11-01 19:45:34 -04001601 struct bio **bio_ret,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001602 unsigned long max_pages,
Chris Masona52d9a82007-08-27 16:49:44 -04001603 bio_end_io_t end_io_func)
1604{
Chris Masona52d9a82007-08-27 16:49:44 -04001605 int ret = 0;
Chris Masonb293f022007-11-01 19:45:34 -04001606 struct bio *bio;
1607 int nr;
Chris Masona52d9a82007-08-27 16:49:44 -04001608
Chris Masonb293f022007-11-01 19:45:34 -04001609 if (bio_ret && *bio_ret) {
1610 bio = *bio_ret;
1611 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1612 bio_add_page(bio, page, size, offset) < size) {
1613 ret = submit_one_bio(rw, bio);
1614 bio = NULL;
1615 } else {
1616 return 0;
1617 }
1618 }
Chris Mason3ab2fb52007-11-08 10:59:22 -05001619 nr = min_t(int, max_pages, bio_get_nr_vecs(bdev));
Chris Masonb293f022007-11-01 19:45:34 -04001620 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1621 if (!bio) {
1622 printk("failed to allocate bio nr %d\n", nr);
1623 }
1624 bio_add_page(bio, page, size, offset);
Chris Masona52d9a82007-08-27 16:49:44 -04001625 bio->bi_end_io = end_io_func;
1626 bio->bi_private = tree;
Chris Masonb293f022007-11-01 19:45:34 -04001627 if (bio_ret) {
1628 *bio_ret = bio;
1629 } else {
1630 ret = submit_one_bio(rw, bio);
1631 }
Chris Masona52d9a82007-08-27 16:49:44 -04001632
Chris Masona52d9a82007-08-27 16:49:44 -04001633 return ret;
1634}
1635
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001636void set_page_extent_mapped(struct page *page)
1637{
1638 if (!PagePrivate(page)) {
1639 SetPagePrivate(page);
1640 WARN_ON(!page->mapping->a_ops->invalidatepage);
Chris Mason19c00dd2007-10-15 16:19:22 -04001641 set_page_private(page, EXTENT_PAGE_PRIVATE);
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001642 page_cache_get(page);
1643 }
1644}
1645
Chris Masona52d9a82007-08-27 16:49:44 -04001646/*
1647 * basic readpage implementation. Locked extent state structs are inserted
1648 * into the tree that are removed when the IO is done (by the end_io
1649 * handlers)
1650 */
Chris Mason3ab2fb52007-11-08 10:59:22 -05001651static int __extent_read_full_page(struct extent_map_tree *tree,
1652 struct page *page,
1653 get_extent_t *get_extent,
1654 struct bio **bio)
Chris Masona52d9a82007-08-27 16:49:44 -04001655{
1656 struct inode *inode = page->mapping->host;
Chris Mason35ebb932007-10-30 16:56:53 -04001657 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001658 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1659 u64 end;
1660 u64 cur = start;
1661 u64 extent_offset;
1662 u64 last_byte = i_size_read(inode);
1663 u64 block_start;
1664 u64 cur_end;
1665 sector_t sector;
1666 struct extent_map *em;
1667 struct block_device *bdev;
1668 int ret;
1669 int nr = 0;
1670 size_t page_offset = 0;
1671 size_t iosize;
1672 size_t blocksize = inode->i_sb->s_blocksize;
1673
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001674 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001675
1676 end = page_end;
1677 lock_extent(tree, start, end, GFP_NOFS);
1678
1679 while (cur <= end) {
1680 if (cur >= last_byte) {
1681 iosize = PAGE_CACHE_SIZE - page_offset;
1682 zero_user_page(page, page_offset, iosize, KM_USER0);
1683 set_extent_uptodate(tree, cur, cur + iosize - 1,
1684 GFP_NOFS);
1685 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1686 break;
1687 }
1688 em = get_extent(inode, page, page_offset, cur, end, 0);
1689 if (IS_ERR(em) || !em) {
1690 SetPageError(page);
1691 unlock_extent(tree, cur, end, GFP_NOFS);
1692 break;
1693 }
1694
1695 extent_offset = cur - em->start;
1696 BUG_ON(em->end < cur);
1697 BUG_ON(end < cur);
1698
1699 iosize = min(em->end - cur, end - cur) + 1;
1700 cur_end = min(em->end, end);
1701 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1702 sector = (em->block_start + extent_offset) >> 9;
1703 bdev = em->bdev;
1704 block_start = em->block_start;
1705 free_extent_map(em);
1706 em = NULL;
1707
1708 /* we've found a hole, just zero and go on */
Chris Mason5f39d392007-10-15 16:14:19 -04001709 if (block_start == EXTENT_MAP_HOLE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001710 zero_user_page(page, page_offset, iosize, KM_USER0);
1711 set_extent_uptodate(tree, cur, cur + iosize - 1,
1712 GFP_NOFS);
1713 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1714 cur = cur + iosize;
1715 page_offset += iosize;
1716 continue;
1717 }
1718 /* the get_extent function already copied into the page */
1719 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1720 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1721 cur = cur + iosize;
1722 page_offset += iosize;
1723 continue;
1724 }
1725
Chris Mason07157aa2007-08-30 08:50:51 -04001726 ret = 0;
1727 if (tree->ops && tree->ops->readpage_io_hook) {
1728 ret = tree->ops->readpage_io_hook(page, cur,
1729 cur + iosize - 1);
1730 }
1731 if (!ret) {
Chris Mason3ab2fb52007-11-08 10:59:22 -05001732 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1733 nr -= page->index;
Chris Mason07157aa2007-08-30 08:50:51 -04001734 ret = submit_extent_page(READ, tree, page,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001735 sector, iosize, page_offset,
1736 bdev, bio, nr,
1737 end_bio_extent_readpage);
Chris Mason07157aa2007-08-30 08:50:51 -04001738 }
Chris Masona52d9a82007-08-27 16:49:44 -04001739 if (ret)
1740 SetPageError(page);
1741 cur = cur + iosize;
1742 page_offset += iosize;
1743 nr++;
1744 }
1745 if (!nr) {
1746 if (!PageError(page))
1747 SetPageUptodate(page);
1748 unlock_page(page);
1749 }
1750 return 0;
1751}
Chris Mason3ab2fb52007-11-08 10:59:22 -05001752
1753int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
1754 get_extent_t *get_extent)
1755{
1756 struct bio *bio = NULL;
1757 int ret;
1758
1759 ret = __extent_read_full_page(tree, page, get_extent, &bio);
1760 if (bio)
1761 submit_one_bio(READ, bio);
1762 return ret;
1763}
Chris Masona52d9a82007-08-27 16:49:44 -04001764EXPORT_SYMBOL(extent_read_full_page);
1765
1766/*
1767 * the writepage semantics are similar to regular writepage. extent
1768 * records are inserted to lock ranges in the tree, and as dirty areas
1769 * are found, they are marked writeback. Then the lock bits are removed
1770 * and the end_io handler clears the writeback ranges
1771 */
Chris Masonb293f022007-11-01 19:45:34 -04001772static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1773 void *data)
Chris Masona52d9a82007-08-27 16:49:44 -04001774{
1775 struct inode *inode = page->mapping->host;
Chris Masonb293f022007-11-01 19:45:34 -04001776 struct extent_page_data *epd = data;
1777 struct extent_map_tree *tree = epd->tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001778 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason3e9fd942007-11-20 10:47:25 -05001779 u64 delalloc_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001780 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1781 u64 end;
1782 u64 cur = start;
1783 u64 extent_offset;
1784 u64 last_byte = i_size_read(inode);
1785 u64 block_start;
Chris Mason179e29e2007-11-01 11:28:41 -04001786 u64 iosize;
Chris Masona52d9a82007-08-27 16:49:44 -04001787 sector_t sector;
1788 struct extent_map *em;
1789 struct block_device *bdev;
1790 int ret;
1791 int nr = 0;
1792 size_t page_offset = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04001793 size_t blocksize;
1794 loff_t i_size = i_size_read(inode);
1795 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001796 u64 nr_delalloc;
1797 u64 delalloc_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001798
Chris Masonb888db22007-08-27 16:49:44 -04001799 WARN_ON(!PageLocked(page));
Chris Masona52d9a82007-08-27 16:49:44 -04001800 if (page->index > end_index) {
1801 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1802 unlock_page(page);
1803 return 0;
1804 }
1805
1806 if (page->index == end_index) {
1807 size_t offset = i_size & (PAGE_CACHE_SIZE - 1);
1808 zero_user_page(page, offset,
1809 PAGE_CACHE_SIZE - offset, KM_USER0);
1810 }
1811
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001812 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001813
Chris Mason3e9fd942007-11-20 10:47:25 -05001814 delalloc_start = start;
1815 delalloc_end = 0;
1816 while(delalloc_end < page_end) {
1817 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
1818 &delalloc_end,
1819 128 * 1024 * 1024);
1820 if (nr_delalloc <= 0)
1821 break;
1822 tree->ops->fill_delalloc(inode, delalloc_start,
1823 delalloc_end);
1824 clear_extent_bit(tree, delalloc_start,
1825 delalloc_end,
1826 EXTENT_LOCKED | EXTENT_DELALLOC,
1827 1, 0, GFP_NOFS);
1828 delalloc_start = delalloc_end + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001829 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001830 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Masonb888db22007-08-27 16:49:44 -04001831
1832 end = page_end;
1833 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1834 printk("found delalloc bits after lock_extent\n");
1835 }
Chris Masona52d9a82007-08-27 16:49:44 -04001836
1837 if (last_byte <= start) {
1838 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1839 goto done;
1840 }
1841
1842 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
1843 blocksize = inode->i_sb->s_blocksize;
1844
1845 while (cur <= end) {
1846 if (cur >= last_byte) {
1847 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
1848 break;
1849 }
Chris Masonb293f022007-11-01 19:45:34 -04001850 em = epd->get_extent(inode, page, page_offset, cur, end, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04001851 if (IS_ERR(em) || !em) {
1852 SetPageError(page);
1853 break;
1854 }
1855
1856 extent_offset = cur - em->start;
1857 BUG_ON(em->end < cur);
1858 BUG_ON(end < cur);
1859 iosize = min(em->end - cur, end - cur) + 1;
1860 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1861 sector = (em->block_start + extent_offset) >> 9;
1862 bdev = em->bdev;
1863 block_start = em->block_start;
1864 free_extent_map(em);
1865 em = NULL;
1866
Chris Mason5f39d392007-10-15 16:14:19 -04001867 if (block_start == EXTENT_MAP_HOLE ||
1868 block_start == EXTENT_MAP_INLINE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001869 clear_extent_dirty(tree, cur,
1870 cur + iosize - 1, GFP_NOFS);
1871 cur = cur + iosize;
1872 page_offset += iosize;
1873 continue;
1874 }
1875
1876 /* leave this out until we have a page_mkwrite call */
1877 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
1878 EXTENT_DIRTY, 0)) {
1879 cur = cur + iosize;
1880 page_offset += iosize;
1881 continue;
1882 }
1883 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
Christoph Hellwigb06355f2007-09-10 20:02:32 -04001884 if (tree->ops && tree->ops->writepage_io_hook) {
1885 ret = tree->ops->writepage_io_hook(page, cur,
1886 cur + iosize - 1);
1887 } else {
1888 ret = 0;
1889 }
Chris Masona52d9a82007-08-27 16:49:44 -04001890 if (ret)
1891 SetPageError(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001892 else {
Chris Mason7073c8e2007-11-20 13:44:45 -05001893 unsigned long max_nr = end_index + 1;
Chris Mason07157aa2007-08-30 08:50:51 -04001894 set_range_writeback(tree, cur, cur + iosize - 1);
Chris Mason7073c8e2007-11-20 13:44:45 -05001895 if (!PageWriteback(page)) {
1896 printk("warning page %lu not writeback, "
1897 "cur %llu end %llu\n", page->index,
1898 (unsigned long long)cur,
1899 (unsigned long long)end);
1900 }
Chris Masonb293f022007-11-01 19:45:34 -04001901
Chris Mason07157aa2007-08-30 08:50:51 -04001902 ret = submit_extent_page(WRITE, tree, page, sector,
1903 iosize, page_offset, bdev,
Chris Mason7073c8e2007-11-20 13:44:45 -05001904 &epd->bio, max_nr,
Chris Mason07157aa2007-08-30 08:50:51 -04001905 end_bio_extent_writepage);
1906 if (ret)
1907 SetPageError(page);
1908 }
Chris Masona52d9a82007-08-27 16:49:44 -04001909 cur = cur + iosize;
1910 page_offset += iosize;
1911 nr++;
1912 }
1913done:
Chris Mason7073c8e2007-11-20 13:44:45 -05001914 if (nr == 0) {
1915 /* make sure the mapping tag for page dirty gets cleared */
1916 set_page_writeback(page);
1917 end_page_writeback(page);
1918 }
Chris Masona52d9a82007-08-27 16:49:44 -04001919 unlock_extent(tree, start, page_end, GFP_NOFS);
1920 unlock_page(page);
1921 return 0;
1922}
Chris Masonb293f022007-11-01 19:45:34 -04001923
1924int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
1925 get_extent_t *get_extent,
1926 struct writeback_control *wbc)
1927{
1928 int ret;
Chris Mason015a739c2007-11-26 16:15:16 -08001929 struct address_space *mapping = page->mapping;
Chris Masonb293f022007-11-01 19:45:34 -04001930 struct extent_page_data epd = {
1931 .bio = NULL,
1932 .tree = tree,
1933 .get_extent = get_extent,
1934 };
Chris Mason015a739c2007-11-26 16:15:16 -08001935 struct writeback_control wbc_writepages = {
1936 .bdi = wbc->bdi,
1937 .sync_mode = WB_SYNC_NONE,
1938 .older_than_this = NULL,
1939 .nr_to_write = 64,
1940 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
1941 .range_end = (loff_t)-1,
1942 };
1943
Chris Masonb293f022007-11-01 19:45:34 -04001944
1945 ret = __extent_writepage(page, wbc, &epd);
Chris Mason015a739c2007-11-26 16:15:16 -08001946
1947 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
Chris Masonb293f022007-11-01 19:45:34 -04001948 if (epd.bio)
1949 submit_one_bio(WRITE, epd.bio);
1950 return ret;
1951}
Chris Masona52d9a82007-08-27 16:49:44 -04001952EXPORT_SYMBOL(extent_write_full_page);
1953
Chris Masonb293f022007-11-01 19:45:34 -04001954int extent_writepages(struct extent_map_tree *tree,
1955 struct address_space *mapping,
1956 get_extent_t *get_extent,
1957 struct writeback_control *wbc)
1958{
1959 int ret;
1960 struct extent_page_data epd = {
1961 .bio = NULL,
1962 .tree = tree,
1963 .get_extent = get_extent,
1964 };
1965
1966 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
1967 if (epd.bio)
1968 submit_one_bio(WRITE, epd.bio);
1969 return ret;
1970}
1971EXPORT_SYMBOL(extent_writepages);
1972
Chris Mason3ab2fb52007-11-08 10:59:22 -05001973int extent_readpages(struct extent_map_tree *tree,
1974 struct address_space *mapping,
1975 struct list_head *pages, unsigned nr_pages,
1976 get_extent_t get_extent)
1977{
1978 struct bio *bio = NULL;
1979 unsigned page_idx;
1980 struct pagevec pvec;
1981
1982 pagevec_init(&pvec, 0);
1983 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
1984 struct page *page = list_entry(pages->prev, struct page, lru);
1985
1986 prefetchw(&page->flags);
1987 list_del(&page->lru);
1988 /*
1989 * what we want to do here is call add_to_page_cache_lru,
1990 * but that isn't exported, so we reproduce it here
1991 */
1992 if (!add_to_page_cache(page, mapping,
1993 page->index, GFP_KERNEL)) {
1994
1995 /* open coding of lru_cache_add, also not exported */
1996 page_cache_get(page);
1997 if (!pagevec_add(&pvec, page))
1998 __pagevec_lru_add(&pvec);
1999 __extent_read_full_page(tree, page, get_extent, &bio);
2000 }
2001 page_cache_release(page);
2002 }
2003 if (pagevec_count(&pvec))
2004 __pagevec_lru_add(&pvec);
2005 BUG_ON(!list_empty(pages));
2006 if (bio)
2007 submit_one_bio(READ, bio);
2008 return 0;
2009}
2010EXPORT_SYMBOL(extent_readpages);
2011
Chris Masona52d9a82007-08-27 16:49:44 -04002012/*
2013 * basic invalidatepage code, this waits on any locked or writeback
2014 * ranges corresponding to the page, and then deletes any extent state
2015 * records from the tree
2016 */
2017int extent_invalidatepage(struct extent_map_tree *tree,
2018 struct page *page, unsigned long offset)
2019{
Chris Mason35ebb932007-10-30 16:56:53 -04002020 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Masona52d9a82007-08-27 16:49:44 -04002021 u64 end = start + PAGE_CACHE_SIZE - 1;
2022 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2023
2024 start += (offset + blocksize -1) & ~(blocksize - 1);
2025 if (start > end)
2026 return 0;
2027
2028 lock_extent(tree, start, end, GFP_NOFS);
2029 wait_on_extent_writeback(tree, start, end);
Chris Mason2bf5a722007-08-30 11:54:02 -04002030 clear_extent_bit(tree, start, end,
2031 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Masona52d9a82007-08-27 16:49:44 -04002032 1, 1, GFP_NOFS);
2033 return 0;
2034}
2035EXPORT_SYMBOL(extent_invalidatepage);
2036
2037/*
2038 * simple commit_write call, set_range_dirty is used to mark both
2039 * the pages and the extent records as dirty
2040 */
2041int extent_commit_write(struct extent_map_tree *tree,
2042 struct inode *inode, struct page *page,
2043 unsigned from, unsigned to)
2044{
2045 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2046
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04002047 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04002048 set_page_dirty(page);
2049
2050 if (pos > inode->i_size) {
2051 i_size_write(inode, pos);
2052 mark_inode_dirty(inode);
2053 }
2054 return 0;
2055}
2056EXPORT_SYMBOL(extent_commit_write);
2057
2058int extent_prepare_write(struct extent_map_tree *tree,
2059 struct inode *inode, struct page *page,
2060 unsigned from, unsigned to, get_extent_t *get_extent)
2061{
Chris Mason35ebb932007-10-30 16:56:53 -04002062 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002063 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2064 u64 block_start;
2065 u64 orig_block_start;
2066 u64 block_end;
2067 u64 cur_end;
2068 struct extent_map *em;
2069 unsigned blocksize = 1 << inode->i_blkbits;
2070 size_t page_offset = 0;
2071 size_t block_off_start;
2072 size_t block_off_end;
2073 int err = 0;
2074 int iocount = 0;
2075 int ret = 0;
2076 int isnew;
2077
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04002078 set_page_extent_mapped(page);
2079
Chris Masona52d9a82007-08-27 16:49:44 -04002080 block_start = (page_start + from) & ~((u64)blocksize - 1);
2081 block_end = (page_start + to - 1) | (blocksize - 1);
2082 orig_block_start = block_start;
2083
2084 lock_extent(tree, page_start, page_end, GFP_NOFS);
2085 while(block_start <= block_end) {
2086 em = get_extent(inode, page, page_offset, block_start,
2087 block_end, 1);
2088 if (IS_ERR(em) || !em) {
2089 goto err;
2090 }
2091 cur_end = min(block_end, em->end);
2092 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2093 block_off_end = block_off_start + blocksize;
2094 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2095
2096 if (!PageUptodate(page) && isnew &&
2097 (block_off_end > to || block_off_start < from)) {
2098 void *kaddr;
2099
2100 kaddr = kmap_atomic(page, KM_USER0);
2101 if (block_off_end > to)
2102 memset(kaddr + to, 0, block_off_end - to);
2103 if (block_off_start < from)
2104 memset(kaddr + block_off_start, 0,
2105 from - block_off_start);
2106 flush_dcache_page(page);
2107 kunmap_atomic(kaddr, KM_USER0);
2108 }
2109 if (!isnew && !PageUptodate(page) &&
2110 (block_off_end > to || block_off_start < from) &&
2111 !test_range_bit(tree, block_start, cur_end,
2112 EXTENT_UPTODATE, 1)) {
2113 u64 sector;
2114 u64 extent_offset = block_start - em->start;
2115 size_t iosize;
2116 sector = (em->block_start + extent_offset) >> 9;
2117 iosize = (cur_end - block_start + blocksize - 1) &
2118 ~((u64)blocksize - 1);
2119 /*
2120 * we've already got the extent locked, but we
2121 * need to split the state such that our end_bio
2122 * handler can clear the lock.
2123 */
2124 set_extent_bit(tree, block_start,
2125 block_start + iosize - 1,
2126 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2127 ret = submit_extent_page(READ, tree, page,
2128 sector, iosize, page_offset, em->bdev,
Chris Masonb293f022007-11-01 19:45:34 -04002129 NULL, 1,
Chris Masona52d9a82007-08-27 16:49:44 -04002130 end_bio_extent_preparewrite);
2131 iocount++;
2132 block_start = block_start + iosize;
2133 } else {
2134 set_extent_uptodate(tree, block_start, cur_end,
2135 GFP_NOFS);
2136 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2137 block_start = cur_end + 1;
2138 }
2139 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2140 free_extent_map(em);
2141 }
2142 if (iocount) {
2143 wait_extent_bit(tree, orig_block_start,
2144 block_end, EXTENT_LOCKED);
2145 }
2146 check_page_uptodate(tree, page);
2147err:
2148 /* FIXME, zero out newly allocated blocks on error */
2149 return err;
2150}
2151EXPORT_SYMBOL(extent_prepare_write);
2152
2153/*
2154 * a helper for releasepage. As long as there are no locked extents
2155 * in the range corresponding to the page, both state records and extent
2156 * map records are removed
2157 */
2158int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page)
2159{
2160 struct extent_map *em;
Chris Mason35ebb932007-10-30 16:56:53 -04002161 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002162 u64 end = start + PAGE_CACHE_SIZE - 1;
2163 u64 orig_start = start;
Chris Masonb888db22007-08-27 16:49:44 -04002164 int ret = 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002165
2166 while (start <= end) {
2167 em = lookup_extent_mapping(tree, start, end);
2168 if (!em || IS_ERR(em))
2169 break;
Chris Masonb888db22007-08-27 16:49:44 -04002170 if (!test_range_bit(tree, em->start, em->end,
2171 EXTENT_LOCKED, 0)) {
2172 remove_extent_mapping(tree, em);
2173 /* once for the rb tree */
Chris Masona52d9a82007-08-27 16:49:44 -04002174 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04002175 }
Chris Masona52d9a82007-08-27 16:49:44 -04002176 start = em->end + 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002177 /* once for us */
2178 free_extent_map(em);
2179 }
Chris Masonb888db22007-08-27 16:49:44 -04002180 if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0))
2181 ret = 0;
2182 else
2183 clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
2184 1, 1, GFP_NOFS);
2185 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002186}
2187EXPORT_SYMBOL(try_release_extent_mapping);
2188
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002189sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2190 get_extent_t *get_extent)
2191{
2192 struct inode *inode = mapping->host;
2193 u64 start = iblock << inode->i_blkbits;
2194 u64 end = start + (1 << inode->i_blkbits) - 1;
Yanc67cda12007-10-29 11:41:05 -04002195 sector_t sector = 0;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002196 struct extent_map *em;
2197
2198 em = get_extent(inode, NULL, 0, start, end, 0);
2199 if (!em || IS_ERR(em))
2200 return 0;
2201
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002202 if (em->block_start == EXTENT_MAP_INLINE ||
Chris Mason5f39d392007-10-15 16:14:19 -04002203 em->block_start == EXTENT_MAP_HOLE)
Yanc67cda12007-10-29 11:41:05 -04002204 goto out;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002205
Yanc67cda12007-10-29 11:41:05 -04002206 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2207out:
2208 free_extent_map(em);
2209 return sector;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002210}
Chris Mason5f39d392007-10-15 16:14:19 -04002211
Chris Mason4dc119042007-10-15 16:18:14 -04002212static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002213{
Chris Mason4dc119042007-10-15 16:18:14 -04002214 if (list_empty(&eb->lru)) {
2215 extent_buffer_get(eb);
2216 list_add(&eb->lru, &tree->buffer_lru);
2217 tree->lru_size++;
2218 if (tree->lru_size >= BUFFER_LRU_MAX) {
2219 struct extent_buffer *rm;
2220 rm = list_entry(tree->buffer_lru.prev,
2221 struct extent_buffer, lru);
2222 tree->lru_size--;
Chris Mason856bf3e2007-11-08 10:59:05 -05002223 list_del_init(&rm->lru);
Chris Mason4dc119042007-10-15 16:18:14 -04002224 free_extent_buffer(rm);
2225 }
2226 } else
2227 list_move(&eb->lru, &tree->buffer_lru);
2228 return 0;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002229}
Chris Mason4dc119042007-10-15 16:18:14 -04002230static struct extent_buffer *find_lru(struct extent_map_tree *tree,
2231 u64 start, unsigned long len)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002232{
Chris Mason4dc119042007-10-15 16:18:14 -04002233 struct list_head *lru = &tree->buffer_lru;
2234 struct list_head *cur = lru->next;
2235 struct extent_buffer *eb;
Chris Masonf510cfe2007-10-15 16:14:48 -04002236
Chris Mason4dc119042007-10-15 16:18:14 -04002237 if (list_empty(lru))
2238 return NULL;
Chris Masonf510cfe2007-10-15 16:14:48 -04002239
Chris Mason4dc119042007-10-15 16:18:14 -04002240 do {
2241 eb = list_entry(cur, struct extent_buffer, lru);
2242 if (eb->start == start && eb->len == len) {
2243 extent_buffer_get(eb);
2244 return eb;
2245 }
2246 cur = cur->next;
2247 } while (cur != lru);
2248 return NULL;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002249}
2250
Chris Masondb945352007-10-15 16:15:53 -04002251static inline unsigned long num_extent_pages(u64 start, u64 len)
2252{
2253 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2254 (start >> PAGE_CACHE_SHIFT);
2255}
Chris Mason4dc119042007-10-15 16:18:14 -04002256
2257static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2258 unsigned long i)
2259{
2260 struct page *p;
Chris Mason3685f792007-10-19 09:23:27 -04002261 struct address_space *mapping;
Chris Mason4dc119042007-10-15 16:18:14 -04002262
2263 if (i == 0)
Chris Mason810191f2007-10-15 16:18:55 -04002264 return eb->first_page;
Chris Mason4dc119042007-10-15 16:18:14 -04002265 i += eb->start >> PAGE_CACHE_SHIFT;
Chris Mason3685f792007-10-19 09:23:27 -04002266 mapping = eb->first_page->mapping;
2267 read_lock_irq(&mapping->tree_lock);
2268 p = radix_tree_lookup(&mapping->page_tree, i);
2269 read_unlock_irq(&mapping->tree_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002270 return p;
2271}
2272
2273static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
2274 u64 start,
2275 unsigned long len,
2276 gfp_t mask)
2277{
2278 struct extent_buffer *eb = NULL;
2279
2280 spin_lock(&tree->lru_lock);
2281 eb = find_lru(tree, start, len);
Chris Mason4dc119042007-10-15 16:18:14 -04002282 spin_unlock(&tree->lru_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002283 if (eb) {
Chris Mason09be2072007-11-07 21:08:16 -05002284 return eb;
Chris Mason4dc119042007-10-15 16:18:14 -04002285 }
Chris Mason09be2072007-11-07 21:08:16 -05002286
2287 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Mason4dc119042007-10-15 16:18:14 -04002288 INIT_LIST_HEAD(&eb->lru);
2289 eb->start = start;
2290 eb->len = len;
2291 atomic_set(&eb->refs, 1);
2292
Chris Mason4dc119042007-10-15 16:18:14 -04002293 return eb;
2294}
2295
2296static void __free_extent_buffer(struct extent_buffer *eb)
2297{
2298 kmem_cache_free(extent_buffer_cache, eb);
2299}
2300
Chris Mason5f39d392007-10-15 16:14:19 -04002301struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
2302 u64 start, unsigned long len,
Chris Mason19c00dd2007-10-15 16:19:22 -04002303 struct page *page0,
Chris Mason5f39d392007-10-15 16:14:19 -04002304 gfp_t mask)
2305{
Chris Masondb945352007-10-15 16:15:53 -04002306 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002307 unsigned long i;
2308 unsigned long index = start >> PAGE_CACHE_SHIFT;
2309 struct extent_buffer *eb;
2310 struct page *p;
2311 struct address_space *mapping = tree->mapping;
Yan65555a02007-10-25 15:42:57 -04002312 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002313
Chris Mason4dc119042007-10-15 16:18:14 -04002314 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002315 if (!eb || IS_ERR(eb))
2316 return NULL;
2317
Chris Mason4dc119042007-10-15 16:18:14 -04002318 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002319 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002320
Chris Mason19c00dd2007-10-15 16:19:22 -04002321 if (page0) {
2322 eb->first_page = page0;
2323 i = 1;
2324 index++;
2325 page_cache_get(page0);
Chris Masonff79f812007-10-15 16:22:25 -04002326 mark_page_accessed(page0);
Chris Mason19c00dd2007-10-15 16:19:22 -04002327 set_page_extent_mapped(page0);
Chris Mason0591fb52007-11-11 08:22:00 -05002328 WARN_ON(!PageUptodate(page0));
Chris Mason19c00dd2007-10-15 16:19:22 -04002329 set_page_private(page0, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2330 len << 2);
2331 } else {
2332 i = 0;
2333 }
2334 for (; i < num_pages; i++, index++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002335 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002336 if (!p) {
Chris Masondb945352007-10-15 16:15:53 -04002337 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002338 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002339 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002340 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002341 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002342 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002343 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002344 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2345 len << 2);
2346 } else {
2347 set_page_private(p, EXTENT_PAGE_PRIVATE);
2348 }
Chris Mason5f39d392007-10-15 16:14:19 -04002349 if (!PageUptodate(p))
2350 uptodate = 0;
2351 unlock_page(p);
2352 }
2353 if (uptodate)
2354 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002355 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002356
2357lru_add:
2358 spin_lock(&tree->lru_lock);
2359 add_lru(tree, eb);
2360 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002361 return eb;
Chris Mason09be2072007-11-07 21:08:16 -05002362
Chris Mason5f39d392007-10-15 16:14:19 -04002363fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002364 spin_lock(&tree->lru_lock);
2365 list_del_init(&eb->lru);
2366 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002367 if (!atomic_dec_and_test(&eb->refs))
2368 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002369 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002370 page_cache_release(extent_buffer_page(eb, index));
2371 }
Chris Mason0591fb52007-11-11 08:22:00 -05002372 if (i > 0)
2373 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002374 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002375 return NULL;
2376}
2377EXPORT_SYMBOL(alloc_extent_buffer);
2378
2379struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
2380 u64 start, unsigned long len,
2381 gfp_t mask)
2382{
Chris Masondb945352007-10-15 16:15:53 -04002383 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason09be2072007-11-07 21:08:16 -05002384 unsigned long i;
2385 unsigned long index = start >> PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002386 struct extent_buffer *eb;
2387 struct page *p;
2388 struct address_space *mapping = tree->mapping;
Chris Mason14048ed2007-10-15 16:16:28 -04002389 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002390
Chris Mason4dc119042007-10-15 16:18:14 -04002391 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002392 if (!eb || IS_ERR(eb))
2393 return NULL;
2394
Chris Mason4dc119042007-10-15 16:18:14 -04002395 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002396 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002397
2398 for (i = 0; i < num_pages; i++, index++) {
Chris Mason14048ed2007-10-15 16:16:28 -04002399 p = find_lock_page(mapping, index);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002400 if (!p) {
Chris Mason5f39d392007-10-15 16:14:19 -04002401 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002402 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002403 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002404 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002405
2406 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002407 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002408 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2409 len << 2);
2410 } else {
2411 set_page_private(p, EXTENT_PAGE_PRIVATE);
2412 }
2413
Chris Mason14048ed2007-10-15 16:16:28 -04002414 if (!PageUptodate(p))
2415 uptodate = 0;
2416 unlock_page(p);
Chris Mason5f39d392007-10-15 16:14:19 -04002417 }
Chris Mason14048ed2007-10-15 16:16:28 -04002418 if (uptodate)
2419 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002420 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002421
2422lru_add:
2423 spin_lock(&tree->lru_lock);
2424 add_lru(tree, eb);
2425 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002426 return eb;
2427fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002428 spin_lock(&tree->lru_lock);
2429 list_del_init(&eb->lru);
2430 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002431 if (!atomic_dec_and_test(&eb->refs))
2432 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002433 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002434 page_cache_release(extent_buffer_page(eb, index));
2435 }
Chris Mason0591fb52007-11-11 08:22:00 -05002436 if (i > 0)
2437 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002438 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002439 return NULL;
2440}
2441EXPORT_SYMBOL(find_extent_buffer);
2442
2443void free_extent_buffer(struct extent_buffer *eb)
2444{
2445 unsigned long i;
2446 unsigned long num_pages;
2447
2448 if (!eb)
2449 return;
2450
2451 if (!atomic_dec_and_test(&eb->refs))
2452 return;
2453
Chris Mason0591fb52007-11-11 08:22:00 -05002454 WARN_ON(!list_empty(&eb->lru));
Chris Masondb945352007-10-15 16:15:53 -04002455 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002456
Chris Mason0591fb52007-11-11 08:22:00 -05002457 for (i = 1; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002458 page_cache_release(extent_buffer_page(eb, i));
Chris Mason5f39d392007-10-15 16:14:19 -04002459 }
Chris Mason0591fb52007-11-11 08:22:00 -05002460 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason6d36dcd2007-10-15 16:14:37 -04002461 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002462}
2463EXPORT_SYMBOL(free_extent_buffer);
2464
2465int clear_extent_buffer_dirty(struct extent_map_tree *tree,
2466 struct extent_buffer *eb)
2467{
2468 int set;
2469 unsigned long i;
2470 unsigned long num_pages;
2471 struct page *page;
2472
2473 u64 start = eb->start;
2474 u64 end = start + eb->len - 1;
2475
2476 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
Chris Masondb945352007-10-15 16:15:53 -04002477 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002478
2479 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002480 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002481 lock_page(page);
2482 /*
2483 * if we're on the last page or the first page and the
2484 * block isn't aligned on a page boundary, do extra checks
2485 * to make sure we don't clean page that is partially dirty
2486 */
2487 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2488 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002489 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason35ebb932007-10-30 16:56:53 -04002490 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002491 end = start + PAGE_CACHE_SIZE - 1;
2492 if (test_range_bit(tree, start, end,
2493 EXTENT_DIRTY, 0)) {
2494 unlock_page(page);
2495 continue;
2496 }
2497 }
2498 clear_page_dirty_for_io(page);
Chris Mason7073c8e2007-11-20 13:44:45 -05002499 write_lock_irq(&page->mapping->tree_lock);
2500 if (!PageDirty(page)) {
2501 radix_tree_tag_clear(&page->mapping->page_tree,
2502 page_index(page),
2503 PAGECACHE_TAG_DIRTY);
2504 }
2505 write_unlock_irq(&page->mapping->tree_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002506 unlock_page(page);
2507 }
2508 return 0;
2509}
2510EXPORT_SYMBOL(clear_extent_buffer_dirty);
2511
2512int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
2513 struct extent_buffer *eb)
2514{
2515 return wait_on_extent_writeback(tree, eb->start,
2516 eb->start + eb->len - 1);
2517}
2518EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2519
2520int set_extent_buffer_dirty(struct extent_map_tree *tree,
2521 struct extent_buffer *eb)
2522{
Chris Mason810191f2007-10-15 16:18:55 -04002523 unsigned long i;
2524 unsigned long num_pages;
2525
2526 num_pages = num_extent_pages(eb->start, eb->len);
2527 for (i = 0; i < num_pages; i++) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002528 struct page *page = extent_buffer_page(eb, i);
2529 /* writepage may need to do something special for the
2530 * first page, we have to make sure page->private is
2531 * properly set. releasepage may drop page->private
2532 * on us if the page isn't already dirty.
2533 */
2534 if (i == 0) {
2535 lock_page(page);
2536 set_page_private(page,
2537 EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2538 eb->len << 2);
2539 }
Chris Mason810191f2007-10-15 16:18:55 -04002540 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Mason19c00dd2007-10-15 16:19:22 -04002541 if (i == 0)
2542 unlock_page(page);
Chris Mason810191f2007-10-15 16:18:55 -04002543 }
2544 return set_extent_dirty(tree, eb->start,
2545 eb->start + eb->len - 1, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002546}
2547EXPORT_SYMBOL(set_extent_buffer_dirty);
2548
2549int set_extent_buffer_uptodate(struct extent_map_tree *tree,
2550 struct extent_buffer *eb)
2551{
2552 unsigned long i;
2553 struct page *page;
2554 unsigned long num_pages;
2555
Chris Masondb945352007-10-15 16:15:53 -04002556 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002557
2558 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2559 GFP_NOFS);
2560 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002561 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002562 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2563 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002564 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason5f39d392007-10-15 16:14:19 -04002565 check_page_uptodate(tree, page);
2566 continue;
2567 }
2568 SetPageUptodate(page);
2569 }
2570 return 0;
2571}
2572EXPORT_SYMBOL(set_extent_buffer_uptodate);
2573
2574int extent_buffer_uptodate(struct extent_map_tree *tree,
2575 struct extent_buffer *eb)
2576{
2577 if (eb->flags & EXTENT_UPTODATE)
2578 return 1;
2579 return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
2580 EXTENT_UPTODATE, 1);
2581}
2582EXPORT_SYMBOL(extent_buffer_uptodate);
2583
2584int read_extent_buffer_pages(struct extent_map_tree *tree,
Chris Mason19c00dd2007-10-15 16:19:22 -04002585 struct extent_buffer *eb,
2586 u64 start,
2587 int wait)
Chris Mason5f39d392007-10-15 16:14:19 -04002588{
2589 unsigned long i;
Chris Mason19c00dd2007-10-15 16:19:22 -04002590 unsigned long start_i;
Chris Mason5f39d392007-10-15 16:14:19 -04002591 struct page *page;
2592 int err;
2593 int ret = 0;
2594 unsigned long num_pages;
2595
2596 if (eb->flags & EXTENT_UPTODATE)
2597 return 0;
2598
Chris Mason14048ed2007-10-15 16:16:28 -04002599 if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason5f39d392007-10-15 16:14:19 -04002600 EXTENT_UPTODATE, 1)) {
2601 return 0;
2602 }
Chris Mason0591fb52007-11-11 08:22:00 -05002603
Chris Mason19c00dd2007-10-15 16:19:22 -04002604 if (start) {
2605 WARN_ON(start < eb->start);
2606 start_i = (start >> PAGE_CACHE_SHIFT) -
2607 (eb->start >> PAGE_CACHE_SHIFT);
2608 } else {
2609 start_i = 0;
2610 }
Chris Mason5f39d392007-10-15 16:14:19 -04002611
Chris Masondb945352007-10-15 16:15:53 -04002612 num_pages = num_extent_pages(eb->start, eb->len);
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 if (PageUptodate(page)) {
2616 continue;
2617 }
2618 if (!wait) {
2619 if (TestSetPageLocked(page)) {
2620 continue;
2621 }
2622 } else {
2623 lock_page(page);
2624 }
2625 if (!PageUptodate(page)) {
2626 err = page->mapping->a_ops->readpage(NULL, page);
2627 if (err) {
2628 ret = err;
2629 }
2630 } else {
2631 unlock_page(page);
2632 }
2633 }
2634
2635 if (ret || !wait) {
2636 return ret;
2637 }
2638
Chris Mason19c00dd2007-10-15 16:19:22 -04002639 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002640 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002641 wait_on_page_locked(page);
2642 if (!PageUptodate(page)) {
2643 ret = -EIO;
2644 }
2645 }
Chris Mason4dc119042007-10-15 16:18:14 -04002646 if (!ret)
2647 eb->flags |= EXTENT_UPTODATE;
Chris Mason5f39d392007-10-15 16:14:19 -04002648 return ret;
2649}
2650EXPORT_SYMBOL(read_extent_buffer_pages);
2651
2652void read_extent_buffer(struct extent_buffer *eb, void *dstv,
2653 unsigned long start,
2654 unsigned long len)
2655{
2656 size_t cur;
2657 size_t offset;
2658 struct page *page;
2659 char *kaddr;
2660 char *dst = (char *)dstv;
2661 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2662 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Mason14048ed2007-10-15 16:16:28 -04002663 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002664
2665 WARN_ON(start > eb->len);
2666 WARN_ON(start + len > eb->start + eb->len);
2667
Chris Mason3685f792007-10-19 09:23:27 -04002668 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002669
2670 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002671 page = extent_buffer_page(eb, i);
Chris Mason14048ed2007-10-15 16:16:28 -04002672 if (!PageUptodate(page)) {
2673 printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len);
2674 WARN_ON(1);
2675 }
Chris Mason5f39d392007-10-15 16:14:19 -04002676 WARN_ON(!PageUptodate(page));
2677
2678 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Mason59d169e2007-10-19 09:23:09 -04002679 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002680 memcpy(dst, kaddr + offset, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002681 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002682
2683 dst += cur;
2684 len -= cur;
2685 offset = 0;
2686 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002687 }
2688}
2689EXPORT_SYMBOL(read_extent_buffer);
2690
Chris Mason19c00dd2007-10-15 16:19:22 -04002691int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masondb945352007-10-15 16:15:53 -04002692 unsigned long min_len, char **token, char **map,
2693 unsigned long *map_start,
2694 unsigned long *map_len, int km)
Chris Mason5f39d392007-10-15 16:14:19 -04002695{
Chris Mason479965d2007-10-15 16:14:27 -04002696 size_t offset = start & (PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002697 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002698 struct page *p;
Chris Mason5f39d392007-10-15 16:14:19 -04002699 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2700 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Yan65555a02007-10-25 15:42:57 -04002701 unsigned long end_i = (start_offset + start + min_len - 1) >>
Chris Mason810191f2007-10-15 16:18:55 -04002702 PAGE_CACHE_SHIFT;
Chris Mason479965d2007-10-15 16:14:27 -04002703
2704 if (i != end_i)
2705 return -EINVAL;
Chris Mason5f39d392007-10-15 16:14:19 -04002706
Chris Mason5f39d392007-10-15 16:14:19 -04002707 if (i == 0) {
2708 offset = start_offset;
2709 *map_start = 0;
2710 } else {
Chris Masondb945352007-10-15 16:15:53 -04002711 offset = 0;
Chris Mason0591fb52007-11-11 08:22:00 -05002712 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
Chris Mason5f39d392007-10-15 16:14:19 -04002713 }
Yan65555a02007-10-25 15:42:57 -04002714 if (start + min_len > eb->len) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002715printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
2716 WARN_ON(1);
2717 }
Chris Mason5f39d392007-10-15 16:14:19 -04002718
Chris Masondb945352007-10-15 16:15:53 -04002719 p = extent_buffer_page(eb, i);
2720 WARN_ON(!PageUptodate(p));
2721 kaddr = kmap_atomic(p, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002722 *token = kaddr;
2723 *map = kaddr + offset;
2724 *map_len = PAGE_CACHE_SIZE - offset;
2725 return 0;
2726}
Chris Mason19c00dd2007-10-15 16:19:22 -04002727EXPORT_SYMBOL(map_private_extent_buffer);
Chris Masondb945352007-10-15 16:15:53 -04002728
2729int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
2730 unsigned long min_len,
2731 char **token, char **map,
2732 unsigned long *map_start,
2733 unsigned long *map_len, int km)
2734{
2735 int err;
2736 int save = 0;
2737 if (eb->map_token) {
Chris Masondb945352007-10-15 16:15:53 -04002738 unmap_extent_buffer(eb, eb->map_token, km);
2739 eb->map_token = NULL;
2740 save = 1;
2741 }
Chris Mason19c00dd2007-10-15 16:19:22 -04002742 err = map_private_extent_buffer(eb, start, min_len, token, map,
2743 map_start, map_len, km);
Chris Masondb945352007-10-15 16:15:53 -04002744 if (!err && save) {
2745 eb->map_token = *token;
2746 eb->kaddr = *map;
2747 eb->map_start = *map_start;
2748 eb->map_len = *map_len;
2749 }
2750 return err;
2751}
Chris Mason5f39d392007-10-15 16:14:19 -04002752EXPORT_SYMBOL(map_extent_buffer);
2753
2754void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
2755{
Chris Masonae5252b2007-10-15 16:14:41 -04002756 kunmap_atomic(token, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002757}
2758EXPORT_SYMBOL(unmap_extent_buffer);
2759
2760int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
2761 unsigned long start,
2762 unsigned long len)
2763{
2764 size_t cur;
2765 size_t offset;
2766 struct page *page;
2767 char *kaddr;
2768 char *ptr = (char *)ptrv;
2769 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2770 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2771 int ret = 0;
2772
2773 WARN_ON(start > eb->len);
2774 WARN_ON(start + len > eb->start + eb->len);
2775
Chris Mason3685f792007-10-19 09:23:27 -04002776 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002777
2778 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002779 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002780 WARN_ON(!PageUptodate(page));
2781
2782 cur = min(len, (PAGE_CACHE_SIZE - offset));
2783
Chris Masonae5252b2007-10-15 16:14:41 -04002784 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002785 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002786 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002787 if (ret)
2788 break;
2789
2790 ptr += cur;
2791 len -= cur;
2792 offset = 0;
2793 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002794 }
2795 return ret;
2796}
2797EXPORT_SYMBOL(memcmp_extent_buffer);
2798
2799void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
2800 unsigned long start, unsigned long len)
2801{
2802 size_t cur;
2803 size_t offset;
2804 struct page *page;
2805 char *kaddr;
2806 char *src = (char *)srcv;
2807 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2808 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2809
2810 WARN_ON(start > eb->len);
2811 WARN_ON(start + len > eb->start + eb->len);
2812
Chris Mason3685f792007-10-19 09:23:27 -04002813 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002814
2815 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002816 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002817 WARN_ON(!PageUptodate(page));
2818
2819 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Mason59d169e2007-10-19 09:23:09 -04002820 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002821 memcpy(kaddr + offset, src, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002822 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002823
2824 src += cur;
2825 len -= cur;
2826 offset = 0;
2827 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002828 }
2829}
2830EXPORT_SYMBOL(write_extent_buffer);
2831
2832void memset_extent_buffer(struct extent_buffer *eb, char c,
2833 unsigned long start, unsigned long len)
2834{
2835 size_t cur;
2836 size_t offset;
2837 struct page *page;
2838 char *kaddr;
2839 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2840 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2841
2842 WARN_ON(start > eb->len);
2843 WARN_ON(start + len > eb->start + eb->len);
2844
Chris Mason3685f792007-10-19 09:23:27 -04002845 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002846
2847 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002848 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002849 WARN_ON(!PageUptodate(page));
2850
2851 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masonae5252b2007-10-15 16:14:41 -04002852 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002853 memset(kaddr + offset, c, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002854 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002855
2856 len -= cur;
2857 offset = 0;
2858 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002859 }
2860}
2861EXPORT_SYMBOL(memset_extent_buffer);
2862
2863void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
2864 unsigned long dst_offset, unsigned long src_offset,
2865 unsigned long len)
2866{
2867 u64 dst_len = dst->len;
2868 size_t cur;
2869 size_t offset;
2870 struct page *page;
2871 char *kaddr;
2872 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2873 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2874
2875 WARN_ON(src->len != dst_len);
2876
Chris Mason3685f792007-10-19 09:23:27 -04002877 offset = (start_offset + dst_offset) &
2878 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002879
2880 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002881 page = extent_buffer_page(dst, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002882 WARN_ON(!PageUptodate(page));
2883
2884 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
2885
Chris Masonff190c02007-10-19 10:39:41 -04002886 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002887 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masonff190c02007-10-19 10:39:41 -04002888 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002889
2890 src_offset += cur;
2891 len -= cur;
2892 offset = 0;
2893 i++;
2894 }
2895}
2896EXPORT_SYMBOL(copy_extent_buffer);
2897
2898static void move_pages(struct page *dst_page, struct page *src_page,
2899 unsigned long dst_off, unsigned long src_off,
2900 unsigned long len)
2901{
Chris Masonae5252b2007-10-15 16:14:41 -04002902 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002903 if (dst_page == src_page) {
2904 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
2905 } else {
Chris Masonae5252b2007-10-15 16:14:41 -04002906 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002907 char *p = dst_kaddr + dst_off + len;
2908 char *s = src_kaddr + src_off + len;
2909
2910 while (len--)
2911 *--p = *--s;
2912
Chris Masonae5252b2007-10-15 16:14:41 -04002913 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002914 }
Chris Masonae5252b2007-10-15 16:14:41 -04002915 kunmap_atomic(dst_kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002916}
2917
2918static void copy_pages(struct page *dst_page, struct page *src_page,
2919 unsigned long dst_off, unsigned long src_off,
2920 unsigned long len)
2921{
Chris Masonae5252b2007-10-15 16:14:41 -04002922 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002923 char *src_kaddr;
2924
2925 if (dst_page != src_page)
Chris Masonae5252b2007-10-15 16:14:41 -04002926 src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002927 else
2928 src_kaddr = dst_kaddr;
2929
2930 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002931 kunmap_atomic(dst_kaddr, KM_USER0);
2932 if (dst_page != src_page)
2933 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002934}
2935
2936void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2937 unsigned long src_offset, unsigned long len)
2938{
2939 size_t cur;
2940 size_t dst_off_in_page;
2941 size_t src_off_in_page;
2942 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2943 unsigned long dst_i;
2944 unsigned long src_i;
2945
2946 if (src_offset + len > dst->len) {
2947 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2948 src_offset, len, dst->len);
2949 BUG_ON(1);
2950 }
2951 if (dst_offset + len > dst->len) {
2952 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
2953 dst_offset, len, dst->len);
2954 BUG_ON(1);
2955 }
2956
2957 while(len > 0) {
Chris Mason3685f792007-10-19 09:23:27 -04002958 dst_off_in_page = (start_offset + dst_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002959 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04002960 src_off_in_page = (start_offset + src_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04002961 ((unsigned long)PAGE_CACHE_SIZE - 1);
2962
2963 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
2964 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
2965
Chris Mason5f39d392007-10-15 16:14:19 -04002966 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
2967 src_off_in_page));
Jens Axboeae2f5412007-10-19 09:22:59 -04002968 cur = min_t(unsigned long, cur,
2969 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
Chris Mason5f39d392007-10-15 16:14:19 -04002970
Chris Mason6d36dcd2007-10-15 16:14:37 -04002971 copy_pages(extent_buffer_page(dst, dst_i),
2972 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04002973 dst_off_in_page, src_off_in_page, cur);
2974
2975 src_offset += cur;
2976 dst_offset += cur;
2977 len -= cur;
2978 }
2979}
2980EXPORT_SYMBOL(memcpy_extent_buffer);
2981
2982void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
2983 unsigned long src_offset, unsigned long len)
2984{
2985 size_t cur;
2986 size_t dst_off_in_page;
2987 size_t src_off_in_page;
2988 unsigned long dst_end = dst_offset + len - 1;
2989 unsigned long src_end = src_offset + len - 1;
2990 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
2991 unsigned long dst_i;
2992 unsigned long src_i;
2993
2994 if (src_offset + len > dst->len) {
2995 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
2996 src_offset, len, dst->len);
2997 BUG_ON(1);
2998 }
2999 if (dst_offset + len > dst->len) {
3000 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3001 dst_offset, len, dst->len);
3002 BUG_ON(1);
3003 }
3004 if (dst_offset < src_offset) {
3005 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3006 return;
3007 }
3008 while(len > 0) {
3009 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3010 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3011
Chris Mason3685f792007-10-19 09:23:27 -04003012 dst_off_in_page = (start_offset + dst_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04003013 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04003014 src_off_in_page = (start_offset + src_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04003015 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003016
Jens Axboeae2f5412007-10-19 09:22:59 -04003017 cur = min_t(unsigned long, len, src_off_in_page + 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003018 cur = min(cur, dst_off_in_page + 1);
Chris Mason6d36dcd2007-10-15 16:14:37 -04003019 move_pages(extent_buffer_page(dst, dst_i),
3020 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04003021 dst_off_in_page - cur + 1,
3022 src_off_in_page - cur + 1, cur);
3023
Chris Masondb945352007-10-15 16:15:53 -04003024 dst_end -= cur;
3025 src_end -= cur;
Chris Mason5f39d392007-10-15 16:14:19 -04003026 len -= cur;
3027 }
3028}
3029EXPORT_SYMBOL(memmove_extent_buffer);