blob: 41cf1b451b412ce5defdbab88b00aa2d320144cd [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 Masonb0c68f82008-01-31 11:05:37 -0500370 set_state_cb(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500371 state->state |= bits;
372 state->start = start;
373 state->end = end;
374 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/*
657 * set some bits on a range in the tree. This may require allocations
658 * or sleeping, so the gfp mask is used to indicate what is allowed.
659 *
660 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
661 * range already has the desired bits set. The start of the existing
662 * range is returned in failed_start in this case.
663 *
664 * [start, end] is inclusive
665 * This takes the tree lock.
666 */
Chris Masond3977122009-01-05 21:25:51 -0500667static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
668 int bits, int exclusive, u64 *failed_start,
669 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;
675 int set;
676 u64 last_start;
677 u64 last_end;
678again:
679 if (!prealloc && (mask & __GFP_WAIT)) {
680 prealloc = alloc_extent_state(mask);
681 if (!prealloc)
682 return -ENOMEM;
683 }
684
Chris Masoncad321a2008-12-17 14:51:42 -0500685 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500686 /*
687 * this search will find all the extents that end after
688 * our range starts.
689 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500690 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500691 if (!node) {
692 err = insert_state(tree, prealloc, start, end, bits);
693 prealloc = NULL;
694 BUG_ON(err == -EEXIST);
695 goto out;
696 }
Chris Masond1310b22008-01-24 16:13:08 -0500697 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400698hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500699 last_start = state->start;
700 last_end = state->end;
701
702 /*
703 * | ---- desired range ---- |
704 * | state |
705 *
706 * Just lock what we found and keep going
707 */
708 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400709 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500710 set = state->state & bits;
711 if (set && exclusive) {
712 *failed_start = state->start;
713 err = -EEXIST;
714 goto out;
715 }
716 set_state_bits(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500717 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400718 if (last_end == (u64)-1)
719 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400720
Yan Zheng5c939df2009-05-27 09:16:03 -0400721 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400722 if (start < end && prealloc && !need_resched()) {
723 next_node = rb_next(node);
724 if (next_node) {
725 state = rb_entry(next_node, struct extent_state,
726 rb_node);
727 if (state->start == start)
728 goto hit_next;
729 }
730 }
Chris Masond1310b22008-01-24 16:13:08 -0500731 goto search_again;
732 }
733
734 /*
735 * | ---- desired range ---- |
736 * | state |
737 * or
738 * | ------------- state -------------- |
739 *
740 * We need to split the extent we found, and may flip bits on
741 * second half.
742 *
743 * If the extent we found extends past our
744 * range, we just split and search again. It'll get split
745 * again the next time though.
746 *
747 * If the extent we found is inside our range, we set the
748 * desired bit on it.
749 */
750 if (state->start < start) {
751 set = state->state & bits;
752 if (exclusive && set) {
753 *failed_start = start;
754 err = -EEXIST;
755 goto out;
756 }
757 err = split_state(tree, state, prealloc, start);
758 BUG_ON(err == -EEXIST);
759 prealloc = NULL;
760 if (err)
761 goto out;
762 if (state->end <= end) {
763 set_state_bits(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500764 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400765 if (last_end == (u64)-1)
766 goto out;
767 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500768 } else {
769 start = state->start;
770 }
771 goto search_again;
772 }
773 /*
774 * | ---- desired range ---- |
775 * | state | or | state |
776 *
777 * There's a hole, we need to insert something in it and
778 * ignore the extent we found.
779 */
780 if (state->start > start) {
781 u64 this_end;
782 if (end < last_start)
783 this_end = end;
784 else
Chris Masond3977122009-01-05 21:25:51 -0500785 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500786 err = insert_state(tree, prealloc, start, this_end,
787 bits);
788 prealloc = NULL;
789 BUG_ON(err == -EEXIST);
790 if (err)
791 goto out;
792 start = this_end + 1;
793 goto search_again;
794 }
795 /*
796 * | ---- desired range ---- |
797 * | state |
798 * We need to split the extent, and set the bit
799 * on the first half
800 */
801 if (state->start <= end && state->end > end) {
802 set = state->state & bits;
803 if (exclusive && set) {
804 *failed_start = start;
805 err = -EEXIST;
806 goto out;
807 }
808 err = split_state(tree, state, prealloc, end + 1);
809 BUG_ON(err == -EEXIST);
810
811 set_state_bits(tree, prealloc, bits);
812 merge_state(tree, prealloc);
813 prealloc = NULL;
814 goto out;
815 }
816
817 goto search_again;
818
819out:
Chris Masoncad321a2008-12-17 14:51:42 -0500820 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500821 if (prealloc)
822 free_extent_state(prealloc);
823
824 return err;
825
826search_again:
827 if (start > end)
828 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500829 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500830 if (mask & __GFP_WAIT)
831 cond_resched();
832 goto again;
833}
Chris Masond1310b22008-01-24 16:13:08 -0500834
835/* wrappers around set/clear extent bit */
836int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
837 gfp_t mask)
838{
839 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
840 mask);
841}
Chris Masond1310b22008-01-24 16:13:08 -0500842
Chris Masone6dcd2d2008-07-17 12:53:50 -0400843int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
844 gfp_t mask)
845{
846 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
847}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400848
Chris Masond1310b22008-01-24 16:13:08 -0500849int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
850 int bits, gfp_t mask)
851{
852 return set_extent_bit(tree, start, end, bits, 0, NULL,
853 mask);
854}
Chris Masond1310b22008-01-24 16:13:08 -0500855
856int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
857 int bits, gfp_t mask)
858{
859 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
860}
Chris Masond1310b22008-01-24 16:13:08 -0500861
862int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
863 gfp_t mask)
864{
865 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400866 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400867 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500868}
Chris Masond1310b22008-01-24 16:13:08 -0500869
870int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
871 gfp_t mask)
872{
873 return clear_extent_bit(tree, start, end,
874 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
875}
Chris Masond1310b22008-01-24 16:13:08 -0500876
Chris Masone6dcd2d2008-07-17 12:53:50 -0400877int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
878 gfp_t mask)
879{
880 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
881}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400882
Chris Masond1310b22008-01-24 16:13:08 -0500883int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
884 gfp_t mask)
885{
886 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
887 mask);
888}
Chris Masond1310b22008-01-24 16:13:08 -0500889
Christoph Hellwigb2950862008-12-02 09:54:17 -0500890static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500891 gfp_t mask)
892{
893 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
894}
Chris Masond1310b22008-01-24 16:13:08 -0500895
896int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
897 gfp_t mask)
898{
899 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
900 mask);
901}
Chris Masond1310b22008-01-24 16:13:08 -0500902
Chris Masond3977122009-01-05 21:25:51 -0500903static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
904 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500905{
906 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
907}
Chris Masond1310b22008-01-24 16:13:08 -0500908
Christoph Hellwigb2950862008-12-02 09:54:17 -0500909static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500910 gfp_t mask)
911{
912 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
913 0, NULL, mask);
914}
Chris Masond1310b22008-01-24 16:13:08 -0500915
Chris Masond3977122009-01-05 21:25:51 -0500916static int clear_extent_writeback(struct extent_io_tree *tree, u64 start,
917 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500918{
919 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
920}
Chris Masond1310b22008-01-24 16:13:08 -0500921
922int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
923{
924 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
925}
Chris Masond1310b22008-01-24 16:13:08 -0500926
Chris Masond352ac62008-09-29 15:18:18 -0400927/*
928 * either insert or lock state struct between start and end use mask to tell
929 * us if waiting is desired.
930 */
Chris Masond1310b22008-01-24 16:13:08 -0500931int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
932{
933 int err;
934 u64 failed_start;
935 while (1) {
936 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
937 &failed_start, mask);
938 if (err == -EEXIST && (mask & __GFP_WAIT)) {
939 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
940 start = failed_start;
941 } else {
942 break;
943 }
944 WARN_ON(start > end);
945 }
946 return err;
947}
Chris Masond1310b22008-01-24 16:13:08 -0500948
Josef Bacik25179202008-10-29 14:49:05 -0400949int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
950 gfp_t mask)
951{
952 int err;
953 u64 failed_start;
954
955 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
956 &failed_start, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400957 if (err == -EEXIST) {
958 if (failed_start > start)
959 clear_extent_bit(tree, start, failed_start - 1,
960 EXTENT_LOCKED, 1, 0, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400961 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400962 }
Josef Bacik25179202008-10-29 14:49:05 -0400963 return 1;
964}
Josef Bacik25179202008-10-29 14:49:05 -0400965
Chris Masond1310b22008-01-24 16:13:08 -0500966int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
967 gfp_t mask)
968{
969 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
970}
Chris Masond1310b22008-01-24 16:13:08 -0500971
972/*
973 * helper function to set pages and extents in the tree dirty
974 */
975int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
976{
977 unsigned long index = start >> PAGE_CACHE_SHIFT;
978 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
979 struct page *page;
980
981 while (index <= end_index) {
982 page = find_get_page(tree->mapping, index);
983 BUG_ON(!page);
984 __set_page_dirty_nobuffers(page);
985 page_cache_release(page);
986 index++;
987 }
988 set_extent_dirty(tree, start, end, GFP_NOFS);
989 return 0;
990}
Chris Masond1310b22008-01-24 16:13:08 -0500991
992/*
993 * helper function to set both pages and extents in the tree writeback
994 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500995static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -0500996{
997 unsigned long index = start >> PAGE_CACHE_SHIFT;
998 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
999 struct page *page;
1000
1001 while (index <= end_index) {
1002 page = find_get_page(tree->mapping, index);
1003 BUG_ON(!page);
1004 set_page_writeback(page);
1005 page_cache_release(page);
1006 index++;
1007 }
1008 set_extent_writeback(tree, start, end, GFP_NOFS);
1009 return 0;
1010}
Chris Masond1310b22008-01-24 16:13:08 -05001011
Chris Masond352ac62008-09-29 15:18:18 -04001012/*
1013 * find the first offset in the io tree with 'bits' set. zero is
1014 * returned if we find something, and *start_ret and *end_ret are
1015 * set to reflect the state struct that was found.
1016 *
1017 * If nothing was found, 1 is returned, < 0 on error
1018 */
Chris Masond1310b22008-01-24 16:13:08 -05001019int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1020 u64 *start_ret, u64 *end_ret, int bits)
1021{
1022 struct rb_node *node;
1023 struct extent_state *state;
1024 int ret = 1;
1025
Chris Masoncad321a2008-12-17 14:51:42 -05001026 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001027 /*
1028 * this search will find all the extents that end after
1029 * our range starts.
1030 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001031 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001032 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001033 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001034
Chris Masond3977122009-01-05 21:25:51 -05001035 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001036 state = rb_entry(node, struct extent_state, rb_node);
1037 if (state->end >= start && (state->state & bits)) {
1038 *start_ret = state->start;
1039 *end_ret = state->end;
1040 ret = 0;
1041 break;
1042 }
1043 node = rb_next(node);
1044 if (!node)
1045 break;
1046 }
1047out:
Chris Masoncad321a2008-12-17 14:51:42 -05001048 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001049 return ret;
1050}
Chris Masond1310b22008-01-24 16:13:08 -05001051
Chris Masond352ac62008-09-29 15:18:18 -04001052/* find the first state struct with 'bits' set after 'start', and
1053 * return it. tree->lock must be held. NULL will returned if
1054 * nothing was found after 'start'
1055 */
Chris Masond7fc6402008-02-18 12:12:38 -05001056struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1057 u64 start, int bits)
1058{
1059 struct rb_node *node;
1060 struct extent_state *state;
1061
1062 /*
1063 * this search will find all the extents that end after
1064 * our range starts.
1065 */
1066 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001067 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001068 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001069
Chris Masond3977122009-01-05 21:25:51 -05001070 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001071 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001072 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001073 return state;
Chris Masond3977122009-01-05 21:25:51 -05001074
Chris Masond7fc6402008-02-18 12:12:38 -05001075 node = rb_next(node);
1076 if (!node)
1077 break;
1078 }
1079out:
1080 return NULL;
1081}
Chris Masond7fc6402008-02-18 12:12:38 -05001082
Chris Masond352ac62008-09-29 15:18:18 -04001083/*
1084 * find a contiguous range of bytes in the file marked as delalloc, not
1085 * more than 'max_bytes'. start and end are used to return the range,
1086 *
1087 * 1 is returned if we find something, 0 if nothing was in the tree
1088 */
Chris Masonc8b97812008-10-29 14:49:59 -04001089static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1090 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001091{
1092 struct rb_node *node;
1093 struct extent_state *state;
1094 u64 cur_start = *start;
1095 u64 found = 0;
1096 u64 total_bytes = 0;
1097
Chris Masoncad321a2008-12-17 14:51:42 -05001098 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001099
Chris Masond1310b22008-01-24 16:13:08 -05001100 /*
1101 * this search will find all the extents that end after
1102 * our range starts.
1103 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001104 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001105 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001106 if (!found)
1107 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001108 goto out;
1109 }
1110
Chris Masond3977122009-01-05 21:25:51 -05001111 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001112 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001113 if (found && (state->start != cur_start ||
1114 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001115 goto out;
1116 }
1117 if (!(state->state & EXTENT_DELALLOC)) {
1118 if (!found)
1119 *end = state->end;
1120 goto out;
1121 }
Chris Masond1310b22008-01-24 16:13:08 -05001122 if (!found)
1123 *start = state->start;
1124 found++;
1125 *end = state->end;
1126 cur_start = state->end + 1;
1127 node = rb_next(node);
1128 if (!node)
1129 break;
1130 total_bytes += state->end - state->start + 1;
1131 if (total_bytes >= max_bytes)
1132 break;
1133 }
1134out:
Chris Masoncad321a2008-12-17 14:51:42 -05001135 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001136 return found;
1137}
1138
Chris Masonc8b97812008-10-29 14:49:59 -04001139static noinline int __unlock_for_delalloc(struct inode *inode,
1140 struct page *locked_page,
1141 u64 start, u64 end)
1142{
1143 int ret;
1144 struct page *pages[16];
1145 unsigned long index = start >> PAGE_CACHE_SHIFT;
1146 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1147 unsigned long nr_pages = end_index - index + 1;
1148 int i;
1149
1150 if (index == locked_page->index && end_index == index)
1151 return 0;
1152
Chris Masond3977122009-01-05 21:25:51 -05001153 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001154 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001155 min_t(unsigned long, nr_pages,
1156 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001157 for (i = 0; i < ret; i++) {
1158 if (pages[i] != locked_page)
1159 unlock_page(pages[i]);
1160 page_cache_release(pages[i]);
1161 }
1162 nr_pages -= ret;
1163 index += ret;
1164 cond_resched();
1165 }
1166 return 0;
1167}
1168
1169static noinline int lock_delalloc_pages(struct inode *inode,
1170 struct page *locked_page,
1171 u64 delalloc_start,
1172 u64 delalloc_end)
1173{
1174 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1175 unsigned long start_index = index;
1176 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1177 unsigned long pages_locked = 0;
1178 struct page *pages[16];
1179 unsigned long nrpages;
1180 int ret;
1181 int i;
1182
1183 /* the caller is responsible for locking the start index */
1184 if (index == locked_page->index && index == end_index)
1185 return 0;
1186
1187 /* skip the page at the start index */
1188 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001189 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001190 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001191 min_t(unsigned long,
1192 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001193 if (ret == 0) {
1194 ret = -EAGAIN;
1195 goto done;
1196 }
1197 /* now we have an array of pages, lock them all */
1198 for (i = 0; i < ret; i++) {
1199 /*
1200 * the caller is taking responsibility for
1201 * locked_page
1202 */
Chris Mason771ed682008-11-06 22:02:51 -05001203 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001204 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001205 if (!PageDirty(pages[i]) ||
1206 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001207 ret = -EAGAIN;
1208 unlock_page(pages[i]);
1209 page_cache_release(pages[i]);
1210 goto done;
1211 }
1212 }
Chris Masonc8b97812008-10-29 14:49:59 -04001213 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001214 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001215 }
Chris Masonc8b97812008-10-29 14:49:59 -04001216 nrpages -= ret;
1217 index += ret;
1218 cond_resched();
1219 }
1220 ret = 0;
1221done:
1222 if (ret && pages_locked) {
1223 __unlock_for_delalloc(inode, locked_page,
1224 delalloc_start,
1225 ((u64)(start_index + pages_locked - 1)) <<
1226 PAGE_CACHE_SHIFT);
1227 }
1228 return ret;
1229}
1230
1231/*
1232 * find a contiguous range of bytes in the file marked as delalloc, not
1233 * more than 'max_bytes'. start and end are used to return the range,
1234 *
1235 * 1 is returned if we find something, 0 if nothing was in the tree
1236 */
1237static noinline u64 find_lock_delalloc_range(struct inode *inode,
1238 struct extent_io_tree *tree,
1239 struct page *locked_page,
1240 u64 *start, u64 *end,
1241 u64 max_bytes)
1242{
1243 u64 delalloc_start;
1244 u64 delalloc_end;
1245 u64 found;
1246 int ret;
1247 int loops = 0;
1248
1249again:
1250 /* step one, find a bunch of delalloc bytes starting at start */
1251 delalloc_start = *start;
1252 delalloc_end = 0;
1253 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1254 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001255 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001256 *start = delalloc_start;
1257 *end = delalloc_end;
1258 return found;
1259 }
1260
1261 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001262 * start comes from the offset of locked_page. We have to lock
1263 * pages in order, so we can't process delalloc bytes before
1264 * locked_page
1265 */
Chris Masond3977122009-01-05 21:25:51 -05001266 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001267 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001268
1269 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001270 * make sure to limit the number of pages we try to lock down
1271 * if we're looping.
1272 */
Chris Masond3977122009-01-05 21:25:51 -05001273 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001274 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001275
Chris Masonc8b97812008-10-29 14:49:59 -04001276 /* step two, lock all the pages after the page that has start */
1277 ret = lock_delalloc_pages(inode, locked_page,
1278 delalloc_start, delalloc_end);
1279 if (ret == -EAGAIN) {
1280 /* some of the pages are gone, lets avoid looping by
1281 * shortening the size of the delalloc range we're searching
1282 */
1283 if (!loops) {
1284 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1285 max_bytes = PAGE_CACHE_SIZE - offset;
1286 loops = 1;
1287 goto again;
1288 } else {
1289 found = 0;
1290 goto out_failed;
1291 }
1292 }
1293 BUG_ON(ret);
1294
1295 /* step three, lock the state bits for the whole range */
1296 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1297
1298 /* then test to make sure it is all still delalloc */
1299 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1300 EXTENT_DELALLOC, 1);
1301 if (!ret) {
1302 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1303 __unlock_for_delalloc(inode, locked_page,
1304 delalloc_start, delalloc_end);
1305 cond_resched();
1306 goto again;
1307 }
1308 *start = delalloc_start;
1309 *end = delalloc_end;
1310out_failed:
1311 return found;
1312}
1313
1314int extent_clear_unlock_delalloc(struct inode *inode,
1315 struct extent_io_tree *tree,
1316 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001317 int unlock_pages,
1318 int clear_unlock,
1319 int clear_delalloc, int clear_dirty,
1320 int set_writeback,
Chris Masonc8b97812008-10-29 14:49:59 -04001321 int end_writeback)
1322{
1323 int ret;
1324 struct page *pages[16];
1325 unsigned long index = start >> PAGE_CACHE_SHIFT;
1326 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1327 unsigned long nr_pages = end_index - index + 1;
1328 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001329 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001330
Chris Mason771ed682008-11-06 22:02:51 -05001331 if (clear_unlock)
1332 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001333 if (clear_dirty)
1334 clear_bits |= EXTENT_DIRTY;
1335
Chris Mason771ed682008-11-06 22:02:51 -05001336 if (clear_delalloc)
1337 clear_bits |= EXTENT_DELALLOC;
1338
Chris Masonc8b97812008-10-29 14:49:59 -04001339 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05001340 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1341 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001342
Chris Masond3977122009-01-05 21:25:51 -05001343 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001344 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001345 min_t(unsigned long,
1346 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001347 for (i = 0; i < ret; i++) {
1348 if (pages[i] == locked_page) {
1349 page_cache_release(pages[i]);
1350 continue;
1351 }
1352 if (clear_dirty)
1353 clear_page_dirty_for_io(pages[i]);
1354 if (set_writeback)
1355 set_page_writeback(pages[i]);
1356 if (end_writeback)
1357 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001358 if (unlock_pages)
1359 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001360 page_cache_release(pages[i]);
1361 }
1362 nr_pages -= ret;
1363 index += ret;
1364 cond_resched();
1365 }
1366 return 0;
1367}
Chris Masonc8b97812008-10-29 14:49:59 -04001368
Chris Masond352ac62008-09-29 15:18:18 -04001369/*
1370 * count the number of bytes in the tree that have a given bit(s)
1371 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1372 * cached. The total number found is returned.
1373 */
Chris Masond1310b22008-01-24 16:13:08 -05001374u64 count_range_bits(struct extent_io_tree *tree,
1375 u64 *start, u64 search_end, u64 max_bytes,
1376 unsigned long bits)
1377{
1378 struct rb_node *node;
1379 struct extent_state *state;
1380 u64 cur_start = *start;
1381 u64 total_bytes = 0;
1382 int found = 0;
1383
1384 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001385 WARN_ON(1);
1386 return 0;
1387 }
1388
Chris Masoncad321a2008-12-17 14:51:42 -05001389 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001390 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1391 total_bytes = tree->dirty_bytes;
1392 goto out;
1393 }
1394 /*
1395 * this search will find all the extents that end after
1396 * our range starts.
1397 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001398 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001399 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001400 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001401
Chris Masond3977122009-01-05 21:25:51 -05001402 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001403 state = rb_entry(node, struct extent_state, rb_node);
1404 if (state->start > search_end)
1405 break;
1406 if (state->end >= cur_start && (state->state & bits)) {
1407 total_bytes += min(search_end, state->end) + 1 -
1408 max(cur_start, state->start);
1409 if (total_bytes >= max_bytes)
1410 break;
1411 if (!found) {
1412 *start = state->start;
1413 found = 1;
1414 }
1415 }
1416 node = rb_next(node);
1417 if (!node)
1418 break;
1419 }
1420out:
Chris Masoncad321a2008-12-17 14:51:42 -05001421 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001422 return total_bytes;
1423}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001424
Chris Masond352ac62008-09-29 15:18:18 -04001425/*
1426 * set the private field for a given byte offset in the tree. If there isn't
1427 * an extent_state there already, this does nothing.
1428 */
Chris Masond1310b22008-01-24 16:13:08 -05001429int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1430{
1431 struct rb_node *node;
1432 struct extent_state *state;
1433 int ret = 0;
1434
Chris Masoncad321a2008-12-17 14:51:42 -05001435 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001436 /*
1437 * this search will find all the extents that end after
1438 * our range starts.
1439 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001440 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001441 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001442 ret = -ENOENT;
1443 goto out;
1444 }
1445 state = rb_entry(node, struct extent_state, rb_node);
1446 if (state->start != start) {
1447 ret = -ENOENT;
1448 goto out;
1449 }
1450 state->private = private;
1451out:
Chris Masoncad321a2008-12-17 14:51:42 -05001452 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001453 return ret;
1454}
1455
1456int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1457{
1458 struct rb_node *node;
1459 struct extent_state *state;
1460 int ret = 0;
1461
Chris Masoncad321a2008-12-17 14:51:42 -05001462 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001463 /*
1464 * this search will find all the extents that end after
1465 * our range starts.
1466 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001467 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001468 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001469 ret = -ENOENT;
1470 goto out;
1471 }
1472 state = rb_entry(node, struct extent_state, rb_node);
1473 if (state->start != start) {
1474 ret = -ENOENT;
1475 goto out;
1476 }
1477 *private = state->private;
1478out:
Chris Masoncad321a2008-12-17 14:51:42 -05001479 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001480 return ret;
1481}
1482
1483/*
1484 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001485 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001486 * has the bits set. Otherwise, 1 is returned if any bit in the
1487 * range is found set.
1488 */
1489int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1490 int bits, int filled)
1491{
1492 struct extent_state *state = NULL;
1493 struct rb_node *node;
1494 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001495
Chris Masoncad321a2008-12-17 14:51:42 -05001496 spin_lock(&tree->lock);
Chris Mason80ea96b2008-02-01 14:51:59 -05001497 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001498 while (node && start <= end) {
1499 state = rb_entry(node, struct extent_state, rb_node);
1500
1501 if (filled && state->start > start) {
1502 bitset = 0;
1503 break;
1504 }
1505
1506 if (state->start > end)
1507 break;
1508
1509 if (state->state & bits) {
1510 bitset = 1;
1511 if (!filled)
1512 break;
1513 } else if (filled) {
1514 bitset = 0;
1515 break;
1516 }
1517 start = state->end + 1;
1518 if (start > end)
1519 break;
1520 node = rb_next(node);
1521 if (!node) {
1522 if (filled)
1523 bitset = 0;
1524 break;
1525 }
1526 }
Chris Masoncad321a2008-12-17 14:51:42 -05001527 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001528 return bitset;
1529}
Chris Masond1310b22008-01-24 16:13:08 -05001530
1531/*
1532 * helper function to set a given page up to date if all the
1533 * extents in the tree for that page are up to date
1534 */
1535static int check_page_uptodate(struct extent_io_tree *tree,
1536 struct page *page)
1537{
1538 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1539 u64 end = start + PAGE_CACHE_SIZE - 1;
1540 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1541 SetPageUptodate(page);
1542 return 0;
1543}
1544
1545/*
1546 * helper function to unlock a page if all the extents in the tree
1547 * for that page are unlocked
1548 */
1549static int check_page_locked(struct extent_io_tree *tree,
1550 struct page *page)
1551{
1552 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1553 u64 end = start + PAGE_CACHE_SIZE - 1;
1554 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1555 unlock_page(page);
1556 return 0;
1557}
1558
1559/*
1560 * helper function to end page writeback if all the extents
1561 * in the tree for that page are done with writeback
1562 */
1563static int check_page_writeback(struct extent_io_tree *tree,
1564 struct page *page)
1565{
1566 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1567 u64 end = start + PAGE_CACHE_SIZE - 1;
1568 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1569 end_page_writeback(page);
1570 return 0;
1571}
1572
1573/* lots and lots of room for performance fixes in the end_bio funcs */
1574
1575/*
1576 * after a writepage IO is done, we need to:
1577 * clear the uptodate bits on error
1578 * clear the writeback bits in the extent tree for this IO
1579 * end_page_writeback if the page has no more pending IO
1580 *
1581 * Scheduling is not allowed, so the extent state tree is expected
1582 * to have one and only one object corresponding to this IO.
1583 */
Chris Masond1310b22008-01-24 16:13:08 -05001584static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001585{
Chris Mason1259ab72008-05-12 13:39:03 -04001586 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001587 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001588 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001589 u64 start;
1590 u64 end;
1591 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001592 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001593
Chris Masond1310b22008-01-24 16:13:08 -05001594 do {
1595 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001596 tree = &BTRFS_I(page->mapping->host)->io_tree;
1597
Chris Masond1310b22008-01-24 16:13:08 -05001598 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1599 bvec->bv_offset;
1600 end = start + bvec->bv_len - 1;
1601
1602 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1603 whole_page = 1;
1604 else
1605 whole_page = 0;
1606
1607 if (--bvec >= bio->bi_io_vec)
1608 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001609 if (tree->ops && tree->ops->writepage_end_io_hook) {
1610 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001611 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001612 if (ret)
1613 uptodate = 0;
1614 }
1615
1616 if (!uptodate && tree->ops &&
1617 tree->ops->writepage_io_failed_hook) {
1618 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001619 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001620 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001621 uptodate = (err == 0);
1622 continue;
1623 }
1624 }
1625
Chris Masond1310b22008-01-24 16:13:08 -05001626 if (!uptodate) {
1627 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1628 ClearPageUptodate(page);
1629 SetPageError(page);
1630 }
Chris Mason70dec802008-01-29 09:59:12 -05001631
David Woodhouse902b22f2008-08-20 08:51:49 -04001632 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001633
1634 if (whole_page)
1635 end_page_writeback(page);
1636 else
1637 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001638 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001639
Chris Masond1310b22008-01-24 16:13:08 -05001640 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001641}
1642
1643/*
1644 * after a readpage IO is done, we need to:
1645 * clear the uptodate bits on error
1646 * set the uptodate bits if things worked
1647 * set the page up to date if all extents in the tree are uptodate
1648 * clear the lock bit in the extent tree
1649 * unlock the page if there are no other extents locked for it
1650 *
1651 * Scheduling is not allowed, so the extent state tree is expected
1652 * to have one and only one object corresponding to this IO.
1653 */
Chris Masond1310b22008-01-24 16:13:08 -05001654static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001655{
1656 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1657 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001658 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001659 u64 start;
1660 u64 end;
1661 int whole_page;
1662 int ret;
1663
Chris Masond20f7042008-12-08 16:58:54 -05001664 if (err)
1665 uptodate = 0;
1666
Chris Masond1310b22008-01-24 16:13:08 -05001667 do {
1668 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001669 tree = &BTRFS_I(page->mapping->host)->io_tree;
1670
Chris Masond1310b22008-01-24 16:13:08 -05001671 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1672 bvec->bv_offset;
1673 end = start + bvec->bv_len - 1;
1674
1675 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1676 whole_page = 1;
1677 else
1678 whole_page = 0;
1679
1680 if (--bvec >= bio->bi_io_vec)
1681 prefetchw(&bvec->bv_page->flags);
1682
1683 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001684 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001685 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001686 if (ret)
1687 uptodate = 0;
1688 }
Chris Mason7e383262008-04-09 16:28:12 -04001689 if (!uptodate && tree->ops &&
1690 tree->ops->readpage_io_failed_hook) {
1691 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001692 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001693 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001694 uptodate =
1695 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001696 if (err)
1697 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001698 continue;
1699 }
1700 }
Chris Mason70dec802008-01-29 09:59:12 -05001701
Chris Mason771ed682008-11-06 22:02:51 -05001702 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001703 set_extent_uptodate(tree, start, end,
1704 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001705 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001706 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001707
Chris Mason70dec802008-01-29 09:59:12 -05001708 if (whole_page) {
1709 if (uptodate) {
1710 SetPageUptodate(page);
1711 } else {
1712 ClearPageUptodate(page);
1713 SetPageError(page);
1714 }
Chris Masond1310b22008-01-24 16:13:08 -05001715 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001716 } else {
1717 if (uptodate) {
1718 check_page_uptodate(tree, page);
1719 } else {
1720 ClearPageUptodate(page);
1721 SetPageError(page);
1722 }
Chris Masond1310b22008-01-24 16:13:08 -05001723 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001724 }
Chris Masond1310b22008-01-24 16:13:08 -05001725 } while (bvec >= bio->bi_io_vec);
1726
1727 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001728}
1729
1730/*
1731 * IO done from prepare_write is pretty simple, we just unlock
1732 * the structs in the extent tree when done, and set the uptodate bits
1733 * as appropriate.
1734 */
Chris Masond1310b22008-01-24 16:13:08 -05001735static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001736{
1737 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1738 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001739 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001740 u64 start;
1741 u64 end;
1742
Chris Masond1310b22008-01-24 16:13:08 -05001743 do {
1744 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001745 tree = &BTRFS_I(page->mapping->host)->io_tree;
1746
Chris Masond1310b22008-01-24 16:13:08 -05001747 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1748 bvec->bv_offset;
1749 end = start + bvec->bv_len - 1;
1750
1751 if (--bvec >= bio->bi_io_vec)
1752 prefetchw(&bvec->bv_page->flags);
1753
1754 if (uptodate) {
1755 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1756 } else {
1757 ClearPageUptodate(page);
1758 SetPageError(page);
1759 }
1760
1761 unlock_extent(tree, start, end, GFP_ATOMIC);
1762
1763 } while (bvec >= bio->bi_io_vec);
1764
1765 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001766}
1767
1768static struct bio *
1769extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1770 gfp_t gfp_flags)
1771{
1772 struct bio *bio;
1773
1774 bio = bio_alloc(gfp_flags, nr_vecs);
1775
1776 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1777 while (!bio && (nr_vecs /= 2))
1778 bio = bio_alloc(gfp_flags, nr_vecs);
1779 }
1780
1781 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001782 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001783 bio->bi_bdev = bdev;
1784 bio->bi_sector = first_sector;
1785 }
1786 return bio;
1787}
1788
Chris Masonc8b97812008-10-29 14:49:59 -04001789static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1790 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001791{
Chris Masond1310b22008-01-24 16:13:08 -05001792 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001793 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1794 struct page *page = bvec->bv_page;
1795 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001796 u64 start;
1797 u64 end;
1798
1799 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1800 end = start + bvec->bv_len - 1;
1801
David Woodhouse902b22f2008-08-20 08:51:49 -04001802 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001803
1804 bio_get(bio);
1805
Chris Mason065631f2008-02-20 12:07:25 -05001806 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001807 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001808 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001809 else
1810 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001811 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1812 ret = -EOPNOTSUPP;
1813 bio_put(bio);
1814 return ret;
1815}
1816
1817static int submit_extent_page(int rw, struct extent_io_tree *tree,
1818 struct page *page, sector_t sector,
1819 size_t size, unsigned long offset,
1820 struct block_device *bdev,
1821 struct bio **bio_ret,
1822 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001823 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001824 int mirror_num,
1825 unsigned long prev_bio_flags,
1826 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001827{
1828 int ret = 0;
1829 struct bio *bio;
1830 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001831 int contig = 0;
1832 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1833 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001834 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001835
1836 if (bio_ret && *bio_ret) {
1837 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001838 if (old_compressed)
1839 contig = bio->bi_sector == sector;
1840 else
1841 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1842 sector;
1843
1844 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001845 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001846 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1847 bio_flags)) ||
1848 bio_add_page(bio, page, page_size, offset) < page_size) {
1849 ret = submit_one_bio(rw, bio, mirror_num,
1850 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001851 bio = NULL;
1852 } else {
1853 return 0;
1854 }
1855 }
Chris Masonc8b97812008-10-29 14:49:59 -04001856 if (this_compressed)
1857 nr = BIO_MAX_PAGES;
1858 else
1859 nr = bio_get_nr_vecs(bdev);
1860
Chris Masond1310b22008-01-24 16:13:08 -05001861 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001862
Chris Masonc8b97812008-10-29 14:49:59 -04001863 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001864 bio->bi_end_io = end_io_func;
1865 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001866
Chris Masond3977122009-01-05 21:25:51 -05001867 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001868 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001869 else
Chris Masonc8b97812008-10-29 14:49:59 -04001870 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001871
1872 return ret;
1873}
1874
1875void set_page_extent_mapped(struct page *page)
1876{
1877 if (!PagePrivate(page)) {
1878 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001879 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001880 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001881 }
1882}
1883
Christoph Hellwigb2950862008-12-02 09:54:17 -05001884static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001885{
1886 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1887}
1888
1889/*
1890 * basic readpage implementation. Locked extent state structs are inserted
1891 * into the tree that are removed when the IO is done (by the end_io
1892 * handlers)
1893 */
1894static int __extent_read_full_page(struct extent_io_tree *tree,
1895 struct page *page,
1896 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001897 struct bio **bio, int mirror_num,
1898 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001899{
1900 struct inode *inode = page->mapping->host;
1901 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1902 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1903 u64 end;
1904 u64 cur = start;
1905 u64 extent_offset;
1906 u64 last_byte = i_size_read(inode);
1907 u64 block_start;
1908 u64 cur_end;
1909 sector_t sector;
1910 struct extent_map *em;
1911 struct block_device *bdev;
1912 int ret;
1913 int nr = 0;
1914 size_t page_offset = 0;
1915 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001916 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001917 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001918 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001919
1920 set_page_extent_mapped(page);
1921
1922 end = page_end;
1923 lock_extent(tree, start, end, GFP_NOFS);
1924
Chris Masonc8b97812008-10-29 14:49:59 -04001925 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1926 char *userpage;
1927 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1928
1929 if (zero_offset) {
1930 iosize = PAGE_CACHE_SIZE - zero_offset;
1931 userpage = kmap_atomic(page, KM_USER0);
1932 memset(userpage + zero_offset, 0, iosize);
1933 flush_dcache_page(page);
1934 kunmap_atomic(userpage, KM_USER0);
1935 }
1936 }
Chris Masond1310b22008-01-24 16:13:08 -05001937 while (cur <= end) {
1938 if (cur >= last_byte) {
1939 char *userpage;
1940 iosize = PAGE_CACHE_SIZE - page_offset;
1941 userpage = kmap_atomic(page, KM_USER0);
1942 memset(userpage + page_offset, 0, iosize);
1943 flush_dcache_page(page);
1944 kunmap_atomic(userpage, KM_USER0);
1945 set_extent_uptodate(tree, cur, cur + iosize - 1,
1946 GFP_NOFS);
1947 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1948 break;
1949 }
1950 em = get_extent(inode, page, page_offset, cur,
1951 end - cur + 1, 0);
1952 if (IS_ERR(em) || !em) {
1953 SetPageError(page);
1954 unlock_extent(tree, cur, end, GFP_NOFS);
1955 break;
1956 }
Chris Masond1310b22008-01-24 16:13:08 -05001957 extent_offset = cur - em->start;
1958 BUG_ON(extent_map_end(em) <= cur);
1959 BUG_ON(end < cur);
1960
Chris Masonc8b97812008-10-29 14:49:59 -04001961 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1962 this_bio_flag = EXTENT_BIO_COMPRESSED;
1963
Chris Masond1310b22008-01-24 16:13:08 -05001964 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1965 cur_end = min(extent_map_end(em) - 1, end);
1966 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04001967 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
1968 disk_io_size = em->block_len;
1969 sector = em->block_start >> 9;
1970 } else {
1971 sector = (em->block_start + extent_offset) >> 9;
1972 disk_io_size = iosize;
1973 }
Chris Masond1310b22008-01-24 16:13:08 -05001974 bdev = em->bdev;
1975 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04001976 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1977 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05001978 free_extent_map(em);
1979 em = NULL;
1980
1981 /* we've found a hole, just zero and go on */
1982 if (block_start == EXTENT_MAP_HOLE) {
1983 char *userpage;
1984 userpage = kmap_atomic(page, KM_USER0);
1985 memset(userpage + page_offset, 0, iosize);
1986 flush_dcache_page(page);
1987 kunmap_atomic(userpage, KM_USER0);
1988
1989 set_extent_uptodate(tree, cur, cur + iosize - 1,
1990 GFP_NOFS);
1991 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1992 cur = cur + iosize;
1993 page_offset += iosize;
1994 continue;
1995 }
1996 /* the get_extent function already copied into the page */
1997 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04001998 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001999 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2000 cur = cur + iosize;
2001 page_offset += iosize;
2002 continue;
2003 }
Chris Mason70dec802008-01-29 09:59:12 -05002004 /* we have an inline extent but it didn't get marked up
2005 * to date. Error out
2006 */
2007 if (block_start == EXTENT_MAP_INLINE) {
2008 SetPageError(page);
2009 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2010 cur = cur + iosize;
2011 page_offset += iosize;
2012 continue;
2013 }
Chris Masond1310b22008-01-24 16:13:08 -05002014
2015 ret = 0;
2016 if (tree->ops && tree->ops->readpage_io_hook) {
2017 ret = tree->ops->readpage_io_hook(page, cur,
2018 cur + iosize - 1);
2019 }
2020 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002021 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2022 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002023 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002024 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002025 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002026 end_bio_extent_readpage, mirror_num,
2027 *bio_flags,
2028 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002029 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002030 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002031 }
2032 if (ret)
2033 SetPageError(page);
2034 cur = cur + iosize;
2035 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002036 }
2037 if (!nr) {
2038 if (!PageError(page))
2039 SetPageUptodate(page);
2040 unlock_page(page);
2041 }
2042 return 0;
2043}
2044
2045int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2046 get_extent_t *get_extent)
2047{
2048 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002049 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002050 int ret;
2051
Chris Masonc8b97812008-10-29 14:49:59 -04002052 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2053 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002054 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002055 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002056 return ret;
2057}
Chris Masond1310b22008-01-24 16:13:08 -05002058
Chris Mason11c83492009-04-20 15:50:09 -04002059static noinline void update_nr_written(struct page *page,
2060 struct writeback_control *wbc,
2061 unsigned long nr_written)
2062{
2063 wbc->nr_to_write -= nr_written;
2064 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2065 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2066 page->mapping->writeback_index = page->index + nr_written;
2067}
2068
Chris Masond1310b22008-01-24 16:13:08 -05002069/*
2070 * the writepage semantics are similar to regular writepage. extent
2071 * records are inserted to lock ranges in the tree, and as dirty areas
2072 * are found, they are marked writeback. Then the lock bits are removed
2073 * and the end_io handler clears the writeback ranges
2074 */
2075static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2076 void *data)
2077{
2078 struct inode *inode = page->mapping->host;
2079 struct extent_page_data *epd = data;
2080 struct extent_io_tree *tree = epd->tree;
2081 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2082 u64 delalloc_start;
2083 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2084 u64 end;
2085 u64 cur = start;
2086 u64 extent_offset;
2087 u64 last_byte = i_size_read(inode);
2088 u64 block_start;
2089 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002090 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002091 sector_t sector;
2092 struct extent_map *em;
2093 struct block_device *bdev;
2094 int ret;
2095 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002096 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002097 size_t blocksize;
2098 loff_t i_size = i_size_read(inode);
2099 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2100 u64 nr_delalloc;
2101 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002102 int page_started;
2103 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002104 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002105 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002106
Chris Masonffbd5172009-04-20 15:50:09 -04002107 if (wbc->sync_mode == WB_SYNC_ALL)
2108 write_flags = WRITE_SYNC_PLUG;
2109 else
2110 write_flags = WRITE;
2111
Chris Masond1310b22008-01-24 16:13:08 -05002112 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002113 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002114 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002115 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002116 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002117 unlock_page(page);
2118 return 0;
2119 }
2120
2121 if (page->index == end_index) {
2122 char *userpage;
2123
Chris Masond1310b22008-01-24 16:13:08 -05002124 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002125 memset(userpage + pg_offset, 0,
2126 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002127 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002128 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002129 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002130 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002131
2132 set_page_extent_mapped(page);
2133
2134 delalloc_start = start;
2135 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002136 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002137 if (!epd->extent_locked) {
Chris Masona97adc92009-08-07 09:28:20 -04002138 u64 delalloc_to_write;
Chris Mason11c83492009-04-20 15:50:09 -04002139 /*
2140 * make sure the wbc mapping index is at least updated
2141 * to this page.
2142 */
2143 update_nr_written(page, wbc, 0);
2144
Chris Masond3977122009-01-05 21:25:51 -05002145 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002146 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002147 page,
2148 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002149 &delalloc_end,
2150 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002151 if (nr_delalloc == 0) {
2152 delalloc_start = delalloc_end + 1;
2153 continue;
2154 }
2155 tree->ops->fill_delalloc(inode, page, delalloc_start,
2156 delalloc_end, &page_started,
2157 &nr_written);
Chris Masona97adc92009-08-07 09:28:20 -04002158 delalloc_to_write = (delalloc_end -
2159 max_t(u64, page_offset(page),
2160 delalloc_start) + 1) >>
2161 PAGE_CACHE_SHIFT;
2162 if (wbc->nr_to_write < delalloc_to_write) {
2163 wbc->nr_to_write = min_t(long, 8192,
2164 delalloc_to_write);
2165 }
Chris Masond1310b22008-01-24 16:13:08 -05002166 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002167 }
Chris Masonc8b97812008-10-29 14:49:59 -04002168
Chris Mason771ed682008-11-06 22:02:51 -05002169 /* did the fill delalloc function already unlock and start
2170 * the IO?
2171 */
2172 if (page_started) {
2173 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002174 /*
2175 * we've unlocked the page, so we can't update
2176 * the mapping's writeback index, just update
2177 * nr_to_write.
2178 */
2179 wbc->nr_to_write -= nr_written;
2180 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002181 }
Chris Masonc8b97812008-10-29 14:49:59 -04002182 }
Chris Masond1310b22008-01-24 16:13:08 -05002183 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002184
Chris Masone6dcd2d2008-07-17 12:53:50 -04002185 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002186
Chris Mason247e7432008-07-17 12:53:51 -04002187 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002188 ret = tree->ops->writepage_start_hook(page, start,
2189 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002190 if (ret == -EAGAIN) {
2191 unlock_extent(tree, start, page_end, GFP_NOFS);
2192 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002193 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002194 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002195 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002196 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002197 }
2198 }
2199
Chris Mason11c83492009-04-20 15:50:09 -04002200 /*
2201 * we don't want to touch the inode after unlocking the page,
2202 * so we update the mapping writeback index now
2203 */
2204 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002205
Chris Masond1310b22008-01-24 16:13:08 -05002206 end = page_end;
Chris Masond3977122009-01-05 21:25:51 -05002207 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2208 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
Chris Masond1310b22008-01-24 16:13:08 -05002209
2210 if (last_byte <= start) {
2211 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002212 unlock_extent(tree, start, page_end, GFP_NOFS);
2213 if (tree->ops && tree->ops->writepage_end_io_hook)
2214 tree->ops->writepage_end_io_hook(page, start,
2215 page_end, NULL, 1);
2216 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002217 goto done;
2218 }
2219
2220 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2221 blocksize = inode->i_sb->s_blocksize;
2222
2223 while (cur <= end) {
2224 if (cur >= last_byte) {
2225 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002226 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2227 if (tree->ops && tree->ops->writepage_end_io_hook)
2228 tree->ops->writepage_end_io_hook(page, cur,
2229 page_end, NULL, 1);
2230 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002231 break;
2232 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002233 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002234 end - cur + 1, 1);
2235 if (IS_ERR(em) || !em) {
2236 SetPageError(page);
2237 break;
2238 }
2239
2240 extent_offset = cur - em->start;
2241 BUG_ON(extent_map_end(em) <= cur);
2242 BUG_ON(end < cur);
2243 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2244 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2245 sector = (em->block_start + extent_offset) >> 9;
2246 bdev = em->bdev;
2247 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002248 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002249 free_extent_map(em);
2250 em = NULL;
2251
Chris Masonc8b97812008-10-29 14:49:59 -04002252 /*
2253 * compressed and inline extents are written through other
2254 * paths in the FS
2255 */
2256 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002257 block_start == EXTENT_MAP_INLINE) {
2258 clear_extent_dirty(tree, cur,
2259 cur + iosize - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002260
Chris Masond3977122009-01-05 21:25:51 -05002261 unlock_extent(tree, unlock_start, cur + iosize - 1,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002262 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002263
Chris Masonc8b97812008-10-29 14:49:59 -04002264 /*
2265 * end_io notification does not happen here for
2266 * compressed extents
2267 */
2268 if (!compressed && tree->ops &&
2269 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002270 tree->ops->writepage_end_io_hook(page, cur,
2271 cur + iosize - 1,
2272 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002273 else if (compressed) {
2274 /* we don't want to end_page_writeback on
2275 * a compressed extent. this happens
2276 * elsewhere
2277 */
2278 nr++;
2279 }
2280
2281 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002282 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002283 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002284 continue;
2285 }
Chris Masond1310b22008-01-24 16:13:08 -05002286 /* leave this out until we have a page_mkwrite call */
2287 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2288 EXTENT_DIRTY, 0)) {
2289 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002290 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002291 continue;
2292 }
Chris Masonc8b97812008-10-29 14:49:59 -04002293
Chris Masond1310b22008-01-24 16:13:08 -05002294 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2295 if (tree->ops && tree->ops->writepage_io_hook) {
2296 ret = tree->ops->writepage_io_hook(page, cur,
2297 cur + iosize - 1);
2298 } else {
2299 ret = 0;
2300 }
Chris Mason1259ab72008-05-12 13:39:03 -04002301 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002302 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002303 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002304 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002305
Chris Masond1310b22008-01-24 16:13:08 -05002306 set_range_writeback(tree, cur, cur + iosize - 1);
2307 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002308 printk(KERN_ERR "btrfs warning page %lu not "
2309 "writeback, cur %llu end %llu\n",
2310 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002311 (unsigned long long)end);
2312 }
2313
Chris Masonffbd5172009-04-20 15:50:09 -04002314 ret = submit_extent_page(write_flags, tree, page,
2315 sector, iosize, pg_offset,
2316 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002317 end_bio_extent_writepage,
2318 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002319 if (ret)
2320 SetPageError(page);
2321 }
2322 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002323 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002324 nr++;
2325 }
2326done:
2327 if (nr == 0) {
2328 /* make sure the mapping tag for page dirty gets cleared */
2329 set_page_writeback(page);
2330 end_page_writeback(page);
2331 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002332 if (unlock_start <= page_end)
2333 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002334 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002335
Chris Mason11c83492009-04-20 15:50:09 -04002336done_unlocked:
2337
Chris Masond1310b22008-01-24 16:13:08 -05002338 return 0;
2339}
2340
Chris Masond1310b22008-01-24 16:13:08 -05002341/**
Chris Mason4bef0842008-09-08 11:18:08 -04002342 * 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 -05002343 * @mapping: address space structure to write
2344 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2345 * @writepage: function called for each page
2346 * @data: data passed to writepage function
2347 *
2348 * If a page is already under I/O, write_cache_pages() skips it, even
2349 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2350 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2351 * and msync() need to guarantee that all the data which was dirty at the time
2352 * the call was made get new I/O started against them. If wbc->sync_mode is
2353 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2354 * existing IO to complete.
2355 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002356static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002357 struct address_space *mapping,
2358 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002359 writepage_t writepage, void *data,
2360 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002361{
Chris Masond1310b22008-01-24 16:13:08 -05002362 int ret = 0;
2363 int done = 0;
2364 struct pagevec pvec;
2365 int nr_pages;
2366 pgoff_t index;
2367 pgoff_t end; /* Inclusive */
2368 int scanned = 0;
2369 int range_whole = 0;
2370
Chris Masond1310b22008-01-24 16:13:08 -05002371 pagevec_init(&pvec, 0);
2372 if (wbc->range_cyclic) {
2373 index = mapping->writeback_index; /* Start from prev offset */
2374 end = -1;
2375 } else {
2376 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2377 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2378 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2379 range_whole = 1;
2380 scanned = 1;
2381 }
2382retry:
2383 while (!done && (index <= end) &&
2384 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002385 PAGECACHE_TAG_DIRTY, min(end - index,
2386 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002387 unsigned i;
2388
2389 scanned = 1;
2390 for (i = 0; i < nr_pages; i++) {
2391 struct page *page = pvec.pages[i];
2392
2393 /*
2394 * At this point we hold neither mapping->tree_lock nor
2395 * lock on the page itself: the page may be truncated or
2396 * invalidated (changing page->mapping to NULL), or even
2397 * swizzled back from swapper_space to tmpfs file
2398 * mapping
2399 */
Chris Mason4bef0842008-09-08 11:18:08 -04002400 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2401 tree->ops->write_cache_pages_lock_hook(page);
2402 else
2403 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002404
2405 if (unlikely(page->mapping != mapping)) {
2406 unlock_page(page);
2407 continue;
2408 }
2409
2410 if (!wbc->range_cyclic && page->index > end) {
2411 done = 1;
2412 unlock_page(page);
2413 continue;
2414 }
2415
Chris Masond2c3f4f2008-11-19 12:44:22 -05002416 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002417 if (PageWriteback(page))
2418 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002419 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002420 }
Chris Masond1310b22008-01-24 16:13:08 -05002421
2422 if (PageWriteback(page) ||
2423 !clear_page_dirty_for_io(page)) {
2424 unlock_page(page);
2425 continue;
2426 }
2427
2428 ret = (*writepage)(page, wbc, data);
2429
2430 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2431 unlock_page(page);
2432 ret = 0;
2433 }
Chris Mason771ed682008-11-06 22:02:51 -05002434 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002435 done = 1;
Chris Masond1310b22008-01-24 16:13:08 -05002436 }
2437 pagevec_release(&pvec);
2438 cond_resched();
2439 }
2440 if (!scanned && !done) {
2441 /*
2442 * We hit the last page and there is more work to be done: wrap
2443 * back to the start of the file
2444 */
2445 scanned = 1;
2446 index = 0;
2447 goto retry;
2448 }
Chris Masond1310b22008-01-24 16:13:08 -05002449 return ret;
2450}
Chris Masond1310b22008-01-24 16:13:08 -05002451
Chris Masonffbd5172009-04-20 15:50:09 -04002452static void flush_epd_write_bio(struct extent_page_data *epd)
2453{
2454 if (epd->bio) {
2455 if (epd->sync_io)
2456 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2457 else
2458 submit_one_bio(WRITE, epd->bio, 0, 0);
2459 epd->bio = NULL;
2460 }
2461}
2462
Chris Masond2c3f4f2008-11-19 12:44:22 -05002463static noinline void flush_write_bio(void *data)
2464{
2465 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002466 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002467}
2468
Chris Masond1310b22008-01-24 16:13:08 -05002469int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2470 get_extent_t *get_extent,
2471 struct writeback_control *wbc)
2472{
2473 int ret;
2474 struct address_space *mapping = page->mapping;
2475 struct extent_page_data epd = {
2476 .bio = NULL,
2477 .tree = tree,
2478 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002479 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002480 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002481 };
2482 struct writeback_control wbc_writepages = {
2483 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002484 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002485 .older_than_this = NULL,
2486 .nr_to_write = 64,
2487 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2488 .range_end = (loff_t)-1,
2489 };
2490
Chris Masond1310b22008-01-24 16:13:08 -05002491 ret = __extent_writepage(page, wbc, &epd);
2492
Chris Mason4bef0842008-09-08 11:18:08 -04002493 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002494 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002495 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002496 return ret;
2497}
Chris Masond1310b22008-01-24 16:13:08 -05002498
Chris Mason771ed682008-11-06 22:02:51 -05002499int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2500 u64 start, u64 end, get_extent_t *get_extent,
2501 int mode)
2502{
2503 int ret = 0;
2504 struct address_space *mapping = inode->i_mapping;
2505 struct page *page;
2506 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2507 PAGE_CACHE_SHIFT;
2508
2509 struct extent_page_data epd = {
2510 .bio = NULL,
2511 .tree = tree,
2512 .get_extent = get_extent,
2513 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002514 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002515 };
2516 struct writeback_control wbc_writepages = {
2517 .bdi = inode->i_mapping->backing_dev_info,
2518 .sync_mode = mode,
2519 .older_than_this = NULL,
2520 .nr_to_write = nr_pages * 2,
2521 .range_start = start,
2522 .range_end = end + 1,
2523 };
2524
Chris Masond3977122009-01-05 21:25:51 -05002525 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002526 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2527 if (clear_page_dirty_for_io(page))
2528 ret = __extent_writepage(page, &wbc_writepages, &epd);
2529 else {
2530 if (tree->ops && tree->ops->writepage_end_io_hook)
2531 tree->ops->writepage_end_io_hook(page, start,
2532 start + PAGE_CACHE_SIZE - 1,
2533 NULL, 1);
2534 unlock_page(page);
2535 }
2536 page_cache_release(page);
2537 start += PAGE_CACHE_SIZE;
2538 }
2539
Chris Masonffbd5172009-04-20 15:50:09 -04002540 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002541 return ret;
2542}
Chris Masond1310b22008-01-24 16:13:08 -05002543
2544int extent_writepages(struct extent_io_tree *tree,
2545 struct address_space *mapping,
2546 get_extent_t *get_extent,
2547 struct writeback_control *wbc)
2548{
2549 int ret = 0;
2550 struct extent_page_data epd = {
2551 .bio = NULL,
2552 .tree = tree,
2553 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002554 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002555 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002556 };
2557
Chris Mason4bef0842008-09-08 11:18:08 -04002558 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002559 __extent_writepage, &epd,
2560 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002561 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002562 return ret;
2563}
Chris Masond1310b22008-01-24 16:13:08 -05002564
2565int extent_readpages(struct extent_io_tree *tree,
2566 struct address_space *mapping,
2567 struct list_head *pages, unsigned nr_pages,
2568 get_extent_t get_extent)
2569{
2570 struct bio *bio = NULL;
2571 unsigned page_idx;
2572 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002573 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002574
2575 pagevec_init(&pvec, 0);
2576 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2577 struct page *page = list_entry(pages->prev, struct page, lru);
2578
2579 prefetchw(&page->flags);
2580 list_del(&page->lru);
2581 /*
2582 * what we want to do here is call add_to_page_cache_lru,
2583 * but that isn't exported, so we reproduce it here
2584 */
2585 if (!add_to_page_cache(page, mapping,
2586 page->index, GFP_KERNEL)) {
2587
2588 /* open coding of lru_cache_add, also not exported */
2589 page_cache_get(page);
2590 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002591 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002592 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002593 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002594 }
2595 page_cache_release(page);
2596 }
2597 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002598 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002599 BUG_ON(!list_empty(pages));
2600 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002601 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002602 return 0;
2603}
Chris Masond1310b22008-01-24 16:13:08 -05002604
2605/*
2606 * basic invalidatepage code, this waits on any locked or writeback
2607 * ranges corresponding to the page, and then deletes any extent state
2608 * records from the tree
2609 */
2610int extent_invalidatepage(struct extent_io_tree *tree,
2611 struct page *page, unsigned long offset)
2612{
2613 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2614 u64 end = start + PAGE_CACHE_SIZE - 1;
2615 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2616
Chris Masond3977122009-01-05 21:25:51 -05002617 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002618 if (start > end)
2619 return 0;
2620
2621 lock_extent(tree, start, end, GFP_NOFS);
2622 wait_on_extent_writeback(tree, start, end);
2623 clear_extent_bit(tree, start, end,
2624 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2625 1, 1, GFP_NOFS);
2626 return 0;
2627}
Chris Masond1310b22008-01-24 16:13:08 -05002628
2629/*
2630 * simple commit_write call, set_range_dirty is used to mark both
2631 * the pages and the extent records as dirty
2632 */
2633int extent_commit_write(struct extent_io_tree *tree,
2634 struct inode *inode, struct page *page,
2635 unsigned from, unsigned to)
2636{
2637 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2638
2639 set_page_extent_mapped(page);
2640 set_page_dirty(page);
2641
2642 if (pos > inode->i_size) {
2643 i_size_write(inode, pos);
2644 mark_inode_dirty(inode);
2645 }
2646 return 0;
2647}
Chris Masond1310b22008-01-24 16:13:08 -05002648
2649int extent_prepare_write(struct extent_io_tree *tree,
2650 struct inode *inode, struct page *page,
2651 unsigned from, unsigned to, get_extent_t *get_extent)
2652{
2653 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2654 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2655 u64 block_start;
2656 u64 orig_block_start;
2657 u64 block_end;
2658 u64 cur_end;
2659 struct extent_map *em;
2660 unsigned blocksize = 1 << inode->i_blkbits;
2661 size_t page_offset = 0;
2662 size_t block_off_start;
2663 size_t block_off_end;
2664 int err = 0;
2665 int iocount = 0;
2666 int ret = 0;
2667 int isnew;
2668
2669 set_page_extent_mapped(page);
2670
2671 block_start = (page_start + from) & ~((u64)blocksize - 1);
2672 block_end = (page_start + to - 1) | (blocksize - 1);
2673 orig_block_start = block_start;
2674
2675 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002676 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002677 em = get_extent(inode, page, page_offset, block_start,
2678 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002679 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002680 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002681
Chris Masond1310b22008-01-24 16:13:08 -05002682 cur_end = min(block_end, extent_map_end(em) - 1);
2683 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2684 block_off_end = block_off_start + blocksize;
2685 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2686
2687 if (!PageUptodate(page) && isnew &&
2688 (block_off_end > to || block_off_start < from)) {
2689 void *kaddr;
2690
2691 kaddr = kmap_atomic(page, KM_USER0);
2692 if (block_off_end > to)
2693 memset(kaddr + to, 0, block_off_end - to);
2694 if (block_off_start < from)
2695 memset(kaddr + block_off_start, 0,
2696 from - block_off_start);
2697 flush_dcache_page(page);
2698 kunmap_atomic(kaddr, KM_USER0);
2699 }
2700 if ((em->block_start != EXTENT_MAP_HOLE &&
2701 em->block_start != EXTENT_MAP_INLINE) &&
2702 !isnew && !PageUptodate(page) &&
2703 (block_off_end > to || block_off_start < from) &&
2704 !test_range_bit(tree, block_start, cur_end,
2705 EXTENT_UPTODATE, 1)) {
2706 u64 sector;
2707 u64 extent_offset = block_start - em->start;
2708 size_t iosize;
2709 sector = (em->block_start + extent_offset) >> 9;
2710 iosize = (cur_end - block_start + blocksize) &
2711 ~((u64)blocksize - 1);
2712 /*
2713 * we've already got the extent locked, but we
2714 * need to split the state such that our end_bio
2715 * handler can clear the lock.
2716 */
2717 set_extent_bit(tree, block_start,
2718 block_start + iosize - 1,
2719 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2720 ret = submit_extent_page(READ, tree, page,
2721 sector, iosize, page_offset, em->bdev,
2722 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002723 end_bio_extent_preparewrite, 0,
2724 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002725 iocount++;
2726 block_start = block_start + iosize;
2727 } else {
2728 set_extent_uptodate(tree, block_start, cur_end,
2729 GFP_NOFS);
2730 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2731 block_start = cur_end + 1;
2732 }
2733 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2734 free_extent_map(em);
2735 }
2736 if (iocount) {
2737 wait_extent_bit(tree, orig_block_start,
2738 block_end, EXTENT_LOCKED);
2739 }
2740 check_page_uptodate(tree, page);
2741err:
2742 /* FIXME, zero out newly allocated blocks on error */
2743 return err;
2744}
Chris Masond1310b22008-01-24 16:13:08 -05002745
2746/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002747 * a helper for releasepage, this tests for areas of the page that
2748 * are locked or under IO and drops the related state bits if it is safe
2749 * to drop the page.
2750 */
2751int try_release_extent_state(struct extent_map_tree *map,
2752 struct extent_io_tree *tree, struct page *page,
2753 gfp_t mask)
2754{
2755 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2756 u64 end = start + PAGE_CACHE_SIZE - 1;
2757 int ret = 1;
2758
Chris Mason211f90e2008-07-18 11:56:15 -04002759 if (test_range_bit(tree, start, end,
2760 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002761 ret = 0;
2762 else {
2763 if ((mask & GFP_NOFS) == GFP_NOFS)
2764 mask = GFP_NOFS;
2765 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2766 1, 1, mask);
2767 }
2768 return ret;
2769}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002770
2771/*
Chris Masond1310b22008-01-24 16:13:08 -05002772 * a helper for releasepage. As long as there are no locked extents
2773 * in the range corresponding to the page, both state records and extent
2774 * map records are removed
2775 */
2776int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002777 struct extent_io_tree *tree, struct page *page,
2778 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002779{
2780 struct extent_map *em;
2781 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2782 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002783
Chris Mason70dec802008-01-29 09:59:12 -05002784 if ((mask & __GFP_WAIT) &&
2785 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002786 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002787 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002788 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002789 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002790 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002791 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002792 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002793 break;
2794 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002795 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2796 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002797 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002798 free_extent_map(em);
2799 break;
2800 }
2801 if (!test_range_bit(tree, em->start,
2802 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002803 EXTENT_LOCKED | EXTENT_WRITEBACK |
2804 EXTENT_ORDERED,
2805 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002806 remove_extent_mapping(map, em);
2807 /* once for the rb tree */
2808 free_extent_map(em);
2809 }
2810 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002811 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002812
2813 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002814 free_extent_map(em);
2815 }
Chris Masond1310b22008-01-24 16:13:08 -05002816 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002817 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002818}
Chris Masond1310b22008-01-24 16:13:08 -05002819
2820sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2821 get_extent_t *get_extent)
2822{
2823 struct inode *inode = mapping->host;
2824 u64 start = iblock << inode->i_blkbits;
2825 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002826 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002827 struct extent_map *em;
2828
Yan Zhengd899e052008-10-30 14:25:28 -04002829 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2830 GFP_NOFS);
2831 em = get_extent(inode, NULL, 0, start, blksize, 0);
2832 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2833 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002834 if (!em || IS_ERR(em))
2835 return 0;
2836
Yan Zhengd899e052008-10-30 14:25:28 -04002837 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002838 goto out;
2839
2840 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002841out:
2842 free_extent_map(em);
2843 return sector;
2844}
2845
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002846int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2847 __u64 start, __u64 len, get_extent_t *get_extent)
2848{
2849 int ret;
2850 u64 off = start;
2851 u64 max = start + len;
2852 u32 flags = 0;
2853 u64 disko = 0;
2854 struct extent_map *em = NULL;
2855 int end = 0;
2856 u64 em_start = 0, em_len = 0;
2857 unsigned long emflags;
2858 ret = 0;
2859
2860 if (len == 0)
2861 return -EINVAL;
2862
2863 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2864 GFP_NOFS);
2865 em = get_extent(inode, NULL, 0, off, max - off, 0);
2866 if (!em)
2867 goto out;
2868 if (IS_ERR(em)) {
2869 ret = PTR_ERR(em);
2870 goto out;
2871 }
2872 while (!end) {
2873 off = em->start + em->len;
2874 if (off >= max)
2875 end = 1;
2876
2877 em_start = em->start;
2878 em_len = em->len;
2879
2880 disko = 0;
2881 flags = 0;
2882
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002883 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002884 end = 1;
2885 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002886 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002887 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002888 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002889 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2890 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002891 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002892 flags |= (FIEMAP_EXTENT_DELALLOC |
2893 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002894 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002895 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002896 }
2897 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2898 flags |= FIEMAP_EXTENT_ENCODED;
2899
2900 emflags = em->flags;
2901 free_extent_map(em);
2902 em = NULL;
2903
2904 if (!end) {
2905 em = get_extent(inode, NULL, 0, off, max - off, 0);
2906 if (!em)
2907 goto out;
2908 if (IS_ERR(em)) {
2909 ret = PTR_ERR(em);
2910 goto out;
2911 }
2912 emflags = em->flags;
2913 }
2914 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2915 flags |= FIEMAP_EXTENT_LAST;
2916 end = 1;
2917 }
2918
2919 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2920 em_len, flags);
2921 if (ret)
2922 goto out_free;
2923 }
2924out_free:
2925 free_extent_map(em);
2926out:
2927 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2928 GFP_NOFS);
2929 return ret;
2930}
2931
Chris Masond1310b22008-01-24 16:13:08 -05002932static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2933 unsigned long i)
2934{
2935 struct page *p;
2936 struct address_space *mapping;
2937
2938 if (i == 0)
2939 return eb->first_page;
2940 i += eb->start >> PAGE_CACHE_SHIFT;
2941 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002942 if (!mapping)
2943 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002944
2945 /*
2946 * extent_buffer_page is only called after pinning the page
2947 * by increasing the reference count. So we know the page must
2948 * be in the radix tree.
2949 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002950 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002951 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002952 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002953
Chris Masond1310b22008-01-24 16:13:08 -05002954 return p;
2955}
2956
Chris Mason6af118ce2008-07-22 11:18:07 -04002957static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002958{
Chris Mason6af118ce2008-07-22 11:18:07 -04002959 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2960 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002961}
2962
Chris Masond1310b22008-01-24 16:13:08 -05002963static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2964 u64 start,
2965 unsigned long len,
2966 gfp_t mask)
2967{
2968 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05002969#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002970 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04002971#endif
Chris Masond1310b22008-01-24 16:13:08 -05002972
Chris Masond1310b22008-01-24 16:13:08 -05002973 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002974 eb->start = start;
2975 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002976 spin_lock_init(&eb->lock);
2977 init_waitqueue_head(&eb->lock_wq);
2978
Chris Mason39351272009-02-04 09:24:05 -05002979#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002980 spin_lock_irqsave(&leak_lock, flags);
2981 list_add(&eb->leak_list, &buffers);
2982 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002983#endif
Chris Masond1310b22008-01-24 16:13:08 -05002984 atomic_set(&eb->refs, 1);
2985
2986 return eb;
2987}
2988
2989static void __free_extent_buffer(struct extent_buffer *eb)
2990{
Chris Mason39351272009-02-04 09:24:05 -05002991#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002992 unsigned long flags;
2993 spin_lock_irqsave(&leak_lock, flags);
2994 list_del(&eb->leak_list);
2995 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002996#endif
Chris Masond1310b22008-01-24 16:13:08 -05002997 kmem_cache_free(extent_buffer_cache, eb);
2998}
2999
3000struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3001 u64 start, unsigned long len,
3002 struct page *page0,
3003 gfp_t mask)
3004{
3005 unsigned long num_pages = num_extent_pages(start, len);
3006 unsigned long i;
3007 unsigned long index = start >> PAGE_CACHE_SHIFT;
3008 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003009 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003010 struct page *p;
3011 struct address_space *mapping = tree->mapping;
3012 int uptodate = 1;
3013
Chris Mason6af118ce2008-07-22 11:18:07 -04003014 spin_lock(&tree->buffer_lock);
3015 eb = buffer_search(tree, start);
3016 if (eb) {
3017 atomic_inc(&eb->refs);
3018 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003019 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003020 return eb;
3021 }
3022 spin_unlock(&tree->buffer_lock);
3023
Chris Masond1310b22008-01-24 16:13:08 -05003024 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003025 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003026 return NULL;
3027
Chris Masond1310b22008-01-24 16:13:08 -05003028 if (page0) {
3029 eb->first_page = page0;
3030 i = 1;
3031 index++;
3032 page_cache_get(page0);
3033 mark_page_accessed(page0);
3034 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003035 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003036 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003037 } else {
3038 i = 0;
3039 }
3040 for (; i < num_pages; i++, index++) {
3041 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3042 if (!p) {
3043 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003044 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003045 }
3046 set_page_extent_mapped(p);
3047 mark_page_accessed(p);
3048 if (i == 0) {
3049 eb->first_page = p;
3050 set_page_extent_head(p, len);
3051 } else {
3052 set_page_private(p, EXTENT_PAGE_PRIVATE);
3053 }
3054 if (!PageUptodate(p))
3055 uptodate = 0;
3056 unlock_page(p);
3057 }
3058 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003059 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003060
Chris Mason6af118ce2008-07-22 11:18:07 -04003061 spin_lock(&tree->buffer_lock);
3062 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3063 if (exists) {
3064 /* add one reference for the caller */
3065 atomic_inc(&exists->refs);
3066 spin_unlock(&tree->buffer_lock);
3067 goto free_eb;
3068 }
3069 spin_unlock(&tree->buffer_lock);
3070
3071 /* add one reference for the tree */
3072 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003073 return eb;
3074
Chris Mason6af118ce2008-07-22 11:18:07 -04003075free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003076 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003077 return exists;
3078 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003079 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003080 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003081 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003082 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003083}
Chris Masond1310b22008-01-24 16:13:08 -05003084
3085struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3086 u64 start, unsigned long len,
3087 gfp_t mask)
3088{
Chris Masond1310b22008-01-24 16:13:08 -05003089 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003090
Chris Mason6af118ce2008-07-22 11:18:07 -04003091 spin_lock(&tree->buffer_lock);
3092 eb = buffer_search(tree, start);
3093 if (eb)
3094 atomic_inc(&eb->refs);
3095 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003096
Josef Bacik0f9dd462008-09-23 13:14:11 -04003097 if (eb)
3098 mark_page_accessed(eb->first_page);
3099
Chris Masond1310b22008-01-24 16:13:08 -05003100 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003101}
Chris Masond1310b22008-01-24 16:13:08 -05003102
3103void free_extent_buffer(struct extent_buffer *eb)
3104{
Chris Masond1310b22008-01-24 16:13:08 -05003105 if (!eb)
3106 return;
3107
3108 if (!atomic_dec_and_test(&eb->refs))
3109 return;
3110
Chris Mason6af118ce2008-07-22 11:18:07 -04003111 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003112}
Chris Masond1310b22008-01-24 16:13:08 -05003113
3114int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3115 struct extent_buffer *eb)
3116{
Chris Masond1310b22008-01-24 16:13:08 -05003117 unsigned long i;
3118 unsigned long num_pages;
3119 struct page *page;
3120
Chris Masond1310b22008-01-24 16:13:08 -05003121 num_pages = num_extent_pages(eb->start, eb->len);
3122
3123 for (i = 0; i < num_pages; i++) {
3124 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003125 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003126 continue;
3127
Chris Masona61e6f22008-07-22 11:18:08 -04003128 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003129 if (i == 0)
3130 set_page_extent_head(page, eb->len);
3131 else
3132 set_page_private(page, EXTENT_PAGE_PRIVATE);
3133
Chris Masond1310b22008-01-24 16:13:08 -05003134 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003135 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003136 if (!PageDirty(page)) {
3137 radix_tree_tag_clear(&page->mapping->page_tree,
3138 page_index(page),
3139 PAGECACHE_TAG_DIRTY);
3140 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003141 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003142 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003143 }
3144 return 0;
3145}
Chris Masond1310b22008-01-24 16:13:08 -05003146
3147int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3148 struct extent_buffer *eb)
3149{
3150 return wait_on_extent_writeback(tree, eb->start,
3151 eb->start + eb->len - 1);
3152}
Chris Masond1310b22008-01-24 16:13:08 -05003153
3154int set_extent_buffer_dirty(struct extent_io_tree *tree,
3155 struct extent_buffer *eb)
3156{
3157 unsigned long i;
3158 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003159 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003160
Chris Masonb9473432009-03-13 11:00:37 -04003161 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003162 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003163 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003164 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003165 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003166}
Chris Masond1310b22008-01-24 16:13:08 -05003167
Chris Mason1259ab72008-05-12 13:39:03 -04003168int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3169 struct extent_buffer *eb)
3170{
3171 unsigned long i;
3172 struct page *page;
3173 unsigned long num_pages;
3174
3175 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003176 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003177
3178 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3179 GFP_NOFS);
3180 for (i = 0; i < num_pages; i++) {
3181 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003182 if (page)
3183 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003184 }
3185 return 0;
3186}
3187
Chris Masond1310b22008-01-24 16:13:08 -05003188int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3189 struct extent_buffer *eb)
3190{
3191 unsigned long i;
3192 struct page *page;
3193 unsigned long num_pages;
3194
3195 num_pages = num_extent_pages(eb->start, eb->len);
3196
3197 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3198 GFP_NOFS);
3199 for (i = 0; i < num_pages; i++) {
3200 page = extent_buffer_page(eb, i);
3201 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3202 ((i == num_pages - 1) &&
3203 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3204 check_page_uptodate(tree, page);
3205 continue;
3206 }
3207 SetPageUptodate(page);
3208 }
3209 return 0;
3210}
Chris Masond1310b22008-01-24 16:13:08 -05003211
Chris Masonce9adaa2008-04-09 16:28:12 -04003212int extent_range_uptodate(struct extent_io_tree *tree,
3213 u64 start, u64 end)
3214{
3215 struct page *page;
3216 int ret;
3217 int pg_uptodate = 1;
3218 int uptodate;
3219 unsigned long index;
3220
3221 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3222 if (ret)
3223 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003224 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003225 index = start >> PAGE_CACHE_SHIFT;
3226 page = find_get_page(tree->mapping, index);
3227 uptodate = PageUptodate(page);
3228 page_cache_release(page);
3229 if (!uptodate) {
3230 pg_uptodate = 0;
3231 break;
3232 }
3233 start += PAGE_CACHE_SIZE;
3234 }
3235 return pg_uptodate;
3236}
3237
Chris Masond1310b22008-01-24 16:13:08 -05003238int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003239 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003240{
Chris Mason728131d2008-04-09 16:28:12 -04003241 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003242 unsigned long num_pages;
3243 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003244 struct page *page;
3245 int pg_uptodate = 1;
3246
Chris Masonb4ce94d2009-02-04 09:25:08 -05003247 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003248 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003249
Chris Mason42352982008-04-28 16:40:52 -04003250 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003251 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003252 if (ret)
3253 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003254
3255 num_pages = num_extent_pages(eb->start, eb->len);
3256 for (i = 0; i < num_pages; i++) {
3257 page = extent_buffer_page(eb, i);
3258 if (!PageUptodate(page)) {
3259 pg_uptodate = 0;
3260 break;
3261 }
3262 }
Chris Mason42352982008-04-28 16:40:52 -04003263 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003264}
Chris Masond1310b22008-01-24 16:13:08 -05003265
3266int read_extent_buffer_pages(struct extent_io_tree *tree,
3267 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003268 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003269 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003270{
3271 unsigned long i;
3272 unsigned long start_i;
3273 struct page *page;
3274 int err;
3275 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003276 int locked_pages = 0;
3277 int all_uptodate = 1;
3278 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003279 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003280 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003281 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003282
Chris Masonb4ce94d2009-02-04 09:25:08 -05003283 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003284 return 0;
3285
Chris Masonce9adaa2008-04-09 16:28:12 -04003286 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003287 EXTENT_UPTODATE, 1)) {
3288 return 0;
3289 }
3290
3291 if (start) {
3292 WARN_ON(start < eb->start);
3293 start_i = (start >> PAGE_CACHE_SHIFT) -
3294 (eb->start >> PAGE_CACHE_SHIFT);
3295 } else {
3296 start_i = 0;
3297 }
3298
3299 num_pages = num_extent_pages(eb->start, eb->len);
3300 for (i = start_i; i < num_pages; i++) {
3301 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003302 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003303 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003304 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003305 } else {
3306 lock_page(page);
3307 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003308 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003309 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003310 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003311 }
3312 if (all_uptodate) {
3313 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003314 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003315 goto unlock_exit;
3316 }
3317
3318 for (i = start_i; i < num_pages; i++) {
3319 page = extent_buffer_page(eb, i);
3320 if (inc_all_pages)
3321 page_cache_get(page);
3322 if (!PageUptodate(page)) {
3323 if (start_i == 0)
3324 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003325 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003326 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003327 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003328 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003329 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003330 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003331 } else {
3332 unlock_page(page);
3333 }
3334 }
3335
Chris Masona86c12c2008-02-07 10:50:54 -05003336 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003337 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003338
Chris Masond3977122009-01-05 21:25:51 -05003339 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003340 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003341
Chris Masond1310b22008-01-24 16:13:08 -05003342 for (i = start_i; i < num_pages; i++) {
3343 page = extent_buffer_page(eb, i);
3344 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003345 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003346 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003347 }
Chris Masond3977122009-01-05 21:25:51 -05003348
Chris Masond1310b22008-01-24 16:13:08 -05003349 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003350 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003351 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003352
3353unlock_exit:
3354 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003355 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003356 page = extent_buffer_page(eb, i);
3357 i++;
3358 unlock_page(page);
3359 locked_pages--;
3360 }
3361 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003362}
Chris Masond1310b22008-01-24 16:13:08 -05003363
3364void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3365 unsigned long start,
3366 unsigned long len)
3367{
3368 size_t cur;
3369 size_t offset;
3370 struct page *page;
3371 char *kaddr;
3372 char *dst = (char *)dstv;
3373 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3374 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003375
3376 WARN_ON(start > eb->len);
3377 WARN_ON(start + len > eb->start + eb->len);
3378
3379 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3380
Chris Masond3977122009-01-05 21:25:51 -05003381 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003382 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003383
3384 cur = min(len, (PAGE_CACHE_SIZE - offset));
3385 kaddr = kmap_atomic(page, KM_USER1);
3386 memcpy(dst, kaddr + offset, cur);
3387 kunmap_atomic(kaddr, KM_USER1);
3388
3389 dst += cur;
3390 len -= cur;
3391 offset = 0;
3392 i++;
3393 }
3394}
Chris Masond1310b22008-01-24 16:13:08 -05003395
3396int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3397 unsigned long min_len, char **token, char **map,
3398 unsigned long *map_start,
3399 unsigned long *map_len, int km)
3400{
3401 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3402 char *kaddr;
3403 struct page *p;
3404 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3405 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3406 unsigned long end_i = (start_offset + start + min_len - 1) >>
3407 PAGE_CACHE_SHIFT;
3408
3409 if (i != end_i)
3410 return -EINVAL;
3411
3412 if (i == 0) {
3413 offset = start_offset;
3414 *map_start = 0;
3415 } else {
3416 offset = 0;
3417 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3418 }
Chris Masond3977122009-01-05 21:25:51 -05003419
Chris Masond1310b22008-01-24 16:13:08 -05003420 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003421 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3422 "wanted %lu %lu\n", (unsigned long long)eb->start,
3423 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003424 WARN_ON(1);
3425 }
3426
3427 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003428 kaddr = kmap_atomic(p, km);
3429 *token = kaddr;
3430 *map = kaddr + offset;
3431 *map_len = PAGE_CACHE_SIZE - offset;
3432 return 0;
3433}
Chris Masond1310b22008-01-24 16:13:08 -05003434
3435int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3436 unsigned long min_len,
3437 char **token, char **map,
3438 unsigned long *map_start,
3439 unsigned long *map_len, int km)
3440{
3441 int err;
3442 int save = 0;
3443 if (eb->map_token) {
3444 unmap_extent_buffer(eb, eb->map_token, km);
3445 eb->map_token = NULL;
3446 save = 1;
3447 }
3448 err = map_private_extent_buffer(eb, start, min_len, token, map,
3449 map_start, map_len, km);
3450 if (!err && save) {
3451 eb->map_token = *token;
3452 eb->kaddr = *map;
3453 eb->map_start = *map_start;
3454 eb->map_len = *map_len;
3455 }
3456 return err;
3457}
Chris Masond1310b22008-01-24 16:13:08 -05003458
3459void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3460{
3461 kunmap_atomic(token, km);
3462}
Chris Masond1310b22008-01-24 16:13:08 -05003463
3464int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3465 unsigned long start,
3466 unsigned long len)
3467{
3468 size_t cur;
3469 size_t offset;
3470 struct page *page;
3471 char *kaddr;
3472 char *ptr = (char *)ptrv;
3473 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3474 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3475 int ret = 0;
3476
3477 WARN_ON(start > eb->len);
3478 WARN_ON(start + len > eb->start + eb->len);
3479
3480 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3481
Chris Masond3977122009-01-05 21:25:51 -05003482 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003483 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003484
3485 cur = min(len, (PAGE_CACHE_SIZE - offset));
3486
3487 kaddr = kmap_atomic(page, KM_USER0);
3488 ret = memcmp(ptr, kaddr + offset, cur);
3489 kunmap_atomic(kaddr, KM_USER0);
3490 if (ret)
3491 break;
3492
3493 ptr += cur;
3494 len -= cur;
3495 offset = 0;
3496 i++;
3497 }
3498 return ret;
3499}
Chris Masond1310b22008-01-24 16:13:08 -05003500
3501void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3502 unsigned long start, unsigned long len)
3503{
3504 size_t cur;
3505 size_t offset;
3506 struct page *page;
3507 char *kaddr;
3508 char *src = (char *)srcv;
3509 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3510 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3511
3512 WARN_ON(start > eb->len);
3513 WARN_ON(start + len > eb->start + eb->len);
3514
3515 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3516
Chris Masond3977122009-01-05 21:25:51 -05003517 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003518 page = extent_buffer_page(eb, i);
3519 WARN_ON(!PageUptodate(page));
3520
3521 cur = min(len, PAGE_CACHE_SIZE - offset);
3522 kaddr = kmap_atomic(page, KM_USER1);
3523 memcpy(kaddr + offset, src, cur);
3524 kunmap_atomic(kaddr, KM_USER1);
3525
3526 src += cur;
3527 len -= cur;
3528 offset = 0;
3529 i++;
3530 }
3531}
Chris Masond1310b22008-01-24 16:13:08 -05003532
3533void memset_extent_buffer(struct extent_buffer *eb, char c,
3534 unsigned long start, unsigned long len)
3535{
3536 size_t cur;
3537 size_t offset;
3538 struct page *page;
3539 char *kaddr;
3540 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3541 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3542
3543 WARN_ON(start > eb->len);
3544 WARN_ON(start + len > eb->start + eb->len);
3545
3546 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3547
Chris Masond3977122009-01-05 21:25:51 -05003548 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003549 page = extent_buffer_page(eb, i);
3550 WARN_ON(!PageUptodate(page));
3551
3552 cur = min(len, PAGE_CACHE_SIZE - offset);
3553 kaddr = kmap_atomic(page, KM_USER0);
3554 memset(kaddr + offset, c, cur);
3555 kunmap_atomic(kaddr, KM_USER0);
3556
3557 len -= cur;
3558 offset = 0;
3559 i++;
3560 }
3561}
Chris Masond1310b22008-01-24 16:13:08 -05003562
3563void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3564 unsigned long dst_offset, unsigned long src_offset,
3565 unsigned long len)
3566{
3567 u64 dst_len = dst->len;
3568 size_t cur;
3569 size_t offset;
3570 struct page *page;
3571 char *kaddr;
3572 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3573 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3574
3575 WARN_ON(src->len != dst_len);
3576
3577 offset = (start_offset + dst_offset) &
3578 ((unsigned long)PAGE_CACHE_SIZE - 1);
3579
Chris Masond3977122009-01-05 21:25:51 -05003580 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003581 page = extent_buffer_page(dst, i);
3582 WARN_ON(!PageUptodate(page));
3583
3584 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3585
3586 kaddr = kmap_atomic(page, KM_USER0);
3587 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3588 kunmap_atomic(kaddr, KM_USER0);
3589
3590 src_offset += cur;
3591 len -= cur;
3592 offset = 0;
3593 i++;
3594 }
3595}
Chris Masond1310b22008-01-24 16:13:08 -05003596
3597static void move_pages(struct page *dst_page, struct page *src_page,
3598 unsigned long dst_off, unsigned long src_off,
3599 unsigned long len)
3600{
3601 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3602 if (dst_page == src_page) {
3603 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3604 } else {
3605 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3606 char *p = dst_kaddr + dst_off + len;
3607 char *s = src_kaddr + src_off + len;
3608
3609 while (len--)
3610 *--p = *--s;
3611
3612 kunmap_atomic(src_kaddr, KM_USER1);
3613 }
3614 kunmap_atomic(dst_kaddr, KM_USER0);
3615}
3616
3617static void copy_pages(struct page *dst_page, struct page *src_page,
3618 unsigned long dst_off, unsigned long src_off,
3619 unsigned long len)
3620{
3621 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3622 char *src_kaddr;
3623
3624 if (dst_page != src_page)
3625 src_kaddr = kmap_atomic(src_page, KM_USER1);
3626 else
3627 src_kaddr = dst_kaddr;
3628
3629 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3630 kunmap_atomic(dst_kaddr, KM_USER0);
3631 if (dst_page != src_page)
3632 kunmap_atomic(src_kaddr, KM_USER1);
3633}
3634
3635void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3636 unsigned long src_offset, unsigned long len)
3637{
3638 size_t cur;
3639 size_t dst_off_in_page;
3640 size_t src_off_in_page;
3641 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3642 unsigned long dst_i;
3643 unsigned long src_i;
3644
3645 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003646 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3647 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003648 BUG_ON(1);
3649 }
3650 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003651 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3652 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003653 BUG_ON(1);
3654 }
3655
Chris Masond3977122009-01-05 21:25:51 -05003656 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003657 dst_off_in_page = (start_offset + dst_offset) &
3658 ((unsigned long)PAGE_CACHE_SIZE - 1);
3659 src_off_in_page = (start_offset + src_offset) &
3660 ((unsigned long)PAGE_CACHE_SIZE - 1);
3661
3662 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3663 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3664
3665 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3666 src_off_in_page));
3667 cur = min_t(unsigned long, cur,
3668 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3669
3670 copy_pages(extent_buffer_page(dst, dst_i),
3671 extent_buffer_page(dst, src_i),
3672 dst_off_in_page, src_off_in_page, cur);
3673
3674 src_offset += cur;
3675 dst_offset += cur;
3676 len -= cur;
3677 }
3678}
Chris Masond1310b22008-01-24 16:13:08 -05003679
3680void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3681 unsigned long src_offset, unsigned long len)
3682{
3683 size_t cur;
3684 size_t dst_off_in_page;
3685 size_t src_off_in_page;
3686 unsigned long dst_end = dst_offset + len - 1;
3687 unsigned long src_end = src_offset + len - 1;
3688 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3689 unsigned long dst_i;
3690 unsigned long src_i;
3691
3692 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003693 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3694 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003695 BUG_ON(1);
3696 }
3697 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003698 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3699 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003700 BUG_ON(1);
3701 }
3702 if (dst_offset < src_offset) {
3703 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3704 return;
3705 }
Chris Masond3977122009-01-05 21:25:51 -05003706 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003707 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3708 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3709
3710 dst_off_in_page = (start_offset + dst_end) &
3711 ((unsigned long)PAGE_CACHE_SIZE - 1);
3712 src_off_in_page = (start_offset + src_end) &
3713 ((unsigned long)PAGE_CACHE_SIZE - 1);
3714
3715 cur = min_t(unsigned long, len, src_off_in_page + 1);
3716 cur = min(cur, dst_off_in_page + 1);
3717 move_pages(extent_buffer_page(dst, dst_i),
3718 extent_buffer_page(dst, src_i),
3719 dst_off_in_page - cur + 1,
3720 src_off_in_page - cur + 1, cur);
3721
3722 dst_end -= cur;
3723 src_end -= cur;
3724 len -= cur;
3725 }
3726}
Chris Mason6af118ce2008-07-22 11:18:07 -04003727
3728int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3729{
3730 u64 start = page_offset(page);
3731 struct extent_buffer *eb;
3732 int ret = 1;
3733 unsigned long i;
3734 unsigned long num_pages;
3735
3736 spin_lock(&tree->buffer_lock);
3737 eb = buffer_search(tree, start);
3738 if (!eb)
3739 goto out;
3740
3741 if (atomic_read(&eb->refs) > 1) {
3742 ret = 0;
3743 goto out;
3744 }
Chris Masonb9473432009-03-13 11:00:37 -04003745 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3746 ret = 0;
3747 goto out;
3748 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003749 /* at this point we can safely release the extent buffer */
3750 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003751 for (i = 0; i < num_pages; i++)
3752 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003753 rb_erase(&eb->rb_node, &tree->buffer);
3754 __free_extent_buffer(eb);
3755out:
3756 spin_unlock(&tree->buffer_lock);
3757 return ret;
3758}