blob: 0cee46e0108192e123b7a2d474d3bed66c3a9cbf [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>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040016#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050019
Chris Masond1310b22008-01-24 16:13:08 -050020static struct kmem_cache *extent_state_cache;
21static struct kmem_cache *extent_buffer_cache;
22
23static LIST_HEAD(buffers);
24static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040025
Chris Masonb47eda82008-11-10 12:34:40 -050026#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050027#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050028static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040029#endif
Chris Masond1310b22008-01-24 16:13:08 -050030
Chris Masond1310b22008-01-24 16:13:08 -050031#define BUFFER_LRU_MAX 64
32
33struct tree_entry {
34 u64 start;
35 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050036 struct rb_node rb_node;
37};
38
39struct extent_page_data {
40 struct bio *bio;
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050043
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
46 */
Chris Masonffbd5172009-04-20 15:50:09 -040047 unsigned int extent_locked:1;
48
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050051};
52
53int __init extent_io_init(void)
54{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020055 extent_state_cache = kmem_cache_create("extent_state",
56 sizeof(struct extent_state), 0,
57 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050058 if (!extent_state_cache)
59 return -ENOMEM;
60
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020061 extent_buffer_cache = kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050064 if (!extent_buffer_cache)
65 goto free_state_cache;
66 return 0;
67
68free_state_cache:
69 kmem_cache_destroy(extent_state_cache);
70 return -ENOMEM;
71}
72
73void extent_io_exit(void)
74{
75 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040076 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050077
78 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040079 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050080 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state->start,
83 (unsigned long long)state->end,
84 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040085 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050086 kmem_cache_free(extent_state_cache, state);
87
88 }
89
Chris Mason2d2ae542008-03-26 16:24:23 -040090 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050092 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb->start,
94 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040095 list_del(&eb->leak_list);
96 kmem_cache_free(extent_buffer_cache, eb);
97 }
Chris Masond1310b22008-01-24 16:13:08 -050098 if (extent_state_cache)
99 kmem_cache_destroy(extent_state_cache);
100 if (extent_buffer_cache)
101 kmem_cache_destroy(extent_buffer_cache);
102}
103
104void extent_io_tree_init(struct extent_io_tree *tree,
105 struct address_space *mapping, gfp_t mask)
106{
Eric Paris6bef4d32010-02-23 19:43:04 +0000107 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400108 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500109 tree->ops = NULL;
110 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500111 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400112 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500113 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500114}
Chris Masond1310b22008-01-24 16:13:08 -0500115
Christoph Hellwigb2950862008-12-02 09:54:17 -0500116static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500117{
118 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500119#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400120 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400121#endif
Chris Masond1310b22008-01-24 16:13:08 -0500122
123 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400124 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500125 return state;
126 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500127 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500128 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500129#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400133#endif
Chris Masond1310b22008-01-24 16:13:08 -0500134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
136 return state;
137}
Chris Masond1310b22008-01-24 16:13:08 -0500138
Chris Mason4845e442010-05-25 20:56:50 -0400139void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500140{
Chris Masond1310b22008-01-24 16:13:08 -0500141 if (!state)
142 return;
143 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500144#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400145 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400146#endif
Chris Mason70dec802008-01-29 09:59:12 -0500147 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500148#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Masond1310b22008-01-24 16:13:08 -0500153 kmem_cache_free(extent_state_cache, state);
154 }
155}
Chris Masond1310b22008-01-24 16:13:08 -0500156
157static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
159{
Chris Masond3977122009-01-05 21:25:51 -0500160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500162 struct tree_entry *entry;
163
Chris Masond3977122009-01-05 21:25:51 -0500164 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500165 parent = *p;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
167
168 if (offset < entry->start)
169 p = &(*p)->rb_left;
170 else if (offset > entry->end)
171 p = &(*p)->rb_right;
172 else
173 return parent;
174 }
175
176 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
179 return NULL;
180}
181
Chris Mason80ea96b2008-02-01 14:51:59 -0500182static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
185{
Chris Mason80ea96b2008-02-01 14:51:59 -0500186 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500187 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
192
Chris Masond3977122009-01-05 21:25:51 -0500193 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500194 entry = rb_entry(n, struct tree_entry, rb_node);
195 prev = n;
196 prev_entry = entry;
197
198 if (offset < entry->start)
199 n = n->rb_left;
200 else if (offset > entry->end)
201 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500202 else
Chris Masond1310b22008-01-24 16:13:08 -0500203 return n;
204 }
205
206 if (prev_ret) {
207 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500208 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
211 }
212 *prev_ret = prev;
213 prev = orig_prev;
214 }
215
216 if (next_ret) {
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500218 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
221 }
222 *next_ret = prev;
223 }
224 return NULL;
225}
226
Chris Mason80ea96b2008-02-01 14:51:59 -0500227static inline struct rb_node *tree_search(struct extent_io_tree *tree,
228 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500229{
Chris Mason70dec802008-01-29 09:59:12 -0500230 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500231 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500232
Chris Mason80ea96b2008-02-01 14:51:59 -0500233 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500234 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500235 return prev;
236 return ret;
237}
238
Josef Bacik9ed74f22009-09-11 16:12:44 -0400239static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
240 struct extent_state *other)
241{
242 if (tree->ops && tree->ops->merge_extent_hook)
243 tree->ops->merge_extent_hook(tree->mapping->host, new,
244 other);
245}
246
Chris Masond1310b22008-01-24 16:13:08 -0500247/*
248 * utility function to look for merge candidates inside a given range.
249 * Any extents with matching state are merged together into a single
250 * extent in the tree. Extents with EXTENT_IO in their state field
251 * are not merged because the end_io handlers need to be able to do
252 * operations on them without sleeping (or doing allocations/splits).
253 *
254 * This should be called with the tree lock held.
255 */
256static int merge_state(struct extent_io_tree *tree,
257 struct extent_state *state)
258{
259 struct extent_state *other;
260 struct rb_node *other_node;
261
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400262 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500263 return 0;
264
265 other_node = rb_prev(&state->rb_node);
266 if (other_node) {
267 other = rb_entry(other_node, struct extent_state, rb_node);
268 if (other->end == state->start - 1 &&
269 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400270 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500271 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500272 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500273 rb_erase(&other->rb_node, &tree->state);
274 free_extent_state(other);
275 }
276 }
277 other_node = rb_next(&state->rb_node);
278 if (other_node) {
279 other = rb_entry(other_node, struct extent_state, rb_node);
280 if (other->start == state->end + 1 &&
281 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400282 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500283 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500284 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500285 rb_erase(&state->rb_node, &tree->state);
286 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400287 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500288 }
289 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400290
Chris Masond1310b22008-01-24 16:13:08 -0500291 return 0;
292}
293
Josef Bacik9ed74f22009-09-11 16:12:44 -0400294static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400295 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500296{
297 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400298 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400299 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500300 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400301
302 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500303}
304
305static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400306 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400308 if (tree->ops && tree->ops->clear_bit_hook)
309 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500310}
311
Chris Masond1310b22008-01-24 16:13:08 -0500312/*
313 * insert an extent_state struct into the tree. 'bits' are set on the
314 * struct before it is inserted.
315 *
316 * This may return -EEXIST if the extent is already there, in which case the
317 * state struct is freed.
318 *
319 * The tree lock is not taken internally. This is a utility function and
320 * probably isn't what you want to call (see set/clear_extent_bit).
321 */
322static int insert_state(struct extent_io_tree *tree,
323 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400324 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500325{
326 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400327 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400328 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500329
330 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500331 printk(KERN_ERR "btrfs end < start %llu %llu\n",
332 (unsigned long long)end,
333 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500334 WARN_ON(1);
335 }
Chris Masond1310b22008-01-24 16:13:08 -0500336 state->start = start;
337 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400338 ret = set_state_cb(tree, state, bits);
339 if (ret)
340 return ret;
341
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400342 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400343 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400344 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500345 node = tree_insert(&tree->state, end, &state->rb_node);
346 if (node) {
347 struct extent_state *found;
348 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500349 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
350 "%llu %llu\n", (unsigned long long)found->start,
351 (unsigned long long)found->end,
352 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500353 free_extent_state(state);
354 return -EEXIST;
355 }
Chris Mason70dec802008-01-29 09:59:12 -0500356 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500357 merge_state(tree, state);
358 return 0;
359}
360
Josef Bacik9ed74f22009-09-11 16:12:44 -0400361static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
362 u64 split)
363{
364 if (tree->ops && tree->ops->split_extent_hook)
365 return tree->ops->split_extent_hook(tree->mapping->host,
366 orig, split);
367 return 0;
368}
369
Chris Masond1310b22008-01-24 16:13:08 -0500370/*
371 * split a given extent state struct in two, inserting the preallocated
372 * struct 'prealloc' as the newly created second half. 'split' indicates an
373 * offset inside 'orig' where it should be split.
374 *
375 * Before calling,
376 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
377 * are two extent state structs in the tree:
378 * prealloc: [orig->start, split - 1]
379 * orig: [ split, orig->end ]
380 *
381 * The tree locks are not taken by this function. They need to be held
382 * by the caller.
383 */
384static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
385 struct extent_state *prealloc, u64 split)
386{
387 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400388
389 split_cb(tree, orig, split);
390
Chris Masond1310b22008-01-24 16:13:08 -0500391 prealloc->start = orig->start;
392 prealloc->end = split - 1;
393 prealloc->state = orig->state;
394 orig->start = split;
395
396 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
397 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500398 free_extent_state(prealloc);
399 return -EEXIST;
400 }
Chris Mason70dec802008-01-29 09:59:12 -0500401 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500402 return 0;
403}
404
405/*
406 * utility function to clear some bits in an extent state struct.
407 * it will optionally wake up any one waiting on this state (wake == 1), or
408 * forcibly remove the state from the tree (delete == 1).
409 *
410 * If no bits are set on the state struct after clearing things, the
411 * struct is freed and removed from the tree
412 */
413static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400414 struct extent_state *state,
415 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500416{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400417 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400418 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500419
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400420 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500421 u64 range = state->end - state->start + 1;
422 WARN_ON(range > tree->dirty_bytes);
423 tree->dirty_bytes -= range;
424 }
Chris Mason291d6732008-01-29 15:55:23 -0500425 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400426 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500427 if (wake)
428 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400429 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500430 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500431 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500432 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500433 free_extent_state(state);
434 } else {
435 WARN_ON(1);
436 }
437 } else {
438 merge_state(tree, state);
439 }
440 return ret;
441}
442
443/*
444 * clear some bits on a range in the tree. This may require splitting
445 * or inserting elements in the tree, so the gfp mask is used to
446 * indicate which allocations or sleeping are allowed.
447 *
448 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
449 * the given range from the tree regardless of state (ie for truncate).
450 *
451 * the range [start, end] is inclusive.
452 *
453 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
454 * bits were already set, or zero if none of the bits were already set.
455 */
456int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400457 int bits, int wake, int delete,
458 struct extent_state **cached_state,
459 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500460{
461 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400462 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500463 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400464 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500465 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400466 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500467 int err;
468 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000469 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500470
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400471 if (delete)
472 bits |= ~EXTENT_CTLBITS;
473 bits |= EXTENT_FIRST_DELALLOC;
474
Josef Bacik2ac55d42010-02-03 19:33:23 +0000475 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
476 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500477again:
478 if (!prealloc && (mask & __GFP_WAIT)) {
479 prealloc = alloc_extent_state(mask);
480 if (!prealloc)
481 return -ENOMEM;
482 }
483
Chris Masoncad321a2008-12-17 14:51:42 -0500484 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400485 if (cached_state) {
486 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000487
488 if (clear) {
489 *cached_state = NULL;
490 cached_state = NULL;
491 }
492
Chris Mason42daec22009-09-23 19:51:09 -0400493 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000494 if (clear)
495 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400496 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400497 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400498 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000499 if (clear)
500 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400501 }
Chris Masond1310b22008-01-24 16:13:08 -0500502 /*
503 * this search will find the extents that end after
504 * our range starts
505 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500506 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500507 if (!node)
508 goto out;
509 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400510hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500511 if (state->start > end)
512 goto out;
513 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400514 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500515
516 /*
517 * | ---- desired range ---- |
518 * | state | or
519 * | ------------- state -------------- |
520 *
521 * We need to split the extent we found, and may flip
522 * bits on second half.
523 *
524 * If the extent we found extends past our range, we
525 * just split and search again. It'll get split again
526 * the next time though.
527 *
528 * If the extent we found is inside our range, we clear
529 * the desired bit on it.
530 */
531
532 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500533 if (!prealloc)
534 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500535 err = split_state(tree, state, prealloc, start);
536 BUG_ON(err == -EEXIST);
537 prealloc = NULL;
538 if (err)
539 goto out;
540 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400541 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400542 if (last_end == (u64)-1)
543 goto out;
544 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500545 }
546 goto search_again;
547 }
548 /*
549 * | ---- desired range ---- |
550 * | state |
551 * We need to split the extent, and clear the bit
552 * on the first half
553 */
554 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500555 if (!prealloc)
556 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500557 err = split_state(tree, state, prealloc, end + 1);
558 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500559 if (wake)
560 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400561
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400562 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400563
Chris Masond1310b22008-01-24 16:13:08 -0500564 prealloc = NULL;
565 goto out;
566 }
Chris Mason42daec22009-09-23 19:51:09 -0400567
Chris Mason2c64c532009-09-02 15:04:12 -0400568 if (state->end < end && prealloc && !need_resched())
569 next_node = rb_next(&state->rb_node);
570 else
571 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400572
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400573 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400574 if (last_end == (u64)-1)
575 goto out;
576 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400577 if (start <= end && next_node) {
578 state = rb_entry(next_node, struct extent_state,
579 rb_node);
580 if (state->start == start)
581 goto hit_next;
582 }
Chris Masond1310b22008-01-24 16:13:08 -0500583 goto search_again;
584
585out:
Chris Masoncad321a2008-12-17 14:51:42 -0500586 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500587 if (prealloc)
588 free_extent_state(prealloc);
589
590 return set;
591
592search_again:
593 if (start > end)
594 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500595 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500596 if (mask & __GFP_WAIT)
597 cond_resched();
598 goto again;
599}
Chris Masond1310b22008-01-24 16:13:08 -0500600
601static int wait_on_state(struct extent_io_tree *tree,
602 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500603 __releases(tree->lock)
604 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500605{
606 DEFINE_WAIT(wait);
607 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500608 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500609 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500610 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500611 finish_wait(&state->wq, &wait);
612 return 0;
613}
614
615/*
616 * waits for one or more bits to clear on a range in the state tree.
617 * The range [start, end] is inclusive.
618 * The tree lock is taken by this function
619 */
620int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
621{
622 struct extent_state *state;
623 struct rb_node *node;
624
Chris Masoncad321a2008-12-17 14:51:42 -0500625 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500626again:
627 while (1) {
628 /*
629 * this search will find all the extents that end after
630 * our range starts
631 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500632 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500633 if (!node)
634 break;
635
636 state = rb_entry(node, struct extent_state, rb_node);
637
638 if (state->start > end)
639 goto out;
640
641 if (state->state & bits) {
642 start = state->start;
643 atomic_inc(&state->refs);
644 wait_on_state(tree, state);
645 free_extent_state(state);
646 goto again;
647 }
648 start = state->end + 1;
649
650 if (start > end)
651 break;
652
653 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500654 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500655 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500656 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500657 }
658 }
659out:
Chris Masoncad321a2008-12-17 14:51:42 -0500660 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500661 return 0;
662}
Chris Masond1310b22008-01-24 16:13:08 -0500663
Josef Bacik9ed74f22009-09-11 16:12:44 -0400664static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500665 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400666 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500667{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400668 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400669 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400670
671 ret = set_state_cb(tree, state, bits);
672 if (ret)
673 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400674 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500675 u64 range = state->end - state->start + 1;
676 tree->dirty_bytes += range;
677 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400678 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400679
680 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500681}
682
Chris Mason2c64c532009-09-02 15:04:12 -0400683static void cache_state(struct extent_state *state,
684 struct extent_state **cached_ptr)
685{
686 if (cached_ptr && !(*cached_ptr)) {
687 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
688 *cached_ptr = state;
689 atomic_inc(&state->refs);
690 }
691 }
692}
693
Arne Jansen507903b2011-04-06 10:02:20 +0000694static void uncache_state(struct extent_state **cached_ptr)
695{
696 if (cached_ptr && (*cached_ptr)) {
697 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400698 *cached_ptr = NULL;
699 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000700 }
701}
702
Chris Masond1310b22008-01-24 16:13:08 -0500703/*
Chris Mason1edbb732009-09-02 13:24:36 -0400704 * set some bits on a range in the tree. This may require allocations or
705 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500706 *
Chris Mason1edbb732009-09-02 13:24:36 -0400707 * If any of the exclusive bits are set, this will fail with -EEXIST if some
708 * part of the range already has the desired bits set. The start of the
709 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500710 *
Chris Mason1edbb732009-09-02 13:24:36 -0400711 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500712 */
Chris Mason1edbb732009-09-02 13:24:36 -0400713
Chris Mason4845e442010-05-25 20:56:50 -0400714int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
715 int bits, int exclusive_bits, u64 *failed_start,
716 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500717{
718 struct extent_state *state;
719 struct extent_state *prealloc = NULL;
720 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500721 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500722 u64 last_start;
723 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400724
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400725 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500726again:
727 if (!prealloc && (mask & __GFP_WAIT)) {
728 prealloc = alloc_extent_state(mask);
729 if (!prealloc)
730 return -ENOMEM;
731 }
732
Chris Masoncad321a2008-12-17 14:51:42 -0500733 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400734 if (cached_state && *cached_state) {
735 state = *cached_state;
736 if (state->start == start && state->tree) {
737 node = &state->rb_node;
738 goto hit_next;
739 }
740 }
Chris Masond1310b22008-01-24 16:13:08 -0500741 /*
742 * this search will find all the extents that end after
743 * our range starts.
744 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500745 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500746 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400747 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500748 prealloc = NULL;
749 BUG_ON(err == -EEXIST);
750 goto out;
751 }
Chris Masond1310b22008-01-24 16:13:08 -0500752 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400753hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500754 last_start = state->start;
755 last_end = state->end;
756
757 /*
758 * | ---- desired range ---- |
759 * | state |
760 *
761 * Just lock what we found and keep going
762 */
763 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400764 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400765 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500766 *failed_start = state->start;
767 err = -EEXIST;
768 goto out;
769 }
Chris Mason42daec22009-09-23 19:51:09 -0400770
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400771 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400772 if (err)
773 goto out;
774
Chris Mason2c64c532009-09-02 15:04:12 -0400775 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500776 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400777 if (last_end == (u64)-1)
778 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400779
Yan Zheng5c939df2009-05-27 09:16:03 -0400780 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400781 if (start < end && prealloc && !need_resched()) {
782 next_node = rb_next(node);
783 if (next_node) {
784 state = rb_entry(next_node, struct extent_state,
785 rb_node);
786 if (state->start == start)
787 goto hit_next;
788 }
789 }
Chris Masond1310b22008-01-24 16:13:08 -0500790 goto search_again;
791 }
792
793 /*
794 * | ---- desired range ---- |
795 * | state |
796 * or
797 * | ------------- state -------------- |
798 *
799 * We need to split the extent we found, and may flip bits on
800 * second half.
801 *
802 * If the extent we found extends past our
803 * range, we just split and search again. It'll get split
804 * again the next time though.
805 *
806 * If the extent we found is inside our range, we set the
807 * desired bit on it.
808 */
809 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400810 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500811 *failed_start = start;
812 err = -EEXIST;
813 goto out;
814 }
815 err = split_state(tree, state, prealloc, start);
816 BUG_ON(err == -EEXIST);
817 prealloc = NULL;
818 if (err)
819 goto out;
820 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400821 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400822 if (err)
823 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400824 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500825 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400826 if (last_end == (u64)-1)
827 goto out;
828 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500829 }
830 goto search_again;
831 }
832 /*
833 * | ---- desired range ---- |
834 * | state | or | state |
835 *
836 * There's a hole, we need to insert something in it and
837 * ignore the extent we found.
838 */
839 if (state->start > start) {
840 u64 this_end;
841 if (end < last_start)
842 this_end = end;
843 else
Chris Masond3977122009-01-05 21:25:51 -0500844 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500845 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400846 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400847 BUG_ON(err == -EEXIST);
848 if (err) {
849 prealloc = NULL;
850 goto out;
851 }
Chris Mason2c64c532009-09-02 15:04:12 -0400852 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500853 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500854 start = this_end + 1;
855 goto search_again;
856 }
857 /*
858 * | ---- desired range ---- |
859 * | state |
860 * We need to split the extent, and set the bit
861 * on the first half
862 */
863 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400864 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500865 *failed_start = start;
866 err = -EEXIST;
867 goto out;
868 }
869 err = split_state(tree, state, prealloc, end + 1);
870 BUG_ON(err == -EEXIST);
871
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400872 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400873 if (err) {
874 prealloc = NULL;
875 goto out;
876 }
Chris Mason2c64c532009-09-02 15:04:12 -0400877 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500878 merge_state(tree, prealloc);
879 prealloc = NULL;
880 goto out;
881 }
882
883 goto search_again;
884
885out:
Chris Masoncad321a2008-12-17 14:51:42 -0500886 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500887 if (prealloc)
888 free_extent_state(prealloc);
889
890 return err;
891
892search_again:
893 if (start > end)
894 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500895 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500896 if (mask & __GFP_WAIT)
897 cond_resched();
898 goto again;
899}
Chris Masond1310b22008-01-24 16:13:08 -0500900
901/* wrappers around set/clear extent bit */
902int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
903 gfp_t mask)
904{
905 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400906 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500907}
Chris Masond1310b22008-01-24 16:13:08 -0500908
909int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
910 int bits, gfp_t mask)
911{
912 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400913 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500914}
Chris Masond1310b22008-01-24 16:13:08 -0500915
916int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
917 int bits, gfp_t mask)
918{
Chris Mason2c64c532009-09-02 15:04:12 -0400919 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500920}
Chris Masond1310b22008-01-24 16:13:08 -0500921
922int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000923 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500924{
925 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400926 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000927 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500928}
Chris Masond1310b22008-01-24 16:13:08 -0500929
930int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
931 gfp_t mask)
932{
933 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400934 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400935 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500936}
Chris Masond1310b22008-01-24 16:13:08 -0500937
938int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
939 gfp_t mask)
940{
941 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400942 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500943}
Chris Masond1310b22008-01-24 16:13:08 -0500944
Christoph Hellwigb2950862008-12-02 09:54:17 -0500945static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500946 gfp_t mask)
947{
Chris Mason2c64c532009-09-02 15:04:12 -0400948 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
949 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500950}
Chris Masond1310b22008-01-24 16:13:08 -0500951
952int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000953 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500954{
Arne Jansen507903b2011-04-06 10:02:20 +0000955 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
956 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500957}
Chris Masond1310b22008-01-24 16:13:08 -0500958
Chris Masond3977122009-01-05 21:25:51 -0500959static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000960 u64 end, struct extent_state **cached_state,
961 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500962{
Chris Mason2c64c532009-09-02 15:04:12 -0400963 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000964 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500965}
Chris Masond1310b22008-01-24 16:13:08 -0500966
Chris Masond1310b22008-01-24 16:13:08 -0500967int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
968{
969 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
970}
Chris Masond1310b22008-01-24 16:13:08 -0500971
Chris Masond352ac62008-09-29 15:18:18 -0400972/*
973 * either insert or lock state struct between start and end use mask to tell
974 * us if waiting is desired.
975 */
Chris Mason1edbb732009-09-02 13:24:36 -0400976int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400977 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500978{
979 int err;
980 u64 failed_start;
981 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400982 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400983 EXTENT_LOCKED, &failed_start,
984 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500985 if (err == -EEXIST && (mask & __GFP_WAIT)) {
986 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
987 start = failed_start;
988 } else {
989 break;
990 }
991 WARN_ON(start > end);
992 }
993 return err;
994}
Chris Masond1310b22008-01-24 16:13:08 -0500995
Chris Mason1edbb732009-09-02 13:24:36 -0400996int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
997{
Chris Mason2c64c532009-09-02 15:04:12 -0400998 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400999}
1000
Josef Bacik25179202008-10-29 14:49:05 -04001001int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1002 gfp_t mask)
1003{
1004 int err;
1005 u64 failed_start;
1006
Chris Mason2c64c532009-09-02 15:04:12 -04001007 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1008 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001009 if (err == -EEXIST) {
1010 if (failed_start > start)
1011 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001012 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001013 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001014 }
Josef Bacik25179202008-10-29 14:49:05 -04001015 return 1;
1016}
Josef Bacik25179202008-10-29 14:49:05 -04001017
Chris Mason2c64c532009-09-02 15:04:12 -04001018int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1019 struct extent_state **cached, gfp_t mask)
1020{
1021 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1022 mask);
1023}
1024
Arne Jansen507903b2011-04-06 10:02:20 +00001025int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001026{
Chris Mason2c64c532009-09-02 15:04:12 -04001027 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1028 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001029}
Chris Masond1310b22008-01-24 16:13:08 -05001030
1031/*
1032 * helper function to set pages and extents in the tree dirty
1033 */
1034int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1035{
1036 unsigned long index = start >> PAGE_CACHE_SHIFT;
1037 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1038 struct page *page;
1039
1040 while (index <= end_index) {
1041 page = find_get_page(tree->mapping, index);
1042 BUG_ON(!page);
1043 __set_page_dirty_nobuffers(page);
1044 page_cache_release(page);
1045 index++;
1046 }
Chris Masond1310b22008-01-24 16:13:08 -05001047 return 0;
1048}
Chris Masond1310b22008-01-24 16:13:08 -05001049
1050/*
1051 * helper function to set both pages and extents in the tree writeback
1052 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001053static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001054{
1055 unsigned long index = start >> PAGE_CACHE_SHIFT;
1056 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1057 struct page *page;
1058
1059 while (index <= end_index) {
1060 page = find_get_page(tree->mapping, index);
1061 BUG_ON(!page);
1062 set_page_writeback(page);
1063 page_cache_release(page);
1064 index++;
1065 }
Chris Masond1310b22008-01-24 16:13:08 -05001066 return 0;
1067}
Chris Masond1310b22008-01-24 16:13:08 -05001068
Chris Masond352ac62008-09-29 15:18:18 -04001069/*
1070 * find the first offset in the io tree with 'bits' set. zero is
1071 * returned if we find something, and *start_ret and *end_ret are
1072 * set to reflect the state struct that was found.
1073 *
1074 * If nothing was found, 1 is returned, < 0 on error
1075 */
Chris Masond1310b22008-01-24 16:13:08 -05001076int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1077 u64 *start_ret, u64 *end_ret, int bits)
1078{
1079 struct rb_node *node;
1080 struct extent_state *state;
1081 int ret = 1;
1082
Chris Masoncad321a2008-12-17 14:51:42 -05001083 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001084 /*
1085 * this search will find all the extents that end after
1086 * our range starts.
1087 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001088 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001089 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001090 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001091
Chris Masond3977122009-01-05 21:25:51 -05001092 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001093 state = rb_entry(node, struct extent_state, rb_node);
1094 if (state->end >= start && (state->state & bits)) {
1095 *start_ret = state->start;
1096 *end_ret = state->end;
1097 ret = 0;
1098 break;
1099 }
1100 node = rb_next(node);
1101 if (!node)
1102 break;
1103 }
1104out:
Chris Masoncad321a2008-12-17 14:51:42 -05001105 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001106 return ret;
1107}
Chris Masond1310b22008-01-24 16:13:08 -05001108
Chris Masond352ac62008-09-29 15:18:18 -04001109/* find the first state struct with 'bits' set after 'start', and
1110 * return it. tree->lock must be held. NULL will returned if
1111 * nothing was found after 'start'
1112 */
Chris Masond7fc6402008-02-18 12:12:38 -05001113struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1114 u64 start, int bits)
1115{
1116 struct rb_node *node;
1117 struct extent_state *state;
1118
1119 /*
1120 * this search will find all the extents that end after
1121 * our range starts.
1122 */
1123 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001124 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001125 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001126
Chris Masond3977122009-01-05 21:25:51 -05001127 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001128 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001129 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001130 return state;
Chris Masond3977122009-01-05 21:25:51 -05001131
Chris Masond7fc6402008-02-18 12:12:38 -05001132 node = rb_next(node);
1133 if (!node)
1134 break;
1135 }
1136out:
1137 return NULL;
1138}
Chris Masond7fc6402008-02-18 12:12:38 -05001139
Chris Masond352ac62008-09-29 15:18:18 -04001140/*
1141 * find a contiguous range of bytes in the file marked as delalloc, not
1142 * more than 'max_bytes'. start and end are used to return the range,
1143 *
1144 * 1 is returned if we find something, 0 if nothing was in the tree
1145 */
Chris Masonc8b97812008-10-29 14:49:59 -04001146static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001147 u64 *start, u64 *end, u64 max_bytes,
1148 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001149{
1150 struct rb_node *node;
1151 struct extent_state *state;
1152 u64 cur_start = *start;
1153 u64 found = 0;
1154 u64 total_bytes = 0;
1155
Chris Masoncad321a2008-12-17 14:51:42 -05001156 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001157
Chris Masond1310b22008-01-24 16:13:08 -05001158 /*
1159 * this search will find all the extents that end after
1160 * our range starts.
1161 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001162 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001163 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001164 if (!found)
1165 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001166 goto out;
1167 }
1168
Chris Masond3977122009-01-05 21:25:51 -05001169 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001170 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001171 if (found && (state->start != cur_start ||
1172 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001173 goto out;
1174 }
1175 if (!(state->state & EXTENT_DELALLOC)) {
1176 if (!found)
1177 *end = state->end;
1178 goto out;
1179 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001180 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001181 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001182 *cached_state = state;
1183 atomic_inc(&state->refs);
1184 }
Chris Masond1310b22008-01-24 16:13:08 -05001185 found++;
1186 *end = state->end;
1187 cur_start = state->end + 1;
1188 node = rb_next(node);
1189 if (!node)
1190 break;
1191 total_bytes += state->end - state->start + 1;
1192 if (total_bytes >= max_bytes)
1193 break;
1194 }
1195out:
Chris Masoncad321a2008-12-17 14:51:42 -05001196 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001197 return found;
1198}
1199
Chris Masonc8b97812008-10-29 14:49:59 -04001200static noinline int __unlock_for_delalloc(struct inode *inode,
1201 struct page *locked_page,
1202 u64 start, u64 end)
1203{
1204 int ret;
1205 struct page *pages[16];
1206 unsigned long index = start >> PAGE_CACHE_SHIFT;
1207 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1208 unsigned long nr_pages = end_index - index + 1;
1209 int i;
1210
1211 if (index == locked_page->index && end_index == index)
1212 return 0;
1213
Chris Masond3977122009-01-05 21:25:51 -05001214 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001215 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001216 min_t(unsigned long, nr_pages,
1217 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001218 for (i = 0; i < ret; i++) {
1219 if (pages[i] != locked_page)
1220 unlock_page(pages[i]);
1221 page_cache_release(pages[i]);
1222 }
1223 nr_pages -= ret;
1224 index += ret;
1225 cond_resched();
1226 }
1227 return 0;
1228}
1229
1230static noinline int lock_delalloc_pages(struct inode *inode,
1231 struct page *locked_page,
1232 u64 delalloc_start,
1233 u64 delalloc_end)
1234{
1235 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1236 unsigned long start_index = index;
1237 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1238 unsigned long pages_locked = 0;
1239 struct page *pages[16];
1240 unsigned long nrpages;
1241 int ret;
1242 int i;
1243
1244 /* the caller is responsible for locking the start index */
1245 if (index == locked_page->index && index == end_index)
1246 return 0;
1247
1248 /* skip the page at the start index */
1249 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001250 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001251 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001252 min_t(unsigned long,
1253 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001254 if (ret == 0) {
1255 ret = -EAGAIN;
1256 goto done;
1257 }
1258 /* now we have an array of pages, lock them all */
1259 for (i = 0; i < ret; i++) {
1260 /*
1261 * the caller is taking responsibility for
1262 * locked_page
1263 */
Chris Mason771ed682008-11-06 22:02:51 -05001264 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001265 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001266 if (!PageDirty(pages[i]) ||
1267 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001268 ret = -EAGAIN;
1269 unlock_page(pages[i]);
1270 page_cache_release(pages[i]);
1271 goto done;
1272 }
1273 }
Chris Masonc8b97812008-10-29 14:49:59 -04001274 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001275 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001276 }
Chris Masonc8b97812008-10-29 14:49:59 -04001277 nrpages -= ret;
1278 index += ret;
1279 cond_resched();
1280 }
1281 ret = 0;
1282done:
1283 if (ret && pages_locked) {
1284 __unlock_for_delalloc(inode, locked_page,
1285 delalloc_start,
1286 ((u64)(start_index + pages_locked - 1)) <<
1287 PAGE_CACHE_SHIFT);
1288 }
1289 return ret;
1290}
1291
1292/*
1293 * find a contiguous range of bytes in the file marked as delalloc, not
1294 * more than 'max_bytes'. start and end are used to return the range,
1295 *
1296 * 1 is returned if we find something, 0 if nothing was in the tree
1297 */
1298static noinline u64 find_lock_delalloc_range(struct inode *inode,
1299 struct extent_io_tree *tree,
1300 struct page *locked_page,
1301 u64 *start, u64 *end,
1302 u64 max_bytes)
1303{
1304 u64 delalloc_start;
1305 u64 delalloc_end;
1306 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001307 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001308 int ret;
1309 int loops = 0;
1310
1311again:
1312 /* step one, find a bunch of delalloc bytes starting at start */
1313 delalloc_start = *start;
1314 delalloc_end = 0;
1315 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001316 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001317 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001318 *start = delalloc_start;
1319 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001320 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001321 return found;
1322 }
1323
1324 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001325 * start comes from the offset of locked_page. We have to lock
1326 * pages in order, so we can't process delalloc bytes before
1327 * locked_page
1328 */
Chris Masond3977122009-01-05 21:25:51 -05001329 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001330 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001331
1332 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001333 * make sure to limit the number of pages we try to lock down
1334 * if we're looping.
1335 */
Chris Masond3977122009-01-05 21:25:51 -05001336 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001337 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001338
Chris Masonc8b97812008-10-29 14:49:59 -04001339 /* step two, lock all the pages after the page that has start */
1340 ret = lock_delalloc_pages(inode, locked_page,
1341 delalloc_start, delalloc_end);
1342 if (ret == -EAGAIN) {
1343 /* some of the pages are gone, lets avoid looping by
1344 * shortening the size of the delalloc range we're searching
1345 */
Chris Mason9655d292009-09-02 15:22:30 -04001346 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001347 if (!loops) {
1348 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1349 max_bytes = PAGE_CACHE_SIZE - offset;
1350 loops = 1;
1351 goto again;
1352 } else {
1353 found = 0;
1354 goto out_failed;
1355 }
1356 }
1357 BUG_ON(ret);
1358
1359 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001360 lock_extent_bits(tree, delalloc_start, delalloc_end,
1361 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001362
1363 /* then test to make sure it is all still delalloc */
1364 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001365 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001366 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001367 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1368 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001369 __unlock_for_delalloc(inode, locked_page,
1370 delalloc_start, delalloc_end);
1371 cond_resched();
1372 goto again;
1373 }
Chris Mason9655d292009-09-02 15:22:30 -04001374 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001375 *start = delalloc_start;
1376 *end = delalloc_end;
1377out_failed:
1378 return found;
1379}
1380
1381int extent_clear_unlock_delalloc(struct inode *inode,
1382 struct extent_io_tree *tree,
1383 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001384 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001385{
1386 int ret;
1387 struct page *pages[16];
1388 unsigned long index = start >> PAGE_CACHE_SHIFT;
1389 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1390 unsigned long nr_pages = end_index - index + 1;
1391 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001392 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001393
Chris Masona791e352009-10-08 11:27:10 -04001394 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001395 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001396 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001397 clear_bits |= EXTENT_DIRTY;
1398
Chris Masona791e352009-10-08 11:27:10 -04001399 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001400 clear_bits |= EXTENT_DELALLOC;
1401
Chris Mason2c64c532009-09-02 15:04:12 -04001402 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001403 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1404 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1405 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001406 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001407
Chris Masond3977122009-01-05 21:25:51 -05001408 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001409 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001410 min_t(unsigned long,
1411 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001412 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001413
Chris Masona791e352009-10-08 11:27:10 -04001414 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001415 SetPagePrivate2(pages[i]);
1416
Chris Masonc8b97812008-10-29 14:49:59 -04001417 if (pages[i] == locked_page) {
1418 page_cache_release(pages[i]);
1419 continue;
1420 }
Chris Masona791e352009-10-08 11:27:10 -04001421 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001422 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001423 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001424 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001425 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001426 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001427 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001428 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001429 page_cache_release(pages[i]);
1430 }
1431 nr_pages -= ret;
1432 index += ret;
1433 cond_resched();
1434 }
1435 return 0;
1436}
Chris Masonc8b97812008-10-29 14:49:59 -04001437
Chris Masond352ac62008-09-29 15:18:18 -04001438/*
1439 * count the number of bytes in the tree that have a given bit(s)
1440 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1441 * cached. The total number found is returned.
1442 */
Chris Masond1310b22008-01-24 16:13:08 -05001443u64 count_range_bits(struct extent_io_tree *tree,
1444 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001445 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001446{
1447 struct rb_node *node;
1448 struct extent_state *state;
1449 u64 cur_start = *start;
1450 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001451 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001452 int found = 0;
1453
1454 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001455 WARN_ON(1);
1456 return 0;
1457 }
1458
Chris Masoncad321a2008-12-17 14:51:42 -05001459 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001460 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1461 total_bytes = tree->dirty_bytes;
1462 goto out;
1463 }
1464 /*
1465 * this search will find all the extents that end after
1466 * our range starts.
1467 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001468 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001469 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001470 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001471
Chris Masond3977122009-01-05 21:25:51 -05001472 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001473 state = rb_entry(node, struct extent_state, rb_node);
1474 if (state->start > search_end)
1475 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001476 if (contig && found && state->start > last + 1)
1477 break;
1478 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001479 total_bytes += min(search_end, state->end) + 1 -
1480 max(cur_start, state->start);
1481 if (total_bytes >= max_bytes)
1482 break;
1483 if (!found) {
1484 *start = state->start;
1485 found = 1;
1486 }
Chris Masonec29ed52011-02-23 16:23:20 -05001487 last = state->end;
1488 } else if (contig && found) {
1489 break;
Chris Masond1310b22008-01-24 16:13:08 -05001490 }
1491 node = rb_next(node);
1492 if (!node)
1493 break;
1494 }
1495out:
Chris Masoncad321a2008-12-17 14:51:42 -05001496 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001497 return total_bytes;
1498}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001499
Chris Masond352ac62008-09-29 15:18:18 -04001500/*
1501 * set the private field for a given byte offset in the tree. If there isn't
1502 * an extent_state there already, this does nothing.
1503 */
Chris Masond1310b22008-01-24 16:13:08 -05001504int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1505{
1506 struct rb_node *node;
1507 struct extent_state *state;
1508 int ret = 0;
1509
Chris Masoncad321a2008-12-17 14:51:42 -05001510 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001511 /*
1512 * this search will find all the extents that end after
1513 * our range starts.
1514 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001515 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001516 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001517 ret = -ENOENT;
1518 goto out;
1519 }
1520 state = rb_entry(node, struct extent_state, rb_node);
1521 if (state->start != start) {
1522 ret = -ENOENT;
1523 goto out;
1524 }
1525 state->private = private;
1526out:
Chris Masoncad321a2008-12-17 14:51:42 -05001527 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001528 return ret;
1529}
1530
1531int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1532{
1533 struct rb_node *node;
1534 struct extent_state *state;
1535 int ret = 0;
1536
Chris Masoncad321a2008-12-17 14:51:42 -05001537 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001538 /*
1539 * this search will find all the extents that end after
1540 * our range starts.
1541 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001542 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001543 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001544 ret = -ENOENT;
1545 goto out;
1546 }
1547 state = rb_entry(node, struct extent_state, rb_node);
1548 if (state->start != start) {
1549 ret = -ENOENT;
1550 goto out;
1551 }
1552 *private = state->private;
1553out:
Chris Masoncad321a2008-12-17 14:51:42 -05001554 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001555 return ret;
1556}
1557
1558/*
1559 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001560 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001561 * has the bits set. Otherwise, 1 is returned if any bit in the
1562 * range is found set.
1563 */
1564int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001565 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001566{
1567 struct extent_state *state = NULL;
1568 struct rb_node *node;
1569 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001570
Chris Masoncad321a2008-12-17 14:51:42 -05001571 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001572 if (cached && cached->tree && cached->start == start)
1573 node = &cached->rb_node;
1574 else
1575 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001576 while (node && start <= end) {
1577 state = rb_entry(node, struct extent_state, rb_node);
1578
1579 if (filled && state->start > start) {
1580 bitset = 0;
1581 break;
1582 }
1583
1584 if (state->start > end)
1585 break;
1586
1587 if (state->state & bits) {
1588 bitset = 1;
1589 if (!filled)
1590 break;
1591 } else if (filled) {
1592 bitset = 0;
1593 break;
1594 }
Chris Mason46562cec2009-09-23 20:23:16 -04001595
1596 if (state->end == (u64)-1)
1597 break;
1598
Chris Masond1310b22008-01-24 16:13:08 -05001599 start = state->end + 1;
1600 if (start > end)
1601 break;
1602 node = rb_next(node);
1603 if (!node) {
1604 if (filled)
1605 bitset = 0;
1606 break;
1607 }
1608 }
Chris Masoncad321a2008-12-17 14:51:42 -05001609 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001610 return bitset;
1611}
Chris Masond1310b22008-01-24 16:13:08 -05001612
1613/*
1614 * helper function to set a given page up to date if all the
1615 * extents in the tree for that page are up to date
1616 */
1617static int check_page_uptodate(struct extent_io_tree *tree,
1618 struct page *page)
1619{
1620 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1621 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001622 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001623 SetPageUptodate(page);
1624 return 0;
1625}
1626
1627/*
1628 * helper function to unlock a page if all the extents in the tree
1629 * for that page are unlocked
1630 */
1631static int check_page_locked(struct extent_io_tree *tree,
1632 struct page *page)
1633{
1634 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1635 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001636 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001637 unlock_page(page);
1638 return 0;
1639}
1640
1641/*
1642 * helper function to end page writeback if all the extents
1643 * in the tree for that page are done with writeback
1644 */
1645static int check_page_writeback(struct extent_io_tree *tree,
1646 struct page *page)
1647{
Chris Mason1edbb732009-09-02 13:24:36 -04001648 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001649 return 0;
1650}
1651
1652/* lots and lots of room for performance fixes in the end_bio funcs */
1653
1654/*
1655 * after a writepage IO is done, we need to:
1656 * clear the uptodate bits on error
1657 * clear the writeback bits in the extent tree for this IO
1658 * end_page_writeback if the page has no more pending IO
1659 *
1660 * Scheduling is not allowed, so the extent state tree is expected
1661 * to have one and only one object corresponding to this IO.
1662 */
Chris Masond1310b22008-01-24 16:13:08 -05001663static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001664{
Chris Mason1259ab72008-05-12 13:39:03 -04001665 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001666 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001667 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001668 u64 start;
1669 u64 end;
1670 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001671 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001672
Chris Masond1310b22008-01-24 16:13:08 -05001673 do {
1674 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001675 tree = &BTRFS_I(page->mapping->host)->io_tree;
1676
Chris Masond1310b22008-01-24 16:13:08 -05001677 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1678 bvec->bv_offset;
1679 end = start + bvec->bv_len - 1;
1680
1681 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1682 whole_page = 1;
1683 else
1684 whole_page = 0;
1685
1686 if (--bvec >= bio->bi_io_vec)
1687 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001688 if (tree->ops && tree->ops->writepage_end_io_hook) {
1689 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001690 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001691 if (ret)
1692 uptodate = 0;
1693 }
1694
1695 if (!uptodate && tree->ops &&
1696 tree->ops->writepage_io_failed_hook) {
1697 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001698 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001699 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001700 uptodate = (err == 0);
1701 continue;
1702 }
1703 }
1704
Chris Masond1310b22008-01-24 16:13:08 -05001705 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001706 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001707 ClearPageUptodate(page);
1708 SetPageError(page);
1709 }
Chris Mason70dec802008-01-29 09:59:12 -05001710
Chris Masond1310b22008-01-24 16:13:08 -05001711 if (whole_page)
1712 end_page_writeback(page);
1713 else
1714 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001715 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001716
Chris Masond1310b22008-01-24 16:13:08 -05001717 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001718}
1719
1720/*
1721 * after a readpage IO is done, we need to:
1722 * clear the uptodate bits on error
1723 * set the uptodate bits if things worked
1724 * set the page up to date if all extents in the tree are uptodate
1725 * clear the lock bit in the extent tree
1726 * unlock the page if there are no other extents locked for it
1727 *
1728 * Scheduling is not allowed, so the extent state tree is expected
1729 * to have one and only one object corresponding to this IO.
1730 */
Chris Masond1310b22008-01-24 16:13:08 -05001731static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001732{
1733 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001734 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1735 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001736 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001737 u64 start;
1738 u64 end;
1739 int whole_page;
1740 int ret;
1741
Chris Masond20f7042008-12-08 16:58:54 -05001742 if (err)
1743 uptodate = 0;
1744
Chris Masond1310b22008-01-24 16:13:08 -05001745 do {
1746 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001747 struct extent_state *cached = NULL;
1748 struct extent_state *state;
1749
David Woodhouse902b22f2008-08-20 08:51:49 -04001750 tree = &BTRFS_I(page->mapping->host)->io_tree;
1751
Chris Masond1310b22008-01-24 16:13:08 -05001752 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1753 bvec->bv_offset;
1754 end = start + bvec->bv_len - 1;
1755
1756 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1757 whole_page = 1;
1758 else
1759 whole_page = 0;
1760
Chris Mason4125bf72010-02-03 18:18:45 +00001761 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001762 prefetchw(&bvec->bv_page->flags);
1763
Arne Jansen507903b2011-04-06 10:02:20 +00001764 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001765 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001766 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001767 /*
1768 * take a reference on the state, unlock will drop
1769 * the ref
1770 */
1771 cache_state(state, &cached);
1772 }
1773 spin_unlock(&tree->lock);
1774
Chris Masond1310b22008-01-24 16:13:08 -05001775 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001776 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001777 state);
Chris Masond1310b22008-01-24 16:13:08 -05001778 if (ret)
1779 uptodate = 0;
1780 }
Chris Mason7e383262008-04-09 16:28:12 -04001781 if (!uptodate && tree->ops &&
1782 tree->ops->readpage_io_failed_hook) {
1783 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001784 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001785 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001786 uptodate =
1787 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001788 if (err)
1789 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001790 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001791 continue;
1792 }
1793 }
Chris Mason70dec802008-01-29 09:59:12 -05001794
Chris Mason771ed682008-11-06 22:02:51 -05001795 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001796 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001797 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001798 }
Arne Jansen507903b2011-04-06 10:02:20 +00001799 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001800
Chris Mason70dec802008-01-29 09:59:12 -05001801 if (whole_page) {
1802 if (uptodate) {
1803 SetPageUptodate(page);
1804 } else {
1805 ClearPageUptodate(page);
1806 SetPageError(page);
1807 }
Chris Masond1310b22008-01-24 16:13:08 -05001808 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001809 } else {
1810 if (uptodate) {
1811 check_page_uptodate(tree, page);
1812 } else {
1813 ClearPageUptodate(page);
1814 SetPageError(page);
1815 }
Chris Masond1310b22008-01-24 16:13:08 -05001816 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001817 }
Chris Mason4125bf72010-02-03 18:18:45 +00001818 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001819
1820 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001821}
1822
1823/*
1824 * IO done from prepare_write is pretty simple, we just unlock
1825 * the structs in the extent tree when done, and set the uptodate bits
1826 * as appropriate.
1827 */
Chris Masond1310b22008-01-24 16:13:08 -05001828static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001829{
1830 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1831 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001832 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001833 u64 start;
1834 u64 end;
1835
Chris Masond1310b22008-01-24 16:13:08 -05001836 do {
1837 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001838 struct extent_state *cached = NULL;
David Woodhouse902b22f2008-08-20 08:51:49 -04001839 tree = &BTRFS_I(page->mapping->host)->io_tree;
1840
Chris Masond1310b22008-01-24 16:13:08 -05001841 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1842 bvec->bv_offset;
1843 end = start + bvec->bv_len - 1;
1844
1845 if (--bvec >= bio->bi_io_vec)
1846 prefetchw(&bvec->bv_page->flags);
1847
1848 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001849 set_extent_uptodate(tree, start, end, &cached,
1850 GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001851 } else {
1852 ClearPageUptodate(page);
1853 SetPageError(page);
1854 }
1855
Arne Jansen507903b2011-04-06 10:02:20 +00001856 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001857
1858 } while (bvec >= bio->bi_io_vec);
1859
1860 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001861}
1862
Miao Xie88f794e2010-11-22 03:02:55 +00001863struct bio *
1864btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1865 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001866{
1867 struct bio *bio;
1868
1869 bio = bio_alloc(gfp_flags, nr_vecs);
1870
1871 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1872 while (!bio && (nr_vecs /= 2))
1873 bio = bio_alloc(gfp_flags, nr_vecs);
1874 }
1875
1876 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001877 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001878 bio->bi_bdev = bdev;
1879 bio->bi_sector = first_sector;
1880 }
1881 return bio;
1882}
1883
Chris Masonc8b97812008-10-29 14:49:59 -04001884static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1885 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001886{
Chris Masond1310b22008-01-24 16:13:08 -05001887 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001888 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1889 struct page *page = bvec->bv_page;
1890 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001891 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001892
1893 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001894
David Woodhouse902b22f2008-08-20 08:51:49 -04001895 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001896
1897 bio_get(bio);
1898
Chris Mason065631f2008-02-20 12:07:25 -05001899 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001900 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001901 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001902 else
1903 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001904 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1905 ret = -EOPNOTSUPP;
1906 bio_put(bio);
1907 return ret;
1908}
1909
1910static int submit_extent_page(int rw, struct extent_io_tree *tree,
1911 struct page *page, sector_t sector,
1912 size_t size, unsigned long offset,
1913 struct block_device *bdev,
1914 struct bio **bio_ret,
1915 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001916 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001917 int mirror_num,
1918 unsigned long prev_bio_flags,
1919 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001920{
1921 int ret = 0;
1922 struct bio *bio;
1923 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001924 int contig = 0;
1925 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1926 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001927 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001928
1929 if (bio_ret && *bio_ret) {
1930 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001931 if (old_compressed)
1932 contig = bio->bi_sector == sector;
1933 else
1934 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1935 sector;
1936
1937 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001938 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001939 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1940 bio_flags)) ||
1941 bio_add_page(bio, page, page_size, offset) < page_size) {
1942 ret = submit_one_bio(rw, bio, mirror_num,
1943 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001944 bio = NULL;
1945 } else {
1946 return 0;
1947 }
1948 }
Chris Masonc8b97812008-10-29 14:49:59 -04001949 if (this_compressed)
1950 nr = BIO_MAX_PAGES;
1951 else
1952 nr = bio_get_nr_vecs(bdev);
1953
Miao Xie88f794e2010-11-22 03:02:55 +00001954 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001955 if (!bio)
1956 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001957
Chris Masonc8b97812008-10-29 14:49:59 -04001958 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001959 bio->bi_end_io = end_io_func;
1960 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001961
Chris Masond3977122009-01-05 21:25:51 -05001962 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001963 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001964 else
Chris Masonc8b97812008-10-29 14:49:59 -04001965 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001966
1967 return ret;
1968}
1969
1970void set_page_extent_mapped(struct page *page)
1971{
1972 if (!PagePrivate(page)) {
1973 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001974 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001975 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001976 }
1977}
1978
Christoph Hellwigb2950862008-12-02 09:54:17 -05001979static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001980{
Chris Masoneb14ab82011-02-10 12:35:00 -05001981 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05001982 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1983}
1984
1985/*
1986 * basic readpage implementation. Locked extent state structs are inserted
1987 * into the tree that are removed when the IO is done (by the end_io
1988 * handlers)
1989 */
1990static int __extent_read_full_page(struct extent_io_tree *tree,
1991 struct page *page,
1992 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001993 struct bio **bio, int mirror_num,
1994 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001995{
1996 struct inode *inode = page->mapping->host;
1997 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1998 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1999 u64 end;
2000 u64 cur = start;
2001 u64 extent_offset;
2002 u64 last_byte = i_size_read(inode);
2003 u64 block_start;
2004 u64 cur_end;
2005 sector_t sector;
2006 struct extent_map *em;
2007 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002008 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002009 int ret;
2010 int nr = 0;
2011 size_t page_offset = 0;
2012 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002013 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002014 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002015 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002016
2017 set_page_extent_mapped(page);
2018
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002019 if (!PageUptodate(page)) {
2020 if (cleancache_get_page(page) == 0) {
2021 BUG_ON(blocksize != PAGE_SIZE);
2022 goto out;
2023 }
2024 }
2025
Chris Masond1310b22008-01-24 16:13:08 -05002026 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002027 while (1) {
2028 lock_extent(tree, start, end, GFP_NOFS);
2029 ordered = btrfs_lookup_ordered_extent(inode, start);
2030 if (!ordered)
2031 break;
2032 unlock_extent(tree, start, end, GFP_NOFS);
2033 btrfs_start_ordered_extent(inode, ordered, 1);
2034 btrfs_put_ordered_extent(ordered);
2035 }
Chris Masond1310b22008-01-24 16:13:08 -05002036
Chris Masonc8b97812008-10-29 14:49:59 -04002037 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2038 char *userpage;
2039 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2040
2041 if (zero_offset) {
2042 iosize = PAGE_CACHE_SIZE - zero_offset;
2043 userpage = kmap_atomic(page, KM_USER0);
2044 memset(userpage + zero_offset, 0, iosize);
2045 flush_dcache_page(page);
2046 kunmap_atomic(userpage, KM_USER0);
2047 }
2048 }
Chris Masond1310b22008-01-24 16:13:08 -05002049 while (cur <= end) {
2050 if (cur >= last_byte) {
2051 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002052 struct extent_state *cached = NULL;
2053
Chris Masond1310b22008-01-24 16:13:08 -05002054 iosize = PAGE_CACHE_SIZE - page_offset;
2055 userpage = kmap_atomic(page, KM_USER0);
2056 memset(userpage + page_offset, 0, iosize);
2057 flush_dcache_page(page);
2058 kunmap_atomic(userpage, KM_USER0);
2059 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002060 &cached, GFP_NOFS);
2061 unlock_extent_cached(tree, cur, cur + iosize - 1,
2062 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002063 break;
2064 }
2065 em = get_extent(inode, page, page_offset, cur,
2066 end - cur + 1, 0);
2067 if (IS_ERR(em) || !em) {
2068 SetPageError(page);
2069 unlock_extent(tree, cur, end, GFP_NOFS);
2070 break;
2071 }
Chris Masond1310b22008-01-24 16:13:08 -05002072 extent_offset = cur - em->start;
2073 BUG_ON(extent_map_end(em) <= cur);
2074 BUG_ON(end < cur);
2075
Li Zefan261507a02010-12-17 14:21:50 +08002076 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002077 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002078 extent_set_compress_type(&this_bio_flag,
2079 em->compress_type);
2080 }
Chris Masonc8b97812008-10-29 14:49:59 -04002081
Chris Masond1310b22008-01-24 16:13:08 -05002082 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2083 cur_end = min(extent_map_end(em) - 1, end);
2084 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002085 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2086 disk_io_size = em->block_len;
2087 sector = em->block_start >> 9;
2088 } else {
2089 sector = (em->block_start + extent_offset) >> 9;
2090 disk_io_size = iosize;
2091 }
Chris Masond1310b22008-01-24 16:13:08 -05002092 bdev = em->bdev;
2093 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002094 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2095 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002096 free_extent_map(em);
2097 em = NULL;
2098
2099 /* we've found a hole, just zero and go on */
2100 if (block_start == EXTENT_MAP_HOLE) {
2101 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002102 struct extent_state *cached = NULL;
2103
Chris Masond1310b22008-01-24 16:13:08 -05002104 userpage = kmap_atomic(page, KM_USER0);
2105 memset(userpage + page_offset, 0, iosize);
2106 flush_dcache_page(page);
2107 kunmap_atomic(userpage, KM_USER0);
2108
2109 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002110 &cached, GFP_NOFS);
2111 unlock_extent_cached(tree, cur, cur + iosize - 1,
2112 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002113 cur = cur + iosize;
2114 page_offset += iosize;
2115 continue;
2116 }
2117 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002118 if (test_range_bit(tree, cur, cur_end,
2119 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002120 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002121 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2122 cur = cur + iosize;
2123 page_offset += iosize;
2124 continue;
2125 }
Chris Mason70dec802008-01-29 09:59:12 -05002126 /* we have an inline extent but it didn't get marked up
2127 * to date. Error out
2128 */
2129 if (block_start == EXTENT_MAP_INLINE) {
2130 SetPageError(page);
2131 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2132 cur = cur + iosize;
2133 page_offset += iosize;
2134 continue;
2135 }
Chris Masond1310b22008-01-24 16:13:08 -05002136
2137 ret = 0;
2138 if (tree->ops && tree->ops->readpage_io_hook) {
2139 ret = tree->ops->readpage_io_hook(page, cur,
2140 cur + iosize - 1);
2141 }
2142 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002143 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2144 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002145 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002146 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002147 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002148 end_bio_extent_readpage, mirror_num,
2149 *bio_flags,
2150 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002151 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002152 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002153 }
2154 if (ret)
2155 SetPageError(page);
2156 cur = cur + iosize;
2157 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002158 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002159out:
Chris Masond1310b22008-01-24 16:13:08 -05002160 if (!nr) {
2161 if (!PageError(page))
2162 SetPageUptodate(page);
2163 unlock_page(page);
2164 }
2165 return 0;
2166}
2167
2168int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2169 get_extent_t *get_extent)
2170{
2171 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002172 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002173 int ret;
2174
Chris Masonc8b97812008-10-29 14:49:59 -04002175 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2176 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002177 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002178 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002179 return ret;
2180}
Chris Masond1310b22008-01-24 16:13:08 -05002181
Chris Mason11c83492009-04-20 15:50:09 -04002182static noinline void update_nr_written(struct page *page,
2183 struct writeback_control *wbc,
2184 unsigned long nr_written)
2185{
2186 wbc->nr_to_write -= nr_written;
2187 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2188 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2189 page->mapping->writeback_index = page->index + nr_written;
2190}
2191
Chris Masond1310b22008-01-24 16:13:08 -05002192/*
2193 * the writepage semantics are similar to regular writepage. extent
2194 * records are inserted to lock ranges in the tree, and as dirty areas
2195 * are found, they are marked writeback. Then the lock bits are removed
2196 * and the end_io handler clears the writeback ranges
2197 */
2198static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2199 void *data)
2200{
2201 struct inode *inode = page->mapping->host;
2202 struct extent_page_data *epd = data;
2203 struct extent_io_tree *tree = epd->tree;
2204 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2205 u64 delalloc_start;
2206 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2207 u64 end;
2208 u64 cur = start;
2209 u64 extent_offset;
2210 u64 last_byte = i_size_read(inode);
2211 u64 block_start;
2212 u64 iosize;
2213 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002214 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002215 struct extent_map *em;
2216 struct block_device *bdev;
2217 int ret;
2218 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002219 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002220 size_t blocksize;
2221 loff_t i_size = i_size_read(inode);
2222 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2223 u64 nr_delalloc;
2224 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002225 int page_started;
2226 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002227 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002228 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002229
Chris Masonffbd5172009-04-20 15:50:09 -04002230 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002231 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002232 else
2233 write_flags = WRITE;
2234
liubo1abe9b82011-03-24 11:18:59 +00002235 trace___extent_writepage(page, inode, wbc);
2236
Chris Masond1310b22008-01-24 16:13:08 -05002237 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002238 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002239 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002240 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002241 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002242 unlock_page(page);
2243 return 0;
2244 }
2245
2246 if (page->index == end_index) {
2247 char *userpage;
2248
Chris Masond1310b22008-01-24 16:13:08 -05002249 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002250 memset(userpage + pg_offset, 0,
2251 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002252 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002253 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002254 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002255 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002256
2257 set_page_extent_mapped(page);
2258
2259 delalloc_start = start;
2260 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002261 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002262 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002263 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002264 /*
2265 * make sure the wbc mapping index is at least updated
2266 * to this page.
2267 */
2268 update_nr_written(page, wbc, 0);
2269
Chris Masond3977122009-01-05 21:25:51 -05002270 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002271 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002272 page,
2273 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002274 &delalloc_end,
2275 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002276 if (nr_delalloc == 0) {
2277 delalloc_start = delalloc_end + 1;
2278 continue;
2279 }
2280 tree->ops->fill_delalloc(inode, page, delalloc_start,
2281 delalloc_end, &page_started,
2282 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002283 /*
2284 * delalloc_end is already one less than the total
2285 * length, so we don't subtract one from
2286 * PAGE_CACHE_SIZE
2287 */
2288 delalloc_to_write += (delalloc_end - delalloc_start +
2289 PAGE_CACHE_SIZE) >>
2290 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002291 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002292 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002293 if (wbc->nr_to_write < delalloc_to_write) {
2294 int thresh = 8192;
2295
2296 if (delalloc_to_write < thresh * 2)
2297 thresh = delalloc_to_write;
2298 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2299 thresh);
2300 }
Chris Masonc8b97812008-10-29 14:49:59 -04002301
Chris Mason771ed682008-11-06 22:02:51 -05002302 /* did the fill delalloc function already unlock and start
2303 * the IO?
2304 */
2305 if (page_started) {
2306 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002307 /*
2308 * we've unlocked the page, so we can't update
2309 * the mapping's writeback index, just update
2310 * nr_to_write.
2311 */
2312 wbc->nr_to_write -= nr_written;
2313 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002314 }
Chris Masonc8b97812008-10-29 14:49:59 -04002315 }
Chris Mason247e7432008-07-17 12:53:51 -04002316 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002317 ret = tree->ops->writepage_start_hook(page, start,
2318 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002319 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002320 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002321 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002322 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002323 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002324 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002325 }
2326 }
2327
Chris Mason11c83492009-04-20 15:50:09 -04002328 /*
2329 * we don't want to touch the inode after unlocking the page,
2330 * so we update the mapping writeback index now
2331 */
2332 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002333
Chris Masond1310b22008-01-24 16:13:08 -05002334 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002335 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002336 if (tree->ops && tree->ops->writepage_end_io_hook)
2337 tree->ops->writepage_end_io_hook(page, start,
2338 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002339 goto done;
2340 }
2341
Chris Masond1310b22008-01-24 16:13:08 -05002342 blocksize = inode->i_sb->s_blocksize;
2343
2344 while (cur <= end) {
2345 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002346 if (tree->ops && tree->ops->writepage_end_io_hook)
2347 tree->ops->writepage_end_io_hook(page, cur,
2348 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002349 break;
2350 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002351 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002352 end - cur + 1, 1);
2353 if (IS_ERR(em) || !em) {
2354 SetPageError(page);
2355 break;
2356 }
2357
2358 extent_offset = cur - em->start;
2359 BUG_ON(extent_map_end(em) <= cur);
2360 BUG_ON(end < cur);
2361 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2362 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2363 sector = (em->block_start + extent_offset) >> 9;
2364 bdev = em->bdev;
2365 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002366 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002367 free_extent_map(em);
2368 em = NULL;
2369
Chris Masonc8b97812008-10-29 14:49:59 -04002370 /*
2371 * compressed and inline extents are written through other
2372 * paths in the FS
2373 */
2374 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002375 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002376 /*
2377 * end_io notification does not happen here for
2378 * compressed extents
2379 */
2380 if (!compressed && tree->ops &&
2381 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002382 tree->ops->writepage_end_io_hook(page, cur,
2383 cur + iosize - 1,
2384 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002385 else if (compressed) {
2386 /* we don't want to end_page_writeback on
2387 * a compressed extent. this happens
2388 * elsewhere
2389 */
2390 nr++;
2391 }
2392
2393 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002394 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002395 continue;
2396 }
Chris Masond1310b22008-01-24 16:13:08 -05002397 /* leave this out until we have a page_mkwrite call */
2398 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002399 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002400 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002401 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002402 continue;
2403 }
Chris Masonc8b97812008-10-29 14:49:59 -04002404
Chris Masond1310b22008-01-24 16:13:08 -05002405 if (tree->ops && tree->ops->writepage_io_hook) {
2406 ret = tree->ops->writepage_io_hook(page, cur,
2407 cur + iosize - 1);
2408 } else {
2409 ret = 0;
2410 }
Chris Mason1259ab72008-05-12 13:39:03 -04002411 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002412 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002413 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002414 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002415
Chris Masond1310b22008-01-24 16:13:08 -05002416 set_range_writeback(tree, cur, cur + iosize - 1);
2417 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002418 printk(KERN_ERR "btrfs warning page %lu not "
2419 "writeback, cur %llu end %llu\n",
2420 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002421 (unsigned long long)end);
2422 }
2423
Chris Masonffbd5172009-04-20 15:50:09 -04002424 ret = submit_extent_page(write_flags, tree, page,
2425 sector, iosize, pg_offset,
2426 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002427 end_bio_extent_writepage,
2428 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002429 if (ret)
2430 SetPageError(page);
2431 }
2432 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002433 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002434 nr++;
2435 }
2436done:
2437 if (nr == 0) {
2438 /* make sure the mapping tag for page dirty gets cleared */
2439 set_page_writeback(page);
2440 end_page_writeback(page);
2441 }
Chris Masond1310b22008-01-24 16:13:08 -05002442 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002443
Chris Mason11c83492009-04-20 15:50:09 -04002444done_unlocked:
2445
Chris Mason2c64c532009-09-02 15:04:12 -04002446 /* drop our reference on any cached states */
2447 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002448 return 0;
2449}
2450
Chris Masond1310b22008-01-24 16:13:08 -05002451/**
Chris Mason4bef0842008-09-08 11:18:08 -04002452 * 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 -05002453 * @mapping: address space structure to write
2454 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2455 * @writepage: function called for each page
2456 * @data: data passed to writepage function
2457 *
2458 * If a page is already under I/O, write_cache_pages() skips it, even
2459 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2460 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2461 * and msync() need to guarantee that all the data which was dirty at the time
2462 * the call was made get new I/O started against them. If wbc->sync_mode is
2463 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2464 * existing IO to complete.
2465 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002466static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002467 struct address_space *mapping,
2468 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002469 writepage_t writepage, void *data,
2470 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002471{
Chris Masond1310b22008-01-24 16:13:08 -05002472 int ret = 0;
2473 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002474 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002475 struct pagevec pvec;
2476 int nr_pages;
2477 pgoff_t index;
2478 pgoff_t end; /* Inclusive */
2479 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002480
Chris Masond1310b22008-01-24 16:13:08 -05002481 pagevec_init(&pvec, 0);
2482 if (wbc->range_cyclic) {
2483 index = mapping->writeback_index; /* Start from prev offset */
2484 end = -1;
2485 } else {
2486 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2487 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002488 scanned = 1;
2489 }
2490retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002491 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002492 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002493 PAGECACHE_TAG_DIRTY, min(end - index,
2494 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002495 unsigned i;
2496
2497 scanned = 1;
2498 for (i = 0; i < nr_pages; i++) {
2499 struct page *page = pvec.pages[i];
2500
2501 /*
2502 * At this point we hold neither mapping->tree_lock nor
2503 * lock on the page itself: the page may be truncated or
2504 * invalidated (changing page->mapping to NULL), or even
2505 * swizzled back from swapper_space to tmpfs file
2506 * mapping
2507 */
Chris Mason4bef0842008-09-08 11:18:08 -04002508 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2509 tree->ops->write_cache_pages_lock_hook(page);
2510 else
2511 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002512
2513 if (unlikely(page->mapping != mapping)) {
2514 unlock_page(page);
2515 continue;
2516 }
2517
2518 if (!wbc->range_cyclic && page->index > end) {
2519 done = 1;
2520 unlock_page(page);
2521 continue;
2522 }
2523
Chris Masond2c3f4f2008-11-19 12:44:22 -05002524 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002525 if (PageWriteback(page))
2526 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002527 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002528 }
Chris Masond1310b22008-01-24 16:13:08 -05002529
2530 if (PageWriteback(page) ||
2531 !clear_page_dirty_for_io(page)) {
2532 unlock_page(page);
2533 continue;
2534 }
2535
2536 ret = (*writepage)(page, wbc, data);
2537
2538 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2539 unlock_page(page);
2540 ret = 0;
2541 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002542 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002543 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002544
2545 /*
2546 * the filesystem may choose to bump up nr_to_write.
2547 * We have to make sure to honor the new nr_to_write
2548 * at any time
2549 */
2550 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002551 }
2552 pagevec_release(&pvec);
2553 cond_resched();
2554 }
2555 if (!scanned && !done) {
2556 /*
2557 * We hit the last page and there is more work to be done: wrap
2558 * back to the start of the file
2559 */
2560 scanned = 1;
2561 index = 0;
2562 goto retry;
2563 }
Chris Masond1310b22008-01-24 16:13:08 -05002564 return ret;
2565}
Chris Masond1310b22008-01-24 16:13:08 -05002566
Chris Masonffbd5172009-04-20 15:50:09 -04002567static void flush_epd_write_bio(struct extent_page_data *epd)
2568{
2569 if (epd->bio) {
2570 if (epd->sync_io)
2571 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2572 else
2573 submit_one_bio(WRITE, epd->bio, 0, 0);
2574 epd->bio = NULL;
2575 }
2576}
2577
Chris Masond2c3f4f2008-11-19 12:44:22 -05002578static noinline void flush_write_bio(void *data)
2579{
2580 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002581 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002582}
2583
Chris Masond1310b22008-01-24 16:13:08 -05002584int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2585 get_extent_t *get_extent,
2586 struct writeback_control *wbc)
2587{
2588 int ret;
2589 struct address_space *mapping = page->mapping;
2590 struct extent_page_data epd = {
2591 .bio = NULL,
2592 .tree = tree,
2593 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002594 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002595 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002596 };
2597 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002598 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002599 .older_than_this = NULL,
2600 .nr_to_write = 64,
2601 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2602 .range_end = (loff_t)-1,
2603 };
2604
Chris Masond1310b22008-01-24 16:13:08 -05002605 ret = __extent_writepage(page, wbc, &epd);
2606
Chris Mason4bef0842008-09-08 11:18:08 -04002607 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002608 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002609 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002610 return ret;
2611}
Chris Masond1310b22008-01-24 16:13:08 -05002612
Chris Mason771ed682008-11-06 22:02:51 -05002613int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2614 u64 start, u64 end, get_extent_t *get_extent,
2615 int mode)
2616{
2617 int ret = 0;
2618 struct address_space *mapping = inode->i_mapping;
2619 struct page *page;
2620 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2621 PAGE_CACHE_SHIFT;
2622
2623 struct extent_page_data epd = {
2624 .bio = NULL,
2625 .tree = tree,
2626 .get_extent = get_extent,
2627 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002628 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002629 };
2630 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002631 .sync_mode = mode,
2632 .older_than_this = NULL,
2633 .nr_to_write = nr_pages * 2,
2634 .range_start = start,
2635 .range_end = end + 1,
2636 };
2637
Chris Masond3977122009-01-05 21:25:51 -05002638 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002639 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2640 if (clear_page_dirty_for_io(page))
2641 ret = __extent_writepage(page, &wbc_writepages, &epd);
2642 else {
2643 if (tree->ops && tree->ops->writepage_end_io_hook)
2644 tree->ops->writepage_end_io_hook(page, start,
2645 start + PAGE_CACHE_SIZE - 1,
2646 NULL, 1);
2647 unlock_page(page);
2648 }
2649 page_cache_release(page);
2650 start += PAGE_CACHE_SIZE;
2651 }
2652
Chris Masonffbd5172009-04-20 15:50:09 -04002653 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002654 return ret;
2655}
Chris Masond1310b22008-01-24 16:13:08 -05002656
2657int extent_writepages(struct extent_io_tree *tree,
2658 struct address_space *mapping,
2659 get_extent_t *get_extent,
2660 struct writeback_control *wbc)
2661{
2662 int ret = 0;
2663 struct extent_page_data epd = {
2664 .bio = NULL,
2665 .tree = tree,
2666 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002667 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002668 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002669 };
2670
Chris Mason4bef0842008-09-08 11:18:08 -04002671 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002672 __extent_writepage, &epd,
2673 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002674 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002675 return ret;
2676}
Chris Masond1310b22008-01-24 16:13:08 -05002677
2678int extent_readpages(struct extent_io_tree *tree,
2679 struct address_space *mapping,
2680 struct list_head *pages, unsigned nr_pages,
2681 get_extent_t get_extent)
2682{
2683 struct bio *bio = NULL;
2684 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002685 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002686
Chris Masond1310b22008-01-24 16:13:08 -05002687 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2688 struct page *page = list_entry(pages->prev, struct page, lru);
2689
2690 prefetchw(&page->flags);
2691 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002692 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002693 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002694 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002695 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002696 }
2697 page_cache_release(page);
2698 }
Chris Masond1310b22008-01-24 16:13:08 -05002699 BUG_ON(!list_empty(pages));
2700 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002701 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002702 return 0;
2703}
Chris Masond1310b22008-01-24 16:13:08 -05002704
2705/*
2706 * basic invalidatepage code, this waits on any locked or writeback
2707 * ranges corresponding to the page, and then deletes any extent state
2708 * records from the tree
2709 */
2710int extent_invalidatepage(struct extent_io_tree *tree,
2711 struct page *page, unsigned long offset)
2712{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002713 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002714 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2715 u64 end = start + PAGE_CACHE_SIZE - 1;
2716 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2717
Chris Masond3977122009-01-05 21:25:51 -05002718 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002719 if (start > end)
2720 return 0;
2721
Josef Bacik2ac55d42010-02-03 19:33:23 +00002722 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002723 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002724 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002725 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2726 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002727 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002728 return 0;
2729}
Chris Masond1310b22008-01-24 16:13:08 -05002730
2731/*
2732 * simple commit_write call, set_range_dirty is used to mark both
2733 * the pages and the extent records as dirty
2734 */
2735int extent_commit_write(struct extent_io_tree *tree,
2736 struct inode *inode, struct page *page,
2737 unsigned from, unsigned to)
2738{
2739 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2740
2741 set_page_extent_mapped(page);
2742 set_page_dirty(page);
2743
2744 if (pos > inode->i_size) {
2745 i_size_write(inode, pos);
2746 mark_inode_dirty(inode);
2747 }
2748 return 0;
2749}
Chris Masond1310b22008-01-24 16:13:08 -05002750
2751int extent_prepare_write(struct extent_io_tree *tree,
2752 struct inode *inode, struct page *page,
2753 unsigned from, unsigned to, get_extent_t *get_extent)
2754{
2755 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2756 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2757 u64 block_start;
2758 u64 orig_block_start;
2759 u64 block_end;
2760 u64 cur_end;
2761 struct extent_map *em;
2762 unsigned blocksize = 1 << inode->i_blkbits;
2763 size_t page_offset = 0;
2764 size_t block_off_start;
2765 size_t block_off_end;
2766 int err = 0;
2767 int iocount = 0;
2768 int ret = 0;
2769 int isnew;
2770
2771 set_page_extent_mapped(page);
2772
2773 block_start = (page_start + from) & ~((u64)blocksize - 1);
2774 block_end = (page_start + to - 1) | (blocksize - 1);
2775 orig_block_start = block_start;
2776
2777 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002778 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002779 em = get_extent(inode, page, page_offset, block_start,
2780 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002781 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002782 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002783
Chris Masond1310b22008-01-24 16:13:08 -05002784 cur_end = min(block_end, extent_map_end(em) - 1);
2785 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2786 block_off_end = block_off_start + blocksize;
2787 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2788
2789 if (!PageUptodate(page) && isnew &&
2790 (block_off_end > to || block_off_start < from)) {
2791 void *kaddr;
2792
2793 kaddr = kmap_atomic(page, KM_USER0);
2794 if (block_off_end > to)
2795 memset(kaddr + to, 0, block_off_end - to);
2796 if (block_off_start < from)
2797 memset(kaddr + block_off_start, 0,
2798 from - block_off_start);
2799 flush_dcache_page(page);
2800 kunmap_atomic(kaddr, KM_USER0);
2801 }
2802 if ((em->block_start != EXTENT_MAP_HOLE &&
2803 em->block_start != EXTENT_MAP_INLINE) &&
2804 !isnew && !PageUptodate(page) &&
2805 (block_off_end > to || block_off_start < from) &&
2806 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002807 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002808 u64 sector;
2809 u64 extent_offset = block_start - em->start;
2810 size_t iosize;
2811 sector = (em->block_start + extent_offset) >> 9;
2812 iosize = (cur_end - block_start + blocksize) &
2813 ~((u64)blocksize - 1);
2814 /*
2815 * we've already got the extent locked, but we
2816 * need to split the state such that our end_bio
2817 * handler can clear the lock.
2818 */
2819 set_extent_bit(tree, block_start,
2820 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002821 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002822 ret = submit_extent_page(READ, tree, page,
2823 sector, iosize, page_offset, em->bdev,
2824 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002825 end_bio_extent_preparewrite, 0,
2826 0, 0);
Andi Kleen411fc6b2010-10-29 15:14:31 -04002827 if (ret && !err)
2828 err = ret;
Chris Masond1310b22008-01-24 16:13:08 -05002829 iocount++;
2830 block_start = block_start + iosize;
2831 } else {
Arne Jansen507903b2011-04-06 10:02:20 +00002832 struct extent_state *cached = NULL;
2833
2834 set_extent_uptodate(tree, block_start, cur_end, &cached,
Chris Masond1310b22008-01-24 16:13:08 -05002835 GFP_NOFS);
Arne Jansen507903b2011-04-06 10:02:20 +00002836 unlock_extent_cached(tree, block_start, cur_end,
2837 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002838 block_start = cur_end + 1;
2839 }
2840 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2841 free_extent_map(em);
2842 }
2843 if (iocount) {
2844 wait_extent_bit(tree, orig_block_start,
2845 block_end, EXTENT_LOCKED);
2846 }
2847 check_page_uptodate(tree, page);
2848err:
2849 /* FIXME, zero out newly allocated blocks on error */
2850 return err;
2851}
Chris Masond1310b22008-01-24 16:13:08 -05002852
2853/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002854 * a helper for releasepage, this tests for areas of the page that
2855 * are locked or under IO and drops the related state bits if it is safe
2856 * to drop the page.
2857 */
2858int try_release_extent_state(struct extent_map_tree *map,
2859 struct extent_io_tree *tree, struct page *page,
2860 gfp_t mask)
2861{
2862 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2863 u64 end = start + PAGE_CACHE_SIZE - 1;
2864 int ret = 1;
2865
Chris Mason211f90e2008-07-18 11:56:15 -04002866 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002867 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002868 ret = 0;
2869 else {
2870 if ((mask & GFP_NOFS) == GFP_NOFS)
2871 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002872 /*
2873 * at this point we can safely clear everything except the
2874 * locked bit and the nodatasum bit
2875 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002876 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002877 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2878 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002879
2880 /* if clear_extent_bit failed for enomem reasons,
2881 * we can't allow the release to continue.
2882 */
2883 if (ret < 0)
2884 ret = 0;
2885 else
2886 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002887 }
2888 return ret;
2889}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002890
2891/*
Chris Masond1310b22008-01-24 16:13:08 -05002892 * a helper for releasepage. As long as there are no locked extents
2893 * in the range corresponding to the page, both state records and extent
2894 * map records are removed
2895 */
2896int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002897 struct extent_io_tree *tree, struct page *page,
2898 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002899{
2900 struct extent_map *em;
2901 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2902 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002903
Chris Mason70dec802008-01-29 09:59:12 -05002904 if ((mask & __GFP_WAIT) &&
2905 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002906 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002907 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002908 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002909 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002910 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002911 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002912 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002913 break;
2914 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002915 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2916 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002917 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002918 free_extent_map(em);
2919 break;
2920 }
2921 if (!test_range_bit(tree, em->start,
2922 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002923 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002924 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002925 remove_extent_mapping(map, em);
2926 /* once for the rb tree */
2927 free_extent_map(em);
2928 }
2929 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002930 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002931
2932 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002933 free_extent_map(em);
2934 }
Chris Masond1310b22008-01-24 16:13:08 -05002935 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002936 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002937}
Chris Masond1310b22008-01-24 16:13:08 -05002938
2939sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2940 get_extent_t *get_extent)
2941{
2942 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002943 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002944 u64 start = iblock << inode->i_blkbits;
2945 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002946 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002947 struct extent_map *em;
2948
Josef Bacik2ac55d42010-02-03 19:33:23 +00002949 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2950 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002951 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002952 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2953 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002954 if (!em || IS_ERR(em))
2955 return 0;
2956
Yan Zhengd899e052008-10-30 14:25:28 -04002957 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002958 goto out;
2959
2960 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002961out:
2962 free_extent_map(em);
2963 return sector;
2964}
2965
Chris Masonec29ed52011-02-23 16:23:20 -05002966/*
2967 * helper function for fiemap, which doesn't want to see any holes.
2968 * This maps until we find something past 'last'
2969 */
2970static struct extent_map *get_extent_skip_holes(struct inode *inode,
2971 u64 offset,
2972 u64 last,
2973 get_extent_t *get_extent)
2974{
2975 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2976 struct extent_map *em;
2977 u64 len;
2978
2979 if (offset >= last)
2980 return NULL;
2981
2982 while(1) {
2983 len = last - offset;
2984 if (len == 0)
2985 break;
2986 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2987 em = get_extent(inode, NULL, 0, offset, len, 0);
2988 if (!em || IS_ERR(em))
2989 return em;
2990
2991 /* if this isn't a hole return it */
2992 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2993 em->block_start != EXTENT_MAP_HOLE) {
2994 return em;
2995 }
2996
2997 /* this is a hole, advance to the next extent */
2998 offset = extent_map_end(em);
2999 free_extent_map(em);
3000 if (offset >= last)
3001 break;
3002 }
3003 return NULL;
3004}
3005
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003006int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3007 __u64 start, __u64 len, get_extent_t *get_extent)
3008{
Josef Bacik975f84f2010-11-23 19:36:57 +00003009 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003010 u64 off = start;
3011 u64 max = start + len;
3012 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003013 u32 found_type;
3014 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003015 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003016 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003017 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003018 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003019 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003020 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003021 struct btrfs_path *path;
3022 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003023 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003024 u64 em_start = 0;
3025 u64 em_len = 0;
3026 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003027 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003028
3029 if (len == 0)
3030 return -EINVAL;
3031
Josef Bacik975f84f2010-11-23 19:36:57 +00003032 path = btrfs_alloc_path();
3033 if (!path)
3034 return -ENOMEM;
3035 path->leave_spinning = 1;
3036
Chris Masonec29ed52011-02-23 16:23:20 -05003037 /*
3038 * lookup the last file extent. We're not using i_size here
3039 * because there might be preallocation past i_size
3040 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003041 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3042 path, inode->i_ino, -1, 0);
3043 if (ret < 0) {
3044 btrfs_free_path(path);
3045 return ret;
3046 }
3047 WARN_ON(!ret);
3048 path->slots[0]--;
3049 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3050 struct btrfs_file_extent_item);
3051 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3052 found_type = btrfs_key_type(&found_key);
3053
Chris Masonec29ed52011-02-23 16:23:20 -05003054 /* No extents, but there might be delalloc bits */
Josef Bacik975f84f2010-11-23 19:36:57 +00003055 if (found_key.objectid != inode->i_ino ||
3056 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003057 /* have to trust i_size as the end */
3058 last = (u64)-1;
3059 last_for_get_extent = isize;
3060 } else {
3061 /*
3062 * remember the start of the last extent. There are a
3063 * bunch of different factors that go into the length of the
3064 * extent, so its much less complex to remember where it started
3065 */
3066 last = found_key.offset;
3067 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003068 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003069 btrfs_free_path(path);
3070
Chris Masonec29ed52011-02-23 16:23:20 -05003071 /*
3072 * we might have some extents allocated but more delalloc past those
3073 * extents. so, we trust isize unless the start of the last extent is
3074 * beyond isize
3075 */
3076 if (last < isize) {
3077 last = (u64)-1;
3078 last_for_get_extent = isize;
3079 }
3080
Josef Bacik2ac55d42010-02-03 19:33:23 +00003081 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3082 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003083
3084 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3085 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003086 if (!em)
3087 goto out;
3088 if (IS_ERR(em)) {
3089 ret = PTR_ERR(em);
3090 goto out;
3091 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003092
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003093 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003094 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003095
Chris Masonea8efc72011-03-08 11:54:40 -05003096 /* break if the extent we found is outside the range */
3097 if (em->start >= max || extent_map_end(em) < off)
3098 break;
3099
3100 /*
3101 * get_extent may return an extent that starts before our
3102 * requested range. We have to make sure the ranges
3103 * we return to fiemap always move forward and don't
3104 * overlap, so adjust the offsets here
3105 */
3106 em_start = max(em->start, off);
3107
3108 /*
3109 * record the offset from the start of the extent
3110 * for adjusting the disk offset below
3111 */
3112 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003113 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003114 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003115 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003116 disko = 0;
3117 flags = 0;
3118
Chris Masonea8efc72011-03-08 11:54:40 -05003119 /*
3120 * bump off for our next call to get_extent
3121 */
3122 off = extent_map_end(em);
3123 if (off >= max)
3124 end = 1;
3125
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003126 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003127 end = 1;
3128 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003129 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003130 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3131 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003132 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003133 flags |= (FIEMAP_EXTENT_DELALLOC |
3134 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003135 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003136 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003137 }
3138 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3139 flags |= FIEMAP_EXTENT_ENCODED;
3140
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003141 free_extent_map(em);
3142 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003143 if ((em_start >= last) || em_len == (u64)-1 ||
3144 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003145 flags |= FIEMAP_EXTENT_LAST;
3146 end = 1;
3147 }
3148
Chris Masonec29ed52011-02-23 16:23:20 -05003149 /* now scan forward to see if this is really the last extent. */
3150 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3151 get_extent);
3152 if (IS_ERR(em)) {
3153 ret = PTR_ERR(em);
3154 goto out;
3155 }
3156 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003157 flags |= FIEMAP_EXTENT_LAST;
3158 end = 1;
3159 }
Chris Masonec29ed52011-02-23 16:23:20 -05003160 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3161 em_len, flags);
3162 if (ret)
3163 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003164 }
3165out_free:
3166 free_extent_map(em);
3167out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003168 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3169 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003170 return ret;
3171}
3172
Chris Masond1310b22008-01-24 16:13:08 -05003173static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3174 unsigned long i)
3175{
3176 struct page *p;
3177 struct address_space *mapping;
3178
3179 if (i == 0)
3180 return eb->first_page;
3181 i += eb->start >> PAGE_CACHE_SHIFT;
3182 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003183 if (!mapping)
3184 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003185
3186 /*
3187 * extent_buffer_page is only called after pinning the page
3188 * by increasing the reference count. So we know the page must
3189 * be in the radix tree.
3190 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003191 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003192 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003193 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003194
Chris Masond1310b22008-01-24 16:13:08 -05003195 return p;
3196}
3197
Chris Mason6af118ce2008-07-22 11:18:07 -04003198static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003199{
Chris Mason6af118ce2008-07-22 11:18:07 -04003200 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3201 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003202}
3203
Chris Masond1310b22008-01-24 16:13:08 -05003204static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3205 u64 start,
3206 unsigned long len,
3207 gfp_t mask)
3208{
3209 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003210#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003211 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003212#endif
Chris Masond1310b22008-01-24 16:13:08 -05003213
Chris Masond1310b22008-01-24 16:13:08 -05003214 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003215 if (eb == NULL)
3216 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003217 eb->start = start;
3218 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003219 spin_lock_init(&eb->lock);
3220 init_waitqueue_head(&eb->lock_wq);
3221
Chris Mason39351272009-02-04 09:24:05 -05003222#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003223 spin_lock_irqsave(&leak_lock, flags);
3224 list_add(&eb->leak_list, &buffers);
3225 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003226#endif
Chris Masond1310b22008-01-24 16:13:08 -05003227 atomic_set(&eb->refs, 1);
3228
3229 return eb;
3230}
3231
3232static void __free_extent_buffer(struct extent_buffer *eb)
3233{
Chris Mason39351272009-02-04 09:24:05 -05003234#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003235 unsigned long flags;
3236 spin_lock_irqsave(&leak_lock, flags);
3237 list_del(&eb->leak_list);
3238 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003239#endif
Chris Masond1310b22008-01-24 16:13:08 -05003240 kmem_cache_free(extent_buffer_cache, eb);
3241}
3242
Miao Xie897ca6e2010-10-26 20:57:29 -04003243/*
3244 * Helper for releasing extent buffer page.
3245 */
3246static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3247 unsigned long start_idx)
3248{
3249 unsigned long index;
3250 struct page *page;
3251
3252 if (!eb->first_page)
3253 return;
3254
3255 index = num_extent_pages(eb->start, eb->len);
3256 if (start_idx >= index)
3257 return;
3258
3259 do {
3260 index--;
3261 page = extent_buffer_page(eb, index);
3262 if (page)
3263 page_cache_release(page);
3264 } while (index != start_idx);
3265}
3266
3267/*
3268 * Helper for releasing the extent buffer.
3269 */
3270static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3271{
3272 btrfs_release_extent_buffer_page(eb, 0);
3273 __free_extent_buffer(eb);
3274}
3275
Chris Masond1310b22008-01-24 16:13:08 -05003276struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3277 u64 start, unsigned long len,
3278 struct page *page0,
3279 gfp_t mask)
3280{
3281 unsigned long num_pages = num_extent_pages(start, len);
3282 unsigned long i;
3283 unsigned long index = start >> PAGE_CACHE_SHIFT;
3284 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003285 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003286 struct page *p;
3287 struct address_space *mapping = tree->mapping;
3288 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003289 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003290
Miao Xie19fe0a82010-10-26 20:57:29 -04003291 rcu_read_lock();
3292 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3293 if (eb && atomic_inc_not_zero(&eb->refs)) {
3294 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003295 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003296 return eb;
3297 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003298 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003299
Chris Masond1310b22008-01-24 16:13:08 -05003300 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003301 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003302 return NULL;
3303
Chris Masond1310b22008-01-24 16:13:08 -05003304 if (page0) {
3305 eb->first_page = page0;
3306 i = 1;
3307 index++;
3308 page_cache_get(page0);
3309 mark_page_accessed(page0);
3310 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003311 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003312 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003313 } else {
3314 i = 0;
3315 }
3316 for (; i < num_pages; i++, index++) {
3317 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3318 if (!p) {
3319 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003320 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003321 }
3322 set_page_extent_mapped(p);
3323 mark_page_accessed(p);
3324 if (i == 0) {
3325 eb->first_page = p;
3326 set_page_extent_head(p, len);
3327 } else {
3328 set_page_private(p, EXTENT_PAGE_PRIVATE);
3329 }
3330 if (!PageUptodate(p))
3331 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003332
3333 /*
3334 * see below about how we avoid a nasty race with release page
3335 * and why we unlock later
3336 */
3337 if (i != 0)
3338 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003339 }
3340 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003341 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003342
Miao Xie19fe0a82010-10-26 20:57:29 -04003343 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3344 if (ret)
3345 goto free_eb;
3346
Chris Mason6af118ce2008-07-22 11:18:07 -04003347 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003348 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3349 if (ret == -EEXIST) {
3350 exists = radix_tree_lookup(&tree->buffer,
3351 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003352 /* add one reference for the caller */
3353 atomic_inc(&exists->refs);
3354 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003355 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003356 goto free_eb;
3357 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003358 /* add one reference for the tree */
3359 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003360 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003361 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003362
3363 /*
3364 * there is a race where release page may have
3365 * tried to find this extent buffer in the radix
3366 * but failed. It will tell the VM it is safe to
3367 * reclaim the, and it will clear the page private bit.
3368 * We must make sure to set the page private bit properly
3369 * after the extent buffer is in the radix tree so
3370 * it doesn't get lost
3371 */
3372 set_page_extent_mapped(eb->first_page);
3373 set_page_extent_head(eb->first_page, eb->len);
3374 if (!page0)
3375 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003376 return eb;
3377
Chris Mason6af118ce2008-07-22 11:18:07 -04003378free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003379 if (eb->first_page && !page0)
3380 unlock_page(eb->first_page);
3381
Chris Masond1310b22008-01-24 16:13:08 -05003382 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003383 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003384 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003385 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003386}
Chris Masond1310b22008-01-24 16:13:08 -05003387
3388struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3389 u64 start, unsigned long len,
3390 gfp_t mask)
3391{
Chris Masond1310b22008-01-24 16:13:08 -05003392 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003393
Miao Xie19fe0a82010-10-26 20:57:29 -04003394 rcu_read_lock();
3395 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3396 if (eb && atomic_inc_not_zero(&eb->refs)) {
3397 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003398 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003399 return eb;
3400 }
3401 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003402
Miao Xie19fe0a82010-10-26 20:57:29 -04003403 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003404}
Chris Masond1310b22008-01-24 16:13:08 -05003405
3406void free_extent_buffer(struct extent_buffer *eb)
3407{
Chris Masond1310b22008-01-24 16:13:08 -05003408 if (!eb)
3409 return;
3410
3411 if (!atomic_dec_and_test(&eb->refs))
3412 return;
3413
Chris Mason6af118ce2008-07-22 11:18:07 -04003414 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003415}
Chris Masond1310b22008-01-24 16:13:08 -05003416
3417int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3418 struct extent_buffer *eb)
3419{
Chris Masond1310b22008-01-24 16:13:08 -05003420 unsigned long i;
3421 unsigned long num_pages;
3422 struct page *page;
3423
Chris Masond1310b22008-01-24 16:13:08 -05003424 num_pages = num_extent_pages(eb->start, eb->len);
3425
3426 for (i = 0; i < num_pages; i++) {
3427 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003428 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003429 continue;
3430
Chris Masona61e6f22008-07-22 11:18:08 -04003431 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003432 WARN_ON(!PagePrivate(page));
3433
3434 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003435 if (i == 0)
3436 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003437
Chris Masond1310b22008-01-24 16:13:08 -05003438 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003439 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003440 if (!PageDirty(page)) {
3441 radix_tree_tag_clear(&page->mapping->page_tree,
3442 page_index(page),
3443 PAGECACHE_TAG_DIRTY);
3444 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003445 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003446 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003447 }
3448 return 0;
3449}
Chris Masond1310b22008-01-24 16:13:08 -05003450
3451int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3452 struct extent_buffer *eb)
3453{
3454 return wait_on_extent_writeback(tree, eb->start,
3455 eb->start + eb->len - 1);
3456}
Chris Masond1310b22008-01-24 16:13:08 -05003457
3458int set_extent_buffer_dirty(struct extent_io_tree *tree,
3459 struct extent_buffer *eb)
3460{
3461 unsigned long i;
3462 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003463 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003464
Chris Masonb9473432009-03-13 11:00:37 -04003465 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003466 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003467 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003468 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003469 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003470}
Chris Masond1310b22008-01-24 16:13:08 -05003471
Chris Mason1259ab72008-05-12 13:39:03 -04003472int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003473 struct extent_buffer *eb,
3474 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003475{
3476 unsigned long i;
3477 struct page *page;
3478 unsigned long num_pages;
3479
3480 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003481 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003482
3483 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003484 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003485 for (i = 0; i < num_pages; i++) {
3486 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003487 if (page)
3488 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003489 }
3490 return 0;
3491}
3492
Chris Masond1310b22008-01-24 16:13:08 -05003493int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3494 struct extent_buffer *eb)
3495{
3496 unsigned long i;
3497 struct page *page;
3498 unsigned long num_pages;
3499
3500 num_pages = num_extent_pages(eb->start, eb->len);
3501
3502 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003503 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003504 for (i = 0; i < num_pages; i++) {
3505 page = extent_buffer_page(eb, i);
3506 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3507 ((i == num_pages - 1) &&
3508 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3509 check_page_uptodate(tree, page);
3510 continue;
3511 }
3512 SetPageUptodate(page);
3513 }
3514 return 0;
3515}
Chris Masond1310b22008-01-24 16:13:08 -05003516
Chris Masonce9adaa2008-04-09 16:28:12 -04003517int extent_range_uptodate(struct extent_io_tree *tree,
3518 u64 start, u64 end)
3519{
3520 struct page *page;
3521 int ret;
3522 int pg_uptodate = 1;
3523 int uptodate;
3524 unsigned long index;
3525
Chris Mason9655d292009-09-02 15:22:30 -04003526 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003527 if (ret)
3528 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003529 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003530 index = start >> PAGE_CACHE_SHIFT;
3531 page = find_get_page(tree->mapping, index);
3532 uptodate = PageUptodate(page);
3533 page_cache_release(page);
3534 if (!uptodate) {
3535 pg_uptodate = 0;
3536 break;
3537 }
3538 start += PAGE_CACHE_SIZE;
3539 }
3540 return pg_uptodate;
3541}
3542
Chris Masond1310b22008-01-24 16:13:08 -05003543int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003544 struct extent_buffer *eb,
3545 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003546{
Chris Mason728131d2008-04-09 16:28:12 -04003547 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003548 unsigned long num_pages;
3549 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003550 struct page *page;
3551 int pg_uptodate = 1;
3552
Chris Masonb4ce94d2009-02-04 09:25:08 -05003553 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003554 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003555
Chris Mason42352982008-04-28 16:40:52 -04003556 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003557 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003558 if (ret)
3559 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003560
3561 num_pages = num_extent_pages(eb->start, eb->len);
3562 for (i = 0; i < num_pages; i++) {
3563 page = extent_buffer_page(eb, i);
3564 if (!PageUptodate(page)) {
3565 pg_uptodate = 0;
3566 break;
3567 }
3568 }
Chris Mason42352982008-04-28 16:40:52 -04003569 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003570}
Chris Masond1310b22008-01-24 16:13:08 -05003571
3572int read_extent_buffer_pages(struct extent_io_tree *tree,
3573 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003574 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003575 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003576{
3577 unsigned long i;
3578 unsigned long start_i;
3579 struct page *page;
3580 int err;
3581 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003582 int locked_pages = 0;
3583 int all_uptodate = 1;
3584 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003585 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003586 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003587 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003588
Chris Masonb4ce94d2009-02-04 09:25:08 -05003589 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003590 return 0;
3591
Chris Masonce9adaa2008-04-09 16:28:12 -04003592 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003593 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003594 return 0;
3595 }
3596
3597 if (start) {
3598 WARN_ON(start < eb->start);
3599 start_i = (start >> PAGE_CACHE_SHIFT) -
3600 (eb->start >> PAGE_CACHE_SHIFT);
3601 } else {
3602 start_i = 0;
3603 }
3604
3605 num_pages = num_extent_pages(eb->start, eb->len);
3606 for (i = start_i; i < num_pages; i++) {
3607 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003608 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003609 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003610 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003611 } else {
3612 lock_page(page);
3613 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003614 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003615 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003616 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003617 }
3618 if (all_uptodate) {
3619 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003620 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003621 goto unlock_exit;
3622 }
3623
3624 for (i = start_i; i < num_pages; i++) {
3625 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003626
3627 WARN_ON(!PagePrivate(page));
3628
3629 set_page_extent_mapped(page);
3630 if (i == 0)
3631 set_page_extent_head(page, eb->len);
3632
Chris Masonce9adaa2008-04-09 16:28:12 -04003633 if (inc_all_pages)
3634 page_cache_get(page);
3635 if (!PageUptodate(page)) {
3636 if (start_i == 0)
3637 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003638 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003639 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003640 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003641 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003642 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003643 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003644 } else {
3645 unlock_page(page);
3646 }
3647 }
3648
Chris Masona86c12c2008-02-07 10:50:54 -05003649 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003650 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003651
Chris Masond3977122009-01-05 21:25:51 -05003652 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003653 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003654
Chris Masond1310b22008-01-24 16:13:08 -05003655 for (i = start_i; i < num_pages; i++) {
3656 page = extent_buffer_page(eb, i);
3657 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003658 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003659 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003660 }
Chris Masond3977122009-01-05 21:25:51 -05003661
Chris Masond1310b22008-01-24 16:13:08 -05003662 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003663 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003664 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003665
3666unlock_exit:
3667 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003668 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003669 page = extent_buffer_page(eb, i);
3670 i++;
3671 unlock_page(page);
3672 locked_pages--;
3673 }
3674 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003675}
Chris Masond1310b22008-01-24 16:13:08 -05003676
3677void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3678 unsigned long start,
3679 unsigned long len)
3680{
3681 size_t cur;
3682 size_t offset;
3683 struct page *page;
3684 char *kaddr;
3685 char *dst = (char *)dstv;
3686 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3687 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003688
3689 WARN_ON(start > eb->len);
3690 WARN_ON(start + len > eb->start + eb->len);
3691
3692 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3693
Chris Masond3977122009-01-05 21:25:51 -05003694 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003695 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003696
3697 cur = min(len, (PAGE_CACHE_SIZE - offset));
3698 kaddr = kmap_atomic(page, KM_USER1);
3699 memcpy(dst, kaddr + offset, cur);
3700 kunmap_atomic(kaddr, KM_USER1);
3701
3702 dst += cur;
3703 len -= cur;
3704 offset = 0;
3705 i++;
3706 }
3707}
Chris Masond1310b22008-01-24 16:13:08 -05003708
3709int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3710 unsigned long min_len, char **token, char **map,
3711 unsigned long *map_start,
3712 unsigned long *map_len, int km)
3713{
3714 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3715 char *kaddr;
3716 struct page *p;
3717 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3718 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3719 unsigned long end_i = (start_offset + start + min_len - 1) >>
3720 PAGE_CACHE_SHIFT;
3721
3722 if (i != end_i)
3723 return -EINVAL;
3724
3725 if (i == 0) {
3726 offset = start_offset;
3727 *map_start = 0;
3728 } else {
3729 offset = 0;
3730 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3731 }
Chris Masond3977122009-01-05 21:25:51 -05003732
Chris Masond1310b22008-01-24 16:13:08 -05003733 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003734 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3735 "wanted %lu %lu\n", (unsigned long long)eb->start,
3736 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003737 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003738 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003739 }
3740
3741 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003742 kaddr = kmap_atomic(p, km);
3743 *token = kaddr;
3744 *map = kaddr + offset;
3745 *map_len = PAGE_CACHE_SIZE - offset;
3746 return 0;
3747}
Chris Masond1310b22008-01-24 16:13:08 -05003748
3749int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3750 unsigned long min_len,
3751 char **token, char **map,
3752 unsigned long *map_start,
3753 unsigned long *map_len, int km)
3754{
3755 int err;
3756 int save = 0;
3757 if (eb->map_token) {
3758 unmap_extent_buffer(eb, eb->map_token, km);
3759 eb->map_token = NULL;
3760 save = 1;
3761 }
3762 err = map_private_extent_buffer(eb, start, min_len, token, map,
3763 map_start, map_len, km);
3764 if (!err && save) {
3765 eb->map_token = *token;
3766 eb->kaddr = *map;
3767 eb->map_start = *map_start;
3768 eb->map_len = *map_len;
3769 }
3770 return err;
3771}
Chris Masond1310b22008-01-24 16:13:08 -05003772
3773void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3774{
3775 kunmap_atomic(token, km);
3776}
Chris Masond1310b22008-01-24 16:13:08 -05003777
3778int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3779 unsigned long start,
3780 unsigned long len)
3781{
3782 size_t cur;
3783 size_t offset;
3784 struct page *page;
3785 char *kaddr;
3786 char *ptr = (char *)ptrv;
3787 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3788 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3789 int ret = 0;
3790
3791 WARN_ON(start > eb->len);
3792 WARN_ON(start + len > eb->start + eb->len);
3793
3794 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3795
Chris Masond3977122009-01-05 21:25:51 -05003796 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003797 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003798
3799 cur = min(len, (PAGE_CACHE_SIZE - offset));
3800
3801 kaddr = kmap_atomic(page, KM_USER0);
3802 ret = memcmp(ptr, kaddr + offset, cur);
3803 kunmap_atomic(kaddr, KM_USER0);
3804 if (ret)
3805 break;
3806
3807 ptr += cur;
3808 len -= cur;
3809 offset = 0;
3810 i++;
3811 }
3812 return ret;
3813}
Chris Masond1310b22008-01-24 16:13:08 -05003814
3815void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3816 unsigned long start, unsigned long len)
3817{
3818 size_t cur;
3819 size_t offset;
3820 struct page *page;
3821 char *kaddr;
3822 char *src = (char *)srcv;
3823 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3824 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3825
3826 WARN_ON(start > eb->len);
3827 WARN_ON(start + len > eb->start + eb->len);
3828
3829 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3830
Chris Masond3977122009-01-05 21:25:51 -05003831 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003832 page = extent_buffer_page(eb, i);
3833 WARN_ON(!PageUptodate(page));
3834
3835 cur = min(len, PAGE_CACHE_SIZE - offset);
3836 kaddr = kmap_atomic(page, KM_USER1);
3837 memcpy(kaddr + offset, src, cur);
3838 kunmap_atomic(kaddr, KM_USER1);
3839
3840 src += cur;
3841 len -= cur;
3842 offset = 0;
3843 i++;
3844 }
3845}
Chris Masond1310b22008-01-24 16:13:08 -05003846
3847void memset_extent_buffer(struct extent_buffer *eb, char c,
3848 unsigned long start, unsigned long len)
3849{
3850 size_t cur;
3851 size_t offset;
3852 struct page *page;
3853 char *kaddr;
3854 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3855 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3856
3857 WARN_ON(start > eb->len);
3858 WARN_ON(start + len > eb->start + eb->len);
3859
3860 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3861
Chris Masond3977122009-01-05 21:25:51 -05003862 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003863 page = extent_buffer_page(eb, i);
3864 WARN_ON(!PageUptodate(page));
3865
3866 cur = min(len, PAGE_CACHE_SIZE - offset);
3867 kaddr = kmap_atomic(page, KM_USER0);
3868 memset(kaddr + offset, c, cur);
3869 kunmap_atomic(kaddr, KM_USER0);
3870
3871 len -= cur;
3872 offset = 0;
3873 i++;
3874 }
3875}
Chris Masond1310b22008-01-24 16:13:08 -05003876
3877void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3878 unsigned long dst_offset, unsigned long src_offset,
3879 unsigned long len)
3880{
3881 u64 dst_len = dst->len;
3882 size_t cur;
3883 size_t offset;
3884 struct page *page;
3885 char *kaddr;
3886 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3887 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3888
3889 WARN_ON(src->len != dst_len);
3890
3891 offset = (start_offset + dst_offset) &
3892 ((unsigned long)PAGE_CACHE_SIZE - 1);
3893
Chris Masond3977122009-01-05 21:25:51 -05003894 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003895 page = extent_buffer_page(dst, i);
3896 WARN_ON(!PageUptodate(page));
3897
3898 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3899
3900 kaddr = kmap_atomic(page, KM_USER0);
3901 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3902 kunmap_atomic(kaddr, KM_USER0);
3903
3904 src_offset += cur;
3905 len -= cur;
3906 offset = 0;
3907 i++;
3908 }
3909}
Chris Masond1310b22008-01-24 16:13:08 -05003910
3911static void move_pages(struct page *dst_page, struct page *src_page,
3912 unsigned long dst_off, unsigned long src_off,
3913 unsigned long len)
3914{
3915 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3916 if (dst_page == src_page) {
3917 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3918 } else {
3919 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3920 char *p = dst_kaddr + dst_off + len;
3921 char *s = src_kaddr + src_off + len;
3922
3923 while (len--)
3924 *--p = *--s;
3925
3926 kunmap_atomic(src_kaddr, KM_USER1);
3927 }
3928 kunmap_atomic(dst_kaddr, KM_USER0);
3929}
3930
Sergei Trofimovich33872062011-04-11 21:52:52 +00003931static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3932{
3933 unsigned long distance = (src > dst) ? src - dst : dst - src;
3934 return distance < len;
3935}
3936
Chris Masond1310b22008-01-24 16:13:08 -05003937static void copy_pages(struct page *dst_page, struct page *src_page,
3938 unsigned long dst_off, unsigned long src_off,
3939 unsigned long len)
3940{
3941 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3942 char *src_kaddr;
3943
Sergei Trofimovich33872062011-04-11 21:52:52 +00003944 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003945 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003946 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003947 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003948 BUG_ON(areas_overlap(src_off, dst_off, len));
3949 }
Chris Masond1310b22008-01-24 16:13:08 -05003950
3951 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3952 kunmap_atomic(dst_kaddr, KM_USER0);
3953 if (dst_page != src_page)
3954 kunmap_atomic(src_kaddr, KM_USER1);
3955}
3956
3957void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3958 unsigned long src_offset, unsigned long len)
3959{
3960 size_t cur;
3961 size_t dst_off_in_page;
3962 size_t src_off_in_page;
3963 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3964 unsigned long dst_i;
3965 unsigned long src_i;
3966
3967 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003968 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3969 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003970 BUG_ON(1);
3971 }
3972 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003973 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3974 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003975 BUG_ON(1);
3976 }
3977
Chris Masond3977122009-01-05 21:25:51 -05003978 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003979 dst_off_in_page = (start_offset + dst_offset) &
3980 ((unsigned long)PAGE_CACHE_SIZE - 1);
3981 src_off_in_page = (start_offset + src_offset) &
3982 ((unsigned long)PAGE_CACHE_SIZE - 1);
3983
3984 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3985 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3986
3987 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3988 src_off_in_page));
3989 cur = min_t(unsigned long, cur,
3990 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3991
3992 copy_pages(extent_buffer_page(dst, dst_i),
3993 extent_buffer_page(dst, src_i),
3994 dst_off_in_page, src_off_in_page, cur);
3995
3996 src_offset += cur;
3997 dst_offset += cur;
3998 len -= cur;
3999 }
4000}
Chris Masond1310b22008-01-24 16:13:08 -05004001
4002void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4003 unsigned long src_offset, unsigned long len)
4004{
4005 size_t cur;
4006 size_t dst_off_in_page;
4007 size_t src_off_in_page;
4008 unsigned long dst_end = dst_offset + len - 1;
4009 unsigned long src_end = src_offset + len - 1;
4010 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4011 unsigned long dst_i;
4012 unsigned long src_i;
4013
4014 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004015 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4016 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004017 BUG_ON(1);
4018 }
4019 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004020 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4021 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004022 BUG_ON(1);
4023 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004024 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004025 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4026 return;
4027 }
Chris Masond3977122009-01-05 21:25:51 -05004028 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004029 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4030 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4031
4032 dst_off_in_page = (start_offset + dst_end) &
4033 ((unsigned long)PAGE_CACHE_SIZE - 1);
4034 src_off_in_page = (start_offset + src_end) &
4035 ((unsigned long)PAGE_CACHE_SIZE - 1);
4036
4037 cur = min_t(unsigned long, len, src_off_in_page + 1);
4038 cur = min(cur, dst_off_in_page + 1);
4039 move_pages(extent_buffer_page(dst, dst_i),
4040 extent_buffer_page(dst, src_i),
4041 dst_off_in_page - cur + 1,
4042 src_off_in_page - cur + 1, cur);
4043
4044 dst_end -= cur;
4045 src_end -= cur;
4046 len -= cur;
4047 }
4048}
Chris Mason6af118ce2008-07-22 11:18:07 -04004049
Miao Xie19fe0a82010-10-26 20:57:29 -04004050static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4051{
4052 struct extent_buffer *eb =
4053 container_of(head, struct extent_buffer, rcu_head);
4054
4055 btrfs_release_extent_buffer(eb);
4056}
4057
Chris Mason6af118ce2008-07-22 11:18:07 -04004058int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4059{
4060 u64 start = page_offset(page);
4061 struct extent_buffer *eb;
4062 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004063
4064 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004065 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004066 if (!eb) {
4067 spin_unlock(&tree->buffer_lock);
4068 return ret;
4069 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004070
Chris Masonb9473432009-03-13 11:00:37 -04004071 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4072 ret = 0;
4073 goto out;
4074 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004075
Miao Xie19fe0a82010-10-26 20:57:29 -04004076 /*
4077 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4078 * Or go back.
4079 */
4080 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4081 ret = 0;
4082 goto out;
4083 }
4084
4085 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004086out:
4087 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004088
4089 /* at this point we can safely release the extent buffer */
4090 if (atomic_read(&eb->refs) == 0)
4091 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004092 return ret;
4093}