blob: 5e76a474cb7edb75cfe178fdb508d6f0421a7ab1 [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 Mason6af118c2008-07-22 11:18:07 -0400111 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500113}
Chris Masond1310b22008-01-24 16:13:08 -0500114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
117 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500118#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
Chris Masond1310b22008-01-24 16:13:08 -0500137
Chris Mason4845e442010-05-25 20:56:50 -0400138void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500143#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400144 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Mason70dec802008-01-29 09:59:12 -0500146 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 kmem_cache_free(extent_state_cache, state);
153 }
154}
Chris Masond1310b22008-01-24 16:13:08 -0500155
156static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
158{
Chris Masond3977122009-01-05 21:25:51 -0500159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500161 struct tree_entry *entry;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500164 parent = *p;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
166
167 if (offset < entry->start)
168 p = &(*p)->rb_left;
169 else if (offset > entry->end)
170 p = &(*p)->rb_right;
171 else
172 return parent;
173 }
174
175 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
178 return NULL;
179}
180
Chris Mason80ea96b2008-02-01 14:51:59 -0500181static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
184{
Chris Mason80ea96b2008-02-01 14:51:59 -0500185 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500186 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
191
Chris Masond3977122009-01-05 21:25:51 -0500192 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500193 entry = rb_entry(n, struct tree_entry, rb_node);
194 prev = n;
195 prev_entry = entry;
196
197 if (offset < entry->start)
198 n = n->rb_left;
199 else if (offset > entry->end)
200 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500201 else
Chris Masond1310b22008-01-24 16:13:08 -0500202 return n;
203 }
204
205 if (prev_ret) {
206 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500207 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
210 }
211 *prev_ret = prev;
212 prev = orig_prev;
213 }
214
215 if (next_ret) {
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *next_ret = prev;
222 }
223 return NULL;
224}
225
Chris Mason80ea96b2008-02-01 14:51:59 -0500226static inline struct rb_node *tree_search(struct extent_io_tree *tree,
227 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500228{
Chris Mason70dec802008-01-29 09:59:12 -0500229 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500230 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500231
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500233 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500234 return prev;
235 return ret;
236}
237
Josef Bacik9ed74f22009-09-11 16:12:44 -0400238static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
239 struct extent_state *other)
240{
241 if (tree->ops && tree->ops->merge_extent_hook)
242 tree->ops->merge_extent_hook(tree->mapping->host, new,
243 other);
244}
245
Chris Masond1310b22008-01-24 16:13:08 -0500246/*
247 * utility function to look for merge candidates inside a given range.
248 * Any extents with matching state are merged together into a single
249 * extent in the tree. Extents with EXTENT_IO in their state field
250 * are not merged because the end_io handlers need to be able to do
251 * operations on them without sleeping (or doing allocations/splits).
252 *
253 * This should be called with the tree lock held.
254 */
255static int merge_state(struct extent_io_tree *tree,
256 struct extent_state *state)
257{
258 struct extent_state *other;
259 struct rb_node *other_node;
260
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400261 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500262 return 0;
263
264 other_node = rb_prev(&state->rb_node);
265 if (other_node) {
266 other = rb_entry(other_node, struct extent_state, rb_node);
267 if (other->end == state->start - 1 &&
268 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400269 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500270 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500271 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500272 rb_erase(&other->rb_node, &tree->state);
273 free_extent_state(other);
274 }
275 }
276 other_node = rb_next(&state->rb_node);
277 if (other_node) {
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->start == state->end + 1 &&
280 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400281 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500282 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500283 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500284 rb_erase(&state->rb_node, &tree->state);
285 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400286 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500287 }
288 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289
Chris Masond1310b22008-01-24 16:13:08 -0500290 return 0;
291}
292
Josef Bacik9ed74f22009-09-11 16:12:44 -0400293static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400294 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500295{
296 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400297 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400298 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500299 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400300
301 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500302}
303
304static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
Chris Masond1310b22008-01-24 16:13:08 -0500311/*
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
314 *
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
317 *
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
320 */
321static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400323 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500324{
325 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400326 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400327 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500328
329 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500330 printk(KERN_ERR "btrfs end < start %llu %llu\n",
331 (unsigned long long)end,
332 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500333 WARN_ON(1);
334 }
Chris Masond1310b22008-01-24 16:13:08 -0500335 state->start = start;
336 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400337 ret = set_state_cb(tree, state, bits);
338 if (ret)
339 return ret;
340
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400341 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400342 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400343 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500344 node = tree_insert(&tree->state, end, &state->rb_node);
345 if (node) {
346 struct extent_state *found;
347 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500348 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found->start,
350 (unsigned long long)found->end,
351 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500352 free_extent_state(state);
353 return -EEXIST;
354 }
Chris Mason70dec802008-01-29 09:59:12 -0500355 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500356 merge_state(tree, state);
357 return 0;
358}
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
361 u64 split)
362{
363 if (tree->ops && tree->ops->split_extent_hook)
364 return tree->ops->split_extent_hook(tree->mapping->host,
365 orig, split);
366 return 0;
367}
368
Chris Masond1310b22008-01-24 16:13:08 -0500369/*
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
373 *
374 * Before calling,
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
379 *
380 * The tree locks are not taken by this function. They need to be held
381 * by the caller.
382 */
383static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
385{
386 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387
388 split_cb(tree, orig, split);
389
Chris Masond1310b22008-01-24 16:13:08 -0500390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
393 orig->start = split;
394
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
396 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500397 free_extent_state(prealloc);
398 return -EEXIST;
399 }
Chris Mason70dec802008-01-29 09:59:12 -0500400 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500401 return 0;
402}
403
404/*
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
408 *
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
411 */
412static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 struct extent_state *state,
414 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500415{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400416 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400417 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500418
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400419 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500420 u64 range = state->end - state->start + 1;
421 WARN_ON(range > tree->dirty_bytes);
422 tree->dirty_bytes -= range;
423 }
Chris Mason291d6732008-01-29 15:55:23 -0500424 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400425 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500426 if (wake)
427 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400428 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500429 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500430 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500431 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500432 free_extent_state(state);
433 } else {
434 WARN_ON(1);
435 }
436 } else {
437 merge_state(tree, state);
438 }
439 return ret;
440}
441
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
Chris Masond1310b22008-01-24 16:13:08 -0500693/*
Chris Mason1edbb732009-09-02 13:24:36 -0400694 * set some bits on a range in the tree. This may require allocations or
695 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500696 *
Chris Mason1edbb732009-09-02 13:24:36 -0400697 * If any of the exclusive bits are set, this will fail with -EEXIST if some
698 * part of the range already has the desired bits set. The start of the
699 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500700 *
Chris Mason1edbb732009-09-02 13:24:36 -0400701 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500702 */
Chris Mason1edbb732009-09-02 13:24:36 -0400703
Chris Mason4845e442010-05-25 20:56:50 -0400704int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
705 int bits, int exclusive_bits, u64 *failed_start,
706 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500707{
708 struct extent_state *state;
709 struct extent_state *prealloc = NULL;
710 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500711 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500712 u64 last_start;
713 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400714
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400715 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500716again:
717 if (!prealloc && (mask & __GFP_WAIT)) {
718 prealloc = alloc_extent_state(mask);
719 if (!prealloc)
720 return -ENOMEM;
721 }
722
Chris Masoncad321a2008-12-17 14:51:42 -0500723 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400724 if (cached_state && *cached_state) {
725 state = *cached_state;
726 if (state->start == start && state->tree) {
727 node = &state->rb_node;
728 goto hit_next;
729 }
730 }
Chris Masond1310b22008-01-24 16:13:08 -0500731 /*
732 * this search will find all the extents that end after
733 * our range starts.
734 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500735 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500736 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400737 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500738 prealloc = NULL;
739 BUG_ON(err == -EEXIST);
740 goto out;
741 }
Chris Masond1310b22008-01-24 16:13:08 -0500742 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400743hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500744 last_start = state->start;
745 last_end = state->end;
746
747 /*
748 * | ---- desired range ---- |
749 * | state |
750 *
751 * Just lock what we found and keep going
752 */
753 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400754 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400755 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500756 *failed_start = state->start;
757 err = -EEXIST;
758 goto out;
759 }
Chris Mason42daec22009-09-23 19:51:09 -0400760
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400761 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400762 if (err)
763 goto out;
764
Chris Mason2c64c532009-09-02 15:04:12 -0400765 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500766 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400767 if (last_end == (u64)-1)
768 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400769
Yan Zheng5c939df2009-05-27 09:16:03 -0400770 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400771 if (start < end && prealloc && !need_resched()) {
772 next_node = rb_next(node);
773 if (next_node) {
774 state = rb_entry(next_node, struct extent_state,
775 rb_node);
776 if (state->start == start)
777 goto hit_next;
778 }
779 }
Chris Masond1310b22008-01-24 16:13:08 -0500780 goto search_again;
781 }
782
783 /*
784 * | ---- desired range ---- |
785 * | state |
786 * or
787 * | ------------- state -------------- |
788 *
789 * We need to split the extent we found, and may flip bits on
790 * second half.
791 *
792 * If the extent we found extends past our
793 * range, we just split and search again. It'll get split
794 * again the next time though.
795 *
796 * If the extent we found is inside our range, we set the
797 * desired bit on it.
798 */
799 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400800 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500801 *failed_start = start;
802 err = -EEXIST;
803 goto out;
804 }
805 err = split_state(tree, state, prealloc, start);
806 BUG_ON(err == -EEXIST);
807 prealloc = NULL;
808 if (err)
809 goto out;
810 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400811 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400812 if (err)
813 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400814 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500815 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400816 if (last_end == (u64)-1)
817 goto out;
818 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500819 }
820 goto search_again;
821 }
822 /*
823 * | ---- desired range ---- |
824 * | state | or | state |
825 *
826 * There's a hole, we need to insert something in it and
827 * ignore the extent we found.
828 */
829 if (state->start > start) {
830 u64 this_end;
831 if (end < last_start)
832 this_end = end;
833 else
Chris Masond3977122009-01-05 21:25:51 -0500834 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500835 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400836 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400837 BUG_ON(err == -EEXIST);
838 if (err) {
839 prealloc = NULL;
840 goto out;
841 }
Chris Mason2c64c532009-09-02 15:04:12 -0400842 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500843 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500844 start = this_end + 1;
845 goto search_again;
846 }
847 /*
848 * | ---- desired range ---- |
849 * | state |
850 * We need to split the extent, and set the bit
851 * on the first half
852 */
853 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400854 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500855 *failed_start = start;
856 err = -EEXIST;
857 goto out;
858 }
859 err = split_state(tree, state, prealloc, end + 1);
860 BUG_ON(err == -EEXIST);
861
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400862 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400863 if (err) {
864 prealloc = NULL;
865 goto out;
866 }
Chris Mason2c64c532009-09-02 15:04:12 -0400867 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500868 merge_state(tree, prealloc);
869 prealloc = NULL;
870 goto out;
871 }
872
873 goto search_again;
874
875out:
Chris Masoncad321a2008-12-17 14:51:42 -0500876 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500877 if (prealloc)
878 free_extent_state(prealloc);
879
880 return err;
881
882search_again:
883 if (start > end)
884 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500885 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500886 if (mask & __GFP_WAIT)
887 cond_resched();
888 goto again;
889}
Chris Masond1310b22008-01-24 16:13:08 -0500890
891/* wrappers around set/clear extent bit */
892int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
893 gfp_t mask)
894{
895 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400896 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500897}
Chris Masond1310b22008-01-24 16:13:08 -0500898
899int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
900 int bits, gfp_t mask)
901{
902 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400903 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500904}
Chris Masond1310b22008-01-24 16:13:08 -0500905
906int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
907 int bits, gfp_t mask)
908{
Chris Mason2c64c532009-09-02 15:04:12 -0400909 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500910}
Chris Masond1310b22008-01-24 16:13:08 -0500911
912int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000913 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500914{
915 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400916 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000917 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500918}
Chris Masond1310b22008-01-24 16:13:08 -0500919
920int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
921 gfp_t mask)
922{
923 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400924 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400925 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500926}
Chris Masond1310b22008-01-24 16:13:08 -0500927
928int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
929 gfp_t mask)
930{
931 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400932 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500933}
Chris Masond1310b22008-01-24 16:13:08 -0500934
Christoph Hellwigb2950862008-12-02 09:54:17 -0500935static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500936 gfp_t mask)
937{
Chris Mason2c64c532009-09-02 15:04:12 -0400938 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
939 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500940}
Chris Masond1310b22008-01-24 16:13:08 -0500941
942int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
943 gfp_t mask)
944{
945 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400946 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500947}
Chris Masond1310b22008-01-24 16:13:08 -0500948
Chris Masond3977122009-01-05 21:25:51 -0500949static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000950 u64 end, struct extent_state **cached_state,
951 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500952{
Chris Mason2c64c532009-09-02 15:04:12 -0400953 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000954 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500955}
Chris Masond1310b22008-01-24 16:13:08 -0500956
Chris Masond1310b22008-01-24 16:13:08 -0500957int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
958{
959 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
960}
Chris Masond1310b22008-01-24 16:13:08 -0500961
Chris Masond352ac62008-09-29 15:18:18 -0400962/*
963 * either insert or lock state struct between start and end use mask to tell
964 * us if waiting is desired.
965 */
Chris Mason1edbb732009-09-02 13:24:36 -0400966int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400967 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500968{
969 int err;
970 u64 failed_start;
971 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400972 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400973 EXTENT_LOCKED, &failed_start,
974 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500975 if (err == -EEXIST && (mask & __GFP_WAIT)) {
976 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
977 start = failed_start;
978 } else {
979 break;
980 }
981 WARN_ON(start > end);
982 }
983 return err;
984}
Chris Masond1310b22008-01-24 16:13:08 -0500985
Chris Mason1edbb732009-09-02 13:24:36 -0400986int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
987{
Chris Mason2c64c532009-09-02 15:04:12 -0400988 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400989}
990
Josef Bacik25179202008-10-29 14:49:05 -0400991int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
992 gfp_t mask)
993{
994 int err;
995 u64 failed_start;
996
Chris Mason2c64c532009-09-02 15:04:12 -0400997 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
998 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400999 if (err == -EEXIST) {
1000 if (failed_start > start)
1001 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001002 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001003 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001004 }
Josef Bacik25179202008-10-29 14:49:05 -04001005 return 1;
1006}
Josef Bacik25179202008-10-29 14:49:05 -04001007
Chris Mason2c64c532009-09-02 15:04:12 -04001008int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1009 struct extent_state **cached, gfp_t mask)
1010{
1011 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1012 mask);
1013}
1014
Chris Masond1310b22008-01-24 16:13:08 -05001015int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1016 gfp_t mask)
1017{
Chris Mason2c64c532009-09-02 15:04:12 -04001018 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1019 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001020}
Chris Masond1310b22008-01-24 16:13:08 -05001021
1022/*
1023 * helper function to set pages and extents in the tree dirty
1024 */
1025int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1026{
1027 unsigned long index = start >> PAGE_CACHE_SHIFT;
1028 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1029 struct page *page;
1030
1031 while (index <= end_index) {
1032 page = find_get_page(tree->mapping, index);
1033 BUG_ON(!page);
1034 __set_page_dirty_nobuffers(page);
1035 page_cache_release(page);
1036 index++;
1037 }
Chris Masond1310b22008-01-24 16:13:08 -05001038 return 0;
1039}
Chris Masond1310b22008-01-24 16:13:08 -05001040
1041/*
1042 * helper function to set both pages and extents in the tree writeback
1043 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001044static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001045{
1046 unsigned long index = start >> PAGE_CACHE_SHIFT;
1047 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1048 struct page *page;
1049
1050 while (index <= end_index) {
1051 page = find_get_page(tree->mapping, index);
1052 BUG_ON(!page);
1053 set_page_writeback(page);
1054 page_cache_release(page);
1055 index++;
1056 }
Chris Masond1310b22008-01-24 16:13:08 -05001057 return 0;
1058}
Chris Masond1310b22008-01-24 16:13:08 -05001059
Chris Masond352ac62008-09-29 15:18:18 -04001060/*
1061 * find the first offset in the io tree with 'bits' set. zero is
1062 * returned if we find something, and *start_ret and *end_ret are
1063 * set to reflect the state struct that was found.
1064 *
1065 * If nothing was found, 1 is returned, < 0 on error
1066 */
Chris Masond1310b22008-01-24 16:13:08 -05001067int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1068 u64 *start_ret, u64 *end_ret, int bits)
1069{
1070 struct rb_node *node;
1071 struct extent_state *state;
1072 int ret = 1;
1073
Chris Masoncad321a2008-12-17 14:51:42 -05001074 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001075 /*
1076 * this search will find all the extents that end after
1077 * our range starts.
1078 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001079 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001080 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001081 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001082
Chris Masond3977122009-01-05 21:25:51 -05001083 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001084 state = rb_entry(node, struct extent_state, rb_node);
1085 if (state->end >= start && (state->state & bits)) {
1086 *start_ret = state->start;
1087 *end_ret = state->end;
1088 ret = 0;
1089 break;
1090 }
1091 node = rb_next(node);
1092 if (!node)
1093 break;
1094 }
1095out:
Chris Masoncad321a2008-12-17 14:51:42 -05001096 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001097 return ret;
1098}
Chris Masond1310b22008-01-24 16:13:08 -05001099
Chris Masond352ac62008-09-29 15:18:18 -04001100/* find the first state struct with 'bits' set after 'start', and
1101 * return it. tree->lock must be held. NULL will returned if
1102 * nothing was found after 'start'
1103 */
Chris Masond7fc6402008-02-18 12:12:38 -05001104struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1105 u64 start, int bits)
1106{
1107 struct rb_node *node;
1108 struct extent_state *state;
1109
1110 /*
1111 * this search will find all the extents that end after
1112 * our range starts.
1113 */
1114 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001115 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001116 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001117
Chris Masond3977122009-01-05 21:25:51 -05001118 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001119 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001120 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001121 return state;
Chris Masond3977122009-01-05 21:25:51 -05001122
Chris Masond7fc6402008-02-18 12:12:38 -05001123 node = rb_next(node);
1124 if (!node)
1125 break;
1126 }
1127out:
1128 return NULL;
1129}
Chris Masond7fc6402008-02-18 12:12:38 -05001130
Chris Masond352ac62008-09-29 15:18:18 -04001131/*
1132 * find a contiguous range of bytes in the file marked as delalloc, not
1133 * more than 'max_bytes'. start and end are used to return the range,
1134 *
1135 * 1 is returned if we find something, 0 if nothing was in the tree
1136 */
Chris Masonc8b97812008-10-29 14:49:59 -04001137static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001138 u64 *start, u64 *end, u64 max_bytes,
1139 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001140{
1141 struct rb_node *node;
1142 struct extent_state *state;
1143 u64 cur_start = *start;
1144 u64 found = 0;
1145 u64 total_bytes = 0;
1146
Chris Masoncad321a2008-12-17 14:51:42 -05001147 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001148
Chris Masond1310b22008-01-24 16:13:08 -05001149 /*
1150 * this search will find all the extents that end after
1151 * our range starts.
1152 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001153 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001154 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001155 if (!found)
1156 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001157 goto out;
1158 }
1159
Chris Masond3977122009-01-05 21:25:51 -05001160 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001161 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001162 if (found && (state->start != cur_start ||
1163 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001164 goto out;
1165 }
1166 if (!(state->state & EXTENT_DELALLOC)) {
1167 if (!found)
1168 *end = state->end;
1169 goto out;
1170 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001171 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001172 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001173 *cached_state = state;
1174 atomic_inc(&state->refs);
1175 }
Chris Masond1310b22008-01-24 16:13:08 -05001176 found++;
1177 *end = state->end;
1178 cur_start = state->end + 1;
1179 node = rb_next(node);
1180 if (!node)
1181 break;
1182 total_bytes += state->end - state->start + 1;
1183 if (total_bytes >= max_bytes)
1184 break;
1185 }
1186out:
Chris Masoncad321a2008-12-17 14:51:42 -05001187 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001188 return found;
1189}
1190
Chris Masonc8b97812008-10-29 14:49:59 -04001191static noinline int __unlock_for_delalloc(struct inode *inode,
1192 struct page *locked_page,
1193 u64 start, u64 end)
1194{
1195 int ret;
1196 struct page *pages[16];
1197 unsigned long index = start >> PAGE_CACHE_SHIFT;
1198 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1199 unsigned long nr_pages = end_index - index + 1;
1200 int i;
1201
1202 if (index == locked_page->index && end_index == index)
1203 return 0;
1204
Chris Masond3977122009-01-05 21:25:51 -05001205 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001206 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001207 min_t(unsigned long, nr_pages,
1208 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001209 for (i = 0; i < ret; i++) {
1210 if (pages[i] != locked_page)
1211 unlock_page(pages[i]);
1212 page_cache_release(pages[i]);
1213 }
1214 nr_pages -= ret;
1215 index += ret;
1216 cond_resched();
1217 }
1218 return 0;
1219}
1220
1221static noinline int lock_delalloc_pages(struct inode *inode,
1222 struct page *locked_page,
1223 u64 delalloc_start,
1224 u64 delalloc_end)
1225{
1226 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1227 unsigned long start_index = index;
1228 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1229 unsigned long pages_locked = 0;
1230 struct page *pages[16];
1231 unsigned long nrpages;
1232 int ret;
1233 int i;
1234
1235 /* the caller is responsible for locking the start index */
1236 if (index == locked_page->index && index == end_index)
1237 return 0;
1238
1239 /* skip the page at the start index */
1240 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001241 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001242 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001243 min_t(unsigned long,
1244 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001245 if (ret == 0) {
1246 ret = -EAGAIN;
1247 goto done;
1248 }
1249 /* now we have an array of pages, lock them all */
1250 for (i = 0; i < ret; i++) {
1251 /*
1252 * the caller is taking responsibility for
1253 * locked_page
1254 */
Chris Mason771ed682008-11-06 22:02:51 -05001255 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001256 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001257 if (!PageDirty(pages[i]) ||
1258 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001259 ret = -EAGAIN;
1260 unlock_page(pages[i]);
1261 page_cache_release(pages[i]);
1262 goto done;
1263 }
1264 }
Chris Masonc8b97812008-10-29 14:49:59 -04001265 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001266 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001267 }
Chris Masonc8b97812008-10-29 14:49:59 -04001268 nrpages -= ret;
1269 index += ret;
1270 cond_resched();
1271 }
1272 ret = 0;
1273done:
1274 if (ret && pages_locked) {
1275 __unlock_for_delalloc(inode, locked_page,
1276 delalloc_start,
1277 ((u64)(start_index + pages_locked - 1)) <<
1278 PAGE_CACHE_SHIFT);
1279 }
1280 return ret;
1281}
1282
1283/*
1284 * find a contiguous range of bytes in the file marked as delalloc, not
1285 * more than 'max_bytes'. start and end are used to return the range,
1286 *
1287 * 1 is returned if we find something, 0 if nothing was in the tree
1288 */
1289static noinline u64 find_lock_delalloc_range(struct inode *inode,
1290 struct extent_io_tree *tree,
1291 struct page *locked_page,
1292 u64 *start, u64 *end,
1293 u64 max_bytes)
1294{
1295 u64 delalloc_start;
1296 u64 delalloc_end;
1297 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001298 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001299 int ret;
1300 int loops = 0;
1301
1302again:
1303 /* step one, find a bunch of delalloc bytes starting at start */
1304 delalloc_start = *start;
1305 delalloc_end = 0;
1306 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001307 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001308 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001309 *start = delalloc_start;
1310 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001311 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001312 return found;
1313 }
1314
1315 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001316 * start comes from the offset of locked_page. We have to lock
1317 * pages in order, so we can't process delalloc bytes before
1318 * locked_page
1319 */
Chris Masond3977122009-01-05 21:25:51 -05001320 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001321 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001322
1323 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001324 * make sure to limit the number of pages we try to lock down
1325 * if we're looping.
1326 */
Chris Masond3977122009-01-05 21:25:51 -05001327 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001328 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001329
Chris Masonc8b97812008-10-29 14:49:59 -04001330 /* step two, lock all the pages after the page that has start */
1331 ret = lock_delalloc_pages(inode, locked_page,
1332 delalloc_start, delalloc_end);
1333 if (ret == -EAGAIN) {
1334 /* some of the pages are gone, lets avoid looping by
1335 * shortening the size of the delalloc range we're searching
1336 */
Chris Mason9655d292009-09-02 15:22:30 -04001337 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001338 if (!loops) {
1339 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1340 max_bytes = PAGE_CACHE_SIZE - offset;
1341 loops = 1;
1342 goto again;
1343 } else {
1344 found = 0;
1345 goto out_failed;
1346 }
1347 }
1348 BUG_ON(ret);
1349
1350 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001351 lock_extent_bits(tree, delalloc_start, delalloc_end,
1352 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001353
1354 /* then test to make sure it is all still delalloc */
1355 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001356 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001357 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001358 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1359 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001360 __unlock_for_delalloc(inode, locked_page,
1361 delalloc_start, delalloc_end);
1362 cond_resched();
1363 goto again;
1364 }
Chris Mason9655d292009-09-02 15:22:30 -04001365 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001366 *start = delalloc_start;
1367 *end = delalloc_end;
1368out_failed:
1369 return found;
1370}
1371
1372int extent_clear_unlock_delalloc(struct inode *inode,
1373 struct extent_io_tree *tree,
1374 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001375 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001376{
1377 int ret;
1378 struct page *pages[16];
1379 unsigned long index = start >> PAGE_CACHE_SHIFT;
1380 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1381 unsigned long nr_pages = end_index - index + 1;
1382 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001383 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001384
Chris Masona791e352009-10-08 11:27:10 -04001385 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001386 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001387 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001388 clear_bits |= EXTENT_DIRTY;
1389
Chris Masona791e352009-10-08 11:27:10 -04001390 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001391 clear_bits |= EXTENT_DELALLOC;
1392
Chris Mason2c64c532009-09-02 15:04:12 -04001393 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001394 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1395 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1396 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001397 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001398
Chris Masond3977122009-01-05 21:25:51 -05001399 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001400 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001401 min_t(unsigned long,
1402 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001403 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001404
Chris Masona791e352009-10-08 11:27:10 -04001405 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001406 SetPagePrivate2(pages[i]);
1407
Chris Masonc8b97812008-10-29 14:49:59 -04001408 if (pages[i] == locked_page) {
1409 page_cache_release(pages[i]);
1410 continue;
1411 }
Chris Masona791e352009-10-08 11:27:10 -04001412 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001413 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001414 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001415 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001416 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001417 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001418 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001419 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001420 page_cache_release(pages[i]);
1421 }
1422 nr_pages -= ret;
1423 index += ret;
1424 cond_resched();
1425 }
1426 return 0;
1427}
Chris Masonc8b97812008-10-29 14:49:59 -04001428
Chris Masond352ac62008-09-29 15:18:18 -04001429/*
1430 * count the number of bytes in the tree that have a given bit(s)
1431 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1432 * cached. The total number found is returned.
1433 */
Chris Masond1310b22008-01-24 16:13:08 -05001434u64 count_range_bits(struct extent_io_tree *tree,
1435 u64 *start, u64 search_end, u64 max_bytes,
1436 unsigned long bits)
1437{
1438 struct rb_node *node;
1439 struct extent_state *state;
1440 u64 cur_start = *start;
1441 u64 total_bytes = 0;
1442 int found = 0;
1443
1444 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001445 WARN_ON(1);
1446 return 0;
1447 }
1448
Chris Masoncad321a2008-12-17 14:51:42 -05001449 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001450 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1451 total_bytes = tree->dirty_bytes;
1452 goto out;
1453 }
1454 /*
1455 * this search will find all the extents that end after
1456 * our range starts.
1457 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001458 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001459 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001460 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001461
Chris Masond3977122009-01-05 21:25:51 -05001462 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001463 state = rb_entry(node, struct extent_state, rb_node);
1464 if (state->start > search_end)
1465 break;
1466 if (state->end >= cur_start && (state->state & bits)) {
1467 total_bytes += min(search_end, state->end) + 1 -
1468 max(cur_start, state->start);
1469 if (total_bytes >= max_bytes)
1470 break;
1471 if (!found) {
1472 *start = state->start;
1473 found = 1;
1474 }
1475 }
1476 node = rb_next(node);
1477 if (!node)
1478 break;
1479 }
1480out:
Chris Masoncad321a2008-12-17 14:51:42 -05001481 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001482 return total_bytes;
1483}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001484
Chris Masond352ac62008-09-29 15:18:18 -04001485/*
1486 * set the private field for a given byte offset in the tree. If there isn't
1487 * an extent_state there already, this does nothing.
1488 */
Chris Masond1310b22008-01-24 16:13:08 -05001489int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1490{
1491 struct rb_node *node;
1492 struct extent_state *state;
1493 int ret = 0;
1494
Chris Masoncad321a2008-12-17 14:51:42 -05001495 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001496 /*
1497 * this search will find all the extents that end after
1498 * our range starts.
1499 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001500 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001501 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001502 ret = -ENOENT;
1503 goto out;
1504 }
1505 state = rb_entry(node, struct extent_state, rb_node);
1506 if (state->start != start) {
1507 ret = -ENOENT;
1508 goto out;
1509 }
1510 state->private = private;
1511out:
Chris Masoncad321a2008-12-17 14:51:42 -05001512 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001513 return ret;
1514}
1515
1516int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1517{
1518 struct rb_node *node;
1519 struct extent_state *state;
1520 int ret = 0;
1521
Chris Masoncad321a2008-12-17 14:51:42 -05001522 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001523 /*
1524 * this search will find all the extents that end after
1525 * our range starts.
1526 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001527 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001528 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001529 ret = -ENOENT;
1530 goto out;
1531 }
1532 state = rb_entry(node, struct extent_state, rb_node);
1533 if (state->start != start) {
1534 ret = -ENOENT;
1535 goto out;
1536 }
1537 *private = state->private;
1538out:
Chris Masoncad321a2008-12-17 14:51:42 -05001539 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001540 return ret;
1541}
1542
1543/*
1544 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001545 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001546 * has the bits set. Otherwise, 1 is returned if any bit in the
1547 * range is found set.
1548 */
1549int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001550 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001551{
1552 struct extent_state *state = NULL;
1553 struct rb_node *node;
1554 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001555
Chris Masoncad321a2008-12-17 14:51:42 -05001556 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001557 if (cached && cached->tree && cached->start == start)
1558 node = &cached->rb_node;
1559 else
1560 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001561 while (node && start <= end) {
1562 state = rb_entry(node, struct extent_state, rb_node);
1563
1564 if (filled && state->start > start) {
1565 bitset = 0;
1566 break;
1567 }
1568
1569 if (state->start > end)
1570 break;
1571
1572 if (state->state & bits) {
1573 bitset = 1;
1574 if (!filled)
1575 break;
1576 } else if (filled) {
1577 bitset = 0;
1578 break;
1579 }
Chris Mason46562ce2009-09-23 20:23:16 -04001580
1581 if (state->end == (u64)-1)
1582 break;
1583
Chris Masond1310b22008-01-24 16:13:08 -05001584 start = state->end + 1;
1585 if (start > end)
1586 break;
1587 node = rb_next(node);
1588 if (!node) {
1589 if (filled)
1590 bitset = 0;
1591 break;
1592 }
1593 }
Chris Masoncad321a2008-12-17 14:51:42 -05001594 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001595 return bitset;
1596}
Chris Masond1310b22008-01-24 16:13:08 -05001597
1598/*
1599 * helper function to set a given page up to date if all the
1600 * extents in the tree for that page are up to date
1601 */
1602static int check_page_uptodate(struct extent_io_tree *tree,
1603 struct page *page)
1604{
1605 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1606 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001607 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001608 SetPageUptodate(page);
1609 return 0;
1610}
1611
1612/*
1613 * helper function to unlock a page if all the extents in the tree
1614 * for that page are unlocked
1615 */
1616static int check_page_locked(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_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001622 unlock_page(page);
1623 return 0;
1624}
1625
1626/*
1627 * helper function to end page writeback if all the extents
1628 * in the tree for that page are done with writeback
1629 */
1630static int check_page_writeback(struct extent_io_tree *tree,
1631 struct page *page)
1632{
Chris Mason1edbb732009-09-02 13:24:36 -04001633 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001634 return 0;
1635}
1636
1637/* lots and lots of room for performance fixes in the end_bio funcs */
1638
1639/*
1640 * after a writepage IO is done, we need to:
1641 * clear the uptodate bits on error
1642 * clear the writeback bits in the extent tree for this IO
1643 * end_page_writeback if the page has no more pending IO
1644 *
1645 * Scheduling is not allowed, so the extent state tree is expected
1646 * to have one and only one object corresponding to this IO.
1647 */
Chris Masond1310b22008-01-24 16:13:08 -05001648static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001649{
Chris Mason1259ab72008-05-12 13:39:03 -04001650 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001651 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001652 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001653 u64 start;
1654 u64 end;
1655 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001656 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001657
Chris Masond1310b22008-01-24 16:13:08 -05001658 do {
1659 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001660 tree = &BTRFS_I(page->mapping->host)->io_tree;
1661
Chris Masond1310b22008-01-24 16:13:08 -05001662 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1663 bvec->bv_offset;
1664 end = start + bvec->bv_len - 1;
1665
1666 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1667 whole_page = 1;
1668 else
1669 whole_page = 0;
1670
1671 if (--bvec >= bio->bi_io_vec)
1672 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001673 if (tree->ops && tree->ops->writepage_end_io_hook) {
1674 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001675 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001676 if (ret)
1677 uptodate = 0;
1678 }
1679
1680 if (!uptodate && tree->ops &&
1681 tree->ops->writepage_io_failed_hook) {
1682 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001683 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001684 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001685 uptodate = (err == 0);
1686 continue;
1687 }
1688 }
1689
Chris Masond1310b22008-01-24 16:13:08 -05001690 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001691 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001692 ClearPageUptodate(page);
1693 SetPageError(page);
1694 }
Chris Mason70dec802008-01-29 09:59:12 -05001695
Chris Masond1310b22008-01-24 16:13:08 -05001696 if (whole_page)
1697 end_page_writeback(page);
1698 else
1699 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001700 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001701
Chris Masond1310b22008-01-24 16:13:08 -05001702 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001703}
1704
1705/*
1706 * after a readpage IO is done, we need to:
1707 * clear the uptodate bits on error
1708 * set the uptodate bits if things worked
1709 * set the page up to date if all extents in the tree are uptodate
1710 * clear the lock bit in the extent tree
1711 * unlock the page if there are no other extents locked for it
1712 *
1713 * Scheduling is not allowed, so the extent state tree is expected
1714 * to have one and only one object corresponding to this IO.
1715 */
Chris Masond1310b22008-01-24 16:13:08 -05001716static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001717{
1718 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001719 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1720 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001721 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001722 u64 start;
1723 u64 end;
1724 int whole_page;
1725 int ret;
1726
Chris Masond20f7042008-12-08 16:58:54 -05001727 if (err)
1728 uptodate = 0;
1729
Chris Masond1310b22008-01-24 16:13:08 -05001730 do {
1731 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001732 tree = &BTRFS_I(page->mapping->host)->io_tree;
1733
Chris Masond1310b22008-01-24 16:13:08 -05001734 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1735 bvec->bv_offset;
1736 end = start + bvec->bv_len - 1;
1737
1738 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1739 whole_page = 1;
1740 else
1741 whole_page = 0;
1742
Chris Mason4125bf72010-02-03 18:18:45 +00001743 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001744 prefetchw(&bvec->bv_page->flags);
1745
1746 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001747 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001748 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001749 if (ret)
1750 uptodate = 0;
1751 }
Chris Mason7e383262008-04-09 16:28:12 -04001752 if (!uptodate && tree->ops &&
1753 tree->ops->readpage_io_failed_hook) {
1754 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001755 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001756 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001757 uptodate =
1758 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001759 if (err)
1760 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001761 continue;
1762 }
1763 }
Chris Mason70dec802008-01-29 09:59:12 -05001764
Chris Mason771ed682008-11-06 22:02:51 -05001765 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001766 set_extent_uptodate(tree, start, end,
1767 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001768 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001769 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001770
Chris Mason70dec802008-01-29 09:59:12 -05001771 if (whole_page) {
1772 if (uptodate) {
1773 SetPageUptodate(page);
1774 } else {
1775 ClearPageUptodate(page);
1776 SetPageError(page);
1777 }
Chris Masond1310b22008-01-24 16:13:08 -05001778 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001779 } else {
1780 if (uptodate) {
1781 check_page_uptodate(tree, page);
1782 } else {
1783 ClearPageUptodate(page);
1784 SetPageError(page);
1785 }
Chris Masond1310b22008-01-24 16:13:08 -05001786 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001787 }
Chris Mason4125bf72010-02-03 18:18:45 +00001788 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001789
1790 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001791}
1792
1793/*
1794 * IO done from prepare_write is pretty simple, we just unlock
1795 * the structs in the extent tree when done, and set the uptodate bits
1796 * as appropriate.
1797 */
Chris Masond1310b22008-01-24 16:13:08 -05001798static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001799{
1800 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1801 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001802 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001803 u64 start;
1804 u64 end;
1805
Chris Masond1310b22008-01-24 16:13:08 -05001806 do {
1807 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001808 tree = &BTRFS_I(page->mapping->host)->io_tree;
1809
Chris Masond1310b22008-01-24 16:13:08 -05001810 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1811 bvec->bv_offset;
1812 end = start + bvec->bv_len - 1;
1813
1814 if (--bvec >= bio->bi_io_vec)
1815 prefetchw(&bvec->bv_page->flags);
1816
1817 if (uptodate) {
1818 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1819 } else {
1820 ClearPageUptodate(page);
1821 SetPageError(page);
1822 }
1823
1824 unlock_extent(tree, start, end, GFP_ATOMIC);
1825
1826 } while (bvec >= bio->bi_io_vec);
1827
1828 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001829}
1830
Miao Xie88f794e2010-11-22 03:02:55 +00001831struct bio *
1832btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1833 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001834{
1835 struct bio *bio;
1836
1837 bio = bio_alloc(gfp_flags, nr_vecs);
1838
1839 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1840 while (!bio && (nr_vecs /= 2))
1841 bio = bio_alloc(gfp_flags, nr_vecs);
1842 }
1843
1844 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001845 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001846 bio->bi_bdev = bdev;
1847 bio->bi_sector = first_sector;
1848 }
1849 return bio;
1850}
1851
Chris Masonc8b97812008-10-29 14:49:59 -04001852static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1853 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001854{
Chris Masond1310b22008-01-24 16:13:08 -05001855 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001856 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1857 struct page *page = bvec->bv_page;
1858 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001859 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001860
1861 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001862
David Woodhouse902b22f2008-08-20 08:51:49 -04001863 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001864
1865 bio_get(bio);
1866
Chris Mason065631f2008-02-20 12:07:25 -05001867 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001868 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001869 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001870 else
1871 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001872 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1873 ret = -EOPNOTSUPP;
1874 bio_put(bio);
1875 return ret;
1876}
1877
1878static int submit_extent_page(int rw, struct extent_io_tree *tree,
1879 struct page *page, sector_t sector,
1880 size_t size, unsigned long offset,
1881 struct block_device *bdev,
1882 struct bio **bio_ret,
1883 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001884 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001885 int mirror_num,
1886 unsigned long prev_bio_flags,
1887 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001888{
1889 int ret = 0;
1890 struct bio *bio;
1891 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001892 int contig = 0;
1893 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1894 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001895 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001896
1897 if (bio_ret && *bio_ret) {
1898 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001899 if (old_compressed)
1900 contig = bio->bi_sector == sector;
1901 else
1902 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1903 sector;
1904
1905 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001906 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001907 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1908 bio_flags)) ||
1909 bio_add_page(bio, page, page_size, offset) < page_size) {
1910 ret = submit_one_bio(rw, bio, mirror_num,
1911 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001912 bio = NULL;
1913 } else {
1914 return 0;
1915 }
1916 }
Chris Masonc8b97812008-10-29 14:49:59 -04001917 if (this_compressed)
1918 nr = BIO_MAX_PAGES;
1919 else
1920 nr = bio_get_nr_vecs(bdev);
1921
Miao Xie88f794e2010-11-22 03:02:55 +00001922 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001923 if (!bio)
1924 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001925
Chris Masonc8b97812008-10-29 14:49:59 -04001926 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001927 bio->bi_end_io = end_io_func;
1928 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001929
Chris Masond3977122009-01-05 21:25:51 -05001930 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001931 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001932 else
Chris Masonc8b97812008-10-29 14:49:59 -04001933 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001934
1935 return ret;
1936}
1937
1938void set_page_extent_mapped(struct page *page)
1939{
1940 if (!PagePrivate(page)) {
1941 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001942 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04001943 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001944 }
1945}
1946
Christoph Hellwigb2950862008-12-02 09:54:17 -05001947static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001948{
1949 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1950}
1951
1952/*
1953 * basic readpage implementation. Locked extent state structs are inserted
1954 * into the tree that are removed when the IO is done (by the end_io
1955 * handlers)
1956 */
1957static int __extent_read_full_page(struct extent_io_tree *tree,
1958 struct page *page,
1959 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001960 struct bio **bio, int mirror_num,
1961 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001962{
1963 struct inode *inode = page->mapping->host;
1964 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1965 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1966 u64 end;
1967 u64 cur = start;
1968 u64 extent_offset;
1969 u64 last_byte = i_size_read(inode);
1970 u64 block_start;
1971 u64 cur_end;
1972 sector_t sector;
1973 struct extent_map *em;
1974 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001975 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05001976 int ret;
1977 int nr = 0;
1978 size_t page_offset = 0;
1979 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001980 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001981 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001982 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001983
1984 set_page_extent_mapped(page);
1985
1986 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001987 while (1) {
1988 lock_extent(tree, start, end, GFP_NOFS);
1989 ordered = btrfs_lookup_ordered_extent(inode, start);
1990 if (!ordered)
1991 break;
1992 unlock_extent(tree, start, end, GFP_NOFS);
1993 btrfs_start_ordered_extent(inode, ordered, 1);
1994 btrfs_put_ordered_extent(ordered);
1995 }
Chris Masond1310b22008-01-24 16:13:08 -05001996
Chris Masonc8b97812008-10-29 14:49:59 -04001997 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1998 char *userpage;
1999 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2000
2001 if (zero_offset) {
2002 iosize = PAGE_CACHE_SIZE - zero_offset;
2003 userpage = kmap_atomic(page, KM_USER0);
2004 memset(userpage + zero_offset, 0, iosize);
2005 flush_dcache_page(page);
2006 kunmap_atomic(userpage, KM_USER0);
2007 }
2008 }
Chris Masond1310b22008-01-24 16:13:08 -05002009 while (cur <= end) {
2010 if (cur >= last_byte) {
2011 char *userpage;
2012 iosize = PAGE_CACHE_SIZE - page_offset;
2013 userpage = kmap_atomic(page, KM_USER0);
2014 memset(userpage + page_offset, 0, iosize);
2015 flush_dcache_page(page);
2016 kunmap_atomic(userpage, KM_USER0);
2017 set_extent_uptodate(tree, cur, cur + iosize - 1,
2018 GFP_NOFS);
2019 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2020 break;
2021 }
2022 em = get_extent(inode, page, page_offset, cur,
2023 end - cur + 1, 0);
2024 if (IS_ERR(em) || !em) {
2025 SetPageError(page);
2026 unlock_extent(tree, cur, end, GFP_NOFS);
2027 break;
2028 }
Chris Masond1310b22008-01-24 16:13:08 -05002029 extent_offset = cur - em->start;
2030 BUG_ON(extent_map_end(em) <= cur);
2031 BUG_ON(end < cur);
2032
Li Zefan261507a02010-12-17 14:21:50 +08002033 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002034 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002035 extent_set_compress_type(&this_bio_flag,
2036 em->compress_type);
2037 }
Chris Masonc8b97812008-10-29 14:49:59 -04002038
Chris Masond1310b22008-01-24 16:13:08 -05002039 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2040 cur_end = min(extent_map_end(em) - 1, end);
2041 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002042 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2043 disk_io_size = em->block_len;
2044 sector = em->block_start >> 9;
2045 } else {
2046 sector = (em->block_start + extent_offset) >> 9;
2047 disk_io_size = iosize;
2048 }
Chris Masond1310b22008-01-24 16:13:08 -05002049 bdev = em->bdev;
2050 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002051 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2052 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002053 free_extent_map(em);
2054 em = NULL;
2055
2056 /* we've found a hole, just zero and go on */
2057 if (block_start == EXTENT_MAP_HOLE) {
2058 char *userpage;
2059 userpage = kmap_atomic(page, KM_USER0);
2060 memset(userpage + page_offset, 0, iosize);
2061 flush_dcache_page(page);
2062 kunmap_atomic(userpage, KM_USER0);
2063
2064 set_extent_uptodate(tree, cur, cur + iosize - 1,
2065 GFP_NOFS);
2066 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2067 cur = cur + iosize;
2068 page_offset += iosize;
2069 continue;
2070 }
2071 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002072 if (test_range_bit(tree, cur, cur_end,
2073 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002074 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002075 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2076 cur = cur + iosize;
2077 page_offset += iosize;
2078 continue;
2079 }
Chris Mason70dec802008-01-29 09:59:12 -05002080 /* we have an inline extent but it didn't get marked up
2081 * to date. Error out
2082 */
2083 if (block_start == EXTENT_MAP_INLINE) {
2084 SetPageError(page);
2085 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2086 cur = cur + iosize;
2087 page_offset += iosize;
2088 continue;
2089 }
Chris Masond1310b22008-01-24 16:13:08 -05002090
2091 ret = 0;
2092 if (tree->ops && tree->ops->readpage_io_hook) {
2093 ret = tree->ops->readpage_io_hook(page, cur,
2094 cur + iosize - 1);
2095 }
2096 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002097 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2098 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002099 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002100 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002101 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002102 end_bio_extent_readpage, mirror_num,
2103 *bio_flags,
2104 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002105 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002106 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002107 }
2108 if (ret)
2109 SetPageError(page);
2110 cur = cur + iosize;
2111 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002112 }
2113 if (!nr) {
2114 if (!PageError(page))
2115 SetPageUptodate(page);
2116 unlock_page(page);
2117 }
2118 return 0;
2119}
2120
2121int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2122 get_extent_t *get_extent)
2123{
2124 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002125 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002126 int ret;
2127
Chris Masonc8b97812008-10-29 14:49:59 -04002128 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2129 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002130 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002131 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002132 return ret;
2133}
Chris Masond1310b22008-01-24 16:13:08 -05002134
Chris Mason11c83492009-04-20 15:50:09 -04002135static noinline void update_nr_written(struct page *page,
2136 struct writeback_control *wbc,
2137 unsigned long nr_written)
2138{
2139 wbc->nr_to_write -= nr_written;
2140 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2141 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2142 page->mapping->writeback_index = page->index + nr_written;
2143}
2144
Chris Masond1310b22008-01-24 16:13:08 -05002145/*
2146 * the writepage semantics are similar to regular writepage. extent
2147 * records are inserted to lock ranges in the tree, and as dirty areas
2148 * are found, they are marked writeback. Then the lock bits are removed
2149 * and the end_io handler clears the writeback ranges
2150 */
2151static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2152 void *data)
2153{
2154 struct inode *inode = page->mapping->host;
2155 struct extent_page_data *epd = data;
2156 struct extent_io_tree *tree = epd->tree;
2157 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2158 u64 delalloc_start;
2159 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2160 u64 end;
2161 u64 cur = start;
2162 u64 extent_offset;
2163 u64 last_byte = i_size_read(inode);
2164 u64 block_start;
2165 u64 iosize;
2166 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002167 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002168 struct extent_map *em;
2169 struct block_device *bdev;
2170 int ret;
2171 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002172 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002173 size_t blocksize;
2174 loff_t i_size = i_size_read(inode);
2175 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2176 u64 nr_delalloc;
2177 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002178 int page_started;
2179 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002180 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002181 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002182
Chris Masonffbd5172009-04-20 15:50:09 -04002183 if (wbc->sync_mode == WB_SYNC_ALL)
2184 write_flags = WRITE_SYNC_PLUG;
2185 else
2186 write_flags = WRITE;
2187
Chris Masond1310b22008-01-24 16:13:08 -05002188 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002189 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002190 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002191 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002192 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002193 unlock_page(page);
2194 return 0;
2195 }
2196
2197 if (page->index == end_index) {
2198 char *userpage;
2199
Chris Masond1310b22008-01-24 16:13:08 -05002200 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002201 memset(userpage + pg_offset, 0,
2202 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002203 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002204 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002205 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002206 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002207
2208 set_page_extent_mapped(page);
2209
2210 delalloc_start = start;
2211 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002212 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002213 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002214 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002215 /*
2216 * make sure the wbc mapping index is at least updated
2217 * to this page.
2218 */
2219 update_nr_written(page, wbc, 0);
2220
Chris Masond3977122009-01-05 21:25:51 -05002221 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002222 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002223 page,
2224 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002225 &delalloc_end,
2226 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002227 if (nr_delalloc == 0) {
2228 delalloc_start = delalloc_end + 1;
2229 continue;
2230 }
2231 tree->ops->fill_delalloc(inode, page, delalloc_start,
2232 delalloc_end, &page_started,
2233 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002234 /*
2235 * delalloc_end is already one less than the total
2236 * length, so we don't subtract one from
2237 * PAGE_CACHE_SIZE
2238 */
2239 delalloc_to_write += (delalloc_end - delalloc_start +
2240 PAGE_CACHE_SIZE) >>
2241 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002242 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002243 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002244 if (wbc->nr_to_write < delalloc_to_write) {
2245 int thresh = 8192;
2246
2247 if (delalloc_to_write < thresh * 2)
2248 thresh = delalloc_to_write;
2249 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2250 thresh);
2251 }
Chris Masonc8b97812008-10-29 14:49:59 -04002252
Chris Mason771ed682008-11-06 22:02:51 -05002253 /* did the fill delalloc function already unlock and start
2254 * the IO?
2255 */
2256 if (page_started) {
2257 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002258 /*
2259 * we've unlocked the page, so we can't update
2260 * the mapping's writeback index, just update
2261 * nr_to_write.
2262 */
2263 wbc->nr_to_write -= nr_written;
2264 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002265 }
Chris Masonc8b97812008-10-29 14:49:59 -04002266 }
Chris Mason247e7432008-07-17 12:53:51 -04002267 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002268 ret = tree->ops->writepage_start_hook(page, start,
2269 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002270 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002271 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002272 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002273 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002274 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002275 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002276 }
2277 }
2278
Chris Mason11c83492009-04-20 15:50:09 -04002279 /*
2280 * we don't want to touch the inode after unlocking the page,
2281 * so we update the mapping writeback index now
2282 */
2283 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002284
Chris Masond1310b22008-01-24 16:13:08 -05002285 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002286 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002287 if (tree->ops && tree->ops->writepage_end_io_hook)
2288 tree->ops->writepage_end_io_hook(page, start,
2289 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002290 goto done;
2291 }
2292
Chris Masond1310b22008-01-24 16:13:08 -05002293 blocksize = inode->i_sb->s_blocksize;
2294
2295 while (cur <= end) {
2296 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002297 if (tree->ops && tree->ops->writepage_end_io_hook)
2298 tree->ops->writepage_end_io_hook(page, cur,
2299 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002300 break;
2301 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002302 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002303 end - cur + 1, 1);
2304 if (IS_ERR(em) || !em) {
2305 SetPageError(page);
2306 break;
2307 }
2308
2309 extent_offset = cur - em->start;
2310 BUG_ON(extent_map_end(em) <= cur);
2311 BUG_ON(end < cur);
2312 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2313 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2314 sector = (em->block_start + extent_offset) >> 9;
2315 bdev = em->bdev;
2316 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002317 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002318 free_extent_map(em);
2319 em = NULL;
2320
Chris Masonc8b97812008-10-29 14:49:59 -04002321 /*
2322 * compressed and inline extents are written through other
2323 * paths in the FS
2324 */
2325 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002326 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002327 /*
2328 * end_io notification does not happen here for
2329 * compressed extents
2330 */
2331 if (!compressed && tree->ops &&
2332 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002333 tree->ops->writepage_end_io_hook(page, cur,
2334 cur + iosize - 1,
2335 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002336 else if (compressed) {
2337 /* we don't want to end_page_writeback on
2338 * a compressed extent. this happens
2339 * elsewhere
2340 */
2341 nr++;
2342 }
2343
2344 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002345 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002346 continue;
2347 }
Chris Masond1310b22008-01-24 16:13:08 -05002348 /* leave this out until we have a page_mkwrite call */
2349 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002350 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002351 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002352 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002353 continue;
2354 }
Chris Masonc8b97812008-10-29 14:49:59 -04002355
Chris Masond1310b22008-01-24 16:13:08 -05002356 if (tree->ops && tree->ops->writepage_io_hook) {
2357 ret = tree->ops->writepage_io_hook(page, cur,
2358 cur + iosize - 1);
2359 } else {
2360 ret = 0;
2361 }
Chris Mason1259ab72008-05-12 13:39:03 -04002362 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002363 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002364 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002365 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002366
Chris Masond1310b22008-01-24 16:13:08 -05002367 set_range_writeback(tree, cur, cur + iosize - 1);
2368 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002369 printk(KERN_ERR "btrfs warning page %lu not "
2370 "writeback, cur %llu end %llu\n",
2371 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002372 (unsigned long long)end);
2373 }
2374
Chris Masonffbd5172009-04-20 15:50:09 -04002375 ret = submit_extent_page(write_flags, tree, page,
2376 sector, iosize, pg_offset,
2377 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002378 end_bio_extent_writepage,
2379 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002380 if (ret)
2381 SetPageError(page);
2382 }
2383 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002384 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002385 nr++;
2386 }
2387done:
2388 if (nr == 0) {
2389 /* make sure the mapping tag for page dirty gets cleared */
2390 set_page_writeback(page);
2391 end_page_writeback(page);
2392 }
Chris Masond1310b22008-01-24 16:13:08 -05002393 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002394
Chris Mason11c83492009-04-20 15:50:09 -04002395done_unlocked:
2396
Chris Mason2c64c532009-09-02 15:04:12 -04002397 /* drop our reference on any cached states */
2398 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002399 return 0;
2400}
2401
Chris Masond1310b22008-01-24 16:13:08 -05002402/**
Chris Mason4bef0842008-09-08 11:18:08 -04002403 * 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 -05002404 * @mapping: address space structure to write
2405 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2406 * @writepage: function called for each page
2407 * @data: data passed to writepage function
2408 *
2409 * If a page is already under I/O, write_cache_pages() skips it, even
2410 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2411 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2412 * and msync() need to guarantee that all the data which was dirty at the time
2413 * the call was made get new I/O started against them. If wbc->sync_mode is
2414 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2415 * existing IO to complete.
2416 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002417static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002418 struct address_space *mapping,
2419 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002420 writepage_t writepage, void *data,
2421 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002422{
Chris Masond1310b22008-01-24 16:13:08 -05002423 int ret = 0;
2424 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002425 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002426 struct pagevec pvec;
2427 int nr_pages;
2428 pgoff_t index;
2429 pgoff_t end; /* Inclusive */
2430 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002431
Chris Masond1310b22008-01-24 16:13:08 -05002432 pagevec_init(&pvec, 0);
2433 if (wbc->range_cyclic) {
2434 index = mapping->writeback_index; /* Start from prev offset */
2435 end = -1;
2436 } else {
2437 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2438 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002439 scanned = 1;
2440 }
2441retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002442 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002443 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002444 PAGECACHE_TAG_DIRTY, min(end - index,
2445 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002446 unsigned i;
2447
2448 scanned = 1;
2449 for (i = 0; i < nr_pages; i++) {
2450 struct page *page = pvec.pages[i];
2451
2452 /*
2453 * At this point we hold neither mapping->tree_lock nor
2454 * lock on the page itself: the page may be truncated or
2455 * invalidated (changing page->mapping to NULL), or even
2456 * swizzled back from swapper_space to tmpfs file
2457 * mapping
2458 */
Chris Mason4bef0842008-09-08 11:18:08 -04002459 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2460 tree->ops->write_cache_pages_lock_hook(page);
2461 else
2462 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002463
2464 if (unlikely(page->mapping != mapping)) {
2465 unlock_page(page);
2466 continue;
2467 }
2468
2469 if (!wbc->range_cyclic && page->index > end) {
2470 done = 1;
2471 unlock_page(page);
2472 continue;
2473 }
2474
Chris Masond2c3f4f2008-11-19 12:44:22 -05002475 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002476 if (PageWriteback(page))
2477 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002478 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002479 }
Chris Masond1310b22008-01-24 16:13:08 -05002480
2481 if (PageWriteback(page) ||
2482 !clear_page_dirty_for_io(page)) {
2483 unlock_page(page);
2484 continue;
2485 }
2486
2487 ret = (*writepage)(page, wbc, data);
2488
2489 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2490 unlock_page(page);
2491 ret = 0;
2492 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002493 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002494 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002495
2496 /*
2497 * the filesystem may choose to bump up nr_to_write.
2498 * We have to make sure to honor the new nr_to_write
2499 * at any time
2500 */
2501 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002502 }
2503 pagevec_release(&pvec);
2504 cond_resched();
2505 }
2506 if (!scanned && !done) {
2507 /*
2508 * We hit the last page and there is more work to be done: wrap
2509 * back to the start of the file
2510 */
2511 scanned = 1;
2512 index = 0;
2513 goto retry;
2514 }
Chris Masond1310b22008-01-24 16:13:08 -05002515 return ret;
2516}
Chris Masond1310b22008-01-24 16:13:08 -05002517
Chris Masonffbd5172009-04-20 15:50:09 -04002518static void flush_epd_write_bio(struct extent_page_data *epd)
2519{
2520 if (epd->bio) {
2521 if (epd->sync_io)
2522 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2523 else
2524 submit_one_bio(WRITE, epd->bio, 0, 0);
2525 epd->bio = NULL;
2526 }
2527}
2528
Chris Masond2c3f4f2008-11-19 12:44:22 -05002529static noinline void flush_write_bio(void *data)
2530{
2531 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002532 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002533}
2534
Chris Masond1310b22008-01-24 16:13:08 -05002535int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2536 get_extent_t *get_extent,
2537 struct writeback_control *wbc)
2538{
2539 int ret;
2540 struct address_space *mapping = page->mapping;
2541 struct extent_page_data epd = {
2542 .bio = NULL,
2543 .tree = tree,
2544 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002545 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002546 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002547 };
2548 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002549 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002550 .older_than_this = NULL,
2551 .nr_to_write = 64,
2552 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2553 .range_end = (loff_t)-1,
2554 };
2555
Chris Masond1310b22008-01-24 16:13:08 -05002556 ret = __extent_writepage(page, wbc, &epd);
2557
Chris Mason4bef0842008-09-08 11:18:08 -04002558 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002559 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002560 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002561 return ret;
2562}
Chris Masond1310b22008-01-24 16:13:08 -05002563
Chris Mason771ed682008-11-06 22:02:51 -05002564int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2565 u64 start, u64 end, get_extent_t *get_extent,
2566 int mode)
2567{
2568 int ret = 0;
2569 struct address_space *mapping = inode->i_mapping;
2570 struct page *page;
2571 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2572 PAGE_CACHE_SHIFT;
2573
2574 struct extent_page_data epd = {
2575 .bio = NULL,
2576 .tree = tree,
2577 .get_extent = get_extent,
2578 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002579 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002580 };
2581 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002582 .sync_mode = mode,
2583 .older_than_this = NULL,
2584 .nr_to_write = nr_pages * 2,
2585 .range_start = start,
2586 .range_end = end + 1,
2587 };
2588
Chris Masond3977122009-01-05 21:25:51 -05002589 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002590 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2591 if (clear_page_dirty_for_io(page))
2592 ret = __extent_writepage(page, &wbc_writepages, &epd);
2593 else {
2594 if (tree->ops && tree->ops->writepage_end_io_hook)
2595 tree->ops->writepage_end_io_hook(page, start,
2596 start + PAGE_CACHE_SIZE - 1,
2597 NULL, 1);
2598 unlock_page(page);
2599 }
2600 page_cache_release(page);
2601 start += PAGE_CACHE_SIZE;
2602 }
2603
Chris Masonffbd5172009-04-20 15:50:09 -04002604 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002605 return ret;
2606}
Chris Masond1310b22008-01-24 16:13:08 -05002607
2608int extent_writepages(struct extent_io_tree *tree,
2609 struct address_space *mapping,
2610 get_extent_t *get_extent,
2611 struct writeback_control *wbc)
2612{
2613 int ret = 0;
2614 struct extent_page_data epd = {
2615 .bio = NULL,
2616 .tree = tree,
2617 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002618 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002619 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002620 };
2621
Chris Mason4bef0842008-09-08 11:18:08 -04002622 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002623 __extent_writepage, &epd,
2624 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002625 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002626 return ret;
2627}
Chris Masond1310b22008-01-24 16:13:08 -05002628
2629int extent_readpages(struct extent_io_tree *tree,
2630 struct address_space *mapping,
2631 struct list_head *pages, unsigned nr_pages,
2632 get_extent_t get_extent)
2633{
2634 struct bio *bio = NULL;
2635 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002636 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002637
Chris Masond1310b22008-01-24 16:13:08 -05002638 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2639 struct page *page = list_entry(pages->prev, struct page, lru);
2640
2641 prefetchw(&page->flags);
2642 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00002643 if (!add_to_page_cache_lru(page, mapping,
Chris Masond1310b22008-01-24 16:13:08 -05002644 page->index, GFP_KERNEL)) {
Chris Masonf1885912008-04-09 16:28:12 -04002645 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002646 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002647 }
2648 page_cache_release(page);
2649 }
Chris Masond1310b22008-01-24 16:13:08 -05002650 BUG_ON(!list_empty(pages));
2651 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002652 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002653 return 0;
2654}
Chris Masond1310b22008-01-24 16:13:08 -05002655
2656/*
2657 * basic invalidatepage code, this waits on any locked or writeback
2658 * ranges corresponding to the page, and then deletes any extent state
2659 * records from the tree
2660 */
2661int extent_invalidatepage(struct extent_io_tree *tree,
2662 struct page *page, unsigned long offset)
2663{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002664 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002665 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2666 u64 end = start + PAGE_CACHE_SIZE - 1;
2667 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2668
Chris Masond3977122009-01-05 21:25:51 -05002669 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002670 if (start > end)
2671 return 0;
2672
Josef Bacik2ac55d42010-02-03 19:33:23 +00002673 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002674 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002675 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002676 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2677 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002678 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002679 return 0;
2680}
Chris Masond1310b22008-01-24 16:13:08 -05002681
2682/*
2683 * simple commit_write call, set_range_dirty is used to mark both
2684 * the pages and the extent records as dirty
2685 */
2686int extent_commit_write(struct extent_io_tree *tree,
2687 struct inode *inode, struct page *page,
2688 unsigned from, unsigned to)
2689{
2690 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2691
2692 set_page_extent_mapped(page);
2693 set_page_dirty(page);
2694
2695 if (pos > inode->i_size) {
2696 i_size_write(inode, pos);
2697 mark_inode_dirty(inode);
2698 }
2699 return 0;
2700}
Chris Masond1310b22008-01-24 16:13:08 -05002701
2702int extent_prepare_write(struct extent_io_tree *tree,
2703 struct inode *inode, struct page *page,
2704 unsigned from, unsigned to, get_extent_t *get_extent)
2705{
2706 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2707 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2708 u64 block_start;
2709 u64 orig_block_start;
2710 u64 block_end;
2711 u64 cur_end;
2712 struct extent_map *em;
2713 unsigned blocksize = 1 << inode->i_blkbits;
2714 size_t page_offset = 0;
2715 size_t block_off_start;
2716 size_t block_off_end;
2717 int err = 0;
2718 int iocount = 0;
2719 int ret = 0;
2720 int isnew;
2721
2722 set_page_extent_mapped(page);
2723
2724 block_start = (page_start + from) & ~((u64)blocksize - 1);
2725 block_end = (page_start + to - 1) | (blocksize - 1);
2726 orig_block_start = block_start;
2727
2728 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002729 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002730 em = get_extent(inode, page, page_offset, block_start,
2731 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002732 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002733 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002734
Chris Masond1310b22008-01-24 16:13:08 -05002735 cur_end = min(block_end, extent_map_end(em) - 1);
2736 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2737 block_off_end = block_off_start + blocksize;
2738 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2739
2740 if (!PageUptodate(page) && isnew &&
2741 (block_off_end > to || block_off_start < from)) {
2742 void *kaddr;
2743
2744 kaddr = kmap_atomic(page, KM_USER0);
2745 if (block_off_end > to)
2746 memset(kaddr + to, 0, block_off_end - to);
2747 if (block_off_start < from)
2748 memset(kaddr + block_off_start, 0,
2749 from - block_off_start);
2750 flush_dcache_page(page);
2751 kunmap_atomic(kaddr, KM_USER0);
2752 }
2753 if ((em->block_start != EXTENT_MAP_HOLE &&
2754 em->block_start != EXTENT_MAP_INLINE) &&
2755 !isnew && !PageUptodate(page) &&
2756 (block_off_end > to || block_off_start < from) &&
2757 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002758 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002759 u64 sector;
2760 u64 extent_offset = block_start - em->start;
2761 size_t iosize;
2762 sector = (em->block_start + extent_offset) >> 9;
2763 iosize = (cur_end - block_start + blocksize) &
2764 ~((u64)blocksize - 1);
2765 /*
2766 * we've already got the extent locked, but we
2767 * need to split the state such that our end_bio
2768 * handler can clear the lock.
2769 */
2770 set_extent_bit(tree, block_start,
2771 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002772 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002773 ret = submit_extent_page(READ, tree, page,
2774 sector, iosize, page_offset, em->bdev,
2775 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002776 end_bio_extent_preparewrite, 0,
2777 0, 0);
Andi Kleen411fc6b2010-10-29 15:14:31 -04002778 if (ret && !err)
2779 err = ret;
Chris Masond1310b22008-01-24 16:13:08 -05002780 iocount++;
2781 block_start = block_start + iosize;
2782 } else {
2783 set_extent_uptodate(tree, block_start, cur_end,
2784 GFP_NOFS);
2785 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2786 block_start = cur_end + 1;
2787 }
2788 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2789 free_extent_map(em);
2790 }
2791 if (iocount) {
2792 wait_extent_bit(tree, orig_block_start,
2793 block_end, EXTENT_LOCKED);
2794 }
2795 check_page_uptodate(tree, page);
2796err:
2797 /* FIXME, zero out newly allocated blocks on error */
2798 return err;
2799}
Chris Masond1310b22008-01-24 16:13:08 -05002800
2801/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002802 * a helper for releasepage, this tests for areas of the page that
2803 * are locked or under IO and drops the related state bits if it is safe
2804 * to drop the page.
2805 */
2806int try_release_extent_state(struct extent_map_tree *map,
2807 struct extent_io_tree *tree, struct page *page,
2808 gfp_t mask)
2809{
2810 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2811 u64 end = start + PAGE_CACHE_SIZE - 1;
2812 int ret = 1;
2813
Chris Mason211f90e2008-07-18 11:56:15 -04002814 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002815 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002816 ret = 0;
2817 else {
2818 if ((mask & GFP_NOFS) == GFP_NOFS)
2819 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002820 /*
2821 * at this point we can safely clear everything except the
2822 * locked bit and the nodatasum bit
2823 */
2824 clear_extent_bit(tree, start, end,
2825 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2826 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002827 }
2828 return ret;
2829}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002830
2831/*
Chris Masond1310b22008-01-24 16:13:08 -05002832 * a helper for releasepage. As long as there are no locked extents
2833 * in the range corresponding to the page, both state records and extent
2834 * map records are removed
2835 */
2836int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002837 struct extent_io_tree *tree, struct page *page,
2838 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002839{
2840 struct extent_map *em;
2841 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2842 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002843
Chris Mason70dec802008-01-29 09:59:12 -05002844 if ((mask & __GFP_WAIT) &&
2845 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002846 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002847 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002848 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002849 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002850 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002851 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002852 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002853 break;
2854 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002855 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2856 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002857 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002858 free_extent_map(em);
2859 break;
2860 }
2861 if (!test_range_bit(tree, em->start,
2862 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002863 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002864 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002865 remove_extent_mapping(map, em);
2866 /* once for the rb tree */
2867 free_extent_map(em);
2868 }
2869 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002870 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002871
2872 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002873 free_extent_map(em);
2874 }
Chris Masond1310b22008-01-24 16:13:08 -05002875 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002876 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002877}
Chris Masond1310b22008-01-24 16:13:08 -05002878
2879sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2880 get_extent_t *get_extent)
2881{
2882 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002883 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002884 u64 start = iblock << inode->i_blkbits;
2885 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002886 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002887 struct extent_map *em;
2888
Josef Bacik2ac55d42010-02-03 19:33:23 +00002889 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2890 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002891 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002892 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2893 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002894 if (!em || IS_ERR(em))
2895 return 0;
2896
Yan Zhengd899e052008-10-30 14:25:28 -04002897 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002898 goto out;
2899
2900 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002901out:
2902 free_extent_map(em);
2903 return sector;
2904}
2905
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002906int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2907 __u64 start, __u64 len, get_extent_t *get_extent)
2908{
Josef Bacik975f84f2010-11-23 19:36:57 +00002909 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002910 u64 off = start;
2911 u64 max = start + len;
2912 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00002913 u32 found_type;
2914 u64 last;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002915 u64 disko = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00002916 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002917 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002918 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00002919 struct btrfs_path *path;
2920 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002921 int end = 0;
2922 u64 em_start = 0, em_len = 0;
2923 unsigned long emflags;
Josef Bacik975f84f2010-11-23 19:36:57 +00002924 int hole = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002925
2926 if (len == 0)
2927 return -EINVAL;
2928
Josef Bacik975f84f2010-11-23 19:36:57 +00002929 path = btrfs_alloc_path();
2930 if (!path)
2931 return -ENOMEM;
2932 path->leave_spinning = 1;
2933
2934 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
2935 path, inode->i_ino, -1, 0);
2936 if (ret < 0) {
2937 btrfs_free_path(path);
2938 return ret;
2939 }
2940 WARN_ON(!ret);
2941 path->slots[0]--;
2942 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2943 struct btrfs_file_extent_item);
2944 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
2945 found_type = btrfs_key_type(&found_key);
2946
2947 /* No extents, just return */
2948 if (found_key.objectid != inode->i_ino ||
2949 found_type != BTRFS_EXTENT_DATA_KEY) {
2950 btrfs_free_path(path);
2951 return 0;
2952 }
2953 last = found_key.offset;
2954 btrfs_free_path(path);
2955
Josef Bacik2ac55d42010-02-03 19:33:23 +00002956 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2957 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002958 em = get_extent(inode, NULL, 0, off, max - off, 0);
2959 if (!em)
2960 goto out;
2961 if (IS_ERR(em)) {
2962 ret = PTR_ERR(em);
2963 goto out;
2964 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002965
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002966 while (!end) {
Josef Bacik975f84f2010-11-23 19:36:57 +00002967 hole = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002968 off = em->start + em->len;
2969 if (off >= max)
2970 end = 1;
2971
Josef Bacik975f84f2010-11-23 19:36:57 +00002972 if (em->block_start == EXTENT_MAP_HOLE) {
2973 hole = 1;
2974 goto next;
2975 }
2976
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002977 em_start = em->start;
2978 em_len = em->len;
2979
2980 disko = 0;
2981 flags = 0;
2982
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002983 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002984 end = 1;
2985 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002986 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002987 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2988 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002989 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002990 flags |= (FIEMAP_EXTENT_DELALLOC |
2991 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002992 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002993 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002994 }
2995 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2996 flags |= FIEMAP_EXTENT_ENCODED;
2997
Josef Bacik975f84f2010-11-23 19:36:57 +00002998next:
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002999 emflags = em->flags;
3000 free_extent_map(em);
3001 em = NULL;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003002 if (!end) {
3003 em = get_extent(inode, NULL, 0, off, max - off, 0);
3004 if (!em)
3005 goto out;
3006 if (IS_ERR(em)) {
3007 ret = PTR_ERR(em);
3008 goto out;
3009 }
3010 emflags = em->flags;
3011 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003012
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003013 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3014 flags |= FIEMAP_EXTENT_LAST;
3015 end = 1;
3016 }
3017
Josef Bacik975f84f2010-11-23 19:36:57 +00003018 if (em_start == last) {
3019 flags |= FIEMAP_EXTENT_LAST;
3020 end = 1;
3021 }
3022
3023 if (!hole) {
3024 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3025 em_len, flags);
3026 if (ret)
3027 goto out_free;
3028 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003029 }
3030out_free:
3031 free_extent_map(em);
3032out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003033 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3034 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003035 return ret;
3036}
3037
Chris Masond1310b22008-01-24 16:13:08 -05003038static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3039 unsigned long i)
3040{
3041 struct page *p;
3042 struct address_space *mapping;
3043
3044 if (i == 0)
3045 return eb->first_page;
3046 i += eb->start >> PAGE_CACHE_SHIFT;
3047 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003048 if (!mapping)
3049 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003050
3051 /*
3052 * extent_buffer_page is only called after pinning the page
3053 * by increasing the reference count. So we know the page must
3054 * be in the radix tree.
3055 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003056 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003057 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003058 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003059
Chris Masond1310b22008-01-24 16:13:08 -05003060 return p;
3061}
3062
Chris Mason6af118c2008-07-22 11:18:07 -04003063static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003064{
Chris Mason6af118c2008-07-22 11:18:07 -04003065 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3066 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003067}
3068
Chris Masond1310b22008-01-24 16:13:08 -05003069static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3070 u64 start,
3071 unsigned long len,
3072 gfp_t mask)
3073{
3074 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003075#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003076 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003077#endif
Chris Masond1310b22008-01-24 16:13:08 -05003078
Chris Masond1310b22008-01-24 16:13:08 -05003079 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003080 if (eb == NULL)
3081 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003082 eb->start = start;
3083 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003084 spin_lock_init(&eb->lock);
3085 init_waitqueue_head(&eb->lock_wq);
3086
Chris Mason39351272009-02-04 09:24:05 -05003087#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003088 spin_lock_irqsave(&leak_lock, flags);
3089 list_add(&eb->leak_list, &buffers);
3090 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003091#endif
Chris Masond1310b22008-01-24 16:13:08 -05003092 atomic_set(&eb->refs, 1);
3093
3094 return eb;
3095}
3096
3097static void __free_extent_buffer(struct extent_buffer *eb)
3098{
Chris Mason39351272009-02-04 09:24:05 -05003099#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003100 unsigned long flags;
3101 spin_lock_irqsave(&leak_lock, flags);
3102 list_del(&eb->leak_list);
3103 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003104#endif
Chris Masond1310b22008-01-24 16:13:08 -05003105 kmem_cache_free(extent_buffer_cache, eb);
3106}
3107
Miao Xie897ca6e2010-10-26 20:57:29 -04003108/*
3109 * Helper for releasing extent buffer page.
3110 */
3111static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3112 unsigned long start_idx)
3113{
3114 unsigned long index;
3115 struct page *page;
3116
3117 if (!eb->first_page)
3118 return;
3119
3120 index = num_extent_pages(eb->start, eb->len);
3121 if (start_idx >= index)
3122 return;
3123
3124 do {
3125 index--;
3126 page = extent_buffer_page(eb, index);
3127 if (page)
3128 page_cache_release(page);
3129 } while (index != start_idx);
3130}
3131
3132/*
3133 * Helper for releasing the extent buffer.
3134 */
3135static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3136{
3137 btrfs_release_extent_buffer_page(eb, 0);
3138 __free_extent_buffer(eb);
3139}
3140
Chris Masond1310b22008-01-24 16:13:08 -05003141struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3142 u64 start, unsigned long len,
3143 struct page *page0,
3144 gfp_t mask)
3145{
3146 unsigned long num_pages = num_extent_pages(start, len);
3147 unsigned long i;
3148 unsigned long index = start >> PAGE_CACHE_SHIFT;
3149 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003150 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003151 struct page *p;
3152 struct address_space *mapping = tree->mapping;
3153 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003154 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003155
Miao Xie19fe0a82010-10-26 20:57:29 -04003156 rcu_read_lock();
3157 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3158 if (eb && atomic_inc_not_zero(&eb->refs)) {
3159 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003160 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003161 return eb;
3162 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003163 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04003164
Chris Masond1310b22008-01-24 16:13:08 -05003165 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003166 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003167 return NULL;
3168
Chris Masond1310b22008-01-24 16:13:08 -05003169 if (page0) {
3170 eb->first_page = page0;
3171 i = 1;
3172 index++;
3173 page_cache_get(page0);
3174 mark_page_accessed(page0);
3175 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003176 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003177 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003178 } else {
3179 i = 0;
3180 }
3181 for (; i < num_pages; i++, index++) {
3182 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3183 if (!p) {
3184 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003185 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003186 }
3187 set_page_extent_mapped(p);
3188 mark_page_accessed(p);
3189 if (i == 0) {
3190 eb->first_page = p;
3191 set_page_extent_head(p, len);
3192 } else {
3193 set_page_private(p, EXTENT_PAGE_PRIVATE);
3194 }
3195 if (!PageUptodate(p))
3196 uptodate = 0;
3197 unlock_page(p);
3198 }
3199 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003200 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003201
Miao Xie19fe0a82010-10-26 20:57:29 -04003202 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3203 if (ret)
3204 goto free_eb;
3205
Chris Mason6af118c2008-07-22 11:18:07 -04003206 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003207 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3208 if (ret == -EEXIST) {
3209 exists = radix_tree_lookup(&tree->buffer,
3210 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003211 /* add one reference for the caller */
3212 atomic_inc(&exists->refs);
3213 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003214 radix_tree_preload_end();
Chris Mason6af118c2008-07-22 11:18:07 -04003215 goto free_eb;
3216 }
Chris Mason6af118c2008-07-22 11:18:07 -04003217 /* add one reference for the tree */
3218 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003219 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003220 radix_tree_preload_end();
Chris Masond1310b22008-01-24 16:13:08 -05003221 return eb;
3222
Chris Mason6af118c2008-07-22 11:18:07 -04003223free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003224 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003225 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003226 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003227 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003228}
Chris Masond1310b22008-01-24 16:13:08 -05003229
3230struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3231 u64 start, unsigned long len,
3232 gfp_t mask)
3233{
Chris Masond1310b22008-01-24 16:13:08 -05003234 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003235
Miao Xie19fe0a82010-10-26 20:57:29 -04003236 rcu_read_lock();
3237 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3238 if (eb && atomic_inc_not_zero(&eb->refs)) {
3239 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003240 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003241 return eb;
3242 }
3243 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003244
Miao Xie19fe0a82010-10-26 20:57:29 -04003245 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003246}
Chris Masond1310b22008-01-24 16:13:08 -05003247
3248void free_extent_buffer(struct extent_buffer *eb)
3249{
Chris Masond1310b22008-01-24 16:13:08 -05003250 if (!eb)
3251 return;
3252
3253 if (!atomic_dec_and_test(&eb->refs))
3254 return;
3255
Chris Mason6af118c2008-07-22 11:18:07 -04003256 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003257}
Chris Masond1310b22008-01-24 16:13:08 -05003258
3259int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3260 struct extent_buffer *eb)
3261{
Chris Masond1310b22008-01-24 16:13:08 -05003262 unsigned long i;
3263 unsigned long num_pages;
3264 struct page *page;
3265
Chris Masond1310b22008-01-24 16:13:08 -05003266 num_pages = num_extent_pages(eb->start, eb->len);
3267
3268 for (i = 0; i < num_pages; i++) {
3269 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003270 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003271 continue;
3272
Chris Masona61e6f22008-07-22 11:18:08 -04003273 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003274 if (i == 0)
3275 set_page_extent_head(page, eb->len);
3276 else
3277 set_page_private(page, EXTENT_PAGE_PRIVATE);
3278
Chris Masond1310b22008-01-24 16:13:08 -05003279 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003280 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003281 if (!PageDirty(page)) {
3282 radix_tree_tag_clear(&page->mapping->page_tree,
3283 page_index(page),
3284 PAGECACHE_TAG_DIRTY);
3285 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003286 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003287 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003288 }
3289 return 0;
3290}
Chris Masond1310b22008-01-24 16:13:08 -05003291
3292int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3293 struct extent_buffer *eb)
3294{
3295 return wait_on_extent_writeback(tree, eb->start,
3296 eb->start + eb->len - 1);
3297}
Chris Masond1310b22008-01-24 16:13:08 -05003298
3299int set_extent_buffer_dirty(struct extent_io_tree *tree,
3300 struct extent_buffer *eb)
3301{
3302 unsigned long i;
3303 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003304 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003305
Chris Masonb9473432009-03-13 11:00:37 -04003306 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003307 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003308 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003309 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003310 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003311}
Chris Masond1310b22008-01-24 16:13:08 -05003312
Chris Mason1259ab72008-05-12 13:39:03 -04003313int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003314 struct extent_buffer *eb,
3315 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003316{
3317 unsigned long i;
3318 struct page *page;
3319 unsigned long num_pages;
3320
3321 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003322 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003323
3324 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003325 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003326 for (i = 0; i < num_pages; i++) {
3327 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003328 if (page)
3329 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003330 }
3331 return 0;
3332}
3333
Chris Masond1310b22008-01-24 16:13:08 -05003334int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3335 struct extent_buffer *eb)
3336{
3337 unsigned long i;
3338 struct page *page;
3339 unsigned long num_pages;
3340
3341 num_pages = num_extent_pages(eb->start, eb->len);
3342
3343 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3344 GFP_NOFS);
3345 for (i = 0; i < num_pages; i++) {
3346 page = extent_buffer_page(eb, i);
3347 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3348 ((i == num_pages - 1) &&
3349 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3350 check_page_uptodate(tree, page);
3351 continue;
3352 }
3353 SetPageUptodate(page);
3354 }
3355 return 0;
3356}
Chris Masond1310b22008-01-24 16:13:08 -05003357
Chris Masonce9adaa2008-04-09 16:28:12 -04003358int extent_range_uptodate(struct extent_io_tree *tree,
3359 u64 start, u64 end)
3360{
3361 struct page *page;
3362 int ret;
3363 int pg_uptodate = 1;
3364 int uptodate;
3365 unsigned long index;
3366
Chris Mason9655d292009-09-02 15:22:30 -04003367 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003368 if (ret)
3369 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003370 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003371 index = start >> PAGE_CACHE_SHIFT;
3372 page = find_get_page(tree->mapping, index);
3373 uptodate = PageUptodate(page);
3374 page_cache_release(page);
3375 if (!uptodate) {
3376 pg_uptodate = 0;
3377 break;
3378 }
3379 start += PAGE_CACHE_SIZE;
3380 }
3381 return pg_uptodate;
3382}
3383
Chris Masond1310b22008-01-24 16:13:08 -05003384int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003385 struct extent_buffer *eb,
3386 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003387{
Chris Mason728131d2008-04-09 16:28:12 -04003388 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003389 unsigned long num_pages;
3390 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003391 struct page *page;
3392 int pg_uptodate = 1;
3393
Chris Masonb4ce94d2009-02-04 09:25:08 -05003394 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003395 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003396
Chris Mason42352982008-04-28 16:40:52 -04003397 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003398 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003399 if (ret)
3400 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003401
3402 num_pages = num_extent_pages(eb->start, eb->len);
3403 for (i = 0; i < num_pages; i++) {
3404 page = extent_buffer_page(eb, i);
3405 if (!PageUptodate(page)) {
3406 pg_uptodate = 0;
3407 break;
3408 }
3409 }
Chris Mason42352982008-04-28 16:40:52 -04003410 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003411}
Chris Masond1310b22008-01-24 16:13:08 -05003412
3413int read_extent_buffer_pages(struct extent_io_tree *tree,
3414 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003415 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003416 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003417{
3418 unsigned long i;
3419 unsigned long start_i;
3420 struct page *page;
3421 int err;
3422 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003423 int locked_pages = 0;
3424 int all_uptodate = 1;
3425 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003426 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003427 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003428 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003429
Chris Masonb4ce94d2009-02-04 09:25:08 -05003430 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003431 return 0;
3432
Chris Masonce9adaa2008-04-09 16:28:12 -04003433 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003434 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003435 return 0;
3436 }
3437
3438 if (start) {
3439 WARN_ON(start < eb->start);
3440 start_i = (start >> PAGE_CACHE_SHIFT) -
3441 (eb->start >> PAGE_CACHE_SHIFT);
3442 } else {
3443 start_i = 0;
3444 }
3445
3446 num_pages = num_extent_pages(eb->start, eb->len);
3447 for (i = start_i; i < num_pages; i++) {
3448 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003449 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003450 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003451 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003452 } else {
3453 lock_page(page);
3454 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003455 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003456 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003457 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003458 }
3459 if (all_uptodate) {
3460 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003461 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003462 goto unlock_exit;
3463 }
3464
3465 for (i = start_i; i < num_pages; i++) {
3466 page = extent_buffer_page(eb, i);
3467 if (inc_all_pages)
3468 page_cache_get(page);
3469 if (!PageUptodate(page)) {
3470 if (start_i == 0)
3471 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003472 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003473 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003474 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003475 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003476 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003477 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003478 } else {
3479 unlock_page(page);
3480 }
3481 }
3482
Chris Masona86c12c2008-02-07 10:50:54 -05003483 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003484 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003485
Chris Masond3977122009-01-05 21:25:51 -05003486 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003487 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003488
Chris Masond1310b22008-01-24 16:13:08 -05003489 for (i = start_i; i < num_pages; i++) {
3490 page = extent_buffer_page(eb, i);
3491 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003492 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003493 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003494 }
Chris Masond3977122009-01-05 21:25:51 -05003495
Chris Masond1310b22008-01-24 16:13:08 -05003496 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003497 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003498 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003499
3500unlock_exit:
3501 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003502 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003503 page = extent_buffer_page(eb, i);
3504 i++;
3505 unlock_page(page);
3506 locked_pages--;
3507 }
3508 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003509}
Chris Masond1310b22008-01-24 16:13:08 -05003510
3511void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3512 unsigned long start,
3513 unsigned long len)
3514{
3515 size_t cur;
3516 size_t offset;
3517 struct page *page;
3518 char *kaddr;
3519 char *dst = (char *)dstv;
3520 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3521 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003522
3523 WARN_ON(start > eb->len);
3524 WARN_ON(start + len > eb->start + eb->len);
3525
3526 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3527
Chris Masond3977122009-01-05 21:25:51 -05003528 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003529 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003530
3531 cur = min(len, (PAGE_CACHE_SIZE - offset));
3532 kaddr = kmap_atomic(page, KM_USER1);
3533 memcpy(dst, kaddr + offset, cur);
3534 kunmap_atomic(kaddr, KM_USER1);
3535
3536 dst += cur;
3537 len -= cur;
3538 offset = 0;
3539 i++;
3540 }
3541}
Chris Masond1310b22008-01-24 16:13:08 -05003542
3543int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3544 unsigned long min_len, char **token, char **map,
3545 unsigned long *map_start,
3546 unsigned long *map_len, int km)
3547{
3548 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3549 char *kaddr;
3550 struct page *p;
3551 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3552 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3553 unsigned long end_i = (start_offset + start + min_len - 1) >>
3554 PAGE_CACHE_SHIFT;
3555
3556 if (i != end_i)
3557 return -EINVAL;
3558
3559 if (i == 0) {
3560 offset = start_offset;
3561 *map_start = 0;
3562 } else {
3563 offset = 0;
3564 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3565 }
Chris Masond3977122009-01-05 21:25:51 -05003566
Chris Masond1310b22008-01-24 16:13:08 -05003567 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003568 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3569 "wanted %lu %lu\n", (unsigned long long)eb->start,
3570 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003571 WARN_ON(1);
3572 }
3573
3574 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003575 kaddr = kmap_atomic(p, km);
3576 *token = kaddr;
3577 *map = kaddr + offset;
3578 *map_len = PAGE_CACHE_SIZE - offset;
3579 return 0;
3580}
Chris Masond1310b22008-01-24 16:13:08 -05003581
3582int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3583 unsigned long min_len,
3584 char **token, char **map,
3585 unsigned long *map_start,
3586 unsigned long *map_len, int km)
3587{
3588 int err;
3589 int save = 0;
3590 if (eb->map_token) {
3591 unmap_extent_buffer(eb, eb->map_token, km);
3592 eb->map_token = NULL;
3593 save = 1;
3594 }
3595 err = map_private_extent_buffer(eb, start, min_len, token, map,
3596 map_start, map_len, km);
3597 if (!err && save) {
3598 eb->map_token = *token;
3599 eb->kaddr = *map;
3600 eb->map_start = *map_start;
3601 eb->map_len = *map_len;
3602 }
3603 return err;
3604}
Chris Masond1310b22008-01-24 16:13:08 -05003605
3606void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3607{
3608 kunmap_atomic(token, km);
3609}
Chris Masond1310b22008-01-24 16:13:08 -05003610
3611int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3612 unsigned long start,
3613 unsigned long len)
3614{
3615 size_t cur;
3616 size_t offset;
3617 struct page *page;
3618 char *kaddr;
3619 char *ptr = (char *)ptrv;
3620 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3621 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3622 int ret = 0;
3623
3624 WARN_ON(start > eb->len);
3625 WARN_ON(start + len > eb->start + eb->len);
3626
3627 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3628
Chris Masond3977122009-01-05 21:25:51 -05003629 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003630 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003631
3632 cur = min(len, (PAGE_CACHE_SIZE - offset));
3633
3634 kaddr = kmap_atomic(page, KM_USER0);
3635 ret = memcmp(ptr, kaddr + offset, cur);
3636 kunmap_atomic(kaddr, KM_USER0);
3637 if (ret)
3638 break;
3639
3640 ptr += cur;
3641 len -= cur;
3642 offset = 0;
3643 i++;
3644 }
3645 return ret;
3646}
Chris Masond1310b22008-01-24 16:13:08 -05003647
3648void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3649 unsigned long start, unsigned long len)
3650{
3651 size_t cur;
3652 size_t offset;
3653 struct page *page;
3654 char *kaddr;
3655 char *src = (char *)srcv;
3656 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3657 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3658
3659 WARN_ON(start > eb->len);
3660 WARN_ON(start + len > eb->start + eb->len);
3661
3662 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3663
Chris Masond3977122009-01-05 21:25:51 -05003664 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003665 page = extent_buffer_page(eb, i);
3666 WARN_ON(!PageUptodate(page));
3667
3668 cur = min(len, PAGE_CACHE_SIZE - offset);
3669 kaddr = kmap_atomic(page, KM_USER1);
3670 memcpy(kaddr + offset, src, cur);
3671 kunmap_atomic(kaddr, KM_USER1);
3672
3673 src += cur;
3674 len -= cur;
3675 offset = 0;
3676 i++;
3677 }
3678}
Chris Masond1310b22008-01-24 16:13:08 -05003679
3680void memset_extent_buffer(struct extent_buffer *eb, char c,
3681 unsigned long start, unsigned long len)
3682{
3683 size_t cur;
3684 size_t offset;
3685 struct page *page;
3686 char *kaddr;
3687 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3688 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3689
3690 WARN_ON(start > eb->len);
3691 WARN_ON(start + len > eb->start + eb->len);
3692
3693 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3694
Chris Masond3977122009-01-05 21:25:51 -05003695 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003696 page = extent_buffer_page(eb, i);
3697 WARN_ON(!PageUptodate(page));
3698
3699 cur = min(len, PAGE_CACHE_SIZE - offset);
3700 kaddr = kmap_atomic(page, KM_USER0);
3701 memset(kaddr + offset, c, cur);
3702 kunmap_atomic(kaddr, KM_USER0);
3703
3704 len -= cur;
3705 offset = 0;
3706 i++;
3707 }
3708}
Chris Masond1310b22008-01-24 16:13:08 -05003709
3710void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3711 unsigned long dst_offset, unsigned long src_offset,
3712 unsigned long len)
3713{
3714 u64 dst_len = dst->len;
3715 size_t cur;
3716 size_t offset;
3717 struct page *page;
3718 char *kaddr;
3719 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3720 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3721
3722 WARN_ON(src->len != dst_len);
3723
3724 offset = (start_offset + dst_offset) &
3725 ((unsigned long)PAGE_CACHE_SIZE - 1);
3726
Chris Masond3977122009-01-05 21:25:51 -05003727 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003728 page = extent_buffer_page(dst, i);
3729 WARN_ON(!PageUptodate(page));
3730
3731 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3732
3733 kaddr = kmap_atomic(page, KM_USER0);
3734 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3735 kunmap_atomic(kaddr, KM_USER0);
3736
3737 src_offset += cur;
3738 len -= cur;
3739 offset = 0;
3740 i++;
3741 }
3742}
Chris Masond1310b22008-01-24 16:13:08 -05003743
3744static void move_pages(struct page *dst_page, struct page *src_page,
3745 unsigned long dst_off, unsigned long src_off,
3746 unsigned long len)
3747{
3748 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3749 if (dst_page == src_page) {
3750 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3751 } else {
3752 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3753 char *p = dst_kaddr + dst_off + len;
3754 char *s = src_kaddr + src_off + len;
3755
3756 while (len--)
3757 *--p = *--s;
3758
3759 kunmap_atomic(src_kaddr, KM_USER1);
3760 }
3761 kunmap_atomic(dst_kaddr, KM_USER0);
3762}
3763
3764static void copy_pages(struct page *dst_page, struct page *src_page,
3765 unsigned long dst_off, unsigned long src_off,
3766 unsigned long len)
3767{
3768 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3769 char *src_kaddr;
3770
3771 if (dst_page != src_page)
3772 src_kaddr = kmap_atomic(src_page, KM_USER1);
3773 else
3774 src_kaddr = dst_kaddr;
3775
3776 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3777 kunmap_atomic(dst_kaddr, KM_USER0);
3778 if (dst_page != src_page)
3779 kunmap_atomic(src_kaddr, KM_USER1);
3780}
3781
3782void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3783 unsigned long src_offset, unsigned long len)
3784{
3785 size_t cur;
3786 size_t dst_off_in_page;
3787 size_t src_off_in_page;
3788 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3789 unsigned long dst_i;
3790 unsigned long src_i;
3791
3792 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003793 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3794 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003795 BUG_ON(1);
3796 }
3797 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003798 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3799 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003800 BUG_ON(1);
3801 }
3802
Chris Masond3977122009-01-05 21:25:51 -05003803 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003804 dst_off_in_page = (start_offset + dst_offset) &
3805 ((unsigned long)PAGE_CACHE_SIZE - 1);
3806 src_off_in_page = (start_offset + src_offset) &
3807 ((unsigned long)PAGE_CACHE_SIZE - 1);
3808
3809 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3810 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3811
3812 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3813 src_off_in_page));
3814 cur = min_t(unsigned long, cur,
3815 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3816
3817 copy_pages(extent_buffer_page(dst, dst_i),
3818 extent_buffer_page(dst, src_i),
3819 dst_off_in_page, src_off_in_page, cur);
3820
3821 src_offset += cur;
3822 dst_offset += cur;
3823 len -= cur;
3824 }
3825}
Chris Masond1310b22008-01-24 16:13:08 -05003826
3827void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3828 unsigned long src_offset, unsigned long len)
3829{
3830 size_t cur;
3831 size_t dst_off_in_page;
3832 size_t src_off_in_page;
3833 unsigned long dst_end = dst_offset + len - 1;
3834 unsigned long src_end = src_offset + len - 1;
3835 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3836 unsigned long dst_i;
3837 unsigned long src_i;
3838
3839 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003840 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3841 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003842 BUG_ON(1);
3843 }
3844 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003845 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3846 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003847 BUG_ON(1);
3848 }
3849 if (dst_offset < src_offset) {
3850 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3851 return;
3852 }
Chris Masond3977122009-01-05 21:25:51 -05003853 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003854 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3855 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3856
3857 dst_off_in_page = (start_offset + dst_end) &
3858 ((unsigned long)PAGE_CACHE_SIZE - 1);
3859 src_off_in_page = (start_offset + src_end) &
3860 ((unsigned long)PAGE_CACHE_SIZE - 1);
3861
3862 cur = min_t(unsigned long, len, src_off_in_page + 1);
3863 cur = min(cur, dst_off_in_page + 1);
3864 move_pages(extent_buffer_page(dst, dst_i),
3865 extent_buffer_page(dst, src_i),
3866 dst_off_in_page - cur + 1,
3867 src_off_in_page - cur + 1, cur);
3868
3869 dst_end -= cur;
3870 src_end -= cur;
3871 len -= cur;
3872 }
3873}
Chris Mason6af118c2008-07-22 11:18:07 -04003874
Miao Xie19fe0a82010-10-26 20:57:29 -04003875static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3876{
3877 struct extent_buffer *eb =
3878 container_of(head, struct extent_buffer, rcu_head);
3879
3880 btrfs_release_extent_buffer(eb);
3881}
3882
Chris Mason6af118c2008-07-22 11:18:07 -04003883int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3884{
3885 u64 start = page_offset(page);
3886 struct extent_buffer *eb;
3887 int ret = 1;
Chris Mason6af118c2008-07-22 11:18:07 -04003888
3889 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003890 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05003891 if (!eb) {
3892 spin_unlock(&tree->buffer_lock);
3893 return ret;
3894 }
Chris Mason6af118c2008-07-22 11:18:07 -04003895
Chris Masonb9473432009-03-13 11:00:37 -04003896 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3897 ret = 0;
3898 goto out;
3899 }
Miao Xie897ca6e2010-10-26 20:57:29 -04003900
Miao Xie19fe0a82010-10-26 20:57:29 -04003901 /*
3902 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3903 * Or go back.
3904 */
3905 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
3906 ret = 0;
3907 goto out;
3908 }
3909
3910 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003911out:
3912 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003913
3914 /* at this point we can safely release the extent buffer */
3915 if (atomic_read(&eb->refs) == 0)
3916 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118c2008-07-22 11:18:07 -04003917 return ret;
3918}