blob: ba41da59e31b1d93348622d7b47655234f5ff930 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
13#include "extent_io.h"
14#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040015#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050018
Chris Masond1310b22008-01-24 16:13:08 -050019static struct kmem_cache *extent_state_cache;
20static struct kmem_cache *extent_buffer_cache;
21
22static LIST_HEAD(buffers);
23static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040024
Chris Masonb47eda82008-11-10 12:34:40 -050025#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050026#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050027static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040028#endif
Chris Masond1310b22008-01-24 16:13:08 -050029
Chris Masond1310b22008-01-24 16:13:08 -050030#define BUFFER_LRU_MAX 64
31
32struct tree_entry {
33 u64 start;
34 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050035 struct rb_node rb_node;
36};
37
38struct extent_page_data {
39 struct bio *bio;
40 struct extent_io_tree *tree;
41 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050042
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
45 */
Chris Masonffbd5172009-04-20 15:50:09 -040046 unsigned int extent_locked:1;
47
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050050};
51
52int __init extent_io_init(void)
53{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020054 extent_state_cache = kmem_cache_create("extent_state",
55 sizeof(struct extent_state), 0,
56 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050057 if (!extent_state_cache)
58 return -ENOMEM;
59
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020060 extent_buffer_cache = kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer), 0,
62 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050063 if (!extent_buffer_cache)
64 goto free_state_cache;
65 return 0;
66
67free_state_cache:
68 kmem_cache_destroy(extent_state_cache);
69 return -ENOMEM;
70}
71
72void extent_io_exit(void)
73{
74 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040075 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050076
77 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040078 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050079 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state->start,
82 (unsigned long long)state->end,
83 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040084 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050085 kmem_cache_free(extent_state_cache, state);
86
87 }
88
Chris Mason2d2ae542008-03-26 16:24:23 -040089 while (!list_empty(&buffers)) {
90 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050091 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb->start,
93 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040094 list_del(&eb->leak_list);
95 kmem_cache_free(extent_buffer_cache, eb);
96 }
Chris Masond1310b22008-01-24 16:13:08 -050097 if (extent_state_cache)
98 kmem_cache_destroy(extent_state_cache);
99 if (extent_buffer_cache)
100 kmem_cache_destroy(extent_buffer_cache);
101}
102
103void extent_io_tree_init(struct extent_io_tree *tree,
104 struct address_space *mapping, gfp_t mask)
105{
Eric Paris6bef4d32010-02-23 19:43:04 +0000106 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400107 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500108 tree->ops = NULL;
109 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500110 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400111 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500113}
Chris Masond1310b22008-01-24 16:13:08 -0500114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
117 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500118#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
Chris Masond1310b22008-01-24 16:13:08 -0500137
Chris Mason4845e442010-05-25 20:56:50 -0400138void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500143#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400144 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Mason70dec802008-01-29 09:59:12 -0500146 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 kmem_cache_free(extent_state_cache, state);
153 }
154}
Chris Masond1310b22008-01-24 16:13:08 -0500155
156static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
158{
Chris Masond3977122009-01-05 21:25:51 -0500159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500161 struct tree_entry *entry;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500164 parent = *p;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
166
167 if (offset < entry->start)
168 p = &(*p)->rb_left;
169 else if (offset > entry->end)
170 p = &(*p)->rb_right;
171 else
172 return parent;
173 }
174
175 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
178 return NULL;
179}
180
Chris Mason80ea96b2008-02-01 14:51:59 -0500181static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
184{
Chris Mason80ea96b2008-02-01 14:51:59 -0500185 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500186 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
191
Chris Masond3977122009-01-05 21:25:51 -0500192 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500193 entry = rb_entry(n, struct tree_entry, rb_node);
194 prev = n;
195 prev_entry = entry;
196
197 if (offset < entry->start)
198 n = n->rb_left;
199 else if (offset > entry->end)
200 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500201 else
Chris Masond1310b22008-01-24 16:13:08 -0500202 return n;
203 }
204
205 if (prev_ret) {
206 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500207 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
210 }
211 *prev_ret = prev;
212 prev = orig_prev;
213 }
214
215 if (next_ret) {
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *next_ret = prev;
222 }
223 return NULL;
224}
225
Chris Mason80ea96b2008-02-01 14:51:59 -0500226static inline struct rb_node *tree_search(struct extent_io_tree *tree,
227 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500228{
Chris Mason70dec802008-01-29 09:59:12 -0500229 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500230 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500231
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500233 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500234 return prev;
235 return ret;
236}
237
Josef Bacik9ed74f22009-09-11 16:12:44 -0400238static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
239 struct extent_state *other)
240{
241 if (tree->ops && tree->ops->merge_extent_hook)
242 tree->ops->merge_extent_hook(tree->mapping->host, new,
243 other);
244}
245
Chris Masond1310b22008-01-24 16:13:08 -0500246/*
247 * utility function to look for merge candidates inside a given range.
248 * Any extents with matching state are merged together into a single
249 * extent in the tree. Extents with EXTENT_IO in their state field
250 * are not merged because the end_io handlers need to be able to do
251 * operations on them without sleeping (or doing allocations/splits).
252 *
253 * This should be called with the tree lock held.
254 */
255static int merge_state(struct extent_io_tree *tree,
256 struct extent_state *state)
257{
258 struct extent_state *other;
259 struct rb_node *other_node;
260
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400261 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500262 return 0;
263
264 other_node = rb_prev(&state->rb_node);
265 if (other_node) {
266 other = rb_entry(other_node, struct extent_state, rb_node);
267 if (other->end == state->start - 1 &&
268 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400269 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500270 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500271 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500272 rb_erase(&other->rb_node, &tree->state);
273 free_extent_state(other);
274 }
275 }
276 other_node = rb_next(&state->rb_node);
277 if (other_node) {
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->start == state->end + 1 &&
280 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400281 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500282 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500283 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500284 rb_erase(&state->rb_node, &tree->state);
285 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400286 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500287 }
288 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289
Chris Masond1310b22008-01-24 16:13:08 -0500290 return 0;
291}
292
Josef Bacik9ed74f22009-09-11 16:12:44 -0400293static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400294 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500295{
296 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400297 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400298 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500299 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400300
301 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500302}
303
304static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
Chris Masond1310b22008-01-24 16:13:08 -0500311/*
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
314 *
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
317 *
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
320 */
321static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400323 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500324{
325 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400326 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400327 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500328
329 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500330 printk(KERN_ERR "btrfs end < start %llu %llu\n",
331 (unsigned long long)end,
332 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500333 WARN_ON(1);
334 }
Chris Masond1310b22008-01-24 16:13:08 -0500335 state->start = start;
336 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400337 ret = set_state_cb(tree, state, bits);
338 if (ret)
339 return ret;
340
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400341 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400342 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400343 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500344 node = tree_insert(&tree->state, end, &state->rb_node);
345 if (node) {
346 struct extent_state *found;
347 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500348 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found->start,
350 (unsigned long long)found->end,
351 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500352 free_extent_state(state);
353 return -EEXIST;
354 }
Chris Mason70dec802008-01-29 09:59:12 -0500355 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500356 merge_state(tree, state);
357 return 0;
358}
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
361 u64 split)
362{
363 if (tree->ops && tree->ops->split_extent_hook)
364 return tree->ops->split_extent_hook(tree->mapping->host,
365 orig, split);
366 return 0;
367}
368
Chris Masond1310b22008-01-24 16:13:08 -0500369/*
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
373 *
374 * Before calling,
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
379 *
380 * The tree locks are not taken by this function. They need to be held
381 * by the caller.
382 */
383static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
385{
386 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387
388 split_cb(tree, orig, split);
389
Chris Masond1310b22008-01-24 16:13:08 -0500390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
393 orig->start = split;
394
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
396 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500397 free_extent_state(prealloc);
398 return -EEXIST;
399 }
Chris Mason70dec802008-01-29 09:59:12 -0500400 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500401 return 0;
402}
403
404/*
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
408 *
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
411 */
412static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 struct extent_state *state,
414 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500415{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400416 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400417 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500418
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400419 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500420 u64 range = state->end - state->start + 1;
421 WARN_ON(range > tree->dirty_bytes);
422 tree->dirty_bytes -= range;
423 }
Chris Mason291d6732008-01-29 15:55:23 -0500424 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400425 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500426 if (wake)
427 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400428 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500429 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500430 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500431 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500432 free_extent_state(state);
433 } else {
434 WARN_ON(1);
435 }
436 } else {
437 merge_state(tree, state);
438 }
439 return ret;
440}
441
442/*
443 * clear some bits on a range in the tree. This may require splitting
444 * or inserting elements in the tree, so the gfp mask is used to
445 * indicate which allocations or sleeping are allowed.
446 *
447 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
448 * the given range from the tree regardless of state (ie for truncate).
449 *
450 * the range [start, end] is inclusive.
451 *
452 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
453 * bits were already set, or zero if none of the bits were already set.
454 */
455int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400456 int bits, int wake, int delete,
457 struct extent_state **cached_state,
458 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500459{
460 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400461 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500462 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400463 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500464 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400465 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500466 int err;
467 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000468 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500469
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400470 if (delete)
471 bits |= ~EXTENT_CTLBITS;
472 bits |= EXTENT_FIRST_DELALLOC;
473
Josef Bacik2ac55d42010-02-03 19:33:23 +0000474 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
475 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500476again:
477 if (!prealloc && (mask & __GFP_WAIT)) {
478 prealloc = alloc_extent_state(mask);
479 if (!prealloc)
480 return -ENOMEM;
481 }
482
Chris Masoncad321a2008-12-17 14:51:42 -0500483 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400484 if (cached_state) {
485 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000486
487 if (clear) {
488 *cached_state = NULL;
489 cached_state = NULL;
490 }
491
Chris Mason42daec22009-09-23 19:51:09 -0400492 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000493 if (clear)
494 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400495 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400496 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400497 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000498 if (clear)
499 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400500 }
Chris Masond1310b22008-01-24 16:13:08 -0500501 /*
502 * this search will find the extents that end after
503 * our range starts
504 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500505 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500506 if (!node)
507 goto out;
508 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400509hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500510 if (state->start > end)
511 goto out;
512 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400513 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500514
515 /*
516 * | ---- desired range ---- |
517 * | state | or
518 * | ------------- state -------------- |
519 *
520 * We need to split the extent we found, and may flip
521 * bits on second half.
522 *
523 * If the extent we found extends past our range, we
524 * just split and search again. It'll get split again
525 * the next time though.
526 *
527 * If the extent we found is inside our range, we clear
528 * the desired bit on it.
529 */
530
531 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500532 if (!prealloc)
533 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500534 err = split_state(tree, state, prealloc, start);
535 BUG_ON(err == -EEXIST);
536 prealloc = NULL;
537 if (err)
538 goto out;
539 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400540 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400541 if (last_end == (u64)-1)
542 goto out;
543 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500544 }
545 goto search_again;
546 }
547 /*
548 * | ---- desired range ---- |
549 * | state |
550 * We need to split the extent, and clear the bit
551 * on the first half
552 */
553 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500554 if (!prealloc)
555 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500556 err = split_state(tree, state, prealloc, end + 1);
557 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500558 if (wake)
559 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400560
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400561 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400562
Chris Masond1310b22008-01-24 16:13:08 -0500563 prealloc = NULL;
564 goto out;
565 }
Chris Mason42daec22009-09-23 19:51:09 -0400566
Chris Mason2c64c532009-09-02 15:04:12 -0400567 if (state->end < end && prealloc && !need_resched())
568 next_node = rb_next(&state->rb_node);
569 else
570 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400571
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400572 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400573 if (last_end == (u64)-1)
574 goto out;
575 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400576 if (start <= end && next_node) {
577 state = rb_entry(next_node, struct extent_state,
578 rb_node);
579 if (state->start == start)
580 goto hit_next;
581 }
Chris Masond1310b22008-01-24 16:13:08 -0500582 goto search_again;
583
584out:
Chris Masoncad321a2008-12-17 14:51:42 -0500585 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500586 if (prealloc)
587 free_extent_state(prealloc);
588
589 return set;
590
591search_again:
592 if (start > end)
593 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500594 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500595 if (mask & __GFP_WAIT)
596 cond_resched();
597 goto again;
598}
Chris Masond1310b22008-01-24 16:13:08 -0500599
600static int wait_on_state(struct extent_io_tree *tree,
601 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500602 __releases(tree->lock)
603 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500604{
605 DEFINE_WAIT(wait);
606 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500607 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500608 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500609 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500610 finish_wait(&state->wq, &wait);
611 return 0;
612}
613
614/*
615 * waits for one or more bits to clear on a range in the state tree.
616 * The range [start, end] is inclusive.
617 * The tree lock is taken by this function
618 */
619int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
620{
621 struct extent_state *state;
622 struct rb_node *node;
623
Chris Masoncad321a2008-12-17 14:51:42 -0500624 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500625again:
626 while (1) {
627 /*
628 * this search will find all the extents that end after
629 * our range starts
630 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500631 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500632 if (!node)
633 break;
634
635 state = rb_entry(node, struct extent_state, rb_node);
636
637 if (state->start > end)
638 goto out;
639
640 if (state->state & bits) {
641 start = state->start;
642 atomic_inc(&state->refs);
643 wait_on_state(tree, state);
644 free_extent_state(state);
645 goto again;
646 }
647 start = state->end + 1;
648
649 if (start > end)
650 break;
651
652 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500653 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500655 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500656 }
657 }
658out:
Chris Masoncad321a2008-12-17 14:51:42 -0500659 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500660 return 0;
661}
Chris Masond1310b22008-01-24 16:13:08 -0500662
Josef Bacik9ed74f22009-09-11 16:12:44 -0400663static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500664 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400665 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500666{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400667 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400668 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400669
670 ret = set_state_cb(tree, state, bits);
671 if (ret)
672 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400673 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500674 u64 range = state->end - state->start + 1;
675 tree->dirty_bytes += range;
676 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400677 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400678
679 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500680}
681
Chris Mason2c64c532009-09-02 15:04:12 -0400682static void cache_state(struct extent_state *state,
683 struct extent_state **cached_ptr)
684{
685 if (cached_ptr && !(*cached_ptr)) {
686 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
687 *cached_ptr = state;
688 atomic_inc(&state->refs);
689 }
690 }
691}
692
Arne Jansen507903b2011-04-06 10:02:20 +0000693static void uncache_state(struct extent_state **cached_ptr)
694{
695 if (cached_ptr && (*cached_ptr)) {
696 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400697 *cached_ptr = NULL;
698 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000699 }
700}
701
Chris Masond1310b22008-01-24 16:13:08 -0500702/*
Chris Mason1edbb732009-09-02 13:24:36 -0400703 * set some bits on a range in the tree. This may require allocations or
704 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500705 *
Chris Mason1edbb732009-09-02 13:24:36 -0400706 * If any of the exclusive bits are set, this will fail with -EEXIST if some
707 * part of the range already has the desired bits set. The start of the
708 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500709 *
Chris Mason1edbb732009-09-02 13:24:36 -0400710 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500711 */
Chris Mason1edbb732009-09-02 13:24:36 -0400712
Chris Mason4845e442010-05-25 20:56:50 -0400713int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
714 int bits, int exclusive_bits, u64 *failed_start,
715 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500716{
717 struct extent_state *state;
718 struct extent_state *prealloc = NULL;
719 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500720 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500721 u64 last_start;
722 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400723
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400724 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500725again:
726 if (!prealloc && (mask & __GFP_WAIT)) {
727 prealloc = alloc_extent_state(mask);
728 if (!prealloc)
729 return -ENOMEM;
730 }
731
Chris Masoncad321a2008-12-17 14:51:42 -0500732 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400733 if (cached_state && *cached_state) {
734 state = *cached_state;
735 if (state->start == start && state->tree) {
736 node = &state->rb_node;
737 goto hit_next;
738 }
739 }
Chris Masond1310b22008-01-24 16:13:08 -0500740 /*
741 * this search will find all the extents that end after
742 * our range starts.
743 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500744 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500745 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400746 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500747 prealloc = NULL;
748 BUG_ON(err == -EEXIST);
749 goto out;
750 }
Chris Masond1310b22008-01-24 16:13:08 -0500751 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400752hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500753 last_start = state->start;
754 last_end = state->end;
755
756 /*
757 * | ---- desired range ---- |
758 * | state |
759 *
760 * Just lock what we found and keep going
761 */
762 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400763 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400764 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500765 *failed_start = state->start;
766 err = -EEXIST;
767 goto out;
768 }
Chris Mason42daec22009-09-23 19:51:09 -0400769
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400770 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400771 if (err)
772 goto out;
773
Chris Mason2c64c532009-09-02 15:04:12 -0400774 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500775 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400776 if (last_end == (u64)-1)
777 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400778
Yan Zheng5c939df2009-05-27 09:16:03 -0400779 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400780 if (start < end && prealloc && !need_resched()) {
781 next_node = rb_next(node);
782 if (next_node) {
783 state = rb_entry(next_node, struct extent_state,
784 rb_node);
785 if (state->start == start)
786 goto hit_next;
787 }
788 }
Chris Masond1310b22008-01-24 16:13:08 -0500789 goto search_again;
790 }
791
792 /*
793 * | ---- desired range ---- |
794 * | state |
795 * or
796 * | ------------- state -------------- |
797 *
798 * We need to split the extent we found, and may flip bits on
799 * second half.
800 *
801 * If the extent we found extends past our
802 * range, we just split and search again. It'll get split
803 * again the next time though.
804 *
805 * If the extent we found is inside our range, we set the
806 * desired bit on it.
807 */
808 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400809 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500810 *failed_start = start;
811 err = -EEXIST;
812 goto out;
813 }
814 err = split_state(tree, state, prealloc, start);
815 BUG_ON(err == -EEXIST);
816 prealloc = NULL;
817 if (err)
818 goto out;
819 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400820 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400821 if (err)
822 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400823 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500824 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400825 if (last_end == (u64)-1)
826 goto out;
827 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500828 }
829 goto search_again;
830 }
831 /*
832 * | ---- desired range ---- |
833 * | state | or | state |
834 *
835 * There's a hole, we need to insert something in it and
836 * ignore the extent we found.
837 */
838 if (state->start > start) {
839 u64 this_end;
840 if (end < last_start)
841 this_end = end;
842 else
Chris Masond3977122009-01-05 21:25:51 -0500843 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500844 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400845 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400846 BUG_ON(err == -EEXIST);
847 if (err) {
848 prealloc = NULL;
849 goto out;
850 }
Chris Mason2c64c532009-09-02 15:04:12 -0400851 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500852 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500853 start = this_end + 1;
854 goto search_again;
855 }
856 /*
857 * | ---- desired range ---- |
858 * | state |
859 * We need to split the extent, and set the bit
860 * on the first half
861 */
862 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400863 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500864 *failed_start = start;
865 err = -EEXIST;
866 goto out;
867 }
868 err = split_state(tree, state, prealloc, end + 1);
869 BUG_ON(err == -EEXIST);
870
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400871 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400872 if (err) {
873 prealloc = NULL;
874 goto out;
875 }
Chris Mason2c64c532009-09-02 15:04:12 -0400876 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500877 merge_state(tree, prealloc);
878 prealloc = NULL;
879 goto out;
880 }
881
882 goto search_again;
883
884out:
Chris Masoncad321a2008-12-17 14:51:42 -0500885 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500886 if (prealloc)
887 free_extent_state(prealloc);
888
889 return err;
890
891search_again:
892 if (start > end)
893 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500894 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500895 if (mask & __GFP_WAIT)
896 cond_resched();
897 goto again;
898}
Chris Masond1310b22008-01-24 16:13:08 -0500899
900/* wrappers around set/clear extent bit */
901int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
902 gfp_t mask)
903{
904 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400905 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500906}
Chris Masond1310b22008-01-24 16:13:08 -0500907
908int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
909 int bits, gfp_t mask)
910{
911 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400912 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500913}
Chris Masond1310b22008-01-24 16:13:08 -0500914
915int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
916 int bits, gfp_t mask)
917{
Chris Mason2c64c532009-09-02 15:04:12 -0400918 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500919}
Chris Masond1310b22008-01-24 16:13:08 -0500920
921int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000922 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500923{
924 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400925 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000926 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500927}
Chris Masond1310b22008-01-24 16:13:08 -0500928
929int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
930 gfp_t mask)
931{
932 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400933 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400934 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500935}
Chris Masond1310b22008-01-24 16:13:08 -0500936
937int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
938 gfp_t mask)
939{
940 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400941 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500942}
Chris Masond1310b22008-01-24 16:13:08 -0500943
Christoph Hellwigb2950862008-12-02 09:54:17 -0500944static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500945 gfp_t mask)
946{
Chris Mason2c64c532009-09-02 15:04:12 -0400947 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
948 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500949}
Chris Masond1310b22008-01-24 16:13:08 -0500950
951int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000952 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500953{
Arne Jansen507903b2011-04-06 10:02:20 +0000954 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
955 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500956}
Chris Masond1310b22008-01-24 16:13:08 -0500957
Chris Masond3977122009-01-05 21:25:51 -0500958static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000959 u64 end, struct extent_state **cached_state,
960 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500961{
Chris Mason2c64c532009-09-02 15:04:12 -0400962 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000963 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500964}
Chris Masond1310b22008-01-24 16:13:08 -0500965
Chris Masond1310b22008-01-24 16:13:08 -0500966int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
967{
968 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
969}
Chris Masond1310b22008-01-24 16:13:08 -0500970
Chris Masond352ac62008-09-29 15:18:18 -0400971/*
972 * either insert or lock state struct between start and end use mask to tell
973 * us if waiting is desired.
974 */
Chris Mason1edbb732009-09-02 13:24:36 -0400975int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400976 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500977{
978 int err;
979 u64 failed_start;
980 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400981 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400982 EXTENT_LOCKED, &failed_start,
983 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500984 if (err == -EEXIST && (mask & __GFP_WAIT)) {
985 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
986 start = failed_start;
987 } else {
988 break;
989 }
990 WARN_ON(start > end);
991 }
992 return err;
993}
Chris Masond1310b22008-01-24 16:13:08 -0500994
Chris Mason1edbb732009-09-02 13:24:36 -0400995int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
996{
Chris Mason2c64c532009-09-02 15:04:12 -0400997 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400998}
999
Josef Bacik25179202008-10-29 14:49:05 -04001000int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1001 gfp_t mask)
1002{
1003 int err;
1004 u64 failed_start;
1005
Chris Mason2c64c532009-09-02 15:04:12 -04001006 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1007 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001008 if (err == -EEXIST) {
1009 if (failed_start > start)
1010 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001011 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001012 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001013 }
Josef Bacik25179202008-10-29 14:49:05 -04001014 return 1;
1015}
Josef Bacik25179202008-10-29 14:49:05 -04001016
Chris Mason2c64c532009-09-02 15:04:12 -04001017int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1018 struct extent_state **cached, gfp_t mask)
1019{
1020 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1021 mask);
1022}
1023
Arne Jansen507903b2011-04-06 10:02:20 +00001024int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001025{
Chris Mason2c64c532009-09-02 15:04:12 -04001026 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1027 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001028}
Chris Masond1310b22008-01-24 16:13:08 -05001029
1030/*
1031 * helper function to set pages and extents in the tree dirty
1032 */
1033int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1034{
1035 unsigned long index = start >> PAGE_CACHE_SHIFT;
1036 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1037 struct page *page;
1038
1039 while (index <= end_index) {
1040 page = find_get_page(tree->mapping, index);
1041 BUG_ON(!page);
1042 __set_page_dirty_nobuffers(page);
1043 page_cache_release(page);
1044 index++;
1045 }
Chris Masond1310b22008-01-24 16:13:08 -05001046 return 0;
1047}
Chris Masond1310b22008-01-24 16:13:08 -05001048
1049/*
1050 * helper function to set both pages and extents in the tree writeback
1051 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001052static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001053{
1054 unsigned long index = start >> PAGE_CACHE_SHIFT;
1055 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1056 struct page *page;
1057
1058 while (index <= end_index) {
1059 page = find_get_page(tree->mapping, index);
1060 BUG_ON(!page);
1061 set_page_writeback(page);
1062 page_cache_release(page);
1063 index++;
1064 }
Chris Masond1310b22008-01-24 16:13:08 -05001065 return 0;
1066}
Chris Masond1310b22008-01-24 16:13:08 -05001067
Chris Masond352ac62008-09-29 15:18:18 -04001068/*
1069 * find the first offset in the io tree with 'bits' set. zero is
1070 * returned if we find something, and *start_ret and *end_ret are
1071 * set to reflect the state struct that was found.
1072 *
1073 * If nothing was found, 1 is returned, < 0 on error
1074 */
Chris Masond1310b22008-01-24 16:13:08 -05001075int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1076 u64 *start_ret, u64 *end_ret, int bits)
1077{
1078 struct rb_node *node;
1079 struct extent_state *state;
1080 int ret = 1;
1081
Chris Masoncad321a2008-12-17 14:51:42 -05001082 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001083 /*
1084 * this search will find all the extents that end after
1085 * our range starts.
1086 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001087 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001088 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001089 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001090
Chris Masond3977122009-01-05 21:25:51 -05001091 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001092 state = rb_entry(node, struct extent_state, rb_node);
1093 if (state->end >= start && (state->state & bits)) {
1094 *start_ret = state->start;
1095 *end_ret = state->end;
1096 ret = 0;
1097 break;
1098 }
1099 node = rb_next(node);
1100 if (!node)
1101 break;
1102 }
1103out:
Chris Masoncad321a2008-12-17 14:51:42 -05001104 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001105 return ret;
1106}
Chris Masond1310b22008-01-24 16:13:08 -05001107
Chris Masond352ac62008-09-29 15:18:18 -04001108/* find the first state struct with 'bits' set after 'start', and
1109 * return it. tree->lock must be held. NULL will returned if
1110 * nothing was found after 'start'
1111 */
Chris Masond7fc6402008-02-18 12:12:38 -05001112struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1113 u64 start, int bits)
1114{
1115 struct rb_node *node;
1116 struct extent_state *state;
1117
1118 /*
1119 * this search will find all the extents that end after
1120 * our range starts.
1121 */
1122 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001123 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001124 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001125
Chris Masond3977122009-01-05 21:25:51 -05001126 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001127 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001128 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001129 return state;
Chris Masond3977122009-01-05 21:25:51 -05001130
Chris Masond7fc6402008-02-18 12:12:38 -05001131 node = rb_next(node);
1132 if (!node)
1133 break;
1134 }
1135out:
1136 return NULL;
1137}
Chris Masond7fc6402008-02-18 12:12:38 -05001138
Chris Masond352ac62008-09-29 15:18:18 -04001139/*
1140 * find a contiguous range of bytes in the file marked as delalloc, not
1141 * more than 'max_bytes'. start and end are used to return the range,
1142 *
1143 * 1 is returned if we find something, 0 if nothing was in the tree
1144 */
Chris Masonc8b97812008-10-29 14:49:59 -04001145static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001146 u64 *start, u64 *end, u64 max_bytes,
1147 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001148{
1149 struct rb_node *node;
1150 struct extent_state *state;
1151 u64 cur_start = *start;
1152 u64 found = 0;
1153 u64 total_bytes = 0;
1154
Chris Masoncad321a2008-12-17 14:51:42 -05001155 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001156
Chris Masond1310b22008-01-24 16:13:08 -05001157 /*
1158 * this search will find all the extents that end after
1159 * our range starts.
1160 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001161 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001162 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001163 if (!found)
1164 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001165 goto out;
1166 }
1167
Chris Masond3977122009-01-05 21:25:51 -05001168 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001169 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001170 if (found && (state->start != cur_start ||
1171 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001172 goto out;
1173 }
1174 if (!(state->state & EXTENT_DELALLOC)) {
1175 if (!found)
1176 *end = state->end;
1177 goto out;
1178 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001179 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001180 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001181 *cached_state = state;
1182 atomic_inc(&state->refs);
1183 }
Chris Masond1310b22008-01-24 16:13:08 -05001184 found++;
1185 *end = state->end;
1186 cur_start = state->end + 1;
1187 node = rb_next(node);
1188 if (!node)
1189 break;
1190 total_bytes += state->end - state->start + 1;
1191 if (total_bytes >= max_bytes)
1192 break;
1193 }
1194out:
Chris Masoncad321a2008-12-17 14:51:42 -05001195 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001196 return found;
1197}
1198
Chris Masonc8b97812008-10-29 14:49:59 -04001199static noinline int __unlock_for_delalloc(struct inode *inode,
1200 struct page *locked_page,
1201 u64 start, u64 end)
1202{
1203 int ret;
1204 struct page *pages[16];
1205 unsigned long index = start >> PAGE_CACHE_SHIFT;
1206 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1207 unsigned long nr_pages = end_index - index + 1;
1208 int i;
1209
1210 if (index == locked_page->index && end_index == index)
1211 return 0;
1212
Chris Masond3977122009-01-05 21:25:51 -05001213 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001214 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001215 min_t(unsigned long, nr_pages,
1216 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001217 for (i = 0; i < ret; i++) {
1218 if (pages[i] != locked_page)
1219 unlock_page(pages[i]);
1220 page_cache_release(pages[i]);
1221 }
1222 nr_pages -= ret;
1223 index += ret;
1224 cond_resched();
1225 }
1226 return 0;
1227}
1228
1229static noinline int lock_delalloc_pages(struct inode *inode,
1230 struct page *locked_page,
1231 u64 delalloc_start,
1232 u64 delalloc_end)
1233{
1234 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1235 unsigned long start_index = index;
1236 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1237 unsigned long pages_locked = 0;
1238 struct page *pages[16];
1239 unsigned long nrpages;
1240 int ret;
1241 int i;
1242
1243 /* the caller is responsible for locking the start index */
1244 if (index == locked_page->index && index == end_index)
1245 return 0;
1246
1247 /* skip the page at the start index */
1248 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001249 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001250 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001251 min_t(unsigned long,
1252 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001253 if (ret == 0) {
1254 ret = -EAGAIN;
1255 goto done;
1256 }
1257 /* now we have an array of pages, lock them all */
1258 for (i = 0; i < ret; i++) {
1259 /*
1260 * the caller is taking responsibility for
1261 * locked_page
1262 */
Chris Mason771ed682008-11-06 22:02:51 -05001263 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001264 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001265 if (!PageDirty(pages[i]) ||
1266 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001267 ret = -EAGAIN;
1268 unlock_page(pages[i]);
1269 page_cache_release(pages[i]);
1270 goto done;
1271 }
1272 }
Chris Masonc8b97812008-10-29 14:49:59 -04001273 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001274 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001275 }
Chris Masonc8b97812008-10-29 14:49:59 -04001276 nrpages -= ret;
1277 index += ret;
1278 cond_resched();
1279 }
1280 ret = 0;
1281done:
1282 if (ret && pages_locked) {
1283 __unlock_for_delalloc(inode, locked_page,
1284 delalloc_start,
1285 ((u64)(start_index + pages_locked - 1)) <<
1286 PAGE_CACHE_SHIFT);
1287 }
1288 return ret;
1289}
1290
1291/*
1292 * find a contiguous range of bytes in the file marked as delalloc, not
1293 * more than 'max_bytes'. start and end are used to return the range,
1294 *
1295 * 1 is returned if we find something, 0 if nothing was in the tree
1296 */
1297static noinline u64 find_lock_delalloc_range(struct inode *inode,
1298 struct extent_io_tree *tree,
1299 struct page *locked_page,
1300 u64 *start, u64 *end,
1301 u64 max_bytes)
1302{
1303 u64 delalloc_start;
1304 u64 delalloc_end;
1305 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001306 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001307 int ret;
1308 int loops = 0;
1309
1310again:
1311 /* step one, find a bunch of delalloc bytes starting at start */
1312 delalloc_start = *start;
1313 delalloc_end = 0;
1314 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001315 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001316 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001317 *start = delalloc_start;
1318 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001319 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001320 return found;
1321 }
1322
1323 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001324 * start comes from the offset of locked_page. We have to lock
1325 * pages in order, so we can't process delalloc bytes before
1326 * locked_page
1327 */
Chris Masond3977122009-01-05 21:25:51 -05001328 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001329 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001330
1331 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001332 * make sure to limit the number of pages we try to lock down
1333 * if we're looping.
1334 */
Chris Masond3977122009-01-05 21:25:51 -05001335 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001336 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001337
Chris Masonc8b97812008-10-29 14:49:59 -04001338 /* step two, lock all the pages after the page that has start */
1339 ret = lock_delalloc_pages(inode, locked_page,
1340 delalloc_start, delalloc_end);
1341 if (ret == -EAGAIN) {
1342 /* some of the pages are gone, lets avoid looping by
1343 * shortening the size of the delalloc range we're searching
1344 */
Chris Mason9655d292009-09-02 15:22:30 -04001345 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001346 if (!loops) {
1347 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1348 max_bytes = PAGE_CACHE_SIZE - offset;
1349 loops = 1;
1350 goto again;
1351 } else {
1352 found = 0;
1353 goto out_failed;
1354 }
1355 }
1356 BUG_ON(ret);
1357
1358 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001359 lock_extent_bits(tree, delalloc_start, delalloc_end,
1360 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001361
1362 /* then test to make sure it is all still delalloc */
1363 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001364 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001365 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001366 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1367 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001368 __unlock_for_delalloc(inode, locked_page,
1369 delalloc_start, delalloc_end);
1370 cond_resched();
1371 goto again;
1372 }
Chris Mason9655d292009-09-02 15:22:30 -04001373 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001374 *start = delalloc_start;
1375 *end = delalloc_end;
1376out_failed:
1377 return found;
1378}
1379
1380int extent_clear_unlock_delalloc(struct inode *inode,
1381 struct extent_io_tree *tree,
1382 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001383 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001384{
1385 int ret;
1386 struct page *pages[16];
1387 unsigned long index = start >> PAGE_CACHE_SHIFT;
1388 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1389 unsigned long nr_pages = end_index - index + 1;
1390 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001391 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001392
Chris Masona791e352009-10-08 11:27:10 -04001393 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001394 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001395 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001396 clear_bits |= EXTENT_DIRTY;
1397
Chris Masona791e352009-10-08 11:27:10 -04001398 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001399 clear_bits |= EXTENT_DELALLOC;
1400
Chris Mason2c64c532009-09-02 15:04:12 -04001401 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001402 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1403 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1404 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001405 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001406
Chris Masond3977122009-01-05 21:25:51 -05001407 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001408 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001409 min_t(unsigned long,
1410 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001411 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001412
Chris Masona791e352009-10-08 11:27:10 -04001413 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001414 SetPagePrivate2(pages[i]);
1415
Chris Masonc8b97812008-10-29 14:49:59 -04001416 if (pages[i] == locked_page) {
1417 page_cache_release(pages[i]);
1418 continue;
1419 }
Chris Masona791e352009-10-08 11:27:10 -04001420 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001421 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001422 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001423 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001424 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001425 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001426 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001427 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001428 page_cache_release(pages[i]);
1429 }
1430 nr_pages -= ret;
1431 index += ret;
1432 cond_resched();
1433 }
1434 return 0;
1435}
Chris Masonc8b97812008-10-29 14:49:59 -04001436
Chris Masond352ac62008-09-29 15:18:18 -04001437/*
1438 * count the number of bytes in the tree that have a given bit(s)
1439 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1440 * cached. The total number found is returned.
1441 */
Chris Masond1310b22008-01-24 16:13:08 -05001442u64 count_range_bits(struct extent_io_tree *tree,
1443 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001444 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001445{
1446 struct rb_node *node;
1447 struct extent_state *state;
1448 u64 cur_start = *start;
1449 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001450 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001451 int found = 0;
1452
1453 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001454 WARN_ON(1);
1455 return 0;
1456 }
1457
Chris Masoncad321a2008-12-17 14:51:42 -05001458 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001459 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1460 total_bytes = tree->dirty_bytes;
1461 goto out;
1462 }
1463 /*
1464 * this search will find all the extents that end after
1465 * our range starts.
1466 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001467 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001468 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001469 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001470
Chris Masond3977122009-01-05 21:25:51 -05001471 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001472 state = rb_entry(node, struct extent_state, rb_node);
1473 if (state->start > search_end)
1474 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001475 if (contig && found && state->start > last + 1)
1476 break;
1477 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001478 total_bytes += min(search_end, state->end) + 1 -
1479 max(cur_start, state->start);
1480 if (total_bytes >= max_bytes)
1481 break;
1482 if (!found) {
1483 *start = state->start;
1484 found = 1;
1485 }
Chris Masonec29ed52011-02-23 16:23:20 -05001486 last = state->end;
1487 } else if (contig && found) {
1488 break;
Chris Masond1310b22008-01-24 16:13:08 -05001489 }
1490 node = rb_next(node);
1491 if (!node)
1492 break;
1493 }
1494out:
Chris Masoncad321a2008-12-17 14:51:42 -05001495 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001496 return total_bytes;
1497}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001498
Chris Masond352ac62008-09-29 15:18:18 -04001499/*
1500 * set the private field for a given byte offset in the tree. If there isn't
1501 * an extent_state there already, this does nothing.
1502 */
Chris Masond1310b22008-01-24 16:13:08 -05001503int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1504{
1505 struct rb_node *node;
1506 struct extent_state *state;
1507 int ret = 0;
1508
Chris Masoncad321a2008-12-17 14:51:42 -05001509 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001510 /*
1511 * this search will find all the extents that end after
1512 * our range starts.
1513 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001514 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001515 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001516 ret = -ENOENT;
1517 goto out;
1518 }
1519 state = rb_entry(node, struct extent_state, rb_node);
1520 if (state->start != start) {
1521 ret = -ENOENT;
1522 goto out;
1523 }
1524 state->private = private;
1525out:
Chris Masoncad321a2008-12-17 14:51:42 -05001526 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001527 return ret;
1528}
1529
1530int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1531{
1532 struct rb_node *node;
1533 struct extent_state *state;
1534 int ret = 0;
1535
Chris Masoncad321a2008-12-17 14:51:42 -05001536 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001537 /*
1538 * this search will find all the extents that end after
1539 * our range starts.
1540 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001541 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001542 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001543 ret = -ENOENT;
1544 goto out;
1545 }
1546 state = rb_entry(node, struct extent_state, rb_node);
1547 if (state->start != start) {
1548 ret = -ENOENT;
1549 goto out;
1550 }
1551 *private = state->private;
1552out:
Chris Masoncad321a2008-12-17 14:51:42 -05001553 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001554 return ret;
1555}
1556
1557/*
1558 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001559 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001560 * has the bits set. Otherwise, 1 is returned if any bit in the
1561 * range is found set.
1562 */
1563int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001564 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001565{
1566 struct extent_state *state = NULL;
1567 struct rb_node *node;
1568 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001569
Chris Masoncad321a2008-12-17 14:51:42 -05001570 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001571 if (cached && cached->tree && cached->start == start)
1572 node = &cached->rb_node;
1573 else
1574 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001575 while (node && start <= end) {
1576 state = rb_entry(node, struct extent_state, rb_node);
1577
1578 if (filled && state->start > start) {
1579 bitset = 0;
1580 break;
1581 }
1582
1583 if (state->start > end)
1584 break;
1585
1586 if (state->state & bits) {
1587 bitset = 1;
1588 if (!filled)
1589 break;
1590 } else if (filled) {
1591 bitset = 0;
1592 break;
1593 }
Chris Mason46562cec2009-09-23 20:23:16 -04001594
1595 if (state->end == (u64)-1)
1596 break;
1597
Chris Masond1310b22008-01-24 16:13:08 -05001598 start = state->end + 1;
1599 if (start > end)
1600 break;
1601 node = rb_next(node);
1602 if (!node) {
1603 if (filled)
1604 bitset = 0;
1605 break;
1606 }
1607 }
Chris Masoncad321a2008-12-17 14:51:42 -05001608 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001609 return bitset;
1610}
Chris Masond1310b22008-01-24 16:13:08 -05001611
1612/*
1613 * helper function to set a given page up to date if all the
1614 * extents in the tree for that page are up to date
1615 */
1616static int check_page_uptodate(struct extent_io_tree *tree,
1617 struct page *page)
1618{
1619 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1620 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001621 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001622 SetPageUptodate(page);
1623 return 0;
1624}
1625
1626/*
1627 * helper function to unlock a page if all the extents in the tree
1628 * for that page are unlocked
1629 */
1630static int check_page_locked(struct extent_io_tree *tree,
1631 struct page *page)
1632{
1633 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1634 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001635 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001636 unlock_page(page);
1637 return 0;
1638}
1639
1640/*
1641 * helper function to end page writeback if all the extents
1642 * in the tree for that page are done with writeback
1643 */
1644static int check_page_writeback(struct extent_io_tree *tree,
1645 struct page *page)
1646{
Chris Mason1edbb732009-09-02 13:24:36 -04001647 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001648 return 0;
1649}
1650
1651/* lots and lots of room for performance fixes in the end_bio funcs */
1652
1653/*
1654 * after a writepage IO is done, we need to:
1655 * clear the uptodate bits on error
1656 * clear the writeback bits in the extent tree for this IO
1657 * end_page_writeback if the page has no more pending IO
1658 *
1659 * Scheduling is not allowed, so the extent state tree is expected
1660 * to have one and only one object corresponding to this IO.
1661 */
Chris Masond1310b22008-01-24 16:13:08 -05001662static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001663{
Chris Mason1259ab72008-05-12 13:39:03 -04001664 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001665 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001666 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001667 u64 start;
1668 u64 end;
1669 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001670 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001671
Chris Masond1310b22008-01-24 16:13:08 -05001672 do {
1673 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001674 tree = &BTRFS_I(page->mapping->host)->io_tree;
1675
Chris Masond1310b22008-01-24 16:13:08 -05001676 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1677 bvec->bv_offset;
1678 end = start + bvec->bv_len - 1;
1679
1680 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1681 whole_page = 1;
1682 else
1683 whole_page = 0;
1684
1685 if (--bvec >= bio->bi_io_vec)
1686 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001687 if (tree->ops && tree->ops->writepage_end_io_hook) {
1688 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001689 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001690 if (ret)
1691 uptodate = 0;
1692 }
1693
1694 if (!uptodate && tree->ops &&
1695 tree->ops->writepage_io_failed_hook) {
1696 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001697 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001698 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001699 uptodate = (err == 0);
1700 continue;
1701 }
1702 }
1703
Chris Masond1310b22008-01-24 16:13:08 -05001704 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001705 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001706 ClearPageUptodate(page);
1707 SetPageError(page);
1708 }
Chris Mason70dec802008-01-29 09:59:12 -05001709
Chris Masond1310b22008-01-24 16:13:08 -05001710 if (whole_page)
1711 end_page_writeback(page);
1712 else
1713 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001714 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001715
Chris Masond1310b22008-01-24 16:13:08 -05001716 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001717}
1718
1719/*
1720 * after a readpage IO is done, we need to:
1721 * clear the uptodate bits on error
1722 * set the uptodate bits if things worked
1723 * set the page up to date if all extents in the tree are uptodate
1724 * clear the lock bit in the extent tree
1725 * unlock the page if there are no other extents locked for it
1726 *
1727 * Scheduling is not allowed, so the extent state tree is expected
1728 * to have one and only one object corresponding to this IO.
1729 */
Chris Masond1310b22008-01-24 16:13:08 -05001730static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001731{
1732 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001733 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1734 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001735 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001736 u64 start;
1737 u64 end;
1738 int whole_page;
1739 int ret;
1740
Chris Masond20f7042008-12-08 16:58:54 -05001741 if (err)
1742 uptodate = 0;
1743
Chris Masond1310b22008-01-24 16:13:08 -05001744 do {
1745 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001746 struct extent_state *cached = NULL;
1747 struct extent_state *state;
1748
David Woodhouse902b22f2008-08-20 08:51:49 -04001749 tree = &BTRFS_I(page->mapping->host)->io_tree;
1750
Chris Masond1310b22008-01-24 16:13:08 -05001751 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1752 bvec->bv_offset;
1753 end = start + bvec->bv_len - 1;
1754
1755 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1756 whole_page = 1;
1757 else
1758 whole_page = 0;
1759
Chris Mason4125bf72010-02-03 18:18:45 +00001760 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001761 prefetchw(&bvec->bv_page->flags);
1762
Arne Jansen507903b2011-04-06 10:02:20 +00001763 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001764 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001765 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001766 /*
1767 * take a reference on the state, unlock will drop
1768 * the ref
1769 */
1770 cache_state(state, &cached);
1771 }
1772 spin_unlock(&tree->lock);
1773
Chris Masond1310b22008-01-24 16:13:08 -05001774 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001775 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001776 state);
Chris Masond1310b22008-01-24 16:13:08 -05001777 if (ret)
1778 uptodate = 0;
1779 }
Chris Mason7e383262008-04-09 16:28:12 -04001780 if (!uptodate && tree->ops &&
1781 tree->ops->readpage_io_failed_hook) {
1782 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001783 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001784 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001785 uptodate =
1786 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001787 if (err)
1788 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001789 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001790 continue;
1791 }
1792 }
Chris Mason70dec802008-01-29 09:59:12 -05001793
Chris Mason771ed682008-11-06 22:02:51 -05001794 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001795 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001796 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001797 }
Arne Jansen507903b2011-04-06 10:02:20 +00001798 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001799
Chris Mason70dec802008-01-29 09:59:12 -05001800 if (whole_page) {
1801 if (uptodate) {
1802 SetPageUptodate(page);
1803 } else {
1804 ClearPageUptodate(page);
1805 SetPageError(page);
1806 }
Chris Masond1310b22008-01-24 16:13:08 -05001807 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001808 } else {
1809 if (uptodate) {
1810 check_page_uptodate(tree, page);
1811 } else {
1812 ClearPageUptodate(page);
1813 SetPageError(page);
1814 }
Chris Masond1310b22008-01-24 16:13:08 -05001815 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001816 }
Chris Mason4125bf72010-02-03 18:18:45 +00001817 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001818
1819 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001820}
1821
1822/*
1823 * IO done from prepare_write is pretty simple, we just unlock
1824 * the structs in the extent tree when done, and set the uptodate bits
1825 * as appropriate.
1826 */
Chris Masond1310b22008-01-24 16:13:08 -05001827static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001828{
1829 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1830 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001831 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001832 u64 start;
1833 u64 end;
1834
Chris Masond1310b22008-01-24 16:13:08 -05001835 do {
1836 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001837 struct extent_state *cached = NULL;
David Woodhouse902b22f2008-08-20 08:51:49 -04001838 tree = &BTRFS_I(page->mapping->host)->io_tree;
1839
Chris Masond1310b22008-01-24 16:13:08 -05001840 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1841 bvec->bv_offset;
1842 end = start + bvec->bv_len - 1;
1843
1844 if (--bvec >= bio->bi_io_vec)
1845 prefetchw(&bvec->bv_page->flags);
1846
1847 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001848 set_extent_uptodate(tree, start, end, &cached,
1849 GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001850 } else {
1851 ClearPageUptodate(page);
1852 SetPageError(page);
1853 }
1854
Arne Jansen507903b2011-04-06 10:02:20 +00001855 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001856
1857 } while (bvec >= bio->bi_io_vec);
1858
1859 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001860}
1861
Miao Xie88f794e2010-11-22 03:02:55 +00001862struct bio *
1863btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1864 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001865{
1866 struct bio *bio;
1867
1868 bio = bio_alloc(gfp_flags, nr_vecs);
1869
1870 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1871 while (!bio && (nr_vecs /= 2))
1872 bio = bio_alloc(gfp_flags, nr_vecs);
1873 }
1874
1875 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001876 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001877 bio->bi_bdev = bdev;
1878 bio->bi_sector = first_sector;
1879 }
1880 return bio;
1881}
1882
Chris Masonc8b97812008-10-29 14:49:59 -04001883static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1884 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001885{
Chris Masond1310b22008-01-24 16:13:08 -05001886 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001887 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1888 struct page *page = bvec->bv_page;
1889 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001890 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001891
1892 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001893
David Woodhouse902b22f2008-08-20 08:51:49 -04001894 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001895
1896 bio_get(bio);
1897
Chris Mason065631f2008-02-20 12:07:25 -05001898 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001899 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001900 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001901 else
1902 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001903 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1904 ret = -EOPNOTSUPP;
1905 bio_put(bio);
1906 return ret;
1907}
1908
1909static int submit_extent_page(int rw, struct extent_io_tree *tree,
1910 struct page *page, sector_t sector,
1911 size_t size, unsigned long offset,
1912 struct block_device *bdev,
1913 struct bio **bio_ret,
1914 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001915 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001916 int mirror_num,
1917 unsigned long prev_bio_flags,
1918 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001919{
1920 int ret = 0;
1921 struct bio *bio;
1922 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001923 int contig = 0;
1924 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1925 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001926 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001927
1928 if (bio_ret && *bio_ret) {
1929 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001930 if (old_compressed)
1931 contig = bio->bi_sector == sector;
1932 else
1933 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1934 sector;
1935
1936 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001937 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001938 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1939 bio_flags)) ||
1940 bio_add_page(bio, page, page_size, offset) < page_size) {
1941 ret = submit_one_bio(rw, bio, mirror_num,
1942 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001943 bio = NULL;
1944 } else {
1945 return 0;
1946 }
1947 }
Chris Masonc8b97812008-10-29 14:49:59 -04001948 if (this_compressed)
1949 nr = BIO_MAX_PAGES;
1950 else
1951 nr = bio_get_nr_vecs(bdev);
1952
Miao Xie88f794e2010-11-22 03:02:55 +00001953 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001954 if (!bio)
1955 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001956
Chris Masonc8b97812008-10-29 14:49:59 -04001957 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001958 bio->bi_end_io = end_io_func;
1959 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001960
Chris Masond3977122009-01-05 21:25:51 -05001961 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001962 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001963 else
Chris Masonc8b97812008-10-29 14:49:59 -04001964 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001965
1966 return ret;
1967}
1968
1969void set_page_extent_mapped(struct page *page)
1970{
1971 if (!PagePrivate(page)) {
1972 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001973 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001974 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001975 }
1976}
1977
Christoph Hellwigb2950862008-12-02 09:54:17 -05001978static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001979{
Chris Masoneb14ab82011-02-10 12:35:00 -05001980 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05001981 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1982}
1983
1984/*
1985 * basic readpage implementation. Locked extent state structs are inserted
1986 * into the tree that are removed when the IO is done (by the end_io
1987 * handlers)
1988 */
1989static int __extent_read_full_page(struct extent_io_tree *tree,
1990 struct page *page,
1991 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001992 struct bio **bio, int mirror_num,
1993 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001994{
1995 struct inode *inode = page->mapping->host;
1996 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1997 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1998 u64 end;
1999 u64 cur = start;
2000 u64 extent_offset;
2001 u64 last_byte = i_size_read(inode);
2002 u64 block_start;
2003 u64 cur_end;
2004 sector_t sector;
2005 struct extent_map *em;
2006 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002007 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002008 int ret;
2009 int nr = 0;
2010 size_t page_offset = 0;
2011 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002012 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002013 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002014 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002015
2016 set_page_extent_mapped(page);
2017
2018 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002019 while (1) {
2020 lock_extent(tree, start, end, GFP_NOFS);
2021 ordered = btrfs_lookup_ordered_extent(inode, start);
2022 if (!ordered)
2023 break;
2024 unlock_extent(tree, start, end, GFP_NOFS);
2025 btrfs_start_ordered_extent(inode, ordered, 1);
2026 btrfs_put_ordered_extent(ordered);
2027 }
Chris Masond1310b22008-01-24 16:13:08 -05002028
Chris Masonc8b97812008-10-29 14:49:59 -04002029 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2030 char *userpage;
2031 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2032
2033 if (zero_offset) {
2034 iosize = PAGE_CACHE_SIZE - zero_offset;
2035 userpage = kmap_atomic(page, KM_USER0);
2036 memset(userpage + zero_offset, 0, iosize);
2037 flush_dcache_page(page);
2038 kunmap_atomic(userpage, KM_USER0);
2039 }
2040 }
Chris Masond1310b22008-01-24 16:13:08 -05002041 while (cur <= end) {
2042 if (cur >= last_byte) {
2043 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002044 struct extent_state *cached = NULL;
2045
Chris Masond1310b22008-01-24 16:13:08 -05002046 iosize = PAGE_CACHE_SIZE - page_offset;
2047 userpage = kmap_atomic(page, KM_USER0);
2048 memset(userpage + page_offset, 0, iosize);
2049 flush_dcache_page(page);
2050 kunmap_atomic(userpage, KM_USER0);
2051 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002052 &cached, GFP_NOFS);
2053 unlock_extent_cached(tree, cur, cur + iosize - 1,
2054 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002055 break;
2056 }
2057 em = get_extent(inode, page, page_offset, cur,
2058 end - cur + 1, 0);
2059 if (IS_ERR(em) || !em) {
2060 SetPageError(page);
2061 unlock_extent(tree, cur, end, GFP_NOFS);
2062 break;
2063 }
Chris Masond1310b22008-01-24 16:13:08 -05002064 extent_offset = cur - em->start;
2065 BUG_ON(extent_map_end(em) <= cur);
2066 BUG_ON(end < cur);
2067
Li Zefan261507a02010-12-17 14:21:50 +08002068 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002069 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002070 extent_set_compress_type(&this_bio_flag,
2071 em->compress_type);
2072 }
Chris Masonc8b97812008-10-29 14:49:59 -04002073
Chris Masond1310b22008-01-24 16:13:08 -05002074 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2075 cur_end = min(extent_map_end(em) - 1, end);
2076 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002077 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2078 disk_io_size = em->block_len;
2079 sector = em->block_start >> 9;
2080 } else {
2081 sector = (em->block_start + extent_offset) >> 9;
2082 disk_io_size = iosize;
2083 }
Chris Masond1310b22008-01-24 16:13:08 -05002084 bdev = em->bdev;
2085 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002086 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2087 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002088 free_extent_map(em);
2089 em = NULL;
2090
2091 /* we've found a hole, just zero and go on */
2092 if (block_start == EXTENT_MAP_HOLE) {
2093 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002094 struct extent_state *cached = NULL;
2095
Chris Masond1310b22008-01-24 16:13:08 -05002096 userpage = kmap_atomic(page, KM_USER0);
2097 memset(userpage + page_offset, 0, iosize);
2098 flush_dcache_page(page);
2099 kunmap_atomic(userpage, KM_USER0);
2100
2101 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002102 &cached, GFP_NOFS);
2103 unlock_extent_cached(tree, cur, cur + iosize - 1,
2104 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002105 cur = cur + iosize;
2106 page_offset += iosize;
2107 continue;
2108 }
2109 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002110 if (test_range_bit(tree, cur, cur_end,
2111 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002112 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002113 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2114 cur = cur + iosize;
2115 page_offset += iosize;
2116 continue;
2117 }
Chris Mason70dec802008-01-29 09:59:12 -05002118 /* we have an inline extent but it didn't get marked up
2119 * to date. Error out
2120 */
2121 if (block_start == EXTENT_MAP_INLINE) {
2122 SetPageError(page);
2123 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2124 cur = cur + iosize;
2125 page_offset += iosize;
2126 continue;
2127 }
Chris Masond1310b22008-01-24 16:13:08 -05002128
2129 ret = 0;
2130 if (tree->ops && tree->ops->readpage_io_hook) {
2131 ret = tree->ops->readpage_io_hook(page, cur,
2132 cur + iosize - 1);
2133 }
2134 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002135 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2136 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002137 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002138 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002139 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002140 end_bio_extent_readpage, mirror_num,
2141 *bio_flags,
2142 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002143 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002144 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002145 }
2146 if (ret)
2147 SetPageError(page);
2148 cur = cur + iosize;
2149 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002150 }
2151 if (!nr) {
2152 if (!PageError(page))
2153 SetPageUptodate(page);
2154 unlock_page(page);
2155 }
2156 return 0;
2157}
2158
2159int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2160 get_extent_t *get_extent)
2161{
2162 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002163 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002164 int ret;
2165
Chris Masonc8b97812008-10-29 14:49:59 -04002166 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2167 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002168 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002169 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002170 return ret;
2171}
Chris Masond1310b22008-01-24 16:13:08 -05002172
Chris Mason11c83492009-04-20 15:50:09 -04002173static noinline void update_nr_written(struct page *page,
2174 struct writeback_control *wbc,
2175 unsigned long nr_written)
2176{
2177 wbc->nr_to_write -= nr_written;
2178 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2179 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2180 page->mapping->writeback_index = page->index + nr_written;
2181}
2182
Chris Masond1310b22008-01-24 16:13:08 -05002183/*
2184 * the writepage semantics are similar to regular writepage. extent
2185 * records are inserted to lock ranges in the tree, and as dirty areas
2186 * are found, they are marked writeback. Then the lock bits are removed
2187 * and the end_io handler clears the writeback ranges
2188 */
2189static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2190 void *data)
2191{
2192 struct inode *inode = page->mapping->host;
2193 struct extent_page_data *epd = data;
2194 struct extent_io_tree *tree = epd->tree;
2195 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2196 u64 delalloc_start;
2197 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2198 u64 end;
2199 u64 cur = start;
2200 u64 extent_offset;
2201 u64 last_byte = i_size_read(inode);
2202 u64 block_start;
2203 u64 iosize;
2204 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002205 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002206 struct extent_map *em;
2207 struct block_device *bdev;
2208 int ret;
2209 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002210 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002211 size_t blocksize;
2212 loff_t i_size = i_size_read(inode);
2213 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2214 u64 nr_delalloc;
2215 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002216 int page_started;
2217 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002218 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002219 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002220
Chris Masonffbd5172009-04-20 15:50:09 -04002221 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002222 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002223 else
2224 write_flags = WRITE;
2225
liubo1abe9b82011-03-24 11:18:59 +00002226 trace___extent_writepage(page, inode, wbc);
2227
Chris Masond1310b22008-01-24 16:13:08 -05002228 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002229 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002230 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002231 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002232 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002233 unlock_page(page);
2234 return 0;
2235 }
2236
2237 if (page->index == end_index) {
2238 char *userpage;
2239
Chris Masond1310b22008-01-24 16:13:08 -05002240 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002241 memset(userpage + pg_offset, 0,
2242 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002243 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002244 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002245 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002246 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002247
2248 set_page_extent_mapped(page);
2249
2250 delalloc_start = start;
2251 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002252 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002253 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002254 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002255 /*
2256 * make sure the wbc mapping index is at least updated
2257 * to this page.
2258 */
2259 update_nr_written(page, wbc, 0);
2260
Chris Masond3977122009-01-05 21:25:51 -05002261 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002262 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002263 page,
2264 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002265 &delalloc_end,
2266 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002267 if (nr_delalloc == 0) {
2268 delalloc_start = delalloc_end + 1;
2269 continue;
2270 }
2271 tree->ops->fill_delalloc(inode, page, delalloc_start,
2272 delalloc_end, &page_started,
2273 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002274 /*
2275 * delalloc_end is already one less than the total
2276 * length, so we don't subtract one from
2277 * PAGE_CACHE_SIZE
2278 */
2279 delalloc_to_write += (delalloc_end - delalloc_start +
2280 PAGE_CACHE_SIZE) >>
2281 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002282 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002283 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002284 if (wbc->nr_to_write < delalloc_to_write) {
2285 int thresh = 8192;
2286
2287 if (delalloc_to_write < thresh * 2)
2288 thresh = delalloc_to_write;
2289 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2290 thresh);
2291 }
Chris Masonc8b97812008-10-29 14:49:59 -04002292
Chris Mason771ed682008-11-06 22:02:51 -05002293 /* did the fill delalloc function already unlock and start
2294 * the IO?
2295 */
2296 if (page_started) {
2297 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002298 /*
2299 * we've unlocked the page, so we can't update
2300 * the mapping's writeback index, just update
2301 * nr_to_write.
2302 */
2303 wbc->nr_to_write -= nr_written;
2304 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002305 }
Chris Masonc8b97812008-10-29 14:49:59 -04002306 }
Chris Mason247e7432008-07-17 12:53:51 -04002307 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002308 ret = tree->ops->writepage_start_hook(page, start,
2309 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002310 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002311 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002312 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002313 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002314 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002315 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002316 }
2317 }
2318
Chris Mason11c83492009-04-20 15:50:09 -04002319 /*
2320 * we don't want to touch the inode after unlocking the page,
2321 * so we update the mapping writeback index now
2322 */
2323 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002324
Chris Masond1310b22008-01-24 16:13:08 -05002325 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002326 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002327 if (tree->ops && tree->ops->writepage_end_io_hook)
2328 tree->ops->writepage_end_io_hook(page, start,
2329 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002330 goto done;
2331 }
2332
Chris Masond1310b22008-01-24 16:13:08 -05002333 blocksize = inode->i_sb->s_blocksize;
2334
2335 while (cur <= end) {
2336 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002337 if (tree->ops && tree->ops->writepage_end_io_hook)
2338 tree->ops->writepage_end_io_hook(page, cur,
2339 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002340 break;
2341 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002342 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002343 end - cur + 1, 1);
2344 if (IS_ERR(em) || !em) {
2345 SetPageError(page);
2346 break;
2347 }
2348
2349 extent_offset = cur - em->start;
2350 BUG_ON(extent_map_end(em) <= cur);
2351 BUG_ON(end < cur);
2352 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2353 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2354 sector = (em->block_start + extent_offset) >> 9;
2355 bdev = em->bdev;
2356 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002357 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002358 free_extent_map(em);
2359 em = NULL;
2360
Chris Masonc8b97812008-10-29 14:49:59 -04002361 /*
2362 * compressed and inline extents are written through other
2363 * paths in the FS
2364 */
2365 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002366 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002367 /*
2368 * end_io notification does not happen here for
2369 * compressed extents
2370 */
2371 if (!compressed && tree->ops &&
2372 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002373 tree->ops->writepage_end_io_hook(page, cur,
2374 cur + iosize - 1,
2375 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002376 else if (compressed) {
2377 /* we don't want to end_page_writeback on
2378 * a compressed extent. this happens
2379 * elsewhere
2380 */
2381 nr++;
2382 }
2383
2384 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002385 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002386 continue;
2387 }
Chris Masond1310b22008-01-24 16:13:08 -05002388 /* leave this out until we have a page_mkwrite call */
2389 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002390 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002391 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002392 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002393 continue;
2394 }
Chris Masonc8b97812008-10-29 14:49:59 -04002395
Chris Masond1310b22008-01-24 16:13:08 -05002396 if (tree->ops && tree->ops->writepage_io_hook) {
2397 ret = tree->ops->writepage_io_hook(page, cur,
2398 cur + iosize - 1);
2399 } else {
2400 ret = 0;
2401 }
Chris Mason1259ab72008-05-12 13:39:03 -04002402 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002403 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002404 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002405 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002406
Chris Masond1310b22008-01-24 16:13:08 -05002407 set_range_writeback(tree, cur, cur + iosize - 1);
2408 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002409 printk(KERN_ERR "btrfs warning page %lu not "
2410 "writeback, cur %llu end %llu\n",
2411 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002412 (unsigned long long)end);
2413 }
2414
Chris Masonffbd5172009-04-20 15:50:09 -04002415 ret = submit_extent_page(write_flags, tree, page,
2416 sector, iosize, pg_offset,
2417 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002418 end_bio_extent_writepage,
2419 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002420 if (ret)
2421 SetPageError(page);
2422 }
2423 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002424 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002425 nr++;
2426 }
2427done:
2428 if (nr == 0) {
2429 /* make sure the mapping tag for page dirty gets cleared */
2430 set_page_writeback(page);
2431 end_page_writeback(page);
2432 }
Chris Masond1310b22008-01-24 16:13:08 -05002433 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002434
Chris Mason11c83492009-04-20 15:50:09 -04002435done_unlocked:
2436
Chris Mason2c64c532009-09-02 15:04:12 -04002437 /* drop our reference on any cached states */
2438 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002439 return 0;
2440}
2441
Chris Masond1310b22008-01-24 16:13:08 -05002442/**
Chris Mason4bef0842008-09-08 11:18:08 -04002443 * 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 -05002444 * @mapping: address space structure to write
2445 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2446 * @writepage: function called for each page
2447 * @data: data passed to writepage function
2448 *
2449 * If a page is already under I/O, write_cache_pages() skips it, even
2450 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2451 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2452 * and msync() need to guarantee that all the data which was dirty at the time
2453 * the call was made get new I/O started against them. If wbc->sync_mode is
2454 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2455 * existing IO to complete.
2456 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002457static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002458 struct address_space *mapping,
2459 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002460 writepage_t writepage, void *data,
2461 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002462{
Chris Masond1310b22008-01-24 16:13:08 -05002463 int ret = 0;
2464 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002465 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002466 struct pagevec pvec;
2467 int nr_pages;
2468 pgoff_t index;
2469 pgoff_t end; /* Inclusive */
2470 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002471
Chris Masond1310b22008-01-24 16:13:08 -05002472 pagevec_init(&pvec, 0);
2473 if (wbc->range_cyclic) {
2474 index = mapping->writeback_index; /* Start from prev offset */
2475 end = -1;
2476 } else {
2477 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2478 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002479 scanned = 1;
2480 }
2481retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002482 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002483 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002484 PAGECACHE_TAG_DIRTY, min(end - index,
2485 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002486 unsigned i;
2487
2488 scanned = 1;
2489 for (i = 0; i < nr_pages; i++) {
2490 struct page *page = pvec.pages[i];
2491
2492 /*
2493 * At this point we hold neither mapping->tree_lock nor
2494 * lock on the page itself: the page may be truncated or
2495 * invalidated (changing page->mapping to NULL), or even
2496 * swizzled back from swapper_space to tmpfs file
2497 * mapping
2498 */
Chris Mason4bef0842008-09-08 11:18:08 -04002499 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2500 tree->ops->write_cache_pages_lock_hook(page);
2501 else
2502 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002503
2504 if (unlikely(page->mapping != mapping)) {
2505 unlock_page(page);
2506 continue;
2507 }
2508
2509 if (!wbc->range_cyclic && page->index > end) {
2510 done = 1;
2511 unlock_page(page);
2512 continue;
2513 }
2514
Chris Masond2c3f4f2008-11-19 12:44:22 -05002515 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002516 if (PageWriteback(page))
2517 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002518 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002519 }
Chris Masond1310b22008-01-24 16:13:08 -05002520
2521 if (PageWriteback(page) ||
2522 !clear_page_dirty_for_io(page)) {
2523 unlock_page(page);
2524 continue;
2525 }
2526
2527 ret = (*writepage)(page, wbc, data);
2528
2529 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2530 unlock_page(page);
2531 ret = 0;
2532 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002533 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002534 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002535
2536 /*
2537 * the filesystem may choose to bump up nr_to_write.
2538 * We have to make sure to honor the new nr_to_write
2539 * at any time
2540 */
2541 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002542 }
2543 pagevec_release(&pvec);
2544 cond_resched();
2545 }
2546 if (!scanned && !done) {
2547 /*
2548 * We hit the last page and there is more work to be done: wrap
2549 * back to the start of the file
2550 */
2551 scanned = 1;
2552 index = 0;
2553 goto retry;
2554 }
Chris Masond1310b22008-01-24 16:13:08 -05002555 return ret;
2556}
Chris Masond1310b22008-01-24 16:13:08 -05002557
Chris Masonffbd5172009-04-20 15:50:09 -04002558static void flush_epd_write_bio(struct extent_page_data *epd)
2559{
2560 if (epd->bio) {
2561 if (epd->sync_io)
2562 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2563 else
2564 submit_one_bio(WRITE, epd->bio, 0, 0);
2565 epd->bio = NULL;
2566 }
2567}
2568
Chris Masond2c3f4f2008-11-19 12:44:22 -05002569static noinline void flush_write_bio(void *data)
2570{
2571 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002572 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002573}
2574
Chris Masond1310b22008-01-24 16:13:08 -05002575int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2576 get_extent_t *get_extent,
2577 struct writeback_control *wbc)
2578{
2579 int ret;
2580 struct address_space *mapping = page->mapping;
2581 struct extent_page_data epd = {
2582 .bio = NULL,
2583 .tree = tree,
2584 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002585 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002586 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002587 };
2588 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002589 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002590 .older_than_this = NULL,
2591 .nr_to_write = 64,
2592 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2593 .range_end = (loff_t)-1,
2594 };
2595
Chris Masond1310b22008-01-24 16:13:08 -05002596 ret = __extent_writepage(page, wbc, &epd);
2597
Chris Mason4bef0842008-09-08 11:18:08 -04002598 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002599 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002600 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002601 return ret;
2602}
Chris Masond1310b22008-01-24 16:13:08 -05002603
Chris Mason771ed682008-11-06 22:02:51 -05002604int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2605 u64 start, u64 end, get_extent_t *get_extent,
2606 int mode)
2607{
2608 int ret = 0;
2609 struct address_space *mapping = inode->i_mapping;
2610 struct page *page;
2611 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2612 PAGE_CACHE_SHIFT;
2613
2614 struct extent_page_data epd = {
2615 .bio = NULL,
2616 .tree = tree,
2617 .get_extent = get_extent,
2618 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002619 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002620 };
2621 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002622 .sync_mode = mode,
2623 .older_than_this = NULL,
2624 .nr_to_write = nr_pages * 2,
2625 .range_start = start,
2626 .range_end = end + 1,
2627 };
2628
Chris Masond3977122009-01-05 21:25:51 -05002629 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002630 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2631 if (clear_page_dirty_for_io(page))
2632 ret = __extent_writepage(page, &wbc_writepages, &epd);
2633 else {
2634 if (tree->ops && tree->ops->writepage_end_io_hook)
2635 tree->ops->writepage_end_io_hook(page, start,
2636 start + PAGE_CACHE_SIZE - 1,
2637 NULL, 1);
2638 unlock_page(page);
2639 }
2640 page_cache_release(page);
2641 start += PAGE_CACHE_SIZE;
2642 }
2643
Chris Masonffbd5172009-04-20 15:50:09 -04002644 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002645 return ret;
2646}
Chris Masond1310b22008-01-24 16:13:08 -05002647
2648int extent_writepages(struct extent_io_tree *tree,
2649 struct address_space *mapping,
2650 get_extent_t *get_extent,
2651 struct writeback_control *wbc)
2652{
2653 int ret = 0;
2654 struct extent_page_data epd = {
2655 .bio = NULL,
2656 .tree = tree,
2657 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002658 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002659 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002660 };
2661
Chris Mason4bef0842008-09-08 11:18:08 -04002662 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002663 __extent_writepage, &epd,
2664 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002665 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002666 return ret;
2667}
Chris Masond1310b22008-01-24 16:13:08 -05002668
2669int extent_readpages(struct extent_io_tree *tree,
2670 struct address_space *mapping,
2671 struct list_head *pages, unsigned nr_pages,
2672 get_extent_t get_extent)
2673{
2674 struct bio *bio = NULL;
2675 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002676 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002677
Chris Masond1310b22008-01-24 16:13:08 -05002678 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2679 struct page *page = list_entry(pages->prev, struct page, lru);
2680
2681 prefetchw(&page->flags);
2682 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002683 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002684 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002685 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002686 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002687 }
2688 page_cache_release(page);
2689 }
Chris Masond1310b22008-01-24 16:13:08 -05002690 BUG_ON(!list_empty(pages));
2691 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002692 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002693 return 0;
2694}
Chris Masond1310b22008-01-24 16:13:08 -05002695
2696/*
2697 * basic invalidatepage code, this waits on any locked or writeback
2698 * ranges corresponding to the page, and then deletes any extent state
2699 * records from the tree
2700 */
2701int extent_invalidatepage(struct extent_io_tree *tree,
2702 struct page *page, unsigned long offset)
2703{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002704 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002705 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2706 u64 end = start + PAGE_CACHE_SIZE - 1;
2707 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2708
Chris Masond3977122009-01-05 21:25:51 -05002709 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002710 if (start > end)
2711 return 0;
2712
Josef Bacik2ac55d42010-02-03 19:33:23 +00002713 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002714 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002715 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002716 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2717 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002718 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002719 return 0;
2720}
Chris Masond1310b22008-01-24 16:13:08 -05002721
2722/*
2723 * simple commit_write call, set_range_dirty is used to mark both
2724 * the pages and the extent records as dirty
2725 */
2726int extent_commit_write(struct extent_io_tree *tree,
2727 struct inode *inode, struct page *page,
2728 unsigned from, unsigned to)
2729{
2730 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2731
2732 set_page_extent_mapped(page);
2733 set_page_dirty(page);
2734
2735 if (pos > inode->i_size) {
2736 i_size_write(inode, pos);
2737 mark_inode_dirty(inode);
2738 }
2739 return 0;
2740}
Chris Masond1310b22008-01-24 16:13:08 -05002741
2742int extent_prepare_write(struct extent_io_tree *tree,
2743 struct inode *inode, struct page *page,
2744 unsigned from, unsigned to, get_extent_t *get_extent)
2745{
2746 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2747 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2748 u64 block_start;
2749 u64 orig_block_start;
2750 u64 block_end;
2751 u64 cur_end;
2752 struct extent_map *em;
2753 unsigned blocksize = 1 << inode->i_blkbits;
2754 size_t page_offset = 0;
2755 size_t block_off_start;
2756 size_t block_off_end;
2757 int err = 0;
2758 int iocount = 0;
2759 int ret = 0;
2760 int isnew;
2761
2762 set_page_extent_mapped(page);
2763
2764 block_start = (page_start + from) & ~((u64)blocksize - 1);
2765 block_end = (page_start + to - 1) | (blocksize - 1);
2766 orig_block_start = block_start;
2767
2768 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002769 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002770 em = get_extent(inode, page, page_offset, block_start,
2771 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002772 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002773 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002774
Chris Masond1310b22008-01-24 16:13:08 -05002775 cur_end = min(block_end, extent_map_end(em) - 1);
2776 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2777 block_off_end = block_off_start + blocksize;
2778 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2779
2780 if (!PageUptodate(page) && isnew &&
2781 (block_off_end > to || block_off_start < from)) {
2782 void *kaddr;
2783
2784 kaddr = kmap_atomic(page, KM_USER0);
2785 if (block_off_end > to)
2786 memset(kaddr + to, 0, block_off_end - to);
2787 if (block_off_start < from)
2788 memset(kaddr + block_off_start, 0,
2789 from - block_off_start);
2790 flush_dcache_page(page);
2791 kunmap_atomic(kaddr, KM_USER0);
2792 }
2793 if ((em->block_start != EXTENT_MAP_HOLE &&
2794 em->block_start != EXTENT_MAP_INLINE) &&
2795 !isnew && !PageUptodate(page) &&
2796 (block_off_end > to || block_off_start < from) &&
2797 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002798 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002799 u64 sector;
2800 u64 extent_offset = block_start - em->start;
2801 size_t iosize;
2802 sector = (em->block_start + extent_offset) >> 9;
2803 iosize = (cur_end - block_start + blocksize) &
2804 ~((u64)blocksize - 1);
2805 /*
2806 * we've already got the extent locked, but we
2807 * need to split the state such that our end_bio
2808 * handler can clear the lock.
2809 */
2810 set_extent_bit(tree, block_start,
2811 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002812 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002813 ret = submit_extent_page(READ, tree, page,
2814 sector, iosize, page_offset, em->bdev,
2815 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002816 end_bio_extent_preparewrite, 0,
2817 0, 0);
Andi Kleen411fc6b2010-10-29 15:14:31 -04002818 if (ret && !err)
2819 err = ret;
Chris Masond1310b22008-01-24 16:13:08 -05002820 iocount++;
2821 block_start = block_start + iosize;
2822 } else {
Arne Jansen507903b2011-04-06 10:02:20 +00002823 struct extent_state *cached = NULL;
2824
2825 set_extent_uptodate(tree, block_start, cur_end, &cached,
Chris Masond1310b22008-01-24 16:13:08 -05002826 GFP_NOFS);
Arne Jansen507903b2011-04-06 10:02:20 +00002827 unlock_extent_cached(tree, block_start, cur_end,
2828 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002829 block_start = cur_end + 1;
2830 }
2831 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2832 free_extent_map(em);
2833 }
2834 if (iocount) {
2835 wait_extent_bit(tree, orig_block_start,
2836 block_end, EXTENT_LOCKED);
2837 }
2838 check_page_uptodate(tree, page);
2839err:
2840 /* FIXME, zero out newly allocated blocks on error */
2841 return err;
2842}
Chris Masond1310b22008-01-24 16:13:08 -05002843
2844/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002845 * a helper for releasepage, this tests for areas of the page that
2846 * are locked or under IO and drops the related state bits if it is safe
2847 * to drop the page.
2848 */
2849int try_release_extent_state(struct extent_map_tree *map,
2850 struct extent_io_tree *tree, struct page *page,
2851 gfp_t mask)
2852{
2853 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2854 u64 end = start + PAGE_CACHE_SIZE - 1;
2855 int ret = 1;
2856
Chris Mason211f90e2008-07-18 11:56:15 -04002857 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002858 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002859 ret = 0;
2860 else {
2861 if ((mask & GFP_NOFS) == GFP_NOFS)
2862 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002863 /*
2864 * at this point we can safely clear everything except the
2865 * locked bit and the nodatasum bit
2866 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002867 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002868 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2869 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002870
2871 /* if clear_extent_bit failed for enomem reasons,
2872 * we can't allow the release to continue.
2873 */
2874 if (ret < 0)
2875 ret = 0;
2876 else
2877 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002878 }
2879 return ret;
2880}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002881
2882/*
Chris Masond1310b22008-01-24 16:13:08 -05002883 * a helper for releasepage. As long as there are no locked extents
2884 * in the range corresponding to the page, both state records and extent
2885 * map records are removed
2886 */
2887int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002888 struct extent_io_tree *tree, struct page *page,
2889 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002890{
2891 struct extent_map *em;
2892 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2893 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002894
Chris Mason70dec802008-01-29 09:59:12 -05002895 if ((mask & __GFP_WAIT) &&
2896 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002897 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002898 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002899 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002900 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002901 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002902 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002903 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002904 break;
2905 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002906 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2907 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002908 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002909 free_extent_map(em);
2910 break;
2911 }
2912 if (!test_range_bit(tree, em->start,
2913 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002914 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002915 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002916 remove_extent_mapping(map, em);
2917 /* once for the rb tree */
2918 free_extent_map(em);
2919 }
2920 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002921 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002922
2923 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002924 free_extent_map(em);
2925 }
Chris Masond1310b22008-01-24 16:13:08 -05002926 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002927 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002928}
Chris Masond1310b22008-01-24 16:13:08 -05002929
2930sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2931 get_extent_t *get_extent)
2932{
2933 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002934 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002935 u64 start = iblock << inode->i_blkbits;
2936 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002937 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002938 struct extent_map *em;
2939
Josef Bacik2ac55d42010-02-03 19:33:23 +00002940 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2941 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002942 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002943 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2944 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002945 if (!em || IS_ERR(em))
2946 return 0;
2947
Yan Zhengd899e052008-10-30 14:25:28 -04002948 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002949 goto out;
2950
2951 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002952out:
2953 free_extent_map(em);
2954 return sector;
2955}
2956
Chris Masonec29ed52011-02-23 16:23:20 -05002957/*
2958 * helper function for fiemap, which doesn't want to see any holes.
2959 * This maps until we find something past 'last'
2960 */
2961static struct extent_map *get_extent_skip_holes(struct inode *inode,
2962 u64 offset,
2963 u64 last,
2964 get_extent_t *get_extent)
2965{
2966 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2967 struct extent_map *em;
2968 u64 len;
2969
2970 if (offset >= last)
2971 return NULL;
2972
2973 while(1) {
2974 len = last - offset;
2975 if (len == 0)
2976 break;
2977 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2978 em = get_extent(inode, NULL, 0, offset, len, 0);
2979 if (!em || IS_ERR(em))
2980 return em;
2981
2982 /* if this isn't a hole return it */
2983 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2984 em->block_start != EXTENT_MAP_HOLE) {
2985 return em;
2986 }
2987
2988 /* this is a hole, advance to the next extent */
2989 offset = extent_map_end(em);
2990 free_extent_map(em);
2991 if (offset >= last)
2992 break;
2993 }
2994 return NULL;
2995}
2996
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002997int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2998 __u64 start, __u64 len, get_extent_t *get_extent)
2999{
Josef Bacik975f84f2010-11-23 19:36:57 +00003000 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003001 u64 off = start;
3002 u64 max = start + len;
3003 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003004 u32 found_type;
3005 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003006 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003007 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003008 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003009 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003010 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003011 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003012 struct btrfs_path *path;
3013 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003014 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003015 u64 em_start = 0;
3016 u64 em_len = 0;
3017 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003018 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003019
3020 if (len == 0)
3021 return -EINVAL;
3022
Josef Bacik975f84f2010-11-23 19:36:57 +00003023 path = btrfs_alloc_path();
3024 if (!path)
3025 return -ENOMEM;
3026 path->leave_spinning = 1;
3027
Chris Masonec29ed52011-02-23 16:23:20 -05003028 /*
3029 * lookup the last file extent. We're not using i_size here
3030 * because there might be preallocation past i_size
3031 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003032 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
3033 path, inode->i_ino, -1, 0);
3034 if (ret < 0) {
3035 btrfs_free_path(path);
3036 return ret;
3037 }
3038 WARN_ON(!ret);
3039 path->slots[0]--;
3040 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3041 struct btrfs_file_extent_item);
3042 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3043 found_type = btrfs_key_type(&found_key);
3044
Chris Masonec29ed52011-02-23 16:23:20 -05003045 /* No extents, but there might be delalloc bits */
Josef Bacik975f84f2010-11-23 19:36:57 +00003046 if (found_key.objectid != inode->i_ino ||
3047 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003048 /* have to trust i_size as the end */
3049 last = (u64)-1;
3050 last_for_get_extent = isize;
3051 } else {
3052 /*
3053 * remember the start of the last extent. There are a
3054 * bunch of different factors that go into the length of the
3055 * extent, so its much less complex to remember where it started
3056 */
3057 last = found_key.offset;
3058 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003059 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003060 btrfs_free_path(path);
3061
Chris Masonec29ed52011-02-23 16:23:20 -05003062 /*
3063 * we might have some extents allocated but more delalloc past those
3064 * extents. so, we trust isize unless the start of the last extent is
3065 * beyond isize
3066 */
3067 if (last < isize) {
3068 last = (u64)-1;
3069 last_for_get_extent = isize;
3070 }
3071
Josef Bacik2ac55d42010-02-03 19:33:23 +00003072 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3073 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003074
3075 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3076 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003077 if (!em)
3078 goto out;
3079 if (IS_ERR(em)) {
3080 ret = PTR_ERR(em);
3081 goto out;
3082 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003083
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003084 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003085 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003086
Chris Masonea8efc72011-03-08 11:54:40 -05003087 /* break if the extent we found is outside the range */
3088 if (em->start >= max || extent_map_end(em) < off)
3089 break;
3090
3091 /*
3092 * get_extent may return an extent that starts before our
3093 * requested range. We have to make sure the ranges
3094 * we return to fiemap always move forward and don't
3095 * overlap, so adjust the offsets here
3096 */
3097 em_start = max(em->start, off);
3098
3099 /*
3100 * record the offset from the start of the extent
3101 * for adjusting the disk offset below
3102 */
3103 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003104 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003105 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003106 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003107 disko = 0;
3108 flags = 0;
3109
Chris Masonea8efc72011-03-08 11:54:40 -05003110 /*
3111 * bump off for our next call to get_extent
3112 */
3113 off = extent_map_end(em);
3114 if (off >= max)
3115 end = 1;
3116
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003117 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003118 end = 1;
3119 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003120 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003121 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3122 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003123 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003124 flags |= (FIEMAP_EXTENT_DELALLOC |
3125 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003126 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003127 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003128 }
3129 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3130 flags |= FIEMAP_EXTENT_ENCODED;
3131
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003132 free_extent_map(em);
3133 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003134 if ((em_start >= last) || em_len == (u64)-1 ||
3135 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003136 flags |= FIEMAP_EXTENT_LAST;
3137 end = 1;
3138 }
3139
Chris Masonec29ed52011-02-23 16:23:20 -05003140 /* now scan forward to see if this is really the last extent. */
3141 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3142 get_extent);
3143 if (IS_ERR(em)) {
3144 ret = PTR_ERR(em);
3145 goto out;
3146 }
3147 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003148 flags |= FIEMAP_EXTENT_LAST;
3149 end = 1;
3150 }
Chris Masonec29ed52011-02-23 16:23:20 -05003151 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3152 em_len, flags);
3153 if (ret)
3154 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003155 }
3156out_free:
3157 free_extent_map(em);
3158out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003159 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3160 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003161 return ret;
3162}
3163
Chris Masond1310b22008-01-24 16:13:08 -05003164static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3165 unsigned long i)
3166{
3167 struct page *p;
3168 struct address_space *mapping;
3169
3170 if (i == 0)
3171 return eb->first_page;
3172 i += eb->start >> PAGE_CACHE_SHIFT;
3173 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003174 if (!mapping)
3175 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003176
3177 /*
3178 * extent_buffer_page is only called after pinning the page
3179 * by increasing the reference count. So we know the page must
3180 * be in the radix tree.
3181 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003182 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003183 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003184 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003185
Chris Masond1310b22008-01-24 16:13:08 -05003186 return p;
3187}
3188
Chris Mason6af118ce2008-07-22 11:18:07 -04003189static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003190{
Chris Mason6af118ce2008-07-22 11:18:07 -04003191 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3192 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003193}
3194
Chris Masond1310b22008-01-24 16:13:08 -05003195static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3196 u64 start,
3197 unsigned long len,
3198 gfp_t mask)
3199{
3200 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003201#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003202 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003203#endif
Chris Masond1310b22008-01-24 16:13:08 -05003204
Chris Masond1310b22008-01-24 16:13:08 -05003205 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003206 if (eb == NULL)
3207 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003208 eb->start = start;
3209 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003210 spin_lock_init(&eb->lock);
3211 init_waitqueue_head(&eb->lock_wq);
3212
Chris Mason39351272009-02-04 09:24:05 -05003213#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003214 spin_lock_irqsave(&leak_lock, flags);
3215 list_add(&eb->leak_list, &buffers);
3216 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003217#endif
Chris Masond1310b22008-01-24 16:13:08 -05003218 atomic_set(&eb->refs, 1);
3219
3220 return eb;
3221}
3222
3223static void __free_extent_buffer(struct extent_buffer *eb)
3224{
Chris Mason39351272009-02-04 09:24:05 -05003225#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003226 unsigned long flags;
3227 spin_lock_irqsave(&leak_lock, flags);
3228 list_del(&eb->leak_list);
3229 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003230#endif
Chris Masond1310b22008-01-24 16:13:08 -05003231 kmem_cache_free(extent_buffer_cache, eb);
3232}
3233
Miao Xie897ca6e2010-10-26 20:57:29 -04003234/*
3235 * Helper for releasing extent buffer page.
3236 */
3237static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3238 unsigned long start_idx)
3239{
3240 unsigned long index;
3241 struct page *page;
3242
3243 if (!eb->first_page)
3244 return;
3245
3246 index = num_extent_pages(eb->start, eb->len);
3247 if (start_idx >= index)
3248 return;
3249
3250 do {
3251 index--;
3252 page = extent_buffer_page(eb, index);
3253 if (page)
3254 page_cache_release(page);
3255 } while (index != start_idx);
3256}
3257
3258/*
3259 * Helper for releasing the extent buffer.
3260 */
3261static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3262{
3263 btrfs_release_extent_buffer_page(eb, 0);
3264 __free_extent_buffer(eb);
3265}
3266
Chris Masond1310b22008-01-24 16:13:08 -05003267struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3268 u64 start, unsigned long len,
3269 struct page *page0,
3270 gfp_t mask)
3271{
3272 unsigned long num_pages = num_extent_pages(start, len);
3273 unsigned long i;
3274 unsigned long index = start >> PAGE_CACHE_SHIFT;
3275 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003276 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003277 struct page *p;
3278 struct address_space *mapping = tree->mapping;
3279 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003280 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003281
Miao Xie19fe0a82010-10-26 20:57:29 -04003282 rcu_read_lock();
3283 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3284 if (eb && atomic_inc_not_zero(&eb->refs)) {
3285 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003286 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003287 return eb;
3288 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003289 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003290
Chris Masond1310b22008-01-24 16:13:08 -05003291 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003292 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003293 return NULL;
3294
Chris Masond1310b22008-01-24 16:13:08 -05003295 if (page0) {
3296 eb->first_page = page0;
3297 i = 1;
3298 index++;
3299 page_cache_get(page0);
3300 mark_page_accessed(page0);
3301 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003302 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003303 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003304 } else {
3305 i = 0;
3306 }
3307 for (; i < num_pages; i++, index++) {
3308 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3309 if (!p) {
3310 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003311 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003312 }
3313 set_page_extent_mapped(p);
3314 mark_page_accessed(p);
3315 if (i == 0) {
3316 eb->first_page = p;
3317 set_page_extent_head(p, len);
3318 } else {
3319 set_page_private(p, EXTENT_PAGE_PRIVATE);
3320 }
3321 if (!PageUptodate(p))
3322 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003323
3324 /*
3325 * see below about how we avoid a nasty race with release page
3326 * and why we unlock later
3327 */
3328 if (i != 0)
3329 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003330 }
3331 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003332 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003333
Miao Xie19fe0a82010-10-26 20:57:29 -04003334 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3335 if (ret)
3336 goto free_eb;
3337
Chris Mason6af118ce2008-07-22 11:18:07 -04003338 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003339 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3340 if (ret == -EEXIST) {
3341 exists = radix_tree_lookup(&tree->buffer,
3342 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003343 /* add one reference for the caller */
3344 atomic_inc(&exists->refs);
3345 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003346 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003347 goto free_eb;
3348 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003349 /* add one reference for the tree */
3350 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003351 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003352 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003353
3354 /*
3355 * there is a race where release page may have
3356 * tried to find this extent buffer in the radix
3357 * but failed. It will tell the VM it is safe to
3358 * reclaim the, and it will clear the page private bit.
3359 * We must make sure to set the page private bit properly
3360 * after the extent buffer is in the radix tree so
3361 * it doesn't get lost
3362 */
3363 set_page_extent_mapped(eb->first_page);
3364 set_page_extent_head(eb->first_page, eb->len);
3365 if (!page0)
3366 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003367 return eb;
3368
Chris Mason6af118ce2008-07-22 11:18:07 -04003369free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003370 if (eb->first_page && !page0)
3371 unlock_page(eb->first_page);
3372
Chris Masond1310b22008-01-24 16:13:08 -05003373 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003374 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003375 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003376 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003377}
Chris Masond1310b22008-01-24 16:13:08 -05003378
3379struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3380 u64 start, unsigned long len,
3381 gfp_t mask)
3382{
Chris Masond1310b22008-01-24 16:13:08 -05003383 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003384
Miao Xie19fe0a82010-10-26 20:57:29 -04003385 rcu_read_lock();
3386 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3387 if (eb && atomic_inc_not_zero(&eb->refs)) {
3388 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003389 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003390 return eb;
3391 }
3392 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003393
Miao Xie19fe0a82010-10-26 20:57:29 -04003394 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003395}
Chris Masond1310b22008-01-24 16:13:08 -05003396
3397void free_extent_buffer(struct extent_buffer *eb)
3398{
Chris Masond1310b22008-01-24 16:13:08 -05003399 if (!eb)
3400 return;
3401
3402 if (!atomic_dec_and_test(&eb->refs))
3403 return;
3404
Chris Mason6af118ce2008-07-22 11:18:07 -04003405 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003406}
Chris Masond1310b22008-01-24 16:13:08 -05003407
3408int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3409 struct extent_buffer *eb)
3410{
Chris Masond1310b22008-01-24 16:13:08 -05003411 unsigned long i;
3412 unsigned long num_pages;
3413 struct page *page;
3414
Chris Masond1310b22008-01-24 16:13:08 -05003415 num_pages = num_extent_pages(eb->start, eb->len);
3416
3417 for (i = 0; i < num_pages; i++) {
3418 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003419 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003420 continue;
3421
Chris Masona61e6f22008-07-22 11:18:08 -04003422 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003423 WARN_ON(!PagePrivate(page));
3424
3425 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003426 if (i == 0)
3427 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003428
Chris Masond1310b22008-01-24 16:13:08 -05003429 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003430 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003431 if (!PageDirty(page)) {
3432 radix_tree_tag_clear(&page->mapping->page_tree,
3433 page_index(page),
3434 PAGECACHE_TAG_DIRTY);
3435 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003436 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003437 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003438 }
3439 return 0;
3440}
Chris Masond1310b22008-01-24 16:13:08 -05003441
3442int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3443 struct extent_buffer *eb)
3444{
3445 return wait_on_extent_writeback(tree, eb->start,
3446 eb->start + eb->len - 1);
3447}
Chris Masond1310b22008-01-24 16:13:08 -05003448
3449int set_extent_buffer_dirty(struct extent_io_tree *tree,
3450 struct extent_buffer *eb)
3451{
3452 unsigned long i;
3453 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003454 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003455
Chris Masonb9473432009-03-13 11:00:37 -04003456 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003457 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003458 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003459 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003460 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003461}
Chris Masond1310b22008-01-24 16:13:08 -05003462
Chris Mason1259ab72008-05-12 13:39:03 -04003463int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003464 struct extent_buffer *eb,
3465 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003466{
3467 unsigned long i;
3468 struct page *page;
3469 unsigned long num_pages;
3470
3471 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003472 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003473
3474 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003475 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003476 for (i = 0; i < num_pages; i++) {
3477 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003478 if (page)
3479 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003480 }
3481 return 0;
3482}
3483
Chris Masond1310b22008-01-24 16:13:08 -05003484int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3485 struct extent_buffer *eb)
3486{
3487 unsigned long i;
3488 struct page *page;
3489 unsigned long num_pages;
3490
3491 num_pages = num_extent_pages(eb->start, eb->len);
3492
3493 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003494 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003495 for (i = 0; i < num_pages; i++) {
3496 page = extent_buffer_page(eb, i);
3497 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3498 ((i == num_pages - 1) &&
3499 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3500 check_page_uptodate(tree, page);
3501 continue;
3502 }
3503 SetPageUptodate(page);
3504 }
3505 return 0;
3506}
Chris Masond1310b22008-01-24 16:13:08 -05003507
Chris Masonce9adaa2008-04-09 16:28:12 -04003508int extent_range_uptodate(struct extent_io_tree *tree,
3509 u64 start, u64 end)
3510{
3511 struct page *page;
3512 int ret;
3513 int pg_uptodate = 1;
3514 int uptodate;
3515 unsigned long index;
3516
Chris Mason9655d292009-09-02 15:22:30 -04003517 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003518 if (ret)
3519 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003520 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003521 index = start >> PAGE_CACHE_SHIFT;
3522 page = find_get_page(tree->mapping, index);
3523 uptodate = PageUptodate(page);
3524 page_cache_release(page);
3525 if (!uptodate) {
3526 pg_uptodate = 0;
3527 break;
3528 }
3529 start += PAGE_CACHE_SIZE;
3530 }
3531 return pg_uptodate;
3532}
3533
Chris Masond1310b22008-01-24 16:13:08 -05003534int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003535 struct extent_buffer *eb,
3536 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003537{
Chris Mason728131d2008-04-09 16:28:12 -04003538 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003539 unsigned long num_pages;
3540 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003541 struct page *page;
3542 int pg_uptodate = 1;
3543
Chris Masonb4ce94d2009-02-04 09:25:08 -05003544 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003545 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003546
Chris Mason42352982008-04-28 16:40:52 -04003547 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003549 if (ret)
3550 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003551
3552 num_pages = num_extent_pages(eb->start, eb->len);
3553 for (i = 0; i < num_pages; i++) {
3554 page = extent_buffer_page(eb, i);
3555 if (!PageUptodate(page)) {
3556 pg_uptodate = 0;
3557 break;
3558 }
3559 }
Chris Mason42352982008-04-28 16:40:52 -04003560 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003561}
Chris Masond1310b22008-01-24 16:13:08 -05003562
3563int read_extent_buffer_pages(struct extent_io_tree *tree,
3564 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003565 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003566 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003567{
3568 unsigned long i;
3569 unsigned long start_i;
3570 struct page *page;
3571 int err;
3572 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003573 int locked_pages = 0;
3574 int all_uptodate = 1;
3575 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003576 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003577 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003578 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003579
Chris Masonb4ce94d2009-02-04 09:25:08 -05003580 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003581 return 0;
3582
Chris Masonce9adaa2008-04-09 16:28:12 -04003583 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003584 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003585 return 0;
3586 }
3587
3588 if (start) {
3589 WARN_ON(start < eb->start);
3590 start_i = (start >> PAGE_CACHE_SHIFT) -
3591 (eb->start >> PAGE_CACHE_SHIFT);
3592 } else {
3593 start_i = 0;
3594 }
3595
3596 num_pages = num_extent_pages(eb->start, eb->len);
3597 for (i = start_i; i < num_pages; i++) {
3598 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003599 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003600 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003601 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003602 } else {
3603 lock_page(page);
3604 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003605 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003606 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003607 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003608 }
3609 if (all_uptodate) {
3610 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003611 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003612 goto unlock_exit;
3613 }
3614
3615 for (i = start_i; i < num_pages; i++) {
3616 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003617
3618 WARN_ON(!PagePrivate(page));
3619
3620 set_page_extent_mapped(page);
3621 if (i == 0)
3622 set_page_extent_head(page, eb->len);
3623
Chris Masonce9adaa2008-04-09 16:28:12 -04003624 if (inc_all_pages)
3625 page_cache_get(page);
3626 if (!PageUptodate(page)) {
3627 if (start_i == 0)
3628 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003629 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003630 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003631 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003632 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003633 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003634 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003635 } else {
3636 unlock_page(page);
3637 }
3638 }
3639
Chris Masona86c12c2008-02-07 10:50:54 -05003640 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003641 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003642
Chris Masond3977122009-01-05 21:25:51 -05003643 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003644 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003645
Chris Masond1310b22008-01-24 16:13:08 -05003646 for (i = start_i; i < num_pages; i++) {
3647 page = extent_buffer_page(eb, i);
3648 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003649 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003650 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003651 }
Chris Masond3977122009-01-05 21:25:51 -05003652
Chris Masond1310b22008-01-24 16:13:08 -05003653 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003654 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003655 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003656
3657unlock_exit:
3658 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003659 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003660 page = extent_buffer_page(eb, i);
3661 i++;
3662 unlock_page(page);
3663 locked_pages--;
3664 }
3665 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003666}
Chris Masond1310b22008-01-24 16:13:08 -05003667
3668void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3669 unsigned long start,
3670 unsigned long len)
3671{
3672 size_t cur;
3673 size_t offset;
3674 struct page *page;
3675 char *kaddr;
3676 char *dst = (char *)dstv;
3677 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3678 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003679
3680 WARN_ON(start > eb->len);
3681 WARN_ON(start + len > eb->start + eb->len);
3682
3683 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3684
Chris Masond3977122009-01-05 21:25:51 -05003685 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003686 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003687
3688 cur = min(len, (PAGE_CACHE_SIZE - offset));
3689 kaddr = kmap_atomic(page, KM_USER1);
3690 memcpy(dst, kaddr + offset, cur);
3691 kunmap_atomic(kaddr, KM_USER1);
3692
3693 dst += cur;
3694 len -= cur;
3695 offset = 0;
3696 i++;
3697 }
3698}
Chris Masond1310b22008-01-24 16:13:08 -05003699
3700int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3701 unsigned long min_len, char **token, char **map,
3702 unsigned long *map_start,
3703 unsigned long *map_len, int km)
3704{
3705 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3706 char *kaddr;
3707 struct page *p;
3708 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3709 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3710 unsigned long end_i = (start_offset + start + min_len - 1) >>
3711 PAGE_CACHE_SHIFT;
3712
3713 if (i != end_i)
3714 return -EINVAL;
3715
3716 if (i == 0) {
3717 offset = start_offset;
3718 *map_start = 0;
3719 } else {
3720 offset = 0;
3721 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3722 }
Chris Masond3977122009-01-05 21:25:51 -05003723
Chris Masond1310b22008-01-24 16:13:08 -05003724 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003725 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3726 "wanted %lu %lu\n", (unsigned long long)eb->start,
3727 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003728 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003729 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003730 }
3731
3732 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003733 kaddr = kmap_atomic(p, km);
3734 *token = kaddr;
3735 *map = kaddr + offset;
3736 *map_len = PAGE_CACHE_SIZE - offset;
3737 return 0;
3738}
Chris Masond1310b22008-01-24 16:13:08 -05003739
3740int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3741 unsigned long min_len,
3742 char **token, char **map,
3743 unsigned long *map_start,
3744 unsigned long *map_len, int km)
3745{
3746 int err;
3747 int save = 0;
3748 if (eb->map_token) {
3749 unmap_extent_buffer(eb, eb->map_token, km);
3750 eb->map_token = NULL;
3751 save = 1;
3752 }
3753 err = map_private_extent_buffer(eb, start, min_len, token, map,
3754 map_start, map_len, km);
3755 if (!err && save) {
3756 eb->map_token = *token;
3757 eb->kaddr = *map;
3758 eb->map_start = *map_start;
3759 eb->map_len = *map_len;
3760 }
3761 return err;
3762}
Chris Masond1310b22008-01-24 16:13:08 -05003763
3764void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3765{
3766 kunmap_atomic(token, km);
3767}
Chris Masond1310b22008-01-24 16:13:08 -05003768
3769int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3770 unsigned long start,
3771 unsigned long len)
3772{
3773 size_t cur;
3774 size_t offset;
3775 struct page *page;
3776 char *kaddr;
3777 char *ptr = (char *)ptrv;
3778 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3779 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3780 int ret = 0;
3781
3782 WARN_ON(start > eb->len);
3783 WARN_ON(start + len > eb->start + eb->len);
3784
3785 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3786
Chris Masond3977122009-01-05 21:25:51 -05003787 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003788 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003789
3790 cur = min(len, (PAGE_CACHE_SIZE - offset));
3791
3792 kaddr = kmap_atomic(page, KM_USER0);
3793 ret = memcmp(ptr, kaddr + offset, cur);
3794 kunmap_atomic(kaddr, KM_USER0);
3795 if (ret)
3796 break;
3797
3798 ptr += cur;
3799 len -= cur;
3800 offset = 0;
3801 i++;
3802 }
3803 return ret;
3804}
Chris Masond1310b22008-01-24 16:13:08 -05003805
3806void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3807 unsigned long start, unsigned long len)
3808{
3809 size_t cur;
3810 size_t offset;
3811 struct page *page;
3812 char *kaddr;
3813 char *src = (char *)srcv;
3814 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3815 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3816
3817 WARN_ON(start > eb->len);
3818 WARN_ON(start + len > eb->start + eb->len);
3819
3820 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3821
Chris Masond3977122009-01-05 21:25:51 -05003822 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003823 page = extent_buffer_page(eb, i);
3824 WARN_ON(!PageUptodate(page));
3825
3826 cur = min(len, PAGE_CACHE_SIZE - offset);
3827 kaddr = kmap_atomic(page, KM_USER1);
3828 memcpy(kaddr + offset, src, cur);
3829 kunmap_atomic(kaddr, KM_USER1);
3830
3831 src += cur;
3832 len -= cur;
3833 offset = 0;
3834 i++;
3835 }
3836}
Chris Masond1310b22008-01-24 16:13:08 -05003837
3838void memset_extent_buffer(struct extent_buffer *eb, char c,
3839 unsigned long start, unsigned long len)
3840{
3841 size_t cur;
3842 size_t offset;
3843 struct page *page;
3844 char *kaddr;
3845 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3846 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3847
3848 WARN_ON(start > eb->len);
3849 WARN_ON(start + len > eb->start + eb->len);
3850
3851 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3852
Chris Masond3977122009-01-05 21:25:51 -05003853 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003854 page = extent_buffer_page(eb, i);
3855 WARN_ON(!PageUptodate(page));
3856
3857 cur = min(len, PAGE_CACHE_SIZE - offset);
3858 kaddr = kmap_atomic(page, KM_USER0);
3859 memset(kaddr + offset, c, cur);
3860 kunmap_atomic(kaddr, KM_USER0);
3861
3862 len -= cur;
3863 offset = 0;
3864 i++;
3865 }
3866}
Chris Masond1310b22008-01-24 16:13:08 -05003867
3868void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3869 unsigned long dst_offset, unsigned long src_offset,
3870 unsigned long len)
3871{
3872 u64 dst_len = dst->len;
3873 size_t cur;
3874 size_t offset;
3875 struct page *page;
3876 char *kaddr;
3877 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3878 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3879
3880 WARN_ON(src->len != dst_len);
3881
3882 offset = (start_offset + dst_offset) &
3883 ((unsigned long)PAGE_CACHE_SIZE - 1);
3884
Chris Masond3977122009-01-05 21:25:51 -05003885 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003886 page = extent_buffer_page(dst, i);
3887 WARN_ON(!PageUptodate(page));
3888
3889 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3890
3891 kaddr = kmap_atomic(page, KM_USER0);
3892 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3893 kunmap_atomic(kaddr, KM_USER0);
3894
3895 src_offset += cur;
3896 len -= cur;
3897 offset = 0;
3898 i++;
3899 }
3900}
Chris Masond1310b22008-01-24 16:13:08 -05003901
3902static void move_pages(struct page *dst_page, struct page *src_page,
3903 unsigned long dst_off, unsigned long src_off,
3904 unsigned long len)
3905{
3906 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3907 if (dst_page == src_page) {
3908 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3909 } else {
3910 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3911 char *p = dst_kaddr + dst_off + len;
3912 char *s = src_kaddr + src_off + len;
3913
3914 while (len--)
3915 *--p = *--s;
3916
3917 kunmap_atomic(src_kaddr, KM_USER1);
3918 }
3919 kunmap_atomic(dst_kaddr, KM_USER0);
3920}
3921
Sergei Trofimovich33872062011-04-11 21:52:52 +00003922static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3923{
3924 unsigned long distance = (src > dst) ? src - dst : dst - src;
3925 return distance < len;
3926}
3927
Chris Masond1310b22008-01-24 16:13:08 -05003928static void copy_pages(struct page *dst_page, struct page *src_page,
3929 unsigned long dst_off, unsigned long src_off,
3930 unsigned long len)
3931{
3932 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3933 char *src_kaddr;
3934
Sergei Trofimovich33872062011-04-11 21:52:52 +00003935 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003936 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003937 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003938 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003939 BUG_ON(areas_overlap(src_off, dst_off, len));
3940 }
Chris Masond1310b22008-01-24 16:13:08 -05003941
3942 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3943 kunmap_atomic(dst_kaddr, KM_USER0);
3944 if (dst_page != src_page)
3945 kunmap_atomic(src_kaddr, KM_USER1);
3946}
3947
3948void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3949 unsigned long src_offset, unsigned long len)
3950{
3951 size_t cur;
3952 size_t dst_off_in_page;
3953 size_t src_off_in_page;
3954 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3955 unsigned long dst_i;
3956 unsigned long src_i;
3957
3958 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003959 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3960 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003961 BUG_ON(1);
3962 }
3963 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003964 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3965 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003966 BUG_ON(1);
3967 }
3968
Chris Masond3977122009-01-05 21:25:51 -05003969 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003970 dst_off_in_page = (start_offset + dst_offset) &
3971 ((unsigned long)PAGE_CACHE_SIZE - 1);
3972 src_off_in_page = (start_offset + src_offset) &
3973 ((unsigned long)PAGE_CACHE_SIZE - 1);
3974
3975 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3976 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3977
3978 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3979 src_off_in_page));
3980 cur = min_t(unsigned long, cur,
3981 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3982
3983 copy_pages(extent_buffer_page(dst, dst_i),
3984 extent_buffer_page(dst, src_i),
3985 dst_off_in_page, src_off_in_page, cur);
3986
3987 src_offset += cur;
3988 dst_offset += cur;
3989 len -= cur;
3990 }
3991}
Chris Masond1310b22008-01-24 16:13:08 -05003992
3993void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3994 unsigned long src_offset, unsigned long len)
3995{
3996 size_t cur;
3997 size_t dst_off_in_page;
3998 size_t src_off_in_page;
3999 unsigned long dst_end = dst_offset + len - 1;
4000 unsigned long src_end = src_offset + len - 1;
4001 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4002 unsigned long dst_i;
4003 unsigned long src_i;
4004
4005 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004006 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4007 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004008 BUG_ON(1);
4009 }
4010 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004011 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4012 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004013 BUG_ON(1);
4014 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004015 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004016 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4017 return;
4018 }
Chris Masond3977122009-01-05 21:25:51 -05004019 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004020 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4021 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4022
4023 dst_off_in_page = (start_offset + dst_end) &
4024 ((unsigned long)PAGE_CACHE_SIZE - 1);
4025 src_off_in_page = (start_offset + src_end) &
4026 ((unsigned long)PAGE_CACHE_SIZE - 1);
4027
4028 cur = min_t(unsigned long, len, src_off_in_page + 1);
4029 cur = min(cur, dst_off_in_page + 1);
4030 move_pages(extent_buffer_page(dst, dst_i),
4031 extent_buffer_page(dst, src_i),
4032 dst_off_in_page - cur + 1,
4033 src_off_in_page - cur + 1, cur);
4034
4035 dst_end -= cur;
4036 src_end -= cur;
4037 len -= cur;
4038 }
4039}
Chris Mason6af118ce2008-07-22 11:18:07 -04004040
Miao Xie19fe0a82010-10-26 20:57:29 -04004041static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4042{
4043 struct extent_buffer *eb =
4044 container_of(head, struct extent_buffer, rcu_head);
4045
4046 btrfs_release_extent_buffer(eb);
4047}
4048
Chris Mason6af118ce2008-07-22 11:18:07 -04004049int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4050{
4051 u64 start = page_offset(page);
4052 struct extent_buffer *eb;
4053 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004054
4055 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004056 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004057 if (!eb) {
4058 spin_unlock(&tree->buffer_lock);
4059 return ret;
4060 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004061
Chris Masonb9473432009-03-13 11:00:37 -04004062 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4063 ret = 0;
4064 goto out;
4065 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004066
Miao Xie19fe0a82010-10-26 20:57:29 -04004067 /*
4068 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4069 * Or go back.
4070 */
4071 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4072 ret = 0;
4073 goto out;
4074 }
4075
4076 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004077out:
4078 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004079
4080 /* at this point we can safely release the extent buffer */
4081 if (atomic_read(&eb->refs) == 0)
4082 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004083 return ret;
4084}