blob: 3c17c9eb0d98e344c65bd3350c138390d4a2984e [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>
Chris Masond1310b22008-01-24 16:13:08 -050012#include <linux/writeback.h>
13#include <linux/pagevec.h>
14#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040016#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050019
Chris Masond1310b22008-01-24 16:13:08 -050020static struct kmem_cache *extent_state_cache;
21static struct kmem_cache *extent_buffer_cache;
22
23static LIST_HEAD(buffers);
24static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040025
Chris Masonb47eda82008-11-10 12:34:40 -050026#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050027#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050028static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040029#endif
Chris Masond1310b22008-01-24 16:13:08 -050030
Chris Masond1310b22008-01-24 16:13:08 -050031#define BUFFER_LRU_MAX 64
32
33struct tree_entry {
34 u64 start;
35 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050036 struct rb_node rb_node;
37};
38
39struct extent_page_data {
40 struct bio *bio;
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050043
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
46 */
Chris Masonffbd5172009-04-20 15:50:09 -040047 unsigned int extent_locked:1;
48
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050051};
52
53int __init extent_io_init(void)
54{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020055 extent_state_cache = kmem_cache_create("extent_state",
56 sizeof(struct extent_state), 0,
57 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050058 if (!extent_state_cache)
59 return -ENOMEM;
60
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020061 extent_buffer_cache = kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050064 if (!extent_buffer_cache)
65 goto free_state_cache;
66 return 0;
67
68free_state_cache:
69 kmem_cache_destroy(extent_state_cache);
70 return -ENOMEM;
71}
72
73void extent_io_exit(void)
74{
75 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040076 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050077
78 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040079 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050080 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state->start,
83 (unsigned long long)state->end,
84 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);
Chris Masond3977122009-01-05 21:25:51 -050092 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb->start,
94 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040095 list_del(&eb->leak_list);
96 kmem_cache_free(extent_buffer_cache, eb);
97 }
Chris Masond1310b22008-01-24 16:13:08 -050098 if (extent_state_cache)
99 kmem_cache_destroy(extent_state_cache);
100 if (extent_buffer_cache)
101 kmem_cache_destroy(extent_buffer_cache);
102}
103
104void extent_io_tree_init(struct extent_io_tree *tree,
105 struct address_space *mapping, gfp_t mask)
106{
Eric Paris6bef4d32010-02-23 19:43:04 +0000107 tree->state = RB_ROOT;
108 tree->buffer = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500109 tree->ops = NULL;
110 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500111 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400112 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500113 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500114}
Chris Masond1310b22008-01-24 16:13:08 -0500115
Christoph Hellwigb2950862008-12-02 09:54:17 -0500116static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500117{
118 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500119#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400120 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400121#endif
Chris Masond1310b22008-01-24 16:13:08 -0500122
123 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400124 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500125 return state;
126 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500127 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500128 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500129#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400133#endif
Chris Masond1310b22008-01-24 16:13:08 -0500134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
136 return state;
137}
Chris Masond1310b22008-01-24 16:13:08 -0500138
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 Mason39351272009-02-04 09:24:05 -0500144#if 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 Mason39351272009-02-04 09:24:05 -0500148#if 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}
Chris Masond1310b22008-01-24 16:13:08 -0500156
157static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
159{
Chris Masond3977122009-01-05 21:25:51 -0500160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500162 struct tree_entry *entry;
163
Chris Masond3977122009-01-05 21:25:51 -0500164 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500165 parent = *p;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
167
168 if (offset < entry->start)
169 p = &(*p)->rb_left;
170 else if (offset > entry->end)
171 p = &(*p)->rb_right;
172 else
173 return parent;
174 }
175
176 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
179 return NULL;
180}
181
Chris Mason80ea96b2008-02-01 14:51:59 -0500182static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
185{
Chris Mason80ea96b2008-02-01 14:51:59 -0500186 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500187 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
192
Chris Masond3977122009-01-05 21:25:51 -0500193 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500194 entry = rb_entry(n, struct tree_entry, rb_node);
195 prev = n;
196 prev_entry = entry;
197
198 if (offset < entry->start)
199 n = n->rb_left;
200 else if (offset > entry->end)
201 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500202 else
Chris Masond1310b22008-01-24 16:13:08 -0500203 return n;
204 }
205
206 if (prev_ret) {
207 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500208 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
211 }
212 *prev_ret = prev;
213 prev = orig_prev;
214 }
215
216 if (next_ret) {
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500218 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
221 }
222 *next_ret = prev;
223 }
224 return NULL;
225}
226
Chris Mason80ea96b2008-02-01 14:51:59 -0500227static inline struct rb_node *tree_search(struct extent_io_tree *tree,
228 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500229{
Chris Mason70dec802008-01-29 09:59:12 -0500230 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500231 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500232
Chris Mason80ea96b2008-02-01 14:51:59 -0500233 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500234 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500235 return prev;
236 return ret;
237}
238
Chris Mason6af118ce2008-07-22 11:18:07 -0400239static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
240 u64 offset, struct rb_node *node)
241{
242 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500243 struct rb_node **p = &root->rb_node;
244 struct rb_node *parent = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400245 struct extent_buffer *eb;
246
Chris Masond3977122009-01-05 21:25:51 -0500247 while (*p) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400248 parent = *p;
249 eb = rb_entry(parent, struct extent_buffer, rb_node);
250
251 if (offset < eb->start)
252 p = &(*p)->rb_left;
253 else if (offset > eb->start)
254 p = &(*p)->rb_right;
255 else
256 return eb;
257 }
258
259 rb_link_node(node, parent, p);
260 rb_insert_color(node, root);
261 return NULL;
262}
263
264static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
265 u64 offset)
266{
267 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500268 struct rb_node *n = root->rb_node;
Chris Mason6af118ce2008-07-22 11:18:07 -0400269 struct extent_buffer *eb;
270
Chris Masond3977122009-01-05 21:25:51 -0500271 while (n) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400272 eb = rb_entry(n, struct extent_buffer, rb_node);
273 if (offset < eb->start)
274 n = n->rb_left;
275 else if (offset > eb->start)
276 n = n->rb_right;
277 else
278 return eb;
279 }
280 return NULL;
281}
282
Josef Bacik9ed74f22009-09-11 16:12:44 -0400283static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
284 struct extent_state *other)
285{
286 if (tree->ops && tree->ops->merge_extent_hook)
287 tree->ops->merge_extent_hook(tree->mapping->host, new,
288 other);
289}
290
Chris Masond1310b22008-01-24 16:13:08 -0500291/*
292 * utility function to look for merge candidates inside a given range.
293 * Any extents with matching state are merged together into a single
294 * extent in the tree. Extents with EXTENT_IO in their state field
295 * are not merged because the end_io handlers need to be able to do
296 * operations on them without sleeping (or doing allocations/splits).
297 *
298 * This should be called with the tree lock held.
299 */
300static int merge_state(struct extent_io_tree *tree,
301 struct extent_state *state)
302{
303 struct extent_state *other;
304 struct rb_node *other_node;
305
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400306 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500307 return 0;
308
309 other_node = rb_prev(&state->rb_node);
310 if (other_node) {
311 other = rb_entry(other_node, struct extent_state, rb_node);
312 if (other->end == state->start - 1 &&
313 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400314 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500315 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500316 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500317 rb_erase(&other->rb_node, &tree->state);
318 free_extent_state(other);
319 }
320 }
321 other_node = rb_next(&state->rb_node);
322 if (other_node) {
323 other = rb_entry(other_node, struct extent_state, rb_node);
324 if (other->start == state->end + 1 &&
325 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400326 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500327 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500328 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500329 rb_erase(&state->rb_node, &tree->state);
330 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400331 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500332 }
333 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400334
Chris Masond1310b22008-01-24 16:13:08 -0500335 return 0;
336}
337
Josef Bacik9ed74f22009-09-11 16:12:44 -0400338static int set_state_cb(struct extent_io_tree *tree,
Chris Mason291d6732008-01-29 15:55:23 -0500339 struct extent_state *state,
340 unsigned long bits)
341{
342 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400343 return tree->ops->set_bit_hook(tree->mapping->host,
344 state->start, state->end,
345 state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500346 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400347
348 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500349}
350
351static void clear_state_cb(struct extent_io_tree *tree,
352 struct extent_state *state,
353 unsigned long bits)
354{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400355 if (tree->ops && tree->ops->clear_bit_hook)
356 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500357}
358
Chris Masond1310b22008-01-24 16:13:08 -0500359/*
360 * insert an extent_state struct into the tree. 'bits' are set on the
361 * struct before it is inserted.
362 *
363 * This may return -EEXIST if the extent is already there, in which case the
364 * state struct is freed.
365 *
366 * The tree lock is not taken internally. This is a utility function and
367 * probably isn't what you want to call (see set/clear_extent_bit).
368 */
369static int insert_state(struct extent_io_tree *tree,
370 struct extent_state *state, u64 start, u64 end,
371 int bits)
372{
373 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400374 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500375
376 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500377 printk(KERN_ERR "btrfs end < start %llu %llu\n",
378 (unsigned long long)end,
379 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500380 WARN_ON(1);
381 }
Chris Masond1310b22008-01-24 16:13:08 -0500382 state->start = start;
383 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400384 ret = set_state_cb(tree, state, bits);
385 if (ret)
386 return ret;
387
388 if (bits & EXTENT_DIRTY)
389 tree->dirty_bytes += end - start + 1;
Chris Masone48c4652009-09-11 11:25:02 -0400390 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500391 node = tree_insert(&tree->state, end, &state->rb_node);
392 if (node) {
393 struct extent_state *found;
394 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500395 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
396 "%llu %llu\n", (unsigned long long)found->start,
397 (unsigned long long)found->end,
398 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500399 free_extent_state(state);
400 return -EEXIST;
401 }
Chris Mason70dec802008-01-29 09:59:12 -0500402 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500403 merge_state(tree, state);
404 return 0;
405}
406
Josef Bacik9ed74f22009-09-11 16:12:44 -0400407static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
408 u64 split)
409{
410 if (tree->ops && tree->ops->split_extent_hook)
411 return tree->ops->split_extent_hook(tree->mapping->host,
412 orig, split);
413 return 0;
414}
415
Chris Masond1310b22008-01-24 16:13:08 -0500416/*
417 * split a given extent state struct in two, inserting the preallocated
418 * struct 'prealloc' as the newly created second half. 'split' indicates an
419 * offset inside 'orig' where it should be split.
420 *
421 * Before calling,
422 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
423 * are two extent state structs in the tree:
424 * prealloc: [orig->start, split - 1]
425 * orig: [ split, orig->end ]
426 *
427 * The tree locks are not taken by this function. They need to be held
428 * by the caller.
429 */
430static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
431 struct extent_state *prealloc, u64 split)
432{
433 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400434
435 split_cb(tree, orig, split);
436
Chris Masond1310b22008-01-24 16:13:08 -0500437 prealloc->start = orig->start;
438 prealloc->end = split - 1;
439 prealloc->state = orig->state;
440 orig->start = split;
441
442 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
443 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500444 free_extent_state(prealloc);
445 return -EEXIST;
446 }
Chris Mason70dec802008-01-29 09:59:12 -0500447 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500448 return 0;
449}
450
451/*
452 * utility function to clear some bits in an extent state struct.
453 * it will optionally wake up any one waiting on this state (wake == 1), or
454 * forcibly remove the state from the tree (delete == 1).
455 *
456 * If no bits are set on the state struct after clearing things, the
457 * struct is freed and removed from the tree
458 */
459static int clear_state_bit(struct extent_io_tree *tree,
460 struct extent_state *state, int bits, int wake,
461 int delete)
462{
Josef Bacik32c00af2009-10-08 13:34:05 -0400463 int bits_to_clear = bits & ~EXTENT_DO_ACCOUNTING;
464 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500465
466 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
467 u64 range = state->end - state->start + 1;
468 WARN_ON(range > tree->dirty_bytes);
469 tree->dirty_bytes -= range;
470 }
Chris Mason291d6732008-01-29 15:55:23 -0500471 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400472 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500473 if (wake)
474 wake_up(&state->wq);
475 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500476 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500477 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500478 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500479 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500480 free_extent_state(state);
481 } else {
482 WARN_ON(1);
483 }
484 } else {
485 merge_state(tree, state);
486 }
487 return ret;
488}
489
490/*
491 * clear some bits on a range in the tree. This may require splitting
492 * or inserting elements in the tree, so the gfp mask is used to
493 * indicate which allocations or sleeping are allowed.
494 *
495 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
496 * the given range from the tree regardless of state (ie for truncate).
497 *
498 * the range [start, end] is inclusive.
499 *
500 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
501 * bits were already set, or zero if none of the bits were already set.
502 */
503int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400504 int bits, int wake, int delete,
505 struct extent_state **cached_state,
506 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500507{
508 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400509 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500510 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400511 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500512 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400513 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500514 int err;
515 int set = 0;
516
517again:
518 if (!prealloc && (mask & __GFP_WAIT)) {
519 prealloc = alloc_extent_state(mask);
520 if (!prealloc)
521 return -ENOMEM;
522 }
523
Chris Masoncad321a2008-12-17 14:51:42 -0500524 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400525 if (cached_state) {
526 cached = *cached_state;
527 *cached_state = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400528 cached_state = NULL;
529 if (cached && cached->tree && cached->start == start) {
Chris Mason2c64c532009-09-02 15:04:12 -0400530 atomic_dec(&cached->refs);
531 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400532 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400533 }
534 free_extent_state(cached);
535 }
Chris Masond1310b22008-01-24 16:13:08 -0500536 /*
537 * this search will find the extents that end after
538 * our range starts
539 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500540 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500541 if (!node)
542 goto out;
543 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400544hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500545 if (state->start > end)
546 goto out;
547 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400548 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500549
550 /*
551 * | ---- desired range ---- |
552 * | state | or
553 * | ------------- state -------------- |
554 *
555 * We need to split the extent we found, and may flip
556 * bits on second half.
557 *
558 * If the extent we found extends past our range, we
559 * just split and search again. It'll get split again
560 * the next time though.
561 *
562 * If the extent we found is inside our range, we clear
563 * the desired bit on it.
564 */
565
566 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500567 if (!prealloc)
568 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500569 err = split_state(tree, state, prealloc, start);
570 BUG_ON(err == -EEXIST);
571 prealloc = NULL;
572 if (err)
573 goto out;
574 if (state->end <= end) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400575 set |= clear_state_bit(tree, state, bits, wake,
576 delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400577 if (last_end == (u64)-1)
578 goto out;
579 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500580 }
581 goto search_again;
582 }
583 /*
584 * | ---- desired range ---- |
585 * | state |
586 * We need to split the extent, and clear the bit
587 * on the first half
588 */
589 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500590 if (!prealloc)
591 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500592 err = split_state(tree, state, prealloc, end + 1);
593 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500594 if (wake)
595 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400596
Josef Bacik9ed74f22009-09-11 16:12:44 -0400597 set |= clear_state_bit(tree, prealloc, bits, wake, delete);
598
Chris Masond1310b22008-01-24 16:13:08 -0500599 prealloc = NULL;
600 goto out;
601 }
Chris Mason42daec22009-09-23 19:51:09 -0400602
Chris Mason2c64c532009-09-02 15:04:12 -0400603 if (state->end < end && prealloc && !need_resched())
604 next_node = rb_next(&state->rb_node);
605 else
606 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400607
Chris Masond1310b22008-01-24 16:13:08 -0500608 set |= clear_state_bit(tree, state, bits, wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400609 if (last_end == (u64)-1)
610 goto out;
611 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400612 if (start <= end && next_node) {
613 state = rb_entry(next_node, struct extent_state,
614 rb_node);
615 if (state->start == start)
616 goto hit_next;
617 }
Chris Masond1310b22008-01-24 16:13:08 -0500618 goto search_again;
619
620out:
Chris Masoncad321a2008-12-17 14:51:42 -0500621 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500622 if (prealloc)
623 free_extent_state(prealloc);
624
625 return set;
626
627search_again:
628 if (start > end)
629 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500630 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500631 if (mask & __GFP_WAIT)
632 cond_resched();
633 goto again;
634}
Chris Masond1310b22008-01-24 16:13:08 -0500635
636static int wait_on_state(struct extent_io_tree *tree,
637 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500638 __releases(tree->lock)
639 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500640{
641 DEFINE_WAIT(wait);
642 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500643 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500644 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500645 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500646 finish_wait(&state->wq, &wait);
647 return 0;
648}
649
650/*
651 * waits for one or more bits to clear on a range in the state tree.
652 * The range [start, end] is inclusive.
653 * The tree lock is taken by this function
654 */
655int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
656{
657 struct extent_state *state;
658 struct rb_node *node;
659
Chris Masoncad321a2008-12-17 14:51:42 -0500660 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500661again:
662 while (1) {
663 /*
664 * this search will find all the extents that end after
665 * our range starts
666 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500667 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500668 if (!node)
669 break;
670
671 state = rb_entry(node, struct extent_state, rb_node);
672
673 if (state->start > end)
674 goto out;
675
676 if (state->state & bits) {
677 start = state->start;
678 atomic_inc(&state->refs);
679 wait_on_state(tree, state);
680 free_extent_state(state);
681 goto again;
682 }
683 start = state->end + 1;
684
685 if (start > end)
686 break;
687
688 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500689 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500690 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500691 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500692 }
693 }
694out:
Chris Masoncad321a2008-12-17 14:51:42 -0500695 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500696 return 0;
697}
Chris Masond1310b22008-01-24 16:13:08 -0500698
Josef Bacik9ed74f22009-09-11 16:12:44 -0400699static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500700 struct extent_state *state,
701 int bits)
702{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400703 int ret;
704
705 ret = set_state_cb(tree, state, bits);
706 if (ret)
707 return ret;
708
Chris Masond1310b22008-01-24 16:13:08 -0500709 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
710 u64 range = state->end - state->start + 1;
711 tree->dirty_bytes += range;
712 }
Chris Masonb0c68f82008-01-31 11:05:37 -0500713 state->state |= bits;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400714
715 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500716}
717
Chris Mason2c64c532009-09-02 15:04:12 -0400718static void cache_state(struct extent_state *state,
719 struct extent_state **cached_ptr)
720{
721 if (cached_ptr && !(*cached_ptr)) {
722 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
723 *cached_ptr = state;
724 atomic_inc(&state->refs);
725 }
726 }
727}
728
Chris Masond1310b22008-01-24 16:13:08 -0500729/*
Chris Mason1edbb732009-09-02 13:24:36 -0400730 * set some bits on a range in the tree. This may require allocations or
731 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500732 *
Chris Mason1edbb732009-09-02 13:24:36 -0400733 * If any of the exclusive bits are set, this will fail with -EEXIST if some
734 * part of the range already has the desired bits set. The start of the
735 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500736 *
Chris Mason1edbb732009-09-02 13:24:36 -0400737 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500738 */
Chris Mason1edbb732009-09-02 13:24:36 -0400739
Chris Masond3977122009-01-05 21:25:51 -0500740static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason1edbb732009-09-02 13:24:36 -0400741 int bits, int exclusive_bits, u64 *failed_start,
Chris Mason2c64c532009-09-02 15:04:12 -0400742 struct extent_state **cached_state,
Chris Masond3977122009-01-05 21:25:51 -0500743 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500744{
745 struct extent_state *state;
746 struct extent_state *prealloc = NULL;
747 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500748 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500749 u64 last_start;
750 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400751
Chris Masond1310b22008-01-24 16:13:08 -0500752again:
753 if (!prealloc && (mask & __GFP_WAIT)) {
754 prealloc = alloc_extent_state(mask);
755 if (!prealloc)
756 return -ENOMEM;
757 }
758
Chris Masoncad321a2008-12-17 14:51:42 -0500759 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400760 if (cached_state && *cached_state) {
761 state = *cached_state;
762 if (state->start == start && state->tree) {
763 node = &state->rb_node;
764 goto hit_next;
765 }
766 }
Chris Masond1310b22008-01-24 16:13:08 -0500767 /*
768 * this search will find all the extents that end after
769 * our range starts.
770 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500771 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500772 if (!node) {
773 err = insert_state(tree, prealloc, start, end, bits);
774 prealloc = NULL;
775 BUG_ON(err == -EEXIST);
776 goto out;
777 }
Chris Masond1310b22008-01-24 16:13:08 -0500778 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400779hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500780 last_start = state->start;
781 last_end = state->end;
782
783 /*
784 * | ---- desired range ---- |
785 * | state |
786 *
787 * Just lock what we found and keep going
788 */
789 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400790 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400791 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500792 *failed_start = state->start;
793 err = -EEXIST;
794 goto out;
795 }
Chris Mason42daec22009-09-23 19:51:09 -0400796
Josef Bacik9ed74f22009-09-11 16:12:44 -0400797 err = set_state_bits(tree, state, bits);
798 if (err)
799 goto out;
800
Chris Mason2c64c532009-09-02 15:04:12 -0400801 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500802 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400803 if (last_end == (u64)-1)
804 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400805
Yan Zheng5c939df2009-05-27 09:16:03 -0400806 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400807 if (start < end && prealloc && !need_resched()) {
808 next_node = rb_next(node);
809 if (next_node) {
810 state = rb_entry(next_node, struct extent_state,
811 rb_node);
812 if (state->start == start)
813 goto hit_next;
814 }
815 }
Chris Masond1310b22008-01-24 16:13:08 -0500816 goto search_again;
817 }
818
819 /*
820 * | ---- desired range ---- |
821 * | state |
822 * or
823 * | ------------- state -------------- |
824 *
825 * We need to split the extent we found, and may flip bits on
826 * second half.
827 *
828 * If the extent we found extends past our
829 * range, we just split and search again. It'll get split
830 * again the next time though.
831 *
832 * If the extent we found is inside our range, we set the
833 * desired bit on it.
834 */
835 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400836 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500837 *failed_start = start;
838 err = -EEXIST;
839 goto out;
840 }
841 err = split_state(tree, state, prealloc, start);
842 BUG_ON(err == -EEXIST);
843 prealloc = NULL;
844 if (err)
845 goto out;
846 if (state->end <= end) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400847 err = set_state_bits(tree, state, bits);
848 if (err)
849 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400850 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500851 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400852 if (last_end == (u64)-1)
853 goto out;
854 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500855 }
856 goto search_again;
857 }
858 /*
859 * | ---- desired range ---- |
860 * | state | or | state |
861 *
862 * There's a hole, we need to insert something in it and
863 * ignore the extent we found.
864 */
865 if (state->start > start) {
866 u64 this_end;
867 if (end < last_start)
868 this_end = end;
869 else
Chris Masond3977122009-01-05 21:25:51 -0500870 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500871 err = insert_state(tree, prealloc, start, this_end,
872 bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400873 BUG_ON(err == -EEXIST);
874 if (err) {
875 prealloc = NULL;
876 goto out;
877 }
Chris Mason2c64c532009-09-02 15:04:12 -0400878 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500879 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500880 start = this_end + 1;
881 goto search_again;
882 }
883 /*
884 * | ---- desired range ---- |
885 * | state |
886 * We need to split the extent, and set the bit
887 * on the first half
888 */
889 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400890 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500891 *failed_start = start;
892 err = -EEXIST;
893 goto out;
894 }
895 err = split_state(tree, state, prealloc, end + 1);
896 BUG_ON(err == -EEXIST);
897
Josef Bacik9ed74f22009-09-11 16:12:44 -0400898 err = set_state_bits(tree, prealloc, bits);
899 if (err) {
900 prealloc = NULL;
901 goto out;
902 }
Chris Mason2c64c532009-09-02 15:04:12 -0400903 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500904 merge_state(tree, prealloc);
905 prealloc = NULL;
906 goto out;
907 }
908
909 goto search_again;
910
911out:
Chris Masoncad321a2008-12-17 14:51:42 -0500912 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500913 if (prealloc)
914 free_extent_state(prealloc);
915
916 return err;
917
918search_again:
919 if (start > end)
920 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500921 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500922 if (mask & __GFP_WAIT)
923 cond_resched();
924 goto again;
925}
Chris Masond1310b22008-01-24 16:13:08 -0500926
927/* wrappers around set/clear extent bit */
928int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
929 gfp_t mask)
930{
931 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400932 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500933}
Chris Masond1310b22008-01-24 16:13:08 -0500934
935int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
936 int bits, gfp_t mask)
937{
938 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400939 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500940}
Chris Masond1310b22008-01-24 16:13:08 -0500941
942int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
943 int bits, gfp_t mask)
944{
Chris Mason2c64c532009-09-02 15:04:12 -0400945 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500946}
Chris Masond1310b22008-01-24 16:13:08 -0500947
948int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
949 gfp_t mask)
950{
951 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400952 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Chris Mason2c64c532009-09-02 15:04:12 -0400953 0, NULL, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500954}
Chris Masond1310b22008-01-24 16:13:08 -0500955
956int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
957 gfp_t mask)
958{
959 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400960 EXTENT_DIRTY | EXTENT_DELALLOC |
961 EXTENT_DO_ACCOUNTING, 0, 0,
Chris Mason2c64c532009-09-02 15:04:12 -0400962 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500963}
Chris Masond1310b22008-01-24 16:13:08 -0500964
965int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
966 gfp_t mask)
967{
968 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400969 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500970}
Chris Masond1310b22008-01-24 16:13:08 -0500971
Christoph Hellwigb2950862008-12-02 09:54:17 -0500972static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500973 gfp_t mask)
974{
Chris Mason2c64c532009-09-02 15:04:12 -0400975 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
976 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500977}
Chris Masond1310b22008-01-24 16:13:08 -0500978
979int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
980 gfp_t mask)
981{
982 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400983 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500984}
Chris Masond1310b22008-01-24 16:13:08 -0500985
Chris Masond3977122009-01-05 21:25:51 -0500986static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
987 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500988{
Chris Mason2c64c532009-09-02 15:04:12 -0400989 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
990 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500991}
Chris Masond1310b22008-01-24 16:13:08 -0500992
Chris Masond1310b22008-01-24 16:13:08 -0500993int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
994{
995 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
996}
Chris Masond1310b22008-01-24 16:13:08 -0500997
Chris Masond352ac62008-09-29 15:18:18 -0400998/*
999 * either insert or lock state struct between start and end use mask to tell
1000 * us if waiting is desired.
1001 */
Chris Mason1edbb732009-09-02 13:24:36 -04001002int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001003 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001004{
1005 int err;
1006 u64 failed_start;
1007 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001008 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001009 EXTENT_LOCKED, &failed_start,
1010 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001011 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1012 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1013 start = failed_start;
1014 } else {
1015 break;
1016 }
1017 WARN_ON(start > end);
1018 }
1019 return err;
1020}
Chris Masond1310b22008-01-24 16:13:08 -05001021
Chris Mason1edbb732009-09-02 13:24:36 -04001022int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1023{
Chris Mason2c64c532009-09-02 15:04:12 -04001024 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001025}
1026
Josef Bacik25179202008-10-29 14:49:05 -04001027int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1028 gfp_t mask)
1029{
1030 int err;
1031 u64 failed_start;
1032
Chris Mason2c64c532009-09-02 15:04:12 -04001033 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1034 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001035 if (err == -EEXIST) {
1036 if (failed_start > start)
1037 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001038 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001039 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001040 }
Josef Bacik25179202008-10-29 14:49:05 -04001041 return 1;
1042}
Josef Bacik25179202008-10-29 14:49:05 -04001043
Chris Mason2c64c532009-09-02 15:04:12 -04001044int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1045 struct extent_state **cached, gfp_t mask)
1046{
1047 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1048 mask);
1049}
1050
Chris Masond1310b22008-01-24 16:13:08 -05001051int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1052 gfp_t mask)
1053{
Chris Mason2c64c532009-09-02 15:04:12 -04001054 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1055 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001056}
Chris Masond1310b22008-01-24 16:13:08 -05001057
1058/*
1059 * helper function to set pages and extents in the tree dirty
1060 */
1061int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1062{
1063 unsigned long index = start >> PAGE_CACHE_SHIFT;
1064 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1065 struct page *page;
1066
1067 while (index <= end_index) {
1068 page = find_get_page(tree->mapping, index);
1069 BUG_ON(!page);
1070 __set_page_dirty_nobuffers(page);
1071 page_cache_release(page);
1072 index++;
1073 }
Chris Masond1310b22008-01-24 16:13:08 -05001074 return 0;
1075}
Chris Masond1310b22008-01-24 16:13:08 -05001076
1077/*
1078 * helper function to set both pages and extents in the tree writeback
1079 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001080static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001081{
1082 unsigned long index = start >> PAGE_CACHE_SHIFT;
1083 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1084 struct page *page;
1085
1086 while (index <= end_index) {
1087 page = find_get_page(tree->mapping, index);
1088 BUG_ON(!page);
1089 set_page_writeback(page);
1090 page_cache_release(page);
1091 index++;
1092 }
Chris Masond1310b22008-01-24 16:13:08 -05001093 return 0;
1094}
Chris Masond1310b22008-01-24 16:13:08 -05001095
Chris Masond352ac62008-09-29 15:18:18 -04001096/*
1097 * find the first offset in the io tree with 'bits' set. zero is
1098 * returned if we find something, and *start_ret and *end_ret are
1099 * set to reflect the state struct that was found.
1100 *
1101 * If nothing was found, 1 is returned, < 0 on error
1102 */
Chris Masond1310b22008-01-24 16:13:08 -05001103int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1104 u64 *start_ret, u64 *end_ret, int bits)
1105{
1106 struct rb_node *node;
1107 struct extent_state *state;
1108 int ret = 1;
1109
Chris Masoncad321a2008-12-17 14:51:42 -05001110 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001111 /*
1112 * this search will find all the extents that end after
1113 * our range starts.
1114 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001115 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001116 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001117 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001118
Chris Masond3977122009-01-05 21:25:51 -05001119 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001120 state = rb_entry(node, struct extent_state, rb_node);
1121 if (state->end >= start && (state->state & bits)) {
1122 *start_ret = state->start;
1123 *end_ret = state->end;
1124 ret = 0;
1125 break;
1126 }
1127 node = rb_next(node);
1128 if (!node)
1129 break;
1130 }
1131out:
Chris Masoncad321a2008-12-17 14:51:42 -05001132 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001133 return ret;
1134}
Chris Masond1310b22008-01-24 16:13:08 -05001135
Chris Masond352ac62008-09-29 15:18:18 -04001136/* find the first state struct with 'bits' set after 'start', and
1137 * return it. tree->lock must be held. NULL will returned if
1138 * nothing was found after 'start'
1139 */
Chris Masond7fc6402008-02-18 12:12:38 -05001140struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1141 u64 start, int bits)
1142{
1143 struct rb_node *node;
1144 struct extent_state *state;
1145
1146 /*
1147 * this search will find all the extents that end after
1148 * our range starts.
1149 */
1150 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001151 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001152 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001153
Chris Masond3977122009-01-05 21:25:51 -05001154 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001155 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001156 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001157 return state;
Chris Masond3977122009-01-05 21:25:51 -05001158
Chris Masond7fc6402008-02-18 12:12:38 -05001159 node = rb_next(node);
1160 if (!node)
1161 break;
1162 }
1163out:
1164 return NULL;
1165}
Chris Masond7fc6402008-02-18 12:12:38 -05001166
Chris Masond352ac62008-09-29 15:18:18 -04001167/*
1168 * find a contiguous range of bytes in the file marked as delalloc, not
1169 * more than 'max_bytes'. start and end are used to return the range,
1170 *
1171 * 1 is returned if we find something, 0 if nothing was in the tree
1172 */
Chris Masonc8b97812008-10-29 14:49:59 -04001173static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001174 u64 *start, u64 *end, u64 max_bytes,
1175 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001176{
1177 struct rb_node *node;
1178 struct extent_state *state;
1179 u64 cur_start = *start;
1180 u64 found = 0;
1181 u64 total_bytes = 0;
1182
Chris Masoncad321a2008-12-17 14:51:42 -05001183 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001184
Chris Masond1310b22008-01-24 16:13:08 -05001185 /*
1186 * this search will find all the extents that end after
1187 * our range starts.
1188 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001189 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001190 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001191 if (!found)
1192 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001193 goto out;
1194 }
1195
Chris Masond3977122009-01-05 21:25:51 -05001196 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001197 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001198 if (found && (state->start != cur_start ||
1199 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001200 goto out;
1201 }
1202 if (!(state->state & EXTENT_DELALLOC)) {
1203 if (!found)
1204 *end = state->end;
1205 goto out;
1206 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001207 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001208 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001209 *cached_state = state;
1210 atomic_inc(&state->refs);
1211 }
Chris Masond1310b22008-01-24 16:13:08 -05001212 found++;
1213 *end = state->end;
1214 cur_start = state->end + 1;
1215 node = rb_next(node);
1216 if (!node)
1217 break;
1218 total_bytes += state->end - state->start + 1;
1219 if (total_bytes >= max_bytes)
1220 break;
1221 }
1222out:
Chris Masoncad321a2008-12-17 14:51:42 -05001223 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001224 return found;
1225}
1226
Chris Masonc8b97812008-10-29 14:49:59 -04001227static noinline int __unlock_for_delalloc(struct inode *inode,
1228 struct page *locked_page,
1229 u64 start, u64 end)
1230{
1231 int ret;
1232 struct page *pages[16];
1233 unsigned long index = start >> PAGE_CACHE_SHIFT;
1234 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1235 unsigned long nr_pages = end_index - index + 1;
1236 int i;
1237
1238 if (index == locked_page->index && end_index == index)
1239 return 0;
1240
Chris Masond3977122009-01-05 21:25:51 -05001241 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001242 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001243 min_t(unsigned long, nr_pages,
1244 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001245 for (i = 0; i < ret; i++) {
1246 if (pages[i] != locked_page)
1247 unlock_page(pages[i]);
1248 page_cache_release(pages[i]);
1249 }
1250 nr_pages -= ret;
1251 index += ret;
1252 cond_resched();
1253 }
1254 return 0;
1255}
1256
1257static noinline int lock_delalloc_pages(struct inode *inode,
1258 struct page *locked_page,
1259 u64 delalloc_start,
1260 u64 delalloc_end)
1261{
1262 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1263 unsigned long start_index = index;
1264 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1265 unsigned long pages_locked = 0;
1266 struct page *pages[16];
1267 unsigned long nrpages;
1268 int ret;
1269 int i;
1270
1271 /* the caller is responsible for locking the start index */
1272 if (index == locked_page->index && index == end_index)
1273 return 0;
1274
1275 /* skip the page at the start index */
1276 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001277 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001278 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001279 min_t(unsigned long,
1280 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001281 if (ret == 0) {
1282 ret = -EAGAIN;
1283 goto done;
1284 }
1285 /* now we have an array of pages, lock them all */
1286 for (i = 0; i < ret; i++) {
1287 /*
1288 * the caller is taking responsibility for
1289 * locked_page
1290 */
Chris Mason771ed682008-11-06 22:02:51 -05001291 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001292 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001293 if (!PageDirty(pages[i]) ||
1294 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001295 ret = -EAGAIN;
1296 unlock_page(pages[i]);
1297 page_cache_release(pages[i]);
1298 goto done;
1299 }
1300 }
Chris Masonc8b97812008-10-29 14:49:59 -04001301 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001302 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001303 }
Chris Masonc8b97812008-10-29 14:49:59 -04001304 nrpages -= ret;
1305 index += ret;
1306 cond_resched();
1307 }
1308 ret = 0;
1309done:
1310 if (ret && pages_locked) {
1311 __unlock_for_delalloc(inode, locked_page,
1312 delalloc_start,
1313 ((u64)(start_index + pages_locked - 1)) <<
1314 PAGE_CACHE_SHIFT);
1315 }
1316 return ret;
1317}
1318
1319/*
1320 * find a contiguous range of bytes in the file marked as delalloc, not
1321 * more than 'max_bytes'. start and end are used to return the range,
1322 *
1323 * 1 is returned if we find something, 0 if nothing was in the tree
1324 */
1325static noinline u64 find_lock_delalloc_range(struct inode *inode,
1326 struct extent_io_tree *tree,
1327 struct page *locked_page,
1328 u64 *start, u64 *end,
1329 u64 max_bytes)
1330{
1331 u64 delalloc_start;
1332 u64 delalloc_end;
1333 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001334 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001335 int ret;
1336 int loops = 0;
1337
1338again:
1339 /* step one, find a bunch of delalloc bytes starting at start */
1340 delalloc_start = *start;
1341 delalloc_end = 0;
1342 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001343 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001344 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001345 *start = delalloc_start;
1346 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001347 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001348 return found;
1349 }
1350
1351 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001352 * start comes from the offset of locked_page. We have to lock
1353 * pages in order, so we can't process delalloc bytes before
1354 * locked_page
1355 */
Chris Masond3977122009-01-05 21:25:51 -05001356 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001357 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001358
1359 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001360 * make sure to limit the number of pages we try to lock down
1361 * if we're looping.
1362 */
Chris Masond3977122009-01-05 21:25:51 -05001363 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001364 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001365
Chris Masonc8b97812008-10-29 14:49:59 -04001366 /* step two, lock all the pages after the page that has start */
1367 ret = lock_delalloc_pages(inode, locked_page,
1368 delalloc_start, delalloc_end);
1369 if (ret == -EAGAIN) {
1370 /* some of the pages are gone, lets avoid looping by
1371 * shortening the size of the delalloc range we're searching
1372 */
Chris Mason9655d292009-09-02 15:22:30 -04001373 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001374 if (!loops) {
1375 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1376 max_bytes = PAGE_CACHE_SIZE - offset;
1377 loops = 1;
1378 goto again;
1379 } else {
1380 found = 0;
1381 goto out_failed;
1382 }
1383 }
1384 BUG_ON(ret);
1385
1386 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001387 lock_extent_bits(tree, delalloc_start, delalloc_end,
1388 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001389
1390 /* then test to make sure it is all still delalloc */
1391 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001392 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001393 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001394 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1395 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001396 __unlock_for_delalloc(inode, locked_page,
1397 delalloc_start, delalloc_end);
1398 cond_resched();
1399 goto again;
1400 }
Chris Mason9655d292009-09-02 15:22:30 -04001401 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001402 *start = delalloc_start;
1403 *end = delalloc_end;
1404out_failed:
1405 return found;
1406}
1407
1408int extent_clear_unlock_delalloc(struct inode *inode,
1409 struct extent_io_tree *tree,
1410 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001411 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001412{
1413 int ret;
1414 struct page *pages[16];
1415 unsigned long index = start >> PAGE_CACHE_SHIFT;
1416 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1417 unsigned long nr_pages = end_index - index + 1;
1418 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001419 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001420
Chris Masona791e352009-10-08 11:27:10 -04001421 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001422 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001423 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001424 clear_bits |= EXTENT_DIRTY;
1425
Chris Masona791e352009-10-08 11:27:10 -04001426 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001427 clear_bits |= EXTENT_DELALLOC;
1428
Josef Bacik32c00af2009-10-08 13:34:05 -04001429 if (op & EXTENT_CLEAR_ACCOUNTING)
1430 clear_bits |= EXTENT_DO_ACCOUNTING;
1431
Chris Mason2c64c532009-09-02 15:04:12 -04001432 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001433 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1434 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1435 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001436 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001437
Chris Masond3977122009-01-05 21:25:51 -05001438 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001439 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001440 min_t(unsigned long,
1441 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001442 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001443
Chris Masona791e352009-10-08 11:27:10 -04001444 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001445 SetPagePrivate2(pages[i]);
1446
Chris Masonc8b97812008-10-29 14:49:59 -04001447 if (pages[i] == locked_page) {
1448 page_cache_release(pages[i]);
1449 continue;
1450 }
Chris Masona791e352009-10-08 11:27:10 -04001451 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001452 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001453 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001454 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001455 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001456 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001457 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001458 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001459 page_cache_release(pages[i]);
1460 }
1461 nr_pages -= ret;
1462 index += ret;
1463 cond_resched();
1464 }
1465 return 0;
1466}
Chris Masonc8b97812008-10-29 14:49:59 -04001467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
1469 * count the number of bytes in the tree that have a given bit(s)
1470 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1471 * cached. The total number found is returned.
1472 */
Chris Masond1310b22008-01-24 16:13:08 -05001473u64 count_range_bits(struct extent_io_tree *tree,
1474 u64 *start, u64 search_end, u64 max_bytes,
1475 unsigned long bits)
1476{
1477 struct rb_node *node;
1478 struct extent_state *state;
1479 u64 cur_start = *start;
1480 u64 total_bytes = 0;
1481 int found = 0;
1482
1483 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001484 WARN_ON(1);
1485 return 0;
1486 }
1487
Chris Masoncad321a2008-12-17 14:51:42 -05001488 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001489 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1490 total_bytes = tree->dirty_bytes;
1491 goto out;
1492 }
1493 /*
1494 * this search will find all the extents that end after
1495 * our range starts.
1496 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001497 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001498 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001499 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001500
Chris Masond3977122009-01-05 21:25:51 -05001501 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001502 state = rb_entry(node, struct extent_state, rb_node);
1503 if (state->start > search_end)
1504 break;
1505 if (state->end >= cur_start && (state->state & bits)) {
1506 total_bytes += min(search_end, state->end) + 1 -
1507 max(cur_start, state->start);
1508 if (total_bytes >= max_bytes)
1509 break;
1510 if (!found) {
1511 *start = state->start;
1512 found = 1;
1513 }
1514 }
1515 node = rb_next(node);
1516 if (!node)
1517 break;
1518 }
1519out:
Chris Masoncad321a2008-12-17 14:51:42 -05001520 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001521 return total_bytes;
1522}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001523
Chris Masond352ac62008-09-29 15:18:18 -04001524/*
1525 * set the private field for a given byte offset in the tree. If there isn't
1526 * an extent_state there already, this does nothing.
1527 */
Chris Masond1310b22008-01-24 16:13:08 -05001528int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1529{
1530 struct rb_node *node;
1531 struct extent_state *state;
1532 int ret = 0;
1533
Chris Masoncad321a2008-12-17 14:51:42 -05001534 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001535 /*
1536 * this search will find all the extents that end after
1537 * our range starts.
1538 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001539 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001540 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001541 ret = -ENOENT;
1542 goto out;
1543 }
1544 state = rb_entry(node, struct extent_state, rb_node);
1545 if (state->start != start) {
1546 ret = -ENOENT;
1547 goto out;
1548 }
1549 state->private = private;
1550out:
Chris Masoncad321a2008-12-17 14:51:42 -05001551 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001552 return ret;
1553}
1554
1555int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1556{
1557 struct rb_node *node;
1558 struct extent_state *state;
1559 int ret = 0;
1560
Chris Masoncad321a2008-12-17 14:51:42 -05001561 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001562 /*
1563 * this search will find all the extents that end after
1564 * our range starts.
1565 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001566 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001567 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001568 ret = -ENOENT;
1569 goto out;
1570 }
1571 state = rb_entry(node, struct extent_state, rb_node);
1572 if (state->start != start) {
1573 ret = -ENOENT;
1574 goto out;
1575 }
1576 *private = state->private;
1577out:
Chris Masoncad321a2008-12-17 14:51:42 -05001578 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001579 return ret;
1580}
1581
1582/*
1583 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001584 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001585 * has the bits set. Otherwise, 1 is returned if any bit in the
1586 * range is found set.
1587 */
1588int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001589 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001590{
1591 struct extent_state *state = NULL;
1592 struct rb_node *node;
1593 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001594
Chris Masoncad321a2008-12-17 14:51:42 -05001595 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001596 if (cached && cached->tree && cached->start == start)
1597 node = &cached->rb_node;
1598 else
1599 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001600 while (node && start <= end) {
1601 state = rb_entry(node, struct extent_state, rb_node);
1602
1603 if (filled && state->start > start) {
1604 bitset = 0;
1605 break;
1606 }
1607
1608 if (state->start > end)
1609 break;
1610
1611 if (state->state & bits) {
1612 bitset = 1;
1613 if (!filled)
1614 break;
1615 } else if (filled) {
1616 bitset = 0;
1617 break;
1618 }
Chris Mason46562ce2009-09-23 20:23:16 -04001619
1620 if (state->end == (u64)-1)
1621 break;
1622
Chris Masond1310b22008-01-24 16:13:08 -05001623 start = state->end + 1;
1624 if (start > end)
1625 break;
1626 node = rb_next(node);
1627 if (!node) {
1628 if (filled)
1629 bitset = 0;
1630 break;
1631 }
1632 }
Chris Masoncad321a2008-12-17 14:51:42 -05001633 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001634 return bitset;
1635}
Chris Masond1310b22008-01-24 16:13:08 -05001636
1637/*
1638 * helper function to set a given page up to date if all the
1639 * extents in the tree for that page are up to date
1640 */
1641static int check_page_uptodate(struct extent_io_tree *tree,
1642 struct page *page)
1643{
1644 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1645 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001646 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001647 SetPageUptodate(page);
1648 return 0;
1649}
1650
1651/*
1652 * helper function to unlock a page if all the extents in the tree
1653 * for that page are unlocked
1654 */
1655static int check_page_locked(struct extent_io_tree *tree,
1656 struct page *page)
1657{
1658 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1659 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001660 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001661 unlock_page(page);
1662 return 0;
1663}
1664
1665/*
1666 * helper function to end page writeback if all the extents
1667 * in the tree for that page are done with writeback
1668 */
1669static int check_page_writeback(struct extent_io_tree *tree,
1670 struct page *page)
1671{
Chris Mason1edbb732009-09-02 13:24:36 -04001672 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001673 return 0;
1674}
1675
1676/* lots and lots of room for performance fixes in the end_bio funcs */
1677
1678/*
1679 * after a writepage IO is done, we need to:
1680 * clear the uptodate bits on error
1681 * clear the writeback bits in the extent tree for this IO
1682 * end_page_writeback if the page has no more pending IO
1683 *
1684 * Scheduling is not allowed, so the extent state tree is expected
1685 * to have one and only one object corresponding to this IO.
1686 */
Chris Masond1310b22008-01-24 16:13:08 -05001687static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001688{
Chris Mason1259ab72008-05-12 13:39:03 -04001689 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001690 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001691 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001692 u64 start;
1693 u64 end;
1694 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001695 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001696
Chris Masond1310b22008-01-24 16:13:08 -05001697 do {
1698 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001699 tree = &BTRFS_I(page->mapping->host)->io_tree;
1700
Chris Masond1310b22008-01-24 16:13:08 -05001701 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1702 bvec->bv_offset;
1703 end = start + bvec->bv_len - 1;
1704
1705 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1706 whole_page = 1;
1707 else
1708 whole_page = 0;
1709
1710 if (--bvec >= bio->bi_io_vec)
1711 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001712 if (tree->ops && tree->ops->writepage_end_io_hook) {
1713 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001714 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001715 if (ret)
1716 uptodate = 0;
1717 }
1718
1719 if (!uptodate && tree->ops &&
1720 tree->ops->writepage_io_failed_hook) {
1721 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001722 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001723 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001724 uptodate = (err == 0);
1725 continue;
1726 }
1727 }
1728
Chris Masond1310b22008-01-24 16:13:08 -05001729 if (!uptodate) {
Chris Mason1edbb732009-09-02 13:24:36 -04001730 clear_extent_uptodate(tree, start, end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001731 ClearPageUptodate(page);
1732 SetPageError(page);
1733 }
Chris Mason70dec802008-01-29 09:59:12 -05001734
Chris Masond1310b22008-01-24 16:13:08 -05001735 if (whole_page)
1736 end_page_writeback(page);
1737 else
1738 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001739 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001740
Chris Masond1310b22008-01-24 16:13:08 -05001741 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001742}
1743
1744/*
1745 * after a readpage IO is done, we need to:
1746 * clear the uptodate bits on error
1747 * set the uptodate bits if things worked
1748 * set the page up to date if all extents in the tree are uptodate
1749 * clear the lock bit in the extent tree
1750 * unlock the page if there are no other extents locked for it
1751 *
1752 * Scheduling is not allowed, so the extent state tree is expected
1753 * to have one and only one object corresponding to this IO.
1754 */
Chris Masond1310b22008-01-24 16:13:08 -05001755static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001756{
1757 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001758 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1759 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001760 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001761 u64 start;
1762 u64 end;
1763 int whole_page;
1764 int ret;
1765
Chris Masond20f7042008-12-08 16:58:54 -05001766 if (err)
1767 uptodate = 0;
1768
Chris Masond1310b22008-01-24 16:13:08 -05001769 do {
1770 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001771 tree = &BTRFS_I(page->mapping->host)->io_tree;
1772
Chris Masond1310b22008-01-24 16:13:08 -05001773 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1774 bvec->bv_offset;
1775 end = start + bvec->bv_len - 1;
1776
1777 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1778 whole_page = 1;
1779 else
1780 whole_page = 0;
1781
Chris Mason4125bf72010-02-03 18:18:45 +00001782 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001783 prefetchw(&bvec->bv_page->flags);
1784
1785 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001786 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001787 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001788 if (ret)
1789 uptodate = 0;
1790 }
Chris Mason7e383262008-04-09 16:28:12 -04001791 if (!uptodate && tree->ops &&
1792 tree->ops->readpage_io_failed_hook) {
1793 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001794 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001795 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001796 uptodate =
1797 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001798 if (err)
1799 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001800 continue;
1801 }
1802 }
Chris Mason70dec802008-01-29 09:59:12 -05001803
Chris Mason771ed682008-11-06 22:02:51 -05001804 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001805 set_extent_uptodate(tree, start, end,
1806 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001807 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001808 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001809
Chris Mason70dec802008-01-29 09:59:12 -05001810 if (whole_page) {
1811 if (uptodate) {
1812 SetPageUptodate(page);
1813 } else {
1814 ClearPageUptodate(page);
1815 SetPageError(page);
1816 }
Chris Masond1310b22008-01-24 16:13:08 -05001817 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001818 } else {
1819 if (uptodate) {
1820 check_page_uptodate(tree, page);
1821 } else {
1822 ClearPageUptodate(page);
1823 SetPageError(page);
1824 }
Chris Masond1310b22008-01-24 16:13:08 -05001825 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001826 }
Chris Mason4125bf72010-02-03 18:18:45 +00001827 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001828
1829 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001830}
1831
1832/*
1833 * IO done from prepare_write is pretty simple, we just unlock
1834 * the structs in the extent tree when done, and set the uptodate bits
1835 * as appropriate.
1836 */
Chris Masond1310b22008-01-24 16:13:08 -05001837static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001838{
1839 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1840 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001841 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001842 u64 start;
1843 u64 end;
1844
Chris Masond1310b22008-01-24 16:13:08 -05001845 do {
1846 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001847 tree = &BTRFS_I(page->mapping->host)->io_tree;
1848
Chris Masond1310b22008-01-24 16:13:08 -05001849 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1850 bvec->bv_offset;
1851 end = start + bvec->bv_len - 1;
1852
1853 if (--bvec >= bio->bi_io_vec)
1854 prefetchw(&bvec->bv_page->flags);
1855
1856 if (uptodate) {
1857 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1858 } else {
1859 ClearPageUptodate(page);
1860 SetPageError(page);
1861 }
1862
1863 unlock_extent(tree, start, end, GFP_ATOMIC);
1864
1865 } while (bvec >= bio->bi_io_vec);
1866
1867 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001868}
1869
1870static struct bio *
1871extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1872 gfp_t gfp_flags)
1873{
1874 struct bio *bio;
1875
1876 bio = bio_alloc(gfp_flags, nr_vecs);
1877
1878 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1879 while (!bio && (nr_vecs /= 2))
1880 bio = bio_alloc(gfp_flags, nr_vecs);
1881 }
1882
1883 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001884 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001885 bio->bi_bdev = bdev;
1886 bio->bi_sector = first_sector;
1887 }
1888 return bio;
1889}
1890
Chris Masonc8b97812008-10-29 14:49:59 -04001891static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1892 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001893{
Chris Masond1310b22008-01-24 16:13:08 -05001894 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001895 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1896 struct page *page = bvec->bv_page;
1897 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001898 u64 start;
1899 u64 end;
1900
1901 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1902 end = start + bvec->bv_len - 1;
1903
David Woodhouse902b22f2008-08-20 08:51:49 -04001904 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001905
1906 bio_get(bio);
1907
Chris Mason065631f2008-02-20 12:07:25 -05001908 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001909 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001910 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001911 else
1912 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001913 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1914 ret = -EOPNOTSUPP;
1915 bio_put(bio);
1916 return ret;
1917}
1918
1919static int submit_extent_page(int rw, struct extent_io_tree *tree,
1920 struct page *page, sector_t sector,
1921 size_t size, unsigned long offset,
1922 struct block_device *bdev,
1923 struct bio **bio_ret,
1924 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001925 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001926 int mirror_num,
1927 unsigned long prev_bio_flags,
1928 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001929{
1930 int ret = 0;
1931 struct bio *bio;
1932 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001933 int contig = 0;
1934 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1935 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001936 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001937
1938 if (bio_ret && *bio_ret) {
1939 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001940 if (old_compressed)
1941 contig = bio->bi_sector == sector;
1942 else
1943 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1944 sector;
1945
1946 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001947 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001948 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1949 bio_flags)) ||
1950 bio_add_page(bio, page, page_size, offset) < page_size) {
1951 ret = submit_one_bio(rw, bio, mirror_num,
1952 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001953 bio = NULL;
1954 } else {
1955 return 0;
1956 }
1957 }
Chris Masonc8b97812008-10-29 14:49:59 -04001958 if (this_compressed)
1959 nr = BIO_MAX_PAGES;
1960 else
1961 nr = bio_get_nr_vecs(bdev);
1962
Chris Masond1310b22008-01-24 16:13:08 -05001963 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001964
Chris Masonc8b97812008-10-29 14:49:59 -04001965 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001966 bio->bi_end_io = end_io_func;
1967 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001968
Chris Masond3977122009-01-05 21:25:51 -05001969 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001970 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001971 else
Chris Masonc8b97812008-10-29 14:49:59 -04001972 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001973
1974 return ret;
1975}
1976
1977void set_page_extent_mapped(struct page *page)
1978{
1979 if (!PagePrivate(page)) {
1980 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001981 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001982 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001983 }
1984}
1985
Christoph Hellwigb2950862008-12-02 09:54:17 -05001986static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001987{
1988 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1989}
1990
1991/*
1992 * basic readpage implementation. Locked extent state structs are inserted
1993 * into the tree that are removed when the IO is done (by the end_io
1994 * handlers)
1995 */
1996static int __extent_read_full_page(struct extent_io_tree *tree,
1997 struct page *page,
1998 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001999 struct bio **bio, int mirror_num,
2000 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002001{
2002 struct inode *inode = page->mapping->host;
2003 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2004 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2005 u64 end;
2006 u64 cur = start;
2007 u64 extent_offset;
2008 u64 last_byte = i_size_read(inode);
2009 u64 block_start;
2010 u64 cur_end;
2011 sector_t sector;
2012 struct extent_map *em;
2013 struct block_device *bdev;
2014 int ret;
2015 int nr = 0;
2016 size_t page_offset = 0;
2017 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002018 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002019 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002020 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002021
2022 set_page_extent_mapped(page);
2023
2024 end = page_end;
2025 lock_extent(tree, start, end, GFP_NOFS);
2026
Chris Masonc8b97812008-10-29 14:49:59 -04002027 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2028 char *userpage;
2029 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2030
2031 if (zero_offset) {
2032 iosize = PAGE_CACHE_SIZE - zero_offset;
2033 userpage = kmap_atomic(page, KM_USER0);
2034 memset(userpage + zero_offset, 0, iosize);
2035 flush_dcache_page(page);
2036 kunmap_atomic(userpage, KM_USER0);
2037 }
2038 }
Chris Masond1310b22008-01-24 16:13:08 -05002039 while (cur <= end) {
2040 if (cur >= last_byte) {
2041 char *userpage;
2042 iosize = PAGE_CACHE_SIZE - page_offset;
2043 userpage = kmap_atomic(page, KM_USER0);
2044 memset(userpage + page_offset, 0, iosize);
2045 flush_dcache_page(page);
2046 kunmap_atomic(userpage, KM_USER0);
2047 set_extent_uptodate(tree, cur, cur + iosize - 1,
2048 GFP_NOFS);
2049 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2050 break;
2051 }
2052 em = get_extent(inode, page, page_offset, cur,
2053 end - cur + 1, 0);
2054 if (IS_ERR(em) || !em) {
2055 SetPageError(page);
2056 unlock_extent(tree, cur, end, GFP_NOFS);
2057 break;
2058 }
Chris Masond1310b22008-01-24 16:13:08 -05002059 extent_offset = cur - em->start;
2060 BUG_ON(extent_map_end(em) <= cur);
2061 BUG_ON(end < cur);
2062
Chris Masonc8b97812008-10-29 14:49:59 -04002063 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2064 this_bio_flag = EXTENT_BIO_COMPRESSED;
2065
Chris Masond1310b22008-01-24 16:13:08 -05002066 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2067 cur_end = min(extent_map_end(em) - 1, end);
2068 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002069 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2070 disk_io_size = em->block_len;
2071 sector = em->block_start >> 9;
2072 } else {
2073 sector = (em->block_start + extent_offset) >> 9;
2074 disk_io_size = iosize;
2075 }
Chris Masond1310b22008-01-24 16:13:08 -05002076 bdev = em->bdev;
2077 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002078 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2079 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002080 free_extent_map(em);
2081 em = NULL;
2082
2083 /* we've found a hole, just zero and go on */
2084 if (block_start == EXTENT_MAP_HOLE) {
2085 char *userpage;
2086 userpage = kmap_atomic(page, KM_USER0);
2087 memset(userpage + page_offset, 0, iosize);
2088 flush_dcache_page(page);
2089 kunmap_atomic(userpage, KM_USER0);
2090
2091 set_extent_uptodate(tree, cur, cur + iosize - 1,
2092 GFP_NOFS);
2093 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2094 cur = cur + iosize;
2095 page_offset += iosize;
2096 continue;
2097 }
2098 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002099 if (test_range_bit(tree, cur, cur_end,
2100 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002101 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002102 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2103 cur = cur + iosize;
2104 page_offset += iosize;
2105 continue;
2106 }
Chris Mason70dec802008-01-29 09:59:12 -05002107 /* we have an inline extent but it didn't get marked up
2108 * to date. Error out
2109 */
2110 if (block_start == EXTENT_MAP_INLINE) {
2111 SetPageError(page);
2112 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2113 cur = cur + iosize;
2114 page_offset += iosize;
2115 continue;
2116 }
Chris Masond1310b22008-01-24 16:13:08 -05002117
2118 ret = 0;
2119 if (tree->ops && tree->ops->readpage_io_hook) {
2120 ret = tree->ops->readpage_io_hook(page, cur,
2121 cur + iosize - 1);
2122 }
2123 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002124 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2125 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002126 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002127 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002128 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002129 end_bio_extent_readpage, mirror_num,
2130 *bio_flags,
2131 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002132 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002133 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002134 }
2135 if (ret)
2136 SetPageError(page);
2137 cur = cur + iosize;
2138 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002139 }
2140 if (!nr) {
2141 if (!PageError(page))
2142 SetPageUptodate(page);
2143 unlock_page(page);
2144 }
2145 return 0;
2146}
2147
2148int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2149 get_extent_t *get_extent)
2150{
2151 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002152 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002153 int ret;
2154
Chris Masonc8b97812008-10-29 14:49:59 -04002155 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2156 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002157 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002158 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002159 return ret;
2160}
Chris Masond1310b22008-01-24 16:13:08 -05002161
Chris Mason11c83492009-04-20 15:50:09 -04002162static noinline void update_nr_written(struct page *page,
2163 struct writeback_control *wbc,
2164 unsigned long nr_written)
2165{
2166 wbc->nr_to_write -= nr_written;
2167 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2168 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2169 page->mapping->writeback_index = page->index + nr_written;
2170}
2171
Chris Masond1310b22008-01-24 16:13:08 -05002172/*
2173 * the writepage semantics are similar to regular writepage. extent
2174 * records are inserted to lock ranges in the tree, and as dirty areas
2175 * are found, they are marked writeback. Then the lock bits are removed
2176 * and the end_io handler clears the writeback ranges
2177 */
2178static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2179 void *data)
2180{
2181 struct inode *inode = page->mapping->host;
2182 struct extent_page_data *epd = data;
2183 struct extent_io_tree *tree = epd->tree;
2184 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2185 u64 delalloc_start;
2186 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2187 u64 end;
2188 u64 cur = start;
2189 u64 extent_offset;
2190 u64 last_byte = i_size_read(inode);
2191 u64 block_start;
2192 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002193 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002194 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002195 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002196 struct extent_map *em;
2197 struct block_device *bdev;
2198 int ret;
2199 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002200 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002201 size_t blocksize;
2202 loff_t i_size = i_size_read(inode);
2203 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2204 u64 nr_delalloc;
2205 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002206 int page_started;
2207 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002208 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002209 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002210
Chris Masonffbd5172009-04-20 15:50:09 -04002211 if (wbc->sync_mode == WB_SYNC_ALL)
2212 write_flags = WRITE_SYNC_PLUG;
2213 else
2214 write_flags = WRITE;
2215
Chris Masond1310b22008-01-24 16:13:08 -05002216 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002217 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002218 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002219 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002220 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002221 unlock_page(page);
2222 return 0;
2223 }
2224
2225 if (page->index == end_index) {
2226 char *userpage;
2227
Chris Masond1310b22008-01-24 16:13:08 -05002228 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002229 memset(userpage + pg_offset, 0,
2230 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002231 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002232 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002233 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002234 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002235
2236 set_page_extent_mapped(page);
2237
2238 delalloc_start = start;
2239 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002240 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002241 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002242 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002243 /*
2244 * make sure the wbc mapping index is at least updated
2245 * to this page.
2246 */
2247 update_nr_written(page, wbc, 0);
2248
Chris Masond3977122009-01-05 21:25:51 -05002249 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002250 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002251 page,
2252 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002253 &delalloc_end,
2254 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002255 if (nr_delalloc == 0) {
2256 delalloc_start = delalloc_end + 1;
2257 continue;
2258 }
2259 tree->ops->fill_delalloc(inode, page, delalloc_start,
2260 delalloc_end, &page_started,
2261 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002262 /*
2263 * delalloc_end is already one less than the total
2264 * length, so we don't subtract one from
2265 * PAGE_CACHE_SIZE
2266 */
2267 delalloc_to_write += (delalloc_end - delalloc_start +
2268 PAGE_CACHE_SIZE) >>
2269 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002270 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002271 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002272 if (wbc->nr_to_write < delalloc_to_write) {
2273 int thresh = 8192;
2274
2275 if (delalloc_to_write < thresh * 2)
2276 thresh = delalloc_to_write;
2277 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2278 thresh);
2279 }
Chris Masonc8b97812008-10-29 14:49:59 -04002280
Chris Mason771ed682008-11-06 22:02:51 -05002281 /* did the fill delalloc function already unlock and start
2282 * the IO?
2283 */
2284 if (page_started) {
2285 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002286 /*
2287 * we've unlocked the page, so we can't update
2288 * the mapping's writeback index, just update
2289 * nr_to_write.
2290 */
2291 wbc->nr_to_write -= nr_written;
2292 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002293 }
Chris Masonc8b97812008-10-29 14:49:59 -04002294 }
Chris Mason247e7432008-07-17 12:53:51 -04002295 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002296 ret = tree->ops->writepage_start_hook(page, start,
2297 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002298 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002299 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002300 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002301 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002302 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002303 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002304 }
2305 }
2306
Chris Mason11c83492009-04-20 15:50:09 -04002307 /*
2308 * we don't want to touch the inode after unlocking the page,
2309 * so we update the mapping writeback index now
2310 */
2311 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002312
Chris Masond1310b22008-01-24 16:13:08 -05002313 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002314 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002315 if (tree->ops && tree->ops->writepage_end_io_hook)
2316 tree->ops->writepage_end_io_hook(page, start,
2317 page_end, NULL, 1);
2318 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002319 goto done;
2320 }
2321
Chris Masond1310b22008-01-24 16:13:08 -05002322 blocksize = inode->i_sb->s_blocksize;
2323
2324 while (cur <= end) {
2325 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002326 if (tree->ops && tree->ops->writepage_end_io_hook)
2327 tree->ops->writepage_end_io_hook(page, cur,
2328 page_end, NULL, 1);
2329 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002330 break;
2331 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002332 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002333 end - cur + 1, 1);
2334 if (IS_ERR(em) || !em) {
2335 SetPageError(page);
2336 break;
2337 }
2338
2339 extent_offset = cur - em->start;
2340 BUG_ON(extent_map_end(em) <= cur);
2341 BUG_ON(end < cur);
2342 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2343 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2344 sector = (em->block_start + extent_offset) >> 9;
2345 bdev = em->bdev;
2346 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002347 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002348 free_extent_map(em);
2349 em = NULL;
2350
Chris Masonc8b97812008-10-29 14:49:59 -04002351 /*
2352 * compressed and inline extents are written through other
2353 * paths in the FS
2354 */
2355 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002356 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002357 /*
2358 * end_io notification does not happen here for
2359 * compressed extents
2360 */
2361 if (!compressed && tree->ops &&
2362 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002363 tree->ops->writepage_end_io_hook(page, cur,
2364 cur + iosize - 1,
2365 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002366 else if (compressed) {
2367 /* we don't want to end_page_writeback on
2368 * a compressed extent. this happens
2369 * elsewhere
2370 */
2371 nr++;
2372 }
2373
2374 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002375 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002376 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002377 continue;
2378 }
Chris Masond1310b22008-01-24 16:13:08 -05002379 /* leave this out until we have a page_mkwrite call */
2380 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002381 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002382 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002383 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002384 continue;
2385 }
Chris Masonc8b97812008-10-29 14:49:59 -04002386
Chris Masond1310b22008-01-24 16:13:08 -05002387 if (tree->ops && tree->ops->writepage_io_hook) {
2388 ret = tree->ops->writepage_io_hook(page, cur,
2389 cur + iosize - 1);
2390 } else {
2391 ret = 0;
2392 }
Chris Mason1259ab72008-05-12 13:39:03 -04002393 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002394 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002395 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002396 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002397
Chris Masond1310b22008-01-24 16:13:08 -05002398 set_range_writeback(tree, cur, cur + iosize - 1);
2399 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002400 printk(KERN_ERR "btrfs warning page %lu not "
2401 "writeback, cur %llu end %llu\n",
2402 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002403 (unsigned long long)end);
2404 }
2405
Chris Masonffbd5172009-04-20 15:50:09 -04002406 ret = submit_extent_page(write_flags, tree, page,
2407 sector, iosize, pg_offset,
2408 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002409 end_bio_extent_writepage,
2410 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002411 if (ret)
2412 SetPageError(page);
2413 }
2414 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002415 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002416 nr++;
2417 }
2418done:
2419 if (nr == 0) {
2420 /* make sure the mapping tag for page dirty gets cleared */
2421 set_page_writeback(page);
2422 end_page_writeback(page);
2423 }
Chris Masond1310b22008-01-24 16:13:08 -05002424 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002425
Chris Mason11c83492009-04-20 15:50:09 -04002426done_unlocked:
2427
Chris Mason2c64c532009-09-02 15:04:12 -04002428 /* drop our reference on any cached states */
2429 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002430 return 0;
2431}
2432
Chris Masond1310b22008-01-24 16:13:08 -05002433/**
Chris Mason4bef0842008-09-08 11:18:08 -04002434 * 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 -05002435 * @mapping: address space structure to write
2436 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2437 * @writepage: function called for each page
2438 * @data: data passed to writepage function
2439 *
2440 * If a page is already under I/O, write_cache_pages() skips it, even
2441 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2442 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2443 * and msync() need to guarantee that all the data which was dirty at the time
2444 * the call was made get new I/O started against them. If wbc->sync_mode is
2445 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2446 * existing IO to complete.
2447 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002448static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002449 struct address_space *mapping,
2450 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002451 writepage_t writepage, void *data,
2452 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002453{
Chris Masond1310b22008-01-24 16:13:08 -05002454 int ret = 0;
2455 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002456 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002457 struct pagevec pvec;
2458 int nr_pages;
2459 pgoff_t index;
2460 pgoff_t end; /* Inclusive */
2461 int scanned = 0;
2462 int range_whole = 0;
2463
Chris Masond1310b22008-01-24 16:13:08 -05002464 pagevec_init(&pvec, 0);
2465 if (wbc->range_cyclic) {
2466 index = mapping->writeback_index; /* Start from prev offset */
2467 end = -1;
2468 } else {
2469 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2470 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2471 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2472 range_whole = 1;
2473 scanned = 1;
2474 }
2475retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002476 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002477 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002478 PAGECACHE_TAG_DIRTY, min(end - index,
2479 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002480 unsigned i;
2481
2482 scanned = 1;
2483 for (i = 0; i < nr_pages; i++) {
2484 struct page *page = pvec.pages[i];
2485
2486 /*
2487 * At this point we hold neither mapping->tree_lock nor
2488 * lock on the page itself: the page may be truncated or
2489 * invalidated (changing page->mapping to NULL), or even
2490 * swizzled back from swapper_space to tmpfs file
2491 * mapping
2492 */
Chris Mason4bef0842008-09-08 11:18:08 -04002493 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2494 tree->ops->write_cache_pages_lock_hook(page);
2495 else
2496 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002497
2498 if (unlikely(page->mapping != mapping)) {
2499 unlock_page(page);
2500 continue;
2501 }
2502
2503 if (!wbc->range_cyclic && page->index > end) {
2504 done = 1;
2505 unlock_page(page);
2506 continue;
2507 }
2508
Chris Masond2c3f4f2008-11-19 12:44:22 -05002509 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002510 if (PageWriteback(page))
2511 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002512 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002513 }
Chris Masond1310b22008-01-24 16:13:08 -05002514
2515 if (PageWriteback(page) ||
2516 !clear_page_dirty_for_io(page)) {
2517 unlock_page(page);
2518 continue;
2519 }
2520
2521 ret = (*writepage)(page, wbc, data);
2522
2523 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2524 unlock_page(page);
2525 ret = 0;
2526 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002527 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002528 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002529
2530 /*
2531 * the filesystem may choose to bump up nr_to_write.
2532 * We have to make sure to honor the new nr_to_write
2533 * at any time
2534 */
2535 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002536 }
2537 pagevec_release(&pvec);
2538 cond_resched();
2539 }
2540 if (!scanned && !done) {
2541 /*
2542 * We hit the last page and there is more work to be done: wrap
2543 * back to the start of the file
2544 */
2545 scanned = 1;
2546 index = 0;
2547 goto retry;
2548 }
Chris Masond1310b22008-01-24 16:13:08 -05002549 return ret;
2550}
Chris Masond1310b22008-01-24 16:13:08 -05002551
Chris Masonffbd5172009-04-20 15:50:09 -04002552static void flush_epd_write_bio(struct extent_page_data *epd)
2553{
2554 if (epd->bio) {
2555 if (epd->sync_io)
2556 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2557 else
2558 submit_one_bio(WRITE, epd->bio, 0, 0);
2559 epd->bio = NULL;
2560 }
2561}
2562
Chris Masond2c3f4f2008-11-19 12:44:22 -05002563static noinline void flush_write_bio(void *data)
2564{
2565 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002566 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002567}
2568
Chris Masond1310b22008-01-24 16:13:08 -05002569int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2570 get_extent_t *get_extent,
2571 struct writeback_control *wbc)
2572{
2573 int ret;
2574 struct address_space *mapping = page->mapping;
2575 struct extent_page_data epd = {
2576 .bio = NULL,
2577 .tree = tree,
2578 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002579 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002580 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002581 };
2582 struct writeback_control wbc_writepages = {
2583 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002584 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002585 .older_than_this = NULL,
2586 .nr_to_write = 64,
2587 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2588 .range_end = (loff_t)-1,
2589 };
2590
Chris Masond1310b22008-01-24 16:13:08 -05002591 ret = __extent_writepage(page, wbc, &epd);
2592
Chris Mason4bef0842008-09-08 11:18:08 -04002593 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002594 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002595 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002596 return ret;
2597}
Chris Masond1310b22008-01-24 16:13:08 -05002598
Chris Mason771ed682008-11-06 22:02:51 -05002599int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2600 u64 start, u64 end, get_extent_t *get_extent,
2601 int mode)
2602{
2603 int ret = 0;
2604 struct address_space *mapping = inode->i_mapping;
2605 struct page *page;
2606 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2607 PAGE_CACHE_SHIFT;
2608
2609 struct extent_page_data epd = {
2610 .bio = NULL,
2611 .tree = tree,
2612 .get_extent = get_extent,
2613 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002614 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002615 };
2616 struct writeback_control wbc_writepages = {
2617 .bdi = inode->i_mapping->backing_dev_info,
2618 .sync_mode = mode,
2619 .older_than_this = NULL,
2620 .nr_to_write = nr_pages * 2,
2621 .range_start = start,
2622 .range_end = end + 1,
2623 };
2624
Chris Masond3977122009-01-05 21:25:51 -05002625 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002626 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2627 if (clear_page_dirty_for_io(page))
2628 ret = __extent_writepage(page, &wbc_writepages, &epd);
2629 else {
2630 if (tree->ops && tree->ops->writepage_end_io_hook)
2631 tree->ops->writepage_end_io_hook(page, start,
2632 start + PAGE_CACHE_SIZE - 1,
2633 NULL, 1);
2634 unlock_page(page);
2635 }
2636 page_cache_release(page);
2637 start += PAGE_CACHE_SIZE;
2638 }
2639
Chris Masonffbd5172009-04-20 15:50:09 -04002640 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002641 return ret;
2642}
Chris Masond1310b22008-01-24 16:13:08 -05002643
2644int extent_writepages(struct extent_io_tree *tree,
2645 struct address_space *mapping,
2646 get_extent_t *get_extent,
2647 struct writeback_control *wbc)
2648{
2649 int ret = 0;
2650 struct extent_page_data epd = {
2651 .bio = NULL,
2652 .tree = tree,
2653 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002654 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002655 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002656 };
2657
Chris Mason4bef0842008-09-08 11:18:08 -04002658 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002659 __extent_writepage, &epd,
2660 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002661 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002662 return ret;
2663}
Chris Masond1310b22008-01-24 16:13:08 -05002664
2665int extent_readpages(struct extent_io_tree *tree,
2666 struct address_space *mapping,
2667 struct list_head *pages, unsigned nr_pages,
2668 get_extent_t get_extent)
2669{
2670 struct bio *bio = NULL;
2671 unsigned page_idx;
2672 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002673 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002674
2675 pagevec_init(&pvec, 0);
2676 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2677 struct page *page = list_entry(pages->prev, struct page, lru);
2678
2679 prefetchw(&page->flags);
2680 list_del(&page->lru);
2681 /*
2682 * what we want to do here is call add_to_page_cache_lru,
2683 * but that isn't exported, so we reproduce it here
2684 */
2685 if (!add_to_page_cache(page, mapping,
2686 page->index, GFP_KERNEL)) {
2687
2688 /* open coding of lru_cache_add, also not exported */
2689 page_cache_get(page);
2690 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002691 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002692 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002693 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002694 }
2695 page_cache_release(page);
2696 }
2697 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002698 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002699 BUG_ON(!list_empty(pages));
2700 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002701 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002702 return 0;
2703}
Chris Masond1310b22008-01-24 16:13:08 -05002704
2705/*
2706 * basic invalidatepage code, this waits on any locked or writeback
2707 * ranges corresponding to the page, and then deletes any extent state
2708 * records from the tree
2709 */
2710int extent_invalidatepage(struct extent_io_tree *tree,
2711 struct page *page, unsigned long offset)
2712{
2713 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2714 u64 end = start + PAGE_CACHE_SIZE - 1;
2715 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2716
Chris Masond3977122009-01-05 21:25:51 -05002717 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002718 if (start > end)
2719 return 0;
2720
2721 lock_extent(tree, start, end, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002722 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002723 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002724 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2725 EXTENT_DO_ACCOUNTING,
Chris Mason2c64c532009-09-02 15:04:12 -04002726 1, 1, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002727 return 0;
2728}
Chris Masond1310b22008-01-24 16:13:08 -05002729
2730/*
2731 * simple commit_write call, set_range_dirty is used to mark both
2732 * the pages and the extent records as dirty
2733 */
2734int extent_commit_write(struct extent_io_tree *tree,
2735 struct inode *inode, struct page *page,
2736 unsigned from, unsigned to)
2737{
2738 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2739
2740 set_page_extent_mapped(page);
2741 set_page_dirty(page);
2742
2743 if (pos > inode->i_size) {
2744 i_size_write(inode, pos);
2745 mark_inode_dirty(inode);
2746 }
2747 return 0;
2748}
Chris Masond1310b22008-01-24 16:13:08 -05002749
2750int extent_prepare_write(struct extent_io_tree *tree,
2751 struct inode *inode, struct page *page,
2752 unsigned from, unsigned to, get_extent_t *get_extent)
2753{
2754 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2755 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2756 u64 block_start;
2757 u64 orig_block_start;
2758 u64 block_end;
2759 u64 cur_end;
2760 struct extent_map *em;
2761 unsigned blocksize = 1 << inode->i_blkbits;
2762 size_t page_offset = 0;
2763 size_t block_off_start;
2764 size_t block_off_end;
2765 int err = 0;
2766 int iocount = 0;
2767 int ret = 0;
2768 int isnew;
2769
2770 set_page_extent_mapped(page);
2771
2772 block_start = (page_start + from) & ~((u64)blocksize - 1);
2773 block_end = (page_start + to - 1) | (blocksize - 1);
2774 orig_block_start = block_start;
2775
2776 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002777 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002778 em = get_extent(inode, page, page_offset, block_start,
2779 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002780 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002781 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002782
Chris Masond1310b22008-01-24 16:13:08 -05002783 cur_end = min(block_end, extent_map_end(em) - 1);
2784 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2785 block_off_end = block_off_start + blocksize;
2786 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2787
2788 if (!PageUptodate(page) && isnew &&
2789 (block_off_end > to || block_off_start < from)) {
2790 void *kaddr;
2791
2792 kaddr = kmap_atomic(page, KM_USER0);
2793 if (block_off_end > to)
2794 memset(kaddr + to, 0, block_off_end - to);
2795 if (block_off_start < from)
2796 memset(kaddr + block_off_start, 0,
2797 from - block_off_start);
2798 flush_dcache_page(page);
2799 kunmap_atomic(kaddr, KM_USER0);
2800 }
2801 if ((em->block_start != EXTENT_MAP_HOLE &&
2802 em->block_start != EXTENT_MAP_INLINE) &&
2803 !isnew && !PageUptodate(page) &&
2804 (block_off_end > to || block_off_start < from) &&
2805 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002806 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002807 u64 sector;
2808 u64 extent_offset = block_start - em->start;
2809 size_t iosize;
2810 sector = (em->block_start + extent_offset) >> 9;
2811 iosize = (cur_end - block_start + blocksize) &
2812 ~((u64)blocksize - 1);
2813 /*
2814 * we've already got the extent locked, but we
2815 * need to split the state such that our end_bio
2816 * handler can clear the lock.
2817 */
2818 set_extent_bit(tree, block_start,
2819 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002820 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002821 ret = submit_extent_page(READ, tree, page,
2822 sector, iosize, page_offset, em->bdev,
2823 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002824 end_bio_extent_preparewrite, 0,
2825 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002826 iocount++;
2827 block_start = block_start + iosize;
2828 } else {
2829 set_extent_uptodate(tree, block_start, cur_end,
2830 GFP_NOFS);
2831 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2832 block_start = cur_end + 1;
2833 }
2834 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2835 free_extent_map(em);
2836 }
2837 if (iocount) {
2838 wait_extent_bit(tree, orig_block_start,
2839 block_end, EXTENT_LOCKED);
2840 }
2841 check_page_uptodate(tree, page);
2842err:
2843 /* FIXME, zero out newly allocated blocks on error */
2844 return err;
2845}
Chris Masond1310b22008-01-24 16:13:08 -05002846
2847/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002848 * a helper for releasepage, this tests for areas of the page that
2849 * are locked or under IO and drops the related state bits if it is safe
2850 * to drop the page.
2851 */
2852int try_release_extent_state(struct extent_map_tree *map,
2853 struct extent_io_tree *tree, struct page *page,
2854 gfp_t mask)
2855{
2856 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2857 u64 end = start + PAGE_CACHE_SIZE - 1;
2858 int ret = 1;
2859
Chris Mason211f90e2008-07-18 11:56:15 -04002860 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002861 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002862 ret = 0;
2863 else {
2864 if ((mask & GFP_NOFS) == GFP_NOFS)
2865 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002866 /*
2867 * at this point we can safely clear everything except the
2868 * locked bit and the nodatasum bit
2869 */
2870 clear_extent_bit(tree, start, end,
2871 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2872 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002873 }
2874 return ret;
2875}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002876
2877/*
Chris Masond1310b22008-01-24 16:13:08 -05002878 * a helper for releasepage. As long as there are no locked extents
2879 * in the range corresponding to the page, both state records and extent
2880 * map records are removed
2881 */
2882int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002883 struct extent_io_tree *tree, struct page *page,
2884 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002885{
2886 struct extent_map *em;
2887 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2888 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002889
Chris Mason70dec802008-01-29 09:59:12 -05002890 if ((mask & __GFP_WAIT) &&
2891 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002892 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002893 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002894 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002895 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002896 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002897 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002898 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002899 break;
2900 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002901 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2902 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002903 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002904 free_extent_map(em);
2905 break;
2906 }
2907 if (!test_range_bit(tree, em->start,
2908 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002909 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002910 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002911 remove_extent_mapping(map, em);
2912 /* once for the rb tree */
2913 free_extent_map(em);
2914 }
2915 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002916 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002917
2918 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002919 free_extent_map(em);
2920 }
Chris Masond1310b22008-01-24 16:13:08 -05002921 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002922 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002923}
Chris Masond1310b22008-01-24 16:13:08 -05002924
2925sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2926 get_extent_t *get_extent)
2927{
2928 struct inode *inode = mapping->host;
2929 u64 start = iblock << inode->i_blkbits;
2930 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002931 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002932 struct extent_map *em;
2933
Yan Zhengd899e052008-10-30 14:25:28 -04002934 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2935 GFP_NOFS);
2936 em = get_extent(inode, NULL, 0, start, blksize, 0);
2937 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2938 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002939 if (!em || IS_ERR(em))
2940 return 0;
2941
Yan Zhengd899e052008-10-30 14:25:28 -04002942 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002943 goto out;
2944
2945 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002946out:
2947 free_extent_map(em);
2948 return sector;
2949}
2950
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002951int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2952 __u64 start, __u64 len, get_extent_t *get_extent)
2953{
2954 int ret;
2955 u64 off = start;
2956 u64 max = start + len;
2957 u32 flags = 0;
2958 u64 disko = 0;
2959 struct extent_map *em = NULL;
2960 int end = 0;
2961 u64 em_start = 0, em_len = 0;
2962 unsigned long emflags;
2963 ret = 0;
2964
2965 if (len == 0)
2966 return -EINVAL;
2967
2968 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2969 GFP_NOFS);
2970 em = get_extent(inode, NULL, 0, off, max - off, 0);
2971 if (!em)
2972 goto out;
2973 if (IS_ERR(em)) {
2974 ret = PTR_ERR(em);
2975 goto out;
2976 }
2977 while (!end) {
2978 off = em->start + em->len;
2979 if (off >= max)
2980 end = 1;
2981
2982 em_start = em->start;
2983 em_len = em->len;
2984
2985 disko = 0;
2986 flags = 0;
2987
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002988 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002989 end = 1;
2990 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002991 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002992 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002993 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002994 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2995 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002996 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002997 flags |= (FIEMAP_EXTENT_DELALLOC |
2998 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002999 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003000 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003001 }
3002 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3003 flags |= FIEMAP_EXTENT_ENCODED;
3004
3005 emflags = em->flags;
3006 free_extent_map(em);
3007 em = NULL;
3008
3009 if (!end) {
3010 em = get_extent(inode, NULL, 0, off, max - off, 0);
3011 if (!em)
3012 goto out;
3013 if (IS_ERR(em)) {
3014 ret = PTR_ERR(em);
3015 goto out;
3016 }
3017 emflags = em->flags;
3018 }
3019 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3020 flags |= FIEMAP_EXTENT_LAST;
3021 end = 1;
3022 }
3023
3024 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3025 em_len, flags);
3026 if (ret)
3027 goto out_free;
3028 }
3029out_free:
3030 free_extent_map(em);
3031out:
3032 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
3033 GFP_NOFS);
3034 return ret;
3035}
3036
Chris Masond1310b22008-01-24 16:13:08 -05003037static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3038 unsigned long i)
3039{
3040 struct page *p;
3041 struct address_space *mapping;
3042
3043 if (i == 0)
3044 return eb->first_page;
3045 i += eb->start >> PAGE_CACHE_SHIFT;
3046 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003047 if (!mapping)
3048 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003049
3050 /*
3051 * extent_buffer_page is only called after pinning the page
3052 * by increasing the reference count. So we know the page must
3053 * be in the radix tree.
3054 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003055 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003056 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003057 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003058
Chris Masond1310b22008-01-24 16:13:08 -05003059 return p;
3060}
3061
Chris Mason6af118ce2008-07-22 11:18:07 -04003062static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003063{
Chris Mason6af118ce2008-07-22 11:18:07 -04003064 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3065 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003066}
3067
Chris Masond1310b22008-01-24 16:13:08 -05003068static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3069 u64 start,
3070 unsigned long len,
3071 gfp_t mask)
3072{
3073 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003074#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003075 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003076#endif
Chris Masond1310b22008-01-24 16:13:08 -05003077
Chris Masond1310b22008-01-24 16:13:08 -05003078 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003079 eb->start = start;
3080 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003081 spin_lock_init(&eb->lock);
3082 init_waitqueue_head(&eb->lock_wq);
3083
Chris Mason39351272009-02-04 09:24:05 -05003084#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003085 spin_lock_irqsave(&leak_lock, flags);
3086 list_add(&eb->leak_list, &buffers);
3087 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003088#endif
Chris Masond1310b22008-01-24 16:13:08 -05003089 atomic_set(&eb->refs, 1);
3090
3091 return eb;
3092}
3093
3094static void __free_extent_buffer(struct extent_buffer *eb)
3095{
Chris Mason39351272009-02-04 09:24:05 -05003096#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003097 unsigned long flags;
3098 spin_lock_irqsave(&leak_lock, flags);
3099 list_del(&eb->leak_list);
3100 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003101#endif
Chris Masond1310b22008-01-24 16:13:08 -05003102 kmem_cache_free(extent_buffer_cache, eb);
3103}
3104
3105struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3106 u64 start, unsigned long len,
3107 struct page *page0,
3108 gfp_t mask)
3109{
3110 unsigned long num_pages = num_extent_pages(start, len);
3111 unsigned long i;
3112 unsigned long index = start >> PAGE_CACHE_SHIFT;
3113 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003114 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003115 struct page *p;
3116 struct address_space *mapping = tree->mapping;
3117 int uptodate = 1;
3118
Chris Mason6af118ce2008-07-22 11:18:07 -04003119 spin_lock(&tree->buffer_lock);
3120 eb = buffer_search(tree, start);
3121 if (eb) {
3122 atomic_inc(&eb->refs);
3123 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003124 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003125 return eb;
3126 }
3127 spin_unlock(&tree->buffer_lock);
3128
Chris Masond1310b22008-01-24 16:13:08 -05003129 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003130 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003131 return NULL;
3132
Chris Masond1310b22008-01-24 16:13:08 -05003133 if (page0) {
3134 eb->first_page = page0;
3135 i = 1;
3136 index++;
3137 page_cache_get(page0);
3138 mark_page_accessed(page0);
3139 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003140 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003141 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003142 } else {
3143 i = 0;
3144 }
3145 for (; i < num_pages; i++, index++) {
3146 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3147 if (!p) {
3148 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003149 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003150 }
3151 set_page_extent_mapped(p);
3152 mark_page_accessed(p);
3153 if (i == 0) {
3154 eb->first_page = p;
3155 set_page_extent_head(p, len);
3156 } else {
3157 set_page_private(p, EXTENT_PAGE_PRIVATE);
3158 }
3159 if (!PageUptodate(p))
3160 uptodate = 0;
3161 unlock_page(p);
3162 }
3163 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003164 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003165
Chris Mason6af118ce2008-07-22 11:18:07 -04003166 spin_lock(&tree->buffer_lock);
3167 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3168 if (exists) {
3169 /* add one reference for the caller */
3170 atomic_inc(&exists->refs);
3171 spin_unlock(&tree->buffer_lock);
3172 goto free_eb;
3173 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003174 /* add one reference for the tree */
3175 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003176 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003177 return eb;
3178
Chris Mason6af118ce2008-07-22 11:18:07 -04003179free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003180 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003181 return exists;
3182 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003183 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003184 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003185 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003186 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003187}
Chris Masond1310b22008-01-24 16:13:08 -05003188
3189struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3190 u64 start, unsigned long len,
3191 gfp_t mask)
3192{
Chris Masond1310b22008-01-24 16:13:08 -05003193 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003194
Chris Mason6af118ce2008-07-22 11:18:07 -04003195 spin_lock(&tree->buffer_lock);
3196 eb = buffer_search(tree, start);
3197 if (eb)
3198 atomic_inc(&eb->refs);
3199 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003200
Josef Bacik0f9dd462008-09-23 13:14:11 -04003201 if (eb)
3202 mark_page_accessed(eb->first_page);
3203
Chris Masond1310b22008-01-24 16:13:08 -05003204 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003205}
Chris Masond1310b22008-01-24 16:13:08 -05003206
3207void free_extent_buffer(struct extent_buffer *eb)
3208{
Chris Masond1310b22008-01-24 16:13:08 -05003209 if (!eb)
3210 return;
3211
3212 if (!atomic_dec_and_test(&eb->refs))
3213 return;
3214
Chris Mason6af118ce2008-07-22 11:18:07 -04003215 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003216}
Chris Masond1310b22008-01-24 16:13:08 -05003217
3218int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3219 struct extent_buffer *eb)
3220{
Chris Masond1310b22008-01-24 16:13:08 -05003221 unsigned long i;
3222 unsigned long num_pages;
3223 struct page *page;
3224
Chris Masond1310b22008-01-24 16:13:08 -05003225 num_pages = num_extent_pages(eb->start, eb->len);
3226
3227 for (i = 0; i < num_pages; i++) {
3228 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003229 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003230 continue;
3231
Chris Masona61e6f22008-07-22 11:18:08 -04003232 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003233 if (i == 0)
3234 set_page_extent_head(page, eb->len);
3235 else
3236 set_page_private(page, EXTENT_PAGE_PRIVATE);
3237
Chris Masond1310b22008-01-24 16:13:08 -05003238 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003239 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003240 if (!PageDirty(page)) {
3241 radix_tree_tag_clear(&page->mapping->page_tree,
3242 page_index(page),
3243 PAGECACHE_TAG_DIRTY);
3244 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003245 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003246 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003247 }
3248 return 0;
3249}
Chris Masond1310b22008-01-24 16:13:08 -05003250
3251int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3252 struct extent_buffer *eb)
3253{
3254 return wait_on_extent_writeback(tree, eb->start,
3255 eb->start + eb->len - 1);
3256}
Chris Masond1310b22008-01-24 16:13:08 -05003257
3258int set_extent_buffer_dirty(struct extent_io_tree *tree,
3259 struct extent_buffer *eb)
3260{
3261 unsigned long i;
3262 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003263 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003264
Chris Masonb9473432009-03-13 11:00:37 -04003265 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003266 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003267 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003268 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003269 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003270}
Chris Masond1310b22008-01-24 16:13:08 -05003271
Chris Mason1259ab72008-05-12 13:39:03 -04003272int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3273 struct extent_buffer *eb)
3274{
3275 unsigned long i;
3276 struct page *page;
3277 unsigned long num_pages;
3278
3279 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003280 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003281
3282 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3283 GFP_NOFS);
3284 for (i = 0; i < num_pages; i++) {
3285 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003286 if (page)
3287 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003288 }
3289 return 0;
3290}
3291
Chris Masond1310b22008-01-24 16:13:08 -05003292int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3293 struct extent_buffer *eb)
3294{
3295 unsigned long i;
3296 struct page *page;
3297 unsigned long num_pages;
3298
3299 num_pages = num_extent_pages(eb->start, eb->len);
3300
3301 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3302 GFP_NOFS);
3303 for (i = 0; i < num_pages; i++) {
3304 page = extent_buffer_page(eb, i);
3305 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3306 ((i == num_pages - 1) &&
3307 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3308 check_page_uptodate(tree, page);
3309 continue;
3310 }
3311 SetPageUptodate(page);
3312 }
3313 return 0;
3314}
Chris Masond1310b22008-01-24 16:13:08 -05003315
Chris Masonce9adaa2008-04-09 16:28:12 -04003316int extent_range_uptodate(struct extent_io_tree *tree,
3317 u64 start, u64 end)
3318{
3319 struct page *page;
3320 int ret;
3321 int pg_uptodate = 1;
3322 int uptodate;
3323 unsigned long index;
3324
Chris Mason9655d292009-09-02 15:22:30 -04003325 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003326 if (ret)
3327 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003328 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003329 index = start >> PAGE_CACHE_SHIFT;
3330 page = find_get_page(tree->mapping, index);
3331 uptodate = PageUptodate(page);
3332 page_cache_release(page);
3333 if (!uptodate) {
3334 pg_uptodate = 0;
3335 break;
3336 }
3337 start += PAGE_CACHE_SIZE;
3338 }
3339 return pg_uptodate;
3340}
3341
Chris Masond1310b22008-01-24 16:13:08 -05003342int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003343 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003344{
Chris Mason728131d2008-04-09 16:28:12 -04003345 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003346 unsigned long num_pages;
3347 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003348 struct page *page;
3349 int pg_uptodate = 1;
3350
Chris Masonb4ce94d2009-02-04 09:25:08 -05003351 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003352 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003353
Chris Mason42352982008-04-28 16:40:52 -04003354 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003355 EXTENT_UPTODATE, 1, NULL);
Chris Mason42352982008-04-28 16:40:52 -04003356 if (ret)
3357 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003358
3359 num_pages = num_extent_pages(eb->start, eb->len);
3360 for (i = 0; i < num_pages; i++) {
3361 page = extent_buffer_page(eb, i);
3362 if (!PageUptodate(page)) {
3363 pg_uptodate = 0;
3364 break;
3365 }
3366 }
Chris Mason42352982008-04-28 16:40:52 -04003367 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003368}
Chris Masond1310b22008-01-24 16:13:08 -05003369
3370int read_extent_buffer_pages(struct extent_io_tree *tree,
3371 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003372 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003373 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003374{
3375 unsigned long i;
3376 unsigned long start_i;
3377 struct page *page;
3378 int err;
3379 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003380 int locked_pages = 0;
3381 int all_uptodate = 1;
3382 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003383 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003384 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003385 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003386
Chris Masonb4ce94d2009-02-04 09:25:08 -05003387 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003388 return 0;
3389
Chris Masonce9adaa2008-04-09 16:28:12 -04003390 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003391 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003392 return 0;
3393 }
3394
3395 if (start) {
3396 WARN_ON(start < eb->start);
3397 start_i = (start >> PAGE_CACHE_SHIFT) -
3398 (eb->start >> PAGE_CACHE_SHIFT);
3399 } else {
3400 start_i = 0;
3401 }
3402
3403 num_pages = num_extent_pages(eb->start, eb->len);
3404 for (i = start_i; i < num_pages; i++) {
3405 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003406 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003407 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003408 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003409 } else {
3410 lock_page(page);
3411 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003412 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003413 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003414 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003415 }
3416 if (all_uptodate) {
3417 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003418 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003419 goto unlock_exit;
3420 }
3421
3422 for (i = start_i; i < num_pages; i++) {
3423 page = extent_buffer_page(eb, i);
3424 if (inc_all_pages)
3425 page_cache_get(page);
3426 if (!PageUptodate(page)) {
3427 if (start_i == 0)
3428 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003429 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003430 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003431 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003432 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003433 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003434 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003435 } else {
3436 unlock_page(page);
3437 }
3438 }
3439
Chris Masona86c12c2008-02-07 10:50:54 -05003440 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003441 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003442
Chris Masond3977122009-01-05 21:25:51 -05003443 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003444 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003445
Chris Masond1310b22008-01-24 16:13:08 -05003446 for (i = start_i; i < num_pages; i++) {
3447 page = extent_buffer_page(eb, i);
3448 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003449 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003450 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003451 }
Chris Masond3977122009-01-05 21:25:51 -05003452
Chris Masond1310b22008-01-24 16:13:08 -05003453 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003454 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003455 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003456
3457unlock_exit:
3458 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003459 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003460 page = extent_buffer_page(eb, i);
3461 i++;
3462 unlock_page(page);
3463 locked_pages--;
3464 }
3465 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003466}
Chris Masond1310b22008-01-24 16:13:08 -05003467
3468void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3469 unsigned long start,
3470 unsigned long len)
3471{
3472 size_t cur;
3473 size_t offset;
3474 struct page *page;
3475 char *kaddr;
3476 char *dst = (char *)dstv;
3477 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3478 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003479
3480 WARN_ON(start > eb->len);
3481 WARN_ON(start + len > eb->start + eb->len);
3482
3483 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3484
Chris Masond3977122009-01-05 21:25:51 -05003485 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003486 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003487
3488 cur = min(len, (PAGE_CACHE_SIZE - offset));
3489 kaddr = kmap_atomic(page, KM_USER1);
3490 memcpy(dst, kaddr + offset, cur);
3491 kunmap_atomic(kaddr, KM_USER1);
3492
3493 dst += cur;
3494 len -= cur;
3495 offset = 0;
3496 i++;
3497 }
3498}
Chris Masond1310b22008-01-24 16:13:08 -05003499
3500int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3501 unsigned long min_len, char **token, char **map,
3502 unsigned long *map_start,
3503 unsigned long *map_len, int km)
3504{
3505 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3506 char *kaddr;
3507 struct page *p;
3508 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3509 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3510 unsigned long end_i = (start_offset + start + min_len - 1) >>
3511 PAGE_CACHE_SHIFT;
3512
3513 if (i != end_i)
3514 return -EINVAL;
3515
3516 if (i == 0) {
3517 offset = start_offset;
3518 *map_start = 0;
3519 } else {
3520 offset = 0;
3521 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3522 }
Chris Masond3977122009-01-05 21:25:51 -05003523
Chris Masond1310b22008-01-24 16:13:08 -05003524 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003525 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3526 "wanted %lu %lu\n", (unsigned long long)eb->start,
3527 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003528 WARN_ON(1);
3529 }
3530
3531 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003532 kaddr = kmap_atomic(p, km);
3533 *token = kaddr;
3534 *map = kaddr + offset;
3535 *map_len = PAGE_CACHE_SIZE - offset;
3536 return 0;
3537}
Chris Masond1310b22008-01-24 16:13:08 -05003538
3539int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3540 unsigned long min_len,
3541 char **token, char **map,
3542 unsigned long *map_start,
3543 unsigned long *map_len, int km)
3544{
3545 int err;
3546 int save = 0;
3547 if (eb->map_token) {
3548 unmap_extent_buffer(eb, eb->map_token, km);
3549 eb->map_token = NULL;
3550 save = 1;
3551 }
3552 err = map_private_extent_buffer(eb, start, min_len, token, map,
3553 map_start, map_len, km);
3554 if (!err && save) {
3555 eb->map_token = *token;
3556 eb->kaddr = *map;
3557 eb->map_start = *map_start;
3558 eb->map_len = *map_len;
3559 }
3560 return err;
3561}
Chris Masond1310b22008-01-24 16:13:08 -05003562
3563void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3564{
3565 kunmap_atomic(token, km);
3566}
Chris Masond1310b22008-01-24 16:13:08 -05003567
3568int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3569 unsigned long start,
3570 unsigned long len)
3571{
3572 size_t cur;
3573 size_t offset;
3574 struct page *page;
3575 char *kaddr;
3576 char *ptr = (char *)ptrv;
3577 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3578 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3579 int ret = 0;
3580
3581 WARN_ON(start > eb->len);
3582 WARN_ON(start + len > eb->start + eb->len);
3583
3584 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3585
Chris Masond3977122009-01-05 21:25:51 -05003586 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003587 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003588
3589 cur = min(len, (PAGE_CACHE_SIZE - offset));
3590
3591 kaddr = kmap_atomic(page, KM_USER0);
3592 ret = memcmp(ptr, kaddr + offset, cur);
3593 kunmap_atomic(kaddr, KM_USER0);
3594 if (ret)
3595 break;
3596
3597 ptr += cur;
3598 len -= cur;
3599 offset = 0;
3600 i++;
3601 }
3602 return ret;
3603}
Chris Masond1310b22008-01-24 16:13:08 -05003604
3605void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3606 unsigned long start, unsigned long len)
3607{
3608 size_t cur;
3609 size_t offset;
3610 struct page *page;
3611 char *kaddr;
3612 char *src = (char *)srcv;
3613 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3614 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3615
3616 WARN_ON(start > eb->len);
3617 WARN_ON(start + len > eb->start + eb->len);
3618
3619 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3620
Chris Masond3977122009-01-05 21:25:51 -05003621 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003622 page = extent_buffer_page(eb, i);
3623 WARN_ON(!PageUptodate(page));
3624
3625 cur = min(len, PAGE_CACHE_SIZE - offset);
3626 kaddr = kmap_atomic(page, KM_USER1);
3627 memcpy(kaddr + offset, src, cur);
3628 kunmap_atomic(kaddr, KM_USER1);
3629
3630 src += cur;
3631 len -= cur;
3632 offset = 0;
3633 i++;
3634 }
3635}
Chris Masond1310b22008-01-24 16:13:08 -05003636
3637void memset_extent_buffer(struct extent_buffer *eb, char c,
3638 unsigned long start, unsigned long len)
3639{
3640 size_t cur;
3641 size_t offset;
3642 struct page *page;
3643 char *kaddr;
3644 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3645 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3646
3647 WARN_ON(start > eb->len);
3648 WARN_ON(start + len > eb->start + eb->len);
3649
3650 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3651
Chris Masond3977122009-01-05 21:25:51 -05003652 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003653 page = extent_buffer_page(eb, i);
3654 WARN_ON(!PageUptodate(page));
3655
3656 cur = min(len, PAGE_CACHE_SIZE - offset);
3657 kaddr = kmap_atomic(page, KM_USER0);
3658 memset(kaddr + offset, c, cur);
3659 kunmap_atomic(kaddr, KM_USER0);
3660
3661 len -= cur;
3662 offset = 0;
3663 i++;
3664 }
3665}
Chris Masond1310b22008-01-24 16:13:08 -05003666
3667void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3668 unsigned long dst_offset, unsigned long src_offset,
3669 unsigned long len)
3670{
3671 u64 dst_len = dst->len;
3672 size_t cur;
3673 size_t offset;
3674 struct page *page;
3675 char *kaddr;
3676 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3677 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3678
3679 WARN_ON(src->len != dst_len);
3680
3681 offset = (start_offset + dst_offset) &
3682 ((unsigned long)PAGE_CACHE_SIZE - 1);
3683
Chris Masond3977122009-01-05 21:25:51 -05003684 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003685 page = extent_buffer_page(dst, i);
3686 WARN_ON(!PageUptodate(page));
3687
3688 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3689
3690 kaddr = kmap_atomic(page, KM_USER0);
3691 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3692 kunmap_atomic(kaddr, KM_USER0);
3693
3694 src_offset += cur;
3695 len -= cur;
3696 offset = 0;
3697 i++;
3698 }
3699}
Chris Masond1310b22008-01-24 16:13:08 -05003700
3701static void move_pages(struct page *dst_page, struct page *src_page,
3702 unsigned long dst_off, unsigned long src_off,
3703 unsigned long len)
3704{
3705 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3706 if (dst_page == src_page) {
3707 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3708 } else {
3709 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3710 char *p = dst_kaddr + dst_off + len;
3711 char *s = src_kaddr + src_off + len;
3712
3713 while (len--)
3714 *--p = *--s;
3715
3716 kunmap_atomic(src_kaddr, KM_USER1);
3717 }
3718 kunmap_atomic(dst_kaddr, KM_USER0);
3719}
3720
3721static void copy_pages(struct page *dst_page, struct page *src_page,
3722 unsigned long dst_off, unsigned long src_off,
3723 unsigned long len)
3724{
3725 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3726 char *src_kaddr;
3727
3728 if (dst_page != src_page)
3729 src_kaddr = kmap_atomic(src_page, KM_USER1);
3730 else
3731 src_kaddr = dst_kaddr;
3732
3733 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3734 kunmap_atomic(dst_kaddr, KM_USER0);
3735 if (dst_page != src_page)
3736 kunmap_atomic(src_kaddr, KM_USER1);
3737}
3738
3739void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3740 unsigned long src_offset, unsigned long len)
3741{
3742 size_t cur;
3743 size_t dst_off_in_page;
3744 size_t src_off_in_page;
3745 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3746 unsigned long dst_i;
3747 unsigned long src_i;
3748
3749 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003750 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3751 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003752 BUG_ON(1);
3753 }
3754 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003755 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3756 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003757 BUG_ON(1);
3758 }
3759
Chris Masond3977122009-01-05 21:25:51 -05003760 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003761 dst_off_in_page = (start_offset + dst_offset) &
3762 ((unsigned long)PAGE_CACHE_SIZE - 1);
3763 src_off_in_page = (start_offset + src_offset) &
3764 ((unsigned long)PAGE_CACHE_SIZE - 1);
3765
3766 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3767 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3768
3769 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3770 src_off_in_page));
3771 cur = min_t(unsigned long, cur,
3772 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3773
3774 copy_pages(extent_buffer_page(dst, dst_i),
3775 extent_buffer_page(dst, src_i),
3776 dst_off_in_page, src_off_in_page, cur);
3777
3778 src_offset += cur;
3779 dst_offset += cur;
3780 len -= cur;
3781 }
3782}
Chris Masond1310b22008-01-24 16:13:08 -05003783
3784void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3785 unsigned long src_offset, unsigned long len)
3786{
3787 size_t cur;
3788 size_t dst_off_in_page;
3789 size_t src_off_in_page;
3790 unsigned long dst_end = dst_offset + len - 1;
3791 unsigned long src_end = src_offset + len - 1;
3792 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3793 unsigned long dst_i;
3794 unsigned long src_i;
3795
3796 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003797 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3798 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003799 BUG_ON(1);
3800 }
3801 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003802 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3803 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003804 BUG_ON(1);
3805 }
3806 if (dst_offset < src_offset) {
3807 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3808 return;
3809 }
Chris Masond3977122009-01-05 21:25:51 -05003810 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003811 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3812 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3813
3814 dst_off_in_page = (start_offset + dst_end) &
3815 ((unsigned long)PAGE_CACHE_SIZE - 1);
3816 src_off_in_page = (start_offset + src_end) &
3817 ((unsigned long)PAGE_CACHE_SIZE - 1);
3818
3819 cur = min_t(unsigned long, len, src_off_in_page + 1);
3820 cur = min(cur, dst_off_in_page + 1);
3821 move_pages(extent_buffer_page(dst, dst_i),
3822 extent_buffer_page(dst, src_i),
3823 dst_off_in_page - cur + 1,
3824 src_off_in_page - cur + 1, cur);
3825
3826 dst_end -= cur;
3827 src_end -= cur;
3828 len -= cur;
3829 }
3830}
Chris Mason6af118ce2008-07-22 11:18:07 -04003831
3832int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3833{
3834 u64 start = page_offset(page);
3835 struct extent_buffer *eb;
3836 int ret = 1;
3837 unsigned long i;
3838 unsigned long num_pages;
3839
3840 spin_lock(&tree->buffer_lock);
3841 eb = buffer_search(tree, start);
3842 if (!eb)
3843 goto out;
3844
3845 if (atomic_read(&eb->refs) > 1) {
3846 ret = 0;
3847 goto out;
3848 }
Chris Masonb9473432009-03-13 11:00:37 -04003849 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3850 ret = 0;
3851 goto out;
3852 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003853 /* at this point we can safely release the extent buffer */
3854 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003855 for (i = 0; i < num_pages; i++)
3856 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003857 rb_erase(&eb->rb_node, &tree->buffer);
3858 __free_extent_buffer(eb);
3859out:
3860 spin_unlock(&tree->buffer_lock);
3861 return ret;
3862}