blob: 624ef10d36cc11eff2c2b24a74e5d61a1718aa50 [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>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Chris Masond1310b22008-01-24 16:13:08 -050021
Chris Masond1310b22008-01-24 16:13:08 -050022static struct kmem_cache *extent_state_cache;
23static struct kmem_cache *extent_buffer_cache;
24
25static LIST_HEAD(buffers);
26static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040027
Chris Masonb47eda82008-11-10 12:34:40 -050028#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050029#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050030static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040031#endif
Chris Masond1310b22008-01-24 16:13:08 -050032
Chris Masond1310b22008-01-24 16:13:08 -050033#define BUFFER_LRU_MAX 64
34
35struct tree_entry {
36 u64 start;
37 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050038 struct rb_node rb_node;
39};
40
41struct extent_page_data {
42 struct bio *bio;
43 struct extent_io_tree *tree;
44 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050045
46 /* tells writepage not to lock the state bits for this range
47 * it still does the unlocking
48 */
Chris Masonffbd5172009-04-20 15:50:09 -040049 unsigned int extent_locked:1;
50
51 /* tells the submit_bio code to use a WRITE_SYNC */
52 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050053};
54
55int __init extent_io_init(void)
56{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020057 extent_state_cache = kmem_cache_create("extent_state",
58 sizeof(struct extent_state), 0,
59 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050060 if (!extent_state_cache)
61 return -ENOMEM;
62
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020063 extent_buffer_cache = kmem_cache_create("extent_buffers",
64 sizeof(struct extent_buffer), 0,
65 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050066 if (!extent_buffer_cache)
67 goto free_state_cache;
68 return 0;
69
70free_state_cache:
71 kmem_cache_destroy(extent_state_cache);
72 return -ENOMEM;
73}
74
75void extent_io_exit(void)
76{
77 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040078 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050079
80 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040081 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050082 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
83 "state %lu in tree %p refs %d\n",
84 (unsigned long long)state->start,
85 (unsigned long long)state->end,
86 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040087 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050088 kmem_cache_free(extent_state_cache, state);
89
90 }
91
Chris Mason2d2ae542008-03-26 16:24:23 -040092 while (!list_empty(&buffers)) {
93 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050094 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
95 "refs %d\n", (unsigned long long)eb->start,
96 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040097 list_del(&eb->leak_list);
98 kmem_cache_free(extent_buffer_cache, eb);
99 }
Chris Masond1310b22008-01-24 16:13:08 -0500100 if (extent_state_cache)
101 kmem_cache_destroy(extent_state_cache);
102 if (extent_buffer_cache)
103 kmem_cache_destroy(extent_buffer_cache);
104}
105
106void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200107 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500108{
Eric Paris6bef4d32010-02-23 19:43:04 +0000109 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400110 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500111 tree->ops = NULL;
112 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500113 spin_lock_init(&tree->lock);
Chris Mason6af118c2008-07-22 11:18:07 -0400114 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500115 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500116}
Chris Masond1310b22008-01-24 16:13:08 -0500117
Christoph Hellwigb2950862008-12-02 09:54:17 -0500118static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500119{
120 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500121#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400122 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400123#endif
Chris Masond1310b22008-01-24 16:13:08 -0500124
125 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400126 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500127 return state;
128 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500129 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500130 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500131#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400132 spin_lock_irqsave(&leak_lock, flags);
133 list_add(&state->leak_list, &states);
134 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400135#endif
Chris Masond1310b22008-01-24 16:13:08 -0500136 atomic_set(&state->refs, 1);
137 init_waitqueue_head(&state->wq);
138 return state;
139}
Chris Masond1310b22008-01-24 16:13:08 -0500140
Chris Mason4845e442010-05-25 20:56:50 -0400141void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500142{
Chris Masond1310b22008-01-24 16:13:08 -0500143 if (!state)
144 return;
145 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500146#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400147 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400148#endif
Chris Mason70dec802008-01-29 09:59:12 -0500149 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500150#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400151 spin_lock_irqsave(&leak_lock, flags);
152 list_del(&state->leak_list);
153 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400154#endif
Chris Masond1310b22008-01-24 16:13:08 -0500155 kmem_cache_free(extent_state_cache, state);
156 }
157}
Chris Masond1310b22008-01-24 16:13:08 -0500158
159static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
160 struct rb_node *node)
161{
Chris Masond3977122009-01-05 21:25:51 -0500162 struct rb_node **p = &root->rb_node;
163 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500164 struct tree_entry *entry;
165
Chris Masond3977122009-01-05 21:25:51 -0500166 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500167 parent = *p;
168 entry = rb_entry(parent, struct tree_entry, rb_node);
169
170 if (offset < entry->start)
171 p = &(*p)->rb_left;
172 else if (offset > entry->end)
173 p = &(*p)->rb_right;
174 else
175 return parent;
176 }
177
178 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500179 rb_link_node(node, parent, p);
180 rb_insert_color(node, root);
181 return NULL;
182}
183
Chris Mason80ea96b2008-02-01 14:51:59 -0500184static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500185 struct rb_node **prev_ret,
186 struct rb_node **next_ret)
187{
Chris Mason80ea96b2008-02-01 14:51:59 -0500188 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500189 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500190 struct rb_node *prev = NULL;
191 struct rb_node *orig_prev = NULL;
192 struct tree_entry *entry;
193 struct tree_entry *prev_entry = NULL;
194
Chris Masond3977122009-01-05 21:25:51 -0500195 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500196 entry = rb_entry(n, struct tree_entry, rb_node);
197 prev = n;
198 prev_entry = entry;
199
200 if (offset < entry->start)
201 n = n->rb_left;
202 else if (offset > entry->end)
203 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500204 else
Chris Masond1310b22008-01-24 16:13:08 -0500205 return n;
206 }
207
208 if (prev_ret) {
209 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500210 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500211 prev = rb_next(prev);
212 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
213 }
214 *prev_ret = prev;
215 prev = orig_prev;
216 }
217
218 if (next_ret) {
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500220 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500221 prev = rb_prev(prev);
222 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
223 }
224 *next_ret = prev;
225 }
226 return NULL;
227}
228
Chris Mason80ea96b2008-02-01 14:51:59 -0500229static inline struct rb_node *tree_search(struct extent_io_tree *tree,
230 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500231{
Chris Mason70dec802008-01-29 09:59:12 -0500232 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500233 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500234
Chris Mason80ea96b2008-02-01 14:51:59 -0500235 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500236 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500237 return prev;
238 return ret;
239}
240
Josef Bacik9ed74f22009-09-11 16:12:44 -0400241static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
242 struct extent_state *other)
243{
244 if (tree->ops && tree->ops->merge_extent_hook)
245 tree->ops->merge_extent_hook(tree->mapping->host, new,
246 other);
247}
248
Chris Masond1310b22008-01-24 16:13:08 -0500249/*
250 * utility function to look for merge candidates inside a given range.
251 * Any extents with matching state are merged together into a single
252 * extent in the tree. Extents with EXTENT_IO in their state field
253 * are not merged because the end_io handlers need to be able to do
254 * operations on them without sleeping (or doing allocations/splits).
255 *
256 * This should be called with the tree lock held.
257 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000258static void merge_state(struct extent_io_tree *tree,
259 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500260{
261 struct extent_state *other;
262 struct rb_node *other_node;
263
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400264 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000265 return;
Chris Masond1310b22008-01-24 16:13:08 -0500266
267 other_node = rb_prev(&state->rb_node);
268 if (other_node) {
269 other = rb_entry(other_node, struct extent_state, rb_node);
270 if (other->end == state->start - 1 &&
271 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400272 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500273 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500274 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500275 rb_erase(&other->rb_node, &tree->state);
276 free_extent_state(other);
277 }
278 }
279 other_node = rb_next(&state->rb_node);
280 if (other_node) {
281 other = rb_entry(other_node, struct extent_state, rb_node);
282 if (other->start == state->end + 1 &&
283 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400284 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400285 state->end = other->end;
286 other->tree = NULL;
287 rb_erase(&other->rb_node, &tree->state);
288 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500289 }
290 }
Chris Masond1310b22008-01-24 16:13:08 -0500291}
292
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000293static void 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{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000296 if (tree->ops && tree->ops->set_bit_hook)
297 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500298}
299
300static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400301 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500302{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400303 if (tree->ops && tree->ops->clear_bit_hook)
304 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500305}
306
Xiao Guangrong3150b692011-07-14 03:19:08 +0000307static void set_state_bits(struct extent_io_tree *tree,
308 struct extent_state *state, int *bits);
309
Chris Masond1310b22008-01-24 16:13:08 -0500310/*
311 * insert an extent_state struct into the tree. 'bits' are set on the
312 * struct before it is inserted.
313 *
314 * This may return -EEXIST if the extent is already there, in which case the
315 * state struct is freed.
316 *
317 * The tree lock is not taken internally. This is a utility function and
318 * probably isn't what you want to call (see set/clear_extent_bit).
319 */
320static int insert_state(struct extent_io_tree *tree,
321 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400322 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500323{
324 struct rb_node *node;
325
326 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500327 printk(KERN_ERR "btrfs end < start %llu %llu\n",
328 (unsigned long long)end,
329 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500330 WARN_ON(1);
331 }
Chris Masond1310b22008-01-24 16:13:08 -0500332 state->start = start;
333 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400334
Xiao Guangrong3150b692011-07-14 03:19:08 +0000335 set_state_bits(tree, state, bits);
336
Chris Masond1310b22008-01-24 16:13:08 -0500337 node = tree_insert(&tree->state, end, &state->rb_node);
338 if (node) {
339 struct extent_state *found;
340 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500341 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
342 "%llu %llu\n", (unsigned long long)found->start,
343 (unsigned long long)found->end,
344 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500345 return -EEXIST;
346 }
Chris Mason70dec802008-01-29 09:59:12 -0500347 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500348 merge_state(tree, state);
349 return 0;
350}
351
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000352static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400353 u64 split)
354{
355 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000356 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400357}
358
Chris Masond1310b22008-01-24 16:13:08 -0500359/*
360 * split a given extent state struct in two, inserting the preallocated
361 * struct 'prealloc' as the newly created second half. 'split' indicates an
362 * offset inside 'orig' where it should be split.
363 *
364 * Before calling,
365 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
366 * are two extent state structs in the tree:
367 * prealloc: [orig->start, split - 1]
368 * orig: [ split, orig->end ]
369 *
370 * The tree locks are not taken by this function. They need to be held
371 * by the caller.
372 */
373static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
374 struct extent_state *prealloc, u64 split)
375{
376 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400377
378 split_cb(tree, orig, split);
379
Chris Masond1310b22008-01-24 16:13:08 -0500380 prealloc->start = orig->start;
381 prealloc->end = split - 1;
382 prealloc->state = orig->state;
383 orig->start = split;
384
385 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
386 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500387 free_extent_state(prealloc);
388 return -EEXIST;
389 }
Chris Mason70dec802008-01-29 09:59:12 -0500390 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500391 return 0;
392}
393
394/*
395 * utility function to clear some bits in an extent state struct.
396 * it will optionally wake up any one waiting on this state (wake == 1), or
397 * forcibly remove the state from the tree (delete == 1).
398 *
399 * If no bits are set on the state struct after clearing things, the
400 * struct is freed and removed from the tree
401 */
402static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400403 struct extent_state *state,
404 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500405{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400406 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400407 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500408
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400409 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500410 u64 range = state->end - state->start + 1;
411 WARN_ON(range > tree->dirty_bytes);
412 tree->dirty_bytes -= range;
413 }
Chris Mason291d6732008-01-29 15:55:23 -0500414 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400415 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500416 if (wake)
417 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500419 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500420 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500421 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500422 free_extent_state(state);
423 } else {
424 WARN_ON(1);
425 }
426 } else {
427 merge_state(tree, state);
428 }
429 return ret;
430}
431
Xiao Guangrong82337672011-04-20 06:44:57 +0000432static struct extent_state *
433alloc_extent_state_atomic(struct extent_state *prealloc)
434{
435 if (!prealloc)
436 prealloc = alloc_extent_state(GFP_ATOMIC);
437
438 return prealloc;
439}
440
Chris Masond1310b22008-01-24 16:13:08 -0500441/*
442 * clear some bits on a range in the tree. This may require splitting
443 * or inserting elements in the tree, so the gfp mask is used to
444 * indicate which allocations or sleeping are allowed.
445 *
446 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
447 * the given range from the tree regardless of state (ie for truncate).
448 *
449 * the range [start, end] is inclusive.
450 *
451 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
452 * bits were already set, or zero if none of the bits were already set.
453 */
454int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400455 int bits, int wake, int delete,
456 struct extent_state **cached_state,
457 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500458{
459 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400460 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500461 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400462 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500463 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400464 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500465 int err;
466 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000467 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500468
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400469 if (delete)
470 bits |= ~EXTENT_CTLBITS;
471 bits |= EXTENT_FIRST_DELALLOC;
472
Josef Bacik2ac55d42010-02-03 19:33:23 +0000473 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
474 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500475again:
476 if (!prealloc && (mask & __GFP_WAIT)) {
477 prealloc = alloc_extent_state(mask);
478 if (!prealloc)
479 return -ENOMEM;
480 }
481
Chris Masoncad321a2008-12-17 14:51:42 -0500482 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400483 if (cached_state) {
484 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000485
486 if (clear) {
487 *cached_state = NULL;
488 cached_state = NULL;
489 }
490
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400491 if (cached && cached->tree && cached->start <= start &&
492 cached->end > 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) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000532 prealloc = alloc_extent_state_atomic(prealloc);
533 BUG_ON(!prealloc);
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) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000554 prealloc = alloc_extent_state_atomic(prealloc);
555 BUG_ON(!prealloc);
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
Xiao Guangrongded91f02011-07-14 03:19:27 +0000652 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500653 }
654out:
Chris Masoncad321a2008-12-17 14:51:42 -0500655 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500656 return 0;
657}
Chris Masond1310b22008-01-24 16:13:08 -0500658
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000659static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500660 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400661 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500662{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400663 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400664
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000665 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400666 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500667 u64 range = state->end - state->start + 1;
668 tree->dirty_bytes += range;
669 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400670 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500671}
672
Chris Mason2c64c532009-09-02 15:04:12 -0400673static void cache_state(struct extent_state *state,
674 struct extent_state **cached_ptr)
675{
676 if (cached_ptr && !(*cached_ptr)) {
677 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
678 *cached_ptr = state;
679 atomic_inc(&state->refs);
680 }
681 }
682}
683
Arne Jansen507903b2011-04-06 10:02:20 +0000684static void uncache_state(struct extent_state **cached_ptr)
685{
686 if (cached_ptr && (*cached_ptr)) {
687 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400688 *cached_ptr = NULL;
689 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000690 }
691}
692
Chris Masond1310b22008-01-24 16:13:08 -0500693/*
Chris Mason1edbb732009-09-02 13:24:36 -0400694 * set some bits on a range in the tree. This may require allocations or
695 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500696 *
Chris Mason1edbb732009-09-02 13:24:36 -0400697 * If any of the exclusive bits are set, this will fail with -EEXIST if some
698 * part of the range already has the desired bits set. The start of the
699 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500700 *
Chris Mason1edbb732009-09-02 13:24:36 -0400701 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500702 */
Chris Mason1edbb732009-09-02 13:24:36 -0400703
Chris Mason4845e442010-05-25 20:56:50 -0400704int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
705 int bits, int exclusive_bits, u64 *failed_start,
706 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500707{
708 struct extent_state *state;
709 struct extent_state *prealloc = NULL;
710 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500711 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500712 u64 last_start;
713 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400714
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400715 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500716again:
717 if (!prealloc && (mask & __GFP_WAIT)) {
718 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000719 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500720 }
721
Chris Masoncad321a2008-12-17 14:51:42 -0500722 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400723 if (cached_state && *cached_state) {
724 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400725 if (state->start <= start && state->end > start &&
726 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400727 node = &state->rb_node;
728 goto hit_next;
729 }
730 }
Chris Masond1310b22008-01-24 16:13:08 -0500731 /*
732 * this search will find all the extents that end after
733 * our range starts.
734 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500735 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500736 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000737 prealloc = alloc_extent_state_atomic(prealloc);
738 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400739 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500740 prealloc = NULL;
741 BUG_ON(err == -EEXIST);
742 goto out;
743 }
Chris Masond1310b22008-01-24 16:13:08 -0500744 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400745hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500746 last_start = state->start;
747 last_end = state->end;
748
749 /*
750 * | ---- desired range ---- |
751 * | state |
752 *
753 * Just lock what we found and keep going
754 */
755 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400756 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400757 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500758 *failed_start = state->start;
759 err = -EEXIST;
760 goto out;
761 }
Chris Mason42daec22009-09-23 19:51:09 -0400762
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000763 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400764
Chris Mason2c64c532009-09-02 15:04:12 -0400765 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500766 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400767 if (last_end == (u64)-1)
768 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400769
Yan Zheng5c939df2009-05-27 09:16:03 -0400770 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400771 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000772 if (next_node && start < end && prealloc && !need_resched()) {
773 state = rb_entry(next_node, struct extent_state,
774 rb_node);
775 if (state->start == start)
776 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400777 }
Chris Masond1310b22008-01-24 16:13:08 -0500778 goto search_again;
779 }
780
781 /*
782 * | ---- desired range ---- |
783 * | state |
784 * or
785 * | ------------- state -------------- |
786 *
787 * We need to split the extent we found, and may flip bits on
788 * second half.
789 *
790 * If the extent we found extends past our
791 * range, we just split and search again. It'll get split
792 * again the next time though.
793 *
794 * If the extent we found is inside our range, we set the
795 * desired bit on it.
796 */
797 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400798 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500799 *failed_start = start;
800 err = -EEXIST;
801 goto out;
802 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000803
804 prealloc = alloc_extent_state_atomic(prealloc);
805 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500806 err = split_state(tree, state, prealloc, start);
807 BUG_ON(err == -EEXIST);
808 prealloc = NULL;
809 if (err)
810 goto out;
811 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000812 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400813 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500814 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400815 if (last_end == (u64)-1)
816 goto out;
817 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500818 }
819 goto search_again;
820 }
821 /*
822 * | ---- desired range ---- |
823 * | state | or | state |
824 *
825 * There's a hole, we need to insert something in it and
826 * ignore the extent we found.
827 */
828 if (state->start > start) {
829 u64 this_end;
830 if (end < last_start)
831 this_end = end;
832 else
Chris Masond3977122009-01-05 21:25:51 -0500833 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000834
835 prealloc = alloc_extent_state_atomic(prealloc);
836 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000837
838 /*
839 * Avoid to free 'prealloc' if it can be merged with
840 * the later extent.
841 */
Chris Masond1310b22008-01-24 16:13:08 -0500842 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400843 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400844 BUG_ON(err == -EEXIST);
845 if (err) {
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000846 free_extent_state(prealloc);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400847 prealloc = NULL;
848 goto out;
849 }
Chris Mason2c64c532009-09-02 15:04:12 -0400850 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500851 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500852 start = this_end + 1;
853 goto search_again;
854 }
855 /*
856 * | ---- desired range ---- |
857 * | state |
858 * We need to split the extent, and set the bit
859 * on the first half
860 */
861 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400862 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500863 *failed_start = start;
864 err = -EEXIST;
865 goto out;
866 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000867
868 prealloc = alloc_extent_state_atomic(prealloc);
869 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500870 err = split_state(tree, state, prealloc, end + 1);
871 BUG_ON(err == -EEXIST);
872
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000873 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400874 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500875 merge_state(tree, prealloc);
876 prealloc = NULL;
877 goto out;
878 }
879
880 goto search_again;
881
882out:
Chris Masoncad321a2008-12-17 14:51:42 -0500883 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500884 if (prealloc)
885 free_extent_state(prealloc);
886
887 return err;
888
889search_again:
890 if (start > end)
891 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500892 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500893 if (mask & __GFP_WAIT)
894 cond_resched();
895 goto again;
896}
Chris Masond1310b22008-01-24 16:13:08 -0500897
898/* wrappers around set/clear extent bit */
899int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
900 gfp_t mask)
901{
902 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400903 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500904}
Chris Masond1310b22008-01-24 16:13:08 -0500905
906int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
907 int bits, gfp_t mask)
908{
909 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400910 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500911}
Chris Masond1310b22008-01-24 16:13:08 -0500912
913int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
914 int bits, gfp_t mask)
915{
Chris Mason2c64c532009-09-02 15:04:12 -0400916 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500917}
Chris Masond1310b22008-01-24 16:13:08 -0500918
919int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000920 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500921{
922 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400923 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000924 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500925}
Chris Masond1310b22008-01-24 16:13:08 -0500926
927int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
928 gfp_t mask)
929{
930 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400931 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400932 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500933}
Chris Masond1310b22008-01-24 16:13:08 -0500934
935int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
936 gfp_t mask)
937{
938 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400939 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500940}
Chris Masond1310b22008-01-24 16:13:08 -0500941
Chris Masond1310b22008-01-24 16:13:08 -0500942int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +0000943 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500944{
Arne Jansen507903b2011-04-06 10:02:20 +0000945 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
946 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500947}
Chris Masond1310b22008-01-24 16:13:08 -0500948
Chris Masond3977122009-01-05 21:25:51 -0500949static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000950 u64 end, struct extent_state **cached_state,
951 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500952{
Chris Mason2c64c532009-09-02 15:04:12 -0400953 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000954 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500955}
Chris Masond1310b22008-01-24 16:13:08 -0500956
Chris Masond352ac62008-09-29 15:18:18 -0400957/*
958 * either insert or lock state struct between start and end use mask to tell
959 * us if waiting is desired.
960 */
Chris Mason1edbb732009-09-02 13:24:36 -0400961int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400962 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500963{
964 int err;
965 u64 failed_start;
966 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -0400967 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -0400968 EXTENT_LOCKED, &failed_start,
969 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500970 if (err == -EEXIST && (mask & __GFP_WAIT)) {
971 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
972 start = failed_start;
973 } else {
974 break;
975 }
976 WARN_ON(start > end);
977 }
978 return err;
979}
Chris Masond1310b22008-01-24 16:13:08 -0500980
Chris Mason1edbb732009-09-02 13:24:36 -0400981int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
982{
Chris Mason2c64c532009-09-02 15:04:12 -0400983 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -0400984}
985
Josef Bacik25179202008-10-29 14:49:05 -0400986int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
987 gfp_t mask)
988{
989 int err;
990 u64 failed_start;
991
Chris Mason2c64c532009-09-02 15:04:12 -0400992 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
993 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -0400994 if (err == -EEXIST) {
995 if (failed_start > start)
996 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -0400997 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -0400998 return 0;
Yan Zheng66435582008-10-30 14:19:50 -0400999 }
Josef Bacik25179202008-10-29 14:49:05 -04001000 return 1;
1001}
Josef Bacik25179202008-10-29 14:49:05 -04001002
Chris Mason2c64c532009-09-02 15:04:12 -04001003int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1004 struct extent_state **cached, gfp_t mask)
1005{
1006 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1007 mask);
1008}
1009
Arne Jansen507903b2011-04-06 10:02:20 +00001010int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001011{
Chris Mason2c64c532009-09-02 15:04:12 -04001012 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1013 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001014}
Chris Masond1310b22008-01-24 16:13:08 -05001015
1016/*
Chris Masond1310b22008-01-24 16:13:08 -05001017 * helper function to set both pages and extents in the tree writeback
1018 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001019static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001020{
1021 unsigned long index = start >> PAGE_CACHE_SHIFT;
1022 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1023 struct page *page;
1024
1025 while (index <= end_index) {
1026 page = find_get_page(tree->mapping, index);
1027 BUG_ON(!page);
1028 set_page_writeback(page);
1029 page_cache_release(page);
1030 index++;
1031 }
Chris Masond1310b22008-01-24 16:13:08 -05001032 return 0;
1033}
Chris Masond1310b22008-01-24 16:13:08 -05001034
Chris Masond352ac62008-09-29 15:18:18 -04001035/* find the first state struct with 'bits' set after 'start', and
1036 * return it. tree->lock must be held. NULL will returned if
1037 * nothing was found after 'start'
1038 */
Chris Masond7fc6402008-02-18 12:12:38 -05001039struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1040 u64 start, int bits)
1041{
1042 struct rb_node *node;
1043 struct extent_state *state;
1044
1045 /*
1046 * this search will find all the extents that end after
1047 * our range starts.
1048 */
1049 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001050 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001051 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001052
Chris Masond3977122009-01-05 21:25:51 -05001053 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001054 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001055 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001056 return state;
Chris Masond3977122009-01-05 21:25:51 -05001057
Chris Masond7fc6402008-02-18 12:12:38 -05001058 node = rb_next(node);
1059 if (!node)
1060 break;
1061 }
1062out:
1063 return NULL;
1064}
Chris Masond7fc6402008-02-18 12:12:38 -05001065
Chris Masond352ac62008-09-29 15:18:18 -04001066/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001067 * find the first offset in the io tree with 'bits' set. zero is
1068 * returned if we find something, and *start_ret and *end_ret are
1069 * set to reflect the state struct that was found.
1070 *
1071 * If nothing was found, 1 is returned, < 0 on error
1072 */
1073int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1074 u64 *start_ret, u64 *end_ret, int bits)
1075{
1076 struct extent_state *state;
1077 int ret = 1;
1078
1079 spin_lock(&tree->lock);
1080 state = find_first_extent_bit_state(tree, start, bits);
1081 if (state) {
1082 *start_ret = state->start;
1083 *end_ret = state->end;
1084 ret = 0;
1085 }
1086 spin_unlock(&tree->lock);
1087 return ret;
1088}
1089
1090/*
Chris Masond352ac62008-09-29 15:18:18 -04001091 * find a contiguous range of bytes in the file marked as delalloc, not
1092 * more than 'max_bytes'. start and end are used to return the range,
1093 *
1094 * 1 is returned if we find something, 0 if nothing was in the tree
1095 */
Chris Masonc8b97812008-10-29 14:49:59 -04001096static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001097 u64 *start, u64 *end, u64 max_bytes,
1098 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001099{
1100 struct rb_node *node;
1101 struct extent_state *state;
1102 u64 cur_start = *start;
1103 u64 found = 0;
1104 u64 total_bytes = 0;
1105
Chris Masoncad321a2008-12-17 14:51:42 -05001106 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001107
Chris Masond1310b22008-01-24 16:13:08 -05001108 /*
1109 * this search will find all the extents that end after
1110 * our range starts.
1111 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001112 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001113 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001114 if (!found)
1115 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001116 goto out;
1117 }
1118
Chris Masond3977122009-01-05 21:25:51 -05001119 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001120 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001121 if (found && (state->start != cur_start ||
1122 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001123 goto out;
1124 }
1125 if (!(state->state & EXTENT_DELALLOC)) {
1126 if (!found)
1127 *end = state->end;
1128 goto out;
1129 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001130 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001131 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001132 *cached_state = state;
1133 atomic_inc(&state->refs);
1134 }
Chris Masond1310b22008-01-24 16:13:08 -05001135 found++;
1136 *end = state->end;
1137 cur_start = state->end + 1;
1138 node = rb_next(node);
1139 if (!node)
1140 break;
1141 total_bytes += state->end - state->start + 1;
1142 if (total_bytes >= max_bytes)
1143 break;
1144 }
1145out:
Chris Masoncad321a2008-12-17 14:51:42 -05001146 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001147 return found;
1148}
1149
Chris Masonc8b97812008-10-29 14:49:59 -04001150static noinline int __unlock_for_delalloc(struct inode *inode,
1151 struct page *locked_page,
1152 u64 start, u64 end)
1153{
1154 int ret;
1155 struct page *pages[16];
1156 unsigned long index = start >> PAGE_CACHE_SHIFT;
1157 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1158 unsigned long nr_pages = end_index - index + 1;
1159 int i;
1160
1161 if (index == locked_page->index && end_index == index)
1162 return 0;
1163
Chris Masond3977122009-01-05 21:25:51 -05001164 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001165 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001166 min_t(unsigned long, nr_pages,
1167 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001168 for (i = 0; i < ret; i++) {
1169 if (pages[i] != locked_page)
1170 unlock_page(pages[i]);
1171 page_cache_release(pages[i]);
1172 }
1173 nr_pages -= ret;
1174 index += ret;
1175 cond_resched();
1176 }
1177 return 0;
1178}
1179
1180static noinline int lock_delalloc_pages(struct inode *inode,
1181 struct page *locked_page,
1182 u64 delalloc_start,
1183 u64 delalloc_end)
1184{
1185 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1186 unsigned long start_index = index;
1187 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1188 unsigned long pages_locked = 0;
1189 struct page *pages[16];
1190 unsigned long nrpages;
1191 int ret;
1192 int i;
1193
1194 /* the caller is responsible for locking the start index */
1195 if (index == locked_page->index && index == end_index)
1196 return 0;
1197
1198 /* skip the page at the start index */
1199 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001200 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001201 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001202 min_t(unsigned long,
1203 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001204 if (ret == 0) {
1205 ret = -EAGAIN;
1206 goto done;
1207 }
1208 /* now we have an array of pages, lock them all */
1209 for (i = 0; i < ret; i++) {
1210 /*
1211 * the caller is taking responsibility for
1212 * locked_page
1213 */
Chris Mason771ed682008-11-06 22:02:51 -05001214 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001215 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001216 if (!PageDirty(pages[i]) ||
1217 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001218 ret = -EAGAIN;
1219 unlock_page(pages[i]);
1220 page_cache_release(pages[i]);
1221 goto done;
1222 }
1223 }
Chris Masonc8b97812008-10-29 14:49:59 -04001224 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001225 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001226 }
Chris Masonc8b97812008-10-29 14:49:59 -04001227 nrpages -= ret;
1228 index += ret;
1229 cond_resched();
1230 }
1231 ret = 0;
1232done:
1233 if (ret && pages_locked) {
1234 __unlock_for_delalloc(inode, locked_page,
1235 delalloc_start,
1236 ((u64)(start_index + pages_locked - 1)) <<
1237 PAGE_CACHE_SHIFT);
1238 }
1239 return ret;
1240}
1241
1242/*
1243 * find a contiguous range of bytes in the file marked as delalloc, not
1244 * more than 'max_bytes'. start and end are used to return the range,
1245 *
1246 * 1 is returned if we find something, 0 if nothing was in the tree
1247 */
1248static noinline u64 find_lock_delalloc_range(struct inode *inode,
1249 struct extent_io_tree *tree,
1250 struct page *locked_page,
1251 u64 *start, u64 *end,
1252 u64 max_bytes)
1253{
1254 u64 delalloc_start;
1255 u64 delalloc_end;
1256 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001257 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001258 int ret;
1259 int loops = 0;
1260
1261again:
1262 /* step one, find a bunch of delalloc bytes starting at start */
1263 delalloc_start = *start;
1264 delalloc_end = 0;
1265 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001266 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001267 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001268 *start = delalloc_start;
1269 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001270 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001271 return found;
1272 }
1273
1274 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001275 * start comes from the offset of locked_page. We have to lock
1276 * pages in order, so we can't process delalloc bytes before
1277 * locked_page
1278 */
Chris Masond3977122009-01-05 21:25:51 -05001279 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001280 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001281
1282 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001283 * make sure to limit the number of pages we try to lock down
1284 * if we're looping.
1285 */
Chris Masond3977122009-01-05 21:25:51 -05001286 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001287 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001288
Chris Masonc8b97812008-10-29 14:49:59 -04001289 /* step two, lock all the pages after the page that has start */
1290 ret = lock_delalloc_pages(inode, locked_page,
1291 delalloc_start, delalloc_end);
1292 if (ret == -EAGAIN) {
1293 /* some of the pages are gone, lets avoid looping by
1294 * shortening the size of the delalloc range we're searching
1295 */
Chris Mason9655d292009-09-02 15:22:30 -04001296 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001297 if (!loops) {
1298 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1299 max_bytes = PAGE_CACHE_SIZE - offset;
1300 loops = 1;
1301 goto again;
1302 } else {
1303 found = 0;
1304 goto out_failed;
1305 }
1306 }
1307 BUG_ON(ret);
1308
1309 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001310 lock_extent_bits(tree, delalloc_start, delalloc_end,
1311 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001312
1313 /* then test to make sure it is all still delalloc */
1314 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001315 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001316 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001317 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1318 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001319 __unlock_for_delalloc(inode, locked_page,
1320 delalloc_start, delalloc_end);
1321 cond_resched();
1322 goto again;
1323 }
Chris Mason9655d292009-09-02 15:22:30 -04001324 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001325 *start = delalloc_start;
1326 *end = delalloc_end;
1327out_failed:
1328 return found;
1329}
1330
1331int extent_clear_unlock_delalloc(struct inode *inode,
1332 struct extent_io_tree *tree,
1333 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001334 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001335{
1336 int ret;
1337 struct page *pages[16];
1338 unsigned long index = start >> PAGE_CACHE_SHIFT;
1339 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1340 unsigned long nr_pages = end_index - index + 1;
1341 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001342 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001343
Chris Masona791e352009-10-08 11:27:10 -04001344 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001345 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001346 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001347 clear_bits |= EXTENT_DIRTY;
1348
Chris Masona791e352009-10-08 11:27:10 -04001349 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001350 clear_bits |= EXTENT_DELALLOC;
1351
Chris Mason2c64c532009-09-02 15:04:12 -04001352 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001353 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1354 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1355 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001356 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001357
Chris Masond3977122009-01-05 21:25:51 -05001358 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001359 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001360 min_t(unsigned long,
1361 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001362 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001363
Chris Masona791e352009-10-08 11:27:10 -04001364 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001365 SetPagePrivate2(pages[i]);
1366
Chris Masonc8b97812008-10-29 14:49:59 -04001367 if (pages[i] == locked_page) {
1368 page_cache_release(pages[i]);
1369 continue;
1370 }
Chris Masona791e352009-10-08 11:27:10 -04001371 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001372 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001373 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001374 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001375 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001376 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001377 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001378 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001379 page_cache_release(pages[i]);
1380 }
1381 nr_pages -= ret;
1382 index += ret;
1383 cond_resched();
1384 }
1385 return 0;
1386}
Chris Masonc8b97812008-10-29 14:49:59 -04001387
Chris Masond352ac62008-09-29 15:18:18 -04001388/*
1389 * count the number of bytes in the tree that have a given bit(s)
1390 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1391 * cached. The total number found is returned.
1392 */
Chris Masond1310b22008-01-24 16:13:08 -05001393u64 count_range_bits(struct extent_io_tree *tree,
1394 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001395 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001396{
1397 struct rb_node *node;
1398 struct extent_state *state;
1399 u64 cur_start = *start;
1400 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001401 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001402 int found = 0;
1403
1404 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001405 WARN_ON(1);
1406 return 0;
1407 }
1408
Chris Masoncad321a2008-12-17 14:51:42 -05001409 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001410 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1411 total_bytes = tree->dirty_bytes;
1412 goto out;
1413 }
1414 /*
1415 * this search will find all the extents that end after
1416 * our range starts.
1417 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001418 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001419 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001420 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001421
Chris Masond3977122009-01-05 21:25:51 -05001422 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001423 state = rb_entry(node, struct extent_state, rb_node);
1424 if (state->start > search_end)
1425 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001426 if (contig && found && state->start > last + 1)
1427 break;
1428 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001429 total_bytes += min(search_end, state->end) + 1 -
1430 max(cur_start, state->start);
1431 if (total_bytes >= max_bytes)
1432 break;
1433 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001434 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001435 found = 1;
1436 }
Chris Masonec29ed52011-02-23 16:23:20 -05001437 last = state->end;
1438 } else if (contig && found) {
1439 break;
Chris Masond1310b22008-01-24 16:13:08 -05001440 }
1441 node = rb_next(node);
1442 if (!node)
1443 break;
1444 }
1445out:
Chris Masoncad321a2008-12-17 14:51:42 -05001446 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001447 return total_bytes;
1448}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001449
Chris Masond352ac62008-09-29 15:18:18 -04001450/*
1451 * set the private field for a given byte offset in the tree. If there isn't
1452 * an extent_state there already, this does nothing.
1453 */
Chris Masond1310b22008-01-24 16:13:08 -05001454int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1455{
1456 struct rb_node *node;
1457 struct extent_state *state;
1458 int ret = 0;
1459
Chris Masoncad321a2008-12-17 14:51:42 -05001460 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001461 /*
1462 * this search will find all the extents that end after
1463 * our range starts.
1464 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001465 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001466 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001467 ret = -ENOENT;
1468 goto out;
1469 }
1470 state = rb_entry(node, struct extent_state, rb_node);
1471 if (state->start != start) {
1472 ret = -ENOENT;
1473 goto out;
1474 }
1475 state->private = private;
1476out:
Chris Masoncad321a2008-12-17 14:51:42 -05001477 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001478 return ret;
1479}
1480
1481int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1482{
1483 struct rb_node *node;
1484 struct extent_state *state;
1485 int ret = 0;
1486
Chris Masoncad321a2008-12-17 14:51:42 -05001487 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001488 /*
1489 * this search will find all the extents that end after
1490 * our range starts.
1491 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001492 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001493 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001494 ret = -ENOENT;
1495 goto out;
1496 }
1497 state = rb_entry(node, struct extent_state, rb_node);
1498 if (state->start != start) {
1499 ret = -ENOENT;
1500 goto out;
1501 }
1502 *private = state->private;
1503out:
Chris Masoncad321a2008-12-17 14:51:42 -05001504 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001505 return ret;
1506}
1507
1508/*
1509 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001510 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001511 * has the bits set. Otherwise, 1 is returned if any bit in the
1512 * range is found set.
1513 */
1514int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001515 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001516{
1517 struct extent_state *state = NULL;
1518 struct rb_node *node;
1519 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001520
Chris Masoncad321a2008-12-17 14:51:42 -05001521 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001522 if (cached && cached->tree && cached->start <= start &&
1523 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001524 node = &cached->rb_node;
1525 else
1526 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001527 while (node && start <= end) {
1528 state = rb_entry(node, struct extent_state, rb_node);
1529
1530 if (filled && state->start > start) {
1531 bitset = 0;
1532 break;
1533 }
1534
1535 if (state->start > end)
1536 break;
1537
1538 if (state->state & bits) {
1539 bitset = 1;
1540 if (!filled)
1541 break;
1542 } else if (filled) {
1543 bitset = 0;
1544 break;
1545 }
Chris Mason46562ce2009-09-23 20:23:16 -04001546
1547 if (state->end == (u64)-1)
1548 break;
1549
Chris Masond1310b22008-01-24 16:13:08 -05001550 start = state->end + 1;
1551 if (start > end)
1552 break;
1553 node = rb_next(node);
1554 if (!node) {
1555 if (filled)
1556 bitset = 0;
1557 break;
1558 }
1559 }
Chris Masoncad321a2008-12-17 14:51:42 -05001560 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001561 return bitset;
1562}
Chris Masond1310b22008-01-24 16:13:08 -05001563
1564/*
1565 * helper function to set a given page up to date if all the
1566 * extents in the tree for that page are up to date
1567 */
1568static int check_page_uptodate(struct extent_io_tree *tree,
1569 struct page *page)
1570{
1571 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1572 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001573 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001574 SetPageUptodate(page);
1575 return 0;
1576}
1577
1578/*
1579 * helper function to unlock a page if all the extents in the tree
1580 * for that page are unlocked
1581 */
1582static int check_page_locked(struct extent_io_tree *tree,
1583 struct page *page)
1584{
1585 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1586 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001587 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001588 unlock_page(page);
1589 return 0;
1590}
1591
1592/*
1593 * helper function to end page writeback if all the extents
1594 * in the tree for that page are done with writeback
1595 */
1596static int check_page_writeback(struct extent_io_tree *tree,
1597 struct page *page)
1598{
Chris Mason1edbb732009-09-02 13:24:36 -04001599 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001600 return 0;
1601}
1602
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001603/*
1604 * When IO fails, either with EIO or csum verification fails, we
1605 * try other mirrors that might have a good copy of the data. This
1606 * io_failure_record is used to record state as we go through all the
1607 * mirrors. If another mirror has good data, the page is set up to date
1608 * and things continue. If a good mirror can't be found, the original
1609 * bio end_io callback is called to indicate things have failed.
1610 */
1611struct io_failure_record {
1612 struct page *page;
1613 u64 start;
1614 u64 len;
1615 u64 logical;
1616 unsigned long bio_flags;
1617 int this_mirror;
1618 int failed_mirror;
1619 int in_validation;
1620};
1621
1622static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1623 int did_repair)
1624{
1625 int ret;
1626 int err = 0;
1627 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1628
1629 set_state_private(failure_tree, rec->start, 0);
1630 ret = clear_extent_bits(failure_tree, rec->start,
1631 rec->start + rec->len - 1,
1632 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1633 if (ret)
1634 err = ret;
1635
1636 if (did_repair) {
1637 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1638 rec->start + rec->len - 1,
1639 EXTENT_DAMAGED, GFP_NOFS);
1640 if (ret && !err)
1641 err = ret;
1642 }
1643
1644 kfree(rec);
1645 return err;
1646}
1647
1648static void repair_io_failure_callback(struct bio *bio, int err)
1649{
1650 complete(bio->bi_private);
1651}
1652
1653/*
1654 * this bypasses the standard btrfs submit functions deliberately, as
1655 * the standard behavior is to write all copies in a raid setup. here we only
1656 * want to write the one bad copy. so we do the mapping for ourselves and issue
1657 * submit_bio directly.
1658 * to avoid any synchonization issues, wait for the data after writing, which
1659 * actually prevents the read that triggered the error from finishing.
1660 * currently, there can be no more than two copies of every data bit. thus,
1661 * exactly one rewrite is required.
1662 */
1663int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1664 u64 length, u64 logical, struct page *page,
1665 int mirror_num)
1666{
1667 struct bio *bio;
1668 struct btrfs_device *dev;
1669 DECLARE_COMPLETION_ONSTACK(compl);
1670 u64 map_length = 0;
1671 u64 sector;
1672 struct btrfs_bio *bbio = NULL;
1673 int ret;
1674
1675 BUG_ON(!mirror_num);
1676
1677 bio = bio_alloc(GFP_NOFS, 1);
1678 if (!bio)
1679 return -EIO;
1680 bio->bi_private = &compl;
1681 bio->bi_end_io = repair_io_failure_callback;
1682 bio->bi_size = 0;
1683 map_length = length;
1684
1685 ret = btrfs_map_block(map_tree, WRITE, logical,
1686 &map_length, &bbio, mirror_num);
1687 if (ret) {
1688 bio_put(bio);
1689 return -EIO;
1690 }
1691 BUG_ON(mirror_num != bbio->mirror_num);
1692 sector = bbio->stripes[mirror_num-1].physical >> 9;
1693 bio->bi_sector = sector;
1694 dev = bbio->stripes[mirror_num-1].dev;
1695 kfree(bbio);
1696 if (!dev || !dev->bdev || !dev->writeable) {
1697 bio_put(bio);
1698 return -EIO;
1699 }
1700 bio->bi_bdev = dev->bdev;
1701 bio_add_page(bio, page, length, start-page_offset(page));
1702 submit_bio(WRITE_SYNC, bio);
1703 wait_for_completion(&compl);
1704
1705 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1706 /* try to remap that extent elsewhere? */
1707 bio_put(bio);
1708 return -EIO;
1709 }
1710
1711 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1712 "sector %llu)\n", page->mapping->host->i_ino, start,
1713 dev->name, sector);
1714
1715 bio_put(bio);
1716 return 0;
1717}
1718
1719/*
1720 * each time an IO finishes, we do a fast check in the IO failure tree
1721 * to see if we need to process or clean up an io_failure_record
1722 */
1723static int clean_io_failure(u64 start, struct page *page)
1724{
1725 u64 private;
1726 u64 private_failure;
1727 struct io_failure_record *failrec;
1728 struct btrfs_mapping_tree *map_tree;
1729 struct extent_state *state;
1730 int num_copies;
1731 int did_repair = 0;
1732 int ret;
1733 struct inode *inode = page->mapping->host;
1734
1735 private = 0;
1736 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1737 (u64)-1, 1, EXTENT_DIRTY, 0);
1738 if (!ret)
1739 return 0;
1740
1741 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1742 &private_failure);
1743 if (ret)
1744 return 0;
1745
1746 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1747 BUG_ON(!failrec->this_mirror);
1748
1749 if (failrec->in_validation) {
1750 /* there was no real error, just free the record */
1751 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1752 failrec->start);
1753 did_repair = 1;
1754 goto out;
1755 }
1756
1757 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1758 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1759 failrec->start,
1760 EXTENT_LOCKED);
1761 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1762
1763 if (state && state->start == failrec->start) {
1764 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1765 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1766 failrec->len);
1767 if (num_copies > 1) {
1768 ret = repair_io_failure(map_tree, start, failrec->len,
1769 failrec->logical, page,
1770 failrec->failed_mirror);
1771 did_repair = !ret;
1772 }
1773 }
1774
1775out:
1776 if (!ret)
1777 ret = free_io_failure(inode, failrec, did_repair);
1778
1779 return ret;
1780}
1781
1782/*
1783 * this is a generic handler for readpage errors (default
1784 * readpage_io_failed_hook). if other copies exist, read those and write back
1785 * good data to the failed position. does not investigate in remapping the
1786 * failed extent elsewhere, hoping the device will be smart enough to do this as
1787 * needed
1788 */
1789
1790static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1791 u64 start, u64 end, int failed_mirror,
1792 struct extent_state *state)
1793{
1794 struct io_failure_record *failrec = NULL;
1795 u64 private;
1796 struct extent_map *em;
1797 struct inode *inode = page->mapping->host;
1798 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1799 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1800 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1801 struct bio *bio;
1802 int num_copies;
1803 int ret;
1804 int read_mode;
1805 u64 logical;
1806
1807 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
1808
1809 ret = get_state_private(failure_tree, start, &private);
1810 if (ret) {
1811 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
1812 if (!failrec)
1813 return -ENOMEM;
1814 failrec->start = start;
1815 failrec->len = end - start + 1;
1816 failrec->this_mirror = 0;
1817 failrec->bio_flags = 0;
1818 failrec->in_validation = 0;
1819
1820 read_lock(&em_tree->lock);
1821 em = lookup_extent_mapping(em_tree, start, failrec->len);
1822 if (!em) {
1823 read_unlock(&em_tree->lock);
1824 kfree(failrec);
1825 return -EIO;
1826 }
1827
1828 if (em->start > start || em->start + em->len < start) {
1829 free_extent_map(em);
1830 em = NULL;
1831 }
1832 read_unlock(&em_tree->lock);
1833
1834 if (!em || IS_ERR(em)) {
1835 kfree(failrec);
1836 return -EIO;
1837 }
1838 logical = start - em->start;
1839 logical = em->block_start + logical;
1840 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1841 logical = em->block_start;
1842 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1843 extent_set_compress_type(&failrec->bio_flags,
1844 em->compress_type);
1845 }
1846 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
1847 "len=%llu\n", logical, start, failrec->len);
1848 failrec->logical = logical;
1849 free_extent_map(em);
1850
1851 /* set the bits in the private failure tree */
1852 ret = set_extent_bits(failure_tree, start, end,
1853 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1854 if (ret >= 0)
1855 ret = set_state_private(failure_tree, start,
1856 (u64)(unsigned long)failrec);
1857 /* set the bits in the inode's tree */
1858 if (ret >= 0)
1859 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
1860 GFP_NOFS);
1861 if (ret < 0) {
1862 kfree(failrec);
1863 return ret;
1864 }
1865 } else {
1866 failrec = (struct io_failure_record *)(unsigned long)private;
1867 pr_debug("bio_readpage_error: (found) logical=%llu, "
1868 "start=%llu, len=%llu, validation=%d\n",
1869 failrec->logical, failrec->start, failrec->len,
1870 failrec->in_validation);
1871 /*
1872 * when data can be on disk more than twice, add to failrec here
1873 * (e.g. with a list for failed_mirror) to make
1874 * clean_io_failure() clean all those errors at once.
1875 */
1876 }
1877 num_copies = btrfs_num_copies(
1878 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1879 failrec->logical, failrec->len);
1880 if (num_copies == 1) {
1881 /*
1882 * we only have a single copy of the data, so don't bother with
1883 * all the retry and error correction code that follows. no
1884 * matter what the error is, it is very likely to persist.
1885 */
1886 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
1887 "state=%p, num_copies=%d, next_mirror %d, "
1888 "failed_mirror %d\n", state, num_copies,
1889 failrec->this_mirror, failed_mirror);
1890 free_io_failure(inode, failrec, 0);
1891 return -EIO;
1892 }
1893
1894 if (!state) {
1895 spin_lock(&tree->lock);
1896 state = find_first_extent_bit_state(tree, failrec->start,
1897 EXTENT_LOCKED);
1898 if (state && state->start != failrec->start)
1899 state = NULL;
1900 spin_unlock(&tree->lock);
1901 }
1902
1903 /*
1904 * there are two premises:
1905 * a) deliver good data to the caller
1906 * b) correct the bad sectors on disk
1907 */
1908 if (failed_bio->bi_vcnt > 1) {
1909 /*
1910 * to fulfill b), we need to know the exact failing sectors, as
1911 * we don't want to rewrite any more than the failed ones. thus,
1912 * we need separate read requests for the failed bio
1913 *
1914 * if the following BUG_ON triggers, our validation request got
1915 * merged. we need separate requests for our algorithm to work.
1916 */
1917 BUG_ON(failrec->in_validation);
1918 failrec->in_validation = 1;
1919 failrec->this_mirror = failed_mirror;
1920 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
1921 } else {
1922 /*
1923 * we're ready to fulfill a) and b) alongside. get a good copy
1924 * of the failed sector and if we succeed, we have setup
1925 * everything for repair_io_failure to do the rest for us.
1926 */
1927 if (failrec->in_validation) {
1928 BUG_ON(failrec->this_mirror != failed_mirror);
1929 failrec->in_validation = 0;
1930 failrec->this_mirror = 0;
1931 }
1932 failrec->failed_mirror = failed_mirror;
1933 failrec->this_mirror++;
1934 if (failrec->this_mirror == failed_mirror)
1935 failrec->this_mirror++;
1936 read_mode = READ_SYNC;
1937 }
1938
1939 if (!state || failrec->this_mirror > num_copies) {
1940 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
1941 "next_mirror %d, failed_mirror %d\n", state,
1942 num_copies, failrec->this_mirror, failed_mirror);
1943 free_io_failure(inode, failrec, 0);
1944 return -EIO;
1945 }
1946
1947 bio = bio_alloc(GFP_NOFS, 1);
1948 bio->bi_private = state;
1949 bio->bi_end_io = failed_bio->bi_end_io;
1950 bio->bi_sector = failrec->logical >> 9;
1951 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
1952 bio->bi_size = 0;
1953
1954 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1955
1956 pr_debug("bio_readpage_error: submitting new read[%#x] to "
1957 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
1958 failrec->this_mirror, num_copies, failrec->in_validation);
1959
1960 tree->ops->submit_bio_hook(inode, read_mode, bio, failrec->this_mirror,
1961 failrec->bio_flags, 0);
1962 return 0;
1963}
1964
Chris Masond1310b22008-01-24 16:13:08 -05001965/* lots and lots of room for performance fixes in the end_bio funcs */
1966
1967/*
1968 * after a writepage IO is done, we need to:
1969 * clear the uptodate bits on error
1970 * clear the writeback bits in the extent tree for this IO
1971 * end_page_writeback if the page has no more pending IO
1972 *
1973 * Scheduling is not allowed, so the extent state tree is expected
1974 * to have one and only one object corresponding to this IO.
1975 */
Chris Masond1310b22008-01-24 16:13:08 -05001976static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001977{
Chris Mason1259ab72008-05-12 13:39:03 -04001978 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001979 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001980 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001981 u64 start;
1982 u64 end;
1983 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001984 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001985
Chris Masond1310b22008-01-24 16:13:08 -05001986 do {
1987 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001988 tree = &BTRFS_I(page->mapping->host)->io_tree;
1989
Chris Masond1310b22008-01-24 16:13:08 -05001990 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1991 bvec->bv_offset;
1992 end = start + bvec->bv_len - 1;
1993
1994 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1995 whole_page = 1;
1996 else
1997 whole_page = 0;
1998
1999 if (--bvec >= bio->bi_io_vec)
2000 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002001 if (tree->ops && tree->ops->writepage_end_io_hook) {
2002 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04002003 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04002004 if (ret)
2005 uptodate = 0;
2006 }
2007
2008 if (!uptodate && tree->ops &&
2009 tree->ops->writepage_io_failed_hook) {
2010 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04002011 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04002012 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04002013 uptodate = (err == 0);
2014 continue;
2015 }
2016 }
2017
Chris Masond1310b22008-01-24 16:13:08 -05002018 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002019 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002020 ClearPageUptodate(page);
2021 SetPageError(page);
2022 }
Chris Mason70dec802008-01-29 09:59:12 -05002023
Chris Masond1310b22008-01-24 16:13:08 -05002024 if (whole_page)
2025 end_page_writeback(page);
2026 else
2027 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002028 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002029
Chris Masond1310b22008-01-24 16:13:08 -05002030 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002031}
2032
2033/*
2034 * after a readpage IO is done, we need to:
2035 * clear the uptodate bits on error
2036 * set the uptodate bits if things worked
2037 * set the page up to date if all extents in the tree are uptodate
2038 * clear the lock bit in the extent tree
2039 * unlock the page if there are no other extents locked for it
2040 *
2041 * Scheduling is not allowed, so the extent state tree is expected
2042 * to have one and only one object corresponding to this IO.
2043 */
Chris Masond1310b22008-01-24 16:13:08 -05002044static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002045{
2046 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002047 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2048 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002049 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002050 u64 start;
2051 u64 end;
2052 int whole_page;
2053 int ret;
2054
Chris Masond20f7042008-12-08 16:58:54 -05002055 if (err)
2056 uptodate = 0;
2057
Chris Masond1310b22008-01-24 16:13:08 -05002058 do {
2059 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002060 struct extent_state *cached = NULL;
2061 struct extent_state *state;
2062
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002063 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2064 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2065 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002066 tree = &BTRFS_I(page->mapping->host)->io_tree;
2067
Chris Masond1310b22008-01-24 16:13:08 -05002068 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2069 bvec->bv_offset;
2070 end = start + bvec->bv_len - 1;
2071
2072 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2073 whole_page = 1;
2074 else
2075 whole_page = 0;
2076
Chris Mason4125bf72010-02-03 18:18:45 +00002077 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002078 prefetchw(&bvec->bv_page->flags);
2079
Arne Jansen507903b2011-04-06 10:02:20 +00002080 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002081 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002082 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002083 /*
2084 * take a reference on the state, unlock will drop
2085 * the ref
2086 */
2087 cache_state(state, &cached);
2088 }
2089 spin_unlock(&tree->lock);
2090
Chris Masond1310b22008-01-24 16:13:08 -05002091 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002092 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002093 state);
Chris Masond1310b22008-01-24 16:13:08 -05002094 if (ret)
2095 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096 else
2097 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002098 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002099 if (!uptodate) {
2100 u64 failed_mirror;
2101 failed_mirror = (u64)bio->bi_bdev;
2102 if (tree->ops && tree->ops->readpage_io_failed_hook)
2103 ret = tree->ops->readpage_io_failed_hook(
2104 bio, page, start, end,
2105 failed_mirror, NULL);
2106 else
2107 ret = bio_readpage_error(bio, page, start, end,
2108 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002109 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002110 uptodate =
2111 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002112 if (err)
2113 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002114 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002115 continue;
2116 }
2117 }
Chris Mason70dec802008-01-29 09:59:12 -05002118
Chris Mason771ed682008-11-06 22:02:51 -05002119 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002120 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002121 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002122 }
Arne Jansen507903b2011-04-06 10:02:20 +00002123 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002124
Chris Mason70dec802008-01-29 09:59:12 -05002125 if (whole_page) {
2126 if (uptodate) {
2127 SetPageUptodate(page);
2128 } else {
2129 ClearPageUptodate(page);
2130 SetPageError(page);
2131 }
Chris Masond1310b22008-01-24 16:13:08 -05002132 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002133 } else {
2134 if (uptodate) {
2135 check_page_uptodate(tree, page);
2136 } else {
2137 ClearPageUptodate(page);
2138 SetPageError(page);
2139 }
Chris Masond1310b22008-01-24 16:13:08 -05002140 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002141 }
Chris Mason4125bf72010-02-03 18:18:45 +00002142 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002143
2144 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002145}
2146
Miao Xie88f794e2010-11-22 03:02:55 +00002147struct bio *
2148btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2149 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002150{
2151 struct bio *bio;
2152
2153 bio = bio_alloc(gfp_flags, nr_vecs);
2154
2155 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2156 while (!bio && (nr_vecs /= 2))
2157 bio = bio_alloc(gfp_flags, nr_vecs);
2158 }
2159
2160 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002161 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002162 bio->bi_bdev = bdev;
2163 bio->bi_sector = first_sector;
2164 }
2165 return bio;
2166}
2167
Chris Masonc8b97812008-10-29 14:49:59 -04002168static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2169 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002170{
Chris Masond1310b22008-01-24 16:13:08 -05002171 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002172 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2173 struct page *page = bvec->bv_page;
2174 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002175 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002176
2177 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002178
David Woodhouse902b22f2008-08-20 08:51:49 -04002179 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002180
2181 bio_get(bio);
2182
Chris Mason065631f2008-02-20 12:07:25 -05002183 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002184 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002185 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002186 else
2187 submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002188
Chris Masond1310b22008-01-24 16:13:08 -05002189 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2190 ret = -EOPNOTSUPP;
2191 bio_put(bio);
2192 return ret;
2193}
2194
2195static int submit_extent_page(int rw, struct extent_io_tree *tree,
2196 struct page *page, sector_t sector,
2197 size_t size, unsigned long offset,
2198 struct block_device *bdev,
2199 struct bio **bio_ret,
2200 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002201 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002202 int mirror_num,
2203 unsigned long prev_bio_flags,
2204 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002205{
2206 int ret = 0;
2207 struct bio *bio;
2208 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002209 int contig = 0;
2210 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2211 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002212 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002213
2214 if (bio_ret && *bio_ret) {
2215 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002216 if (old_compressed)
2217 contig = bio->bi_sector == sector;
2218 else
2219 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2220 sector;
2221
2222 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002223 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002224 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2225 bio_flags)) ||
2226 bio_add_page(bio, page, page_size, offset) < page_size) {
2227 ret = submit_one_bio(rw, bio, mirror_num,
2228 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002229 bio = NULL;
2230 } else {
2231 return 0;
2232 }
2233 }
Chris Masonc8b97812008-10-29 14:49:59 -04002234 if (this_compressed)
2235 nr = BIO_MAX_PAGES;
2236 else
2237 nr = bio_get_nr_vecs(bdev);
2238
Miao Xie88f794e2010-11-22 03:02:55 +00002239 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002240 if (!bio)
2241 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002242
Chris Masonc8b97812008-10-29 14:49:59 -04002243 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002244 bio->bi_end_io = end_io_func;
2245 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002246
Chris Masond3977122009-01-05 21:25:51 -05002247 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002248 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002249 else
Chris Masonc8b97812008-10-29 14:49:59 -04002250 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002251
2252 return ret;
2253}
2254
2255void set_page_extent_mapped(struct page *page)
2256{
2257 if (!PagePrivate(page)) {
2258 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002259 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002260 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002261 }
2262}
2263
Christoph Hellwigb2950862008-12-02 09:54:17 -05002264static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002265{
Chris Masoneb14ab82011-02-10 12:35:00 -05002266 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002267 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2268}
2269
2270/*
2271 * basic readpage implementation. Locked extent state structs are inserted
2272 * into the tree that are removed when the IO is done (by the end_io
2273 * handlers)
2274 */
2275static int __extent_read_full_page(struct extent_io_tree *tree,
2276 struct page *page,
2277 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002278 struct bio **bio, int mirror_num,
2279 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002280{
2281 struct inode *inode = page->mapping->host;
2282 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2283 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2284 u64 end;
2285 u64 cur = start;
2286 u64 extent_offset;
2287 u64 last_byte = i_size_read(inode);
2288 u64 block_start;
2289 u64 cur_end;
2290 sector_t sector;
2291 struct extent_map *em;
2292 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002293 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002294 int ret;
2295 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002296 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002297 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002298 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002299 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002300 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002301
2302 set_page_extent_mapped(page);
2303
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002304 if (!PageUptodate(page)) {
2305 if (cleancache_get_page(page) == 0) {
2306 BUG_ON(blocksize != PAGE_SIZE);
2307 goto out;
2308 }
2309 }
2310
Chris Masond1310b22008-01-24 16:13:08 -05002311 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002312 while (1) {
2313 lock_extent(tree, start, end, GFP_NOFS);
2314 ordered = btrfs_lookup_ordered_extent(inode, start);
2315 if (!ordered)
2316 break;
2317 unlock_extent(tree, start, end, GFP_NOFS);
2318 btrfs_start_ordered_extent(inode, ordered, 1);
2319 btrfs_put_ordered_extent(ordered);
2320 }
Chris Masond1310b22008-01-24 16:13:08 -05002321
Chris Masonc8b97812008-10-29 14:49:59 -04002322 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2323 char *userpage;
2324 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2325
2326 if (zero_offset) {
2327 iosize = PAGE_CACHE_SIZE - zero_offset;
2328 userpage = kmap_atomic(page, KM_USER0);
2329 memset(userpage + zero_offset, 0, iosize);
2330 flush_dcache_page(page);
2331 kunmap_atomic(userpage, KM_USER0);
2332 }
2333 }
Chris Masond1310b22008-01-24 16:13:08 -05002334 while (cur <= end) {
2335 if (cur >= last_byte) {
2336 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002337 struct extent_state *cached = NULL;
2338
David Sterba306e16c2011-04-19 14:29:38 +02002339 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002340 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002341 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002342 flush_dcache_page(page);
2343 kunmap_atomic(userpage, KM_USER0);
2344 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002345 &cached, GFP_NOFS);
2346 unlock_extent_cached(tree, cur, cur + iosize - 1,
2347 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002348 break;
2349 }
David Sterba306e16c2011-04-19 14:29:38 +02002350 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002351 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002352 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002353 SetPageError(page);
2354 unlock_extent(tree, cur, end, GFP_NOFS);
2355 break;
2356 }
Chris Masond1310b22008-01-24 16:13:08 -05002357 extent_offset = cur - em->start;
2358 BUG_ON(extent_map_end(em) <= cur);
2359 BUG_ON(end < cur);
2360
Li Zefan261507a02010-12-17 14:21:50 +08002361 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002362 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002363 extent_set_compress_type(&this_bio_flag,
2364 em->compress_type);
2365 }
Chris Masonc8b97812008-10-29 14:49:59 -04002366
Chris Masond1310b22008-01-24 16:13:08 -05002367 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2368 cur_end = min(extent_map_end(em) - 1, end);
2369 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002370 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2371 disk_io_size = em->block_len;
2372 sector = em->block_start >> 9;
2373 } else {
2374 sector = (em->block_start + extent_offset) >> 9;
2375 disk_io_size = iosize;
2376 }
Chris Masond1310b22008-01-24 16:13:08 -05002377 bdev = em->bdev;
2378 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002379 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2380 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002381 free_extent_map(em);
2382 em = NULL;
2383
2384 /* we've found a hole, just zero and go on */
2385 if (block_start == EXTENT_MAP_HOLE) {
2386 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002387 struct extent_state *cached = NULL;
2388
Chris Masond1310b22008-01-24 16:13:08 -05002389 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002390 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002391 flush_dcache_page(page);
2392 kunmap_atomic(userpage, KM_USER0);
2393
2394 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002395 &cached, GFP_NOFS);
2396 unlock_extent_cached(tree, cur, cur + iosize - 1,
2397 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002398 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002399 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002400 continue;
2401 }
2402 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002403 if (test_range_bit(tree, cur, cur_end,
2404 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002405 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002406 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2407 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002408 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002409 continue;
2410 }
Chris Mason70dec802008-01-29 09:59:12 -05002411 /* we have an inline extent but it didn't get marked up
2412 * to date. Error out
2413 */
2414 if (block_start == EXTENT_MAP_INLINE) {
2415 SetPageError(page);
2416 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2417 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002418 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002419 continue;
2420 }
Chris Masond1310b22008-01-24 16:13:08 -05002421
2422 ret = 0;
2423 if (tree->ops && tree->ops->readpage_io_hook) {
2424 ret = tree->ops->readpage_io_hook(page, cur,
2425 cur + iosize - 1);
2426 }
2427 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002428 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2429 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002430 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002431 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002432 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002433 end_bio_extent_readpage, mirror_num,
2434 *bio_flags,
2435 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002436 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002437 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002438 }
2439 if (ret)
2440 SetPageError(page);
2441 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002442 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002443 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002444out:
Chris Masond1310b22008-01-24 16:13:08 -05002445 if (!nr) {
2446 if (!PageError(page))
2447 SetPageUptodate(page);
2448 unlock_page(page);
2449 }
2450 return 0;
2451}
2452
2453int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002454 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002455{
2456 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002457 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002458 int ret;
2459
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002460 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002461 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002462 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002463 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002464 return ret;
2465}
Chris Masond1310b22008-01-24 16:13:08 -05002466
Chris Mason11c83492009-04-20 15:50:09 -04002467static noinline void update_nr_written(struct page *page,
2468 struct writeback_control *wbc,
2469 unsigned long nr_written)
2470{
2471 wbc->nr_to_write -= nr_written;
2472 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2473 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2474 page->mapping->writeback_index = page->index + nr_written;
2475}
2476
Chris Masond1310b22008-01-24 16:13:08 -05002477/*
2478 * the writepage semantics are similar to regular writepage. extent
2479 * records are inserted to lock ranges in the tree, and as dirty areas
2480 * are found, they are marked writeback. Then the lock bits are removed
2481 * and the end_io handler clears the writeback ranges
2482 */
2483static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2484 void *data)
2485{
2486 struct inode *inode = page->mapping->host;
2487 struct extent_page_data *epd = data;
2488 struct extent_io_tree *tree = epd->tree;
2489 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2490 u64 delalloc_start;
2491 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2492 u64 end;
2493 u64 cur = start;
2494 u64 extent_offset;
2495 u64 last_byte = i_size_read(inode);
2496 u64 block_start;
2497 u64 iosize;
2498 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002499 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002500 struct extent_map *em;
2501 struct block_device *bdev;
2502 int ret;
2503 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002504 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002505 size_t blocksize;
2506 loff_t i_size = i_size_read(inode);
2507 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2508 u64 nr_delalloc;
2509 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002510 int page_started;
2511 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002512 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002513 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002514
Chris Masonffbd5172009-04-20 15:50:09 -04002515 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002516 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002517 else
2518 write_flags = WRITE;
2519
liubo1abe9b82011-03-24 11:18:59 +00002520 trace___extent_writepage(page, inode, wbc);
2521
Chris Masond1310b22008-01-24 16:13:08 -05002522 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002523 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002524 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002525 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002526 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002527 unlock_page(page);
2528 return 0;
2529 }
2530
2531 if (page->index == end_index) {
2532 char *userpage;
2533
Chris Masond1310b22008-01-24 16:13:08 -05002534 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002535 memset(userpage + pg_offset, 0,
2536 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002537 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002538 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002539 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002540 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002541
2542 set_page_extent_mapped(page);
2543
2544 delalloc_start = start;
2545 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002546 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002547 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002548 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002549 /*
2550 * make sure the wbc mapping index is at least updated
2551 * to this page.
2552 */
2553 update_nr_written(page, wbc, 0);
2554
Chris Masond3977122009-01-05 21:25:51 -05002555 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002556 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002557 page,
2558 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002559 &delalloc_end,
2560 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002561 if (nr_delalloc == 0) {
2562 delalloc_start = delalloc_end + 1;
2563 continue;
2564 }
2565 tree->ops->fill_delalloc(inode, page, delalloc_start,
2566 delalloc_end, &page_started,
2567 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002568 /*
2569 * delalloc_end is already one less than the total
2570 * length, so we don't subtract one from
2571 * PAGE_CACHE_SIZE
2572 */
2573 delalloc_to_write += (delalloc_end - delalloc_start +
2574 PAGE_CACHE_SIZE) >>
2575 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002576 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002577 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002578 if (wbc->nr_to_write < delalloc_to_write) {
2579 int thresh = 8192;
2580
2581 if (delalloc_to_write < thresh * 2)
2582 thresh = delalloc_to_write;
2583 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2584 thresh);
2585 }
Chris Masonc8b97812008-10-29 14:49:59 -04002586
Chris Mason771ed682008-11-06 22:02:51 -05002587 /* did the fill delalloc function already unlock and start
2588 * the IO?
2589 */
2590 if (page_started) {
2591 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002592 /*
2593 * we've unlocked the page, so we can't update
2594 * the mapping's writeback index, just update
2595 * nr_to_write.
2596 */
2597 wbc->nr_to_write -= nr_written;
2598 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002599 }
Chris Masonc8b97812008-10-29 14:49:59 -04002600 }
Chris Mason247e7432008-07-17 12:53:51 -04002601 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002602 ret = tree->ops->writepage_start_hook(page, start,
2603 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002604 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002605 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002606 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002607 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002608 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002609 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002610 }
2611 }
2612
Chris Mason11c83492009-04-20 15:50:09 -04002613 /*
2614 * we don't want to touch the inode after unlocking the page,
2615 * so we update the mapping writeback index now
2616 */
2617 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002618
Chris Masond1310b22008-01-24 16:13:08 -05002619 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002620 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621 if (tree->ops && tree->ops->writepage_end_io_hook)
2622 tree->ops->writepage_end_io_hook(page, start,
2623 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002624 goto done;
2625 }
2626
Chris Masond1310b22008-01-24 16:13:08 -05002627 blocksize = inode->i_sb->s_blocksize;
2628
2629 while (cur <= end) {
2630 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002631 if (tree->ops && tree->ops->writepage_end_io_hook)
2632 tree->ops->writepage_end_io_hook(page, cur,
2633 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002634 break;
2635 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002636 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002637 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002638 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002639 SetPageError(page);
2640 break;
2641 }
2642
2643 extent_offset = cur - em->start;
2644 BUG_ON(extent_map_end(em) <= cur);
2645 BUG_ON(end < cur);
2646 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2647 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2648 sector = (em->block_start + extent_offset) >> 9;
2649 bdev = em->bdev;
2650 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002651 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002652 free_extent_map(em);
2653 em = NULL;
2654
Chris Masonc8b97812008-10-29 14:49:59 -04002655 /*
2656 * compressed and inline extents are written through other
2657 * paths in the FS
2658 */
2659 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002660 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002661 /*
2662 * end_io notification does not happen here for
2663 * compressed extents
2664 */
2665 if (!compressed && tree->ops &&
2666 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002667 tree->ops->writepage_end_io_hook(page, cur,
2668 cur + iosize - 1,
2669 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002670 else if (compressed) {
2671 /* we don't want to end_page_writeback on
2672 * a compressed extent. this happens
2673 * elsewhere
2674 */
2675 nr++;
2676 }
2677
2678 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002679 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002680 continue;
2681 }
Chris Masond1310b22008-01-24 16:13:08 -05002682 /* leave this out until we have a page_mkwrite call */
2683 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002684 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002685 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002686 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002687 continue;
2688 }
Chris Masonc8b97812008-10-29 14:49:59 -04002689
Chris Masond1310b22008-01-24 16:13:08 -05002690 if (tree->ops && tree->ops->writepage_io_hook) {
2691 ret = tree->ops->writepage_io_hook(page, cur,
2692 cur + iosize - 1);
2693 } else {
2694 ret = 0;
2695 }
Chris Mason1259ab72008-05-12 13:39:03 -04002696 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002697 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002698 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002699 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002700
Chris Masond1310b22008-01-24 16:13:08 -05002701 set_range_writeback(tree, cur, cur + iosize - 1);
2702 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002703 printk(KERN_ERR "btrfs warning page %lu not "
2704 "writeback, cur %llu end %llu\n",
2705 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002706 (unsigned long long)end);
2707 }
2708
Chris Masonffbd5172009-04-20 15:50:09 -04002709 ret = submit_extent_page(write_flags, tree, page,
2710 sector, iosize, pg_offset,
2711 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002712 end_bio_extent_writepage,
2713 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002714 if (ret)
2715 SetPageError(page);
2716 }
2717 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002718 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002719 nr++;
2720 }
2721done:
2722 if (nr == 0) {
2723 /* make sure the mapping tag for page dirty gets cleared */
2724 set_page_writeback(page);
2725 end_page_writeback(page);
2726 }
Chris Masond1310b22008-01-24 16:13:08 -05002727 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002728
Chris Mason11c83492009-04-20 15:50:09 -04002729done_unlocked:
2730
Chris Mason2c64c532009-09-02 15:04:12 -04002731 /* drop our reference on any cached states */
2732 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002733 return 0;
2734}
2735
Chris Masond1310b22008-01-24 16:13:08 -05002736/**
Chris Mason4bef0842008-09-08 11:18:08 -04002737 * 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 -05002738 * @mapping: address space structure to write
2739 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2740 * @writepage: function called for each page
2741 * @data: data passed to writepage function
2742 *
2743 * If a page is already under I/O, write_cache_pages() skips it, even
2744 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2745 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2746 * and msync() need to guarantee that all the data which was dirty at the time
2747 * the call was made get new I/O started against them. If wbc->sync_mode is
2748 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2749 * existing IO to complete.
2750 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002751static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002752 struct address_space *mapping,
2753 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002754 writepage_t writepage, void *data,
2755 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002756{
Chris Masond1310b22008-01-24 16:13:08 -05002757 int ret = 0;
2758 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002759 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002760 struct pagevec pvec;
2761 int nr_pages;
2762 pgoff_t index;
2763 pgoff_t end; /* Inclusive */
2764 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00002765 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05002766
Chris Masond1310b22008-01-24 16:13:08 -05002767 pagevec_init(&pvec, 0);
2768 if (wbc->range_cyclic) {
2769 index = mapping->writeback_index; /* Start from prev offset */
2770 end = -1;
2771 } else {
2772 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2773 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002774 scanned = 1;
2775 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00002776 if (wbc->sync_mode == WB_SYNC_ALL)
2777 tag = PAGECACHE_TAG_TOWRITE;
2778 else
2779 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05002780retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00002781 if (wbc->sync_mode == WB_SYNC_ALL)
2782 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002783 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00002784 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
2785 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002786 unsigned i;
2787
2788 scanned = 1;
2789 for (i = 0; i < nr_pages; i++) {
2790 struct page *page = pvec.pages[i];
2791
2792 /*
2793 * At this point we hold neither mapping->tree_lock nor
2794 * lock on the page itself: the page may be truncated or
2795 * invalidated (changing page->mapping to NULL), or even
2796 * swizzled back from swapper_space to tmpfs file
2797 * mapping
2798 */
Chris Mason4bef0842008-09-08 11:18:08 -04002799 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2800 tree->ops->write_cache_pages_lock_hook(page);
2801 else
2802 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002803
2804 if (unlikely(page->mapping != mapping)) {
2805 unlock_page(page);
2806 continue;
2807 }
2808
2809 if (!wbc->range_cyclic && page->index > end) {
2810 done = 1;
2811 unlock_page(page);
2812 continue;
2813 }
2814
Chris Masond2c3f4f2008-11-19 12:44:22 -05002815 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002816 if (PageWriteback(page))
2817 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002818 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002819 }
Chris Masond1310b22008-01-24 16:13:08 -05002820
2821 if (PageWriteback(page) ||
2822 !clear_page_dirty_for_io(page)) {
2823 unlock_page(page);
2824 continue;
2825 }
2826
2827 ret = (*writepage)(page, wbc, data);
2828
2829 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2830 unlock_page(page);
2831 ret = 0;
2832 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002833 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002834 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002835
2836 /*
2837 * the filesystem may choose to bump up nr_to_write.
2838 * We have to make sure to honor the new nr_to_write
2839 * at any time
2840 */
2841 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002842 }
2843 pagevec_release(&pvec);
2844 cond_resched();
2845 }
2846 if (!scanned && !done) {
2847 /*
2848 * We hit the last page and there is more work to be done: wrap
2849 * back to the start of the file
2850 */
2851 scanned = 1;
2852 index = 0;
2853 goto retry;
2854 }
Chris Masond1310b22008-01-24 16:13:08 -05002855 return ret;
2856}
Chris Masond1310b22008-01-24 16:13:08 -05002857
Chris Masonffbd5172009-04-20 15:50:09 -04002858static void flush_epd_write_bio(struct extent_page_data *epd)
2859{
2860 if (epd->bio) {
2861 if (epd->sync_io)
2862 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2863 else
2864 submit_one_bio(WRITE, epd->bio, 0, 0);
2865 epd->bio = NULL;
2866 }
2867}
2868
Chris Masond2c3f4f2008-11-19 12:44:22 -05002869static noinline void flush_write_bio(void *data)
2870{
2871 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002872 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002873}
2874
Chris Masond1310b22008-01-24 16:13:08 -05002875int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2876 get_extent_t *get_extent,
2877 struct writeback_control *wbc)
2878{
2879 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05002880 struct extent_page_data epd = {
2881 .bio = NULL,
2882 .tree = tree,
2883 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002884 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002885 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002886 };
Chris Masond1310b22008-01-24 16:13:08 -05002887
Chris Masond1310b22008-01-24 16:13:08 -05002888 ret = __extent_writepage(page, wbc, &epd);
2889
Chris Masonffbd5172009-04-20 15:50:09 -04002890 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002891 return ret;
2892}
Chris Masond1310b22008-01-24 16:13:08 -05002893
Chris Mason771ed682008-11-06 22:02:51 -05002894int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2895 u64 start, u64 end, get_extent_t *get_extent,
2896 int mode)
2897{
2898 int ret = 0;
2899 struct address_space *mapping = inode->i_mapping;
2900 struct page *page;
2901 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2902 PAGE_CACHE_SHIFT;
2903
2904 struct extent_page_data epd = {
2905 .bio = NULL,
2906 .tree = tree,
2907 .get_extent = get_extent,
2908 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002909 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002910 };
2911 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05002912 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05002913 .nr_to_write = nr_pages * 2,
2914 .range_start = start,
2915 .range_end = end + 1,
2916 };
2917
Chris Masond3977122009-01-05 21:25:51 -05002918 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002919 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2920 if (clear_page_dirty_for_io(page))
2921 ret = __extent_writepage(page, &wbc_writepages, &epd);
2922 else {
2923 if (tree->ops && tree->ops->writepage_end_io_hook)
2924 tree->ops->writepage_end_io_hook(page, start,
2925 start + PAGE_CACHE_SIZE - 1,
2926 NULL, 1);
2927 unlock_page(page);
2928 }
2929 page_cache_release(page);
2930 start += PAGE_CACHE_SIZE;
2931 }
2932
Chris Masonffbd5172009-04-20 15:50:09 -04002933 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002934 return ret;
2935}
Chris Masond1310b22008-01-24 16:13:08 -05002936
2937int extent_writepages(struct extent_io_tree *tree,
2938 struct address_space *mapping,
2939 get_extent_t *get_extent,
2940 struct writeback_control *wbc)
2941{
2942 int ret = 0;
2943 struct extent_page_data epd = {
2944 .bio = NULL,
2945 .tree = tree,
2946 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002947 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002948 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002949 };
2950
Chris Mason4bef0842008-09-08 11:18:08 -04002951 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002952 __extent_writepage, &epd,
2953 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002954 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002955 return ret;
2956}
Chris Masond1310b22008-01-24 16:13:08 -05002957
2958int extent_readpages(struct extent_io_tree *tree,
2959 struct address_space *mapping,
2960 struct list_head *pages, unsigned nr_pages,
2961 get_extent_t get_extent)
2962{
2963 struct bio *bio = NULL;
2964 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002965 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002966
Chris Masond1310b22008-01-24 16:13:08 -05002967 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2968 struct page *page = list_entry(pages->prev, struct page, lru);
2969
2970 prefetchw(&page->flags);
2971 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00002972 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04002973 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04002974 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002975 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002976 }
2977 page_cache_release(page);
2978 }
Chris Masond1310b22008-01-24 16:13:08 -05002979 BUG_ON(!list_empty(pages));
2980 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002981 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002982 return 0;
2983}
Chris Masond1310b22008-01-24 16:13:08 -05002984
2985/*
2986 * basic invalidatepage code, this waits on any locked or writeback
2987 * ranges corresponding to the page, and then deletes any extent state
2988 * records from the tree
2989 */
2990int extent_invalidatepage(struct extent_io_tree *tree,
2991 struct page *page, unsigned long offset)
2992{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002993 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002994 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2995 u64 end = start + PAGE_CACHE_SIZE - 1;
2996 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2997
Chris Masond3977122009-01-05 21:25:51 -05002998 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002999 if (start > end)
3000 return 0;
3001
Josef Bacik2ac55d42010-02-03 19:33:23 +00003002 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003003 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003004 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003005 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3006 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003007 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003008 return 0;
3009}
Chris Masond1310b22008-01-24 16:13:08 -05003010
3011/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003012 * a helper for releasepage, this tests for areas of the page that
3013 * are locked or under IO and drops the related state bits if it is safe
3014 * to drop the page.
3015 */
3016int try_release_extent_state(struct extent_map_tree *map,
3017 struct extent_io_tree *tree, struct page *page,
3018 gfp_t mask)
3019{
3020 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3021 u64 end = start + PAGE_CACHE_SIZE - 1;
3022 int ret = 1;
3023
Chris Mason211f90e2008-07-18 11:56:15 -04003024 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003025 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003026 ret = 0;
3027 else {
3028 if ((mask & GFP_NOFS) == GFP_NOFS)
3029 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003030 /*
3031 * at this point we can safely clear everything except the
3032 * locked bit and the nodatasum bit
3033 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003034 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003035 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3036 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003037
3038 /* if clear_extent_bit failed for enomem reasons,
3039 * we can't allow the release to continue.
3040 */
3041 if (ret < 0)
3042 ret = 0;
3043 else
3044 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003045 }
3046 return ret;
3047}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003048
3049/*
Chris Masond1310b22008-01-24 16:13:08 -05003050 * a helper for releasepage. As long as there are no locked extents
3051 * in the range corresponding to the page, both state records and extent
3052 * map records are removed
3053 */
3054int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003055 struct extent_io_tree *tree, struct page *page,
3056 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003057{
3058 struct extent_map *em;
3059 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3060 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003061
Chris Mason70dec802008-01-29 09:59:12 -05003062 if ((mask & __GFP_WAIT) &&
3063 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003064 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003065 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003066 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003067 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003068 em = lookup_extent_mapping(map, start, len);
David Sterbac7040052011-04-19 18:00:01 +02003069 if (IS_ERR_OR_NULL(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04003070 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003071 break;
3072 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003073 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3074 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003075 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003076 free_extent_map(em);
3077 break;
3078 }
3079 if (!test_range_bit(tree, em->start,
3080 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003081 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003082 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003083 remove_extent_mapping(map, em);
3084 /* once for the rb tree */
3085 free_extent_map(em);
3086 }
3087 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003088 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003089
3090 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003091 free_extent_map(em);
3092 }
Chris Masond1310b22008-01-24 16:13:08 -05003093 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003094 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003095}
Chris Masond1310b22008-01-24 16:13:08 -05003096
Chris Masonec29ed52011-02-23 16:23:20 -05003097/*
3098 * helper function for fiemap, which doesn't want to see any holes.
3099 * This maps until we find something past 'last'
3100 */
3101static struct extent_map *get_extent_skip_holes(struct inode *inode,
3102 u64 offset,
3103 u64 last,
3104 get_extent_t *get_extent)
3105{
3106 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3107 struct extent_map *em;
3108 u64 len;
3109
3110 if (offset >= last)
3111 return NULL;
3112
3113 while(1) {
3114 len = last - offset;
3115 if (len == 0)
3116 break;
3117 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3118 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003119 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003120 return em;
3121
3122 /* if this isn't a hole return it */
3123 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3124 em->block_start != EXTENT_MAP_HOLE) {
3125 return em;
3126 }
3127
3128 /* this is a hole, advance to the next extent */
3129 offset = extent_map_end(em);
3130 free_extent_map(em);
3131 if (offset >= last)
3132 break;
3133 }
3134 return NULL;
3135}
3136
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003137int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3138 __u64 start, __u64 len, get_extent_t *get_extent)
3139{
Josef Bacik975f84f2010-11-23 19:36:57 +00003140 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003141 u64 off = start;
3142 u64 max = start + len;
3143 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003144 u32 found_type;
3145 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003146 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003147 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003148 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003149 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003150 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003151 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003152 struct btrfs_path *path;
3153 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003154 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003155 u64 em_start = 0;
3156 u64 em_len = 0;
3157 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003158 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003159
3160 if (len == 0)
3161 return -EINVAL;
3162
Josef Bacik975f84f2010-11-23 19:36:57 +00003163 path = btrfs_alloc_path();
3164 if (!path)
3165 return -ENOMEM;
3166 path->leave_spinning = 1;
3167
Chris Masonec29ed52011-02-23 16:23:20 -05003168 /*
3169 * lookup the last file extent. We're not using i_size here
3170 * because there might be preallocation past i_size
3171 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003172 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003173 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003174 if (ret < 0) {
3175 btrfs_free_path(path);
3176 return ret;
3177 }
3178 WARN_ON(!ret);
3179 path->slots[0]--;
3180 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3181 struct btrfs_file_extent_item);
3182 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3183 found_type = btrfs_key_type(&found_key);
3184
Chris Masonec29ed52011-02-23 16:23:20 -05003185 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003186 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003187 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003188 /* have to trust i_size as the end */
3189 last = (u64)-1;
3190 last_for_get_extent = isize;
3191 } else {
3192 /*
3193 * remember the start of the last extent. There are a
3194 * bunch of different factors that go into the length of the
3195 * extent, so its much less complex to remember where it started
3196 */
3197 last = found_key.offset;
3198 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003199 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003200 btrfs_free_path(path);
3201
Chris Masonec29ed52011-02-23 16:23:20 -05003202 /*
3203 * we might have some extents allocated but more delalloc past those
3204 * extents. so, we trust isize unless the start of the last extent is
3205 * beyond isize
3206 */
3207 if (last < isize) {
3208 last = (u64)-1;
3209 last_for_get_extent = isize;
3210 }
3211
Josef Bacik2ac55d42010-02-03 19:33:23 +00003212 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3213 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003214
3215 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3216 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003217 if (!em)
3218 goto out;
3219 if (IS_ERR(em)) {
3220 ret = PTR_ERR(em);
3221 goto out;
3222 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003223
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003224 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003225 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003226
Chris Masonea8efc72011-03-08 11:54:40 -05003227 /* break if the extent we found is outside the range */
3228 if (em->start >= max || extent_map_end(em) < off)
3229 break;
3230
3231 /*
3232 * get_extent may return an extent that starts before our
3233 * requested range. We have to make sure the ranges
3234 * we return to fiemap always move forward and don't
3235 * overlap, so adjust the offsets here
3236 */
3237 em_start = max(em->start, off);
3238
3239 /*
3240 * record the offset from the start of the extent
3241 * for adjusting the disk offset below
3242 */
3243 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003244 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003245 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003246 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003247 disko = 0;
3248 flags = 0;
3249
Chris Masonea8efc72011-03-08 11:54:40 -05003250 /*
3251 * bump off for our next call to get_extent
3252 */
3253 off = extent_map_end(em);
3254 if (off >= max)
3255 end = 1;
3256
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003257 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003258 end = 1;
3259 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003260 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003261 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3262 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003263 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003264 flags |= (FIEMAP_EXTENT_DELALLOC |
3265 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003266 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003267 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003268 }
3269 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3270 flags |= FIEMAP_EXTENT_ENCODED;
3271
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003272 free_extent_map(em);
3273 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003274 if ((em_start >= last) || em_len == (u64)-1 ||
3275 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003276 flags |= FIEMAP_EXTENT_LAST;
3277 end = 1;
3278 }
3279
Chris Masonec29ed52011-02-23 16:23:20 -05003280 /* now scan forward to see if this is really the last extent. */
3281 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3282 get_extent);
3283 if (IS_ERR(em)) {
3284 ret = PTR_ERR(em);
3285 goto out;
3286 }
3287 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003288 flags |= FIEMAP_EXTENT_LAST;
3289 end = 1;
3290 }
Chris Masonec29ed52011-02-23 16:23:20 -05003291 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3292 em_len, flags);
3293 if (ret)
3294 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003295 }
3296out_free:
3297 free_extent_map(em);
3298out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003299 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3300 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003301 return ret;
3302}
3303
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003304inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003305 unsigned long i)
3306{
3307 struct page *p;
3308 struct address_space *mapping;
3309
3310 if (i == 0)
3311 return eb->first_page;
3312 i += eb->start >> PAGE_CACHE_SHIFT;
3313 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003314 if (!mapping)
3315 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003316
3317 /*
3318 * extent_buffer_page is only called after pinning the page
3319 * by increasing the reference count. So we know the page must
3320 * be in the radix tree.
3321 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003322 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003323 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003324 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003325
Chris Masond1310b22008-01-24 16:13:08 -05003326 return p;
3327}
3328
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003329inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003330{
Chris Mason6af118c2008-07-22 11:18:07 -04003331 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3332 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003333}
3334
Chris Masond1310b22008-01-24 16:13:08 -05003335static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3336 u64 start,
3337 unsigned long len,
3338 gfp_t mask)
3339{
3340 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003341#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003342 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003343#endif
Chris Masond1310b22008-01-24 16:13:08 -05003344
Chris Masond1310b22008-01-24 16:13:08 -05003345 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003346 if (eb == NULL)
3347 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003348 eb->start = start;
3349 eb->len = len;
Chris Masonbd681512011-07-16 15:23:14 -04003350 rwlock_init(&eb->lock);
3351 atomic_set(&eb->write_locks, 0);
3352 atomic_set(&eb->read_locks, 0);
3353 atomic_set(&eb->blocking_readers, 0);
3354 atomic_set(&eb->blocking_writers, 0);
3355 atomic_set(&eb->spinning_readers, 0);
3356 atomic_set(&eb->spinning_writers, 0);
3357 init_waitqueue_head(&eb->write_lock_wq);
3358 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003359
Chris Mason39351272009-02-04 09:24:05 -05003360#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003361 spin_lock_irqsave(&leak_lock, flags);
3362 list_add(&eb->leak_list, &buffers);
3363 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003364#endif
Chris Masond1310b22008-01-24 16:13:08 -05003365 atomic_set(&eb->refs, 1);
3366
3367 return eb;
3368}
3369
3370static void __free_extent_buffer(struct extent_buffer *eb)
3371{
Chris Mason39351272009-02-04 09:24:05 -05003372#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003373 unsigned long flags;
3374 spin_lock_irqsave(&leak_lock, flags);
3375 list_del(&eb->leak_list);
3376 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003377#endif
Chris Masond1310b22008-01-24 16:13:08 -05003378 kmem_cache_free(extent_buffer_cache, eb);
3379}
3380
Miao Xie897ca6e2010-10-26 20:57:29 -04003381/*
3382 * Helper for releasing extent buffer page.
3383 */
3384static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3385 unsigned long start_idx)
3386{
3387 unsigned long index;
3388 struct page *page;
3389
3390 if (!eb->first_page)
3391 return;
3392
3393 index = num_extent_pages(eb->start, eb->len);
3394 if (start_idx >= index)
3395 return;
3396
3397 do {
3398 index--;
3399 page = extent_buffer_page(eb, index);
3400 if (page)
3401 page_cache_release(page);
3402 } while (index != start_idx);
3403}
3404
3405/*
3406 * Helper for releasing the extent buffer.
3407 */
3408static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3409{
3410 btrfs_release_extent_buffer_page(eb, 0);
3411 __free_extent_buffer(eb);
3412}
3413
Chris Masond1310b22008-01-24 16:13:08 -05003414struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3415 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003416 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003417{
3418 unsigned long num_pages = num_extent_pages(start, len);
3419 unsigned long i;
3420 unsigned long index = start >> PAGE_CACHE_SHIFT;
3421 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003422 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003423 struct page *p;
3424 struct address_space *mapping = tree->mapping;
3425 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003426 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003427
Miao Xie19fe0a82010-10-26 20:57:29 -04003428 rcu_read_lock();
3429 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3430 if (eb && atomic_inc_not_zero(&eb->refs)) {
3431 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003432 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003433 return eb;
3434 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003435 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04003436
David Sterbaba144192011-04-21 01:12:06 +02003437 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003438 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003439 return NULL;
3440
Chris Masond1310b22008-01-24 16:13:08 -05003441 if (page0) {
3442 eb->first_page = page0;
3443 i = 1;
3444 index++;
3445 page_cache_get(page0);
3446 mark_page_accessed(page0);
3447 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003448 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003449 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003450 } else {
3451 i = 0;
3452 }
3453 for (; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003454 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003455 if (!p) {
3456 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003457 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003458 }
3459 set_page_extent_mapped(p);
3460 mark_page_accessed(p);
3461 if (i == 0) {
3462 eb->first_page = p;
3463 set_page_extent_head(p, len);
3464 } else {
3465 set_page_private(p, EXTENT_PAGE_PRIVATE);
3466 }
3467 if (!PageUptodate(p))
3468 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003469
3470 /*
3471 * see below about how we avoid a nasty race with release page
3472 * and why we unlock later
3473 */
3474 if (i != 0)
3475 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003476 }
3477 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003478 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003479
Miao Xie19fe0a82010-10-26 20:57:29 -04003480 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3481 if (ret)
3482 goto free_eb;
3483
Chris Mason6af118c2008-07-22 11:18:07 -04003484 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003485 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3486 if (ret == -EEXIST) {
3487 exists = radix_tree_lookup(&tree->buffer,
3488 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003489 /* add one reference for the caller */
3490 atomic_inc(&exists->refs);
3491 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003492 radix_tree_preload_end();
Chris Mason6af118c2008-07-22 11:18:07 -04003493 goto free_eb;
3494 }
Chris Mason6af118c2008-07-22 11:18:07 -04003495 /* add one reference for the tree */
3496 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003497 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003498 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003499
3500 /*
3501 * there is a race where release page may have
3502 * tried to find this extent buffer in the radix
3503 * but failed. It will tell the VM it is safe to
3504 * reclaim the, and it will clear the page private bit.
3505 * We must make sure to set the page private bit properly
3506 * after the extent buffer is in the radix tree so
3507 * it doesn't get lost
3508 */
3509 set_page_extent_mapped(eb->first_page);
3510 set_page_extent_head(eb->first_page, eb->len);
3511 if (!page0)
3512 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003513 return eb;
3514
Chris Mason6af118c2008-07-22 11:18:07 -04003515free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003516 if (eb->first_page && !page0)
3517 unlock_page(eb->first_page);
3518
Chris Masond1310b22008-01-24 16:13:08 -05003519 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003520 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003521 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003522 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003523}
Chris Masond1310b22008-01-24 16:13:08 -05003524
3525struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003526 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003527{
Chris Masond1310b22008-01-24 16:13:08 -05003528 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003529
Miao Xie19fe0a82010-10-26 20:57:29 -04003530 rcu_read_lock();
3531 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3532 if (eb && atomic_inc_not_zero(&eb->refs)) {
3533 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003534 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003535 return eb;
3536 }
3537 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003538
Miao Xie19fe0a82010-10-26 20:57:29 -04003539 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003540}
Chris Masond1310b22008-01-24 16:13:08 -05003541
3542void free_extent_buffer(struct extent_buffer *eb)
3543{
Chris Masond1310b22008-01-24 16:13:08 -05003544 if (!eb)
3545 return;
3546
3547 if (!atomic_dec_and_test(&eb->refs))
3548 return;
3549
Chris Mason6af118c2008-07-22 11:18:07 -04003550 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003551}
Chris Masond1310b22008-01-24 16:13:08 -05003552
3553int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3554 struct extent_buffer *eb)
3555{
Chris Masond1310b22008-01-24 16:13:08 -05003556 unsigned long i;
3557 unsigned long num_pages;
3558 struct page *page;
3559
Chris Masond1310b22008-01-24 16:13:08 -05003560 num_pages = num_extent_pages(eb->start, eb->len);
3561
3562 for (i = 0; i < num_pages; i++) {
3563 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003564 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003565 continue;
3566
Chris Masona61e6f22008-07-22 11:18:08 -04003567 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003568 WARN_ON(!PagePrivate(page));
3569
3570 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003571 if (i == 0)
3572 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003573
Chris Masond1310b22008-01-24 16:13:08 -05003574 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003575 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003576 if (!PageDirty(page)) {
3577 radix_tree_tag_clear(&page->mapping->page_tree,
3578 page_index(page),
3579 PAGECACHE_TAG_DIRTY);
3580 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003581 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003582 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003583 }
3584 return 0;
3585}
Chris Masond1310b22008-01-24 16:13:08 -05003586
Chris Masond1310b22008-01-24 16:13:08 -05003587int set_extent_buffer_dirty(struct extent_io_tree *tree,
3588 struct extent_buffer *eb)
3589{
3590 unsigned long i;
3591 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003592 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003593
Chris Masonb9473432009-03-13 11:00:37 -04003594 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003595 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003596 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003597 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003598 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003599}
Chris Masond1310b22008-01-24 16:13:08 -05003600
Chris Mason19b6caf2011-07-25 06:50:50 -04003601static int __eb_straddles_pages(u64 start, u64 len)
3602{
3603 if (len < PAGE_CACHE_SIZE)
3604 return 1;
3605 if (start & (PAGE_CACHE_SIZE - 1))
3606 return 1;
3607 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3608 return 1;
3609 return 0;
3610}
3611
3612static int eb_straddles_pages(struct extent_buffer *eb)
3613{
3614 return __eb_straddles_pages(eb->start, eb->len);
3615}
3616
Chris Mason1259ab72008-05-12 13:39:03 -04003617int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003618 struct extent_buffer *eb,
3619 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003620{
3621 unsigned long i;
3622 struct page *page;
3623 unsigned long num_pages;
3624
3625 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003626 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003627
Chris Mason19b6caf2011-07-25 06:50:50 -04003628 if (eb_straddles_pages(eb)) {
3629 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3630 cached_state, GFP_NOFS);
3631 }
Chris Mason1259ab72008-05-12 13:39:03 -04003632 for (i = 0; i < num_pages; i++) {
3633 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003634 if (page)
3635 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003636 }
3637 return 0;
3638}
3639
Chris Masond1310b22008-01-24 16:13:08 -05003640int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3641 struct extent_buffer *eb)
3642{
3643 unsigned long i;
3644 struct page *page;
3645 unsigned long num_pages;
3646
3647 num_pages = num_extent_pages(eb->start, eb->len);
3648
Chris Mason19b6caf2011-07-25 06:50:50 -04003649 if (eb_straddles_pages(eb)) {
3650 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3651 NULL, GFP_NOFS);
3652 }
Chris Masond1310b22008-01-24 16:13:08 -05003653 for (i = 0; i < num_pages; i++) {
3654 page = extent_buffer_page(eb, i);
3655 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3656 ((i == num_pages - 1) &&
3657 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3658 check_page_uptodate(tree, page);
3659 continue;
3660 }
3661 SetPageUptodate(page);
3662 }
3663 return 0;
3664}
Chris Masond1310b22008-01-24 16:13:08 -05003665
Chris Masonce9adaa2008-04-09 16:28:12 -04003666int extent_range_uptodate(struct extent_io_tree *tree,
3667 u64 start, u64 end)
3668{
3669 struct page *page;
3670 int ret;
3671 int pg_uptodate = 1;
3672 int uptodate;
3673 unsigned long index;
3674
Chris Mason19b6caf2011-07-25 06:50:50 -04003675 if (__eb_straddles_pages(start, end - start + 1)) {
3676 ret = test_range_bit(tree, start, end,
3677 EXTENT_UPTODATE, 1, NULL);
3678 if (ret)
3679 return 1;
3680 }
Chris Masond3977122009-01-05 21:25:51 -05003681 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003682 index = start >> PAGE_CACHE_SHIFT;
3683 page = find_get_page(tree->mapping, index);
3684 uptodate = PageUptodate(page);
3685 page_cache_release(page);
3686 if (!uptodate) {
3687 pg_uptodate = 0;
3688 break;
3689 }
3690 start += PAGE_CACHE_SIZE;
3691 }
3692 return pg_uptodate;
3693}
3694
Chris Masond1310b22008-01-24 16:13:08 -05003695int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003696 struct extent_buffer *eb,
3697 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003698{
Chris Mason728131d2008-04-09 16:28:12 -04003699 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003700 unsigned long num_pages;
3701 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003702 struct page *page;
3703 int pg_uptodate = 1;
3704
Chris Masonb4ce94d2009-02-04 09:25:08 -05003705 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003706 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003707
Chris Mason19b6caf2011-07-25 06:50:50 -04003708 if (eb_straddles_pages(eb)) {
3709 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3710 EXTENT_UPTODATE, 1, cached_state);
3711 if (ret)
3712 return ret;
3713 }
Chris Mason728131d2008-04-09 16:28:12 -04003714
3715 num_pages = num_extent_pages(eb->start, eb->len);
3716 for (i = 0; i < num_pages; i++) {
3717 page = extent_buffer_page(eb, i);
3718 if (!PageUptodate(page)) {
3719 pg_uptodate = 0;
3720 break;
3721 }
3722 }
Chris Mason42352982008-04-28 16:40:52 -04003723 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003724}
Chris Masond1310b22008-01-24 16:13:08 -05003725
3726int read_extent_buffer_pages(struct extent_io_tree *tree,
3727 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003728 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003729 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003730{
3731 unsigned long i;
3732 unsigned long start_i;
3733 struct page *page;
3734 int err;
3735 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003736 int locked_pages = 0;
3737 int all_uptodate = 1;
3738 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003739 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003740 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003741 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003742
Chris Masonb4ce94d2009-02-04 09:25:08 -05003743 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003744 return 0;
3745
Chris Mason19b6caf2011-07-25 06:50:50 -04003746 if (eb_straddles_pages(eb)) {
3747 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3748 EXTENT_UPTODATE, 1, NULL)) {
3749 return 0;
3750 }
Chris Masond1310b22008-01-24 16:13:08 -05003751 }
3752
3753 if (start) {
3754 WARN_ON(start < eb->start);
3755 start_i = (start >> PAGE_CACHE_SHIFT) -
3756 (eb->start >> PAGE_CACHE_SHIFT);
3757 } else {
3758 start_i = 0;
3759 }
3760
3761 num_pages = num_extent_pages(eb->start, eb->len);
3762 for (i = start_i; i < num_pages; i++) {
3763 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003764 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003765 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003766 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003767 } else {
3768 lock_page(page);
3769 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003770 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003771 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003772 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003773 }
3774 if (all_uptodate) {
3775 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003776 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003777 goto unlock_exit;
3778 }
3779
3780 for (i = start_i; i < num_pages; i++) {
3781 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05003782
3783 WARN_ON(!PagePrivate(page));
3784
3785 set_page_extent_mapped(page);
3786 if (i == 0)
3787 set_page_extent_head(page, eb->len);
3788
Chris Masonce9adaa2008-04-09 16:28:12 -04003789 if (inc_all_pages)
3790 page_cache_get(page);
3791 if (!PageUptodate(page)) {
3792 if (start_i == 0)
3793 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003794 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003795 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003796 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003797 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003798 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003799 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003800 } else {
3801 unlock_page(page);
3802 }
3803 }
3804
Chris Masona86c12c2008-02-07 10:50:54 -05003805 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003806 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003807
Chris Masond3977122009-01-05 21:25:51 -05003808 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003809 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003810
Chris Masond1310b22008-01-24 16:13:08 -05003811 for (i = start_i; i < num_pages; i++) {
3812 page = extent_buffer_page(eb, i);
3813 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003814 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003815 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003816 }
Chris Masond3977122009-01-05 21:25:51 -05003817
Chris Masond1310b22008-01-24 16:13:08 -05003818 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003819 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003820 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003821
3822unlock_exit:
3823 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003824 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003825 page = extent_buffer_page(eb, i);
3826 i++;
3827 unlock_page(page);
3828 locked_pages--;
3829 }
3830 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003831}
Chris Masond1310b22008-01-24 16:13:08 -05003832
3833void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3834 unsigned long start,
3835 unsigned long len)
3836{
3837 size_t cur;
3838 size_t offset;
3839 struct page *page;
3840 char *kaddr;
3841 char *dst = (char *)dstv;
3842 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3843 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003844
3845 WARN_ON(start > eb->len);
3846 WARN_ON(start + len > eb->start + eb->len);
3847
3848 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3849
Chris Masond3977122009-01-05 21:25:51 -05003850 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003851 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003852
3853 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04003854 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05003855 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05003856
3857 dst += cur;
3858 len -= cur;
3859 offset = 0;
3860 i++;
3861 }
3862}
Chris Masond1310b22008-01-24 16:13:08 -05003863
3864int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04003865 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05003866 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04003867 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05003868{
3869 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3870 char *kaddr;
3871 struct page *p;
3872 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3873 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3874 unsigned long end_i = (start_offset + start + min_len - 1) >>
3875 PAGE_CACHE_SHIFT;
3876
3877 if (i != end_i)
3878 return -EINVAL;
3879
3880 if (i == 0) {
3881 offset = start_offset;
3882 *map_start = 0;
3883 } else {
3884 offset = 0;
3885 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3886 }
Chris Masond3977122009-01-05 21:25:51 -05003887
Chris Masond1310b22008-01-24 16:13:08 -05003888 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003889 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3890 "wanted %lu %lu\n", (unsigned long long)eb->start,
3891 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003892 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04003893 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05003894 }
3895
3896 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04003897 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05003898 *map = kaddr + offset;
3899 *map_len = PAGE_CACHE_SIZE - offset;
3900 return 0;
3901}
Chris Masond1310b22008-01-24 16:13:08 -05003902
Chris Masond1310b22008-01-24 16:13:08 -05003903int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3904 unsigned long start,
3905 unsigned long len)
3906{
3907 size_t cur;
3908 size_t offset;
3909 struct page *page;
3910 char *kaddr;
3911 char *ptr = (char *)ptrv;
3912 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3913 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3914 int ret = 0;
3915
3916 WARN_ON(start > eb->len);
3917 WARN_ON(start + len > eb->start + eb->len);
3918
3919 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3920
Chris Masond3977122009-01-05 21:25:51 -05003921 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003922 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003923
3924 cur = min(len, (PAGE_CACHE_SIZE - offset));
3925
Chris Masona6591712011-07-19 12:04:14 -04003926 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05003927 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05003928 if (ret)
3929 break;
3930
3931 ptr += cur;
3932 len -= cur;
3933 offset = 0;
3934 i++;
3935 }
3936 return ret;
3937}
Chris Masond1310b22008-01-24 16:13:08 -05003938
3939void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3940 unsigned long start, unsigned long len)
3941{
3942 size_t cur;
3943 size_t offset;
3944 struct page *page;
3945 char *kaddr;
3946 char *src = (char *)srcv;
3947 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3948 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3949
3950 WARN_ON(start > eb->len);
3951 WARN_ON(start + len > eb->start + eb->len);
3952
3953 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3954
Chris Masond3977122009-01-05 21:25:51 -05003955 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003956 page = extent_buffer_page(eb, i);
3957 WARN_ON(!PageUptodate(page));
3958
3959 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04003960 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05003961 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05003962
3963 src += cur;
3964 len -= cur;
3965 offset = 0;
3966 i++;
3967 }
3968}
Chris Masond1310b22008-01-24 16:13:08 -05003969
3970void memset_extent_buffer(struct extent_buffer *eb, char c,
3971 unsigned long start, unsigned long len)
3972{
3973 size_t cur;
3974 size_t offset;
3975 struct page *page;
3976 char *kaddr;
3977 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3978 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3979
3980 WARN_ON(start > eb->len);
3981 WARN_ON(start + len > eb->start + eb->len);
3982
3983 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3984
Chris Masond3977122009-01-05 21:25:51 -05003985 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003986 page = extent_buffer_page(eb, i);
3987 WARN_ON(!PageUptodate(page));
3988
3989 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04003990 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05003991 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05003992
3993 len -= cur;
3994 offset = 0;
3995 i++;
3996 }
3997}
Chris Masond1310b22008-01-24 16:13:08 -05003998
3999void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4000 unsigned long dst_offset, unsigned long src_offset,
4001 unsigned long len)
4002{
4003 u64 dst_len = dst->len;
4004 size_t cur;
4005 size_t offset;
4006 struct page *page;
4007 char *kaddr;
4008 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4009 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4010
4011 WARN_ON(src->len != dst_len);
4012
4013 offset = (start_offset + dst_offset) &
4014 ((unsigned long)PAGE_CACHE_SIZE - 1);
4015
Chris Masond3977122009-01-05 21:25:51 -05004016 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004017 page = extent_buffer_page(dst, i);
4018 WARN_ON(!PageUptodate(page));
4019
4020 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4021
Chris Masona6591712011-07-19 12:04:14 -04004022 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004023 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004024
4025 src_offset += cur;
4026 len -= cur;
4027 offset = 0;
4028 i++;
4029 }
4030}
Chris Masond1310b22008-01-24 16:13:08 -05004031
4032static void move_pages(struct page *dst_page, struct page *src_page,
4033 unsigned long dst_off, unsigned long src_off,
4034 unsigned long len)
4035{
Chris Masona6591712011-07-19 12:04:14 -04004036 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004037 if (dst_page == src_page) {
4038 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4039 } else {
Chris Masona6591712011-07-19 12:04:14 -04004040 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004041 char *p = dst_kaddr + dst_off + len;
4042 char *s = src_kaddr + src_off + len;
4043
4044 while (len--)
4045 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004046 }
Chris Masond1310b22008-01-24 16:13:08 -05004047}
4048
Sergei Trofimovich33872062011-04-11 21:52:52 +00004049static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4050{
4051 unsigned long distance = (src > dst) ? src - dst : dst - src;
4052 return distance < len;
4053}
4054
Chris Masond1310b22008-01-24 16:13:08 -05004055static void copy_pages(struct page *dst_page, struct page *src_page,
4056 unsigned long dst_off, unsigned long src_off,
4057 unsigned long len)
4058{
Chris Masona6591712011-07-19 12:04:14 -04004059 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004060 char *src_kaddr;
4061
Sergei Trofimovich33872062011-04-11 21:52:52 +00004062 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004063 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004064 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004065 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004066 BUG_ON(areas_overlap(src_off, dst_off, len));
4067 }
Chris Masond1310b22008-01-24 16:13:08 -05004068
4069 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004070}
4071
4072void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4073 unsigned long src_offset, unsigned long len)
4074{
4075 size_t cur;
4076 size_t dst_off_in_page;
4077 size_t src_off_in_page;
4078 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4079 unsigned long dst_i;
4080 unsigned long src_i;
4081
4082 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004083 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4084 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004085 BUG_ON(1);
4086 }
4087 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004088 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4089 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004090 BUG_ON(1);
4091 }
4092
Chris Masond3977122009-01-05 21:25:51 -05004093 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004094 dst_off_in_page = (start_offset + dst_offset) &
4095 ((unsigned long)PAGE_CACHE_SIZE - 1);
4096 src_off_in_page = (start_offset + src_offset) &
4097 ((unsigned long)PAGE_CACHE_SIZE - 1);
4098
4099 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4100 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4101
4102 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4103 src_off_in_page));
4104 cur = min_t(unsigned long, cur,
4105 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4106
4107 copy_pages(extent_buffer_page(dst, dst_i),
4108 extent_buffer_page(dst, src_i),
4109 dst_off_in_page, src_off_in_page, cur);
4110
4111 src_offset += cur;
4112 dst_offset += cur;
4113 len -= cur;
4114 }
4115}
Chris Masond1310b22008-01-24 16:13:08 -05004116
4117void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4118 unsigned long src_offset, unsigned long len)
4119{
4120 size_t cur;
4121 size_t dst_off_in_page;
4122 size_t src_off_in_page;
4123 unsigned long dst_end = dst_offset + len - 1;
4124 unsigned long src_end = src_offset + len - 1;
4125 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4126 unsigned long dst_i;
4127 unsigned long src_i;
4128
4129 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004130 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4131 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004132 BUG_ON(1);
4133 }
4134 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004135 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4136 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004137 BUG_ON(1);
4138 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004139 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004140 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4141 return;
4142 }
Chris Masond3977122009-01-05 21:25:51 -05004143 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004144 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4145 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4146
4147 dst_off_in_page = (start_offset + dst_end) &
4148 ((unsigned long)PAGE_CACHE_SIZE - 1);
4149 src_off_in_page = (start_offset + src_end) &
4150 ((unsigned long)PAGE_CACHE_SIZE - 1);
4151
4152 cur = min_t(unsigned long, len, src_off_in_page + 1);
4153 cur = min(cur, dst_off_in_page + 1);
4154 move_pages(extent_buffer_page(dst, dst_i),
4155 extent_buffer_page(dst, src_i),
4156 dst_off_in_page - cur + 1,
4157 src_off_in_page - cur + 1, cur);
4158
4159 dst_end -= cur;
4160 src_end -= cur;
4161 len -= cur;
4162 }
4163}
Chris Mason6af118c2008-07-22 11:18:07 -04004164
Miao Xie19fe0a82010-10-26 20:57:29 -04004165static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4166{
4167 struct extent_buffer *eb =
4168 container_of(head, struct extent_buffer, rcu_head);
4169
4170 btrfs_release_extent_buffer(eb);
4171}
4172
Chris Mason6af118c2008-07-22 11:18:07 -04004173int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4174{
4175 u64 start = page_offset(page);
4176 struct extent_buffer *eb;
4177 int ret = 1;
Chris Mason6af118c2008-07-22 11:18:07 -04004178
4179 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004180 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004181 if (!eb) {
4182 spin_unlock(&tree->buffer_lock);
4183 return ret;
4184 }
Chris Mason6af118c2008-07-22 11:18:07 -04004185
Chris Masonb9473432009-03-13 11:00:37 -04004186 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4187 ret = 0;
4188 goto out;
4189 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004190
Miao Xie19fe0a82010-10-26 20:57:29 -04004191 /*
4192 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4193 * Or go back.
4194 */
4195 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4196 ret = 0;
4197 goto out;
4198 }
4199
4200 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04004201out:
4202 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004203
4204 /* at this point we can safely release the extent buffer */
4205 if (atomic_read(&eb->refs) == 0)
4206 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118c2008-07-22 11:18:07 -04004207 return ret;
4208}