blob: a0dff34dd437a43f3d8434d9fa4a42685f4c1e8a [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
Christian Hesse17636e02007-12-11 09:25:06 -050072void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040073{
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)) {
Chris Mason190662b2007-12-18 16:25:45 -05001073 *end = (u64)-1;
Chris Masonb888db22007-08-27 16:49:44 -04001074 goto out;
1075 }
1076
1077 while(1) {
1078 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason3e9fd942007-11-20 10:47:25 -05001079 if (found && state->start != cur_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001080 goto out;
1081 }
1082 if (!(state->state & EXTENT_DELALLOC)) {
Chris Mason190662b2007-12-18 16:25:45 -05001083 if (!found)
1084 *end = state->end;
Chris Masonb888db22007-08-27 16:49:44 -04001085 goto out;
1086 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001087 if (!found) {
1088 struct extent_state *prev_state;
1089 struct rb_node *prev_node = node;
1090 while(1) {
1091 prev_node = rb_prev(prev_node);
1092 if (!prev_node)
1093 break;
1094 prev_state = rb_entry(prev_node,
1095 struct extent_state,
1096 rb_node);
1097 if (!(prev_state->state & EXTENT_DELALLOC))
1098 break;
1099 state = prev_state;
1100 node = prev_node;
Chris Masonb888db22007-08-27 16:49:44 -04001101 }
Chris Masonb888db22007-08-27 16:49:44 -04001102 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001103 if (state->state & EXTENT_LOCKED) {
1104 DEFINE_WAIT(wait);
1105 atomic_inc(&state->refs);
1106 prepare_to_wait(&state->wq, &wait,
1107 TASK_UNINTERRUPTIBLE);
1108 write_unlock_irq(&tree->lock);
1109 schedule();
1110 write_lock_irq(&tree->lock);
1111 finish_wait(&state->wq, &wait);
1112 free_extent_state(state);
1113 goto search_again;
1114 }
1115 state->state |= EXTENT_LOCKED;
1116 if (!found)
1117 *start = state->start;
Chris Masonb888db22007-08-27 16:49:44 -04001118 found++;
1119 *end = state->end;
1120 cur_start = state->end + 1;
1121 node = rb_next(node);
1122 if (!node)
1123 break;
Yan944746e2007-11-01 11:28:42 -04001124 total_bytes += state->end - state->start + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001125 if (total_bytes >= max_bytes)
1126 break;
1127 }
1128out:
1129 write_unlock_irq(&tree->lock);
1130 return found;
1131}
1132
Chris Mason793955b2007-11-26 16:34:41 -08001133u64 count_range_bits(struct extent_map_tree *tree,
1134 u64 *start, u64 max_bytes, unsigned long bits)
1135{
1136 struct rb_node *node;
1137 struct extent_state *state;
1138 u64 cur_start = *start;
1139 u64 total_bytes = 0;
1140 int found = 0;
1141
1142 write_lock_irq(&tree->lock);
Chris Masonca664622007-11-27 11:16:35 -05001143 if (bits == EXTENT_DIRTY) {
1144 *start = 0;
1145 total_bytes = tree->dirty_bytes;
1146 goto out;
1147 }
Chris Mason793955b2007-11-26 16:34:41 -08001148 /*
1149 * this search will find all the extents that end after
1150 * our range starts.
1151 */
1152 node = tree_search(&tree->state, cur_start);
1153 if (!node || IS_ERR(node)) {
1154 goto out;
1155 }
1156
1157 while(1) {
1158 state = rb_entry(node, struct extent_state, rb_node);
1159 if ((state->state & bits)) {
1160 total_bytes += state->end - state->start + 1;
1161 if (total_bytes >= max_bytes)
1162 break;
1163 if (!found) {
1164 *start = state->start;
1165 found = 1;
1166 }
1167 }
1168 node = rb_next(node);
1169 if (!node)
1170 break;
1171 }
1172out:
1173 write_unlock_irq(&tree->lock);
1174 return total_bytes;
1175}
1176
Chris Masona52d9a82007-08-27 16:49:44 -04001177/*
1178 * helper function to lock both pages and extents in the tree.
1179 * pages must be locked first.
1180 */
1181int lock_range(struct extent_map_tree *tree, u64 start, u64 end)
1182{
1183 unsigned long index = start >> PAGE_CACHE_SHIFT;
1184 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1185 struct page *page;
1186 int err;
1187
1188 while (index <= end_index) {
1189 page = grab_cache_page(tree->mapping, index);
1190 if (!page) {
1191 err = -ENOMEM;
1192 goto failed;
1193 }
1194 if (IS_ERR(page)) {
1195 err = PTR_ERR(page);
1196 goto failed;
1197 }
1198 index++;
1199 }
1200 lock_extent(tree, start, end, GFP_NOFS);
1201 return 0;
1202
1203failed:
1204 /*
1205 * we failed above in getting the page at 'index', so we undo here
1206 * up to but not including the page at 'index'
1207 */
1208 end_index = index;
1209 index = start >> PAGE_CACHE_SHIFT;
1210 while (index < end_index) {
1211 page = find_get_page(tree->mapping, index);
1212 unlock_page(page);
1213 page_cache_release(page);
1214 index++;
1215 }
1216 return err;
1217}
1218EXPORT_SYMBOL(lock_range);
1219
1220/*
1221 * helper function to unlock both pages and extents in the tree.
1222 */
1223int unlock_range(struct extent_map_tree *tree, u64 start, u64 end)
1224{
1225 unsigned long index = start >> PAGE_CACHE_SHIFT;
1226 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1227 struct page *page;
1228
1229 while (index <= end_index) {
1230 page = find_get_page(tree->mapping, index);
1231 unlock_page(page);
1232 page_cache_release(page);
1233 index++;
1234 }
1235 unlock_extent(tree, start, end, GFP_NOFS);
1236 return 0;
1237}
1238EXPORT_SYMBOL(unlock_range);
1239
Chris Mason07157aa2007-08-30 08:50:51 -04001240int set_state_private(struct extent_map_tree *tree, u64 start, u64 private)
1241{
1242 struct rb_node *node;
1243 struct extent_state *state;
1244 int ret = 0;
1245
1246 write_lock_irq(&tree->lock);
1247 /*
1248 * this search will find all the extents that end after
1249 * our range starts.
1250 */
1251 node = tree_search(&tree->state, start);
1252 if (!node || IS_ERR(node)) {
1253 ret = -ENOENT;
1254 goto out;
1255 }
1256 state = rb_entry(node, struct extent_state, rb_node);
1257 if (state->start != start) {
1258 ret = -ENOENT;
1259 goto out;
1260 }
1261 state->private = private;
1262out:
1263 write_unlock_irq(&tree->lock);
1264 return ret;
Chris Mason07157aa2007-08-30 08:50:51 -04001265}
1266
1267int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private)
1268{
1269 struct rb_node *node;
1270 struct extent_state *state;
1271 int ret = 0;
1272
1273 read_lock_irq(&tree->lock);
1274 /*
1275 * this search will find all the extents that end after
1276 * our range starts.
1277 */
1278 node = tree_search(&tree->state, start);
1279 if (!node || IS_ERR(node)) {
1280 ret = -ENOENT;
1281 goto out;
1282 }
1283 state = rb_entry(node, struct extent_state, rb_node);
1284 if (state->start != start) {
1285 ret = -ENOENT;
1286 goto out;
1287 }
1288 *private = state->private;
1289out:
1290 read_unlock_irq(&tree->lock);
1291 return ret;
1292}
1293
Chris Masona52d9a82007-08-27 16:49:44 -04001294/*
1295 * searches a range in the state tree for a given mask.
1296 * If 'filled' == 1, this returns 1 only if ever extent in the tree
1297 * has the bits set. Otherwise, 1 is returned if any bit in the
1298 * range is found set.
1299 */
Chris Mason1a5bc162007-10-15 16:15:26 -04001300int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
1301 int bits, int filled)
Chris Masona52d9a82007-08-27 16:49:44 -04001302{
1303 struct extent_state *state = NULL;
1304 struct rb_node *node;
1305 int bitset = 0;
1306
1307 read_lock_irq(&tree->lock);
1308 node = tree_search(&tree->state, start);
1309 while (node && start <= end) {
1310 state = rb_entry(node, struct extent_state, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -04001311
1312 if (filled && state->start > start) {
1313 bitset = 0;
1314 break;
1315 }
Chris Mason0591fb52007-11-11 08:22:00 -05001316
1317 if (state->start > end)
1318 break;
1319
Chris Masona52d9a82007-08-27 16:49:44 -04001320 if (state->state & bits) {
1321 bitset = 1;
1322 if (!filled)
1323 break;
1324 } else if (filled) {
1325 bitset = 0;
1326 break;
1327 }
1328 start = state->end + 1;
1329 if (start > end)
1330 break;
1331 node = rb_next(node);
1332 }
1333 read_unlock_irq(&tree->lock);
1334 return bitset;
1335}
Chris Mason1a5bc162007-10-15 16:15:26 -04001336EXPORT_SYMBOL(test_range_bit);
Chris Masona52d9a82007-08-27 16:49:44 -04001337
1338/*
1339 * helper function to set a given page up to date if all the
1340 * extents in the tree for that page are up to date
1341 */
1342static int check_page_uptodate(struct extent_map_tree *tree,
1343 struct page *page)
1344{
Chris Mason35ebb932007-10-30 16:56:53 -04001345 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001346 u64 end = start + PAGE_CACHE_SIZE - 1;
1347 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1348 SetPageUptodate(page);
1349 return 0;
1350}
1351
1352/*
1353 * helper function to unlock a page if all the extents in the tree
1354 * for that page are unlocked
1355 */
1356static int check_page_locked(struct extent_map_tree *tree,
1357 struct page *page)
1358{
Chris Mason35ebb932007-10-30 16:56:53 -04001359 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001360 u64 end = start + PAGE_CACHE_SIZE - 1;
1361 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1362 unlock_page(page);
1363 return 0;
1364}
1365
1366/*
1367 * helper function to end page writeback if all the extents
1368 * in the tree for that page are done with writeback
1369 */
1370static int check_page_writeback(struct extent_map_tree *tree,
1371 struct page *page)
1372{
Chris Mason35ebb932007-10-30 16:56:53 -04001373 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001374 u64 end = start + PAGE_CACHE_SIZE - 1;
1375 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1376 end_page_writeback(page);
1377 return 0;
1378}
1379
1380/* lots and lots of room for performance fixes in the end_bio funcs */
1381
1382/*
1383 * after a writepage IO is done, we need to:
1384 * clear the uptodate bits on error
1385 * clear the writeback bits in the extent tree for this IO
1386 * end_page_writeback if the page has no more pending IO
1387 *
1388 * Scheduling is not allowed, so the extent state tree is expected
1389 * to have one and only one object corresponding to this IO.
1390 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001391#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1392static void end_bio_extent_writepage(struct bio *bio, int err)
1393#else
Chris Masona52d9a82007-08-27 16:49:44 -04001394static int end_bio_extent_writepage(struct bio *bio,
1395 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001396#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001397{
1398 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1399 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1400 struct extent_map_tree *tree = bio->bi_private;
1401 u64 start;
1402 u64 end;
1403 int whole_page;
1404
Jens Axboe0a2118d2007-10-19 09:23:05 -04001405#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001406 if (bio->bi_size)
1407 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001408#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001409
1410 do {
1411 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001412 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1413 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001414 end = start + bvec->bv_len - 1;
1415
1416 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1417 whole_page = 1;
1418 else
1419 whole_page = 0;
1420
1421 if (--bvec >= bio->bi_io_vec)
1422 prefetchw(&bvec->bv_page->flags);
1423
1424 if (!uptodate) {
1425 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1426 ClearPageUptodate(page);
1427 SetPageError(page);
1428 }
1429 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
1430
1431 if (whole_page)
1432 end_page_writeback(page);
1433 else
1434 check_page_writeback(tree, page);
Christoph Hellwig0e2752a2007-09-10 20:02:33 -04001435 if (tree->ops && tree->ops->writepage_end_io_hook)
1436 tree->ops->writepage_end_io_hook(page, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001437 } while (bvec >= bio->bi_io_vec);
1438
1439 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001440#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001441 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001442#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001443}
1444
1445/*
1446 * after a readpage IO is done, we need to:
1447 * clear the uptodate bits on error
1448 * set the uptodate bits if things worked
1449 * set the page up to date if all extents in the tree are uptodate
1450 * clear the lock bit in the extent tree
1451 * unlock the page if there are no other extents locked for it
1452 *
1453 * Scheduling is not allowed, so the extent state tree is expected
1454 * to have one and only one object corresponding to this IO.
1455 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001456#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1457static void end_bio_extent_readpage(struct bio *bio, int err)
1458#else
Chris Masona52d9a82007-08-27 16:49:44 -04001459static int end_bio_extent_readpage(struct bio *bio,
1460 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001461#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001462{
Chris Mason07157aa2007-08-30 08:50:51 -04001463 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04001464 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1465 struct extent_map_tree *tree = bio->bi_private;
1466 u64 start;
1467 u64 end;
1468 int whole_page;
Chris Mason07157aa2007-08-30 08:50:51 -04001469 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04001470
Jens Axboe0a2118d2007-10-19 09:23:05 -04001471#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001472 if (bio->bi_size)
1473 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001474#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001475
1476 do {
1477 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001478 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1479 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001480 end = start + bvec->bv_len - 1;
1481
1482 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1483 whole_page = 1;
1484 else
1485 whole_page = 0;
1486
1487 if (--bvec >= bio->bi_io_vec)
1488 prefetchw(&bvec->bv_page->flags);
1489
Chris Mason07157aa2007-08-30 08:50:51 -04001490 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1491 ret = tree->ops->readpage_end_io_hook(page, start, end);
1492 if (ret)
1493 uptodate = 0;
1494 }
Chris Masona52d9a82007-08-27 16:49:44 -04001495 if (uptodate) {
1496 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1497 if (whole_page)
1498 SetPageUptodate(page);
1499 else
1500 check_page_uptodate(tree, page);
1501 } else {
1502 ClearPageUptodate(page);
1503 SetPageError(page);
1504 }
1505
1506 unlock_extent(tree, start, end, GFP_ATOMIC);
1507
1508 if (whole_page)
1509 unlock_page(page);
1510 else
1511 check_page_locked(tree, page);
1512 } while (bvec >= bio->bi_io_vec);
1513
1514 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001515#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001516 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001517#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001518}
1519
1520/*
1521 * IO done from prepare_write is pretty simple, we just unlock
1522 * the structs in the extent tree when done, and set the uptodate bits
1523 * as appropriate.
1524 */
Jens Axboe0a2118d2007-10-19 09:23:05 -04001525#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1526static void end_bio_extent_preparewrite(struct bio *bio, int err)
1527#else
Chris Masona52d9a82007-08-27 16:49:44 -04001528static int end_bio_extent_preparewrite(struct bio *bio,
1529 unsigned int bytes_done, int err)
Jens Axboe0a2118d2007-10-19 09:23:05 -04001530#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001531{
1532 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1533 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1534 struct extent_map_tree *tree = bio->bi_private;
1535 u64 start;
1536 u64 end;
1537
Jens Axboe0a2118d2007-10-19 09:23:05 -04001538#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001539 if (bio->bi_size)
1540 return 1;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001541#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001542
1543 do {
1544 struct page *page = bvec->bv_page;
Chris Mason35ebb932007-10-30 16:56:53 -04001545 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1546 bvec->bv_offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001547 end = start + bvec->bv_len - 1;
1548
1549 if (--bvec >= bio->bi_io_vec)
1550 prefetchw(&bvec->bv_page->flags);
1551
1552 if (uptodate) {
1553 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1554 } else {
1555 ClearPageUptodate(page);
1556 SetPageError(page);
1557 }
1558
1559 unlock_extent(tree, start, end, GFP_ATOMIC);
1560
1561 } while (bvec >= bio->bi_io_vec);
1562
1563 bio_put(bio);
Jens Axboe0a2118d2007-10-19 09:23:05 -04001564#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masona52d9a82007-08-27 16:49:44 -04001565 return 0;
Jens Axboe0a2118d2007-10-19 09:23:05 -04001566#endif
Chris Masona52d9a82007-08-27 16:49:44 -04001567}
1568
Chris Masonb293f022007-11-01 19:45:34 -04001569static struct bio *
1570extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1571 gfp_t gfp_flags)
1572{
1573 struct bio *bio;
1574
1575 bio = bio_alloc(gfp_flags, nr_vecs);
1576
1577 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1578 while (!bio && (nr_vecs /= 2))
1579 bio = bio_alloc(gfp_flags, nr_vecs);
1580 }
1581
1582 if (bio) {
1583 bio->bi_bdev = bdev;
1584 bio->bi_sector = first_sector;
1585 }
1586 return bio;
1587}
1588
1589static int submit_one_bio(int rw, struct bio *bio)
1590{
Chris Mason6da6aba2007-12-18 16:15:09 -05001591 u64 maxsector;
Chris Masonb293f022007-11-01 19:45:34 -04001592 int ret = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -05001593
Chris Masonb293f022007-11-01 19:45:34 -04001594 bio_get(bio);
Chris Mason6da6aba2007-12-18 16:15:09 -05001595
1596 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1597 if (maxsector < bio->bi_sector) {
1598 printk("sector too large max %Lu got %llu\n", maxsector,
1599 (unsigned long long)bio->bi_sector);
1600 WARN_ON(1);
1601 }
1602
Chris Masonb293f022007-11-01 19:45:34 -04001603 submit_bio(rw, bio);
1604 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1605 ret = -EOPNOTSUPP;
1606 bio_put(bio);
1607 return ret;
1608}
1609
Chris Masona52d9a82007-08-27 16:49:44 -04001610static int submit_extent_page(int rw, struct extent_map_tree *tree,
1611 struct page *page, sector_t sector,
1612 size_t size, unsigned long offset,
1613 struct block_device *bdev,
Chris Masonb293f022007-11-01 19:45:34 -04001614 struct bio **bio_ret,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001615 unsigned long max_pages,
Chris Masona52d9a82007-08-27 16:49:44 -04001616 bio_end_io_t end_io_func)
1617{
Chris Masona52d9a82007-08-27 16:49:44 -04001618 int ret = 0;
Chris Masonb293f022007-11-01 19:45:34 -04001619 struct bio *bio;
1620 int nr;
Chris Masona52d9a82007-08-27 16:49:44 -04001621
Chris Masonb293f022007-11-01 19:45:34 -04001622 if (bio_ret && *bio_ret) {
1623 bio = *bio_ret;
1624 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
1625 bio_add_page(bio, page, size, offset) < size) {
1626 ret = submit_one_bio(rw, bio);
1627 bio = NULL;
1628 } else {
1629 return 0;
1630 }
1631 }
Chris Mason3ab2fb52007-11-08 10:59:22 -05001632 nr = min_t(int, max_pages, bio_get_nr_vecs(bdev));
Chris Masonb293f022007-11-01 19:45:34 -04001633 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1634 if (!bio) {
1635 printk("failed to allocate bio nr %d\n", nr);
1636 }
1637 bio_add_page(bio, page, size, offset);
Chris Masona52d9a82007-08-27 16:49:44 -04001638 bio->bi_end_io = end_io_func;
1639 bio->bi_private = tree;
Chris Masonb293f022007-11-01 19:45:34 -04001640 if (bio_ret) {
1641 *bio_ret = bio;
1642 } else {
1643 ret = submit_one_bio(rw, bio);
1644 }
Chris Masona52d9a82007-08-27 16:49:44 -04001645
Chris Masona52d9a82007-08-27 16:49:44 -04001646 return ret;
1647}
1648
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001649void set_page_extent_mapped(struct page *page)
1650{
1651 if (!PagePrivate(page)) {
1652 SetPagePrivate(page);
1653 WARN_ON(!page->mapping->a_ops->invalidatepage);
Chris Mason19c00dd2007-10-15 16:19:22 -04001654 set_page_private(page, EXTENT_PAGE_PRIVATE);
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001655 page_cache_get(page);
1656 }
1657}
1658
Chris Masona52d9a82007-08-27 16:49:44 -04001659/*
1660 * basic readpage implementation. Locked extent state structs are inserted
1661 * into the tree that are removed when the IO is done (by the end_io
1662 * handlers)
1663 */
Chris Mason3ab2fb52007-11-08 10:59:22 -05001664static int __extent_read_full_page(struct extent_map_tree *tree,
1665 struct page *page,
1666 get_extent_t *get_extent,
1667 struct bio **bio)
Chris Masona52d9a82007-08-27 16:49:44 -04001668{
1669 struct inode *inode = page->mapping->host;
Chris Mason35ebb932007-10-30 16:56:53 -04001670 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001671 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1672 u64 end;
1673 u64 cur = start;
1674 u64 extent_offset;
1675 u64 last_byte = i_size_read(inode);
1676 u64 block_start;
1677 u64 cur_end;
1678 sector_t sector;
1679 struct extent_map *em;
1680 struct block_device *bdev;
1681 int ret;
1682 int nr = 0;
1683 size_t page_offset = 0;
1684 size_t iosize;
1685 size_t blocksize = inode->i_sb->s_blocksize;
1686
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001687 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001688
1689 end = page_end;
1690 lock_extent(tree, start, end, GFP_NOFS);
1691
1692 while (cur <= end) {
1693 if (cur >= last_byte) {
Chris Mason6da6aba2007-12-18 16:15:09 -05001694 char *userpage;
Chris Masona52d9a82007-08-27 16:49:44 -04001695 iosize = PAGE_CACHE_SIZE - page_offset;
Chris Mason6da6aba2007-12-18 16:15:09 -05001696 userpage = kmap_atomic(page, KM_USER0);
1697 memset(userpage + page_offset, 0, iosize);
1698 flush_dcache_page(page);
1699 kunmap_atomic(userpage, KM_USER0);
Chris Masona52d9a82007-08-27 16:49:44 -04001700 set_extent_uptodate(tree, cur, cur + iosize - 1,
1701 GFP_NOFS);
1702 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1703 break;
1704 }
1705 em = get_extent(inode, page, page_offset, cur, end, 0);
1706 if (IS_ERR(em) || !em) {
1707 SetPageError(page);
1708 unlock_extent(tree, cur, end, GFP_NOFS);
1709 break;
1710 }
1711
1712 extent_offset = cur - em->start;
1713 BUG_ON(em->end < cur);
1714 BUG_ON(end < cur);
1715
1716 iosize = min(em->end - cur, end - cur) + 1;
1717 cur_end = min(em->end, end);
1718 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1719 sector = (em->block_start + extent_offset) >> 9;
1720 bdev = em->bdev;
1721 block_start = em->block_start;
1722 free_extent_map(em);
1723 em = NULL;
1724
1725 /* we've found a hole, just zero and go on */
Chris Mason5f39d392007-10-15 16:14:19 -04001726 if (block_start == EXTENT_MAP_HOLE) {
Chris Mason6da6aba2007-12-18 16:15:09 -05001727 char *userpage;
1728 userpage = kmap_atomic(page, KM_USER0);
1729 memset(userpage + page_offset, 0, iosize);
1730 flush_dcache_page(page);
1731 kunmap_atomic(userpage, KM_USER0);
1732
Chris Masona52d9a82007-08-27 16:49:44 -04001733 set_extent_uptodate(tree, cur, cur + iosize - 1,
1734 GFP_NOFS);
1735 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1736 cur = cur + iosize;
1737 page_offset += iosize;
1738 continue;
1739 }
1740 /* the get_extent function already copied into the page */
1741 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1742 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1743 cur = cur + iosize;
1744 page_offset += iosize;
1745 continue;
1746 }
1747
Chris Mason07157aa2007-08-30 08:50:51 -04001748 ret = 0;
1749 if (tree->ops && tree->ops->readpage_io_hook) {
1750 ret = tree->ops->readpage_io_hook(page, cur,
1751 cur + iosize - 1);
1752 }
1753 if (!ret) {
Chris Mason3ab2fb52007-11-08 10:59:22 -05001754 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1755 nr -= page->index;
Chris Mason07157aa2007-08-30 08:50:51 -04001756 ret = submit_extent_page(READ, tree, page,
Chris Mason3ab2fb52007-11-08 10:59:22 -05001757 sector, iosize, page_offset,
1758 bdev, bio, nr,
1759 end_bio_extent_readpage);
Chris Mason07157aa2007-08-30 08:50:51 -04001760 }
Chris Masona52d9a82007-08-27 16:49:44 -04001761 if (ret)
1762 SetPageError(page);
1763 cur = cur + iosize;
1764 page_offset += iosize;
1765 nr++;
1766 }
1767 if (!nr) {
1768 if (!PageError(page))
1769 SetPageUptodate(page);
1770 unlock_page(page);
1771 }
1772 return 0;
1773}
Chris Mason3ab2fb52007-11-08 10:59:22 -05001774
1775int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
1776 get_extent_t *get_extent)
1777{
1778 struct bio *bio = NULL;
1779 int ret;
1780
1781 ret = __extent_read_full_page(tree, page, get_extent, &bio);
1782 if (bio)
1783 submit_one_bio(READ, bio);
1784 return ret;
1785}
Chris Masona52d9a82007-08-27 16:49:44 -04001786EXPORT_SYMBOL(extent_read_full_page);
1787
1788/*
1789 * the writepage semantics are similar to regular writepage. extent
1790 * records are inserted to lock ranges in the tree, and as dirty areas
1791 * are found, they are marked writeback. Then the lock bits are removed
1792 * and the end_io handler clears the writeback ranges
1793 */
Chris Masonb293f022007-11-01 19:45:34 -04001794static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1795 void *data)
Chris Masona52d9a82007-08-27 16:49:44 -04001796{
1797 struct inode *inode = page->mapping->host;
Chris Masonb293f022007-11-01 19:45:34 -04001798 struct extent_page_data *epd = data;
1799 struct extent_map_tree *tree = epd->tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001800 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason3e9fd942007-11-20 10:47:25 -05001801 u64 delalloc_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001802 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1803 u64 end;
1804 u64 cur = start;
1805 u64 extent_offset;
1806 u64 last_byte = i_size_read(inode);
1807 u64 block_start;
Chris Mason179e29e2007-11-01 11:28:41 -04001808 u64 iosize;
Chris Masona52d9a82007-08-27 16:49:44 -04001809 sector_t sector;
1810 struct extent_map *em;
1811 struct block_device *bdev;
1812 int ret;
1813 int nr = 0;
1814 size_t page_offset = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04001815 size_t blocksize;
1816 loff_t i_size = i_size_read(inode);
1817 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001818 u64 nr_delalloc;
1819 u64 delalloc_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001820
Chris Masonb888db22007-08-27 16:49:44 -04001821 WARN_ON(!PageLocked(page));
Chris Masona52d9a82007-08-27 16:49:44 -04001822 if (page->index > end_index) {
1823 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1824 unlock_page(page);
1825 return 0;
1826 }
1827
1828 if (page->index == end_index) {
Chris Mason6da6aba2007-12-18 16:15:09 -05001829 char *userpage;
1830
Chris Masona52d9a82007-08-27 16:49:44 -04001831 size_t offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason6da6aba2007-12-18 16:15:09 -05001832
1833 userpage = kmap_atomic(page, KM_USER0);
1834 memset(userpage + offset, 0, PAGE_CACHE_SIZE - offset);
1835 flush_dcache_page(page);
1836 kunmap_atomic(userpage, KM_USER0);
Chris Masona52d9a82007-08-27 16:49:44 -04001837 }
1838
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001839 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001840
Chris Mason3e9fd942007-11-20 10:47:25 -05001841 delalloc_start = start;
1842 delalloc_end = 0;
1843 while(delalloc_end < page_end) {
1844 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
1845 &delalloc_end,
1846 128 * 1024 * 1024);
Chris Mason190662b2007-12-18 16:25:45 -05001847 if (nr_delalloc == 0) {
1848 delalloc_start = delalloc_end + 1;
1849 continue;
1850 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001851 tree->ops->fill_delalloc(inode, delalloc_start,
1852 delalloc_end);
1853 clear_extent_bit(tree, delalloc_start,
1854 delalloc_end,
1855 EXTENT_LOCKED | EXTENT_DELALLOC,
1856 1, 0, GFP_NOFS);
1857 delalloc_start = delalloc_end + 1;
Chris Masonb888db22007-08-27 16:49:44 -04001858 }
Chris Mason3e9fd942007-11-20 10:47:25 -05001859 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Masonb888db22007-08-27 16:49:44 -04001860
1861 end = page_end;
1862 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
1863 printk("found delalloc bits after lock_extent\n");
1864 }
Chris Masona52d9a82007-08-27 16:49:44 -04001865
1866 if (last_byte <= start) {
1867 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
1868 goto done;
1869 }
1870
1871 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
1872 blocksize = inode->i_sb->s_blocksize;
1873
1874 while (cur <= end) {
1875 if (cur >= last_byte) {
1876 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
1877 break;
1878 }
Chris Masonb293f022007-11-01 19:45:34 -04001879 em = epd->get_extent(inode, page, page_offset, cur, end, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04001880 if (IS_ERR(em) || !em) {
1881 SetPageError(page);
1882 break;
1883 }
1884
1885 extent_offset = cur - em->start;
1886 BUG_ON(em->end < cur);
1887 BUG_ON(end < cur);
1888 iosize = min(em->end - cur, end - cur) + 1;
1889 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1890 sector = (em->block_start + extent_offset) >> 9;
1891 bdev = em->bdev;
1892 block_start = em->block_start;
1893 free_extent_map(em);
1894 em = NULL;
1895
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (block_start == EXTENT_MAP_HOLE ||
1897 block_start == EXTENT_MAP_INLINE) {
Chris Masona52d9a82007-08-27 16:49:44 -04001898 clear_extent_dirty(tree, cur,
1899 cur + iosize - 1, GFP_NOFS);
1900 cur = cur + iosize;
1901 page_offset += iosize;
1902 continue;
1903 }
1904
1905 /* leave this out until we have a page_mkwrite call */
1906 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
1907 EXTENT_DIRTY, 0)) {
1908 cur = cur + iosize;
1909 page_offset += iosize;
1910 continue;
1911 }
1912 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
Christoph Hellwigb06355f2007-09-10 20:02:32 -04001913 if (tree->ops && tree->ops->writepage_io_hook) {
1914 ret = tree->ops->writepage_io_hook(page, cur,
1915 cur + iosize - 1);
1916 } else {
1917 ret = 0;
1918 }
Chris Masona52d9a82007-08-27 16:49:44 -04001919 if (ret)
1920 SetPageError(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001921 else {
Chris Mason7073c8e2007-11-20 13:44:45 -05001922 unsigned long max_nr = end_index + 1;
Chris Mason07157aa2007-08-30 08:50:51 -04001923 set_range_writeback(tree, cur, cur + iosize - 1);
Chris Mason7073c8e2007-11-20 13:44:45 -05001924 if (!PageWriteback(page)) {
1925 printk("warning page %lu not writeback, "
1926 "cur %llu end %llu\n", page->index,
1927 (unsigned long long)cur,
1928 (unsigned long long)end);
1929 }
Chris Masonb293f022007-11-01 19:45:34 -04001930
Chris Mason07157aa2007-08-30 08:50:51 -04001931 ret = submit_extent_page(WRITE, tree, page, sector,
1932 iosize, page_offset, bdev,
Chris Mason7073c8e2007-11-20 13:44:45 -05001933 &epd->bio, max_nr,
Chris Mason07157aa2007-08-30 08:50:51 -04001934 end_bio_extent_writepage);
1935 if (ret)
1936 SetPageError(page);
1937 }
Chris Masona52d9a82007-08-27 16:49:44 -04001938 cur = cur + iosize;
1939 page_offset += iosize;
1940 nr++;
1941 }
1942done:
Chris Mason7073c8e2007-11-20 13:44:45 -05001943 if (nr == 0) {
1944 /* make sure the mapping tag for page dirty gets cleared */
1945 set_page_writeback(page);
1946 end_page_writeback(page);
1947 }
Chris Masona52d9a82007-08-27 16:49:44 -04001948 unlock_extent(tree, start, page_end, GFP_NOFS);
1949 unlock_page(page);
1950 return 0;
1951}
Chris Masonb293f022007-11-01 19:45:34 -04001952
Chris Mason6da6aba2007-12-18 16:15:09 -05001953#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1954
1955/* Taken directly from 2.6.23 for 2.6.18 back port */
1956typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
1957 void *data);
1958
1959/**
1960 * write_cache_pages - walk the list of dirty pages of the given address space
1961 * and write all of them.
1962 * @mapping: address space structure to write
1963 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
1964 * @writepage: function called for each page
1965 * @data: data passed to writepage function
1966 *
1967 * If a page is already under I/O, write_cache_pages() skips it, even
1968 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
1969 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
1970 * and msync() need to guarantee that all the data which was dirty at the time
1971 * the call was made get new I/O started against them. If wbc->sync_mode is
1972 * WB_SYNC_ALL then we were called for data integrity and we must wait for
1973 * existing IO to complete.
1974 */
1975static int write_cache_pages(struct address_space *mapping,
1976 struct writeback_control *wbc, writepage_t writepage,
1977 void *data)
1978{
1979 struct backing_dev_info *bdi = mapping->backing_dev_info;
1980 int ret = 0;
1981 int done = 0;
1982 struct pagevec pvec;
1983 int nr_pages;
1984 pgoff_t index;
1985 pgoff_t end; /* Inclusive */
1986 int scanned = 0;
1987 int range_whole = 0;
1988
1989 if (wbc->nonblocking && bdi_write_congested(bdi)) {
1990 wbc->encountered_congestion = 1;
1991 return 0;
1992 }
1993
1994 pagevec_init(&pvec, 0);
1995 if (wbc->range_cyclic) {
1996 index = mapping->writeback_index; /* Start from prev offset */
1997 end = -1;
1998 } else {
1999 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2000 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2001 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2002 range_whole = 1;
2003 scanned = 1;
2004 }
2005retry:
2006 while (!done && (index <= end) &&
2007 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2008 PAGECACHE_TAG_DIRTY,
2009 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2010 unsigned i;
2011
2012 scanned = 1;
2013 for (i = 0; i < nr_pages; i++) {
2014 struct page *page = pvec.pages[i];
2015
2016 /*
2017 * At this point we hold neither mapping->tree_lock nor
2018 * lock on the page itself: the page may be truncated or
2019 * invalidated (changing page->mapping to NULL), or even
2020 * swizzled back from swapper_space to tmpfs file
2021 * mapping
2022 */
2023 lock_page(page);
2024
2025 if (unlikely(page->mapping != mapping)) {
2026 unlock_page(page);
2027 continue;
2028 }
2029
2030 if (!wbc->range_cyclic && page->index > end) {
2031 done = 1;
2032 unlock_page(page);
2033 continue;
2034 }
2035
2036 if (wbc->sync_mode != WB_SYNC_NONE)
2037 wait_on_page_writeback(page);
2038
2039 if (PageWriteback(page) ||
2040 !clear_page_dirty_for_io(page)) {
2041 unlock_page(page);
2042 continue;
2043 }
2044
2045 ret = (*writepage)(page, wbc, data);
2046
2047 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2048 unlock_page(page);
2049 ret = 0;
2050 }
2051 if (ret || (--(wbc->nr_to_write) <= 0))
2052 done = 1;
2053 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2054 wbc->encountered_congestion = 1;
2055 done = 1;
2056 }
2057 }
2058 pagevec_release(&pvec);
2059 cond_resched();
2060 }
2061 if (!scanned && !done) {
2062 /*
2063 * We hit the last page and there is more work to be done: wrap
2064 * back to the start of the file
2065 */
2066 scanned = 1;
2067 index = 0;
2068 goto retry;
2069 }
2070 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2071 mapping->writeback_index = index;
2072 return ret;
2073}
2074#endif
2075
Chris Masonb293f022007-11-01 19:45:34 -04002076int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
2077 get_extent_t *get_extent,
2078 struct writeback_control *wbc)
2079{
2080 int ret;
Chris Mason015a739c2007-11-26 16:15:16 -08002081 struct address_space *mapping = page->mapping;
Chris Masonb293f022007-11-01 19:45:34 -04002082 struct extent_page_data epd = {
2083 .bio = NULL,
2084 .tree = tree,
2085 .get_extent = get_extent,
2086 };
Chris Mason015a739c2007-11-26 16:15:16 -08002087 struct writeback_control wbc_writepages = {
2088 .bdi = wbc->bdi,
2089 .sync_mode = WB_SYNC_NONE,
2090 .older_than_this = NULL,
2091 .nr_to_write = 64,
2092 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2093 .range_end = (loff_t)-1,
2094 };
2095
Chris Masonb293f022007-11-01 19:45:34 -04002096
2097 ret = __extent_writepage(page, wbc, &epd);
Chris Mason015a739c2007-11-26 16:15:16 -08002098
2099 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
Chris Mason6da6aba2007-12-18 16:15:09 -05002100 if (epd.bio) {
Chris Masonb293f022007-11-01 19:45:34 -04002101 submit_one_bio(WRITE, epd.bio);
Chris Mason6da6aba2007-12-18 16:15:09 -05002102 }
Chris Masonb293f022007-11-01 19:45:34 -04002103 return ret;
2104}
Chris Masona52d9a82007-08-27 16:49:44 -04002105EXPORT_SYMBOL(extent_write_full_page);
2106
Chris Mason6da6aba2007-12-18 16:15:09 -05002107
Chris Masonb293f022007-11-01 19:45:34 -04002108int extent_writepages(struct extent_map_tree *tree,
2109 struct address_space *mapping,
2110 get_extent_t *get_extent,
2111 struct writeback_control *wbc)
2112{
Chris Mason6da6aba2007-12-18 16:15:09 -05002113 int ret = 0;
Chris Masonb293f022007-11-01 19:45:34 -04002114 struct extent_page_data epd = {
2115 .bio = NULL,
2116 .tree = tree,
2117 .get_extent = get_extent,
2118 };
2119
2120 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
Chris Mason6da6aba2007-12-18 16:15:09 -05002121 if (epd.bio) {
Chris Masonb293f022007-11-01 19:45:34 -04002122 submit_one_bio(WRITE, epd.bio);
Chris Mason6da6aba2007-12-18 16:15:09 -05002123 }
Chris Masonb293f022007-11-01 19:45:34 -04002124 return ret;
2125}
2126EXPORT_SYMBOL(extent_writepages);
2127
Chris Mason3ab2fb52007-11-08 10:59:22 -05002128int extent_readpages(struct extent_map_tree *tree,
2129 struct address_space *mapping,
2130 struct list_head *pages, unsigned nr_pages,
2131 get_extent_t get_extent)
2132{
2133 struct bio *bio = NULL;
2134 unsigned page_idx;
2135 struct pagevec pvec;
2136
2137 pagevec_init(&pvec, 0);
2138 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2139 struct page *page = list_entry(pages->prev, struct page, lru);
2140
2141 prefetchw(&page->flags);
2142 list_del(&page->lru);
2143 /*
2144 * what we want to do here is call add_to_page_cache_lru,
2145 * but that isn't exported, so we reproduce it here
2146 */
2147 if (!add_to_page_cache(page, mapping,
2148 page->index, GFP_KERNEL)) {
2149
2150 /* open coding of lru_cache_add, also not exported */
2151 page_cache_get(page);
2152 if (!pagevec_add(&pvec, page))
2153 __pagevec_lru_add(&pvec);
2154 __extent_read_full_page(tree, page, get_extent, &bio);
2155 }
2156 page_cache_release(page);
2157 }
2158 if (pagevec_count(&pvec))
2159 __pagevec_lru_add(&pvec);
2160 BUG_ON(!list_empty(pages));
2161 if (bio)
2162 submit_one_bio(READ, bio);
2163 return 0;
2164}
2165EXPORT_SYMBOL(extent_readpages);
2166
Chris Masona52d9a82007-08-27 16:49:44 -04002167/*
2168 * basic invalidatepage code, this waits on any locked or writeback
2169 * ranges corresponding to the page, and then deletes any extent state
2170 * records from the tree
2171 */
2172int extent_invalidatepage(struct extent_map_tree *tree,
2173 struct page *page, unsigned long offset)
2174{
Chris Mason35ebb932007-10-30 16:56:53 -04002175 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Masona52d9a82007-08-27 16:49:44 -04002176 u64 end = start + PAGE_CACHE_SIZE - 1;
2177 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2178
2179 start += (offset + blocksize -1) & ~(blocksize - 1);
2180 if (start > end)
2181 return 0;
2182
2183 lock_extent(tree, start, end, GFP_NOFS);
2184 wait_on_extent_writeback(tree, start, end);
Chris Mason2bf5a722007-08-30 11:54:02 -04002185 clear_extent_bit(tree, start, end,
2186 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Masona52d9a82007-08-27 16:49:44 -04002187 1, 1, GFP_NOFS);
2188 return 0;
2189}
2190EXPORT_SYMBOL(extent_invalidatepage);
2191
2192/*
2193 * simple commit_write call, set_range_dirty is used to mark both
2194 * the pages and the extent records as dirty
2195 */
2196int extent_commit_write(struct extent_map_tree *tree,
2197 struct inode *inode, struct page *page,
2198 unsigned from, unsigned to)
2199{
2200 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2201
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04002202 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04002203 set_page_dirty(page);
2204
2205 if (pos > inode->i_size) {
2206 i_size_write(inode, pos);
2207 mark_inode_dirty(inode);
2208 }
2209 return 0;
2210}
2211EXPORT_SYMBOL(extent_commit_write);
2212
2213int extent_prepare_write(struct extent_map_tree *tree,
2214 struct inode *inode, struct page *page,
2215 unsigned from, unsigned to, get_extent_t *get_extent)
2216{
Chris Mason35ebb932007-10-30 16:56:53 -04002217 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002218 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2219 u64 block_start;
2220 u64 orig_block_start;
2221 u64 block_end;
2222 u64 cur_end;
2223 struct extent_map *em;
2224 unsigned blocksize = 1 << inode->i_blkbits;
2225 size_t page_offset = 0;
2226 size_t block_off_start;
2227 size_t block_off_end;
2228 int err = 0;
2229 int iocount = 0;
2230 int ret = 0;
2231 int isnew;
2232
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04002233 set_page_extent_mapped(page);
2234
Chris Masona52d9a82007-08-27 16:49:44 -04002235 block_start = (page_start + from) & ~((u64)blocksize - 1);
2236 block_end = (page_start + to - 1) | (blocksize - 1);
2237 orig_block_start = block_start;
2238
2239 lock_extent(tree, page_start, page_end, GFP_NOFS);
2240 while(block_start <= block_end) {
2241 em = get_extent(inode, page, page_offset, block_start,
2242 block_end, 1);
2243 if (IS_ERR(em) || !em) {
2244 goto err;
2245 }
2246 cur_end = min(block_end, em->end);
2247 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2248 block_off_end = block_off_start + blocksize;
2249 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2250
2251 if (!PageUptodate(page) && isnew &&
2252 (block_off_end > to || block_off_start < from)) {
2253 void *kaddr;
2254
2255 kaddr = kmap_atomic(page, KM_USER0);
2256 if (block_off_end > to)
2257 memset(kaddr + to, 0, block_off_end - to);
2258 if (block_off_start < from)
2259 memset(kaddr + block_off_start, 0,
2260 from - block_off_start);
2261 flush_dcache_page(page);
2262 kunmap_atomic(kaddr, KM_USER0);
2263 }
Chris Mason6da6aba2007-12-18 16:15:09 -05002264 if ((em->block_start != EXTENT_MAP_HOLE &&
2265 em->block_start != EXTENT_MAP_INLINE) &&
2266 !isnew && !PageUptodate(page) &&
Chris Masona52d9a82007-08-27 16:49:44 -04002267 (block_off_end > to || block_off_start < from) &&
2268 !test_range_bit(tree, block_start, cur_end,
2269 EXTENT_UPTODATE, 1)) {
2270 u64 sector;
2271 u64 extent_offset = block_start - em->start;
2272 size_t iosize;
2273 sector = (em->block_start + extent_offset) >> 9;
2274 iosize = (cur_end - block_start + blocksize - 1) &
2275 ~((u64)blocksize - 1);
2276 /*
2277 * we've already got the extent locked, but we
2278 * need to split the state such that our end_bio
2279 * handler can clear the lock.
2280 */
2281 set_extent_bit(tree, block_start,
2282 block_start + iosize - 1,
2283 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2284 ret = submit_extent_page(READ, tree, page,
2285 sector, iosize, page_offset, em->bdev,
Chris Masonb293f022007-11-01 19:45:34 -04002286 NULL, 1,
Chris Masona52d9a82007-08-27 16:49:44 -04002287 end_bio_extent_preparewrite);
2288 iocount++;
2289 block_start = block_start + iosize;
2290 } else {
2291 set_extent_uptodate(tree, block_start, cur_end,
2292 GFP_NOFS);
2293 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2294 block_start = cur_end + 1;
2295 }
2296 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2297 free_extent_map(em);
2298 }
2299 if (iocount) {
2300 wait_extent_bit(tree, orig_block_start,
2301 block_end, EXTENT_LOCKED);
2302 }
2303 check_page_uptodate(tree, page);
2304err:
2305 /* FIXME, zero out newly allocated blocks on error */
2306 return err;
2307}
2308EXPORT_SYMBOL(extent_prepare_write);
2309
2310/*
2311 * a helper for releasepage. As long as there are no locked extents
2312 * in the range corresponding to the page, both state records and extent
2313 * map records are removed
2314 */
2315int try_release_extent_mapping(struct extent_map_tree *tree, struct page *page)
2316{
2317 struct extent_map *em;
Chris Mason35ebb932007-10-30 16:56:53 -04002318 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002319 u64 end = start + PAGE_CACHE_SIZE - 1;
2320 u64 orig_start = start;
Chris Masonb888db22007-08-27 16:49:44 -04002321 int ret = 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002322
2323 while (start <= end) {
2324 em = lookup_extent_mapping(tree, start, end);
2325 if (!em || IS_ERR(em))
2326 break;
Chris Masonb888db22007-08-27 16:49:44 -04002327 if (!test_range_bit(tree, em->start, em->end,
2328 EXTENT_LOCKED, 0)) {
2329 remove_extent_mapping(tree, em);
2330 /* once for the rb tree */
Chris Masona52d9a82007-08-27 16:49:44 -04002331 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04002332 }
Chris Masona52d9a82007-08-27 16:49:44 -04002333 start = em->end + 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002334 /* once for us */
2335 free_extent_map(em);
2336 }
Chris Masonb888db22007-08-27 16:49:44 -04002337 if (test_range_bit(tree, orig_start, end, EXTENT_LOCKED, 0))
2338 ret = 0;
2339 else
2340 clear_extent_bit(tree, orig_start, end, EXTENT_UPTODATE,
2341 1, 1, GFP_NOFS);
2342 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002343}
2344EXPORT_SYMBOL(try_release_extent_mapping);
2345
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002346sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2347 get_extent_t *get_extent)
2348{
2349 struct inode *inode = mapping->host;
2350 u64 start = iblock << inode->i_blkbits;
2351 u64 end = start + (1 << inode->i_blkbits) - 1;
Yanc67cda12007-10-29 11:41:05 -04002352 sector_t sector = 0;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002353 struct extent_map *em;
2354
2355 em = get_extent(inode, NULL, 0, start, end, 0);
2356 if (!em || IS_ERR(em))
2357 return 0;
2358
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002359 if (em->block_start == EXTENT_MAP_INLINE ||
Chris Mason5f39d392007-10-15 16:14:19 -04002360 em->block_start == EXTENT_MAP_HOLE)
Yanc67cda12007-10-29 11:41:05 -04002361 goto out;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002362
Yanc67cda12007-10-29 11:41:05 -04002363 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2364out:
2365 free_extent_map(em);
2366 return sector;
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002367}
Chris Mason5f39d392007-10-15 16:14:19 -04002368
Chris Mason4dc119042007-10-15 16:18:14 -04002369static int add_lru(struct extent_map_tree *tree, struct extent_buffer *eb)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002370{
Chris Mason4dc119042007-10-15 16:18:14 -04002371 if (list_empty(&eb->lru)) {
2372 extent_buffer_get(eb);
2373 list_add(&eb->lru, &tree->buffer_lru);
2374 tree->lru_size++;
2375 if (tree->lru_size >= BUFFER_LRU_MAX) {
2376 struct extent_buffer *rm;
2377 rm = list_entry(tree->buffer_lru.prev,
2378 struct extent_buffer, lru);
2379 tree->lru_size--;
Chris Mason856bf3e2007-11-08 10:59:05 -05002380 list_del_init(&rm->lru);
Chris Mason4dc119042007-10-15 16:18:14 -04002381 free_extent_buffer(rm);
2382 }
2383 } else
2384 list_move(&eb->lru, &tree->buffer_lru);
2385 return 0;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002386}
Chris Mason4dc119042007-10-15 16:18:14 -04002387static struct extent_buffer *find_lru(struct extent_map_tree *tree,
2388 u64 start, unsigned long len)
Chris Mason6d36dcd2007-10-15 16:14:37 -04002389{
Chris Mason4dc119042007-10-15 16:18:14 -04002390 struct list_head *lru = &tree->buffer_lru;
2391 struct list_head *cur = lru->next;
2392 struct extent_buffer *eb;
Chris Masonf510cfe2007-10-15 16:14:48 -04002393
Chris Mason4dc119042007-10-15 16:18:14 -04002394 if (list_empty(lru))
2395 return NULL;
Chris Masonf510cfe2007-10-15 16:14:48 -04002396
Chris Mason4dc119042007-10-15 16:18:14 -04002397 do {
2398 eb = list_entry(cur, struct extent_buffer, lru);
2399 if (eb->start == start && eb->len == len) {
2400 extent_buffer_get(eb);
2401 return eb;
2402 }
2403 cur = cur->next;
2404 } while (cur != lru);
2405 return NULL;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002406}
2407
Chris Masondb945352007-10-15 16:15:53 -04002408static inline unsigned long num_extent_pages(u64 start, u64 len)
2409{
2410 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2411 (start >> PAGE_CACHE_SHIFT);
2412}
Chris Mason4dc119042007-10-15 16:18:14 -04002413
2414static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2415 unsigned long i)
2416{
2417 struct page *p;
Chris Mason3685f792007-10-19 09:23:27 -04002418 struct address_space *mapping;
Chris Mason4dc119042007-10-15 16:18:14 -04002419
2420 if (i == 0)
Chris Mason810191f2007-10-15 16:18:55 -04002421 return eb->first_page;
Chris Mason4dc119042007-10-15 16:18:14 -04002422 i += eb->start >> PAGE_CACHE_SHIFT;
Chris Mason3685f792007-10-19 09:23:27 -04002423 mapping = eb->first_page->mapping;
2424 read_lock_irq(&mapping->tree_lock);
2425 p = radix_tree_lookup(&mapping->page_tree, i);
2426 read_unlock_irq(&mapping->tree_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002427 return p;
2428}
2429
2430static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
2431 u64 start,
2432 unsigned long len,
2433 gfp_t mask)
2434{
2435 struct extent_buffer *eb = NULL;
2436
2437 spin_lock(&tree->lru_lock);
2438 eb = find_lru(tree, start, len);
Chris Mason4dc119042007-10-15 16:18:14 -04002439 spin_unlock(&tree->lru_lock);
Chris Mason4dc119042007-10-15 16:18:14 -04002440 if (eb) {
Chris Mason09be2072007-11-07 21:08:16 -05002441 return eb;
Chris Mason4dc119042007-10-15 16:18:14 -04002442 }
Chris Mason09be2072007-11-07 21:08:16 -05002443
2444 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Mason4dc119042007-10-15 16:18:14 -04002445 INIT_LIST_HEAD(&eb->lru);
2446 eb->start = start;
2447 eb->len = len;
2448 atomic_set(&eb->refs, 1);
2449
Chris Mason4dc119042007-10-15 16:18:14 -04002450 return eb;
2451}
2452
2453static void __free_extent_buffer(struct extent_buffer *eb)
2454{
2455 kmem_cache_free(extent_buffer_cache, eb);
2456}
2457
Chris Mason5f39d392007-10-15 16:14:19 -04002458struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
2459 u64 start, unsigned long len,
Chris Mason19c00dd2007-10-15 16:19:22 -04002460 struct page *page0,
Chris Mason5f39d392007-10-15 16:14:19 -04002461 gfp_t mask)
2462{
Chris Masondb945352007-10-15 16:15:53 -04002463 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason5f39d392007-10-15 16:14:19 -04002464 unsigned long i;
2465 unsigned long index = start >> PAGE_CACHE_SHIFT;
2466 struct extent_buffer *eb;
2467 struct page *p;
2468 struct address_space *mapping = tree->mapping;
Yan65555a02007-10-25 15:42:57 -04002469 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002470
Chris Mason4dc119042007-10-15 16:18:14 -04002471 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002472 if (!eb || IS_ERR(eb))
2473 return NULL;
2474
Chris Mason4dc119042007-10-15 16:18:14 -04002475 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002476 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002477
Chris Mason19c00dd2007-10-15 16:19:22 -04002478 if (page0) {
2479 eb->first_page = page0;
2480 i = 1;
2481 index++;
2482 page_cache_get(page0);
Chris Masonff79f812007-10-15 16:22:25 -04002483 mark_page_accessed(page0);
Chris Mason19c00dd2007-10-15 16:19:22 -04002484 set_page_extent_mapped(page0);
Chris Mason0591fb52007-11-11 08:22:00 -05002485 WARN_ON(!PageUptodate(page0));
Chris Mason19c00dd2007-10-15 16:19:22 -04002486 set_page_private(page0, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2487 len << 2);
2488 } else {
2489 i = 0;
2490 }
2491 for (; i < num_pages; i++, index++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002492 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002493 if (!p) {
Chris Masondb945352007-10-15 16:15:53 -04002494 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002495 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002496 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002497 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002498 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002499 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002500 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002501 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2502 len << 2);
2503 } else {
2504 set_page_private(p, EXTENT_PAGE_PRIVATE);
2505 }
Chris Mason5f39d392007-10-15 16:14:19 -04002506 if (!PageUptodate(p))
2507 uptodate = 0;
2508 unlock_page(p);
2509 }
2510 if (uptodate)
2511 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002512 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002513
2514lru_add:
2515 spin_lock(&tree->lru_lock);
2516 add_lru(tree, eb);
2517 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002518 return eb;
Chris Mason09be2072007-11-07 21:08:16 -05002519
Chris Mason5f39d392007-10-15 16:14:19 -04002520fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002521 spin_lock(&tree->lru_lock);
2522 list_del_init(&eb->lru);
2523 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002524 if (!atomic_dec_and_test(&eb->refs))
2525 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002526 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002527 page_cache_release(extent_buffer_page(eb, index));
2528 }
Chris Mason0591fb52007-11-11 08:22:00 -05002529 if (i > 0)
2530 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002531 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002532 return NULL;
2533}
2534EXPORT_SYMBOL(alloc_extent_buffer);
2535
2536struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
2537 u64 start, unsigned long len,
2538 gfp_t mask)
2539{
Chris Masondb945352007-10-15 16:15:53 -04002540 unsigned long num_pages = num_extent_pages(start, len);
Chris Mason09be2072007-11-07 21:08:16 -05002541 unsigned long i;
2542 unsigned long index = start >> PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002543 struct extent_buffer *eb;
2544 struct page *p;
2545 struct address_space *mapping = tree->mapping;
Chris Mason14048ed2007-10-15 16:16:28 -04002546 int uptodate = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002547
Chris Mason4dc119042007-10-15 16:18:14 -04002548 eb = __alloc_extent_buffer(tree, start, len, mask);
Chris Mason5f39d392007-10-15 16:14:19 -04002549 if (!eb || IS_ERR(eb))
2550 return NULL;
2551
Chris Mason4dc119042007-10-15 16:18:14 -04002552 if (eb->flags & EXTENT_BUFFER_FILLED)
Chris Mason09be2072007-11-07 21:08:16 -05002553 goto lru_add;
Chris Mason5f39d392007-10-15 16:14:19 -04002554
2555 for (i = 0; i < num_pages; i++, index++) {
Chris Mason14048ed2007-10-15 16:16:28 -04002556 p = find_lock_page(mapping, index);
Chris Mason6d36dcd2007-10-15 16:14:37 -04002557 if (!p) {
Chris Mason5f39d392007-10-15 16:14:19 -04002558 goto fail;
Chris Mason6d36dcd2007-10-15 16:14:37 -04002559 }
Chris Masonf510cfe2007-10-15 16:14:48 -04002560 set_page_extent_mapped(p);
Chris Masonff79f812007-10-15 16:22:25 -04002561 mark_page_accessed(p);
Chris Mason19c00dd2007-10-15 16:19:22 -04002562
2563 if (i == 0) {
Chris Mason810191f2007-10-15 16:18:55 -04002564 eb->first_page = p;
Chris Mason19c00dd2007-10-15 16:19:22 -04002565 set_page_private(p, EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2566 len << 2);
2567 } else {
2568 set_page_private(p, EXTENT_PAGE_PRIVATE);
2569 }
2570
Chris Mason14048ed2007-10-15 16:16:28 -04002571 if (!PageUptodate(p))
2572 uptodate = 0;
2573 unlock_page(p);
Chris Mason5f39d392007-10-15 16:14:19 -04002574 }
Chris Mason14048ed2007-10-15 16:16:28 -04002575 if (uptodate)
2576 eb->flags |= EXTENT_UPTODATE;
Chris Mason4dc119042007-10-15 16:18:14 -04002577 eb->flags |= EXTENT_BUFFER_FILLED;
Chris Mason09be2072007-11-07 21:08:16 -05002578
2579lru_add:
2580 spin_lock(&tree->lru_lock);
2581 add_lru(tree, eb);
2582 spin_unlock(&tree->lru_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002583 return eb;
2584fail:
Chris Mason856bf3e2007-11-08 10:59:05 -05002585 spin_lock(&tree->lru_lock);
2586 list_del_init(&eb->lru);
2587 spin_unlock(&tree->lru_lock);
Chris Mason09be2072007-11-07 21:08:16 -05002588 if (!atomic_dec_and_test(&eb->refs))
2589 return NULL;
Chris Mason0591fb52007-11-11 08:22:00 -05002590 for (index = 1; index < i; index++) {
Chris Mason09be2072007-11-07 21:08:16 -05002591 page_cache_release(extent_buffer_page(eb, index));
2592 }
Chris Mason0591fb52007-11-11 08:22:00 -05002593 if (i > 0)
2594 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason09be2072007-11-07 21:08:16 -05002595 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002596 return NULL;
2597}
2598EXPORT_SYMBOL(find_extent_buffer);
2599
2600void free_extent_buffer(struct extent_buffer *eb)
2601{
2602 unsigned long i;
2603 unsigned long num_pages;
2604
2605 if (!eb)
2606 return;
2607
2608 if (!atomic_dec_and_test(&eb->refs))
2609 return;
2610
Chris Mason0591fb52007-11-11 08:22:00 -05002611 WARN_ON(!list_empty(&eb->lru));
Chris Masondb945352007-10-15 16:15:53 -04002612 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002613
Chris Mason0591fb52007-11-11 08:22:00 -05002614 for (i = 1; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002615 page_cache_release(extent_buffer_page(eb, i));
Chris Mason5f39d392007-10-15 16:14:19 -04002616 }
Chris Mason0591fb52007-11-11 08:22:00 -05002617 page_cache_release(extent_buffer_page(eb, 0));
Chris Mason6d36dcd2007-10-15 16:14:37 -04002618 __free_extent_buffer(eb);
Chris Mason5f39d392007-10-15 16:14:19 -04002619}
2620EXPORT_SYMBOL(free_extent_buffer);
2621
2622int clear_extent_buffer_dirty(struct extent_map_tree *tree,
2623 struct extent_buffer *eb)
2624{
2625 int set;
2626 unsigned long i;
2627 unsigned long num_pages;
2628 struct page *page;
2629
2630 u64 start = eb->start;
2631 u64 end = start + eb->len - 1;
2632
2633 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
Chris Masondb945352007-10-15 16:15:53 -04002634 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002635
2636 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002637 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002638 lock_page(page);
2639 /*
2640 * if we're on the last page or the first page and the
2641 * block isn't aligned on a page boundary, do extra checks
2642 * to make sure we don't clean page that is partially dirty
2643 */
2644 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2645 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002646 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason35ebb932007-10-30 16:56:53 -04002647 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -04002648 end = start + PAGE_CACHE_SIZE - 1;
2649 if (test_range_bit(tree, start, end,
2650 EXTENT_DIRTY, 0)) {
2651 unlock_page(page);
2652 continue;
2653 }
2654 }
2655 clear_page_dirty_for_io(page);
Chris Mason7073c8e2007-11-20 13:44:45 -05002656 write_lock_irq(&page->mapping->tree_lock);
2657 if (!PageDirty(page)) {
2658 radix_tree_tag_clear(&page->mapping->page_tree,
2659 page_index(page),
2660 PAGECACHE_TAG_DIRTY);
2661 }
2662 write_unlock_irq(&page->mapping->tree_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04002663 unlock_page(page);
2664 }
2665 return 0;
2666}
2667EXPORT_SYMBOL(clear_extent_buffer_dirty);
2668
2669int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
2670 struct extent_buffer *eb)
2671{
2672 return wait_on_extent_writeback(tree, eb->start,
2673 eb->start + eb->len - 1);
2674}
2675EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2676
2677int set_extent_buffer_dirty(struct extent_map_tree *tree,
2678 struct extent_buffer *eb)
2679{
Chris Mason810191f2007-10-15 16:18:55 -04002680 unsigned long i;
2681 unsigned long num_pages;
2682
2683 num_pages = num_extent_pages(eb->start, eb->len);
2684 for (i = 0; i < num_pages; i++) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002685 struct page *page = extent_buffer_page(eb, i);
2686 /* writepage may need to do something special for the
2687 * first page, we have to make sure page->private is
2688 * properly set. releasepage may drop page->private
2689 * on us if the page isn't already dirty.
2690 */
2691 if (i == 0) {
2692 lock_page(page);
2693 set_page_private(page,
2694 EXTENT_PAGE_PRIVATE_FIRST_PAGE |
2695 eb->len << 2);
2696 }
Chris Mason810191f2007-10-15 16:18:55 -04002697 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Mason19c00dd2007-10-15 16:19:22 -04002698 if (i == 0)
2699 unlock_page(page);
Chris Mason810191f2007-10-15 16:18:55 -04002700 }
2701 return set_extent_dirty(tree, eb->start,
2702 eb->start + eb->len - 1, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002703}
2704EXPORT_SYMBOL(set_extent_buffer_dirty);
2705
2706int set_extent_buffer_uptodate(struct extent_map_tree *tree,
2707 struct extent_buffer *eb)
2708{
2709 unsigned long i;
2710 struct page *page;
2711 unsigned long num_pages;
2712
Chris Masondb945352007-10-15 16:15:53 -04002713 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002714
2715 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2716 GFP_NOFS);
2717 for (i = 0; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002718 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002719 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2720 ((i == num_pages - 1) &&
Yan65555a02007-10-25 15:42:57 -04002721 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
Chris Mason5f39d392007-10-15 16:14:19 -04002722 check_page_uptodate(tree, page);
2723 continue;
2724 }
2725 SetPageUptodate(page);
2726 }
2727 return 0;
2728}
2729EXPORT_SYMBOL(set_extent_buffer_uptodate);
2730
2731int extent_buffer_uptodate(struct extent_map_tree *tree,
2732 struct extent_buffer *eb)
2733{
2734 if (eb->flags & EXTENT_UPTODATE)
2735 return 1;
2736 return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
2737 EXTENT_UPTODATE, 1);
2738}
2739EXPORT_SYMBOL(extent_buffer_uptodate);
2740
2741int read_extent_buffer_pages(struct extent_map_tree *tree,
Chris Mason19c00dd2007-10-15 16:19:22 -04002742 struct extent_buffer *eb,
2743 u64 start,
2744 int wait)
Chris Mason5f39d392007-10-15 16:14:19 -04002745{
2746 unsigned long i;
Chris Mason19c00dd2007-10-15 16:19:22 -04002747 unsigned long start_i;
Chris Mason5f39d392007-10-15 16:14:19 -04002748 struct page *page;
2749 int err;
2750 int ret = 0;
2751 unsigned long num_pages;
2752
2753 if (eb->flags & EXTENT_UPTODATE)
2754 return 0;
2755
Chris Mason14048ed2007-10-15 16:16:28 -04002756 if (0 && test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason5f39d392007-10-15 16:14:19 -04002757 EXTENT_UPTODATE, 1)) {
2758 return 0;
2759 }
Chris Mason0591fb52007-11-11 08:22:00 -05002760
Chris Mason19c00dd2007-10-15 16:19:22 -04002761 if (start) {
2762 WARN_ON(start < eb->start);
2763 start_i = (start >> PAGE_CACHE_SHIFT) -
2764 (eb->start >> PAGE_CACHE_SHIFT);
2765 } else {
2766 start_i = 0;
2767 }
Chris Mason5f39d392007-10-15 16:14:19 -04002768
Chris Masondb945352007-10-15 16:15:53 -04002769 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason19c00dd2007-10-15 16:19:22 -04002770 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002771 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002772 if (PageUptodate(page)) {
2773 continue;
2774 }
2775 if (!wait) {
2776 if (TestSetPageLocked(page)) {
2777 continue;
2778 }
2779 } else {
2780 lock_page(page);
2781 }
2782 if (!PageUptodate(page)) {
2783 err = page->mapping->a_ops->readpage(NULL, page);
2784 if (err) {
2785 ret = err;
2786 }
2787 } else {
2788 unlock_page(page);
2789 }
2790 }
2791
2792 if (ret || !wait) {
2793 return ret;
2794 }
2795
Chris Mason19c00dd2007-10-15 16:19:22 -04002796 for (i = start_i; i < num_pages; i++) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002797 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002798 wait_on_page_locked(page);
2799 if (!PageUptodate(page)) {
2800 ret = -EIO;
2801 }
2802 }
Chris Mason4dc119042007-10-15 16:18:14 -04002803 if (!ret)
2804 eb->flags |= EXTENT_UPTODATE;
Chris Mason5f39d392007-10-15 16:14:19 -04002805 return ret;
2806}
2807EXPORT_SYMBOL(read_extent_buffer_pages);
2808
2809void read_extent_buffer(struct extent_buffer *eb, void *dstv,
2810 unsigned long start,
2811 unsigned long len)
2812{
2813 size_t cur;
2814 size_t offset;
2815 struct page *page;
2816 char *kaddr;
2817 char *dst = (char *)dstv;
2818 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2819 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Mason14048ed2007-10-15 16:16:28 -04002820 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason5f39d392007-10-15 16:14:19 -04002821
2822 WARN_ON(start > eb->len);
2823 WARN_ON(start + len > eb->start + eb->len);
2824
Chris Mason3685f792007-10-19 09:23:27 -04002825 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002826
2827 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002828 page = extent_buffer_page(eb, i);
Chris Mason14048ed2007-10-15 16:16:28 -04002829 if (!PageUptodate(page)) {
2830 printk("page %lu not up to date i %lu, total %lu, len %lu\n", page->index, i, num_pages, eb->len);
2831 WARN_ON(1);
2832 }
Chris Mason5f39d392007-10-15 16:14:19 -04002833 WARN_ON(!PageUptodate(page));
2834
2835 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Mason59d169e2007-10-19 09:23:09 -04002836 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002837 memcpy(dst, kaddr + offset, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002838 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002839
2840 dst += cur;
2841 len -= cur;
2842 offset = 0;
2843 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002844 }
2845}
2846EXPORT_SYMBOL(read_extent_buffer);
2847
Chris Mason19c00dd2007-10-15 16:19:22 -04002848int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masondb945352007-10-15 16:15:53 -04002849 unsigned long min_len, char **token, char **map,
2850 unsigned long *map_start,
2851 unsigned long *map_len, int km)
Chris Mason5f39d392007-10-15 16:14:19 -04002852{
Chris Mason479965d2007-10-15 16:14:27 -04002853 size_t offset = start & (PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002854 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002855 struct page *p;
Chris Mason5f39d392007-10-15 16:14:19 -04002856 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2857 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Yan65555a02007-10-25 15:42:57 -04002858 unsigned long end_i = (start_offset + start + min_len - 1) >>
Chris Mason810191f2007-10-15 16:18:55 -04002859 PAGE_CACHE_SHIFT;
Chris Mason479965d2007-10-15 16:14:27 -04002860
2861 if (i != end_i)
2862 return -EINVAL;
Chris Mason5f39d392007-10-15 16:14:19 -04002863
Chris Mason5f39d392007-10-15 16:14:19 -04002864 if (i == 0) {
2865 offset = start_offset;
2866 *map_start = 0;
2867 } else {
Chris Masondb945352007-10-15 16:15:53 -04002868 offset = 0;
Chris Mason0591fb52007-11-11 08:22:00 -05002869 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
Chris Mason5f39d392007-10-15 16:14:19 -04002870 }
Yan65555a02007-10-25 15:42:57 -04002871 if (start + min_len > eb->len) {
Chris Mason19c00dd2007-10-15 16:19:22 -04002872printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
2873 WARN_ON(1);
2874 }
Chris Mason5f39d392007-10-15 16:14:19 -04002875
Chris Masondb945352007-10-15 16:15:53 -04002876 p = extent_buffer_page(eb, i);
2877 WARN_ON(!PageUptodate(p));
2878 kaddr = kmap_atomic(p, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002879 *token = kaddr;
2880 *map = kaddr + offset;
2881 *map_len = PAGE_CACHE_SIZE - offset;
2882 return 0;
2883}
Chris Mason19c00dd2007-10-15 16:19:22 -04002884EXPORT_SYMBOL(map_private_extent_buffer);
Chris Masondb945352007-10-15 16:15:53 -04002885
2886int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
2887 unsigned long min_len,
2888 char **token, char **map,
2889 unsigned long *map_start,
2890 unsigned long *map_len, int km)
2891{
2892 int err;
2893 int save = 0;
2894 if (eb->map_token) {
Chris Masondb945352007-10-15 16:15:53 -04002895 unmap_extent_buffer(eb, eb->map_token, km);
2896 eb->map_token = NULL;
2897 save = 1;
2898 }
Chris Mason19c00dd2007-10-15 16:19:22 -04002899 err = map_private_extent_buffer(eb, start, min_len, token, map,
2900 map_start, map_len, km);
Chris Masondb945352007-10-15 16:15:53 -04002901 if (!err && save) {
2902 eb->map_token = *token;
2903 eb->kaddr = *map;
2904 eb->map_start = *map_start;
2905 eb->map_len = *map_len;
2906 }
2907 return err;
2908}
Chris Mason5f39d392007-10-15 16:14:19 -04002909EXPORT_SYMBOL(map_extent_buffer);
2910
2911void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
2912{
Chris Masonae5252b2007-10-15 16:14:41 -04002913 kunmap_atomic(token, km);
Chris Mason5f39d392007-10-15 16:14:19 -04002914}
2915EXPORT_SYMBOL(unmap_extent_buffer);
2916
2917int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
2918 unsigned long start,
2919 unsigned long len)
2920{
2921 size_t cur;
2922 size_t offset;
2923 struct page *page;
2924 char *kaddr;
2925 char *ptr = (char *)ptrv;
2926 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2927 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2928 int ret = 0;
2929
2930 WARN_ON(start > eb->len);
2931 WARN_ON(start + len > eb->start + eb->len);
2932
Chris Mason3685f792007-10-19 09:23:27 -04002933 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002934
2935 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002936 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002937 WARN_ON(!PageUptodate(page));
2938
2939 cur = min(len, (PAGE_CACHE_SIZE - offset));
2940
Chris Masonae5252b2007-10-15 16:14:41 -04002941 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002942 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04002943 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04002944 if (ret)
2945 break;
2946
2947 ptr += cur;
2948 len -= cur;
2949 offset = 0;
2950 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002951 }
2952 return ret;
2953}
2954EXPORT_SYMBOL(memcmp_extent_buffer);
2955
2956void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
2957 unsigned long start, unsigned long len)
2958{
2959 size_t cur;
2960 size_t offset;
2961 struct page *page;
2962 char *kaddr;
2963 char *src = (char *)srcv;
2964 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2965 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2966
2967 WARN_ON(start > eb->len);
2968 WARN_ON(start + len > eb->start + eb->len);
2969
Chris Mason3685f792007-10-19 09:23:27 -04002970 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002971
2972 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04002973 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04002974 WARN_ON(!PageUptodate(page));
2975
2976 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Mason59d169e2007-10-19 09:23:09 -04002977 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002978 memcpy(kaddr + offset, src, cur);
Chris Mason59d169e2007-10-19 09:23:09 -04002979 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002980
2981 src += cur;
2982 len -= cur;
2983 offset = 0;
2984 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04002985 }
2986}
2987EXPORT_SYMBOL(write_extent_buffer);
2988
2989void memset_extent_buffer(struct extent_buffer *eb, char c,
2990 unsigned long start, unsigned long len)
2991{
2992 size_t cur;
2993 size_t offset;
2994 struct page *page;
2995 char *kaddr;
2996 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
2997 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
2998
2999 WARN_ON(start > eb->len);
3000 WARN_ON(start + len > eb->start + eb->len);
3001
Chris Mason3685f792007-10-19 09:23:27 -04003002 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003003
3004 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04003005 page = extent_buffer_page(eb, i);
Chris Mason5f39d392007-10-15 16:14:19 -04003006 WARN_ON(!PageUptodate(page));
3007
3008 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masonae5252b2007-10-15 16:14:41 -04003009 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003010 memset(kaddr + offset, c, cur);
Chris Masonae5252b2007-10-15 16:14:41 -04003011 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003012
3013 len -= cur;
3014 offset = 0;
3015 i++;
Chris Mason5f39d392007-10-15 16:14:19 -04003016 }
3017}
3018EXPORT_SYMBOL(memset_extent_buffer);
3019
3020void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3021 unsigned long dst_offset, unsigned long src_offset,
3022 unsigned long len)
3023{
3024 u64 dst_len = dst->len;
3025 size_t cur;
3026 size_t offset;
3027 struct page *page;
3028 char *kaddr;
3029 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3030 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3031
3032 WARN_ON(src->len != dst_len);
3033
Chris Mason3685f792007-10-19 09:23:27 -04003034 offset = (start_offset + dst_offset) &
3035 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003036
3037 while(len > 0) {
Chris Mason6d36dcd2007-10-15 16:14:37 -04003038 page = extent_buffer_page(dst, i);
Chris Mason5f39d392007-10-15 16:14:19 -04003039 WARN_ON(!PageUptodate(page));
3040
3041 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3042
Chris Masonff190c02007-10-19 10:39:41 -04003043 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003044 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masonff190c02007-10-19 10:39:41 -04003045 kunmap_atomic(kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003046
3047 src_offset += cur;
3048 len -= cur;
3049 offset = 0;
3050 i++;
3051 }
3052}
3053EXPORT_SYMBOL(copy_extent_buffer);
3054
3055static void move_pages(struct page *dst_page, struct page *src_page,
3056 unsigned long dst_off, unsigned long src_off,
3057 unsigned long len)
3058{
Chris Masonae5252b2007-10-15 16:14:41 -04003059 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003060 if (dst_page == src_page) {
3061 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3062 } else {
Chris Masonae5252b2007-10-15 16:14:41 -04003063 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04003064 char *p = dst_kaddr + dst_off + len;
3065 char *s = src_kaddr + src_off + len;
3066
3067 while (len--)
3068 *--p = *--s;
3069
Chris Masonae5252b2007-10-15 16:14:41 -04003070 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04003071 }
Chris Masonae5252b2007-10-15 16:14:41 -04003072 kunmap_atomic(dst_kaddr, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003073}
3074
3075static void copy_pages(struct page *dst_page, struct page *src_page,
3076 unsigned long dst_off, unsigned long src_off,
3077 unsigned long len)
3078{
Chris Masonae5252b2007-10-15 16:14:41 -04003079 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -04003080 char *src_kaddr;
3081
3082 if (dst_page != src_page)
Chris Masonae5252b2007-10-15 16:14:41 -04003083 src_kaddr = kmap_atomic(src_page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04003084 else
3085 src_kaddr = dst_kaddr;
3086
3087 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Mason5f39d392007-10-15 16:14:19 -04003088 kunmap_atomic(dst_kaddr, KM_USER0);
3089 if (dst_page != src_page)
3090 kunmap_atomic(src_kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04003091}
3092
3093void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3094 unsigned long src_offset, unsigned long len)
3095{
3096 size_t cur;
3097 size_t dst_off_in_page;
3098 size_t src_off_in_page;
3099 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3100 unsigned long dst_i;
3101 unsigned long src_i;
3102
3103 if (src_offset + len > dst->len) {
3104 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3105 src_offset, len, dst->len);
3106 BUG_ON(1);
3107 }
3108 if (dst_offset + len > dst->len) {
3109 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3110 dst_offset, len, dst->len);
3111 BUG_ON(1);
3112 }
3113
3114 while(len > 0) {
Chris Mason3685f792007-10-19 09:23:27 -04003115 dst_off_in_page = (start_offset + dst_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04003116 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04003117 src_off_in_page = (start_offset + src_offset) &
Chris Mason5f39d392007-10-15 16:14:19 -04003118 ((unsigned long)PAGE_CACHE_SIZE - 1);
3119
3120 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3121 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3122
Chris Mason5f39d392007-10-15 16:14:19 -04003123 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3124 src_off_in_page));
Jens Axboeae2f5412007-10-19 09:22:59 -04003125 cur = min_t(unsigned long, cur,
3126 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
Chris Mason5f39d392007-10-15 16:14:19 -04003127
Chris Mason6d36dcd2007-10-15 16:14:37 -04003128 copy_pages(extent_buffer_page(dst, dst_i),
3129 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04003130 dst_off_in_page, src_off_in_page, cur);
3131
3132 src_offset += cur;
3133 dst_offset += cur;
3134 len -= cur;
3135 }
3136}
3137EXPORT_SYMBOL(memcpy_extent_buffer);
3138
3139void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3140 unsigned long src_offset, unsigned long len)
3141{
3142 size_t cur;
3143 size_t dst_off_in_page;
3144 size_t src_off_in_page;
3145 unsigned long dst_end = dst_offset + len - 1;
3146 unsigned long src_end = src_offset + len - 1;
3147 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3148 unsigned long dst_i;
3149 unsigned long src_i;
3150
3151 if (src_offset + len > dst->len) {
3152 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3153 src_offset, len, dst->len);
3154 BUG_ON(1);
3155 }
3156 if (dst_offset + len > dst->len) {
3157 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3158 dst_offset, len, dst->len);
3159 BUG_ON(1);
3160 }
3161 if (dst_offset < src_offset) {
3162 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3163 return;
3164 }
3165 while(len > 0) {
3166 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3167 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3168
Chris Mason3685f792007-10-19 09:23:27 -04003169 dst_off_in_page = (start_offset + dst_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04003170 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason3685f792007-10-19 09:23:27 -04003171 src_off_in_page = (start_offset + src_end) &
Chris Mason5f39d392007-10-15 16:14:19 -04003172 ((unsigned long)PAGE_CACHE_SIZE - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003173
Jens Axboeae2f5412007-10-19 09:22:59 -04003174 cur = min_t(unsigned long, len, src_off_in_page + 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003175 cur = min(cur, dst_off_in_page + 1);
Chris Mason6d36dcd2007-10-15 16:14:37 -04003176 move_pages(extent_buffer_page(dst, dst_i),
3177 extent_buffer_page(dst, src_i),
Chris Mason5f39d392007-10-15 16:14:19 -04003178 dst_off_in_page - cur + 1,
3179 src_off_in_page - cur + 1, cur);
3180
Chris Masondb945352007-10-15 16:15:53 -04003181 dst_end -= cur;
3182 src_end -= cur;
Chris Mason5f39d392007-10-15 16:14:19 -04003183 len -= cur;
3184 }
3185}
3186EXPORT_SYMBOL(memmove_extent_buffer);