blob: d2d03684fab261fb9663f1d18ef19d9f23d98b78 [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>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
13#include "extent_io.h"
14#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040015#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050018
Chris Masond1310b22008-01-24 16:13:08 -050019static struct kmem_cache *extent_state_cache;
20static struct kmem_cache *extent_buffer_cache;
21
22static LIST_HEAD(buffers);
23static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040024
Chris Masonb47eda82008-11-10 12:34:40 -050025#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050026#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050027static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040028#endif
Chris Masond1310b22008-01-24 16:13:08 -050029
Chris Masond1310b22008-01-24 16:13:08 -050030#define BUFFER_LRU_MAX 64
31
32struct tree_entry {
33 u64 start;
34 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050035 struct rb_node rb_node;
36};
37
38struct extent_page_data {
39 struct bio *bio;
40 struct extent_io_tree *tree;
41 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050042
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
45 */
Chris Masonffbd5172009-04-20 15:50:09 -040046 unsigned int extent_locked:1;
47
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050050};
51
52int __init extent_io_init(void)
53{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020054 extent_state_cache = kmem_cache_create("extent_state",
55 sizeof(struct extent_state), 0,
56 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050057 if (!extent_state_cache)
58 return -ENOMEM;
59
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020060 extent_buffer_cache = kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer), 0,
62 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050063 if (!extent_buffer_cache)
64 goto free_state_cache;
65 return 0;
66
67free_state_cache:
68 kmem_cache_destroy(extent_state_cache);
69 return -ENOMEM;
70}
71
72void extent_io_exit(void)
73{
74 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040075 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050076
77 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040078 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050079 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state->start,
82 (unsigned long long)state->end,
83 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040084 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050085 kmem_cache_free(extent_state_cache, state);
86
87 }
88
Chris Mason2d2ae542008-03-26 16:24:23 -040089 while (!list_empty(&buffers)) {
90 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050091 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb->start,
93 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040094 list_del(&eb->leak_list);
95 kmem_cache_free(extent_buffer_cache, eb);
96 }
Chris Masond1310b22008-01-24 16:13:08 -050097 if (extent_state_cache)
98 kmem_cache_destroy(extent_state_cache);
99 if (extent_buffer_cache)
100 kmem_cache_destroy(extent_buffer_cache);
101}
102
103void extent_io_tree_init(struct extent_io_tree *tree,
104 struct address_space *mapping, gfp_t mask)
105{
Eric Paris6bef4d32010-02-23 19:43:04 +0000106 tree->state = RB_ROOT;
107 tree->buffer = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500108 tree->ops = NULL;
109 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500110 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400111 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500113}
Chris Masond1310b22008-01-24 16:13:08 -0500114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
117 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500118#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
Chris Masond1310b22008-01-24 16:13:08 -0500137
Christoph Hellwigb2950862008-12-02 09:54:17 -0500138static void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500143#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400144 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Mason70dec802008-01-29 09:59:12 -0500146 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 kmem_cache_free(extent_state_cache, state);
153 }
154}
Chris Masond1310b22008-01-24 16:13:08 -0500155
156static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
158{
Chris Masond3977122009-01-05 21:25:51 -0500159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500161 struct tree_entry *entry;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500164 parent = *p;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
166
167 if (offset < entry->start)
168 p = &(*p)->rb_left;
169 else if (offset > entry->end)
170 p = &(*p)->rb_right;
171 else
172 return parent;
173 }
174
175 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
178 return NULL;
179}
180
Chris Mason80ea96b2008-02-01 14:51:59 -0500181static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
184{
Chris Mason80ea96b2008-02-01 14:51:59 -0500185 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500186 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
191
Chris Masond3977122009-01-05 21:25:51 -0500192 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500193 entry = rb_entry(n, struct tree_entry, rb_node);
194 prev = n;
195 prev_entry = entry;
196
197 if (offset < entry->start)
198 n = n->rb_left;
199 else if (offset > entry->end)
200 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500201 else
Chris Masond1310b22008-01-24 16:13:08 -0500202 return n;
203 }
204
205 if (prev_ret) {
206 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500207 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
210 }
211 *prev_ret = prev;
212 prev = orig_prev;
213 }
214
215 if (next_ret) {
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *next_ret = prev;
222 }
223 return NULL;
224}
225
Chris Mason80ea96b2008-02-01 14:51:59 -0500226static inline struct rb_node *tree_search(struct extent_io_tree *tree,
227 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500228{
Chris Mason70dec802008-01-29 09:59:12 -0500229 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500230 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500231
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500233 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500234 return prev;
235 return ret;
236}
237
Chris Mason6af118ce2008-07-22 11:18:07 -0400238static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
239 u64 offset, struct rb_node *node)
240{
241 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500242 struct rb_node **p = &root->rb_node;
243 struct rb_node *parent = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400244 struct extent_buffer *eb;
245
Chris Masond3977122009-01-05 21:25:51 -0500246 while (*p) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400247 parent = *p;
248 eb = rb_entry(parent, struct extent_buffer, rb_node);
249
250 if (offset < eb->start)
251 p = &(*p)->rb_left;
252 else if (offset > eb->start)
253 p = &(*p)->rb_right;
254 else
255 return eb;
256 }
257
258 rb_link_node(node, parent, p);
259 rb_insert_color(node, root);
260 return NULL;
261}
262
263static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
264 u64 offset)
265{
266 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500267 struct rb_node *n = root->rb_node;
Chris Mason6af118ce2008-07-22 11:18:07 -0400268 struct extent_buffer *eb;
269
Chris Masond3977122009-01-05 21:25:51 -0500270 while (n) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400271 eb = rb_entry(n, struct extent_buffer, rb_node);
272 if (offset < eb->start)
273 n = n->rb_left;
274 else if (offset > eb->start)
275 n = n->rb_right;
276 else
277 return eb;
278 }
279 return NULL;
280}
281
Josef Bacik9ed74f22009-09-11 16:12:44 -0400282static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
283 struct extent_state *other)
284{
285 if (tree->ops && tree->ops->merge_extent_hook)
286 tree->ops->merge_extent_hook(tree->mapping->host, new,
287 other);
288}
289
Chris Masond1310b22008-01-24 16:13:08 -0500290/*
291 * utility function to look for merge candidates inside a given range.
292 * Any extents with matching state are merged together into a single
293 * extent in the tree. Extents with EXTENT_IO in their state field
294 * are not merged because the end_io handlers need to be able to do
295 * operations on them without sleeping (or doing allocations/splits).
296 *
297 * This should be called with the tree lock held.
298 */
299static int merge_state(struct extent_io_tree *tree,
300 struct extent_state *state)
301{
302 struct extent_state *other;
303 struct rb_node *other_node;
304
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400305 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500306 return 0;
307
308 other_node = rb_prev(&state->rb_node);
309 if (other_node) {
310 other = rb_entry(other_node, struct extent_state, rb_node);
311 if (other->end == state->start - 1 &&
312 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400313 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500314 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500315 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500316 rb_erase(&other->rb_node, &tree->state);
317 free_extent_state(other);
318 }
319 }
320 other_node = rb_next(&state->rb_node);
321 if (other_node) {
322 other = rb_entry(other_node, struct extent_state, rb_node);
323 if (other->start == state->end + 1 &&
324 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400325 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500326 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500327 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500328 rb_erase(&state->rb_node, &tree->state);
329 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400330 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500331 }
332 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400333
Chris Masond1310b22008-01-24 16:13:08 -0500334 return 0;
335}
336
Josef Bacik9ed74f22009-09-11 16:12:44 -0400337static int set_state_cb(struct extent_io_tree *tree,
Chris Mason291d6732008-01-29 15:55:23 -0500338 struct extent_state *state,
339 unsigned long bits)
340{
341 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400342 return tree->ops->set_bit_hook(tree->mapping->host,
343 state->start, state->end,
344 state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500345 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400346
347 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500348}
349
350static void clear_state_cb(struct extent_io_tree *tree,
351 struct extent_state *state,
352 unsigned long bits)
353{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400354 if (tree->ops && tree->ops->clear_bit_hook)
355 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500356}
357
Chris Masond1310b22008-01-24 16:13:08 -0500358/*
359 * insert an extent_state struct into the tree. 'bits' are set on the
360 * struct before it is inserted.
361 *
362 * This may return -EEXIST if the extent is already there, in which case the
363 * state struct is freed.
364 *
365 * The tree lock is not taken internally. This is a utility function and
366 * probably isn't what you want to call (see set/clear_extent_bit).
367 */
368static int insert_state(struct extent_io_tree *tree,
369 struct extent_state *state, u64 start, u64 end,
370 int bits)
371{
372 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400373 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500374
375 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500376 printk(KERN_ERR "btrfs end < start %llu %llu\n",
377 (unsigned long long)end,
378 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500379 WARN_ON(1);
380 }
Chris Masond1310b22008-01-24 16:13:08 -0500381 state->start = start;
382 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400383 ret = set_state_cb(tree, state, bits);
384 if (ret)
385 return ret;
386
387 if (bits & EXTENT_DIRTY)
388 tree->dirty_bytes += end - start + 1;
Chris Masone48c4652009-09-11 11:25:02 -0400389 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500390 node = tree_insert(&tree->state, end, &state->rb_node);
391 if (node) {
392 struct extent_state *found;
393 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500394 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
395 "%llu %llu\n", (unsigned long long)found->start,
396 (unsigned long long)found->end,
397 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500398 free_extent_state(state);
399 return -EEXIST;
400 }
Chris Mason70dec802008-01-29 09:59:12 -0500401 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500402 merge_state(tree, state);
403 return 0;
404}
405
Josef Bacik9ed74f22009-09-11 16:12:44 -0400406static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
407 u64 split)
408{
409 if (tree->ops && tree->ops->split_extent_hook)
410 return tree->ops->split_extent_hook(tree->mapping->host,
411 orig, split);
412 return 0;
413}
414
Chris Masond1310b22008-01-24 16:13:08 -0500415/*
416 * split a given extent state struct in two, inserting the preallocated
417 * struct 'prealloc' as the newly created second half. 'split' indicates an
418 * offset inside 'orig' where it should be split.
419 *
420 * Before calling,
421 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
422 * are two extent state structs in the tree:
423 * prealloc: [orig->start, split - 1]
424 * orig: [ split, orig->end ]
425 *
426 * The tree locks are not taken by this function. They need to be held
427 * by the caller.
428 */
429static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
430 struct extent_state *prealloc, u64 split)
431{
432 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400433
434 split_cb(tree, orig, split);
435
Chris Masond1310b22008-01-24 16:13:08 -0500436 prealloc->start = orig->start;
437 prealloc->end = split - 1;
438 prealloc->state = orig->state;
439 orig->start = split;
440
441 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
442 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500443 free_extent_state(prealloc);
444 return -EEXIST;
445 }
Chris Mason70dec802008-01-29 09:59:12 -0500446 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500447 return 0;
448}
449
450/*
451 * utility function to clear some bits in an extent state struct.
452 * it will optionally wake up any one waiting on this state (wake == 1), or
453 * forcibly remove the state from the tree (delete == 1).
454 *
455 * If no bits are set on the state struct after clearing things, the
456 * struct is freed and removed from the tree
457 */
458static int clear_state_bit(struct extent_io_tree *tree,
459 struct extent_state *state, int bits, int wake,
460 int delete)
461{
Josef Bacik32c00af2009-10-08 13:34:05 -0400462 int bits_to_clear = bits & ~EXTENT_DO_ACCOUNTING;
463 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500464
465 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
466 u64 range = state->end - state->start + 1;
467 WARN_ON(range > tree->dirty_bytes);
468 tree->dirty_bytes -= range;
469 }
Chris Mason291d6732008-01-29 15:55:23 -0500470 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400471 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500472 if (wake)
473 wake_up(&state->wq);
474 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500475 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500476 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500477 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500478 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500479 free_extent_state(state);
480 } else {
481 WARN_ON(1);
482 }
483 } else {
484 merge_state(tree, state);
485 }
486 return ret;
487}
488
489/*
490 * clear some bits on a range in the tree. This may require splitting
491 * or inserting elements in the tree, so the gfp mask is used to
492 * indicate which allocations or sleeping are allowed.
493 *
494 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
495 * the given range from the tree regardless of state (ie for truncate).
496 *
497 * the range [start, end] is inclusive.
498 *
499 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
500 * bits were already set, or zero if none of the bits were already set.
501 */
502int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400503 int bits, int wake, int delete,
504 struct extent_state **cached_state,
505 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500506{
507 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400508 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500509 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400510 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500511 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400512 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500513 int err;
514 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000515 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500516
Josef Bacik2ac55d42010-02-03 19:33:23 +0000517 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
518 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500519again:
520 if (!prealloc && (mask & __GFP_WAIT)) {
521 prealloc = alloc_extent_state(mask);
522 if (!prealloc)
523 return -ENOMEM;
524 }
525
Chris Masoncad321a2008-12-17 14:51:42 -0500526 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400527 if (cached_state) {
528 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000529
530 if (clear) {
531 *cached_state = NULL;
532 cached_state = NULL;
533 }
534
Chris Mason42daec22009-09-23 19:51:09 -0400535 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000536 if (clear)
537 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400538 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400539 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400540 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000541 if (clear)
542 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400543 }
Chris Masond1310b22008-01-24 16:13:08 -0500544 /*
545 * this search will find the extents that end after
546 * our range starts
547 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500548 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500549 if (!node)
550 goto out;
551 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400552hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500553 if (state->start > end)
554 goto out;
555 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400556 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500557
558 /*
559 * | ---- desired range ---- |
560 * | state | or
561 * | ------------- state -------------- |
562 *
563 * We need to split the extent we found, and may flip
564 * bits on second half.
565 *
566 * If the extent we found extends past our range, we
567 * just split and search again. It'll get split again
568 * the next time though.
569 *
570 * If the extent we found is inside our range, we clear
571 * the desired bit on it.
572 */
573
574 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500575 if (!prealloc)
576 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500577 err = split_state(tree, state, prealloc, start);
578 BUG_ON(err == -EEXIST);
579 prealloc = NULL;
580 if (err)
581 goto out;
582 if (state->end <= end) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400583 set |= clear_state_bit(tree, state, bits, wake,
584 delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400585 if (last_end == (u64)-1)
586 goto out;
587 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500588 }
589 goto search_again;
590 }
591 /*
592 * | ---- desired range ---- |
593 * | state |
594 * We need to split the extent, and clear the bit
595 * on the first half
596 */
597 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500598 if (!prealloc)
599 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500600 err = split_state(tree, state, prealloc, end + 1);
601 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500602 if (wake)
603 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400604
Josef Bacik9ed74f22009-09-11 16:12:44 -0400605 set |= clear_state_bit(tree, prealloc, bits, wake, delete);
606
Chris Masond1310b22008-01-24 16:13:08 -0500607 prealloc = NULL;
608 goto out;
609 }
Chris Mason42daec22009-09-23 19:51:09 -0400610
Chris Mason2c64c532009-09-02 15:04:12 -0400611 if (state->end < end && prealloc && !need_resched())
612 next_node = rb_next(&state->rb_node);
613 else
614 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400615
Chris Masond1310b22008-01-24 16:13:08 -0500616 set |= clear_state_bit(tree, state, bits, wake, delete);
Yan Zheng5c939df2009-05-27 09:16:03 -0400617 if (last_end == (u64)-1)
618 goto out;
619 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400620 if (start <= end && next_node) {
621 state = rb_entry(next_node, struct extent_state,
622 rb_node);
623 if (state->start == start)
624 goto hit_next;
625 }
Chris Masond1310b22008-01-24 16:13:08 -0500626 goto search_again;
627
628out:
Chris Masoncad321a2008-12-17 14:51:42 -0500629 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630 if (prealloc)
631 free_extent_state(prealloc);
632
633 return set;
634
635search_again:
636 if (start > end)
637 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500638 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500639 if (mask & __GFP_WAIT)
640 cond_resched();
641 goto again;
642}
Chris Masond1310b22008-01-24 16:13:08 -0500643
644static int wait_on_state(struct extent_io_tree *tree,
645 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500646 __releases(tree->lock)
647 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500648{
649 DEFINE_WAIT(wait);
650 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500651 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500652 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500653 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654 finish_wait(&state->wq, &wait);
655 return 0;
656}
657
658/*
659 * waits for one or more bits to clear on a range in the state tree.
660 * The range [start, end] is inclusive.
661 * The tree lock is taken by this function
662 */
663int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
664{
665 struct extent_state *state;
666 struct rb_node *node;
667
Chris Masoncad321a2008-12-17 14:51:42 -0500668 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500669again:
670 while (1) {
671 /*
672 * this search will find all the extents that end after
673 * our range starts
674 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500675 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500676 if (!node)
677 break;
678
679 state = rb_entry(node, struct extent_state, rb_node);
680
681 if (state->start > end)
682 goto out;
683
684 if (state->state & bits) {
685 start = state->start;
686 atomic_inc(&state->refs);
687 wait_on_state(tree, state);
688 free_extent_state(state);
689 goto again;
690 }
691 start = state->end + 1;
692
693 if (start > end)
694 break;
695
696 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500697 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500698 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500699 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500700 }
701 }
702out:
Chris Masoncad321a2008-12-17 14:51:42 -0500703 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500704 return 0;
705}
Chris Masond1310b22008-01-24 16:13:08 -0500706
Josef Bacik9ed74f22009-09-11 16:12:44 -0400707static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500708 struct extent_state *state,
709 int bits)
710{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400711 int ret;
712
713 ret = set_state_cb(tree, state, bits);
714 if (ret)
715 return ret;
716
Chris Masond1310b22008-01-24 16:13:08 -0500717 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
718 u64 range = state->end - state->start + 1;
719 tree->dirty_bytes += range;
720 }
Chris Masonb0c68f82008-01-31 11:05:37 -0500721 state->state |= bits;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400722
723 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500724}
725
Chris Mason2c64c532009-09-02 15:04:12 -0400726static void cache_state(struct extent_state *state,
727 struct extent_state **cached_ptr)
728{
729 if (cached_ptr && !(*cached_ptr)) {
730 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
731 *cached_ptr = state;
732 atomic_inc(&state->refs);
733 }
734 }
735}
736
Chris Masond1310b22008-01-24 16:13:08 -0500737/*
Chris Mason1edbb732009-09-02 13:24:36 -0400738 * set some bits on a range in the tree. This may require allocations or
739 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500740 *
Chris Mason1edbb732009-09-02 13:24:36 -0400741 * If any of the exclusive bits are set, this will fail with -EEXIST if some
742 * part of the range already has the desired bits set. The start of the
743 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500744 *
Chris Mason1edbb732009-09-02 13:24:36 -0400745 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500746 */
Chris Mason1edbb732009-09-02 13:24:36 -0400747
Chris Masond3977122009-01-05 21:25:51 -0500748static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason1edbb732009-09-02 13:24:36 -0400749 int bits, int exclusive_bits, u64 *failed_start,
Chris Mason2c64c532009-09-02 15:04:12 -0400750 struct extent_state **cached_state,
Chris Masond3977122009-01-05 21:25:51 -0500751 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500752{
753 struct extent_state *state;
754 struct extent_state *prealloc = NULL;
755 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500756 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500757 u64 last_start;
758 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400759
Chris Masond1310b22008-01-24 16:13:08 -0500760again:
761 if (!prealloc && (mask & __GFP_WAIT)) {
762 prealloc = alloc_extent_state(mask);
763 if (!prealloc)
764 return -ENOMEM;
765 }
766
Chris Masoncad321a2008-12-17 14:51:42 -0500767 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400768 if (cached_state && *cached_state) {
769 state = *cached_state;
770 if (state->start == start && state->tree) {
771 node = &state->rb_node;
772 goto hit_next;
773 }
774 }
Chris Masond1310b22008-01-24 16:13:08 -0500775 /*
776 * this search will find all the extents that end after
777 * our range starts.
778 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500779 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500780 if (!node) {
781 err = insert_state(tree, prealloc, start, end, bits);
782 prealloc = NULL;
783 BUG_ON(err == -EEXIST);
784 goto out;
785 }
Chris Masond1310b22008-01-24 16:13:08 -0500786 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400787hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500788 last_start = state->start;
789 last_end = state->end;
790
791 /*
792 * | ---- desired range ---- |
793 * | state |
794 *
795 * Just lock what we found and keep going
796 */
797 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400798 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400799 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500800 *failed_start = state->start;
801 err = -EEXIST;
802 goto out;
803 }
Chris Mason42daec22009-09-23 19:51:09 -0400804
Josef Bacik9ed74f22009-09-11 16:12:44 -0400805 err = set_state_bits(tree, state, bits);
806 if (err)
807 goto out;
808
Chris Mason2c64c532009-09-02 15:04:12 -0400809 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500810 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400811 if (last_end == (u64)-1)
812 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400813
Yan Zheng5c939df2009-05-27 09:16:03 -0400814 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400815 if (start < end && prealloc && !need_resched()) {
816 next_node = rb_next(node);
817 if (next_node) {
818 state = rb_entry(next_node, struct extent_state,
819 rb_node);
820 if (state->start == start)
821 goto hit_next;
822 }
823 }
Chris Masond1310b22008-01-24 16:13:08 -0500824 goto search_again;
825 }
826
827 /*
828 * | ---- desired range ---- |
829 * | state |
830 * or
831 * | ------------- state -------------- |
832 *
833 * We need to split the extent we found, and may flip bits on
834 * second half.
835 *
836 * If the extent we found extends past our
837 * range, we just split and search again. It'll get split
838 * again the next time though.
839 *
840 * If the extent we found is inside our range, we set the
841 * desired bit on it.
842 */
843 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400844 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500845 *failed_start = start;
846 err = -EEXIST;
847 goto out;
848 }
849 err = split_state(tree, state, prealloc, start);
850 BUG_ON(err == -EEXIST);
851 prealloc = NULL;
852 if (err)
853 goto out;
854 if (state->end <= end) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400855 err = set_state_bits(tree, state, bits);
856 if (err)
857 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400858 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500859 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400860 if (last_end == (u64)-1)
861 goto out;
862 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500863 }
864 goto search_again;
865 }
866 /*
867 * | ---- desired range ---- |
868 * | state | or | state |
869 *
870 * There's a hole, we need to insert something in it and
871 * ignore the extent we found.
872 */
873 if (state->start > start) {
874 u64 this_end;
875 if (end < last_start)
876 this_end = end;
877 else
Chris Masond3977122009-01-05 21:25:51 -0500878 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500879 err = insert_state(tree, prealloc, start, this_end,
880 bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400881 BUG_ON(err == -EEXIST);
882 if (err) {
883 prealloc = NULL;
884 goto out;
885 }
Chris Mason2c64c532009-09-02 15:04:12 -0400886 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500887 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500888 start = this_end + 1;
889 goto search_again;
890 }
891 /*
892 * | ---- desired range ---- |
893 * | state |
894 * We need to split the extent, and set the bit
895 * on the first half
896 */
897 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400898 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500899 *failed_start = start;
900 err = -EEXIST;
901 goto out;
902 }
903 err = split_state(tree, state, prealloc, end + 1);
904 BUG_ON(err == -EEXIST);
905
Josef Bacik9ed74f22009-09-11 16:12:44 -0400906 err = set_state_bits(tree, prealloc, bits);
907 if (err) {
908 prealloc = NULL;
909 goto out;
910 }
Chris Mason2c64c532009-09-02 15:04:12 -0400911 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500912 merge_state(tree, prealloc);
913 prealloc = NULL;
914 goto out;
915 }
916
917 goto search_again;
918
919out:
Chris Masoncad321a2008-12-17 14:51:42 -0500920 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500921 if (prealloc)
922 free_extent_state(prealloc);
923
924 return err;
925
926search_again:
927 if (start > end)
928 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500929 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500930 if (mask & __GFP_WAIT)
931 cond_resched();
932 goto again;
933}
Chris Masond1310b22008-01-24 16:13:08 -0500934
935/* wrappers around set/clear extent bit */
936int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
937 gfp_t mask)
938{
939 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400940 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500941}
Chris Masond1310b22008-01-24 16:13:08 -0500942
943int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
944 int bits, gfp_t mask)
945{
946 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400947 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500948}
Chris Masond1310b22008-01-24 16:13:08 -0500949
950int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
951 int bits, gfp_t mask)
952{
Chris Mason2c64c532009-09-02 15:04:12 -0400953 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500954}
Chris Masond1310b22008-01-24 16:13:08 -0500955
956int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000957 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500958{
959 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400960 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000961 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500962}
Chris Masond1310b22008-01-24 16:13:08 -0500963
964int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
965 gfp_t mask)
966{
967 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400968 EXTENT_DIRTY | EXTENT_DELALLOC |
969 EXTENT_DO_ACCOUNTING, 0, 0,
Chris Mason2c64c532009-09-02 15:04:12 -0400970 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500971}
Chris Masond1310b22008-01-24 16:13:08 -0500972
973int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
974 gfp_t mask)
975{
976 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400977 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500978}
Chris Masond1310b22008-01-24 16:13:08 -0500979
Christoph Hellwigb2950862008-12-02 09:54:17 -0500980static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500981 gfp_t mask)
982{
Chris Mason2c64c532009-09-02 15:04:12 -0400983 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
984 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500985}
Chris Masond1310b22008-01-24 16:13:08 -0500986
987int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
988 gfp_t mask)
989{
990 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400991 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500992}
Chris Masond1310b22008-01-24 16:13:08 -0500993
Chris Masond3977122009-01-05 21:25:51 -0500994static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000995 u64 end, struct extent_state **cached_state,
996 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500997{
Chris Mason2c64c532009-09-02 15:04:12 -0400998 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000999 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001000}
Chris Masond1310b22008-01-24 16:13:08 -05001001
Chris Masond1310b22008-01-24 16:13:08 -05001002int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1003{
1004 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
1005}
Chris Masond1310b22008-01-24 16:13:08 -05001006
Chris Masond352ac62008-09-29 15:18:18 -04001007/*
1008 * either insert or lock state struct between start and end use mask to tell
1009 * us if waiting is desired.
1010 */
Chris Mason1edbb732009-09-02 13:24:36 -04001011int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001012 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001013{
1014 int err;
1015 u64 failed_start;
1016 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001017 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001018 EXTENT_LOCKED, &failed_start,
1019 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001020 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1021 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1022 start = failed_start;
1023 } else {
1024 break;
1025 }
1026 WARN_ON(start > end);
1027 }
1028 return err;
1029}
Chris Masond1310b22008-01-24 16:13:08 -05001030
Chris Mason1edbb732009-09-02 13:24:36 -04001031int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1032{
Chris Mason2c64c532009-09-02 15:04:12 -04001033 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001034}
1035
Josef Bacik25179202008-10-29 14:49:05 -04001036int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1037 gfp_t mask)
1038{
1039 int err;
1040 u64 failed_start;
1041
Chris Mason2c64c532009-09-02 15:04:12 -04001042 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1043 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001044 if (err == -EEXIST) {
1045 if (failed_start > start)
1046 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001047 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001048 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001049 }
Josef Bacik25179202008-10-29 14:49:05 -04001050 return 1;
1051}
Josef Bacik25179202008-10-29 14:49:05 -04001052
Chris Mason2c64c532009-09-02 15:04:12 -04001053int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1054 struct extent_state **cached, gfp_t mask)
1055{
1056 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1057 mask);
1058}
1059
Chris Masond1310b22008-01-24 16:13:08 -05001060int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1061 gfp_t mask)
1062{
Chris Mason2c64c532009-09-02 15:04:12 -04001063 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1064 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001065}
Chris Masond1310b22008-01-24 16:13:08 -05001066
1067/*
1068 * helper function to set pages and extents in the tree dirty
1069 */
1070int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1071{
1072 unsigned long index = start >> PAGE_CACHE_SHIFT;
1073 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1074 struct page *page;
1075
1076 while (index <= end_index) {
1077 page = find_get_page(tree->mapping, index);
1078 BUG_ON(!page);
1079 __set_page_dirty_nobuffers(page);
1080 page_cache_release(page);
1081 index++;
1082 }
Chris Masond1310b22008-01-24 16:13:08 -05001083 return 0;
1084}
Chris Masond1310b22008-01-24 16:13:08 -05001085
1086/*
1087 * helper function to set both pages and extents in the tree writeback
1088 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001089static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001090{
1091 unsigned long index = start >> PAGE_CACHE_SHIFT;
1092 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1093 struct page *page;
1094
1095 while (index <= end_index) {
1096 page = find_get_page(tree->mapping, index);
1097 BUG_ON(!page);
1098 set_page_writeback(page);
1099 page_cache_release(page);
1100 index++;
1101 }
Chris Masond1310b22008-01-24 16:13:08 -05001102 return 0;
1103}
Chris Masond1310b22008-01-24 16:13:08 -05001104
Chris Masond352ac62008-09-29 15:18:18 -04001105/*
1106 * find the first offset in the io tree with 'bits' set. zero is
1107 * returned if we find something, and *start_ret and *end_ret are
1108 * set to reflect the state struct that was found.
1109 *
1110 * If nothing was found, 1 is returned, < 0 on error
1111 */
Chris Masond1310b22008-01-24 16:13:08 -05001112int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1113 u64 *start_ret, u64 *end_ret, int bits)
1114{
1115 struct rb_node *node;
1116 struct extent_state *state;
1117 int ret = 1;
1118
Chris Masoncad321a2008-12-17 14:51:42 -05001119 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001120 /*
1121 * this search will find all the extents that end after
1122 * our range starts.
1123 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001124 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001125 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001126 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001127
Chris Masond3977122009-01-05 21:25:51 -05001128 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001129 state = rb_entry(node, struct extent_state, rb_node);
1130 if (state->end >= start && (state->state & bits)) {
1131 *start_ret = state->start;
1132 *end_ret = state->end;
1133 ret = 0;
1134 break;
1135 }
1136 node = rb_next(node);
1137 if (!node)
1138 break;
1139 }
1140out:
Chris Masoncad321a2008-12-17 14:51:42 -05001141 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001142 return ret;
1143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
Chris Masond352ac62008-09-29 15:18:18 -04001145/* find the first state struct with 'bits' set after 'start', and
1146 * return it. tree->lock must be held. NULL will returned if
1147 * nothing was found after 'start'
1148 */
Chris Masond7fc6402008-02-18 12:12:38 -05001149struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1150 u64 start, int bits)
1151{
1152 struct rb_node *node;
1153 struct extent_state *state;
1154
1155 /*
1156 * this search will find all the extents that end after
1157 * our range starts.
1158 */
1159 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001160 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001161 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001162
Chris Masond3977122009-01-05 21:25:51 -05001163 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001164 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001165 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001166 return state;
Chris Masond3977122009-01-05 21:25:51 -05001167
Chris Masond7fc6402008-02-18 12:12:38 -05001168 node = rb_next(node);
1169 if (!node)
1170 break;
1171 }
1172out:
1173 return NULL;
1174}
Chris Masond7fc6402008-02-18 12:12:38 -05001175
Chris Masond352ac62008-09-29 15:18:18 -04001176/*
1177 * find a contiguous range of bytes in the file marked as delalloc, not
1178 * more than 'max_bytes'. start and end are used to return the range,
1179 *
1180 * 1 is returned if we find something, 0 if nothing was in the tree
1181 */
Chris Masonc8b97812008-10-29 14:49:59 -04001182static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001183 u64 *start, u64 *end, u64 max_bytes,
1184 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001185{
1186 struct rb_node *node;
1187 struct extent_state *state;
1188 u64 cur_start = *start;
1189 u64 found = 0;
1190 u64 total_bytes = 0;
1191
Chris Masoncad321a2008-12-17 14:51:42 -05001192 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001193
Chris Masond1310b22008-01-24 16:13:08 -05001194 /*
1195 * this search will find all the extents that end after
1196 * our range starts.
1197 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001198 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001199 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001200 if (!found)
1201 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001202 goto out;
1203 }
1204
Chris Masond3977122009-01-05 21:25:51 -05001205 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001206 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001207 if (found && (state->start != cur_start ||
1208 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001209 goto out;
1210 }
1211 if (!(state->state & EXTENT_DELALLOC)) {
1212 if (!found)
1213 *end = state->end;
1214 goto out;
1215 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001216 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001217 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001218 *cached_state = state;
1219 atomic_inc(&state->refs);
1220 }
Chris Masond1310b22008-01-24 16:13:08 -05001221 found++;
1222 *end = state->end;
1223 cur_start = state->end + 1;
1224 node = rb_next(node);
1225 if (!node)
1226 break;
1227 total_bytes += state->end - state->start + 1;
1228 if (total_bytes >= max_bytes)
1229 break;
1230 }
1231out:
Chris Masoncad321a2008-12-17 14:51:42 -05001232 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001233 return found;
1234}
1235
Chris Masonc8b97812008-10-29 14:49:59 -04001236static noinline int __unlock_for_delalloc(struct inode *inode,
1237 struct page *locked_page,
1238 u64 start, u64 end)
1239{
1240 int ret;
1241 struct page *pages[16];
1242 unsigned long index = start >> PAGE_CACHE_SHIFT;
1243 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1244 unsigned long nr_pages = end_index - index + 1;
1245 int i;
1246
1247 if (index == locked_page->index && end_index == index)
1248 return 0;
1249
Chris Masond3977122009-01-05 21:25:51 -05001250 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001251 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001252 min_t(unsigned long, nr_pages,
1253 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001254 for (i = 0; i < ret; i++) {
1255 if (pages[i] != locked_page)
1256 unlock_page(pages[i]);
1257 page_cache_release(pages[i]);
1258 }
1259 nr_pages -= ret;
1260 index += ret;
1261 cond_resched();
1262 }
1263 return 0;
1264}
1265
1266static noinline int lock_delalloc_pages(struct inode *inode,
1267 struct page *locked_page,
1268 u64 delalloc_start,
1269 u64 delalloc_end)
1270{
1271 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1272 unsigned long start_index = index;
1273 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1274 unsigned long pages_locked = 0;
1275 struct page *pages[16];
1276 unsigned long nrpages;
1277 int ret;
1278 int i;
1279
1280 /* the caller is responsible for locking the start index */
1281 if (index == locked_page->index && index == end_index)
1282 return 0;
1283
1284 /* skip the page at the start index */
1285 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001286 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001287 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001288 min_t(unsigned long,
1289 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001290 if (ret == 0) {
1291 ret = -EAGAIN;
1292 goto done;
1293 }
1294 /* now we have an array of pages, lock them all */
1295 for (i = 0; i < ret; i++) {
1296 /*
1297 * the caller is taking responsibility for
1298 * locked_page
1299 */
Chris Mason771ed682008-11-06 22:02:51 -05001300 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001301 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001302 if (!PageDirty(pages[i]) ||
1303 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001304 ret = -EAGAIN;
1305 unlock_page(pages[i]);
1306 page_cache_release(pages[i]);
1307 goto done;
1308 }
1309 }
Chris Masonc8b97812008-10-29 14:49:59 -04001310 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001311 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001312 }
Chris Masonc8b97812008-10-29 14:49:59 -04001313 nrpages -= ret;
1314 index += ret;
1315 cond_resched();
1316 }
1317 ret = 0;
1318done:
1319 if (ret && pages_locked) {
1320 __unlock_for_delalloc(inode, locked_page,
1321 delalloc_start,
1322 ((u64)(start_index + pages_locked - 1)) <<
1323 PAGE_CACHE_SHIFT);
1324 }
1325 return ret;
1326}
1327
1328/*
1329 * find a contiguous range of bytes in the file marked as delalloc, not
1330 * more than 'max_bytes'. start and end are used to return the range,
1331 *
1332 * 1 is returned if we find something, 0 if nothing was in the tree
1333 */
1334static noinline u64 find_lock_delalloc_range(struct inode *inode,
1335 struct extent_io_tree *tree,
1336 struct page *locked_page,
1337 u64 *start, u64 *end,
1338 u64 max_bytes)
1339{
1340 u64 delalloc_start;
1341 u64 delalloc_end;
1342 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001343 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001344 int ret;
1345 int loops = 0;
1346
1347again:
1348 /* step one, find a bunch of delalloc bytes starting at start */
1349 delalloc_start = *start;
1350 delalloc_end = 0;
1351 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001352 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001353 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001354 *start = delalloc_start;
1355 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001356 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001357 return found;
1358 }
1359
1360 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001361 * start comes from the offset of locked_page. We have to lock
1362 * pages in order, so we can't process delalloc bytes before
1363 * locked_page
1364 */
Chris Masond3977122009-01-05 21:25:51 -05001365 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001366 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001367
1368 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001369 * make sure to limit the number of pages we try to lock down
1370 * if we're looping.
1371 */
Chris Masond3977122009-01-05 21:25:51 -05001372 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001373 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001374
Chris Masonc8b97812008-10-29 14:49:59 -04001375 /* step two, lock all the pages after the page that has start */
1376 ret = lock_delalloc_pages(inode, locked_page,
1377 delalloc_start, delalloc_end);
1378 if (ret == -EAGAIN) {
1379 /* some of the pages are gone, lets avoid looping by
1380 * shortening the size of the delalloc range we're searching
1381 */
Chris Mason9655d292009-09-02 15:22:30 -04001382 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001383 if (!loops) {
1384 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1385 max_bytes = PAGE_CACHE_SIZE - offset;
1386 loops = 1;
1387 goto again;
1388 } else {
1389 found = 0;
1390 goto out_failed;
1391 }
1392 }
1393 BUG_ON(ret);
1394
1395 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001396 lock_extent_bits(tree, delalloc_start, delalloc_end,
1397 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001398
1399 /* then test to make sure it is all still delalloc */
1400 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001401 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001402 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001403 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1404 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001405 __unlock_for_delalloc(inode, locked_page,
1406 delalloc_start, delalloc_end);
1407 cond_resched();
1408 goto again;
1409 }
Chris Mason9655d292009-09-02 15:22:30 -04001410 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001411 *start = delalloc_start;
1412 *end = delalloc_end;
1413out_failed:
1414 return found;
1415}
1416
1417int extent_clear_unlock_delalloc(struct inode *inode,
1418 struct extent_io_tree *tree,
1419 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001420 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001421{
1422 int ret;
1423 struct page *pages[16];
1424 unsigned long index = start >> PAGE_CACHE_SHIFT;
1425 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1426 unsigned long nr_pages = end_index - index + 1;
1427 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001428 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001429
Chris Masona791e352009-10-08 11:27:10 -04001430 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001431 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001432 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001433 clear_bits |= EXTENT_DIRTY;
1434
Chris Masona791e352009-10-08 11:27:10 -04001435 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001436 clear_bits |= EXTENT_DELALLOC;
1437
Josef Bacik32c00af2009-10-08 13:34:05 -04001438 if (op & EXTENT_CLEAR_ACCOUNTING)
1439 clear_bits |= EXTENT_DO_ACCOUNTING;
1440
Chris Mason2c64c532009-09-02 15:04:12 -04001441 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001442 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1443 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1444 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001445 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001446
Chris Masond3977122009-01-05 21:25:51 -05001447 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001448 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001449 min_t(unsigned long,
1450 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001451 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001452
Chris Masona791e352009-10-08 11:27:10 -04001453 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001454 SetPagePrivate2(pages[i]);
1455
Chris Masonc8b97812008-10-29 14:49:59 -04001456 if (pages[i] == locked_page) {
1457 page_cache_release(pages[i]);
1458 continue;
1459 }
Chris Masona791e352009-10-08 11:27:10 -04001460 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001461 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001462 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001463 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001464 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001465 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001466 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001467 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001468 page_cache_release(pages[i]);
1469 }
1470 nr_pages -= ret;
1471 index += ret;
1472 cond_resched();
1473 }
1474 return 0;
1475}
Chris Masonc8b97812008-10-29 14:49:59 -04001476
Chris Masond352ac62008-09-29 15:18:18 -04001477/*
1478 * count the number of bytes in the tree that have a given bit(s)
1479 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1480 * cached. The total number found is returned.
1481 */
Chris Masond1310b22008-01-24 16:13:08 -05001482u64 count_range_bits(struct extent_io_tree *tree,
1483 u64 *start, u64 search_end, u64 max_bytes,
1484 unsigned long bits)
1485{
1486 struct rb_node *node;
1487 struct extent_state *state;
1488 u64 cur_start = *start;
1489 u64 total_bytes = 0;
1490 int found = 0;
1491
1492 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001493 WARN_ON(1);
1494 return 0;
1495 }
1496
Chris Masoncad321a2008-12-17 14:51:42 -05001497 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001498 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1499 total_bytes = tree->dirty_bytes;
1500 goto out;
1501 }
1502 /*
1503 * this search will find all the extents that end after
1504 * our range starts.
1505 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001506 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001507 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001508 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001509
Chris Masond3977122009-01-05 21:25:51 -05001510 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001511 state = rb_entry(node, struct extent_state, rb_node);
1512 if (state->start > search_end)
1513 break;
1514 if (state->end >= cur_start && (state->state & bits)) {
1515 total_bytes += min(search_end, state->end) + 1 -
1516 max(cur_start, state->start);
1517 if (total_bytes >= max_bytes)
1518 break;
1519 if (!found) {
1520 *start = state->start;
1521 found = 1;
1522 }
1523 }
1524 node = rb_next(node);
1525 if (!node)
1526 break;
1527 }
1528out:
Chris Masoncad321a2008-12-17 14:51:42 -05001529 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001530 return total_bytes;
1531}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001532
Chris Masond352ac62008-09-29 15:18:18 -04001533/*
1534 * set the private field for a given byte offset in the tree. If there isn't
1535 * an extent_state there already, this does nothing.
1536 */
Chris Masond1310b22008-01-24 16:13:08 -05001537int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1538{
1539 struct rb_node *node;
1540 struct extent_state *state;
1541 int ret = 0;
1542
Chris Masoncad321a2008-12-17 14:51:42 -05001543 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001544 /*
1545 * this search will find all the extents that end after
1546 * our range starts.
1547 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001548 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001549 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001550 ret = -ENOENT;
1551 goto out;
1552 }
1553 state = rb_entry(node, struct extent_state, rb_node);
1554 if (state->start != start) {
1555 ret = -ENOENT;
1556 goto out;
1557 }
1558 state->private = private;
1559out:
Chris Masoncad321a2008-12-17 14:51:42 -05001560 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001561 return ret;
1562}
1563
1564int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1565{
1566 struct rb_node *node;
1567 struct extent_state *state;
1568 int ret = 0;
1569
Chris Masoncad321a2008-12-17 14:51:42 -05001570 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001571 /*
1572 * this search will find all the extents that end after
1573 * our range starts.
1574 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001575 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001576 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001577 ret = -ENOENT;
1578 goto out;
1579 }
1580 state = rb_entry(node, struct extent_state, rb_node);
1581 if (state->start != start) {
1582 ret = -ENOENT;
1583 goto out;
1584 }
1585 *private = state->private;
1586out:
Chris Masoncad321a2008-12-17 14:51:42 -05001587 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001588 return ret;
1589}
1590
1591/*
1592 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001593 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001594 * has the bits set. Otherwise, 1 is returned if any bit in the
1595 * range is found set.
1596 */
1597int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001598 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001599{
1600 struct extent_state *state = NULL;
1601 struct rb_node *node;
1602 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001603
Chris Masoncad321a2008-12-17 14:51:42 -05001604 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001605 if (cached && cached->tree && cached->start == start)
1606 node = &cached->rb_node;
1607 else
1608 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001609 while (node && start <= end) {
1610 state = rb_entry(node, struct extent_state, rb_node);
1611
1612 if (filled && state->start > start) {
1613 bitset = 0;
1614 break;
1615 }
1616
1617 if (state->start > end)
1618 break;
1619
1620 if (state->state & bits) {
1621 bitset = 1;
1622 if (!filled)
1623 break;
1624 } else if (filled) {
1625 bitset = 0;
1626 break;
1627 }
Chris Mason46562ce2009-09-23 20:23:16 -04001628
1629 if (state->end == (u64)-1)
1630 break;
1631
Chris Masond1310b22008-01-24 16:13:08 -05001632 start = state->end + 1;
1633 if (start > end)
1634 break;
1635 node = rb_next(node);
1636 if (!node) {
1637 if (filled)
1638 bitset = 0;
1639 break;
1640 }
1641 }
Chris Masoncad321a2008-12-17 14:51:42 -05001642 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001643 return bitset;
1644}
Chris Masond1310b22008-01-24 16:13:08 -05001645
1646/*
1647 * helper function to set a given page up to date if all the
1648 * extents in the tree for that page are up to date
1649 */
1650static int check_page_uptodate(struct extent_io_tree *tree,
1651 struct page *page)
1652{
1653 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1654 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001655 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001656 SetPageUptodate(page);
1657 return 0;
1658}
1659
1660/*
1661 * helper function to unlock a page if all the extents in the tree
1662 * for that page are unlocked
1663 */
1664static int check_page_locked(struct extent_io_tree *tree,
1665 struct page *page)
1666{
1667 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1668 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001669 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001670 unlock_page(page);
1671 return 0;
1672}
1673
1674/*
1675 * helper function to end page writeback if all the extents
1676 * in the tree for that page are done with writeback
1677 */
1678static int check_page_writeback(struct extent_io_tree *tree,
1679 struct page *page)
1680{
Chris Mason1edbb732009-09-02 13:24:36 -04001681 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001682 return 0;
1683}
1684
1685/* lots and lots of room for performance fixes in the end_bio funcs */
1686
1687/*
1688 * after a writepage IO is done, we need to:
1689 * clear the uptodate bits on error
1690 * clear the writeback bits in the extent tree for this IO
1691 * end_page_writeback if the page has no more pending IO
1692 *
1693 * Scheduling is not allowed, so the extent state tree is expected
1694 * to have one and only one object corresponding to this IO.
1695 */
Chris Masond1310b22008-01-24 16:13:08 -05001696static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001697{
Chris Mason1259ab72008-05-12 13:39:03 -04001698 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001699 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001700 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001701 u64 start;
1702 u64 end;
1703 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001704 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001705
Chris Masond1310b22008-01-24 16:13:08 -05001706 do {
1707 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001708 tree = &BTRFS_I(page->mapping->host)->io_tree;
1709
Chris Masond1310b22008-01-24 16:13:08 -05001710 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1711 bvec->bv_offset;
1712 end = start + bvec->bv_len - 1;
1713
1714 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1715 whole_page = 1;
1716 else
1717 whole_page = 0;
1718
1719 if (--bvec >= bio->bi_io_vec)
1720 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001721 if (tree->ops && tree->ops->writepage_end_io_hook) {
1722 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001723 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001724 if (ret)
1725 uptodate = 0;
1726 }
1727
1728 if (!uptodate && tree->ops &&
1729 tree->ops->writepage_io_failed_hook) {
1730 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001731 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001732 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001733 uptodate = (err == 0);
1734 continue;
1735 }
1736 }
1737
Chris Masond1310b22008-01-24 16:13:08 -05001738 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001740 ClearPageUptodate(page);
1741 SetPageError(page);
1742 }
Chris Mason70dec802008-01-29 09:59:12 -05001743
Chris Masond1310b22008-01-24 16:13:08 -05001744 if (whole_page)
1745 end_page_writeback(page);
1746 else
1747 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001748 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001749
Chris Masond1310b22008-01-24 16:13:08 -05001750 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001751}
1752
1753/*
1754 * after a readpage IO is done, we need to:
1755 * clear the uptodate bits on error
1756 * set the uptodate bits if things worked
1757 * set the page up to date if all extents in the tree are uptodate
1758 * clear the lock bit in the extent tree
1759 * unlock the page if there are no other extents locked for it
1760 *
1761 * Scheduling is not allowed, so the extent state tree is expected
1762 * to have one and only one object corresponding to this IO.
1763 */
Chris Masond1310b22008-01-24 16:13:08 -05001764static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001765{
1766 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001767 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1768 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001769 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001770 u64 start;
1771 u64 end;
1772 int whole_page;
1773 int ret;
1774
Chris Masond20f7042008-12-08 16:58:54 -05001775 if (err)
1776 uptodate = 0;
1777
Chris Masond1310b22008-01-24 16:13:08 -05001778 do {
1779 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001780 tree = &BTRFS_I(page->mapping->host)->io_tree;
1781
Chris Masond1310b22008-01-24 16:13:08 -05001782 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1783 bvec->bv_offset;
1784 end = start + bvec->bv_len - 1;
1785
1786 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1787 whole_page = 1;
1788 else
1789 whole_page = 0;
1790
Chris Mason4125bf72010-02-03 18:18:45 +00001791 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001792 prefetchw(&bvec->bv_page->flags);
1793
1794 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001795 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001796 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001797 if (ret)
1798 uptodate = 0;
1799 }
Chris Mason7e383262008-04-09 16:28:12 -04001800 if (!uptodate && tree->ops &&
1801 tree->ops->readpage_io_failed_hook) {
1802 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001803 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001804 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001805 uptodate =
1806 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001807 if (err)
1808 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001809 continue;
1810 }
1811 }
Chris Mason70dec802008-01-29 09:59:12 -05001812
Chris Mason771ed682008-11-06 22:02:51 -05001813 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001814 set_extent_uptodate(tree, start, end,
1815 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001816 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001817 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001818
Chris Mason70dec802008-01-29 09:59:12 -05001819 if (whole_page) {
1820 if (uptodate) {
1821 SetPageUptodate(page);
1822 } else {
1823 ClearPageUptodate(page);
1824 SetPageError(page);
1825 }
Chris Masond1310b22008-01-24 16:13:08 -05001826 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001827 } else {
1828 if (uptodate) {
1829 check_page_uptodate(tree, page);
1830 } else {
1831 ClearPageUptodate(page);
1832 SetPageError(page);
1833 }
Chris Masond1310b22008-01-24 16:13:08 -05001834 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001835 }
Chris Mason4125bf72010-02-03 18:18:45 +00001836 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001837
1838 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001839}
1840
1841/*
1842 * IO done from prepare_write is pretty simple, we just unlock
1843 * the structs in the extent tree when done, and set the uptodate bits
1844 * as appropriate.
1845 */
Chris Masond1310b22008-01-24 16:13:08 -05001846static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001847{
1848 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1849 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001850 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001851 u64 start;
1852 u64 end;
1853
Chris Masond1310b22008-01-24 16:13:08 -05001854 do {
1855 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001856 tree = &BTRFS_I(page->mapping->host)->io_tree;
1857
Chris Masond1310b22008-01-24 16:13:08 -05001858 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1859 bvec->bv_offset;
1860 end = start + bvec->bv_len - 1;
1861
1862 if (--bvec >= bio->bi_io_vec)
1863 prefetchw(&bvec->bv_page->flags);
1864
1865 if (uptodate) {
1866 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1867 } else {
1868 ClearPageUptodate(page);
1869 SetPageError(page);
1870 }
1871
1872 unlock_extent(tree, start, end, GFP_ATOMIC);
1873
1874 } while (bvec >= bio->bi_io_vec);
1875
1876 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001877}
1878
1879static struct bio *
1880extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1881 gfp_t gfp_flags)
1882{
1883 struct bio *bio;
1884
1885 bio = bio_alloc(gfp_flags, nr_vecs);
1886
1887 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1888 while (!bio && (nr_vecs /= 2))
1889 bio = bio_alloc(gfp_flags, nr_vecs);
1890 }
1891
1892 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001893 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001894 bio->bi_bdev = bdev;
1895 bio->bi_sector = first_sector;
1896 }
1897 return bio;
1898}
1899
Chris Masonc8b97812008-10-29 14:49:59 -04001900static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1901 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001902{
Chris Masond1310b22008-01-24 16:13:08 -05001903 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001904 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1905 struct page *page = bvec->bv_page;
1906 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001907 u64 start;
1908 u64 end;
1909
1910 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1911 end = start + bvec->bv_len - 1;
1912
David Woodhouse902b22f2008-08-20 08:51:49 -04001913 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001914
1915 bio_get(bio);
1916
Chris Mason065631f2008-02-20 12:07:25 -05001917 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001918 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001919 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001920 else
1921 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001922 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1923 ret = -EOPNOTSUPP;
1924 bio_put(bio);
1925 return ret;
1926}
1927
1928static int submit_extent_page(int rw, struct extent_io_tree *tree,
1929 struct page *page, sector_t sector,
1930 size_t size, unsigned long offset,
1931 struct block_device *bdev,
1932 struct bio **bio_ret,
1933 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001934 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001935 int mirror_num,
1936 unsigned long prev_bio_flags,
1937 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001938{
1939 int ret = 0;
1940 struct bio *bio;
1941 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001942 int contig = 0;
1943 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1944 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001945 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001946
1947 if (bio_ret && *bio_ret) {
1948 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001949 if (old_compressed)
1950 contig = bio->bi_sector == sector;
1951 else
1952 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1953 sector;
1954
1955 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001956 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001957 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1958 bio_flags)) ||
1959 bio_add_page(bio, page, page_size, offset) < page_size) {
1960 ret = submit_one_bio(rw, bio, mirror_num,
1961 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001962 bio = NULL;
1963 } else {
1964 return 0;
1965 }
1966 }
Chris Masonc8b97812008-10-29 14:49:59 -04001967 if (this_compressed)
1968 nr = BIO_MAX_PAGES;
1969 else
1970 nr = bio_get_nr_vecs(bdev);
1971
Chris Masond1310b22008-01-24 16:13:08 -05001972 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001973
Chris Masonc8b97812008-10-29 14:49:59 -04001974 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001975 bio->bi_end_io = end_io_func;
1976 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001977
Chris Masond3977122009-01-05 21:25:51 -05001978 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001979 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001980 else
Chris Masonc8b97812008-10-29 14:49:59 -04001981 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001982
1983 return ret;
1984}
1985
1986void set_page_extent_mapped(struct page *page)
1987{
1988 if (!PagePrivate(page)) {
1989 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001990 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001991 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001992 }
1993}
1994
Christoph Hellwigb2950862008-12-02 09:54:17 -05001995static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001996{
1997 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1998}
1999
2000/*
2001 * basic readpage implementation. Locked extent state structs are inserted
2002 * into the tree that are removed when the IO is done (by the end_io
2003 * handlers)
2004 */
2005static int __extent_read_full_page(struct extent_io_tree *tree,
2006 struct page *page,
2007 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002008 struct bio **bio, int mirror_num,
2009 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002010{
2011 struct inode *inode = page->mapping->host;
2012 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2013 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2014 u64 end;
2015 u64 cur = start;
2016 u64 extent_offset;
2017 u64 last_byte = i_size_read(inode);
2018 u64 block_start;
2019 u64 cur_end;
2020 sector_t sector;
2021 struct extent_map *em;
2022 struct block_device *bdev;
2023 int ret;
2024 int nr = 0;
2025 size_t page_offset = 0;
2026 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002027 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002028 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002029 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002030
2031 set_page_extent_mapped(page);
2032
2033 end = page_end;
2034 lock_extent(tree, start, end, GFP_NOFS);
2035
Chris Masonc8b97812008-10-29 14:49:59 -04002036 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2037 char *userpage;
2038 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2039
2040 if (zero_offset) {
2041 iosize = PAGE_CACHE_SIZE - zero_offset;
2042 userpage = kmap_atomic(page, KM_USER0);
2043 memset(userpage + zero_offset, 0, iosize);
2044 flush_dcache_page(page);
2045 kunmap_atomic(userpage, KM_USER0);
2046 }
2047 }
Chris Masond1310b22008-01-24 16:13:08 -05002048 while (cur <= end) {
2049 if (cur >= last_byte) {
2050 char *userpage;
2051 iosize = PAGE_CACHE_SIZE - page_offset;
2052 userpage = kmap_atomic(page, KM_USER0);
2053 memset(userpage + page_offset, 0, iosize);
2054 flush_dcache_page(page);
2055 kunmap_atomic(userpage, KM_USER0);
2056 set_extent_uptodate(tree, cur, cur + iosize - 1,
2057 GFP_NOFS);
2058 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2059 break;
2060 }
2061 em = get_extent(inode, page, page_offset, cur,
2062 end - cur + 1, 0);
2063 if (IS_ERR(em) || !em) {
2064 SetPageError(page);
2065 unlock_extent(tree, cur, end, GFP_NOFS);
2066 break;
2067 }
Chris Masond1310b22008-01-24 16:13:08 -05002068 extent_offset = cur - em->start;
2069 BUG_ON(extent_map_end(em) <= cur);
2070 BUG_ON(end < cur);
2071
Chris Masonc8b97812008-10-29 14:49:59 -04002072 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2073 this_bio_flag = EXTENT_BIO_COMPRESSED;
2074
Chris Masond1310b22008-01-24 16:13:08 -05002075 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2076 cur_end = min(extent_map_end(em) - 1, end);
2077 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002078 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2079 disk_io_size = em->block_len;
2080 sector = em->block_start >> 9;
2081 } else {
2082 sector = (em->block_start + extent_offset) >> 9;
2083 disk_io_size = iosize;
2084 }
Chris Masond1310b22008-01-24 16:13:08 -05002085 bdev = em->bdev;
2086 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002087 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2088 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002089 free_extent_map(em);
2090 em = NULL;
2091
2092 /* we've found a hole, just zero and go on */
2093 if (block_start == EXTENT_MAP_HOLE) {
2094 char *userpage;
2095 userpage = kmap_atomic(page, KM_USER0);
2096 memset(userpage + page_offset, 0, iosize);
2097 flush_dcache_page(page);
2098 kunmap_atomic(userpage, KM_USER0);
2099
2100 set_extent_uptodate(tree, cur, cur + iosize - 1,
2101 GFP_NOFS);
2102 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2103 cur = cur + iosize;
2104 page_offset += iosize;
2105 continue;
2106 }
2107 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002108 if (test_range_bit(tree, cur, cur_end,
2109 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002110 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002111 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2112 cur = cur + iosize;
2113 page_offset += iosize;
2114 continue;
2115 }
Chris Mason70dec802008-01-29 09:59:12 -05002116 /* we have an inline extent but it didn't get marked up
2117 * to date. Error out
2118 */
2119 if (block_start == EXTENT_MAP_INLINE) {
2120 SetPageError(page);
2121 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2122 cur = cur + iosize;
2123 page_offset += iosize;
2124 continue;
2125 }
Chris Masond1310b22008-01-24 16:13:08 -05002126
2127 ret = 0;
2128 if (tree->ops && tree->ops->readpage_io_hook) {
2129 ret = tree->ops->readpage_io_hook(page, cur,
2130 cur + iosize - 1);
2131 }
2132 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002133 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2134 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002135 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002136 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002137 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002138 end_bio_extent_readpage, mirror_num,
2139 *bio_flags,
2140 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002141 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002142 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002143 }
2144 if (ret)
2145 SetPageError(page);
2146 cur = cur + iosize;
2147 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002148 }
2149 if (!nr) {
2150 if (!PageError(page))
2151 SetPageUptodate(page);
2152 unlock_page(page);
2153 }
2154 return 0;
2155}
2156
2157int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2158 get_extent_t *get_extent)
2159{
2160 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002161 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002162 int ret;
2163
Chris Masonc8b97812008-10-29 14:49:59 -04002164 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2165 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002166 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002167 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002168 return ret;
2169}
Chris Masond1310b22008-01-24 16:13:08 -05002170
Chris Mason11c83492009-04-20 15:50:09 -04002171static noinline void update_nr_written(struct page *page,
2172 struct writeback_control *wbc,
2173 unsigned long nr_written)
2174{
2175 wbc->nr_to_write -= nr_written;
2176 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2177 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2178 page->mapping->writeback_index = page->index + nr_written;
2179}
2180
Chris Masond1310b22008-01-24 16:13:08 -05002181/*
2182 * the writepage semantics are similar to regular writepage. extent
2183 * records are inserted to lock ranges in the tree, and as dirty areas
2184 * are found, they are marked writeback. Then the lock bits are removed
2185 * and the end_io handler clears the writeback ranges
2186 */
2187static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2188 void *data)
2189{
2190 struct inode *inode = page->mapping->host;
2191 struct extent_page_data *epd = data;
2192 struct extent_io_tree *tree = epd->tree;
2193 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2194 u64 delalloc_start;
2195 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2196 u64 end;
2197 u64 cur = start;
2198 u64 extent_offset;
2199 u64 last_byte = i_size_read(inode);
2200 u64 block_start;
2201 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002202 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002203 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002204 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002205 struct extent_map *em;
2206 struct block_device *bdev;
2207 int ret;
2208 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002209 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002210 size_t blocksize;
2211 loff_t i_size = i_size_read(inode);
2212 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2213 u64 nr_delalloc;
2214 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002215 int page_started;
2216 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002217 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002218 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002219
Chris Masonffbd5172009-04-20 15:50:09 -04002220 if (wbc->sync_mode == WB_SYNC_ALL)
2221 write_flags = WRITE_SYNC_PLUG;
2222 else
2223 write_flags = WRITE;
2224
Chris Masond1310b22008-01-24 16:13:08 -05002225 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002226 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002227 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002228 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002229 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002230 unlock_page(page);
2231 return 0;
2232 }
2233
2234 if (page->index == end_index) {
2235 char *userpage;
2236
Chris Masond1310b22008-01-24 16:13:08 -05002237 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002238 memset(userpage + pg_offset, 0,
2239 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002240 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002241 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002242 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002243 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002244
2245 set_page_extent_mapped(page);
2246
2247 delalloc_start = start;
2248 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002249 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002250 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002251 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002252 /*
2253 * make sure the wbc mapping index is at least updated
2254 * to this page.
2255 */
2256 update_nr_written(page, wbc, 0);
2257
Chris Masond3977122009-01-05 21:25:51 -05002258 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002259 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002260 page,
2261 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002262 &delalloc_end,
2263 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002264 if (nr_delalloc == 0) {
2265 delalloc_start = delalloc_end + 1;
2266 continue;
2267 }
2268 tree->ops->fill_delalloc(inode, page, delalloc_start,
2269 delalloc_end, &page_started,
2270 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002271 /*
2272 * delalloc_end is already one less than the total
2273 * length, so we don't subtract one from
2274 * PAGE_CACHE_SIZE
2275 */
2276 delalloc_to_write += (delalloc_end - delalloc_start +
2277 PAGE_CACHE_SIZE) >>
2278 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002279 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002280 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002281 if (wbc->nr_to_write < delalloc_to_write) {
2282 int thresh = 8192;
2283
2284 if (delalloc_to_write < thresh * 2)
2285 thresh = delalloc_to_write;
2286 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2287 thresh);
2288 }
Chris Masonc8b97812008-10-29 14:49:59 -04002289
Chris Mason771ed682008-11-06 22:02:51 -05002290 /* did the fill delalloc function already unlock and start
2291 * the IO?
2292 */
2293 if (page_started) {
2294 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002295 /*
2296 * we've unlocked the page, so we can't update
2297 * the mapping's writeback index, just update
2298 * nr_to_write.
2299 */
2300 wbc->nr_to_write -= nr_written;
2301 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002302 }
Chris Masonc8b97812008-10-29 14:49:59 -04002303 }
Chris Mason247e7432008-07-17 12:53:51 -04002304 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002305 ret = tree->ops->writepage_start_hook(page, start,
2306 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002307 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002308 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002309 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002310 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002311 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002312 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002313 }
2314 }
2315
Chris Mason11c83492009-04-20 15:50:09 -04002316 /*
2317 * we don't want to touch the inode after unlocking the page,
2318 * so we update the mapping writeback index now
2319 */
2320 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002321
Chris Masond1310b22008-01-24 16:13:08 -05002322 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002323 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002324 if (tree->ops && tree->ops->writepage_end_io_hook)
2325 tree->ops->writepage_end_io_hook(page, start,
2326 page_end, NULL, 1);
2327 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002328 goto done;
2329 }
2330
Chris Masond1310b22008-01-24 16:13:08 -05002331 blocksize = inode->i_sb->s_blocksize;
2332
2333 while (cur <= end) {
2334 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002335 if (tree->ops && tree->ops->writepage_end_io_hook)
2336 tree->ops->writepage_end_io_hook(page, cur,
2337 page_end, NULL, 1);
2338 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002339 break;
2340 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002341 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002342 end - cur + 1, 1);
2343 if (IS_ERR(em) || !em) {
2344 SetPageError(page);
2345 break;
2346 }
2347
2348 extent_offset = cur - em->start;
2349 BUG_ON(extent_map_end(em) <= cur);
2350 BUG_ON(end < cur);
2351 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2352 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2353 sector = (em->block_start + extent_offset) >> 9;
2354 bdev = em->bdev;
2355 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002356 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002357 free_extent_map(em);
2358 em = NULL;
2359
Chris Masonc8b97812008-10-29 14:49:59 -04002360 /*
2361 * compressed and inline extents are written through other
2362 * paths in the FS
2363 */
2364 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002365 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002366 /*
2367 * end_io notification does not happen here for
2368 * compressed extents
2369 */
2370 if (!compressed && tree->ops &&
2371 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002372 tree->ops->writepage_end_io_hook(page, cur,
2373 cur + iosize - 1,
2374 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002375 else if (compressed) {
2376 /* we don't want to end_page_writeback on
2377 * a compressed extent. this happens
2378 * elsewhere
2379 */
2380 nr++;
2381 }
2382
2383 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002384 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002385 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002386 continue;
2387 }
Chris Masond1310b22008-01-24 16:13:08 -05002388 /* leave this out until we have a page_mkwrite call */
2389 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002390 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002391 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002392 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002393 continue;
2394 }
Chris Masonc8b97812008-10-29 14:49:59 -04002395
Chris Masond1310b22008-01-24 16:13:08 -05002396 if (tree->ops && tree->ops->writepage_io_hook) {
2397 ret = tree->ops->writepage_io_hook(page, cur,
2398 cur + iosize - 1);
2399 } else {
2400 ret = 0;
2401 }
Chris Mason1259ab72008-05-12 13:39:03 -04002402 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002403 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002404 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002405 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002406
Chris Masond1310b22008-01-24 16:13:08 -05002407 set_range_writeback(tree, cur, cur + iosize - 1);
2408 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002409 printk(KERN_ERR "btrfs warning page %lu not "
2410 "writeback, cur %llu end %llu\n",
2411 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002412 (unsigned long long)end);
2413 }
2414
Chris Masonffbd5172009-04-20 15:50:09 -04002415 ret = submit_extent_page(write_flags, tree, page,
2416 sector, iosize, pg_offset,
2417 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002418 end_bio_extent_writepage,
2419 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002420 if (ret)
2421 SetPageError(page);
2422 }
2423 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002424 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002425 nr++;
2426 }
2427done:
2428 if (nr == 0) {
2429 /* make sure the mapping tag for page dirty gets cleared */
2430 set_page_writeback(page);
2431 end_page_writeback(page);
2432 }
Chris Masond1310b22008-01-24 16:13:08 -05002433 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002434
Chris Mason11c83492009-04-20 15:50:09 -04002435done_unlocked:
2436
Chris Mason2c64c532009-09-02 15:04:12 -04002437 /* drop our reference on any cached states */
2438 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002439 return 0;
2440}
2441
Chris Masond1310b22008-01-24 16:13:08 -05002442/**
Chris Mason4bef0842008-09-08 11:18:08 -04002443 * 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 -05002444 * @mapping: address space structure to write
2445 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2446 * @writepage: function called for each page
2447 * @data: data passed to writepage function
2448 *
2449 * If a page is already under I/O, write_cache_pages() skips it, even
2450 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2451 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2452 * and msync() need to guarantee that all the data which was dirty at the time
2453 * the call was made get new I/O started against them. If wbc->sync_mode is
2454 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2455 * existing IO to complete.
2456 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002457static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002458 struct address_space *mapping,
2459 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002460 writepage_t writepage, void *data,
2461 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002462{
Chris Masond1310b22008-01-24 16:13:08 -05002463 int ret = 0;
2464 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002465 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002466 struct pagevec pvec;
2467 int nr_pages;
2468 pgoff_t index;
2469 pgoff_t end; /* Inclusive */
2470 int scanned = 0;
2471 int range_whole = 0;
2472
Chris Masond1310b22008-01-24 16:13:08 -05002473 pagevec_init(&pvec, 0);
2474 if (wbc->range_cyclic) {
2475 index = mapping->writeback_index; /* Start from prev offset */
2476 end = -1;
2477 } else {
2478 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2479 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2480 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2481 range_whole = 1;
2482 scanned = 1;
2483 }
2484retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002485 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002486 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002487 PAGECACHE_TAG_DIRTY, min(end - index,
2488 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002489 unsigned i;
2490
2491 scanned = 1;
2492 for (i = 0; i < nr_pages; i++) {
2493 struct page *page = pvec.pages[i];
2494
2495 /*
2496 * At this point we hold neither mapping->tree_lock nor
2497 * lock on the page itself: the page may be truncated or
2498 * invalidated (changing page->mapping to NULL), or even
2499 * swizzled back from swapper_space to tmpfs file
2500 * mapping
2501 */
Chris Mason4bef0842008-09-08 11:18:08 -04002502 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2503 tree->ops->write_cache_pages_lock_hook(page);
2504 else
2505 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002506
2507 if (unlikely(page->mapping != mapping)) {
2508 unlock_page(page);
2509 continue;
2510 }
2511
2512 if (!wbc->range_cyclic && page->index > end) {
2513 done = 1;
2514 unlock_page(page);
2515 continue;
2516 }
2517
Chris Masond2c3f4f2008-11-19 12:44:22 -05002518 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002519 if (PageWriteback(page))
2520 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002521 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002522 }
Chris Masond1310b22008-01-24 16:13:08 -05002523
2524 if (PageWriteback(page) ||
2525 !clear_page_dirty_for_io(page)) {
2526 unlock_page(page);
2527 continue;
2528 }
2529
2530 ret = (*writepage)(page, wbc, data);
2531
2532 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2533 unlock_page(page);
2534 ret = 0;
2535 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002536 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002537 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002538
2539 /*
2540 * the filesystem may choose to bump up nr_to_write.
2541 * We have to make sure to honor the new nr_to_write
2542 * at any time
2543 */
2544 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002545 }
2546 pagevec_release(&pvec);
2547 cond_resched();
2548 }
2549 if (!scanned && !done) {
2550 /*
2551 * We hit the last page and there is more work to be done: wrap
2552 * back to the start of the file
2553 */
2554 scanned = 1;
2555 index = 0;
2556 goto retry;
2557 }
Chris Masond1310b22008-01-24 16:13:08 -05002558 return ret;
2559}
Chris Masond1310b22008-01-24 16:13:08 -05002560
Chris Masonffbd5172009-04-20 15:50:09 -04002561static void flush_epd_write_bio(struct extent_page_data *epd)
2562{
2563 if (epd->bio) {
2564 if (epd->sync_io)
2565 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2566 else
2567 submit_one_bio(WRITE, epd->bio, 0, 0);
2568 epd->bio = NULL;
2569 }
2570}
2571
Chris Masond2c3f4f2008-11-19 12:44:22 -05002572static noinline void flush_write_bio(void *data)
2573{
2574 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002575 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002576}
2577
Chris Masond1310b22008-01-24 16:13:08 -05002578int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2579 get_extent_t *get_extent,
2580 struct writeback_control *wbc)
2581{
2582 int ret;
2583 struct address_space *mapping = page->mapping;
2584 struct extent_page_data epd = {
2585 .bio = NULL,
2586 .tree = tree,
2587 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002588 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002589 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002590 };
2591 struct writeback_control wbc_writepages = {
2592 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002593 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002594 .older_than_this = NULL,
2595 .nr_to_write = 64,
2596 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2597 .range_end = (loff_t)-1,
2598 };
2599
Chris Masond1310b22008-01-24 16:13:08 -05002600 ret = __extent_writepage(page, wbc, &epd);
2601
Chris Mason4bef0842008-09-08 11:18:08 -04002602 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002603 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002604 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002605 return ret;
2606}
Chris Masond1310b22008-01-24 16:13:08 -05002607
Chris Mason771ed682008-11-06 22:02:51 -05002608int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2609 u64 start, u64 end, get_extent_t *get_extent,
2610 int mode)
2611{
2612 int ret = 0;
2613 struct address_space *mapping = inode->i_mapping;
2614 struct page *page;
2615 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2616 PAGE_CACHE_SHIFT;
2617
2618 struct extent_page_data epd = {
2619 .bio = NULL,
2620 .tree = tree,
2621 .get_extent = get_extent,
2622 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002623 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002624 };
2625 struct writeback_control wbc_writepages = {
2626 .bdi = inode->i_mapping->backing_dev_info,
2627 .sync_mode = mode,
2628 .older_than_this = NULL,
2629 .nr_to_write = nr_pages * 2,
2630 .range_start = start,
2631 .range_end = end + 1,
2632 };
2633
Chris Masond3977122009-01-05 21:25:51 -05002634 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002635 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2636 if (clear_page_dirty_for_io(page))
2637 ret = __extent_writepage(page, &wbc_writepages, &epd);
2638 else {
2639 if (tree->ops && tree->ops->writepage_end_io_hook)
2640 tree->ops->writepage_end_io_hook(page, start,
2641 start + PAGE_CACHE_SIZE - 1,
2642 NULL, 1);
2643 unlock_page(page);
2644 }
2645 page_cache_release(page);
2646 start += PAGE_CACHE_SIZE;
2647 }
2648
Chris Masonffbd5172009-04-20 15:50:09 -04002649 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002650 return ret;
2651}
Chris Masond1310b22008-01-24 16:13:08 -05002652
2653int extent_writepages(struct extent_io_tree *tree,
2654 struct address_space *mapping,
2655 get_extent_t *get_extent,
2656 struct writeback_control *wbc)
2657{
2658 int ret = 0;
2659 struct extent_page_data epd = {
2660 .bio = NULL,
2661 .tree = tree,
2662 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002663 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002664 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002665 };
2666
Chris Mason4bef0842008-09-08 11:18:08 -04002667 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002668 __extent_writepage, &epd,
2669 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002670 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002671 return ret;
2672}
Chris Masond1310b22008-01-24 16:13:08 -05002673
2674int extent_readpages(struct extent_io_tree *tree,
2675 struct address_space *mapping,
2676 struct list_head *pages, unsigned nr_pages,
2677 get_extent_t get_extent)
2678{
2679 struct bio *bio = NULL;
2680 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002681 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002682
Chris Masond1310b22008-01-24 16:13:08 -05002683 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2684 struct page *page = list_entry(pages->prev, struct page, lru);
2685
2686 prefetchw(&page->flags);
2687 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002688 if (!add_to_page_cache_lru(page, mapping,
Chris Masond1310b22008-01-24 16:13:08 -05002689 page->index, GFP_KERNEL)) {
Chris Masonf1885912008-04-09 16:28:12 -04002690 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002691 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002692 }
2693 page_cache_release(page);
2694 }
Chris Masond1310b22008-01-24 16:13:08 -05002695 BUG_ON(!list_empty(pages));
2696 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002697 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002698 return 0;
2699}
Chris Masond1310b22008-01-24 16:13:08 -05002700
2701/*
2702 * basic invalidatepage code, this waits on any locked or writeback
2703 * ranges corresponding to the page, and then deletes any extent state
2704 * records from the tree
2705 */
2706int extent_invalidatepage(struct extent_io_tree *tree,
2707 struct page *page, unsigned long offset)
2708{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002709 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002710 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2711 u64 end = start + PAGE_CACHE_SIZE - 1;
2712 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2713
Chris Masond3977122009-01-05 21:25:51 -05002714 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002715 if (start > end)
2716 return 0;
2717
Josef Bacik2ac55d42010-02-03 19:33:23 +00002718 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002719 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002720 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002721 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2722 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002723 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002724 return 0;
2725}
Chris Masond1310b22008-01-24 16:13:08 -05002726
2727/*
2728 * simple commit_write call, set_range_dirty is used to mark both
2729 * the pages and the extent records as dirty
2730 */
2731int extent_commit_write(struct extent_io_tree *tree,
2732 struct inode *inode, struct page *page,
2733 unsigned from, unsigned to)
2734{
2735 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2736
2737 set_page_extent_mapped(page);
2738 set_page_dirty(page);
2739
2740 if (pos > inode->i_size) {
2741 i_size_write(inode, pos);
2742 mark_inode_dirty(inode);
2743 }
2744 return 0;
2745}
Chris Masond1310b22008-01-24 16:13:08 -05002746
2747int extent_prepare_write(struct extent_io_tree *tree,
2748 struct inode *inode, struct page *page,
2749 unsigned from, unsigned to, get_extent_t *get_extent)
2750{
2751 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2752 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2753 u64 block_start;
2754 u64 orig_block_start;
2755 u64 block_end;
2756 u64 cur_end;
2757 struct extent_map *em;
2758 unsigned blocksize = 1 << inode->i_blkbits;
2759 size_t page_offset = 0;
2760 size_t block_off_start;
2761 size_t block_off_end;
2762 int err = 0;
2763 int iocount = 0;
2764 int ret = 0;
2765 int isnew;
2766
2767 set_page_extent_mapped(page);
2768
2769 block_start = (page_start + from) & ~((u64)blocksize - 1);
2770 block_end = (page_start + to - 1) | (blocksize - 1);
2771 orig_block_start = block_start;
2772
2773 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002774 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002775 em = get_extent(inode, page, page_offset, block_start,
2776 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002777 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002778 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002779
Chris Masond1310b22008-01-24 16:13:08 -05002780 cur_end = min(block_end, extent_map_end(em) - 1);
2781 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2782 block_off_end = block_off_start + blocksize;
2783 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2784
2785 if (!PageUptodate(page) && isnew &&
2786 (block_off_end > to || block_off_start < from)) {
2787 void *kaddr;
2788
2789 kaddr = kmap_atomic(page, KM_USER0);
2790 if (block_off_end > to)
2791 memset(kaddr + to, 0, block_off_end - to);
2792 if (block_off_start < from)
2793 memset(kaddr + block_off_start, 0,
2794 from - block_off_start);
2795 flush_dcache_page(page);
2796 kunmap_atomic(kaddr, KM_USER0);
2797 }
2798 if ((em->block_start != EXTENT_MAP_HOLE &&
2799 em->block_start != EXTENT_MAP_INLINE) &&
2800 !isnew && !PageUptodate(page) &&
2801 (block_off_end > to || block_off_start < from) &&
2802 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002803 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002804 u64 sector;
2805 u64 extent_offset = block_start - em->start;
2806 size_t iosize;
2807 sector = (em->block_start + extent_offset) >> 9;
2808 iosize = (cur_end - block_start + blocksize) &
2809 ~((u64)blocksize - 1);
2810 /*
2811 * we've already got the extent locked, but we
2812 * need to split the state such that our end_bio
2813 * handler can clear the lock.
2814 */
2815 set_extent_bit(tree, block_start,
2816 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002817 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002818 ret = submit_extent_page(READ, tree, page,
2819 sector, iosize, page_offset, em->bdev,
2820 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002821 end_bio_extent_preparewrite, 0,
2822 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002823 iocount++;
2824 block_start = block_start + iosize;
2825 } else {
2826 set_extent_uptodate(tree, block_start, cur_end,
2827 GFP_NOFS);
2828 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2829 block_start = cur_end + 1;
2830 }
2831 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2832 free_extent_map(em);
2833 }
2834 if (iocount) {
2835 wait_extent_bit(tree, orig_block_start,
2836 block_end, EXTENT_LOCKED);
2837 }
2838 check_page_uptodate(tree, page);
2839err:
2840 /* FIXME, zero out newly allocated blocks on error */
2841 return err;
2842}
Chris Masond1310b22008-01-24 16:13:08 -05002843
2844/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002845 * a helper for releasepage, this tests for areas of the page that
2846 * are locked or under IO and drops the related state bits if it is safe
2847 * to drop the page.
2848 */
2849int try_release_extent_state(struct extent_map_tree *map,
2850 struct extent_io_tree *tree, struct page *page,
2851 gfp_t mask)
2852{
2853 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2854 u64 end = start + PAGE_CACHE_SIZE - 1;
2855 int ret = 1;
2856
Chris Mason211f90e2008-07-18 11:56:15 -04002857 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002858 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002859 ret = 0;
2860 else {
2861 if ((mask & GFP_NOFS) == GFP_NOFS)
2862 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002863 /*
2864 * at this point we can safely clear everything except the
2865 * locked bit and the nodatasum bit
2866 */
2867 clear_extent_bit(tree, start, end,
2868 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2869 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002870 }
2871 return ret;
2872}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002873
2874/*
Chris Masond1310b22008-01-24 16:13:08 -05002875 * a helper for releasepage. As long as there are no locked extents
2876 * in the range corresponding to the page, both state records and extent
2877 * map records are removed
2878 */
2879int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002880 struct extent_io_tree *tree, struct page *page,
2881 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002882{
2883 struct extent_map *em;
2884 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2885 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002886
Chris Mason70dec802008-01-29 09:59:12 -05002887 if ((mask & __GFP_WAIT) &&
2888 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002889 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002890 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002891 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002892 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002893 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002894 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002895 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002896 break;
2897 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002898 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2899 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002900 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002901 free_extent_map(em);
2902 break;
2903 }
2904 if (!test_range_bit(tree, em->start,
2905 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002906 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002907 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002908 remove_extent_mapping(map, em);
2909 /* once for the rb tree */
2910 free_extent_map(em);
2911 }
2912 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002913 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002914
2915 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002916 free_extent_map(em);
2917 }
Chris Masond1310b22008-01-24 16:13:08 -05002918 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002919 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002920}
Chris Masond1310b22008-01-24 16:13:08 -05002921
2922sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2923 get_extent_t *get_extent)
2924{
2925 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002926 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002927 u64 start = iblock << inode->i_blkbits;
2928 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002929 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002930 struct extent_map *em;
2931
Josef Bacik2ac55d42010-02-03 19:33:23 +00002932 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2933 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002934 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002935 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2936 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002937 if (!em || IS_ERR(em))
2938 return 0;
2939
Yan Zhengd899e052008-10-30 14:25:28 -04002940 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002941 goto out;
2942
2943 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002944out:
2945 free_extent_map(em);
2946 return sector;
2947}
2948
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002949int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2950 __u64 start, __u64 len, get_extent_t *get_extent)
2951{
2952 int ret;
2953 u64 off = start;
2954 u64 max = start + len;
2955 u32 flags = 0;
2956 u64 disko = 0;
2957 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002958 struct extent_state *cached_state = NULL;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002959 int end = 0;
2960 u64 em_start = 0, em_len = 0;
2961 unsigned long emflags;
2962 ret = 0;
2963
2964 if (len == 0)
2965 return -EINVAL;
2966
Josef Bacik2ac55d42010-02-03 19:33:23 +00002967 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2968 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002969 em = get_extent(inode, NULL, 0, off, max - off, 0);
2970 if (!em)
2971 goto out;
2972 if (IS_ERR(em)) {
2973 ret = PTR_ERR(em);
2974 goto out;
2975 }
2976 while (!end) {
2977 off = em->start + em->len;
2978 if (off >= max)
2979 end = 1;
2980
2981 em_start = em->start;
2982 em_len = em->len;
2983
2984 disko = 0;
2985 flags = 0;
2986
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002987 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002988 end = 1;
2989 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002990 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002991 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002992 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002993 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2994 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002995 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002996 flags |= (FIEMAP_EXTENT_DELALLOC |
2997 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002998 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002999 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003000 }
3001 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3002 flags |= FIEMAP_EXTENT_ENCODED;
3003
3004 emflags = em->flags;
3005 free_extent_map(em);
3006 em = NULL;
3007
3008 if (!end) {
3009 em = get_extent(inode, NULL, 0, off, max - off, 0);
3010 if (!em)
3011 goto out;
3012 if (IS_ERR(em)) {
3013 ret = PTR_ERR(em);
3014 goto out;
3015 }
3016 emflags = em->flags;
3017 }
3018 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3019 flags |= FIEMAP_EXTENT_LAST;
3020 end = 1;
3021 }
3022
3023 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3024 em_len, flags);
3025 if (ret)
3026 goto out_free;
3027 }
3028out_free:
3029 free_extent_map(em);
3030out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003031 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3032 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003033 return ret;
3034}
3035
Chris Masond1310b22008-01-24 16:13:08 -05003036static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3037 unsigned long i)
3038{
3039 struct page *p;
3040 struct address_space *mapping;
3041
3042 if (i == 0)
3043 return eb->first_page;
3044 i += eb->start >> PAGE_CACHE_SHIFT;
3045 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003046 if (!mapping)
3047 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003048
3049 /*
3050 * extent_buffer_page is only called after pinning the page
3051 * by increasing the reference count. So we know the page must
3052 * be in the radix tree.
3053 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003054 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003055 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003056 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003057
Chris Masond1310b22008-01-24 16:13:08 -05003058 return p;
3059}
3060
Chris Mason6af118ce2008-07-22 11:18:07 -04003061static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003062{
Chris Mason6af118ce2008-07-22 11:18:07 -04003063 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3064 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003065}
3066
Chris Masond1310b22008-01-24 16:13:08 -05003067static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3068 u64 start,
3069 unsigned long len,
3070 gfp_t mask)
3071{
3072 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003073#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003074 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003075#endif
Chris Masond1310b22008-01-24 16:13:08 -05003076
Chris Masond1310b22008-01-24 16:13:08 -05003077 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003078 eb->start = start;
3079 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003080 spin_lock_init(&eb->lock);
3081 init_waitqueue_head(&eb->lock_wq);
3082
Chris Mason39351272009-02-04 09:24:05 -05003083#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003084 spin_lock_irqsave(&leak_lock, flags);
3085 list_add(&eb->leak_list, &buffers);
3086 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003087#endif
Chris Masond1310b22008-01-24 16:13:08 -05003088 atomic_set(&eb->refs, 1);
3089
3090 return eb;
3091}
3092
3093static void __free_extent_buffer(struct extent_buffer *eb)
3094{
Chris Mason39351272009-02-04 09:24:05 -05003095#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003096 unsigned long flags;
3097 spin_lock_irqsave(&leak_lock, flags);
3098 list_del(&eb->leak_list);
3099 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003100#endif
Chris Masond1310b22008-01-24 16:13:08 -05003101 kmem_cache_free(extent_buffer_cache, eb);
3102}
3103
3104struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3105 u64 start, unsigned long len,
3106 struct page *page0,
3107 gfp_t mask)
3108{
3109 unsigned long num_pages = num_extent_pages(start, len);
3110 unsigned long i;
3111 unsigned long index = start >> PAGE_CACHE_SHIFT;
3112 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003113 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003114 struct page *p;
3115 struct address_space *mapping = tree->mapping;
3116 int uptodate = 1;
3117
Chris Mason6af118ce2008-07-22 11:18:07 -04003118 spin_lock(&tree->buffer_lock);
3119 eb = buffer_search(tree, start);
3120 if (eb) {
3121 atomic_inc(&eb->refs);
3122 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003123 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003124 return eb;
3125 }
3126 spin_unlock(&tree->buffer_lock);
3127
Chris Masond1310b22008-01-24 16:13:08 -05003128 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003129 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003130 return NULL;
3131
Chris Masond1310b22008-01-24 16:13:08 -05003132 if (page0) {
3133 eb->first_page = page0;
3134 i = 1;
3135 index++;
3136 page_cache_get(page0);
3137 mark_page_accessed(page0);
3138 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003139 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003140 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003141 } else {
3142 i = 0;
3143 }
3144 for (; i < num_pages; i++, index++) {
3145 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3146 if (!p) {
3147 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003148 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003149 }
3150 set_page_extent_mapped(p);
3151 mark_page_accessed(p);
3152 if (i == 0) {
3153 eb->first_page = p;
3154 set_page_extent_head(p, len);
3155 } else {
3156 set_page_private(p, EXTENT_PAGE_PRIVATE);
3157 }
3158 if (!PageUptodate(p))
3159 uptodate = 0;
3160 unlock_page(p);
3161 }
3162 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003163 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003164
Chris Mason6af118ce2008-07-22 11:18:07 -04003165 spin_lock(&tree->buffer_lock);
3166 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3167 if (exists) {
3168 /* add one reference for the caller */
3169 atomic_inc(&exists->refs);
3170 spin_unlock(&tree->buffer_lock);
3171 goto free_eb;
3172 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003173 /* add one reference for the tree */
3174 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003175 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003176 return eb;
3177
Chris Mason6af118ce2008-07-22 11:18:07 -04003178free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003179 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003180 return exists;
3181 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003182 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003183 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003184 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003185 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003186}
Chris Masond1310b22008-01-24 16:13:08 -05003187
3188struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3189 u64 start, unsigned long len,
3190 gfp_t mask)
3191{
Chris Masond1310b22008-01-24 16:13:08 -05003192 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003193
Chris Mason6af118ce2008-07-22 11:18:07 -04003194 spin_lock(&tree->buffer_lock);
3195 eb = buffer_search(tree, start);
3196 if (eb)
3197 atomic_inc(&eb->refs);
3198 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003199
Josef Bacik0f9dd462008-09-23 13:14:11 -04003200 if (eb)
3201 mark_page_accessed(eb->first_page);
3202
Chris Masond1310b22008-01-24 16:13:08 -05003203 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003204}
Chris Masond1310b22008-01-24 16:13:08 -05003205
3206void free_extent_buffer(struct extent_buffer *eb)
3207{
Chris Masond1310b22008-01-24 16:13:08 -05003208 if (!eb)
3209 return;
3210
3211 if (!atomic_dec_and_test(&eb->refs))
3212 return;
3213
Chris Mason6af118ce2008-07-22 11:18:07 -04003214 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003215}
Chris Masond1310b22008-01-24 16:13:08 -05003216
3217int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3218 struct extent_buffer *eb)
3219{
Chris Masond1310b22008-01-24 16:13:08 -05003220 unsigned long i;
3221 unsigned long num_pages;
3222 struct page *page;
3223
Chris Masond1310b22008-01-24 16:13:08 -05003224 num_pages = num_extent_pages(eb->start, eb->len);
3225
3226 for (i = 0; i < num_pages; i++) {
3227 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003228 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003229 continue;
3230
Chris Masona61e6f22008-07-22 11:18:08 -04003231 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003232 if (i == 0)
3233 set_page_extent_head(page, eb->len);
3234 else
3235 set_page_private(page, EXTENT_PAGE_PRIVATE);
3236
Chris Masond1310b22008-01-24 16:13:08 -05003237 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003238 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003239 if (!PageDirty(page)) {
3240 radix_tree_tag_clear(&page->mapping->page_tree,
3241 page_index(page),
3242 PAGECACHE_TAG_DIRTY);
3243 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003244 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003245 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003246 }
3247 return 0;
3248}
Chris Masond1310b22008-01-24 16:13:08 -05003249
3250int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3251 struct extent_buffer *eb)
3252{
3253 return wait_on_extent_writeback(tree, eb->start,
3254 eb->start + eb->len - 1);
3255}
Chris Masond1310b22008-01-24 16:13:08 -05003256
3257int set_extent_buffer_dirty(struct extent_io_tree *tree,
3258 struct extent_buffer *eb)
3259{
3260 unsigned long i;
3261 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003262 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003263
Chris Masonb9473432009-03-13 11:00:37 -04003264 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003265 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003266 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003267 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003268 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003269}
Chris Masond1310b22008-01-24 16:13:08 -05003270
Chris Mason1259ab72008-05-12 13:39:03 -04003271int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003272 struct extent_buffer *eb,
3273 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003274{
3275 unsigned long i;
3276 struct page *page;
3277 unsigned long num_pages;
3278
3279 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003280 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003281
3282 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003283 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003284 for (i = 0; i < num_pages; i++) {
3285 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003286 if (page)
3287 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003288 }
3289 return 0;
3290}
3291
Chris Masond1310b22008-01-24 16:13:08 -05003292int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3293 struct extent_buffer *eb)
3294{
3295 unsigned long i;
3296 struct page *page;
3297 unsigned long num_pages;
3298
3299 num_pages = num_extent_pages(eb->start, eb->len);
3300
3301 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3302 GFP_NOFS);
3303 for (i = 0; i < num_pages; i++) {
3304 page = extent_buffer_page(eb, i);
3305 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3306 ((i == num_pages - 1) &&
3307 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3308 check_page_uptodate(tree, page);
3309 continue;
3310 }
3311 SetPageUptodate(page);
3312 }
3313 return 0;
3314}
Chris Masond1310b22008-01-24 16:13:08 -05003315
Chris Masonce9adaa2008-04-09 16:28:12 -04003316int extent_range_uptodate(struct extent_io_tree *tree,
3317 u64 start, u64 end)
3318{
3319 struct page *page;
3320 int ret;
3321 int pg_uptodate = 1;
3322 int uptodate;
3323 unsigned long index;
3324
Chris Mason9655d292009-09-02 15:22:30 -04003325 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003326 if (ret)
3327 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003328 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003329 index = start >> PAGE_CACHE_SHIFT;
3330 page = find_get_page(tree->mapping, index);
3331 uptodate = PageUptodate(page);
3332 page_cache_release(page);
3333 if (!uptodate) {
3334 pg_uptodate = 0;
3335 break;
3336 }
3337 start += PAGE_CACHE_SIZE;
3338 }
3339 return pg_uptodate;
3340}
3341
Chris Masond1310b22008-01-24 16:13:08 -05003342int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003343 struct extent_buffer *eb,
3344 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003345{
Chris Mason728131d2008-04-09 16:28:12 -04003346 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003347 unsigned long num_pages;
3348 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003349 struct page *page;
3350 int pg_uptodate = 1;
3351
Chris Masonb4ce94d2009-02-04 09:25:08 -05003352 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003353 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003354
Chris Mason42352982008-04-28 16:40:52 -04003355 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003356 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003357 if (ret)
3358 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003359
3360 num_pages = num_extent_pages(eb->start, eb->len);
3361 for (i = 0; i < num_pages; i++) {
3362 page = extent_buffer_page(eb, i);
3363 if (!PageUptodate(page)) {
3364 pg_uptodate = 0;
3365 break;
3366 }
3367 }
Chris Mason42352982008-04-28 16:40:52 -04003368 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003369}
Chris Masond1310b22008-01-24 16:13:08 -05003370
3371int read_extent_buffer_pages(struct extent_io_tree *tree,
3372 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003373 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003374 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003375{
3376 unsigned long i;
3377 unsigned long start_i;
3378 struct page *page;
3379 int err;
3380 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003381 int locked_pages = 0;
3382 int all_uptodate = 1;
3383 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003384 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003385 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003386 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003387
Chris Masonb4ce94d2009-02-04 09:25:08 -05003388 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003389 return 0;
3390
Chris Masonce9adaa2008-04-09 16:28:12 -04003391 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003392 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003393 return 0;
3394 }
3395
3396 if (start) {
3397 WARN_ON(start < eb->start);
3398 start_i = (start >> PAGE_CACHE_SHIFT) -
3399 (eb->start >> PAGE_CACHE_SHIFT);
3400 } else {
3401 start_i = 0;
3402 }
3403
3404 num_pages = num_extent_pages(eb->start, eb->len);
3405 for (i = start_i; i < num_pages; i++) {
3406 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003407 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003408 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003409 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003410 } else {
3411 lock_page(page);
3412 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003413 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003414 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003415 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003416 }
3417 if (all_uptodate) {
3418 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003419 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003420 goto unlock_exit;
3421 }
3422
3423 for (i = start_i; i < num_pages; i++) {
3424 page = extent_buffer_page(eb, i);
3425 if (inc_all_pages)
3426 page_cache_get(page);
3427 if (!PageUptodate(page)) {
3428 if (start_i == 0)
3429 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003430 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003431 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003432 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003433 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003434 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003435 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003436 } else {
3437 unlock_page(page);
3438 }
3439 }
3440
Chris Masona86c12c2008-02-07 10:50:54 -05003441 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003442 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003443
Chris Masond3977122009-01-05 21:25:51 -05003444 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003445 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003446
Chris Masond1310b22008-01-24 16:13:08 -05003447 for (i = start_i; i < num_pages; i++) {
3448 page = extent_buffer_page(eb, i);
3449 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003450 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003451 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003452 }
Chris Masond3977122009-01-05 21:25:51 -05003453
Chris Masond1310b22008-01-24 16:13:08 -05003454 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003455 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003456 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003457
3458unlock_exit:
3459 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003460 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003461 page = extent_buffer_page(eb, i);
3462 i++;
3463 unlock_page(page);
3464 locked_pages--;
3465 }
3466 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003467}
Chris Masond1310b22008-01-24 16:13:08 -05003468
3469void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3470 unsigned long start,
3471 unsigned long len)
3472{
3473 size_t cur;
3474 size_t offset;
3475 struct page *page;
3476 char *kaddr;
3477 char *dst = (char *)dstv;
3478 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3479 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003480
3481 WARN_ON(start > eb->len);
3482 WARN_ON(start + len > eb->start + eb->len);
3483
3484 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3485
Chris Masond3977122009-01-05 21:25:51 -05003486 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003487 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003488
3489 cur = min(len, (PAGE_CACHE_SIZE - offset));
3490 kaddr = kmap_atomic(page, KM_USER1);
3491 memcpy(dst, kaddr + offset, cur);
3492 kunmap_atomic(kaddr, KM_USER1);
3493
3494 dst += cur;
3495 len -= cur;
3496 offset = 0;
3497 i++;
3498 }
3499}
Chris Masond1310b22008-01-24 16:13:08 -05003500
3501int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3502 unsigned long min_len, char **token, char **map,
3503 unsigned long *map_start,
3504 unsigned long *map_len, int km)
3505{
3506 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3507 char *kaddr;
3508 struct page *p;
3509 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3510 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3511 unsigned long end_i = (start_offset + start + min_len - 1) >>
3512 PAGE_CACHE_SHIFT;
3513
3514 if (i != end_i)
3515 return -EINVAL;
3516
3517 if (i == 0) {
3518 offset = start_offset;
3519 *map_start = 0;
3520 } else {
3521 offset = 0;
3522 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3523 }
Chris Masond3977122009-01-05 21:25:51 -05003524
Chris Masond1310b22008-01-24 16:13:08 -05003525 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003526 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3527 "wanted %lu %lu\n", (unsigned long long)eb->start,
3528 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003529 WARN_ON(1);
3530 }
3531
3532 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003533 kaddr = kmap_atomic(p, km);
3534 *token = kaddr;
3535 *map = kaddr + offset;
3536 *map_len = PAGE_CACHE_SIZE - offset;
3537 return 0;
3538}
Chris Masond1310b22008-01-24 16:13:08 -05003539
3540int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3541 unsigned long min_len,
3542 char **token, char **map,
3543 unsigned long *map_start,
3544 unsigned long *map_len, int km)
3545{
3546 int err;
3547 int save = 0;
3548 if (eb->map_token) {
3549 unmap_extent_buffer(eb, eb->map_token, km);
3550 eb->map_token = NULL;
3551 save = 1;
3552 }
3553 err = map_private_extent_buffer(eb, start, min_len, token, map,
3554 map_start, map_len, km);
3555 if (!err && save) {
3556 eb->map_token = *token;
3557 eb->kaddr = *map;
3558 eb->map_start = *map_start;
3559 eb->map_len = *map_len;
3560 }
3561 return err;
3562}
Chris Masond1310b22008-01-24 16:13:08 -05003563
3564void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3565{
3566 kunmap_atomic(token, km);
3567}
Chris Masond1310b22008-01-24 16:13:08 -05003568
3569int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3570 unsigned long start,
3571 unsigned long len)
3572{
3573 size_t cur;
3574 size_t offset;
3575 struct page *page;
3576 char *kaddr;
3577 char *ptr = (char *)ptrv;
3578 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3579 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3580 int ret = 0;
3581
3582 WARN_ON(start > eb->len);
3583 WARN_ON(start + len > eb->start + eb->len);
3584
3585 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3586
Chris Masond3977122009-01-05 21:25:51 -05003587 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003588 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003589
3590 cur = min(len, (PAGE_CACHE_SIZE - offset));
3591
3592 kaddr = kmap_atomic(page, KM_USER0);
3593 ret = memcmp(ptr, kaddr + offset, cur);
3594 kunmap_atomic(kaddr, KM_USER0);
3595 if (ret)
3596 break;
3597
3598 ptr += cur;
3599 len -= cur;
3600 offset = 0;
3601 i++;
3602 }
3603 return ret;
3604}
Chris Masond1310b22008-01-24 16:13:08 -05003605
3606void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3607 unsigned long start, unsigned long len)
3608{
3609 size_t cur;
3610 size_t offset;
3611 struct page *page;
3612 char *kaddr;
3613 char *src = (char *)srcv;
3614 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3615 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3616
3617 WARN_ON(start > eb->len);
3618 WARN_ON(start + len > eb->start + eb->len);
3619
3620 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3621
Chris Masond3977122009-01-05 21:25:51 -05003622 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003623 page = extent_buffer_page(eb, i);
3624 WARN_ON(!PageUptodate(page));
3625
3626 cur = min(len, PAGE_CACHE_SIZE - offset);
3627 kaddr = kmap_atomic(page, KM_USER1);
3628 memcpy(kaddr + offset, src, cur);
3629 kunmap_atomic(kaddr, KM_USER1);
3630
3631 src += cur;
3632 len -= cur;
3633 offset = 0;
3634 i++;
3635 }
3636}
Chris Masond1310b22008-01-24 16:13:08 -05003637
3638void memset_extent_buffer(struct extent_buffer *eb, char c,
3639 unsigned long start, unsigned long len)
3640{
3641 size_t cur;
3642 size_t offset;
3643 struct page *page;
3644 char *kaddr;
3645 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3646 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3647
3648 WARN_ON(start > eb->len);
3649 WARN_ON(start + len > eb->start + eb->len);
3650
3651 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3652
Chris Masond3977122009-01-05 21:25:51 -05003653 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003654 page = extent_buffer_page(eb, i);
3655 WARN_ON(!PageUptodate(page));
3656
3657 cur = min(len, PAGE_CACHE_SIZE - offset);
3658 kaddr = kmap_atomic(page, KM_USER0);
3659 memset(kaddr + offset, c, cur);
3660 kunmap_atomic(kaddr, KM_USER0);
3661
3662 len -= cur;
3663 offset = 0;
3664 i++;
3665 }
3666}
Chris Masond1310b22008-01-24 16:13:08 -05003667
3668void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3669 unsigned long dst_offset, unsigned long src_offset,
3670 unsigned long len)
3671{
3672 u64 dst_len = dst->len;
3673 size_t cur;
3674 size_t offset;
3675 struct page *page;
3676 char *kaddr;
3677 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3678 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3679
3680 WARN_ON(src->len != dst_len);
3681
3682 offset = (start_offset + dst_offset) &
3683 ((unsigned long)PAGE_CACHE_SIZE - 1);
3684
Chris Masond3977122009-01-05 21:25:51 -05003685 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003686 page = extent_buffer_page(dst, i);
3687 WARN_ON(!PageUptodate(page));
3688
3689 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3690
3691 kaddr = kmap_atomic(page, KM_USER0);
3692 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3693 kunmap_atomic(kaddr, KM_USER0);
3694
3695 src_offset += cur;
3696 len -= cur;
3697 offset = 0;
3698 i++;
3699 }
3700}
Chris Masond1310b22008-01-24 16:13:08 -05003701
3702static void move_pages(struct page *dst_page, struct page *src_page,
3703 unsigned long dst_off, unsigned long src_off,
3704 unsigned long len)
3705{
3706 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3707 if (dst_page == src_page) {
3708 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3709 } else {
3710 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3711 char *p = dst_kaddr + dst_off + len;
3712 char *s = src_kaddr + src_off + len;
3713
3714 while (len--)
3715 *--p = *--s;
3716
3717 kunmap_atomic(src_kaddr, KM_USER1);
3718 }
3719 kunmap_atomic(dst_kaddr, KM_USER0);
3720}
3721
3722static void copy_pages(struct page *dst_page, struct page *src_page,
3723 unsigned long dst_off, unsigned long src_off,
3724 unsigned long len)
3725{
3726 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3727 char *src_kaddr;
3728
3729 if (dst_page != src_page)
3730 src_kaddr = kmap_atomic(src_page, KM_USER1);
3731 else
3732 src_kaddr = dst_kaddr;
3733
3734 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3735 kunmap_atomic(dst_kaddr, KM_USER0);
3736 if (dst_page != src_page)
3737 kunmap_atomic(src_kaddr, KM_USER1);
3738}
3739
3740void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3741 unsigned long src_offset, unsigned long len)
3742{
3743 size_t cur;
3744 size_t dst_off_in_page;
3745 size_t src_off_in_page;
3746 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3747 unsigned long dst_i;
3748 unsigned long src_i;
3749
3750 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003751 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3752 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003753 BUG_ON(1);
3754 }
3755 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003756 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3757 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003758 BUG_ON(1);
3759 }
3760
Chris Masond3977122009-01-05 21:25:51 -05003761 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003762 dst_off_in_page = (start_offset + dst_offset) &
3763 ((unsigned long)PAGE_CACHE_SIZE - 1);
3764 src_off_in_page = (start_offset + src_offset) &
3765 ((unsigned long)PAGE_CACHE_SIZE - 1);
3766
3767 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3768 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3769
3770 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3771 src_off_in_page));
3772 cur = min_t(unsigned long, cur,
3773 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3774
3775 copy_pages(extent_buffer_page(dst, dst_i),
3776 extent_buffer_page(dst, src_i),
3777 dst_off_in_page, src_off_in_page, cur);
3778
3779 src_offset += cur;
3780 dst_offset += cur;
3781 len -= cur;
3782 }
3783}
Chris Masond1310b22008-01-24 16:13:08 -05003784
3785void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3786 unsigned long src_offset, unsigned long len)
3787{
3788 size_t cur;
3789 size_t dst_off_in_page;
3790 size_t src_off_in_page;
3791 unsigned long dst_end = dst_offset + len - 1;
3792 unsigned long src_end = src_offset + len - 1;
3793 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3794 unsigned long dst_i;
3795 unsigned long src_i;
3796
3797 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003798 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3799 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003800 BUG_ON(1);
3801 }
3802 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003803 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3804 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003805 BUG_ON(1);
3806 }
3807 if (dst_offset < src_offset) {
3808 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3809 return;
3810 }
Chris Masond3977122009-01-05 21:25:51 -05003811 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003812 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3813 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3814
3815 dst_off_in_page = (start_offset + dst_end) &
3816 ((unsigned long)PAGE_CACHE_SIZE - 1);
3817 src_off_in_page = (start_offset + src_end) &
3818 ((unsigned long)PAGE_CACHE_SIZE - 1);
3819
3820 cur = min_t(unsigned long, len, src_off_in_page + 1);
3821 cur = min(cur, dst_off_in_page + 1);
3822 move_pages(extent_buffer_page(dst, dst_i),
3823 extent_buffer_page(dst, src_i),
3824 dst_off_in_page - cur + 1,
3825 src_off_in_page - cur + 1, cur);
3826
3827 dst_end -= cur;
3828 src_end -= cur;
3829 len -= cur;
3830 }
3831}
Chris Mason6af118ce2008-07-22 11:18:07 -04003832
3833int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3834{
3835 u64 start = page_offset(page);
3836 struct extent_buffer *eb;
3837 int ret = 1;
3838 unsigned long i;
3839 unsigned long num_pages;
3840
3841 spin_lock(&tree->buffer_lock);
3842 eb = buffer_search(tree, start);
3843 if (!eb)
3844 goto out;
3845
3846 if (atomic_read(&eb->refs) > 1) {
3847 ret = 0;
3848 goto out;
3849 }
Chris Masonb9473432009-03-13 11:00:37 -04003850 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3851 ret = 0;
3852 goto out;
3853 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003854 /* at this point we can safely release the extent buffer */
3855 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003856 for (i = 0; i < num_pages; i++)
3857 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003858 rb_erase(&eb->rb_node, &tree->buffer);
3859 __free_extent_buffer(eb);
3860out:
3861 spin_unlock(&tree->buffer_lock);
3862 return ret;
3863}