blob: 0adceb5251678b0766fcac8cb0e35c9163852058 [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 Bacik251792012008-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 Bacik251792012008-10-29 14:49:05 -04001048 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001049 }
Josef Bacik251792012008-10-29 14:49:05 -04001050 return 1;
1051}
Josef Bacik251792012008-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 Mason46562cec2009-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;
2681 struct pagevec pvec;
Chris Masonc8b97812008-10-29 14:49:59 -04002682 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002683
2684 pagevec_init(&pvec, 0);
2685 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2686 struct page *page = list_entry(pages->prev, struct page, lru);
2687
2688 prefetchw(&page->flags);
2689 list_del(&page->lru);
2690 /*
2691 * what we want to do here is call add_to_page_cache_lru,
2692 * but that isn't exported, so we reproduce it here
2693 */
2694 if (!add_to_page_cache(page, mapping,
2695 page->index, GFP_KERNEL)) {
2696
2697 /* open coding of lru_cache_add, also not exported */
2698 page_cache_get(page);
2699 if (!pagevec_add(&pvec, page))
Chris Mason15916de2008-11-19 21:17:22 -05002700 __pagevec_lru_add_file(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002701 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002702 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002703 }
2704 page_cache_release(page);
2705 }
2706 if (pagevec_count(&pvec))
Chris Mason15916de2008-11-19 21:17:22 -05002707 __pagevec_lru_add_file(&pvec);
Chris Masond1310b22008-01-24 16:13:08 -05002708 BUG_ON(!list_empty(pages));
2709 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002710 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002711 return 0;
2712}
Chris Masond1310b22008-01-24 16:13:08 -05002713
2714/*
2715 * basic invalidatepage code, this waits on any locked or writeback
2716 * ranges corresponding to the page, and then deletes any extent state
2717 * records from the tree
2718 */
2719int extent_invalidatepage(struct extent_io_tree *tree,
2720 struct page *page, unsigned long offset)
2721{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002722 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002723 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2724 u64 end = start + PAGE_CACHE_SIZE - 1;
2725 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2726
Chris Masond3977122009-01-05 21:25:51 -05002727 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002728 if (start > end)
2729 return 0;
2730
Josef Bacik2ac55d42010-02-03 19:33:23 +00002731 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002732 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002733 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002734 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2735 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002736 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002737 return 0;
2738}
Chris Masond1310b22008-01-24 16:13:08 -05002739
2740/*
2741 * simple commit_write call, set_range_dirty is used to mark both
2742 * the pages and the extent records as dirty
2743 */
2744int extent_commit_write(struct extent_io_tree *tree,
2745 struct inode *inode, struct page *page,
2746 unsigned from, unsigned to)
2747{
2748 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2749
2750 set_page_extent_mapped(page);
2751 set_page_dirty(page);
2752
2753 if (pos > inode->i_size) {
2754 i_size_write(inode, pos);
2755 mark_inode_dirty(inode);
2756 }
2757 return 0;
2758}
Chris Masond1310b22008-01-24 16:13:08 -05002759
2760int extent_prepare_write(struct extent_io_tree *tree,
2761 struct inode *inode, struct page *page,
2762 unsigned from, unsigned to, get_extent_t *get_extent)
2763{
2764 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2765 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2766 u64 block_start;
2767 u64 orig_block_start;
2768 u64 block_end;
2769 u64 cur_end;
2770 struct extent_map *em;
2771 unsigned blocksize = 1 << inode->i_blkbits;
2772 size_t page_offset = 0;
2773 size_t block_off_start;
2774 size_t block_off_end;
2775 int err = 0;
2776 int iocount = 0;
2777 int ret = 0;
2778 int isnew;
2779
2780 set_page_extent_mapped(page);
2781
2782 block_start = (page_start + from) & ~((u64)blocksize - 1);
2783 block_end = (page_start + to - 1) | (blocksize - 1);
2784 orig_block_start = block_start;
2785
2786 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002787 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002788 em = get_extent(inode, page, page_offset, block_start,
2789 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002790 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002791 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002792
Chris Masond1310b22008-01-24 16:13:08 -05002793 cur_end = min(block_end, extent_map_end(em) - 1);
2794 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2795 block_off_end = block_off_start + blocksize;
2796 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2797
2798 if (!PageUptodate(page) && isnew &&
2799 (block_off_end > to || block_off_start < from)) {
2800 void *kaddr;
2801
2802 kaddr = kmap_atomic(page, KM_USER0);
2803 if (block_off_end > to)
2804 memset(kaddr + to, 0, block_off_end - to);
2805 if (block_off_start < from)
2806 memset(kaddr + block_off_start, 0,
2807 from - block_off_start);
2808 flush_dcache_page(page);
2809 kunmap_atomic(kaddr, KM_USER0);
2810 }
2811 if ((em->block_start != EXTENT_MAP_HOLE &&
2812 em->block_start != EXTENT_MAP_INLINE) &&
2813 !isnew && !PageUptodate(page) &&
2814 (block_off_end > to || block_off_start < from) &&
2815 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002816 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002817 u64 sector;
2818 u64 extent_offset = block_start - em->start;
2819 size_t iosize;
2820 sector = (em->block_start + extent_offset) >> 9;
2821 iosize = (cur_end - block_start + blocksize) &
2822 ~((u64)blocksize - 1);
2823 /*
2824 * we've already got the extent locked, but we
2825 * need to split the state such that our end_bio
2826 * handler can clear the lock.
2827 */
2828 set_extent_bit(tree, block_start,
2829 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002830 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002831 ret = submit_extent_page(READ, tree, page,
2832 sector, iosize, page_offset, em->bdev,
2833 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002834 end_bio_extent_preparewrite, 0,
2835 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002836 iocount++;
2837 block_start = block_start + iosize;
2838 } else {
2839 set_extent_uptodate(tree, block_start, cur_end,
2840 GFP_NOFS);
2841 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2842 block_start = cur_end + 1;
2843 }
2844 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2845 free_extent_map(em);
2846 }
2847 if (iocount) {
2848 wait_extent_bit(tree, orig_block_start,
2849 block_end, EXTENT_LOCKED);
2850 }
2851 check_page_uptodate(tree, page);
2852err:
2853 /* FIXME, zero out newly allocated blocks on error */
2854 return err;
2855}
Chris Masond1310b22008-01-24 16:13:08 -05002856
2857/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002858 * a helper for releasepage, this tests for areas of the page that
2859 * are locked or under IO and drops the related state bits if it is safe
2860 * to drop the page.
2861 */
2862int try_release_extent_state(struct extent_map_tree *map,
2863 struct extent_io_tree *tree, struct page *page,
2864 gfp_t mask)
2865{
2866 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2867 u64 end = start + PAGE_CACHE_SIZE - 1;
2868 int ret = 1;
2869
Chris Mason211f90e2008-07-18 11:56:15 -04002870 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002871 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002872 ret = 0;
2873 else {
2874 if ((mask & GFP_NOFS) == GFP_NOFS)
2875 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002876 /*
2877 * at this point we can safely clear everything except the
2878 * locked bit and the nodatasum bit
2879 */
2880 clear_extent_bit(tree, start, end,
2881 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2882 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002883 }
2884 return ret;
2885}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002886
2887/*
Chris Masond1310b22008-01-24 16:13:08 -05002888 * a helper for releasepage. As long as there are no locked extents
2889 * in the range corresponding to the page, both state records and extent
2890 * map records are removed
2891 */
2892int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002893 struct extent_io_tree *tree, struct page *page,
2894 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002895{
2896 struct extent_map *em;
2897 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2898 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002899
Chris Mason70dec802008-01-29 09:59:12 -05002900 if ((mask & __GFP_WAIT) &&
2901 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002902 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002903 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002904 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002905 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002906 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002907 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002908 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002909 break;
2910 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002911 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2912 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002913 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002914 free_extent_map(em);
2915 break;
2916 }
2917 if (!test_range_bit(tree, em->start,
2918 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002919 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002920 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002921 remove_extent_mapping(map, em);
2922 /* once for the rb tree */
2923 free_extent_map(em);
2924 }
2925 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002926 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002927
2928 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002929 free_extent_map(em);
2930 }
Chris Masond1310b22008-01-24 16:13:08 -05002931 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002932 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002933}
Chris Masond1310b22008-01-24 16:13:08 -05002934
2935sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2936 get_extent_t *get_extent)
2937{
2938 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002939 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002940 u64 start = iblock << inode->i_blkbits;
2941 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002942 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002943 struct extent_map *em;
2944
Josef Bacik2ac55d42010-02-03 19:33:23 +00002945 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2946 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002947 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002948 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2949 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002950 if (!em || IS_ERR(em))
2951 return 0;
2952
Yan Zhengd899e052008-10-30 14:25:28 -04002953 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002954 goto out;
2955
2956 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002957out:
2958 free_extent_map(em);
2959 return sector;
2960}
2961
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002962int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2963 __u64 start, __u64 len, get_extent_t *get_extent)
2964{
2965 int ret;
2966 u64 off = start;
2967 u64 max = start + len;
2968 u32 flags = 0;
2969 u64 disko = 0;
2970 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002971 struct extent_state *cached_state = NULL;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002972 int end = 0;
2973 u64 em_start = 0, em_len = 0;
2974 unsigned long emflags;
2975 ret = 0;
2976
2977 if (len == 0)
2978 return -EINVAL;
2979
Josef Bacik2ac55d42010-02-03 19:33:23 +00002980 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2981 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002982 em = get_extent(inode, NULL, 0, off, max - off, 0);
2983 if (!em)
2984 goto out;
2985 if (IS_ERR(em)) {
2986 ret = PTR_ERR(em);
2987 goto out;
2988 }
2989 while (!end) {
2990 off = em->start + em->len;
2991 if (off >= max)
2992 end = 1;
2993
2994 em_start = em->start;
2995 em_len = em->len;
2996
2997 disko = 0;
2998 flags = 0;
2999
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003000 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003001 end = 1;
3002 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003003 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003004 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003005 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003006 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3007 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003008 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003009 flags |= (FIEMAP_EXTENT_DELALLOC |
3010 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003011 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003012 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003013 }
3014 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3015 flags |= FIEMAP_EXTENT_ENCODED;
3016
3017 emflags = em->flags;
3018 free_extent_map(em);
3019 em = NULL;
3020
3021 if (!end) {
3022 em = get_extent(inode, NULL, 0, off, max - off, 0);
3023 if (!em)
3024 goto out;
3025 if (IS_ERR(em)) {
3026 ret = PTR_ERR(em);
3027 goto out;
3028 }
3029 emflags = em->flags;
3030 }
3031 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3032 flags |= FIEMAP_EXTENT_LAST;
3033 end = 1;
3034 }
3035
3036 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3037 em_len, flags);
3038 if (ret)
3039 goto out_free;
3040 }
3041out_free:
3042 free_extent_map(em);
3043out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003044 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3045 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003046 return ret;
3047}
3048
Chris Masond1310b22008-01-24 16:13:08 -05003049static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3050 unsigned long i)
3051{
3052 struct page *p;
3053 struct address_space *mapping;
3054
3055 if (i == 0)
3056 return eb->first_page;
3057 i += eb->start >> PAGE_CACHE_SHIFT;
3058 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003059 if (!mapping)
3060 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003061
3062 /*
3063 * extent_buffer_page is only called after pinning the page
3064 * by increasing the reference count. So we know the page must
3065 * be in the radix tree.
3066 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003067 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003068 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003069 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003070
Chris Masond1310b22008-01-24 16:13:08 -05003071 return p;
3072}
3073
Chris Mason6af118ce2008-07-22 11:18:07 -04003074static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003075{
Chris Mason6af118ce2008-07-22 11:18:07 -04003076 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3077 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003078}
3079
Chris Masond1310b22008-01-24 16:13:08 -05003080static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3081 u64 start,
3082 unsigned long len,
3083 gfp_t mask)
3084{
3085 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003086#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003087 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003088#endif
Chris Masond1310b22008-01-24 16:13:08 -05003089
Chris Masond1310b22008-01-24 16:13:08 -05003090 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003091 eb->start = start;
3092 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003093 spin_lock_init(&eb->lock);
3094 init_waitqueue_head(&eb->lock_wq);
3095
Chris Mason39351272009-02-04 09:24:05 -05003096#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003097 spin_lock_irqsave(&leak_lock, flags);
3098 list_add(&eb->leak_list, &buffers);
3099 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003100#endif
Chris Masond1310b22008-01-24 16:13:08 -05003101 atomic_set(&eb->refs, 1);
3102
3103 return eb;
3104}
3105
3106static void __free_extent_buffer(struct extent_buffer *eb)
3107{
Chris Mason39351272009-02-04 09:24:05 -05003108#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003109 unsigned long flags;
3110 spin_lock_irqsave(&leak_lock, flags);
3111 list_del(&eb->leak_list);
3112 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003113#endif
Chris Masond1310b22008-01-24 16:13:08 -05003114 kmem_cache_free(extent_buffer_cache, eb);
3115}
3116
3117struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3118 u64 start, unsigned long len,
3119 struct page *page0,
3120 gfp_t mask)
3121{
3122 unsigned long num_pages = num_extent_pages(start, len);
3123 unsigned long i;
3124 unsigned long index = start >> PAGE_CACHE_SHIFT;
3125 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003126 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003127 struct page *p;
3128 struct address_space *mapping = tree->mapping;
3129 int uptodate = 1;
3130
Chris Mason6af118ce2008-07-22 11:18:07 -04003131 spin_lock(&tree->buffer_lock);
3132 eb = buffer_search(tree, start);
3133 if (eb) {
3134 atomic_inc(&eb->refs);
3135 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003136 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003137 return eb;
3138 }
3139 spin_unlock(&tree->buffer_lock);
3140
Chris Masond1310b22008-01-24 16:13:08 -05003141 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003142 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003143 return NULL;
3144
Chris Masond1310b22008-01-24 16:13:08 -05003145 if (page0) {
3146 eb->first_page = page0;
3147 i = 1;
3148 index++;
3149 page_cache_get(page0);
3150 mark_page_accessed(page0);
3151 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003152 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003153 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003154 } else {
3155 i = 0;
3156 }
3157 for (; i < num_pages; i++, index++) {
3158 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3159 if (!p) {
3160 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003161 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003162 }
3163 set_page_extent_mapped(p);
3164 mark_page_accessed(p);
3165 if (i == 0) {
3166 eb->first_page = p;
3167 set_page_extent_head(p, len);
3168 } else {
3169 set_page_private(p, EXTENT_PAGE_PRIVATE);
3170 }
3171 if (!PageUptodate(p))
3172 uptodate = 0;
3173 unlock_page(p);
3174 }
3175 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003176 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003177
Chris Mason6af118ce2008-07-22 11:18:07 -04003178 spin_lock(&tree->buffer_lock);
3179 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3180 if (exists) {
3181 /* add one reference for the caller */
3182 atomic_inc(&exists->refs);
3183 spin_unlock(&tree->buffer_lock);
3184 goto free_eb;
3185 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003186 /* add one reference for the tree */
3187 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003188 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003189 return eb;
3190
Chris Mason6af118ce2008-07-22 11:18:07 -04003191free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003192 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003193 return exists;
3194 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003195 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003196 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003197 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003198 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003199}
Chris Masond1310b22008-01-24 16:13:08 -05003200
3201struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3202 u64 start, unsigned long len,
3203 gfp_t mask)
3204{
Chris Masond1310b22008-01-24 16:13:08 -05003205 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003206
Chris Mason6af118ce2008-07-22 11:18:07 -04003207 spin_lock(&tree->buffer_lock);
3208 eb = buffer_search(tree, start);
3209 if (eb)
3210 atomic_inc(&eb->refs);
3211 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003212
Josef Bacik0f9dd462008-09-23 13:14:11 -04003213 if (eb)
3214 mark_page_accessed(eb->first_page);
3215
Chris Masond1310b22008-01-24 16:13:08 -05003216 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003217}
Chris Masond1310b22008-01-24 16:13:08 -05003218
3219void free_extent_buffer(struct extent_buffer *eb)
3220{
Chris Masond1310b22008-01-24 16:13:08 -05003221 if (!eb)
3222 return;
3223
3224 if (!atomic_dec_and_test(&eb->refs))
3225 return;
3226
Chris Mason6af118ce2008-07-22 11:18:07 -04003227 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003228}
Chris Masond1310b22008-01-24 16:13:08 -05003229
3230int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3231 struct extent_buffer *eb)
3232{
Chris Masond1310b22008-01-24 16:13:08 -05003233 unsigned long i;
3234 unsigned long num_pages;
3235 struct page *page;
3236
Chris Masond1310b22008-01-24 16:13:08 -05003237 num_pages = num_extent_pages(eb->start, eb->len);
3238
3239 for (i = 0; i < num_pages; i++) {
3240 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003241 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003242 continue;
3243
Chris Masona61e6f22008-07-22 11:18:08 -04003244 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003245 if (i == 0)
3246 set_page_extent_head(page, eb->len);
3247 else
3248 set_page_private(page, EXTENT_PAGE_PRIVATE);
3249
Chris Masond1310b22008-01-24 16:13:08 -05003250 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003251 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003252 if (!PageDirty(page)) {
3253 radix_tree_tag_clear(&page->mapping->page_tree,
3254 page_index(page),
3255 PAGECACHE_TAG_DIRTY);
3256 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003257 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003258 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003259 }
3260 return 0;
3261}
Chris Masond1310b22008-01-24 16:13:08 -05003262
3263int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3264 struct extent_buffer *eb)
3265{
3266 return wait_on_extent_writeback(tree, eb->start,
3267 eb->start + eb->len - 1);
3268}
Chris Masond1310b22008-01-24 16:13:08 -05003269
3270int set_extent_buffer_dirty(struct extent_io_tree *tree,
3271 struct extent_buffer *eb)
3272{
3273 unsigned long i;
3274 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003275 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003276
Chris Masonb9473432009-03-13 11:00:37 -04003277 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003278 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003279 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003280 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003281 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003282}
Chris Masond1310b22008-01-24 16:13:08 -05003283
Chris Mason1259ab72008-05-12 13:39:03 -04003284int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003285 struct extent_buffer *eb,
3286 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003287{
3288 unsigned long i;
3289 struct page *page;
3290 unsigned long num_pages;
3291
3292 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003293 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003294
3295 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003296 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003297 for (i = 0; i < num_pages; i++) {
3298 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003299 if (page)
3300 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003301 }
3302 return 0;
3303}
3304
Chris Masond1310b22008-01-24 16:13:08 -05003305int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3306 struct extent_buffer *eb)
3307{
3308 unsigned long i;
3309 struct page *page;
3310 unsigned long num_pages;
3311
3312 num_pages = num_extent_pages(eb->start, eb->len);
3313
3314 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3315 GFP_NOFS);
3316 for (i = 0; i < num_pages; i++) {
3317 page = extent_buffer_page(eb, i);
3318 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3319 ((i == num_pages - 1) &&
3320 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3321 check_page_uptodate(tree, page);
3322 continue;
3323 }
3324 SetPageUptodate(page);
3325 }
3326 return 0;
3327}
Chris Masond1310b22008-01-24 16:13:08 -05003328
Chris Masonce9adaa2008-04-09 16:28:12 -04003329int extent_range_uptodate(struct extent_io_tree *tree,
3330 u64 start, u64 end)
3331{
3332 struct page *page;
3333 int ret;
3334 int pg_uptodate = 1;
3335 int uptodate;
3336 unsigned long index;
3337
Chris Mason9655d292009-09-02 15:22:30 -04003338 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003339 if (ret)
3340 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003341 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003342 index = start >> PAGE_CACHE_SHIFT;
3343 page = find_get_page(tree->mapping, index);
3344 uptodate = PageUptodate(page);
3345 page_cache_release(page);
3346 if (!uptodate) {
3347 pg_uptodate = 0;
3348 break;
3349 }
3350 start += PAGE_CACHE_SIZE;
3351 }
3352 return pg_uptodate;
3353}
3354
Chris Masond1310b22008-01-24 16:13:08 -05003355int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003356 struct extent_buffer *eb,
3357 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003358{
Chris Mason728131d2008-04-09 16:28:12 -04003359 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003360 unsigned long num_pages;
3361 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003362 struct page *page;
3363 int pg_uptodate = 1;
3364
Chris Masonb4ce94d2009-02-04 09:25:08 -05003365 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003366 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003367
Chris Mason42352982008-04-28 16:40:52 -04003368 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003369 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003370 if (ret)
3371 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003372
3373 num_pages = num_extent_pages(eb->start, eb->len);
3374 for (i = 0; i < num_pages; i++) {
3375 page = extent_buffer_page(eb, i);
3376 if (!PageUptodate(page)) {
3377 pg_uptodate = 0;
3378 break;
3379 }
3380 }
Chris Mason42352982008-04-28 16:40:52 -04003381 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003382}
Chris Masond1310b22008-01-24 16:13:08 -05003383
3384int read_extent_buffer_pages(struct extent_io_tree *tree,
3385 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003386 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003387 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003388{
3389 unsigned long i;
3390 unsigned long start_i;
3391 struct page *page;
3392 int err;
3393 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003394 int locked_pages = 0;
3395 int all_uptodate = 1;
3396 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003397 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003398 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003399 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003400
Chris Masonb4ce94d2009-02-04 09:25:08 -05003401 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003402 return 0;
3403
Chris Masonce9adaa2008-04-09 16:28:12 -04003404 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003405 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003406 return 0;
3407 }
3408
3409 if (start) {
3410 WARN_ON(start < eb->start);
3411 start_i = (start >> PAGE_CACHE_SHIFT) -
3412 (eb->start >> PAGE_CACHE_SHIFT);
3413 } else {
3414 start_i = 0;
3415 }
3416
3417 num_pages = num_extent_pages(eb->start, eb->len);
3418 for (i = start_i; i < num_pages; i++) {
3419 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003420 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003421 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003422 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003423 } else {
3424 lock_page(page);
3425 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003426 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003427 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003428 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003429 }
3430 if (all_uptodate) {
3431 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003432 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003433 goto unlock_exit;
3434 }
3435
3436 for (i = start_i; i < num_pages; i++) {
3437 page = extent_buffer_page(eb, i);
3438 if (inc_all_pages)
3439 page_cache_get(page);
3440 if (!PageUptodate(page)) {
3441 if (start_i == 0)
3442 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003443 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003444 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003445 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003446 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003447 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003448 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003449 } else {
3450 unlock_page(page);
3451 }
3452 }
3453
Chris Masona86c12c2008-02-07 10:50:54 -05003454 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003455 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003456
Chris Masond3977122009-01-05 21:25:51 -05003457 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003458 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003459
Chris Masond1310b22008-01-24 16:13:08 -05003460 for (i = start_i; i < num_pages; i++) {
3461 page = extent_buffer_page(eb, i);
3462 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003463 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003464 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003465 }
Chris Masond3977122009-01-05 21:25:51 -05003466
Chris Masond1310b22008-01-24 16:13:08 -05003467 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003468 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003469 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003470
3471unlock_exit:
3472 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003473 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003474 page = extent_buffer_page(eb, i);
3475 i++;
3476 unlock_page(page);
3477 locked_pages--;
3478 }
3479 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003480}
Chris Masond1310b22008-01-24 16:13:08 -05003481
3482void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3483 unsigned long start,
3484 unsigned long len)
3485{
3486 size_t cur;
3487 size_t offset;
3488 struct page *page;
3489 char *kaddr;
3490 char *dst = (char *)dstv;
3491 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3492 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003493
3494 WARN_ON(start > eb->len);
3495 WARN_ON(start + len > eb->start + eb->len);
3496
3497 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3498
Chris Masond3977122009-01-05 21:25:51 -05003499 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003500 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003501
3502 cur = min(len, (PAGE_CACHE_SIZE - offset));
3503 kaddr = kmap_atomic(page, KM_USER1);
3504 memcpy(dst, kaddr + offset, cur);
3505 kunmap_atomic(kaddr, KM_USER1);
3506
3507 dst += cur;
3508 len -= cur;
3509 offset = 0;
3510 i++;
3511 }
3512}
Chris Masond1310b22008-01-24 16:13:08 -05003513
3514int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3515 unsigned long min_len, char **token, char **map,
3516 unsigned long *map_start,
3517 unsigned long *map_len, int km)
3518{
3519 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3520 char *kaddr;
3521 struct page *p;
3522 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3523 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3524 unsigned long end_i = (start_offset + start + min_len - 1) >>
3525 PAGE_CACHE_SHIFT;
3526
3527 if (i != end_i)
3528 return -EINVAL;
3529
3530 if (i == 0) {
3531 offset = start_offset;
3532 *map_start = 0;
3533 } else {
3534 offset = 0;
3535 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3536 }
Chris Masond3977122009-01-05 21:25:51 -05003537
Chris Masond1310b22008-01-24 16:13:08 -05003538 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003539 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3540 "wanted %lu %lu\n", (unsigned long long)eb->start,
3541 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003542 WARN_ON(1);
3543 }
3544
3545 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003546 kaddr = kmap_atomic(p, km);
3547 *token = kaddr;
3548 *map = kaddr + offset;
3549 *map_len = PAGE_CACHE_SIZE - offset;
3550 return 0;
3551}
Chris Masond1310b22008-01-24 16:13:08 -05003552
3553int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3554 unsigned long min_len,
3555 char **token, char **map,
3556 unsigned long *map_start,
3557 unsigned long *map_len, int km)
3558{
3559 int err;
3560 int save = 0;
3561 if (eb->map_token) {
3562 unmap_extent_buffer(eb, eb->map_token, km);
3563 eb->map_token = NULL;
3564 save = 1;
3565 }
3566 err = map_private_extent_buffer(eb, start, min_len, token, map,
3567 map_start, map_len, km);
3568 if (!err && save) {
3569 eb->map_token = *token;
3570 eb->kaddr = *map;
3571 eb->map_start = *map_start;
3572 eb->map_len = *map_len;
3573 }
3574 return err;
3575}
Chris Masond1310b22008-01-24 16:13:08 -05003576
3577void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3578{
3579 kunmap_atomic(token, km);
3580}
Chris Masond1310b22008-01-24 16:13:08 -05003581
3582int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3583 unsigned long start,
3584 unsigned long len)
3585{
3586 size_t cur;
3587 size_t offset;
3588 struct page *page;
3589 char *kaddr;
3590 char *ptr = (char *)ptrv;
3591 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3592 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3593 int ret = 0;
3594
3595 WARN_ON(start > eb->len);
3596 WARN_ON(start + len > eb->start + eb->len);
3597
3598 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3599
Chris Masond3977122009-01-05 21:25:51 -05003600 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003601 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003602
3603 cur = min(len, (PAGE_CACHE_SIZE - offset));
3604
3605 kaddr = kmap_atomic(page, KM_USER0);
3606 ret = memcmp(ptr, kaddr + offset, cur);
3607 kunmap_atomic(kaddr, KM_USER0);
3608 if (ret)
3609 break;
3610
3611 ptr += cur;
3612 len -= cur;
3613 offset = 0;
3614 i++;
3615 }
3616 return ret;
3617}
Chris Masond1310b22008-01-24 16:13:08 -05003618
3619void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3620 unsigned long start, unsigned long len)
3621{
3622 size_t cur;
3623 size_t offset;
3624 struct page *page;
3625 char *kaddr;
3626 char *src = (char *)srcv;
3627 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3628 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3629
3630 WARN_ON(start > eb->len);
3631 WARN_ON(start + len > eb->start + eb->len);
3632
3633 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3634
Chris Masond3977122009-01-05 21:25:51 -05003635 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003636 page = extent_buffer_page(eb, i);
3637 WARN_ON(!PageUptodate(page));
3638
3639 cur = min(len, PAGE_CACHE_SIZE - offset);
3640 kaddr = kmap_atomic(page, KM_USER1);
3641 memcpy(kaddr + offset, src, cur);
3642 kunmap_atomic(kaddr, KM_USER1);
3643
3644 src += cur;
3645 len -= cur;
3646 offset = 0;
3647 i++;
3648 }
3649}
Chris Masond1310b22008-01-24 16:13:08 -05003650
3651void memset_extent_buffer(struct extent_buffer *eb, char c,
3652 unsigned long start, unsigned long len)
3653{
3654 size_t cur;
3655 size_t offset;
3656 struct page *page;
3657 char *kaddr;
3658 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3659 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3660
3661 WARN_ON(start > eb->len);
3662 WARN_ON(start + len > eb->start + eb->len);
3663
3664 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3665
Chris Masond3977122009-01-05 21:25:51 -05003666 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003667 page = extent_buffer_page(eb, i);
3668 WARN_ON(!PageUptodate(page));
3669
3670 cur = min(len, PAGE_CACHE_SIZE - offset);
3671 kaddr = kmap_atomic(page, KM_USER0);
3672 memset(kaddr + offset, c, cur);
3673 kunmap_atomic(kaddr, KM_USER0);
3674
3675 len -= cur;
3676 offset = 0;
3677 i++;
3678 }
3679}
Chris Masond1310b22008-01-24 16:13:08 -05003680
3681void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3682 unsigned long dst_offset, unsigned long src_offset,
3683 unsigned long len)
3684{
3685 u64 dst_len = dst->len;
3686 size_t cur;
3687 size_t offset;
3688 struct page *page;
3689 char *kaddr;
3690 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3691 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3692
3693 WARN_ON(src->len != dst_len);
3694
3695 offset = (start_offset + dst_offset) &
3696 ((unsigned long)PAGE_CACHE_SIZE - 1);
3697
Chris Masond3977122009-01-05 21:25:51 -05003698 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003699 page = extent_buffer_page(dst, i);
3700 WARN_ON(!PageUptodate(page));
3701
3702 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3703
3704 kaddr = kmap_atomic(page, KM_USER0);
3705 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3706 kunmap_atomic(kaddr, KM_USER0);
3707
3708 src_offset += cur;
3709 len -= cur;
3710 offset = 0;
3711 i++;
3712 }
3713}
Chris Masond1310b22008-01-24 16:13:08 -05003714
3715static void move_pages(struct page *dst_page, struct page *src_page,
3716 unsigned long dst_off, unsigned long src_off,
3717 unsigned long len)
3718{
3719 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3720 if (dst_page == src_page) {
3721 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3722 } else {
3723 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3724 char *p = dst_kaddr + dst_off + len;
3725 char *s = src_kaddr + src_off + len;
3726
3727 while (len--)
3728 *--p = *--s;
3729
3730 kunmap_atomic(src_kaddr, KM_USER1);
3731 }
3732 kunmap_atomic(dst_kaddr, KM_USER0);
3733}
3734
3735static void copy_pages(struct page *dst_page, struct page *src_page,
3736 unsigned long dst_off, unsigned long src_off,
3737 unsigned long len)
3738{
3739 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3740 char *src_kaddr;
3741
3742 if (dst_page != src_page)
3743 src_kaddr = kmap_atomic(src_page, KM_USER1);
3744 else
3745 src_kaddr = dst_kaddr;
3746
3747 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3748 kunmap_atomic(dst_kaddr, KM_USER0);
3749 if (dst_page != src_page)
3750 kunmap_atomic(src_kaddr, KM_USER1);
3751}
3752
3753void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3754 unsigned long src_offset, unsigned long len)
3755{
3756 size_t cur;
3757 size_t dst_off_in_page;
3758 size_t src_off_in_page;
3759 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3760 unsigned long dst_i;
3761 unsigned long src_i;
3762
3763 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003764 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3765 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003766 BUG_ON(1);
3767 }
3768 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003769 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3770 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003771 BUG_ON(1);
3772 }
3773
Chris Masond3977122009-01-05 21:25:51 -05003774 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003775 dst_off_in_page = (start_offset + dst_offset) &
3776 ((unsigned long)PAGE_CACHE_SIZE - 1);
3777 src_off_in_page = (start_offset + src_offset) &
3778 ((unsigned long)PAGE_CACHE_SIZE - 1);
3779
3780 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3781 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3782
3783 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3784 src_off_in_page));
3785 cur = min_t(unsigned long, cur,
3786 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3787
3788 copy_pages(extent_buffer_page(dst, dst_i),
3789 extent_buffer_page(dst, src_i),
3790 dst_off_in_page, src_off_in_page, cur);
3791
3792 src_offset += cur;
3793 dst_offset += cur;
3794 len -= cur;
3795 }
3796}
Chris Masond1310b22008-01-24 16:13:08 -05003797
3798void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3799 unsigned long src_offset, unsigned long len)
3800{
3801 size_t cur;
3802 size_t dst_off_in_page;
3803 size_t src_off_in_page;
3804 unsigned long dst_end = dst_offset + len - 1;
3805 unsigned long src_end = src_offset + len - 1;
3806 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3807 unsigned long dst_i;
3808 unsigned long src_i;
3809
3810 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003811 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3812 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003813 BUG_ON(1);
3814 }
3815 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003816 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3817 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003818 BUG_ON(1);
3819 }
3820 if (dst_offset < src_offset) {
3821 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3822 return;
3823 }
Chris Masond3977122009-01-05 21:25:51 -05003824 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003825 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3826 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3827
3828 dst_off_in_page = (start_offset + dst_end) &
3829 ((unsigned long)PAGE_CACHE_SIZE - 1);
3830 src_off_in_page = (start_offset + src_end) &
3831 ((unsigned long)PAGE_CACHE_SIZE - 1);
3832
3833 cur = min_t(unsigned long, len, src_off_in_page + 1);
3834 cur = min(cur, dst_off_in_page + 1);
3835 move_pages(extent_buffer_page(dst, dst_i),
3836 extent_buffer_page(dst, src_i),
3837 dst_off_in_page - cur + 1,
3838 src_off_in_page - cur + 1, cur);
3839
3840 dst_end -= cur;
3841 src_end -= cur;
3842 len -= cur;
3843 }
3844}
Chris Mason6af118ce2008-07-22 11:18:07 -04003845
3846int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3847{
3848 u64 start = page_offset(page);
3849 struct extent_buffer *eb;
3850 int ret = 1;
3851 unsigned long i;
3852 unsigned long num_pages;
3853
3854 spin_lock(&tree->buffer_lock);
3855 eb = buffer_search(tree, start);
3856 if (!eb)
3857 goto out;
3858
3859 if (atomic_read(&eb->refs) > 1) {
3860 ret = 0;
3861 goto out;
3862 }
Chris Masonb9473432009-03-13 11:00:37 -04003863 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3864 ret = 0;
3865 goto out;
3866 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003867 /* at this point we can safely release the extent buffer */
3868 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003869 for (i = 0; i < num_pages; i++)
3870 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003871 rb_erase(&eb->rb_node, &tree->buffer);
3872 __free_extent_buffer(eb);
3873out:
3874 spin_unlock(&tree->buffer_lock);
3875 return ret;
3876}