blob: 91208296ff2bff8717b05824525ec31bf0216fcf [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
13#include "extent_io.h"
14#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040015#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050018
Chris Masond1310b22008-01-24 16:13:08 -050019static struct kmem_cache *extent_state_cache;
20static struct kmem_cache *extent_buffer_cache;
21
22static LIST_HEAD(buffers);
23static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040024
Chris Masonb47eda82008-11-10 12:34:40 -050025#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050026#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050027static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040028#endif
Chris Masond1310b22008-01-24 16:13:08 -050029
Chris Masond1310b22008-01-24 16:13:08 -050030#define BUFFER_LRU_MAX 64
31
32struct tree_entry {
33 u64 start;
34 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050035 struct rb_node rb_node;
36};
37
38struct extent_page_data {
39 struct bio *bio;
40 struct extent_io_tree *tree;
41 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050042
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
45 */
Chris Masonffbd5172009-04-20 15:50:09 -040046 unsigned int extent_locked:1;
47
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050050};
51
52int __init extent_io_init(void)
53{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020054 extent_state_cache = kmem_cache_create("extent_state",
55 sizeof(struct extent_state), 0,
56 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050057 if (!extent_state_cache)
58 return -ENOMEM;
59
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020060 extent_buffer_cache = kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer), 0,
62 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050063 if (!extent_buffer_cache)
64 goto free_state_cache;
65 return 0;
66
67free_state_cache:
68 kmem_cache_destroy(extent_state_cache);
69 return -ENOMEM;
70}
71
72void extent_io_exit(void)
73{
74 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040075 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050076
77 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040078 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050079 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state->start,
82 (unsigned long long)state->end,
83 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040084 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050085 kmem_cache_free(extent_state_cache, state);
86
87 }
88
Chris Mason2d2ae542008-03-26 16:24:23 -040089 while (!list_empty(&buffers)) {
90 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050091 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb->start,
93 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040094 list_del(&eb->leak_list);
95 kmem_cache_free(extent_buffer_cache, eb);
96 }
Chris Masond1310b22008-01-24 16:13:08 -050097 if (extent_state_cache)
98 kmem_cache_destroy(extent_state_cache);
99 if (extent_buffer_cache)
100 kmem_cache_destroy(extent_buffer_cache);
101}
102
103void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200104 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500105{
Eric Paris6bef4d32010-02-23 19:43:04 +0000106 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400107 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500108 tree->ops = NULL;
109 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500110 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400111 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500113}
Chris Masond1310b22008-01-24 16:13:08 -0500114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
117 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500118#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
Chris Masond1310b22008-01-24 16:13:08 -0500137
Chris Mason4845e442010-05-25 20:56:50 -0400138void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500143#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400144 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Mason70dec802008-01-29 09:59:12 -0500146 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 kmem_cache_free(extent_state_cache, state);
153 }
154}
Chris Masond1310b22008-01-24 16:13:08 -0500155
156static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
158{
Chris Masond3977122009-01-05 21:25:51 -0500159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500161 struct tree_entry *entry;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500164 parent = *p;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
166
167 if (offset < entry->start)
168 p = &(*p)->rb_left;
169 else if (offset > entry->end)
170 p = &(*p)->rb_right;
171 else
172 return parent;
173 }
174
175 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
178 return NULL;
179}
180
Chris Mason80ea96b2008-02-01 14:51:59 -0500181static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
184{
Chris Mason80ea96b2008-02-01 14:51:59 -0500185 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500186 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
191
Chris Masond3977122009-01-05 21:25:51 -0500192 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500193 entry = rb_entry(n, struct tree_entry, rb_node);
194 prev = n;
195 prev_entry = entry;
196
197 if (offset < entry->start)
198 n = n->rb_left;
199 else if (offset > entry->end)
200 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500201 else
Chris Masond1310b22008-01-24 16:13:08 -0500202 return n;
203 }
204
205 if (prev_ret) {
206 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500207 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
210 }
211 *prev_ret = prev;
212 prev = orig_prev;
213 }
214
215 if (next_ret) {
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *next_ret = prev;
222 }
223 return NULL;
224}
225
Chris Mason80ea96b2008-02-01 14:51:59 -0500226static inline struct rb_node *tree_search(struct extent_io_tree *tree,
227 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500228{
Chris Mason70dec802008-01-29 09:59:12 -0500229 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500230 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500231
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500233 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500234 return prev;
235 return ret;
236}
237
Josef Bacik9ed74f22009-09-11 16:12:44 -0400238static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
239 struct extent_state *other)
240{
241 if (tree->ops && tree->ops->merge_extent_hook)
242 tree->ops->merge_extent_hook(tree->mapping->host, new,
243 other);
244}
245
Chris Masond1310b22008-01-24 16:13:08 -0500246/*
247 * utility function to look for merge candidates inside a given range.
248 * Any extents with matching state are merged together into a single
249 * extent in the tree. Extents with EXTENT_IO in their state field
250 * are not merged because the end_io handlers need to be able to do
251 * operations on them without sleeping (or doing allocations/splits).
252 *
253 * This should be called with the tree lock held.
254 */
255static int merge_state(struct extent_io_tree *tree,
256 struct extent_state *state)
257{
258 struct extent_state *other;
259 struct rb_node *other_node;
260
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400261 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500262 return 0;
263
264 other_node = rb_prev(&state->rb_node);
265 if (other_node) {
266 other = rb_entry(other_node, struct extent_state, rb_node);
267 if (other->end == state->start - 1 &&
268 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400269 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500270 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500271 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500272 rb_erase(&other->rb_node, &tree->state);
273 free_extent_state(other);
274 }
275 }
276 other_node = rb_next(&state->rb_node);
277 if (other_node) {
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->start == state->end + 1 &&
280 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400281 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500282 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500283 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500284 rb_erase(&state->rb_node, &tree->state);
285 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400286 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500287 }
288 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289
Chris Masond1310b22008-01-24 16:13:08 -0500290 return 0;
291}
292
Josef Bacik9ed74f22009-09-11 16:12:44 -0400293static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400294 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500295{
296 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400297 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400298 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500299 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400300
301 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500302}
303
304static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
Chris Masond1310b22008-01-24 16:13:08 -0500311/*
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
314 *
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
317 *
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
320 */
321static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400323 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500324{
325 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400326 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400327 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500328
329 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500330 printk(KERN_ERR "btrfs end < start %llu %llu\n",
331 (unsigned long long)end,
332 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500333 WARN_ON(1);
334 }
Chris Masond1310b22008-01-24 16:13:08 -0500335 state->start = start;
336 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400337 ret = set_state_cb(tree, state, bits);
338 if (ret)
339 return ret;
340
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400341 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400342 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400343 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500344 node = tree_insert(&tree->state, end, &state->rb_node);
345 if (node) {
346 struct extent_state *found;
347 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500348 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found->start,
350 (unsigned long long)found->end,
351 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500352 free_extent_state(state);
353 return -EEXIST;
354 }
Chris Mason70dec802008-01-29 09:59:12 -0500355 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500356 merge_state(tree, state);
357 return 0;
358}
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
361 u64 split)
362{
363 if (tree->ops && tree->ops->split_extent_hook)
364 return tree->ops->split_extent_hook(tree->mapping->host,
365 orig, split);
366 return 0;
367}
368
Chris Masond1310b22008-01-24 16:13:08 -0500369/*
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
373 *
374 * Before calling,
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
379 *
380 * The tree locks are not taken by this function. They need to be held
381 * by the caller.
382 */
383static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
385{
386 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387
388 split_cb(tree, orig, split);
389
Chris Masond1310b22008-01-24 16:13:08 -0500390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
393 orig->start = split;
394
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
396 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500397 free_extent_state(prealloc);
398 return -EEXIST;
399 }
Chris Mason70dec802008-01-29 09:59:12 -0500400 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500401 return 0;
402}
403
404/*
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
408 *
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
411 */
412static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 struct extent_state *state,
414 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500415{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400416 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400417 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500418
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400419 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500420 u64 range = state->end - state->start + 1;
421 WARN_ON(range > tree->dirty_bytes);
422 tree->dirty_bytes -= range;
423 }
Chris Mason291d6732008-01-29 15:55:23 -0500424 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400425 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500426 if (wake)
427 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400428 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500429 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500430 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500431 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500432 free_extent_state(state);
433 } else {
434 WARN_ON(1);
435 }
436 } else {
437 merge_state(tree, state);
438 }
439 return ret;
440}
441
442/*
443 * clear some bits on a range in the tree. This may require splitting
444 * or inserting elements in the tree, so the gfp mask is used to
445 * indicate which allocations or sleeping are allowed.
446 *
447 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
448 * the given range from the tree regardless of state (ie for truncate).
449 *
450 * the range [start, end] is inclusive.
451 *
452 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
453 * bits were already set, or zero if none of the bits were already set.
454 */
455int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400456 int bits, int wake, int delete,
457 struct extent_state **cached_state,
458 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500459{
460 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400461 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500462 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400463 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500464 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400465 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500466 int err;
467 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000468 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500469
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400470 if (delete)
471 bits |= ~EXTENT_CTLBITS;
472 bits |= EXTENT_FIRST_DELALLOC;
473
Josef Bacik2ac55d42010-02-03 19:33:23 +0000474 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
475 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500476again:
477 if (!prealloc && (mask & __GFP_WAIT)) {
478 prealloc = alloc_extent_state(mask);
479 if (!prealloc)
480 return -ENOMEM;
481 }
482
Chris Masoncad321a2008-12-17 14:51:42 -0500483 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400484 if (cached_state) {
485 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000486
487 if (clear) {
488 *cached_state = NULL;
489 cached_state = NULL;
490 }
491
Chris Mason42daec22009-09-23 19:51:09 -0400492 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000493 if (clear)
494 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400495 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400496 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400497 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000498 if (clear)
499 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400500 }
Chris Masond1310b22008-01-24 16:13:08 -0500501 /*
502 * this search will find the extents that end after
503 * our range starts
504 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500505 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500506 if (!node)
507 goto out;
508 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400509hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500510 if (state->start > end)
511 goto out;
512 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400513 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500514
515 /*
516 * | ---- desired range ---- |
517 * | state | or
518 * | ------------- state -------------- |
519 *
520 * We need to split the extent we found, and may flip
521 * bits on second half.
522 *
523 * If the extent we found extends past our range, we
524 * just split and search again. It'll get split again
525 * the next time though.
526 *
527 * If the extent we found is inside our range, we clear
528 * the desired bit on it.
529 */
530
531 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500532 if (!prealloc)
533 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500534 err = split_state(tree, state, prealloc, start);
535 BUG_ON(err == -EEXIST);
536 prealloc = NULL;
537 if (err)
538 goto out;
539 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400540 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400541 if (last_end == (u64)-1)
542 goto out;
543 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500544 }
545 goto search_again;
546 }
547 /*
548 * | ---- desired range ---- |
549 * | state |
550 * We need to split the extent, and clear the bit
551 * on the first half
552 */
553 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500554 if (!prealloc)
555 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500556 err = split_state(tree, state, prealloc, end + 1);
557 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500558 if (wake)
559 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400560
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400561 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400562
Chris Masond1310b22008-01-24 16:13:08 -0500563 prealloc = NULL;
564 goto out;
565 }
Chris Mason42daec22009-09-23 19:51:09 -0400566
Chris Mason2c64c532009-09-02 15:04:12 -0400567 if (state->end < end && prealloc && !need_resched())
568 next_node = rb_next(&state->rb_node);
569 else
570 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400571
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400572 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400573 if (last_end == (u64)-1)
574 goto out;
575 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400576 if (start <= end && next_node) {
577 state = rb_entry(next_node, struct extent_state,
578 rb_node);
579 if (state->start == start)
580 goto hit_next;
581 }
Chris Masond1310b22008-01-24 16:13:08 -0500582 goto search_again;
583
584out:
Chris Masoncad321a2008-12-17 14:51:42 -0500585 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500586 if (prealloc)
587 free_extent_state(prealloc);
588
589 return set;
590
591search_again:
592 if (start > end)
593 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500594 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500595 if (mask & __GFP_WAIT)
596 cond_resched();
597 goto again;
598}
Chris Masond1310b22008-01-24 16:13:08 -0500599
600static int wait_on_state(struct extent_io_tree *tree,
601 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500602 __releases(tree->lock)
603 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500604{
605 DEFINE_WAIT(wait);
606 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500607 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500608 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500609 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500610 finish_wait(&state->wq, &wait);
611 return 0;
612}
613
614/*
615 * waits for one or more bits to clear on a range in the state tree.
616 * The range [start, end] is inclusive.
617 * The tree lock is taken by this function
618 */
619int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
620{
621 struct extent_state *state;
622 struct rb_node *node;
623
Chris Masoncad321a2008-12-17 14:51:42 -0500624 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500625again:
626 while (1) {
627 /*
628 * this search will find all the extents that end after
629 * our range starts
630 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500631 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500632 if (!node)
633 break;
634
635 state = rb_entry(node, struct extent_state, rb_node);
636
637 if (state->start > end)
638 goto out;
639
640 if (state->state & bits) {
641 start = state->start;
642 atomic_inc(&state->refs);
643 wait_on_state(tree, state);
644 free_extent_state(state);
645 goto again;
646 }
647 start = state->end + 1;
648
649 if (start > end)
650 break;
651
652 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500653 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500655 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500656 }
657 }
658out:
Chris Masoncad321a2008-12-17 14:51:42 -0500659 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500660 return 0;
661}
Chris Masond1310b22008-01-24 16:13:08 -0500662
Josef Bacik9ed74f22009-09-11 16:12:44 -0400663static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500664 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400665 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500666{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400667 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400668 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400669
670 ret = set_state_cb(tree, state, bits);
671 if (ret)
672 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400673 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500674 u64 range = state->end - state->start + 1;
675 tree->dirty_bytes += range;
676 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400677 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400678
679 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500680}
681
Chris Mason2c64c532009-09-02 15:04:12 -0400682static void cache_state(struct extent_state *state,
683 struct extent_state **cached_ptr)
684{
685 if (cached_ptr && !(*cached_ptr)) {
686 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
687 *cached_ptr = state;
688 atomic_inc(&state->refs);
689 }
690 }
691}
692
Arne Jansen507903b2011-04-06 10:02:20 +0000693static void uncache_state(struct extent_state **cached_ptr)
694{
695 if (cached_ptr && (*cached_ptr)) {
696 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400697 *cached_ptr = NULL;
698 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000699 }
700}
701
Chris Masond1310b22008-01-24 16:13:08 -0500702/*
Chris Mason1edbb732009-09-02 13:24:36 -0400703 * set some bits on a range in the tree. This may require allocations or
704 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500705 *
Chris Mason1edbb732009-09-02 13:24:36 -0400706 * If any of the exclusive bits are set, this will fail with -EEXIST if some
707 * part of the range already has the desired bits set. The start of the
708 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500709 *
Chris Mason1edbb732009-09-02 13:24:36 -0400710 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500711 */
Chris Mason1edbb732009-09-02 13:24:36 -0400712
Chris Mason4845e442010-05-25 20:56:50 -0400713int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
714 int bits, int exclusive_bits, u64 *failed_start,
715 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500716{
717 struct extent_state *state;
718 struct extent_state *prealloc = NULL;
719 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500720 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500721 u64 last_start;
722 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400723
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400724 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500725again:
726 if (!prealloc && (mask & __GFP_WAIT)) {
727 prealloc = alloc_extent_state(mask);
728 if (!prealloc)
729 return -ENOMEM;
730 }
731
Chris Masoncad321a2008-12-17 14:51:42 -0500732 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400733 if (cached_state && *cached_state) {
734 state = *cached_state;
735 if (state->start == start && state->tree) {
736 node = &state->rb_node;
737 goto hit_next;
738 }
739 }
Chris Masond1310b22008-01-24 16:13:08 -0500740 /*
741 * this search will find all the extents that end after
742 * our range starts.
743 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500744 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500745 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400746 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500747 prealloc = NULL;
748 BUG_ON(err == -EEXIST);
749 goto out;
750 }
Chris Masond1310b22008-01-24 16:13:08 -0500751 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400752hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500753 last_start = state->start;
754 last_end = state->end;
755
756 /*
757 * | ---- desired range ---- |
758 * | state |
759 *
760 * Just lock what we found and keep going
761 */
762 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400763 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400764 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500765 *failed_start = state->start;
766 err = -EEXIST;
767 goto out;
768 }
Chris Mason42daec22009-09-23 19:51:09 -0400769
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400770 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400771 if (err)
772 goto out;
773
Chris Mason2c64c532009-09-02 15:04:12 -0400774 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500775 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400776 if (last_end == (u64)-1)
777 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400778
Yan Zheng5c939df2009-05-27 09:16:03 -0400779 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400780 if (start < end && prealloc && !need_resched()) {
781 next_node = rb_next(node);
782 if (next_node) {
783 state = rb_entry(next_node, struct extent_state,
784 rb_node);
785 if (state->start == start)
786 goto hit_next;
787 }
788 }
Chris Masond1310b22008-01-24 16:13:08 -0500789 goto search_again;
790 }
791
792 /*
793 * | ---- desired range ---- |
794 * | state |
795 * or
796 * | ------------- state -------------- |
797 *
798 * We need to split the extent we found, and may flip bits on
799 * second half.
800 *
801 * If the extent we found extends past our
802 * range, we just split and search again. It'll get split
803 * again the next time though.
804 *
805 * If the extent we found is inside our range, we set the
806 * desired bit on it.
807 */
808 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400809 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500810 *failed_start = start;
811 err = -EEXIST;
812 goto out;
813 }
814 err = split_state(tree, state, prealloc, start);
815 BUG_ON(err == -EEXIST);
816 prealloc = NULL;
817 if (err)
818 goto out;
819 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400820 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400821 if (err)
822 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400823 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500824 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400825 if (last_end == (u64)-1)
826 goto out;
827 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500828 }
829 goto search_again;
830 }
831 /*
832 * | ---- desired range ---- |
833 * | state | or | state |
834 *
835 * There's a hole, we need to insert something in it and
836 * ignore the extent we found.
837 */
838 if (state->start > start) {
839 u64 this_end;
840 if (end < last_start)
841 this_end = end;
842 else
Chris Masond3977122009-01-05 21:25:51 -0500843 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500844 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400845 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400846 BUG_ON(err == -EEXIST);
847 if (err) {
848 prealloc = NULL;
849 goto out;
850 }
Chris Mason2c64c532009-09-02 15:04:12 -0400851 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500852 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500853 start = this_end + 1;
854 goto search_again;
855 }
856 /*
857 * | ---- desired range ---- |
858 * | state |
859 * We need to split the extent, and set the bit
860 * on the first half
861 */
862 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400863 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500864 *failed_start = start;
865 err = -EEXIST;
866 goto out;
867 }
868 err = split_state(tree, state, prealloc, end + 1);
869 BUG_ON(err == -EEXIST);
870
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400871 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400872 if (err) {
873 prealloc = NULL;
874 goto out;
875 }
Chris Mason2c64c532009-09-02 15:04:12 -0400876 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500877 merge_state(tree, prealloc);
878 prealloc = NULL;
879 goto out;
880 }
881
882 goto search_again;
883
884out:
Chris Masoncad321a2008-12-17 14:51:42 -0500885 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500886 if (prealloc)
887 free_extent_state(prealloc);
888
889 return err;
890
891search_again:
892 if (start > end)
893 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500894 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500895 if (mask & __GFP_WAIT)
896 cond_resched();
897 goto again;
898}
Chris Masond1310b22008-01-24 16:13:08 -0500899
900/* wrappers around set/clear extent bit */
901int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
902 gfp_t mask)
903{
904 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400905 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500906}
Chris Masond1310b22008-01-24 16:13:08 -0500907
908int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
909 int bits, gfp_t mask)
910{
911 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400912 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500913}
Chris Masond1310b22008-01-24 16:13:08 -0500914
915int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
916 int bits, gfp_t mask)
917{
Chris Mason2c64c532009-09-02 15:04:12 -0400918 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500919}
Chris Masond1310b22008-01-24 16:13:08 -0500920
921int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000922 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500923{
924 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400925 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000926 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500927}
Chris Masond1310b22008-01-24 16:13:08 -0500928
929int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
930 gfp_t mask)
931{
932 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400933 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400934 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500935}
Chris Masond1310b22008-01-24 16:13:08 -0500936
937int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
938 gfp_t mask)
939{
940 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400941 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500942}
Chris Masond1310b22008-01-24 16:13:08 -0500943
Chris Masond1310b22008-01-24 16:13:08 -0500944int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000945 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500946{
Arne Jansen507903b2011-04-06 10:02:20 +0000947 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
948 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500949}
Chris Masond1310b22008-01-24 16:13:08 -0500950
Chris Masond3977122009-01-05 21:25:51 -0500951static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000952 u64 end, struct extent_state **cached_state,
953 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500954{
Chris Mason2c64c532009-09-02 15:04:12 -0400955 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000956 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500957}
Chris Masond1310b22008-01-24 16:13:08 -0500958
Chris Masond352ac62008-09-29 15:18:18 -0400959/*
960 * either insert or lock state struct between start and end use mask to tell
961 * us if waiting is desired.
962 */
Chris Mason1edbb732009-09-02 13:24:36 -0400963int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400964 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500965{
966 int err;
967 u64 failed_start;
968 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400969 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400970 EXTENT_LOCKED, &failed_start,
971 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500972 if (err == -EEXIST && (mask & __GFP_WAIT)) {
973 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
974 start = failed_start;
975 } else {
976 break;
977 }
978 WARN_ON(start > end);
979 }
980 return err;
981}
Chris Masond1310b22008-01-24 16:13:08 -0500982
Chris Mason1edbb732009-09-02 13:24:36 -0400983int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
984{
Chris Mason2c64c532009-09-02 15:04:12 -0400985 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400986}
987
Josef Bacik25179202008-10-29 14:49:05 -0400988int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
989 gfp_t mask)
990{
991 int err;
992 u64 failed_start;
993
Chris Mason2c64c532009-09-02 15:04:12 -0400994 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
995 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400996 if (err == -EEXIST) {
997 if (failed_start > start)
998 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -0400999 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001000 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001001 }
Josef Bacik25179202008-10-29 14:49:05 -04001002 return 1;
1003}
Josef Bacik25179202008-10-29 14:49:05 -04001004
Chris Mason2c64c532009-09-02 15:04:12 -04001005int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1006 struct extent_state **cached, gfp_t mask)
1007{
1008 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1009 mask);
1010}
1011
Arne Jansen507903b2011-04-06 10:02:20 +00001012int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001013{
Chris Mason2c64c532009-09-02 15:04:12 -04001014 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1015 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001016}
Chris Masond1310b22008-01-24 16:13:08 -05001017
1018/*
Chris Masond1310b22008-01-24 16:13:08 -05001019 * helper function to set both pages and extents in the tree writeback
1020 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001021static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001022{
1023 unsigned long index = start >> PAGE_CACHE_SHIFT;
1024 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1025 struct page *page;
1026
1027 while (index <= end_index) {
1028 page = find_get_page(tree->mapping, index);
1029 BUG_ON(!page);
1030 set_page_writeback(page);
1031 page_cache_release(page);
1032 index++;
1033 }
Chris Masond1310b22008-01-24 16:13:08 -05001034 return 0;
1035}
Chris Masond1310b22008-01-24 16:13:08 -05001036
Chris Masond352ac62008-09-29 15:18:18 -04001037/*
1038 * find the first offset in the io tree with 'bits' set. zero is
1039 * returned if we find something, and *start_ret and *end_ret are
1040 * set to reflect the state struct that was found.
1041 *
1042 * If nothing was found, 1 is returned, < 0 on error
1043 */
Chris Masond1310b22008-01-24 16:13:08 -05001044int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1045 u64 *start_ret, u64 *end_ret, int bits)
1046{
1047 struct rb_node *node;
1048 struct extent_state *state;
1049 int ret = 1;
1050
Chris Masoncad321a2008-12-17 14:51:42 -05001051 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001052 /*
1053 * this search will find all the extents that end after
1054 * our range starts.
1055 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001056 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001057 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001058 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001059
Chris Masond3977122009-01-05 21:25:51 -05001060 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001061 state = rb_entry(node, struct extent_state, rb_node);
1062 if (state->end >= start && (state->state & bits)) {
1063 *start_ret = state->start;
1064 *end_ret = state->end;
1065 ret = 0;
1066 break;
1067 }
1068 node = rb_next(node);
1069 if (!node)
1070 break;
1071 }
1072out:
Chris Masoncad321a2008-12-17 14:51:42 -05001073 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001074 return ret;
1075}
Chris Masond1310b22008-01-24 16:13:08 -05001076
Chris Masond352ac62008-09-29 15:18:18 -04001077/* find the first state struct with 'bits' set after 'start', and
1078 * return it. tree->lock must be held. NULL will returned if
1079 * nothing was found after 'start'
1080 */
Chris Masond7fc6402008-02-18 12:12:38 -05001081struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1082 u64 start, int bits)
1083{
1084 struct rb_node *node;
1085 struct extent_state *state;
1086
1087 /*
1088 * this search will find all the extents that end after
1089 * our range starts.
1090 */
1091 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001092 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001093 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001094
Chris Masond3977122009-01-05 21:25:51 -05001095 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001096 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001097 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001098 return state;
Chris Masond3977122009-01-05 21:25:51 -05001099
Chris Masond7fc6402008-02-18 12:12:38 -05001100 node = rb_next(node);
1101 if (!node)
1102 break;
1103 }
1104out:
1105 return NULL;
1106}
Chris Masond7fc6402008-02-18 12:12:38 -05001107
Chris Masond352ac62008-09-29 15:18:18 -04001108/*
1109 * find a contiguous range of bytes in the file marked as delalloc, not
1110 * more than 'max_bytes'. start and end are used to return the range,
1111 *
1112 * 1 is returned if we find something, 0 if nothing was in the tree
1113 */
Chris Masonc8b97812008-10-29 14:49:59 -04001114static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001115 u64 *start, u64 *end, u64 max_bytes,
1116 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001117{
1118 struct rb_node *node;
1119 struct extent_state *state;
1120 u64 cur_start = *start;
1121 u64 found = 0;
1122 u64 total_bytes = 0;
1123
Chris Masoncad321a2008-12-17 14:51:42 -05001124 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001125
Chris Masond1310b22008-01-24 16:13:08 -05001126 /*
1127 * this search will find all the extents that end after
1128 * our range starts.
1129 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001130 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001131 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001132 if (!found)
1133 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001134 goto out;
1135 }
1136
Chris Masond3977122009-01-05 21:25:51 -05001137 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001138 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001139 if (found && (state->start != cur_start ||
1140 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001141 goto out;
1142 }
1143 if (!(state->state & EXTENT_DELALLOC)) {
1144 if (!found)
1145 *end = state->end;
1146 goto out;
1147 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001148 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001149 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001150 *cached_state = state;
1151 atomic_inc(&state->refs);
1152 }
Chris Masond1310b22008-01-24 16:13:08 -05001153 found++;
1154 *end = state->end;
1155 cur_start = state->end + 1;
1156 node = rb_next(node);
1157 if (!node)
1158 break;
1159 total_bytes += state->end - state->start + 1;
1160 if (total_bytes >= max_bytes)
1161 break;
1162 }
1163out:
Chris Masoncad321a2008-12-17 14:51:42 -05001164 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001165 return found;
1166}
1167
Chris Masonc8b97812008-10-29 14:49:59 -04001168static noinline int __unlock_for_delalloc(struct inode *inode,
1169 struct page *locked_page,
1170 u64 start, u64 end)
1171{
1172 int ret;
1173 struct page *pages[16];
1174 unsigned long index = start >> PAGE_CACHE_SHIFT;
1175 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1176 unsigned long nr_pages = end_index - index + 1;
1177 int i;
1178
1179 if (index == locked_page->index && end_index == index)
1180 return 0;
1181
Chris Masond3977122009-01-05 21:25:51 -05001182 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001183 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001184 min_t(unsigned long, nr_pages,
1185 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001186 for (i = 0; i < ret; i++) {
1187 if (pages[i] != locked_page)
1188 unlock_page(pages[i]);
1189 page_cache_release(pages[i]);
1190 }
1191 nr_pages -= ret;
1192 index += ret;
1193 cond_resched();
1194 }
1195 return 0;
1196}
1197
1198static noinline int lock_delalloc_pages(struct inode *inode,
1199 struct page *locked_page,
1200 u64 delalloc_start,
1201 u64 delalloc_end)
1202{
1203 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1204 unsigned long start_index = index;
1205 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1206 unsigned long pages_locked = 0;
1207 struct page *pages[16];
1208 unsigned long nrpages;
1209 int ret;
1210 int i;
1211
1212 /* the caller is responsible for locking the start index */
1213 if (index == locked_page->index && index == end_index)
1214 return 0;
1215
1216 /* skip the page at the start index */
1217 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001218 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001219 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001220 min_t(unsigned long,
1221 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001222 if (ret == 0) {
1223 ret = -EAGAIN;
1224 goto done;
1225 }
1226 /* now we have an array of pages, lock them all */
1227 for (i = 0; i < ret; i++) {
1228 /*
1229 * the caller is taking responsibility for
1230 * locked_page
1231 */
Chris Mason771ed682008-11-06 22:02:51 -05001232 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001233 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001234 if (!PageDirty(pages[i]) ||
1235 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001236 ret = -EAGAIN;
1237 unlock_page(pages[i]);
1238 page_cache_release(pages[i]);
1239 goto done;
1240 }
1241 }
Chris Masonc8b97812008-10-29 14:49:59 -04001242 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001243 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001244 }
Chris Masonc8b97812008-10-29 14:49:59 -04001245 nrpages -= ret;
1246 index += ret;
1247 cond_resched();
1248 }
1249 ret = 0;
1250done:
1251 if (ret && pages_locked) {
1252 __unlock_for_delalloc(inode, locked_page,
1253 delalloc_start,
1254 ((u64)(start_index + pages_locked - 1)) <<
1255 PAGE_CACHE_SHIFT);
1256 }
1257 return ret;
1258}
1259
1260/*
1261 * find a contiguous range of bytes in the file marked as delalloc, not
1262 * more than 'max_bytes'. start and end are used to return the range,
1263 *
1264 * 1 is returned if we find something, 0 if nothing was in the tree
1265 */
1266static noinline u64 find_lock_delalloc_range(struct inode *inode,
1267 struct extent_io_tree *tree,
1268 struct page *locked_page,
1269 u64 *start, u64 *end,
1270 u64 max_bytes)
1271{
1272 u64 delalloc_start;
1273 u64 delalloc_end;
1274 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001275 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001276 int ret;
1277 int loops = 0;
1278
1279again:
1280 /* step one, find a bunch of delalloc bytes starting at start */
1281 delalloc_start = *start;
1282 delalloc_end = 0;
1283 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001284 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001285 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001286 *start = delalloc_start;
1287 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001288 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001289 return found;
1290 }
1291
1292 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001293 * start comes from the offset of locked_page. We have to lock
1294 * pages in order, so we can't process delalloc bytes before
1295 * locked_page
1296 */
Chris Masond3977122009-01-05 21:25:51 -05001297 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001298 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001299
1300 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001301 * make sure to limit the number of pages we try to lock down
1302 * if we're looping.
1303 */
Chris Masond3977122009-01-05 21:25:51 -05001304 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001305 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001306
Chris Masonc8b97812008-10-29 14:49:59 -04001307 /* step two, lock all the pages after the page that has start */
1308 ret = lock_delalloc_pages(inode, locked_page,
1309 delalloc_start, delalloc_end);
1310 if (ret == -EAGAIN) {
1311 /* some of the pages are gone, lets avoid looping by
1312 * shortening the size of the delalloc range we're searching
1313 */
Chris Mason9655d292009-09-02 15:22:30 -04001314 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001315 if (!loops) {
1316 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1317 max_bytes = PAGE_CACHE_SIZE - offset;
1318 loops = 1;
1319 goto again;
1320 } else {
1321 found = 0;
1322 goto out_failed;
1323 }
1324 }
1325 BUG_ON(ret);
1326
1327 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001328 lock_extent_bits(tree, delalloc_start, delalloc_end,
1329 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001330
1331 /* then test to make sure it is all still delalloc */
1332 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001333 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001334 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001335 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1336 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001337 __unlock_for_delalloc(inode, locked_page,
1338 delalloc_start, delalloc_end);
1339 cond_resched();
1340 goto again;
1341 }
Chris Mason9655d292009-09-02 15:22:30 -04001342 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001343 *start = delalloc_start;
1344 *end = delalloc_end;
1345out_failed:
1346 return found;
1347}
1348
1349int extent_clear_unlock_delalloc(struct inode *inode,
1350 struct extent_io_tree *tree,
1351 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001352 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001353{
1354 int ret;
1355 struct page *pages[16];
1356 unsigned long index = start >> PAGE_CACHE_SHIFT;
1357 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1358 unsigned long nr_pages = end_index - index + 1;
1359 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001360 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001361
Chris Masona791e352009-10-08 11:27:10 -04001362 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001363 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001364 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001365 clear_bits |= EXTENT_DIRTY;
1366
Chris Masona791e352009-10-08 11:27:10 -04001367 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001368 clear_bits |= EXTENT_DELALLOC;
1369
Chris Mason2c64c532009-09-02 15:04:12 -04001370 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001371 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1372 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1373 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001374 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001375
Chris Masond3977122009-01-05 21:25:51 -05001376 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001377 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001378 min_t(unsigned long,
1379 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001380 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001381
Chris Masona791e352009-10-08 11:27:10 -04001382 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001383 SetPagePrivate2(pages[i]);
1384
Chris Masonc8b97812008-10-29 14:49:59 -04001385 if (pages[i] == locked_page) {
1386 page_cache_release(pages[i]);
1387 continue;
1388 }
Chris Masona791e352009-10-08 11:27:10 -04001389 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001390 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001391 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001392 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001393 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001394 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001395 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001396 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001397 page_cache_release(pages[i]);
1398 }
1399 nr_pages -= ret;
1400 index += ret;
1401 cond_resched();
1402 }
1403 return 0;
1404}
Chris Masonc8b97812008-10-29 14:49:59 -04001405
Chris Masond352ac62008-09-29 15:18:18 -04001406/*
1407 * count the number of bytes in the tree that have a given bit(s)
1408 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1409 * cached. The total number found is returned.
1410 */
Chris Masond1310b22008-01-24 16:13:08 -05001411u64 count_range_bits(struct extent_io_tree *tree,
1412 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001413 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001414{
1415 struct rb_node *node;
1416 struct extent_state *state;
1417 u64 cur_start = *start;
1418 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001419 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001420 int found = 0;
1421
1422 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001423 WARN_ON(1);
1424 return 0;
1425 }
1426
Chris Masoncad321a2008-12-17 14:51:42 -05001427 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001428 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1429 total_bytes = tree->dirty_bytes;
1430 goto out;
1431 }
1432 /*
1433 * this search will find all the extents that end after
1434 * our range starts.
1435 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001436 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001437 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001438 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001439
Chris Masond3977122009-01-05 21:25:51 -05001440 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001441 state = rb_entry(node, struct extent_state, rb_node);
1442 if (state->start > search_end)
1443 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001444 if (contig && found && state->start > last + 1)
1445 break;
1446 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001447 total_bytes += min(search_end, state->end) + 1 -
1448 max(cur_start, state->start);
1449 if (total_bytes >= max_bytes)
1450 break;
1451 if (!found) {
1452 *start = state->start;
1453 found = 1;
1454 }
Chris Masonec29ed52011-02-23 16:23:20 -05001455 last = state->end;
1456 } else if (contig && found) {
1457 break;
Chris Masond1310b22008-01-24 16:13:08 -05001458 }
1459 node = rb_next(node);
1460 if (!node)
1461 break;
1462 }
1463out:
Chris Masoncad321a2008-12-17 14:51:42 -05001464 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001465 return total_bytes;
1466}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001467
Chris Masond352ac62008-09-29 15:18:18 -04001468/*
1469 * set the private field for a given byte offset in the tree. If there isn't
1470 * an extent_state there already, this does nothing.
1471 */
Chris Masond1310b22008-01-24 16:13:08 -05001472int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1473{
1474 struct rb_node *node;
1475 struct extent_state *state;
1476 int ret = 0;
1477
Chris Masoncad321a2008-12-17 14:51:42 -05001478 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001479 /*
1480 * this search will find all the extents that end after
1481 * our range starts.
1482 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001483 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001484 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001485 ret = -ENOENT;
1486 goto out;
1487 }
1488 state = rb_entry(node, struct extent_state, rb_node);
1489 if (state->start != start) {
1490 ret = -ENOENT;
1491 goto out;
1492 }
1493 state->private = private;
1494out:
Chris Masoncad321a2008-12-17 14:51:42 -05001495 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001496 return ret;
1497}
1498
1499int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1500{
1501 struct rb_node *node;
1502 struct extent_state *state;
1503 int ret = 0;
1504
Chris Masoncad321a2008-12-17 14:51:42 -05001505 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001506 /*
1507 * this search will find all the extents that end after
1508 * our range starts.
1509 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001510 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001511 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001512 ret = -ENOENT;
1513 goto out;
1514 }
1515 state = rb_entry(node, struct extent_state, rb_node);
1516 if (state->start != start) {
1517 ret = -ENOENT;
1518 goto out;
1519 }
1520 *private = state->private;
1521out:
Chris Masoncad321a2008-12-17 14:51:42 -05001522 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001523 return ret;
1524}
1525
1526/*
1527 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001528 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001529 * has the bits set. Otherwise, 1 is returned if any bit in the
1530 * range is found set.
1531 */
1532int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001533 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001534{
1535 struct extent_state *state = NULL;
1536 struct rb_node *node;
1537 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001538
Chris Masoncad321a2008-12-17 14:51:42 -05001539 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001540 if (cached && cached->tree && cached->start == start)
1541 node = &cached->rb_node;
1542 else
1543 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001544 while (node && start <= end) {
1545 state = rb_entry(node, struct extent_state, rb_node);
1546
1547 if (filled && state->start > start) {
1548 bitset = 0;
1549 break;
1550 }
1551
1552 if (state->start > end)
1553 break;
1554
1555 if (state->state & bits) {
1556 bitset = 1;
1557 if (!filled)
1558 break;
1559 } else if (filled) {
1560 bitset = 0;
1561 break;
1562 }
Chris Mason46562ce2009-09-23 20:23:16 -04001563
1564 if (state->end == (u64)-1)
1565 break;
1566
Chris Masond1310b22008-01-24 16:13:08 -05001567 start = state->end + 1;
1568 if (start > end)
1569 break;
1570 node = rb_next(node);
1571 if (!node) {
1572 if (filled)
1573 bitset = 0;
1574 break;
1575 }
1576 }
Chris Masoncad321a2008-12-17 14:51:42 -05001577 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001578 return bitset;
1579}
Chris Masond1310b22008-01-24 16:13:08 -05001580
1581/*
1582 * helper function to set a given page up to date if all the
1583 * extents in the tree for that page are up to date
1584 */
1585static int check_page_uptodate(struct extent_io_tree *tree,
1586 struct page *page)
1587{
1588 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1589 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001590 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001591 SetPageUptodate(page);
1592 return 0;
1593}
1594
1595/*
1596 * helper function to unlock a page if all the extents in the tree
1597 * for that page are unlocked
1598 */
1599static int check_page_locked(struct extent_io_tree *tree,
1600 struct page *page)
1601{
1602 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1603 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001604 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001605 unlock_page(page);
1606 return 0;
1607}
1608
1609/*
1610 * helper function to end page writeback if all the extents
1611 * in the tree for that page are done with writeback
1612 */
1613static int check_page_writeback(struct extent_io_tree *tree,
1614 struct page *page)
1615{
Chris Mason1edbb732009-09-02 13:24:36 -04001616 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001617 return 0;
1618}
1619
1620/* lots and lots of room for performance fixes in the end_bio funcs */
1621
1622/*
1623 * after a writepage IO is done, we need to:
1624 * clear the uptodate bits on error
1625 * clear the writeback bits in the extent tree for this IO
1626 * end_page_writeback if the page has no more pending IO
1627 *
1628 * Scheduling is not allowed, so the extent state tree is expected
1629 * to have one and only one object corresponding to this IO.
1630 */
Chris Masond1310b22008-01-24 16:13:08 -05001631static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001632{
Chris Mason1259ab72008-05-12 13:39:03 -04001633 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001634 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001635 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001636 u64 start;
1637 u64 end;
1638 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001639 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001640
Chris Masond1310b22008-01-24 16:13:08 -05001641 do {
1642 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001643 tree = &BTRFS_I(page->mapping->host)->io_tree;
1644
Chris Masond1310b22008-01-24 16:13:08 -05001645 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1646 bvec->bv_offset;
1647 end = start + bvec->bv_len - 1;
1648
1649 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1650 whole_page = 1;
1651 else
1652 whole_page = 0;
1653
1654 if (--bvec >= bio->bi_io_vec)
1655 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001656 if (tree->ops && tree->ops->writepage_end_io_hook) {
1657 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001658 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001659 if (ret)
1660 uptodate = 0;
1661 }
1662
1663 if (!uptodate && tree->ops &&
1664 tree->ops->writepage_io_failed_hook) {
1665 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001666 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001667 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001668 uptodate = (err == 0);
1669 continue;
1670 }
1671 }
1672
Chris Masond1310b22008-01-24 16:13:08 -05001673 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001674 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001675 ClearPageUptodate(page);
1676 SetPageError(page);
1677 }
Chris Mason70dec802008-01-29 09:59:12 -05001678
Chris Masond1310b22008-01-24 16:13:08 -05001679 if (whole_page)
1680 end_page_writeback(page);
1681 else
1682 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001683 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001684
Chris Masond1310b22008-01-24 16:13:08 -05001685 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001686}
1687
1688/*
1689 * after a readpage IO is done, we need to:
1690 * clear the uptodate bits on error
1691 * set the uptodate bits if things worked
1692 * set the page up to date if all extents in the tree are uptodate
1693 * clear the lock bit in the extent tree
1694 * unlock the page if there are no other extents locked for it
1695 *
1696 * Scheduling is not allowed, so the extent state tree is expected
1697 * to have one and only one object corresponding to this IO.
1698 */
Chris Masond1310b22008-01-24 16:13:08 -05001699static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001700{
1701 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001702 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1703 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001704 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001705 u64 start;
1706 u64 end;
1707 int whole_page;
1708 int ret;
1709
Chris Masond20f7042008-12-08 16:58:54 -05001710 if (err)
1711 uptodate = 0;
1712
Chris Masond1310b22008-01-24 16:13:08 -05001713 do {
1714 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00001715 struct extent_state *cached = NULL;
1716 struct extent_state *state;
1717
David Woodhouse902b22f2008-08-20 08:51:49 -04001718 tree = &BTRFS_I(page->mapping->host)->io_tree;
1719
Chris Masond1310b22008-01-24 16:13:08 -05001720 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1721 bvec->bv_offset;
1722 end = start + bvec->bv_len - 1;
1723
1724 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1725 whole_page = 1;
1726 else
1727 whole_page = 0;
1728
Chris Mason4125bf72010-02-03 18:18:45 +00001729 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001730 prefetchw(&bvec->bv_page->flags);
1731
Arne Jansen507903b2011-04-06 10:02:20 +00001732 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04001733 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04001734 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00001735 /*
1736 * take a reference on the state, unlock will drop
1737 * the ref
1738 */
1739 cache_state(state, &cached);
1740 }
1741 spin_unlock(&tree->lock);
1742
Chris Masond1310b22008-01-24 16:13:08 -05001743 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001744 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00001745 state);
Chris Masond1310b22008-01-24 16:13:08 -05001746 if (ret)
1747 uptodate = 0;
1748 }
Chris Mason7e383262008-04-09 16:28:12 -04001749 if (!uptodate && tree->ops &&
1750 tree->ops->readpage_io_failed_hook) {
1751 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001752 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001753 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001754 uptodate =
1755 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001756 if (err)
1757 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00001758 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04001759 continue;
1760 }
1761 }
Chris Mason70dec802008-01-29 09:59:12 -05001762
Chris Mason771ed682008-11-06 22:02:51 -05001763 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00001764 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04001765 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001766 }
Arne Jansen507903b2011-04-06 10:02:20 +00001767 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001768
Chris Mason70dec802008-01-29 09:59:12 -05001769 if (whole_page) {
1770 if (uptodate) {
1771 SetPageUptodate(page);
1772 } else {
1773 ClearPageUptodate(page);
1774 SetPageError(page);
1775 }
Chris Masond1310b22008-01-24 16:13:08 -05001776 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001777 } else {
1778 if (uptodate) {
1779 check_page_uptodate(tree, page);
1780 } else {
1781 ClearPageUptodate(page);
1782 SetPageError(page);
1783 }
Chris Masond1310b22008-01-24 16:13:08 -05001784 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001785 }
Chris Mason4125bf72010-02-03 18:18:45 +00001786 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001787
1788 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001789}
1790
Miao Xie88f794e2010-11-22 03:02:55 +00001791struct bio *
1792btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1793 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001794{
1795 struct bio *bio;
1796
1797 bio = bio_alloc(gfp_flags, nr_vecs);
1798
1799 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1800 while (!bio && (nr_vecs /= 2))
1801 bio = bio_alloc(gfp_flags, nr_vecs);
1802 }
1803
1804 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001805 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001806 bio->bi_bdev = bdev;
1807 bio->bi_sector = first_sector;
1808 }
1809 return bio;
1810}
1811
Chris Masonc8b97812008-10-29 14:49:59 -04001812static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1813 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001814{
Chris Masond1310b22008-01-24 16:13:08 -05001815 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001816 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1817 struct page *page = bvec->bv_page;
1818 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001819 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05001820
1821 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001822
David Woodhouse902b22f2008-08-20 08:51:49 -04001823 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001824
1825 bio_get(bio);
1826
Chris Mason065631f2008-02-20 12:07:25 -05001827 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00001828 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001829 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001830 else
1831 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001832 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1833 ret = -EOPNOTSUPP;
1834 bio_put(bio);
1835 return ret;
1836}
1837
1838static int submit_extent_page(int rw, struct extent_io_tree *tree,
1839 struct page *page, sector_t sector,
1840 size_t size, unsigned long offset,
1841 struct block_device *bdev,
1842 struct bio **bio_ret,
1843 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001844 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001845 int mirror_num,
1846 unsigned long prev_bio_flags,
1847 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001848{
1849 int ret = 0;
1850 struct bio *bio;
1851 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001852 int contig = 0;
1853 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1854 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001855 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001856
1857 if (bio_ret && *bio_ret) {
1858 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001859 if (old_compressed)
1860 contig = bio->bi_sector == sector;
1861 else
1862 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1863 sector;
1864
1865 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001866 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001867 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1868 bio_flags)) ||
1869 bio_add_page(bio, page, page_size, offset) < page_size) {
1870 ret = submit_one_bio(rw, bio, mirror_num,
1871 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001872 bio = NULL;
1873 } else {
1874 return 0;
1875 }
1876 }
Chris Masonc8b97812008-10-29 14:49:59 -04001877 if (this_compressed)
1878 nr = BIO_MAX_PAGES;
1879 else
1880 nr = bio_get_nr_vecs(bdev);
1881
Miao Xie88f794e2010-11-22 03:02:55 +00001882 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00001883 if (!bio)
1884 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05001885
Chris Masonc8b97812008-10-29 14:49:59 -04001886 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001887 bio->bi_end_io = end_io_func;
1888 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001889
Chris Masond3977122009-01-05 21:25:51 -05001890 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001891 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001892 else
Chris Masonc8b97812008-10-29 14:49:59 -04001893 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001894
1895 return ret;
1896}
1897
1898void set_page_extent_mapped(struct page *page)
1899{
1900 if (!PagePrivate(page)) {
1901 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001902 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001903 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001904 }
1905}
1906
Christoph Hellwigb2950862008-12-02 09:54:17 -05001907static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001908{
Chris Masoneb14ab82011-02-10 12:35:00 -05001909 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05001910 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1911}
1912
1913/*
1914 * basic readpage implementation. Locked extent state structs are inserted
1915 * into the tree that are removed when the IO is done (by the end_io
1916 * handlers)
1917 */
1918static int __extent_read_full_page(struct extent_io_tree *tree,
1919 struct page *page,
1920 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04001921 struct bio **bio, int mirror_num,
1922 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001923{
1924 struct inode *inode = page->mapping->host;
1925 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1926 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1927 u64 end;
1928 u64 cur = start;
1929 u64 extent_offset;
1930 u64 last_byte = i_size_read(inode);
1931 u64 block_start;
1932 u64 cur_end;
1933 sector_t sector;
1934 struct extent_map *em;
1935 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001936 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05001937 int ret;
1938 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02001939 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001940 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04001941 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05001942 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04001943 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001944
1945 set_page_extent_mapped(page);
1946
1947 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001948 while (1) {
1949 lock_extent(tree, start, end, GFP_NOFS);
1950 ordered = btrfs_lookup_ordered_extent(inode, start);
1951 if (!ordered)
1952 break;
1953 unlock_extent(tree, start, end, GFP_NOFS);
1954 btrfs_start_ordered_extent(inode, ordered, 1);
1955 btrfs_put_ordered_extent(ordered);
1956 }
Chris Masond1310b22008-01-24 16:13:08 -05001957
Chris Masonc8b97812008-10-29 14:49:59 -04001958 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
1959 char *userpage;
1960 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
1961
1962 if (zero_offset) {
1963 iosize = PAGE_CACHE_SIZE - zero_offset;
1964 userpage = kmap_atomic(page, KM_USER0);
1965 memset(userpage + zero_offset, 0, iosize);
1966 flush_dcache_page(page);
1967 kunmap_atomic(userpage, KM_USER0);
1968 }
1969 }
Chris Masond1310b22008-01-24 16:13:08 -05001970 while (cur <= end) {
1971 if (cur >= last_byte) {
1972 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00001973 struct extent_state *cached = NULL;
1974
David Sterba306e16c2011-04-19 14:29:38 +02001975 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05001976 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02001977 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05001978 flush_dcache_page(page);
1979 kunmap_atomic(userpage, KM_USER0);
1980 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00001981 &cached, GFP_NOFS);
1982 unlock_extent_cached(tree, cur, cur + iosize - 1,
1983 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001984 break;
1985 }
David Sterba306e16c2011-04-19 14:29:38 +02001986 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05001987 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02001988 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05001989 SetPageError(page);
1990 unlock_extent(tree, cur, end, GFP_NOFS);
1991 break;
1992 }
Chris Masond1310b22008-01-24 16:13:08 -05001993 extent_offset = cur - em->start;
1994 BUG_ON(extent_map_end(em) <= cur);
1995 BUG_ON(end < cur);
1996
Li Zefan261507a02010-12-17 14:21:50 +08001997 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04001998 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001999 extent_set_compress_type(&this_bio_flag,
2000 em->compress_type);
2001 }
Chris Masonc8b97812008-10-29 14:49:59 -04002002
Chris Masond1310b22008-01-24 16:13:08 -05002003 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2004 cur_end = min(extent_map_end(em) - 1, end);
2005 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002006 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2007 disk_io_size = em->block_len;
2008 sector = em->block_start >> 9;
2009 } else {
2010 sector = (em->block_start + extent_offset) >> 9;
2011 disk_io_size = iosize;
2012 }
Chris Masond1310b22008-01-24 16:13:08 -05002013 bdev = em->bdev;
2014 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002015 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2016 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002017 free_extent_map(em);
2018 em = NULL;
2019
2020 /* we've found a hole, just zero and go on */
2021 if (block_start == EXTENT_MAP_HOLE) {
2022 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002023 struct extent_state *cached = NULL;
2024
Chris Masond1310b22008-01-24 16:13:08 -05002025 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002026 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002027 flush_dcache_page(page);
2028 kunmap_atomic(userpage, KM_USER0);
2029
2030 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002031 &cached, GFP_NOFS);
2032 unlock_extent_cached(tree, cur, cur + iosize - 1,
2033 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002034 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002035 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002036 continue;
2037 }
2038 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002039 if (test_range_bit(tree, cur, cur_end,
2040 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002041 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002042 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2043 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002044 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002045 continue;
2046 }
Chris Mason70dec802008-01-29 09:59:12 -05002047 /* we have an inline extent but it didn't get marked up
2048 * to date. Error out
2049 */
2050 if (block_start == EXTENT_MAP_INLINE) {
2051 SetPageError(page);
2052 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2053 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002054 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002055 continue;
2056 }
Chris Masond1310b22008-01-24 16:13:08 -05002057
2058 ret = 0;
2059 if (tree->ops && tree->ops->readpage_io_hook) {
2060 ret = tree->ops->readpage_io_hook(page, cur,
2061 cur + iosize - 1);
2062 }
2063 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002064 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2065 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002066 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002067 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002068 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002069 end_bio_extent_readpage, mirror_num,
2070 *bio_flags,
2071 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002072 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002073 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002074 }
2075 if (ret)
2076 SetPageError(page);
2077 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002078 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002079 }
2080 if (!nr) {
2081 if (!PageError(page))
2082 SetPageUptodate(page);
2083 unlock_page(page);
2084 }
2085 return 0;
2086}
2087
2088int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2089 get_extent_t *get_extent)
2090{
2091 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002092 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002093 int ret;
2094
Chris Masonc8b97812008-10-29 14:49:59 -04002095 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2096 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002097 if (bio)
liubo6b82ce82011-01-26 06:21:39 +00002098 ret = submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002099 return ret;
2100}
Chris Masond1310b22008-01-24 16:13:08 -05002101
Chris Mason11c83492009-04-20 15:50:09 -04002102static noinline void update_nr_written(struct page *page,
2103 struct writeback_control *wbc,
2104 unsigned long nr_written)
2105{
2106 wbc->nr_to_write -= nr_written;
2107 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2108 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2109 page->mapping->writeback_index = page->index + nr_written;
2110}
2111
Chris Masond1310b22008-01-24 16:13:08 -05002112/*
2113 * the writepage semantics are similar to regular writepage. extent
2114 * records are inserted to lock ranges in the tree, and as dirty areas
2115 * are found, they are marked writeback. Then the lock bits are removed
2116 * and the end_io handler clears the writeback ranges
2117 */
2118static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2119 void *data)
2120{
2121 struct inode *inode = page->mapping->host;
2122 struct extent_page_data *epd = data;
2123 struct extent_io_tree *tree = epd->tree;
2124 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2125 u64 delalloc_start;
2126 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2127 u64 end;
2128 u64 cur = start;
2129 u64 extent_offset;
2130 u64 last_byte = i_size_read(inode);
2131 u64 block_start;
2132 u64 iosize;
2133 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002134 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002135 struct extent_map *em;
2136 struct block_device *bdev;
2137 int ret;
2138 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002139 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002140 size_t blocksize;
2141 loff_t i_size = i_size_read(inode);
2142 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2143 u64 nr_delalloc;
2144 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002145 int page_started;
2146 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002147 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002148 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002149
Chris Masonffbd5172009-04-20 15:50:09 -04002150 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002151 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002152 else
2153 write_flags = WRITE;
2154
liubo1abe9b82011-03-24 11:18:59 +00002155 trace___extent_writepage(page, inode, wbc);
2156
Chris Masond1310b22008-01-24 16:13:08 -05002157 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002158 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002159 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002160 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002161 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002162 unlock_page(page);
2163 return 0;
2164 }
2165
2166 if (page->index == end_index) {
2167 char *userpage;
2168
Chris Masond1310b22008-01-24 16:13:08 -05002169 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002170 memset(userpage + pg_offset, 0,
2171 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002172 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002173 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002174 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002175 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002176
2177 set_page_extent_mapped(page);
2178
2179 delalloc_start = start;
2180 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002181 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002182 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002183 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002184 /*
2185 * make sure the wbc mapping index is at least updated
2186 * to this page.
2187 */
2188 update_nr_written(page, wbc, 0);
2189
Chris Masond3977122009-01-05 21:25:51 -05002190 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002191 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002192 page,
2193 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002194 &delalloc_end,
2195 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002196 if (nr_delalloc == 0) {
2197 delalloc_start = delalloc_end + 1;
2198 continue;
2199 }
2200 tree->ops->fill_delalloc(inode, page, delalloc_start,
2201 delalloc_end, &page_started,
2202 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002203 /*
2204 * delalloc_end is already one less than the total
2205 * length, so we don't subtract one from
2206 * PAGE_CACHE_SIZE
2207 */
2208 delalloc_to_write += (delalloc_end - delalloc_start +
2209 PAGE_CACHE_SIZE) >>
2210 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002211 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002212 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002213 if (wbc->nr_to_write < delalloc_to_write) {
2214 int thresh = 8192;
2215
2216 if (delalloc_to_write < thresh * 2)
2217 thresh = delalloc_to_write;
2218 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2219 thresh);
2220 }
Chris Masonc8b97812008-10-29 14:49:59 -04002221
Chris Mason771ed682008-11-06 22:02:51 -05002222 /* did the fill delalloc function already unlock and start
2223 * the IO?
2224 */
2225 if (page_started) {
2226 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002227 /*
2228 * we've unlocked the page, so we can't update
2229 * the mapping's writeback index, just update
2230 * nr_to_write.
2231 */
2232 wbc->nr_to_write -= nr_written;
2233 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002234 }
Chris Masonc8b97812008-10-29 14:49:59 -04002235 }
Chris Mason247e7432008-07-17 12:53:51 -04002236 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002237 ret = tree->ops->writepage_start_hook(page, start,
2238 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002239 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002240 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002241 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002242 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002243 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002244 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002245 }
2246 }
2247
Chris Mason11c83492009-04-20 15:50:09 -04002248 /*
2249 * we don't want to touch the inode after unlocking the page,
2250 * so we update the mapping writeback index now
2251 */
2252 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002253
Chris Masond1310b22008-01-24 16:13:08 -05002254 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002255 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002256 if (tree->ops && tree->ops->writepage_end_io_hook)
2257 tree->ops->writepage_end_io_hook(page, start,
2258 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002259 goto done;
2260 }
2261
Chris Masond1310b22008-01-24 16:13:08 -05002262 blocksize = inode->i_sb->s_blocksize;
2263
2264 while (cur <= end) {
2265 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002266 if (tree->ops && tree->ops->writepage_end_io_hook)
2267 tree->ops->writepage_end_io_hook(page, cur,
2268 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002269 break;
2270 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002271 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002272 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002273 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002274 SetPageError(page);
2275 break;
2276 }
2277
2278 extent_offset = cur - em->start;
2279 BUG_ON(extent_map_end(em) <= cur);
2280 BUG_ON(end < cur);
2281 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2282 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2283 sector = (em->block_start + extent_offset) >> 9;
2284 bdev = em->bdev;
2285 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002286 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002287 free_extent_map(em);
2288 em = NULL;
2289
Chris Masonc8b97812008-10-29 14:49:59 -04002290 /*
2291 * compressed and inline extents are written through other
2292 * paths in the FS
2293 */
2294 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002295 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002296 /*
2297 * end_io notification does not happen here for
2298 * compressed extents
2299 */
2300 if (!compressed && tree->ops &&
2301 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002302 tree->ops->writepage_end_io_hook(page, cur,
2303 cur + iosize - 1,
2304 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002305 else if (compressed) {
2306 /* we don't want to end_page_writeback on
2307 * a compressed extent. this happens
2308 * elsewhere
2309 */
2310 nr++;
2311 }
2312
2313 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002314 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002315 continue;
2316 }
Chris Masond1310b22008-01-24 16:13:08 -05002317 /* leave this out until we have a page_mkwrite call */
2318 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002319 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002320 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002321 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002322 continue;
2323 }
Chris Masonc8b97812008-10-29 14:49:59 -04002324
Chris Masond1310b22008-01-24 16:13:08 -05002325 if (tree->ops && tree->ops->writepage_io_hook) {
2326 ret = tree->ops->writepage_io_hook(page, cur,
2327 cur + iosize - 1);
2328 } else {
2329 ret = 0;
2330 }
Chris Mason1259ab72008-05-12 13:39:03 -04002331 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002332 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002333 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002334 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002335
Chris Masond1310b22008-01-24 16:13:08 -05002336 set_range_writeback(tree, cur, cur + iosize - 1);
2337 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002338 printk(KERN_ERR "btrfs warning page %lu not "
2339 "writeback, cur %llu end %llu\n",
2340 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002341 (unsigned long long)end);
2342 }
2343
Chris Masonffbd5172009-04-20 15:50:09 -04002344 ret = submit_extent_page(write_flags, tree, page,
2345 sector, iosize, pg_offset,
2346 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002347 end_bio_extent_writepage,
2348 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002349 if (ret)
2350 SetPageError(page);
2351 }
2352 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002353 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002354 nr++;
2355 }
2356done:
2357 if (nr == 0) {
2358 /* make sure the mapping tag for page dirty gets cleared */
2359 set_page_writeback(page);
2360 end_page_writeback(page);
2361 }
Chris Masond1310b22008-01-24 16:13:08 -05002362 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002363
Chris Mason11c83492009-04-20 15:50:09 -04002364done_unlocked:
2365
Chris Mason2c64c532009-09-02 15:04:12 -04002366 /* drop our reference on any cached states */
2367 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002368 return 0;
2369}
2370
Chris Masond1310b22008-01-24 16:13:08 -05002371/**
Chris Mason4bef0842008-09-08 11:18:08 -04002372 * 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 -05002373 * @mapping: address space structure to write
2374 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2375 * @writepage: function called for each page
2376 * @data: data passed to writepage function
2377 *
2378 * If a page is already under I/O, write_cache_pages() skips it, even
2379 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2380 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2381 * and msync() need to guarantee that all the data which was dirty at the time
2382 * the call was made get new I/O started against them. If wbc->sync_mode is
2383 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2384 * existing IO to complete.
2385 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002386static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002387 struct address_space *mapping,
2388 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002389 writepage_t writepage, void *data,
2390 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002391{
Chris Masond1310b22008-01-24 16:13:08 -05002392 int ret = 0;
2393 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002394 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002395 struct pagevec pvec;
2396 int nr_pages;
2397 pgoff_t index;
2398 pgoff_t end; /* Inclusive */
2399 int scanned = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002400
Chris Masond1310b22008-01-24 16:13:08 -05002401 pagevec_init(&pvec, 0);
2402 if (wbc->range_cyclic) {
2403 index = mapping->writeback_index; /* Start from prev offset */
2404 end = -1;
2405 } else {
2406 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2407 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002408 scanned = 1;
2409 }
2410retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002411 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002412 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002413 PAGECACHE_TAG_DIRTY, min(end - index,
2414 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002415 unsigned i;
2416
2417 scanned = 1;
2418 for (i = 0; i < nr_pages; i++) {
2419 struct page *page = pvec.pages[i];
2420
2421 /*
2422 * At this point we hold neither mapping->tree_lock nor
2423 * lock on the page itself: the page may be truncated or
2424 * invalidated (changing page->mapping to NULL), or even
2425 * swizzled back from swapper_space to tmpfs file
2426 * mapping
2427 */
Chris Mason4bef0842008-09-08 11:18:08 -04002428 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2429 tree->ops->write_cache_pages_lock_hook(page);
2430 else
2431 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002432
2433 if (unlikely(page->mapping != mapping)) {
2434 unlock_page(page);
2435 continue;
2436 }
2437
2438 if (!wbc->range_cyclic && page->index > end) {
2439 done = 1;
2440 unlock_page(page);
2441 continue;
2442 }
2443
Chris Masond2c3f4f2008-11-19 12:44:22 -05002444 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002445 if (PageWriteback(page))
2446 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002447 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002448 }
Chris Masond1310b22008-01-24 16:13:08 -05002449
2450 if (PageWriteback(page) ||
2451 !clear_page_dirty_for_io(page)) {
2452 unlock_page(page);
2453 continue;
2454 }
2455
2456 ret = (*writepage)(page, wbc, data);
2457
2458 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2459 unlock_page(page);
2460 ret = 0;
2461 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002462 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002463 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002464
2465 /*
2466 * the filesystem may choose to bump up nr_to_write.
2467 * We have to make sure to honor the new nr_to_write
2468 * at any time
2469 */
2470 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002471 }
2472 pagevec_release(&pvec);
2473 cond_resched();
2474 }
2475 if (!scanned && !done) {
2476 /*
2477 * We hit the last page and there is more work to be done: wrap
2478 * back to the start of the file
2479 */
2480 scanned = 1;
2481 index = 0;
2482 goto retry;
2483 }
Chris Masond1310b22008-01-24 16:13:08 -05002484 return ret;
2485}
Chris Masond1310b22008-01-24 16:13:08 -05002486
Chris Masonffbd5172009-04-20 15:50:09 -04002487static void flush_epd_write_bio(struct extent_page_data *epd)
2488{
2489 if (epd->bio) {
2490 if (epd->sync_io)
2491 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2492 else
2493 submit_one_bio(WRITE, epd->bio, 0, 0);
2494 epd->bio = NULL;
2495 }
2496}
2497
Chris Masond2c3f4f2008-11-19 12:44:22 -05002498static noinline void flush_write_bio(void *data)
2499{
2500 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002501 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002502}
2503
Chris Masond1310b22008-01-24 16:13:08 -05002504int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2505 get_extent_t *get_extent,
2506 struct writeback_control *wbc)
2507{
2508 int ret;
2509 struct address_space *mapping = page->mapping;
2510 struct extent_page_data epd = {
2511 .bio = NULL,
2512 .tree = tree,
2513 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002514 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002515 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002516 };
2517 struct writeback_control wbc_writepages = {
Chris Masond313d7a2009-04-20 15:50:09 -04002518 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002519 .older_than_this = NULL,
2520 .nr_to_write = 64,
2521 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2522 .range_end = (loff_t)-1,
2523 };
2524
Chris Masond1310b22008-01-24 16:13:08 -05002525 ret = __extent_writepage(page, wbc, &epd);
2526
Chris Mason4bef0842008-09-08 11:18:08 -04002527 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002528 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002529 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002530 return ret;
2531}
Chris Masond1310b22008-01-24 16:13:08 -05002532
Chris Mason771ed682008-11-06 22:02:51 -05002533int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2534 u64 start, u64 end, get_extent_t *get_extent,
2535 int mode)
2536{
2537 int ret = 0;
2538 struct address_space *mapping = inode->i_mapping;
2539 struct page *page;
2540 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2541 PAGE_CACHE_SHIFT;
2542
2543 struct extent_page_data epd = {
2544 .bio = NULL,
2545 .tree = tree,
2546 .get_extent = get_extent,
2547 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002548 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002549 };
2550 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002551 .sync_mode = mode,
2552 .older_than_this = NULL,
2553 .nr_to_write = nr_pages * 2,
2554 .range_start = start,
2555 .range_end = end + 1,
2556 };
2557
Chris Masond3977122009-01-05 21:25:51 -05002558 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002559 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2560 if (clear_page_dirty_for_io(page))
2561 ret = __extent_writepage(page, &wbc_writepages, &epd);
2562 else {
2563 if (tree->ops && tree->ops->writepage_end_io_hook)
2564 tree->ops->writepage_end_io_hook(page, start,
2565 start + PAGE_CACHE_SIZE - 1,
2566 NULL, 1);
2567 unlock_page(page);
2568 }
2569 page_cache_release(page);
2570 start += PAGE_CACHE_SIZE;
2571 }
2572
Chris Masonffbd5172009-04-20 15:50:09 -04002573 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002574 return ret;
2575}
Chris Masond1310b22008-01-24 16:13:08 -05002576
2577int extent_writepages(struct extent_io_tree *tree,
2578 struct address_space *mapping,
2579 get_extent_t *get_extent,
2580 struct writeback_control *wbc)
2581{
2582 int ret = 0;
2583 struct extent_page_data epd = {
2584 .bio = NULL,
2585 .tree = tree,
2586 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002587 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002588 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002589 };
2590
Chris Mason4bef0842008-09-08 11:18:08 -04002591 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002592 __extent_writepage, &epd,
2593 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002594 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002595 return ret;
2596}
Chris Masond1310b22008-01-24 16:13:08 -05002597
2598int extent_readpages(struct extent_io_tree *tree,
2599 struct address_space *mapping,
2600 struct list_head *pages, unsigned nr_pages,
2601 get_extent_t get_extent)
2602{
2603 struct bio *bio = NULL;
2604 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002605 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002606
Chris Masond1310b22008-01-24 16:13:08 -05002607 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2608 struct page *page = list_entry(pages->prev, struct page, lru);
2609
2610 prefetchw(&page->flags);
2611 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002612 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002613 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002614 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002615 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002616 }
2617 page_cache_release(page);
2618 }
Chris Masond1310b22008-01-24 16:13:08 -05002619 BUG_ON(!list_empty(pages));
2620 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002621 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002622 return 0;
2623}
Chris Masond1310b22008-01-24 16:13:08 -05002624
2625/*
2626 * basic invalidatepage code, this waits on any locked or writeback
2627 * ranges corresponding to the page, and then deletes any extent state
2628 * records from the tree
2629 */
2630int extent_invalidatepage(struct extent_io_tree *tree,
2631 struct page *page, unsigned long offset)
2632{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002633 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002634 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2635 u64 end = start + PAGE_CACHE_SIZE - 1;
2636 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2637
Chris Masond3977122009-01-05 21:25:51 -05002638 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002639 if (start > end)
2640 return 0;
2641
Josef Bacik2ac55d42010-02-03 19:33:23 +00002642 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002643 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002644 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002645 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2646 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002647 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002648 return 0;
2649}
Chris Masond1310b22008-01-24 16:13:08 -05002650
2651/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002652 * a helper for releasepage, this tests for areas of the page that
2653 * are locked or under IO and drops the related state bits if it is safe
2654 * to drop the page.
2655 */
2656int try_release_extent_state(struct extent_map_tree *map,
2657 struct extent_io_tree *tree, struct page *page,
2658 gfp_t mask)
2659{
2660 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2661 u64 end = start + PAGE_CACHE_SIZE - 1;
2662 int ret = 1;
2663
Chris Mason211f90e2008-07-18 11:56:15 -04002664 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002665 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002666 ret = 0;
2667 else {
2668 if ((mask & GFP_NOFS) == GFP_NOFS)
2669 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002670 /*
2671 * at this point we can safely clear everything except the
2672 * locked bit and the nodatasum bit
2673 */
Chris Masone3f24cc2011-02-14 12:52:08 -05002674 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04002675 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2676 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05002677
2678 /* if clear_extent_bit failed for enomem reasons,
2679 * we can't allow the release to continue.
2680 */
2681 if (ret < 0)
2682 ret = 0;
2683 else
2684 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002685 }
2686 return ret;
2687}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002688
2689/*
Chris Masond1310b22008-01-24 16:13:08 -05002690 * a helper for releasepage. As long as there are no locked extents
2691 * in the range corresponding to the page, both state records and extent
2692 * map records are removed
2693 */
2694int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002695 struct extent_io_tree *tree, struct page *page,
2696 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002697{
2698 struct extent_map *em;
2699 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2700 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002701
Chris Mason70dec802008-01-29 09:59:12 -05002702 if ((mask & __GFP_WAIT) &&
2703 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002704 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002705 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002706 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002707 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002708 em = lookup_extent_mapping(map, start, len);
David Sterbac7040052011-04-19 18:00:01 +02002709 if (IS_ERR_OR_NULL(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002710 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002711 break;
2712 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002713 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2714 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002715 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002716 free_extent_map(em);
2717 break;
2718 }
2719 if (!test_range_bit(tree, em->start,
2720 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002721 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002722 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002723 remove_extent_mapping(map, em);
2724 /* once for the rb tree */
2725 free_extent_map(em);
2726 }
2727 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002728 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002729
2730 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002731 free_extent_map(em);
2732 }
Chris Masond1310b22008-01-24 16:13:08 -05002733 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002734 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002735}
Chris Masond1310b22008-01-24 16:13:08 -05002736
Chris Masonec29ed52011-02-23 16:23:20 -05002737/*
2738 * helper function for fiemap, which doesn't want to see any holes.
2739 * This maps until we find something past 'last'
2740 */
2741static struct extent_map *get_extent_skip_holes(struct inode *inode,
2742 u64 offset,
2743 u64 last,
2744 get_extent_t *get_extent)
2745{
2746 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
2747 struct extent_map *em;
2748 u64 len;
2749
2750 if (offset >= last)
2751 return NULL;
2752
2753 while(1) {
2754 len = last - offset;
2755 if (len == 0)
2756 break;
2757 len = (len + sectorsize - 1) & ~(sectorsize - 1);
2758 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02002759 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05002760 return em;
2761
2762 /* if this isn't a hole return it */
2763 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
2764 em->block_start != EXTENT_MAP_HOLE) {
2765 return em;
2766 }
2767
2768 /* this is a hole, advance to the next extent */
2769 offset = extent_map_end(em);
2770 free_extent_map(em);
2771 if (offset >= last)
2772 break;
2773 }
2774 return NULL;
2775}
2776
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002777int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2778 __u64 start, __u64 len, get_extent_t *get_extent)
2779{
Josef Bacik975f84f2010-11-23 19:36:57 +00002780 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002781 u64 off = start;
2782 u64 max = start + len;
2783 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00002784 u32 found_type;
2785 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05002786 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002787 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002788 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00002789 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002790 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002791 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00002792 struct btrfs_path *path;
2793 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002794 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05002795 u64 em_start = 0;
2796 u64 em_len = 0;
2797 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002798 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002799
2800 if (len == 0)
2801 return -EINVAL;
2802
Josef Bacik975f84f2010-11-23 19:36:57 +00002803 path = btrfs_alloc_path();
2804 if (!path)
2805 return -ENOMEM;
2806 path->leave_spinning = 1;
2807
Chris Masonec29ed52011-02-23 16:23:20 -05002808 /*
2809 * lookup the last file extent. We're not using i_size here
2810 * because there might be preallocation past i_size
2811 */
Josef Bacik975f84f2010-11-23 19:36:57 +00002812 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
2813 path, inode->i_ino, -1, 0);
2814 if (ret < 0) {
2815 btrfs_free_path(path);
2816 return ret;
2817 }
2818 WARN_ON(!ret);
2819 path->slots[0]--;
2820 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2821 struct btrfs_file_extent_item);
2822 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
2823 found_type = btrfs_key_type(&found_key);
2824
Chris Masonec29ed52011-02-23 16:23:20 -05002825 /* No extents, but there might be delalloc bits */
Josef Bacik975f84f2010-11-23 19:36:57 +00002826 if (found_key.objectid != inode->i_ino ||
2827 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05002828 /* have to trust i_size as the end */
2829 last = (u64)-1;
2830 last_for_get_extent = isize;
2831 } else {
2832 /*
2833 * remember the start of the last extent. There are a
2834 * bunch of different factors that go into the length of the
2835 * extent, so its much less complex to remember where it started
2836 */
2837 last = found_key.offset;
2838 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00002839 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002840 btrfs_free_path(path);
2841
Chris Masonec29ed52011-02-23 16:23:20 -05002842 /*
2843 * we might have some extents allocated but more delalloc past those
2844 * extents. so, we trust isize unless the start of the last extent is
2845 * beyond isize
2846 */
2847 if (last < isize) {
2848 last = (u64)-1;
2849 last_for_get_extent = isize;
2850 }
2851
Josef Bacik2ac55d42010-02-03 19:33:23 +00002852 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2853 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05002854
2855 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2856 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002857 if (!em)
2858 goto out;
2859 if (IS_ERR(em)) {
2860 ret = PTR_ERR(em);
2861 goto out;
2862 }
Josef Bacik975f84f2010-11-23 19:36:57 +00002863
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002864 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05002865 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002866
Chris Masonea8efc72011-03-08 11:54:40 -05002867 /* break if the extent we found is outside the range */
2868 if (em->start >= max || extent_map_end(em) < off)
2869 break;
2870
2871 /*
2872 * get_extent may return an extent that starts before our
2873 * requested range. We have to make sure the ranges
2874 * we return to fiemap always move forward and don't
2875 * overlap, so adjust the offsets here
2876 */
2877 em_start = max(em->start, off);
2878
2879 /*
2880 * record the offset from the start of the extent
2881 * for adjusting the disk offset below
2882 */
2883 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05002884 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05002885 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05002886 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002887 disko = 0;
2888 flags = 0;
2889
Chris Masonea8efc72011-03-08 11:54:40 -05002890 /*
2891 * bump off for our next call to get_extent
2892 */
2893 off = extent_map_end(em);
2894 if (off >= max)
2895 end = 1;
2896
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002897 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002898 end = 1;
2899 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002900 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002901 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2902 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002903 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002904 flags |= (FIEMAP_EXTENT_DELALLOC |
2905 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002906 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05002907 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002908 }
2909 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2910 flags |= FIEMAP_EXTENT_ENCODED;
2911
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002912 free_extent_map(em);
2913 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05002914 if ((em_start >= last) || em_len == (u64)-1 ||
2915 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002916 flags |= FIEMAP_EXTENT_LAST;
2917 end = 1;
2918 }
2919
Chris Masonec29ed52011-02-23 16:23:20 -05002920 /* now scan forward to see if this is really the last extent. */
2921 em = get_extent_skip_holes(inode, off, last_for_get_extent,
2922 get_extent);
2923 if (IS_ERR(em)) {
2924 ret = PTR_ERR(em);
2925 goto out;
2926 }
2927 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00002928 flags |= FIEMAP_EXTENT_LAST;
2929 end = 1;
2930 }
Chris Masonec29ed52011-02-23 16:23:20 -05002931 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
2932 em_len, flags);
2933 if (ret)
2934 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002935 }
2936out_free:
2937 free_extent_map(em);
2938out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002939 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
2940 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002941 return ret;
2942}
2943
Chris Masond1310b22008-01-24 16:13:08 -05002944static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2945 unsigned long i)
2946{
2947 struct page *p;
2948 struct address_space *mapping;
2949
2950 if (i == 0)
2951 return eb->first_page;
2952 i += eb->start >> PAGE_CACHE_SHIFT;
2953 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002954 if (!mapping)
2955 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002956
2957 /*
2958 * extent_buffer_page is only called after pinning the page
2959 * by increasing the reference count. So we know the page must
2960 * be in the radix tree.
2961 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002962 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05002963 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002964 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04002965
Chris Masond1310b22008-01-24 16:13:08 -05002966 return p;
2967}
2968
Chris Mason6af118ce2008-07-22 11:18:07 -04002969static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002970{
Chris Mason6af118ce2008-07-22 11:18:07 -04002971 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2972 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002973}
2974
Chris Masond1310b22008-01-24 16:13:08 -05002975static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2976 u64 start,
2977 unsigned long len,
2978 gfp_t mask)
2979{
2980 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05002981#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002982 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04002983#endif
Chris Masond1310b22008-01-24 16:13:08 -05002984
Chris Masond1310b22008-01-24 16:13:08 -05002985 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002986 if (eb == NULL)
2987 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002988 eb->start = start;
2989 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002990 spin_lock_init(&eb->lock);
2991 init_waitqueue_head(&eb->lock_wq);
2992
Chris Mason39351272009-02-04 09:24:05 -05002993#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04002994 spin_lock_irqsave(&leak_lock, flags);
2995 list_add(&eb->leak_list, &buffers);
2996 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04002997#endif
Chris Masond1310b22008-01-24 16:13:08 -05002998 atomic_set(&eb->refs, 1);
2999
3000 return eb;
3001}
3002
3003static void __free_extent_buffer(struct extent_buffer *eb)
3004{
Chris Mason39351272009-02-04 09:24:05 -05003005#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003006 unsigned long flags;
3007 spin_lock_irqsave(&leak_lock, flags);
3008 list_del(&eb->leak_list);
3009 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003010#endif
Chris Masond1310b22008-01-24 16:13:08 -05003011 kmem_cache_free(extent_buffer_cache, eb);
3012}
3013
Miao Xie897ca6e2010-10-26 20:57:29 -04003014/*
3015 * Helper for releasing extent buffer page.
3016 */
3017static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3018 unsigned long start_idx)
3019{
3020 unsigned long index;
3021 struct page *page;
3022
3023 if (!eb->first_page)
3024 return;
3025
3026 index = num_extent_pages(eb->start, eb->len);
3027 if (start_idx >= index)
3028 return;
3029
3030 do {
3031 index--;
3032 page = extent_buffer_page(eb, index);
3033 if (page)
3034 page_cache_release(page);
3035 } while (index != start_idx);
3036}
3037
3038/*
3039 * Helper for releasing the extent buffer.
3040 */
3041static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3042{
3043 btrfs_release_extent_buffer_page(eb, 0);
3044 __free_extent_buffer(eb);
3045}
3046
Chris Masond1310b22008-01-24 16:13:08 -05003047struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3048 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003049 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003050{
3051 unsigned long num_pages = num_extent_pages(start, len);
3052 unsigned long i;
3053 unsigned long index = start >> PAGE_CACHE_SHIFT;
3054 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003055 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003056 struct page *p;
3057 struct address_space *mapping = tree->mapping;
3058 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003059 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003060
Miao Xie19fe0a82010-10-26 20:57:29 -04003061 rcu_read_lock();
3062 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3063 if (eb && atomic_inc_not_zero(&eb->refs)) {
3064 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003065 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003066 return eb;
3067 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003068 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003069
David Sterbaba144192011-04-21 01:12:06 +02003070 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003071 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003072 return NULL;
3073
Chris Masond1310b22008-01-24 16:13:08 -05003074 if (page0) {
3075 eb->first_page = page0;
3076 i = 1;
3077 index++;
3078 page_cache_get(page0);
3079 mark_page_accessed(page0);
3080 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003081 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003082 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003083 } else {
3084 i = 0;
3085 }
3086 for (; i < num_pages; i++, index++) {
David Sterbaba144192011-04-21 01:12:06 +02003087 p = find_or_create_page(mapping, index, GFP_NOFS | __GFP_HIGHMEM);
Chris Masond1310b22008-01-24 16:13:08 -05003088 if (!p) {
3089 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003090 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003091 }
3092 set_page_extent_mapped(p);
3093 mark_page_accessed(p);
3094 if (i == 0) {
3095 eb->first_page = p;
3096 set_page_extent_head(p, len);
3097 } else {
3098 set_page_private(p, EXTENT_PAGE_PRIVATE);
3099 }
3100 if (!PageUptodate(p))
3101 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003102
3103 /*
3104 * see below about how we avoid a nasty race with release page
3105 * and why we unlock later
3106 */
3107 if (i != 0)
3108 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003109 }
3110 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003111 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003112
Miao Xie19fe0a82010-10-26 20:57:29 -04003113 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3114 if (ret)
3115 goto free_eb;
3116
Chris Mason6af118ce2008-07-22 11:18:07 -04003117 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003118 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3119 if (ret == -EEXIST) {
3120 exists = radix_tree_lookup(&tree->buffer,
3121 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003122 /* add one reference for the caller */
3123 atomic_inc(&exists->refs);
3124 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003125 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003126 goto free_eb;
3127 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003128 /* add one reference for the tree */
3129 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003130 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003131 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003132
3133 /*
3134 * there is a race where release page may have
3135 * tried to find this extent buffer in the radix
3136 * but failed. It will tell the VM it is safe to
3137 * reclaim the, and it will clear the page private bit.
3138 * We must make sure to set the page private bit properly
3139 * after the extent buffer is in the radix tree so
3140 * it doesn't get lost
3141 */
3142 set_page_extent_mapped(eb->first_page);
3143 set_page_extent_head(eb->first_page, eb->len);
3144 if (!page0)
3145 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003146 return eb;
3147
Chris Mason6af118ce2008-07-22 11:18:07 -04003148free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003149 if (eb->first_page && !page0)
3150 unlock_page(eb->first_page);
3151
Chris Masond1310b22008-01-24 16:13:08 -05003152 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003153 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003154 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003155 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003156}
Chris Masond1310b22008-01-24 16:13:08 -05003157
3158struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003159 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003160{
Chris Masond1310b22008-01-24 16:13:08 -05003161 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003162
Miao Xie19fe0a82010-10-26 20:57:29 -04003163 rcu_read_lock();
3164 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3165 if (eb && atomic_inc_not_zero(&eb->refs)) {
3166 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003167 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003168 return eb;
3169 }
3170 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003171
Miao Xie19fe0a82010-10-26 20:57:29 -04003172 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003173}
Chris Masond1310b22008-01-24 16:13:08 -05003174
3175void free_extent_buffer(struct extent_buffer *eb)
3176{
Chris Masond1310b22008-01-24 16:13:08 -05003177 if (!eb)
3178 return;
3179
3180 if (!atomic_dec_and_test(&eb->refs))
3181 return;
3182
Chris Mason6af118ce2008-07-22 11:18:07 -04003183 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003184}
Chris Masond1310b22008-01-24 16:13:08 -05003185
3186int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3187 struct extent_buffer *eb)
3188{
Chris Masond1310b22008-01-24 16:13:08 -05003189 unsigned long i;
3190 unsigned long num_pages;
3191 struct page *page;
3192
Chris Masond1310b22008-01-24 16:13:08 -05003193 num_pages = num_extent_pages(eb->start, eb->len);
3194
3195 for (i = 0; i < num_pages; i++) {
3196 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003197 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003198 continue;
3199
Chris Masona61e6f22008-07-22 11:18:08 -04003200 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003201 WARN_ON(!PagePrivate(page));
3202
3203 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003204 if (i == 0)
3205 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003206
Chris Masond1310b22008-01-24 16:13:08 -05003207 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003208 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003209 if (!PageDirty(page)) {
3210 radix_tree_tag_clear(&page->mapping->page_tree,
3211 page_index(page),
3212 PAGECACHE_TAG_DIRTY);
3213 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003214 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003215 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003216 }
3217 return 0;
3218}
Chris Masond1310b22008-01-24 16:13:08 -05003219
Chris Masond1310b22008-01-24 16:13:08 -05003220int set_extent_buffer_dirty(struct extent_io_tree *tree,
3221 struct extent_buffer *eb)
3222{
3223 unsigned long i;
3224 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003225 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003226
Chris Masonb9473432009-03-13 11:00:37 -04003227 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003228 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003229 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003230 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003231 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003232}
Chris Masond1310b22008-01-24 16:13:08 -05003233
Chris Mason1259ab72008-05-12 13:39:03 -04003234int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003235 struct extent_buffer *eb,
3236 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003237{
3238 unsigned long i;
3239 struct page *page;
3240 unsigned long num_pages;
3241
3242 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003243 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003244
3245 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003246 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003247 for (i = 0; i < num_pages; i++) {
3248 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003249 if (page)
3250 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003251 }
3252 return 0;
3253}
3254
Chris Masond1310b22008-01-24 16:13:08 -05003255int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3256 struct extent_buffer *eb)
3257{
3258 unsigned long i;
3259 struct page *page;
3260 unsigned long num_pages;
3261
3262 num_pages = num_extent_pages(eb->start, eb->len);
3263
3264 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003265 NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003266 for (i = 0; i < num_pages; i++) {
3267 page = extent_buffer_page(eb, i);
3268 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3269 ((i == num_pages - 1) &&
3270 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3271 check_page_uptodate(tree, page);
3272 continue;
3273 }
3274 SetPageUptodate(page);
3275 }
3276 return 0;
3277}
Chris Masond1310b22008-01-24 16:13:08 -05003278
Chris Masonce9adaa2008-04-09 16:28:12 -04003279int extent_range_uptodate(struct extent_io_tree *tree,
3280 u64 start, u64 end)
3281{
3282 struct page *page;
3283 int ret;
3284 int pg_uptodate = 1;
3285 int uptodate;
3286 unsigned long index;
3287
Chris Mason9655d292009-09-02 15:22:30 -04003288 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003289 if (ret)
3290 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003291 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003292 index = start >> PAGE_CACHE_SHIFT;
3293 page = find_get_page(tree->mapping, index);
3294 uptodate = PageUptodate(page);
3295 page_cache_release(page);
3296 if (!uptodate) {
3297 pg_uptodate = 0;
3298 break;
3299 }
3300 start += PAGE_CACHE_SIZE;
3301 }
3302 return pg_uptodate;
3303}
3304
Chris Masond1310b22008-01-24 16:13:08 -05003305int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003306 struct extent_buffer *eb,
3307 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003308{
Chris Mason728131d2008-04-09 16:28:12 -04003309 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003310 unsigned long num_pages;
3311 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003312 struct page *page;
3313 int pg_uptodate = 1;
3314
Chris Masonb4ce94d2009-02-04 09:25:08 -05003315 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003316 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003317
Chris Mason42352982008-04-28 16:40:52 -04003318 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003319 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003320 if (ret)
3321 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003322
3323 num_pages = num_extent_pages(eb->start, eb->len);
3324 for (i = 0; i < num_pages; i++) {
3325 page = extent_buffer_page(eb, i);
3326 if (!PageUptodate(page)) {
3327 pg_uptodate = 0;
3328 break;
3329 }
3330 }
Chris Mason42352982008-04-28 16:40:52 -04003331 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003332}
Chris Masond1310b22008-01-24 16:13:08 -05003333
3334int read_extent_buffer_pages(struct extent_io_tree *tree,
3335 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003336 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003337 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003338{
3339 unsigned long i;
3340 unsigned long start_i;
3341 struct page *page;
3342 int err;
3343 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003344 int locked_pages = 0;
3345 int all_uptodate = 1;
3346 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003347 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003348 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003349 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003350
Chris Masonb4ce94d2009-02-04 09:25:08 -05003351 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003352 return 0;
3353
Chris Masonce9adaa2008-04-09 16:28:12 -04003354 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003355 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003356 return 0;
3357 }
3358
3359 if (start) {
3360 WARN_ON(start < eb->start);
3361 start_i = (start >> PAGE_CACHE_SHIFT) -
3362 (eb->start >> PAGE_CACHE_SHIFT);
3363 } else {
3364 start_i = 0;
3365 }
3366
3367 num_pages = num_extent_pages(eb->start, eb->len);
3368 for (i = start_i; i < num_pages; i++) {
3369 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003370 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003371 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003372 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003373 } else {
3374 lock_page(page);
3375 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003376 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003377 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003378 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003379 }
3380 if (all_uptodate) {
3381 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003382 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003383 goto unlock_exit;
3384 }
3385
3386 for (i = start_i; i < num_pages; i++) {
3387 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003388
3389 WARN_ON(!PagePrivate(page));
3390
3391 set_page_extent_mapped(page);
3392 if (i == 0)
3393 set_page_extent_head(page, eb->len);
3394
Chris Masonce9adaa2008-04-09 16:28:12 -04003395 if (inc_all_pages)
3396 page_cache_get(page);
3397 if (!PageUptodate(page)) {
3398 if (start_i == 0)
3399 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003400 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003401 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003402 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003403 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003404 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003405 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003406 } else {
3407 unlock_page(page);
3408 }
3409 }
3410
Chris Masona86c12c2008-02-07 10:50:54 -05003411 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003412 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003413
Chris Masond3977122009-01-05 21:25:51 -05003414 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003415 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003416
Chris Masond1310b22008-01-24 16:13:08 -05003417 for (i = start_i; i < num_pages; i++) {
3418 page = extent_buffer_page(eb, i);
3419 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003420 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003421 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003422 }
Chris Masond3977122009-01-05 21:25:51 -05003423
Chris Masond1310b22008-01-24 16:13:08 -05003424 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003425 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003426 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003427
3428unlock_exit:
3429 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003430 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003431 page = extent_buffer_page(eb, i);
3432 i++;
3433 unlock_page(page);
3434 locked_pages--;
3435 }
3436 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003437}
Chris Masond1310b22008-01-24 16:13:08 -05003438
3439void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3440 unsigned long start,
3441 unsigned long len)
3442{
3443 size_t cur;
3444 size_t offset;
3445 struct page *page;
3446 char *kaddr;
3447 char *dst = (char *)dstv;
3448 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3449 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003450
3451 WARN_ON(start > eb->len);
3452 WARN_ON(start + len > eb->start + eb->len);
3453
3454 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3455
Chris Masond3977122009-01-05 21:25:51 -05003456 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003457 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003458
3459 cur = min(len, (PAGE_CACHE_SIZE - offset));
3460 kaddr = kmap_atomic(page, KM_USER1);
3461 memcpy(dst, kaddr + offset, cur);
3462 kunmap_atomic(kaddr, KM_USER1);
3463
3464 dst += cur;
3465 len -= cur;
3466 offset = 0;
3467 i++;
3468 }
3469}
Chris Masond1310b22008-01-24 16:13:08 -05003470
3471int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3472 unsigned long min_len, char **token, char **map,
3473 unsigned long *map_start,
3474 unsigned long *map_len, int km)
3475{
3476 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3477 char *kaddr;
3478 struct page *p;
3479 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3480 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3481 unsigned long end_i = (start_offset + start + min_len - 1) >>
3482 PAGE_CACHE_SHIFT;
3483
3484 if (i != end_i)
3485 return -EINVAL;
3486
3487 if (i == 0) {
3488 offset = start_offset;
3489 *map_start = 0;
3490 } else {
3491 offset = 0;
3492 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3493 }
Chris Masond3977122009-01-05 21:25:51 -05003494
Chris Masond1310b22008-01-24 16:13:08 -05003495 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003496 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3497 "wanted %lu %lu\n", (unsigned long long)eb->start,
3498 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003499 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003500 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003501 }
3502
3503 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003504 kaddr = kmap_atomic(p, km);
3505 *token = kaddr;
3506 *map = kaddr + offset;
3507 *map_len = PAGE_CACHE_SIZE - offset;
3508 return 0;
3509}
Chris Masond1310b22008-01-24 16:13:08 -05003510
3511int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3512 unsigned long min_len,
3513 char **token, char **map,
3514 unsigned long *map_start,
3515 unsigned long *map_len, int km)
3516{
3517 int err;
3518 int save = 0;
3519 if (eb->map_token) {
3520 unmap_extent_buffer(eb, eb->map_token, km);
3521 eb->map_token = NULL;
3522 save = 1;
3523 }
3524 err = map_private_extent_buffer(eb, start, min_len, token, map,
3525 map_start, map_len, km);
3526 if (!err && save) {
3527 eb->map_token = *token;
3528 eb->kaddr = *map;
3529 eb->map_start = *map_start;
3530 eb->map_len = *map_len;
3531 }
3532 return err;
3533}
Chris Masond1310b22008-01-24 16:13:08 -05003534
3535void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3536{
3537 kunmap_atomic(token, km);
3538}
Chris Masond1310b22008-01-24 16:13:08 -05003539
3540int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3541 unsigned long start,
3542 unsigned long len)
3543{
3544 size_t cur;
3545 size_t offset;
3546 struct page *page;
3547 char *kaddr;
3548 char *ptr = (char *)ptrv;
3549 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3550 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3551 int ret = 0;
3552
3553 WARN_ON(start > eb->len);
3554 WARN_ON(start + len > eb->start + eb->len);
3555
3556 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3557
Chris Masond3977122009-01-05 21:25:51 -05003558 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003559 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003560
3561 cur = min(len, (PAGE_CACHE_SIZE - offset));
3562
3563 kaddr = kmap_atomic(page, KM_USER0);
3564 ret = memcmp(ptr, kaddr + offset, cur);
3565 kunmap_atomic(kaddr, KM_USER0);
3566 if (ret)
3567 break;
3568
3569 ptr += cur;
3570 len -= cur;
3571 offset = 0;
3572 i++;
3573 }
3574 return ret;
3575}
Chris Masond1310b22008-01-24 16:13:08 -05003576
3577void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3578 unsigned long start, unsigned long len)
3579{
3580 size_t cur;
3581 size_t offset;
3582 struct page *page;
3583 char *kaddr;
3584 char *src = (char *)srcv;
3585 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3586 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3587
3588 WARN_ON(start > eb->len);
3589 WARN_ON(start + len > eb->start + eb->len);
3590
3591 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3592
Chris Masond3977122009-01-05 21:25:51 -05003593 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003594 page = extent_buffer_page(eb, i);
3595 WARN_ON(!PageUptodate(page));
3596
3597 cur = min(len, PAGE_CACHE_SIZE - offset);
3598 kaddr = kmap_atomic(page, KM_USER1);
3599 memcpy(kaddr + offset, src, cur);
3600 kunmap_atomic(kaddr, KM_USER1);
3601
3602 src += cur;
3603 len -= cur;
3604 offset = 0;
3605 i++;
3606 }
3607}
Chris Masond1310b22008-01-24 16:13:08 -05003608
3609void memset_extent_buffer(struct extent_buffer *eb, char c,
3610 unsigned long start, unsigned long len)
3611{
3612 size_t cur;
3613 size_t offset;
3614 struct page *page;
3615 char *kaddr;
3616 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3617 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3618
3619 WARN_ON(start > eb->len);
3620 WARN_ON(start + len > eb->start + eb->len);
3621
3622 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3623
Chris Masond3977122009-01-05 21:25:51 -05003624 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003625 page = extent_buffer_page(eb, i);
3626 WARN_ON(!PageUptodate(page));
3627
3628 cur = min(len, PAGE_CACHE_SIZE - offset);
3629 kaddr = kmap_atomic(page, KM_USER0);
3630 memset(kaddr + offset, c, cur);
3631 kunmap_atomic(kaddr, KM_USER0);
3632
3633 len -= cur;
3634 offset = 0;
3635 i++;
3636 }
3637}
Chris Masond1310b22008-01-24 16:13:08 -05003638
3639void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3640 unsigned long dst_offset, unsigned long src_offset,
3641 unsigned long len)
3642{
3643 u64 dst_len = dst->len;
3644 size_t cur;
3645 size_t offset;
3646 struct page *page;
3647 char *kaddr;
3648 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3649 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3650
3651 WARN_ON(src->len != dst_len);
3652
3653 offset = (start_offset + dst_offset) &
3654 ((unsigned long)PAGE_CACHE_SIZE - 1);
3655
Chris Masond3977122009-01-05 21:25:51 -05003656 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003657 page = extent_buffer_page(dst, i);
3658 WARN_ON(!PageUptodate(page));
3659
3660 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3661
3662 kaddr = kmap_atomic(page, KM_USER0);
3663 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3664 kunmap_atomic(kaddr, KM_USER0);
3665
3666 src_offset += cur;
3667 len -= cur;
3668 offset = 0;
3669 i++;
3670 }
3671}
Chris Masond1310b22008-01-24 16:13:08 -05003672
3673static void move_pages(struct page *dst_page, struct page *src_page,
3674 unsigned long dst_off, unsigned long src_off,
3675 unsigned long len)
3676{
3677 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3678 if (dst_page == src_page) {
3679 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3680 } else {
3681 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3682 char *p = dst_kaddr + dst_off + len;
3683 char *s = src_kaddr + src_off + len;
3684
3685 while (len--)
3686 *--p = *--s;
3687
3688 kunmap_atomic(src_kaddr, KM_USER1);
3689 }
3690 kunmap_atomic(dst_kaddr, KM_USER0);
3691}
3692
Sergei Trofimovich33872062011-04-11 21:52:52 +00003693static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
3694{
3695 unsigned long distance = (src > dst) ? src - dst : dst - src;
3696 return distance < len;
3697}
3698
Chris Masond1310b22008-01-24 16:13:08 -05003699static void copy_pages(struct page *dst_page, struct page *src_page,
3700 unsigned long dst_off, unsigned long src_off,
3701 unsigned long len)
3702{
3703 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3704 char *src_kaddr;
3705
Sergei Trofimovich33872062011-04-11 21:52:52 +00003706 if (dst_page != src_page) {
Chris Masond1310b22008-01-24 16:13:08 -05003707 src_kaddr = kmap_atomic(src_page, KM_USER1);
Sergei Trofimovich33872062011-04-11 21:52:52 +00003708 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003709 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00003710 BUG_ON(areas_overlap(src_off, dst_off, len));
3711 }
Chris Masond1310b22008-01-24 16:13:08 -05003712
3713 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3714 kunmap_atomic(dst_kaddr, KM_USER0);
3715 if (dst_page != src_page)
3716 kunmap_atomic(src_kaddr, KM_USER1);
3717}
3718
3719void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3720 unsigned long src_offset, unsigned long len)
3721{
3722 size_t cur;
3723 size_t dst_off_in_page;
3724 size_t src_off_in_page;
3725 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3726 unsigned long dst_i;
3727 unsigned long src_i;
3728
3729 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003730 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3731 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003732 BUG_ON(1);
3733 }
3734 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003735 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3736 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003737 BUG_ON(1);
3738 }
3739
Chris Masond3977122009-01-05 21:25:51 -05003740 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003741 dst_off_in_page = (start_offset + dst_offset) &
3742 ((unsigned long)PAGE_CACHE_SIZE - 1);
3743 src_off_in_page = (start_offset + src_offset) &
3744 ((unsigned long)PAGE_CACHE_SIZE - 1);
3745
3746 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3747 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3748
3749 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3750 src_off_in_page));
3751 cur = min_t(unsigned long, cur,
3752 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3753
3754 copy_pages(extent_buffer_page(dst, dst_i),
3755 extent_buffer_page(dst, src_i),
3756 dst_off_in_page, src_off_in_page, cur);
3757
3758 src_offset += cur;
3759 dst_offset += cur;
3760 len -= cur;
3761 }
3762}
Chris Masond1310b22008-01-24 16:13:08 -05003763
3764void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3765 unsigned long src_offset, unsigned long len)
3766{
3767 size_t cur;
3768 size_t dst_off_in_page;
3769 size_t src_off_in_page;
3770 unsigned long dst_end = dst_offset + len - 1;
3771 unsigned long src_end = src_offset + len - 1;
3772 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3773 unsigned long dst_i;
3774 unsigned long src_i;
3775
3776 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003777 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3778 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003779 BUG_ON(1);
3780 }
3781 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003782 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3783 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003784 BUG_ON(1);
3785 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00003786 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05003787 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3788 return;
3789 }
Chris Masond3977122009-01-05 21:25:51 -05003790 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003791 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3792 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3793
3794 dst_off_in_page = (start_offset + dst_end) &
3795 ((unsigned long)PAGE_CACHE_SIZE - 1);
3796 src_off_in_page = (start_offset + src_end) &
3797 ((unsigned long)PAGE_CACHE_SIZE - 1);
3798
3799 cur = min_t(unsigned long, len, src_off_in_page + 1);
3800 cur = min(cur, dst_off_in_page + 1);
3801 move_pages(extent_buffer_page(dst, dst_i),
3802 extent_buffer_page(dst, src_i),
3803 dst_off_in_page - cur + 1,
3804 src_off_in_page - cur + 1, cur);
3805
3806 dst_end -= cur;
3807 src_end -= cur;
3808 len -= cur;
3809 }
3810}
Chris Mason6af118ce2008-07-22 11:18:07 -04003811
Miao Xie19fe0a82010-10-26 20:57:29 -04003812static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3813{
3814 struct extent_buffer *eb =
3815 container_of(head, struct extent_buffer, rcu_head);
3816
3817 btrfs_release_extent_buffer(eb);
3818}
3819
Chris Mason6af118ce2008-07-22 11:18:07 -04003820int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3821{
3822 u64 start = page_offset(page);
3823 struct extent_buffer *eb;
3824 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04003825
3826 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003827 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05003828 if (!eb) {
3829 spin_unlock(&tree->buffer_lock);
3830 return ret;
3831 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003832
Chris Masonb9473432009-03-13 11:00:37 -04003833 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3834 ret = 0;
3835 goto out;
3836 }
Miao Xie897ca6e2010-10-26 20:57:29 -04003837
Miao Xie19fe0a82010-10-26 20:57:29 -04003838 /*
3839 * set @eb->refs to 0 if it is already 1, and then release the @eb.
3840 * Or go back.
3841 */
3842 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
3843 ret = 0;
3844 goto out;
3845 }
3846
3847 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003848out:
3849 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003850
3851 /* at this point we can safely release the extent buffer */
3852 if (atomic_read(&eb->refs) == 0)
3853 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04003854 return ret;
3855}