blob: 7c70613eb72c10547ac94fde0b9ea600ef1f2cc6 [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,
474 int bits, int wake, int delete, gfp_t mask)
475{
476 struct extent_state *state;
477 struct extent_state *prealloc = NULL;
478 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400479 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500480 int err;
481 int set = 0;
482
483again:
484 if (!prealloc && (mask & __GFP_WAIT)) {
485 prealloc = alloc_extent_state(mask);
486 if (!prealloc)
487 return -ENOMEM;
488 }
489
Chris Masoncad321a2008-12-17 14:51:42 -0500490 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500491 /*
492 * this search will find the extents that end after
493 * our range starts
494 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500495 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500496 if (!node)
497 goto out;
498 state = rb_entry(node, struct extent_state, rb_node);
499 if (state->start > end)
500 goto out;
501 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400502 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500503
504 /*
505 * | ---- desired range ---- |
506 * | state | or
507 * | ------------- state -------------- |
508 *
509 * We need to split the extent we found, and may flip
510 * bits on second half.
511 *
512 * If the extent we found extends past our range, we
513 * just split and search again. It'll get split again
514 * the next time though.
515 *
516 * If the extent we found is inside our range, we clear
517 * the desired bit on it.
518 */
519
520 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500521 if (!prealloc)
522 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500523 err = split_state(tree, state, prealloc, start);
524 BUG_ON(err == -EEXIST);
525 prealloc = NULL;
526 if (err)
527 goto out;
528 if (state->end <= end) {
Chris Masond1310b22008-01-24 16:13:08 -0500529 set |= clear_state_bit(tree, state, bits,
530 wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400531 if (last_end == (u64)-1)
532 goto out;
533 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500534 } else {
535 start = state->start;
536 }
537 goto search_again;
538 }
539 /*
540 * | ---- desired range ---- |
541 * | state |
542 * We need to split the extent, and clear the bit
543 * on the first half
544 */
545 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500546 if (!prealloc)
547 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500548 err = split_state(tree, state, prealloc, end + 1);
549 BUG_ON(err == -EEXIST);
550
551 if (wake)
552 wake_up(&state->wq);
553 set |= clear_state_bit(tree, prealloc, bits,
554 wake, delete);
555 prealloc = NULL;
556 goto out;
557 }
558
Chris Masond1310b22008-01-24 16:13:08 -0500559 set |= clear_state_bit(tree, state, bits, wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400560 if (last_end == (u64)-1)
561 goto out;
562 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500563 goto search_again;
564
565out:
Chris Masoncad321a2008-12-17 14:51:42 -0500566 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500567 if (prealloc)
568 free_extent_state(prealloc);
569
570 return set;
571
572search_again:
573 if (start > end)
574 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500575 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500576 if (mask & __GFP_WAIT)
577 cond_resched();
578 goto again;
579}
Chris Masond1310b22008-01-24 16:13:08 -0500580
581static int wait_on_state(struct extent_io_tree *tree,
582 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500583 __releases(tree->lock)
584 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500585{
586 DEFINE_WAIT(wait);
587 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500588 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500589 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500590 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500591 finish_wait(&state->wq, &wait);
592 return 0;
593}
594
595/*
596 * waits for one or more bits to clear on a range in the state tree.
597 * The range [start, end] is inclusive.
598 * The tree lock is taken by this function
599 */
600int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
601{
602 struct extent_state *state;
603 struct rb_node *node;
604
Chris Masoncad321a2008-12-17 14:51:42 -0500605 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500606again:
607 while (1) {
608 /*
609 * this search will find all the extents that end after
610 * our range starts
611 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500612 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500613 if (!node)
614 break;
615
616 state = rb_entry(node, struct extent_state, rb_node);
617
618 if (state->start > end)
619 goto out;
620
621 if (state->state & bits) {
622 start = state->start;
623 atomic_inc(&state->refs);
624 wait_on_state(tree, state);
625 free_extent_state(state);
626 goto again;
627 }
628 start = state->end + 1;
629
630 if (start > end)
631 break;
632
633 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500634 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500635 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500636 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500637 }
638 }
639out:
Chris Masoncad321a2008-12-17 14:51:42 -0500640 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500641 return 0;
642}
Chris Masond1310b22008-01-24 16:13:08 -0500643
644static void set_state_bits(struct extent_io_tree *tree,
645 struct extent_state *state,
646 int bits)
647{
648 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
649 u64 range = state->end - state->start + 1;
650 tree->dirty_bytes += range;
651 }
Chris Mason291d6732008-01-29 15:55:23 -0500652 set_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500653 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500654}
655
656/*
Chris Mason1edbb732009-09-02 13:24:36 -0400657 * set some bits on a range in the tree. This may require allocations or
658 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500659 *
Chris Mason1edbb732009-09-02 13:24:36 -0400660 * If any of the exclusive bits are set, this will fail with -EEXIST if some
661 * part of the range already has the desired bits set. The start of the
662 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500663 *
Chris Mason1edbb732009-09-02 13:24:36 -0400664 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500665 */
Chris Mason1edbb732009-09-02 13:24:36 -0400666
Chris Masond3977122009-01-05 21:25:51 -0500667static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason1edbb732009-09-02 13:24:36 -0400668 int bits, int exclusive_bits, u64 *failed_start,
Chris Masond3977122009-01-05 21:25:51 -0500669 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500670{
671 struct extent_state *state;
672 struct extent_state *prealloc = NULL;
673 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500674 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500675 u64 last_start;
676 u64 last_end;
677again:
678 if (!prealloc && (mask & __GFP_WAIT)) {
679 prealloc = alloc_extent_state(mask);
680 if (!prealloc)
681 return -ENOMEM;
682 }
683
Chris Masoncad321a2008-12-17 14:51:42 -0500684 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500685 /*
686 * this search will find all the extents that end after
687 * our range starts.
688 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500689 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500690 if (!node) {
691 err = insert_state(tree, prealloc, start, end, bits);
692 prealloc = NULL;
693 BUG_ON(err == -EEXIST);
694 goto out;
695 }
Chris Masond1310b22008-01-24 16:13:08 -0500696 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400697hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500698 last_start = state->start;
699 last_end = state->end;
700
701 /*
702 * | ---- desired range ---- |
703 * | state |
704 *
705 * Just lock what we found and keep going
706 */
707 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400708 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400709 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500710 *failed_start = state->start;
711 err = -EEXIST;
712 goto out;
713 }
714 set_state_bits(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500715 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400716 if (last_end == (u64)-1)
717 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400718
Yan Zheng5c939df2009-05-27 09:16:03 -0400719 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400720 if (start < end && prealloc && !need_resched()) {
721 next_node = rb_next(node);
722 if (next_node) {
723 state = rb_entry(next_node, struct extent_state,
724 rb_node);
725 if (state->start == start)
726 goto hit_next;
727 }
728 }
Chris Masond1310b22008-01-24 16:13:08 -0500729 goto search_again;
730 }
731
732 /*
733 * | ---- desired range ---- |
734 * | state |
735 * or
736 * | ------------- state -------------- |
737 *
738 * We need to split the extent we found, and may flip bits on
739 * second half.
740 *
741 * If the extent we found extends past our
742 * range, we just split and search again. It'll get split
743 * again the next time though.
744 *
745 * If the extent we found is inside our range, we set the
746 * desired bit on it.
747 */
748 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400749 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500750 *failed_start = start;
751 err = -EEXIST;
752 goto out;
753 }
754 err = split_state(tree, state, prealloc, start);
755 BUG_ON(err == -EEXIST);
756 prealloc = NULL;
757 if (err)
758 goto out;
759 if (state->end <= end) {
760 set_state_bits(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500761 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400762 if (last_end == (u64)-1)
763 goto out;
764 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500765 } else {
766 start = state->start;
767 }
768 goto search_again;
769 }
770 /*
771 * | ---- desired range ---- |
772 * | state | or | state |
773 *
774 * There's a hole, we need to insert something in it and
775 * ignore the extent we found.
776 */
777 if (state->start > start) {
778 u64 this_end;
779 if (end < last_start)
780 this_end = end;
781 else
Chris Masond3977122009-01-05 21:25:51 -0500782 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500783 err = insert_state(tree, prealloc, start, this_end,
784 bits);
785 prealloc = NULL;
786 BUG_ON(err == -EEXIST);
787 if (err)
788 goto out;
789 start = this_end + 1;
790 goto search_again;
791 }
792 /*
793 * | ---- desired range ---- |
794 * | state |
795 * We need to split the extent, and set the bit
796 * on the first half
797 */
798 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400799 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500800 *failed_start = start;
801 err = -EEXIST;
802 goto out;
803 }
804 err = split_state(tree, state, prealloc, end + 1);
805 BUG_ON(err == -EEXIST);
806
807 set_state_bits(tree, prealloc, bits);
808 merge_state(tree, prealloc);
809 prealloc = NULL;
810 goto out;
811 }
812
813 goto search_again;
814
815out:
Chris Masoncad321a2008-12-17 14:51:42 -0500816 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500817 if (prealloc)
818 free_extent_state(prealloc);
819
820 return err;
821
822search_again:
823 if (start > end)
824 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500825 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500826 if (mask & __GFP_WAIT)
827 cond_resched();
828 goto again;
829}
Chris Masond1310b22008-01-24 16:13:08 -0500830
831/* wrappers around set/clear extent bit */
832int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
833 gfp_t mask)
834{
835 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
836 mask);
837}
Chris Masond1310b22008-01-24 16:13:08 -0500838
Chris Masone6dcd2d2008-07-17 12:53:50 -0400839int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
840 gfp_t mask)
841{
842 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
843}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400844
Chris Masond1310b22008-01-24 16:13:08 -0500845int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
846 int bits, gfp_t mask)
847{
848 return set_extent_bit(tree, start, end, bits, 0, NULL,
849 mask);
850}
Chris Masond1310b22008-01-24 16:13:08 -0500851
852int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
853 int bits, gfp_t mask)
854{
855 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
856}
Chris Masond1310b22008-01-24 16:13:08 -0500857
858int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
859 gfp_t mask)
860{
861 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400862 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400863 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500864}
Chris Masond1310b22008-01-24 16:13:08 -0500865
866int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
867 gfp_t mask)
868{
869 return clear_extent_bit(tree, start, end,
870 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
871}
Chris Masond1310b22008-01-24 16:13:08 -0500872
Chris Masone6dcd2d2008-07-17 12:53:50 -0400873int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
874 gfp_t mask)
875{
876 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
877}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400878
Chris Masond1310b22008-01-24 16:13:08 -0500879int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
880 gfp_t mask)
881{
882 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
883 mask);
884}
Chris Masond1310b22008-01-24 16:13:08 -0500885
Christoph Hellwigb2950862008-12-02 09:54:17 -0500886static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500887 gfp_t mask)
888{
889 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
890}
Chris Masond1310b22008-01-24 16:13:08 -0500891
892int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
893 gfp_t mask)
894{
895 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
896 mask);
897}
Chris Masond1310b22008-01-24 16:13:08 -0500898
Chris Masond3977122009-01-05 21:25:51 -0500899static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
900 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500901{
902 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
903}
Chris Masond1310b22008-01-24 16:13:08 -0500904
Chris Masond1310b22008-01-24 16:13:08 -0500905int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
906{
907 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
908}
Chris Masond1310b22008-01-24 16:13:08 -0500909
Chris Masond352ac62008-09-29 15:18:18 -0400910/*
911 * either insert or lock state struct between start and end use mask to tell
912 * us if waiting is desired.
913 */
Chris Mason1edbb732009-09-02 13:24:36 -0400914int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
915 int bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500916{
917 int err;
918 u64 failed_start;
919 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400920 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
921 EXTENT_LOCKED, &failed_start, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500922 if (err == -EEXIST && (mask & __GFP_WAIT)) {
923 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
924 start = failed_start;
925 } else {
926 break;
927 }
928 WARN_ON(start > end);
929 }
930 return err;
931}
Chris Masond1310b22008-01-24 16:13:08 -0500932
Chris Mason1edbb732009-09-02 13:24:36 -0400933int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
934{
935 return lock_extent_bits(tree, start, end, 0, mask);
936}
937
Josef Bacik25179202008-10-29 14:49:05 -0400938int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
939 gfp_t mask)
940{
941 int err;
942 u64 failed_start;
943
944 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
945 &failed_start, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400946 if (err == -EEXIST) {
947 if (failed_start > start)
948 clear_extent_bit(tree, start, failed_start - 1,
949 EXTENT_LOCKED, 1, 0, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400950 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400951 }
Josef Bacik25179202008-10-29 14:49:05 -0400952 return 1;
953}
Josef Bacik25179202008-10-29 14:49:05 -0400954
Chris Masond1310b22008-01-24 16:13:08 -0500955int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
956 gfp_t mask)
957{
958 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
959}
Chris Masond1310b22008-01-24 16:13:08 -0500960
961/*
962 * helper function to set pages and extents in the tree dirty
963 */
964int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
965{
966 unsigned long index = start >> PAGE_CACHE_SHIFT;
967 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
968 struct page *page;
969
970 while (index <= end_index) {
971 page = find_get_page(tree->mapping, index);
972 BUG_ON(!page);
973 __set_page_dirty_nobuffers(page);
974 page_cache_release(page);
975 index++;
976 }
Chris Masond1310b22008-01-24 16:13:08 -0500977 return 0;
978}
Chris Masond1310b22008-01-24 16:13:08 -0500979
980/*
981 * helper function to set both pages and extents in the tree writeback
982 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500983static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -0500984{
985 unsigned long index = start >> PAGE_CACHE_SHIFT;
986 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
987 struct page *page;
988
989 while (index <= end_index) {
990 page = find_get_page(tree->mapping, index);
991 BUG_ON(!page);
992 set_page_writeback(page);
993 page_cache_release(page);
994 index++;
995 }
Chris Masond1310b22008-01-24 16:13:08 -0500996 return 0;
997}
Chris Masond1310b22008-01-24 16:13:08 -0500998
Chris Masond352ac62008-09-29 15:18:18 -0400999/*
1000 * find the first offset in the io tree with 'bits' set. zero is
1001 * returned if we find something, and *start_ret and *end_ret are
1002 * set to reflect the state struct that was found.
1003 *
1004 * If nothing was found, 1 is returned, < 0 on error
1005 */
Chris Masond1310b22008-01-24 16:13:08 -05001006int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1007 u64 *start_ret, u64 *end_ret, int bits)
1008{
1009 struct rb_node *node;
1010 struct extent_state *state;
1011 int ret = 1;
1012
Chris Masoncad321a2008-12-17 14:51:42 -05001013 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001014 /*
1015 * this search will find all the extents that end after
1016 * our range starts.
1017 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001018 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001019 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001020 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001021
Chris Masond3977122009-01-05 21:25:51 -05001022 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001023 state = rb_entry(node, struct extent_state, rb_node);
1024 if (state->end >= start && (state->state & bits)) {
1025 *start_ret = state->start;
1026 *end_ret = state->end;
1027 ret = 0;
1028 break;
1029 }
1030 node = rb_next(node);
1031 if (!node)
1032 break;
1033 }
1034out:
Chris Masoncad321a2008-12-17 14:51:42 -05001035 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001036 return ret;
1037}
Chris Masond1310b22008-01-24 16:13:08 -05001038
Chris Masond352ac62008-09-29 15:18:18 -04001039/* find the first state struct with 'bits' set after 'start', and
1040 * return it. tree->lock must be held. NULL will returned if
1041 * nothing was found after 'start'
1042 */
Chris Masond7fc6402008-02-18 12:12:38 -05001043struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1044 u64 start, int bits)
1045{
1046 struct rb_node *node;
1047 struct extent_state *state;
1048
1049 /*
1050 * this search will find all the extents that end after
1051 * our range starts.
1052 */
1053 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001054 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001055 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001056
Chris Masond3977122009-01-05 21:25:51 -05001057 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001058 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001059 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001060 return state;
Chris Masond3977122009-01-05 21:25:51 -05001061
Chris Masond7fc6402008-02-18 12:12:38 -05001062 node = rb_next(node);
1063 if (!node)
1064 break;
1065 }
1066out:
1067 return NULL;
1068}
Chris Masond7fc6402008-02-18 12:12:38 -05001069
Chris Masond352ac62008-09-29 15:18:18 -04001070/*
1071 * find a contiguous range of bytes in the file marked as delalloc, not
1072 * more than 'max_bytes'. start and end are used to return the range,
1073 *
1074 * 1 is returned if we find something, 0 if nothing was in the tree
1075 */
Chris Masonc8b97812008-10-29 14:49:59 -04001076static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1077 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001078{
1079 struct rb_node *node;
1080 struct extent_state *state;
1081 u64 cur_start = *start;
1082 u64 found = 0;
1083 u64 total_bytes = 0;
1084
Chris Masoncad321a2008-12-17 14:51:42 -05001085 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001086
Chris Masond1310b22008-01-24 16:13:08 -05001087 /*
1088 * this search will find all the extents that end after
1089 * our range starts.
1090 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001091 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001092 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001093 if (!found)
1094 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001095 goto out;
1096 }
1097
Chris Masond3977122009-01-05 21:25:51 -05001098 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001099 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001100 if (found && (state->start != cur_start ||
1101 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001102 goto out;
1103 }
1104 if (!(state->state & EXTENT_DELALLOC)) {
1105 if (!found)
1106 *end = state->end;
1107 goto out;
1108 }
Chris Masond1310b22008-01-24 16:13:08 -05001109 if (!found)
1110 *start = state->start;
1111 found++;
1112 *end = state->end;
1113 cur_start = state->end + 1;
1114 node = rb_next(node);
1115 if (!node)
1116 break;
1117 total_bytes += state->end - state->start + 1;
1118 if (total_bytes >= max_bytes)
1119 break;
1120 }
1121out:
Chris Masoncad321a2008-12-17 14:51:42 -05001122 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001123 return found;
1124}
1125
Chris Masonc8b97812008-10-29 14:49:59 -04001126static noinline int __unlock_for_delalloc(struct inode *inode,
1127 struct page *locked_page,
1128 u64 start, u64 end)
1129{
1130 int ret;
1131 struct page *pages[16];
1132 unsigned long index = start >> PAGE_CACHE_SHIFT;
1133 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1134 unsigned long nr_pages = end_index - index + 1;
1135 int i;
1136
1137 if (index == locked_page->index && end_index == index)
1138 return 0;
1139
Chris Masond3977122009-01-05 21:25:51 -05001140 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001141 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001142 min_t(unsigned long, nr_pages,
1143 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001144 for (i = 0; i < ret; i++) {
1145 if (pages[i] != locked_page)
1146 unlock_page(pages[i]);
1147 page_cache_release(pages[i]);
1148 }
1149 nr_pages -= ret;
1150 index += ret;
1151 cond_resched();
1152 }
1153 return 0;
1154}
1155
1156static noinline int lock_delalloc_pages(struct inode *inode,
1157 struct page *locked_page,
1158 u64 delalloc_start,
1159 u64 delalloc_end)
1160{
1161 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1162 unsigned long start_index = index;
1163 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1164 unsigned long pages_locked = 0;
1165 struct page *pages[16];
1166 unsigned long nrpages;
1167 int ret;
1168 int i;
1169
1170 /* the caller is responsible for locking the start index */
1171 if (index == locked_page->index && index == end_index)
1172 return 0;
1173
1174 /* skip the page at the start index */
1175 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001176 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001177 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001178 min_t(unsigned long,
1179 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001180 if (ret == 0) {
1181 ret = -EAGAIN;
1182 goto done;
1183 }
1184 /* now we have an array of pages, lock them all */
1185 for (i = 0; i < ret; i++) {
1186 /*
1187 * the caller is taking responsibility for
1188 * locked_page
1189 */
Chris Mason771ed682008-11-06 22:02:51 -05001190 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001191 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001192 if (!PageDirty(pages[i]) ||
1193 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001194 ret = -EAGAIN;
1195 unlock_page(pages[i]);
1196 page_cache_release(pages[i]);
1197 goto done;
1198 }
1199 }
Chris Masonc8b97812008-10-29 14:49:59 -04001200 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001201 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001202 }
Chris Masonc8b97812008-10-29 14:49:59 -04001203 nrpages -= ret;
1204 index += ret;
1205 cond_resched();
1206 }
1207 ret = 0;
1208done:
1209 if (ret && pages_locked) {
1210 __unlock_for_delalloc(inode, locked_page,
1211 delalloc_start,
1212 ((u64)(start_index + pages_locked - 1)) <<
1213 PAGE_CACHE_SHIFT);
1214 }
1215 return ret;
1216}
1217
1218/*
1219 * find a contiguous range of bytes in the file marked as delalloc, not
1220 * more than 'max_bytes'. start and end are used to return the range,
1221 *
1222 * 1 is returned if we find something, 0 if nothing was in the tree
1223 */
1224static noinline u64 find_lock_delalloc_range(struct inode *inode,
1225 struct extent_io_tree *tree,
1226 struct page *locked_page,
1227 u64 *start, u64 *end,
1228 u64 max_bytes)
1229{
1230 u64 delalloc_start;
1231 u64 delalloc_end;
1232 u64 found;
1233 int ret;
1234 int loops = 0;
1235
1236again:
1237 /* step one, find a bunch of delalloc bytes starting at start */
1238 delalloc_start = *start;
1239 delalloc_end = 0;
1240 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1241 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001242 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001243 *start = delalloc_start;
1244 *end = delalloc_end;
1245 return found;
1246 }
1247
1248 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001249 * start comes from the offset of locked_page. We have to lock
1250 * pages in order, so we can't process delalloc bytes before
1251 * locked_page
1252 */
Chris Masond3977122009-01-05 21:25:51 -05001253 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001254 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001255
1256 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001257 * make sure to limit the number of pages we try to lock down
1258 * if we're looping.
1259 */
Chris Masond3977122009-01-05 21:25:51 -05001260 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001261 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001262
Chris Masonc8b97812008-10-29 14:49:59 -04001263 /* step two, lock all the pages after the page that has start */
1264 ret = lock_delalloc_pages(inode, locked_page,
1265 delalloc_start, delalloc_end);
1266 if (ret == -EAGAIN) {
1267 /* some of the pages are gone, lets avoid looping by
1268 * shortening the size of the delalloc range we're searching
1269 */
1270 if (!loops) {
1271 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1272 max_bytes = PAGE_CACHE_SIZE - offset;
1273 loops = 1;
1274 goto again;
1275 } else {
1276 found = 0;
1277 goto out_failed;
1278 }
1279 }
1280 BUG_ON(ret);
1281
1282 /* step three, lock the state bits for the whole range */
1283 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1284
1285 /* then test to make sure it is all still delalloc */
1286 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1287 EXTENT_DELALLOC, 1);
1288 if (!ret) {
1289 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1290 __unlock_for_delalloc(inode, locked_page,
1291 delalloc_start, delalloc_end);
1292 cond_resched();
1293 goto again;
1294 }
1295 *start = delalloc_start;
1296 *end = delalloc_end;
1297out_failed:
1298 return found;
1299}
1300
1301int extent_clear_unlock_delalloc(struct inode *inode,
1302 struct extent_io_tree *tree,
1303 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001304 int unlock_pages,
1305 int clear_unlock,
1306 int clear_delalloc, int clear_dirty,
1307 int set_writeback,
Chris Masonc8b97812008-10-29 14:49:59 -04001308 int end_writeback)
1309{
1310 int ret;
1311 struct page *pages[16];
1312 unsigned long index = start >> PAGE_CACHE_SHIFT;
1313 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1314 unsigned long nr_pages = end_index - index + 1;
1315 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001316 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001317
Chris Mason771ed682008-11-06 22:02:51 -05001318 if (clear_unlock)
1319 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001320 if (clear_dirty)
1321 clear_bits |= EXTENT_DIRTY;
1322
Chris Mason771ed682008-11-06 22:02:51 -05001323 if (clear_delalloc)
1324 clear_bits |= EXTENT_DELALLOC;
1325
Chris Masonc8b97812008-10-29 14:49:59 -04001326 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05001327 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1328 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001329
Chris Masond3977122009-01-05 21:25:51 -05001330 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001331 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001332 min_t(unsigned long,
1333 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001334 for (i = 0; i < ret; i++) {
1335 if (pages[i] == locked_page) {
1336 page_cache_release(pages[i]);
1337 continue;
1338 }
1339 if (clear_dirty)
1340 clear_page_dirty_for_io(pages[i]);
1341 if (set_writeback)
1342 set_page_writeback(pages[i]);
1343 if (end_writeback)
1344 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001345 if (unlock_pages)
1346 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001347 page_cache_release(pages[i]);
1348 }
1349 nr_pages -= ret;
1350 index += ret;
1351 cond_resched();
1352 }
1353 return 0;
1354}
Chris Masonc8b97812008-10-29 14:49:59 -04001355
Chris Masond352ac62008-09-29 15:18:18 -04001356/*
1357 * count the number of bytes in the tree that have a given bit(s)
1358 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1359 * cached. The total number found is returned.
1360 */
Chris Masond1310b22008-01-24 16:13:08 -05001361u64 count_range_bits(struct extent_io_tree *tree,
1362 u64 *start, u64 search_end, u64 max_bytes,
1363 unsigned long bits)
1364{
1365 struct rb_node *node;
1366 struct extent_state *state;
1367 u64 cur_start = *start;
1368 u64 total_bytes = 0;
1369 int found = 0;
1370
1371 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001372 WARN_ON(1);
1373 return 0;
1374 }
1375
Chris Masoncad321a2008-12-17 14:51:42 -05001376 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001377 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1378 total_bytes = tree->dirty_bytes;
1379 goto out;
1380 }
1381 /*
1382 * this search will find all the extents that end after
1383 * our range starts.
1384 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001385 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001386 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001387 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001388
Chris Masond3977122009-01-05 21:25:51 -05001389 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001390 state = rb_entry(node, struct extent_state, rb_node);
1391 if (state->start > search_end)
1392 break;
1393 if (state->end >= cur_start && (state->state & bits)) {
1394 total_bytes += min(search_end, state->end) + 1 -
1395 max(cur_start, state->start);
1396 if (total_bytes >= max_bytes)
1397 break;
1398 if (!found) {
1399 *start = state->start;
1400 found = 1;
1401 }
1402 }
1403 node = rb_next(node);
1404 if (!node)
1405 break;
1406 }
1407out:
Chris Masoncad321a2008-12-17 14:51:42 -05001408 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001409 return total_bytes;
1410}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001411
Chris Masond352ac62008-09-29 15:18:18 -04001412/*
1413 * set the private field for a given byte offset in the tree. If there isn't
1414 * an extent_state there already, this does nothing.
1415 */
Chris Masond1310b22008-01-24 16:13:08 -05001416int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1417{
1418 struct rb_node *node;
1419 struct extent_state *state;
1420 int ret = 0;
1421
Chris Masoncad321a2008-12-17 14:51:42 -05001422 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001423 /*
1424 * this search will find all the extents that end after
1425 * our range starts.
1426 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001427 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001428 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001429 ret = -ENOENT;
1430 goto out;
1431 }
1432 state = rb_entry(node, struct extent_state, rb_node);
1433 if (state->start != start) {
1434 ret = -ENOENT;
1435 goto out;
1436 }
1437 state->private = private;
1438out:
Chris Masoncad321a2008-12-17 14:51:42 -05001439 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001440 return ret;
1441}
1442
1443int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1444{
1445 struct rb_node *node;
1446 struct extent_state *state;
1447 int ret = 0;
1448
Chris Masoncad321a2008-12-17 14:51:42 -05001449 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001450 /*
1451 * this search will find all the extents that end after
1452 * our range starts.
1453 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001454 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001455 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001456 ret = -ENOENT;
1457 goto out;
1458 }
1459 state = rb_entry(node, struct extent_state, rb_node);
1460 if (state->start != start) {
1461 ret = -ENOENT;
1462 goto out;
1463 }
1464 *private = state->private;
1465out:
Chris Masoncad321a2008-12-17 14:51:42 -05001466 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001467 return ret;
1468}
1469
1470/*
1471 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001472 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001473 * has the bits set. Otherwise, 1 is returned if any bit in the
1474 * range is found set.
1475 */
1476int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1477 int bits, int filled)
1478{
1479 struct extent_state *state = NULL;
1480 struct rb_node *node;
1481 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001482
Chris Masoncad321a2008-12-17 14:51:42 -05001483 spin_lock(&tree->lock);
Chris Mason80ea96b2008-02-01 14:51:59 -05001484 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001485 while (node && start <= end) {
1486 state = rb_entry(node, struct extent_state, rb_node);
1487
1488 if (filled && state->start > start) {
1489 bitset = 0;
1490 break;
1491 }
1492
1493 if (state->start > end)
1494 break;
1495
1496 if (state->state & bits) {
1497 bitset = 1;
1498 if (!filled)
1499 break;
1500 } else if (filled) {
1501 bitset = 0;
1502 break;
1503 }
1504 start = state->end + 1;
1505 if (start > end)
1506 break;
1507 node = rb_next(node);
1508 if (!node) {
1509 if (filled)
1510 bitset = 0;
1511 break;
1512 }
1513 }
Chris Masoncad321a2008-12-17 14:51:42 -05001514 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001515 return bitset;
1516}
Chris Masond1310b22008-01-24 16:13:08 -05001517
1518/*
1519 * helper function to set a given page up to date if all the
1520 * extents in the tree for that page are up to date
1521 */
1522static int check_page_uptodate(struct extent_io_tree *tree,
1523 struct page *page)
1524{
1525 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1526 u64 end = start + PAGE_CACHE_SIZE - 1;
1527 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1528 SetPageUptodate(page);
1529 return 0;
1530}
1531
1532/*
1533 * helper function to unlock a page if all the extents in the tree
1534 * for that page are unlocked
1535 */
1536static int check_page_locked(struct extent_io_tree *tree,
1537 struct page *page)
1538{
1539 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1540 u64 end = start + PAGE_CACHE_SIZE - 1;
1541 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1542 unlock_page(page);
1543 return 0;
1544}
1545
1546/*
1547 * helper function to end page writeback if all the extents
1548 * in the tree for that page are done with writeback
1549 */
1550static int check_page_writeback(struct extent_io_tree *tree,
1551 struct page *page)
1552{
Chris Mason1edbb732009-09-02 13:24:36 -04001553 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001554 return 0;
1555}
1556
1557/* lots and lots of room for performance fixes in the end_bio funcs */
1558
1559/*
1560 * after a writepage IO is done, we need to:
1561 * clear the uptodate bits on error
1562 * clear the writeback bits in the extent tree for this IO
1563 * end_page_writeback if the page has no more pending IO
1564 *
1565 * Scheduling is not allowed, so the extent state tree is expected
1566 * to have one and only one object corresponding to this IO.
1567 */
Chris Masond1310b22008-01-24 16:13:08 -05001568static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001569{
Chris Mason1259ab72008-05-12 13:39:03 -04001570 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001571 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001572 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001573 u64 start;
1574 u64 end;
1575 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001576 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001577
Chris Masond1310b22008-01-24 16:13:08 -05001578 do {
1579 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001580 tree = &BTRFS_I(page->mapping->host)->io_tree;
1581
Chris Masond1310b22008-01-24 16:13:08 -05001582 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1583 bvec->bv_offset;
1584 end = start + bvec->bv_len - 1;
1585
1586 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1587 whole_page = 1;
1588 else
1589 whole_page = 0;
1590
1591 if (--bvec >= bio->bi_io_vec)
1592 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001593 if (tree->ops && tree->ops->writepage_end_io_hook) {
1594 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001595 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001596 if (ret)
1597 uptodate = 0;
1598 }
1599
1600 if (!uptodate && tree->ops &&
1601 tree->ops->writepage_io_failed_hook) {
1602 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001603 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001604 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001605 uptodate = (err == 0);
1606 continue;
1607 }
1608 }
1609
Chris Masond1310b22008-01-24 16:13:08 -05001610 if (!uptodate) {
Chris Mason1edbb732009-09-02 13:24:36 -04001611 clear_extent_uptodate(tree, start, end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001612 ClearPageUptodate(page);
1613 SetPageError(page);
1614 }
Chris Mason70dec802008-01-29 09:59:12 -05001615
Chris Masond1310b22008-01-24 16:13:08 -05001616 if (whole_page)
1617 end_page_writeback(page);
1618 else
1619 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001620 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001621
Chris Masond1310b22008-01-24 16:13:08 -05001622 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001623}
1624
1625/*
1626 * after a readpage IO is done, we need to:
1627 * clear the uptodate bits on error
1628 * set the uptodate bits if things worked
1629 * set the page up to date if all extents in the tree are uptodate
1630 * clear the lock bit in the extent tree
1631 * unlock the page if there are no other extents locked for it
1632 *
1633 * Scheduling is not allowed, so the extent state tree is expected
1634 * to have one and only one object corresponding to this IO.
1635 */
Chris Masond1310b22008-01-24 16:13:08 -05001636static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001637{
1638 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1639 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001640 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001641 u64 start;
1642 u64 end;
1643 int whole_page;
1644 int ret;
1645
Chris Masond20f7042008-12-08 16:58:54 -05001646 if (err)
1647 uptodate = 0;
1648
Chris Masond1310b22008-01-24 16:13:08 -05001649 do {
1650 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001651 tree = &BTRFS_I(page->mapping->host)->io_tree;
1652
Chris Masond1310b22008-01-24 16:13:08 -05001653 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1654 bvec->bv_offset;
1655 end = start + bvec->bv_len - 1;
1656
1657 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1658 whole_page = 1;
1659 else
1660 whole_page = 0;
1661
1662 if (--bvec >= bio->bi_io_vec)
1663 prefetchw(&bvec->bv_page->flags);
1664
1665 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001666 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001667 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001668 if (ret)
1669 uptodate = 0;
1670 }
Chris Mason7e383262008-04-09 16:28:12 -04001671 if (!uptodate && tree->ops &&
1672 tree->ops->readpage_io_failed_hook) {
1673 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001674 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001675 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001676 uptodate =
1677 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001678 if (err)
1679 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001680 continue;
1681 }
1682 }
Chris Mason70dec802008-01-29 09:59:12 -05001683
Chris Mason771ed682008-11-06 22:02:51 -05001684 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001685 set_extent_uptodate(tree, start, end,
1686 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001687 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001688 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001689
Chris Mason70dec802008-01-29 09:59:12 -05001690 if (whole_page) {
1691 if (uptodate) {
1692 SetPageUptodate(page);
1693 } else {
1694 ClearPageUptodate(page);
1695 SetPageError(page);
1696 }
Chris Masond1310b22008-01-24 16:13:08 -05001697 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001698 } else {
1699 if (uptodate) {
1700 check_page_uptodate(tree, page);
1701 } else {
1702 ClearPageUptodate(page);
1703 SetPageError(page);
1704 }
Chris Masond1310b22008-01-24 16:13:08 -05001705 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001706 }
Chris Masond1310b22008-01-24 16:13:08 -05001707 } while (bvec >= bio->bi_io_vec);
1708
1709 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001710}
1711
1712/*
1713 * IO done from prepare_write is pretty simple, we just unlock
1714 * the structs in the extent tree when done, and set the uptodate bits
1715 * as appropriate.
1716 */
Chris Masond1310b22008-01-24 16:13:08 -05001717static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001718{
1719 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1720 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001721 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001722 u64 start;
1723 u64 end;
1724
Chris Masond1310b22008-01-24 16:13:08 -05001725 do {
1726 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001727 tree = &BTRFS_I(page->mapping->host)->io_tree;
1728
Chris Masond1310b22008-01-24 16:13:08 -05001729 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1730 bvec->bv_offset;
1731 end = start + bvec->bv_len - 1;
1732
1733 if (--bvec >= bio->bi_io_vec)
1734 prefetchw(&bvec->bv_page->flags);
1735
1736 if (uptodate) {
1737 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1738 } else {
1739 ClearPageUptodate(page);
1740 SetPageError(page);
1741 }
1742
1743 unlock_extent(tree, start, end, GFP_ATOMIC);
1744
1745 } while (bvec >= bio->bi_io_vec);
1746
1747 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001748}
1749
1750static struct bio *
1751extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1752 gfp_t gfp_flags)
1753{
1754 struct bio *bio;
1755
1756 bio = bio_alloc(gfp_flags, nr_vecs);
1757
1758 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1759 while (!bio && (nr_vecs /= 2))
1760 bio = bio_alloc(gfp_flags, nr_vecs);
1761 }
1762
1763 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001764 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001765 bio->bi_bdev = bdev;
1766 bio->bi_sector = first_sector;
1767 }
1768 return bio;
1769}
1770
Chris Masonc8b97812008-10-29 14:49:59 -04001771static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1772 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001773{
Chris Masond1310b22008-01-24 16:13:08 -05001774 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001775 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1776 struct page *page = bvec->bv_page;
1777 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001778 u64 start;
1779 u64 end;
1780
1781 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1782 end = start + bvec->bv_len - 1;
1783
David Woodhouse902b22f2008-08-20 08:51:49 -04001784 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001785
1786 bio_get(bio);
1787
Chris Mason065631f2008-02-20 12:07:25 -05001788 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001789 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001790 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001791 else
1792 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001793 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1794 ret = -EOPNOTSUPP;
1795 bio_put(bio);
1796 return ret;
1797}
1798
1799static int submit_extent_page(int rw, struct extent_io_tree *tree,
1800 struct page *page, sector_t sector,
1801 size_t size, unsigned long offset,
1802 struct block_device *bdev,
1803 struct bio **bio_ret,
1804 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001805 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001806 int mirror_num,
1807 unsigned long prev_bio_flags,
1808 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001809{
1810 int ret = 0;
1811 struct bio *bio;
1812 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001813 int contig = 0;
1814 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1815 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001816 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001817
1818 if (bio_ret && *bio_ret) {
1819 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001820 if (old_compressed)
1821 contig = bio->bi_sector == sector;
1822 else
1823 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1824 sector;
1825
1826 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001827 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001828 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1829 bio_flags)) ||
1830 bio_add_page(bio, page, page_size, offset) < page_size) {
1831 ret = submit_one_bio(rw, bio, mirror_num,
1832 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001833 bio = NULL;
1834 } else {
1835 return 0;
1836 }
1837 }
Chris Masonc8b97812008-10-29 14:49:59 -04001838 if (this_compressed)
1839 nr = BIO_MAX_PAGES;
1840 else
1841 nr = bio_get_nr_vecs(bdev);
1842
Chris Masond1310b22008-01-24 16:13:08 -05001843 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001844
Chris Masonc8b97812008-10-29 14:49:59 -04001845 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001846 bio->bi_end_io = end_io_func;
1847 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001848
Chris Masond3977122009-01-05 21:25:51 -05001849 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001850 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001851 else
Chris Masonc8b97812008-10-29 14:49:59 -04001852 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001853
1854 return ret;
1855}
1856
1857void set_page_extent_mapped(struct page *page)
1858{
1859 if (!PagePrivate(page)) {
1860 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001861 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001862 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001863 }
1864}
1865
Christoph Hellwigb2950862008-12-02 09:54:17 -05001866static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001867{
1868 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1869}
1870
1871/*
1872 * basic readpage implementation. Locked extent state structs are inserted
1873 * into the tree that are removed when the IO is done (by the end_io
1874 * handlers)
1875 */
1876static int __extent_read_full_page(struct extent_io_tree *tree,
1877 struct page *page,
1878 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001879 struct bio **bio, int mirror_num,
1880 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001881{
1882 struct inode *inode = page->mapping->host;
1883 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1884 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1885 u64 end;
1886 u64 cur = start;
1887 u64 extent_offset;
1888 u64 last_byte = i_size_read(inode);
1889 u64 block_start;
1890 u64 cur_end;
1891 sector_t sector;
1892 struct extent_map *em;
1893 struct block_device *bdev;
1894 int ret;
1895 int nr = 0;
1896 size_t page_offset = 0;
1897 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001898 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001899 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001900 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001901
1902 set_page_extent_mapped(page);
1903
1904 end = page_end;
1905 lock_extent(tree, start, end, GFP_NOFS);
1906
Chris Masonc8b97812008-10-29 14:49:59 -04001907 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1908 char *userpage;
1909 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1910
1911 if (zero_offset) {
1912 iosize = PAGE_CACHE_SIZE - zero_offset;
1913 userpage = kmap_atomic(page, KM_USER0);
1914 memset(userpage + zero_offset, 0, iosize);
1915 flush_dcache_page(page);
1916 kunmap_atomic(userpage, KM_USER0);
1917 }
1918 }
Chris Masond1310b22008-01-24 16:13:08 -05001919 while (cur <= end) {
1920 if (cur >= last_byte) {
1921 char *userpage;
1922 iosize = PAGE_CACHE_SIZE - page_offset;
1923 userpage = kmap_atomic(page, KM_USER0);
1924 memset(userpage + page_offset, 0, iosize);
1925 flush_dcache_page(page);
1926 kunmap_atomic(userpage, KM_USER0);
1927 set_extent_uptodate(tree, cur, cur + iosize - 1,
1928 GFP_NOFS);
1929 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1930 break;
1931 }
1932 em = get_extent(inode, page, page_offset, cur,
1933 end - cur + 1, 0);
1934 if (IS_ERR(em) || !em) {
1935 SetPageError(page);
1936 unlock_extent(tree, cur, end, GFP_NOFS);
1937 break;
1938 }
Chris Masond1310b22008-01-24 16:13:08 -05001939 extent_offset = cur - em->start;
1940 BUG_ON(extent_map_end(em) <= cur);
1941 BUG_ON(end < cur);
1942
Chris Masonc8b97812008-10-29 14:49:59 -04001943 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1944 this_bio_flag = EXTENT_BIO_COMPRESSED;
1945
Chris Masond1310b22008-01-24 16:13:08 -05001946 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1947 cur_end = min(extent_map_end(em) - 1, end);
1948 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04001949 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
1950 disk_io_size = em->block_len;
1951 sector = em->block_start >> 9;
1952 } else {
1953 sector = (em->block_start + extent_offset) >> 9;
1954 disk_io_size = iosize;
1955 }
Chris Masond1310b22008-01-24 16:13:08 -05001956 bdev = em->bdev;
1957 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04001958 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1959 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05001960 free_extent_map(em);
1961 em = NULL;
1962
1963 /* we've found a hole, just zero and go on */
1964 if (block_start == EXTENT_MAP_HOLE) {
1965 char *userpage;
1966 userpage = kmap_atomic(page, KM_USER0);
1967 memset(userpage + page_offset, 0, iosize);
1968 flush_dcache_page(page);
1969 kunmap_atomic(userpage, KM_USER0);
1970
1971 set_extent_uptodate(tree, cur, cur + iosize - 1,
1972 GFP_NOFS);
1973 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1974 cur = cur + iosize;
1975 page_offset += iosize;
1976 continue;
1977 }
1978 /* the get_extent function already copied into the page */
1979 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04001980 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001981 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1982 cur = cur + iosize;
1983 page_offset += iosize;
1984 continue;
1985 }
Chris Mason70dec802008-01-29 09:59:12 -05001986 /* we have an inline extent but it didn't get marked up
1987 * to date. Error out
1988 */
1989 if (block_start == EXTENT_MAP_INLINE) {
1990 SetPageError(page);
1991 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1992 cur = cur + iosize;
1993 page_offset += iosize;
1994 continue;
1995 }
Chris Masond1310b22008-01-24 16:13:08 -05001996
1997 ret = 0;
1998 if (tree->ops && tree->ops->readpage_io_hook) {
1999 ret = tree->ops->readpage_io_hook(page, cur,
2000 cur + iosize - 1);
2001 }
2002 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002003 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2004 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002005 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002006 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002007 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002008 end_bio_extent_readpage, mirror_num,
2009 *bio_flags,
2010 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002011 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002012 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002013 }
2014 if (ret)
2015 SetPageError(page);
2016 cur = cur + iosize;
2017 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002018 }
2019 if (!nr) {
2020 if (!PageError(page))
2021 SetPageUptodate(page);
2022 unlock_page(page);
2023 }
2024 return 0;
2025}
2026
2027int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2028 get_extent_t *get_extent)
2029{
2030 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002031 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002032 int ret;
2033
Chris Masonc8b97812008-10-29 14:49:59 -04002034 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2035 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002036 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002037 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002038 return ret;
2039}
Chris Masond1310b22008-01-24 16:13:08 -05002040
Chris Mason11c83492009-04-20 15:50:09 -04002041static noinline void update_nr_written(struct page *page,
2042 struct writeback_control *wbc,
2043 unsigned long nr_written)
2044{
2045 wbc->nr_to_write -= nr_written;
2046 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2047 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2048 page->mapping->writeback_index = page->index + nr_written;
2049}
2050
Chris Masond1310b22008-01-24 16:13:08 -05002051/*
2052 * the writepage semantics are similar to regular writepage. extent
2053 * records are inserted to lock ranges in the tree, and as dirty areas
2054 * are found, they are marked writeback. Then the lock bits are removed
2055 * and the end_io handler clears the writeback ranges
2056 */
2057static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2058 void *data)
2059{
2060 struct inode *inode = page->mapping->host;
2061 struct extent_page_data *epd = data;
2062 struct extent_io_tree *tree = epd->tree;
2063 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2064 u64 delalloc_start;
2065 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2066 u64 end;
2067 u64 cur = start;
2068 u64 extent_offset;
2069 u64 last_byte = i_size_read(inode);
2070 u64 block_start;
2071 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002072 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002073 sector_t sector;
2074 struct extent_map *em;
2075 struct block_device *bdev;
2076 int ret;
2077 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002078 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002079 size_t blocksize;
2080 loff_t i_size = i_size_read(inode);
2081 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2082 u64 nr_delalloc;
2083 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002084 int page_started;
2085 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002086 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002087 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002088
Chris Masonffbd5172009-04-20 15:50:09 -04002089 if (wbc->sync_mode == WB_SYNC_ALL)
2090 write_flags = WRITE_SYNC_PLUG;
2091 else
2092 write_flags = WRITE;
2093
Chris Masond1310b22008-01-24 16:13:08 -05002094 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002095 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002096 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002097 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002098 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002099 unlock_page(page);
2100 return 0;
2101 }
2102
2103 if (page->index == end_index) {
2104 char *userpage;
2105
Chris Masond1310b22008-01-24 16:13:08 -05002106 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002107 memset(userpage + pg_offset, 0,
2108 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002109 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002110 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002111 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002112 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002113
2114 set_page_extent_mapped(page);
2115
2116 delalloc_start = start;
2117 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002118 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002119 if (!epd->extent_locked) {
Chris Masona97adc92009-08-07 09:28:20 -04002120 u64 delalloc_to_write;
Chris Mason11c83492009-04-20 15:50:09 -04002121 /*
2122 * make sure the wbc mapping index is at least updated
2123 * to this page.
2124 */
2125 update_nr_written(page, wbc, 0);
2126
Chris Masond3977122009-01-05 21:25:51 -05002127 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002128 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002129 page,
2130 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002131 &delalloc_end,
2132 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002133 if (nr_delalloc == 0) {
2134 delalloc_start = delalloc_end + 1;
2135 continue;
2136 }
2137 tree->ops->fill_delalloc(inode, page, delalloc_start,
2138 delalloc_end, &page_started,
2139 &nr_written);
Chris Masona97adc92009-08-07 09:28:20 -04002140 delalloc_to_write = (delalloc_end -
2141 max_t(u64, page_offset(page),
2142 delalloc_start) + 1) >>
2143 PAGE_CACHE_SHIFT;
2144 if (wbc->nr_to_write < delalloc_to_write) {
2145 wbc->nr_to_write = min_t(long, 8192,
2146 delalloc_to_write);
2147 }
Chris Masond1310b22008-01-24 16:13:08 -05002148 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002149 }
Chris Masonc8b97812008-10-29 14:49:59 -04002150
Chris Mason771ed682008-11-06 22:02:51 -05002151 /* did the fill delalloc function already unlock and start
2152 * the IO?
2153 */
2154 if (page_started) {
2155 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002156 /*
2157 * we've unlocked the page, so we can't update
2158 * the mapping's writeback index, just update
2159 * nr_to_write.
2160 */
2161 wbc->nr_to_write -= nr_written;
2162 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002163 }
Chris Masonc8b97812008-10-29 14:49:59 -04002164 }
Chris Masond1310b22008-01-24 16:13:08 -05002165 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002166
Chris Masone6dcd2d2008-07-17 12:53:50 -04002167 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002168
Chris Mason247e7432008-07-17 12:53:51 -04002169 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002170 ret = tree->ops->writepage_start_hook(page, start,
2171 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002172 if (ret == -EAGAIN) {
2173 unlock_extent(tree, start, page_end, GFP_NOFS);
2174 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002175 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002176 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002177 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002178 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002179 }
2180 }
2181
Chris Mason11c83492009-04-20 15:50:09 -04002182 /*
2183 * we don't want to touch the inode after unlocking the page,
2184 * so we update the mapping writeback index now
2185 */
2186 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002187
Chris Masond1310b22008-01-24 16:13:08 -05002188 end = page_end;
Chris Masond3977122009-01-05 21:25:51 -05002189 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2190 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
Chris Masond1310b22008-01-24 16:13:08 -05002191
2192 if (last_byte <= start) {
Chris Mason1edbb732009-09-02 13:24:36 -04002193 clear_extent_bit(tree, start, page_end,
2194 EXTENT_LOCKED | EXTENT_DIRTY,
2195 1, 0, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002196 if (tree->ops && tree->ops->writepage_end_io_hook)
2197 tree->ops->writepage_end_io_hook(page, start,
2198 page_end, NULL, 1);
2199 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002200 goto done;
2201 }
2202
Chris Masond1310b22008-01-24 16:13:08 -05002203 blocksize = inode->i_sb->s_blocksize;
2204
2205 while (cur <= end) {
2206 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002207 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2208 if (tree->ops && tree->ops->writepage_end_io_hook)
2209 tree->ops->writepage_end_io_hook(page, cur,
2210 page_end, NULL, 1);
2211 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002212 break;
2213 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002214 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002215 end - cur + 1, 1);
2216 if (IS_ERR(em) || !em) {
2217 SetPageError(page);
2218 break;
2219 }
2220
2221 extent_offset = cur - em->start;
2222 BUG_ON(extent_map_end(em) <= cur);
2223 BUG_ON(end < cur);
2224 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2225 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2226 sector = (em->block_start + extent_offset) >> 9;
2227 bdev = em->bdev;
2228 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002229 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002230 free_extent_map(em);
2231 em = NULL;
2232
Chris Masonc8b97812008-10-29 14:49:59 -04002233 /*
2234 * compressed and inline extents are written through other
2235 * paths in the FS
2236 */
2237 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002238 block_start == EXTENT_MAP_INLINE) {
Chris Masond3977122009-01-05 21:25:51 -05002239 unlock_extent(tree, unlock_start, cur + iosize - 1,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002240 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002241
Chris Masonc8b97812008-10-29 14:49:59 -04002242 /*
2243 * end_io notification does not happen here for
2244 * compressed extents
2245 */
2246 if (!compressed && tree->ops &&
2247 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002248 tree->ops->writepage_end_io_hook(page, cur,
2249 cur + iosize - 1,
2250 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002251 else if (compressed) {
2252 /* we don't want to end_page_writeback on
2253 * a compressed extent. this happens
2254 * elsewhere
2255 */
2256 nr++;
2257 }
2258
2259 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002260 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002261 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002262 continue;
2263 }
Chris Masond1310b22008-01-24 16:13:08 -05002264 /* leave this out until we have a page_mkwrite call */
2265 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2266 EXTENT_DIRTY, 0)) {
2267 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002268 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002269 continue;
2270 }
Chris Masonc8b97812008-10-29 14:49:59 -04002271
Chris Masond1310b22008-01-24 16:13:08 -05002272 if (tree->ops && tree->ops->writepage_io_hook) {
2273 ret = tree->ops->writepage_io_hook(page, cur,
2274 cur + iosize - 1);
2275 } else {
2276 ret = 0;
2277 }
Chris Mason1259ab72008-05-12 13:39:03 -04002278 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002279 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002280 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002281 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002282
Chris Masond1310b22008-01-24 16:13:08 -05002283 set_range_writeback(tree, cur, cur + iosize - 1);
2284 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002285 printk(KERN_ERR "btrfs warning page %lu not "
2286 "writeback, cur %llu end %llu\n",
2287 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002288 (unsigned long long)end);
2289 }
2290
Chris Masonffbd5172009-04-20 15:50:09 -04002291 ret = submit_extent_page(write_flags, tree, page,
2292 sector, iosize, pg_offset,
2293 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002294 end_bio_extent_writepage,
2295 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002296 if (ret)
2297 SetPageError(page);
2298 }
2299 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002300 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002301 nr++;
2302 }
2303done:
2304 if (nr == 0) {
2305 /* make sure the mapping tag for page dirty gets cleared */
2306 set_page_writeback(page);
2307 end_page_writeback(page);
2308 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002309 if (unlock_start <= page_end)
2310 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002311 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002312
Chris Mason11c83492009-04-20 15:50:09 -04002313done_unlocked:
2314
Chris Masond1310b22008-01-24 16:13:08 -05002315 return 0;
2316}
2317
Chris Masond1310b22008-01-24 16:13:08 -05002318/**
Chris Mason4bef0842008-09-08 11:18:08 -04002319 * 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 -05002320 * @mapping: address space structure to write
2321 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2322 * @writepage: function called for each page
2323 * @data: data passed to writepage function
2324 *
2325 * If a page is already under I/O, write_cache_pages() skips it, even
2326 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2327 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2328 * and msync() need to guarantee that all the data which was dirty at the time
2329 * the call was made get new I/O started against them. If wbc->sync_mode is
2330 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2331 * existing IO to complete.
2332 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002333static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002334 struct address_space *mapping,
2335 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002336 writepage_t writepage, void *data,
2337 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002338{
Chris Masond1310b22008-01-24 16:13:08 -05002339 int ret = 0;
2340 int done = 0;
2341 struct pagevec pvec;
2342 int nr_pages;
2343 pgoff_t index;
2344 pgoff_t end; /* Inclusive */
2345 int scanned = 0;
2346 int range_whole = 0;
2347
Chris Masond1310b22008-01-24 16:13:08 -05002348 pagevec_init(&pvec, 0);
2349 if (wbc->range_cyclic) {
2350 index = mapping->writeback_index; /* Start from prev offset */
2351 end = -1;
2352 } else {
2353 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2354 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2355 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2356 range_whole = 1;
2357 scanned = 1;
2358 }
2359retry:
2360 while (!done && (index <= end) &&
2361 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002362 PAGECACHE_TAG_DIRTY, min(end - index,
2363 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002364 unsigned i;
2365
2366 scanned = 1;
2367 for (i = 0; i < nr_pages; i++) {
2368 struct page *page = pvec.pages[i];
2369
2370 /*
2371 * At this point we hold neither mapping->tree_lock nor
2372 * lock on the page itself: the page may be truncated or
2373 * invalidated (changing page->mapping to NULL), or even
2374 * swizzled back from swapper_space to tmpfs file
2375 * mapping
2376 */
Chris Mason4bef0842008-09-08 11:18:08 -04002377 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2378 tree->ops->write_cache_pages_lock_hook(page);
2379 else
2380 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002381
2382 if (unlikely(page->mapping != mapping)) {
2383 unlock_page(page);
2384 continue;
2385 }
2386
2387 if (!wbc->range_cyclic && page->index > end) {
2388 done = 1;
2389 unlock_page(page);
2390 continue;
2391 }
2392
Chris Masond2c3f4f2008-11-19 12:44:22 -05002393 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002394 if (PageWriteback(page))
2395 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002396 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002397 }
Chris Masond1310b22008-01-24 16:13:08 -05002398
2399 if (PageWriteback(page) ||
2400 !clear_page_dirty_for_io(page)) {
2401 unlock_page(page);
2402 continue;
2403 }
2404
2405 ret = (*writepage)(page, wbc, data);
2406
2407 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2408 unlock_page(page);
2409 ret = 0;
2410 }
Chris Mason771ed682008-11-06 22:02:51 -05002411 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002412 done = 1;
Chris Masond1310b22008-01-24 16:13:08 -05002413 }
2414 pagevec_release(&pvec);
2415 cond_resched();
2416 }
2417 if (!scanned && !done) {
2418 /*
2419 * We hit the last page and there is more work to be done: wrap
2420 * back to the start of the file
2421 */
2422 scanned = 1;
2423 index = 0;
2424 goto retry;
2425 }
Chris Masond1310b22008-01-24 16:13:08 -05002426 return ret;
2427}
Chris Masond1310b22008-01-24 16:13:08 -05002428
Chris Masonffbd5172009-04-20 15:50:09 -04002429static void flush_epd_write_bio(struct extent_page_data *epd)
2430{
2431 if (epd->bio) {
2432 if (epd->sync_io)
2433 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2434 else
2435 submit_one_bio(WRITE, epd->bio, 0, 0);
2436 epd->bio = NULL;
2437 }
2438}
2439
Chris Masond2c3f4f2008-11-19 12:44:22 -05002440static noinline void flush_write_bio(void *data)
2441{
2442 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002443 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002444}
2445
Chris Masond1310b22008-01-24 16:13:08 -05002446int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2447 get_extent_t *get_extent,
2448 struct writeback_control *wbc)
2449{
2450 int ret;
2451 struct address_space *mapping = page->mapping;
2452 struct extent_page_data epd = {
2453 .bio = NULL,
2454 .tree = tree,
2455 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002456 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002457 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002458 };
2459 struct writeback_control wbc_writepages = {
2460 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002461 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002462 .older_than_this = NULL,
2463 .nr_to_write = 64,
2464 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2465 .range_end = (loff_t)-1,
2466 };
2467
Chris Masond1310b22008-01-24 16:13:08 -05002468 ret = __extent_writepage(page, wbc, &epd);
2469
Chris Mason4bef0842008-09-08 11:18:08 -04002470 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002471 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002472 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002473 return ret;
2474}
Chris Masond1310b22008-01-24 16:13:08 -05002475
Chris Mason771ed682008-11-06 22:02:51 -05002476int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2477 u64 start, u64 end, get_extent_t *get_extent,
2478 int mode)
2479{
2480 int ret = 0;
2481 struct address_space *mapping = inode->i_mapping;
2482 struct page *page;
2483 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2484 PAGE_CACHE_SHIFT;
2485
2486 struct extent_page_data epd = {
2487 .bio = NULL,
2488 .tree = tree,
2489 .get_extent = get_extent,
2490 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002491 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002492 };
2493 struct writeback_control wbc_writepages = {
2494 .bdi = inode->i_mapping->backing_dev_info,
2495 .sync_mode = mode,
2496 .older_than_this = NULL,
2497 .nr_to_write = nr_pages * 2,
2498 .range_start = start,
2499 .range_end = end + 1,
2500 };
2501
Chris Masond3977122009-01-05 21:25:51 -05002502 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002503 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2504 if (clear_page_dirty_for_io(page))
2505 ret = __extent_writepage(page, &wbc_writepages, &epd);
2506 else {
2507 if (tree->ops && tree->ops->writepage_end_io_hook)
2508 tree->ops->writepage_end_io_hook(page, start,
2509 start + PAGE_CACHE_SIZE - 1,
2510 NULL, 1);
2511 unlock_page(page);
2512 }
2513 page_cache_release(page);
2514 start += PAGE_CACHE_SIZE;
2515 }
2516
Chris Masonffbd5172009-04-20 15:50:09 -04002517 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002518 return ret;
2519}
Chris Masond1310b22008-01-24 16:13:08 -05002520
2521int extent_writepages(struct extent_io_tree *tree,
2522 struct address_space *mapping,
2523 get_extent_t *get_extent,
2524 struct writeback_control *wbc)
2525{
2526 int ret = 0;
2527 struct extent_page_data epd = {
2528 .bio = NULL,
2529 .tree = tree,
2530 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002531 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002532 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002533 };
2534
Chris Mason4bef0842008-09-08 11:18:08 -04002535 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002536 __extent_writepage, &epd,
2537 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002538 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002539 return ret;
2540}
Chris Masond1310b22008-01-24 16:13:08 -05002541
2542int extent_readpages(struct extent_io_tree *tree,
2543 struct address_space *mapping,
2544 struct list_head *pages, unsigned nr_pages,
2545 get_extent_t get_extent)
2546{
2547 struct bio *bio = NULL;
2548 unsigned page_idx;
2549 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002550 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002551
2552 pagevec_init(&pvec, 0);
2553 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2554 struct page *page = list_entry(pages->prev, struct page, lru);
2555
2556 prefetchw(&page->flags);
2557 list_del(&page->lru);
2558 /*
2559 * what we want to do here is call add_to_page_cache_lru,
2560 * but that isn't exported, so we reproduce it here
2561 */
2562 if (!add_to_page_cache(page, mapping,
2563 page->index, GFP_KERNEL)) {
2564
2565 /* open coding of lru_cache_add, also not exported */
2566 page_cache_get(page);
2567 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002568 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002569 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002570 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002571 }
2572 page_cache_release(page);
2573 }
2574 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002575 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002576 BUG_ON(!list_empty(pages));
2577 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002578 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002579 return 0;
2580}
Chris Masond1310b22008-01-24 16:13:08 -05002581
2582/*
2583 * basic invalidatepage code, this waits on any locked or writeback
2584 * ranges corresponding to the page, and then deletes any extent state
2585 * records from the tree
2586 */
2587int extent_invalidatepage(struct extent_io_tree *tree,
2588 struct page *page, unsigned long offset)
2589{
2590 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2591 u64 end = start + PAGE_CACHE_SIZE - 1;
2592 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2593
Chris Masond3977122009-01-05 21:25:51 -05002594 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002595 if (start > end)
2596 return 0;
2597
2598 lock_extent(tree, start, end, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002599 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002600 clear_extent_bit(tree, start, end,
2601 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2602 1, 1, GFP_NOFS);
2603 return 0;
2604}
Chris Masond1310b22008-01-24 16:13:08 -05002605
2606/*
2607 * simple commit_write call, set_range_dirty is used to mark both
2608 * the pages and the extent records as dirty
2609 */
2610int extent_commit_write(struct extent_io_tree *tree,
2611 struct inode *inode, struct page *page,
2612 unsigned from, unsigned to)
2613{
2614 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2615
2616 set_page_extent_mapped(page);
2617 set_page_dirty(page);
2618
2619 if (pos > inode->i_size) {
2620 i_size_write(inode, pos);
2621 mark_inode_dirty(inode);
2622 }
2623 return 0;
2624}
Chris Masond1310b22008-01-24 16:13:08 -05002625
2626int extent_prepare_write(struct extent_io_tree *tree,
2627 struct inode *inode, struct page *page,
2628 unsigned from, unsigned to, get_extent_t *get_extent)
2629{
2630 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2631 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2632 u64 block_start;
2633 u64 orig_block_start;
2634 u64 block_end;
2635 u64 cur_end;
2636 struct extent_map *em;
2637 unsigned blocksize = 1 << inode->i_blkbits;
2638 size_t page_offset = 0;
2639 size_t block_off_start;
2640 size_t block_off_end;
2641 int err = 0;
2642 int iocount = 0;
2643 int ret = 0;
2644 int isnew;
2645
2646 set_page_extent_mapped(page);
2647
2648 block_start = (page_start + from) & ~((u64)blocksize - 1);
2649 block_end = (page_start + to - 1) | (blocksize - 1);
2650 orig_block_start = block_start;
2651
2652 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002653 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002654 em = get_extent(inode, page, page_offset, block_start,
2655 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002656 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002657 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002658
Chris Masond1310b22008-01-24 16:13:08 -05002659 cur_end = min(block_end, extent_map_end(em) - 1);
2660 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2661 block_off_end = block_off_start + blocksize;
2662 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2663
2664 if (!PageUptodate(page) && isnew &&
2665 (block_off_end > to || block_off_start < from)) {
2666 void *kaddr;
2667
2668 kaddr = kmap_atomic(page, KM_USER0);
2669 if (block_off_end > to)
2670 memset(kaddr + to, 0, block_off_end - to);
2671 if (block_off_start < from)
2672 memset(kaddr + block_off_start, 0,
2673 from - block_off_start);
2674 flush_dcache_page(page);
2675 kunmap_atomic(kaddr, KM_USER0);
2676 }
2677 if ((em->block_start != EXTENT_MAP_HOLE &&
2678 em->block_start != EXTENT_MAP_INLINE) &&
2679 !isnew && !PageUptodate(page) &&
2680 (block_off_end > to || block_off_start < from) &&
2681 !test_range_bit(tree, block_start, cur_end,
2682 EXTENT_UPTODATE, 1)) {
2683 u64 sector;
2684 u64 extent_offset = block_start - em->start;
2685 size_t iosize;
2686 sector = (em->block_start + extent_offset) >> 9;
2687 iosize = (cur_end - block_start + blocksize) &
2688 ~((u64)blocksize - 1);
2689 /*
2690 * we've already got the extent locked, but we
2691 * need to split the state such that our end_bio
2692 * handler can clear the lock.
2693 */
2694 set_extent_bit(tree, block_start,
2695 block_start + iosize - 1,
2696 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2697 ret = submit_extent_page(READ, tree, page,
2698 sector, iosize, page_offset, em->bdev,
2699 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002700 end_bio_extent_preparewrite, 0,
2701 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002702 iocount++;
2703 block_start = block_start + iosize;
2704 } else {
2705 set_extent_uptodate(tree, block_start, cur_end,
2706 GFP_NOFS);
2707 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2708 block_start = cur_end + 1;
2709 }
2710 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2711 free_extent_map(em);
2712 }
2713 if (iocount) {
2714 wait_extent_bit(tree, orig_block_start,
2715 block_end, EXTENT_LOCKED);
2716 }
2717 check_page_uptodate(tree, page);
2718err:
2719 /* FIXME, zero out newly allocated blocks on error */
2720 return err;
2721}
Chris Masond1310b22008-01-24 16:13:08 -05002722
2723/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002724 * a helper for releasepage, this tests for areas of the page that
2725 * are locked or under IO and drops the related state bits if it is safe
2726 * to drop the page.
2727 */
2728int try_release_extent_state(struct extent_map_tree *map,
2729 struct extent_io_tree *tree, struct page *page,
2730 gfp_t mask)
2731{
2732 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2733 u64 end = start + PAGE_CACHE_SIZE - 1;
2734 int ret = 1;
2735
Chris Mason211f90e2008-07-18 11:56:15 -04002736 if (test_range_bit(tree, start, end,
2737 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002738 ret = 0;
2739 else {
2740 if ((mask & GFP_NOFS) == GFP_NOFS)
2741 mask = GFP_NOFS;
2742 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2743 1, 1, mask);
2744 }
2745 return ret;
2746}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002747
2748/*
Chris Masond1310b22008-01-24 16:13:08 -05002749 * a helper for releasepage. As long as there are no locked extents
2750 * in the range corresponding to the page, both state records and extent
2751 * map records are removed
2752 */
2753int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002754 struct extent_io_tree *tree, struct page *page,
2755 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002756{
2757 struct extent_map *em;
2758 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2759 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002760
Chris Mason70dec802008-01-29 09:59:12 -05002761 if ((mask & __GFP_WAIT) &&
2762 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002763 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002764 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002765 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002766 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002767 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002768 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002769 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002770 break;
2771 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002772 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2773 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002774 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002775 free_extent_map(em);
2776 break;
2777 }
2778 if (!test_range_bit(tree, em->start,
2779 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002780 EXTENT_LOCKED | EXTENT_WRITEBACK |
2781 EXTENT_ORDERED,
2782 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002783 remove_extent_mapping(map, em);
2784 /* once for the rb tree */
2785 free_extent_map(em);
2786 }
2787 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002788 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002789
2790 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002791 free_extent_map(em);
2792 }
Chris Masond1310b22008-01-24 16:13:08 -05002793 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002794 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002795}
Chris Masond1310b22008-01-24 16:13:08 -05002796
2797sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2798 get_extent_t *get_extent)
2799{
2800 struct inode *inode = mapping->host;
2801 u64 start = iblock << inode->i_blkbits;
2802 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002803 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002804 struct extent_map *em;
2805
Yan Zhengd899e052008-10-30 14:25:28 -04002806 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2807 GFP_NOFS);
2808 em = get_extent(inode, NULL, 0, start, blksize, 0);
2809 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2810 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002811 if (!em || IS_ERR(em))
2812 return 0;
2813
Yan Zhengd899e052008-10-30 14:25:28 -04002814 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002815 goto out;
2816
2817 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002818out:
2819 free_extent_map(em);
2820 return sector;
2821}
2822
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002823int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2824 __u64 start, __u64 len, get_extent_t *get_extent)
2825{
2826 int ret;
2827 u64 off = start;
2828 u64 max = start + len;
2829 u32 flags = 0;
2830 u64 disko = 0;
2831 struct extent_map *em = NULL;
2832 int end = 0;
2833 u64 em_start = 0, em_len = 0;
2834 unsigned long emflags;
2835 ret = 0;
2836
2837 if (len == 0)
2838 return -EINVAL;
2839
2840 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2841 GFP_NOFS);
2842 em = get_extent(inode, NULL, 0, off, max - off, 0);
2843 if (!em)
2844 goto out;
2845 if (IS_ERR(em)) {
2846 ret = PTR_ERR(em);
2847 goto out;
2848 }
2849 while (!end) {
2850 off = em->start + em->len;
2851 if (off >= max)
2852 end = 1;
2853
2854 em_start = em->start;
2855 em_len = em->len;
2856
2857 disko = 0;
2858 flags = 0;
2859
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002860 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002861 end = 1;
2862 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002863 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002864 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002865 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002866 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2867 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002868 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002869 flags |= (FIEMAP_EXTENT_DELALLOC |
2870 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002871 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002872 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002873 }
2874 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2875 flags |= FIEMAP_EXTENT_ENCODED;
2876
2877 emflags = em->flags;
2878 free_extent_map(em);
2879 em = NULL;
2880
2881 if (!end) {
2882 em = get_extent(inode, NULL, 0, off, max - off, 0);
2883 if (!em)
2884 goto out;
2885 if (IS_ERR(em)) {
2886 ret = PTR_ERR(em);
2887 goto out;
2888 }
2889 emflags = em->flags;
2890 }
2891 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2892 flags |= FIEMAP_EXTENT_LAST;
2893 end = 1;
2894 }
2895
2896 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2897 em_len, flags);
2898 if (ret)
2899 goto out_free;
2900 }
2901out_free:
2902 free_extent_map(em);
2903out:
2904 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2905 GFP_NOFS);
2906 return ret;
2907}
2908
Chris Masond1310b22008-01-24 16:13:08 -05002909static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2910 unsigned long i)
2911{
2912 struct page *p;
2913 struct address_space *mapping;
2914
2915 if (i == 0)
2916 return eb->first_page;
2917 i += eb->start >> PAGE_CACHE_SHIFT;
2918 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002919 if (!mapping)
2920 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002921
2922 /*
2923 * extent_buffer_page is only called after pinning the page
2924 * by increasing the reference count. So we know the page must
2925 * be in the radix tree.
2926 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002927 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002928 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002929 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002930
Chris Masond1310b22008-01-24 16:13:08 -05002931 return p;
2932}
2933
Chris Mason6af118ce2008-07-22 11:18:07 -04002934static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002935{
Chris Mason6af118ce2008-07-22 11:18:07 -04002936 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2937 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002938}
2939
Chris Masond1310b22008-01-24 16:13:08 -05002940static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2941 u64 start,
2942 unsigned long len,
2943 gfp_t mask)
2944{
2945 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05002946#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002947 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04002948#endif
Chris Masond1310b22008-01-24 16:13:08 -05002949
Chris Masond1310b22008-01-24 16:13:08 -05002950 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002951 eb->start = start;
2952 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002953 spin_lock_init(&eb->lock);
2954 init_waitqueue_head(&eb->lock_wq);
2955
Chris Mason39351272009-02-04 09:24:05 -05002956#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002957 spin_lock_irqsave(&leak_lock, flags);
2958 list_add(&eb->leak_list, &buffers);
2959 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002960#endif
Chris Masond1310b22008-01-24 16:13:08 -05002961 atomic_set(&eb->refs, 1);
2962
2963 return eb;
2964}
2965
2966static void __free_extent_buffer(struct extent_buffer *eb)
2967{
Chris Mason39351272009-02-04 09:24:05 -05002968#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002969 unsigned long flags;
2970 spin_lock_irqsave(&leak_lock, flags);
2971 list_del(&eb->leak_list);
2972 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002973#endif
Chris Masond1310b22008-01-24 16:13:08 -05002974 kmem_cache_free(extent_buffer_cache, eb);
2975}
2976
2977struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2978 u64 start, unsigned long len,
2979 struct page *page0,
2980 gfp_t mask)
2981{
2982 unsigned long num_pages = num_extent_pages(start, len);
2983 unsigned long i;
2984 unsigned long index = start >> PAGE_CACHE_SHIFT;
2985 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04002986 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002987 struct page *p;
2988 struct address_space *mapping = tree->mapping;
2989 int uptodate = 1;
2990
Chris Mason6af118ce2008-07-22 11:18:07 -04002991 spin_lock(&tree->buffer_lock);
2992 eb = buffer_search(tree, start);
2993 if (eb) {
2994 atomic_inc(&eb->refs);
2995 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002996 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002997 return eb;
2998 }
2999 spin_unlock(&tree->buffer_lock);
3000
Chris Masond1310b22008-01-24 16:13:08 -05003001 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003002 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003003 return NULL;
3004
Chris Masond1310b22008-01-24 16:13:08 -05003005 if (page0) {
3006 eb->first_page = page0;
3007 i = 1;
3008 index++;
3009 page_cache_get(page0);
3010 mark_page_accessed(page0);
3011 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003012 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003013 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003014 } else {
3015 i = 0;
3016 }
3017 for (; i < num_pages; i++, index++) {
3018 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3019 if (!p) {
3020 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003021 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003022 }
3023 set_page_extent_mapped(p);
3024 mark_page_accessed(p);
3025 if (i == 0) {
3026 eb->first_page = p;
3027 set_page_extent_head(p, len);
3028 } else {
3029 set_page_private(p, EXTENT_PAGE_PRIVATE);
3030 }
3031 if (!PageUptodate(p))
3032 uptodate = 0;
3033 unlock_page(p);
3034 }
3035 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003036 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003037
Chris Mason6af118ce2008-07-22 11:18:07 -04003038 spin_lock(&tree->buffer_lock);
3039 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3040 if (exists) {
3041 /* add one reference for the caller */
3042 atomic_inc(&exists->refs);
3043 spin_unlock(&tree->buffer_lock);
3044 goto free_eb;
3045 }
3046 spin_unlock(&tree->buffer_lock);
3047
3048 /* add one reference for the tree */
3049 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003050 return eb;
3051
Chris Mason6af118ce2008-07-22 11:18:07 -04003052free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003053 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003054 return exists;
3055 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003056 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003057 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003058 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003059 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003060}
Chris Masond1310b22008-01-24 16:13:08 -05003061
3062struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3063 u64 start, unsigned long len,
3064 gfp_t mask)
3065{
Chris Masond1310b22008-01-24 16:13:08 -05003066 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003067
Chris Mason6af118ce2008-07-22 11:18:07 -04003068 spin_lock(&tree->buffer_lock);
3069 eb = buffer_search(tree, start);
3070 if (eb)
3071 atomic_inc(&eb->refs);
3072 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003073
Josef Bacik0f9dd462008-09-23 13:14:11 -04003074 if (eb)
3075 mark_page_accessed(eb->first_page);
3076
Chris Masond1310b22008-01-24 16:13:08 -05003077 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003078}
Chris Masond1310b22008-01-24 16:13:08 -05003079
3080void free_extent_buffer(struct extent_buffer *eb)
3081{
Chris Masond1310b22008-01-24 16:13:08 -05003082 if (!eb)
3083 return;
3084
3085 if (!atomic_dec_and_test(&eb->refs))
3086 return;
3087
Chris Mason6af118ce2008-07-22 11:18:07 -04003088 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003089}
Chris Masond1310b22008-01-24 16:13:08 -05003090
3091int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3092 struct extent_buffer *eb)
3093{
Chris Masond1310b22008-01-24 16:13:08 -05003094 unsigned long i;
3095 unsigned long num_pages;
3096 struct page *page;
3097
Chris Masond1310b22008-01-24 16:13:08 -05003098 num_pages = num_extent_pages(eb->start, eb->len);
3099
3100 for (i = 0; i < num_pages; i++) {
3101 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003102 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003103 continue;
3104
Chris Masona61e6f22008-07-22 11:18:08 -04003105 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003106 if (i == 0)
3107 set_page_extent_head(page, eb->len);
3108 else
3109 set_page_private(page, EXTENT_PAGE_PRIVATE);
3110
Chris Masond1310b22008-01-24 16:13:08 -05003111 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003112 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003113 if (!PageDirty(page)) {
3114 radix_tree_tag_clear(&page->mapping->page_tree,
3115 page_index(page),
3116 PAGECACHE_TAG_DIRTY);
3117 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003118 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003119 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003120 }
3121 return 0;
3122}
Chris Masond1310b22008-01-24 16:13:08 -05003123
3124int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3125 struct extent_buffer *eb)
3126{
3127 return wait_on_extent_writeback(tree, eb->start,
3128 eb->start + eb->len - 1);
3129}
Chris Masond1310b22008-01-24 16:13:08 -05003130
3131int set_extent_buffer_dirty(struct extent_io_tree *tree,
3132 struct extent_buffer *eb)
3133{
3134 unsigned long i;
3135 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003136 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003137
Chris Masonb9473432009-03-13 11:00:37 -04003138 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003139 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003140 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003141 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003142 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003143}
Chris Masond1310b22008-01-24 16:13:08 -05003144
Chris Mason1259ab72008-05-12 13:39:03 -04003145int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3146 struct extent_buffer *eb)
3147{
3148 unsigned long i;
3149 struct page *page;
3150 unsigned long num_pages;
3151
3152 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003153 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003154
3155 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3156 GFP_NOFS);
3157 for (i = 0; i < num_pages; i++) {
3158 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003159 if (page)
3160 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003161 }
3162 return 0;
3163}
3164
Chris Masond1310b22008-01-24 16:13:08 -05003165int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3166 struct extent_buffer *eb)
3167{
3168 unsigned long i;
3169 struct page *page;
3170 unsigned long num_pages;
3171
3172 num_pages = num_extent_pages(eb->start, eb->len);
3173
3174 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3175 GFP_NOFS);
3176 for (i = 0; i < num_pages; i++) {
3177 page = extent_buffer_page(eb, i);
3178 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3179 ((i == num_pages - 1) &&
3180 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3181 check_page_uptodate(tree, page);
3182 continue;
3183 }
3184 SetPageUptodate(page);
3185 }
3186 return 0;
3187}
Chris Masond1310b22008-01-24 16:13:08 -05003188
Chris Masonce9adaa2008-04-09 16:28:12 -04003189int extent_range_uptodate(struct extent_io_tree *tree,
3190 u64 start, u64 end)
3191{
3192 struct page *page;
3193 int ret;
3194 int pg_uptodate = 1;
3195 int uptodate;
3196 unsigned long index;
3197
3198 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3199 if (ret)
3200 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003201 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003202 index = start >> PAGE_CACHE_SHIFT;
3203 page = find_get_page(tree->mapping, index);
3204 uptodate = PageUptodate(page);
3205 page_cache_release(page);
3206 if (!uptodate) {
3207 pg_uptodate = 0;
3208 break;
3209 }
3210 start += PAGE_CACHE_SIZE;
3211 }
3212 return pg_uptodate;
3213}
3214
Chris Masond1310b22008-01-24 16:13:08 -05003215int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003216 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003217{
Chris Mason728131d2008-04-09 16:28:12 -04003218 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003219 unsigned long num_pages;
3220 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003221 struct page *page;
3222 int pg_uptodate = 1;
3223
Chris Masonb4ce94d2009-02-04 09:25:08 -05003224 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003225 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003226
Chris Mason42352982008-04-28 16:40:52 -04003227 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003228 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003229 if (ret)
3230 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003231
3232 num_pages = num_extent_pages(eb->start, eb->len);
3233 for (i = 0; i < num_pages; i++) {
3234 page = extent_buffer_page(eb, i);
3235 if (!PageUptodate(page)) {
3236 pg_uptodate = 0;
3237 break;
3238 }
3239 }
Chris Mason42352982008-04-28 16:40:52 -04003240 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003241}
Chris Masond1310b22008-01-24 16:13:08 -05003242
3243int read_extent_buffer_pages(struct extent_io_tree *tree,
3244 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003245 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003246 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003247{
3248 unsigned long i;
3249 unsigned long start_i;
3250 struct page *page;
3251 int err;
3252 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003253 int locked_pages = 0;
3254 int all_uptodate = 1;
3255 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003256 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003257 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003258 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003259
Chris Masonb4ce94d2009-02-04 09:25:08 -05003260 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003261 return 0;
3262
Chris Masonce9adaa2008-04-09 16:28:12 -04003263 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003264 EXTENT_UPTODATE, 1)) {
3265 return 0;
3266 }
3267
3268 if (start) {
3269 WARN_ON(start < eb->start);
3270 start_i = (start >> PAGE_CACHE_SHIFT) -
3271 (eb->start >> PAGE_CACHE_SHIFT);
3272 } else {
3273 start_i = 0;
3274 }
3275
3276 num_pages = num_extent_pages(eb->start, eb->len);
3277 for (i = start_i; i < num_pages; i++) {
3278 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003279 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003280 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003281 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003282 } else {
3283 lock_page(page);
3284 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003285 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003286 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003287 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003288 }
3289 if (all_uptodate) {
3290 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003291 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003292 goto unlock_exit;
3293 }
3294
3295 for (i = start_i; i < num_pages; i++) {
3296 page = extent_buffer_page(eb, i);
3297 if (inc_all_pages)
3298 page_cache_get(page);
3299 if (!PageUptodate(page)) {
3300 if (start_i == 0)
3301 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003302 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003303 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003304 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003305 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003306 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003307 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003308 } else {
3309 unlock_page(page);
3310 }
3311 }
3312
Chris Masona86c12c2008-02-07 10:50:54 -05003313 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003314 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003315
Chris Masond3977122009-01-05 21:25:51 -05003316 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003317 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003318
Chris Masond1310b22008-01-24 16:13:08 -05003319 for (i = start_i; i < num_pages; i++) {
3320 page = extent_buffer_page(eb, i);
3321 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003322 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003323 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003324 }
Chris Masond3977122009-01-05 21:25:51 -05003325
Chris Masond1310b22008-01-24 16:13:08 -05003326 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003327 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003328 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003329
3330unlock_exit:
3331 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003332 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003333 page = extent_buffer_page(eb, i);
3334 i++;
3335 unlock_page(page);
3336 locked_pages--;
3337 }
3338 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003339}
Chris Masond1310b22008-01-24 16:13:08 -05003340
3341void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3342 unsigned long start,
3343 unsigned long len)
3344{
3345 size_t cur;
3346 size_t offset;
3347 struct page *page;
3348 char *kaddr;
3349 char *dst = (char *)dstv;
3350 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3351 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003352
3353 WARN_ON(start > eb->len);
3354 WARN_ON(start + len > eb->start + eb->len);
3355
3356 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3357
Chris Masond3977122009-01-05 21:25:51 -05003358 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003359 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003360
3361 cur = min(len, (PAGE_CACHE_SIZE - offset));
3362 kaddr = kmap_atomic(page, KM_USER1);
3363 memcpy(dst, kaddr + offset, cur);
3364 kunmap_atomic(kaddr, KM_USER1);
3365
3366 dst += cur;
3367 len -= cur;
3368 offset = 0;
3369 i++;
3370 }
3371}
Chris Masond1310b22008-01-24 16:13:08 -05003372
3373int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3374 unsigned long min_len, char **token, char **map,
3375 unsigned long *map_start,
3376 unsigned long *map_len, int km)
3377{
3378 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3379 char *kaddr;
3380 struct page *p;
3381 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3382 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3383 unsigned long end_i = (start_offset + start + min_len - 1) >>
3384 PAGE_CACHE_SHIFT;
3385
3386 if (i != end_i)
3387 return -EINVAL;
3388
3389 if (i == 0) {
3390 offset = start_offset;
3391 *map_start = 0;
3392 } else {
3393 offset = 0;
3394 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3395 }
Chris Masond3977122009-01-05 21:25:51 -05003396
Chris Masond1310b22008-01-24 16:13:08 -05003397 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003398 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3399 "wanted %lu %lu\n", (unsigned long long)eb->start,
3400 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003401 WARN_ON(1);
3402 }
3403
3404 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003405 kaddr = kmap_atomic(p, km);
3406 *token = kaddr;
3407 *map = kaddr + offset;
3408 *map_len = PAGE_CACHE_SIZE - offset;
3409 return 0;
3410}
Chris Masond1310b22008-01-24 16:13:08 -05003411
3412int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3413 unsigned long min_len,
3414 char **token, char **map,
3415 unsigned long *map_start,
3416 unsigned long *map_len, int km)
3417{
3418 int err;
3419 int save = 0;
3420 if (eb->map_token) {
3421 unmap_extent_buffer(eb, eb->map_token, km);
3422 eb->map_token = NULL;
3423 save = 1;
3424 }
3425 err = map_private_extent_buffer(eb, start, min_len, token, map,
3426 map_start, map_len, km);
3427 if (!err && save) {
3428 eb->map_token = *token;
3429 eb->kaddr = *map;
3430 eb->map_start = *map_start;
3431 eb->map_len = *map_len;
3432 }
3433 return err;
3434}
Chris Masond1310b22008-01-24 16:13:08 -05003435
3436void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3437{
3438 kunmap_atomic(token, km);
3439}
Chris Masond1310b22008-01-24 16:13:08 -05003440
3441int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3442 unsigned long start,
3443 unsigned long len)
3444{
3445 size_t cur;
3446 size_t offset;
3447 struct page *page;
3448 char *kaddr;
3449 char *ptr = (char *)ptrv;
3450 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3451 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3452 int ret = 0;
3453
3454 WARN_ON(start > eb->len);
3455 WARN_ON(start + len > eb->start + eb->len);
3456
3457 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3458
Chris Masond3977122009-01-05 21:25:51 -05003459 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003460 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003461
3462 cur = min(len, (PAGE_CACHE_SIZE - offset));
3463
3464 kaddr = kmap_atomic(page, KM_USER0);
3465 ret = memcmp(ptr, kaddr + offset, cur);
3466 kunmap_atomic(kaddr, KM_USER0);
3467 if (ret)
3468 break;
3469
3470 ptr += cur;
3471 len -= cur;
3472 offset = 0;
3473 i++;
3474 }
3475 return ret;
3476}
Chris Masond1310b22008-01-24 16:13:08 -05003477
3478void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3479 unsigned long start, unsigned long len)
3480{
3481 size_t cur;
3482 size_t offset;
3483 struct page *page;
3484 char *kaddr;
3485 char *src = (char *)srcv;
3486 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3487 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3488
3489 WARN_ON(start > eb->len);
3490 WARN_ON(start + len > eb->start + eb->len);
3491
3492 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3493
Chris Masond3977122009-01-05 21:25:51 -05003494 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003495 page = extent_buffer_page(eb, i);
3496 WARN_ON(!PageUptodate(page));
3497
3498 cur = min(len, PAGE_CACHE_SIZE - offset);
3499 kaddr = kmap_atomic(page, KM_USER1);
3500 memcpy(kaddr + offset, src, cur);
3501 kunmap_atomic(kaddr, KM_USER1);
3502
3503 src += cur;
3504 len -= cur;
3505 offset = 0;
3506 i++;
3507 }
3508}
Chris Masond1310b22008-01-24 16:13:08 -05003509
3510void memset_extent_buffer(struct extent_buffer *eb, char c,
3511 unsigned long start, unsigned long len)
3512{
3513 size_t cur;
3514 size_t offset;
3515 struct page *page;
3516 char *kaddr;
3517 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3518 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3519
3520 WARN_ON(start > eb->len);
3521 WARN_ON(start + len > eb->start + eb->len);
3522
3523 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3524
Chris Masond3977122009-01-05 21:25:51 -05003525 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003526 page = extent_buffer_page(eb, i);
3527 WARN_ON(!PageUptodate(page));
3528
3529 cur = min(len, PAGE_CACHE_SIZE - offset);
3530 kaddr = kmap_atomic(page, KM_USER0);
3531 memset(kaddr + offset, c, cur);
3532 kunmap_atomic(kaddr, KM_USER0);
3533
3534 len -= cur;
3535 offset = 0;
3536 i++;
3537 }
3538}
Chris Masond1310b22008-01-24 16:13:08 -05003539
3540void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3541 unsigned long dst_offset, unsigned long src_offset,
3542 unsigned long len)
3543{
3544 u64 dst_len = dst->len;
3545 size_t cur;
3546 size_t offset;
3547 struct page *page;
3548 char *kaddr;
3549 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3550 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3551
3552 WARN_ON(src->len != dst_len);
3553
3554 offset = (start_offset + dst_offset) &
3555 ((unsigned long)PAGE_CACHE_SIZE - 1);
3556
Chris Masond3977122009-01-05 21:25:51 -05003557 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003558 page = extent_buffer_page(dst, i);
3559 WARN_ON(!PageUptodate(page));
3560
3561 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3562
3563 kaddr = kmap_atomic(page, KM_USER0);
3564 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3565 kunmap_atomic(kaddr, KM_USER0);
3566
3567 src_offset += cur;
3568 len -= cur;
3569 offset = 0;
3570 i++;
3571 }
3572}
Chris Masond1310b22008-01-24 16:13:08 -05003573
3574static void move_pages(struct page *dst_page, struct page *src_page,
3575 unsigned long dst_off, unsigned long src_off,
3576 unsigned long len)
3577{
3578 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3579 if (dst_page == src_page) {
3580 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3581 } else {
3582 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3583 char *p = dst_kaddr + dst_off + len;
3584 char *s = src_kaddr + src_off + len;
3585
3586 while (len--)
3587 *--p = *--s;
3588
3589 kunmap_atomic(src_kaddr, KM_USER1);
3590 }
3591 kunmap_atomic(dst_kaddr, KM_USER0);
3592}
3593
3594static void copy_pages(struct page *dst_page, struct page *src_page,
3595 unsigned long dst_off, unsigned long src_off,
3596 unsigned long len)
3597{
3598 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3599 char *src_kaddr;
3600
3601 if (dst_page != src_page)
3602 src_kaddr = kmap_atomic(src_page, KM_USER1);
3603 else
3604 src_kaddr = dst_kaddr;
3605
3606 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3607 kunmap_atomic(dst_kaddr, KM_USER0);
3608 if (dst_page != src_page)
3609 kunmap_atomic(src_kaddr, KM_USER1);
3610}
3611
3612void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3613 unsigned long src_offset, unsigned long len)
3614{
3615 size_t cur;
3616 size_t dst_off_in_page;
3617 size_t src_off_in_page;
3618 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3619 unsigned long dst_i;
3620 unsigned long src_i;
3621
3622 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003623 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3624 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003625 BUG_ON(1);
3626 }
3627 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003628 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3629 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003630 BUG_ON(1);
3631 }
3632
Chris Masond3977122009-01-05 21:25:51 -05003633 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003634 dst_off_in_page = (start_offset + dst_offset) &
3635 ((unsigned long)PAGE_CACHE_SIZE - 1);
3636 src_off_in_page = (start_offset + src_offset) &
3637 ((unsigned long)PAGE_CACHE_SIZE - 1);
3638
3639 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3640 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3641
3642 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3643 src_off_in_page));
3644 cur = min_t(unsigned long, cur,
3645 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3646
3647 copy_pages(extent_buffer_page(dst, dst_i),
3648 extent_buffer_page(dst, src_i),
3649 dst_off_in_page, src_off_in_page, cur);
3650
3651 src_offset += cur;
3652 dst_offset += cur;
3653 len -= cur;
3654 }
3655}
Chris Masond1310b22008-01-24 16:13:08 -05003656
3657void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3658 unsigned long src_offset, unsigned long len)
3659{
3660 size_t cur;
3661 size_t dst_off_in_page;
3662 size_t src_off_in_page;
3663 unsigned long dst_end = dst_offset + len - 1;
3664 unsigned long src_end = src_offset + len - 1;
3665 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3666 unsigned long dst_i;
3667 unsigned long src_i;
3668
3669 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003670 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3671 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003672 BUG_ON(1);
3673 }
3674 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003675 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3676 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003677 BUG_ON(1);
3678 }
3679 if (dst_offset < src_offset) {
3680 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3681 return;
3682 }
Chris Masond3977122009-01-05 21:25:51 -05003683 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003684 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3685 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3686
3687 dst_off_in_page = (start_offset + dst_end) &
3688 ((unsigned long)PAGE_CACHE_SIZE - 1);
3689 src_off_in_page = (start_offset + src_end) &
3690 ((unsigned long)PAGE_CACHE_SIZE - 1);
3691
3692 cur = min_t(unsigned long, len, src_off_in_page + 1);
3693 cur = min(cur, dst_off_in_page + 1);
3694 move_pages(extent_buffer_page(dst, dst_i),
3695 extent_buffer_page(dst, src_i),
3696 dst_off_in_page - cur + 1,
3697 src_off_in_page - cur + 1, cur);
3698
3699 dst_end -= cur;
3700 src_end -= cur;
3701 len -= cur;
3702 }
3703}
Chris Mason6af118ce2008-07-22 11:18:07 -04003704
3705int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3706{
3707 u64 start = page_offset(page);
3708 struct extent_buffer *eb;
3709 int ret = 1;
3710 unsigned long i;
3711 unsigned long num_pages;
3712
3713 spin_lock(&tree->buffer_lock);
3714 eb = buffer_search(tree, start);
3715 if (!eb)
3716 goto out;
3717
3718 if (atomic_read(&eb->refs) > 1) {
3719 ret = 0;
3720 goto out;
3721 }
Chris Masonb9473432009-03-13 11:00:37 -04003722 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3723 ret = 0;
3724 goto out;
3725 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003726 /* at this point we can safely release the extent buffer */
3727 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003728 for (i = 0; i < num_pages; i++)
3729 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003730 rb_erase(&eb->rb_node, &tree->buffer);
3731 __free_extent_buffer(eb);
3732out:
3733 spin_unlock(&tree->buffer_lock);
3734 return ret;
3735}