blob: 4f9893243dae26be226c3a72d09a966e36b645d1 [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,
106 struct address_space *mapping, gfp_t mask)
107{
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
444/*
445 * clear some bits on a range in the tree. This may require splitting
446 * or inserting elements in the tree, so the gfp mask is used to
447 * indicate which allocations or sleeping are allowed.
448 *
449 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
450 * the given range from the tree regardless of state (ie for truncate).
451 *
452 * the range [start, end] is inclusive.
453 *
454 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
455 * bits were already set, or zero if none of the bits were already set.
456 */
457int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400458 int bits, int wake, int delete,
459 struct extent_state **cached_state,
460 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500461{
462 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400463 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500464 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400465 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500466 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400467 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500468 int err;
469 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000470 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500471
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400472 if (delete)
473 bits |= ~EXTENT_CTLBITS;
474 bits |= EXTENT_FIRST_DELALLOC;
475
Josef Bacik2ac55d42010-02-03 19:33:23 +0000476 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
477 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500478again:
479 if (!prealloc && (mask & __GFP_WAIT)) {
480 prealloc = alloc_extent_state(mask);
481 if (!prealloc)
482 return -ENOMEM;
483 }
484
Chris Masoncad321a2008-12-17 14:51:42 -0500485 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400486 if (cached_state) {
487 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000488
489 if (clear) {
490 *cached_state = NULL;
491 cached_state = NULL;
492 }
493
Chris Mason42daec22009-09-23 19:51:09 -0400494 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000495 if (clear)
496 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400497 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400498 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400499 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000500 if (clear)
501 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400502 }
Chris Masond1310b22008-01-24 16:13:08 -0500503 /*
504 * this search will find the extents that end after
505 * our range starts
506 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500507 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500508 if (!node)
509 goto out;
510 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400511hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500512 if (state->start > end)
513 goto out;
514 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400515 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500516
517 /*
518 * | ---- desired range ---- |
519 * | state | or
520 * | ------------- state -------------- |
521 *
522 * We need to split the extent we found, and may flip
523 * bits on second half.
524 *
525 * If the extent we found extends past our range, we
526 * just split and search again. It'll get split again
527 * the next time though.
528 *
529 * If the extent we found is inside our range, we clear
530 * the desired bit on it.
531 */
532
533 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500534 if (!prealloc)
535 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500536 err = split_state(tree, state, prealloc, start);
537 BUG_ON(err == -EEXIST);
538 prealloc = NULL;
539 if (err)
540 goto out;
541 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400542 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400543 if (last_end == (u64)-1)
544 goto out;
545 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500546 }
547 goto search_again;
548 }
549 /*
550 * | ---- desired range ---- |
551 * | state |
552 * We need to split the extent, and clear the bit
553 * on the first half
554 */
555 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500556 if (!prealloc)
557 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500558 err = split_state(tree, state, prealloc, end + 1);
559 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500560 if (wake)
561 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400562
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400563 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400564
Chris Masond1310b22008-01-24 16:13:08 -0500565 prealloc = NULL;
566 goto out;
567 }
Chris Mason42daec22009-09-23 19:51:09 -0400568
Chris Mason2c64c532009-09-02 15:04:12 -0400569 if (state->end < end && prealloc && !need_resched())
570 next_node = rb_next(&state->rb_node);
571 else
572 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400573
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400574 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400575 if (last_end == (u64)-1)
576 goto out;
577 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400578 if (start <= end && next_node) {
579 state = rb_entry(next_node, struct extent_state,
580 rb_node);
581 if (state->start == start)
582 goto hit_next;
583 }
Chris Masond1310b22008-01-24 16:13:08 -0500584 goto search_again;
585
586out:
Chris Masoncad321a2008-12-17 14:51:42 -0500587 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500588 if (prealloc)
589 free_extent_state(prealloc);
590
591 return set;
592
593search_again:
594 if (start > end)
595 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500596 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500597 if (mask & __GFP_WAIT)
598 cond_resched();
599 goto again;
600}
Chris Masond1310b22008-01-24 16:13:08 -0500601
602static int wait_on_state(struct extent_io_tree *tree,
603 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500604 __releases(tree->lock)
605 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500606{
607 DEFINE_WAIT(wait);
608 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500609 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500610 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500611 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500612 finish_wait(&state->wq, &wait);
613 return 0;
614}
615
616/*
617 * waits for one or more bits to clear on a range in the state tree.
618 * The range [start, end] is inclusive.
619 * The tree lock is taken by this function
620 */
621int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
622{
623 struct extent_state *state;
624 struct rb_node *node;
625
Chris Masoncad321a2008-12-17 14:51:42 -0500626 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500627again:
628 while (1) {
629 /*
630 * this search will find all the extents that end after
631 * our range starts
632 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500633 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500634 if (!node)
635 break;
636
637 state = rb_entry(node, struct extent_state, rb_node);
638
639 if (state->start > end)
640 goto out;
641
642 if (state->state & bits) {
643 start = state->start;
644 atomic_inc(&state->refs);
645 wait_on_state(tree, state);
646 free_extent_state(state);
647 goto again;
648 }
649 start = state->end + 1;
650
651 if (start > end)
652 break;
653
654 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500655 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500656 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500657 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500658 }
659 }
660out:
Chris Masoncad321a2008-12-17 14:51:42 -0500661 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500662 return 0;
663}
Chris Masond1310b22008-01-24 16:13:08 -0500664
Josef Bacik9ed74f22009-09-11 16:12:44 -0400665static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500666 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400667 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500668{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400669 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400670 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400671
672 ret = set_state_cb(tree, state, bits);
673 if (ret)
674 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400675 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500676 u64 range = state->end - state->start + 1;
677 tree->dirty_bytes += range;
678 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400679 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400680
681 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500682}
683
Chris Mason2c64c532009-09-02 15:04:12 -0400684static void cache_state(struct extent_state *state,
685 struct extent_state **cached_ptr)
686{
687 if (cached_ptr && !(*cached_ptr)) {
688 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
689 *cached_ptr = state;
690 atomic_inc(&state->refs);
691 }
692 }
693}
694
Arne Jansen507903b2011-04-06 10:02:20 +0000695static void uncache_state(struct extent_state **cached_ptr)
696{
697 if (cached_ptr && (*cached_ptr)) {
698 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400699 *cached_ptr = NULL;
700 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000701 }
702}
703
Chris Masond1310b22008-01-24 16:13:08 -0500704/*
Chris Mason1edbb732009-09-02 13:24:36 -0400705 * set some bits on a range in the tree. This may require allocations or
706 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500707 *
Chris Mason1edbb732009-09-02 13:24:36 -0400708 * If any of the exclusive bits are set, this will fail with -EEXIST if some
709 * part of the range already has the desired bits set. The start of the
710 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500711 *
Chris Mason1edbb732009-09-02 13:24:36 -0400712 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500713 */
Chris Mason1edbb732009-09-02 13:24:36 -0400714
Chris Mason4845e442010-05-25 20:56:50 -0400715int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
716 int bits, int exclusive_bits, u64 *failed_start,
717 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500718{
719 struct extent_state *state;
720 struct extent_state *prealloc = NULL;
721 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500722 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500723 u64 last_start;
724 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400725
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400726 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500727again:
728 if (!prealloc && (mask & __GFP_WAIT)) {
729 prealloc = alloc_extent_state(mask);
730 if (!prealloc)
731 return -ENOMEM;
732 }
733
Chris Masoncad321a2008-12-17 14:51:42 -0500734 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400735 if (cached_state && *cached_state) {
736 state = *cached_state;
737 if (state->start == start && state->tree) {
738 node = &state->rb_node;
739 goto hit_next;
740 }
741 }
Chris Masond1310b22008-01-24 16:13:08 -0500742 /*
743 * this search will find all the extents that end after
744 * our range starts.
745 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500746 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500747 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400748 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500749 prealloc = NULL;
750 BUG_ON(err == -EEXIST);
751 goto out;
752 }
Chris Masond1310b22008-01-24 16:13:08 -0500753 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400754hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500755 last_start = state->start;
756 last_end = state->end;
757
758 /*
759 * | ---- desired range ---- |
760 * | state |
761 *
762 * Just lock what we found and keep going
763 */
764 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400765 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400766 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500767 *failed_start = state->start;
768 err = -EEXIST;
769 goto out;
770 }
Chris Mason42daec22009-09-23 19:51:09 -0400771
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400772 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400773 if (err)
774 goto out;
775
Chris Mason2c64c532009-09-02 15:04:12 -0400776 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500777 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400778 if (last_end == (u64)-1)
779 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400780
Yan Zheng5c939df2009-05-27 09:16:03 -0400781 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400782 if (start < end && prealloc && !need_resched()) {
783 next_node = rb_next(node);
784 if (next_node) {
785 state = rb_entry(next_node, struct extent_state,
786 rb_node);
787 if (state->start == start)
788 goto hit_next;
789 }
790 }
Chris Masond1310b22008-01-24 16:13:08 -0500791 goto search_again;
792 }
793
794 /*
795 * | ---- desired range ---- |
796 * | state |
797 * or
798 * | ------------- state -------------- |
799 *
800 * We need to split the extent we found, and may flip bits on
801 * second half.
802 *
803 * If the extent we found extends past our
804 * range, we just split and search again. It'll get split
805 * again the next time though.
806 *
807 * If the extent we found is inside our range, we set the
808 * desired bit on it.
809 */
810 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400811 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500812 *failed_start = start;
813 err = -EEXIST;
814 goto out;
815 }
816 err = split_state(tree, state, prealloc, start);
817 BUG_ON(err == -EEXIST);
818 prealloc = NULL;
819 if (err)
820 goto out;
821 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400822 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400823 if (err)
824 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400825 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500826 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400827 if (last_end == (u64)-1)
828 goto out;
829 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500830 }
831 goto search_again;
832 }
833 /*
834 * | ---- desired range ---- |
835 * | state | or | state |
836 *
837 * There's a hole, we need to insert something in it and
838 * ignore the extent we found.
839 */
840 if (state->start > start) {
841 u64 this_end;
842 if (end < last_start)
843 this_end = end;
844 else
Chris Masond3977122009-01-05 21:25:51 -0500845 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500846 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400847 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400848 BUG_ON(err == -EEXIST);
849 if (err) {
850 prealloc = NULL;
851 goto out;
852 }
Chris Mason2c64c532009-09-02 15:04:12 -0400853 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500854 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500855 start = this_end + 1;
856 goto search_again;
857 }
858 /*
859 * | ---- desired range ---- |
860 * | state |
861 * We need to split the extent, and set the bit
862 * on the first half
863 */
864 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400865 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500866 *failed_start = start;
867 err = -EEXIST;
868 goto out;
869 }
870 err = split_state(tree, state, prealloc, end + 1);
871 BUG_ON(err == -EEXIST);
872
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400873 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400874 if (err) {
875 prealloc = NULL;
876 goto out;
877 }
Chris Mason2c64c532009-09-02 15:04:12 -0400878 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500879 merge_state(tree, prealloc);
880 prealloc = NULL;
881 goto out;
882 }
883
884 goto search_again;
885
886out:
Chris Masoncad321a2008-12-17 14:51:42 -0500887 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500888 if (prealloc)
889 free_extent_state(prealloc);
890
891 return err;
892
893search_again:
894 if (start > end)
895 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500896 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500897 if (mask & __GFP_WAIT)
898 cond_resched();
899 goto again;
900}
Chris Masond1310b22008-01-24 16:13:08 -0500901
902/* wrappers around set/clear extent bit */
903int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
904 gfp_t mask)
905{
906 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400907 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500908}
Chris Masond1310b22008-01-24 16:13:08 -0500909
910int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
911 int bits, gfp_t mask)
912{
913 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400914 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500915}
Chris Masond1310b22008-01-24 16:13:08 -0500916
917int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
918 int bits, gfp_t mask)
919{
Chris Mason2c64c532009-09-02 15:04:12 -0400920 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500921}
Chris Masond1310b22008-01-24 16:13:08 -0500922
923int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000924 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500925{
926 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400927 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000928 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500929}
Chris Masond1310b22008-01-24 16:13:08 -0500930
931int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
932 gfp_t mask)
933{
934 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400935 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400936 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500937}
Chris Masond1310b22008-01-24 16:13:08 -0500938
939int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
940 gfp_t mask)
941{
942 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400943 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500944}
Chris Masond1310b22008-01-24 16:13:08 -0500945
Christoph Hellwigb2950862008-12-02 09:54:17 -0500946static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500947 gfp_t mask)
948{
Chris Mason2c64c532009-09-02 15:04:12 -0400949 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
950 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500951}
Chris Masond1310b22008-01-24 16:13:08 -0500952
953int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000954 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500955{
Arne Jansen507903b2011-04-06 10:02:20 +0000956 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
957 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500958}
Chris Masond1310b22008-01-24 16:13:08 -0500959
Chris Masond3977122009-01-05 21:25:51 -0500960static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000961 u64 end, struct extent_state **cached_state,
962 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500963{
Chris Mason2c64c532009-09-02 15:04:12 -0400964 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000965 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500966}
Chris Masond1310b22008-01-24 16:13:08 -0500967
Chris Masond1310b22008-01-24 16:13:08 -0500968int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
969{
970 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
971}
Chris Masond1310b22008-01-24 16:13:08 -0500972
Chris Masond352ac62008-09-29 15:18:18 -0400973/*
974 * either insert or lock state struct between start and end use mask to tell
975 * us if waiting is desired.
976 */
Chris Mason1edbb732009-09-02 13:24:36 -0400977int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400978 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500979{
980 int err;
981 u64 failed_start;
982 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400983 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400984 EXTENT_LOCKED, &failed_start,
985 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500986 if (err == -EEXIST && (mask & __GFP_WAIT)) {
987 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
988 start = failed_start;
989 } else {
990 break;
991 }
992 WARN_ON(start > end);
993 }
994 return err;
995}
Chris Masond1310b22008-01-24 16:13:08 -0500996
Chris Mason1edbb732009-09-02 13:24:36 -0400997int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
998{
Chris Mason2c64c532009-09-02 15:04:12 -0400999 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001000}
1001
Josef Bacik25179202008-10-29 14:49:05 -04001002int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1003 gfp_t mask)
1004{
1005 int err;
1006 u64 failed_start;
1007
Chris Mason2c64c532009-09-02 15:04:12 -04001008 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1009 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001010 if (err == -EEXIST) {
1011 if (failed_start > start)
1012 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001013 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001014 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001015 }
Josef Bacik25179202008-10-29 14:49:05 -04001016 return 1;
1017}
Josef Bacik25179202008-10-29 14:49:05 -04001018
Chris Mason2c64c532009-09-02 15:04:12 -04001019int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1020 struct extent_state **cached, gfp_t mask)
1021{
1022 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1023 mask);
1024}
1025
Arne Jansen507903b2011-04-06 10:02:20 +00001026int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001027{
Chris Mason2c64c532009-09-02 15:04:12 -04001028 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1029 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001030}
Chris Masond1310b22008-01-24 16:13:08 -05001031
1032/*
1033 * helper function to set pages and extents in the tree dirty
1034 */
1035int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1036{
1037 unsigned long index = start >> PAGE_CACHE_SHIFT;
1038 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1039 struct page *page;
1040
1041 while (index <= end_index) {
1042 page = find_get_page(tree->mapping, index);
1043 BUG_ON(!page);
1044 __set_page_dirty_nobuffers(page);
1045 page_cache_release(page);
1046 index++;
1047 }
Chris Masond1310b22008-01-24 16:13:08 -05001048 return 0;
1049}
Chris Masond1310b22008-01-24 16:13:08 -05001050
1051/*
1052 * helper function to set both pages and extents in the tree writeback
1053 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001054static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001055{
1056 unsigned long index = start >> PAGE_CACHE_SHIFT;
1057 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1058 struct page *page;
1059
1060 while (index <= end_index) {
1061 page = find_get_page(tree->mapping, index);
1062 BUG_ON(!page);
1063 set_page_writeback(page);
1064 page_cache_release(page);
1065 index++;
1066 }
Chris Masond1310b22008-01-24 16:13:08 -05001067 return 0;
1068}
Chris Masond1310b22008-01-24 16:13:08 -05001069
Chris Masond352ac62008-09-29 15:18:18 -04001070/*
1071 * find the first offset in the io tree with 'bits' set. zero is
1072 * returned if we find something, and *start_ret and *end_ret are
1073 * set to reflect the state struct that was found.
1074 *
1075 * If nothing was found, 1 is returned, < 0 on error
1076 */
Chris Masond1310b22008-01-24 16:13:08 -05001077int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1078 u64 *start_ret, u64 *end_ret, int bits)
1079{
1080 struct rb_node *node;
1081 struct extent_state *state;
1082 int ret = 1;
1083
Chris Masoncad321a2008-12-17 14:51:42 -05001084 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001085 /*
1086 * this search will find all the extents that end after
1087 * our range starts.
1088 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001089 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001090 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001091 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001092
Chris Masond3977122009-01-05 21:25:51 -05001093 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001094 state = rb_entry(node, struct extent_state, rb_node);
1095 if (state->end >= start && (state->state & bits)) {
1096 *start_ret = state->start;
1097 *end_ret = state->end;
1098 ret = 0;
1099 break;
1100 }
1101 node = rb_next(node);
1102 if (!node)
1103 break;
1104 }
1105out:
Chris Masoncad321a2008-12-17 14:51:42 -05001106 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001107 return ret;
1108}
Chris Masond1310b22008-01-24 16:13:08 -05001109
Chris Masond352ac62008-09-29 15:18:18 -04001110/* find the first state struct with 'bits' set after 'start', and
1111 * return it. tree->lock must be held. NULL will returned if
1112 * nothing was found after 'start'
1113 */
Chris Masond7fc6402008-02-18 12:12:38 -05001114struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1115 u64 start, int bits)
1116{
1117 struct rb_node *node;
1118 struct extent_state *state;
1119
1120 /*
1121 * this search will find all the extents that end after
1122 * our range starts.
1123 */
1124 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001125 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001126 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001127
Chris Masond3977122009-01-05 21:25:51 -05001128 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001129 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001130 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001131 return state;
Chris Masond3977122009-01-05 21:25:51 -05001132
Chris Masond7fc6402008-02-18 12:12:38 -05001133 node = rb_next(node);
1134 if (!node)
1135 break;
1136 }
1137out:
1138 return NULL;
1139}
Chris Masond7fc6402008-02-18 12:12:38 -05001140
Chris Masond352ac62008-09-29 15:18:18 -04001141/*
1142 * find a contiguous range of bytes in the file marked as delalloc, not
1143 * more than 'max_bytes'. start and end are used to return the range,
1144 *
1145 * 1 is returned if we find something, 0 if nothing was in the tree
1146 */
Chris Masonc8b97812008-10-29 14:49:59 -04001147static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001148 u64 *start, u64 *end, u64 max_bytes,
1149 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001150{
1151 struct rb_node *node;
1152 struct extent_state *state;
1153 u64 cur_start = *start;
1154 u64 found = 0;
1155 u64 total_bytes = 0;
1156
Chris Masoncad321a2008-12-17 14:51:42 -05001157 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001158
Chris Masond1310b22008-01-24 16:13:08 -05001159 /*
1160 * this search will find all the extents that end after
1161 * our range starts.
1162 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001163 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001164 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001165 if (!found)
1166 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001167 goto out;
1168 }
1169
Chris Masond3977122009-01-05 21:25:51 -05001170 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001171 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001172 if (found && (state->start != cur_start ||
1173 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001174 goto out;
1175 }
1176 if (!(state->state & EXTENT_DELALLOC)) {
1177 if (!found)
1178 *end = state->end;
1179 goto out;
1180 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001181 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001182 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001183 *cached_state = state;
1184 atomic_inc(&state->refs);
1185 }
Chris Masond1310b22008-01-24 16:13:08 -05001186 found++;
1187 *end = state->end;
1188 cur_start = state->end + 1;
1189 node = rb_next(node);
1190 if (!node)
1191 break;
1192 total_bytes += state->end - state->start + 1;
1193 if (total_bytes >= max_bytes)
1194 break;
1195 }
1196out:
Chris Masoncad321a2008-12-17 14:51:42 -05001197 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001198 return found;
1199}
1200
Chris Masonc8b97812008-10-29 14:49:59 -04001201static noinline int __unlock_for_delalloc(struct inode *inode,
1202 struct page *locked_page,
1203 u64 start, u64 end)
1204{
1205 int ret;
1206 struct page *pages[16];
1207 unsigned long index = start >> PAGE_CACHE_SHIFT;
1208 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1209 unsigned long nr_pages = end_index - index + 1;
1210 int i;
1211
1212 if (index == locked_page->index && end_index == index)
1213 return 0;
1214
Chris Masond3977122009-01-05 21:25:51 -05001215 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001216 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001217 min_t(unsigned long, nr_pages,
1218 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001219 for (i = 0; i < ret; i++) {
1220 if (pages[i] != locked_page)
1221 unlock_page(pages[i]);
1222 page_cache_release(pages[i]);
1223 }
1224 nr_pages -= ret;
1225 index += ret;
1226 cond_resched();
1227 }
1228 return 0;
1229}
1230
1231static noinline int lock_delalloc_pages(struct inode *inode,
1232 struct page *locked_page,
1233 u64 delalloc_start,
1234 u64 delalloc_end)
1235{
1236 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1237 unsigned long start_index = index;
1238 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1239 unsigned long pages_locked = 0;
1240 struct page *pages[16];
1241 unsigned long nrpages;
1242 int ret;
1243 int i;
1244
1245 /* the caller is responsible for locking the start index */
1246 if (index == locked_page->index && index == end_index)
1247 return 0;
1248
1249 /* skip the page at the start index */
1250 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001251 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001252 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001253 min_t(unsigned long,
1254 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001255 if (ret == 0) {
1256 ret = -EAGAIN;
1257 goto done;
1258 }
1259 /* now we have an array of pages, lock them all */
1260 for (i = 0; i < ret; i++) {
1261 /*
1262 * the caller is taking responsibility for
1263 * locked_page
1264 */
Chris Mason771ed682008-11-06 22:02:51 -05001265 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001266 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001267 if (!PageDirty(pages[i]) ||
1268 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001269 ret = -EAGAIN;
1270 unlock_page(pages[i]);
1271 page_cache_release(pages[i]);
1272 goto done;
1273 }
1274 }
Chris Masonc8b97812008-10-29 14:49:59 -04001275 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001276 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001277 }
Chris Masonc8b97812008-10-29 14:49:59 -04001278 nrpages -= ret;
1279 index += ret;
1280 cond_resched();
1281 }
1282 ret = 0;
1283done:
1284 if (ret && pages_locked) {
1285 __unlock_for_delalloc(inode, locked_page,
1286 delalloc_start,
1287 ((u64)(start_index + pages_locked - 1)) <<
1288 PAGE_CACHE_SHIFT);
1289 }
1290 return ret;
1291}
1292
1293/*
1294 * find a contiguous range of bytes in the file marked as delalloc, not
1295 * more than 'max_bytes'. start and end are used to return the range,
1296 *
1297 * 1 is returned if we find something, 0 if nothing was in the tree
1298 */
1299static noinline u64 find_lock_delalloc_range(struct inode *inode,
1300 struct extent_io_tree *tree,
1301 struct page *locked_page,
1302 u64 *start, u64 *end,
1303 u64 max_bytes)
1304{
1305 u64 delalloc_start;
1306 u64 delalloc_end;
1307 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001308 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001309 int ret;
1310 int loops = 0;
1311
1312again:
1313 /* step one, find a bunch of delalloc bytes starting at start */
1314 delalloc_start = *start;
1315 delalloc_end = 0;
1316 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001317 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001318 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001319 *start = delalloc_start;
1320 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001321 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001322 return found;
1323 }
1324
1325 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001326 * start comes from the offset of locked_page. We have to lock
1327 * pages in order, so we can't process delalloc bytes before
1328 * locked_page
1329 */
Chris Masond3977122009-01-05 21:25:51 -05001330 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001331 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001332
1333 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001334 * make sure to limit the number of pages we try to lock down
1335 * if we're looping.
1336 */
Chris Masond3977122009-01-05 21:25:51 -05001337 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001338 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001339
Chris Masonc8b97812008-10-29 14:49:59 -04001340 /* step two, lock all the pages after the page that has start */
1341 ret = lock_delalloc_pages(inode, locked_page,
1342 delalloc_start, delalloc_end);
1343 if (ret == -EAGAIN) {
1344 /* some of the pages are gone, lets avoid looping by
1345 * shortening the size of the delalloc range we're searching
1346 */
Chris Mason9655d292009-09-02 15:22:30 -04001347 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001348 if (!loops) {
1349 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1350 max_bytes = PAGE_CACHE_SIZE - offset;
1351 loops = 1;
1352 goto again;
1353 } else {
1354 found = 0;
1355 goto out_failed;
1356 }
1357 }
1358 BUG_ON(ret);
1359
1360 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001361 lock_extent_bits(tree, delalloc_start, delalloc_end,
1362 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001363
1364 /* then test to make sure it is all still delalloc */
1365 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001366 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001367 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001368 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1369 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001370 __unlock_for_delalloc(inode, locked_page,
1371 delalloc_start, delalloc_end);
1372 cond_resched();
1373 goto again;
1374 }
Chris Mason9655d292009-09-02 15:22:30 -04001375 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001376 *start = delalloc_start;
1377 *end = delalloc_end;
1378out_failed:
1379 return found;
1380}
1381
1382int extent_clear_unlock_delalloc(struct inode *inode,
1383 struct extent_io_tree *tree,
1384 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001385 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001386{
1387 int ret;
1388 struct page *pages[16];
1389 unsigned long index = start >> PAGE_CACHE_SHIFT;
1390 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1391 unsigned long nr_pages = end_index - index + 1;
1392 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001393 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001394
Chris Masona791e352009-10-08 11:27:10 -04001395 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001396 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001397 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001398 clear_bits |= EXTENT_DIRTY;
1399
Chris Masona791e352009-10-08 11:27:10 -04001400 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001401 clear_bits |= EXTENT_DELALLOC;
1402
Chris Mason2c64c532009-09-02 15:04:12 -04001403 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001404 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1405 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1406 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001407 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001408
Chris Masond3977122009-01-05 21:25:51 -05001409 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001410 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001411 min_t(unsigned long,
1412 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001413 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001414
Chris Masona791e352009-10-08 11:27:10 -04001415 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001416 SetPagePrivate2(pages[i]);
1417
Chris Masonc8b97812008-10-29 14:49:59 -04001418 if (pages[i] == locked_page) {
1419 page_cache_release(pages[i]);
1420 continue;
1421 }
Chris Masona791e352009-10-08 11:27:10 -04001422 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001423 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001424 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001425 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001426 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001427 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001428 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001429 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001430 page_cache_release(pages[i]);
1431 }
1432 nr_pages -= ret;
1433 index += ret;
1434 cond_resched();
1435 }
1436 return 0;
1437}
Chris Masonc8b97812008-10-29 14:49:59 -04001438
Chris Masond352ac62008-09-29 15:18:18 -04001439/*
1440 * count the number of bytes in the tree that have a given bit(s)
1441 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1442 * cached. The total number found is returned.
1443 */
Chris Masond1310b22008-01-24 16:13:08 -05001444u64 count_range_bits(struct extent_io_tree *tree,
1445 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001446 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001447{
1448 struct rb_node *node;
1449 struct extent_state *state;
1450 u64 cur_start = *start;
1451 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001452 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001453 int found = 0;
1454
1455 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001456 WARN_ON(1);
1457 return 0;
1458 }
1459
Chris Masoncad321a2008-12-17 14:51:42 -05001460 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001461 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1462 total_bytes = tree->dirty_bytes;
1463 goto out;
1464 }
1465 /*
1466 * this search will find all the extents that end after
1467 * our range starts.
1468 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001469 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001470 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001471 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001472
Chris Masond3977122009-01-05 21:25:51 -05001473 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001474 state = rb_entry(node, struct extent_state, rb_node);
1475 if (state->start > search_end)
1476 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001477 if (contig && found && state->start > last + 1)
1478 break;
1479 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001480 total_bytes += min(search_end, state->end) + 1 -
1481 max(cur_start, state->start);
1482 if (total_bytes >= max_bytes)
1483 break;
1484 if (!found) {
1485 *start = state->start;
1486 found = 1;
1487 }
Chris Masonec29ed52011-02-23 16:23:20 -05001488 last = state->end;
1489 } else if (contig && found) {
1490 break;
Chris Masond1310b22008-01-24 16:13:08 -05001491 }
1492 node = rb_next(node);
1493 if (!node)
1494 break;
1495 }
1496out:
Chris Masoncad321a2008-12-17 14:51:42 -05001497 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001498 return total_bytes;
1499}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001500
Chris Masond352ac62008-09-29 15:18:18 -04001501/*
1502 * set the private field for a given byte offset in the tree. If there isn't
1503 * an extent_state there already, this does nothing.
1504 */
Chris Masond1310b22008-01-24 16:13:08 -05001505int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1506{
1507 struct rb_node *node;
1508 struct extent_state *state;
1509 int ret = 0;
1510
Chris Masoncad321a2008-12-17 14:51:42 -05001511 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001512 /*
1513 * this search will find all the extents that end after
1514 * our range starts.
1515 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001516 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001517 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001518 ret = -ENOENT;
1519 goto out;
1520 }
1521 state = rb_entry(node, struct extent_state, rb_node);
1522 if (state->start != start) {
1523 ret = -ENOENT;
1524 goto out;
1525 }
1526 state->private = private;
1527out:
Chris Masoncad321a2008-12-17 14:51:42 -05001528 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001529 return ret;
1530}
1531
1532int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1533{
1534 struct rb_node *node;
1535 struct extent_state *state;
1536 int ret = 0;
1537
Chris Masoncad321a2008-12-17 14:51:42 -05001538 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001539 /*
1540 * this search will find all the extents that end after
1541 * our range starts.
1542 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001543 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001544 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001545 ret = -ENOENT;
1546 goto out;
1547 }
1548 state = rb_entry(node, struct extent_state, rb_node);
1549 if (state->start != start) {
1550 ret = -ENOENT;
1551 goto out;
1552 }
1553 *private = state->private;
1554out:
Chris Masoncad321a2008-12-17 14:51:42 -05001555 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001556 return ret;
1557}
1558
1559/*
1560 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001561 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001562 * has the bits set. Otherwise, 1 is returned if any bit in the
1563 * range is found set.
1564 */
1565int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001566 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001567{
1568 struct extent_state *state = NULL;
1569 struct rb_node *node;
1570 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001571
Chris Masoncad321a2008-12-17 14:51:42 -05001572 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001573 if (cached && cached->tree && cached->start == start)
1574 node = &cached->rb_node;
1575 else
1576 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001577 while (node && start <= end) {
1578 state = rb_entry(node, struct extent_state, rb_node);
1579
1580 if (filled && state->start > start) {
1581 bitset = 0;
1582 break;
1583 }
1584
1585 if (state->start > end)
1586 break;
1587
1588 if (state->state & bits) {
1589 bitset = 1;
1590 if (!filled)
1591 break;
1592 } else if (filled) {
1593 bitset = 0;
1594 break;
1595 }
Chris Mason46562ce2009-09-23 20:23:16 -04001596
1597 if (state->end == (u64)-1)
1598 break;
1599
Chris Masond1310b22008-01-24 16:13:08 -05001600 start = state->end + 1;
1601 if (start > end)
1602 break;
1603 node = rb_next(node);
1604 if (!node) {
1605 if (filled)
1606 bitset = 0;
1607 break;
1608 }
1609 }
Chris Masoncad321a2008-12-17 14:51:42 -05001610 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001611 return bitset;
1612}
Chris Masond1310b22008-01-24 16:13:08 -05001613
1614/*
1615 * helper function to set a given page up to date if all the
1616 * extents in the tree for that page are up to date
1617 */
1618static int check_page_uptodate(struct extent_io_tree *tree,
1619 struct page *page)
1620{
1621 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1622 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001623 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001624 SetPageUptodate(page);
1625 return 0;
1626}
1627
1628/*
1629 * helper function to unlock a page if all the extents in the tree
1630 * for that page are unlocked
1631 */
1632static int check_page_locked(struct extent_io_tree *tree,
1633 struct page *page)
1634{
1635 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1636 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001637 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001638 unlock_page(page);
1639 return 0;
1640}
1641
1642/*
1643 * helper function to end page writeback if all the extents
1644 * in the tree for that page are done with writeback
1645 */
1646static int check_page_writeback(struct extent_io_tree *tree,
1647 struct page *page)
1648{
Chris Mason1edbb732009-09-02 13:24:36 -04001649 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001650 return 0;
1651}
1652
1653/* lots and lots of room for performance fixes in the end_bio funcs */
1654
1655/*
1656 * after a writepage IO is done, we need to:
1657 * clear the uptodate bits on error
1658 * clear the writeback bits in the extent tree for this IO
1659 * end_page_writeback if the page has no more pending IO
1660 *
1661 * Scheduling is not allowed, so the extent state tree is expected
1662 * to have one and only one object corresponding to this IO.
1663 */
Chris Masond1310b22008-01-24 16:13:08 -05001664static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001665{
Chris Mason1259ab72008-05-12 13:39:03 -04001666 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001667 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001668 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001669 u64 start;
1670 u64 end;
1671 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001672 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001673
Chris Masond1310b22008-01-24 16:13:08 -05001674 do {
1675 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001676 tree = &BTRFS_I(page->mapping->host)->io_tree;
1677
Chris Masond1310b22008-01-24 16:13:08 -05001678 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1679 bvec->bv_offset;
1680 end = start + bvec->bv_len - 1;
1681
1682 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1683 whole_page = 1;
1684 else
1685 whole_page = 0;
1686
1687 if (--bvec >= bio->bi_io_vec)
1688 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001689 if (tree->ops && tree->ops->writepage_end_io_hook) {
1690 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001691 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001692 if (ret)
1693 uptodate = 0;
1694 }
1695
1696 if (!uptodate && tree->ops &&
1697 tree->ops->writepage_io_failed_hook) {
1698 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001699 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001700 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001701 uptodate = (err == 0);
1702 continue;
1703 }
1704 }
1705
Chris Masond1310b22008-01-24 16:13:08 -05001706 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001707 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001708 ClearPageUptodate(page);
1709 SetPageError(page);
1710 }
Chris Mason70dec802008-01-29 09:59:12 -05001711
Chris Masond1310b22008-01-24 16:13:08 -05001712 if (whole_page)
1713 end_page_writeback(page);
1714 else
1715 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001716 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001717
Chris Masond1310b22008-01-24 16:13:08 -05001718 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001719}
1720
1721/*
1722 * after a readpage IO is done, we need to:
1723 * clear the uptodate bits on error
1724 * set the uptodate bits if things worked
1725 * set the page up to date if all extents in the tree are uptodate
1726 * clear the lock bit in the extent tree
1727 * unlock the page if there are no other extents locked for it
1728 *
1729 * Scheduling is not allowed, so the extent state tree is expected
1730 * to have one and only one object corresponding to this IO.
1731 */
Chris Masond1310b22008-01-24 16:13:08 -05001732static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001733{
1734 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001735 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1736 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001737 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001738 u64 start;
1739 u64 end;
1740 int whole_page;
1741 int ret;
1742
Chris Masond20f7042008-12-08 16:58:54 -05001743 if (err)
1744 uptodate = 0;
1745
Chris Masond1310b22008-01-24 16:13:08 -05001746 do {
1747 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001748 struct extent_state *cached = NULL;
1749 struct extent_state *state;
1750
David Woodhouse902b22f2008-08-20 08:51:49 -04001751 tree = &BTRFS_I(page->mapping->host)->io_tree;
1752
Chris Masond1310b22008-01-24 16:13:08 -05001753 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1754 bvec->bv_offset;
1755 end = start + bvec->bv_len - 1;
1756
1757 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1758 whole_page = 1;
1759 else
1760 whole_page = 0;
1761
Chris Mason4125bf72010-02-03 18:18:45 +00001762 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001763 prefetchw(&bvec->bv_page->flags);
1764
Arne Jansen507903b2011-04-06 10:02:20 +00001765 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001766 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001767 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001768 /*
1769 * take a reference on the state, unlock will drop
1770 * the ref
1771 */
1772 cache_state(state, &cached);
1773 }
1774 spin_unlock(&tree->lock);
1775
Chris Masond1310b22008-01-24 16:13:08 -05001776 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001777 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001778 state);
Chris Masond1310b22008-01-24 16:13:08 -05001779 if (ret)
1780 uptodate = 0;
1781 }
Chris Mason7e383262008-04-09 16:28:12 -04001782 if (!uptodate && tree->ops &&
1783 tree->ops->readpage_io_failed_hook) {
1784 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001785 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001786 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001787 uptodate =
1788 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001789 if (err)
1790 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001791 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001792 continue;
1793 }
1794 }
Chris Mason70dec802008-01-29 09:59:12 -05001795
Chris Mason771ed682008-11-06 22:02:51 -05001796 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001797 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001798 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001799 }
Arne Jansen507903b2011-04-06 10:02:20 +00001800 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001801
Chris Mason70dec802008-01-29 09:59:12 -05001802 if (whole_page) {
1803 if (uptodate) {
1804 SetPageUptodate(page);
1805 } else {
1806 ClearPageUptodate(page);
1807 SetPageError(page);
1808 }
Chris Masond1310b22008-01-24 16:13:08 -05001809 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001810 } else {
1811 if (uptodate) {
1812 check_page_uptodate(tree, page);
1813 } else {
1814 ClearPageUptodate(page);
1815 SetPageError(page);
1816 }
Chris Masond1310b22008-01-24 16:13:08 -05001817 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001818 }
Chris Mason4125bf72010-02-03 18:18:45 +00001819 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001820
1821 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001822}
1823
1824/*
1825 * IO done from prepare_write is pretty simple, we just unlock
1826 * the structs in the extent tree when done, and set the uptodate bits
1827 * as appropriate.
1828 */
Chris Masond1310b22008-01-24 16:13:08 -05001829static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001830{
1831 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1832 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001833 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001834 u64 start;
1835 u64 end;
1836
Chris Masond1310b22008-01-24 16:13:08 -05001837 do {
1838 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001839 struct extent_state *cached = NULL;
David Woodhouse902b22f2008-08-20 08:51:49 -04001840 tree = &BTRFS_I(page->mapping->host)->io_tree;
1841
Chris Masond1310b22008-01-24 16:13:08 -05001842 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1843 bvec->bv_offset;
1844 end = start + bvec->bv_len - 1;
1845
1846 if (--bvec >= bio->bi_io_vec)
1847 prefetchw(&bvec->bv_page->flags);
1848
1849 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001850 set_extent_uptodate(tree, start, end, &cached,
1851 GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001852 } else {
1853 ClearPageUptodate(page);
1854 SetPageError(page);
1855 }
1856
Arne Jansen507903b2011-04-06 10:02:20 +00001857 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001858
1859 } while (bvec >= bio->bi_io_vec);
1860
1861 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001862}
1863
Miao Xie88f794e2010-11-22 03:02:55 +00001864struct bio *
1865btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1866 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001867{
1868 struct bio *bio;
1869
1870 bio = bio_alloc(gfp_flags, nr_vecs);
1871
1872 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1873 while (!bio && (nr_vecs /= 2))
1874 bio = bio_alloc(gfp_flags, nr_vecs);
1875 }
1876
1877 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001878 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001879 bio->bi_bdev = bdev;
1880 bio->bi_sector = first_sector;
1881 }
1882 return bio;
1883}
1884
Chris Masonc8b97812008-10-29 14:49:59 -04001885static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1886 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001887{
Chris Masond1310b22008-01-24 16:13:08 -05001888 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001889 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1890 struct page *page = bvec->bv_page;
1891 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001892 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001893
1894 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001895
David Woodhouse902b22f2008-08-20 08:51:49 -04001896 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001897
1898 bio_get(bio);
1899
Chris Mason065631f2008-02-20 12:07:25 -05001900 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001901 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001902 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001903 else
1904 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001905 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1906 ret = -EOPNOTSUPP;
1907 bio_put(bio);
1908 return ret;
1909}
1910
1911static int submit_extent_page(int rw, struct extent_io_tree *tree,
1912 struct page *page, sector_t sector,
1913 size_t size, unsigned long offset,
1914 struct block_device *bdev,
1915 struct bio **bio_ret,
1916 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001917 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001918 int mirror_num,
1919 unsigned long prev_bio_flags,
1920 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001921{
1922 int ret = 0;
1923 struct bio *bio;
1924 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001925 int contig = 0;
1926 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1927 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001928 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001929
1930 if (bio_ret && *bio_ret) {
1931 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001932 if (old_compressed)
1933 contig = bio->bi_sector == sector;
1934 else
1935 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1936 sector;
1937
1938 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001939 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001940 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1941 bio_flags)) ||
1942 bio_add_page(bio, page, page_size, offset) < page_size) {
1943 ret = submit_one_bio(rw, bio, mirror_num,
1944 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001945 bio = NULL;
1946 } else {
1947 return 0;
1948 }
1949 }
Chris Masonc8b97812008-10-29 14:49:59 -04001950 if (this_compressed)
1951 nr = BIO_MAX_PAGES;
1952 else
1953 nr = bio_get_nr_vecs(bdev);
1954
Miao Xie88f794e2010-11-22 03:02:55 +00001955 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001956 if (!bio)
1957 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001958
Chris Masonc8b97812008-10-29 14:49:59 -04001959 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001960 bio->bi_end_io = end_io_func;
1961 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001962
Chris Masond3977122009-01-05 21:25:51 -05001963 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001964 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001965 else
Chris Masonc8b97812008-10-29 14:49:59 -04001966 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001967
1968 return ret;
1969}
1970
1971void set_page_extent_mapped(struct page *page)
1972{
1973 if (!PagePrivate(page)) {
1974 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001975 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04001976 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001977 }
1978}
1979
Christoph Hellwigb2950862008-12-02 09:54:17 -05001980static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001981{
Chris Masoneb14ab82011-02-10 12:35:00 -05001982 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05001983 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1984}
1985
1986/*
1987 * basic readpage implementation. Locked extent state structs are inserted
1988 * into the tree that are removed when the IO is done (by the end_io
1989 * handlers)
1990 */
1991static int __extent_read_full_page(struct extent_io_tree *tree,
1992 struct page *page,
1993 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001994 struct bio **bio, int mirror_num,
1995 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001996{
1997 struct inode *inode = page->mapping->host;
1998 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1999 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2000 u64 end;
2001 u64 cur = start;
2002 u64 extent_offset;
2003 u64 last_byte = i_size_read(inode);
2004 u64 block_start;
2005 u64 cur_end;
2006 sector_t sector;
2007 struct extent_map *em;
2008 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002009 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002010 int ret;
2011 int nr = 0;
2012 size_t page_offset = 0;
2013 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002014 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002015 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002016 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002017
2018 set_page_extent_mapped(page);
2019
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002020 if (!PageUptodate(page)) {
2021 if (cleancache_get_page(page) == 0) {
2022 BUG_ON(blocksize != PAGE_SIZE);
2023 goto out;
2024 }
2025 }
2026
Chris Masond1310b22008-01-24 16:13:08 -05002027 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002028 while (1) {
2029 lock_extent(tree, start, end, GFP_NOFS);
2030 ordered = btrfs_lookup_ordered_extent(inode, start);
2031 if (!ordered)
2032 break;
2033 unlock_extent(tree, start, end, GFP_NOFS);
2034 btrfs_start_ordered_extent(inode, ordered, 1);
2035 btrfs_put_ordered_extent(ordered);
2036 }
Chris Masond1310b22008-01-24 16:13:08 -05002037
Chris Masonc8b97812008-10-29 14:49:59 -04002038 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2039 char *userpage;
2040 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2041
2042 if (zero_offset) {
2043 iosize = PAGE_CACHE_SIZE - zero_offset;
2044 userpage = kmap_atomic(page, KM_USER0);
2045 memset(userpage + zero_offset, 0, iosize);
2046 flush_dcache_page(page);
2047 kunmap_atomic(userpage, KM_USER0);
2048 }
2049 }
Chris Masond1310b22008-01-24 16:13:08 -05002050 while (cur <= end) {
2051 if (cur >= last_byte) {
2052 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002053 struct extent_state *cached = NULL;
2054
Chris Masond1310b22008-01-24 16:13:08 -05002055 iosize = PAGE_CACHE_SIZE - page_offset;
2056 userpage = kmap_atomic(page, KM_USER0);
2057 memset(userpage + page_offset, 0, iosize);
2058 flush_dcache_page(page);
2059 kunmap_atomic(userpage, KM_USER0);
2060 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002061 &cached, GFP_NOFS);
2062 unlock_extent_cached(tree, cur, cur + iosize - 1,
2063 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002064 break;
2065 }
2066 em = get_extent(inode, page, page_offset, cur,
2067 end - cur + 1, 0);
2068 if (IS_ERR(em) || !em) {
2069 SetPageError(page);
2070 unlock_extent(tree, cur, end, GFP_NOFS);
2071 break;
2072 }
Chris Masond1310b22008-01-24 16:13:08 -05002073 extent_offset = cur - em->start;
2074 BUG_ON(extent_map_end(em) <= cur);
2075 BUG_ON(end < cur);
2076
Li Zefan261507a02010-12-17 14:21:50 +08002077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002078 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002079 extent_set_compress_type(&this_bio_flag,
2080 em->compress_type);
2081 }
Chris Masonc8b97812008-10-29 14:49:59 -04002082
Chris Masond1310b22008-01-24 16:13:08 -05002083 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2084 cur_end = min(extent_map_end(em) - 1, end);
2085 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002086 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2087 disk_io_size = em->block_len;
2088 sector = em->block_start >> 9;
2089 } else {
2090 sector = (em->block_start + extent_offset) >> 9;
2091 disk_io_size = iosize;
2092 }
Chris Masond1310b22008-01-24 16:13:08 -05002093 bdev = em->bdev;
2094 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002095 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2096 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002097 free_extent_map(em);
2098 em = NULL;
2099
2100 /* we've found a hole, just zero and go on */
2101 if (block_start == EXTENT_MAP_HOLE) {
2102 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002103 struct extent_state *cached = NULL;
2104
Chris Masond1310b22008-01-24 16:13:08 -05002105 userpage = kmap_atomic(page, KM_USER0);
2106 memset(userpage + page_offset, 0, iosize);
2107 flush_dcache_page(page);
2108 kunmap_atomic(userpage, KM_USER0);
2109
2110 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002111 &cached, GFP_NOFS);
2112 unlock_extent_cached(tree, cur, cur + iosize - 1,
2113 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002114 cur = cur + iosize;
2115 page_offset += iosize;
2116 continue;
2117 }
2118 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002119 if (test_range_bit(tree, cur, cur_end,
2120 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002121 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002122 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2123 cur = cur + iosize;
2124 page_offset += iosize;
2125 continue;
2126 }
Chris Mason70dec802008-01-29 09:59:12 -05002127 /* we have an inline extent but it didn't get marked up
2128 * to date. Error out
2129 */
2130 if (block_start == EXTENT_MAP_INLINE) {
2131 SetPageError(page);
2132 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2133 cur = cur + iosize;
2134 page_offset += iosize;
2135 continue;
2136 }
Chris Masond1310b22008-01-24 16:13:08 -05002137
2138 ret = 0;
2139 if (tree->ops && tree->ops->readpage_io_hook) {
2140 ret = tree->ops->readpage_io_hook(page, cur,
2141 cur + iosize - 1);
2142 }
2143 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002144 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2145 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002146 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002147 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002148 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002149 end_bio_extent_readpage, mirror_num,
2150 *bio_flags,
2151 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002152 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002153 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002154 }
2155 if (ret)
2156 SetPageError(page);
2157 cur = cur + iosize;
2158 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002159 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002160out:
Chris Masond1310b22008-01-24 16:13:08 -05002161 if (!nr) {
2162 if (!PageError(page))
2163 SetPageUptodate(page);
2164 unlock_page(page);
2165 }
2166 return 0;
2167}
2168
2169int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2170 get_extent_t *get_extent)
2171{
2172 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002173 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002174 int ret;
2175
Chris Masonc8b97812008-10-29 14:49:59 -04002176 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2177 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002178 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002179 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002180 return ret;
2181}
Chris Masond1310b22008-01-24 16:13:08 -05002182
Chris Mason11c83492009-04-20 15:50:09 -04002183static noinline void update_nr_written(struct page *page,
2184 struct writeback_control *wbc,
2185 unsigned long nr_written)
2186{
2187 wbc->nr_to_write -= nr_written;
2188 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2189 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2190 page->mapping->writeback_index = page->index + nr_written;
2191}
2192
Chris Masond1310b22008-01-24 16:13:08 -05002193/*
2194 * the writepage semantics are similar to regular writepage. extent
2195 * records are inserted to lock ranges in the tree, and as dirty areas
2196 * are found, they are marked writeback. Then the lock bits are removed
2197 * and the end_io handler clears the writeback ranges
2198 */
2199static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2200 void *data)
2201{
2202 struct inode *inode = page->mapping->host;
2203 struct extent_page_data *epd = data;
2204 struct extent_io_tree *tree = epd->tree;
2205 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2206 u64 delalloc_start;
2207 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2208 u64 end;
2209 u64 cur = start;
2210 u64 extent_offset;
2211 u64 last_byte = i_size_read(inode);
2212 u64 block_start;
2213 u64 iosize;
2214 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002215 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002216 struct extent_map *em;
2217 struct block_device *bdev;
2218 int ret;
2219 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002220 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002221 size_t blocksize;
2222 loff_t i_size = i_size_read(inode);
2223 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2224 u64 nr_delalloc;
2225 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002226 int page_started;
2227 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002228 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002229 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002230
Chris Masonffbd5172009-04-20 15:50:09 -04002231 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002232 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002233 else
2234 write_flags = WRITE;
2235
liubo1abe9b82011-03-24 11:18:59 +00002236 trace___extent_writepage(page, inode, wbc);
2237
Chris Masond1310b22008-01-24 16:13:08 -05002238 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002239 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002240 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002241 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002242 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002243 unlock_page(page);
2244 return 0;
2245 }
2246
2247 if (page->index == end_index) {
2248 char *userpage;
2249
Chris Masond1310b22008-01-24 16:13:08 -05002250 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002251 memset(userpage + pg_offset, 0,
2252 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002253 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002254 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002255 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002256 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002257
2258 set_page_extent_mapped(page);
2259
2260 delalloc_start = start;
2261 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002262 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002263 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002264 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002265 /*
2266 * make sure the wbc mapping index is at least updated
2267 * to this page.
2268 */
2269 update_nr_written(page, wbc, 0);
2270
Chris Masond3977122009-01-05 21:25:51 -05002271 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002272 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002273 page,
2274 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002275 &delalloc_end,
2276 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002277 if (nr_delalloc == 0) {
2278 delalloc_start = delalloc_end + 1;
2279 continue;
2280 }
2281 tree->ops->fill_delalloc(inode, page, delalloc_start,
2282 delalloc_end, &page_started,
2283 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002284 /*
2285 * delalloc_end is already one less than the total
2286 * length, so we don't subtract one from
2287 * PAGE_CACHE_SIZE
2288 */
2289 delalloc_to_write += (delalloc_end - delalloc_start +
2290 PAGE_CACHE_SIZE) >>
2291 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002292 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002293 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002294 if (wbc->nr_to_write < delalloc_to_write) {
2295 int thresh = 8192;
2296
2297 if (delalloc_to_write < thresh * 2)
2298 thresh = delalloc_to_write;
2299 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2300 thresh);
2301 }
Chris Masonc8b97812008-10-29 14:49:59 -04002302
Chris Mason771ed682008-11-06 22:02:51 -05002303 /* did the fill delalloc function already unlock and start
2304 * the IO?
2305 */
2306 if (page_started) {
2307 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002308 /*
2309 * we've unlocked the page, so we can't update
2310 * the mapping's writeback index, just update
2311 * nr_to_write.
2312 */
2313 wbc->nr_to_write -= nr_written;
2314 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002315 }
Chris Masonc8b97812008-10-29 14:49:59 -04002316 }
Chris Mason247e7432008-07-17 12:53:51 -04002317 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002318 ret = tree->ops->writepage_start_hook(page, start,
2319 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002320 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002321 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002322 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002323 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002324 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002325 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002326 }
2327 }
2328
Chris Mason11c83492009-04-20 15:50:09 -04002329 /*
2330 * we don't want to touch the inode after unlocking the page,
2331 * so we update the mapping writeback index now
2332 */
2333 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002334
Chris Masond1310b22008-01-24 16:13:08 -05002335 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002336 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002337 if (tree->ops && tree->ops->writepage_end_io_hook)
2338 tree->ops->writepage_end_io_hook(page, start,
2339 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002340 goto done;
2341 }
2342
Chris Masond1310b22008-01-24 16:13:08 -05002343 blocksize = inode->i_sb->s_blocksize;
2344
2345 while (cur <= end) {
2346 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002347 if (tree->ops && tree->ops->writepage_end_io_hook)
2348 tree->ops->writepage_end_io_hook(page, cur,
2349 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002350 break;
2351 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002352 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002353 end - cur + 1, 1);
2354 if (IS_ERR(em) || !em) {
2355 SetPageError(page);
2356 break;
2357 }
2358
2359 extent_offset = cur - em->start;
2360 BUG_ON(extent_map_end(em) <= cur);
2361 BUG_ON(end < cur);
2362 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2363 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2364 sector = (em->block_start + extent_offset) >> 9;
2365 bdev = em->bdev;
2366 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002367 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002368 free_extent_map(em);
2369 em = NULL;
2370
Chris Masonc8b97812008-10-29 14:49:59 -04002371 /*
2372 * compressed and inline extents are written through other
2373 * paths in the FS
2374 */
2375 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002376 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002377 /*
2378 * end_io notification does not happen here for
2379 * compressed extents
2380 */
2381 if (!compressed && tree->ops &&
2382 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002383 tree->ops->writepage_end_io_hook(page, cur,
2384 cur + iosize - 1,
2385 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002386 else if (compressed) {
2387 /* we don't want to end_page_writeback on
2388 * a compressed extent. this happens
2389 * elsewhere
2390 */
2391 nr++;
2392 }
2393
2394 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002395 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002396 continue;
2397 }
Chris Masond1310b22008-01-24 16:13:08 -05002398 /* leave this out until we have a page_mkwrite call */
2399 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002400 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002401 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002402 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002403 continue;
2404 }
Chris Masonc8b97812008-10-29 14:49:59 -04002405
Chris Masond1310b22008-01-24 16:13:08 -05002406 if (tree->ops && tree->ops->writepage_io_hook) {
2407 ret = tree->ops->writepage_io_hook(page, cur,
2408 cur + iosize - 1);
2409 } else {
2410 ret = 0;
2411 }
Chris Mason1259ab72008-05-12 13:39:03 -04002412 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002413 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002414 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002415 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002416
Chris Masond1310b22008-01-24 16:13:08 -05002417 set_range_writeback(tree, cur, cur + iosize - 1);
2418 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002419 printk(KERN_ERR "btrfs warning page %lu not "
2420 "writeback, cur %llu end %llu\n",
2421 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002422 (unsigned long long)end);
2423 }
2424
Chris Masonffbd5172009-04-20 15:50:09 -04002425 ret = submit_extent_page(write_flags, tree, page,
2426 sector, iosize, pg_offset,
2427 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002428 end_bio_extent_writepage,
2429 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002430 if (ret)
2431 SetPageError(page);
2432 }
2433 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002434 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002435 nr++;
2436 }
2437done:
2438 if (nr == 0) {
2439 /* make sure the mapping tag for page dirty gets cleared */
2440 set_page_writeback(page);
2441 end_page_writeback(page);
2442 }
Chris Masond1310b22008-01-24 16:13:08 -05002443 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002444
Chris Mason11c83492009-04-20 15:50:09 -04002445done_unlocked:
2446
Chris Mason2c64c532009-09-02 15:04:12 -04002447 /* drop our reference on any cached states */
2448 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002449 return 0;
2450}
2451
Chris Masond1310b22008-01-24 16:13:08 -05002452/**
Chris Mason4bef0842008-09-08 11:18:08 -04002453 * 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 -05002454 * @mapping: address space structure to write
2455 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2456 * @writepage: function called for each page
2457 * @data: data passed to writepage function
2458 *
2459 * If a page is already under I/O, write_cache_pages() skips it, even
2460 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2461 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2462 * and msync() need to guarantee that all the data which was dirty at the time
2463 * the call was made get new I/O started against them. If wbc->sync_mode is
2464 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2465 * existing IO to complete.
2466 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002467static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002468 struct address_space *mapping,
2469 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002470 writepage_t writepage, void *data,
2471 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002472{
Chris Masond1310b22008-01-24 16:13:08 -05002473 int ret = 0;
2474 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002475 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002476 struct pagevec pvec;
2477 int nr_pages;
2478 pgoff_t index;
2479 pgoff_t end; /* Inclusive */
2480 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002481
Chris Masond1310b22008-01-24 16:13:08 -05002482 pagevec_init(&pvec, 0);
2483 if (wbc->range_cyclic) {
2484 index = mapping->writeback_index; /* Start from prev offset */
2485 end = -1;
2486 } else {
2487 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2488 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002489 scanned = 1;
2490 }
2491retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002492 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002493 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002494 PAGECACHE_TAG_DIRTY, min(end - index,
2495 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002496 unsigned i;
2497
2498 scanned = 1;
2499 for (i = 0; i < nr_pages; i++) {
2500 struct page *page = pvec.pages[i];
2501
2502 /*
2503 * At this point we hold neither mapping->tree_lock nor
2504 * lock on the page itself: the page may be truncated or
2505 * invalidated (changing page->mapping to NULL), or even
2506 * swizzled back from swapper_space to tmpfs file
2507 * mapping
2508 */
Chris Mason4bef0842008-09-08 11:18:08 -04002509 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2510 tree->ops->write_cache_pages_lock_hook(page);
2511 else
2512 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002513
2514 if (unlikely(page->mapping != mapping)) {
2515 unlock_page(page);
2516 continue;
2517 }
2518
2519 if (!wbc->range_cyclic && page->index > end) {
2520 done = 1;
2521 unlock_page(page);
2522 continue;
2523 }
2524
Chris Masond2c3f4f2008-11-19 12:44:22 -05002525 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002526 if (PageWriteback(page))
2527 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002528 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002529 }
Chris Masond1310b22008-01-24 16:13:08 -05002530
2531 if (PageWriteback(page) ||
2532 !clear_page_dirty_for_io(page)) {
2533 unlock_page(page);
2534 continue;
2535 }
2536
2537 ret = (*writepage)(page, wbc, data);
2538
2539 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2540 unlock_page(page);
2541 ret = 0;
2542 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002543 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002544 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002545
2546 /*
2547 * the filesystem may choose to bump up nr_to_write.
2548 * We have to make sure to honor the new nr_to_write
2549 * at any time
2550 */
2551 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002552 }
2553 pagevec_release(&pvec);
2554 cond_resched();
2555 }
2556 if (!scanned && !done) {
2557 /*
2558 * We hit the last page and there is more work to be done: wrap
2559 * back to the start of the file
2560 */
2561 scanned = 1;
2562 index = 0;
2563 goto retry;
2564 }
Chris Masond1310b22008-01-24 16:13:08 -05002565 return ret;
2566}
Chris Masond1310b22008-01-24 16:13:08 -05002567
Chris Masonffbd5172009-04-20 15:50:09 -04002568static void flush_epd_write_bio(struct extent_page_data *epd)
2569{
2570 if (epd->bio) {
2571 if (epd->sync_io)
2572 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2573 else
2574 submit_one_bio(WRITE, epd->bio, 0, 0);
2575 epd->bio = NULL;
2576 }
2577}
2578
Chris Masond2c3f4f2008-11-19 12:44:22 -05002579static noinline void flush_write_bio(void *data)
2580{
2581 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002582 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002583}
2584
Chris Masond1310b22008-01-24 16:13:08 -05002585int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2586 get_extent_t *get_extent,
2587 struct writeback_control *wbc)
2588{
2589 int ret;
2590 struct address_space *mapping = page->mapping;
2591 struct extent_page_data epd = {
2592 .bio = NULL,
2593 .tree = tree,
2594 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002595 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002596 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002597 };
2598 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002599 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002600 .older_than_this = NULL,
2601 .nr_to_write = 64,
2602 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2603 .range_end = (loff_t)-1,
2604 };
2605
Chris Masond1310b22008-01-24 16:13:08 -05002606 ret = __extent_writepage(page, wbc, &epd);
2607
Chris Mason4bef0842008-09-08 11:18:08 -04002608 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002609 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002610 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002611 return ret;
2612}
Chris Masond1310b22008-01-24 16:13:08 -05002613
Chris Mason771ed682008-11-06 22:02:51 -05002614int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2615 u64 start, u64 end, get_extent_t *get_extent,
2616 int mode)
2617{
2618 int ret = 0;
2619 struct address_space *mapping = inode->i_mapping;
2620 struct page *page;
2621 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2622 PAGE_CACHE_SHIFT;
2623
2624 struct extent_page_data epd = {
2625 .bio = NULL,
2626 .tree = tree,
2627 .get_extent = get_extent,
2628 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002629 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002630 };
2631 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002632 .sync_mode = mode,
2633 .older_than_this = NULL,
2634 .nr_to_write = nr_pages * 2,
2635 .range_start = start,
2636 .range_end = end + 1,
2637 };
2638
Chris Masond3977122009-01-05 21:25:51 -05002639 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002640 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2641 if (clear_page_dirty_for_io(page))
2642 ret = __extent_writepage(page, &wbc_writepages, &epd);
2643 else {
2644 if (tree->ops && tree->ops->writepage_end_io_hook)
2645 tree->ops->writepage_end_io_hook(page, start,
2646 start + PAGE_CACHE_SIZE - 1,
2647 NULL, 1);
2648 unlock_page(page);
2649 }
2650 page_cache_release(page);
2651 start += PAGE_CACHE_SIZE;
2652 }
2653
Chris Masonffbd5172009-04-20 15:50:09 -04002654 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002655 return ret;
2656}
Chris Masond1310b22008-01-24 16:13:08 -05002657
2658int extent_writepages(struct extent_io_tree *tree,
2659 struct address_space *mapping,
2660 get_extent_t *get_extent,
2661 struct writeback_control *wbc)
2662{
2663 int ret = 0;
2664 struct extent_page_data epd = {
2665 .bio = NULL,
2666 .tree = tree,
2667 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002668 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002669 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002670 };
2671
Chris Mason4bef0842008-09-08 11:18:08 -04002672 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002673 __extent_writepage, &epd,
2674 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002675 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002676 return ret;
2677}
Chris Masond1310b22008-01-24 16:13:08 -05002678
2679int extent_readpages(struct extent_io_tree *tree,
2680 struct address_space *mapping,
2681 struct list_head *pages, unsigned nr_pages,
2682 get_extent_t get_extent)
2683{
2684 struct bio *bio = NULL;
2685 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002686 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002687
Chris Masond1310b22008-01-24 16:13:08 -05002688 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2689 struct page *page = list_entry(pages->prev, struct page, lru);
2690
2691 prefetchw(&page->flags);
2692 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00002693 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002694 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002695 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002696 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002697 }
2698 page_cache_release(page);
2699 }
Chris Masond1310b22008-01-24 16:13:08 -05002700 BUG_ON(!list_empty(pages));
2701 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002702 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002703 return 0;
2704}
Chris Masond1310b22008-01-24 16:13:08 -05002705
2706/*
2707 * basic invalidatepage code, this waits on any locked or writeback
2708 * ranges corresponding to the page, and then deletes any extent state
2709 * records from the tree
2710 */
2711int extent_invalidatepage(struct extent_io_tree *tree,
2712 struct page *page, unsigned long offset)
2713{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002714 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002715 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2716 u64 end = start + PAGE_CACHE_SIZE - 1;
2717 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2718
Chris Masond3977122009-01-05 21:25:51 -05002719 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002720 if (start > end)
2721 return 0;
2722
Josef Bacik2ac55d42010-02-03 19:33:23 +00002723 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002724 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002725 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002726 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2727 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002728 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002729 return 0;
2730}
Chris Masond1310b22008-01-24 16:13:08 -05002731
2732/*
2733 * simple commit_write call, set_range_dirty is used to mark both
2734 * the pages and the extent records as dirty
2735 */
2736int extent_commit_write(struct extent_io_tree *tree,
2737 struct inode *inode, struct page *page,
2738 unsigned from, unsigned to)
2739{
2740 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2741
2742 set_page_extent_mapped(page);
2743 set_page_dirty(page);
2744
2745 if (pos > inode->i_size) {
2746 i_size_write(inode, pos);
2747 mark_inode_dirty(inode);
2748 }
2749 return 0;
2750}
Chris Masond1310b22008-01-24 16:13:08 -05002751
2752int extent_prepare_write(struct extent_io_tree *tree,
2753 struct inode *inode, struct page *page,
2754 unsigned from, unsigned to, get_extent_t *get_extent)
2755{
2756 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2757 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2758 u64 block_start;
2759 u64 orig_block_start;
2760 u64 block_end;
2761 u64 cur_end;
2762 struct extent_map *em;
2763 unsigned blocksize = 1 << inode->i_blkbits;
2764 size_t page_offset = 0;
2765 size_t block_off_start;
2766 size_t block_off_end;
2767 int err = 0;
2768 int iocount = 0;
2769 int ret = 0;
2770 int isnew;
2771
2772 set_page_extent_mapped(page);
2773
2774 block_start = (page_start + from) & ~((u64)blocksize - 1);
2775 block_end = (page_start + to - 1) | (blocksize - 1);
2776 orig_block_start = block_start;
2777
2778 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002779 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002780 em = get_extent(inode, page, page_offset, block_start,
2781 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002782 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002783 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002784
Chris Masond1310b22008-01-24 16:13:08 -05002785 cur_end = min(block_end, extent_map_end(em) - 1);
2786 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2787 block_off_end = block_off_start + blocksize;
2788 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2789
2790 if (!PageUptodate(page) && isnew &&
2791 (block_off_end > to || block_off_start < from)) {
2792 void *kaddr;
2793
2794 kaddr = kmap_atomic(page, KM_USER0);
2795 if (block_off_end > to)
2796 memset(kaddr + to, 0, block_off_end - to);
2797 if (block_off_start < from)
2798 memset(kaddr + block_off_start, 0,
2799 from - block_off_start);
2800 flush_dcache_page(page);
2801 kunmap_atomic(kaddr, KM_USER0);
2802 }
2803 if ((em->block_start != EXTENT_MAP_HOLE &&
2804 em->block_start != EXTENT_MAP_INLINE) &&
2805 !isnew && !PageUptodate(page) &&
2806 (block_off_end > to || block_off_start < from) &&
2807 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002808 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002809 u64 sector;
2810 u64 extent_offset = block_start - em->start;
2811 size_t iosize;
2812 sector = (em->block_start + extent_offset) >> 9;
2813 iosize = (cur_end - block_start + blocksize) &
2814 ~((u64)blocksize - 1);
2815 /*
2816 * we've already got the extent locked, but we
2817 * need to split the state such that our end_bio
2818 * handler can clear the lock.
2819 */
2820 set_extent_bit(tree, block_start,
2821 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002822 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002823 ret = submit_extent_page(READ, tree, page,
2824 sector, iosize, page_offset, em->bdev,
2825 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002826 end_bio_extent_preparewrite, 0,
2827 0, 0);
Andi Kleen411fc6b2010-10-29 15:14:31 -04002828 if (ret && !err)
2829 err = ret;
Chris Masond1310b22008-01-24 16:13:08 -05002830 iocount++;
2831 block_start = block_start + iosize;
2832 } else {
Arne Jansen507903b2011-04-06 10:02:20 +00002833 struct extent_state *cached = NULL;
2834
2835 set_extent_uptodate(tree, block_start, cur_end, &cached,
Chris Masond1310b22008-01-24 16:13:08 -05002836 GFP_NOFS);
Arne Jansen507903b2011-04-06 10:02:20 +00002837 unlock_extent_cached(tree, block_start, cur_end,
2838 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002839 block_start = cur_end + 1;
2840 }
2841 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2842 free_extent_map(em);
2843 }
2844 if (iocount) {
2845 wait_extent_bit(tree, orig_block_start,
2846 block_end, EXTENT_LOCKED);
2847 }
2848 check_page_uptodate(tree, page);
2849err:
2850 /* FIXME, zero out newly allocated blocks on error */
2851 return err;
2852}
Chris Masond1310b22008-01-24 16:13:08 -05002853
2854/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002855 * a helper for releasepage, this tests for areas of the page that
2856 * are locked or under IO and drops the related state bits if it is safe
2857 * to drop the page.
2858 */
2859int try_release_extent_state(struct extent_map_tree *map,
2860 struct extent_io_tree *tree, struct page *page,
2861 gfp_t mask)
2862{
2863 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2864 u64 end = start + PAGE_CACHE_SIZE - 1;
2865 int ret = 1;
2866
Chris Mason211f90e2008-07-18 11:56:15 -04002867 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002868 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002869 ret = 0;
2870 else {
2871 if ((mask & GFP_NOFS) == GFP_NOFS)
2872 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002873 /*
2874 * at this point we can safely clear everything except the
2875 * locked bit and the nodatasum bit
2876 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002877 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002878 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2879 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002880
2881 /* if clear_extent_bit failed for enomem reasons,
2882 * we can't allow the release to continue.
2883 */
2884 if (ret < 0)
2885 ret = 0;
2886 else
2887 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002888 }
2889 return ret;
2890}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002891
2892/*
Chris Masond1310b22008-01-24 16:13:08 -05002893 * a helper for releasepage. As long as there are no locked extents
2894 * in the range corresponding to the page, both state records and extent
2895 * map records are removed
2896 */
2897int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002898 struct extent_io_tree *tree, struct page *page,
2899 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002900{
2901 struct extent_map *em;
2902 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2903 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002904
Chris Mason70dec802008-01-29 09:59:12 -05002905 if ((mask & __GFP_WAIT) &&
2906 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002907 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002908 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002909 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002910 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002911 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002912 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002913 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002914 break;
2915 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002916 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2917 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002918 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002919 free_extent_map(em);
2920 break;
2921 }
2922 if (!test_range_bit(tree, em->start,
2923 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002924 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002925 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002926 remove_extent_mapping(map, em);
2927 /* once for the rb tree */
2928 free_extent_map(em);
2929 }
2930 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002931 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002932
2933 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002934 free_extent_map(em);
2935 }
Chris Masond1310b22008-01-24 16:13:08 -05002936 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002937 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002938}
Chris Masond1310b22008-01-24 16:13:08 -05002939
2940sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2941 get_extent_t *get_extent)
2942{
2943 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002944 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002945 u64 start = iblock << inode->i_blkbits;
2946 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002947 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002948 struct extent_map *em;
2949
Josef Bacik2ac55d42010-02-03 19:33:23 +00002950 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2951 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002952 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002953 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2954 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002955 if (!em || IS_ERR(em))
2956 return 0;
2957
Yan Zhengd899e052008-10-30 14:25:28 -04002958 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002959 goto out;
2960
2961 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002962out:
2963 free_extent_map(em);
2964 return sector;
2965}
2966
Chris Masonec29ed52011-02-23 16:23:20 -05002967/*
2968 * helper function for fiemap, which doesn't want to see any holes.
2969 * This maps until we find something past 'last'
2970 */
2971static struct extent_map *get_extent_skip_holes(struct inode *inode,
2972 u64 offset,
2973 u64 last,
2974 get_extent_t *get_extent)
2975{
2976 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2977 struct extent_map *em;
2978 u64 len;
2979
2980 if (offset >= last)
2981 return NULL;
2982
2983 while(1) {
2984 len = last - offset;
2985 if (len == 0)
2986 break;
2987 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2988 em = get_extent(inode, NULL, 0, offset, len, 0);
2989 if (!em || IS_ERR(em))
2990 return em;
2991
2992 /* if this isn't a hole return it */
2993 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2994 em->block_start != EXTENT_MAP_HOLE) {
2995 return em;
2996 }
2997
2998 /* this is a hole, advance to the next extent */
2999 offset = extent_map_end(em);
3000 free_extent_map(em);
3001 if (offset >= last)
3002 break;
3003 }
3004 return NULL;
3005}
3006
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003007int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3008 __u64 start, __u64 len, get_extent_t *get_extent)
3009{
Josef Bacik975f84f2010-11-23 19:36:57 +00003010 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003011 u64 off = start;
3012 u64 max = start + len;
3013 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003014 u32 found_type;
3015 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003016 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003017 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003018 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003019 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003020 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003021 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003022 struct btrfs_path *path;
3023 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003024 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003025 u64 em_start = 0;
3026 u64 em_len = 0;
3027 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003028 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003029
3030 if (len == 0)
3031 return -EINVAL;
3032
Josef Bacik975f84f2010-11-23 19:36:57 +00003033 path = btrfs_alloc_path();
3034 if (!path)
3035 return -ENOMEM;
3036 path->leave_spinning = 1;
3037
Chris Masonec29ed52011-02-23 16:23:20 -05003038 /*
3039 * lookup the last file extent. We're not using i_size here
3040 * because there might be preallocation past i_size
3041 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003042 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3043 path, inode->i_ino, -1, 0);
3044 if (ret < 0) {
3045 btrfs_free_path(path);
3046 return ret;
3047 }
3048 WARN_ON(!ret);
3049 path->slots[0]--;
3050 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3051 struct btrfs_file_extent_item);
3052 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3053 found_type = btrfs_key_type(&found_key);
3054
Chris Masonec29ed52011-02-23 16:23:20 -05003055 /* No extents, but there might be delalloc bits */
Josef Bacik975f84f2010-11-23 19:36:57 +00003056 if (found_key.objectid != inode->i_ino ||
3057 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003058 /* have to trust i_size as the end */
3059 last = (u64)-1;
3060 last_for_get_extent = isize;
3061 } else {
3062 /*
3063 * remember the start of the last extent. There are a
3064 * bunch of different factors that go into the length of the
3065 * extent, so its much less complex to remember where it started
3066 */
3067 last = found_key.offset;
3068 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003069 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003070 btrfs_free_path(path);
3071
Chris Masonec29ed52011-02-23 16:23:20 -05003072 /*
3073 * we might have some extents allocated but more delalloc past those
3074 * extents. so, we trust isize unless the start of the last extent is
3075 * beyond isize
3076 */
3077 if (last < isize) {
3078 last = (u64)-1;
3079 last_for_get_extent = isize;
3080 }
3081
Josef Bacik2ac55d42010-02-03 19:33:23 +00003082 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3083 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003084
3085 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3086 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003087 if (!em)
3088 goto out;
3089 if (IS_ERR(em)) {
3090 ret = PTR_ERR(em);
3091 goto out;
3092 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003093
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003094 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003095 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003096
Chris Masonea8efc72011-03-08 11:54:40 -05003097 /* break if the extent we found is outside the range */
3098 if (em->start >= max || extent_map_end(em) < off)
3099 break;
3100
3101 /*
3102 * get_extent may return an extent that starts before our
3103 * requested range. We have to make sure the ranges
3104 * we return to fiemap always move forward and don't
3105 * overlap, so adjust the offsets here
3106 */
3107 em_start = max(em->start, off);
3108
3109 /*
3110 * record the offset from the start of the extent
3111 * for adjusting the disk offset below
3112 */
3113 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003114 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003115 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003116 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003117 disko = 0;
3118 flags = 0;
3119
Chris Masonea8efc72011-03-08 11:54:40 -05003120 /*
3121 * bump off for our next call to get_extent
3122 */
3123 off = extent_map_end(em);
3124 if (off >= max)
3125 end = 1;
3126
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003127 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003128 end = 1;
3129 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003130 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003131 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3132 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003133 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003134 flags |= (FIEMAP_EXTENT_DELALLOC |
3135 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003136 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003137 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003138 }
3139 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3140 flags |= FIEMAP_EXTENT_ENCODED;
3141
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003142 free_extent_map(em);
3143 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003144 if ((em_start >= last) || em_len == (u64)-1 ||
3145 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003146 flags |= FIEMAP_EXTENT_LAST;
3147 end = 1;
3148 }
3149
Chris Masonec29ed52011-02-23 16:23:20 -05003150 /* now scan forward to see if this is really the last extent. */
3151 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3152 get_extent);
3153 if (IS_ERR(em)) {
3154 ret = PTR_ERR(em);
3155 goto out;
3156 }
3157 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003158 flags |= FIEMAP_EXTENT_LAST;
3159 end = 1;
3160 }
Chris Masonec29ed52011-02-23 16:23:20 -05003161 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3162 em_len, flags);
3163 if (ret)
3164 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003165 }
3166out_free:
3167 free_extent_map(em);
3168out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003169 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3170 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003171 return ret;
3172}
3173
Chris Masond1310b22008-01-24 16:13:08 -05003174static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3175 unsigned long i)
3176{
3177 struct page *p;
3178 struct address_space *mapping;
3179
3180 if (i == 0)
3181 return eb->first_page;
3182 i += eb->start >> PAGE_CACHE_SHIFT;
3183 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003184 if (!mapping)
3185 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003186
3187 /*
3188 * extent_buffer_page is only called after pinning the page
3189 * by increasing the reference count. So we know the page must
3190 * be in the radix tree.
3191 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003192 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003193 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003194 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003195
Chris Masond1310b22008-01-24 16:13:08 -05003196 return p;
3197}
3198
Chris Mason6af118c2008-07-22 11:18:07 -04003199static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003200{
Chris Mason6af118c2008-07-22 11:18:07 -04003201 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3202 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003203}
3204
Chris Masond1310b22008-01-24 16:13:08 -05003205static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3206 u64 start,
3207 unsigned long len,
3208 gfp_t mask)
3209{
3210 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003211#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003212 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003213#endif
Chris Masond1310b22008-01-24 16:13:08 -05003214
Chris Masond1310b22008-01-24 16:13:08 -05003215 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003216 if (eb == NULL)
3217 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003218 eb->start = start;
3219 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003220 spin_lock_init(&eb->lock);
3221 init_waitqueue_head(&eb->lock_wq);
3222
Chris Mason39351272009-02-04 09:24:05 -05003223#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003224 spin_lock_irqsave(&leak_lock, flags);
3225 list_add(&eb->leak_list, &buffers);
3226 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003227#endif
Chris Masond1310b22008-01-24 16:13:08 -05003228 atomic_set(&eb->refs, 1);
3229
3230 return eb;
3231}
3232
3233static void __free_extent_buffer(struct extent_buffer *eb)
3234{
Chris Mason39351272009-02-04 09:24:05 -05003235#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003236 unsigned long flags;
3237 spin_lock_irqsave(&leak_lock, flags);
3238 list_del(&eb->leak_list);
3239 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003240#endif
Chris Masond1310b22008-01-24 16:13:08 -05003241 kmem_cache_free(extent_buffer_cache, eb);
3242}
3243
Miao Xie897ca6e2010-10-26 20:57:29 -04003244/*
3245 * Helper for releasing extent buffer page.
3246 */
3247static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3248 unsigned long start_idx)
3249{
3250 unsigned long index;
3251 struct page *page;
3252
3253 if (!eb->first_page)
3254 return;
3255
3256 index = num_extent_pages(eb->start, eb->len);
3257 if (start_idx >= index)
3258 return;
3259
3260 do {
3261 index--;
3262 page = extent_buffer_page(eb, index);
3263 if (page)
3264 page_cache_release(page);
3265 } while (index != start_idx);
3266}
3267
3268/*
3269 * Helper for releasing the extent buffer.
3270 */
3271static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3272{
3273 btrfs_release_extent_buffer_page(eb, 0);
3274 __free_extent_buffer(eb);
3275}
3276
Chris Masond1310b22008-01-24 16:13:08 -05003277struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3278 u64 start, unsigned long len,
3279 struct page *page0,
3280 gfp_t mask)
3281{
3282 unsigned long num_pages = num_extent_pages(start, len);
3283 unsigned long i;
3284 unsigned long index = start >> PAGE_CACHE_SHIFT;
3285 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003286 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003287 struct page *p;
3288 struct address_space *mapping = tree->mapping;
3289 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003290 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003291
Miao Xie19fe0a82010-10-26 20:57:29 -04003292 rcu_read_lock();
3293 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3294 if (eb && atomic_inc_not_zero(&eb->refs)) {
3295 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003296 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003297 return eb;
3298 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003299 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04003300
Chris Masond1310b22008-01-24 16:13:08 -05003301 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003302 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003303 return NULL;
3304
Chris Masond1310b22008-01-24 16:13:08 -05003305 if (page0) {
3306 eb->first_page = page0;
3307 i = 1;
3308 index++;
3309 page_cache_get(page0);
3310 mark_page_accessed(page0);
3311 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003312 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003313 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003314 } else {
3315 i = 0;
3316 }
3317 for (; i < num_pages; i++, index++) {
3318 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3319 if (!p) {
3320 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003321 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003322 }
3323 set_page_extent_mapped(p);
3324 mark_page_accessed(p);
3325 if (i == 0) {
3326 eb->first_page = p;
3327 set_page_extent_head(p, len);
3328 } else {
3329 set_page_private(p, EXTENT_PAGE_PRIVATE);
3330 }
3331 if (!PageUptodate(p))
3332 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003333
3334 /*
3335 * see below about how we avoid a nasty race with release page
3336 * and why we unlock later
3337 */
3338 if (i != 0)
3339 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003340 }
3341 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003342 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003343
Miao Xie19fe0a82010-10-26 20:57:29 -04003344 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3345 if (ret)
3346 goto free_eb;
3347
Chris Mason6af118c2008-07-22 11:18:07 -04003348 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003349 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3350 if (ret == -EEXIST) {
3351 exists = radix_tree_lookup(&tree->buffer,
3352 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003353 /* add one reference for the caller */
3354 atomic_inc(&exists->refs);
3355 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003356 radix_tree_preload_end();
Chris Mason6af118c2008-07-22 11:18:07 -04003357 goto free_eb;
3358 }
Chris Mason6af118c2008-07-22 11:18:07 -04003359 /* add one reference for the tree */
3360 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003361 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003362 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003363
3364 /*
3365 * there is a race where release page may have
3366 * tried to find this extent buffer in the radix
3367 * but failed. It will tell the VM it is safe to
3368 * reclaim the, and it will clear the page private bit.
3369 * We must make sure to set the page private bit properly
3370 * after the extent buffer is in the radix tree so
3371 * it doesn't get lost
3372 */
3373 set_page_extent_mapped(eb->first_page);
3374 set_page_extent_head(eb->first_page, eb->len);
3375 if (!page0)
3376 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003377 return eb;
3378
Chris Mason6af118c2008-07-22 11:18:07 -04003379free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003380 if (eb->first_page && !page0)
3381 unlock_page(eb->first_page);
3382
Chris Masond1310b22008-01-24 16:13:08 -05003383 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003384 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003385 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003386 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003387}
Chris Masond1310b22008-01-24 16:13:08 -05003388
3389struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3390 u64 start, unsigned long len,
3391 gfp_t mask)
3392{
Chris Masond1310b22008-01-24 16:13:08 -05003393 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003394
Miao Xie19fe0a82010-10-26 20:57:29 -04003395 rcu_read_lock();
3396 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3397 if (eb && atomic_inc_not_zero(&eb->refs)) {
3398 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003399 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003400 return eb;
3401 }
3402 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003403
Miao Xie19fe0a82010-10-26 20:57:29 -04003404 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003405}
Chris Masond1310b22008-01-24 16:13:08 -05003406
3407void free_extent_buffer(struct extent_buffer *eb)
3408{
Chris Masond1310b22008-01-24 16:13:08 -05003409 if (!eb)
3410 return;
3411
3412 if (!atomic_dec_and_test(&eb->refs))
3413 return;
3414
Chris Mason6af118c2008-07-22 11:18:07 -04003415 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003416}
Chris Masond1310b22008-01-24 16:13:08 -05003417
3418int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3419 struct extent_buffer *eb)
3420{
Chris Masond1310b22008-01-24 16:13:08 -05003421 unsigned long i;
3422 unsigned long num_pages;
3423 struct page *page;
3424
Chris Masond1310b22008-01-24 16:13:08 -05003425 num_pages = num_extent_pages(eb->start, eb->len);
3426
3427 for (i = 0; i < num_pages; i++) {
3428 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003429 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003430 continue;
3431
Chris Masona61e6f22008-07-22 11:18:08 -04003432 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003433 WARN_ON(!PagePrivate(page));
3434
3435 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003436 if (i == 0)
3437 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003438
Chris Masond1310b22008-01-24 16:13:08 -05003439 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003440 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003441 if (!PageDirty(page)) {
3442 radix_tree_tag_clear(&page->mapping->page_tree,
3443 page_index(page),
3444 PAGECACHE_TAG_DIRTY);
3445 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003446 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003447 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003448 }
3449 return 0;
3450}
Chris Masond1310b22008-01-24 16:13:08 -05003451
3452int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3453 struct extent_buffer *eb)
3454{
3455 return wait_on_extent_writeback(tree, eb->start,
3456 eb->start + eb->len - 1);
3457}
Chris Masond1310b22008-01-24 16:13:08 -05003458
3459int set_extent_buffer_dirty(struct extent_io_tree *tree,
3460 struct extent_buffer *eb)
3461{
3462 unsigned long i;
3463 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003464 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003465
Chris Masonb9473432009-03-13 11:00:37 -04003466 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003467 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003468 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003469 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003470 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003471}
Chris Masond1310b22008-01-24 16:13:08 -05003472
Chris Mason1259ab72008-05-12 13:39:03 -04003473int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003474 struct extent_buffer *eb,
3475 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003476{
3477 unsigned long i;
3478 struct page *page;
3479 unsigned long num_pages;
3480
3481 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003482 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003483
3484 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003485 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003486 for (i = 0; i < num_pages; i++) {
3487 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003488 if (page)
3489 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003490 }
3491 return 0;
3492}
3493
Chris Masond1310b22008-01-24 16:13:08 -05003494int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3495 struct extent_buffer *eb)
3496{
3497 unsigned long i;
3498 struct page *page;
3499 unsigned long num_pages;
3500
3501 num_pages = num_extent_pages(eb->start, eb->len);
3502
3503 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003504 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003505 for (i = 0; i < num_pages; i++) {
3506 page = extent_buffer_page(eb, i);
3507 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3508 ((i == num_pages - 1) &&
3509 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3510 check_page_uptodate(tree, page);
3511 continue;
3512 }
3513 SetPageUptodate(page);
3514 }
3515 return 0;
3516}
Chris Masond1310b22008-01-24 16:13:08 -05003517
Chris Masonce9adaa2008-04-09 16:28:12 -04003518int extent_range_uptodate(struct extent_io_tree *tree,
3519 u64 start, u64 end)
3520{
3521 struct page *page;
3522 int ret;
3523 int pg_uptodate = 1;
3524 int uptodate;
3525 unsigned long index;
3526
Chris Mason9655d292009-09-02 15:22:30 -04003527 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003528 if (ret)
3529 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003530 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003531 index = start >> PAGE_CACHE_SHIFT;
3532 page = find_get_page(tree->mapping, index);
3533 uptodate = PageUptodate(page);
3534 page_cache_release(page);
3535 if (!uptodate) {
3536 pg_uptodate = 0;
3537 break;
3538 }
3539 start += PAGE_CACHE_SIZE;
3540 }
3541 return pg_uptodate;
3542}
3543
Chris Masond1310b22008-01-24 16:13:08 -05003544int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003545 struct extent_buffer *eb,
3546 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003547{
Chris Mason728131d2008-04-09 16:28:12 -04003548 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003549 unsigned long num_pages;
3550 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003551 struct page *page;
3552 int pg_uptodate = 1;
3553
Chris Masonb4ce94d2009-02-04 09:25:08 -05003554 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003555 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003556
Chris Mason42352982008-04-28 16:40:52 -04003557 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003558 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003559 if (ret)
3560 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003561
3562 num_pages = num_extent_pages(eb->start, eb->len);
3563 for (i = 0; i < num_pages; i++) {
3564 page = extent_buffer_page(eb, i);
3565 if (!PageUptodate(page)) {
3566 pg_uptodate = 0;
3567 break;
3568 }
3569 }
Chris Mason42352982008-04-28 16:40:52 -04003570 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003571}
Chris Masond1310b22008-01-24 16:13:08 -05003572
3573int read_extent_buffer_pages(struct extent_io_tree *tree,
3574 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003575 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003576 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003577{
3578 unsigned long i;
3579 unsigned long start_i;
3580 struct page *page;
3581 int err;
3582 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003583 int locked_pages = 0;
3584 int all_uptodate = 1;
3585 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003586 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003587 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003588 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003589
Chris Masonb4ce94d2009-02-04 09:25:08 -05003590 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003591 return 0;
3592
Chris Masonce9adaa2008-04-09 16:28:12 -04003593 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003594 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003595 return 0;
3596 }
3597
3598 if (start) {
3599 WARN_ON(start < eb->start);
3600 start_i = (start >> PAGE_CACHE_SHIFT) -
3601 (eb->start >> PAGE_CACHE_SHIFT);
3602 } else {
3603 start_i = 0;
3604 }
3605
3606 num_pages = num_extent_pages(eb->start, eb->len);
3607 for (i = start_i; i < num_pages; i++) {
3608 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003609 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003610 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003611 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003612 } else {
3613 lock_page(page);
3614 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003615 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003616 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003617 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003618 }
3619 if (all_uptodate) {
3620 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003621 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003622 goto unlock_exit;
3623 }
3624
3625 for (i = start_i; i < num_pages; i++) {
3626 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003627
3628 WARN_ON(!PagePrivate(page));
3629
3630 set_page_extent_mapped(page);
3631 if (i == 0)
3632 set_page_extent_head(page, eb->len);
3633
Chris Masonce9adaa2008-04-09 16:28:12 -04003634 if (inc_all_pages)
3635 page_cache_get(page);
3636 if (!PageUptodate(page)) {
3637 if (start_i == 0)
3638 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003639 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003640 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003641 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003642 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003643 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003644 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003645 } else {
3646 unlock_page(page);
3647 }
3648 }
3649
Chris Masona86c12c2008-02-07 10:50:54 -05003650 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003651 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003652
Chris Masond3977122009-01-05 21:25:51 -05003653 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003654 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003655
Chris Masond1310b22008-01-24 16:13:08 -05003656 for (i = start_i; i < num_pages; i++) {
3657 page = extent_buffer_page(eb, i);
3658 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003659 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003660 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003661 }
Chris Masond3977122009-01-05 21:25:51 -05003662
Chris Masond1310b22008-01-24 16:13:08 -05003663 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003664 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003665 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003666
3667unlock_exit:
3668 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003669 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003670 page = extent_buffer_page(eb, i);
3671 i++;
3672 unlock_page(page);
3673 locked_pages--;
3674 }
3675 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003676}
Chris Masond1310b22008-01-24 16:13:08 -05003677
3678void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3679 unsigned long start,
3680 unsigned long len)
3681{
3682 size_t cur;
3683 size_t offset;
3684 struct page *page;
3685 char *kaddr;
3686 char *dst = (char *)dstv;
3687 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3688 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003689
3690 WARN_ON(start > eb->len);
3691 WARN_ON(start + len > eb->start + eb->len);
3692
3693 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3694
Chris Masond3977122009-01-05 21:25:51 -05003695 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003696 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003697
3698 cur = min(len, (PAGE_CACHE_SIZE - offset));
3699 kaddr = kmap_atomic(page, KM_USER1);
3700 memcpy(dst, kaddr + offset, cur);
3701 kunmap_atomic(kaddr, KM_USER1);
3702
3703 dst += cur;
3704 len -= cur;
3705 offset = 0;
3706 i++;
3707 }
3708}
Chris Masond1310b22008-01-24 16:13:08 -05003709
3710int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3711 unsigned long min_len, char **token, char **map,
3712 unsigned long *map_start,
3713 unsigned long *map_len, int km)
3714{
3715 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3716 char *kaddr;
3717 struct page *p;
3718 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3719 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3720 unsigned long end_i = (start_offset + start + min_len - 1) >>
3721 PAGE_CACHE_SHIFT;
3722
3723 if (i != end_i)
3724 return -EINVAL;
3725
3726 if (i == 0) {
3727 offset = start_offset;
3728 *map_start = 0;
3729 } else {
3730 offset = 0;
3731 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3732 }
Chris Masond3977122009-01-05 21:25:51 -05003733
Chris Masond1310b22008-01-24 16:13:08 -05003734 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003735 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3736 "wanted %lu %lu\n", (unsigned long long)eb->start,
3737 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003738 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003739 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003740 }
3741
3742 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003743 kaddr = kmap_atomic(p, km);
3744 *token = kaddr;
3745 *map = kaddr + offset;
3746 *map_len = PAGE_CACHE_SIZE - offset;
3747 return 0;
3748}
Chris Masond1310b22008-01-24 16:13:08 -05003749
3750int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3751 unsigned long min_len,
3752 char **token, char **map,
3753 unsigned long *map_start,
3754 unsigned long *map_len, int km)
3755{
3756 int err;
3757 int save = 0;
3758 if (eb->map_token) {
3759 unmap_extent_buffer(eb, eb->map_token, km);
3760 eb->map_token = NULL;
3761 save = 1;
3762 }
3763 err = map_private_extent_buffer(eb, start, min_len, token, map,
3764 map_start, map_len, km);
3765 if (!err && save) {
3766 eb->map_token = *token;
3767 eb->kaddr = *map;
3768 eb->map_start = *map_start;
3769 eb->map_len = *map_len;
3770 }
3771 return err;
3772}
Chris Masond1310b22008-01-24 16:13:08 -05003773
3774void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3775{
3776 kunmap_atomic(token, km);
3777}
Chris Masond1310b22008-01-24 16:13:08 -05003778
3779int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3780 unsigned long start,
3781 unsigned long len)
3782{
3783 size_t cur;
3784 size_t offset;
3785 struct page *page;
3786 char *kaddr;
3787 char *ptr = (char *)ptrv;
3788 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3789 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3790 int ret = 0;
3791
3792 WARN_ON(start > eb->len);
3793 WARN_ON(start + len > eb->start + eb->len);
3794
3795 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3796
Chris Masond3977122009-01-05 21:25:51 -05003797 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003798 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003799
3800 cur = min(len, (PAGE_CACHE_SIZE - offset));
3801
3802 kaddr = kmap_atomic(page, KM_USER0);
3803 ret = memcmp(ptr, kaddr + offset, cur);
3804 kunmap_atomic(kaddr, KM_USER0);
3805 if (ret)
3806 break;
3807
3808 ptr += cur;
3809 len -= cur;
3810 offset = 0;
3811 i++;
3812 }
3813 return ret;
3814}
Chris Masond1310b22008-01-24 16:13:08 -05003815
3816void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3817 unsigned long start, unsigned long len)
3818{
3819 size_t cur;
3820 size_t offset;
3821 struct page *page;
3822 char *kaddr;
3823 char *src = (char *)srcv;
3824 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3825 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3826
3827 WARN_ON(start > eb->len);
3828 WARN_ON(start + len > eb->start + eb->len);
3829
3830 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3831
Chris Masond3977122009-01-05 21:25:51 -05003832 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003833 page = extent_buffer_page(eb, i);
3834 WARN_ON(!PageUptodate(page));
3835
3836 cur = min(len, PAGE_CACHE_SIZE - offset);
3837 kaddr = kmap_atomic(page, KM_USER1);
3838 memcpy(kaddr + offset, src, cur);
3839 kunmap_atomic(kaddr, KM_USER1);
3840
3841 src += cur;
3842 len -= cur;
3843 offset = 0;
3844 i++;
3845 }
3846}
Chris Masond1310b22008-01-24 16:13:08 -05003847
3848void memset_extent_buffer(struct extent_buffer *eb, char c,
3849 unsigned long start, unsigned long len)
3850{
3851 size_t cur;
3852 size_t offset;
3853 struct page *page;
3854 char *kaddr;
3855 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3856 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3857
3858 WARN_ON(start > eb->len);
3859 WARN_ON(start + len > eb->start + eb->len);
3860
3861 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3862
Chris Masond3977122009-01-05 21:25:51 -05003863 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003864 page = extent_buffer_page(eb, i);
3865 WARN_ON(!PageUptodate(page));
3866
3867 cur = min(len, PAGE_CACHE_SIZE - offset);
3868 kaddr = kmap_atomic(page, KM_USER0);
3869 memset(kaddr + offset, c, cur);
3870 kunmap_atomic(kaddr, KM_USER0);
3871
3872 len -= cur;
3873 offset = 0;
3874 i++;
3875 }
3876}
Chris Masond1310b22008-01-24 16:13:08 -05003877
3878void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3879 unsigned long dst_offset, unsigned long src_offset,
3880 unsigned long len)
3881{
3882 u64 dst_len = dst->len;
3883 size_t cur;
3884 size_t offset;
3885 struct page *page;
3886 char *kaddr;
3887 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3888 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3889
3890 WARN_ON(src->len != dst_len);
3891
3892 offset = (start_offset + dst_offset) &
3893 ((unsigned long)PAGE_CACHE_SIZE - 1);
3894
Chris Masond3977122009-01-05 21:25:51 -05003895 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003896 page = extent_buffer_page(dst, i);
3897 WARN_ON(!PageUptodate(page));
3898
3899 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3900
3901 kaddr = kmap_atomic(page, KM_USER0);
3902 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3903 kunmap_atomic(kaddr, KM_USER0);
3904
3905 src_offset += cur;
3906 len -= cur;
3907 offset = 0;
3908 i++;
3909 }
3910}
Chris Masond1310b22008-01-24 16:13:08 -05003911
3912static void move_pages(struct page *dst_page, struct page *src_page,
3913 unsigned long dst_off, unsigned long src_off,
3914 unsigned long len)
3915{
3916 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3917 if (dst_page == src_page) {
3918 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3919 } else {
3920 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3921 char *p = dst_kaddr + dst_off + len;
3922 char *s = src_kaddr + src_off + len;
3923
3924 while (len--)
3925 *--p = *--s;
3926
3927 kunmap_atomic(src_kaddr, KM_USER1);
3928 }
3929 kunmap_atomic(dst_kaddr, KM_USER0);
3930}
3931
Sergei Trofimovich33872062011-04-11 21:52:52 +00003932static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3933{
3934 unsigned long distance = (src > dst) ? src - dst : dst - src;
3935 return distance < len;
3936}
3937
Chris Masond1310b22008-01-24 16:13:08 -05003938static void copy_pages(struct page *dst_page, struct page *src_page,
3939 unsigned long dst_off, unsigned long src_off,
3940 unsigned long len)
3941{
3942 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3943 char *src_kaddr;
3944
Sergei Trofimovich33872062011-04-11 21:52:52 +00003945 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003946 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003947 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003948 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003949 BUG_ON(areas_overlap(src_off, dst_off, len));
3950 }
Chris Masond1310b22008-01-24 16:13:08 -05003951
3952 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3953 kunmap_atomic(dst_kaddr, KM_USER0);
3954 if (dst_page != src_page)
3955 kunmap_atomic(src_kaddr, KM_USER1);
3956}
3957
3958void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3959 unsigned long src_offset, unsigned long len)
3960{
3961 size_t cur;
3962 size_t dst_off_in_page;
3963 size_t src_off_in_page;
3964 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3965 unsigned long dst_i;
3966 unsigned long src_i;
3967
3968 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003969 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3970 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003971 BUG_ON(1);
3972 }
3973 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003974 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3975 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003976 BUG_ON(1);
3977 }
3978
Chris Masond3977122009-01-05 21:25:51 -05003979 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003980 dst_off_in_page = (start_offset + dst_offset) &
3981 ((unsigned long)PAGE_CACHE_SIZE - 1);
3982 src_off_in_page = (start_offset + src_offset) &
3983 ((unsigned long)PAGE_CACHE_SIZE - 1);
3984
3985 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3986 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3987
3988 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3989 src_off_in_page));
3990 cur = min_t(unsigned long, cur,
3991 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3992
3993 copy_pages(extent_buffer_page(dst, dst_i),
3994 extent_buffer_page(dst, src_i),
3995 dst_off_in_page, src_off_in_page, cur);
3996
3997 src_offset += cur;
3998 dst_offset += cur;
3999 len -= cur;
4000 }
4001}
Chris Masond1310b22008-01-24 16:13:08 -05004002
4003void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4004 unsigned long src_offset, unsigned long len)
4005{
4006 size_t cur;
4007 size_t dst_off_in_page;
4008 size_t src_off_in_page;
4009 unsigned long dst_end = dst_offset + len - 1;
4010 unsigned long src_end = src_offset + len - 1;
4011 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4012 unsigned long dst_i;
4013 unsigned long src_i;
4014
4015 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004016 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4017 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004018 BUG_ON(1);
4019 }
4020 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004021 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4022 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004023 BUG_ON(1);
4024 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004025 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004026 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4027 return;
4028 }
Chris Masond3977122009-01-05 21:25:51 -05004029 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004030 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4031 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4032
4033 dst_off_in_page = (start_offset + dst_end) &
4034 ((unsigned long)PAGE_CACHE_SIZE - 1);
4035 src_off_in_page = (start_offset + src_end) &
4036 ((unsigned long)PAGE_CACHE_SIZE - 1);
4037
4038 cur = min_t(unsigned long, len, src_off_in_page + 1);
4039 cur = min(cur, dst_off_in_page + 1);
4040 move_pages(extent_buffer_page(dst, dst_i),
4041 extent_buffer_page(dst, src_i),
4042 dst_off_in_page - cur + 1,
4043 src_off_in_page - cur + 1, cur);
4044
4045 dst_end -= cur;
4046 src_end -= cur;
4047 len -= cur;
4048 }
4049}
Chris Mason6af118c2008-07-22 11:18:07 -04004050
Miao Xie19fe0a82010-10-26 20:57:29 -04004051static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4052{
4053 struct extent_buffer *eb =
4054 container_of(head, struct extent_buffer, rcu_head);
4055
4056 btrfs_release_extent_buffer(eb);
4057}
4058
Chris Mason6af118c2008-07-22 11:18:07 -04004059int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4060{
4061 u64 start = page_offset(page);
4062 struct extent_buffer *eb;
4063 int ret = 1;
Chris Mason6af118c2008-07-22 11:18:07 -04004064
4065 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004066 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004067 if (!eb) {
4068 spin_unlock(&tree->buffer_lock);
4069 return ret;
4070 }
Chris Mason6af118c2008-07-22 11:18:07 -04004071
Chris Masonb9473432009-03-13 11:00:37 -04004072 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4073 ret = 0;
4074 goto out;
4075 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004076
Miao Xie19fe0a82010-10-26 20:57:29 -04004077 /*
4078 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4079 * Or go back.
4080 */
4081 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4082 ret = 0;
4083 goto out;
4084 }
4085
4086 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04004087out:
4088 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004089
4090 /* at this point we can safely release the extent buffer */
4091 if (atomic_read(&eb->refs) == 0)
4092 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118c2008-07-22 11:18:07 -04004093 return ret;
4094}