blob: a4a7a18cdd3d2c9555523d54f3b4e0ff83aa14a3 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Chris Masond1310b22008-01-24 16:13:08 -050022
Chris Masond1310b22008-01-24 16:13:08 -050023static struct kmem_cache *extent_state_cache;
24static struct kmem_cache *extent_buffer_cache;
25
26static LIST_HEAD(buffers);
27static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040028
Chris Masonb47eda82008-11-10 12:34:40 -050029#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050030#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050031static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040032#endif
Chris Masond1310b22008-01-24 16:13:08 -050033
Chris Masond1310b22008-01-24 16:13:08 -050034#define BUFFER_LRU_MAX 64
35
36struct tree_entry {
37 u64 start;
38 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050039 struct rb_node rb_node;
40};
41
42struct extent_page_data {
43 struct bio *bio;
44 struct extent_io_tree *tree;
45 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050046
47 /* tells writepage not to lock the state bits for this range
48 * it still does the unlocking
49 */
Chris Masonffbd5172009-04-20 15:50:09 -040050 unsigned int extent_locked:1;
51
52 /* tells the submit_bio code to use a WRITE_SYNC */
53 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050054};
55
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040056static inline struct btrfs_fs_info *
57tree_fs_info(struct extent_io_tree *tree)
58{
59 return btrfs_sb(tree->mapping->host->i_sb);
60}
61
Chris Masond1310b22008-01-24 16:13:08 -050062int __init extent_io_init(void)
63{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020064 extent_state_cache = kmem_cache_create("extent_state",
65 sizeof(struct extent_state), 0,
66 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050067 if (!extent_state_cache)
68 return -ENOMEM;
69
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020070 extent_buffer_cache = kmem_cache_create("extent_buffers",
71 sizeof(struct extent_buffer), 0,
72 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050073 if (!extent_buffer_cache)
74 goto free_state_cache;
75 return 0;
76
77free_state_cache:
78 kmem_cache_destroy(extent_state_cache);
79 return -ENOMEM;
80}
81
82void extent_io_exit(void)
83{
84 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040085 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050086
87 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040088 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050089 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
90 "state %lu in tree %p refs %d\n",
91 (unsigned long long)state->start,
92 (unsigned long long)state->end,
93 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040094 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050095 kmem_cache_free(extent_state_cache, state);
96
97 }
98
Chris Mason2d2ae542008-03-26 16:24:23 -040099 while (!list_empty(&buffers)) {
100 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500101 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
102 "refs %d\n", (unsigned long long)eb->start,
103 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400104 list_del(&eb->leak_list);
105 kmem_cache_free(extent_buffer_cache, eb);
106 }
Chris Masond1310b22008-01-24 16:13:08 -0500107 if (extent_state_cache)
108 kmem_cache_destroy(extent_state_cache);
109 if (extent_buffer_cache)
110 kmem_cache_destroy(extent_buffer_cache);
111}
112
113void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200114 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500115{
Eric Paris6bef4d32010-02-23 19:43:04 +0000116 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400117 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500118 tree->ops = NULL;
119 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500120 spin_lock_init(&tree->lock);
Chris Mason6af118c2008-07-22 11:18:07 -0400121 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500122 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500123}
Chris Masond1310b22008-01-24 16:13:08 -0500124
Christoph Hellwigb2950862008-12-02 09:54:17 -0500125static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500126{
127 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400130#endif
Chris Masond1310b22008-01-24 16:13:08 -0500131
132 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400133 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500134 return state;
135 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500136 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500137 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500138#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400139 spin_lock_irqsave(&leak_lock, flags);
140 list_add(&state->leak_list, &states);
141 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400142#endif
Chris Masond1310b22008-01-24 16:13:08 -0500143 atomic_set(&state->refs, 1);
144 init_waitqueue_head(&state->wq);
145 return state;
146}
Chris Masond1310b22008-01-24 16:13:08 -0500147
Chris Mason4845e442010-05-25 20:56:50 -0400148void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500149{
Chris Masond1310b22008-01-24 16:13:08 -0500150 if (!state)
151 return;
152 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500153#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400154 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400155#endif
Chris Mason70dec802008-01-29 09:59:12 -0500156 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500157#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400158 spin_lock_irqsave(&leak_lock, flags);
159 list_del(&state->leak_list);
160 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400161#endif
Chris Masond1310b22008-01-24 16:13:08 -0500162 kmem_cache_free(extent_state_cache, state);
163 }
164}
Chris Masond1310b22008-01-24 16:13:08 -0500165
166static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
167 struct rb_node *node)
168{
Chris Masond3977122009-01-05 21:25:51 -0500169 struct rb_node **p = &root->rb_node;
170 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500171 struct tree_entry *entry;
172
Chris Masond3977122009-01-05 21:25:51 -0500173 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500174 parent = *p;
175 entry = rb_entry(parent, struct tree_entry, rb_node);
176
177 if (offset < entry->start)
178 p = &(*p)->rb_left;
179 else if (offset > entry->end)
180 p = &(*p)->rb_right;
181 else
182 return parent;
183 }
184
185 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500186 rb_link_node(node, parent, p);
187 rb_insert_color(node, root);
188 return NULL;
189}
190
Chris Mason80ea96b2008-02-01 14:51:59 -0500191static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500192 struct rb_node **prev_ret,
193 struct rb_node **next_ret)
194{
Chris Mason80ea96b2008-02-01 14:51:59 -0500195 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500196 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500197 struct rb_node *prev = NULL;
198 struct rb_node *orig_prev = NULL;
199 struct tree_entry *entry;
200 struct tree_entry *prev_entry = NULL;
201
Chris Masond3977122009-01-05 21:25:51 -0500202 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500203 entry = rb_entry(n, struct tree_entry, rb_node);
204 prev = n;
205 prev_entry = entry;
206
207 if (offset < entry->start)
208 n = n->rb_left;
209 else if (offset > entry->end)
210 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500211 else
Chris Masond1310b22008-01-24 16:13:08 -0500212 return n;
213 }
214
215 if (prev_ret) {
216 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_next(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *prev_ret = prev;
222 prev = orig_prev;
223 }
224
225 if (next_ret) {
226 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500227 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500228 prev = rb_prev(prev);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 }
231 *next_ret = prev;
232 }
233 return NULL;
234}
235
Chris Mason80ea96b2008-02-01 14:51:59 -0500236static inline struct rb_node *tree_search(struct extent_io_tree *tree,
237 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500238{
Chris Mason70dec802008-01-29 09:59:12 -0500239 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500240 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500241
Chris Mason80ea96b2008-02-01 14:51:59 -0500242 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500243 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500244 return prev;
245 return ret;
246}
247
Josef Bacik9ed74f22009-09-11 16:12:44 -0400248static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
249 struct extent_state *other)
250{
251 if (tree->ops && tree->ops->merge_extent_hook)
252 tree->ops->merge_extent_hook(tree->mapping->host, new,
253 other);
254}
255
Chris Masond1310b22008-01-24 16:13:08 -0500256/*
257 * utility function to look for merge candidates inside a given range.
258 * Any extents with matching state are merged together into a single
259 * extent in the tree. Extents with EXTENT_IO in their state field
260 * are not merged because the end_io handlers need to be able to do
261 * operations on them without sleeping (or doing allocations/splits).
262 *
263 * This should be called with the tree lock held.
264 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000265static void merge_state(struct extent_io_tree *tree,
266 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500267{
268 struct extent_state *other;
269 struct rb_node *other_node;
270
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400271 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000272 return;
Chris Masond1310b22008-01-24 16:13:08 -0500273
274 other_node = rb_prev(&state->rb_node);
275 if (other_node) {
276 other = rb_entry(other_node, struct extent_state, rb_node);
277 if (other->end == state->start - 1 &&
278 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400279 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500280 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500281 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500282 rb_erase(&other->rb_node, &tree->state);
283 free_extent_state(other);
284 }
285 }
286 other_node = rb_next(&state->rb_node);
287 if (other_node) {
288 other = rb_entry(other_node, struct extent_state, rb_node);
289 if (other->start == state->end + 1 &&
290 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400291 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400292 state->end = other->end;
293 other->tree = NULL;
294 rb_erase(&other->rb_node, &tree->state);
295 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500296 }
297 }
Chris Masond1310b22008-01-24 16:13:08 -0500298}
299
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000300static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400301 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500302{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000303 if (tree->ops && tree->ops->set_bit_hook)
304 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500305}
306
307static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400308 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500309{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400310 if (tree->ops && tree->ops->clear_bit_hook)
311 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500312}
313
Xiao Guangrong3150b692011-07-14 03:19:08 +0000314static void set_state_bits(struct extent_io_tree *tree,
315 struct extent_state *state, int *bits);
316
Chris Masond1310b22008-01-24 16:13:08 -0500317/*
318 * insert an extent_state struct into the tree. 'bits' are set on the
319 * struct before it is inserted.
320 *
321 * This may return -EEXIST if the extent is already there, in which case the
322 * state struct is freed.
323 *
324 * The tree lock is not taken internally. This is a utility function and
325 * probably isn't what you want to call (see set/clear_extent_bit).
326 */
327static int insert_state(struct extent_io_tree *tree,
328 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400329 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500330{
331 struct rb_node *node;
332
333 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500334 printk(KERN_ERR "btrfs end < start %llu %llu\n",
335 (unsigned long long)end,
336 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500337 WARN_ON(1);
338 }
Chris Masond1310b22008-01-24 16:13:08 -0500339 state->start = start;
340 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400341
Xiao Guangrong3150b692011-07-14 03:19:08 +0000342 set_state_bits(tree, state, bits);
343
Chris Masond1310b22008-01-24 16:13:08 -0500344 node = tree_insert(&tree->state, end, &state->rb_node);
345 if (node) {
346 struct extent_state *found;
347 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500348 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
349 "%llu %llu\n", (unsigned long long)found->start,
350 (unsigned long long)found->end,
351 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500352 return -EEXIST;
353 }
Chris Mason70dec802008-01-29 09:59:12 -0500354 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500355 merge_state(tree, state);
356 return 0;
357}
358
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000359static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360 u64 split)
361{
362 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000363 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400364}
365
Chris Masond1310b22008-01-24 16:13:08 -0500366/*
367 * split a given extent state struct in two, inserting the preallocated
368 * struct 'prealloc' as the newly created second half. 'split' indicates an
369 * offset inside 'orig' where it should be split.
370 *
371 * Before calling,
372 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
373 * are two extent state structs in the tree:
374 * prealloc: [orig->start, split - 1]
375 * orig: [ split, orig->end ]
376 *
377 * The tree locks are not taken by this function. They need to be held
378 * by the caller.
379 */
380static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
381 struct extent_state *prealloc, u64 split)
382{
383 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400384
385 split_cb(tree, orig, split);
386
Chris Masond1310b22008-01-24 16:13:08 -0500387 prealloc->start = orig->start;
388 prealloc->end = split - 1;
389 prealloc->state = orig->state;
390 orig->start = split;
391
392 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
393 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500394 free_extent_state(prealloc);
395 return -EEXIST;
396 }
Chris Mason70dec802008-01-29 09:59:12 -0500397 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500398 return 0;
399}
400
401/*
402 * utility function to clear some bits in an extent state struct.
403 * it will optionally wake up any one waiting on this state (wake == 1), or
404 * forcibly remove the state from the tree (delete == 1).
405 *
406 * If no bits are set on the state struct after clearing things, the
407 * struct is freed and removed from the tree
408 */
409static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400410 struct extent_state *state,
411 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500412{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400414 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500415
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400416 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500417 u64 range = state->end - state->start + 1;
418 WARN_ON(range > tree->dirty_bytes);
419 tree->dirty_bytes -= range;
420 }
Chris Mason291d6732008-01-29 15:55:23 -0500421 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400422 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500423 if (wake)
424 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400425 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500426 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500427 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500428 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500429 free_extent_state(state);
430 } else {
431 WARN_ON(1);
432 }
433 } else {
434 merge_state(tree, state);
435 }
436 return ret;
437}
438
Xiao Guangrong82337672011-04-20 06:44:57 +0000439static struct extent_state *
440alloc_extent_state_atomic(struct extent_state *prealloc)
441{
442 if (!prealloc)
443 prealloc = alloc_extent_state(GFP_ATOMIC);
444
445 return prealloc;
446}
447
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400448void extent_io_tree_panic(struct extent_io_tree *tree, int err)
449{
450 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
451 "Extent tree was modified by another "
452 "thread while locked.");
453}
454
Chris Masond1310b22008-01-24 16:13:08 -0500455/*
456 * clear some bits on a range in the tree. This may require splitting
457 * or inserting elements in the tree, so the gfp mask is used to
458 * indicate which allocations or sleeping are allowed.
459 *
460 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
461 * the given range from the tree regardless of state (ie for truncate).
462 *
463 * the range [start, end] is inclusive.
464 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100465 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500466 */
467int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400468 int bits, int wake, int delete,
469 struct extent_state **cached_state,
470 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500471{
472 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400473 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500474 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400475 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500476 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400477 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500478 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000479 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500480
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400481 if (delete)
482 bits |= ~EXTENT_CTLBITS;
483 bits |= EXTENT_FIRST_DELALLOC;
484
Josef Bacik2ac55d42010-02-03 19:33:23 +0000485 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
486 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500487again:
488 if (!prealloc && (mask & __GFP_WAIT)) {
489 prealloc = alloc_extent_state(mask);
490 if (!prealloc)
491 return -ENOMEM;
492 }
493
Chris Masoncad321a2008-12-17 14:51:42 -0500494 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400495 if (cached_state) {
496 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000497
498 if (clear) {
499 *cached_state = NULL;
500 cached_state = NULL;
501 }
502
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400503 if (cached && cached->tree && cached->start <= start &&
504 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000505 if (clear)
506 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400507 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400508 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400509 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000510 if (clear)
511 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400512 }
Chris Masond1310b22008-01-24 16:13:08 -0500513 /*
514 * this search will find the extents that end after
515 * our range starts
516 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500517 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500518 if (!node)
519 goto out;
520 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400521hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500522 if (state->start > end)
523 goto out;
524 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400525 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500526
Liu Bo04493142012-02-16 18:34:37 +0800527 if (state->end < end && !need_resched())
528 next_node = rb_next(&state->rb_node);
529 else
530 next_node = NULL;
531
532 /* the state doesn't have the wanted bits, go ahead */
533 if (!(state->state & bits))
534 goto next;
535
Chris Masond1310b22008-01-24 16:13:08 -0500536 /*
537 * | ---- desired range ---- |
538 * | state | or
539 * | ------------- state -------------- |
540 *
541 * We need to split the extent we found, and may flip
542 * bits on second half.
543 *
544 * If the extent we found extends past our range, we
545 * just split and search again. It'll get split again
546 * the next time though.
547 *
548 * If the extent we found is inside our range, we clear
549 * the desired bit on it.
550 */
551
552 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000553 prealloc = alloc_extent_state_atomic(prealloc);
554 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500555 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400556 if (err)
557 extent_io_tree_panic(tree, err);
558
Chris Masond1310b22008-01-24 16:13:08 -0500559 prealloc = NULL;
560 if (err)
561 goto out;
562 if (state->end <= end) {
Jeff Mahoney6763af82012-03-01 14:56:29 +0100563 clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400564 if (last_end == (u64)-1)
565 goto out;
566 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500567 }
568 goto search_again;
569 }
570 /*
571 * | ---- desired range ---- |
572 * | state |
573 * We need to split the extent, and clear the bit
574 * on the first half
575 */
576 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000577 prealloc = alloc_extent_state_atomic(prealloc);
578 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500579 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400580 if (err)
581 extent_io_tree_panic(tree, err);
582
Chris Masond1310b22008-01-24 16:13:08 -0500583 if (wake)
584 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400585
Jeff Mahoney6763af82012-03-01 14:56:29 +0100586 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400587
Chris Masond1310b22008-01-24 16:13:08 -0500588 prealloc = NULL;
589 goto out;
590 }
Chris Mason42daec22009-09-23 19:51:09 -0400591
Jeff Mahoney6763af82012-03-01 14:56:29 +0100592 clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800593next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400594 if (last_end == (u64)-1)
595 goto out;
596 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400597 if (start <= end && next_node) {
598 state = rb_entry(next_node, struct extent_state,
599 rb_node);
Liu Bo692e5752012-02-16 18:34:36 +0800600 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400601 }
Chris Masond1310b22008-01-24 16:13:08 -0500602 goto search_again;
603
604out:
Chris Masoncad321a2008-12-17 14:51:42 -0500605 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500606 if (prealloc)
607 free_extent_state(prealloc);
608
Jeff Mahoney6763af82012-03-01 14:56:29 +0100609 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500610
611search_again:
612 if (start > end)
613 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500614 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500615 if (mask & __GFP_WAIT)
616 cond_resched();
617 goto again;
618}
Chris Masond1310b22008-01-24 16:13:08 -0500619
620static int wait_on_state(struct extent_io_tree *tree,
621 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500622 __releases(tree->lock)
623 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500624{
625 DEFINE_WAIT(wait);
626 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500627 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500628 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500629 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630 finish_wait(&state->wq, &wait);
631 return 0;
632}
633
634/*
635 * waits for one or more bits to clear on a range in the state tree.
636 * The range [start, end] is inclusive.
637 * The tree lock is taken by this function
638 */
639int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
640{
641 struct extent_state *state;
642 struct rb_node *node;
643
Chris Masoncad321a2008-12-17 14:51:42 -0500644 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500645again:
646 while (1) {
647 /*
648 * this search will find all the extents that end after
649 * our range starts
650 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500651 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500652 if (!node)
653 break;
654
655 state = rb_entry(node, struct extent_state, rb_node);
656
657 if (state->start > end)
658 goto out;
659
660 if (state->state & bits) {
661 start = state->start;
662 atomic_inc(&state->refs);
663 wait_on_state(tree, state);
664 free_extent_state(state);
665 goto again;
666 }
667 start = state->end + 1;
668
669 if (start > end)
670 break;
671
Xiao Guangrongded91f02011-07-14 03:19:27 +0000672 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500673 }
674out:
Chris Masoncad321a2008-12-17 14:51:42 -0500675 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500676 return 0;
677}
Chris Masond1310b22008-01-24 16:13:08 -0500678
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000679static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500680 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400681 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500682{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400683 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400684
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000685 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400686 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500687 u64 range = state->end - state->start + 1;
688 tree->dirty_bytes += range;
689 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500691}
692
Chris Mason2c64c532009-09-02 15:04:12 -0400693static void cache_state(struct extent_state *state,
694 struct extent_state **cached_ptr)
695{
696 if (cached_ptr && !(*cached_ptr)) {
697 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
698 *cached_ptr = state;
699 atomic_inc(&state->refs);
700 }
701 }
702}
703
Arne Jansen507903b2011-04-06 10:02:20 +0000704static void uncache_state(struct extent_state **cached_ptr)
705{
706 if (cached_ptr && (*cached_ptr)) {
707 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400708 *cached_ptr = NULL;
709 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000710 }
711}
712
Chris Masond1310b22008-01-24 16:13:08 -0500713/*
Chris Mason1edbb732009-09-02 13:24:36 -0400714 * set some bits on a range in the tree. This may require allocations or
715 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500716 *
Chris Mason1edbb732009-09-02 13:24:36 -0400717 * If any of the exclusive bits are set, this will fail with -EEXIST if some
718 * part of the range already has the desired bits set. The start of the
719 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500720 *
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500722 */
Chris Mason1edbb732009-09-02 13:24:36 -0400723
Chris Mason4845e442010-05-25 20:56:50 -0400724int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
725 int bits, int exclusive_bits, u64 *failed_start,
726 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500727{
728 struct extent_state *state;
729 struct extent_state *prealloc = NULL;
730 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500731 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500732 u64 last_start;
733 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400734
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400735 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500736again:
737 if (!prealloc && (mask & __GFP_WAIT)) {
738 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000739 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500740 }
741
Chris Masoncad321a2008-12-17 14:51:42 -0500742 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400743 if (cached_state && *cached_state) {
744 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400745 if (state->start <= start && state->end > start &&
746 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400747 node = &state->rb_node;
748 goto hit_next;
749 }
750 }
Chris Masond1310b22008-01-24 16:13:08 -0500751 /*
752 * this search will find all the extents that end after
753 * our range starts.
754 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500755 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500756 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000757 prealloc = alloc_extent_state_atomic(prealloc);
758 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400759 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400760 if (err)
761 extent_io_tree_panic(tree, err);
762
Chris Masond1310b22008-01-24 16:13:08 -0500763 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500764 goto out;
765 }
Chris Masond1310b22008-01-24 16:13:08 -0500766 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400767hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500768 last_start = state->start;
769 last_end = state->end;
770
771 /*
772 * | ---- desired range ---- |
773 * | state |
774 *
775 * Just lock what we found and keep going
776 */
777 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400778 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400779 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500780 *failed_start = state->start;
781 err = -EEXIST;
782 goto out;
783 }
Chris Mason42daec22009-09-23 19:51:09 -0400784
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000785 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400786
Chris Mason2c64c532009-09-02 15:04:12 -0400787 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500788 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400789 if (last_end == (u64)-1)
790 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400791
Yan Zheng5c939df2009-05-27 09:16:03 -0400792 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400793 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000794 if (next_node && start < end && prealloc && !need_resched()) {
795 state = rb_entry(next_node, struct extent_state,
796 rb_node);
797 if (state->start == start)
798 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400799 }
Chris Masond1310b22008-01-24 16:13:08 -0500800 goto search_again;
801 }
802
803 /*
804 * | ---- desired range ---- |
805 * | state |
806 * or
807 * | ------------- state -------------- |
808 *
809 * We need to split the extent we found, and may flip bits on
810 * second half.
811 *
812 * If the extent we found extends past our
813 * range, we just split and search again. It'll get split
814 * again the next time though.
815 *
816 * If the extent we found is inside our range, we set the
817 * desired bit on it.
818 */
819 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400820 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500821 *failed_start = start;
822 err = -EEXIST;
823 goto out;
824 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000825
826 prealloc = alloc_extent_state_atomic(prealloc);
827 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500828 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400829 if (err)
830 extent_io_tree_panic(tree, err);
831
Chris Masond1310b22008-01-24 16:13:08 -0500832 prealloc = NULL;
833 if (err)
834 goto out;
835 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000836 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400837 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500838 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400839 if (last_end == (u64)-1)
840 goto out;
841 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500842 }
843 goto search_again;
844 }
845 /*
846 * | ---- desired range ---- |
847 * | state | or | state |
848 *
849 * There's a hole, we need to insert something in it and
850 * ignore the extent we found.
851 */
852 if (state->start > start) {
853 u64 this_end;
854 if (end < last_start)
855 this_end = end;
856 else
Chris Masond3977122009-01-05 21:25:51 -0500857 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000858
859 prealloc = alloc_extent_state_atomic(prealloc);
860 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000861
862 /*
863 * Avoid to free 'prealloc' if it can be merged with
864 * the later extent.
865 */
Chris Masond1310b22008-01-24 16:13:08 -0500866 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400867 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400868 if (err)
869 extent_io_tree_panic(tree, err);
870
Chris Mason2c64c532009-09-02 15:04:12 -0400871 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500872 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500873 start = this_end + 1;
874 goto search_again;
875 }
876 /*
877 * | ---- desired range ---- |
878 * | state |
879 * We need to split the extent, and set the bit
880 * on the first half
881 */
882 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400883 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500884 *failed_start = start;
885 err = -EEXIST;
886 goto out;
887 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000888
889 prealloc = alloc_extent_state_atomic(prealloc);
890 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500891 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400892 if (err)
893 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500894
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000895 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400896 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500897 merge_state(tree, prealloc);
898 prealloc = NULL;
899 goto out;
900 }
901
902 goto search_again;
903
904out:
Chris Masoncad321a2008-12-17 14:51:42 -0500905 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500906 if (prealloc)
907 free_extent_state(prealloc);
908
909 return err;
910
911search_again:
912 if (start > end)
913 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500914 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500915 if (mask & __GFP_WAIT)
916 cond_resched();
917 goto again;
918}
Chris Masond1310b22008-01-24 16:13:08 -0500919
Josef Bacik462d6fa2011-09-26 13:56:12 -0400920/**
921 * convert_extent - convert all bits in a given range from one bit to another
922 * @tree: the io tree to search
923 * @start: the start offset in bytes
924 * @end: the end offset in bytes (inclusive)
925 * @bits: the bits to set in this range
926 * @clear_bits: the bits to clear in this range
927 * @mask: the allocation mask
928 *
929 * This will go through and set bits for the given range. If any states exist
930 * already in this range they are set with the given bit and cleared of the
931 * clear_bits. This is only meant to be used by things that are mergeable, ie
932 * converting from say DELALLOC to DIRTY. This is not meant to be used with
933 * boundary bits like LOCK.
934 */
935int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
936 int bits, int clear_bits, gfp_t mask)
937{
938 struct extent_state *state;
939 struct extent_state *prealloc = NULL;
940 struct rb_node *node;
941 int err = 0;
942 u64 last_start;
943 u64 last_end;
944
945again:
946 if (!prealloc && (mask & __GFP_WAIT)) {
947 prealloc = alloc_extent_state(mask);
948 if (!prealloc)
949 return -ENOMEM;
950 }
951
952 spin_lock(&tree->lock);
953 /*
954 * this search will find all the extents that end after
955 * our range starts.
956 */
957 node = tree_search(tree, start);
958 if (!node) {
959 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500960 if (!prealloc) {
961 err = -ENOMEM;
962 goto out;
963 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400964 err = insert_state(tree, prealloc, start, end, &bits);
965 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400966 if (err)
967 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400968 goto out;
969 }
970 state = rb_entry(node, struct extent_state, rb_node);
971hit_next:
972 last_start = state->start;
973 last_end = state->end;
974
975 /*
976 * | ---- desired range ---- |
977 * | state |
978 *
979 * Just lock what we found and keep going
980 */
981 if (state->start == start && state->end <= end) {
982 struct rb_node *next_node;
983
984 set_state_bits(tree, state, &bits);
985 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400986 if (last_end == (u64)-1)
987 goto out;
988
989 start = last_end + 1;
990 next_node = rb_next(&state->rb_node);
991 if (next_node && start < end && prealloc && !need_resched()) {
992 state = rb_entry(next_node, struct extent_state,
993 rb_node);
994 if (state->start == start)
995 goto hit_next;
996 }
997 goto search_again;
998 }
999
1000 /*
1001 * | ---- desired range ---- |
1002 * | state |
1003 * or
1004 * | ------------- state -------------- |
1005 *
1006 * We need to split the extent we found, and may flip bits on
1007 * second half.
1008 *
1009 * If the extent we found extends past our
1010 * range, we just split and search again. It'll get split
1011 * again the next time though.
1012 *
1013 * If the extent we found is inside our range, we set the
1014 * desired bit on it.
1015 */
1016 if (state->start < start) {
1017 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001018 if (!prealloc) {
1019 err = -ENOMEM;
1020 goto out;
1021 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001022 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001023 if (err)
1024 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001025 prealloc = NULL;
1026 if (err)
1027 goto out;
1028 if (state->end <= end) {
1029 set_state_bits(tree, state, &bits);
1030 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001031 if (last_end == (u64)-1)
1032 goto out;
1033 start = last_end + 1;
1034 }
1035 goto search_again;
1036 }
1037 /*
1038 * | ---- desired range ---- |
1039 * | state | or | state |
1040 *
1041 * There's a hole, we need to insert something in it and
1042 * ignore the extent we found.
1043 */
1044 if (state->start > start) {
1045 u64 this_end;
1046 if (end < last_start)
1047 this_end = end;
1048 else
1049 this_end = last_start - 1;
1050
1051 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001052 if (!prealloc) {
1053 err = -ENOMEM;
1054 goto out;
1055 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001056
1057 /*
1058 * Avoid to free 'prealloc' if it can be merged with
1059 * the later extent.
1060 */
1061 err = insert_state(tree, prealloc, start, this_end,
1062 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001063 if (err)
1064 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001065 prealloc = NULL;
1066 start = this_end + 1;
1067 goto search_again;
1068 }
1069 /*
1070 * | ---- desired range ---- |
1071 * | state |
1072 * We need to split the extent, and set the bit
1073 * on the first half
1074 */
1075 if (state->start <= end && state->end > end) {
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 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001083 if (err)
1084 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001085
1086 set_state_bits(tree, prealloc, &bits);
1087 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001088 prealloc = NULL;
1089 goto out;
1090 }
1091
1092 goto search_again;
1093
1094out:
1095 spin_unlock(&tree->lock);
1096 if (prealloc)
1097 free_extent_state(prealloc);
1098
1099 return err;
1100
1101search_again:
1102 if (start > end)
1103 goto out;
1104 spin_unlock(&tree->lock);
1105 if (mask & __GFP_WAIT)
1106 cond_resched();
1107 goto again;
1108}
1109
Chris Masond1310b22008-01-24 16:13:08 -05001110/* wrappers around set/clear extent bit */
1111int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1112 gfp_t mask)
1113{
1114 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001115 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001116}
Chris Masond1310b22008-01-24 16:13:08 -05001117
1118int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1119 int bits, gfp_t mask)
1120{
1121 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001122 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001123}
Chris Masond1310b22008-01-24 16:13:08 -05001124
1125int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1126 int bits, gfp_t mask)
1127{
Chris Mason2c64c532009-09-02 15:04:12 -04001128 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001129}
Chris Masond1310b22008-01-24 16:13:08 -05001130
1131int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001132 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001133{
1134 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001135 EXTENT_DELALLOC | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001136 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001137}
Chris Masond1310b22008-01-24 16:13:08 -05001138
1139int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1140 gfp_t mask)
1141{
1142 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001143 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001144 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001145}
Chris Masond1310b22008-01-24 16:13:08 -05001146
1147int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1148 gfp_t mask)
1149{
1150 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001151 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001152}
Chris Masond1310b22008-01-24 16:13:08 -05001153
Chris Masond1310b22008-01-24 16:13:08 -05001154int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001155 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001156{
Arne Jansen507903b2011-04-06 10:02:20 +00001157 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1158 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001159}
Chris Masond1310b22008-01-24 16:13:08 -05001160
Chris Masond3977122009-01-05 21:25:51 -05001161static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001162 u64 end, struct extent_state **cached_state,
1163 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001164{
Chris Mason2c64c532009-09-02 15:04:12 -04001165 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001166 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001167}
Chris Masond1310b22008-01-24 16:13:08 -05001168
Chris Masond352ac62008-09-29 15:18:18 -04001169/*
1170 * either insert or lock state struct between start and end use mask to tell
1171 * us if waiting is desired.
1172 */
Chris Mason1edbb732009-09-02 13:24:36 -04001173int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001174 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001175{
1176 int err;
1177 u64 failed_start;
1178 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001179 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001180 EXTENT_LOCKED, &failed_start,
1181 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001182 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1183 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1184 start = failed_start;
1185 } else {
1186 break;
1187 }
1188 WARN_ON(start > end);
1189 }
1190 return err;
1191}
Chris Masond1310b22008-01-24 16:13:08 -05001192
Chris Mason1edbb732009-09-02 13:24:36 -04001193int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1194{
Chris Mason2c64c532009-09-02 15:04:12 -04001195 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001196}
1197
Josef Bacik25179202008-10-29 14:49:05 -04001198int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1199 gfp_t mask)
1200{
1201 int err;
1202 u64 failed_start;
1203
Chris Mason2c64c532009-09-02 15:04:12 -04001204 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1205 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001206 if (err == -EEXIST) {
1207 if (failed_start > start)
1208 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001209 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001210 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001211 }
Josef Bacik25179202008-10-29 14:49:05 -04001212 return 1;
1213}
Josef Bacik25179202008-10-29 14:49:05 -04001214
Chris Mason2c64c532009-09-02 15:04:12 -04001215int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1216 struct extent_state **cached, gfp_t mask)
1217{
1218 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1219 mask);
1220}
1221
Arne Jansen507903b2011-04-06 10:02:20 +00001222int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001223{
Chris Mason2c64c532009-09-02 15:04:12 -04001224 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1225 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001226}
Chris Masond1310b22008-01-24 16:13:08 -05001227
1228/*
Chris Masond1310b22008-01-24 16:13:08 -05001229 * helper function to set both pages and extents in the tree writeback
1230 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001231static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001232{
1233 unsigned long index = start >> PAGE_CACHE_SHIFT;
1234 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1235 struct page *page;
1236
1237 while (index <= end_index) {
1238 page = find_get_page(tree->mapping, index);
1239 BUG_ON(!page);
1240 set_page_writeback(page);
1241 page_cache_release(page);
1242 index++;
1243 }
Chris Masond1310b22008-01-24 16:13:08 -05001244 return 0;
1245}
Chris Masond1310b22008-01-24 16:13:08 -05001246
Chris Masond352ac62008-09-29 15:18:18 -04001247/* find the first state struct with 'bits' set after 'start', and
1248 * return it. tree->lock must be held. NULL will returned if
1249 * nothing was found after 'start'
1250 */
Chris Masond7fc6402008-02-18 12:12:38 -05001251struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1252 u64 start, int bits)
1253{
1254 struct rb_node *node;
1255 struct extent_state *state;
1256
1257 /*
1258 * this search will find all the extents that end after
1259 * our range starts.
1260 */
1261 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001262 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001263 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001264
Chris Masond3977122009-01-05 21:25:51 -05001265 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001266 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001267 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001268 return state;
Chris Masond3977122009-01-05 21:25:51 -05001269
Chris Masond7fc6402008-02-18 12:12:38 -05001270 node = rb_next(node);
1271 if (!node)
1272 break;
1273 }
1274out:
1275 return NULL;
1276}
Chris Masond7fc6402008-02-18 12:12:38 -05001277
Chris Masond352ac62008-09-29 15:18:18 -04001278/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001279 * find the first offset in the io tree with 'bits' set. zero is
1280 * returned if we find something, and *start_ret and *end_ret are
1281 * set to reflect the state struct that was found.
1282 *
1283 * If nothing was found, 1 is returned, < 0 on error
1284 */
1285int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1286 u64 *start_ret, u64 *end_ret, int bits)
1287{
1288 struct extent_state *state;
1289 int ret = 1;
1290
1291 spin_lock(&tree->lock);
1292 state = find_first_extent_bit_state(tree, start, bits);
1293 if (state) {
1294 *start_ret = state->start;
1295 *end_ret = state->end;
1296 ret = 0;
1297 }
1298 spin_unlock(&tree->lock);
1299 return ret;
1300}
1301
1302/*
Chris Masond352ac62008-09-29 15:18:18 -04001303 * find a contiguous range of bytes in the file marked as delalloc, not
1304 * more than 'max_bytes'. start and end are used to return the range,
1305 *
1306 * 1 is returned if we find something, 0 if nothing was in the tree
1307 */
Chris Masonc8b97812008-10-29 14:49:59 -04001308static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001309 u64 *start, u64 *end, u64 max_bytes,
1310 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001311{
1312 struct rb_node *node;
1313 struct extent_state *state;
1314 u64 cur_start = *start;
1315 u64 found = 0;
1316 u64 total_bytes = 0;
1317
Chris Masoncad321a2008-12-17 14:51:42 -05001318 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001319
Chris Masond1310b22008-01-24 16:13:08 -05001320 /*
1321 * this search will find all the extents that end after
1322 * our range starts.
1323 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001324 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001325 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001326 if (!found)
1327 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001328 goto out;
1329 }
1330
Chris Masond3977122009-01-05 21:25:51 -05001331 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001332 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001333 if (found && (state->start != cur_start ||
1334 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001335 goto out;
1336 }
1337 if (!(state->state & EXTENT_DELALLOC)) {
1338 if (!found)
1339 *end = state->end;
1340 goto out;
1341 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001342 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001343 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001344 *cached_state = state;
1345 atomic_inc(&state->refs);
1346 }
Chris Masond1310b22008-01-24 16:13:08 -05001347 found++;
1348 *end = state->end;
1349 cur_start = state->end + 1;
1350 node = rb_next(node);
1351 if (!node)
1352 break;
1353 total_bytes += state->end - state->start + 1;
1354 if (total_bytes >= max_bytes)
1355 break;
1356 }
1357out:
Chris Masoncad321a2008-12-17 14:51:42 -05001358 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001359 return found;
1360}
1361
Chris Masonc8b97812008-10-29 14:49:59 -04001362static noinline int __unlock_for_delalloc(struct inode *inode,
1363 struct page *locked_page,
1364 u64 start, u64 end)
1365{
1366 int ret;
1367 struct page *pages[16];
1368 unsigned long index = start >> PAGE_CACHE_SHIFT;
1369 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1370 unsigned long nr_pages = end_index - index + 1;
1371 int i;
1372
1373 if (index == locked_page->index && end_index == index)
1374 return 0;
1375
Chris Masond3977122009-01-05 21:25:51 -05001376 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001377 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001378 min_t(unsigned long, nr_pages,
1379 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001380 for (i = 0; i < ret; i++) {
1381 if (pages[i] != locked_page)
1382 unlock_page(pages[i]);
1383 page_cache_release(pages[i]);
1384 }
1385 nr_pages -= ret;
1386 index += ret;
1387 cond_resched();
1388 }
1389 return 0;
1390}
1391
1392static noinline int lock_delalloc_pages(struct inode *inode,
1393 struct page *locked_page,
1394 u64 delalloc_start,
1395 u64 delalloc_end)
1396{
1397 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1398 unsigned long start_index = index;
1399 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1400 unsigned long pages_locked = 0;
1401 struct page *pages[16];
1402 unsigned long nrpages;
1403 int ret;
1404 int i;
1405
1406 /* the caller is responsible for locking the start index */
1407 if (index == locked_page->index && index == end_index)
1408 return 0;
1409
1410 /* skip the page at the start index */
1411 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001412 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001413 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001414 min_t(unsigned long,
1415 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001416 if (ret == 0) {
1417 ret = -EAGAIN;
1418 goto done;
1419 }
1420 /* now we have an array of pages, lock them all */
1421 for (i = 0; i < ret; i++) {
1422 /*
1423 * the caller is taking responsibility for
1424 * locked_page
1425 */
Chris Mason771ed682008-11-06 22:02:51 -05001426 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001427 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001428 if (!PageDirty(pages[i]) ||
1429 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001430 ret = -EAGAIN;
1431 unlock_page(pages[i]);
1432 page_cache_release(pages[i]);
1433 goto done;
1434 }
1435 }
Chris Masonc8b97812008-10-29 14:49:59 -04001436 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001437 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001438 }
Chris Masonc8b97812008-10-29 14:49:59 -04001439 nrpages -= ret;
1440 index += ret;
1441 cond_resched();
1442 }
1443 ret = 0;
1444done:
1445 if (ret && pages_locked) {
1446 __unlock_for_delalloc(inode, locked_page,
1447 delalloc_start,
1448 ((u64)(start_index + pages_locked - 1)) <<
1449 PAGE_CACHE_SHIFT);
1450 }
1451 return ret;
1452}
1453
1454/*
1455 * find a contiguous range of bytes in the file marked as delalloc, not
1456 * more than 'max_bytes'. start and end are used to return the range,
1457 *
1458 * 1 is returned if we find something, 0 if nothing was in the tree
1459 */
1460static noinline u64 find_lock_delalloc_range(struct inode *inode,
1461 struct extent_io_tree *tree,
1462 struct page *locked_page,
1463 u64 *start, u64 *end,
1464 u64 max_bytes)
1465{
1466 u64 delalloc_start;
1467 u64 delalloc_end;
1468 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001469 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001470 int ret;
1471 int loops = 0;
1472
1473again:
1474 /* step one, find a bunch of delalloc bytes starting at start */
1475 delalloc_start = *start;
1476 delalloc_end = 0;
1477 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001478 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001479 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001480 *start = delalloc_start;
1481 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001482 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001483 return found;
1484 }
1485
1486 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001487 * start comes from the offset of locked_page. We have to lock
1488 * pages in order, so we can't process delalloc bytes before
1489 * locked_page
1490 */
Chris Masond3977122009-01-05 21:25:51 -05001491 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001492 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001493
1494 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001495 * make sure to limit the number of pages we try to lock down
1496 * if we're looping.
1497 */
Chris Masond3977122009-01-05 21:25:51 -05001498 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001499 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001500
Chris Masonc8b97812008-10-29 14:49:59 -04001501 /* step two, lock all the pages after the page that has start */
1502 ret = lock_delalloc_pages(inode, locked_page,
1503 delalloc_start, delalloc_end);
1504 if (ret == -EAGAIN) {
1505 /* some of the pages are gone, lets avoid looping by
1506 * shortening the size of the delalloc range we're searching
1507 */
Chris Mason9655d292009-09-02 15:22:30 -04001508 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001509 if (!loops) {
1510 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1511 max_bytes = PAGE_CACHE_SIZE - offset;
1512 loops = 1;
1513 goto again;
1514 } else {
1515 found = 0;
1516 goto out_failed;
1517 }
1518 }
1519 BUG_ON(ret);
1520
1521 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001522 lock_extent_bits(tree, delalloc_start, delalloc_end,
1523 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001524
1525 /* then test to make sure it is all still delalloc */
1526 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001527 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001528 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001529 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1530 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001531 __unlock_for_delalloc(inode, locked_page,
1532 delalloc_start, delalloc_end);
1533 cond_resched();
1534 goto again;
1535 }
Chris Mason9655d292009-09-02 15:22:30 -04001536 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001537 *start = delalloc_start;
1538 *end = delalloc_end;
1539out_failed:
1540 return found;
1541}
1542
1543int extent_clear_unlock_delalloc(struct inode *inode,
1544 struct extent_io_tree *tree,
1545 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001546 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001547{
1548 int ret;
1549 struct page *pages[16];
1550 unsigned long index = start >> PAGE_CACHE_SHIFT;
1551 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1552 unsigned long nr_pages = end_index - index + 1;
1553 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001554 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001555
Chris Masona791e352009-10-08 11:27:10 -04001556 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001557 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001558 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001559 clear_bits |= EXTENT_DIRTY;
1560
Chris Masona791e352009-10-08 11:27:10 -04001561 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001562 clear_bits |= EXTENT_DELALLOC;
1563
Chris Mason2c64c532009-09-02 15:04:12 -04001564 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001565 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1566 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1567 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001568 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001569
Chris Masond3977122009-01-05 21:25:51 -05001570 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001571 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001572 min_t(unsigned long,
1573 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001574 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001575
Chris Masona791e352009-10-08 11:27:10 -04001576 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001577 SetPagePrivate2(pages[i]);
1578
Chris Masonc8b97812008-10-29 14:49:59 -04001579 if (pages[i] == locked_page) {
1580 page_cache_release(pages[i]);
1581 continue;
1582 }
Chris Masona791e352009-10-08 11:27:10 -04001583 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001584 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001585 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001586 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001587 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001588 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001589 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001590 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001591 page_cache_release(pages[i]);
1592 }
1593 nr_pages -= ret;
1594 index += ret;
1595 cond_resched();
1596 }
1597 return 0;
1598}
Chris Masonc8b97812008-10-29 14:49:59 -04001599
Chris Masond352ac62008-09-29 15:18:18 -04001600/*
1601 * count the number of bytes in the tree that have a given bit(s)
1602 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1603 * cached. The total number found is returned.
1604 */
Chris Masond1310b22008-01-24 16:13:08 -05001605u64 count_range_bits(struct extent_io_tree *tree,
1606 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001607 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001608{
1609 struct rb_node *node;
1610 struct extent_state *state;
1611 u64 cur_start = *start;
1612 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001613 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001614 int found = 0;
1615
1616 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001617 WARN_ON(1);
1618 return 0;
1619 }
1620
Chris Masoncad321a2008-12-17 14:51:42 -05001621 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001622 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1623 total_bytes = tree->dirty_bytes;
1624 goto out;
1625 }
1626 /*
1627 * this search will find all the extents that end after
1628 * our range starts.
1629 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001630 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001631 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001632 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001633
Chris Masond3977122009-01-05 21:25:51 -05001634 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001635 state = rb_entry(node, struct extent_state, rb_node);
1636 if (state->start > search_end)
1637 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001638 if (contig && found && state->start > last + 1)
1639 break;
1640 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001641 total_bytes += min(search_end, state->end) + 1 -
1642 max(cur_start, state->start);
1643 if (total_bytes >= max_bytes)
1644 break;
1645 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001646 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001647 found = 1;
1648 }
Chris Masonec29ed52011-02-23 16:23:20 -05001649 last = state->end;
1650 } else if (contig && found) {
1651 break;
Chris Masond1310b22008-01-24 16:13:08 -05001652 }
1653 node = rb_next(node);
1654 if (!node)
1655 break;
1656 }
1657out:
Chris Masoncad321a2008-12-17 14:51:42 -05001658 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001659 return total_bytes;
1660}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001661
Chris Masond352ac62008-09-29 15:18:18 -04001662/*
1663 * set the private field for a given byte offset in the tree. If there isn't
1664 * an extent_state there already, this does nothing.
1665 */
Chris Masond1310b22008-01-24 16:13:08 -05001666int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1667{
1668 struct rb_node *node;
1669 struct extent_state *state;
1670 int ret = 0;
1671
Chris Masoncad321a2008-12-17 14:51:42 -05001672 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001673 /*
1674 * this search will find all the extents that end after
1675 * our range starts.
1676 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001677 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001678 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001679 ret = -ENOENT;
1680 goto out;
1681 }
1682 state = rb_entry(node, struct extent_state, rb_node);
1683 if (state->start != start) {
1684 ret = -ENOENT;
1685 goto out;
1686 }
1687 state->private = private;
1688out:
Chris Masoncad321a2008-12-17 14:51:42 -05001689 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001690 return ret;
1691}
1692
1693int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1694{
1695 struct rb_node *node;
1696 struct extent_state *state;
1697 int ret = 0;
1698
Chris Masoncad321a2008-12-17 14:51:42 -05001699 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001700 /*
1701 * this search will find all the extents that end after
1702 * our range starts.
1703 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001704 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001705 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001706 ret = -ENOENT;
1707 goto out;
1708 }
1709 state = rb_entry(node, struct extent_state, rb_node);
1710 if (state->start != start) {
1711 ret = -ENOENT;
1712 goto out;
1713 }
1714 *private = state->private;
1715out:
Chris Masoncad321a2008-12-17 14:51:42 -05001716 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001717 return ret;
1718}
1719
1720/*
1721 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001722 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001723 * has the bits set. Otherwise, 1 is returned if any bit in the
1724 * range is found set.
1725 */
1726int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001727 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001728{
1729 struct extent_state *state = NULL;
1730 struct rb_node *node;
1731 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001732
Chris Masoncad321a2008-12-17 14:51:42 -05001733 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001734 if (cached && cached->tree && cached->start <= start &&
1735 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001736 node = &cached->rb_node;
1737 else
1738 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001739 while (node && start <= end) {
1740 state = rb_entry(node, struct extent_state, rb_node);
1741
1742 if (filled && state->start > start) {
1743 bitset = 0;
1744 break;
1745 }
1746
1747 if (state->start > end)
1748 break;
1749
1750 if (state->state & bits) {
1751 bitset = 1;
1752 if (!filled)
1753 break;
1754 } else if (filled) {
1755 bitset = 0;
1756 break;
1757 }
Chris Mason46562ce2009-09-23 20:23:16 -04001758
1759 if (state->end == (u64)-1)
1760 break;
1761
Chris Masond1310b22008-01-24 16:13:08 -05001762 start = state->end + 1;
1763 if (start > end)
1764 break;
1765 node = rb_next(node);
1766 if (!node) {
1767 if (filled)
1768 bitset = 0;
1769 break;
1770 }
1771 }
Chris Masoncad321a2008-12-17 14:51:42 -05001772 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001773 return bitset;
1774}
Chris Masond1310b22008-01-24 16:13:08 -05001775
1776/*
1777 * helper function to set a given page up to date if all the
1778 * extents in the tree for that page are up to date
1779 */
1780static int check_page_uptodate(struct extent_io_tree *tree,
1781 struct page *page)
1782{
1783 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1784 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001785 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001786 SetPageUptodate(page);
1787 return 0;
1788}
1789
1790/*
1791 * helper function to unlock a page if all the extents in the tree
1792 * for that page are unlocked
1793 */
1794static int check_page_locked(struct extent_io_tree *tree,
1795 struct page *page)
1796{
1797 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1798 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001799 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001800 unlock_page(page);
1801 return 0;
1802}
1803
1804/*
1805 * helper function to end page writeback if all the extents
1806 * in the tree for that page are done with writeback
1807 */
1808static int check_page_writeback(struct extent_io_tree *tree,
1809 struct page *page)
1810{
Chris Mason1edbb732009-09-02 13:24:36 -04001811 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001812 return 0;
1813}
1814
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001815/*
1816 * When IO fails, either with EIO or csum verification fails, we
1817 * try other mirrors that might have a good copy of the data. This
1818 * io_failure_record is used to record state as we go through all the
1819 * mirrors. If another mirror has good data, the page is set up to date
1820 * and things continue. If a good mirror can't be found, the original
1821 * bio end_io callback is called to indicate things have failed.
1822 */
1823struct io_failure_record {
1824 struct page *page;
1825 u64 start;
1826 u64 len;
1827 u64 logical;
1828 unsigned long bio_flags;
1829 int this_mirror;
1830 int failed_mirror;
1831 int in_validation;
1832};
1833
1834static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1835 int did_repair)
1836{
1837 int ret;
1838 int err = 0;
1839 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1840
1841 set_state_private(failure_tree, rec->start, 0);
1842 ret = clear_extent_bits(failure_tree, rec->start,
1843 rec->start + rec->len - 1,
1844 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1845 if (ret)
1846 err = ret;
1847
1848 if (did_repair) {
1849 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1850 rec->start + rec->len - 1,
1851 EXTENT_DAMAGED, GFP_NOFS);
1852 if (ret && !err)
1853 err = ret;
1854 }
1855
1856 kfree(rec);
1857 return err;
1858}
1859
1860static void repair_io_failure_callback(struct bio *bio, int err)
1861{
1862 complete(bio->bi_private);
1863}
1864
1865/*
1866 * this bypasses the standard btrfs submit functions deliberately, as
1867 * the standard behavior is to write all copies in a raid setup. here we only
1868 * want to write the one bad copy. so we do the mapping for ourselves and issue
1869 * submit_bio directly.
1870 * to avoid any synchonization issues, wait for the data after writing, which
1871 * actually prevents the read that triggered the error from finishing.
1872 * currently, there can be no more than two copies of every data bit. thus,
1873 * exactly one rewrite is required.
1874 */
1875int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1876 u64 length, u64 logical, struct page *page,
1877 int mirror_num)
1878{
1879 struct bio *bio;
1880 struct btrfs_device *dev;
1881 DECLARE_COMPLETION_ONSTACK(compl);
1882 u64 map_length = 0;
1883 u64 sector;
1884 struct btrfs_bio *bbio = NULL;
1885 int ret;
1886
1887 BUG_ON(!mirror_num);
1888
1889 bio = bio_alloc(GFP_NOFS, 1);
1890 if (!bio)
1891 return -EIO;
1892 bio->bi_private = &compl;
1893 bio->bi_end_io = repair_io_failure_callback;
1894 bio->bi_size = 0;
1895 map_length = length;
1896
1897 ret = btrfs_map_block(map_tree, WRITE, logical,
1898 &map_length, &bbio, mirror_num);
1899 if (ret) {
1900 bio_put(bio);
1901 return -EIO;
1902 }
1903 BUG_ON(mirror_num != bbio->mirror_num);
1904 sector = bbio->stripes[mirror_num-1].physical >> 9;
1905 bio->bi_sector = sector;
1906 dev = bbio->stripes[mirror_num-1].dev;
1907 kfree(bbio);
1908 if (!dev || !dev->bdev || !dev->writeable) {
1909 bio_put(bio);
1910 return -EIO;
1911 }
1912 bio->bi_bdev = dev->bdev;
1913 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001914 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001915 wait_for_completion(&compl);
1916
1917 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1918 /* try to remap that extent elsewhere? */
1919 bio_put(bio);
1920 return -EIO;
1921 }
1922
1923 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1924 "sector %llu)\n", page->mapping->host->i_ino, start,
1925 dev->name, sector);
1926
1927 bio_put(bio);
1928 return 0;
1929}
1930
1931/*
1932 * each time an IO finishes, we do a fast check in the IO failure tree
1933 * to see if we need to process or clean up an io_failure_record
1934 */
1935static int clean_io_failure(u64 start, struct page *page)
1936{
1937 u64 private;
1938 u64 private_failure;
1939 struct io_failure_record *failrec;
1940 struct btrfs_mapping_tree *map_tree;
1941 struct extent_state *state;
1942 int num_copies;
1943 int did_repair = 0;
1944 int ret;
1945 struct inode *inode = page->mapping->host;
1946
1947 private = 0;
1948 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1949 (u64)-1, 1, EXTENT_DIRTY, 0);
1950 if (!ret)
1951 return 0;
1952
1953 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1954 &private_failure);
1955 if (ret)
1956 return 0;
1957
1958 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1959 BUG_ON(!failrec->this_mirror);
1960
1961 if (failrec->in_validation) {
1962 /* there was no real error, just free the record */
1963 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1964 failrec->start);
1965 did_repair = 1;
1966 goto out;
1967 }
1968
1969 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1970 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1971 failrec->start,
1972 EXTENT_LOCKED);
1973 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1974
1975 if (state && state->start == failrec->start) {
1976 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1977 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1978 failrec->len);
1979 if (num_copies > 1) {
1980 ret = repair_io_failure(map_tree, start, failrec->len,
1981 failrec->logical, page,
1982 failrec->failed_mirror);
1983 did_repair = !ret;
1984 }
1985 }
1986
1987out:
1988 if (!ret)
1989 ret = free_io_failure(inode, failrec, did_repair);
1990
1991 return ret;
1992}
1993
1994/*
1995 * this is a generic handler for readpage errors (default
1996 * readpage_io_failed_hook). if other copies exist, read those and write back
1997 * good data to the failed position. does not investigate in remapping the
1998 * failed extent elsewhere, hoping the device will be smart enough to do this as
1999 * needed
2000 */
2001
2002static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2003 u64 start, u64 end, int failed_mirror,
2004 struct extent_state *state)
2005{
2006 struct io_failure_record *failrec = NULL;
2007 u64 private;
2008 struct extent_map *em;
2009 struct inode *inode = page->mapping->host;
2010 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2011 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2012 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2013 struct bio *bio;
2014 int num_copies;
2015 int ret;
2016 int read_mode;
2017 u64 logical;
2018
2019 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2020
2021 ret = get_state_private(failure_tree, start, &private);
2022 if (ret) {
2023 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2024 if (!failrec)
2025 return -ENOMEM;
2026 failrec->start = start;
2027 failrec->len = end - start + 1;
2028 failrec->this_mirror = 0;
2029 failrec->bio_flags = 0;
2030 failrec->in_validation = 0;
2031
2032 read_lock(&em_tree->lock);
2033 em = lookup_extent_mapping(em_tree, start, failrec->len);
2034 if (!em) {
2035 read_unlock(&em_tree->lock);
2036 kfree(failrec);
2037 return -EIO;
2038 }
2039
2040 if (em->start > start || em->start + em->len < start) {
2041 free_extent_map(em);
2042 em = NULL;
2043 }
2044 read_unlock(&em_tree->lock);
2045
2046 if (!em || IS_ERR(em)) {
2047 kfree(failrec);
2048 return -EIO;
2049 }
2050 logical = start - em->start;
2051 logical = em->block_start + logical;
2052 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2053 logical = em->block_start;
2054 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2055 extent_set_compress_type(&failrec->bio_flags,
2056 em->compress_type);
2057 }
2058 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2059 "len=%llu\n", logical, start, failrec->len);
2060 failrec->logical = logical;
2061 free_extent_map(em);
2062
2063 /* set the bits in the private failure tree */
2064 ret = set_extent_bits(failure_tree, start, end,
2065 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2066 if (ret >= 0)
2067 ret = set_state_private(failure_tree, start,
2068 (u64)(unsigned long)failrec);
2069 /* set the bits in the inode's tree */
2070 if (ret >= 0)
2071 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2072 GFP_NOFS);
2073 if (ret < 0) {
2074 kfree(failrec);
2075 return ret;
2076 }
2077 } else {
2078 failrec = (struct io_failure_record *)(unsigned long)private;
2079 pr_debug("bio_readpage_error: (found) logical=%llu, "
2080 "start=%llu, len=%llu, validation=%d\n",
2081 failrec->logical, failrec->start, failrec->len,
2082 failrec->in_validation);
2083 /*
2084 * when data can be on disk more than twice, add to failrec here
2085 * (e.g. with a list for failed_mirror) to make
2086 * clean_io_failure() clean all those errors at once.
2087 */
2088 }
2089 num_copies = btrfs_num_copies(
2090 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2091 failrec->logical, failrec->len);
2092 if (num_copies == 1) {
2093 /*
2094 * we only have a single copy of the data, so don't bother with
2095 * all the retry and error correction code that follows. no
2096 * matter what the error is, it is very likely to persist.
2097 */
2098 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2099 "state=%p, num_copies=%d, next_mirror %d, "
2100 "failed_mirror %d\n", state, num_copies,
2101 failrec->this_mirror, failed_mirror);
2102 free_io_failure(inode, failrec, 0);
2103 return -EIO;
2104 }
2105
2106 if (!state) {
2107 spin_lock(&tree->lock);
2108 state = find_first_extent_bit_state(tree, failrec->start,
2109 EXTENT_LOCKED);
2110 if (state && state->start != failrec->start)
2111 state = NULL;
2112 spin_unlock(&tree->lock);
2113 }
2114
2115 /*
2116 * there are two premises:
2117 * a) deliver good data to the caller
2118 * b) correct the bad sectors on disk
2119 */
2120 if (failed_bio->bi_vcnt > 1) {
2121 /*
2122 * to fulfill b), we need to know the exact failing sectors, as
2123 * we don't want to rewrite any more than the failed ones. thus,
2124 * we need separate read requests for the failed bio
2125 *
2126 * if the following BUG_ON triggers, our validation request got
2127 * merged. we need separate requests for our algorithm to work.
2128 */
2129 BUG_ON(failrec->in_validation);
2130 failrec->in_validation = 1;
2131 failrec->this_mirror = failed_mirror;
2132 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2133 } else {
2134 /*
2135 * we're ready to fulfill a) and b) alongside. get a good copy
2136 * of the failed sector and if we succeed, we have setup
2137 * everything for repair_io_failure to do the rest for us.
2138 */
2139 if (failrec->in_validation) {
2140 BUG_ON(failrec->this_mirror != failed_mirror);
2141 failrec->in_validation = 0;
2142 failrec->this_mirror = 0;
2143 }
2144 failrec->failed_mirror = failed_mirror;
2145 failrec->this_mirror++;
2146 if (failrec->this_mirror == failed_mirror)
2147 failrec->this_mirror++;
2148 read_mode = READ_SYNC;
2149 }
2150
2151 if (!state || failrec->this_mirror > num_copies) {
2152 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2153 "next_mirror %d, failed_mirror %d\n", state,
2154 num_copies, failrec->this_mirror, failed_mirror);
2155 free_io_failure(inode, failrec, 0);
2156 return -EIO;
2157 }
2158
2159 bio = bio_alloc(GFP_NOFS, 1);
2160 bio->bi_private = state;
2161 bio->bi_end_io = failed_bio->bi_end_io;
2162 bio->bi_sector = failrec->logical >> 9;
2163 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2164 bio->bi_size = 0;
2165
2166 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2167
2168 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2169 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2170 failrec->this_mirror, num_copies, failrec->in_validation);
2171
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002172 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2173 failrec->this_mirror,
2174 failrec->bio_flags, 0);
2175 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002176}
2177
Chris Masond1310b22008-01-24 16:13:08 -05002178/* lots and lots of room for performance fixes in the end_bio funcs */
2179
Jeff Mahoney87826df2012-02-15 16:23:57 +01002180int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2181{
2182 int uptodate = (err == 0);
2183 struct extent_io_tree *tree;
2184 int ret;
2185
2186 tree = &BTRFS_I(page->mapping->host)->io_tree;
2187
2188 if (tree->ops && tree->ops->writepage_end_io_hook) {
2189 ret = tree->ops->writepage_end_io_hook(page, start,
2190 end, NULL, uptodate);
2191 if (ret)
2192 uptodate = 0;
2193 }
2194
2195 if (!uptodate && tree->ops &&
2196 tree->ops->writepage_io_failed_hook) {
2197 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2198 start, end, NULL);
2199 /* Writeback already completed */
2200 if (ret == 0)
2201 return 1;
2202 }
2203
2204 if (!uptodate) {
2205 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2206 ClearPageUptodate(page);
2207 SetPageError(page);
2208 }
2209 return 0;
2210}
2211
Chris Masond1310b22008-01-24 16:13:08 -05002212/*
2213 * after a writepage IO is done, we need to:
2214 * clear the uptodate bits on error
2215 * clear the writeback bits in the extent tree for this IO
2216 * end_page_writeback if the page has no more pending IO
2217 *
2218 * Scheduling is not allowed, so the extent state tree is expected
2219 * to have one and only one object corresponding to this IO.
2220 */
Chris Masond1310b22008-01-24 16:13:08 -05002221static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002222{
Chris Masond1310b22008-01-24 16:13:08 -05002223 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002224 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002225 u64 start;
2226 u64 end;
2227 int whole_page;
2228
Chris Masond1310b22008-01-24 16:13:08 -05002229 do {
2230 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002231 tree = &BTRFS_I(page->mapping->host)->io_tree;
2232
Chris Masond1310b22008-01-24 16:13:08 -05002233 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2234 bvec->bv_offset;
2235 end = start + bvec->bv_len - 1;
2236
2237 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2238 whole_page = 1;
2239 else
2240 whole_page = 0;
2241
2242 if (--bvec >= bio->bi_io_vec)
2243 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002244
Jeff Mahoney87826df2012-02-15 16:23:57 +01002245 if (end_extent_writepage(page, err, start, end))
2246 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002247
Chris Masond1310b22008-01-24 16:13:08 -05002248 if (whole_page)
2249 end_page_writeback(page);
2250 else
2251 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002252 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002253
Chris Masond1310b22008-01-24 16:13:08 -05002254 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002255}
2256
2257/*
2258 * after a readpage IO is done, we need to:
2259 * clear the uptodate bits on error
2260 * set the uptodate bits if things worked
2261 * set the page up to date if all extents in the tree are uptodate
2262 * clear the lock bit in the extent tree
2263 * unlock the page if there are no other extents locked for it
2264 *
2265 * Scheduling is not allowed, so the extent state tree is expected
2266 * to have one and only one object corresponding to this IO.
2267 */
Chris Masond1310b22008-01-24 16:13:08 -05002268static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002269{
2270 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002271 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2272 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002273 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002274 u64 start;
2275 u64 end;
2276 int whole_page;
2277 int ret;
2278
Chris Masond20f7042008-12-08 16:58:54 -05002279 if (err)
2280 uptodate = 0;
2281
Chris Masond1310b22008-01-24 16:13:08 -05002282 do {
2283 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002284 struct extent_state *cached = NULL;
2285 struct extent_state *state;
2286
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002287 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2288 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2289 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002290 tree = &BTRFS_I(page->mapping->host)->io_tree;
2291
Chris Masond1310b22008-01-24 16:13:08 -05002292 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2293 bvec->bv_offset;
2294 end = start + bvec->bv_len - 1;
2295
2296 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2297 whole_page = 1;
2298 else
2299 whole_page = 0;
2300
Chris Mason4125bf72010-02-03 18:18:45 +00002301 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002302 prefetchw(&bvec->bv_page->flags);
2303
Arne Jansen507903b2011-04-06 10:02:20 +00002304 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002305 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002306 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002307 /*
2308 * take a reference on the state, unlock will drop
2309 * the ref
2310 */
2311 cache_state(state, &cached);
2312 }
2313 spin_unlock(&tree->lock);
2314
Chris Masond1310b22008-01-24 16:13:08 -05002315 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002316 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002317 state);
Chris Masond1310b22008-01-24 16:13:08 -05002318 if (ret)
2319 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002320 else
2321 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002322 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002323 if (!uptodate) {
Jan Schmidt32240a92011-11-20 07:33:38 -05002324 int failed_mirror;
2325 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002326 /*
2327 * The generic bio_readpage_error handles errors the
2328 * following way: If possible, new read requests are
2329 * created and submitted and will end up in
2330 * end_bio_extent_readpage as well (if we're lucky, not
2331 * in the !uptodate case). In that case it returns 0 and
2332 * we just go on with the next page in our bio. If it
2333 * can't handle the error it will return -EIO and we
2334 * remain responsible for that page.
2335 */
2336 ret = bio_readpage_error(bio, page, start, end,
2337 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002338 if (ret == 0) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002339error_handled:
Chris Mason3b951512008-04-17 11:29:12 -04002340 uptodate =
2341 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002342 if (err)
2343 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002344 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002345 continue;
2346 }
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002347 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2348 ret = tree->ops->readpage_io_failed_hook(
2349 bio, page, start, end,
2350 failed_mirror, state);
2351 if (ret == 0)
2352 goto error_handled;
2353 }
Chris Mason7e383262008-04-09 16:28:12 -04002354 }
Chris Mason70dec802008-01-29 09:59:12 -05002355
Chris Mason771ed682008-11-06 22:02:51 -05002356 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002357 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002358 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002359 }
Arne Jansen507903b2011-04-06 10:02:20 +00002360 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002361
Chris Mason70dec802008-01-29 09:59:12 -05002362 if (whole_page) {
2363 if (uptodate) {
2364 SetPageUptodate(page);
2365 } else {
2366 ClearPageUptodate(page);
2367 SetPageError(page);
2368 }
Chris Masond1310b22008-01-24 16:13:08 -05002369 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002370 } else {
2371 if (uptodate) {
2372 check_page_uptodate(tree, page);
2373 } else {
2374 ClearPageUptodate(page);
2375 SetPageError(page);
2376 }
Chris Masond1310b22008-01-24 16:13:08 -05002377 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002378 }
Chris Mason4125bf72010-02-03 18:18:45 +00002379 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002380
2381 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002382}
2383
Miao Xie88f794e2010-11-22 03:02:55 +00002384struct bio *
2385btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2386 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002387{
2388 struct bio *bio;
2389
2390 bio = bio_alloc(gfp_flags, nr_vecs);
2391
2392 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2393 while (!bio && (nr_vecs /= 2))
2394 bio = bio_alloc(gfp_flags, nr_vecs);
2395 }
2396
2397 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002398 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002399 bio->bi_bdev = bdev;
2400 bio->bi_sector = first_sector;
2401 }
2402 return bio;
2403}
2404
Chris Masonc8b97812008-10-29 14:49:59 -04002405static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2406 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002407{
Chris Masond1310b22008-01-24 16:13:08 -05002408 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002409 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2410 struct page *page = bvec->bv_page;
2411 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002412 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002413
2414 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002415
David Woodhouse902b22f2008-08-20 08:51:49 -04002416 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002417
2418 bio_get(bio);
2419
Chris Mason065631f2008-02-20 12:07:25 -05002420 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002421 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002422 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002423 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002424 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002425
Chris Masond1310b22008-01-24 16:13:08 -05002426 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2427 ret = -EOPNOTSUPP;
2428 bio_put(bio);
2429 return ret;
2430}
2431
2432static int submit_extent_page(int rw, struct extent_io_tree *tree,
2433 struct page *page, sector_t sector,
2434 size_t size, unsigned long offset,
2435 struct block_device *bdev,
2436 struct bio **bio_ret,
2437 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002438 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002439 int mirror_num,
2440 unsigned long prev_bio_flags,
2441 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002442{
2443 int ret = 0;
2444 struct bio *bio;
2445 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002446 int contig = 0;
2447 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2448 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002449 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002450
2451 if (bio_ret && *bio_ret) {
2452 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002453 if (old_compressed)
2454 contig = bio->bi_sector == sector;
2455 else
2456 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2457 sector;
2458
2459 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002460 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002461 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2462 bio_flags)) ||
2463 bio_add_page(bio, page, page_size, offset) < page_size) {
2464 ret = submit_one_bio(rw, bio, mirror_num,
2465 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002466 bio = NULL;
2467 } else {
2468 return 0;
2469 }
2470 }
Chris Masonc8b97812008-10-29 14:49:59 -04002471 if (this_compressed)
2472 nr = BIO_MAX_PAGES;
2473 else
2474 nr = bio_get_nr_vecs(bdev);
2475
Miao Xie88f794e2010-11-22 03:02:55 +00002476 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002477 if (!bio)
2478 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002479
Chris Masonc8b97812008-10-29 14:49:59 -04002480 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002481 bio->bi_end_io = end_io_func;
2482 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002483
Chris Masond3977122009-01-05 21:25:51 -05002484 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002485 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002486 else
Chris Masonc8b97812008-10-29 14:49:59 -04002487 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002488
2489 return ret;
2490}
2491
2492void set_page_extent_mapped(struct page *page)
2493{
2494 if (!PagePrivate(page)) {
2495 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002496 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002497 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002498 }
2499}
2500
Christoph Hellwigb2950862008-12-02 09:54:17 -05002501static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002502{
Chris Masoneb14ab82011-02-10 12:35:00 -05002503 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002504 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2505}
2506
2507/*
2508 * basic readpage implementation. Locked extent state structs are inserted
2509 * into the tree that are removed when the IO is done (by the end_io
2510 * handlers)
2511 */
2512static int __extent_read_full_page(struct extent_io_tree *tree,
2513 struct page *page,
2514 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002515 struct bio **bio, int mirror_num,
2516 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002517{
2518 struct inode *inode = page->mapping->host;
2519 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2520 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2521 u64 end;
2522 u64 cur = start;
2523 u64 extent_offset;
2524 u64 last_byte = i_size_read(inode);
2525 u64 block_start;
2526 u64 cur_end;
2527 sector_t sector;
2528 struct extent_map *em;
2529 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002530 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002531 int ret;
2532 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002533 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002534 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002535 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002536 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002537 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002538
2539 set_page_extent_mapped(page);
2540
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002541 if (!PageUptodate(page)) {
2542 if (cleancache_get_page(page) == 0) {
2543 BUG_ON(blocksize != PAGE_SIZE);
2544 goto out;
2545 }
2546 }
2547
Chris Masond1310b22008-01-24 16:13:08 -05002548 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002549 while (1) {
2550 lock_extent(tree, start, end, GFP_NOFS);
2551 ordered = btrfs_lookup_ordered_extent(inode, start);
2552 if (!ordered)
2553 break;
2554 unlock_extent(tree, start, end, GFP_NOFS);
2555 btrfs_start_ordered_extent(inode, ordered, 1);
2556 btrfs_put_ordered_extent(ordered);
2557 }
Chris Masond1310b22008-01-24 16:13:08 -05002558
Chris Masonc8b97812008-10-29 14:49:59 -04002559 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2560 char *userpage;
2561 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2562
2563 if (zero_offset) {
2564 iosize = PAGE_CACHE_SIZE - zero_offset;
2565 userpage = kmap_atomic(page, KM_USER0);
2566 memset(userpage + zero_offset, 0, iosize);
2567 flush_dcache_page(page);
2568 kunmap_atomic(userpage, KM_USER0);
2569 }
2570 }
Chris Masond1310b22008-01-24 16:13:08 -05002571 while (cur <= end) {
2572 if (cur >= last_byte) {
2573 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002574 struct extent_state *cached = NULL;
2575
David Sterba306e16c2011-04-19 14:29:38 +02002576 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002577 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002578 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002579 flush_dcache_page(page);
2580 kunmap_atomic(userpage, KM_USER0);
2581 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002582 &cached, GFP_NOFS);
2583 unlock_extent_cached(tree, cur, cur + iosize - 1,
2584 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002585 break;
2586 }
David Sterba306e16c2011-04-19 14:29:38 +02002587 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002588 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002589 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002590 SetPageError(page);
2591 unlock_extent(tree, cur, end, GFP_NOFS);
2592 break;
2593 }
Chris Masond1310b22008-01-24 16:13:08 -05002594 extent_offset = cur - em->start;
2595 BUG_ON(extent_map_end(em) <= cur);
2596 BUG_ON(end < cur);
2597
Li Zefan261507a02010-12-17 14:21:50 +08002598 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002599 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002600 extent_set_compress_type(&this_bio_flag,
2601 em->compress_type);
2602 }
Chris Masonc8b97812008-10-29 14:49:59 -04002603
Chris Masond1310b22008-01-24 16:13:08 -05002604 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2605 cur_end = min(extent_map_end(em) - 1, end);
2606 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002607 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2608 disk_io_size = em->block_len;
2609 sector = em->block_start >> 9;
2610 } else {
2611 sector = (em->block_start + extent_offset) >> 9;
2612 disk_io_size = iosize;
2613 }
Chris Masond1310b22008-01-24 16:13:08 -05002614 bdev = em->bdev;
2615 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002616 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2617 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002618 free_extent_map(em);
2619 em = NULL;
2620
2621 /* we've found a hole, just zero and go on */
2622 if (block_start == EXTENT_MAP_HOLE) {
2623 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002624 struct extent_state *cached = NULL;
2625
Chris Masond1310b22008-01-24 16:13:08 -05002626 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002627 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002628 flush_dcache_page(page);
2629 kunmap_atomic(userpage, KM_USER0);
2630
2631 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002632 &cached, GFP_NOFS);
2633 unlock_extent_cached(tree, cur, cur + iosize - 1,
2634 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002635 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002636 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002637 continue;
2638 }
2639 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002640 if (test_range_bit(tree, cur, cur_end,
2641 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002642 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002643 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2644 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002645 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002646 continue;
2647 }
Chris Mason70dec802008-01-29 09:59:12 -05002648 /* we have an inline extent but it didn't get marked up
2649 * to date. Error out
2650 */
2651 if (block_start == EXTENT_MAP_INLINE) {
2652 SetPageError(page);
2653 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2654 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002655 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002656 continue;
2657 }
Chris Masond1310b22008-01-24 16:13:08 -05002658
2659 ret = 0;
2660 if (tree->ops && tree->ops->readpage_io_hook) {
2661 ret = tree->ops->readpage_io_hook(page, cur,
2662 cur + iosize - 1);
2663 }
2664 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002665 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2666 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002667 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002668 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002669 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002670 end_bio_extent_readpage, mirror_num,
2671 *bio_flags,
2672 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002673 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002674 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002675 }
2676 if (ret)
2677 SetPageError(page);
2678 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002679 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002680 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002681out:
Chris Masond1310b22008-01-24 16:13:08 -05002682 if (!nr) {
2683 if (!PageError(page))
2684 SetPageUptodate(page);
2685 unlock_page(page);
2686 }
2687 return 0;
2688}
2689
2690int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002691 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002692{
2693 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002694 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002695 int ret;
2696
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002697 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002698 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002699 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002700 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002701 return ret;
2702}
Chris Masond1310b22008-01-24 16:13:08 -05002703
Chris Mason11c83492009-04-20 15:50:09 -04002704static noinline void update_nr_written(struct page *page,
2705 struct writeback_control *wbc,
2706 unsigned long nr_written)
2707{
2708 wbc->nr_to_write -= nr_written;
2709 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2710 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2711 page->mapping->writeback_index = page->index + nr_written;
2712}
2713
Chris Masond1310b22008-01-24 16:13:08 -05002714/*
2715 * the writepage semantics are similar to regular writepage. extent
2716 * records are inserted to lock ranges in the tree, and as dirty areas
2717 * are found, they are marked writeback. Then the lock bits are removed
2718 * and the end_io handler clears the writeback ranges
2719 */
2720static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2721 void *data)
2722{
2723 struct inode *inode = page->mapping->host;
2724 struct extent_page_data *epd = data;
2725 struct extent_io_tree *tree = epd->tree;
2726 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2727 u64 delalloc_start;
2728 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2729 u64 end;
2730 u64 cur = start;
2731 u64 extent_offset;
2732 u64 last_byte = i_size_read(inode);
2733 u64 block_start;
2734 u64 iosize;
2735 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002736 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002737 struct extent_map *em;
2738 struct block_device *bdev;
2739 int ret;
2740 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002741 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002742 size_t blocksize;
2743 loff_t i_size = i_size_read(inode);
2744 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2745 u64 nr_delalloc;
2746 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002747 int page_started;
2748 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002749 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002750 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002751 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002752
Chris Masonffbd5172009-04-20 15:50:09 -04002753 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002754 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002755 else
2756 write_flags = WRITE;
2757
liubo1abe9b82011-03-24 11:18:59 +00002758 trace___extent_writepage(page, inode, wbc);
2759
Chris Masond1310b22008-01-24 16:13:08 -05002760 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002761
2762 ClearPageError(page);
2763
Chris Mason7f3c74f2008-07-18 12:01:11 -04002764 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002765 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002766 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002767 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002768 unlock_page(page);
2769 return 0;
2770 }
2771
2772 if (page->index == end_index) {
2773 char *userpage;
2774
Chris Masond1310b22008-01-24 16:13:08 -05002775 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002776 memset(userpage + pg_offset, 0,
2777 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002778 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002779 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002780 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002781 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002782
2783 set_page_extent_mapped(page);
2784
Josef Bacik9e487102011-08-01 12:08:18 -04002785 if (!tree->ops || !tree->ops->fill_delalloc)
2786 fill_delalloc = false;
2787
Chris Masond1310b22008-01-24 16:13:08 -05002788 delalloc_start = start;
2789 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002790 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002791 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002792 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002793 /*
2794 * make sure the wbc mapping index is at least updated
2795 * to this page.
2796 */
2797 update_nr_written(page, wbc, 0);
2798
Chris Masond3977122009-01-05 21:25:51 -05002799 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002800 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002801 page,
2802 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002803 &delalloc_end,
2804 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002805 if (nr_delalloc == 0) {
2806 delalloc_start = delalloc_end + 1;
2807 continue;
2808 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002809 ret = tree->ops->fill_delalloc(inode, page,
2810 delalloc_start,
2811 delalloc_end,
2812 &page_started,
2813 &nr_written);
2814 BUG_ON(ret);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002815 /*
2816 * delalloc_end is already one less than the total
2817 * length, so we don't subtract one from
2818 * PAGE_CACHE_SIZE
2819 */
2820 delalloc_to_write += (delalloc_end - delalloc_start +
2821 PAGE_CACHE_SIZE) >>
2822 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002823 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002824 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002825 if (wbc->nr_to_write < delalloc_to_write) {
2826 int thresh = 8192;
2827
2828 if (delalloc_to_write < thresh * 2)
2829 thresh = delalloc_to_write;
2830 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2831 thresh);
2832 }
Chris Masonc8b97812008-10-29 14:49:59 -04002833
Chris Mason771ed682008-11-06 22:02:51 -05002834 /* did the fill delalloc function already unlock and start
2835 * the IO?
2836 */
2837 if (page_started) {
2838 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002839 /*
2840 * we've unlocked the page, so we can't update
2841 * the mapping's writeback index, just update
2842 * nr_to_write.
2843 */
2844 wbc->nr_to_write -= nr_written;
2845 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002846 }
Chris Masonc8b97812008-10-29 14:49:59 -04002847 }
Chris Mason247e7432008-07-17 12:53:51 -04002848 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002849 ret = tree->ops->writepage_start_hook(page, start,
2850 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002851 if (ret) {
2852 /* Fixup worker will requeue */
2853 if (ret == -EBUSY)
2854 wbc->pages_skipped++;
2855 else
2856 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002857 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002858 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002859 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002860 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002861 }
2862 }
2863
Chris Mason11c83492009-04-20 15:50:09 -04002864 /*
2865 * we don't want to touch the inode after unlocking the page,
2866 * so we update the mapping writeback index now
2867 */
2868 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002869
Chris Masond1310b22008-01-24 16:13:08 -05002870 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002871 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002872 if (tree->ops && tree->ops->writepage_end_io_hook)
2873 tree->ops->writepage_end_io_hook(page, start,
2874 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002875 goto done;
2876 }
2877
Chris Masond1310b22008-01-24 16:13:08 -05002878 blocksize = inode->i_sb->s_blocksize;
2879
2880 while (cur <= end) {
2881 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002882 if (tree->ops && tree->ops->writepage_end_io_hook)
2883 tree->ops->writepage_end_io_hook(page, cur,
2884 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002885 break;
2886 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002887 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002888 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002889 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002890 SetPageError(page);
2891 break;
2892 }
2893
2894 extent_offset = cur - em->start;
2895 BUG_ON(extent_map_end(em) <= cur);
2896 BUG_ON(end < cur);
2897 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2898 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2899 sector = (em->block_start + extent_offset) >> 9;
2900 bdev = em->bdev;
2901 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002902 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002903 free_extent_map(em);
2904 em = NULL;
2905
Chris Masonc8b97812008-10-29 14:49:59 -04002906 /*
2907 * compressed and inline extents are written through other
2908 * paths in the FS
2909 */
2910 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002911 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002912 /*
2913 * end_io notification does not happen here for
2914 * compressed extents
2915 */
2916 if (!compressed && tree->ops &&
2917 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002918 tree->ops->writepage_end_io_hook(page, cur,
2919 cur + iosize - 1,
2920 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002921 else if (compressed) {
2922 /* we don't want to end_page_writeback on
2923 * a compressed extent. this happens
2924 * elsewhere
2925 */
2926 nr++;
2927 }
2928
2929 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002930 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002931 continue;
2932 }
Chris Masond1310b22008-01-24 16:13:08 -05002933 /* leave this out until we have a page_mkwrite call */
2934 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002935 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002936 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002937 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002938 continue;
2939 }
Chris Masonc8b97812008-10-29 14:49:59 -04002940
Chris Masond1310b22008-01-24 16:13:08 -05002941 if (tree->ops && tree->ops->writepage_io_hook) {
2942 ret = tree->ops->writepage_io_hook(page, cur,
2943 cur + iosize - 1);
2944 } else {
2945 ret = 0;
2946 }
Chris Mason1259ab72008-05-12 13:39:03 -04002947 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002948 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002949 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002950 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002951
Chris Masond1310b22008-01-24 16:13:08 -05002952 set_range_writeback(tree, cur, cur + iosize - 1);
2953 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002954 printk(KERN_ERR "btrfs warning page %lu not "
2955 "writeback, cur %llu end %llu\n",
2956 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002957 (unsigned long long)end);
2958 }
2959
Chris Masonffbd5172009-04-20 15:50:09 -04002960 ret = submit_extent_page(write_flags, tree, page,
2961 sector, iosize, pg_offset,
2962 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002963 end_bio_extent_writepage,
2964 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002965 if (ret)
2966 SetPageError(page);
2967 }
2968 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002969 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002970 nr++;
2971 }
2972done:
2973 if (nr == 0) {
2974 /* make sure the mapping tag for page dirty gets cleared */
2975 set_page_writeback(page);
2976 end_page_writeback(page);
2977 }
Chris Masond1310b22008-01-24 16:13:08 -05002978 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002979
Chris Mason11c83492009-04-20 15:50:09 -04002980done_unlocked:
2981
Chris Mason2c64c532009-09-02 15:04:12 -04002982 /* drop our reference on any cached states */
2983 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002984 return 0;
2985}
2986
Chris Masond1310b22008-01-24 16:13:08 -05002987/**
Chris Mason4bef0842008-09-08 11:18:08 -04002988 * 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 -05002989 * @mapping: address space structure to write
2990 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2991 * @writepage: function called for each page
2992 * @data: data passed to writepage function
2993 *
2994 * If a page is already under I/O, write_cache_pages() skips it, even
2995 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2996 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2997 * and msync() need to guarantee that all the data which was dirty at the time
2998 * the call was made get new I/O started against them. If wbc->sync_mode is
2999 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3000 * existing IO to complete.
3001 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003002static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003003 struct address_space *mapping,
3004 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003005 writepage_t writepage, void *data,
3006 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003007{
Chris Masond1310b22008-01-24 16:13:08 -05003008 int ret = 0;
3009 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003010 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003011 struct pagevec pvec;
3012 int nr_pages;
3013 pgoff_t index;
3014 pgoff_t end; /* Inclusive */
3015 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003016 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003017
Chris Masond1310b22008-01-24 16:13:08 -05003018 pagevec_init(&pvec, 0);
3019 if (wbc->range_cyclic) {
3020 index = mapping->writeback_index; /* Start from prev offset */
3021 end = -1;
3022 } else {
3023 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3024 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003025 scanned = 1;
3026 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003027 if (wbc->sync_mode == WB_SYNC_ALL)
3028 tag = PAGECACHE_TAG_TOWRITE;
3029 else
3030 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003031retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003032 if (wbc->sync_mode == WB_SYNC_ALL)
3033 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003034 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003035 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3036 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003037 unsigned i;
3038
3039 scanned = 1;
3040 for (i = 0; i < nr_pages; i++) {
3041 struct page *page = pvec.pages[i];
3042
3043 /*
3044 * At this point we hold neither mapping->tree_lock nor
3045 * lock on the page itself: the page may be truncated or
3046 * invalidated (changing page->mapping to NULL), or even
3047 * swizzled back from swapper_space to tmpfs file
3048 * mapping
3049 */
Chris Mason01d658f2011-11-01 10:08:06 -04003050 if (tree->ops &&
3051 tree->ops->write_cache_pages_lock_hook) {
3052 tree->ops->write_cache_pages_lock_hook(page,
3053 data, flush_fn);
3054 } else {
3055 if (!trylock_page(page)) {
3056 flush_fn(data);
3057 lock_page(page);
3058 }
3059 }
Chris Masond1310b22008-01-24 16:13:08 -05003060
3061 if (unlikely(page->mapping != mapping)) {
3062 unlock_page(page);
3063 continue;
3064 }
3065
3066 if (!wbc->range_cyclic && page->index > end) {
3067 done = 1;
3068 unlock_page(page);
3069 continue;
3070 }
3071
Chris Masond2c3f4f2008-11-19 12:44:22 -05003072 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003073 if (PageWriteback(page))
3074 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003075 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003076 }
Chris Masond1310b22008-01-24 16:13:08 -05003077
3078 if (PageWriteback(page) ||
3079 !clear_page_dirty_for_io(page)) {
3080 unlock_page(page);
3081 continue;
3082 }
3083
3084 ret = (*writepage)(page, wbc, data);
3085
3086 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3087 unlock_page(page);
3088 ret = 0;
3089 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003090 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003091 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003092
3093 /*
3094 * the filesystem may choose to bump up nr_to_write.
3095 * We have to make sure to honor the new nr_to_write
3096 * at any time
3097 */
3098 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003099 }
3100 pagevec_release(&pvec);
3101 cond_resched();
3102 }
3103 if (!scanned && !done) {
3104 /*
3105 * We hit the last page and there is more work to be done: wrap
3106 * back to the start of the file
3107 */
3108 scanned = 1;
3109 index = 0;
3110 goto retry;
3111 }
Chris Masond1310b22008-01-24 16:13:08 -05003112 return ret;
3113}
Chris Masond1310b22008-01-24 16:13:08 -05003114
Chris Masonffbd5172009-04-20 15:50:09 -04003115static void flush_epd_write_bio(struct extent_page_data *epd)
3116{
3117 if (epd->bio) {
3118 if (epd->sync_io)
3119 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3120 else
3121 submit_one_bio(WRITE, epd->bio, 0, 0);
3122 epd->bio = NULL;
3123 }
3124}
3125
Chris Masond2c3f4f2008-11-19 12:44:22 -05003126static noinline void flush_write_bio(void *data)
3127{
3128 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003129 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003130}
3131
Chris Masond1310b22008-01-24 16:13:08 -05003132int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3133 get_extent_t *get_extent,
3134 struct writeback_control *wbc)
3135{
3136 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003137 struct extent_page_data epd = {
3138 .bio = NULL,
3139 .tree = tree,
3140 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003141 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003142 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003143 };
Chris Masond1310b22008-01-24 16:13:08 -05003144
Chris Masond1310b22008-01-24 16:13:08 -05003145 ret = __extent_writepage(page, wbc, &epd);
3146
Chris Masonffbd5172009-04-20 15:50:09 -04003147 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003148 return ret;
3149}
Chris Masond1310b22008-01-24 16:13:08 -05003150
Chris Mason771ed682008-11-06 22:02:51 -05003151int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3152 u64 start, u64 end, get_extent_t *get_extent,
3153 int mode)
3154{
3155 int ret = 0;
3156 struct address_space *mapping = inode->i_mapping;
3157 struct page *page;
3158 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3159 PAGE_CACHE_SHIFT;
3160
3161 struct extent_page_data epd = {
3162 .bio = NULL,
3163 .tree = tree,
3164 .get_extent = get_extent,
3165 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003166 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003167 };
3168 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003169 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003170 .nr_to_write = nr_pages * 2,
3171 .range_start = start,
3172 .range_end = end + 1,
3173 };
3174
Chris Masond3977122009-01-05 21:25:51 -05003175 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003176 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3177 if (clear_page_dirty_for_io(page))
3178 ret = __extent_writepage(page, &wbc_writepages, &epd);
3179 else {
3180 if (tree->ops && tree->ops->writepage_end_io_hook)
3181 tree->ops->writepage_end_io_hook(page, start,
3182 start + PAGE_CACHE_SIZE - 1,
3183 NULL, 1);
3184 unlock_page(page);
3185 }
3186 page_cache_release(page);
3187 start += PAGE_CACHE_SIZE;
3188 }
3189
Chris Masonffbd5172009-04-20 15:50:09 -04003190 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003191 return ret;
3192}
Chris Masond1310b22008-01-24 16:13:08 -05003193
3194int extent_writepages(struct extent_io_tree *tree,
3195 struct address_space *mapping,
3196 get_extent_t *get_extent,
3197 struct writeback_control *wbc)
3198{
3199 int ret = 0;
3200 struct extent_page_data epd = {
3201 .bio = NULL,
3202 .tree = tree,
3203 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003204 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003205 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003206 };
3207
Chris Mason4bef0842008-09-08 11:18:08 -04003208 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003209 __extent_writepage, &epd,
3210 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003211 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003212 return ret;
3213}
Chris Masond1310b22008-01-24 16:13:08 -05003214
3215int extent_readpages(struct extent_io_tree *tree,
3216 struct address_space *mapping,
3217 struct list_head *pages, unsigned nr_pages,
3218 get_extent_t get_extent)
3219{
3220 struct bio *bio = NULL;
3221 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003222 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003223
Chris Masond1310b22008-01-24 16:13:08 -05003224 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3225 struct page *page = list_entry(pages->prev, struct page, lru);
3226
3227 prefetchw(&page->flags);
3228 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003229 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003230 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003231 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003232 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003233 }
3234 page_cache_release(page);
3235 }
Chris Masond1310b22008-01-24 16:13:08 -05003236 BUG_ON(!list_empty(pages));
3237 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003238 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003239 return 0;
3240}
Chris Masond1310b22008-01-24 16:13:08 -05003241
3242/*
3243 * basic invalidatepage code, this waits on any locked or writeback
3244 * ranges corresponding to the page, and then deletes any extent state
3245 * records from the tree
3246 */
3247int extent_invalidatepage(struct extent_io_tree *tree,
3248 struct page *page, unsigned long offset)
3249{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003250 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003251 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3252 u64 end = start + PAGE_CACHE_SIZE - 1;
3253 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3254
Chris Masond3977122009-01-05 21:25:51 -05003255 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003256 if (start > end)
3257 return 0;
3258
Josef Bacik2ac55d42010-02-03 19:33:23 +00003259 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003260 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003261 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003262 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3263 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003264 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003265 return 0;
3266}
Chris Masond1310b22008-01-24 16:13:08 -05003267
3268/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003269 * a helper for releasepage, this tests for areas of the page that
3270 * are locked or under IO and drops the related state bits if it is safe
3271 * to drop the page.
3272 */
3273int try_release_extent_state(struct extent_map_tree *map,
3274 struct extent_io_tree *tree, struct page *page,
3275 gfp_t mask)
3276{
3277 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3278 u64 end = start + PAGE_CACHE_SIZE - 1;
3279 int ret = 1;
3280
Chris Mason211f90e2008-07-18 11:56:15 -04003281 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003282 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003283 ret = 0;
3284 else {
3285 if ((mask & GFP_NOFS) == GFP_NOFS)
3286 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003287 /*
3288 * at this point we can safely clear everything except the
3289 * locked bit and the nodatasum bit
3290 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003291 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003292 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3293 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003294
3295 /* if clear_extent_bit failed for enomem reasons,
3296 * we can't allow the release to continue.
3297 */
3298 if (ret < 0)
3299 ret = 0;
3300 else
3301 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003302 }
3303 return ret;
3304}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003305
3306/*
Chris Masond1310b22008-01-24 16:13:08 -05003307 * a helper for releasepage. As long as there are no locked extents
3308 * in the range corresponding to the page, both state records and extent
3309 * map records are removed
3310 */
3311int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003312 struct extent_io_tree *tree, struct page *page,
3313 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003314{
3315 struct extent_map *em;
3316 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3317 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003318
Chris Mason70dec802008-01-29 09:59:12 -05003319 if ((mask & __GFP_WAIT) &&
3320 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003321 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003322 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003323 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003324 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003325 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003326 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003327 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003328 break;
3329 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003330 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3331 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003332 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003333 free_extent_map(em);
3334 break;
3335 }
3336 if (!test_range_bit(tree, em->start,
3337 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003338 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003339 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003340 remove_extent_mapping(map, em);
3341 /* once for the rb tree */
3342 free_extent_map(em);
3343 }
3344 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003345 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003346
3347 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003348 free_extent_map(em);
3349 }
Chris Masond1310b22008-01-24 16:13:08 -05003350 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003351 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003352}
Chris Masond1310b22008-01-24 16:13:08 -05003353
Chris Masonec29ed52011-02-23 16:23:20 -05003354/*
3355 * helper function for fiemap, which doesn't want to see any holes.
3356 * This maps until we find something past 'last'
3357 */
3358static struct extent_map *get_extent_skip_holes(struct inode *inode,
3359 u64 offset,
3360 u64 last,
3361 get_extent_t *get_extent)
3362{
3363 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3364 struct extent_map *em;
3365 u64 len;
3366
3367 if (offset >= last)
3368 return NULL;
3369
3370 while(1) {
3371 len = last - offset;
3372 if (len == 0)
3373 break;
3374 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3375 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003376 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003377 return em;
3378
3379 /* if this isn't a hole return it */
3380 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3381 em->block_start != EXTENT_MAP_HOLE) {
3382 return em;
3383 }
3384
3385 /* this is a hole, advance to the next extent */
3386 offset = extent_map_end(em);
3387 free_extent_map(em);
3388 if (offset >= last)
3389 break;
3390 }
3391 return NULL;
3392}
3393
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003394int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3395 __u64 start, __u64 len, get_extent_t *get_extent)
3396{
Josef Bacik975f84f2010-11-23 19:36:57 +00003397 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003398 u64 off = start;
3399 u64 max = start + len;
3400 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003401 u32 found_type;
3402 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003403 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003404 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003405 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003406 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003407 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003408 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003409 struct btrfs_path *path;
3410 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003411 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003412 u64 em_start = 0;
3413 u64 em_len = 0;
3414 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003415 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003416
3417 if (len == 0)
3418 return -EINVAL;
3419
Josef Bacik975f84f2010-11-23 19:36:57 +00003420 path = btrfs_alloc_path();
3421 if (!path)
3422 return -ENOMEM;
3423 path->leave_spinning = 1;
3424
Josef Bacik4d479cf2011-11-17 11:34:31 -05003425 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3426 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3427
Chris Masonec29ed52011-02-23 16:23:20 -05003428 /*
3429 * lookup the last file extent. We're not using i_size here
3430 * because there might be preallocation past i_size
3431 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003432 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003433 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003434 if (ret < 0) {
3435 btrfs_free_path(path);
3436 return ret;
3437 }
3438 WARN_ON(!ret);
3439 path->slots[0]--;
3440 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3441 struct btrfs_file_extent_item);
3442 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3443 found_type = btrfs_key_type(&found_key);
3444
Chris Masonec29ed52011-02-23 16:23:20 -05003445 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003446 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003447 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003448 /* have to trust i_size as the end */
3449 last = (u64)-1;
3450 last_for_get_extent = isize;
3451 } else {
3452 /*
3453 * remember the start of the last extent. There are a
3454 * bunch of different factors that go into the length of the
3455 * extent, so its much less complex to remember where it started
3456 */
3457 last = found_key.offset;
3458 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003459 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003460 btrfs_free_path(path);
3461
Chris Masonec29ed52011-02-23 16:23:20 -05003462 /*
3463 * we might have some extents allocated but more delalloc past those
3464 * extents. so, we trust isize unless the start of the last extent is
3465 * beyond isize
3466 */
3467 if (last < isize) {
3468 last = (u64)-1;
3469 last_for_get_extent = isize;
3470 }
3471
Josef Bacik2ac55d42010-02-03 19:33:23 +00003472 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3473 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003474
Josef Bacik4d479cf2011-11-17 11:34:31 -05003475 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003476 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003477 if (!em)
3478 goto out;
3479 if (IS_ERR(em)) {
3480 ret = PTR_ERR(em);
3481 goto out;
3482 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003483
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003484 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003485 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003486
Chris Masonea8efc72011-03-08 11:54:40 -05003487 /* break if the extent we found is outside the range */
3488 if (em->start >= max || extent_map_end(em) < off)
3489 break;
3490
3491 /*
3492 * get_extent may return an extent that starts before our
3493 * requested range. We have to make sure the ranges
3494 * we return to fiemap always move forward and don't
3495 * overlap, so adjust the offsets here
3496 */
3497 em_start = max(em->start, off);
3498
3499 /*
3500 * record the offset from the start of the extent
3501 * for adjusting the disk offset below
3502 */
3503 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003504 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003505 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003506 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003507 disko = 0;
3508 flags = 0;
3509
Chris Masonea8efc72011-03-08 11:54:40 -05003510 /*
3511 * bump off for our next call to get_extent
3512 */
3513 off = extent_map_end(em);
3514 if (off >= max)
3515 end = 1;
3516
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003517 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003518 end = 1;
3519 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003520 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003521 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3522 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003523 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003524 flags |= (FIEMAP_EXTENT_DELALLOC |
3525 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003526 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003527 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003528 }
3529 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3530 flags |= FIEMAP_EXTENT_ENCODED;
3531
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003532 free_extent_map(em);
3533 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003534 if ((em_start >= last) || em_len == (u64)-1 ||
3535 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003536 flags |= FIEMAP_EXTENT_LAST;
3537 end = 1;
3538 }
3539
Chris Masonec29ed52011-02-23 16:23:20 -05003540 /* now scan forward to see if this is really the last extent. */
3541 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3542 get_extent);
3543 if (IS_ERR(em)) {
3544 ret = PTR_ERR(em);
3545 goto out;
3546 }
3547 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003548 flags |= FIEMAP_EXTENT_LAST;
3549 end = 1;
3550 }
Chris Masonec29ed52011-02-23 16:23:20 -05003551 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3552 em_len, flags);
3553 if (ret)
3554 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003555 }
3556out_free:
3557 free_extent_map(em);
3558out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003559 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3560 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003561 return ret;
3562}
3563
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003564inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003565 unsigned long i)
3566{
3567 struct page *p;
3568 struct address_space *mapping;
3569
3570 if (i == 0)
3571 return eb->first_page;
3572 i += eb->start >> PAGE_CACHE_SHIFT;
3573 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003574 if (!mapping)
3575 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003576
3577 /*
3578 * extent_buffer_page is only called after pinning the page
3579 * by increasing the reference count. So we know the page must
3580 * be in the radix tree.
3581 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003582 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003583 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003584 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003585
Chris Masond1310b22008-01-24 16:13:08 -05003586 return p;
3587}
3588
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003589inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003590{
Chris Mason6af118c2008-07-22 11:18:07 -04003591 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3592 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003593}
3594
Chris Masond1310b22008-01-24 16:13:08 -05003595static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3596 u64 start,
3597 unsigned long len,
3598 gfp_t mask)
3599{
3600 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003601#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003602 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003603#endif
Chris Masond1310b22008-01-24 16:13:08 -05003604
Chris Masond1310b22008-01-24 16:13:08 -05003605 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003606 if (eb == NULL)
3607 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003608 eb->start = start;
3609 eb->len = len;
Chris Masonbd681512011-07-16 15:23:14 -04003610 rwlock_init(&eb->lock);
3611 atomic_set(&eb->write_locks, 0);
3612 atomic_set(&eb->read_locks, 0);
3613 atomic_set(&eb->blocking_readers, 0);
3614 atomic_set(&eb->blocking_writers, 0);
3615 atomic_set(&eb->spinning_readers, 0);
3616 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003617 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003618 init_waitqueue_head(&eb->write_lock_wq);
3619 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003620
Chris Mason39351272009-02-04 09:24:05 -05003621#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003622 spin_lock_irqsave(&leak_lock, flags);
3623 list_add(&eb->leak_list, &buffers);
3624 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003625#endif
Chris Masond1310b22008-01-24 16:13:08 -05003626 atomic_set(&eb->refs, 1);
3627
3628 return eb;
3629}
3630
3631static void __free_extent_buffer(struct extent_buffer *eb)
3632{
Chris Mason39351272009-02-04 09:24:05 -05003633#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003634 unsigned long flags;
3635 spin_lock_irqsave(&leak_lock, flags);
3636 list_del(&eb->leak_list);
3637 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003638#endif
Chris Masond1310b22008-01-24 16:13:08 -05003639 kmem_cache_free(extent_buffer_cache, eb);
3640}
3641
Miao Xie897ca6e2010-10-26 20:57:29 -04003642/*
3643 * Helper for releasing extent buffer page.
3644 */
3645static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3646 unsigned long start_idx)
3647{
3648 unsigned long index;
3649 struct page *page;
3650
3651 if (!eb->first_page)
3652 return;
3653
3654 index = num_extent_pages(eb->start, eb->len);
3655 if (start_idx >= index)
3656 return;
3657
3658 do {
3659 index--;
3660 page = extent_buffer_page(eb, index);
3661 if (page)
3662 page_cache_release(page);
3663 } while (index != start_idx);
3664}
3665
3666/*
3667 * Helper for releasing the extent buffer.
3668 */
3669static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3670{
3671 btrfs_release_extent_buffer_page(eb, 0);
3672 __free_extent_buffer(eb);
3673}
3674
Chris Masond1310b22008-01-24 16:13:08 -05003675struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3676 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003677 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003678{
3679 unsigned long num_pages = num_extent_pages(start, len);
3680 unsigned long i;
3681 unsigned long index = start >> PAGE_CACHE_SHIFT;
3682 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04003683 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003684 struct page *p;
3685 struct address_space *mapping = tree->mapping;
3686 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003687 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003688
Miao Xie19fe0a82010-10-26 20:57:29 -04003689 rcu_read_lock();
3690 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3691 if (eb && atomic_inc_not_zero(&eb->refs)) {
3692 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003693 mark_page_accessed(eb->first_page);
Chris Mason6af118c2008-07-22 11:18:07 -04003694 return eb;
3695 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003696 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04003697
David Sterbaba144192011-04-21 01:12:06 +02003698 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003699 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003700 return NULL;
3701
Chris Masond1310b22008-01-24 16:13:08 -05003702 if (page0) {
3703 eb->first_page = page0;
3704 i = 1;
3705 index++;
3706 page_cache_get(page0);
3707 mark_page_accessed(page0);
3708 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003709 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003710 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003711 } else {
3712 i = 0;
3713 }
3714 for (; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003715 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003716 if (!p) {
3717 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04003718 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003719 }
3720 set_page_extent_mapped(p);
3721 mark_page_accessed(p);
3722 if (i == 0) {
3723 eb->first_page = p;
3724 set_page_extent_head(p, len);
3725 } else {
3726 set_page_private(p, EXTENT_PAGE_PRIVATE);
3727 }
3728 if (!PageUptodate(p))
3729 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003730
3731 /*
3732 * see below about how we avoid a nasty race with release page
3733 * and why we unlock later
3734 */
3735 if (i != 0)
3736 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003737 }
3738 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003739 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003740
Miao Xie19fe0a82010-10-26 20:57:29 -04003741 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3742 if (ret)
3743 goto free_eb;
3744
Chris Mason6af118c2008-07-22 11:18:07 -04003745 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003746 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3747 if (ret == -EEXIST) {
3748 exists = radix_tree_lookup(&tree->buffer,
3749 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04003750 /* add one reference for the caller */
3751 atomic_inc(&exists->refs);
3752 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003753 radix_tree_preload_end();
Chris Mason6af118c2008-07-22 11:18:07 -04003754 goto free_eb;
3755 }
Chris Mason6af118c2008-07-22 11:18:07 -04003756 /* add one reference for the tree */
3757 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003758 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003759 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003760
3761 /*
3762 * there is a race where release page may have
3763 * tried to find this extent buffer in the radix
3764 * but failed. It will tell the VM it is safe to
3765 * reclaim the, and it will clear the page private bit.
3766 * We must make sure to set the page private bit properly
3767 * after the extent buffer is in the radix tree so
3768 * it doesn't get lost
3769 */
3770 set_page_extent_mapped(eb->first_page);
3771 set_page_extent_head(eb->first_page, eb->len);
3772 if (!page0)
3773 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003774 return eb;
3775
Chris Mason6af118c2008-07-22 11:18:07 -04003776free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003777 if (eb->first_page && !page0)
3778 unlock_page(eb->first_page);
3779
Chris Masond1310b22008-01-24 16:13:08 -05003780 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04003781 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003782 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04003783 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003784}
Chris Masond1310b22008-01-24 16:13:08 -05003785
3786struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003787 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003788{
Chris Masond1310b22008-01-24 16:13:08 -05003789 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003790
Miao Xie19fe0a82010-10-26 20:57:29 -04003791 rcu_read_lock();
3792 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3793 if (eb && atomic_inc_not_zero(&eb->refs)) {
3794 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003795 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003796 return eb;
3797 }
3798 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003799
Miao Xie19fe0a82010-10-26 20:57:29 -04003800 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003801}
Chris Masond1310b22008-01-24 16:13:08 -05003802
3803void free_extent_buffer(struct extent_buffer *eb)
3804{
Chris Masond1310b22008-01-24 16:13:08 -05003805 if (!eb)
3806 return;
3807
3808 if (!atomic_dec_and_test(&eb->refs))
3809 return;
3810
Chris Mason6af118c2008-07-22 11:18:07 -04003811 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003812}
Chris Masond1310b22008-01-24 16:13:08 -05003813
3814int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3815 struct extent_buffer *eb)
3816{
Chris Masond1310b22008-01-24 16:13:08 -05003817 unsigned long i;
3818 unsigned long num_pages;
3819 struct page *page;
3820
Chris Masond1310b22008-01-24 16:13:08 -05003821 num_pages = num_extent_pages(eb->start, eb->len);
3822
3823 for (i = 0; i < num_pages; i++) {
3824 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003825 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003826 continue;
3827
Chris Masona61e6f22008-07-22 11:18:08 -04003828 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003829 WARN_ON(!PagePrivate(page));
3830
3831 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003832 if (i == 0)
3833 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003834
Chris Masond1310b22008-01-24 16:13:08 -05003835 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003836 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003837 if (!PageDirty(page)) {
3838 radix_tree_tag_clear(&page->mapping->page_tree,
3839 page_index(page),
3840 PAGECACHE_TAG_DIRTY);
3841 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003842 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04003843 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04003844 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003845 }
3846 return 0;
3847}
Chris Masond1310b22008-01-24 16:13:08 -05003848
Chris Masond1310b22008-01-24 16:13:08 -05003849int set_extent_buffer_dirty(struct extent_io_tree *tree,
3850 struct extent_buffer *eb)
3851{
3852 unsigned long i;
3853 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003854 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003855
Chris Masonb9473432009-03-13 11:00:37 -04003856 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003857 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003858 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003859 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003860 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003861}
Chris Masond1310b22008-01-24 16:13:08 -05003862
Chris Mason19b6caf2011-07-25 06:50:50 -04003863static int __eb_straddles_pages(u64 start, u64 len)
3864{
3865 if (len < PAGE_CACHE_SIZE)
3866 return 1;
3867 if (start & (PAGE_CACHE_SIZE - 1))
3868 return 1;
3869 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3870 return 1;
3871 return 0;
3872}
3873
3874static int eb_straddles_pages(struct extent_buffer *eb)
3875{
3876 return __eb_straddles_pages(eb->start, eb->len);
3877}
3878
Chris Mason1259ab72008-05-12 13:39:03 -04003879int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003880 struct extent_buffer *eb,
3881 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003882{
3883 unsigned long i;
3884 struct page *page;
3885 unsigned long num_pages;
3886
3887 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003888 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003889
Chris Mason50653192012-02-22 12:36:24 -05003890 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3891 cached_state, GFP_NOFS);
3892
Chris Mason1259ab72008-05-12 13:39:03 -04003893 for (i = 0; i < num_pages; i++) {
3894 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003895 if (page)
3896 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003897 }
3898 return 0;
3899}
3900
Chris Masond1310b22008-01-24 16:13:08 -05003901int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3902 struct extent_buffer *eb)
3903{
3904 unsigned long i;
3905 struct page *page;
3906 unsigned long num_pages;
3907
3908 num_pages = num_extent_pages(eb->start, eb->len);
3909
Chris Mason19b6caf2011-07-25 06:50:50 -04003910 if (eb_straddles_pages(eb)) {
3911 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3912 NULL, GFP_NOFS);
3913 }
Chris Masond1310b22008-01-24 16:13:08 -05003914 for (i = 0; i < num_pages; i++) {
3915 page = extent_buffer_page(eb, i);
3916 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3917 ((i == num_pages - 1) &&
3918 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3919 check_page_uptodate(tree, page);
3920 continue;
3921 }
3922 SetPageUptodate(page);
3923 }
3924 return 0;
3925}
Chris Masond1310b22008-01-24 16:13:08 -05003926
Chris Masonce9adaa2008-04-09 16:28:12 -04003927int extent_range_uptodate(struct extent_io_tree *tree,
3928 u64 start, u64 end)
3929{
3930 struct page *page;
3931 int ret;
3932 int pg_uptodate = 1;
3933 int uptodate;
3934 unsigned long index;
3935
Chris Mason19b6caf2011-07-25 06:50:50 -04003936 if (__eb_straddles_pages(start, end - start + 1)) {
3937 ret = test_range_bit(tree, start, end,
3938 EXTENT_UPTODATE, 1, NULL);
3939 if (ret)
3940 return 1;
3941 }
Chris Masond3977122009-01-05 21:25:51 -05003942 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003943 index = start >> PAGE_CACHE_SHIFT;
3944 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05003945 if (!page)
3946 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04003947 uptodate = PageUptodate(page);
3948 page_cache_release(page);
3949 if (!uptodate) {
3950 pg_uptodate = 0;
3951 break;
3952 }
3953 start += PAGE_CACHE_SIZE;
3954 }
3955 return pg_uptodate;
3956}
3957
Chris Masond1310b22008-01-24 16:13:08 -05003958int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003959 struct extent_buffer *eb,
3960 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003961{
Chris Mason728131d2008-04-09 16:28:12 -04003962 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003963 unsigned long num_pages;
3964 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003965 struct page *page;
3966 int pg_uptodate = 1;
3967
Chris Masonb4ce94d2009-02-04 09:25:08 -05003968 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003969 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003970
Chris Mason19b6caf2011-07-25 06:50:50 -04003971 if (eb_straddles_pages(eb)) {
3972 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3973 EXTENT_UPTODATE, 1, cached_state);
3974 if (ret)
3975 return ret;
3976 }
Chris Mason728131d2008-04-09 16:28:12 -04003977
3978 num_pages = num_extent_pages(eb->start, eb->len);
3979 for (i = 0; i < num_pages; i++) {
3980 page = extent_buffer_page(eb, i);
3981 if (!PageUptodate(page)) {
3982 pg_uptodate = 0;
3983 break;
3984 }
3985 }
Chris Mason42352982008-04-28 16:40:52 -04003986 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003987}
Chris Masond1310b22008-01-24 16:13:08 -05003988
3989int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02003990 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003991 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003992{
3993 unsigned long i;
3994 unsigned long start_i;
3995 struct page *page;
3996 int err;
3997 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003998 int locked_pages = 0;
3999 int all_uptodate = 1;
4000 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004001 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05004002 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004003 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004004
Chris Masonb4ce94d2009-02-04 09:25:08 -05004005 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004006 return 0;
4007
Chris Mason19b6caf2011-07-25 06:50:50 -04004008 if (eb_straddles_pages(eb)) {
4009 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4010 EXTENT_UPTODATE, 1, NULL)) {
4011 return 0;
4012 }
Chris Masond1310b22008-01-24 16:13:08 -05004013 }
4014
4015 if (start) {
4016 WARN_ON(start < eb->start);
4017 start_i = (start >> PAGE_CACHE_SHIFT) -
4018 (eb->start >> PAGE_CACHE_SHIFT);
4019 } else {
4020 start_i = 0;
4021 }
4022
4023 num_pages = num_extent_pages(eb->start, eb->len);
4024 for (i = start_i; i < num_pages; i++) {
4025 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004026 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004027 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004028 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004029 } else {
4030 lock_page(page);
4031 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004032 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05004033 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004034 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004035 }
4036 if (all_uptodate) {
4037 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004038 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004039 goto unlock_exit;
4040 }
4041
4042 for (i = start_i; i < num_pages; i++) {
4043 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05004044
4045 WARN_ON(!PagePrivate(page));
4046
4047 set_page_extent_mapped(page);
4048 if (i == 0)
4049 set_page_extent_head(page, eb->len);
4050
Chris Masonce9adaa2008-04-09 16:28:12 -04004051 if (inc_all_pages)
4052 page_cache_get(page);
4053 if (!PageUptodate(page)) {
4054 if (start_i == 0)
4055 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04004056 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004057 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004058 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004059 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004060 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004061 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004062 } else {
4063 unlock_page(page);
4064 }
4065 }
4066
Chris Masona86c12c2008-02-07 10:50:54 -05004067 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04004068 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05004069
Arne Jansenbb82ab82011-06-10 14:06:53 +02004070 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004071 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004072
Chris Masond1310b22008-01-24 16:13:08 -05004073 for (i = start_i; i < num_pages; i++) {
4074 page = extent_buffer_page(eb, i);
4075 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004076 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004077 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004078 }
Chris Masond3977122009-01-05 21:25:51 -05004079
Chris Masond1310b22008-01-24 16:13:08 -05004080 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004081 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004082 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004083
4084unlock_exit:
4085 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004086 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004087 page = extent_buffer_page(eb, i);
4088 i++;
4089 unlock_page(page);
4090 locked_pages--;
4091 }
4092 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004093}
Chris Masond1310b22008-01-24 16:13:08 -05004094
4095void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4096 unsigned long start,
4097 unsigned long len)
4098{
4099 size_t cur;
4100 size_t offset;
4101 struct page *page;
4102 char *kaddr;
4103 char *dst = (char *)dstv;
4104 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4105 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004106
4107 WARN_ON(start > eb->len);
4108 WARN_ON(start + len > eb->start + eb->len);
4109
4110 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4111
Chris Masond3977122009-01-05 21:25:51 -05004112 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004113 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004114
4115 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004116 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004117 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004118
4119 dst += cur;
4120 len -= cur;
4121 offset = 0;
4122 i++;
4123 }
4124}
Chris Masond1310b22008-01-24 16:13:08 -05004125
4126int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004127 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004128 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004129 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004130{
4131 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4132 char *kaddr;
4133 struct page *p;
4134 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4135 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4136 unsigned long end_i = (start_offset + start + min_len - 1) >>
4137 PAGE_CACHE_SHIFT;
4138
4139 if (i != end_i)
4140 return -EINVAL;
4141
4142 if (i == 0) {
4143 offset = start_offset;
4144 *map_start = 0;
4145 } else {
4146 offset = 0;
4147 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4148 }
Chris Masond3977122009-01-05 21:25:51 -05004149
Chris Masond1310b22008-01-24 16:13:08 -05004150 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004151 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4152 "wanted %lu %lu\n", (unsigned long long)eb->start,
4153 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004154 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004155 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004156 }
4157
4158 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004159 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004160 *map = kaddr + offset;
4161 *map_len = PAGE_CACHE_SIZE - offset;
4162 return 0;
4163}
Chris Masond1310b22008-01-24 16:13:08 -05004164
Chris Masond1310b22008-01-24 16:13:08 -05004165int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4166 unsigned long start,
4167 unsigned long len)
4168{
4169 size_t cur;
4170 size_t offset;
4171 struct page *page;
4172 char *kaddr;
4173 char *ptr = (char *)ptrv;
4174 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4175 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4176 int ret = 0;
4177
4178 WARN_ON(start > eb->len);
4179 WARN_ON(start + len > eb->start + eb->len);
4180
4181 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4182
Chris Masond3977122009-01-05 21:25:51 -05004183 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004184 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004185
4186 cur = min(len, (PAGE_CACHE_SIZE - offset));
4187
Chris Masona6591712011-07-19 12:04:14 -04004188 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004189 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004190 if (ret)
4191 break;
4192
4193 ptr += cur;
4194 len -= cur;
4195 offset = 0;
4196 i++;
4197 }
4198 return ret;
4199}
Chris Masond1310b22008-01-24 16:13:08 -05004200
4201void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4202 unsigned long start, unsigned long len)
4203{
4204 size_t cur;
4205 size_t offset;
4206 struct page *page;
4207 char *kaddr;
4208 char *src = (char *)srcv;
4209 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4210 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4211
4212 WARN_ON(start > eb->len);
4213 WARN_ON(start + len > eb->start + eb->len);
4214
4215 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4216
Chris Masond3977122009-01-05 21:25:51 -05004217 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004218 page = extent_buffer_page(eb, i);
4219 WARN_ON(!PageUptodate(page));
4220
4221 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004222 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004223 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004224
4225 src += cur;
4226 len -= cur;
4227 offset = 0;
4228 i++;
4229 }
4230}
Chris Masond1310b22008-01-24 16:13:08 -05004231
4232void memset_extent_buffer(struct extent_buffer *eb, char c,
4233 unsigned long start, unsigned long len)
4234{
4235 size_t cur;
4236 size_t offset;
4237 struct page *page;
4238 char *kaddr;
4239 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4240 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4241
4242 WARN_ON(start > eb->len);
4243 WARN_ON(start + len > eb->start + eb->len);
4244
4245 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4246
Chris Masond3977122009-01-05 21:25:51 -05004247 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004248 page = extent_buffer_page(eb, i);
4249 WARN_ON(!PageUptodate(page));
4250
4251 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004252 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004253 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004254
4255 len -= cur;
4256 offset = 0;
4257 i++;
4258 }
4259}
Chris Masond1310b22008-01-24 16:13:08 -05004260
4261void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4262 unsigned long dst_offset, unsigned long src_offset,
4263 unsigned long len)
4264{
4265 u64 dst_len = dst->len;
4266 size_t cur;
4267 size_t offset;
4268 struct page *page;
4269 char *kaddr;
4270 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4271 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4272
4273 WARN_ON(src->len != dst_len);
4274
4275 offset = (start_offset + dst_offset) &
4276 ((unsigned long)PAGE_CACHE_SIZE - 1);
4277
Chris Masond3977122009-01-05 21:25:51 -05004278 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004279 page = extent_buffer_page(dst, i);
4280 WARN_ON(!PageUptodate(page));
4281
4282 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4283
Chris Masona6591712011-07-19 12:04:14 -04004284 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004285 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004286
4287 src_offset += cur;
4288 len -= cur;
4289 offset = 0;
4290 i++;
4291 }
4292}
Chris Masond1310b22008-01-24 16:13:08 -05004293
4294static void move_pages(struct page *dst_page, struct page *src_page,
4295 unsigned long dst_off, unsigned long src_off,
4296 unsigned long len)
4297{
Chris Masona6591712011-07-19 12:04:14 -04004298 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004299 if (dst_page == src_page) {
4300 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4301 } else {
Chris Masona6591712011-07-19 12:04:14 -04004302 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004303 char *p = dst_kaddr + dst_off + len;
4304 char *s = src_kaddr + src_off + len;
4305
4306 while (len--)
4307 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004308 }
Chris Masond1310b22008-01-24 16:13:08 -05004309}
4310
Sergei Trofimovich33872062011-04-11 21:52:52 +00004311static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4312{
4313 unsigned long distance = (src > dst) ? src - dst : dst - src;
4314 return distance < len;
4315}
4316
Chris Masond1310b22008-01-24 16:13:08 -05004317static void copy_pages(struct page *dst_page, struct page *src_page,
4318 unsigned long dst_off, unsigned long src_off,
4319 unsigned long len)
4320{
Chris Masona6591712011-07-19 12:04:14 -04004321 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004322 char *src_kaddr;
4323
Sergei Trofimovich33872062011-04-11 21:52:52 +00004324 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004325 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004326 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004327 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004328 BUG_ON(areas_overlap(src_off, dst_off, len));
4329 }
Chris Masond1310b22008-01-24 16:13:08 -05004330
4331 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004332}
4333
4334void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4335 unsigned long src_offset, unsigned long len)
4336{
4337 size_t cur;
4338 size_t dst_off_in_page;
4339 size_t src_off_in_page;
4340 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4341 unsigned long dst_i;
4342 unsigned long src_i;
4343
4344 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004345 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4346 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004347 BUG_ON(1);
4348 }
4349 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004350 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4351 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004352 BUG_ON(1);
4353 }
4354
Chris Masond3977122009-01-05 21:25:51 -05004355 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004356 dst_off_in_page = (start_offset + dst_offset) &
4357 ((unsigned long)PAGE_CACHE_SIZE - 1);
4358 src_off_in_page = (start_offset + src_offset) &
4359 ((unsigned long)PAGE_CACHE_SIZE - 1);
4360
4361 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4362 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4363
4364 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4365 src_off_in_page));
4366 cur = min_t(unsigned long, cur,
4367 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4368
4369 copy_pages(extent_buffer_page(dst, dst_i),
4370 extent_buffer_page(dst, src_i),
4371 dst_off_in_page, src_off_in_page, cur);
4372
4373 src_offset += cur;
4374 dst_offset += cur;
4375 len -= cur;
4376 }
4377}
Chris Masond1310b22008-01-24 16:13:08 -05004378
4379void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4380 unsigned long src_offset, unsigned long len)
4381{
4382 size_t cur;
4383 size_t dst_off_in_page;
4384 size_t src_off_in_page;
4385 unsigned long dst_end = dst_offset + len - 1;
4386 unsigned long src_end = src_offset + len - 1;
4387 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4388 unsigned long dst_i;
4389 unsigned long src_i;
4390
4391 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004392 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4393 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004394 BUG_ON(1);
4395 }
4396 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004397 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4398 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004399 BUG_ON(1);
4400 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004401 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004402 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4403 return;
4404 }
Chris Masond3977122009-01-05 21:25:51 -05004405 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004406 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4407 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4408
4409 dst_off_in_page = (start_offset + dst_end) &
4410 ((unsigned long)PAGE_CACHE_SIZE - 1);
4411 src_off_in_page = (start_offset + src_end) &
4412 ((unsigned long)PAGE_CACHE_SIZE - 1);
4413
4414 cur = min_t(unsigned long, len, src_off_in_page + 1);
4415 cur = min(cur, dst_off_in_page + 1);
4416 move_pages(extent_buffer_page(dst, dst_i),
4417 extent_buffer_page(dst, src_i),
4418 dst_off_in_page - cur + 1,
4419 src_off_in_page - cur + 1, cur);
4420
4421 dst_end -= cur;
4422 src_end -= cur;
4423 len -= cur;
4424 }
4425}
Chris Mason6af118c2008-07-22 11:18:07 -04004426
Miao Xie19fe0a82010-10-26 20:57:29 -04004427static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4428{
4429 struct extent_buffer *eb =
4430 container_of(head, struct extent_buffer, rcu_head);
4431
4432 btrfs_release_extent_buffer(eb);
4433}
4434
Chris Mason6af118c2008-07-22 11:18:07 -04004435int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4436{
4437 u64 start = page_offset(page);
4438 struct extent_buffer *eb;
4439 int ret = 1;
Chris Mason6af118c2008-07-22 11:18:07 -04004440
4441 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004442 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004443 if (!eb) {
4444 spin_unlock(&tree->buffer_lock);
4445 return ret;
4446 }
Chris Mason6af118c2008-07-22 11:18:07 -04004447
Chris Masonb9473432009-03-13 11:00:37 -04004448 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4449 ret = 0;
4450 goto out;
4451 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004452
Miao Xie19fe0a82010-10-26 20:57:29 -04004453 /*
4454 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4455 * Or go back.
4456 */
4457 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4458 ret = 0;
4459 goto out;
4460 }
4461
4462 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118c2008-07-22 11:18:07 -04004463out:
4464 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004465
4466 /* at this point we can safely release the extent buffer */
4467 if (atomic_read(&eb->refs) == 0)
4468 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118c2008-07-22 11:18:07 -04004469 return ret;
4470}