blob: ffa7cc3370c7cb289ee63a2197b4e0c1db52d647 [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);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100145 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500146 return state;
147}
Chris Masond1310b22008-01-24 16:13:08 -0500148
Chris Mason4845e442010-05-25 20:56:50 -0400149void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500150{
Chris Masond1310b22008-01-24 16:13:08 -0500151 if (!state)
152 return;
153 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500154#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400155 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400156#endif
Chris Mason70dec802008-01-29 09:59:12 -0500157 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500158#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400159 spin_lock_irqsave(&leak_lock, flags);
160 list_del(&state->leak_list);
161 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400162#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100163 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500164 kmem_cache_free(extent_state_cache, state);
165 }
166}
Chris Masond1310b22008-01-24 16:13:08 -0500167
168static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
169 struct rb_node *node)
170{
Chris Masond3977122009-01-05 21:25:51 -0500171 struct rb_node **p = &root->rb_node;
172 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500173 struct tree_entry *entry;
174
Chris Masond3977122009-01-05 21:25:51 -0500175 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500176 parent = *p;
177 entry = rb_entry(parent, struct tree_entry, rb_node);
178
179 if (offset < entry->start)
180 p = &(*p)->rb_left;
181 else if (offset > entry->end)
182 p = &(*p)->rb_right;
183 else
184 return parent;
185 }
186
187 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500188 rb_link_node(node, parent, p);
189 rb_insert_color(node, root);
190 return NULL;
191}
192
Chris Mason80ea96b2008-02-01 14:51:59 -0500193static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500194 struct rb_node **prev_ret,
195 struct rb_node **next_ret)
196{
Chris Mason80ea96b2008-02-01 14:51:59 -0500197 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500198 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500199 struct rb_node *prev = NULL;
200 struct rb_node *orig_prev = NULL;
201 struct tree_entry *entry;
202 struct tree_entry *prev_entry = NULL;
203
Chris Masond3977122009-01-05 21:25:51 -0500204 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500205 entry = rb_entry(n, struct tree_entry, rb_node);
206 prev = n;
207 prev_entry = entry;
208
209 if (offset < entry->start)
210 n = n->rb_left;
211 else if (offset > entry->end)
212 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500213 else
Chris Masond1310b22008-01-24 16:13:08 -0500214 return n;
215 }
216
217 if (prev_ret) {
218 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500219 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500220 prev = rb_next(prev);
221 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
222 }
223 *prev_ret = prev;
224 prev = orig_prev;
225 }
226
227 if (next_ret) {
228 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500229 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500230 prev = rb_prev(prev);
231 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
232 }
233 *next_ret = prev;
234 }
235 return NULL;
236}
237
Chris Mason80ea96b2008-02-01 14:51:59 -0500238static inline struct rb_node *tree_search(struct extent_io_tree *tree,
239 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500240{
Chris Mason70dec802008-01-29 09:59:12 -0500241 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500242 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500243
Chris Mason80ea96b2008-02-01 14:51:59 -0500244 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500245 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500246 return prev;
247 return ret;
248}
249
Josef Bacik9ed74f22009-09-11 16:12:44 -0400250static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
251 struct extent_state *other)
252{
253 if (tree->ops && tree->ops->merge_extent_hook)
254 tree->ops->merge_extent_hook(tree->mapping->host, new,
255 other);
256}
257
Chris Masond1310b22008-01-24 16:13:08 -0500258/*
259 * utility function to look for merge candidates inside a given range.
260 * Any extents with matching state are merged together into a single
261 * extent in the tree. Extents with EXTENT_IO in their state field
262 * are not merged because the end_io handlers need to be able to do
263 * operations on them without sleeping (or doing allocations/splits).
264 *
265 * This should be called with the tree lock held.
266 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000267static void merge_state(struct extent_io_tree *tree,
268 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500269{
270 struct extent_state *other;
271 struct rb_node *other_node;
272
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400273 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000274 return;
Chris Masond1310b22008-01-24 16:13:08 -0500275
276 other_node = rb_prev(&state->rb_node);
277 if (other_node) {
278 other = rb_entry(other_node, struct extent_state, rb_node);
279 if (other->end == state->start - 1 &&
280 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400281 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500282 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500283 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500284 rb_erase(&other->rb_node, &tree->state);
285 free_extent_state(other);
286 }
287 }
288 other_node = rb_next(&state->rb_node);
289 if (other_node) {
290 other = rb_entry(other_node, struct extent_state, rb_node);
291 if (other->start == state->end + 1 &&
292 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400293 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400294 state->end = other->end;
295 other->tree = NULL;
296 rb_erase(&other->rb_node, &tree->state);
297 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500298 }
299 }
Chris Masond1310b22008-01-24 16:13:08 -0500300}
301
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000302static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400303 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500304{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000305 if (tree->ops && tree->ops->set_bit_hook)
306 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500307}
308
309static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400310 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500311{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400312 if (tree->ops && tree->ops->clear_bit_hook)
313 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500314}
315
Xiao Guangrong3150b692011-07-14 03:19:08 +0000316static void set_state_bits(struct extent_io_tree *tree,
317 struct extent_state *state, int *bits);
318
Chris Masond1310b22008-01-24 16:13:08 -0500319/*
320 * insert an extent_state struct into the tree. 'bits' are set on the
321 * struct before it is inserted.
322 *
323 * This may return -EEXIST if the extent is already there, in which case the
324 * state struct is freed.
325 *
326 * The tree lock is not taken internally. This is a utility function and
327 * probably isn't what you want to call (see set/clear_extent_bit).
328 */
329static int insert_state(struct extent_io_tree *tree,
330 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400331 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500332{
333 struct rb_node *node;
334
335 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500336 printk(KERN_ERR "btrfs end < start %llu %llu\n",
337 (unsigned long long)end,
338 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500339 WARN_ON(1);
340 }
Chris Masond1310b22008-01-24 16:13:08 -0500341 state->start = start;
342 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400343
Xiao Guangrong3150b692011-07-14 03:19:08 +0000344 set_state_bits(tree, state, bits);
345
Chris Masond1310b22008-01-24 16:13:08 -0500346 node = tree_insert(&tree->state, end, &state->rb_node);
347 if (node) {
348 struct extent_state *found;
349 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500350 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
351 "%llu %llu\n", (unsigned long long)found->start,
352 (unsigned long long)found->end,
353 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500354 return -EEXIST;
355 }
Chris Mason70dec802008-01-29 09:59:12 -0500356 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500357 merge_state(tree, state);
358 return 0;
359}
360
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000361static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400362 u64 split)
363{
364 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000365 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400366}
367
Chris Masond1310b22008-01-24 16:13:08 -0500368/*
369 * split a given extent state struct in two, inserting the preallocated
370 * struct 'prealloc' as the newly created second half. 'split' indicates an
371 * offset inside 'orig' where it should be split.
372 *
373 * Before calling,
374 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
375 * are two extent state structs in the tree:
376 * prealloc: [orig->start, split - 1]
377 * orig: [ split, orig->end ]
378 *
379 * The tree locks are not taken by this function. They need to be held
380 * by the caller.
381 */
382static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
383 struct extent_state *prealloc, u64 split)
384{
385 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400386
387 split_cb(tree, orig, split);
388
Chris Masond1310b22008-01-24 16:13:08 -0500389 prealloc->start = orig->start;
390 prealloc->end = split - 1;
391 prealloc->state = orig->state;
392 orig->start = split;
393
394 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
395 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500396 free_extent_state(prealloc);
397 return -EEXIST;
398 }
Chris Mason70dec802008-01-29 09:59:12 -0500399 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500400 return 0;
401}
402
403/*
404 * utility function to clear some bits in an extent state struct.
405 * it will optionally wake up any one waiting on this state (wake == 1), or
406 * forcibly remove the state from the tree (delete == 1).
407 *
408 * If no bits are set on the state struct after clearing things, the
409 * struct is freed and removed from the tree
410 */
411static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400412 struct extent_state *state,
413 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500414{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400415 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400416 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500417
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500419 u64 range = state->end - state->start + 1;
420 WARN_ON(range > tree->dirty_bytes);
421 tree->dirty_bytes -= range;
422 }
Chris Mason291d6732008-01-29 15:55:23 -0500423 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400424 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500425 if (wake)
426 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400427 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500428 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500429 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500430 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500431 free_extent_state(state);
432 } else {
433 WARN_ON(1);
434 }
435 } else {
436 merge_state(tree, state);
437 }
438 return ret;
439}
440
Xiao Guangrong82337672011-04-20 06:44:57 +0000441static struct extent_state *
442alloc_extent_state_atomic(struct extent_state *prealloc)
443{
444 if (!prealloc)
445 prealloc = alloc_extent_state(GFP_ATOMIC);
446
447 return prealloc;
448}
449
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400450void extent_io_tree_panic(struct extent_io_tree *tree, int err)
451{
452 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
453 "Extent tree was modified by another "
454 "thread while locked.");
455}
456
Chris Masond1310b22008-01-24 16:13:08 -0500457/*
458 * clear some bits on a range in the tree. This may require splitting
459 * or inserting elements in the tree, so the gfp mask is used to
460 * indicate which allocations or sleeping are allowed.
461 *
462 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
463 * the given range from the tree regardless of state (ie for truncate).
464 *
465 * the range [start, end] is inclusive.
466 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100467 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500468 */
469int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400470 int bits, int wake, int delete,
471 struct extent_state **cached_state,
472 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500473{
474 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400475 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500476 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400477 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500478 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400479 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500480 int err;
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) {
Jeff Mahoney6763af82012-03-01 14:56:29 +0100565 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
Jeff Mahoney6763af82012-03-01 14:56:29 +0100588 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
Jeff Mahoney6763af82012-03-01 14:56:29 +0100594 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
Jeff Mahoney6763af82012-03-01 14:56:29 +0100611 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500612
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
Jeff Mahoney143bede2012-03-01 14:56:26 +0100622static void 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);
Chris Masond1310b22008-01-24 16:13:08 -0500633}
634
635/*
636 * waits for one or more bits to clear on a range in the state tree.
637 * The range [start, end] is inclusive.
638 * The tree lock is taken by this function
639 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100640void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500641{
642 struct extent_state *state;
643 struct rb_node *node;
644
Chris Masoncad321a2008-12-17 14:51:42 -0500645 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500646again:
647 while (1) {
648 /*
649 * this search will find all the extents that end after
650 * our range starts
651 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500652 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500653 if (!node)
654 break;
655
656 state = rb_entry(node, struct extent_state, rb_node);
657
658 if (state->start > end)
659 goto out;
660
661 if (state->state & bits) {
662 start = state->start;
663 atomic_inc(&state->refs);
664 wait_on_state(tree, state);
665 free_extent_state(state);
666 goto again;
667 }
668 start = state->end + 1;
669
670 if (start > end)
671 break;
672
Xiao Guangrongded91f02011-07-14 03:19:27 +0000673 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500674 }
675out:
Chris Masoncad321a2008-12-17 14:51:42 -0500676 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500677}
Chris Masond1310b22008-01-24 16:13:08 -0500678
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000679static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500680 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400681 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500682{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400683 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400684
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000685 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400686 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500687 u64 range = state->end - state->start + 1;
688 tree->dirty_bytes += range;
689 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500691}
692
Chris Mason2c64c532009-09-02 15:04:12 -0400693static void cache_state(struct extent_state *state,
694 struct extent_state **cached_ptr)
695{
696 if (cached_ptr && !(*cached_ptr)) {
697 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
698 *cached_ptr = state;
699 atomic_inc(&state->refs);
700 }
701 }
702}
703
Arne Jansen507903b2011-04-06 10:02:20 +0000704static void uncache_state(struct extent_state **cached_ptr)
705{
706 if (cached_ptr && (*cached_ptr)) {
707 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400708 *cached_ptr = NULL;
709 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000710 }
711}
712
Chris Masond1310b22008-01-24 16:13:08 -0500713/*
Chris Mason1edbb732009-09-02 13:24:36 -0400714 * set some bits on a range in the tree. This may require allocations or
715 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500716 *
Chris Mason1edbb732009-09-02 13:24:36 -0400717 * If any of the exclusive bits are set, this will fail with -EEXIST if some
718 * part of the range already has the desired bits set. The start of the
719 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500720 *
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500722 */
Chris Mason1edbb732009-09-02 13:24:36 -0400723
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100724static int __must_check
725__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
726 int bits, int exclusive_bits, u64 *failed_start,
727 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500728{
729 struct extent_state *state;
730 struct extent_state *prealloc = NULL;
731 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500732 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500733 u64 last_start;
734 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400735
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400736 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500737again:
738 if (!prealloc && (mask & __GFP_WAIT)) {
739 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000740 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500741 }
742
Chris Masoncad321a2008-12-17 14:51:42 -0500743 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400744 if (cached_state && *cached_state) {
745 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400746 if (state->start <= start && state->end > start &&
747 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400748 node = &state->rb_node;
749 goto hit_next;
750 }
751 }
Chris Masond1310b22008-01-24 16:13:08 -0500752 /*
753 * this search will find all the extents that end after
754 * our range starts.
755 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500756 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500757 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000758 prealloc = alloc_extent_state_atomic(prealloc);
759 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400760 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400761 if (err)
762 extent_io_tree_panic(tree, err);
763
Chris Masond1310b22008-01-24 16:13:08 -0500764 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500765 goto out;
766 }
Chris Masond1310b22008-01-24 16:13:08 -0500767 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400768hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500769 last_start = state->start;
770 last_end = state->end;
771
772 /*
773 * | ---- desired range ---- |
774 * | state |
775 *
776 * Just lock what we found and keep going
777 */
778 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400779 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400780 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500781 *failed_start = state->start;
782 err = -EEXIST;
783 goto out;
784 }
Chris Mason42daec22009-09-23 19:51:09 -0400785
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000786 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400787
Chris Mason2c64c532009-09-02 15:04:12 -0400788 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500789 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400790 if (last_end == (u64)-1)
791 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400792
Yan Zheng5c939df2009-05-27 09:16:03 -0400793 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400794 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000795 if (next_node && start < end && prealloc && !need_resched()) {
796 state = rb_entry(next_node, struct extent_state,
797 rb_node);
798 if (state->start == start)
799 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400800 }
Chris Masond1310b22008-01-24 16:13:08 -0500801 goto search_again;
802 }
803
804 /*
805 * | ---- desired range ---- |
806 * | state |
807 * or
808 * | ------------- state -------------- |
809 *
810 * We need to split the extent we found, and may flip bits on
811 * second half.
812 *
813 * If the extent we found extends past our
814 * range, we just split and search again. It'll get split
815 * again the next time though.
816 *
817 * If the extent we found is inside our range, we set the
818 * desired bit on it.
819 */
820 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400821 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500822 *failed_start = start;
823 err = -EEXIST;
824 goto out;
825 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000826
827 prealloc = alloc_extent_state_atomic(prealloc);
828 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500829 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400830 if (err)
831 extent_io_tree_panic(tree, err);
832
Chris Masond1310b22008-01-24 16:13:08 -0500833 prealloc = NULL;
834 if (err)
835 goto out;
836 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000837 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400838 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500839 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400840 if (last_end == (u64)-1)
841 goto out;
842 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500843 }
844 goto search_again;
845 }
846 /*
847 * | ---- desired range ---- |
848 * | state | or | state |
849 *
850 * There's a hole, we need to insert something in it and
851 * ignore the extent we found.
852 */
853 if (state->start > start) {
854 u64 this_end;
855 if (end < last_start)
856 this_end = end;
857 else
Chris Masond3977122009-01-05 21:25:51 -0500858 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000859
860 prealloc = alloc_extent_state_atomic(prealloc);
861 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000862
863 /*
864 * Avoid to free 'prealloc' if it can be merged with
865 * the later extent.
866 */
Chris Masond1310b22008-01-24 16:13:08 -0500867 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400868 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400869 if (err)
870 extent_io_tree_panic(tree, err);
871
Chris Mason2c64c532009-09-02 15:04:12 -0400872 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500873 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500874 start = this_end + 1;
875 goto search_again;
876 }
877 /*
878 * | ---- desired range ---- |
879 * | state |
880 * We need to split the extent, and set the bit
881 * on the first half
882 */
883 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400884 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500885 *failed_start = start;
886 err = -EEXIST;
887 goto out;
888 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000889
890 prealloc = alloc_extent_state_atomic(prealloc);
891 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500892 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400893 if (err)
894 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500895
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000896 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400897 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500898 merge_state(tree, prealloc);
899 prealloc = NULL;
900 goto out;
901 }
902
903 goto search_again;
904
905out:
Chris Masoncad321a2008-12-17 14:51:42 -0500906 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500907 if (prealloc)
908 free_extent_state(prealloc);
909
910 return err;
911
912search_again:
913 if (start > end)
914 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500915 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500916 if (mask & __GFP_WAIT)
917 cond_resched();
918 goto again;
919}
Chris Masond1310b22008-01-24 16:13:08 -0500920
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100921int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
922 u64 *failed_start, struct extent_state **cached_state,
923 gfp_t mask)
924{
925 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
926 cached_state, mask);
927}
928
929
Josef Bacik462d6fa2011-09-26 13:56:12 -0400930/**
931 * convert_extent - convert all bits in a given range from one bit to another
932 * @tree: the io tree to search
933 * @start: the start offset in bytes
934 * @end: the end offset in bytes (inclusive)
935 * @bits: the bits to set in this range
936 * @clear_bits: the bits to clear in this range
937 * @mask: the allocation mask
938 *
939 * This will go through and set bits for the given range. If any states exist
940 * already in this range they are set with the given bit and cleared of the
941 * clear_bits. This is only meant to be used by things that are mergeable, ie
942 * converting from say DELALLOC to DIRTY. This is not meant to be used with
943 * boundary bits like LOCK.
944 */
945int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
946 int bits, int clear_bits, gfp_t mask)
947{
948 struct extent_state *state;
949 struct extent_state *prealloc = NULL;
950 struct rb_node *node;
951 int err = 0;
952 u64 last_start;
953 u64 last_end;
954
955again:
956 if (!prealloc && (mask & __GFP_WAIT)) {
957 prealloc = alloc_extent_state(mask);
958 if (!prealloc)
959 return -ENOMEM;
960 }
961
962 spin_lock(&tree->lock);
963 /*
964 * this search will find all the extents that end after
965 * our range starts.
966 */
967 node = tree_search(tree, start);
968 if (!node) {
969 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500970 if (!prealloc) {
971 err = -ENOMEM;
972 goto out;
973 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400974 err = insert_state(tree, prealloc, start, end, &bits);
975 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400976 if (err)
977 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400978 goto out;
979 }
980 state = rb_entry(node, struct extent_state, rb_node);
981hit_next:
982 last_start = state->start;
983 last_end = state->end;
984
985 /*
986 * | ---- desired range ---- |
987 * | state |
988 *
989 * Just lock what we found and keep going
990 */
991 if (state->start == start && state->end <= end) {
992 struct rb_node *next_node;
993
994 set_state_bits(tree, state, &bits);
995 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400996 if (last_end == (u64)-1)
997 goto out;
998
999 start = last_end + 1;
1000 next_node = rb_next(&state->rb_node);
1001 if (next_node && start < end && prealloc && !need_resched()) {
1002 state = rb_entry(next_node, struct extent_state,
1003 rb_node);
1004 if (state->start == start)
1005 goto hit_next;
1006 }
1007 goto search_again;
1008 }
1009
1010 /*
1011 * | ---- desired range ---- |
1012 * | state |
1013 * or
1014 * | ------------- state -------------- |
1015 *
1016 * We need to split the extent we found, and may flip bits on
1017 * second half.
1018 *
1019 * If the extent we found extends past our
1020 * range, we just split and search again. It'll get split
1021 * again the next time though.
1022 *
1023 * If the extent we found is inside our range, we set the
1024 * desired bit on it.
1025 */
1026 if (state->start < start) {
1027 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001028 if (!prealloc) {
1029 err = -ENOMEM;
1030 goto out;
1031 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001032 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001033 if (err)
1034 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001035 prealloc = NULL;
1036 if (err)
1037 goto out;
1038 if (state->end <= end) {
1039 set_state_bits(tree, state, &bits);
1040 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001041 if (last_end == (u64)-1)
1042 goto out;
1043 start = last_end + 1;
1044 }
1045 goto search_again;
1046 }
1047 /*
1048 * | ---- desired range ---- |
1049 * | state | or | state |
1050 *
1051 * There's a hole, we need to insert something in it and
1052 * ignore the extent we found.
1053 */
1054 if (state->start > start) {
1055 u64 this_end;
1056 if (end < last_start)
1057 this_end = end;
1058 else
1059 this_end = last_start - 1;
1060
1061 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001062 if (!prealloc) {
1063 err = -ENOMEM;
1064 goto out;
1065 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001066
1067 /*
1068 * Avoid to free 'prealloc' if it can be merged with
1069 * the later extent.
1070 */
1071 err = insert_state(tree, prealloc, start, this_end,
1072 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001073 if (err)
1074 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001075 prealloc = NULL;
1076 start = this_end + 1;
1077 goto search_again;
1078 }
1079 /*
1080 * | ---- desired range ---- |
1081 * | state |
1082 * We need to split the extent, and set the bit
1083 * on the first half
1084 */
1085 if (state->start <= end && state->end > end) {
1086 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001087 if (!prealloc) {
1088 err = -ENOMEM;
1089 goto out;
1090 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001091
1092 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001093 if (err)
1094 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001095
1096 set_state_bits(tree, prealloc, &bits);
1097 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001098 prealloc = NULL;
1099 goto out;
1100 }
1101
1102 goto search_again;
1103
1104out:
1105 spin_unlock(&tree->lock);
1106 if (prealloc)
1107 free_extent_state(prealloc);
1108
1109 return err;
1110
1111search_again:
1112 if (start > end)
1113 goto out;
1114 spin_unlock(&tree->lock);
1115 if (mask & __GFP_WAIT)
1116 cond_resched();
1117 goto again;
1118}
1119
Chris Masond1310b22008-01-24 16:13:08 -05001120/* wrappers around set/clear extent bit */
1121int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1122 gfp_t mask)
1123{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001124 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001125 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001126}
Chris Masond1310b22008-01-24 16:13:08 -05001127
1128int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1129 int bits, gfp_t mask)
1130{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001131 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001132 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001133}
Chris Masond1310b22008-01-24 16:13:08 -05001134
1135int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1136 int bits, gfp_t mask)
1137{
Chris Mason2c64c532009-09-02 15:04:12 -04001138 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001139}
Chris Masond1310b22008-01-24 16:13:08 -05001140
1141int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001142 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001143{
1144 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001145 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001146 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001147}
Chris Masond1310b22008-01-24 16:13:08 -05001148
1149int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1150 gfp_t mask)
1151{
1152 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001153 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001154 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001155}
Chris Masond1310b22008-01-24 16:13:08 -05001156
1157int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1158 gfp_t mask)
1159{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001160 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001161 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001162}
Chris Masond1310b22008-01-24 16:13:08 -05001163
Chris Masond1310b22008-01-24 16:13:08 -05001164int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001165 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001166{
Arne Jansen507903b2011-04-06 10:02:20 +00001167 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001168 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001169}
Chris Masond1310b22008-01-24 16:13:08 -05001170
Chris Masond3977122009-01-05 21:25:51 -05001171static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001172 u64 end, struct extent_state **cached_state,
1173 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001174{
Chris Mason2c64c532009-09-02 15:04:12 -04001175 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001176 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001177}
Chris Masond1310b22008-01-24 16:13:08 -05001178
Chris Masond352ac62008-09-29 15:18:18 -04001179/*
1180 * either insert or lock state struct between start and end use mask to tell
1181 * us if waiting is desired.
1182 */
Chris Mason1edbb732009-09-02 13:24:36 -04001183int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001184 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001185{
1186 int err;
1187 u64 failed_start;
1188 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001189 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1190 EXTENT_LOCKED, &failed_start,
1191 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001192 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001193 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1194 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001195 } else
Chris Masond1310b22008-01-24 16:13:08 -05001196 break;
Chris Masond1310b22008-01-24 16:13:08 -05001197 WARN_ON(start > end);
1198 }
1199 return err;
1200}
Chris Masond1310b22008-01-24 16:13:08 -05001201
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001202int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001203{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001204 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001205}
1206
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001207int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001208{
1209 int err;
1210 u64 failed_start;
1211
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001212 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1213 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001214 if (err == -EEXIST) {
1215 if (failed_start > start)
1216 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001217 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001218 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001219 }
Josef Bacik25179202008-10-29 14:49:05 -04001220 return 1;
1221}
Josef Bacik25179202008-10-29 14:49:05 -04001222
Chris Mason2c64c532009-09-02 15:04:12 -04001223int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1224 struct extent_state **cached, gfp_t mask)
1225{
1226 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1227 mask);
1228}
1229
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001230int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001231{
Chris Mason2c64c532009-09-02 15:04:12 -04001232 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001233 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001234}
Chris Masond1310b22008-01-24 16:13:08 -05001235
1236/*
Chris Masond1310b22008-01-24 16:13:08 -05001237 * helper function to set both pages and extents in the tree writeback
1238 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001239static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001240{
1241 unsigned long index = start >> PAGE_CACHE_SHIFT;
1242 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1243 struct page *page;
1244
1245 while (index <= end_index) {
1246 page = find_get_page(tree->mapping, index);
1247 BUG_ON(!page);
1248 set_page_writeback(page);
1249 page_cache_release(page);
1250 index++;
1251 }
Chris Masond1310b22008-01-24 16:13:08 -05001252 return 0;
1253}
Chris Masond1310b22008-01-24 16:13:08 -05001254
Chris Masond352ac62008-09-29 15:18:18 -04001255/* find the first state struct with 'bits' set after 'start', and
1256 * return it. tree->lock must be held. NULL will returned if
1257 * nothing was found after 'start'
1258 */
Chris Masond7fc6402008-02-18 12:12:38 -05001259struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1260 u64 start, int bits)
1261{
1262 struct rb_node *node;
1263 struct extent_state *state;
1264
1265 /*
1266 * this search will find all the extents that end after
1267 * our range starts.
1268 */
1269 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001270 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001271 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001272
Chris Masond3977122009-01-05 21:25:51 -05001273 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001274 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001275 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001276 return state;
Chris Masond3977122009-01-05 21:25:51 -05001277
Chris Masond7fc6402008-02-18 12:12:38 -05001278 node = rb_next(node);
1279 if (!node)
1280 break;
1281 }
1282out:
1283 return NULL;
1284}
Chris Masond7fc6402008-02-18 12:12:38 -05001285
Chris Masond352ac62008-09-29 15:18:18 -04001286/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001287 * find the first offset in the io tree with 'bits' set. zero is
1288 * returned if we find something, and *start_ret and *end_ret are
1289 * set to reflect the state struct that was found.
1290 *
1291 * If nothing was found, 1 is returned, < 0 on error
1292 */
1293int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1294 u64 *start_ret, u64 *end_ret, int bits)
1295{
1296 struct extent_state *state;
1297 int ret = 1;
1298
1299 spin_lock(&tree->lock);
1300 state = find_first_extent_bit_state(tree, start, bits);
1301 if (state) {
1302 *start_ret = state->start;
1303 *end_ret = state->end;
1304 ret = 0;
1305 }
1306 spin_unlock(&tree->lock);
1307 return ret;
1308}
1309
1310/*
Chris Masond352ac62008-09-29 15:18:18 -04001311 * find a contiguous range of bytes in the file marked as delalloc, not
1312 * more than 'max_bytes'. start and end are used to return the range,
1313 *
1314 * 1 is returned if we find something, 0 if nothing was in the tree
1315 */
Chris Masonc8b97812008-10-29 14:49:59 -04001316static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001317 u64 *start, u64 *end, u64 max_bytes,
1318 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001319{
1320 struct rb_node *node;
1321 struct extent_state *state;
1322 u64 cur_start = *start;
1323 u64 found = 0;
1324 u64 total_bytes = 0;
1325
Chris Masoncad321a2008-12-17 14:51:42 -05001326 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001327
Chris Masond1310b22008-01-24 16:13:08 -05001328 /*
1329 * this search will find all the extents that end after
1330 * our range starts.
1331 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001332 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001333 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001334 if (!found)
1335 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001336 goto out;
1337 }
1338
Chris Masond3977122009-01-05 21:25:51 -05001339 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001340 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001341 if (found && (state->start != cur_start ||
1342 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001343 goto out;
1344 }
1345 if (!(state->state & EXTENT_DELALLOC)) {
1346 if (!found)
1347 *end = state->end;
1348 goto out;
1349 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001350 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001351 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001352 *cached_state = state;
1353 atomic_inc(&state->refs);
1354 }
Chris Masond1310b22008-01-24 16:13:08 -05001355 found++;
1356 *end = state->end;
1357 cur_start = state->end + 1;
1358 node = rb_next(node);
1359 if (!node)
1360 break;
1361 total_bytes += state->end - state->start + 1;
1362 if (total_bytes >= max_bytes)
1363 break;
1364 }
1365out:
Chris Masoncad321a2008-12-17 14:51:42 -05001366 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001367 return found;
1368}
1369
Jeff Mahoney143bede2012-03-01 14:56:26 +01001370static noinline void __unlock_for_delalloc(struct inode *inode,
1371 struct page *locked_page,
1372 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001373{
1374 int ret;
1375 struct page *pages[16];
1376 unsigned long index = start >> PAGE_CACHE_SHIFT;
1377 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1378 unsigned long nr_pages = end_index - index + 1;
1379 int i;
1380
1381 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001382 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001383
Chris Masond3977122009-01-05 21:25:51 -05001384 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001385 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001386 min_t(unsigned long, nr_pages,
1387 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001388 for (i = 0; i < ret; i++) {
1389 if (pages[i] != locked_page)
1390 unlock_page(pages[i]);
1391 page_cache_release(pages[i]);
1392 }
1393 nr_pages -= ret;
1394 index += ret;
1395 cond_resched();
1396 }
Chris Masonc8b97812008-10-29 14:49:59 -04001397}
1398
1399static noinline int lock_delalloc_pages(struct inode *inode,
1400 struct page *locked_page,
1401 u64 delalloc_start,
1402 u64 delalloc_end)
1403{
1404 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1405 unsigned long start_index = index;
1406 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1407 unsigned long pages_locked = 0;
1408 struct page *pages[16];
1409 unsigned long nrpages;
1410 int ret;
1411 int i;
1412
1413 /* the caller is responsible for locking the start index */
1414 if (index == locked_page->index && index == end_index)
1415 return 0;
1416
1417 /* skip the page at the start index */
1418 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001419 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001420 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001421 min_t(unsigned long,
1422 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001423 if (ret == 0) {
1424 ret = -EAGAIN;
1425 goto done;
1426 }
1427 /* now we have an array of pages, lock them all */
1428 for (i = 0; i < ret; i++) {
1429 /*
1430 * the caller is taking responsibility for
1431 * locked_page
1432 */
Chris Mason771ed682008-11-06 22:02:51 -05001433 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001434 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001435 if (!PageDirty(pages[i]) ||
1436 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001437 ret = -EAGAIN;
1438 unlock_page(pages[i]);
1439 page_cache_release(pages[i]);
1440 goto done;
1441 }
1442 }
Chris Masonc8b97812008-10-29 14:49:59 -04001443 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001444 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001445 }
Chris Masonc8b97812008-10-29 14:49:59 -04001446 nrpages -= ret;
1447 index += ret;
1448 cond_resched();
1449 }
1450 ret = 0;
1451done:
1452 if (ret && pages_locked) {
1453 __unlock_for_delalloc(inode, locked_page,
1454 delalloc_start,
1455 ((u64)(start_index + pages_locked - 1)) <<
1456 PAGE_CACHE_SHIFT);
1457 }
1458 return ret;
1459}
1460
1461/*
1462 * find a contiguous range of bytes in the file marked as delalloc, not
1463 * more than 'max_bytes'. start and end are used to return the range,
1464 *
1465 * 1 is returned if we find something, 0 if nothing was in the tree
1466 */
1467static noinline u64 find_lock_delalloc_range(struct inode *inode,
1468 struct extent_io_tree *tree,
1469 struct page *locked_page,
1470 u64 *start, u64 *end,
1471 u64 max_bytes)
1472{
1473 u64 delalloc_start;
1474 u64 delalloc_end;
1475 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001476 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001477 int ret;
1478 int loops = 0;
1479
1480again:
1481 /* step one, find a bunch of delalloc bytes starting at start */
1482 delalloc_start = *start;
1483 delalloc_end = 0;
1484 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001485 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001486 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001487 *start = delalloc_start;
1488 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001489 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001490 return found;
1491 }
1492
1493 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001494 * start comes from the offset of locked_page. We have to lock
1495 * pages in order, so we can't process delalloc bytes before
1496 * locked_page
1497 */
Chris Masond3977122009-01-05 21:25:51 -05001498 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001499 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001500
1501 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001502 * make sure to limit the number of pages we try to lock down
1503 * if we're looping.
1504 */
Chris Masond3977122009-01-05 21:25:51 -05001505 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001506 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001507
Chris Masonc8b97812008-10-29 14:49:59 -04001508 /* step two, lock all the pages after the page that has start */
1509 ret = lock_delalloc_pages(inode, locked_page,
1510 delalloc_start, delalloc_end);
1511 if (ret == -EAGAIN) {
1512 /* some of the pages are gone, lets avoid looping by
1513 * shortening the size of the delalloc range we're searching
1514 */
Chris Mason9655d292009-09-02 15:22:30 -04001515 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001516 if (!loops) {
1517 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1518 max_bytes = PAGE_CACHE_SIZE - offset;
1519 loops = 1;
1520 goto again;
1521 } else {
1522 found = 0;
1523 goto out_failed;
1524 }
1525 }
1526 BUG_ON(ret);
1527
1528 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001529 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001530
1531 /* then test to make sure it is all still delalloc */
1532 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001533 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001534 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001535 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1536 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001537 __unlock_for_delalloc(inode, locked_page,
1538 delalloc_start, delalloc_end);
1539 cond_resched();
1540 goto again;
1541 }
Chris Mason9655d292009-09-02 15:22:30 -04001542 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001543 *start = delalloc_start;
1544 *end = delalloc_end;
1545out_failed:
1546 return found;
1547}
1548
1549int extent_clear_unlock_delalloc(struct inode *inode,
1550 struct extent_io_tree *tree,
1551 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001552 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001553{
1554 int ret;
1555 struct page *pages[16];
1556 unsigned long index = start >> PAGE_CACHE_SHIFT;
1557 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1558 unsigned long nr_pages = end_index - index + 1;
1559 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001560 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001561
Chris Masona791e352009-10-08 11:27:10 -04001562 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001563 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001564 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001565 clear_bits |= EXTENT_DIRTY;
1566
Chris Masona791e352009-10-08 11:27:10 -04001567 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001568 clear_bits |= EXTENT_DELALLOC;
1569
Chris Mason2c64c532009-09-02 15:04:12 -04001570 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001571 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1572 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1573 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001574 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001575
Chris Masond3977122009-01-05 21:25:51 -05001576 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001577 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001578 min_t(unsigned long,
1579 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001580 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001581
Chris Masona791e352009-10-08 11:27:10 -04001582 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001583 SetPagePrivate2(pages[i]);
1584
Chris Masonc8b97812008-10-29 14:49:59 -04001585 if (pages[i] == locked_page) {
1586 page_cache_release(pages[i]);
1587 continue;
1588 }
Chris Masona791e352009-10-08 11:27:10 -04001589 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001590 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001591 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001592 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001593 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001594 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001595 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001596 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001597 page_cache_release(pages[i]);
1598 }
1599 nr_pages -= ret;
1600 index += ret;
1601 cond_resched();
1602 }
1603 return 0;
1604}
Chris Masonc8b97812008-10-29 14:49:59 -04001605
Chris Masond352ac62008-09-29 15:18:18 -04001606/*
1607 * count the number of bytes in the tree that have a given bit(s)
1608 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1609 * cached. The total number found is returned.
1610 */
Chris Masond1310b22008-01-24 16:13:08 -05001611u64 count_range_bits(struct extent_io_tree *tree,
1612 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001613 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001614{
1615 struct rb_node *node;
1616 struct extent_state *state;
1617 u64 cur_start = *start;
1618 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001619 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001620 int found = 0;
1621
1622 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001623 WARN_ON(1);
1624 return 0;
1625 }
1626
Chris Masoncad321a2008-12-17 14:51:42 -05001627 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001628 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1629 total_bytes = tree->dirty_bytes;
1630 goto out;
1631 }
1632 /*
1633 * this search will find all the extents that end after
1634 * our range starts.
1635 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001636 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001637 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001638 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001639
Chris Masond3977122009-01-05 21:25:51 -05001640 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001641 state = rb_entry(node, struct extent_state, rb_node);
1642 if (state->start > search_end)
1643 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001644 if (contig && found && state->start > last + 1)
1645 break;
1646 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001647 total_bytes += min(search_end, state->end) + 1 -
1648 max(cur_start, state->start);
1649 if (total_bytes >= max_bytes)
1650 break;
1651 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001652 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001653 found = 1;
1654 }
Chris Masonec29ed52011-02-23 16:23:20 -05001655 last = state->end;
1656 } else if (contig && found) {
1657 break;
Chris Masond1310b22008-01-24 16:13:08 -05001658 }
1659 node = rb_next(node);
1660 if (!node)
1661 break;
1662 }
1663out:
Chris Masoncad321a2008-12-17 14:51:42 -05001664 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001665 return total_bytes;
1666}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001667
Chris Masond352ac62008-09-29 15:18:18 -04001668/*
1669 * set the private field for a given byte offset in the tree. If there isn't
1670 * an extent_state there already, this does nothing.
1671 */
Chris Masond1310b22008-01-24 16:13:08 -05001672int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1673{
1674 struct rb_node *node;
1675 struct extent_state *state;
1676 int ret = 0;
1677
Chris Masoncad321a2008-12-17 14:51:42 -05001678 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001679 /*
1680 * this search will find all the extents that end after
1681 * our range starts.
1682 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001683 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001684 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001685 ret = -ENOENT;
1686 goto out;
1687 }
1688 state = rb_entry(node, struct extent_state, rb_node);
1689 if (state->start != start) {
1690 ret = -ENOENT;
1691 goto out;
1692 }
1693 state->private = private;
1694out:
Chris Masoncad321a2008-12-17 14:51:42 -05001695 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001696 return ret;
1697}
1698
1699int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1700{
1701 struct rb_node *node;
1702 struct extent_state *state;
1703 int ret = 0;
1704
Chris Masoncad321a2008-12-17 14:51:42 -05001705 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001706 /*
1707 * this search will find all the extents that end after
1708 * our range starts.
1709 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001710 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001711 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001712 ret = -ENOENT;
1713 goto out;
1714 }
1715 state = rb_entry(node, struct extent_state, rb_node);
1716 if (state->start != start) {
1717 ret = -ENOENT;
1718 goto out;
1719 }
1720 *private = state->private;
1721out:
Chris Masoncad321a2008-12-17 14:51:42 -05001722 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001723 return ret;
1724}
1725
1726/*
1727 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001728 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001729 * has the bits set. Otherwise, 1 is returned if any bit in the
1730 * range is found set.
1731 */
1732int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001733 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001734{
1735 struct extent_state *state = NULL;
1736 struct rb_node *node;
1737 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001738
Chris Masoncad321a2008-12-17 14:51:42 -05001739 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001740 if (cached && cached->tree && cached->start <= start &&
1741 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001742 node = &cached->rb_node;
1743 else
1744 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001745 while (node && start <= end) {
1746 state = rb_entry(node, struct extent_state, rb_node);
1747
1748 if (filled && state->start > start) {
1749 bitset = 0;
1750 break;
1751 }
1752
1753 if (state->start > end)
1754 break;
1755
1756 if (state->state & bits) {
1757 bitset = 1;
1758 if (!filled)
1759 break;
1760 } else if (filled) {
1761 bitset = 0;
1762 break;
1763 }
Chris Mason46562ce2009-09-23 20:23:16 -04001764
1765 if (state->end == (u64)-1)
1766 break;
1767
Chris Masond1310b22008-01-24 16:13:08 -05001768 start = state->end + 1;
1769 if (start > end)
1770 break;
1771 node = rb_next(node);
1772 if (!node) {
1773 if (filled)
1774 bitset = 0;
1775 break;
1776 }
1777 }
Chris Masoncad321a2008-12-17 14:51:42 -05001778 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001779 return bitset;
1780}
Chris Masond1310b22008-01-24 16:13:08 -05001781
1782/*
1783 * helper function to set a given page up to date if all the
1784 * extents in the tree for that page are up to date
1785 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001786static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001787{
1788 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1789 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001790 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001791 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001792}
1793
1794/*
1795 * helper function to unlock a page if all the extents in the tree
1796 * for that page are unlocked
1797 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001798static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001799{
1800 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1801 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001802 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001803 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001804}
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 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001810static void check_page_writeback(struct extent_io_tree *tree,
1811 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001812{
Chris Mason1edbb732009-09-02 13:24:36 -04001813 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001814}
1815
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001816/*
1817 * When IO fails, either with EIO or csum verification fails, we
1818 * try other mirrors that might have a good copy of the data. This
1819 * io_failure_record is used to record state as we go through all the
1820 * mirrors. If another mirror has good data, the page is set up to date
1821 * and things continue. If a good mirror can't be found, the original
1822 * bio end_io callback is called to indicate things have failed.
1823 */
1824struct io_failure_record {
1825 struct page *page;
1826 u64 start;
1827 u64 len;
1828 u64 logical;
1829 unsigned long bio_flags;
1830 int this_mirror;
1831 int failed_mirror;
1832 int in_validation;
1833};
1834
1835static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1836 int did_repair)
1837{
1838 int ret;
1839 int err = 0;
1840 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1841
1842 set_state_private(failure_tree, rec->start, 0);
1843 ret = clear_extent_bits(failure_tree, rec->start,
1844 rec->start + rec->len - 1,
1845 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1846 if (ret)
1847 err = ret;
1848
1849 if (did_repair) {
1850 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1851 rec->start + rec->len - 1,
1852 EXTENT_DAMAGED, GFP_NOFS);
1853 if (ret && !err)
1854 err = ret;
1855 }
1856
1857 kfree(rec);
1858 return err;
1859}
1860
1861static void repair_io_failure_callback(struct bio *bio, int err)
1862{
1863 complete(bio->bi_private);
1864}
1865
1866/*
1867 * this bypasses the standard btrfs submit functions deliberately, as
1868 * the standard behavior is to write all copies in a raid setup. here we only
1869 * want to write the one bad copy. so we do the mapping for ourselves and issue
1870 * submit_bio directly.
1871 * to avoid any synchonization issues, wait for the data after writing, which
1872 * actually prevents the read that triggered the error from finishing.
1873 * currently, there can be no more than two copies of every data bit. thus,
1874 * exactly one rewrite is required.
1875 */
1876int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1877 u64 length, u64 logical, struct page *page,
1878 int mirror_num)
1879{
1880 struct bio *bio;
1881 struct btrfs_device *dev;
1882 DECLARE_COMPLETION_ONSTACK(compl);
1883 u64 map_length = 0;
1884 u64 sector;
1885 struct btrfs_bio *bbio = NULL;
1886 int ret;
1887
1888 BUG_ON(!mirror_num);
1889
1890 bio = bio_alloc(GFP_NOFS, 1);
1891 if (!bio)
1892 return -EIO;
1893 bio->bi_private = &compl;
1894 bio->bi_end_io = repair_io_failure_callback;
1895 bio->bi_size = 0;
1896 map_length = length;
1897
1898 ret = btrfs_map_block(map_tree, WRITE, logical,
1899 &map_length, &bbio, mirror_num);
1900 if (ret) {
1901 bio_put(bio);
1902 return -EIO;
1903 }
1904 BUG_ON(mirror_num != bbio->mirror_num);
1905 sector = bbio->stripes[mirror_num-1].physical >> 9;
1906 bio->bi_sector = sector;
1907 dev = bbio->stripes[mirror_num-1].dev;
1908 kfree(bbio);
1909 if (!dev || !dev->bdev || !dev->writeable) {
1910 bio_put(bio);
1911 return -EIO;
1912 }
1913 bio->bi_bdev = dev->bdev;
1914 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001915 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001916 wait_for_completion(&compl);
1917
1918 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1919 /* try to remap that extent elsewhere? */
1920 bio_put(bio);
1921 return -EIO;
1922 }
1923
1924 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1925 "sector %llu)\n", page->mapping->host->i_ino, start,
1926 dev->name, sector);
1927
1928 bio_put(bio);
1929 return 0;
1930}
1931
1932/*
1933 * each time an IO finishes, we do a fast check in the IO failure tree
1934 * to see if we need to process or clean up an io_failure_record
1935 */
1936static int clean_io_failure(u64 start, struct page *page)
1937{
1938 u64 private;
1939 u64 private_failure;
1940 struct io_failure_record *failrec;
1941 struct btrfs_mapping_tree *map_tree;
1942 struct extent_state *state;
1943 int num_copies;
1944 int did_repair = 0;
1945 int ret;
1946 struct inode *inode = page->mapping->host;
1947
1948 private = 0;
1949 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1950 (u64)-1, 1, EXTENT_DIRTY, 0);
1951 if (!ret)
1952 return 0;
1953
1954 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1955 &private_failure);
1956 if (ret)
1957 return 0;
1958
1959 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1960 BUG_ON(!failrec->this_mirror);
1961
1962 if (failrec->in_validation) {
1963 /* there was no real error, just free the record */
1964 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1965 failrec->start);
1966 did_repair = 1;
1967 goto out;
1968 }
1969
1970 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1971 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1972 failrec->start,
1973 EXTENT_LOCKED);
1974 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1975
1976 if (state && state->start == failrec->start) {
1977 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1978 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1979 failrec->len);
1980 if (num_copies > 1) {
1981 ret = repair_io_failure(map_tree, start, failrec->len,
1982 failrec->logical, page,
1983 failrec->failed_mirror);
1984 did_repair = !ret;
1985 }
1986 }
1987
1988out:
1989 if (!ret)
1990 ret = free_io_failure(inode, failrec, did_repair);
1991
1992 return ret;
1993}
1994
1995/*
1996 * this is a generic handler for readpage errors (default
1997 * readpage_io_failed_hook). if other copies exist, read those and write back
1998 * good data to the failed position. does not investigate in remapping the
1999 * failed extent elsewhere, hoping the device will be smart enough to do this as
2000 * needed
2001 */
2002
2003static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2004 u64 start, u64 end, int failed_mirror,
2005 struct extent_state *state)
2006{
2007 struct io_failure_record *failrec = NULL;
2008 u64 private;
2009 struct extent_map *em;
2010 struct inode *inode = page->mapping->host;
2011 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2012 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2013 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2014 struct bio *bio;
2015 int num_copies;
2016 int ret;
2017 int read_mode;
2018 u64 logical;
2019
2020 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2021
2022 ret = get_state_private(failure_tree, start, &private);
2023 if (ret) {
2024 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2025 if (!failrec)
2026 return -ENOMEM;
2027 failrec->start = start;
2028 failrec->len = end - start + 1;
2029 failrec->this_mirror = 0;
2030 failrec->bio_flags = 0;
2031 failrec->in_validation = 0;
2032
2033 read_lock(&em_tree->lock);
2034 em = lookup_extent_mapping(em_tree, start, failrec->len);
2035 if (!em) {
2036 read_unlock(&em_tree->lock);
2037 kfree(failrec);
2038 return -EIO;
2039 }
2040
2041 if (em->start > start || em->start + em->len < start) {
2042 free_extent_map(em);
2043 em = NULL;
2044 }
2045 read_unlock(&em_tree->lock);
2046
2047 if (!em || IS_ERR(em)) {
2048 kfree(failrec);
2049 return -EIO;
2050 }
2051 logical = start - em->start;
2052 logical = em->block_start + logical;
2053 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2054 logical = em->block_start;
2055 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2056 extent_set_compress_type(&failrec->bio_flags,
2057 em->compress_type);
2058 }
2059 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2060 "len=%llu\n", logical, start, failrec->len);
2061 failrec->logical = logical;
2062 free_extent_map(em);
2063
2064 /* set the bits in the private failure tree */
2065 ret = set_extent_bits(failure_tree, start, end,
2066 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2067 if (ret >= 0)
2068 ret = set_state_private(failure_tree, start,
2069 (u64)(unsigned long)failrec);
2070 /* set the bits in the inode's tree */
2071 if (ret >= 0)
2072 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2073 GFP_NOFS);
2074 if (ret < 0) {
2075 kfree(failrec);
2076 return ret;
2077 }
2078 } else {
2079 failrec = (struct io_failure_record *)(unsigned long)private;
2080 pr_debug("bio_readpage_error: (found) logical=%llu, "
2081 "start=%llu, len=%llu, validation=%d\n",
2082 failrec->logical, failrec->start, failrec->len,
2083 failrec->in_validation);
2084 /*
2085 * when data can be on disk more than twice, add to failrec here
2086 * (e.g. with a list for failed_mirror) to make
2087 * clean_io_failure() clean all those errors at once.
2088 */
2089 }
2090 num_copies = btrfs_num_copies(
2091 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2092 failrec->logical, failrec->len);
2093 if (num_copies == 1) {
2094 /*
2095 * we only have a single copy of the data, so don't bother with
2096 * all the retry and error correction code that follows. no
2097 * matter what the error is, it is very likely to persist.
2098 */
2099 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2100 "state=%p, num_copies=%d, next_mirror %d, "
2101 "failed_mirror %d\n", state, num_copies,
2102 failrec->this_mirror, failed_mirror);
2103 free_io_failure(inode, failrec, 0);
2104 return -EIO;
2105 }
2106
2107 if (!state) {
2108 spin_lock(&tree->lock);
2109 state = find_first_extent_bit_state(tree, failrec->start,
2110 EXTENT_LOCKED);
2111 if (state && state->start != failrec->start)
2112 state = NULL;
2113 spin_unlock(&tree->lock);
2114 }
2115
2116 /*
2117 * there are two premises:
2118 * a) deliver good data to the caller
2119 * b) correct the bad sectors on disk
2120 */
2121 if (failed_bio->bi_vcnt > 1) {
2122 /*
2123 * to fulfill b), we need to know the exact failing sectors, as
2124 * we don't want to rewrite any more than the failed ones. thus,
2125 * we need separate read requests for the failed bio
2126 *
2127 * if the following BUG_ON triggers, our validation request got
2128 * merged. we need separate requests for our algorithm to work.
2129 */
2130 BUG_ON(failrec->in_validation);
2131 failrec->in_validation = 1;
2132 failrec->this_mirror = failed_mirror;
2133 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2134 } else {
2135 /*
2136 * we're ready to fulfill a) and b) alongside. get a good copy
2137 * of the failed sector and if we succeed, we have setup
2138 * everything for repair_io_failure to do the rest for us.
2139 */
2140 if (failrec->in_validation) {
2141 BUG_ON(failrec->this_mirror != failed_mirror);
2142 failrec->in_validation = 0;
2143 failrec->this_mirror = 0;
2144 }
2145 failrec->failed_mirror = failed_mirror;
2146 failrec->this_mirror++;
2147 if (failrec->this_mirror == failed_mirror)
2148 failrec->this_mirror++;
2149 read_mode = READ_SYNC;
2150 }
2151
2152 if (!state || failrec->this_mirror > num_copies) {
2153 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2154 "next_mirror %d, failed_mirror %d\n", state,
2155 num_copies, failrec->this_mirror, failed_mirror);
2156 free_io_failure(inode, failrec, 0);
2157 return -EIO;
2158 }
2159
2160 bio = bio_alloc(GFP_NOFS, 1);
2161 bio->bi_private = state;
2162 bio->bi_end_io = failed_bio->bi_end_io;
2163 bio->bi_sector = failrec->logical >> 9;
2164 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2165 bio->bi_size = 0;
2166
2167 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2168
2169 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2170 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2171 failrec->this_mirror, num_copies, failrec->in_validation);
2172
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002173 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2174 failrec->this_mirror,
2175 failrec->bio_flags, 0);
2176 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177}
2178
Chris Masond1310b22008-01-24 16:13:08 -05002179/* lots and lots of room for performance fixes in the end_bio funcs */
2180
Jeff Mahoney87826df2012-02-15 16:23:57 +01002181int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2182{
2183 int uptodate = (err == 0);
2184 struct extent_io_tree *tree;
2185 int ret;
2186
2187 tree = &BTRFS_I(page->mapping->host)->io_tree;
2188
2189 if (tree->ops && tree->ops->writepage_end_io_hook) {
2190 ret = tree->ops->writepage_end_io_hook(page, start,
2191 end, NULL, uptodate);
2192 if (ret)
2193 uptodate = 0;
2194 }
2195
2196 if (!uptodate && tree->ops &&
2197 tree->ops->writepage_io_failed_hook) {
2198 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2199 start, end, NULL);
2200 /* Writeback already completed */
2201 if (ret == 0)
2202 return 1;
Jeff Mahoney355808c2011-10-03 23:23:14 -04002203 BUG_ON(ret < 0);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002204 }
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 }
Jeff Mahoney355808c2011-10-03 23:23:14 -04002356 BUG_ON(ret < 0);
Chris Mason7e383262008-04-09 16:28:12 -04002357 }
Chris Mason70dec802008-01-29 09:59:12 -05002358
Chris Mason771ed682008-11-06 22:02:51 -05002359 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002360 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002361 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002362 }
Arne Jansen507903b2011-04-06 10:02:20 +00002363 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002364
Chris Mason70dec802008-01-29 09:59:12 -05002365 if (whole_page) {
2366 if (uptodate) {
2367 SetPageUptodate(page);
2368 } else {
2369 ClearPageUptodate(page);
2370 SetPageError(page);
2371 }
Chris Masond1310b22008-01-24 16:13:08 -05002372 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002373 } else {
2374 if (uptodate) {
2375 check_page_uptodate(tree, page);
2376 } else {
2377 ClearPageUptodate(page);
2378 SetPageError(page);
2379 }
Chris Masond1310b22008-01-24 16:13:08 -05002380 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002381 }
Chris Mason4125bf72010-02-03 18:18:45 +00002382 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002383
2384 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002385}
2386
Miao Xie88f794e2010-11-22 03:02:55 +00002387struct bio *
2388btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2389 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002390{
2391 struct bio *bio;
2392
2393 bio = bio_alloc(gfp_flags, nr_vecs);
2394
2395 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2396 while (!bio && (nr_vecs /= 2))
2397 bio = bio_alloc(gfp_flags, nr_vecs);
2398 }
2399
2400 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002401 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002402 bio->bi_bdev = bdev;
2403 bio->bi_sector = first_sector;
2404 }
2405 return bio;
2406}
2407
Jeff Mahoney355808c2011-10-03 23:23:14 -04002408static int __must_check submit_one_bio(int rw, struct bio *bio,
2409 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002410{
Chris Masond1310b22008-01-24 16:13:08 -05002411 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002412 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2413 struct page *page = bvec->bv_page;
2414 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002415 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002416
2417 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002418
David Woodhouse902b22f2008-08-20 08:51:49 -04002419 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002420
2421 bio_get(bio);
2422
Chris Mason065631f2008-02-20 12:07:25 -05002423 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002424 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002425 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002426 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002427 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002428
Chris Masond1310b22008-01-24 16:13:08 -05002429 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2430 ret = -EOPNOTSUPP;
2431 bio_put(bio);
2432 return ret;
2433}
2434
Jeff Mahoney3444a972011-10-03 23:23:13 -04002435static int merge_bio(struct extent_io_tree *tree, struct page *page,
2436 unsigned long offset, size_t size, struct bio *bio,
2437 unsigned long bio_flags)
2438{
2439 int ret = 0;
2440 if (tree->ops && tree->ops->merge_bio_hook)
2441 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2442 bio_flags);
2443 BUG_ON(ret < 0);
2444 return ret;
2445
2446}
2447
Chris Masond1310b22008-01-24 16:13:08 -05002448static int submit_extent_page(int rw, struct extent_io_tree *tree,
2449 struct page *page, sector_t sector,
2450 size_t size, unsigned long offset,
2451 struct block_device *bdev,
2452 struct bio **bio_ret,
2453 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002454 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002455 int mirror_num,
2456 unsigned long prev_bio_flags,
2457 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002458{
2459 int ret = 0;
2460 struct bio *bio;
2461 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002462 int contig = 0;
2463 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2464 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002465 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002466
2467 if (bio_ret && *bio_ret) {
2468 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002469 if (old_compressed)
2470 contig = bio->bi_sector == sector;
2471 else
2472 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2473 sector;
2474
2475 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002476 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002477 bio_add_page(bio, page, page_size, offset) < page_size) {
2478 ret = submit_one_bio(rw, bio, mirror_num,
2479 prev_bio_flags);
Jeff Mahoney355808c2011-10-03 23:23:14 -04002480 BUG_ON(ret < 0);
Chris Masond1310b22008-01-24 16:13:08 -05002481 bio = NULL;
2482 } else {
2483 return 0;
2484 }
2485 }
Chris Masonc8b97812008-10-29 14:49:59 -04002486 if (this_compressed)
2487 nr = BIO_MAX_PAGES;
2488 else
2489 nr = bio_get_nr_vecs(bdev);
2490
Miao Xie88f794e2010-11-22 03:02:55 +00002491 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002492 if (!bio)
2493 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002494
Chris Masonc8b97812008-10-29 14:49:59 -04002495 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002496 bio->bi_end_io = end_io_func;
2497 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002498
Chris Masond3977122009-01-05 21:25:51 -05002499 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002500 *bio_ret = bio;
Jeff Mahoney355808c2011-10-03 23:23:14 -04002501 else {
Chris Masonc8b97812008-10-29 14:49:59 -04002502 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Jeff Mahoney355808c2011-10-03 23:23:14 -04002503 BUG_ON(ret < 0);
2504 }
Chris Masond1310b22008-01-24 16:13:08 -05002505
2506 return ret;
2507}
2508
2509void set_page_extent_mapped(struct page *page)
2510{
2511 if (!PagePrivate(page)) {
2512 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002513 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002514 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002515 }
2516}
2517
Christoph Hellwigb2950862008-12-02 09:54:17 -05002518static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002519{
Chris Masoneb14ab82011-02-10 12:35:00 -05002520 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002521 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2522}
2523
2524/*
2525 * basic readpage implementation. Locked extent state structs are inserted
2526 * into the tree that are removed when the IO is done (by the end_io
2527 * handlers)
2528 */
2529static int __extent_read_full_page(struct extent_io_tree *tree,
2530 struct page *page,
2531 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002532 struct bio **bio, int mirror_num,
2533 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002534{
2535 struct inode *inode = page->mapping->host;
2536 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2537 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2538 u64 end;
2539 u64 cur = start;
2540 u64 extent_offset;
2541 u64 last_byte = i_size_read(inode);
2542 u64 block_start;
2543 u64 cur_end;
2544 sector_t sector;
2545 struct extent_map *em;
2546 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002547 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002548 int ret;
2549 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002550 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002551 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002552 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002553 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002554 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002555
2556 set_page_extent_mapped(page);
2557
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002558 if (!PageUptodate(page)) {
2559 if (cleancache_get_page(page) == 0) {
2560 BUG_ON(blocksize != PAGE_SIZE);
2561 goto out;
2562 }
2563 }
2564
Chris Masond1310b22008-01-24 16:13:08 -05002565 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002566 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002567 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002568 ordered = btrfs_lookup_ordered_extent(inode, start);
2569 if (!ordered)
2570 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002571 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002572 btrfs_start_ordered_extent(inode, ordered, 1);
2573 btrfs_put_ordered_extent(ordered);
2574 }
Chris Masond1310b22008-01-24 16:13:08 -05002575
Chris Masonc8b97812008-10-29 14:49:59 -04002576 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2577 char *userpage;
2578 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2579
2580 if (zero_offset) {
2581 iosize = PAGE_CACHE_SIZE - zero_offset;
2582 userpage = kmap_atomic(page, KM_USER0);
2583 memset(userpage + zero_offset, 0, iosize);
2584 flush_dcache_page(page);
2585 kunmap_atomic(userpage, KM_USER0);
2586 }
2587 }
Chris Masond1310b22008-01-24 16:13:08 -05002588 while (cur <= end) {
2589 if (cur >= last_byte) {
2590 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002591 struct extent_state *cached = NULL;
2592
David Sterba306e16c2011-04-19 14:29:38 +02002593 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002594 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002595 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002596 flush_dcache_page(page);
2597 kunmap_atomic(userpage, KM_USER0);
2598 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002599 &cached, GFP_NOFS);
2600 unlock_extent_cached(tree, cur, cur + iosize - 1,
2601 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002602 break;
2603 }
David Sterba306e16c2011-04-19 14:29:38 +02002604 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002605 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002606 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002607 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002608 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002609 break;
2610 }
Chris Masond1310b22008-01-24 16:13:08 -05002611 extent_offset = cur - em->start;
2612 BUG_ON(extent_map_end(em) <= cur);
2613 BUG_ON(end < cur);
2614
Li Zefan261507a02010-12-17 14:21:50 +08002615 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002616 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002617 extent_set_compress_type(&this_bio_flag,
2618 em->compress_type);
2619 }
Chris Masonc8b97812008-10-29 14:49:59 -04002620
Chris Masond1310b22008-01-24 16:13:08 -05002621 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2622 cur_end = min(extent_map_end(em) - 1, end);
2623 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002624 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2625 disk_io_size = em->block_len;
2626 sector = em->block_start >> 9;
2627 } else {
2628 sector = (em->block_start + extent_offset) >> 9;
2629 disk_io_size = iosize;
2630 }
Chris Masond1310b22008-01-24 16:13:08 -05002631 bdev = em->bdev;
2632 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002633 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2634 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002635 free_extent_map(em);
2636 em = NULL;
2637
2638 /* we've found a hole, just zero and go on */
2639 if (block_start == EXTENT_MAP_HOLE) {
2640 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002641 struct extent_state *cached = NULL;
2642
Chris Masond1310b22008-01-24 16:13:08 -05002643 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002644 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002645 flush_dcache_page(page);
2646 kunmap_atomic(userpage, KM_USER0);
2647
2648 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002649 &cached, GFP_NOFS);
2650 unlock_extent_cached(tree, cur, cur + iosize - 1,
2651 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002652 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002653 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002654 continue;
2655 }
2656 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002657 if (test_range_bit(tree, cur, cur_end,
2658 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002659 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002660 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002661 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002662 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002663 continue;
2664 }
Chris Mason70dec802008-01-29 09:59:12 -05002665 /* we have an inline extent but it didn't get marked up
2666 * to date. Error out
2667 */
2668 if (block_start == EXTENT_MAP_INLINE) {
2669 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002670 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002671 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002672 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002673 continue;
2674 }
Chris Masond1310b22008-01-24 16:13:08 -05002675
2676 ret = 0;
2677 if (tree->ops && tree->ops->readpage_io_hook) {
2678 ret = tree->ops->readpage_io_hook(page, cur,
2679 cur + iosize - 1);
2680 }
2681 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002682 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2683 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002684 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002685 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002686 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002687 end_bio_extent_readpage, mirror_num,
2688 *bio_flags,
2689 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002690 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002691 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002692 }
2693 if (ret)
2694 SetPageError(page);
2695 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002696 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002697 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002698out:
Chris Masond1310b22008-01-24 16:13:08 -05002699 if (!nr) {
2700 if (!PageError(page))
2701 SetPageUptodate(page);
2702 unlock_page(page);
2703 }
2704 return 0;
2705}
2706
2707int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002708 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002709{
2710 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002711 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002712 int ret;
2713
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002714 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002715 &bio_flags);
Jeff Mahoney355808c2011-10-03 23:23:14 -04002716 if (bio) {
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002717 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney355808c2011-10-03 23:23:14 -04002718 BUG_ON(ret < 0);
2719 }
Chris Masond1310b22008-01-24 16:13:08 -05002720 return ret;
2721}
Chris Masond1310b22008-01-24 16:13:08 -05002722
Chris Mason11c83492009-04-20 15:50:09 -04002723static noinline void update_nr_written(struct page *page,
2724 struct writeback_control *wbc,
2725 unsigned long nr_written)
2726{
2727 wbc->nr_to_write -= nr_written;
2728 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2729 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2730 page->mapping->writeback_index = page->index + nr_written;
2731}
2732
Chris Masond1310b22008-01-24 16:13:08 -05002733/*
2734 * the writepage semantics are similar to regular writepage. extent
2735 * records are inserted to lock ranges in the tree, and as dirty areas
2736 * are found, they are marked writeback. Then the lock bits are removed
2737 * and the end_io handler clears the writeback ranges
2738 */
2739static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2740 void *data)
2741{
2742 struct inode *inode = page->mapping->host;
2743 struct extent_page_data *epd = data;
2744 struct extent_io_tree *tree = epd->tree;
2745 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2746 u64 delalloc_start;
2747 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2748 u64 end;
2749 u64 cur = start;
2750 u64 extent_offset;
2751 u64 last_byte = i_size_read(inode);
2752 u64 block_start;
2753 u64 iosize;
2754 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002755 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002756 struct extent_map *em;
2757 struct block_device *bdev;
2758 int ret;
2759 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002760 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002761 size_t blocksize;
2762 loff_t i_size = i_size_read(inode);
2763 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2764 u64 nr_delalloc;
2765 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002766 int page_started;
2767 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002768 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002769 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002770 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002771
Chris Masonffbd5172009-04-20 15:50:09 -04002772 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002773 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002774 else
2775 write_flags = WRITE;
2776
liubo1abe9b82011-03-24 11:18:59 +00002777 trace___extent_writepage(page, inode, wbc);
2778
Chris Masond1310b22008-01-24 16:13:08 -05002779 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002780
2781 ClearPageError(page);
2782
Chris Mason7f3c74f2008-07-18 12:01:11 -04002783 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002784 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002785 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002786 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002787 unlock_page(page);
2788 return 0;
2789 }
2790
2791 if (page->index == end_index) {
2792 char *userpage;
2793
Chris Masond1310b22008-01-24 16:13:08 -05002794 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002795 memset(userpage + pg_offset, 0,
2796 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002797 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002798 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002799 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002800 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002801
2802 set_page_extent_mapped(page);
2803
Josef Bacik9e487102011-08-01 12:08:18 -04002804 if (!tree->ops || !tree->ops->fill_delalloc)
2805 fill_delalloc = false;
2806
Chris Masond1310b22008-01-24 16:13:08 -05002807 delalloc_start = start;
2808 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002809 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002810 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002811 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002812 /*
2813 * make sure the wbc mapping index is at least updated
2814 * to this page.
2815 */
2816 update_nr_written(page, wbc, 0);
2817
Chris Masond3977122009-01-05 21:25:51 -05002818 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002819 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002820 page,
2821 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002822 &delalloc_end,
2823 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002824 if (nr_delalloc == 0) {
2825 delalloc_start = delalloc_end + 1;
2826 continue;
2827 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002828 ret = tree->ops->fill_delalloc(inode, page,
2829 delalloc_start,
2830 delalloc_end,
2831 &page_started,
2832 &nr_written);
2833 BUG_ON(ret);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002834 /*
2835 * delalloc_end is already one less than the total
2836 * length, so we don't subtract one from
2837 * PAGE_CACHE_SIZE
2838 */
2839 delalloc_to_write += (delalloc_end - delalloc_start +
2840 PAGE_CACHE_SIZE) >>
2841 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002842 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002843 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002844 if (wbc->nr_to_write < delalloc_to_write) {
2845 int thresh = 8192;
2846
2847 if (delalloc_to_write < thresh * 2)
2848 thresh = delalloc_to_write;
2849 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2850 thresh);
2851 }
Chris Masonc8b97812008-10-29 14:49:59 -04002852
Chris Mason771ed682008-11-06 22:02:51 -05002853 /* did the fill delalloc function already unlock and start
2854 * the IO?
2855 */
2856 if (page_started) {
2857 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002858 /*
2859 * we've unlocked the page, so we can't update
2860 * the mapping's writeback index, just update
2861 * nr_to_write.
2862 */
2863 wbc->nr_to_write -= nr_written;
2864 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002865 }
Chris Masonc8b97812008-10-29 14:49:59 -04002866 }
Chris Mason247e7432008-07-17 12:53:51 -04002867 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002868 ret = tree->ops->writepage_start_hook(page, start,
2869 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002870 if (ret) {
2871 /* Fixup worker will requeue */
2872 if (ret == -EBUSY)
2873 wbc->pages_skipped++;
2874 else
2875 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002876 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002877 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002878 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002879 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002880 }
2881 }
2882
Chris Mason11c83492009-04-20 15:50:09 -04002883 /*
2884 * we don't want to touch the inode after unlocking the page,
2885 * so we update the mapping writeback index now
2886 */
2887 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002888
Chris Masond1310b22008-01-24 16:13:08 -05002889 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002890 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002891 if (tree->ops && tree->ops->writepage_end_io_hook)
2892 tree->ops->writepage_end_io_hook(page, start,
2893 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002894 goto done;
2895 }
2896
Chris Masond1310b22008-01-24 16:13:08 -05002897 blocksize = inode->i_sb->s_blocksize;
2898
2899 while (cur <= end) {
2900 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002901 if (tree->ops && tree->ops->writepage_end_io_hook)
2902 tree->ops->writepage_end_io_hook(page, cur,
2903 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002904 break;
2905 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002906 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002907 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002908 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002909 SetPageError(page);
2910 break;
2911 }
2912
2913 extent_offset = cur - em->start;
2914 BUG_ON(extent_map_end(em) <= cur);
2915 BUG_ON(end < cur);
2916 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2917 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2918 sector = (em->block_start + extent_offset) >> 9;
2919 bdev = em->bdev;
2920 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002921 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002922 free_extent_map(em);
2923 em = NULL;
2924
Chris Masonc8b97812008-10-29 14:49:59 -04002925 /*
2926 * compressed and inline extents are written through other
2927 * paths in the FS
2928 */
2929 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002930 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002931 /*
2932 * end_io notification does not happen here for
2933 * compressed extents
2934 */
2935 if (!compressed && tree->ops &&
2936 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002937 tree->ops->writepage_end_io_hook(page, cur,
2938 cur + iosize - 1,
2939 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002940 else if (compressed) {
2941 /* we don't want to end_page_writeback on
2942 * a compressed extent. this happens
2943 * elsewhere
2944 */
2945 nr++;
2946 }
2947
2948 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002949 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002950 continue;
2951 }
Chris Masond1310b22008-01-24 16:13:08 -05002952 /* leave this out until we have a page_mkwrite call */
2953 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002954 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002955 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002956 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002957 continue;
2958 }
Chris Masonc8b97812008-10-29 14:49:59 -04002959
Chris Masond1310b22008-01-24 16:13:08 -05002960 if (tree->ops && tree->ops->writepage_io_hook) {
2961 ret = tree->ops->writepage_io_hook(page, cur,
2962 cur + iosize - 1);
2963 } else {
2964 ret = 0;
2965 }
Chris Mason1259ab72008-05-12 13:39:03 -04002966 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002967 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002968 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002969 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002970
Chris Masond1310b22008-01-24 16:13:08 -05002971 set_range_writeback(tree, cur, cur + iosize - 1);
2972 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002973 printk(KERN_ERR "btrfs warning page %lu not "
2974 "writeback, cur %llu end %llu\n",
2975 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002976 (unsigned long long)end);
2977 }
2978
Chris Masonffbd5172009-04-20 15:50:09 -04002979 ret = submit_extent_page(write_flags, tree, page,
2980 sector, iosize, pg_offset,
2981 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002982 end_bio_extent_writepage,
2983 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002984 if (ret)
2985 SetPageError(page);
2986 }
2987 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002988 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002989 nr++;
2990 }
2991done:
2992 if (nr == 0) {
2993 /* make sure the mapping tag for page dirty gets cleared */
2994 set_page_writeback(page);
2995 end_page_writeback(page);
2996 }
Chris Masond1310b22008-01-24 16:13:08 -05002997 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002998
Chris Mason11c83492009-04-20 15:50:09 -04002999done_unlocked:
3000
Chris Mason2c64c532009-09-02 15:04:12 -04003001 /* drop our reference on any cached states */
3002 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003003 return 0;
3004}
3005
Chris Masond1310b22008-01-24 16:13:08 -05003006/**
Chris Mason4bef0842008-09-08 11:18:08 -04003007 * 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 -05003008 * @mapping: address space structure to write
3009 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3010 * @writepage: function called for each page
3011 * @data: data passed to writepage function
3012 *
3013 * If a page is already under I/O, write_cache_pages() skips it, even
3014 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3015 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3016 * and msync() need to guarantee that all the data which was dirty at the time
3017 * the call was made get new I/O started against them. If wbc->sync_mode is
3018 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3019 * existing IO to complete.
3020 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003021static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003022 struct address_space *mapping,
3023 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003024 writepage_t writepage, void *data,
3025 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003026{
Chris Masond1310b22008-01-24 16:13:08 -05003027 int ret = 0;
3028 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003029 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003030 struct pagevec pvec;
3031 int nr_pages;
3032 pgoff_t index;
3033 pgoff_t end; /* Inclusive */
3034 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003035 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003036
Chris Masond1310b22008-01-24 16:13:08 -05003037 pagevec_init(&pvec, 0);
3038 if (wbc->range_cyclic) {
3039 index = mapping->writeback_index; /* Start from prev offset */
3040 end = -1;
3041 } else {
3042 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3043 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003044 scanned = 1;
3045 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003046 if (wbc->sync_mode == WB_SYNC_ALL)
3047 tag = PAGECACHE_TAG_TOWRITE;
3048 else
3049 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003050retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003051 if (wbc->sync_mode == WB_SYNC_ALL)
3052 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003053 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003054 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3055 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003056 unsigned i;
3057
3058 scanned = 1;
3059 for (i = 0; i < nr_pages; i++) {
3060 struct page *page = pvec.pages[i];
3061
3062 /*
3063 * At this point we hold neither mapping->tree_lock nor
3064 * lock on the page itself: the page may be truncated or
3065 * invalidated (changing page->mapping to NULL), or even
3066 * swizzled back from swapper_space to tmpfs file
3067 * mapping
3068 */
Chris Mason01d658f2011-11-01 10:08:06 -04003069 if (tree->ops &&
3070 tree->ops->write_cache_pages_lock_hook) {
3071 tree->ops->write_cache_pages_lock_hook(page,
3072 data, flush_fn);
3073 } else {
3074 if (!trylock_page(page)) {
3075 flush_fn(data);
3076 lock_page(page);
3077 }
3078 }
Chris Masond1310b22008-01-24 16:13:08 -05003079
3080 if (unlikely(page->mapping != mapping)) {
3081 unlock_page(page);
3082 continue;
3083 }
3084
3085 if (!wbc->range_cyclic && page->index > end) {
3086 done = 1;
3087 unlock_page(page);
3088 continue;
3089 }
3090
Chris Masond2c3f4f2008-11-19 12:44:22 -05003091 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003092 if (PageWriteback(page))
3093 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003094 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003095 }
Chris Masond1310b22008-01-24 16:13:08 -05003096
3097 if (PageWriteback(page) ||
3098 !clear_page_dirty_for_io(page)) {
3099 unlock_page(page);
3100 continue;
3101 }
3102
3103 ret = (*writepage)(page, wbc, data);
3104
3105 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3106 unlock_page(page);
3107 ret = 0;
3108 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003109 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003110 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003111
3112 /*
3113 * the filesystem may choose to bump up nr_to_write.
3114 * We have to make sure to honor the new nr_to_write
3115 * at any time
3116 */
3117 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003118 }
3119 pagevec_release(&pvec);
3120 cond_resched();
3121 }
3122 if (!scanned && !done) {
3123 /*
3124 * We hit the last page and there is more work to be done: wrap
3125 * back to the start of the file
3126 */
3127 scanned = 1;
3128 index = 0;
3129 goto retry;
3130 }
Chris Masond1310b22008-01-24 16:13:08 -05003131 return ret;
3132}
Chris Masond1310b22008-01-24 16:13:08 -05003133
Chris Masonffbd5172009-04-20 15:50:09 -04003134static void flush_epd_write_bio(struct extent_page_data *epd)
3135{
3136 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003137 int rw = WRITE;
3138 int ret;
3139
Chris Masonffbd5172009-04-20 15:50:09 -04003140 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003141 rw = WRITE_SYNC;
3142
3143 ret = submit_one_bio(rw, epd->bio, 0, 0);
3144 BUG_ON(ret < 0);
Chris Masonffbd5172009-04-20 15:50:09 -04003145 epd->bio = NULL;
3146 }
3147}
3148
Chris Masond2c3f4f2008-11-19 12:44:22 -05003149static noinline void flush_write_bio(void *data)
3150{
3151 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003152 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003153}
3154
Chris Masond1310b22008-01-24 16:13:08 -05003155int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3156 get_extent_t *get_extent,
3157 struct writeback_control *wbc)
3158{
3159 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003160 struct extent_page_data epd = {
3161 .bio = NULL,
3162 .tree = tree,
3163 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003164 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003165 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003166 };
Chris Masond1310b22008-01-24 16:13:08 -05003167
Chris Masond1310b22008-01-24 16:13:08 -05003168 ret = __extent_writepage(page, wbc, &epd);
3169
Chris Masonffbd5172009-04-20 15:50:09 -04003170 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003171 return ret;
3172}
Chris Masond1310b22008-01-24 16:13:08 -05003173
Chris Mason771ed682008-11-06 22:02:51 -05003174int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3175 u64 start, u64 end, get_extent_t *get_extent,
3176 int mode)
3177{
3178 int ret = 0;
3179 struct address_space *mapping = inode->i_mapping;
3180 struct page *page;
3181 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3182 PAGE_CACHE_SHIFT;
3183
3184 struct extent_page_data epd = {
3185 .bio = NULL,
3186 .tree = tree,
3187 .get_extent = get_extent,
3188 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003189 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003190 };
3191 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003192 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003193 .nr_to_write = nr_pages * 2,
3194 .range_start = start,
3195 .range_end = end + 1,
3196 };
3197
Chris Masond3977122009-01-05 21:25:51 -05003198 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003199 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3200 if (clear_page_dirty_for_io(page))
3201 ret = __extent_writepage(page, &wbc_writepages, &epd);
3202 else {
3203 if (tree->ops && tree->ops->writepage_end_io_hook)
3204 tree->ops->writepage_end_io_hook(page, start,
3205 start + PAGE_CACHE_SIZE - 1,
3206 NULL, 1);
3207 unlock_page(page);
3208 }
3209 page_cache_release(page);
3210 start += PAGE_CACHE_SIZE;
3211 }
3212
Chris Masonffbd5172009-04-20 15:50:09 -04003213 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003214 return ret;
3215}
Chris Masond1310b22008-01-24 16:13:08 -05003216
3217int extent_writepages(struct extent_io_tree *tree,
3218 struct address_space *mapping,
3219 get_extent_t *get_extent,
3220 struct writeback_control *wbc)
3221{
3222 int ret = 0;
3223 struct extent_page_data epd = {
3224 .bio = NULL,
3225 .tree = tree,
3226 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003227 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003228 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003229 };
3230
Chris Mason4bef0842008-09-08 11:18:08 -04003231 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003232 __extent_writepage, &epd,
3233 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003234 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003235 return ret;
3236}
Chris Masond1310b22008-01-24 16:13:08 -05003237
3238int extent_readpages(struct extent_io_tree *tree,
3239 struct address_space *mapping,
3240 struct list_head *pages, unsigned nr_pages,
3241 get_extent_t get_extent)
3242{
3243 struct bio *bio = NULL;
3244 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003245 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003246
Chris Masond1310b22008-01-24 16:13:08 -05003247 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3248 struct page *page = list_entry(pages->prev, struct page, lru);
3249
3250 prefetchw(&page->flags);
3251 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003252 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003253 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003254 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003255 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003256 }
3257 page_cache_release(page);
3258 }
Chris Masond1310b22008-01-24 16:13:08 -05003259 BUG_ON(!list_empty(pages));
Jeff Mahoney355808c2011-10-03 23:23:14 -04003260 if (bio) {
3261 int ret = submit_one_bio(READ, bio, 0, bio_flags);
3262 BUG_ON(ret < 0);
3263 }
Chris Masond1310b22008-01-24 16:13:08 -05003264 return 0;
3265}
Chris Masond1310b22008-01-24 16:13:08 -05003266
3267/*
3268 * basic invalidatepage code, this waits on any locked or writeback
3269 * ranges corresponding to the page, and then deletes any extent state
3270 * records from the tree
3271 */
3272int extent_invalidatepage(struct extent_io_tree *tree,
3273 struct page *page, unsigned long offset)
3274{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003275 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003276 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3277 u64 end = start + PAGE_CACHE_SIZE - 1;
3278 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3279
Chris Masond3977122009-01-05 21:25:51 -05003280 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003281 if (start > end)
3282 return 0;
3283
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003284 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003285 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003286 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003287 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3288 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003289 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003290 return 0;
3291}
Chris Masond1310b22008-01-24 16:13:08 -05003292
3293/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003294 * a helper for releasepage, this tests for areas of the page that
3295 * are locked or under IO and drops the related state bits if it is safe
3296 * to drop the page.
3297 */
3298int try_release_extent_state(struct extent_map_tree *map,
3299 struct extent_io_tree *tree, struct page *page,
3300 gfp_t mask)
3301{
3302 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3303 u64 end = start + PAGE_CACHE_SIZE - 1;
3304 int ret = 1;
3305
Chris Mason211f90e2008-07-18 11:56:15 -04003306 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003307 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003308 ret = 0;
3309 else {
3310 if ((mask & GFP_NOFS) == GFP_NOFS)
3311 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003312 /*
3313 * at this point we can safely clear everything except the
3314 * locked bit and the nodatasum bit
3315 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003316 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003317 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3318 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003319
3320 /* if clear_extent_bit failed for enomem reasons,
3321 * we can't allow the release to continue.
3322 */
3323 if (ret < 0)
3324 ret = 0;
3325 else
3326 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003327 }
3328 return ret;
3329}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003330
3331/*
Chris Masond1310b22008-01-24 16:13:08 -05003332 * a helper for releasepage. As long as there are no locked extents
3333 * in the range corresponding to the page, both state records and extent
3334 * map records are removed
3335 */
3336int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003337 struct extent_io_tree *tree, struct page *page,
3338 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003339{
3340 struct extent_map *em;
3341 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3342 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003343
Chris Mason70dec802008-01-29 09:59:12 -05003344 if ((mask & __GFP_WAIT) &&
3345 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003346 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003347 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003348 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003349 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003350 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003351 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003352 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003353 break;
3354 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003355 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3356 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003357 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003358 free_extent_map(em);
3359 break;
3360 }
3361 if (!test_range_bit(tree, em->start,
3362 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003363 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003364 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003365 remove_extent_mapping(map, em);
3366 /* once for the rb tree */
3367 free_extent_map(em);
3368 }
3369 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003370 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003371
3372 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003373 free_extent_map(em);
3374 }
Chris Masond1310b22008-01-24 16:13:08 -05003375 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003376 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003377}
Chris Masond1310b22008-01-24 16:13:08 -05003378
Chris Masonec29ed52011-02-23 16:23:20 -05003379/*
3380 * helper function for fiemap, which doesn't want to see any holes.
3381 * This maps until we find something past 'last'
3382 */
3383static struct extent_map *get_extent_skip_holes(struct inode *inode,
3384 u64 offset,
3385 u64 last,
3386 get_extent_t *get_extent)
3387{
3388 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3389 struct extent_map *em;
3390 u64 len;
3391
3392 if (offset >= last)
3393 return NULL;
3394
3395 while(1) {
3396 len = last - offset;
3397 if (len == 0)
3398 break;
3399 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3400 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003401 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003402 return em;
3403
3404 /* if this isn't a hole return it */
3405 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3406 em->block_start != EXTENT_MAP_HOLE) {
3407 return em;
3408 }
3409
3410 /* this is a hole, advance to the next extent */
3411 offset = extent_map_end(em);
3412 free_extent_map(em);
3413 if (offset >= last)
3414 break;
3415 }
3416 return NULL;
3417}
3418
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003419int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3420 __u64 start, __u64 len, get_extent_t *get_extent)
3421{
Josef Bacik975f84f2010-11-23 19:36:57 +00003422 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003423 u64 off = start;
3424 u64 max = start + len;
3425 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003426 u32 found_type;
3427 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003428 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003429 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003430 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003431 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003432 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003433 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003434 struct btrfs_path *path;
3435 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003436 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003437 u64 em_start = 0;
3438 u64 em_len = 0;
3439 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003440 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003441
3442 if (len == 0)
3443 return -EINVAL;
3444
Josef Bacik975f84f2010-11-23 19:36:57 +00003445 path = btrfs_alloc_path();
3446 if (!path)
3447 return -ENOMEM;
3448 path->leave_spinning = 1;
3449
Josef Bacik4d479cf2011-11-17 11:34:31 -05003450 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3451 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3452
Chris Masonec29ed52011-02-23 16:23:20 -05003453 /*
3454 * lookup the last file extent. We're not using i_size here
3455 * because there might be preallocation past i_size
3456 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003457 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003458 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003459 if (ret < 0) {
3460 btrfs_free_path(path);
3461 return ret;
3462 }
3463 WARN_ON(!ret);
3464 path->slots[0]--;
3465 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3466 struct btrfs_file_extent_item);
3467 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3468 found_type = btrfs_key_type(&found_key);
3469
Chris Masonec29ed52011-02-23 16:23:20 -05003470 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003471 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003472 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003473 /* have to trust i_size as the end */
3474 last = (u64)-1;
3475 last_for_get_extent = isize;
3476 } else {
3477 /*
3478 * remember the start of the last extent. There are a
3479 * bunch of different factors that go into the length of the
3480 * extent, so its much less complex to remember where it started
3481 */
3482 last = found_key.offset;
3483 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003484 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003485 btrfs_free_path(path);
3486
Chris Masonec29ed52011-02-23 16:23:20 -05003487 /*
3488 * we might have some extents allocated but more delalloc past those
3489 * extents. so, we trust isize unless the start of the last extent is
3490 * beyond isize
3491 */
3492 if (last < isize) {
3493 last = (u64)-1;
3494 last_for_get_extent = isize;
3495 }
3496
Josef Bacik2ac55d42010-02-03 19:33:23 +00003497 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003498 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003499
Josef Bacik4d479cf2011-11-17 11:34:31 -05003500 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003501 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003502 if (!em)
3503 goto out;
3504 if (IS_ERR(em)) {
3505 ret = PTR_ERR(em);
3506 goto out;
3507 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003508
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003509 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003510 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003511
Chris Masonea8efc72011-03-08 11:54:40 -05003512 /* break if the extent we found is outside the range */
3513 if (em->start >= max || extent_map_end(em) < off)
3514 break;
3515
3516 /*
3517 * get_extent may return an extent that starts before our
3518 * requested range. We have to make sure the ranges
3519 * we return to fiemap always move forward and don't
3520 * overlap, so adjust the offsets here
3521 */
3522 em_start = max(em->start, off);
3523
3524 /*
3525 * record the offset from the start of the extent
3526 * for adjusting the disk offset below
3527 */
3528 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003529 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003530 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003531 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003532 disko = 0;
3533 flags = 0;
3534
Chris Masonea8efc72011-03-08 11:54:40 -05003535 /*
3536 * bump off for our next call to get_extent
3537 */
3538 off = extent_map_end(em);
3539 if (off >= max)
3540 end = 1;
3541
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003542 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003543 end = 1;
3544 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003545 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003546 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3547 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003548 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003549 flags |= (FIEMAP_EXTENT_DELALLOC |
3550 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003551 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003552 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003553 }
3554 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3555 flags |= FIEMAP_EXTENT_ENCODED;
3556
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003557 free_extent_map(em);
3558 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003559 if ((em_start >= last) || em_len == (u64)-1 ||
3560 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003561 flags |= FIEMAP_EXTENT_LAST;
3562 end = 1;
3563 }
3564
Chris Masonec29ed52011-02-23 16:23:20 -05003565 /* now scan forward to see if this is really the last extent. */
3566 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3567 get_extent);
3568 if (IS_ERR(em)) {
3569 ret = PTR_ERR(em);
3570 goto out;
3571 }
3572 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003573 flags |= FIEMAP_EXTENT_LAST;
3574 end = 1;
3575 }
Chris Masonec29ed52011-02-23 16:23:20 -05003576 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3577 em_len, flags);
3578 if (ret)
3579 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003580 }
3581out_free:
3582 free_extent_map(em);
3583out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003584 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3585 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003586 return ret;
3587}
3588
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003589inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003590 unsigned long i)
3591{
3592 struct page *p;
3593 struct address_space *mapping;
3594
3595 if (i == 0)
3596 return eb->first_page;
3597 i += eb->start >> PAGE_CACHE_SHIFT;
3598 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003599 if (!mapping)
3600 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003601
3602 /*
3603 * extent_buffer_page is only called after pinning the page
3604 * by increasing the reference count. So we know the page must
3605 * be in the radix tree.
3606 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003607 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003608 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003609 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003610
Chris Masond1310b22008-01-24 16:13:08 -05003611 return p;
3612}
3613
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003614inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003615{
Chris Mason6af118ce2008-07-22 11:18:07 -04003616 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3617 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003618}
3619
Chris Masond1310b22008-01-24 16:13:08 -05003620static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3621 u64 start,
3622 unsigned long len,
3623 gfp_t mask)
3624{
3625 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003626#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003627 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003628#endif
Chris Masond1310b22008-01-24 16:13:08 -05003629
Chris Masond1310b22008-01-24 16:13:08 -05003630 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003631 if (eb == NULL)
3632 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003633 eb->start = start;
3634 eb->len = len;
Chris Masonbd681512011-07-16 15:23:14 -04003635 rwlock_init(&eb->lock);
3636 atomic_set(&eb->write_locks, 0);
3637 atomic_set(&eb->read_locks, 0);
3638 atomic_set(&eb->blocking_readers, 0);
3639 atomic_set(&eb->blocking_writers, 0);
3640 atomic_set(&eb->spinning_readers, 0);
3641 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003642 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003643 init_waitqueue_head(&eb->write_lock_wq);
3644 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003645
Chris Mason39351272009-02-04 09:24:05 -05003646#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003647 spin_lock_irqsave(&leak_lock, flags);
3648 list_add(&eb->leak_list, &buffers);
3649 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003650#endif
Chris Masond1310b22008-01-24 16:13:08 -05003651 atomic_set(&eb->refs, 1);
3652
3653 return eb;
3654}
3655
3656static void __free_extent_buffer(struct extent_buffer *eb)
3657{
Chris Mason39351272009-02-04 09:24:05 -05003658#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003659 unsigned long flags;
3660 spin_lock_irqsave(&leak_lock, flags);
3661 list_del(&eb->leak_list);
3662 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003663#endif
Chris Masond1310b22008-01-24 16:13:08 -05003664 kmem_cache_free(extent_buffer_cache, eb);
3665}
3666
Miao Xie897ca6e92010-10-26 20:57:29 -04003667/*
3668 * Helper for releasing extent buffer page.
3669 */
3670static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3671 unsigned long start_idx)
3672{
3673 unsigned long index;
3674 struct page *page;
3675
3676 if (!eb->first_page)
3677 return;
3678
3679 index = num_extent_pages(eb->start, eb->len);
3680 if (start_idx >= index)
3681 return;
3682
3683 do {
3684 index--;
3685 page = extent_buffer_page(eb, index);
3686 if (page)
3687 page_cache_release(page);
3688 } while (index != start_idx);
3689}
3690
3691/*
3692 * Helper for releasing the extent buffer.
3693 */
3694static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3695{
3696 btrfs_release_extent_buffer_page(eb, 0);
3697 __free_extent_buffer(eb);
3698}
3699
Chris Masond1310b22008-01-24 16:13:08 -05003700struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3701 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003702 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003703{
3704 unsigned long num_pages = num_extent_pages(start, len);
3705 unsigned long i;
3706 unsigned long index = start >> PAGE_CACHE_SHIFT;
3707 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003708 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003709 struct page *p;
3710 struct address_space *mapping = tree->mapping;
3711 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003712 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003713
Miao Xie19fe0a82010-10-26 20:57:29 -04003714 rcu_read_lock();
3715 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3716 if (eb && atomic_inc_not_zero(&eb->refs)) {
3717 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003718 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003719 return eb;
3720 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003721 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003722
David Sterbaba144192011-04-21 01:12:06 +02003723 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003724 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003725 return NULL;
3726
Chris Masond1310b22008-01-24 16:13:08 -05003727 if (page0) {
3728 eb->first_page = page0;
3729 i = 1;
3730 index++;
3731 page_cache_get(page0);
3732 mark_page_accessed(page0);
3733 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003734 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003735 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003736 } else {
3737 i = 0;
3738 }
3739 for (; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003740 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003741 if (!p) {
3742 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003743 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003744 }
3745 set_page_extent_mapped(p);
3746 mark_page_accessed(p);
3747 if (i == 0) {
3748 eb->first_page = p;
3749 set_page_extent_head(p, len);
3750 } else {
3751 set_page_private(p, EXTENT_PAGE_PRIVATE);
3752 }
3753 if (!PageUptodate(p))
3754 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003755
3756 /*
3757 * see below about how we avoid a nasty race with release page
3758 * and why we unlock later
3759 */
3760 if (i != 0)
3761 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003762 }
3763 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003764 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003765
Miao Xie19fe0a82010-10-26 20:57:29 -04003766 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3767 if (ret)
3768 goto free_eb;
3769
Chris Mason6af118ce2008-07-22 11:18:07 -04003770 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003771 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3772 if (ret == -EEXIST) {
3773 exists = radix_tree_lookup(&tree->buffer,
3774 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003775 /* add one reference for the caller */
3776 atomic_inc(&exists->refs);
3777 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003778 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003779 goto free_eb;
3780 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003781 /* add one reference for the tree */
3782 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003783 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003784 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003785
3786 /*
3787 * there is a race where release page may have
3788 * tried to find this extent buffer in the radix
3789 * but failed. It will tell the VM it is safe to
3790 * reclaim the, and it will clear the page private bit.
3791 * We must make sure to set the page private bit properly
3792 * after the extent buffer is in the radix tree so
3793 * it doesn't get lost
3794 */
3795 set_page_extent_mapped(eb->first_page);
3796 set_page_extent_head(eb->first_page, eb->len);
3797 if (!page0)
3798 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003799 return eb;
3800
Chris Mason6af118ce2008-07-22 11:18:07 -04003801free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003802 if (eb->first_page && !page0)
3803 unlock_page(eb->first_page);
3804
Chris Masond1310b22008-01-24 16:13:08 -05003805 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003806 return exists;
Miao Xie897ca6e92010-10-26 20:57:29 -04003807 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003808 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003809}
Chris Masond1310b22008-01-24 16:13:08 -05003810
3811struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003812 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003813{
Chris Masond1310b22008-01-24 16:13:08 -05003814 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003815
Miao Xie19fe0a82010-10-26 20:57:29 -04003816 rcu_read_lock();
3817 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3818 if (eb && atomic_inc_not_zero(&eb->refs)) {
3819 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003820 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003821 return eb;
3822 }
3823 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003824
Miao Xie19fe0a82010-10-26 20:57:29 -04003825 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003826}
Chris Masond1310b22008-01-24 16:13:08 -05003827
3828void free_extent_buffer(struct extent_buffer *eb)
3829{
Chris Masond1310b22008-01-24 16:13:08 -05003830 if (!eb)
3831 return;
3832
3833 if (!atomic_dec_and_test(&eb->refs))
3834 return;
3835
Chris Mason6af118ce2008-07-22 11:18:07 -04003836 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003837}
Chris Masond1310b22008-01-24 16:13:08 -05003838
Jeff Mahoney143bede2012-03-01 14:56:26 +01003839void clear_extent_buffer_dirty(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -05003840 struct extent_buffer *eb)
3841{
Chris Masond1310b22008-01-24 16:13:08 -05003842 unsigned long i;
3843 unsigned long num_pages;
3844 struct page *page;
3845
Chris Masond1310b22008-01-24 16:13:08 -05003846 num_pages = num_extent_pages(eb->start, eb->len);
3847
3848 for (i = 0; i < num_pages; i++) {
3849 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003850 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003851 continue;
3852
Chris Masona61e6f22008-07-22 11:18:08 -04003853 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003854 WARN_ON(!PagePrivate(page));
3855
3856 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003857 if (i == 0)
3858 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003859
Chris Masond1310b22008-01-24 16:13:08 -05003860 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003861 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003862 if (!PageDirty(page)) {
3863 radix_tree_tag_clear(&page->mapping->page_tree,
3864 page_index(page),
3865 PAGECACHE_TAG_DIRTY);
3866 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003867 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04003868 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04003869 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003870 }
Chris Masond1310b22008-01-24 16:13:08 -05003871}
Chris Masond1310b22008-01-24 16:13:08 -05003872
Chris Masond1310b22008-01-24 16:13:08 -05003873int set_extent_buffer_dirty(struct extent_io_tree *tree,
3874 struct extent_buffer *eb)
3875{
3876 unsigned long i;
3877 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003878 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003879
Chris Masonb9473432009-03-13 11:00:37 -04003880 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003881 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003882 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003883 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003884 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003885}
Chris Masond1310b22008-01-24 16:13:08 -05003886
Chris Mason19b6caf2011-07-25 06:50:50 -04003887static int __eb_straddles_pages(u64 start, u64 len)
3888{
3889 if (len < PAGE_CACHE_SIZE)
3890 return 1;
3891 if (start & (PAGE_CACHE_SIZE - 1))
3892 return 1;
3893 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3894 return 1;
3895 return 0;
3896}
3897
3898static int eb_straddles_pages(struct extent_buffer *eb)
3899{
3900 return __eb_straddles_pages(eb->start, eb->len);
3901}
3902
Chris Mason1259ab72008-05-12 13:39:03 -04003903int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003904 struct extent_buffer *eb,
3905 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003906{
3907 unsigned long i;
3908 struct page *page;
3909 unsigned long num_pages;
3910
3911 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003912 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003913
Chris Mason50653192012-02-22 12:36:24 -05003914 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3915 cached_state, GFP_NOFS);
3916
Chris Mason1259ab72008-05-12 13:39:03 -04003917 for (i = 0; i < num_pages; i++) {
3918 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003919 if (page)
3920 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003921 }
3922 return 0;
3923}
3924
Chris Masond1310b22008-01-24 16:13:08 -05003925int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3926 struct extent_buffer *eb)
3927{
3928 unsigned long i;
3929 struct page *page;
3930 unsigned long num_pages;
3931
3932 num_pages = num_extent_pages(eb->start, eb->len);
3933
Chris Mason19b6caf2011-07-25 06:50:50 -04003934 if (eb_straddles_pages(eb)) {
3935 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3936 NULL, GFP_NOFS);
3937 }
Chris Masond1310b22008-01-24 16:13:08 -05003938 for (i = 0; i < num_pages; i++) {
3939 page = extent_buffer_page(eb, i);
3940 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3941 ((i == num_pages - 1) &&
3942 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3943 check_page_uptodate(tree, page);
3944 continue;
3945 }
3946 SetPageUptodate(page);
3947 }
3948 return 0;
3949}
Chris Masond1310b22008-01-24 16:13:08 -05003950
Chris Masonce9adaa2008-04-09 16:28:12 -04003951int extent_range_uptodate(struct extent_io_tree *tree,
3952 u64 start, u64 end)
3953{
3954 struct page *page;
3955 int ret;
3956 int pg_uptodate = 1;
3957 int uptodate;
3958 unsigned long index;
3959
Chris Mason19b6caf2011-07-25 06:50:50 -04003960 if (__eb_straddles_pages(start, end - start + 1)) {
3961 ret = test_range_bit(tree, start, end,
3962 EXTENT_UPTODATE, 1, NULL);
3963 if (ret)
3964 return 1;
3965 }
Chris Masond3977122009-01-05 21:25:51 -05003966 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003967 index = start >> PAGE_CACHE_SHIFT;
3968 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05003969 if (!page)
3970 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04003971 uptodate = PageUptodate(page);
3972 page_cache_release(page);
3973 if (!uptodate) {
3974 pg_uptodate = 0;
3975 break;
3976 }
3977 start += PAGE_CACHE_SIZE;
3978 }
3979 return pg_uptodate;
3980}
3981
Chris Masond1310b22008-01-24 16:13:08 -05003982int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003983 struct extent_buffer *eb,
3984 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003985{
Chris Mason728131d2008-04-09 16:28:12 -04003986 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003987 unsigned long num_pages;
3988 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003989 struct page *page;
3990 int pg_uptodate = 1;
3991
Chris Masonb4ce94d2009-02-04 09:25:08 -05003992 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003993 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003994
Chris Mason19b6caf2011-07-25 06:50:50 -04003995 if (eb_straddles_pages(eb)) {
3996 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3997 EXTENT_UPTODATE, 1, cached_state);
3998 if (ret)
3999 return ret;
4000 }
Chris Mason728131d2008-04-09 16:28:12 -04004001
4002 num_pages = num_extent_pages(eb->start, eb->len);
4003 for (i = 0; i < num_pages; i++) {
4004 page = extent_buffer_page(eb, i);
4005 if (!PageUptodate(page)) {
4006 pg_uptodate = 0;
4007 break;
4008 }
4009 }
Chris Mason42352982008-04-28 16:40:52 -04004010 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05004011}
Chris Masond1310b22008-01-24 16:13:08 -05004012
4013int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004014 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004015 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004016{
4017 unsigned long i;
4018 unsigned long start_i;
4019 struct page *page;
4020 int err;
4021 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004022 int locked_pages = 0;
4023 int all_uptodate = 1;
4024 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004025 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05004026 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004027 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004028
Chris Masonb4ce94d2009-02-04 09:25:08 -05004029 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004030 return 0;
4031
Chris Mason19b6caf2011-07-25 06:50:50 -04004032 if (eb_straddles_pages(eb)) {
4033 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4034 EXTENT_UPTODATE, 1, NULL)) {
4035 return 0;
4036 }
Chris Masond1310b22008-01-24 16:13:08 -05004037 }
4038
4039 if (start) {
4040 WARN_ON(start < eb->start);
4041 start_i = (start >> PAGE_CACHE_SHIFT) -
4042 (eb->start >> PAGE_CACHE_SHIFT);
4043 } else {
4044 start_i = 0;
4045 }
4046
4047 num_pages = num_extent_pages(eb->start, eb->len);
4048 for (i = start_i; i < num_pages; i++) {
4049 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004050 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004051 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004052 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004053 } else {
4054 lock_page(page);
4055 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004056 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05004057 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004058 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004059 }
4060 if (all_uptodate) {
4061 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004062 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004063 goto unlock_exit;
4064 }
4065
4066 for (i = start_i; i < num_pages; i++) {
4067 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05004068
4069 WARN_ON(!PagePrivate(page));
4070
4071 set_page_extent_mapped(page);
4072 if (i == 0)
4073 set_page_extent_head(page, eb->len);
4074
Chris Masonce9adaa2008-04-09 16:28:12 -04004075 if (inc_all_pages)
4076 page_cache_get(page);
4077 if (!PageUptodate(page)) {
4078 if (start_i == 0)
4079 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04004080 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004081 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004082 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004083 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004084 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004085 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004086 } else {
4087 unlock_page(page);
4088 }
4089 }
4090
Jeff Mahoney355808c2011-10-03 23:23:14 -04004091 if (bio) {
4092 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4093 BUG_ON(err < 0);
4094 }
Chris Masona86c12c2008-02-07 10:50:54 -05004095
Arne Jansenbb82ab82011-06-10 14:06:53 +02004096 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004097 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004098
Chris Masond1310b22008-01-24 16:13:08 -05004099 for (i = start_i; i < num_pages; i++) {
4100 page = extent_buffer_page(eb, i);
4101 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004102 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004103 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004104 }
Chris Masond3977122009-01-05 21:25:51 -05004105
Chris Masond1310b22008-01-24 16:13:08 -05004106 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004107 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004108 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004109
4110unlock_exit:
4111 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004112 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004113 page = extent_buffer_page(eb, i);
4114 i++;
4115 unlock_page(page);
4116 locked_pages--;
4117 }
4118 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004119}
Chris Masond1310b22008-01-24 16:13:08 -05004120
4121void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4122 unsigned long start,
4123 unsigned long len)
4124{
4125 size_t cur;
4126 size_t offset;
4127 struct page *page;
4128 char *kaddr;
4129 char *dst = (char *)dstv;
4130 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4131 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004132
4133 WARN_ON(start > eb->len);
4134 WARN_ON(start + len > eb->start + eb->len);
4135
4136 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4137
Chris Masond3977122009-01-05 21:25:51 -05004138 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004139 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004140
4141 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004142 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004143 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004144
4145 dst += cur;
4146 len -= cur;
4147 offset = 0;
4148 i++;
4149 }
4150}
Chris Masond1310b22008-01-24 16:13:08 -05004151
4152int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004153 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004154 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004155 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004156{
4157 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4158 char *kaddr;
4159 struct page *p;
4160 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4161 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4162 unsigned long end_i = (start_offset + start + min_len - 1) >>
4163 PAGE_CACHE_SHIFT;
4164
4165 if (i != end_i)
4166 return -EINVAL;
4167
4168 if (i == 0) {
4169 offset = start_offset;
4170 *map_start = 0;
4171 } else {
4172 offset = 0;
4173 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4174 }
Chris Masond3977122009-01-05 21:25:51 -05004175
Chris Masond1310b22008-01-24 16:13:08 -05004176 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004177 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4178 "wanted %lu %lu\n", (unsigned long long)eb->start,
4179 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004180 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004181 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004182 }
4183
4184 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004185 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004186 *map = kaddr + offset;
4187 *map_len = PAGE_CACHE_SIZE - offset;
4188 return 0;
4189}
Chris Masond1310b22008-01-24 16:13:08 -05004190
Chris Masond1310b22008-01-24 16:13:08 -05004191int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4192 unsigned long start,
4193 unsigned long len)
4194{
4195 size_t cur;
4196 size_t offset;
4197 struct page *page;
4198 char *kaddr;
4199 char *ptr = (char *)ptrv;
4200 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4201 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4202 int ret = 0;
4203
4204 WARN_ON(start > eb->len);
4205 WARN_ON(start + len > eb->start + eb->len);
4206
4207 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4208
Chris Masond3977122009-01-05 21:25:51 -05004209 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004210 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004211
4212 cur = min(len, (PAGE_CACHE_SIZE - offset));
4213
Chris Masona6591712011-07-19 12:04:14 -04004214 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004215 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004216 if (ret)
4217 break;
4218
4219 ptr += cur;
4220 len -= cur;
4221 offset = 0;
4222 i++;
4223 }
4224 return ret;
4225}
Chris Masond1310b22008-01-24 16:13:08 -05004226
4227void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4228 unsigned long start, unsigned long len)
4229{
4230 size_t cur;
4231 size_t offset;
4232 struct page *page;
4233 char *kaddr;
4234 char *src = (char *)srcv;
4235 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4236 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4237
4238 WARN_ON(start > eb->len);
4239 WARN_ON(start + len > eb->start + eb->len);
4240
4241 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4242
Chris Masond3977122009-01-05 21:25:51 -05004243 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004244 page = extent_buffer_page(eb, i);
4245 WARN_ON(!PageUptodate(page));
4246
4247 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004248 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004249 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004250
4251 src += cur;
4252 len -= cur;
4253 offset = 0;
4254 i++;
4255 }
4256}
Chris Masond1310b22008-01-24 16:13:08 -05004257
4258void memset_extent_buffer(struct extent_buffer *eb, char c,
4259 unsigned long start, unsigned long len)
4260{
4261 size_t cur;
4262 size_t offset;
4263 struct page *page;
4264 char *kaddr;
4265 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4266 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4267
4268 WARN_ON(start > eb->len);
4269 WARN_ON(start + len > eb->start + eb->len);
4270
4271 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4272
Chris Masond3977122009-01-05 21:25:51 -05004273 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004274 page = extent_buffer_page(eb, i);
4275 WARN_ON(!PageUptodate(page));
4276
4277 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004278 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004279 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004280
4281 len -= cur;
4282 offset = 0;
4283 i++;
4284 }
4285}
Chris Masond1310b22008-01-24 16:13:08 -05004286
4287void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4288 unsigned long dst_offset, unsigned long src_offset,
4289 unsigned long len)
4290{
4291 u64 dst_len = dst->len;
4292 size_t cur;
4293 size_t offset;
4294 struct page *page;
4295 char *kaddr;
4296 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4297 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4298
4299 WARN_ON(src->len != dst_len);
4300
4301 offset = (start_offset + dst_offset) &
4302 ((unsigned long)PAGE_CACHE_SIZE - 1);
4303
Chris Masond3977122009-01-05 21:25:51 -05004304 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004305 page = extent_buffer_page(dst, i);
4306 WARN_ON(!PageUptodate(page));
4307
4308 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4309
Chris Masona6591712011-07-19 12:04:14 -04004310 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004311 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004312
4313 src_offset += cur;
4314 len -= cur;
4315 offset = 0;
4316 i++;
4317 }
4318}
Chris Masond1310b22008-01-24 16:13:08 -05004319
4320static void move_pages(struct page *dst_page, struct page *src_page,
4321 unsigned long dst_off, unsigned long src_off,
4322 unsigned long len)
4323{
Chris Masona6591712011-07-19 12:04:14 -04004324 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004325 if (dst_page == src_page) {
4326 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4327 } else {
Chris Masona6591712011-07-19 12:04:14 -04004328 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004329 char *p = dst_kaddr + dst_off + len;
4330 char *s = src_kaddr + src_off + len;
4331
4332 while (len--)
4333 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004334 }
Chris Masond1310b22008-01-24 16:13:08 -05004335}
4336
Sergei Trofimovich33872062011-04-11 21:52:52 +00004337static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4338{
4339 unsigned long distance = (src > dst) ? src - dst : dst - src;
4340 return distance < len;
4341}
4342
Chris Masond1310b22008-01-24 16:13:08 -05004343static void copy_pages(struct page *dst_page, struct page *src_page,
4344 unsigned long dst_off, unsigned long src_off,
4345 unsigned long len)
4346{
Chris Masona6591712011-07-19 12:04:14 -04004347 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004348 char *src_kaddr;
4349
Sergei Trofimovich33872062011-04-11 21:52:52 +00004350 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004351 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004352 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004353 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004354 BUG_ON(areas_overlap(src_off, dst_off, len));
4355 }
Chris Masond1310b22008-01-24 16:13:08 -05004356
4357 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004358}
4359
4360void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4361 unsigned long src_offset, unsigned long len)
4362{
4363 size_t cur;
4364 size_t dst_off_in_page;
4365 size_t src_off_in_page;
4366 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4367 unsigned long dst_i;
4368 unsigned long src_i;
4369
4370 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004371 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4372 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004373 BUG_ON(1);
4374 }
4375 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004376 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4377 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004378 BUG_ON(1);
4379 }
4380
Chris Masond3977122009-01-05 21:25:51 -05004381 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004382 dst_off_in_page = (start_offset + dst_offset) &
4383 ((unsigned long)PAGE_CACHE_SIZE - 1);
4384 src_off_in_page = (start_offset + src_offset) &
4385 ((unsigned long)PAGE_CACHE_SIZE - 1);
4386
4387 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4388 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4389
4390 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4391 src_off_in_page));
4392 cur = min_t(unsigned long, cur,
4393 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4394
4395 copy_pages(extent_buffer_page(dst, dst_i),
4396 extent_buffer_page(dst, src_i),
4397 dst_off_in_page, src_off_in_page, cur);
4398
4399 src_offset += cur;
4400 dst_offset += cur;
4401 len -= cur;
4402 }
4403}
Chris Masond1310b22008-01-24 16:13:08 -05004404
4405void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4406 unsigned long src_offset, unsigned long len)
4407{
4408 size_t cur;
4409 size_t dst_off_in_page;
4410 size_t src_off_in_page;
4411 unsigned long dst_end = dst_offset + len - 1;
4412 unsigned long src_end = src_offset + len - 1;
4413 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4414 unsigned long dst_i;
4415 unsigned long src_i;
4416
4417 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004418 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4419 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004420 BUG_ON(1);
4421 }
4422 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004423 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4424 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004425 BUG_ON(1);
4426 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004427 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004428 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4429 return;
4430 }
Chris Masond3977122009-01-05 21:25:51 -05004431 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004432 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4433 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4434
4435 dst_off_in_page = (start_offset + dst_end) &
4436 ((unsigned long)PAGE_CACHE_SIZE - 1);
4437 src_off_in_page = (start_offset + src_end) &
4438 ((unsigned long)PAGE_CACHE_SIZE - 1);
4439
4440 cur = min_t(unsigned long, len, src_off_in_page + 1);
4441 cur = min(cur, dst_off_in_page + 1);
4442 move_pages(extent_buffer_page(dst, dst_i),
4443 extent_buffer_page(dst, src_i),
4444 dst_off_in_page - cur + 1,
4445 src_off_in_page - cur + 1, cur);
4446
4447 dst_end -= cur;
4448 src_end -= cur;
4449 len -= cur;
4450 }
4451}
Chris Mason6af118ce2008-07-22 11:18:07 -04004452
Miao Xie19fe0a82010-10-26 20:57:29 -04004453static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4454{
4455 struct extent_buffer *eb =
4456 container_of(head, struct extent_buffer, rcu_head);
4457
4458 btrfs_release_extent_buffer(eb);
4459}
4460
Chris Mason6af118ce2008-07-22 11:18:07 -04004461int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4462{
4463 u64 start = page_offset(page);
4464 struct extent_buffer *eb;
4465 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004466
4467 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004468 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004469 if (!eb) {
4470 spin_unlock(&tree->buffer_lock);
4471 return ret;
4472 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004473
Chris Masonb9473432009-03-13 11:00:37 -04004474 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4475 ret = 0;
4476 goto out;
4477 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004478
Miao Xie19fe0a82010-10-26 20:57:29 -04004479 /*
4480 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4481 * Or go back.
4482 */
4483 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4484 ret = 0;
4485 goto out;
4486 }
4487
4488 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004489out:
4490 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004491
4492 /* at this point we can safely release the extent buffer */
4493 if (atomic_read(&eb->refs) == 0)
4494 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004495 return ret;
4496}