blob: 561262d35689f463875e6bf4214f12ff5725b775 [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>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050020
Chris Masond1310b22008-01-24 16:13:08 -050021static struct kmem_cache *extent_state_cache;
22static struct kmem_cache *extent_buffer_cache;
23
24static LIST_HEAD(buffers);
25static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040026
Chris Masonb47eda82008-11-10 12:34:40 -050027#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050028#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050029static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040030#endif
Chris Masond1310b22008-01-24 16:13:08 -050031
Chris Masond1310b22008-01-24 16:13:08 -050032#define BUFFER_LRU_MAX 64
33
34struct tree_entry {
35 u64 start;
36 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050037 struct rb_node rb_node;
38};
39
40struct extent_page_data {
41 struct bio *bio;
42 struct extent_io_tree *tree;
43 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050044
45 /* tells writepage not to lock the state bits for this range
46 * it still does the unlocking
47 */
Chris Masonffbd5172009-04-20 15:50:09 -040048 unsigned int extent_locked:1;
49
50 /* tells the submit_bio code to use a WRITE_SYNC */
51 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050052};
53
54int __init extent_io_init(void)
55{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020056 extent_state_cache = kmem_cache_create("extent_state",
57 sizeof(struct extent_state), 0,
58 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050059 if (!extent_state_cache)
60 return -ENOMEM;
61
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020062 extent_buffer_cache = kmem_cache_create("extent_buffers",
63 sizeof(struct extent_buffer), 0,
64 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050065 if (!extent_buffer_cache)
66 goto free_state_cache;
67 return 0;
68
69free_state_cache:
70 kmem_cache_destroy(extent_state_cache);
71 return -ENOMEM;
72}
73
74void extent_io_exit(void)
75{
76 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040077 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050078
79 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040080 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050081 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
82 "state %lu in tree %p refs %d\n",
83 (unsigned long long)state->start,
84 (unsigned long long)state->end,
85 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040086 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050087 kmem_cache_free(extent_state_cache, state);
88
89 }
90
Chris Mason2d2ae542008-03-26 16:24:23 -040091 while (!list_empty(&buffers)) {
92 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050093 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
94 "refs %d\n", (unsigned long long)eb->start,
95 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040096 list_del(&eb->leak_list);
97 kmem_cache_free(extent_buffer_cache, eb);
98 }
Chris Masond1310b22008-01-24 16:13:08 -050099 if (extent_state_cache)
100 kmem_cache_destroy(extent_state_cache);
101 if (extent_buffer_cache)
102 kmem_cache_destroy(extent_buffer_cache);
103}
104
105void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200106 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500107{
Eric Paris6bef4d32010-02-23 19:43:04 +0000108 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400109 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500110 tree->ops = NULL;
111 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500112 spin_lock_init(&tree->lock);
Chris Mason6af118c2008-07-22 11:18:07 -0400113 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500114 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500115}
Chris Masond1310b22008-01-24 16:13:08 -0500116
Christoph Hellwigb2950862008-12-02 09:54:17 -0500117static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500118{
119 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500120#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400121 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400122#endif
Chris Masond1310b22008-01-24 16:13:08 -0500123
124 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400125 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500126 return state;
127 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500128 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500129 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500130#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400131 spin_lock_irqsave(&leak_lock, flags);
132 list_add(&state->leak_list, &states);
133 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400134#endif
Chris Masond1310b22008-01-24 16:13:08 -0500135 atomic_set(&state->refs, 1);
136 init_waitqueue_head(&state->wq);
137 return state;
138}
Chris Masond1310b22008-01-24 16:13:08 -0500139
Chris Mason4845e442010-05-25 20:56:50 -0400140void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500141{
Chris Masond1310b22008-01-24 16:13:08 -0500142 if (!state)
143 return;
144 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500145#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400146 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400147#endif
Chris Mason70dec802008-01-29 09:59:12 -0500148 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500149#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400150 spin_lock_irqsave(&leak_lock, flags);
151 list_del(&state->leak_list);
152 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400153#endif
Chris Masond1310b22008-01-24 16:13:08 -0500154 kmem_cache_free(extent_state_cache, state);
155 }
156}
Chris Masond1310b22008-01-24 16:13:08 -0500157
158static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
159 struct rb_node *node)
160{
Chris Masond3977122009-01-05 21:25:51 -0500161 struct rb_node **p = &root->rb_node;
162 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500163 struct tree_entry *entry;
164
Chris Masond3977122009-01-05 21:25:51 -0500165 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500166 parent = *p;
167 entry = rb_entry(parent, struct tree_entry, rb_node);
168
169 if (offset < entry->start)
170 p = &(*p)->rb_left;
171 else if (offset > entry->end)
172 p = &(*p)->rb_right;
173 else
174 return parent;
175 }
176
177 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500178 rb_link_node(node, parent, p);
179 rb_insert_color(node, root);
180 return NULL;
181}
182
Chris Mason80ea96b2008-02-01 14:51:59 -0500183static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500184 struct rb_node **prev_ret,
185 struct rb_node **next_ret)
186{
Chris Mason80ea96b2008-02-01 14:51:59 -0500187 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500188 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500189 struct rb_node *prev = NULL;
190 struct rb_node *orig_prev = NULL;
191 struct tree_entry *entry;
192 struct tree_entry *prev_entry = NULL;
193
Chris Masond3977122009-01-05 21:25:51 -0500194 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500195 entry = rb_entry(n, struct tree_entry, rb_node);
196 prev = n;
197 prev_entry = entry;
198
199 if (offset < entry->start)
200 n = n->rb_left;
201 else if (offset > entry->end)
202 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500203 else
Chris Masond1310b22008-01-24 16:13:08 -0500204 return n;
205 }
206
207 if (prev_ret) {
208 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500209 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500210 prev = rb_next(prev);
211 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
212 }
213 *prev_ret = prev;
214 prev = orig_prev;
215 }
216
217 if (next_ret) {
218 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500219 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500220 prev = rb_prev(prev);
221 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
222 }
223 *next_ret = prev;
224 }
225 return NULL;
226}
227
Chris Mason80ea96b2008-02-01 14:51:59 -0500228static inline struct rb_node *tree_search(struct extent_io_tree *tree,
229 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500230{
Chris Mason70dec802008-01-29 09:59:12 -0500231 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500232 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500233
Chris Mason80ea96b2008-02-01 14:51:59 -0500234 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500235 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500236 return prev;
237 return ret;
238}
239
Josef Bacik9ed74f22009-09-11 16:12:44 -0400240static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
241 struct extent_state *other)
242{
243 if (tree->ops && tree->ops->merge_extent_hook)
244 tree->ops->merge_extent_hook(tree->mapping->host, new,
245 other);
246}
247
Chris Masond1310b22008-01-24 16:13:08 -0500248/*
249 * utility function to look for merge candidates inside a given range.
250 * Any extents with matching state are merged together into a single
251 * extent in the tree. Extents with EXTENT_IO in their state field
252 * are not merged because the end_io handlers need to be able to do
253 * operations on them without sleeping (or doing allocations/splits).
254 *
255 * This should be called with the tree lock held.
256 */
257static int merge_state(struct extent_io_tree *tree,
258 struct extent_state *state)
259{
260 struct extent_state *other;
261 struct rb_node *other_node;
262
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400263 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500264 return 0;
265
266 other_node = rb_prev(&state->rb_node);
267 if (other_node) {
268 other = rb_entry(other_node, struct extent_state, rb_node);
269 if (other->end == state->start - 1 &&
270 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400271 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500272 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500273 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500274 rb_erase(&other->rb_node, &tree->state);
275 free_extent_state(other);
276 }
277 }
278 other_node = rb_next(&state->rb_node);
279 if (other_node) {
280 other = rb_entry(other_node, struct extent_state, rb_node);
281 if (other->start == state->end + 1 &&
282 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400283 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500284 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500285 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500286 rb_erase(&state->rb_node, &tree->state);
287 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400288 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500289 }
290 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400291
Chris Masond1310b22008-01-24 16:13:08 -0500292 return 0;
293}
294
Josef Bacik9ed74f22009-09-11 16:12:44 -0400295static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400296 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500297{
298 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400299 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400300 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500301 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400302
303 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500304}
305
306static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400307 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500308{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400309 if (tree->ops && tree->ops->clear_bit_hook)
310 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500311}
312
Chris Masond1310b22008-01-24 16:13:08 -0500313/*
314 * insert an extent_state struct into the tree. 'bits' are set on the
315 * struct before it is inserted.
316 *
317 * This may return -EEXIST if the extent is already there, in which case the
318 * state struct is freed.
319 *
320 * The tree lock is not taken internally. This is a utility function and
321 * probably isn't what you want to call (see set/clear_extent_bit).
322 */
323static int insert_state(struct extent_io_tree *tree,
324 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400325 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500326{
327 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400328 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400329 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500330
331 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500332 printk(KERN_ERR "btrfs end < start %llu %llu\n",
333 (unsigned long long)end,
334 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500335 WARN_ON(1);
336 }
Chris Masond1310b22008-01-24 16:13:08 -0500337 state->start = start;
338 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400339 ret = set_state_cb(tree, state, bits);
340 if (ret)
341 return ret;
342
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400343 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400344 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400345 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500346 node = tree_insert(&tree->state, end, &state->rb_node);
347 if (node) {
348 struct extent_state *found;
349 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500350 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
351 "%llu %llu\n", (unsigned long long)found->start,
352 (unsigned long long)found->end,
353 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500354 free_extent_state(state);
355 return -EEXIST;
356 }
Chris Mason70dec802008-01-29 09:59:12 -0500357 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500358 merge_state(tree, state);
359 return 0;
360}
361
Josef Bacik9ed74f22009-09-11 16:12:44 -0400362static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
363 u64 split)
364{
365 if (tree->ops && tree->ops->split_extent_hook)
366 return tree->ops->split_extent_hook(tree->mapping->host,
367 orig, split);
368 return 0;
369}
370
Chris Masond1310b22008-01-24 16:13:08 -0500371/*
372 * split a given extent state struct in two, inserting the preallocated
373 * struct 'prealloc' as the newly created second half. 'split' indicates an
374 * offset inside 'orig' where it should be split.
375 *
376 * Before calling,
377 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
378 * are two extent state structs in the tree:
379 * prealloc: [orig->start, split - 1]
380 * orig: [ split, orig->end ]
381 *
382 * The tree locks are not taken by this function. They need to be held
383 * by the caller.
384 */
385static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
386 struct extent_state *prealloc, u64 split)
387{
388 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400389
390 split_cb(tree, orig, split);
391
Chris Masond1310b22008-01-24 16:13:08 -0500392 prealloc->start = orig->start;
393 prealloc->end = split - 1;
394 prealloc->state = orig->state;
395 orig->start = split;
396
397 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
398 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500399 free_extent_state(prealloc);
400 return -EEXIST;
401 }
Chris Mason70dec802008-01-29 09:59:12 -0500402 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500403 return 0;
404}
405
406/*
407 * utility function to clear some bits in an extent state struct.
408 * it will optionally wake up any one waiting on this state (wake == 1), or
409 * forcibly remove the state from the tree (delete == 1).
410 *
411 * If no bits are set on the state struct after clearing things, the
412 * struct is freed and removed from the tree
413 */
414static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400415 struct extent_state *state,
416 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500417{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400419 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500420
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400421 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500422 u64 range = state->end - state->start + 1;
423 WARN_ON(range > tree->dirty_bytes);
424 tree->dirty_bytes -= range;
425 }
Chris Mason291d6732008-01-29 15:55:23 -0500426 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400427 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500428 if (wake)
429 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400430 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500431 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500432 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500433 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500434 free_extent_state(state);
435 } else {
436 WARN_ON(1);
437 }
438 } else {
439 merge_state(tree, state);
440 }
441 return ret;
442}
443
Xiao Guangrong82337672011-04-20 06:44:57 +0000444static struct extent_state *
445alloc_extent_state_atomic(struct extent_state *prealloc)
446{
447 if (!prealloc)
448 prealloc = alloc_extent_state(GFP_ATOMIC);
449
450 return prealloc;
451}
452
Chris Masond1310b22008-01-24 16:13:08 -0500453/*
454 * clear some bits on a range in the tree. This may require splitting
455 * or inserting elements in the tree, so the gfp mask is used to
456 * indicate which allocations or sleeping are allowed.
457 *
458 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
459 * the given range from the tree regardless of state (ie for truncate).
460 *
461 * the range [start, end] is inclusive.
462 *
463 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
464 * bits were already set, or zero if none of the bits were already set.
465 */
466int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400467 int bits, int wake, int delete,
468 struct extent_state **cached_state,
469 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500470{
471 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400472 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500473 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400474 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500475 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400476 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500477 int err;
478 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000479 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500480
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400481 if (delete)
482 bits |= ~EXTENT_CTLBITS;
483 bits |= EXTENT_FIRST_DELALLOC;
484
Josef Bacik2ac55d42010-02-03 19:33:23 +0000485 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
486 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500487again:
488 if (!prealloc && (mask & __GFP_WAIT)) {
489 prealloc = alloc_extent_state(mask);
490 if (!prealloc)
491 return -ENOMEM;
492 }
493
Chris Masoncad321a2008-12-17 14:51:42 -0500494 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400495 if (cached_state) {
496 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000497
498 if (clear) {
499 *cached_state = NULL;
500 cached_state = NULL;
501 }
502
Chris Mason42daec22009-09-23 19:51:09 -0400503 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000504 if (clear)
505 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400506 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400507 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400508 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000509 if (clear)
510 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400511 }
Chris Masond1310b22008-01-24 16:13:08 -0500512 /*
513 * this search will find the extents that end after
514 * our range starts
515 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500516 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500517 if (!node)
518 goto out;
519 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400520hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500521 if (state->start > end)
522 goto out;
523 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400524 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500525
526 /*
527 * | ---- desired range ---- |
528 * | state | or
529 * | ------------- state -------------- |
530 *
531 * We need to split the extent we found, and may flip
532 * bits on second half.
533 *
534 * If the extent we found extends past our range, we
535 * just split and search again. It'll get split again
536 * the next time though.
537 *
538 * If the extent we found is inside our range, we clear
539 * the desired bit on it.
540 */
541
542 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000543 prealloc = alloc_extent_state_atomic(prealloc);
544 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500545 err = split_state(tree, state, prealloc, start);
546 BUG_ON(err == -EEXIST);
547 prealloc = NULL;
548 if (err)
549 goto out;
550 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400551 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400552 if (last_end == (u64)-1)
553 goto out;
554 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500555 }
556 goto search_again;
557 }
558 /*
559 * | ---- desired range ---- |
560 * | state |
561 * We need to split the extent, and clear the bit
562 * on the first half
563 */
564 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000565 prealloc = alloc_extent_state_atomic(prealloc);
566 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500567 err = split_state(tree, state, prealloc, end + 1);
568 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500569 if (wake)
570 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400571
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400572 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400573
Chris Masond1310b22008-01-24 16:13:08 -0500574 prealloc = NULL;
575 goto out;
576 }
Chris Mason42daec22009-09-23 19:51:09 -0400577
Chris Mason2c64c532009-09-02 15:04:12 -0400578 if (state->end < end && prealloc && !need_resched())
579 next_node = rb_next(&state->rb_node);
580 else
581 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400582
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400583 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400584 if (last_end == (u64)-1)
585 goto out;
586 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400587 if (start <= end && next_node) {
588 state = rb_entry(next_node, struct extent_state,
589 rb_node);
590 if (state->start == start)
591 goto hit_next;
592 }
Chris Masond1310b22008-01-24 16:13:08 -0500593 goto search_again;
594
595out:
Chris Masoncad321a2008-12-17 14:51:42 -0500596 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500597 if (prealloc)
598 free_extent_state(prealloc);
599
600 return set;
601
602search_again:
603 if (start > end)
604 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500605 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500606 if (mask & __GFP_WAIT)
607 cond_resched();
608 goto again;
609}
Chris Masond1310b22008-01-24 16:13:08 -0500610
611static int wait_on_state(struct extent_io_tree *tree,
612 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500613 __releases(tree->lock)
614 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500615{
616 DEFINE_WAIT(wait);
617 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500618 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500619 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500620 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500621 finish_wait(&state->wq, &wait);
622 return 0;
623}
624
625/*
626 * waits for one or more bits to clear on a range in the state tree.
627 * The range [start, end] is inclusive.
628 * The tree lock is taken by this function
629 */
630int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
631{
632 struct extent_state *state;
633 struct rb_node *node;
634
Chris Masoncad321a2008-12-17 14:51:42 -0500635 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500636again:
637 while (1) {
638 /*
639 * this search will find all the extents that end after
640 * our range starts
641 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500642 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500643 if (!node)
644 break;
645
646 state = rb_entry(node, struct extent_state, rb_node);
647
648 if (state->start > end)
649 goto out;
650
651 if (state->state & bits) {
652 start = state->start;
653 atomic_inc(&state->refs);
654 wait_on_state(tree, state);
655 free_extent_state(state);
656 goto again;
657 }
658 start = state->end + 1;
659
660 if (start > end)
661 break;
662
663 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500664 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500665 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500666 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500667 }
668 }
669out:
Chris Masoncad321a2008-12-17 14:51:42 -0500670 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500671 return 0;
672}
Chris Masond1310b22008-01-24 16:13:08 -0500673
Josef Bacik9ed74f22009-09-11 16:12:44 -0400674static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500675 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400676 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500677{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400678 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400679 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400680
681 ret = set_state_cb(tree, state, bits);
682 if (ret)
683 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400684 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500685 u64 range = state->end - state->start + 1;
686 tree->dirty_bytes += range;
687 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400688 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400689
690 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500691}
692
Chris Mason2c64c532009-09-02 15:04:12 -0400693static void cache_state(struct extent_state *state,
694 struct extent_state **cached_ptr)
695{
696 if (cached_ptr && !(*cached_ptr)) {
697 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
698 *cached_ptr = state;
699 atomic_inc(&state->refs);
700 }
701 }
702}
703
Arne Jansen507903b2011-04-06 10:02:20 +0000704static void uncache_state(struct extent_state **cached_ptr)
705{
706 if (cached_ptr && (*cached_ptr)) {
707 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400708 *cached_ptr = NULL;
709 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000710 }
711}
712
Chris Masond1310b22008-01-24 16:13:08 -0500713/*
Chris Mason1edbb732009-09-02 13:24:36 -0400714 * set some bits on a range in the tree. This may require allocations or
715 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500716 *
Chris Mason1edbb732009-09-02 13:24:36 -0400717 * If any of the exclusive bits are set, this will fail with -EEXIST if some
718 * part of the range already has the desired bits set. The start of the
719 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500720 *
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500722 */
Chris Mason1edbb732009-09-02 13:24:36 -0400723
Chris Mason4845e442010-05-25 20:56:50 -0400724int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
725 int bits, int exclusive_bits, u64 *failed_start,
726 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500727{
728 struct extent_state *state;
729 struct extent_state *prealloc = NULL;
730 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500731 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500732 u64 last_start;
733 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400734
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400735 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500736again:
737 if (!prealloc && (mask & __GFP_WAIT)) {
738 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000739 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500740 }
741
Chris Masoncad321a2008-12-17 14:51:42 -0500742 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400743 if (cached_state && *cached_state) {
744 state = *cached_state;
745 if (state->start == start && state->tree) {
746 node = &state->rb_node;
747 goto hit_next;
748 }
749 }
Chris Masond1310b22008-01-24 16:13:08 -0500750 /*
751 * this search will find all the extents that end after
752 * our range starts.
753 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500754 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500755 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000756 prealloc = alloc_extent_state_atomic(prealloc);
757 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400758 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500759 prealloc = NULL;
760 BUG_ON(err == -EEXIST);
761 goto out;
762 }
Chris Masond1310b22008-01-24 16:13:08 -0500763 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400764hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500765 last_start = state->start;
766 last_end = state->end;
767
768 /*
769 * | ---- desired range ---- |
770 * | state |
771 *
772 * Just lock what we found and keep going
773 */
774 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400775 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400776 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500777 *failed_start = state->start;
778 err = -EEXIST;
779 goto out;
780 }
Chris Mason42daec22009-09-23 19:51:09 -0400781
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400782 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400783 if (err)
784 goto out;
785
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000786 next_node = rb_next(node);
Chris Mason2c64c532009-09-02 15:04:12 -0400787 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500788 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400789 if (last_end == (u64)-1)
790 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400791
Yan Zheng5c939df2009-05-27 09:16:03 -0400792 start = last_end + 1;
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000793 if (next_node && start < end && prealloc && !need_resched()) {
794 state = rb_entry(next_node, struct extent_state,
795 rb_node);
796 if (state->start == start)
797 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400798 }
Chris Masond1310b22008-01-24 16:13:08 -0500799 goto search_again;
800 }
801
802 /*
803 * | ---- desired range ---- |
804 * | state |
805 * or
806 * | ------------- state -------------- |
807 *
808 * We need to split the extent we found, and may flip bits on
809 * second half.
810 *
811 * If the extent we found extends past our
812 * range, we just split and search again. It'll get split
813 * again the next time though.
814 *
815 * If the extent we found is inside our range, we set the
816 * desired bit on it.
817 */
818 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400819 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500820 *failed_start = start;
821 err = -EEXIST;
822 goto out;
823 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000824
825 prealloc = alloc_extent_state_atomic(prealloc);
826 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500827 err = split_state(tree, state, prealloc, start);
828 BUG_ON(err == -EEXIST);
829 prealloc = NULL;
830 if (err)
831 goto out;
832 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400833 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400834 if (err)
835 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400836 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500837 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400838 if (last_end == (u64)-1)
839 goto out;
840 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500841 }
842 goto search_again;
843 }
844 /*
845 * | ---- desired range ---- |
846 * | state | or | state |
847 *
848 * There's a hole, we need to insert something in it and
849 * ignore the extent we found.
850 */
851 if (state->start > start) {
852 u64 this_end;
853 if (end < last_start)
854 this_end = end;
855 else
Chris Masond3977122009-01-05 21:25:51 -0500856 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000857
858 prealloc = alloc_extent_state_atomic(prealloc);
859 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000860
861 /*
862 * Avoid to free 'prealloc' if it can be merged with
863 * the later extent.
864 */
865 atomic_inc(&prealloc->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500866 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400867 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400868 BUG_ON(err == -EEXIST);
869 if (err) {
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000870 free_extent_state(prealloc);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400871 prealloc = NULL;
872 goto out;
873 }
Chris Mason2c64c532009-09-02 15:04:12 -0400874 cache_state(prealloc, cached_state);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000875 free_extent_state(prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500876 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500877 start = this_end + 1;
878 goto search_again;
879 }
880 /*
881 * | ---- desired range ---- |
882 * | state |
883 * We need to split the extent, and set the bit
884 * on the first half
885 */
886 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400887 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500888 *failed_start = start;
889 err = -EEXIST;
890 goto out;
891 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000892
893 prealloc = alloc_extent_state_atomic(prealloc);
894 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500895 err = split_state(tree, state, prealloc, end + 1);
896 BUG_ON(err == -EEXIST);
897
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400898 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400899 if (err) {
900 prealloc = NULL;
901 goto out;
902 }
Chris Mason2c64c532009-09-02 15:04:12 -0400903 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500904 merge_state(tree, prealloc);
905 prealloc = NULL;
906 goto out;
907 }
908
909 goto search_again;
910
911out:
Chris Masoncad321a2008-12-17 14:51:42 -0500912 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500913 if (prealloc)
914 free_extent_state(prealloc);
915
916 return err;
917
918search_again:
919 if (start > end)
920 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500921 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500922 if (mask & __GFP_WAIT)
923 cond_resched();
924 goto again;
925}
Chris Masond1310b22008-01-24 16:13:08 -0500926
927/* wrappers around set/clear extent bit */
928int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
929 gfp_t mask)
930{
931 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400932 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500933}
Chris Masond1310b22008-01-24 16:13:08 -0500934
935int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
936 int bits, gfp_t mask)
937{
938 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400939 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500940}
Chris Masond1310b22008-01-24 16:13:08 -0500941
942int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
943 int bits, gfp_t mask)
944{
Chris Mason2c64c532009-09-02 15:04:12 -0400945 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500946}
Chris Masond1310b22008-01-24 16:13:08 -0500947
948int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000949 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500950{
951 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400952 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000953 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500954}
Chris Masond1310b22008-01-24 16:13:08 -0500955
956int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
957 gfp_t mask)
958{
959 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400960 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400961 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500962}
Chris Masond1310b22008-01-24 16:13:08 -0500963
964int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
965 gfp_t mask)
966{
967 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400968 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500969}
Chris Masond1310b22008-01-24 16:13:08 -0500970
Chris Masond1310b22008-01-24 16:13:08 -0500971int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000972 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500973{
Arne Jansen507903b2011-04-06 10:02:20 +0000974 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
975 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500976}
Chris Masond1310b22008-01-24 16:13:08 -0500977
Chris Masond3977122009-01-05 21:25:51 -0500978static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000979 u64 end, struct extent_state **cached_state,
980 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500981{
Chris Mason2c64c532009-09-02 15:04:12 -0400982 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000983 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500984}
Chris Masond1310b22008-01-24 16:13:08 -0500985
Chris Masond352ac62008-09-29 15:18:18 -0400986/*
987 * either insert or lock state struct between start and end use mask to tell
988 * us if waiting is desired.
989 */
Chris Mason1edbb732009-09-02 13:24:36 -0400990int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400991 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500992{
993 int err;
994 u64 failed_start;
995 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400996 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400997 EXTENT_LOCKED, &failed_start,
998 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500999 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1000 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1001 start = failed_start;
1002 } else {
1003 break;
1004 }
1005 WARN_ON(start > end);
1006 }
1007 return err;
1008}
Chris Masond1310b22008-01-24 16:13:08 -05001009
Chris Mason1edbb732009-09-02 13:24:36 -04001010int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1011{
Chris Mason2c64c532009-09-02 15:04:12 -04001012 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001013}
1014
Josef Bacik25179202008-10-29 14:49:05 -04001015int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1016 gfp_t mask)
1017{
1018 int err;
1019 u64 failed_start;
1020
Chris Mason2c64c532009-09-02 15:04:12 -04001021 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1022 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001023 if (err == -EEXIST) {
1024 if (failed_start > start)
1025 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001026 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001027 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001028 }
Josef Bacik25179202008-10-29 14:49:05 -04001029 return 1;
1030}
Josef Bacik25179202008-10-29 14:49:05 -04001031
Chris Mason2c64c532009-09-02 15:04:12 -04001032int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1033 struct extent_state **cached, gfp_t mask)
1034{
1035 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1036 mask);
1037}
1038
Arne Jansen507903b2011-04-06 10:02:20 +00001039int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001040{
Chris Mason2c64c532009-09-02 15:04:12 -04001041 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1042 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001043}
Chris Masond1310b22008-01-24 16:13:08 -05001044
1045/*
Chris Masond1310b22008-01-24 16:13:08 -05001046 * helper function to set both pages and extents in the tree writeback
1047 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001048static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001049{
1050 unsigned long index = start >> PAGE_CACHE_SHIFT;
1051 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1052 struct page *page;
1053
1054 while (index <= end_index) {
1055 page = find_get_page(tree->mapping, index);
1056 BUG_ON(!page);
1057 set_page_writeback(page);
1058 page_cache_release(page);
1059 index++;
1060 }
Chris Masond1310b22008-01-24 16:13:08 -05001061 return 0;
1062}
Chris Masond1310b22008-01-24 16:13:08 -05001063
Chris Masond352ac62008-09-29 15:18:18 -04001064/*
1065 * find the first offset in the io tree with 'bits' set. zero is
1066 * returned if we find something, and *start_ret and *end_ret are
1067 * set to reflect the state struct that was found.
1068 *
1069 * If nothing was found, 1 is returned, < 0 on error
1070 */
Chris Masond1310b22008-01-24 16:13:08 -05001071int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1072 u64 *start_ret, u64 *end_ret, int bits)
1073{
1074 struct rb_node *node;
1075 struct extent_state *state;
1076 int ret = 1;
1077
Chris Masoncad321a2008-12-17 14:51:42 -05001078 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001079 /*
1080 * this search will find all the extents that end after
1081 * our range starts.
1082 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001083 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001084 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001085 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001086
Chris Masond3977122009-01-05 21:25:51 -05001087 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001088 state = rb_entry(node, struct extent_state, rb_node);
1089 if (state->end >= start && (state->state & bits)) {
1090 *start_ret = state->start;
1091 *end_ret = state->end;
1092 ret = 0;
1093 break;
1094 }
1095 node = rb_next(node);
1096 if (!node)
1097 break;
1098 }
1099out:
Chris Masoncad321a2008-12-17 14:51:42 -05001100 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001101 return ret;
1102}
Chris Masond1310b22008-01-24 16:13:08 -05001103
Chris Masond352ac62008-09-29 15:18:18 -04001104/* find the first state struct with 'bits' set after 'start', and
1105 * return it. tree->lock must be held. NULL will returned if
1106 * nothing was found after 'start'
1107 */
Chris Masond7fc6402008-02-18 12:12:38 -05001108struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1109 u64 start, int bits)
1110{
1111 struct rb_node *node;
1112 struct extent_state *state;
1113
1114 /*
1115 * this search will find all the extents that end after
1116 * our range starts.
1117 */
1118 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001119 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001120 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001121
Chris Masond3977122009-01-05 21:25:51 -05001122 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001123 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001124 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001125 return state;
Chris Masond3977122009-01-05 21:25:51 -05001126
Chris Masond7fc6402008-02-18 12:12:38 -05001127 node = rb_next(node);
1128 if (!node)
1129 break;
1130 }
1131out:
1132 return NULL;
1133}
Chris Masond7fc6402008-02-18 12:12:38 -05001134
Chris Masond352ac62008-09-29 15:18:18 -04001135/*
1136 * find a contiguous range of bytes in the file marked as delalloc, not
1137 * more than 'max_bytes'. start and end are used to return the range,
1138 *
1139 * 1 is returned if we find something, 0 if nothing was in the tree
1140 */
Chris Masonc8b97812008-10-29 14:49:59 -04001141static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001142 u64 *start, u64 *end, u64 max_bytes,
1143 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001144{
1145 struct rb_node *node;
1146 struct extent_state *state;
1147 u64 cur_start = *start;
1148 u64 found = 0;
1149 u64 total_bytes = 0;
1150
Chris Masoncad321a2008-12-17 14:51:42 -05001151 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001152
Chris Masond1310b22008-01-24 16:13:08 -05001153 /*
1154 * this search will find all the extents that end after
1155 * our range starts.
1156 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001157 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001158 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001159 if (!found)
1160 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001161 goto out;
1162 }
1163
Chris Masond3977122009-01-05 21:25:51 -05001164 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001165 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001166 if (found && (state->start != cur_start ||
1167 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001168 goto out;
1169 }
1170 if (!(state->state & EXTENT_DELALLOC)) {
1171 if (!found)
1172 *end = state->end;
1173 goto out;
1174 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001175 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001176 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001177 *cached_state = state;
1178 atomic_inc(&state->refs);
1179 }
Chris Masond1310b22008-01-24 16:13:08 -05001180 found++;
1181 *end = state->end;
1182 cur_start = state->end + 1;
1183 node = rb_next(node);
1184 if (!node)
1185 break;
1186 total_bytes += state->end - state->start + 1;
1187 if (total_bytes >= max_bytes)
1188 break;
1189 }
1190out:
Chris Masoncad321a2008-12-17 14:51:42 -05001191 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001192 return found;
1193}
1194
Chris Masonc8b97812008-10-29 14:49:59 -04001195static noinline int __unlock_for_delalloc(struct inode *inode,
1196 struct page *locked_page,
1197 u64 start, u64 end)
1198{
1199 int ret;
1200 struct page *pages[16];
1201 unsigned long index = start >> PAGE_CACHE_SHIFT;
1202 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1203 unsigned long nr_pages = end_index - index + 1;
1204 int i;
1205
1206 if (index == locked_page->index && end_index == index)
1207 return 0;
1208
Chris Masond3977122009-01-05 21:25:51 -05001209 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001210 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001211 min_t(unsigned long, nr_pages,
1212 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001213 for (i = 0; i < ret; i++) {
1214 if (pages[i] != locked_page)
1215 unlock_page(pages[i]);
1216 page_cache_release(pages[i]);
1217 }
1218 nr_pages -= ret;
1219 index += ret;
1220 cond_resched();
1221 }
1222 return 0;
1223}
1224
1225static noinline int lock_delalloc_pages(struct inode *inode,
1226 struct page *locked_page,
1227 u64 delalloc_start,
1228 u64 delalloc_end)
1229{
1230 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1231 unsigned long start_index = index;
1232 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1233 unsigned long pages_locked = 0;
1234 struct page *pages[16];
1235 unsigned long nrpages;
1236 int ret;
1237 int i;
1238
1239 /* the caller is responsible for locking the start index */
1240 if (index == locked_page->index && index == end_index)
1241 return 0;
1242
1243 /* skip the page at the start index */
1244 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001245 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001246 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001247 min_t(unsigned long,
1248 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001249 if (ret == 0) {
1250 ret = -EAGAIN;
1251 goto done;
1252 }
1253 /* now we have an array of pages, lock them all */
1254 for (i = 0; i < ret; i++) {
1255 /*
1256 * the caller is taking responsibility for
1257 * locked_page
1258 */
Chris Mason771ed682008-11-06 22:02:51 -05001259 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001260 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001261 if (!PageDirty(pages[i]) ||
1262 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001263 ret = -EAGAIN;
1264 unlock_page(pages[i]);
1265 page_cache_release(pages[i]);
1266 goto done;
1267 }
1268 }
Chris Masonc8b97812008-10-29 14:49:59 -04001269 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001270 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001271 }
Chris Masonc8b97812008-10-29 14:49:59 -04001272 nrpages -= ret;
1273 index += ret;
1274 cond_resched();
1275 }
1276 ret = 0;
1277done:
1278 if (ret && pages_locked) {
1279 __unlock_for_delalloc(inode, locked_page,
1280 delalloc_start,
1281 ((u64)(start_index + pages_locked - 1)) <<
1282 PAGE_CACHE_SHIFT);
1283 }
1284 return ret;
1285}
1286
1287/*
1288 * find a contiguous range of bytes in the file marked as delalloc, not
1289 * more than 'max_bytes'. start and end are used to return the range,
1290 *
1291 * 1 is returned if we find something, 0 if nothing was in the tree
1292 */
1293static noinline u64 find_lock_delalloc_range(struct inode *inode,
1294 struct extent_io_tree *tree,
1295 struct page *locked_page,
1296 u64 *start, u64 *end,
1297 u64 max_bytes)
1298{
1299 u64 delalloc_start;
1300 u64 delalloc_end;
1301 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001302 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001303 int ret;
1304 int loops = 0;
1305
1306again:
1307 /* step one, find a bunch of delalloc bytes starting at start */
1308 delalloc_start = *start;
1309 delalloc_end = 0;
1310 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001311 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001312 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001313 *start = delalloc_start;
1314 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001315 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001316 return found;
1317 }
1318
1319 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001320 * start comes from the offset of locked_page. We have to lock
1321 * pages in order, so we can't process delalloc bytes before
1322 * locked_page
1323 */
Chris Masond3977122009-01-05 21:25:51 -05001324 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001325 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001326
1327 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001328 * make sure to limit the number of pages we try to lock down
1329 * if we're looping.
1330 */
Chris Masond3977122009-01-05 21:25:51 -05001331 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001332 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001333
Chris Masonc8b97812008-10-29 14:49:59 -04001334 /* step two, lock all the pages after the page that has start */
1335 ret = lock_delalloc_pages(inode, locked_page,
1336 delalloc_start, delalloc_end);
1337 if (ret == -EAGAIN) {
1338 /* some of the pages are gone, lets avoid looping by
1339 * shortening the size of the delalloc range we're searching
1340 */
Chris Mason9655d292009-09-02 15:22:30 -04001341 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001342 if (!loops) {
1343 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1344 max_bytes = PAGE_CACHE_SIZE - offset;
1345 loops = 1;
1346 goto again;
1347 } else {
1348 found = 0;
1349 goto out_failed;
1350 }
1351 }
1352 BUG_ON(ret);
1353
1354 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001355 lock_extent_bits(tree, delalloc_start, delalloc_end,
1356 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001357
1358 /* then test to make sure it is all still delalloc */
1359 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001360 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001361 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001362 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1363 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001364 __unlock_for_delalloc(inode, locked_page,
1365 delalloc_start, delalloc_end);
1366 cond_resched();
1367 goto again;
1368 }
Chris Mason9655d292009-09-02 15:22:30 -04001369 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001370 *start = delalloc_start;
1371 *end = delalloc_end;
1372out_failed:
1373 return found;
1374}
1375
1376int extent_clear_unlock_delalloc(struct inode *inode,
1377 struct extent_io_tree *tree,
1378 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001379 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001380{
1381 int ret;
1382 struct page *pages[16];
1383 unsigned long index = start >> PAGE_CACHE_SHIFT;
1384 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1385 unsigned long nr_pages = end_index - index + 1;
1386 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001387 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001388
Chris Masona791e352009-10-08 11:27:10 -04001389 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001390 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001391 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001392 clear_bits |= EXTENT_DIRTY;
1393
Chris Masona791e352009-10-08 11:27:10 -04001394 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001395 clear_bits |= EXTENT_DELALLOC;
1396
Chris Mason2c64c532009-09-02 15:04:12 -04001397 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001398 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1399 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1400 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001401 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001402
Chris Masond3977122009-01-05 21:25:51 -05001403 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001404 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001405 min_t(unsigned long,
1406 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001407 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001408
Chris Masona791e352009-10-08 11:27:10 -04001409 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001410 SetPagePrivate2(pages[i]);
1411
Chris Masonc8b97812008-10-29 14:49:59 -04001412 if (pages[i] == locked_page) {
1413 page_cache_release(pages[i]);
1414 continue;
1415 }
Chris Masona791e352009-10-08 11:27:10 -04001416 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001417 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001418 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001419 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001420 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001421 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001422 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001423 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001424 page_cache_release(pages[i]);
1425 }
1426 nr_pages -= ret;
1427 index += ret;
1428 cond_resched();
1429 }
1430 return 0;
1431}
Chris Masonc8b97812008-10-29 14:49:59 -04001432
Chris Masond352ac62008-09-29 15:18:18 -04001433/*
1434 * count the number of bytes in the tree that have a given bit(s)
1435 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1436 * cached. The total number found is returned.
1437 */
Chris Masond1310b22008-01-24 16:13:08 -05001438u64 count_range_bits(struct extent_io_tree *tree,
1439 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001440 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001441{
1442 struct rb_node *node;
1443 struct extent_state *state;
1444 u64 cur_start = *start;
1445 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001446 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001447 int found = 0;
1448
1449 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001450 WARN_ON(1);
1451 return 0;
1452 }
1453
Chris Masoncad321a2008-12-17 14:51:42 -05001454 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001455 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1456 total_bytes = tree->dirty_bytes;
1457 goto out;
1458 }
1459 /*
1460 * this search will find all the extents that end after
1461 * our range starts.
1462 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001463 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001464 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001465 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001466
Chris Masond3977122009-01-05 21:25:51 -05001467 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001468 state = rb_entry(node, struct extent_state, rb_node);
1469 if (state->start > search_end)
1470 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001471 if (contig && found && state->start > last + 1)
1472 break;
1473 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001474 total_bytes += min(search_end, state->end) + 1 -
1475 max(cur_start, state->start);
1476 if (total_bytes >= max_bytes)
1477 break;
1478 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001479 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001480 found = 1;
1481 }
Chris Masonec29ed52011-02-23 16:23:20 -05001482 last = state->end;
1483 } else if (contig && found) {
1484 break;
Chris Masond1310b22008-01-24 16:13:08 -05001485 }
1486 node = rb_next(node);
1487 if (!node)
1488 break;
1489 }
1490out:
Chris Masoncad321a2008-12-17 14:51:42 -05001491 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001492 return total_bytes;
1493}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001494
Chris Masond352ac62008-09-29 15:18:18 -04001495/*
1496 * set the private field for a given byte offset in the tree. If there isn't
1497 * an extent_state there already, this does nothing.
1498 */
Chris Masond1310b22008-01-24 16:13:08 -05001499int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1500{
1501 struct rb_node *node;
1502 struct extent_state *state;
1503 int ret = 0;
1504
Chris Masoncad321a2008-12-17 14:51:42 -05001505 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001506 /*
1507 * this search will find all the extents that end after
1508 * our range starts.
1509 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001510 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001511 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001512 ret = -ENOENT;
1513 goto out;
1514 }
1515 state = rb_entry(node, struct extent_state, rb_node);
1516 if (state->start != start) {
1517 ret = -ENOENT;
1518 goto out;
1519 }
1520 state->private = private;
1521out:
Chris Masoncad321a2008-12-17 14:51:42 -05001522 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001523 return ret;
1524}
1525
1526int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1527{
1528 struct rb_node *node;
1529 struct extent_state *state;
1530 int ret = 0;
1531
Chris Masoncad321a2008-12-17 14:51:42 -05001532 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001533 /*
1534 * this search will find all the extents that end after
1535 * our range starts.
1536 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001537 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001538 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001539 ret = -ENOENT;
1540 goto out;
1541 }
1542 state = rb_entry(node, struct extent_state, rb_node);
1543 if (state->start != start) {
1544 ret = -ENOENT;
1545 goto out;
1546 }
1547 *private = state->private;
1548out:
Chris Masoncad321a2008-12-17 14:51:42 -05001549 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001550 return ret;
1551}
1552
1553/*
1554 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001555 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001556 * has the bits set. Otherwise, 1 is returned if any bit in the
1557 * range is found set.
1558 */
1559int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001560 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001561{
1562 struct extent_state *state = NULL;
1563 struct rb_node *node;
1564 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001565
Chris Masoncad321a2008-12-17 14:51:42 -05001566 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001567 if (cached && cached->tree && cached->start == start)
1568 node = &cached->rb_node;
1569 else
1570 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001571 while (node && start <= end) {
1572 state = rb_entry(node, struct extent_state, rb_node);
1573
1574 if (filled && state->start > start) {
1575 bitset = 0;
1576 break;
1577 }
1578
1579 if (state->start > end)
1580 break;
1581
1582 if (state->state & bits) {
1583 bitset = 1;
1584 if (!filled)
1585 break;
1586 } else if (filled) {
1587 bitset = 0;
1588 break;
1589 }
Chris Mason46562ce2009-09-23 20:23:16 -04001590
1591 if (state->end == (u64)-1)
1592 break;
1593
Chris Masond1310b22008-01-24 16:13:08 -05001594 start = state->end + 1;
1595 if (start > end)
1596 break;
1597 node = rb_next(node);
1598 if (!node) {
1599 if (filled)
1600 bitset = 0;
1601 break;
1602 }
1603 }
Chris Masoncad321a2008-12-17 14:51:42 -05001604 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001605 return bitset;
1606}
Chris Masond1310b22008-01-24 16:13:08 -05001607
1608/*
1609 * helper function to set a given page up to date if all the
1610 * extents in the tree for that page are up to date
1611 */
1612static int check_page_uptodate(struct extent_io_tree *tree,
1613 struct page *page)
1614{
1615 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1616 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001617 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001618 SetPageUptodate(page);
1619 return 0;
1620}
1621
1622/*
1623 * helper function to unlock a page if all the extents in the tree
1624 * for that page are unlocked
1625 */
1626static int check_page_locked(struct extent_io_tree *tree,
1627 struct page *page)
1628{
1629 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1630 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001631 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001632 unlock_page(page);
1633 return 0;
1634}
1635
1636/*
1637 * helper function to end page writeback if all the extents
1638 * in the tree for that page are done with writeback
1639 */
1640static int check_page_writeback(struct extent_io_tree *tree,
1641 struct page *page)
1642{
Chris Mason1edbb732009-09-02 13:24:36 -04001643 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001644 return 0;
1645}
1646
1647/* lots and lots of room for performance fixes in the end_bio funcs */
1648
1649/*
1650 * after a writepage IO is done, we need to:
1651 * clear the uptodate bits on error
1652 * clear the writeback bits in the extent tree for this IO
1653 * end_page_writeback if the page has no more pending IO
1654 *
1655 * Scheduling is not allowed, so the extent state tree is expected
1656 * to have one and only one object corresponding to this IO.
1657 */
Chris Masond1310b22008-01-24 16:13:08 -05001658static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001659{
Chris Mason1259ab72008-05-12 13:39:03 -04001660 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001661 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001662 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001663 u64 start;
1664 u64 end;
1665 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001666 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001667
Chris Masond1310b22008-01-24 16:13:08 -05001668 do {
1669 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001670 tree = &BTRFS_I(page->mapping->host)->io_tree;
1671
Chris Masond1310b22008-01-24 16:13:08 -05001672 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1673 bvec->bv_offset;
1674 end = start + bvec->bv_len - 1;
1675
1676 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1677 whole_page = 1;
1678 else
1679 whole_page = 0;
1680
1681 if (--bvec >= bio->bi_io_vec)
1682 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001683 if (tree->ops && tree->ops->writepage_end_io_hook) {
1684 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001685 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001686 if (ret)
1687 uptodate = 0;
1688 }
1689
1690 if (!uptodate && tree->ops &&
1691 tree->ops->writepage_io_failed_hook) {
1692 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001693 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001694 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001695 uptodate = (err == 0);
1696 continue;
1697 }
1698 }
1699
Chris Masond1310b22008-01-24 16:13:08 -05001700 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001701 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001702 ClearPageUptodate(page);
1703 SetPageError(page);
1704 }
Chris Mason70dec802008-01-29 09:59:12 -05001705
Chris Masond1310b22008-01-24 16:13:08 -05001706 if (whole_page)
1707 end_page_writeback(page);
1708 else
1709 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001710 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001711
Chris Masond1310b22008-01-24 16:13:08 -05001712 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001713}
1714
1715/*
1716 * after a readpage IO is done, we need to:
1717 * clear the uptodate bits on error
1718 * set the uptodate bits if things worked
1719 * set the page up to date if all extents in the tree are uptodate
1720 * clear the lock bit in the extent tree
1721 * unlock the page if there are no other extents locked for it
1722 *
1723 * Scheduling is not allowed, so the extent state tree is expected
1724 * to have one and only one object corresponding to this IO.
1725 */
Chris Masond1310b22008-01-24 16:13:08 -05001726static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001727{
1728 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001729 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1730 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001731 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001732 u64 start;
1733 u64 end;
1734 int whole_page;
1735 int ret;
1736
Chris Masond20f7042008-12-08 16:58:54 -05001737 if (err)
1738 uptodate = 0;
1739
Chris Masond1310b22008-01-24 16:13:08 -05001740 do {
1741 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001742 struct extent_state *cached = NULL;
1743 struct extent_state *state;
1744
David Woodhouse902b22f2008-08-20 08:51:49 -04001745 tree = &BTRFS_I(page->mapping->host)->io_tree;
1746
Chris Masond1310b22008-01-24 16:13:08 -05001747 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1748 bvec->bv_offset;
1749 end = start + bvec->bv_len - 1;
1750
1751 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1752 whole_page = 1;
1753 else
1754 whole_page = 0;
1755
Chris Mason4125bf72010-02-03 18:18:45 +00001756 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001757 prefetchw(&bvec->bv_page->flags);
1758
Arne Jansen507903b2011-04-06 10:02:20 +00001759 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001760 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001761 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001762 /*
1763 * take a reference on the state, unlock will drop
1764 * the ref
1765 */
1766 cache_state(state, &cached);
1767 }
1768 spin_unlock(&tree->lock);
1769
Chris Masond1310b22008-01-24 16:13:08 -05001770 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001771 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001772 state);
Chris Masond1310b22008-01-24 16:13:08 -05001773 if (ret)
1774 uptodate = 0;
1775 }
Chris Mason7e383262008-04-09 16:28:12 -04001776 if (!uptodate && tree->ops &&
1777 tree->ops->readpage_io_failed_hook) {
1778 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001779 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001780 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001781 uptodate =
1782 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001783 if (err)
1784 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001785 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001786 continue;
1787 }
1788 }
Chris Mason70dec802008-01-29 09:59:12 -05001789
Chris Mason771ed682008-11-06 22:02:51 -05001790 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001791 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001792 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001793 }
Arne Jansen507903b2011-04-06 10:02:20 +00001794 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001795
Chris Mason70dec802008-01-29 09:59:12 -05001796 if (whole_page) {
1797 if (uptodate) {
1798 SetPageUptodate(page);
1799 } else {
1800 ClearPageUptodate(page);
1801 SetPageError(page);
1802 }
Chris Masond1310b22008-01-24 16:13:08 -05001803 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001804 } else {
1805 if (uptodate) {
1806 check_page_uptodate(tree, page);
1807 } else {
1808 ClearPageUptodate(page);
1809 SetPageError(page);
1810 }
Chris Masond1310b22008-01-24 16:13:08 -05001811 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001812 }
Chris Mason4125bf72010-02-03 18:18:45 +00001813 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001814
1815 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001816}
1817
Miao Xie88f794e2010-11-22 03:02:55 +00001818struct bio *
1819btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1820 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001821{
1822 struct bio *bio;
1823
1824 bio = bio_alloc(gfp_flags, nr_vecs);
1825
1826 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1827 while (!bio && (nr_vecs /= 2))
1828 bio = bio_alloc(gfp_flags, nr_vecs);
1829 }
1830
1831 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001832 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001833 bio->bi_bdev = bdev;
1834 bio->bi_sector = first_sector;
1835 }
1836 return bio;
1837}
1838
Chris Masonc8b97812008-10-29 14:49:59 -04001839static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1840 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001841{
Chris Masond1310b22008-01-24 16:13:08 -05001842 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001843 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1844 struct page *page = bvec->bv_page;
1845 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001846 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001847
1848 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001849
David Woodhouse902b22f2008-08-20 08:51:49 -04001850 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001851
1852 bio_get(bio);
1853
Chris Mason065631f2008-02-20 12:07:25 -05001854 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001855 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001856 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001857 else
1858 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001859 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1860 ret = -EOPNOTSUPP;
1861 bio_put(bio);
1862 return ret;
1863}
1864
1865static int submit_extent_page(int rw, struct extent_io_tree *tree,
1866 struct page *page, sector_t sector,
1867 size_t size, unsigned long offset,
1868 struct block_device *bdev,
1869 struct bio **bio_ret,
1870 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001871 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001872 int mirror_num,
1873 unsigned long prev_bio_flags,
1874 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001875{
1876 int ret = 0;
1877 struct bio *bio;
1878 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001879 int contig = 0;
1880 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1881 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001882 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001883
1884 if (bio_ret && *bio_ret) {
1885 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001886 if (old_compressed)
1887 contig = bio->bi_sector == sector;
1888 else
1889 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1890 sector;
1891
1892 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001893 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001894 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1895 bio_flags)) ||
1896 bio_add_page(bio, page, page_size, offset) < page_size) {
1897 ret = submit_one_bio(rw, bio, mirror_num,
1898 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001899 bio = NULL;
1900 } else {
1901 return 0;
1902 }
1903 }
Chris Masonc8b97812008-10-29 14:49:59 -04001904 if (this_compressed)
1905 nr = BIO_MAX_PAGES;
1906 else
1907 nr = bio_get_nr_vecs(bdev);
1908
Miao Xie88f794e2010-11-22 03:02:55 +00001909 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001910 if (!bio)
1911 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001912
Chris Masonc8b97812008-10-29 14:49:59 -04001913 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001914 bio->bi_end_io = end_io_func;
1915 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001916
Chris Masond3977122009-01-05 21:25:51 -05001917 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001918 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001919 else
Chris Masonc8b97812008-10-29 14:49:59 -04001920 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001921
1922 return ret;
1923}
1924
1925void set_page_extent_mapped(struct page *page)
1926{
1927 if (!PagePrivate(page)) {
1928 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001929 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04001930 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001931 }
1932}
1933
Christoph Hellwigb2950862008-12-02 09:54:17 -05001934static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001935{
Chris Masoneb14ab82011-02-10 12:35:00 -05001936 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05001937 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1938}
1939
1940/*
1941 * basic readpage implementation. Locked extent state structs are inserted
1942 * into the tree that are removed when the IO is done (by the end_io
1943 * handlers)
1944 */
1945static int __extent_read_full_page(struct extent_io_tree *tree,
1946 struct page *page,
1947 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001948 struct bio **bio, int mirror_num,
1949 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001950{
1951 struct inode *inode = page->mapping->host;
1952 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1953 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1954 u64 end;
1955 u64 cur = start;
1956 u64 extent_offset;
1957 u64 last_byte = i_size_read(inode);
1958 u64 block_start;
1959 u64 cur_end;
1960 sector_t sector;
1961 struct extent_map *em;
1962 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001963 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05001964 int ret;
1965 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02001966 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001967 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001968 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001969 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001970 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001971
1972 set_page_extent_mapped(page);
1973
Dan Magenheimer90a887c2011-05-26 10:01:56 -06001974 if (!PageUptodate(page)) {
1975 if (cleancache_get_page(page) == 0) {
1976 BUG_ON(blocksize != PAGE_SIZE);
1977 goto out;
1978 }
1979 }
1980
Chris Masond1310b22008-01-24 16:13:08 -05001981 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001982 while (1) {
1983 lock_extent(tree, start, end, GFP_NOFS);
1984 ordered = btrfs_lookup_ordered_extent(inode, start);
1985 if (!ordered)
1986 break;
1987 unlock_extent(tree, start, end, GFP_NOFS);
1988 btrfs_start_ordered_extent(inode, ordered, 1);
1989 btrfs_put_ordered_extent(ordered);
1990 }
Chris Masond1310b22008-01-24 16:13:08 -05001991
Chris Masonc8b97812008-10-29 14:49:59 -04001992 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1993 char *userpage;
1994 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1995
1996 if (zero_offset) {
1997 iosize = PAGE_CACHE_SIZE - zero_offset;
1998 userpage = kmap_atomic(page, KM_USER0);
1999 memset(userpage + zero_offset, 0, iosize);
2000 flush_dcache_page(page);
2001 kunmap_atomic(userpage, KM_USER0);
2002 }
2003 }
Chris Masond1310b22008-01-24 16:13:08 -05002004 while (cur <= end) {
2005 if (cur >= last_byte) {
2006 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002007 struct extent_state *cached = NULL;
2008
David Sterba306e16c2011-04-19 14:29:38 +02002009 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002010 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002011 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002012 flush_dcache_page(page);
2013 kunmap_atomic(userpage, KM_USER0);
2014 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002015 &cached, GFP_NOFS);
2016 unlock_extent_cached(tree, cur, cur + iosize - 1,
2017 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002018 break;
2019 }
David Sterba306e16c2011-04-19 14:29:38 +02002020 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002021 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002022 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002023 SetPageError(page);
2024 unlock_extent(tree, cur, end, GFP_NOFS);
2025 break;
2026 }
Chris Masond1310b22008-01-24 16:13:08 -05002027 extent_offset = cur - em->start;
2028 BUG_ON(extent_map_end(em) <= cur);
2029 BUG_ON(end < cur);
2030
Li Zefan261507a02010-12-17 14:21:50 +08002031 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002032 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002033 extent_set_compress_type(&this_bio_flag,
2034 em->compress_type);
2035 }
Chris Masonc8b97812008-10-29 14:49:59 -04002036
Chris Masond1310b22008-01-24 16:13:08 -05002037 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2038 cur_end = min(extent_map_end(em) - 1, end);
2039 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002040 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2041 disk_io_size = em->block_len;
2042 sector = em->block_start >> 9;
2043 } else {
2044 sector = (em->block_start + extent_offset) >> 9;
2045 disk_io_size = iosize;
2046 }
Chris Masond1310b22008-01-24 16:13:08 -05002047 bdev = em->bdev;
2048 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002049 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2050 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002051 free_extent_map(em);
2052 em = NULL;
2053
2054 /* we've found a hole, just zero and go on */
2055 if (block_start == EXTENT_MAP_HOLE) {
2056 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002057 struct extent_state *cached = NULL;
2058
Chris Masond1310b22008-01-24 16:13:08 -05002059 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002060 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002061 flush_dcache_page(page);
2062 kunmap_atomic(userpage, KM_USER0);
2063
2064 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002065 &cached, GFP_NOFS);
2066 unlock_extent_cached(tree, cur, cur + iosize - 1,
2067 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002068 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002069 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002070 continue;
2071 }
2072 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002073 if (test_range_bit(tree, cur, cur_end,
2074 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002075 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002076 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2077 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002078 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002079 continue;
2080 }
Chris Mason70dec802008-01-29 09:59:12 -05002081 /* we have an inline extent but it didn't get marked up
2082 * to date. Error out
2083 */
2084 if (block_start == EXTENT_MAP_INLINE) {
2085 SetPageError(page);
2086 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2087 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002088 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002089 continue;
2090 }
Chris Masond1310b22008-01-24 16:13:08 -05002091
2092 ret = 0;
2093 if (tree->ops && tree->ops->readpage_io_hook) {
2094 ret = tree->ops->readpage_io_hook(page, cur,
2095 cur + iosize - 1);
2096 }
2097 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002098 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2099 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002100 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002101 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002102 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002103 end_bio_extent_readpage, mirror_num,
2104 *bio_flags,
2105 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002106 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002107 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002108 }
2109 if (ret)
2110 SetPageError(page);
2111 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002112 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002113 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002114out:
Chris Masond1310b22008-01-24 16:13:08 -05002115 if (!nr) {
2116 if (!PageError(page))
2117 SetPageUptodate(page);
2118 unlock_page(page);
2119 }
2120 return 0;
2121}
2122
2123int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2124 get_extent_t *get_extent)
2125{
2126 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002127 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002128 int ret;
2129
Chris Masonc8b97812008-10-29 14:49:59 -04002130 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2131 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002132 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002133 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002134 return ret;
2135}
Chris Masond1310b22008-01-24 16:13:08 -05002136
Chris Mason11c83492009-04-20 15:50:09 -04002137static noinline void update_nr_written(struct page *page,
2138 struct writeback_control *wbc,
2139 unsigned long nr_written)
2140{
2141 wbc->nr_to_write -= nr_written;
2142 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2143 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2144 page->mapping->writeback_index = page->index + nr_written;
2145}
2146
Chris Masond1310b22008-01-24 16:13:08 -05002147/*
2148 * the writepage semantics are similar to regular writepage. extent
2149 * records are inserted to lock ranges in the tree, and as dirty areas
2150 * are found, they are marked writeback. Then the lock bits are removed
2151 * and the end_io handler clears the writeback ranges
2152 */
2153static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2154 void *data)
2155{
2156 struct inode *inode = page->mapping->host;
2157 struct extent_page_data *epd = data;
2158 struct extent_io_tree *tree = epd->tree;
2159 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2160 u64 delalloc_start;
2161 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2162 u64 end;
2163 u64 cur = start;
2164 u64 extent_offset;
2165 u64 last_byte = i_size_read(inode);
2166 u64 block_start;
2167 u64 iosize;
2168 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002169 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002170 struct extent_map *em;
2171 struct block_device *bdev;
2172 int ret;
2173 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002174 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002175 size_t blocksize;
2176 loff_t i_size = i_size_read(inode);
2177 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2178 u64 nr_delalloc;
2179 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002180 int page_started;
2181 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002182 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002183 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002184
Chris Masonffbd5172009-04-20 15:50:09 -04002185 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002186 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002187 else
2188 write_flags = WRITE;
2189
liubo1abe9b82011-03-24 11:18:59 +00002190 trace___extent_writepage(page, inode, wbc);
2191
Chris Masond1310b22008-01-24 16:13:08 -05002192 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002193 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002194 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002195 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002196 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002197 unlock_page(page);
2198 return 0;
2199 }
2200
2201 if (page->index == end_index) {
2202 char *userpage;
2203
Chris Masond1310b22008-01-24 16:13:08 -05002204 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002205 memset(userpage + pg_offset, 0,
2206 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002207 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002208 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002209 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002210 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002211
2212 set_page_extent_mapped(page);
2213
2214 delalloc_start = start;
2215 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002216 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002217 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002218 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002219 /*
2220 * make sure the wbc mapping index is at least updated
2221 * to this page.
2222 */
2223 update_nr_written(page, wbc, 0);
2224
Chris Masond3977122009-01-05 21:25:51 -05002225 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002226 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002227 page,
2228 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002229 &delalloc_end,
2230 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002231 if (nr_delalloc == 0) {
2232 delalloc_start = delalloc_end + 1;
2233 continue;
2234 }
2235 tree->ops->fill_delalloc(inode, page, delalloc_start,
2236 delalloc_end, &page_started,
2237 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002238 /*
2239 * delalloc_end is already one less than the total
2240 * length, so we don't subtract one from
2241 * PAGE_CACHE_SIZE
2242 */
2243 delalloc_to_write += (delalloc_end - delalloc_start +
2244 PAGE_CACHE_SIZE) >>
2245 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002246 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002247 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002248 if (wbc->nr_to_write < delalloc_to_write) {
2249 int thresh = 8192;
2250
2251 if (delalloc_to_write < thresh * 2)
2252 thresh = delalloc_to_write;
2253 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2254 thresh);
2255 }
Chris Masonc8b97812008-10-29 14:49:59 -04002256
Chris Mason771ed682008-11-06 22:02:51 -05002257 /* did the fill delalloc function already unlock and start
2258 * the IO?
2259 */
2260 if (page_started) {
2261 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002262 /*
2263 * we've unlocked the page, so we can't update
2264 * the mapping's writeback index, just update
2265 * nr_to_write.
2266 */
2267 wbc->nr_to_write -= nr_written;
2268 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002269 }
Chris Masonc8b97812008-10-29 14:49:59 -04002270 }
Chris Mason247e7432008-07-17 12:53:51 -04002271 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002272 ret = tree->ops->writepage_start_hook(page, start,
2273 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002274 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002275 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002276 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002277 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002278 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002279 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002280 }
2281 }
2282
Chris Mason11c83492009-04-20 15:50:09 -04002283 /*
2284 * we don't want to touch the inode after unlocking the page,
2285 * so we update the mapping writeback index now
2286 */
2287 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002288
Chris Masond1310b22008-01-24 16:13:08 -05002289 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002290 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002291 if (tree->ops && tree->ops->writepage_end_io_hook)
2292 tree->ops->writepage_end_io_hook(page, start,
2293 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002294 goto done;
2295 }
2296
Chris Masond1310b22008-01-24 16:13:08 -05002297 blocksize = inode->i_sb->s_blocksize;
2298
2299 while (cur <= end) {
2300 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002301 if (tree->ops && tree->ops->writepage_end_io_hook)
2302 tree->ops->writepage_end_io_hook(page, cur,
2303 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002304 break;
2305 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002306 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002307 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002308 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002309 SetPageError(page);
2310 break;
2311 }
2312
2313 extent_offset = cur - em->start;
2314 BUG_ON(extent_map_end(em) <= cur);
2315 BUG_ON(end < cur);
2316 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2317 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2318 sector = (em->block_start + extent_offset) >> 9;
2319 bdev = em->bdev;
2320 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002321 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002322 free_extent_map(em);
2323 em = NULL;
2324
Chris Masonc8b97812008-10-29 14:49:59 -04002325 /*
2326 * compressed and inline extents are written through other
2327 * paths in the FS
2328 */
2329 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002330 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002331 /*
2332 * end_io notification does not happen here for
2333 * compressed extents
2334 */
2335 if (!compressed && tree->ops &&
2336 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002337 tree->ops->writepage_end_io_hook(page, cur,
2338 cur + iosize - 1,
2339 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002340 else if (compressed) {
2341 /* we don't want to end_page_writeback on
2342 * a compressed extent. this happens
2343 * elsewhere
2344 */
2345 nr++;
2346 }
2347
2348 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002349 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002350 continue;
2351 }
Chris Masond1310b22008-01-24 16:13:08 -05002352 /* leave this out until we have a page_mkwrite call */
2353 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002354 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002355 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002356 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002357 continue;
2358 }
Chris Masonc8b97812008-10-29 14:49:59 -04002359
Chris Masond1310b22008-01-24 16:13:08 -05002360 if (tree->ops && tree->ops->writepage_io_hook) {
2361 ret = tree->ops->writepage_io_hook(page, cur,
2362 cur + iosize - 1);
2363 } else {
2364 ret = 0;
2365 }
Chris Mason1259ab72008-05-12 13:39:03 -04002366 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002367 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002368 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002369 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002370
Chris Masond1310b22008-01-24 16:13:08 -05002371 set_range_writeback(tree, cur, cur + iosize - 1);
2372 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002373 printk(KERN_ERR "btrfs warning page %lu not "
2374 "writeback, cur %llu end %llu\n",
2375 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002376 (unsigned long long)end);
2377 }
2378
Chris Masonffbd5172009-04-20 15:50:09 -04002379 ret = submit_extent_page(write_flags, tree, page,
2380 sector, iosize, pg_offset,
2381 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002382 end_bio_extent_writepage,
2383 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002384 if (ret)
2385 SetPageError(page);
2386 }
2387 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002388 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002389 nr++;
2390 }
2391done:
2392 if (nr == 0) {
2393 /* make sure the mapping tag for page dirty gets cleared */
2394 set_page_writeback(page);
2395 end_page_writeback(page);
2396 }
Chris Masond1310b22008-01-24 16:13:08 -05002397 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002398
Chris Mason11c83492009-04-20 15:50:09 -04002399done_unlocked:
2400
Chris Mason2c64c532009-09-02 15:04:12 -04002401 /* drop our reference on any cached states */
2402 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002403 return 0;
2404}
2405
Chris Masond1310b22008-01-24 16:13:08 -05002406/**
Chris Mason4bef0842008-09-08 11:18:08 -04002407 * 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 -05002408 * @mapping: address space structure to write
2409 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2410 * @writepage: function called for each page
2411 * @data: data passed to writepage function
2412 *
2413 * If a page is already under I/O, write_cache_pages() skips it, even
2414 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2415 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2416 * and msync() need to guarantee that all the data which was dirty at the time
2417 * the call was made get new I/O started against them. If wbc->sync_mode is
2418 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2419 * existing IO to complete.
2420 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002421static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002422 struct address_space *mapping,
2423 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002424 writepage_t writepage, void *data,
2425 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002426{
Chris Masond1310b22008-01-24 16:13:08 -05002427 int ret = 0;
2428 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002429 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002430 struct pagevec pvec;
2431 int nr_pages;
2432 pgoff_t index;
2433 pgoff_t end; /* Inclusive */
2434 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002435
Chris Masond1310b22008-01-24 16:13:08 -05002436 pagevec_init(&pvec, 0);
2437 if (wbc->range_cyclic) {
2438 index = mapping->writeback_index; /* Start from prev offset */
2439 end = -1;
2440 } else {
2441 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2442 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002443 scanned = 1;
2444 }
2445retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002446 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002447 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002448 PAGECACHE_TAG_DIRTY, min(end - index,
2449 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002450 unsigned i;
2451
2452 scanned = 1;
2453 for (i = 0; i < nr_pages; i++) {
2454 struct page *page = pvec.pages[i];
2455
2456 /*
2457 * At this point we hold neither mapping->tree_lock nor
2458 * lock on the page itself: the page may be truncated or
2459 * invalidated (changing page->mapping to NULL), or even
2460 * swizzled back from swapper_space to tmpfs file
2461 * mapping
2462 */
Chris Mason4bef0842008-09-08 11:18:08 -04002463 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2464 tree->ops->write_cache_pages_lock_hook(page);
2465 else
2466 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002467
2468 if (unlikely(page->mapping != mapping)) {
2469 unlock_page(page);
2470 continue;
2471 }
2472
2473 if (!wbc->range_cyclic && page->index > end) {
2474 done = 1;
2475 unlock_page(page);
2476 continue;
2477 }
2478
Chris Masond2c3f4f2008-11-19 12:44:22 -05002479 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002480 if (PageWriteback(page))
2481 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002482 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002483 }
Chris Masond1310b22008-01-24 16:13:08 -05002484
2485 if (PageWriteback(page) ||
2486 !clear_page_dirty_for_io(page)) {
2487 unlock_page(page);
2488 continue;
2489 }
2490
2491 ret = (*writepage)(page, wbc, data);
2492
2493 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2494 unlock_page(page);
2495 ret = 0;
2496 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002497 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002498 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002499
2500 /*
2501 * the filesystem may choose to bump up nr_to_write.
2502 * We have to make sure to honor the new nr_to_write
2503 * at any time
2504 */
2505 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002506 }
2507 pagevec_release(&pvec);
2508 cond_resched();
2509 }
2510 if (!scanned && !done) {
2511 /*
2512 * We hit the last page and there is more work to be done: wrap
2513 * back to the start of the file
2514 */
2515 scanned = 1;
2516 index = 0;
2517 goto retry;
2518 }
Chris Masond1310b22008-01-24 16:13:08 -05002519 return ret;
2520}
Chris Masond1310b22008-01-24 16:13:08 -05002521
Chris Masonffbd5172009-04-20 15:50:09 -04002522static void flush_epd_write_bio(struct extent_page_data *epd)
2523{
2524 if (epd->bio) {
2525 if (epd->sync_io)
2526 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2527 else
2528 submit_one_bio(WRITE, epd->bio, 0, 0);
2529 epd->bio = NULL;
2530 }
2531}
2532
Chris Masond2c3f4f2008-11-19 12:44:22 -05002533static noinline void flush_write_bio(void *data)
2534{
2535 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002536 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002537}
2538
Chris Masond1310b22008-01-24 16:13:08 -05002539int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2540 get_extent_t *get_extent,
2541 struct writeback_control *wbc)
2542{
2543 int ret;
2544 struct address_space *mapping = page->mapping;
2545 struct extent_page_data epd = {
2546 .bio = NULL,
2547 .tree = tree,
2548 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002549 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002550 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002551 };
2552 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002553 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002554 .nr_to_write = 64,
2555 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2556 .range_end = (loff_t)-1,
2557 };
2558
Chris Masond1310b22008-01-24 16:13:08 -05002559 ret = __extent_writepage(page, wbc, &epd);
2560
Chris Mason4bef0842008-09-08 11:18:08 -04002561 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002562 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002563 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002564 return ret;
2565}
Chris Masond1310b22008-01-24 16:13:08 -05002566
Chris Mason771ed682008-11-06 22:02:51 -05002567int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2568 u64 start, u64 end, get_extent_t *get_extent,
2569 int mode)
2570{
2571 int ret = 0;
2572 struct address_space *mapping = inode->i_mapping;
2573 struct page *page;
2574 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2575 PAGE_CACHE_SHIFT;
2576
2577 struct extent_page_data epd = {
2578 .bio = NULL,
2579 .tree = tree,
2580 .get_extent = get_extent,
2581 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002582 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002583 };
2584 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002585 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05002586 .nr_to_write = nr_pages * 2,
2587 .range_start = start,
2588 .range_end = end + 1,
2589 };
2590
Chris Masond3977122009-01-05 21:25:51 -05002591 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002592 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2593 if (clear_page_dirty_for_io(page))
2594 ret = __extent_writepage(page, &wbc_writepages, &epd);
2595 else {
2596 if (tree->ops && tree->ops->writepage_end_io_hook)
2597 tree->ops->writepage_end_io_hook(page, start,
2598 start + PAGE_CACHE_SIZE - 1,
2599 NULL, 1);
2600 unlock_page(page);
2601 }
2602 page_cache_release(page);
2603 start += PAGE_CACHE_SIZE;
2604 }
2605
Chris Masonffbd5172009-04-20 15:50:09 -04002606 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002607 return ret;
2608}
Chris Masond1310b22008-01-24 16:13:08 -05002609
2610int extent_writepages(struct extent_io_tree *tree,
2611 struct address_space *mapping,
2612 get_extent_t *get_extent,
2613 struct writeback_control *wbc)
2614{
2615 int ret = 0;
2616 struct extent_page_data epd = {
2617 .bio = NULL,
2618 .tree = tree,
2619 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002620 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002621 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002622 };
2623
Chris Mason4bef0842008-09-08 11:18:08 -04002624 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002625 __extent_writepage, &epd,
2626 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002627 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002628 return ret;
2629}
Chris Masond1310b22008-01-24 16:13:08 -05002630
2631int extent_readpages(struct extent_io_tree *tree,
2632 struct address_space *mapping,
2633 struct list_head *pages, unsigned nr_pages,
2634 get_extent_t get_extent)
2635{
2636 struct bio *bio = NULL;
2637 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002638 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002639
Chris Masond1310b22008-01-24 16:13:08 -05002640 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2641 struct page *page = list_entry(pages->prev, struct page, lru);
2642
2643 prefetchw(&page->flags);
2644 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00002645 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002646 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002647 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002648 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002649 }
2650 page_cache_release(page);
2651 }
Chris Masond1310b22008-01-24 16:13:08 -05002652 BUG_ON(!list_empty(pages));
2653 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002654 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002655 return 0;
2656}
Chris Masond1310b22008-01-24 16:13:08 -05002657
2658/*
2659 * basic invalidatepage code, this waits on any locked or writeback
2660 * ranges corresponding to the page, and then deletes any extent state
2661 * records from the tree
2662 */
2663int extent_invalidatepage(struct extent_io_tree *tree,
2664 struct page *page, unsigned long offset)
2665{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002666 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002667 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2668 u64 end = start + PAGE_CACHE_SIZE - 1;
2669 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2670
Chris Masond3977122009-01-05 21:25:51 -05002671 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002672 if (start > end)
2673 return 0;
2674
Josef Bacik2ac55d42010-02-03 19:33:23 +00002675 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002676 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002677 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002678 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2679 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002680 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002681 return 0;
2682}
Chris Masond1310b22008-01-24 16:13:08 -05002683
2684/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002685 * a helper for releasepage, this tests for areas of the page that
2686 * are locked or under IO and drops the related state bits if it is safe
2687 * to drop the page.
2688 */
2689int try_release_extent_state(struct extent_map_tree *map,
2690 struct extent_io_tree *tree, struct page *page,
2691 gfp_t mask)
2692{
2693 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2694 u64 end = start + PAGE_CACHE_SIZE - 1;
2695 int ret = 1;
2696
Chris Mason211f90e2008-07-18 11:56:15 -04002697 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002698 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002699 ret = 0;
2700 else {
2701 if ((mask & GFP_NOFS) == GFP_NOFS)
2702 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002703 /*
2704 * at this point we can safely clear everything except the
2705 * locked bit and the nodatasum bit
2706 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002707 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002708 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2709 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002710
2711 /* if clear_extent_bit failed for enomem reasons,
2712 * we can't allow the release to continue.
2713 */
2714 if (ret < 0)
2715 ret = 0;
2716 else
2717 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002718 }
2719 return ret;
2720}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002721
2722/*
Chris Masond1310b22008-01-24 16:13:08 -05002723 * a helper for releasepage. As long as there are no locked extents
2724 * in the range corresponding to the page, both state records and extent
2725 * map records are removed
2726 */
2727int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002728 struct extent_io_tree *tree, struct page *page,
2729 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002730{
2731 struct extent_map *em;
2732 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2733 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002734
Chris Mason70dec802008-01-29 09:59:12 -05002735 if ((mask & __GFP_WAIT) &&
2736 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002737 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002738 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002739 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002740 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002741 em = lookup_extent_mapping(map, start, len);
David Sterbac7040052011-04-19 18:00:01 +02002742 if (IS_ERR_OR_NULL(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002743 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002744 break;
2745 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002746 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2747 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002748 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002749 free_extent_map(em);
2750 break;
2751 }
2752 if (!test_range_bit(tree, em->start,
2753 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002754 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002755 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002756 remove_extent_mapping(map, em);
2757 /* once for the rb tree */
2758 free_extent_map(em);
2759 }
2760 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002761 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002762
2763 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002764 free_extent_map(em);
2765 }
Chris Masond1310b22008-01-24 16:13:08 -05002766 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002767 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002768}
Chris Masond1310b22008-01-24 16:13:08 -05002769
Chris Masonec29ed52011-02-23 16:23:20 -05002770/*
2771 * helper function for fiemap, which doesn't want to see any holes.
2772 * This maps until we find something past 'last'
2773 */
2774static struct extent_map *get_extent_skip_holes(struct inode *inode,
2775 u64 offset,
2776 u64 last,
2777 get_extent_t *get_extent)
2778{
2779 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2780 struct extent_map *em;
2781 u64 len;
2782
2783 if (offset >= last)
2784 return NULL;
2785
2786 while(1) {
2787 len = last - offset;
2788 if (len == 0)
2789 break;
2790 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2791 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02002792 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05002793 return em;
2794
2795 /* if this isn't a hole return it */
2796 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2797 em->block_start != EXTENT_MAP_HOLE) {
2798 return em;
2799 }
2800
2801 /* this is a hole, advance to the next extent */
2802 offset = extent_map_end(em);
2803 free_extent_map(em);
2804 if (offset >= last)
2805 break;
2806 }
2807 return NULL;
2808}
2809
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002810int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2811 __u64 start, __u64 len, get_extent_t *get_extent)
2812{
Josef Bacik975f84f2010-11-23 19:36:57 +00002813 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002814 u64 off = start;
2815 u64 max = start + len;
2816 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00002817 u32 found_type;
2818 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05002819 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002820 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002821 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00002822 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002823 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002824 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00002825 struct btrfs_path *path;
2826 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002827 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002828 u64 em_start = 0;
2829 u64 em_len = 0;
2830 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002831 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002832
2833 if (len == 0)
2834 return -EINVAL;
2835
Josef Bacik975f84f2010-11-23 19:36:57 +00002836 path = btrfs_alloc_path();
2837 if (!path)
2838 return -ENOMEM;
2839 path->leave_spinning = 1;
2840
Chris Masonec29ed52011-02-23 16:23:20 -05002841 /*
2842 * lookup the last file extent. We're not using i_size here
2843 * because there might be preallocation past i_size
2844 */
Josef Bacik975f84f2010-11-23 19:36:57 +00002845 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08002846 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00002847 if (ret < 0) {
2848 btrfs_free_path(path);
2849 return ret;
2850 }
2851 WARN_ON(!ret);
2852 path->slots[0]--;
2853 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2854 struct btrfs_file_extent_item);
2855 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
2856 found_type = btrfs_key_type(&found_key);
2857
Chris Masonec29ed52011-02-23 16:23:20 -05002858 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08002859 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00002860 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05002861 /* have to trust i_size as the end */
2862 last = (u64)-1;
2863 last_for_get_extent = isize;
2864 } else {
2865 /*
2866 * remember the start of the last extent. There are a
2867 * bunch of different factors that go into the length of the
2868 * extent, so its much less complex to remember where it started
2869 */
2870 last = found_key.offset;
2871 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00002872 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002873 btrfs_free_path(path);
2874
Chris Masonec29ed52011-02-23 16:23:20 -05002875 /*
2876 * we might have some extents allocated but more delalloc past those
2877 * extents. so, we trust isize unless the start of the last extent is
2878 * beyond isize
2879 */
2880 if (last < isize) {
2881 last = (u64)-1;
2882 last_for_get_extent = isize;
2883 }
2884
Josef Bacik2ac55d42010-02-03 19:33:23 +00002885 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2886 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05002887
2888 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2889 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002890 if (!em)
2891 goto out;
2892 if (IS_ERR(em)) {
2893 ret = PTR_ERR(em);
2894 goto out;
2895 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002896
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002897 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05002898 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002899
Chris Masonea8efc72011-03-08 11:54:40 -05002900 /* break if the extent we found is outside the range */
2901 if (em->start >= max || extent_map_end(em) < off)
2902 break;
2903
2904 /*
2905 * get_extent may return an extent that starts before our
2906 * requested range. We have to make sure the ranges
2907 * we return to fiemap always move forward and don't
2908 * overlap, so adjust the offsets here
2909 */
2910 em_start = max(em->start, off);
2911
2912 /*
2913 * record the offset from the start of the extent
2914 * for adjusting the disk offset below
2915 */
2916 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05002917 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05002918 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05002919 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002920 disko = 0;
2921 flags = 0;
2922
Chris Masonea8efc72011-03-08 11:54:40 -05002923 /*
2924 * bump off for our next call to get_extent
2925 */
2926 off = extent_map_end(em);
2927 if (off >= max)
2928 end = 1;
2929
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002930 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002931 end = 1;
2932 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002933 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002934 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2935 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002936 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002937 flags |= (FIEMAP_EXTENT_DELALLOC |
2938 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002939 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05002940 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002941 }
2942 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2943 flags |= FIEMAP_EXTENT_ENCODED;
2944
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002945 free_extent_map(em);
2946 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05002947 if ((em_start >= last) || em_len == (u64)-1 ||
2948 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002949 flags |= FIEMAP_EXTENT_LAST;
2950 end = 1;
2951 }
2952
Chris Masonec29ed52011-02-23 16:23:20 -05002953 /* now scan forward to see if this is really the last extent. */
2954 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2955 get_extent);
2956 if (IS_ERR(em)) {
2957 ret = PTR_ERR(em);
2958 goto out;
2959 }
2960 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00002961 flags |= FIEMAP_EXTENT_LAST;
2962 end = 1;
2963 }
Chris Masonec29ed52011-02-23 16:23:20 -05002964 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2965 em_len, flags);
2966 if (ret)
2967 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002968 }
2969out_free:
2970 free_extent_map(em);
2971out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002972 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
2973 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002974 return ret;
2975}
2976
Chris Masond1310b22008-01-24 16:13:08 -05002977static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2978 unsigned long i)
2979{
2980 struct page *p;
2981 struct address_space *mapping;
2982
2983 if (i == 0)
2984 return eb->first_page;
2985 i += eb->start >> PAGE_CACHE_SHIFT;
2986 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002987 if (!mapping)
2988 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002989
2990 /*
2991 * extent_buffer_page is only called after pinning the page
2992 * by increasing the reference count. So we know the page must
2993 * be in the radix tree.
2994 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002995 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002996 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002997 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002998
Chris Masond1310b22008-01-24 16:13:08 -05002999 return p;
3000}
3001
Chris Mason6af118c2008-07-22 11:18:07 -04003002static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003003{
Chris Mason6af118c2008-07-22 11:18:07 -04003004 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3005 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003006}
3007
Chris Masond1310b22008-01-24 16:13:08 -05003008static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3009 u64 start,
3010 unsigned long len,
3011 gfp_t mask)
3012{
3013 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003014#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003015 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003016#endif
Chris Masond1310b22008-01-24 16:13:08 -05003017
Chris Masond1310b22008-01-24 16:13:08 -05003018 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003019 if (eb == NULL)
3020 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003021 eb->start = start;
3022 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003023 spin_lock_init(&eb->lock);
3024 init_waitqueue_head(&eb->lock_wq);
3025
Chris Mason39351272009-02-04 09:24:05 -05003026#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003027 spin_lock_irqsave(&leak_lock, flags);
3028 list_add(&eb->leak_list, &buffers);
3029 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003030#endif
Chris Masond1310b22008-01-24 16:13:08 -05003031 atomic_set(&eb->refs, 1);
3032
3033 return eb;
3034}
3035
3036static void __free_extent_buffer(struct extent_buffer *eb)
3037{
Chris Mason39351272009-02-04 09:24:05 -05003038#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003039 unsigned long flags;
3040 spin_lock_irqsave(&leak_lock, flags);
3041 list_del(&eb->leak_list);
3042 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003043#endif
Chris Masond1310b22008-01-24 16:13:08 -05003044 kmem_cache_free(extent_buffer_cache, eb);
3045}
3046
Miao Xie897ca6e2010-10-26 20:57:29 -04003047/*
3048 * Helper for releasing extent buffer page.
3049 */
3050static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3051 unsigned long start_idx)
3052{
3053 unsigned long index;
3054 struct page *page;
3055
3056 if (!eb->first_page)
3057 return;
3058
3059 index = num_extent_pages(eb->start, eb->len);
3060 if (start_idx >= index)
3061 return;
3062
3063 do {
3064 index--;
3065 page = extent_buffer_page(eb, index);
3066 if (page)
3067 page_cache_release(page);
3068 } while (index != start_idx);
3069}
3070
3071/*
3072 * Helper for releasing the extent buffer.
3073 */
3074static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3075{
3076 btrfs_release_extent_buffer_page(eb, 0);
3077 __free_extent_buffer(eb);
3078}
3079
Chris Masond1310b22008-01-24 16:13:08 -05003080struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3081 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003082 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003083{
3084 unsigned long num_pages = num_extent_pages(start, len);
3085 unsigned long i;
3086 unsigned long index = start >> PAGE_CACHE_SHIFT;
3087 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003088 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003089 struct page *p;
3090 struct address_space *mapping = tree->mapping;
3091 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003092 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003093
Miao Xie19fe0a82010-10-26 20:57:29 -04003094 rcu_read_lock();
3095 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3096 if (eb && atomic_inc_not_zero(&eb->refs)) {
3097 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003098 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003099 return eb;
3100 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003101 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04003102
David Sterbaba144192011-04-21 01:12:06 +02003103 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003104 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003105 return NULL;
3106
Chris Masond1310b22008-01-24 16:13:08 -05003107 if (page0) {
3108 eb->first_page = page0;
3109 i = 1;
3110 index++;
3111 page_cache_get(page0);
3112 mark_page_accessed(page0);
3113 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003114 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003115 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003116 } else {
3117 i = 0;
3118 }
3119 for (; i < num_pages; i++, index++) {
David Sterbaba144192011-04-21 01:12:06 +02003120 p = find_or_create_page(mapping, index, GFP_NOFS | __GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -05003121 if (!p) {
3122 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003123 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003124 }
3125 set_page_extent_mapped(p);
3126 mark_page_accessed(p);
3127 if (i == 0) {
3128 eb->first_page = p;
3129 set_page_extent_head(p, len);
3130 } else {
3131 set_page_private(p, EXTENT_PAGE_PRIVATE);
3132 }
3133 if (!PageUptodate(p))
3134 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003135
3136 /*
3137 * see below about how we avoid a nasty race with release page
3138 * and why we unlock later
3139 */
3140 if (i != 0)
3141 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003142 }
3143 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003144 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003145
Miao Xie19fe0a82010-10-26 20:57:29 -04003146 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3147 if (ret)
3148 goto free_eb;
3149
Chris Mason6af118c2008-07-22 11:18:07 -04003150 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003151 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3152 if (ret == -EEXIST) {
3153 exists = radix_tree_lookup(&tree->buffer,
3154 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003155 /* add one reference for the caller */
3156 atomic_inc(&exists->refs);
3157 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003158 radix_tree_preload_end();
Chris Mason6af118c2008-07-22 11:18:07 -04003159 goto free_eb;
3160 }
Chris Mason6af118c2008-07-22 11:18:07 -04003161 /* add one reference for the tree */
3162 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003163 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003164 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003165
3166 /*
3167 * there is a race where release page may have
3168 * tried to find this extent buffer in the radix
3169 * but failed. It will tell the VM it is safe to
3170 * reclaim the, and it will clear the page private bit.
3171 * We must make sure to set the page private bit properly
3172 * after the extent buffer is in the radix tree so
3173 * it doesn't get lost
3174 */
3175 set_page_extent_mapped(eb->first_page);
3176 set_page_extent_head(eb->first_page, eb->len);
3177 if (!page0)
3178 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003179 return eb;
3180
Chris Mason6af118c2008-07-22 11:18:07 -04003181free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003182 if (eb->first_page && !page0)
3183 unlock_page(eb->first_page);
3184
Chris Masond1310b22008-01-24 16:13:08 -05003185 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003186 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003187 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003188 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003189}
Chris Masond1310b22008-01-24 16:13:08 -05003190
3191struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003192 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003193{
Chris Masond1310b22008-01-24 16:13:08 -05003194 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003195
Miao Xie19fe0a82010-10-26 20:57:29 -04003196 rcu_read_lock();
3197 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3198 if (eb && atomic_inc_not_zero(&eb->refs)) {
3199 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003200 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003201 return eb;
3202 }
3203 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003204
Miao Xie19fe0a82010-10-26 20:57:29 -04003205 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003206}
Chris Masond1310b22008-01-24 16:13:08 -05003207
3208void free_extent_buffer(struct extent_buffer *eb)
3209{
Chris Masond1310b22008-01-24 16:13:08 -05003210 if (!eb)
3211 return;
3212
3213 if (!atomic_dec_and_test(&eb->refs))
3214 return;
3215
Chris Mason6af118c2008-07-22 11:18:07 -04003216 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003217}
Chris Masond1310b22008-01-24 16:13:08 -05003218
3219int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3220 struct extent_buffer *eb)
3221{
Chris Masond1310b22008-01-24 16:13:08 -05003222 unsigned long i;
3223 unsigned long num_pages;
3224 struct page *page;
3225
Chris Masond1310b22008-01-24 16:13:08 -05003226 num_pages = num_extent_pages(eb->start, eb->len);
3227
3228 for (i = 0; i < num_pages; i++) {
3229 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003230 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003231 continue;
3232
Chris Masona61e6f22008-07-22 11:18:08 -04003233 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003234 WARN_ON(!PagePrivate(page));
3235
3236 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003237 if (i == 0)
3238 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003239
Chris Masond1310b22008-01-24 16:13:08 -05003240 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003241 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003242 if (!PageDirty(page)) {
3243 radix_tree_tag_clear(&page->mapping->page_tree,
3244 page_index(page),
3245 PAGECACHE_TAG_DIRTY);
3246 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003247 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003248 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003249 }
3250 return 0;
3251}
Chris Masond1310b22008-01-24 16:13:08 -05003252
Chris Masond1310b22008-01-24 16:13:08 -05003253int set_extent_buffer_dirty(struct extent_io_tree *tree,
3254 struct extent_buffer *eb)
3255{
3256 unsigned long i;
3257 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003258 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003259
Chris Masonb9473432009-03-13 11:00:37 -04003260 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003261 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003262 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003263 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003264 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003265}
Chris Masond1310b22008-01-24 16:13:08 -05003266
Chris Mason1259ab72008-05-12 13:39:03 -04003267int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003268 struct extent_buffer *eb,
3269 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003270{
3271 unsigned long i;
3272 struct page *page;
3273 unsigned long num_pages;
3274
3275 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003276 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003277
3278 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003279 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003280 for (i = 0; i < num_pages; i++) {
3281 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003282 if (page)
3283 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003284 }
3285 return 0;
3286}
3287
Chris Masond1310b22008-01-24 16:13:08 -05003288int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3289 struct extent_buffer *eb)
3290{
3291 unsigned long i;
3292 struct page *page;
3293 unsigned long num_pages;
3294
3295 num_pages = num_extent_pages(eb->start, eb->len);
3296
3297 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003298 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003299 for (i = 0; i < num_pages; i++) {
3300 page = extent_buffer_page(eb, i);
3301 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3302 ((i == num_pages - 1) &&
3303 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3304 check_page_uptodate(tree, page);
3305 continue;
3306 }
3307 SetPageUptodate(page);
3308 }
3309 return 0;
3310}
Chris Masond1310b22008-01-24 16:13:08 -05003311
Chris Masonce9adaa2008-04-09 16:28:12 -04003312int extent_range_uptodate(struct extent_io_tree *tree,
3313 u64 start, u64 end)
3314{
3315 struct page *page;
3316 int ret;
3317 int pg_uptodate = 1;
3318 int uptodate;
3319 unsigned long index;
3320
Chris Mason9655d292009-09-02 15:22:30 -04003321 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003322 if (ret)
3323 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003324 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003325 index = start >> PAGE_CACHE_SHIFT;
3326 page = find_get_page(tree->mapping, index);
3327 uptodate = PageUptodate(page);
3328 page_cache_release(page);
3329 if (!uptodate) {
3330 pg_uptodate = 0;
3331 break;
3332 }
3333 start += PAGE_CACHE_SIZE;
3334 }
3335 return pg_uptodate;
3336}
3337
Chris Masond1310b22008-01-24 16:13:08 -05003338int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003339 struct extent_buffer *eb,
3340 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003341{
Chris Mason728131d2008-04-09 16:28:12 -04003342 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003343 unsigned long num_pages;
3344 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003345 struct page *page;
3346 int pg_uptodate = 1;
3347
Chris Masonb4ce94d2009-02-04 09:25:08 -05003348 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003349 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003350
Chris Mason42352982008-04-28 16:40:52 -04003351 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003352 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003353 if (ret)
3354 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003355
3356 num_pages = num_extent_pages(eb->start, eb->len);
3357 for (i = 0; i < num_pages; i++) {
3358 page = extent_buffer_page(eb, i);
3359 if (!PageUptodate(page)) {
3360 pg_uptodate = 0;
3361 break;
3362 }
3363 }
Chris Mason42352982008-04-28 16:40:52 -04003364 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003365}
Chris Masond1310b22008-01-24 16:13:08 -05003366
3367int read_extent_buffer_pages(struct extent_io_tree *tree,
3368 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003369 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003370 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003371{
3372 unsigned long i;
3373 unsigned long start_i;
3374 struct page *page;
3375 int err;
3376 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003377 int locked_pages = 0;
3378 int all_uptodate = 1;
3379 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003380 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003381 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003382 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003383
Chris Masonb4ce94d2009-02-04 09:25:08 -05003384 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003385 return 0;
3386
Chris Masonce9adaa2008-04-09 16:28:12 -04003387 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003388 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003389 return 0;
3390 }
3391
3392 if (start) {
3393 WARN_ON(start < eb->start);
3394 start_i = (start >> PAGE_CACHE_SHIFT) -
3395 (eb->start >> PAGE_CACHE_SHIFT);
3396 } else {
3397 start_i = 0;
3398 }
3399
3400 num_pages = num_extent_pages(eb->start, eb->len);
3401 for (i = start_i; i < num_pages; i++) {
3402 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003403 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003404 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003405 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003406 } else {
3407 lock_page(page);
3408 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003409 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003410 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003411 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003412 }
3413 if (all_uptodate) {
3414 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003415 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003416 goto unlock_exit;
3417 }
3418
3419 for (i = start_i; i < num_pages; i++) {
3420 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003421
3422 WARN_ON(!PagePrivate(page));
3423
3424 set_page_extent_mapped(page);
3425 if (i == 0)
3426 set_page_extent_head(page, eb->len);
3427
Chris Masonce9adaa2008-04-09 16:28:12 -04003428 if (inc_all_pages)
3429 page_cache_get(page);
3430 if (!PageUptodate(page)) {
3431 if (start_i == 0)
3432 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003433 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003434 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003435 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003436 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003437 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003438 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003439 } else {
3440 unlock_page(page);
3441 }
3442 }
3443
Chris Masona86c12c2008-02-07 10:50:54 -05003444 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003445 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003446
Chris Masond3977122009-01-05 21:25:51 -05003447 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003448 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003449
Chris Masond1310b22008-01-24 16:13:08 -05003450 for (i = start_i; i < num_pages; i++) {
3451 page = extent_buffer_page(eb, i);
3452 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003453 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003454 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003455 }
Chris Masond3977122009-01-05 21:25:51 -05003456
Chris Masond1310b22008-01-24 16:13:08 -05003457 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003458 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003459 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003460
3461unlock_exit:
3462 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003463 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003464 page = extent_buffer_page(eb, i);
3465 i++;
3466 unlock_page(page);
3467 locked_pages--;
3468 }
3469 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003470}
Chris Masond1310b22008-01-24 16:13:08 -05003471
3472void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3473 unsigned long start,
3474 unsigned long len)
3475{
3476 size_t cur;
3477 size_t offset;
3478 struct page *page;
3479 char *kaddr;
3480 char *dst = (char *)dstv;
3481 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3482 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003483
3484 WARN_ON(start > eb->len);
3485 WARN_ON(start + len > eb->start + eb->len);
3486
3487 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3488
Chris Masond3977122009-01-05 21:25:51 -05003489 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003490 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003491
3492 cur = min(len, (PAGE_CACHE_SIZE - offset));
3493 kaddr = kmap_atomic(page, KM_USER1);
3494 memcpy(dst, kaddr + offset, cur);
3495 kunmap_atomic(kaddr, KM_USER1);
3496
3497 dst += cur;
3498 len -= cur;
3499 offset = 0;
3500 i++;
3501 }
3502}
Chris Masond1310b22008-01-24 16:13:08 -05003503
3504int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3505 unsigned long min_len, char **token, char **map,
3506 unsigned long *map_start,
3507 unsigned long *map_len, int km)
3508{
3509 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3510 char *kaddr;
3511 struct page *p;
3512 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3513 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3514 unsigned long end_i = (start_offset + start + min_len - 1) >>
3515 PAGE_CACHE_SHIFT;
3516
3517 if (i != end_i)
3518 return -EINVAL;
3519
3520 if (i == 0) {
3521 offset = start_offset;
3522 *map_start = 0;
3523 } else {
3524 offset = 0;
3525 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3526 }
Chris Masond3977122009-01-05 21:25:51 -05003527
Chris Masond1310b22008-01-24 16:13:08 -05003528 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003529 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3530 "wanted %lu %lu\n", (unsigned long long)eb->start,
3531 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003532 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003533 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003534 }
3535
3536 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003537 kaddr = kmap_atomic(p, km);
3538 *token = kaddr;
3539 *map = kaddr + offset;
3540 *map_len = PAGE_CACHE_SIZE - offset;
3541 return 0;
3542}
Chris Masond1310b22008-01-24 16:13:08 -05003543
3544int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3545 unsigned long min_len,
3546 char **token, char **map,
3547 unsigned long *map_start,
3548 unsigned long *map_len, int km)
3549{
3550 int err;
3551 int save = 0;
3552 if (eb->map_token) {
3553 unmap_extent_buffer(eb, eb->map_token, km);
3554 eb->map_token = NULL;
3555 save = 1;
3556 }
3557 err = map_private_extent_buffer(eb, start, min_len, token, map,
3558 map_start, map_len, km);
3559 if (!err && save) {
3560 eb->map_token = *token;
3561 eb->kaddr = *map;
3562 eb->map_start = *map_start;
3563 eb->map_len = *map_len;
3564 }
3565 return err;
3566}
Chris Masond1310b22008-01-24 16:13:08 -05003567
3568void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3569{
3570 kunmap_atomic(token, km);
3571}
Chris Masond1310b22008-01-24 16:13:08 -05003572
3573int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3574 unsigned long start,
3575 unsigned long len)
3576{
3577 size_t cur;
3578 size_t offset;
3579 struct page *page;
3580 char *kaddr;
3581 char *ptr = (char *)ptrv;
3582 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3583 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3584 int ret = 0;
3585
3586 WARN_ON(start > eb->len);
3587 WARN_ON(start + len > eb->start + eb->len);
3588
3589 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3590
Chris Masond3977122009-01-05 21:25:51 -05003591 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003592 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003593
3594 cur = min(len, (PAGE_CACHE_SIZE - offset));
3595
3596 kaddr = kmap_atomic(page, KM_USER0);
3597 ret = memcmp(ptr, kaddr + offset, cur);
3598 kunmap_atomic(kaddr, KM_USER0);
3599 if (ret)
3600 break;
3601
3602 ptr += cur;
3603 len -= cur;
3604 offset = 0;
3605 i++;
3606 }
3607 return ret;
3608}
Chris Masond1310b22008-01-24 16:13:08 -05003609
3610void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3611 unsigned long start, unsigned long len)
3612{
3613 size_t cur;
3614 size_t offset;
3615 struct page *page;
3616 char *kaddr;
3617 char *src = (char *)srcv;
3618 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3619 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3620
3621 WARN_ON(start > eb->len);
3622 WARN_ON(start + len > eb->start + eb->len);
3623
3624 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3625
Chris Masond3977122009-01-05 21:25:51 -05003626 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003627 page = extent_buffer_page(eb, i);
3628 WARN_ON(!PageUptodate(page));
3629
3630 cur = min(len, PAGE_CACHE_SIZE - offset);
3631 kaddr = kmap_atomic(page, KM_USER1);
3632 memcpy(kaddr + offset, src, cur);
3633 kunmap_atomic(kaddr, KM_USER1);
3634
3635 src += cur;
3636 len -= cur;
3637 offset = 0;
3638 i++;
3639 }
3640}
Chris Masond1310b22008-01-24 16:13:08 -05003641
3642void memset_extent_buffer(struct extent_buffer *eb, char c,
3643 unsigned long start, unsigned long len)
3644{
3645 size_t cur;
3646 size_t offset;
3647 struct page *page;
3648 char *kaddr;
3649 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3650 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3651
3652 WARN_ON(start > eb->len);
3653 WARN_ON(start + len > eb->start + eb->len);
3654
3655 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3656
Chris Masond3977122009-01-05 21:25:51 -05003657 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003658 page = extent_buffer_page(eb, i);
3659 WARN_ON(!PageUptodate(page));
3660
3661 cur = min(len, PAGE_CACHE_SIZE - offset);
3662 kaddr = kmap_atomic(page, KM_USER0);
3663 memset(kaddr + offset, c, cur);
3664 kunmap_atomic(kaddr, KM_USER0);
3665
3666 len -= cur;
3667 offset = 0;
3668 i++;
3669 }
3670}
Chris Masond1310b22008-01-24 16:13:08 -05003671
3672void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3673 unsigned long dst_offset, unsigned long src_offset,
3674 unsigned long len)
3675{
3676 u64 dst_len = dst->len;
3677 size_t cur;
3678 size_t offset;
3679 struct page *page;
3680 char *kaddr;
3681 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3682 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3683
3684 WARN_ON(src->len != dst_len);
3685
3686 offset = (start_offset + dst_offset) &
3687 ((unsigned long)PAGE_CACHE_SIZE - 1);
3688
Chris Masond3977122009-01-05 21:25:51 -05003689 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003690 page = extent_buffer_page(dst, i);
3691 WARN_ON(!PageUptodate(page));
3692
3693 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3694
3695 kaddr = kmap_atomic(page, KM_USER0);
3696 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3697 kunmap_atomic(kaddr, KM_USER0);
3698
3699 src_offset += cur;
3700 len -= cur;
3701 offset = 0;
3702 i++;
3703 }
3704}
Chris Masond1310b22008-01-24 16:13:08 -05003705
3706static void move_pages(struct page *dst_page, struct page *src_page,
3707 unsigned long dst_off, unsigned long src_off,
3708 unsigned long len)
3709{
3710 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3711 if (dst_page == src_page) {
3712 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3713 } else {
3714 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3715 char *p = dst_kaddr + dst_off + len;
3716 char *s = src_kaddr + src_off + len;
3717
3718 while (len--)
3719 *--p = *--s;
3720
3721 kunmap_atomic(src_kaddr, KM_USER1);
3722 }
3723 kunmap_atomic(dst_kaddr, KM_USER0);
3724}
3725
Sergei Trofimovich33872062011-04-11 21:52:52 +00003726static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3727{
3728 unsigned long distance = (src > dst) ? src - dst : dst - src;
3729 return distance < len;
3730}
3731
Chris Masond1310b22008-01-24 16:13:08 -05003732static void copy_pages(struct page *dst_page, struct page *src_page,
3733 unsigned long dst_off, unsigned long src_off,
3734 unsigned long len)
3735{
3736 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3737 char *src_kaddr;
3738
Sergei Trofimovich33872062011-04-11 21:52:52 +00003739 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003740 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003741 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003742 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003743 BUG_ON(areas_overlap(src_off, dst_off, len));
3744 }
Chris Masond1310b22008-01-24 16:13:08 -05003745
3746 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3747 kunmap_atomic(dst_kaddr, KM_USER0);
3748 if (dst_page != src_page)
3749 kunmap_atomic(src_kaddr, KM_USER1);
3750}
3751
3752void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3753 unsigned long src_offset, unsigned long len)
3754{
3755 size_t cur;
3756 size_t dst_off_in_page;
3757 size_t src_off_in_page;
3758 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3759 unsigned long dst_i;
3760 unsigned long src_i;
3761
3762 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003763 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3764 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003765 BUG_ON(1);
3766 }
3767 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003768 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3769 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003770 BUG_ON(1);
3771 }
3772
Chris Masond3977122009-01-05 21:25:51 -05003773 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003774 dst_off_in_page = (start_offset + dst_offset) &
3775 ((unsigned long)PAGE_CACHE_SIZE - 1);
3776 src_off_in_page = (start_offset + src_offset) &
3777 ((unsigned long)PAGE_CACHE_SIZE - 1);
3778
3779 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3780 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3781
3782 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3783 src_off_in_page));
3784 cur = min_t(unsigned long, cur,
3785 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3786
3787 copy_pages(extent_buffer_page(dst, dst_i),
3788 extent_buffer_page(dst, src_i),
3789 dst_off_in_page, src_off_in_page, cur);
3790
3791 src_offset += cur;
3792 dst_offset += cur;
3793 len -= cur;
3794 }
3795}
Chris Masond1310b22008-01-24 16:13:08 -05003796
3797void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3798 unsigned long src_offset, unsigned long len)
3799{
3800 size_t cur;
3801 size_t dst_off_in_page;
3802 size_t src_off_in_page;
3803 unsigned long dst_end = dst_offset + len - 1;
3804 unsigned long src_end = src_offset + len - 1;
3805 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3806 unsigned long dst_i;
3807 unsigned long src_i;
3808
3809 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003810 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3811 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003812 BUG_ON(1);
3813 }
3814 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003815 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3816 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003817 BUG_ON(1);
3818 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00003819 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05003820 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3821 return;
3822 }
Chris Masond3977122009-01-05 21:25:51 -05003823 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003824 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3825 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3826
3827 dst_off_in_page = (start_offset + dst_end) &
3828 ((unsigned long)PAGE_CACHE_SIZE - 1);
3829 src_off_in_page = (start_offset + src_end) &
3830 ((unsigned long)PAGE_CACHE_SIZE - 1);
3831
3832 cur = min_t(unsigned long, len, src_off_in_page + 1);
3833 cur = min(cur, dst_off_in_page + 1);
3834 move_pages(extent_buffer_page(dst, dst_i),
3835 extent_buffer_page(dst, src_i),
3836 dst_off_in_page - cur + 1,
3837 src_off_in_page - cur + 1, cur);
3838
3839 dst_end -= cur;
3840 src_end -= cur;
3841 len -= cur;
3842 }
3843}
Chris Mason6af118c2008-07-22 11:18:07 -04003844
Miao Xie19fe0a82010-10-26 20:57:29 -04003845static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3846{
3847 struct extent_buffer *eb =
3848 container_of(head, struct extent_buffer, rcu_head);
3849
3850 btrfs_release_extent_buffer(eb);
3851}
3852
Chris Mason6af118c2008-07-22 11:18:07 -04003853int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3854{
3855 u64 start = page_offset(page);
3856 struct extent_buffer *eb;
3857 int ret = 1;
Chris Mason6af118c2008-07-22 11:18:07 -04003858
3859 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003860 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05003861 if (!eb) {
3862 spin_unlock(&tree->buffer_lock);
3863 return ret;
3864 }
Chris Mason6af118c2008-07-22 11:18:07 -04003865
Chris Masonb9473432009-03-13 11:00:37 -04003866 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3867 ret = 0;
3868 goto out;
3869 }
Miao Xie897ca6e2010-10-26 20:57:29 -04003870
Miao Xie19fe0a82010-10-26 20:57:29 -04003871 /*
3872 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3873 * Or go back.
3874 */
3875 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
3876 ret = 0;
3877 goto out;
3878 }
3879
3880 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003881out:
3882 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003883
3884 /* at this point we can safely release the extent buffer */
3885 if (atomic_read(&eb->refs) == 0)
3886 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118c2008-07-22 11:18:07 -04003887 return ret;
3888}