blob: c7a5e860fe2151cb6155c4981f2e27e1a5800118 [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{
107 tree->state.rb_node = NULL;
Chris Mason6af118c2008-07-22 11:18:07 -0400108 tree->buffer.rb_node = NULL;
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 Mason6af118c2008-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 Mason6af118c2008-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 Mason6af118c2008-07-22 11:18:07 -0400245 struct extent_buffer *eb;
246
Chris Masond3977122009-01-05 21:25:51 -0500247 while (*p) {
Chris Mason6af118c2008-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 Mason6af118c2008-07-22 11:18:07 -0400269 struct extent_buffer *eb;
270
Chris Masond3977122009-01-05 21:25:51 -0500271 while (n) {
Chris Mason6af118c2008-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
Chris Masond1310b22008-01-24 16:13:08 -0500283/*
284 * utility function to look for merge candidates inside a given range.
285 * Any extents with matching state are merged together into a single
286 * extent in the tree. Extents with EXTENT_IO in their state field
287 * are not merged because the end_io handlers need to be able to do
288 * operations on them without sleeping (or doing allocations/splits).
289 *
290 * This should be called with the tree lock held.
291 */
292static int merge_state(struct extent_io_tree *tree,
293 struct extent_state *state)
294{
295 struct extent_state *other;
296 struct rb_node *other_node;
297
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400298 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500299 return 0;
300
301 other_node = rb_prev(&state->rb_node);
302 if (other_node) {
303 other = rb_entry(other_node, struct extent_state, rb_node);
304 if (other->end == state->start - 1 &&
305 other->state == state->state) {
306 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500307 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500308 rb_erase(&other->rb_node, &tree->state);
309 free_extent_state(other);
310 }
311 }
312 other_node = rb_next(&state->rb_node);
313 if (other_node) {
314 other = rb_entry(other_node, struct extent_state, rb_node);
315 if (other->start == state->end + 1 &&
316 other->state == state->state) {
317 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500318 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500319 rb_erase(&state->rb_node, &tree->state);
320 free_extent_state(state);
321 }
322 }
323 return 0;
324}
325
Chris Mason291d6732008-01-29 15:55:23 -0500326static void set_state_cb(struct extent_io_tree *tree,
327 struct extent_state *state,
328 unsigned long bits)
329{
330 if (tree->ops && tree->ops->set_bit_hook) {
331 tree->ops->set_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500332 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500333 }
334}
335
336static void clear_state_cb(struct extent_io_tree *tree,
337 struct extent_state *state,
338 unsigned long bits)
339{
Liu Huic5844822009-01-05 15:49:55 -0500340 if (tree->ops && tree->ops->clear_bit_hook) {
Chris Mason291d6732008-01-29 15:55:23 -0500341 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500342 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500343 }
344}
345
Chris Masond1310b22008-01-24 16:13:08 -0500346/*
347 * insert an extent_state struct into the tree. 'bits' are set on the
348 * struct before it is inserted.
349 *
350 * This may return -EEXIST if the extent is already there, in which case the
351 * state struct is freed.
352 *
353 * The tree lock is not taken internally. This is a utility function and
354 * probably isn't what you want to call (see set/clear_extent_bit).
355 */
356static int insert_state(struct extent_io_tree *tree,
357 struct extent_state *state, u64 start, u64 end,
358 int bits)
359{
360 struct rb_node *node;
361
362 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500363 printk(KERN_ERR "btrfs end < start %llu %llu\n",
364 (unsigned long long)end,
365 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500366 WARN_ON(1);
367 }
368 if (bits & EXTENT_DIRTY)
369 tree->dirty_bytes += end - start + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500370 state->start = start;
371 state->end = end;
Chris Masone48c4652009-09-11 11:25:02 -0400372 set_state_cb(tree, state, bits);
373 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500374 node = tree_insert(&tree->state, end, &state->rb_node);
375 if (node) {
376 struct extent_state *found;
377 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500378 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
379 "%llu %llu\n", (unsigned long long)found->start,
380 (unsigned long long)found->end,
381 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500382 free_extent_state(state);
383 return -EEXIST;
384 }
Chris Mason70dec802008-01-29 09:59:12 -0500385 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500386 merge_state(tree, state);
387 return 0;
388}
389
390/*
391 * split a given extent state struct in two, inserting the preallocated
392 * struct 'prealloc' as the newly created second half. 'split' indicates an
393 * offset inside 'orig' where it should be split.
394 *
395 * Before calling,
396 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
397 * are two extent state structs in the tree:
398 * prealloc: [orig->start, split - 1]
399 * orig: [ split, orig->end ]
400 *
401 * The tree locks are not taken by this function. They need to be held
402 * by the caller.
403 */
404static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
405 struct extent_state *prealloc, u64 split)
406{
407 struct rb_node *node;
408 prealloc->start = orig->start;
409 prealloc->end = split - 1;
410 prealloc->state = orig->state;
411 orig->start = split;
412
413 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
414 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500415 free_extent_state(prealloc);
416 return -EEXIST;
417 }
Chris Mason70dec802008-01-29 09:59:12 -0500418 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500419 return 0;
420}
421
422/*
423 * utility function to clear some bits in an extent state struct.
424 * it will optionally wake up any one waiting on this state (wake == 1), or
425 * forcibly remove the state from the tree (delete == 1).
426 *
427 * If no bits are set on the state struct after clearing things, the
428 * struct is freed and removed from the tree
429 */
430static int clear_state_bit(struct extent_io_tree *tree,
431 struct extent_state *state, int bits, int wake,
432 int delete)
433{
434 int ret = state->state & bits;
435
436 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
437 u64 range = state->end - state->start + 1;
438 WARN_ON(range > tree->dirty_bytes);
439 tree->dirty_bytes -= range;
440 }
Chris Mason291d6732008-01-29 15:55:23 -0500441 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500442 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500443 if (wake)
444 wake_up(&state->wq);
445 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500446 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500447 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500448 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500449 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500450 free_extent_state(state);
451 } else {
452 WARN_ON(1);
453 }
454 } else {
455 merge_state(tree, state);
456 }
457 return ret;
458}
459
460/*
461 * clear some bits on a range in the tree. This may require splitting
462 * or inserting elements in the tree, so the gfp mask is used to
463 * indicate which allocations or sleeping are allowed.
464 *
465 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
466 * the given range from the tree regardless of state (ie for truncate).
467 *
468 * the range [start, end] is inclusive.
469 *
470 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
471 * bits were already set, or zero if none of the bits were already set.
472 */
473int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400474 int bits, int wake, int delete,
475 struct extent_state **cached_state,
476 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500477{
478 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400479 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500480 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400481 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500482 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400483 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500484 int err;
485 int set = 0;
486
487again:
488 if (!prealloc && (mask & __GFP_WAIT)) {
489 prealloc = alloc_extent_state(mask);
490 if (!prealloc)
491 return -ENOMEM;
492 }
493
Chris Masoncad321a2008-12-17 14:51:42 -0500494 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400495 if (cached_state) {
496 cached = *cached_state;
497 *cached_state = NULL;
498 if (cached->tree && cached->start == start) {
499 atomic_dec(&cached->refs);
500 state = cached;
501 last_end = state->end;
502 goto found;
503 }
504 free_extent_state(cached);
505 }
Chris Masond1310b22008-01-24 16:13:08 -0500506 /*
507 * this search will find the extents that end after
508 * our range starts
509 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500510 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500511 if (!node)
512 goto out;
513 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400514hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500515 if (state->start > end)
516 goto out;
517 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400518 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500519
520 /*
521 * | ---- desired range ---- |
522 * | state | or
523 * | ------------- state -------------- |
524 *
525 * We need to split the extent we found, and may flip
526 * bits on second half.
527 *
528 * If the extent we found extends past our range, we
529 * just split and search again. It'll get split again
530 * the next time though.
531 *
532 * If the extent we found is inside our range, we clear
533 * the desired bit on it.
534 */
535
536 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500537 if (!prealloc)
538 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500539 err = split_state(tree, state, prealloc, start);
540 BUG_ON(err == -EEXIST);
541 prealloc = NULL;
542 if (err)
543 goto out;
544 if (state->end <= end) {
Chris Masond1310b22008-01-24 16:13:08 -0500545 set |= clear_state_bit(tree, state, bits,
546 wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400547 if (last_end == (u64)-1)
548 goto out;
549 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500550 } else {
551 start = state->start;
552 }
553 goto search_again;
554 }
555 /*
556 * | ---- desired range ---- |
557 * | state |
558 * We need to split the extent, and clear the bit
559 * on the first half
560 */
561 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500562 if (!prealloc)
563 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500564 err = split_state(tree, state, prealloc, end + 1);
565 BUG_ON(err == -EEXIST);
566
567 if (wake)
568 wake_up(&state->wq);
569 set |= clear_state_bit(tree, prealloc, bits,
570 wake, delete);
571 prealloc = NULL;
572 goto out;
573 }
Chris Mason2c64c532009-09-02 15:04:12 -0400574found:
575 if (state->end < end && prealloc && !need_resched())
576 next_node = rb_next(&state->rb_node);
577 else
578 next_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500579 set |= clear_state_bit(tree, state, bits, wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400580 if (last_end == (u64)-1)
581 goto out;
582 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400583 if (start <= end && next_node) {
584 state = rb_entry(next_node, struct extent_state,
585 rb_node);
586 if (state->start == start)
587 goto hit_next;
588 }
Chris Masond1310b22008-01-24 16:13:08 -0500589 goto search_again;
590
591out:
Chris Masoncad321a2008-12-17 14:51:42 -0500592 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500593 if (prealloc)
594 free_extent_state(prealloc);
595
596 return set;
597
598search_again:
599 if (start > end)
600 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500601 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500602 if (mask & __GFP_WAIT)
603 cond_resched();
604 goto again;
605}
Chris Masond1310b22008-01-24 16:13:08 -0500606
607static int wait_on_state(struct extent_io_tree *tree,
608 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500609 __releases(tree->lock)
610 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500611{
612 DEFINE_WAIT(wait);
613 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500614 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500615 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500616 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500617 finish_wait(&state->wq, &wait);
618 return 0;
619}
620
621/*
622 * waits for one or more bits to clear on a range in the state tree.
623 * The range [start, end] is inclusive.
624 * The tree lock is taken by this function
625 */
626int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
627{
628 struct extent_state *state;
629 struct rb_node *node;
630
Chris Masoncad321a2008-12-17 14:51:42 -0500631 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500632again:
633 while (1) {
634 /*
635 * this search will find all the extents that end after
636 * our range starts
637 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500638 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500639 if (!node)
640 break;
641
642 state = rb_entry(node, struct extent_state, rb_node);
643
644 if (state->start > end)
645 goto out;
646
647 if (state->state & bits) {
648 start = state->start;
649 atomic_inc(&state->refs);
650 wait_on_state(tree, state);
651 free_extent_state(state);
652 goto again;
653 }
654 start = state->end + 1;
655
656 if (start > end)
657 break;
658
659 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500660 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500661 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500662 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500663 }
664 }
665out:
Chris Masoncad321a2008-12-17 14:51:42 -0500666 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500667 return 0;
668}
Chris Masond1310b22008-01-24 16:13:08 -0500669
670static void set_state_bits(struct extent_io_tree *tree,
671 struct extent_state *state,
672 int bits)
673{
674 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
675 u64 range = state->end - state->start + 1;
676 tree->dirty_bytes += range;
677 }
Chris Mason291d6732008-01-29 15:55:23 -0500678 set_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500679 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500680}
681
Chris Mason2c64c532009-09-02 15:04:12 -0400682static void cache_state(struct extent_state *state,
683 struct extent_state **cached_ptr)
684{
685 if (cached_ptr && !(*cached_ptr)) {
686 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
687 *cached_ptr = state;
688 atomic_inc(&state->refs);
689 }
690 }
691}
692
Chris Masond1310b22008-01-24 16:13:08 -0500693/*
Chris Mason1edbb732009-09-02 13:24:36 -0400694 * set some bits on a range in the tree. This may require allocations or
695 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500696 *
Chris Mason1edbb732009-09-02 13:24:36 -0400697 * If any of the exclusive bits are set, this will fail with -EEXIST if some
698 * part of the range already has the desired bits set. The start of the
699 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500700 *
Chris Mason1edbb732009-09-02 13:24:36 -0400701 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500702 */
Chris Mason1edbb732009-09-02 13:24:36 -0400703
Chris Masond3977122009-01-05 21:25:51 -0500704static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason1edbb732009-09-02 13:24:36 -0400705 int bits, int exclusive_bits, u64 *failed_start,
Chris Mason2c64c532009-09-02 15:04:12 -0400706 struct extent_state **cached_state,
Chris Masond3977122009-01-05 21:25:51 -0500707 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500708{
709 struct extent_state *state;
710 struct extent_state *prealloc = NULL;
711 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500712 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500713 u64 last_start;
714 u64 last_end;
715again:
716 if (!prealloc && (mask & __GFP_WAIT)) {
717 prealloc = alloc_extent_state(mask);
718 if (!prealloc)
719 return -ENOMEM;
720 }
721
Chris Masoncad321a2008-12-17 14:51:42 -0500722 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500723 /*
724 * this search will find all the extents that end after
725 * our range starts.
726 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500727 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500728 if (!node) {
729 err = insert_state(tree, prealloc, start, end, bits);
730 prealloc = NULL;
731 BUG_ON(err == -EEXIST);
732 goto out;
733 }
Chris Masond1310b22008-01-24 16:13:08 -0500734 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400735hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500736 last_start = state->start;
737 last_end = state->end;
738
739 /*
740 * | ---- desired range ---- |
741 * | state |
742 *
743 * Just lock what we found and keep going
744 */
745 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400746 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400747 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500748 *failed_start = state->start;
749 err = -EEXIST;
750 goto out;
751 }
752 set_state_bits(tree, state, bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400753 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500754 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400755 if (last_end == (u64)-1)
756 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400757
Yan Zheng5c939df2009-05-27 09:16:03 -0400758 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400759 if (start < end && prealloc && !need_resched()) {
760 next_node = rb_next(node);
761 if (next_node) {
762 state = rb_entry(next_node, struct extent_state,
763 rb_node);
764 if (state->start == start)
765 goto hit_next;
766 }
767 }
Chris Masond1310b22008-01-24 16:13:08 -0500768 goto search_again;
769 }
770
771 /*
772 * | ---- desired range ---- |
773 * | state |
774 * or
775 * | ------------- state -------------- |
776 *
777 * We need to split the extent we found, and may flip bits on
778 * second half.
779 *
780 * If the extent we found extends past our
781 * range, we just split and search again. It'll get split
782 * again the next time though.
783 *
784 * If the extent we found is inside our range, we set the
785 * desired bit on it.
786 */
787 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400788 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500789 *failed_start = start;
790 err = -EEXIST;
791 goto out;
792 }
793 err = split_state(tree, state, prealloc, start);
794 BUG_ON(err == -EEXIST);
795 prealloc = NULL;
796 if (err)
797 goto out;
798 if (state->end <= end) {
799 set_state_bits(tree, state, bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400800 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500801 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400802 if (last_end == (u64)-1)
803 goto out;
804 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500805 } else {
806 start = state->start;
807 }
808 goto search_again;
809 }
810 /*
811 * | ---- desired range ---- |
812 * | state | or | state |
813 *
814 * There's a hole, we need to insert something in it and
815 * ignore the extent we found.
816 */
817 if (state->start > start) {
818 u64 this_end;
819 if (end < last_start)
820 this_end = end;
821 else
Chris Masond3977122009-01-05 21:25:51 -0500822 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500823 err = insert_state(tree, prealloc, start, this_end,
824 bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400825 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500826 prealloc = NULL;
827 BUG_ON(err == -EEXIST);
828 if (err)
829 goto out;
830 start = this_end + 1;
831 goto search_again;
832 }
833 /*
834 * | ---- desired range ---- |
835 * | state |
836 * We need to split the extent, and set the bit
837 * on the first half
838 */
839 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400840 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500841 *failed_start = start;
842 err = -EEXIST;
843 goto out;
844 }
845 err = split_state(tree, state, prealloc, end + 1);
846 BUG_ON(err == -EEXIST);
847
848 set_state_bits(tree, prealloc, bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400849 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500850 merge_state(tree, prealloc);
851 prealloc = NULL;
852 goto out;
853 }
854
855 goto search_again;
856
857out:
Chris Masoncad321a2008-12-17 14:51:42 -0500858 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500859 if (prealloc)
860 free_extent_state(prealloc);
861
862 return err;
863
864search_again:
865 if (start > end)
866 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500867 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500868 if (mask & __GFP_WAIT)
869 cond_resched();
870 goto again;
871}
Chris Masond1310b22008-01-24 16:13:08 -0500872
873/* wrappers around set/clear extent bit */
874int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
875 gfp_t mask)
876{
877 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400878 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500879}
Chris Masond1310b22008-01-24 16:13:08 -0500880
Chris Masone6dcd2d2008-07-17 12:53:50 -0400881int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
882 gfp_t mask)
883{
Chris Mason2c64c532009-09-02 15:04:12 -0400884 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, NULL,
885 mask);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400886}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400887
Chris Masond1310b22008-01-24 16:13:08 -0500888int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
889 int bits, gfp_t mask)
890{
891 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400892 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500893}
Chris Masond1310b22008-01-24 16:13:08 -0500894
895int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
896 int bits, gfp_t mask)
897{
Chris Mason2c64c532009-09-02 15:04:12 -0400898 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500899}
Chris Masond1310b22008-01-24 16:13:08 -0500900
901int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
902 gfp_t mask)
903{
904 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400905 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Chris Mason2c64c532009-09-02 15:04:12 -0400906 0, NULL, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500907}
Chris Masond1310b22008-01-24 16:13:08 -0500908
909int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
910 gfp_t mask)
911{
912 return clear_extent_bit(tree, start, end,
Chris Mason2c64c532009-09-02 15:04:12 -0400913 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
914 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500915}
Chris Masond1310b22008-01-24 16:13:08 -0500916
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
918 gfp_t mask)
919{
Chris Mason2c64c532009-09-02 15:04:12 -0400920 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0,
921 NULL, mask);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400922}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400923
Chris Masond1310b22008-01-24 16:13:08 -0500924int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
925 gfp_t mask)
926{
927 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400928 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500929}
Chris Masond1310b22008-01-24 16:13:08 -0500930
Christoph Hellwigb2950862008-12-02 09:54:17 -0500931static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500932 gfp_t mask)
933{
Chris Mason2c64c532009-09-02 15:04:12 -0400934 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
935 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500936}
Chris Masond1310b22008-01-24 16:13:08 -0500937
938int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
939 gfp_t mask)
940{
941 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400942 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500943}
Chris Masond1310b22008-01-24 16:13:08 -0500944
Chris Masond3977122009-01-05 21:25:51 -0500945static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
946 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500947{
Chris Mason2c64c532009-09-02 15:04:12 -0400948 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
949 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500950}
Chris Masond1310b22008-01-24 16:13:08 -0500951
Chris Masond1310b22008-01-24 16:13:08 -0500952int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
953{
954 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
955}
Chris Masond1310b22008-01-24 16:13:08 -0500956
Chris Masond352ac62008-09-29 15:18:18 -0400957/*
958 * either insert or lock state struct between start and end use mask to tell
959 * us if waiting is desired.
960 */
Chris Mason1edbb732009-09-02 13:24:36 -0400961int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400962 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500963{
964 int err;
965 u64 failed_start;
966 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400967 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400968 EXTENT_LOCKED, &failed_start,
969 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500970 if (err == -EEXIST && (mask & __GFP_WAIT)) {
971 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
972 start = failed_start;
973 } else {
974 break;
975 }
976 WARN_ON(start > end);
977 }
978 return err;
979}
Chris Masond1310b22008-01-24 16:13:08 -0500980
Chris Mason1edbb732009-09-02 13:24:36 -0400981int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
982{
Chris Mason2c64c532009-09-02 15:04:12 -0400983 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400984}
985
Josef Bacik25179202008-10-29 14:49:05 -0400986int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
987 gfp_t mask)
988{
989 int err;
990 u64 failed_start;
991
Chris Mason2c64c532009-09-02 15:04:12 -0400992 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
993 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400994 if (err == -EEXIST) {
995 if (failed_start > start)
996 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -0400997 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400998 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400999 }
Josef Bacik25179202008-10-29 14:49:05 -04001000 return 1;
1001}
Josef Bacik25179202008-10-29 14:49:05 -04001002
Chris Mason2c64c532009-09-02 15:04:12 -04001003int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1004 struct extent_state **cached, gfp_t mask)
1005{
1006 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1007 mask);
1008}
1009
Chris Masond1310b22008-01-24 16:13:08 -05001010int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1011 gfp_t mask)
1012{
Chris Mason2c64c532009-09-02 15:04:12 -04001013 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1014 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001015}
Chris Masond1310b22008-01-24 16:13:08 -05001016
1017/*
1018 * helper function to set pages and extents in the tree dirty
1019 */
1020int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1021{
1022 unsigned long index = start >> PAGE_CACHE_SHIFT;
1023 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1024 struct page *page;
1025
1026 while (index <= end_index) {
1027 page = find_get_page(tree->mapping, index);
1028 BUG_ON(!page);
1029 __set_page_dirty_nobuffers(page);
1030 page_cache_release(page);
1031 index++;
1032 }
Chris Masond1310b22008-01-24 16:13:08 -05001033 return 0;
1034}
Chris Masond1310b22008-01-24 16:13:08 -05001035
1036/*
1037 * helper function to set both pages and extents in the tree writeback
1038 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001039static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001040{
1041 unsigned long index = start >> PAGE_CACHE_SHIFT;
1042 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1043 struct page *page;
1044
1045 while (index <= end_index) {
1046 page = find_get_page(tree->mapping, index);
1047 BUG_ON(!page);
1048 set_page_writeback(page);
1049 page_cache_release(page);
1050 index++;
1051 }
Chris Masond1310b22008-01-24 16:13:08 -05001052 return 0;
1053}
Chris Masond1310b22008-01-24 16:13:08 -05001054
Chris Masond352ac62008-09-29 15:18:18 -04001055/*
1056 * find the first offset in the io tree with 'bits' set. zero is
1057 * returned if we find something, and *start_ret and *end_ret are
1058 * set to reflect the state struct that was found.
1059 *
1060 * If nothing was found, 1 is returned, < 0 on error
1061 */
Chris Masond1310b22008-01-24 16:13:08 -05001062int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1063 u64 *start_ret, u64 *end_ret, int bits)
1064{
1065 struct rb_node *node;
1066 struct extent_state *state;
1067 int ret = 1;
1068
Chris Masoncad321a2008-12-17 14:51:42 -05001069 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001070 /*
1071 * this search will find all the extents that end after
1072 * our range starts.
1073 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001074 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001075 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001076 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001077
Chris Masond3977122009-01-05 21:25:51 -05001078 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001079 state = rb_entry(node, struct extent_state, rb_node);
1080 if (state->end >= start && (state->state & bits)) {
1081 *start_ret = state->start;
1082 *end_ret = state->end;
1083 ret = 0;
1084 break;
1085 }
1086 node = rb_next(node);
1087 if (!node)
1088 break;
1089 }
1090out:
Chris Masoncad321a2008-12-17 14:51:42 -05001091 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001092 return ret;
1093}
Chris Masond1310b22008-01-24 16:13:08 -05001094
Chris Masond352ac62008-09-29 15:18:18 -04001095/* find the first state struct with 'bits' set after 'start', and
1096 * return it. tree->lock must be held. NULL will returned if
1097 * nothing was found after 'start'
1098 */
Chris Masond7fc6402008-02-18 12:12:38 -05001099struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1100 u64 start, int bits)
1101{
1102 struct rb_node *node;
1103 struct extent_state *state;
1104
1105 /*
1106 * this search will find all the extents that end after
1107 * our range starts.
1108 */
1109 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001110 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001111 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001112
Chris Masond3977122009-01-05 21:25:51 -05001113 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001114 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001115 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001116 return state;
Chris Masond3977122009-01-05 21:25:51 -05001117
Chris Masond7fc6402008-02-18 12:12:38 -05001118 node = rb_next(node);
1119 if (!node)
1120 break;
1121 }
1122out:
1123 return NULL;
1124}
Chris Masond7fc6402008-02-18 12:12:38 -05001125
Chris Masond352ac62008-09-29 15:18:18 -04001126/*
1127 * find a contiguous range of bytes in the file marked as delalloc, not
1128 * more than 'max_bytes'. start and end are used to return the range,
1129 *
1130 * 1 is returned if we find something, 0 if nothing was in the tree
1131 */
Chris Masonc8b97812008-10-29 14:49:59 -04001132static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1133 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001134{
1135 struct rb_node *node;
1136 struct extent_state *state;
1137 u64 cur_start = *start;
1138 u64 found = 0;
1139 u64 total_bytes = 0;
1140
Chris Masoncad321a2008-12-17 14:51:42 -05001141 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001142
Chris Masond1310b22008-01-24 16:13:08 -05001143 /*
1144 * this search will find all the extents that end after
1145 * our range starts.
1146 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001147 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001148 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001149 if (!found)
1150 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001151 goto out;
1152 }
1153
Chris Masond3977122009-01-05 21:25:51 -05001154 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001155 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001156 if (found && (state->start != cur_start ||
1157 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001158 goto out;
1159 }
1160 if (!(state->state & EXTENT_DELALLOC)) {
1161 if (!found)
1162 *end = state->end;
1163 goto out;
1164 }
Chris Masond1310b22008-01-24 16:13:08 -05001165 if (!found)
1166 *start = state->start;
1167 found++;
1168 *end = state->end;
1169 cur_start = state->end + 1;
1170 node = rb_next(node);
1171 if (!node)
1172 break;
1173 total_bytes += state->end - state->start + 1;
1174 if (total_bytes >= max_bytes)
1175 break;
1176 }
1177out:
Chris Masoncad321a2008-12-17 14:51:42 -05001178 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001179 return found;
1180}
1181
Chris Masonc8b97812008-10-29 14:49:59 -04001182static noinline int __unlock_for_delalloc(struct inode *inode,
1183 struct page *locked_page,
1184 u64 start, u64 end)
1185{
1186 int ret;
1187 struct page *pages[16];
1188 unsigned long index = start >> PAGE_CACHE_SHIFT;
1189 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1190 unsigned long nr_pages = end_index - index + 1;
1191 int i;
1192
1193 if (index == locked_page->index && end_index == index)
1194 return 0;
1195
Chris Masond3977122009-01-05 21:25:51 -05001196 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001197 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001198 min_t(unsigned long, nr_pages,
1199 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001200 for (i = 0; i < ret; i++) {
1201 if (pages[i] != locked_page)
1202 unlock_page(pages[i]);
1203 page_cache_release(pages[i]);
1204 }
1205 nr_pages -= ret;
1206 index += ret;
1207 cond_resched();
1208 }
1209 return 0;
1210}
1211
1212static noinline int lock_delalloc_pages(struct inode *inode,
1213 struct page *locked_page,
1214 u64 delalloc_start,
1215 u64 delalloc_end)
1216{
1217 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1218 unsigned long start_index = index;
1219 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1220 unsigned long pages_locked = 0;
1221 struct page *pages[16];
1222 unsigned long nrpages;
1223 int ret;
1224 int i;
1225
1226 /* the caller is responsible for locking the start index */
1227 if (index == locked_page->index && index == end_index)
1228 return 0;
1229
1230 /* skip the page at the start index */
1231 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001232 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001233 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001234 min_t(unsigned long,
1235 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001236 if (ret == 0) {
1237 ret = -EAGAIN;
1238 goto done;
1239 }
1240 /* now we have an array of pages, lock them all */
1241 for (i = 0; i < ret; i++) {
1242 /*
1243 * the caller is taking responsibility for
1244 * locked_page
1245 */
Chris Mason771ed682008-11-06 22:02:51 -05001246 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001247 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001248 if (!PageDirty(pages[i]) ||
1249 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001250 ret = -EAGAIN;
1251 unlock_page(pages[i]);
1252 page_cache_release(pages[i]);
1253 goto done;
1254 }
1255 }
Chris Masonc8b97812008-10-29 14:49:59 -04001256 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001257 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001258 }
Chris Masonc8b97812008-10-29 14:49:59 -04001259 nrpages -= ret;
1260 index += ret;
1261 cond_resched();
1262 }
1263 ret = 0;
1264done:
1265 if (ret && pages_locked) {
1266 __unlock_for_delalloc(inode, locked_page,
1267 delalloc_start,
1268 ((u64)(start_index + pages_locked - 1)) <<
1269 PAGE_CACHE_SHIFT);
1270 }
1271 return ret;
1272}
1273
1274/*
1275 * find a contiguous range of bytes in the file marked as delalloc, not
1276 * more than 'max_bytes'. start and end are used to return the range,
1277 *
1278 * 1 is returned if we find something, 0 if nothing was in the tree
1279 */
1280static noinline u64 find_lock_delalloc_range(struct inode *inode,
1281 struct extent_io_tree *tree,
1282 struct page *locked_page,
1283 u64 *start, u64 *end,
1284 u64 max_bytes)
1285{
1286 u64 delalloc_start;
1287 u64 delalloc_end;
1288 u64 found;
1289 int ret;
1290 int loops = 0;
1291
1292again:
1293 /* step one, find a bunch of delalloc bytes starting at start */
1294 delalloc_start = *start;
1295 delalloc_end = 0;
1296 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1297 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001298 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001299 *start = delalloc_start;
1300 *end = delalloc_end;
1301 return found;
1302 }
1303
1304 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001305 * start comes from the offset of locked_page. We have to lock
1306 * pages in order, so we can't process delalloc bytes before
1307 * locked_page
1308 */
Chris Masond3977122009-01-05 21:25:51 -05001309 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001310 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001311
1312 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001313 * make sure to limit the number of pages we try to lock down
1314 * if we're looping.
1315 */
Chris Masond3977122009-01-05 21:25:51 -05001316 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001317 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001318
Chris Masonc8b97812008-10-29 14:49:59 -04001319 /* step two, lock all the pages after the page that has start */
1320 ret = lock_delalloc_pages(inode, locked_page,
1321 delalloc_start, delalloc_end);
1322 if (ret == -EAGAIN) {
1323 /* some of the pages are gone, lets avoid looping by
1324 * shortening the size of the delalloc range we're searching
1325 */
1326 if (!loops) {
1327 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1328 max_bytes = PAGE_CACHE_SIZE - offset;
1329 loops = 1;
1330 goto again;
1331 } else {
1332 found = 0;
1333 goto out_failed;
1334 }
1335 }
1336 BUG_ON(ret);
1337
1338 /* step three, lock the state bits for the whole range */
1339 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1340
1341 /* then test to make sure it is all still delalloc */
1342 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1343 EXTENT_DELALLOC, 1);
1344 if (!ret) {
1345 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1346 __unlock_for_delalloc(inode, locked_page,
1347 delalloc_start, delalloc_end);
1348 cond_resched();
1349 goto again;
1350 }
1351 *start = delalloc_start;
1352 *end = delalloc_end;
1353out_failed:
1354 return found;
1355}
1356
1357int extent_clear_unlock_delalloc(struct inode *inode,
1358 struct extent_io_tree *tree,
1359 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001360 int unlock_pages,
1361 int clear_unlock,
1362 int clear_delalloc, int clear_dirty,
1363 int set_writeback,
Chris Masonc8b97812008-10-29 14:49:59 -04001364 int end_writeback)
1365{
1366 int ret;
1367 struct page *pages[16];
1368 unsigned long index = start >> PAGE_CACHE_SHIFT;
1369 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1370 unsigned long nr_pages = end_index - index + 1;
1371 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001372 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001373
Chris Mason771ed682008-11-06 22:02:51 -05001374 if (clear_unlock)
1375 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001376 if (clear_dirty)
1377 clear_bits |= EXTENT_DIRTY;
1378
Chris Mason771ed682008-11-06 22:02:51 -05001379 if (clear_delalloc)
1380 clear_bits |= EXTENT_DELALLOC;
1381
Chris Mason2c64c532009-09-02 15:04:12 -04001382 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05001383 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1384 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001385
Chris Masond3977122009-01-05 21:25:51 -05001386 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001387 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001388 min_t(unsigned long,
1389 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001390 for (i = 0; i < ret; i++) {
1391 if (pages[i] == locked_page) {
1392 page_cache_release(pages[i]);
1393 continue;
1394 }
1395 if (clear_dirty)
1396 clear_page_dirty_for_io(pages[i]);
1397 if (set_writeback)
1398 set_page_writeback(pages[i]);
1399 if (end_writeback)
1400 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001401 if (unlock_pages)
1402 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001403 page_cache_release(pages[i]);
1404 }
1405 nr_pages -= ret;
1406 index += ret;
1407 cond_resched();
1408 }
1409 return 0;
1410}
Chris Masonc8b97812008-10-29 14:49:59 -04001411
Chris Masond352ac62008-09-29 15:18:18 -04001412/*
1413 * count the number of bytes in the tree that have a given bit(s)
1414 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1415 * cached. The total number found is returned.
1416 */
Chris Masond1310b22008-01-24 16:13:08 -05001417u64 count_range_bits(struct extent_io_tree *tree,
1418 u64 *start, u64 search_end, u64 max_bytes,
1419 unsigned long bits)
1420{
1421 struct rb_node *node;
1422 struct extent_state *state;
1423 u64 cur_start = *start;
1424 u64 total_bytes = 0;
1425 int found = 0;
1426
1427 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001428 WARN_ON(1);
1429 return 0;
1430 }
1431
Chris Masoncad321a2008-12-17 14:51:42 -05001432 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001433 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1434 total_bytes = tree->dirty_bytes;
1435 goto out;
1436 }
1437 /*
1438 * this search will find all the extents that end after
1439 * our range starts.
1440 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001441 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001442 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001443 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001444
Chris Masond3977122009-01-05 21:25:51 -05001445 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001446 state = rb_entry(node, struct extent_state, rb_node);
1447 if (state->start > search_end)
1448 break;
1449 if (state->end >= cur_start && (state->state & bits)) {
1450 total_bytes += min(search_end, state->end) + 1 -
1451 max(cur_start, state->start);
1452 if (total_bytes >= max_bytes)
1453 break;
1454 if (!found) {
1455 *start = state->start;
1456 found = 1;
1457 }
1458 }
1459 node = rb_next(node);
1460 if (!node)
1461 break;
1462 }
1463out:
Chris Masoncad321a2008-12-17 14:51:42 -05001464 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001465 return total_bytes;
1466}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
1469 * set the private field for a given byte offset in the tree. If there isn't
1470 * an extent_state there already, this does nothing.
1471 */
Chris Masond1310b22008-01-24 16:13:08 -05001472int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1473{
1474 struct rb_node *node;
1475 struct extent_state *state;
1476 int ret = 0;
1477
Chris Masoncad321a2008-12-17 14:51:42 -05001478 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001479 /*
1480 * this search will find all the extents that end after
1481 * our range starts.
1482 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001483 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001484 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001485 ret = -ENOENT;
1486 goto out;
1487 }
1488 state = rb_entry(node, struct extent_state, rb_node);
1489 if (state->start != start) {
1490 ret = -ENOENT;
1491 goto out;
1492 }
1493 state->private = private;
1494out:
Chris Masoncad321a2008-12-17 14:51:42 -05001495 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001496 return ret;
1497}
1498
1499int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1500{
1501 struct rb_node *node;
1502 struct extent_state *state;
1503 int ret = 0;
1504
Chris Masoncad321a2008-12-17 14:51:42 -05001505 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001506 /*
1507 * this search will find all the extents that end after
1508 * our range starts.
1509 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001510 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001511 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001512 ret = -ENOENT;
1513 goto out;
1514 }
1515 state = rb_entry(node, struct extent_state, rb_node);
1516 if (state->start != start) {
1517 ret = -ENOENT;
1518 goto out;
1519 }
1520 *private = state->private;
1521out:
Chris Masoncad321a2008-12-17 14:51:42 -05001522 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001523 return ret;
1524}
1525
1526/*
1527 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001528 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001529 * has the bits set. Otherwise, 1 is returned if any bit in the
1530 * range is found set.
1531 */
1532int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1533 int bits, int filled)
1534{
1535 struct extent_state *state = NULL;
1536 struct rb_node *node;
1537 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001538
Chris Masoncad321a2008-12-17 14:51:42 -05001539 spin_lock(&tree->lock);
Chris Mason80ea96b2008-02-01 14:51:59 -05001540 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001541 while (node && start <= end) {
1542 state = rb_entry(node, struct extent_state, rb_node);
1543
1544 if (filled && state->start > start) {
1545 bitset = 0;
1546 break;
1547 }
1548
1549 if (state->start > end)
1550 break;
1551
1552 if (state->state & bits) {
1553 bitset = 1;
1554 if (!filled)
1555 break;
1556 } else if (filled) {
1557 bitset = 0;
1558 break;
1559 }
1560 start = state->end + 1;
1561 if (start > end)
1562 break;
1563 node = rb_next(node);
1564 if (!node) {
1565 if (filled)
1566 bitset = 0;
1567 break;
1568 }
1569 }
Chris Masoncad321a2008-12-17 14:51:42 -05001570 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001571 return bitset;
1572}
Chris Masond1310b22008-01-24 16:13:08 -05001573
1574/*
1575 * helper function to set a given page up to date if all the
1576 * extents in the tree for that page are up to date
1577 */
1578static int check_page_uptodate(struct extent_io_tree *tree,
1579 struct page *page)
1580{
1581 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1582 u64 end = start + PAGE_CACHE_SIZE - 1;
1583 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1584 SetPageUptodate(page);
1585 return 0;
1586}
1587
1588/*
1589 * helper function to unlock a page if all the extents in the tree
1590 * for that page are unlocked
1591 */
1592static int check_page_locked(struct extent_io_tree *tree,
1593 struct page *page)
1594{
1595 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1596 u64 end = start + PAGE_CACHE_SIZE - 1;
1597 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1598 unlock_page(page);
1599 return 0;
1600}
1601
1602/*
1603 * helper function to end page writeback if all the extents
1604 * in the tree for that page are done with writeback
1605 */
1606static int check_page_writeback(struct extent_io_tree *tree,
1607 struct page *page)
1608{
Chris Mason1edbb732009-09-02 13:24:36 -04001609 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001610 return 0;
1611}
1612
1613/* lots and lots of room for performance fixes in the end_bio funcs */
1614
1615/*
1616 * after a writepage IO is done, we need to:
1617 * clear the uptodate bits on error
1618 * clear the writeback bits in the extent tree for this IO
1619 * end_page_writeback if the page has no more pending IO
1620 *
1621 * Scheduling is not allowed, so the extent state tree is expected
1622 * to have one and only one object corresponding to this IO.
1623 */
Chris Masond1310b22008-01-24 16:13:08 -05001624static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001625{
Chris Mason1259ab72008-05-12 13:39:03 -04001626 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001627 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001628 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001629 u64 start;
1630 u64 end;
1631 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001632 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001633
Chris Masond1310b22008-01-24 16:13:08 -05001634 do {
1635 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001636 tree = &BTRFS_I(page->mapping->host)->io_tree;
1637
Chris Masond1310b22008-01-24 16:13:08 -05001638 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1639 bvec->bv_offset;
1640 end = start + bvec->bv_len - 1;
1641
1642 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1643 whole_page = 1;
1644 else
1645 whole_page = 0;
1646
1647 if (--bvec >= bio->bi_io_vec)
1648 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001649 if (tree->ops && tree->ops->writepage_end_io_hook) {
1650 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001651 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001652 if (ret)
1653 uptodate = 0;
1654 }
1655
1656 if (!uptodate && tree->ops &&
1657 tree->ops->writepage_io_failed_hook) {
1658 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001659 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001660 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001661 uptodate = (err == 0);
1662 continue;
1663 }
1664 }
1665
Chris Masond1310b22008-01-24 16:13:08 -05001666 if (!uptodate) {
Chris Mason1edbb732009-09-02 13:24:36 -04001667 clear_extent_uptodate(tree, start, end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001668 ClearPageUptodate(page);
1669 SetPageError(page);
1670 }
Chris Mason70dec802008-01-29 09:59:12 -05001671
Chris Masond1310b22008-01-24 16:13:08 -05001672 if (whole_page)
1673 end_page_writeback(page);
1674 else
1675 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001676 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001677
Chris Masond1310b22008-01-24 16:13:08 -05001678 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001679}
1680
1681/*
1682 * after a readpage IO is done, we need to:
1683 * clear the uptodate bits on error
1684 * set the uptodate bits if things worked
1685 * set the page up to date if all extents in the tree are uptodate
1686 * clear the lock bit in the extent tree
1687 * unlock the page if there are no other extents locked for it
1688 *
1689 * Scheduling is not allowed, so the extent state tree is expected
1690 * to have one and only one object corresponding to this IO.
1691 */
Chris Masond1310b22008-01-24 16:13:08 -05001692static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001693{
1694 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1695 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001696 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001697 u64 start;
1698 u64 end;
1699 int whole_page;
1700 int ret;
1701
Chris Masond20f7042008-12-08 16:58:54 -05001702 if (err)
1703 uptodate = 0;
1704
Chris Masond1310b22008-01-24 16:13:08 -05001705 do {
1706 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001707 tree = &BTRFS_I(page->mapping->host)->io_tree;
1708
Chris Masond1310b22008-01-24 16:13:08 -05001709 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1710 bvec->bv_offset;
1711 end = start + bvec->bv_len - 1;
1712
1713 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1714 whole_page = 1;
1715 else
1716 whole_page = 0;
1717
1718 if (--bvec >= bio->bi_io_vec)
1719 prefetchw(&bvec->bv_page->flags);
1720
1721 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001722 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001723 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001724 if (ret)
1725 uptodate = 0;
1726 }
Chris Mason7e383262008-04-09 16:28:12 -04001727 if (!uptodate && tree->ops &&
1728 tree->ops->readpage_io_failed_hook) {
1729 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001730 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001731 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001732 uptodate =
1733 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001734 if (err)
1735 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001736 continue;
1737 }
1738 }
Chris Mason70dec802008-01-29 09:59:12 -05001739
Chris Mason771ed682008-11-06 22:02:51 -05001740 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001741 set_extent_uptodate(tree, start, end,
1742 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001743 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001744 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001745
Chris Mason70dec802008-01-29 09:59:12 -05001746 if (whole_page) {
1747 if (uptodate) {
1748 SetPageUptodate(page);
1749 } else {
1750 ClearPageUptodate(page);
1751 SetPageError(page);
1752 }
Chris Masond1310b22008-01-24 16:13:08 -05001753 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001754 } else {
1755 if (uptodate) {
1756 check_page_uptodate(tree, page);
1757 } else {
1758 ClearPageUptodate(page);
1759 SetPageError(page);
1760 }
Chris Masond1310b22008-01-24 16:13:08 -05001761 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001762 }
Chris Masond1310b22008-01-24 16:13:08 -05001763 } while (bvec >= bio->bi_io_vec);
1764
1765 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001766}
1767
1768/*
1769 * IO done from prepare_write is pretty simple, we just unlock
1770 * the structs in the extent tree when done, and set the uptodate bits
1771 * as appropriate.
1772 */
Chris Masond1310b22008-01-24 16:13:08 -05001773static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001774{
1775 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1776 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001777 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001778 u64 start;
1779 u64 end;
1780
Chris Masond1310b22008-01-24 16:13:08 -05001781 do {
1782 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001783 tree = &BTRFS_I(page->mapping->host)->io_tree;
1784
Chris Masond1310b22008-01-24 16:13:08 -05001785 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1786 bvec->bv_offset;
1787 end = start + bvec->bv_len - 1;
1788
1789 if (--bvec >= bio->bi_io_vec)
1790 prefetchw(&bvec->bv_page->flags);
1791
1792 if (uptodate) {
1793 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1794 } else {
1795 ClearPageUptodate(page);
1796 SetPageError(page);
1797 }
1798
1799 unlock_extent(tree, start, end, GFP_ATOMIC);
1800
1801 } while (bvec >= bio->bi_io_vec);
1802
1803 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001804}
1805
1806static struct bio *
1807extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1808 gfp_t gfp_flags)
1809{
1810 struct bio *bio;
1811
1812 bio = bio_alloc(gfp_flags, nr_vecs);
1813
1814 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1815 while (!bio && (nr_vecs /= 2))
1816 bio = bio_alloc(gfp_flags, nr_vecs);
1817 }
1818
1819 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001820 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001821 bio->bi_bdev = bdev;
1822 bio->bi_sector = first_sector;
1823 }
1824 return bio;
1825}
1826
Chris Masonc8b97812008-10-29 14:49:59 -04001827static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1828 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001829{
Chris Masond1310b22008-01-24 16:13:08 -05001830 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001831 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1832 struct page *page = bvec->bv_page;
1833 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001834 u64 start;
1835 u64 end;
1836
1837 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1838 end = start + bvec->bv_len - 1;
1839
David Woodhouse902b22f2008-08-20 08:51:49 -04001840 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001841
1842 bio_get(bio);
1843
Chris Mason065631f2008-02-20 12:07:25 -05001844 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001845 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001846 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001847 else
1848 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001849 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1850 ret = -EOPNOTSUPP;
1851 bio_put(bio);
1852 return ret;
1853}
1854
1855static int submit_extent_page(int rw, struct extent_io_tree *tree,
1856 struct page *page, sector_t sector,
1857 size_t size, unsigned long offset,
1858 struct block_device *bdev,
1859 struct bio **bio_ret,
1860 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001861 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001862 int mirror_num,
1863 unsigned long prev_bio_flags,
1864 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001865{
1866 int ret = 0;
1867 struct bio *bio;
1868 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001869 int contig = 0;
1870 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1871 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001872 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001873
1874 if (bio_ret && *bio_ret) {
1875 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001876 if (old_compressed)
1877 contig = bio->bi_sector == sector;
1878 else
1879 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1880 sector;
1881
1882 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001883 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001884 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1885 bio_flags)) ||
1886 bio_add_page(bio, page, page_size, offset) < page_size) {
1887 ret = submit_one_bio(rw, bio, mirror_num,
1888 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001889 bio = NULL;
1890 } else {
1891 return 0;
1892 }
1893 }
Chris Masonc8b97812008-10-29 14:49:59 -04001894 if (this_compressed)
1895 nr = BIO_MAX_PAGES;
1896 else
1897 nr = bio_get_nr_vecs(bdev);
1898
Chris Masond1310b22008-01-24 16:13:08 -05001899 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001900
Chris Masonc8b97812008-10-29 14:49:59 -04001901 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001902 bio->bi_end_io = end_io_func;
1903 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001904
Chris Masond3977122009-01-05 21:25:51 -05001905 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001906 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001907 else
Chris Masonc8b97812008-10-29 14:49:59 -04001908 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001909
1910 return ret;
1911}
1912
1913void set_page_extent_mapped(struct page *page)
1914{
1915 if (!PagePrivate(page)) {
1916 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001917 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04001918 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001919 }
1920}
1921
Christoph Hellwigb2950862008-12-02 09:54:17 -05001922static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001923{
1924 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1925}
1926
1927/*
1928 * basic readpage implementation. Locked extent state structs are inserted
1929 * into the tree that are removed when the IO is done (by the end_io
1930 * handlers)
1931 */
1932static int __extent_read_full_page(struct extent_io_tree *tree,
1933 struct page *page,
1934 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001935 struct bio **bio, int mirror_num,
1936 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001937{
1938 struct inode *inode = page->mapping->host;
1939 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1940 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1941 u64 end;
1942 u64 cur = start;
1943 u64 extent_offset;
1944 u64 last_byte = i_size_read(inode);
1945 u64 block_start;
1946 u64 cur_end;
1947 sector_t sector;
1948 struct extent_map *em;
1949 struct block_device *bdev;
1950 int ret;
1951 int nr = 0;
1952 size_t page_offset = 0;
1953 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001954 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001955 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001956 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001957
1958 set_page_extent_mapped(page);
1959
1960 end = page_end;
1961 lock_extent(tree, start, end, GFP_NOFS);
1962
Chris Masonc8b97812008-10-29 14:49:59 -04001963 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1964 char *userpage;
1965 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1966
1967 if (zero_offset) {
1968 iosize = PAGE_CACHE_SIZE - zero_offset;
1969 userpage = kmap_atomic(page, KM_USER0);
1970 memset(userpage + zero_offset, 0, iosize);
1971 flush_dcache_page(page);
1972 kunmap_atomic(userpage, KM_USER0);
1973 }
1974 }
Chris Masond1310b22008-01-24 16:13:08 -05001975 while (cur <= end) {
1976 if (cur >= last_byte) {
1977 char *userpage;
1978 iosize = PAGE_CACHE_SIZE - page_offset;
1979 userpage = kmap_atomic(page, KM_USER0);
1980 memset(userpage + page_offset, 0, iosize);
1981 flush_dcache_page(page);
1982 kunmap_atomic(userpage, KM_USER0);
1983 set_extent_uptodate(tree, cur, cur + iosize - 1,
1984 GFP_NOFS);
1985 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1986 break;
1987 }
1988 em = get_extent(inode, page, page_offset, cur,
1989 end - cur + 1, 0);
1990 if (IS_ERR(em) || !em) {
1991 SetPageError(page);
1992 unlock_extent(tree, cur, end, GFP_NOFS);
1993 break;
1994 }
Chris Masond1310b22008-01-24 16:13:08 -05001995 extent_offset = cur - em->start;
1996 BUG_ON(extent_map_end(em) <= cur);
1997 BUG_ON(end < cur);
1998
Chris Masonc8b97812008-10-29 14:49:59 -04001999 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2000 this_bio_flag = EXTENT_BIO_COMPRESSED;
2001
Chris Masond1310b22008-01-24 16:13:08 -05002002 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2003 cur_end = min(extent_map_end(em) - 1, end);
2004 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002005 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2006 disk_io_size = em->block_len;
2007 sector = em->block_start >> 9;
2008 } else {
2009 sector = (em->block_start + extent_offset) >> 9;
2010 disk_io_size = iosize;
2011 }
Chris Masond1310b22008-01-24 16:13:08 -05002012 bdev = em->bdev;
2013 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002014 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2015 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002016 free_extent_map(em);
2017 em = NULL;
2018
2019 /* we've found a hole, just zero and go on */
2020 if (block_start == EXTENT_MAP_HOLE) {
2021 char *userpage;
2022 userpage = kmap_atomic(page, KM_USER0);
2023 memset(userpage + page_offset, 0, iosize);
2024 flush_dcache_page(page);
2025 kunmap_atomic(userpage, KM_USER0);
2026
2027 set_extent_uptodate(tree, cur, cur + iosize - 1,
2028 GFP_NOFS);
2029 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2030 cur = cur + iosize;
2031 page_offset += iosize;
2032 continue;
2033 }
2034 /* the get_extent function already copied into the page */
2035 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002036 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002037 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2038 cur = cur + iosize;
2039 page_offset += iosize;
2040 continue;
2041 }
Chris Mason70dec802008-01-29 09:59:12 -05002042 /* we have an inline extent but it didn't get marked up
2043 * to date. Error out
2044 */
2045 if (block_start == EXTENT_MAP_INLINE) {
2046 SetPageError(page);
2047 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2048 cur = cur + iosize;
2049 page_offset += iosize;
2050 continue;
2051 }
Chris Masond1310b22008-01-24 16:13:08 -05002052
2053 ret = 0;
2054 if (tree->ops && tree->ops->readpage_io_hook) {
2055 ret = tree->ops->readpage_io_hook(page, cur,
2056 cur + iosize - 1);
2057 }
2058 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002059 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2060 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002061 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002062 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002063 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002064 end_bio_extent_readpage, mirror_num,
2065 *bio_flags,
2066 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002067 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002068 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002069 }
2070 if (ret)
2071 SetPageError(page);
2072 cur = cur + iosize;
2073 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002074 }
2075 if (!nr) {
2076 if (!PageError(page))
2077 SetPageUptodate(page);
2078 unlock_page(page);
2079 }
2080 return 0;
2081}
2082
2083int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2084 get_extent_t *get_extent)
2085{
2086 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002087 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002088 int ret;
2089
Chris Masonc8b97812008-10-29 14:49:59 -04002090 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2091 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002092 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002093 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002094 return ret;
2095}
Chris Masond1310b22008-01-24 16:13:08 -05002096
Chris Mason11c83492009-04-20 15:50:09 -04002097static noinline void update_nr_written(struct page *page,
2098 struct writeback_control *wbc,
2099 unsigned long nr_written)
2100{
2101 wbc->nr_to_write -= nr_written;
2102 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2103 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2104 page->mapping->writeback_index = page->index + nr_written;
2105}
2106
Chris Masond1310b22008-01-24 16:13:08 -05002107/*
2108 * the writepage semantics are similar to regular writepage. extent
2109 * records are inserted to lock ranges in the tree, and as dirty areas
2110 * are found, they are marked writeback. Then the lock bits are removed
2111 * and the end_io handler clears the writeback ranges
2112 */
2113static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2114 void *data)
2115{
2116 struct inode *inode = page->mapping->host;
2117 struct extent_page_data *epd = data;
2118 struct extent_io_tree *tree = epd->tree;
2119 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2120 u64 delalloc_start;
2121 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2122 u64 end;
2123 u64 cur = start;
2124 u64 extent_offset;
2125 u64 last_byte = i_size_read(inode);
2126 u64 block_start;
2127 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002128 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002129 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002130 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002131 struct extent_map *em;
2132 struct block_device *bdev;
2133 int ret;
2134 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002135 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002136 size_t blocksize;
2137 loff_t i_size = i_size_read(inode);
2138 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2139 u64 nr_delalloc;
2140 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002141 int page_started;
2142 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002143 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002144 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002145
Chris Masonffbd5172009-04-20 15:50:09 -04002146 if (wbc->sync_mode == WB_SYNC_ALL)
2147 write_flags = WRITE_SYNC_PLUG;
2148 else
2149 write_flags = WRITE;
2150
Chris Masond1310b22008-01-24 16:13:08 -05002151 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002152 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002153 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002154 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002155 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002156 unlock_page(page);
2157 return 0;
2158 }
2159
2160 if (page->index == end_index) {
2161 char *userpage;
2162
Chris Masond1310b22008-01-24 16:13:08 -05002163 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002164 memset(userpage + pg_offset, 0,
2165 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002166 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002167 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002168 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002169 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002170
2171 set_page_extent_mapped(page);
2172
2173 delalloc_start = start;
2174 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002175 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002176 if (!epd->extent_locked) {
Chris Masona97adc92009-08-07 09:28:20 -04002177 u64 delalloc_to_write;
Chris Mason11c83492009-04-20 15:50:09 -04002178 /*
2179 * make sure the wbc mapping index is at least updated
2180 * to this page.
2181 */
2182 update_nr_written(page, wbc, 0);
2183
Chris Masond3977122009-01-05 21:25:51 -05002184 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002185 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002186 page,
2187 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002188 &delalloc_end,
2189 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002190 if (nr_delalloc == 0) {
2191 delalloc_start = delalloc_end + 1;
2192 continue;
2193 }
2194 tree->ops->fill_delalloc(inode, page, delalloc_start,
2195 delalloc_end, &page_started,
2196 &nr_written);
Chris Masona97adc92009-08-07 09:28:20 -04002197 delalloc_to_write = (delalloc_end -
2198 max_t(u64, page_offset(page),
2199 delalloc_start) + 1) >>
2200 PAGE_CACHE_SHIFT;
2201 if (wbc->nr_to_write < delalloc_to_write) {
2202 wbc->nr_to_write = min_t(long, 8192,
2203 delalloc_to_write);
2204 }
Chris Masond1310b22008-01-24 16:13:08 -05002205 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002206 }
Chris Masonc8b97812008-10-29 14:49:59 -04002207
Chris Mason771ed682008-11-06 22:02:51 -05002208 /* did the fill delalloc function already unlock and start
2209 * the IO?
2210 */
2211 if (page_started) {
2212 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002213 /*
2214 * we've unlocked the page, so we can't update
2215 * the mapping's writeback index, just update
2216 * nr_to_write.
2217 */
2218 wbc->nr_to_write -= nr_written;
2219 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002220 }
Chris Masonc8b97812008-10-29 14:49:59 -04002221 }
Chris Mason2c64c532009-09-02 15:04:12 -04002222 lock_extent_bits(tree, start, page_end, 0, &cached_state, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002223
Chris Masone6dcd2d2008-07-17 12:53:50 -04002224 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002225
Chris Mason247e7432008-07-17 12:53:51 -04002226 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002227 ret = tree->ops->writepage_start_hook(page, start,
2228 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002229 if (ret == -EAGAIN) {
Chris Mason2c64c532009-09-02 15:04:12 -04002230 unlock_extent_cached(tree, start, page_end,
2231 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002232 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002233 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002234 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002235 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002236 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002237 }
2238 }
2239
Chris Mason11c83492009-04-20 15:50:09 -04002240 /*
2241 * we don't want to touch the inode after unlocking the page,
2242 * so we update the mapping writeback index now
2243 */
2244 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002245
Chris Masond1310b22008-01-24 16:13:08 -05002246 end = page_end;
Chris Masond3977122009-01-05 21:25:51 -05002247 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2248 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
Chris Masond1310b22008-01-24 16:13:08 -05002249
2250 if (last_byte <= start) {
Chris Mason1edbb732009-09-02 13:24:36 -04002251 clear_extent_bit(tree, start, page_end,
2252 EXTENT_LOCKED | EXTENT_DIRTY,
Chris Mason2c64c532009-09-02 15:04:12 -04002253 1, 0, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002254 if (tree->ops && tree->ops->writepage_end_io_hook)
2255 tree->ops->writepage_end_io_hook(page, start,
2256 page_end, NULL, 1);
2257 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002258 goto done;
2259 }
2260
Chris Masond1310b22008-01-24 16:13:08 -05002261 blocksize = inode->i_sb->s_blocksize;
2262
2263 while (cur <= end) {
2264 if (cur >= last_byte) {
Chris Mason2c64c532009-09-02 15:04:12 -04002265 unlock_extent_cached(tree, unlock_start, page_end,
2266 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002267 if (tree->ops && tree->ops->writepage_end_io_hook)
2268 tree->ops->writepage_end_io_hook(page, cur,
2269 page_end, NULL, 1);
2270 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002271 break;
2272 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002273 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002274 end - cur + 1, 1);
2275 if (IS_ERR(em) || !em) {
2276 SetPageError(page);
2277 break;
2278 }
2279
2280 extent_offset = cur - em->start;
2281 BUG_ON(extent_map_end(em) <= cur);
2282 BUG_ON(end < cur);
2283 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2284 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2285 sector = (em->block_start + extent_offset) >> 9;
2286 bdev = em->bdev;
2287 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002288 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002289 free_extent_map(em);
2290 em = NULL;
2291
Chris Masonc8b97812008-10-29 14:49:59 -04002292 /*
2293 * compressed and inline extents are written through other
2294 * paths in the FS
2295 */
2296 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002297 block_start == EXTENT_MAP_INLINE) {
Chris Mason2c64c532009-09-02 15:04:12 -04002298 unlock_extent_cached(tree, unlock_start,
2299 cur + iosize - 1, &cached_state,
2300 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002301
Chris Masonc8b97812008-10-29 14:49:59 -04002302 /*
2303 * end_io notification does not happen here for
2304 * compressed extents
2305 */
2306 if (!compressed && tree->ops &&
2307 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002308 tree->ops->writepage_end_io_hook(page, cur,
2309 cur + iosize - 1,
2310 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002311 else if (compressed) {
2312 /* we don't want to end_page_writeback on
2313 * a compressed extent. this happens
2314 * elsewhere
2315 */
2316 nr++;
2317 }
2318
2319 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002320 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002321 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002322 continue;
2323 }
Chris Masond1310b22008-01-24 16:13:08 -05002324 /* leave this out until we have a page_mkwrite call */
2325 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2326 EXTENT_DIRTY, 0)) {
2327 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002328 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002329 continue;
2330 }
Chris Masonc8b97812008-10-29 14:49:59 -04002331
Chris Masond1310b22008-01-24 16:13:08 -05002332 if (tree->ops && tree->ops->writepage_io_hook) {
2333 ret = tree->ops->writepage_io_hook(page, cur,
2334 cur + iosize - 1);
2335 } else {
2336 ret = 0;
2337 }
Chris Mason1259ab72008-05-12 13:39:03 -04002338 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002339 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002340 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002341 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002342
Chris Masond1310b22008-01-24 16:13:08 -05002343 set_range_writeback(tree, cur, cur + iosize - 1);
2344 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002345 printk(KERN_ERR "btrfs warning page %lu not "
2346 "writeback, cur %llu end %llu\n",
2347 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002348 (unsigned long long)end);
2349 }
2350
Chris Masonffbd5172009-04-20 15:50:09 -04002351 ret = submit_extent_page(write_flags, tree, page,
2352 sector, iosize, pg_offset,
2353 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002354 end_bio_extent_writepage,
2355 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002356 if (ret)
2357 SetPageError(page);
2358 }
2359 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002360 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002361 nr++;
2362 }
2363done:
2364 if (nr == 0) {
2365 /* make sure the mapping tag for page dirty gets cleared */
2366 set_page_writeback(page);
2367 end_page_writeback(page);
2368 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002369 if (unlock_start <= page_end)
Chris Mason2c64c532009-09-02 15:04:12 -04002370 unlock_extent_cached(tree, unlock_start, page_end,
2371 &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002372 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002373
Chris Mason11c83492009-04-20 15:50:09 -04002374done_unlocked:
2375
Chris Mason2c64c532009-09-02 15:04:12 -04002376 /* drop our reference on any cached states */
2377 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002378 return 0;
2379}
2380
Chris Masond1310b22008-01-24 16:13:08 -05002381/**
Chris Mason4bef0842008-09-08 11:18:08 -04002382 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05002383 * @mapping: address space structure to write
2384 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2385 * @writepage: function called for each page
2386 * @data: data passed to writepage function
2387 *
2388 * If a page is already under I/O, write_cache_pages() skips it, even
2389 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2390 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2391 * and msync() need to guarantee that all the data which was dirty at the time
2392 * the call was made get new I/O started against them. If wbc->sync_mode is
2393 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2394 * existing IO to complete.
2395 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002396static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002397 struct address_space *mapping,
2398 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002399 writepage_t writepage, void *data,
2400 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002401{
Chris Masond1310b22008-01-24 16:13:08 -05002402 int ret = 0;
2403 int done = 0;
2404 struct pagevec pvec;
2405 int nr_pages;
2406 pgoff_t index;
2407 pgoff_t end; /* Inclusive */
2408 int scanned = 0;
2409 int range_whole = 0;
2410
Chris Masond1310b22008-01-24 16:13:08 -05002411 pagevec_init(&pvec, 0);
2412 if (wbc->range_cyclic) {
2413 index = mapping->writeback_index; /* Start from prev offset */
2414 end = -1;
2415 } else {
2416 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2417 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2418 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2419 range_whole = 1;
2420 scanned = 1;
2421 }
2422retry:
2423 while (!done && (index <= end) &&
2424 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002425 PAGECACHE_TAG_DIRTY, min(end - index,
2426 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002427 unsigned i;
2428
2429 scanned = 1;
2430 for (i = 0; i < nr_pages; i++) {
2431 struct page *page = pvec.pages[i];
2432
2433 /*
2434 * At this point we hold neither mapping->tree_lock nor
2435 * lock on the page itself: the page may be truncated or
2436 * invalidated (changing page->mapping to NULL), or even
2437 * swizzled back from swapper_space to tmpfs file
2438 * mapping
2439 */
Chris Mason4bef0842008-09-08 11:18:08 -04002440 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2441 tree->ops->write_cache_pages_lock_hook(page);
2442 else
2443 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002444
2445 if (unlikely(page->mapping != mapping)) {
2446 unlock_page(page);
2447 continue;
2448 }
2449
2450 if (!wbc->range_cyclic && page->index > end) {
2451 done = 1;
2452 unlock_page(page);
2453 continue;
2454 }
2455
Chris Masond2c3f4f2008-11-19 12:44:22 -05002456 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002457 if (PageWriteback(page))
2458 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002459 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002460 }
Chris Masond1310b22008-01-24 16:13:08 -05002461
2462 if (PageWriteback(page) ||
2463 !clear_page_dirty_for_io(page)) {
2464 unlock_page(page);
2465 continue;
2466 }
2467
2468 ret = (*writepage)(page, wbc, data);
2469
2470 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2471 unlock_page(page);
2472 ret = 0;
2473 }
Chris Mason771ed682008-11-06 22:02:51 -05002474 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002475 done = 1;
Chris Masond1310b22008-01-24 16:13:08 -05002476 }
2477 pagevec_release(&pvec);
2478 cond_resched();
2479 }
2480 if (!scanned && !done) {
2481 /*
2482 * We hit the last page and there is more work to be done: wrap
2483 * back to the start of the file
2484 */
2485 scanned = 1;
2486 index = 0;
2487 goto retry;
2488 }
Chris Masond1310b22008-01-24 16:13:08 -05002489 return ret;
2490}
Chris Masond1310b22008-01-24 16:13:08 -05002491
Chris Masonffbd5172009-04-20 15:50:09 -04002492static void flush_epd_write_bio(struct extent_page_data *epd)
2493{
2494 if (epd->bio) {
2495 if (epd->sync_io)
2496 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2497 else
2498 submit_one_bio(WRITE, epd->bio, 0, 0);
2499 epd->bio = NULL;
2500 }
2501}
2502
Chris Masond2c3f4f2008-11-19 12:44:22 -05002503static noinline void flush_write_bio(void *data)
2504{
2505 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002506 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002507}
2508
Chris Masond1310b22008-01-24 16:13:08 -05002509int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2510 get_extent_t *get_extent,
2511 struct writeback_control *wbc)
2512{
2513 int ret;
2514 struct address_space *mapping = page->mapping;
2515 struct extent_page_data epd = {
2516 .bio = NULL,
2517 .tree = tree,
2518 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002519 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002520 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002521 };
2522 struct writeback_control wbc_writepages = {
2523 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002524 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002525 .older_than_this = NULL,
2526 .nr_to_write = 64,
2527 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2528 .range_end = (loff_t)-1,
2529 };
2530
Chris Masond1310b22008-01-24 16:13:08 -05002531 ret = __extent_writepage(page, wbc, &epd);
2532
Chris Mason4bef0842008-09-08 11:18:08 -04002533 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002534 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002535 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002536 return ret;
2537}
Chris Masond1310b22008-01-24 16:13:08 -05002538
Chris Mason771ed682008-11-06 22:02:51 -05002539int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2540 u64 start, u64 end, get_extent_t *get_extent,
2541 int mode)
2542{
2543 int ret = 0;
2544 struct address_space *mapping = inode->i_mapping;
2545 struct page *page;
2546 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2547 PAGE_CACHE_SHIFT;
2548
2549 struct extent_page_data epd = {
2550 .bio = NULL,
2551 .tree = tree,
2552 .get_extent = get_extent,
2553 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002554 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002555 };
2556 struct writeback_control wbc_writepages = {
2557 .bdi = inode->i_mapping->backing_dev_info,
2558 .sync_mode = mode,
2559 .older_than_this = NULL,
2560 .nr_to_write = nr_pages * 2,
2561 .range_start = start,
2562 .range_end = end + 1,
2563 };
2564
Chris Masond3977122009-01-05 21:25:51 -05002565 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002566 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2567 if (clear_page_dirty_for_io(page))
2568 ret = __extent_writepage(page, &wbc_writepages, &epd);
2569 else {
2570 if (tree->ops && tree->ops->writepage_end_io_hook)
2571 tree->ops->writepage_end_io_hook(page, start,
2572 start + PAGE_CACHE_SIZE - 1,
2573 NULL, 1);
2574 unlock_page(page);
2575 }
2576 page_cache_release(page);
2577 start += PAGE_CACHE_SIZE;
2578 }
2579
Chris Masonffbd5172009-04-20 15:50:09 -04002580 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002581 return ret;
2582}
Chris Masond1310b22008-01-24 16:13:08 -05002583
2584int extent_writepages(struct extent_io_tree *tree,
2585 struct address_space *mapping,
2586 get_extent_t *get_extent,
2587 struct writeback_control *wbc)
2588{
2589 int ret = 0;
2590 struct extent_page_data epd = {
2591 .bio = NULL,
2592 .tree = tree,
2593 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002594 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002595 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002596 };
2597
Chris Mason4bef0842008-09-08 11:18:08 -04002598 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002599 __extent_writepage, &epd,
2600 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002601 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002602 return ret;
2603}
Chris Masond1310b22008-01-24 16:13:08 -05002604
2605int extent_readpages(struct extent_io_tree *tree,
2606 struct address_space *mapping,
2607 struct list_head *pages, unsigned nr_pages,
2608 get_extent_t get_extent)
2609{
2610 struct bio *bio = NULL;
2611 unsigned page_idx;
2612 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002613 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002614
2615 pagevec_init(&pvec, 0);
2616 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2617 struct page *page = list_entry(pages->prev, struct page, lru);
2618
2619 prefetchw(&page->flags);
2620 list_del(&page->lru);
2621 /*
2622 * what we want to do here is call add_to_page_cache_lru,
2623 * but that isn't exported, so we reproduce it here
2624 */
2625 if (!add_to_page_cache(page, mapping,
2626 page->index, GFP_KERNEL)) {
2627
2628 /* open coding of lru_cache_add, also not exported */
2629 page_cache_get(page);
2630 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002631 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002632 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002633 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002634 }
2635 page_cache_release(page);
2636 }
2637 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002638 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002639 BUG_ON(!list_empty(pages));
2640 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002641 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002642 return 0;
2643}
Chris Masond1310b22008-01-24 16:13:08 -05002644
2645/*
2646 * basic invalidatepage code, this waits on any locked or writeback
2647 * ranges corresponding to the page, and then deletes any extent state
2648 * records from the tree
2649 */
2650int extent_invalidatepage(struct extent_io_tree *tree,
2651 struct page *page, unsigned long offset)
2652{
2653 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2654 u64 end = start + PAGE_CACHE_SIZE - 1;
2655 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2656
Chris Masond3977122009-01-05 21:25:51 -05002657 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002658 if (start > end)
2659 return 0;
2660
2661 lock_extent(tree, start, end, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002662 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002663 clear_extent_bit(tree, start, end,
2664 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Mason2c64c532009-09-02 15:04:12 -04002665 1, 1, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002666 return 0;
2667}
Chris Masond1310b22008-01-24 16:13:08 -05002668
2669/*
2670 * simple commit_write call, set_range_dirty is used to mark both
2671 * the pages and the extent records as dirty
2672 */
2673int extent_commit_write(struct extent_io_tree *tree,
2674 struct inode *inode, struct page *page,
2675 unsigned from, unsigned to)
2676{
2677 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2678
2679 set_page_extent_mapped(page);
2680 set_page_dirty(page);
2681
2682 if (pos > inode->i_size) {
2683 i_size_write(inode, pos);
2684 mark_inode_dirty(inode);
2685 }
2686 return 0;
2687}
Chris Masond1310b22008-01-24 16:13:08 -05002688
2689int extent_prepare_write(struct extent_io_tree *tree,
2690 struct inode *inode, struct page *page,
2691 unsigned from, unsigned to, get_extent_t *get_extent)
2692{
2693 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2694 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2695 u64 block_start;
2696 u64 orig_block_start;
2697 u64 block_end;
2698 u64 cur_end;
2699 struct extent_map *em;
2700 unsigned blocksize = 1 << inode->i_blkbits;
2701 size_t page_offset = 0;
2702 size_t block_off_start;
2703 size_t block_off_end;
2704 int err = 0;
2705 int iocount = 0;
2706 int ret = 0;
2707 int isnew;
2708
2709 set_page_extent_mapped(page);
2710
2711 block_start = (page_start + from) & ~((u64)blocksize - 1);
2712 block_end = (page_start + to - 1) | (blocksize - 1);
2713 orig_block_start = block_start;
2714
2715 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002716 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002717 em = get_extent(inode, page, page_offset, block_start,
2718 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002719 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002720 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002721
Chris Masond1310b22008-01-24 16:13:08 -05002722 cur_end = min(block_end, extent_map_end(em) - 1);
2723 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2724 block_off_end = block_off_start + blocksize;
2725 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2726
2727 if (!PageUptodate(page) && isnew &&
2728 (block_off_end > to || block_off_start < from)) {
2729 void *kaddr;
2730
2731 kaddr = kmap_atomic(page, KM_USER0);
2732 if (block_off_end > to)
2733 memset(kaddr + to, 0, block_off_end - to);
2734 if (block_off_start < from)
2735 memset(kaddr + block_off_start, 0,
2736 from - block_off_start);
2737 flush_dcache_page(page);
2738 kunmap_atomic(kaddr, KM_USER0);
2739 }
2740 if ((em->block_start != EXTENT_MAP_HOLE &&
2741 em->block_start != EXTENT_MAP_INLINE) &&
2742 !isnew && !PageUptodate(page) &&
2743 (block_off_end > to || block_off_start < from) &&
2744 !test_range_bit(tree, block_start, cur_end,
2745 EXTENT_UPTODATE, 1)) {
2746 u64 sector;
2747 u64 extent_offset = block_start - em->start;
2748 size_t iosize;
2749 sector = (em->block_start + extent_offset) >> 9;
2750 iosize = (cur_end - block_start + blocksize) &
2751 ~((u64)blocksize - 1);
2752 /*
2753 * we've already got the extent locked, but we
2754 * need to split the state such that our end_bio
2755 * handler can clear the lock.
2756 */
2757 set_extent_bit(tree, block_start,
2758 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002759 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002760 ret = submit_extent_page(READ, tree, page,
2761 sector, iosize, page_offset, em->bdev,
2762 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002763 end_bio_extent_preparewrite, 0,
2764 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002765 iocount++;
2766 block_start = block_start + iosize;
2767 } else {
2768 set_extent_uptodate(tree, block_start, cur_end,
2769 GFP_NOFS);
2770 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2771 block_start = cur_end + 1;
2772 }
2773 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2774 free_extent_map(em);
2775 }
2776 if (iocount) {
2777 wait_extent_bit(tree, orig_block_start,
2778 block_end, EXTENT_LOCKED);
2779 }
2780 check_page_uptodate(tree, page);
2781err:
2782 /* FIXME, zero out newly allocated blocks on error */
2783 return err;
2784}
Chris Masond1310b22008-01-24 16:13:08 -05002785
2786/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002787 * a helper for releasepage, this tests for areas of the page that
2788 * are locked or under IO and drops the related state bits if it is safe
2789 * to drop the page.
2790 */
2791int try_release_extent_state(struct extent_map_tree *map,
2792 struct extent_io_tree *tree, struct page *page,
2793 gfp_t mask)
2794{
2795 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2796 u64 end = start + PAGE_CACHE_SIZE - 1;
2797 int ret = 1;
2798
Chris Mason211f90e2008-07-18 11:56:15 -04002799 if (test_range_bit(tree, start, end,
2800 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002801 ret = 0;
2802 else {
2803 if ((mask & GFP_NOFS) == GFP_NOFS)
2804 mask = GFP_NOFS;
2805 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
Chris Mason2c64c532009-09-02 15:04:12 -04002806 1, 1, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002807 }
2808 return ret;
2809}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002810
2811/*
Chris Masond1310b22008-01-24 16:13:08 -05002812 * a helper for releasepage. As long as there are no locked extents
2813 * in the range corresponding to the page, both state records and extent
2814 * map records are removed
2815 */
2816int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002817 struct extent_io_tree *tree, struct page *page,
2818 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002819{
2820 struct extent_map *em;
2821 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2822 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002823
Chris Mason70dec802008-01-29 09:59:12 -05002824 if ((mask & __GFP_WAIT) &&
2825 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002826 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002827 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002828 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002829 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002830 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002831 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002832 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002833 break;
2834 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002835 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2836 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002837 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002838 free_extent_map(em);
2839 break;
2840 }
2841 if (!test_range_bit(tree, em->start,
2842 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002843 EXTENT_LOCKED | EXTENT_WRITEBACK |
2844 EXTENT_ORDERED,
2845 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002846 remove_extent_mapping(map, em);
2847 /* once for the rb tree */
2848 free_extent_map(em);
2849 }
2850 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002851 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002852
2853 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002854 free_extent_map(em);
2855 }
Chris Masond1310b22008-01-24 16:13:08 -05002856 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002857 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002858}
Chris Masond1310b22008-01-24 16:13:08 -05002859
2860sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2861 get_extent_t *get_extent)
2862{
2863 struct inode *inode = mapping->host;
2864 u64 start = iblock << inode->i_blkbits;
2865 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002866 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002867 struct extent_map *em;
2868
Yan Zhengd899e052008-10-30 14:25:28 -04002869 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2870 GFP_NOFS);
2871 em = get_extent(inode, NULL, 0, start, blksize, 0);
2872 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2873 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002874 if (!em || IS_ERR(em))
2875 return 0;
2876
Yan Zhengd899e052008-10-30 14:25:28 -04002877 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002878 goto out;
2879
2880 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002881out:
2882 free_extent_map(em);
2883 return sector;
2884}
2885
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002886int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2887 __u64 start, __u64 len, get_extent_t *get_extent)
2888{
2889 int ret;
2890 u64 off = start;
2891 u64 max = start + len;
2892 u32 flags = 0;
2893 u64 disko = 0;
2894 struct extent_map *em = NULL;
2895 int end = 0;
2896 u64 em_start = 0, em_len = 0;
2897 unsigned long emflags;
2898 ret = 0;
2899
2900 if (len == 0)
2901 return -EINVAL;
2902
2903 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2904 GFP_NOFS);
2905 em = get_extent(inode, NULL, 0, off, max - off, 0);
2906 if (!em)
2907 goto out;
2908 if (IS_ERR(em)) {
2909 ret = PTR_ERR(em);
2910 goto out;
2911 }
2912 while (!end) {
2913 off = em->start + em->len;
2914 if (off >= max)
2915 end = 1;
2916
2917 em_start = em->start;
2918 em_len = em->len;
2919
2920 disko = 0;
2921 flags = 0;
2922
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002923 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002924 end = 1;
2925 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002926 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002927 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002928 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002929 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2930 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002931 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002932 flags |= (FIEMAP_EXTENT_DELALLOC |
2933 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002934 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002935 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002936 }
2937 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2938 flags |= FIEMAP_EXTENT_ENCODED;
2939
2940 emflags = em->flags;
2941 free_extent_map(em);
2942 em = NULL;
2943
2944 if (!end) {
2945 em = get_extent(inode, NULL, 0, off, max - off, 0);
2946 if (!em)
2947 goto out;
2948 if (IS_ERR(em)) {
2949 ret = PTR_ERR(em);
2950 goto out;
2951 }
2952 emflags = em->flags;
2953 }
2954 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2955 flags |= FIEMAP_EXTENT_LAST;
2956 end = 1;
2957 }
2958
2959 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2960 em_len, flags);
2961 if (ret)
2962 goto out_free;
2963 }
2964out_free:
2965 free_extent_map(em);
2966out:
2967 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2968 GFP_NOFS);
2969 return ret;
2970}
2971
Chris Masond1310b22008-01-24 16:13:08 -05002972static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2973 unsigned long i)
2974{
2975 struct page *p;
2976 struct address_space *mapping;
2977
2978 if (i == 0)
2979 return eb->first_page;
2980 i += eb->start >> PAGE_CACHE_SHIFT;
2981 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002982 if (!mapping)
2983 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002984
2985 /*
2986 * extent_buffer_page is only called after pinning the page
2987 * by increasing the reference count. So we know the page must
2988 * be in the radix tree.
2989 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002990 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002991 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002992 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002993
Chris Masond1310b22008-01-24 16:13:08 -05002994 return p;
2995}
2996
Chris Mason6af118c2008-07-22 11:18:07 -04002997static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002998{
Chris Mason6af118c2008-07-22 11:18:07 -04002999 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3000 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003001}
3002
Chris Masond1310b22008-01-24 16:13:08 -05003003static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3004 u64 start,
3005 unsigned long len,
3006 gfp_t mask)
3007{
3008 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003009#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003010 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003011#endif
Chris Masond1310b22008-01-24 16:13:08 -05003012
Chris Masond1310b22008-01-24 16:13:08 -05003013 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003014 eb->start = start;
3015 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003016 spin_lock_init(&eb->lock);
3017 init_waitqueue_head(&eb->lock_wq);
3018
Chris Mason39351272009-02-04 09:24:05 -05003019#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003020 spin_lock_irqsave(&leak_lock, flags);
3021 list_add(&eb->leak_list, &buffers);
3022 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003023#endif
Chris Masond1310b22008-01-24 16:13:08 -05003024 atomic_set(&eb->refs, 1);
3025
3026 return eb;
3027}
3028
3029static void __free_extent_buffer(struct extent_buffer *eb)
3030{
Chris Mason39351272009-02-04 09:24:05 -05003031#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003032 unsigned long flags;
3033 spin_lock_irqsave(&leak_lock, flags);
3034 list_del(&eb->leak_list);
3035 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003036#endif
Chris Masond1310b22008-01-24 16:13:08 -05003037 kmem_cache_free(extent_buffer_cache, eb);
3038}
3039
3040struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3041 u64 start, unsigned long len,
3042 struct page *page0,
3043 gfp_t mask)
3044{
3045 unsigned long num_pages = num_extent_pages(start, len);
3046 unsigned long i;
3047 unsigned long index = start >> PAGE_CACHE_SHIFT;
3048 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003049 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003050 struct page *p;
3051 struct address_space *mapping = tree->mapping;
3052 int uptodate = 1;
3053
Chris Mason6af118c2008-07-22 11:18:07 -04003054 spin_lock(&tree->buffer_lock);
3055 eb = buffer_search(tree, start);
3056 if (eb) {
3057 atomic_inc(&eb->refs);
3058 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003059 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003060 return eb;
3061 }
3062 spin_unlock(&tree->buffer_lock);
3063
Chris Masond1310b22008-01-24 16:13:08 -05003064 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003065 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003066 return NULL;
3067
Chris Masond1310b22008-01-24 16:13:08 -05003068 if (page0) {
3069 eb->first_page = page0;
3070 i = 1;
3071 index++;
3072 page_cache_get(page0);
3073 mark_page_accessed(page0);
3074 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003075 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003076 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003077 } else {
3078 i = 0;
3079 }
3080 for (; i < num_pages; i++, index++) {
3081 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3082 if (!p) {
3083 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003084 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003085 }
3086 set_page_extent_mapped(p);
3087 mark_page_accessed(p);
3088 if (i == 0) {
3089 eb->first_page = p;
3090 set_page_extent_head(p, len);
3091 } else {
3092 set_page_private(p, EXTENT_PAGE_PRIVATE);
3093 }
3094 if (!PageUptodate(p))
3095 uptodate = 0;
3096 unlock_page(p);
3097 }
3098 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003099 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003100
Chris Mason6af118c2008-07-22 11:18:07 -04003101 spin_lock(&tree->buffer_lock);
3102 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3103 if (exists) {
3104 /* add one reference for the caller */
3105 atomic_inc(&exists->refs);
3106 spin_unlock(&tree->buffer_lock);
3107 goto free_eb;
3108 }
3109 spin_unlock(&tree->buffer_lock);
3110
3111 /* add one reference for the tree */
3112 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003113 return eb;
3114
Chris Mason6af118c2008-07-22 11:18:07 -04003115free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003116 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003117 return exists;
3118 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003119 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118c2008-07-22 11:18:07 -04003120 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003121 __free_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003122 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003123}
Chris Masond1310b22008-01-24 16:13:08 -05003124
3125struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3126 u64 start, unsigned long len,
3127 gfp_t mask)
3128{
Chris Masond1310b22008-01-24 16:13:08 -05003129 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003130
Chris Mason6af118c2008-07-22 11:18:07 -04003131 spin_lock(&tree->buffer_lock);
3132 eb = buffer_search(tree, start);
3133 if (eb)
3134 atomic_inc(&eb->refs);
3135 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003136
Josef Bacik0f9dd462008-09-23 13:14:11 -04003137 if (eb)
3138 mark_page_accessed(eb->first_page);
3139
Chris Masond1310b22008-01-24 16:13:08 -05003140 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003141}
Chris Masond1310b22008-01-24 16:13:08 -05003142
3143void free_extent_buffer(struct extent_buffer *eb)
3144{
Chris Masond1310b22008-01-24 16:13:08 -05003145 if (!eb)
3146 return;
3147
3148 if (!atomic_dec_and_test(&eb->refs))
3149 return;
3150
Chris Mason6af118c2008-07-22 11:18:07 -04003151 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003152}
Chris Masond1310b22008-01-24 16:13:08 -05003153
3154int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3155 struct extent_buffer *eb)
3156{
Chris Masond1310b22008-01-24 16:13:08 -05003157 unsigned long i;
3158 unsigned long num_pages;
3159 struct page *page;
3160
Chris Masond1310b22008-01-24 16:13:08 -05003161 num_pages = num_extent_pages(eb->start, eb->len);
3162
3163 for (i = 0; i < num_pages; i++) {
3164 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003165 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003166 continue;
3167
Chris Masona61e6f22008-07-22 11:18:08 -04003168 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003169 if (i == 0)
3170 set_page_extent_head(page, eb->len);
3171 else
3172 set_page_private(page, EXTENT_PAGE_PRIVATE);
3173
Chris Masond1310b22008-01-24 16:13:08 -05003174 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003175 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003176 if (!PageDirty(page)) {
3177 radix_tree_tag_clear(&page->mapping->page_tree,
3178 page_index(page),
3179 PAGECACHE_TAG_DIRTY);
3180 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003181 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003182 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003183 }
3184 return 0;
3185}
Chris Masond1310b22008-01-24 16:13:08 -05003186
3187int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3188 struct extent_buffer *eb)
3189{
3190 return wait_on_extent_writeback(tree, eb->start,
3191 eb->start + eb->len - 1);
3192}
Chris Masond1310b22008-01-24 16:13:08 -05003193
3194int set_extent_buffer_dirty(struct extent_io_tree *tree,
3195 struct extent_buffer *eb)
3196{
3197 unsigned long i;
3198 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003199 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003200
Chris Masonb9473432009-03-13 11:00:37 -04003201 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003202 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003203 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003204 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003205 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003206}
Chris Masond1310b22008-01-24 16:13:08 -05003207
Chris Mason1259ab72008-05-12 13:39:03 -04003208int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3209 struct extent_buffer *eb)
3210{
3211 unsigned long i;
3212 struct page *page;
3213 unsigned long num_pages;
3214
3215 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003216 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003217
3218 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3219 GFP_NOFS);
3220 for (i = 0; i < num_pages; i++) {
3221 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003222 if (page)
3223 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003224 }
3225 return 0;
3226}
3227
Chris Masond1310b22008-01-24 16:13:08 -05003228int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3229 struct extent_buffer *eb)
3230{
3231 unsigned long i;
3232 struct page *page;
3233 unsigned long num_pages;
3234
3235 num_pages = num_extent_pages(eb->start, eb->len);
3236
3237 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3238 GFP_NOFS);
3239 for (i = 0; i < num_pages; i++) {
3240 page = extent_buffer_page(eb, i);
3241 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3242 ((i == num_pages - 1) &&
3243 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3244 check_page_uptodate(tree, page);
3245 continue;
3246 }
3247 SetPageUptodate(page);
3248 }
3249 return 0;
3250}
Chris Masond1310b22008-01-24 16:13:08 -05003251
Chris Masonce9adaa2008-04-09 16:28:12 -04003252int extent_range_uptodate(struct extent_io_tree *tree,
3253 u64 start, u64 end)
3254{
3255 struct page *page;
3256 int ret;
3257 int pg_uptodate = 1;
3258 int uptodate;
3259 unsigned long index;
3260
3261 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3262 if (ret)
3263 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003264 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003265 index = start >> PAGE_CACHE_SHIFT;
3266 page = find_get_page(tree->mapping, index);
3267 uptodate = PageUptodate(page);
3268 page_cache_release(page);
3269 if (!uptodate) {
3270 pg_uptodate = 0;
3271 break;
3272 }
3273 start += PAGE_CACHE_SIZE;
3274 }
3275 return pg_uptodate;
3276}
3277
Chris Masond1310b22008-01-24 16:13:08 -05003278int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003279 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003280{
Chris Mason728131d2008-04-09 16:28:12 -04003281 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003282 unsigned long num_pages;
3283 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003284 struct page *page;
3285 int pg_uptodate = 1;
3286
Chris Masonb4ce94d2009-02-04 09:25:08 -05003287 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003288 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003289
Chris Mason42352982008-04-28 16:40:52 -04003290 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003291 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003292 if (ret)
3293 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003294
3295 num_pages = num_extent_pages(eb->start, eb->len);
3296 for (i = 0; i < num_pages; i++) {
3297 page = extent_buffer_page(eb, i);
3298 if (!PageUptodate(page)) {
3299 pg_uptodate = 0;
3300 break;
3301 }
3302 }
Chris Mason42352982008-04-28 16:40:52 -04003303 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003304}
Chris Masond1310b22008-01-24 16:13:08 -05003305
3306int read_extent_buffer_pages(struct extent_io_tree *tree,
3307 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003308 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003309 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003310{
3311 unsigned long i;
3312 unsigned long start_i;
3313 struct page *page;
3314 int err;
3315 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003316 int locked_pages = 0;
3317 int all_uptodate = 1;
3318 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003319 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003320 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003321 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003322
Chris Masonb4ce94d2009-02-04 09:25:08 -05003323 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003324 return 0;
3325
Chris Masonce9adaa2008-04-09 16:28:12 -04003326 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003327 EXTENT_UPTODATE, 1)) {
3328 return 0;
3329 }
3330
3331 if (start) {
3332 WARN_ON(start < eb->start);
3333 start_i = (start >> PAGE_CACHE_SHIFT) -
3334 (eb->start >> PAGE_CACHE_SHIFT);
3335 } else {
3336 start_i = 0;
3337 }
3338
3339 num_pages = num_extent_pages(eb->start, eb->len);
3340 for (i = start_i; i < num_pages; i++) {
3341 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003342 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003343 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003344 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003345 } else {
3346 lock_page(page);
3347 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003348 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003349 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003350 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003351 }
3352 if (all_uptodate) {
3353 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003354 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003355 goto unlock_exit;
3356 }
3357
3358 for (i = start_i; i < num_pages; i++) {
3359 page = extent_buffer_page(eb, i);
3360 if (inc_all_pages)
3361 page_cache_get(page);
3362 if (!PageUptodate(page)) {
3363 if (start_i == 0)
3364 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003365 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003366 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003367 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003368 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003369 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003370 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003371 } else {
3372 unlock_page(page);
3373 }
3374 }
3375
Chris Masona86c12c2008-02-07 10:50:54 -05003376 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003377 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003378
Chris Masond3977122009-01-05 21:25:51 -05003379 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003380 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003381
Chris Masond1310b22008-01-24 16:13:08 -05003382 for (i = start_i; i < num_pages; i++) {
3383 page = extent_buffer_page(eb, i);
3384 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003385 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003386 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003387 }
Chris Masond3977122009-01-05 21:25:51 -05003388
Chris Masond1310b22008-01-24 16:13:08 -05003389 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003390 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003391 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003392
3393unlock_exit:
3394 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003395 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003396 page = extent_buffer_page(eb, i);
3397 i++;
3398 unlock_page(page);
3399 locked_pages--;
3400 }
3401 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003402}
Chris Masond1310b22008-01-24 16:13:08 -05003403
3404void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3405 unsigned long start,
3406 unsigned long len)
3407{
3408 size_t cur;
3409 size_t offset;
3410 struct page *page;
3411 char *kaddr;
3412 char *dst = (char *)dstv;
3413 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3414 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003415
3416 WARN_ON(start > eb->len);
3417 WARN_ON(start + len > eb->start + eb->len);
3418
3419 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3420
Chris Masond3977122009-01-05 21:25:51 -05003421 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003422 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003423
3424 cur = min(len, (PAGE_CACHE_SIZE - offset));
3425 kaddr = kmap_atomic(page, KM_USER1);
3426 memcpy(dst, kaddr + offset, cur);
3427 kunmap_atomic(kaddr, KM_USER1);
3428
3429 dst += cur;
3430 len -= cur;
3431 offset = 0;
3432 i++;
3433 }
3434}
Chris Masond1310b22008-01-24 16:13:08 -05003435
3436int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3437 unsigned long min_len, char **token, char **map,
3438 unsigned long *map_start,
3439 unsigned long *map_len, int km)
3440{
3441 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3442 char *kaddr;
3443 struct page *p;
3444 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3445 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3446 unsigned long end_i = (start_offset + start + min_len - 1) >>
3447 PAGE_CACHE_SHIFT;
3448
3449 if (i != end_i)
3450 return -EINVAL;
3451
3452 if (i == 0) {
3453 offset = start_offset;
3454 *map_start = 0;
3455 } else {
3456 offset = 0;
3457 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3458 }
Chris Masond3977122009-01-05 21:25:51 -05003459
Chris Masond1310b22008-01-24 16:13:08 -05003460 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003461 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3462 "wanted %lu %lu\n", (unsigned long long)eb->start,
3463 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003464 WARN_ON(1);
3465 }
3466
3467 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003468 kaddr = kmap_atomic(p, km);
3469 *token = kaddr;
3470 *map = kaddr + offset;
3471 *map_len = PAGE_CACHE_SIZE - offset;
3472 return 0;
3473}
Chris Masond1310b22008-01-24 16:13:08 -05003474
3475int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3476 unsigned long min_len,
3477 char **token, char **map,
3478 unsigned long *map_start,
3479 unsigned long *map_len, int km)
3480{
3481 int err;
3482 int save = 0;
3483 if (eb->map_token) {
3484 unmap_extent_buffer(eb, eb->map_token, km);
3485 eb->map_token = NULL;
3486 save = 1;
3487 }
3488 err = map_private_extent_buffer(eb, start, min_len, token, map,
3489 map_start, map_len, km);
3490 if (!err && save) {
3491 eb->map_token = *token;
3492 eb->kaddr = *map;
3493 eb->map_start = *map_start;
3494 eb->map_len = *map_len;
3495 }
3496 return err;
3497}
Chris Masond1310b22008-01-24 16:13:08 -05003498
3499void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3500{
3501 kunmap_atomic(token, km);
3502}
Chris Masond1310b22008-01-24 16:13:08 -05003503
3504int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3505 unsigned long start,
3506 unsigned long len)
3507{
3508 size_t cur;
3509 size_t offset;
3510 struct page *page;
3511 char *kaddr;
3512 char *ptr = (char *)ptrv;
3513 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3514 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3515 int ret = 0;
3516
3517 WARN_ON(start > eb->len);
3518 WARN_ON(start + len > eb->start + eb->len);
3519
3520 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3521
Chris Masond3977122009-01-05 21:25:51 -05003522 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003523 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003524
3525 cur = min(len, (PAGE_CACHE_SIZE - offset));
3526
3527 kaddr = kmap_atomic(page, KM_USER0);
3528 ret = memcmp(ptr, kaddr + offset, cur);
3529 kunmap_atomic(kaddr, KM_USER0);
3530 if (ret)
3531 break;
3532
3533 ptr += cur;
3534 len -= cur;
3535 offset = 0;
3536 i++;
3537 }
3538 return ret;
3539}
Chris Masond1310b22008-01-24 16:13:08 -05003540
3541void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3542 unsigned long start, unsigned long len)
3543{
3544 size_t cur;
3545 size_t offset;
3546 struct page *page;
3547 char *kaddr;
3548 char *src = (char *)srcv;
3549 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3550 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3551
3552 WARN_ON(start > eb->len);
3553 WARN_ON(start + len > eb->start + eb->len);
3554
3555 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3556
Chris Masond3977122009-01-05 21:25:51 -05003557 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003558 page = extent_buffer_page(eb, i);
3559 WARN_ON(!PageUptodate(page));
3560
3561 cur = min(len, PAGE_CACHE_SIZE - offset);
3562 kaddr = kmap_atomic(page, KM_USER1);
3563 memcpy(kaddr + offset, src, cur);
3564 kunmap_atomic(kaddr, KM_USER1);
3565
3566 src += cur;
3567 len -= cur;
3568 offset = 0;
3569 i++;
3570 }
3571}
Chris Masond1310b22008-01-24 16:13:08 -05003572
3573void memset_extent_buffer(struct extent_buffer *eb, char c,
3574 unsigned long start, unsigned long len)
3575{
3576 size_t cur;
3577 size_t offset;
3578 struct page *page;
3579 char *kaddr;
3580 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3581 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3582
3583 WARN_ON(start > eb->len);
3584 WARN_ON(start + len > eb->start + eb->len);
3585
3586 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3587
Chris Masond3977122009-01-05 21:25:51 -05003588 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003589 page = extent_buffer_page(eb, i);
3590 WARN_ON(!PageUptodate(page));
3591
3592 cur = min(len, PAGE_CACHE_SIZE - offset);
3593 kaddr = kmap_atomic(page, KM_USER0);
3594 memset(kaddr + offset, c, cur);
3595 kunmap_atomic(kaddr, KM_USER0);
3596
3597 len -= cur;
3598 offset = 0;
3599 i++;
3600 }
3601}
Chris Masond1310b22008-01-24 16:13:08 -05003602
3603void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3604 unsigned long dst_offset, unsigned long src_offset,
3605 unsigned long len)
3606{
3607 u64 dst_len = dst->len;
3608 size_t cur;
3609 size_t offset;
3610 struct page *page;
3611 char *kaddr;
3612 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3613 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3614
3615 WARN_ON(src->len != dst_len);
3616
3617 offset = (start_offset + dst_offset) &
3618 ((unsigned long)PAGE_CACHE_SIZE - 1);
3619
Chris Masond3977122009-01-05 21:25:51 -05003620 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003621 page = extent_buffer_page(dst, i);
3622 WARN_ON(!PageUptodate(page));
3623
3624 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3625
3626 kaddr = kmap_atomic(page, KM_USER0);
3627 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3628 kunmap_atomic(kaddr, KM_USER0);
3629
3630 src_offset += cur;
3631 len -= cur;
3632 offset = 0;
3633 i++;
3634 }
3635}
Chris Masond1310b22008-01-24 16:13:08 -05003636
3637static void move_pages(struct page *dst_page, struct page *src_page,
3638 unsigned long dst_off, unsigned long src_off,
3639 unsigned long len)
3640{
3641 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3642 if (dst_page == src_page) {
3643 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3644 } else {
3645 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3646 char *p = dst_kaddr + dst_off + len;
3647 char *s = src_kaddr + src_off + len;
3648
3649 while (len--)
3650 *--p = *--s;
3651
3652 kunmap_atomic(src_kaddr, KM_USER1);
3653 }
3654 kunmap_atomic(dst_kaddr, KM_USER0);
3655}
3656
3657static void copy_pages(struct page *dst_page, struct page *src_page,
3658 unsigned long dst_off, unsigned long src_off,
3659 unsigned long len)
3660{
3661 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3662 char *src_kaddr;
3663
3664 if (dst_page != src_page)
3665 src_kaddr = kmap_atomic(src_page, KM_USER1);
3666 else
3667 src_kaddr = dst_kaddr;
3668
3669 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3670 kunmap_atomic(dst_kaddr, KM_USER0);
3671 if (dst_page != src_page)
3672 kunmap_atomic(src_kaddr, KM_USER1);
3673}
3674
3675void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3676 unsigned long src_offset, unsigned long len)
3677{
3678 size_t cur;
3679 size_t dst_off_in_page;
3680 size_t src_off_in_page;
3681 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3682 unsigned long dst_i;
3683 unsigned long src_i;
3684
3685 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003686 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3687 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003688 BUG_ON(1);
3689 }
3690 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003691 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3692 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003693 BUG_ON(1);
3694 }
3695
Chris Masond3977122009-01-05 21:25:51 -05003696 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003697 dst_off_in_page = (start_offset + dst_offset) &
3698 ((unsigned long)PAGE_CACHE_SIZE - 1);
3699 src_off_in_page = (start_offset + src_offset) &
3700 ((unsigned long)PAGE_CACHE_SIZE - 1);
3701
3702 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3703 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3704
3705 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3706 src_off_in_page));
3707 cur = min_t(unsigned long, cur,
3708 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3709
3710 copy_pages(extent_buffer_page(dst, dst_i),
3711 extent_buffer_page(dst, src_i),
3712 dst_off_in_page, src_off_in_page, cur);
3713
3714 src_offset += cur;
3715 dst_offset += cur;
3716 len -= cur;
3717 }
3718}
Chris Masond1310b22008-01-24 16:13:08 -05003719
3720void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3721 unsigned long src_offset, unsigned long len)
3722{
3723 size_t cur;
3724 size_t dst_off_in_page;
3725 size_t src_off_in_page;
3726 unsigned long dst_end = dst_offset + len - 1;
3727 unsigned long src_end = src_offset + len - 1;
3728 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3729 unsigned long dst_i;
3730 unsigned long src_i;
3731
3732 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003733 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3734 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003735 BUG_ON(1);
3736 }
3737 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003738 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3739 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003740 BUG_ON(1);
3741 }
3742 if (dst_offset < src_offset) {
3743 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3744 return;
3745 }
Chris Masond3977122009-01-05 21:25:51 -05003746 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003747 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3748 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3749
3750 dst_off_in_page = (start_offset + dst_end) &
3751 ((unsigned long)PAGE_CACHE_SIZE - 1);
3752 src_off_in_page = (start_offset + src_end) &
3753 ((unsigned long)PAGE_CACHE_SIZE - 1);
3754
3755 cur = min_t(unsigned long, len, src_off_in_page + 1);
3756 cur = min(cur, dst_off_in_page + 1);
3757 move_pages(extent_buffer_page(dst, dst_i),
3758 extent_buffer_page(dst, src_i),
3759 dst_off_in_page - cur + 1,
3760 src_off_in_page - cur + 1, cur);
3761
3762 dst_end -= cur;
3763 src_end -= cur;
3764 len -= cur;
3765 }
3766}
Chris Mason6af118c2008-07-22 11:18:07 -04003767
3768int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3769{
3770 u64 start = page_offset(page);
3771 struct extent_buffer *eb;
3772 int ret = 1;
3773 unsigned long i;
3774 unsigned long num_pages;
3775
3776 spin_lock(&tree->buffer_lock);
3777 eb = buffer_search(tree, start);
3778 if (!eb)
3779 goto out;
3780
3781 if (atomic_read(&eb->refs) > 1) {
3782 ret = 0;
3783 goto out;
3784 }
Chris Masonb9473432009-03-13 11:00:37 -04003785 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3786 ret = 0;
3787 goto out;
3788 }
Chris Mason6af118c2008-07-22 11:18:07 -04003789 /* at this point we can safely release the extent buffer */
3790 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003791 for (i = 0; i < num_pages; i++)
3792 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118c2008-07-22 11:18:07 -04003793 rb_erase(&eb->rb_node, &tree->buffer);
3794 __free_extent_buffer(eb);
3795out:
3796 spin_unlock(&tree->buffer_lock);
3797 return ret;
3798}