blob: ebfff5b44752814556b42769456e01e742ce94d0 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
13#include "extent_io.h"
14#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040015#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050018
Chris Masond1310b22008-01-24 16:13:08 -050019static struct kmem_cache *extent_state_cache;
20static struct kmem_cache *extent_buffer_cache;
21
22static LIST_HEAD(buffers);
23static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040024
Chris Masonb47eda82008-11-10 12:34:40 -050025#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050026#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050027static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040028#endif
Chris Masond1310b22008-01-24 16:13:08 -050029
Chris Masond1310b22008-01-24 16:13:08 -050030#define BUFFER_LRU_MAX 64
31
32struct tree_entry {
33 u64 start;
34 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050035 struct rb_node rb_node;
36};
37
38struct extent_page_data {
39 struct bio *bio;
40 struct extent_io_tree *tree;
41 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050042
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
45 */
Chris Masonffbd5172009-04-20 15:50:09 -040046 unsigned int extent_locked:1;
47
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050050};
51
52int __init extent_io_init(void)
53{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020054 extent_state_cache = kmem_cache_create("extent_state",
55 sizeof(struct extent_state), 0,
56 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050057 if (!extent_state_cache)
58 return -ENOMEM;
59
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020060 extent_buffer_cache = kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer), 0,
62 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050063 if (!extent_buffer_cache)
64 goto free_state_cache;
65 return 0;
66
67free_state_cache:
68 kmem_cache_destroy(extent_state_cache);
69 return -ENOMEM;
70}
71
72void extent_io_exit(void)
73{
74 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040075 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050076
77 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040078 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050079 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state->start,
82 (unsigned long long)state->end,
83 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040084 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050085 kmem_cache_free(extent_state_cache, state);
86
87 }
88
Chris Mason2d2ae542008-03-26 16:24:23 -040089 while (!list_empty(&buffers)) {
90 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050091 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb->start,
93 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040094 list_del(&eb->leak_list);
95 kmem_cache_free(extent_buffer_cache, eb);
96 }
Chris Masond1310b22008-01-24 16:13:08 -050097 if (extent_state_cache)
98 kmem_cache_destroy(extent_state_cache);
99 if (extent_buffer_cache)
100 kmem_cache_destroy(extent_buffer_cache);
101}
102
103void extent_io_tree_init(struct extent_io_tree *tree,
104 struct address_space *mapping, gfp_t mask)
105{
Eric Paris6bef4d32010-02-23 19:43:04 +0000106 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400107 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500108 tree->ops = NULL;
109 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500110 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400111 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500113}
Chris Masond1310b22008-01-24 16:13:08 -0500114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
117 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500118#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
Chris Masond1310b22008-01-24 16:13:08 -0500137
Chris Mason4845e442010-05-25 20:56:50 -0400138void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500143#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400144 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Mason70dec802008-01-29 09:59:12 -0500146 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 kmem_cache_free(extent_state_cache, state);
153 }
154}
Chris Masond1310b22008-01-24 16:13:08 -0500155
156static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
158{
Chris Masond3977122009-01-05 21:25:51 -0500159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500161 struct tree_entry *entry;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500164 parent = *p;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
166
167 if (offset < entry->start)
168 p = &(*p)->rb_left;
169 else if (offset > entry->end)
170 p = &(*p)->rb_right;
171 else
172 return parent;
173 }
174
175 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
178 return NULL;
179}
180
Chris Mason80ea96b2008-02-01 14:51:59 -0500181static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
184{
Chris Mason80ea96b2008-02-01 14:51:59 -0500185 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500186 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
191
Chris Masond3977122009-01-05 21:25:51 -0500192 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500193 entry = rb_entry(n, struct tree_entry, rb_node);
194 prev = n;
195 prev_entry = entry;
196
197 if (offset < entry->start)
198 n = n->rb_left;
199 else if (offset > entry->end)
200 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500201 else
Chris Masond1310b22008-01-24 16:13:08 -0500202 return n;
203 }
204
205 if (prev_ret) {
206 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500207 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
210 }
211 *prev_ret = prev;
212 prev = orig_prev;
213 }
214
215 if (next_ret) {
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *next_ret = prev;
222 }
223 return NULL;
224}
225
Chris Mason80ea96b2008-02-01 14:51:59 -0500226static inline struct rb_node *tree_search(struct extent_io_tree *tree,
227 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500228{
Chris Mason70dec802008-01-29 09:59:12 -0500229 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500230 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500231
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500233 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500234 return prev;
235 return ret;
236}
237
Josef Bacik9ed74f22009-09-11 16:12:44 -0400238static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
239 struct extent_state *other)
240{
241 if (tree->ops && tree->ops->merge_extent_hook)
242 tree->ops->merge_extent_hook(tree->mapping->host, new,
243 other);
244}
245
Chris Masond1310b22008-01-24 16:13:08 -0500246/*
247 * utility function to look for merge candidates inside a given range.
248 * Any extents with matching state are merged together into a single
249 * extent in the tree. Extents with EXTENT_IO in their state field
250 * are not merged because the end_io handlers need to be able to do
251 * operations on them without sleeping (or doing allocations/splits).
252 *
253 * This should be called with the tree lock held.
254 */
255static int merge_state(struct extent_io_tree *tree,
256 struct extent_state *state)
257{
258 struct extent_state *other;
259 struct rb_node *other_node;
260
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400261 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500262 return 0;
263
264 other_node = rb_prev(&state->rb_node);
265 if (other_node) {
266 other = rb_entry(other_node, struct extent_state, rb_node);
267 if (other->end == state->start - 1 &&
268 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400269 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500270 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500271 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500272 rb_erase(&other->rb_node, &tree->state);
273 free_extent_state(other);
274 }
275 }
276 other_node = rb_next(&state->rb_node);
277 if (other_node) {
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->start == state->end + 1 &&
280 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400281 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500282 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500283 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500284 rb_erase(&state->rb_node, &tree->state);
285 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400286 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500287 }
288 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289
Chris Masond1310b22008-01-24 16:13:08 -0500290 return 0;
291}
292
Josef Bacik9ed74f22009-09-11 16:12:44 -0400293static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400294 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500295{
296 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400297 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400298 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500299 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400300
301 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500302}
303
304static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
Chris Masond1310b22008-01-24 16:13:08 -0500311/*
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
314 *
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
317 *
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
320 */
321static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400323 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500324{
325 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400326 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400327 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500328
329 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500330 printk(KERN_ERR "btrfs end < start %llu %llu\n",
331 (unsigned long long)end,
332 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500333 WARN_ON(1);
334 }
Chris Masond1310b22008-01-24 16:13:08 -0500335 state->start = start;
336 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400337 ret = set_state_cb(tree, state, bits);
338 if (ret)
339 return ret;
340
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400341 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400342 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400343 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500344 node = tree_insert(&tree->state, end, &state->rb_node);
345 if (node) {
346 struct extent_state *found;
347 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500348 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found->start,
350 (unsigned long long)found->end,
351 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500352 free_extent_state(state);
353 return -EEXIST;
354 }
Chris Mason70dec802008-01-29 09:59:12 -0500355 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500356 merge_state(tree, state);
357 return 0;
358}
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
361 u64 split)
362{
363 if (tree->ops && tree->ops->split_extent_hook)
364 return tree->ops->split_extent_hook(tree->mapping->host,
365 orig, split);
366 return 0;
367}
368
Chris Masond1310b22008-01-24 16:13:08 -0500369/*
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
373 *
374 * Before calling,
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
379 *
380 * The tree locks are not taken by this function. They need to be held
381 * by the caller.
382 */
383static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
385{
386 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387
388 split_cb(tree, orig, split);
389
Chris Masond1310b22008-01-24 16:13:08 -0500390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
393 orig->start = split;
394
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
396 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500397 free_extent_state(prealloc);
398 return -EEXIST;
399 }
Chris Mason70dec802008-01-29 09:59:12 -0500400 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500401 return 0;
402}
403
404/*
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
408 *
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
411 */
412static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 struct extent_state *state,
414 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500415{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400416 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400417 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500418
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400419 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500420 u64 range = state->end - state->start + 1;
421 WARN_ON(range > tree->dirty_bytes);
422 tree->dirty_bytes -= range;
423 }
Chris Mason291d6732008-01-29 15:55:23 -0500424 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400425 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500426 if (wake)
427 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400428 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500429 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500430 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500431 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500432 free_extent_state(state);
433 } else {
434 WARN_ON(1);
435 }
436 } else {
437 merge_state(tree, state);
438 }
439 return ret;
440}
441
Xiao Guangrong82337672011-04-20 06:44:57 +0000442static struct extent_state *
443alloc_extent_state_atomic(struct extent_state *prealloc)
444{
445 if (!prealloc)
446 prealloc = alloc_extent_state(GFP_ATOMIC);
447
448 return prealloc;
449}
450
Chris Masond1310b22008-01-24 16:13:08 -0500451/*
452 * clear some bits on a range in the tree. This may require splitting
453 * or inserting elements in the tree, so the gfp mask is used to
454 * indicate which allocations or sleeping are allowed.
455 *
456 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
457 * the given range from the tree regardless of state (ie for truncate).
458 *
459 * the range [start, end] is inclusive.
460 *
461 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
462 * bits were already set, or zero if none of the bits were already set.
463 */
464int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400465 int bits, int wake, int delete,
466 struct extent_state **cached_state,
467 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500468{
469 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400470 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500471 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400472 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500473 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400474 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500475 int err;
476 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000477 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500478
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400479 if (delete)
480 bits |= ~EXTENT_CTLBITS;
481 bits |= EXTENT_FIRST_DELALLOC;
482
Josef Bacik2ac55d42010-02-03 19:33:23 +0000483 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
484 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500485again:
486 if (!prealloc && (mask & __GFP_WAIT)) {
487 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000488 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500489 }
490
Chris Masoncad321a2008-12-17 14:51:42 -0500491 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400492 if (cached_state) {
493 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000494
495 if (clear) {
496 *cached_state = NULL;
497 cached_state = NULL;
498 }
499
Chris Mason42daec22009-09-23 19:51:09 -0400500 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000501 if (clear)
502 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400503 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400504 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400505 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000506 if (clear)
507 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400508 }
Chris Masond1310b22008-01-24 16:13:08 -0500509 /*
510 * this search will find the extents that end after
511 * our range starts
512 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500513 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500514 if (!node)
515 goto out;
516 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400517hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500518 if (state->start > end)
519 goto out;
520 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400521 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500522
523 /*
524 * | ---- desired range ---- |
525 * | state | or
526 * | ------------- state -------------- |
527 *
528 * We need to split the extent we found, and may flip
529 * bits on second half.
530 *
531 * If the extent we found extends past our range, we
532 * just split and search again. It'll get split again
533 * the next time though.
534 *
535 * If the extent we found is inside our range, we clear
536 * the desired bit on it.
537 */
538
539 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000540 prealloc = alloc_extent_state_atomic(prealloc);
541 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500542 err = split_state(tree, state, prealloc, start);
543 BUG_ON(err == -EEXIST);
544 prealloc = NULL;
545 if (err)
546 goto out;
547 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400548 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400549 if (last_end == (u64)-1)
550 goto out;
551 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500552 }
553 goto search_again;
554 }
555 /*
556 * | ---- desired range ---- |
557 * | state |
558 * We need to split the extent, and clear the bit
559 * on the first half
560 */
561 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000562 prealloc = alloc_extent_state_atomic(prealloc);
563 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500564 err = split_state(tree, state, prealloc, end + 1);
565 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500566 if (wake)
567 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400568
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400569 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400570
Chris Masond1310b22008-01-24 16:13:08 -0500571 prealloc = NULL;
572 goto out;
573 }
Chris Mason42daec22009-09-23 19:51:09 -0400574
Chris Mason2c64c532009-09-02 15:04:12 -0400575 if (state->end < end && prealloc && !need_resched())
576 next_node = rb_next(&state->rb_node);
577 else
578 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400579
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400580 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400581 if (last_end == (u64)-1)
582 goto out;
583 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400584 if (start <= end && next_node) {
585 state = rb_entry(next_node, struct extent_state,
586 rb_node);
587 if (state->start == start)
588 goto hit_next;
589 }
Chris Masond1310b22008-01-24 16:13:08 -0500590 goto search_again;
591
592out:
Chris Masoncad321a2008-12-17 14:51:42 -0500593 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500594 if (prealloc)
595 free_extent_state(prealloc);
596
597 return set;
598
599search_again:
600 if (start > end)
601 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500602 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500603 if (mask & __GFP_WAIT)
604 cond_resched();
605 goto again;
606}
Chris Masond1310b22008-01-24 16:13:08 -0500607
608static int wait_on_state(struct extent_io_tree *tree,
609 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500610 __releases(tree->lock)
611 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500612{
613 DEFINE_WAIT(wait);
614 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500615 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500616 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500617 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500618 finish_wait(&state->wq, &wait);
619 return 0;
620}
621
622/*
623 * waits for one or more bits to clear on a range in the state tree.
624 * The range [start, end] is inclusive.
625 * The tree lock is taken by this function
626 */
627int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
628{
629 struct extent_state *state;
630 struct rb_node *node;
631
Chris Masoncad321a2008-12-17 14:51:42 -0500632 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500633again:
634 while (1) {
635 /*
636 * this search will find all the extents that end after
637 * our range starts
638 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500639 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500640 if (!node)
641 break;
642
643 state = rb_entry(node, struct extent_state, rb_node);
644
645 if (state->start > end)
646 goto out;
647
648 if (state->state & bits) {
649 start = state->start;
650 atomic_inc(&state->refs);
651 wait_on_state(tree, state);
652 free_extent_state(state);
653 goto again;
654 }
655 start = state->end + 1;
656
657 if (start > end)
658 break;
659
660 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500661 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500662 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500663 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500664 }
665 }
666out:
Chris Masoncad321a2008-12-17 14:51:42 -0500667 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500668 return 0;
669}
Chris Masond1310b22008-01-24 16:13:08 -0500670
Josef Bacik9ed74f22009-09-11 16:12:44 -0400671static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500672 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400673 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500674{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400675 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400676 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400677
678 ret = set_state_cb(tree, state, bits);
679 if (ret)
680 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400681 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500682 u64 range = state->end - state->start + 1;
683 tree->dirty_bytes += range;
684 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400685 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400686
687 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500688}
689
Chris Mason2c64c532009-09-02 15:04:12 -0400690static void cache_state(struct extent_state *state,
691 struct extent_state **cached_ptr)
692{
693 if (cached_ptr && !(*cached_ptr)) {
694 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
695 *cached_ptr = state;
696 atomic_inc(&state->refs);
697 }
698 }
699}
700
Arne Jansen507903b2011-04-06 10:02:20 +0000701static void uncache_state(struct extent_state **cached_ptr)
702{
703 if (cached_ptr && (*cached_ptr)) {
704 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400705 *cached_ptr = NULL;
706 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000707 }
708}
709
Chris Masond1310b22008-01-24 16:13:08 -0500710/*
Chris Mason1edbb732009-09-02 13:24:36 -0400711 * set some bits on a range in the tree. This may require allocations or
712 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500713 *
Chris Mason1edbb732009-09-02 13:24:36 -0400714 * If any of the exclusive bits are set, this will fail with -EEXIST if some
715 * part of the range already has the desired bits set. The start of the
716 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500717 *
Chris Mason1edbb732009-09-02 13:24:36 -0400718 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500719 */
Chris Mason1edbb732009-09-02 13:24:36 -0400720
Chris Mason4845e442010-05-25 20:56:50 -0400721int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
722 int bits, int exclusive_bits, u64 *failed_start,
723 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500724{
725 struct extent_state *state;
726 struct extent_state *prealloc = NULL;
727 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500728 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500729 u64 last_start;
730 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400731
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400732 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500733again:
734 if (!prealloc && (mask & __GFP_WAIT)) {
735 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000736 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500737 }
738
Chris Masoncad321a2008-12-17 14:51:42 -0500739 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400740 if (cached_state && *cached_state) {
741 state = *cached_state;
742 if (state->start == start && state->tree) {
743 node = &state->rb_node;
744 goto hit_next;
745 }
746 }
Chris Masond1310b22008-01-24 16:13:08 -0500747 /*
748 * this search will find all the extents that end after
749 * our range starts.
750 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500751 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500752 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000753 prealloc = alloc_extent_state_atomic(prealloc);
754 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400755 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500756 prealloc = NULL;
757 BUG_ON(err == -EEXIST);
758 goto out;
759 }
Chris Masond1310b22008-01-24 16:13:08 -0500760 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400761hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500762 last_start = state->start;
763 last_end = state->end;
764
765 /*
766 * | ---- desired range ---- |
767 * | state |
768 *
769 * Just lock what we found and keep going
770 */
771 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400772 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400773 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500774 *failed_start = state->start;
775 err = -EEXIST;
776 goto out;
777 }
Chris Mason42daec22009-09-23 19:51:09 -0400778
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400779 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400780 if (err)
781 goto out;
782
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000783 next_node = rb_next(node);
Chris Mason2c64c532009-09-02 15:04:12 -0400784 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500785 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400786 if (last_end == (u64)-1)
787 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400788
Yan Zheng5c939df2009-05-27 09:16:03 -0400789 start = last_end + 1;
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000790 if (next_node && start < end && prealloc && !need_resched()) {
791 state = rb_entry(next_node, struct extent_state,
792 rb_node);
793 if (state->start == start)
794 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400795 }
Chris Masond1310b22008-01-24 16:13:08 -0500796 goto search_again;
797 }
798
799 /*
800 * | ---- desired range ---- |
801 * | state |
802 * or
803 * | ------------- state -------------- |
804 *
805 * We need to split the extent we found, and may flip bits on
806 * second half.
807 *
808 * If the extent we found extends past our
809 * range, we just split and search again. It'll get split
810 * again the next time though.
811 *
812 * If the extent we found is inside our range, we set the
813 * desired bit on it.
814 */
815 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400816 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500817 *failed_start = start;
818 err = -EEXIST;
819 goto out;
820 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000821
822 prealloc = alloc_extent_state_atomic(prealloc);
823 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500824 err = split_state(tree, state, prealloc, start);
825 BUG_ON(err == -EEXIST);
826 prealloc = NULL;
827 if (err)
828 goto out;
829 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400830 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400831 if (err)
832 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400833 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500834 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400835 if (last_end == (u64)-1)
836 goto out;
837 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500838 }
839 goto search_again;
840 }
841 /*
842 * | ---- desired range ---- |
843 * | state | or | state |
844 *
845 * There's a hole, we need to insert something in it and
846 * ignore the extent we found.
847 */
848 if (state->start > start) {
849 u64 this_end;
850 if (end < last_start)
851 this_end = end;
852 else
Chris Masond3977122009-01-05 21:25:51 -0500853 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000854
855 prealloc = alloc_extent_state_atomic(prealloc);
856 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000857
858 /*
859 * Avoid to free 'prealloc' if it can be merged with
860 * the later extent.
861 */
862 atomic_inc(&prealloc->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500863 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400864 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400865 BUG_ON(err == -EEXIST);
866 if (err) {
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000867 free_extent_state(prealloc);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400868 prealloc = NULL;
869 goto out;
870 }
Chris Mason2c64c532009-09-02 15:04:12 -0400871 cache_state(prealloc, cached_state);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000872 free_extent_state(prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500873 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500874 start = this_end + 1;
875 goto search_again;
876 }
877 /*
878 * | ---- desired range ---- |
879 * | state |
880 * We need to split the extent, and set the bit
881 * on the first half
882 */
883 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400884 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500885 *failed_start = start;
886 err = -EEXIST;
887 goto out;
888 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000889
890 prealloc = alloc_extent_state_atomic(prealloc);
891 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500892 err = split_state(tree, state, prealloc, end + 1);
893 BUG_ON(err == -EEXIST);
894
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400895 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400896 if (err) {
897 prealloc = NULL;
898 goto out;
899 }
Chris Mason2c64c532009-09-02 15:04:12 -0400900 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500901 merge_state(tree, prealloc);
902 prealloc = NULL;
903 goto out;
904 }
905
906 goto search_again;
907
908out:
Chris Masoncad321a2008-12-17 14:51:42 -0500909 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500910 if (prealloc)
911 free_extent_state(prealloc);
912
913 return err;
914
915search_again:
916 if (start > end)
917 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500918 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500919 if (mask & __GFP_WAIT)
920 cond_resched();
921 goto again;
922}
Chris Masond1310b22008-01-24 16:13:08 -0500923
924/* wrappers around set/clear extent bit */
925int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
926 gfp_t mask)
927{
928 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400929 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500930}
Chris Masond1310b22008-01-24 16:13:08 -0500931
932int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
933 int bits, gfp_t mask)
934{
935 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400936 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500937}
Chris Masond1310b22008-01-24 16:13:08 -0500938
939int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
940 int bits, gfp_t mask)
941{
Chris Mason2c64c532009-09-02 15:04:12 -0400942 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500943}
Chris Masond1310b22008-01-24 16:13:08 -0500944
945int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000946 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500947{
948 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400949 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000950 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500951}
Chris Masond1310b22008-01-24 16:13:08 -0500952
953int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
954 gfp_t mask)
955{
956 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400957 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400958 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500959}
Chris Masond1310b22008-01-24 16:13:08 -0500960
961int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
962 gfp_t mask)
963{
964 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400965 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500966}
Chris Masond1310b22008-01-24 16:13:08 -0500967
Christoph Hellwigb2950862008-12-02 09:54:17 -0500968static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500969 gfp_t mask)
970{
Chris Mason2c64c532009-09-02 15:04:12 -0400971 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
972 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500973}
Chris Masond1310b22008-01-24 16:13:08 -0500974
975int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000976 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500977{
Arne Jansen507903b2011-04-06 10:02:20 +0000978 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
979 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500980}
Chris Masond1310b22008-01-24 16:13:08 -0500981
Chris Masond3977122009-01-05 21:25:51 -0500982static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000983 u64 end, struct extent_state **cached_state,
984 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500985{
Chris Mason2c64c532009-09-02 15:04:12 -0400986 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000987 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500988}
Chris Masond1310b22008-01-24 16:13:08 -0500989
Chris Masond1310b22008-01-24 16:13:08 -0500990int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
991{
992 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
993}
Chris Masond1310b22008-01-24 16:13:08 -0500994
Chris Masond352ac62008-09-29 15:18:18 -0400995/*
996 * either insert or lock state struct between start and end use mask to tell
997 * us if waiting is desired.
998 */
Chris Mason1edbb732009-09-02 13:24:36 -0400999int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001000 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001001{
1002 int err;
1003 u64 failed_start;
1004 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001005 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001006 EXTENT_LOCKED, &failed_start,
1007 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001008 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1009 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1010 start = failed_start;
1011 } else {
1012 break;
1013 }
1014 WARN_ON(start > end);
1015 }
1016 return err;
1017}
Chris Masond1310b22008-01-24 16:13:08 -05001018
Chris Mason1edbb732009-09-02 13:24:36 -04001019int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1020{
Chris Mason2c64c532009-09-02 15:04:12 -04001021 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001022}
1023
Josef Bacik25179202008-10-29 14:49:05 -04001024int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1025 gfp_t mask)
1026{
1027 int err;
1028 u64 failed_start;
1029
Chris Mason2c64c532009-09-02 15:04:12 -04001030 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1031 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001032 if (err == -EEXIST) {
1033 if (failed_start > start)
1034 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001035 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001036 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001037 }
Josef Bacik25179202008-10-29 14:49:05 -04001038 return 1;
1039}
Josef Bacik25179202008-10-29 14:49:05 -04001040
Chris Mason2c64c532009-09-02 15:04:12 -04001041int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1042 struct extent_state **cached, gfp_t mask)
1043{
1044 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1045 mask);
1046}
1047
Arne Jansen507903b2011-04-06 10:02:20 +00001048int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001049{
Chris Mason2c64c532009-09-02 15:04:12 -04001050 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1051 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001052}
Chris Masond1310b22008-01-24 16:13:08 -05001053
1054/*
1055 * helper function to set pages and extents in the tree dirty
1056 */
1057int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1058{
1059 unsigned long index = start >> PAGE_CACHE_SHIFT;
1060 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1061 struct page *page;
1062
1063 while (index <= end_index) {
1064 page = find_get_page(tree->mapping, index);
1065 BUG_ON(!page);
1066 __set_page_dirty_nobuffers(page);
1067 page_cache_release(page);
1068 index++;
1069 }
Chris Masond1310b22008-01-24 16:13:08 -05001070 return 0;
1071}
Chris Masond1310b22008-01-24 16:13:08 -05001072
1073/*
1074 * helper function to set both pages and extents in the tree writeback
1075 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001076static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001077{
1078 unsigned long index = start >> PAGE_CACHE_SHIFT;
1079 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1080 struct page *page;
1081
1082 while (index <= end_index) {
1083 page = find_get_page(tree->mapping, index);
1084 BUG_ON(!page);
1085 set_page_writeback(page);
1086 page_cache_release(page);
1087 index++;
1088 }
Chris Masond1310b22008-01-24 16:13:08 -05001089 return 0;
1090}
Chris Masond1310b22008-01-24 16:13:08 -05001091
Chris Masond352ac62008-09-29 15:18:18 -04001092/*
1093 * find the first offset in the io tree with 'bits' set. zero is
1094 * returned if we find something, and *start_ret and *end_ret are
1095 * set to reflect the state struct that was found.
1096 *
1097 * If nothing was found, 1 is returned, < 0 on error
1098 */
Chris Masond1310b22008-01-24 16:13:08 -05001099int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1100 u64 *start_ret, u64 *end_ret, int bits)
1101{
1102 struct rb_node *node;
1103 struct extent_state *state;
1104 int ret = 1;
1105
Chris Masoncad321a2008-12-17 14:51:42 -05001106 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001107 /*
1108 * this search will find all the extents that end after
1109 * our range starts.
1110 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001111 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001112 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001113 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001114
Chris Masond3977122009-01-05 21:25:51 -05001115 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001116 state = rb_entry(node, struct extent_state, rb_node);
1117 if (state->end >= start && (state->state & bits)) {
1118 *start_ret = state->start;
1119 *end_ret = state->end;
1120 ret = 0;
1121 break;
1122 }
1123 node = rb_next(node);
1124 if (!node)
1125 break;
1126 }
1127out:
Chris Masoncad321a2008-12-17 14:51:42 -05001128 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001129 return ret;
1130}
Chris Masond1310b22008-01-24 16:13:08 -05001131
Chris Masond352ac62008-09-29 15:18:18 -04001132/* find the first state struct with 'bits' set after 'start', and
1133 * return it. tree->lock must be held. NULL will returned if
1134 * nothing was found after 'start'
1135 */
Chris Masond7fc6402008-02-18 12:12:38 -05001136struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1137 u64 start, int bits)
1138{
1139 struct rb_node *node;
1140 struct extent_state *state;
1141
1142 /*
1143 * this search will find all the extents that end after
1144 * our range starts.
1145 */
1146 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001147 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001148 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001149
Chris Masond3977122009-01-05 21:25:51 -05001150 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001151 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001152 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001153 return state;
Chris Masond3977122009-01-05 21:25:51 -05001154
Chris Masond7fc6402008-02-18 12:12:38 -05001155 node = rb_next(node);
1156 if (!node)
1157 break;
1158 }
1159out:
1160 return NULL;
1161}
Chris Masond7fc6402008-02-18 12:12:38 -05001162
Chris Masond352ac62008-09-29 15:18:18 -04001163/*
1164 * find a contiguous range of bytes in the file marked as delalloc, not
1165 * more than 'max_bytes'. start and end are used to return the range,
1166 *
1167 * 1 is returned if we find something, 0 if nothing was in the tree
1168 */
Chris Masonc8b97812008-10-29 14:49:59 -04001169static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001170 u64 *start, u64 *end, u64 max_bytes,
1171 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001172{
1173 struct rb_node *node;
1174 struct extent_state *state;
1175 u64 cur_start = *start;
1176 u64 found = 0;
1177 u64 total_bytes = 0;
1178
Chris Masoncad321a2008-12-17 14:51:42 -05001179 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001180
Chris Masond1310b22008-01-24 16:13:08 -05001181 /*
1182 * this search will find all the extents that end after
1183 * our range starts.
1184 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001185 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001186 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001187 if (!found)
1188 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001189 goto out;
1190 }
1191
Chris Masond3977122009-01-05 21:25:51 -05001192 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001193 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001194 if (found && (state->start != cur_start ||
1195 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001196 goto out;
1197 }
1198 if (!(state->state & EXTENT_DELALLOC)) {
1199 if (!found)
1200 *end = state->end;
1201 goto out;
1202 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001203 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001204 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001205 *cached_state = state;
1206 atomic_inc(&state->refs);
1207 }
Chris Masond1310b22008-01-24 16:13:08 -05001208 found++;
1209 *end = state->end;
1210 cur_start = state->end + 1;
1211 node = rb_next(node);
1212 if (!node)
1213 break;
1214 total_bytes += state->end - state->start + 1;
1215 if (total_bytes >= max_bytes)
1216 break;
1217 }
1218out:
Chris Masoncad321a2008-12-17 14:51:42 -05001219 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001220 return found;
1221}
1222
Chris Masonc8b97812008-10-29 14:49:59 -04001223static noinline int __unlock_for_delalloc(struct inode *inode,
1224 struct page *locked_page,
1225 u64 start, u64 end)
1226{
1227 int ret;
1228 struct page *pages[16];
1229 unsigned long index = start >> PAGE_CACHE_SHIFT;
1230 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1231 unsigned long nr_pages = end_index - index + 1;
1232 int i;
1233
1234 if (index == locked_page->index && end_index == index)
1235 return 0;
1236
Chris Masond3977122009-01-05 21:25:51 -05001237 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001238 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001239 min_t(unsigned long, nr_pages,
1240 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001241 for (i = 0; i < ret; i++) {
1242 if (pages[i] != locked_page)
1243 unlock_page(pages[i]);
1244 page_cache_release(pages[i]);
1245 }
1246 nr_pages -= ret;
1247 index += ret;
1248 cond_resched();
1249 }
1250 return 0;
1251}
1252
1253static noinline int lock_delalloc_pages(struct inode *inode,
1254 struct page *locked_page,
1255 u64 delalloc_start,
1256 u64 delalloc_end)
1257{
1258 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1259 unsigned long start_index = index;
1260 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1261 unsigned long pages_locked = 0;
1262 struct page *pages[16];
1263 unsigned long nrpages;
1264 int ret;
1265 int i;
1266
1267 /* the caller is responsible for locking the start index */
1268 if (index == locked_page->index && index == end_index)
1269 return 0;
1270
1271 /* skip the page at the start index */
1272 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001273 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001274 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001275 min_t(unsigned long,
1276 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001277 if (ret == 0) {
1278 ret = -EAGAIN;
1279 goto done;
1280 }
1281 /* now we have an array of pages, lock them all */
1282 for (i = 0; i < ret; i++) {
1283 /*
1284 * the caller is taking responsibility for
1285 * locked_page
1286 */
Chris Mason771ed682008-11-06 22:02:51 -05001287 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001288 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001289 if (!PageDirty(pages[i]) ||
1290 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001291 ret = -EAGAIN;
1292 unlock_page(pages[i]);
1293 page_cache_release(pages[i]);
1294 goto done;
1295 }
1296 }
Chris Masonc8b97812008-10-29 14:49:59 -04001297 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001298 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001299 }
Chris Masonc8b97812008-10-29 14:49:59 -04001300 nrpages -= ret;
1301 index += ret;
1302 cond_resched();
1303 }
1304 ret = 0;
1305done:
1306 if (ret && pages_locked) {
1307 __unlock_for_delalloc(inode, locked_page,
1308 delalloc_start,
1309 ((u64)(start_index + pages_locked - 1)) <<
1310 PAGE_CACHE_SHIFT);
1311 }
1312 return ret;
1313}
1314
1315/*
1316 * find a contiguous range of bytes in the file marked as delalloc, not
1317 * more than 'max_bytes'. start and end are used to return the range,
1318 *
1319 * 1 is returned if we find something, 0 if nothing was in the tree
1320 */
1321static noinline u64 find_lock_delalloc_range(struct inode *inode,
1322 struct extent_io_tree *tree,
1323 struct page *locked_page,
1324 u64 *start, u64 *end,
1325 u64 max_bytes)
1326{
1327 u64 delalloc_start;
1328 u64 delalloc_end;
1329 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001330 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001331 int ret;
1332 int loops = 0;
1333
1334again:
1335 /* step one, find a bunch of delalloc bytes starting at start */
1336 delalloc_start = *start;
1337 delalloc_end = 0;
1338 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001339 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001340 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001341 *start = delalloc_start;
1342 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001343 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001344 return found;
1345 }
1346
1347 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001348 * start comes from the offset of locked_page. We have to lock
1349 * pages in order, so we can't process delalloc bytes before
1350 * locked_page
1351 */
Chris Masond3977122009-01-05 21:25:51 -05001352 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001353 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001354
1355 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001356 * make sure to limit the number of pages we try to lock down
1357 * if we're looping.
1358 */
Chris Masond3977122009-01-05 21:25:51 -05001359 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001360 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001361
Chris Masonc8b97812008-10-29 14:49:59 -04001362 /* step two, lock all the pages after the page that has start */
1363 ret = lock_delalloc_pages(inode, locked_page,
1364 delalloc_start, delalloc_end);
1365 if (ret == -EAGAIN) {
1366 /* some of the pages are gone, lets avoid looping by
1367 * shortening the size of the delalloc range we're searching
1368 */
Chris Mason9655d292009-09-02 15:22:30 -04001369 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001370 if (!loops) {
1371 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1372 max_bytes = PAGE_CACHE_SIZE - offset;
1373 loops = 1;
1374 goto again;
1375 } else {
1376 found = 0;
1377 goto out_failed;
1378 }
1379 }
1380 BUG_ON(ret);
1381
1382 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001383 lock_extent_bits(tree, delalloc_start, delalloc_end,
1384 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001385
1386 /* then test to make sure it is all still delalloc */
1387 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001388 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001389 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001390 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1391 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001392 __unlock_for_delalloc(inode, locked_page,
1393 delalloc_start, delalloc_end);
1394 cond_resched();
1395 goto again;
1396 }
Chris Mason9655d292009-09-02 15:22:30 -04001397 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001398 *start = delalloc_start;
1399 *end = delalloc_end;
1400out_failed:
1401 return found;
1402}
1403
1404int extent_clear_unlock_delalloc(struct inode *inode,
1405 struct extent_io_tree *tree,
1406 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001407 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001408{
1409 int ret;
1410 struct page *pages[16];
1411 unsigned long index = start >> PAGE_CACHE_SHIFT;
1412 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1413 unsigned long nr_pages = end_index - index + 1;
1414 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001415 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001416
Chris Masona791e352009-10-08 11:27:10 -04001417 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001418 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001419 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001420 clear_bits |= EXTENT_DIRTY;
1421
Chris Masona791e352009-10-08 11:27:10 -04001422 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001423 clear_bits |= EXTENT_DELALLOC;
1424
Chris Mason2c64c532009-09-02 15:04:12 -04001425 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001426 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1427 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1428 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001429 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001430
Chris Masond3977122009-01-05 21:25:51 -05001431 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001432 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001433 min_t(unsigned long,
1434 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001435 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001436
Chris Masona791e352009-10-08 11:27:10 -04001437 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001438 SetPagePrivate2(pages[i]);
1439
Chris Masonc8b97812008-10-29 14:49:59 -04001440 if (pages[i] == locked_page) {
1441 page_cache_release(pages[i]);
1442 continue;
1443 }
Chris Masona791e352009-10-08 11:27:10 -04001444 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001445 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001446 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001447 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001448 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001449 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001450 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001451 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001452 page_cache_release(pages[i]);
1453 }
1454 nr_pages -= ret;
1455 index += ret;
1456 cond_resched();
1457 }
1458 return 0;
1459}
Chris Masonc8b97812008-10-29 14:49:59 -04001460
Chris Masond352ac62008-09-29 15:18:18 -04001461/*
1462 * count the number of bytes in the tree that have a given bit(s)
1463 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1464 * cached. The total number found is returned.
1465 */
Chris Masond1310b22008-01-24 16:13:08 -05001466u64 count_range_bits(struct extent_io_tree *tree,
1467 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001468 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001469{
1470 struct rb_node *node;
1471 struct extent_state *state;
1472 u64 cur_start = *start;
1473 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001474 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001475 int found = 0;
1476
1477 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001478 WARN_ON(1);
1479 return 0;
1480 }
1481
Chris Masoncad321a2008-12-17 14:51:42 -05001482 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001483 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1484 total_bytes = tree->dirty_bytes;
1485 goto out;
1486 }
1487 /*
1488 * this search will find all the extents that end after
1489 * our range starts.
1490 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001491 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001492 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001493 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001494
Chris Masond3977122009-01-05 21:25:51 -05001495 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001496 state = rb_entry(node, struct extent_state, rb_node);
1497 if (state->start > search_end)
1498 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001499 if (contig && found && state->start > last + 1)
1500 break;
1501 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001502 total_bytes += min(search_end, state->end) + 1 -
1503 max(cur_start, state->start);
1504 if (total_bytes >= max_bytes)
1505 break;
1506 if (!found) {
1507 *start = state->start;
1508 found = 1;
1509 }
Chris Masonec29ed52011-02-23 16:23:20 -05001510 last = state->end;
1511 } else if (contig && found) {
1512 break;
Chris Masond1310b22008-01-24 16:13:08 -05001513 }
1514 node = rb_next(node);
1515 if (!node)
1516 break;
1517 }
1518out:
Chris Masoncad321a2008-12-17 14:51:42 -05001519 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001520 return total_bytes;
1521}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001522
Chris Masond352ac62008-09-29 15:18:18 -04001523/*
1524 * set the private field for a given byte offset in the tree. If there isn't
1525 * an extent_state there already, this does nothing.
1526 */
Chris Masond1310b22008-01-24 16:13:08 -05001527int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1528{
1529 struct rb_node *node;
1530 struct extent_state *state;
1531 int ret = 0;
1532
Chris Masoncad321a2008-12-17 14:51:42 -05001533 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001534 /*
1535 * this search will find all the extents that end after
1536 * our range starts.
1537 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001538 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001539 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001540 ret = -ENOENT;
1541 goto out;
1542 }
1543 state = rb_entry(node, struct extent_state, rb_node);
1544 if (state->start != start) {
1545 ret = -ENOENT;
1546 goto out;
1547 }
1548 state->private = private;
1549out:
Chris Masoncad321a2008-12-17 14:51:42 -05001550 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001551 return ret;
1552}
1553
1554int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1555{
1556 struct rb_node *node;
1557 struct extent_state *state;
1558 int ret = 0;
1559
Chris Masoncad321a2008-12-17 14:51:42 -05001560 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001561 /*
1562 * this search will find all the extents that end after
1563 * our range starts.
1564 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001565 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001566 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001567 ret = -ENOENT;
1568 goto out;
1569 }
1570 state = rb_entry(node, struct extent_state, rb_node);
1571 if (state->start != start) {
1572 ret = -ENOENT;
1573 goto out;
1574 }
1575 *private = state->private;
1576out:
Chris Masoncad321a2008-12-17 14:51:42 -05001577 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001578 return ret;
1579}
1580
1581/*
1582 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001583 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001584 * has the bits set. Otherwise, 1 is returned if any bit in the
1585 * range is found set.
1586 */
1587int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001588 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001589{
1590 struct extent_state *state = NULL;
1591 struct rb_node *node;
1592 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001593
Chris Masoncad321a2008-12-17 14:51:42 -05001594 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001595 if (cached && cached->tree && cached->start == start)
1596 node = &cached->rb_node;
1597 else
1598 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001599 while (node && start <= end) {
1600 state = rb_entry(node, struct extent_state, rb_node);
1601
1602 if (filled && state->start > start) {
1603 bitset = 0;
1604 break;
1605 }
1606
1607 if (state->start > end)
1608 break;
1609
1610 if (state->state & bits) {
1611 bitset = 1;
1612 if (!filled)
1613 break;
1614 } else if (filled) {
1615 bitset = 0;
1616 break;
1617 }
Chris Mason46562cec2009-09-23 20:23:16 -04001618
1619 if (state->end == (u64)-1)
1620 break;
1621
Chris Masond1310b22008-01-24 16:13:08 -05001622 start = state->end + 1;
1623 if (start > end)
1624 break;
1625 node = rb_next(node);
1626 if (!node) {
1627 if (filled)
1628 bitset = 0;
1629 break;
1630 }
1631 }
Chris Masoncad321a2008-12-17 14:51:42 -05001632 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001633 return bitset;
1634}
Chris Masond1310b22008-01-24 16:13:08 -05001635
1636/*
1637 * helper function to set a given page up to date if all the
1638 * extents in the tree for that page are up to date
1639 */
1640static int check_page_uptodate(struct extent_io_tree *tree,
1641 struct page *page)
1642{
1643 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1644 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001645 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001646 SetPageUptodate(page);
1647 return 0;
1648}
1649
1650/*
1651 * helper function to unlock a page if all the extents in the tree
1652 * for that page are unlocked
1653 */
1654static int check_page_locked(struct extent_io_tree *tree,
1655 struct page *page)
1656{
1657 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1658 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001659 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001660 unlock_page(page);
1661 return 0;
1662}
1663
1664/*
1665 * helper function to end page writeback if all the extents
1666 * in the tree for that page are done with writeback
1667 */
1668static int check_page_writeback(struct extent_io_tree *tree,
1669 struct page *page)
1670{
Chris Mason1edbb732009-09-02 13:24:36 -04001671 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001672 return 0;
1673}
1674
1675/* lots and lots of room for performance fixes in the end_bio funcs */
1676
1677/*
1678 * after a writepage IO is done, we need to:
1679 * clear the uptodate bits on error
1680 * clear the writeback bits in the extent tree for this IO
1681 * end_page_writeback if the page has no more pending IO
1682 *
1683 * Scheduling is not allowed, so the extent state tree is expected
1684 * to have one and only one object corresponding to this IO.
1685 */
Chris Masond1310b22008-01-24 16:13:08 -05001686static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001687{
Chris Mason1259ab72008-05-12 13:39:03 -04001688 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001689 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001690 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001691 u64 start;
1692 u64 end;
1693 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001694 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001695
Chris Masond1310b22008-01-24 16:13:08 -05001696 do {
1697 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001698 tree = &BTRFS_I(page->mapping->host)->io_tree;
1699
Chris Masond1310b22008-01-24 16:13:08 -05001700 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1701 bvec->bv_offset;
1702 end = start + bvec->bv_len - 1;
1703
1704 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1705 whole_page = 1;
1706 else
1707 whole_page = 0;
1708
1709 if (--bvec >= bio->bi_io_vec)
1710 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001711 if (tree->ops && tree->ops->writepage_end_io_hook) {
1712 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001713 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001714 if (ret)
1715 uptodate = 0;
1716 }
1717
1718 if (!uptodate && tree->ops &&
1719 tree->ops->writepage_io_failed_hook) {
1720 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001721 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001722 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001723 uptodate = (err == 0);
1724 continue;
1725 }
1726 }
1727
Chris Masond1310b22008-01-24 16:13:08 -05001728 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001729 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001730 ClearPageUptodate(page);
1731 SetPageError(page);
1732 }
Chris Mason70dec802008-01-29 09:59:12 -05001733
Chris Masond1310b22008-01-24 16:13:08 -05001734 if (whole_page)
1735 end_page_writeback(page);
1736 else
1737 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001738 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001739
Chris Masond1310b22008-01-24 16:13:08 -05001740 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001741}
1742
1743/*
1744 * after a readpage IO is done, we need to:
1745 * clear the uptodate bits on error
1746 * set the uptodate bits if things worked
1747 * set the page up to date if all extents in the tree are uptodate
1748 * clear the lock bit in the extent tree
1749 * unlock the page if there are no other extents locked for it
1750 *
1751 * Scheduling is not allowed, so the extent state tree is expected
1752 * to have one and only one object corresponding to this IO.
1753 */
Chris Masond1310b22008-01-24 16:13:08 -05001754static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001755{
1756 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001757 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1758 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001759 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001760 u64 start;
1761 u64 end;
1762 int whole_page;
1763 int ret;
1764
Chris Masond20f7042008-12-08 16:58:54 -05001765 if (err)
1766 uptodate = 0;
1767
Chris Masond1310b22008-01-24 16:13:08 -05001768 do {
1769 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001770 struct extent_state *cached = NULL;
1771 struct extent_state *state;
1772
David Woodhouse902b22f2008-08-20 08:51:49 -04001773 tree = &BTRFS_I(page->mapping->host)->io_tree;
1774
Chris Masond1310b22008-01-24 16:13:08 -05001775 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1776 bvec->bv_offset;
1777 end = start + bvec->bv_len - 1;
1778
1779 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1780 whole_page = 1;
1781 else
1782 whole_page = 0;
1783
Chris Mason4125bf72010-02-03 18:18:45 +00001784 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001785 prefetchw(&bvec->bv_page->flags);
1786
Arne Jansen507903b2011-04-06 10:02:20 +00001787 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001788 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001789 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001790 /*
1791 * take a reference on the state, unlock will drop
1792 * the ref
1793 */
1794 cache_state(state, &cached);
1795 }
1796 spin_unlock(&tree->lock);
1797
Chris Masond1310b22008-01-24 16:13:08 -05001798 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001799 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001800 state);
Chris Masond1310b22008-01-24 16:13:08 -05001801 if (ret)
1802 uptodate = 0;
1803 }
Chris Mason7e383262008-04-09 16:28:12 -04001804 if (!uptodate && tree->ops &&
1805 tree->ops->readpage_io_failed_hook) {
1806 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001807 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001808 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001809 uptodate =
1810 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001811 if (err)
1812 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001813 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001814 continue;
1815 }
1816 }
Chris Mason70dec802008-01-29 09:59:12 -05001817
Chris Mason771ed682008-11-06 22:02:51 -05001818 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001819 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001820 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001821 }
Arne Jansen507903b2011-04-06 10:02:20 +00001822 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001823
Chris Mason70dec802008-01-29 09:59:12 -05001824 if (whole_page) {
1825 if (uptodate) {
1826 SetPageUptodate(page);
1827 } else {
1828 ClearPageUptodate(page);
1829 SetPageError(page);
1830 }
Chris Masond1310b22008-01-24 16:13:08 -05001831 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001832 } else {
1833 if (uptodate) {
1834 check_page_uptodate(tree, page);
1835 } else {
1836 ClearPageUptodate(page);
1837 SetPageError(page);
1838 }
Chris Masond1310b22008-01-24 16:13:08 -05001839 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001840 }
Chris Mason4125bf72010-02-03 18:18:45 +00001841 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001842
1843 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001844}
1845
1846/*
1847 * IO done from prepare_write is pretty simple, we just unlock
1848 * the structs in the extent tree when done, and set the uptodate bits
1849 * as appropriate.
1850 */
Chris Masond1310b22008-01-24 16:13:08 -05001851static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001852{
1853 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1854 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001855 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001856 u64 start;
1857 u64 end;
1858
Chris Masond1310b22008-01-24 16:13:08 -05001859 do {
1860 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001861 struct extent_state *cached = NULL;
David Woodhouse902b22f2008-08-20 08:51:49 -04001862 tree = &BTRFS_I(page->mapping->host)->io_tree;
1863
Chris Masond1310b22008-01-24 16:13:08 -05001864 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1865 bvec->bv_offset;
1866 end = start + bvec->bv_len - 1;
1867
1868 if (--bvec >= bio->bi_io_vec)
1869 prefetchw(&bvec->bv_page->flags);
1870
1871 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001872 set_extent_uptodate(tree, start, end, &cached,
1873 GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001874 } else {
1875 ClearPageUptodate(page);
1876 SetPageError(page);
1877 }
1878
Arne Jansen507903b2011-04-06 10:02:20 +00001879 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001880
1881 } while (bvec >= bio->bi_io_vec);
1882
1883 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001884}
1885
Miao Xie88f794e2010-11-22 03:02:55 +00001886struct bio *
1887btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1888 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001889{
1890 struct bio *bio;
1891
1892 bio = bio_alloc(gfp_flags, nr_vecs);
1893
1894 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1895 while (!bio && (nr_vecs /= 2))
1896 bio = bio_alloc(gfp_flags, nr_vecs);
1897 }
1898
1899 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001900 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001901 bio->bi_bdev = bdev;
1902 bio->bi_sector = first_sector;
1903 }
1904 return bio;
1905}
1906
Chris Masonc8b97812008-10-29 14:49:59 -04001907static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1908 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001909{
Chris Masond1310b22008-01-24 16:13:08 -05001910 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001911 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1912 struct page *page = bvec->bv_page;
1913 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001914 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001915
1916 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001917
David Woodhouse902b22f2008-08-20 08:51:49 -04001918 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001919
1920 bio_get(bio);
1921
Chris Mason065631f2008-02-20 12:07:25 -05001922 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001923 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001924 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001925 else
1926 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001927 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1928 ret = -EOPNOTSUPP;
1929 bio_put(bio);
1930 return ret;
1931}
1932
1933static int submit_extent_page(int rw, struct extent_io_tree *tree,
1934 struct page *page, sector_t sector,
1935 size_t size, unsigned long offset,
1936 struct block_device *bdev,
1937 struct bio **bio_ret,
1938 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001939 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001940 int mirror_num,
1941 unsigned long prev_bio_flags,
1942 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001943{
1944 int ret = 0;
1945 struct bio *bio;
1946 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001947 int contig = 0;
1948 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1949 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001950 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001951
1952 if (bio_ret && *bio_ret) {
1953 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001954 if (old_compressed)
1955 contig = bio->bi_sector == sector;
1956 else
1957 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1958 sector;
1959
1960 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001961 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001962 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1963 bio_flags)) ||
1964 bio_add_page(bio, page, page_size, offset) < page_size) {
1965 ret = submit_one_bio(rw, bio, mirror_num,
1966 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001967 bio = NULL;
1968 } else {
1969 return 0;
1970 }
1971 }
Chris Masonc8b97812008-10-29 14:49:59 -04001972 if (this_compressed)
1973 nr = BIO_MAX_PAGES;
1974 else
1975 nr = bio_get_nr_vecs(bdev);
1976
Miao Xie88f794e2010-11-22 03:02:55 +00001977 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001978 if (!bio)
1979 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001980
Chris Masonc8b97812008-10-29 14:49:59 -04001981 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001982 bio->bi_end_io = end_io_func;
1983 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001984
Chris Masond3977122009-01-05 21:25:51 -05001985 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001986 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001987 else
Chris Masonc8b97812008-10-29 14:49:59 -04001988 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001989
1990 return ret;
1991}
1992
1993void set_page_extent_mapped(struct page *page)
1994{
1995 if (!PagePrivate(page)) {
1996 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001997 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001998 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001999 }
2000}
2001
Christoph Hellwigb2950862008-12-02 09:54:17 -05002002static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002003{
Chris Masoneb14ab82011-02-10 12:35:00 -05002004 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002005 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2006}
2007
2008/*
2009 * basic readpage implementation. Locked extent state structs are inserted
2010 * into the tree that are removed when the IO is done (by the end_io
2011 * handlers)
2012 */
2013static int __extent_read_full_page(struct extent_io_tree *tree,
2014 struct page *page,
2015 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002016 struct bio **bio, int mirror_num,
2017 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002018{
2019 struct inode *inode = page->mapping->host;
2020 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2021 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2022 u64 end;
2023 u64 cur = start;
2024 u64 extent_offset;
2025 u64 last_byte = i_size_read(inode);
2026 u64 block_start;
2027 u64 cur_end;
2028 sector_t sector;
2029 struct extent_map *em;
2030 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002031 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002032 int ret;
2033 int nr = 0;
2034 size_t page_offset = 0;
2035 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002036 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002037 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002038 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002039
2040 set_page_extent_mapped(page);
2041
2042 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002043 while (1) {
2044 lock_extent(tree, start, end, GFP_NOFS);
2045 ordered = btrfs_lookup_ordered_extent(inode, start);
2046 if (!ordered)
2047 break;
2048 unlock_extent(tree, start, end, GFP_NOFS);
2049 btrfs_start_ordered_extent(inode, ordered, 1);
2050 btrfs_put_ordered_extent(ordered);
2051 }
Chris Masond1310b22008-01-24 16:13:08 -05002052
Chris Masonc8b97812008-10-29 14:49:59 -04002053 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2054 char *userpage;
2055 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2056
2057 if (zero_offset) {
2058 iosize = PAGE_CACHE_SIZE - zero_offset;
2059 userpage = kmap_atomic(page, KM_USER0);
2060 memset(userpage + zero_offset, 0, iosize);
2061 flush_dcache_page(page);
2062 kunmap_atomic(userpage, KM_USER0);
2063 }
2064 }
Chris Masond1310b22008-01-24 16:13:08 -05002065 while (cur <= end) {
2066 if (cur >= last_byte) {
2067 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002068 struct extent_state *cached = NULL;
2069
Chris Masond1310b22008-01-24 16:13:08 -05002070 iosize = PAGE_CACHE_SIZE - page_offset;
2071 userpage = kmap_atomic(page, KM_USER0);
2072 memset(userpage + page_offset, 0, iosize);
2073 flush_dcache_page(page);
2074 kunmap_atomic(userpage, KM_USER0);
2075 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002076 &cached, GFP_NOFS);
2077 unlock_extent_cached(tree, cur, cur + iosize - 1,
2078 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002079 break;
2080 }
2081 em = get_extent(inode, page, page_offset, cur,
2082 end - cur + 1, 0);
2083 if (IS_ERR(em) || !em) {
2084 SetPageError(page);
2085 unlock_extent(tree, cur, end, GFP_NOFS);
2086 break;
2087 }
Chris Masond1310b22008-01-24 16:13:08 -05002088 extent_offset = cur - em->start;
2089 BUG_ON(extent_map_end(em) <= cur);
2090 BUG_ON(end < cur);
2091
Li Zefan261507a02010-12-17 14:21:50 +08002092 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002093 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002094 extent_set_compress_type(&this_bio_flag,
2095 em->compress_type);
2096 }
Chris Masonc8b97812008-10-29 14:49:59 -04002097
Chris Masond1310b22008-01-24 16:13:08 -05002098 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2099 cur_end = min(extent_map_end(em) - 1, end);
2100 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002101 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2102 disk_io_size = em->block_len;
2103 sector = em->block_start >> 9;
2104 } else {
2105 sector = (em->block_start + extent_offset) >> 9;
2106 disk_io_size = iosize;
2107 }
Chris Masond1310b22008-01-24 16:13:08 -05002108 bdev = em->bdev;
2109 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002110 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2111 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002112 free_extent_map(em);
2113 em = NULL;
2114
2115 /* we've found a hole, just zero and go on */
2116 if (block_start == EXTENT_MAP_HOLE) {
2117 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002118 struct extent_state *cached = NULL;
2119
Chris Masond1310b22008-01-24 16:13:08 -05002120 userpage = kmap_atomic(page, KM_USER0);
2121 memset(userpage + page_offset, 0, iosize);
2122 flush_dcache_page(page);
2123 kunmap_atomic(userpage, KM_USER0);
2124
2125 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002126 &cached, GFP_NOFS);
2127 unlock_extent_cached(tree, cur, cur + iosize - 1,
2128 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002129 cur = cur + iosize;
2130 page_offset += iosize;
2131 continue;
2132 }
2133 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002134 if (test_range_bit(tree, cur, cur_end,
2135 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002136 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002137 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2138 cur = cur + iosize;
2139 page_offset += iosize;
2140 continue;
2141 }
Chris Mason70dec802008-01-29 09:59:12 -05002142 /* we have an inline extent but it didn't get marked up
2143 * to date. Error out
2144 */
2145 if (block_start == EXTENT_MAP_INLINE) {
2146 SetPageError(page);
2147 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2148 cur = cur + iosize;
2149 page_offset += iosize;
2150 continue;
2151 }
Chris Masond1310b22008-01-24 16:13:08 -05002152
2153 ret = 0;
2154 if (tree->ops && tree->ops->readpage_io_hook) {
2155 ret = tree->ops->readpage_io_hook(page, cur,
2156 cur + iosize - 1);
2157 }
2158 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002159 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2160 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002161 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002162 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002163 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002164 end_bio_extent_readpage, mirror_num,
2165 *bio_flags,
2166 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002167 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002168 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002169 }
2170 if (ret)
2171 SetPageError(page);
2172 cur = cur + iosize;
2173 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002174 }
2175 if (!nr) {
2176 if (!PageError(page))
2177 SetPageUptodate(page);
2178 unlock_page(page);
2179 }
2180 return 0;
2181}
2182
2183int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2184 get_extent_t *get_extent)
2185{
2186 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002187 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002188 int ret;
2189
Chris Masonc8b97812008-10-29 14:49:59 -04002190 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2191 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002192 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002193 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002194 return ret;
2195}
Chris Masond1310b22008-01-24 16:13:08 -05002196
Chris Mason11c83492009-04-20 15:50:09 -04002197static noinline void update_nr_written(struct page *page,
2198 struct writeback_control *wbc,
2199 unsigned long nr_written)
2200{
2201 wbc->nr_to_write -= nr_written;
2202 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2203 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2204 page->mapping->writeback_index = page->index + nr_written;
2205}
2206
Chris Masond1310b22008-01-24 16:13:08 -05002207/*
2208 * the writepage semantics are similar to regular writepage. extent
2209 * records are inserted to lock ranges in the tree, and as dirty areas
2210 * are found, they are marked writeback. Then the lock bits are removed
2211 * and the end_io handler clears the writeback ranges
2212 */
2213static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2214 void *data)
2215{
2216 struct inode *inode = page->mapping->host;
2217 struct extent_page_data *epd = data;
2218 struct extent_io_tree *tree = epd->tree;
2219 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2220 u64 delalloc_start;
2221 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2222 u64 end;
2223 u64 cur = start;
2224 u64 extent_offset;
2225 u64 last_byte = i_size_read(inode);
2226 u64 block_start;
2227 u64 iosize;
2228 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002229 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002230 struct extent_map *em;
2231 struct block_device *bdev;
2232 int ret;
2233 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002234 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002235 size_t blocksize;
2236 loff_t i_size = i_size_read(inode);
2237 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2238 u64 nr_delalloc;
2239 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002240 int page_started;
2241 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002242 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002243 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002244
Chris Masonffbd5172009-04-20 15:50:09 -04002245 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002246 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002247 else
2248 write_flags = WRITE;
2249
liubo1abe9b82011-03-24 11:18:59 +00002250 trace___extent_writepage(page, inode, wbc);
2251
Chris Masond1310b22008-01-24 16:13:08 -05002252 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002253 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002254 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002255 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002256 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002257 unlock_page(page);
2258 return 0;
2259 }
2260
2261 if (page->index == end_index) {
2262 char *userpage;
2263
Chris Masond1310b22008-01-24 16:13:08 -05002264 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002265 memset(userpage + pg_offset, 0,
2266 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002267 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002268 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002269 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002270 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002271
2272 set_page_extent_mapped(page);
2273
2274 delalloc_start = start;
2275 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002276 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002277 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002278 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002279 /*
2280 * make sure the wbc mapping index is at least updated
2281 * to this page.
2282 */
2283 update_nr_written(page, wbc, 0);
2284
Chris Masond3977122009-01-05 21:25:51 -05002285 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002286 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002287 page,
2288 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002289 &delalloc_end,
2290 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002291 if (nr_delalloc == 0) {
2292 delalloc_start = delalloc_end + 1;
2293 continue;
2294 }
2295 tree->ops->fill_delalloc(inode, page, delalloc_start,
2296 delalloc_end, &page_started,
2297 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002298 /*
2299 * delalloc_end is already one less than the total
2300 * length, so we don't subtract one from
2301 * PAGE_CACHE_SIZE
2302 */
2303 delalloc_to_write += (delalloc_end - delalloc_start +
2304 PAGE_CACHE_SIZE) >>
2305 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002306 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002307 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002308 if (wbc->nr_to_write < delalloc_to_write) {
2309 int thresh = 8192;
2310
2311 if (delalloc_to_write < thresh * 2)
2312 thresh = delalloc_to_write;
2313 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2314 thresh);
2315 }
Chris Masonc8b97812008-10-29 14:49:59 -04002316
Chris Mason771ed682008-11-06 22:02:51 -05002317 /* did the fill delalloc function already unlock and start
2318 * the IO?
2319 */
2320 if (page_started) {
2321 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002322 /*
2323 * we've unlocked the page, so we can't update
2324 * the mapping's writeback index, just update
2325 * nr_to_write.
2326 */
2327 wbc->nr_to_write -= nr_written;
2328 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002329 }
Chris Masonc8b97812008-10-29 14:49:59 -04002330 }
Chris Mason247e7432008-07-17 12:53:51 -04002331 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002332 ret = tree->ops->writepage_start_hook(page, start,
2333 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002334 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002335 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002336 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002337 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002338 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002339 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002340 }
2341 }
2342
Chris Mason11c83492009-04-20 15:50:09 -04002343 /*
2344 * we don't want to touch the inode after unlocking the page,
2345 * so we update the mapping writeback index now
2346 */
2347 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002348
Chris Masond1310b22008-01-24 16:13:08 -05002349 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002350 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002351 if (tree->ops && tree->ops->writepage_end_io_hook)
2352 tree->ops->writepage_end_io_hook(page, start,
2353 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002354 goto done;
2355 }
2356
Chris Masond1310b22008-01-24 16:13:08 -05002357 blocksize = inode->i_sb->s_blocksize;
2358
2359 while (cur <= end) {
2360 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002361 if (tree->ops && tree->ops->writepage_end_io_hook)
2362 tree->ops->writepage_end_io_hook(page, cur,
2363 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002364 break;
2365 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002366 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002367 end - cur + 1, 1);
2368 if (IS_ERR(em) || !em) {
2369 SetPageError(page);
2370 break;
2371 }
2372
2373 extent_offset = cur - em->start;
2374 BUG_ON(extent_map_end(em) <= cur);
2375 BUG_ON(end < cur);
2376 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2377 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2378 sector = (em->block_start + extent_offset) >> 9;
2379 bdev = em->bdev;
2380 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002381 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002382 free_extent_map(em);
2383 em = NULL;
2384
Chris Masonc8b97812008-10-29 14:49:59 -04002385 /*
2386 * compressed and inline extents are written through other
2387 * paths in the FS
2388 */
2389 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002390 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002391 /*
2392 * end_io notification does not happen here for
2393 * compressed extents
2394 */
2395 if (!compressed && tree->ops &&
2396 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002397 tree->ops->writepage_end_io_hook(page, cur,
2398 cur + iosize - 1,
2399 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002400 else if (compressed) {
2401 /* we don't want to end_page_writeback on
2402 * a compressed extent. this happens
2403 * elsewhere
2404 */
2405 nr++;
2406 }
2407
2408 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002409 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002410 continue;
2411 }
Chris Masond1310b22008-01-24 16:13:08 -05002412 /* leave this out until we have a page_mkwrite call */
2413 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002414 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002415 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002416 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002417 continue;
2418 }
Chris Masonc8b97812008-10-29 14:49:59 -04002419
Chris Masond1310b22008-01-24 16:13:08 -05002420 if (tree->ops && tree->ops->writepage_io_hook) {
2421 ret = tree->ops->writepage_io_hook(page, cur,
2422 cur + iosize - 1);
2423 } else {
2424 ret = 0;
2425 }
Chris Mason1259ab72008-05-12 13:39:03 -04002426 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002427 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002428 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002429 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002430
Chris Masond1310b22008-01-24 16:13:08 -05002431 set_range_writeback(tree, cur, cur + iosize - 1);
2432 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002433 printk(KERN_ERR "btrfs warning page %lu not "
2434 "writeback, cur %llu end %llu\n",
2435 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002436 (unsigned long long)end);
2437 }
2438
Chris Masonffbd5172009-04-20 15:50:09 -04002439 ret = submit_extent_page(write_flags, tree, page,
2440 sector, iosize, pg_offset,
2441 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002442 end_bio_extent_writepage,
2443 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002444 if (ret)
2445 SetPageError(page);
2446 }
2447 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002448 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002449 nr++;
2450 }
2451done:
2452 if (nr == 0) {
2453 /* make sure the mapping tag for page dirty gets cleared */
2454 set_page_writeback(page);
2455 end_page_writeback(page);
2456 }
Chris Masond1310b22008-01-24 16:13:08 -05002457 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002458
Chris Mason11c83492009-04-20 15:50:09 -04002459done_unlocked:
2460
Chris Mason2c64c532009-09-02 15:04:12 -04002461 /* drop our reference on any cached states */
2462 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002463 return 0;
2464}
2465
Chris Masond1310b22008-01-24 16:13:08 -05002466/**
Chris Mason4bef0842008-09-08 11:18:08 -04002467 * 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 -05002468 * @mapping: address space structure to write
2469 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2470 * @writepage: function called for each page
2471 * @data: data passed to writepage function
2472 *
2473 * If a page is already under I/O, write_cache_pages() skips it, even
2474 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2475 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2476 * and msync() need to guarantee that all the data which was dirty at the time
2477 * the call was made get new I/O started against them. If wbc->sync_mode is
2478 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2479 * existing IO to complete.
2480 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002481static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002482 struct address_space *mapping,
2483 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002484 writepage_t writepage, void *data,
2485 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002486{
Chris Masond1310b22008-01-24 16:13:08 -05002487 int ret = 0;
2488 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002489 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002490 struct pagevec pvec;
2491 int nr_pages;
2492 pgoff_t index;
2493 pgoff_t end; /* Inclusive */
2494 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002495
Chris Masond1310b22008-01-24 16:13:08 -05002496 pagevec_init(&pvec, 0);
2497 if (wbc->range_cyclic) {
2498 index = mapping->writeback_index; /* Start from prev offset */
2499 end = -1;
2500 } else {
2501 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2502 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002503 scanned = 1;
2504 }
2505retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002506 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002507 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002508 PAGECACHE_TAG_DIRTY, min(end - index,
2509 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002510 unsigned i;
2511
2512 scanned = 1;
2513 for (i = 0; i < nr_pages; i++) {
2514 struct page *page = pvec.pages[i];
2515
2516 /*
2517 * At this point we hold neither mapping->tree_lock nor
2518 * lock on the page itself: the page may be truncated or
2519 * invalidated (changing page->mapping to NULL), or even
2520 * swizzled back from swapper_space to tmpfs file
2521 * mapping
2522 */
Chris Mason4bef0842008-09-08 11:18:08 -04002523 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2524 tree->ops->write_cache_pages_lock_hook(page);
2525 else
2526 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002527
2528 if (unlikely(page->mapping != mapping)) {
2529 unlock_page(page);
2530 continue;
2531 }
2532
2533 if (!wbc->range_cyclic && page->index > end) {
2534 done = 1;
2535 unlock_page(page);
2536 continue;
2537 }
2538
Chris Masond2c3f4f2008-11-19 12:44:22 -05002539 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002540 if (PageWriteback(page))
2541 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002542 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002543 }
Chris Masond1310b22008-01-24 16:13:08 -05002544
2545 if (PageWriteback(page) ||
2546 !clear_page_dirty_for_io(page)) {
2547 unlock_page(page);
2548 continue;
2549 }
2550
2551 ret = (*writepage)(page, wbc, data);
2552
2553 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2554 unlock_page(page);
2555 ret = 0;
2556 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002557 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002558 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002559
2560 /*
2561 * the filesystem may choose to bump up nr_to_write.
2562 * We have to make sure to honor the new nr_to_write
2563 * at any time
2564 */
2565 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002566 }
2567 pagevec_release(&pvec);
2568 cond_resched();
2569 }
2570 if (!scanned && !done) {
2571 /*
2572 * We hit the last page and there is more work to be done: wrap
2573 * back to the start of the file
2574 */
2575 scanned = 1;
2576 index = 0;
2577 goto retry;
2578 }
Chris Masond1310b22008-01-24 16:13:08 -05002579 return ret;
2580}
Chris Masond1310b22008-01-24 16:13:08 -05002581
Chris Masonffbd5172009-04-20 15:50:09 -04002582static void flush_epd_write_bio(struct extent_page_data *epd)
2583{
2584 if (epd->bio) {
2585 if (epd->sync_io)
2586 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2587 else
2588 submit_one_bio(WRITE, epd->bio, 0, 0);
2589 epd->bio = NULL;
2590 }
2591}
2592
Chris Masond2c3f4f2008-11-19 12:44:22 -05002593static noinline void flush_write_bio(void *data)
2594{
2595 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002596 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002597}
2598
Chris Masond1310b22008-01-24 16:13:08 -05002599int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2600 get_extent_t *get_extent,
2601 struct writeback_control *wbc)
2602{
2603 int ret;
2604 struct address_space *mapping = page->mapping;
2605 struct extent_page_data epd = {
2606 .bio = NULL,
2607 .tree = tree,
2608 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002609 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002610 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002611 };
2612 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002613 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002614 .older_than_this = NULL,
2615 .nr_to_write = 64,
2616 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2617 .range_end = (loff_t)-1,
2618 };
2619
Chris Masond1310b22008-01-24 16:13:08 -05002620 ret = __extent_writepage(page, wbc, &epd);
2621
Chris Mason4bef0842008-09-08 11:18:08 -04002622 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002623 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002624 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002625 return ret;
2626}
Chris Masond1310b22008-01-24 16:13:08 -05002627
Chris Mason771ed682008-11-06 22:02:51 -05002628int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2629 u64 start, u64 end, get_extent_t *get_extent,
2630 int mode)
2631{
2632 int ret = 0;
2633 struct address_space *mapping = inode->i_mapping;
2634 struct page *page;
2635 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2636 PAGE_CACHE_SHIFT;
2637
2638 struct extent_page_data epd = {
2639 .bio = NULL,
2640 .tree = tree,
2641 .get_extent = get_extent,
2642 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002643 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002644 };
2645 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002646 .sync_mode = mode,
2647 .older_than_this = NULL,
2648 .nr_to_write = nr_pages * 2,
2649 .range_start = start,
2650 .range_end = end + 1,
2651 };
2652
Chris Masond3977122009-01-05 21:25:51 -05002653 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002654 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2655 if (clear_page_dirty_for_io(page))
2656 ret = __extent_writepage(page, &wbc_writepages, &epd);
2657 else {
2658 if (tree->ops && tree->ops->writepage_end_io_hook)
2659 tree->ops->writepage_end_io_hook(page, start,
2660 start + PAGE_CACHE_SIZE - 1,
2661 NULL, 1);
2662 unlock_page(page);
2663 }
2664 page_cache_release(page);
2665 start += PAGE_CACHE_SIZE;
2666 }
2667
Chris Masonffbd5172009-04-20 15:50:09 -04002668 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002669 return ret;
2670}
Chris Masond1310b22008-01-24 16:13:08 -05002671
2672int extent_writepages(struct extent_io_tree *tree,
2673 struct address_space *mapping,
2674 get_extent_t *get_extent,
2675 struct writeback_control *wbc)
2676{
2677 int ret = 0;
2678 struct extent_page_data epd = {
2679 .bio = NULL,
2680 .tree = tree,
2681 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002682 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002683 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002684 };
2685
Chris Mason4bef0842008-09-08 11:18:08 -04002686 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002687 __extent_writepage, &epd,
2688 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002689 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002690 return ret;
2691}
Chris Masond1310b22008-01-24 16:13:08 -05002692
2693int extent_readpages(struct extent_io_tree *tree,
2694 struct address_space *mapping,
2695 struct list_head *pages, unsigned nr_pages,
2696 get_extent_t get_extent)
2697{
2698 struct bio *bio = NULL;
2699 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002700 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002701
Chris Masond1310b22008-01-24 16:13:08 -05002702 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2703 struct page *page = list_entry(pages->prev, struct page, lru);
2704
2705 prefetchw(&page->flags);
2706 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002707 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002708 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002709 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002710 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002711 }
2712 page_cache_release(page);
2713 }
Chris Masond1310b22008-01-24 16:13:08 -05002714 BUG_ON(!list_empty(pages));
2715 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002716 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002717 return 0;
2718}
Chris Masond1310b22008-01-24 16:13:08 -05002719
2720/*
2721 * basic invalidatepage code, this waits on any locked or writeback
2722 * ranges corresponding to the page, and then deletes any extent state
2723 * records from the tree
2724 */
2725int extent_invalidatepage(struct extent_io_tree *tree,
2726 struct page *page, unsigned long offset)
2727{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002728 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002729 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2730 u64 end = start + PAGE_CACHE_SIZE - 1;
2731 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2732
Chris Masond3977122009-01-05 21:25:51 -05002733 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002734 if (start > end)
2735 return 0;
2736
Josef Bacik2ac55d42010-02-03 19:33:23 +00002737 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002738 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002739 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002740 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2741 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002742 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002743 return 0;
2744}
Chris Masond1310b22008-01-24 16:13:08 -05002745
2746/*
2747 * simple commit_write call, set_range_dirty is used to mark both
2748 * the pages and the extent records as dirty
2749 */
2750int extent_commit_write(struct extent_io_tree *tree,
2751 struct inode *inode, struct page *page,
2752 unsigned from, unsigned to)
2753{
2754 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2755
2756 set_page_extent_mapped(page);
2757 set_page_dirty(page);
2758
2759 if (pos > inode->i_size) {
2760 i_size_write(inode, pos);
2761 mark_inode_dirty(inode);
2762 }
2763 return 0;
2764}
Chris Masond1310b22008-01-24 16:13:08 -05002765
2766int extent_prepare_write(struct extent_io_tree *tree,
2767 struct inode *inode, struct page *page,
2768 unsigned from, unsigned to, get_extent_t *get_extent)
2769{
2770 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2771 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2772 u64 block_start;
2773 u64 orig_block_start;
2774 u64 block_end;
2775 u64 cur_end;
2776 struct extent_map *em;
2777 unsigned blocksize = 1 << inode->i_blkbits;
2778 size_t page_offset = 0;
2779 size_t block_off_start;
2780 size_t block_off_end;
2781 int err = 0;
2782 int iocount = 0;
2783 int ret = 0;
2784 int isnew;
2785
2786 set_page_extent_mapped(page);
2787
2788 block_start = (page_start + from) & ~((u64)blocksize - 1);
2789 block_end = (page_start + to - 1) | (blocksize - 1);
2790 orig_block_start = block_start;
2791
2792 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002793 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002794 em = get_extent(inode, page, page_offset, block_start,
2795 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002796 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002797 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002798
Chris Masond1310b22008-01-24 16:13:08 -05002799 cur_end = min(block_end, extent_map_end(em) - 1);
2800 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2801 block_off_end = block_off_start + blocksize;
2802 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2803
2804 if (!PageUptodate(page) && isnew &&
2805 (block_off_end > to || block_off_start < from)) {
2806 void *kaddr;
2807
2808 kaddr = kmap_atomic(page, KM_USER0);
2809 if (block_off_end > to)
2810 memset(kaddr + to, 0, block_off_end - to);
2811 if (block_off_start < from)
2812 memset(kaddr + block_off_start, 0,
2813 from - block_off_start);
2814 flush_dcache_page(page);
2815 kunmap_atomic(kaddr, KM_USER0);
2816 }
2817 if ((em->block_start != EXTENT_MAP_HOLE &&
2818 em->block_start != EXTENT_MAP_INLINE) &&
2819 !isnew && !PageUptodate(page) &&
2820 (block_off_end > to || block_off_start < from) &&
2821 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002822 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002823 u64 sector;
2824 u64 extent_offset = block_start - em->start;
2825 size_t iosize;
2826 sector = (em->block_start + extent_offset) >> 9;
2827 iosize = (cur_end - block_start + blocksize) &
2828 ~((u64)blocksize - 1);
2829 /*
2830 * we've already got the extent locked, but we
2831 * need to split the state such that our end_bio
2832 * handler can clear the lock.
2833 */
2834 set_extent_bit(tree, block_start,
2835 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002836 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002837 ret = submit_extent_page(READ, tree, page,
2838 sector, iosize, page_offset, em->bdev,
2839 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002840 end_bio_extent_preparewrite, 0,
2841 0, 0);
Andi Kleen411fc6b2010-10-29 15:14:31 -04002842 if (ret && !err)
2843 err = ret;
Chris Masond1310b22008-01-24 16:13:08 -05002844 iocount++;
2845 block_start = block_start + iosize;
2846 } else {
Arne Jansen507903b2011-04-06 10:02:20 +00002847 struct extent_state *cached = NULL;
2848
2849 set_extent_uptodate(tree, block_start, cur_end, &cached,
Chris Masond1310b22008-01-24 16:13:08 -05002850 GFP_NOFS);
Arne Jansen507903b2011-04-06 10:02:20 +00002851 unlock_extent_cached(tree, block_start, cur_end,
2852 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002853 block_start = cur_end + 1;
2854 }
2855 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2856 free_extent_map(em);
2857 }
2858 if (iocount) {
2859 wait_extent_bit(tree, orig_block_start,
2860 block_end, EXTENT_LOCKED);
2861 }
2862 check_page_uptodate(tree, page);
2863err:
2864 /* FIXME, zero out newly allocated blocks on error */
2865 return err;
2866}
Chris Masond1310b22008-01-24 16:13:08 -05002867
2868/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002869 * a helper for releasepage, this tests for areas of the page that
2870 * are locked or under IO and drops the related state bits if it is safe
2871 * to drop the page.
2872 */
2873int try_release_extent_state(struct extent_map_tree *map,
2874 struct extent_io_tree *tree, struct page *page,
2875 gfp_t mask)
2876{
2877 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2878 u64 end = start + PAGE_CACHE_SIZE - 1;
2879 int ret = 1;
2880
Chris Mason211f90e2008-07-18 11:56:15 -04002881 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002882 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002883 ret = 0;
2884 else {
2885 if ((mask & GFP_NOFS) == GFP_NOFS)
2886 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002887 /*
2888 * at this point we can safely clear everything except the
2889 * locked bit and the nodatasum bit
2890 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002891 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002892 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2893 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002894
2895 /* if clear_extent_bit failed for enomem reasons,
2896 * we can't allow the release to continue.
2897 */
2898 if (ret < 0)
2899 ret = 0;
2900 else
2901 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002902 }
2903 return ret;
2904}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002905
2906/*
Chris Masond1310b22008-01-24 16:13:08 -05002907 * a helper for releasepage. As long as there are no locked extents
2908 * in the range corresponding to the page, both state records and extent
2909 * map records are removed
2910 */
2911int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002912 struct extent_io_tree *tree, struct page *page,
2913 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002914{
2915 struct extent_map *em;
2916 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2917 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002918
Chris Mason70dec802008-01-29 09:59:12 -05002919 if ((mask & __GFP_WAIT) &&
2920 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002921 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002922 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002923 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002924 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002925 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002926 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002927 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002928 break;
2929 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002930 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2931 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002932 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002933 free_extent_map(em);
2934 break;
2935 }
2936 if (!test_range_bit(tree, em->start,
2937 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002938 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002939 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002940 remove_extent_mapping(map, em);
2941 /* once for the rb tree */
2942 free_extent_map(em);
2943 }
2944 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002945 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002946
2947 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002948 free_extent_map(em);
2949 }
Chris Masond1310b22008-01-24 16:13:08 -05002950 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002951 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002952}
Chris Masond1310b22008-01-24 16:13:08 -05002953
2954sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2955 get_extent_t *get_extent)
2956{
2957 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002958 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002959 u64 start = iblock << inode->i_blkbits;
2960 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002961 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002962 struct extent_map *em;
2963
Josef Bacik2ac55d42010-02-03 19:33:23 +00002964 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2965 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002966 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002967 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2968 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002969 if (!em || IS_ERR(em))
2970 return 0;
2971
Yan Zhengd899e052008-10-30 14:25:28 -04002972 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002973 goto out;
2974
2975 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002976out:
2977 free_extent_map(em);
2978 return sector;
2979}
2980
Chris Masonec29ed52011-02-23 16:23:20 -05002981/*
2982 * helper function for fiemap, which doesn't want to see any holes.
2983 * This maps until we find something past 'last'
2984 */
2985static struct extent_map *get_extent_skip_holes(struct inode *inode,
2986 u64 offset,
2987 u64 last,
2988 get_extent_t *get_extent)
2989{
2990 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2991 struct extent_map *em;
2992 u64 len;
2993
2994 if (offset >= last)
2995 return NULL;
2996
2997 while(1) {
2998 len = last - offset;
2999 if (len == 0)
3000 break;
3001 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3002 em = get_extent(inode, NULL, 0, offset, len, 0);
3003 if (!em || IS_ERR(em))
3004 return em;
3005
3006 /* if this isn't a hole return it */
3007 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3008 em->block_start != EXTENT_MAP_HOLE) {
3009 return em;
3010 }
3011
3012 /* this is a hole, advance to the next extent */
3013 offset = extent_map_end(em);
3014 free_extent_map(em);
3015 if (offset >= last)
3016 break;
3017 }
3018 return NULL;
3019}
3020
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003021int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3022 __u64 start, __u64 len, get_extent_t *get_extent)
3023{
Josef Bacik975f84f2010-11-23 19:36:57 +00003024 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003025 u64 off = start;
3026 u64 max = start + len;
3027 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003028 u32 found_type;
3029 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003030 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003031 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003032 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003033 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003034 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003035 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003036 struct btrfs_path *path;
3037 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003038 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003039 u64 em_start = 0;
3040 u64 em_len = 0;
3041 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003042 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003043
3044 if (len == 0)
3045 return -EINVAL;
3046
Josef Bacik975f84f2010-11-23 19:36:57 +00003047 path = btrfs_alloc_path();
3048 if (!path)
3049 return -ENOMEM;
3050 path->leave_spinning = 1;
3051
Chris Masonec29ed52011-02-23 16:23:20 -05003052 /*
3053 * lookup the last file extent. We're not using i_size here
3054 * because there might be preallocation past i_size
3055 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003056 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3057 path, inode->i_ino, -1, 0);
3058 if (ret < 0) {
3059 btrfs_free_path(path);
3060 return ret;
3061 }
3062 WARN_ON(!ret);
3063 path->slots[0]--;
3064 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3065 struct btrfs_file_extent_item);
3066 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3067 found_type = btrfs_key_type(&found_key);
3068
Chris Masonec29ed52011-02-23 16:23:20 -05003069 /* No extents, but there might be delalloc bits */
Josef Bacik975f84f2010-11-23 19:36:57 +00003070 if (found_key.objectid != inode->i_ino ||
3071 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003072 /* have to trust i_size as the end */
3073 last = (u64)-1;
3074 last_for_get_extent = isize;
3075 } else {
3076 /*
3077 * remember the start of the last extent. There are a
3078 * bunch of different factors that go into the length of the
3079 * extent, so its much less complex to remember where it started
3080 */
3081 last = found_key.offset;
3082 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003083 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003084 btrfs_free_path(path);
3085
Chris Masonec29ed52011-02-23 16:23:20 -05003086 /*
3087 * we might have some extents allocated but more delalloc past those
3088 * extents. so, we trust isize unless the start of the last extent is
3089 * beyond isize
3090 */
3091 if (last < isize) {
3092 last = (u64)-1;
3093 last_for_get_extent = isize;
3094 }
3095
Josef Bacik2ac55d42010-02-03 19:33:23 +00003096 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3097 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003098
3099 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3100 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003101 if (!em)
3102 goto out;
3103 if (IS_ERR(em)) {
3104 ret = PTR_ERR(em);
3105 goto out;
3106 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003107
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003108 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003109 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003110
Chris Masonea8efc72011-03-08 11:54:40 -05003111 /* break if the extent we found is outside the range */
3112 if (em->start >= max || extent_map_end(em) < off)
3113 break;
3114
3115 /*
3116 * get_extent may return an extent that starts before our
3117 * requested range. We have to make sure the ranges
3118 * we return to fiemap always move forward and don't
3119 * overlap, so adjust the offsets here
3120 */
3121 em_start = max(em->start, off);
3122
3123 /*
3124 * record the offset from the start of the extent
3125 * for adjusting the disk offset below
3126 */
3127 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003128 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003129 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003130 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003131 disko = 0;
3132 flags = 0;
3133
Chris Masonea8efc72011-03-08 11:54:40 -05003134 /*
3135 * bump off for our next call to get_extent
3136 */
3137 off = extent_map_end(em);
3138 if (off >= max)
3139 end = 1;
3140
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003141 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003142 end = 1;
3143 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003144 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003145 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3146 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003147 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003148 flags |= (FIEMAP_EXTENT_DELALLOC |
3149 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003150 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003151 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003152 }
3153 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3154 flags |= FIEMAP_EXTENT_ENCODED;
3155
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003156 free_extent_map(em);
3157 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003158 if ((em_start >= last) || em_len == (u64)-1 ||
3159 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003160 flags |= FIEMAP_EXTENT_LAST;
3161 end = 1;
3162 }
3163
Chris Masonec29ed52011-02-23 16:23:20 -05003164 /* now scan forward to see if this is really the last extent. */
3165 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3166 get_extent);
3167 if (IS_ERR(em)) {
3168 ret = PTR_ERR(em);
3169 goto out;
3170 }
3171 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003172 flags |= FIEMAP_EXTENT_LAST;
3173 end = 1;
3174 }
Chris Masonec29ed52011-02-23 16:23:20 -05003175 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3176 em_len, flags);
3177 if (ret)
3178 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003179 }
3180out_free:
3181 free_extent_map(em);
3182out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003183 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3184 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003185 return ret;
3186}
3187
Chris Masond1310b22008-01-24 16:13:08 -05003188static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3189 unsigned long i)
3190{
3191 struct page *p;
3192 struct address_space *mapping;
3193
3194 if (i == 0)
3195 return eb->first_page;
3196 i += eb->start >> PAGE_CACHE_SHIFT;
3197 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003198 if (!mapping)
3199 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003200
3201 /*
3202 * extent_buffer_page is only called after pinning the page
3203 * by increasing the reference count. So we know the page must
3204 * be in the radix tree.
3205 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003206 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003207 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003208 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003209
Chris Masond1310b22008-01-24 16:13:08 -05003210 return p;
3211}
3212
Chris Mason6af118ce2008-07-22 11:18:07 -04003213static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003214{
Chris Mason6af118ce2008-07-22 11:18:07 -04003215 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3216 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003217}
3218
Chris Masond1310b22008-01-24 16:13:08 -05003219static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3220 u64 start,
3221 unsigned long len,
3222 gfp_t mask)
3223{
3224 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003225#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003226 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003227#endif
Chris Masond1310b22008-01-24 16:13:08 -05003228
Chris Masond1310b22008-01-24 16:13:08 -05003229 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003230 if (eb == NULL)
3231 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003232 eb->start = start;
3233 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003234 spin_lock_init(&eb->lock);
3235 init_waitqueue_head(&eb->lock_wq);
3236
Chris Mason39351272009-02-04 09:24:05 -05003237#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003238 spin_lock_irqsave(&leak_lock, flags);
3239 list_add(&eb->leak_list, &buffers);
3240 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003241#endif
Chris Masond1310b22008-01-24 16:13:08 -05003242 atomic_set(&eb->refs, 1);
3243
3244 return eb;
3245}
3246
3247static void __free_extent_buffer(struct extent_buffer *eb)
3248{
Chris Mason39351272009-02-04 09:24:05 -05003249#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003250 unsigned long flags;
3251 spin_lock_irqsave(&leak_lock, flags);
3252 list_del(&eb->leak_list);
3253 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003254#endif
Chris Masond1310b22008-01-24 16:13:08 -05003255 kmem_cache_free(extent_buffer_cache, eb);
3256}
3257
Miao Xie897ca6e2010-10-26 20:57:29 -04003258/*
3259 * Helper for releasing extent buffer page.
3260 */
3261static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3262 unsigned long start_idx)
3263{
3264 unsigned long index;
3265 struct page *page;
3266
3267 if (!eb->first_page)
3268 return;
3269
3270 index = num_extent_pages(eb->start, eb->len);
3271 if (start_idx >= index)
3272 return;
3273
3274 do {
3275 index--;
3276 page = extent_buffer_page(eb, index);
3277 if (page)
3278 page_cache_release(page);
3279 } while (index != start_idx);
3280}
3281
3282/*
3283 * Helper for releasing the extent buffer.
3284 */
3285static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3286{
3287 btrfs_release_extent_buffer_page(eb, 0);
3288 __free_extent_buffer(eb);
3289}
3290
Chris Masond1310b22008-01-24 16:13:08 -05003291struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3292 u64 start, unsigned long len,
3293 struct page *page0,
3294 gfp_t mask)
3295{
3296 unsigned long num_pages = num_extent_pages(start, len);
3297 unsigned long i;
3298 unsigned long index = start >> PAGE_CACHE_SHIFT;
3299 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003300 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003301 struct page *p;
3302 struct address_space *mapping = tree->mapping;
3303 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003304 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003305
Miao Xie19fe0a82010-10-26 20:57:29 -04003306 rcu_read_lock();
3307 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3308 if (eb && atomic_inc_not_zero(&eb->refs)) {
3309 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003310 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003311 return eb;
3312 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003313 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003314
Chris Masond1310b22008-01-24 16:13:08 -05003315 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003316 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003317 return NULL;
3318
Chris Masond1310b22008-01-24 16:13:08 -05003319 if (page0) {
3320 eb->first_page = page0;
3321 i = 1;
3322 index++;
3323 page_cache_get(page0);
3324 mark_page_accessed(page0);
3325 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003326 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003327 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003328 } else {
3329 i = 0;
3330 }
3331 for (; i < num_pages; i++, index++) {
3332 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3333 if (!p) {
3334 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003335 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003336 }
3337 set_page_extent_mapped(p);
3338 mark_page_accessed(p);
3339 if (i == 0) {
3340 eb->first_page = p;
3341 set_page_extent_head(p, len);
3342 } else {
3343 set_page_private(p, EXTENT_PAGE_PRIVATE);
3344 }
3345 if (!PageUptodate(p))
3346 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003347
3348 /*
3349 * see below about how we avoid a nasty race with release page
3350 * and why we unlock later
3351 */
3352 if (i != 0)
3353 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003354 }
3355 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003356 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003357
Miao Xie19fe0a82010-10-26 20:57:29 -04003358 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3359 if (ret)
3360 goto free_eb;
3361
Chris Mason6af118ce2008-07-22 11:18:07 -04003362 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003363 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3364 if (ret == -EEXIST) {
3365 exists = radix_tree_lookup(&tree->buffer,
3366 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003367 /* add one reference for the caller */
3368 atomic_inc(&exists->refs);
3369 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003370 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003371 goto free_eb;
3372 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003373 /* add one reference for the tree */
3374 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003375 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003376 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003377
3378 /*
3379 * there is a race where release page may have
3380 * tried to find this extent buffer in the radix
3381 * but failed. It will tell the VM it is safe to
3382 * reclaim the, and it will clear the page private bit.
3383 * We must make sure to set the page private bit properly
3384 * after the extent buffer is in the radix tree so
3385 * it doesn't get lost
3386 */
3387 set_page_extent_mapped(eb->first_page);
3388 set_page_extent_head(eb->first_page, eb->len);
3389 if (!page0)
3390 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003391 return eb;
3392
Chris Mason6af118ce2008-07-22 11:18:07 -04003393free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003394 if (eb->first_page && !page0)
3395 unlock_page(eb->first_page);
3396
Chris Masond1310b22008-01-24 16:13:08 -05003397 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003398 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003399 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003400 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003401}
Chris Masond1310b22008-01-24 16:13:08 -05003402
3403struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3404 u64 start, unsigned long len,
3405 gfp_t mask)
3406{
Chris Masond1310b22008-01-24 16:13:08 -05003407 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003408
Miao Xie19fe0a82010-10-26 20:57:29 -04003409 rcu_read_lock();
3410 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3411 if (eb && atomic_inc_not_zero(&eb->refs)) {
3412 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003413 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003414 return eb;
3415 }
3416 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003417
Miao Xie19fe0a82010-10-26 20:57:29 -04003418 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003419}
Chris Masond1310b22008-01-24 16:13:08 -05003420
3421void free_extent_buffer(struct extent_buffer *eb)
3422{
Chris Masond1310b22008-01-24 16:13:08 -05003423 if (!eb)
3424 return;
3425
3426 if (!atomic_dec_and_test(&eb->refs))
3427 return;
3428
Chris Mason6af118ce2008-07-22 11:18:07 -04003429 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003430}
Chris Masond1310b22008-01-24 16:13:08 -05003431
3432int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3433 struct extent_buffer *eb)
3434{
Chris Masond1310b22008-01-24 16:13:08 -05003435 unsigned long i;
3436 unsigned long num_pages;
3437 struct page *page;
3438
Chris Masond1310b22008-01-24 16:13:08 -05003439 num_pages = num_extent_pages(eb->start, eb->len);
3440
3441 for (i = 0; i < num_pages; i++) {
3442 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003443 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003444 continue;
3445
Chris Masona61e6f22008-07-22 11:18:08 -04003446 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003447 WARN_ON(!PagePrivate(page));
3448
3449 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003450 if (i == 0)
3451 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003452
Chris Masond1310b22008-01-24 16:13:08 -05003453 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003454 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003455 if (!PageDirty(page)) {
3456 radix_tree_tag_clear(&page->mapping->page_tree,
3457 page_index(page),
3458 PAGECACHE_TAG_DIRTY);
3459 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003460 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003461 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003462 }
3463 return 0;
3464}
Chris Masond1310b22008-01-24 16:13:08 -05003465
3466int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3467 struct extent_buffer *eb)
3468{
3469 return wait_on_extent_writeback(tree, eb->start,
3470 eb->start + eb->len - 1);
3471}
Chris Masond1310b22008-01-24 16:13:08 -05003472
3473int set_extent_buffer_dirty(struct extent_io_tree *tree,
3474 struct extent_buffer *eb)
3475{
3476 unsigned long i;
3477 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003478 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003479
Chris Masonb9473432009-03-13 11:00:37 -04003480 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003481 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003482 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003483 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003484 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003485}
Chris Masond1310b22008-01-24 16:13:08 -05003486
Chris Mason1259ab72008-05-12 13:39:03 -04003487int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003488 struct extent_buffer *eb,
3489 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003490{
3491 unsigned long i;
3492 struct page *page;
3493 unsigned long num_pages;
3494
3495 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003496 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003497
3498 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003499 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003500 for (i = 0; i < num_pages; i++) {
3501 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003502 if (page)
3503 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003504 }
3505 return 0;
3506}
3507
Chris Masond1310b22008-01-24 16:13:08 -05003508int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3509 struct extent_buffer *eb)
3510{
3511 unsigned long i;
3512 struct page *page;
3513 unsigned long num_pages;
3514
3515 num_pages = num_extent_pages(eb->start, eb->len);
3516
3517 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003518 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003519 for (i = 0; i < num_pages; i++) {
3520 page = extent_buffer_page(eb, i);
3521 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3522 ((i == num_pages - 1) &&
3523 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3524 check_page_uptodate(tree, page);
3525 continue;
3526 }
3527 SetPageUptodate(page);
3528 }
3529 return 0;
3530}
Chris Masond1310b22008-01-24 16:13:08 -05003531
Chris Masonce9adaa2008-04-09 16:28:12 -04003532int extent_range_uptodate(struct extent_io_tree *tree,
3533 u64 start, u64 end)
3534{
3535 struct page *page;
3536 int ret;
3537 int pg_uptodate = 1;
3538 int uptodate;
3539 unsigned long index;
3540
Chris Mason9655d292009-09-02 15:22:30 -04003541 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003542 if (ret)
3543 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003544 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003545 index = start >> PAGE_CACHE_SHIFT;
3546 page = find_get_page(tree->mapping, index);
3547 uptodate = PageUptodate(page);
3548 page_cache_release(page);
3549 if (!uptodate) {
3550 pg_uptodate = 0;
3551 break;
3552 }
3553 start += PAGE_CACHE_SIZE;
3554 }
3555 return pg_uptodate;
3556}
3557
Chris Masond1310b22008-01-24 16:13:08 -05003558int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003559 struct extent_buffer *eb,
3560 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003561{
Chris Mason728131d2008-04-09 16:28:12 -04003562 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003563 unsigned long num_pages;
3564 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003565 struct page *page;
3566 int pg_uptodate = 1;
3567
Chris Masonb4ce94d2009-02-04 09:25:08 -05003568 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003569 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003570
Chris Mason42352982008-04-28 16:40:52 -04003571 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003572 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003573 if (ret)
3574 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003575
3576 num_pages = num_extent_pages(eb->start, eb->len);
3577 for (i = 0; i < num_pages; i++) {
3578 page = extent_buffer_page(eb, i);
3579 if (!PageUptodate(page)) {
3580 pg_uptodate = 0;
3581 break;
3582 }
3583 }
Chris Mason42352982008-04-28 16:40:52 -04003584 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003585}
Chris Masond1310b22008-01-24 16:13:08 -05003586
3587int read_extent_buffer_pages(struct extent_io_tree *tree,
3588 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003589 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003590 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003591{
3592 unsigned long i;
3593 unsigned long start_i;
3594 struct page *page;
3595 int err;
3596 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003597 int locked_pages = 0;
3598 int all_uptodate = 1;
3599 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003600 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003601 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003602 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003603
Chris Masonb4ce94d2009-02-04 09:25:08 -05003604 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003605 return 0;
3606
Chris Masonce9adaa2008-04-09 16:28:12 -04003607 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003608 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003609 return 0;
3610 }
3611
3612 if (start) {
3613 WARN_ON(start < eb->start);
3614 start_i = (start >> PAGE_CACHE_SHIFT) -
3615 (eb->start >> PAGE_CACHE_SHIFT);
3616 } else {
3617 start_i = 0;
3618 }
3619
3620 num_pages = num_extent_pages(eb->start, eb->len);
3621 for (i = start_i; i < num_pages; i++) {
3622 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003623 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003624 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003625 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003626 } else {
3627 lock_page(page);
3628 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003629 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003630 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003631 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003632 }
3633 if (all_uptodate) {
3634 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003635 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003636 goto unlock_exit;
3637 }
3638
3639 for (i = start_i; i < num_pages; i++) {
3640 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003641
3642 WARN_ON(!PagePrivate(page));
3643
3644 set_page_extent_mapped(page);
3645 if (i == 0)
3646 set_page_extent_head(page, eb->len);
3647
Chris Masonce9adaa2008-04-09 16:28:12 -04003648 if (inc_all_pages)
3649 page_cache_get(page);
3650 if (!PageUptodate(page)) {
3651 if (start_i == 0)
3652 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003653 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003654 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003655 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003656 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003657 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003658 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003659 } else {
3660 unlock_page(page);
3661 }
3662 }
3663
Chris Masona86c12c2008-02-07 10:50:54 -05003664 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003665 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003666
Chris Masond3977122009-01-05 21:25:51 -05003667 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003668 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003669
Chris Masond1310b22008-01-24 16:13:08 -05003670 for (i = start_i; i < num_pages; i++) {
3671 page = extent_buffer_page(eb, i);
3672 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003673 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003674 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003675 }
Chris Masond3977122009-01-05 21:25:51 -05003676
Chris Masond1310b22008-01-24 16:13:08 -05003677 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003678 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003679 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003680
3681unlock_exit:
3682 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003683 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003684 page = extent_buffer_page(eb, i);
3685 i++;
3686 unlock_page(page);
3687 locked_pages--;
3688 }
3689 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003690}
Chris Masond1310b22008-01-24 16:13:08 -05003691
3692void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3693 unsigned long start,
3694 unsigned long len)
3695{
3696 size_t cur;
3697 size_t offset;
3698 struct page *page;
3699 char *kaddr;
3700 char *dst = (char *)dstv;
3701 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3702 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003703
3704 WARN_ON(start > eb->len);
3705 WARN_ON(start + len > eb->start + eb->len);
3706
3707 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3708
Chris Masond3977122009-01-05 21:25:51 -05003709 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003710 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003711
3712 cur = min(len, (PAGE_CACHE_SIZE - offset));
3713 kaddr = kmap_atomic(page, KM_USER1);
3714 memcpy(dst, kaddr + offset, cur);
3715 kunmap_atomic(kaddr, KM_USER1);
3716
3717 dst += cur;
3718 len -= cur;
3719 offset = 0;
3720 i++;
3721 }
3722}
Chris Masond1310b22008-01-24 16:13:08 -05003723
3724int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3725 unsigned long min_len, char **token, char **map,
3726 unsigned long *map_start,
3727 unsigned long *map_len, int km)
3728{
3729 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3730 char *kaddr;
3731 struct page *p;
3732 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3733 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3734 unsigned long end_i = (start_offset + start + min_len - 1) >>
3735 PAGE_CACHE_SHIFT;
3736
3737 if (i != end_i)
3738 return -EINVAL;
3739
3740 if (i == 0) {
3741 offset = start_offset;
3742 *map_start = 0;
3743 } else {
3744 offset = 0;
3745 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3746 }
Chris Masond3977122009-01-05 21:25:51 -05003747
Chris Masond1310b22008-01-24 16:13:08 -05003748 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003749 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3750 "wanted %lu %lu\n", (unsigned long long)eb->start,
3751 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003752 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003753 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003754 }
3755
3756 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003757 kaddr = kmap_atomic(p, km);
3758 *token = kaddr;
3759 *map = kaddr + offset;
3760 *map_len = PAGE_CACHE_SIZE - offset;
3761 return 0;
3762}
Chris Masond1310b22008-01-24 16:13:08 -05003763
3764int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3765 unsigned long min_len,
3766 char **token, char **map,
3767 unsigned long *map_start,
3768 unsigned long *map_len, int km)
3769{
3770 int err;
3771 int save = 0;
3772 if (eb->map_token) {
3773 unmap_extent_buffer(eb, eb->map_token, km);
3774 eb->map_token = NULL;
3775 save = 1;
3776 }
3777 err = map_private_extent_buffer(eb, start, min_len, token, map,
3778 map_start, map_len, km);
3779 if (!err && save) {
3780 eb->map_token = *token;
3781 eb->kaddr = *map;
3782 eb->map_start = *map_start;
3783 eb->map_len = *map_len;
3784 }
3785 return err;
3786}
Chris Masond1310b22008-01-24 16:13:08 -05003787
3788void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3789{
3790 kunmap_atomic(token, km);
3791}
Chris Masond1310b22008-01-24 16:13:08 -05003792
3793int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3794 unsigned long start,
3795 unsigned long len)
3796{
3797 size_t cur;
3798 size_t offset;
3799 struct page *page;
3800 char *kaddr;
3801 char *ptr = (char *)ptrv;
3802 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3803 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3804 int ret = 0;
3805
3806 WARN_ON(start > eb->len);
3807 WARN_ON(start + len > eb->start + eb->len);
3808
3809 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3810
Chris Masond3977122009-01-05 21:25:51 -05003811 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003812 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003813
3814 cur = min(len, (PAGE_CACHE_SIZE - offset));
3815
3816 kaddr = kmap_atomic(page, KM_USER0);
3817 ret = memcmp(ptr, kaddr + offset, cur);
3818 kunmap_atomic(kaddr, KM_USER0);
3819 if (ret)
3820 break;
3821
3822 ptr += cur;
3823 len -= cur;
3824 offset = 0;
3825 i++;
3826 }
3827 return ret;
3828}
Chris Masond1310b22008-01-24 16:13:08 -05003829
3830void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3831 unsigned long start, unsigned long len)
3832{
3833 size_t cur;
3834 size_t offset;
3835 struct page *page;
3836 char *kaddr;
3837 char *src = (char *)srcv;
3838 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3839 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3840
3841 WARN_ON(start > eb->len);
3842 WARN_ON(start + len > eb->start + eb->len);
3843
3844 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3845
Chris Masond3977122009-01-05 21:25:51 -05003846 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003847 page = extent_buffer_page(eb, i);
3848 WARN_ON(!PageUptodate(page));
3849
3850 cur = min(len, PAGE_CACHE_SIZE - offset);
3851 kaddr = kmap_atomic(page, KM_USER1);
3852 memcpy(kaddr + offset, src, cur);
3853 kunmap_atomic(kaddr, KM_USER1);
3854
3855 src += cur;
3856 len -= cur;
3857 offset = 0;
3858 i++;
3859 }
3860}
Chris Masond1310b22008-01-24 16:13:08 -05003861
3862void memset_extent_buffer(struct extent_buffer *eb, char c,
3863 unsigned long start, unsigned long len)
3864{
3865 size_t cur;
3866 size_t offset;
3867 struct page *page;
3868 char *kaddr;
3869 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3870 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3871
3872 WARN_ON(start > eb->len);
3873 WARN_ON(start + len > eb->start + eb->len);
3874
3875 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3876
Chris Masond3977122009-01-05 21:25:51 -05003877 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003878 page = extent_buffer_page(eb, i);
3879 WARN_ON(!PageUptodate(page));
3880
3881 cur = min(len, PAGE_CACHE_SIZE - offset);
3882 kaddr = kmap_atomic(page, KM_USER0);
3883 memset(kaddr + offset, c, cur);
3884 kunmap_atomic(kaddr, KM_USER0);
3885
3886 len -= cur;
3887 offset = 0;
3888 i++;
3889 }
3890}
Chris Masond1310b22008-01-24 16:13:08 -05003891
3892void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3893 unsigned long dst_offset, unsigned long src_offset,
3894 unsigned long len)
3895{
3896 u64 dst_len = dst->len;
3897 size_t cur;
3898 size_t offset;
3899 struct page *page;
3900 char *kaddr;
3901 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3902 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3903
3904 WARN_ON(src->len != dst_len);
3905
3906 offset = (start_offset + dst_offset) &
3907 ((unsigned long)PAGE_CACHE_SIZE - 1);
3908
Chris Masond3977122009-01-05 21:25:51 -05003909 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003910 page = extent_buffer_page(dst, i);
3911 WARN_ON(!PageUptodate(page));
3912
3913 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3914
3915 kaddr = kmap_atomic(page, KM_USER0);
3916 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3917 kunmap_atomic(kaddr, KM_USER0);
3918
3919 src_offset += cur;
3920 len -= cur;
3921 offset = 0;
3922 i++;
3923 }
3924}
Chris Masond1310b22008-01-24 16:13:08 -05003925
3926static void move_pages(struct page *dst_page, struct page *src_page,
3927 unsigned long dst_off, unsigned long src_off,
3928 unsigned long len)
3929{
3930 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3931 if (dst_page == src_page) {
3932 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3933 } else {
3934 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3935 char *p = dst_kaddr + dst_off + len;
3936 char *s = src_kaddr + src_off + len;
3937
3938 while (len--)
3939 *--p = *--s;
3940
3941 kunmap_atomic(src_kaddr, KM_USER1);
3942 }
3943 kunmap_atomic(dst_kaddr, KM_USER0);
3944}
3945
Sergei Trofimovich33872062011-04-11 21:52:52 +00003946static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3947{
3948 unsigned long distance = (src > dst) ? src - dst : dst - src;
3949 return distance < len;
3950}
3951
Chris Masond1310b22008-01-24 16:13:08 -05003952static void copy_pages(struct page *dst_page, struct page *src_page,
3953 unsigned long dst_off, unsigned long src_off,
3954 unsigned long len)
3955{
3956 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3957 char *src_kaddr;
3958
Sergei Trofimovich33872062011-04-11 21:52:52 +00003959 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003960 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003961 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003962 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003963 BUG_ON(areas_overlap(src_off, dst_off, len));
3964 }
Chris Masond1310b22008-01-24 16:13:08 -05003965
3966 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3967 kunmap_atomic(dst_kaddr, KM_USER0);
3968 if (dst_page != src_page)
3969 kunmap_atomic(src_kaddr, KM_USER1);
3970}
3971
3972void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3973 unsigned long src_offset, unsigned long len)
3974{
3975 size_t cur;
3976 size_t dst_off_in_page;
3977 size_t src_off_in_page;
3978 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3979 unsigned long dst_i;
3980 unsigned long src_i;
3981
3982 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003983 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3984 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003985 BUG_ON(1);
3986 }
3987 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003988 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3989 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003990 BUG_ON(1);
3991 }
3992
Chris Masond3977122009-01-05 21:25:51 -05003993 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003994 dst_off_in_page = (start_offset + dst_offset) &
3995 ((unsigned long)PAGE_CACHE_SIZE - 1);
3996 src_off_in_page = (start_offset + src_offset) &
3997 ((unsigned long)PAGE_CACHE_SIZE - 1);
3998
3999 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4000 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4001
4002 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4003 src_off_in_page));
4004 cur = min_t(unsigned long, cur,
4005 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4006
4007 copy_pages(extent_buffer_page(dst, dst_i),
4008 extent_buffer_page(dst, src_i),
4009 dst_off_in_page, src_off_in_page, cur);
4010
4011 src_offset += cur;
4012 dst_offset += cur;
4013 len -= cur;
4014 }
4015}
Chris Masond1310b22008-01-24 16:13:08 -05004016
4017void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4018 unsigned long src_offset, unsigned long len)
4019{
4020 size_t cur;
4021 size_t dst_off_in_page;
4022 size_t src_off_in_page;
4023 unsigned long dst_end = dst_offset + len - 1;
4024 unsigned long src_end = src_offset + len - 1;
4025 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4026 unsigned long dst_i;
4027 unsigned long src_i;
4028
4029 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004030 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4031 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004032 BUG_ON(1);
4033 }
4034 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004035 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4036 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004037 BUG_ON(1);
4038 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004039 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004040 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4041 return;
4042 }
Chris Masond3977122009-01-05 21:25:51 -05004043 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004044 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4045 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4046
4047 dst_off_in_page = (start_offset + dst_end) &
4048 ((unsigned long)PAGE_CACHE_SIZE - 1);
4049 src_off_in_page = (start_offset + src_end) &
4050 ((unsigned long)PAGE_CACHE_SIZE - 1);
4051
4052 cur = min_t(unsigned long, len, src_off_in_page + 1);
4053 cur = min(cur, dst_off_in_page + 1);
4054 move_pages(extent_buffer_page(dst, dst_i),
4055 extent_buffer_page(dst, src_i),
4056 dst_off_in_page - cur + 1,
4057 src_off_in_page - cur + 1, cur);
4058
4059 dst_end -= cur;
4060 src_end -= cur;
4061 len -= cur;
4062 }
4063}
Chris Mason6af118ce2008-07-22 11:18:07 -04004064
Miao Xie19fe0a82010-10-26 20:57:29 -04004065static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4066{
4067 struct extent_buffer *eb =
4068 container_of(head, struct extent_buffer, rcu_head);
4069
4070 btrfs_release_extent_buffer(eb);
4071}
4072
Chris Mason6af118ce2008-07-22 11:18:07 -04004073int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4074{
4075 u64 start = page_offset(page);
4076 struct extent_buffer *eb;
4077 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004078
4079 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004080 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004081 if (!eb) {
4082 spin_unlock(&tree->buffer_lock);
4083 return ret;
4084 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004085
Chris Masonb9473432009-03-13 11:00:37 -04004086 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4087 ret = 0;
4088 goto out;
4089 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004090
Miao Xie19fe0a82010-10-26 20:57:29 -04004091 /*
4092 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4093 * Or go back.
4094 */
4095 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4096 ret = 0;
4097 goto out;
4098 }
4099
4100 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004101out:
4102 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004103
4104 /* at this point we can safely release the extent buffer */
4105 if (atomic_read(&eb->refs) == 0)
4106 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004107 return ret;
4108}