blob: 0cb88f8146ea85efa5bc944c9d95995a3d0ad772 [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 Mason6af118ce2008-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 Mason6af118ce2008-07-22 11:18:07 -0400112 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500113 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500114}
Chris Masond1310b22008-01-24 16:13:08 -0500115
Christoph Hellwigb2950862008-12-02 09:54:17 -0500116static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500117{
118 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500119#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400120 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400121#endif
Chris Masond1310b22008-01-24 16:13:08 -0500122
123 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400124 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500125 return state;
126 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500127 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500128 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500129#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400133#endif
Chris Masond1310b22008-01-24 16:13:08 -0500134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
136 return state;
137}
Chris Masond1310b22008-01-24 16:13:08 -0500138
Christoph Hellwigb2950862008-12-02 09:54:17 -0500139static void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500140{
Chris Masond1310b22008-01-24 16:13:08 -0500141 if (!state)
142 return;
143 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500144#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400145 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400146#endif
Chris Mason70dec802008-01-29 09:59:12 -0500147 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500148#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Masond1310b22008-01-24 16:13:08 -0500153 kmem_cache_free(extent_state_cache, state);
154 }
155}
Chris Masond1310b22008-01-24 16:13:08 -0500156
157static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
159{
Chris Masond3977122009-01-05 21:25:51 -0500160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500162 struct tree_entry *entry;
163
Chris Masond3977122009-01-05 21:25:51 -0500164 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500165 parent = *p;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
167
168 if (offset < entry->start)
169 p = &(*p)->rb_left;
170 else if (offset > entry->end)
171 p = &(*p)->rb_right;
172 else
173 return parent;
174 }
175
176 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
179 return NULL;
180}
181
Chris Mason80ea96b2008-02-01 14:51:59 -0500182static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
185{
Chris Mason80ea96b2008-02-01 14:51:59 -0500186 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500187 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
192
Chris Masond3977122009-01-05 21:25:51 -0500193 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500194 entry = rb_entry(n, struct tree_entry, rb_node);
195 prev = n;
196 prev_entry = entry;
197
198 if (offset < entry->start)
199 n = n->rb_left;
200 else if (offset > entry->end)
201 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500202 else
Chris Masond1310b22008-01-24 16:13:08 -0500203 return n;
204 }
205
206 if (prev_ret) {
207 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500208 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
211 }
212 *prev_ret = prev;
213 prev = orig_prev;
214 }
215
216 if (next_ret) {
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500218 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
221 }
222 *next_ret = prev;
223 }
224 return NULL;
225}
226
Chris Mason80ea96b2008-02-01 14:51:59 -0500227static inline struct rb_node *tree_search(struct extent_io_tree *tree,
228 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500229{
Chris Mason70dec802008-01-29 09:59:12 -0500230 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500231 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500232
Chris Mason80ea96b2008-02-01 14:51:59 -0500233 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500234 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500235 return prev;
236 return ret;
237}
238
Chris Mason6af118ce2008-07-22 11:18:07 -0400239static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
240 u64 offset, struct rb_node *node)
241{
242 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500243 struct rb_node **p = &root->rb_node;
244 struct rb_node *parent = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400245 struct extent_buffer *eb;
246
Chris Masond3977122009-01-05 21:25:51 -0500247 while (*p) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400248 parent = *p;
249 eb = rb_entry(parent, struct extent_buffer, rb_node);
250
251 if (offset < eb->start)
252 p = &(*p)->rb_left;
253 else if (offset > eb->start)
254 p = &(*p)->rb_right;
255 else
256 return eb;
257 }
258
259 rb_link_node(node, parent, p);
260 rb_insert_color(node, root);
261 return NULL;
262}
263
264static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
265 u64 offset)
266{
267 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500268 struct rb_node *n = root->rb_node;
Chris Mason6af118ce2008-07-22 11:18:07 -0400269 struct extent_buffer *eb;
270
Chris Masond3977122009-01-05 21:25:51 -0500271 while (n) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400272 eb = rb_entry(n, struct extent_buffer, rb_node);
273 if (offset < eb->start)
274 n = n->rb_left;
275 else if (offset > eb->start)
276 n = n->rb_right;
277 else
278 return eb;
279 }
280 return NULL;
281}
282
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;
Chris Mason42daec22009-09-23 19:51:09 -0400498 cached_state = NULL;
499 if (cached && cached->tree && cached->start == start) {
Chris Mason2c64c532009-09-02 15:04:12 -0400500 atomic_dec(&cached->refs);
501 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400502 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400503 }
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 }
551 goto search_again;
552 }
553 /*
554 * | ---- desired range ---- |
555 * | state |
556 * We need to split the extent, and clear the bit
557 * on the first half
558 */
559 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500560 if (!prealloc)
561 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500562 err = split_state(tree, state, prealloc, end + 1);
563 BUG_ON(err == -EEXIST);
564
565 if (wake)
566 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400567
Chris Masond1310b22008-01-24 16:13:08 -0500568 set |= clear_state_bit(tree, prealloc, bits,
569 wake, delete);
570 prealloc = NULL;
571 goto out;
572 }
Chris Mason42daec22009-09-23 19:51:09 -0400573
Chris Mason2c64c532009-09-02 15:04:12 -0400574 if (state->end < end && prealloc && !need_resched())
575 next_node = rb_next(&state->rb_node);
576 else
577 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400578
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;
Chris Mason42daec22009-09-23 19:51:09 -0400715
Chris Masond1310b22008-01-24 16:13:08 -0500716again:
717 if (!prealloc && (mask & __GFP_WAIT)) {
718 prealloc = alloc_extent_state(mask);
719 if (!prealloc)
720 return -ENOMEM;
721 }
722
Chris Masoncad321a2008-12-17 14:51:42 -0500723 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400724 if (cached_state && *cached_state) {
725 state = *cached_state;
726 if (state->start == start && state->tree) {
727 node = &state->rb_node;
728 goto hit_next;
729 }
730 }
Chris Masond1310b22008-01-24 16:13:08 -0500731 /*
732 * this search will find all the extents that end after
733 * our range starts.
734 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500735 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500736 if (!node) {
737 err = insert_state(tree, prealloc, start, end, bits);
738 prealloc = NULL;
739 BUG_ON(err == -EEXIST);
740 goto out;
741 }
Chris Masond1310b22008-01-24 16:13:08 -0500742 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400743hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500744 last_start = state->start;
745 last_end = state->end;
746
747 /*
748 * | ---- desired range ---- |
749 * | state |
750 *
751 * Just lock what we found and keep going
752 */
753 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400754 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400755 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500756 *failed_start = state->start;
757 err = -EEXIST;
758 goto out;
759 }
Chris Mason42daec22009-09-23 19:51:09 -0400760
Chris Masond1310b22008-01-24 16:13:08 -0500761 set_state_bits(tree, state, bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400762 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500763 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400764 if (last_end == (u64)-1)
765 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400766
Yan Zheng5c939df2009-05-27 09:16:03 -0400767 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400768 if (start < end && prealloc && !need_resched()) {
769 next_node = rb_next(node);
770 if (next_node) {
771 state = rb_entry(next_node, struct extent_state,
772 rb_node);
773 if (state->start == start)
774 goto hit_next;
775 }
776 }
Chris Masond1310b22008-01-24 16:13:08 -0500777 goto search_again;
778 }
779
780 /*
781 * | ---- desired range ---- |
782 * | state |
783 * or
784 * | ------------- state -------------- |
785 *
786 * We need to split the extent we found, and may flip bits on
787 * second half.
788 *
789 * If the extent we found extends past our
790 * range, we just split and search again. It'll get split
791 * again the next time though.
792 *
793 * If the extent we found is inside our range, we set the
794 * desired bit on it.
795 */
796 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400797 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500798 *failed_start = start;
799 err = -EEXIST;
800 goto out;
801 }
802 err = split_state(tree, state, prealloc, start);
803 BUG_ON(err == -EEXIST);
804 prealloc = NULL;
805 if (err)
806 goto out;
807 if (state->end <= end) {
808 set_state_bits(tree, state, bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400809 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500810 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400811 if (last_end == (u64)-1)
812 goto out;
813 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500814 }
815 goto search_again;
816 }
817 /*
818 * | ---- desired range ---- |
819 * | state | or | state |
820 *
821 * There's a hole, we need to insert something in it and
822 * ignore the extent we found.
823 */
824 if (state->start > start) {
825 u64 this_end;
826 if (end < last_start)
827 this_end = end;
828 else
Chris Masond3977122009-01-05 21:25:51 -0500829 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500830 err = insert_state(tree, prealloc, start, this_end,
831 bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400832 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500833 prealloc = NULL;
834 BUG_ON(err == -EEXIST);
835 if (err)
836 goto out;
837 start = this_end + 1;
838 goto search_again;
839 }
840 /*
841 * | ---- desired range ---- |
842 * | state |
843 * We need to split the extent, and set the bit
844 * on the first half
845 */
846 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400847 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500848 *failed_start = start;
849 err = -EEXIST;
850 goto out;
851 }
852 err = split_state(tree, state, prealloc, end + 1);
853 BUG_ON(err == -EEXIST);
854
855 set_state_bits(tree, prealloc, bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400856 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500857 merge_state(tree, prealloc);
858 prealloc = NULL;
859 goto out;
860 }
861
862 goto search_again;
863
864out:
Chris Masoncad321a2008-12-17 14:51:42 -0500865 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500866 if (prealloc)
867 free_extent_state(prealloc);
868
869 return err;
870
871search_again:
872 if (start > end)
873 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500874 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500875 if (mask & __GFP_WAIT)
876 cond_resched();
877 goto again;
878}
Chris Masond1310b22008-01-24 16:13:08 -0500879
880/* wrappers around set/clear extent bit */
881int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
882 gfp_t mask)
883{
884 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400885 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500886}
Chris Masond1310b22008-01-24 16:13:08 -0500887
888int 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
917int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
918 gfp_t mask)
919{
920 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400921 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500922}
Chris Masond1310b22008-01-24 16:13:08 -0500923
Christoph Hellwigb2950862008-12-02 09:54:17 -0500924static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500925 gfp_t mask)
926{
Chris Mason2c64c532009-09-02 15:04:12 -0400927 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
928 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500929}
Chris Masond1310b22008-01-24 16:13:08 -0500930
931int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
932 gfp_t mask)
933{
934 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400935 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500936}
Chris Masond1310b22008-01-24 16:13:08 -0500937
Chris Masond3977122009-01-05 21:25:51 -0500938static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
939 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500940{
Chris Mason2c64c532009-09-02 15:04:12 -0400941 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
942 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500943}
Chris Masond1310b22008-01-24 16:13:08 -0500944
Chris Masond1310b22008-01-24 16:13:08 -0500945int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
946{
947 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
948}
Chris Masond1310b22008-01-24 16:13:08 -0500949
Chris Masond352ac62008-09-29 15:18:18 -0400950/*
951 * either insert or lock state struct between start and end use mask to tell
952 * us if waiting is desired.
953 */
Chris Mason1edbb732009-09-02 13:24:36 -0400954int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400955 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500956{
957 int err;
958 u64 failed_start;
959 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400960 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400961 EXTENT_LOCKED, &failed_start,
962 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500963 if (err == -EEXIST && (mask & __GFP_WAIT)) {
964 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
965 start = failed_start;
966 } else {
967 break;
968 }
969 WARN_ON(start > end);
970 }
971 return err;
972}
Chris Masond1310b22008-01-24 16:13:08 -0500973
Chris Mason1edbb732009-09-02 13:24:36 -0400974int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
975{
Chris Mason2c64c532009-09-02 15:04:12 -0400976 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400977}
978
Josef Bacik25179202008-10-29 14:49:05 -0400979int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
980 gfp_t mask)
981{
982 int err;
983 u64 failed_start;
984
Chris Mason2c64c532009-09-02 15:04:12 -0400985 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
986 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400987 if (err == -EEXIST) {
988 if (failed_start > start)
989 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -0400990 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400991 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400992 }
Josef Bacik25179202008-10-29 14:49:05 -0400993 return 1;
994}
Josef Bacik25179202008-10-29 14:49:05 -0400995
Chris Mason2c64c532009-09-02 15:04:12 -0400996int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
997 struct extent_state **cached, gfp_t mask)
998{
999 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1000 mask);
1001}
1002
Chris Masond1310b22008-01-24 16:13:08 -05001003int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1004 gfp_t mask)
1005{
Chris Mason2c64c532009-09-02 15:04:12 -04001006 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1007 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001008}
Chris Masond1310b22008-01-24 16:13:08 -05001009
1010/*
1011 * helper function to set pages and extents in the tree dirty
1012 */
1013int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1014{
1015 unsigned long index = start >> PAGE_CACHE_SHIFT;
1016 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1017 struct page *page;
1018
1019 while (index <= end_index) {
1020 page = find_get_page(tree->mapping, index);
1021 BUG_ON(!page);
1022 __set_page_dirty_nobuffers(page);
1023 page_cache_release(page);
1024 index++;
1025 }
Chris Masond1310b22008-01-24 16:13:08 -05001026 return 0;
1027}
Chris Masond1310b22008-01-24 16:13:08 -05001028
1029/*
1030 * helper function to set both pages and extents in the tree writeback
1031 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001032static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001033{
1034 unsigned long index = start >> PAGE_CACHE_SHIFT;
1035 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1036 struct page *page;
1037
1038 while (index <= end_index) {
1039 page = find_get_page(tree->mapping, index);
1040 BUG_ON(!page);
1041 set_page_writeback(page);
1042 page_cache_release(page);
1043 index++;
1044 }
Chris Masond1310b22008-01-24 16:13:08 -05001045 return 0;
1046}
Chris Masond1310b22008-01-24 16:13:08 -05001047
Chris Masond352ac62008-09-29 15:18:18 -04001048/*
1049 * find the first offset in the io tree with 'bits' set. zero is
1050 * returned if we find something, and *start_ret and *end_ret are
1051 * set to reflect the state struct that was found.
1052 *
1053 * If nothing was found, 1 is returned, < 0 on error
1054 */
Chris Masond1310b22008-01-24 16:13:08 -05001055int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1056 u64 *start_ret, u64 *end_ret, int bits)
1057{
1058 struct rb_node *node;
1059 struct extent_state *state;
1060 int ret = 1;
1061
Chris Masoncad321a2008-12-17 14:51:42 -05001062 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001063 /*
1064 * this search will find all the extents that end after
1065 * our range starts.
1066 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001067 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001068 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001069 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001070
Chris Masond3977122009-01-05 21:25:51 -05001071 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001072 state = rb_entry(node, struct extent_state, rb_node);
1073 if (state->end >= start && (state->state & bits)) {
1074 *start_ret = state->start;
1075 *end_ret = state->end;
1076 ret = 0;
1077 break;
1078 }
1079 node = rb_next(node);
1080 if (!node)
1081 break;
1082 }
1083out:
Chris Masoncad321a2008-12-17 14:51:42 -05001084 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001085 return ret;
1086}
Chris Masond1310b22008-01-24 16:13:08 -05001087
Chris Masond352ac62008-09-29 15:18:18 -04001088/* find the first state struct with 'bits' set after 'start', and
1089 * return it. tree->lock must be held. NULL will returned if
1090 * nothing was found after 'start'
1091 */
Chris Masond7fc6402008-02-18 12:12:38 -05001092struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1093 u64 start, int bits)
1094{
1095 struct rb_node *node;
1096 struct extent_state *state;
1097
1098 /*
1099 * this search will find all the extents that end after
1100 * our range starts.
1101 */
1102 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001103 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001104 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001105
Chris Masond3977122009-01-05 21:25:51 -05001106 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001107 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001108 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001109 return state;
Chris Masond3977122009-01-05 21:25:51 -05001110
Chris Masond7fc6402008-02-18 12:12:38 -05001111 node = rb_next(node);
1112 if (!node)
1113 break;
1114 }
1115out:
1116 return NULL;
1117}
Chris Masond7fc6402008-02-18 12:12:38 -05001118
Chris Masond352ac62008-09-29 15:18:18 -04001119/*
1120 * find a contiguous range of bytes in the file marked as delalloc, not
1121 * more than 'max_bytes'. start and end are used to return the range,
1122 *
1123 * 1 is returned if we find something, 0 if nothing was in the tree
1124 */
Chris Masonc8b97812008-10-29 14:49:59 -04001125static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1126 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001127{
1128 struct rb_node *node;
1129 struct extent_state *state;
1130 u64 cur_start = *start;
1131 u64 found = 0;
1132 u64 total_bytes = 0;
1133
Chris Masoncad321a2008-12-17 14:51:42 -05001134 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001135
Chris Masond1310b22008-01-24 16:13:08 -05001136 /*
1137 * this search will find all the extents that end after
1138 * our range starts.
1139 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001140 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001141 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001142 if (!found)
1143 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001144 goto out;
1145 }
1146
Chris Masond3977122009-01-05 21:25:51 -05001147 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001148 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001149 if (found && (state->start != cur_start ||
1150 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001151 goto out;
1152 }
1153 if (!(state->state & EXTENT_DELALLOC)) {
1154 if (!found)
1155 *end = state->end;
1156 goto out;
1157 }
Chris Masond1310b22008-01-24 16:13:08 -05001158 if (!found)
1159 *start = state->start;
1160 found++;
1161 *end = state->end;
1162 cur_start = state->end + 1;
1163 node = rb_next(node);
1164 if (!node)
1165 break;
1166 total_bytes += state->end - state->start + 1;
1167 if (total_bytes >= max_bytes)
1168 break;
1169 }
1170out:
Chris Masoncad321a2008-12-17 14:51:42 -05001171 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001172 return found;
1173}
1174
Chris Masonc8b97812008-10-29 14:49:59 -04001175static noinline int __unlock_for_delalloc(struct inode *inode,
1176 struct page *locked_page,
1177 u64 start, u64 end)
1178{
1179 int ret;
1180 struct page *pages[16];
1181 unsigned long index = start >> PAGE_CACHE_SHIFT;
1182 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1183 unsigned long nr_pages = end_index - index + 1;
1184 int i;
1185
1186 if (index == locked_page->index && end_index == index)
1187 return 0;
1188
Chris Masond3977122009-01-05 21:25:51 -05001189 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001190 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001191 min_t(unsigned long, nr_pages,
1192 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001193 for (i = 0; i < ret; i++) {
1194 if (pages[i] != locked_page)
1195 unlock_page(pages[i]);
1196 page_cache_release(pages[i]);
1197 }
1198 nr_pages -= ret;
1199 index += ret;
1200 cond_resched();
1201 }
1202 return 0;
1203}
1204
1205static noinline int lock_delalloc_pages(struct inode *inode,
1206 struct page *locked_page,
1207 u64 delalloc_start,
1208 u64 delalloc_end)
1209{
1210 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1211 unsigned long start_index = index;
1212 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1213 unsigned long pages_locked = 0;
1214 struct page *pages[16];
1215 unsigned long nrpages;
1216 int ret;
1217 int i;
1218
1219 /* the caller is responsible for locking the start index */
1220 if (index == locked_page->index && index == end_index)
1221 return 0;
1222
1223 /* skip the page at the start index */
1224 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001225 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001226 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001227 min_t(unsigned long,
1228 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001229 if (ret == 0) {
1230 ret = -EAGAIN;
1231 goto done;
1232 }
1233 /* now we have an array of pages, lock them all */
1234 for (i = 0; i < ret; i++) {
1235 /*
1236 * the caller is taking responsibility for
1237 * locked_page
1238 */
Chris Mason771ed682008-11-06 22:02:51 -05001239 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001240 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001241 if (!PageDirty(pages[i]) ||
1242 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001243 ret = -EAGAIN;
1244 unlock_page(pages[i]);
1245 page_cache_release(pages[i]);
1246 goto done;
1247 }
1248 }
Chris Masonc8b97812008-10-29 14:49:59 -04001249 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001250 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001251 }
Chris Masonc8b97812008-10-29 14:49:59 -04001252 nrpages -= ret;
1253 index += ret;
1254 cond_resched();
1255 }
1256 ret = 0;
1257done:
1258 if (ret && pages_locked) {
1259 __unlock_for_delalloc(inode, locked_page,
1260 delalloc_start,
1261 ((u64)(start_index + pages_locked - 1)) <<
1262 PAGE_CACHE_SHIFT);
1263 }
1264 return ret;
1265}
1266
1267/*
1268 * find a contiguous range of bytes in the file marked as delalloc, not
1269 * more than 'max_bytes'. start and end are used to return the range,
1270 *
1271 * 1 is returned if we find something, 0 if nothing was in the tree
1272 */
1273static noinline u64 find_lock_delalloc_range(struct inode *inode,
1274 struct extent_io_tree *tree,
1275 struct page *locked_page,
1276 u64 *start, u64 *end,
1277 u64 max_bytes)
1278{
1279 u64 delalloc_start;
1280 u64 delalloc_end;
1281 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001282 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001283 int ret;
1284 int loops = 0;
1285
1286again:
1287 /* step one, find a bunch of delalloc bytes starting at start */
1288 delalloc_start = *start;
1289 delalloc_end = 0;
1290 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1291 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001292 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001293 *start = delalloc_start;
1294 *end = delalloc_end;
1295 return found;
1296 }
1297
1298 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001299 * start comes from the offset of locked_page. We have to lock
1300 * pages in order, so we can't process delalloc bytes before
1301 * locked_page
1302 */
Chris Masond3977122009-01-05 21:25:51 -05001303 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001304 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001305
1306 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001307 * make sure to limit the number of pages we try to lock down
1308 * if we're looping.
1309 */
Chris Masond3977122009-01-05 21:25:51 -05001310 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001311 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001312
Chris Masonc8b97812008-10-29 14:49:59 -04001313 /* step two, lock all the pages after the page that has start */
1314 ret = lock_delalloc_pages(inode, locked_page,
1315 delalloc_start, delalloc_end);
1316 if (ret == -EAGAIN) {
1317 /* some of the pages are gone, lets avoid looping by
1318 * shortening the size of the delalloc range we're searching
1319 */
Chris Mason9655d292009-09-02 15:22:30 -04001320 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001321 if (!loops) {
1322 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1323 max_bytes = PAGE_CACHE_SIZE - offset;
1324 loops = 1;
1325 goto again;
1326 } else {
1327 found = 0;
1328 goto out_failed;
1329 }
1330 }
1331 BUG_ON(ret);
1332
1333 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001334 lock_extent_bits(tree, delalloc_start, delalloc_end,
1335 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001336
1337 /* then test to make sure it is all still delalloc */
1338 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001339 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001340 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001341 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1342 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001343 __unlock_for_delalloc(inode, locked_page,
1344 delalloc_start, delalloc_end);
1345 cond_resched();
1346 goto again;
1347 }
Chris Mason9655d292009-09-02 15:22:30 -04001348 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001349 *start = delalloc_start;
1350 *end = delalloc_end;
1351out_failed:
1352 return found;
1353}
1354
1355int extent_clear_unlock_delalloc(struct inode *inode,
1356 struct extent_io_tree *tree,
1357 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001358 int unlock_pages,
1359 int clear_unlock,
1360 int clear_delalloc, int clear_dirty,
1361 int set_writeback,
Chris Mason8b62b722009-09-02 16:53:46 -04001362 int end_writeback,
1363 int set_private2)
Chris Masonc8b97812008-10-29 14:49:59 -04001364{
1365 int ret;
1366 struct page *pages[16];
1367 unsigned long index = start >> PAGE_CACHE_SHIFT;
1368 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1369 unsigned long nr_pages = end_index - index + 1;
1370 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001371 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001372
Chris Mason771ed682008-11-06 22:02:51 -05001373 if (clear_unlock)
1374 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001375 if (clear_dirty)
1376 clear_bits |= EXTENT_DIRTY;
1377
Chris Mason771ed682008-11-06 22:02:51 -05001378 if (clear_delalloc)
1379 clear_bits |= EXTENT_DELALLOC;
1380
Chris Mason2c64c532009-09-02 15:04:12 -04001381 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04001382 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback ||
1383 set_private2))
Chris Mason771ed682008-11-06 22:02:51 -05001384 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++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001391
1392 if (set_private2)
1393 SetPagePrivate2(pages[i]);
1394
Chris Masonc8b97812008-10-29 14:49:59 -04001395 if (pages[i] == locked_page) {
1396 page_cache_release(pages[i]);
1397 continue;
1398 }
1399 if (clear_dirty)
1400 clear_page_dirty_for_io(pages[i]);
1401 if (set_writeback)
1402 set_page_writeback(pages[i]);
1403 if (end_writeback)
1404 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001405 if (unlock_pages)
1406 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001407 page_cache_release(pages[i]);
1408 }
1409 nr_pages -= ret;
1410 index += ret;
1411 cond_resched();
1412 }
1413 return 0;
1414}
Chris Masonc8b97812008-10-29 14:49:59 -04001415
Chris Masond352ac62008-09-29 15:18:18 -04001416/*
1417 * count the number of bytes in the tree that have a given bit(s)
1418 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1419 * cached. The total number found is returned.
1420 */
Chris Masond1310b22008-01-24 16:13:08 -05001421u64 count_range_bits(struct extent_io_tree *tree,
1422 u64 *start, u64 search_end, u64 max_bytes,
1423 unsigned long bits)
1424{
1425 struct rb_node *node;
1426 struct extent_state *state;
1427 u64 cur_start = *start;
1428 u64 total_bytes = 0;
1429 int found = 0;
1430
1431 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001432 WARN_ON(1);
1433 return 0;
1434 }
1435
Chris Masoncad321a2008-12-17 14:51:42 -05001436 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001437 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1438 total_bytes = tree->dirty_bytes;
1439 goto out;
1440 }
1441 /*
1442 * this search will find all the extents that end after
1443 * our range starts.
1444 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001445 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001446 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001447 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001448
Chris Masond3977122009-01-05 21:25:51 -05001449 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001450 state = rb_entry(node, struct extent_state, rb_node);
1451 if (state->start > search_end)
1452 break;
1453 if (state->end >= cur_start && (state->state & bits)) {
1454 total_bytes += min(search_end, state->end) + 1 -
1455 max(cur_start, state->start);
1456 if (total_bytes >= max_bytes)
1457 break;
1458 if (!found) {
1459 *start = state->start;
1460 found = 1;
1461 }
1462 }
1463 node = rb_next(node);
1464 if (!node)
1465 break;
1466 }
1467out:
Chris Masoncad321a2008-12-17 14:51:42 -05001468 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001469 return total_bytes;
1470}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001471
Chris Masond352ac62008-09-29 15:18:18 -04001472/*
1473 * set the private field for a given byte offset in the tree. If there isn't
1474 * an extent_state there already, this does nothing.
1475 */
Chris Masond1310b22008-01-24 16:13:08 -05001476int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1477{
1478 struct rb_node *node;
1479 struct extent_state *state;
1480 int ret = 0;
1481
Chris Masoncad321a2008-12-17 14:51:42 -05001482 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001483 /*
1484 * this search will find all the extents that end after
1485 * our range starts.
1486 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001487 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001488 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001489 ret = -ENOENT;
1490 goto out;
1491 }
1492 state = rb_entry(node, struct extent_state, rb_node);
1493 if (state->start != start) {
1494 ret = -ENOENT;
1495 goto out;
1496 }
1497 state->private = private;
1498out:
Chris Masoncad321a2008-12-17 14:51:42 -05001499 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001500 return ret;
1501}
1502
1503int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1504{
1505 struct rb_node *node;
1506 struct extent_state *state;
1507 int ret = 0;
1508
Chris Masoncad321a2008-12-17 14:51:42 -05001509 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001510 /*
1511 * this search will find all the extents that end after
1512 * our range starts.
1513 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001514 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001515 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001516 ret = -ENOENT;
1517 goto out;
1518 }
1519 state = rb_entry(node, struct extent_state, rb_node);
1520 if (state->start != start) {
1521 ret = -ENOENT;
1522 goto out;
1523 }
1524 *private = state->private;
1525out:
Chris Masoncad321a2008-12-17 14:51:42 -05001526 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001527 return ret;
1528}
1529
1530/*
1531 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001532 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001533 * has the bits set. Otherwise, 1 is returned if any bit in the
1534 * range is found set.
1535 */
1536int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001537 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001538{
1539 struct extent_state *state = NULL;
1540 struct rb_node *node;
1541 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001542
Chris Masoncad321a2008-12-17 14:51:42 -05001543 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001544 if (cached && cached->tree && cached->start == start)
1545 node = &cached->rb_node;
1546 else
1547 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001548 while (node && start <= end) {
1549 state = rb_entry(node, struct extent_state, rb_node);
1550
1551 if (filled && state->start > start) {
1552 bitset = 0;
1553 break;
1554 }
1555
1556 if (state->start > end)
1557 break;
1558
1559 if (state->state & bits) {
1560 bitset = 1;
1561 if (!filled)
1562 break;
1563 } else if (filled) {
1564 bitset = 0;
1565 break;
1566 }
Chris Mason46562cec2009-09-23 20:23:16 -04001567
1568 if (state->end == (u64)-1)
1569 break;
1570
Chris Masond1310b22008-01-24 16:13:08 -05001571 start = state->end + 1;
1572 if (start > end)
1573 break;
1574 node = rb_next(node);
1575 if (!node) {
1576 if (filled)
1577 bitset = 0;
1578 break;
1579 }
1580 }
Chris Masoncad321a2008-12-17 14:51:42 -05001581 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001582 return bitset;
1583}
Chris Masond1310b22008-01-24 16:13:08 -05001584
1585/*
1586 * helper function to set a given page up to date if all the
1587 * extents in the tree for that page are up to date
1588 */
1589static int check_page_uptodate(struct extent_io_tree *tree,
1590 struct page *page)
1591{
1592 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1593 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001594 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001595 SetPageUptodate(page);
1596 return 0;
1597}
1598
1599/*
1600 * helper function to unlock a page if all the extents in the tree
1601 * for that page are unlocked
1602 */
1603static int check_page_locked(struct extent_io_tree *tree,
1604 struct page *page)
1605{
1606 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1607 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001608 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001609 unlock_page(page);
1610 return 0;
1611}
1612
1613/*
1614 * helper function to end page writeback if all the extents
1615 * in the tree for that page are done with writeback
1616 */
1617static int check_page_writeback(struct extent_io_tree *tree,
1618 struct page *page)
1619{
Chris Mason1edbb732009-09-02 13:24:36 -04001620 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001621 return 0;
1622}
1623
1624/* lots and lots of room for performance fixes in the end_bio funcs */
1625
1626/*
1627 * after a writepage IO is done, we need to:
1628 * clear the uptodate bits on error
1629 * clear the writeback bits in the extent tree for this IO
1630 * end_page_writeback if the page has no more pending IO
1631 *
1632 * Scheduling is not allowed, so the extent state tree is expected
1633 * to have one and only one object corresponding to this IO.
1634 */
Chris Masond1310b22008-01-24 16:13:08 -05001635static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001636{
Chris Mason1259ab72008-05-12 13:39:03 -04001637 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001638 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001639 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001640 u64 start;
1641 u64 end;
1642 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001643 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001644
Chris Masond1310b22008-01-24 16:13:08 -05001645 do {
1646 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001647 tree = &BTRFS_I(page->mapping->host)->io_tree;
1648
Chris Masond1310b22008-01-24 16:13:08 -05001649 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1650 bvec->bv_offset;
1651 end = start + bvec->bv_len - 1;
1652
1653 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1654 whole_page = 1;
1655 else
1656 whole_page = 0;
1657
1658 if (--bvec >= bio->bi_io_vec)
1659 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001660 if (tree->ops && tree->ops->writepage_end_io_hook) {
1661 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001662 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001663 if (ret)
1664 uptodate = 0;
1665 }
1666
1667 if (!uptodate && tree->ops &&
1668 tree->ops->writepage_io_failed_hook) {
1669 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001670 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001671 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001672 uptodate = (err == 0);
1673 continue;
1674 }
1675 }
1676
Chris Masond1310b22008-01-24 16:13:08 -05001677 if (!uptodate) {
Chris Mason1edbb732009-09-02 13:24:36 -04001678 clear_extent_uptodate(tree, start, end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001679 ClearPageUptodate(page);
1680 SetPageError(page);
1681 }
Chris Mason70dec802008-01-29 09:59:12 -05001682
Chris Masond1310b22008-01-24 16:13:08 -05001683 if (whole_page)
1684 end_page_writeback(page);
1685 else
1686 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001687 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001688
Chris Masond1310b22008-01-24 16:13:08 -05001689 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001690}
1691
1692/*
1693 * after a readpage IO is done, we need to:
1694 * clear the uptodate bits on error
1695 * set the uptodate bits if things worked
1696 * set the page up to date if all extents in the tree are uptodate
1697 * clear the lock bit in the extent tree
1698 * unlock the page if there are no other extents locked for it
1699 *
1700 * Scheduling is not allowed, so the extent state tree is expected
1701 * to have one and only one object corresponding to this IO.
1702 */
Chris Masond1310b22008-01-24 16:13:08 -05001703static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001704{
1705 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1706 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001707 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001708 u64 start;
1709 u64 end;
1710 int whole_page;
1711 int ret;
1712
Chris Masond20f7042008-12-08 16:58:54 -05001713 if (err)
1714 uptodate = 0;
1715
Chris Masond1310b22008-01-24 16:13:08 -05001716 do {
1717 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001718 tree = &BTRFS_I(page->mapping->host)->io_tree;
1719
Chris Masond1310b22008-01-24 16:13:08 -05001720 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1721 bvec->bv_offset;
1722 end = start + bvec->bv_len - 1;
1723
1724 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1725 whole_page = 1;
1726 else
1727 whole_page = 0;
1728
1729 if (--bvec >= bio->bi_io_vec)
1730 prefetchw(&bvec->bv_page->flags);
1731
1732 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001733 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001734 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001735 if (ret)
1736 uptodate = 0;
1737 }
Chris Mason7e383262008-04-09 16:28:12 -04001738 if (!uptodate && tree->ops &&
1739 tree->ops->readpage_io_failed_hook) {
1740 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001741 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001742 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001743 uptodate =
1744 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001745 if (err)
1746 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001747 continue;
1748 }
1749 }
Chris Mason70dec802008-01-29 09:59:12 -05001750
Chris Mason771ed682008-11-06 22:02:51 -05001751 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001752 set_extent_uptodate(tree, start, end,
1753 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001754 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001755 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001756
Chris Mason70dec802008-01-29 09:59:12 -05001757 if (whole_page) {
1758 if (uptodate) {
1759 SetPageUptodate(page);
1760 } else {
1761 ClearPageUptodate(page);
1762 SetPageError(page);
1763 }
Chris Masond1310b22008-01-24 16:13:08 -05001764 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001765 } else {
1766 if (uptodate) {
1767 check_page_uptodate(tree, page);
1768 } else {
1769 ClearPageUptodate(page);
1770 SetPageError(page);
1771 }
Chris Masond1310b22008-01-24 16:13:08 -05001772 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001773 }
Chris Masond1310b22008-01-24 16:13:08 -05001774 } while (bvec >= bio->bi_io_vec);
1775
1776 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001777}
1778
1779/*
1780 * IO done from prepare_write is pretty simple, we just unlock
1781 * the structs in the extent tree when done, and set the uptodate bits
1782 * as appropriate.
1783 */
Chris Masond1310b22008-01-24 16:13:08 -05001784static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001785{
1786 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1787 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001788 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001789 u64 start;
1790 u64 end;
1791
Chris Masond1310b22008-01-24 16:13:08 -05001792 do {
1793 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001794 tree = &BTRFS_I(page->mapping->host)->io_tree;
1795
Chris Masond1310b22008-01-24 16:13:08 -05001796 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1797 bvec->bv_offset;
1798 end = start + bvec->bv_len - 1;
1799
1800 if (--bvec >= bio->bi_io_vec)
1801 prefetchw(&bvec->bv_page->flags);
1802
1803 if (uptodate) {
1804 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1805 } else {
1806 ClearPageUptodate(page);
1807 SetPageError(page);
1808 }
1809
1810 unlock_extent(tree, start, end, GFP_ATOMIC);
1811
1812 } while (bvec >= bio->bi_io_vec);
1813
1814 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001815}
1816
1817static struct bio *
1818extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1819 gfp_t gfp_flags)
1820{
1821 struct bio *bio;
1822
1823 bio = bio_alloc(gfp_flags, nr_vecs);
1824
1825 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1826 while (!bio && (nr_vecs /= 2))
1827 bio = bio_alloc(gfp_flags, nr_vecs);
1828 }
1829
1830 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001831 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001832 bio->bi_bdev = bdev;
1833 bio->bi_sector = first_sector;
1834 }
1835 return bio;
1836}
1837
Chris Masonc8b97812008-10-29 14:49:59 -04001838static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1839 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001840{
Chris Masond1310b22008-01-24 16:13:08 -05001841 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001842 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1843 struct page *page = bvec->bv_page;
1844 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001845 u64 start;
1846 u64 end;
1847
1848 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1849 end = start + bvec->bv_len - 1;
1850
David Woodhouse902b22f2008-08-20 08:51:49 -04001851 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001852
1853 bio_get(bio);
1854
Chris Mason065631f2008-02-20 12:07:25 -05001855 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001856 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001857 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001858 else
1859 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001860 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1861 ret = -EOPNOTSUPP;
1862 bio_put(bio);
1863 return ret;
1864}
1865
1866static int submit_extent_page(int rw, struct extent_io_tree *tree,
1867 struct page *page, sector_t sector,
1868 size_t size, unsigned long offset,
1869 struct block_device *bdev,
1870 struct bio **bio_ret,
1871 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001872 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001873 int mirror_num,
1874 unsigned long prev_bio_flags,
1875 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001876{
1877 int ret = 0;
1878 struct bio *bio;
1879 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001880 int contig = 0;
1881 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1882 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001883 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001884
1885 if (bio_ret && *bio_ret) {
1886 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001887 if (old_compressed)
1888 contig = bio->bi_sector == sector;
1889 else
1890 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1891 sector;
1892
1893 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001894 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001895 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1896 bio_flags)) ||
1897 bio_add_page(bio, page, page_size, offset) < page_size) {
1898 ret = submit_one_bio(rw, bio, mirror_num,
1899 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001900 bio = NULL;
1901 } else {
1902 return 0;
1903 }
1904 }
Chris Masonc8b97812008-10-29 14:49:59 -04001905 if (this_compressed)
1906 nr = BIO_MAX_PAGES;
1907 else
1908 nr = bio_get_nr_vecs(bdev);
1909
Chris Masond1310b22008-01-24 16:13:08 -05001910 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001911
Chris Masonc8b97812008-10-29 14:49:59 -04001912 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001913 bio->bi_end_io = end_io_func;
1914 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001915
Chris Masond3977122009-01-05 21:25:51 -05001916 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001917 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001918 else
Chris Masonc8b97812008-10-29 14:49:59 -04001919 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001920
1921 return ret;
1922}
1923
1924void set_page_extent_mapped(struct page *page)
1925{
1926 if (!PagePrivate(page)) {
1927 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001928 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001929 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001930 }
1931}
1932
Christoph Hellwigb2950862008-12-02 09:54:17 -05001933static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001934{
1935 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1936}
1937
1938/*
1939 * basic readpage implementation. Locked extent state structs are inserted
1940 * into the tree that are removed when the IO is done (by the end_io
1941 * handlers)
1942 */
1943static int __extent_read_full_page(struct extent_io_tree *tree,
1944 struct page *page,
1945 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001946 struct bio **bio, int mirror_num,
1947 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001948{
1949 struct inode *inode = page->mapping->host;
1950 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1951 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1952 u64 end;
1953 u64 cur = start;
1954 u64 extent_offset;
1955 u64 last_byte = i_size_read(inode);
1956 u64 block_start;
1957 u64 cur_end;
1958 sector_t sector;
1959 struct extent_map *em;
1960 struct block_device *bdev;
1961 int ret;
1962 int nr = 0;
1963 size_t page_offset = 0;
1964 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001965 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001966 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001967 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001968
1969 set_page_extent_mapped(page);
1970
1971 end = page_end;
1972 lock_extent(tree, start, end, GFP_NOFS);
1973
Chris Masonc8b97812008-10-29 14:49:59 -04001974 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1975 char *userpage;
1976 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1977
1978 if (zero_offset) {
1979 iosize = PAGE_CACHE_SIZE - zero_offset;
1980 userpage = kmap_atomic(page, KM_USER0);
1981 memset(userpage + zero_offset, 0, iosize);
1982 flush_dcache_page(page);
1983 kunmap_atomic(userpage, KM_USER0);
1984 }
1985 }
Chris Masond1310b22008-01-24 16:13:08 -05001986 while (cur <= end) {
1987 if (cur >= last_byte) {
1988 char *userpage;
1989 iosize = PAGE_CACHE_SIZE - page_offset;
1990 userpage = kmap_atomic(page, KM_USER0);
1991 memset(userpage + page_offset, 0, iosize);
1992 flush_dcache_page(page);
1993 kunmap_atomic(userpage, KM_USER0);
1994 set_extent_uptodate(tree, cur, cur + iosize - 1,
1995 GFP_NOFS);
1996 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1997 break;
1998 }
1999 em = get_extent(inode, page, page_offset, cur,
2000 end - cur + 1, 0);
2001 if (IS_ERR(em) || !em) {
2002 SetPageError(page);
2003 unlock_extent(tree, cur, end, GFP_NOFS);
2004 break;
2005 }
Chris Masond1310b22008-01-24 16:13:08 -05002006 extent_offset = cur - em->start;
2007 BUG_ON(extent_map_end(em) <= cur);
2008 BUG_ON(end < cur);
2009
Chris Masonc8b97812008-10-29 14:49:59 -04002010 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2011 this_bio_flag = EXTENT_BIO_COMPRESSED;
2012
Chris Masond1310b22008-01-24 16:13:08 -05002013 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2014 cur_end = min(extent_map_end(em) - 1, end);
2015 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002016 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2017 disk_io_size = em->block_len;
2018 sector = em->block_start >> 9;
2019 } else {
2020 sector = (em->block_start + extent_offset) >> 9;
2021 disk_io_size = iosize;
2022 }
Chris Masond1310b22008-01-24 16:13:08 -05002023 bdev = em->bdev;
2024 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002025 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2026 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002027 free_extent_map(em);
2028 em = NULL;
2029
2030 /* we've found a hole, just zero and go on */
2031 if (block_start == EXTENT_MAP_HOLE) {
2032 char *userpage;
2033 userpage = kmap_atomic(page, KM_USER0);
2034 memset(userpage + page_offset, 0, iosize);
2035 flush_dcache_page(page);
2036 kunmap_atomic(userpage, KM_USER0);
2037
2038 set_extent_uptodate(tree, cur, cur + iosize - 1,
2039 GFP_NOFS);
2040 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2041 cur = cur + iosize;
2042 page_offset += iosize;
2043 continue;
2044 }
2045 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002046 if (test_range_bit(tree, cur, cur_end,
2047 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002048 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002049 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2050 cur = cur + iosize;
2051 page_offset += iosize;
2052 continue;
2053 }
Chris Mason70dec802008-01-29 09:59:12 -05002054 /* we have an inline extent but it didn't get marked up
2055 * to date. Error out
2056 */
2057 if (block_start == EXTENT_MAP_INLINE) {
2058 SetPageError(page);
2059 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2060 cur = cur + iosize;
2061 page_offset += iosize;
2062 continue;
2063 }
Chris Masond1310b22008-01-24 16:13:08 -05002064
2065 ret = 0;
2066 if (tree->ops && tree->ops->readpage_io_hook) {
2067 ret = tree->ops->readpage_io_hook(page, cur,
2068 cur + iosize - 1);
2069 }
2070 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002071 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2072 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002073 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002074 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002075 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002076 end_bio_extent_readpage, mirror_num,
2077 *bio_flags,
2078 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002079 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002080 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002081 }
2082 if (ret)
2083 SetPageError(page);
2084 cur = cur + iosize;
2085 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002086 }
2087 if (!nr) {
2088 if (!PageError(page))
2089 SetPageUptodate(page);
2090 unlock_page(page);
2091 }
2092 return 0;
2093}
2094
2095int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2096 get_extent_t *get_extent)
2097{
2098 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002099 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002100 int ret;
2101
Chris Masonc8b97812008-10-29 14:49:59 -04002102 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2103 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002104 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002105 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002106 return ret;
2107}
Chris Masond1310b22008-01-24 16:13:08 -05002108
Chris Mason11c83492009-04-20 15:50:09 -04002109static noinline void update_nr_written(struct page *page,
2110 struct writeback_control *wbc,
2111 unsigned long nr_written)
2112{
2113 wbc->nr_to_write -= nr_written;
2114 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2115 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2116 page->mapping->writeback_index = page->index + nr_written;
2117}
2118
Chris Masond1310b22008-01-24 16:13:08 -05002119/*
2120 * the writepage semantics are similar to regular writepage. extent
2121 * records are inserted to lock ranges in the tree, and as dirty areas
2122 * are found, they are marked writeback. Then the lock bits are removed
2123 * and the end_io handler clears the writeback ranges
2124 */
2125static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2126 void *data)
2127{
2128 struct inode *inode = page->mapping->host;
2129 struct extent_page_data *epd = data;
2130 struct extent_io_tree *tree = epd->tree;
2131 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2132 u64 delalloc_start;
2133 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2134 u64 end;
2135 u64 cur = start;
2136 u64 extent_offset;
2137 u64 last_byte = i_size_read(inode);
2138 u64 block_start;
2139 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002140 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002141 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002142 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002143 struct extent_map *em;
2144 struct block_device *bdev;
2145 int ret;
2146 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002147 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002148 size_t blocksize;
2149 loff_t i_size = i_size_read(inode);
2150 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2151 u64 nr_delalloc;
2152 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002153 int page_started;
2154 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002155 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002156 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002157
Chris Masonffbd5172009-04-20 15:50:09 -04002158 if (wbc->sync_mode == WB_SYNC_ALL)
2159 write_flags = WRITE_SYNC_PLUG;
2160 else
2161 write_flags = WRITE;
2162
Chris Masond1310b22008-01-24 16:13:08 -05002163 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002164 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002165 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002166 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002167 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002168 unlock_page(page);
2169 return 0;
2170 }
2171
2172 if (page->index == end_index) {
2173 char *userpage;
2174
Chris Masond1310b22008-01-24 16:13:08 -05002175 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002176 memset(userpage + pg_offset, 0,
2177 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002178 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002179 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002180 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002181 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002182
2183 set_page_extent_mapped(page);
2184
2185 delalloc_start = start;
2186 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002187 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002188 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002189 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002190 /*
2191 * make sure the wbc mapping index is at least updated
2192 * to this page.
2193 */
2194 update_nr_written(page, wbc, 0);
2195
Chris Masond3977122009-01-05 21:25:51 -05002196 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002197 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002198 page,
2199 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002200 &delalloc_end,
2201 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002202 if (nr_delalloc == 0) {
2203 delalloc_start = delalloc_end + 1;
2204 continue;
2205 }
2206 tree->ops->fill_delalloc(inode, page, delalloc_start,
2207 delalloc_end, &page_started,
2208 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002209 /*
2210 * delalloc_end is already one less than the total
2211 * length, so we don't subtract one from
2212 * PAGE_CACHE_SIZE
2213 */
2214 delalloc_to_write += (delalloc_end - delalloc_start +
2215 PAGE_CACHE_SIZE) >>
2216 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002217 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002218 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002219 if (wbc->nr_to_write < delalloc_to_write) {
2220 int thresh = 8192;
2221
2222 if (delalloc_to_write < thresh * 2)
2223 thresh = delalloc_to_write;
2224 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2225 thresh);
2226 }
Chris Masonc8b97812008-10-29 14:49:59 -04002227
Chris Mason771ed682008-11-06 22:02:51 -05002228 /* did the fill delalloc function already unlock and start
2229 * the IO?
2230 */
2231 if (page_started) {
2232 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002233 /*
2234 * we've unlocked the page, so we can't update
2235 * the mapping's writeback index, just update
2236 * nr_to_write.
2237 */
2238 wbc->nr_to_write -= nr_written;
2239 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002240 }
Chris Masonc8b97812008-10-29 14:49:59 -04002241 }
Chris Mason247e7432008-07-17 12:53:51 -04002242 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002243 ret = tree->ops->writepage_start_hook(page, start,
2244 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002245 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002246 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002247 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002248 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002249 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002250 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002251 }
2252 }
2253
Chris Mason11c83492009-04-20 15:50:09 -04002254 /*
2255 * we don't want to touch the inode after unlocking the page,
2256 * so we update the mapping writeback index now
2257 */
2258 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002259
Chris Masond1310b22008-01-24 16:13:08 -05002260 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002261 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002262 if (tree->ops && tree->ops->writepage_end_io_hook)
2263 tree->ops->writepage_end_io_hook(page, start,
2264 page_end, NULL, 1);
2265 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002266 goto done;
2267 }
2268
Chris Masond1310b22008-01-24 16:13:08 -05002269 blocksize = inode->i_sb->s_blocksize;
2270
2271 while (cur <= end) {
2272 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002273 if (tree->ops && tree->ops->writepage_end_io_hook)
2274 tree->ops->writepage_end_io_hook(page, cur,
2275 page_end, NULL, 1);
2276 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002277 break;
2278 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002279 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002280 end - cur + 1, 1);
2281 if (IS_ERR(em) || !em) {
2282 SetPageError(page);
2283 break;
2284 }
2285
2286 extent_offset = cur - em->start;
2287 BUG_ON(extent_map_end(em) <= cur);
2288 BUG_ON(end < cur);
2289 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2290 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2291 sector = (em->block_start + extent_offset) >> 9;
2292 bdev = em->bdev;
2293 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002294 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002295 free_extent_map(em);
2296 em = NULL;
2297
Chris Masonc8b97812008-10-29 14:49:59 -04002298 /*
2299 * compressed and inline extents are written through other
2300 * paths in the FS
2301 */
2302 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002303 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002304 /*
2305 * end_io notification does not happen here for
2306 * compressed extents
2307 */
2308 if (!compressed && tree->ops &&
2309 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002310 tree->ops->writepage_end_io_hook(page, cur,
2311 cur + iosize - 1,
2312 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002313 else if (compressed) {
2314 /* we don't want to end_page_writeback on
2315 * a compressed extent. this happens
2316 * elsewhere
2317 */
2318 nr++;
2319 }
2320
2321 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002322 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002323 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002324 continue;
2325 }
Chris Masond1310b22008-01-24 16:13:08 -05002326 /* leave this out until we have a page_mkwrite call */
2327 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002328 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002329 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002330 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002331 continue;
2332 }
Chris Masonc8b97812008-10-29 14:49:59 -04002333
Chris Masond1310b22008-01-24 16:13:08 -05002334 if (tree->ops && tree->ops->writepage_io_hook) {
2335 ret = tree->ops->writepage_io_hook(page, cur,
2336 cur + iosize - 1);
2337 } else {
2338 ret = 0;
2339 }
Chris Mason1259ab72008-05-12 13:39:03 -04002340 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002341 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002342 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002343 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002344
Chris Masond1310b22008-01-24 16:13:08 -05002345 set_range_writeback(tree, cur, cur + iosize - 1);
2346 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002347 printk(KERN_ERR "btrfs warning page %lu not "
2348 "writeback, cur %llu end %llu\n",
2349 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002350 (unsigned long long)end);
2351 }
2352
Chris Masonffbd5172009-04-20 15:50:09 -04002353 ret = submit_extent_page(write_flags, tree, page,
2354 sector, iosize, pg_offset,
2355 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002356 end_bio_extent_writepage,
2357 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002358 if (ret)
2359 SetPageError(page);
2360 }
2361 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002362 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002363 nr++;
2364 }
2365done:
2366 if (nr == 0) {
2367 /* make sure the mapping tag for page dirty gets cleared */
2368 set_page_writeback(page);
2369 end_page_writeback(page);
2370 }
Chris Masond1310b22008-01-24 16:13:08 -05002371 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002372
Chris Mason11c83492009-04-20 15:50:09 -04002373done_unlocked:
2374
Chris Mason2c64c532009-09-02 15:04:12 -04002375 /* drop our reference on any cached states */
2376 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002377 return 0;
2378}
2379
Chris Masond1310b22008-01-24 16:13:08 -05002380/**
Chris Mason4bef0842008-09-08 11:18:08 -04002381 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05002382 * @mapping: address space structure to write
2383 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2384 * @writepage: function called for each page
2385 * @data: data passed to writepage function
2386 *
2387 * If a page is already under I/O, write_cache_pages() skips it, even
2388 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2389 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2390 * and msync() need to guarantee that all the data which was dirty at the time
2391 * the call was made get new I/O started against them. If wbc->sync_mode is
2392 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2393 * existing IO to complete.
2394 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002395static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002396 struct address_space *mapping,
2397 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002398 writepage_t writepage, void *data,
2399 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002400{
Chris Masond1310b22008-01-24 16:13:08 -05002401 int ret = 0;
2402 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002403 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002404 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:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002423 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002424 (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 Masonf85d7d6c2009-09-18 16:03:16 -04002474 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002475 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002476
2477 /*
2478 * the filesystem may choose to bump up nr_to_write.
2479 * We have to make sure to honor the new nr_to_write
2480 * at any time
2481 */
2482 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002483 }
2484 pagevec_release(&pvec);
2485 cond_resched();
2486 }
2487 if (!scanned && !done) {
2488 /*
2489 * We hit the last page and there is more work to be done: wrap
2490 * back to the start of the file
2491 */
2492 scanned = 1;
2493 index = 0;
2494 goto retry;
2495 }
Chris Masond1310b22008-01-24 16:13:08 -05002496 return ret;
2497}
Chris Masond1310b22008-01-24 16:13:08 -05002498
Chris Masonffbd5172009-04-20 15:50:09 -04002499static void flush_epd_write_bio(struct extent_page_data *epd)
2500{
2501 if (epd->bio) {
2502 if (epd->sync_io)
2503 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2504 else
2505 submit_one_bio(WRITE, epd->bio, 0, 0);
2506 epd->bio = NULL;
2507 }
2508}
2509
Chris Masond2c3f4f2008-11-19 12:44:22 -05002510static noinline void flush_write_bio(void *data)
2511{
2512 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002513 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002514}
2515
Chris Masond1310b22008-01-24 16:13:08 -05002516int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2517 get_extent_t *get_extent,
2518 struct writeback_control *wbc)
2519{
2520 int ret;
2521 struct address_space *mapping = page->mapping;
2522 struct extent_page_data epd = {
2523 .bio = NULL,
2524 .tree = tree,
2525 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002526 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002527 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002528 };
2529 struct writeback_control wbc_writepages = {
2530 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002531 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002532 .older_than_this = NULL,
2533 .nr_to_write = 64,
2534 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2535 .range_end = (loff_t)-1,
2536 };
2537
Chris Masond1310b22008-01-24 16:13:08 -05002538 ret = __extent_writepage(page, wbc, &epd);
2539
Chris Mason4bef0842008-09-08 11:18:08 -04002540 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002541 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002542 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002543 return ret;
2544}
Chris Masond1310b22008-01-24 16:13:08 -05002545
Chris Mason771ed682008-11-06 22:02:51 -05002546int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2547 u64 start, u64 end, get_extent_t *get_extent,
2548 int mode)
2549{
2550 int ret = 0;
2551 struct address_space *mapping = inode->i_mapping;
2552 struct page *page;
2553 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2554 PAGE_CACHE_SHIFT;
2555
2556 struct extent_page_data epd = {
2557 .bio = NULL,
2558 .tree = tree,
2559 .get_extent = get_extent,
2560 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002561 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002562 };
2563 struct writeback_control wbc_writepages = {
2564 .bdi = inode->i_mapping->backing_dev_info,
2565 .sync_mode = mode,
2566 .older_than_this = NULL,
2567 .nr_to_write = nr_pages * 2,
2568 .range_start = start,
2569 .range_end = end + 1,
2570 };
2571
Chris Masond3977122009-01-05 21:25:51 -05002572 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002573 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2574 if (clear_page_dirty_for_io(page))
2575 ret = __extent_writepage(page, &wbc_writepages, &epd);
2576 else {
2577 if (tree->ops && tree->ops->writepage_end_io_hook)
2578 tree->ops->writepage_end_io_hook(page, start,
2579 start + PAGE_CACHE_SIZE - 1,
2580 NULL, 1);
2581 unlock_page(page);
2582 }
2583 page_cache_release(page);
2584 start += PAGE_CACHE_SIZE;
2585 }
2586
Chris Masonffbd5172009-04-20 15:50:09 -04002587 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002588 return ret;
2589}
Chris Masond1310b22008-01-24 16:13:08 -05002590
2591int extent_writepages(struct extent_io_tree *tree,
2592 struct address_space *mapping,
2593 get_extent_t *get_extent,
2594 struct writeback_control *wbc)
2595{
2596 int ret = 0;
2597 struct extent_page_data epd = {
2598 .bio = NULL,
2599 .tree = tree,
2600 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002601 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002602 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002603 };
2604
Chris Mason4bef0842008-09-08 11:18:08 -04002605 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002606 __extent_writepage, &epd,
2607 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002608 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002609 return ret;
2610}
Chris Masond1310b22008-01-24 16:13:08 -05002611
2612int extent_readpages(struct extent_io_tree *tree,
2613 struct address_space *mapping,
2614 struct list_head *pages, unsigned nr_pages,
2615 get_extent_t get_extent)
2616{
2617 struct bio *bio = NULL;
2618 unsigned page_idx;
2619 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002620 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002621
2622 pagevec_init(&pvec, 0);
2623 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2624 struct page *page = list_entry(pages->prev, struct page, lru);
2625
2626 prefetchw(&page->flags);
2627 list_del(&page->lru);
2628 /*
2629 * what we want to do here is call add_to_page_cache_lru,
2630 * but that isn't exported, so we reproduce it here
2631 */
2632 if (!add_to_page_cache(page, mapping,
2633 page->index, GFP_KERNEL)) {
2634
2635 /* open coding of lru_cache_add, also not exported */
2636 page_cache_get(page);
2637 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002638 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002639 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002640 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002641 }
2642 page_cache_release(page);
2643 }
2644 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002645 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002646 BUG_ON(!list_empty(pages));
2647 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002648 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002649 return 0;
2650}
Chris Masond1310b22008-01-24 16:13:08 -05002651
2652/*
2653 * basic invalidatepage code, this waits on any locked or writeback
2654 * ranges corresponding to the page, and then deletes any extent state
2655 * records from the tree
2656 */
2657int extent_invalidatepage(struct extent_io_tree *tree,
2658 struct page *page, unsigned long offset)
2659{
2660 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2661 u64 end = start + PAGE_CACHE_SIZE - 1;
2662 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2663
Chris Masond3977122009-01-05 21:25:51 -05002664 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002665 if (start > end)
2666 return 0;
2667
2668 lock_extent(tree, start, end, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002669 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002670 clear_extent_bit(tree, start, end,
2671 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Mason2c64c532009-09-02 15:04:12 -04002672 1, 1, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002673 return 0;
2674}
Chris Masond1310b22008-01-24 16:13:08 -05002675
2676/*
2677 * simple commit_write call, set_range_dirty is used to mark both
2678 * the pages and the extent records as dirty
2679 */
2680int extent_commit_write(struct extent_io_tree *tree,
2681 struct inode *inode, struct page *page,
2682 unsigned from, unsigned to)
2683{
2684 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2685
2686 set_page_extent_mapped(page);
2687 set_page_dirty(page);
2688
2689 if (pos > inode->i_size) {
2690 i_size_write(inode, pos);
2691 mark_inode_dirty(inode);
2692 }
2693 return 0;
2694}
Chris Masond1310b22008-01-24 16:13:08 -05002695
2696int extent_prepare_write(struct extent_io_tree *tree,
2697 struct inode *inode, struct page *page,
2698 unsigned from, unsigned to, get_extent_t *get_extent)
2699{
2700 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2701 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2702 u64 block_start;
2703 u64 orig_block_start;
2704 u64 block_end;
2705 u64 cur_end;
2706 struct extent_map *em;
2707 unsigned blocksize = 1 << inode->i_blkbits;
2708 size_t page_offset = 0;
2709 size_t block_off_start;
2710 size_t block_off_end;
2711 int err = 0;
2712 int iocount = 0;
2713 int ret = 0;
2714 int isnew;
2715
2716 set_page_extent_mapped(page);
2717
2718 block_start = (page_start + from) & ~((u64)blocksize - 1);
2719 block_end = (page_start + to - 1) | (blocksize - 1);
2720 orig_block_start = block_start;
2721
2722 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002723 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002724 em = get_extent(inode, page, page_offset, block_start,
2725 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002726 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002727 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002728
Chris Masond1310b22008-01-24 16:13:08 -05002729 cur_end = min(block_end, extent_map_end(em) - 1);
2730 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2731 block_off_end = block_off_start + blocksize;
2732 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2733
2734 if (!PageUptodate(page) && isnew &&
2735 (block_off_end > to || block_off_start < from)) {
2736 void *kaddr;
2737
2738 kaddr = kmap_atomic(page, KM_USER0);
2739 if (block_off_end > to)
2740 memset(kaddr + to, 0, block_off_end - to);
2741 if (block_off_start < from)
2742 memset(kaddr + block_off_start, 0,
2743 from - block_off_start);
2744 flush_dcache_page(page);
2745 kunmap_atomic(kaddr, KM_USER0);
2746 }
2747 if ((em->block_start != EXTENT_MAP_HOLE &&
2748 em->block_start != EXTENT_MAP_INLINE) &&
2749 !isnew && !PageUptodate(page) &&
2750 (block_off_end > to || block_off_start < from) &&
2751 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002752 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002753 u64 sector;
2754 u64 extent_offset = block_start - em->start;
2755 size_t iosize;
2756 sector = (em->block_start + extent_offset) >> 9;
2757 iosize = (cur_end - block_start + blocksize) &
2758 ~((u64)blocksize - 1);
2759 /*
2760 * we've already got the extent locked, but we
2761 * need to split the state such that our end_bio
2762 * handler can clear the lock.
2763 */
2764 set_extent_bit(tree, block_start,
2765 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002766 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002767 ret = submit_extent_page(READ, tree, page,
2768 sector, iosize, page_offset, em->bdev,
2769 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002770 end_bio_extent_preparewrite, 0,
2771 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002772 iocount++;
2773 block_start = block_start + iosize;
2774 } else {
2775 set_extent_uptodate(tree, block_start, cur_end,
2776 GFP_NOFS);
2777 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2778 block_start = cur_end + 1;
2779 }
2780 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2781 free_extent_map(em);
2782 }
2783 if (iocount) {
2784 wait_extent_bit(tree, orig_block_start,
2785 block_end, EXTENT_LOCKED);
2786 }
2787 check_page_uptodate(tree, page);
2788err:
2789 /* FIXME, zero out newly allocated blocks on error */
2790 return err;
2791}
Chris Masond1310b22008-01-24 16:13:08 -05002792
2793/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002794 * a helper for releasepage, this tests for areas of the page that
2795 * are locked or under IO and drops the related state bits if it is safe
2796 * to drop the page.
2797 */
2798int try_release_extent_state(struct extent_map_tree *map,
2799 struct extent_io_tree *tree, struct page *page,
2800 gfp_t mask)
2801{
2802 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2803 u64 end = start + PAGE_CACHE_SIZE - 1;
2804 int ret = 1;
2805
Chris Mason211f90e2008-07-18 11:56:15 -04002806 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002807 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002808 ret = 0;
2809 else {
2810 if ((mask & GFP_NOFS) == GFP_NOFS)
2811 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002812 /*
2813 * at this point we can safely clear everything except the
2814 * locked bit and the nodatasum bit
2815 */
2816 clear_extent_bit(tree, start, end,
2817 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2818 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002819 }
2820 return ret;
2821}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002822
2823/*
Chris Masond1310b22008-01-24 16:13:08 -05002824 * a helper for releasepage. As long as there are no locked extents
2825 * in the range corresponding to the page, both state records and extent
2826 * map records are removed
2827 */
2828int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002829 struct extent_io_tree *tree, struct page *page,
2830 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002831{
2832 struct extent_map *em;
2833 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2834 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002835
Chris Mason70dec802008-01-29 09:59:12 -05002836 if ((mask & __GFP_WAIT) &&
2837 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002838 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002839 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002840 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002841 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002842 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002843 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002844 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002845 break;
2846 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002847 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2848 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002849 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002850 free_extent_map(em);
2851 break;
2852 }
2853 if (!test_range_bit(tree, em->start,
2854 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002855 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002856 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002857 remove_extent_mapping(map, em);
2858 /* once for the rb tree */
2859 free_extent_map(em);
2860 }
2861 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002862 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002863
2864 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002865 free_extent_map(em);
2866 }
Chris Masond1310b22008-01-24 16:13:08 -05002867 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002868 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002869}
Chris Masond1310b22008-01-24 16:13:08 -05002870
2871sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2872 get_extent_t *get_extent)
2873{
2874 struct inode *inode = mapping->host;
2875 u64 start = iblock << inode->i_blkbits;
2876 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002877 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002878 struct extent_map *em;
2879
Yan Zhengd899e052008-10-30 14:25:28 -04002880 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2881 GFP_NOFS);
2882 em = get_extent(inode, NULL, 0, start, blksize, 0);
2883 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2884 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002885 if (!em || IS_ERR(em))
2886 return 0;
2887
Yan Zhengd899e052008-10-30 14:25:28 -04002888 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002889 goto out;
2890
2891 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002892out:
2893 free_extent_map(em);
2894 return sector;
2895}
2896
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002897int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2898 __u64 start, __u64 len, get_extent_t *get_extent)
2899{
2900 int ret;
2901 u64 off = start;
2902 u64 max = start + len;
2903 u32 flags = 0;
2904 u64 disko = 0;
2905 struct extent_map *em = NULL;
2906 int end = 0;
2907 u64 em_start = 0, em_len = 0;
2908 unsigned long emflags;
2909 ret = 0;
2910
2911 if (len == 0)
2912 return -EINVAL;
2913
2914 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2915 GFP_NOFS);
2916 em = get_extent(inode, NULL, 0, off, max - off, 0);
2917 if (!em)
2918 goto out;
2919 if (IS_ERR(em)) {
2920 ret = PTR_ERR(em);
2921 goto out;
2922 }
2923 while (!end) {
2924 off = em->start + em->len;
2925 if (off >= max)
2926 end = 1;
2927
2928 em_start = em->start;
2929 em_len = em->len;
2930
2931 disko = 0;
2932 flags = 0;
2933
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002934 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002935 end = 1;
2936 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002937 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002938 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002939 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002940 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2941 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002942 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002943 flags |= (FIEMAP_EXTENT_DELALLOC |
2944 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002945 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002946 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002947 }
2948 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2949 flags |= FIEMAP_EXTENT_ENCODED;
2950
2951 emflags = em->flags;
2952 free_extent_map(em);
2953 em = NULL;
2954
2955 if (!end) {
2956 em = get_extent(inode, NULL, 0, off, max - off, 0);
2957 if (!em)
2958 goto out;
2959 if (IS_ERR(em)) {
2960 ret = PTR_ERR(em);
2961 goto out;
2962 }
2963 emflags = em->flags;
2964 }
2965 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2966 flags |= FIEMAP_EXTENT_LAST;
2967 end = 1;
2968 }
2969
2970 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2971 em_len, flags);
2972 if (ret)
2973 goto out_free;
2974 }
2975out_free:
2976 free_extent_map(em);
2977out:
2978 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2979 GFP_NOFS);
2980 return ret;
2981}
2982
Chris Masond1310b22008-01-24 16:13:08 -05002983static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2984 unsigned long i)
2985{
2986 struct page *p;
2987 struct address_space *mapping;
2988
2989 if (i == 0)
2990 return eb->first_page;
2991 i += eb->start >> PAGE_CACHE_SHIFT;
2992 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002993 if (!mapping)
2994 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002995
2996 /*
2997 * extent_buffer_page is only called after pinning the page
2998 * by increasing the reference count. So we know the page must
2999 * be in the radix tree.
3000 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003001 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003002 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003003 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003004
Chris Masond1310b22008-01-24 16:13:08 -05003005 return p;
3006}
3007
Chris Mason6af118ce2008-07-22 11:18:07 -04003008static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003009{
Chris Mason6af118ce2008-07-22 11:18:07 -04003010 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3011 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003012}
3013
Chris Masond1310b22008-01-24 16:13:08 -05003014static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3015 u64 start,
3016 unsigned long len,
3017 gfp_t mask)
3018{
3019 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003020#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003021 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003022#endif
Chris Masond1310b22008-01-24 16:13:08 -05003023
Chris Masond1310b22008-01-24 16:13:08 -05003024 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003025 eb->start = start;
3026 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003027 spin_lock_init(&eb->lock);
3028 init_waitqueue_head(&eb->lock_wq);
3029
Chris Mason39351272009-02-04 09:24:05 -05003030#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003031 spin_lock_irqsave(&leak_lock, flags);
3032 list_add(&eb->leak_list, &buffers);
3033 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003034#endif
Chris Masond1310b22008-01-24 16:13:08 -05003035 atomic_set(&eb->refs, 1);
3036
3037 return eb;
3038}
3039
3040static void __free_extent_buffer(struct extent_buffer *eb)
3041{
Chris Mason39351272009-02-04 09:24:05 -05003042#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003043 unsigned long flags;
3044 spin_lock_irqsave(&leak_lock, flags);
3045 list_del(&eb->leak_list);
3046 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003047#endif
Chris Masond1310b22008-01-24 16:13:08 -05003048 kmem_cache_free(extent_buffer_cache, eb);
3049}
3050
3051struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3052 u64 start, unsigned long len,
3053 struct page *page0,
3054 gfp_t mask)
3055{
3056 unsigned long num_pages = num_extent_pages(start, len);
3057 unsigned long i;
3058 unsigned long index = start >> PAGE_CACHE_SHIFT;
3059 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003060 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003061 struct page *p;
3062 struct address_space *mapping = tree->mapping;
3063 int uptodate = 1;
3064
Chris Mason6af118ce2008-07-22 11:18:07 -04003065 spin_lock(&tree->buffer_lock);
3066 eb = buffer_search(tree, start);
3067 if (eb) {
3068 atomic_inc(&eb->refs);
3069 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003070 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003071 return eb;
3072 }
3073 spin_unlock(&tree->buffer_lock);
3074
Chris Masond1310b22008-01-24 16:13:08 -05003075 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003076 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003077 return NULL;
3078
Chris Masond1310b22008-01-24 16:13:08 -05003079 if (page0) {
3080 eb->first_page = page0;
3081 i = 1;
3082 index++;
3083 page_cache_get(page0);
3084 mark_page_accessed(page0);
3085 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003086 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003087 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003088 } else {
3089 i = 0;
3090 }
3091 for (; i < num_pages; i++, index++) {
3092 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3093 if (!p) {
3094 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003095 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003096 }
3097 set_page_extent_mapped(p);
3098 mark_page_accessed(p);
3099 if (i == 0) {
3100 eb->first_page = p;
3101 set_page_extent_head(p, len);
3102 } else {
3103 set_page_private(p, EXTENT_PAGE_PRIVATE);
3104 }
3105 if (!PageUptodate(p))
3106 uptodate = 0;
3107 unlock_page(p);
3108 }
3109 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003110 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003111
Chris Mason6af118ce2008-07-22 11:18:07 -04003112 spin_lock(&tree->buffer_lock);
3113 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3114 if (exists) {
3115 /* add one reference for the caller */
3116 atomic_inc(&exists->refs);
3117 spin_unlock(&tree->buffer_lock);
3118 goto free_eb;
3119 }
3120 spin_unlock(&tree->buffer_lock);
3121
3122 /* add one reference for the tree */
3123 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003124 return eb;
3125
Chris Mason6af118ce2008-07-22 11:18:07 -04003126free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003127 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003128 return exists;
3129 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003130 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003131 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003132 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003133 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003134}
Chris Masond1310b22008-01-24 16:13:08 -05003135
3136struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3137 u64 start, unsigned long len,
3138 gfp_t mask)
3139{
Chris Masond1310b22008-01-24 16:13:08 -05003140 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003141
Chris Mason6af118ce2008-07-22 11:18:07 -04003142 spin_lock(&tree->buffer_lock);
3143 eb = buffer_search(tree, start);
3144 if (eb)
3145 atomic_inc(&eb->refs);
3146 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003147
Josef Bacik0f9dd462008-09-23 13:14:11 -04003148 if (eb)
3149 mark_page_accessed(eb->first_page);
3150
Chris Masond1310b22008-01-24 16:13:08 -05003151 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003152}
Chris Masond1310b22008-01-24 16:13:08 -05003153
3154void free_extent_buffer(struct extent_buffer *eb)
3155{
Chris Masond1310b22008-01-24 16:13:08 -05003156 if (!eb)
3157 return;
3158
3159 if (!atomic_dec_and_test(&eb->refs))
3160 return;
3161
Chris Mason6af118ce2008-07-22 11:18:07 -04003162 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003163}
Chris Masond1310b22008-01-24 16:13:08 -05003164
3165int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3166 struct extent_buffer *eb)
3167{
Chris Masond1310b22008-01-24 16:13:08 -05003168 unsigned long i;
3169 unsigned long num_pages;
3170 struct page *page;
3171
Chris Masond1310b22008-01-24 16:13:08 -05003172 num_pages = num_extent_pages(eb->start, eb->len);
3173
3174 for (i = 0; i < num_pages; i++) {
3175 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003176 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003177 continue;
3178
Chris Masona61e6f22008-07-22 11:18:08 -04003179 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003180 if (i == 0)
3181 set_page_extent_head(page, eb->len);
3182 else
3183 set_page_private(page, EXTENT_PAGE_PRIVATE);
3184
Chris Masond1310b22008-01-24 16:13:08 -05003185 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003186 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003187 if (!PageDirty(page)) {
3188 radix_tree_tag_clear(&page->mapping->page_tree,
3189 page_index(page),
3190 PAGECACHE_TAG_DIRTY);
3191 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003192 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003193 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003194 }
3195 return 0;
3196}
Chris Masond1310b22008-01-24 16:13:08 -05003197
3198int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3199 struct extent_buffer *eb)
3200{
3201 return wait_on_extent_writeback(tree, eb->start,
3202 eb->start + eb->len - 1);
3203}
Chris Masond1310b22008-01-24 16:13:08 -05003204
3205int set_extent_buffer_dirty(struct extent_io_tree *tree,
3206 struct extent_buffer *eb)
3207{
3208 unsigned long i;
3209 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003210 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003211
Chris Masonb9473432009-03-13 11:00:37 -04003212 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003213 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003214 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003215 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003216 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003217}
Chris Masond1310b22008-01-24 16:13:08 -05003218
Chris Mason1259ab72008-05-12 13:39:03 -04003219int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3220 struct extent_buffer *eb)
3221{
3222 unsigned long i;
3223 struct page *page;
3224 unsigned long num_pages;
3225
3226 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003227 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003228
3229 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3230 GFP_NOFS);
3231 for (i = 0; i < num_pages; i++) {
3232 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003233 if (page)
3234 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003235 }
3236 return 0;
3237}
3238
Chris Masond1310b22008-01-24 16:13:08 -05003239int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3240 struct extent_buffer *eb)
3241{
3242 unsigned long i;
3243 struct page *page;
3244 unsigned long num_pages;
3245
3246 num_pages = num_extent_pages(eb->start, eb->len);
3247
3248 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3249 GFP_NOFS);
3250 for (i = 0; i < num_pages; i++) {
3251 page = extent_buffer_page(eb, i);
3252 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3253 ((i == num_pages - 1) &&
3254 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3255 check_page_uptodate(tree, page);
3256 continue;
3257 }
3258 SetPageUptodate(page);
3259 }
3260 return 0;
3261}
Chris Masond1310b22008-01-24 16:13:08 -05003262
Chris Masonce9adaa2008-04-09 16:28:12 -04003263int extent_range_uptodate(struct extent_io_tree *tree,
3264 u64 start, u64 end)
3265{
3266 struct page *page;
3267 int ret;
3268 int pg_uptodate = 1;
3269 int uptodate;
3270 unsigned long index;
3271
Chris Mason9655d292009-09-02 15:22:30 -04003272 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003273 if (ret)
3274 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003275 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003276 index = start >> PAGE_CACHE_SHIFT;
3277 page = find_get_page(tree->mapping, index);
3278 uptodate = PageUptodate(page);
3279 page_cache_release(page);
3280 if (!uptodate) {
3281 pg_uptodate = 0;
3282 break;
3283 }
3284 start += PAGE_CACHE_SIZE;
3285 }
3286 return pg_uptodate;
3287}
3288
Chris Masond1310b22008-01-24 16:13:08 -05003289int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003290 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003291{
Chris Mason728131d2008-04-09 16:28:12 -04003292 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003293 unsigned long num_pages;
3294 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003295 struct page *page;
3296 int pg_uptodate = 1;
3297
Chris Masonb4ce94d2009-02-04 09:25:08 -05003298 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003299 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003300
Chris Mason42352982008-04-28 16:40:52 -04003301 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003302 EXTENT_UPTODATE, 1, NULL);
Chris Mason42352982008-04-28 16:40:52 -04003303 if (ret)
3304 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003305
3306 num_pages = num_extent_pages(eb->start, eb->len);
3307 for (i = 0; i < num_pages; i++) {
3308 page = extent_buffer_page(eb, i);
3309 if (!PageUptodate(page)) {
3310 pg_uptodate = 0;
3311 break;
3312 }
3313 }
Chris Mason42352982008-04-28 16:40:52 -04003314 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003315}
Chris Masond1310b22008-01-24 16:13:08 -05003316
3317int read_extent_buffer_pages(struct extent_io_tree *tree,
3318 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003319 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003320 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003321{
3322 unsigned long i;
3323 unsigned long start_i;
3324 struct page *page;
3325 int err;
3326 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003327 int locked_pages = 0;
3328 int all_uptodate = 1;
3329 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003330 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003331 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003332 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003333
Chris Masonb4ce94d2009-02-04 09:25:08 -05003334 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003335 return 0;
3336
Chris Masonce9adaa2008-04-09 16:28:12 -04003337 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003338 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003339 return 0;
3340 }
3341
3342 if (start) {
3343 WARN_ON(start < eb->start);
3344 start_i = (start >> PAGE_CACHE_SHIFT) -
3345 (eb->start >> PAGE_CACHE_SHIFT);
3346 } else {
3347 start_i = 0;
3348 }
3349
3350 num_pages = num_extent_pages(eb->start, eb->len);
3351 for (i = start_i; i < num_pages; i++) {
3352 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003353 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003354 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003355 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003356 } else {
3357 lock_page(page);
3358 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003359 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003360 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003361 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003362 }
3363 if (all_uptodate) {
3364 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003365 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003366 goto unlock_exit;
3367 }
3368
3369 for (i = start_i; i < num_pages; i++) {
3370 page = extent_buffer_page(eb, i);
3371 if (inc_all_pages)
3372 page_cache_get(page);
3373 if (!PageUptodate(page)) {
3374 if (start_i == 0)
3375 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003376 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003377 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003378 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003379 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003380 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003381 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003382 } else {
3383 unlock_page(page);
3384 }
3385 }
3386
Chris Masona86c12c2008-02-07 10:50:54 -05003387 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003388 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003389
Chris Masond3977122009-01-05 21:25:51 -05003390 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003391 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003392
Chris Masond1310b22008-01-24 16:13:08 -05003393 for (i = start_i; i < num_pages; i++) {
3394 page = extent_buffer_page(eb, i);
3395 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003396 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003397 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003398 }
Chris Masond3977122009-01-05 21:25:51 -05003399
Chris Masond1310b22008-01-24 16:13:08 -05003400 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003401 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003402 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003403
3404unlock_exit:
3405 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003406 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003407 page = extent_buffer_page(eb, i);
3408 i++;
3409 unlock_page(page);
3410 locked_pages--;
3411 }
3412 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003413}
Chris Masond1310b22008-01-24 16:13:08 -05003414
3415void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3416 unsigned long start,
3417 unsigned long len)
3418{
3419 size_t cur;
3420 size_t offset;
3421 struct page *page;
3422 char *kaddr;
3423 char *dst = (char *)dstv;
3424 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3425 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003426
3427 WARN_ON(start > eb->len);
3428 WARN_ON(start + len > eb->start + eb->len);
3429
3430 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3431
Chris Masond3977122009-01-05 21:25:51 -05003432 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003433 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003434
3435 cur = min(len, (PAGE_CACHE_SIZE - offset));
3436 kaddr = kmap_atomic(page, KM_USER1);
3437 memcpy(dst, kaddr + offset, cur);
3438 kunmap_atomic(kaddr, KM_USER1);
3439
3440 dst += cur;
3441 len -= cur;
3442 offset = 0;
3443 i++;
3444 }
3445}
Chris Masond1310b22008-01-24 16:13:08 -05003446
3447int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3448 unsigned long min_len, char **token, char **map,
3449 unsigned long *map_start,
3450 unsigned long *map_len, int km)
3451{
3452 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3453 char *kaddr;
3454 struct page *p;
3455 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3456 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3457 unsigned long end_i = (start_offset + start + min_len - 1) >>
3458 PAGE_CACHE_SHIFT;
3459
3460 if (i != end_i)
3461 return -EINVAL;
3462
3463 if (i == 0) {
3464 offset = start_offset;
3465 *map_start = 0;
3466 } else {
3467 offset = 0;
3468 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3469 }
Chris Masond3977122009-01-05 21:25:51 -05003470
Chris Masond1310b22008-01-24 16:13:08 -05003471 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003472 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3473 "wanted %lu %lu\n", (unsigned long long)eb->start,
3474 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003475 WARN_ON(1);
3476 }
3477
3478 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003479 kaddr = kmap_atomic(p, km);
3480 *token = kaddr;
3481 *map = kaddr + offset;
3482 *map_len = PAGE_CACHE_SIZE - offset;
3483 return 0;
3484}
Chris Masond1310b22008-01-24 16:13:08 -05003485
3486int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3487 unsigned long min_len,
3488 char **token, char **map,
3489 unsigned long *map_start,
3490 unsigned long *map_len, int km)
3491{
3492 int err;
3493 int save = 0;
3494 if (eb->map_token) {
3495 unmap_extent_buffer(eb, eb->map_token, km);
3496 eb->map_token = NULL;
3497 save = 1;
3498 }
3499 err = map_private_extent_buffer(eb, start, min_len, token, map,
3500 map_start, map_len, km);
3501 if (!err && save) {
3502 eb->map_token = *token;
3503 eb->kaddr = *map;
3504 eb->map_start = *map_start;
3505 eb->map_len = *map_len;
3506 }
3507 return err;
3508}
Chris Masond1310b22008-01-24 16:13:08 -05003509
3510void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3511{
3512 kunmap_atomic(token, km);
3513}
Chris Masond1310b22008-01-24 16:13:08 -05003514
3515int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3516 unsigned long start,
3517 unsigned long len)
3518{
3519 size_t cur;
3520 size_t offset;
3521 struct page *page;
3522 char *kaddr;
3523 char *ptr = (char *)ptrv;
3524 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3525 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3526 int ret = 0;
3527
3528 WARN_ON(start > eb->len);
3529 WARN_ON(start + len > eb->start + eb->len);
3530
3531 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3532
Chris Masond3977122009-01-05 21:25:51 -05003533 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003534 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003535
3536 cur = min(len, (PAGE_CACHE_SIZE - offset));
3537
3538 kaddr = kmap_atomic(page, KM_USER0);
3539 ret = memcmp(ptr, kaddr + offset, cur);
3540 kunmap_atomic(kaddr, KM_USER0);
3541 if (ret)
3542 break;
3543
3544 ptr += cur;
3545 len -= cur;
3546 offset = 0;
3547 i++;
3548 }
3549 return ret;
3550}
Chris Masond1310b22008-01-24 16:13:08 -05003551
3552void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3553 unsigned long start, unsigned long len)
3554{
3555 size_t cur;
3556 size_t offset;
3557 struct page *page;
3558 char *kaddr;
3559 char *src = (char *)srcv;
3560 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3561 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3562
3563 WARN_ON(start > eb->len);
3564 WARN_ON(start + len > eb->start + eb->len);
3565
3566 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3567
Chris Masond3977122009-01-05 21:25:51 -05003568 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003569 page = extent_buffer_page(eb, i);
3570 WARN_ON(!PageUptodate(page));
3571
3572 cur = min(len, PAGE_CACHE_SIZE - offset);
3573 kaddr = kmap_atomic(page, KM_USER1);
3574 memcpy(kaddr + offset, src, cur);
3575 kunmap_atomic(kaddr, KM_USER1);
3576
3577 src += cur;
3578 len -= cur;
3579 offset = 0;
3580 i++;
3581 }
3582}
Chris Masond1310b22008-01-24 16:13:08 -05003583
3584void memset_extent_buffer(struct extent_buffer *eb, char c,
3585 unsigned long start, unsigned long len)
3586{
3587 size_t cur;
3588 size_t offset;
3589 struct page *page;
3590 char *kaddr;
3591 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3592 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3593
3594 WARN_ON(start > eb->len);
3595 WARN_ON(start + len > eb->start + eb->len);
3596
3597 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3598
Chris Masond3977122009-01-05 21:25:51 -05003599 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003600 page = extent_buffer_page(eb, i);
3601 WARN_ON(!PageUptodate(page));
3602
3603 cur = min(len, PAGE_CACHE_SIZE - offset);
3604 kaddr = kmap_atomic(page, KM_USER0);
3605 memset(kaddr + offset, c, cur);
3606 kunmap_atomic(kaddr, KM_USER0);
3607
3608 len -= cur;
3609 offset = 0;
3610 i++;
3611 }
3612}
Chris Masond1310b22008-01-24 16:13:08 -05003613
3614void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3615 unsigned long dst_offset, unsigned long src_offset,
3616 unsigned long len)
3617{
3618 u64 dst_len = dst->len;
3619 size_t cur;
3620 size_t offset;
3621 struct page *page;
3622 char *kaddr;
3623 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3624 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3625
3626 WARN_ON(src->len != dst_len);
3627
3628 offset = (start_offset + dst_offset) &
3629 ((unsigned long)PAGE_CACHE_SIZE - 1);
3630
Chris Masond3977122009-01-05 21:25:51 -05003631 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003632 page = extent_buffer_page(dst, i);
3633 WARN_ON(!PageUptodate(page));
3634
3635 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3636
3637 kaddr = kmap_atomic(page, KM_USER0);
3638 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3639 kunmap_atomic(kaddr, KM_USER0);
3640
3641 src_offset += cur;
3642 len -= cur;
3643 offset = 0;
3644 i++;
3645 }
3646}
Chris Masond1310b22008-01-24 16:13:08 -05003647
3648static void move_pages(struct page *dst_page, struct page *src_page,
3649 unsigned long dst_off, unsigned long src_off,
3650 unsigned long len)
3651{
3652 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3653 if (dst_page == src_page) {
3654 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3655 } else {
3656 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3657 char *p = dst_kaddr + dst_off + len;
3658 char *s = src_kaddr + src_off + len;
3659
3660 while (len--)
3661 *--p = *--s;
3662
3663 kunmap_atomic(src_kaddr, KM_USER1);
3664 }
3665 kunmap_atomic(dst_kaddr, KM_USER0);
3666}
3667
3668static void copy_pages(struct page *dst_page, struct page *src_page,
3669 unsigned long dst_off, unsigned long src_off,
3670 unsigned long len)
3671{
3672 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3673 char *src_kaddr;
3674
3675 if (dst_page != src_page)
3676 src_kaddr = kmap_atomic(src_page, KM_USER1);
3677 else
3678 src_kaddr = dst_kaddr;
3679
3680 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3681 kunmap_atomic(dst_kaddr, KM_USER0);
3682 if (dst_page != src_page)
3683 kunmap_atomic(src_kaddr, KM_USER1);
3684}
3685
3686void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3687 unsigned long src_offset, unsigned long len)
3688{
3689 size_t cur;
3690 size_t dst_off_in_page;
3691 size_t src_off_in_page;
3692 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3693 unsigned long dst_i;
3694 unsigned long src_i;
3695
3696 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003697 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3698 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003699 BUG_ON(1);
3700 }
3701 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003702 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3703 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003704 BUG_ON(1);
3705 }
3706
Chris Masond3977122009-01-05 21:25:51 -05003707 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003708 dst_off_in_page = (start_offset + dst_offset) &
3709 ((unsigned long)PAGE_CACHE_SIZE - 1);
3710 src_off_in_page = (start_offset + src_offset) &
3711 ((unsigned long)PAGE_CACHE_SIZE - 1);
3712
3713 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3714 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3715
3716 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3717 src_off_in_page));
3718 cur = min_t(unsigned long, cur,
3719 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3720
3721 copy_pages(extent_buffer_page(dst, dst_i),
3722 extent_buffer_page(dst, src_i),
3723 dst_off_in_page, src_off_in_page, cur);
3724
3725 src_offset += cur;
3726 dst_offset += cur;
3727 len -= cur;
3728 }
3729}
Chris Masond1310b22008-01-24 16:13:08 -05003730
3731void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3732 unsigned long src_offset, unsigned long len)
3733{
3734 size_t cur;
3735 size_t dst_off_in_page;
3736 size_t src_off_in_page;
3737 unsigned long dst_end = dst_offset + len - 1;
3738 unsigned long src_end = src_offset + len - 1;
3739 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3740 unsigned long dst_i;
3741 unsigned long src_i;
3742
3743 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003744 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3745 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003746 BUG_ON(1);
3747 }
3748 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003749 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3750 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003751 BUG_ON(1);
3752 }
3753 if (dst_offset < src_offset) {
3754 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3755 return;
3756 }
Chris Masond3977122009-01-05 21:25:51 -05003757 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003758 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3759 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3760
3761 dst_off_in_page = (start_offset + dst_end) &
3762 ((unsigned long)PAGE_CACHE_SIZE - 1);
3763 src_off_in_page = (start_offset + src_end) &
3764 ((unsigned long)PAGE_CACHE_SIZE - 1);
3765
3766 cur = min_t(unsigned long, len, src_off_in_page + 1);
3767 cur = min(cur, dst_off_in_page + 1);
3768 move_pages(extent_buffer_page(dst, dst_i),
3769 extent_buffer_page(dst, src_i),
3770 dst_off_in_page - cur + 1,
3771 src_off_in_page - cur + 1, cur);
3772
3773 dst_end -= cur;
3774 src_end -= cur;
3775 len -= cur;
3776 }
3777}
Chris Mason6af118ce2008-07-22 11:18:07 -04003778
3779int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3780{
3781 u64 start = page_offset(page);
3782 struct extent_buffer *eb;
3783 int ret = 1;
3784 unsigned long i;
3785 unsigned long num_pages;
3786
3787 spin_lock(&tree->buffer_lock);
3788 eb = buffer_search(tree, start);
3789 if (!eb)
3790 goto out;
3791
3792 if (atomic_read(&eb->refs) > 1) {
3793 ret = 0;
3794 goto out;
3795 }
Chris Masonb9473432009-03-13 11:00:37 -04003796 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3797 ret = 0;
3798 goto out;
3799 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003800 /* at this point we can safely release the extent buffer */
3801 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003802 for (i = 0; i < num_pages; i++)
3803 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003804 rb_erase(&eb->rb_node, &tree->buffer);
3805 __free_extent_buffer(eb);
3806out:
3807 spin_unlock(&tree->buffer_lock);
3808 return ret;
3809}