blob: a32ebfeb91cf509504a126753ac749eb4a95f824 [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"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Chris Masond1310b22008-01-24 16:13:08 -050024
Chris Masond1310b22008-01-24 16:13:08 -050025static struct kmem_cache *extent_state_cache;
26static struct kmem_cache *extent_buffer_cache;
27
28static LIST_HEAD(buffers);
29static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040030
Chris Masonb47eda82008-11-10 12:34:40 -050031#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050032#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050033static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040034#endif
Chris Masond1310b22008-01-24 16:13:08 -050035
Chris Masond1310b22008-01-24 16:13:08 -050036#define BUFFER_LRU_MAX 64
37
38struct tree_entry {
39 u64 start;
40 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050041 struct rb_node rb_node;
42};
43
44struct extent_page_data {
45 struct bio *bio;
46 struct extent_io_tree *tree;
47 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -040048 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -050049
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
52 */
Chris Masonffbd5172009-04-20 15:50:09 -040053 unsigned int extent_locked:1;
54
55 /* tells the submit_bio code to use a WRITE_SYNC */
56 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050057};
58
Josef Bacik0b32f4b2012-03-13 09:38:00 -040059static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040060static inline struct btrfs_fs_info *
61tree_fs_info(struct extent_io_tree *tree)
62{
63 return btrfs_sb(tree->mapping->host->i_sb);
64}
Josef Bacik0b32f4b2012-03-13 09:38:00 -040065
Chris Masond1310b22008-01-24 16:13:08 -050066int __init extent_io_init(void)
67{
David Sterba837e1972012-09-07 03:00:48 -060068 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020069 sizeof(struct extent_state), 0,
70 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050071 if (!extent_state_cache)
72 return -ENOMEM;
73
David Sterba837e1972012-09-07 03:00:48 -060074 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020075 sizeof(struct extent_buffer), 0,
76 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050077 if (!extent_buffer_cache)
78 goto free_state_cache;
79 return 0;
80
81free_state_cache:
82 kmem_cache_destroy(extent_state_cache);
83 return -ENOMEM;
84}
85
86void extent_io_exit(void)
87{
88 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040089 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050090
91 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040092 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050093 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
94 "state %lu in tree %p refs %d\n",
95 (unsigned long long)state->start,
96 (unsigned long long)state->end,
97 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040098 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050099 kmem_cache_free(extent_state_cache, state);
100
101 }
102
Chris Mason2d2ae542008-03-26 16:24:23 -0400103 while (!list_empty(&buffers)) {
104 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500105 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
106 "refs %d\n", (unsigned long long)eb->start,
107 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400108 list_del(&eb->leak_list);
109 kmem_cache_free(extent_buffer_cache, eb);
110 }
Chris Masond1310b22008-01-24 16:13:08 -0500111 if (extent_state_cache)
112 kmem_cache_destroy(extent_state_cache);
113 if (extent_buffer_cache)
114 kmem_cache_destroy(extent_buffer_cache);
115}
116
117void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200118 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500119{
Eric Paris6bef4d32010-02-23 19:43:04 +0000120 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400121 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500122 tree->ops = NULL;
123 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500124 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400125 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500126 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500127}
Chris Masond1310b22008-01-24 16:13:08 -0500128
Christoph Hellwigb2950862008-12-02 09:54:17 -0500129static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500130{
131 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500132#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400133 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400134#endif
Chris Masond1310b22008-01-24 16:13:08 -0500135
136 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400137 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500138 return state;
139 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500140 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500141 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500142#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400143 spin_lock_irqsave(&leak_lock, flags);
144 list_add(&state->leak_list, &states);
145 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400146#endif
Chris Masond1310b22008-01-24 16:13:08 -0500147 atomic_set(&state->refs, 1);
148 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100149 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500150 return state;
151}
Chris Masond1310b22008-01-24 16:13:08 -0500152
Chris Mason4845e442010-05-25 20:56:50 -0400153void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500154{
Chris Masond1310b22008-01-24 16:13:08 -0500155 if (!state)
156 return;
157 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500158#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400159 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400160#endif
Chris Mason70dec802008-01-29 09:59:12 -0500161 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500162#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400163 spin_lock_irqsave(&leak_lock, flags);
164 list_del(&state->leak_list);
165 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400166#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100167 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500168 kmem_cache_free(extent_state_cache, state);
169 }
170}
Chris Masond1310b22008-01-24 16:13:08 -0500171
172static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
173 struct rb_node *node)
174{
Chris Masond3977122009-01-05 21:25:51 -0500175 struct rb_node **p = &root->rb_node;
176 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500177 struct tree_entry *entry;
178
Chris Masond3977122009-01-05 21:25:51 -0500179 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500180 parent = *p;
181 entry = rb_entry(parent, struct tree_entry, rb_node);
182
183 if (offset < entry->start)
184 p = &(*p)->rb_left;
185 else if (offset > entry->end)
186 p = &(*p)->rb_right;
187 else
188 return parent;
189 }
190
Chris Masond1310b22008-01-24 16:13:08 -0500191 rb_link_node(node, parent, p);
192 rb_insert_color(node, root);
193 return NULL;
194}
195
Chris Mason80ea96b2008-02-01 14:51:59 -0500196static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500197 struct rb_node **prev_ret,
198 struct rb_node **next_ret)
199{
Chris Mason80ea96b2008-02-01 14:51:59 -0500200 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500201 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500202 struct rb_node *prev = NULL;
203 struct rb_node *orig_prev = NULL;
204 struct tree_entry *entry;
205 struct tree_entry *prev_entry = NULL;
206
Chris Masond3977122009-01-05 21:25:51 -0500207 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 entry = rb_entry(n, struct tree_entry, rb_node);
209 prev = n;
210 prev_entry = entry;
211
212 if (offset < entry->start)
213 n = n->rb_left;
214 else if (offset > entry->end)
215 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500216 else
Chris Masond1310b22008-01-24 16:13:08 -0500217 return n;
218 }
219
220 if (prev_ret) {
221 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500222 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500223 prev = rb_next(prev);
224 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
225 }
226 *prev_ret = prev;
227 prev = orig_prev;
228 }
229
230 if (next_ret) {
231 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500232 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500233 prev = rb_prev(prev);
234 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
235 }
236 *next_ret = prev;
237 }
238 return NULL;
239}
240
Chris Mason80ea96b2008-02-01 14:51:59 -0500241static inline struct rb_node *tree_search(struct extent_io_tree *tree,
242 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500243{
Chris Mason70dec802008-01-29 09:59:12 -0500244 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500245 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500246
Chris Mason80ea96b2008-02-01 14:51:59 -0500247 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500248 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500249 return prev;
250 return ret;
251}
252
Josef Bacik9ed74f22009-09-11 16:12:44 -0400253static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
254 struct extent_state *other)
255{
256 if (tree->ops && tree->ops->merge_extent_hook)
257 tree->ops->merge_extent_hook(tree->mapping->host, new,
258 other);
259}
260
Chris Masond1310b22008-01-24 16:13:08 -0500261/*
262 * utility function to look for merge candidates inside a given range.
263 * Any extents with matching state are merged together into a single
264 * extent in the tree. Extents with EXTENT_IO in their state field
265 * are not merged because the end_io handlers need to be able to do
266 * operations on them without sleeping (or doing allocations/splits).
267 *
268 * This should be called with the tree lock held.
269 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000270static void merge_state(struct extent_io_tree *tree,
271 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500272{
273 struct extent_state *other;
274 struct rb_node *other_node;
275
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400276 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000277 return;
Chris Masond1310b22008-01-24 16:13:08 -0500278
279 other_node = rb_prev(&state->rb_node);
280 if (other_node) {
281 other = rb_entry(other_node, struct extent_state, rb_node);
282 if (other->end == state->start - 1 &&
283 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400284 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500285 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500286 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500287 rb_erase(&other->rb_node, &tree->state);
288 free_extent_state(other);
289 }
290 }
291 other_node = rb_next(&state->rb_node);
292 if (other_node) {
293 other = rb_entry(other_node, struct extent_state, rb_node);
294 if (other->start == state->end + 1 &&
295 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400296 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400297 state->end = other->end;
298 other->tree = NULL;
299 rb_erase(&other->rb_node, &tree->state);
300 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500301 }
302 }
Chris Masond1310b22008-01-24 16:13:08 -0500303}
304
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000305static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400306 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000308 if (tree->ops && tree->ops->set_bit_hook)
309 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500310}
311
312static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400313 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500314{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400315 if (tree->ops && tree->ops->clear_bit_hook)
316 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500317}
318
Xiao Guangrong3150b692011-07-14 03:19:08 +0000319static void set_state_bits(struct extent_io_tree *tree,
320 struct extent_state *state, int *bits);
321
Chris Masond1310b22008-01-24 16:13:08 -0500322/*
323 * insert an extent_state struct into the tree. 'bits' are set on the
324 * struct before it is inserted.
325 *
326 * This may return -EEXIST if the extent is already there, in which case the
327 * state struct is freed.
328 *
329 * The tree lock is not taken internally. This is a utility function and
330 * probably isn't what you want to call (see set/clear_extent_bit).
331 */
332static int insert_state(struct extent_io_tree *tree,
333 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400334 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500335{
336 struct rb_node *node;
337
338 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500339 printk(KERN_ERR "btrfs end < start %llu %llu\n",
340 (unsigned long long)end,
341 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500342 WARN_ON(1);
343 }
Chris Masond1310b22008-01-24 16:13:08 -0500344 state->start = start;
345 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400346
Xiao Guangrong3150b692011-07-14 03:19:08 +0000347 set_state_bits(tree, state, bits);
348
Chris Masond1310b22008-01-24 16:13:08 -0500349 node = tree_insert(&tree->state, end, &state->rb_node);
350 if (node) {
351 struct extent_state *found;
352 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500353 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
354 "%llu %llu\n", (unsigned long long)found->start,
355 (unsigned long long)found->end,
356 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500357 return -EEXIST;
358 }
Chris Mason70dec802008-01-29 09:59:12 -0500359 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500360 merge_state(tree, state);
361 return 0;
362}
363
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000364static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400365 u64 split)
366{
367 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000368 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400369}
370
Chris Masond1310b22008-01-24 16:13:08 -0500371/*
372 * split a given extent state struct in two, inserting the preallocated
373 * struct 'prealloc' as the newly created second half. 'split' indicates an
374 * offset inside 'orig' where it should be split.
375 *
376 * Before calling,
377 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
378 * are two extent state structs in the tree:
379 * prealloc: [orig->start, split - 1]
380 * orig: [ split, orig->end ]
381 *
382 * The tree locks are not taken by this function. They need to be held
383 * by the caller.
384 */
385static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
386 struct extent_state *prealloc, u64 split)
387{
388 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400389
390 split_cb(tree, orig, split);
391
Chris Masond1310b22008-01-24 16:13:08 -0500392 prealloc->start = orig->start;
393 prealloc->end = split - 1;
394 prealloc->state = orig->state;
395 orig->start = split;
396
397 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
398 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500399 free_extent_state(prealloc);
400 return -EEXIST;
401 }
Chris Mason70dec802008-01-29 09:59:12 -0500402 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500403 return 0;
404}
405
Li Zefancdc6a392012-03-12 16:39:48 +0800406static struct extent_state *next_state(struct extent_state *state)
407{
408 struct rb_node *next = rb_next(&state->rb_node);
409 if (next)
410 return rb_entry(next, struct extent_state, rb_node);
411 else
412 return NULL;
413}
414
Chris Masond1310b22008-01-24 16:13:08 -0500415/*
416 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800417 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500418 *
419 * If no bits are set on the state struct after clearing things, the
420 * struct is freed and removed from the tree
421 */
Li Zefancdc6a392012-03-12 16:39:48 +0800422static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
423 struct extent_state *state,
424 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500425{
Li Zefancdc6a392012-03-12 16:39:48 +0800426 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400427 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500428
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400429 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500430 u64 range = state->end - state->start + 1;
431 WARN_ON(range > tree->dirty_bytes);
432 tree->dirty_bytes -= range;
433 }
Chris Mason291d6732008-01-29 15:55:23 -0500434 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400435 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500436 if (wake)
437 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400438 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800439 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500440 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500441 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500442 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500443 free_extent_state(state);
444 } else {
445 WARN_ON(1);
446 }
447 } else {
448 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800449 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500450 }
Li Zefancdc6a392012-03-12 16:39:48 +0800451 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500452}
453
Xiao Guangrong82337672011-04-20 06:44:57 +0000454static struct extent_state *
455alloc_extent_state_atomic(struct extent_state *prealloc)
456{
457 if (!prealloc)
458 prealloc = alloc_extent_state(GFP_ATOMIC);
459
460 return prealloc;
461}
462
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400463void extent_io_tree_panic(struct extent_io_tree *tree, int err)
464{
465 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
466 "Extent tree was modified by another "
467 "thread while locked.");
468}
469
Chris Masond1310b22008-01-24 16:13:08 -0500470/*
471 * clear some bits on a range in the tree. This may require splitting
472 * or inserting elements in the tree, so the gfp mask is used to
473 * indicate which allocations or sleeping are allowed.
474 *
475 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
476 * the given range from the tree regardless of state (ie for truncate).
477 *
478 * the range [start, end] is inclusive.
479 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100480 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500481 */
482int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400483 int bits, int wake, int delete,
484 struct extent_state **cached_state,
485 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500486{
487 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400488 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500489 struct extent_state *prealloc = NULL;
490 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400491 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500492 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000493 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500494
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400495 if (delete)
496 bits |= ~EXTENT_CTLBITS;
497 bits |= EXTENT_FIRST_DELALLOC;
498
Josef Bacik2ac55d42010-02-03 19:33:23 +0000499 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
500 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500501again:
502 if (!prealloc && (mask & __GFP_WAIT)) {
503 prealloc = alloc_extent_state(mask);
504 if (!prealloc)
505 return -ENOMEM;
506 }
507
Chris Masoncad321a2008-12-17 14:51:42 -0500508 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400509 if (cached_state) {
510 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000511
512 if (clear) {
513 *cached_state = NULL;
514 cached_state = NULL;
515 }
516
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400517 if (cached && cached->tree && cached->start <= start &&
518 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000519 if (clear)
520 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400521 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400522 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400523 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000524 if (clear)
525 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400526 }
Chris Masond1310b22008-01-24 16:13:08 -0500527 /*
528 * this search will find the extents that end after
529 * our range starts
530 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500531 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500532 if (!node)
533 goto out;
534 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400535hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500536 if (state->start > end)
537 goto out;
538 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400539 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500540
Liu Bo04493142012-02-16 18:34:37 +0800541 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800542 if (!(state->state & bits)) {
543 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800544 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800545 }
Liu Bo04493142012-02-16 18:34:37 +0800546
Chris Masond1310b22008-01-24 16:13:08 -0500547 /*
548 * | ---- desired range ---- |
549 * | state | or
550 * | ------------- state -------------- |
551 *
552 * We need to split the extent we found, and may flip
553 * bits on second half.
554 *
555 * If the extent we found extends past our range, we
556 * just split and search again. It'll get split again
557 * the next time though.
558 *
559 * If the extent we found is inside our range, we clear
560 * the desired bit on it.
561 */
562
563 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000564 prealloc = alloc_extent_state_atomic(prealloc);
565 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500566 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400567 if (err)
568 extent_io_tree_panic(tree, err);
569
Chris Masond1310b22008-01-24 16:13:08 -0500570 prealloc = NULL;
571 if (err)
572 goto out;
573 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800574 state = clear_state_bit(tree, state, &bits, wake);
575 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500576 }
577 goto search_again;
578 }
579 /*
580 * | ---- desired range ---- |
581 * | state |
582 * We need to split the extent, and clear the bit
583 * on the first half
584 */
585 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000586 prealloc = alloc_extent_state_atomic(prealloc);
587 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500588 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400589 if (err)
590 extent_io_tree_panic(tree, err);
591
Chris Masond1310b22008-01-24 16:13:08 -0500592 if (wake)
593 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400594
Jeff Mahoney6763af82012-03-01 14:56:29 +0100595 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400596
Chris Masond1310b22008-01-24 16:13:08 -0500597 prealloc = NULL;
598 goto out;
599 }
Chris Mason42daec22009-09-23 19:51:09 -0400600
Li Zefancdc6a392012-03-12 16:39:48 +0800601 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800602next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400603 if (last_end == (u64)-1)
604 goto out;
605 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800606 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800607 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500608 goto search_again;
609
610out:
Chris Masoncad321a2008-12-17 14:51:42 -0500611 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500612 if (prealloc)
613 free_extent_state(prealloc);
614
Jeff Mahoney6763af82012-03-01 14:56:29 +0100615 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500616
617search_again:
618 if (start > end)
619 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500620 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500621 if (mask & __GFP_WAIT)
622 cond_resched();
623 goto again;
624}
Chris Masond1310b22008-01-24 16:13:08 -0500625
Jeff Mahoney143bede2012-03-01 14:56:26 +0100626static void wait_on_state(struct extent_io_tree *tree,
627 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500628 __releases(tree->lock)
629 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500630{
631 DEFINE_WAIT(wait);
632 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500633 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500634 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500635 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500636 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500637}
638
639/*
640 * waits for one or more bits to clear on a range in the state tree.
641 * The range [start, end] is inclusive.
642 * The tree lock is taken by this function
643 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100644void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500645{
646 struct extent_state *state;
647 struct rb_node *node;
648
Chris Masoncad321a2008-12-17 14:51:42 -0500649 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500650again:
651 while (1) {
652 /*
653 * this search will find all the extents that end after
654 * our range starts
655 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500656 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500657 if (!node)
658 break;
659
660 state = rb_entry(node, struct extent_state, rb_node);
661
662 if (state->start > end)
663 goto out;
664
665 if (state->state & bits) {
666 start = state->start;
667 atomic_inc(&state->refs);
668 wait_on_state(tree, state);
669 free_extent_state(state);
670 goto again;
671 }
672 start = state->end + 1;
673
674 if (start > end)
675 break;
676
Xiao Guangrongded91f02011-07-14 03:19:27 +0000677 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500678 }
679out:
Chris Masoncad321a2008-12-17 14:51:42 -0500680 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500681}
Chris Masond1310b22008-01-24 16:13:08 -0500682
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000683static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500684 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400685 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500686{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400687 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400688
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000689 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500691 u64 range = state->end - state->start + 1;
692 tree->dirty_bytes += range;
693 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400694 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500695}
696
Chris Mason2c64c532009-09-02 15:04:12 -0400697static void cache_state(struct extent_state *state,
698 struct extent_state **cached_ptr)
699{
700 if (cached_ptr && !(*cached_ptr)) {
701 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
702 *cached_ptr = state;
703 atomic_inc(&state->refs);
704 }
705 }
706}
707
Arne Jansen507903b2011-04-06 10:02:20 +0000708static void uncache_state(struct extent_state **cached_ptr)
709{
710 if (cached_ptr && (*cached_ptr)) {
711 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400712 *cached_ptr = NULL;
713 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000714 }
715}
716
Chris Masond1310b22008-01-24 16:13:08 -0500717/*
Chris Mason1edbb732009-09-02 13:24:36 -0400718 * set some bits on a range in the tree. This may require allocations or
719 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500720 *
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * If any of the exclusive bits are set, this will fail with -EEXIST if some
722 * part of the range already has the desired bits set. The start of the
723 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500724 *
Chris Mason1edbb732009-09-02 13:24:36 -0400725 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500726 */
Chris Mason1edbb732009-09-02 13:24:36 -0400727
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100728static int __must_check
729__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
730 int bits, int exclusive_bits, u64 *failed_start,
731 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500732{
733 struct extent_state *state;
734 struct extent_state *prealloc = NULL;
735 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500736 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500737 u64 last_start;
738 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400739
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400740 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500741again:
742 if (!prealloc && (mask & __GFP_WAIT)) {
743 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000744 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500745 }
746
Chris Masoncad321a2008-12-17 14:51:42 -0500747 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400748 if (cached_state && *cached_state) {
749 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400750 if (state->start <= start && state->end > start &&
751 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400752 node = &state->rb_node;
753 goto hit_next;
754 }
755 }
Chris Masond1310b22008-01-24 16:13:08 -0500756 /*
757 * this search will find all the extents that end after
758 * our range starts.
759 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500760 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500761 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000762 prealloc = alloc_extent_state_atomic(prealloc);
763 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400764 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400765 if (err)
766 extent_io_tree_panic(tree, err);
767
Chris Masond1310b22008-01-24 16:13:08 -0500768 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500769 goto out;
770 }
Chris Masond1310b22008-01-24 16:13:08 -0500771 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400772hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500773 last_start = state->start;
774 last_end = state->end;
775
776 /*
777 * | ---- desired range ---- |
778 * | state |
779 *
780 * Just lock what we found and keep going
781 */
782 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400783 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500784 *failed_start = state->start;
785 err = -EEXIST;
786 goto out;
787 }
Chris Mason42daec22009-09-23 19:51:09 -0400788
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000789 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400790 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500791 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400792 if (last_end == (u64)-1)
793 goto out;
794 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800795 state = next_state(state);
796 if (start < end && state && state->start == start &&
797 !need_resched())
798 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500799 goto search_again;
800 }
801
802 /*
803 * | ---- desired range ---- |
804 * | state |
805 * or
806 * | ------------- state -------------- |
807 *
808 * We need to split the extent we found, and may flip bits on
809 * second half.
810 *
811 * If the extent we found extends past our
812 * range, we just split and search again. It'll get split
813 * again the next time though.
814 *
815 * If the extent we found is inside our range, we set the
816 * desired bit on it.
817 */
818 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400819 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500820 *failed_start = start;
821 err = -EEXIST;
822 goto out;
823 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000824
825 prealloc = alloc_extent_state_atomic(prealloc);
826 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500827 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400828 if (err)
829 extent_io_tree_panic(tree, err);
830
Chris Masond1310b22008-01-24 16:13:08 -0500831 prealloc = NULL;
832 if (err)
833 goto out;
834 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000835 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400836 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500837 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400838 if (last_end == (u64)-1)
839 goto out;
840 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800841 state = next_state(state);
842 if (start < end && state && state->start == start &&
843 !need_resched())
844 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500845 }
846 goto search_again;
847 }
848 /*
849 * | ---- desired range ---- |
850 * | state | or | state |
851 *
852 * There's a hole, we need to insert something in it and
853 * ignore the extent we found.
854 */
855 if (state->start > start) {
856 u64 this_end;
857 if (end < last_start)
858 this_end = end;
859 else
Chris Masond3977122009-01-05 21:25:51 -0500860 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000861
862 prealloc = alloc_extent_state_atomic(prealloc);
863 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000864
865 /*
866 * Avoid to free 'prealloc' if it can be merged with
867 * the later extent.
868 */
Chris Masond1310b22008-01-24 16:13:08 -0500869 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400870 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400871 if (err)
872 extent_io_tree_panic(tree, err);
873
Chris Mason2c64c532009-09-02 15:04:12 -0400874 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500875 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500876 start = this_end + 1;
877 goto search_again;
878 }
879 /*
880 * | ---- desired range ---- |
881 * | state |
882 * We need to split the extent, and set the bit
883 * on the first half
884 */
885 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400886 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500887 *failed_start = start;
888 err = -EEXIST;
889 goto out;
890 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000891
892 prealloc = alloc_extent_state_atomic(prealloc);
893 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500894 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400895 if (err)
896 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500897
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000898 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400899 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500900 merge_state(tree, prealloc);
901 prealloc = NULL;
902 goto out;
903 }
904
905 goto search_again;
906
907out:
Chris Masoncad321a2008-12-17 14:51:42 -0500908 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500909 if (prealloc)
910 free_extent_state(prealloc);
911
912 return err;
913
914search_again:
915 if (start > end)
916 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500917 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500918 if (mask & __GFP_WAIT)
919 cond_resched();
920 goto again;
921}
Chris Masond1310b22008-01-24 16:13:08 -0500922
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100923int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
924 u64 *failed_start, struct extent_state **cached_state,
925 gfp_t mask)
926{
927 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
928 cached_state, mask);
929}
930
931
Josef Bacik462d6fa2011-09-26 13:56:12 -0400932/**
Liu Bo10983f22012-07-11 15:26:19 +0800933 * convert_extent_bit - convert all bits in a given range from one bit to
934 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400935 * @tree: the io tree to search
936 * @start: the start offset in bytes
937 * @end: the end offset in bytes (inclusive)
938 * @bits: the bits to set in this range
939 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400940 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400941 * @mask: the allocation mask
942 *
943 * This will go through and set bits for the given range. If any states exist
944 * already in this range they are set with the given bit and cleared of the
945 * clear_bits. This is only meant to be used by things that are mergeable, ie
946 * converting from say DELALLOC to DIRTY. This is not meant to be used with
947 * boundary bits like LOCK.
948 */
949int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacike6138872012-09-27 17:07:30 -0400950 int bits, int clear_bits,
951 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400952{
953 struct extent_state *state;
954 struct extent_state *prealloc = NULL;
955 struct rb_node *node;
956 int err = 0;
957 u64 last_start;
958 u64 last_end;
959
960again:
961 if (!prealloc && (mask & __GFP_WAIT)) {
962 prealloc = alloc_extent_state(mask);
963 if (!prealloc)
964 return -ENOMEM;
965 }
966
967 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400968 if (cached_state && *cached_state) {
969 state = *cached_state;
970 if (state->start <= start && state->end > start &&
971 state->tree) {
972 node = &state->rb_node;
973 goto hit_next;
974 }
975 }
976
Josef Bacik462d6fa2011-09-26 13:56:12 -0400977 /*
978 * this search will find all the extents that end after
979 * our range starts.
980 */
981 node = tree_search(tree, start);
982 if (!node) {
983 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500984 if (!prealloc) {
985 err = -ENOMEM;
986 goto out;
987 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400988 err = insert_state(tree, prealloc, start, end, &bits);
989 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400990 if (err)
991 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400992 goto out;
993 }
994 state = rb_entry(node, struct extent_state, rb_node);
995hit_next:
996 last_start = state->start;
997 last_end = state->end;
998
999 /*
1000 * | ---- desired range ---- |
1001 * | state |
1002 *
1003 * Just lock what we found and keep going
1004 */
1005 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001006 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001007 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001008 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001009 if (last_end == (u64)-1)
1010 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001011 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001012 if (start < end && state && state->start == start &&
1013 !need_resched())
1014 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001015 goto search_again;
1016 }
1017
1018 /*
1019 * | ---- desired range ---- |
1020 * | state |
1021 * or
1022 * | ------------- state -------------- |
1023 *
1024 * We need to split the extent we found, and may flip bits on
1025 * second half.
1026 *
1027 * If the extent we found extends past our
1028 * range, we just split and search again. It'll get split
1029 * again the next time though.
1030 *
1031 * If the extent we found is inside our range, we set the
1032 * desired bit on it.
1033 */
1034 if (state->start < start) {
1035 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001036 if (!prealloc) {
1037 err = -ENOMEM;
1038 goto out;
1039 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001040 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001041 if (err)
1042 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001043 prealloc = NULL;
1044 if (err)
1045 goto out;
1046 if (state->end <= end) {
1047 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001048 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001049 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001050 if (last_end == (u64)-1)
1051 goto out;
1052 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001053 if (start < end && state && state->start == start &&
1054 !need_resched())
1055 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001056 }
1057 goto search_again;
1058 }
1059 /*
1060 * | ---- desired range ---- |
1061 * | state | or | state |
1062 *
1063 * There's a hole, we need to insert something in it and
1064 * ignore the extent we found.
1065 */
1066 if (state->start > start) {
1067 u64 this_end;
1068 if (end < last_start)
1069 this_end = end;
1070 else
1071 this_end = last_start - 1;
1072
1073 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001074 if (!prealloc) {
1075 err = -ENOMEM;
1076 goto out;
1077 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001078
1079 /*
1080 * Avoid to free 'prealloc' if it can be merged with
1081 * the later extent.
1082 */
1083 err = insert_state(tree, prealloc, start, this_end,
1084 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001085 if (err)
1086 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001087 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001088 prealloc = NULL;
1089 start = this_end + 1;
1090 goto search_again;
1091 }
1092 /*
1093 * | ---- desired range ---- |
1094 * | state |
1095 * We need to split the extent, and set the bit
1096 * on the first half
1097 */
1098 if (state->start <= end && state->end > end) {
1099 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001100 if (!prealloc) {
1101 err = -ENOMEM;
1102 goto out;
1103 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001104
1105 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001106 if (err)
1107 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108
1109 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001110 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001111 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001112 prealloc = NULL;
1113 goto out;
1114 }
1115
1116 goto search_again;
1117
1118out:
1119 spin_unlock(&tree->lock);
1120 if (prealloc)
1121 free_extent_state(prealloc);
1122
1123 return err;
1124
1125search_again:
1126 if (start > end)
1127 goto out;
1128 spin_unlock(&tree->lock);
1129 if (mask & __GFP_WAIT)
1130 cond_resched();
1131 goto again;
1132}
1133
Chris Masond1310b22008-01-24 16:13:08 -05001134/* wrappers around set/clear extent bit */
1135int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1136 gfp_t mask)
1137{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001138 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001139 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001140}
Chris Masond1310b22008-01-24 16:13:08 -05001141
1142int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1143 int bits, gfp_t mask)
1144{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001145 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001146 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001147}
Chris Masond1310b22008-01-24 16:13:08 -05001148
1149int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1150 int bits, gfp_t mask)
1151{
Chris Mason2c64c532009-09-02 15:04:12 -04001152 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001153}
Chris Masond1310b22008-01-24 16:13:08 -05001154
1155int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001156 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001157{
1158 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001159 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001160 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001161}
Chris Masond1310b22008-01-24 16:13:08 -05001162
Liu Bo9e8a4a82012-09-05 19:10:51 -06001163int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1164 struct extent_state **cached_state, gfp_t mask)
1165{
1166 return set_extent_bit(tree, start, end,
1167 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1168 NULL, cached_state, mask);
1169}
1170
Chris Masond1310b22008-01-24 16:13:08 -05001171int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1172 gfp_t mask)
1173{
1174 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001175 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001176 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001177}
Chris Masond1310b22008-01-24 16:13:08 -05001178
1179int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1180 gfp_t mask)
1181{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001182 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001183 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001184}
Chris Masond1310b22008-01-24 16:13:08 -05001185
Chris Masond1310b22008-01-24 16:13:08 -05001186int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001187 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001188{
Arne Jansen507903b2011-04-06 10:02:20 +00001189 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001190 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001191}
Chris Masond1310b22008-01-24 16:13:08 -05001192
Josef Bacik5fd02042012-05-02 14:00:54 -04001193int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1194 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001195{
Chris Mason2c64c532009-09-02 15:04:12 -04001196 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001197 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001198}
Chris Masond1310b22008-01-24 16:13:08 -05001199
Chris Masond352ac62008-09-29 15:18:18 -04001200/*
1201 * either insert or lock state struct between start and end use mask to tell
1202 * us if waiting is desired.
1203 */
Chris Mason1edbb732009-09-02 13:24:36 -04001204int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001205 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001206{
1207 int err;
1208 u64 failed_start;
1209 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001210 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1211 EXTENT_LOCKED, &failed_start,
1212 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001213 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001214 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1215 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001216 } else
Chris Masond1310b22008-01-24 16:13:08 -05001217 break;
Chris Masond1310b22008-01-24 16:13:08 -05001218 WARN_ON(start > end);
1219 }
1220 return err;
1221}
Chris Masond1310b22008-01-24 16:13:08 -05001222
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001223int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001224{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001225 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001226}
1227
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001228int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001229{
1230 int err;
1231 u64 failed_start;
1232
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001233 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1234 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001235 if (err == -EEXIST) {
1236 if (failed_start > start)
1237 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001238 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001239 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001240 }
Josef Bacik25179202008-10-29 14:49:05 -04001241 return 1;
1242}
Josef Bacik25179202008-10-29 14:49:05 -04001243
Chris Mason2c64c532009-09-02 15:04:12 -04001244int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1245 struct extent_state **cached, gfp_t mask)
1246{
1247 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1248 mask);
1249}
1250
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001251int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001252{
Chris Mason2c64c532009-09-02 15:04:12 -04001253 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001254 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001255}
Chris Masond1310b22008-01-24 16:13:08 -05001256
1257/*
Chris Masond1310b22008-01-24 16:13:08 -05001258 * helper function to set both pages and extents in the tree writeback
1259 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001260static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001261{
1262 unsigned long index = start >> PAGE_CACHE_SHIFT;
1263 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1264 struct page *page;
1265
1266 while (index <= end_index) {
1267 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001268 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001269 set_page_writeback(page);
1270 page_cache_release(page);
1271 index++;
1272 }
Chris Masond1310b22008-01-24 16:13:08 -05001273 return 0;
1274}
Chris Masond1310b22008-01-24 16:13:08 -05001275
Chris Masond352ac62008-09-29 15:18:18 -04001276/* find the first state struct with 'bits' set after 'start', and
1277 * return it. tree->lock must be held. NULL will returned if
1278 * nothing was found after 'start'
1279 */
Chris Masond7fc6402008-02-18 12:12:38 -05001280struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1281 u64 start, int bits)
1282{
1283 struct rb_node *node;
1284 struct extent_state *state;
1285
1286 /*
1287 * this search will find all the extents that end after
1288 * our range starts.
1289 */
1290 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001291 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001292 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001293
Chris Masond3977122009-01-05 21:25:51 -05001294 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001295 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001296 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001297 return state;
Chris Masond3977122009-01-05 21:25:51 -05001298
Chris Masond7fc6402008-02-18 12:12:38 -05001299 node = rb_next(node);
1300 if (!node)
1301 break;
1302 }
1303out:
1304 return NULL;
1305}
Chris Masond7fc6402008-02-18 12:12:38 -05001306
Chris Masond352ac62008-09-29 15:18:18 -04001307/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001308 * find the first offset in the io tree with 'bits' set. zero is
1309 * returned if we find something, and *start_ret and *end_ret are
1310 * set to reflect the state struct that was found.
1311 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001312 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001313 */
1314int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Josef Bacike6138872012-09-27 17:07:30 -04001315 u64 *start_ret, u64 *end_ret, int bits,
1316 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001317{
1318 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001319 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001320 int ret = 1;
1321
1322 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001323 if (cached_state && *cached_state) {
1324 state = *cached_state;
1325 if (state->end == start - 1 && state->tree) {
1326 n = rb_next(&state->rb_node);
1327 while (n) {
1328 state = rb_entry(n, struct extent_state,
1329 rb_node);
1330 if (state->state & bits)
1331 goto got_it;
1332 n = rb_next(n);
1333 }
1334 free_extent_state(*cached_state);
1335 *cached_state = NULL;
1336 goto out;
1337 }
1338 free_extent_state(*cached_state);
1339 *cached_state = NULL;
1340 }
1341
Xiao Guangrong69261c42011-07-14 03:19:45 +00001342 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001343got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001344 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001345 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001346 *start_ret = state->start;
1347 *end_ret = state->end;
1348 ret = 0;
1349 }
Josef Bacike6138872012-09-27 17:07:30 -04001350out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001351 spin_unlock(&tree->lock);
1352 return ret;
1353}
1354
1355/*
Chris Masond352ac62008-09-29 15:18:18 -04001356 * find a contiguous range of bytes in the file marked as delalloc, not
1357 * more than 'max_bytes'. start and end are used to return the range,
1358 *
1359 * 1 is returned if we find something, 0 if nothing was in the tree
1360 */
Chris Masonc8b97812008-10-29 14:49:59 -04001361static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001362 u64 *start, u64 *end, u64 max_bytes,
1363 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001364{
1365 struct rb_node *node;
1366 struct extent_state *state;
1367 u64 cur_start = *start;
1368 u64 found = 0;
1369 u64 total_bytes = 0;
1370
Chris Masoncad321a2008-12-17 14:51:42 -05001371 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001372
Chris Masond1310b22008-01-24 16:13:08 -05001373 /*
1374 * this search will find all the extents that end after
1375 * our range starts.
1376 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001377 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001378 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001379 if (!found)
1380 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001381 goto out;
1382 }
1383
Chris Masond3977122009-01-05 21:25:51 -05001384 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001385 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001386 if (found && (state->start != cur_start ||
1387 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001388 goto out;
1389 }
1390 if (!(state->state & EXTENT_DELALLOC)) {
1391 if (!found)
1392 *end = state->end;
1393 goto out;
1394 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001395 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001396 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001397 *cached_state = state;
1398 atomic_inc(&state->refs);
1399 }
Chris Masond1310b22008-01-24 16:13:08 -05001400 found++;
1401 *end = state->end;
1402 cur_start = state->end + 1;
1403 node = rb_next(node);
1404 if (!node)
1405 break;
1406 total_bytes += state->end - state->start + 1;
1407 if (total_bytes >= max_bytes)
1408 break;
1409 }
1410out:
Chris Masoncad321a2008-12-17 14:51:42 -05001411 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001412 return found;
1413}
1414
Jeff Mahoney143bede2012-03-01 14:56:26 +01001415static noinline void __unlock_for_delalloc(struct inode *inode,
1416 struct page *locked_page,
1417 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001418{
1419 int ret;
1420 struct page *pages[16];
1421 unsigned long index = start >> PAGE_CACHE_SHIFT;
1422 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1423 unsigned long nr_pages = end_index - index + 1;
1424 int i;
1425
1426 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001427 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001428
Chris Masond3977122009-01-05 21:25:51 -05001429 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001430 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001431 min_t(unsigned long, nr_pages,
1432 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001433 for (i = 0; i < ret; i++) {
1434 if (pages[i] != locked_page)
1435 unlock_page(pages[i]);
1436 page_cache_release(pages[i]);
1437 }
1438 nr_pages -= ret;
1439 index += ret;
1440 cond_resched();
1441 }
Chris Masonc8b97812008-10-29 14:49:59 -04001442}
1443
1444static noinline int lock_delalloc_pages(struct inode *inode,
1445 struct page *locked_page,
1446 u64 delalloc_start,
1447 u64 delalloc_end)
1448{
1449 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1450 unsigned long start_index = index;
1451 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1452 unsigned long pages_locked = 0;
1453 struct page *pages[16];
1454 unsigned long nrpages;
1455 int ret;
1456 int i;
1457
1458 /* the caller is responsible for locking the start index */
1459 if (index == locked_page->index && index == end_index)
1460 return 0;
1461
1462 /* skip the page at the start index */
1463 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001464 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001465 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001466 min_t(unsigned long,
1467 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001468 if (ret == 0) {
1469 ret = -EAGAIN;
1470 goto done;
1471 }
1472 /* now we have an array of pages, lock them all */
1473 for (i = 0; i < ret; i++) {
1474 /*
1475 * the caller is taking responsibility for
1476 * locked_page
1477 */
Chris Mason771ed682008-11-06 22:02:51 -05001478 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001479 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001480 if (!PageDirty(pages[i]) ||
1481 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001482 ret = -EAGAIN;
1483 unlock_page(pages[i]);
1484 page_cache_release(pages[i]);
1485 goto done;
1486 }
1487 }
Chris Masonc8b97812008-10-29 14:49:59 -04001488 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001489 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001490 }
Chris Masonc8b97812008-10-29 14:49:59 -04001491 nrpages -= ret;
1492 index += ret;
1493 cond_resched();
1494 }
1495 ret = 0;
1496done:
1497 if (ret && pages_locked) {
1498 __unlock_for_delalloc(inode, locked_page,
1499 delalloc_start,
1500 ((u64)(start_index + pages_locked - 1)) <<
1501 PAGE_CACHE_SHIFT);
1502 }
1503 return ret;
1504}
1505
1506/*
1507 * find a contiguous range of bytes in the file marked as delalloc, not
1508 * more than 'max_bytes'. start and end are used to return the range,
1509 *
1510 * 1 is returned if we find something, 0 if nothing was in the tree
1511 */
1512static noinline u64 find_lock_delalloc_range(struct inode *inode,
1513 struct extent_io_tree *tree,
1514 struct page *locked_page,
1515 u64 *start, u64 *end,
1516 u64 max_bytes)
1517{
1518 u64 delalloc_start;
1519 u64 delalloc_end;
1520 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001521 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001522 int ret;
1523 int loops = 0;
1524
1525again:
1526 /* step one, find a bunch of delalloc bytes starting at start */
1527 delalloc_start = *start;
1528 delalloc_end = 0;
1529 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001530 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001531 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001532 *start = delalloc_start;
1533 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001534 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001535 return found;
1536 }
1537
1538 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001539 * start comes from the offset of locked_page. We have to lock
1540 * pages in order, so we can't process delalloc bytes before
1541 * locked_page
1542 */
Chris Masond3977122009-01-05 21:25:51 -05001543 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001544 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001545
1546 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001547 * make sure to limit the number of pages we try to lock down
1548 * if we're looping.
1549 */
Chris Masond3977122009-01-05 21:25:51 -05001550 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001551 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001552
Chris Masonc8b97812008-10-29 14:49:59 -04001553 /* step two, lock all the pages after the page that has start */
1554 ret = lock_delalloc_pages(inode, locked_page,
1555 delalloc_start, delalloc_end);
1556 if (ret == -EAGAIN) {
1557 /* some of the pages are gone, lets avoid looping by
1558 * shortening the size of the delalloc range we're searching
1559 */
Chris Mason9655d292009-09-02 15:22:30 -04001560 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001561 if (!loops) {
1562 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1563 max_bytes = PAGE_CACHE_SIZE - offset;
1564 loops = 1;
1565 goto again;
1566 } else {
1567 found = 0;
1568 goto out_failed;
1569 }
1570 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001571 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001572
1573 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001574 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001575
1576 /* then test to make sure it is all still delalloc */
1577 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001578 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001579 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001580 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1581 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001582 __unlock_for_delalloc(inode, locked_page,
1583 delalloc_start, delalloc_end);
1584 cond_resched();
1585 goto again;
1586 }
Chris Mason9655d292009-09-02 15:22:30 -04001587 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001588 *start = delalloc_start;
1589 *end = delalloc_end;
1590out_failed:
1591 return found;
1592}
1593
1594int extent_clear_unlock_delalloc(struct inode *inode,
1595 struct extent_io_tree *tree,
1596 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001597 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001598{
1599 int ret;
1600 struct page *pages[16];
1601 unsigned long index = start >> PAGE_CACHE_SHIFT;
1602 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1603 unsigned long nr_pages = end_index - index + 1;
1604 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001605 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001606
Chris Masona791e352009-10-08 11:27:10 -04001607 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001608 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001609 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001610 clear_bits |= EXTENT_DIRTY;
1611
Chris Masona791e352009-10-08 11:27:10 -04001612 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001613 clear_bits |= EXTENT_DELALLOC;
1614
Chris Mason2c64c532009-09-02 15:04:12 -04001615 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001616 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1617 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1618 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001619 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001620
Chris Masond3977122009-01-05 21:25:51 -05001621 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001622 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001623 min_t(unsigned long,
1624 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001625 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001626
Chris Masona791e352009-10-08 11:27:10 -04001627 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001628 SetPagePrivate2(pages[i]);
1629
Chris Masonc8b97812008-10-29 14:49:59 -04001630 if (pages[i] == locked_page) {
1631 page_cache_release(pages[i]);
1632 continue;
1633 }
Chris Masona791e352009-10-08 11:27:10 -04001634 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001635 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001636 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001637 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001638 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001639 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001640 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001641 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001642 page_cache_release(pages[i]);
1643 }
1644 nr_pages -= ret;
1645 index += ret;
1646 cond_resched();
1647 }
1648 return 0;
1649}
Chris Masonc8b97812008-10-29 14:49:59 -04001650
Chris Masond352ac62008-09-29 15:18:18 -04001651/*
1652 * count the number of bytes in the tree that have a given bit(s)
1653 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1654 * cached. The total number found is returned.
1655 */
Chris Masond1310b22008-01-24 16:13:08 -05001656u64 count_range_bits(struct extent_io_tree *tree,
1657 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001658 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001659{
1660 struct rb_node *node;
1661 struct extent_state *state;
1662 u64 cur_start = *start;
1663 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001664 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001665 int found = 0;
1666
1667 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001668 WARN_ON(1);
1669 return 0;
1670 }
1671
Chris Masoncad321a2008-12-17 14:51:42 -05001672 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001673 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1674 total_bytes = tree->dirty_bytes;
1675 goto out;
1676 }
1677 /*
1678 * this search will find all the extents that end after
1679 * our range starts.
1680 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001681 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001682 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001683 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001684
Chris Masond3977122009-01-05 21:25:51 -05001685 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001686 state = rb_entry(node, struct extent_state, rb_node);
1687 if (state->start > search_end)
1688 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001689 if (contig && found && state->start > last + 1)
1690 break;
1691 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001692 total_bytes += min(search_end, state->end) + 1 -
1693 max(cur_start, state->start);
1694 if (total_bytes >= max_bytes)
1695 break;
1696 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001697 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001698 found = 1;
1699 }
Chris Masonec29ed52011-02-23 16:23:20 -05001700 last = state->end;
1701 } else if (contig && found) {
1702 break;
Chris Masond1310b22008-01-24 16:13:08 -05001703 }
1704 node = rb_next(node);
1705 if (!node)
1706 break;
1707 }
1708out:
Chris Masoncad321a2008-12-17 14:51:42 -05001709 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001710 return total_bytes;
1711}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001712
Chris Masond352ac62008-09-29 15:18:18 -04001713/*
1714 * set the private field for a given byte offset in the tree. If there isn't
1715 * an extent_state there already, this does nothing.
1716 */
Chris Masond1310b22008-01-24 16:13:08 -05001717int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1718{
1719 struct rb_node *node;
1720 struct extent_state *state;
1721 int ret = 0;
1722
Chris Masoncad321a2008-12-17 14:51:42 -05001723 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001724 /*
1725 * this search will find all the extents that end after
1726 * our range starts.
1727 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001728 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001729 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001730 ret = -ENOENT;
1731 goto out;
1732 }
1733 state = rb_entry(node, struct extent_state, rb_node);
1734 if (state->start != start) {
1735 ret = -ENOENT;
1736 goto out;
1737 }
1738 state->private = private;
1739out:
Chris Masoncad321a2008-12-17 14:51:42 -05001740 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001741 return ret;
1742}
1743
1744int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1745{
1746 struct rb_node *node;
1747 struct extent_state *state;
1748 int ret = 0;
1749
Chris Masoncad321a2008-12-17 14:51:42 -05001750 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001751 /*
1752 * this search will find all the extents that end after
1753 * our range starts.
1754 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001755 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001756 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001757 ret = -ENOENT;
1758 goto out;
1759 }
1760 state = rb_entry(node, struct extent_state, rb_node);
1761 if (state->start != start) {
1762 ret = -ENOENT;
1763 goto out;
1764 }
1765 *private = state->private;
1766out:
Chris Masoncad321a2008-12-17 14:51:42 -05001767 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001768 return ret;
1769}
1770
1771/*
1772 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001773 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001774 * has the bits set. Otherwise, 1 is returned if any bit in the
1775 * range is found set.
1776 */
1777int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001778 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001779{
1780 struct extent_state *state = NULL;
1781 struct rb_node *node;
1782 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001783
Chris Masoncad321a2008-12-17 14:51:42 -05001784 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001785 if (cached && cached->tree && cached->start <= start &&
1786 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001787 node = &cached->rb_node;
1788 else
1789 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001790 while (node && start <= end) {
1791 state = rb_entry(node, struct extent_state, rb_node);
1792
1793 if (filled && state->start > start) {
1794 bitset = 0;
1795 break;
1796 }
1797
1798 if (state->start > end)
1799 break;
1800
1801 if (state->state & bits) {
1802 bitset = 1;
1803 if (!filled)
1804 break;
1805 } else if (filled) {
1806 bitset = 0;
1807 break;
1808 }
Chris Mason46562cec2009-09-23 20:23:16 -04001809
1810 if (state->end == (u64)-1)
1811 break;
1812
Chris Masond1310b22008-01-24 16:13:08 -05001813 start = state->end + 1;
1814 if (start > end)
1815 break;
1816 node = rb_next(node);
1817 if (!node) {
1818 if (filled)
1819 bitset = 0;
1820 break;
1821 }
1822 }
Chris Masoncad321a2008-12-17 14:51:42 -05001823 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001824 return bitset;
1825}
Chris Masond1310b22008-01-24 16:13:08 -05001826
1827/*
1828 * helper function to set a given page up to date if all the
1829 * extents in the tree for that page are up to date
1830 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001831static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001832{
1833 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1834 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001835 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001836 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001837}
1838
1839/*
1840 * helper function to unlock a page if all the extents in the tree
1841 * for that page are unlocked
1842 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001843static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001844{
1845 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1846 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001847 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001848 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001849}
1850
1851/*
1852 * helper function to end page writeback if all the extents
1853 * in the tree for that page are done with writeback
1854 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001855static void check_page_writeback(struct extent_io_tree *tree,
1856 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001857{
Chris Mason1edbb732009-09-02 13:24:36 -04001858 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001859}
1860
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001861/*
1862 * When IO fails, either with EIO or csum verification fails, we
1863 * try other mirrors that might have a good copy of the data. This
1864 * io_failure_record is used to record state as we go through all the
1865 * mirrors. If another mirror has good data, the page is set up to date
1866 * and things continue. If a good mirror can't be found, the original
1867 * bio end_io callback is called to indicate things have failed.
1868 */
1869struct io_failure_record {
1870 struct page *page;
1871 u64 start;
1872 u64 len;
1873 u64 logical;
1874 unsigned long bio_flags;
1875 int this_mirror;
1876 int failed_mirror;
1877 int in_validation;
1878};
1879
1880static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1881 int did_repair)
1882{
1883 int ret;
1884 int err = 0;
1885 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1886
1887 set_state_private(failure_tree, rec->start, 0);
1888 ret = clear_extent_bits(failure_tree, rec->start,
1889 rec->start + rec->len - 1,
1890 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1891 if (ret)
1892 err = ret;
1893
1894 if (did_repair) {
1895 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1896 rec->start + rec->len - 1,
1897 EXTENT_DAMAGED, GFP_NOFS);
1898 if (ret && !err)
1899 err = ret;
1900 }
1901
1902 kfree(rec);
1903 return err;
1904}
1905
1906static void repair_io_failure_callback(struct bio *bio, int err)
1907{
1908 complete(bio->bi_private);
1909}
1910
1911/*
1912 * this bypasses the standard btrfs submit functions deliberately, as
1913 * the standard behavior is to write all copies in a raid setup. here we only
1914 * want to write the one bad copy. so we do the mapping for ourselves and issue
1915 * submit_bio directly.
1916 * to avoid any synchonization issues, wait for the data after writing, which
1917 * actually prevents the read that triggered the error from finishing.
1918 * currently, there can be no more than two copies of every data bit. thus,
1919 * exactly one rewrite is required.
1920 */
1921int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1922 u64 length, u64 logical, struct page *page,
1923 int mirror_num)
1924{
1925 struct bio *bio;
1926 struct btrfs_device *dev;
1927 DECLARE_COMPLETION_ONSTACK(compl);
1928 u64 map_length = 0;
1929 u64 sector;
1930 struct btrfs_bio *bbio = NULL;
1931 int ret;
1932
1933 BUG_ON(!mirror_num);
1934
1935 bio = bio_alloc(GFP_NOFS, 1);
1936 if (!bio)
1937 return -EIO;
1938 bio->bi_private = &compl;
1939 bio->bi_end_io = repair_io_failure_callback;
1940 bio->bi_size = 0;
1941 map_length = length;
1942
1943 ret = btrfs_map_block(map_tree, WRITE, logical,
1944 &map_length, &bbio, mirror_num);
1945 if (ret) {
1946 bio_put(bio);
1947 return -EIO;
1948 }
1949 BUG_ON(mirror_num != bbio->mirror_num);
1950 sector = bbio->stripes[mirror_num-1].physical >> 9;
1951 bio->bi_sector = sector;
1952 dev = bbio->stripes[mirror_num-1].dev;
1953 kfree(bbio);
1954 if (!dev || !dev->bdev || !dev->writeable) {
1955 bio_put(bio);
1956 return -EIO;
1957 }
1958 bio->bi_bdev = dev->bdev;
1959 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001960 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001961 wait_for_completion(&compl);
1962
1963 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1964 /* try to remap that extent elsewhere? */
1965 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001966 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001967 return -EIO;
1968 }
1969
Anand Jaind5b025d2012-07-02 22:05:21 -06001970 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04001971 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1972 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001973
1974 bio_put(bio);
1975 return 0;
1976}
1977
Josef Bacikea466792012-03-26 21:57:36 -04001978int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1979 int mirror_num)
1980{
1981 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1982 u64 start = eb->start;
1983 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001984 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001985
1986 for (i = 0; i < num_pages; i++) {
1987 struct page *p = extent_buffer_page(eb, i);
1988 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1989 start, p, mirror_num);
1990 if (ret)
1991 break;
1992 start += PAGE_CACHE_SIZE;
1993 }
1994
1995 return ret;
1996}
1997
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001998/*
1999 * each time an IO finishes, we do a fast check in the IO failure tree
2000 * to see if we need to process or clean up an io_failure_record
2001 */
2002static int clean_io_failure(u64 start, struct page *page)
2003{
2004 u64 private;
2005 u64 private_failure;
2006 struct io_failure_record *failrec;
2007 struct btrfs_mapping_tree *map_tree;
2008 struct extent_state *state;
2009 int num_copies;
2010 int did_repair = 0;
2011 int ret;
2012 struct inode *inode = page->mapping->host;
2013
2014 private = 0;
2015 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2016 (u64)-1, 1, EXTENT_DIRTY, 0);
2017 if (!ret)
2018 return 0;
2019
2020 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2021 &private_failure);
2022 if (ret)
2023 return 0;
2024
2025 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2026 BUG_ON(!failrec->this_mirror);
2027
2028 if (failrec->in_validation) {
2029 /* there was no real error, just free the record */
2030 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2031 failrec->start);
2032 did_repair = 1;
2033 goto out;
2034 }
2035
2036 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2037 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2038 failrec->start,
2039 EXTENT_LOCKED);
2040 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2041
2042 if (state && state->start == failrec->start) {
2043 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2044 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2045 failrec->len);
2046 if (num_copies > 1) {
2047 ret = repair_io_failure(map_tree, start, failrec->len,
2048 failrec->logical, page,
2049 failrec->failed_mirror);
2050 did_repair = !ret;
2051 }
2052 }
2053
2054out:
2055 if (!ret)
2056 ret = free_io_failure(inode, failrec, did_repair);
2057
2058 return ret;
2059}
2060
2061/*
2062 * this is a generic handler for readpage errors (default
2063 * readpage_io_failed_hook). if other copies exist, read those and write back
2064 * good data to the failed position. does not investigate in remapping the
2065 * failed extent elsewhere, hoping the device will be smart enough to do this as
2066 * needed
2067 */
2068
2069static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2070 u64 start, u64 end, int failed_mirror,
2071 struct extent_state *state)
2072{
2073 struct io_failure_record *failrec = NULL;
2074 u64 private;
2075 struct extent_map *em;
2076 struct inode *inode = page->mapping->host;
2077 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2078 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2079 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2080 struct bio *bio;
2081 int num_copies;
2082 int ret;
2083 int read_mode;
2084 u64 logical;
2085
2086 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2087
2088 ret = get_state_private(failure_tree, start, &private);
2089 if (ret) {
2090 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2091 if (!failrec)
2092 return -ENOMEM;
2093 failrec->start = start;
2094 failrec->len = end - start + 1;
2095 failrec->this_mirror = 0;
2096 failrec->bio_flags = 0;
2097 failrec->in_validation = 0;
2098
2099 read_lock(&em_tree->lock);
2100 em = lookup_extent_mapping(em_tree, start, failrec->len);
2101 if (!em) {
2102 read_unlock(&em_tree->lock);
2103 kfree(failrec);
2104 return -EIO;
2105 }
2106
2107 if (em->start > start || em->start + em->len < start) {
2108 free_extent_map(em);
2109 em = NULL;
2110 }
2111 read_unlock(&em_tree->lock);
2112
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002113 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002114 kfree(failrec);
2115 return -EIO;
2116 }
2117 logical = start - em->start;
2118 logical = em->block_start + logical;
2119 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2120 logical = em->block_start;
2121 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2122 extent_set_compress_type(&failrec->bio_flags,
2123 em->compress_type);
2124 }
2125 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2126 "len=%llu\n", logical, start, failrec->len);
2127 failrec->logical = logical;
2128 free_extent_map(em);
2129
2130 /* set the bits in the private failure tree */
2131 ret = set_extent_bits(failure_tree, start, end,
2132 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2133 if (ret >= 0)
2134 ret = set_state_private(failure_tree, start,
2135 (u64)(unsigned long)failrec);
2136 /* set the bits in the inode's tree */
2137 if (ret >= 0)
2138 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2139 GFP_NOFS);
2140 if (ret < 0) {
2141 kfree(failrec);
2142 return ret;
2143 }
2144 } else {
2145 failrec = (struct io_failure_record *)(unsigned long)private;
2146 pr_debug("bio_readpage_error: (found) logical=%llu, "
2147 "start=%llu, len=%llu, validation=%d\n",
2148 failrec->logical, failrec->start, failrec->len,
2149 failrec->in_validation);
2150 /*
2151 * when data can be on disk more than twice, add to failrec here
2152 * (e.g. with a list for failed_mirror) to make
2153 * clean_io_failure() clean all those errors at once.
2154 */
2155 }
2156 num_copies = btrfs_num_copies(
2157 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2158 failrec->logical, failrec->len);
2159 if (num_copies == 1) {
2160 /*
2161 * we only have a single copy of the data, so don't bother with
2162 * all the retry and error correction code that follows. no
2163 * matter what the error is, it is very likely to persist.
2164 */
2165 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2166 "state=%p, num_copies=%d, next_mirror %d, "
2167 "failed_mirror %d\n", state, num_copies,
2168 failrec->this_mirror, failed_mirror);
2169 free_io_failure(inode, failrec, 0);
2170 return -EIO;
2171 }
2172
2173 if (!state) {
2174 spin_lock(&tree->lock);
2175 state = find_first_extent_bit_state(tree, failrec->start,
2176 EXTENT_LOCKED);
2177 if (state && state->start != failrec->start)
2178 state = NULL;
2179 spin_unlock(&tree->lock);
2180 }
2181
2182 /*
2183 * there are two premises:
2184 * a) deliver good data to the caller
2185 * b) correct the bad sectors on disk
2186 */
2187 if (failed_bio->bi_vcnt > 1) {
2188 /*
2189 * to fulfill b), we need to know the exact failing sectors, as
2190 * we don't want to rewrite any more than the failed ones. thus,
2191 * we need separate read requests for the failed bio
2192 *
2193 * if the following BUG_ON triggers, our validation request got
2194 * merged. we need separate requests for our algorithm to work.
2195 */
2196 BUG_ON(failrec->in_validation);
2197 failrec->in_validation = 1;
2198 failrec->this_mirror = failed_mirror;
2199 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2200 } else {
2201 /*
2202 * we're ready to fulfill a) and b) alongside. get a good copy
2203 * of the failed sector and if we succeed, we have setup
2204 * everything for repair_io_failure to do the rest for us.
2205 */
2206 if (failrec->in_validation) {
2207 BUG_ON(failrec->this_mirror != failed_mirror);
2208 failrec->in_validation = 0;
2209 failrec->this_mirror = 0;
2210 }
2211 failrec->failed_mirror = failed_mirror;
2212 failrec->this_mirror++;
2213 if (failrec->this_mirror == failed_mirror)
2214 failrec->this_mirror++;
2215 read_mode = READ_SYNC;
2216 }
2217
2218 if (!state || failrec->this_mirror > num_copies) {
2219 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2220 "next_mirror %d, failed_mirror %d\n", state,
2221 num_copies, failrec->this_mirror, failed_mirror);
2222 free_io_failure(inode, failrec, 0);
2223 return -EIO;
2224 }
2225
2226 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002227 if (!bio) {
2228 free_io_failure(inode, failrec, 0);
2229 return -EIO;
2230 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002231 bio->bi_private = state;
2232 bio->bi_end_io = failed_bio->bi_end_io;
2233 bio->bi_sector = failrec->logical >> 9;
2234 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2235 bio->bi_size = 0;
2236
2237 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2238
2239 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2240 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2241 failrec->this_mirror, num_copies, failrec->in_validation);
2242
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002243 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2244 failrec->this_mirror,
2245 failrec->bio_flags, 0);
2246 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002247}
2248
Chris Masond1310b22008-01-24 16:13:08 -05002249/* lots and lots of room for performance fixes in the end_bio funcs */
2250
Jeff Mahoney87826df2012-02-15 16:23:57 +01002251int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2252{
2253 int uptodate = (err == 0);
2254 struct extent_io_tree *tree;
2255 int ret;
2256
2257 tree = &BTRFS_I(page->mapping->host)->io_tree;
2258
2259 if (tree->ops && tree->ops->writepage_end_io_hook) {
2260 ret = tree->ops->writepage_end_io_hook(page, start,
2261 end, NULL, uptodate);
2262 if (ret)
2263 uptodate = 0;
2264 }
2265
Jeff Mahoney87826df2012-02-15 16:23:57 +01002266 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002267 ClearPageUptodate(page);
2268 SetPageError(page);
2269 }
2270 return 0;
2271}
2272
Chris Masond1310b22008-01-24 16:13:08 -05002273/*
2274 * after a writepage IO is done, we need to:
2275 * clear the uptodate bits on error
2276 * clear the writeback bits in the extent tree for this IO
2277 * end_page_writeback if the page has no more pending IO
2278 *
2279 * Scheduling is not allowed, so the extent state tree is expected
2280 * to have one and only one object corresponding to this IO.
2281 */
Chris Masond1310b22008-01-24 16:13:08 -05002282static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002283{
Chris Masond1310b22008-01-24 16:13:08 -05002284 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002285 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002286 u64 start;
2287 u64 end;
2288 int whole_page;
2289
Chris Masond1310b22008-01-24 16:13:08 -05002290 do {
2291 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002292 tree = &BTRFS_I(page->mapping->host)->io_tree;
2293
Chris Masond1310b22008-01-24 16:13:08 -05002294 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2295 bvec->bv_offset;
2296 end = start + bvec->bv_len - 1;
2297
2298 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2299 whole_page = 1;
2300 else
2301 whole_page = 0;
2302
2303 if (--bvec >= bio->bi_io_vec)
2304 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002305
Jeff Mahoney87826df2012-02-15 16:23:57 +01002306 if (end_extent_writepage(page, err, start, end))
2307 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002308
Chris Masond1310b22008-01-24 16:13:08 -05002309 if (whole_page)
2310 end_page_writeback(page);
2311 else
2312 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002313 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002314
Chris Masond1310b22008-01-24 16:13:08 -05002315 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002316}
2317
2318/*
2319 * after a readpage IO is done, we need to:
2320 * clear the uptodate bits on error
2321 * set the uptodate bits if things worked
2322 * set the page up to date if all extents in the tree are uptodate
2323 * clear the lock bit in the extent tree
2324 * unlock the page if there are no other extents locked for it
2325 *
2326 * Scheduling is not allowed, so the extent state tree is expected
2327 * to have one and only one object corresponding to this IO.
2328 */
Chris Masond1310b22008-01-24 16:13:08 -05002329static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002330{
2331 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002332 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2333 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002334 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002335 u64 start;
2336 u64 end;
2337 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002338 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002339 int ret;
2340
Chris Masond20f7042008-12-08 16:58:54 -05002341 if (err)
2342 uptodate = 0;
2343
Chris Masond1310b22008-01-24 16:13:08 -05002344 do {
2345 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002346 struct extent_state *cached = NULL;
2347 struct extent_state *state;
2348
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002349 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2350 "mirror=%ld\n", (u64)bio->bi_sector, err,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002352 tree = &BTRFS_I(page->mapping->host)->io_tree;
2353
Chris Masond1310b22008-01-24 16:13:08 -05002354 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2355 bvec->bv_offset;
2356 end = start + bvec->bv_len - 1;
2357
2358 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2359 whole_page = 1;
2360 else
2361 whole_page = 0;
2362
Chris Mason4125bf72010-02-03 18:18:45 +00002363 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002364 prefetchw(&bvec->bv_page->flags);
2365
Arne Jansen507903b2011-04-06 10:02:20 +00002366 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002367 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002368 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002369 /*
2370 * take a reference on the state, unlock will drop
2371 * the ref
2372 */
2373 cache_state(state, &cached);
2374 }
2375 spin_unlock(&tree->lock);
2376
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002377 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002378 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002379 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002380 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002381 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002382 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002383 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002384 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002385 }
Josef Bacikea466792012-03-26 21:57:36 -04002386
Josef Bacikea466792012-03-26 21:57:36 -04002387 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002388 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002389 if (!ret && !err &&
2390 test_bit(BIO_UPTODATE, &bio->bi_flags))
2391 uptodate = 1;
2392 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002393 /*
2394 * The generic bio_readpage_error handles errors the
2395 * following way: If possible, new read requests are
2396 * created and submitted and will end up in
2397 * end_bio_extent_readpage as well (if we're lucky, not
2398 * in the !uptodate case). In that case it returns 0 and
2399 * we just go on with the next page in our bio. If it
2400 * can't handle the error it will return -EIO and we
2401 * remain responsible for that page.
2402 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002403 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002404 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002405 uptodate =
2406 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002407 if (err)
2408 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002409 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002410 continue;
2411 }
2412 }
Chris Mason70dec802008-01-29 09:59:12 -05002413
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002414 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002415 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002416 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002417 }
Arne Jansen507903b2011-04-06 10:02:20 +00002418 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002419
Chris Mason70dec802008-01-29 09:59:12 -05002420 if (whole_page) {
2421 if (uptodate) {
2422 SetPageUptodate(page);
2423 } else {
2424 ClearPageUptodate(page);
2425 SetPageError(page);
2426 }
Chris Masond1310b22008-01-24 16:13:08 -05002427 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002428 } else {
2429 if (uptodate) {
2430 check_page_uptodate(tree, page);
2431 } else {
2432 ClearPageUptodate(page);
2433 SetPageError(page);
2434 }
Chris Masond1310b22008-01-24 16:13:08 -05002435 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002436 }
Chris Mason4125bf72010-02-03 18:18:45 +00002437 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002438
2439 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002440}
2441
Miao Xie88f794e2010-11-22 03:02:55 +00002442struct bio *
2443btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2444 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002445{
2446 struct bio *bio;
2447
2448 bio = bio_alloc(gfp_flags, nr_vecs);
2449
2450 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2451 while (!bio && (nr_vecs /= 2))
2452 bio = bio_alloc(gfp_flags, nr_vecs);
2453 }
2454
2455 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002456 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002457 bio->bi_bdev = bdev;
2458 bio->bi_sector = first_sector;
2459 }
2460 return bio;
2461}
2462
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002463/*
2464 * Since writes are async, they will only return -ENOMEM.
2465 * Reads can return the full range of I/O error conditions.
2466 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002467static int __must_check submit_one_bio(int rw, struct bio *bio,
2468 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002469{
Chris Masond1310b22008-01-24 16:13:08 -05002470 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002471 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2472 struct page *page = bvec->bv_page;
2473 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002474 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002475
2476 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002477
David Woodhouse902b22f2008-08-20 08:51:49 -04002478 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002479
2480 bio_get(bio);
2481
Chris Mason065631f2008-02-20 12:07:25 -05002482 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002483 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002484 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002485 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002486 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002487
Chris Masond1310b22008-01-24 16:13:08 -05002488 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2489 ret = -EOPNOTSUPP;
2490 bio_put(bio);
2491 return ret;
2492}
2493
Jeff Mahoney3444a972011-10-03 23:23:13 -04002494static int merge_bio(struct extent_io_tree *tree, struct page *page,
2495 unsigned long offset, size_t size, struct bio *bio,
2496 unsigned long bio_flags)
2497{
2498 int ret = 0;
2499 if (tree->ops && tree->ops->merge_bio_hook)
2500 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2501 bio_flags);
2502 BUG_ON(ret < 0);
2503 return ret;
2504
2505}
2506
Chris Masond1310b22008-01-24 16:13:08 -05002507static int submit_extent_page(int rw, struct extent_io_tree *tree,
2508 struct page *page, sector_t sector,
2509 size_t size, unsigned long offset,
2510 struct block_device *bdev,
2511 struct bio **bio_ret,
2512 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002513 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002514 int mirror_num,
2515 unsigned long prev_bio_flags,
2516 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002517{
2518 int ret = 0;
2519 struct bio *bio;
2520 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002521 int contig = 0;
2522 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2523 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002524 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002525
2526 if (bio_ret && *bio_ret) {
2527 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002528 if (old_compressed)
2529 contig = bio->bi_sector == sector;
2530 else
2531 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2532 sector;
2533
2534 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002535 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002536 bio_add_page(bio, page, page_size, offset) < page_size) {
2537 ret = submit_one_bio(rw, bio, mirror_num,
2538 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002539 if (ret < 0)
2540 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002541 bio = NULL;
2542 } else {
2543 return 0;
2544 }
2545 }
Chris Masonc8b97812008-10-29 14:49:59 -04002546 if (this_compressed)
2547 nr = BIO_MAX_PAGES;
2548 else
2549 nr = bio_get_nr_vecs(bdev);
2550
Miao Xie88f794e2010-11-22 03:02:55 +00002551 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002552 if (!bio)
2553 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002554
Chris Masonc8b97812008-10-29 14:49:59 -04002555 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002556 bio->bi_end_io = end_io_func;
2557 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002558
Chris Masond3977122009-01-05 21:25:51 -05002559 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002560 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002561 else
Chris Masonc8b97812008-10-29 14:49:59 -04002562 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002563
2564 return ret;
2565}
2566
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002567void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2568{
2569 if (!PagePrivate(page)) {
2570 SetPagePrivate(page);
2571 page_cache_get(page);
2572 set_page_private(page, (unsigned long)eb);
2573 } else {
2574 WARN_ON(page->private != (unsigned long)eb);
2575 }
2576}
2577
Chris Masond1310b22008-01-24 16:13:08 -05002578void set_page_extent_mapped(struct page *page)
2579{
2580 if (!PagePrivate(page)) {
2581 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002582 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002583 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002584 }
2585}
2586
Chris Masond1310b22008-01-24 16:13:08 -05002587/*
2588 * basic readpage implementation. Locked extent state structs are inserted
2589 * into the tree that are removed when the IO is done (by the end_io
2590 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002591 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002592 */
2593static int __extent_read_full_page(struct extent_io_tree *tree,
2594 struct page *page,
2595 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002596 struct bio **bio, int mirror_num,
2597 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002598{
2599 struct inode *inode = page->mapping->host;
2600 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2601 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2602 u64 end;
2603 u64 cur = start;
2604 u64 extent_offset;
2605 u64 last_byte = i_size_read(inode);
2606 u64 block_start;
2607 u64 cur_end;
2608 sector_t sector;
2609 struct extent_map *em;
2610 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002611 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002612 int ret;
2613 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002614 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002615 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002616 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002617 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002618 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002619
2620 set_page_extent_mapped(page);
2621
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002622 if (!PageUptodate(page)) {
2623 if (cleancache_get_page(page) == 0) {
2624 BUG_ON(blocksize != PAGE_SIZE);
2625 goto out;
2626 }
2627 }
2628
Chris Masond1310b22008-01-24 16:13:08 -05002629 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002630 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002631 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002632 ordered = btrfs_lookup_ordered_extent(inode, start);
2633 if (!ordered)
2634 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002635 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002636 btrfs_start_ordered_extent(inode, ordered, 1);
2637 btrfs_put_ordered_extent(ordered);
2638 }
Chris Masond1310b22008-01-24 16:13:08 -05002639
Chris Masonc8b97812008-10-29 14:49:59 -04002640 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2641 char *userpage;
2642 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2643
2644 if (zero_offset) {
2645 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002646 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002647 memset(userpage + zero_offset, 0, iosize);
2648 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002649 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002650 }
2651 }
Chris Masond1310b22008-01-24 16:13:08 -05002652 while (cur <= end) {
2653 if (cur >= last_byte) {
2654 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002655 struct extent_state *cached = NULL;
2656
David Sterba306e16c2011-04-19 14:29:38 +02002657 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002658 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002659 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002660 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002661 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002662 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002663 &cached, GFP_NOFS);
2664 unlock_extent_cached(tree, cur, cur + iosize - 1,
2665 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002666 break;
2667 }
David Sterba306e16c2011-04-19 14:29:38 +02002668 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002669 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002670 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002671 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002672 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002673 break;
2674 }
Chris Masond1310b22008-01-24 16:13:08 -05002675 extent_offset = cur - em->start;
2676 BUG_ON(extent_map_end(em) <= cur);
2677 BUG_ON(end < cur);
2678
Li Zefan261507a02010-12-17 14:21:50 +08002679 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002680 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002681 extent_set_compress_type(&this_bio_flag,
2682 em->compress_type);
2683 }
Chris Masonc8b97812008-10-29 14:49:59 -04002684
Chris Masond1310b22008-01-24 16:13:08 -05002685 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2686 cur_end = min(extent_map_end(em) - 1, end);
2687 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002688 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2689 disk_io_size = em->block_len;
2690 sector = em->block_start >> 9;
2691 } else {
2692 sector = (em->block_start + extent_offset) >> 9;
2693 disk_io_size = iosize;
2694 }
Chris Masond1310b22008-01-24 16:13:08 -05002695 bdev = em->bdev;
2696 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002697 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2698 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002699 free_extent_map(em);
2700 em = NULL;
2701
2702 /* we've found a hole, just zero and go on */
2703 if (block_start == EXTENT_MAP_HOLE) {
2704 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002705 struct extent_state *cached = NULL;
2706
Cong Wang7ac687d2011-11-25 23:14:28 +08002707 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002708 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002709 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002710 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002711
2712 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002713 &cached, GFP_NOFS);
2714 unlock_extent_cached(tree, cur, cur + iosize - 1,
2715 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002716 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002717 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002718 continue;
2719 }
2720 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002721 if (test_range_bit(tree, cur, cur_end,
2722 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002723 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002724 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002725 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002726 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002727 continue;
2728 }
Chris Mason70dec802008-01-29 09:59:12 -05002729 /* we have an inline extent but it didn't get marked up
2730 * to date. Error out
2731 */
2732 if (block_start == EXTENT_MAP_INLINE) {
2733 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002734 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002735 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002736 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002737 continue;
2738 }
Chris Masond1310b22008-01-24 16:13:08 -05002739
2740 ret = 0;
2741 if (tree->ops && tree->ops->readpage_io_hook) {
2742 ret = tree->ops->readpage_io_hook(page, cur,
2743 cur + iosize - 1);
2744 }
2745 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002746 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2747 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002748 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002749 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002750 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002751 end_bio_extent_readpage, mirror_num,
2752 *bio_flags,
2753 this_bio_flag);
Josef Bacikedd33c92012-10-05 16:40:32 -04002754 if (!ret) {
2755 nr++;
2756 *bio_flags = this_bio_flag;
2757 }
Chris Masond1310b22008-01-24 16:13:08 -05002758 }
Josef Bacikedd33c92012-10-05 16:40:32 -04002759 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002760 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002761 unlock_extent(tree, cur, cur + iosize - 1);
2762 }
Chris Masond1310b22008-01-24 16:13:08 -05002763 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002764 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002765 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002766out:
Chris Masond1310b22008-01-24 16:13:08 -05002767 if (!nr) {
2768 if (!PageError(page))
2769 SetPageUptodate(page);
2770 unlock_page(page);
2771 }
2772 return 0;
2773}
2774
2775int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002776 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002777{
2778 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002779 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002780 int ret;
2781
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002782 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002783 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002784 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002785 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002786 return ret;
2787}
Chris Masond1310b22008-01-24 16:13:08 -05002788
Chris Mason11c83492009-04-20 15:50:09 -04002789static noinline void update_nr_written(struct page *page,
2790 struct writeback_control *wbc,
2791 unsigned long nr_written)
2792{
2793 wbc->nr_to_write -= nr_written;
2794 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2795 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2796 page->mapping->writeback_index = page->index + nr_written;
2797}
2798
Chris Masond1310b22008-01-24 16:13:08 -05002799/*
2800 * the writepage semantics are similar to regular writepage. extent
2801 * records are inserted to lock ranges in the tree, and as dirty areas
2802 * are found, they are marked writeback. Then the lock bits are removed
2803 * and the end_io handler clears the writeback ranges
2804 */
2805static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2806 void *data)
2807{
2808 struct inode *inode = page->mapping->host;
2809 struct extent_page_data *epd = data;
2810 struct extent_io_tree *tree = epd->tree;
2811 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2812 u64 delalloc_start;
2813 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2814 u64 end;
2815 u64 cur = start;
2816 u64 extent_offset;
2817 u64 last_byte = i_size_read(inode);
2818 u64 block_start;
2819 u64 iosize;
2820 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002821 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002822 struct extent_map *em;
2823 struct block_device *bdev;
2824 int ret;
2825 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002826 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002827 size_t blocksize;
2828 loff_t i_size = i_size_read(inode);
2829 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2830 u64 nr_delalloc;
2831 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002832 int page_started;
2833 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002834 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002835 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002836 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002837
Chris Masonffbd5172009-04-20 15:50:09 -04002838 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002839 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002840 else
2841 write_flags = WRITE;
2842
liubo1abe9b82011-03-24 11:18:59 +00002843 trace___extent_writepage(page, inode, wbc);
2844
Chris Masond1310b22008-01-24 16:13:08 -05002845 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002846
2847 ClearPageError(page);
2848
Chris Mason7f3c74f2008-07-18 12:01:11 -04002849 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002850 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002851 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002852 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002853 unlock_page(page);
2854 return 0;
2855 }
2856
2857 if (page->index == end_index) {
2858 char *userpage;
2859
Cong Wang7ac687d2011-11-25 23:14:28 +08002860 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002861 memset(userpage + pg_offset, 0,
2862 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002863 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002864 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002865 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002866 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002867
2868 set_page_extent_mapped(page);
2869
Josef Bacik9e487102011-08-01 12:08:18 -04002870 if (!tree->ops || !tree->ops->fill_delalloc)
2871 fill_delalloc = false;
2872
Chris Masond1310b22008-01-24 16:13:08 -05002873 delalloc_start = start;
2874 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002875 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002876 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002877 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002878 /*
2879 * make sure the wbc mapping index is at least updated
2880 * to this page.
2881 */
2882 update_nr_written(page, wbc, 0);
2883
Chris Masond3977122009-01-05 21:25:51 -05002884 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002885 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002886 page,
2887 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002888 &delalloc_end,
2889 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002890 if (nr_delalloc == 0) {
2891 delalloc_start = delalloc_end + 1;
2892 continue;
2893 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002894 ret = tree->ops->fill_delalloc(inode, page,
2895 delalloc_start,
2896 delalloc_end,
2897 &page_started,
2898 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 /* File system has been set read-only */
2900 if (ret) {
2901 SetPageError(page);
2902 goto done;
2903 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002904 /*
2905 * delalloc_end is already one less than the total
2906 * length, so we don't subtract one from
2907 * PAGE_CACHE_SIZE
2908 */
2909 delalloc_to_write += (delalloc_end - delalloc_start +
2910 PAGE_CACHE_SIZE) >>
2911 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002912 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002913 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002914 if (wbc->nr_to_write < delalloc_to_write) {
2915 int thresh = 8192;
2916
2917 if (delalloc_to_write < thresh * 2)
2918 thresh = delalloc_to_write;
2919 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2920 thresh);
2921 }
Chris Masonc8b97812008-10-29 14:49:59 -04002922
Chris Mason771ed682008-11-06 22:02:51 -05002923 /* did the fill delalloc function already unlock and start
2924 * the IO?
2925 */
2926 if (page_started) {
2927 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002928 /*
2929 * we've unlocked the page, so we can't update
2930 * the mapping's writeback index, just update
2931 * nr_to_write.
2932 */
2933 wbc->nr_to_write -= nr_written;
2934 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002935 }
Chris Masonc8b97812008-10-29 14:49:59 -04002936 }
Chris Mason247e7432008-07-17 12:53:51 -04002937 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002938 ret = tree->ops->writepage_start_hook(page, start,
2939 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002940 if (ret) {
2941 /* Fixup worker will requeue */
2942 if (ret == -EBUSY)
2943 wbc->pages_skipped++;
2944 else
2945 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002946 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002947 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002948 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002949 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002950 }
2951 }
2952
Chris Mason11c83492009-04-20 15:50:09 -04002953 /*
2954 * we don't want to touch the inode after unlocking the page,
2955 * so we update the mapping writeback index now
2956 */
2957 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002958
Chris Masond1310b22008-01-24 16:13:08 -05002959 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002960 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961 if (tree->ops && tree->ops->writepage_end_io_hook)
2962 tree->ops->writepage_end_io_hook(page, start,
2963 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002964 goto done;
2965 }
2966
Chris Masond1310b22008-01-24 16:13:08 -05002967 blocksize = inode->i_sb->s_blocksize;
2968
2969 while (cur <= end) {
2970 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002971 if (tree->ops && tree->ops->writepage_end_io_hook)
2972 tree->ops->writepage_end_io_hook(page, cur,
2973 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002974 break;
2975 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002976 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002977 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002978 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002979 SetPageError(page);
2980 break;
2981 }
2982
2983 extent_offset = cur - em->start;
2984 BUG_ON(extent_map_end(em) <= cur);
2985 BUG_ON(end < cur);
2986 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2987 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2988 sector = (em->block_start + extent_offset) >> 9;
2989 bdev = em->bdev;
2990 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002991 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002992 free_extent_map(em);
2993 em = NULL;
2994
Chris Masonc8b97812008-10-29 14:49:59 -04002995 /*
2996 * compressed and inline extents are written through other
2997 * paths in the FS
2998 */
2999 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003000 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003001 /*
3002 * end_io notification does not happen here for
3003 * compressed extents
3004 */
3005 if (!compressed && tree->ops &&
3006 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003007 tree->ops->writepage_end_io_hook(page, cur,
3008 cur + iosize - 1,
3009 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003010 else if (compressed) {
3011 /* we don't want to end_page_writeback on
3012 * a compressed extent. this happens
3013 * elsewhere
3014 */
3015 nr++;
3016 }
3017
3018 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003019 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003020 continue;
3021 }
Chris Masond1310b22008-01-24 16:13:08 -05003022 /* leave this out until we have a page_mkwrite call */
3023 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003024 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003025 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003026 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003027 continue;
3028 }
Chris Masonc8b97812008-10-29 14:49:59 -04003029
Chris Masond1310b22008-01-24 16:13:08 -05003030 if (tree->ops && tree->ops->writepage_io_hook) {
3031 ret = tree->ops->writepage_io_hook(page, cur,
3032 cur + iosize - 1);
3033 } else {
3034 ret = 0;
3035 }
Chris Mason1259ab72008-05-12 13:39:03 -04003036 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003037 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003038 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003039 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003040
Chris Masond1310b22008-01-24 16:13:08 -05003041 set_range_writeback(tree, cur, cur + iosize - 1);
3042 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003043 printk(KERN_ERR "btrfs warning page %lu not "
3044 "writeback, cur %llu end %llu\n",
3045 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003046 (unsigned long long)end);
3047 }
3048
Chris Masonffbd5172009-04-20 15:50:09 -04003049 ret = submit_extent_page(write_flags, tree, page,
3050 sector, iosize, pg_offset,
3051 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003052 end_bio_extent_writepage,
3053 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003054 if (ret)
3055 SetPageError(page);
3056 }
3057 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003058 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003059 nr++;
3060 }
3061done:
3062 if (nr == 0) {
3063 /* make sure the mapping tag for page dirty gets cleared */
3064 set_page_writeback(page);
3065 end_page_writeback(page);
3066 }
Chris Masond1310b22008-01-24 16:13:08 -05003067 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003068
Chris Mason11c83492009-04-20 15:50:09 -04003069done_unlocked:
3070
Chris Mason2c64c532009-09-02 15:04:12 -04003071 /* drop our reference on any cached states */
3072 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003073 return 0;
3074}
3075
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003076static int eb_wait(void *word)
3077{
3078 io_schedule();
3079 return 0;
3080}
3081
3082static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3083{
3084 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3085 TASK_UNINTERRUPTIBLE);
3086}
3087
3088static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3089 struct btrfs_fs_info *fs_info,
3090 struct extent_page_data *epd)
3091{
3092 unsigned long i, num_pages;
3093 int flush = 0;
3094 int ret = 0;
3095
3096 if (!btrfs_try_tree_write_lock(eb)) {
3097 flush = 1;
3098 flush_write_bio(epd);
3099 btrfs_tree_lock(eb);
3100 }
3101
3102 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3103 btrfs_tree_unlock(eb);
3104 if (!epd->sync_io)
3105 return 0;
3106 if (!flush) {
3107 flush_write_bio(epd);
3108 flush = 1;
3109 }
Chris Masona098d8e2012-03-21 12:09:56 -04003110 while (1) {
3111 wait_on_extent_buffer_writeback(eb);
3112 btrfs_tree_lock(eb);
3113 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3114 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003115 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003116 }
3117 }
3118
Josef Bacik51561ff2012-07-20 16:25:24 -04003119 /*
3120 * We need to do this to prevent races in people who check if the eb is
3121 * under IO since we can end up having no IO bits set for a short period
3122 * of time.
3123 */
3124 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003125 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3126 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003127 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003128 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3129 spin_lock(&fs_info->delalloc_lock);
3130 if (fs_info->dirty_metadata_bytes >= eb->len)
3131 fs_info->dirty_metadata_bytes -= eb->len;
3132 else
3133 WARN_ON(1);
3134 spin_unlock(&fs_info->delalloc_lock);
3135 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003136 } else {
3137 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003138 }
3139
3140 btrfs_tree_unlock(eb);
3141
3142 if (!ret)
3143 return ret;
3144
3145 num_pages = num_extent_pages(eb->start, eb->len);
3146 for (i = 0; i < num_pages; i++) {
3147 struct page *p = extent_buffer_page(eb, i);
3148
3149 if (!trylock_page(p)) {
3150 if (!flush) {
3151 flush_write_bio(epd);
3152 flush = 1;
3153 }
3154 lock_page(p);
3155 }
3156 }
3157
3158 return ret;
3159}
3160
3161static void end_extent_buffer_writeback(struct extent_buffer *eb)
3162{
3163 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3164 smp_mb__after_clear_bit();
3165 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3166}
3167
3168static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3169{
3170 int uptodate = err == 0;
3171 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3172 struct extent_buffer *eb;
3173 int done;
3174
3175 do {
3176 struct page *page = bvec->bv_page;
3177
3178 bvec--;
3179 eb = (struct extent_buffer *)page->private;
3180 BUG_ON(!eb);
3181 done = atomic_dec_and_test(&eb->io_pages);
3182
3183 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3184 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3185 ClearPageUptodate(page);
3186 SetPageError(page);
3187 }
3188
3189 end_page_writeback(page);
3190
3191 if (!done)
3192 continue;
3193
3194 end_extent_buffer_writeback(eb);
3195 } while (bvec >= bio->bi_io_vec);
3196
3197 bio_put(bio);
3198
3199}
3200
3201static int write_one_eb(struct extent_buffer *eb,
3202 struct btrfs_fs_info *fs_info,
3203 struct writeback_control *wbc,
3204 struct extent_page_data *epd)
3205{
3206 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3207 u64 offset = eb->start;
3208 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003209 unsigned long bio_flags = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003210 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003211 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003212
3213 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3214 num_pages = num_extent_pages(eb->start, eb->len);
3215 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003216 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3217 bio_flags = EXTENT_BIO_TREE_LOG;
3218
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003219 for (i = 0; i < num_pages; i++) {
3220 struct page *p = extent_buffer_page(eb, i);
3221
3222 clear_page_dirty_for_io(p);
3223 set_page_writeback(p);
3224 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3225 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3226 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003227 0, epd->bio_flags, bio_flags);
3228 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003229 if (ret) {
3230 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3231 SetPageError(p);
3232 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3233 end_extent_buffer_writeback(eb);
3234 ret = -EIO;
3235 break;
3236 }
3237 offset += PAGE_CACHE_SIZE;
3238 update_nr_written(p, wbc, 1);
3239 unlock_page(p);
3240 }
3241
3242 if (unlikely(ret)) {
3243 for (; i < num_pages; i++) {
3244 struct page *p = extent_buffer_page(eb, i);
3245 unlock_page(p);
3246 }
3247 }
3248
3249 return ret;
3250}
3251
3252int btree_write_cache_pages(struct address_space *mapping,
3253 struct writeback_control *wbc)
3254{
3255 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3256 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3257 struct extent_buffer *eb, *prev_eb = NULL;
3258 struct extent_page_data epd = {
3259 .bio = NULL,
3260 .tree = tree,
3261 .extent_locked = 0,
3262 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003263 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003264 };
3265 int ret = 0;
3266 int done = 0;
3267 int nr_to_write_done = 0;
3268 struct pagevec pvec;
3269 int nr_pages;
3270 pgoff_t index;
3271 pgoff_t end; /* Inclusive */
3272 int scanned = 0;
3273 int tag;
3274
3275 pagevec_init(&pvec, 0);
3276 if (wbc->range_cyclic) {
3277 index = mapping->writeback_index; /* Start from prev offset */
3278 end = -1;
3279 } else {
3280 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3281 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3282 scanned = 1;
3283 }
3284 if (wbc->sync_mode == WB_SYNC_ALL)
3285 tag = PAGECACHE_TAG_TOWRITE;
3286 else
3287 tag = PAGECACHE_TAG_DIRTY;
3288retry:
3289 if (wbc->sync_mode == WB_SYNC_ALL)
3290 tag_pages_for_writeback(mapping, index, end);
3291 while (!done && !nr_to_write_done && (index <= end) &&
3292 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3293 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3294 unsigned i;
3295
3296 scanned = 1;
3297 for (i = 0; i < nr_pages; i++) {
3298 struct page *page = pvec.pages[i];
3299
3300 if (!PagePrivate(page))
3301 continue;
3302
3303 if (!wbc->range_cyclic && page->index > end) {
3304 done = 1;
3305 break;
3306 }
3307
Josef Bacikb5bae262012-09-14 13:43:01 -04003308 spin_lock(&mapping->private_lock);
3309 if (!PagePrivate(page)) {
3310 spin_unlock(&mapping->private_lock);
3311 continue;
3312 }
3313
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003314 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003315
3316 /*
3317 * Shouldn't happen and normally this would be a BUG_ON
3318 * but no sense in crashing the users box for something
3319 * we can survive anyway.
3320 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003321 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003322 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003323 WARN_ON(1);
3324 continue;
3325 }
3326
Josef Bacikb5bae262012-09-14 13:43:01 -04003327 if (eb == prev_eb) {
3328 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003329 continue;
3330 }
3331
Josef Bacikb5bae262012-09-14 13:43:01 -04003332 ret = atomic_inc_not_zero(&eb->refs);
3333 spin_unlock(&mapping->private_lock);
3334 if (!ret)
3335 continue;
3336
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003337 prev_eb = eb;
3338 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3339 if (!ret) {
3340 free_extent_buffer(eb);
3341 continue;
3342 }
3343
3344 ret = write_one_eb(eb, fs_info, wbc, &epd);
3345 if (ret) {
3346 done = 1;
3347 free_extent_buffer(eb);
3348 break;
3349 }
3350 free_extent_buffer(eb);
3351
3352 /*
3353 * the filesystem may choose to bump up nr_to_write.
3354 * We have to make sure to honor the new nr_to_write
3355 * at any time
3356 */
3357 nr_to_write_done = wbc->nr_to_write <= 0;
3358 }
3359 pagevec_release(&pvec);
3360 cond_resched();
3361 }
3362 if (!scanned && !done) {
3363 /*
3364 * We hit the last page and there is more work to be done: wrap
3365 * back to the start of the file
3366 */
3367 scanned = 1;
3368 index = 0;
3369 goto retry;
3370 }
3371 flush_write_bio(&epd);
3372 return ret;
3373}
3374
Chris Masond1310b22008-01-24 16:13:08 -05003375/**
Chris Mason4bef0842008-09-08 11:18:08 -04003376 * 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 -05003377 * @mapping: address space structure to write
3378 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3379 * @writepage: function called for each page
3380 * @data: data passed to writepage function
3381 *
3382 * If a page is already under I/O, write_cache_pages() skips it, even
3383 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3384 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3385 * and msync() need to guarantee that all the data which was dirty at the time
3386 * the call was made get new I/O started against them. If wbc->sync_mode is
3387 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3388 * existing IO to complete.
3389 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003390static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003391 struct address_space *mapping,
3392 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003393 writepage_t writepage, void *data,
3394 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003395{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003396 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003397 int ret = 0;
3398 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003399 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003400 struct pagevec pvec;
3401 int nr_pages;
3402 pgoff_t index;
3403 pgoff_t end; /* Inclusive */
3404 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003405 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003406
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003407 /*
3408 * We have to hold onto the inode so that ordered extents can do their
3409 * work when the IO finishes. The alternative to this is failing to add
3410 * an ordered extent if the igrab() fails there and that is a huge pain
3411 * to deal with, so instead just hold onto the inode throughout the
3412 * writepages operation. If it fails here we are freeing up the inode
3413 * anyway and we'd rather not waste our time writing out stuff that is
3414 * going to be truncated anyway.
3415 */
3416 if (!igrab(inode))
3417 return 0;
3418
Chris Masond1310b22008-01-24 16:13:08 -05003419 pagevec_init(&pvec, 0);
3420 if (wbc->range_cyclic) {
3421 index = mapping->writeback_index; /* Start from prev offset */
3422 end = -1;
3423 } else {
3424 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3425 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003426 scanned = 1;
3427 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003428 if (wbc->sync_mode == WB_SYNC_ALL)
3429 tag = PAGECACHE_TAG_TOWRITE;
3430 else
3431 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003432retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003433 if (wbc->sync_mode == WB_SYNC_ALL)
3434 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003435 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003436 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3437 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003438 unsigned i;
3439
3440 scanned = 1;
3441 for (i = 0; i < nr_pages; i++) {
3442 struct page *page = pvec.pages[i];
3443
3444 /*
3445 * At this point we hold neither mapping->tree_lock nor
3446 * lock on the page itself: the page may be truncated or
3447 * invalidated (changing page->mapping to NULL), or even
3448 * swizzled back from swapper_space to tmpfs file
3449 * mapping
3450 */
Chris Mason01d658f2011-11-01 10:08:06 -04003451 if (tree->ops &&
3452 tree->ops->write_cache_pages_lock_hook) {
3453 tree->ops->write_cache_pages_lock_hook(page,
3454 data, flush_fn);
3455 } else {
3456 if (!trylock_page(page)) {
3457 flush_fn(data);
3458 lock_page(page);
3459 }
3460 }
Chris Masond1310b22008-01-24 16:13:08 -05003461
3462 if (unlikely(page->mapping != mapping)) {
3463 unlock_page(page);
3464 continue;
3465 }
3466
3467 if (!wbc->range_cyclic && page->index > end) {
3468 done = 1;
3469 unlock_page(page);
3470 continue;
3471 }
3472
Chris Masond2c3f4f2008-11-19 12:44:22 -05003473 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003474 if (PageWriteback(page))
3475 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003476 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003477 }
Chris Masond1310b22008-01-24 16:13:08 -05003478
3479 if (PageWriteback(page) ||
3480 !clear_page_dirty_for_io(page)) {
3481 unlock_page(page);
3482 continue;
3483 }
3484
3485 ret = (*writepage)(page, wbc, data);
3486
3487 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3488 unlock_page(page);
3489 ret = 0;
3490 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003491 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003492 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003493
3494 /*
3495 * the filesystem may choose to bump up nr_to_write.
3496 * We have to make sure to honor the new nr_to_write
3497 * at any time
3498 */
3499 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003500 }
3501 pagevec_release(&pvec);
3502 cond_resched();
3503 }
3504 if (!scanned && !done) {
3505 /*
3506 * We hit the last page and there is more work to be done: wrap
3507 * back to the start of the file
3508 */
3509 scanned = 1;
3510 index = 0;
3511 goto retry;
3512 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003513 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003514 return ret;
3515}
Chris Masond1310b22008-01-24 16:13:08 -05003516
Chris Masonffbd5172009-04-20 15:50:09 -04003517static void flush_epd_write_bio(struct extent_page_data *epd)
3518{
3519 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003520 int rw = WRITE;
3521 int ret;
3522
Chris Masonffbd5172009-04-20 15:50:09 -04003523 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003524 rw = WRITE_SYNC;
3525
Josef Bacikde0022b2012-09-25 14:25:58 -04003526 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003527 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003528 epd->bio = NULL;
3529 }
3530}
3531
Chris Masond2c3f4f2008-11-19 12:44:22 -05003532static noinline void flush_write_bio(void *data)
3533{
3534 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003535 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003536}
3537
Chris Masond1310b22008-01-24 16:13:08 -05003538int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3539 get_extent_t *get_extent,
3540 struct writeback_control *wbc)
3541{
3542 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003543 struct extent_page_data epd = {
3544 .bio = NULL,
3545 .tree = tree,
3546 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003547 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003548 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003549 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003550 };
Chris Masond1310b22008-01-24 16:13:08 -05003551
Chris Masond1310b22008-01-24 16:13:08 -05003552 ret = __extent_writepage(page, wbc, &epd);
3553
Chris Masonffbd5172009-04-20 15:50:09 -04003554 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003555 return ret;
3556}
Chris Masond1310b22008-01-24 16:13:08 -05003557
Chris Mason771ed682008-11-06 22:02:51 -05003558int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3559 u64 start, u64 end, get_extent_t *get_extent,
3560 int mode)
3561{
3562 int ret = 0;
3563 struct address_space *mapping = inode->i_mapping;
3564 struct page *page;
3565 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3566 PAGE_CACHE_SHIFT;
3567
3568 struct extent_page_data epd = {
3569 .bio = NULL,
3570 .tree = tree,
3571 .get_extent = get_extent,
3572 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003573 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003574 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003575 };
3576 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003577 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003578 .nr_to_write = nr_pages * 2,
3579 .range_start = start,
3580 .range_end = end + 1,
3581 };
3582
Chris Masond3977122009-01-05 21:25:51 -05003583 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003584 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3585 if (clear_page_dirty_for_io(page))
3586 ret = __extent_writepage(page, &wbc_writepages, &epd);
3587 else {
3588 if (tree->ops && tree->ops->writepage_end_io_hook)
3589 tree->ops->writepage_end_io_hook(page, start,
3590 start + PAGE_CACHE_SIZE - 1,
3591 NULL, 1);
3592 unlock_page(page);
3593 }
3594 page_cache_release(page);
3595 start += PAGE_CACHE_SIZE;
3596 }
3597
Chris Masonffbd5172009-04-20 15:50:09 -04003598 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003599 return ret;
3600}
Chris Masond1310b22008-01-24 16:13:08 -05003601
3602int extent_writepages(struct extent_io_tree *tree,
3603 struct address_space *mapping,
3604 get_extent_t *get_extent,
3605 struct writeback_control *wbc)
3606{
3607 int ret = 0;
3608 struct extent_page_data epd = {
3609 .bio = NULL,
3610 .tree = tree,
3611 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003612 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003613 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003614 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003615 };
3616
Chris Mason4bef0842008-09-08 11:18:08 -04003617 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003618 __extent_writepage, &epd,
3619 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003620 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003621 return ret;
3622}
Chris Masond1310b22008-01-24 16:13:08 -05003623
3624int extent_readpages(struct extent_io_tree *tree,
3625 struct address_space *mapping,
3626 struct list_head *pages, unsigned nr_pages,
3627 get_extent_t get_extent)
3628{
3629 struct bio *bio = NULL;
3630 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003631 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003632 struct page *pagepool[16];
3633 struct page *page;
3634 int i = 0;
3635 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003636
Chris Masond1310b22008-01-24 16:13:08 -05003637 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003638 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003639
3640 prefetchw(&page->flags);
3641 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003642 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003643 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003644 page_cache_release(page);
3645 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003646 }
Liu Bo67c96842012-07-20 21:43:09 -06003647
3648 pagepool[nr++] = page;
3649 if (nr < ARRAY_SIZE(pagepool))
3650 continue;
3651 for (i = 0; i < nr; i++) {
3652 __extent_read_full_page(tree, pagepool[i], get_extent,
3653 &bio, 0, &bio_flags);
3654 page_cache_release(pagepool[i]);
3655 }
3656 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003657 }
Liu Bo67c96842012-07-20 21:43:09 -06003658 for (i = 0; i < nr; i++) {
3659 __extent_read_full_page(tree, pagepool[i], get_extent,
3660 &bio, 0, &bio_flags);
3661 page_cache_release(pagepool[i]);
3662 }
3663
Chris Masond1310b22008-01-24 16:13:08 -05003664 BUG_ON(!list_empty(pages));
3665 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003666 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003667 return 0;
3668}
Chris Masond1310b22008-01-24 16:13:08 -05003669
3670/*
3671 * basic invalidatepage code, this waits on any locked or writeback
3672 * ranges corresponding to the page, and then deletes any extent state
3673 * records from the tree
3674 */
3675int extent_invalidatepage(struct extent_io_tree *tree,
3676 struct page *page, unsigned long offset)
3677{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003678 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003679 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3680 u64 end = start + PAGE_CACHE_SIZE - 1;
3681 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3682
Chris Masond3977122009-01-05 21:25:51 -05003683 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003684 if (start > end)
3685 return 0;
3686
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003687 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003688 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003689 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003690 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3691 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003692 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003693 return 0;
3694}
Chris Masond1310b22008-01-24 16:13:08 -05003695
3696/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003697 * a helper for releasepage, this tests for areas of the page that
3698 * are locked or under IO and drops the related state bits if it is safe
3699 * to drop the page.
3700 */
3701int try_release_extent_state(struct extent_map_tree *map,
3702 struct extent_io_tree *tree, struct page *page,
3703 gfp_t mask)
3704{
3705 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3706 u64 end = start + PAGE_CACHE_SIZE - 1;
3707 int ret = 1;
3708
Chris Mason211f90e2008-07-18 11:56:15 -04003709 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003710 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003711 ret = 0;
3712 else {
3713 if ((mask & GFP_NOFS) == GFP_NOFS)
3714 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003715 /*
3716 * at this point we can safely clear everything except the
3717 * locked bit and the nodatasum bit
3718 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003719 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003720 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3721 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003722
3723 /* if clear_extent_bit failed for enomem reasons,
3724 * we can't allow the release to continue.
3725 */
3726 if (ret < 0)
3727 ret = 0;
3728 else
3729 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003730 }
3731 return ret;
3732}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003733
3734/*
Chris Masond1310b22008-01-24 16:13:08 -05003735 * a helper for releasepage. As long as there are no locked extents
3736 * in the range corresponding to the page, both state records and extent
3737 * map records are removed
3738 */
3739int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003740 struct extent_io_tree *tree, struct page *page,
3741 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003742{
3743 struct extent_map *em;
3744 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3745 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003746
Chris Mason70dec802008-01-29 09:59:12 -05003747 if ((mask & __GFP_WAIT) &&
3748 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003749 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003750 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003751 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003752 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003753 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003754 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003755 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003756 break;
3757 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003758 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3759 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003760 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003761 free_extent_map(em);
3762 break;
3763 }
3764 if (!test_range_bit(tree, em->start,
3765 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003766 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003767 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003768 remove_extent_mapping(map, em);
3769 /* once for the rb tree */
3770 free_extent_map(em);
3771 }
3772 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003773 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003774
3775 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003776 free_extent_map(em);
3777 }
Chris Masond1310b22008-01-24 16:13:08 -05003778 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003779 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003780}
Chris Masond1310b22008-01-24 16:13:08 -05003781
Chris Masonec29ed52011-02-23 16:23:20 -05003782/*
3783 * helper function for fiemap, which doesn't want to see any holes.
3784 * This maps until we find something past 'last'
3785 */
3786static struct extent_map *get_extent_skip_holes(struct inode *inode,
3787 u64 offset,
3788 u64 last,
3789 get_extent_t *get_extent)
3790{
3791 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3792 struct extent_map *em;
3793 u64 len;
3794
3795 if (offset >= last)
3796 return NULL;
3797
3798 while(1) {
3799 len = last - offset;
3800 if (len == 0)
3801 break;
3802 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3803 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003804 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003805 return em;
3806
3807 /* if this isn't a hole return it */
3808 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3809 em->block_start != EXTENT_MAP_HOLE) {
3810 return em;
3811 }
3812
3813 /* this is a hole, advance to the next extent */
3814 offset = extent_map_end(em);
3815 free_extent_map(em);
3816 if (offset >= last)
3817 break;
3818 }
3819 return NULL;
3820}
3821
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003822int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3823 __u64 start, __u64 len, get_extent_t *get_extent)
3824{
Josef Bacik975f84f2010-11-23 19:36:57 +00003825 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003826 u64 off = start;
3827 u64 max = start + len;
3828 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003829 u32 found_type;
3830 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003831 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003832 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003833 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003834 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003835 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003836 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003837 struct btrfs_path *path;
3838 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003839 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003840 u64 em_start = 0;
3841 u64 em_len = 0;
3842 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003843 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003844
3845 if (len == 0)
3846 return -EINVAL;
3847
Josef Bacik975f84f2010-11-23 19:36:57 +00003848 path = btrfs_alloc_path();
3849 if (!path)
3850 return -ENOMEM;
3851 path->leave_spinning = 1;
3852
Josef Bacik4d479cf2011-11-17 11:34:31 -05003853 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3854 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3855
Chris Masonec29ed52011-02-23 16:23:20 -05003856 /*
3857 * lookup the last file extent. We're not using i_size here
3858 * because there might be preallocation past i_size
3859 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003860 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003861 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003862 if (ret < 0) {
3863 btrfs_free_path(path);
3864 return ret;
3865 }
3866 WARN_ON(!ret);
3867 path->slots[0]--;
3868 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3869 struct btrfs_file_extent_item);
3870 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3871 found_type = btrfs_key_type(&found_key);
3872
Chris Masonec29ed52011-02-23 16:23:20 -05003873 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003874 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003875 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003876 /* have to trust i_size as the end */
3877 last = (u64)-1;
3878 last_for_get_extent = isize;
3879 } else {
3880 /*
3881 * remember the start of the last extent. There are a
3882 * bunch of different factors that go into the length of the
3883 * extent, so its much less complex to remember where it started
3884 */
3885 last = found_key.offset;
3886 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003887 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003888 btrfs_free_path(path);
3889
Chris Masonec29ed52011-02-23 16:23:20 -05003890 /*
3891 * we might have some extents allocated but more delalloc past those
3892 * extents. so, we trust isize unless the start of the last extent is
3893 * beyond isize
3894 */
3895 if (last < isize) {
3896 last = (u64)-1;
3897 last_for_get_extent = isize;
3898 }
3899
Josef Bacik2ac55d42010-02-03 19:33:23 +00003900 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003901 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003902
Josef Bacik4d479cf2011-11-17 11:34:31 -05003903 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003904 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003905 if (!em)
3906 goto out;
3907 if (IS_ERR(em)) {
3908 ret = PTR_ERR(em);
3909 goto out;
3910 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003911
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003912 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003913 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003914
Chris Masonea8efc72011-03-08 11:54:40 -05003915 /* break if the extent we found is outside the range */
3916 if (em->start >= max || extent_map_end(em) < off)
3917 break;
3918
3919 /*
3920 * get_extent may return an extent that starts before our
3921 * requested range. We have to make sure the ranges
3922 * we return to fiemap always move forward and don't
3923 * overlap, so adjust the offsets here
3924 */
3925 em_start = max(em->start, off);
3926
3927 /*
3928 * record the offset from the start of the extent
3929 * for adjusting the disk offset below
3930 */
3931 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003932 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003933 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003934 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003935 disko = 0;
3936 flags = 0;
3937
Chris Masonea8efc72011-03-08 11:54:40 -05003938 /*
3939 * bump off for our next call to get_extent
3940 */
3941 off = extent_map_end(em);
3942 if (off >= max)
3943 end = 1;
3944
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003945 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003946 end = 1;
3947 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003948 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003949 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3950 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003951 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003952 flags |= (FIEMAP_EXTENT_DELALLOC |
3953 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003954 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003955 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003956 }
3957 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3958 flags |= FIEMAP_EXTENT_ENCODED;
3959
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003960 free_extent_map(em);
3961 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003962 if ((em_start >= last) || em_len == (u64)-1 ||
3963 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003964 flags |= FIEMAP_EXTENT_LAST;
3965 end = 1;
3966 }
3967
Chris Masonec29ed52011-02-23 16:23:20 -05003968 /* now scan forward to see if this is really the last extent. */
3969 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3970 get_extent);
3971 if (IS_ERR(em)) {
3972 ret = PTR_ERR(em);
3973 goto out;
3974 }
3975 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003976 flags |= FIEMAP_EXTENT_LAST;
3977 end = 1;
3978 }
Chris Masonec29ed52011-02-23 16:23:20 -05003979 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3980 em_len, flags);
3981 if (ret)
3982 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003983 }
3984out_free:
3985 free_extent_map(em);
3986out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003987 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3988 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003989 return ret;
3990}
3991
Chris Mason727011e2010-08-06 13:21:20 -04003992static void __free_extent_buffer(struct extent_buffer *eb)
3993{
3994#if LEAK_DEBUG
3995 unsigned long flags;
3996 spin_lock_irqsave(&leak_lock, flags);
3997 list_del(&eb->leak_list);
3998 spin_unlock_irqrestore(&leak_lock, flags);
3999#endif
4000 if (eb->pages && eb->pages != eb->inline_pages)
4001 kfree(eb->pages);
4002 kmem_cache_free(extent_buffer_cache, eb);
4003}
4004
Chris Masond1310b22008-01-24 16:13:08 -05004005static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4006 u64 start,
4007 unsigned long len,
4008 gfp_t mask)
4009{
4010 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05004011#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004012 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04004013#endif
Chris Masond1310b22008-01-24 16:13:08 -05004014
Chris Masond1310b22008-01-24 16:13:08 -05004015 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004016 if (eb == NULL)
4017 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004018 eb->start = start;
4019 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004020 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004021 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004022 rwlock_init(&eb->lock);
4023 atomic_set(&eb->write_locks, 0);
4024 atomic_set(&eb->read_locks, 0);
4025 atomic_set(&eb->blocking_readers, 0);
4026 atomic_set(&eb->blocking_writers, 0);
4027 atomic_set(&eb->spinning_readers, 0);
4028 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004029 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004030 init_waitqueue_head(&eb->write_lock_wq);
4031 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004032
Chris Mason39351272009-02-04 09:24:05 -05004033#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004034 spin_lock_irqsave(&leak_lock, flags);
4035 list_add(&eb->leak_list, &buffers);
4036 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04004037#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05004038 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004039 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004040 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004041
4042 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
4043 struct page **pages;
4044 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
4045 PAGE_CACHE_SHIFT;
4046 pages = kzalloc(num_pages, mask);
4047 if (!pages) {
4048 __free_extent_buffer(eb);
4049 return NULL;
4050 }
4051 eb->pages = pages;
4052 } else {
4053 eb->pages = eb->inline_pages;
4054 }
Chris Masond1310b22008-01-24 16:13:08 -05004055
4056 return eb;
4057}
4058
Jan Schmidt815a51c2012-05-16 17:00:02 +02004059struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4060{
4061 unsigned long i;
4062 struct page *p;
4063 struct extent_buffer *new;
4064 unsigned long num_pages = num_extent_pages(src->start, src->len);
4065
4066 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4067 if (new == NULL)
4068 return NULL;
4069
4070 for (i = 0; i < num_pages; i++) {
4071 p = alloc_page(GFP_ATOMIC);
4072 BUG_ON(!p);
4073 attach_extent_buffer_page(new, p);
4074 WARN_ON(PageDirty(p));
4075 SetPageUptodate(p);
4076 new->pages[i] = p;
4077 }
4078
4079 copy_extent_buffer(new, src, 0, 0, src->len);
4080 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4081 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4082
4083 return new;
4084}
4085
4086struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4087{
4088 struct extent_buffer *eb;
4089 unsigned long num_pages = num_extent_pages(0, len);
4090 unsigned long i;
4091
4092 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4093 if (!eb)
4094 return NULL;
4095
4096 for (i = 0; i < num_pages; i++) {
4097 eb->pages[i] = alloc_page(GFP_ATOMIC);
4098 if (!eb->pages[i])
4099 goto err;
4100 }
4101 set_extent_buffer_uptodate(eb);
4102 btrfs_set_header_nritems(eb, 0);
4103 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4104
4105 return eb;
4106err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004107 for (; i > 0; i--)
4108 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004109 __free_extent_buffer(eb);
4110 return NULL;
4111}
4112
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004113static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004114{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004115 return (atomic_read(&eb->io_pages) ||
4116 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4117 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004118}
4119
Miao Xie897ca6e2010-10-26 20:57:29 -04004120/*
4121 * Helper for releasing extent buffer page.
4122 */
4123static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4124 unsigned long start_idx)
4125{
4126 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004127 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004128 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004129 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004130
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004131 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004132
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004133 num_pages = num_extent_pages(eb->start, eb->len);
4134 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004135 if (start_idx >= index)
4136 return;
4137
4138 do {
4139 index--;
4140 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004141 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004142 spin_lock(&page->mapping->private_lock);
4143 /*
4144 * We do this since we'll remove the pages after we've
4145 * removed the eb from the radix tree, so we could race
4146 * and have this page now attached to the new eb. So
4147 * only clear page_private if it's still connected to
4148 * this eb.
4149 */
4150 if (PagePrivate(page) &&
4151 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004152 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004153 BUG_ON(PageDirty(page));
4154 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004155 /*
4156 * We need to make sure we haven't be attached
4157 * to a new eb.
4158 */
4159 ClearPagePrivate(page);
4160 set_page_private(page, 0);
4161 /* One for the page private */
4162 page_cache_release(page);
4163 }
4164 spin_unlock(&page->mapping->private_lock);
4165
Jan Schmidt815a51c2012-05-16 17:00:02 +02004166 }
4167 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004168 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004169 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004170 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004171 } while (index != start_idx);
4172}
4173
4174/*
4175 * Helper for releasing the extent buffer.
4176 */
4177static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4178{
4179 btrfs_release_extent_buffer_page(eb, 0);
4180 __free_extent_buffer(eb);
4181}
4182
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004183static void check_buffer_tree_ref(struct extent_buffer *eb)
4184{
4185 /* the ref bit is tricky. We have to make sure it is set
4186 * if we have the buffer dirty. Otherwise the
4187 * code to free a buffer can end up dropping a dirty
4188 * page
4189 *
4190 * Once the ref bit is set, it won't go away while the
4191 * buffer is dirty or in writeback, and it also won't
4192 * go away while we have the reference count on the
4193 * eb bumped.
4194 *
4195 * We can't just set the ref bit without bumping the
4196 * ref on the eb because free_extent_buffer might
4197 * see the ref bit and try to clear it. If this happens
4198 * free_extent_buffer might end up dropping our original
4199 * ref by mistake and freeing the page before we are able
4200 * to add one more ref.
4201 *
4202 * So bump the ref count first, then set the bit. If someone
4203 * beat us to it, drop the ref we added.
4204 */
Josef Bacik594831c2012-07-20 16:11:08 -04004205 spin_lock(&eb->refs_lock);
4206 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004207 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004208 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004209}
4210
Josef Bacik5df42352012-03-15 18:24:42 -04004211static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4212{
4213 unsigned long num_pages, i;
4214
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004215 check_buffer_tree_ref(eb);
4216
Josef Bacik5df42352012-03-15 18:24:42 -04004217 num_pages = num_extent_pages(eb->start, eb->len);
4218 for (i = 0; i < num_pages; i++) {
4219 struct page *p = extent_buffer_page(eb, i);
4220 mark_page_accessed(p);
4221 }
4222}
4223
Chris Masond1310b22008-01-24 16:13:08 -05004224struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004225 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004226{
4227 unsigned long num_pages = num_extent_pages(start, len);
4228 unsigned long i;
4229 unsigned long index = start >> PAGE_CACHE_SHIFT;
4230 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004231 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004232 struct page *p;
4233 struct address_space *mapping = tree->mapping;
4234 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004235 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004236
Miao Xie19fe0a82010-10-26 20:57:29 -04004237 rcu_read_lock();
4238 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4239 if (eb && atomic_inc_not_zero(&eb->refs)) {
4240 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004241 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004242 return eb;
4243 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004244 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004245
David Sterbaba144192011-04-21 01:12:06 +02004246 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004247 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004248 return NULL;
4249
Chris Mason727011e2010-08-06 13:21:20 -04004250 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004251 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004252 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004253 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004254
4255 spin_lock(&mapping->private_lock);
4256 if (PagePrivate(p)) {
4257 /*
4258 * We could have already allocated an eb for this page
4259 * and attached one so lets see if we can get a ref on
4260 * the existing eb, and if we can we know it's good and
4261 * we can just return that one, else we know we can just
4262 * overwrite page->private.
4263 */
4264 exists = (struct extent_buffer *)p->private;
4265 if (atomic_inc_not_zero(&exists->refs)) {
4266 spin_unlock(&mapping->private_lock);
4267 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004268 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004269 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004270 goto free_eb;
4271 }
4272
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004273 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004274 * Do this so attach doesn't complain and we need to
4275 * drop the ref the old guy had.
4276 */
4277 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004278 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004279 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004280 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004281 attach_extent_buffer_page(eb, p);
4282 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004283 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004284 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004285 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004286 if (!PageUptodate(p))
4287 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004288
4289 /*
4290 * see below about how we avoid a nasty race with release page
4291 * and why we unlock later
4292 */
Chris Masond1310b22008-01-24 16:13:08 -05004293 }
4294 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004295 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004296again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004297 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4298 if (ret)
4299 goto free_eb;
4300
Chris Mason6af118ce2008-07-22 11:18:07 -04004301 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004302 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4303 if (ret == -EEXIST) {
4304 exists = radix_tree_lookup(&tree->buffer,
4305 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004306 if (!atomic_inc_not_zero(&exists->refs)) {
4307 spin_unlock(&tree->buffer_lock);
4308 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004309 exists = NULL;
4310 goto again;
4311 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004312 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004313 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004314 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004315 goto free_eb;
4316 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004317 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004318 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004319 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004320 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004321
4322 /*
4323 * there is a race where release page may have
4324 * tried to find this extent buffer in the radix
4325 * but failed. It will tell the VM it is safe to
4326 * reclaim the, and it will clear the page private bit.
4327 * We must make sure to set the page private bit properly
4328 * after the extent buffer is in the radix tree so
4329 * it doesn't get lost
4330 */
Chris Mason727011e2010-08-06 13:21:20 -04004331 SetPageChecked(eb->pages[0]);
4332 for (i = 1; i < num_pages; i++) {
4333 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004334 ClearPageChecked(p);
4335 unlock_page(p);
4336 }
4337 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004338 return eb;
4339
Chris Mason6af118ce2008-07-22 11:18:07 -04004340free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004341 for (i = 0; i < num_pages; i++) {
4342 if (eb->pages[i])
4343 unlock_page(eb->pages[i]);
4344 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004345
Josef Bacik17de39a2012-05-04 15:16:06 -04004346 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004347 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004348 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004349}
Chris Masond1310b22008-01-24 16:13:08 -05004350
4351struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004352 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004353{
Chris Masond1310b22008-01-24 16:13:08 -05004354 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004355
Miao Xie19fe0a82010-10-26 20:57:29 -04004356 rcu_read_lock();
4357 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4358 if (eb && atomic_inc_not_zero(&eb->refs)) {
4359 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004360 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004361 return eb;
4362 }
4363 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004364
Miao Xie19fe0a82010-10-26 20:57:29 -04004365 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004366}
Chris Masond1310b22008-01-24 16:13:08 -05004367
Josef Bacik3083ee22012-03-09 16:01:49 -05004368static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4369{
4370 struct extent_buffer *eb =
4371 container_of(head, struct extent_buffer, rcu_head);
4372
4373 __free_extent_buffer(eb);
4374}
4375
Josef Bacik3083ee22012-03-09 16:01:49 -05004376/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004377static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004378{
4379 WARN_ON(atomic_read(&eb->refs) == 0);
4380 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004381 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4382 spin_unlock(&eb->refs_lock);
4383 } else {
4384 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004385
Jan Schmidt815a51c2012-05-16 17:00:02 +02004386 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004387
Jan Schmidt815a51c2012-05-16 17:00:02 +02004388 spin_lock(&tree->buffer_lock);
4389 radix_tree_delete(&tree->buffer,
4390 eb->start >> PAGE_CACHE_SHIFT);
4391 spin_unlock(&tree->buffer_lock);
4392 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004393
4394 /* Should be safe to release our pages at this point */
4395 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004396 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004397 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004398 }
4399 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004400
4401 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004402}
4403
Chris Masond1310b22008-01-24 16:13:08 -05004404void free_extent_buffer(struct extent_buffer *eb)
4405{
Chris Masond1310b22008-01-24 16:13:08 -05004406 if (!eb)
4407 return;
4408
Josef Bacik3083ee22012-03-09 16:01:49 -05004409 spin_lock(&eb->refs_lock);
4410 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004411 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4412 atomic_dec(&eb->refs);
4413
4414 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004415 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004416 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004417 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4418 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004419
Josef Bacik3083ee22012-03-09 16:01:49 -05004420 /*
4421 * I know this is terrible, but it's temporary until we stop tracking
4422 * the uptodate bits and such for the extent buffers.
4423 */
4424 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004425}
Chris Masond1310b22008-01-24 16:13:08 -05004426
Josef Bacik3083ee22012-03-09 16:01:49 -05004427void free_extent_buffer_stale(struct extent_buffer *eb)
4428{
4429 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004430 return;
4431
Josef Bacik3083ee22012-03-09 16:01:49 -05004432 spin_lock(&eb->refs_lock);
4433 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4434
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004435 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004436 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4437 atomic_dec(&eb->refs);
4438 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004439}
4440
Chris Mason1d4284b2012-03-28 20:31:37 -04004441void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004442{
Chris Masond1310b22008-01-24 16:13:08 -05004443 unsigned long i;
4444 unsigned long num_pages;
4445 struct page *page;
4446
Chris Masond1310b22008-01-24 16:13:08 -05004447 num_pages = num_extent_pages(eb->start, eb->len);
4448
4449 for (i = 0; i < num_pages; i++) {
4450 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004451 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004452 continue;
4453
Chris Masona61e6f22008-07-22 11:18:08 -04004454 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004455 WARN_ON(!PagePrivate(page));
4456
Chris Masond1310b22008-01-24 16:13:08 -05004457 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004458 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004459 if (!PageDirty(page)) {
4460 radix_tree_tag_clear(&page->mapping->page_tree,
4461 page_index(page),
4462 PAGECACHE_TAG_DIRTY);
4463 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004464 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004465 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004466 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004467 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004468 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004469}
Chris Masond1310b22008-01-24 16:13:08 -05004470
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004471int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004472{
4473 unsigned long i;
4474 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004475 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004476
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004477 check_buffer_tree_ref(eb);
4478
Chris Masonb9473432009-03-13 11:00:37 -04004479 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004480
Chris Masond1310b22008-01-24 16:13:08 -05004481 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004482 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004483 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4484
Chris Masonb9473432009-03-13 11:00:37 -04004485 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004486 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004487 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004488}
Chris Masond1310b22008-01-24 16:13:08 -05004489
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004490static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004491{
4492 if (len < PAGE_CACHE_SIZE)
4493 return 1;
4494 if (start & (PAGE_CACHE_SIZE - 1))
4495 return 1;
4496 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4497 return 1;
4498 return 0;
4499}
4500
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004501int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004502{
4503 unsigned long i;
4504 struct page *page;
4505 unsigned long num_pages;
4506
Chris Masonb4ce94d2009-02-04 09:25:08 -05004507 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004508 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004509 for (i = 0; i < num_pages; i++) {
4510 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004511 if (page)
4512 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004513 }
4514 return 0;
4515}
4516
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004517int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004518{
4519 unsigned long i;
4520 struct page *page;
4521 unsigned long num_pages;
4522
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004523 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004524 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004525 for (i = 0; i < num_pages; i++) {
4526 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004527 SetPageUptodate(page);
4528 }
4529 return 0;
4530}
Chris Masond1310b22008-01-24 16:13:08 -05004531
Chris Masonce9adaa2008-04-09 16:28:12 -04004532int extent_range_uptodate(struct extent_io_tree *tree,
4533 u64 start, u64 end)
4534{
4535 struct page *page;
4536 int ret;
4537 int pg_uptodate = 1;
4538 int uptodate;
4539 unsigned long index;
4540
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004541 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004542 ret = test_range_bit(tree, start, end,
4543 EXTENT_UPTODATE, 1, NULL);
4544 if (ret)
4545 return 1;
4546 }
Chris Masond3977122009-01-05 21:25:51 -05004547 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004548 index = start >> PAGE_CACHE_SHIFT;
4549 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004550 if (!page)
4551 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004552 uptodate = PageUptodate(page);
4553 page_cache_release(page);
4554 if (!uptodate) {
4555 pg_uptodate = 0;
4556 break;
4557 }
4558 start += PAGE_CACHE_SIZE;
4559 }
4560 return pg_uptodate;
4561}
4562
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004563int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004564{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004565 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004566}
Chris Masond1310b22008-01-24 16:13:08 -05004567
4568int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004569 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004570 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004571{
4572 unsigned long i;
4573 unsigned long start_i;
4574 struct page *page;
4575 int err;
4576 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004577 int locked_pages = 0;
4578 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004579 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004580 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004581 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004582 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004583
Chris Masonb4ce94d2009-02-04 09:25:08 -05004584 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004585 return 0;
4586
Chris Masond1310b22008-01-24 16:13:08 -05004587 if (start) {
4588 WARN_ON(start < eb->start);
4589 start_i = (start >> PAGE_CACHE_SHIFT) -
4590 (eb->start >> PAGE_CACHE_SHIFT);
4591 } else {
4592 start_i = 0;
4593 }
4594
4595 num_pages = num_extent_pages(eb->start, eb->len);
4596 for (i = start_i; i < num_pages; i++) {
4597 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004598 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004599 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004600 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004601 } else {
4602 lock_page(page);
4603 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004604 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004605 if (!PageUptodate(page)) {
4606 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004607 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004608 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004609 }
4610 if (all_uptodate) {
4611 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004612 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004613 goto unlock_exit;
4614 }
4615
Josef Bacikea466792012-03-26 21:57:36 -04004616 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004617 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004618 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004619 for (i = start_i; i < num_pages; i++) {
4620 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004621 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004622 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004623 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004624 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004625 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004626 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004627 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004628 } else {
4629 unlock_page(page);
4630 }
4631 }
4632
Jeff Mahoney355808c2011-10-03 23:23:14 -04004633 if (bio) {
4634 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004635 if (err)
4636 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004637 }
Chris Masona86c12c2008-02-07 10:50:54 -05004638
Arne Jansenbb82ab82011-06-10 14:06:53 +02004639 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004640 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004641
Chris Masond1310b22008-01-24 16:13:08 -05004642 for (i = start_i; i < num_pages; i++) {
4643 page = extent_buffer_page(eb, i);
4644 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004645 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004646 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004647 }
Chris Masond3977122009-01-05 21:25:51 -05004648
Chris Masond1310b22008-01-24 16:13:08 -05004649 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004650
4651unlock_exit:
4652 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004653 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004654 page = extent_buffer_page(eb, i);
4655 i++;
4656 unlock_page(page);
4657 locked_pages--;
4658 }
4659 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004660}
Chris Masond1310b22008-01-24 16:13:08 -05004661
4662void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4663 unsigned long start,
4664 unsigned long len)
4665{
4666 size_t cur;
4667 size_t offset;
4668 struct page *page;
4669 char *kaddr;
4670 char *dst = (char *)dstv;
4671 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4672 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004673
4674 WARN_ON(start > eb->len);
4675 WARN_ON(start + len > eb->start + eb->len);
4676
4677 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4678
Chris Masond3977122009-01-05 21:25:51 -05004679 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004680 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004681
4682 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004683 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004684 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004685
4686 dst += cur;
4687 len -= cur;
4688 offset = 0;
4689 i++;
4690 }
4691}
Chris Masond1310b22008-01-24 16:13:08 -05004692
4693int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004694 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004695 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004696 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004697{
4698 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4699 char *kaddr;
4700 struct page *p;
4701 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4702 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4703 unsigned long end_i = (start_offset + start + min_len - 1) >>
4704 PAGE_CACHE_SHIFT;
4705
4706 if (i != end_i)
4707 return -EINVAL;
4708
4709 if (i == 0) {
4710 offset = start_offset;
4711 *map_start = 0;
4712 } else {
4713 offset = 0;
4714 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4715 }
Chris Masond3977122009-01-05 21:25:51 -05004716
Chris Masond1310b22008-01-24 16:13:08 -05004717 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004718 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4719 "wanted %lu %lu\n", (unsigned long long)eb->start,
4720 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004721 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004722 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004723 }
4724
4725 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004726 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004727 *map = kaddr + offset;
4728 *map_len = PAGE_CACHE_SIZE - offset;
4729 return 0;
4730}
Chris Masond1310b22008-01-24 16:13:08 -05004731
Chris Masond1310b22008-01-24 16:13:08 -05004732int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4733 unsigned long start,
4734 unsigned long len)
4735{
4736 size_t cur;
4737 size_t offset;
4738 struct page *page;
4739 char *kaddr;
4740 char *ptr = (char *)ptrv;
4741 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4742 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4743 int ret = 0;
4744
4745 WARN_ON(start > eb->len);
4746 WARN_ON(start + len > eb->start + eb->len);
4747
4748 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4749
Chris Masond3977122009-01-05 21:25:51 -05004750 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004751 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004752
4753 cur = min(len, (PAGE_CACHE_SIZE - offset));
4754
Chris Masona6591712011-07-19 12:04:14 -04004755 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004756 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004757 if (ret)
4758 break;
4759
4760 ptr += cur;
4761 len -= cur;
4762 offset = 0;
4763 i++;
4764 }
4765 return ret;
4766}
Chris Masond1310b22008-01-24 16:13:08 -05004767
4768void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4769 unsigned long start, unsigned long len)
4770{
4771 size_t cur;
4772 size_t offset;
4773 struct page *page;
4774 char *kaddr;
4775 char *src = (char *)srcv;
4776 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4777 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4778
4779 WARN_ON(start > eb->len);
4780 WARN_ON(start + len > eb->start + eb->len);
4781
4782 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4783
Chris Masond3977122009-01-05 21:25:51 -05004784 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004785 page = extent_buffer_page(eb, i);
4786 WARN_ON(!PageUptodate(page));
4787
4788 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004789 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004790 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004791
4792 src += cur;
4793 len -= cur;
4794 offset = 0;
4795 i++;
4796 }
4797}
Chris Masond1310b22008-01-24 16:13:08 -05004798
4799void memset_extent_buffer(struct extent_buffer *eb, char c,
4800 unsigned long start, unsigned long len)
4801{
4802 size_t cur;
4803 size_t offset;
4804 struct page *page;
4805 char *kaddr;
4806 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4807 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4808
4809 WARN_ON(start > eb->len);
4810 WARN_ON(start + len > eb->start + eb->len);
4811
4812 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4813
Chris Masond3977122009-01-05 21:25:51 -05004814 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004815 page = extent_buffer_page(eb, i);
4816 WARN_ON(!PageUptodate(page));
4817
4818 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004819 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004820 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004821
4822 len -= cur;
4823 offset = 0;
4824 i++;
4825 }
4826}
Chris Masond1310b22008-01-24 16:13:08 -05004827
4828void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4829 unsigned long dst_offset, unsigned long src_offset,
4830 unsigned long len)
4831{
4832 u64 dst_len = dst->len;
4833 size_t cur;
4834 size_t offset;
4835 struct page *page;
4836 char *kaddr;
4837 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4838 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4839
4840 WARN_ON(src->len != dst_len);
4841
4842 offset = (start_offset + dst_offset) &
4843 ((unsigned long)PAGE_CACHE_SIZE - 1);
4844
Chris Masond3977122009-01-05 21:25:51 -05004845 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004846 page = extent_buffer_page(dst, i);
4847 WARN_ON(!PageUptodate(page));
4848
4849 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4850
Chris Masona6591712011-07-19 12:04:14 -04004851 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004852 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004853
4854 src_offset += cur;
4855 len -= cur;
4856 offset = 0;
4857 i++;
4858 }
4859}
Chris Masond1310b22008-01-24 16:13:08 -05004860
4861static void move_pages(struct page *dst_page, struct page *src_page,
4862 unsigned long dst_off, unsigned long src_off,
4863 unsigned long len)
4864{
Chris Masona6591712011-07-19 12:04:14 -04004865 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004866 if (dst_page == src_page) {
4867 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4868 } else {
Chris Masona6591712011-07-19 12:04:14 -04004869 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004870 char *p = dst_kaddr + dst_off + len;
4871 char *s = src_kaddr + src_off + len;
4872
4873 while (len--)
4874 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004875 }
Chris Masond1310b22008-01-24 16:13:08 -05004876}
4877
Sergei Trofimovich33872062011-04-11 21:52:52 +00004878static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4879{
4880 unsigned long distance = (src > dst) ? src - dst : dst - src;
4881 return distance < len;
4882}
4883
Chris Masond1310b22008-01-24 16:13:08 -05004884static void copy_pages(struct page *dst_page, struct page *src_page,
4885 unsigned long dst_off, unsigned long src_off,
4886 unsigned long len)
4887{
Chris Masona6591712011-07-19 12:04:14 -04004888 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004889 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004890 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004891
Sergei Trofimovich33872062011-04-11 21:52:52 +00004892 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004893 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004894 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004895 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004896 if (areas_overlap(src_off, dst_off, len))
4897 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004898 }
Chris Masond1310b22008-01-24 16:13:08 -05004899
Chris Mason727011e2010-08-06 13:21:20 -04004900 if (must_memmove)
4901 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4902 else
4903 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004904}
4905
4906void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4907 unsigned long src_offset, unsigned long len)
4908{
4909 size_t cur;
4910 size_t dst_off_in_page;
4911 size_t src_off_in_page;
4912 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4913 unsigned long dst_i;
4914 unsigned long src_i;
4915
4916 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004917 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4918 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004919 BUG_ON(1);
4920 }
4921 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004922 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4923 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004924 BUG_ON(1);
4925 }
4926
Chris Masond3977122009-01-05 21:25:51 -05004927 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004928 dst_off_in_page = (start_offset + dst_offset) &
4929 ((unsigned long)PAGE_CACHE_SIZE - 1);
4930 src_off_in_page = (start_offset + src_offset) &
4931 ((unsigned long)PAGE_CACHE_SIZE - 1);
4932
4933 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4934 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4935
4936 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4937 src_off_in_page));
4938 cur = min_t(unsigned long, cur,
4939 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4940
4941 copy_pages(extent_buffer_page(dst, dst_i),
4942 extent_buffer_page(dst, src_i),
4943 dst_off_in_page, src_off_in_page, cur);
4944
4945 src_offset += cur;
4946 dst_offset += cur;
4947 len -= cur;
4948 }
4949}
Chris Masond1310b22008-01-24 16:13:08 -05004950
4951void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4952 unsigned long src_offset, unsigned long len)
4953{
4954 size_t cur;
4955 size_t dst_off_in_page;
4956 size_t src_off_in_page;
4957 unsigned long dst_end = dst_offset + len - 1;
4958 unsigned long src_end = src_offset + len - 1;
4959 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4960 unsigned long dst_i;
4961 unsigned long src_i;
4962
4963 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004964 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4965 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004966 BUG_ON(1);
4967 }
4968 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004969 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4970 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004971 BUG_ON(1);
4972 }
Chris Mason727011e2010-08-06 13:21:20 -04004973 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004974 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4975 return;
4976 }
Chris Masond3977122009-01-05 21:25:51 -05004977 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004978 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4979 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4980
4981 dst_off_in_page = (start_offset + dst_end) &
4982 ((unsigned long)PAGE_CACHE_SIZE - 1);
4983 src_off_in_page = (start_offset + src_end) &
4984 ((unsigned long)PAGE_CACHE_SIZE - 1);
4985
4986 cur = min_t(unsigned long, len, src_off_in_page + 1);
4987 cur = min(cur, dst_off_in_page + 1);
4988 move_pages(extent_buffer_page(dst, dst_i),
4989 extent_buffer_page(dst, src_i),
4990 dst_off_in_page - cur + 1,
4991 src_off_in_page - cur + 1, cur);
4992
4993 dst_end -= cur;
4994 src_end -= cur;
4995 len -= cur;
4996 }
4997}
Chris Mason6af118ce2008-07-22 11:18:07 -04004998
Josef Bacik3083ee22012-03-09 16:01:49 -05004999int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04005000{
Chris Mason6af118ce2008-07-22 11:18:07 -04005001 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005002
Miao Xie19fe0a82010-10-26 20:57:29 -04005003 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005004 * We need to make sure noboody is attaching this page to an eb right
5005 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005006 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005007 spin_lock(&page->mapping->private_lock);
5008 if (!PagePrivate(page)) {
5009 spin_unlock(&page->mapping->private_lock);
5010 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005011 }
5012
Josef Bacik3083ee22012-03-09 16:01:49 -05005013 eb = (struct extent_buffer *)page->private;
5014 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005015
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005016 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005017 * This is a little awful but should be ok, we need to make sure that
5018 * the eb doesn't disappear out from under us while we're looking at
5019 * this page.
5020 */
5021 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005022 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005023 spin_unlock(&eb->refs_lock);
5024 spin_unlock(&page->mapping->private_lock);
5025 return 0;
5026 }
5027 spin_unlock(&page->mapping->private_lock);
5028
5029 if ((mask & GFP_NOFS) == GFP_NOFS)
5030 mask = GFP_NOFS;
5031
5032 /*
5033 * If tree ref isn't set then we know the ref on this eb is a real ref,
5034 * so just return, this page will likely be freed soon anyway.
5035 */
5036 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5037 spin_unlock(&eb->refs_lock);
5038 return 0;
5039 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005040
Josef Bacike64860a2012-07-20 16:05:36 -04005041 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04005042}