blob: c3dfe2a0ec858ba78afe319867a5c99992ae7799 [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 Masonb47eda82008-11-10 12:34:40 -050033#define LEAK_DEBUG 0
Chris Mason4bef0842008-09-08 11:18:08 -040034#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -040035static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
Chris Mason4bef0842008-09-08 11:18:08 -040036#endif
Chris Masond1310b22008-01-24 16:13:08 -050037
Chris Masond1310b22008-01-24 16:13:08 -050038#define BUFFER_LRU_MAX 64
39
40struct tree_entry {
41 u64 start;
42 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050043 struct rb_node rb_node;
44};
45
46struct extent_page_data {
47 struct bio *bio;
48 struct extent_io_tree *tree;
49 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050050
51 /* tells writepage not to lock the state bits for this range
52 * it still does the unlocking
53 */
54 int extent_locked;
Chris Masond1310b22008-01-24 16:13:08 -050055};
56
57int __init extent_io_init(void)
58{
59 extent_state_cache = btrfs_cache_create("extent_state",
60 sizeof(struct extent_state), 0,
61 NULL);
62 if (!extent_state_cache)
63 return -ENOMEM;
64
65 extent_buffer_cache = btrfs_cache_create("extent_buffers",
66 sizeof(struct extent_buffer), 0,
67 NULL);
68 if (!extent_buffer_cache)
69 goto free_state_cache;
70 return 0;
71
72free_state_cache:
73 kmem_cache_destroy(extent_state_cache);
74 return -ENOMEM;
75}
76
77void extent_io_exit(void)
78{
79 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040080 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050081
82 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040083 state = list_entry(states.next, struct extent_state, leak_list);
Chris Mason70dec802008-01-29 09:59:12 -050084 printk("state leak: start %Lu end %Lu state %lu in tree %p refs %d\n", state->start, state->end, state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040085 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050086 kmem_cache_free(extent_state_cache, state);
87
88 }
89
Chris Mason2d2ae542008-03-26 16:24:23 -040090 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
92 printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs));
93 list_del(&eb->leak_list);
94 kmem_cache_free(extent_buffer_cache, eb);
95 }
Chris Masond1310b22008-01-24 16:13:08 -050096 if (extent_state_cache)
97 kmem_cache_destroy(extent_state_cache);
98 if (extent_buffer_cache)
99 kmem_cache_destroy(extent_buffer_cache);
100}
101
102void extent_io_tree_init(struct extent_io_tree *tree,
103 struct address_space *mapping, gfp_t mask)
104{
105 tree->state.rb_node = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400106 tree->buffer.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500107 tree->ops = NULL;
108 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500109 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400110 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500111 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500112}
113EXPORT_SYMBOL(extent_io_tree_init);
114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
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
Christoph Hellwigb2950862008-12-02 09:54:17 -0500139static void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500140{
Chris Masond1310b22008-01-24 16:13:08 -0500141 if (!state)
142 return;
143 if (atomic_dec_and_test(&state->refs)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400144#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400145 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400146#endif
Chris Mason70dec802008-01-29 09:59:12 -0500147 WARN_ON(state->tree);
Chris Mason4bef0842008-09-08 11:18:08 -0400148#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Masond1310b22008-01-24 16:13:08 -0500153 kmem_cache_free(extent_state_cache, state);
154 }
155}
156EXPORT_SYMBOL(free_extent_state);
157
158static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
159 struct rb_node *node)
160{
161 struct rb_node ** p = &root->rb_node;
162 struct rb_node * parent = NULL;
163 struct tree_entry *entry;
164
165 while(*p) {
166 parent = *p;
167 entry = rb_entry(parent, struct tree_entry, rb_node);
168
169 if (offset < entry->start)
170 p = &(*p)->rb_left;
171 else if (offset > entry->end)
172 p = &(*p)->rb_right;
173 else
174 return parent;
175 }
176
177 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500178 rb_link_node(node, parent, p);
179 rb_insert_color(node, root);
180 return NULL;
181}
182
Chris Mason80ea96b2008-02-01 14:51:59 -0500183static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500184 struct rb_node **prev_ret,
185 struct rb_node **next_ret)
186{
Chris Mason80ea96b2008-02-01 14:51:59 -0500187 struct rb_root *root = &tree->state;
Chris Masond1310b22008-01-24 16:13:08 -0500188 struct rb_node * n = root->rb_node;
189 struct rb_node *prev = NULL;
190 struct rb_node *orig_prev = NULL;
191 struct tree_entry *entry;
192 struct tree_entry *prev_entry = NULL;
193
194 while(n) {
195 entry = rb_entry(n, struct tree_entry, rb_node);
196 prev = n;
197 prev_entry = entry;
198
199 if (offset < entry->start)
200 n = n->rb_left;
201 else if (offset > entry->end)
202 n = n->rb_right;
Chris Mason80ea96b2008-02-01 14:51:59 -0500203 else {
Chris Masond1310b22008-01-24 16:13:08 -0500204 return n;
Chris Mason80ea96b2008-02-01 14:51:59 -0500205 }
Chris Masond1310b22008-01-24 16:13:08 -0500206 }
207
208 if (prev_ret) {
209 orig_prev = prev;
210 while(prev && offset > prev_entry->end) {
211 prev = rb_next(prev);
212 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
213 }
214 *prev_ret = prev;
215 prev = orig_prev;
216 }
217
218 if (next_ret) {
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 while(prev && offset < prev_entry->start) {
221 prev = rb_prev(prev);
222 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
223 }
224 *next_ret = prev;
225 }
226 return NULL;
227}
228
Chris Mason80ea96b2008-02-01 14:51:59 -0500229static inline struct rb_node *tree_search(struct extent_io_tree *tree,
230 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500231{
Chris Mason70dec802008-01-29 09:59:12 -0500232 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500233 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500234
Chris Mason80ea96b2008-02-01 14:51:59 -0500235 ret = __etree_search(tree, offset, &prev, NULL);
236 if (!ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500237 return prev;
Chris Mason80ea96b2008-02-01 14:51:59 -0500238 }
Chris Masond1310b22008-01-24 16:13:08 -0500239 return ret;
240}
241
Chris Mason6af118ce2008-07-22 11:18:07 -0400242static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
243 u64 offset, struct rb_node *node)
244{
245 struct rb_root *root = &tree->buffer;
246 struct rb_node ** p = &root->rb_node;
247 struct rb_node * parent = NULL;
248 struct extent_buffer *eb;
249
250 while(*p) {
251 parent = *p;
252 eb = rb_entry(parent, struct extent_buffer, rb_node);
253
254 if (offset < eb->start)
255 p = &(*p)->rb_left;
256 else if (offset > eb->start)
257 p = &(*p)->rb_right;
258 else
259 return eb;
260 }
261
262 rb_link_node(node, parent, p);
263 rb_insert_color(node, root);
264 return NULL;
265}
266
267static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
268 u64 offset)
269{
270 struct rb_root *root = &tree->buffer;
271 struct rb_node * n = root->rb_node;
272 struct extent_buffer *eb;
273
274 while(n) {
275 eb = rb_entry(n, struct extent_buffer, rb_node);
276 if (offset < eb->start)
277 n = n->rb_left;
278 else if (offset > eb->start)
279 n = n->rb_right;
280 else
281 return eb;
282 }
283 return NULL;
284}
285
Chris Masond1310b22008-01-24 16:13:08 -0500286/*
287 * utility function to look for merge candidates inside a given range.
288 * Any extents with matching state are merged together into a single
289 * extent in the tree. Extents with EXTENT_IO in their state field
290 * are not merged because the end_io handlers need to be able to do
291 * operations on them without sleeping (or doing allocations/splits).
292 *
293 * This should be called with the tree lock held.
294 */
295static int merge_state(struct extent_io_tree *tree,
296 struct extent_state *state)
297{
298 struct extent_state *other;
299 struct rb_node *other_node;
300
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400301 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500302 return 0;
303
304 other_node = rb_prev(&state->rb_node);
305 if (other_node) {
306 other = rb_entry(other_node, struct extent_state, rb_node);
307 if (other->end == state->start - 1 &&
308 other->state == state->state) {
309 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500310 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500311 rb_erase(&other->rb_node, &tree->state);
312 free_extent_state(other);
313 }
314 }
315 other_node = rb_next(&state->rb_node);
316 if (other_node) {
317 other = rb_entry(other_node, struct extent_state, rb_node);
318 if (other->start == state->end + 1 &&
319 other->state == state->state) {
320 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500321 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500322 rb_erase(&state->rb_node, &tree->state);
323 free_extent_state(state);
324 }
325 }
326 return 0;
327}
328
Chris Mason291d6732008-01-29 15:55:23 -0500329static void set_state_cb(struct extent_io_tree *tree,
330 struct extent_state *state,
331 unsigned long bits)
332{
333 if (tree->ops && tree->ops->set_bit_hook) {
334 tree->ops->set_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500335 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500336 }
337}
338
339static void clear_state_cb(struct extent_io_tree *tree,
340 struct extent_state *state,
341 unsigned long bits)
342{
343 if (tree->ops && tree->ops->set_bit_hook) {
344 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500345 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500346 }
347}
348
Chris Masond1310b22008-01-24 16:13:08 -0500349/*
350 * insert an extent_state struct into the tree. 'bits' are set on the
351 * struct before it is inserted.
352 *
353 * This may return -EEXIST if the extent is already there, in which case the
354 * state struct is freed.
355 *
356 * The tree lock is not taken internally. This is a utility function and
357 * probably isn't what you want to call (see set/clear_extent_bit).
358 */
359static int insert_state(struct extent_io_tree *tree,
360 struct extent_state *state, u64 start, u64 end,
361 int bits)
362{
363 struct rb_node *node;
364
365 if (end < start) {
366 printk("end < start %Lu %Lu\n", end, start);
367 WARN_ON(1);
368 }
369 if (bits & EXTENT_DIRTY)
370 tree->dirty_bytes += end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500371 set_state_cb(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500372 state->state |= bits;
373 state->start = start;
374 state->end = end;
375 node = tree_insert(&tree->state, end, &state->rb_node);
376 if (node) {
377 struct extent_state *found;
378 found = rb_entry(node, struct extent_state, rb_node);
379 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
380 free_extent_state(state);
381 return -EEXIST;
382 }
Chris Mason70dec802008-01-29 09:59:12 -0500383 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500384 merge_state(tree, state);
385 return 0;
386}
387
388/*
389 * split a given extent state struct in two, inserting the preallocated
390 * struct 'prealloc' as the newly created second half. 'split' indicates an
391 * offset inside 'orig' where it should be split.
392 *
393 * Before calling,
394 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
395 * are two extent state structs in the tree:
396 * prealloc: [orig->start, split - 1]
397 * orig: [ split, orig->end ]
398 *
399 * The tree locks are not taken by this function. They need to be held
400 * by the caller.
401 */
402static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
403 struct extent_state *prealloc, u64 split)
404{
405 struct rb_node *node;
406 prealloc->start = orig->start;
407 prealloc->end = split - 1;
408 prealloc->state = orig->state;
409 orig->start = split;
410
411 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
412 if (node) {
413 struct extent_state *found;
414 found = rb_entry(node, struct extent_state, rb_node);
415 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
416 free_extent_state(prealloc);
417 return -EEXIST;
418 }
Chris Mason70dec802008-01-29 09:59:12 -0500419 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500420 return 0;
421}
422
423/*
424 * utility function to clear some bits in an extent state struct.
425 * it will optionally wake up any one waiting on this state (wake == 1), or
426 * forcibly remove the state from the tree (delete == 1).
427 *
428 * If no bits are set on the state struct after clearing things, the
429 * struct is freed and removed from the tree
430 */
431static int clear_state_bit(struct extent_io_tree *tree,
432 struct extent_state *state, int bits, int wake,
433 int delete)
434{
435 int ret = state->state & bits;
436
437 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
438 u64 range = state->end - state->start + 1;
439 WARN_ON(range > tree->dirty_bytes);
440 tree->dirty_bytes -= range;
441 }
Chris Mason291d6732008-01-29 15:55:23 -0500442 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500443 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500444 if (wake)
445 wake_up(&state->wq);
446 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500447 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500448 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500449 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500450 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500451 free_extent_state(state);
452 } else {
453 WARN_ON(1);
454 }
455 } else {
456 merge_state(tree, state);
457 }
458 return ret;
459}
460
461/*
462 * clear some bits on a range in the tree. This may require splitting
463 * or inserting elements in the tree, so the gfp mask is used to
464 * indicate which allocations or sleeping are allowed.
465 *
466 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
467 * the given range from the tree regardless of state (ie for truncate).
468 *
469 * the range [start, end] is inclusive.
470 *
471 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
472 * bits were already set, or zero if none of the bits were already set.
473 */
474int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
475 int bits, int wake, int delete, gfp_t mask)
476{
477 struct extent_state *state;
478 struct extent_state *prealloc = NULL;
479 struct rb_node *node;
480 unsigned long flags;
481 int err;
482 int set = 0;
483
484again:
485 if (!prealloc && (mask & __GFP_WAIT)) {
486 prealloc = alloc_extent_state(mask);
487 if (!prealloc)
488 return -ENOMEM;
489 }
490
Chris Mason70dec802008-01-29 09:59:12 -0500491 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500492 /*
493 * this search will find the extents that end after
494 * our range starts
495 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500496 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500497 if (!node)
498 goto out;
499 state = rb_entry(node, struct extent_state, rb_node);
500 if (state->start > end)
501 goto out;
502 WARN_ON(state->end < start);
503
504 /*
505 * | ---- desired range ---- |
506 * | state | or
507 * | ------------- state -------------- |
508 *
509 * We need to split the extent we found, and may flip
510 * bits on second half.
511 *
512 * If the extent we found extends past our range, we
513 * just split and search again. It'll get split again
514 * the next time though.
515 *
516 * If the extent we found is inside our range, we clear
517 * the desired bit on it.
518 */
519
520 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500521 if (!prealloc)
522 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500523 err = split_state(tree, state, prealloc, start);
524 BUG_ON(err == -EEXIST);
525 prealloc = NULL;
526 if (err)
527 goto out;
528 if (state->end <= end) {
529 start = state->end + 1;
530 set |= clear_state_bit(tree, state, bits,
531 wake, delete);
532 } else {
533 start = state->start;
534 }
535 goto search_again;
536 }
537 /*
538 * | ---- desired range ---- |
539 * | state |
540 * We need to split the extent, and clear the bit
541 * on the first half
542 */
543 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500544 if (!prealloc)
545 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500546 err = split_state(tree, state, prealloc, end + 1);
547 BUG_ON(err == -EEXIST);
548
549 if (wake)
550 wake_up(&state->wq);
551 set |= clear_state_bit(tree, prealloc, bits,
552 wake, delete);
553 prealloc = NULL;
554 goto out;
555 }
556
557 start = state->end + 1;
558 set |= clear_state_bit(tree, state, bits, wake, delete);
559 goto search_again;
560
561out:
Chris Mason70dec802008-01-29 09:59:12 -0500562 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500563 if (prealloc)
564 free_extent_state(prealloc);
565
566 return set;
567
568search_again:
569 if (start > end)
570 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500571 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500572 if (mask & __GFP_WAIT)
573 cond_resched();
574 goto again;
575}
576EXPORT_SYMBOL(clear_extent_bit);
577
578static int wait_on_state(struct extent_io_tree *tree,
579 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500580 __releases(tree->lock)
581 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500582{
583 DEFINE_WAIT(wait);
584 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Mason70dec802008-01-29 09:59:12 -0500585 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500586 schedule();
Chris Mason70dec802008-01-29 09:59:12 -0500587 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500588 finish_wait(&state->wq, &wait);
589 return 0;
590}
591
592/*
593 * waits for one or more bits to clear on a range in the state tree.
594 * The range [start, end] is inclusive.
595 * The tree lock is taken by this function
596 */
597int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
598{
599 struct extent_state *state;
600 struct rb_node *node;
601
Chris Mason70dec802008-01-29 09:59:12 -0500602 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500603again:
604 while (1) {
605 /*
606 * this search will find all the extents that end after
607 * our range starts
608 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500609 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500610 if (!node)
611 break;
612
613 state = rb_entry(node, struct extent_state, rb_node);
614
615 if (state->start > end)
616 goto out;
617
618 if (state->state & bits) {
619 start = state->start;
620 atomic_inc(&state->refs);
621 wait_on_state(tree, state);
622 free_extent_state(state);
623 goto again;
624 }
625 start = state->end + 1;
626
627 if (start > end)
628 break;
629
630 if (need_resched()) {
Chris Mason70dec802008-01-29 09:59:12 -0500631 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500632 cond_resched();
Chris Mason70dec802008-01-29 09:59:12 -0500633 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500634 }
635 }
636out:
Chris Mason70dec802008-01-29 09:59:12 -0500637 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500638 return 0;
639}
640EXPORT_SYMBOL(wait_extent_bit);
641
642static void set_state_bits(struct extent_io_tree *tree,
643 struct extent_state *state,
644 int bits)
645{
646 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
647 u64 range = state->end - state->start + 1;
648 tree->dirty_bytes += range;
649 }
Chris Mason291d6732008-01-29 15:55:23 -0500650 set_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500651 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500652}
653
654/*
655 * set some bits on a range in the tree. This may require allocations
656 * or sleeping, so the gfp mask is used to indicate what is allowed.
657 *
658 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
659 * range already has the desired bits set. The start of the existing
660 * range is returned in failed_start in this case.
661 *
662 * [start, end] is inclusive
663 * This takes the tree lock.
664 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500665static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
Chris Masond1310b22008-01-24 16:13:08 -0500666 int exclusive, u64 *failed_start, gfp_t mask)
667{
668 struct extent_state *state;
669 struct extent_state *prealloc = NULL;
670 struct rb_node *node;
671 unsigned long flags;
672 int err = 0;
673 int set;
674 u64 last_start;
675 u64 last_end;
676again:
677 if (!prealloc && (mask & __GFP_WAIT)) {
678 prealloc = alloc_extent_state(mask);
679 if (!prealloc)
680 return -ENOMEM;
681 }
682
Chris Mason70dec802008-01-29 09:59:12 -0500683 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500684 /*
685 * this search will find all the extents that end after
686 * our range starts.
687 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500688 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500689 if (!node) {
690 err = insert_state(tree, prealloc, start, end, bits);
691 prealloc = NULL;
692 BUG_ON(err == -EEXIST);
693 goto out;
694 }
695
696 state = rb_entry(node, struct extent_state, rb_node);
697 last_start = state->start;
698 last_end = state->end;
699
700 /*
701 * | ---- desired range ---- |
702 * | state |
703 *
704 * Just lock what we found and keep going
705 */
706 if (state->start == start && state->end <= end) {
707 set = state->state & bits;
708 if (set && exclusive) {
709 *failed_start = state->start;
710 err = -EEXIST;
711 goto out;
712 }
713 set_state_bits(tree, state, bits);
714 start = state->end + 1;
715 merge_state(tree, state);
716 goto search_again;
717 }
718
719 /*
720 * | ---- desired range ---- |
721 * | state |
722 * or
723 * | ------------- state -------------- |
724 *
725 * We need to split the extent we found, and may flip bits on
726 * second half.
727 *
728 * If the extent we found extends past our
729 * range, we just split and search again. It'll get split
730 * again the next time though.
731 *
732 * If the extent we found is inside our range, we set the
733 * desired bit on it.
734 */
735 if (state->start < start) {
736 set = state->state & bits;
737 if (exclusive && set) {
738 *failed_start = start;
739 err = -EEXIST;
740 goto out;
741 }
742 err = split_state(tree, state, prealloc, start);
743 BUG_ON(err == -EEXIST);
744 prealloc = NULL;
745 if (err)
746 goto out;
747 if (state->end <= end) {
748 set_state_bits(tree, state, bits);
749 start = state->end + 1;
750 merge_state(tree, state);
751 } else {
752 start = state->start;
753 }
754 goto search_again;
755 }
756 /*
757 * | ---- desired range ---- |
758 * | state | or | state |
759 *
760 * There's a hole, we need to insert something in it and
761 * ignore the extent we found.
762 */
763 if (state->start > start) {
764 u64 this_end;
765 if (end < last_start)
766 this_end = end;
767 else
768 this_end = last_start -1;
769 err = insert_state(tree, prealloc, start, this_end,
770 bits);
771 prealloc = NULL;
772 BUG_ON(err == -EEXIST);
773 if (err)
774 goto out;
775 start = this_end + 1;
776 goto search_again;
777 }
778 /*
779 * | ---- desired range ---- |
780 * | state |
781 * We need to split the extent, and set the bit
782 * on the first half
783 */
784 if (state->start <= end && state->end > end) {
785 set = state->state & bits;
786 if (exclusive && set) {
787 *failed_start = start;
788 err = -EEXIST;
789 goto out;
790 }
791 err = split_state(tree, state, prealloc, end + 1);
792 BUG_ON(err == -EEXIST);
793
794 set_state_bits(tree, prealloc, bits);
795 merge_state(tree, prealloc);
796 prealloc = NULL;
797 goto out;
798 }
799
800 goto search_again;
801
802out:
Chris Mason70dec802008-01-29 09:59:12 -0500803 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500804 if (prealloc)
805 free_extent_state(prealloc);
806
807 return err;
808
809search_again:
810 if (start > end)
811 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500812 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500813 if (mask & __GFP_WAIT)
814 cond_resched();
815 goto again;
816}
817EXPORT_SYMBOL(set_extent_bit);
818
819/* wrappers around set/clear extent bit */
820int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
821 gfp_t mask)
822{
823 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
824 mask);
825}
826EXPORT_SYMBOL(set_extent_dirty);
827
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
829 gfp_t mask)
830{
831 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
832}
833EXPORT_SYMBOL(set_extent_ordered);
834
Chris Masond1310b22008-01-24 16:13:08 -0500835int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
836 int bits, gfp_t mask)
837{
838 return set_extent_bit(tree, start, end, bits, 0, NULL,
839 mask);
840}
841EXPORT_SYMBOL(set_extent_bits);
842
843int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
844 int bits, gfp_t mask)
845{
846 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
847}
848EXPORT_SYMBOL(clear_extent_bits);
849
850int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
851 gfp_t mask)
852{
853 return set_extent_bit(tree, start, end,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400854 EXTENT_DELALLOC | EXTENT_DIRTY,
855 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500856}
857EXPORT_SYMBOL(set_extent_delalloc);
858
859int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
860 gfp_t mask)
861{
862 return clear_extent_bit(tree, start, end,
863 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
864}
865EXPORT_SYMBOL(clear_extent_dirty);
866
Chris Masone6dcd2d2008-07-17 12:53:50 -0400867int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
868 gfp_t mask)
869{
870 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
871}
872EXPORT_SYMBOL(clear_extent_ordered);
873
Chris Masond1310b22008-01-24 16:13:08 -0500874int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
875 gfp_t mask)
876{
877 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
878 mask);
879}
880EXPORT_SYMBOL(set_extent_new);
881
Christoph Hellwigb2950862008-12-02 09:54:17 -0500882static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500883 gfp_t mask)
884{
885 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
886}
Chris Masond1310b22008-01-24 16:13:08 -0500887
888int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
889 gfp_t mask)
890{
891 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
892 mask);
893}
894EXPORT_SYMBOL(set_extent_uptodate);
895
Christoph Hellwigb2950862008-12-02 09:54:17 -0500896static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500897 gfp_t mask)
898{
899 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
900}
Chris Masond1310b22008-01-24 16:13:08 -0500901
Christoph Hellwigb2950862008-12-02 09:54:17 -0500902static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500903 gfp_t mask)
904{
905 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
906 0, NULL, mask);
907}
Chris Masond1310b22008-01-24 16:13:08 -0500908
Christoph Hellwigb2950862008-12-02 09:54:17 -0500909static int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500910 gfp_t mask)
911{
912 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
913}
Chris Masond1310b22008-01-24 16:13:08 -0500914
915int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
916{
917 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
918}
919EXPORT_SYMBOL(wait_on_extent_writeback);
920
Chris Masond352ac62008-09-29 15:18:18 -0400921/*
922 * either insert or lock state struct between start and end use mask to tell
923 * us if waiting is desired.
924 */
Chris Masond1310b22008-01-24 16:13:08 -0500925int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
926{
927 int err;
928 u64 failed_start;
929 while (1) {
930 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
931 &failed_start, mask);
932 if (err == -EEXIST && (mask & __GFP_WAIT)) {
933 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
934 start = failed_start;
935 } else {
936 break;
937 }
938 WARN_ON(start > end);
939 }
940 return err;
941}
942EXPORT_SYMBOL(lock_extent);
943
Josef Bacik25179202008-10-29 14:49:05 -0400944int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
945 gfp_t mask)
946{
947 int err;
948 u64 failed_start;
949
950 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
951 &failed_start, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400952 if (err == -EEXIST) {
953 if (failed_start > start)
954 clear_extent_bit(tree, start, failed_start - 1,
955 EXTENT_LOCKED, 1, 0, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400956 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400957 }
Josef Bacik25179202008-10-29 14:49:05 -0400958 return 1;
959}
960EXPORT_SYMBOL(try_lock_extent);
961
Chris Masond1310b22008-01-24 16:13:08 -0500962int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
963 gfp_t mask)
964{
965 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
966}
967EXPORT_SYMBOL(unlock_extent);
968
969/*
970 * helper function to set pages and extents in the tree dirty
971 */
972int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
973{
974 unsigned long index = start >> PAGE_CACHE_SHIFT;
975 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
976 struct page *page;
977
978 while (index <= end_index) {
979 page = find_get_page(tree->mapping, index);
980 BUG_ON(!page);
981 __set_page_dirty_nobuffers(page);
982 page_cache_release(page);
983 index++;
984 }
985 set_extent_dirty(tree, start, end, GFP_NOFS);
986 return 0;
987}
988EXPORT_SYMBOL(set_range_dirty);
989
990/*
991 * helper function to set both pages and extents in the tree writeback
992 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500993static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -0500994{
995 unsigned long index = start >> PAGE_CACHE_SHIFT;
996 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
997 struct page *page;
998
999 while (index <= end_index) {
1000 page = find_get_page(tree->mapping, index);
1001 BUG_ON(!page);
1002 set_page_writeback(page);
1003 page_cache_release(page);
1004 index++;
1005 }
1006 set_extent_writeback(tree, start, end, GFP_NOFS);
1007 return 0;
1008}
Chris Masond1310b22008-01-24 16:13:08 -05001009
Chris Masond352ac62008-09-29 15:18:18 -04001010/*
1011 * find the first offset in the io tree with 'bits' set. zero is
1012 * returned if we find something, and *start_ret and *end_ret are
1013 * set to reflect the state struct that was found.
1014 *
1015 * If nothing was found, 1 is returned, < 0 on error
1016 */
Chris Masond1310b22008-01-24 16:13:08 -05001017int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1018 u64 *start_ret, u64 *end_ret, int bits)
1019{
1020 struct rb_node *node;
1021 struct extent_state *state;
1022 int ret = 1;
1023
Chris Mason70dec802008-01-29 09:59:12 -05001024 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001025 /*
1026 * this search will find all the extents that end after
1027 * our range starts.
1028 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001029 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001030 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001031 goto out;
1032 }
1033
1034 while(1) {
1035 state = rb_entry(node, struct extent_state, rb_node);
1036 if (state->end >= start && (state->state & bits)) {
1037 *start_ret = state->start;
1038 *end_ret = state->end;
1039 ret = 0;
1040 break;
1041 }
1042 node = rb_next(node);
1043 if (!node)
1044 break;
1045 }
1046out:
Chris Mason70dec802008-01-29 09:59:12 -05001047 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001048 return ret;
1049}
1050EXPORT_SYMBOL(find_first_extent_bit);
1051
Chris Masond352ac62008-09-29 15:18:18 -04001052/* find the first state struct with 'bits' set after 'start', and
1053 * return it. tree->lock must be held. NULL will returned if
1054 * nothing was found after 'start'
1055 */
Chris Masond7fc6402008-02-18 12:12:38 -05001056struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1057 u64 start, int bits)
1058{
1059 struct rb_node *node;
1060 struct extent_state *state;
1061
1062 /*
1063 * this search will find all the extents that end after
1064 * our range starts.
1065 */
1066 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001067 if (!node) {
Chris Masond7fc6402008-02-18 12:12:38 -05001068 goto out;
1069 }
1070
1071 while(1) {
1072 state = rb_entry(node, struct extent_state, rb_node);
1073 if (state->end >= start && (state->state & bits)) {
1074 return state;
1075 }
1076 node = rb_next(node);
1077 if (!node)
1078 break;
1079 }
1080out:
1081 return NULL;
1082}
1083EXPORT_SYMBOL(find_first_extent_bit_state);
1084
Chris Masond352ac62008-09-29 15:18:18 -04001085/*
1086 * find a contiguous range of bytes in the file marked as delalloc, not
1087 * more than 'max_bytes'. start and end are used to return the range,
1088 *
1089 * 1 is returned if we find something, 0 if nothing was in the tree
1090 */
Chris Masonc8b97812008-10-29 14:49:59 -04001091static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1092 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001093{
1094 struct rb_node *node;
1095 struct extent_state *state;
1096 u64 cur_start = *start;
1097 u64 found = 0;
1098 u64 total_bytes = 0;
1099
Chris Mason70dec802008-01-29 09:59:12 -05001100 spin_lock_irq(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001101
Chris Masond1310b22008-01-24 16:13:08 -05001102 /*
1103 * this search will find all the extents that end after
1104 * our range starts.
1105 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001106 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001107 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001108 if (!found)
1109 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001110 goto out;
1111 }
1112
1113 while(1) {
1114 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001115 if (found && (state->start != cur_start ||
1116 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001117 goto out;
1118 }
1119 if (!(state->state & EXTENT_DELALLOC)) {
1120 if (!found)
1121 *end = state->end;
1122 goto out;
1123 }
Chris Masond1310b22008-01-24 16:13:08 -05001124 if (!found)
1125 *start = state->start;
1126 found++;
1127 *end = state->end;
1128 cur_start = state->end + 1;
1129 node = rb_next(node);
1130 if (!node)
1131 break;
1132 total_bytes += state->end - state->start + 1;
1133 if (total_bytes >= max_bytes)
1134 break;
1135 }
1136out:
Chris Mason70dec802008-01-29 09:59:12 -05001137 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001138 return found;
1139}
1140
Chris Masonc8b97812008-10-29 14:49:59 -04001141static noinline int __unlock_for_delalloc(struct inode *inode,
1142 struct page *locked_page,
1143 u64 start, u64 end)
1144{
1145 int ret;
1146 struct page *pages[16];
1147 unsigned long index = start >> PAGE_CACHE_SHIFT;
1148 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1149 unsigned long nr_pages = end_index - index + 1;
1150 int i;
1151
1152 if (index == locked_page->index && end_index == index)
1153 return 0;
1154
1155 while(nr_pages > 0) {
1156 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001157 min_t(unsigned long, nr_pages,
1158 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001159 for (i = 0; i < ret; i++) {
1160 if (pages[i] != locked_page)
1161 unlock_page(pages[i]);
1162 page_cache_release(pages[i]);
1163 }
1164 nr_pages -= ret;
1165 index += ret;
1166 cond_resched();
1167 }
1168 return 0;
1169}
1170
1171static noinline int lock_delalloc_pages(struct inode *inode,
1172 struct page *locked_page,
1173 u64 delalloc_start,
1174 u64 delalloc_end)
1175{
1176 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1177 unsigned long start_index = index;
1178 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1179 unsigned long pages_locked = 0;
1180 struct page *pages[16];
1181 unsigned long nrpages;
1182 int ret;
1183 int i;
1184
1185 /* the caller is responsible for locking the start index */
1186 if (index == locked_page->index && index == end_index)
1187 return 0;
1188
1189 /* skip the page at the start index */
1190 nrpages = end_index - index + 1;
1191 while(nrpages > 0) {
1192 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001193 min_t(unsigned long,
1194 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001195 if (ret == 0) {
1196 ret = -EAGAIN;
1197 goto done;
1198 }
1199 /* now we have an array of pages, lock them all */
1200 for (i = 0; i < ret; i++) {
1201 /*
1202 * the caller is taking responsibility for
1203 * locked_page
1204 */
Chris Mason771ed682008-11-06 22:02:51 -05001205 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001206 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001207 if (!PageDirty(pages[i]) ||
1208 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001209 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,
Chris Mason5b050f02008-11-11 09:34:41 -05001348 min_t(unsigned long,
1349 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001350 for (i = 0; i < ret; i++) {
1351 if (pages[i] == locked_page) {
1352 page_cache_release(pages[i]);
1353 continue;
1354 }
1355 if (clear_dirty)
1356 clear_page_dirty_for_io(pages[i]);
1357 if (set_writeback)
1358 set_page_writeback(pages[i]);
1359 if (end_writeback)
1360 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001361 if (unlock_pages)
1362 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001363 page_cache_release(pages[i]);
1364 }
1365 nr_pages -= ret;
1366 index += ret;
1367 cond_resched();
1368 }
1369 return 0;
1370}
1371EXPORT_SYMBOL(extent_clear_unlock_delalloc);
1372
Chris Masond352ac62008-09-29 15:18:18 -04001373/*
1374 * count the number of bytes in the tree that have a given bit(s)
1375 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1376 * cached. The total number found is returned.
1377 */
Chris Masond1310b22008-01-24 16:13:08 -05001378u64 count_range_bits(struct extent_io_tree *tree,
1379 u64 *start, u64 search_end, u64 max_bytes,
1380 unsigned long bits)
1381{
1382 struct rb_node *node;
1383 struct extent_state *state;
1384 u64 cur_start = *start;
1385 u64 total_bytes = 0;
1386 int found = 0;
1387
1388 if (search_end <= cur_start) {
1389 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1390 WARN_ON(1);
1391 return 0;
1392 }
1393
Chris Mason70dec802008-01-29 09:59:12 -05001394 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001395 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1396 total_bytes = tree->dirty_bytes;
1397 goto out;
1398 }
1399 /*
1400 * this search will find all the extents that end after
1401 * our range starts.
1402 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001403 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001404 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001405 goto out;
1406 }
1407
1408 while(1) {
1409 state = rb_entry(node, struct extent_state, rb_node);
1410 if (state->start > search_end)
1411 break;
1412 if (state->end >= cur_start && (state->state & bits)) {
1413 total_bytes += min(search_end, state->end) + 1 -
1414 max(cur_start, state->start);
1415 if (total_bytes >= max_bytes)
1416 break;
1417 if (!found) {
1418 *start = state->start;
1419 found = 1;
1420 }
1421 }
1422 node = rb_next(node);
1423 if (!node)
1424 break;
1425 }
1426out:
Chris Mason70dec802008-01-29 09:59:12 -05001427 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001428 return total_bytes;
1429}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001430
1431#if 0
Chris Masond1310b22008-01-24 16:13:08 -05001432/*
1433 * helper function to lock both pages and extents in the tree.
1434 * pages must be locked first.
1435 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001436static int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001437{
1438 unsigned long index = start >> PAGE_CACHE_SHIFT;
1439 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1440 struct page *page;
1441 int err;
1442
1443 while (index <= end_index) {
1444 page = grab_cache_page(tree->mapping, index);
1445 if (!page) {
1446 err = -ENOMEM;
1447 goto failed;
1448 }
1449 if (IS_ERR(page)) {
1450 err = PTR_ERR(page);
1451 goto failed;
1452 }
1453 index++;
1454 }
1455 lock_extent(tree, start, end, GFP_NOFS);
1456 return 0;
1457
1458failed:
1459 /*
1460 * we failed above in getting the page at 'index', so we undo here
1461 * up to but not including the page at 'index'
1462 */
1463 end_index = index;
1464 index = start >> PAGE_CACHE_SHIFT;
1465 while (index < end_index) {
1466 page = find_get_page(tree->mapping, index);
1467 unlock_page(page);
1468 page_cache_release(page);
1469 index++;
1470 }
1471 return err;
1472}
Chris Masond1310b22008-01-24 16:13:08 -05001473
1474/*
1475 * helper function to unlock both pages and extents in the tree.
1476 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001477static int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001478{
1479 unsigned long index = start >> PAGE_CACHE_SHIFT;
1480 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1481 struct page *page;
1482
1483 while (index <= end_index) {
1484 page = find_get_page(tree->mapping, index);
1485 unlock_page(page);
1486 page_cache_release(page);
1487 index++;
1488 }
1489 unlock_extent(tree, start, end, GFP_NOFS);
1490 return 0;
1491}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001492#endif
Chris Masond1310b22008-01-24 16:13:08 -05001493
Chris Masond352ac62008-09-29 15:18:18 -04001494/*
1495 * set the private field for a given byte offset in the tree. If there isn't
1496 * an extent_state there already, this does nothing.
1497 */
Chris Masond1310b22008-01-24 16:13:08 -05001498int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1499{
1500 struct rb_node *node;
1501 struct extent_state *state;
1502 int ret = 0;
1503
Chris Mason70dec802008-01-29 09:59:12 -05001504 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001505 /*
1506 * this search will find all the extents that end after
1507 * our range starts.
1508 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001509 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001510 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001511 ret = -ENOENT;
1512 goto out;
1513 }
1514 state = rb_entry(node, struct extent_state, rb_node);
1515 if (state->start != start) {
1516 ret = -ENOENT;
1517 goto out;
1518 }
1519 state->private = private;
1520out:
Chris Mason70dec802008-01-29 09:59:12 -05001521 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001522 return ret;
1523}
1524
1525int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1526{
1527 struct rb_node *node;
1528 struct extent_state *state;
1529 int ret = 0;
1530
Chris Mason70dec802008-01-29 09:59:12 -05001531 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001532 /*
1533 * this search will find all the extents that end after
1534 * our range starts.
1535 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001536 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001537 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001538 ret = -ENOENT;
1539 goto out;
1540 }
1541 state = rb_entry(node, struct extent_state, rb_node);
1542 if (state->start != start) {
1543 ret = -ENOENT;
1544 goto out;
1545 }
1546 *private = state->private;
1547out:
Chris Mason70dec802008-01-29 09:59:12 -05001548 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001549 return ret;
1550}
1551
1552/*
1553 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001554 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001555 * has the bits set. Otherwise, 1 is returned if any bit in the
1556 * range is found set.
1557 */
1558int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1559 int bits, int filled)
1560{
1561 struct extent_state *state = NULL;
1562 struct rb_node *node;
1563 int bitset = 0;
1564 unsigned long flags;
1565
Chris Mason70dec802008-01-29 09:59:12 -05001566 spin_lock_irqsave(&tree->lock, flags);
Chris Mason80ea96b2008-02-01 14:51:59 -05001567 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001568 while (node && start <= end) {
1569 state = rb_entry(node, struct extent_state, rb_node);
1570
1571 if (filled && state->start > start) {
1572 bitset = 0;
1573 break;
1574 }
1575
1576 if (state->start > end)
1577 break;
1578
1579 if (state->state & bits) {
1580 bitset = 1;
1581 if (!filled)
1582 break;
1583 } else if (filled) {
1584 bitset = 0;
1585 break;
1586 }
1587 start = state->end + 1;
1588 if (start > end)
1589 break;
1590 node = rb_next(node);
1591 if (!node) {
1592 if (filled)
1593 bitset = 0;
1594 break;
1595 }
1596 }
Chris Mason70dec802008-01-29 09:59:12 -05001597 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05001598 return bitset;
1599}
1600EXPORT_SYMBOL(test_range_bit);
1601
1602/*
1603 * helper function to set a given page up to date if all the
1604 * extents in the tree for that page are up to date
1605 */
1606static int check_page_uptodate(struct extent_io_tree *tree,
1607 struct page *page)
1608{
1609 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1610 u64 end = start + PAGE_CACHE_SIZE - 1;
1611 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1612 SetPageUptodate(page);
1613 return 0;
1614}
1615
1616/*
1617 * helper function to unlock a page if all the extents in the tree
1618 * for that page are unlocked
1619 */
1620static int check_page_locked(struct extent_io_tree *tree,
1621 struct page *page)
1622{
1623 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1624 u64 end = start + PAGE_CACHE_SIZE - 1;
1625 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1626 unlock_page(page);
1627 return 0;
1628}
1629
1630/*
1631 * helper function to end page writeback if all the extents
1632 * in the tree for that page are done with writeback
1633 */
1634static int check_page_writeback(struct extent_io_tree *tree,
1635 struct page *page)
1636{
1637 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1638 u64 end = start + PAGE_CACHE_SIZE - 1;
1639 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1640 end_page_writeback(page);
1641 return 0;
1642}
1643
1644/* lots and lots of room for performance fixes in the end_bio funcs */
1645
1646/*
1647 * after a writepage IO is done, we need to:
1648 * clear the uptodate bits on error
1649 * clear the writeback bits in the extent tree for this IO
1650 * end_page_writeback if the page has no more pending IO
1651 *
1652 * Scheduling is not allowed, so the extent state tree is expected
1653 * to have one and only one object corresponding to this IO.
1654 */
Chris Masond1310b22008-01-24 16:13:08 -05001655static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001656{
Chris Mason1259ab72008-05-12 13:39:03 -04001657 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001658 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001659 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001660 u64 start;
1661 u64 end;
1662 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001663 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001664
Chris Masond1310b22008-01-24 16:13:08 -05001665 do {
1666 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001667 tree = &BTRFS_I(page->mapping->host)->io_tree;
1668
Chris Masond1310b22008-01-24 16:13:08 -05001669 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1670 bvec->bv_offset;
1671 end = start + bvec->bv_len - 1;
1672
1673 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1674 whole_page = 1;
1675 else
1676 whole_page = 0;
1677
1678 if (--bvec >= bio->bi_io_vec)
1679 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001680 if (tree->ops && tree->ops->writepage_end_io_hook) {
1681 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001682 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001683 if (ret)
1684 uptodate = 0;
1685 }
1686
1687 if (!uptodate && tree->ops &&
1688 tree->ops->writepage_io_failed_hook) {
1689 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001690 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001691 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001692 uptodate = (err == 0);
1693 continue;
1694 }
1695 }
1696
Chris Masond1310b22008-01-24 16:13:08 -05001697 if (!uptodate) {
1698 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1699 ClearPageUptodate(page);
1700 SetPageError(page);
1701 }
Chris Mason70dec802008-01-29 09:59:12 -05001702
David Woodhouse902b22f2008-08-20 08:51:49 -04001703 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001704
1705 if (whole_page)
1706 end_page_writeback(page);
1707 else
1708 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001709 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001710
Chris Masond1310b22008-01-24 16:13:08 -05001711 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001712}
1713
1714/*
1715 * after a readpage IO is done, we need to:
1716 * clear the uptodate bits on error
1717 * set the uptodate bits if things worked
1718 * set the page up to date if all extents in the tree are uptodate
1719 * clear the lock bit in the extent tree
1720 * unlock the page if there are no other extents locked for it
1721 *
1722 * Scheduling is not allowed, so the extent state tree is expected
1723 * to have one and only one object corresponding to this IO.
1724 */
Chris Masond1310b22008-01-24 16:13:08 -05001725static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001726{
1727 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1728 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001729 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001730 u64 start;
1731 u64 end;
1732 int whole_page;
1733 int ret;
1734
Chris Masond1310b22008-01-24 16:13:08 -05001735 do {
1736 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001737 tree = &BTRFS_I(page->mapping->host)->io_tree;
1738
Chris Masond1310b22008-01-24 16:13:08 -05001739 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1740 bvec->bv_offset;
1741 end = start + bvec->bv_len - 1;
1742
1743 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1744 whole_page = 1;
1745 else
1746 whole_page = 0;
1747
1748 if (--bvec >= bio->bi_io_vec)
1749 prefetchw(&bvec->bv_page->flags);
1750
1751 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001752 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001753 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001754 if (ret)
1755 uptodate = 0;
1756 }
Chris Mason7e383262008-04-09 16:28:12 -04001757 if (!uptodate && tree->ops &&
1758 tree->ops->readpage_io_failed_hook) {
1759 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001760 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001761 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001762 uptodate =
1763 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason7e383262008-04-09 16:28:12 -04001764 continue;
1765 }
1766 }
Chris Mason70dec802008-01-29 09:59:12 -05001767
Chris Mason771ed682008-11-06 22:02:51 -05001768 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001769 set_extent_uptodate(tree, start, end,
1770 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001771 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001772 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001773
Chris Mason70dec802008-01-29 09:59:12 -05001774 if (whole_page) {
1775 if (uptodate) {
1776 SetPageUptodate(page);
1777 } else {
1778 ClearPageUptodate(page);
1779 SetPageError(page);
1780 }
Chris Masond1310b22008-01-24 16:13:08 -05001781 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001782 } else {
1783 if (uptodate) {
1784 check_page_uptodate(tree, page);
1785 } else {
1786 ClearPageUptodate(page);
1787 SetPageError(page);
1788 }
Chris Masond1310b22008-01-24 16:13:08 -05001789 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001790 }
Chris Masond1310b22008-01-24 16:13:08 -05001791 } while (bvec >= bio->bi_io_vec);
1792
1793 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001794}
1795
1796/*
1797 * IO done from prepare_write is pretty simple, we just unlock
1798 * the structs in the extent tree when done, and set the uptodate bits
1799 * as appropriate.
1800 */
Chris Masond1310b22008-01-24 16:13:08 -05001801static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001802{
1803 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1804 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001805 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001806 u64 start;
1807 u64 end;
1808
Chris Masond1310b22008-01-24 16:13:08 -05001809 do {
1810 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001811 tree = &BTRFS_I(page->mapping->host)->io_tree;
1812
Chris Masond1310b22008-01-24 16:13:08 -05001813 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1814 bvec->bv_offset;
1815 end = start + bvec->bv_len - 1;
1816
1817 if (--bvec >= bio->bi_io_vec)
1818 prefetchw(&bvec->bv_page->flags);
1819
1820 if (uptodate) {
1821 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1822 } else {
1823 ClearPageUptodate(page);
1824 SetPageError(page);
1825 }
1826
1827 unlock_extent(tree, start, end, GFP_ATOMIC);
1828
1829 } while (bvec >= bio->bi_io_vec);
1830
1831 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001832}
1833
1834static struct bio *
1835extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1836 gfp_t gfp_flags)
1837{
1838 struct bio *bio;
1839
1840 bio = bio_alloc(gfp_flags, nr_vecs);
1841
1842 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1843 while (!bio && (nr_vecs /= 2))
1844 bio = bio_alloc(gfp_flags, nr_vecs);
1845 }
1846
1847 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001848 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001849 bio->bi_bdev = bdev;
1850 bio->bi_sector = first_sector;
1851 }
1852 return bio;
1853}
1854
Chris Masonc8b97812008-10-29 14:49:59 -04001855static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1856 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001857{
Chris Masond1310b22008-01-24 16:13:08 -05001858 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001859 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1860 struct page *page = bvec->bv_page;
1861 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001862 u64 start;
1863 u64 end;
1864
1865 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1866 end = start + bvec->bv_len - 1;
1867
David Woodhouse902b22f2008-08-20 08:51:49 -04001868 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001869
1870 bio_get(bio);
1871
Chris Mason065631f2008-02-20 12:07:25 -05001872 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001873 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001874 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001875 else
1876 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001877 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1878 ret = -EOPNOTSUPP;
1879 bio_put(bio);
1880 return ret;
1881}
1882
1883static int submit_extent_page(int rw, struct extent_io_tree *tree,
1884 struct page *page, sector_t sector,
1885 size_t size, unsigned long offset,
1886 struct block_device *bdev,
1887 struct bio **bio_ret,
1888 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001889 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001890 int mirror_num,
1891 unsigned long prev_bio_flags,
1892 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001893{
1894 int ret = 0;
1895 struct bio *bio;
1896 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001897 int contig = 0;
1898 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1899 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001900 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001901
1902 if (bio_ret && *bio_ret) {
1903 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001904 if (old_compressed)
1905 contig = bio->bi_sector == sector;
1906 else
1907 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1908 sector;
1909
1910 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001911 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001912 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1913 bio_flags)) ||
1914 bio_add_page(bio, page, page_size, offset) < page_size) {
1915 ret = submit_one_bio(rw, bio, mirror_num,
1916 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001917 bio = NULL;
1918 } else {
1919 return 0;
1920 }
1921 }
Chris Masonc8b97812008-10-29 14:49:59 -04001922 if (this_compressed)
1923 nr = BIO_MAX_PAGES;
1924 else
1925 nr = bio_get_nr_vecs(bdev);
1926
Chris Masond1310b22008-01-24 16:13:08 -05001927 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1928 if (!bio) {
1929 printk("failed to allocate bio nr %d\n", nr);
1930 }
Chris Mason70dec802008-01-29 09:59:12 -05001931
Chris Masonc8b97812008-10-29 14:49:59 -04001932 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001933 bio->bi_end_io = end_io_func;
1934 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001935
Chris Masond1310b22008-01-24 16:13:08 -05001936 if (bio_ret) {
1937 *bio_ret = bio;
1938 } else {
Chris Masonc8b97812008-10-29 14:49:59 -04001939 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001940 }
1941
1942 return ret;
1943}
1944
1945void set_page_extent_mapped(struct page *page)
1946{
1947 if (!PagePrivate(page)) {
1948 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001949 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001950 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001951 }
1952}
Chris Mason771ed682008-11-06 22:02:51 -05001953EXPORT_SYMBOL(set_page_extent_mapped);
Chris Masond1310b22008-01-24 16:13:08 -05001954
Christoph Hellwigb2950862008-12-02 09:54:17 -05001955static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001956{
1957 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1958}
1959
1960/*
1961 * basic readpage implementation. Locked extent state structs are inserted
1962 * into the tree that are removed when the IO is done (by the end_io
1963 * handlers)
1964 */
1965static int __extent_read_full_page(struct extent_io_tree *tree,
1966 struct page *page,
1967 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001968 struct bio **bio, int mirror_num,
1969 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001970{
1971 struct inode *inode = page->mapping->host;
1972 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1973 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1974 u64 end;
1975 u64 cur = start;
1976 u64 extent_offset;
1977 u64 last_byte = i_size_read(inode);
1978 u64 block_start;
1979 u64 cur_end;
1980 sector_t sector;
1981 struct extent_map *em;
1982 struct block_device *bdev;
1983 int ret;
1984 int nr = 0;
1985 size_t page_offset = 0;
1986 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001987 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001988 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001989 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001990
1991 set_page_extent_mapped(page);
1992
1993 end = page_end;
1994 lock_extent(tree, start, end, GFP_NOFS);
1995
Chris Masonc8b97812008-10-29 14:49:59 -04001996 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1997 char *userpage;
1998 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1999
2000 if (zero_offset) {
2001 iosize = PAGE_CACHE_SIZE - zero_offset;
2002 userpage = kmap_atomic(page, KM_USER0);
2003 memset(userpage + zero_offset, 0, iosize);
2004 flush_dcache_page(page);
2005 kunmap_atomic(userpage, KM_USER0);
2006 }
2007 }
Chris Masond1310b22008-01-24 16:13:08 -05002008 while (cur <= end) {
2009 if (cur >= last_byte) {
2010 char *userpage;
2011 iosize = PAGE_CACHE_SIZE - page_offset;
2012 userpage = kmap_atomic(page, KM_USER0);
2013 memset(userpage + page_offset, 0, iosize);
2014 flush_dcache_page(page);
2015 kunmap_atomic(userpage, KM_USER0);
2016 set_extent_uptodate(tree, cur, cur + iosize - 1,
2017 GFP_NOFS);
2018 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2019 break;
2020 }
2021 em = get_extent(inode, page, page_offset, cur,
2022 end - cur + 1, 0);
2023 if (IS_ERR(em) || !em) {
2024 SetPageError(page);
2025 unlock_extent(tree, cur, end, GFP_NOFS);
2026 break;
2027 }
Chris Masond1310b22008-01-24 16:13:08 -05002028 extent_offset = cur - em->start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002029 if (extent_map_end(em) <= cur) {
2030printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur);
2031 }
Chris Masond1310b22008-01-24 16:13:08 -05002032 BUG_ON(extent_map_end(em) <= cur);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002033 if (end < cur) {
2034printk("2bad mapping end %Lu cur %Lu\n", end, cur);
2035 }
Chris Masond1310b22008-01-24 16:13:08 -05002036 BUG_ON(end < cur);
2037
Chris Masonc8b97812008-10-29 14:49:59 -04002038 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2039 this_bio_flag = EXTENT_BIO_COMPRESSED;
2040
Chris Masond1310b22008-01-24 16:13:08 -05002041 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2042 cur_end = min(extent_map_end(em) - 1, end);
2043 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002044 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2045 disk_io_size = em->block_len;
2046 sector = em->block_start >> 9;
2047 } else {
2048 sector = (em->block_start + extent_offset) >> 9;
2049 disk_io_size = iosize;
2050 }
Chris Masond1310b22008-01-24 16:13:08 -05002051 bdev = em->bdev;
2052 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002053 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2054 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002055 free_extent_map(em);
2056 em = NULL;
2057
2058 /* we've found a hole, just zero and go on */
2059 if (block_start == EXTENT_MAP_HOLE) {
2060 char *userpage;
2061 userpage = kmap_atomic(page, KM_USER0);
2062 memset(userpage + page_offset, 0, iosize);
2063 flush_dcache_page(page);
2064 kunmap_atomic(userpage, KM_USER0);
2065
2066 set_extent_uptodate(tree, cur, cur + iosize - 1,
2067 GFP_NOFS);
2068 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2069 cur = cur + iosize;
2070 page_offset += iosize;
2071 continue;
2072 }
2073 /* the get_extent function already copied into the page */
2074 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002075 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002076 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2077 cur = cur + iosize;
2078 page_offset += iosize;
2079 continue;
2080 }
Chris Mason70dec802008-01-29 09:59:12 -05002081 /* we have an inline extent but it didn't get marked up
2082 * to date. Error out
2083 */
2084 if (block_start == EXTENT_MAP_INLINE) {
2085 SetPageError(page);
2086 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2087 cur = cur + iosize;
2088 page_offset += iosize;
2089 continue;
2090 }
Chris Masond1310b22008-01-24 16:13:08 -05002091
2092 ret = 0;
2093 if (tree->ops && tree->ops->readpage_io_hook) {
2094 ret = tree->ops->readpage_io_hook(page, cur,
2095 cur + iosize - 1);
2096 }
2097 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002098 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2099 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002100 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002101 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002102 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002103 end_bio_extent_readpage, mirror_num,
2104 *bio_flags,
2105 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002106 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002107 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002108 }
2109 if (ret)
2110 SetPageError(page);
2111 cur = cur + iosize;
2112 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002113 }
2114 if (!nr) {
2115 if (!PageError(page))
2116 SetPageUptodate(page);
2117 unlock_page(page);
2118 }
2119 return 0;
2120}
2121
2122int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2123 get_extent_t *get_extent)
2124{
2125 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002126 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002127 int ret;
2128
Chris Masonc8b97812008-10-29 14:49:59 -04002129 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2130 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002131 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002132 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002133 return ret;
2134}
2135EXPORT_SYMBOL(extent_read_full_page);
2136
2137/*
2138 * the writepage semantics are similar to regular writepage. extent
2139 * records are inserted to lock ranges in the tree, and as dirty areas
2140 * are found, they are marked writeback. Then the lock bits are removed
2141 * and the end_io handler clears the writeback ranges
2142 */
2143static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2144 void *data)
2145{
2146 struct inode *inode = page->mapping->host;
2147 struct extent_page_data *epd = data;
2148 struct extent_io_tree *tree = epd->tree;
2149 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2150 u64 delalloc_start;
2151 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2152 u64 end;
2153 u64 cur = start;
2154 u64 extent_offset;
2155 u64 last_byte = i_size_read(inode);
2156 u64 block_start;
2157 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002158 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002159 sector_t sector;
2160 struct extent_map *em;
2161 struct block_device *bdev;
2162 int ret;
2163 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002164 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002165 size_t blocksize;
2166 loff_t i_size = i_size_read(inode);
2167 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2168 u64 nr_delalloc;
2169 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002170 int page_started;
2171 int compressed;
Chris Mason771ed682008-11-06 22:02:51 -05002172 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002173
2174 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002175 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002176 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002177 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002178 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002179 unlock_page(page);
2180 return 0;
2181 }
2182
2183 if (page->index == end_index) {
2184 char *userpage;
2185
Chris Masond1310b22008-01-24 16:13:08 -05002186 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002187 memset(userpage + pg_offset, 0,
2188 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002189 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002190 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002191 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002192 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002193
2194 set_page_extent_mapped(page);
2195
2196 delalloc_start = start;
2197 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002198 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002199 if (!epd->extent_locked) {
2200 while(delalloc_end < page_end) {
2201 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002202 page,
2203 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002204 &delalloc_end,
2205 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002206 if (nr_delalloc == 0) {
2207 delalloc_start = delalloc_end + 1;
2208 continue;
2209 }
2210 tree->ops->fill_delalloc(inode, page, delalloc_start,
2211 delalloc_end, &page_started,
2212 &nr_written);
Chris Masond1310b22008-01-24 16:13:08 -05002213 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002214 }
Chris Masonc8b97812008-10-29 14:49:59 -04002215
Chris Mason771ed682008-11-06 22:02:51 -05002216 /* did the fill delalloc function already unlock and start
2217 * the IO?
2218 */
2219 if (page_started) {
2220 ret = 0;
2221 goto update_nr_written;
2222 }
Chris Masonc8b97812008-10-29 14:49:59 -04002223 }
Chris Masond1310b22008-01-24 16:13:08 -05002224 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002225
Chris Masone6dcd2d2008-07-17 12:53:50 -04002226 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002227
Chris Mason247e7432008-07-17 12:53:51 -04002228 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002229 ret = tree->ops->writepage_start_hook(page, start,
2230 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002231 if (ret == -EAGAIN) {
2232 unlock_extent(tree, start, page_end, GFP_NOFS);
2233 redirty_page_for_writepage(wbc, page);
2234 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002235 ret = 0;
2236 goto update_nr_written;
Chris Mason247e7432008-07-17 12:53:51 -04002237 }
2238 }
2239
Chris Mason771ed682008-11-06 22:02:51 -05002240 nr_written++;
2241
Chris Masond1310b22008-01-24 16:13:08 -05002242 end = page_end;
2243 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2244 printk("found delalloc bits after lock_extent\n");
2245 }
2246
2247 if (last_byte <= start) {
2248 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002249 unlock_extent(tree, start, page_end, GFP_NOFS);
2250 if (tree->ops && tree->ops->writepage_end_io_hook)
2251 tree->ops->writepage_end_io_hook(page, start,
2252 page_end, NULL, 1);
2253 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002254 goto done;
2255 }
2256
2257 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2258 blocksize = inode->i_sb->s_blocksize;
2259
2260 while (cur <= end) {
2261 if (cur >= last_byte) {
2262 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002263 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2264 if (tree->ops && tree->ops->writepage_end_io_hook)
2265 tree->ops->writepage_end_io_hook(page, cur,
2266 page_end, NULL, 1);
2267 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002268 break;
2269 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002270 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002271 end - cur + 1, 1);
2272 if (IS_ERR(em) || !em) {
2273 SetPageError(page);
2274 break;
2275 }
2276
2277 extent_offset = cur - em->start;
2278 BUG_ON(extent_map_end(em) <= cur);
2279 BUG_ON(end < cur);
2280 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2281 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2282 sector = (em->block_start + extent_offset) >> 9;
2283 bdev = em->bdev;
2284 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002285 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002286 free_extent_map(em);
2287 em = NULL;
2288
Chris Masonc8b97812008-10-29 14:49:59 -04002289 /*
2290 * compressed and inline extents are written through other
2291 * paths in the FS
2292 */
2293 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002294 block_start == EXTENT_MAP_INLINE) {
2295 clear_extent_dirty(tree, cur,
2296 cur + iosize - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002297
2298 unlock_extent(tree, unlock_start, cur + iosize -1,
2299 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002300
Chris Masonc8b97812008-10-29 14:49:59 -04002301 /*
2302 * end_io notification does not happen here for
2303 * compressed extents
2304 */
2305 if (!compressed && tree->ops &&
2306 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002307 tree->ops->writepage_end_io_hook(page, cur,
2308 cur + iosize - 1,
2309 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002310 else if (compressed) {
2311 /* we don't want to end_page_writeback on
2312 * a compressed extent. this happens
2313 * elsewhere
2314 */
2315 nr++;
2316 }
2317
2318 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002319 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002320 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002321 continue;
2322 }
Chris Masond1310b22008-01-24 16:13:08 -05002323 /* leave this out until we have a page_mkwrite call */
2324 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2325 EXTENT_DIRTY, 0)) {
2326 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002327 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002328 continue;
2329 }
Chris Masonc8b97812008-10-29 14:49:59 -04002330
Chris Masond1310b22008-01-24 16:13:08 -05002331 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2332 if (tree->ops && tree->ops->writepage_io_hook) {
2333 ret = tree->ops->writepage_io_hook(page, cur,
2334 cur + iosize - 1);
2335 } else {
2336 ret = 0;
2337 }
Chris Mason1259ab72008-05-12 13:39:03 -04002338 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002339 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002340 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002341 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002342
Chris Masond1310b22008-01-24 16:13:08 -05002343 set_range_writeback(tree, cur, cur + iosize - 1);
2344 if (!PageWriteback(page)) {
2345 printk("warning page %lu not writeback, "
2346 "cur %llu end %llu\n", page->index,
2347 (unsigned long long)cur,
2348 (unsigned long long)end);
2349 }
2350
2351 ret = submit_extent_page(WRITE, tree, page, sector,
Chris Mason7f3c74f2008-07-18 12:01:11 -04002352 iosize, pg_offset, bdev,
Chris Masond1310b22008-01-24 16:13:08 -05002353 &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002354 end_bio_extent_writepage,
2355 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002356 if (ret)
2357 SetPageError(page);
2358 }
2359 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002360 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002361 nr++;
2362 }
2363done:
2364 if (nr == 0) {
2365 /* make sure the mapping tag for page dirty gets cleared */
2366 set_page_writeback(page);
2367 end_page_writeback(page);
2368 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002369 if (unlock_start <= page_end)
2370 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002371 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002372
2373update_nr_written:
2374 wbc->nr_to_write -= nr_written;
2375 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2376 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2377 page->mapping->writeback_index = page->index + nr_written;
Chris Masond1310b22008-01-24 16:13:08 -05002378 return 0;
2379}
2380
Chris Masond1310b22008-01-24 16:13:08 -05002381/**
Chris Mason4bef0842008-09-08 11:18:08 -04002382 * 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 -05002383 * @mapping: address space structure to write
2384 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2385 * @writepage: function called for each page
2386 * @data: data passed to writepage function
2387 *
2388 * If a page is already under I/O, write_cache_pages() skips it, even
2389 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2390 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2391 * and msync() need to guarantee that all the data which was dirty at the time
2392 * the call was made get new I/O started against them. If wbc->sync_mode is
2393 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2394 * existing IO to complete.
2395 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002396static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002397 struct address_space *mapping,
2398 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002399 writepage_t writepage, void *data,
2400 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002401{
2402 struct backing_dev_info *bdi = mapping->backing_dev_info;
2403 int ret = 0;
2404 int done = 0;
2405 struct pagevec pvec;
2406 int nr_pages;
2407 pgoff_t index;
2408 pgoff_t end; /* Inclusive */
2409 int scanned = 0;
2410 int range_whole = 0;
2411
2412 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2413 wbc->encountered_congestion = 1;
2414 return 0;
2415 }
2416
2417 pagevec_init(&pvec, 0);
2418 if (wbc->range_cyclic) {
2419 index = mapping->writeback_index; /* Start from prev offset */
2420 end = -1;
2421 } else {
2422 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2423 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2424 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2425 range_whole = 1;
2426 scanned = 1;
2427 }
2428retry:
2429 while (!done && (index <= end) &&
2430 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2431 PAGECACHE_TAG_DIRTY,
2432 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2433 unsigned i;
2434
2435 scanned = 1;
2436 for (i = 0; i < nr_pages; i++) {
2437 struct page *page = pvec.pages[i];
2438
2439 /*
2440 * At this point we hold neither mapping->tree_lock nor
2441 * lock on the page itself: the page may be truncated or
2442 * invalidated (changing page->mapping to NULL), or even
2443 * swizzled back from swapper_space to tmpfs file
2444 * mapping
2445 */
Chris Mason4bef0842008-09-08 11:18:08 -04002446 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2447 tree->ops->write_cache_pages_lock_hook(page);
2448 else
2449 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002450
2451 if (unlikely(page->mapping != mapping)) {
2452 unlock_page(page);
2453 continue;
2454 }
2455
2456 if (!wbc->range_cyclic && page->index > end) {
2457 done = 1;
2458 unlock_page(page);
2459 continue;
2460 }
2461
Chris Masond2c3f4f2008-11-19 12:44:22 -05002462 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002463 if (PageWriteback(page))
2464 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002465 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002466 }
Chris Masond1310b22008-01-24 16:13:08 -05002467
2468 if (PageWriteback(page) ||
2469 !clear_page_dirty_for_io(page)) {
2470 unlock_page(page);
2471 continue;
2472 }
2473
2474 ret = (*writepage)(page, wbc, data);
2475
2476 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2477 unlock_page(page);
2478 ret = 0;
2479 }
Chris Mason771ed682008-11-06 22:02:51 -05002480 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002481 done = 1;
2482 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2483 wbc->encountered_congestion = 1;
2484 done = 1;
2485 }
2486 }
2487 pagevec_release(&pvec);
2488 cond_resched();
2489 }
2490 if (!scanned && !done) {
2491 /*
2492 * We hit the last page and there is more work to be done: wrap
2493 * back to the start of the file
2494 */
2495 scanned = 1;
2496 index = 0;
2497 goto retry;
2498 }
Chris Masond1310b22008-01-24 16:13:08 -05002499 return ret;
2500}
Chris Masond1310b22008-01-24 16:13:08 -05002501
Chris Masond2c3f4f2008-11-19 12:44:22 -05002502static noinline void flush_write_bio(void *data)
2503{
2504 struct extent_page_data *epd = data;
2505 if (epd->bio) {
2506 submit_one_bio(WRITE, epd->bio, 0, 0);
2507 epd->bio = NULL;
2508 }
2509}
2510
Chris Masond1310b22008-01-24 16:13:08 -05002511int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2512 get_extent_t *get_extent,
2513 struct writeback_control *wbc)
2514{
2515 int ret;
2516 struct address_space *mapping = page->mapping;
2517 struct extent_page_data epd = {
2518 .bio = NULL,
2519 .tree = tree,
2520 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002521 .extent_locked = 0,
Chris Masond1310b22008-01-24 16:13:08 -05002522 };
2523 struct writeback_control wbc_writepages = {
2524 .bdi = wbc->bdi,
2525 .sync_mode = WB_SYNC_NONE,
2526 .older_than_this = NULL,
2527 .nr_to_write = 64,
2528 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2529 .range_end = (loff_t)-1,
2530 };
2531
2532
2533 ret = __extent_writepage(page, wbc, &epd);
2534
Chris Mason4bef0842008-09-08 11:18:08 -04002535 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002536 __extent_writepage, &epd, flush_write_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002537 if (epd.bio) {
Chris Masonc8b97812008-10-29 14:49:59 -04002538 submit_one_bio(WRITE, epd.bio, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002539 }
2540 return ret;
2541}
2542EXPORT_SYMBOL(extent_write_full_page);
2543
Chris Mason771ed682008-11-06 22:02:51 -05002544int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2545 u64 start, u64 end, get_extent_t *get_extent,
2546 int mode)
2547{
2548 int ret = 0;
2549 struct address_space *mapping = inode->i_mapping;
2550 struct page *page;
2551 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2552 PAGE_CACHE_SHIFT;
2553
2554 struct extent_page_data epd = {
2555 .bio = NULL,
2556 .tree = tree,
2557 .get_extent = get_extent,
2558 .extent_locked = 1,
2559 };
2560 struct writeback_control wbc_writepages = {
2561 .bdi = inode->i_mapping->backing_dev_info,
2562 .sync_mode = mode,
2563 .older_than_this = NULL,
2564 .nr_to_write = nr_pages * 2,
2565 .range_start = start,
2566 .range_end = end + 1,
2567 };
2568
2569 while(start <= end) {
2570 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2571 if (clear_page_dirty_for_io(page))
2572 ret = __extent_writepage(page, &wbc_writepages, &epd);
2573 else {
2574 if (tree->ops && tree->ops->writepage_end_io_hook)
2575 tree->ops->writepage_end_io_hook(page, start,
2576 start + PAGE_CACHE_SIZE - 1,
2577 NULL, 1);
2578 unlock_page(page);
2579 }
2580 page_cache_release(page);
2581 start += PAGE_CACHE_SIZE;
2582 }
2583
2584 if (epd.bio)
2585 submit_one_bio(WRITE, epd.bio, 0, 0);
2586 return ret;
2587}
2588EXPORT_SYMBOL(extent_write_locked_range);
2589
Chris Masond1310b22008-01-24 16:13:08 -05002590
2591int extent_writepages(struct extent_io_tree *tree,
2592 struct address_space *mapping,
2593 get_extent_t *get_extent,
2594 struct writeback_control *wbc)
2595{
2596 int ret = 0;
2597 struct extent_page_data epd = {
2598 .bio = NULL,
2599 .tree = tree,
2600 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002601 .extent_locked = 0,
Chris Masond1310b22008-01-24 16:13:08 -05002602 };
2603
Chris Mason4bef0842008-09-08 11:18:08 -04002604 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002605 __extent_writepage, &epd,
2606 flush_write_bio);
Chris Masond1310b22008-01-24 16:13:08 -05002607 if (epd.bio) {
Chris Masonc8b97812008-10-29 14:49:59 -04002608 submit_one_bio(WRITE, epd.bio, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002609 }
2610 return ret;
2611}
2612EXPORT_SYMBOL(extent_writepages);
2613
2614int extent_readpages(struct extent_io_tree *tree,
2615 struct address_space *mapping,
2616 struct list_head *pages, unsigned nr_pages,
2617 get_extent_t get_extent)
2618{
2619 struct bio *bio = NULL;
2620 unsigned page_idx;
2621 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002622 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002623
2624 pagevec_init(&pvec, 0);
2625 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2626 struct page *page = list_entry(pages->prev, struct page, lru);
2627
2628 prefetchw(&page->flags);
2629 list_del(&page->lru);
2630 /*
2631 * what we want to do here is call add_to_page_cache_lru,
2632 * but that isn't exported, so we reproduce it here
2633 */
2634 if (!add_to_page_cache(page, mapping,
2635 page->index, GFP_KERNEL)) {
2636
2637 /* open coding of lru_cache_add, also not exported */
2638 page_cache_get(page);
2639 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002640 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002641 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002642 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002643 }
2644 page_cache_release(page);
2645 }
2646 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002647 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002648 BUG_ON(!list_empty(pages));
2649 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002650 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002651 return 0;
2652}
2653EXPORT_SYMBOL(extent_readpages);
2654
2655/*
2656 * basic invalidatepage code, this waits on any locked or writeback
2657 * ranges corresponding to the page, and then deletes any extent state
2658 * records from the tree
2659 */
2660int extent_invalidatepage(struct extent_io_tree *tree,
2661 struct page *page, unsigned long offset)
2662{
2663 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2664 u64 end = start + PAGE_CACHE_SIZE - 1;
2665 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2666
2667 start += (offset + blocksize -1) & ~(blocksize - 1);
2668 if (start > end)
2669 return 0;
2670
2671 lock_extent(tree, start, end, GFP_NOFS);
2672 wait_on_extent_writeback(tree, start, end);
2673 clear_extent_bit(tree, start, end,
2674 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2675 1, 1, GFP_NOFS);
2676 return 0;
2677}
2678EXPORT_SYMBOL(extent_invalidatepage);
2679
2680/*
2681 * simple commit_write call, set_range_dirty is used to mark both
2682 * the pages and the extent records as dirty
2683 */
2684int extent_commit_write(struct extent_io_tree *tree,
2685 struct inode *inode, struct page *page,
2686 unsigned from, unsigned to)
2687{
2688 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2689
2690 set_page_extent_mapped(page);
2691 set_page_dirty(page);
2692
2693 if (pos > inode->i_size) {
2694 i_size_write(inode, pos);
2695 mark_inode_dirty(inode);
2696 }
2697 return 0;
2698}
2699EXPORT_SYMBOL(extent_commit_write);
2700
2701int extent_prepare_write(struct extent_io_tree *tree,
2702 struct inode *inode, struct page *page,
2703 unsigned from, unsigned to, get_extent_t *get_extent)
2704{
2705 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2706 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2707 u64 block_start;
2708 u64 orig_block_start;
2709 u64 block_end;
2710 u64 cur_end;
2711 struct extent_map *em;
2712 unsigned blocksize = 1 << inode->i_blkbits;
2713 size_t page_offset = 0;
2714 size_t block_off_start;
2715 size_t block_off_end;
2716 int err = 0;
2717 int iocount = 0;
2718 int ret = 0;
2719 int isnew;
2720
2721 set_page_extent_mapped(page);
2722
2723 block_start = (page_start + from) & ~((u64)blocksize - 1);
2724 block_end = (page_start + to - 1) | (blocksize - 1);
2725 orig_block_start = block_start;
2726
2727 lock_extent(tree, page_start, page_end, GFP_NOFS);
2728 while(block_start <= block_end) {
2729 em = get_extent(inode, page, page_offset, block_start,
2730 block_end - block_start + 1, 1);
2731 if (IS_ERR(em) || !em) {
2732 goto err;
2733 }
2734 cur_end = min(block_end, extent_map_end(em) - 1);
2735 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2736 block_off_end = block_off_start + blocksize;
2737 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2738
2739 if (!PageUptodate(page) && isnew &&
2740 (block_off_end > to || block_off_start < from)) {
2741 void *kaddr;
2742
2743 kaddr = kmap_atomic(page, KM_USER0);
2744 if (block_off_end > to)
2745 memset(kaddr + to, 0, block_off_end - to);
2746 if (block_off_start < from)
2747 memset(kaddr + block_off_start, 0,
2748 from - block_off_start);
2749 flush_dcache_page(page);
2750 kunmap_atomic(kaddr, KM_USER0);
2751 }
2752 if ((em->block_start != EXTENT_MAP_HOLE &&
2753 em->block_start != EXTENT_MAP_INLINE) &&
2754 !isnew && !PageUptodate(page) &&
2755 (block_off_end > to || block_off_start < from) &&
2756 !test_range_bit(tree, block_start, cur_end,
2757 EXTENT_UPTODATE, 1)) {
2758 u64 sector;
2759 u64 extent_offset = block_start - em->start;
2760 size_t iosize;
2761 sector = (em->block_start + extent_offset) >> 9;
2762 iosize = (cur_end - block_start + blocksize) &
2763 ~((u64)blocksize - 1);
2764 /*
2765 * we've already got the extent locked, but we
2766 * need to split the state such that our end_bio
2767 * handler can clear the lock.
2768 */
2769 set_extent_bit(tree, block_start,
2770 block_start + iosize - 1,
2771 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2772 ret = submit_extent_page(READ, tree, page,
2773 sector, iosize, page_offset, em->bdev,
2774 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002775 end_bio_extent_preparewrite, 0,
2776 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002777 iocount++;
2778 block_start = block_start + iosize;
2779 } else {
2780 set_extent_uptodate(tree, block_start, cur_end,
2781 GFP_NOFS);
2782 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2783 block_start = cur_end + 1;
2784 }
2785 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2786 free_extent_map(em);
2787 }
2788 if (iocount) {
2789 wait_extent_bit(tree, orig_block_start,
2790 block_end, EXTENT_LOCKED);
2791 }
2792 check_page_uptodate(tree, page);
2793err:
2794 /* FIXME, zero out newly allocated blocks on error */
2795 return err;
2796}
2797EXPORT_SYMBOL(extent_prepare_write);
2798
2799/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002800 * a helper for releasepage, this tests for areas of the page that
2801 * are locked or under IO and drops the related state bits if it is safe
2802 * to drop the page.
2803 */
2804int try_release_extent_state(struct extent_map_tree *map,
2805 struct extent_io_tree *tree, struct page *page,
2806 gfp_t mask)
2807{
2808 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2809 u64 end = start + PAGE_CACHE_SIZE - 1;
2810 int ret = 1;
2811
Chris Mason211f90e2008-07-18 11:56:15 -04002812 if (test_range_bit(tree, start, end,
2813 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002814 ret = 0;
2815 else {
2816 if ((mask & GFP_NOFS) == GFP_NOFS)
2817 mask = GFP_NOFS;
2818 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2819 1, 1, mask);
2820 }
2821 return ret;
2822}
2823EXPORT_SYMBOL(try_release_extent_state);
2824
2825/*
Chris Masond1310b22008-01-24 16:13:08 -05002826 * a helper for releasepage. As long as there are no locked extents
2827 * in the range corresponding to the page, both state records and extent
2828 * map records are removed
2829 */
2830int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002831 struct extent_io_tree *tree, struct page *page,
2832 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002833{
2834 struct extent_map *em;
2835 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2836 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002837
Chris Mason70dec802008-01-29 09:59:12 -05002838 if ((mask & __GFP_WAIT) &&
2839 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002840 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002841 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002842 len = end - start + 1;
Chris Mason70dec802008-01-29 09:59:12 -05002843 spin_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002844 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002845 if (!em || IS_ERR(em)) {
2846 spin_unlock(&map->lock);
2847 break;
2848 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002849 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2850 em->start != start) {
Chris Mason70dec802008-01-29 09:59:12 -05002851 spin_unlock(&map->lock);
2852 free_extent_map(em);
2853 break;
2854 }
2855 if (!test_range_bit(tree, em->start,
2856 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002857 EXTENT_LOCKED | EXTENT_WRITEBACK |
2858 EXTENT_ORDERED,
2859 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002860 remove_extent_mapping(map, em);
2861 /* once for the rb tree */
2862 free_extent_map(em);
2863 }
2864 start = extent_map_end(em);
Chris Masond1310b22008-01-24 16:13:08 -05002865 spin_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002866
2867 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002868 free_extent_map(em);
2869 }
Chris Masond1310b22008-01-24 16:13:08 -05002870 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002871 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002872}
2873EXPORT_SYMBOL(try_release_extent_mapping);
2874
2875sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2876 get_extent_t *get_extent)
2877{
2878 struct inode *inode = mapping->host;
2879 u64 start = iblock << inode->i_blkbits;
2880 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002881 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002882 struct extent_map *em;
2883
Yan Zhengd899e052008-10-30 14:25:28 -04002884 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2885 GFP_NOFS);
2886 em = get_extent(inode, NULL, 0, start, blksize, 0);
2887 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2888 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002889 if (!em || IS_ERR(em))
2890 return 0;
2891
Yan Zhengd899e052008-10-30 14:25:28 -04002892 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002893 goto out;
2894
2895 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002896out:
2897 free_extent_map(em);
2898 return sector;
2899}
2900
Chris Masond1310b22008-01-24 16:13:08 -05002901static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2902 unsigned long i)
2903{
2904 struct page *p;
2905 struct address_space *mapping;
2906
2907 if (i == 0)
2908 return eb->first_page;
2909 i += eb->start >> PAGE_CACHE_SHIFT;
2910 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002911 if (!mapping)
2912 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002913
2914 /*
2915 * extent_buffer_page is only called after pinning the page
2916 * by increasing the reference count. So we know the page must
2917 * be in the radix tree.
2918 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002919 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002920 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002921 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002922
Chris Masond1310b22008-01-24 16:13:08 -05002923 return p;
2924}
2925
Chris Mason6af118ce2008-07-22 11:18:07 -04002926static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002927{
Chris Mason6af118ce2008-07-22 11:18:07 -04002928 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2929 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002930}
2931
Chris Masond1310b22008-01-24 16:13:08 -05002932static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2933 u64 start,
2934 unsigned long len,
2935 gfp_t mask)
2936{
2937 struct extent_buffer *eb = NULL;
Chris Mason4bef0842008-09-08 11:18:08 -04002938#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002939 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04002940#endif
Chris Masond1310b22008-01-24 16:13:08 -05002941
Chris Masond1310b22008-01-24 16:13:08 -05002942 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002943 eb->start = start;
2944 eb->len = len;
Chris Masona61e6f22008-07-22 11:18:08 -04002945 mutex_init(&eb->mutex);
Chris Mason4bef0842008-09-08 11:18:08 -04002946#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002947 spin_lock_irqsave(&leak_lock, flags);
2948 list_add(&eb->leak_list, &buffers);
2949 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002950#endif
Chris Masond1310b22008-01-24 16:13:08 -05002951 atomic_set(&eb->refs, 1);
2952
2953 return eb;
2954}
2955
2956static void __free_extent_buffer(struct extent_buffer *eb)
2957{
Chris Mason4bef0842008-09-08 11:18:08 -04002958#ifdef LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002959 unsigned long flags;
2960 spin_lock_irqsave(&leak_lock, flags);
2961 list_del(&eb->leak_list);
2962 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002963#endif
Chris Masond1310b22008-01-24 16:13:08 -05002964 kmem_cache_free(extent_buffer_cache, eb);
2965}
2966
2967struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2968 u64 start, unsigned long len,
2969 struct page *page0,
2970 gfp_t mask)
2971{
2972 unsigned long num_pages = num_extent_pages(start, len);
2973 unsigned long i;
2974 unsigned long index = start >> PAGE_CACHE_SHIFT;
2975 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04002976 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002977 struct page *p;
2978 struct address_space *mapping = tree->mapping;
2979 int uptodate = 1;
2980
Chris Mason6af118ce2008-07-22 11:18:07 -04002981 spin_lock(&tree->buffer_lock);
2982 eb = buffer_search(tree, start);
2983 if (eb) {
2984 atomic_inc(&eb->refs);
2985 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002986 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002987 return eb;
2988 }
2989 spin_unlock(&tree->buffer_lock);
2990
Chris Masond1310b22008-01-24 16:13:08 -05002991 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04002992 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05002993 return NULL;
2994
Chris Masond1310b22008-01-24 16:13:08 -05002995 if (page0) {
2996 eb->first_page = page0;
2997 i = 1;
2998 index++;
2999 page_cache_get(page0);
3000 mark_page_accessed(page0);
3001 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003002 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003003 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003004 } else {
3005 i = 0;
3006 }
3007 for (; i < num_pages; i++, index++) {
3008 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3009 if (!p) {
3010 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003011 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003012 }
3013 set_page_extent_mapped(p);
3014 mark_page_accessed(p);
3015 if (i == 0) {
3016 eb->first_page = p;
3017 set_page_extent_head(p, len);
3018 } else {
3019 set_page_private(p, EXTENT_PAGE_PRIVATE);
3020 }
3021 if (!PageUptodate(p))
3022 uptodate = 0;
3023 unlock_page(p);
3024 }
3025 if (uptodate)
3026 eb->flags |= EXTENT_UPTODATE;
3027 eb->flags |= EXTENT_BUFFER_FILLED;
3028
Chris Mason6af118ce2008-07-22 11:18:07 -04003029 spin_lock(&tree->buffer_lock);
3030 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3031 if (exists) {
3032 /* add one reference for the caller */
3033 atomic_inc(&exists->refs);
3034 spin_unlock(&tree->buffer_lock);
3035 goto free_eb;
3036 }
3037 spin_unlock(&tree->buffer_lock);
3038
3039 /* add one reference for the tree */
3040 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003041 return eb;
3042
Chris Mason6af118ce2008-07-22 11:18:07 -04003043free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003044 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003045 return exists;
3046 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003047 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003048 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003049 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003050 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003051}
3052EXPORT_SYMBOL(alloc_extent_buffer);
3053
3054struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3055 u64 start, unsigned long len,
3056 gfp_t mask)
3057{
Chris Masond1310b22008-01-24 16:13:08 -05003058 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003059
Chris Mason6af118ce2008-07-22 11:18:07 -04003060 spin_lock(&tree->buffer_lock);
3061 eb = buffer_search(tree, start);
3062 if (eb)
3063 atomic_inc(&eb->refs);
3064 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003065
Josef Bacik0f9dd462008-09-23 13:14:11 -04003066 if (eb)
3067 mark_page_accessed(eb->first_page);
3068
Chris Masond1310b22008-01-24 16:13:08 -05003069 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003070}
3071EXPORT_SYMBOL(find_extent_buffer);
3072
3073void free_extent_buffer(struct extent_buffer *eb)
3074{
Chris Masond1310b22008-01-24 16:13:08 -05003075 if (!eb)
3076 return;
3077
3078 if (!atomic_dec_and_test(&eb->refs))
3079 return;
3080
Chris Mason6af118ce2008-07-22 11:18:07 -04003081 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003082}
3083EXPORT_SYMBOL(free_extent_buffer);
3084
3085int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3086 struct extent_buffer *eb)
3087{
3088 int set;
3089 unsigned long i;
3090 unsigned long num_pages;
3091 struct page *page;
3092
3093 u64 start = eb->start;
3094 u64 end = start + eb->len - 1;
3095
3096 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
3097 num_pages = num_extent_pages(eb->start, eb->len);
3098
3099 for (i = 0; i < num_pages; i++) {
3100 page = extent_buffer_page(eb, i);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003101 if (!set && !PageDirty(page))
3102 continue;
3103
Chris Masona61e6f22008-07-22 11:18:08 -04003104 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003105 if (i == 0)
3106 set_page_extent_head(page, eb->len);
3107 else
3108 set_page_private(page, EXTENT_PAGE_PRIVATE);
3109
3110 /*
3111 * if we're on the last page or the first page and the
3112 * block isn't aligned on a page boundary, do extra checks
3113 * to make sure we don't clean page that is partially dirty
3114 */
3115 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3116 ((i == num_pages - 1) &&
3117 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3118 start = (u64)page->index << PAGE_CACHE_SHIFT;
3119 end = start + PAGE_CACHE_SIZE - 1;
3120 if (test_range_bit(tree, start, end,
3121 EXTENT_DIRTY, 0)) {
Chris Masona61e6f22008-07-22 11:18:08 -04003122 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003123 continue;
3124 }
3125 }
3126 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003127 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003128 if (!PageDirty(page)) {
3129 radix_tree_tag_clear(&page->mapping->page_tree,
3130 page_index(page),
3131 PAGECACHE_TAG_DIRTY);
3132 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003133 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003134 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003135 }
3136 return 0;
3137}
3138EXPORT_SYMBOL(clear_extent_buffer_dirty);
3139
3140int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3141 struct extent_buffer *eb)
3142{
3143 return wait_on_extent_writeback(tree, eb->start,
3144 eb->start + eb->len - 1);
3145}
3146EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
3147
3148int set_extent_buffer_dirty(struct extent_io_tree *tree,
3149 struct extent_buffer *eb)
3150{
3151 unsigned long i;
3152 unsigned long num_pages;
3153
3154 num_pages = num_extent_pages(eb->start, eb->len);
3155 for (i = 0; i < num_pages; i++) {
3156 struct page *page = extent_buffer_page(eb, i);
3157 /* writepage may need to do something special for the
3158 * first page, we have to make sure page->private is
3159 * properly set. releasepage may drop page->private
3160 * on us if the page isn't already dirty.
3161 */
Chris Masona1b32a52008-09-05 16:09:51 -04003162 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003163 if (i == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003164 set_page_extent_head(page, eb->len);
3165 } else if (PagePrivate(page) &&
3166 page->private != EXTENT_PAGE_PRIVATE) {
Chris Masond1310b22008-01-24 16:13:08 -05003167 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003168 }
3169 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masona1b32a52008-09-05 16:09:51 -04003170 set_extent_dirty(tree, page_offset(page),
3171 page_offset(page) + PAGE_CACHE_SIZE -1,
3172 GFP_NOFS);
3173 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003174 }
Chris Masona1b32a52008-09-05 16:09:51 -04003175 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05003176}
3177EXPORT_SYMBOL(set_extent_buffer_dirty);
3178
Chris Mason1259ab72008-05-12 13:39:03 -04003179int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3180 struct extent_buffer *eb)
3181{
3182 unsigned long i;
3183 struct page *page;
3184 unsigned long num_pages;
3185
3186 num_pages = num_extent_pages(eb->start, eb->len);
3187 eb->flags &= ~EXTENT_UPTODATE;
3188
3189 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3190 GFP_NOFS);
3191 for (i = 0; i < num_pages; i++) {
3192 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003193 if (page)
3194 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003195 }
3196 return 0;
3197}
3198
Chris Masond1310b22008-01-24 16:13:08 -05003199int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3200 struct extent_buffer *eb)
3201{
3202 unsigned long i;
3203 struct page *page;
3204 unsigned long num_pages;
3205
3206 num_pages = num_extent_pages(eb->start, eb->len);
3207
3208 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3209 GFP_NOFS);
3210 for (i = 0; i < num_pages; i++) {
3211 page = extent_buffer_page(eb, i);
3212 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3213 ((i == num_pages - 1) &&
3214 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3215 check_page_uptodate(tree, page);
3216 continue;
3217 }
3218 SetPageUptodate(page);
3219 }
3220 return 0;
3221}
3222EXPORT_SYMBOL(set_extent_buffer_uptodate);
3223
Chris Masonce9adaa2008-04-09 16:28:12 -04003224int extent_range_uptodate(struct extent_io_tree *tree,
3225 u64 start, u64 end)
3226{
3227 struct page *page;
3228 int ret;
3229 int pg_uptodate = 1;
3230 int uptodate;
3231 unsigned long index;
3232
3233 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3234 if (ret)
3235 return 1;
3236 while(start <= end) {
3237 index = start >> PAGE_CACHE_SHIFT;
3238 page = find_get_page(tree->mapping, index);
3239 uptodate = PageUptodate(page);
3240 page_cache_release(page);
3241 if (!uptodate) {
3242 pg_uptodate = 0;
3243 break;
3244 }
3245 start += PAGE_CACHE_SIZE;
3246 }
3247 return pg_uptodate;
3248}
3249
Chris Masond1310b22008-01-24 16:13:08 -05003250int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003251 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003252{
Chris Mason728131d2008-04-09 16:28:12 -04003253 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003254 unsigned long num_pages;
3255 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003256 struct page *page;
3257 int pg_uptodate = 1;
3258
Chris Masond1310b22008-01-24 16:13:08 -05003259 if (eb->flags & EXTENT_UPTODATE)
Chris Mason42352982008-04-28 16:40:52 -04003260 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003261
Chris Mason42352982008-04-28 16:40:52 -04003262 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003263 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003264 if (ret)
3265 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003266
3267 num_pages = num_extent_pages(eb->start, eb->len);
3268 for (i = 0; i < num_pages; i++) {
3269 page = extent_buffer_page(eb, i);
3270 if (!PageUptodate(page)) {
3271 pg_uptodate = 0;
3272 break;
3273 }
3274 }
Chris Mason42352982008-04-28 16:40:52 -04003275 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003276}
3277EXPORT_SYMBOL(extent_buffer_uptodate);
3278
3279int read_extent_buffer_pages(struct extent_io_tree *tree,
3280 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003281 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003282 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003283{
3284 unsigned long i;
3285 unsigned long start_i;
3286 struct page *page;
3287 int err;
3288 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003289 int locked_pages = 0;
3290 int all_uptodate = 1;
3291 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003292 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003293 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003294 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003295
Chris Masond1310b22008-01-24 16:13:08 -05003296 if (eb->flags & EXTENT_UPTODATE)
3297 return 0;
3298
Chris Masonce9adaa2008-04-09 16:28:12 -04003299 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003300 EXTENT_UPTODATE, 1)) {
3301 return 0;
3302 }
3303
3304 if (start) {
3305 WARN_ON(start < eb->start);
3306 start_i = (start >> PAGE_CACHE_SHIFT) -
3307 (eb->start >> PAGE_CACHE_SHIFT);
3308 } else {
3309 start_i = 0;
3310 }
3311
3312 num_pages = num_extent_pages(eb->start, eb->len);
3313 for (i = start_i; i < num_pages; i++) {
3314 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003315 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003316 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003317 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003318 } else {
3319 lock_page(page);
3320 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003321 locked_pages++;
Chris Masond1310b22008-01-24 16:13:08 -05003322 if (!PageUptodate(page)) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003323 all_uptodate = 0;
3324 }
3325 }
3326 if (all_uptodate) {
3327 if (start_i == 0)
3328 eb->flags |= EXTENT_UPTODATE;
Chris Masona1b32a52008-09-05 16:09:51 -04003329 if (ret) {
3330 printk("all up to date but ret is %d\n", ret);
3331 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003332 goto unlock_exit;
3333 }
3334
3335 for (i = start_i; i < num_pages; i++) {
3336 page = extent_buffer_page(eb, i);
3337 if (inc_all_pages)
3338 page_cache_get(page);
3339 if (!PageUptodate(page)) {
3340 if (start_i == 0)
3341 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003342 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003343 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003344 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003345 mirror_num, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003346 if (err) {
3347 ret = err;
Chris Masona1b32a52008-09-05 16:09:51 -04003348 printk("err %d from __extent_read_full_page\n", ret);
Chris Masond1310b22008-01-24 16:13:08 -05003349 }
3350 } else {
3351 unlock_page(page);
3352 }
3353 }
3354
Chris Masona86c12c2008-02-07 10:50:54 -05003355 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003356 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003357
Chris Masond1310b22008-01-24 16:13:08 -05003358 if (ret || !wait) {
Chris Masona1b32a52008-09-05 16:09:51 -04003359 if (ret)
3360 printk("ret %d wait %d returning\n", ret, wait);
Chris Masond1310b22008-01-24 16:13:08 -05003361 return ret;
3362 }
Chris Masond1310b22008-01-24 16:13:08 -05003363 for (i = start_i; i < num_pages; i++) {
3364 page = extent_buffer_page(eb, i);
3365 wait_on_page_locked(page);
3366 if (!PageUptodate(page)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003367 printk("page not uptodate after wait_on_page_locked\n");
Chris Masond1310b22008-01-24 16:13:08 -05003368 ret = -EIO;
3369 }
3370 }
3371 if (!ret)
3372 eb->flags |= EXTENT_UPTODATE;
3373 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003374
3375unlock_exit:
3376 i = start_i;
3377 while(locked_pages > 0) {
3378 page = extent_buffer_page(eb, i);
3379 i++;
3380 unlock_page(page);
3381 locked_pages--;
3382 }
3383 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003384}
3385EXPORT_SYMBOL(read_extent_buffer_pages);
3386
3387void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3388 unsigned long start,
3389 unsigned long len)
3390{
3391 size_t cur;
3392 size_t offset;
3393 struct page *page;
3394 char *kaddr;
3395 char *dst = (char *)dstv;
3396 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3397 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003398
3399 WARN_ON(start > eb->len);
3400 WARN_ON(start + len > eb->start + eb->len);
3401
3402 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3403
3404 while(len > 0) {
3405 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003406
3407 cur = min(len, (PAGE_CACHE_SIZE - offset));
3408 kaddr = kmap_atomic(page, KM_USER1);
3409 memcpy(dst, kaddr + offset, cur);
3410 kunmap_atomic(kaddr, KM_USER1);
3411
3412 dst += cur;
3413 len -= cur;
3414 offset = 0;
3415 i++;
3416 }
3417}
3418EXPORT_SYMBOL(read_extent_buffer);
3419
3420int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3421 unsigned long min_len, char **token, char **map,
3422 unsigned long *map_start,
3423 unsigned long *map_len, int km)
3424{
3425 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3426 char *kaddr;
3427 struct page *p;
3428 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3429 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3430 unsigned long end_i = (start_offset + start + min_len - 1) >>
3431 PAGE_CACHE_SHIFT;
3432
3433 if (i != end_i)
3434 return -EINVAL;
3435
3436 if (i == 0) {
3437 offset = start_offset;
3438 *map_start = 0;
3439 } else {
3440 offset = 0;
3441 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3442 }
3443 if (start + min_len > eb->len) {
3444printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3445 WARN_ON(1);
3446 }
3447
3448 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003449 kaddr = kmap_atomic(p, km);
3450 *token = kaddr;
3451 *map = kaddr + offset;
3452 *map_len = PAGE_CACHE_SIZE - offset;
3453 return 0;
3454}
3455EXPORT_SYMBOL(map_private_extent_buffer);
3456
3457int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3458 unsigned long min_len,
3459 char **token, char **map,
3460 unsigned long *map_start,
3461 unsigned long *map_len, int km)
3462{
3463 int err;
3464 int save = 0;
3465 if (eb->map_token) {
3466 unmap_extent_buffer(eb, eb->map_token, km);
3467 eb->map_token = NULL;
3468 save = 1;
3469 }
3470 err = map_private_extent_buffer(eb, start, min_len, token, map,
3471 map_start, map_len, km);
3472 if (!err && save) {
3473 eb->map_token = *token;
3474 eb->kaddr = *map;
3475 eb->map_start = *map_start;
3476 eb->map_len = *map_len;
3477 }
3478 return err;
3479}
3480EXPORT_SYMBOL(map_extent_buffer);
3481
3482void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3483{
3484 kunmap_atomic(token, km);
3485}
3486EXPORT_SYMBOL(unmap_extent_buffer);
3487
3488int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3489 unsigned long start,
3490 unsigned long len)
3491{
3492 size_t cur;
3493 size_t offset;
3494 struct page *page;
3495 char *kaddr;
3496 char *ptr = (char *)ptrv;
3497 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3498 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3499 int ret = 0;
3500
3501 WARN_ON(start > eb->len);
3502 WARN_ON(start + len > eb->start + eb->len);
3503
3504 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3505
3506 while(len > 0) {
3507 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003508
3509 cur = min(len, (PAGE_CACHE_SIZE - offset));
3510
3511 kaddr = kmap_atomic(page, KM_USER0);
3512 ret = memcmp(ptr, kaddr + offset, cur);
3513 kunmap_atomic(kaddr, KM_USER0);
3514 if (ret)
3515 break;
3516
3517 ptr += cur;
3518 len -= cur;
3519 offset = 0;
3520 i++;
3521 }
3522 return ret;
3523}
3524EXPORT_SYMBOL(memcmp_extent_buffer);
3525
3526void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3527 unsigned long start, unsigned long len)
3528{
3529 size_t cur;
3530 size_t offset;
3531 struct page *page;
3532 char *kaddr;
3533 char *src = (char *)srcv;
3534 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3535 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3536
3537 WARN_ON(start > eb->len);
3538 WARN_ON(start + len > eb->start + eb->len);
3539
3540 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3541
3542 while(len > 0) {
3543 page = extent_buffer_page(eb, i);
3544 WARN_ON(!PageUptodate(page));
3545
3546 cur = min(len, PAGE_CACHE_SIZE - offset);
3547 kaddr = kmap_atomic(page, KM_USER1);
3548 memcpy(kaddr + offset, src, cur);
3549 kunmap_atomic(kaddr, KM_USER1);
3550
3551 src += cur;
3552 len -= cur;
3553 offset = 0;
3554 i++;
3555 }
3556}
3557EXPORT_SYMBOL(write_extent_buffer);
3558
3559void memset_extent_buffer(struct extent_buffer *eb, char c,
3560 unsigned long start, unsigned long len)
3561{
3562 size_t cur;
3563 size_t offset;
3564 struct page *page;
3565 char *kaddr;
3566 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3567 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3568
3569 WARN_ON(start > eb->len);
3570 WARN_ON(start + len > eb->start + eb->len);
3571
3572 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3573
3574 while(len > 0) {
3575 page = extent_buffer_page(eb, i);
3576 WARN_ON(!PageUptodate(page));
3577
3578 cur = min(len, PAGE_CACHE_SIZE - offset);
3579 kaddr = kmap_atomic(page, KM_USER0);
3580 memset(kaddr + offset, c, cur);
3581 kunmap_atomic(kaddr, KM_USER0);
3582
3583 len -= cur;
3584 offset = 0;
3585 i++;
3586 }
3587}
3588EXPORT_SYMBOL(memset_extent_buffer);
3589
3590void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3591 unsigned long dst_offset, unsigned long src_offset,
3592 unsigned long len)
3593{
3594 u64 dst_len = dst->len;
3595 size_t cur;
3596 size_t offset;
3597 struct page *page;
3598 char *kaddr;
3599 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3600 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3601
3602 WARN_ON(src->len != dst_len);
3603
3604 offset = (start_offset + dst_offset) &
3605 ((unsigned long)PAGE_CACHE_SIZE - 1);
3606
3607 while(len > 0) {
3608 page = extent_buffer_page(dst, i);
3609 WARN_ON(!PageUptodate(page));
3610
3611 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3612
3613 kaddr = kmap_atomic(page, KM_USER0);
3614 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3615 kunmap_atomic(kaddr, KM_USER0);
3616
3617 src_offset += cur;
3618 len -= cur;
3619 offset = 0;
3620 i++;
3621 }
3622}
3623EXPORT_SYMBOL(copy_extent_buffer);
3624
3625static void move_pages(struct page *dst_page, struct page *src_page,
3626 unsigned long dst_off, unsigned long src_off,
3627 unsigned long len)
3628{
3629 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3630 if (dst_page == src_page) {
3631 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3632 } else {
3633 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3634 char *p = dst_kaddr + dst_off + len;
3635 char *s = src_kaddr + src_off + len;
3636
3637 while (len--)
3638 *--p = *--s;
3639
3640 kunmap_atomic(src_kaddr, KM_USER1);
3641 }
3642 kunmap_atomic(dst_kaddr, KM_USER0);
3643}
3644
3645static void copy_pages(struct page *dst_page, struct page *src_page,
3646 unsigned long dst_off, unsigned long src_off,
3647 unsigned long len)
3648{
3649 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3650 char *src_kaddr;
3651
3652 if (dst_page != src_page)
3653 src_kaddr = kmap_atomic(src_page, KM_USER1);
3654 else
3655 src_kaddr = dst_kaddr;
3656
3657 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3658 kunmap_atomic(dst_kaddr, KM_USER0);
3659 if (dst_page != src_page)
3660 kunmap_atomic(src_kaddr, KM_USER1);
3661}
3662
3663void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3664 unsigned long src_offset, unsigned long len)
3665{
3666 size_t cur;
3667 size_t dst_off_in_page;
3668 size_t src_off_in_page;
3669 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3670 unsigned long dst_i;
3671 unsigned long src_i;
3672
3673 if (src_offset + len > dst->len) {
3674 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3675 src_offset, len, dst->len);
3676 BUG_ON(1);
3677 }
3678 if (dst_offset + len > dst->len) {
3679 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3680 dst_offset, len, dst->len);
3681 BUG_ON(1);
3682 }
3683
3684 while(len > 0) {
3685 dst_off_in_page = (start_offset + dst_offset) &
3686 ((unsigned long)PAGE_CACHE_SIZE - 1);
3687 src_off_in_page = (start_offset + src_offset) &
3688 ((unsigned long)PAGE_CACHE_SIZE - 1);
3689
3690 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3691 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3692
3693 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3694 src_off_in_page));
3695 cur = min_t(unsigned long, cur,
3696 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3697
3698 copy_pages(extent_buffer_page(dst, dst_i),
3699 extent_buffer_page(dst, src_i),
3700 dst_off_in_page, src_off_in_page, cur);
3701
3702 src_offset += cur;
3703 dst_offset += cur;
3704 len -= cur;
3705 }
3706}
3707EXPORT_SYMBOL(memcpy_extent_buffer);
3708
3709void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3710 unsigned long src_offset, unsigned long len)
3711{
3712 size_t cur;
3713 size_t dst_off_in_page;
3714 size_t src_off_in_page;
3715 unsigned long dst_end = dst_offset + len - 1;
3716 unsigned long src_end = src_offset + len - 1;
3717 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3718 unsigned long dst_i;
3719 unsigned long src_i;
3720
3721 if (src_offset + len > dst->len) {
3722 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3723 src_offset, len, dst->len);
3724 BUG_ON(1);
3725 }
3726 if (dst_offset + len > dst->len) {
3727 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3728 dst_offset, len, dst->len);
3729 BUG_ON(1);
3730 }
3731 if (dst_offset < src_offset) {
3732 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3733 return;
3734 }
3735 while(len > 0) {
3736 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3737 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3738
3739 dst_off_in_page = (start_offset + dst_end) &
3740 ((unsigned long)PAGE_CACHE_SIZE - 1);
3741 src_off_in_page = (start_offset + src_end) &
3742 ((unsigned long)PAGE_CACHE_SIZE - 1);
3743
3744 cur = min_t(unsigned long, len, src_off_in_page + 1);
3745 cur = min(cur, dst_off_in_page + 1);
3746 move_pages(extent_buffer_page(dst, dst_i),
3747 extent_buffer_page(dst, src_i),
3748 dst_off_in_page - cur + 1,
3749 src_off_in_page - cur + 1, cur);
3750
3751 dst_end -= cur;
3752 src_end -= cur;
3753 len -= cur;
3754 }
3755}
3756EXPORT_SYMBOL(memmove_extent_buffer);
Chris Mason6af118ce2008-07-22 11:18:07 -04003757
3758int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3759{
3760 u64 start = page_offset(page);
3761 struct extent_buffer *eb;
3762 int ret = 1;
3763 unsigned long i;
3764 unsigned long num_pages;
3765
3766 spin_lock(&tree->buffer_lock);
3767 eb = buffer_search(tree, start);
3768 if (!eb)
3769 goto out;
3770
3771 if (atomic_read(&eb->refs) > 1) {
3772 ret = 0;
3773 goto out;
3774 }
3775 /* at this point we can safely release the extent buffer */
3776 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003777 for (i = 0; i < num_pages; i++)
3778 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003779 rb_erase(&eb->rb_node, &tree->buffer);
3780 __free_extent_buffer(eb);
3781out:
3782 spin_unlock(&tree->buffer_lock);
3783 return ret;
3784}
3785EXPORT_SYMBOL(try_release_extent_buffer);