blob: 601f23bddea8dc7faa02e9653e7657f3b34c7520 [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 Mason6af118ce2008-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 *
465 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
466 * bits were already set, or zero if none of the bits were already set.
467 */
468int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400469 int bits, int wake, int delete,
470 struct extent_state **cached_state,
471 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500472{
473 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400474 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500475 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400476 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500477 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400478 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500479 int err;
480 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000481 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500482
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400483 if (delete)
484 bits |= ~EXTENT_CTLBITS;
485 bits |= EXTENT_FIRST_DELALLOC;
486
Josef Bacik2ac55d42010-02-03 19:33:23 +0000487 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
488 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500489again:
490 if (!prealloc && (mask & __GFP_WAIT)) {
491 prealloc = alloc_extent_state(mask);
492 if (!prealloc)
493 return -ENOMEM;
494 }
495
Chris Masoncad321a2008-12-17 14:51:42 -0500496 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400497 if (cached_state) {
498 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000499
500 if (clear) {
501 *cached_state = NULL;
502 cached_state = NULL;
503 }
504
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400505 if (cached && cached->tree && cached->start <= start &&
506 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000507 if (clear)
508 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400509 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400510 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400511 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000512 if (clear)
513 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400514 }
Chris Masond1310b22008-01-24 16:13:08 -0500515 /*
516 * this search will find the extents that end after
517 * our range starts
518 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500519 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500520 if (!node)
521 goto out;
522 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400523hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500524 if (state->start > end)
525 goto out;
526 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400527 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500528
Liu Bo04493142012-02-16 18:34:37 +0800529 if (state->end < end && !need_resched())
530 next_node = rb_next(&state->rb_node);
531 else
532 next_node = NULL;
533
534 /* the state doesn't have the wanted bits, go ahead */
535 if (!(state->state & bits))
536 goto next;
537
Chris Masond1310b22008-01-24 16:13:08 -0500538 /*
539 * | ---- desired range ---- |
540 * | state | or
541 * | ------------- state -------------- |
542 *
543 * We need to split the extent we found, and may flip
544 * bits on second half.
545 *
546 * If the extent we found extends past our range, we
547 * just split and search again. It'll get split again
548 * the next time though.
549 *
550 * If the extent we found is inside our range, we clear
551 * the desired bit on it.
552 */
553
554 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000555 prealloc = alloc_extent_state_atomic(prealloc);
556 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500557 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400558 if (err)
559 extent_io_tree_panic(tree, err);
560
Chris Masond1310b22008-01-24 16:13:08 -0500561 prealloc = NULL;
562 if (err)
563 goto out;
564 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400565 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400566 if (last_end == (u64)-1)
567 goto out;
568 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500569 }
570 goto search_again;
571 }
572 /*
573 * | ---- desired range ---- |
574 * | state |
575 * We need to split the extent, and clear the bit
576 * on the first half
577 */
578 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000579 prealloc = alloc_extent_state_atomic(prealloc);
580 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500581 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400582 if (err)
583 extent_io_tree_panic(tree, err);
584
Chris Masond1310b22008-01-24 16:13:08 -0500585 if (wake)
586 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400587
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400588 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400589
Chris Masond1310b22008-01-24 16:13:08 -0500590 prealloc = NULL;
591 goto out;
592 }
Chris Mason42daec22009-09-23 19:51:09 -0400593
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400594 set |= clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800595next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400596 if (last_end == (u64)-1)
597 goto out;
598 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400599 if (start <= end && next_node) {
600 state = rb_entry(next_node, struct extent_state,
601 rb_node);
Liu Bo692e5752012-02-16 18:34:36 +0800602 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400603 }
Chris Masond1310b22008-01-24 16:13:08 -0500604 goto search_again;
605
606out:
Chris Masoncad321a2008-12-17 14:51:42 -0500607 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500608 if (prealloc)
609 free_extent_state(prealloc);
610
611 return set;
612
613search_again:
614 if (start > end)
615 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500616 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500617 if (mask & __GFP_WAIT)
618 cond_resched();
619 goto again;
620}
Chris Masond1310b22008-01-24 16:13:08 -0500621
622static int wait_on_state(struct extent_io_tree *tree,
623 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500624 __releases(tree->lock)
625 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500626{
627 DEFINE_WAIT(wait);
628 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500629 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500631 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500632 finish_wait(&state->wq, &wait);
633 return 0;
634}
635
636/*
637 * waits for one or more bits to clear on a range in the state tree.
638 * The range [start, end] is inclusive.
639 * The tree lock is taken by this function
640 */
641int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
642{
643 struct extent_state *state;
644 struct rb_node *node;
645
Chris Masoncad321a2008-12-17 14:51:42 -0500646 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500647again:
648 while (1) {
649 /*
650 * this search will find all the extents that end after
651 * our range starts
652 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500653 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500654 if (!node)
655 break;
656
657 state = rb_entry(node, struct extent_state, rb_node);
658
659 if (state->start > end)
660 goto out;
661
662 if (state->state & bits) {
663 start = state->start;
664 atomic_inc(&state->refs);
665 wait_on_state(tree, state);
666 free_extent_state(state);
667 goto again;
668 }
669 start = state->end + 1;
670
671 if (start > end)
672 break;
673
Xiao Guangrongded91f02011-07-14 03:19:27 +0000674 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500675 }
676out:
Chris Masoncad321a2008-12-17 14:51:42 -0500677 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500678 return 0;
679}
Chris Masond1310b22008-01-24 16:13:08 -0500680
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000681static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500682 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400683 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500684{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400685 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400686
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000687 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400688 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500689 u64 range = state->end - state->start + 1;
690 tree->dirty_bytes += range;
691 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400692 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500693}
694
Chris Mason2c64c532009-09-02 15:04:12 -0400695static void cache_state(struct extent_state *state,
696 struct extent_state **cached_ptr)
697{
698 if (cached_ptr && !(*cached_ptr)) {
699 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
700 *cached_ptr = state;
701 atomic_inc(&state->refs);
702 }
703 }
704}
705
Arne Jansen507903b2011-04-06 10:02:20 +0000706static void uncache_state(struct extent_state **cached_ptr)
707{
708 if (cached_ptr && (*cached_ptr)) {
709 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400710 *cached_ptr = NULL;
711 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000712 }
713}
714
Chris Masond1310b22008-01-24 16:13:08 -0500715/*
Chris Mason1edbb732009-09-02 13:24:36 -0400716 * set some bits on a range in the tree. This may require allocations or
717 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500718 *
Chris Mason1edbb732009-09-02 13:24:36 -0400719 * If any of the exclusive bits are set, this will fail with -EEXIST if some
720 * part of the range already has the desired bits set. The start of the
721 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500722 *
Chris Mason1edbb732009-09-02 13:24:36 -0400723 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500724 */
Chris Mason1edbb732009-09-02 13:24:36 -0400725
Chris Mason4845e442010-05-25 20:56:50 -0400726int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
727 int bits, int exclusive_bits, u64 *failed_start,
728 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500729{
730 struct extent_state *state;
731 struct extent_state *prealloc = NULL;
732 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500733 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500734 u64 last_start;
735 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400736
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400737 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500738again:
739 if (!prealloc && (mask & __GFP_WAIT)) {
740 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000741 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500742 }
743
Chris Masoncad321a2008-12-17 14:51:42 -0500744 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400745 if (cached_state && *cached_state) {
746 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400747 if (state->start <= start && state->end > start &&
748 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400749 node = &state->rb_node;
750 goto hit_next;
751 }
752 }
Chris Masond1310b22008-01-24 16:13:08 -0500753 /*
754 * this search will find all the extents that end after
755 * our range starts.
756 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500757 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500758 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000759 prealloc = alloc_extent_state_atomic(prealloc);
760 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400761 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400762 if (err)
763 extent_io_tree_panic(tree, err);
764
Chris Masond1310b22008-01-24 16:13:08 -0500765 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500766 goto out;
767 }
Chris Masond1310b22008-01-24 16:13:08 -0500768 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400769hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500770 last_start = state->start;
771 last_end = state->end;
772
773 /*
774 * | ---- desired range ---- |
775 * | state |
776 *
777 * Just lock what we found and keep going
778 */
779 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400780 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400781 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500782 *failed_start = state->start;
783 err = -EEXIST;
784 goto out;
785 }
Chris Mason42daec22009-09-23 19:51:09 -0400786
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000787 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400788
Chris Mason2c64c532009-09-02 15:04:12 -0400789 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500790 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400791 if (last_end == (u64)-1)
792 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400793
Yan Zheng5c939df2009-05-27 09:16:03 -0400794 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400795 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000796 if (next_node && start < end && prealloc && !need_resched()) {
797 state = rb_entry(next_node, struct extent_state,
798 rb_node);
799 if (state->start == start)
800 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400801 }
Chris Masond1310b22008-01-24 16:13:08 -0500802 goto search_again;
803 }
804
805 /*
806 * | ---- desired range ---- |
807 * | state |
808 * or
809 * | ------------- state -------------- |
810 *
811 * We need to split the extent we found, and may flip bits on
812 * second half.
813 *
814 * If the extent we found extends past our
815 * range, we just split and search again. It'll get split
816 * again the next time though.
817 *
818 * If the extent we found is inside our range, we set the
819 * desired bit on it.
820 */
821 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400822 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500823 *failed_start = start;
824 err = -EEXIST;
825 goto out;
826 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000827
828 prealloc = alloc_extent_state_atomic(prealloc);
829 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500830 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400831 if (err)
832 extent_io_tree_panic(tree, err);
833
Chris Masond1310b22008-01-24 16:13:08 -0500834 prealloc = NULL;
835 if (err)
836 goto out;
837 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000838 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400839 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500840 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400841 if (last_end == (u64)-1)
842 goto out;
843 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500844 }
845 goto search_again;
846 }
847 /*
848 * | ---- desired range ---- |
849 * | state | or | state |
850 *
851 * There's a hole, we need to insert something in it and
852 * ignore the extent we found.
853 */
854 if (state->start > start) {
855 u64 this_end;
856 if (end < last_start)
857 this_end = end;
858 else
Chris Masond3977122009-01-05 21:25:51 -0500859 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000860
861 prealloc = alloc_extent_state_atomic(prealloc);
862 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000863
864 /*
865 * Avoid to free 'prealloc' if it can be merged with
866 * the later extent.
867 */
Chris Masond1310b22008-01-24 16:13:08 -0500868 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400869 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400870 if (err)
871 extent_io_tree_panic(tree, err);
872
Chris Mason2c64c532009-09-02 15:04:12 -0400873 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500874 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500875 start = this_end + 1;
876 goto search_again;
877 }
878 /*
879 * | ---- desired range ---- |
880 * | state |
881 * We need to split the extent, and set the bit
882 * on the first half
883 */
884 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400885 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500886 *failed_start = start;
887 err = -EEXIST;
888 goto out;
889 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000890
891 prealloc = alloc_extent_state_atomic(prealloc);
892 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500893 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400894 if (err)
895 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500896
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000897 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400898 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500899 merge_state(tree, prealloc);
900 prealloc = NULL;
901 goto out;
902 }
903
904 goto search_again;
905
906out:
Chris Masoncad321a2008-12-17 14:51:42 -0500907 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500908 if (prealloc)
909 free_extent_state(prealloc);
910
911 return err;
912
913search_again:
914 if (start > end)
915 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500916 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500917 if (mask & __GFP_WAIT)
918 cond_resched();
919 goto again;
920}
Chris Masond1310b22008-01-24 16:13:08 -0500921
Josef Bacik462d6fa2011-09-26 13:56:12 -0400922/**
923 * convert_extent - convert all bits in a given range from one bit to another
924 * @tree: the io tree to search
925 * @start: the start offset in bytes
926 * @end: the end offset in bytes (inclusive)
927 * @bits: the bits to set in this range
928 * @clear_bits: the bits to clear in this range
929 * @mask: the allocation mask
930 *
931 * This will go through and set bits for the given range. If any states exist
932 * already in this range they are set with the given bit and cleared of the
933 * clear_bits. This is only meant to be used by things that are mergeable, ie
934 * converting from say DELALLOC to DIRTY. This is not meant to be used with
935 * boundary bits like LOCK.
936 */
937int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
938 int bits, int clear_bits, gfp_t mask)
939{
940 struct extent_state *state;
941 struct extent_state *prealloc = NULL;
942 struct rb_node *node;
943 int err = 0;
944 u64 last_start;
945 u64 last_end;
946
947again:
948 if (!prealloc && (mask & __GFP_WAIT)) {
949 prealloc = alloc_extent_state(mask);
950 if (!prealloc)
951 return -ENOMEM;
952 }
953
954 spin_lock(&tree->lock);
955 /*
956 * this search will find all the extents that end after
957 * our range starts.
958 */
959 node = tree_search(tree, start);
960 if (!node) {
961 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500962 if (!prealloc) {
963 err = -ENOMEM;
964 goto out;
965 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400966 err = insert_state(tree, prealloc, start, end, &bits);
967 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400968 if (err)
969 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400970 goto out;
971 }
972 state = rb_entry(node, struct extent_state, rb_node);
973hit_next:
974 last_start = state->start;
975 last_end = state->end;
976
977 /*
978 * | ---- desired range ---- |
979 * | state |
980 *
981 * Just lock what we found and keep going
982 */
983 if (state->start == start && state->end <= end) {
984 struct rb_node *next_node;
985
986 set_state_bits(tree, state, &bits);
987 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400988 if (last_end == (u64)-1)
989 goto out;
990
991 start = last_end + 1;
992 next_node = rb_next(&state->rb_node);
993 if (next_node && start < end && prealloc && !need_resched()) {
994 state = rb_entry(next_node, struct extent_state,
995 rb_node);
996 if (state->start == start)
997 goto hit_next;
998 }
999 goto search_again;
1000 }
1001
1002 /*
1003 * | ---- desired range ---- |
1004 * | state |
1005 * or
1006 * | ------------- state -------------- |
1007 *
1008 * We need to split the extent we found, and may flip bits on
1009 * second half.
1010 *
1011 * If the extent we found extends past our
1012 * range, we just split and search again. It'll get split
1013 * again the next time though.
1014 *
1015 * If the extent we found is inside our range, we set the
1016 * desired bit on it.
1017 */
1018 if (state->start < start) {
1019 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001020 if (!prealloc) {
1021 err = -ENOMEM;
1022 goto out;
1023 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001024 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001025 if (err)
1026 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001027 prealloc = NULL;
1028 if (err)
1029 goto out;
1030 if (state->end <= end) {
1031 set_state_bits(tree, state, &bits);
1032 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001033 if (last_end == (u64)-1)
1034 goto out;
1035 start = last_end + 1;
1036 }
1037 goto search_again;
1038 }
1039 /*
1040 * | ---- desired range ---- |
1041 * | state | or | state |
1042 *
1043 * There's a hole, we need to insert something in it and
1044 * ignore the extent we found.
1045 */
1046 if (state->start > start) {
1047 u64 this_end;
1048 if (end < last_start)
1049 this_end = end;
1050 else
1051 this_end = last_start - 1;
1052
1053 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001054 if (!prealloc) {
1055 err = -ENOMEM;
1056 goto out;
1057 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001058
1059 /*
1060 * Avoid to free 'prealloc' if it can be merged with
1061 * the later extent.
1062 */
1063 err = insert_state(tree, prealloc, start, this_end,
1064 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001065 if (err)
1066 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001067 prealloc = NULL;
1068 start = this_end + 1;
1069 goto search_again;
1070 }
1071 /*
1072 * | ---- desired range ---- |
1073 * | state |
1074 * We need to split the extent, and set the bit
1075 * on the first half
1076 */
1077 if (state->start <= end && state->end > end) {
1078 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001079 if (!prealloc) {
1080 err = -ENOMEM;
1081 goto out;
1082 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001083
1084 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001085 if (err)
1086 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001087
1088 set_state_bits(tree, prealloc, &bits);
1089 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090 prealloc = NULL;
1091 goto out;
1092 }
1093
1094 goto search_again;
1095
1096out:
1097 spin_unlock(&tree->lock);
1098 if (prealloc)
1099 free_extent_state(prealloc);
1100
1101 return err;
1102
1103search_again:
1104 if (start > end)
1105 goto out;
1106 spin_unlock(&tree->lock);
1107 if (mask & __GFP_WAIT)
1108 cond_resched();
1109 goto again;
1110}
1111
Chris Masond1310b22008-01-24 16:13:08 -05001112/* wrappers around set/clear extent bit */
1113int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1114 gfp_t mask)
1115{
1116 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001117 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001118}
Chris Masond1310b22008-01-24 16:13:08 -05001119
1120int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1121 int bits, gfp_t mask)
1122{
1123 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001124 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001125}
Chris Masond1310b22008-01-24 16:13:08 -05001126
1127int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1128 int bits, gfp_t mask)
1129{
Chris Mason2c64c532009-09-02 15:04:12 -04001130 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001131}
Chris Masond1310b22008-01-24 16:13:08 -05001132
1133int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001134 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001135{
1136 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001137 EXTENT_DELALLOC | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001138 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001139}
Chris Masond1310b22008-01-24 16:13:08 -05001140
1141int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1142 gfp_t mask)
1143{
1144 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001145 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001146 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001147}
Chris Masond1310b22008-01-24 16:13:08 -05001148
1149int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1150 gfp_t mask)
1151{
1152 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001153 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001154}
Chris Masond1310b22008-01-24 16:13:08 -05001155
Chris Masond1310b22008-01-24 16:13:08 -05001156int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001157 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001158{
Arne Jansen507903b2011-04-06 10:02:20 +00001159 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1160 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001161}
Chris Masond1310b22008-01-24 16:13:08 -05001162
Chris Masond3977122009-01-05 21:25:51 -05001163static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001164 u64 end, struct extent_state **cached_state,
1165 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001166{
Chris Mason2c64c532009-09-02 15:04:12 -04001167 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001168 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001169}
Chris Masond1310b22008-01-24 16:13:08 -05001170
Chris Masond352ac62008-09-29 15:18:18 -04001171/*
1172 * either insert or lock state struct between start and end use mask to tell
1173 * us if waiting is desired.
1174 */
Chris Mason1edbb732009-09-02 13:24:36 -04001175int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001176 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001177{
1178 int err;
1179 u64 failed_start;
1180 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001181 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001182 EXTENT_LOCKED, &failed_start,
1183 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001184 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1185 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1186 start = failed_start;
1187 } else {
1188 break;
1189 }
1190 WARN_ON(start > end);
1191 }
1192 return err;
1193}
Chris Masond1310b22008-01-24 16:13:08 -05001194
Chris Mason1edbb732009-09-02 13:24:36 -04001195int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1196{
Chris Mason2c64c532009-09-02 15:04:12 -04001197 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001198}
1199
Josef Bacik25179202008-10-29 14:49:05 -04001200int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1201 gfp_t mask)
1202{
1203 int err;
1204 u64 failed_start;
1205
Chris Mason2c64c532009-09-02 15:04:12 -04001206 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1207 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001208 if (err == -EEXIST) {
1209 if (failed_start > start)
1210 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001211 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001212 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001213 }
Josef Bacik25179202008-10-29 14:49:05 -04001214 return 1;
1215}
Josef Bacik25179202008-10-29 14:49:05 -04001216
Chris Mason2c64c532009-09-02 15:04:12 -04001217int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1218 struct extent_state **cached, gfp_t mask)
1219{
1220 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1221 mask);
1222}
1223
Arne Jansen507903b2011-04-06 10:02:20 +00001224int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001225{
Chris Mason2c64c532009-09-02 15:04:12 -04001226 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1227 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001228}
Chris Masond1310b22008-01-24 16:13:08 -05001229
1230/*
Chris Masond1310b22008-01-24 16:13:08 -05001231 * helper function to set both pages and extents in the tree writeback
1232 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001233static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001234{
1235 unsigned long index = start >> PAGE_CACHE_SHIFT;
1236 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1237 struct page *page;
1238
1239 while (index <= end_index) {
1240 page = find_get_page(tree->mapping, index);
1241 BUG_ON(!page);
1242 set_page_writeback(page);
1243 page_cache_release(page);
1244 index++;
1245 }
Chris Masond1310b22008-01-24 16:13:08 -05001246 return 0;
1247}
Chris Masond1310b22008-01-24 16:13:08 -05001248
Chris Masond352ac62008-09-29 15:18:18 -04001249/* find the first state struct with 'bits' set after 'start', and
1250 * return it. tree->lock must be held. NULL will returned if
1251 * nothing was found after 'start'
1252 */
Chris Masond7fc6402008-02-18 12:12:38 -05001253struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1254 u64 start, int bits)
1255{
1256 struct rb_node *node;
1257 struct extent_state *state;
1258
1259 /*
1260 * this search will find all the extents that end after
1261 * our range starts.
1262 */
1263 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001264 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001265 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001266
Chris Masond3977122009-01-05 21:25:51 -05001267 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001268 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001269 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001270 return state;
Chris Masond3977122009-01-05 21:25:51 -05001271
Chris Masond7fc6402008-02-18 12:12:38 -05001272 node = rb_next(node);
1273 if (!node)
1274 break;
1275 }
1276out:
1277 return NULL;
1278}
Chris Masond7fc6402008-02-18 12:12:38 -05001279
Chris Masond352ac62008-09-29 15:18:18 -04001280/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001281 * find the first offset in the io tree with 'bits' set. zero is
1282 * returned if we find something, and *start_ret and *end_ret are
1283 * set to reflect the state struct that was found.
1284 *
1285 * If nothing was found, 1 is returned, < 0 on error
1286 */
1287int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1288 u64 *start_ret, u64 *end_ret, int bits)
1289{
1290 struct extent_state *state;
1291 int ret = 1;
1292
1293 spin_lock(&tree->lock);
1294 state = find_first_extent_bit_state(tree, start, bits);
1295 if (state) {
1296 *start_ret = state->start;
1297 *end_ret = state->end;
1298 ret = 0;
1299 }
1300 spin_unlock(&tree->lock);
1301 return ret;
1302}
1303
1304/*
Chris Masond352ac62008-09-29 15:18:18 -04001305 * find a contiguous range of bytes in the file marked as delalloc, not
1306 * more than 'max_bytes'. start and end are used to return the range,
1307 *
1308 * 1 is returned if we find something, 0 if nothing was in the tree
1309 */
Chris Masonc8b97812008-10-29 14:49:59 -04001310static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001311 u64 *start, u64 *end, u64 max_bytes,
1312 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001313{
1314 struct rb_node *node;
1315 struct extent_state *state;
1316 u64 cur_start = *start;
1317 u64 found = 0;
1318 u64 total_bytes = 0;
1319
Chris Masoncad321a2008-12-17 14:51:42 -05001320 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001321
Chris Masond1310b22008-01-24 16:13:08 -05001322 /*
1323 * this search will find all the extents that end after
1324 * our range starts.
1325 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001326 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001327 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001328 if (!found)
1329 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001330 goto out;
1331 }
1332
Chris Masond3977122009-01-05 21:25:51 -05001333 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001334 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001335 if (found && (state->start != cur_start ||
1336 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001337 goto out;
1338 }
1339 if (!(state->state & EXTENT_DELALLOC)) {
1340 if (!found)
1341 *end = state->end;
1342 goto out;
1343 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001344 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001345 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001346 *cached_state = state;
1347 atomic_inc(&state->refs);
1348 }
Chris Masond1310b22008-01-24 16:13:08 -05001349 found++;
1350 *end = state->end;
1351 cur_start = state->end + 1;
1352 node = rb_next(node);
1353 if (!node)
1354 break;
1355 total_bytes += state->end - state->start + 1;
1356 if (total_bytes >= max_bytes)
1357 break;
1358 }
1359out:
Chris Masoncad321a2008-12-17 14:51:42 -05001360 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001361 return found;
1362}
1363
Chris Masonc8b97812008-10-29 14:49:59 -04001364static noinline int __unlock_for_delalloc(struct inode *inode,
1365 struct page *locked_page,
1366 u64 start, u64 end)
1367{
1368 int ret;
1369 struct page *pages[16];
1370 unsigned long index = start >> PAGE_CACHE_SHIFT;
1371 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1372 unsigned long nr_pages = end_index - index + 1;
1373 int i;
1374
1375 if (index == locked_page->index && end_index == index)
1376 return 0;
1377
Chris Masond3977122009-01-05 21:25:51 -05001378 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001379 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001380 min_t(unsigned long, nr_pages,
1381 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001382 for (i = 0; i < ret; i++) {
1383 if (pages[i] != locked_page)
1384 unlock_page(pages[i]);
1385 page_cache_release(pages[i]);
1386 }
1387 nr_pages -= ret;
1388 index += ret;
1389 cond_resched();
1390 }
1391 return 0;
1392}
1393
1394static noinline int lock_delalloc_pages(struct inode *inode,
1395 struct page *locked_page,
1396 u64 delalloc_start,
1397 u64 delalloc_end)
1398{
1399 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1400 unsigned long start_index = index;
1401 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1402 unsigned long pages_locked = 0;
1403 struct page *pages[16];
1404 unsigned long nrpages;
1405 int ret;
1406 int i;
1407
1408 /* the caller is responsible for locking the start index */
1409 if (index == locked_page->index && index == end_index)
1410 return 0;
1411
1412 /* skip the page at the start index */
1413 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001414 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001415 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001416 min_t(unsigned long,
1417 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001418 if (ret == 0) {
1419 ret = -EAGAIN;
1420 goto done;
1421 }
1422 /* now we have an array of pages, lock them all */
1423 for (i = 0; i < ret; i++) {
1424 /*
1425 * the caller is taking responsibility for
1426 * locked_page
1427 */
Chris Mason771ed682008-11-06 22:02:51 -05001428 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001429 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001430 if (!PageDirty(pages[i]) ||
1431 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001432 ret = -EAGAIN;
1433 unlock_page(pages[i]);
1434 page_cache_release(pages[i]);
1435 goto done;
1436 }
1437 }
Chris Masonc8b97812008-10-29 14:49:59 -04001438 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001439 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001440 }
Chris Masonc8b97812008-10-29 14:49:59 -04001441 nrpages -= ret;
1442 index += ret;
1443 cond_resched();
1444 }
1445 ret = 0;
1446done:
1447 if (ret && pages_locked) {
1448 __unlock_for_delalloc(inode, locked_page,
1449 delalloc_start,
1450 ((u64)(start_index + pages_locked - 1)) <<
1451 PAGE_CACHE_SHIFT);
1452 }
1453 return ret;
1454}
1455
1456/*
1457 * find a contiguous range of bytes in the file marked as delalloc, not
1458 * more than 'max_bytes'. start and end are used to return the range,
1459 *
1460 * 1 is returned if we find something, 0 if nothing was in the tree
1461 */
1462static noinline u64 find_lock_delalloc_range(struct inode *inode,
1463 struct extent_io_tree *tree,
1464 struct page *locked_page,
1465 u64 *start, u64 *end,
1466 u64 max_bytes)
1467{
1468 u64 delalloc_start;
1469 u64 delalloc_end;
1470 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001471 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001472 int ret;
1473 int loops = 0;
1474
1475again:
1476 /* step one, find a bunch of delalloc bytes starting at start */
1477 delalloc_start = *start;
1478 delalloc_end = 0;
1479 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001480 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001481 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001482 *start = delalloc_start;
1483 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001484 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001485 return found;
1486 }
1487
1488 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001489 * start comes from the offset of locked_page. We have to lock
1490 * pages in order, so we can't process delalloc bytes before
1491 * locked_page
1492 */
Chris Masond3977122009-01-05 21:25:51 -05001493 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001494 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001495
1496 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001497 * make sure to limit the number of pages we try to lock down
1498 * if we're looping.
1499 */
Chris Masond3977122009-01-05 21:25:51 -05001500 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001501 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001502
Chris Masonc8b97812008-10-29 14:49:59 -04001503 /* step two, lock all the pages after the page that has start */
1504 ret = lock_delalloc_pages(inode, locked_page,
1505 delalloc_start, delalloc_end);
1506 if (ret == -EAGAIN) {
1507 /* some of the pages are gone, lets avoid looping by
1508 * shortening the size of the delalloc range we're searching
1509 */
Chris Mason9655d292009-09-02 15:22:30 -04001510 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001511 if (!loops) {
1512 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1513 max_bytes = PAGE_CACHE_SIZE - offset;
1514 loops = 1;
1515 goto again;
1516 } else {
1517 found = 0;
1518 goto out_failed;
1519 }
1520 }
1521 BUG_ON(ret);
1522
1523 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001524 lock_extent_bits(tree, delalloc_start, delalloc_end,
1525 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001526
1527 /* then test to make sure it is all still delalloc */
1528 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001529 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001530 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001531 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1532 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001533 __unlock_for_delalloc(inode, locked_page,
1534 delalloc_start, delalloc_end);
1535 cond_resched();
1536 goto again;
1537 }
Chris Mason9655d292009-09-02 15:22:30 -04001538 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001539 *start = delalloc_start;
1540 *end = delalloc_end;
1541out_failed:
1542 return found;
1543}
1544
1545int extent_clear_unlock_delalloc(struct inode *inode,
1546 struct extent_io_tree *tree,
1547 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001548 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001549{
1550 int ret;
1551 struct page *pages[16];
1552 unsigned long index = start >> PAGE_CACHE_SHIFT;
1553 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1554 unsigned long nr_pages = end_index - index + 1;
1555 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001556 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001557
Chris Masona791e352009-10-08 11:27:10 -04001558 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001559 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001560 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001561 clear_bits |= EXTENT_DIRTY;
1562
Chris Masona791e352009-10-08 11:27:10 -04001563 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001564 clear_bits |= EXTENT_DELALLOC;
1565
Chris Mason2c64c532009-09-02 15:04:12 -04001566 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001567 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1568 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1569 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001570 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001571
Chris Masond3977122009-01-05 21:25:51 -05001572 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001573 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001574 min_t(unsigned long,
1575 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001576 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001577
Chris Masona791e352009-10-08 11:27:10 -04001578 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001579 SetPagePrivate2(pages[i]);
1580
Chris Masonc8b97812008-10-29 14:49:59 -04001581 if (pages[i] == locked_page) {
1582 page_cache_release(pages[i]);
1583 continue;
1584 }
Chris Masona791e352009-10-08 11:27:10 -04001585 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001586 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001587 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001588 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001589 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001590 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001591 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001592 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001593 page_cache_release(pages[i]);
1594 }
1595 nr_pages -= ret;
1596 index += ret;
1597 cond_resched();
1598 }
1599 return 0;
1600}
Chris Masonc8b97812008-10-29 14:49:59 -04001601
Chris Masond352ac62008-09-29 15:18:18 -04001602/*
1603 * count the number of bytes in the tree that have a given bit(s)
1604 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1605 * cached. The total number found is returned.
1606 */
Chris Masond1310b22008-01-24 16:13:08 -05001607u64 count_range_bits(struct extent_io_tree *tree,
1608 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001609 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001610{
1611 struct rb_node *node;
1612 struct extent_state *state;
1613 u64 cur_start = *start;
1614 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001615 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001616 int found = 0;
1617
1618 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001619 WARN_ON(1);
1620 return 0;
1621 }
1622
Chris Masoncad321a2008-12-17 14:51:42 -05001623 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001624 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1625 total_bytes = tree->dirty_bytes;
1626 goto out;
1627 }
1628 /*
1629 * this search will find all the extents that end after
1630 * our range starts.
1631 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001632 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001633 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001634 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001635
Chris Masond3977122009-01-05 21:25:51 -05001636 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001637 state = rb_entry(node, struct extent_state, rb_node);
1638 if (state->start > search_end)
1639 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001640 if (contig && found && state->start > last + 1)
1641 break;
1642 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001643 total_bytes += min(search_end, state->end) + 1 -
1644 max(cur_start, state->start);
1645 if (total_bytes >= max_bytes)
1646 break;
1647 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001648 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001649 found = 1;
1650 }
Chris Masonec29ed52011-02-23 16:23:20 -05001651 last = state->end;
1652 } else if (contig && found) {
1653 break;
Chris Masond1310b22008-01-24 16:13:08 -05001654 }
1655 node = rb_next(node);
1656 if (!node)
1657 break;
1658 }
1659out:
Chris Masoncad321a2008-12-17 14:51:42 -05001660 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001661 return total_bytes;
1662}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001663
Chris Masond352ac62008-09-29 15:18:18 -04001664/*
1665 * set the private field for a given byte offset in the tree. If there isn't
1666 * an extent_state there already, this does nothing.
1667 */
Chris Masond1310b22008-01-24 16:13:08 -05001668int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1669{
1670 struct rb_node *node;
1671 struct extent_state *state;
1672 int ret = 0;
1673
Chris Masoncad321a2008-12-17 14:51:42 -05001674 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001675 /*
1676 * this search will find all the extents that end after
1677 * our range starts.
1678 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001679 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001680 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001681 ret = -ENOENT;
1682 goto out;
1683 }
1684 state = rb_entry(node, struct extent_state, rb_node);
1685 if (state->start != start) {
1686 ret = -ENOENT;
1687 goto out;
1688 }
1689 state->private = private;
1690out:
Chris Masoncad321a2008-12-17 14:51:42 -05001691 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001692 return ret;
1693}
1694
1695int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1696{
1697 struct rb_node *node;
1698 struct extent_state *state;
1699 int ret = 0;
1700
Chris Masoncad321a2008-12-17 14:51:42 -05001701 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001702 /*
1703 * this search will find all the extents that end after
1704 * our range starts.
1705 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001706 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001707 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001708 ret = -ENOENT;
1709 goto out;
1710 }
1711 state = rb_entry(node, struct extent_state, rb_node);
1712 if (state->start != start) {
1713 ret = -ENOENT;
1714 goto out;
1715 }
1716 *private = state->private;
1717out:
Chris Masoncad321a2008-12-17 14:51:42 -05001718 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001719 return ret;
1720}
1721
1722/*
1723 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001724 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001725 * has the bits set. Otherwise, 1 is returned if any bit in the
1726 * range is found set.
1727 */
1728int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001729 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001730{
1731 struct extent_state *state = NULL;
1732 struct rb_node *node;
1733 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001734
Chris Masoncad321a2008-12-17 14:51:42 -05001735 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001736 if (cached && cached->tree && cached->start <= start &&
1737 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001738 node = &cached->rb_node;
1739 else
1740 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001741 while (node && start <= end) {
1742 state = rb_entry(node, struct extent_state, rb_node);
1743
1744 if (filled && state->start > start) {
1745 bitset = 0;
1746 break;
1747 }
1748
1749 if (state->start > end)
1750 break;
1751
1752 if (state->state & bits) {
1753 bitset = 1;
1754 if (!filled)
1755 break;
1756 } else if (filled) {
1757 bitset = 0;
1758 break;
1759 }
Chris Mason46562ce2009-09-23 20:23:16 -04001760
1761 if (state->end == (u64)-1)
1762 break;
1763
Chris Masond1310b22008-01-24 16:13:08 -05001764 start = state->end + 1;
1765 if (start > end)
1766 break;
1767 node = rb_next(node);
1768 if (!node) {
1769 if (filled)
1770 bitset = 0;
1771 break;
1772 }
1773 }
Chris Masoncad321a2008-12-17 14:51:42 -05001774 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001775 return bitset;
1776}
Chris Masond1310b22008-01-24 16:13:08 -05001777
1778/*
1779 * helper function to set a given page up to date if all the
1780 * extents in the tree for that page are up to date
1781 */
1782static int check_page_uptodate(struct extent_io_tree *tree,
1783 struct page *page)
1784{
1785 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1786 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001787 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001788 SetPageUptodate(page);
1789 return 0;
1790}
1791
1792/*
1793 * helper function to unlock a page if all the extents in the tree
1794 * for that page are unlocked
1795 */
1796static int check_page_locked(struct extent_io_tree *tree,
1797 struct page *page)
1798{
1799 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1800 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001801 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001802 unlock_page(page);
1803 return 0;
1804}
1805
1806/*
1807 * helper function to end page writeback if all the extents
1808 * in the tree for that page are done with writeback
1809 */
1810static int check_page_writeback(struct extent_io_tree *tree,
1811 struct page *page)
1812{
Chris Mason1edbb732009-09-02 13:24:36 -04001813 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001814 return 0;
1815}
1816
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001817/*
1818 * When IO fails, either with EIO or csum verification fails, we
1819 * try other mirrors that might have a good copy of the data. This
1820 * io_failure_record is used to record state as we go through all the
1821 * mirrors. If another mirror has good data, the page is set up to date
1822 * and things continue. If a good mirror can't be found, the original
1823 * bio end_io callback is called to indicate things have failed.
1824 */
1825struct io_failure_record {
1826 struct page *page;
1827 u64 start;
1828 u64 len;
1829 u64 logical;
1830 unsigned long bio_flags;
1831 int this_mirror;
1832 int failed_mirror;
1833 int in_validation;
1834};
1835
1836static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1837 int did_repair)
1838{
1839 int ret;
1840 int err = 0;
1841 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1842
1843 set_state_private(failure_tree, rec->start, 0);
1844 ret = clear_extent_bits(failure_tree, rec->start,
1845 rec->start + rec->len - 1,
1846 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1847 if (ret)
1848 err = ret;
1849
1850 if (did_repair) {
1851 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1852 rec->start + rec->len - 1,
1853 EXTENT_DAMAGED, GFP_NOFS);
1854 if (ret && !err)
1855 err = ret;
1856 }
1857
1858 kfree(rec);
1859 return err;
1860}
1861
1862static void repair_io_failure_callback(struct bio *bio, int err)
1863{
1864 complete(bio->bi_private);
1865}
1866
1867/*
1868 * this bypasses the standard btrfs submit functions deliberately, as
1869 * the standard behavior is to write all copies in a raid setup. here we only
1870 * want to write the one bad copy. so we do the mapping for ourselves and issue
1871 * submit_bio directly.
1872 * to avoid any synchonization issues, wait for the data after writing, which
1873 * actually prevents the read that triggered the error from finishing.
1874 * currently, there can be no more than two copies of every data bit. thus,
1875 * exactly one rewrite is required.
1876 */
1877int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1878 u64 length, u64 logical, struct page *page,
1879 int mirror_num)
1880{
1881 struct bio *bio;
1882 struct btrfs_device *dev;
1883 DECLARE_COMPLETION_ONSTACK(compl);
1884 u64 map_length = 0;
1885 u64 sector;
1886 struct btrfs_bio *bbio = NULL;
1887 int ret;
1888
1889 BUG_ON(!mirror_num);
1890
1891 bio = bio_alloc(GFP_NOFS, 1);
1892 if (!bio)
1893 return -EIO;
1894 bio->bi_private = &compl;
1895 bio->bi_end_io = repair_io_failure_callback;
1896 bio->bi_size = 0;
1897 map_length = length;
1898
1899 ret = btrfs_map_block(map_tree, WRITE, logical,
1900 &map_length, &bbio, mirror_num);
1901 if (ret) {
1902 bio_put(bio);
1903 return -EIO;
1904 }
1905 BUG_ON(mirror_num != bbio->mirror_num);
1906 sector = bbio->stripes[mirror_num-1].physical >> 9;
1907 bio->bi_sector = sector;
1908 dev = bbio->stripes[mirror_num-1].dev;
1909 kfree(bbio);
1910 if (!dev || !dev->bdev || !dev->writeable) {
1911 bio_put(bio);
1912 return -EIO;
1913 }
1914 bio->bi_bdev = dev->bdev;
1915 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001916 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001917 wait_for_completion(&compl);
1918
1919 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1920 /* try to remap that extent elsewhere? */
1921 bio_put(bio);
1922 return -EIO;
1923 }
1924
1925 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1926 "sector %llu)\n", page->mapping->host->i_ino, start,
1927 dev->name, sector);
1928
1929 bio_put(bio);
1930 return 0;
1931}
1932
1933/*
1934 * each time an IO finishes, we do a fast check in the IO failure tree
1935 * to see if we need to process or clean up an io_failure_record
1936 */
1937static int clean_io_failure(u64 start, struct page *page)
1938{
1939 u64 private;
1940 u64 private_failure;
1941 struct io_failure_record *failrec;
1942 struct btrfs_mapping_tree *map_tree;
1943 struct extent_state *state;
1944 int num_copies;
1945 int did_repair = 0;
1946 int ret;
1947 struct inode *inode = page->mapping->host;
1948
1949 private = 0;
1950 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1951 (u64)-1, 1, EXTENT_DIRTY, 0);
1952 if (!ret)
1953 return 0;
1954
1955 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1956 &private_failure);
1957 if (ret)
1958 return 0;
1959
1960 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1961 BUG_ON(!failrec->this_mirror);
1962
1963 if (failrec->in_validation) {
1964 /* there was no real error, just free the record */
1965 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1966 failrec->start);
1967 did_repair = 1;
1968 goto out;
1969 }
1970
1971 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1972 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1973 failrec->start,
1974 EXTENT_LOCKED);
1975 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1976
1977 if (state && state->start == failrec->start) {
1978 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1979 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1980 failrec->len);
1981 if (num_copies > 1) {
1982 ret = repair_io_failure(map_tree, start, failrec->len,
1983 failrec->logical, page,
1984 failrec->failed_mirror);
1985 did_repair = !ret;
1986 }
1987 }
1988
1989out:
1990 if (!ret)
1991 ret = free_io_failure(inode, failrec, did_repair);
1992
1993 return ret;
1994}
1995
1996/*
1997 * this is a generic handler for readpage errors (default
1998 * readpage_io_failed_hook). if other copies exist, read those and write back
1999 * good data to the failed position. does not investigate in remapping the
2000 * failed extent elsewhere, hoping the device will be smart enough to do this as
2001 * needed
2002 */
2003
2004static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2005 u64 start, u64 end, int failed_mirror,
2006 struct extent_state *state)
2007{
2008 struct io_failure_record *failrec = NULL;
2009 u64 private;
2010 struct extent_map *em;
2011 struct inode *inode = page->mapping->host;
2012 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2013 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2014 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2015 struct bio *bio;
2016 int num_copies;
2017 int ret;
2018 int read_mode;
2019 u64 logical;
2020
2021 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2022
2023 ret = get_state_private(failure_tree, start, &private);
2024 if (ret) {
2025 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2026 if (!failrec)
2027 return -ENOMEM;
2028 failrec->start = start;
2029 failrec->len = end - start + 1;
2030 failrec->this_mirror = 0;
2031 failrec->bio_flags = 0;
2032 failrec->in_validation = 0;
2033
2034 read_lock(&em_tree->lock);
2035 em = lookup_extent_mapping(em_tree, start, failrec->len);
2036 if (!em) {
2037 read_unlock(&em_tree->lock);
2038 kfree(failrec);
2039 return -EIO;
2040 }
2041
2042 if (em->start > start || em->start + em->len < start) {
2043 free_extent_map(em);
2044 em = NULL;
2045 }
2046 read_unlock(&em_tree->lock);
2047
2048 if (!em || IS_ERR(em)) {
2049 kfree(failrec);
2050 return -EIO;
2051 }
2052 logical = start - em->start;
2053 logical = em->block_start + logical;
2054 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2055 logical = em->block_start;
2056 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2057 extent_set_compress_type(&failrec->bio_flags,
2058 em->compress_type);
2059 }
2060 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2061 "len=%llu\n", logical, start, failrec->len);
2062 failrec->logical = logical;
2063 free_extent_map(em);
2064
2065 /* set the bits in the private failure tree */
2066 ret = set_extent_bits(failure_tree, start, end,
2067 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2068 if (ret >= 0)
2069 ret = set_state_private(failure_tree, start,
2070 (u64)(unsigned long)failrec);
2071 /* set the bits in the inode's tree */
2072 if (ret >= 0)
2073 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2074 GFP_NOFS);
2075 if (ret < 0) {
2076 kfree(failrec);
2077 return ret;
2078 }
2079 } else {
2080 failrec = (struct io_failure_record *)(unsigned long)private;
2081 pr_debug("bio_readpage_error: (found) logical=%llu, "
2082 "start=%llu, len=%llu, validation=%d\n",
2083 failrec->logical, failrec->start, failrec->len,
2084 failrec->in_validation);
2085 /*
2086 * when data can be on disk more than twice, add to failrec here
2087 * (e.g. with a list for failed_mirror) to make
2088 * clean_io_failure() clean all those errors at once.
2089 */
2090 }
2091 num_copies = btrfs_num_copies(
2092 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2093 failrec->logical, failrec->len);
2094 if (num_copies == 1) {
2095 /*
2096 * we only have a single copy of the data, so don't bother with
2097 * all the retry and error correction code that follows. no
2098 * matter what the error is, it is very likely to persist.
2099 */
2100 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2101 "state=%p, num_copies=%d, next_mirror %d, "
2102 "failed_mirror %d\n", state, num_copies,
2103 failrec->this_mirror, failed_mirror);
2104 free_io_failure(inode, failrec, 0);
2105 return -EIO;
2106 }
2107
2108 if (!state) {
2109 spin_lock(&tree->lock);
2110 state = find_first_extent_bit_state(tree, failrec->start,
2111 EXTENT_LOCKED);
2112 if (state && state->start != failrec->start)
2113 state = NULL;
2114 spin_unlock(&tree->lock);
2115 }
2116
2117 /*
2118 * there are two premises:
2119 * a) deliver good data to the caller
2120 * b) correct the bad sectors on disk
2121 */
2122 if (failed_bio->bi_vcnt > 1) {
2123 /*
2124 * to fulfill b), we need to know the exact failing sectors, as
2125 * we don't want to rewrite any more than the failed ones. thus,
2126 * we need separate read requests for the failed bio
2127 *
2128 * if the following BUG_ON triggers, our validation request got
2129 * merged. we need separate requests for our algorithm to work.
2130 */
2131 BUG_ON(failrec->in_validation);
2132 failrec->in_validation = 1;
2133 failrec->this_mirror = failed_mirror;
2134 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2135 } else {
2136 /*
2137 * we're ready to fulfill a) and b) alongside. get a good copy
2138 * of the failed sector and if we succeed, we have setup
2139 * everything for repair_io_failure to do the rest for us.
2140 */
2141 if (failrec->in_validation) {
2142 BUG_ON(failrec->this_mirror != failed_mirror);
2143 failrec->in_validation = 0;
2144 failrec->this_mirror = 0;
2145 }
2146 failrec->failed_mirror = failed_mirror;
2147 failrec->this_mirror++;
2148 if (failrec->this_mirror == failed_mirror)
2149 failrec->this_mirror++;
2150 read_mode = READ_SYNC;
2151 }
2152
2153 if (!state || failrec->this_mirror > num_copies) {
2154 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2155 "next_mirror %d, failed_mirror %d\n", state,
2156 num_copies, failrec->this_mirror, failed_mirror);
2157 free_io_failure(inode, failrec, 0);
2158 return -EIO;
2159 }
2160
2161 bio = bio_alloc(GFP_NOFS, 1);
2162 bio->bi_private = state;
2163 bio->bi_end_io = failed_bio->bi_end_io;
2164 bio->bi_sector = failrec->logical >> 9;
2165 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2166 bio->bi_size = 0;
2167
2168 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2169
2170 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2171 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2172 failrec->this_mirror, num_copies, failrec->in_validation);
2173
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002174 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2175 failrec->this_mirror,
2176 failrec->bio_flags, 0);
2177 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002178}
2179
Chris Masond1310b22008-01-24 16:13:08 -05002180/* lots and lots of room for performance fixes in the end_bio funcs */
2181
Jeff Mahoney87826df2012-02-15 16:23:57 +01002182int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2183{
2184 int uptodate = (err == 0);
2185 struct extent_io_tree *tree;
2186 int ret;
2187
2188 tree = &BTRFS_I(page->mapping->host)->io_tree;
2189
2190 if (tree->ops && tree->ops->writepage_end_io_hook) {
2191 ret = tree->ops->writepage_end_io_hook(page, start,
2192 end, NULL, uptodate);
2193 if (ret)
2194 uptodate = 0;
2195 }
2196
2197 if (!uptodate && tree->ops &&
2198 tree->ops->writepage_io_failed_hook) {
2199 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2200 start, end, NULL);
2201 /* Writeback already completed */
2202 if (ret == 0)
2203 return 1;
2204 }
2205
2206 if (!uptodate) {
2207 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2208 ClearPageUptodate(page);
2209 SetPageError(page);
2210 }
2211 return 0;
2212}
2213
Chris Masond1310b22008-01-24 16:13:08 -05002214/*
2215 * after a writepage IO is done, we need to:
2216 * clear the uptodate bits on error
2217 * clear the writeback bits in the extent tree for this IO
2218 * end_page_writeback if the page has no more pending IO
2219 *
2220 * Scheduling is not allowed, so the extent state tree is expected
2221 * to have one and only one object corresponding to this IO.
2222 */
Chris Masond1310b22008-01-24 16:13:08 -05002223static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002224{
Chris Masond1310b22008-01-24 16:13:08 -05002225 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002226 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002227 u64 start;
2228 u64 end;
2229 int whole_page;
2230
Chris Masond1310b22008-01-24 16:13:08 -05002231 do {
2232 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002233 tree = &BTRFS_I(page->mapping->host)->io_tree;
2234
Chris Masond1310b22008-01-24 16:13:08 -05002235 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2236 bvec->bv_offset;
2237 end = start + bvec->bv_len - 1;
2238
2239 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2240 whole_page = 1;
2241 else
2242 whole_page = 0;
2243
2244 if (--bvec >= bio->bi_io_vec)
2245 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002246
Jeff Mahoney87826df2012-02-15 16:23:57 +01002247 if (end_extent_writepage(page, err, start, end))
2248 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002249
Chris Masond1310b22008-01-24 16:13:08 -05002250 if (whole_page)
2251 end_page_writeback(page);
2252 else
2253 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002254 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002255
Chris Masond1310b22008-01-24 16:13:08 -05002256 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002257}
2258
2259/*
2260 * after a readpage IO is done, we need to:
2261 * clear the uptodate bits on error
2262 * set the uptodate bits if things worked
2263 * set the page up to date if all extents in the tree are uptodate
2264 * clear the lock bit in the extent tree
2265 * unlock the page if there are no other extents locked for it
2266 *
2267 * Scheduling is not allowed, so the extent state tree is expected
2268 * to have one and only one object corresponding to this IO.
2269 */
Chris Masond1310b22008-01-24 16:13:08 -05002270static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002271{
2272 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002273 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2274 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002275 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002276 u64 start;
2277 u64 end;
2278 int whole_page;
2279 int ret;
2280
Chris Masond20f7042008-12-08 16:58:54 -05002281 if (err)
2282 uptodate = 0;
2283
Chris Masond1310b22008-01-24 16:13:08 -05002284 do {
2285 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002286 struct extent_state *cached = NULL;
2287 struct extent_state *state;
2288
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002289 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2290 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2291 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002292 tree = &BTRFS_I(page->mapping->host)->io_tree;
2293
Chris Masond1310b22008-01-24 16:13:08 -05002294 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2295 bvec->bv_offset;
2296 end = start + bvec->bv_len - 1;
2297
2298 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2299 whole_page = 1;
2300 else
2301 whole_page = 0;
2302
Chris Mason4125bf72010-02-03 18:18:45 +00002303 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002304 prefetchw(&bvec->bv_page->flags);
2305
Arne Jansen507903b2011-04-06 10:02:20 +00002306 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002307 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002308 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002309 /*
2310 * take a reference on the state, unlock will drop
2311 * the ref
2312 */
2313 cache_state(state, &cached);
2314 }
2315 spin_unlock(&tree->lock);
2316
Chris Masond1310b22008-01-24 16:13:08 -05002317 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002318 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002319 state);
Chris Masond1310b22008-01-24 16:13:08 -05002320 if (ret)
2321 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002322 else
2323 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002324 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002325 if (!uptodate) {
Jan Schmidt32240a92011-11-20 07:33:38 -05002326 int failed_mirror;
2327 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002328 /*
2329 * The generic bio_readpage_error handles errors the
2330 * following way: If possible, new read requests are
2331 * created and submitted and will end up in
2332 * end_bio_extent_readpage as well (if we're lucky, not
2333 * in the !uptodate case). In that case it returns 0 and
2334 * we just go on with the next page in our bio. If it
2335 * can't handle the error it will return -EIO and we
2336 * remain responsible for that page.
2337 */
2338 ret = bio_readpage_error(bio, page, start, end,
2339 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002340 if (ret == 0) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002341error_handled:
Chris Mason3b951512008-04-17 11:29:12 -04002342 uptodate =
2343 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002344 if (err)
2345 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002346 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002347 continue;
2348 }
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002349 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2350 ret = tree->ops->readpage_io_failed_hook(
2351 bio, page, start, end,
2352 failed_mirror, state);
2353 if (ret == 0)
2354 goto error_handled;
2355 }
Chris Mason7e383262008-04-09 16:28:12 -04002356 }
Chris Mason70dec802008-01-29 09:59:12 -05002357
Chris Mason771ed682008-11-06 22:02:51 -05002358 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002359 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002360 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002361 }
Arne Jansen507903b2011-04-06 10:02:20 +00002362 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002363
Chris Mason70dec802008-01-29 09:59:12 -05002364 if (whole_page) {
2365 if (uptodate) {
2366 SetPageUptodate(page);
2367 } else {
2368 ClearPageUptodate(page);
2369 SetPageError(page);
2370 }
Chris Masond1310b22008-01-24 16:13:08 -05002371 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002372 } else {
2373 if (uptodate) {
2374 check_page_uptodate(tree, page);
2375 } else {
2376 ClearPageUptodate(page);
2377 SetPageError(page);
2378 }
Chris Masond1310b22008-01-24 16:13:08 -05002379 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002380 }
Chris Mason4125bf72010-02-03 18:18:45 +00002381 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002382
2383 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002384}
2385
Miao Xie88f794e2010-11-22 03:02:55 +00002386struct bio *
2387btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2388 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002389{
2390 struct bio *bio;
2391
2392 bio = bio_alloc(gfp_flags, nr_vecs);
2393
2394 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2395 while (!bio && (nr_vecs /= 2))
2396 bio = bio_alloc(gfp_flags, nr_vecs);
2397 }
2398
2399 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002400 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002401 bio->bi_bdev = bdev;
2402 bio->bi_sector = first_sector;
2403 }
2404 return bio;
2405}
2406
Chris Masonc8b97812008-10-29 14:49:59 -04002407static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2408 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002409{
Chris Masond1310b22008-01-24 16:13:08 -05002410 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002411 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2412 struct page *page = bvec->bv_page;
2413 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002414 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002415
2416 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002417
David Woodhouse902b22f2008-08-20 08:51:49 -04002418 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002419
2420 bio_get(bio);
2421
Chris Mason065631f2008-02-20 12:07:25 -05002422 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002423 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002424 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002425 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002426 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002427
Chris Masond1310b22008-01-24 16:13:08 -05002428 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2429 ret = -EOPNOTSUPP;
2430 bio_put(bio);
2431 return ret;
2432}
2433
2434static int submit_extent_page(int rw, struct extent_io_tree *tree,
2435 struct page *page, sector_t sector,
2436 size_t size, unsigned long offset,
2437 struct block_device *bdev,
2438 struct bio **bio_ret,
2439 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002440 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002441 int mirror_num,
2442 unsigned long prev_bio_flags,
2443 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002444{
2445 int ret = 0;
2446 struct bio *bio;
2447 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002448 int contig = 0;
2449 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2450 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002451 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002452
2453 if (bio_ret && *bio_ret) {
2454 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002455 if (old_compressed)
2456 contig = bio->bi_sector == sector;
2457 else
2458 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2459 sector;
2460
2461 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002462 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002463 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2464 bio_flags)) ||
2465 bio_add_page(bio, page, page_size, offset) < page_size) {
2466 ret = submit_one_bio(rw, bio, mirror_num,
2467 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002468 bio = NULL;
2469 } else {
2470 return 0;
2471 }
2472 }
Chris Masonc8b97812008-10-29 14:49:59 -04002473 if (this_compressed)
2474 nr = BIO_MAX_PAGES;
2475 else
2476 nr = bio_get_nr_vecs(bdev);
2477
Miao Xie88f794e2010-11-22 03:02:55 +00002478 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002479 if (!bio)
2480 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002481
Chris Masonc8b97812008-10-29 14:49:59 -04002482 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002483 bio->bi_end_io = end_io_func;
2484 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002485
Chris Masond3977122009-01-05 21:25:51 -05002486 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002487 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002488 else
Chris Masonc8b97812008-10-29 14:49:59 -04002489 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002490
2491 return ret;
2492}
2493
2494void set_page_extent_mapped(struct page *page)
2495{
2496 if (!PagePrivate(page)) {
2497 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002498 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002499 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002500 }
2501}
2502
Christoph Hellwigb2950862008-12-02 09:54:17 -05002503static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002504{
Chris Masoneb14ab82011-02-10 12:35:00 -05002505 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002506 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2507}
2508
2509/*
2510 * basic readpage implementation. Locked extent state structs are inserted
2511 * into the tree that are removed when the IO is done (by the end_io
2512 * handlers)
2513 */
2514static int __extent_read_full_page(struct extent_io_tree *tree,
2515 struct page *page,
2516 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002517 struct bio **bio, int mirror_num,
2518 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002519{
2520 struct inode *inode = page->mapping->host;
2521 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2522 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2523 u64 end;
2524 u64 cur = start;
2525 u64 extent_offset;
2526 u64 last_byte = i_size_read(inode);
2527 u64 block_start;
2528 u64 cur_end;
2529 sector_t sector;
2530 struct extent_map *em;
2531 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002532 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002533 int ret;
2534 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002535 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002536 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002537 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002538 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002539 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002540
2541 set_page_extent_mapped(page);
2542
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002543 if (!PageUptodate(page)) {
2544 if (cleancache_get_page(page) == 0) {
2545 BUG_ON(blocksize != PAGE_SIZE);
2546 goto out;
2547 }
2548 }
2549
Chris Masond1310b22008-01-24 16:13:08 -05002550 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002551 while (1) {
2552 lock_extent(tree, start, end, GFP_NOFS);
2553 ordered = btrfs_lookup_ordered_extent(inode, start);
2554 if (!ordered)
2555 break;
2556 unlock_extent(tree, start, end, GFP_NOFS);
2557 btrfs_start_ordered_extent(inode, ordered, 1);
2558 btrfs_put_ordered_extent(ordered);
2559 }
Chris Masond1310b22008-01-24 16:13:08 -05002560
Chris Masonc8b97812008-10-29 14:49:59 -04002561 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2562 char *userpage;
2563 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2564
2565 if (zero_offset) {
2566 iosize = PAGE_CACHE_SIZE - zero_offset;
2567 userpage = kmap_atomic(page, KM_USER0);
2568 memset(userpage + zero_offset, 0, iosize);
2569 flush_dcache_page(page);
2570 kunmap_atomic(userpage, KM_USER0);
2571 }
2572 }
Chris Masond1310b22008-01-24 16:13:08 -05002573 while (cur <= end) {
2574 if (cur >= last_byte) {
2575 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002576 struct extent_state *cached = NULL;
2577
David Sterba306e16c2011-04-19 14:29:38 +02002578 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002579 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002580 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002581 flush_dcache_page(page);
2582 kunmap_atomic(userpage, KM_USER0);
2583 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002584 &cached, GFP_NOFS);
2585 unlock_extent_cached(tree, cur, cur + iosize - 1,
2586 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002587 break;
2588 }
David Sterba306e16c2011-04-19 14:29:38 +02002589 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002590 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002591 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002592 SetPageError(page);
2593 unlock_extent(tree, cur, end, GFP_NOFS);
2594 break;
2595 }
Chris Masond1310b22008-01-24 16:13:08 -05002596 extent_offset = cur - em->start;
2597 BUG_ON(extent_map_end(em) <= cur);
2598 BUG_ON(end < cur);
2599
Li Zefan261507a02010-12-17 14:21:50 +08002600 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002601 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002602 extent_set_compress_type(&this_bio_flag,
2603 em->compress_type);
2604 }
Chris Masonc8b97812008-10-29 14:49:59 -04002605
Chris Masond1310b22008-01-24 16:13:08 -05002606 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2607 cur_end = min(extent_map_end(em) - 1, end);
2608 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002609 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2610 disk_io_size = em->block_len;
2611 sector = em->block_start >> 9;
2612 } else {
2613 sector = (em->block_start + extent_offset) >> 9;
2614 disk_io_size = iosize;
2615 }
Chris Masond1310b22008-01-24 16:13:08 -05002616 bdev = em->bdev;
2617 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002618 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2619 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002620 free_extent_map(em);
2621 em = NULL;
2622
2623 /* we've found a hole, just zero and go on */
2624 if (block_start == EXTENT_MAP_HOLE) {
2625 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002626 struct extent_state *cached = NULL;
2627
Chris Masond1310b22008-01-24 16:13:08 -05002628 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002629 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002630 flush_dcache_page(page);
2631 kunmap_atomic(userpage, KM_USER0);
2632
2633 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002634 &cached, GFP_NOFS);
2635 unlock_extent_cached(tree, cur, cur + iosize - 1,
2636 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002637 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002638 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002639 continue;
2640 }
2641 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002642 if (test_range_bit(tree, cur, cur_end,
2643 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002644 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002645 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2646 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002647 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002648 continue;
2649 }
Chris Mason70dec802008-01-29 09:59:12 -05002650 /* we have an inline extent but it didn't get marked up
2651 * to date. Error out
2652 */
2653 if (block_start == EXTENT_MAP_INLINE) {
2654 SetPageError(page);
2655 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2656 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002657 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002658 continue;
2659 }
Chris Masond1310b22008-01-24 16:13:08 -05002660
2661 ret = 0;
2662 if (tree->ops && tree->ops->readpage_io_hook) {
2663 ret = tree->ops->readpage_io_hook(page, cur,
2664 cur + iosize - 1);
2665 }
2666 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002667 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2668 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002669 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002670 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002671 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002672 end_bio_extent_readpage, mirror_num,
2673 *bio_flags,
2674 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002675 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002676 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002677 }
2678 if (ret)
2679 SetPageError(page);
2680 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002681 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002682 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002683out:
Chris Masond1310b22008-01-24 16:13:08 -05002684 if (!nr) {
2685 if (!PageError(page))
2686 SetPageUptodate(page);
2687 unlock_page(page);
2688 }
2689 return 0;
2690}
2691
2692int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002693 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002694{
2695 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002696 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002697 int ret;
2698
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002699 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002700 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002701 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002702 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002703 return ret;
2704}
Chris Masond1310b22008-01-24 16:13:08 -05002705
Chris Mason11c83492009-04-20 15:50:09 -04002706static noinline void update_nr_written(struct page *page,
2707 struct writeback_control *wbc,
2708 unsigned long nr_written)
2709{
2710 wbc->nr_to_write -= nr_written;
2711 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2712 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2713 page->mapping->writeback_index = page->index + nr_written;
2714}
2715
Chris Masond1310b22008-01-24 16:13:08 -05002716/*
2717 * the writepage semantics are similar to regular writepage. extent
2718 * records are inserted to lock ranges in the tree, and as dirty areas
2719 * are found, they are marked writeback. Then the lock bits are removed
2720 * and the end_io handler clears the writeback ranges
2721 */
2722static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2723 void *data)
2724{
2725 struct inode *inode = page->mapping->host;
2726 struct extent_page_data *epd = data;
2727 struct extent_io_tree *tree = epd->tree;
2728 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2729 u64 delalloc_start;
2730 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2731 u64 end;
2732 u64 cur = start;
2733 u64 extent_offset;
2734 u64 last_byte = i_size_read(inode);
2735 u64 block_start;
2736 u64 iosize;
2737 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002738 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002739 struct extent_map *em;
2740 struct block_device *bdev;
2741 int ret;
2742 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002743 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002744 size_t blocksize;
2745 loff_t i_size = i_size_read(inode);
2746 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2747 u64 nr_delalloc;
2748 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002749 int page_started;
2750 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002751 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002752 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002753 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002754
Chris Masonffbd5172009-04-20 15:50:09 -04002755 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002756 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002757 else
2758 write_flags = WRITE;
2759
liubo1abe9b82011-03-24 11:18:59 +00002760 trace___extent_writepage(page, inode, wbc);
2761
Chris Masond1310b22008-01-24 16:13:08 -05002762 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002763
2764 ClearPageError(page);
2765
Chris Mason7f3c74f2008-07-18 12:01:11 -04002766 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002767 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002768 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002769 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002770 unlock_page(page);
2771 return 0;
2772 }
2773
2774 if (page->index == end_index) {
2775 char *userpage;
2776
Chris Masond1310b22008-01-24 16:13:08 -05002777 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002778 memset(userpage + pg_offset, 0,
2779 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002780 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002781 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002782 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002783 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002784
2785 set_page_extent_mapped(page);
2786
Josef Bacik9e487102011-08-01 12:08:18 -04002787 if (!tree->ops || !tree->ops->fill_delalloc)
2788 fill_delalloc = false;
2789
Chris Masond1310b22008-01-24 16:13:08 -05002790 delalloc_start = start;
2791 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002792 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002793 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002794 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002795 /*
2796 * make sure the wbc mapping index is at least updated
2797 * to this page.
2798 */
2799 update_nr_written(page, wbc, 0);
2800
Chris Masond3977122009-01-05 21:25:51 -05002801 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002802 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002803 page,
2804 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002805 &delalloc_end,
2806 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002807 if (nr_delalloc == 0) {
2808 delalloc_start = delalloc_end + 1;
2809 continue;
2810 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002811 ret = tree->ops->fill_delalloc(inode, page,
2812 delalloc_start,
2813 delalloc_end,
2814 &page_started,
2815 &nr_written);
2816 BUG_ON(ret);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002817 /*
2818 * delalloc_end is already one less than the total
2819 * length, so we don't subtract one from
2820 * PAGE_CACHE_SIZE
2821 */
2822 delalloc_to_write += (delalloc_end - delalloc_start +
2823 PAGE_CACHE_SIZE) >>
2824 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002825 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002826 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002827 if (wbc->nr_to_write < delalloc_to_write) {
2828 int thresh = 8192;
2829
2830 if (delalloc_to_write < thresh * 2)
2831 thresh = delalloc_to_write;
2832 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2833 thresh);
2834 }
Chris Masonc8b97812008-10-29 14:49:59 -04002835
Chris Mason771ed682008-11-06 22:02:51 -05002836 /* did the fill delalloc function already unlock and start
2837 * the IO?
2838 */
2839 if (page_started) {
2840 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002841 /*
2842 * we've unlocked the page, so we can't update
2843 * the mapping's writeback index, just update
2844 * nr_to_write.
2845 */
2846 wbc->nr_to_write -= nr_written;
2847 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002848 }
Chris Masonc8b97812008-10-29 14:49:59 -04002849 }
Chris Mason247e7432008-07-17 12:53:51 -04002850 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002851 ret = tree->ops->writepage_start_hook(page, start,
2852 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002853 if (ret) {
2854 /* Fixup worker will requeue */
2855 if (ret == -EBUSY)
2856 wbc->pages_skipped++;
2857 else
2858 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002859 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002860 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002861 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002862 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002863 }
2864 }
2865
Chris Mason11c83492009-04-20 15:50:09 -04002866 /*
2867 * we don't want to touch the inode after unlocking the page,
2868 * so we update the mapping writeback index now
2869 */
2870 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002871
Chris Masond1310b22008-01-24 16:13:08 -05002872 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002873 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002874 if (tree->ops && tree->ops->writepage_end_io_hook)
2875 tree->ops->writepage_end_io_hook(page, start,
2876 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002877 goto done;
2878 }
2879
Chris Masond1310b22008-01-24 16:13:08 -05002880 blocksize = inode->i_sb->s_blocksize;
2881
2882 while (cur <= end) {
2883 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002884 if (tree->ops && tree->ops->writepage_end_io_hook)
2885 tree->ops->writepage_end_io_hook(page, cur,
2886 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002887 break;
2888 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002889 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002890 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002891 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002892 SetPageError(page);
2893 break;
2894 }
2895
2896 extent_offset = cur - em->start;
2897 BUG_ON(extent_map_end(em) <= cur);
2898 BUG_ON(end < cur);
2899 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2900 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2901 sector = (em->block_start + extent_offset) >> 9;
2902 bdev = em->bdev;
2903 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002904 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002905 free_extent_map(em);
2906 em = NULL;
2907
Chris Masonc8b97812008-10-29 14:49:59 -04002908 /*
2909 * compressed and inline extents are written through other
2910 * paths in the FS
2911 */
2912 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002913 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002914 /*
2915 * end_io notification does not happen here for
2916 * compressed extents
2917 */
2918 if (!compressed && tree->ops &&
2919 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002920 tree->ops->writepage_end_io_hook(page, cur,
2921 cur + iosize - 1,
2922 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002923 else if (compressed) {
2924 /* we don't want to end_page_writeback on
2925 * a compressed extent. this happens
2926 * elsewhere
2927 */
2928 nr++;
2929 }
2930
2931 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002932 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002933 continue;
2934 }
Chris Masond1310b22008-01-24 16:13:08 -05002935 /* leave this out until we have a page_mkwrite call */
2936 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002937 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002938 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002939 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002940 continue;
2941 }
Chris Masonc8b97812008-10-29 14:49:59 -04002942
Chris Masond1310b22008-01-24 16:13:08 -05002943 if (tree->ops && tree->ops->writepage_io_hook) {
2944 ret = tree->ops->writepage_io_hook(page, cur,
2945 cur + iosize - 1);
2946 } else {
2947 ret = 0;
2948 }
Chris Mason1259ab72008-05-12 13:39:03 -04002949 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002950 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002951 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002952 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002953
Chris Masond1310b22008-01-24 16:13:08 -05002954 set_range_writeback(tree, cur, cur + iosize - 1);
2955 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002956 printk(KERN_ERR "btrfs warning page %lu not "
2957 "writeback, cur %llu end %llu\n",
2958 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002959 (unsigned long long)end);
2960 }
2961
Chris Masonffbd5172009-04-20 15:50:09 -04002962 ret = submit_extent_page(write_flags, tree, page,
2963 sector, iosize, pg_offset,
2964 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002965 end_bio_extent_writepage,
2966 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002967 if (ret)
2968 SetPageError(page);
2969 }
2970 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002971 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002972 nr++;
2973 }
2974done:
2975 if (nr == 0) {
2976 /* make sure the mapping tag for page dirty gets cleared */
2977 set_page_writeback(page);
2978 end_page_writeback(page);
2979 }
Chris Masond1310b22008-01-24 16:13:08 -05002980 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002981
Chris Mason11c83492009-04-20 15:50:09 -04002982done_unlocked:
2983
Chris Mason2c64c532009-09-02 15:04:12 -04002984 /* drop our reference on any cached states */
2985 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002986 return 0;
2987}
2988
Chris Masond1310b22008-01-24 16:13:08 -05002989/**
Chris Mason4bef0842008-09-08 11:18:08 -04002990 * 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 -05002991 * @mapping: address space structure to write
2992 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2993 * @writepage: function called for each page
2994 * @data: data passed to writepage function
2995 *
2996 * If a page is already under I/O, write_cache_pages() skips it, even
2997 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2998 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2999 * and msync() need to guarantee that all the data which was dirty at the time
3000 * the call was made get new I/O started against them. If wbc->sync_mode is
3001 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3002 * existing IO to complete.
3003 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003004static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003005 struct address_space *mapping,
3006 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003007 writepage_t writepage, void *data,
3008 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003009{
Chris Masond1310b22008-01-24 16:13:08 -05003010 int ret = 0;
3011 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003012 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003013 struct pagevec pvec;
3014 int nr_pages;
3015 pgoff_t index;
3016 pgoff_t end; /* Inclusive */
3017 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003018 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003019
Chris Masond1310b22008-01-24 16:13:08 -05003020 pagevec_init(&pvec, 0);
3021 if (wbc->range_cyclic) {
3022 index = mapping->writeback_index; /* Start from prev offset */
3023 end = -1;
3024 } else {
3025 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3026 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003027 scanned = 1;
3028 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003029 if (wbc->sync_mode == WB_SYNC_ALL)
3030 tag = PAGECACHE_TAG_TOWRITE;
3031 else
3032 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003033retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003034 if (wbc->sync_mode == WB_SYNC_ALL)
3035 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003036 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003037 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3038 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003039 unsigned i;
3040
3041 scanned = 1;
3042 for (i = 0; i < nr_pages; i++) {
3043 struct page *page = pvec.pages[i];
3044
3045 /*
3046 * At this point we hold neither mapping->tree_lock nor
3047 * lock on the page itself: the page may be truncated or
3048 * invalidated (changing page->mapping to NULL), or even
3049 * swizzled back from swapper_space to tmpfs file
3050 * mapping
3051 */
Chris Mason01d658f2011-11-01 10:08:06 -04003052 if (tree->ops &&
3053 tree->ops->write_cache_pages_lock_hook) {
3054 tree->ops->write_cache_pages_lock_hook(page,
3055 data, flush_fn);
3056 } else {
3057 if (!trylock_page(page)) {
3058 flush_fn(data);
3059 lock_page(page);
3060 }
3061 }
Chris Masond1310b22008-01-24 16:13:08 -05003062
3063 if (unlikely(page->mapping != mapping)) {
3064 unlock_page(page);
3065 continue;
3066 }
3067
3068 if (!wbc->range_cyclic && page->index > end) {
3069 done = 1;
3070 unlock_page(page);
3071 continue;
3072 }
3073
Chris Masond2c3f4f2008-11-19 12:44:22 -05003074 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003075 if (PageWriteback(page))
3076 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003077 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003078 }
Chris Masond1310b22008-01-24 16:13:08 -05003079
3080 if (PageWriteback(page) ||
3081 !clear_page_dirty_for_io(page)) {
3082 unlock_page(page);
3083 continue;
3084 }
3085
3086 ret = (*writepage)(page, wbc, data);
3087
3088 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3089 unlock_page(page);
3090 ret = 0;
3091 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003092 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003093 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003094
3095 /*
3096 * the filesystem may choose to bump up nr_to_write.
3097 * We have to make sure to honor the new nr_to_write
3098 * at any time
3099 */
3100 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003101 }
3102 pagevec_release(&pvec);
3103 cond_resched();
3104 }
3105 if (!scanned && !done) {
3106 /*
3107 * We hit the last page and there is more work to be done: wrap
3108 * back to the start of the file
3109 */
3110 scanned = 1;
3111 index = 0;
3112 goto retry;
3113 }
Chris Masond1310b22008-01-24 16:13:08 -05003114 return ret;
3115}
Chris Masond1310b22008-01-24 16:13:08 -05003116
Chris Masonffbd5172009-04-20 15:50:09 -04003117static void flush_epd_write_bio(struct extent_page_data *epd)
3118{
3119 if (epd->bio) {
3120 if (epd->sync_io)
3121 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3122 else
3123 submit_one_bio(WRITE, epd->bio, 0, 0);
3124 epd->bio = NULL;
3125 }
3126}
3127
Chris Masond2c3f4f2008-11-19 12:44:22 -05003128static noinline void flush_write_bio(void *data)
3129{
3130 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003131 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003132}
3133
Chris Masond1310b22008-01-24 16:13:08 -05003134int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3135 get_extent_t *get_extent,
3136 struct writeback_control *wbc)
3137{
3138 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003139 struct extent_page_data epd = {
3140 .bio = NULL,
3141 .tree = tree,
3142 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003143 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003144 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003145 };
Chris Masond1310b22008-01-24 16:13:08 -05003146
Chris Masond1310b22008-01-24 16:13:08 -05003147 ret = __extent_writepage(page, wbc, &epd);
3148
Chris Masonffbd5172009-04-20 15:50:09 -04003149 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003150 return ret;
3151}
Chris Masond1310b22008-01-24 16:13:08 -05003152
Chris Mason771ed682008-11-06 22:02:51 -05003153int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3154 u64 start, u64 end, get_extent_t *get_extent,
3155 int mode)
3156{
3157 int ret = 0;
3158 struct address_space *mapping = inode->i_mapping;
3159 struct page *page;
3160 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3161 PAGE_CACHE_SHIFT;
3162
3163 struct extent_page_data epd = {
3164 .bio = NULL,
3165 .tree = tree,
3166 .get_extent = get_extent,
3167 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003168 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003169 };
3170 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003171 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003172 .nr_to_write = nr_pages * 2,
3173 .range_start = start,
3174 .range_end = end + 1,
3175 };
3176
Chris Masond3977122009-01-05 21:25:51 -05003177 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003178 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3179 if (clear_page_dirty_for_io(page))
3180 ret = __extent_writepage(page, &wbc_writepages, &epd);
3181 else {
3182 if (tree->ops && tree->ops->writepage_end_io_hook)
3183 tree->ops->writepage_end_io_hook(page, start,
3184 start + PAGE_CACHE_SIZE - 1,
3185 NULL, 1);
3186 unlock_page(page);
3187 }
3188 page_cache_release(page);
3189 start += PAGE_CACHE_SIZE;
3190 }
3191
Chris Masonffbd5172009-04-20 15:50:09 -04003192 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003193 return ret;
3194}
Chris Masond1310b22008-01-24 16:13:08 -05003195
3196int extent_writepages(struct extent_io_tree *tree,
3197 struct address_space *mapping,
3198 get_extent_t *get_extent,
3199 struct writeback_control *wbc)
3200{
3201 int ret = 0;
3202 struct extent_page_data epd = {
3203 .bio = NULL,
3204 .tree = tree,
3205 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003206 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003207 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003208 };
3209
Chris Mason4bef0842008-09-08 11:18:08 -04003210 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003211 __extent_writepage, &epd,
3212 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003213 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003214 return ret;
3215}
Chris Masond1310b22008-01-24 16:13:08 -05003216
3217int extent_readpages(struct extent_io_tree *tree,
3218 struct address_space *mapping,
3219 struct list_head *pages, unsigned nr_pages,
3220 get_extent_t get_extent)
3221{
3222 struct bio *bio = NULL;
3223 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003224 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003225
Chris Masond1310b22008-01-24 16:13:08 -05003226 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3227 struct page *page = list_entry(pages->prev, struct page, lru);
3228
3229 prefetchw(&page->flags);
3230 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003231 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003232 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003233 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003234 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003235 }
3236 page_cache_release(page);
3237 }
Chris Masond1310b22008-01-24 16:13:08 -05003238 BUG_ON(!list_empty(pages));
3239 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003240 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003241 return 0;
3242}
Chris Masond1310b22008-01-24 16:13:08 -05003243
3244/*
3245 * basic invalidatepage code, this waits on any locked or writeback
3246 * ranges corresponding to the page, and then deletes any extent state
3247 * records from the tree
3248 */
3249int extent_invalidatepage(struct extent_io_tree *tree,
3250 struct page *page, unsigned long offset)
3251{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003252 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003253 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3254 u64 end = start + PAGE_CACHE_SIZE - 1;
3255 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3256
Chris Masond3977122009-01-05 21:25:51 -05003257 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003258 if (start > end)
3259 return 0;
3260
Josef Bacik2ac55d42010-02-03 19:33:23 +00003261 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003262 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003263 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003264 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3265 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003266 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003267 return 0;
3268}
Chris Masond1310b22008-01-24 16:13:08 -05003269
3270/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003271 * a helper for releasepage, this tests for areas of the page that
3272 * are locked or under IO and drops the related state bits if it is safe
3273 * to drop the page.
3274 */
3275int try_release_extent_state(struct extent_map_tree *map,
3276 struct extent_io_tree *tree, struct page *page,
3277 gfp_t mask)
3278{
3279 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3280 u64 end = start + PAGE_CACHE_SIZE - 1;
3281 int ret = 1;
3282
Chris Mason211f90e2008-07-18 11:56:15 -04003283 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003284 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003285 ret = 0;
3286 else {
3287 if ((mask & GFP_NOFS) == GFP_NOFS)
3288 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003289 /*
3290 * at this point we can safely clear everything except the
3291 * locked bit and the nodatasum bit
3292 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003293 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003294 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3295 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003296
3297 /* if clear_extent_bit failed for enomem reasons,
3298 * we can't allow the release to continue.
3299 */
3300 if (ret < 0)
3301 ret = 0;
3302 else
3303 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003304 }
3305 return ret;
3306}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003307
3308/*
Chris Masond1310b22008-01-24 16:13:08 -05003309 * a helper for releasepage. As long as there are no locked extents
3310 * in the range corresponding to the page, both state records and extent
3311 * map records are removed
3312 */
3313int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003314 struct extent_io_tree *tree, struct page *page,
3315 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003316{
3317 struct extent_map *em;
3318 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3319 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003320
Chris Mason70dec802008-01-29 09:59:12 -05003321 if ((mask & __GFP_WAIT) &&
3322 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003323 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003324 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003325 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003326 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003327 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003328 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003329 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003330 break;
3331 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003332 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3333 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003334 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003335 free_extent_map(em);
3336 break;
3337 }
3338 if (!test_range_bit(tree, em->start,
3339 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003340 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003341 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003342 remove_extent_mapping(map, em);
3343 /* once for the rb tree */
3344 free_extent_map(em);
3345 }
3346 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003347 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003348
3349 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003350 free_extent_map(em);
3351 }
Chris Masond1310b22008-01-24 16:13:08 -05003352 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003353 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003354}
Chris Masond1310b22008-01-24 16:13:08 -05003355
Chris Masonec29ed52011-02-23 16:23:20 -05003356/*
3357 * helper function for fiemap, which doesn't want to see any holes.
3358 * This maps until we find something past 'last'
3359 */
3360static struct extent_map *get_extent_skip_holes(struct inode *inode,
3361 u64 offset,
3362 u64 last,
3363 get_extent_t *get_extent)
3364{
3365 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3366 struct extent_map *em;
3367 u64 len;
3368
3369 if (offset >= last)
3370 return NULL;
3371
3372 while(1) {
3373 len = last - offset;
3374 if (len == 0)
3375 break;
3376 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3377 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003378 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003379 return em;
3380
3381 /* if this isn't a hole return it */
3382 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3383 em->block_start != EXTENT_MAP_HOLE) {
3384 return em;
3385 }
3386
3387 /* this is a hole, advance to the next extent */
3388 offset = extent_map_end(em);
3389 free_extent_map(em);
3390 if (offset >= last)
3391 break;
3392 }
3393 return NULL;
3394}
3395
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003396int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3397 __u64 start, __u64 len, get_extent_t *get_extent)
3398{
Josef Bacik975f84f2010-11-23 19:36:57 +00003399 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003400 u64 off = start;
3401 u64 max = start + len;
3402 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003403 u32 found_type;
3404 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003405 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003406 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003407 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003408 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003409 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003410 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003411 struct btrfs_path *path;
3412 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003413 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003414 u64 em_start = 0;
3415 u64 em_len = 0;
3416 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003417 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003418
3419 if (len == 0)
3420 return -EINVAL;
3421
Josef Bacik975f84f2010-11-23 19:36:57 +00003422 path = btrfs_alloc_path();
3423 if (!path)
3424 return -ENOMEM;
3425 path->leave_spinning = 1;
3426
Josef Bacik4d479cf2011-11-17 11:34:31 -05003427 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3428 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3429
Chris Masonec29ed52011-02-23 16:23:20 -05003430 /*
3431 * lookup the last file extent. We're not using i_size here
3432 * because there might be preallocation past i_size
3433 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003434 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003435 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003436 if (ret < 0) {
3437 btrfs_free_path(path);
3438 return ret;
3439 }
3440 WARN_ON(!ret);
3441 path->slots[0]--;
3442 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3443 struct btrfs_file_extent_item);
3444 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3445 found_type = btrfs_key_type(&found_key);
3446
Chris Masonec29ed52011-02-23 16:23:20 -05003447 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003448 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003449 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003450 /* have to trust i_size as the end */
3451 last = (u64)-1;
3452 last_for_get_extent = isize;
3453 } else {
3454 /*
3455 * remember the start of the last extent. There are a
3456 * bunch of different factors that go into the length of the
3457 * extent, so its much less complex to remember where it started
3458 */
3459 last = found_key.offset;
3460 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003461 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003462 btrfs_free_path(path);
3463
Chris Masonec29ed52011-02-23 16:23:20 -05003464 /*
3465 * we might have some extents allocated but more delalloc past those
3466 * extents. so, we trust isize unless the start of the last extent is
3467 * beyond isize
3468 */
3469 if (last < isize) {
3470 last = (u64)-1;
3471 last_for_get_extent = isize;
3472 }
3473
Josef Bacik2ac55d42010-02-03 19:33:23 +00003474 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3475 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003476
Josef Bacik4d479cf2011-11-17 11:34:31 -05003477 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003478 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003479 if (!em)
3480 goto out;
3481 if (IS_ERR(em)) {
3482 ret = PTR_ERR(em);
3483 goto out;
3484 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003485
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003486 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003487 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003488
Chris Masonea8efc72011-03-08 11:54:40 -05003489 /* break if the extent we found is outside the range */
3490 if (em->start >= max || extent_map_end(em) < off)
3491 break;
3492
3493 /*
3494 * get_extent may return an extent that starts before our
3495 * requested range. We have to make sure the ranges
3496 * we return to fiemap always move forward and don't
3497 * overlap, so adjust the offsets here
3498 */
3499 em_start = max(em->start, off);
3500
3501 /*
3502 * record the offset from the start of the extent
3503 * for adjusting the disk offset below
3504 */
3505 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003506 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003507 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003508 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003509 disko = 0;
3510 flags = 0;
3511
Chris Masonea8efc72011-03-08 11:54:40 -05003512 /*
3513 * bump off for our next call to get_extent
3514 */
3515 off = extent_map_end(em);
3516 if (off >= max)
3517 end = 1;
3518
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003519 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003520 end = 1;
3521 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003522 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003523 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3524 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003525 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003526 flags |= (FIEMAP_EXTENT_DELALLOC |
3527 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003528 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003529 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003530 }
3531 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3532 flags |= FIEMAP_EXTENT_ENCODED;
3533
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003534 free_extent_map(em);
3535 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003536 if ((em_start >= last) || em_len == (u64)-1 ||
3537 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003538 flags |= FIEMAP_EXTENT_LAST;
3539 end = 1;
3540 }
3541
Chris Masonec29ed52011-02-23 16:23:20 -05003542 /* now scan forward to see if this is really the last extent. */
3543 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3544 get_extent);
3545 if (IS_ERR(em)) {
3546 ret = PTR_ERR(em);
3547 goto out;
3548 }
3549 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003550 flags |= FIEMAP_EXTENT_LAST;
3551 end = 1;
3552 }
Chris Masonec29ed52011-02-23 16:23:20 -05003553 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3554 em_len, flags);
3555 if (ret)
3556 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003557 }
3558out_free:
3559 free_extent_map(em);
3560out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003561 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3562 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003563 return ret;
3564}
3565
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003566inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003567 unsigned long i)
3568{
3569 struct page *p;
3570 struct address_space *mapping;
3571
3572 if (i == 0)
3573 return eb->first_page;
3574 i += eb->start >> PAGE_CACHE_SHIFT;
3575 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003576 if (!mapping)
3577 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003578
3579 /*
3580 * extent_buffer_page is only called after pinning the page
3581 * by increasing the reference count. So we know the page must
3582 * be in the radix tree.
3583 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003584 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003585 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003586 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003587
Chris Masond1310b22008-01-24 16:13:08 -05003588 return p;
3589}
3590
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003591inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003592{
Chris Mason6af118ce2008-07-22 11:18:07 -04003593 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3594 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003595}
3596
Chris Masond1310b22008-01-24 16:13:08 -05003597static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3598 u64 start,
3599 unsigned long len,
3600 gfp_t mask)
3601{
3602 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003603#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003604 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003605#endif
Chris Masond1310b22008-01-24 16:13:08 -05003606
Chris Masond1310b22008-01-24 16:13:08 -05003607 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003608 if (eb == NULL)
3609 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003610 eb->start = start;
3611 eb->len = len;
Chris Masonbd681512011-07-16 15:23:14 -04003612 rwlock_init(&eb->lock);
3613 atomic_set(&eb->write_locks, 0);
3614 atomic_set(&eb->read_locks, 0);
3615 atomic_set(&eb->blocking_readers, 0);
3616 atomic_set(&eb->blocking_writers, 0);
3617 atomic_set(&eb->spinning_readers, 0);
3618 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003619 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003620 init_waitqueue_head(&eb->write_lock_wq);
3621 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003622
Chris Mason39351272009-02-04 09:24:05 -05003623#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003624 spin_lock_irqsave(&leak_lock, flags);
3625 list_add(&eb->leak_list, &buffers);
3626 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003627#endif
Chris Masond1310b22008-01-24 16:13:08 -05003628 atomic_set(&eb->refs, 1);
3629
3630 return eb;
3631}
3632
3633static void __free_extent_buffer(struct extent_buffer *eb)
3634{
Chris Mason39351272009-02-04 09:24:05 -05003635#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003636 unsigned long flags;
3637 spin_lock_irqsave(&leak_lock, flags);
3638 list_del(&eb->leak_list);
3639 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003640#endif
Chris Masond1310b22008-01-24 16:13:08 -05003641 kmem_cache_free(extent_buffer_cache, eb);
3642}
3643
Miao Xie897ca6e92010-10-26 20:57:29 -04003644/*
3645 * Helper for releasing extent buffer page.
3646 */
3647static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3648 unsigned long start_idx)
3649{
3650 unsigned long index;
3651 struct page *page;
3652
3653 if (!eb->first_page)
3654 return;
3655
3656 index = num_extent_pages(eb->start, eb->len);
3657 if (start_idx >= index)
3658 return;
3659
3660 do {
3661 index--;
3662 page = extent_buffer_page(eb, index);
3663 if (page)
3664 page_cache_release(page);
3665 } while (index != start_idx);
3666}
3667
3668/*
3669 * Helper for releasing the extent buffer.
3670 */
3671static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3672{
3673 btrfs_release_extent_buffer_page(eb, 0);
3674 __free_extent_buffer(eb);
3675}
3676
Chris Masond1310b22008-01-24 16:13:08 -05003677struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3678 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003679 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003680{
3681 unsigned long num_pages = num_extent_pages(start, len);
3682 unsigned long i;
3683 unsigned long index = start >> PAGE_CACHE_SHIFT;
3684 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003685 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003686 struct page *p;
3687 struct address_space *mapping = tree->mapping;
3688 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003689 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003690
Miao Xie19fe0a82010-10-26 20:57:29 -04003691 rcu_read_lock();
3692 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3693 if (eb && atomic_inc_not_zero(&eb->refs)) {
3694 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003695 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003696 return eb;
3697 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003698 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003699
David Sterbaba144192011-04-21 01:12:06 +02003700 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003701 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003702 return NULL;
3703
Chris Masond1310b22008-01-24 16:13:08 -05003704 if (page0) {
3705 eb->first_page = page0;
3706 i = 1;
3707 index++;
3708 page_cache_get(page0);
3709 mark_page_accessed(page0);
3710 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003711 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003712 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003713 } else {
3714 i = 0;
3715 }
3716 for (; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003717 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003718 if (!p) {
3719 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003720 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003721 }
3722 set_page_extent_mapped(p);
3723 mark_page_accessed(p);
3724 if (i == 0) {
3725 eb->first_page = p;
3726 set_page_extent_head(p, len);
3727 } else {
3728 set_page_private(p, EXTENT_PAGE_PRIVATE);
3729 }
3730 if (!PageUptodate(p))
3731 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003732
3733 /*
3734 * see below about how we avoid a nasty race with release page
3735 * and why we unlock later
3736 */
3737 if (i != 0)
3738 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003739 }
3740 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003741 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003742
Miao Xie19fe0a82010-10-26 20:57:29 -04003743 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3744 if (ret)
3745 goto free_eb;
3746
Chris Mason6af118ce2008-07-22 11:18:07 -04003747 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003748 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3749 if (ret == -EEXIST) {
3750 exists = radix_tree_lookup(&tree->buffer,
3751 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003752 /* add one reference for the caller */
3753 atomic_inc(&exists->refs);
3754 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003755 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003756 goto free_eb;
3757 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003758 /* add one reference for the tree */
3759 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003760 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003761 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003762
3763 /*
3764 * there is a race where release page may have
3765 * tried to find this extent buffer in the radix
3766 * but failed. It will tell the VM it is safe to
3767 * reclaim the, and it will clear the page private bit.
3768 * We must make sure to set the page private bit properly
3769 * after the extent buffer is in the radix tree so
3770 * it doesn't get lost
3771 */
3772 set_page_extent_mapped(eb->first_page);
3773 set_page_extent_head(eb->first_page, eb->len);
3774 if (!page0)
3775 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003776 return eb;
3777
Chris Mason6af118ce2008-07-22 11:18:07 -04003778free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003779 if (eb->first_page && !page0)
3780 unlock_page(eb->first_page);
3781
Chris Masond1310b22008-01-24 16:13:08 -05003782 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003783 return exists;
Miao Xie897ca6e92010-10-26 20:57:29 -04003784 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003785 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003786}
Chris Masond1310b22008-01-24 16:13:08 -05003787
3788struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003789 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003790{
Chris Masond1310b22008-01-24 16:13:08 -05003791 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003792
Miao Xie19fe0a82010-10-26 20:57:29 -04003793 rcu_read_lock();
3794 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3795 if (eb && atomic_inc_not_zero(&eb->refs)) {
3796 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003797 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003798 return eb;
3799 }
3800 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003801
Miao Xie19fe0a82010-10-26 20:57:29 -04003802 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003803}
Chris Masond1310b22008-01-24 16:13:08 -05003804
3805void free_extent_buffer(struct extent_buffer *eb)
3806{
Chris Masond1310b22008-01-24 16:13:08 -05003807 if (!eb)
3808 return;
3809
3810 if (!atomic_dec_and_test(&eb->refs))
3811 return;
3812
Chris Mason6af118ce2008-07-22 11:18:07 -04003813 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003814}
Chris Masond1310b22008-01-24 16:13:08 -05003815
3816int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3817 struct extent_buffer *eb)
3818{
Chris Masond1310b22008-01-24 16:13:08 -05003819 unsigned long i;
3820 unsigned long num_pages;
3821 struct page *page;
3822
Chris Masond1310b22008-01-24 16:13:08 -05003823 num_pages = num_extent_pages(eb->start, eb->len);
3824
3825 for (i = 0; i < num_pages; i++) {
3826 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003827 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003828 continue;
3829
Chris Masona61e6f22008-07-22 11:18:08 -04003830 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003831 WARN_ON(!PagePrivate(page));
3832
3833 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003834 if (i == 0)
3835 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003836
Chris Masond1310b22008-01-24 16:13:08 -05003837 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003838 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003839 if (!PageDirty(page)) {
3840 radix_tree_tag_clear(&page->mapping->page_tree,
3841 page_index(page),
3842 PAGECACHE_TAG_DIRTY);
3843 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003844 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04003845 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04003846 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003847 }
3848 return 0;
3849}
Chris Masond1310b22008-01-24 16:13:08 -05003850
Chris Masond1310b22008-01-24 16:13:08 -05003851int set_extent_buffer_dirty(struct extent_io_tree *tree,
3852 struct extent_buffer *eb)
3853{
3854 unsigned long i;
3855 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003856 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003857
Chris Masonb9473432009-03-13 11:00:37 -04003858 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003859 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003860 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003861 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003862 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003863}
Chris Masond1310b22008-01-24 16:13:08 -05003864
Chris Mason19b6caf2011-07-25 06:50:50 -04003865static int __eb_straddles_pages(u64 start, u64 len)
3866{
3867 if (len < PAGE_CACHE_SIZE)
3868 return 1;
3869 if (start & (PAGE_CACHE_SIZE - 1))
3870 return 1;
3871 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3872 return 1;
3873 return 0;
3874}
3875
3876static int eb_straddles_pages(struct extent_buffer *eb)
3877{
3878 return __eb_straddles_pages(eb->start, eb->len);
3879}
3880
Chris Mason1259ab72008-05-12 13:39:03 -04003881int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003882 struct extent_buffer *eb,
3883 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003884{
3885 unsigned long i;
3886 struct page *page;
3887 unsigned long num_pages;
3888
3889 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003890 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003891
Chris Mason50653192012-02-22 12:36:24 -05003892 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3893 cached_state, GFP_NOFS);
3894
Chris Mason1259ab72008-05-12 13:39:03 -04003895 for (i = 0; i < num_pages; i++) {
3896 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003897 if (page)
3898 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003899 }
3900 return 0;
3901}
3902
Chris Masond1310b22008-01-24 16:13:08 -05003903int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3904 struct extent_buffer *eb)
3905{
3906 unsigned long i;
3907 struct page *page;
3908 unsigned long num_pages;
3909
3910 num_pages = num_extent_pages(eb->start, eb->len);
3911
Chris Mason19b6caf2011-07-25 06:50:50 -04003912 if (eb_straddles_pages(eb)) {
3913 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3914 NULL, GFP_NOFS);
3915 }
Chris Masond1310b22008-01-24 16:13:08 -05003916 for (i = 0; i < num_pages; i++) {
3917 page = extent_buffer_page(eb, i);
3918 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3919 ((i == num_pages - 1) &&
3920 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3921 check_page_uptodate(tree, page);
3922 continue;
3923 }
3924 SetPageUptodate(page);
3925 }
3926 return 0;
3927}
Chris Masond1310b22008-01-24 16:13:08 -05003928
Chris Masonce9adaa2008-04-09 16:28:12 -04003929int extent_range_uptodate(struct extent_io_tree *tree,
3930 u64 start, u64 end)
3931{
3932 struct page *page;
3933 int ret;
3934 int pg_uptodate = 1;
3935 int uptodate;
3936 unsigned long index;
3937
Chris Mason19b6caf2011-07-25 06:50:50 -04003938 if (__eb_straddles_pages(start, end - start + 1)) {
3939 ret = test_range_bit(tree, start, end,
3940 EXTENT_UPTODATE, 1, NULL);
3941 if (ret)
3942 return 1;
3943 }
Chris Masond3977122009-01-05 21:25:51 -05003944 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003945 index = start >> PAGE_CACHE_SHIFT;
3946 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05003947 if (!page)
3948 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04003949 uptodate = PageUptodate(page);
3950 page_cache_release(page);
3951 if (!uptodate) {
3952 pg_uptodate = 0;
3953 break;
3954 }
3955 start += PAGE_CACHE_SIZE;
3956 }
3957 return pg_uptodate;
3958}
3959
Chris Masond1310b22008-01-24 16:13:08 -05003960int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003961 struct extent_buffer *eb,
3962 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003963{
Chris Mason728131d2008-04-09 16:28:12 -04003964 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003965 unsigned long num_pages;
3966 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003967 struct page *page;
3968 int pg_uptodate = 1;
3969
Chris Masonb4ce94d2009-02-04 09:25:08 -05003970 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003971 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003972
Chris Mason19b6caf2011-07-25 06:50:50 -04003973 if (eb_straddles_pages(eb)) {
3974 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3975 EXTENT_UPTODATE, 1, cached_state);
3976 if (ret)
3977 return ret;
3978 }
Chris Mason728131d2008-04-09 16:28:12 -04003979
3980 num_pages = num_extent_pages(eb->start, eb->len);
3981 for (i = 0; i < num_pages; i++) {
3982 page = extent_buffer_page(eb, i);
3983 if (!PageUptodate(page)) {
3984 pg_uptodate = 0;
3985 break;
3986 }
3987 }
Chris Mason42352982008-04-28 16:40:52 -04003988 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003989}
Chris Masond1310b22008-01-24 16:13:08 -05003990
3991int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02003992 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003993 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003994{
3995 unsigned long i;
3996 unsigned long start_i;
3997 struct page *page;
3998 int err;
3999 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004000 int locked_pages = 0;
4001 int all_uptodate = 1;
4002 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004003 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05004004 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004005 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004006
Chris Masonb4ce94d2009-02-04 09:25:08 -05004007 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004008 return 0;
4009
Chris Mason19b6caf2011-07-25 06:50:50 -04004010 if (eb_straddles_pages(eb)) {
4011 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4012 EXTENT_UPTODATE, 1, NULL)) {
4013 return 0;
4014 }
Chris Masond1310b22008-01-24 16:13:08 -05004015 }
4016
4017 if (start) {
4018 WARN_ON(start < eb->start);
4019 start_i = (start >> PAGE_CACHE_SHIFT) -
4020 (eb->start >> PAGE_CACHE_SHIFT);
4021 } else {
4022 start_i = 0;
4023 }
4024
4025 num_pages = num_extent_pages(eb->start, eb->len);
4026 for (i = start_i; i < num_pages; i++) {
4027 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004028 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004029 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004030 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004031 } else {
4032 lock_page(page);
4033 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004034 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05004035 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004036 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004037 }
4038 if (all_uptodate) {
4039 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004040 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004041 goto unlock_exit;
4042 }
4043
4044 for (i = start_i; i < num_pages; i++) {
4045 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05004046
4047 WARN_ON(!PagePrivate(page));
4048
4049 set_page_extent_mapped(page);
4050 if (i == 0)
4051 set_page_extent_head(page, eb->len);
4052
Chris Masonce9adaa2008-04-09 16:28:12 -04004053 if (inc_all_pages)
4054 page_cache_get(page);
4055 if (!PageUptodate(page)) {
4056 if (start_i == 0)
4057 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04004058 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004059 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004060 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004061 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004062 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004063 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004064 } else {
4065 unlock_page(page);
4066 }
4067 }
4068
Chris Masona86c12c2008-02-07 10:50:54 -05004069 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04004070 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05004071
Arne Jansenbb82ab82011-06-10 14:06:53 +02004072 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004073 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004074
Chris Masond1310b22008-01-24 16:13:08 -05004075 for (i = start_i; i < num_pages; i++) {
4076 page = extent_buffer_page(eb, i);
4077 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004078 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004079 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004080 }
Chris Masond3977122009-01-05 21:25:51 -05004081
Chris Masond1310b22008-01-24 16:13:08 -05004082 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004083 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004084 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004085
4086unlock_exit:
4087 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004088 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004089 page = extent_buffer_page(eb, i);
4090 i++;
4091 unlock_page(page);
4092 locked_pages--;
4093 }
4094 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004095}
Chris Masond1310b22008-01-24 16:13:08 -05004096
4097void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4098 unsigned long start,
4099 unsigned long len)
4100{
4101 size_t cur;
4102 size_t offset;
4103 struct page *page;
4104 char *kaddr;
4105 char *dst = (char *)dstv;
4106 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4107 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004108
4109 WARN_ON(start > eb->len);
4110 WARN_ON(start + len > eb->start + eb->len);
4111
4112 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4113
Chris Masond3977122009-01-05 21:25:51 -05004114 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004115 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004116
4117 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004118 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004119 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004120
4121 dst += cur;
4122 len -= cur;
4123 offset = 0;
4124 i++;
4125 }
4126}
Chris Masond1310b22008-01-24 16:13:08 -05004127
4128int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004129 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004130 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004131 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004132{
4133 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4134 char *kaddr;
4135 struct page *p;
4136 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4137 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4138 unsigned long end_i = (start_offset + start + min_len - 1) >>
4139 PAGE_CACHE_SHIFT;
4140
4141 if (i != end_i)
4142 return -EINVAL;
4143
4144 if (i == 0) {
4145 offset = start_offset;
4146 *map_start = 0;
4147 } else {
4148 offset = 0;
4149 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4150 }
Chris Masond3977122009-01-05 21:25:51 -05004151
Chris Masond1310b22008-01-24 16:13:08 -05004152 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004153 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4154 "wanted %lu %lu\n", (unsigned long long)eb->start,
4155 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004156 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004157 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004158 }
4159
4160 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004161 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004162 *map = kaddr + offset;
4163 *map_len = PAGE_CACHE_SIZE - offset;
4164 return 0;
4165}
Chris Masond1310b22008-01-24 16:13:08 -05004166
Chris Masond1310b22008-01-24 16:13:08 -05004167int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4168 unsigned long start,
4169 unsigned long len)
4170{
4171 size_t cur;
4172 size_t offset;
4173 struct page *page;
4174 char *kaddr;
4175 char *ptr = (char *)ptrv;
4176 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4177 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4178 int ret = 0;
4179
4180 WARN_ON(start > eb->len);
4181 WARN_ON(start + len > eb->start + eb->len);
4182
4183 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4184
Chris Masond3977122009-01-05 21:25:51 -05004185 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004186 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004187
4188 cur = min(len, (PAGE_CACHE_SIZE - offset));
4189
Chris Masona6591712011-07-19 12:04:14 -04004190 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004191 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004192 if (ret)
4193 break;
4194
4195 ptr += cur;
4196 len -= cur;
4197 offset = 0;
4198 i++;
4199 }
4200 return ret;
4201}
Chris Masond1310b22008-01-24 16:13:08 -05004202
4203void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4204 unsigned long start, unsigned long len)
4205{
4206 size_t cur;
4207 size_t offset;
4208 struct page *page;
4209 char *kaddr;
4210 char *src = (char *)srcv;
4211 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4212 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4213
4214 WARN_ON(start > eb->len);
4215 WARN_ON(start + len > eb->start + eb->len);
4216
4217 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4218
Chris Masond3977122009-01-05 21:25:51 -05004219 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004220 page = extent_buffer_page(eb, i);
4221 WARN_ON(!PageUptodate(page));
4222
4223 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004224 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004225 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004226
4227 src += cur;
4228 len -= cur;
4229 offset = 0;
4230 i++;
4231 }
4232}
Chris Masond1310b22008-01-24 16:13:08 -05004233
4234void memset_extent_buffer(struct extent_buffer *eb, char c,
4235 unsigned long start, unsigned long len)
4236{
4237 size_t cur;
4238 size_t offset;
4239 struct page *page;
4240 char *kaddr;
4241 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4242 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4243
4244 WARN_ON(start > eb->len);
4245 WARN_ON(start + len > eb->start + eb->len);
4246
4247 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4248
Chris Masond3977122009-01-05 21:25:51 -05004249 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004250 page = extent_buffer_page(eb, i);
4251 WARN_ON(!PageUptodate(page));
4252
4253 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004254 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004255 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004256
4257 len -= cur;
4258 offset = 0;
4259 i++;
4260 }
4261}
Chris Masond1310b22008-01-24 16:13:08 -05004262
4263void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4264 unsigned long dst_offset, unsigned long src_offset,
4265 unsigned long len)
4266{
4267 u64 dst_len = dst->len;
4268 size_t cur;
4269 size_t offset;
4270 struct page *page;
4271 char *kaddr;
4272 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4273 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4274
4275 WARN_ON(src->len != dst_len);
4276
4277 offset = (start_offset + dst_offset) &
4278 ((unsigned long)PAGE_CACHE_SIZE - 1);
4279
Chris Masond3977122009-01-05 21:25:51 -05004280 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004281 page = extent_buffer_page(dst, i);
4282 WARN_ON(!PageUptodate(page));
4283
4284 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4285
Chris Masona6591712011-07-19 12:04:14 -04004286 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004287 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004288
4289 src_offset += cur;
4290 len -= cur;
4291 offset = 0;
4292 i++;
4293 }
4294}
Chris Masond1310b22008-01-24 16:13:08 -05004295
4296static void move_pages(struct page *dst_page, struct page *src_page,
4297 unsigned long dst_off, unsigned long src_off,
4298 unsigned long len)
4299{
Chris Masona6591712011-07-19 12:04:14 -04004300 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004301 if (dst_page == src_page) {
4302 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4303 } else {
Chris Masona6591712011-07-19 12:04:14 -04004304 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004305 char *p = dst_kaddr + dst_off + len;
4306 char *s = src_kaddr + src_off + len;
4307
4308 while (len--)
4309 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004310 }
Chris Masond1310b22008-01-24 16:13:08 -05004311}
4312
Sergei Trofimovich33872062011-04-11 21:52:52 +00004313static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4314{
4315 unsigned long distance = (src > dst) ? src - dst : dst - src;
4316 return distance < len;
4317}
4318
Chris Masond1310b22008-01-24 16:13:08 -05004319static void copy_pages(struct page *dst_page, struct page *src_page,
4320 unsigned long dst_off, unsigned long src_off,
4321 unsigned long len)
4322{
Chris Masona6591712011-07-19 12:04:14 -04004323 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004324 char *src_kaddr;
4325
Sergei Trofimovich33872062011-04-11 21:52:52 +00004326 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004327 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004328 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004329 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004330 BUG_ON(areas_overlap(src_off, dst_off, len));
4331 }
Chris Masond1310b22008-01-24 16:13:08 -05004332
4333 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004334}
4335
4336void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4337 unsigned long src_offset, unsigned long len)
4338{
4339 size_t cur;
4340 size_t dst_off_in_page;
4341 size_t src_off_in_page;
4342 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4343 unsigned long dst_i;
4344 unsigned long src_i;
4345
4346 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004347 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4348 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004349 BUG_ON(1);
4350 }
4351 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004352 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4353 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004354 BUG_ON(1);
4355 }
4356
Chris Masond3977122009-01-05 21:25:51 -05004357 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004358 dst_off_in_page = (start_offset + dst_offset) &
4359 ((unsigned long)PAGE_CACHE_SIZE - 1);
4360 src_off_in_page = (start_offset + src_offset) &
4361 ((unsigned long)PAGE_CACHE_SIZE - 1);
4362
4363 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4364 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4365
4366 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4367 src_off_in_page));
4368 cur = min_t(unsigned long, cur,
4369 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4370
4371 copy_pages(extent_buffer_page(dst, dst_i),
4372 extent_buffer_page(dst, src_i),
4373 dst_off_in_page, src_off_in_page, cur);
4374
4375 src_offset += cur;
4376 dst_offset += cur;
4377 len -= cur;
4378 }
4379}
Chris Masond1310b22008-01-24 16:13:08 -05004380
4381void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4382 unsigned long src_offset, unsigned long len)
4383{
4384 size_t cur;
4385 size_t dst_off_in_page;
4386 size_t src_off_in_page;
4387 unsigned long dst_end = dst_offset + len - 1;
4388 unsigned long src_end = src_offset + len - 1;
4389 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4390 unsigned long dst_i;
4391 unsigned long src_i;
4392
4393 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004394 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4395 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004396 BUG_ON(1);
4397 }
4398 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004399 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4400 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004401 BUG_ON(1);
4402 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004403 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004404 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4405 return;
4406 }
Chris Masond3977122009-01-05 21:25:51 -05004407 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004408 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4409 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4410
4411 dst_off_in_page = (start_offset + dst_end) &
4412 ((unsigned long)PAGE_CACHE_SIZE - 1);
4413 src_off_in_page = (start_offset + src_end) &
4414 ((unsigned long)PAGE_CACHE_SIZE - 1);
4415
4416 cur = min_t(unsigned long, len, src_off_in_page + 1);
4417 cur = min(cur, dst_off_in_page + 1);
4418 move_pages(extent_buffer_page(dst, dst_i),
4419 extent_buffer_page(dst, src_i),
4420 dst_off_in_page - cur + 1,
4421 src_off_in_page - cur + 1, cur);
4422
4423 dst_end -= cur;
4424 src_end -= cur;
4425 len -= cur;
4426 }
4427}
Chris Mason6af118ce2008-07-22 11:18:07 -04004428
Miao Xie19fe0a82010-10-26 20:57:29 -04004429static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4430{
4431 struct extent_buffer *eb =
4432 container_of(head, struct extent_buffer, rcu_head);
4433
4434 btrfs_release_extent_buffer(eb);
4435}
4436
Chris Mason6af118ce2008-07-22 11:18:07 -04004437int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4438{
4439 u64 start = page_offset(page);
4440 struct extent_buffer *eb;
4441 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004442
4443 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004444 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004445 if (!eb) {
4446 spin_unlock(&tree->buffer_lock);
4447 return ret;
4448 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004449
Chris Masonb9473432009-03-13 11:00:37 -04004450 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4451 ret = 0;
4452 goto out;
4453 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004454
Miao Xie19fe0a82010-10-26 20:57:29 -04004455 /*
4456 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4457 * Or go back.
4458 */
4459 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4460 ret = 0;
4461 goto out;
4462 }
4463
4464 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004465out:
4466 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004467
4468 /* at this point we can safely release the extent buffer */
4469 if (atomic_read(&eb->refs) == 0)
4470 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004471 return ret;
4472}