blob: 17c508a941eb36de2e8d2471d40107ea16cf51f0 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#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>
11#include <linux/swap.h>
12#include <linux/version.h>
13#include <linux/writeback.h>
14#include <linux/pagevec.h>
15#include "extent_io.h"
16#include "extent_map.h"
17
18/* temporary define until extent_map moves out of btrfs */
19struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
20 unsigned long extra_flags,
21 void (*ctor)(void *, struct kmem_cache *,
22 unsigned long));
23
24static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
27static LIST_HEAD(buffers);
28static LIST_HEAD(states);
Chris Mason2d2ae542008-03-26 16:24:23 -040029static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
Chris Masond1310b22008-01-24 16:13:08 -050030
Chris Masond1310b22008-01-24 16:13:08 -050031#define BUFFER_LRU_MAX 64
32
33struct tree_entry {
34 u64 start;
35 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050036 struct rb_node rb_node;
37};
38
39struct extent_page_data {
40 struct bio *bio;
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
43};
44
45int __init extent_io_init(void)
46{
47 extent_state_cache = btrfs_cache_create("extent_state",
48 sizeof(struct extent_state), 0,
49 NULL);
50 if (!extent_state_cache)
51 return -ENOMEM;
52
53 extent_buffer_cache = btrfs_cache_create("extent_buffers",
54 sizeof(struct extent_buffer), 0,
55 NULL);
56 if (!extent_buffer_cache)
57 goto free_state_cache;
58 return 0;
59
60free_state_cache:
61 kmem_cache_destroy(extent_state_cache);
62 return -ENOMEM;
63}
64
65void extent_io_exit(void)
66{
67 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040068 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050069
70 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040071 state = list_entry(states.next, struct extent_state, leak_list);
Chris Mason70dec802008-01-29 09:59:12 -050072 printk("state leak: start %Lu end %Lu state %lu in tree %p refs %d\n", state->start, state->end, state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040073 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050074 kmem_cache_free(extent_state_cache, state);
75
76 }
77
Chris Mason2d2ae542008-03-26 16:24:23 -040078 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
80 printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs));
81 list_del(&eb->leak_list);
82 kmem_cache_free(extent_buffer_cache, eb);
83 }
Chris Masond1310b22008-01-24 16:13:08 -050084 if (extent_state_cache)
85 kmem_cache_destroy(extent_state_cache);
86 if (extent_buffer_cache)
87 kmem_cache_destroy(extent_buffer_cache);
88}
89
90void extent_io_tree_init(struct extent_io_tree *tree,
91 struct address_space *mapping, gfp_t mask)
92{
93 tree->state.rb_node = NULL;
94 tree->ops = NULL;
95 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -050096 spin_lock_init(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -050097 spin_lock_init(&tree->lru_lock);
98 tree->mapping = mapping;
99 INIT_LIST_HEAD(&tree->buffer_lru);
100 tree->lru_size = 0;
Chris Mason80ea96b2008-02-01 14:51:59 -0500101 tree->last = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500102}
103EXPORT_SYMBOL(extent_io_tree_init);
104
105void extent_io_tree_empty_lru(struct extent_io_tree *tree)
106{
107 struct extent_buffer *eb;
108 while(!list_empty(&tree->buffer_lru)) {
109 eb = list_entry(tree->buffer_lru.next, struct extent_buffer,
110 lru);
111 list_del_init(&eb->lru);
112 free_extent_buffer(eb);
113 }
114}
115EXPORT_SYMBOL(extent_io_tree_empty_lru);
116
117struct extent_state *alloc_extent_state(gfp_t mask)
118{
119 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -0400120 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason2d2ae542008-03-26 16:24:23 -0400128 spin_lock_irqsave(&leak_lock, flags);
129 list_add(&state->leak_list, &states);
130 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500131
132 atomic_set(&state->refs, 1);
133 init_waitqueue_head(&state->wq);
134 return state;
135}
136EXPORT_SYMBOL(alloc_extent_state);
137
138void free_extent_state(struct extent_state *state)
139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason2d2ae542008-03-26 16:24:23 -0400143 unsigned long flags;
Chris Mason70dec802008-01-29 09:59:12 -0500144 WARN_ON(state->tree);
Chris Mason2d2ae542008-03-26 16:24:23 -0400145 spin_lock_irqsave(&leak_lock, flags);
146 list_del(&state->leak_list);
147 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500148 kmem_cache_free(extent_state_cache, state);
149 }
150}
151EXPORT_SYMBOL(free_extent_state);
152
153static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
154 struct rb_node *node)
155{
156 struct rb_node ** p = &root->rb_node;
157 struct rb_node * parent = NULL;
158 struct tree_entry *entry;
159
160 while(*p) {
161 parent = *p;
162 entry = rb_entry(parent, struct tree_entry, rb_node);
163
164 if (offset < entry->start)
165 p = &(*p)->rb_left;
166 else if (offset > entry->end)
167 p = &(*p)->rb_right;
168 else
169 return parent;
170 }
171
172 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500173 rb_link_node(node, parent, p);
174 rb_insert_color(node, root);
175 return NULL;
176}
177
Chris Mason80ea96b2008-02-01 14:51:59 -0500178static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500179 struct rb_node **prev_ret,
180 struct rb_node **next_ret)
181{
Chris Mason80ea96b2008-02-01 14:51:59 -0500182 struct rb_root *root = &tree->state;
Chris Masond1310b22008-01-24 16:13:08 -0500183 struct rb_node * n = root->rb_node;
184 struct rb_node *prev = NULL;
185 struct rb_node *orig_prev = NULL;
186 struct tree_entry *entry;
187 struct tree_entry *prev_entry = NULL;
188
Chris Mason80ea96b2008-02-01 14:51:59 -0500189 if (tree->last) {
190 struct extent_state *state;
191 state = tree->last;
192 if (state->start <= offset && offset <= state->end)
193 return &tree->last->rb_node;
194 }
Chris Masond1310b22008-01-24 16:13:08 -0500195 while(n) {
196 entry = rb_entry(n, struct tree_entry, rb_node);
197 prev = n;
198 prev_entry = entry;
199
200 if (offset < entry->start)
201 n = n->rb_left;
202 else if (offset > entry->end)
203 n = n->rb_right;
Chris Mason80ea96b2008-02-01 14:51:59 -0500204 else {
205 tree->last = rb_entry(n, struct extent_state, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500206 return n;
Chris Mason80ea96b2008-02-01 14:51:59 -0500207 }
Chris Masond1310b22008-01-24 16:13:08 -0500208 }
209
210 if (prev_ret) {
211 orig_prev = prev;
212 while(prev && offset > prev_entry->end) {
213 prev = rb_next(prev);
214 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
215 }
216 *prev_ret = prev;
217 prev = orig_prev;
218 }
219
220 if (next_ret) {
221 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
222 while(prev && offset < prev_entry->start) {
223 prev = rb_prev(prev);
224 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
225 }
226 *next_ret = prev;
227 }
228 return NULL;
229}
230
Chris Mason80ea96b2008-02-01 14:51:59 -0500231static inline struct rb_node *tree_search(struct extent_io_tree *tree,
232 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500233{
Chris Mason70dec802008-01-29 09:59:12 -0500234 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500235 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500236
Chris Mason80ea96b2008-02-01 14:51:59 -0500237 ret = __etree_search(tree, offset, &prev, NULL);
238 if (!ret) {
239 if (prev) {
240 tree->last = rb_entry(prev, struct extent_state,
241 rb_node);
242 }
Chris Masond1310b22008-01-24 16:13:08 -0500243 return prev;
Chris Mason80ea96b2008-02-01 14:51:59 -0500244 }
Chris Masond1310b22008-01-24 16:13:08 -0500245 return ret;
246}
247
248/*
249 * utility function to look for merge candidates inside a given range.
250 * Any extents with matching state are merged together into a single
251 * extent in the tree. Extents with EXTENT_IO in their state field
252 * are not merged because the end_io handlers need to be able to do
253 * operations on them without sleeping (or doing allocations/splits).
254 *
255 * This should be called with the tree lock held.
256 */
257static int merge_state(struct extent_io_tree *tree,
258 struct extent_state *state)
259{
260 struct extent_state *other;
261 struct rb_node *other_node;
262
263 if (state->state & EXTENT_IOBITS)
264 return 0;
265
266 other_node = rb_prev(&state->rb_node);
267 if (other_node) {
268 other = rb_entry(other_node, struct extent_state, rb_node);
269 if (other->end == state->start - 1 &&
270 other->state == state->state) {
271 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500272 other->tree = NULL;
Chris Mason80ea96b2008-02-01 14:51:59 -0500273 if (tree->last == other)
Chris Masond7fc6402008-02-18 12:12:38 -0500274 tree->last = state;
Chris Masond1310b22008-01-24 16:13:08 -0500275 rb_erase(&other->rb_node, &tree->state);
276 free_extent_state(other);
277 }
278 }
279 other_node = rb_next(&state->rb_node);
280 if (other_node) {
281 other = rb_entry(other_node, struct extent_state, rb_node);
282 if (other->start == state->end + 1 &&
283 other->state == state->state) {
284 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500285 state->tree = NULL;
Chris Mason80ea96b2008-02-01 14:51:59 -0500286 if (tree->last == state)
Chris Masond7fc6402008-02-18 12:12:38 -0500287 tree->last = other;
Chris Masond1310b22008-01-24 16:13:08 -0500288 rb_erase(&state->rb_node, &tree->state);
289 free_extent_state(state);
290 }
291 }
292 return 0;
293}
294
Chris Mason291d6732008-01-29 15:55:23 -0500295static void set_state_cb(struct extent_io_tree *tree,
296 struct extent_state *state,
297 unsigned long bits)
298{
299 if (tree->ops && tree->ops->set_bit_hook) {
300 tree->ops->set_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500301 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500302 }
303}
304
305static void clear_state_cb(struct extent_io_tree *tree,
306 struct extent_state *state,
307 unsigned long bits)
308{
309 if (tree->ops && tree->ops->set_bit_hook) {
310 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500311 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500312 }
313}
314
Chris Masond1310b22008-01-24 16:13:08 -0500315/*
316 * insert an extent_state struct into the tree. 'bits' are set on the
317 * struct before it is inserted.
318 *
319 * This may return -EEXIST if the extent is already there, in which case the
320 * state struct is freed.
321 *
322 * The tree lock is not taken internally. This is a utility function and
323 * probably isn't what you want to call (see set/clear_extent_bit).
324 */
325static int insert_state(struct extent_io_tree *tree,
326 struct extent_state *state, u64 start, u64 end,
327 int bits)
328{
329 struct rb_node *node;
330
331 if (end < start) {
332 printk("end < start %Lu %Lu\n", end, start);
333 WARN_ON(1);
334 }
335 if (bits & EXTENT_DIRTY)
336 tree->dirty_bytes += end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500337 set_state_cb(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500338 state->state |= bits;
339 state->start = start;
340 state->end = end;
341 node = tree_insert(&tree->state, end, &state->rb_node);
342 if (node) {
343 struct extent_state *found;
344 found = rb_entry(node, struct extent_state, rb_node);
345 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
346 free_extent_state(state);
347 return -EEXIST;
348 }
Chris Mason70dec802008-01-29 09:59:12 -0500349 state->tree = tree;
Chris Mason80ea96b2008-02-01 14:51:59 -0500350 tree->last = state;
Chris Masond1310b22008-01-24 16:13:08 -0500351 merge_state(tree, state);
352 return 0;
353}
354
355/*
356 * split a given extent state struct in two, inserting the preallocated
357 * struct 'prealloc' as the newly created second half. 'split' indicates an
358 * offset inside 'orig' where it should be split.
359 *
360 * Before calling,
361 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
362 * are two extent state structs in the tree:
363 * prealloc: [orig->start, split - 1]
364 * orig: [ split, orig->end ]
365 *
366 * The tree locks are not taken by this function. They need to be held
367 * by the caller.
368 */
369static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
370 struct extent_state *prealloc, u64 split)
371{
372 struct rb_node *node;
373 prealloc->start = orig->start;
374 prealloc->end = split - 1;
375 prealloc->state = orig->state;
376 orig->start = split;
377
378 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
379 if (node) {
380 struct extent_state *found;
381 found = rb_entry(node, struct extent_state, rb_node);
382 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
383 free_extent_state(prealloc);
384 return -EEXIST;
385 }
Chris Mason70dec802008-01-29 09:59:12 -0500386 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500387 return 0;
388}
389
390/*
391 * utility function to clear some bits in an extent state struct.
392 * it will optionally wake up any one waiting on this state (wake == 1), or
393 * forcibly remove the state from the tree (delete == 1).
394 *
395 * If no bits are set on the state struct after clearing things, the
396 * struct is freed and removed from the tree
397 */
398static int clear_state_bit(struct extent_io_tree *tree,
399 struct extent_state *state, int bits, int wake,
400 int delete)
401{
402 int ret = state->state & bits;
403
404 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
405 u64 range = state->end - state->start + 1;
406 WARN_ON(range > tree->dirty_bytes);
407 tree->dirty_bytes -= range;
408 }
Chris Mason291d6732008-01-29 15:55:23 -0500409 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500410 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500411 if (wake)
412 wake_up(&state->wq);
413 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500414 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500415 clear_state_cb(tree, state, state->state);
Chris Masond7fc6402008-02-18 12:12:38 -0500416 if (tree->last == state) {
417 tree->last = extent_state_next(state);
418 }
Chris Masond1310b22008-01-24 16:13:08 -0500419 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500420 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500421 free_extent_state(state);
422 } else {
423 WARN_ON(1);
424 }
425 } else {
426 merge_state(tree, state);
427 }
428 return ret;
429}
430
431/*
432 * clear some bits on a range in the tree. This may require splitting
433 * or inserting elements in the tree, so the gfp mask is used to
434 * indicate which allocations or sleeping are allowed.
435 *
436 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
437 * the given range from the tree regardless of state (ie for truncate).
438 *
439 * the range [start, end] is inclusive.
440 *
441 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
442 * bits were already set, or zero if none of the bits were already set.
443 */
444int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
445 int bits, int wake, int delete, gfp_t mask)
446{
447 struct extent_state *state;
448 struct extent_state *prealloc = NULL;
449 struct rb_node *node;
450 unsigned long flags;
451 int err;
452 int set = 0;
453
454again:
455 if (!prealloc && (mask & __GFP_WAIT)) {
456 prealloc = alloc_extent_state(mask);
457 if (!prealloc)
458 return -ENOMEM;
459 }
460
Chris Mason70dec802008-01-29 09:59:12 -0500461 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500462 /*
463 * this search will find the extents that end after
464 * our range starts
465 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500466 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500467 if (!node)
468 goto out;
469 state = rb_entry(node, struct extent_state, rb_node);
470 if (state->start > end)
471 goto out;
472 WARN_ON(state->end < start);
473
474 /*
475 * | ---- desired range ---- |
476 * | state | or
477 * | ------------- state -------------- |
478 *
479 * We need to split the extent we found, and may flip
480 * bits on second half.
481 *
482 * If the extent we found extends past our range, we
483 * just split and search again. It'll get split again
484 * the next time though.
485 *
486 * If the extent we found is inside our range, we clear
487 * the desired bit on it.
488 */
489
490 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500491 if (!prealloc)
492 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500493 err = split_state(tree, state, prealloc, start);
494 BUG_ON(err == -EEXIST);
495 prealloc = NULL;
496 if (err)
497 goto out;
498 if (state->end <= end) {
499 start = state->end + 1;
500 set |= clear_state_bit(tree, state, bits,
501 wake, delete);
502 } else {
503 start = state->start;
504 }
505 goto search_again;
506 }
507 /*
508 * | ---- desired range ---- |
509 * | state |
510 * We need to split the extent, and clear the bit
511 * on the first half
512 */
513 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500514 if (!prealloc)
515 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500516 err = split_state(tree, state, prealloc, end + 1);
517 BUG_ON(err == -EEXIST);
518
519 if (wake)
520 wake_up(&state->wq);
521 set |= clear_state_bit(tree, prealloc, bits,
522 wake, delete);
523 prealloc = NULL;
524 goto out;
525 }
526
527 start = state->end + 1;
528 set |= clear_state_bit(tree, state, bits, wake, delete);
529 goto search_again;
530
531out:
Chris Mason70dec802008-01-29 09:59:12 -0500532 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500533 if (prealloc)
534 free_extent_state(prealloc);
535
536 return set;
537
538search_again:
539 if (start > end)
540 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500541 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500542 if (mask & __GFP_WAIT)
543 cond_resched();
544 goto again;
545}
546EXPORT_SYMBOL(clear_extent_bit);
547
548static int wait_on_state(struct extent_io_tree *tree,
549 struct extent_state *state)
550{
551 DEFINE_WAIT(wait);
552 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Mason70dec802008-01-29 09:59:12 -0500553 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500554 schedule();
Chris Mason70dec802008-01-29 09:59:12 -0500555 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500556 finish_wait(&state->wq, &wait);
557 return 0;
558}
559
560/*
561 * waits for one or more bits to clear on a range in the state tree.
562 * The range [start, end] is inclusive.
563 * The tree lock is taken by this function
564 */
565int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
566{
567 struct extent_state *state;
568 struct rb_node *node;
569
Chris Mason70dec802008-01-29 09:59:12 -0500570 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500571again:
572 while (1) {
573 /*
574 * this search will find all the extents that end after
575 * our range starts
576 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500577 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500578 if (!node)
579 break;
580
581 state = rb_entry(node, struct extent_state, rb_node);
582
583 if (state->start > end)
584 goto out;
585
586 if (state->state & bits) {
587 start = state->start;
588 atomic_inc(&state->refs);
589 wait_on_state(tree, state);
590 free_extent_state(state);
591 goto again;
592 }
593 start = state->end + 1;
594
595 if (start > end)
596 break;
597
598 if (need_resched()) {
Chris Mason70dec802008-01-29 09:59:12 -0500599 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500600 cond_resched();
Chris Mason70dec802008-01-29 09:59:12 -0500601 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500602 }
603 }
604out:
Chris Mason70dec802008-01-29 09:59:12 -0500605 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500606 return 0;
607}
608EXPORT_SYMBOL(wait_extent_bit);
609
610static void set_state_bits(struct extent_io_tree *tree,
611 struct extent_state *state,
612 int bits)
613{
614 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
615 u64 range = state->end - state->start + 1;
616 tree->dirty_bytes += range;
617 }
Chris Mason291d6732008-01-29 15:55:23 -0500618 set_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500619 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500620}
621
622/*
623 * set some bits on a range in the tree. This may require allocations
624 * or sleeping, so the gfp mask is used to indicate what is allowed.
625 *
626 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
627 * range already has the desired bits set. The start of the existing
628 * range is returned in failed_start in this case.
629 *
630 * [start, end] is inclusive
631 * This takes the tree lock.
632 */
633int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
634 int exclusive, u64 *failed_start, gfp_t mask)
635{
636 struct extent_state *state;
637 struct extent_state *prealloc = NULL;
638 struct rb_node *node;
639 unsigned long flags;
640 int err = 0;
641 int set;
642 u64 last_start;
643 u64 last_end;
644again:
645 if (!prealloc && (mask & __GFP_WAIT)) {
646 prealloc = alloc_extent_state(mask);
647 if (!prealloc)
648 return -ENOMEM;
649 }
650
Chris Mason70dec802008-01-29 09:59:12 -0500651 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500652 /*
653 * this search will find all the extents that end after
654 * our range starts.
655 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500656 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500657 if (!node) {
658 err = insert_state(tree, prealloc, start, end, bits);
659 prealloc = NULL;
660 BUG_ON(err == -EEXIST);
661 goto out;
662 }
663
664 state = rb_entry(node, struct extent_state, rb_node);
665 last_start = state->start;
666 last_end = state->end;
667
668 /*
669 * | ---- desired range ---- |
670 * | state |
671 *
672 * Just lock what we found and keep going
673 */
674 if (state->start == start && state->end <= end) {
675 set = state->state & bits;
676 if (set && exclusive) {
677 *failed_start = state->start;
678 err = -EEXIST;
679 goto out;
680 }
681 set_state_bits(tree, state, bits);
682 start = state->end + 1;
683 merge_state(tree, state);
684 goto search_again;
685 }
686
687 /*
688 * | ---- desired range ---- |
689 * | state |
690 * or
691 * | ------------- state -------------- |
692 *
693 * We need to split the extent we found, and may flip bits on
694 * second half.
695 *
696 * If the extent we found extends past our
697 * range, we just split and search again. It'll get split
698 * again the next time though.
699 *
700 * If the extent we found is inside our range, we set the
701 * desired bit on it.
702 */
703 if (state->start < start) {
704 set = state->state & bits;
705 if (exclusive && set) {
706 *failed_start = start;
707 err = -EEXIST;
708 goto out;
709 }
710 err = split_state(tree, state, prealloc, start);
711 BUG_ON(err == -EEXIST);
712 prealloc = NULL;
713 if (err)
714 goto out;
715 if (state->end <= end) {
716 set_state_bits(tree, state, bits);
717 start = state->end + 1;
718 merge_state(tree, state);
719 } else {
720 start = state->start;
721 }
722 goto search_again;
723 }
724 /*
725 * | ---- desired range ---- |
726 * | state | or | state |
727 *
728 * There's a hole, we need to insert something in it and
729 * ignore the extent we found.
730 */
731 if (state->start > start) {
732 u64 this_end;
733 if (end < last_start)
734 this_end = end;
735 else
736 this_end = last_start -1;
737 err = insert_state(tree, prealloc, start, this_end,
738 bits);
739 prealloc = NULL;
740 BUG_ON(err == -EEXIST);
741 if (err)
742 goto out;
743 start = this_end + 1;
744 goto search_again;
745 }
746 /*
747 * | ---- desired range ---- |
748 * | state |
749 * We need to split the extent, and set the bit
750 * on the first half
751 */
752 if (state->start <= end && state->end > end) {
753 set = state->state & bits;
754 if (exclusive && set) {
755 *failed_start = start;
756 err = -EEXIST;
757 goto out;
758 }
759 err = split_state(tree, state, prealloc, end + 1);
760 BUG_ON(err == -EEXIST);
761
762 set_state_bits(tree, prealloc, bits);
763 merge_state(tree, prealloc);
764 prealloc = NULL;
765 goto out;
766 }
767
768 goto search_again;
769
770out:
Chris Mason70dec802008-01-29 09:59:12 -0500771 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500772 if (prealloc)
773 free_extent_state(prealloc);
774
775 return err;
776
777search_again:
778 if (start > end)
779 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500780 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500781 if (mask & __GFP_WAIT)
782 cond_resched();
783 goto again;
784}
785EXPORT_SYMBOL(set_extent_bit);
786
787/* wrappers around set/clear extent bit */
788int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
789 gfp_t mask)
790{
791 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
792 mask);
793}
794EXPORT_SYMBOL(set_extent_dirty);
795
796int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
797 int bits, gfp_t mask)
798{
799 return set_extent_bit(tree, start, end, bits, 0, NULL,
800 mask);
801}
802EXPORT_SYMBOL(set_extent_bits);
803
804int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
805 int bits, gfp_t mask)
806{
807 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
808}
809EXPORT_SYMBOL(clear_extent_bits);
810
811int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
812 gfp_t mask)
813{
814 return set_extent_bit(tree, start, end,
815 EXTENT_DELALLOC | EXTENT_DIRTY, 0, NULL,
816 mask);
817}
818EXPORT_SYMBOL(set_extent_delalloc);
819
820int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
821 gfp_t mask)
822{
823 return clear_extent_bit(tree, start, end,
824 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
825}
826EXPORT_SYMBOL(clear_extent_dirty);
827
828int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
829 gfp_t mask)
830{
831 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
832 mask);
833}
834EXPORT_SYMBOL(set_extent_new);
835
836int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
837 gfp_t mask)
838{
839 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
840}
841EXPORT_SYMBOL(clear_extent_new);
842
843int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
844 gfp_t mask)
845{
846 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
847 mask);
848}
849EXPORT_SYMBOL(set_extent_uptodate);
850
851int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
852 gfp_t mask)
853{
854 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
855}
856EXPORT_SYMBOL(clear_extent_uptodate);
857
858int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
859 gfp_t mask)
860{
861 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
862 0, NULL, mask);
863}
864EXPORT_SYMBOL(set_extent_writeback);
865
866int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
867 gfp_t mask)
868{
869 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
870}
871EXPORT_SYMBOL(clear_extent_writeback);
872
873int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
874{
875 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
876}
877EXPORT_SYMBOL(wait_on_extent_writeback);
878
Chris Masond1310b22008-01-24 16:13:08 -0500879int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
880{
881 int err;
882 u64 failed_start;
883 while (1) {
884 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
885 &failed_start, mask);
886 if (err == -EEXIST && (mask & __GFP_WAIT)) {
887 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
888 start = failed_start;
889 } else {
890 break;
891 }
892 WARN_ON(start > end);
893 }
894 return err;
895}
896EXPORT_SYMBOL(lock_extent);
897
898int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
899 gfp_t mask)
900{
901 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
902}
903EXPORT_SYMBOL(unlock_extent);
904
905/*
906 * helper function to set pages and extents in the tree dirty
907 */
908int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
909{
910 unsigned long index = start >> PAGE_CACHE_SHIFT;
911 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
912 struct page *page;
913
914 while (index <= end_index) {
915 page = find_get_page(tree->mapping, index);
916 BUG_ON(!page);
917 __set_page_dirty_nobuffers(page);
918 page_cache_release(page);
919 index++;
920 }
921 set_extent_dirty(tree, start, end, GFP_NOFS);
922 return 0;
923}
924EXPORT_SYMBOL(set_range_dirty);
925
926/*
927 * helper function to set both pages and extents in the tree writeback
928 */
929int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
930{
931 unsigned long index = start >> PAGE_CACHE_SHIFT;
932 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
933 struct page *page;
934
935 while (index <= end_index) {
936 page = find_get_page(tree->mapping, index);
937 BUG_ON(!page);
938 set_page_writeback(page);
939 page_cache_release(page);
940 index++;
941 }
942 set_extent_writeback(tree, start, end, GFP_NOFS);
943 return 0;
944}
945EXPORT_SYMBOL(set_range_writeback);
946
947int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
948 u64 *start_ret, u64 *end_ret, int bits)
949{
950 struct rb_node *node;
951 struct extent_state *state;
952 int ret = 1;
953
Chris Mason70dec802008-01-29 09:59:12 -0500954 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500955 /*
956 * this search will find all the extents that end after
957 * our range starts.
958 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500959 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -0400960 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -0500961 goto out;
962 }
963
964 while(1) {
965 state = rb_entry(node, struct extent_state, rb_node);
966 if (state->end >= start && (state->state & bits)) {
967 *start_ret = state->start;
968 *end_ret = state->end;
969 ret = 0;
970 break;
971 }
972 node = rb_next(node);
973 if (!node)
974 break;
975 }
976out:
Chris Mason70dec802008-01-29 09:59:12 -0500977 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500978 return ret;
979}
980EXPORT_SYMBOL(find_first_extent_bit);
981
Chris Masond7fc6402008-02-18 12:12:38 -0500982struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
983 u64 start, int bits)
984{
985 struct rb_node *node;
986 struct extent_state *state;
987
988 /*
989 * this search will find all the extents that end after
990 * our range starts.
991 */
992 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -0400993 if (!node) {
Chris Masond7fc6402008-02-18 12:12:38 -0500994 goto out;
995 }
996
997 while(1) {
998 state = rb_entry(node, struct extent_state, rb_node);
999 if (state->end >= start && (state->state & bits)) {
1000 return state;
1001 }
1002 node = rb_next(node);
1003 if (!node)
1004 break;
1005 }
1006out:
1007 return NULL;
1008}
1009EXPORT_SYMBOL(find_first_extent_bit_state);
1010
Chris Masond1310b22008-01-24 16:13:08 -05001011u64 find_lock_delalloc_range(struct extent_io_tree *tree,
1012 u64 *start, u64 *end, u64 max_bytes)
1013{
1014 struct rb_node *node;
1015 struct extent_state *state;
1016 u64 cur_start = *start;
1017 u64 found = 0;
1018 u64 total_bytes = 0;
1019
Chris Mason70dec802008-01-29 09:59:12 -05001020 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001021 /*
1022 * this search will find all the extents that end after
1023 * our range starts.
1024 */
1025search_again:
Chris Mason80ea96b2008-02-01 14:51:59 -05001026 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001027 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001028 if (!found)
1029 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001030 goto out;
1031 }
1032
1033 while(1) {
1034 state = rb_entry(node, struct extent_state, rb_node);
1035 if (found && state->start != cur_start) {
1036 goto out;
1037 }
1038 if (!(state->state & EXTENT_DELALLOC)) {
1039 if (!found)
1040 *end = state->end;
1041 goto out;
1042 }
1043 if (!found) {
1044 struct extent_state *prev_state;
1045 struct rb_node *prev_node = node;
1046 while(1) {
1047 prev_node = rb_prev(prev_node);
1048 if (!prev_node)
1049 break;
1050 prev_state = rb_entry(prev_node,
1051 struct extent_state,
1052 rb_node);
1053 if (!(prev_state->state & EXTENT_DELALLOC))
1054 break;
1055 state = prev_state;
1056 node = prev_node;
1057 }
1058 }
1059 if (state->state & EXTENT_LOCKED) {
1060 DEFINE_WAIT(wait);
1061 atomic_inc(&state->refs);
1062 prepare_to_wait(&state->wq, &wait,
1063 TASK_UNINTERRUPTIBLE);
Chris Mason70dec802008-01-29 09:59:12 -05001064 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001065 schedule();
Chris Mason70dec802008-01-29 09:59:12 -05001066 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001067 finish_wait(&state->wq, &wait);
1068 free_extent_state(state);
1069 goto search_again;
1070 }
Chris Mason291d6732008-01-29 15:55:23 -05001071 set_state_cb(tree, state, EXTENT_LOCKED);
Chris Masonb0c68f82008-01-31 11:05:37 -05001072 state->state |= EXTENT_LOCKED;
Chris Masond1310b22008-01-24 16:13:08 -05001073 if (!found)
1074 *start = state->start;
1075 found++;
1076 *end = state->end;
1077 cur_start = state->end + 1;
1078 node = rb_next(node);
1079 if (!node)
1080 break;
1081 total_bytes += state->end - state->start + 1;
1082 if (total_bytes >= max_bytes)
1083 break;
1084 }
1085out:
Chris Mason70dec802008-01-29 09:59:12 -05001086 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001087 return found;
1088}
1089
1090u64 count_range_bits(struct extent_io_tree *tree,
1091 u64 *start, u64 search_end, u64 max_bytes,
1092 unsigned long bits)
1093{
1094 struct rb_node *node;
1095 struct extent_state *state;
1096 u64 cur_start = *start;
1097 u64 total_bytes = 0;
1098 int found = 0;
1099
1100 if (search_end <= cur_start) {
1101 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1102 WARN_ON(1);
1103 return 0;
1104 }
1105
Chris Mason70dec802008-01-29 09:59:12 -05001106 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001107 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1108 total_bytes = tree->dirty_bytes;
1109 goto out;
1110 }
1111 /*
1112 * this search will find all the extents that end after
1113 * our range starts.
1114 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001115 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001116 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001117 goto out;
1118 }
1119
1120 while(1) {
1121 state = rb_entry(node, struct extent_state, rb_node);
1122 if (state->start > search_end)
1123 break;
1124 if (state->end >= cur_start && (state->state & bits)) {
1125 total_bytes += min(search_end, state->end) + 1 -
1126 max(cur_start, state->start);
1127 if (total_bytes >= max_bytes)
1128 break;
1129 if (!found) {
1130 *start = state->start;
1131 found = 1;
1132 }
1133 }
1134 node = rb_next(node);
1135 if (!node)
1136 break;
1137 }
1138out:
Chris Mason70dec802008-01-29 09:59:12 -05001139 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001140 return total_bytes;
1141}
1142/*
1143 * helper function to lock both pages and extents in the tree.
1144 * pages must be locked first.
1145 */
1146int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1147{
1148 unsigned long index = start >> PAGE_CACHE_SHIFT;
1149 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1150 struct page *page;
1151 int err;
1152
1153 while (index <= end_index) {
1154 page = grab_cache_page(tree->mapping, index);
1155 if (!page) {
1156 err = -ENOMEM;
1157 goto failed;
1158 }
1159 if (IS_ERR(page)) {
1160 err = PTR_ERR(page);
1161 goto failed;
1162 }
1163 index++;
1164 }
1165 lock_extent(tree, start, end, GFP_NOFS);
1166 return 0;
1167
1168failed:
1169 /*
1170 * we failed above in getting the page at 'index', so we undo here
1171 * up to but not including the page at 'index'
1172 */
1173 end_index = index;
1174 index = start >> PAGE_CACHE_SHIFT;
1175 while (index < end_index) {
1176 page = find_get_page(tree->mapping, index);
1177 unlock_page(page);
1178 page_cache_release(page);
1179 index++;
1180 }
1181 return err;
1182}
1183EXPORT_SYMBOL(lock_range);
1184
1185/*
1186 * helper function to unlock both pages and extents in the tree.
1187 */
1188int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1189{
1190 unsigned long index = start >> PAGE_CACHE_SHIFT;
1191 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1192 struct page *page;
1193
1194 while (index <= end_index) {
1195 page = find_get_page(tree->mapping, index);
1196 unlock_page(page);
1197 page_cache_release(page);
1198 index++;
1199 }
1200 unlock_extent(tree, start, end, GFP_NOFS);
1201 return 0;
1202}
1203EXPORT_SYMBOL(unlock_range);
1204
1205int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1206{
1207 struct rb_node *node;
1208 struct extent_state *state;
1209 int ret = 0;
1210
Chris Mason70dec802008-01-29 09:59:12 -05001211 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001212 /*
1213 * this search will find all the extents that end after
1214 * our range starts.
1215 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001216 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001217 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001218 ret = -ENOENT;
1219 goto out;
1220 }
1221 state = rb_entry(node, struct extent_state, rb_node);
1222 if (state->start != start) {
1223 ret = -ENOENT;
1224 goto out;
1225 }
1226 state->private = private;
1227out:
Chris Mason70dec802008-01-29 09:59:12 -05001228 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001229 return ret;
1230}
1231
1232int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1233{
1234 struct rb_node *node;
1235 struct extent_state *state;
1236 int ret = 0;
1237
Chris Mason70dec802008-01-29 09:59:12 -05001238 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001239 /*
1240 * this search will find all the extents that end after
1241 * our range starts.
1242 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001243 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001244 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001245 ret = -ENOENT;
1246 goto out;
1247 }
1248 state = rb_entry(node, struct extent_state, rb_node);
1249 if (state->start != start) {
1250 ret = -ENOENT;
1251 goto out;
1252 }
1253 *private = state->private;
1254out:
Chris Mason70dec802008-01-29 09:59:12 -05001255 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001256 return ret;
1257}
1258
1259/*
1260 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001261 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001262 * has the bits set. Otherwise, 1 is returned if any bit in the
1263 * range is found set.
1264 */
1265int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1266 int bits, int filled)
1267{
1268 struct extent_state *state = NULL;
1269 struct rb_node *node;
1270 int bitset = 0;
1271 unsigned long flags;
1272
Chris Mason70dec802008-01-29 09:59:12 -05001273 spin_lock_irqsave(&tree->lock, flags);
Chris Mason80ea96b2008-02-01 14:51:59 -05001274 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001275 while (node && start <= end) {
1276 state = rb_entry(node, struct extent_state, rb_node);
1277
1278 if (filled && state->start > start) {
1279 bitset = 0;
1280 break;
1281 }
1282
1283 if (state->start > end)
1284 break;
1285
1286 if (state->state & bits) {
1287 bitset = 1;
1288 if (!filled)
1289 break;
1290 } else if (filled) {
1291 bitset = 0;
1292 break;
1293 }
1294 start = state->end + 1;
1295 if (start > end)
1296 break;
1297 node = rb_next(node);
1298 if (!node) {
1299 if (filled)
1300 bitset = 0;
1301 break;
1302 }
1303 }
Chris Mason70dec802008-01-29 09:59:12 -05001304 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05001305 return bitset;
1306}
1307EXPORT_SYMBOL(test_range_bit);
1308
1309/*
1310 * helper function to set a given page up to date if all the
1311 * extents in the tree for that page are up to date
1312 */
1313static int check_page_uptodate(struct extent_io_tree *tree,
1314 struct page *page)
1315{
1316 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1317 u64 end = start + PAGE_CACHE_SIZE - 1;
1318 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1319 SetPageUptodate(page);
1320 return 0;
1321}
1322
1323/*
1324 * helper function to unlock a page if all the extents in the tree
1325 * for that page are unlocked
1326 */
1327static int check_page_locked(struct extent_io_tree *tree,
1328 struct page *page)
1329{
1330 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1331 u64 end = start + PAGE_CACHE_SIZE - 1;
1332 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1333 unlock_page(page);
1334 return 0;
1335}
1336
1337/*
1338 * helper function to end page writeback if all the extents
1339 * in the tree for that page are done with writeback
1340 */
1341static int check_page_writeback(struct extent_io_tree *tree,
1342 struct page *page)
1343{
1344 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1345 u64 end = start + PAGE_CACHE_SIZE - 1;
1346 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1347 end_page_writeback(page);
1348 return 0;
1349}
1350
1351/* lots and lots of room for performance fixes in the end_bio funcs */
1352
1353/*
1354 * after a writepage IO is done, we need to:
1355 * clear the uptodate bits on error
1356 * clear the writeback bits in the extent tree for this IO
1357 * end_page_writeback if the page has no more pending IO
1358 *
1359 * Scheduling is not allowed, so the extent state tree is expected
1360 * to have one and only one object corresponding to this IO.
1361 */
1362#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1363static void end_bio_extent_writepage(struct bio *bio, int err)
1364#else
1365static int end_bio_extent_writepage(struct bio *bio,
1366 unsigned int bytes_done, int err)
1367#endif
1368{
Chris Mason1259ab72008-05-12 13:39:03 -04001369 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001370 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Mason70dec802008-01-29 09:59:12 -05001371 struct extent_state *state = bio->bi_private;
1372 struct extent_io_tree *tree = state->tree;
1373 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -05001374 u64 start;
1375 u64 end;
Chris Mason70dec802008-01-29 09:59:12 -05001376 u64 cur;
Chris Masond1310b22008-01-24 16:13:08 -05001377 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001378 int ret;
Chris Mason70dec802008-01-29 09:59:12 -05001379 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001380
1381#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1382 if (bio->bi_size)
1383 return 1;
1384#endif
Chris Masond1310b22008-01-24 16:13:08 -05001385 do {
1386 struct page *page = bvec->bv_page;
1387 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1388 bvec->bv_offset;
1389 end = start + bvec->bv_len - 1;
1390
1391 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1392 whole_page = 1;
1393 else
1394 whole_page = 0;
1395
1396 if (--bvec >= bio->bi_io_vec)
1397 prefetchw(&bvec->bv_page->flags);
1398
Chris Mason1259ab72008-05-12 13:39:03 -04001399 if (tree->ops && tree->ops->writepage_end_io_hook) {
1400 ret = tree->ops->writepage_end_io_hook(page, start,
1401 end, state);
1402 if (ret)
1403 uptodate = 0;
1404 }
1405
1406 if (!uptodate && tree->ops &&
1407 tree->ops->writepage_io_failed_hook) {
1408 ret = tree->ops->writepage_io_failed_hook(bio, page,
1409 start, end, state);
1410 if (ret == 0) {
1411 state = NULL;
1412 uptodate = (err == 0);
1413 continue;
1414 }
1415 }
1416
Chris Masond1310b22008-01-24 16:13:08 -05001417 if (!uptodate) {
1418 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1419 ClearPageUptodate(page);
1420 SetPageError(page);
1421 }
Chris Mason70dec802008-01-29 09:59:12 -05001422
Chris Mason70dec802008-01-29 09:59:12 -05001423 /*
1424 * bios can get merged in funny ways, and so we need to
1425 * be careful with the state variable. We know the
1426 * state won't be merged with others because it has
1427 * WRITEBACK set, but we can't be sure each biovec is
1428 * sequential in the file. So, if our cached state
1429 * doesn't match the expected end, search the tree
1430 * for the correct one.
1431 */
1432
1433 spin_lock_irqsave(&tree->lock, flags);
1434 if (!state || state->end != end) {
1435 state = NULL;
Chris Mason80ea96b2008-02-01 14:51:59 -05001436 node = __etree_search(tree, start, NULL, NULL);
Chris Mason70dec802008-01-29 09:59:12 -05001437 if (node) {
1438 state = rb_entry(node, struct extent_state,
1439 rb_node);
1440 if (state->end != end ||
1441 !(state->state & EXTENT_WRITEBACK))
1442 state = NULL;
1443 }
1444 if (!state) {
1445 spin_unlock_irqrestore(&tree->lock, flags);
1446 clear_extent_writeback(tree, start,
1447 end, GFP_ATOMIC);
1448 goto next_io;
1449 }
1450 }
1451 cur = end;
1452 while(1) {
1453 struct extent_state *clear = state;
1454 cur = state->start;
1455 node = rb_prev(&state->rb_node);
1456 if (node) {
1457 state = rb_entry(node,
1458 struct extent_state,
1459 rb_node);
1460 } else {
1461 state = NULL;
1462 }
1463
1464 clear_state_bit(tree, clear, EXTENT_WRITEBACK,
1465 1, 0);
1466 if (cur == start)
1467 break;
1468 if (cur < start) {
1469 WARN_ON(1);
1470 break;
1471 }
1472 if (!node)
1473 break;
1474 }
1475 /* before releasing the lock, make sure the next state
1476 * variable has the expected bits set and corresponds
1477 * to the correct offsets in the file
1478 */
1479 if (state && (state->end + 1 != start ||
Yanc2e639f2008-02-04 08:57:25 -05001480 !(state->state & EXTENT_WRITEBACK))) {
Chris Mason70dec802008-01-29 09:59:12 -05001481 state = NULL;
1482 }
1483 spin_unlock_irqrestore(&tree->lock, flags);
1484next_io:
Chris Masond1310b22008-01-24 16:13:08 -05001485
1486 if (whole_page)
1487 end_page_writeback(page);
1488 else
1489 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001490 } while (bvec >= bio->bi_io_vec);
Chris Masond1310b22008-01-24 16:13:08 -05001491 bio_put(bio);
1492#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1493 return 0;
1494#endif
1495}
1496
1497/*
1498 * after a readpage IO is done, we need to:
1499 * clear the uptodate bits on error
1500 * set the uptodate bits if things worked
1501 * set the page up to date if all extents in the tree are uptodate
1502 * clear the lock bit in the extent tree
1503 * unlock the page if there are no other extents locked for it
1504 *
1505 * Scheduling is not allowed, so the extent state tree is expected
1506 * to have one and only one object corresponding to this IO.
1507 */
1508#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1509static void end_bio_extent_readpage(struct bio *bio, int err)
1510#else
1511static int end_bio_extent_readpage(struct bio *bio,
1512 unsigned int bytes_done, int err)
1513#endif
1514{
1515 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1516 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Mason70dec802008-01-29 09:59:12 -05001517 struct extent_state *state = bio->bi_private;
1518 struct extent_io_tree *tree = state->tree;
1519 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -05001520 u64 start;
1521 u64 end;
Chris Mason70dec802008-01-29 09:59:12 -05001522 u64 cur;
1523 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001524 int whole_page;
1525 int ret;
1526
1527#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1528 if (bio->bi_size)
1529 return 1;
1530#endif
1531
1532 do {
1533 struct page *page = bvec->bv_page;
1534 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1535 bvec->bv_offset;
1536 end = start + bvec->bv_len - 1;
1537
1538 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1539 whole_page = 1;
1540 else
1541 whole_page = 0;
1542
1543 if (--bvec >= bio->bi_io_vec)
1544 prefetchw(&bvec->bv_page->flags);
1545
1546 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001547 ret = tree->ops->readpage_end_io_hook(page, start, end,
1548 state);
Chris Masond1310b22008-01-24 16:13:08 -05001549 if (ret)
1550 uptodate = 0;
1551 }
Chris Mason7e383262008-04-09 16:28:12 -04001552 if (!uptodate && tree->ops &&
1553 tree->ops->readpage_io_failed_hook) {
1554 ret = tree->ops->readpage_io_failed_hook(bio, page,
1555 start, end, state);
1556 if (ret == 0) {
1557 state = NULL;
Chris Mason3b951512008-04-17 11:29:12 -04001558 uptodate =
1559 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason7e383262008-04-09 16:28:12 -04001560 continue;
1561 }
1562 }
Chris Mason70dec802008-01-29 09:59:12 -05001563
1564 spin_lock_irqsave(&tree->lock, flags);
1565 if (!state || state->end != end) {
1566 state = NULL;
Chris Mason80ea96b2008-02-01 14:51:59 -05001567 node = __etree_search(tree, start, NULL, NULL);
Chris Mason70dec802008-01-29 09:59:12 -05001568 if (node) {
1569 state = rb_entry(node, struct extent_state,
1570 rb_node);
1571 if (state->end != end ||
1572 !(state->state & EXTENT_LOCKED))
1573 state = NULL;
1574 }
Chris Mason3b951512008-04-17 11:29:12 -04001575 if (!state) {
Chris Mason70dec802008-01-29 09:59:12 -05001576 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason3b951512008-04-17 11:29:12 -04001577 if (uptodate)
1578 set_extent_uptodate(tree, start, end,
1579 GFP_ATOMIC);
Chris Mason70dec802008-01-29 09:59:12 -05001580 unlock_extent(tree, start, end, GFP_ATOMIC);
1581 goto next_io;
1582 }
Chris Masond1310b22008-01-24 16:13:08 -05001583 }
1584
Chris Mason70dec802008-01-29 09:59:12 -05001585 cur = end;
1586 while(1) {
1587 struct extent_state *clear = state;
1588 cur = state->start;
1589 node = rb_prev(&state->rb_node);
1590 if (node) {
1591 state = rb_entry(node,
1592 struct extent_state,
1593 rb_node);
1594 } else {
1595 state = NULL;
1596 }
Chris Masonf1885912008-04-09 16:28:12 -04001597 if (uptodate) {
1598 set_state_cb(tree, clear, EXTENT_UPTODATE);
1599 clear->state |= EXTENT_UPTODATE;
1600 }
Chris Mason70dec802008-01-29 09:59:12 -05001601 clear_state_bit(tree, clear, EXTENT_LOCKED,
1602 1, 0);
1603 if (cur == start)
1604 break;
1605 if (cur < start) {
1606 WARN_ON(1);
1607 break;
1608 }
1609 if (!node)
1610 break;
1611 }
1612 /* before releasing the lock, make sure the next state
1613 * variable has the expected bits set and corresponds
1614 * to the correct offsets in the file
1615 */
1616 if (state && (state->end + 1 != start ||
Yanc2e639f2008-02-04 08:57:25 -05001617 !(state->state & EXTENT_LOCKED))) {
Chris Mason70dec802008-01-29 09:59:12 -05001618 state = NULL;
1619 }
1620 spin_unlock_irqrestore(&tree->lock, flags);
1621next_io:
1622 if (whole_page) {
1623 if (uptodate) {
1624 SetPageUptodate(page);
1625 } else {
1626 ClearPageUptodate(page);
1627 SetPageError(page);
1628 }
Chris Masond1310b22008-01-24 16:13:08 -05001629 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001630 } else {
1631 if (uptodate) {
1632 check_page_uptodate(tree, page);
1633 } else {
1634 ClearPageUptodate(page);
1635 SetPageError(page);
1636 }
Chris Masond1310b22008-01-24 16:13:08 -05001637 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001638 }
Chris Masond1310b22008-01-24 16:13:08 -05001639 } while (bvec >= bio->bi_io_vec);
1640
1641 bio_put(bio);
1642#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1643 return 0;
1644#endif
1645}
1646
1647/*
1648 * IO done from prepare_write is pretty simple, we just unlock
1649 * the structs in the extent tree when done, and set the uptodate bits
1650 * as appropriate.
1651 */
1652#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1653static void end_bio_extent_preparewrite(struct bio *bio, int err)
1654#else
1655static int end_bio_extent_preparewrite(struct bio *bio,
1656 unsigned int bytes_done, int err)
1657#endif
1658{
1659 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1660 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Mason70dec802008-01-29 09:59:12 -05001661 struct extent_state *state = bio->bi_private;
1662 struct extent_io_tree *tree = state->tree;
Chris Masond1310b22008-01-24 16:13:08 -05001663 u64 start;
1664 u64 end;
1665
1666#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1667 if (bio->bi_size)
1668 return 1;
1669#endif
1670
1671 do {
1672 struct page *page = bvec->bv_page;
1673 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1674 bvec->bv_offset;
1675 end = start + bvec->bv_len - 1;
1676
1677 if (--bvec >= bio->bi_io_vec)
1678 prefetchw(&bvec->bv_page->flags);
1679
1680 if (uptodate) {
1681 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1682 } else {
1683 ClearPageUptodate(page);
1684 SetPageError(page);
1685 }
1686
1687 unlock_extent(tree, start, end, GFP_ATOMIC);
1688
1689 } while (bvec >= bio->bi_io_vec);
1690
1691 bio_put(bio);
1692#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1693 return 0;
1694#endif
1695}
1696
1697static struct bio *
1698extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1699 gfp_t gfp_flags)
1700{
1701 struct bio *bio;
1702
1703 bio = bio_alloc(gfp_flags, nr_vecs);
1704
1705 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1706 while (!bio && (nr_vecs /= 2))
1707 bio = bio_alloc(gfp_flags, nr_vecs);
1708 }
1709
1710 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001711 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001712 bio->bi_bdev = bdev;
1713 bio->bi_sector = first_sector;
1714 }
1715 return bio;
1716}
1717
Chris Masonf1885912008-04-09 16:28:12 -04001718static int submit_one_bio(int rw, struct bio *bio, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05001719{
Chris Masond1310b22008-01-24 16:13:08 -05001720 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001721 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1722 struct page *page = bvec->bv_page;
1723 struct extent_io_tree *tree = bio->bi_private;
1724 struct rb_node *node;
1725 struct extent_state *state;
1726 u64 start;
1727 u64 end;
1728
1729 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1730 end = start + bvec->bv_len - 1;
1731
1732 spin_lock_irq(&tree->lock);
Chris Mason80ea96b2008-02-01 14:51:59 -05001733 node = __etree_search(tree, start, NULL, NULL);
Chris Mason70dec802008-01-29 09:59:12 -05001734 BUG_ON(!node);
1735 state = rb_entry(node, struct extent_state, rb_node);
1736 while(state->end < end) {
1737 node = rb_next(node);
1738 state = rb_entry(node, struct extent_state, rb_node);
1739 }
1740 BUG_ON(state->end != end);
1741 spin_unlock_irq(&tree->lock);
1742
1743 bio->bi_private = state;
Chris Masond1310b22008-01-24 16:13:08 -05001744
1745 bio_get(bio);
1746
Chris Mason065631f2008-02-20 12:07:25 -05001747 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001748 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1749 mirror_num);
Chris Mason0b86a832008-03-24 15:01:56 -04001750 else
1751 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001752 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1753 ret = -EOPNOTSUPP;
1754 bio_put(bio);
1755 return ret;
1756}
1757
1758static int submit_extent_page(int rw, struct extent_io_tree *tree,
1759 struct page *page, sector_t sector,
1760 size_t size, unsigned long offset,
1761 struct block_device *bdev,
1762 struct bio **bio_ret,
1763 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001764 bio_end_io_t end_io_func,
1765 int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05001766{
1767 int ret = 0;
1768 struct bio *bio;
1769 int nr;
1770
1771 if (bio_ret && *bio_ret) {
1772 bio = *bio_ret;
1773 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
Chris Mason239b14b2008-03-24 15:02:07 -04001774 (tree->ops && tree->ops->merge_bio_hook &&
1775 tree->ops->merge_bio_hook(page, offset, size, bio)) ||
Chris Masond1310b22008-01-24 16:13:08 -05001776 bio_add_page(bio, page, size, offset) < size) {
Chris Masonf1885912008-04-09 16:28:12 -04001777 ret = submit_one_bio(rw, bio, mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05001778 bio = NULL;
1779 } else {
1780 return 0;
1781 }
1782 }
Chris Mason961d0232008-02-06 11:01:42 -05001783 nr = bio_get_nr_vecs(bdev);
Chris Masond1310b22008-01-24 16:13:08 -05001784 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1785 if (!bio) {
1786 printk("failed to allocate bio nr %d\n", nr);
1787 }
Chris Mason70dec802008-01-29 09:59:12 -05001788
1789
Chris Masond1310b22008-01-24 16:13:08 -05001790 bio_add_page(bio, page, size, offset);
1791 bio->bi_end_io = end_io_func;
1792 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001793
Chris Masond1310b22008-01-24 16:13:08 -05001794 if (bio_ret) {
1795 *bio_ret = bio;
1796 } else {
Chris Masonf1885912008-04-09 16:28:12 -04001797 ret = submit_one_bio(rw, bio, mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05001798 }
1799
1800 return ret;
1801}
1802
1803void set_page_extent_mapped(struct page *page)
1804{
1805 if (!PagePrivate(page)) {
1806 SetPagePrivate(page);
1807 WARN_ON(!page->mapping->a_ops->invalidatepage);
1808 set_page_private(page, EXTENT_PAGE_PRIVATE);
1809 page_cache_get(page);
1810 }
1811}
1812
1813void set_page_extent_head(struct page *page, unsigned long len)
1814{
1815 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1816}
1817
1818/*
1819 * basic readpage implementation. Locked extent state structs are inserted
1820 * into the tree that are removed when the IO is done (by the end_io
1821 * handlers)
1822 */
1823static int __extent_read_full_page(struct extent_io_tree *tree,
1824 struct page *page,
1825 get_extent_t *get_extent,
Chris Masonf1885912008-04-09 16:28:12 -04001826 struct bio **bio, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05001827{
1828 struct inode *inode = page->mapping->host;
1829 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1830 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1831 u64 end;
1832 u64 cur = start;
1833 u64 extent_offset;
1834 u64 last_byte = i_size_read(inode);
1835 u64 block_start;
1836 u64 cur_end;
1837 sector_t sector;
1838 struct extent_map *em;
1839 struct block_device *bdev;
1840 int ret;
1841 int nr = 0;
1842 size_t page_offset = 0;
1843 size_t iosize;
1844 size_t blocksize = inode->i_sb->s_blocksize;
1845
1846 set_page_extent_mapped(page);
1847
1848 end = page_end;
1849 lock_extent(tree, start, end, GFP_NOFS);
1850
1851 while (cur <= end) {
1852 if (cur >= last_byte) {
1853 char *userpage;
1854 iosize = PAGE_CACHE_SIZE - page_offset;
1855 userpage = kmap_atomic(page, KM_USER0);
1856 memset(userpage + page_offset, 0, iosize);
1857 flush_dcache_page(page);
1858 kunmap_atomic(userpage, KM_USER0);
1859 set_extent_uptodate(tree, cur, cur + iosize - 1,
1860 GFP_NOFS);
1861 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1862 break;
1863 }
1864 em = get_extent(inode, page, page_offset, cur,
1865 end - cur + 1, 0);
1866 if (IS_ERR(em) || !em) {
1867 SetPageError(page);
1868 unlock_extent(tree, cur, end, GFP_NOFS);
1869 break;
1870 }
1871
1872 extent_offset = cur - em->start;
1873 BUG_ON(extent_map_end(em) <= cur);
1874 BUG_ON(end < cur);
1875
1876 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1877 cur_end = min(extent_map_end(em) - 1, end);
1878 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1879 sector = (em->block_start + extent_offset) >> 9;
1880 bdev = em->bdev;
1881 block_start = em->block_start;
1882 free_extent_map(em);
1883 em = NULL;
1884
1885 /* we've found a hole, just zero and go on */
1886 if (block_start == EXTENT_MAP_HOLE) {
1887 char *userpage;
1888 userpage = kmap_atomic(page, KM_USER0);
1889 memset(userpage + page_offset, 0, iosize);
1890 flush_dcache_page(page);
1891 kunmap_atomic(userpage, KM_USER0);
1892
1893 set_extent_uptodate(tree, cur, cur + iosize - 1,
1894 GFP_NOFS);
1895 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1896 cur = cur + iosize;
1897 page_offset += iosize;
1898 continue;
1899 }
1900 /* the get_extent function already copied into the page */
1901 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1902 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1903 cur = cur + iosize;
1904 page_offset += iosize;
1905 continue;
1906 }
Chris Mason70dec802008-01-29 09:59:12 -05001907 /* we have an inline extent but it didn't get marked up
1908 * to date. Error out
1909 */
1910 if (block_start == EXTENT_MAP_INLINE) {
1911 SetPageError(page);
1912 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1913 cur = cur + iosize;
1914 page_offset += iosize;
1915 continue;
1916 }
Chris Masond1310b22008-01-24 16:13:08 -05001917
1918 ret = 0;
1919 if (tree->ops && tree->ops->readpage_io_hook) {
1920 ret = tree->ops->readpage_io_hook(page, cur,
1921 cur + iosize - 1);
1922 }
1923 if (!ret) {
1924 unsigned long nr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1925 nr -= page->index;
1926 ret = submit_extent_page(READ, tree, page,
1927 sector, iosize, page_offset,
1928 bdev, bio, nr,
Chris Masonf1885912008-04-09 16:28:12 -04001929 end_bio_extent_readpage, mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05001930 }
1931 if (ret)
1932 SetPageError(page);
1933 cur = cur + iosize;
1934 page_offset += iosize;
1935 nr++;
1936 }
1937 if (!nr) {
1938 if (!PageError(page))
1939 SetPageUptodate(page);
1940 unlock_page(page);
1941 }
1942 return 0;
1943}
1944
1945int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
1946 get_extent_t *get_extent)
1947{
1948 struct bio *bio = NULL;
1949 int ret;
1950
Chris Masonf1885912008-04-09 16:28:12 -04001951 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001952 if (bio)
Chris Masonf1885912008-04-09 16:28:12 -04001953 submit_one_bio(READ, bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001954 return ret;
1955}
1956EXPORT_SYMBOL(extent_read_full_page);
1957
1958/*
1959 * the writepage semantics are similar to regular writepage. extent
1960 * records are inserted to lock ranges in the tree, and as dirty areas
1961 * are found, they are marked writeback. Then the lock bits are removed
1962 * and the end_io handler clears the writeback ranges
1963 */
1964static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1965 void *data)
1966{
1967 struct inode *inode = page->mapping->host;
1968 struct extent_page_data *epd = data;
1969 struct extent_io_tree *tree = epd->tree;
1970 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1971 u64 delalloc_start;
1972 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1973 u64 end;
1974 u64 cur = start;
1975 u64 extent_offset;
1976 u64 last_byte = i_size_read(inode);
1977 u64 block_start;
1978 u64 iosize;
1979 sector_t sector;
1980 struct extent_map *em;
1981 struct block_device *bdev;
1982 int ret;
1983 int nr = 0;
1984 size_t page_offset = 0;
1985 size_t blocksize;
1986 loff_t i_size = i_size_read(inode);
1987 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
1988 u64 nr_delalloc;
1989 u64 delalloc_end;
1990
Chris Mason211c17f2008-05-15 09:13:45 -04001991
Chris Masond1310b22008-01-24 16:13:08 -05001992 WARN_ON(!PageLocked(page));
Chris Mason211c17f2008-05-15 09:13:45 -04001993 page_offset = i_size & (PAGE_CACHE_SIZE - 1);
1994 if (page->index > end_index ||
1995 (page->index == end_index && !page_offset)) {
1996 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001997 unlock_page(page);
1998 return 0;
1999 }
2000
2001 if (page->index == end_index) {
2002 char *userpage;
2003
Chris Masond1310b22008-01-24 16:13:08 -05002004 userpage = kmap_atomic(page, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002005 memset(userpage + page_offset, 0,
2006 PAGE_CACHE_SIZE - page_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002007 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002008 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002009 }
Chris Mason211c17f2008-05-15 09:13:45 -04002010 page_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002011
2012 set_page_extent_mapped(page);
2013
2014 delalloc_start = start;
2015 delalloc_end = 0;
2016 while(delalloc_end < page_end) {
2017 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
2018 &delalloc_end,
2019 128 * 1024 * 1024);
2020 if (nr_delalloc == 0) {
2021 delalloc_start = delalloc_end + 1;
2022 continue;
2023 }
2024 tree->ops->fill_delalloc(inode, delalloc_start,
2025 delalloc_end);
2026 clear_extent_bit(tree, delalloc_start,
2027 delalloc_end,
2028 EXTENT_LOCKED | EXTENT_DELALLOC,
2029 1, 0, GFP_NOFS);
2030 delalloc_start = delalloc_end + 1;
2031 }
2032 lock_extent(tree, start, page_end, GFP_NOFS);
2033
2034 end = page_end;
2035 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2036 printk("found delalloc bits after lock_extent\n");
2037 }
2038
2039 if (last_byte <= start) {
2040 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
2041 goto done;
2042 }
2043
2044 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2045 blocksize = inode->i_sb->s_blocksize;
2046
2047 while (cur <= end) {
2048 if (cur >= last_byte) {
2049 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
2050 break;
2051 }
2052 em = epd->get_extent(inode, page, page_offset, cur,
2053 end - cur + 1, 1);
2054 if (IS_ERR(em) || !em) {
2055 SetPageError(page);
2056 break;
2057 }
2058
2059 extent_offset = cur - em->start;
2060 BUG_ON(extent_map_end(em) <= cur);
2061 BUG_ON(end < cur);
2062 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2063 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2064 sector = (em->block_start + extent_offset) >> 9;
2065 bdev = em->bdev;
2066 block_start = em->block_start;
2067 free_extent_map(em);
2068 em = NULL;
2069
2070 if (block_start == EXTENT_MAP_HOLE ||
2071 block_start == EXTENT_MAP_INLINE) {
2072 clear_extent_dirty(tree, cur,
2073 cur + iosize - 1, GFP_NOFS);
2074 cur = cur + iosize;
2075 page_offset += iosize;
2076 continue;
2077 }
2078
2079 /* leave this out until we have a page_mkwrite call */
2080 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2081 EXTENT_DIRTY, 0)) {
2082 cur = cur + iosize;
2083 page_offset += iosize;
2084 continue;
2085 }
2086 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2087 if (tree->ops && tree->ops->writepage_io_hook) {
2088 ret = tree->ops->writepage_io_hook(page, cur,
2089 cur + iosize - 1);
2090 } else {
2091 ret = 0;
2092 }
Chris Mason1259ab72008-05-12 13:39:03 -04002093 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002094 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002095 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002096 unsigned long max_nr = end_index + 1;
2097 set_range_writeback(tree, cur, cur + iosize - 1);
2098 if (!PageWriteback(page)) {
2099 printk("warning page %lu not writeback, "
2100 "cur %llu end %llu\n", page->index,
2101 (unsigned long long)cur,
2102 (unsigned long long)end);
2103 }
2104
2105 ret = submit_extent_page(WRITE, tree, page, sector,
2106 iosize, page_offset, bdev,
2107 &epd->bio, max_nr,
Chris Masonf1885912008-04-09 16:28:12 -04002108 end_bio_extent_writepage, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002109 if (ret)
2110 SetPageError(page);
2111 }
2112 cur = cur + iosize;
2113 page_offset += iosize;
2114 nr++;
2115 }
2116done:
2117 if (nr == 0) {
2118 /* make sure the mapping tag for page dirty gets cleared */
2119 set_page_writeback(page);
2120 end_page_writeback(page);
2121 }
2122 unlock_extent(tree, start, page_end, GFP_NOFS);
2123 unlock_page(page);
2124 return 0;
2125}
2126
Chris Mason5e478dc2008-04-25 09:10:45 -04002127#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
Chris Masond1310b22008-01-24 16:13:08 -05002128/* Taken directly from 2.6.23 for 2.6.18 back port */
2129typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
2130 void *data);
2131
2132/**
2133 * write_cache_pages - walk the list of dirty pages of the given address space
2134 * and write all of them.
2135 * @mapping: address space structure to write
2136 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2137 * @writepage: function called for each page
2138 * @data: data passed to writepage function
2139 *
2140 * If a page is already under I/O, write_cache_pages() skips it, even
2141 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2142 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2143 * and msync() need to guarantee that all the data which was dirty at the time
2144 * the call was made get new I/O started against them. If wbc->sync_mode is
2145 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2146 * existing IO to complete.
2147 */
2148static int write_cache_pages(struct address_space *mapping,
2149 struct writeback_control *wbc, writepage_t writepage,
2150 void *data)
2151{
2152 struct backing_dev_info *bdi = mapping->backing_dev_info;
2153 int ret = 0;
2154 int done = 0;
2155 struct pagevec pvec;
2156 int nr_pages;
2157 pgoff_t index;
2158 pgoff_t end; /* Inclusive */
2159 int scanned = 0;
2160 int range_whole = 0;
2161
2162 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2163 wbc->encountered_congestion = 1;
2164 return 0;
2165 }
2166
2167 pagevec_init(&pvec, 0);
2168 if (wbc->range_cyclic) {
2169 index = mapping->writeback_index; /* Start from prev offset */
2170 end = -1;
2171 } else {
2172 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2173 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2174 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2175 range_whole = 1;
2176 scanned = 1;
2177 }
2178retry:
2179 while (!done && (index <= end) &&
2180 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2181 PAGECACHE_TAG_DIRTY,
2182 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2183 unsigned i;
2184
2185 scanned = 1;
2186 for (i = 0; i < nr_pages; i++) {
2187 struct page *page = pvec.pages[i];
2188
2189 /*
2190 * At this point we hold neither mapping->tree_lock nor
2191 * lock on the page itself: the page may be truncated or
2192 * invalidated (changing page->mapping to NULL), or even
2193 * swizzled back from swapper_space to tmpfs file
2194 * mapping
2195 */
2196 lock_page(page);
2197
2198 if (unlikely(page->mapping != mapping)) {
2199 unlock_page(page);
2200 continue;
2201 }
2202
2203 if (!wbc->range_cyclic && page->index > end) {
2204 done = 1;
2205 unlock_page(page);
2206 continue;
2207 }
2208
2209 if (wbc->sync_mode != WB_SYNC_NONE)
2210 wait_on_page_writeback(page);
2211
2212 if (PageWriteback(page) ||
2213 !clear_page_dirty_for_io(page)) {
2214 unlock_page(page);
2215 continue;
2216 }
2217
2218 ret = (*writepage)(page, wbc, data);
2219
2220 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2221 unlock_page(page);
2222 ret = 0;
2223 }
2224 if (ret || (--(wbc->nr_to_write) <= 0))
2225 done = 1;
2226 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2227 wbc->encountered_congestion = 1;
2228 done = 1;
2229 }
2230 }
2231 pagevec_release(&pvec);
2232 cond_resched();
2233 }
2234 if (!scanned && !done) {
2235 /*
2236 * We hit the last page and there is more work to be done: wrap
2237 * back to the start of the file
2238 */
2239 scanned = 1;
2240 index = 0;
2241 goto retry;
2242 }
2243 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2244 mapping->writeback_index = index;
2245 return ret;
2246}
2247#endif
2248
2249int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2250 get_extent_t *get_extent,
2251 struct writeback_control *wbc)
2252{
2253 int ret;
2254 struct address_space *mapping = page->mapping;
2255 struct extent_page_data epd = {
2256 .bio = NULL,
2257 .tree = tree,
2258 .get_extent = get_extent,
2259 };
2260 struct writeback_control wbc_writepages = {
2261 .bdi = wbc->bdi,
2262 .sync_mode = WB_SYNC_NONE,
2263 .older_than_this = NULL,
2264 .nr_to_write = 64,
2265 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2266 .range_end = (loff_t)-1,
2267 };
2268
2269
2270 ret = __extent_writepage(page, wbc, &epd);
2271
2272 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
2273 if (epd.bio) {
Chris Masonf1885912008-04-09 16:28:12 -04002274 submit_one_bio(WRITE, epd.bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002275 }
2276 return ret;
2277}
2278EXPORT_SYMBOL(extent_write_full_page);
2279
2280
2281int extent_writepages(struct extent_io_tree *tree,
2282 struct address_space *mapping,
2283 get_extent_t *get_extent,
2284 struct writeback_control *wbc)
2285{
2286 int ret = 0;
2287 struct extent_page_data epd = {
2288 .bio = NULL,
2289 .tree = tree,
2290 .get_extent = get_extent,
2291 };
2292
2293 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
2294 if (epd.bio) {
Chris Masonf1885912008-04-09 16:28:12 -04002295 submit_one_bio(WRITE, epd.bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002296 }
2297 return ret;
2298}
2299EXPORT_SYMBOL(extent_writepages);
2300
2301int extent_readpages(struct extent_io_tree *tree,
2302 struct address_space *mapping,
2303 struct list_head *pages, unsigned nr_pages,
2304 get_extent_t get_extent)
2305{
2306 struct bio *bio = NULL;
2307 unsigned page_idx;
2308 struct pagevec pvec;
2309
2310 pagevec_init(&pvec, 0);
2311 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2312 struct page *page = list_entry(pages->prev, struct page, lru);
2313
2314 prefetchw(&page->flags);
2315 list_del(&page->lru);
2316 /*
2317 * what we want to do here is call add_to_page_cache_lru,
2318 * but that isn't exported, so we reproduce it here
2319 */
2320 if (!add_to_page_cache(page, mapping,
2321 page->index, GFP_KERNEL)) {
2322
2323 /* open coding of lru_cache_add, also not exported */
2324 page_cache_get(page);
2325 if (!pagevec_add(&pvec, page))
2326 __pagevec_lru_add(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002327 __extent_read_full_page(tree, page, get_extent,
2328 &bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002329 }
2330 page_cache_release(page);
2331 }
2332 if (pagevec_count(&pvec))
2333 __pagevec_lru_add(&pvec);
2334 BUG_ON(!list_empty(pages));
2335 if (bio)
Chris Masonf1885912008-04-09 16:28:12 -04002336 submit_one_bio(READ, bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002337 return 0;
2338}
2339EXPORT_SYMBOL(extent_readpages);
2340
2341/*
2342 * basic invalidatepage code, this waits on any locked or writeback
2343 * ranges corresponding to the page, and then deletes any extent state
2344 * records from the tree
2345 */
2346int extent_invalidatepage(struct extent_io_tree *tree,
2347 struct page *page, unsigned long offset)
2348{
2349 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2350 u64 end = start + PAGE_CACHE_SIZE - 1;
2351 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2352
2353 start += (offset + blocksize -1) & ~(blocksize - 1);
2354 if (start > end)
2355 return 0;
2356
2357 lock_extent(tree, start, end, GFP_NOFS);
2358 wait_on_extent_writeback(tree, start, end);
2359 clear_extent_bit(tree, start, end,
2360 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2361 1, 1, GFP_NOFS);
2362 return 0;
2363}
2364EXPORT_SYMBOL(extent_invalidatepage);
2365
2366/*
2367 * simple commit_write call, set_range_dirty is used to mark both
2368 * the pages and the extent records as dirty
2369 */
2370int extent_commit_write(struct extent_io_tree *tree,
2371 struct inode *inode, struct page *page,
2372 unsigned from, unsigned to)
2373{
2374 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2375
2376 set_page_extent_mapped(page);
2377 set_page_dirty(page);
2378
2379 if (pos > inode->i_size) {
2380 i_size_write(inode, pos);
2381 mark_inode_dirty(inode);
2382 }
2383 return 0;
2384}
2385EXPORT_SYMBOL(extent_commit_write);
2386
2387int extent_prepare_write(struct extent_io_tree *tree,
2388 struct inode *inode, struct page *page,
2389 unsigned from, unsigned to, get_extent_t *get_extent)
2390{
2391 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2392 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2393 u64 block_start;
2394 u64 orig_block_start;
2395 u64 block_end;
2396 u64 cur_end;
2397 struct extent_map *em;
2398 unsigned blocksize = 1 << inode->i_blkbits;
2399 size_t page_offset = 0;
2400 size_t block_off_start;
2401 size_t block_off_end;
2402 int err = 0;
2403 int iocount = 0;
2404 int ret = 0;
2405 int isnew;
2406
2407 set_page_extent_mapped(page);
2408
2409 block_start = (page_start + from) & ~((u64)blocksize - 1);
2410 block_end = (page_start + to - 1) | (blocksize - 1);
2411 orig_block_start = block_start;
2412
2413 lock_extent(tree, page_start, page_end, GFP_NOFS);
2414 while(block_start <= block_end) {
2415 em = get_extent(inode, page, page_offset, block_start,
2416 block_end - block_start + 1, 1);
2417 if (IS_ERR(em) || !em) {
2418 goto err;
2419 }
2420 cur_end = min(block_end, extent_map_end(em) - 1);
2421 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2422 block_off_end = block_off_start + blocksize;
2423 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2424
2425 if (!PageUptodate(page) && isnew &&
2426 (block_off_end > to || block_off_start < from)) {
2427 void *kaddr;
2428
2429 kaddr = kmap_atomic(page, KM_USER0);
2430 if (block_off_end > to)
2431 memset(kaddr + to, 0, block_off_end - to);
2432 if (block_off_start < from)
2433 memset(kaddr + block_off_start, 0,
2434 from - block_off_start);
2435 flush_dcache_page(page);
2436 kunmap_atomic(kaddr, KM_USER0);
2437 }
2438 if ((em->block_start != EXTENT_MAP_HOLE &&
2439 em->block_start != EXTENT_MAP_INLINE) &&
2440 !isnew && !PageUptodate(page) &&
2441 (block_off_end > to || block_off_start < from) &&
2442 !test_range_bit(tree, block_start, cur_end,
2443 EXTENT_UPTODATE, 1)) {
2444 u64 sector;
2445 u64 extent_offset = block_start - em->start;
2446 size_t iosize;
2447 sector = (em->block_start + extent_offset) >> 9;
2448 iosize = (cur_end - block_start + blocksize) &
2449 ~((u64)blocksize - 1);
2450 /*
2451 * we've already got the extent locked, but we
2452 * need to split the state such that our end_bio
2453 * handler can clear the lock.
2454 */
2455 set_extent_bit(tree, block_start,
2456 block_start + iosize - 1,
2457 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2458 ret = submit_extent_page(READ, tree, page,
2459 sector, iosize, page_offset, em->bdev,
2460 NULL, 1,
Chris Masonf1885912008-04-09 16:28:12 -04002461 end_bio_extent_preparewrite, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002462 iocount++;
2463 block_start = block_start + iosize;
2464 } else {
2465 set_extent_uptodate(tree, block_start, cur_end,
2466 GFP_NOFS);
2467 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2468 block_start = cur_end + 1;
2469 }
2470 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2471 free_extent_map(em);
2472 }
2473 if (iocount) {
2474 wait_extent_bit(tree, orig_block_start,
2475 block_end, EXTENT_LOCKED);
2476 }
2477 check_page_uptodate(tree, page);
2478err:
2479 /* FIXME, zero out newly allocated blocks on error */
2480 return err;
2481}
2482EXPORT_SYMBOL(extent_prepare_write);
2483
2484/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002485 * a helper for releasepage, this tests for areas of the page that
2486 * are locked or under IO and drops the related state bits if it is safe
2487 * to drop the page.
2488 */
2489int try_release_extent_state(struct extent_map_tree *map,
2490 struct extent_io_tree *tree, struct page *page,
2491 gfp_t mask)
2492{
2493 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2494 u64 end = start + PAGE_CACHE_SIZE - 1;
2495 int ret = 1;
2496
2497 if (test_range_bit(tree, start, end, EXTENT_IOBITS, 0))
2498 ret = 0;
2499 else {
2500 if ((mask & GFP_NOFS) == GFP_NOFS)
2501 mask = GFP_NOFS;
2502 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2503 1, 1, mask);
2504 }
2505 return ret;
2506}
2507EXPORT_SYMBOL(try_release_extent_state);
2508
2509/*
Chris Masond1310b22008-01-24 16:13:08 -05002510 * a helper for releasepage. As long as there are no locked extents
2511 * in the range corresponding to the page, both state records and extent
2512 * map records are removed
2513 */
2514int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002515 struct extent_io_tree *tree, struct page *page,
2516 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002517{
2518 struct extent_map *em;
2519 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2520 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002521
Chris Mason70dec802008-01-29 09:59:12 -05002522 if ((mask & __GFP_WAIT) &&
2523 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002524 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002525 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002526 len = end - start + 1;
Chris Mason70dec802008-01-29 09:59:12 -05002527 spin_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002528 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002529 if (!em || IS_ERR(em)) {
2530 spin_unlock(&map->lock);
2531 break;
2532 }
2533 if (em->start != start) {
2534 spin_unlock(&map->lock);
2535 free_extent_map(em);
2536 break;
2537 }
2538 if (!test_range_bit(tree, em->start,
2539 extent_map_end(em) - 1,
2540 EXTENT_LOCKED, 0)) {
2541 remove_extent_mapping(map, em);
2542 /* once for the rb tree */
2543 free_extent_map(em);
2544 }
2545 start = extent_map_end(em);
Chris Masond1310b22008-01-24 16:13:08 -05002546 spin_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002547
2548 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002549 free_extent_map(em);
2550 }
Chris Masond1310b22008-01-24 16:13:08 -05002551 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002552 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002553}
2554EXPORT_SYMBOL(try_release_extent_mapping);
2555
2556sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2557 get_extent_t *get_extent)
2558{
2559 struct inode *inode = mapping->host;
2560 u64 start = iblock << inode->i_blkbits;
2561 sector_t sector = 0;
2562 struct extent_map *em;
2563
2564 em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0);
2565 if (!em || IS_ERR(em))
2566 return 0;
2567
2568 if (em->block_start == EXTENT_MAP_INLINE ||
2569 em->block_start == EXTENT_MAP_HOLE)
2570 goto out;
2571
2572 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002573out:
2574 free_extent_map(em);
2575 return sector;
2576}
2577
2578static int add_lru(struct extent_io_tree *tree, struct extent_buffer *eb)
2579{
2580 if (list_empty(&eb->lru)) {
2581 extent_buffer_get(eb);
2582 list_add(&eb->lru, &tree->buffer_lru);
2583 tree->lru_size++;
2584 if (tree->lru_size >= BUFFER_LRU_MAX) {
2585 struct extent_buffer *rm;
2586 rm = list_entry(tree->buffer_lru.prev,
2587 struct extent_buffer, lru);
2588 tree->lru_size--;
2589 list_del_init(&rm->lru);
2590 free_extent_buffer(rm);
2591 }
2592 } else
2593 list_move(&eb->lru, &tree->buffer_lru);
2594 return 0;
2595}
2596static struct extent_buffer *find_lru(struct extent_io_tree *tree,
2597 u64 start, unsigned long len)
2598{
2599 struct list_head *lru = &tree->buffer_lru;
2600 struct list_head *cur = lru->next;
2601 struct extent_buffer *eb;
2602
2603 if (list_empty(lru))
2604 return NULL;
2605
2606 do {
2607 eb = list_entry(cur, struct extent_buffer, lru);
2608 if (eb->start == start && eb->len == len) {
2609 extent_buffer_get(eb);
2610 return eb;
2611 }
2612 cur = cur->next;
2613 } while (cur != lru);
2614 return NULL;
2615}
2616
2617static inline unsigned long num_extent_pages(u64 start, u64 len)
2618{
2619 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2620 (start >> PAGE_CACHE_SHIFT);
2621}
2622
2623static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2624 unsigned long i)
2625{
2626 struct page *p;
2627 struct address_space *mapping;
2628
2629 if (i == 0)
2630 return eb->first_page;
2631 i += eb->start >> PAGE_CACHE_SHIFT;
2632 mapping = eb->first_page->mapping;
2633 read_lock_irq(&mapping->tree_lock);
2634 p = radix_tree_lookup(&mapping->page_tree, i);
2635 read_unlock_irq(&mapping->tree_lock);
2636 return p;
2637}
2638
Chris Masonce9adaa2008-04-09 16:28:12 -04002639int release_extent_buffer_tail_pages(struct extent_buffer *eb)
2640{
2641 unsigned long num_pages = num_extent_pages(eb->start, eb->len);
2642 struct page *page;
2643 unsigned long i;
2644
2645 if (num_pages == 1)
2646 return 0;
2647 for (i = 1; i < num_pages; i++) {
2648 page = extent_buffer_page(eb, i);
2649 page_cache_release(page);
2650 }
2651 return 0;
2652}
2653
2654
Chris Mason728131d2008-04-09 16:28:12 -04002655int invalidate_extent_lru(struct extent_io_tree *tree, u64 start,
2656 unsigned long len)
2657{
2658 struct list_head *lru = &tree->buffer_lru;
2659 struct list_head *cur = lru->next;
2660 struct extent_buffer *eb;
2661 int found = 0;
2662
2663 spin_lock(&tree->lru_lock);
2664 if (list_empty(lru))
2665 goto out;
2666
2667 do {
2668 eb = list_entry(cur, struct extent_buffer, lru);
2669 if (eb->start <= start && eb->start + eb->len > start) {
2670 eb->flags &= ~EXTENT_UPTODATE;
2671 }
Chris Mason728131d2008-04-09 16:28:12 -04002672 cur = cur->next;
2673 } while (cur != lru);
2674out:
2675 spin_unlock(&tree->lru_lock);
2676 return found;
2677}
2678
Chris Masond1310b22008-01-24 16:13:08 -05002679static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2680 u64 start,
2681 unsigned long len,
2682 gfp_t mask)
2683{
2684 struct extent_buffer *eb = NULL;
Chris Mason2d2ae542008-03-26 16:24:23 -04002685 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05002686
2687 spin_lock(&tree->lru_lock);
2688 eb = find_lru(tree, start, len);
2689 spin_unlock(&tree->lru_lock);
2690 if (eb) {
2691 return eb;
2692 }
2693
2694 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
2695 INIT_LIST_HEAD(&eb->lru);
2696 eb->start = start;
2697 eb->len = len;
Chris Mason2d2ae542008-03-26 16:24:23 -04002698 spin_lock_irqsave(&leak_lock, flags);
2699 list_add(&eb->leak_list, &buffers);
2700 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05002701 atomic_set(&eb->refs, 1);
2702
2703 return eb;
2704}
2705
2706static void __free_extent_buffer(struct extent_buffer *eb)
2707{
Chris Mason2d2ae542008-03-26 16:24:23 -04002708 unsigned long flags;
2709 spin_lock_irqsave(&leak_lock, flags);
2710 list_del(&eb->leak_list);
2711 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05002712 kmem_cache_free(extent_buffer_cache, eb);
2713}
2714
2715struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2716 u64 start, unsigned long len,
2717 struct page *page0,
2718 gfp_t mask)
2719{
2720 unsigned long num_pages = num_extent_pages(start, len);
2721 unsigned long i;
2722 unsigned long index = start >> PAGE_CACHE_SHIFT;
2723 struct extent_buffer *eb;
2724 struct page *p;
2725 struct address_space *mapping = tree->mapping;
2726 int uptodate = 1;
2727
2728 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04002729 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05002730 return NULL;
2731
2732 if (eb->flags & EXTENT_BUFFER_FILLED)
2733 goto lru_add;
2734
2735 if (page0) {
2736 eb->first_page = page0;
2737 i = 1;
2738 index++;
2739 page_cache_get(page0);
2740 mark_page_accessed(page0);
2741 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002742 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04002743 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002744 } else {
2745 i = 0;
2746 }
2747 for (; i < num_pages; i++, index++) {
2748 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
2749 if (!p) {
2750 WARN_ON(1);
2751 goto fail;
2752 }
2753 set_page_extent_mapped(p);
2754 mark_page_accessed(p);
2755 if (i == 0) {
2756 eb->first_page = p;
2757 set_page_extent_head(p, len);
2758 } else {
2759 set_page_private(p, EXTENT_PAGE_PRIVATE);
2760 }
2761 if (!PageUptodate(p))
2762 uptodate = 0;
2763 unlock_page(p);
2764 }
2765 if (uptodate)
2766 eb->flags |= EXTENT_UPTODATE;
2767 eb->flags |= EXTENT_BUFFER_FILLED;
2768
2769lru_add:
2770 spin_lock(&tree->lru_lock);
2771 add_lru(tree, eb);
2772 spin_unlock(&tree->lru_lock);
2773 return eb;
2774
2775fail:
2776 spin_lock(&tree->lru_lock);
2777 list_del_init(&eb->lru);
2778 spin_unlock(&tree->lru_lock);
2779 if (!atomic_dec_and_test(&eb->refs))
2780 return NULL;
2781 for (index = 1; index < i; index++) {
2782 page_cache_release(extent_buffer_page(eb, index));
2783 }
2784 if (i > 0)
2785 page_cache_release(extent_buffer_page(eb, 0));
2786 __free_extent_buffer(eb);
2787 return NULL;
2788}
2789EXPORT_SYMBOL(alloc_extent_buffer);
2790
2791struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
2792 u64 start, unsigned long len,
2793 gfp_t mask)
2794{
2795 unsigned long num_pages = num_extent_pages(start, len);
2796 unsigned long i;
2797 unsigned long index = start >> PAGE_CACHE_SHIFT;
2798 struct extent_buffer *eb;
2799 struct page *p;
2800 struct address_space *mapping = tree->mapping;
2801 int uptodate = 1;
2802
2803 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04002804 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05002805 return NULL;
2806
2807 if (eb->flags & EXTENT_BUFFER_FILLED)
2808 goto lru_add;
2809
2810 for (i = 0; i < num_pages; i++, index++) {
2811 p = find_lock_page(mapping, index);
2812 if (!p) {
2813 goto fail;
2814 }
2815 set_page_extent_mapped(p);
2816 mark_page_accessed(p);
2817
2818 if (i == 0) {
2819 eb->first_page = p;
2820 set_page_extent_head(p, len);
2821 } else {
2822 set_page_private(p, EXTENT_PAGE_PRIVATE);
2823 }
2824
2825 if (!PageUptodate(p))
2826 uptodate = 0;
2827 unlock_page(p);
2828 }
2829 if (uptodate)
2830 eb->flags |= EXTENT_UPTODATE;
2831 eb->flags |= EXTENT_BUFFER_FILLED;
2832
2833lru_add:
2834 spin_lock(&tree->lru_lock);
2835 add_lru(tree, eb);
2836 spin_unlock(&tree->lru_lock);
2837 return eb;
2838fail:
2839 spin_lock(&tree->lru_lock);
2840 list_del_init(&eb->lru);
2841 spin_unlock(&tree->lru_lock);
2842 if (!atomic_dec_and_test(&eb->refs))
2843 return NULL;
2844 for (index = 1; index < i; index++) {
2845 page_cache_release(extent_buffer_page(eb, index));
2846 }
2847 if (i > 0)
2848 page_cache_release(extent_buffer_page(eb, 0));
2849 __free_extent_buffer(eb);
2850 return NULL;
2851}
2852EXPORT_SYMBOL(find_extent_buffer);
2853
2854void free_extent_buffer(struct extent_buffer *eb)
2855{
2856 unsigned long i;
2857 unsigned long num_pages;
2858
2859 if (!eb)
2860 return;
2861
2862 if (!atomic_dec_and_test(&eb->refs))
2863 return;
2864
2865 WARN_ON(!list_empty(&eb->lru));
2866 num_pages = num_extent_pages(eb->start, eb->len);
2867
2868 for (i = 1; i < num_pages; i++) {
2869 page_cache_release(extent_buffer_page(eb, i));
2870 }
2871 page_cache_release(extent_buffer_page(eb, 0));
2872 __free_extent_buffer(eb);
2873}
2874EXPORT_SYMBOL(free_extent_buffer);
2875
2876int clear_extent_buffer_dirty(struct extent_io_tree *tree,
2877 struct extent_buffer *eb)
2878{
2879 int set;
2880 unsigned long i;
2881 unsigned long num_pages;
2882 struct page *page;
2883
2884 u64 start = eb->start;
2885 u64 end = start + eb->len - 1;
2886
2887 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
2888 num_pages = num_extent_pages(eb->start, eb->len);
2889
2890 for (i = 0; i < num_pages; i++) {
2891 page = extent_buffer_page(eb, i);
2892 lock_page(page);
2893 if (i == 0)
2894 set_page_extent_head(page, eb->len);
2895 else
2896 set_page_private(page, EXTENT_PAGE_PRIVATE);
2897
2898 /*
2899 * if we're on the last page or the first page and the
2900 * block isn't aligned on a page boundary, do extra checks
2901 * to make sure we don't clean page that is partially dirty
2902 */
2903 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2904 ((i == num_pages - 1) &&
2905 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2906 start = (u64)page->index << PAGE_CACHE_SHIFT;
2907 end = start + PAGE_CACHE_SIZE - 1;
2908 if (test_range_bit(tree, start, end,
2909 EXTENT_DIRTY, 0)) {
2910 unlock_page(page);
2911 continue;
2912 }
2913 }
2914 clear_page_dirty_for_io(page);
Chris Mason70dec802008-01-29 09:59:12 -05002915 read_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05002916 if (!PageDirty(page)) {
2917 radix_tree_tag_clear(&page->mapping->page_tree,
2918 page_index(page),
2919 PAGECACHE_TAG_DIRTY);
2920 }
Chris Mason70dec802008-01-29 09:59:12 -05002921 read_unlock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05002922 unlock_page(page);
2923 }
2924 return 0;
2925}
2926EXPORT_SYMBOL(clear_extent_buffer_dirty);
2927
2928int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
2929 struct extent_buffer *eb)
2930{
2931 return wait_on_extent_writeback(tree, eb->start,
2932 eb->start + eb->len - 1);
2933}
2934EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2935
2936int set_extent_buffer_dirty(struct extent_io_tree *tree,
2937 struct extent_buffer *eb)
2938{
2939 unsigned long i;
2940 unsigned long num_pages;
2941
2942 num_pages = num_extent_pages(eb->start, eb->len);
2943 for (i = 0; i < num_pages; i++) {
2944 struct page *page = extent_buffer_page(eb, i);
2945 /* writepage may need to do something special for the
2946 * first page, we have to make sure page->private is
2947 * properly set. releasepage may drop page->private
2948 * on us if the page isn't already dirty.
2949 */
2950 if (i == 0) {
2951 lock_page(page);
2952 set_page_extent_head(page, eb->len);
2953 } else if (PagePrivate(page) &&
2954 page->private != EXTENT_PAGE_PRIVATE) {
2955 lock_page(page);
2956 set_page_extent_mapped(page);
2957 unlock_page(page);
2958 }
2959 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
2960 if (i == 0)
2961 unlock_page(page);
2962 }
2963 return set_extent_dirty(tree, eb->start,
2964 eb->start + eb->len - 1, GFP_NOFS);
2965}
2966EXPORT_SYMBOL(set_extent_buffer_dirty);
2967
Chris Mason1259ab72008-05-12 13:39:03 -04002968int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
2969 struct extent_buffer *eb)
2970{
2971 unsigned long i;
2972 struct page *page;
2973 unsigned long num_pages;
2974
2975 num_pages = num_extent_pages(eb->start, eb->len);
2976 eb->flags &= ~EXTENT_UPTODATE;
2977
2978 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2979 GFP_NOFS);
2980 for (i = 0; i < num_pages; i++) {
2981 page = extent_buffer_page(eb, i);
2982 ClearPageUptodate(page);
2983 }
2984 return 0;
2985}
2986
Chris Masond1310b22008-01-24 16:13:08 -05002987int set_extent_buffer_uptodate(struct extent_io_tree *tree,
2988 struct extent_buffer *eb)
2989{
2990 unsigned long i;
2991 struct page *page;
2992 unsigned long num_pages;
2993
2994 num_pages = num_extent_pages(eb->start, eb->len);
2995
2996 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2997 GFP_NOFS);
2998 for (i = 0; i < num_pages; i++) {
2999 page = extent_buffer_page(eb, i);
3000 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3001 ((i == num_pages - 1) &&
3002 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3003 check_page_uptodate(tree, page);
3004 continue;
3005 }
3006 SetPageUptodate(page);
3007 }
3008 return 0;
3009}
3010EXPORT_SYMBOL(set_extent_buffer_uptodate);
3011
Chris Masonce9adaa2008-04-09 16:28:12 -04003012int extent_range_uptodate(struct extent_io_tree *tree,
3013 u64 start, u64 end)
3014{
3015 struct page *page;
3016 int ret;
3017 int pg_uptodate = 1;
3018 int uptodate;
3019 unsigned long index;
3020
3021 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3022 if (ret)
3023 return 1;
3024 while(start <= end) {
3025 index = start >> PAGE_CACHE_SHIFT;
3026 page = find_get_page(tree->mapping, index);
3027 uptodate = PageUptodate(page);
3028 page_cache_release(page);
3029 if (!uptodate) {
3030 pg_uptodate = 0;
3031 break;
3032 }
3033 start += PAGE_CACHE_SIZE;
3034 }
3035 return pg_uptodate;
3036}
3037
Chris Masond1310b22008-01-24 16:13:08 -05003038int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003039 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003040{
Chris Mason728131d2008-04-09 16:28:12 -04003041 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003042 unsigned long num_pages;
3043 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003044 struct page *page;
3045 int pg_uptodate = 1;
3046
Chris Masond1310b22008-01-24 16:13:08 -05003047 if (eb->flags & EXTENT_UPTODATE)
Chris Mason42352982008-04-28 16:40:52 -04003048 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003049
Chris Mason42352982008-04-28 16:40:52 -04003050 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003051 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003052 if (ret)
3053 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003054
3055 num_pages = num_extent_pages(eb->start, eb->len);
3056 for (i = 0; i < num_pages; i++) {
3057 page = extent_buffer_page(eb, i);
3058 if (!PageUptodate(page)) {
3059 pg_uptodate = 0;
3060 break;
3061 }
3062 }
Chris Mason42352982008-04-28 16:40:52 -04003063 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003064}
3065EXPORT_SYMBOL(extent_buffer_uptodate);
3066
3067int read_extent_buffer_pages(struct extent_io_tree *tree,
3068 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003069 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003070 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003071{
3072 unsigned long i;
3073 unsigned long start_i;
3074 struct page *page;
3075 int err;
3076 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003077 int locked_pages = 0;
3078 int all_uptodate = 1;
3079 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003080 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003081 struct bio *bio = NULL;
3082
Chris Masond1310b22008-01-24 16:13:08 -05003083 if (eb->flags & EXTENT_UPTODATE)
3084 return 0;
3085
Chris Masonce9adaa2008-04-09 16:28:12 -04003086 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003087 EXTENT_UPTODATE, 1)) {
3088 return 0;
3089 }
3090
3091 if (start) {
3092 WARN_ON(start < eb->start);
3093 start_i = (start >> PAGE_CACHE_SHIFT) -
3094 (eb->start >> PAGE_CACHE_SHIFT);
3095 } else {
3096 start_i = 0;
3097 }
3098
3099 num_pages = num_extent_pages(eb->start, eb->len);
3100 for (i = start_i; i < num_pages; i++) {
3101 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003102 if (!wait) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003103 if (TestSetPageLocked(page))
3104 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003105 } else {
3106 lock_page(page);
3107 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003108 locked_pages++;
Chris Masond1310b22008-01-24 16:13:08 -05003109 if (!PageUptodate(page)) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003110 all_uptodate = 0;
3111 }
3112 }
3113 if (all_uptodate) {
3114 if (start_i == 0)
3115 eb->flags |= EXTENT_UPTODATE;
3116 goto unlock_exit;
3117 }
3118
3119 for (i = start_i; i < num_pages; i++) {
3120 page = extent_buffer_page(eb, i);
3121 if (inc_all_pages)
3122 page_cache_get(page);
3123 if (!PageUptodate(page)) {
3124 if (start_i == 0)
3125 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003126 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003127 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003128 get_extent, &bio,
3129 mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05003130 if (err) {
3131 ret = err;
3132 }
3133 } else {
3134 unlock_page(page);
3135 }
3136 }
3137
Chris Masona86c12c2008-02-07 10:50:54 -05003138 if (bio)
Chris Masonf1885912008-04-09 16:28:12 -04003139 submit_one_bio(READ, bio, mirror_num);
Chris Masona86c12c2008-02-07 10:50:54 -05003140
Chris Masond1310b22008-01-24 16:13:08 -05003141 if (ret || !wait) {
3142 return ret;
3143 }
Chris Masond1310b22008-01-24 16:13:08 -05003144 for (i = start_i; i < num_pages; i++) {
3145 page = extent_buffer_page(eb, i);
3146 wait_on_page_locked(page);
3147 if (!PageUptodate(page)) {
3148 ret = -EIO;
3149 }
3150 }
3151 if (!ret)
3152 eb->flags |= EXTENT_UPTODATE;
3153 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003154
3155unlock_exit:
3156 i = start_i;
3157 while(locked_pages > 0) {
3158 page = extent_buffer_page(eb, i);
3159 i++;
3160 unlock_page(page);
3161 locked_pages--;
3162 }
3163 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003164}
3165EXPORT_SYMBOL(read_extent_buffer_pages);
3166
3167void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3168 unsigned long start,
3169 unsigned long len)
3170{
3171 size_t cur;
3172 size_t offset;
3173 struct page *page;
3174 char *kaddr;
3175 char *dst = (char *)dstv;
3176 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3177 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003178
3179 WARN_ON(start > eb->len);
3180 WARN_ON(start + len > eb->start + eb->len);
3181
3182 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3183
3184 while(len > 0) {
3185 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003186
3187 cur = min(len, (PAGE_CACHE_SIZE - offset));
3188 kaddr = kmap_atomic(page, KM_USER1);
3189 memcpy(dst, kaddr + offset, cur);
3190 kunmap_atomic(kaddr, KM_USER1);
3191
3192 dst += cur;
3193 len -= cur;
3194 offset = 0;
3195 i++;
3196 }
3197}
3198EXPORT_SYMBOL(read_extent_buffer);
3199
3200int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3201 unsigned long min_len, char **token, char **map,
3202 unsigned long *map_start,
3203 unsigned long *map_len, int km)
3204{
3205 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3206 char *kaddr;
3207 struct page *p;
3208 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3209 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3210 unsigned long end_i = (start_offset + start + min_len - 1) >>
3211 PAGE_CACHE_SHIFT;
3212
3213 if (i != end_i)
3214 return -EINVAL;
3215
3216 if (i == 0) {
3217 offset = start_offset;
3218 *map_start = 0;
3219 } else {
3220 offset = 0;
3221 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3222 }
3223 if (start + min_len > eb->len) {
3224printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3225 WARN_ON(1);
3226 }
3227
3228 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003229 kaddr = kmap_atomic(p, km);
3230 *token = kaddr;
3231 *map = kaddr + offset;
3232 *map_len = PAGE_CACHE_SIZE - offset;
3233 return 0;
3234}
3235EXPORT_SYMBOL(map_private_extent_buffer);
3236
3237int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3238 unsigned long min_len,
3239 char **token, char **map,
3240 unsigned long *map_start,
3241 unsigned long *map_len, int km)
3242{
3243 int err;
3244 int save = 0;
3245 if (eb->map_token) {
3246 unmap_extent_buffer(eb, eb->map_token, km);
3247 eb->map_token = NULL;
3248 save = 1;
3249 }
3250 err = map_private_extent_buffer(eb, start, min_len, token, map,
3251 map_start, map_len, km);
3252 if (!err && save) {
3253 eb->map_token = *token;
3254 eb->kaddr = *map;
3255 eb->map_start = *map_start;
3256 eb->map_len = *map_len;
3257 }
3258 return err;
3259}
3260EXPORT_SYMBOL(map_extent_buffer);
3261
3262void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3263{
3264 kunmap_atomic(token, km);
3265}
3266EXPORT_SYMBOL(unmap_extent_buffer);
3267
3268int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3269 unsigned long start,
3270 unsigned long len)
3271{
3272 size_t cur;
3273 size_t offset;
3274 struct page *page;
3275 char *kaddr;
3276 char *ptr = (char *)ptrv;
3277 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3278 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3279 int ret = 0;
3280
3281 WARN_ON(start > eb->len);
3282 WARN_ON(start + len > eb->start + eb->len);
3283
3284 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3285
3286 while(len > 0) {
3287 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003288
3289 cur = min(len, (PAGE_CACHE_SIZE - offset));
3290
3291 kaddr = kmap_atomic(page, KM_USER0);
3292 ret = memcmp(ptr, kaddr + offset, cur);
3293 kunmap_atomic(kaddr, KM_USER0);
3294 if (ret)
3295 break;
3296
3297 ptr += cur;
3298 len -= cur;
3299 offset = 0;
3300 i++;
3301 }
3302 return ret;
3303}
3304EXPORT_SYMBOL(memcmp_extent_buffer);
3305
3306void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3307 unsigned long start, unsigned long len)
3308{
3309 size_t cur;
3310 size_t offset;
3311 struct page *page;
3312 char *kaddr;
3313 char *src = (char *)srcv;
3314 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3315 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3316
3317 WARN_ON(start > eb->len);
3318 WARN_ON(start + len > eb->start + eb->len);
3319
3320 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3321
3322 while(len > 0) {
3323 page = extent_buffer_page(eb, i);
3324 WARN_ON(!PageUptodate(page));
3325
3326 cur = min(len, PAGE_CACHE_SIZE - offset);
3327 kaddr = kmap_atomic(page, KM_USER1);
3328 memcpy(kaddr + offset, src, cur);
3329 kunmap_atomic(kaddr, KM_USER1);
3330
3331 src += cur;
3332 len -= cur;
3333 offset = 0;
3334 i++;
3335 }
3336}
3337EXPORT_SYMBOL(write_extent_buffer);
3338
3339void memset_extent_buffer(struct extent_buffer *eb, char c,
3340 unsigned long start, unsigned long len)
3341{
3342 size_t cur;
3343 size_t offset;
3344 struct page *page;
3345 char *kaddr;
3346 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3347 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3348
3349 WARN_ON(start > eb->len);
3350 WARN_ON(start + len > eb->start + eb->len);
3351
3352 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3353
3354 while(len > 0) {
3355 page = extent_buffer_page(eb, i);
3356 WARN_ON(!PageUptodate(page));
3357
3358 cur = min(len, PAGE_CACHE_SIZE - offset);
3359 kaddr = kmap_atomic(page, KM_USER0);
3360 memset(kaddr + offset, c, cur);
3361 kunmap_atomic(kaddr, KM_USER0);
3362
3363 len -= cur;
3364 offset = 0;
3365 i++;
3366 }
3367}
3368EXPORT_SYMBOL(memset_extent_buffer);
3369
3370void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3371 unsigned long dst_offset, unsigned long src_offset,
3372 unsigned long len)
3373{
3374 u64 dst_len = dst->len;
3375 size_t cur;
3376 size_t offset;
3377 struct page *page;
3378 char *kaddr;
3379 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3380 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3381
3382 WARN_ON(src->len != dst_len);
3383
3384 offset = (start_offset + dst_offset) &
3385 ((unsigned long)PAGE_CACHE_SIZE - 1);
3386
3387 while(len > 0) {
3388 page = extent_buffer_page(dst, i);
3389 WARN_ON(!PageUptodate(page));
3390
3391 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3392
3393 kaddr = kmap_atomic(page, KM_USER0);
3394 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3395 kunmap_atomic(kaddr, KM_USER0);
3396
3397 src_offset += cur;
3398 len -= cur;
3399 offset = 0;
3400 i++;
3401 }
3402}
3403EXPORT_SYMBOL(copy_extent_buffer);
3404
3405static void move_pages(struct page *dst_page, struct page *src_page,
3406 unsigned long dst_off, unsigned long src_off,
3407 unsigned long len)
3408{
3409 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3410 if (dst_page == src_page) {
3411 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3412 } else {
3413 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3414 char *p = dst_kaddr + dst_off + len;
3415 char *s = src_kaddr + src_off + len;
3416
3417 while (len--)
3418 *--p = *--s;
3419
3420 kunmap_atomic(src_kaddr, KM_USER1);
3421 }
3422 kunmap_atomic(dst_kaddr, KM_USER0);
3423}
3424
3425static void copy_pages(struct page *dst_page, struct page *src_page,
3426 unsigned long dst_off, unsigned long src_off,
3427 unsigned long len)
3428{
3429 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3430 char *src_kaddr;
3431
3432 if (dst_page != src_page)
3433 src_kaddr = kmap_atomic(src_page, KM_USER1);
3434 else
3435 src_kaddr = dst_kaddr;
3436
3437 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3438 kunmap_atomic(dst_kaddr, KM_USER0);
3439 if (dst_page != src_page)
3440 kunmap_atomic(src_kaddr, KM_USER1);
3441}
3442
3443void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3444 unsigned long src_offset, unsigned long len)
3445{
3446 size_t cur;
3447 size_t dst_off_in_page;
3448 size_t src_off_in_page;
3449 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3450 unsigned long dst_i;
3451 unsigned long src_i;
3452
3453 if (src_offset + len > dst->len) {
3454 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3455 src_offset, len, dst->len);
3456 BUG_ON(1);
3457 }
3458 if (dst_offset + len > dst->len) {
3459 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3460 dst_offset, len, dst->len);
3461 BUG_ON(1);
3462 }
3463
3464 while(len > 0) {
3465 dst_off_in_page = (start_offset + dst_offset) &
3466 ((unsigned long)PAGE_CACHE_SIZE - 1);
3467 src_off_in_page = (start_offset + src_offset) &
3468 ((unsigned long)PAGE_CACHE_SIZE - 1);
3469
3470 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3471 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3472
3473 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3474 src_off_in_page));
3475 cur = min_t(unsigned long, cur,
3476 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3477
3478 copy_pages(extent_buffer_page(dst, dst_i),
3479 extent_buffer_page(dst, src_i),
3480 dst_off_in_page, src_off_in_page, cur);
3481
3482 src_offset += cur;
3483 dst_offset += cur;
3484 len -= cur;
3485 }
3486}
3487EXPORT_SYMBOL(memcpy_extent_buffer);
3488
3489void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3490 unsigned long src_offset, unsigned long len)
3491{
3492 size_t cur;
3493 size_t dst_off_in_page;
3494 size_t src_off_in_page;
3495 unsigned long dst_end = dst_offset + len - 1;
3496 unsigned long src_end = src_offset + len - 1;
3497 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3498 unsigned long dst_i;
3499 unsigned long src_i;
3500
3501 if (src_offset + len > dst->len) {
3502 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3503 src_offset, len, dst->len);
3504 BUG_ON(1);
3505 }
3506 if (dst_offset + len > dst->len) {
3507 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3508 dst_offset, len, dst->len);
3509 BUG_ON(1);
3510 }
3511 if (dst_offset < src_offset) {
3512 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3513 return;
3514 }
3515 while(len > 0) {
3516 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3517 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3518
3519 dst_off_in_page = (start_offset + dst_end) &
3520 ((unsigned long)PAGE_CACHE_SIZE - 1);
3521 src_off_in_page = (start_offset + src_end) &
3522 ((unsigned long)PAGE_CACHE_SIZE - 1);
3523
3524 cur = min_t(unsigned long, len, src_off_in_page + 1);
3525 cur = min(cur, dst_off_in_page + 1);
3526 move_pages(extent_buffer_page(dst, dst_i),
3527 extent_buffer_page(dst, src_i),
3528 dst_off_in_page - cur + 1,
3529 src_off_in_page - cur + 1, cur);
3530
3531 dst_end -= cur;
3532 src_end -= cur;
3533 len -= cur;
3534 }
3535}
3536EXPORT_SYMBOL(memmove_extent_buffer);