blob: 05a1c42e25bf6ec5d12c34e32b6a6188e88b2df4 [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
20/* temporary define until extent_map moves out of btrfs */
21struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
22 unsigned long extra_flags,
23 void (*ctor)(void *, struct kmem_cache *,
24 unsigned long));
25
26static struct kmem_cache *extent_state_cache;
27static struct kmem_cache *extent_buffer_cache;
28
29static LIST_HEAD(buffers);
30static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040031
Chris Masonb47eda82008-11-10 12:34:40 -050032#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050033#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050034static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040035#endif
Chris Masond1310b22008-01-24 16:13:08 -050036
Chris Masond1310b22008-01-24 16:13:08 -050037#define BUFFER_LRU_MAX 64
38
39struct tree_entry {
40 u64 start;
41 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050042 struct rb_node rb_node;
43};
44
45struct extent_page_data {
46 struct bio *bio;
47 struct extent_io_tree *tree;
48 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050049
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
52 */
Chris Masonffbd5172009-04-20 15:50:09 -040053 unsigned int extent_locked:1;
54
55 /* tells the submit_bio code to use a WRITE_SYNC */
56 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050057};
58
59int __init extent_io_init(void)
60{
61 extent_state_cache = btrfs_cache_create("extent_state",
62 sizeof(struct extent_state), 0,
63 NULL);
64 if (!extent_state_cache)
65 return -ENOMEM;
66
67 extent_buffer_cache = btrfs_cache_create("extent_buffers",
68 sizeof(struct extent_buffer), 0,
69 NULL);
70 if (!extent_buffer_cache)
71 goto free_state_cache;
72 return 0;
73
74free_state_cache:
75 kmem_cache_destroy(extent_state_cache);
76 return -ENOMEM;
77}
78
79void extent_io_exit(void)
80{
81 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040082 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050083
84 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040085 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050086 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
87 "state %lu in tree %p refs %d\n",
88 (unsigned long long)state->start,
89 (unsigned long long)state->end,
90 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040091 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050092 kmem_cache_free(extent_state_cache, state);
93
94 }
95
Chris Mason2d2ae542008-03-26 16:24:23 -040096 while (!list_empty(&buffers)) {
97 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050098 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
99 "refs %d\n", (unsigned long long)eb->start,
100 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400101 list_del(&eb->leak_list);
102 kmem_cache_free(extent_buffer_cache, eb);
103 }
Chris Masond1310b22008-01-24 16:13:08 -0500104 if (extent_state_cache)
105 kmem_cache_destroy(extent_state_cache);
106 if (extent_buffer_cache)
107 kmem_cache_destroy(extent_buffer_cache);
108}
109
110void extent_io_tree_init(struct extent_io_tree *tree,
111 struct address_space *mapping, gfp_t mask)
112{
113 tree->state.rb_node = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400114 tree->buffer.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500115 tree->ops = NULL;
116 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500117 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400118 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500119 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500120}
Chris Masond1310b22008-01-24 16:13:08 -0500121
Christoph Hellwigb2950862008-12-02 09:54:17 -0500122static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500123{
124 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500125#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400126 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400127#endif
Chris Masond1310b22008-01-24 16:13:08 -0500128
129 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400130 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500131 return state;
132 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500133 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500134 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500135#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400136 spin_lock_irqsave(&leak_lock, flags);
137 list_add(&state->leak_list, &states);
138 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400139#endif
Chris Masond1310b22008-01-24 16:13:08 -0500140 atomic_set(&state->refs, 1);
141 init_waitqueue_head(&state->wq);
142 return state;
143}
Chris Masond1310b22008-01-24 16:13:08 -0500144
Christoph Hellwigb2950862008-12-02 09:54:17 -0500145static void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500146{
Chris Masond1310b22008-01-24 16:13:08 -0500147 if (!state)
148 return;
149 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500150#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400151 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Mason70dec802008-01-29 09:59:12 -0500153 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500154#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400155 spin_lock_irqsave(&leak_lock, flags);
156 list_del(&state->leak_list);
157 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400158#endif
Chris Masond1310b22008-01-24 16:13:08 -0500159 kmem_cache_free(extent_state_cache, state);
160 }
161}
Chris Masond1310b22008-01-24 16:13:08 -0500162
163static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
164 struct rb_node *node)
165{
Chris Masond3977122009-01-05 21:25:51 -0500166 struct rb_node **p = &root->rb_node;
167 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500168 struct tree_entry *entry;
169
Chris Masond3977122009-01-05 21:25:51 -0500170 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500171 parent = *p;
172 entry = rb_entry(parent, struct tree_entry, rb_node);
173
174 if (offset < entry->start)
175 p = &(*p)->rb_left;
176 else if (offset > entry->end)
177 p = &(*p)->rb_right;
178 else
179 return parent;
180 }
181
182 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500183 rb_link_node(node, parent, p);
184 rb_insert_color(node, root);
185 return NULL;
186}
187
Chris Mason80ea96b2008-02-01 14:51:59 -0500188static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500189 struct rb_node **prev_ret,
190 struct rb_node **next_ret)
191{
Chris Mason80ea96b2008-02-01 14:51:59 -0500192 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500193 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500194 struct rb_node *prev = NULL;
195 struct rb_node *orig_prev = NULL;
196 struct tree_entry *entry;
197 struct tree_entry *prev_entry = NULL;
198
Chris Masond3977122009-01-05 21:25:51 -0500199 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500200 entry = rb_entry(n, struct tree_entry, rb_node);
201 prev = n;
202 prev_entry = entry;
203
204 if (offset < entry->start)
205 n = n->rb_left;
206 else if (offset > entry->end)
207 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500208 else
Chris Masond1310b22008-01-24 16:13:08 -0500209 return n;
210 }
211
212 if (prev_ret) {
213 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500214 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500215 prev = rb_next(prev);
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
217 }
218 *prev_ret = prev;
219 prev = orig_prev;
220 }
221
222 if (next_ret) {
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500224 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500225 prev = rb_prev(prev);
226 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
227 }
228 *next_ret = prev;
229 }
230 return NULL;
231}
232
Chris Mason80ea96b2008-02-01 14:51:59 -0500233static inline struct rb_node *tree_search(struct extent_io_tree *tree,
234 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500235{
Chris Mason70dec802008-01-29 09:59:12 -0500236 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500237 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500238
Chris Mason80ea96b2008-02-01 14:51:59 -0500239 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500240 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500241 return prev;
242 return ret;
243}
244
Chris Mason6af118ce2008-07-22 11:18:07 -0400245static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
246 u64 offset, struct rb_node *node)
247{
248 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500249 struct rb_node **p = &root->rb_node;
250 struct rb_node *parent = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400251 struct extent_buffer *eb;
252
Chris Masond3977122009-01-05 21:25:51 -0500253 while (*p) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400254 parent = *p;
255 eb = rb_entry(parent, struct extent_buffer, rb_node);
256
257 if (offset < eb->start)
258 p = &(*p)->rb_left;
259 else if (offset > eb->start)
260 p = &(*p)->rb_right;
261 else
262 return eb;
263 }
264
265 rb_link_node(node, parent, p);
266 rb_insert_color(node, root);
267 return NULL;
268}
269
270static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
271 u64 offset)
272{
273 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500274 struct rb_node *n = root->rb_node;
Chris Mason6af118ce2008-07-22 11:18:07 -0400275 struct extent_buffer *eb;
276
Chris Masond3977122009-01-05 21:25:51 -0500277 while (n) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400278 eb = rb_entry(n, struct extent_buffer, rb_node);
279 if (offset < eb->start)
280 n = n->rb_left;
281 else if (offset > eb->start)
282 n = n->rb_right;
283 else
284 return eb;
285 }
286 return NULL;
287}
288
Chris Masond1310b22008-01-24 16:13:08 -0500289/*
290 * utility function to look for merge candidates inside a given range.
291 * Any extents with matching state are merged together into a single
292 * extent in the tree. Extents with EXTENT_IO in their state field
293 * are not merged because the end_io handlers need to be able to do
294 * operations on them without sleeping (or doing allocations/splits).
295 *
296 * This should be called with the tree lock held.
297 */
298static int merge_state(struct extent_io_tree *tree,
299 struct extent_state *state)
300{
301 struct extent_state *other;
302 struct rb_node *other_node;
303
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400304 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500305 return 0;
306
307 other_node = rb_prev(&state->rb_node);
308 if (other_node) {
309 other = rb_entry(other_node, struct extent_state, rb_node);
310 if (other->end == state->start - 1 &&
311 other->state == state->state) {
312 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500313 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500314 rb_erase(&other->rb_node, &tree->state);
315 free_extent_state(other);
316 }
317 }
318 other_node = rb_next(&state->rb_node);
319 if (other_node) {
320 other = rb_entry(other_node, struct extent_state, rb_node);
321 if (other->start == state->end + 1 &&
322 other->state == state->state) {
323 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500324 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500325 rb_erase(&state->rb_node, &tree->state);
326 free_extent_state(state);
327 }
328 }
329 return 0;
330}
331
Chris Mason291d6732008-01-29 15:55:23 -0500332static void set_state_cb(struct extent_io_tree *tree,
333 struct extent_state *state,
334 unsigned long bits)
335{
336 if (tree->ops && tree->ops->set_bit_hook) {
337 tree->ops->set_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500338 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500339 }
340}
341
342static void clear_state_cb(struct extent_io_tree *tree,
343 struct extent_state *state,
344 unsigned long bits)
345{
Liu Huic5844822009-01-05 15:49:55 -0500346 if (tree->ops && tree->ops->clear_bit_hook) {
Chris Mason291d6732008-01-29 15:55:23 -0500347 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500348 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500349 }
350}
351
Chris Masond1310b22008-01-24 16:13:08 -0500352/*
353 * insert an extent_state struct into the tree. 'bits' are set on the
354 * struct before it is inserted.
355 *
356 * This may return -EEXIST if the extent is already there, in which case the
357 * state struct is freed.
358 *
359 * The tree lock is not taken internally. This is a utility function and
360 * probably isn't what you want to call (see set/clear_extent_bit).
361 */
362static int insert_state(struct extent_io_tree *tree,
363 struct extent_state *state, u64 start, u64 end,
364 int bits)
365{
366 struct rb_node *node;
367
368 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500369 printk(KERN_ERR "btrfs end < start %llu %llu\n",
370 (unsigned long long)end,
371 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500372 WARN_ON(1);
373 }
374 if (bits & EXTENT_DIRTY)
375 tree->dirty_bytes += end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500376 set_state_cb(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500377 state->state |= bits;
378 state->start = start;
379 state->end = end;
380 node = tree_insert(&tree->state, end, &state->rb_node);
381 if (node) {
382 struct extent_state *found;
383 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500384 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
385 "%llu %llu\n", (unsigned long long)found->start,
386 (unsigned long long)found->end,
387 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500388 free_extent_state(state);
389 return -EEXIST;
390 }
Chris Mason70dec802008-01-29 09:59:12 -0500391 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500392 merge_state(tree, state);
393 return 0;
394}
395
396/*
397 * split a given extent state struct in two, inserting the preallocated
398 * struct 'prealloc' as the newly created second half. 'split' indicates an
399 * offset inside 'orig' where it should be split.
400 *
401 * Before calling,
402 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
403 * are two extent state structs in the tree:
404 * prealloc: [orig->start, split - 1]
405 * orig: [ split, orig->end ]
406 *
407 * The tree locks are not taken by this function. They need to be held
408 * by the caller.
409 */
410static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
411 struct extent_state *prealloc, u64 split)
412{
413 struct rb_node *node;
414 prealloc->start = orig->start;
415 prealloc->end = split - 1;
416 prealloc->state = orig->state;
417 orig->start = split;
418
419 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
420 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500421 free_extent_state(prealloc);
422 return -EEXIST;
423 }
Chris Mason70dec802008-01-29 09:59:12 -0500424 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500425 return 0;
426}
427
428/*
429 * utility function to clear some bits in an extent state struct.
430 * it will optionally wake up any one waiting on this state (wake == 1), or
431 * forcibly remove the state from the tree (delete == 1).
432 *
433 * If no bits are set on the state struct after clearing things, the
434 * struct is freed and removed from the tree
435 */
436static int clear_state_bit(struct extent_io_tree *tree,
437 struct extent_state *state, int bits, int wake,
438 int delete)
439{
440 int ret = state->state & bits;
441
442 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
443 u64 range = state->end - state->start + 1;
444 WARN_ON(range > tree->dirty_bytes);
445 tree->dirty_bytes -= range;
446 }
Chris Mason291d6732008-01-29 15:55:23 -0500447 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500448 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500449 if (wake)
450 wake_up(&state->wq);
451 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500452 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500453 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500454 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500455 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500456 free_extent_state(state);
457 } else {
458 WARN_ON(1);
459 }
460 } else {
461 merge_state(tree, state);
462 }
463 return ret;
464}
465
466/*
467 * clear some bits on a range in the tree. This may require splitting
468 * or inserting elements in the tree, so the gfp mask is used to
469 * indicate which allocations or sleeping are allowed.
470 *
471 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
472 * the given range from the tree regardless of state (ie for truncate).
473 *
474 * the range [start, end] is inclusive.
475 *
476 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
477 * bits were already set, or zero if none of the bits were already set.
478 */
479int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
480 int bits, int wake, int delete, gfp_t mask)
481{
482 struct extent_state *state;
483 struct extent_state *prealloc = NULL;
484 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500485 int err;
486 int set = 0;
487
488again:
489 if (!prealloc && (mask & __GFP_WAIT)) {
490 prealloc = alloc_extent_state(mask);
491 if (!prealloc)
492 return -ENOMEM;
493 }
494
Chris Masoncad321a2008-12-17 14:51:42 -0500495 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500496 /*
497 * this search will find the extents that end after
498 * our range starts
499 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500500 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500501 if (!node)
502 goto out;
503 state = rb_entry(node, struct extent_state, rb_node);
504 if (state->start > end)
505 goto out;
506 WARN_ON(state->end < start);
507
508 /*
509 * | ---- desired range ---- |
510 * | state | or
511 * | ------------- state -------------- |
512 *
513 * We need to split the extent we found, and may flip
514 * bits on second half.
515 *
516 * If the extent we found extends past our range, we
517 * just split and search again. It'll get split again
518 * the next time though.
519 *
520 * If the extent we found is inside our range, we clear
521 * the desired bit on it.
522 */
523
524 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500525 if (!prealloc)
526 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500527 err = split_state(tree, state, prealloc, start);
528 BUG_ON(err == -EEXIST);
529 prealloc = NULL;
530 if (err)
531 goto out;
532 if (state->end <= end) {
533 start = state->end + 1;
534 set |= clear_state_bit(tree, state, bits,
535 wake, delete);
536 } else {
537 start = state->start;
538 }
539 goto search_again;
540 }
541 /*
542 * | ---- desired range ---- |
543 * | state |
544 * We need to split the extent, and clear the bit
545 * on the first half
546 */
547 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500548 if (!prealloc)
549 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500550 err = split_state(tree, state, prealloc, end + 1);
551 BUG_ON(err == -EEXIST);
552
553 if (wake)
554 wake_up(&state->wq);
555 set |= clear_state_bit(tree, prealloc, bits,
556 wake, delete);
557 prealloc = NULL;
558 goto out;
559 }
560
561 start = state->end + 1;
562 set |= clear_state_bit(tree, state, bits, wake, delete);
563 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 }
697
698 state = rb_entry(node, struct extent_state, rb_node);
699 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) {
709 set = state->state & bits;
710 if (set && exclusive) {
711 *failed_start = state->start;
712 err = -EEXIST;
713 goto out;
714 }
715 set_state_bits(tree, state, bits);
716 start = state->end + 1;
717 merge_state(tree, state);
718 goto search_again;
719 }
720
721 /*
722 * | ---- desired range ---- |
723 * | state |
724 * or
725 * | ------------- state -------------- |
726 *
727 * We need to split the extent we found, and may flip bits on
728 * second half.
729 *
730 * If the extent we found extends past our
731 * range, we just split and search again. It'll get split
732 * again the next time though.
733 *
734 * If the extent we found is inside our range, we set the
735 * desired bit on it.
736 */
737 if (state->start < start) {
738 set = state->state & bits;
739 if (exclusive && set) {
740 *failed_start = start;
741 err = -EEXIST;
742 goto out;
743 }
744 err = split_state(tree, state, prealloc, start);
745 BUG_ON(err == -EEXIST);
746 prealloc = NULL;
747 if (err)
748 goto out;
749 if (state->end <= end) {
750 set_state_bits(tree, state, bits);
751 start = state->end + 1;
752 merge_state(tree, state);
753 } else {
754 start = state->start;
755 }
756 goto search_again;
757 }
758 /*
759 * | ---- desired range ---- |
760 * | state | or | state |
761 *
762 * There's a hole, we need to insert something in it and
763 * ignore the extent we found.
764 */
765 if (state->start > start) {
766 u64 this_end;
767 if (end < last_start)
768 this_end = end;
769 else
Chris Masond3977122009-01-05 21:25:51 -0500770 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500771 err = insert_state(tree, prealloc, start, this_end,
772 bits);
773 prealloc = NULL;
774 BUG_ON(err == -EEXIST);
775 if (err)
776 goto out;
777 start = this_end + 1;
778 goto search_again;
779 }
780 /*
781 * | ---- desired range ---- |
782 * | state |
783 * We need to split the extent, and set the bit
784 * on the first half
785 */
786 if (state->start <= end && state->end > end) {
787 set = state->state & bits;
788 if (exclusive && set) {
789 *failed_start = start;
790 err = -EEXIST;
791 goto out;
792 }
793 err = split_state(tree, state, prealloc, end + 1);
794 BUG_ON(err == -EEXIST);
795
796 set_state_bits(tree, prealloc, bits);
797 merge_state(tree, prealloc);
798 prealloc = NULL;
799 goto out;
800 }
801
802 goto search_again;
803
804out:
Chris Masoncad321a2008-12-17 14:51:42 -0500805 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500806 if (prealloc)
807 free_extent_state(prealloc);
808
809 return err;
810
811search_again:
812 if (start > end)
813 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500814 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500815 if (mask & __GFP_WAIT)
816 cond_resched();
817 goto again;
818}
Chris Masond1310b22008-01-24 16:13:08 -0500819
820/* wrappers around set/clear extent bit */
821int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
822 gfp_t mask)
823{
824 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
825 mask);
826}
Chris Masond1310b22008-01-24 16:13:08 -0500827
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
829 gfp_t mask)
830{
831 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
832}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833
Chris Masond1310b22008-01-24 16:13:08 -0500834int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
835 int bits, gfp_t mask)
836{
837 return set_extent_bit(tree, start, end, bits, 0, NULL,
838 mask);
839}
Chris Masond1310b22008-01-24 16:13:08 -0500840
841int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
842 int bits, gfp_t mask)
843{
844 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
845}
Chris Masond1310b22008-01-24 16:13:08 -0500846
847int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
848 gfp_t mask)
849{
850 return set_extent_bit(tree, start, end,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400851 EXTENT_DELALLOC | EXTENT_DIRTY,
852 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500853}
Chris Masond1310b22008-01-24 16:13:08 -0500854
855int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
856 gfp_t mask)
857{
858 return clear_extent_bit(tree, start, end,
859 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
860}
Chris Masond1310b22008-01-24 16:13:08 -0500861
Chris Masone6dcd2d2008-07-17 12:53:50 -0400862int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
863 gfp_t mask)
864{
865 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
866}
Chris Masone6dcd2d2008-07-17 12:53:50 -0400867
Chris Masond1310b22008-01-24 16:13:08 -0500868int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
869 gfp_t mask)
870{
871 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
872 mask);
873}
Chris Masond1310b22008-01-24 16:13:08 -0500874
Christoph Hellwigb2950862008-12-02 09:54:17 -0500875static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500876 gfp_t mask)
877{
878 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
879}
Chris Masond1310b22008-01-24 16:13:08 -0500880
881int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
882 gfp_t mask)
883{
884 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
885 mask);
886}
Chris Masond1310b22008-01-24 16:13:08 -0500887
Chris Masond3977122009-01-05 21:25:51 -0500888static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
889 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500890{
891 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
892}
Chris Masond1310b22008-01-24 16:13:08 -0500893
Christoph Hellwigb2950862008-12-02 09:54:17 -0500894static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500895 gfp_t mask)
896{
897 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
898 0, NULL, mask);
899}
Chris Masond1310b22008-01-24 16:13:08 -0500900
Chris Masond3977122009-01-05 21:25:51 -0500901static int clear_extent_writeback(struct extent_io_tree *tree, u64 start,
902 u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500903{
904 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
905}
Chris Masond1310b22008-01-24 16:13:08 -0500906
907int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
908{
909 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
910}
Chris Masond1310b22008-01-24 16:13:08 -0500911
Chris Masond352ac62008-09-29 15:18:18 -0400912/*
913 * either insert or lock state struct between start and end use mask to tell
914 * us if waiting is desired.
915 */
Chris Masond1310b22008-01-24 16:13:08 -0500916int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
917{
918 int err;
919 u64 failed_start;
920 while (1) {
921 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
922 &failed_start, mask);
923 if (err == -EEXIST && (mask & __GFP_WAIT)) {
924 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
925 start = failed_start;
926 } else {
927 break;
928 }
929 WARN_ON(start > end);
930 }
931 return err;
932}
Chris Masond1310b22008-01-24 16:13:08 -0500933
Josef Bacik25179202008-10-29 14:49:05 -0400934int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
935 gfp_t mask)
936{
937 int err;
938 u64 failed_start;
939
940 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
941 &failed_start, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400942 if (err == -EEXIST) {
943 if (failed_start > start)
944 clear_extent_bit(tree, start, failed_start - 1,
945 EXTENT_LOCKED, 1, 0, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400946 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400947 }
Josef Bacik25179202008-10-29 14:49:05 -0400948 return 1;
949}
Josef Bacik25179202008-10-29 14:49:05 -0400950
Chris Masond1310b22008-01-24 16:13:08 -0500951int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
952 gfp_t mask)
953{
954 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
955}
Chris Masond1310b22008-01-24 16:13:08 -0500956
957/*
958 * helper function to set pages and extents in the tree dirty
959 */
960int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
961{
962 unsigned long index = start >> PAGE_CACHE_SHIFT;
963 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
964 struct page *page;
965
966 while (index <= end_index) {
967 page = find_get_page(tree->mapping, index);
968 BUG_ON(!page);
969 __set_page_dirty_nobuffers(page);
970 page_cache_release(page);
971 index++;
972 }
973 set_extent_dirty(tree, start, end, GFP_NOFS);
974 return 0;
975}
Chris Masond1310b22008-01-24 16:13:08 -0500976
977/*
978 * helper function to set both pages and extents in the tree writeback
979 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500980static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -0500981{
982 unsigned long index = start >> PAGE_CACHE_SHIFT;
983 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
984 struct page *page;
985
986 while (index <= end_index) {
987 page = find_get_page(tree->mapping, index);
988 BUG_ON(!page);
989 set_page_writeback(page);
990 page_cache_release(page);
991 index++;
992 }
993 set_extent_writeback(tree, start, end, GFP_NOFS);
994 return 0;
995}
Chris Masond1310b22008-01-24 16:13:08 -0500996
Chris Masond352ac62008-09-29 15:18:18 -0400997/*
998 * find the first offset in the io tree with 'bits' set. zero is
999 * returned if we find something, and *start_ret and *end_ret are
1000 * set to reflect the state struct that was found.
1001 *
1002 * If nothing was found, 1 is returned, < 0 on error
1003 */
Chris Masond1310b22008-01-24 16:13:08 -05001004int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1005 u64 *start_ret, u64 *end_ret, int bits)
1006{
1007 struct rb_node *node;
1008 struct extent_state *state;
1009 int ret = 1;
1010
Chris Masoncad321a2008-12-17 14:51:42 -05001011 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001012 /*
1013 * this search will find all the extents that end after
1014 * our range starts.
1015 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001016 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001017 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001018 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001019
Chris Masond3977122009-01-05 21:25:51 -05001020 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001021 state = rb_entry(node, struct extent_state, rb_node);
1022 if (state->end >= start && (state->state & bits)) {
1023 *start_ret = state->start;
1024 *end_ret = state->end;
1025 ret = 0;
1026 break;
1027 }
1028 node = rb_next(node);
1029 if (!node)
1030 break;
1031 }
1032out:
Chris Masoncad321a2008-12-17 14:51:42 -05001033 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001034 return ret;
1035}
Chris Masond1310b22008-01-24 16:13:08 -05001036
Chris Masond352ac62008-09-29 15:18:18 -04001037/* find the first state struct with 'bits' set after 'start', and
1038 * return it. tree->lock must be held. NULL will returned if
1039 * nothing was found after 'start'
1040 */
Chris Masond7fc6402008-02-18 12:12:38 -05001041struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1042 u64 start, int bits)
1043{
1044 struct rb_node *node;
1045 struct extent_state *state;
1046
1047 /*
1048 * this search will find all the extents that end after
1049 * our range starts.
1050 */
1051 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001052 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001053 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001054
Chris Masond3977122009-01-05 21:25:51 -05001055 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001056 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001057 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001058 return state;
Chris Masond3977122009-01-05 21:25:51 -05001059
Chris Masond7fc6402008-02-18 12:12:38 -05001060 node = rb_next(node);
1061 if (!node)
1062 break;
1063 }
1064out:
1065 return NULL;
1066}
Chris Masond7fc6402008-02-18 12:12:38 -05001067
Chris Masond352ac62008-09-29 15:18:18 -04001068/*
1069 * find a contiguous range of bytes in the file marked as delalloc, not
1070 * more than 'max_bytes'. start and end are used to return the range,
1071 *
1072 * 1 is returned if we find something, 0 if nothing was in the tree
1073 */
Chris Masonc8b97812008-10-29 14:49:59 -04001074static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1075 u64 *start, u64 *end, u64 max_bytes)
Chris Masond1310b22008-01-24 16:13:08 -05001076{
1077 struct rb_node *node;
1078 struct extent_state *state;
1079 u64 cur_start = *start;
1080 u64 found = 0;
1081 u64 total_bytes = 0;
1082
Chris Masoncad321a2008-12-17 14:51:42 -05001083 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001084
Chris Masond1310b22008-01-24 16:13:08 -05001085 /*
1086 * this search will find all the extents that end after
1087 * our range starts.
1088 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001089 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001090 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001091 if (!found)
1092 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001093 goto out;
1094 }
1095
Chris Masond3977122009-01-05 21:25:51 -05001096 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001097 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001098 if (found && (state->start != cur_start ||
1099 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001100 goto out;
1101 }
1102 if (!(state->state & EXTENT_DELALLOC)) {
1103 if (!found)
1104 *end = state->end;
1105 goto out;
1106 }
Chris Masond1310b22008-01-24 16:13:08 -05001107 if (!found)
1108 *start = state->start;
1109 found++;
1110 *end = state->end;
1111 cur_start = state->end + 1;
1112 node = rb_next(node);
1113 if (!node)
1114 break;
1115 total_bytes += state->end - state->start + 1;
1116 if (total_bytes >= max_bytes)
1117 break;
1118 }
1119out:
Chris Masoncad321a2008-12-17 14:51:42 -05001120 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001121 return found;
1122}
1123
Chris Masonc8b97812008-10-29 14:49:59 -04001124static noinline int __unlock_for_delalloc(struct inode *inode,
1125 struct page *locked_page,
1126 u64 start, u64 end)
1127{
1128 int ret;
1129 struct page *pages[16];
1130 unsigned long index = start >> PAGE_CACHE_SHIFT;
1131 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1132 unsigned long nr_pages = end_index - index + 1;
1133 int i;
1134
1135 if (index == locked_page->index && end_index == index)
1136 return 0;
1137
Chris Masond3977122009-01-05 21:25:51 -05001138 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001139 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001140 min_t(unsigned long, nr_pages,
1141 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001142 for (i = 0; i < ret; i++) {
1143 if (pages[i] != locked_page)
1144 unlock_page(pages[i]);
1145 page_cache_release(pages[i]);
1146 }
1147 nr_pages -= ret;
1148 index += ret;
1149 cond_resched();
1150 }
1151 return 0;
1152}
1153
1154static noinline int lock_delalloc_pages(struct inode *inode,
1155 struct page *locked_page,
1156 u64 delalloc_start,
1157 u64 delalloc_end)
1158{
1159 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1160 unsigned long start_index = index;
1161 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1162 unsigned long pages_locked = 0;
1163 struct page *pages[16];
1164 unsigned long nrpages;
1165 int ret;
1166 int i;
1167
1168 /* the caller is responsible for locking the start index */
1169 if (index == locked_page->index && index == end_index)
1170 return 0;
1171
1172 /* skip the page at the start index */
1173 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001174 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001175 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001176 min_t(unsigned long,
1177 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001178 if (ret == 0) {
1179 ret = -EAGAIN;
1180 goto done;
1181 }
1182 /* now we have an array of pages, lock them all */
1183 for (i = 0; i < ret; i++) {
1184 /*
1185 * the caller is taking responsibility for
1186 * locked_page
1187 */
Chris Mason771ed682008-11-06 22:02:51 -05001188 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001189 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001190 if (!PageDirty(pages[i]) ||
1191 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001192 ret = -EAGAIN;
1193 unlock_page(pages[i]);
1194 page_cache_release(pages[i]);
1195 goto done;
1196 }
1197 }
Chris Masonc8b97812008-10-29 14:49:59 -04001198 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001199 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001200 }
Chris Masonc8b97812008-10-29 14:49:59 -04001201 nrpages -= ret;
1202 index += ret;
1203 cond_resched();
1204 }
1205 ret = 0;
1206done:
1207 if (ret && pages_locked) {
1208 __unlock_for_delalloc(inode, locked_page,
1209 delalloc_start,
1210 ((u64)(start_index + pages_locked - 1)) <<
1211 PAGE_CACHE_SHIFT);
1212 }
1213 return ret;
1214}
1215
1216/*
1217 * find a contiguous range of bytes in the file marked as delalloc, not
1218 * more than 'max_bytes'. start and end are used to return the range,
1219 *
1220 * 1 is returned if we find something, 0 if nothing was in the tree
1221 */
1222static noinline u64 find_lock_delalloc_range(struct inode *inode,
1223 struct extent_io_tree *tree,
1224 struct page *locked_page,
1225 u64 *start, u64 *end,
1226 u64 max_bytes)
1227{
1228 u64 delalloc_start;
1229 u64 delalloc_end;
1230 u64 found;
1231 int ret;
1232 int loops = 0;
1233
1234again:
1235 /* step one, find a bunch of delalloc bytes starting at start */
1236 delalloc_start = *start;
1237 delalloc_end = 0;
1238 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1239 max_bytes);
Chris Mason70b99e62008-10-31 12:46:39 -04001240 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001241 *start = delalloc_start;
1242 *end = delalloc_end;
1243 return found;
1244 }
1245
1246 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001247 * start comes from the offset of locked_page. We have to lock
1248 * pages in order, so we can't process delalloc bytes before
1249 * locked_page
1250 */
Chris Masond3977122009-01-05 21:25:51 -05001251 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001252 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001253
1254 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001255 * make sure to limit the number of pages we try to lock down
1256 * if we're looping.
1257 */
Chris Masond3977122009-01-05 21:25:51 -05001258 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001259 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001260
Chris Masonc8b97812008-10-29 14:49:59 -04001261 /* step two, lock all the pages after the page that has start */
1262 ret = lock_delalloc_pages(inode, locked_page,
1263 delalloc_start, delalloc_end);
1264 if (ret == -EAGAIN) {
1265 /* some of the pages are gone, lets avoid looping by
1266 * shortening the size of the delalloc range we're searching
1267 */
1268 if (!loops) {
1269 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1270 max_bytes = PAGE_CACHE_SIZE - offset;
1271 loops = 1;
1272 goto again;
1273 } else {
1274 found = 0;
1275 goto out_failed;
1276 }
1277 }
1278 BUG_ON(ret);
1279
1280 /* step three, lock the state bits for the whole range */
1281 lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1282
1283 /* then test to make sure it is all still delalloc */
1284 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1285 EXTENT_DELALLOC, 1);
1286 if (!ret) {
1287 unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS);
1288 __unlock_for_delalloc(inode, locked_page,
1289 delalloc_start, delalloc_end);
1290 cond_resched();
1291 goto again;
1292 }
1293 *start = delalloc_start;
1294 *end = delalloc_end;
1295out_failed:
1296 return found;
1297}
1298
1299int extent_clear_unlock_delalloc(struct inode *inode,
1300 struct extent_io_tree *tree,
1301 u64 start, u64 end, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001302 int unlock_pages,
1303 int clear_unlock,
1304 int clear_delalloc, int clear_dirty,
1305 int set_writeback,
Chris Masonc8b97812008-10-29 14:49:59 -04001306 int end_writeback)
1307{
1308 int ret;
1309 struct page *pages[16];
1310 unsigned long index = start >> PAGE_CACHE_SHIFT;
1311 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1312 unsigned long nr_pages = end_index - index + 1;
1313 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001314 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001315
Chris Mason771ed682008-11-06 22:02:51 -05001316 if (clear_unlock)
1317 clear_bits |= EXTENT_LOCKED;
Chris Masonc8b97812008-10-29 14:49:59 -04001318 if (clear_dirty)
1319 clear_bits |= EXTENT_DIRTY;
1320
Chris Mason771ed682008-11-06 22:02:51 -05001321 if (clear_delalloc)
1322 clear_bits |= EXTENT_DELALLOC;
1323
Chris Masonc8b97812008-10-29 14:49:59 -04001324 clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05001325 if (!(unlock_pages || clear_dirty || set_writeback || end_writeback))
1326 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001327
Chris Masond3977122009-01-05 21:25:51 -05001328 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001329 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001330 min_t(unsigned long,
1331 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001332 for (i = 0; i < ret; i++) {
1333 if (pages[i] == locked_page) {
1334 page_cache_release(pages[i]);
1335 continue;
1336 }
1337 if (clear_dirty)
1338 clear_page_dirty_for_io(pages[i]);
1339 if (set_writeback)
1340 set_page_writeback(pages[i]);
1341 if (end_writeback)
1342 end_page_writeback(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001343 if (unlock_pages)
1344 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001345 page_cache_release(pages[i]);
1346 }
1347 nr_pages -= ret;
1348 index += ret;
1349 cond_resched();
1350 }
1351 return 0;
1352}
Chris Masonc8b97812008-10-29 14:49:59 -04001353
Chris Masond352ac62008-09-29 15:18:18 -04001354/*
1355 * count the number of bytes in the tree that have a given bit(s)
1356 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1357 * cached. The total number found is returned.
1358 */
Chris Masond1310b22008-01-24 16:13:08 -05001359u64 count_range_bits(struct extent_io_tree *tree,
1360 u64 *start, u64 search_end, u64 max_bytes,
1361 unsigned long bits)
1362{
1363 struct rb_node *node;
1364 struct extent_state *state;
1365 u64 cur_start = *start;
1366 u64 total_bytes = 0;
1367 int found = 0;
1368
1369 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001370 WARN_ON(1);
1371 return 0;
1372 }
1373
Chris Masoncad321a2008-12-17 14:51:42 -05001374 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001375 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1376 total_bytes = tree->dirty_bytes;
1377 goto out;
1378 }
1379 /*
1380 * this search will find all the extents that end after
1381 * our range starts.
1382 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001383 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001384 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001385 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001386
Chris Masond3977122009-01-05 21:25:51 -05001387 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001388 state = rb_entry(node, struct extent_state, rb_node);
1389 if (state->start > search_end)
1390 break;
1391 if (state->end >= cur_start && (state->state & bits)) {
1392 total_bytes += min(search_end, state->end) + 1 -
1393 max(cur_start, state->start);
1394 if (total_bytes >= max_bytes)
1395 break;
1396 if (!found) {
1397 *start = state->start;
1398 found = 1;
1399 }
1400 }
1401 node = rb_next(node);
1402 if (!node)
1403 break;
1404 }
1405out:
Chris Masoncad321a2008-12-17 14:51:42 -05001406 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001407 return total_bytes;
1408}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001409
1410#if 0
Chris Masond1310b22008-01-24 16:13:08 -05001411/*
1412 * helper function to lock both pages and extents in the tree.
1413 * pages must be locked first.
1414 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001415static int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001416{
1417 unsigned long index = start >> PAGE_CACHE_SHIFT;
1418 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1419 struct page *page;
1420 int err;
1421
1422 while (index <= end_index) {
1423 page = grab_cache_page(tree->mapping, index);
1424 if (!page) {
1425 err = -ENOMEM;
1426 goto failed;
1427 }
1428 if (IS_ERR(page)) {
1429 err = PTR_ERR(page);
1430 goto failed;
1431 }
1432 index++;
1433 }
1434 lock_extent(tree, start, end, GFP_NOFS);
1435 return 0;
1436
1437failed:
1438 /*
1439 * we failed above in getting the page at 'index', so we undo here
1440 * up to but not including the page at 'index'
1441 */
1442 end_index = index;
1443 index = start >> PAGE_CACHE_SHIFT;
1444 while (index < end_index) {
1445 page = find_get_page(tree->mapping, index);
1446 unlock_page(page);
1447 page_cache_release(page);
1448 index++;
1449 }
1450 return err;
1451}
Chris Masond1310b22008-01-24 16:13:08 -05001452
1453/*
1454 * helper function to unlock both pages and extents in the tree.
1455 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001456static int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001457{
1458 unsigned long index = start >> PAGE_CACHE_SHIFT;
1459 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1460 struct page *page;
1461
1462 while (index <= end_index) {
1463 page = find_get_page(tree->mapping, index);
1464 unlock_page(page);
1465 page_cache_release(page);
1466 index++;
1467 }
1468 unlock_extent(tree, start, end, GFP_NOFS);
1469 return 0;
1470}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001471#endif
Chris Masond1310b22008-01-24 16:13:08 -05001472
Chris Masond352ac62008-09-29 15:18:18 -04001473/*
1474 * set the private field for a given byte offset in the tree. If there isn't
1475 * an extent_state there already, this does nothing.
1476 */
Chris Masond1310b22008-01-24 16:13:08 -05001477int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1478{
1479 struct rb_node *node;
1480 struct extent_state *state;
1481 int ret = 0;
1482
Chris Masoncad321a2008-12-17 14:51:42 -05001483 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001484 /*
1485 * this search will find all the extents that end after
1486 * our range starts.
1487 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001488 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001489 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001490 ret = -ENOENT;
1491 goto out;
1492 }
1493 state = rb_entry(node, struct extent_state, rb_node);
1494 if (state->start != start) {
1495 ret = -ENOENT;
1496 goto out;
1497 }
1498 state->private = private;
1499out:
Chris Masoncad321a2008-12-17 14:51:42 -05001500 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001501 return ret;
1502}
1503
1504int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1505{
1506 struct rb_node *node;
1507 struct extent_state *state;
1508 int ret = 0;
1509
Chris Masoncad321a2008-12-17 14:51:42 -05001510 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001511 /*
1512 * this search will find all the extents that end after
1513 * our range starts.
1514 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001515 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001516 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001517 ret = -ENOENT;
1518 goto out;
1519 }
1520 state = rb_entry(node, struct extent_state, rb_node);
1521 if (state->start != start) {
1522 ret = -ENOENT;
1523 goto out;
1524 }
1525 *private = state->private;
1526out:
Chris Masoncad321a2008-12-17 14:51:42 -05001527 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001528 return ret;
1529}
1530
1531/*
1532 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001533 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001534 * has the bits set. Otherwise, 1 is returned if any bit in the
1535 * range is found set.
1536 */
1537int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1538 int bits, int filled)
1539{
1540 struct extent_state *state = NULL;
1541 struct rb_node *node;
1542 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001543
Chris Masoncad321a2008-12-17 14:51:42 -05001544 spin_lock(&tree->lock);
Chris Mason80ea96b2008-02-01 14:51:59 -05001545 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001546 while (node && start <= end) {
1547 state = rb_entry(node, struct extent_state, rb_node);
1548
1549 if (filled && state->start > start) {
1550 bitset = 0;
1551 break;
1552 }
1553
1554 if (state->start > end)
1555 break;
1556
1557 if (state->state & bits) {
1558 bitset = 1;
1559 if (!filled)
1560 break;
1561 } else if (filled) {
1562 bitset = 0;
1563 break;
1564 }
1565 start = state->end + 1;
1566 if (start > end)
1567 break;
1568 node = rb_next(node);
1569 if (!node) {
1570 if (filled)
1571 bitset = 0;
1572 break;
1573 }
1574 }
Chris Masoncad321a2008-12-17 14:51:42 -05001575 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001576 return bitset;
1577}
Chris Masond1310b22008-01-24 16:13:08 -05001578
1579/*
1580 * helper function to set a given page up to date if all the
1581 * extents in the tree for that page are up to date
1582 */
1583static int check_page_uptodate(struct extent_io_tree *tree,
1584 struct page *page)
1585{
1586 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1587 u64 end = start + PAGE_CACHE_SIZE - 1;
1588 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1589 SetPageUptodate(page);
1590 return 0;
1591}
1592
1593/*
1594 * helper function to unlock a page if all the extents in the tree
1595 * for that page are unlocked
1596 */
1597static int check_page_locked(struct extent_io_tree *tree,
1598 struct page *page)
1599{
1600 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1601 u64 end = start + PAGE_CACHE_SIZE - 1;
1602 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1603 unlock_page(page);
1604 return 0;
1605}
1606
1607/*
1608 * helper function to end page writeback if all the extents
1609 * in the tree for that page are done with writeback
1610 */
1611static int check_page_writeback(struct extent_io_tree *tree,
1612 struct page *page)
1613{
1614 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1615 u64 end = start + PAGE_CACHE_SIZE - 1;
1616 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1617 end_page_writeback(page);
1618 return 0;
1619}
1620
1621/* lots and lots of room for performance fixes in the end_bio funcs */
1622
1623/*
1624 * after a writepage IO is done, we need to:
1625 * clear the uptodate bits on error
1626 * clear the writeback bits in the extent tree for this IO
1627 * end_page_writeback if the page has no more pending IO
1628 *
1629 * Scheduling is not allowed, so the extent state tree is expected
1630 * to have one and only one object corresponding to this IO.
1631 */
Chris Masond1310b22008-01-24 16:13:08 -05001632static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001633{
Chris Mason1259ab72008-05-12 13:39:03 -04001634 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001635 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001636 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001637 u64 start;
1638 u64 end;
1639 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001640 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001641
Chris Masond1310b22008-01-24 16:13:08 -05001642 do {
1643 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001644 tree = &BTRFS_I(page->mapping->host)->io_tree;
1645
Chris Masond1310b22008-01-24 16:13:08 -05001646 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1647 bvec->bv_offset;
1648 end = start + bvec->bv_len - 1;
1649
1650 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1651 whole_page = 1;
1652 else
1653 whole_page = 0;
1654
1655 if (--bvec >= bio->bi_io_vec)
1656 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001657 if (tree->ops && tree->ops->writepage_end_io_hook) {
1658 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001659 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001660 if (ret)
1661 uptodate = 0;
1662 }
1663
1664 if (!uptodate && tree->ops &&
1665 tree->ops->writepage_io_failed_hook) {
1666 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001667 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001668 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001669 uptodate = (err == 0);
1670 continue;
1671 }
1672 }
1673
Chris Masond1310b22008-01-24 16:13:08 -05001674 if (!uptodate) {
1675 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1676 ClearPageUptodate(page);
1677 SetPageError(page);
1678 }
Chris Mason70dec802008-01-29 09:59:12 -05001679
David Woodhouse902b22f2008-08-20 08:51:49 -04001680 clear_extent_writeback(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001681
1682 if (whole_page)
1683 end_page_writeback(page);
1684 else
1685 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001686 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001687
Chris Masond1310b22008-01-24 16:13:08 -05001688 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001689}
1690
1691/*
1692 * after a readpage IO is done, we need to:
1693 * clear the uptodate bits on error
1694 * set the uptodate bits if things worked
1695 * set the page up to date if all extents in the tree are uptodate
1696 * clear the lock bit in the extent tree
1697 * unlock the page if there are no other extents locked for it
1698 *
1699 * Scheduling is not allowed, so the extent state tree is expected
1700 * to have one and only one object corresponding to this IO.
1701 */
Chris Masond1310b22008-01-24 16:13:08 -05001702static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001703{
1704 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1705 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001706 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001707 u64 start;
1708 u64 end;
1709 int whole_page;
1710 int ret;
1711
Chris Masond20f7042008-12-08 16:58:54 -05001712 if (err)
1713 uptodate = 0;
1714
Chris Masond1310b22008-01-24 16:13:08 -05001715 do {
1716 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001717 tree = &BTRFS_I(page->mapping->host)->io_tree;
1718
Chris Masond1310b22008-01-24 16:13:08 -05001719 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1720 bvec->bv_offset;
1721 end = start + bvec->bv_len - 1;
1722
1723 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1724 whole_page = 1;
1725 else
1726 whole_page = 0;
1727
1728 if (--bvec >= bio->bi_io_vec)
1729 prefetchw(&bvec->bv_page->flags);
1730
1731 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001732 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001733 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001734 if (ret)
1735 uptodate = 0;
1736 }
Chris Mason7e383262008-04-09 16:28:12 -04001737 if (!uptodate && tree->ops &&
1738 tree->ops->readpage_io_failed_hook) {
1739 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001740 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001741 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001742 uptodate =
1743 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001744 if (err)
1745 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001746 continue;
1747 }
1748 }
Chris Mason70dec802008-01-29 09:59:12 -05001749
Chris Mason771ed682008-11-06 22:02:51 -05001750 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001751 set_extent_uptodate(tree, start, end,
1752 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001753 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001754 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001755
Chris Mason70dec802008-01-29 09:59:12 -05001756 if (whole_page) {
1757 if (uptodate) {
1758 SetPageUptodate(page);
1759 } else {
1760 ClearPageUptodate(page);
1761 SetPageError(page);
1762 }
Chris Masond1310b22008-01-24 16:13:08 -05001763 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001764 } else {
1765 if (uptodate) {
1766 check_page_uptodate(tree, page);
1767 } else {
1768 ClearPageUptodate(page);
1769 SetPageError(page);
1770 }
Chris Masond1310b22008-01-24 16:13:08 -05001771 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001772 }
Chris Masond1310b22008-01-24 16:13:08 -05001773 } while (bvec >= bio->bi_io_vec);
1774
1775 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001776}
1777
1778/*
1779 * IO done from prepare_write is pretty simple, we just unlock
1780 * the structs in the extent tree when done, and set the uptodate bits
1781 * as appropriate.
1782 */
Chris Masond1310b22008-01-24 16:13:08 -05001783static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001784{
1785 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1786 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001787 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001788 u64 start;
1789 u64 end;
1790
Chris Masond1310b22008-01-24 16:13:08 -05001791 do {
1792 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001793 tree = &BTRFS_I(page->mapping->host)->io_tree;
1794
Chris Masond1310b22008-01-24 16:13:08 -05001795 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1796 bvec->bv_offset;
1797 end = start + bvec->bv_len - 1;
1798
1799 if (--bvec >= bio->bi_io_vec)
1800 prefetchw(&bvec->bv_page->flags);
1801
1802 if (uptodate) {
1803 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1804 } else {
1805 ClearPageUptodate(page);
1806 SetPageError(page);
1807 }
1808
1809 unlock_extent(tree, start, end, GFP_ATOMIC);
1810
1811 } while (bvec >= bio->bi_io_vec);
1812
1813 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001814}
1815
1816static struct bio *
1817extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1818 gfp_t gfp_flags)
1819{
1820 struct bio *bio;
1821
1822 bio = bio_alloc(gfp_flags, nr_vecs);
1823
1824 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1825 while (!bio && (nr_vecs /= 2))
1826 bio = bio_alloc(gfp_flags, nr_vecs);
1827 }
1828
1829 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001830 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001831 bio->bi_bdev = bdev;
1832 bio->bi_sector = first_sector;
1833 }
1834 return bio;
1835}
1836
Chris Masonc8b97812008-10-29 14:49:59 -04001837static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1838 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001839{
Chris Masond1310b22008-01-24 16:13:08 -05001840 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001841 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1842 struct page *page = bvec->bv_page;
1843 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001844 u64 start;
1845 u64 end;
1846
1847 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1848 end = start + bvec->bv_len - 1;
1849
David Woodhouse902b22f2008-08-20 08:51:49 -04001850 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001851
1852 bio_get(bio);
1853
Chris Mason065631f2008-02-20 12:07:25 -05001854 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001855 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001856 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001857 else
1858 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001859 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1860 ret = -EOPNOTSUPP;
1861 bio_put(bio);
1862 return ret;
1863}
1864
1865static int submit_extent_page(int rw, struct extent_io_tree *tree,
1866 struct page *page, sector_t sector,
1867 size_t size, unsigned long offset,
1868 struct block_device *bdev,
1869 struct bio **bio_ret,
1870 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001871 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001872 int mirror_num,
1873 unsigned long prev_bio_flags,
1874 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001875{
1876 int ret = 0;
1877 struct bio *bio;
1878 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001879 int contig = 0;
1880 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1881 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001882 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001883
1884 if (bio_ret && *bio_ret) {
1885 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001886 if (old_compressed)
1887 contig = bio->bi_sector == sector;
1888 else
1889 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1890 sector;
1891
1892 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001893 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001894 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1895 bio_flags)) ||
1896 bio_add_page(bio, page, page_size, offset) < page_size) {
1897 ret = submit_one_bio(rw, bio, mirror_num,
1898 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001899 bio = NULL;
1900 } else {
1901 return 0;
1902 }
1903 }
Chris Masonc8b97812008-10-29 14:49:59 -04001904 if (this_compressed)
1905 nr = BIO_MAX_PAGES;
1906 else
1907 nr = bio_get_nr_vecs(bdev);
1908
Chris Masond1310b22008-01-24 16:13:08 -05001909 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001910
Chris Masonc8b97812008-10-29 14:49:59 -04001911 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001912 bio->bi_end_io = end_io_func;
1913 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001914
Chris Masond3977122009-01-05 21:25:51 -05001915 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001916 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001917 else
Chris Masonc8b97812008-10-29 14:49:59 -04001918 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001919
1920 return ret;
1921}
1922
1923void set_page_extent_mapped(struct page *page)
1924{
1925 if (!PagePrivate(page)) {
1926 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001927 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001928 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001929 }
1930}
1931
Christoph Hellwigb2950862008-12-02 09:54:17 -05001932static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001933{
1934 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1935}
1936
1937/*
1938 * basic readpage implementation. Locked extent state structs are inserted
1939 * into the tree that are removed when the IO is done (by the end_io
1940 * handlers)
1941 */
1942static int __extent_read_full_page(struct extent_io_tree *tree,
1943 struct page *page,
1944 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001945 struct bio **bio, int mirror_num,
1946 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001947{
1948 struct inode *inode = page->mapping->host;
1949 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1950 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1951 u64 end;
1952 u64 cur = start;
1953 u64 extent_offset;
1954 u64 last_byte = i_size_read(inode);
1955 u64 block_start;
1956 u64 cur_end;
1957 sector_t sector;
1958 struct extent_map *em;
1959 struct block_device *bdev;
1960 int ret;
1961 int nr = 0;
1962 size_t page_offset = 0;
1963 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001964 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001965 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001966 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001967
1968 set_page_extent_mapped(page);
1969
1970 end = page_end;
1971 lock_extent(tree, start, end, GFP_NOFS);
1972
Chris Masonc8b97812008-10-29 14:49:59 -04001973 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1974 char *userpage;
1975 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1976
1977 if (zero_offset) {
1978 iosize = PAGE_CACHE_SIZE - zero_offset;
1979 userpage = kmap_atomic(page, KM_USER0);
1980 memset(userpage + zero_offset, 0, iosize);
1981 flush_dcache_page(page);
1982 kunmap_atomic(userpage, KM_USER0);
1983 }
1984 }
Chris Masond1310b22008-01-24 16:13:08 -05001985 while (cur <= end) {
1986 if (cur >= last_byte) {
1987 char *userpage;
1988 iosize = PAGE_CACHE_SIZE - page_offset;
1989 userpage = kmap_atomic(page, KM_USER0);
1990 memset(userpage + page_offset, 0, iosize);
1991 flush_dcache_page(page);
1992 kunmap_atomic(userpage, KM_USER0);
1993 set_extent_uptodate(tree, cur, cur + iosize - 1,
1994 GFP_NOFS);
1995 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1996 break;
1997 }
1998 em = get_extent(inode, page, page_offset, cur,
1999 end - cur + 1, 0);
2000 if (IS_ERR(em) || !em) {
2001 SetPageError(page);
2002 unlock_extent(tree, cur, end, GFP_NOFS);
2003 break;
2004 }
Chris Masond1310b22008-01-24 16:13:08 -05002005 extent_offset = cur - em->start;
2006 BUG_ON(extent_map_end(em) <= cur);
2007 BUG_ON(end < cur);
2008
Chris Masonc8b97812008-10-29 14:49:59 -04002009 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2010 this_bio_flag = EXTENT_BIO_COMPRESSED;
2011
Chris Masond1310b22008-01-24 16:13:08 -05002012 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2013 cur_end = min(extent_map_end(em) - 1, end);
2014 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002015 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2016 disk_io_size = em->block_len;
2017 sector = em->block_start >> 9;
2018 } else {
2019 sector = (em->block_start + extent_offset) >> 9;
2020 disk_io_size = iosize;
2021 }
Chris Masond1310b22008-01-24 16:13:08 -05002022 bdev = em->bdev;
2023 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002024 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2025 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002026 free_extent_map(em);
2027 em = NULL;
2028
2029 /* we've found a hole, just zero and go on */
2030 if (block_start == EXTENT_MAP_HOLE) {
2031 char *userpage;
2032 userpage = kmap_atomic(page, KM_USER0);
2033 memset(userpage + page_offset, 0, iosize);
2034 flush_dcache_page(page);
2035 kunmap_atomic(userpage, KM_USER0);
2036
2037 set_extent_uptodate(tree, cur, cur + iosize - 1,
2038 GFP_NOFS);
2039 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2040 cur = cur + iosize;
2041 page_offset += iosize;
2042 continue;
2043 }
2044 /* the get_extent function already copied into the page */
2045 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002046 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002047 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2048 cur = cur + iosize;
2049 page_offset += iosize;
2050 continue;
2051 }
Chris Mason70dec802008-01-29 09:59:12 -05002052 /* we have an inline extent but it didn't get marked up
2053 * to date. Error out
2054 */
2055 if (block_start == EXTENT_MAP_INLINE) {
2056 SetPageError(page);
2057 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2058 cur = cur + iosize;
2059 page_offset += iosize;
2060 continue;
2061 }
Chris Masond1310b22008-01-24 16:13:08 -05002062
2063 ret = 0;
2064 if (tree->ops && tree->ops->readpage_io_hook) {
2065 ret = tree->ops->readpage_io_hook(page, cur,
2066 cur + iosize - 1);
2067 }
2068 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002069 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2070 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002071 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002072 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002073 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002074 end_bio_extent_readpage, mirror_num,
2075 *bio_flags,
2076 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002077 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002078 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002079 }
2080 if (ret)
2081 SetPageError(page);
2082 cur = cur + iosize;
2083 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002084 }
2085 if (!nr) {
2086 if (!PageError(page))
2087 SetPageUptodate(page);
2088 unlock_page(page);
2089 }
2090 return 0;
2091}
2092
2093int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2094 get_extent_t *get_extent)
2095{
2096 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002097 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002098 int ret;
2099
Chris Masonc8b97812008-10-29 14:49:59 -04002100 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2101 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002102 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002103 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002104 return ret;
2105}
Chris Masond1310b22008-01-24 16:13:08 -05002106
Chris Mason11c83492009-04-20 15:50:09 -04002107static noinline void update_nr_written(struct page *page,
2108 struct writeback_control *wbc,
2109 unsigned long nr_written)
2110{
2111 wbc->nr_to_write -= nr_written;
2112 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2113 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2114 page->mapping->writeback_index = page->index + nr_written;
2115}
2116
Chris Masond1310b22008-01-24 16:13:08 -05002117/*
2118 * the writepage semantics are similar to regular writepage. extent
2119 * records are inserted to lock ranges in the tree, and as dirty areas
2120 * are found, they are marked writeback. Then the lock bits are removed
2121 * and the end_io handler clears the writeback ranges
2122 */
2123static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2124 void *data)
2125{
2126 struct inode *inode = page->mapping->host;
2127 struct extent_page_data *epd = data;
2128 struct extent_io_tree *tree = epd->tree;
2129 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2130 u64 delalloc_start;
2131 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2132 u64 end;
2133 u64 cur = start;
2134 u64 extent_offset;
2135 u64 last_byte = i_size_read(inode);
2136 u64 block_start;
2137 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002138 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002139 sector_t sector;
2140 struct extent_map *em;
2141 struct block_device *bdev;
2142 int ret;
2143 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002144 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002145 size_t blocksize;
2146 loff_t i_size = i_size_read(inode);
2147 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2148 u64 nr_delalloc;
2149 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002150 int page_started;
2151 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002152 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002153 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002154
Chris Masonffbd5172009-04-20 15:50:09 -04002155 if (wbc->sync_mode == WB_SYNC_ALL)
2156 write_flags = WRITE_SYNC_PLUG;
2157 else
2158 write_flags = WRITE;
2159
Chris Masond1310b22008-01-24 16:13:08 -05002160 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002161 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002162 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002163 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002164 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002165 unlock_page(page);
2166 return 0;
2167 }
2168
2169 if (page->index == end_index) {
2170 char *userpage;
2171
Chris Masond1310b22008-01-24 16:13:08 -05002172 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002173 memset(userpage + pg_offset, 0,
2174 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002175 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002176 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002177 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002178 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002179
2180 set_page_extent_mapped(page);
2181
2182 delalloc_start = start;
2183 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002184 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002185 if (!epd->extent_locked) {
Chris Mason11c83492009-04-20 15:50:09 -04002186 /*
2187 * make sure the wbc mapping index is at least updated
2188 * to this page.
2189 */
2190 update_nr_written(page, wbc, 0);
2191
Chris Masond3977122009-01-05 21:25:51 -05002192 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002193 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002194 page,
2195 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002196 &delalloc_end,
2197 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002198 if (nr_delalloc == 0) {
2199 delalloc_start = delalloc_end + 1;
2200 continue;
2201 }
2202 tree->ops->fill_delalloc(inode, page, delalloc_start,
2203 delalloc_end, &page_started,
2204 &nr_written);
Chris Masond1310b22008-01-24 16:13:08 -05002205 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002206 }
Chris Masonc8b97812008-10-29 14:49:59 -04002207
Chris Mason771ed682008-11-06 22:02:51 -05002208 /* did the fill delalloc function already unlock and start
2209 * the IO?
2210 */
2211 if (page_started) {
2212 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002213 /*
2214 * we've unlocked the page, so we can't update
2215 * the mapping's writeback index, just update
2216 * nr_to_write.
2217 */
2218 wbc->nr_to_write -= nr_written;
2219 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002220 }
Chris Masonc8b97812008-10-29 14:49:59 -04002221 }
Chris Masond1310b22008-01-24 16:13:08 -05002222 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -05002223
Chris Masone6dcd2d2008-07-17 12:53:50 -04002224 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002225
Chris Mason247e7432008-07-17 12:53:51 -04002226 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002227 ret = tree->ops->writepage_start_hook(page, start,
2228 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002229 if (ret == -EAGAIN) {
2230 unlock_extent(tree, start, page_end, GFP_NOFS);
2231 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002232 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002233 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002234 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002235 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002236 }
2237 }
2238
Chris Mason11c83492009-04-20 15:50:09 -04002239 /*
2240 * we don't want to touch the inode after unlocking the page,
2241 * so we update the mapping writeback index now
2242 */
2243 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002244
Chris Masond1310b22008-01-24 16:13:08 -05002245 end = page_end;
Chris Masond3977122009-01-05 21:25:51 -05002246 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0))
2247 printk(KERN_ERR "btrfs delalloc bits after lock_extent\n");
Chris Masond1310b22008-01-24 16:13:08 -05002248
2249 if (last_byte <= start) {
2250 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251 unlock_extent(tree, start, page_end, GFP_NOFS);
2252 if (tree->ops && tree->ops->writepage_end_io_hook)
2253 tree->ops->writepage_end_io_hook(page, start,
2254 page_end, NULL, 1);
2255 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002256 goto done;
2257 }
2258
2259 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2260 blocksize = inode->i_sb->s_blocksize;
2261
2262 while (cur <= end) {
2263 if (cur >= last_byte) {
2264 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002265 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2266 if (tree->ops && tree->ops->writepage_end_io_hook)
2267 tree->ops->writepage_end_io_hook(page, cur,
2268 page_end, NULL, 1);
2269 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002270 break;
2271 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002272 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002273 end - cur + 1, 1);
2274 if (IS_ERR(em) || !em) {
2275 SetPageError(page);
2276 break;
2277 }
2278
2279 extent_offset = cur - em->start;
2280 BUG_ON(extent_map_end(em) <= cur);
2281 BUG_ON(end < cur);
2282 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2283 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2284 sector = (em->block_start + extent_offset) >> 9;
2285 bdev = em->bdev;
2286 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002287 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002288 free_extent_map(em);
2289 em = NULL;
2290
Chris Masonc8b97812008-10-29 14:49:59 -04002291 /*
2292 * compressed and inline extents are written through other
2293 * paths in the FS
2294 */
2295 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002296 block_start == EXTENT_MAP_INLINE) {
2297 clear_extent_dirty(tree, cur,
2298 cur + iosize - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002299
Chris Masond3977122009-01-05 21:25:51 -05002300 unlock_extent(tree, unlock_start, cur + iosize - 1,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002301 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002302
Chris Masonc8b97812008-10-29 14:49:59 -04002303 /*
2304 * end_io notification does not happen here for
2305 * compressed extents
2306 */
2307 if (!compressed && tree->ops &&
2308 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002309 tree->ops->writepage_end_io_hook(page, cur,
2310 cur + iosize - 1,
2311 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002312 else if (compressed) {
2313 /* we don't want to end_page_writeback on
2314 * a compressed extent. this happens
2315 * elsewhere
2316 */
2317 nr++;
2318 }
2319
2320 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002321 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002322 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002323 continue;
2324 }
Chris Masond1310b22008-01-24 16:13:08 -05002325 /* leave this out until we have a page_mkwrite call */
2326 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2327 EXTENT_DIRTY, 0)) {
2328 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002329 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002330 continue;
2331 }
Chris Masonc8b97812008-10-29 14:49:59 -04002332
Chris Masond1310b22008-01-24 16:13:08 -05002333 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2334 if (tree->ops && tree->ops->writepage_io_hook) {
2335 ret = tree->ops->writepage_io_hook(page, cur,
2336 cur + iosize - 1);
2337 } else {
2338 ret = 0;
2339 }
Chris Mason1259ab72008-05-12 13:39:03 -04002340 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002341 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002342 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002343 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002344
Chris Masond1310b22008-01-24 16:13:08 -05002345 set_range_writeback(tree, cur, cur + iosize - 1);
2346 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002347 printk(KERN_ERR "btrfs warning page %lu not "
2348 "writeback, cur %llu end %llu\n",
2349 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002350 (unsigned long long)end);
2351 }
2352
Chris Masonffbd5172009-04-20 15:50:09 -04002353 ret = submit_extent_page(write_flags, tree, page,
2354 sector, iosize, pg_offset,
2355 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002356 end_bio_extent_writepage,
2357 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002358 if (ret)
2359 SetPageError(page);
2360 }
2361 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002362 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002363 nr++;
2364 }
2365done:
2366 if (nr == 0) {
2367 /* make sure the mapping tag for page dirty gets cleared */
2368 set_page_writeback(page);
2369 end_page_writeback(page);
2370 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002371 if (unlock_start <= page_end)
2372 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002373 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002374
Chris Mason11c83492009-04-20 15:50:09 -04002375done_unlocked:
2376
Chris Masond1310b22008-01-24 16:13:08 -05002377 return 0;
2378}
2379
Chris Masond1310b22008-01-24 16:13:08 -05002380/**
Chris Mason4bef0842008-09-08 11:18:08 -04002381 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05002382 * @mapping: address space structure to write
2383 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2384 * @writepage: function called for each page
2385 * @data: data passed to writepage function
2386 *
2387 * If a page is already under I/O, write_cache_pages() skips it, even
2388 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2389 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2390 * and msync() need to guarantee that all the data which was dirty at the time
2391 * the call was made get new I/O started against them. If wbc->sync_mode is
2392 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2393 * existing IO to complete.
2394 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002395static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002396 struct address_space *mapping,
2397 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002398 writepage_t writepage, void *data,
2399 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002400{
2401 struct backing_dev_info *bdi = mapping->backing_dev_info;
2402 int ret = 0;
2403 int done = 0;
2404 struct pagevec pvec;
2405 int nr_pages;
2406 pgoff_t index;
2407 pgoff_t end; /* Inclusive */
2408 int scanned = 0;
2409 int range_whole = 0;
2410
Chris Masond1310b22008-01-24 16:13:08 -05002411 pagevec_init(&pvec, 0);
2412 if (wbc->range_cyclic) {
2413 index = mapping->writeback_index; /* Start from prev offset */
2414 end = -1;
2415 } else {
2416 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2417 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2418 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2419 range_whole = 1;
2420 scanned = 1;
2421 }
2422retry:
2423 while (!done && (index <= end) &&
2424 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002425 PAGECACHE_TAG_DIRTY, min(end - index,
2426 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002427 unsigned i;
2428
2429 scanned = 1;
2430 for (i = 0; i < nr_pages; i++) {
2431 struct page *page = pvec.pages[i];
2432
2433 /*
2434 * At this point we hold neither mapping->tree_lock nor
2435 * lock on the page itself: the page may be truncated or
2436 * invalidated (changing page->mapping to NULL), or even
2437 * swizzled back from swapper_space to tmpfs file
2438 * mapping
2439 */
Chris Mason4bef0842008-09-08 11:18:08 -04002440 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2441 tree->ops->write_cache_pages_lock_hook(page);
2442 else
2443 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002444
2445 if (unlikely(page->mapping != mapping)) {
2446 unlock_page(page);
2447 continue;
2448 }
2449
2450 if (!wbc->range_cyclic && page->index > end) {
2451 done = 1;
2452 unlock_page(page);
2453 continue;
2454 }
2455
Chris Masond2c3f4f2008-11-19 12:44:22 -05002456 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002457 if (PageWriteback(page))
2458 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002459 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002460 }
Chris Masond1310b22008-01-24 16:13:08 -05002461
2462 if (PageWriteback(page) ||
2463 !clear_page_dirty_for_io(page)) {
2464 unlock_page(page);
2465 continue;
2466 }
2467
2468 ret = (*writepage)(page, wbc, data);
2469
2470 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2471 unlock_page(page);
2472 ret = 0;
2473 }
Chris Mason771ed682008-11-06 22:02:51 -05002474 if (ret || wbc->nr_to_write <= 0)
Chris Masond1310b22008-01-24 16:13:08 -05002475 done = 1;
2476 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2477 wbc->encountered_congestion = 1;
2478 done = 1;
2479 }
2480 }
2481 pagevec_release(&pvec);
2482 cond_resched();
2483 }
2484 if (!scanned && !done) {
2485 /*
2486 * We hit the last page and there is more work to be done: wrap
2487 * back to the start of the file
2488 */
2489 scanned = 1;
2490 index = 0;
2491 goto retry;
2492 }
Chris Masond1310b22008-01-24 16:13:08 -05002493 return ret;
2494}
Chris Masond1310b22008-01-24 16:13:08 -05002495
Chris Masonffbd5172009-04-20 15:50:09 -04002496static void flush_epd_write_bio(struct extent_page_data *epd)
2497{
2498 if (epd->bio) {
2499 if (epd->sync_io)
2500 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2501 else
2502 submit_one_bio(WRITE, epd->bio, 0, 0);
2503 epd->bio = NULL;
2504 }
2505}
2506
Chris Masond2c3f4f2008-11-19 12:44:22 -05002507static noinline void flush_write_bio(void *data)
2508{
2509 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002510 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002511}
2512
Chris Masond1310b22008-01-24 16:13:08 -05002513int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2514 get_extent_t *get_extent,
2515 struct writeback_control *wbc)
2516{
2517 int ret;
2518 struct address_space *mapping = page->mapping;
2519 struct extent_page_data epd = {
2520 .bio = NULL,
2521 .tree = tree,
2522 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002523 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002524 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002525 };
2526 struct writeback_control wbc_writepages = {
2527 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002528 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002529 .older_than_this = NULL,
2530 .nr_to_write = 64,
2531 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2532 .range_end = (loff_t)-1,
2533 };
2534
Chris Masond1310b22008-01-24 16:13:08 -05002535 ret = __extent_writepage(page, wbc, &epd);
2536
Chris Mason4bef0842008-09-08 11:18:08 -04002537 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002538 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002539 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002540 return ret;
2541}
Chris Masond1310b22008-01-24 16:13:08 -05002542
Chris Mason771ed682008-11-06 22:02:51 -05002543int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2544 u64 start, u64 end, get_extent_t *get_extent,
2545 int mode)
2546{
2547 int ret = 0;
2548 struct address_space *mapping = inode->i_mapping;
2549 struct page *page;
2550 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2551 PAGE_CACHE_SHIFT;
2552
2553 struct extent_page_data epd = {
2554 .bio = NULL,
2555 .tree = tree,
2556 .get_extent = get_extent,
2557 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002558 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002559 };
2560 struct writeback_control wbc_writepages = {
2561 .bdi = inode->i_mapping->backing_dev_info,
2562 .sync_mode = mode,
2563 .older_than_this = NULL,
2564 .nr_to_write = nr_pages * 2,
2565 .range_start = start,
2566 .range_end = end + 1,
2567 };
2568
Chris Masond3977122009-01-05 21:25:51 -05002569 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002570 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2571 if (clear_page_dirty_for_io(page))
2572 ret = __extent_writepage(page, &wbc_writepages, &epd);
2573 else {
2574 if (tree->ops && tree->ops->writepage_end_io_hook)
2575 tree->ops->writepage_end_io_hook(page, start,
2576 start + PAGE_CACHE_SIZE - 1,
2577 NULL, 1);
2578 unlock_page(page);
2579 }
2580 page_cache_release(page);
2581 start += PAGE_CACHE_SIZE;
2582 }
2583
Chris Masonffbd5172009-04-20 15:50:09 -04002584 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002585 return ret;
2586}
Chris Masond1310b22008-01-24 16:13:08 -05002587
2588int extent_writepages(struct extent_io_tree *tree,
2589 struct address_space *mapping,
2590 get_extent_t *get_extent,
2591 struct writeback_control *wbc)
2592{
2593 int ret = 0;
2594 struct extent_page_data epd = {
2595 .bio = NULL,
2596 .tree = tree,
2597 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002598 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002599 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002600 };
2601
Chris Mason4bef0842008-09-08 11:18:08 -04002602 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002603 __extent_writepage, &epd,
2604 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002605 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002606 return ret;
2607}
Chris Masond1310b22008-01-24 16:13:08 -05002608
2609int extent_readpages(struct extent_io_tree *tree,
2610 struct address_space *mapping,
2611 struct list_head *pages, unsigned nr_pages,
2612 get_extent_t get_extent)
2613{
2614 struct bio *bio = NULL;
2615 unsigned page_idx;
2616 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002617 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002618
2619 pagevec_init(&pvec, 0);
2620 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2621 struct page *page = list_entry(pages->prev, struct page, lru);
2622
2623 prefetchw(&page->flags);
2624 list_del(&page->lru);
2625 /*
2626 * what we want to do here is call add_to_page_cache_lru,
2627 * but that isn't exported, so we reproduce it here
2628 */
2629 if (!add_to_page_cache(page, mapping,
2630 page->index, GFP_KERNEL)) {
2631
2632 /* open coding of lru_cache_add, also not exported */
2633 page_cache_get(page);
2634 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002635 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002636 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002637 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002638 }
2639 page_cache_release(page);
2640 }
2641 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002642 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002643 BUG_ON(!list_empty(pages));
2644 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002645 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002646 return 0;
2647}
Chris Masond1310b22008-01-24 16:13:08 -05002648
2649/*
2650 * basic invalidatepage code, this waits on any locked or writeback
2651 * ranges corresponding to the page, and then deletes any extent state
2652 * records from the tree
2653 */
2654int extent_invalidatepage(struct extent_io_tree *tree,
2655 struct page *page, unsigned long offset)
2656{
2657 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2658 u64 end = start + PAGE_CACHE_SIZE - 1;
2659 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2660
Chris Masond3977122009-01-05 21:25:51 -05002661 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002662 if (start > end)
2663 return 0;
2664
2665 lock_extent(tree, start, end, GFP_NOFS);
2666 wait_on_extent_writeback(tree, start, end);
2667 clear_extent_bit(tree, start, end,
2668 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2669 1, 1, GFP_NOFS);
2670 return 0;
2671}
Chris Masond1310b22008-01-24 16:13:08 -05002672
2673/*
2674 * simple commit_write call, set_range_dirty is used to mark both
2675 * the pages and the extent records as dirty
2676 */
2677int extent_commit_write(struct extent_io_tree *tree,
2678 struct inode *inode, struct page *page,
2679 unsigned from, unsigned to)
2680{
2681 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2682
2683 set_page_extent_mapped(page);
2684 set_page_dirty(page);
2685
2686 if (pos > inode->i_size) {
2687 i_size_write(inode, pos);
2688 mark_inode_dirty(inode);
2689 }
2690 return 0;
2691}
Chris Masond1310b22008-01-24 16:13:08 -05002692
2693int extent_prepare_write(struct extent_io_tree *tree,
2694 struct inode *inode, struct page *page,
2695 unsigned from, unsigned to, get_extent_t *get_extent)
2696{
2697 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2698 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2699 u64 block_start;
2700 u64 orig_block_start;
2701 u64 block_end;
2702 u64 cur_end;
2703 struct extent_map *em;
2704 unsigned blocksize = 1 << inode->i_blkbits;
2705 size_t page_offset = 0;
2706 size_t block_off_start;
2707 size_t block_off_end;
2708 int err = 0;
2709 int iocount = 0;
2710 int ret = 0;
2711 int isnew;
2712
2713 set_page_extent_mapped(page);
2714
2715 block_start = (page_start + from) & ~((u64)blocksize - 1);
2716 block_end = (page_start + to - 1) | (blocksize - 1);
2717 orig_block_start = block_start;
2718
2719 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002720 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002721 em = get_extent(inode, page, page_offset, block_start,
2722 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002723 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002724 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002725
Chris Masond1310b22008-01-24 16:13:08 -05002726 cur_end = min(block_end, extent_map_end(em) - 1);
2727 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2728 block_off_end = block_off_start + blocksize;
2729 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2730
2731 if (!PageUptodate(page) && isnew &&
2732 (block_off_end > to || block_off_start < from)) {
2733 void *kaddr;
2734
2735 kaddr = kmap_atomic(page, KM_USER0);
2736 if (block_off_end > to)
2737 memset(kaddr + to, 0, block_off_end - to);
2738 if (block_off_start < from)
2739 memset(kaddr + block_off_start, 0,
2740 from - block_off_start);
2741 flush_dcache_page(page);
2742 kunmap_atomic(kaddr, KM_USER0);
2743 }
2744 if ((em->block_start != EXTENT_MAP_HOLE &&
2745 em->block_start != EXTENT_MAP_INLINE) &&
2746 !isnew && !PageUptodate(page) &&
2747 (block_off_end > to || block_off_start < from) &&
2748 !test_range_bit(tree, block_start, cur_end,
2749 EXTENT_UPTODATE, 1)) {
2750 u64 sector;
2751 u64 extent_offset = block_start - em->start;
2752 size_t iosize;
2753 sector = (em->block_start + extent_offset) >> 9;
2754 iosize = (cur_end - block_start + blocksize) &
2755 ~((u64)blocksize - 1);
2756 /*
2757 * we've already got the extent locked, but we
2758 * need to split the state such that our end_bio
2759 * handler can clear the lock.
2760 */
2761 set_extent_bit(tree, block_start,
2762 block_start + iosize - 1,
2763 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2764 ret = submit_extent_page(READ, tree, page,
2765 sector, iosize, page_offset, em->bdev,
2766 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002767 end_bio_extent_preparewrite, 0,
2768 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002769 iocount++;
2770 block_start = block_start + iosize;
2771 } else {
2772 set_extent_uptodate(tree, block_start, cur_end,
2773 GFP_NOFS);
2774 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2775 block_start = cur_end + 1;
2776 }
2777 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2778 free_extent_map(em);
2779 }
2780 if (iocount) {
2781 wait_extent_bit(tree, orig_block_start,
2782 block_end, EXTENT_LOCKED);
2783 }
2784 check_page_uptodate(tree, page);
2785err:
2786 /* FIXME, zero out newly allocated blocks on error */
2787 return err;
2788}
Chris Masond1310b22008-01-24 16:13:08 -05002789
2790/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002791 * a helper for releasepage, this tests for areas of the page that
2792 * are locked or under IO and drops the related state bits if it is safe
2793 * to drop the page.
2794 */
2795int try_release_extent_state(struct extent_map_tree *map,
2796 struct extent_io_tree *tree, struct page *page,
2797 gfp_t mask)
2798{
2799 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2800 u64 end = start + PAGE_CACHE_SIZE - 1;
2801 int ret = 1;
2802
Chris Mason211f90e2008-07-18 11:56:15 -04002803 if (test_range_bit(tree, start, end,
2804 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002805 ret = 0;
2806 else {
2807 if ((mask & GFP_NOFS) == GFP_NOFS)
2808 mask = GFP_NOFS;
2809 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2810 1, 1, mask);
2811 }
2812 return ret;
2813}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002814
2815/*
Chris Masond1310b22008-01-24 16:13:08 -05002816 * a helper for releasepage. As long as there are no locked extents
2817 * in the range corresponding to the page, both state records and extent
2818 * map records are removed
2819 */
2820int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002821 struct extent_io_tree *tree, struct page *page,
2822 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002823{
2824 struct extent_map *em;
2825 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2826 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002827
Chris Mason70dec802008-01-29 09:59:12 -05002828 if ((mask & __GFP_WAIT) &&
2829 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002830 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002831 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002832 len = end - start + 1;
Chris Mason70dec802008-01-29 09:59:12 -05002833 spin_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002834 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002835 if (!em || IS_ERR(em)) {
2836 spin_unlock(&map->lock);
2837 break;
2838 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002839 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2840 em->start != start) {
Chris Mason70dec802008-01-29 09:59:12 -05002841 spin_unlock(&map->lock);
2842 free_extent_map(em);
2843 break;
2844 }
2845 if (!test_range_bit(tree, em->start,
2846 extent_map_end(em) - 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002847 EXTENT_LOCKED | EXTENT_WRITEBACK |
2848 EXTENT_ORDERED,
2849 0)) {
Chris Mason70dec802008-01-29 09:59:12 -05002850 remove_extent_mapping(map, em);
2851 /* once for the rb tree */
2852 free_extent_map(em);
2853 }
2854 start = extent_map_end(em);
Chris Masond1310b22008-01-24 16:13:08 -05002855 spin_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002856
2857 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002858 free_extent_map(em);
2859 }
Chris Masond1310b22008-01-24 16:13:08 -05002860 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002861 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002862}
Chris Masond1310b22008-01-24 16:13:08 -05002863
2864sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2865 get_extent_t *get_extent)
2866{
2867 struct inode *inode = mapping->host;
2868 u64 start = iblock << inode->i_blkbits;
2869 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002870 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002871 struct extent_map *em;
2872
Yan Zhengd899e052008-10-30 14:25:28 -04002873 lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2874 GFP_NOFS);
2875 em = get_extent(inode, NULL, 0, start, blksize, 0);
2876 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2877 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002878 if (!em || IS_ERR(em))
2879 return 0;
2880
Yan Zhengd899e052008-10-30 14:25:28 -04002881 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002882 goto out;
2883
2884 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002885out:
2886 free_extent_map(em);
2887 return sector;
2888}
2889
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002890int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2891 __u64 start, __u64 len, get_extent_t *get_extent)
2892{
2893 int ret;
2894 u64 off = start;
2895 u64 max = start + len;
2896 u32 flags = 0;
2897 u64 disko = 0;
2898 struct extent_map *em = NULL;
2899 int end = 0;
2900 u64 em_start = 0, em_len = 0;
2901 unsigned long emflags;
2902 ret = 0;
2903
2904 if (len == 0)
2905 return -EINVAL;
2906
2907 lock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2908 GFP_NOFS);
2909 em = get_extent(inode, NULL, 0, off, max - off, 0);
2910 if (!em)
2911 goto out;
2912 if (IS_ERR(em)) {
2913 ret = PTR_ERR(em);
2914 goto out;
2915 }
2916 while (!end) {
2917 off = em->start + em->len;
2918 if (off >= max)
2919 end = 1;
2920
2921 em_start = em->start;
2922 em_len = em->len;
2923
2924 disko = 0;
2925 flags = 0;
2926
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002927 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002928 end = 1;
2929 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002930 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002931 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002932 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002933 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2934 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002935 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002936 flags |= (FIEMAP_EXTENT_DELALLOC |
2937 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002938 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002939 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002940 }
2941 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2942 flags |= FIEMAP_EXTENT_ENCODED;
2943
2944 emflags = em->flags;
2945 free_extent_map(em);
2946 em = NULL;
2947
2948 if (!end) {
2949 em = get_extent(inode, NULL, 0, off, max - off, 0);
2950 if (!em)
2951 goto out;
2952 if (IS_ERR(em)) {
2953 ret = PTR_ERR(em);
2954 goto out;
2955 }
2956 emflags = em->flags;
2957 }
2958 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
2959 flags |= FIEMAP_EXTENT_LAST;
2960 end = 1;
2961 }
2962
2963 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2964 em_len, flags);
2965 if (ret)
2966 goto out_free;
2967 }
2968out_free:
2969 free_extent_map(em);
2970out:
2971 unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len,
2972 GFP_NOFS);
2973 return ret;
2974}
2975
Chris Masond1310b22008-01-24 16:13:08 -05002976static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2977 unsigned long i)
2978{
2979 struct page *p;
2980 struct address_space *mapping;
2981
2982 if (i == 0)
2983 return eb->first_page;
2984 i += eb->start >> PAGE_CACHE_SHIFT;
2985 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002986 if (!mapping)
2987 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002988
2989 /*
2990 * extent_buffer_page is only called after pinning the page
2991 * by increasing the reference count. So we know the page must
2992 * be in the radix tree.
2993 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002994 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002995 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002996 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002997
Chris Masond1310b22008-01-24 16:13:08 -05002998 return p;
2999}
3000
Chris Mason6af118ce2008-07-22 11:18:07 -04003001static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003002{
Chris Mason6af118ce2008-07-22 11:18:07 -04003003 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3004 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003005}
3006
Chris Masond1310b22008-01-24 16:13:08 -05003007static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3008 u64 start,
3009 unsigned long len,
3010 gfp_t mask)
3011{
3012 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003013#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003014 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003015#endif
Chris Masond1310b22008-01-24 16:13:08 -05003016
Chris Masond1310b22008-01-24 16:13:08 -05003017 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003018 eb->start = start;
3019 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003020 spin_lock_init(&eb->lock);
3021 init_waitqueue_head(&eb->lock_wq);
3022
Chris Mason39351272009-02-04 09:24:05 -05003023#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003024 spin_lock_irqsave(&leak_lock, flags);
3025 list_add(&eb->leak_list, &buffers);
3026 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003027#endif
Chris Masond1310b22008-01-24 16:13:08 -05003028 atomic_set(&eb->refs, 1);
3029
3030 return eb;
3031}
3032
3033static void __free_extent_buffer(struct extent_buffer *eb)
3034{
Chris Mason39351272009-02-04 09:24:05 -05003035#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003036 unsigned long flags;
3037 spin_lock_irqsave(&leak_lock, flags);
3038 list_del(&eb->leak_list);
3039 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003040#endif
Chris Masond1310b22008-01-24 16:13:08 -05003041 kmem_cache_free(extent_buffer_cache, eb);
3042}
3043
3044struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3045 u64 start, unsigned long len,
3046 struct page *page0,
3047 gfp_t mask)
3048{
3049 unsigned long num_pages = num_extent_pages(start, len);
3050 unsigned long i;
3051 unsigned long index = start >> PAGE_CACHE_SHIFT;
3052 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003053 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003054 struct page *p;
3055 struct address_space *mapping = tree->mapping;
3056 int uptodate = 1;
3057
Chris Mason6af118ce2008-07-22 11:18:07 -04003058 spin_lock(&tree->buffer_lock);
3059 eb = buffer_search(tree, start);
3060 if (eb) {
3061 atomic_inc(&eb->refs);
3062 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003063 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003064 return eb;
3065 }
3066 spin_unlock(&tree->buffer_lock);
3067
Chris Masond1310b22008-01-24 16:13:08 -05003068 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003069 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003070 return NULL;
3071
Chris Masond1310b22008-01-24 16:13:08 -05003072 if (page0) {
3073 eb->first_page = page0;
3074 i = 1;
3075 index++;
3076 page_cache_get(page0);
3077 mark_page_accessed(page0);
3078 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003079 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003080 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003081 } else {
3082 i = 0;
3083 }
3084 for (; i < num_pages; i++, index++) {
3085 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3086 if (!p) {
3087 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003088 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003089 }
3090 set_page_extent_mapped(p);
3091 mark_page_accessed(p);
3092 if (i == 0) {
3093 eb->first_page = p;
3094 set_page_extent_head(p, len);
3095 } else {
3096 set_page_private(p, EXTENT_PAGE_PRIVATE);
3097 }
3098 if (!PageUptodate(p))
3099 uptodate = 0;
3100 unlock_page(p);
3101 }
3102 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003103 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003104
Chris Mason6af118ce2008-07-22 11:18:07 -04003105 spin_lock(&tree->buffer_lock);
3106 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3107 if (exists) {
3108 /* add one reference for the caller */
3109 atomic_inc(&exists->refs);
3110 spin_unlock(&tree->buffer_lock);
3111 goto free_eb;
3112 }
3113 spin_unlock(&tree->buffer_lock);
3114
3115 /* add one reference for the tree */
3116 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05003117 return eb;
3118
Chris Mason6af118ce2008-07-22 11:18:07 -04003119free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003120 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003121 return exists;
3122 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003123 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003124 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003125 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003126 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003127}
Chris Masond1310b22008-01-24 16:13:08 -05003128
3129struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3130 u64 start, unsigned long len,
3131 gfp_t mask)
3132{
Chris Masond1310b22008-01-24 16:13:08 -05003133 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003134
Chris Mason6af118ce2008-07-22 11:18:07 -04003135 spin_lock(&tree->buffer_lock);
3136 eb = buffer_search(tree, start);
3137 if (eb)
3138 atomic_inc(&eb->refs);
3139 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003140
Josef Bacik0f9dd462008-09-23 13:14:11 -04003141 if (eb)
3142 mark_page_accessed(eb->first_page);
3143
Chris Masond1310b22008-01-24 16:13:08 -05003144 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003145}
Chris Masond1310b22008-01-24 16:13:08 -05003146
3147void free_extent_buffer(struct extent_buffer *eb)
3148{
Chris Masond1310b22008-01-24 16:13:08 -05003149 if (!eb)
3150 return;
3151
3152 if (!atomic_dec_and_test(&eb->refs))
3153 return;
3154
Chris Mason6af118ce2008-07-22 11:18:07 -04003155 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003156}
Chris Masond1310b22008-01-24 16:13:08 -05003157
3158int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3159 struct extent_buffer *eb)
3160{
Chris Masond1310b22008-01-24 16:13:08 -05003161 unsigned long i;
3162 unsigned long num_pages;
3163 struct page *page;
3164
Chris Masond1310b22008-01-24 16:13:08 -05003165 num_pages = num_extent_pages(eb->start, eb->len);
3166
3167 for (i = 0; i < num_pages; i++) {
3168 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003169 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003170 continue;
3171
Chris Masona61e6f22008-07-22 11:18:08 -04003172 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003173 if (i == 0)
3174 set_page_extent_head(page, eb->len);
3175 else
3176 set_page_private(page, EXTENT_PAGE_PRIVATE);
3177
Chris Masond1310b22008-01-24 16:13:08 -05003178 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003179 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003180 if (!PageDirty(page)) {
3181 radix_tree_tag_clear(&page->mapping->page_tree,
3182 page_index(page),
3183 PAGECACHE_TAG_DIRTY);
3184 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003185 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003186 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003187 }
3188 return 0;
3189}
Chris Masond1310b22008-01-24 16:13:08 -05003190
3191int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3192 struct extent_buffer *eb)
3193{
3194 return wait_on_extent_writeback(tree, eb->start,
3195 eb->start + eb->len - 1);
3196}
Chris Masond1310b22008-01-24 16:13:08 -05003197
3198int set_extent_buffer_dirty(struct extent_io_tree *tree,
3199 struct extent_buffer *eb)
3200{
3201 unsigned long i;
3202 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003203 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003204
Chris Masonb9473432009-03-13 11:00:37 -04003205 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003206 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003207 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003208 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003209 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003210}
Chris Masond1310b22008-01-24 16:13:08 -05003211
Chris Mason1259ab72008-05-12 13:39:03 -04003212int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3213 struct extent_buffer *eb)
3214{
3215 unsigned long i;
3216 struct page *page;
3217 unsigned long num_pages;
3218
3219 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003220 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003221
3222 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3223 GFP_NOFS);
3224 for (i = 0; i < num_pages; i++) {
3225 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003226 if (page)
3227 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003228 }
3229 return 0;
3230}
3231
Chris Masond1310b22008-01-24 16:13:08 -05003232int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3233 struct extent_buffer *eb)
3234{
3235 unsigned long i;
3236 struct page *page;
3237 unsigned long num_pages;
3238
3239 num_pages = num_extent_pages(eb->start, eb->len);
3240
3241 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3242 GFP_NOFS);
3243 for (i = 0; i < num_pages; i++) {
3244 page = extent_buffer_page(eb, i);
3245 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3246 ((i == num_pages - 1) &&
3247 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3248 check_page_uptodate(tree, page);
3249 continue;
3250 }
3251 SetPageUptodate(page);
3252 }
3253 return 0;
3254}
Chris Masond1310b22008-01-24 16:13:08 -05003255
Chris Masonce9adaa2008-04-09 16:28:12 -04003256int extent_range_uptodate(struct extent_io_tree *tree,
3257 u64 start, u64 end)
3258{
3259 struct page *page;
3260 int ret;
3261 int pg_uptodate = 1;
3262 int uptodate;
3263 unsigned long index;
3264
3265 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
3266 if (ret)
3267 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003268 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003269 index = start >> PAGE_CACHE_SHIFT;
3270 page = find_get_page(tree->mapping, index);
3271 uptodate = PageUptodate(page);
3272 page_cache_release(page);
3273 if (!uptodate) {
3274 pg_uptodate = 0;
3275 break;
3276 }
3277 start += PAGE_CACHE_SIZE;
3278 }
3279 return pg_uptodate;
3280}
3281
Chris Masond1310b22008-01-24 16:13:08 -05003282int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04003283 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003284{
Chris Mason728131d2008-04-09 16:28:12 -04003285 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003286 unsigned long num_pages;
3287 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003288 struct page *page;
3289 int pg_uptodate = 1;
3290
Chris Masonb4ce94d2009-02-04 09:25:08 -05003291 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003292 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003293
Chris Mason42352982008-04-28 16:40:52 -04003294 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003295 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003296 if (ret)
3297 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003298
3299 num_pages = num_extent_pages(eb->start, eb->len);
3300 for (i = 0; i < num_pages; i++) {
3301 page = extent_buffer_page(eb, i);
3302 if (!PageUptodate(page)) {
3303 pg_uptodate = 0;
3304 break;
3305 }
3306 }
Chris Mason42352982008-04-28 16:40:52 -04003307 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003308}
Chris Masond1310b22008-01-24 16:13:08 -05003309
3310int read_extent_buffer_pages(struct extent_io_tree *tree,
3311 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003312 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003313 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003314{
3315 unsigned long i;
3316 unsigned long start_i;
3317 struct page *page;
3318 int err;
3319 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003320 int locked_pages = 0;
3321 int all_uptodate = 1;
3322 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003323 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003324 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003325 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003326
Chris Masonb4ce94d2009-02-04 09:25:08 -05003327 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003328 return 0;
3329
Chris Masonce9adaa2008-04-09 16:28:12 -04003330 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003331 EXTENT_UPTODATE, 1)) {
3332 return 0;
3333 }
3334
3335 if (start) {
3336 WARN_ON(start < eb->start);
3337 start_i = (start >> PAGE_CACHE_SHIFT) -
3338 (eb->start >> PAGE_CACHE_SHIFT);
3339 } else {
3340 start_i = 0;
3341 }
3342
3343 num_pages = num_extent_pages(eb->start, eb->len);
3344 for (i = start_i; i < num_pages; i++) {
3345 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003346 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003347 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003348 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003349 } else {
3350 lock_page(page);
3351 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003352 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003353 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003354 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003355 }
3356 if (all_uptodate) {
3357 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003358 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003359 goto unlock_exit;
3360 }
3361
3362 for (i = start_i; i < num_pages; i++) {
3363 page = extent_buffer_page(eb, i);
3364 if (inc_all_pages)
3365 page_cache_get(page);
3366 if (!PageUptodate(page)) {
3367 if (start_i == 0)
3368 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003369 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003370 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003371 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003372 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003373 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003374 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003375 } else {
3376 unlock_page(page);
3377 }
3378 }
3379
Chris Masona86c12c2008-02-07 10:50:54 -05003380 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003381 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003382
Chris Masond3977122009-01-05 21:25:51 -05003383 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003384 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003385
Chris Masond1310b22008-01-24 16:13:08 -05003386 for (i = start_i; i < num_pages; i++) {
3387 page = extent_buffer_page(eb, i);
3388 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003389 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003390 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003391 }
Chris Masond3977122009-01-05 21:25:51 -05003392
Chris Masond1310b22008-01-24 16:13:08 -05003393 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003394 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003395 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003396
3397unlock_exit:
3398 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003399 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003400 page = extent_buffer_page(eb, i);
3401 i++;
3402 unlock_page(page);
3403 locked_pages--;
3404 }
3405 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003406}
Chris Masond1310b22008-01-24 16:13:08 -05003407
3408void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3409 unsigned long start,
3410 unsigned long len)
3411{
3412 size_t cur;
3413 size_t offset;
3414 struct page *page;
3415 char *kaddr;
3416 char *dst = (char *)dstv;
3417 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3418 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003419
3420 WARN_ON(start > eb->len);
3421 WARN_ON(start + len > eb->start + eb->len);
3422
3423 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3424
Chris Masond3977122009-01-05 21:25:51 -05003425 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003426 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003427
3428 cur = min(len, (PAGE_CACHE_SIZE - offset));
3429 kaddr = kmap_atomic(page, KM_USER1);
3430 memcpy(dst, kaddr + offset, cur);
3431 kunmap_atomic(kaddr, KM_USER1);
3432
3433 dst += cur;
3434 len -= cur;
3435 offset = 0;
3436 i++;
3437 }
3438}
Chris Masond1310b22008-01-24 16:13:08 -05003439
3440int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3441 unsigned long min_len, char **token, char **map,
3442 unsigned long *map_start,
3443 unsigned long *map_len, int km)
3444{
3445 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3446 char *kaddr;
3447 struct page *p;
3448 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3449 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3450 unsigned long end_i = (start_offset + start + min_len - 1) >>
3451 PAGE_CACHE_SHIFT;
3452
3453 if (i != end_i)
3454 return -EINVAL;
3455
3456 if (i == 0) {
3457 offset = start_offset;
3458 *map_start = 0;
3459 } else {
3460 offset = 0;
3461 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3462 }
Chris Masond3977122009-01-05 21:25:51 -05003463
Chris Masond1310b22008-01-24 16:13:08 -05003464 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003465 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3466 "wanted %lu %lu\n", (unsigned long long)eb->start,
3467 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003468 WARN_ON(1);
3469 }
3470
3471 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003472 kaddr = kmap_atomic(p, km);
3473 *token = kaddr;
3474 *map = kaddr + offset;
3475 *map_len = PAGE_CACHE_SIZE - offset;
3476 return 0;
3477}
Chris Masond1310b22008-01-24 16:13:08 -05003478
3479int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3480 unsigned long min_len,
3481 char **token, char **map,
3482 unsigned long *map_start,
3483 unsigned long *map_len, int km)
3484{
3485 int err;
3486 int save = 0;
3487 if (eb->map_token) {
3488 unmap_extent_buffer(eb, eb->map_token, km);
3489 eb->map_token = NULL;
3490 save = 1;
3491 }
3492 err = map_private_extent_buffer(eb, start, min_len, token, map,
3493 map_start, map_len, km);
3494 if (!err && save) {
3495 eb->map_token = *token;
3496 eb->kaddr = *map;
3497 eb->map_start = *map_start;
3498 eb->map_len = *map_len;
3499 }
3500 return err;
3501}
Chris Masond1310b22008-01-24 16:13:08 -05003502
3503void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3504{
3505 kunmap_atomic(token, km);
3506}
Chris Masond1310b22008-01-24 16:13:08 -05003507
3508int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3509 unsigned long start,
3510 unsigned long len)
3511{
3512 size_t cur;
3513 size_t offset;
3514 struct page *page;
3515 char *kaddr;
3516 char *ptr = (char *)ptrv;
3517 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3518 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3519 int ret = 0;
3520
3521 WARN_ON(start > eb->len);
3522 WARN_ON(start + len > eb->start + eb->len);
3523
3524 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3525
Chris Masond3977122009-01-05 21:25:51 -05003526 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003527 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003528
3529 cur = min(len, (PAGE_CACHE_SIZE - offset));
3530
3531 kaddr = kmap_atomic(page, KM_USER0);
3532 ret = memcmp(ptr, kaddr + offset, cur);
3533 kunmap_atomic(kaddr, KM_USER0);
3534 if (ret)
3535 break;
3536
3537 ptr += cur;
3538 len -= cur;
3539 offset = 0;
3540 i++;
3541 }
3542 return ret;
3543}
Chris Masond1310b22008-01-24 16:13:08 -05003544
3545void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3546 unsigned long start, unsigned long len)
3547{
3548 size_t cur;
3549 size_t offset;
3550 struct page *page;
3551 char *kaddr;
3552 char *src = (char *)srcv;
3553 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3554 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3555
3556 WARN_ON(start > eb->len);
3557 WARN_ON(start + len > eb->start + eb->len);
3558
3559 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3560
Chris Masond3977122009-01-05 21:25:51 -05003561 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003562 page = extent_buffer_page(eb, i);
3563 WARN_ON(!PageUptodate(page));
3564
3565 cur = min(len, PAGE_CACHE_SIZE - offset);
3566 kaddr = kmap_atomic(page, KM_USER1);
3567 memcpy(kaddr + offset, src, cur);
3568 kunmap_atomic(kaddr, KM_USER1);
3569
3570 src += cur;
3571 len -= cur;
3572 offset = 0;
3573 i++;
3574 }
3575}
Chris Masond1310b22008-01-24 16:13:08 -05003576
3577void memset_extent_buffer(struct extent_buffer *eb, char c,
3578 unsigned long start, unsigned long len)
3579{
3580 size_t cur;
3581 size_t offset;
3582 struct page *page;
3583 char *kaddr;
3584 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3585 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3586
3587 WARN_ON(start > eb->len);
3588 WARN_ON(start + len > eb->start + eb->len);
3589
3590 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3591
Chris Masond3977122009-01-05 21:25:51 -05003592 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003593 page = extent_buffer_page(eb, i);
3594 WARN_ON(!PageUptodate(page));
3595
3596 cur = min(len, PAGE_CACHE_SIZE - offset);
3597 kaddr = kmap_atomic(page, KM_USER0);
3598 memset(kaddr + offset, c, cur);
3599 kunmap_atomic(kaddr, KM_USER0);
3600
3601 len -= cur;
3602 offset = 0;
3603 i++;
3604 }
3605}
Chris Masond1310b22008-01-24 16:13:08 -05003606
3607void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3608 unsigned long dst_offset, unsigned long src_offset,
3609 unsigned long len)
3610{
3611 u64 dst_len = dst->len;
3612 size_t cur;
3613 size_t offset;
3614 struct page *page;
3615 char *kaddr;
3616 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3617 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3618
3619 WARN_ON(src->len != dst_len);
3620
3621 offset = (start_offset + dst_offset) &
3622 ((unsigned long)PAGE_CACHE_SIZE - 1);
3623
Chris Masond3977122009-01-05 21:25:51 -05003624 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003625 page = extent_buffer_page(dst, i);
3626 WARN_ON(!PageUptodate(page));
3627
3628 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3629
3630 kaddr = kmap_atomic(page, KM_USER0);
3631 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3632 kunmap_atomic(kaddr, KM_USER0);
3633
3634 src_offset += cur;
3635 len -= cur;
3636 offset = 0;
3637 i++;
3638 }
3639}
Chris Masond1310b22008-01-24 16:13:08 -05003640
3641static void move_pages(struct page *dst_page, struct page *src_page,
3642 unsigned long dst_off, unsigned long src_off,
3643 unsigned long len)
3644{
3645 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3646 if (dst_page == src_page) {
3647 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3648 } else {
3649 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3650 char *p = dst_kaddr + dst_off + len;
3651 char *s = src_kaddr + src_off + len;
3652
3653 while (len--)
3654 *--p = *--s;
3655
3656 kunmap_atomic(src_kaddr, KM_USER1);
3657 }
3658 kunmap_atomic(dst_kaddr, KM_USER0);
3659}
3660
3661static void copy_pages(struct page *dst_page, struct page *src_page,
3662 unsigned long dst_off, unsigned long src_off,
3663 unsigned long len)
3664{
3665 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3666 char *src_kaddr;
3667
3668 if (dst_page != src_page)
3669 src_kaddr = kmap_atomic(src_page, KM_USER1);
3670 else
3671 src_kaddr = dst_kaddr;
3672
3673 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3674 kunmap_atomic(dst_kaddr, KM_USER0);
3675 if (dst_page != src_page)
3676 kunmap_atomic(src_kaddr, KM_USER1);
3677}
3678
3679void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3680 unsigned long src_offset, unsigned long len)
3681{
3682 size_t cur;
3683 size_t dst_off_in_page;
3684 size_t src_off_in_page;
3685 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3686 unsigned long dst_i;
3687 unsigned long src_i;
3688
3689 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003690 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3691 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003692 BUG_ON(1);
3693 }
3694 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003695 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3696 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003697 BUG_ON(1);
3698 }
3699
Chris Masond3977122009-01-05 21:25:51 -05003700 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003701 dst_off_in_page = (start_offset + dst_offset) &
3702 ((unsigned long)PAGE_CACHE_SIZE - 1);
3703 src_off_in_page = (start_offset + src_offset) &
3704 ((unsigned long)PAGE_CACHE_SIZE - 1);
3705
3706 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3707 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3708
3709 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3710 src_off_in_page));
3711 cur = min_t(unsigned long, cur,
3712 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3713
3714 copy_pages(extent_buffer_page(dst, dst_i),
3715 extent_buffer_page(dst, src_i),
3716 dst_off_in_page, src_off_in_page, cur);
3717
3718 src_offset += cur;
3719 dst_offset += cur;
3720 len -= cur;
3721 }
3722}
Chris Masond1310b22008-01-24 16:13:08 -05003723
3724void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3725 unsigned long src_offset, unsigned long len)
3726{
3727 size_t cur;
3728 size_t dst_off_in_page;
3729 size_t src_off_in_page;
3730 unsigned long dst_end = dst_offset + len - 1;
3731 unsigned long src_end = src_offset + len - 1;
3732 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3733 unsigned long dst_i;
3734 unsigned long src_i;
3735
3736 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003737 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3738 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003739 BUG_ON(1);
3740 }
3741 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003742 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3743 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003744 BUG_ON(1);
3745 }
3746 if (dst_offset < src_offset) {
3747 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3748 return;
3749 }
Chris Masond3977122009-01-05 21:25:51 -05003750 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003751 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3752 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3753
3754 dst_off_in_page = (start_offset + dst_end) &
3755 ((unsigned long)PAGE_CACHE_SIZE - 1);
3756 src_off_in_page = (start_offset + src_end) &
3757 ((unsigned long)PAGE_CACHE_SIZE - 1);
3758
3759 cur = min_t(unsigned long, len, src_off_in_page + 1);
3760 cur = min(cur, dst_off_in_page + 1);
3761 move_pages(extent_buffer_page(dst, dst_i),
3762 extent_buffer_page(dst, src_i),
3763 dst_off_in_page - cur + 1,
3764 src_off_in_page - cur + 1, cur);
3765
3766 dst_end -= cur;
3767 src_end -= cur;
3768 len -= cur;
3769 }
3770}
Chris Mason6af118ce2008-07-22 11:18:07 -04003771
3772int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3773{
3774 u64 start = page_offset(page);
3775 struct extent_buffer *eb;
3776 int ret = 1;
3777 unsigned long i;
3778 unsigned long num_pages;
3779
3780 spin_lock(&tree->buffer_lock);
3781 eb = buffer_search(tree, start);
3782 if (!eb)
3783 goto out;
3784
3785 if (atomic_read(&eb->refs) > 1) {
3786 ret = 0;
3787 goto out;
3788 }
Chris Masonb9473432009-03-13 11:00:37 -04003789 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3790 ret = 0;
3791 goto out;
3792 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003793 /* at this point we can safely release the extent buffer */
3794 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003795 for (i = 0; i < num_pages; i++)
3796 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003797 rb_erase(&eb->rb_node, &tree->buffer);
3798 __free_extent_buffer(eb);
3799out:
3800 spin_unlock(&tree->buffer_lock);
3801 return ret;
3802}