blob: a53aca338c7f0c8ce6974db242f2106ed98d9172 [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,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400338 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500339{
340 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400341 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400342 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500343 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400344
345 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500346}
347
348static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400349 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500350{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400351 if (tree->ops && tree->ops->clear_bit_hook)
352 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500353}
354
Chris Masond1310b22008-01-24 16:13:08 -0500355/*
356 * insert an extent_state struct into the tree. 'bits' are set on the
357 * struct before it is inserted.
358 *
359 * This may return -EEXIST if the extent is already there, in which case the
360 * state struct is freed.
361 *
362 * The tree lock is not taken internally. This is a utility function and
363 * probably isn't what you want to call (see set/clear_extent_bit).
364 */
365static int insert_state(struct extent_io_tree *tree,
366 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400367 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500368{
369 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400370 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400371 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500372
373 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500374 printk(KERN_ERR "btrfs end < start %llu %llu\n",
375 (unsigned long long)end,
376 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500377 WARN_ON(1);
378 }
Chris Masond1310b22008-01-24 16:13:08 -0500379 state->start = start;
380 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400381 ret = set_state_cb(tree, state, bits);
382 if (ret)
383 return ret;
384
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400385 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400386 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400387 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500388 node = tree_insert(&tree->state, end, &state->rb_node);
389 if (node) {
390 struct extent_state *found;
391 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500392 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
393 "%llu %llu\n", (unsigned long long)found->start,
394 (unsigned long long)found->end,
395 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500396 free_extent_state(state);
397 return -EEXIST;
398 }
Chris Mason70dec802008-01-29 09:59:12 -0500399 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500400 merge_state(tree, state);
401 return 0;
402}
403
Josef Bacik9ed74f22009-09-11 16:12:44 -0400404static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
405 u64 split)
406{
407 if (tree->ops && tree->ops->split_extent_hook)
408 return tree->ops->split_extent_hook(tree->mapping->host,
409 orig, split);
410 return 0;
411}
412
Chris Masond1310b22008-01-24 16:13:08 -0500413/*
414 * split a given extent state struct in two, inserting the preallocated
415 * struct 'prealloc' as the newly created second half. 'split' indicates an
416 * offset inside 'orig' where it should be split.
417 *
418 * Before calling,
419 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
420 * are two extent state structs in the tree:
421 * prealloc: [orig->start, split - 1]
422 * orig: [ split, orig->end ]
423 *
424 * The tree locks are not taken by this function. They need to be held
425 * by the caller.
426 */
427static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
428 struct extent_state *prealloc, u64 split)
429{
430 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400431
432 split_cb(tree, orig, split);
433
Chris Masond1310b22008-01-24 16:13:08 -0500434 prealloc->start = orig->start;
435 prealloc->end = split - 1;
436 prealloc->state = orig->state;
437 orig->start = split;
438
439 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
440 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500441 free_extent_state(prealloc);
442 return -EEXIST;
443 }
Chris Mason70dec802008-01-29 09:59:12 -0500444 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500445 return 0;
446}
447
448/*
449 * utility function to clear some bits in an extent state struct.
450 * it will optionally wake up any one waiting on this state (wake == 1), or
451 * forcibly remove the state from the tree (delete == 1).
452 *
453 * If no bits are set on the state struct after clearing things, the
454 * struct is freed and removed from the tree
455 */
456static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400457 struct extent_state *state,
458 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500459{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400460 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400461 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500462
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400463 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500464 u64 range = state->end - state->start + 1;
465 WARN_ON(range > tree->dirty_bytes);
466 tree->dirty_bytes -= range;
467 }
Chris Mason291d6732008-01-29 15:55:23 -0500468 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400469 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500470 if (wake)
471 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400472 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500473 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500474 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500475 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500476 free_extent_state(state);
477 } else {
478 WARN_ON(1);
479 }
480 } else {
481 merge_state(tree, state);
482 }
483 return ret;
484}
485
486/*
487 * clear some bits on a range in the tree. This may require splitting
488 * or inserting elements in the tree, so the gfp mask is used to
489 * indicate which allocations or sleeping are allowed.
490 *
491 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
492 * the given range from the tree regardless of state (ie for truncate).
493 *
494 * the range [start, end] is inclusive.
495 *
496 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
497 * bits were already set, or zero if none of the bits were already set.
498 */
499int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400500 int bits, int wake, int delete,
501 struct extent_state **cached_state,
502 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500503{
504 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400505 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500506 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400507 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500508 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400509 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500510 int err;
511 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000512 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500513
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400514 if (delete)
515 bits |= ~EXTENT_CTLBITS;
516 bits |= EXTENT_FIRST_DELALLOC;
517
Josef Bacik2ac55d42010-02-03 19:33:23 +0000518 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
519 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500520again:
521 if (!prealloc && (mask & __GFP_WAIT)) {
522 prealloc = alloc_extent_state(mask);
523 if (!prealloc)
524 return -ENOMEM;
525 }
526
Chris Masoncad321a2008-12-17 14:51:42 -0500527 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400528 if (cached_state) {
529 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000530
531 if (clear) {
532 *cached_state = NULL;
533 cached_state = NULL;
534 }
535
Chris Mason42daec22009-09-23 19:51:09 -0400536 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000537 if (clear)
538 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400539 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400540 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400541 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000542 if (clear)
543 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400544 }
Chris Masond1310b22008-01-24 16:13:08 -0500545 /*
546 * this search will find the extents that end after
547 * our range starts
548 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500549 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500550 if (!node)
551 goto out;
552 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400553hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500554 if (state->start > end)
555 goto out;
556 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400557 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500558
559 /*
560 * | ---- desired range ---- |
561 * | state | or
562 * | ------------- state -------------- |
563 *
564 * We need to split the extent we found, and may flip
565 * bits on second half.
566 *
567 * If the extent we found extends past our range, we
568 * just split and search again. It'll get split again
569 * the next time though.
570 *
571 * If the extent we found is inside our range, we clear
572 * the desired bit on it.
573 */
574
575 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500576 if (!prealloc)
577 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500578 err = split_state(tree, state, prealloc, start);
579 BUG_ON(err == -EEXIST);
580 prealloc = NULL;
581 if (err)
582 goto out;
583 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400584 set |= clear_state_bit(tree, state, &bits, wake);
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
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400605 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400606
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
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400616 set |= clear_state_bit(tree, state, &bits, wake);
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,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400709 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500710{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400711 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400712 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400713
714 ret = set_state_cb(tree, state, bits);
715 if (ret)
716 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400717 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500718 u64 range = state->end - state->start + 1;
719 tree->dirty_bytes += range;
720 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400721 state->state |= bits_to_set;
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
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400760 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500761again:
762 if (!prealloc && (mask & __GFP_WAIT)) {
763 prealloc = alloc_extent_state(mask);
764 if (!prealloc)
765 return -ENOMEM;
766 }
767
Chris Masoncad321a2008-12-17 14:51:42 -0500768 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400769 if (cached_state && *cached_state) {
770 state = *cached_state;
771 if (state->start == start && state->tree) {
772 node = &state->rb_node;
773 goto hit_next;
774 }
775 }
Chris Masond1310b22008-01-24 16:13:08 -0500776 /*
777 * this search will find all the extents that end after
778 * our range starts.
779 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500780 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500781 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400782 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500783 prealloc = NULL;
784 BUG_ON(err == -EEXIST);
785 goto out;
786 }
Chris Masond1310b22008-01-24 16:13:08 -0500787 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400788hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500789 last_start = state->start;
790 last_end = state->end;
791
792 /*
793 * | ---- desired range ---- |
794 * | state |
795 *
796 * Just lock what we found and keep going
797 */
798 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400799 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400800 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500801 *failed_start = state->start;
802 err = -EEXIST;
803 goto out;
804 }
Chris Mason42daec22009-09-23 19:51:09 -0400805
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400806 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400807 if (err)
808 goto out;
809
Chris Mason2c64c532009-09-02 15:04:12 -0400810 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500811 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400812 if (last_end == (u64)-1)
813 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400814
Yan Zheng5c939df2009-05-27 09:16:03 -0400815 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400816 if (start < end && prealloc && !need_resched()) {
817 next_node = rb_next(node);
818 if (next_node) {
819 state = rb_entry(next_node, struct extent_state,
820 rb_node);
821 if (state->start == start)
822 goto hit_next;
823 }
824 }
Chris Masond1310b22008-01-24 16:13:08 -0500825 goto search_again;
826 }
827
828 /*
829 * | ---- desired range ---- |
830 * | state |
831 * or
832 * | ------------- state -------------- |
833 *
834 * We need to split the extent we found, and may flip bits on
835 * second half.
836 *
837 * If the extent we found extends past our
838 * range, we just split and search again. It'll get split
839 * again the next time though.
840 *
841 * If the extent we found is inside our range, we set the
842 * desired bit on it.
843 */
844 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400845 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500846 *failed_start = start;
847 err = -EEXIST;
848 goto out;
849 }
850 err = split_state(tree, state, prealloc, start);
851 BUG_ON(err == -EEXIST);
852 prealloc = NULL;
853 if (err)
854 goto out;
855 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400856 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400857 if (err)
858 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400859 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500860 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400861 if (last_end == (u64)-1)
862 goto out;
863 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500864 }
865 goto search_again;
866 }
867 /*
868 * | ---- desired range ---- |
869 * | state | or | state |
870 *
871 * There's a hole, we need to insert something in it and
872 * ignore the extent we found.
873 */
874 if (state->start > start) {
875 u64 this_end;
876 if (end < last_start)
877 this_end = end;
878 else
Chris Masond3977122009-01-05 21:25:51 -0500879 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500880 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400881 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400882 BUG_ON(err == -EEXIST);
883 if (err) {
884 prealloc = NULL;
885 goto out;
886 }
Chris Mason2c64c532009-09-02 15:04:12 -0400887 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500888 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500889 start = this_end + 1;
890 goto search_again;
891 }
892 /*
893 * | ---- desired range ---- |
894 * | state |
895 * We need to split the extent, and set the bit
896 * on the first half
897 */
898 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400899 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500900 *failed_start = start;
901 err = -EEXIST;
902 goto out;
903 }
904 err = split_state(tree, state, prealloc, end + 1);
905 BUG_ON(err == -EEXIST);
906
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400907 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400908 if (err) {
909 prealloc = NULL;
910 goto out;
911 }
Chris Mason2c64c532009-09-02 15:04:12 -0400912 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500913 merge_state(tree, prealloc);
914 prealloc = NULL;
915 goto out;
916 }
917
918 goto search_again;
919
920out:
Chris Masoncad321a2008-12-17 14:51:42 -0500921 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500922 if (prealloc)
923 free_extent_state(prealloc);
924
925 return err;
926
927search_again:
928 if (start > end)
929 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500930 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500931 if (mask & __GFP_WAIT)
932 cond_resched();
933 goto again;
934}
Chris Masond1310b22008-01-24 16:13:08 -0500935
936/* wrappers around set/clear extent bit */
937int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
938 gfp_t mask)
939{
940 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400941 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500942}
Chris Masond1310b22008-01-24 16:13:08 -0500943
944int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
945 int bits, gfp_t mask)
946{
947 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400948 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500949}
Chris Masond1310b22008-01-24 16:13:08 -0500950
951int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
952 int bits, gfp_t mask)
953{
Chris Mason2c64c532009-09-02 15:04:12 -0400954 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500955}
Chris Masond1310b22008-01-24 16:13:08 -0500956
957int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000958 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500959{
960 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400961 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000962 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500963}
Chris Masond1310b22008-01-24 16:13:08 -0500964
965int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
966 gfp_t mask)
967{
968 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400969 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400970 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500971}
Chris Masond1310b22008-01-24 16:13:08 -0500972
973int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
974 gfp_t mask)
975{
976 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400977 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500978}
Chris Masond1310b22008-01-24 16:13:08 -0500979
Christoph Hellwigb2950862008-12-02 09:54:17 -0500980static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500981 gfp_t mask)
982{
Chris Mason2c64c532009-09-02 15:04:12 -0400983 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
984 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500985}
Chris Masond1310b22008-01-24 16:13:08 -0500986
987int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
988 gfp_t mask)
989{
990 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400991 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500992}
Chris Masond1310b22008-01-24 16:13:08 -0500993
Chris Masond3977122009-01-05 21:25:51 -0500994static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000995 u64 end, struct extent_state **cached_state,
996 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500997{
Chris Mason2c64c532009-09-02 15:04:12 -0400998 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000999 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001000}
Chris Masond1310b22008-01-24 16:13:08 -05001001
Chris Masond1310b22008-01-24 16:13:08 -05001002int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1003{
1004 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
1005}
Chris Masond1310b22008-01-24 16:13:08 -05001006
Chris Masond352ac62008-09-29 15:18:18 -04001007/*
1008 * either insert or lock state struct between start and end use mask to tell
1009 * us if waiting is desired.
1010 */
Chris Mason1edbb732009-09-02 13:24:36 -04001011int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001012 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001013{
1014 int err;
1015 u64 failed_start;
1016 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001017 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001018 EXTENT_LOCKED, &failed_start,
1019 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001020 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1021 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1022 start = failed_start;
1023 } else {
1024 break;
1025 }
1026 WARN_ON(start > end);
1027 }
1028 return err;
1029}
Chris Masond1310b22008-01-24 16:13:08 -05001030
Chris Mason1edbb732009-09-02 13:24:36 -04001031int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1032{
Chris Mason2c64c532009-09-02 15:04:12 -04001033 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001034}
1035
Josef Bacik25179202008-10-29 14:49:05 -04001036int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1037 gfp_t mask)
1038{
1039 int err;
1040 u64 failed_start;
1041
Chris Mason2c64c532009-09-02 15:04:12 -04001042 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1043 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001044 if (err == -EEXIST) {
1045 if (failed_start > start)
1046 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001047 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001048 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001049 }
Josef Bacik25179202008-10-29 14:49:05 -04001050 return 1;
1051}
Josef Bacik25179202008-10-29 14:49:05 -04001052
Chris Mason2c64c532009-09-02 15:04:12 -04001053int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1054 struct extent_state **cached, gfp_t mask)
1055{
1056 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1057 mask);
1058}
1059
Chris Masond1310b22008-01-24 16:13:08 -05001060int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1061 gfp_t mask)
1062{
Chris Mason2c64c532009-09-02 15:04:12 -04001063 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1064 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001065}
Chris Masond1310b22008-01-24 16:13:08 -05001066
1067/*
1068 * helper function to set pages and extents in the tree dirty
1069 */
1070int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1071{
1072 unsigned long index = start >> PAGE_CACHE_SHIFT;
1073 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1074 struct page *page;
1075
1076 while (index <= end_index) {
1077 page = find_get_page(tree->mapping, index);
1078 BUG_ON(!page);
1079 __set_page_dirty_nobuffers(page);
1080 page_cache_release(page);
1081 index++;
1082 }
Chris Masond1310b22008-01-24 16:13:08 -05001083 return 0;
1084}
Chris Masond1310b22008-01-24 16:13:08 -05001085
1086/*
1087 * helper function to set both pages and extents in the tree writeback
1088 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001089static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001090{
1091 unsigned long index = start >> PAGE_CACHE_SHIFT;
1092 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1093 struct page *page;
1094
1095 while (index <= end_index) {
1096 page = find_get_page(tree->mapping, index);
1097 BUG_ON(!page);
1098 set_page_writeback(page);
1099 page_cache_release(page);
1100 index++;
1101 }
Chris Masond1310b22008-01-24 16:13:08 -05001102 return 0;
1103}
Chris Masond1310b22008-01-24 16:13:08 -05001104
Chris Masond352ac62008-09-29 15:18:18 -04001105/*
1106 * find the first offset in the io tree with 'bits' set. zero is
1107 * returned if we find something, and *start_ret and *end_ret are
1108 * set to reflect the state struct that was found.
1109 *
1110 * If nothing was found, 1 is returned, < 0 on error
1111 */
Chris Masond1310b22008-01-24 16:13:08 -05001112int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1113 u64 *start_ret, u64 *end_ret, int bits)
1114{
1115 struct rb_node *node;
1116 struct extent_state *state;
1117 int ret = 1;
1118
Chris Masoncad321a2008-12-17 14:51:42 -05001119 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001120 /*
1121 * this search will find all the extents that end after
1122 * our range starts.
1123 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001124 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001125 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001126 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001127
Chris Masond3977122009-01-05 21:25:51 -05001128 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001129 state = rb_entry(node, struct extent_state, rb_node);
1130 if (state->end >= start && (state->state & bits)) {
1131 *start_ret = state->start;
1132 *end_ret = state->end;
1133 ret = 0;
1134 break;
1135 }
1136 node = rb_next(node);
1137 if (!node)
1138 break;
1139 }
1140out:
Chris Masoncad321a2008-12-17 14:51:42 -05001141 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001142 return ret;
1143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
Chris Masond352ac62008-09-29 15:18:18 -04001145/* find the first state struct with 'bits' set after 'start', and
1146 * return it. tree->lock must be held. NULL will returned if
1147 * nothing was found after 'start'
1148 */
Chris Masond7fc6402008-02-18 12:12:38 -05001149struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1150 u64 start, int bits)
1151{
1152 struct rb_node *node;
1153 struct extent_state *state;
1154
1155 /*
1156 * this search will find all the extents that end after
1157 * our range starts.
1158 */
1159 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001160 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001161 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001162
Chris Masond3977122009-01-05 21:25:51 -05001163 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001164 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001165 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001166 return state;
Chris Masond3977122009-01-05 21:25:51 -05001167
Chris Masond7fc6402008-02-18 12:12:38 -05001168 node = rb_next(node);
1169 if (!node)
1170 break;
1171 }
1172out:
1173 return NULL;
1174}
Chris Masond7fc6402008-02-18 12:12:38 -05001175
Chris Masond352ac62008-09-29 15:18:18 -04001176/*
1177 * find a contiguous range of bytes in the file marked as delalloc, not
1178 * more than 'max_bytes'. start and end are used to return the range,
1179 *
1180 * 1 is returned if we find something, 0 if nothing was in the tree
1181 */
Chris Masonc8b97812008-10-29 14:49:59 -04001182static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001183 u64 *start, u64 *end, u64 max_bytes,
1184 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001185{
1186 struct rb_node *node;
1187 struct extent_state *state;
1188 u64 cur_start = *start;
1189 u64 found = 0;
1190 u64 total_bytes = 0;
1191
Chris Masoncad321a2008-12-17 14:51:42 -05001192 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001193
Chris Masond1310b22008-01-24 16:13:08 -05001194 /*
1195 * this search will find all the extents that end after
1196 * our range starts.
1197 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001198 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001199 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001200 if (!found)
1201 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001202 goto out;
1203 }
1204
Chris Masond3977122009-01-05 21:25:51 -05001205 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001206 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001207 if (found && (state->start != cur_start ||
1208 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001209 goto out;
1210 }
1211 if (!(state->state & EXTENT_DELALLOC)) {
1212 if (!found)
1213 *end = state->end;
1214 goto out;
1215 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001216 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001217 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001218 *cached_state = state;
1219 atomic_inc(&state->refs);
1220 }
Chris Masond1310b22008-01-24 16:13:08 -05001221 found++;
1222 *end = state->end;
1223 cur_start = state->end + 1;
1224 node = rb_next(node);
1225 if (!node)
1226 break;
1227 total_bytes += state->end - state->start + 1;
1228 if (total_bytes >= max_bytes)
1229 break;
1230 }
1231out:
Chris Masoncad321a2008-12-17 14:51:42 -05001232 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001233 return found;
1234}
1235
Chris Masonc8b97812008-10-29 14:49:59 -04001236static noinline int __unlock_for_delalloc(struct inode *inode,
1237 struct page *locked_page,
1238 u64 start, u64 end)
1239{
1240 int ret;
1241 struct page *pages[16];
1242 unsigned long index = start >> PAGE_CACHE_SHIFT;
1243 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1244 unsigned long nr_pages = end_index - index + 1;
1245 int i;
1246
1247 if (index == locked_page->index && end_index == index)
1248 return 0;
1249
Chris Masond3977122009-01-05 21:25:51 -05001250 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001251 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001252 min_t(unsigned long, nr_pages,
1253 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001254 for (i = 0; i < ret; i++) {
1255 if (pages[i] != locked_page)
1256 unlock_page(pages[i]);
1257 page_cache_release(pages[i]);
1258 }
1259 nr_pages -= ret;
1260 index += ret;
1261 cond_resched();
1262 }
1263 return 0;
1264}
1265
1266static noinline int lock_delalloc_pages(struct inode *inode,
1267 struct page *locked_page,
1268 u64 delalloc_start,
1269 u64 delalloc_end)
1270{
1271 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1272 unsigned long start_index = index;
1273 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1274 unsigned long pages_locked = 0;
1275 struct page *pages[16];
1276 unsigned long nrpages;
1277 int ret;
1278 int i;
1279
1280 /* the caller is responsible for locking the start index */
1281 if (index == locked_page->index && index == end_index)
1282 return 0;
1283
1284 /* skip the page at the start index */
1285 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001286 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001287 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001288 min_t(unsigned long,
1289 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001290 if (ret == 0) {
1291 ret = -EAGAIN;
1292 goto done;
1293 }
1294 /* now we have an array of pages, lock them all */
1295 for (i = 0; i < ret; i++) {
1296 /*
1297 * the caller is taking responsibility for
1298 * locked_page
1299 */
Chris Mason771ed682008-11-06 22:02:51 -05001300 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001301 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001302 if (!PageDirty(pages[i]) ||
1303 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001304 ret = -EAGAIN;
1305 unlock_page(pages[i]);
1306 page_cache_release(pages[i]);
1307 goto done;
1308 }
1309 }
Chris Masonc8b97812008-10-29 14:49:59 -04001310 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001311 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001312 }
Chris Masonc8b97812008-10-29 14:49:59 -04001313 nrpages -= ret;
1314 index += ret;
1315 cond_resched();
1316 }
1317 ret = 0;
1318done:
1319 if (ret && pages_locked) {
1320 __unlock_for_delalloc(inode, locked_page,
1321 delalloc_start,
1322 ((u64)(start_index + pages_locked - 1)) <<
1323 PAGE_CACHE_SHIFT);
1324 }
1325 return ret;
1326}
1327
1328/*
1329 * find a contiguous range of bytes in the file marked as delalloc, not
1330 * more than 'max_bytes'. start and end are used to return the range,
1331 *
1332 * 1 is returned if we find something, 0 if nothing was in the tree
1333 */
1334static noinline u64 find_lock_delalloc_range(struct inode *inode,
1335 struct extent_io_tree *tree,
1336 struct page *locked_page,
1337 u64 *start, u64 *end,
1338 u64 max_bytes)
1339{
1340 u64 delalloc_start;
1341 u64 delalloc_end;
1342 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001343 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001344 int ret;
1345 int loops = 0;
1346
1347again:
1348 /* step one, find a bunch of delalloc bytes starting at start */
1349 delalloc_start = *start;
1350 delalloc_end = 0;
1351 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001352 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001353 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001354 *start = delalloc_start;
1355 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001356 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001357 return found;
1358 }
1359
1360 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001361 * start comes from the offset of locked_page. We have to lock
1362 * pages in order, so we can't process delalloc bytes before
1363 * locked_page
1364 */
Chris Masond3977122009-01-05 21:25:51 -05001365 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001366 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001367
1368 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001369 * make sure to limit the number of pages we try to lock down
1370 * if we're looping.
1371 */
Chris Masond3977122009-01-05 21:25:51 -05001372 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001373 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001374
Chris Masonc8b97812008-10-29 14:49:59 -04001375 /* step two, lock all the pages after the page that has start */
1376 ret = lock_delalloc_pages(inode, locked_page,
1377 delalloc_start, delalloc_end);
1378 if (ret == -EAGAIN) {
1379 /* some of the pages are gone, lets avoid looping by
1380 * shortening the size of the delalloc range we're searching
1381 */
Chris Mason9655d292009-09-02 15:22:30 -04001382 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001383 if (!loops) {
1384 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1385 max_bytes = PAGE_CACHE_SIZE - offset;
1386 loops = 1;
1387 goto again;
1388 } else {
1389 found = 0;
1390 goto out_failed;
1391 }
1392 }
1393 BUG_ON(ret);
1394
1395 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001396 lock_extent_bits(tree, delalloc_start, delalloc_end,
1397 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001398
1399 /* then test to make sure it is all still delalloc */
1400 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001401 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001402 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001403 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1404 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001405 __unlock_for_delalloc(inode, locked_page,
1406 delalloc_start, delalloc_end);
1407 cond_resched();
1408 goto again;
1409 }
Chris Mason9655d292009-09-02 15:22:30 -04001410 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001411 *start = delalloc_start;
1412 *end = delalloc_end;
1413out_failed:
1414 return found;
1415}
1416
1417int extent_clear_unlock_delalloc(struct inode *inode,
1418 struct extent_io_tree *tree,
1419 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001420 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001421{
1422 int ret;
1423 struct page *pages[16];
1424 unsigned long index = start >> PAGE_CACHE_SHIFT;
1425 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1426 unsigned long nr_pages = end_index - index + 1;
1427 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001428 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001429
Chris Masona791e352009-10-08 11:27:10 -04001430 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001431 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001432 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001433 clear_bits |= EXTENT_DIRTY;
1434
Chris Masona791e352009-10-08 11:27:10 -04001435 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001436 clear_bits |= EXTENT_DELALLOC;
1437
Chris Mason2c64c532009-09-02 15:04:12 -04001438 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001439 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1440 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1441 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001442 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001443
Chris Masond3977122009-01-05 21:25:51 -05001444 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001445 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001446 min_t(unsigned long,
1447 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001448 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001449
Chris Masona791e352009-10-08 11:27:10 -04001450 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001451 SetPagePrivate2(pages[i]);
1452
Chris Masonc8b97812008-10-29 14:49:59 -04001453 if (pages[i] == locked_page) {
1454 page_cache_release(pages[i]);
1455 continue;
1456 }
Chris Masona791e352009-10-08 11:27:10 -04001457 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001458 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001459 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001460 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001461 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001462 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001463 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001464 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001465 page_cache_release(pages[i]);
1466 }
1467 nr_pages -= ret;
1468 index += ret;
1469 cond_resched();
1470 }
1471 return 0;
1472}
Chris Masonc8b97812008-10-29 14:49:59 -04001473
Chris Masond352ac62008-09-29 15:18:18 -04001474/*
1475 * count the number of bytes in the tree that have a given bit(s)
1476 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1477 * cached. The total number found is returned.
1478 */
Chris Masond1310b22008-01-24 16:13:08 -05001479u64 count_range_bits(struct extent_io_tree *tree,
1480 u64 *start, u64 search_end, u64 max_bytes,
1481 unsigned long bits)
1482{
1483 struct rb_node *node;
1484 struct extent_state *state;
1485 u64 cur_start = *start;
1486 u64 total_bytes = 0;
1487 int found = 0;
1488
1489 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001490 WARN_ON(1);
1491 return 0;
1492 }
1493
Chris Masoncad321a2008-12-17 14:51:42 -05001494 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001495 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1496 total_bytes = tree->dirty_bytes;
1497 goto out;
1498 }
1499 /*
1500 * this search will find all the extents that end after
1501 * our range starts.
1502 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001503 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001504 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001505 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001506
Chris Masond3977122009-01-05 21:25:51 -05001507 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001508 state = rb_entry(node, struct extent_state, rb_node);
1509 if (state->start > search_end)
1510 break;
1511 if (state->end >= cur_start && (state->state & bits)) {
1512 total_bytes += min(search_end, state->end) + 1 -
1513 max(cur_start, state->start);
1514 if (total_bytes >= max_bytes)
1515 break;
1516 if (!found) {
1517 *start = state->start;
1518 found = 1;
1519 }
1520 }
1521 node = rb_next(node);
1522 if (!node)
1523 break;
1524 }
1525out:
Chris Masoncad321a2008-12-17 14:51:42 -05001526 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001527 return total_bytes;
1528}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001529
Chris Masond352ac62008-09-29 15:18:18 -04001530/*
1531 * set the private field for a given byte offset in the tree. If there isn't
1532 * an extent_state there already, this does nothing.
1533 */
Chris Masond1310b22008-01-24 16:13:08 -05001534int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1535{
1536 struct rb_node *node;
1537 struct extent_state *state;
1538 int ret = 0;
1539
Chris Masoncad321a2008-12-17 14:51:42 -05001540 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001541 /*
1542 * this search will find all the extents that end after
1543 * our range starts.
1544 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001545 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001546 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001547 ret = -ENOENT;
1548 goto out;
1549 }
1550 state = rb_entry(node, struct extent_state, rb_node);
1551 if (state->start != start) {
1552 ret = -ENOENT;
1553 goto out;
1554 }
1555 state->private = private;
1556out:
Chris Masoncad321a2008-12-17 14:51:42 -05001557 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001558 return ret;
1559}
1560
1561int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1562{
1563 struct rb_node *node;
1564 struct extent_state *state;
1565 int ret = 0;
1566
Chris Masoncad321a2008-12-17 14:51:42 -05001567 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001568 /*
1569 * this search will find all the extents that end after
1570 * our range starts.
1571 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001572 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001573 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001574 ret = -ENOENT;
1575 goto out;
1576 }
1577 state = rb_entry(node, struct extent_state, rb_node);
1578 if (state->start != start) {
1579 ret = -ENOENT;
1580 goto out;
1581 }
1582 *private = state->private;
1583out:
Chris Masoncad321a2008-12-17 14:51:42 -05001584 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001585 return ret;
1586}
1587
1588/*
1589 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001590 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001591 * has the bits set. Otherwise, 1 is returned if any bit in the
1592 * range is found set.
1593 */
1594int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001595 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001596{
1597 struct extent_state *state = NULL;
1598 struct rb_node *node;
1599 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001600
Chris Masoncad321a2008-12-17 14:51:42 -05001601 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001602 if (cached && cached->tree && cached->start == start)
1603 node = &cached->rb_node;
1604 else
1605 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001606 while (node && start <= end) {
1607 state = rb_entry(node, struct extent_state, rb_node);
1608
1609 if (filled && state->start > start) {
1610 bitset = 0;
1611 break;
1612 }
1613
1614 if (state->start > end)
1615 break;
1616
1617 if (state->state & bits) {
1618 bitset = 1;
1619 if (!filled)
1620 break;
1621 } else if (filled) {
1622 bitset = 0;
1623 break;
1624 }
Chris Mason46562ce2009-09-23 20:23:16 -04001625
1626 if (state->end == (u64)-1)
1627 break;
1628
Chris Masond1310b22008-01-24 16:13:08 -05001629 start = state->end + 1;
1630 if (start > end)
1631 break;
1632 node = rb_next(node);
1633 if (!node) {
1634 if (filled)
1635 bitset = 0;
1636 break;
1637 }
1638 }
Chris Masoncad321a2008-12-17 14:51:42 -05001639 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001640 return bitset;
1641}
Chris Masond1310b22008-01-24 16:13:08 -05001642
1643/*
1644 * helper function to set a given page up to date if all the
1645 * extents in the tree for that page are up to date
1646 */
1647static int check_page_uptodate(struct extent_io_tree *tree,
1648 struct page *page)
1649{
1650 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1651 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001652 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001653 SetPageUptodate(page);
1654 return 0;
1655}
1656
1657/*
1658 * helper function to unlock a page if all the extents in the tree
1659 * for that page are unlocked
1660 */
1661static int check_page_locked(struct extent_io_tree *tree,
1662 struct page *page)
1663{
1664 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1665 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001666 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001667 unlock_page(page);
1668 return 0;
1669}
1670
1671/*
1672 * helper function to end page writeback if all the extents
1673 * in the tree for that page are done with writeback
1674 */
1675static int check_page_writeback(struct extent_io_tree *tree,
1676 struct page *page)
1677{
Chris Mason1edbb732009-09-02 13:24:36 -04001678 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001679 return 0;
1680}
1681
1682/* lots and lots of room for performance fixes in the end_bio funcs */
1683
1684/*
1685 * after a writepage IO is done, we need to:
1686 * clear the uptodate bits on error
1687 * clear the writeback bits in the extent tree for this IO
1688 * end_page_writeback if the page has no more pending IO
1689 *
1690 * Scheduling is not allowed, so the extent state tree is expected
1691 * to have one and only one object corresponding to this IO.
1692 */
Chris Masond1310b22008-01-24 16:13:08 -05001693static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001694{
Chris Mason1259ab72008-05-12 13:39:03 -04001695 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001696 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001697 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001698 u64 start;
1699 u64 end;
1700 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001701 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001702
Chris Masond1310b22008-01-24 16:13:08 -05001703 do {
1704 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001705 tree = &BTRFS_I(page->mapping->host)->io_tree;
1706
Chris Masond1310b22008-01-24 16:13:08 -05001707 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1708 bvec->bv_offset;
1709 end = start + bvec->bv_len - 1;
1710
1711 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1712 whole_page = 1;
1713 else
1714 whole_page = 0;
1715
1716 if (--bvec >= bio->bi_io_vec)
1717 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001718 if (tree->ops && tree->ops->writepage_end_io_hook) {
1719 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001720 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001721 if (ret)
1722 uptodate = 0;
1723 }
1724
1725 if (!uptodate && tree->ops &&
1726 tree->ops->writepage_io_failed_hook) {
1727 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001728 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001729 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001730 uptodate = (err == 0);
1731 continue;
1732 }
1733 }
1734
Chris Masond1310b22008-01-24 16:13:08 -05001735 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001736 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001737 ClearPageUptodate(page);
1738 SetPageError(page);
1739 }
Chris Mason70dec802008-01-29 09:59:12 -05001740
Chris Masond1310b22008-01-24 16:13:08 -05001741 if (whole_page)
1742 end_page_writeback(page);
1743 else
1744 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001745 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001746
Chris Masond1310b22008-01-24 16:13:08 -05001747 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001748}
1749
1750/*
1751 * after a readpage IO is done, we need to:
1752 * clear the uptodate bits on error
1753 * set the uptodate bits if things worked
1754 * set the page up to date if all extents in the tree are uptodate
1755 * clear the lock bit in the extent tree
1756 * unlock the page if there are no other extents locked for it
1757 *
1758 * Scheduling is not allowed, so the extent state tree is expected
1759 * to have one and only one object corresponding to this IO.
1760 */
Chris Masond1310b22008-01-24 16:13:08 -05001761static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001762{
1763 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001764 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1765 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001766 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001767 u64 start;
1768 u64 end;
1769 int whole_page;
1770 int ret;
1771
Chris Masond20f7042008-12-08 16:58:54 -05001772 if (err)
1773 uptodate = 0;
1774
Chris Masond1310b22008-01-24 16:13:08 -05001775 do {
1776 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001777 tree = &BTRFS_I(page->mapping->host)->io_tree;
1778
Chris Masond1310b22008-01-24 16:13:08 -05001779 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1780 bvec->bv_offset;
1781 end = start + bvec->bv_len - 1;
1782
1783 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1784 whole_page = 1;
1785 else
1786 whole_page = 0;
1787
Chris Mason4125bf72010-02-03 18:18:45 +00001788 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001789 prefetchw(&bvec->bv_page->flags);
1790
1791 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001792 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001793 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001794 if (ret)
1795 uptodate = 0;
1796 }
Chris Mason7e383262008-04-09 16:28:12 -04001797 if (!uptodate && tree->ops &&
1798 tree->ops->readpage_io_failed_hook) {
1799 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001800 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001801 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001802 uptodate =
1803 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001804 if (err)
1805 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001806 continue;
1807 }
1808 }
Chris Mason70dec802008-01-29 09:59:12 -05001809
Chris Mason771ed682008-11-06 22:02:51 -05001810 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001811 set_extent_uptodate(tree, start, end,
1812 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001813 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001814 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001815
Chris Mason70dec802008-01-29 09:59:12 -05001816 if (whole_page) {
1817 if (uptodate) {
1818 SetPageUptodate(page);
1819 } else {
1820 ClearPageUptodate(page);
1821 SetPageError(page);
1822 }
Chris Masond1310b22008-01-24 16:13:08 -05001823 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001824 } else {
1825 if (uptodate) {
1826 check_page_uptodate(tree, page);
1827 } else {
1828 ClearPageUptodate(page);
1829 SetPageError(page);
1830 }
Chris Masond1310b22008-01-24 16:13:08 -05001831 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001832 }
Chris Mason4125bf72010-02-03 18:18:45 +00001833 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001834
1835 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001836}
1837
1838/*
1839 * IO done from prepare_write is pretty simple, we just unlock
1840 * the structs in the extent tree when done, and set the uptodate bits
1841 * as appropriate.
1842 */
Chris Masond1310b22008-01-24 16:13:08 -05001843static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001844{
1845 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1846 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001847 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001848 u64 start;
1849 u64 end;
1850
Chris Masond1310b22008-01-24 16:13:08 -05001851 do {
1852 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001853 tree = &BTRFS_I(page->mapping->host)->io_tree;
1854
Chris Masond1310b22008-01-24 16:13:08 -05001855 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1856 bvec->bv_offset;
1857 end = start + bvec->bv_len - 1;
1858
1859 if (--bvec >= bio->bi_io_vec)
1860 prefetchw(&bvec->bv_page->flags);
1861
1862 if (uptodate) {
1863 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1864 } else {
1865 ClearPageUptodate(page);
1866 SetPageError(page);
1867 }
1868
1869 unlock_extent(tree, start, end, GFP_ATOMIC);
1870
1871 } while (bvec >= bio->bi_io_vec);
1872
1873 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001874}
1875
1876static struct bio *
1877extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1878 gfp_t gfp_flags)
1879{
1880 struct bio *bio;
1881
1882 bio = bio_alloc(gfp_flags, nr_vecs);
1883
1884 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1885 while (!bio && (nr_vecs /= 2))
1886 bio = bio_alloc(gfp_flags, nr_vecs);
1887 }
1888
1889 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001890 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001891 bio->bi_bdev = bdev;
1892 bio->bi_sector = first_sector;
1893 }
1894 return bio;
1895}
1896
Chris Masonc8b97812008-10-29 14:49:59 -04001897static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1898 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001899{
Chris Masond1310b22008-01-24 16:13:08 -05001900 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001901 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1902 struct page *page = bvec->bv_page;
1903 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001904 u64 start;
1905 u64 end;
1906
1907 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1908 end = start + bvec->bv_len - 1;
1909
David Woodhouse902b22f2008-08-20 08:51:49 -04001910 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001911
1912 bio_get(bio);
1913
Chris Mason065631f2008-02-20 12:07:25 -05001914 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001915 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001916 mirror_num, bio_flags);
Chris Mason0b86a832008-03-24 15:01:56 -04001917 else
1918 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001919 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1920 ret = -EOPNOTSUPP;
1921 bio_put(bio);
1922 return ret;
1923}
1924
1925static int submit_extent_page(int rw, struct extent_io_tree *tree,
1926 struct page *page, sector_t sector,
1927 size_t size, unsigned long offset,
1928 struct block_device *bdev,
1929 struct bio **bio_ret,
1930 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001931 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001932 int mirror_num,
1933 unsigned long prev_bio_flags,
1934 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001935{
1936 int ret = 0;
1937 struct bio *bio;
1938 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001939 int contig = 0;
1940 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1941 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001942 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001943
1944 if (bio_ret && *bio_ret) {
1945 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001946 if (old_compressed)
1947 contig = bio->bi_sector == sector;
1948 else
1949 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1950 sector;
1951
1952 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001953 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001954 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1955 bio_flags)) ||
1956 bio_add_page(bio, page, page_size, offset) < page_size) {
1957 ret = submit_one_bio(rw, bio, mirror_num,
1958 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001959 bio = NULL;
1960 } else {
1961 return 0;
1962 }
1963 }
Chris Masonc8b97812008-10-29 14:49:59 -04001964 if (this_compressed)
1965 nr = BIO_MAX_PAGES;
1966 else
1967 nr = bio_get_nr_vecs(bdev);
1968
Chris Masond1310b22008-01-24 16:13:08 -05001969 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001970
Chris Masonc8b97812008-10-29 14:49:59 -04001971 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001972 bio->bi_end_io = end_io_func;
1973 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001974
Chris Masond3977122009-01-05 21:25:51 -05001975 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001976 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001977 else
Chris Masonc8b97812008-10-29 14:49:59 -04001978 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001979
1980 return ret;
1981}
1982
1983void set_page_extent_mapped(struct page *page)
1984{
1985 if (!PagePrivate(page)) {
1986 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001987 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001988 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001989 }
1990}
1991
Christoph Hellwigb2950862008-12-02 09:54:17 -05001992static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001993{
1994 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1995}
1996
1997/*
1998 * basic readpage implementation. Locked extent state structs are inserted
1999 * into the tree that are removed when the IO is done (by the end_io
2000 * handlers)
2001 */
2002static int __extent_read_full_page(struct extent_io_tree *tree,
2003 struct page *page,
2004 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002005 struct bio **bio, int mirror_num,
2006 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002007{
2008 struct inode *inode = page->mapping->host;
2009 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2010 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2011 u64 end;
2012 u64 cur = start;
2013 u64 extent_offset;
2014 u64 last_byte = i_size_read(inode);
2015 u64 block_start;
2016 u64 cur_end;
2017 sector_t sector;
2018 struct extent_map *em;
2019 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002020 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002021 int ret;
2022 int nr = 0;
2023 size_t page_offset = 0;
2024 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002025 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002026 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002027 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002028
2029 set_page_extent_mapped(page);
2030
2031 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002032 while (1) {
2033 lock_extent(tree, start, end, GFP_NOFS);
2034 ordered = btrfs_lookup_ordered_extent(inode, start);
2035 if (!ordered)
2036 break;
2037 unlock_extent(tree, start, end, GFP_NOFS);
2038 btrfs_start_ordered_extent(inode, ordered, 1);
2039 btrfs_put_ordered_extent(ordered);
2040 }
Chris Masond1310b22008-01-24 16:13:08 -05002041
Chris Masonc8b97812008-10-29 14:49:59 -04002042 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2043 char *userpage;
2044 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2045
2046 if (zero_offset) {
2047 iosize = PAGE_CACHE_SIZE - zero_offset;
2048 userpage = kmap_atomic(page, KM_USER0);
2049 memset(userpage + zero_offset, 0, iosize);
2050 flush_dcache_page(page);
2051 kunmap_atomic(userpage, KM_USER0);
2052 }
2053 }
Chris Masond1310b22008-01-24 16:13:08 -05002054 while (cur <= end) {
2055 if (cur >= last_byte) {
2056 char *userpage;
2057 iosize = PAGE_CACHE_SIZE - page_offset;
2058 userpage = kmap_atomic(page, KM_USER0);
2059 memset(userpage + page_offset, 0, iosize);
2060 flush_dcache_page(page);
2061 kunmap_atomic(userpage, KM_USER0);
2062 set_extent_uptodate(tree, cur, cur + iosize - 1,
2063 GFP_NOFS);
2064 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2065 break;
2066 }
2067 em = get_extent(inode, page, page_offset, cur,
2068 end - cur + 1, 0);
2069 if (IS_ERR(em) || !em) {
2070 SetPageError(page);
2071 unlock_extent(tree, cur, end, GFP_NOFS);
2072 break;
2073 }
Chris Masond1310b22008-01-24 16:13:08 -05002074 extent_offset = cur - em->start;
2075 BUG_ON(extent_map_end(em) <= cur);
2076 BUG_ON(end < cur);
2077
Chris Masonc8b97812008-10-29 14:49:59 -04002078 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2079 this_bio_flag = EXTENT_BIO_COMPRESSED;
2080
Chris Masond1310b22008-01-24 16:13:08 -05002081 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2082 cur_end = min(extent_map_end(em) - 1, end);
2083 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002084 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2085 disk_io_size = em->block_len;
2086 sector = em->block_start >> 9;
2087 } else {
2088 sector = (em->block_start + extent_offset) >> 9;
2089 disk_io_size = iosize;
2090 }
Chris Masond1310b22008-01-24 16:13:08 -05002091 bdev = em->bdev;
2092 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002093 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2094 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002095 free_extent_map(em);
2096 em = NULL;
2097
2098 /* we've found a hole, just zero and go on */
2099 if (block_start == EXTENT_MAP_HOLE) {
2100 char *userpage;
2101 userpage = kmap_atomic(page, KM_USER0);
2102 memset(userpage + page_offset, 0, iosize);
2103 flush_dcache_page(page);
2104 kunmap_atomic(userpage, KM_USER0);
2105
2106 set_extent_uptodate(tree, cur, cur + iosize - 1,
2107 GFP_NOFS);
2108 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2109 cur = cur + iosize;
2110 page_offset += iosize;
2111 continue;
2112 }
2113 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002114 if (test_range_bit(tree, cur, cur_end,
2115 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002116 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002117 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2118 cur = cur + iosize;
2119 page_offset += iosize;
2120 continue;
2121 }
Chris Mason70dec802008-01-29 09:59:12 -05002122 /* we have an inline extent but it didn't get marked up
2123 * to date. Error out
2124 */
2125 if (block_start == EXTENT_MAP_INLINE) {
2126 SetPageError(page);
2127 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2128 cur = cur + iosize;
2129 page_offset += iosize;
2130 continue;
2131 }
Chris Masond1310b22008-01-24 16:13:08 -05002132
2133 ret = 0;
2134 if (tree->ops && tree->ops->readpage_io_hook) {
2135 ret = tree->ops->readpage_io_hook(page, cur,
2136 cur + iosize - 1);
2137 }
2138 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002139 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2140 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002141 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002142 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002143 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002144 end_bio_extent_readpage, mirror_num,
2145 *bio_flags,
2146 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002147 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002148 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002149 }
2150 if (ret)
2151 SetPageError(page);
2152 cur = cur + iosize;
2153 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002154 }
2155 if (!nr) {
2156 if (!PageError(page))
2157 SetPageUptodate(page);
2158 unlock_page(page);
2159 }
2160 return 0;
2161}
2162
2163int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2164 get_extent_t *get_extent)
2165{
2166 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002167 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002168 int ret;
2169
Chris Masonc8b97812008-10-29 14:49:59 -04002170 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2171 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002172 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002173 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002174 return ret;
2175}
Chris Masond1310b22008-01-24 16:13:08 -05002176
Chris Mason11c83492009-04-20 15:50:09 -04002177static noinline void update_nr_written(struct page *page,
2178 struct writeback_control *wbc,
2179 unsigned long nr_written)
2180{
2181 wbc->nr_to_write -= nr_written;
2182 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2183 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2184 page->mapping->writeback_index = page->index + nr_written;
2185}
2186
Chris Masond1310b22008-01-24 16:13:08 -05002187/*
2188 * the writepage semantics are similar to regular writepage. extent
2189 * records are inserted to lock ranges in the tree, and as dirty areas
2190 * are found, they are marked writeback. Then the lock bits are removed
2191 * and the end_io handler clears the writeback ranges
2192 */
2193static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2194 void *data)
2195{
2196 struct inode *inode = page->mapping->host;
2197 struct extent_page_data *epd = data;
2198 struct extent_io_tree *tree = epd->tree;
2199 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2200 u64 delalloc_start;
2201 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2202 u64 end;
2203 u64 cur = start;
2204 u64 extent_offset;
2205 u64 last_byte = i_size_read(inode);
2206 u64 block_start;
2207 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002208 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002209 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002210 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002211 struct extent_map *em;
2212 struct block_device *bdev;
2213 int ret;
2214 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002215 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002216 size_t blocksize;
2217 loff_t i_size = i_size_read(inode);
2218 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2219 u64 nr_delalloc;
2220 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002221 int page_started;
2222 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002223 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002224 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002225
Chris Masonffbd5172009-04-20 15:50:09 -04002226 if (wbc->sync_mode == WB_SYNC_ALL)
2227 write_flags = WRITE_SYNC_PLUG;
2228 else
2229 write_flags = WRITE;
2230
Chris Masond1310b22008-01-24 16:13:08 -05002231 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002232 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002233 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002234 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002235 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002236 unlock_page(page);
2237 return 0;
2238 }
2239
2240 if (page->index == end_index) {
2241 char *userpage;
2242
Chris Masond1310b22008-01-24 16:13:08 -05002243 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002244 memset(userpage + pg_offset, 0,
2245 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002246 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002247 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002248 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002249 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002250
2251 set_page_extent_mapped(page);
2252
2253 delalloc_start = start;
2254 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002255 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002256 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002257 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002258 /*
2259 * make sure the wbc mapping index is at least updated
2260 * to this page.
2261 */
2262 update_nr_written(page, wbc, 0);
2263
Chris Masond3977122009-01-05 21:25:51 -05002264 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002265 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002266 page,
2267 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002268 &delalloc_end,
2269 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002270 if (nr_delalloc == 0) {
2271 delalloc_start = delalloc_end + 1;
2272 continue;
2273 }
2274 tree->ops->fill_delalloc(inode, page, delalloc_start,
2275 delalloc_end, &page_started,
2276 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002277 /*
2278 * delalloc_end is already one less than the total
2279 * length, so we don't subtract one from
2280 * PAGE_CACHE_SIZE
2281 */
2282 delalloc_to_write += (delalloc_end - delalloc_start +
2283 PAGE_CACHE_SIZE) >>
2284 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002285 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002286 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002287 if (wbc->nr_to_write < delalloc_to_write) {
2288 int thresh = 8192;
2289
2290 if (delalloc_to_write < thresh * 2)
2291 thresh = delalloc_to_write;
2292 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2293 thresh);
2294 }
Chris Masonc8b97812008-10-29 14:49:59 -04002295
Chris Mason771ed682008-11-06 22:02:51 -05002296 /* did the fill delalloc function already unlock and start
2297 * the IO?
2298 */
2299 if (page_started) {
2300 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002301 /*
2302 * we've unlocked the page, so we can't update
2303 * the mapping's writeback index, just update
2304 * nr_to_write.
2305 */
2306 wbc->nr_to_write -= nr_written;
2307 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002308 }
Chris Masonc8b97812008-10-29 14:49:59 -04002309 }
Chris Mason247e7432008-07-17 12:53:51 -04002310 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002311 ret = tree->ops->writepage_start_hook(page, start,
2312 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002313 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002314 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002315 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002316 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002317 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002318 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002319 }
2320 }
2321
Chris Mason11c83492009-04-20 15:50:09 -04002322 /*
2323 * we don't want to touch the inode after unlocking the page,
2324 * so we update the mapping writeback index now
2325 */
2326 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002327
Chris Masond1310b22008-01-24 16:13:08 -05002328 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002329 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002330 if (tree->ops && tree->ops->writepage_end_io_hook)
2331 tree->ops->writepage_end_io_hook(page, start,
2332 page_end, NULL, 1);
2333 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002334 goto done;
2335 }
2336
Chris Masond1310b22008-01-24 16:13:08 -05002337 blocksize = inode->i_sb->s_blocksize;
2338
2339 while (cur <= end) {
2340 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002341 if (tree->ops && tree->ops->writepage_end_io_hook)
2342 tree->ops->writepage_end_io_hook(page, cur,
2343 page_end, NULL, 1);
2344 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002345 break;
2346 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002347 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002348 end - cur + 1, 1);
2349 if (IS_ERR(em) || !em) {
2350 SetPageError(page);
2351 break;
2352 }
2353
2354 extent_offset = cur - em->start;
2355 BUG_ON(extent_map_end(em) <= cur);
2356 BUG_ON(end < cur);
2357 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2358 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2359 sector = (em->block_start + extent_offset) >> 9;
2360 bdev = em->bdev;
2361 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002362 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002363 free_extent_map(em);
2364 em = NULL;
2365
Chris Masonc8b97812008-10-29 14:49:59 -04002366 /*
2367 * compressed and inline extents are written through other
2368 * paths in the FS
2369 */
2370 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002371 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002372 /*
2373 * end_io notification does not happen here for
2374 * compressed extents
2375 */
2376 if (!compressed && tree->ops &&
2377 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002378 tree->ops->writepage_end_io_hook(page, cur,
2379 cur + iosize - 1,
2380 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002381 else if (compressed) {
2382 /* we don't want to end_page_writeback on
2383 * a compressed extent. this happens
2384 * elsewhere
2385 */
2386 nr++;
2387 }
2388
2389 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002390 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002391 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002392 continue;
2393 }
Chris Masond1310b22008-01-24 16:13:08 -05002394 /* leave this out until we have a page_mkwrite call */
2395 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002396 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002397 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002398 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002399 continue;
2400 }
Chris Masonc8b97812008-10-29 14:49:59 -04002401
Chris Masond1310b22008-01-24 16:13:08 -05002402 if (tree->ops && tree->ops->writepage_io_hook) {
2403 ret = tree->ops->writepage_io_hook(page, cur,
2404 cur + iosize - 1);
2405 } else {
2406 ret = 0;
2407 }
Chris Mason1259ab72008-05-12 13:39:03 -04002408 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002409 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002410 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002411 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002412
Chris Masond1310b22008-01-24 16:13:08 -05002413 set_range_writeback(tree, cur, cur + iosize - 1);
2414 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002415 printk(KERN_ERR "btrfs warning page %lu not "
2416 "writeback, cur %llu end %llu\n",
2417 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002418 (unsigned long long)end);
2419 }
2420
Chris Masonffbd5172009-04-20 15:50:09 -04002421 ret = submit_extent_page(write_flags, tree, page,
2422 sector, iosize, pg_offset,
2423 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002424 end_bio_extent_writepage,
2425 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002426 if (ret)
2427 SetPageError(page);
2428 }
2429 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002430 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002431 nr++;
2432 }
2433done:
2434 if (nr == 0) {
2435 /* make sure the mapping tag for page dirty gets cleared */
2436 set_page_writeback(page);
2437 end_page_writeback(page);
2438 }
Chris Masond1310b22008-01-24 16:13:08 -05002439 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002440
Chris Mason11c83492009-04-20 15:50:09 -04002441done_unlocked:
2442
Chris Mason2c64c532009-09-02 15:04:12 -04002443 /* drop our reference on any cached states */
2444 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002445 return 0;
2446}
2447
Chris Masond1310b22008-01-24 16:13:08 -05002448/**
Chris Mason4bef0842008-09-08 11:18:08 -04002449 * 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 -05002450 * @mapping: address space structure to write
2451 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2452 * @writepage: function called for each page
2453 * @data: data passed to writepage function
2454 *
2455 * If a page is already under I/O, write_cache_pages() skips it, even
2456 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2457 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2458 * and msync() need to guarantee that all the data which was dirty at the time
2459 * the call was made get new I/O started against them. If wbc->sync_mode is
2460 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2461 * existing IO to complete.
2462 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002463static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002464 struct address_space *mapping,
2465 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002466 writepage_t writepage, void *data,
2467 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002468{
Chris Masond1310b22008-01-24 16:13:08 -05002469 int ret = 0;
2470 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002471 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002472 struct pagevec pvec;
2473 int nr_pages;
2474 pgoff_t index;
2475 pgoff_t end; /* Inclusive */
2476 int scanned = 0;
2477 int range_whole = 0;
2478
Chris Masond1310b22008-01-24 16:13:08 -05002479 pagevec_init(&pvec, 0);
2480 if (wbc->range_cyclic) {
2481 index = mapping->writeback_index; /* Start from prev offset */
2482 end = -1;
2483 } else {
2484 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2485 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2486 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2487 range_whole = 1;
2488 scanned = 1;
2489 }
2490retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002491 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002492 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002493 PAGECACHE_TAG_DIRTY, min(end - index,
2494 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002495 unsigned i;
2496
2497 scanned = 1;
2498 for (i = 0; i < nr_pages; i++) {
2499 struct page *page = pvec.pages[i];
2500
2501 /*
2502 * At this point we hold neither mapping->tree_lock nor
2503 * lock on the page itself: the page may be truncated or
2504 * invalidated (changing page->mapping to NULL), or even
2505 * swizzled back from swapper_space to tmpfs file
2506 * mapping
2507 */
Chris Mason4bef0842008-09-08 11:18:08 -04002508 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2509 tree->ops->write_cache_pages_lock_hook(page);
2510 else
2511 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002512
2513 if (unlikely(page->mapping != mapping)) {
2514 unlock_page(page);
2515 continue;
2516 }
2517
2518 if (!wbc->range_cyclic && page->index > end) {
2519 done = 1;
2520 unlock_page(page);
2521 continue;
2522 }
2523
Chris Masond2c3f4f2008-11-19 12:44:22 -05002524 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002525 if (PageWriteback(page))
2526 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002527 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002528 }
Chris Masond1310b22008-01-24 16:13:08 -05002529
2530 if (PageWriteback(page) ||
2531 !clear_page_dirty_for_io(page)) {
2532 unlock_page(page);
2533 continue;
2534 }
2535
2536 ret = (*writepage)(page, wbc, data);
2537
2538 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2539 unlock_page(page);
2540 ret = 0;
2541 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002542 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002543 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002544
2545 /*
2546 * the filesystem may choose to bump up nr_to_write.
2547 * We have to make sure to honor the new nr_to_write
2548 * at any time
2549 */
2550 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002551 }
2552 pagevec_release(&pvec);
2553 cond_resched();
2554 }
2555 if (!scanned && !done) {
2556 /*
2557 * We hit the last page and there is more work to be done: wrap
2558 * back to the start of the file
2559 */
2560 scanned = 1;
2561 index = 0;
2562 goto retry;
2563 }
Chris Masond1310b22008-01-24 16:13:08 -05002564 return ret;
2565}
Chris Masond1310b22008-01-24 16:13:08 -05002566
Chris Masonffbd5172009-04-20 15:50:09 -04002567static void flush_epd_write_bio(struct extent_page_data *epd)
2568{
2569 if (epd->bio) {
2570 if (epd->sync_io)
2571 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2572 else
2573 submit_one_bio(WRITE, epd->bio, 0, 0);
2574 epd->bio = NULL;
2575 }
2576}
2577
Chris Masond2c3f4f2008-11-19 12:44:22 -05002578static noinline void flush_write_bio(void *data)
2579{
2580 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002581 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002582}
2583
Chris Masond1310b22008-01-24 16:13:08 -05002584int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2585 get_extent_t *get_extent,
2586 struct writeback_control *wbc)
2587{
2588 int ret;
2589 struct address_space *mapping = page->mapping;
2590 struct extent_page_data epd = {
2591 .bio = NULL,
2592 .tree = tree,
2593 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002594 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002595 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002596 };
2597 struct writeback_control wbc_writepages = {
2598 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002599 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002600 .older_than_this = NULL,
2601 .nr_to_write = 64,
2602 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2603 .range_end = (loff_t)-1,
2604 };
2605
Chris Masond1310b22008-01-24 16:13:08 -05002606 ret = __extent_writepage(page, wbc, &epd);
2607
Chris Mason4bef0842008-09-08 11:18:08 -04002608 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002609 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002610 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002611 return ret;
2612}
Chris Masond1310b22008-01-24 16:13:08 -05002613
Chris Mason771ed682008-11-06 22:02:51 -05002614int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2615 u64 start, u64 end, get_extent_t *get_extent,
2616 int mode)
2617{
2618 int ret = 0;
2619 struct address_space *mapping = inode->i_mapping;
2620 struct page *page;
2621 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2622 PAGE_CACHE_SHIFT;
2623
2624 struct extent_page_data epd = {
2625 .bio = NULL,
2626 .tree = tree,
2627 .get_extent = get_extent,
2628 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002629 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002630 };
2631 struct writeback_control wbc_writepages = {
2632 .bdi = inode->i_mapping->backing_dev_info,
2633 .sync_mode = mode,
2634 .older_than_this = NULL,
2635 .nr_to_write = nr_pages * 2,
2636 .range_start = start,
2637 .range_end = end + 1,
2638 };
2639
Chris Masond3977122009-01-05 21:25:51 -05002640 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002641 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2642 if (clear_page_dirty_for_io(page))
2643 ret = __extent_writepage(page, &wbc_writepages, &epd);
2644 else {
2645 if (tree->ops && tree->ops->writepage_end_io_hook)
2646 tree->ops->writepage_end_io_hook(page, start,
2647 start + PAGE_CACHE_SIZE - 1,
2648 NULL, 1);
2649 unlock_page(page);
2650 }
2651 page_cache_release(page);
2652 start += PAGE_CACHE_SIZE;
2653 }
2654
Chris Masonffbd5172009-04-20 15:50:09 -04002655 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002656 return ret;
2657}
Chris Masond1310b22008-01-24 16:13:08 -05002658
2659int extent_writepages(struct extent_io_tree *tree,
2660 struct address_space *mapping,
2661 get_extent_t *get_extent,
2662 struct writeback_control *wbc)
2663{
2664 int ret = 0;
2665 struct extent_page_data epd = {
2666 .bio = NULL,
2667 .tree = tree,
2668 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002669 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002670 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002671 };
2672
Chris Mason4bef0842008-09-08 11:18:08 -04002673 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002674 __extent_writepage, &epd,
2675 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002676 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002677 return ret;
2678}
Chris Masond1310b22008-01-24 16:13:08 -05002679
2680int extent_readpages(struct extent_io_tree *tree,
2681 struct address_space *mapping,
2682 struct list_head *pages, unsigned nr_pages,
2683 get_extent_t get_extent)
2684{
2685 struct bio *bio = NULL;
2686 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002687 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002688
Chris Masond1310b22008-01-24 16:13:08 -05002689 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2690 struct page *page = list_entry(pages->prev, struct page, lru);
2691
2692 prefetchw(&page->flags);
2693 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002694 if (!add_to_page_cache_lru(page, mapping,
Chris Masond1310b22008-01-24 16:13:08 -05002695 page->index, GFP_KERNEL)) {
Chris Masonf1885912008-04-09 16:28:12 -04002696 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002697 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002698 }
2699 page_cache_release(page);
2700 }
Chris Masond1310b22008-01-24 16:13:08 -05002701 BUG_ON(!list_empty(pages));
2702 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002703 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002704 return 0;
2705}
Chris Masond1310b22008-01-24 16:13:08 -05002706
2707/*
2708 * basic invalidatepage code, this waits on any locked or writeback
2709 * ranges corresponding to the page, and then deletes any extent state
2710 * records from the tree
2711 */
2712int extent_invalidatepage(struct extent_io_tree *tree,
2713 struct page *page, unsigned long offset)
2714{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002715 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002716 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2717 u64 end = start + PAGE_CACHE_SIZE - 1;
2718 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2719
Chris Masond3977122009-01-05 21:25:51 -05002720 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002721 if (start > end)
2722 return 0;
2723
Josef Bacik2ac55d42010-02-03 19:33:23 +00002724 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002725 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002726 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002727 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2728 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002729 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002730 return 0;
2731}
Chris Masond1310b22008-01-24 16:13:08 -05002732
2733/*
2734 * simple commit_write call, set_range_dirty is used to mark both
2735 * the pages and the extent records as dirty
2736 */
2737int extent_commit_write(struct extent_io_tree *tree,
2738 struct inode *inode, struct page *page,
2739 unsigned from, unsigned to)
2740{
2741 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2742
2743 set_page_extent_mapped(page);
2744 set_page_dirty(page);
2745
2746 if (pos > inode->i_size) {
2747 i_size_write(inode, pos);
2748 mark_inode_dirty(inode);
2749 }
2750 return 0;
2751}
Chris Masond1310b22008-01-24 16:13:08 -05002752
2753int extent_prepare_write(struct extent_io_tree *tree,
2754 struct inode *inode, struct page *page,
2755 unsigned from, unsigned to, get_extent_t *get_extent)
2756{
2757 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2758 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2759 u64 block_start;
2760 u64 orig_block_start;
2761 u64 block_end;
2762 u64 cur_end;
2763 struct extent_map *em;
2764 unsigned blocksize = 1 << inode->i_blkbits;
2765 size_t page_offset = 0;
2766 size_t block_off_start;
2767 size_t block_off_end;
2768 int err = 0;
2769 int iocount = 0;
2770 int ret = 0;
2771 int isnew;
2772
2773 set_page_extent_mapped(page);
2774
2775 block_start = (page_start + from) & ~((u64)blocksize - 1);
2776 block_end = (page_start + to - 1) | (blocksize - 1);
2777 orig_block_start = block_start;
2778
2779 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002780 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002781 em = get_extent(inode, page, page_offset, block_start,
2782 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002783 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002784 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002785
Chris Masond1310b22008-01-24 16:13:08 -05002786 cur_end = min(block_end, extent_map_end(em) - 1);
2787 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2788 block_off_end = block_off_start + blocksize;
2789 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2790
2791 if (!PageUptodate(page) && isnew &&
2792 (block_off_end > to || block_off_start < from)) {
2793 void *kaddr;
2794
2795 kaddr = kmap_atomic(page, KM_USER0);
2796 if (block_off_end > to)
2797 memset(kaddr + to, 0, block_off_end - to);
2798 if (block_off_start < from)
2799 memset(kaddr + block_off_start, 0,
2800 from - block_off_start);
2801 flush_dcache_page(page);
2802 kunmap_atomic(kaddr, KM_USER0);
2803 }
2804 if ((em->block_start != EXTENT_MAP_HOLE &&
2805 em->block_start != EXTENT_MAP_INLINE) &&
2806 !isnew && !PageUptodate(page) &&
2807 (block_off_end > to || block_off_start < from) &&
2808 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002809 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002810 u64 sector;
2811 u64 extent_offset = block_start - em->start;
2812 size_t iosize;
2813 sector = (em->block_start + extent_offset) >> 9;
2814 iosize = (cur_end - block_start + blocksize) &
2815 ~((u64)blocksize - 1);
2816 /*
2817 * we've already got the extent locked, but we
2818 * need to split the state such that our end_bio
2819 * handler can clear the lock.
2820 */
2821 set_extent_bit(tree, block_start,
2822 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002823 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002824 ret = submit_extent_page(READ, tree, page,
2825 sector, iosize, page_offset, em->bdev,
2826 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002827 end_bio_extent_preparewrite, 0,
2828 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002829 iocount++;
2830 block_start = block_start + iosize;
2831 } else {
2832 set_extent_uptodate(tree, block_start, cur_end,
2833 GFP_NOFS);
2834 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2835 block_start = cur_end + 1;
2836 }
2837 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2838 free_extent_map(em);
2839 }
2840 if (iocount) {
2841 wait_extent_bit(tree, orig_block_start,
2842 block_end, EXTENT_LOCKED);
2843 }
2844 check_page_uptodate(tree, page);
2845err:
2846 /* FIXME, zero out newly allocated blocks on error */
2847 return err;
2848}
Chris Masond1310b22008-01-24 16:13:08 -05002849
2850/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002851 * a helper for releasepage, this tests for areas of the page that
2852 * are locked or under IO and drops the related state bits if it is safe
2853 * to drop the page.
2854 */
2855int try_release_extent_state(struct extent_map_tree *map,
2856 struct extent_io_tree *tree, struct page *page,
2857 gfp_t mask)
2858{
2859 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2860 u64 end = start + PAGE_CACHE_SIZE - 1;
2861 int ret = 1;
2862
Chris Mason211f90e2008-07-18 11:56:15 -04002863 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002864 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002865 ret = 0;
2866 else {
2867 if ((mask & GFP_NOFS) == GFP_NOFS)
2868 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002869 /*
2870 * at this point we can safely clear everything except the
2871 * locked bit and the nodatasum bit
2872 */
2873 clear_extent_bit(tree, start, end,
2874 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2875 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002876 }
2877 return ret;
2878}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002879
2880/*
Chris Masond1310b22008-01-24 16:13:08 -05002881 * a helper for releasepage. As long as there are no locked extents
2882 * in the range corresponding to the page, both state records and extent
2883 * map records are removed
2884 */
2885int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002886 struct extent_io_tree *tree, struct page *page,
2887 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002888{
2889 struct extent_map *em;
2890 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2891 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002892
Chris Mason70dec802008-01-29 09:59:12 -05002893 if ((mask & __GFP_WAIT) &&
2894 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002895 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002896 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002897 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002898 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002899 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002900 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002901 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002902 break;
2903 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002904 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2905 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002906 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002907 free_extent_map(em);
2908 break;
2909 }
2910 if (!test_range_bit(tree, em->start,
2911 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002912 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002913 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002914 remove_extent_mapping(map, em);
2915 /* once for the rb tree */
2916 free_extent_map(em);
2917 }
2918 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002919 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002920
2921 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002922 free_extent_map(em);
2923 }
Chris Masond1310b22008-01-24 16:13:08 -05002924 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002925 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002926}
Chris Masond1310b22008-01-24 16:13:08 -05002927
2928sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2929 get_extent_t *get_extent)
2930{
2931 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002932 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002933 u64 start = iblock << inode->i_blkbits;
2934 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002935 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002936 struct extent_map *em;
2937
Josef Bacik2ac55d42010-02-03 19:33:23 +00002938 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2939 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002940 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002941 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2942 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002943 if (!em || IS_ERR(em))
2944 return 0;
2945
Yan Zhengd899e052008-10-30 14:25:28 -04002946 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002947 goto out;
2948
2949 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002950out:
2951 free_extent_map(em);
2952 return sector;
2953}
2954
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002955int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2956 __u64 start, __u64 len, get_extent_t *get_extent)
2957{
2958 int ret;
2959 u64 off = start;
2960 u64 max = start + len;
2961 u32 flags = 0;
2962 u64 disko = 0;
2963 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002964 struct extent_state *cached_state = NULL;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002965 int end = 0;
2966 u64 em_start = 0, em_len = 0;
2967 unsigned long emflags;
2968 ret = 0;
2969
2970 if (len == 0)
2971 return -EINVAL;
2972
Josef Bacik2ac55d42010-02-03 19:33:23 +00002973 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2974 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002975 em = get_extent(inode, NULL, 0, off, max - off, 0);
2976 if (!em)
2977 goto out;
2978 if (IS_ERR(em)) {
2979 ret = PTR_ERR(em);
2980 goto out;
2981 }
2982 while (!end) {
2983 off = em->start + em->len;
2984 if (off >= max)
2985 end = 1;
2986
2987 em_start = em->start;
2988 em_len = em->len;
2989
2990 disko = 0;
2991 flags = 0;
2992
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002993 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002994 end = 1;
2995 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002996 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002997 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002998 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002999 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3000 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003001 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003002 flags |= (FIEMAP_EXTENT_DELALLOC |
3003 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003004 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003005 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003006 }
3007 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3008 flags |= FIEMAP_EXTENT_ENCODED;
3009
3010 emflags = em->flags;
3011 free_extent_map(em);
3012 em = NULL;
3013
3014 if (!end) {
3015 em = get_extent(inode, NULL, 0, off, max - off, 0);
3016 if (!em)
3017 goto out;
3018 if (IS_ERR(em)) {
3019 ret = PTR_ERR(em);
3020 goto out;
3021 }
3022 emflags = em->flags;
3023 }
3024 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3025 flags |= FIEMAP_EXTENT_LAST;
3026 end = 1;
3027 }
3028
3029 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3030 em_len, flags);
3031 if (ret)
3032 goto out_free;
3033 }
3034out_free:
3035 free_extent_map(em);
3036out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003037 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3038 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003039 return ret;
3040}
3041
Chris Masond1310b22008-01-24 16:13:08 -05003042static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3043 unsigned long i)
3044{
3045 struct page *p;
3046 struct address_space *mapping;
3047
3048 if (i == 0)
3049 return eb->first_page;
3050 i += eb->start >> PAGE_CACHE_SHIFT;
3051 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003052 if (!mapping)
3053 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003054
3055 /*
3056 * extent_buffer_page is only called after pinning the page
3057 * by increasing the reference count. So we know the page must
3058 * be in the radix tree.
3059 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003060 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003061 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003062 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003063
Chris Masond1310b22008-01-24 16:13:08 -05003064 return p;
3065}
3066
Chris Mason6af118ce2008-07-22 11:18:07 -04003067static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003068{
Chris Mason6af118ce2008-07-22 11:18:07 -04003069 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3070 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003071}
3072
Chris Masond1310b22008-01-24 16:13:08 -05003073static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3074 u64 start,
3075 unsigned long len,
3076 gfp_t mask)
3077{
3078 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003079#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003080 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003081#endif
Chris Masond1310b22008-01-24 16:13:08 -05003082
Chris Masond1310b22008-01-24 16:13:08 -05003083 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003084 eb->start = start;
3085 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003086 spin_lock_init(&eb->lock);
3087 init_waitqueue_head(&eb->lock_wq);
3088
Chris Mason39351272009-02-04 09:24:05 -05003089#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003090 spin_lock_irqsave(&leak_lock, flags);
3091 list_add(&eb->leak_list, &buffers);
3092 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003093#endif
Chris Masond1310b22008-01-24 16:13:08 -05003094 atomic_set(&eb->refs, 1);
3095
3096 return eb;
3097}
3098
3099static void __free_extent_buffer(struct extent_buffer *eb)
3100{
Chris Mason39351272009-02-04 09:24:05 -05003101#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003102 unsigned long flags;
3103 spin_lock_irqsave(&leak_lock, flags);
3104 list_del(&eb->leak_list);
3105 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003106#endif
Chris Masond1310b22008-01-24 16:13:08 -05003107 kmem_cache_free(extent_buffer_cache, eb);
3108}
3109
3110struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3111 u64 start, unsigned long len,
3112 struct page *page0,
3113 gfp_t mask)
3114{
3115 unsigned long num_pages = num_extent_pages(start, len);
3116 unsigned long i;
3117 unsigned long index = start >> PAGE_CACHE_SHIFT;
3118 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003119 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003120 struct page *p;
3121 struct address_space *mapping = tree->mapping;
3122 int uptodate = 1;
3123
Chris Mason6af118ce2008-07-22 11:18:07 -04003124 spin_lock(&tree->buffer_lock);
3125 eb = buffer_search(tree, start);
3126 if (eb) {
3127 atomic_inc(&eb->refs);
3128 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003129 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003130 return eb;
3131 }
3132 spin_unlock(&tree->buffer_lock);
3133
Chris Masond1310b22008-01-24 16:13:08 -05003134 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003135 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003136 return NULL;
3137
Chris Masond1310b22008-01-24 16:13:08 -05003138 if (page0) {
3139 eb->first_page = page0;
3140 i = 1;
3141 index++;
3142 page_cache_get(page0);
3143 mark_page_accessed(page0);
3144 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003145 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003146 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003147 } else {
3148 i = 0;
3149 }
3150 for (; i < num_pages; i++, index++) {
3151 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3152 if (!p) {
3153 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003154 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003155 }
3156 set_page_extent_mapped(p);
3157 mark_page_accessed(p);
3158 if (i == 0) {
3159 eb->first_page = p;
3160 set_page_extent_head(p, len);
3161 } else {
3162 set_page_private(p, EXTENT_PAGE_PRIVATE);
3163 }
3164 if (!PageUptodate(p))
3165 uptodate = 0;
3166 unlock_page(p);
3167 }
3168 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003169 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003170
Chris Mason6af118ce2008-07-22 11:18:07 -04003171 spin_lock(&tree->buffer_lock);
3172 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3173 if (exists) {
3174 /* add one reference for the caller */
3175 atomic_inc(&exists->refs);
3176 spin_unlock(&tree->buffer_lock);
3177 goto free_eb;
3178 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003179 /* add one reference for the tree */
3180 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003181 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003182 return eb;
3183
Chris Mason6af118ce2008-07-22 11:18:07 -04003184free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003185 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003186 return exists;
3187 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003188 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003189 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003190 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003191 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003192}
Chris Masond1310b22008-01-24 16:13:08 -05003193
3194struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3195 u64 start, unsigned long len,
3196 gfp_t mask)
3197{
Chris Masond1310b22008-01-24 16:13:08 -05003198 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003199
Chris Mason6af118ce2008-07-22 11:18:07 -04003200 spin_lock(&tree->buffer_lock);
3201 eb = buffer_search(tree, start);
3202 if (eb)
3203 atomic_inc(&eb->refs);
3204 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003205
Josef Bacik0f9dd462008-09-23 13:14:11 -04003206 if (eb)
3207 mark_page_accessed(eb->first_page);
3208
Chris Masond1310b22008-01-24 16:13:08 -05003209 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003210}
Chris Masond1310b22008-01-24 16:13:08 -05003211
3212void free_extent_buffer(struct extent_buffer *eb)
3213{
Chris Masond1310b22008-01-24 16:13:08 -05003214 if (!eb)
3215 return;
3216
3217 if (!atomic_dec_and_test(&eb->refs))
3218 return;
3219
Chris Mason6af118ce2008-07-22 11:18:07 -04003220 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003221}
Chris Masond1310b22008-01-24 16:13:08 -05003222
3223int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3224 struct extent_buffer *eb)
3225{
Chris Masond1310b22008-01-24 16:13:08 -05003226 unsigned long i;
3227 unsigned long num_pages;
3228 struct page *page;
3229
Chris Masond1310b22008-01-24 16:13:08 -05003230 num_pages = num_extent_pages(eb->start, eb->len);
3231
3232 for (i = 0; i < num_pages; i++) {
3233 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003234 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003235 continue;
3236
Chris Masona61e6f22008-07-22 11:18:08 -04003237 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003238 if (i == 0)
3239 set_page_extent_head(page, eb->len);
3240 else
3241 set_page_private(page, EXTENT_PAGE_PRIVATE);
3242
Chris Masond1310b22008-01-24 16:13:08 -05003243 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003244 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003245 if (!PageDirty(page)) {
3246 radix_tree_tag_clear(&page->mapping->page_tree,
3247 page_index(page),
3248 PAGECACHE_TAG_DIRTY);
3249 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003250 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003251 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003252 }
3253 return 0;
3254}
Chris Masond1310b22008-01-24 16:13:08 -05003255
3256int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3257 struct extent_buffer *eb)
3258{
3259 return wait_on_extent_writeback(tree, eb->start,
3260 eb->start + eb->len - 1);
3261}
Chris Masond1310b22008-01-24 16:13:08 -05003262
3263int set_extent_buffer_dirty(struct extent_io_tree *tree,
3264 struct extent_buffer *eb)
3265{
3266 unsigned long i;
3267 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003268 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003269
Chris Masonb9473432009-03-13 11:00:37 -04003270 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003271 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003272 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003273 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003274 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003275}
Chris Masond1310b22008-01-24 16:13:08 -05003276
Chris Mason1259ab72008-05-12 13:39:03 -04003277int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003278 struct extent_buffer *eb,
3279 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003280{
3281 unsigned long i;
3282 struct page *page;
3283 unsigned long num_pages;
3284
3285 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003286 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003287
3288 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003289 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003290 for (i = 0; i < num_pages; i++) {
3291 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003292 if (page)
3293 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003294 }
3295 return 0;
3296}
3297
Chris Masond1310b22008-01-24 16:13:08 -05003298int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3299 struct extent_buffer *eb)
3300{
3301 unsigned long i;
3302 struct page *page;
3303 unsigned long num_pages;
3304
3305 num_pages = num_extent_pages(eb->start, eb->len);
3306
3307 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3308 GFP_NOFS);
3309 for (i = 0; i < num_pages; i++) {
3310 page = extent_buffer_page(eb, i);
3311 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3312 ((i == num_pages - 1) &&
3313 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3314 check_page_uptodate(tree, page);
3315 continue;
3316 }
3317 SetPageUptodate(page);
3318 }
3319 return 0;
3320}
Chris Masond1310b22008-01-24 16:13:08 -05003321
Chris Masonce9adaa2008-04-09 16:28:12 -04003322int extent_range_uptodate(struct extent_io_tree *tree,
3323 u64 start, u64 end)
3324{
3325 struct page *page;
3326 int ret;
3327 int pg_uptodate = 1;
3328 int uptodate;
3329 unsigned long index;
3330
Chris Mason9655d292009-09-02 15:22:30 -04003331 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003332 if (ret)
3333 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003334 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003335 index = start >> PAGE_CACHE_SHIFT;
3336 page = find_get_page(tree->mapping, index);
3337 uptodate = PageUptodate(page);
3338 page_cache_release(page);
3339 if (!uptodate) {
3340 pg_uptodate = 0;
3341 break;
3342 }
3343 start += PAGE_CACHE_SIZE;
3344 }
3345 return pg_uptodate;
3346}
3347
Chris Masond1310b22008-01-24 16:13:08 -05003348int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003349 struct extent_buffer *eb,
3350 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003351{
Chris Mason728131d2008-04-09 16:28:12 -04003352 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003353 unsigned long num_pages;
3354 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003355 struct page *page;
3356 int pg_uptodate = 1;
3357
Chris Masonb4ce94d2009-02-04 09:25:08 -05003358 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003359 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003360
Chris Mason42352982008-04-28 16:40:52 -04003361 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003362 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003363 if (ret)
3364 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003365
3366 num_pages = num_extent_pages(eb->start, eb->len);
3367 for (i = 0; i < num_pages; i++) {
3368 page = extent_buffer_page(eb, i);
3369 if (!PageUptodate(page)) {
3370 pg_uptodate = 0;
3371 break;
3372 }
3373 }
Chris Mason42352982008-04-28 16:40:52 -04003374 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003375}
Chris Masond1310b22008-01-24 16:13:08 -05003376
3377int read_extent_buffer_pages(struct extent_io_tree *tree,
3378 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003379 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003380 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003381{
3382 unsigned long i;
3383 unsigned long start_i;
3384 struct page *page;
3385 int err;
3386 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003387 int locked_pages = 0;
3388 int all_uptodate = 1;
3389 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003390 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003391 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003392 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003393
Chris Masonb4ce94d2009-02-04 09:25:08 -05003394 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003395 return 0;
3396
Chris Masonce9adaa2008-04-09 16:28:12 -04003397 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003398 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003399 return 0;
3400 }
3401
3402 if (start) {
3403 WARN_ON(start < eb->start);
3404 start_i = (start >> PAGE_CACHE_SHIFT) -
3405 (eb->start >> PAGE_CACHE_SHIFT);
3406 } else {
3407 start_i = 0;
3408 }
3409
3410 num_pages = num_extent_pages(eb->start, eb->len);
3411 for (i = start_i; i < num_pages; i++) {
3412 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003413 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003414 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003415 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003416 } else {
3417 lock_page(page);
3418 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003419 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003420 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003421 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003422 }
3423 if (all_uptodate) {
3424 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003425 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003426 goto unlock_exit;
3427 }
3428
3429 for (i = start_i; i < num_pages; i++) {
3430 page = extent_buffer_page(eb, i);
3431 if (inc_all_pages)
3432 page_cache_get(page);
3433 if (!PageUptodate(page)) {
3434 if (start_i == 0)
3435 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003436 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003437 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003438 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003439 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003440 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003441 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003442 } else {
3443 unlock_page(page);
3444 }
3445 }
3446
Chris Masona86c12c2008-02-07 10:50:54 -05003447 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003448 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003449
Chris Masond3977122009-01-05 21:25:51 -05003450 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003451 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003452
Chris Masond1310b22008-01-24 16:13:08 -05003453 for (i = start_i; i < num_pages; i++) {
3454 page = extent_buffer_page(eb, i);
3455 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003456 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003457 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003458 }
Chris Masond3977122009-01-05 21:25:51 -05003459
Chris Masond1310b22008-01-24 16:13:08 -05003460 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003461 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003462 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003463
3464unlock_exit:
3465 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003466 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003467 page = extent_buffer_page(eb, i);
3468 i++;
3469 unlock_page(page);
3470 locked_pages--;
3471 }
3472 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003473}
Chris Masond1310b22008-01-24 16:13:08 -05003474
3475void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3476 unsigned long start,
3477 unsigned long len)
3478{
3479 size_t cur;
3480 size_t offset;
3481 struct page *page;
3482 char *kaddr;
3483 char *dst = (char *)dstv;
3484 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3485 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003486
3487 WARN_ON(start > eb->len);
3488 WARN_ON(start + len > eb->start + eb->len);
3489
3490 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3491
Chris Masond3977122009-01-05 21:25:51 -05003492 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003493 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003494
3495 cur = min(len, (PAGE_CACHE_SIZE - offset));
3496 kaddr = kmap_atomic(page, KM_USER1);
3497 memcpy(dst, kaddr + offset, cur);
3498 kunmap_atomic(kaddr, KM_USER1);
3499
3500 dst += cur;
3501 len -= cur;
3502 offset = 0;
3503 i++;
3504 }
3505}
Chris Masond1310b22008-01-24 16:13:08 -05003506
3507int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3508 unsigned long min_len, char **token, char **map,
3509 unsigned long *map_start,
3510 unsigned long *map_len, int km)
3511{
3512 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3513 char *kaddr;
3514 struct page *p;
3515 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3516 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3517 unsigned long end_i = (start_offset + start + min_len - 1) >>
3518 PAGE_CACHE_SHIFT;
3519
3520 if (i != end_i)
3521 return -EINVAL;
3522
3523 if (i == 0) {
3524 offset = start_offset;
3525 *map_start = 0;
3526 } else {
3527 offset = 0;
3528 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3529 }
Chris Masond3977122009-01-05 21:25:51 -05003530
Chris Masond1310b22008-01-24 16:13:08 -05003531 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003532 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3533 "wanted %lu %lu\n", (unsigned long long)eb->start,
3534 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003535 WARN_ON(1);
3536 }
3537
3538 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003539 kaddr = kmap_atomic(p, km);
3540 *token = kaddr;
3541 *map = kaddr + offset;
3542 *map_len = PAGE_CACHE_SIZE - offset;
3543 return 0;
3544}
Chris Masond1310b22008-01-24 16:13:08 -05003545
3546int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3547 unsigned long min_len,
3548 char **token, char **map,
3549 unsigned long *map_start,
3550 unsigned long *map_len, int km)
3551{
3552 int err;
3553 int save = 0;
3554 if (eb->map_token) {
3555 unmap_extent_buffer(eb, eb->map_token, km);
3556 eb->map_token = NULL;
3557 save = 1;
3558 }
3559 err = map_private_extent_buffer(eb, start, min_len, token, map,
3560 map_start, map_len, km);
3561 if (!err && save) {
3562 eb->map_token = *token;
3563 eb->kaddr = *map;
3564 eb->map_start = *map_start;
3565 eb->map_len = *map_len;
3566 }
3567 return err;
3568}
Chris Masond1310b22008-01-24 16:13:08 -05003569
3570void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3571{
3572 kunmap_atomic(token, km);
3573}
Chris Masond1310b22008-01-24 16:13:08 -05003574
3575int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3576 unsigned long start,
3577 unsigned long len)
3578{
3579 size_t cur;
3580 size_t offset;
3581 struct page *page;
3582 char *kaddr;
3583 char *ptr = (char *)ptrv;
3584 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3585 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3586 int ret = 0;
3587
3588 WARN_ON(start > eb->len);
3589 WARN_ON(start + len > eb->start + eb->len);
3590
3591 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3592
Chris Masond3977122009-01-05 21:25:51 -05003593 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003594 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003595
3596 cur = min(len, (PAGE_CACHE_SIZE - offset));
3597
3598 kaddr = kmap_atomic(page, KM_USER0);
3599 ret = memcmp(ptr, kaddr + offset, cur);
3600 kunmap_atomic(kaddr, KM_USER0);
3601 if (ret)
3602 break;
3603
3604 ptr += cur;
3605 len -= cur;
3606 offset = 0;
3607 i++;
3608 }
3609 return ret;
3610}
Chris Masond1310b22008-01-24 16:13:08 -05003611
3612void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3613 unsigned long start, unsigned long len)
3614{
3615 size_t cur;
3616 size_t offset;
3617 struct page *page;
3618 char *kaddr;
3619 char *src = (char *)srcv;
3620 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3621 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3622
3623 WARN_ON(start > eb->len);
3624 WARN_ON(start + len > eb->start + eb->len);
3625
3626 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3627
Chris Masond3977122009-01-05 21:25:51 -05003628 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003629 page = extent_buffer_page(eb, i);
3630 WARN_ON(!PageUptodate(page));
3631
3632 cur = min(len, PAGE_CACHE_SIZE - offset);
3633 kaddr = kmap_atomic(page, KM_USER1);
3634 memcpy(kaddr + offset, src, cur);
3635 kunmap_atomic(kaddr, KM_USER1);
3636
3637 src += cur;
3638 len -= cur;
3639 offset = 0;
3640 i++;
3641 }
3642}
Chris Masond1310b22008-01-24 16:13:08 -05003643
3644void memset_extent_buffer(struct extent_buffer *eb, char c,
3645 unsigned long start, unsigned long len)
3646{
3647 size_t cur;
3648 size_t offset;
3649 struct page *page;
3650 char *kaddr;
3651 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3652 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3653
3654 WARN_ON(start > eb->len);
3655 WARN_ON(start + len > eb->start + eb->len);
3656
3657 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3658
Chris Masond3977122009-01-05 21:25:51 -05003659 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003660 page = extent_buffer_page(eb, i);
3661 WARN_ON(!PageUptodate(page));
3662
3663 cur = min(len, PAGE_CACHE_SIZE - offset);
3664 kaddr = kmap_atomic(page, KM_USER0);
3665 memset(kaddr + offset, c, cur);
3666 kunmap_atomic(kaddr, KM_USER0);
3667
3668 len -= cur;
3669 offset = 0;
3670 i++;
3671 }
3672}
Chris Masond1310b22008-01-24 16:13:08 -05003673
3674void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3675 unsigned long dst_offset, unsigned long src_offset,
3676 unsigned long len)
3677{
3678 u64 dst_len = dst->len;
3679 size_t cur;
3680 size_t offset;
3681 struct page *page;
3682 char *kaddr;
3683 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3684 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3685
3686 WARN_ON(src->len != dst_len);
3687
3688 offset = (start_offset + dst_offset) &
3689 ((unsigned long)PAGE_CACHE_SIZE - 1);
3690
Chris Masond3977122009-01-05 21:25:51 -05003691 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003692 page = extent_buffer_page(dst, i);
3693 WARN_ON(!PageUptodate(page));
3694
3695 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3696
3697 kaddr = kmap_atomic(page, KM_USER0);
3698 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3699 kunmap_atomic(kaddr, KM_USER0);
3700
3701 src_offset += cur;
3702 len -= cur;
3703 offset = 0;
3704 i++;
3705 }
3706}
Chris Masond1310b22008-01-24 16:13:08 -05003707
3708static void move_pages(struct page *dst_page, struct page *src_page,
3709 unsigned long dst_off, unsigned long src_off,
3710 unsigned long len)
3711{
3712 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3713 if (dst_page == src_page) {
3714 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3715 } else {
3716 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3717 char *p = dst_kaddr + dst_off + len;
3718 char *s = src_kaddr + src_off + len;
3719
3720 while (len--)
3721 *--p = *--s;
3722
3723 kunmap_atomic(src_kaddr, KM_USER1);
3724 }
3725 kunmap_atomic(dst_kaddr, KM_USER0);
3726}
3727
3728static void copy_pages(struct page *dst_page, struct page *src_page,
3729 unsigned long dst_off, unsigned long src_off,
3730 unsigned long len)
3731{
3732 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3733 char *src_kaddr;
3734
3735 if (dst_page != src_page)
3736 src_kaddr = kmap_atomic(src_page, KM_USER1);
3737 else
3738 src_kaddr = dst_kaddr;
3739
3740 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3741 kunmap_atomic(dst_kaddr, KM_USER0);
3742 if (dst_page != src_page)
3743 kunmap_atomic(src_kaddr, KM_USER1);
3744}
3745
3746void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3747 unsigned long src_offset, unsigned long len)
3748{
3749 size_t cur;
3750 size_t dst_off_in_page;
3751 size_t src_off_in_page;
3752 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3753 unsigned long dst_i;
3754 unsigned long src_i;
3755
3756 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003757 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3758 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003759 BUG_ON(1);
3760 }
3761 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003762 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3763 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003764 BUG_ON(1);
3765 }
3766
Chris Masond3977122009-01-05 21:25:51 -05003767 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003768 dst_off_in_page = (start_offset + dst_offset) &
3769 ((unsigned long)PAGE_CACHE_SIZE - 1);
3770 src_off_in_page = (start_offset + src_offset) &
3771 ((unsigned long)PAGE_CACHE_SIZE - 1);
3772
3773 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3774 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3775
3776 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3777 src_off_in_page));
3778 cur = min_t(unsigned long, cur,
3779 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3780
3781 copy_pages(extent_buffer_page(dst, dst_i),
3782 extent_buffer_page(dst, src_i),
3783 dst_off_in_page, src_off_in_page, cur);
3784
3785 src_offset += cur;
3786 dst_offset += cur;
3787 len -= cur;
3788 }
3789}
Chris Masond1310b22008-01-24 16:13:08 -05003790
3791void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3792 unsigned long src_offset, unsigned long len)
3793{
3794 size_t cur;
3795 size_t dst_off_in_page;
3796 size_t src_off_in_page;
3797 unsigned long dst_end = dst_offset + len - 1;
3798 unsigned long src_end = src_offset + len - 1;
3799 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3800 unsigned long dst_i;
3801 unsigned long src_i;
3802
3803 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003804 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3805 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003806 BUG_ON(1);
3807 }
3808 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003809 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3810 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003811 BUG_ON(1);
3812 }
3813 if (dst_offset < src_offset) {
3814 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3815 return;
3816 }
Chris Masond3977122009-01-05 21:25:51 -05003817 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003818 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3819 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3820
3821 dst_off_in_page = (start_offset + dst_end) &
3822 ((unsigned long)PAGE_CACHE_SIZE - 1);
3823 src_off_in_page = (start_offset + src_end) &
3824 ((unsigned long)PAGE_CACHE_SIZE - 1);
3825
3826 cur = min_t(unsigned long, len, src_off_in_page + 1);
3827 cur = min(cur, dst_off_in_page + 1);
3828 move_pages(extent_buffer_page(dst, dst_i),
3829 extent_buffer_page(dst, src_i),
3830 dst_off_in_page - cur + 1,
3831 src_off_in_page - cur + 1, cur);
3832
3833 dst_end -= cur;
3834 src_end -= cur;
3835 len -= cur;
3836 }
3837}
Chris Mason6af118ce2008-07-22 11:18:07 -04003838
3839int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3840{
3841 u64 start = page_offset(page);
3842 struct extent_buffer *eb;
3843 int ret = 1;
3844 unsigned long i;
3845 unsigned long num_pages;
3846
3847 spin_lock(&tree->buffer_lock);
3848 eb = buffer_search(tree, start);
3849 if (!eb)
3850 goto out;
3851
3852 if (atomic_read(&eb->refs) > 1) {
3853 ret = 0;
3854 goto out;
3855 }
Chris Masonb9473432009-03-13 11:00:37 -04003856 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3857 ret = 0;
3858 goto out;
3859 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003860 /* at this point we can safely release the extent buffer */
3861 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003862 for (i = 0; i < num_pages; i++)
3863 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003864 rb_erase(&eb->rb_node, &tree->buffer);
3865 __free_extent_buffer(eb);
3866out:
3867 spin_unlock(&tree->buffer_lock);
3868 return ret;
3869}