blob: af2d9a9300a729b837cc9055fe1098d9ecfa0c96 [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"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050020
21/* temporary define until extent_map moves out of btrfs */
22struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
23 unsigned long extra_flags,
24 void (*ctor)(void *, struct kmem_cache *,
25 unsigned long));
26
27static struct kmem_cache *extent_state_cache;
28static struct kmem_cache *extent_buffer_cache;
29
30static LIST_HEAD(buffers);
31static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040032
Chris Masonc8b97812008-10-29 14:49:59 -040033#define LEAK_DEBUG 1
Chris Mason4bef0842008-09-08 11:18:08 -040034#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -040035static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
Chris Mason4bef0842008-09-08 11:18:08 -040036#endif
Chris Masond1310b22008-01-24 16:13:08 -050037
Chris Masond1310b22008-01-24 16:13:08 -050038#define BUFFER_LRU_MAX 64
39
40struct tree_entry {
41 u64 start;
42 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050043 struct rb_node rb_node;
44};
45
46struct extent_page_data {
47 struct bio *bio;
48 struct extent_io_tree *tree;
49 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050050
51 /* tells writepage not to lock the state bits for this range
52 * it still does the unlocking
53 */
54 int extent_locked;
Chris Masond1310b22008-01-24 16:13:08 -050055};
56
57int __init extent_io_init(void)
58{
59 extent_state_cache = btrfs_cache_create("extent_state",
60 sizeof(struct extent_state), 0,
61 NULL);
62 if (!extent_state_cache)
63 return -ENOMEM;
64
65 extent_buffer_cache = btrfs_cache_create("extent_buffers",
66 sizeof(struct extent_buffer), 0,
67 NULL);
68 if (!extent_buffer_cache)
69 goto free_state_cache;
70 return 0;
71
72free_state_cache:
73 kmem_cache_destroy(extent_state_cache);
74 return -ENOMEM;
75}
76
77void extent_io_exit(void)
78{
79 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040080 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050081
82 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040083 state = list_entry(states.next, struct extent_state, leak_list);
Chris Mason70dec802008-01-29 09:59:12 -050084 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 -040085 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050086 kmem_cache_free(extent_state_cache, state);
87
88 }
89
Chris Mason2d2ae542008-03-26 16:24:23 -040090 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
92 printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs));
93 list_del(&eb->leak_list);
94 kmem_cache_free(extent_buffer_cache, eb);
95 }
Chris Masond1310b22008-01-24 16:13:08 -050096 if (extent_state_cache)
97 kmem_cache_destroy(extent_state_cache);
98 if (extent_buffer_cache)
99 kmem_cache_destroy(extent_buffer_cache);
100}
101
102void extent_io_tree_init(struct extent_io_tree *tree,
103 struct address_space *mapping, gfp_t mask)
104{
105 tree->state.rb_node = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400106 tree->buffer.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500107 tree->ops = NULL;
108 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500109 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400110 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500111 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500112}
113EXPORT_SYMBOL(extent_io_tree_init);
114
Chris Masond1310b22008-01-24 16:13:08 -0500115struct extent_state *alloc_extent_state(gfp_t mask)
116{
117 struct extent_state *state;
Chris Mason4bef0842008-09-08 11:18:08 -0400118#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
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 Mason4bef0842008-09-08 11:18:08 -0400128#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
137EXPORT_SYMBOL(alloc_extent_state);
138
139void free_extent_state(struct extent_state *state)
140{
Chris Masond1310b22008-01-24 16:13:08 -0500141 if (!state)
142 return;
143 if (atomic_dec_and_test(&state->refs)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400144#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400145 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400146#endif
Chris Mason70dec802008-01-29 09:59:12 -0500147 WARN_ON(state->tree);
Chris Mason4bef0842008-09-08 11:18:08 -0400148#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Masond1310b22008-01-24 16:13:08 -0500153 kmem_cache_free(extent_state_cache, state);
154 }
155}
156EXPORT_SYMBOL(free_extent_state);
157
158static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
159 struct rb_node *node)
160{
161 struct rb_node ** p = &root->rb_node;
162 struct rb_node * parent = NULL;
163 struct tree_entry *entry;
164
165 while(*p) {
166 parent = *p;
167 entry = rb_entry(parent, struct tree_entry, rb_node);
168
169 if (offset < entry->start)
170 p = &(*p)->rb_left;
171 else if (offset > entry->end)
172 p = &(*p)->rb_right;
173 else
174 return parent;
175 }
176
177 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500178 rb_link_node(node, parent, p);
179 rb_insert_color(node, root);
180 return NULL;
181}
182
Chris Mason80ea96b2008-02-01 14:51:59 -0500183static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500184 struct rb_node **prev_ret,
185 struct rb_node **next_ret)
186{
Chris Mason80ea96b2008-02-01 14:51:59 -0500187 struct rb_root *root = &tree->state;
Chris Masond1310b22008-01-24 16:13:08 -0500188 struct rb_node * n = root->rb_node;
189 struct rb_node *prev = NULL;
190 struct rb_node *orig_prev = NULL;
191 struct tree_entry *entry;
192 struct tree_entry *prev_entry = NULL;
193
194 while(n) {
195 entry = rb_entry(n, struct tree_entry, rb_node);
196 prev = n;
197 prev_entry = entry;
198
199 if (offset < entry->start)
200 n = n->rb_left;
201 else if (offset > entry->end)
202 n = n->rb_right;
Chris Mason80ea96b2008-02-01 14:51:59 -0500203 else {
Chris Masond1310b22008-01-24 16:13:08 -0500204 return n;
Chris Mason80ea96b2008-02-01 14:51:59 -0500205 }
Chris Masond1310b22008-01-24 16:13:08 -0500206 }
207
208 if (prev_ret) {
209 orig_prev = prev;
210 while(prev && offset > prev_entry->end) {
211 prev = rb_next(prev);
212 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
213 }
214 *prev_ret = prev;
215 prev = orig_prev;
216 }
217
218 if (next_ret) {
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 while(prev && offset < prev_entry->start) {
221 prev = rb_prev(prev);
222 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
223 }
224 *next_ret = prev;
225 }
226 return NULL;
227}
228
Chris Mason80ea96b2008-02-01 14:51:59 -0500229static inline struct rb_node *tree_search(struct extent_io_tree *tree,
230 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500231{
Chris Mason70dec802008-01-29 09:59:12 -0500232 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500233 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500234
Chris Mason80ea96b2008-02-01 14:51:59 -0500235 ret = __etree_search(tree, offset, &prev, NULL);
236 if (!ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500237 return prev;
Chris Mason80ea96b2008-02-01 14:51:59 -0500238 }
Chris Masond1310b22008-01-24 16:13:08 -0500239 return ret;
240}
241
Chris Mason6af118ce2008-07-22 11:18:07 -0400242static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
243 u64 offset, struct rb_node *node)
244{
245 struct rb_root *root = &tree->buffer;
246 struct rb_node ** p = &root->rb_node;
247 struct rb_node * parent = NULL;
248 struct extent_buffer *eb;
249
250 while(*p) {
251 parent = *p;
252 eb = rb_entry(parent, struct extent_buffer, rb_node);
253
254 if (offset < eb->start)
255 p = &(*p)->rb_left;
256 else if (offset > eb->start)
257 p = &(*p)->rb_right;
258 else
259 return eb;
260 }
261
262 rb_link_node(node, parent, p);
263 rb_insert_color(node, root);
264 return NULL;
265}
266
267static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
268 u64 offset)
269{
270 struct rb_root *root = &tree->buffer;
271 struct rb_node * n = root->rb_node;
272 struct extent_buffer *eb;
273
274 while(n) {
275 eb = rb_entry(n, struct extent_buffer, rb_node);
276 if (offset < eb->start)
277 n = n->rb_left;
278 else if (offset > eb->start)
279 n = n->rb_right;
280 else
281 return eb;
282 }
283 return NULL;
284}
285
Chris Masond1310b22008-01-24 16:13:08 -0500286/*
287 * utility function to look for merge candidates inside a given range.
288 * Any extents with matching state are merged together into a single
289 * extent in the tree. Extents with EXTENT_IO in their state field
290 * are not merged because the end_io handlers need to be able to do
291 * operations on them without sleeping (or doing allocations/splits).
292 *
293 * This should be called with the tree lock held.
294 */
295static int merge_state(struct extent_io_tree *tree,
296 struct extent_state *state)
297{
298 struct extent_state *other;
299 struct rb_node *other_node;
300
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400301 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500302 return 0;
303
304 other_node = rb_prev(&state->rb_node);
305 if (other_node) {
306 other = rb_entry(other_node, struct extent_state, rb_node);
307 if (other->end == state->start - 1 &&
308 other->state == state->state) {
309 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500310 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500311 rb_erase(&other->rb_node, &tree->state);
312 free_extent_state(other);
313 }
314 }
315 other_node = rb_next(&state->rb_node);
316 if (other_node) {
317 other = rb_entry(other_node, struct extent_state, rb_node);
318 if (other->start == state->end + 1 &&
319 other->state == state->state) {
320 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500321 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500322 rb_erase(&state->rb_node, &tree->state);
323 free_extent_state(state);
324 }
325 }
326 return 0;
327}
328
Chris Mason291d6732008-01-29 15:55:23 -0500329static void set_state_cb(struct extent_io_tree *tree,
330 struct extent_state *state,
331 unsigned long bits)
332{
333 if (tree->ops && tree->ops->set_bit_hook) {
334 tree->ops->set_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500335 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500336 }
337}
338
339static void clear_state_cb(struct extent_io_tree *tree,
340 struct extent_state *state,
341 unsigned long bits)
342{
343 if (tree->ops && tree->ops->set_bit_hook) {
344 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500345 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500346 }
347}
348
Chris Masond1310b22008-01-24 16:13:08 -0500349/*
350 * insert an extent_state struct into the tree. 'bits' are set on the
351 * struct before it is inserted.
352 *
353 * This may return -EEXIST if the extent is already there, in which case the
354 * state struct is freed.
355 *
356 * The tree lock is not taken internally. This is a utility function and
357 * probably isn't what you want to call (see set/clear_extent_bit).
358 */
359static int insert_state(struct extent_io_tree *tree,
360 struct extent_state *state, u64 start, u64 end,
361 int bits)
362{
363 struct rb_node *node;
364
365 if (end < start) {
366 printk("end < start %Lu %Lu\n", end, start);
367 WARN_ON(1);
368 }
369 if (bits & EXTENT_DIRTY)
370 tree->dirty_bytes += end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500371 set_state_cb(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500372 state->state |= bits;
373 state->start = start;
374 state->end = end;
375 node = tree_insert(&tree->state, end, &state->rb_node);
376 if (node) {
377 struct extent_state *found;
378 found = rb_entry(node, struct extent_state, rb_node);
379 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
380 free_extent_state(state);
381 return -EEXIST;
382 }
Chris Mason70dec802008-01-29 09:59:12 -0500383 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500384 merge_state(tree, state);
385 return 0;
386}
387
388/*
389 * split a given extent state struct in two, inserting the preallocated
390 * struct 'prealloc' as the newly created second half. 'split' indicates an
391 * offset inside 'orig' where it should be split.
392 *
393 * Before calling,
394 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
395 * are two extent state structs in the tree:
396 * prealloc: [orig->start, split - 1]
397 * orig: [ split, orig->end ]
398 *
399 * The tree locks are not taken by this function. They need to be held
400 * by the caller.
401 */
402static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
403 struct extent_state *prealloc, u64 split)
404{
405 struct rb_node *node;
406 prealloc->start = orig->start;
407 prealloc->end = split - 1;
408 prealloc->state = orig->state;
409 orig->start = split;
410
411 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
412 if (node) {
413 struct extent_state *found;
414 found = rb_entry(node, struct extent_state, rb_node);
415 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
416 free_extent_state(prealloc);
417 return -EEXIST;
418 }
Chris Mason70dec802008-01-29 09:59:12 -0500419 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500420 return 0;
421}
422
423/*
424 * utility function to clear some bits in an extent state struct.
425 * it will optionally wake up any one waiting on this state (wake == 1), or
426 * forcibly remove the state from the tree (delete == 1).
427 *
428 * If no bits are set on the state struct after clearing things, the
429 * struct is freed and removed from the tree
430 */
431static int clear_state_bit(struct extent_io_tree *tree,
432 struct extent_state *state, int bits, int wake,
433 int delete)
434{
435 int ret = state->state & bits;
436
437 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
438 u64 range = state->end - state->start + 1;
439 WARN_ON(range > tree->dirty_bytes);
440 tree->dirty_bytes -= range;
441 }
Chris Mason291d6732008-01-29 15:55:23 -0500442 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500443 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500444 if (wake)
445 wake_up(&state->wq);
446 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500447 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500448 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500449 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500450 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500451 free_extent_state(state);
452 } else {
453 WARN_ON(1);
454 }
455 } else {
456 merge_state(tree, state);
457 }
458 return ret;
459}
460
461/*
462 * clear some bits on a range in the tree. This may require splitting
463 * or inserting elements in the tree, so the gfp mask is used to
464 * indicate which allocations or sleeping are allowed.
465 *
466 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
467 * the given range from the tree regardless of state (ie for truncate).
468 *
469 * the range [start, end] is inclusive.
470 *
471 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
472 * bits were already set, or zero if none of the bits were already set.
473 */
474int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
475 int bits, int wake, int delete, gfp_t mask)
476{
477 struct extent_state *state;
478 struct extent_state *prealloc = NULL;
479 struct rb_node *node;
480 unsigned long flags;
481 int err;
482 int set = 0;
483
484again:
485 if (!prealloc && (mask & __GFP_WAIT)) {
486 prealloc = alloc_extent_state(mask);
487 if (!prealloc)
488 return -ENOMEM;
489 }
490
Chris Mason70dec802008-01-29 09:59:12 -0500491 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500492 /*
493 * this search will find the extents that end after
494 * our range starts
495 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500496 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500497 if (!node)
498 goto out;
499 state = rb_entry(node, struct extent_state, rb_node);
500 if (state->start > end)
501 goto out;
502 WARN_ON(state->end < start);
503
504 /*
505 * | ---- desired range ---- |
506 * | state | or
507 * | ------------- state -------------- |
508 *
509 * We need to split the extent we found, and may flip
510 * bits on second half.
511 *
512 * If the extent we found extends past our range, we
513 * just split and search again. It'll get split again
514 * the next time though.
515 *
516 * If the extent we found is inside our range, we clear
517 * the desired bit on it.
518 */
519
520 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500521 if (!prealloc)
522 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500523 err = split_state(tree, state, prealloc, start);
524 BUG_ON(err == -EEXIST);
525 prealloc = NULL;
526 if (err)
527 goto out;
528 if (state->end <= end) {
529 start = state->end + 1;
530 set |= clear_state_bit(tree, state, bits,
531 wake, delete);
532 } else {
533 start = state->start;
534 }
535 goto search_again;
536 }
537 /*
538 * | ---- desired range ---- |
539 * | state |
540 * We need to split the extent, and clear the bit
541 * on the first half
542 */
543 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500544 if (!prealloc)
545 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500546 err = split_state(tree, state, prealloc, end + 1);
547 BUG_ON(err == -EEXIST);
548
549 if (wake)
550 wake_up(&state->wq);
551 set |= clear_state_bit(tree, prealloc, bits,
552 wake, delete);
553 prealloc = NULL;
554 goto out;
555 }
556
557 start = state->end + 1;
558 set |= clear_state_bit(tree, state, bits, wake, delete);
559 goto search_again;
560
561out:
Chris Mason70dec802008-01-29 09:59:12 -0500562 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500563 if (prealloc)
564 free_extent_state(prealloc);
565
566 return set;
567
568search_again:
569 if (start > end)
570 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500571 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500572 if (mask & __GFP_WAIT)
573 cond_resched();
574 goto again;
575}
576EXPORT_SYMBOL(clear_extent_bit);
577
578static int wait_on_state(struct extent_io_tree *tree,
579 struct extent_state *state)
580{
581 DEFINE_WAIT(wait);
582 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Mason70dec802008-01-29 09:59:12 -0500583 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500584 schedule();
Chris Mason70dec802008-01-29 09:59:12 -0500585 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500586 finish_wait(&state->wq, &wait);
587 return 0;
588}
589
590/*
591 * waits for one or more bits to clear on a range in the state tree.
592 * The range [start, end] is inclusive.
593 * The tree lock is taken by this function
594 */
595int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
596{
597 struct extent_state *state;
598 struct rb_node *node;
599
Chris Mason70dec802008-01-29 09:59:12 -0500600 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500601again:
602 while (1) {
603 /*
604 * this search will find all the extents that end after
605 * our range starts
606 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500607 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500608 if (!node)
609 break;
610
611 state = rb_entry(node, struct extent_state, rb_node);
612
613 if (state->start > end)
614 goto out;
615
616 if (state->state & bits) {
617 start = state->start;
618 atomic_inc(&state->refs);
619 wait_on_state(tree, state);
620 free_extent_state(state);
621 goto again;
622 }
623 start = state->end + 1;
624
625 if (start > end)
626 break;
627
628 if (need_resched()) {
Chris Mason70dec802008-01-29 09:59:12 -0500629 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630 cond_resched();
Chris Mason70dec802008-01-29 09:59:12 -0500631 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500632 }
633 }
634out:
Chris Mason70dec802008-01-29 09:59:12 -0500635 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500636 return 0;
637}
638EXPORT_SYMBOL(wait_extent_bit);
639
640static void set_state_bits(struct extent_io_tree *tree,
641 struct extent_state *state,
642 int bits)
643{
644 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
645 u64 range = state->end - state->start + 1;
646 tree->dirty_bytes += range;
647 }
Chris Mason291d6732008-01-29 15:55:23 -0500648 set_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500649 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500650}
651
652/*
653 * set some bits on a range in the tree. This may require allocations
654 * or sleeping, so the gfp mask is used to indicate what is allowed.
655 *
656 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
657 * range already has the desired bits set. The start of the existing
658 * range is returned in failed_start in this case.
659 *
660 * [start, end] is inclusive
661 * This takes the tree lock.
662 */
663int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
664 int exclusive, u64 *failed_start, gfp_t mask)
665{
666 struct extent_state *state;
667 struct extent_state *prealloc = NULL;
668 struct rb_node *node;
669 unsigned long flags;
670 int err = 0;
671 int set;
672 u64 last_start;
673 u64 last_end;
674again:
675 if (!prealloc && (mask & __GFP_WAIT)) {
676 prealloc = alloc_extent_state(mask);
677 if (!prealloc)
678 return -ENOMEM;
679 }
680
Chris Mason70dec802008-01-29 09:59:12 -0500681 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500682 /*
683 * this search will find all the extents that end after
684 * our range starts.
685 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500686 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500687 if (!node) {
688 err = insert_state(tree, prealloc, start, end, bits);
689 prealloc = NULL;
690 BUG_ON(err == -EEXIST);
691 goto out;
692 }
693
694 state = rb_entry(node, struct extent_state, rb_node);
695 last_start = state->start;
696 last_end = state->end;
697
698 /*
699 * | ---- desired range ---- |
700 * | state |
701 *
702 * Just lock what we found and keep going
703 */
704 if (state->start == start && state->end <= end) {
705 set = state->state & bits;
706 if (set && exclusive) {
707 *failed_start = state->start;
708 err = -EEXIST;
709 goto out;
710 }
711 set_state_bits(tree, state, bits);
712 start = state->end + 1;
713 merge_state(tree, state);
714 goto search_again;
715 }
716
717 /*
718 * | ---- desired range ---- |
719 * | state |
720 * or
721 * | ------------- state -------------- |
722 *
723 * We need to split the extent we found, and may flip bits on
724 * second half.
725 *
726 * If the extent we found extends past our
727 * range, we just split and search again. It'll get split
728 * again the next time though.
729 *
730 * If the extent we found is inside our range, we set the
731 * desired bit on it.
732 */
733 if (state->start < start) {
734 set = state->state & bits;
735 if (exclusive && set) {
736 *failed_start = start;
737 err = -EEXIST;
738 goto out;
739 }
740 err = split_state(tree, state, prealloc, start);
741 BUG_ON(err == -EEXIST);
742 prealloc = NULL;
743 if (err)
744 goto out;
745 if (state->end <= end) {
746 set_state_bits(tree, state, bits);
747 start = state->end + 1;
748 merge_state(tree, state);
749 } else {
750 start = state->start;
751 }
752 goto search_again;
753 }
754 /*
755 * | ---- desired range ---- |
756 * | state | or | state |
757 *
758 * There's a hole, we need to insert something in it and
759 * ignore the extent we found.
760 */
761 if (state->start > start) {
762 u64 this_end;
763 if (end < last_start)
764 this_end = end;
765 else
766 this_end = last_start -1;
767 err = insert_state(tree, prealloc, start, this_end,
768 bits);
769 prealloc = NULL;
770 BUG_ON(err == -EEXIST);
771 if (err)
772 goto out;
773 start = this_end + 1;
774 goto search_again;
775 }
776 /*
777 * | ---- desired range ---- |
778 * | state |
779 * We need to split the extent, and set the bit
780 * on the first half
781 */
782 if (state->start <= end && state->end > end) {
783 set = state->state & bits;
784 if (exclusive && set) {
785 *failed_start = start;
786 err = -EEXIST;
787 goto out;
788 }
789 err = split_state(tree, state, prealloc, end + 1);
790 BUG_ON(err == -EEXIST);
791
792 set_state_bits(tree, prealloc, bits);
793 merge_state(tree, prealloc);
794 prealloc = NULL;
795 goto out;
796 }
797
798 goto search_again;
799
800out:
Chris Mason70dec802008-01-29 09:59:12 -0500801 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500802 if (prealloc)
803 free_extent_state(prealloc);
804
805 return err;
806
807search_again:
808 if (start > end)
809 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500810 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500811 if (mask & __GFP_WAIT)
812 cond_resched();
813 goto again;
814}
815EXPORT_SYMBOL(set_extent_bit);
816
817/* wrappers around set/clear extent bit */
818int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
819 gfp_t mask)
820{
821 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
822 mask);
823}
824EXPORT_SYMBOL(set_extent_dirty);
825
Chris Masone6dcd2d2008-07-17 12:53:50 -0400826int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
827 gfp_t mask)
828{
829 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
830}
831EXPORT_SYMBOL(set_extent_ordered);
832
Chris Masond1310b22008-01-24 16:13:08 -0500833int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
834 int bits, gfp_t mask)
835{
836 return set_extent_bit(tree, start, end, bits, 0, NULL,
837 mask);
838}
839EXPORT_SYMBOL(set_extent_bits);
840
841int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
842 int bits, gfp_t mask)
843{
844 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
845}
846EXPORT_SYMBOL(clear_extent_bits);
847
848int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
849 gfp_t mask)
850{
851 return set_extent_bit(tree, start, end,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400852 EXTENT_DELALLOC | EXTENT_DIRTY,
853 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500854}
855EXPORT_SYMBOL(set_extent_delalloc);
856
857int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
858 gfp_t mask)
859{
860 return clear_extent_bit(tree, start, end,
861 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
862}
863EXPORT_SYMBOL(clear_extent_dirty);
864
Chris Masone6dcd2d2008-07-17 12:53:50 -0400865int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
866 gfp_t mask)
867{
868 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
869}
870EXPORT_SYMBOL(clear_extent_ordered);
871
Chris Masond1310b22008-01-24 16:13:08 -0500872int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
873 gfp_t mask)
874{
875 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
876 mask);
877}
878EXPORT_SYMBOL(set_extent_new);
879
880int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
881 gfp_t mask)
882{
883 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
884}
885EXPORT_SYMBOL(clear_extent_new);
886
887int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
888 gfp_t mask)
889{
890 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
891 mask);
892}
893EXPORT_SYMBOL(set_extent_uptodate);
894
895int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
896 gfp_t mask)
897{
898 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
899}
900EXPORT_SYMBOL(clear_extent_uptodate);
901
902int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
903 gfp_t mask)
904{
905 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
906 0, NULL, mask);
907}
908EXPORT_SYMBOL(set_extent_writeback);
909
910int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
911 gfp_t mask)
912{
913 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
914}
915EXPORT_SYMBOL(clear_extent_writeback);
916
917int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
918{
919 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
920}
921EXPORT_SYMBOL(wait_on_extent_writeback);
922
Chris Masond352ac62008-09-29 15:18:18 -0400923/*
924 * either insert or lock state struct between start and end use mask to tell
925 * us if waiting is desired.
926 */
Chris Masond1310b22008-01-24 16:13:08 -0500927int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
928{
929 int err;
930 u64 failed_start;
931 while (1) {
932 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
933 &failed_start, mask);
934 if (err == -EEXIST && (mask & __GFP_WAIT)) {
935 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
936 start = failed_start;
937 } else {
938 break;
939 }
940 WARN_ON(start > end);
941 }
942 return err;
943}
944EXPORT_SYMBOL(lock_extent);
945
Josef Bacik25179202008-10-29 14:49:05 -0400946int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
947 gfp_t mask)
948{
949 int err;
950 u64 failed_start;
951
952 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
953 &failed_start, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400954 if (err == -EEXIST) {
955 if (failed_start > start)
956 clear_extent_bit(tree, start, failed_start - 1,
957 EXTENT_LOCKED, 1, 0, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400958 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400959 }
Josef Bacik25179202008-10-29 14:49:05 -0400960 return 1;
961}
962EXPORT_SYMBOL(try_lock_extent);
963
Chris Masond1310b22008-01-24 16:13:08 -0500964int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
965 gfp_t mask)
966{
967 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
968}
969EXPORT_SYMBOL(unlock_extent);
970
971/*
972 * helper function to set pages and extents in the tree dirty
973 */
974int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
975{
976 unsigned long index = start >> PAGE_CACHE_SHIFT;
977 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
978 struct page *page;
979
980 while (index <= end_index) {
981 page = find_get_page(tree->mapping, index);
982 BUG_ON(!page);
983 __set_page_dirty_nobuffers(page);
984 page_cache_release(page);
985 index++;
986 }
987 set_extent_dirty(tree, start, end, GFP_NOFS);
988 return 0;
989}
990EXPORT_SYMBOL(set_range_dirty);
991
992/*
993 * helper function to set both pages and extents in the tree writeback
994 */
995int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
996{
997 unsigned long index = start >> PAGE_CACHE_SHIFT;
998 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
999 struct page *page;
1000
1001 while (index <= end_index) {
1002 page = find_get_page(tree->mapping, index);
1003 BUG_ON(!page);
1004 set_page_writeback(page);
1005 page_cache_release(page);
1006 index++;
1007 }
1008 set_extent_writeback(tree, start, end, GFP_NOFS);
1009 return 0;
1010}
1011EXPORT_SYMBOL(set_range_writeback);
1012
Chris Masond352ac62008-09-29 15:18:18 -04001013/*
1014 * find the first offset in the io tree with 'bits' set. zero is
1015 * returned if we find something, and *start_ret and *end_ret are
1016 * set to reflect the state struct that was found.
1017 *
1018 * If nothing was found, 1 is returned, < 0 on error
1019 */
Chris Masond1310b22008-01-24 16:13:08 -05001020int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1021 u64 *start_ret, u64 *end_ret, int bits)
1022{
1023 struct rb_node *node;
1024 struct extent_state *state;
1025 int ret = 1;
1026
Chris Mason70dec802008-01-29 09:59:12 -05001027 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001028 /*
1029 * this search will find all the extents that end after
1030 * our range starts.
1031 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001032 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001033 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001034 goto out;
1035 }
1036
1037 while(1) {
1038 state = rb_entry(node, struct extent_state, rb_node);
1039 if (state->end >= start && (state->state & bits)) {
1040 *start_ret = state->start;
1041 *end_ret = state->end;
1042 ret = 0;
1043 break;
1044 }
1045 node = rb_next(node);
1046 if (!node)
1047 break;
1048 }
1049out:
Chris Mason70dec802008-01-29 09:59:12 -05001050 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001051 return ret;
1052}
1053EXPORT_SYMBOL(find_first_extent_bit);
1054
Chris Masond352ac62008-09-29 15:18:18 -04001055/* find the first state struct with 'bits' set after 'start', and
1056 * return it. tree->lock must be held. NULL will returned if
1057 * nothing was found after 'start'
1058 */
Chris Masond7fc6402008-02-18 12:12:38 -05001059struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1060 u64 start, int bits)
1061{
1062 struct rb_node *node;
1063 struct extent_state *state;
1064
1065 /*
1066 * this search will find all the extents that end after
1067 * our range starts.
1068 */
1069 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001070 if (!node) {
Chris Masond7fc6402008-02-18 12:12:38 -05001071 goto out;
1072 }
1073
1074 while(1) {
1075 state = rb_entry(node, struct extent_state, rb_node);
1076 if (state->end >= start && (state->state & bits)) {
1077 return state;
1078 }
1079 node = rb_next(node);
1080 if (!node)
1081 break;
1082 }
1083out:
1084 return NULL;
1085}
1086EXPORT_SYMBOL(find_first_extent_bit_state);
1087
Chris Masond352ac62008-09-29 15:18:18 -04001088/*
1089 * find a contiguous range of bytes in the file marked as delalloc, not
1090 * more than 'max_bytes'. start and end are used to return the range,
1091 *
1092 * 1 is returned if we find something, 0 if nothing was in the tree
1093 */
Chris Masonc8b97812008-10-29 14:49:59 -04001094static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1095 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001096{
1097 struct rb_node *node;
1098 struct extent_state *state;
1099 u64 cur_start = *start;
1100 u64 found = 0;
1101 u64 total_bytes = 0;
1102
Chris Mason70dec802008-01-29 09:59:12 -05001103 spin_lock_irq(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001104
Chris Masond1310b22008-01-24 16:13:08 -05001105 /*
1106 * this search will find all the extents that end after
1107 * our range starts.
1108 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001109 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001110 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001111 if (!found)
1112 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001113 goto out;
1114 }
1115
1116 while(1) {
1117 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001118 if (found && (state->start != cur_start ||
1119 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001120 goto out;
1121 }
1122 if (!(state->state & EXTENT_DELALLOC)) {
1123 if (!found)
1124 *end = state->end;
1125 goto out;
1126 }
Chris Masond1310b22008-01-24 16:13:08 -05001127 if (!found)
1128 *start = state->start;
1129 found++;
1130 *end = state->end;
1131 cur_start = state->end + 1;
1132 node = rb_next(node);
1133 if (!node)
1134 break;
1135 total_bytes += state->end - state->start + 1;
1136 if (total_bytes >= max_bytes)
1137 break;
1138 }
1139out:
Chris Mason70dec802008-01-29 09:59:12 -05001140 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001141 return found;
1142}
1143
Chris Masonc8b97812008-10-29 14:49:59 -04001144static noinline int __unlock_for_delalloc(struct inode *inode,
1145 struct page *locked_page,
1146 u64 start, u64 end)
1147{
1148 int ret;
1149 struct page *pages[16];
1150 unsigned long index = start >> PAGE_CACHE_SHIFT;
1151 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1152 unsigned long nr_pages = end_index - index + 1;
1153 int i;
1154
1155 if (index == locked_page->index && end_index == index)
1156 return 0;
1157
1158 while(nr_pages > 0) {
1159 ret = find_get_pages_contig(inode->i_mapping, index,
1160 min(nr_pages, ARRAY_SIZE(pages)), pages);
1161 for (i = 0; i < ret; i++) {
1162 if (pages[i] != locked_page)
1163 unlock_page(pages[i]);
1164 page_cache_release(pages[i]);
1165 }
1166 nr_pages -= ret;
1167 index += ret;
1168 cond_resched();
1169 }
1170 return 0;
1171}
1172
1173static noinline int lock_delalloc_pages(struct inode *inode,
1174 struct page *locked_page,
1175 u64 delalloc_start,
1176 u64 delalloc_end)
1177{
1178 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1179 unsigned long start_index = index;
1180 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1181 unsigned long pages_locked = 0;
1182 struct page *pages[16];
1183 unsigned long nrpages;
1184 int ret;
1185 int i;
1186
1187 /* the caller is responsible for locking the start index */
1188 if (index == locked_page->index && index == end_index)
1189 return 0;
1190
1191 /* skip the page at the start index */
1192 nrpages = end_index - index + 1;
1193 while(nrpages > 0) {
1194 ret = find_get_pages_contig(inode->i_mapping, index,
1195 min(nrpages, ARRAY_SIZE(pages)), pages);
1196 if (ret == 0) {
1197 ret = -EAGAIN;
1198 goto done;
1199 }
1200 /* now we have an array of pages, lock them all */
1201 for (i = 0; i < ret; i++) {
1202 /*
1203 * the caller is taking responsibility for
1204 * locked_page
1205 */
Chris Mason771ed682008-11-06 22:02:51 -05001206 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001207 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001208 if (!PageDirty(pages[i]) ||
1209 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001210 ret = -EAGAIN;
1211 unlock_page(pages[i]);
1212 page_cache_release(pages[i]);
1213 goto done;
1214 }
1215 }
Chris Masonc8b97812008-10-29 14:49:59 -04001216 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001217 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001218 }
Chris Masonc8b97812008-10-29 14:49:59 -04001219 nrpages -= ret;
1220 index += ret;
1221 cond_resched();
1222 }
1223 ret = 0;
1224done:
1225 if (ret && pages_locked) {
1226 __unlock_for_delalloc(inode, locked_page,
1227 delalloc_start,
1228 ((u64)(start_index + pages_locked - 1)) <<
1229 PAGE_CACHE_SHIFT);
1230 }
1231 return ret;
1232}
1233
1234/*
1235 * find a contiguous range of bytes in the file marked as delalloc, not
1236 * more than 'max_bytes'. start and end are used to return the range,
1237 *
1238 * 1 is returned if we find something, 0 if nothing was in the tree
1239 */
1240static noinline u64 find_lock_delalloc_range(struct inode *inode,
1241 struct extent_io_tree *tree,
1242 struct page *locked_page,
1243 u64 *start, u64 *end,
1244 u64 max_bytes)
1245{
1246 u64 delalloc_start;
1247 u64 delalloc_end;
1248 u64 found;
1249 int ret;
1250 int loops = 0;
1251
1252again:
1253 /* step one, find a bunch of delalloc bytes starting at start */
1254 delalloc_start = *start;
1255 delalloc_end = 0;
1256 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1257 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001258 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001259 *start = delalloc_start;
1260 *end = delalloc_end;
1261 return found;
1262 }
1263
1264 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001265 * start comes from the offset of locked_page. We have to lock
1266 * pages in order, so we can't process delalloc bytes before
1267 * locked_page
1268 */
1269 if (delalloc_start < *start) {
1270 delalloc_start = *start;
1271 }
1272
1273 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001274 * make sure to limit the number of pages we try to lock down
1275 * if we're looping.
1276 */
1277 if (delalloc_end + 1 - delalloc_start > max_bytes && loops) {
Chris Mason771ed682008-11-06 22:02:51 -05001278 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masonc8b97812008-10-29 14:49:59 -04001279 }
1280 /* step two, lock all the pages after the page that has start */
1281 ret = lock_delalloc_pages(inode, locked_page,
1282 delalloc_start, delalloc_end);
1283 if (ret == -EAGAIN) {
1284 /* some of the pages are gone, lets avoid looping by
1285 * shortening the size of the delalloc range we're searching
1286 */
1287 if (!loops) {
1288 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1289 max_bytes = PAGE_CACHE_SIZE - offset;
1290 loops = 1;
1291 goto again;
1292 } else {
1293 found = 0;
1294 goto out_failed;
1295 }
1296 }
1297 BUG_ON(ret);
1298
1299 /* step three, lock the state bits for the whole range */
1300 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1301
1302 /* then test to make sure it is all still delalloc */
1303 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1304 EXTENT_DELALLOC, 1);
1305 if (!ret) {
1306 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1307 __unlock_for_delalloc(inode, locked_page,
1308 delalloc_start, delalloc_end);
1309 cond_resched();
1310 goto again;
1311 }
1312 *start = delalloc_start;
1313 *end = delalloc_end;
1314out_failed:
1315 return found;
1316}
1317
1318int extent_clear_unlock_delalloc(struct inode *inode,
1319 struct extent_io_tree *tree,
1320 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001321 int unlock_pages,
1322 int clear_unlock,
1323 int clear_delalloc, int clear_dirty,
1324 int set_writeback,
Chris Masonc8b97812008-10-29 14:49:59 -04001325 int end_writeback)
1326{
1327 int ret;
1328 struct page *pages[16];
1329 unsigned long index = start >> PAGE_CACHE_SHIFT;
1330 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1331 unsigned long nr_pages = end_index - index + 1;
1332 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001333 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001334
Chris Mason771ed682008-11-06 22:02:51 -05001335 if (clear_unlock)
1336 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001337 if (clear_dirty)
1338 clear_bits |= EXTENT_DIRTY;
1339
Chris Mason771ed682008-11-06 22:02:51 -05001340 if (clear_delalloc)
1341 clear_bits |= EXTENT_DELALLOC;
1342
Chris Masonc8b97812008-10-29 14:49:59 -04001343 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05001344 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1345 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001346
1347 while(nr_pages > 0) {
1348 ret = find_get_pages_contig(inode->i_mapping, index,
1349 min(nr_pages, ARRAY_SIZE(pages)), pages);
1350 for (i = 0; i < ret; i++) {
1351 if (pages[i] == locked_page) {
1352 page_cache_release(pages[i]);
1353 continue;
1354 }
1355 if (clear_dirty)
1356 clear_page_dirty_for_io(pages[i]);
1357 if (set_writeback)
1358 set_page_writeback(pages[i]);
1359 if (end_writeback)
1360 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001361 if (unlock_pages)
1362 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001363 page_cache_release(pages[i]);
1364 }
1365 nr_pages -= ret;
1366 index += ret;
1367 cond_resched();
1368 }
1369 return 0;
1370}
1371EXPORT_SYMBOL(extent_clear_unlock_delalloc);
1372
Chris Masond352ac62008-09-29 15:18:18 -04001373/*
1374 * count the number of bytes in the tree that have a given bit(s)
1375 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1376 * cached. The total number found is returned.
1377 */
Chris Masond1310b22008-01-24 16:13:08 -05001378u64 count_range_bits(struct extent_io_tree *tree,
1379 u64 *start, u64 search_end, u64 max_bytes,
1380 unsigned long bits)
1381{
1382 struct rb_node *node;
1383 struct extent_state *state;
1384 u64 cur_start = *start;
1385 u64 total_bytes = 0;
1386 int found = 0;
1387
1388 if (search_end <= cur_start) {
1389 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1390 WARN_ON(1);
1391 return 0;
1392 }
1393
Chris Mason70dec802008-01-29 09:59:12 -05001394 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001395 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1396 total_bytes = tree->dirty_bytes;
1397 goto out;
1398 }
1399 /*
1400 * this search will find all the extents that end after
1401 * our range starts.
1402 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001403 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001404 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001405 goto out;
1406 }
1407
1408 while(1) {
1409 state = rb_entry(node, struct extent_state, rb_node);
1410 if (state->start > search_end)
1411 break;
1412 if (state->end >= cur_start && (state->state & bits)) {
1413 total_bytes += min(search_end, state->end) + 1 -
1414 max(cur_start, state->start);
1415 if (total_bytes >= max_bytes)
1416 break;
1417 if (!found) {
1418 *start = state->start;
1419 found = 1;
1420 }
1421 }
1422 node = rb_next(node);
1423 if (!node)
1424 break;
1425 }
1426out:
Chris Mason70dec802008-01-29 09:59:12 -05001427 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001428 return total_bytes;
1429}
1430/*
1431 * helper function to lock both pages and extents in the tree.
1432 * pages must be locked first.
1433 */
1434int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1435{
1436 unsigned long index = start >> PAGE_CACHE_SHIFT;
1437 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1438 struct page *page;
1439 int err;
1440
1441 while (index <= end_index) {
1442 page = grab_cache_page(tree->mapping, index);
1443 if (!page) {
1444 err = -ENOMEM;
1445 goto failed;
1446 }
1447 if (IS_ERR(page)) {
1448 err = PTR_ERR(page);
1449 goto failed;
1450 }
1451 index++;
1452 }
1453 lock_extent(tree, start, end, GFP_NOFS);
1454 return 0;
1455
1456failed:
1457 /*
1458 * we failed above in getting the page at 'index', so we undo here
1459 * up to but not including the page at 'index'
1460 */
1461 end_index = index;
1462 index = start >> PAGE_CACHE_SHIFT;
1463 while (index < end_index) {
1464 page = find_get_page(tree->mapping, index);
1465 unlock_page(page);
1466 page_cache_release(page);
1467 index++;
1468 }
1469 return err;
1470}
1471EXPORT_SYMBOL(lock_range);
1472
1473/*
1474 * helper function to unlock both pages and extents in the tree.
1475 */
1476int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1477{
1478 unsigned long index = start >> PAGE_CACHE_SHIFT;
1479 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1480 struct page *page;
1481
1482 while (index <= end_index) {
1483 page = find_get_page(tree->mapping, index);
1484 unlock_page(page);
1485 page_cache_release(page);
1486 index++;
1487 }
1488 unlock_extent(tree, start, end, GFP_NOFS);
1489 return 0;
1490}
1491EXPORT_SYMBOL(unlock_range);
1492
Chris Masond352ac62008-09-29 15:18:18 -04001493/*
1494 * set the private field for a given byte offset in the tree. If there isn't
1495 * an extent_state there already, this does nothing.
1496 */
Chris Masond1310b22008-01-24 16:13:08 -05001497int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1498{
1499 struct rb_node *node;
1500 struct extent_state *state;
1501 int ret = 0;
1502
Chris Mason70dec802008-01-29 09:59:12 -05001503 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001504 /*
1505 * this search will find all the extents that end after
1506 * our range starts.
1507 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001508 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001509 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001510 ret = -ENOENT;
1511 goto out;
1512 }
1513 state = rb_entry(node, struct extent_state, rb_node);
1514 if (state->start != start) {
1515 ret = -ENOENT;
1516 goto out;
1517 }
1518 state->private = private;
1519out:
Chris Mason70dec802008-01-29 09:59:12 -05001520 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001521 return ret;
1522}
1523
1524int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1525{
1526 struct rb_node *node;
1527 struct extent_state *state;
1528 int ret = 0;
1529
Chris Mason70dec802008-01-29 09:59:12 -05001530 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001531 /*
1532 * this search will find all the extents that end after
1533 * our range starts.
1534 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001535 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001536 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001537 ret = -ENOENT;
1538 goto out;
1539 }
1540 state = rb_entry(node, struct extent_state, rb_node);
1541 if (state->start != start) {
1542 ret = -ENOENT;
1543 goto out;
1544 }
1545 *private = state->private;
1546out:
Chris Mason70dec802008-01-29 09:59:12 -05001547 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001548 return ret;
1549}
1550
1551/*
1552 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001553 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001554 * has the bits set. Otherwise, 1 is returned if any bit in the
1555 * range is found set.
1556 */
1557int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1558 int bits, int filled)
1559{
1560 struct extent_state *state = NULL;
1561 struct rb_node *node;
1562 int bitset = 0;
1563 unsigned long flags;
1564
Chris Mason70dec802008-01-29 09:59:12 -05001565 spin_lock_irqsave(&tree->lock, flags);
Chris Mason80ea96b2008-02-01 14:51:59 -05001566 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001567 while (node && start <= end) {
1568 state = rb_entry(node, struct extent_state, rb_node);
1569
1570 if (filled && state->start > start) {
1571 bitset = 0;
1572 break;
1573 }
1574
1575 if (state->start > end)
1576 break;
1577
1578 if (state->state & bits) {
1579 bitset = 1;
1580 if (!filled)
1581 break;
1582 } else if (filled) {
1583 bitset = 0;
1584 break;
1585 }
1586 start = state->end + 1;
1587 if (start > end)
1588 break;
1589 node = rb_next(node);
1590 if (!node) {
1591 if (filled)
1592 bitset = 0;
1593 break;
1594 }
1595 }
Chris Mason70dec802008-01-29 09:59:12 -05001596 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05001597 return bitset;
1598}
1599EXPORT_SYMBOL(test_range_bit);
1600
1601/*
1602 * helper function to set a given page up to date if all the
1603 * extents in the tree for that page are up to date
1604 */
1605static int check_page_uptodate(struct extent_io_tree *tree,
1606 struct page *page)
1607{
1608 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1609 u64 end = start + PAGE_CACHE_SIZE - 1;
1610 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1611 SetPageUptodate(page);
1612 return 0;
1613}
1614
1615/*
1616 * helper function to unlock a page if all the extents in the tree
1617 * for that page are unlocked
1618 */
1619static int check_page_locked(struct extent_io_tree *tree,
1620 struct page *page)
1621{
1622 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1623 u64 end = start + PAGE_CACHE_SIZE - 1;
1624 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1625 unlock_page(page);
1626 return 0;
1627}
1628
1629/*
1630 * helper function to end page writeback if all the extents
1631 * in the tree for that page are done with writeback
1632 */
1633static int check_page_writeback(struct extent_io_tree *tree,
1634 struct page *page)
1635{
1636 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1637 u64 end = start + PAGE_CACHE_SIZE - 1;
1638 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1639 end_page_writeback(page);
1640 return 0;
1641}
1642
1643/* lots and lots of room for performance fixes in the end_bio funcs */
1644
1645/*
1646 * after a writepage IO is done, we need to:
1647 * clear the uptodate bits on error
1648 * clear the writeback bits in the extent tree for this IO
1649 * end_page_writeback if the page has no more pending IO
1650 *
1651 * Scheduling is not allowed, so the extent state tree is expected
1652 * to have one and only one object corresponding to this IO.
1653 */
Chris Masond1310b22008-01-24 16:13:08 -05001654static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001655{
Chris Mason1259ab72008-05-12 13:39:03 -04001656 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001657 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001658 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001659 u64 start;
1660 u64 end;
1661 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001662 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001663
Chris Masond1310b22008-01-24 16:13:08 -05001664 do {
1665 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001666 tree = &BTRFS_I(page->mapping->host)->io_tree;
1667
Chris Masond1310b22008-01-24 16:13:08 -05001668 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1669 bvec->bv_offset;
1670 end = start + bvec->bv_len - 1;
1671
1672 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1673 whole_page = 1;
1674 else
1675 whole_page = 0;
1676
1677 if (--bvec >= bio->bi_io_vec)
1678 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001679 if (tree->ops && tree->ops->writepage_end_io_hook) {
1680 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001681 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001682 if (ret)
1683 uptodate = 0;
1684 }
1685
1686 if (!uptodate && tree->ops &&
1687 tree->ops->writepage_io_failed_hook) {
1688 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001689 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001690 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001691 uptodate = (err == 0);
1692 continue;
1693 }
1694 }
1695
Chris Masond1310b22008-01-24 16:13:08 -05001696 if (!uptodate) {
1697 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1698 ClearPageUptodate(page);
1699 SetPageError(page);
1700 }
Chris Mason70dec802008-01-29 09:59:12 -05001701
David Woodhouse902b22f2008-08-20 08:51:49 -04001702 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001703
1704 if (whole_page)
1705 end_page_writeback(page);
1706 else
1707 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001708 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001709
Chris Masond1310b22008-01-24 16:13:08 -05001710 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001711}
1712
1713/*
1714 * after a readpage IO is done, we need to:
1715 * clear the uptodate bits on error
1716 * set the uptodate bits if things worked
1717 * set the page up to date if all extents in the tree are uptodate
1718 * clear the lock bit in the extent tree
1719 * unlock the page if there are no other extents locked for it
1720 *
1721 * Scheduling is not allowed, so the extent state tree is expected
1722 * to have one and only one object corresponding to this IO.
1723 */
Chris Masond1310b22008-01-24 16:13:08 -05001724static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001725{
1726 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1727 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001728 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001729 u64 start;
1730 u64 end;
1731 int whole_page;
1732 int ret;
1733
Chris Masond1310b22008-01-24 16:13:08 -05001734 do {
1735 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001736 tree = &BTRFS_I(page->mapping->host)->io_tree;
1737
Chris Masond1310b22008-01-24 16:13:08 -05001738 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1739 bvec->bv_offset;
1740 end = start + bvec->bv_len - 1;
1741
1742 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1743 whole_page = 1;
1744 else
1745 whole_page = 0;
1746
1747 if (--bvec >= bio->bi_io_vec)
1748 prefetchw(&bvec->bv_page->flags);
1749
1750 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001751 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001752 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001753 if (ret)
1754 uptodate = 0;
1755 }
Chris Mason7e383262008-04-09 16:28:12 -04001756 if (!uptodate && tree->ops &&
1757 tree->ops->readpage_io_failed_hook) {
1758 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001759 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001760 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001761 uptodate =
1762 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason7e383262008-04-09 16:28:12 -04001763 continue;
1764 }
1765 }
Chris Mason70dec802008-01-29 09:59:12 -05001766
Chris Mason771ed682008-11-06 22:02:51 -05001767 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001768 set_extent_uptodate(tree, start, end,
1769 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001770 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001771 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001772
Chris Mason70dec802008-01-29 09:59:12 -05001773 if (whole_page) {
1774 if (uptodate) {
1775 SetPageUptodate(page);
1776 } else {
1777 ClearPageUptodate(page);
1778 SetPageError(page);
1779 }
Chris Masond1310b22008-01-24 16:13:08 -05001780 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001781 } else {
1782 if (uptodate) {
1783 check_page_uptodate(tree, page);
1784 } else {
1785 ClearPageUptodate(page);
1786 SetPageError(page);
1787 }
Chris Masond1310b22008-01-24 16:13:08 -05001788 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001789 }
Chris Masond1310b22008-01-24 16:13:08 -05001790 } while (bvec >= bio->bi_io_vec);
1791
1792 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001793}
1794
1795/*
1796 * IO done from prepare_write is pretty simple, we just unlock
1797 * the structs in the extent tree when done, and set the uptodate bits
1798 * as appropriate.
1799 */
Chris Masond1310b22008-01-24 16:13:08 -05001800static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001801{
1802 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1803 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001804 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001805 u64 start;
1806 u64 end;
1807
Chris Masond1310b22008-01-24 16:13:08 -05001808 do {
1809 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001810 tree = &BTRFS_I(page->mapping->host)->io_tree;
1811
Chris Masond1310b22008-01-24 16:13:08 -05001812 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1813 bvec->bv_offset;
1814 end = start + bvec->bv_len - 1;
1815
1816 if (--bvec >= bio->bi_io_vec)
1817 prefetchw(&bvec->bv_page->flags);
1818
1819 if (uptodate) {
1820 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1821 } else {
1822 ClearPageUptodate(page);
1823 SetPageError(page);
1824 }
1825
1826 unlock_extent(tree, start, end, GFP_ATOMIC);
1827
1828 } while (bvec >= bio->bi_io_vec);
1829
1830 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001831}
1832
1833static struct bio *
1834extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1835 gfp_t gfp_flags)
1836{
1837 struct bio *bio;
1838
1839 bio = bio_alloc(gfp_flags, nr_vecs);
1840
1841 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1842 while (!bio && (nr_vecs /= 2))
1843 bio = bio_alloc(gfp_flags, nr_vecs);
1844 }
1845
1846 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001847 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001848 bio->bi_bdev = bdev;
1849 bio->bi_sector = first_sector;
1850 }
1851 return bio;
1852}
1853
Chris Masonc8b97812008-10-29 14:49:59 -04001854static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1855 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001856{
Chris Masond1310b22008-01-24 16:13:08 -05001857 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001858 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1859 struct page *page = bvec->bv_page;
1860 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001861 u64 start;
1862 u64 end;
1863
1864 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1865 end = start + bvec->bv_len - 1;
1866
David Woodhouse902b22f2008-08-20 08:51:49 -04001867 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001868
1869 bio_get(bio);
1870
Chris Mason065631f2008-02-20 12:07:25 -05001871 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001872 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001873 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001874 else
1875 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001876 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1877 ret = -EOPNOTSUPP;
1878 bio_put(bio);
1879 return ret;
1880}
1881
1882static int submit_extent_page(int rw, struct extent_io_tree *tree,
1883 struct page *page, sector_t sector,
1884 size_t size, unsigned long offset,
1885 struct block_device *bdev,
1886 struct bio **bio_ret,
1887 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001888 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001889 int mirror_num,
1890 unsigned long prev_bio_flags,
1891 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001892{
1893 int ret = 0;
1894 struct bio *bio;
1895 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001896 int contig = 0;
1897 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1898 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1899 size_t page_size = min(size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001900
1901 if (bio_ret && *bio_ret) {
1902 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001903 if (old_compressed)
1904 contig = bio->bi_sector == sector;
1905 else
1906 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1907 sector;
1908
1909 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001910 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001911 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1912 bio_flags)) ||
1913 bio_add_page(bio, page, page_size, offset) < page_size) {
1914 ret = submit_one_bio(rw, bio, mirror_num,
1915 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001916 bio = NULL;
1917 } else {
1918 return 0;
1919 }
1920 }
Chris Masonc8b97812008-10-29 14:49:59 -04001921 if (this_compressed)
1922 nr = BIO_MAX_PAGES;
1923 else
1924 nr = bio_get_nr_vecs(bdev);
1925
Chris Masond1310b22008-01-24 16:13:08 -05001926 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1927 if (!bio) {
1928 printk("failed to allocate bio nr %d\n", nr);
1929 }
Chris Mason70dec802008-01-29 09:59:12 -05001930
Chris Masonc8b97812008-10-29 14:49:59 -04001931 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001932 bio->bi_end_io = end_io_func;
1933 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001934
Chris Masond1310b22008-01-24 16:13:08 -05001935 if (bio_ret) {
1936 *bio_ret = bio;
1937 } else {
Chris Masonc8b97812008-10-29 14:49:59 -04001938 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001939 }
1940
1941 return ret;
1942}
1943
1944void set_page_extent_mapped(struct page *page)
1945{
1946 if (!PagePrivate(page)) {
1947 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001948 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001949 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001950 }
1951}
Chris Mason771ed682008-11-06 22:02:51 -05001952EXPORT_SYMBOL(set_page_extent_mapped);
Chris Masond1310b22008-01-24 16:13:08 -05001953
1954void set_page_extent_head(struct page *page, unsigned long len)
1955{
1956 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1957}
1958
1959/*
1960 * basic readpage implementation. Locked extent state structs are inserted
1961 * into the tree that are removed when the IO is done (by the end_io
1962 * handlers)
1963 */
1964static int __extent_read_full_page(struct extent_io_tree *tree,
1965 struct page *page,
1966 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001967 struct bio **bio, int mirror_num,
1968 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001969{
1970 struct inode *inode = page->mapping->host;
1971 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
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 cur_end;
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 iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001986 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001987 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001988 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001989
1990 set_page_extent_mapped(page);
1991
1992 end = page_end;
1993 lock_extent(tree, start, end, GFP_NOFS);
1994
Chris Masonc8b97812008-10-29 14:49:59 -04001995 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1996 char *userpage;
1997 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1998
1999 if (zero_offset) {
2000 iosize = PAGE_CACHE_SIZE - zero_offset;
2001 userpage = kmap_atomic(page, KM_USER0);
2002 memset(userpage + zero_offset, 0, iosize);
2003 flush_dcache_page(page);
2004 kunmap_atomic(userpage, KM_USER0);
2005 }
2006 }
Chris Masond1310b22008-01-24 16:13:08 -05002007 while (cur <= end) {
2008 if (cur >= last_byte) {
2009 char *userpage;
2010 iosize = PAGE_CACHE_SIZE - page_offset;
2011 userpage = kmap_atomic(page, KM_USER0);
2012 memset(userpage + page_offset, 0, iosize);
2013 flush_dcache_page(page);
2014 kunmap_atomic(userpage, KM_USER0);
2015 set_extent_uptodate(tree, cur, cur + iosize - 1,
2016 GFP_NOFS);
2017 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2018 break;
2019 }
2020 em = get_extent(inode, page, page_offset, cur,
2021 end - cur + 1, 0);
2022 if (IS_ERR(em) || !em) {
2023 SetPageError(page);
2024 unlock_extent(tree, cur, end, GFP_NOFS);
2025 break;
2026 }
Chris Masond1310b22008-01-24 16:13:08 -05002027 extent_offset = cur - em->start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002028 if (extent_map_end(em) <= cur) {
2029printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur);
2030 }
Chris Masond1310b22008-01-24 16:13:08 -05002031 BUG_ON(extent_map_end(em) <= cur);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002032 if (end < cur) {
2033printk("2bad mapping end %Lu cur %Lu\n", end, cur);
2034 }
Chris Masond1310b22008-01-24 16:13:08 -05002035 BUG_ON(end < cur);
2036
Chris Masonc8b97812008-10-29 14:49:59 -04002037 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2038 this_bio_flag = EXTENT_BIO_COMPRESSED;
2039
Chris Masond1310b22008-01-24 16:13:08 -05002040 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2041 cur_end = min(extent_map_end(em) - 1, end);
2042 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002043 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2044 disk_io_size = em->block_len;
2045 sector = em->block_start >> 9;
2046 } else {
2047 sector = (em->block_start + extent_offset) >> 9;
2048 disk_io_size = iosize;
2049 }
Chris Masond1310b22008-01-24 16:13:08 -05002050 bdev = em->bdev;
2051 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002052 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2053 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002054 free_extent_map(em);
2055 em = NULL;
2056
2057 /* we've found a hole, just zero and go on */
2058 if (block_start == EXTENT_MAP_HOLE) {
2059 char *userpage;
2060 userpage = kmap_atomic(page, KM_USER0);
2061 memset(userpage + page_offset, 0, iosize);
2062 flush_dcache_page(page);
2063 kunmap_atomic(userpage, KM_USER0);
2064
2065 set_extent_uptodate(tree, cur, cur + iosize - 1,
2066 GFP_NOFS);
2067 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2068 cur = cur + iosize;
2069 page_offset += iosize;
2070 continue;
2071 }
2072 /* the get_extent function already copied into the page */
2073 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002074 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002075 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2076 cur = cur + iosize;
2077 page_offset += iosize;
2078 continue;
2079 }
Chris Mason70dec802008-01-29 09:59:12 -05002080 /* we have an inline extent but it didn't get marked up
2081 * to date. Error out
2082 */
2083 if (block_start == EXTENT_MAP_INLINE) {
2084 SetPageError(page);
2085 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2086 cur = cur + iosize;
2087 page_offset += iosize;
2088 continue;
2089 }
Chris Masond1310b22008-01-24 16:13:08 -05002090
2091 ret = 0;
2092 if (tree->ops && tree->ops->readpage_io_hook) {
2093 ret = tree->ops->readpage_io_hook(page, cur,
2094 cur + iosize - 1);
2095 }
2096 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002097 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2098 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002099 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002100 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002101 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002102 end_bio_extent_readpage, mirror_num,
2103 *bio_flags,
2104 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002105 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002106 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002107 }
2108 if (ret)
2109 SetPageError(page);
2110 cur = cur + iosize;
2111 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002112 }
2113 if (!nr) {
2114 if (!PageError(page))
2115 SetPageUptodate(page);
2116 unlock_page(page);
2117 }
2118 return 0;
2119}
2120
2121int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2122 get_extent_t *get_extent)
2123{
2124 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002125 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002126 int ret;
2127
Chris Masonc8b97812008-10-29 14:49:59 -04002128 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2129 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002130 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002131 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002132 return ret;
2133}
2134EXPORT_SYMBOL(extent_read_full_page);
2135
2136/*
2137 * the writepage semantics are similar to regular writepage. extent
2138 * records are inserted to lock ranges in the tree, and as dirty areas
2139 * are found, they are marked writeback. Then the lock bits are removed
2140 * and the end_io handler clears the writeback ranges
2141 */
2142static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2143 void *data)
2144{
2145 struct inode *inode = page->mapping->host;
2146 struct extent_page_data *epd = data;
2147 struct extent_io_tree *tree = epd->tree;
2148 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2149 u64 delalloc_start;
2150 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2151 u64 end;
2152 u64 cur = start;
2153 u64 extent_offset;
2154 u64 last_byte = i_size_read(inode);
2155 u64 block_start;
2156 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002157 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002158 sector_t sector;
2159 struct extent_map *em;
2160 struct block_device *bdev;
2161 int ret;
2162 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002163 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002164 size_t blocksize;
2165 loff_t i_size = i_size_read(inode);
2166 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2167 u64 nr_delalloc;
2168 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002169 int page_started;
2170 int compressed;
Chris Mason771ed682008-11-06 22:02:51 -05002171 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002172
2173 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002174 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002175 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002176 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002177 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002178 unlock_page(page);
2179 return 0;
2180 }
2181
2182 if (page->index == end_index) {
2183 char *userpage;
2184
Chris Masond1310b22008-01-24 16:13:08 -05002185 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002186 memset(userpage + pg_offset, 0,
2187 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002188 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002189 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002190 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002191 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002192
2193 set_page_extent_mapped(page);
2194
2195 delalloc_start = start;
2196 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002197 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002198 if (!epd->extent_locked) {
2199 while(delalloc_end < page_end) {
2200 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002201 page,
2202 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002203 &delalloc_end,
2204 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002205 if (nr_delalloc == 0) {
2206 delalloc_start = delalloc_end + 1;
2207 continue;
2208 }
2209 tree->ops->fill_delalloc(inode, page, delalloc_start,
2210 delalloc_end, &page_started,
2211 &nr_written);
Chris Masond1310b22008-01-24 16:13:08 -05002212 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002213 }
Chris Masonc8b97812008-10-29 14:49:59 -04002214
Chris Mason771ed682008-11-06 22:02:51 -05002215 /* did the fill delalloc function already unlock and start
2216 * the IO?
2217 */
2218 if (page_started) {
2219 ret = 0;
2220 goto update_nr_written;
2221 }
Chris Masonc8b97812008-10-29 14:49:59 -04002222 }
Chris Masond1310b22008-01-24 16:13:08 -05002223 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002224
Chris Masone6dcd2d2008-07-17 12:53:50 -04002225 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002226
Chris Mason247e7432008-07-17 12:53:51 -04002227 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002228 ret = tree->ops->writepage_start_hook(page, start,
2229 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002230 if (ret == -EAGAIN) {
2231 unlock_extent(tree, start, page_end, GFP_NOFS);
2232 redirty_page_for_writepage(wbc, page);
2233 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002234 ret = 0;
2235 goto update_nr_written;
Chris Mason247e7432008-07-17 12:53:51 -04002236 }
2237 }
2238
Chris Mason771ed682008-11-06 22:02:51 -05002239 nr_written++;
2240
Chris Masond1310b22008-01-24 16:13:08 -05002241 end = page_end;
2242 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2243 printk("found delalloc bits after lock_extent\n");
2244 }
2245
2246 if (last_byte <= start) {
2247 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002248 unlock_extent(tree, start, page_end, GFP_NOFS);
2249 if (tree->ops && tree->ops->writepage_end_io_hook)
2250 tree->ops->writepage_end_io_hook(page, start,
2251 page_end, NULL, 1);
2252 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002253 goto done;
2254 }
2255
2256 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2257 blocksize = inode->i_sb->s_blocksize;
2258
2259 while (cur <= end) {
2260 if (cur >= last_byte) {
2261 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002262 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2263 if (tree->ops && tree->ops->writepage_end_io_hook)
2264 tree->ops->writepage_end_io_hook(page, cur,
2265 page_end, NULL, 1);
2266 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002267 break;
2268 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002269 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002270 end - cur + 1, 1);
2271 if (IS_ERR(em) || !em) {
2272 SetPageError(page);
2273 break;
2274 }
2275
2276 extent_offset = cur - em->start;
2277 BUG_ON(extent_map_end(em) <= cur);
2278 BUG_ON(end < cur);
2279 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2280 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2281 sector = (em->block_start + extent_offset) >> 9;
2282 bdev = em->bdev;
2283 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002284 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002285 free_extent_map(em);
2286 em = NULL;
2287
Chris Masonc8b97812008-10-29 14:49:59 -04002288 /*
2289 * compressed and inline extents are written through other
2290 * paths in the FS
2291 */
2292 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002293 block_start == EXTENT_MAP_INLINE) {
2294 clear_extent_dirty(tree, cur,
2295 cur + iosize - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002296
2297 unlock_extent(tree, unlock_start, cur + iosize -1,
2298 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002299
Chris Masonc8b97812008-10-29 14:49:59 -04002300 /*
2301 * end_io notification does not happen here for
2302 * compressed extents
2303 */
2304 if (!compressed && tree->ops &&
2305 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002306 tree->ops->writepage_end_io_hook(page, cur,
2307 cur + iosize - 1,
2308 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002309 else if (compressed) {
2310 /* we don't want to end_page_writeback on
2311 * a compressed extent. this happens
2312 * elsewhere
2313 */
2314 nr++;
2315 }
2316
2317 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002318 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002319 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002320 continue;
2321 }
Chris Masond1310b22008-01-24 16:13:08 -05002322 /* leave this out until we have a page_mkwrite call */
2323 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2324 EXTENT_DIRTY, 0)) {
2325 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002326 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002327 continue;
2328 }
Chris Masonc8b97812008-10-29 14:49:59 -04002329
Chris Masond1310b22008-01-24 16:13:08 -05002330 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2331 if (tree->ops && tree->ops->writepage_io_hook) {
2332 ret = tree->ops->writepage_io_hook(page, cur,
2333 cur + iosize - 1);
2334 } else {
2335 ret = 0;
2336 }
Chris Mason1259ab72008-05-12 13:39:03 -04002337 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002338 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002339 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002340 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002341
Chris Masond1310b22008-01-24 16:13:08 -05002342 set_range_writeback(tree, cur, cur + iosize - 1);
2343 if (!PageWriteback(page)) {
2344 printk("warning page %lu not writeback, "
2345 "cur %llu end %llu\n", page->index,
2346 (unsigned long long)cur,
2347 (unsigned long long)end);
2348 }
2349
2350 ret = submit_extent_page(WRITE, tree, page, sector,
Chris Mason7f3c74f2008-07-18 12:01:11 -04002351 iosize, pg_offset, bdev,
Chris Masond1310b22008-01-24 16:13:08 -05002352 &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002353 end_bio_extent_writepage,
2354 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002355 if (ret)
2356 SetPageError(page);
2357 }
2358 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002359 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002360 nr++;
2361 }
2362done:
2363 if (nr == 0) {
2364 /* make sure the mapping tag for page dirty gets cleared */
2365 set_page_writeback(page);
2366 end_page_writeback(page);
2367 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002368 if (unlock_start <= page_end)
2369 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002370 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002371
2372update_nr_written:
2373 wbc->nr_to_write -= nr_written;
2374 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2375 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2376 page->mapping->writeback_index = page->index + nr_written;
Chris Masond1310b22008-01-24 16:13:08 -05002377 return 0;
2378}
2379
Chris Masond1310b22008-01-24 16:13:08 -05002380/**
Chris Mason4bef0842008-09-08 11:18:08 -04002381 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05002382 * @mapping: address space structure to write
2383 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2384 * @writepage: function called for each page
2385 * @data: data passed to writepage function
2386 *
2387 * If a page is already under I/O, write_cache_pages() skips it, even
2388 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2389 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2390 * and msync() need to guarantee that all the data which was dirty at the time
2391 * the call was made get new I/O started against them. If wbc->sync_mode is
2392 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2393 * existing IO to complete.
2394 */
Chris Mason4bef0842008-09-08 11:18:08 -04002395int extent_write_cache_pages(struct extent_io_tree *tree,
2396 struct address_space *mapping,
2397 struct writeback_control *wbc,
2398 writepage_t writepage, void *data)
Chris Masond1310b22008-01-24 16:13:08 -05002399{
2400 struct backing_dev_info *bdi = mapping->backing_dev_info;
2401 int ret = 0;
2402 int done = 0;
2403 struct pagevec pvec;
2404 int nr_pages;
2405 pgoff_t index;
2406 pgoff_t end; /* Inclusive */
2407 int scanned = 0;
2408 int range_whole = 0;
2409
2410 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2411 wbc->encountered_congestion = 1;
2412 return 0;
2413 }
2414
2415 pagevec_init(&pvec, 0);
2416 if (wbc->range_cyclic) {
2417 index = mapping->writeback_index; /* Start from prev offset */
2418 end = -1;
2419 } else {
2420 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2421 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2422 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2423 range_whole = 1;
2424 scanned = 1;
2425 }
2426retry:
2427 while (!done && (index <= end) &&
2428 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2429 PAGECACHE_TAG_DIRTY,
2430 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2431 unsigned i;
2432
2433 scanned = 1;
2434 for (i = 0; i < nr_pages; i++) {
2435 struct page *page = pvec.pages[i];
2436
2437 /*
2438 * At this point we hold neither mapping->tree_lock nor
2439 * lock on the page itself: the page may be truncated or
2440 * invalidated (changing page->mapping to NULL), or even
2441 * swizzled back from swapper_space to tmpfs file
2442 * mapping
2443 */
Chris Mason4bef0842008-09-08 11:18:08 -04002444 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2445 tree->ops->write_cache_pages_lock_hook(page);
2446 else
2447 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002448
2449 if (unlikely(page->mapping != mapping)) {
2450 unlock_page(page);
2451 continue;
2452 }
2453
2454 if (!wbc->range_cyclic && page->index > end) {
2455 done = 1;
2456 unlock_page(page);
2457 continue;
2458 }
2459
2460 if (wbc->sync_mode != WB_SYNC_NONE)
2461 wait_on_page_writeback(page);
2462
2463 if (PageWriteback(page) ||
2464 !clear_page_dirty_for_io(page)) {
2465 unlock_page(page);
2466 continue;
2467 }
2468
2469 ret = (*writepage)(page, wbc, data);
2470
2471 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2472 unlock_page(page);
2473 ret = 0;
2474 }
Chris Mason771ed682008-11-06 22:02:51 -05002475 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002476 done = 1;
2477 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2478 wbc->encountered_congestion = 1;
2479 done = 1;
2480 }
2481 }
2482 pagevec_release(&pvec);
2483 cond_resched();
2484 }
2485 if (!scanned && !done) {
2486 /*
2487 * We hit the last page and there is more work to be done: wrap
2488 * back to the start of the file
2489 */
2490 scanned = 1;
2491 index = 0;
2492 goto retry;
2493 }
Chris Masond1310b22008-01-24 16:13:08 -05002494 return ret;
2495}
Chris Mason4bef0842008-09-08 11:18:08 -04002496EXPORT_SYMBOL(extent_write_cache_pages);
Chris Masond1310b22008-01-24 16:13:08 -05002497
2498int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2499 get_extent_t *get_extent,
2500 struct writeback_control *wbc)
2501{
2502 int ret;
2503 struct address_space *mapping = page->mapping;
2504 struct extent_page_data epd = {
2505 .bio = NULL,
2506 .tree = tree,
2507 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002508 .extent_locked = 0,
Chris Masond1310b22008-01-24 16:13:08 -05002509 };
2510 struct writeback_control wbc_writepages = {
2511 .bdi = wbc->bdi,
2512 .sync_mode = WB_SYNC_NONE,
2513 .older_than_this = NULL,
2514 .nr_to_write = 64,
2515 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2516 .range_end = (loff_t)-1,
2517 };
2518
2519
2520 ret = __extent_writepage(page, wbc, &epd);
2521
Chris Mason4bef0842008-09-08 11:18:08 -04002522 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2523 __extent_writepage, &epd);
Chris Masond1310b22008-01-24 16:13:08 -05002524 if (epd.bio) {
Chris Masonc8b97812008-10-29 14:49:59 -04002525 submit_one_bio(WRITE, epd.bio, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002526 }
2527 return ret;
2528}
2529EXPORT_SYMBOL(extent_write_full_page);
2530
Chris Mason771ed682008-11-06 22:02:51 -05002531int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2532 u64 start, u64 end, get_extent_t *get_extent,
2533 int mode)
2534{
2535 int ret = 0;
2536 struct address_space *mapping = inode->i_mapping;
2537 struct page *page;
2538 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2539 PAGE_CACHE_SHIFT;
2540
2541 struct extent_page_data epd = {
2542 .bio = NULL,
2543 .tree = tree,
2544 .get_extent = get_extent,
2545 .extent_locked = 1,
2546 };
2547 struct writeback_control wbc_writepages = {
2548 .bdi = inode->i_mapping->backing_dev_info,
2549 .sync_mode = mode,
2550 .older_than_this = NULL,
2551 .nr_to_write = nr_pages * 2,
2552 .range_start = start,
2553 .range_end = end + 1,
2554 };
2555
2556 while(start <= end) {
2557 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2558 if (clear_page_dirty_for_io(page))
2559 ret = __extent_writepage(page, &wbc_writepages, &epd);
2560 else {
2561 if (tree->ops && tree->ops->writepage_end_io_hook)
2562 tree->ops->writepage_end_io_hook(page, start,
2563 start + PAGE_CACHE_SIZE - 1,
2564 NULL, 1);
2565 unlock_page(page);
2566 }
2567 page_cache_release(page);
2568 start += PAGE_CACHE_SIZE;
2569 }
2570
2571 if (epd.bio)
2572 submit_one_bio(WRITE, epd.bio, 0, 0);
2573 return ret;
2574}
2575EXPORT_SYMBOL(extent_write_locked_range);
2576
Chris Masond1310b22008-01-24 16:13:08 -05002577
2578int extent_writepages(struct extent_io_tree *tree,
2579 struct address_space *mapping,
2580 get_extent_t *get_extent,
2581 struct writeback_control *wbc)
2582{
2583 int ret = 0;
2584 struct extent_page_data epd = {
2585 .bio = NULL,
2586 .tree = tree,
2587 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002588 .extent_locked = 0,
Chris Masond1310b22008-01-24 16:13:08 -05002589 };
2590
Chris Mason4bef0842008-09-08 11:18:08 -04002591 ret = extent_write_cache_pages(tree, mapping, wbc,
2592 __extent_writepage, &epd);
Chris Masond1310b22008-01-24 16:13:08 -05002593 if (epd.bio) {
Chris Masonc8b97812008-10-29 14:49:59 -04002594 submit_one_bio(WRITE, epd.bio, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002595 }
2596 return ret;
2597}
2598EXPORT_SYMBOL(extent_writepages);
2599
2600int extent_readpages(struct extent_io_tree *tree,
2601 struct address_space *mapping,
2602 struct list_head *pages, unsigned nr_pages,
2603 get_extent_t get_extent)
2604{
2605 struct bio *bio = NULL;
2606 unsigned page_idx;
2607 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002608 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002609
2610 pagevec_init(&pvec, 0);
2611 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2612 struct page *page = list_entry(pages->prev, struct page, lru);
2613
2614 prefetchw(&page->flags);
2615 list_del(&page->lru);
2616 /*
2617 * what we want to do here is call add_to_page_cache_lru,
2618 * but that isn't exported, so we reproduce it here
2619 */
2620 if (!add_to_page_cache(page, mapping,
2621 page->index, GFP_KERNEL)) {
2622
2623 /* open coding of lru_cache_add, also not exported */
2624 page_cache_get(page);
2625 if (!pagevec_add(&pvec, page))
2626 __pagevec_lru_add(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002627 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002628 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002629 }
2630 page_cache_release(page);
2631 }
2632 if (pagevec_count(&pvec))
2633 __pagevec_lru_add(&pvec);
2634 BUG_ON(!list_empty(pages));
2635 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002636 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002637 return 0;
2638}
2639EXPORT_SYMBOL(extent_readpages);
2640
2641/*
2642 * basic invalidatepage code, this waits on any locked or writeback
2643 * ranges corresponding to the page, and then deletes any extent state
2644 * records from the tree
2645 */
2646int extent_invalidatepage(struct extent_io_tree *tree,
2647 struct page *page, unsigned long offset)
2648{
2649 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2650 u64 end = start + PAGE_CACHE_SIZE - 1;
2651 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2652
2653 start += (offset + blocksize -1) & ~(blocksize - 1);
2654 if (start > end)
2655 return 0;
2656
2657 lock_extent(tree, start, end, GFP_NOFS);
2658 wait_on_extent_writeback(tree, start, end);
2659 clear_extent_bit(tree, start, end,
2660 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2661 1, 1, GFP_NOFS);
2662 return 0;
2663}
2664EXPORT_SYMBOL(extent_invalidatepage);
2665
2666/*
2667 * simple commit_write call, set_range_dirty is used to mark both
2668 * the pages and the extent records as dirty
2669 */
2670int extent_commit_write(struct extent_io_tree *tree,
2671 struct inode *inode, struct page *page,
2672 unsigned from, unsigned to)
2673{
2674 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2675
2676 set_page_extent_mapped(page);
2677 set_page_dirty(page);
2678
2679 if (pos > inode->i_size) {
2680 i_size_write(inode, pos);
2681 mark_inode_dirty(inode);
2682 }
2683 return 0;
2684}
2685EXPORT_SYMBOL(extent_commit_write);
2686
2687int extent_prepare_write(struct extent_io_tree *tree,
2688 struct inode *inode, struct page *page,
2689 unsigned from, unsigned to, get_extent_t *get_extent)
2690{
2691 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2692 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2693 u64 block_start;
2694 u64 orig_block_start;
2695 u64 block_end;
2696 u64 cur_end;
2697 struct extent_map *em;
2698 unsigned blocksize = 1 << inode->i_blkbits;
2699 size_t page_offset = 0;
2700 size_t block_off_start;
2701 size_t block_off_end;
2702 int err = 0;
2703 int iocount = 0;
2704 int ret = 0;
2705 int isnew;
2706
2707 set_page_extent_mapped(page);
2708
2709 block_start = (page_start + from) & ~((u64)blocksize - 1);
2710 block_end = (page_start + to - 1) | (blocksize - 1);
2711 orig_block_start = block_start;
2712
2713 lock_extent(tree, page_start, page_end, GFP_NOFS);
2714 while(block_start <= block_end) {
2715 em = get_extent(inode, page, page_offset, block_start,
2716 block_end - block_start + 1, 1);
2717 if (IS_ERR(em) || !em) {
2718 goto err;
2719 }
2720 cur_end = min(block_end, extent_map_end(em) - 1);
2721 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2722 block_off_end = block_off_start + blocksize;
2723 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2724
2725 if (!PageUptodate(page) && isnew &&
2726 (block_off_end > to || block_off_start < from)) {
2727 void *kaddr;
2728
2729 kaddr = kmap_atomic(page, KM_USER0);
2730 if (block_off_end > to)
2731 memset(kaddr + to, 0, block_off_end - to);
2732 if (block_off_start < from)
2733 memset(kaddr + block_off_start, 0,
2734 from - block_off_start);
2735 flush_dcache_page(page);
2736 kunmap_atomic(kaddr, KM_USER0);
2737 }
2738 if ((em->block_start != EXTENT_MAP_HOLE &&
2739 em->block_start != EXTENT_MAP_INLINE) &&
2740 !isnew && !PageUptodate(page) &&
2741 (block_off_end > to || block_off_start < from) &&
2742 !test_range_bit(tree, block_start, cur_end,
2743 EXTENT_UPTODATE, 1)) {
2744 u64 sector;
2745 u64 extent_offset = block_start - em->start;
2746 size_t iosize;
2747 sector = (em->block_start + extent_offset) >> 9;
2748 iosize = (cur_end - block_start + blocksize) &
2749 ~((u64)blocksize - 1);
2750 /*
2751 * we've already got the extent locked, but we
2752 * need to split the state such that our end_bio
2753 * handler can clear the lock.
2754 */
2755 set_extent_bit(tree, block_start,
2756 block_start + iosize - 1,
2757 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2758 ret = submit_extent_page(READ, tree, page,
2759 sector, iosize, page_offset, em->bdev,
2760 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002761 end_bio_extent_preparewrite, 0,
2762 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002763 iocount++;
2764 block_start = block_start + iosize;
2765 } else {
2766 set_extent_uptodate(tree, block_start, cur_end,
2767 GFP_NOFS);
2768 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2769 block_start = cur_end + 1;
2770 }
2771 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2772 free_extent_map(em);
2773 }
2774 if (iocount) {
2775 wait_extent_bit(tree, orig_block_start,
2776 block_end, EXTENT_LOCKED);
2777 }
2778 check_page_uptodate(tree, page);
2779err:
2780 /* FIXME, zero out newly allocated blocks on error */
2781 return err;
2782}
2783EXPORT_SYMBOL(extent_prepare_write);
2784
2785/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002786 * a helper for releasepage, this tests for areas of the page that
2787 * are locked or under IO and drops the related state bits if it is safe
2788 * to drop the page.
2789 */
2790int try_release_extent_state(struct extent_map_tree *map,
2791 struct extent_io_tree *tree, struct page *page,
2792 gfp_t mask)
2793{
2794 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2795 u64 end = start + PAGE_CACHE_SIZE - 1;
2796 int ret = 1;
2797
Chris Mason211f90e2008-07-18 11:56:15 -04002798 if (test_range_bit(tree, start, end,
2799 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002800 ret = 0;
2801 else {
2802 if ((mask & GFP_NOFS) == GFP_NOFS)
2803 mask = GFP_NOFS;
2804 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2805 1, 1, mask);
2806 }
2807 return ret;
2808}
2809EXPORT_SYMBOL(try_release_extent_state);
2810
2811/*
Chris Masond1310b22008-01-24 16:13:08 -05002812 * a helper for releasepage. As long as there are no locked extents
2813 * in the range corresponding to the page, both state records and extent
2814 * map records are removed
2815 */
2816int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002817 struct extent_io_tree *tree, struct page *page,
2818 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002819{
2820 struct extent_map *em;
2821 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2822 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002823
Chris Mason70dec802008-01-29 09:59:12 -05002824 if ((mask & __GFP_WAIT) &&
2825 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002826 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002827 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002828 len = end - start + 1;
Chris Mason70dec802008-01-29 09:59:12 -05002829 spin_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002830 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002831 if (!em || IS_ERR(em)) {
2832 spin_unlock(&map->lock);
2833 break;
2834 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002835 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2836 em->start != start) {
Chris Mason70dec802008-01-29 09:59:12 -05002837 spin_unlock(&map->lock);
2838 free_extent_map(em);
2839 break;
2840 }
2841 if (!test_range_bit(tree, em->start,
2842 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002843 EXTENT_LOCKED | EXTENT_WRITEBACK |
2844 EXTENT_ORDERED,
2845 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002846 remove_extent_mapping(map, em);
2847 /* once for the rb tree */
2848 free_extent_map(em);
2849 }
2850 start = extent_map_end(em);
Chris Masond1310b22008-01-24 16:13:08 -05002851 spin_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002852
2853 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002854 free_extent_map(em);
2855 }
Chris Masond1310b22008-01-24 16:13:08 -05002856 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002857 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002858}
2859EXPORT_SYMBOL(try_release_extent_mapping);
2860
2861sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2862 get_extent_t *get_extent)
2863{
2864 struct inode *inode = mapping->host;
2865 u64 start = iblock << inode->i_blkbits;
2866 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002867 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002868 struct extent_map *em;
2869
Yan Zhengd899e052008-10-30 14:25:28 -04002870 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2871 GFP_NOFS);
2872 em = get_extent(inode, NULL, 0, start, blksize, 0);
2873 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2874 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002875 if (!em || IS_ERR(em))
2876 return 0;
2877
Yan Zhengd899e052008-10-30 14:25:28 -04002878 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002879 goto out;
2880
2881 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002882out:
2883 free_extent_map(em);
2884 return sector;
2885}
2886
Chris Masond1310b22008-01-24 16:13:08 -05002887static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2888 unsigned long i)
2889{
2890 struct page *p;
2891 struct address_space *mapping;
2892
2893 if (i == 0)
2894 return eb->first_page;
2895 i += eb->start >> PAGE_CACHE_SHIFT;
2896 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002897 if (!mapping)
2898 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002899
2900 /*
2901 * extent_buffer_page is only called after pinning the page
2902 * by increasing the reference count. So we know the page must
2903 * be in the radix tree.
2904 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002905 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002906 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002907 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002908
Chris Masond1310b22008-01-24 16:13:08 -05002909 return p;
2910}
2911
Chris Mason6af118ce2008-07-22 11:18:07 -04002912static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002913{
Chris Mason6af118ce2008-07-22 11:18:07 -04002914 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2915 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002916}
2917
Chris Masond1310b22008-01-24 16:13:08 -05002918static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2919 u64 start,
2920 unsigned long len,
2921 gfp_t mask)
2922{
2923 struct extent_buffer *eb = NULL;
Chris Mason4bef0842008-09-08 11:18:08 -04002924#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002925 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04002926#endif
Chris Masond1310b22008-01-24 16:13:08 -05002927
Chris Masond1310b22008-01-24 16:13:08 -05002928 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002929 eb->start = start;
2930 eb->len = len;
Chris Masona61e6f22008-07-22 11:18:08 -04002931 mutex_init(&eb->mutex);
Chris Mason4bef0842008-09-08 11:18:08 -04002932#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002933 spin_lock_irqsave(&leak_lock, flags);
2934 list_add(&eb->leak_list, &buffers);
2935 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002936#endif
Chris Masond1310b22008-01-24 16:13:08 -05002937 atomic_set(&eb->refs, 1);
2938
2939 return eb;
2940}
2941
2942static void __free_extent_buffer(struct extent_buffer *eb)
2943{
Chris Mason4bef0842008-09-08 11:18:08 -04002944#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002945 unsigned long flags;
2946 spin_lock_irqsave(&leak_lock, flags);
2947 list_del(&eb->leak_list);
2948 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002949#endif
Chris Masond1310b22008-01-24 16:13:08 -05002950 kmem_cache_free(extent_buffer_cache, eb);
2951}
2952
2953struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2954 u64 start, unsigned long len,
2955 struct page *page0,
2956 gfp_t mask)
2957{
2958 unsigned long num_pages = num_extent_pages(start, len);
2959 unsigned long i;
2960 unsigned long index = start >> PAGE_CACHE_SHIFT;
2961 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04002962 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002963 struct page *p;
2964 struct address_space *mapping = tree->mapping;
2965 int uptodate = 1;
2966
Chris Mason6af118ce2008-07-22 11:18:07 -04002967 spin_lock(&tree->buffer_lock);
2968 eb = buffer_search(tree, start);
2969 if (eb) {
2970 atomic_inc(&eb->refs);
2971 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002972 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002973 return eb;
2974 }
2975 spin_unlock(&tree->buffer_lock);
2976
Chris Masond1310b22008-01-24 16:13:08 -05002977 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04002978 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05002979 return NULL;
2980
Chris Masond1310b22008-01-24 16:13:08 -05002981 if (page0) {
2982 eb->first_page = page0;
2983 i = 1;
2984 index++;
2985 page_cache_get(page0);
2986 mark_page_accessed(page0);
2987 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002988 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04002989 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002990 } else {
2991 i = 0;
2992 }
2993 for (; i < num_pages; i++, index++) {
2994 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
2995 if (!p) {
2996 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04002997 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05002998 }
2999 set_page_extent_mapped(p);
3000 mark_page_accessed(p);
3001 if (i == 0) {
3002 eb->first_page = p;
3003 set_page_extent_head(p, len);
3004 } else {
3005 set_page_private(p, EXTENT_PAGE_PRIVATE);
3006 }
3007 if (!PageUptodate(p))
3008 uptodate = 0;
3009 unlock_page(p);
3010 }
3011 if (uptodate)
3012 eb->flags |= EXTENT_UPTODATE;
3013 eb->flags |= EXTENT_BUFFER_FILLED;
3014
Chris Mason6af118ce2008-07-22 11:18:07 -04003015 spin_lock(&tree->buffer_lock);
3016 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3017 if (exists) {
3018 /* add one reference for the caller */
3019 atomic_inc(&exists->refs);
3020 spin_unlock(&tree->buffer_lock);
3021 goto free_eb;
3022 }
3023 spin_unlock(&tree->buffer_lock);
3024
3025 /* add one reference for the tree */
3026 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003027 return eb;
3028
Chris Mason6af118ce2008-07-22 11:18:07 -04003029free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003030 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003031 return exists;
3032 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003033 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003034 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003035 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003036 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003037}
3038EXPORT_SYMBOL(alloc_extent_buffer);
3039
3040struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3041 u64 start, unsigned long len,
3042 gfp_t mask)
3043{
Chris Masond1310b22008-01-24 16:13:08 -05003044 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003045
Chris Mason6af118ce2008-07-22 11:18:07 -04003046 spin_lock(&tree->buffer_lock);
3047 eb = buffer_search(tree, start);
3048 if (eb)
3049 atomic_inc(&eb->refs);
3050 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003051
Josef Bacik0f9dd462008-09-23 13:14:11 -04003052 if (eb)
3053 mark_page_accessed(eb->first_page);
3054
Chris Masond1310b22008-01-24 16:13:08 -05003055 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003056}
3057EXPORT_SYMBOL(find_extent_buffer);
3058
3059void free_extent_buffer(struct extent_buffer *eb)
3060{
Chris Masond1310b22008-01-24 16:13:08 -05003061 if (!eb)
3062 return;
3063
3064 if (!atomic_dec_and_test(&eb->refs))
3065 return;
3066
Chris Mason6af118ce2008-07-22 11:18:07 -04003067 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003068}
3069EXPORT_SYMBOL(free_extent_buffer);
3070
3071int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3072 struct extent_buffer *eb)
3073{
3074 int set;
3075 unsigned long i;
3076 unsigned long num_pages;
3077 struct page *page;
3078
3079 u64 start = eb->start;
3080 u64 end = start + eb->len - 1;
3081
3082 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
3083 num_pages = num_extent_pages(eb->start, eb->len);
3084
3085 for (i = 0; i < num_pages; i++) {
3086 page = extent_buffer_page(eb, i);
Chris Masona61e6f22008-07-22 11:18:08 -04003087 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003088 if (i == 0)
3089 set_page_extent_head(page, eb->len);
3090 else
3091 set_page_private(page, EXTENT_PAGE_PRIVATE);
3092
3093 /*
3094 * if we're on the last page or the first page and the
3095 * block isn't aligned on a page boundary, do extra checks
3096 * to make sure we don't clean page that is partially dirty
3097 */
3098 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3099 ((i == num_pages - 1) &&
3100 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3101 start = (u64)page->index << PAGE_CACHE_SHIFT;
3102 end = start + PAGE_CACHE_SIZE - 1;
3103 if (test_range_bit(tree, start, end,
3104 EXTENT_DIRTY, 0)) {
Chris Masona61e6f22008-07-22 11:18:08 -04003105 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003106 continue;
3107 }
3108 }
3109 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003110 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003111 if (!PageDirty(page)) {
3112 radix_tree_tag_clear(&page->mapping->page_tree,
3113 page_index(page),
3114 PAGECACHE_TAG_DIRTY);
3115 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003116 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003117 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003118 }
3119 return 0;
3120}
3121EXPORT_SYMBOL(clear_extent_buffer_dirty);
3122
3123int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3124 struct extent_buffer *eb)
3125{
3126 return wait_on_extent_writeback(tree, eb->start,
3127 eb->start + eb->len - 1);
3128}
3129EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
3130
3131int set_extent_buffer_dirty(struct extent_io_tree *tree,
3132 struct extent_buffer *eb)
3133{
3134 unsigned long i;
3135 unsigned long num_pages;
3136
3137 num_pages = num_extent_pages(eb->start, eb->len);
3138 for (i = 0; i < num_pages; i++) {
3139 struct page *page = extent_buffer_page(eb, i);
3140 /* writepage may need to do something special for the
3141 * first page, we have to make sure page->private is
3142 * properly set. releasepage may drop page->private
3143 * on us if the page isn't already dirty.
3144 */
Chris Masona1b32a52008-09-05 16:09:51 -04003145 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003146 if (i == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003147 set_page_extent_head(page, eb->len);
3148 } else if (PagePrivate(page) &&
3149 page->private != EXTENT_PAGE_PRIVATE) {
Chris Masond1310b22008-01-24 16:13:08 -05003150 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003151 }
3152 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masona1b32a52008-09-05 16:09:51 -04003153 set_extent_dirty(tree, page_offset(page),
3154 page_offset(page) + PAGE_CACHE_SIZE -1,
3155 GFP_NOFS);
3156 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003157 }
Chris Masona1b32a52008-09-05 16:09:51 -04003158 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05003159}
3160EXPORT_SYMBOL(set_extent_buffer_dirty);
3161
Chris Mason1259ab72008-05-12 13:39:03 -04003162int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3163 struct extent_buffer *eb)
3164{
3165 unsigned long i;
3166 struct page *page;
3167 unsigned long num_pages;
3168
3169 num_pages = num_extent_pages(eb->start, eb->len);
3170 eb->flags &= ~EXTENT_UPTODATE;
3171
3172 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3173 GFP_NOFS);
3174 for (i = 0; i < num_pages; i++) {
3175 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003176 if (page)
3177 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003178 }
3179 return 0;
3180}
3181
Chris Masond1310b22008-01-24 16:13:08 -05003182int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3183 struct extent_buffer *eb)
3184{
3185 unsigned long i;
3186 struct page *page;
3187 unsigned long num_pages;
3188
3189 num_pages = num_extent_pages(eb->start, eb->len);
3190
3191 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3192 GFP_NOFS);
3193 for (i = 0; i < num_pages; i++) {
3194 page = extent_buffer_page(eb, i);
3195 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3196 ((i == num_pages - 1) &&
3197 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3198 check_page_uptodate(tree, page);
3199 continue;
3200 }
3201 SetPageUptodate(page);
3202 }
3203 return 0;
3204}
3205EXPORT_SYMBOL(set_extent_buffer_uptodate);
3206
Chris Masonce9adaa2008-04-09 16:28:12 -04003207int extent_range_uptodate(struct extent_io_tree *tree,
3208 u64 start, u64 end)
3209{
3210 struct page *page;
3211 int ret;
3212 int pg_uptodate = 1;
3213 int uptodate;
3214 unsigned long index;
3215
3216 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3217 if (ret)
3218 return 1;
3219 while(start <= end) {
3220 index = start >> PAGE_CACHE_SHIFT;
3221 page = find_get_page(tree->mapping, index);
3222 uptodate = PageUptodate(page);
3223 page_cache_release(page);
3224 if (!uptodate) {
3225 pg_uptodate = 0;
3226 break;
3227 }
3228 start += PAGE_CACHE_SIZE;
3229 }
3230 return pg_uptodate;
3231}
3232
Chris Masond1310b22008-01-24 16:13:08 -05003233int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003234 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003235{
Chris Mason728131d2008-04-09 16:28:12 -04003236 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003237 unsigned long num_pages;
3238 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003239 struct page *page;
3240 int pg_uptodate = 1;
3241
Chris Masond1310b22008-01-24 16:13:08 -05003242 if (eb->flags & EXTENT_UPTODATE)
Chris Mason42352982008-04-28 16:40:52 -04003243 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003244
Chris Mason42352982008-04-28 16:40:52 -04003245 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003246 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003247 if (ret)
3248 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003249
3250 num_pages = num_extent_pages(eb->start, eb->len);
3251 for (i = 0; i < num_pages; i++) {
3252 page = extent_buffer_page(eb, i);
3253 if (!PageUptodate(page)) {
3254 pg_uptodate = 0;
3255 break;
3256 }
3257 }
Chris Mason42352982008-04-28 16:40:52 -04003258 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003259}
3260EXPORT_SYMBOL(extent_buffer_uptodate);
3261
3262int read_extent_buffer_pages(struct extent_io_tree *tree,
3263 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003264 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003265 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003266{
3267 unsigned long i;
3268 unsigned long start_i;
3269 struct page *page;
3270 int err;
3271 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003272 int locked_pages = 0;
3273 int all_uptodate = 1;
3274 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003275 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003276 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003277 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003278
Chris Masond1310b22008-01-24 16:13:08 -05003279 if (eb->flags & EXTENT_UPTODATE)
3280 return 0;
3281
Chris Masonce9adaa2008-04-09 16:28:12 -04003282 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003283 EXTENT_UPTODATE, 1)) {
3284 return 0;
3285 }
3286
3287 if (start) {
3288 WARN_ON(start < eb->start);
3289 start_i = (start >> PAGE_CACHE_SHIFT) -
3290 (eb->start >> PAGE_CACHE_SHIFT);
3291 } else {
3292 start_i = 0;
3293 }
3294
3295 num_pages = num_extent_pages(eb->start, eb->len);
3296 for (i = start_i; i < num_pages; i++) {
3297 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003298 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003299 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003300 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003301 } else {
3302 lock_page(page);
3303 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003304 locked_pages++;
Chris Masond1310b22008-01-24 16:13:08 -05003305 if (!PageUptodate(page)) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003306 all_uptodate = 0;
3307 }
3308 }
3309 if (all_uptodate) {
3310 if (start_i == 0)
3311 eb->flags |= EXTENT_UPTODATE;
Chris Masona1b32a52008-09-05 16:09:51 -04003312 if (ret) {
3313 printk("all up to date but ret is %d\n", ret);
3314 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003315 goto unlock_exit;
3316 }
3317
3318 for (i = start_i; i < num_pages; i++) {
3319 page = extent_buffer_page(eb, i);
3320 if (inc_all_pages)
3321 page_cache_get(page);
3322 if (!PageUptodate(page)) {
3323 if (start_i == 0)
3324 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003325 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003326 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003327 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003328 mirror_num, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003329 if (err) {
3330 ret = err;
Chris Masona1b32a52008-09-05 16:09:51 -04003331 printk("err %d from __extent_read_full_page\n", ret);
Chris Masond1310b22008-01-24 16:13:08 -05003332 }
3333 } else {
3334 unlock_page(page);
3335 }
3336 }
3337
Chris Masona86c12c2008-02-07 10:50:54 -05003338 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003339 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003340
Chris Masond1310b22008-01-24 16:13:08 -05003341 if (ret || !wait) {
Chris Masona1b32a52008-09-05 16:09:51 -04003342 if (ret)
3343 printk("ret %d wait %d returning\n", ret, wait);
Chris Masond1310b22008-01-24 16:13:08 -05003344 return ret;
3345 }
Chris Masond1310b22008-01-24 16:13:08 -05003346 for (i = start_i; i < num_pages; i++) {
3347 page = extent_buffer_page(eb, i);
3348 wait_on_page_locked(page);
3349 if (!PageUptodate(page)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003350 printk("page not uptodate after wait_on_page_locked\n");
Chris Masond1310b22008-01-24 16:13:08 -05003351 ret = -EIO;
3352 }
3353 }
3354 if (!ret)
3355 eb->flags |= EXTENT_UPTODATE;
3356 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003357
3358unlock_exit:
3359 i = start_i;
3360 while(locked_pages > 0) {
3361 page = extent_buffer_page(eb, i);
3362 i++;
3363 unlock_page(page);
3364 locked_pages--;
3365 }
3366 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003367}
3368EXPORT_SYMBOL(read_extent_buffer_pages);
3369
3370void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3371 unsigned long start,
3372 unsigned long len)
3373{
3374 size_t cur;
3375 size_t offset;
3376 struct page *page;
3377 char *kaddr;
3378 char *dst = (char *)dstv;
3379 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3380 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003381
3382 WARN_ON(start > eb->len);
3383 WARN_ON(start + len > eb->start + eb->len);
3384
3385 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3386
3387 while(len > 0) {
3388 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003389
3390 cur = min(len, (PAGE_CACHE_SIZE - offset));
3391 kaddr = kmap_atomic(page, KM_USER1);
3392 memcpy(dst, kaddr + offset, cur);
3393 kunmap_atomic(kaddr, KM_USER1);
3394
3395 dst += cur;
3396 len -= cur;
3397 offset = 0;
3398 i++;
3399 }
3400}
3401EXPORT_SYMBOL(read_extent_buffer);
3402
3403int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3404 unsigned long min_len, char **token, char **map,
3405 unsigned long *map_start,
3406 unsigned long *map_len, int km)
3407{
3408 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3409 char *kaddr;
3410 struct page *p;
3411 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3412 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3413 unsigned long end_i = (start_offset + start + min_len - 1) >>
3414 PAGE_CACHE_SHIFT;
3415
3416 if (i != end_i)
3417 return -EINVAL;
3418
3419 if (i == 0) {
3420 offset = start_offset;
3421 *map_start = 0;
3422 } else {
3423 offset = 0;
3424 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3425 }
3426 if (start + min_len > eb->len) {
3427printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3428 WARN_ON(1);
3429 }
3430
3431 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003432 kaddr = kmap_atomic(p, km);
3433 *token = kaddr;
3434 *map = kaddr + offset;
3435 *map_len = PAGE_CACHE_SIZE - offset;
3436 return 0;
3437}
3438EXPORT_SYMBOL(map_private_extent_buffer);
3439
3440int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3441 unsigned long min_len,
3442 char **token, char **map,
3443 unsigned long *map_start,
3444 unsigned long *map_len, int km)
3445{
3446 int err;
3447 int save = 0;
3448 if (eb->map_token) {
3449 unmap_extent_buffer(eb, eb->map_token, km);
3450 eb->map_token = NULL;
3451 save = 1;
3452 }
3453 err = map_private_extent_buffer(eb, start, min_len, token, map,
3454 map_start, map_len, km);
3455 if (!err && save) {
3456 eb->map_token = *token;
3457 eb->kaddr = *map;
3458 eb->map_start = *map_start;
3459 eb->map_len = *map_len;
3460 }
3461 return err;
3462}
3463EXPORT_SYMBOL(map_extent_buffer);
3464
3465void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3466{
3467 kunmap_atomic(token, km);
3468}
3469EXPORT_SYMBOL(unmap_extent_buffer);
3470
3471int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3472 unsigned long start,
3473 unsigned long len)
3474{
3475 size_t cur;
3476 size_t offset;
3477 struct page *page;
3478 char *kaddr;
3479 char *ptr = (char *)ptrv;
3480 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3481 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3482 int ret = 0;
3483
3484 WARN_ON(start > eb->len);
3485 WARN_ON(start + len > eb->start + eb->len);
3486
3487 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3488
3489 while(len > 0) {
3490 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003491
3492 cur = min(len, (PAGE_CACHE_SIZE - offset));
3493
3494 kaddr = kmap_atomic(page, KM_USER0);
3495 ret = memcmp(ptr, kaddr + offset, cur);
3496 kunmap_atomic(kaddr, KM_USER0);
3497 if (ret)
3498 break;
3499
3500 ptr += cur;
3501 len -= cur;
3502 offset = 0;
3503 i++;
3504 }
3505 return ret;
3506}
3507EXPORT_SYMBOL(memcmp_extent_buffer);
3508
3509void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3510 unsigned long start, unsigned long len)
3511{
3512 size_t cur;
3513 size_t offset;
3514 struct page *page;
3515 char *kaddr;
3516 char *src = (char *)srcv;
3517 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3518 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3519
3520 WARN_ON(start > eb->len);
3521 WARN_ON(start + len > eb->start + eb->len);
3522
3523 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3524
3525 while(len > 0) {
3526 page = extent_buffer_page(eb, i);
3527 WARN_ON(!PageUptodate(page));
3528
3529 cur = min(len, PAGE_CACHE_SIZE - offset);
3530 kaddr = kmap_atomic(page, KM_USER1);
3531 memcpy(kaddr + offset, src, cur);
3532 kunmap_atomic(kaddr, KM_USER1);
3533
3534 src += cur;
3535 len -= cur;
3536 offset = 0;
3537 i++;
3538 }
3539}
3540EXPORT_SYMBOL(write_extent_buffer);
3541
3542void memset_extent_buffer(struct extent_buffer *eb, char c,
3543 unsigned long start, unsigned long len)
3544{
3545 size_t cur;
3546 size_t offset;
3547 struct page *page;
3548 char *kaddr;
3549 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3550 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3551
3552 WARN_ON(start > eb->len);
3553 WARN_ON(start + len > eb->start + eb->len);
3554
3555 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3556
3557 while(len > 0) {
3558 page = extent_buffer_page(eb, i);
3559 WARN_ON(!PageUptodate(page));
3560
3561 cur = min(len, PAGE_CACHE_SIZE - offset);
3562 kaddr = kmap_atomic(page, KM_USER0);
3563 memset(kaddr + offset, c, cur);
3564 kunmap_atomic(kaddr, KM_USER0);
3565
3566 len -= cur;
3567 offset = 0;
3568 i++;
3569 }
3570}
3571EXPORT_SYMBOL(memset_extent_buffer);
3572
3573void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3574 unsigned long dst_offset, unsigned long src_offset,
3575 unsigned long len)
3576{
3577 u64 dst_len = dst->len;
3578 size_t cur;
3579 size_t offset;
3580 struct page *page;
3581 char *kaddr;
3582 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3583 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3584
3585 WARN_ON(src->len != dst_len);
3586
3587 offset = (start_offset + dst_offset) &
3588 ((unsigned long)PAGE_CACHE_SIZE - 1);
3589
3590 while(len > 0) {
3591 page = extent_buffer_page(dst, i);
3592 WARN_ON(!PageUptodate(page));
3593
3594 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3595
3596 kaddr = kmap_atomic(page, KM_USER0);
3597 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3598 kunmap_atomic(kaddr, KM_USER0);
3599
3600 src_offset += cur;
3601 len -= cur;
3602 offset = 0;
3603 i++;
3604 }
3605}
3606EXPORT_SYMBOL(copy_extent_buffer);
3607
3608static void move_pages(struct page *dst_page, struct page *src_page,
3609 unsigned long dst_off, unsigned long src_off,
3610 unsigned long len)
3611{
3612 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3613 if (dst_page == src_page) {
3614 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3615 } else {
3616 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3617 char *p = dst_kaddr + dst_off + len;
3618 char *s = src_kaddr + src_off + len;
3619
3620 while (len--)
3621 *--p = *--s;
3622
3623 kunmap_atomic(src_kaddr, KM_USER1);
3624 }
3625 kunmap_atomic(dst_kaddr, KM_USER0);
3626}
3627
3628static void copy_pages(struct page *dst_page, struct page *src_page,
3629 unsigned long dst_off, unsigned long src_off,
3630 unsigned long len)
3631{
3632 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3633 char *src_kaddr;
3634
3635 if (dst_page != src_page)
3636 src_kaddr = kmap_atomic(src_page, KM_USER1);
3637 else
3638 src_kaddr = dst_kaddr;
3639
3640 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3641 kunmap_atomic(dst_kaddr, KM_USER0);
3642 if (dst_page != src_page)
3643 kunmap_atomic(src_kaddr, KM_USER1);
3644}
3645
3646void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3647 unsigned long src_offset, unsigned long len)
3648{
3649 size_t cur;
3650 size_t dst_off_in_page;
3651 size_t src_off_in_page;
3652 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3653 unsigned long dst_i;
3654 unsigned long src_i;
3655
3656 if (src_offset + len > dst->len) {
3657 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3658 src_offset, len, dst->len);
3659 BUG_ON(1);
3660 }
3661 if (dst_offset + len > dst->len) {
3662 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3663 dst_offset, len, dst->len);
3664 BUG_ON(1);
3665 }
3666
3667 while(len > 0) {
3668 dst_off_in_page = (start_offset + dst_offset) &
3669 ((unsigned long)PAGE_CACHE_SIZE - 1);
3670 src_off_in_page = (start_offset + src_offset) &
3671 ((unsigned long)PAGE_CACHE_SIZE - 1);
3672
3673 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3674 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3675
3676 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3677 src_off_in_page));
3678 cur = min_t(unsigned long, cur,
3679 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3680
3681 copy_pages(extent_buffer_page(dst, dst_i),
3682 extent_buffer_page(dst, src_i),
3683 dst_off_in_page, src_off_in_page, cur);
3684
3685 src_offset += cur;
3686 dst_offset += cur;
3687 len -= cur;
3688 }
3689}
3690EXPORT_SYMBOL(memcpy_extent_buffer);
3691
3692void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3693 unsigned long src_offset, unsigned long len)
3694{
3695 size_t cur;
3696 size_t dst_off_in_page;
3697 size_t src_off_in_page;
3698 unsigned long dst_end = dst_offset + len - 1;
3699 unsigned long src_end = src_offset + len - 1;
3700 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3701 unsigned long dst_i;
3702 unsigned long src_i;
3703
3704 if (src_offset + len > dst->len) {
3705 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3706 src_offset, len, dst->len);
3707 BUG_ON(1);
3708 }
3709 if (dst_offset + len > dst->len) {
3710 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3711 dst_offset, len, dst->len);
3712 BUG_ON(1);
3713 }
3714 if (dst_offset < src_offset) {
3715 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3716 return;
3717 }
3718 while(len > 0) {
3719 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3720 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3721
3722 dst_off_in_page = (start_offset + dst_end) &
3723 ((unsigned long)PAGE_CACHE_SIZE - 1);
3724 src_off_in_page = (start_offset + src_end) &
3725 ((unsigned long)PAGE_CACHE_SIZE - 1);
3726
3727 cur = min_t(unsigned long, len, src_off_in_page + 1);
3728 cur = min(cur, dst_off_in_page + 1);
3729 move_pages(extent_buffer_page(dst, dst_i),
3730 extent_buffer_page(dst, src_i),
3731 dst_off_in_page - cur + 1,
3732 src_off_in_page - cur + 1, cur);
3733
3734 dst_end -= cur;
3735 src_end -= cur;
3736 len -= cur;
3737 }
3738}
3739EXPORT_SYMBOL(memmove_extent_buffer);
Chris Mason6af118ce2008-07-22 11:18:07 -04003740
3741int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3742{
3743 u64 start = page_offset(page);
3744 struct extent_buffer *eb;
3745 int ret = 1;
3746 unsigned long i;
3747 unsigned long num_pages;
3748
3749 spin_lock(&tree->buffer_lock);
3750 eb = buffer_search(tree, start);
3751 if (!eb)
3752 goto out;
3753
3754 if (atomic_read(&eb->refs) > 1) {
3755 ret = 0;
3756 goto out;
3757 }
3758 /* at this point we can safely release the extent buffer */
3759 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003760 for (i = 0; i < num_pages; i++)
3761 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003762 rb_erase(&eb->rb_node, &tree->buffer);
3763 __free_extent_buffer(eb);
3764out:
3765 spin_unlock(&tree->buffer_lock);
3766 return ret;
3767}
3768EXPORT_SYMBOL(try_release_extent_buffer);