blob: bbe3bcfcf4ae5b141e893f30e2993c40ff9f8e24 [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 Mason6af118c2008-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 Mason6af118c2008-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 Mason6af118c2008-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 Mason771ed682008-11-06 22:02:51 -05001208 if (pages[i]->mapping != inode->i_mapping) {
1209 ret = -EAGAIN;
1210 unlock_page(pages[i]);
1211 page_cache_release(pages[i]);
1212 goto done;
1213 }
1214 }
Chris Masonc8b97812008-10-29 14:49:59 -04001215 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001216 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001217 }
Chris Masonc8b97812008-10-29 14:49:59 -04001218 nrpages -= ret;
1219 index += ret;
1220 cond_resched();
1221 }
1222 ret = 0;
1223done:
1224 if (ret && pages_locked) {
1225 __unlock_for_delalloc(inode, locked_page,
1226 delalloc_start,
1227 ((u64)(start_index + pages_locked - 1)) <<
1228 PAGE_CACHE_SHIFT);
1229 }
1230 return ret;
1231}
1232
1233/*
1234 * find a contiguous range of bytes in the file marked as delalloc, not
1235 * more than 'max_bytes'. start and end are used to return the range,
1236 *
1237 * 1 is returned if we find something, 0 if nothing was in the tree
1238 */
1239static noinline u64 find_lock_delalloc_range(struct inode *inode,
1240 struct extent_io_tree *tree,
1241 struct page *locked_page,
1242 u64 *start, u64 *end,
1243 u64 max_bytes)
1244{
1245 u64 delalloc_start;
1246 u64 delalloc_end;
1247 u64 found;
1248 int ret;
1249 int loops = 0;
1250
1251again:
1252 /* step one, find a bunch of delalloc bytes starting at start */
1253 delalloc_start = *start;
1254 delalloc_end = 0;
1255 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1256 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001257 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001258 *start = delalloc_start;
1259 *end = delalloc_end;
1260 return found;
1261 }
1262
1263 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001264 * start comes from the offset of locked_page. We have to lock
1265 * pages in order, so we can't process delalloc bytes before
1266 * locked_page
1267 */
1268 if (delalloc_start < *start) {
1269 delalloc_start = *start;
1270 }
1271
1272 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001273 * make sure to limit the number of pages we try to lock down
1274 * if we're looping.
1275 */
1276 if (delalloc_end + 1 - delalloc_start > max_bytes && loops) {
Chris Mason771ed682008-11-06 22:02:51 -05001277 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masonc8b97812008-10-29 14:49:59 -04001278 }
1279 /* step two, lock all the pages after the page that has start */
1280 ret = lock_delalloc_pages(inode, locked_page,
1281 delalloc_start, delalloc_end);
1282 if (ret == -EAGAIN) {
1283 /* some of the pages are gone, lets avoid looping by
1284 * shortening the size of the delalloc range we're searching
1285 */
1286 if (!loops) {
1287 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1288 max_bytes = PAGE_CACHE_SIZE - offset;
1289 loops = 1;
1290 goto again;
1291 } else {
1292 found = 0;
1293 goto out_failed;
1294 }
1295 }
1296 BUG_ON(ret);
1297
1298 /* step three, lock the state bits for the whole range */
1299 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1300
1301 /* then test to make sure it is all still delalloc */
1302 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1303 EXTENT_DELALLOC, 1);
1304 if (!ret) {
1305 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1306 __unlock_for_delalloc(inode, locked_page,
1307 delalloc_start, delalloc_end);
1308 cond_resched();
1309 goto again;
1310 }
1311 *start = delalloc_start;
1312 *end = delalloc_end;
1313out_failed:
1314 return found;
1315}
1316
1317int extent_clear_unlock_delalloc(struct inode *inode,
1318 struct extent_io_tree *tree,
1319 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001320 int unlock_pages,
1321 int clear_unlock,
1322 int clear_delalloc, int clear_dirty,
1323 int set_writeback,
Chris Masonc8b97812008-10-29 14:49:59 -04001324 int end_writeback)
1325{
1326 int ret;
1327 struct page *pages[16];
1328 unsigned long index = start >> PAGE_CACHE_SHIFT;
1329 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1330 unsigned long nr_pages = end_index - index + 1;
1331 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001332 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001333
Chris Mason771ed682008-11-06 22:02:51 -05001334 if (clear_unlock)
1335 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001336 if (clear_dirty)
1337 clear_bits |= EXTENT_DIRTY;
1338
Chris Mason771ed682008-11-06 22:02:51 -05001339 if (clear_delalloc)
1340 clear_bits |= EXTENT_DELALLOC;
1341
Chris Masonc8b97812008-10-29 14:49:59 -04001342 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05001343 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1344 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001345
1346 while(nr_pages > 0) {
1347 ret = find_get_pages_contig(inode->i_mapping, index,
1348 min(nr_pages, ARRAY_SIZE(pages)), pages);
1349 for (i = 0; i < ret; i++) {
1350 if (pages[i] == locked_page) {
1351 page_cache_release(pages[i]);
1352 continue;
1353 }
1354 if (clear_dirty)
1355 clear_page_dirty_for_io(pages[i]);
1356 if (set_writeback)
1357 set_page_writeback(pages[i]);
1358 if (end_writeback)
1359 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001360 if (unlock_pages)
1361 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001362 page_cache_release(pages[i]);
1363 }
1364 nr_pages -= ret;
1365 index += ret;
1366 cond_resched();
1367 }
1368 return 0;
1369}
1370EXPORT_SYMBOL(extent_clear_unlock_delalloc);
1371
Chris Masond352ac62008-09-29 15:18:18 -04001372/*
1373 * count the number of bytes in the tree that have a given bit(s)
1374 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1375 * cached. The total number found is returned.
1376 */
Chris Masond1310b22008-01-24 16:13:08 -05001377u64 count_range_bits(struct extent_io_tree *tree,
1378 u64 *start, u64 search_end, u64 max_bytes,
1379 unsigned long bits)
1380{
1381 struct rb_node *node;
1382 struct extent_state *state;
1383 u64 cur_start = *start;
1384 u64 total_bytes = 0;
1385 int found = 0;
1386
1387 if (search_end <= cur_start) {
1388 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1389 WARN_ON(1);
1390 return 0;
1391 }
1392
Chris Mason70dec802008-01-29 09:59:12 -05001393 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001394 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1395 total_bytes = tree->dirty_bytes;
1396 goto out;
1397 }
1398 /*
1399 * this search will find all the extents that end after
1400 * our range starts.
1401 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001402 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001403 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001404 goto out;
1405 }
1406
1407 while(1) {
1408 state = rb_entry(node, struct extent_state, rb_node);
1409 if (state->start > search_end)
1410 break;
1411 if (state->end >= cur_start && (state->state & bits)) {
1412 total_bytes += min(search_end, state->end) + 1 -
1413 max(cur_start, state->start);
1414 if (total_bytes >= max_bytes)
1415 break;
1416 if (!found) {
1417 *start = state->start;
1418 found = 1;
1419 }
1420 }
1421 node = rb_next(node);
1422 if (!node)
1423 break;
1424 }
1425out:
Chris Mason70dec802008-01-29 09:59:12 -05001426 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001427 return total_bytes;
1428}
1429/*
1430 * helper function to lock both pages and extents in the tree.
1431 * pages must be locked first.
1432 */
1433int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1434{
1435 unsigned long index = start >> PAGE_CACHE_SHIFT;
1436 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1437 struct page *page;
1438 int err;
1439
1440 while (index <= end_index) {
1441 page = grab_cache_page(tree->mapping, index);
1442 if (!page) {
1443 err = -ENOMEM;
1444 goto failed;
1445 }
1446 if (IS_ERR(page)) {
1447 err = PTR_ERR(page);
1448 goto failed;
1449 }
1450 index++;
1451 }
1452 lock_extent(tree, start, end, GFP_NOFS);
1453 return 0;
1454
1455failed:
1456 /*
1457 * we failed above in getting the page at 'index', so we undo here
1458 * up to but not including the page at 'index'
1459 */
1460 end_index = index;
1461 index = start >> PAGE_CACHE_SHIFT;
1462 while (index < end_index) {
1463 page = find_get_page(tree->mapping, index);
1464 unlock_page(page);
1465 page_cache_release(page);
1466 index++;
1467 }
1468 return err;
1469}
1470EXPORT_SYMBOL(lock_range);
1471
1472/*
1473 * helper function to unlock both pages and extents in the tree.
1474 */
1475int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1476{
1477 unsigned long index = start >> PAGE_CACHE_SHIFT;
1478 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1479 struct page *page;
1480
1481 while (index <= end_index) {
1482 page = find_get_page(tree->mapping, index);
1483 unlock_page(page);
1484 page_cache_release(page);
1485 index++;
1486 }
1487 unlock_extent(tree, start, end, GFP_NOFS);
1488 return 0;
1489}
1490EXPORT_SYMBOL(unlock_range);
1491
Chris Masond352ac62008-09-29 15:18:18 -04001492/*
1493 * set the private field for a given byte offset in the tree. If there isn't
1494 * an extent_state there already, this does nothing.
1495 */
Chris Masond1310b22008-01-24 16:13:08 -05001496int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1497{
1498 struct rb_node *node;
1499 struct extent_state *state;
1500 int ret = 0;
1501
Chris Mason70dec802008-01-29 09:59:12 -05001502 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001503 /*
1504 * this search will find all the extents that end after
1505 * our range starts.
1506 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001507 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001508 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001509 ret = -ENOENT;
1510 goto out;
1511 }
1512 state = rb_entry(node, struct extent_state, rb_node);
1513 if (state->start != start) {
1514 ret = -ENOENT;
1515 goto out;
1516 }
1517 state->private = private;
1518out:
Chris Mason70dec802008-01-29 09:59:12 -05001519 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001520 return ret;
1521}
1522
1523int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1524{
1525 struct rb_node *node;
1526 struct extent_state *state;
1527 int ret = 0;
1528
Chris Mason70dec802008-01-29 09:59:12 -05001529 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001530 /*
1531 * this search will find all the extents that end after
1532 * our range starts.
1533 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001534 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001535 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001536 ret = -ENOENT;
1537 goto out;
1538 }
1539 state = rb_entry(node, struct extent_state, rb_node);
1540 if (state->start != start) {
1541 ret = -ENOENT;
1542 goto out;
1543 }
1544 *private = state->private;
1545out:
Chris Mason70dec802008-01-29 09:59:12 -05001546 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001547 return ret;
1548}
1549
1550/*
1551 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001552 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001553 * has the bits set. Otherwise, 1 is returned if any bit in the
1554 * range is found set.
1555 */
1556int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1557 int bits, int filled)
1558{
1559 struct extent_state *state = NULL;
1560 struct rb_node *node;
1561 int bitset = 0;
1562 unsigned long flags;
1563
Chris Mason70dec802008-01-29 09:59:12 -05001564 spin_lock_irqsave(&tree->lock, flags);
Chris Mason80ea96b2008-02-01 14:51:59 -05001565 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001566 while (node && start <= end) {
1567 state = rb_entry(node, struct extent_state, rb_node);
1568
1569 if (filled && state->start > start) {
1570 bitset = 0;
1571 break;
1572 }
1573
1574 if (state->start > end)
1575 break;
1576
1577 if (state->state & bits) {
1578 bitset = 1;
1579 if (!filled)
1580 break;
1581 } else if (filled) {
1582 bitset = 0;
1583 break;
1584 }
1585 start = state->end + 1;
1586 if (start > end)
1587 break;
1588 node = rb_next(node);
1589 if (!node) {
1590 if (filled)
1591 bitset = 0;
1592 break;
1593 }
1594 }
Chris Mason70dec802008-01-29 09:59:12 -05001595 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05001596 return bitset;
1597}
1598EXPORT_SYMBOL(test_range_bit);
1599
1600/*
1601 * helper function to set a given page up to date if all the
1602 * extents in the tree for that page are up to date
1603 */
1604static int check_page_uptodate(struct extent_io_tree *tree,
1605 struct page *page)
1606{
1607 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1608 u64 end = start + PAGE_CACHE_SIZE - 1;
1609 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1610 SetPageUptodate(page);
1611 return 0;
1612}
1613
1614/*
1615 * helper function to unlock a page if all the extents in the tree
1616 * for that page are unlocked
1617 */
1618static int check_page_locked(struct extent_io_tree *tree,
1619 struct page *page)
1620{
1621 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1622 u64 end = start + PAGE_CACHE_SIZE - 1;
1623 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1624 unlock_page(page);
1625 return 0;
1626}
1627
1628/*
1629 * helper function to end page writeback if all the extents
1630 * in the tree for that page are done with writeback
1631 */
1632static int check_page_writeback(struct extent_io_tree *tree,
1633 struct page *page)
1634{
1635 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1636 u64 end = start + PAGE_CACHE_SIZE - 1;
1637 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1638 end_page_writeback(page);
1639 return 0;
1640}
1641
1642/* lots and lots of room for performance fixes in the end_bio funcs */
1643
1644/*
1645 * after a writepage IO is done, we need to:
1646 * clear the uptodate bits on error
1647 * clear the writeback bits in the extent tree for this IO
1648 * end_page_writeback if the page has no more pending IO
1649 *
1650 * Scheduling is not allowed, so the extent state tree is expected
1651 * to have one and only one object corresponding to this IO.
1652 */
Chris Masond1310b22008-01-24 16:13:08 -05001653static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001654{
Chris Mason1259ab72008-05-12 13:39:03 -04001655 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001656 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001657 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001658 u64 start;
1659 u64 end;
1660 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001661 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001662
Chris Masond1310b22008-01-24 16:13:08 -05001663 do {
1664 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001665 tree = &BTRFS_I(page->mapping->host)->io_tree;
1666
Chris Masond1310b22008-01-24 16:13:08 -05001667 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1668 bvec->bv_offset;
1669 end = start + bvec->bv_len - 1;
1670
1671 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1672 whole_page = 1;
1673 else
1674 whole_page = 0;
1675
1676 if (--bvec >= bio->bi_io_vec)
1677 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001678 if (tree->ops && tree->ops->writepage_end_io_hook) {
1679 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001680 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001681 if (ret)
1682 uptodate = 0;
1683 }
1684
1685 if (!uptodate && tree->ops &&
1686 tree->ops->writepage_io_failed_hook) {
1687 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001688 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001689 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001690 uptodate = (err == 0);
1691 continue;
1692 }
1693 }
1694
Chris Masond1310b22008-01-24 16:13:08 -05001695 if (!uptodate) {
1696 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1697 ClearPageUptodate(page);
1698 SetPageError(page);
1699 }
Chris Mason70dec802008-01-29 09:59:12 -05001700
David Woodhouse902b22f2008-08-20 08:51:49 -04001701 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001702
1703 if (whole_page)
1704 end_page_writeback(page);
1705 else
1706 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001707 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001708
Chris Masond1310b22008-01-24 16:13:08 -05001709 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001710}
1711
1712/*
1713 * after a readpage IO is done, we need to:
1714 * clear the uptodate bits on error
1715 * set the uptodate bits if things worked
1716 * set the page up to date if all extents in the tree are uptodate
1717 * clear the lock bit in the extent tree
1718 * unlock the page if there are no other extents locked for it
1719 *
1720 * Scheduling is not allowed, so the extent state tree is expected
1721 * to have one and only one object corresponding to this IO.
1722 */
Chris Masond1310b22008-01-24 16:13:08 -05001723static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001724{
1725 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1726 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001727 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001728 u64 start;
1729 u64 end;
1730 int whole_page;
1731 int ret;
1732
Chris Masond1310b22008-01-24 16:13:08 -05001733 do {
1734 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001735 tree = &BTRFS_I(page->mapping->host)->io_tree;
1736
Chris Masond1310b22008-01-24 16:13:08 -05001737 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1738 bvec->bv_offset;
1739 end = start + bvec->bv_len - 1;
1740
1741 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1742 whole_page = 1;
1743 else
1744 whole_page = 0;
1745
1746 if (--bvec >= bio->bi_io_vec)
1747 prefetchw(&bvec->bv_page->flags);
1748
1749 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001750 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001751 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001752 if (ret)
1753 uptodate = 0;
1754 }
Chris Mason7e383262008-04-09 16:28:12 -04001755 if (!uptodate && tree->ops &&
1756 tree->ops->readpage_io_failed_hook) {
1757 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001758 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001759 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001760 uptodate =
1761 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason7e383262008-04-09 16:28:12 -04001762 continue;
1763 }
1764 }
Chris Mason70dec802008-01-29 09:59:12 -05001765
Chris Mason771ed682008-11-06 22:02:51 -05001766 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001767 set_extent_uptodate(tree, start, end,
1768 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001769 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001770 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001771
Chris Mason70dec802008-01-29 09:59:12 -05001772 if (whole_page) {
1773 if (uptodate) {
1774 SetPageUptodate(page);
1775 } else {
1776 ClearPageUptodate(page);
1777 SetPageError(page);
1778 }
Chris Masond1310b22008-01-24 16:13:08 -05001779 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001780 } else {
1781 if (uptodate) {
1782 check_page_uptodate(tree, page);
1783 } else {
1784 ClearPageUptodate(page);
1785 SetPageError(page);
1786 }
Chris Masond1310b22008-01-24 16:13:08 -05001787 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001788 }
Chris Masond1310b22008-01-24 16:13:08 -05001789 } while (bvec >= bio->bi_io_vec);
1790
1791 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001792}
1793
1794/*
1795 * IO done from prepare_write is pretty simple, we just unlock
1796 * the structs in the extent tree when done, and set the uptodate bits
1797 * as appropriate.
1798 */
Chris Masond1310b22008-01-24 16:13:08 -05001799static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001800{
1801 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1802 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001803 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001804 u64 start;
1805 u64 end;
1806
Chris Masond1310b22008-01-24 16:13:08 -05001807 do {
1808 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001809 tree = &BTRFS_I(page->mapping->host)->io_tree;
1810
Chris Masond1310b22008-01-24 16:13:08 -05001811 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1812 bvec->bv_offset;
1813 end = start + bvec->bv_len - 1;
1814
1815 if (--bvec >= bio->bi_io_vec)
1816 prefetchw(&bvec->bv_page->flags);
1817
1818 if (uptodate) {
1819 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1820 } else {
1821 ClearPageUptodate(page);
1822 SetPageError(page);
1823 }
1824
1825 unlock_extent(tree, start, end, GFP_ATOMIC);
1826
1827 } while (bvec >= bio->bi_io_vec);
1828
1829 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001830}
1831
1832static struct bio *
1833extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1834 gfp_t gfp_flags)
1835{
1836 struct bio *bio;
1837
1838 bio = bio_alloc(gfp_flags, nr_vecs);
1839
1840 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1841 while (!bio && (nr_vecs /= 2))
1842 bio = bio_alloc(gfp_flags, nr_vecs);
1843 }
1844
1845 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001846 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001847 bio->bi_bdev = bdev;
1848 bio->bi_sector = first_sector;
1849 }
1850 return bio;
1851}
1852
Chris Masonc8b97812008-10-29 14:49:59 -04001853static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1854 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001855{
Chris Masond1310b22008-01-24 16:13:08 -05001856 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001857 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1858 struct page *page = bvec->bv_page;
1859 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001860 u64 start;
1861 u64 end;
1862
1863 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1864 end = start + bvec->bv_len - 1;
1865
David Woodhouse902b22f2008-08-20 08:51:49 -04001866 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001867
1868 bio_get(bio);
1869
Chris Mason065631f2008-02-20 12:07:25 -05001870 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001871 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001872 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001873 else
1874 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001875 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1876 ret = -EOPNOTSUPP;
1877 bio_put(bio);
1878 return ret;
1879}
1880
1881static int submit_extent_page(int rw, struct extent_io_tree *tree,
1882 struct page *page, sector_t sector,
1883 size_t size, unsigned long offset,
1884 struct block_device *bdev,
1885 struct bio **bio_ret,
1886 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001887 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001888 int mirror_num,
1889 unsigned long prev_bio_flags,
1890 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001891{
1892 int ret = 0;
1893 struct bio *bio;
1894 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001895 int contig = 0;
1896 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1897 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1898 size_t page_size = min(size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001899
1900 if (bio_ret && *bio_ret) {
1901 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001902 if (old_compressed)
1903 contig = bio->bi_sector == sector;
1904 else
1905 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1906 sector;
1907
1908 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001909 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001910 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1911 bio_flags)) ||
1912 bio_add_page(bio, page, page_size, offset) < page_size) {
1913 ret = submit_one_bio(rw, bio, mirror_num,
1914 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001915 bio = NULL;
1916 } else {
1917 return 0;
1918 }
1919 }
Chris Masonc8b97812008-10-29 14:49:59 -04001920 if (this_compressed)
1921 nr = BIO_MAX_PAGES;
1922 else
1923 nr = bio_get_nr_vecs(bdev);
1924
Chris Masond1310b22008-01-24 16:13:08 -05001925 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1926 if (!bio) {
1927 printk("failed to allocate bio nr %d\n", nr);
1928 }
Chris Mason70dec802008-01-29 09:59:12 -05001929
Chris Masonc8b97812008-10-29 14:49:59 -04001930 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001931 bio->bi_end_io = end_io_func;
1932 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001933
Chris Masond1310b22008-01-24 16:13:08 -05001934 if (bio_ret) {
1935 *bio_ret = bio;
1936 } else {
Chris Masonc8b97812008-10-29 14:49:59 -04001937 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001938 }
1939
1940 return ret;
1941}
1942
1943void set_page_extent_mapped(struct page *page)
1944{
1945 if (!PagePrivate(page)) {
1946 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001947 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04001948 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001949 }
1950}
Chris Mason771ed682008-11-06 22:02:51 -05001951EXPORT_SYMBOL(set_page_extent_mapped);
Chris Masond1310b22008-01-24 16:13:08 -05001952
1953void set_page_extent_head(struct page *page, unsigned long len)
1954{
1955 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1956}
1957
1958/*
1959 * basic readpage implementation. Locked extent state structs are inserted
1960 * into the tree that are removed when the IO is done (by the end_io
1961 * handlers)
1962 */
1963static int __extent_read_full_page(struct extent_io_tree *tree,
1964 struct page *page,
1965 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001966 struct bio **bio, int mirror_num,
1967 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001968{
1969 struct inode *inode = page->mapping->host;
1970 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1971 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1972 u64 end;
1973 u64 cur = start;
1974 u64 extent_offset;
1975 u64 last_byte = i_size_read(inode);
1976 u64 block_start;
1977 u64 cur_end;
1978 sector_t sector;
1979 struct extent_map *em;
1980 struct block_device *bdev;
1981 int ret;
1982 int nr = 0;
1983 size_t page_offset = 0;
1984 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001985 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001986 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001987 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001988
1989 set_page_extent_mapped(page);
1990
1991 end = page_end;
1992 lock_extent(tree, start, end, GFP_NOFS);
1993
Chris Masonc8b97812008-10-29 14:49:59 -04001994 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1995 char *userpage;
1996 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1997
1998 if (zero_offset) {
1999 iosize = PAGE_CACHE_SIZE - zero_offset;
2000 userpage = kmap_atomic(page, KM_USER0);
2001 memset(userpage + zero_offset, 0, iosize);
2002 flush_dcache_page(page);
2003 kunmap_atomic(userpage, KM_USER0);
2004 }
2005 }
Chris Masond1310b22008-01-24 16:13:08 -05002006 while (cur <= end) {
2007 if (cur >= last_byte) {
2008 char *userpage;
2009 iosize = PAGE_CACHE_SIZE - page_offset;
2010 userpage = kmap_atomic(page, KM_USER0);
2011 memset(userpage + page_offset, 0, iosize);
2012 flush_dcache_page(page);
2013 kunmap_atomic(userpage, KM_USER0);
2014 set_extent_uptodate(tree, cur, cur + iosize - 1,
2015 GFP_NOFS);
2016 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2017 break;
2018 }
2019 em = get_extent(inode, page, page_offset, cur,
2020 end - cur + 1, 0);
2021 if (IS_ERR(em) || !em) {
2022 SetPageError(page);
2023 unlock_extent(tree, cur, end, GFP_NOFS);
2024 break;
2025 }
Chris Masond1310b22008-01-24 16:13:08 -05002026 extent_offset = cur - em->start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002027 if (extent_map_end(em) <= cur) {
2028printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur);
2029 }
Chris Masond1310b22008-01-24 16:13:08 -05002030 BUG_ON(extent_map_end(em) <= cur);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002031 if (end < cur) {
2032printk("2bad mapping end %Lu cur %Lu\n", end, cur);
2033 }
Chris Masond1310b22008-01-24 16:13:08 -05002034 BUG_ON(end < cur);
2035
Chris Masonc8b97812008-10-29 14:49:59 -04002036 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2037 this_bio_flag = EXTENT_BIO_COMPRESSED;
2038
Chris Masond1310b22008-01-24 16:13:08 -05002039 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2040 cur_end = min(extent_map_end(em) - 1, end);
2041 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002042 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2043 disk_io_size = em->block_len;
2044 sector = em->block_start >> 9;
2045 } else {
2046 sector = (em->block_start + extent_offset) >> 9;
2047 disk_io_size = iosize;
2048 }
Chris Masond1310b22008-01-24 16:13:08 -05002049 bdev = em->bdev;
2050 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002051 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2052 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002053 free_extent_map(em);
2054 em = NULL;
2055
2056 /* we've found a hole, just zero and go on */
2057 if (block_start == EXTENT_MAP_HOLE) {
2058 char *userpage;
2059 userpage = kmap_atomic(page, KM_USER0);
2060 memset(userpage + page_offset, 0, iosize);
2061 flush_dcache_page(page);
2062 kunmap_atomic(userpage, KM_USER0);
2063
2064 set_extent_uptodate(tree, cur, cur + iosize - 1,
2065 GFP_NOFS);
2066 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2067 cur = cur + iosize;
2068 page_offset += iosize;
2069 continue;
2070 }
2071 /* the get_extent function already copied into the page */
2072 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002073 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002074 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2075 cur = cur + iosize;
2076 page_offset += iosize;
2077 continue;
2078 }
Chris Mason70dec802008-01-29 09:59:12 -05002079 /* we have an inline extent but it didn't get marked up
2080 * to date. Error out
2081 */
2082 if (block_start == EXTENT_MAP_INLINE) {
2083 SetPageError(page);
2084 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2085 cur = cur + iosize;
2086 page_offset += iosize;
2087 continue;
2088 }
Chris Masond1310b22008-01-24 16:13:08 -05002089
2090 ret = 0;
2091 if (tree->ops && tree->ops->readpage_io_hook) {
2092 ret = tree->ops->readpage_io_hook(page, cur,
2093 cur + iosize - 1);
2094 }
2095 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002096 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2097 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002098 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002099 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002100 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002101 end_bio_extent_readpage, mirror_num,
2102 *bio_flags,
2103 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002104 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002105 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002106 }
2107 if (ret)
2108 SetPageError(page);
2109 cur = cur + iosize;
2110 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002111 }
2112 if (!nr) {
2113 if (!PageError(page))
2114 SetPageUptodate(page);
2115 unlock_page(page);
2116 }
2117 return 0;
2118}
2119
2120int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2121 get_extent_t *get_extent)
2122{
2123 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002124 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002125 int ret;
2126
Chris Masonc8b97812008-10-29 14:49:59 -04002127 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2128 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002129 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002130 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002131 return ret;
2132}
2133EXPORT_SYMBOL(extent_read_full_page);
2134
2135/*
2136 * the writepage semantics are similar to regular writepage. extent
2137 * records are inserted to lock ranges in the tree, and as dirty areas
2138 * are found, they are marked writeback. Then the lock bits are removed
2139 * and the end_io handler clears the writeback ranges
2140 */
2141static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2142 void *data)
2143{
2144 struct inode *inode = page->mapping->host;
2145 struct extent_page_data *epd = data;
2146 struct extent_io_tree *tree = epd->tree;
2147 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2148 u64 delalloc_start;
2149 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2150 u64 end;
2151 u64 cur = start;
2152 u64 extent_offset;
2153 u64 last_byte = i_size_read(inode);
2154 u64 block_start;
2155 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002156 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002157 sector_t sector;
2158 struct extent_map *em;
2159 struct block_device *bdev;
2160 int ret;
2161 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002162 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002163 size_t blocksize;
2164 loff_t i_size = i_size_read(inode);
2165 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2166 u64 nr_delalloc;
2167 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002168 int page_started;
2169 int compressed;
Chris Mason771ed682008-11-06 22:02:51 -05002170 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002171
2172 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002173 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002174 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002175 (page->index == end_index && !pg_offset)) {
Chris Mason771ed682008-11-06 22:02:51 -05002176 if (epd->extent_locked) {
2177 if (tree->ops && tree->ops->writepage_end_io_hook)
2178 tree->ops->writepage_end_io_hook(page, start,
2179 page_end, NULL, 1);
2180 }
Chris Masond1310b22008-01-24 16:13:08 -05002181 unlock_page(page);
2182 return 0;
2183 }
2184
2185 if (page->index == end_index) {
2186 char *userpage;
2187
Chris Masond1310b22008-01-24 16:13:08 -05002188 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002189 memset(userpage + pg_offset, 0,
2190 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002191 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002192 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002193 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002194 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002195
2196 set_page_extent_mapped(page);
2197
2198 delalloc_start = start;
2199 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002200 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002201 if (!epd->extent_locked) {
2202 while(delalloc_end < page_end) {
2203 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002204 page,
2205 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002206 &delalloc_end,
2207 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002208 if (nr_delalloc == 0) {
2209 delalloc_start = delalloc_end + 1;
2210 continue;
2211 }
2212 tree->ops->fill_delalloc(inode, page, delalloc_start,
2213 delalloc_end, &page_started,
2214 &nr_written);
Chris Masond1310b22008-01-24 16:13:08 -05002215 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002216 }
Chris Masonc8b97812008-10-29 14:49:59 -04002217
Chris Mason771ed682008-11-06 22:02:51 -05002218 /* did the fill delalloc function already unlock and start
2219 * the IO?
2220 */
2221 if (page_started) {
2222 ret = 0;
2223 goto update_nr_written;
2224 }
Chris Masonc8b97812008-10-29 14:49:59 -04002225 }
Chris Masond1310b22008-01-24 16:13:08 -05002226 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002227
Chris Masone6dcd2d2008-07-17 12:53:50 -04002228 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002229
Chris Mason247e7432008-07-17 12:53:51 -04002230 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002231 ret = tree->ops->writepage_start_hook(page, start,
2232 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002233 if (ret == -EAGAIN) {
2234 unlock_extent(tree, start, page_end, GFP_NOFS);
2235 redirty_page_for_writepage(wbc, page);
2236 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002237 ret = 0;
2238 goto update_nr_written;
Chris Mason247e7432008-07-17 12:53:51 -04002239 }
2240 }
2241
Chris Mason771ed682008-11-06 22:02:51 -05002242 nr_written++;
2243
Chris Masond1310b22008-01-24 16:13:08 -05002244 end = page_end;
2245 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2246 printk("found delalloc bits after lock_extent\n");
2247 }
2248
2249 if (last_byte <= start) {
2250 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251 unlock_extent(tree, start, page_end, GFP_NOFS);
2252 if (tree->ops && tree->ops->writepage_end_io_hook)
2253 tree->ops->writepage_end_io_hook(page, start,
2254 page_end, NULL, 1);
2255 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002256 goto done;
2257 }
2258
2259 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2260 blocksize = inode->i_sb->s_blocksize;
2261
2262 while (cur <= end) {
2263 if (cur >= last_byte) {
2264 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002265 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2266 if (tree->ops && tree->ops->writepage_end_io_hook)
2267 tree->ops->writepage_end_io_hook(page, cur,
2268 page_end, NULL, 1);
2269 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002270 break;
2271 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002272 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002273 end - cur + 1, 1);
2274 if (IS_ERR(em) || !em) {
2275 SetPageError(page);
2276 break;
2277 }
2278
2279 extent_offset = cur - em->start;
2280 BUG_ON(extent_map_end(em) <= cur);
2281 BUG_ON(end < cur);
2282 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2283 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2284 sector = (em->block_start + extent_offset) >> 9;
2285 bdev = em->bdev;
2286 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002287 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002288 free_extent_map(em);
2289 em = NULL;
2290
Chris Masonc8b97812008-10-29 14:49:59 -04002291 /*
2292 * compressed and inline extents are written through other
2293 * paths in the FS
2294 */
2295 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002296 block_start == EXTENT_MAP_INLINE) {
2297 clear_extent_dirty(tree, cur,
2298 cur + iosize - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002299
2300 unlock_extent(tree, unlock_start, cur + iosize -1,
2301 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002302
Chris Masonc8b97812008-10-29 14:49:59 -04002303 /*
2304 * end_io notification does not happen here for
2305 * compressed extents
2306 */
2307 if (!compressed && tree->ops &&
2308 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002309 tree->ops->writepage_end_io_hook(page, cur,
2310 cur + iosize - 1,
2311 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002312 else if (compressed) {
2313 /* we don't want to end_page_writeback on
2314 * a compressed extent. this happens
2315 * elsewhere
2316 */
2317 nr++;
2318 }
2319
2320 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002321 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002322 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002323 continue;
2324 }
Chris Masond1310b22008-01-24 16:13:08 -05002325 /* leave this out until we have a page_mkwrite call */
2326 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2327 EXTENT_DIRTY, 0)) {
2328 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002329 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002330 continue;
2331 }
Chris Masonc8b97812008-10-29 14:49:59 -04002332
Chris Masond1310b22008-01-24 16:13:08 -05002333 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2334 if (tree->ops && tree->ops->writepage_io_hook) {
2335 ret = tree->ops->writepage_io_hook(page, cur,
2336 cur + iosize - 1);
2337 } else {
2338 ret = 0;
2339 }
Chris Mason1259ab72008-05-12 13:39:03 -04002340 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002341 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002342 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002343 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002344
Chris Masond1310b22008-01-24 16:13:08 -05002345 set_range_writeback(tree, cur, cur + iosize - 1);
2346 if (!PageWriteback(page)) {
2347 printk("warning page %lu not writeback, "
2348 "cur %llu end %llu\n", page->index,
2349 (unsigned long long)cur,
2350 (unsigned long long)end);
2351 }
2352
2353 ret = submit_extent_page(WRITE, tree, page, sector,
Chris Mason7f3c74f2008-07-18 12:01:11 -04002354 iosize, pg_offset, bdev,
Chris Masond1310b22008-01-24 16:13:08 -05002355 &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002356 end_bio_extent_writepage,
2357 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002358 if (ret)
2359 SetPageError(page);
2360 }
2361 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002362 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002363 nr++;
2364 }
2365done:
2366 if (nr == 0) {
2367 /* make sure the mapping tag for page dirty gets cleared */
2368 set_page_writeback(page);
2369 end_page_writeback(page);
2370 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002371 if (unlock_start <= page_end)
2372 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002373 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002374
2375update_nr_written:
2376 wbc->nr_to_write -= nr_written;
2377 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2378 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2379 page->mapping->writeback_index = page->index + nr_written;
Chris Masond1310b22008-01-24 16:13:08 -05002380 return 0;
2381}
2382
Chris Masond1310b22008-01-24 16:13:08 -05002383/**
Chris Mason4bef0842008-09-08 11:18:08 -04002384 * 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 -05002385 * @mapping: address space structure to write
2386 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2387 * @writepage: function called for each page
2388 * @data: data passed to writepage function
2389 *
2390 * If a page is already under I/O, write_cache_pages() skips it, even
2391 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2392 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2393 * and msync() need to guarantee that all the data which was dirty at the time
2394 * the call was made get new I/O started against them. If wbc->sync_mode is
2395 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2396 * existing IO to complete.
2397 */
Chris Mason4bef0842008-09-08 11:18:08 -04002398int extent_write_cache_pages(struct extent_io_tree *tree,
2399 struct address_space *mapping,
2400 struct writeback_control *wbc,
2401 writepage_t writepage, void *data)
Chris Masond1310b22008-01-24 16:13:08 -05002402{
2403 struct backing_dev_info *bdi = mapping->backing_dev_info;
2404 int ret = 0;
2405 int done = 0;
2406 struct pagevec pvec;
2407 int nr_pages;
2408 pgoff_t index;
2409 pgoff_t end; /* Inclusive */
2410 int scanned = 0;
2411 int range_whole = 0;
2412
2413 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2414 wbc->encountered_congestion = 1;
2415 return 0;
2416 }
2417
2418 pagevec_init(&pvec, 0);
2419 if (wbc->range_cyclic) {
2420 index = mapping->writeback_index; /* Start from prev offset */
2421 end = -1;
2422 } else {
2423 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2424 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2425 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2426 range_whole = 1;
2427 scanned = 1;
2428 }
2429retry:
2430 while (!done && (index <= end) &&
2431 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2432 PAGECACHE_TAG_DIRTY,
2433 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2434 unsigned i;
2435
2436 scanned = 1;
2437 for (i = 0; i < nr_pages; i++) {
2438 struct page *page = pvec.pages[i];
2439
2440 /*
2441 * At this point we hold neither mapping->tree_lock nor
2442 * lock on the page itself: the page may be truncated or
2443 * invalidated (changing page->mapping to NULL), or even
2444 * swizzled back from swapper_space to tmpfs file
2445 * mapping
2446 */
Chris Mason4bef0842008-09-08 11:18:08 -04002447 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2448 tree->ops->write_cache_pages_lock_hook(page);
2449 else
2450 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002451
2452 if (unlikely(page->mapping != mapping)) {
2453 unlock_page(page);
2454 continue;
2455 }
2456
2457 if (!wbc->range_cyclic && page->index > end) {
2458 done = 1;
2459 unlock_page(page);
2460 continue;
2461 }
2462
2463 if (wbc->sync_mode != WB_SYNC_NONE)
2464 wait_on_page_writeback(page);
2465
2466 if (PageWriteback(page) ||
2467 !clear_page_dirty_for_io(page)) {
2468 unlock_page(page);
2469 continue;
2470 }
2471
2472 ret = (*writepage)(page, wbc, data);
2473
2474 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2475 unlock_page(page);
2476 ret = 0;
2477 }
Chris Mason771ed682008-11-06 22:02:51 -05002478 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002479 done = 1;
2480 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2481 wbc->encountered_congestion = 1;
2482 done = 1;
2483 }
2484 }
2485 pagevec_release(&pvec);
2486 cond_resched();
2487 }
2488 if (!scanned && !done) {
2489 /*
2490 * We hit the last page and there is more work to be done: wrap
2491 * back to the start of the file
2492 */
2493 scanned = 1;
2494 index = 0;
2495 goto retry;
2496 }
2497 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2498 mapping->writeback_index = index;
Chris Mason771ed682008-11-06 22:02:51 -05002499 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2500 range_whole = 1;
Chris Mason2b1f55b2008-09-24 11:48:04 -04002501
Chris Mason4bef0842008-09-08 11:18:08 -04002502 if (wbc->range_cont)
2503 wbc->range_start = index << PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002504 return ret;
2505}
Chris Mason4bef0842008-09-08 11:18:08 -04002506EXPORT_SYMBOL(extent_write_cache_pages);
Chris Masond1310b22008-01-24 16:13:08 -05002507
2508int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2509 get_extent_t *get_extent,
2510 struct writeback_control *wbc)
2511{
2512 int ret;
2513 struct address_space *mapping = page->mapping;
2514 struct extent_page_data epd = {
2515 .bio = NULL,
2516 .tree = tree,
2517 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002518 .extent_locked = 0,
Chris Masond1310b22008-01-24 16:13:08 -05002519 };
2520 struct writeback_control wbc_writepages = {
2521 .bdi = wbc->bdi,
2522 .sync_mode = WB_SYNC_NONE,
2523 .older_than_this = NULL,
2524 .nr_to_write = 64,
2525 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2526 .range_end = (loff_t)-1,
2527 };
2528
2529
2530 ret = __extent_writepage(page, wbc, &epd);
2531
Chris Mason4bef0842008-09-08 11:18:08 -04002532 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2533 __extent_writepage, &epd);
Chris Masond1310b22008-01-24 16:13:08 -05002534 if (epd.bio) {
Chris Masonc8b97812008-10-29 14:49:59 -04002535 submit_one_bio(WRITE, epd.bio, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002536 }
2537 return ret;
2538}
2539EXPORT_SYMBOL(extent_write_full_page);
2540
Chris Mason771ed682008-11-06 22:02:51 -05002541int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2542 u64 start, u64 end, get_extent_t *get_extent,
2543 int mode)
2544{
2545 int ret = 0;
2546 struct address_space *mapping = inode->i_mapping;
2547 struct page *page;
2548 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2549 PAGE_CACHE_SHIFT;
2550
2551 struct extent_page_data epd = {
2552 .bio = NULL,
2553 .tree = tree,
2554 .get_extent = get_extent,
2555 .extent_locked = 1,
2556 };
2557 struct writeback_control wbc_writepages = {
2558 .bdi = inode->i_mapping->backing_dev_info,
2559 .sync_mode = mode,
2560 .older_than_this = NULL,
2561 .nr_to_write = nr_pages * 2,
2562 .range_start = start,
2563 .range_end = end + 1,
2564 };
2565
2566 while(start <= end) {
2567 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2568 if (clear_page_dirty_for_io(page))
2569 ret = __extent_writepage(page, &wbc_writepages, &epd);
2570 else {
2571 if (tree->ops && tree->ops->writepage_end_io_hook)
2572 tree->ops->writepage_end_io_hook(page, start,
2573 start + PAGE_CACHE_SIZE - 1,
2574 NULL, 1);
2575 unlock_page(page);
2576 }
2577 page_cache_release(page);
2578 start += PAGE_CACHE_SIZE;
2579 }
2580
2581 if (epd.bio)
2582 submit_one_bio(WRITE, epd.bio, 0, 0);
2583 return ret;
2584}
2585EXPORT_SYMBOL(extent_write_locked_range);
2586
Chris Masond1310b22008-01-24 16:13:08 -05002587
2588int extent_writepages(struct extent_io_tree *tree,
2589 struct address_space *mapping,
2590 get_extent_t *get_extent,
2591 struct writeback_control *wbc)
2592{
2593 int ret = 0;
2594 struct extent_page_data epd = {
2595 .bio = NULL,
2596 .tree = tree,
2597 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002598 .extent_locked = 0,
Chris Masond1310b22008-01-24 16:13:08 -05002599 };
2600
Chris Mason4bef0842008-09-08 11:18:08 -04002601 ret = extent_write_cache_pages(tree, mapping, wbc,
2602 __extent_writepage, &epd);
Chris Masond1310b22008-01-24 16:13:08 -05002603 if (epd.bio) {
Chris Masonc8b97812008-10-29 14:49:59 -04002604 submit_one_bio(WRITE, epd.bio, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002605 }
2606 return ret;
2607}
2608EXPORT_SYMBOL(extent_writepages);
2609
2610int extent_readpages(struct extent_io_tree *tree,
2611 struct address_space *mapping,
2612 struct list_head *pages, unsigned nr_pages,
2613 get_extent_t get_extent)
2614{
2615 struct bio *bio = NULL;
2616 unsigned page_idx;
2617 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002618 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002619
2620 pagevec_init(&pvec, 0);
2621 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2622 struct page *page = list_entry(pages->prev, struct page, lru);
2623
2624 prefetchw(&page->flags);
2625 list_del(&page->lru);
2626 /*
2627 * what we want to do here is call add_to_page_cache_lru,
2628 * but that isn't exported, so we reproduce it here
2629 */
2630 if (!add_to_page_cache(page, mapping,
2631 page->index, GFP_KERNEL)) {
2632
2633 /* open coding of lru_cache_add, also not exported */
2634 page_cache_get(page);
2635 if (!pagevec_add(&pvec, page))
2636 __pagevec_lru_add(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002637 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002638 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002639 }
2640 page_cache_release(page);
2641 }
2642 if (pagevec_count(&pvec))
2643 __pagevec_lru_add(&pvec);
2644 BUG_ON(!list_empty(pages));
2645 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002646 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002647 return 0;
2648}
2649EXPORT_SYMBOL(extent_readpages);
2650
2651/*
2652 * basic invalidatepage code, this waits on any locked or writeback
2653 * ranges corresponding to the page, and then deletes any extent state
2654 * records from the tree
2655 */
2656int extent_invalidatepage(struct extent_io_tree *tree,
2657 struct page *page, unsigned long offset)
2658{
2659 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2660 u64 end = start + PAGE_CACHE_SIZE - 1;
2661 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2662
2663 start += (offset + blocksize -1) & ~(blocksize - 1);
2664 if (start > end)
2665 return 0;
2666
2667 lock_extent(tree, start, end, GFP_NOFS);
2668 wait_on_extent_writeback(tree, start, end);
2669 clear_extent_bit(tree, start, end,
2670 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2671 1, 1, GFP_NOFS);
2672 return 0;
2673}
2674EXPORT_SYMBOL(extent_invalidatepage);
2675
2676/*
2677 * simple commit_write call, set_range_dirty is used to mark both
2678 * the pages and the extent records as dirty
2679 */
2680int extent_commit_write(struct extent_io_tree *tree,
2681 struct inode *inode, struct page *page,
2682 unsigned from, unsigned to)
2683{
2684 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2685
2686 set_page_extent_mapped(page);
2687 set_page_dirty(page);
2688
2689 if (pos > inode->i_size) {
2690 i_size_write(inode, pos);
2691 mark_inode_dirty(inode);
2692 }
2693 return 0;
2694}
2695EXPORT_SYMBOL(extent_commit_write);
2696
2697int extent_prepare_write(struct extent_io_tree *tree,
2698 struct inode *inode, struct page *page,
2699 unsigned from, unsigned to, get_extent_t *get_extent)
2700{
2701 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2702 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2703 u64 block_start;
2704 u64 orig_block_start;
2705 u64 block_end;
2706 u64 cur_end;
2707 struct extent_map *em;
2708 unsigned blocksize = 1 << inode->i_blkbits;
2709 size_t page_offset = 0;
2710 size_t block_off_start;
2711 size_t block_off_end;
2712 int err = 0;
2713 int iocount = 0;
2714 int ret = 0;
2715 int isnew;
2716
2717 set_page_extent_mapped(page);
2718
2719 block_start = (page_start + from) & ~((u64)blocksize - 1);
2720 block_end = (page_start + to - 1) | (blocksize - 1);
2721 orig_block_start = block_start;
2722
2723 lock_extent(tree, page_start, page_end, GFP_NOFS);
2724 while(block_start <= block_end) {
2725 em = get_extent(inode, page, page_offset, block_start,
2726 block_end - block_start + 1, 1);
2727 if (IS_ERR(em) || !em) {
2728 goto err;
2729 }
2730 cur_end = min(block_end, extent_map_end(em) - 1);
2731 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2732 block_off_end = block_off_start + blocksize;
2733 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2734
2735 if (!PageUptodate(page) && isnew &&
2736 (block_off_end > to || block_off_start < from)) {
2737 void *kaddr;
2738
2739 kaddr = kmap_atomic(page, KM_USER0);
2740 if (block_off_end > to)
2741 memset(kaddr + to, 0, block_off_end - to);
2742 if (block_off_start < from)
2743 memset(kaddr + block_off_start, 0,
2744 from - block_off_start);
2745 flush_dcache_page(page);
2746 kunmap_atomic(kaddr, KM_USER0);
2747 }
2748 if ((em->block_start != EXTENT_MAP_HOLE &&
2749 em->block_start != EXTENT_MAP_INLINE) &&
2750 !isnew && !PageUptodate(page) &&
2751 (block_off_end > to || block_off_start < from) &&
2752 !test_range_bit(tree, block_start, cur_end,
2753 EXTENT_UPTODATE, 1)) {
2754 u64 sector;
2755 u64 extent_offset = block_start - em->start;
2756 size_t iosize;
2757 sector = (em->block_start + extent_offset) >> 9;
2758 iosize = (cur_end - block_start + blocksize) &
2759 ~((u64)blocksize - 1);
2760 /*
2761 * we've already got the extent locked, but we
2762 * need to split the state such that our end_bio
2763 * handler can clear the lock.
2764 */
2765 set_extent_bit(tree, block_start,
2766 block_start + iosize - 1,
2767 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2768 ret = submit_extent_page(READ, tree, page,
2769 sector, iosize, page_offset, em->bdev,
2770 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002771 end_bio_extent_preparewrite, 0,
2772 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002773 iocount++;
2774 block_start = block_start + iosize;
2775 } else {
2776 set_extent_uptodate(tree, block_start, cur_end,
2777 GFP_NOFS);
2778 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2779 block_start = cur_end + 1;
2780 }
2781 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2782 free_extent_map(em);
2783 }
2784 if (iocount) {
2785 wait_extent_bit(tree, orig_block_start,
2786 block_end, EXTENT_LOCKED);
2787 }
2788 check_page_uptodate(tree, page);
2789err:
2790 /* FIXME, zero out newly allocated blocks on error */
2791 return err;
2792}
2793EXPORT_SYMBOL(extent_prepare_write);
2794
2795/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002796 * a helper for releasepage, this tests for areas of the page that
2797 * are locked or under IO and drops the related state bits if it is safe
2798 * to drop the page.
2799 */
2800int try_release_extent_state(struct extent_map_tree *map,
2801 struct extent_io_tree *tree, struct page *page,
2802 gfp_t mask)
2803{
2804 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2805 u64 end = start + PAGE_CACHE_SIZE - 1;
2806 int ret = 1;
2807
Chris Mason211f90e2008-07-18 11:56:15 -04002808 if (test_range_bit(tree, start, end,
2809 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002810 ret = 0;
2811 else {
2812 if ((mask & GFP_NOFS) == GFP_NOFS)
2813 mask = GFP_NOFS;
2814 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2815 1, 1, mask);
2816 }
2817 return ret;
2818}
2819EXPORT_SYMBOL(try_release_extent_state);
2820
2821/*
Chris Masond1310b22008-01-24 16:13:08 -05002822 * a helper for releasepage. As long as there are no locked extents
2823 * in the range corresponding to the page, both state records and extent
2824 * map records are removed
2825 */
2826int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002827 struct extent_io_tree *tree, struct page *page,
2828 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002829{
2830 struct extent_map *em;
2831 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2832 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002833
Chris Mason70dec802008-01-29 09:59:12 -05002834 if ((mask & __GFP_WAIT) &&
2835 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002836 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002837 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002838 len = end - start + 1;
Chris Mason70dec802008-01-29 09:59:12 -05002839 spin_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002840 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002841 if (!em || IS_ERR(em)) {
2842 spin_unlock(&map->lock);
2843 break;
2844 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002845 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2846 em->start != start) {
Chris Mason70dec802008-01-29 09:59:12 -05002847 spin_unlock(&map->lock);
2848 free_extent_map(em);
2849 break;
2850 }
2851 if (!test_range_bit(tree, em->start,
2852 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002853 EXTENT_LOCKED | EXTENT_WRITEBACK |
2854 EXTENT_ORDERED,
2855 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002856 remove_extent_mapping(map, em);
2857 /* once for the rb tree */
2858 free_extent_map(em);
2859 }
2860 start = extent_map_end(em);
Chris Masond1310b22008-01-24 16:13:08 -05002861 spin_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002862
2863 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002864 free_extent_map(em);
2865 }
Chris Masond1310b22008-01-24 16:13:08 -05002866 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002867 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002868}
2869EXPORT_SYMBOL(try_release_extent_mapping);
2870
2871sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2872 get_extent_t *get_extent)
2873{
2874 struct inode *inode = mapping->host;
2875 u64 start = iblock << inode->i_blkbits;
2876 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002877 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002878 struct extent_map *em;
2879
Yan Zhengd899e052008-10-30 14:25:28 -04002880 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2881 GFP_NOFS);
2882 em = get_extent(inode, NULL, 0, start, blksize, 0);
2883 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2884 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002885 if (!em || IS_ERR(em))
2886 return 0;
2887
Yan Zhengd899e052008-10-30 14:25:28 -04002888 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002889 goto out;
2890
2891 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002892out:
2893 free_extent_map(em);
2894 return sector;
2895}
2896
Chris Masond1310b22008-01-24 16:13:08 -05002897static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2898 unsigned long i)
2899{
2900 struct page *p;
2901 struct address_space *mapping;
2902
2903 if (i == 0)
2904 return eb->first_page;
2905 i += eb->start >> PAGE_CACHE_SHIFT;
2906 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002907 if (!mapping)
2908 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002909
2910 /*
2911 * extent_buffer_page is only called after pinning the page
2912 * by increasing the reference count. So we know the page must
2913 * be in the radix tree.
2914 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002915 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002916 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002917 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002918
Chris Masond1310b22008-01-24 16:13:08 -05002919 return p;
2920}
2921
Chris Mason6af118c2008-07-22 11:18:07 -04002922static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002923{
Chris Mason6af118c2008-07-22 11:18:07 -04002924 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2925 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002926}
2927
Chris Masond1310b22008-01-24 16:13:08 -05002928static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2929 u64 start,
2930 unsigned long len,
2931 gfp_t mask)
2932{
2933 struct extent_buffer *eb = NULL;
Chris Mason4bef0842008-09-08 11:18:08 -04002934#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002935 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04002936#endif
Chris Masond1310b22008-01-24 16:13:08 -05002937
Chris Masond1310b22008-01-24 16:13:08 -05002938 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002939 eb->start = start;
2940 eb->len = len;
Chris Masona61e6f22008-07-22 11:18:08 -04002941 mutex_init(&eb->mutex);
Chris Mason4bef0842008-09-08 11:18:08 -04002942#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002943 spin_lock_irqsave(&leak_lock, flags);
2944 list_add(&eb->leak_list, &buffers);
2945 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002946#endif
Chris Masond1310b22008-01-24 16:13:08 -05002947 atomic_set(&eb->refs, 1);
2948
2949 return eb;
2950}
2951
2952static void __free_extent_buffer(struct extent_buffer *eb)
2953{
Chris Mason4bef0842008-09-08 11:18:08 -04002954#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002955 unsigned long flags;
2956 spin_lock_irqsave(&leak_lock, flags);
2957 list_del(&eb->leak_list);
2958 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002959#endif
Chris Masond1310b22008-01-24 16:13:08 -05002960 kmem_cache_free(extent_buffer_cache, eb);
2961}
2962
2963struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2964 u64 start, unsigned long len,
2965 struct page *page0,
2966 gfp_t mask)
2967{
2968 unsigned long num_pages = num_extent_pages(start, len);
2969 unsigned long i;
2970 unsigned long index = start >> PAGE_CACHE_SHIFT;
2971 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04002972 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002973 struct page *p;
2974 struct address_space *mapping = tree->mapping;
2975 int uptodate = 1;
2976
Chris Mason6af118c2008-07-22 11:18:07 -04002977 spin_lock(&tree->buffer_lock);
2978 eb = buffer_search(tree, start);
2979 if (eb) {
2980 atomic_inc(&eb->refs);
2981 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002982 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04002983 return eb;
2984 }
2985 spin_unlock(&tree->buffer_lock);
2986
Chris Masond1310b22008-01-24 16:13:08 -05002987 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04002988 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05002989 return NULL;
2990
Chris Masond1310b22008-01-24 16:13:08 -05002991 if (page0) {
2992 eb->first_page = page0;
2993 i = 1;
2994 index++;
2995 page_cache_get(page0);
2996 mark_page_accessed(page0);
2997 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002998 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04002999 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003000 } else {
3001 i = 0;
3002 }
3003 for (; i < num_pages; i++, index++) {
3004 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3005 if (!p) {
3006 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003007 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003008 }
3009 set_page_extent_mapped(p);
3010 mark_page_accessed(p);
3011 if (i == 0) {
3012 eb->first_page = p;
3013 set_page_extent_head(p, len);
3014 } else {
3015 set_page_private(p, EXTENT_PAGE_PRIVATE);
3016 }
3017 if (!PageUptodate(p))
3018 uptodate = 0;
3019 unlock_page(p);
3020 }
3021 if (uptodate)
3022 eb->flags |= EXTENT_UPTODATE;
3023 eb->flags |= EXTENT_BUFFER_FILLED;
3024
Chris Mason6af118c2008-07-22 11:18:07 -04003025 spin_lock(&tree->buffer_lock);
3026 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3027 if (exists) {
3028 /* add one reference for the caller */
3029 atomic_inc(&exists->refs);
3030 spin_unlock(&tree->buffer_lock);
3031 goto free_eb;
3032 }
3033 spin_unlock(&tree->buffer_lock);
3034
3035 /* add one reference for the tree */
3036 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003037 return eb;
3038
Chris Mason6af118c2008-07-22 11:18:07 -04003039free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003040 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003041 return exists;
3042 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003043 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118c2008-07-22 11:18:07 -04003044 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003045 __free_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003046 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003047}
3048EXPORT_SYMBOL(alloc_extent_buffer);
3049
3050struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3051 u64 start, unsigned long len,
3052 gfp_t mask)
3053{
Chris Masond1310b22008-01-24 16:13:08 -05003054 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003055
Chris Mason6af118c2008-07-22 11:18:07 -04003056 spin_lock(&tree->buffer_lock);
3057 eb = buffer_search(tree, start);
3058 if (eb)
3059 atomic_inc(&eb->refs);
3060 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003061
Josef Bacik0f9dd462008-09-23 13:14:11 -04003062 if (eb)
3063 mark_page_accessed(eb->first_page);
3064
Chris Masond1310b22008-01-24 16:13:08 -05003065 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003066}
3067EXPORT_SYMBOL(find_extent_buffer);
3068
3069void free_extent_buffer(struct extent_buffer *eb)
3070{
Chris Masond1310b22008-01-24 16:13:08 -05003071 if (!eb)
3072 return;
3073
3074 if (!atomic_dec_and_test(&eb->refs))
3075 return;
3076
Chris Mason6af118c2008-07-22 11:18:07 -04003077 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003078}
3079EXPORT_SYMBOL(free_extent_buffer);
3080
3081int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3082 struct extent_buffer *eb)
3083{
3084 int set;
3085 unsigned long i;
3086 unsigned long num_pages;
3087 struct page *page;
3088
3089 u64 start = eb->start;
3090 u64 end = start + eb->len - 1;
3091
3092 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
3093 num_pages = num_extent_pages(eb->start, eb->len);
3094
3095 for (i = 0; i < num_pages; i++) {
3096 page = extent_buffer_page(eb, i);
Chris Masona61e6f22008-07-22 11:18:08 -04003097 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003098 if (i == 0)
3099 set_page_extent_head(page, eb->len);
3100 else
3101 set_page_private(page, EXTENT_PAGE_PRIVATE);
3102
3103 /*
3104 * if we're on the last page or the first page and the
3105 * block isn't aligned on a page boundary, do extra checks
3106 * to make sure we don't clean page that is partially dirty
3107 */
3108 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3109 ((i == num_pages - 1) &&
3110 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3111 start = (u64)page->index << PAGE_CACHE_SHIFT;
3112 end = start + PAGE_CACHE_SIZE - 1;
3113 if (test_range_bit(tree, start, end,
3114 EXTENT_DIRTY, 0)) {
Chris Masona61e6f22008-07-22 11:18:08 -04003115 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003116 continue;
3117 }
3118 }
3119 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003120 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003121 if (!PageDirty(page)) {
3122 radix_tree_tag_clear(&page->mapping->page_tree,
3123 page_index(page),
3124 PAGECACHE_TAG_DIRTY);
3125 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003126 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003127 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003128 }
3129 return 0;
3130}
3131EXPORT_SYMBOL(clear_extent_buffer_dirty);
3132
3133int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3134 struct extent_buffer *eb)
3135{
3136 return wait_on_extent_writeback(tree, eb->start,
3137 eb->start + eb->len - 1);
3138}
3139EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
3140
3141int set_extent_buffer_dirty(struct extent_io_tree *tree,
3142 struct extent_buffer *eb)
3143{
3144 unsigned long i;
3145 unsigned long num_pages;
3146
3147 num_pages = num_extent_pages(eb->start, eb->len);
3148 for (i = 0; i < num_pages; i++) {
3149 struct page *page = extent_buffer_page(eb, i);
3150 /* writepage may need to do something special for the
3151 * first page, we have to make sure page->private is
3152 * properly set. releasepage may drop page->private
3153 * on us if the page isn't already dirty.
3154 */
Chris Masona1b32a52008-09-05 16:09:51 -04003155 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003156 if (i == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003157 set_page_extent_head(page, eb->len);
3158 } else if (PagePrivate(page) &&
3159 page->private != EXTENT_PAGE_PRIVATE) {
Chris Masond1310b22008-01-24 16:13:08 -05003160 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003161 }
3162 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masona1b32a52008-09-05 16:09:51 -04003163 set_extent_dirty(tree, page_offset(page),
3164 page_offset(page) + PAGE_CACHE_SIZE -1,
3165 GFP_NOFS);
3166 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003167 }
Chris Masona1b32a52008-09-05 16:09:51 -04003168 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05003169}
3170EXPORT_SYMBOL(set_extent_buffer_dirty);
3171
Chris Mason1259ab72008-05-12 13:39:03 -04003172int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3173 struct extent_buffer *eb)
3174{
3175 unsigned long i;
3176 struct page *page;
3177 unsigned long num_pages;
3178
3179 num_pages = num_extent_pages(eb->start, eb->len);
3180 eb->flags &= ~EXTENT_UPTODATE;
3181
3182 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3183 GFP_NOFS);
3184 for (i = 0; i < num_pages; i++) {
3185 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003186 if (page)
3187 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003188 }
3189 return 0;
3190}
3191
Chris Masond1310b22008-01-24 16:13:08 -05003192int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3193 struct extent_buffer *eb)
3194{
3195 unsigned long i;
3196 struct page *page;
3197 unsigned long num_pages;
3198
3199 num_pages = num_extent_pages(eb->start, eb->len);
3200
3201 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3202 GFP_NOFS);
3203 for (i = 0; i < num_pages; i++) {
3204 page = extent_buffer_page(eb, i);
3205 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3206 ((i == num_pages - 1) &&
3207 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3208 check_page_uptodate(tree, page);
3209 continue;
3210 }
3211 SetPageUptodate(page);
3212 }
3213 return 0;
3214}
3215EXPORT_SYMBOL(set_extent_buffer_uptodate);
3216
Chris Masonce9adaa2008-04-09 16:28:12 -04003217int extent_range_uptodate(struct extent_io_tree *tree,
3218 u64 start, u64 end)
3219{
3220 struct page *page;
3221 int ret;
3222 int pg_uptodate = 1;
3223 int uptodate;
3224 unsigned long index;
3225
3226 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3227 if (ret)
3228 return 1;
3229 while(start <= end) {
3230 index = start >> PAGE_CACHE_SHIFT;
3231 page = find_get_page(tree->mapping, index);
3232 uptodate = PageUptodate(page);
3233 page_cache_release(page);
3234 if (!uptodate) {
3235 pg_uptodate = 0;
3236 break;
3237 }
3238 start += PAGE_CACHE_SIZE;
3239 }
3240 return pg_uptodate;
3241}
3242
Chris Masond1310b22008-01-24 16:13:08 -05003243int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003244 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003245{
Chris Mason728131d2008-04-09 16:28:12 -04003246 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003247 unsigned long num_pages;
3248 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003249 struct page *page;
3250 int pg_uptodate = 1;
3251
Chris Masond1310b22008-01-24 16:13:08 -05003252 if (eb->flags & EXTENT_UPTODATE)
Chris Mason42352982008-04-28 16:40:52 -04003253 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003254
Chris Mason42352982008-04-28 16:40:52 -04003255 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003256 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003257 if (ret)
3258 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003259
3260 num_pages = num_extent_pages(eb->start, eb->len);
3261 for (i = 0; i < num_pages; i++) {
3262 page = extent_buffer_page(eb, i);
3263 if (!PageUptodate(page)) {
3264 pg_uptodate = 0;
3265 break;
3266 }
3267 }
Chris Mason42352982008-04-28 16:40:52 -04003268 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003269}
3270EXPORT_SYMBOL(extent_buffer_uptodate);
3271
3272int read_extent_buffer_pages(struct extent_io_tree *tree,
3273 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003274 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003275 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003276{
3277 unsigned long i;
3278 unsigned long start_i;
3279 struct page *page;
3280 int err;
3281 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003282 int locked_pages = 0;
3283 int all_uptodate = 1;
3284 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003285 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003286 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003287 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003288
Chris Masond1310b22008-01-24 16:13:08 -05003289 if (eb->flags & EXTENT_UPTODATE)
3290 return 0;
3291
Chris Masonce9adaa2008-04-09 16:28:12 -04003292 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003293 EXTENT_UPTODATE, 1)) {
3294 return 0;
3295 }
3296
3297 if (start) {
3298 WARN_ON(start < eb->start);
3299 start_i = (start >> PAGE_CACHE_SHIFT) -
3300 (eb->start >> PAGE_CACHE_SHIFT);
3301 } else {
3302 start_i = 0;
3303 }
3304
3305 num_pages = num_extent_pages(eb->start, eb->len);
3306 for (i = start_i; i < num_pages; i++) {
3307 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003308 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003309 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003310 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003311 } else {
3312 lock_page(page);
3313 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003314 locked_pages++;
Chris Masond1310b22008-01-24 16:13:08 -05003315 if (!PageUptodate(page)) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003316 all_uptodate = 0;
3317 }
3318 }
3319 if (all_uptodate) {
3320 if (start_i == 0)
3321 eb->flags |= EXTENT_UPTODATE;
Chris Masona1b32a52008-09-05 16:09:51 -04003322 if (ret) {
3323 printk("all up to date but ret is %d\n", ret);
3324 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003325 goto unlock_exit;
3326 }
3327
3328 for (i = start_i; i < num_pages; i++) {
3329 page = extent_buffer_page(eb, i);
3330 if (inc_all_pages)
3331 page_cache_get(page);
3332 if (!PageUptodate(page)) {
3333 if (start_i == 0)
3334 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003335 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003336 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003337 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003338 mirror_num, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003339 if (err) {
3340 ret = err;
Chris Masona1b32a52008-09-05 16:09:51 -04003341 printk("err %d from __extent_read_full_page\n", ret);
Chris Masond1310b22008-01-24 16:13:08 -05003342 }
3343 } else {
3344 unlock_page(page);
3345 }
3346 }
3347
Chris Masona86c12c2008-02-07 10:50:54 -05003348 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003349 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003350
Chris Masond1310b22008-01-24 16:13:08 -05003351 if (ret || !wait) {
Chris Masona1b32a52008-09-05 16:09:51 -04003352 if (ret)
3353 printk("ret %d wait %d returning\n", ret, wait);
Chris Masond1310b22008-01-24 16:13:08 -05003354 return ret;
3355 }
Chris Masond1310b22008-01-24 16:13:08 -05003356 for (i = start_i; i < num_pages; i++) {
3357 page = extent_buffer_page(eb, i);
3358 wait_on_page_locked(page);
3359 if (!PageUptodate(page)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003360 printk("page not uptodate after wait_on_page_locked\n");
Chris Masond1310b22008-01-24 16:13:08 -05003361 ret = -EIO;
3362 }
3363 }
3364 if (!ret)
3365 eb->flags |= EXTENT_UPTODATE;
3366 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003367
3368unlock_exit:
3369 i = start_i;
3370 while(locked_pages > 0) {
3371 page = extent_buffer_page(eb, i);
3372 i++;
3373 unlock_page(page);
3374 locked_pages--;
3375 }
3376 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003377}
3378EXPORT_SYMBOL(read_extent_buffer_pages);
3379
3380void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3381 unsigned long start,
3382 unsigned long len)
3383{
3384 size_t cur;
3385 size_t offset;
3386 struct page *page;
3387 char *kaddr;
3388 char *dst = (char *)dstv;
3389 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3390 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003391
3392 WARN_ON(start > eb->len);
3393 WARN_ON(start + len > eb->start + eb->len);
3394
3395 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3396
3397 while(len > 0) {
3398 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003399
3400 cur = min(len, (PAGE_CACHE_SIZE - offset));
3401 kaddr = kmap_atomic(page, KM_USER1);
3402 memcpy(dst, kaddr + offset, cur);
3403 kunmap_atomic(kaddr, KM_USER1);
3404
3405 dst += cur;
3406 len -= cur;
3407 offset = 0;
3408 i++;
3409 }
3410}
3411EXPORT_SYMBOL(read_extent_buffer);
3412
3413int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3414 unsigned long min_len, char **token, char **map,
3415 unsigned long *map_start,
3416 unsigned long *map_len, int km)
3417{
3418 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3419 char *kaddr;
3420 struct page *p;
3421 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3422 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3423 unsigned long end_i = (start_offset + start + min_len - 1) >>
3424 PAGE_CACHE_SHIFT;
3425
3426 if (i != end_i)
3427 return -EINVAL;
3428
3429 if (i == 0) {
3430 offset = start_offset;
3431 *map_start = 0;
3432 } else {
3433 offset = 0;
3434 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3435 }
3436 if (start + min_len > eb->len) {
3437printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3438 WARN_ON(1);
3439 }
3440
3441 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003442 kaddr = kmap_atomic(p, km);
3443 *token = kaddr;
3444 *map = kaddr + offset;
3445 *map_len = PAGE_CACHE_SIZE - offset;
3446 return 0;
3447}
3448EXPORT_SYMBOL(map_private_extent_buffer);
3449
3450int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3451 unsigned long min_len,
3452 char **token, char **map,
3453 unsigned long *map_start,
3454 unsigned long *map_len, int km)
3455{
3456 int err;
3457 int save = 0;
3458 if (eb->map_token) {
3459 unmap_extent_buffer(eb, eb->map_token, km);
3460 eb->map_token = NULL;
3461 save = 1;
3462 }
3463 err = map_private_extent_buffer(eb, start, min_len, token, map,
3464 map_start, map_len, km);
3465 if (!err && save) {
3466 eb->map_token = *token;
3467 eb->kaddr = *map;
3468 eb->map_start = *map_start;
3469 eb->map_len = *map_len;
3470 }
3471 return err;
3472}
3473EXPORT_SYMBOL(map_extent_buffer);
3474
3475void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3476{
3477 kunmap_atomic(token, km);
3478}
3479EXPORT_SYMBOL(unmap_extent_buffer);
3480
3481int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3482 unsigned long start,
3483 unsigned long len)
3484{
3485 size_t cur;
3486 size_t offset;
3487 struct page *page;
3488 char *kaddr;
3489 char *ptr = (char *)ptrv;
3490 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3491 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3492 int ret = 0;
3493
3494 WARN_ON(start > eb->len);
3495 WARN_ON(start + len > eb->start + eb->len);
3496
3497 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3498
3499 while(len > 0) {
3500 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003501
3502 cur = min(len, (PAGE_CACHE_SIZE - offset));
3503
3504 kaddr = kmap_atomic(page, KM_USER0);
3505 ret = memcmp(ptr, kaddr + offset, cur);
3506 kunmap_atomic(kaddr, KM_USER0);
3507 if (ret)
3508 break;
3509
3510 ptr += cur;
3511 len -= cur;
3512 offset = 0;
3513 i++;
3514 }
3515 return ret;
3516}
3517EXPORT_SYMBOL(memcmp_extent_buffer);
3518
3519void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3520 unsigned long start, unsigned long len)
3521{
3522 size_t cur;
3523 size_t offset;
3524 struct page *page;
3525 char *kaddr;
3526 char *src = (char *)srcv;
3527 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3528 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3529
3530 WARN_ON(start > eb->len);
3531 WARN_ON(start + len > eb->start + eb->len);
3532
3533 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3534
3535 while(len > 0) {
3536 page = extent_buffer_page(eb, i);
3537 WARN_ON(!PageUptodate(page));
3538
3539 cur = min(len, PAGE_CACHE_SIZE - offset);
3540 kaddr = kmap_atomic(page, KM_USER1);
3541 memcpy(kaddr + offset, src, cur);
3542 kunmap_atomic(kaddr, KM_USER1);
3543
3544 src += cur;
3545 len -= cur;
3546 offset = 0;
3547 i++;
3548 }
3549}
3550EXPORT_SYMBOL(write_extent_buffer);
3551
3552void memset_extent_buffer(struct extent_buffer *eb, char c,
3553 unsigned long start, unsigned long len)
3554{
3555 size_t cur;
3556 size_t offset;
3557 struct page *page;
3558 char *kaddr;
3559 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3560 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3561
3562 WARN_ON(start > eb->len);
3563 WARN_ON(start + len > eb->start + eb->len);
3564
3565 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3566
3567 while(len > 0) {
3568 page = extent_buffer_page(eb, i);
3569 WARN_ON(!PageUptodate(page));
3570
3571 cur = min(len, PAGE_CACHE_SIZE - offset);
3572 kaddr = kmap_atomic(page, KM_USER0);
3573 memset(kaddr + offset, c, cur);
3574 kunmap_atomic(kaddr, KM_USER0);
3575
3576 len -= cur;
3577 offset = 0;
3578 i++;
3579 }
3580}
3581EXPORT_SYMBOL(memset_extent_buffer);
3582
3583void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3584 unsigned long dst_offset, unsigned long src_offset,
3585 unsigned long len)
3586{
3587 u64 dst_len = dst->len;
3588 size_t cur;
3589 size_t offset;
3590 struct page *page;
3591 char *kaddr;
3592 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3593 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3594
3595 WARN_ON(src->len != dst_len);
3596
3597 offset = (start_offset + dst_offset) &
3598 ((unsigned long)PAGE_CACHE_SIZE - 1);
3599
3600 while(len > 0) {
3601 page = extent_buffer_page(dst, i);
3602 WARN_ON(!PageUptodate(page));
3603
3604 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3605
3606 kaddr = kmap_atomic(page, KM_USER0);
3607 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3608 kunmap_atomic(kaddr, KM_USER0);
3609
3610 src_offset += cur;
3611 len -= cur;
3612 offset = 0;
3613 i++;
3614 }
3615}
3616EXPORT_SYMBOL(copy_extent_buffer);
3617
3618static void move_pages(struct page *dst_page, struct page *src_page,
3619 unsigned long dst_off, unsigned long src_off,
3620 unsigned long len)
3621{
3622 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3623 if (dst_page == src_page) {
3624 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3625 } else {
3626 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3627 char *p = dst_kaddr + dst_off + len;
3628 char *s = src_kaddr + src_off + len;
3629
3630 while (len--)
3631 *--p = *--s;
3632
3633 kunmap_atomic(src_kaddr, KM_USER1);
3634 }
3635 kunmap_atomic(dst_kaddr, KM_USER0);
3636}
3637
3638static void copy_pages(struct page *dst_page, struct page *src_page,
3639 unsigned long dst_off, unsigned long src_off,
3640 unsigned long len)
3641{
3642 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3643 char *src_kaddr;
3644
3645 if (dst_page != src_page)
3646 src_kaddr = kmap_atomic(src_page, KM_USER1);
3647 else
3648 src_kaddr = dst_kaddr;
3649
3650 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3651 kunmap_atomic(dst_kaddr, KM_USER0);
3652 if (dst_page != src_page)
3653 kunmap_atomic(src_kaddr, KM_USER1);
3654}
3655
3656void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3657 unsigned long src_offset, unsigned long len)
3658{
3659 size_t cur;
3660 size_t dst_off_in_page;
3661 size_t src_off_in_page;
3662 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3663 unsigned long dst_i;
3664 unsigned long src_i;
3665
3666 if (src_offset + len > dst->len) {
3667 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3668 src_offset, len, dst->len);
3669 BUG_ON(1);
3670 }
3671 if (dst_offset + len > dst->len) {
3672 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3673 dst_offset, len, dst->len);
3674 BUG_ON(1);
3675 }
3676
3677 while(len > 0) {
3678 dst_off_in_page = (start_offset + dst_offset) &
3679 ((unsigned long)PAGE_CACHE_SIZE - 1);
3680 src_off_in_page = (start_offset + src_offset) &
3681 ((unsigned long)PAGE_CACHE_SIZE - 1);
3682
3683 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3684 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3685
3686 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3687 src_off_in_page));
3688 cur = min_t(unsigned long, cur,
3689 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3690
3691 copy_pages(extent_buffer_page(dst, dst_i),
3692 extent_buffer_page(dst, src_i),
3693 dst_off_in_page, src_off_in_page, cur);
3694
3695 src_offset += cur;
3696 dst_offset += cur;
3697 len -= cur;
3698 }
3699}
3700EXPORT_SYMBOL(memcpy_extent_buffer);
3701
3702void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3703 unsigned long src_offset, unsigned long len)
3704{
3705 size_t cur;
3706 size_t dst_off_in_page;
3707 size_t src_off_in_page;
3708 unsigned long dst_end = dst_offset + len - 1;
3709 unsigned long src_end = src_offset + len - 1;
3710 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3711 unsigned long dst_i;
3712 unsigned long src_i;
3713
3714 if (src_offset + len > dst->len) {
3715 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3716 src_offset, len, dst->len);
3717 BUG_ON(1);
3718 }
3719 if (dst_offset + len > dst->len) {
3720 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3721 dst_offset, len, dst->len);
3722 BUG_ON(1);
3723 }
3724 if (dst_offset < src_offset) {
3725 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3726 return;
3727 }
3728 while(len > 0) {
3729 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3730 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3731
3732 dst_off_in_page = (start_offset + dst_end) &
3733 ((unsigned long)PAGE_CACHE_SIZE - 1);
3734 src_off_in_page = (start_offset + src_end) &
3735 ((unsigned long)PAGE_CACHE_SIZE - 1);
3736
3737 cur = min_t(unsigned long, len, src_off_in_page + 1);
3738 cur = min(cur, dst_off_in_page + 1);
3739 move_pages(extent_buffer_page(dst, dst_i),
3740 extent_buffer_page(dst, src_i),
3741 dst_off_in_page - cur + 1,
3742 src_off_in_page - cur + 1, cur);
3743
3744 dst_end -= cur;
3745 src_end -= cur;
3746 len -= cur;
3747 }
3748}
3749EXPORT_SYMBOL(memmove_extent_buffer);
Chris Mason6af118c2008-07-22 11:18:07 -04003750
3751int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3752{
3753 u64 start = page_offset(page);
3754 struct extent_buffer *eb;
3755 int ret = 1;
3756 unsigned long i;
3757 unsigned long num_pages;
3758
3759 spin_lock(&tree->buffer_lock);
3760 eb = buffer_search(tree, start);
3761 if (!eb)
3762 goto out;
3763
3764 if (atomic_read(&eb->refs) > 1) {
3765 ret = 0;
3766 goto out;
3767 }
3768 /* at this point we can safely release the extent buffer */
3769 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003770 for (i = 0; i < num_pages; i++)
3771 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118c2008-07-22 11:18:07 -04003772 rb_erase(&eb->rb_node, &tree->buffer);
3773 __free_extent_buffer(eb);
3774out:
3775 spin_unlock(&tree->buffer_lock);
3776 return ret;
3777}
3778EXPORT_SYMBOL(try_release_extent_buffer);