blob: a90c4a12556b12bc432057fc1576fa209f19fbd5 [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,
David Sterbaf993c882011-04-20 23:35:57 +0200104 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500105{
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 Mason6af118c2008-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
Chris Masond1310b22008-01-24 16:13:08 -0500968int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000969 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500970{
Arne Jansen507903b2011-04-06 10:02:20 +0000971 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
972 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500973}
Chris Masond1310b22008-01-24 16:13:08 -0500974
Chris Masond3977122009-01-05 21:25:51 -0500975static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000976 u64 end, struct extent_state **cached_state,
977 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500978{
Chris Mason2c64c532009-09-02 15:04:12 -0400979 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000980 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500981}
Chris Masond1310b22008-01-24 16:13:08 -0500982
Chris Masond352ac62008-09-29 15:18:18 -0400983/*
984 * either insert or lock state struct between start and end use mask to tell
985 * us if waiting is desired.
986 */
Chris Mason1edbb732009-09-02 13:24:36 -0400987int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400988 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500989{
990 int err;
991 u64 failed_start;
992 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400993 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400994 EXTENT_LOCKED, &failed_start,
995 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500996 if (err == -EEXIST && (mask & __GFP_WAIT)) {
997 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
998 start = failed_start;
999 } else {
1000 break;
1001 }
1002 WARN_ON(start > end);
1003 }
1004 return err;
1005}
Chris Masond1310b22008-01-24 16:13:08 -05001006
Chris Mason1edbb732009-09-02 13:24:36 -04001007int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1008{
Chris Mason2c64c532009-09-02 15:04:12 -04001009 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001010}
1011
Josef Bacik25179202008-10-29 14:49:05 -04001012int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1013 gfp_t mask)
1014{
1015 int err;
1016 u64 failed_start;
1017
Chris Mason2c64c532009-09-02 15:04:12 -04001018 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1019 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001020 if (err == -EEXIST) {
1021 if (failed_start > start)
1022 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001023 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001024 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001025 }
Josef Bacik25179202008-10-29 14:49:05 -04001026 return 1;
1027}
Josef Bacik25179202008-10-29 14:49:05 -04001028
Chris Mason2c64c532009-09-02 15:04:12 -04001029int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1030 struct extent_state **cached, gfp_t mask)
1031{
1032 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1033 mask);
1034}
1035
Arne Jansen507903b2011-04-06 10:02:20 +00001036int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001037{
Chris Mason2c64c532009-09-02 15:04:12 -04001038 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1039 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001040}
Chris Masond1310b22008-01-24 16:13:08 -05001041
1042/*
Chris Masond1310b22008-01-24 16:13:08 -05001043 * helper function to set both pages and extents in the tree writeback
1044 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001045static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001046{
1047 unsigned long index = start >> PAGE_CACHE_SHIFT;
1048 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1049 struct page *page;
1050
1051 while (index <= end_index) {
1052 page = find_get_page(tree->mapping, index);
1053 BUG_ON(!page);
1054 set_page_writeback(page);
1055 page_cache_release(page);
1056 index++;
1057 }
Chris Masond1310b22008-01-24 16:13:08 -05001058 return 0;
1059}
Chris Masond1310b22008-01-24 16:13:08 -05001060
Chris Masond352ac62008-09-29 15:18:18 -04001061/*
1062 * find the first offset in the io tree with 'bits' set. zero is
1063 * returned if we find something, and *start_ret and *end_ret are
1064 * set to reflect the state struct that was found.
1065 *
1066 * If nothing was found, 1 is returned, < 0 on error
1067 */
Chris Masond1310b22008-01-24 16:13:08 -05001068int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1069 u64 *start_ret, u64 *end_ret, int bits)
1070{
1071 struct rb_node *node;
1072 struct extent_state *state;
1073 int ret = 1;
1074
Chris Masoncad321a2008-12-17 14:51:42 -05001075 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001076 /*
1077 * this search will find all the extents that end after
1078 * our range starts.
1079 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001080 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001081 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001082 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001083
Chris Masond3977122009-01-05 21:25:51 -05001084 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001085 state = rb_entry(node, struct extent_state, rb_node);
1086 if (state->end >= start && (state->state & bits)) {
1087 *start_ret = state->start;
1088 *end_ret = state->end;
1089 ret = 0;
1090 break;
1091 }
1092 node = rb_next(node);
1093 if (!node)
1094 break;
1095 }
1096out:
Chris Masoncad321a2008-12-17 14:51:42 -05001097 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001098 return ret;
1099}
Chris Masond1310b22008-01-24 16:13:08 -05001100
Chris Masond352ac62008-09-29 15:18:18 -04001101/* find the first state struct with 'bits' set after 'start', and
1102 * return it. tree->lock must be held. NULL will returned if
1103 * nothing was found after 'start'
1104 */
Chris Masond7fc6402008-02-18 12:12:38 -05001105struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1106 u64 start, int bits)
1107{
1108 struct rb_node *node;
1109 struct extent_state *state;
1110
1111 /*
1112 * this search will find all the extents that end after
1113 * our range starts.
1114 */
1115 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001116 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001117 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001118
Chris Masond3977122009-01-05 21:25:51 -05001119 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001120 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001121 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001122 return state;
Chris Masond3977122009-01-05 21:25:51 -05001123
Chris Masond7fc6402008-02-18 12:12:38 -05001124 node = rb_next(node);
1125 if (!node)
1126 break;
1127 }
1128out:
1129 return NULL;
1130}
Chris Masond7fc6402008-02-18 12:12:38 -05001131
Chris Masond352ac62008-09-29 15:18:18 -04001132/*
1133 * find a contiguous range of bytes in the file marked as delalloc, not
1134 * more than 'max_bytes'. start and end are used to return the range,
1135 *
1136 * 1 is returned if we find something, 0 if nothing was in the tree
1137 */
Chris Masonc8b97812008-10-29 14:49:59 -04001138static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001139 u64 *start, u64 *end, u64 max_bytes,
1140 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001141{
1142 struct rb_node *node;
1143 struct extent_state *state;
1144 u64 cur_start = *start;
1145 u64 found = 0;
1146 u64 total_bytes = 0;
1147
Chris Masoncad321a2008-12-17 14:51:42 -05001148 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001149
Chris Masond1310b22008-01-24 16:13:08 -05001150 /*
1151 * this search will find all the extents that end after
1152 * our range starts.
1153 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001154 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001155 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001156 if (!found)
1157 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001158 goto out;
1159 }
1160
Chris Masond3977122009-01-05 21:25:51 -05001161 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001162 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001163 if (found && (state->start != cur_start ||
1164 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001165 goto out;
1166 }
1167 if (!(state->state & EXTENT_DELALLOC)) {
1168 if (!found)
1169 *end = state->end;
1170 goto out;
1171 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001172 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001173 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001174 *cached_state = state;
1175 atomic_inc(&state->refs);
1176 }
Chris Masond1310b22008-01-24 16:13:08 -05001177 found++;
1178 *end = state->end;
1179 cur_start = state->end + 1;
1180 node = rb_next(node);
1181 if (!node)
1182 break;
1183 total_bytes += state->end - state->start + 1;
1184 if (total_bytes >= max_bytes)
1185 break;
1186 }
1187out:
Chris Masoncad321a2008-12-17 14:51:42 -05001188 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001189 return found;
1190}
1191
Chris Masonc8b97812008-10-29 14:49:59 -04001192static noinline int __unlock_for_delalloc(struct inode *inode,
1193 struct page *locked_page,
1194 u64 start, u64 end)
1195{
1196 int ret;
1197 struct page *pages[16];
1198 unsigned long index = start >> PAGE_CACHE_SHIFT;
1199 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1200 unsigned long nr_pages = end_index - index + 1;
1201 int i;
1202
1203 if (index == locked_page->index && end_index == index)
1204 return 0;
1205
Chris Masond3977122009-01-05 21:25:51 -05001206 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001207 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001208 min_t(unsigned long, nr_pages,
1209 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001210 for (i = 0; i < ret; i++) {
1211 if (pages[i] != locked_page)
1212 unlock_page(pages[i]);
1213 page_cache_release(pages[i]);
1214 }
1215 nr_pages -= ret;
1216 index += ret;
1217 cond_resched();
1218 }
1219 return 0;
1220}
1221
1222static noinline int lock_delalloc_pages(struct inode *inode,
1223 struct page *locked_page,
1224 u64 delalloc_start,
1225 u64 delalloc_end)
1226{
1227 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1228 unsigned long start_index = index;
1229 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1230 unsigned long pages_locked = 0;
1231 struct page *pages[16];
1232 unsigned long nrpages;
1233 int ret;
1234 int i;
1235
1236 /* the caller is responsible for locking the start index */
1237 if (index == locked_page->index && index == end_index)
1238 return 0;
1239
1240 /* skip the page at the start index */
1241 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001242 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001243 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001244 min_t(unsigned long,
1245 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001246 if (ret == 0) {
1247 ret = -EAGAIN;
1248 goto done;
1249 }
1250 /* now we have an array of pages, lock them all */
1251 for (i = 0; i < ret; i++) {
1252 /*
1253 * the caller is taking responsibility for
1254 * locked_page
1255 */
Chris Mason771ed682008-11-06 22:02:51 -05001256 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001257 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001258 if (!PageDirty(pages[i]) ||
1259 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001260 ret = -EAGAIN;
1261 unlock_page(pages[i]);
1262 page_cache_release(pages[i]);
1263 goto done;
1264 }
1265 }
Chris Masonc8b97812008-10-29 14:49:59 -04001266 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001267 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001268 }
Chris Masonc8b97812008-10-29 14:49:59 -04001269 nrpages -= ret;
1270 index += ret;
1271 cond_resched();
1272 }
1273 ret = 0;
1274done:
1275 if (ret && pages_locked) {
1276 __unlock_for_delalloc(inode, locked_page,
1277 delalloc_start,
1278 ((u64)(start_index + pages_locked - 1)) <<
1279 PAGE_CACHE_SHIFT);
1280 }
1281 return ret;
1282}
1283
1284/*
1285 * find a contiguous range of bytes in the file marked as delalloc, not
1286 * more than 'max_bytes'. start and end are used to return the range,
1287 *
1288 * 1 is returned if we find something, 0 if nothing was in the tree
1289 */
1290static noinline u64 find_lock_delalloc_range(struct inode *inode,
1291 struct extent_io_tree *tree,
1292 struct page *locked_page,
1293 u64 *start, u64 *end,
1294 u64 max_bytes)
1295{
1296 u64 delalloc_start;
1297 u64 delalloc_end;
1298 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001299 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001300 int ret;
1301 int loops = 0;
1302
1303again:
1304 /* step one, find a bunch of delalloc bytes starting at start */
1305 delalloc_start = *start;
1306 delalloc_end = 0;
1307 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001308 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001309 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001310 *start = delalloc_start;
1311 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001312 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001313 return found;
1314 }
1315
1316 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001317 * start comes from the offset of locked_page. We have to lock
1318 * pages in order, so we can't process delalloc bytes before
1319 * locked_page
1320 */
Chris Masond3977122009-01-05 21:25:51 -05001321 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001322 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001323
1324 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001325 * make sure to limit the number of pages we try to lock down
1326 * if we're looping.
1327 */
Chris Masond3977122009-01-05 21:25:51 -05001328 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001329 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001330
Chris Masonc8b97812008-10-29 14:49:59 -04001331 /* step two, lock all the pages after the page that has start */
1332 ret = lock_delalloc_pages(inode, locked_page,
1333 delalloc_start, delalloc_end);
1334 if (ret == -EAGAIN) {
1335 /* some of the pages are gone, lets avoid looping by
1336 * shortening the size of the delalloc range we're searching
1337 */
Chris Mason9655d292009-09-02 15:22:30 -04001338 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001339 if (!loops) {
1340 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1341 max_bytes = PAGE_CACHE_SIZE - offset;
1342 loops = 1;
1343 goto again;
1344 } else {
1345 found = 0;
1346 goto out_failed;
1347 }
1348 }
1349 BUG_ON(ret);
1350
1351 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001352 lock_extent_bits(tree, delalloc_start, delalloc_end,
1353 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001354
1355 /* then test to make sure it is all still delalloc */
1356 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001357 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001358 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001359 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1360 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001361 __unlock_for_delalloc(inode, locked_page,
1362 delalloc_start, delalloc_end);
1363 cond_resched();
1364 goto again;
1365 }
Chris Mason9655d292009-09-02 15:22:30 -04001366 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001367 *start = delalloc_start;
1368 *end = delalloc_end;
1369out_failed:
1370 return found;
1371}
1372
1373int extent_clear_unlock_delalloc(struct inode *inode,
1374 struct extent_io_tree *tree,
1375 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001376 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001377{
1378 int ret;
1379 struct page *pages[16];
1380 unsigned long index = start >> PAGE_CACHE_SHIFT;
1381 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1382 unsigned long nr_pages = end_index - index + 1;
1383 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001384 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001385
Chris Masona791e352009-10-08 11:27:10 -04001386 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001387 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001388 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001389 clear_bits |= EXTENT_DIRTY;
1390
Chris Masona791e352009-10-08 11:27:10 -04001391 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001392 clear_bits |= EXTENT_DELALLOC;
1393
Chris Mason2c64c532009-09-02 15:04:12 -04001394 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001395 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1396 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1397 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001398 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001399
Chris Masond3977122009-01-05 21:25:51 -05001400 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001401 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001402 min_t(unsigned long,
1403 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001404 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001405
Chris Masona791e352009-10-08 11:27:10 -04001406 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001407 SetPagePrivate2(pages[i]);
1408
Chris Masonc8b97812008-10-29 14:49:59 -04001409 if (pages[i] == locked_page) {
1410 page_cache_release(pages[i]);
1411 continue;
1412 }
Chris Masona791e352009-10-08 11:27:10 -04001413 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001414 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001415 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001416 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001417 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001418 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001419 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001420 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001421 page_cache_release(pages[i]);
1422 }
1423 nr_pages -= ret;
1424 index += ret;
1425 cond_resched();
1426 }
1427 return 0;
1428}
Chris Masonc8b97812008-10-29 14:49:59 -04001429
Chris Masond352ac62008-09-29 15:18:18 -04001430/*
1431 * count the number of bytes in the tree that have a given bit(s)
1432 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1433 * cached. The total number found is returned.
1434 */
Chris Masond1310b22008-01-24 16:13:08 -05001435u64 count_range_bits(struct extent_io_tree *tree,
1436 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001437 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001438{
1439 struct rb_node *node;
1440 struct extent_state *state;
1441 u64 cur_start = *start;
1442 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001443 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001444 int found = 0;
1445
1446 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001447 WARN_ON(1);
1448 return 0;
1449 }
1450
Chris Masoncad321a2008-12-17 14:51:42 -05001451 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001452 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1453 total_bytes = tree->dirty_bytes;
1454 goto out;
1455 }
1456 /*
1457 * this search will find all the extents that end after
1458 * our range starts.
1459 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001460 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001461 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001462 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001463
Chris Masond3977122009-01-05 21:25:51 -05001464 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001465 state = rb_entry(node, struct extent_state, rb_node);
1466 if (state->start > search_end)
1467 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001468 if (contig && found && state->start > last + 1)
1469 break;
1470 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001471 total_bytes += min(search_end, state->end) + 1 -
1472 max(cur_start, state->start);
1473 if (total_bytes >= max_bytes)
1474 break;
1475 if (!found) {
1476 *start = state->start;
1477 found = 1;
1478 }
Chris Masonec29ed52011-02-23 16:23:20 -05001479 last = state->end;
1480 } else if (contig && found) {
1481 break;
Chris Masond1310b22008-01-24 16:13:08 -05001482 }
1483 node = rb_next(node);
1484 if (!node)
1485 break;
1486 }
1487out:
Chris Masoncad321a2008-12-17 14:51:42 -05001488 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001489 return total_bytes;
1490}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001491
Chris Masond352ac62008-09-29 15:18:18 -04001492/*
1493 * set the private field for a given byte offset in the tree. If there isn't
1494 * an extent_state there already, this does nothing.
1495 */
Chris Masond1310b22008-01-24 16:13:08 -05001496int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1497{
1498 struct rb_node *node;
1499 struct extent_state *state;
1500 int ret = 0;
1501
Chris Masoncad321a2008-12-17 14:51:42 -05001502 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001503 /*
1504 * this search will find all the extents that end after
1505 * our range starts.
1506 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001507 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001508 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001509 ret = -ENOENT;
1510 goto out;
1511 }
1512 state = rb_entry(node, struct extent_state, rb_node);
1513 if (state->start != start) {
1514 ret = -ENOENT;
1515 goto out;
1516 }
1517 state->private = private;
1518out:
Chris Masoncad321a2008-12-17 14:51:42 -05001519 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001520 return ret;
1521}
1522
1523int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1524{
1525 struct rb_node *node;
1526 struct extent_state *state;
1527 int ret = 0;
1528
Chris Masoncad321a2008-12-17 14:51:42 -05001529 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001530 /*
1531 * this search will find all the extents that end after
1532 * our range starts.
1533 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001534 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001535 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001536 ret = -ENOENT;
1537 goto out;
1538 }
1539 state = rb_entry(node, struct extent_state, rb_node);
1540 if (state->start != start) {
1541 ret = -ENOENT;
1542 goto out;
1543 }
1544 *private = state->private;
1545out:
Chris Masoncad321a2008-12-17 14:51:42 -05001546 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001547 return ret;
1548}
1549
1550/*
1551 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001552 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001553 * has the bits set. Otherwise, 1 is returned if any bit in the
1554 * range is found set.
1555 */
1556int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001557 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001558{
1559 struct extent_state *state = NULL;
1560 struct rb_node *node;
1561 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001562
Chris Masoncad321a2008-12-17 14:51:42 -05001563 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001564 if (cached && cached->tree && cached->start == start)
1565 node = &cached->rb_node;
1566 else
1567 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001568 while (node && start <= end) {
1569 state = rb_entry(node, struct extent_state, rb_node);
1570
1571 if (filled && state->start > start) {
1572 bitset = 0;
1573 break;
1574 }
1575
1576 if (state->start > end)
1577 break;
1578
1579 if (state->state & bits) {
1580 bitset = 1;
1581 if (!filled)
1582 break;
1583 } else if (filled) {
1584 bitset = 0;
1585 break;
1586 }
Chris Mason46562ce2009-09-23 20:23:16 -04001587
1588 if (state->end == (u64)-1)
1589 break;
1590
Chris Masond1310b22008-01-24 16:13:08 -05001591 start = state->end + 1;
1592 if (start > end)
1593 break;
1594 node = rb_next(node);
1595 if (!node) {
1596 if (filled)
1597 bitset = 0;
1598 break;
1599 }
1600 }
Chris Masoncad321a2008-12-17 14:51:42 -05001601 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001602 return bitset;
1603}
Chris Masond1310b22008-01-24 16:13:08 -05001604
1605/*
1606 * helper function to set a given page up to date if all the
1607 * extents in the tree for that page are up to date
1608 */
1609static int check_page_uptodate(struct extent_io_tree *tree,
1610 struct page *page)
1611{
1612 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1613 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001614 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001615 SetPageUptodate(page);
1616 return 0;
1617}
1618
1619/*
1620 * helper function to unlock a page if all the extents in the tree
1621 * for that page are unlocked
1622 */
1623static int check_page_locked(struct extent_io_tree *tree,
1624 struct page *page)
1625{
1626 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1627 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001628 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001629 unlock_page(page);
1630 return 0;
1631}
1632
1633/*
1634 * helper function to end page writeback if all the extents
1635 * in the tree for that page are done with writeback
1636 */
1637static int check_page_writeback(struct extent_io_tree *tree,
1638 struct page *page)
1639{
Chris Mason1edbb732009-09-02 13:24:36 -04001640 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001641 return 0;
1642}
1643
1644/* lots and lots of room for performance fixes in the end_bio funcs */
1645
1646/*
1647 * after a writepage IO is done, we need to:
1648 * clear the uptodate bits on error
1649 * clear the writeback bits in the extent tree for this IO
1650 * end_page_writeback if the page has no more pending IO
1651 *
1652 * Scheduling is not allowed, so the extent state tree is expected
1653 * to have one and only one object corresponding to this IO.
1654 */
Chris Masond1310b22008-01-24 16:13:08 -05001655static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001656{
Chris Mason1259ab72008-05-12 13:39:03 -04001657 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001658 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001659 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001660 u64 start;
1661 u64 end;
1662 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001663 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001664
Chris Masond1310b22008-01-24 16:13:08 -05001665 do {
1666 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001667 tree = &BTRFS_I(page->mapping->host)->io_tree;
1668
Chris Masond1310b22008-01-24 16:13:08 -05001669 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1670 bvec->bv_offset;
1671 end = start + bvec->bv_len - 1;
1672
1673 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1674 whole_page = 1;
1675 else
1676 whole_page = 0;
1677
1678 if (--bvec >= bio->bi_io_vec)
1679 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001680 if (tree->ops && tree->ops->writepage_end_io_hook) {
1681 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001682 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001683 if (ret)
1684 uptodate = 0;
1685 }
1686
1687 if (!uptodate && tree->ops &&
1688 tree->ops->writepage_io_failed_hook) {
1689 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001690 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001691 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001692 uptodate = (err == 0);
1693 continue;
1694 }
1695 }
1696
Chris Masond1310b22008-01-24 16:13:08 -05001697 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001698 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001699 ClearPageUptodate(page);
1700 SetPageError(page);
1701 }
Chris Mason70dec802008-01-29 09:59:12 -05001702
Chris Masond1310b22008-01-24 16:13:08 -05001703 if (whole_page)
1704 end_page_writeback(page);
1705 else
1706 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001707 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001708
Chris Masond1310b22008-01-24 16:13:08 -05001709 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001710}
1711
1712/*
1713 * after a readpage IO is done, we need to:
1714 * clear the uptodate bits on error
1715 * set the uptodate bits if things worked
1716 * set the page up to date if all extents in the tree are uptodate
1717 * clear the lock bit in the extent tree
1718 * unlock the page if there are no other extents locked for it
1719 *
1720 * Scheduling is not allowed, so the extent state tree is expected
1721 * to have one and only one object corresponding to this IO.
1722 */
Chris Masond1310b22008-01-24 16:13:08 -05001723static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001724{
1725 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001726 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1727 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001728 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001729 u64 start;
1730 u64 end;
1731 int whole_page;
1732 int ret;
1733
Chris Masond20f7042008-12-08 16:58:54 -05001734 if (err)
1735 uptodate = 0;
1736
Chris Masond1310b22008-01-24 16:13:08 -05001737 do {
1738 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001739 struct extent_state *cached = NULL;
1740 struct extent_state *state;
1741
David Woodhouse902b22f2008-08-20 08:51:49 -04001742 tree = &BTRFS_I(page->mapping->host)->io_tree;
1743
Chris Masond1310b22008-01-24 16:13:08 -05001744 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1745 bvec->bv_offset;
1746 end = start + bvec->bv_len - 1;
1747
1748 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1749 whole_page = 1;
1750 else
1751 whole_page = 0;
1752
Chris Mason4125bf72010-02-03 18:18:45 +00001753 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001754 prefetchw(&bvec->bv_page->flags);
1755
Arne Jansen507903b2011-04-06 10:02:20 +00001756 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001757 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001758 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001759 /*
1760 * take a reference on the state, unlock will drop
1761 * the ref
1762 */
1763 cache_state(state, &cached);
1764 }
1765 spin_unlock(&tree->lock);
1766
Chris Masond1310b22008-01-24 16:13:08 -05001767 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001768 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001769 state);
Chris Masond1310b22008-01-24 16:13:08 -05001770 if (ret)
1771 uptodate = 0;
1772 }
Chris Mason7e383262008-04-09 16:28:12 -04001773 if (!uptodate && tree->ops &&
1774 tree->ops->readpage_io_failed_hook) {
1775 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001776 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001777 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001778 uptodate =
1779 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001780 if (err)
1781 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001782 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001783 continue;
1784 }
1785 }
Chris Mason70dec802008-01-29 09:59:12 -05001786
Chris Mason771ed682008-11-06 22:02:51 -05001787 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001788 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001789 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001790 }
Arne Jansen507903b2011-04-06 10:02:20 +00001791 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001792
Chris Mason70dec802008-01-29 09:59:12 -05001793 if (whole_page) {
1794 if (uptodate) {
1795 SetPageUptodate(page);
1796 } else {
1797 ClearPageUptodate(page);
1798 SetPageError(page);
1799 }
Chris Masond1310b22008-01-24 16:13:08 -05001800 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001801 } else {
1802 if (uptodate) {
1803 check_page_uptodate(tree, page);
1804 } else {
1805 ClearPageUptodate(page);
1806 SetPageError(page);
1807 }
Chris Masond1310b22008-01-24 16:13:08 -05001808 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001809 }
Chris Mason4125bf72010-02-03 18:18:45 +00001810 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001811
1812 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001813}
1814
Miao Xie88f794e2010-11-22 03:02:55 +00001815struct bio *
1816btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1817 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001818{
1819 struct bio *bio;
1820
1821 bio = bio_alloc(gfp_flags, nr_vecs);
1822
1823 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1824 while (!bio && (nr_vecs /= 2))
1825 bio = bio_alloc(gfp_flags, nr_vecs);
1826 }
1827
1828 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001829 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001830 bio->bi_bdev = bdev;
1831 bio->bi_sector = first_sector;
1832 }
1833 return bio;
1834}
1835
Chris Masonc8b97812008-10-29 14:49:59 -04001836static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1837 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001838{
Chris Masond1310b22008-01-24 16:13:08 -05001839 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001840 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1841 struct page *page = bvec->bv_page;
1842 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001843 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001844
1845 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001846
David Woodhouse902b22f2008-08-20 08:51:49 -04001847 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001848
1849 bio_get(bio);
1850
Chris Mason065631f2008-02-20 12:07:25 -05001851 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001852 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001853 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001854 else
1855 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001856 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1857 ret = -EOPNOTSUPP;
1858 bio_put(bio);
1859 return ret;
1860}
1861
1862static int submit_extent_page(int rw, struct extent_io_tree *tree,
1863 struct page *page, sector_t sector,
1864 size_t size, unsigned long offset,
1865 struct block_device *bdev,
1866 struct bio **bio_ret,
1867 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001868 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001869 int mirror_num,
1870 unsigned long prev_bio_flags,
1871 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001872{
1873 int ret = 0;
1874 struct bio *bio;
1875 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001876 int contig = 0;
1877 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1878 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001879 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001880
1881 if (bio_ret && *bio_ret) {
1882 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001883 if (old_compressed)
1884 contig = bio->bi_sector == sector;
1885 else
1886 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1887 sector;
1888
1889 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001890 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001891 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1892 bio_flags)) ||
1893 bio_add_page(bio, page, page_size, offset) < page_size) {
1894 ret = submit_one_bio(rw, bio, mirror_num,
1895 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001896 bio = NULL;
1897 } else {
1898 return 0;
1899 }
1900 }
Chris Masonc8b97812008-10-29 14:49:59 -04001901 if (this_compressed)
1902 nr = BIO_MAX_PAGES;
1903 else
1904 nr = bio_get_nr_vecs(bdev);
1905
Miao Xie88f794e2010-11-22 03:02:55 +00001906 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001907 if (!bio)
1908 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001909
Chris Masonc8b97812008-10-29 14:49:59 -04001910 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001911 bio->bi_end_io = end_io_func;
1912 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001913
Chris Masond3977122009-01-05 21:25:51 -05001914 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001915 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001916 else
Chris Masonc8b97812008-10-29 14:49:59 -04001917 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001918
1919 return ret;
1920}
1921
1922void set_page_extent_mapped(struct page *page)
1923{
1924 if (!PagePrivate(page)) {
1925 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001926 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04001927 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001928 }
1929}
1930
Christoph Hellwigb2950862008-12-02 09:54:17 -05001931static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001932{
Chris Masoneb14ab82011-02-10 12:35:00 -05001933 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05001934 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1935}
1936
1937/*
1938 * basic readpage implementation. Locked extent state structs are inserted
1939 * into the tree that are removed when the IO is done (by the end_io
1940 * handlers)
1941 */
1942static int __extent_read_full_page(struct extent_io_tree *tree,
1943 struct page *page,
1944 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001945 struct bio **bio, int mirror_num,
1946 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001947{
1948 struct inode *inode = page->mapping->host;
1949 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1950 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1951 u64 end;
1952 u64 cur = start;
1953 u64 extent_offset;
1954 u64 last_byte = i_size_read(inode);
1955 u64 block_start;
1956 u64 cur_end;
1957 sector_t sector;
1958 struct extent_map *em;
1959 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001960 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05001961 int ret;
1962 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02001963 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001964 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001965 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001966 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001967 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001968
1969 set_page_extent_mapped(page);
1970
1971 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001972 while (1) {
1973 lock_extent(tree, start, end, GFP_NOFS);
1974 ordered = btrfs_lookup_ordered_extent(inode, start);
1975 if (!ordered)
1976 break;
1977 unlock_extent(tree, start, end, GFP_NOFS);
1978 btrfs_start_ordered_extent(inode, ordered, 1);
1979 btrfs_put_ordered_extent(ordered);
1980 }
Chris Masond1310b22008-01-24 16:13:08 -05001981
Chris Masonc8b97812008-10-29 14:49:59 -04001982 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1983 char *userpage;
1984 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1985
1986 if (zero_offset) {
1987 iosize = PAGE_CACHE_SIZE - zero_offset;
1988 userpage = kmap_atomic(page, KM_USER0);
1989 memset(userpage + zero_offset, 0, iosize);
1990 flush_dcache_page(page);
1991 kunmap_atomic(userpage, KM_USER0);
1992 }
1993 }
Chris Masond1310b22008-01-24 16:13:08 -05001994 while (cur <= end) {
1995 if (cur >= last_byte) {
1996 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00001997 struct extent_state *cached = NULL;
1998
David Sterba306e16c2011-04-19 14:29:38 +02001999 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002000 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002001 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002002 flush_dcache_page(page);
2003 kunmap_atomic(userpage, KM_USER0);
2004 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002005 &cached, GFP_NOFS);
2006 unlock_extent_cached(tree, cur, cur + iosize - 1,
2007 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002008 break;
2009 }
David Sterba306e16c2011-04-19 14:29:38 +02002010 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002011 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002012 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002013 SetPageError(page);
2014 unlock_extent(tree, cur, end, GFP_NOFS);
2015 break;
2016 }
Chris Masond1310b22008-01-24 16:13:08 -05002017 extent_offset = cur - em->start;
2018 BUG_ON(extent_map_end(em) <= cur);
2019 BUG_ON(end < cur);
2020
Li Zefan261507a02010-12-17 14:21:50 +08002021 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002022 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002023 extent_set_compress_type(&this_bio_flag,
2024 em->compress_type);
2025 }
Chris Masonc8b97812008-10-29 14:49:59 -04002026
Chris Masond1310b22008-01-24 16:13:08 -05002027 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2028 cur_end = min(extent_map_end(em) - 1, end);
2029 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002030 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2031 disk_io_size = em->block_len;
2032 sector = em->block_start >> 9;
2033 } else {
2034 sector = (em->block_start + extent_offset) >> 9;
2035 disk_io_size = iosize;
2036 }
Chris Masond1310b22008-01-24 16:13:08 -05002037 bdev = em->bdev;
2038 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002039 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2040 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002041 free_extent_map(em);
2042 em = NULL;
2043
2044 /* we've found a hole, just zero and go on */
2045 if (block_start == EXTENT_MAP_HOLE) {
2046 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002047 struct extent_state *cached = NULL;
2048
Chris Masond1310b22008-01-24 16:13:08 -05002049 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002050 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002051 flush_dcache_page(page);
2052 kunmap_atomic(userpage, KM_USER0);
2053
2054 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002055 &cached, GFP_NOFS);
2056 unlock_extent_cached(tree, cur, cur + iosize - 1,
2057 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002058 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002059 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002060 continue;
2061 }
2062 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002063 if (test_range_bit(tree, cur, cur_end,
2064 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002065 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002066 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2067 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002068 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002069 continue;
2070 }
Chris Mason70dec802008-01-29 09:59:12 -05002071 /* we have an inline extent but it didn't get marked up
2072 * to date. Error out
2073 */
2074 if (block_start == EXTENT_MAP_INLINE) {
2075 SetPageError(page);
2076 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2077 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002078 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002079 continue;
2080 }
Chris Masond1310b22008-01-24 16:13:08 -05002081
2082 ret = 0;
2083 if (tree->ops && tree->ops->readpage_io_hook) {
2084 ret = tree->ops->readpage_io_hook(page, cur,
2085 cur + iosize - 1);
2086 }
2087 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002088 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2089 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002090 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002091 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002092 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002093 end_bio_extent_readpage, mirror_num,
2094 *bio_flags,
2095 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002096 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002097 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002098 }
2099 if (ret)
2100 SetPageError(page);
2101 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002102 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002103 }
2104 if (!nr) {
2105 if (!PageError(page))
2106 SetPageUptodate(page);
2107 unlock_page(page);
2108 }
2109 return 0;
2110}
2111
2112int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2113 get_extent_t *get_extent)
2114{
2115 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002116 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002117 int ret;
2118
Chris Masonc8b97812008-10-29 14:49:59 -04002119 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2120 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002121 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002122 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002123 return ret;
2124}
Chris Masond1310b22008-01-24 16:13:08 -05002125
Chris Mason11c83492009-04-20 15:50:09 -04002126static noinline void update_nr_written(struct page *page,
2127 struct writeback_control *wbc,
2128 unsigned long nr_written)
2129{
2130 wbc->nr_to_write -= nr_written;
2131 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2132 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2133 page->mapping->writeback_index = page->index + nr_written;
2134}
2135
Chris Masond1310b22008-01-24 16:13:08 -05002136/*
2137 * the writepage semantics are similar to regular writepage. extent
2138 * records are inserted to lock ranges in the tree, and as dirty areas
2139 * are found, they are marked writeback. Then the lock bits are removed
2140 * and the end_io handler clears the writeback ranges
2141 */
2142static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2143 void *data)
2144{
2145 struct inode *inode = page->mapping->host;
2146 struct extent_page_data *epd = data;
2147 struct extent_io_tree *tree = epd->tree;
2148 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2149 u64 delalloc_start;
2150 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2151 u64 end;
2152 u64 cur = start;
2153 u64 extent_offset;
2154 u64 last_byte = i_size_read(inode);
2155 u64 block_start;
2156 u64 iosize;
2157 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002158 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002159 struct extent_map *em;
2160 struct block_device *bdev;
2161 int ret;
2162 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002163 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002164 size_t blocksize;
2165 loff_t i_size = i_size_read(inode);
2166 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2167 u64 nr_delalloc;
2168 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002169 int page_started;
2170 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002171 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002172 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002173
Chris Masonffbd5172009-04-20 15:50:09 -04002174 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002175 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002176 else
2177 write_flags = WRITE;
2178
liubo1abe9b82011-03-24 11:18:59 +00002179 trace___extent_writepage(page, inode, wbc);
2180
Chris Masond1310b22008-01-24 16:13:08 -05002181 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002182 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002183 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002184 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002185 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002186 unlock_page(page);
2187 return 0;
2188 }
2189
2190 if (page->index == end_index) {
2191 char *userpage;
2192
Chris Masond1310b22008-01-24 16:13:08 -05002193 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002194 memset(userpage + pg_offset, 0,
2195 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002196 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002197 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002198 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002199 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002200
2201 set_page_extent_mapped(page);
2202
2203 delalloc_start = start;
2204 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002205 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002206 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002207 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002208 /*
2209 * make sure the wbc mapping index is at least updated
2210 * to this page.
2211 */
2212 update_nr_written(page, wbc, 0);
2213
Chris Masond3977122009-01-05 21:25:51 -05002214 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002215 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002216 page,
2217 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002218 &delalloc_end,
2219 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002220 if (nr_delalloc == 0) {
2221 delalloc_start = delalloc_end + 1;
2222 continue;
2223 }
2224 tree->ops->fill_delalloc(inode, page, delalloc_start,
2225 delalloc_end, &page_started,
2226 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002227 /*
2228 * delalloc_end is already one less than the total
2229 * length, so we don't subtract one from
2230 * PAGE_CACHE_SIZE
2231 */
2232 delalloc_to_write += (delalloc_end - delalloc_start +
2233 PAGE_CACHE_SIZE) >>
2234 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002235 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002236 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002237 if (wbc->nr_to_write < delalloc_to_write) {
2238 int thresh = 8192;
2239
2240 if (delalloc_to_write < thresh * 2)
2241 thresh = delalloc_to_write;
2242 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2243 thresh);
2244 }
Chris Masonc8b97812008-10-29 14:49:59 -04002245
Chris Mason771ed682008-11-06 22:02:51 -05002246 /* did the fill delalloc function already unlock and start
2247 * the IO?
2248 */
2249 if (page_started) {
2250 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002251 /*
2252 * we've unlocked the page, so we can't update
2253 * the mapping's writeback index, just update
2254 * nr_to_write.
2255 */
2256 wbc->nr_to_write -= nr_written;
2257 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002258 }
Chris Masonc8b97812008-10-29 14:49:59 -04002259 }
Chris Mason247e7432008-07-17 12:53:51 -04002260 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002261 ret = tree->ops->writepage_start_hook(page, start,
2262 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002263 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002264 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002265 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002266 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002267 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002268 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002269 }
2270 }
2271
Chris Mason11c83492009-04-20 15:50:09 -04002272 /*
2273 * we don't want to touch the inode after unlocking the page,
2274 * so we update the mapping writeback index now
2275 */
2276 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002277
Chris Masond1310b22008-01-24 16:13:08 -05002278 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002279 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002280 if (tree->ops && tree->ops->writepage_end_io_hook)
2281 tree->ops->writepage_end_io_hook(page, start,
2282 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002283 goto done;
2284 }
2285
Chris Masond1310b22008-01-24 16:13:08 -05002286 blocksize = inode->i_sb->s_blocksize;
2287
2288 while (cur <= end) {
2289 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002290 if (tree->ops && tree->ops->writepage_end_io_hook)
2291 tree->ops->writepage_end_io_hook(page, cur,
2292 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002293 break;
2294 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002295 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002296 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002297 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002298 SetPageError(page);
2299 break;
2300 }
2301
2302 extent_offset = cur - em->start;
2303 BUG_ON(extent_map_end(em) <= cur);
2304 BUG_ON(end < cur);
2305 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2306 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2307 sector = (em->block_start + extent_offset) >> 9;
2308 bdev = em->bdev;
2309 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002310 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002311 free_extent_map(em);
2312 em = NULL;
2313
Chris Masonc8b97812008-10-29 14:49:59 -04002314 /*
2315 * compressed and inline extents are written through other
2316 * paths in the FS
2317 */
2318 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002319 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002320 /*
2321 * end_io notification does not happen here for
2322 * compressed extents
2323 */
2324 if (!compressed && tree->ops &&
2325 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002326 tree->ops->writepage_end_io_hook(page, cur,
2327 cur + iosize - 1,
2328 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002329 else if (compressed) {
2330 /* we don't want to end_page_writeback on
2331 * a compressed extent. this happens
2332 * elsewhere
2333 */
2334 nr++;
2335 }
2336
2337 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002338 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002339 continue;
2340 }
Chris Masond1310b22008-01-24 16:13:08 -05002341 /* leave this out until we have a page_mkwrite call */
2342 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002343 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002344 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002345 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002346 continue;
2347 }
Chris Masonc8b97812008-10-29 14:49:59 -04002348
Chris Masond1310b22008-01-24 16:13:08 -05002349 if (tree->ops && tree->ops->writepage_io_hook) {
2350 ret = tree->ops->writepage_io_hook(page, cur,
2351 cur + iosize - 1);
2352 } else {
2353 ret = 0;
2354 }
Chris Mason1259ab72008-05-12 13:39:03 -04002355 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002356 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002357 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002358 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002359
Chris Masond1310b22008-01-24 16:13:08 -05002360 set_range_writeback(tree, cur, cur + iosize - 1);
2361 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002362 printk(KERN_ERR "btrfs warning page %lu not "
2363 "writeback, cur %llu end %llu\n",
2364 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002365 (unsigned long long)end);
2366 }
2367
Chris Masonffbd5172009-04-20 15:50:09 -04002368 ret = submit_extent_page(write_flags, tree, page,
2369 sector, iosize, pg_offset,
2370 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002371 end_bio_extent_writepage,
2372 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002373 if (ret)
2374 SetPageError(page);
2375 }
2376 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002377 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002378 nr++;
2379 }
2380done:
2381 if (nr == 0) {
2382 /* make sure the mapping tag for page dirty gets cleared */
2383 set_page_writeback(page);
2384 end_page_writeback(page);
2385 }
Chris Masond1310b22008-01-24 16:13:08 -05002386 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002387
Chris Mason11c83492009-04-20 15:50:09 -04002388done_unlocked:
2389
Chris Mason2c64c532009-09-02 15:04:12 -04002390 /* drop our reference on any cached states */
2391 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002392 return 0;
2393}
2394
Chris Masond1310b22008-01-24 16:13:08 -05002395/**
Chris Mason4bef0842008-09-08 11:18:08 -04002396 * 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 -05002397 * @mapping: address space structure to write
2398 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2399 * @writepage: function called for each page
2400 * @data: data passed to writepage function
2401 *
2402 * If a page is already under I/O, write_cache_pages() skips it, even
2403 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2404 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2405 * and msync() need to guarantee that all the data which was dirty at the time
2406 * the call was made get new I/O started against them. If wbc->sync_mode is
2407 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2408 * existing IO to complete.
2409 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002410static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002411 struct address_space *mapping,
2412 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002413 writepage_t writepage, void *data,
2414 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002415{
Chris Masond1310b22008-01-24 16:13:08 -05002416 int ret = 0;
2417 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002418 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002419 struct pagevec pvec;
2420 int nr_pages;
2421 pgoff_t index;
2422 pgoff_t end; /* Inclusive */
2423 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002424
Chris Masond1310b22008-01-24 16:13:08 -05002425 pagevec_init(&pvec, 0);
2426 if (wbc->range_cyclic) {
2427 index = mapping->writeback_index; /* Start from prev offset */
2428 end = -1;
2429 } else {
2430 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2431 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002432 scanned = 1;
2433 }
2434retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002435 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002436 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002437 PAGECACHE_TAG_DIRTY, min(end - index,
2438 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002439 unsigned i;
2440
2441 scanned = 1;
2442 for (i = 0; i < nr_pages; i++) {
2443 struct page *page = pvec.pages[i];
2444
2445 /*
2446 * At this point we hold neither mapping->tree_lock nor
2447 * lock on the page itself: the page may be truncated or
2448 * invalidated (changing page->mapping to NULL), or even
2449 * swizzled back from swapper_space to tmpfs file
2450 * mapping
2451 */
Chris Mason4bef0842008-09-08 11:18:08 -04002452 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2453 tree->ops->write_cache_pages_lock_hook(page);
2454 else
2455 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002456
2457 if (unlikely(page->mapping != mapping)) {
2458 unlock_page(page);
2459 continue;
2460 }
2461
2462 if (!wbc->range_cyclic && page->index > end) {
2463 done = 1;
2464 unlock_page(page);
2465 continue;
2466 }
2467
Chris Masond2c3f4f2008-11-19 12:44:22 -05002468 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002469 if (PageWriteback(page))
2470 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002471 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002472 }
Chris Masond1310b22008-01-24 16:13:08 -05002473
2474 if (PageWriteback(page) ||
2475 !clear_page_dirty_for_io(page)) {
2476 unlock_page(page);
2477 continue;
2478 }
2479
2480 ret = (*writepage)(page, wbc, data);
2481
2482 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2483 unlock_page(page);
2484 ret = 0;
2485 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002486 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002487 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002488
2489 /*
2490 * the filesystem may choose to bump up nr_to_write.
2491 * We have to make sure to honor the new nr_to_write
2492 * at any time
2493 */
2494 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002495 }
2496 pagevec_release(&pvec);
2497 cond_resched();
2498 }
2499 if (!scanned && !done) {
2500 /*
2501 * We hit the last page and there is more work to be done: wrap
2502 * back to the start of the file
2503 */
2504 scanned = 1;
2505 index = 0;
2506 goto retry;
2507 }
Chris Masond1310b22008-01-24 16:13:08 -05002508 return ret;
2509}
Chris Masond1310b22008-01-24 16:13:08 -05002510
Chris Masonffbd5172009-04-20 15:50:09 -04002511static void flush_epd_write_bio(struct extent_page_data *epd)
2512{
2513 if (epd->bio) {
2514 if (epd->sync_io)
2515 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2516 else
2517 submit_one_bio(WRITE, epd->bio, 0, 0);
2518 epd->bio = NULL;
2519 }
2520}
2521
Chris Masond2c3f4f2008-11-19 12:44:22 -05002522static noinline void flush_write_bio(void *data)
2523{
2524 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002525 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002526}
2527
Chris Masond1310b22008-01-24 16:13:08 -05002528int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2529 get_extent_t *get_extent,
2530 struct writeback_control *wbc)
2531{
2532 int ret;
2533 struct address_space *mapping = page->mapping;
2534 struct extent_page_data epd = {
2535 .bio = NULL,
2536 .tree = tree,
2537 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002538 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002539 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002540 };
2541 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002542 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002543 .older_than_this = NULL,
2544 .nr_to_write = 64,
2545 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2546 .range_end = (loff_t)-1,
2547 };
2548
Chris Masond1310b22008-01-24 16:13:08 -05002549 ret = __extent_writepage(page, wbc, &epd);
2550
Chris Mason4bef0842008-09-08 11:18:08 -04002551 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002552 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002553 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002554 return ret;
2555}
Chris Masond1310b22008-01-24 16:13:08 -05002556
Chris Mason771ed682008-11-06 22:02:51 -05002557int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2558 u64 start, u64 end, get_extent_t *get_extent,
2559 int mode)
2560{
2561 int ret = 0;
2562 struct address_space *mapping = inode->i_mapping;
2563 struct page *page;
2564 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2565 PAGE_CACHE_SHIFT;
2566
2567 struct extent_page_data epd = {
2568 .bio = NULL,
2569 .tree = tree,
2570 .get_extent = get_extent,
2571 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002572 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002573 };
2574 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002575 .sync_mode = mode,
2576 .older_than_this = NULL,
2577 .nr_to_write = nr_pages * 2,
2578 .range_start = start,
2579 .range_end = end + 1,
2580 };
2581
Chris Masond3977122009-01-05 21:25:51 -05002582 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002583 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2584 if (clear_page_dirty_for_io(page))
2585 ret = __extent_writepage(page, &wbc_writepages, &epd);
2586 else {
2587 if (tree->ops && tree->ops->writepage_end_io_hook)
2588 tree->ops->writepage_end_io_hook(page, start,
2589 start + PAGE_CACHE_SIZE - 1,
2590 NULL, 1);
2591 unlock_page(page);
2592 }
2593 page_cache_release(page);
2594 start += PAGE_CACHE_SIZE;
2595 }
2596
Chris Masonffbd5172009-04-20 15:50:09 -04002597 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002598 return ret;
2599}
Chris Masond1310b22008-01-24 16:13:08 -05002600
2601int extent_writepages(struct extent_io_tree *tree,
2602 struct address_space *mapping,
2603 get_extent_t *get_extent,
2604 struct writeback_control *wbc)
2605{
2606 int ret = 0;
2607 struct extent_page_data epd = {
2608 .bio = NULL,
2609 .tree = tree,
2610 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002611 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002612 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002613 };
2614
Chris Mason4bef0842008-09-08 11:18:08 -04002615 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002616 __extent_writepage, &epd,
2617 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002618 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002619 return ret;
2620}
Chris Masond1310b22008-01-24 16:13:08 -05002621
2622int extent_readpages(struct extent_io_tree *tree,
2623 struct address_space *mapping,
2624 struct list_head *pages, unsigned nr_pages,
2625 get_extent_t get_extent)
2626{
2627 struct bio *bio = NULL;
2628 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002629 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002630
Chris Masond1310b22008-01-24 16:13:08 -05002631 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2632 struct page *page = list_entry(pages->prev, struct page, lru);
2633
2634 prefetchw(&page->flags);
2635 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00002636 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002637 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002638 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002639 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002640 }
2641 page_cache_release(page);
2642 }
Chris Masond1310b22008-01-24 16:13:08 -05002643 BUG_ON(!list_empty(pages));
2644 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002645 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002646 return 0;
2647}
Chris Masond1310b22008-01-24 16:13:08 -05002648
2649/*
2650 * basic invalidatepage code, this waits on any locked or writeback
2651 * ranges corresponding to the page, and then deletes any extent state
2652 * records from the tree
2653 */
2654int extent_invalidatepage(struct extent_io_tree *tree,
2655 struct page *page, unsigned long offset)
2656{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002657 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002658 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2659 u64 end = start + PAGE_CACHE_SIZE - 1;
2660 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2661
Chris Masond3977122009-01-05 21:25:51 -05002662 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002663 if (start > end)
2664 return 0;
2665
Josef Bacik2ac55d42010-02-03 19:33:23 +00002666 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002667 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002668 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002669 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2670 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002671 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002672 return 0;
2673}
Chris Masond1310b22008-01-24 16:13:08 -05002674
2675/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002676 * a helper for releasepage, this tests for areas of the page that
2677 * are locked or under IO and drops the related state bits if it is safe
2678 * to drop the page.
2679 */
2680int try_release_extent_state(struct extent_map_tree *map,
2681 struct extent_io_tree *tree, struct page *page,
2682 gfp_t mask)
2683{
2684 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2685 u64 end = start + PAGE_CACHE_SIZE - 1;
2686 int ret = 1;
2687
Chris Mason211f90e2008-07-18 11:56:15 -04002688 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002689 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002690 ret = 0;
2691 else {
2692 if ((mask & GFP_NOFS) == GFP_NOFS)
2693 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002694 /*
2695 * at this point we can safely clear everything except the
2696 * locked bit and the nodatasum bit
2697 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002698 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002699 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2700 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002701
2702 /* if clear_extent_bit failed for enomem reasons,
2703 * we can't allow the release to continue.
2704 */
2705 if (ret < 0)
2706 ret = 0;
2707 else
2708 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002709 }
2710 return ret;
2711}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002712
2713/*
Chris Masond1310b22008-01-24 16:13:08 -05002714 * a helper for releasepage. As long as there are no locked extents
2715 * in the range corresponding to the page, both state records and extent
2716 * map records are removed
2717 */
2718int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002719 struct extent_io_tree *tree, struct page *page,
2720 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002721{
2722 struct extent_map *em;
2723 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2724 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002725
Chris Mason70dec802008-01-29 09:59:12 -05002726 if ((mask & __GFP_WAIT) &&
2727 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002728 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002729 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002730 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002731 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002732 em = lookup_extent_mapping(map, start, len);
David Sterbac7040052011-04-19 18:00:01 +02002733 if (IS_ERR_OR_NULL(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002734 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002735 break;
2736 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002737 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2738 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002739 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002740 free_extent_map(em);
2741 break;
2742 }
2743 if (!test_range_bit(tree, em->start,
2744 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002745 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002746 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002747 remove_extent_mapping(map, em);
2748 /* once for the rb tree */
2749 free_extent_map(em);
2750 }
2751 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002752 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002753
2754 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002755 free_extent_map(em);
2756 }
Chris Masond1310b22008-01-24 16:13:08 -05002757 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002758 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002759}
Chris Masond1310b22008-01-24 16:13:08 -05002760
Chris Masonec29ed52011-02-23 16:23:20 -05002761/*
2762 * helper function for fiemap, which doesn't want to see any holes.
2763 * This maps until we find something past 'last'
2764 */
2765static struct extent_map *get_extent_skip_holes(struct inode *inode,
2766 u64 offset,
2767 u64 last,
2768 get_extent_t *get_extent)
2769{
2770 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2771 struct extent_map *em;
2772 u64 len;
2773
2774 if (offset >= last)
2775 return NULL;
2776
2777 while(1) {
2778 len = last - offset;
2779 if (len == 0)
2780 break;
2781 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2782 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02002783 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05002784 return em;
2785
2786 /* if this isn't a hole return it */
2787 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2788 em->block_start != EXTENT_MAP_HOLE) {
2789 return em;
2790 }
2791
2792 /* this is a hole, advance to the next extent */
2793 offset = extent_map_end(em);
2794 free_extent_map(em);
2795 if (offset >= last)
2796 break;
2797 }
2798 return NULL;
2799}
2800
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002801int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2802 __u64 start, __u64 len, get_extent_t *get_extent)
2803{
Josef Bacik975f84f2010-11-23 19:36:57 +00002804 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002805 u64 off = start;
2806 u64 max = start + len;
2807 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00002808 u32 found_type;
2809 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05002810 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002811 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002812 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00002813 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002814 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002815 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00002816 struct btrfs_path *path;
2817 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002818 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002819 u64 em_start = 0;
2820 u64 em_len = 0;
2821 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002822 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002823
2824 if (len == 0)
2825 return -EINVAL;
2826
Josef Bacik975f84f2010-11-23 19:36:57 +00002827 path = btrfs_alloc_path();
2828 if (!path)
2829 return -ENOMEM;
2830 path->leave_spinning = 1;
2831
Chris Masonec29ed52011-02-23 16:23:20 -05002832 /*
2833 * lookup the last file extent. We're not using i_size here
2834 * because there might be preallocation past i_size
2835 */
Josef Bacik975f84f2010-11-23 19:36:57 +00002836 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08002837 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00002838 if (ret < 0) {
2839 btrfs_free_path(path);
2840 return ret;
2841 }
2842 WARN_ON(!ret);
2843 path->slots[0]--;
2844 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2845 struct btrfs_file_extent_item);
2846 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
2847 found_type = btrfs_key_type(&found_key);
2848
Chris Masonec29ed52011-02-23 16:23:20 -05002849 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08002850 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00002851 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05002852 /* have to trust i_size as the end */
2853 last = (u64)-1;
2854 last_for_get_extent = isize;
2855 } else {
2856 /*
2857 * remember the start of the last extent. There are a
2858 * bunch of different factors that go into the length of the
2859 * extent, so its much less complex to remember where it started
2860 */
2861 last = found_key.offset;
2862 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00002863 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002864 btrfs_free_path(path);
2865
Chris Masonec29ed52011-02-23 16:23:20 -05002866 /*
2867 * we might have some extents allocated but more delalloc past those
2868 * extents. so, we trust isize unless the start of the last extent is
2869 * beyond isize
2870 */
2871 if (last < isize) {
2872 last = (u64)-1;
2873 last_for_get_extent = isize;
2874 }
2875
Josef Bacik2ac55d42010-02-03 19:33:23 +00002876 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2877 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05002878
2879 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2880 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002881 if (!em)
2882 goto out;
2883 if (IS_ERR(em)) {
2884 ret = PTR_ERR(em);
2885 goto out;
2886 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002887
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002888 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05002889 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002890
Chris Masonea8efc72011-03-08 11:54:40 -05002891 /* break if the extent we found is outside the range */
2892 if (em->start >= max || extent_map_end(em) < off)
2893 break;
2894
2895 /*
2896 * get_extent may return an extent that starts before our
2897 * requested range. We have to make sure the ranges
2898 * we return to fiemap always move forward and don't
2899 * overlap, so adjust the offsets here
2900 */
2901 em_start = max(em->start, off);
2902
2903 /*
2904 * record the offset from the start of the extent
2905 * for adjusting the disk offset below
2906 */
2907 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05002908 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05002909 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05002910 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002911 disko = 0;
2912 flags = 0;
2913
Chris Masonea8efc72011-03-08 11:54:40 -05002914 /*
2915 * bump off for our next call to get_extent
2916 */
2917 off = extent_map_end(em);
2918 if (off >= max)
2919 end = 1;
2920
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002921 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002922 end = 1;
2923 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002924 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002925 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2926 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002927 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002928 flags |= (FIEMAP_EXTENT_DELALLOC |
2929 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002930 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05002931 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002932 }
2933 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2934 flags |= FIEMAP_EXTENT_ENCODED;
2935
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002936 free_extent_map(em);
2937 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05002938 if ((em_start >= last) || em_len == (u64)-1 ||
2939 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002940 flags |= FIEMAP_EXTENT_LAST;
2941 end = 1;
2942 }
2943
Chris Masonec29ed52011-02-23 16:23:20 -05002944 /* now scan forward to see if this is really the last extent. */
2945 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2946 get_extent);
2947 if (IS_ERR(em)) {
2948 ret = PTR_ERR(em);
2949 goto out;
2950 }
2951 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00002952 flags |= FIEMAP_EXTENT_LAST;
2953 end = 1;
2954 }
Chris Masonec29ed52011-02-23 16:23:20 -05002955 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2956 em_len, flags);
2957 if (ret)
2958 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002959 }
2960out_free:
2961 free_extent_map(em);
2962out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002963 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
2964 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002965 return ret;
2966}
2967
Chris Masond1310b22008-01-24 16:13:08 -05002968static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2969 unsigned long i)
2970{
2971 struct page *p;
2972 struct address_space *mapping;
2973
2974 if (i == 0)
2975 return eb->first_page;
2976 i += eb->start >> PAGE_CACHE_SHIFT;
2977 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002978 if (!mapping)
2979 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002980
2981 /*
2982 * extent_buffer_page is only called after pinning the page
2983 * by increasing the reference count. So we know the page must
2984 * be in the radix tree.
2985 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002986 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002987 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002988 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002989
Chris Masond1310b22008-01-24 16:13:08 -05002990 return p;
2991}
2992
Chris Mason6af118c2008-07-22 11:18:07 -04002993static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002994{
Chris Mason6af118c2008-07-22 11:18:07 -04002995 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2996 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002997}
2998
Chris Masond1310b22008-01-24 16:13:08 -05002999static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3000 u64 start,
3001 unsigned long len,
3002 gfp_t mask)
3003{
3004 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003005#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003006 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003007#endif
Chris Masond1310b22008-01-24 16:13:08 -05003008
Chris Masond1310b22008-01-24 16:13:08 -05003009 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003010 if (eb == NULL)
3011 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003012 eb->start = start;
3013 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003014 spin_lock_init(&eb->lock);
3015 init_waitqueue_head(&eb->lock_wq);
3016
Chris Mason39351272009-02-04 09:24:05 -05003017#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003018 spin_lock_irqsave(&leak_lock, flags);
3019 list_add(&eb->leak_list, &buffers);
3020 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003021#endif
Chris Masond1310b22008-01-24 16:13:08 -05003022 atomic_set(&eb->refs, 1);
3023
3024 return eb;
3025}
3026
3027static void __free_extent_buffer(struct extent_buffer *eb)
3028{
Chris Mason39351272009-02-04 09:24:05 -05003029#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003030 unsigned long flags;
3031 spin_lock_irqsave(&leak_lock, flags);
3032 list_del(&eb->leak_list);
3033 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003034#endif
Chris Masond1310b22008-01-24 16:13:08 -05003035 kmem_cache_free(extent_buffer_cache, eb);
3036}
3037
Miao Xie897ca6e2010-10-26 20:57:29 -04003038/*
3039 * Helper for releasing extent buffer page.
3040 */
3041static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3042 unsigned long start_idx)
3043{
3044 unsigned long index;
3045 struct page *page;
3046
3047 if (!eb->first_page)
3048 return;
3049
3050 index = num_extent_pages(eb->start, eb->len);
3051 if (start_idx >= index)
3052 return;
3053
3054 do {
3055 index--;
3056 page = extent_buffer_page(eb, index);
3057 if (page)
3058 page_cache_release(page);
3059 } while (index != start_idx);
3060}
3061
3062/*
3063 * Helper for releasing the extent buffer.
3064 */
3065static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3066{
3067 btrfs_release_extent_buffer_page(eb, 0);
3068 __free_extent_buffer(eb);
3069}
3070
Chris Masond1310b22008-01-24 16:13:08 -05003071struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3072 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003073 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003074{
3075 unsigned long num_pages = num_extent_pages(start, len);
3076 unsigned long i;
3077 unsigned long index = start >> PAGE_CACHE_SHIFT;
3078 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003079 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003080 struct page *p;
3081 struct address_space *mapping = tree->mapping;
3082 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003083 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003084
Miao Xie19fe0a82010-10-26 20:57:29 -04003085 rcu_read_lock();
3086 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3087 if (eb && atomic_inc_not_zero(&eb->refs)) {
3088 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003089 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003090 return eb;
3091 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003092 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04003093
David Sterbaba144192011-04-21 01:12:06 +02003094 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003095 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003096 return NULL;
3097
Chris Masond1310b22008-01-24 16:13:08 -05003098 if (page0) {
3099 eb->first_page = page0;
3100 i = 1;
3101 index++;
3102 page_cache_get(page0);
3103 mark_page_accessed(page0);
3104 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003105 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003106 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003107 } else {
3108 i = 0;
3109 }
3110 for (; i < num_pages; i++, index++) {
David Sterbaba144192011-04-21 01:12:06 +02003111 p = find_or_create_page(mapping, index, GFP_NOFS | __GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -05003112 if (!p) {
3113 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003114 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003115 }
3116 set_page_extent_mapped(p);
3117 mark_page_accessed(p);
3118 if (i == 0) {
3119 eb->first_page = p;
3120 set_page_extent_head(p, len);
3121 } else {
3122 set_page_private(p, EXTENT_PAGE_PRIVATE);
3123 }
3124 if (!PageUptodate(p))
3125 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003126
3127 /*
3128 * see below about how we avoid a nasty race with release page
3129 * and why we unlock later
3130 */
3131 if (i != 0)
3132 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003133 }
3134 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003135 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003136
Miao Xie19fe0a82010-10-26 20:57:29 -04003137 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3138 if (ret)
3139 goto free_eb;
3140
Chris Mason6af118c2008-07-22 11:18:07 -04003141 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003142 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3143 if (ret == -EEXIST) {
3144 exists = radix_tree_lookup(&tree->buffer,
3145 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003146 /* add one reference for the caller */
3147 atomic_inc(&exists->refs);
3148 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003149 radix_tree_preload_end();
Chris Mason6af118c2008-07-22 11:18:07 -04003150 goto free_eb;
3151 }
Chris Mason6af118c2008-07-22 11:18:07 -04003152 /* add one reference for the tree */
3153 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003154 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003155 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003156
3157 /*
3158 * there is a race where release page may have
3159 * tried to find this extent buffer in the radix
3160 * but failed. It will tell the VM it is safe to
3161 * reclaim the, and it will clear the page private bit.
3162 * We must make sure to set the page private bit properly
3163 * after the extent buffer is in the radix tree so
3164 * it doesn't get lost
3165 */
3166 set_page_extent_mapped(eb->first_page);
3167 set_page_extent_head(eb->first_page, eb->len);
3168 if (!page0)
3169 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003170 return eb;
3171
Chris Mason6af118c2008-07-22 11:18:07 -04003172free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003173 if (eb->first_page && !page0)
3174 unlock_page(eb->first_page);
3175
Chris Masond1310b22008-01-24 16:13:08 -05003176 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003177 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003178 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003179 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003180}
Chris Masond1310b22008-01-24 16:13:08 -05003181
3182struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003183 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003184{
Chris Masond1310b22008-01-24 16:13:08 -05003185 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003186
Miao Xie19fe0a82010-10-26 20:57:29 -04003187 rcu_read_lock();
3188 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3189 if (eb && atomic_inc_not_zero(&eb->refs)) {
3190 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003191 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003192 return eb;
3193 }
3194 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003195
Miao Xie19fe0a82010-10-26 20:57:29 -04003196 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003197}
Chris Masond1310b22008-01-24 16:13:08 -05003198
3199void free_extent_buffer(struct extent_buffer *eb)
3200{
Chris Masond1310b22008-01-24 16:13:08 -05003201 if (!eb)
3202 return;
3203
3204 if (!atomic_dec_and_test(&eb->refs))
3205 return;
3206
Chris Mason6af118c2008-07-22 11:18:07 -04003207 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003208}
Chris Masond1310b22008-01-24 16:13:08 -05003209
3210int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3211 struct extent_buffer *eb)
3212{
Chris Masond1310b22008-01-24 16:13:08 -05003213 unsigned long i;
3214 unsigned long num_pages;
3215 struct page *page;
3216
Chris Masond1310b22008-01-24 16:13:08 -05003217 num_pages = num_extent_pages(eb->start, eb->len);
3218
3219 for (i = 0; i < num_pages; i++) {
3220 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003221 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003222 continue;
3223
Chris Masona61e6f22008-07-22 11:18:08 -04003224 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003225 WARN_ON(!PagePrivate(page));
3226
3227 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003228 if (i == 0)
3229 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003230
Chris Masond1310b22008-01-24 16:13:08 -05003231 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003232 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003233 if (!PageDirty(page)) {
3234 radix_tree_tag_clear(&page->mapping->page_tree,
3235 page_index(page),
3236 PAGECACHE_TAG_DIRTY);
3237 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003238 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003239 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003240 }
3241 return 0;
3242}
Chris Masond1310b22008-01-24 16:13:08 -05003243
Chris Masond1310b22008-01-24 16:13:08 -05003244int set_extent_buffer_dirty(struct extent_io_tree *tree,
3245 struct extent_buffer *eb)
3246{
3247 unsigned long i;
3248 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003249 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003250
Chris Masonb9473432009-03-13 11:00:37 -04003251 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003252 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003253 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003254 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003255 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003256}
Chris Masond1310b22008-01-24 16:13:08 -05003257
Chris Mason1259ab72008-05-12 13:39:03 -04003258int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003259 struct extent_buffer *eb,
3260 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003261{
3262 unsigned long i;
3263 struct page *page;
3264 unsigned long num_pages;
3265
3266 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003267 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003268
3269 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003270 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003271 for (i = 0; i < num_pages; i++) {
3272 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003273 if (page)
3274 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003275 }
3276 return 0;
3277}
3278
Chris Masond1310b22008-01-24 16:13:08 -05003279int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3280 struct extent_buffer *eb)
3281{
3282 unsigned long i;
3283 struct page *page;
3284 unsigned long num_pages;
3285
3286 num_pages = num_extent_pages(eb->start, eb->len);
3287
3288 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003289 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003290 for (i = 0; i < num_pages; i++) {
3291 page = extent_buffer_page(eb, i);
3292 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3293 ((i == num_pages - 1) &&
3294 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3295 check_page_uptodate(tree, page);
3296 continue;
3297 }
3298 SetPageUptodate(page);
3299 }
3300 return 0;
3301}
Chris Masond1310b22008-01-24 16:13:08 -05003302
Chris Masonce9adaa2008-04-09 16:28:12 -04003303int extent_range_uptodate(struct extent_io_tree *tree,
3304 u64 start, u64 end)
3305{
3306 struct page *page;
3307 int ret;
3308 int pg_uptodate = 1;
3309 int uptodate;
3310 unsigned long index;
3311
Chris Mason9655d292009-09-02 15:22:30 -04003312 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003313 if (ret)
3314 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003315 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003316 index = start >> PAGE_CACHE_SHIFT;
3317 page = find_get_page(tree->mapping, index);
3318 uptodate = PageUptodate(page);
3319 page_cache_release(page);
3320 if (!uptodate) {
3321 pg_uptodate = 0;
3322 break;
3323 }
3324 start += PAGE_CACHE_SIZE;
3325 }
3326 return pg_uptodate;
3327}
3328
Chris Masond1310b22008-01-24 16:13:08 -05003329int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003330 struct extent_buffer *eb,
3331 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003332{
Chris Mason728131d2008-04-09 16:28:12 -04003333 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003334 unsigned long num_pages;
3335 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003336 struct page *page;
3337 int pg_uptodate = 1;
3338
Chris Masonb4ce94d2009-02-04 09:25:08 -05003339 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003340 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003341
Chris Mason42352982008-04-28 16:40:52 -04003342 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003343 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003344 if (ret)
3345 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003346
3347 num_pages = num_extent_pages(eb->start, eb->len);
3348 for (i = 0; i < num_pages; i++) {
3349 page = extent_buffer_page(eb, i);
3350 if (!PageUptodate(page)) {
3351 pg_uptodate = 0;
3352 break;
3353 }
3354 }
Chris Mason42352982008-04-28 16:40:52 -04003355 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003356}
Chris Masond1310b22008-01-24 16:13:08 -05003357
3358int read_extent_buffer_pages(struct extent_io_tree *tree,
3359 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003360 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003361 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003362{
3363 unsigned long i;
3364 unsigned long start_i;
3365 struct page *page;
3366 int err;
3367 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003368 int locked_pages = 0;
3369 int all_uptodate = 1;
3370 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003371 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003372 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003373 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003374
Chris Masonb4ce94d2009-02-04 09:25:08 -05003375 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003376 return 0;
3377
Chris Masonce9adaa2008-04-09 16:28:12 -04003378 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003379 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003380 return 0;
3381 }
3382
3383 if (start) {
3384 WARN_ON(start < eb->start);
3385 start_i = (start >> PAGE_CACHE_SHIFT) -
3386 (eb->start >> PAGE_CACHE_SHIFT);
3387 } else {
3388 start_i = 0;
3389 }
3390
3391 num_pages = num_extent_pages(eb->start, eb->len);
3392 for (i = start_i; i < num_pages; i++) {
3393 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003394 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003395 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003396 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003397 } else {
3398 lock_page(page);
3399 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003400 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003401 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003402 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003403 }
3404 if (all_uptodate) {
3405 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003406 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003407 goto unlock_exit;
3408 }
3409
3410 for (i = start_i; i < num_pages; i++) {
3411 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003412
3413 WARN_ON(!PagePrivate(page));
3414
3415 set_page_extent_mapped(page);
3416 if (i == 0)
3417 set_page_extent_head(page, eb->len);
3418
Chris Masonce9adaa2008-04-09 16:28:12 -04003419 if (inc_all_pages)
3420 page_cache_get(page);
3421 if (!PageUptodate(page)) {
3422 if (start_i == 0)
3423 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003424 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003425 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003426 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003427 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003428 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003429 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003430 } else {
3431 unlock_page(page);
3432 }
3433 }
3434
Chris Masona86c12c2008-02-07 10:50:54 -05003435 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003436 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003437
Chris Masond3977122009-01-05 21:25:51 -05003438 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003439 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003440
Chris Masond1310b22008-01-24 16:13:08 -05003441 for (i = start_i; i < num_pages; i++) {
3442 page = extent_buffer_page(eb, i);
3443 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003444 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003445 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003446 }
Chris Masond3977122009-01-05 21:25:51 -05003447
Chris Masond1310b22008-01-24 16:13:08 -05003448 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003449 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003450 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003451
3452unlock_exit:
3453 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003454 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003455 page = extent_buffer_page(eb, i);
3456 i++;
3457 unlock_page(page);
3458 locked_pages--;
3459 }
3460 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003461}
Chris Masond1310b22008-01-24 16:13:08 -05003462
3463void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3464 unsigned long start,
3465 unsigned long len)
3466{
3467 size_t cur;
3468 size_t offset;
3469 struct page *page;
3470 char *kaddr;
3471 char *dst = (char *)dstv;
3472 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3473 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003474
3475 WARN_ON(start > eb->len);
3476 WARN_ON(start + len > eb->start + eb->len);
3477
3478 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3479
Chris Masond3977122009-01-05 21:25:51 -05003480 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003481 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003482
3483 cur = min(len, (PAGE_CACHE_SIZE - offset));
3484 kaddr = kmap_atomic(page, KM_USER1);
3485 memcpy(dst, kaddr + offset, cur);
3486 kunmap_atomic(kaddr, KM_USER1);
3487
3488 dst += cur;
3489 len -= cur;
3490 offset = 0;
3491 i++;
3492 }
3493}
Chris Masond1310b22008-01-24 16:13:08 -05003494
3495int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3496 unsigned long min_len, char **token, char **map,
3497 unsigned long *map_start,
3498 unsigned long *map_len, int km)
3499{
3500 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3501 char *kaddr;
3502 struct page *p;
3503 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3504 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3505 unsigned long end_i = (start_offset + start + min_len - 1) >>
3506 PAGE_CACHE_SHIFT;
3507
3508 if (i != end_i)
3509 return -EINVAL;
3510
3511 if (i == 0) {
3512 offset = start_offset;
3513 *map_start = 0;
3514 } else {
3515 offset = 0;
3516 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3517 }
Chris Masond3977122009-01-05 21:25:51 -05003518
Chris Masond1310b22008-01-24 16:13:08 -05003519 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003520 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3521 "wanted %lu %lu\n", (unsigned long long)eb->start,
3522 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003523 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003524 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003525 }
3526
3527 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003528 kaddr = kmap_atomic(p, km);
3529 *token = kaddr;
3530 *map = kaddr + offset;
3531 *map_len = PAGE_CACHE_SIZE - offset;
3532 return 0;
3533}
Chris Masond1310b22008-01-24 16:13:08 -05003534
3535int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3536 unsigned long min_len,
3537 char **token, char **map,
3538 unsigned long *map_start,
3539 unsigned long *map_len, int km)
3540{
3541 int err;
3542 int save = 0;
3543 if (eb->map_token) {
3544 unmap_extent_buffer(eb, eb->map_token, km);
3545 eb->map_token = NULL;
3546 save = 1;
3547 }
3548 err = map_private_extent_buffer(eb, start, min_len, token, map,
3549 map_start, map_len, km);
3550 if (!err && save) {
3551 eb->map_token = *token;
3552 eb->kaddr = *map;
3553 eb->map_start = *map_start;
3554 eb->map_len = *map_len;
3555 }
3556 return err;
3557}
Chris Masond1310b22008-01-24 16:13:08 -05003558
3559void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3560{
3561 kunmap_atomic(token, km);
3562}
Chris Masond1310b22008-01-24 16:13:08 -05003563
3564int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3565 unsigned long start,
3566 unsigned long len)
3567{
3568 size_t cur;
3569 size_t offset;
3570 struct page *page;
3571 char *kaddr;
3572 char *ptr = (char *)ptrv;
3573 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3574 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3575 int ret = 0;
3576
3577 WARN_ON(start > eb->len);
3578 WARN_ON(start + len > eb->start + eb->len);
3579
3580 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3581
Chris Masond3977122009-01-05 21:25:51 -05003582 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003583 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003584
3585 cur = min(len, (PAGE_CACHE_SIZE - offset));
3586
3587 kaddr = kmap_atomic(page, KM_USER0);
3588 ret = memcmp(ptr, kaddr + offset, cur);
3589 kunmap_atomic(kaddr, KM_USER0);
3590 if (ret)
3591 break;
3592
3593 ptr += cur;
3594 len -= cur;
3595 offset = 0;
3596 i++;
3597 }
3598 return ret;
3599}
Chris Masond1310b22008-01-24 16:13:08 -05003600
3601void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3602 unsigned long start, unsigned long len)
3603{
3604 size_t cur;
3605 size_t offset;
3606 struct page *page;
3607 char *kaddr;
3608 char *src = (char *)srcv;
3609 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3610 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3611
3612 WARN_ON(start > eb->len);
3613 WARN_ON(start + len > eb->start + eb->len);
3614
3615 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3616
Chris Masond3977122009-01-05 21:25:51 -05003617 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003618 page = extent_buffer_page(eb, i);
3619 WARN_ON(!PageUptodate(page));
3620
3621 cur = min(len, PAGE_CACHE_SIZE - offset);
3622 kaddr = kmap_atomic(page, KM_USER1);
3623 memcpy(kaddr + offset, src, cur);
3624 kunmap_atomic(kaddr, KM_USER1);
3625
3626 src += cur;
3627 len -= cur;
3628 offset = 0;
3629 i++;
3630 }
3631}
Chris Masond1310b22008-01-24 16:13:08 -05003632
3633void memset_extent_buffer(struct extent_buffer *eb, char c,
3634 unsigned long start, unsigned long len)
3635{
3636 size_t cur;
3637 size_t offset;
3638 struct page *page;
3639 char *kaddr;
3640 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3641 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3642
3643 WARN_ON(start > eb->len);
3644 WARN_ON(start + len > eb->start + eb->len);
3645
3646 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3647
Chris Masond3977122009-01-05 21:25:51 -05003648 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003649 page = extent_buffer_page(eb, i);
3650 WARN_ON(!PageUptodate(page));
3651
3652 cur = min(len, PAGE_CACHE_SIZE - offset);
3653 kaddr = kmap_atomic(page, KM_USER0);
3654 memset(kaddr + offset, c, cur);
3655 kunmap_atomic(kaddr, KM_USER0);
3656
3657 len -= cur;
3658 offset = 0;
3659 i++;
3660 }
3661}
Chris Masond1310b22008-01-24 16:13:08 -05003662
3663void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3664 unsigned long dst_offset, unsigned long src_offset,
3665 unsigned long len)
3666{
3667 u64 dst_len = dst->len;
3668 size_t cur;
3669 size_t offset;
3670 struct page *page;
3671 char *kaddr;
3672 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3673 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3674
3675 WARN_ON(src->len != dst_len);
3676
3677 offset = (start_offset + dst_offset) &
3678 ((unsigned long)PAGE_CACHE_SIZE - 1);
3679
Chris Masond3977122009-01-05 21:25:51 -05003680 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003681 page = extent_buffer_page(dst, i);
3682 WARN_ON(!PageUptodate(page));
3683
3684 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3685
3686 kaddr = kmap_atomic(page, KM_USER0);
3687 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3688 kunmap_atomic(kaddr, KM_USER0);
3689
3690 src_offset += cur;
3691 len -= cur;
3692 offset = 0;
3693 i++;
3694 }
3695}
Chris Masond1310b22008-01-24 16:13:08 -05003696
3697static void move_pages(struct page *dst_page, struct page *src_page,
3698 unsigned long dst_off, unsigned long src_off,
3699 unsigned long len)
3700{
3701 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3702 if (dst_page == src_page) {
3703 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3704 } else {
3705 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3706 char *p = dst_kaddr + dst_off + len;
3707 char *s = src_kaddr + src_off + len;
3708
3709 while (len--)
3710 *--p = *--s;
3711
3712 kunmap_atomic(src_kaddr, KM_USER1);
3713 }
3714 kunmap_atomic(dst_kaddr, KM_USER0);
3715}
3716
Sergei Trofimovich33872062011-04-11 21:52:52 +00003717static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3718{
3719 unsigned long distance = (src > dst) ? src - dst : dst - src;
3720 return distance < len;
3721}
3722
Chris Masond1310b22008-01-24 16:13:08 -05003723static void copy_pages(struct page *dst_page, struct page *src_page,
3724 unsigned long dst_off, unsigned long src_off,
3725 unsigned long len)
3726{
3727 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3728 char *src_kaddr;
3729
Sergei Trofimovich33872062011-04-11 21:52:52 +00003730 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003731 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003732 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003733 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003734 BUG_ON(areas_overlap(src_off, dst_off, len));
3735 }
Chris Masond1310b22008-01-24 16:13:08 -05003736
3737 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3738 kunmap_atomic(dst_kaddr, KM_USER0);
3739 if (dst_page != src_page)
3740 kunmap_atomic(src_kaddr, KM_USER1);
3741}
3742
3743void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3744 unsigned long src_offset, unsigned long len)
3745{
3746 size_t cur;
3747 size_t dst_off_in_page;
3748 size_t src_off_in_page;
3749 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3750 unsigned long dst_i;
3751 unsigned long src_i;
3752
3753 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003754 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3755 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003756 BUG_ON(1);
3757 }
3758 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003759 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3760 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003761 BUG_ON(1);
3762 }
3763
Chris Masond3977122009-01-05 21:25:51 -05003764 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003765 dst_off_in_page = (start_offset + dst_offset) &
3766 ((unsigned long)PAGE_CACHE_SIZE - 1);
3767 src_off_in_page = (start_offset + src_offset) &
3768 ((unsigned long)PAGE_CACHE_SIZE - 1);
3769
3770 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3771 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3772
3773 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3774 src_off_in_page));
3775 cur = min_t(unsigned long, cur,
3776 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3777
3778 copy_pages(extent_buffer_page(dst, dst_i),
3779 extent_buffer_page(dst, src_i),
3780 dst_off_in_page, src_off_in_page, cur);
3781
3782 src_offset += cur;
3783 dst_offset += cur;
3784 len -= cur;
3785 }
3786}
Chris Masond1310b22008-01-24 16:13:08 -05003787
3788void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3789 unsigned long src_offset, unsigned long len)
3790{
3791 size_t cur;
3792 size_t dst_off_in_page;
3793 size_t src_off_in_page;
3794 unsigned long dst_end = dst_offset + len - 1;
3795 unsigned long src_end = src_offset + len - 1;
3796 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3797 unsigned long dst_i;
3798 unsigned long src_i;
3799
3800 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003801 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3802 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003803 BUG_ON(1);
3804 }
3805 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003806 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3807 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003808 BUG_ON(1);
3809 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00003810 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05003811 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3812 return;
3813 }
Chris Masond3977122009-01-05 21:25:51 -05003814 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003815 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3816 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3817
3818 dst_off_in_page = (start_offset + dst_end) &
3819 ((unsigned long)PAGE_CACHE_SIZE - 1);
3820 src_off_in_page = (start_offset + src_end) &
3821 ((unsigned long)PAGE_CACHE_SIZE - 1);
3822
3823 cur = min_t(unsigned long, len, src_off_in_page + 1);
3824 cur = min(cur, dst_off_in_page + 1);
3825 move_pages(extent_buffer_page(dst, dst_i),
3826 extent_buffer_page(dst, src_i),
3827 dst_off_in_page - cur + 1,
3828 src_off_in_page - cur + 1, cur);
3829
3830 dst_end -= cur;
3831 src_end -= cur;
3832 len -= cur;
3833 }
3834}
Chris Mason6af118c2008-07-22 11:18:07 -04003835
Miao Xie19fe0a82010-10-26 20:57:29 -04003836static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3837{
3838 struct extent_buffer *eb =
3839 container_of(head, struct extent_buffer, rcu_head);
3840
3841 btrfs_release_extent_buffer(eb);
3842}
3843
Chris Mason6af118c2008-07-22 11:18:07 -04003844int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3845{
3846 u64 start = page_offset(page);
3847 struct extent_buffer *eb;
3848 int ret = 1;
Chris Mason6af118c2008-07-22 11:18:07 -04003849
3850 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003851 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05003852 if (!eb) {
3853 spin_unlock(&tree->buffer_lock);
3854 return ret;
3855 }
Chris Mason6af118c2008-07-22 11:18:07 -04003856
Chris Masonb9473432009-03-13 11:00:37 -04003857 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3858 ret = 0;
3859 goto out;
3860 }
Miao Xie897ca6e2010-10-26 20:57:29 -04003861
Miao Xie19fe0a82010-10-26 20:57:29 -04003862 /*
3863 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3864 * Or go back.
3865 */
3866 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
3867 ret = 0;
3868 goto out;
3869 }
3870
3871 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003872out:
3873 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003874
3875 /* at this point we can safely release the extent buffer */
3876 if (atomic_read(&eb->refs) == 0)
3877 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118c2008-07-22 11:18:07 -04003878 return ret;
3879}