blob: f173c5af64610de66597cce1e38ac493729d6125 [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>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/spinlock.h>
8#include <linux/blkdev.h>
9#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050010#include <linux/writeback.h>
11#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070012#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040016#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020019#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010020#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040021#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040022#include "rcu-string.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
27static LIST_HEAD(buffers);
28static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040029
Chris Masonb47eda82008-11-10 12:34:40 -050030#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050031#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050032static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040033#endif
Chris Masond1310b22008-01-24 16:13:08 -050034
Chris Masond1310b22008-01-24 16:13:08 -050035#define BUFFER_LRU_MAX 64
36
37struct tree_entry {
38 u64 start;
39 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050040 struct rb_node rb_node;
41};
42
43struct extent_page_data {
44 struct bio *bio;
45 struct extent_io_tree *tree;
46 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -040047 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -050048
49 /* tells writepage not to lock the state bits for this range
50 * it still does the unlocking
51 */
Chris Masonffbd5172009-04-20 15:50:09 -040052 unsigned int extent_locked:1;
53
54 /* tells the submit_bio code to use a WRITE_SYNC */
55 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050056};
57
Josef Bacik0b32f4b2012-03-13 09:38:00 -040058static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040059static inline struct btrfs_fs_info *
60tree_fs_info(struct extent_io_tree *tree)
61{
62 return btrfs_sb(tree->mapping->host->i_sb);
63}
Josef Bacik0b32f4b2012-03-13 09:38:00 -040064
Chris Masond1310b22008-01-24 16:13:08 -050065int __init extent_io_init(void)
66{
David Sterba837e1972012-09-07 03:00:48 -060067 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020068 sizeof(struct extent_state), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050070 if (!extent_state_cache)
71 return -ENOMEM;
72
David Sterba837e1972012-09-07 03:00:48 -060073 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020074 sizeof(struct extent_buffer), 0,
75 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050076 if (!extent_buffer_cache)
77 goto free_state_cache;
78 return 0;
79
80free_state_cache:
81 kmem_cache_destroy(extent_state_cache);
82 return -ENOMEM;
83}
84
85void extent_io_exit(void)
86{
87 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040088 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050089
90 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040091 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050092 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
93 "state %lu in tree %p refs %d\n",
94 (unsigned long long)state->start,
95 (unsigned long long)state->end,
96 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040097 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050098 kmem_cache_free(extent_state_cache, state);
99
100 }
101
Chris Mason2d2ae542008-03-26 16:24:23 -0400102 while (!list_empty(&buffers)) {
103 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500104 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
105 "refs %d\n", (unsigned long long)eb->start,
106 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400107 list_del(&eb->leak_list);
108 kmem_cache_free(extent_buffer_cache, eb);
109 }
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000110
111 /*
112 * Make sure all delayed rcu free are flushed before we
113 * destroy caches.
114 */
115 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500116 if (extent_state_cache)
117 kmem_cache_destroy(extent_state_cache);
118 if (extent_buffer_cache)
119 kmem_cache_destroy(extent_buffer_cache);
120}
121
122void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200123 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500124{
Eric Paris6bef4d32010-02-23 19:43:04 +0000125 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400126 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500127 tree->ops = NULL;
128 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500129 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400130 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500131 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500132}
Chris Masond1310b22008-01-24 16:13:08 -0500133
Christoph Hellwigb2950862008-12-02 09:54:17 -0500134static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500135{
136 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500137#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400138 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400139#endif
Chris Masond1310b22008-01-24 16:13:08 -0500140
141 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400142 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500143 return state;
144 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500145 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500146 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_add(&state->leak_list, &states);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 atomic_set(&state->refs, 1);
153 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100154 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500155 return state;
156}
Chris Masond1310b22008-01-24 16:13:08 -0500157
Chris Mason4845e442010-05-25 20:56:50 -0400158void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500159{
Chris Masond1310b22008-01-24 16:13:08 -0500160 if (!state)
161 return;
162 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500163#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400164 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400165#endif
Chris Mason70dec802008-01-29 09:59:12 -0500166 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500167#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400168 spin_lock_irqsave(&leak_lock, flags);
169 list_del(&state->leak_list);
170 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400171#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100172 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500173 kmem_cache_free(extent_state_cache, state);
174 }
175}
Chris Masond1310b22008-01-24 16:13:08 -0500176
177static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
178 struct rb_node *node)
179{
Chris Masond3977122009-01-05 21:25:51 -0500180 struct rb_node **p = &root->rb_node;
181 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct tree_entry *entry;
183
Chris Masond3977122009-01-05 21:25:51 -0500184 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500185 parent = *p;
186 entry = rb_entry(parent, struct tree_entry, rb_node);
187
188 if (offset < entry->start)
189 p = &(*p)->rb_left;
190 else if (offset > entry->end)
191 p = &(*p)->rb_right;
192 else
193 return parent;
194 }
195
Chris Masond1310b22008-01-24 16:13:08 -0500196 rb_link_node(node, parent, p);
197 rb_insert_color(node, root);
198 return NULL;
199}
200
Chris Mason80ea96b2008-02-01 14:51:59 -0500201static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500202 struct rb_node **prev_ret,
203 struct rb_node **next_ret)
204{
Chris Mason80ea96b2008-02-01 14:51:59 -0500205 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500206 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500207 struct rb_node *prev = NULL;
208 struct rb_node *orig_prev = NULL;
209 struct tree_entry *entry;
210 struct tree_entry *prev_entry = NULL;
211
Chris Masond3977122009-01-05 21:25:51 -0500212 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500213 entry = rb_entry(n, struct tree_entry, rb_node);
214 prev = n;
215 prev_entry = entry;
216
217 if (offset < entry->start)
218 n = n->rb_left;
219 else if (offset > entry->end)
220 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500221 else
Chris Masond1310b22008-01-24 16:13:08 -0500222 return n;
223 }
224
225 if (prev_ret) {
226 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500227 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500228 prev = rb_next(prev);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 }
231 *prev_ret = prev;
232 prev = orig_prev;
233 }
234
235 if (next_ret) {
236 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500237 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500238 prev = rb_prev(prev);
239 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
240 }
241 *next_ret = prev;
242 }
243 return NULL;
244}
245
Chris Mason80ea96b2008-02-01 14:51:59 -0500246static inline struct rb_node *tree_search(struct extent_io_tree *tree,
247 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500248{
Chris Mason70dec802008-01-29 09:59:12 -0500249 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500250 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500251
Chris Mason80ea96b2008-02-01 14:51:59 -0500252 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500253 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500254 return prev;
255 return ret;
256}
257
Josef Bacik9ed74f22009-09-11 16:12:44 -0400258static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
259 struct extent_state *other)
260{
261 if (tree->ops && tree->ops->merge_extent_hook)
262 tree->ops->merge_extent_hook(tree->mapping->host, new,
263 other);
264}
265
Chris Masond1310b22008-01-24 16:13:08 -0500266/*
267 * utility function to look for merge candidates inside a given range.
268 * Any extents with matching state are merged together into a single
269 * extent in the tree. Extents with EXTENT_IO in their state field
270 * are not merged because the end_io handlers need to be able to do
271 * operations on them without sleeping (or doing allocations/splits).
272 *
273 * This should be called with the tree lock held.
274 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000275static void merge_state(struct extent_io_tree *tree,
276 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500277{
278 struct extent_state *other;
279 struct rb_node *other_node;
280
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400281 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000282 return;
Chris Masond1310b22008-01-24 16:13:08 -0500283
284 other_node = rb_prev(&state->rb_node);
285 if (other_node) {
286 other = rb_entry(other_node, struct extent_state, rb_node);
287 if (other->end == state->start - 1 &&
288 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500290 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500291 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500292 rb_erase(&other->rb_node, &tree->state);
293 free_extent_state(other);
294 }
295 }
296 other_node = rb_next(&state->rb_node);
297 if (other_node) {
298 other = rb_entry(other_node, struct extent_state, rb_node);
299 if (other->start == state->end + 1 &&
300 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400301 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400302 state->end = other->end;
303 other->tree = NULL;
304 rb_erase(&other->rb_node, &tree->state);
305 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500306 }
307 }
Chris Masond1310b22008-01-24 16:13:08 -0500308}
309
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000310static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400311 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500312{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000313 if (tree->ops && tree->ops->set_bit_hook)
314 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500315}
316
317static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500319{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400320 if (tree->ops && tree->ops->clear_bit_hook)
321 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500322}
323
Xiao Guangrong3150b692011-07-14 03:19:08 +0000324static void set_state_bits(struct extent_io_tree *tree,
325 struct extent_state *state, int *bits);
326
Chris Masond1310b22008-01-24 16:13:08 -0500327/*
328 * insert an extent_state struct into the tree. 'bits' are set on the
329 * struct before it is inserted.
330 *
331 * This may return -EEXIST if the extent is already there, in which case the
332 * state struct is freed.
333 *
334 * The tree lock is not taken internally. This is a utility function and
335 * probably isn't what you want to call (see set/clear_extent_bit).
336 */
337static int insert_state(struct extent_io_tree *tree,
338 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400339 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500340{
341 struct rb_node *node;
342
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000343 if (end < start)
344 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Chris Masond3977122009-01-05 21:25:51 -0500345 (unsigned long long)end,
346 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500347 state->start = start;
348 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400349
Xiao Guangrong3150b692011-07-14 03:19:08 +0000350 set_state_bits(tree, state, bits);
351
Chris Masond1310b22008-01-24 16:13:08 -0500352 node = tree_insert(&tree->state, end, &state->rb_node);
353 if (node) {
354 struct extent_state *found;
355 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500356 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
357 "%llu %llu\n", (unsigned long long)found->start,
358 (unsigned long long)found->end,
359 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500360 return -EEXIST;
361 }
Chris Mason70dec802008-01-29 09:59:12 -0500362 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500363 merge_state(tree, state);
364 return 0;
365}
366
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000367static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400368 u64 split)
369{
370 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000371 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400372}
373
Chris Masond1310b22008-01-24 16:13:08 -0500374/*
375 * split a given extent state struct in two, inserting the preallocated
376 * struct 'prealloc' as the newly created second half. 'split' indicates an
377 * offset inside 'orig' where it should be split.
378 *
379 * Before calling,
380 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
381 * are two extent state structs in the tree:
382 * prealloc: [orig->start, split - 1]
383 * orig: [ split, orig->end ]
384 *
385 * The tree locks are not taken by this function. They need to be held
386 * by the caller.
387 */
388static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
389 struct extent_state *prealloc, u64 split)
390{
391 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400392
393 split_cb(tree, orig, split);
394
Chris Masond1310b22008-01-24 16:13:08 -0500395 prealloc->start = orig->start;
396 prealloc->end = split - 1;
397 prealloc->state = orig->state;
398 orig->start = split;
399
400 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
401 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500402 free_extent_state(prealloc);
403 return -EEXIST;
404 }
Chris Mason70dec802008-01-29 09:59:12 -0500405 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500406 return 0;
407}
408
Li Zefancdc6a392012-03-12 16:39:48 +0800409static struct extent_state *next_state(struct extent_state *state)
410{
411 struct rb_node *next = rb_next(&state->rb_node);
412 if (next)
413 return rb_entry(next, struct extent_state, rb_node);
414 else
415 return NULL;
416}
417
Chris Masond1310b22008-01-24 16:13:08 -0500418/*
419 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800420 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500421 *
422 * If no bits are set on the state struct after clearing things, the
423 * struct is freed and removed from the tree
424 */
Li Zefancdc6a392012-03-12 16:39:48 +0800425static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
426 struct extent_state *state,
427 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500428{
Li Zefancdc6a392012-03-12 16:39:48 +0800429 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400430 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500431
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400432 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500433 u64 range = state->end - state->start + 1;
434 WARN_ON(range > tree->dirty_bytes);
435 tree->dirty_bytes -= range;
436 }
Chris Mason291d6732008-01-29 15:55:23 -0500437 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400438 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500439 if (wake)
440 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400441 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800442 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500443 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500444 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500445 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500446 free_extent_state(state);
447 } else {
448 WARN_ON(1);
449 }
450 } else {
451 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800452 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500453 }
Li Zefancdc6a392012-03-12 16:39:48 +0800454 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500455}
456
Xiao Guangrong82337672011-04-20 06:44:57 +0000457static struct extent_state *
458alloc_extent_state_atomic(struct extent_state *prealloc)
459{
460 if (!prealloc)
461 prealloc = alloc_extent_state(GFP_ATOMIC);
462
463 return prealloc;
464}
465
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400466void extent_io_tree_panic(struct extent_io_tree *tree, int err)
467{
468 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
469 "Extent tree was modified by another "
470 "thread while locked.");
471}
472
Chris Masond1310b22008-01-24 16:13:08 -0500473/*
474 * clear some bits on a range in the tree. This may require splitting
475 * or inserting elements in the tree, so the gfp mask is used to
476 * indicate which allocations or sleeping are allowed.
477 *
478 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
479 * the given range from the tree regardless of state (ie for truncate).
480 *
481 * the range [start, end] is inclusive.
482 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100483 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500484 */
485int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400486 int bits, int wake, int delete,
487 struct extent_state **cached_state,
488 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500489{
490 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400491 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500492 struct extent_state *prealloc = NULL;
493 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400494 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500495 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000496 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500497
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400498 if (delete)
499 bits |= ~EXTENT_CTLBITS;
500 bits |= EXTENT_FIRST_DELALLOC;
501
Josef Bacik2ac55d42010-02-03 19:33:23 +0000502 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
503 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500504again:
505 if (!prealloc && (mask & __GFP_WAIT)) {
506 prealloc = alloc_extent_state(mask);
507 if (!prealloc)
508 return -ENOMEM;
509 }
510
Chris Masoncad321a2008-12-17 14:51:42 -0500511 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400512 if (cached_state) {
513 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000514
515 if (clear) {
516 *cached_state = NULL;
517 cached_state = NULL;
518 }
519
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400520 if (cached && cached->tree && cached->start <= start &&
521 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000522 if (clear)
523 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400524 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400525 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400526 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000527 if (clear)
528 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400529 }
Chris Masond1310b22008-01-24 16:13:08 -0500530 /*
531 * this search will find the extents that end after
532 * our range starts
533 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500534 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500535 if (!node)
536 goto out;
537 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400538hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500539 if (state->start > end)
540 goto out;
541 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400542 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500543
Liu Bo04493142012-02-16 18:34:37 +0800544 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800545 if (!(state->state & bits)) {
546 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800547 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800548 }
Liu Bo04493142012-02-16 18:34:37 +0800549
Chris Masond1310b22008-01-24 16:13:08 -0500550 /*
551 * | ---- desired range ---- |
552 * | state | or
553 * | ------------- state -------------- |
554 *
555 * We need to split the extent we found, and may flip
556 * bits on second half.
557 *
558 * If the extent we found extends past our range, we
559 * just split and search again. It'll get split again
560 * the next time though.
561 *
562 * If the extent we found is inside our range, we clear
563 * the desired bit on it.
564 */
565
566 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000567 prealloc = alloc_extent_state_atomic(prealloc);
568 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500569 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400570 if (err)
571 extent_io_tree_panic(tree, err);
572
Chris Masond1310b22008-01-24 16:13:08 -0500573 prealloc = NULL;
574 if (err)
575 goto out;
576 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800577 state = clear_state_bit(tree, state, &bits, wake);
578 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500579 }
580 goto search_again;
581 }
582 /*
583 * | ---- desired range ---- |
584 * | state |
585 * We need to split the extent, and clear the bit
586 * on the first half
587 */
588 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000589 prealloc = alloc_extent_state_atomic(prealloc);
590 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500591 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400592 if (err)
593 extent_io_tree_panic(tree, err);
594
Chris Masond1310b22008-01-24 16:13:08 -0500595 if (wake)
596 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400597
Jeff Mahoney6763af82012-03-01 14:56:29 +0100598 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400599
Chris Masond1310b22008-01-24 16:13:08 -0500600 prealloc = NULL;
601 goto out;
602 }
Chris Mason42daec22009-09-23 19:51:09 -0400603
Li Zefancdc6a392012-03-12 16:39:48 +0800604 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800605next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400606 if (last_end == (u64)-1)
607 goto out;
608 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800609 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800610 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500611 goto search_again;
612
613out:
Chris Masoncad321a2008-12-17 14:51:42 -0500614 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500615 if (prealloc)
616 free_extent_state(prealloc);
617
Jeff Mahoney6763af82012-03-01 14:56:29 +0100618 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500619
620search_again:
621 if (start > end)
622 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500623 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500624 if (mask & __GFP_WAIT)
625 cond_resched();
626 goto again;
627}
Chris Masond1310b22008-01-24 16:13:08 -0500628
Jeff Mahoney143bede2012-03-01 14:56:26 +0100629static void wait_on_state(struct extent_io_tree *tree,
630 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500631 __releases(tree->lock)
632 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500633{
634 DEFINE_WAIT(wait);
635 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500636 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500637 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500638 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500639 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500640}
641
642/*
643 * waits for one or more bits to clear on a range in the state tree.
644 * The range [start, end] is inclusive.
645 * The tree lock is taken by this function
646 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100647void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500648{
649 struct extent_state *state;
650 struct rb_node *node;
651
Chris Masoncad321a2008-12-17 14:51:42 -0500652 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500653again:
654 while (1) {
655 /*
656 * this search will find all the extents that end after
657 * our range starts
658 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500659 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500660 if (!node)
661 break;
662
663 state = rb_entry(node, struct extent_state, rb_node);
664
665 if (state->start > end)
666 goto out;
667
668 if (state->state & bits) {
669 start = state->start;
670 atomic_inc(&state->refs);
671 wait_on_state(tree, state);
672 free_extent_state(state);
673 goto again;
674 }
675 start = state->end + 1;
676
677 if (start > end)
678 break;
679
Xiao Guangrongded91f02011-07-14 03:19:27 +0000680 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500681 }
682out:
Chris Masoncad321a2008-12-17 14:51:42 -0500683 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500684}
Chris Masond1310b22008-01-24 16:13:08 -0500685
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000686static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500687 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400688 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500689{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400691
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000692 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400693 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500694 u64 range = state->end - state->start + 1;
695 tree->dirty_bytes += range;
696 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400697 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500698}
699
Chris Mason2c64c532009-09-02 15:04:12 -0400700static void cache_state(struct extent_state *state,
701 struct extent_state **cached_ptr)
702{
703 if (cached_ptr && !(*cached_ptr)) {
704 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
705 *cached_ptr = state;
706 atomic_inc(&state->refs);
707 }
708 }
709}
710
Arne Jansen507903b2011-04-06 10:02:20 +0000711static void uncache_state(struct extent_state **cached_ptr)
712{
713 if (cached_ptr && (*cached_ptr)) {
714 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400715 *cached_ptr = NULL;
716 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000717 }
718}
719
Chris Masond1310b22008-01-24 16:13:08 -0500720/*
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * set some bits on a range in the tree. This may require allocations or
722 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500723 *
Chris Mason1edbb732009-09-02 13:24:36 -0400724 * If any of the exclusive bits are set, this will fail with -EEXIST if some
725 * part of the range already has the desired bits set. The start of the
726 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500727 *
Chris Mason1edbb732009-09-02 13:24:36 -0400728 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500729 */
Chris Mason1edbb732009-09-02 13:24:36 -0400730
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100731static int __must_check
732__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
733 int bits, int exclusive_bits, u64 *failed_start,
734 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500735{
736 struct extent_state *state;
737 struct extent_state *prealloc = NULL;
738 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500739 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500740 u64 last_start;
741 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400742
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400743 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500744again:
745 if (!prealloc && (mask & __GFP_WAIT)) {
746 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000747 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500748 }
749
Chris Masoncad321a2008-12-17 14:51:42 -0500750 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400751 if (cached_state && *cached_state) {
752 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400753 if (state->start <= start && state->end > start &&
754 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400755 node = &state->rb_node;
756 goto hit_next;
757 }
758 }
Chris Masond1310b22008-01-24 16:13:08 -0500759 /*
760 * this search will find all the extents that end after
761 * our range starts.
762 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500763 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500764 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000765 prealloc = alloc_extent_state_atomic(prealloc);
766 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400767 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400768 if (err)
769 extent_io_tree_panic(tree, err);
770
Chris Masond1310b22008-01-24 16:13:08 -0500771 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500772 goto out;
773 }
Chris Masond1310b22008-01-24 16:13:08 -0500774 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400775hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500776 last_start = state->start;
777 last_end = state->end;
778
779 /*
780 * | ---- desired range ---- |
781 * | state |
782 *
783 * Just lock what we found and keep going
784 */
785 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400786 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500787 *failed_start = state->start;
788 err = -EEXIST;
789 goto out;
790 }
Chris Mason42daec22009-09-23 19:51:09 -0400791
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000792 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400793 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500794 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400795 if (last_end == (u64)-1)
796 goto out;
797 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800798 state = next_state(state);
799 if (start < end && state && state->start == start &&
800 !need_resched())
801 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500802 goto search_again;
803 }
804
805 /*
806 * | ---- desired range ---- |
807 * | state |
808 * or
809 * | ------------- state -------------- |
810 *
811 * We need to split the extent we found, and may flip bits on
812 * second half.
813 *
814 * If the extent we found extends past our
815 * range, we just split and search again. It'll get split
816 * again the next time though.
817 *
818 * If the extent we found is inside our range, we set the
819 * desired bit on it.
820 */
821 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400822 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500823 *failed_start = start;
824 err = -EEXIST;
825 goto out;
826 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000827
828 prealloc = alloc_extent_state_atomic(prealloc);
829 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500830 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400831 if (err)
832 extent_io_tree_panic(tree, err);
833
Chris Masond1310b22008-01-24 16:13:08 -0500834 prealloc = NULL;
835 if (err)
836 goto out;
837 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000838 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400839 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500840 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400841 if (last_end == (u64)-1)
842 goto out;
843 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800844 state = next_state(state);
845 if (start < end && state && state->start == start &&
846 !need_resched())
847 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500848 }
849 goto search_again;
850 }
851 /*
852 * | ---- desired range ---- |
853 * | state | or | state |
854 *
855 * There's a hole, we need to insert something in it and
856 * ignore the extent we found.
857 */
858 if (state->start > start) {
859 u64 this_end;
860 if (end < last_start)
861 this_end = end;
862 else
Chris Masond3977122009-01-05 21:25:51 -0500863 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000864
865 prealloc = alloc_extent_state_atomic(prealloc);
866 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000867
868 /*
869 * Avoid to free 'prealloc' if it can be merged with
870 * the later extent.
871 */
Chris Masond1310b22008-01-24 16:13:08 -0500872 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400873 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400874 if (err)
875 extent_io_tree_panic(tree, err);
876
Chris Mason2c64c532009-09-02 15:04:12 -0400877 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500878 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500879 start = this_end + 1;
880 goto search_again;
881 }
882 /*
883 * | ---- desired range ---- |
884 * | state |
885 * We need to split the extent, and set the bit
886 * on the first half
887 */
888 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400889 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500890 *failed_start = start;
891 err = -EEXIST;
892 goto out;
893 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000894
895 prealloc = alloc_extent_state_atomic(prealloc);
896 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500897 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400898 if (err)
899 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500900
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000901 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400902 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500903 merge_state(tree, prealloc);
904 prealloc = NULL;
905 goto out;
906 }
907
908 goto search_again;
909
910out:
Chris Masoncad321a2008-12-17 14:51:42 -0500911 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500912 if (prealloc)
913 free_extent_state(prealloc);
914
915 return err;
916
917search_again:
918 if (start > end)
919 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500920 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500921 if (mask & __GFP_WAIT)
922 cond_resched();
923 goto again;
924}
Chris Masond1310b22008-01-24 16:13:08 -0500925
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100926int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
927 u64 *failed_start, struct extent_state **cached_state,
928 gfp_t mask)
929{
930 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
931 cached_state, mask);
932}
933
934
Josef Bacik462d6fa2011-09-26 13:56:12 -0400935/**
Liu Bo10983f22012-07-11 15:26:19 +0800936 * convert_extent_bit - convert all bits in a given range from one bit to
937 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400938 * @tree: the io tree to search
939 * @start: the start offset in bytes
940 * @end: the end offset in bytes (inclusive)
941 * @bits: the bits to set in this range
942 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400943 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400944 * @mask: the allocation mask
945 *
946 * This will go through and set bits for the given range. If any states exist
947 * already in this range they are set with the given bit and cleared of the
948 * clear_bits. This is only meant to be used by things that are mergeable, ie
949 * converting from say DELALLOC to DIRTY. This is not meant to be used with
950 * boundary bits like LOCK.
951 */
952int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacike6138872012-09-27 17:07:30 -0400953 int bits, int clear_bits,
954 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400955{
956 struct extent_state *state;
957 struct extent_state *prealloc = NULL;
958 struct rb_node *node;
959 int err = 0;
960 u64 last_start;
961 u64 last_end;
962
963again:
964 if (!prealloc && (mask & __GFP_WAIT)) {
965 prealloc = alloc_extent_state(mask);
966 if (!prealloc)
967 return -ENOMEM;
968 }
969
970 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400971 if (cached_state && *cached_state) {
972 state = *cached_state;
973 if (state->start <= start && state->end > start &&
974 state->tree) {
975 node = &state->rb_node;
976 goto hit_next;
977 }
978 }
979
Josef Bacik462d6fa2011-09-26 13:56:12 -0400980 /*
981 * this search will find all the extents that end after
982 * our range starts.
983 */
984 node = tree_search(tree, start);
985 if (!node) {
986 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500987 if (!prealloc) {
988 err = -ENOMEM;
989 goto out;
990 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400991 err = insert_state(tree, prealloc, start, end, &bits);
992 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400993 if (err)
994 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400995 goto out;
996 }
997 state = rb_entry(node, struct extent_state, rb_node);
998hit_next:
999 last_start = state->start;
1000 last_end = state->end;
1001
1002 /*
1003 * | ---- desired range ---- |
1004 * | state |
1005 *
1006 * Just lock what we found and keep going
1007 */
1008 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001009 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001010 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001011 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001012 if (last_end == (u64)-1)
1013 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001014 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001015 if (start < end && state && state->start == start &&
1016 !need_resched())
1017 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001018 goto search_again;
1019 }
1020
1021 /*
1022 * | ---- desired range ---- |
1023 * | state |
1024 * or
1025 * | ------------- state -------------- |
1026 *
1027 * We need to split the extent we found, and may flip bits on
1028 * second half.
1029 *
1030 * If the extent we found extends past our
1031 * range, we just split and search again. It'll get split
1032 * again the next time though.
1033 *
1034 * If the extent we found is inside our range, we set the
1035 * desired bit on it.
1036 */
1037 if (state->start < start) {
1038 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001039 if (!prealloc) {
1040 err = -ENOMEM;
1041 goto out;
1042 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001043 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001044 if (err)
1045 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001046 prealloc = NULL;
1047 if (err)
1048 goto out;
1049 if (state->end <= end) {
1050 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001051 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001052 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001053 if (last_end == (u64)-1)
1054 goto out;
1055 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001056 if (start < end && state && state->start == start &&
1057 !need_resched())
1058 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001059 }
1060 goto search_again;
1061 }
1062 /*
1063 * | ---- desired range ---- |
1064 * | state | or | state |
1065 *
1066 * There's a hole, we need to insert something in it and
1067 * ignore the extent we found.
1068 */
1069 if (state->start > start) {
1070 u64 this_end;
1071 if (end < last_start)
1072 this_end = end;
1073 else
1074 this_end = last_start - 1;
1075
1076 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001077 if (!prealloc) {
1078 err = -ENOMEM;
1079 goto out;
1080 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001081
1082 /*
1083 * Avoid to free 'prealloc' if it can be merged with
1084 * the later extent.
1085 */
1086 err = insert_state(tree, prealloc, start, this_end,
1087 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001088 if (err)
1089 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001090 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001091 prealloc = NULL;
1092 start = this_end + 1;
1093 goto search_again;
1094 }
1095 /*
1096 * | ---- desired range ---- |
1097 * | state |
1098 * We need to split the extent, and set the bit
1099 * on the first half
1100 */
1101 if (state->start <= end && state->end > end) {
1102 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001103 if (!prealloc) {
1104 err = -ENOMEM;
1105 goto out;
1106 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001107
1108 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001109 if (err)
1110 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001111
1112 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001113 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001114 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001115 prealloc = NULL;
1116 goto out;
1117 }
1118
1119 goto search_again;
1120
1121out:
1122 spin_unlock(&tree->lock);
1123 if (prealloc)
1124 free_extent_state(prealloc);
1125
1126 return err;
1127
1128search_again:
1129 if (start > end)
1130 goto out;
1131 spin_unlock(&tree->lock);
1132 if (mask & __GFP_WAIT)
1133 cond_resched();
1134 goto again;
1135}
1136
Chris Masond1310b22008-01-24 16:13:08 -05001137/* wrappers around set/clear extent bit */
1138int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1139 gfp_t mask)
1140{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001141 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001142 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
1145int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1146 int bits, gfp_t mask)
1147{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001148 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001149 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001150}
Chris Masond1310b22008-01-24 16:13:08 -05001151
1152int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1153 int bits, gfp_t mask)
1154{
Chris Mason2c64c532009-09-02 15:04:12 -04001155 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001156}
Chris Masond1310b22008-01-24 16:13:08 -05001157
1158int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001159 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001160{
1161 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001162 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001163 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001164}
Chris Masond1310b22008-01-24 16:13:08 -05001165
Liu Bo9e8a4a82012-09-05 19:10:51 -06001166int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1167 struct extent_state **cached_state, gfp_t mask)
1168{
1169 return set_extent_bit(tree, start, end,
1170 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1171 NULL, cached_state, mask);
1172}
1173
Chris Masond1310b22008-01-24 16:13:08 -05001174int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1175 gfp_t mask)
1176{
1177 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001178 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001179 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001180}
Chris Masond1310b22008-01-24 16:13:08 -05001181
1182int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1183 gfp_t mask)
1184{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001185 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001186 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001187}
Chris Masond1310b22008-01-24 16:13:08 -05001188
Chris Masond1310b22008-01-24 16:13:08 -05001189int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001190 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001191{
Arne Jansen507903b2011-04-06 10:02:20 +00001192 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001193 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001194}
Chris Masond1310b22008-01-24 16:13:08 -05001195
Josef Bacik5fd02042012-05-02 14:00:54 -04001196int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1197 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001198{
Chris Mason2c64c532009-09-02 15:04:12 -04001199 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001200 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001201}
Chris Masond1310b22008-01-24 16:13:08 -05001202
Chris Masond352ac62008-09-29 15:18:18 -04001203/*
1204 * either insert or lock state struct between start and end use mask to tell
1205 * us if waiting is desired.
1206 */
Chris Mason1edbb732009-09-02 13:24:36 -04001207int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001208 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001209{
1210 int err;
1211 u64 failed_start;
1212 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001213 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1214 EXTENT_LOCKED, &failed_start,
1215 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001216 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001217 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1218 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001219 } else
Chris Masond1310b22008-01-24 16:13:08 -05001220 break;
Chris Masond1310b22008-01-24 16:13:08 -05001221 WARN_ON(start > end);
1222 }
1223 return err;
1224}
Chris Masond1310b22008-01-24 16:13:08 -05001225
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001226int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001227{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001228 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001229}
1230
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001231int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001232{
1233 int err;
1234 u64 failed_start;
1235
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001236 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1237 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001238 if (err == -EEXIST) {
1239 if (failed_start > start)
1240 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001241 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001242 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001243 }
Josef Bacik25179202008-10-29 14:49:05 -04001244 return 1;
1245}
Josef Bacik25179202008-10-29 14:49:05 -04001246
Chris Mason2c64c532009-09-02 15:04:12 -04001247int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1248 struct extent_state **cached, gfp_t mask)
1249{
1250 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1251 mask);
1252}
1253
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001254int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001255{
Chris Mason2c64c532009-09-02 15:04:12 -04001256 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001257 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001258}
Chris Masond1310b22008-01-24 16:13:08 -05001259
1260/*
Chris Masond1310b22008-01-24 16:13:08 -05001261 * helper function to set both pages and extents in the tree writeback
1262 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001263static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001264{
1265 unsigned long index = start >> PAGE_CACHE_SHIFT;
1266 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1267 struct page *page;
1268
1269 while (index <= end_index) {
1270 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001271 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001272 set_page_writeback(page);
1273 page_cache_release(page);
1274 index++;
1275 }
Chris Masond1310b22008-01-24 16:13:08 -05001276 return 0;
1277}
Chris Masond1310b22008-01-24 16:13:08 -05001278
Chris Masond352ac62008-09-29 15:18:18 -04001279/* find the first state struct with 'bits' set after 'start', and
1280 * return it. tree->lock must be held. NULL will returned if
1281 * nothing was found after 'start'
1282 */
Chris Masond7fc6402008-02-18 12:12:38 -05001283struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1284 u64 start, int bits)
1285{
1286 struct rb_node *node;
1287 struct extent_state *state;
1288
1289 /*
1290 * this search will find all the extents that end after
1291 * our range starts.
1292 */
1293 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001294 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001295 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001296
Chris Masond3977122009-01-05 21:25:51 -05001297 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001298 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001299 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001300 return state;
Chris Masond3977122009-01-05 21:25:51 -05001301
Chris Masond7fc6402008-02-18 12:12:38 -05001302 node = rb_next(node);
1303 if (!node)
1304 break;
1305 }
1306out:
1307 return NULL;
1308}
Chris Masond7fc6402008-02-18 12:12:38 -05001309
Chris Masond352ac62008-09-29 15:18:18 -04001310/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001311 * find the first offset in the io tree with 'bits' set. zero is
1312 * returned if we find something, and *start_ret and *end_ret are
1313 * set to reflect the state struct that was found.
1314 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001315 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001316 */
1317int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Josef Bacike6138872012-09-27 17:07:30 -04001318 u64 *start_ret, u64 *end_ret, int bits,
1319 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001320{
1321 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001322 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001323 int ret = 1;
1324
1325 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001326 if (cached_state && *cached_state) {
1327 state = *cached_state;
1328 if (state->end == start - 1 && state->tree) {
1329 n = rb_next(&state->rb_node);
1330 while (n) {
1331 state = rb_entry(n, struct extent_state,
1332 rb_node);
1333 if (state->state & bits)
1334 goto got_it;
1335 n = rb_next(n);
1336 }
1337 free_extent_state(*cached_state);
1338 *cached_state = NULL;
1339 goto out;
1340 }
1341 free_extent_state(*cached_state);
1342 *cached_state = NULL;
1343 }
1344
Xiao Guangrong69261c42011-07-14 03:19:45 +00001345 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001346got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001347 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001348 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001349 *start_ret = state->start;
1350 *end_ret = state->end;
1351 ret = 0;
1352 }
Josef Bacike6138872012-09-27 17:07:30 -04001353out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001354 spin_unlock(&tree->lock);
1355 return ret;
1356}
1357
1358/*
Chris Masond352ac62008-09-29 15:18:18 -04001359 * find a contiguous range of bytes in the file marked as delalloc, not
1360 * more than 'max_bytes'. start and end are used to return the range,
1361 *
1362 * 1 is returned if we find something, 0 if nothing was in the tree
1363 */
Chris Masonc8b97812008-10-29 14:49:59 -04001364static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001365 u64 *start, u64 *end, u64 max_bytes,
1366 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001367{
1368 struct rb_node *node;
1369 struct extent_state *state;
1370 u64 cur_start = *start;
1371 u64 found = 0;
1372 u64 total_bytes = 0;
1373
Chris Masoncad321a2008-12-17 14:51:42 -05001374 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001375
Chris Masond1310b22008-01-24 16:13:08 -05001376 /*
1377 * this search will find all the extents that end after
1378 * our range starts.
1379 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001380 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001381 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001382 if (!found)
1383 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001384 goto out;
1385 }
1386
Chris Masond3977122009-01-05 21:25:51 -05001387 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001388 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001389 if (found && (state->start != cur_start ||
1390 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001391 goto out;
1392 }
1393 if (!(state->state & EXTENT_DELALLOC)) {
1394 if (!found)
1395 *end = state->end;
1396 goto out;
1397 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001398 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001399 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001400 *cached_state = state;
1401 atomic_inc(&state->refs);
1402 }
Chris Masond1310b22008-01-24 16:13:08 -05001403 found++;
1404 *end = state->end;
1405 cur_start = state->end + 1;
1406 node = rb_next(node);
1407 if (!node)
1408 break;
1409 total_bytes += state->end - state->start + 1;
1410 if (total_bytes >= max_bytes)
1411 break;
1412 }
1413out:
Chris Masoncad321a2008-12-17 14:51:42 -05001414 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001415 return found;
1416}
1417
Jeff Mahoney143bede2012-03-01 14:56:26 +01001418static noinline void __unlock_for_delalloc(struct inode *inode,
1419 struct page *locked_page,
1420 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001421{
1422 int ret;
1423 struct page *pages[16];
1424 unsigned long index = start >> PAGE_CACHE_SHIFT;
1425 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1426 unsigned long nr_pages = end_index - index + 1;
1427 int i;
1428
1429 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001430 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001431
Chris Masond3977122009-01-05 21:25:51 -05001432 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001433 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001434 min_t(unsigned long, nr_pages,
1435 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001436 for (i = 0; i < ret; i++) {
1437 if (pages[i] != locked_page)
1438 unlock_page(pages[i]);
1439 page_cache_release(pages[i]);
1440 }
1441 nr_pages -= ret;
1442 index += ret;
1443 cond_resched();
1444 }
Chris Masonc8b97812008-10-29 14:49:59 -04001445}
1446
1447static noinline int lock_delalloc_pages(struct inode *inode,
1448 struct page *locked_page,
1449 u64 delalloc_start,
1450 u64 delalloc_end)
1451{
1452 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1453 unsigned long start_index = index;
1454 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1455 unsigned long pages_locked = 0;
1456 struct page *pages[16];
1457 unsigned long nrpages;
1458 int ret;
1459 int i;
1460
1461 /* the caller is responsible for locking the start index */
1462 if (index == locked_page->index && index == end_index)
1463 return 0;
1464
1465 /* skip the page at the start index */
1466 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001467 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001468 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001469 min_t(unsigned long,
1470 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001471 if (ret == 0) {
1472 ret = -EAGAIN;
1473 goto done;
1474 }
1475 /* now we have an array of pages, lock them all */
1476 for (i = 0; i < ret; i++) {
1477 /*
1478 * the caller is taking responsibility for
1479 * locked_page
1480 */
Chris Mason771ed682008-11-06 22:02:51 -05001481 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001482 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001483 if (!PageDirty(pages[i]) ||
1484 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001485 ret = -EAGAIN;
1486 unlock_page(pages[i]);
1487 page_cache_release(pages[i]);
1488 goto done;
1489 }
1490 }
Chris Masonc8b97812008-10-29 14:49:59 -04001491 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001492 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001493 }
Chris Masonc8b97812008-10-29 14:49:59 -04001494 nrpages -= ret;
1495 index += ret;
1496 cond_resched();
1497 }
1498 ret = 0;
1499done:
1500 if (ret && pages_locked) {
1501 __unlock_for_delalloc(inode, locked_page,
1502 delalloc_start,
1503 ((u64)(start_index + pages_locked - 1)) <<
1504 PAGE_CACHE_SHIFT);
1505 }
1506 return ret;
1507}
1508
1509/*
1510 * find a contiguous range of bytes in the file marked as delalloc, not
1511 * more than 'max_bytes'. start and end are used to return the range,
1512 *
1513 * 1 is returned if we find something, 0 if nothing was in the tree
1514 */
1515static noinline u64 find_lock_delalloc_range(struct inode *inode,
1516 struct extent_io_tree *tree,
1517 struct page *locked_page,
1518 u64 *start, u64 *end,
1519 u64 max_bytes)
1520{
1521 u64 delalloc_start;
1522 u64 delalloc_end;
1523 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001524 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001525 int ret;
1526 int loops = 0;
1527
1528again:
1529 /* step one, find a bunch of delalloc bytes starting at start */
1530 delalloc_start = *start;
1531 delalloc_end = 0;
1532 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001533 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001534 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001535 *start = delalloc_start;
1536 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001537 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001538 return found;
1539 }
1540
1541 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001542 * start comes from the offset of locked_page. We have to lock
1543 * pages in order, so we can't process delalloc bytes before
1544 * locked_page
1545 */
Chris Masond3977122009-01-05 21:25:51 -05001546 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001547 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001548
1549 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001550 * make sure to limit the number of pages we try to lock down
1551 * if we're looping.
1552 */
Chris Masond3977122009-01-05 21:25:51 -05001553 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001554 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001555
Chris Masonc8b97812008-10-29 14:49:59 -04001556 /* step two, lock all the pages after the page that has start */
1557 ret = lock_delalloc_pages(inode, locked_page,
1558 delalloc_start, delalloc_end);
1559 if (ret == -EAGAIN) {
1560 /* some of the pages are gone, lets avoid looping by
1561 * shortening the size of the delalloc range we're searching
1562 */
Chris Mason9655d292009-09-02 15:22:30 -04001563 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001564 if (!loops) {
1565 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1566 max_bytes = PAGE_CACHE_SIZE - offset;
1567 loops = 1;
1568 goto again;
1569 } else {
1570 found = 0;
1571 goto out_failed;
1572 }
1573 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001574 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001575
1576 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001577 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001578
1579 /* then test to make sure it is all still delalloc */
1580 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001581 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001582 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001583 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1584 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001585 __unlock_for_delalloc(inode, locked_page,
1586 delalloc_start, delalloc_end);
1587 cond_resched();
1588 goto again;
1589 }
Chris Mason9655d292009-09-02 15:22:30 -04001590 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001591 *start = delalloc_start;
1592 *end = delalloc_end;
1593out_failed:
1594 return found;
1595}
1596
1597int extent_clear_unlock_delalloc(struct inode *inode,
1598 struct extent_io_tree *tree,
1599 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001600 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001601{
1602 int ret;
1603 struct page *pages[16];
1604 unsigned long index = start >> PAGE_CACHE_SHIFT;
1605 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1606 unsigned long nr_pages = end_index - index + 1;
1607 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001608 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001609
Chris Masona791e352009-10-08 11:27:10 -04001610 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001611 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001612 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001613 clear_bits |= EXTENT_DIRTY;
1614
Chris Masona791e352009-10-08 11:27:10 -04001615 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001616 clear_bits |= EXTENT_DELALLOC;
1617
Chris Mason2c64c532009-09-02 15:04:12 -04001618 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001619 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1620 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1621 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001622 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001623
Chris Masond3977122009-01-05 21:25:51 -05001624 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001625 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001626 min_t(unsigned long,
1627 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001628 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001629
Chris Masona791e352009-10-08 11:27:10 -04001630 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001631 SetPagePrivate2(pages[i]);
1632
Chris Masonc8b97812008-10-29 14:49:59 -04001633 if (pages[i] == locked_page) {
1634 page_cache_release(pages[i]);
1635 continue;
1636 }
Chris Masona791e352009-10-08 11:27:10 -04001637 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001638 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001639 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001640 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001641 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001642 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001643 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001644 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001645 page_cache_release(pages[i]);
1646 }
1647 nr_pages -= ret;
1648 index += ret;
1649 cond_resched();
1650 }
1651 return 0;
1652}
Chris Masonc8b97812008-10-29 14:49:59 -04001653
Chris Masond352ac62008-09-29 15:18:18 -04001654/*
1655 * count the number of bytes in the tree that have a given bit(s)
1656 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1657 * cached. The total number found is returned.
1658 */
Chris Masond1310b22008-01-24 16:13:08 -05001659u64 count_range_bits(struct extent_io_tree *tree,
1660 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001661 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001662{
1663 struct rb_node *node;
1664 struct extent_state *state;
1665 u64 cur_start = *start;
1666 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001667 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001668 int found = 0;
1669
1670 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001671 WARN_ON(1);
1672 return 0;
1673 }
1674
Chris Masoncad321a2008-12-17 14:51:42 -05001675 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001676 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1677 total_bytes = tree->dirty_bytes;
1678 goto out;
1679 }
1680 /*
1681 * this search will find all the extents that end after
1682 * our range starts.
1683 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001684 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001685 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001686 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001687
Chris Masond3977122009-01-05 21:25:51 -05001688 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001689 state = rb_entry(node, struct extent_state, rb_node);
1690 if (state->start > search_end)
1691 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001692 if (contig && found && state->start > last + 1)
1693 break;
1694 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001695 total_bytes += min(search_end, state->end) + 1 -
1696 max(cur_start, state->start);
1697 if (total_bytes >= max_bytes)
1698 break;
1699 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001700 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001701 found = 1;
1702 }
Chris Masonec29ed52011-02-23 16:23:20 -05001703 last = state->end;
1704 } else if (contig && found) {
1705 break;
Chris Masond1310b22008-01-24 16:13:08 -05001706 }
1707 node = rb_next(node);
1708 if (!node)
1709 break;
1710 }
1711out:
Chris Masoncad321a2008-12-17 14:51:42 -05001712 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001713 return total_bytes;
1714}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001715
Chris Masond352ac62008-09-29 15:18:18 -04001716/*
1717 * set the private field for a given byte offset in the tree. If there isn't
1718 * an extent_state there already, this does nothing.
1719 */
Chris Masond1310b22008-01-24 16:13:08 -05001720int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1721{
1722 struct rb_node *node;
1723 struct extent_state *state;
1724 int ret = 0;
1725
Chris Masoncad321a2008-12-17 14:51:42 -05001726 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001727 /*
1728 * this search will find all the extents that end after
1729 * our range starts.
1730 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001731 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001732 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001733 ret = -ENOENT;
1734 goto out;
1735 }
1736 state = rb_entry(node, struct extent_state, rb_node);
1737 if (state->start != start) {
1738 ret = -ENOENT;
1739 goto out;
1740 }
1741 state->private = private;
1742out:
Chris Masoncad321a2008-12-17 14:51:42 -05001743 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001744 return ret;
1745}
1746
1747int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1748{
1749 struct rb_node *node;
1750 struct extent_state *state;
1751 int ret = 0;
1752
Chris Masoncad321a2008-12-17 14:51:42 -05001753 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001754 /*
1755 * this search will find all the extents that end after
1756 * our range starts.
1757 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001758 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001759 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001760 ret = -ENOENT;
1761 goto out;
1762 }
1763 state = rb_entry(node, struct extent_state, rb_node);
1764 if (state->start != start) {
1765 ret = -ENOENT;
1766 goto out;
1767 }
1768 *private = state->private;
1769out:
Chris Masoncad321a2008-12-17 14:51:42 -05001770 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001771 return ret;
1772}
1773
1774/*
1775 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001776 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001777 * has the bits set. Otherwise, 1 is returned if any bit in the
1778 * range is found set.
1779 */
1780int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001781 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001782{
1783 struct extent_state *state = NULL;
1784 struct rb_node *node;
1785 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001786
Chris Masoncad321a2008-12-17 14:51:42 -05001787 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001788 if (cached && cached->tree && cached->start <= start &&
1789 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001790 node = &cached->rb_node;
1791 else
1792 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001793 while (node && start <= end) {
1794 state = rb_entry(node, struct extent_state, rb_node);
1795
1796 if (filled && state->start > start) {
1797 bitset = 0;
1798 break;
1799 }
1800
1801 if (state->start > end)
1802 break;
1803
1804 if (state->state & bits) {
1805 bitset = 1;
1806 if (!filled)
1807 break;
1808 } else if (filled) {
1809 bitset = 0;
1810 break;
1811 }
Chris Mason46562cec2009-09-23 20:23:16 -04001812
1813 if (state->end == (u64)-1)
1814 break;
1815
Chris Masond1310b22008-01-24 16:13:08 -05001816 start = state->end + 1;
1817 if (start > end)
1818 break;
1819 node = rb_next(node);
1820 if (!node) {
1821 if (filled)
1822 bitset = 0;
1823 break;
1824 }
1825 }
Chris Masoncad321a2008-12-17 14:51:42 -05001826 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001827 return bitset;
1828}
Chris Masond1310b22008-01-24 16:13:08 -05001829
1830/*
1831 * helper function to set a given page up to date if all the
1832 * extents in the tree for that page are up to date
1833 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001834static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001835{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001836 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001837 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001838 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001839 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001840}
1841
1842/*
1843 * helper function to unlock a page if all the extents in the tree
1844 * for that page are unlocked
1845 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001846static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001847{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001848 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001849 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001850 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001851 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001852}
1853
1854/*
1855 * helper function to end page writeback if all the extents
1856 * in the tree for that page are done with writeback
1857 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001858static void check_page_writeback(struct extent_io_tree *tree,
1859 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001860{
Chris Mason1edbb732009-09-02 13:24:36 -04001861 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001862}
1863
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001864/*
1865 * When IO fails, either with EIO or csum verification fails, we
1866 * try other mirrors that might have a good copy of the data. This
1867 * io_failure_record is used to record state as we go through all the
1868 * mirrors. If another mirror has good data, the page is set up to date
1869 * and things continue. If a good mirror can't be found, the original
1870 * bio end_io callback is called to indicate things have failed.
1871 */
1872struct io_failure_record {
1873 struct page *page;
1874 u64 start;
1875 u64 len;
1876 u64 logical;
1877 unsigned long bio_flags;
1878 int this_mirror;
1879 int failed_mirror;
1880 int in_validation;
1881};
1882
1883static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1884 int did_repair)
1885{
1886 int ret;
1887 int err = 0;
1888 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1889
1890 set_state_private(failure_tree, rec->start, 0);
1891 ret = clear_extent_bits(failure_tree, rec->start,
1892 rec->start + rec->len - 1,
1893 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1894 if (ret)
1895 err = ret;
1896
David Woodhouse53b381b2013-01-29 18:40:14 -05001897 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1898 rec->start + rec->len - 1,
1899 EXTENT_DAMAGED, GFP_NOFS);
1900 if (ret && !err)
1901 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001902
1903 kfree(rec);
1904 return err;
1905}
1906
1907static void repair_io_failure_callback(struct bio *bio, int err)
1908{
1909 complete(bio->bi_private);
1910}
1911
1912/*
1913 * this bypasses the standard btrfs submit functions deliberately, as
1914 * the standard behavior is to write all copies in a raid setup. here we only
1915 * want to write the one bad copy. so we do the mapping for ourselves and issue
1916 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001917 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001918 * actually prevents the read that triggered the error from finishing.
1919 * currently, there can be no more than two copies of every data bit. thus,
1920 * exactly one rewrite is required.
1921 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001922int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001923 u64 length, u64 logical, struct page *page,
1924 int mirror_num)
1925{
1926 struct bio *bio;
1927 struct btrfs_device *dev;
1928 DECLARE_COMPLETION_ONSTACK(compl);
1929 u64 map_length = 0;
1930 u64 sector;
1931 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05001932 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001933 int ret;
1934
1935 BUG_ON(!mirror_num);
1936
David Woodhouse53b381b2013-01-29 18:40:14 -05001937 /* we can't repair anything in raid56 yet */
1938 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
1939 return 0;
1940
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001941 bio = bio_alloc(GFP_NOFS, 1);
1942 if (!bio)
1943 return -EIO;
1944 bio->bi_private = &compl;
1945 bio->bi_end_io = repair_io_failure_callback;
1946 bio->bi_size = 0;
1947 map_length = length;
1948
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001949 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001950 &map_length, &bbio, mirror_num);
1951 if (ret) {
1952 bio_put(bio);
1953 return -EIO;
1954 }
1955 BUG_ON(mirror_num != bbio->mirror_num);
1956 sector = bbio->stripes[mirror_num-1].physical >> 9;
1957 bio->bi_sector = sector;
1958 dev = bbio->stripes[mirror_num-1].dev;
1959 kfree(bbio);
1960 if (!dev || !dev->bdev || !dev->writeable) {
1961 bio_put(bio);
1962 return -EIO;
1963 }
1964 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00001965 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001966 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001967 wait_for_completion(&compl);
1968
1969 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1970 /* try to remap that extent elsewhere? */
1971 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001972 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001973 return -EIO;
1974 }
1975
Anand Jaind5b025d2012-07-02 22:05:21 -06001976 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04001977 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1978 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001979
1980 bio_put(bio);
1981 return 0;
1982}
1983
Josef Bacikea466792012-03-26 21:57:36 -04001984int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1985 int mirror_num)
1986{
Josef Bacikea466792012-03-26 21:57:36 -04001987 u64 start = eb->start;
1988 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001989 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001990
1991 for (i = 0; i < num_pages; i++) {
1992 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001993 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04001994 start, p, mirror_num);
1995 if (ret)
1996 break;
1997 start += PAGE_CACHE_SIZE;
1998 }
1999
2000 return ret;
2001}
2002
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002003/*
2004 * each time an IO finishes, we do a fast check in the IO failure tree
2005 * to see if we need to process or clean up an io_failure_record
2006 */
2007static int clean_io_failure(u64 start, struct page *page)
2008{
2009 u64 private;
2010 u64 private_failure;
2011 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002012 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002013 struct extent_state *state;
2014 int num_copies;
2015 int did_repair = 0;
2016 int ret;
2017 struct inode *inode = page->mapping->host;
2018
2019 private = 0;
2020 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2021 (u64)-1, 1, EXTENT_DIRTY, 0);
2022 if (!ret)
2023 return 0;
2024
2025 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2026 &private_failure);
2027 if (ret)
2028 return 0;
2029
2030 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2031 BUG_ON(!failrec->this_mirror);
2032
2033 if (failrec->in_validation) {
2034 /* there was no real error, just free the record */
2035 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2036 failrec->start);
2037 did_repair = 1;
2038 goto out;
2039 }
2040
2041 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2042 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2043 failrec->start,
2044 EXTENT_LOCKED);
2045 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2046
2047 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002048 fs_info = BTRFS_I(inode)->root->fs_info;
2049 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2050 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002052 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002053 failrec->logical, page,
2054 failrec->failed_mirror);
2055 did_repair = !ret;
2056 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002057 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002058 }
2059
2060out:
2061 if (!ret)
2062 ret = free_io_failure(inode, failrec, did_repair);
2063
2064 return ret;
2065}
2066
2067/*
2068 * this is a generic handler for readpage errors (default
2069 * readpage_io_failed_hook). if other copies exist, read those and write back
2070 * good data to the failed position. does not investigate in remapping the
2071 * failed extent elsewhere, hoping the device will be smart enough to do this as
2072 * needed
2073 */
2074
2075static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2076 u64 start, u64 end, int failed_mirror,
2077 struct extent_state *state)
2078{
2079 struct io_failure_record *failrec = NULL;
2080 u64 private;
2081 struct extent_map *em;
2082 struct inode *inode = page->mapping->host;
2083 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2084 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2085 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2086 struct bio *bio;
2087 int num_copies;
2088 int ret;
2089 int read_mode;
2090 u64 logical;
2091
2092 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2093
2094 ret = get_state_private(failure_tree, start, &private);
2095 if (ret) {
2096 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2097 if (!failrec)
2098 return -ENOMEM;
2099 failrec->start = start;
2100 failrec->len = end - start + 1;
2101 failrec->this_mirror = 0;
2102 failrec->bio_flags = 0;
2103 failrec->in_validation = 0;
2104
2105 read_lock(&em_tree->lock);
2106 em = lookup_extent_mapping(em_tree, start, failrec->len);
2107 if (!em) {
2108 read_unlock(&em_tree->lock);
2109 kfree(failrec);
2110 return -EIO;
2111 }
2112
2113 if (em->start > start || em->start + em->len < start) {
2114 free_extent_map(em);
2115 em = NULL;
2116 }
2117 read_unlock(&em_tree->lock);
2118
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002119 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 kfree(failrec);
2121 return -EIO;
2122 }
2123 logical = start - em->start;
2124 logical = em->block_start + logical;
2125 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2126 logical = em->block_start;
2127 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2128 extent_set_compress_type(&failrec->bio_flags,
2129 em->compress_type);
2130 }
2131 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2132 "len=%llu\n", logical, start, failrec->len);
2133 failrec->logical = logical;
2134 free_extent_map(em);
2135
2136 /* set the bits in the private failure tree */
2137 ret = set_extent_bits(failure_tree, start, end,
2138 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2139 if (ret >= 0)
2140 ret = set_state_private(failure_tree, start,
2141 (u64)(unsigned long)failrec);
2142 /* set the bits in the inode's tree */
2143 if (ret >= 0)
2144 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2145 GFP_NOFS);
2146 if (ret < 0) {
2147 kfree(failrec);
2148 return ret;
2149 }
2150 } else {
2151 failrec = (struct io_failure_record *)(unsigned long)private;
2152 pr_debug("bio_readpage_error: (found) logical=%llu, "
2153 "start=%llu, len=%llu, validation=%d\n",
2154 failrec->logical, failrec->start, failrec->len,
2155 failrec->in_validation);
2156 /*
2157 * when data can be on disk more than twice, add to failrec here
2158 * (e.g. with a list for failed_mirror) to make
2159 * clean_io_failure() clean all those errors at once.
2160 */
2161 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002162 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2163 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002164 if (num_copies == 1) {
2165 /*
2166 * we only have a single copy of the data, so don't bother with
2167 * all the retry and error correction code that follows. no
2168 * matter what the error is, it is very likely to persist.
2169 */
2170 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2171 "state=%p, num_copies=%d, next_mirror %d, "
2172 "failed_mirror %d\n", state, num_copies,
2173 failrec->this_mirror, failed_mirror);
2174 free_io_failure(inode, failrec, 0);
2175 return -EIO;
2176 }
2177
2178 if (!state) {
2179 spin_lock(&tree->lock);
2180 state = find_first_extent_bit_state(tree, failrec->start,
2181 EXTENT_LOCKED);
2182 if (state && state->start != failrec->start)
2183 state = NULL;
2184 spin_unlock(&tree->lock);
2185 }
2186
2187 /*
2188 * there are two premises:
2189 * a) deliver good data to the caller
2190 * b) correct the bad sectors on disk
2191 */
2192 if (failed_bio->bi_vcnt > 1) {
2193 /*
2194 * to fulfill b), we need to know the exact failing sectors, as
2195 * we don't want to rewrite any more than the failed ones. thus,
2196 * we need separate read requests for the failed bio
2197 *
2198 * if the following BUG_ON triggers, our validation request got
2199 * merged. we need separate requests for our algorithm to work.
2200 */
2201 BUG_ON(failrec->in_validation);
2202 failrec->in_validation = 1;
2203 failrec->this_mirror = failed_mirror;
2204 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2205 } else {
2206 /*
2207 * we're ready to fulfill a) and b) alongside. get a good copy
2208 * of the failed sector and if we succeed, we have setup
2209 * everything for repair_io_failure to do the rest for us.
2210 */
2211 if (failrec->in_validation) {
2212 BUG_ON(failrec->this_mirror != failed_mirror);
2213 failrec->in_validation = 0;
2214 failrec->this_mirror = 0;
2215 }
2216 failrec->failed_mirror = failed_mirror;
2217 failrec->this_mirror++;
2218 if (failrec->this_mirror == failed_mirror)
2219 failrec->this_mirror++;
2220 read_mode = READ_SYNC;
2221 }
2222
2223 if (!state || failrec->this_mirror > num_copies) {
2224 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2225 "next_mirror %d, failed_mirror %d\n", state,
2226 num_copies, failrec->this_mirror, failed_mirror);
2227 free_io_failure(inode, failrec, 0);
2228 return -EIO;
2229 }
2230
2231 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002232 if (!bio) {
2233 free_io_failure(inode, failrec, 0);
2234 return -EIO;
2235 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002236 bio->bi_private = state;
2237 bio->bi_end_io = failed_bio->bi_end_io;
2238 bio->bi_sector = failrec->logical >> 9;
2239 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2240 bio->bi_size = 0;
2241
2242 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2243
2244 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2245 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2246 failrec->this_mirror, num_copies, failrec->in_validation);
2247
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002248 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2249 failrec->this_mirror,
2250 failrec->bio_flags, 0);
2251 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002252}
2253
Chris Masond1310b22008-01-24 16:13:08 -05002254/* lots and lots of room for performance fixes in the end_bio funcs */
2255
Jeff Mahoney87826df2012-02-15 16:23:57 +01002256int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2257{
2258 int uptodate = (err == 0);
2259 struct extent_io_tree *tree;
2260 int ret;
2261
2262 tree = &BTRFS_I(page->mapping->host)->io_tree;
2263
2264 if (tree->ops && tree->ops->writepage_end_io_hook) {
2265 ret = tree->ops->writepage_end_io_hook(page, start,
2266 end, NULL, uptodate);
2267 if (ret)
2268 uptodate = 0;
2269 }
2270
Jeff Mahoney87826df2012-02-15 16:23:57 +01002271 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002272 ClearPageUptodate(page);
2273 SetPageError(page);
2274 }
2275 return 0;
2276}
2277
Chris Masond1310b22008-01-24 16:13:08 -05002278/*
2279 * after a writepage IO is done, we need to:
2280 * clear the uptodate bits on error
2281 * clear the writeback bits in the extent tree for this IO
2282 * end_page_writeback if the page has no more pending IO
2283 *
2284 * Scheduling is not allowed, so the extent state tree is expected
2285 * to have one and only one object corresponding to this IO.
2286 */
Chris Masond1310b22008-01-24 16:13:08 -05002287static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002288{
Chris Masond1310b22008-01-24 16:13:08 -05002289 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002290 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002291 u64 start;
2292 u64 end;
2293 int whole_page;
2294
Chris Masond1310b22008-01-24 16:13:08 -05002295 do {
2296 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002297 tree = &BTRFS_I(page->mapping->host)->io_tree;
2298
Miao Xie4eee4fa2012-12-21 09:17:45 +00002299 start = page_offset(page) + bvec->bv_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002300 end = start + bvec->bv_len - 1;
2301
2302 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2303 whole_page = 1;
2304 else
2305 whole_page = 0;
2306
2307 if (--bvec >= bio->bi_io_vec)
2308 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002309
Jeff Mahoney87826df2012-02-15 16:23:57 +01002310 if (end_extent_writepage(page, err, start, end))
2311 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002312
Chris Masond1310b22008-01-24 16:13:08 -05002313 if (whole_page)
2314 end_page_writeback(page);
2315 else
2316 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002317 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002318
Chris Masond1310b22008-01-24 16:13:08 -05002319 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002320}
2321
2322/*
2323 * after a readpage IO is done, we need to:
2324 * clear the uptodate bits on error
2325 * set the uptodate bits if things worked
2326 * set the page up to date if all extents in the tree are uptodate
2327 * clear the lock bit in the extent tree
2328 * unlock the page if there are no other extents locked for it
2329 *
2330 * Scheduling is not allowed, so the extent state tree is expected
2331 * to have one and only one object corresponding to this IO.
2332 */
Chris Masond1310b22008-01-24 16:13:08 -05002333static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002334{
2335 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002336 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2337 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002338 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002339 u64 start;
2340 u64 end;
2341 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002342 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002343 int ret;
2344
Chris Masond20f7042008-12-08 16:58:54 -05002345 if (err)
2346 uptodate = 0;
2347
Chris Masond1310b22008-01-24 16:13:08 -05002348 do {
2349 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002350 struct extent_state *cached = NULL;
2351 struct extent_state *state;
2352
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002353 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2354 "mirror=%ld\n", (u64)bio->bi_sector, err,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002355 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002356 tree = &BTRFS_I(page->mapping->host)->io_tree;
2357
Miao Xie4eee4fa2012-12-21 09:17:45 +00002358 start = page_offset(page) + bvec->bv_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002359 end = start + bvec->bv_len - 1;
2360
2361 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2362 whole_page = 1;
2363 else
2364 whole_page = 0;
2365
Chris Mason4125bf72010-02-03 18:18:45 +00002366 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002367 prefetchw(&bvec->bv_page->flags);
2368
Arne Jansen507903b2011-04-06 10:02:20 +00002369 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002370 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002371 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002372 /*
2373 * take a reference on the state, unlock will drop
2374 * the ref
2375 */
2376 cache_state(state, &cached);
2377 }
2378 spin_unlock(&tree->lock);
2379
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002380 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002381 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002382 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002383 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002384 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002385 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002386 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002387 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002388 }
Josef Bacikea466792012-03-26 21:57:36 -04002389
Josef Bacikea466792012-03-26 21:57:36 -04002390 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002391 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002392 if (!ret && !err &&
2393 test_bit(BIO_UPTODATE, &bio->bi_flags))
2394 uptodate = 1;
2395 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002396 /*
2397 * The generic bio_readpage_error handles errors the
2398 * following way: If possible, new read requests are
2399 * created and submitted and will end up in
2400 * end_bio_extent_readpage as well (if we're lucky, not
2401 * in the !uptodate case). In that case it returns 0 and
2402 * we just go on with the next page in our bio. If it
2403 * can't handle the error it will return -EIO and we
2404 * remain responsible for that page.
2405 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002406 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002407 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002408 uptodate =
2409 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002410 if (err)
2411 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002412 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002413 continue;
2414 }
2415 }
Chris Mason70dec802008-01-29 09:59:12 -05002416
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002417 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002418 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002419 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002420 }
Arne Jansen507903b2011-04-06 10:02:20 +00002421 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002422
Chris Mason70dec802008-01-29 09:59:12 -05002423 if (whole_page) {
2424 if (uptodate) {
2425 SetPageUptodate(page);
2426 } else {
2427 ClearPageUptodate(page);
2428 SetPageError(page);
2429 }
Chris Masond1310b22008-01-24 16:13:08 -05002430 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002431 } else {
2432 if (uptodate) {
2433 check_page_uptodate(tree, page);
2434 } else {
2435 ClearPageUptodate(page);
2436 SetPageError(page);
2437 }
Chris Masond1310b22008-01-24 16:13:08 -05002438 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002439 }
Chris Mason4125bf72010-02-03 18:18:45 +00002440 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002441
2442 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002443}
2444
Miao Xie88f794e2010-11-22 03:02:55 +00002445struct bio *
2446btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2447 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002448{
2449 struct bio *bio;
2450
2451 bio = bio_alloc(gfp_flags, nr_vecs);
2452
2453 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2454 while (!bio && (nr_vecs /= 2))
2455 bio = bio_alloc(gfp_flags, nr_vecs);
2456 }
2457
2458 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002459 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002460 bio->bi_bdev = bdev;
2461 bio->bi_sector = first_sector;
2462 }
2463 return bio;
2464}
2465
Jeff Mahoney355808c2011-10-03 23:23:14 -04002466static int __must_check submit_one_bio(int rw, struct bio *bio,
2467 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002468{
Chris Masond1310b22008-01-24 16:13:08 -05002469 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002470 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2471 struct page *page = bvec->bv_page;
2472 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002473 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002474
Miao Xie4eee4fa2012-12-21 09:17:45 +00002475 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002476
David Woodhouse902b22f2008-08-20 08:51:49 -04002477 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002478
2479 bio_get(bio);
2480
Chris Mason065631f2008-02-20 12:07:25 -05002481 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002482 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002483 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002484 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002485 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002486
Chris Masond1310b22008-01-24 16:13:08 -05002487 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2488 ret = -EOPNOTSUPP;
2489 bio_put(bio);
2490 return ret;
2491}
2492
David Woodhouse64a16702009-07-15 23:29:37 +01002493static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002494 unsigned long offset, size_t size, struct bio *bio,
2495 unsigned long bio_flags)
2496{
2497 int ret = 0;
2498 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002499 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002500 bio_flags);
2501 BUG_ON(ret < 0);
2502 return ret;
2503
2504}
2505
Chris Masond1310b22008-01-24 16:13:08 -05002506static int submit_extent_page(int rw, struct extent_io_tree *tree,
2507 struct page *page, sector_t sector,
2508 size_t size, unsigned long offset,
2509 struct block_device *bdev,
2510 struct bio **bio_ret,
2511 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002512 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002513 int mirror_num,
2514 unsigned long prev_bio_flags,
2515 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002516{
2517 int ret = 0;
2518 struct bio *bio;
2519 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002520 int contig = 0;
2521 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2522 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002523 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002524
2525 if (bio_ret && *bio_ret) {
2526 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002527 if (old_compressed)
2528 contig = bio->bi_sector == sector;
2529 else
2530 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2531 sector;
2532
2533 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002534 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002535 bio_add_page(bio, page, page_size, offset) < page_size) {
2536 ret = submit_one_bio(rw, bio, mirror_num,
2537 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002538 if (ret < 0)
2539 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002540 bio = NULL;
2541 } else {
2542 return 0;
2543 }
2544 }
Chris Masonc8b97812008-10-29 14:49:59 -04002545 if (this_compressed)
2546 nr = BIO_MAX_PAGES;
2547 else
2548 nr = bio_get_nr_vecs(bdev);
2549
Miao Xie88f794e2010-11-22 03:02:55 +00002550 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002551 if (!bio)
2552 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002553
Chris Masonc8b97812008-10-29 14:49:59 -04002554 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002555 bio->bi_end_io = end_io_func;
2556 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002557
Chris Masond3977122009-01-05 21:25:51 -05002558 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002559 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002560 else
Chris Masonc8b97812008-10-29 14:49:59 -04002561 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002562
2563 return ret;
2564}
2565
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002566void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2567{
2568 if (!PagePrivate(page)) {
2569 SetPagePrivate(page);
2570 page_cache_get(page);
2571 set_page_private(page, (unsigned long)eb);
2572 } else {
2573 WARN_ON(page->private != (unsigned long)eb);
2574 }
2575}
2576
Chris Masond1310b22008-01-24 16:13:08 -05002577void set_page_extent_mapped(struct page *page)
2578{
2579 if (!PagePrivate(page)) {
2580 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002581 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002582 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002583 }
2584}
2585
Chris Masond1310b22008-01-24 16:13:08 -05002586/*
2587 * basic readpage implementation. Locked extent state structs are inserted
2588 * into the tree that are removed when the IO is done (by the end_io
2589 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002590 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002591 */
2592static int __extent_read_full_page(struct extent_io_tree *tree,
2593 struct page *page,
2594 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002595 struct bio **bio, int mirror_num,
2596 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002597{
2598 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002599 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002600 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2601 u64 end;
2602 u64 cur = start;
2603 u64 extent_offset;
2604 u64 last_byte = i_size_read(inode);
2605 u64 block_start;
2606 u64 cur_end;
2607 sector_t sector;
2608 struct extent_map *em;
2609 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002610 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002611 int ret;
2612 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002613 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002614 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002615 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002616 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002617 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002618
2619 set_page_extent_mapped(page);
2620
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002621 if (!PageUptodate(page)) {
2622 if (cleancache_get_page(page) == 0) {
2623 BUG_ON(blocksize != PAGE_SIZE);
2624 goto out;
2625 }
2626 }
2627
Chris Masond1310b22008-01-24 16:13:08 -05002628 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002629 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002630 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002631 ordered = btrfs_lookup_ordered_extent(inode, start);
2632 if (!ordered)
2633 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002634 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002635 btrfs_start_ordered_extent(inode, ordered, 1);
2636 btrfs_put_ordered_extent(ordered);
2637 }
Chris Masond1310b22008-01-24 16:13:08 -05002638
Chris Masonc8b97812008-10-29 14:49:59 -04002639 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2640 char *userpage;
2641 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2642
2643 if (zero_offset) {
2644 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002645 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002646 memset(userpage + zero_offset, 0, iosize);
2647 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002648 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002649 }
2650 }
Chris Masond1310b22008-01-24 16:13:08 -05002651 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002652 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2653
Chris Masond1310b22008-01-24 16:13:08 -05002654 if (cur >= last_byte) {
2655 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002656 struct extent_state *cached = NULL;
2657
David Sterba306e16c2011-04-19 14:29:38 +02002658 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002659 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002660 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002661 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002662 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002663 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002664 &cached, GFP_NOFS);
2665 unlock_extent_cached(tree, cur, cur + iosize - 1,
2666 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002667 break;
2668 }
David Sterba306e16c2011-04-19 14:29:38 +02002669 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002670 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002671 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002672 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002673 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002674 break;
2675 }
Chris Masond1310b22008-01-24 16:13:08 -05002676 extent_offset = cur - em->start;
2677 BUG_ON(extent_map_end(em) <= cur);
2678 BUG_ON(end < cur);
2679
Li Zefan261507a02010-12-17 14:21:50 +08002680 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002681 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002682 extent_set_compress_type(&this_bio_flag,
2683 em->compress_type);
2684 }
Chris Masonc8b97812008-10-29 14:49:59 -04002685
Chris Masond1310b22008-01-24 16:13:08 -05002686 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2687 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002688 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002689 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2690 disk_io_size = em->block_len;
2691 sector = em->block_start >> 9;
2692 } else {
2693 sector = (em->block_start + extent_offset) >> 9;
2694 disk_io_size = iosize;
2695 }
Chris Masond1310b22008-01-24 16:13:08 -05002696 bdev = em->bdev;
2697 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002698 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2699 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002700 free_extent_map(em);
2701 em = NULL;
2702
2703 /* we've found a hole, just zero and go on */
2704 if (block_start == EXTENT_MAP_HOLE) {
2705 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002706 struct extent_state *cached = NULL;
2707
Cong Wang7ac687d2011-11-25 23:14:28 +08002708 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002709 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002710 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002711 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002712
2713 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002714 &cached, GFP_NOFS);
2715 unlock_extent_cached(tree, cur, cur + iosize - 1,
2716 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002717 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002718 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002719 continue;
2720 }
2721 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002722 if (test_range_bit(tree, cur, cur_end,
2723 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002724 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002725 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002726 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002727 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002728 continue;
2729 }
Chris Mason70dec802008-01-29 09:59:12 -05002730 /* we have an inline extent but it didn't get marked up
2731 * to date. Error out
2732 */
2733 if (block_start == EXTENT_MAP_INLINE) {
2734 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002735 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002736 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002737 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002738 continue;
2739 }
Chris Masond1310b22008-01-24 16:13:08 -05002740
Josef Bacikc8f2f242013-02-11 11:33:00 -05002741 pnr -= page->index;
2742 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002743 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002744 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002745 end_bio_extent_readpage, mirror_num,
2746 *bio_flags,
2747 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002748 if (!ret) {
2749 nr++;
2750 *bio_flags = this_bio_flag;
2751 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002752 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002753 unlock_extent(tree, cur, cur + iosize - 1);
2754 }
Chris Masond1310b22008-01-24 16:13:08 -05002755 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002756 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002757 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002758out:
Chris Masond1310b22008-01-24 16:13:08 -05002759 if (!nr) {
2760 if (!PageError(page))
2761 SetPageUptodate(page);
2762 unlock_page(page);
2763 }
2764 return 0;
2765}
2766
2767int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002768 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002769{
2770 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002771 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002772 int ret;
2773
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002774 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002775 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002776 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002777 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002778 return ret;
2779}
Chris Masond1310b22008-01-24 16:13:08 -05002780
Chris Mason11c83492009-04-20 15:50:09 -04002781static noinline void update_nr_written(struct page *page,
2782 struct writeback_control *wbc,
2783 unsigned long nr_written)
2784{
2785 wbc->nr_to_write -= nr_written;
2786 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2787 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2788 page->mapping->writeback_index = page->index + nr_written;
2789}
2790
Chris Masond1310b22008-01-24 16:13:08 -05002791/*
2792 * the writepage semantics are similar to regular writepage. extent
2793 * records are inserted to lock ranges in the tree, and as dirty areas
2794 * are found, they are marked writeback. Then the lock bits are removed
2795 * and the end_io handler clears the writeback ranges
2796 */
2797static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2798 void *data)
2799{
2800 struct inode *inode = page->mapping->host;
2801 struct extent_page_data *epd = data;
2802 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002803 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002804 u64 delalloc_start;
2805 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2806 u64 end;
2807 u64 cur = start;
2808 u64 extent_offset;
2809 u64 last_byte = i_size_read(inode);
2810 u64 block_start;
2811 u64 iosize;
2812 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002813 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002814 struct extent_map *em;
2815 struct block_device *bdev;
2816 int ret;
2817 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002818 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002819 size_t blocksize;
2820 loff_t i_size = i_size_read(inode);
2821 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2822 u64 nr_delalloc;
2823 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002824 int page_started;
2825 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002826 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002827 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002828 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002829
Chris Masonffbd5172009-04-20 15:50:09 -04002830 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002831 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002832 else
2833 write_flags = WRITE;
2834
liubo1abe9b82011-03-24 11:18:59 +00002835 trace___extent_writepage(page, inode, wbc);
2836
Chris Masond1310b22008-01-24 16:13:08 -05002837 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002838
2839 ClearPageError(page);
2840
Chris Mason7f3c74f2008-07-18 12:01:11 -04002841 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002842 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002843 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002844 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002845 unlock_page(page);
2846 return 0;
2847 }
2848
2849 if (page->index == end_index) {
2850 char *userpage;
2851
Cong Wang7ac687d2011-11-25 23:14:28 +08002852 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002853 memset(userpage + pg_offset, 0,
2854 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002855 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002856 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002857 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002858 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002859
2860 set_page_extent_mapped(page);
2861
Josef Bacik9e487102011-08-01 12:08:18 -04002862 if (!tree->ops || !tree->ops->fill_delalloc)
2863 fill_delalloc = false;
2864
Chris Masond1310b22008-01-24 16:13:08 -05002865 delalloc_start = start;
2866 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002867 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002868 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002869 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002870 /*
2871 * make sure the wbc mapping index is at least updated
2872 * to this page.
2873 */
2874 update_nr_written(page, wbc, 0);
2875
Chris Masond3977122009-01-05 21:25:51 -05002876 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002877 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002878 page,
2879 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002880 &delalloc_end,
2881 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002882 if (nr_delalloc == 0) {
2883 delalloc_start = delalloc_end + 1;
2884 continue;
2885 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002886 ret = tree->ops->fill_delalloc(inode, page,
2887 delalloc_start,
2888 delalloc_end,
2889 &page_started,
2890 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 /* File system has been set read-only */
2892 if (ret) {
2893 SetPageError(page);
2894 goto done;
2895 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002896 /*
2897 * delalloc_end is already one less than the total
2898 * length, so we don't subtract one from
2899 * PAGE_CACHE_SIZE
2900 */
2901 delalloc_to_write += (delalloc_end - delalloc_start +
2902 PAGE_CACHE_SIZE) >>
2903 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002904 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002905 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002906 if (wbc->nr_to_write < delalloc_to_write) {
2907 int thresh = 8192;
2908
2909 if (delalloc_to_write < thresh * 2)
2910 thresh = delalloc_to_write;
2911 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2912 thresh);
2913 }
Chris Masonc8b97812008-10-29 14:49:59 -04002914
Chris Mason771ed682008-11-06 22:02:51 -05002915 /* did the fill delalloc function already unlock and start
2916 * the IO?
2917 */
2918 if (page_started) {
2919 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002920 /*
2921 * we've unlocked the page, so we can't update
2922 * the mapping's writeback index, just update
2923 * nr_to_write.
2924 */
2925 wbc->nr_to_write -= nr_written;
2926 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002927 }
Chris Masonc8b97812008-10-29 14:49:59 -04002928 }
Chris Mason247e7432008-07-17 12:53:51 -04002929 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002930 ret = tree->ops->writepage_start_hook(page, start,
2931 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002932 if (ret) {
2933 /* Fixup worker will requeue */
2934 if (ret == -EBUSY)
2935 wbc->pages_skipped++;
2936 else
2937 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002938 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002939 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002940 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002941 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002942 }
2943 }
2944
Chris Mason11c83492009-04-20 15:50:09 -04002945 /*
2946 * we don't want to touch the inode after unlocking the page,
2947 * so we update the mapping writeback index now
2948 */
2949 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002950
Chris Masond1310b22008-01-24 16:13:08 -05002951 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002952 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953 if (tree->ops && tree->ops->writepage_end_io_hook)
2954 tree->ops->writepage_end_io_hook(page, start,
2955 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002956 goto done;
2957 }
2958
Chris Masond1310b22008-01-24 16:13:08 -05002959 blocksize = inode->i_sb->s_blocksize;
2960
2961 while (cur <= end) {
2962 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002963 if (tree->ops && tree->ops->writepage_end_io_hook)
2964 tree->ops->writepage_end_io_hook(page, cur,
2965 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002966 break;
2967 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002968 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002969 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002970 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002971 SetPageError(page);
2972 break;
2973 }
2974
2975 extent_offset = cur - em->start;
2976 BUG_ON(extent_map_end(em) <= cur);
2977 BUG_ON(end < cur);
2978 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00002979 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05002980 sector = (em->block_start + extent_offset) >> 9;
2981 bdev = em->bdev;
2982 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002983 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002984 free_extent_map(em);
2985 em = NULL;
2986
Chris Masonc8b97812008-10-29 14:49:59 -04002987 /*
2988 * compressed and inline extents are written through other
2989 * paths in the FS
2990 */
2991 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002992 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002993 /*
2994 * end_io notification does not happen here for
2995 * compressed extents
2996 */
2997 if (!compressed && tree->ops &&
2998 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002999 tree->ops->writepage_end_io_hook(page, cur,
3000 cur + iosize - 1,
3001 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003002 else if (compressed) {
3003 /* we don't want to end_page_writeback on
3004 * a compressed extent. this happens
3005 * elsewhere
3006 */
3007 nr++;
3008 }
3009
3010 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003011 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003012 continue;
3013 }
Chris Masond1310b22008-01-24 16:13:08 -05003014 /* leave this out until we have a page_mkwrite call */
3015 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003016 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003017 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003018 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003019 continue;
3020 }
Chris Masonc8b97812008-10-29 14:49:59 -04003021
Chris Masond1310b22008-01-24 16:13:08 -05003022 if (tree->ops && tree->ops->writepage_io_hook) {
3023 ret = tree->ops->writepage_io_hook(page, cur,
3024 cur + iosize - 1);
3025 } else {
3026 ret = 0;
3027 }
Chris Mason1259ab72008-05-12 13:39:03 -04003028 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003029 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003030 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003031 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003032
Chris Masond1310b22008-01-24 16:13:08 -05003033 set_range_writeback(tree, cur, cur + iosize - 1);
3034 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003035 printk(KERN_ERR "btrfs warning page %lu not "
3036 "writeback, cur %llu end %llu\n",
3037 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003038 (unsigned long long)end);
3039 }
3040
Chris Masonffbd5172009-04-20 15:50:09 -04003041 ret = submit_extent_page(write_flags, tree, page,
3042 sector, iosize, pg_offset,
3043 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003044 end_bio_extent_writepage,
3045 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003046 if (ret)
3047 SetPageError(page);
3048 }
3049 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003050 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003051 nr++;
3052 }
3053done:
3054 if (nr == 0) {
3055 /* make sure the mapping tag for page dirty gets cleared */
3056 set_page_writeback(page);
3057 end_page_writeback(page);
3058 }
Chris Masond1310b22008-01-24 16:13:08 -05003059 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003060
Chris Mason11c83492009-04-20 15:50:09 -04003061done_unlocked:
3062
Chris Mason2c64c532009-09-02 15:04:12 -04003063 /* drop our reference on any cached states */
3064 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003065 return 0;
3066}
3067
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003068static int eb_wait(void *word)
3069{
3070 io_schedule();
3071 return 0;
3072}
3073
3074static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3075{
3076 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3077 TASK_UNINTERRUPTIBLE);
3078}
3079
3080static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3081 struct btrfs_fs_info *fs_info,
3082 struct extent_page_data *epd)
3083{
3084 unsigned long i, num_pages;
3085 int flush = 0;
3086 int ret = 0;
3087
3088 if (!btrfs_try_tree_write_lock(eb)) {
3089 flush = 1;
3090 flush_write_bio(epd);
3091 btrfs_tree_lock(eb);
3092 }
3093
3094 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3095 btrfs_tree_unlock(eb);
3096 if (!epd->sync_io)
3097 return 0;
3098 if (!flush) {
3099 flush_write_bio(epd);
3100 flush = 1;
3101 }
Chris Masona098d8e2012-03-21 12:09:56 -04003102 while (1) {
3103 wait_on_extent_buffer_writeback(eb);
3104 btrfs_tree_lock(eb);
3105 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3106 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003107 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003108 }
3109 }
3110
Josef Bacik51561ff2012-07-20 16:25:24 -04003111 /*
3112 * We need to do this to prevent races in people who check if the eb is
3113 * under IO since we can end up having no IO bits set for a short period
3114 * of time.
3115 */
3116 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003117 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3118 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003119 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003120 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003121 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3122 -eb->len,
3123 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003124 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003125 } else {
3126 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003127 }
3128
3129 btrfs_tree_unlock(eb);
3130
3131 if (!ret)
3132 return ret;
3133
3134 num_pages = num_extent_pages(eb->start, eb->len);
3135 for (i = 0; i < num_pages; i++) {
3136 struct page *p = extent_buffer_page(eb, i);
3137
3138 if (!trylock_page(p)) {
3139 if (!flush) {
3140 flush_write_bio(epd);
3141 flush = 1;
3142 }
3143 lock_page(p);
3144 }
3145 }
3146
3147 return ret;
3148}
3149
3150static void end_extent_buffer_writeback(struct extent_buffer *eb)
3151{
3152 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3153 smp_mb__after_clear_bit();
3154 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3155}
3156
3157static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3158{
3159 int uptodate = err == 0;
3160 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3161 struct extent_buffer *eb;
3162 int done;
3163
3164 do {
3165 struct page *page = bvec->bv_page;
3166
3167 bvec--;
3168 eb = (struct extent_buffer *)page->private;
3169 BUG_ON(!eb);
3170 done = atomic_dec_and_test(&eb->io_pages);
3171
3172 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3173 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3174 ClearPageUptodate(page);
3175 SetPageError(page);
3176 }
3177
3178 end_page_writeback(page);
3179
3180 if (!done)
3181 continue;
3182
3183 end_extent_buffer_writeback(eb);
3184 } while (bvec >= bio->bi_io_vec);
3185
3186 bio_put(bio);
3187
3188}
3189
3190static int write_one_eb(struct extent_buffer *eb,
3191 struct btrfs_fs_info *fs_info,
3192 struct writeback_control *wbc,
3193 struct extent_page_data *epd)
3194{
3195 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3196 u64 offset = eb->start;
3197 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003198 unsigned long bio_flags = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003199 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003200 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003201
3202 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3203 num_pages = num_extent_pages(eb->start, eb->len);
3204 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003205 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3206 bio_flags = EXTENT_BIO_TREE_LOG;
3207
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003208 for (i = 0; i < num_pages; i++) {
3209 struct page *p = extent_buffer_page(eb, i);
3210
3211 clear_page_dirty_for_io(p);
3212 set_page_writeback(p);
3213 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3214 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3215 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003216 0, epd->bio_flags, bio_flags);
3217 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003218 if (ret) {
3219 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3220 SetPageError(p);
3221 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3222 end_extent_buffer_writeback(eb);
3223 ret = -EIO;
3224 break;
3225 }
3226 offset += PAGE_CACHE_SIZE;
3227 update_nr_written(p, wbc, 1);
3228 unlock_page(p);
3229 }
3230
3231 if (unlikely(ret)) {
3232 for (; i < num_pages; i++) {
3233 struct page *p = extent_buffer_page(eb, i);
3234 unlock_page(p);
3235 }
3236 }
3237
3238 return ret;
3239}
3240
3241int btree_write_cache_pages(struct address_space *mapping,
3242 struct writeback_control *wbc)
3243{
3244 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3245 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3246 struct extent_buffer *eb, *prev_eb = NULL;
3247 struct extent_page_data epd = {
3248 .bio = NULL,
3249 .tree = tree,
3250 .extent_locked = 0,
3251 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003252 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003253 };
3254 int ret = 0;
3255 int done = 0;
3256 int nr_to_write_done = 0;
3257 struct pagevec pvec;
3258 int nr_pages;
3259 pgoff_t index;
3260 pgoff_t end; /* Inclusive */
3261 int scanned = 0;
3262 int tag;
3263
3264 pagevec_init(&pvec, 0);
3265 if (wbc->range_cyclic) {
3266 index = mapping->writeback_index; /* Start from prev offset */
3267 end = -1;
3268 } else {
3269 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3270 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3271 scanned = 1;
3272 }
3273 if (wbc->sync_mode == WB_SYNC_ALL)
3274 tag = PAGECACHE_TAG_TOWRITE;
3275 else
3276 tag = PAGECACHE_TAG_DIRTY;
3277retry:
3278 if (wbc->sync_mode == WB_SYNC_ALL)
3279 tag_pages_for_writeback(mapping, index, end);
3280 while (!done && !nr_to_write_done && (index <= end) &&
3281 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3282 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3283 unsigned i;
3284
3285 scanned = 1;
3286 for (i = 0; i < nr_pages; i++) {
3287 struct page *page = pvec.pages[i];
3288
3289 if (!PagePrivate(page))
3290 continue;
3291
3292 if (!wbc->range_cyclic && page->index > end) {
3293 done = 1;
3294 break;
3295 }
3296
Josef Bacikb5bae262012-09-14 13:43:01 -04003297 spin_lock(&mapping->private_lock);
3298 if (!PagePrivate(page)) {
3299 spin_unlock(&mapping->private_lock);
3300 continue;
3301 }
3302
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003303 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003304
3305 /*
3306 * Shouldn't happen and normally this would be a BUG_ON
3307 * but no sense in crashing the users box for something
3308 * we can survive anyway.
3309 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003310 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003311 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003312 WARN_ON(1);
3313 continue;
3314 }
3315
Josef Bacikb5bae262012-09-14 13:43:01 -04003316 if (eb == prev_eb) {
3317 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003318 continue;
3319 }
3320
Josef Bacikb5bae262012-09-14 13:43:01 -04003321 ret = atomic_inc_not_zero(&eb->refs);
3322 spin_unlock(&mapping->private_lock);
3323 if (!ret)
3324 continue;
3325
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003326 prev_eb = eb;
3327 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3328 if (!ret) {
3329 free_extent_buffer(eb);
3330 continue;
3331 }
3332
3333 ret = write_one_eb(eb, fs_info, wbc, &epd);
3334 if (ret) {
3335 done = 1;
3336 free_extent_buffer(eb);
3337 break;
3338 }
3339 free_extent_buffer(eb);
3340
3341 /*
3342 * the filesystem may choose to bump up nr_to_write.
3343 * We have to make sure to honor the new nr_to_write
3344 * at any time
3345 */
3346 nr_to_write_done = wbc->nr_to_write <= 0;
3347 }
3348 pagevec_release(&pvec);
3349 cond_resched();
3350 }
3351 if (!scanned && !done) {
3352 /*
3353 * We hit the last page and there is more work to be done: wrap
3354 * back to the start of the file
3355 */
3356 scanned = 1;
3357 index = 0;
3358 goto retry;
3359 }
3360 flush_write_bio(&epd);
3361 return ret;
3362}
3363
Chris Masond1310b22008-01-24 16:13:08 -05003364/**
Chris Mason4bef0842008-09-08 11:18:08 -04003365 * 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 -05003366 * @mapping: address space structure to write
3367 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3368 * @writepage: function called for each page
3369 * @data: data passed to writepage function
3370 *
3371 * If a page is already under I/O, write_cache_pages() skips it, even
3372 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3373 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3374 * and msync() need to guarantee that all the data which was dirty at the time
3375 * the call was made get new I/O started against them. If wbc->sync_mode is
3376 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3377 * existing IO to complete.
3378 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003379static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003380 struct address_space *mapping,
3381 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003382 writepage_t writepage, void *data,
3383 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003384{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003385 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003386 int ret = 0;
3387 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003388 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003389 struct pagevec pvec;
3390 int nr_pages;
3391 pgoff_t index;
3392 pgoff_t end; /* Inclusive */
3393 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003394 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003395
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003396 /*
3397 * We have to hold onto the inode so that ordered extents can do their
3398 * work when the IO finishes. The alternative to this is failing to add
3399 * an ordered extent if the igrab() fails there and that is a huge pain
3400 * to deal with, so instead just hold onto the inode throughout the
3401 * writepages operation. If it fails here we are freeing up the inode
3402 * anyway and we'd rather not waste our time writing out stuff that is
3403 * going to be truncated anyway.
3404 */
3405 if (!igrab(inode))
3406 return 0;
3407
Chris Masond1310b22008-01-24 16:13:08 -05003408 pagevec_init(&pvec, 0);
3409 if (wbc->range_cyclic) {
3410 index = mapping->writeback_index; /* Start from prev offset */
3411 end = -1;
3412 } else {
3413 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3414 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003415 scanned = 1;
3416 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003417 if (wbc->sync_mode == WB_SYNC_ALL)
3418 tag = PAGECACHE_TAG_TOWRITE;
3419 else
3420 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003421retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003422 if (wbc->sync_mode == WB_SYNC_ALL)
3423 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003424 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003425 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3426 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003427 unsigned i;
3428
3429 scanned = 1;
3430 for (i = 0; i < nr_pages; i++) {
3431 struct page *page = pvec.pages[i];
3432
3433 /*
3434 * At this point we hold neither mapping->tree_lock nor
3435 * lock on the page itself: the page may be truncated or
3436 * invalidated (changing page->mapping to NULL), or even
3437 * swizzled back from swapper_space to tmpfs file
3438 * mapping
3439 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003440 if (!trylock_page(page)) {
3441 flush_fn(data);
3442 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003443 }
Chris Masond1310b22008-01-24 16:13:08 -05003444
3445 if (unlikely(page->mapping != mapping)) {
3446 unlock_page(page);
3447 continue;
3448 }
3449
3450 if (!wbc->range_cyclic && page->index > end) {
3451 done = 1;
3452 unlock_page(page);
3453 continue;
3454 }
3455
Chris Masond2c3f4f2008-11-19 12:44:22 -05003456 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003457 if (PageWriteback(page))
3458 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003459 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003460 }
Chris Masond1310b22008-01-24 16:13:08 -05003461
3462 if (PageWriteback(page) ||
3463 !clear_page_dirty_for_io(page)) {
3464 unlock_page(page);
3465 continue;
3466 }
3467
3468 ret = (*writepage)(page, wbc, data);
3469
3470 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3471 unlock_page(page);
3472 ret = 0;
3473 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003474 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003475 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003476
3477 /*
3478 * the filesystem may choose to bump up nr_to_write.
3479 * We have to make sure to honor the new nr_to_write
3480 * at any time
3481 */
3482 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003483 }
3484 pagevec_release(&pvec);
3485 cond_resched();
3486 }
3487 if (!scanned && !done) {
3488 /*
3489 * We hit the last page and there is more work to be done: wrap
3490 * back to the start of the file
3491 */
3492 scanned = 1;
3493 index = 0;
3494 goto retry;
3495 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003496 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003497 return ret;
3498}
Chris Masond1310b22008-01-24 16:13:08 -05003499
Chris Masonffbd5172009-04-20 15:50:09 -04003500static void flush_epd_write_bio(struct extent_page_data *epd)
3501{
3502 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003503 int rw = WRITE;
3504 int ret;
3505
Chris Masonffbd5172009-04-20 15:50:09 -04003506 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003507 rw = WRITE_SYNC;
3508
Josef Bacikde0022b2012-09-25 14:25:58 -04003509 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003510 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003511 epd->bio = NULL;
3512 }
3513}
3514
Chris Masond2c3f4f2008-11-19 12:44:22 -05003515static noinline void flush_write_bio(void *data)
3516{
3517 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003518 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003519}
3520
Chris Masond1310b22008-01-24 16:13:08 -05003521int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3522 get_extent_t *get_extent,
3523 struct writeback_control *wbc)
3524{
3525 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003526 struct extent_page_data epd = {
3527 .bio = NULL,
3528 .tree = tree,
3529 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003530 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003531 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003532 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003533 };
Chris Masond1310b22008-01-24 16:13:08 -05003534
Chris Masond1310b22008-01-24 16:13:08 -05003535 ret = __extent_writepage(page, wbc, &epd);
3536
Chris Masonffbd5172009-04-20 15:50:09 -04003537 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003538 return ret;
3539}
Chris Masond1310b22008-01-24 16:13:08 -05003540
Chris Mason771ed682008-11-06 22:02:51 -05003541int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3542 u64 start, u64 end, get_extent_t *get_extent,
3543 int mode)
3544{
3545 int ret = 0;
3546 struct address_space *mapping = inode->i_mapping;
3547 struct page *page;
3548 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3549 PAGE_CACHE_SHIFT;
3550
3551 struct extent_page_data epd = {
3552 .bio = NULL,
3553 .tree = tree,
3554 .get_extent = get_extent,
3555 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003556 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003557 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003558 };
3559 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003560 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003561 .nr_to_write = nr_pages * 2,
3562 .range_start = start,
3563 .range_end = end + 1,
3564 };
3565
Chris Masond3977122009-01-05 21:25:51 -05003566 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003567 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3568 if (clear_page_dirty_for_io(page))
3569 ret = __extent_writepage(page, &wbc_writepages, &epd);
3570 else {
3571 if (tree->ops && tree->ops->writepage_end_io_hook)
3572 tree->ops->writepage_end_io_hook(page, start,
3573 start + PAGE_CACHE_SIZE - 1,
3574 NULL, 1);
3575 unlock_page(page);
3576 }
3577 page_cache_release(page);
3578 start += PAGE_CACHE_SIZE;
3579 }
3580
Chris Masonffbd5172009-04-20 15:50:09 -04003581 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003582 return ret;
3583}
Chris Masond1310b22008-01-24 16:13:08 -05003584
3585int extent_writepages(struct extent_io_tree *tree,
3586 struct address_space *mapping,
3587 get_extent_t *get_extent,
3588 struct writeback_control *wbc)
3589{
3590 int ret = 0;
3591 struct extent_page_data epd = {
3592 .bio = NULL,
3593 .tree = tree,
3594 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003595 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003596 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003597 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003598 };
3599
Chris Mason4bef0842008-09-08 11:18:08 -04003600 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003601 __extent_writepage, &epd,
3602 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003603 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003604 return ret;
3605}
Chris Masond1310b22008-01-24 16:13:08 -05003606
3607int extent_readpages(struct extent_io_tree *tree,
3608 struct address_space *mapping,
3609 struct list_head *pages, unsigned nr_pages,
3610 get_extent_t get_extent)
3611{
3612 struct bio *bio = NULL;
3613 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003614 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003615 struct page *pagepool[16];
3616 struct page *page;
3617 int i = 0;
3618 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003619
Chris Masond1310b22008-01-24 16:13:08 -05003620 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003621 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003622
3623 prefetchw(&page->flags);
3624 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003625 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003626 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003627 page_cache_release(page);
3628 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003629 }
Liu Bo67c96842012-07-20 21:43:09 -06003630
3631 pagepool[nr++] = page;
3632 if (nr < ARRAY_SIZE(pagepool))
3633 continue;
3634 for (i = 0; i < nr; i++) {
3635 __extent_read_full_page(tree, pagepool[i], get_extent,
3636 &bio, 0, &bio_flags);
3637 page_cache_release(pagepool[i]);
3638 }
3639 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003640 }
Liu Bo67c96842012-07-20 21:43:09 -06003641 for (i = 0; i < nr; i++) {
3642 __extent_read_full_page(tree, pagepool[i], get_extent,
3643 &bio, 0, &bio_flags);
3644 page_cache_release(pagepool[i]);
3645 }
3646
Chris Masond1310b22008-01-24 16:13:08 -05003647 BUG_ON(!list_empty(pages));
3648 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003649 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003650 return 0;
3651}
Chris Masond1310b22008-01-24 16:13:08 -05003652
3653/*
3654 * basic invalidatepage code, this waits on any locked or writeback
3655 * ranges corresponding to the page, and then deletes any extent state
3656 * records from the tree
3657 */
3658int extent_invalidatepage(struct extent_io_tree *tree,
3659 struct page *page, unsigned long offset)
3660{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003661 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003662 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003663 u64 end = start + PAGE_CACHE_SIZE - 1;
3664 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3665
Qu Wenruofda28322013-02-26 08:10:22 +00003666 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003667 if (start > end)
3668 return 0;
3669
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003670 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003671 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003672 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003673 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3674 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003675 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003676 return 0;
3677}
Chris Masond1310b22008-01-24 16:13:08 -05003678
3679/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003680 * a helper for releasepage, this tests for areas of the page that
3681 * are locked or under IO and drops the related state bits if it is safe
3682 * to drop the page.
3683 */
3684int try_release_extent_state(struct extent_map_tree *map,
3685 struct extent_io_tree *tree, struct page *page,
3686 gfp_t mask)
3687{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003688 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003689 u64 end = start + PAGE_CACHE_SIZE - 1;
3690 int ret = 1;
3691
Chris Mason211f90e2008-07-18 11:56:15 -04003692 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003693 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003694 ret = 0;
3695 else {
3696 if ((mask & GFP_NOFS) == GFP_NOFS)
3697 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003698 /*
3699 * at this point we can safely clear everything except the
3700 * locked bit and the nodatasum bit
3701 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003702 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003703 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3704 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003705
3706 /* if clear_extent_bit failed for enomem reasons,
3707 * we can't allow the release to continue.
3708 */
3709 if (ret < 0)
3710 ret = 0;
3711 else
3712 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003713 }
3714 return ret;
3715}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003716
3717/*
Chris Masond1310b22008-01-24 16:13:08 -05003718 * a helper for releasepage. As long as there are no locked extents
3719 * in the range corresponding to the page, both state records and extent
3720 * map records are removed
3721 */
3722int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003723 struct extent_io_tree *tree, struct page *page,
3724 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003725{
3726 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003727 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003728 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003729
Chris Mason70dec802008-01-29 09:59:12 -05003730 if ((mask & __GFP_WAIT) &&
3731 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003732 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003733 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003734 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003735 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003736 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003737 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003738 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003739 break;
3740 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003741 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3742 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003743 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003744 free_extent_map(em);
3745 break;
3746 }
3747 if (!test_range_bit(tree, em->start,
3748 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003749 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003750 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003751 remove_extent_mapping(map, em);
3752 /* once for the rb tree */
3753 free_extent_map(em);
3754 }
3755 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003756 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003757
3758 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003759 free_extent_map(em);
3760 }
Chris Masond1310b22008-01-24 16:13:08 -05003761 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003762 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003763}
Chris Masond1310b22008-01-24 16:13:08 -05003764
Chris Masonec29ed52011-02-23 16:23:20 -05003765/*
3766 * helper function for fiemap, which doesn't want to see any holes.
3767 * This maps until we find something past 'last'
3768 */
3769static struct extent_map *get_extent_skip_holes(struct inode *inode,
3770 u64 offset,
3771 u64 last,
3772 get_extent_t *get_extent)
3773{
3774 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3775 struct extent_map *em;
3776 u64 len;
3777
3778 if (offset >= last)
3779 return NULL;
3780
3781 while(1) {
3782 len = last - offset;
3783 if (len == 0)
3784 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003785 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003786 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003787 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003788 return em;
3789
3790 /* if this isn't a hole return it */
3791 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3792 em->block_start != EXTENT_MAP_HOLE) {
3793 return em;
3794 }
3795
3796 /* this is a hole, advance to the next extent */
3797 offset = extent_map_end(em);
3798 free_extent_map(em);
3799 if (offset >= last)
3800 break;
3801 }
3802 return NULL;
3803}
3804
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003805int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3806 __u64 start, __u64 len, get_extent_t *get_extent)
3807{
Josef Bacik975f84f2010-11-23 19:36:57 +00003808 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003809 u64 off = start;
3810 u64 max = start + len;
3811 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003812 u32 found_type;
3813 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003814 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003815 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003816 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003817 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003818 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003819 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003820 struct btrfs_path *path;
3821 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003822 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003823 u64 em_start = 0;
3824 u64 em_len = 0;
3825 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003826 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003827
3828 if (len == 0)
3829 return -EINVAL;
3830
Josef Bacik975f84f2010-11-23 19:36:57 +00003831 path = btrfs_alloc_path();
3832 if (!path)
3833 return -ENOMEM;
3834 path->leave_spinning = 1;
3835
Josef Bacik4d479cf2011-11-17 11:34:31 -05003836 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3837 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3838
Chris Masonec29ed52011-02-23 16:23:20 -05003839 /*
3840 * lookup the last file extent. We're not using i_size here
3841 * because there might be preallocation past i_size
3842 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003843 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003844 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003845 if (ret < 0) {
3846 btrfs_free_path(path);
3847 return ret;
3848 }
3849 WARN_ON(!ret);
3850 path->slots[0]--;
3851 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3852 struct btrfs_file_extent_item);
3853 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3854 found_type = btrfs_key_type(&found_key);
3855
Chris Masonec29ed52011-02-23 16:23:20 -05003856 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003857 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003858 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003859 /* have to trust i_size as the end */
3860 last = (u64)-1;
3861 last_for_get_extent = isize;
3862 } else {
3863 /*
3864 * remember the start of the last extent. There are a
3865 * bunch of different factors that go into the length of the
3866 * extent, so its much less complex to remember where it started
3867 */
3868 last = found_key.offset;
3869 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003870 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003871 btrfs_free_path(path);
3872
Chris Masonec29ed52011-02-23 16:23:20 -05003873 /*
3874 * we might have some extents allocated but more delalloc past those
3875 * extents. so, we trust isize unless the start of the last extent is
3876 * beyond isize
3877 */
3878 if (last < isize) {
3879 last = (u64)-1;
3880 last_for_get_extent = isize;
3881 }
3882
Josef Bacik2ac55d42010-02-03 19:33:23 +00003883 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003884 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003885
Josef Bacik4d479cf2011-11-17 11:34:31 -05003886 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003887 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003888 if (!em)
3889 goto out;
3890 if (IS_ERR(em)) {
3891 ret = PTR_ERR(em);
3892 goto out;
3893 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003894
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003895 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003896 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003897
Chris Masonea8efc72011-03-08 11:54:40 -05003898 /* break if the extent we found is outside the range */
3899 if (em->start >= max || extent_map_end(em) < off)
3900 break;
3901
3902 /*
3903 * get_extent may return an extent that starts before our
3904 * requested range. We have to make sure the ranges
3905 * we return to fiemap always move forward and don't
3906 * overlap, so adjust the offsets here
3907 */
3908 em_start = max(em->start, off);
3909
3910 /*
3911 * record the offset from the start of the extent
3912 * for adjusting the disk offset below
3913 */
3914 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003915 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003916 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003917 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003918 disko = 0;
3919 flags = 0;
3920
Chris Masonea8efc72011-03-08 11:54:40 -05003921 /*
3922 * bump off for our next call to get_extent
3923 */
3924 off = extent_map_end(em);
3925 if (off >= max)
3926 end = 1;
3927
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003928 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003929 end = 1;
3930 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003931 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003932 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3933 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003934 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003935 flags |= (FIEMAP_EXTENT_DELALLOC |
3936 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003937 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003938 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003939 }
3940 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3941 flags |= FIEMAP_EXTENT_ENCODED;
3942
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003943 free_extent_map(em);
3944 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003945 if ((em_start >= last) || em_len == (u64)-1 ||
3946 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003947 flags |= FIEMAP_EXTENT_LAST;
3948 end = 1;
3949 }
3950
Chris Masonec29ed52011-02-23 16:23:20 -05003951 /* now scan forward to see if this is really the last extent. */
3952 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3953 get_extent);
3954 if (IS_ERR(em)) {
3955 ret = PTR_ERR(em);
3956 goto out;
3957 }
3958 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003959 flags |= FIEMAP_EXTENT_LAST;
3960 end = 1;
3961 }
Chris Masonec29ed52011-02-23 16:23:20 -05003962 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3963 em_len, flags);
3964 if (ret)
3965 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003966 }
3967out_free:
3968 free_extent_map(em);
3969out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003970 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3971 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003972 return ret;
3973}
3974
Chris Mason727011e2010-08-06 13:21:20 -04003975static void __free_extent_buffer(struct extent_buffer *eb)
3976{
3977#if LEAK_DEBUG
3978 unsigned long flags;
3979 spin_lock_irqsave(&leak_lock, flags);
3980 list_del(&eb->leak_list);
3981 spin_unlock_irqrestore(&leak_lock, flags);
3982#endif
Chris Mason727011e2010-08-06 13:21:20 -04003983 kmem_cache_free(extent_buffer_cache, eb);
3984}
3985
Chris Masond1310b22008-01-24 16:13:08 -05003986static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3987 u64 start,
3988 unsigned long len,
3989 gfp_t mask)
3990{
3991 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003992#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003993 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003994#endif
Chris Masond1310b22008-01-24 16:13:08 -05003995
Chris Masond1310b22008-01-24 16:13:08 -05003996 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003997 if (eb == NULL)
3998 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003999 eb->start = start;
4000 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004001 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004002 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004003 rwlock_init(&eb->lock);
4004 atomic_set(&eb->write_locks, 0);
4005 atomic_set(&eb->read_locks, 0);
4006 atomic_set(&eb->blocking_readers, 0);
4007 atomic_set(&eb->blocking_writers, 0);
4008 atomic_set(&eb->spinning_readers, 0);
4009 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004010 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004011 init_waitqueue_head(&eb->write_lock_wq);
4012 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004013
Chris Mason39351272009-02-04 09:24:05 -05004014#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004015 spin_lock_irqsave(&leak_lock, flags);
4016 list_add(&eb->leak_list, &buffers);
4017 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04004018#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05004019 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004020 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004021 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004022
David Sterbab8dae312013-02-28 14:54:18 +00004023 /*
4024 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4025 */
4026 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4027 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4028 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004029
4030 return eb;
4031}
4032
Jan Schmidt815a51c2012-05-16 17:00:02 +02004033struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4034{
4035 unsigned long i;
4036 struct page *p;
4037 struct extent_buffer *new;
4038 unsigned long num_pages = num_extent_pages(src->start, src->len);
4039
4040 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4041 if (new == NULL)
4042 return NULL;
4043
4044 for (i = 0; i < num_pages; i++) {
4045 p = alloc_page(GFP_ATOMIC);
4046 BUG_ON(!p);
4047 attach_extent_buffer_page(new, p);
4048 WARN_ON(PageDirty(p));
4049 SetPageUptodate(p);
4050 new->pages[i] = p;
4051 }
4052
4053 copy_extent_buffer(new, src, 0, 0, src->len);
4054 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4055 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4056
4057 return new;
4058}
4059
4060struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4061{
4062 struct extent_buffer *eb;
4063 unsigned long num_pages = num_extent_pages(0, len);
4064 unsigned long i;
4065
4066 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4067 if (!eb)
4068 return NULL;
4069
4070 for (i = 0; i < num_pages; i++) {
4071 eb->pages[i] = alloc_page(GFP_ATOMIC);
4072 if (!eb->pages[i])
4073 goto err;
4074 }
4075 set_extent_buffer_uptodate(eb);
4076 btrfs_set_header_nritems(eb, 0);
4077 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4078
4079 return eb;
4080err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004081 for (; i > 0; i--)
4082 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004083 __free_extent_buffer(eb);
4084 return NULL;
4085}
4086
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004087static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004088{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004089 return (atomic_read(&eb->io_pages) ||
4090 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4091 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004092}
4093
Miao Xie897ca6e2010-10-26 20:57:29 -04004094/*
4095 * Helper for releasing extent buffer page.
4096 */
4097static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4098 unsigned long start_idx)
4099{
4100 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004101 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004102 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004103 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004104
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004105 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004106
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004107 num_pages = num_extent_pages(eb->start, eb->len);
4108 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004109 if (start_idx >= index)
4110 return;
4111
4112 do {
4113 index--;
4114 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004115 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004116 spin_lock(&page->mapping->private_lock);
4117 /*
4118 * We do this since we'll remove the pages after we've
4119 * removed the eb from the radix tree, so we could race
4120 * and have this page now attached to the new eb. So
4121 * only clear page_private if it's still connected to
4122 * this eb.
4123 */
4124 if (PagePrivate(page) &&
4125 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004126 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004127 BUG_ON(PageDirty(page));
4128 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004129 /*
4130 * We need to make sure we haven't be attached
4131 * to a new eb.
4132 */
4133 ClearPagePrivate(page);
4134 set_page_private(page, 0);
4135 /* One for the page private */
4136 page_cache_release(page);
4137 }
4138 spin_unlock(&page->mapping->private_lock);
4139
Jan Schmidt815a51c2012-05-16 17:00:02 +02004140 }
4141 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004142 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004143 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004144 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004145 } while (index != start_idx);
4146}
4147
4148/*
4149 * Helper for releasing the extent buffer.
4150 */
4151static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4152{
4153 btrfs_release_extent_buffer_page(eb, 0);
4154 __free_extent_buffer(eb);
4155}
4156
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004157static void check_buffer_tree_ref(struct extent_buffer *eb)
4158{
Chris Mason242e18c2013-01-29 17:49:37 -05004159 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004160 /* the ref bit is tricky. We have to make sure it is set
4161 * if we have the buffer dirty. Otherwise the
4162 * code to free a buffer can end up dropping a dirty
4163 * page
4164 *
4165 * Once the ref bit is set, it won't go away while the
4166 * buffer is dirty or in writeback, and it also won't
4167 * go away while we have the reference count on the
4168 * eb bumped.
4169 *
4170 * We can't just set the ref bit without bumping the
4171 * ref on the eb because free_extent_buffer might
4172 * see the ref bit and try to clear it. If this happens
4173 * free_extent_buffer might end up dropping our original
4174 * ref by mistake and freeing the page before we are able
4175 * to add one more ref.
4176 *
4177 * So bump the ref count first, then set the bit. If someone
4178 * beat us to it, drop the ref we added.
4179 */
Chris Mason242e18c2013-01-29 17:49:37 -05004180 refs = atomic_read(&eb->refs);
4181 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4182 return;
4183
Josef Bacik594831c2012-07-20 16:11:08 -04004184 spin_lock(&eb->refs_lock);
4185 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004186 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004187 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004188}
4189
Josef Bacik5df42352012-03-15 18:24:42 -04004190static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4191{
4192 unsigned long num_pages, i;
4193
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004194 check_buffer_tree_ref(eb);
4195
Josef Bacik5df42352012-03-15 18:24:42 -04004196 num_pages = num_extent_pages(eb->start, eb->len);
4197 for (i = 0; i < num_pages; i++) {
4198 struct page *p = extent_buffer_page(eb, i);
4199 mark_page_accessed(p);
4200 }
4201}
4202
Chris Masond1310b22008-01-24 16:13:08 -05004203struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004204 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004205{
4206 unsigned long num_pages = num_extent_pages(start, len);
4207 unsigned long i;
4208 unsigned long index = start >> PAGE_CACHE_SHIFT;
4209 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004210 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004211 struct page *p;
4212 struct address_space *mapping = tree->mapping;
4213 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004214 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004215
Miao Xie19fe0a82010-10-26 20:57:29 -04004216 rcu_read_lock();
4217 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4218 if (eb && atomic_inc_not_zero(&eb->refs)) {
4219 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004220 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004221 return eb;
4222 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004223 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004224
David Sterbaba144192011-04-21 01:12:06 +02004225 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004226 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004227 return NULL;
4228
Chris Mason727011e2010-08-06 13:21:20 -04004229 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004230 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004231 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004232 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004233
4234 spin_lock(&mapping->private_lock);
4235 if (PagePrivate(p)) {
4236 /*
4237 * We could have already allocated an eb for this page
4238 * and attached one so lets see if we can get a ref on
4239 * the existing eb, and if we can we know it's good and
4240 * we can just return that one, else we know we can just
4241 * overwrite page->private.
4242 */
4243 exists = (struct extent_buffer *)p->private;
4244 if (atomic_inc_not_zero(&exists->refs)) {
4245 spin_unlock(&mapping->private_lock);
4246 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004247 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004248 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004249 goto free_eb;
4250 }
4251
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004252 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004253 * Do this so attach doesn't complain and we need to
4254 * drop the ref the old guy had.
4255 */
4256 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004257 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004258 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004259 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004260 attach_extent_buffer_page(eb, p);
4261 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004262 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004263 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004264 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004265 if (!PageUptodate(p))
4266 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004267
4268 /*
4269 * see below about how we avoid a nasty race with release page
4270 * and why we unlock later
4271 */
Chris Masond1310b22008-01-24 16:13:08 -05004272 }
4273 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004274 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004275again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004276 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4277 if (ret)
4278 goto free_eb;
4279
Chris Mason6af118ce2008-07-22 11:18:07 -04004280 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004281 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4282 if (ret == -EEXIST) {
4283 exists = radix_tree_lookup(&tree->buffer,
4284 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004285 if (!atomic_inc_not_zero(&exists->refs)) {
4286 spin_unlock(&tree->buffer_lock);
4287 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004288 exists = NULL;
4289 goto again;
4290 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004291 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004292 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004293 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004294 goto free_eb;
4295 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004296 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004297 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004298 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004299 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004300
4301 /*
4302 * there is a race where release page may have
4303 * tried to find this extent buffer in the radix
4304 * but failed. It will tell the VM it is safe to
4305 * reclaim the, and it will clear the page private bit.
4306 * We must make sure to set the page private bit properly
4307 * after the extent buffer is in the radix tree so
4308 * it doesn't get lost
4309 */
Chris Mason727011e2010-08-06 13:21:20 -04004310 SetPageChecked(eb->pages[0]);
4311 for (i = 1; i < num_pages; i++) {
4312 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004313 ClearPageChecked(p);
4314 unlock_page(p);
4315 }
4316 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004317 return eb;
4318
Chris Mason6af118ce2008-07-22 11:18:07 -04004319free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004320 for (i = 0; i < num_pages; i++) {
4321 if (eb->pages[i])
4322 unlock_page(eb->pages[i]);
4323 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004324
Josef Bacik17de39a2012-05-04 15:16:06 -04004325 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004326 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004327 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004328}
Chris Masond1310b22008-01-24 16:13:08 -05004329
4330struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004331 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004332{
Chris Masond1310b22008-01-24 16:13:08 -05004333 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004334
Miao Xie19fe0a82010-10-26 20:57:29 -04004335 rcu_read_lock();
4336 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4337 if (eb && atomic_inc_not_zero(&eb->refs)) {
4338 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004339 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004340 return eb;
4341 }
4342 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004343
Miao Xie19fe0a82010-10-26 20:57:29 -04004344 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004345}
Chris Masond1310b22008-01-24 16:13:08 -05004346
Josef Bacik3083ee22012-03-09 16:01:49 -05004347static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4348{
4349 struct extent_buffer *eb =
4350 container_of(head, struct extent_buffer, rcu_head);
4351
4352 __free_extent_buffer(eb);
4353}
4354
Josef Bacik3083ee22012-03-09 16:01:49 -05004355/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004356static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004357{
4358 WARN_ON(atomic_read(&eb->refs) == 0);
4359 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004360 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4361 spin_unlock(&eb->refs_lock);
4362 } else {
4363 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004364
Jan Schmidt815a51c2012-05-16 17:00:02 +02004365 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004366
Jan Schmidt815a51c2012-05-16 17:00:02 +02004367 spin_lock(&tree->buffer_lock);
4368 radix_tree_delete(&tree->buffer,
4369 eb->start >> PAGE_CACHE_SHIFT);
4370 spin_unlock(&tree->buffer_lock);
4371 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004372
4373 /* Should be safe to release our pages at this point */
4374 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004375 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004376 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004377 }
4378 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004379
4380 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004381}
4382
Chris Masond1310b22008-01-24 16:13:08 -05004383void free_extent_buffer(struct extent_buffer *eb)
4384{
Chris Mason242e18c2013-01-29 17:49:37 -05004385 int refs;
4386 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004387 if (!eb)
4388 return;
4389
Chris Mason242e18c2013-01-29 17:49:37 -05004390 while (1) {
4391 refs = atomic_read(&eb->refs);
4392 if (refs <= 3)
4393 break;
4394 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4395 if (old == refs)
4396 return;
4397 }
4398
Josef Bacik3083ee22012-03-09 16:01:49 -05004399 spin_lock(&eb->refs_lock);
4400 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004401 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4402 atomic_dec(&eb->refs);
4403
4404 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004405 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004406 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004407 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4408 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004409
Josef Bacik3083ee22012-03-09 16:01:49 -05004410 /*
4411 * I know this is terrible, but it's temporary until we stop tracking
4412 * the uptodate bits and such for the extent buffers.
4413 */
4414 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004415}
Chris Masond1310b22008-01-24 16:13:08 -05004416
Josef Bacik3083ee22012-03-09 16:01:49 -05004417void free_extent_buffer_stale(struct extent_buffer *eb)
4418{
4419 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004420 return;
4421
Josef Bacik3083ee22012-03-09 16:01:49 -05004422 spin_lock(&eb->refs_lock);
4423 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4424
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004425 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004426 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4427 atomic_dec(&eb->refs);
4428 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004429}
4430
Chris Mason1d4284b2012-03-28 20:31:37 -04004431void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004432{
Chris Masond1310b22008-01-24 16:13:08 -05004433 unsigned long i;
4434 unsigned long num_pages;
4435 struct page *page;
4436
Chris Masond1310b22008-01-24 16:13:08 -05004437 num_pages = num_extent_pages(eb->start, eb->len);
4438
4439 for (i = 0; i < num_pages; i++) {
4440 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004441 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004442 continue;
4443
Chris Masona61e6f22008-07-22 11:18:08 -04004444 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004445 WARN_ON(!PagePrivate(page));
4446
Chris Masond1310b22008-01-24 16:13:08 -05004447 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004448 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004449 if (!PageDirty(page)) {
4450 radix_tree_tag_clear(&page->mapping->page_tree,
4451 page_index(page),
4452 PAGECACHE_TAG_DIRTY);
4453 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004454 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004455 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004456 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004457 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004458 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004459}
Chris Masond1310b22008-01-24 16:13:08 -05004460
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004461int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004462{
4463 unsigned long i;
4464 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004465 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004466
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004467 check_buffer_tree_ref(eb);
4468
Chris Masonb9473432009-03-13 11:00:37 -04004469 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004470
Chris Masond1310b22008-01-24 16:13:08 -05004471 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004472 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004473 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4474
Chris Masonb9473432009-03-13 11:00:37 -04004475 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004476 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004477 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004478}
Chris Masond1310b22008-01-24 16:13:08 -05004479
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004480static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004481{
4482 if (len < PAGE_CACHE_SIZE)
4483 return 1;
4484 if (start & (PAGE_CACHE_SIZE - 1))
4485 return 1;
4486 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4487 return 1;
4488 return 0;
4489}
4490
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004491int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004492{
4493 unsigned long i;
4494 struct page *page;
4495 unsigned long num_pages;
4496
Chris Masonb4ce94d2009-02-04 09:25:08 -05004497 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004498 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004499 for (i = 0; i < num_pages; i++) {
4500 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004501 if (page)
4502 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004503 }
4504 return 0;
4505}
4506
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004507int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004508{
4509 unsigned long i;
4510 struct page *page;
4511 unsigned long num_pages;
4512
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004513 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004514 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004515 for (i = 0; i < num_pages; i++) {
4516 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004517 SetPageUptodate(page);
4518 }
4519 return 0;
4520}
Chris Masond1310b22008-01-24 16:13:08 -05004521
Chris Masonce9adaa2008-04-09 16:28:12 -04004522int extent_range_uptodate(struct extent_io_tree *tree,
4523 u64 start, u64 end)
4524{
4525 struct page *page;
4526 int ret;
4527 int pg_uptodate = 1;
4528 int uptodate;
4529 unsigned long index;
4530
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004531 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004532 ret = test_range_bit(tree, start, end,
4533 EXTENT_UPTODATE, 1, NULL);
4534 if (ret)
4535 return 1;
4536 }
Chris Masond3977122009-01-05 21:25:51 -05004537 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004538 index = start >> PAGE_CACHE_SHIFT;
4539 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004540 if (!page)
4541 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004542 uptodate = PageUptodate(page);
4543 page_cache_release(page);
4544 if (!uptodate) {
4545 pg_uptodate = 0;
4546 break;
4547 }
4548 start += PAGE_CACHE_SIZE;
4549 }
4550 return pg_uptodate;
4551}
4552
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004553int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004554{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004555 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004556}
Chris Masond1310b22008-01-24 16:13:08 -05004557
4558int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004559 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004560 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004561{
4562 unsigned long i;
4563 unsigned long start_i;
4564 struct page *page;
4565 int err;
4566 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004567 int locked_pages = 0;
4568 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004569 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004570 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004571 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004572 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004573
Chris Masonb4ce94d2009-02-04 09:25:08 -05004574 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004575 return 0;
4576
Chris Masond1310b22008-01-24 16:13:08 -05004577 if (start) {
4578 WARN_ON(start < eb->start);
4579 start_i = (start >> PAGE_CACHE_SHIFT) -
4580 (eb->start >> PAGE_CACHE_SHIFT);
4581 } else {
4582 start_i = 0;
4583 }
4584
4585 num_pages = num_extent_pages(eb->start, eb->len);
4586 for (i = start_i; i < num_pages; i++) {
4587 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004588 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004589 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004590 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004591 } else {
4592 lock_page(page);
4593 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004594 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004595 if (!PageUptodate(page)) {
4596 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004597 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004598 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004599 }
4600 if (all_uptodate) {
4601 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004602 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004603 goto unlock_exit;
4604 }
4605
Josef Bacikea466792012-03-26 21:57:36 -04004606 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004607 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004608 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004609 for (i = start_i; i < num_pages; i++) {
4610 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004611 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004612 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004613 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004614 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004615 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004616 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004617 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004618 } else {
4619 unlock_page(page);
4620 }
4621 }
4622
Jeff Mahoney355808c2011-10-03 23:23:14 -04004623 if (bio) {
4624 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004625 if (err)
4626 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004627 }
Chris Masona86c12c2008-02-07 10:50:54 -05004628
Arne Jansenbb82ab82011-06-10 14:06:53 +02004629 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004630 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004631
Chris Masond1310b22008-01-24 16:13:08 -05004632 for (i = start_i; i < num_pages; i++) {
4633 page = extent_buffer_page(eb, i);
4634 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004635 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004636 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004637 }
Chris Masond3977122009-01-05 21:25:51 -05004638
Chris Masond1310b22008-01-24 16:13:08 -05004639 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004640
4641unlock_exit:
4642 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004643 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004644 page = extent_buffer_page(eb, i);
4645 i++;
4646 unlock_page(page);
4647 locked_pages--;
4648 }
4649 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004650}
Chris Masond1310b22008-01-24 16:13:08 -05004651
4652void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4653 unsigned long start,
4654 unsigned long len)
4655{
4656 size_t cur;
4657 size_t offset;
4658 struct page *page;
4659 char *kaddr;
4660 char *dst = (char *)dstv;
4661 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4662 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004663
4664 WARN_ON(start > eb->len);
4665 WARN_ON(start + len > eb->start + eb->len);
4666
4667 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4668
Chris Masond3977122009-01-05 21:25:51 -05004669 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004670 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004671
4672 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004673 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004674 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004675
4676 dst += cur;
4677 len -= cur;
4678 offset = 0;
4679 i++;
4680 }
4681}
Chris Masond1310b22008-01-24 16:13:08 -05004682
4683int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004684 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004685 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004686 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004687{
4688 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4689 char *kaddr;
4690 struct page *p;
4691 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4692 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4693 unsigned long end_i = (start_offset + start + min_len - 1) >>
4694 PAGE_CACHE_SHIFT;
4695
4696 if (i != end_i)
4697 return -EINVAL;
4698
4699 if (i == 0) {
4700 offset = start_offset;
4701 *map_start = 0;
4702 } else {
4703 offset = 0;
4704 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4705 }
Chris Masond3977122009-01-05 21:25:51 -05004706
Chris Masond1310b22008-01-24 16:13:08 -05004707 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004708 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004709 "wanted %lu %lu\n", (unsigned long long)eb->start,
4710 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004711 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004712 }
4713
4714 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004715 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004716 *map = kaddr + offset;
4717 *map_len = PAGE_CACHE_SIZE - offset;
4718 return 0;
4719}
Chris Masond1310b22008-01-24 16:13:08 -05004720
Chris Masond1310b22008-01-24 16:13:08 -05004721int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4722 unsigned long start,
4723 unsigned long len)
4724{
4725 size_t cur;
4726 size_t offset;
4727 struct page *page;
4728 char *kaddr;
4729 char *ptr = (char *)ptrv;
4730 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4731 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4732 int ret = 0;
4733
4734 WARN_ON(start > eb->len);
4735 WARN_ON(start + len > eb->start + eb->len);
4736
4737 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4738
Chris Masond3977122009-01-05 21:25:51 -05004739 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004740 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004741
4742 cur = min(len, (PAGE_CACHE_SIZE - offset));
4743
Chris Masona6591712011-07-19 12:04:14 -04004744 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004745 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004746 if (ret)
4747 break;
4748
4749 ptr += cur;
4750 len -= cur;
4751 offset = 0;
4752 i++;
4753 }
4754 return ret;
4755}
Chris Masond1310b22008-01-24 16:13:08 -05004756
4757void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4758 unsigned long start, unsigned long len)
4759{
4760 size_t cur;
4761 size_t offset;
4762 struct page *page;
4763 char *kaddr;
4764 char *src = (char *)srcv;
4765 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4766 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4767
4768 WARN_ON(start > eb->len);
4769 WARN_ON(start + len > eb->start + eb->len);
4770
4771 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4772
Chris Masond3977122009-01-05 21:25:51 -05004773 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004774 page = extent_buffer_page(eb, i);
4775 WARN_ON(!PageUptodate(page));
4776
4777 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004778 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004779 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004780
4781 src += cur;
4782 len -= cur;
4783 offset = 0;
4784 i++;
4785 }
4786}
Chris Masond1310b22008-01-24 16:13:08 -05004787
4788void memset_extent_buffer(struct extent_buffer *eb, char c,
4789 unsigned long start, unsigned long len)
4790{
4791 size_t cur;
4792 size_t offset;
4793 struct page *page;
4794 char *kaddr;
4795 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4796 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4797
4798 WARN_ON(start > eb->len);
4799 WARN_ON(start + len > eb->start + eb->len);
4800
4801 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4802
Chris Masond3977122009-01-05 21:25:51 -05004803 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004804 page = extent_buffer_page(eb, i);
4805 WARN_ON(!PageUptodate(page));
4806
4807 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004808 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004809 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004810
4811 len -= cur;
4812 offset = 0;
4813 i++;
4814 }
4815}
Chris Masond1310b22008-01-24 16:13:08 -05004816
4817void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4818 unsigned long dst_offset, unsigned long src_offset,
4819 unsigned long len)
4820{
4821 u64 dst_len = dst->len;
4822 size_t cur;
4823 size_t offset;
4824 struct page *page;
4825 char *kaddr;
4826 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4827 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4828
4829 WARN_ON(src->len != dst_len);
4830
4831 offset = (start_offset + dst_offset) &
4832 ((unsigned long)PAGE_CACHE_SIZE - 1);
4833
Chris Masond3977122009-01-05 21:25:51 -05004834 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004835 page = extent_buffer_page(dst, i);
4836 WARN_ON(!PageUptodate(page));
4837
4838 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4839
Chris Masona6591712011-07-19 12:04:14 -04004840 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004841 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004842
4843 src_offset += cur;
4844 len -= cur;
4845 offset = 0;
4846 i++;
4847 }
4848}
Chris Masond1310b22008-01-24 16:13:08 -05004849
4850static void move_pages(struct page *dst_page, struct page *src_page,
4851 unsigned long dst_off, unsigned long src_off,
4852 unsigned long len)
4853{
Chris Masona6591712011-07-19 12:04:14 -04004854 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004855 if (dst_page == src_page) {
4856 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4857 } else {
Chris Masona6591712011-07-19 12:04:14 -04004858 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004859 char *p = dst_kaddr + dst_off + len;
4860 char *s = src_kaddr + src_off + len;
4861
4862 while (len--)
4863 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004864 }
Chris Masond1310b22008-01-24 16:13:08 -05004865}
4866
Sergei Trofimovich33872062011-04-11 21:52:52 +00004867static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4868{
4869 unsigned long distance = (src > dst) ? src - dst : dst - src;
4870 return distance < len;
4871}
4872
Chris Masond1310b22008-01-24 16:13:08 -05004873static void copy_pages(struct page *dst_page, struct page *src_page,
4874 unsigned long dst_off, unsigned long src_off,
4875 unsigned long len)
4876{
Chris Masona6591712011-07-19 12:04:14 -04004877 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004878 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004879 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004880
Sergei Trofimovich33872062011-04-11 21:52:52 +00004881 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004882 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004883 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004884 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004885 if (areas_overlap(src_off, dst_off, len))
4886 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004887 }
Chris Masond1310b22008-01-24 16:13:08 -05004888
Chris Mason727011e2010-08-06 13:21:20 -04004889 if (must_memmove)
4890 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4891 else
4892 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004893}
4894
4895void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4896 unsigned long src_offset, unsigned long len)
4897{
4898 size_t cur;
4899 size_t dst_off_in_page;
4900 size_t src_off_in_page;
4901 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4902 unsigned long dst_i;
4903 unsigned long src_i;
4904
4905 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004906 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4907 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004908 BUG_ON(1);
4909 }
4910 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004911 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4912 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004913 BUG_ON(1);
4914 }
4915
Chris Masond3977122009-01-05 21:25:51 -05004916 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004917 dst_off_in_page = (start_offset + dst_offset) &
4918 ((unsigned long)PAGE_CACHE_SIZE - 1);
4919 src_off_in_page = (start_offset + src_offset) &
4920 ((unsigned long)PAGE_CACHE_SIZE - 1);
4921
4922 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4923 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4924
4925 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4926 src_off_in_page));
4927 cur = min_t(unsigned long, cur,
4928 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4929
4930 copy_pages(extent_buffer_page(dst, dst_i),
4931 extent_buffer_page(dst, src_i),
4932 dst_off_in_page, src_off_in_page, cur);
4933
4934 src_offset += cur;
4935 dst_offset += cur;
4936 len -= cur;
4937 }
4938}
Chris Masond1310b22008-01-24 16:13:08 -05004939
4940void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4941 unsigned long src_offset, unsigned long len)
4942{
4943 size_t cur;
4944 size_t dst_off_in_page;
4945 size_t src_off_in_page;
4946 unsigned long dst_end = dst_offset + len - 1;
4947 unsigned long src_end = src_offset + len - 1;
4948 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4949 unsigned long dst_i;
4950 unsigned long src_i;
4951
4952 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004953 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4954 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004955 BUG_ON(1);
4956 }
4957 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004958 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4959 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004960 BUG_ON(1);
4961 }
Chris Mason727011e2010-08-06 13:21:20 -04004962 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004963 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4964 return;
4965 }
Chris Masond3977122009-01-05 21:25:51 -05004966 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004967 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4968 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4969
4970 dst_off_in_page = (start_offset + dst_end) &
4971 ((unsigned long)PAGE_CACHE_SIZE - 1);
4972 src_off_in_page = (start_offset + src_end) &
4973 ((unsigned long)PAGE_CACHE_SIZE - 1);
4974
4975 cur = min_t(unsigned long, len, src_off_in_page + 1);
4976 cur = min(cur, dst_off_in_page + 1);
4977 move_pages(extent_buffer_page(dst, dst_i),
4978 extent_buffer_page(dst, src_i),
4979 dst_off_in_page - cur + 1,
4980 src_off_in_page - cur + 1, cur);
4981
4982 dst_end -= cur;
4983 src_end -= cur;
4984 len -= cur;
4985 }
4986}
Chris Mason6af118ce2008-07-22 11:18:07 -04004987
Josef Bacik3083ee22012-03-09 16:01:49 -05004988int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004989{
Chris Mason6af118ce2008-07-22 11:18:07 -04004990 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004991
Miao Xie19fe0a82010-10-26 20:57:29 -04004992 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004993 * We need to make sure noboody is attaching this page to an eb right
4994 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004995 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004996 spin_lock(&page->mapping->private_lock);
4997 if (!PagePrivate(page)) {
4998 spin_unlock(&page->mapping->private_lock);
4999 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005000 }
5001
Josef Bacik3083ee22012-03-09 16:01:49 -05005002 eb = (struct extent_buffer *)page->private;
5003 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005004
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005005 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005006 * This is a little awful but should be ok, we need to make sure that
5007 * the eb doesn't disappear out from under us while we're looking at
5008 * this page.
5009 */
5010 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005011 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005012 spin_unlock(&eb->refs_lock);
5013 spin_unlock(&page->mapping->private_lock);
5014 return 0;
5015 }
5016 spin_unlock(&page->mapping->private_lock);
5017
5018 if ((mask & GFP_NOFS) == GFP_NOFS)
5019 mask = GFP_NOFS;
5020
5021 /*
5022 * If tree ref isn't set then we know the ref on this eb is a real ref,
5023 * so just return, this page will likely be freed soon anyway.
5024 */
5025 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5026 spin_unlock(&eb->refs_lock);
5027 return 0;
5028 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005029
Josef Bacike64860a2012-07-20 16:05:36 -04005030 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04005031}