blob: 89ba79fb945c3aeecb8a5e6dff0c85ee4497d6c9 [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
56int __init extent_io_init(void)
57{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020058 extent_state_cache = kmem_cache_create("extent_state",
59 sizeof(struct extent_state), 0,
60 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050061 if (!extent_state_cache)
62 return -ENOMEM;
63
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020064 extent_buffer_cache = kmem_cache_create("extent_buffers",
65 sizeof(struct extent_buffer), 0,
66 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050067 if (!extent_buffer_cache)
68 goto free_state_cache;
69 return 0;
70
71free_state_cache:
72 kmem_cache_destroy(extent_state_cache);
73 return -ENOMEM;
74}
75
76void extent_io_exit(void)
77{
78 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040079 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050080
81 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040082 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050083 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
84 "state %lu in tree %p refs %d\n",
85 (unsigned long long)state->start,
86 (unsigned long long)state->end,
87 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040088 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050089 kmem_cache_free(extent_state_cache, state);
90
91 }
92
Chris Mason2d2ae542008-03-26 16:24:23 -040093 while (!list_empty(&buffers)) {
94 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050095 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
96 "refs %d\n", (unsigned long long)eb->start,
97 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040098 list_del(&eb->leak_list);
99 kmem_cache_free(extent_buffer_cache, eb);
100 }
Chris Masond1310b22008-01-24 16:13:08 -0500101 if (extent_state_cache)
102 kmem_cache_destroy(extent_state_cache);
103 if (extent_buffer_cache)
104 kmem_cache_destroy(extent_buffer_cache);
105}
106
107void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200108 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500109{
Eric Paris6bef4d32010-02-23 19:43:04 +0000110 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400111 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->ops = NULL;
113 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500114 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400115 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500116 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500117}
Chris Masond1310b22008-01-24 16:13:08 -0500118
Christoph Hellwigb2950862008-12-02 09:54:17 -0500119static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500120{
121 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500122#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400123 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400124#endif
Chris Masond1310b22008-01-24 16:13:08 -0500125
126 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400127 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500128 return state;
129 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500130 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500131 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500132#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400133 spin_lock_irqsave(&leak_lock, flags);
134 list_add(&state->leak_list, &states);
135 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400136#endif
Chris Masond1310b22008-01-24 16:13:08 -0500137 atomic_set(&state->refs, 1);
138 init_waitqueue_head(&state->wq);
139 return state;
140}
Chris Masond1310b22008-01-24 16:13:08 -0500141
Chris Mason4845e442010-05-25 20:56:50 -0400142void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500143{
Chris Masond1310b22008-01-24 16:13:08 -0500144 if (!state)
145 return;
146 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400149#endif
Chris Mason70dec802008-01-29 09:59:12 -0500150 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500151#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400152 spin_lock_irqsave(&leak_lock, flags);
153 list_del(&state->leak_list);
154 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400155#endif
Chris Masond1310b22008-01-24 16:13:08 -0500156 kmem_cache_free(extent_state_cache, state);
157 }
158}
Chris Masond1310b22008-01-24 16:13:08 -0500159
160static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
161 struct rb_node *node)
162{
Chris Masond3977122009-01-05 21:25:51 -0500163 struct rb_node **p = &root->rb_node;
164 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500165 struct tree_entry *entry;
166
Chris Masond3977122009-01-05 21:25:51 -0500167 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500168 parent = *p;
169 entry = rb_entry(parent, struct tree_entry, rb_node);
170
171 if (offset < entry->start)
172 p = &(*p)->rb_left;
173 else if (offset > entry->end)
174 p = &(*p)->rb_right;
175 else
176 return parent;
177 }
178
179 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500180 rb_link_node(node, parent, p);
181 rb_insert_color(node, root);
182 return NULL;
183}
184
Chris Mason80ea96b2008-02-01 14:51:59 -0500185static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500186 struct rb_node **prev_ret,
187 struct rb_node **next_ret)
188{
Chris Mason80ea96b2008-02-01 14:51:59 -0500189 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500190 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500191 struct rb_node *prev = NULL;
192 struct rb_node *orig_prev = NULL;
193 struct tree_entry *entry;
194 struct tree_entry *prev_entry = NULL;
195
Chris Masond3977122009-01-05 21:25:51 -0500196 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500197 entry = rb_entry(n, struct tree_entry, rb_node);
198 prev = n;
199 prev_entry = entry;
200
201 if (offset < entry->start)
202 n = n->rb_left;
203 else if (offset > entry->end)
204 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500205 else
Chris Masond1310b22008-01-24 16:13:08 -0500206 return n;
207 }
208
209 if (prev_ret) {
210 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500211 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500212 prev = rb_next(prev);
213 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
214 }
215 *prev_ret = prev;
216 prev = orig_prev;
217 }
218
219 if (next_ret) {
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500221 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500222 prev = rb_prev(prev);
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
224 }
225 *next_ret = prev;
226 }
227 return NULL;
228}
229
Chris Mason80ea96b2008-02-01 14:51:59 -0500230static inline struct rb_node *tree_search(struct extent_io_tree *tree,
231 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500232{
Chris Mason70dec802008-01-29 09:59:12 -0500233 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500234 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500235
Chris Mason80ea96b2008-02-01 14:51:59 -0500236 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500237 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500238 return prev;
239 return ret;
240}
241
Josef Bacik9ed74f22009-09-11 16:12:44 -0400242static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
243 struct extent_state *other)
244{
245 if (tree->ops && tree->ops->merge_extent_hook)
246 tree->ops->merge_extent_hook(tree->mapping->host, new,
247 other);
248}
249
Chris Masond1310b22008-01-24 16:13:08 -0500250/*
251 * utility function to look for merge candidates inside a given range.
252 * Any extents with matching state are merged together into a single
253 * extent in the tree. Extents with EXTENT_IO in their state field
254 * are not merged because the end_io handlers need to be able to do
255 * operations on them without sleeping (or doing allocations/splits).
256 *
257 * This should be called with the tree lock held.
258 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000259static void merge_state(struct extent_io_tree *tree,
260 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500261{
262 struct extent_state *other;
263 struct rb_node *other_node;
264
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400265 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000266 return;
Chris Masond1310b22008-01-24 16:13:08 -0500267
268 other_node = rb_prev(&state->rb_node);
269 if (other_node) {
270 other = rb_entry(other_node, struct extent_state, rb_node);
271 if (other->end == state->start - 1 &&
272 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400273 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500274 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500275 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500276 rb_erase(&other->rb_node, &tree->state);
277 free_extent_state(other);
278 }
279 }
280 other_node = rb_next(&state->rb_node);
281 if (other_node) {
282 other = rb_entry(other_node, struct extent_state, rb_node);
283 if (other->start == state->end + 1 &&
284 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400285 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400286 state->end = other->end;
287 other->tree = NULL;
288 rb_erase(&other->rb_node, &tree->state);
289 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500290 }
291 }
Chris Masond1310b22008-01-24 16:13:08 -0500292}
293
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000294static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400295 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500296{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000297 if (tree->ops && tree->ops->set_bit_hook)
298 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500299}
300
301static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500303{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400304 if (tree->ops && tree->ops->clear_bit_hook)
305 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500306}
307
Xiao Guangrong3150b692011-07-14 03:19:08 +0000308static void set_state_bits(struct extent_io_tree *tree,
309 struct extent_state *state, int *bits);
310
Chris Masond1310b22008-01-24 16:13:08 -0500311/*
312 * insert an extent_state struct into the tree. 'bits' are set on the
313 * struct before it is inserted.
314 *
315 * This may return -EEXIST if the extent is already there, in which case the
316 * state struct is freed.
317 *
318 * The tree lock is not taken internally. This is a utility function and
319 * probably isn't what you want to call (see set/clear_extent_bit).
320 */
321static int insert_state(struct extent_io_tree *tree,
322 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400323 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500324{
325 struct rb_node *node;
326
327 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500328 printk(KERN_ERR "btrfs end < start %llu %llu\n",
329 (unsigned long long)end,
330 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500331 WARN_ON(1);
332 }
Chris Masond1310b22008-01-24 16:13:08 -0500333 state->start = start;
334 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400335
Xiao Guangrong3150b692011-07-14 03:19:08 +0000336 set_state_bits(tree, state, bits);
337
Chris Masond1310b22008-01-24 16:13:08 -0500338 node = tree_insert(&tree->state, end, &state->rb_node);
339 if (node) {
340 struct extent_state *found;
341 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500342 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
343 "%llu %llu\n", (unsigned long long)found->start,
344 (unsigned long long)found->end,
345 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500346 return -EEXIST;
347 }
Chris Mason70dec802008-01-29 09:59:12 -0500348 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500349 merge_state(tree, state);
350 return 0;
351}
352
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000353static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400354 u64 split)
355{
356 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000357 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400358}
359
Chris Masond1310b22008-01-24 16:13:08 -0500360/*
361 * split a given extent state struct in two, inserting the preallocated
362 * struct 'prealloc' as the newly created second half. 'split' indicates an
363 * offset inside 'orig' where it should be split.
364 *
365 * Before calling,
366 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
367 * are two extent state structs in the tree:
368 * prealloc: [orig->start, split - 1]
369 * orig: [ split, orig->end ]
370 *
371 * The tree locks are not taken by this function. They need to be held
372 * by the caller.
373 */
374static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
375 struct extent_state *prealloc, u64 split)
376{
377 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400378
379 split_cb(tree, orig, split);
380
Chris Masond1310b22008-01-24 16:13:08 -0500381 prealloc->start = orig->start;
382 prealloc->end = split - 1;
383 prealloc->state = orig->state;
384 orig->start = split;
385
386 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
387 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500388 free_extent_state(prealloc);
389 return -EEXIST;
390 }
Chris Mason70dec802008-01-29 09:59:12 -0500391 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500392 return 0;
393}
394
395/*
396 * utility function to clear some bits in an extent state struct.
397 * it will optionally wake up any one waiting on this state (wake == 1), or
398 * forcibly remove the state from the tree (delete == 1).
399 *
400 * If no bits are set on the state struct after clearing things, the
401 * struct is freed and removed from the tree
402 */
403static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400404 struct extent_state *state,
405 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500406{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400407 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400408 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500409
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400410 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500411 u64 range = state->end - state->start + 1;
412 WARN_ON(range > tree->dirty_bytes);
413 tree->dirty_bytes -= range;
414 }
Chris Mason291d6732008-01-29 15:55:23 -0500415 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400416 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500417 if (wake)
418 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400419 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500420 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500421 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500422 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500423 free_extent_state(state);
424 } else {
425 WARN_ON(1);
426 }
427 } else {
428 merge_state(tree, state);
429 }
430 return ret;
431}
432
Xiao Guangrong82337672011-04-20 06:44:57 +0000433static struct extent_state *
434alloc_extent_state_atomic(struct extent_state *prealloc)
435{
436 if (!prealloc)
437 prealloc = alloc_extent_state(GFP_ATOMIC);
438
439 return prealloc;
440}
441
Chris Masond1310b22008-01-24 16:13:08 -0500442/*
443 * clear some bits on a range in the tree. This may require splitting
444 * or inserting elements in the tree, so the gfp mask is used to
445 * indicate which allocations or sleeping are allowed.
446 *
447 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
448 * the given range from the tree regardless of state (ie for truncate).
449 *
450 * the range [start, end] is inclusive.
451 *
452 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
453 * bits were already set, or zero if none of the bits were already set.
454 */
455int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400456 int bits, int wake, int delete,
457 struct extent_state **cached_state,
458 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500459{
460 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400461 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500462 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400463 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500464 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400465 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500466 int err;
467 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000468 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500469
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400470 if (delete)
471 bits |= ~EXTENT_CTLBITS;
472 bits |= EXTENT_FIRST_DELALLOC;
473
Josef Bacik2ac55d42010-02-03 19:33:23 +0000474 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
475 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500476again:
477 if (!prealloc && (mask & __GFP_WAIT)) {
478 prealloc = alloc_extent_state(mask);
479 if (!prealloc)
480 return -ENOMEM;
481 }
482
Chris Masoncad321a2008-12-17 14:51:42 -0500483 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400484 if (cached_state) {
485 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000486
487 if (clear) {
488 *cached_state = NULL;
489 cached_state = NULL;
490 }
491
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400492 if (cached && cached->tree && cached->start <= start &&
493 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000494 if (clear)
495 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400496 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400497 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400498 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000499 if (clear)
500 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400501 }
Chris Masond1310b22008-01-24 16:13:08 -0500502 /*
503 * this search will find the extents that end after
504 * our range starts
505 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500506 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500507 if (!node)
508 goto out;
509 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400510hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500511 if (state->start > end)
512 goto out;
513 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400514 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500515
516 /*
517 * | ---- desired range ---- |
518 * | state | or
519 * | ------------- state -------------- |
520 *
521 * We need to split the extent we found, and may flip
522 * bits on second half.
523 *
524 * If the extent we found extends past our range, we
525 * just split and search again. It'll get split again
526 * the next time though.
527 *
528 * If the extent we found is inside our range, we clear
529 * the desired bit on it.
530 */
531
532 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000533 prealloc = alloc_extent_state_atomic(prealloc);
534 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500535 err = split_state(tree, state, prealloc, start);
536 BUG_ON(err == -EEXIST);
537 prealloc = NULL;
538 if (err)
539 goto out;
540 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400541 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400542 if (last_end == (u64)-1)
543 goto out;
544 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500545 }
546 goto search_again;
547 }
548 /*
549 * | ---- desired range ---- |
550 * | state |
551 * We need to split the extent, and clear the bit
552 * on the first half
553 */
554 if (state->start <= end && state->end > end) {
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, end + 1);
558 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500559 if (wake)
560 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400561
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400562 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400563
Chris Masond1310b22008-01-24 16:13:08 -0500564 prealloc = NULL;
565 goto out;
566 }
Chris Mason42daec22009-09-23 19:51:09 -0400567
Chris Mason2c64c532009-09-02 15:04:12 -0400568 if (state->end < end && prealloc && !need_resched())
569 next_node = rb_next(&state->rb_node);
570 else
571 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400572
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400573 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400574 if (last_end == (u64)-1)
575 goto out;
576 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400577 if (start <= end && next_node) {
578 state = rb_entry(next_node, struct extent_state,
579 rb_node);
580 if (state->start == start)
581 goto hit_next;
582 }
Chris Masond1310b22008-01-24 16:13:08 -0500583 goto search_again;
584
585out:
Chris Masoncad321a2008-12-17 14:51:42 -0500586 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500587 if (prealloc)
588 free_extent_state(prealloc);
589
590 return set;
591
592search_again:
593 if (start > end)
594 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500595 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500596 if (mask & __GFP_WAIT)
597 cond_resched();
598 goto again;
599}
Chris Masond1310b22008-01-24 16:13:08 -0500600
601static int wait_on_state(struct extent_io_tree *tree,
602 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500603 __releases(tree->lock)
604 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500605{
606 DEFINE_WAIT(wait);
607 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500608 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500609 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500610 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500611 finish_wait(&state->wq, &wait);
612 return 0;
613}
614
615/*
616 * waits for one or more bits to clear on a range in the state tree.
617 * The range [start, end] is inclusive.
618 * The tree lock is taken by this function
619 */
620int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
621{
622 struct extent_state *state;
623 struct rb_node *node;
624
Chris Masoncad321a2008-12-17 14:51:42 -0500625 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500626again:
627 while (1) {
628 /*
629 * this search will find all the extents that end after
630 * our range starts
631 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500632 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500633 if (!node)
634 break;
635
636 state = rb_entry(node, struct extent_state, rb_node);
637
638 if (state->start > end)
639 goto out;
640
641 if (state->state & bits) {
642 start = state->start;
643 atomic_inc(&state->refs);
644 wait_on_state(tree, state);
645 free_extent_state(state);
646 goto again;
647 }
648 start = state->end + 1;
649
650 if (start > end)
651 break;
652
Xiao Guangrongded91f02011-07-14 03:19:27 +0000653 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654 }
655out:
Chris Masoncad321a2008-12-17 14:51:42 -0500656 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500657 return 0;
658}
Chris Masond1310b22008-01-24 16:13:08 -0500659
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000660static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500661 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400662 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500663{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400664 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400665
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000666 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400667 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500668 u64 range = state->end - state->start + 1;
669 tree->dirty_bytes += range;
670 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400671 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500672}
673
Chris Mason2c64c532009-09-02 15:04:12 -0400674static void cache_state(struct extent_state *state,
675 struct extent_state **cached_ptr)
676{
677 if (cached_ptr && !(*cached_ptr)) {
678 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
679 *cached_ptr = state;
680 atomic_inc(&state->refs);
681 }
682 }
683}
684
Arne Jansen507903b2011-04-06 10:02:20 +0000685static void uncache_state(struct extent_state **cached_ptr)
686{
687 if (cached_ptr && (*cached_ptr)) {
688 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400689 *cached_ptr = NULL;
690 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000691 }
692}
693
Chris Masond1310b22008-01-24 16:13:08 -0500694/*
Chris Mason1edbb732009-09-02 13:24:36 -0400695 * set some bits on a range in the tree. This may require allocations or
696 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500697 *
Chris Mason1edbb732009-09-02 13:24:36 -0400698 * If any of the exclusive bits are set, this will fail with -EEXIST if some
699 * part of the range already has the desired bits set. The start of the
700 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500701 *
Chris Mason1edbb732009-09-02 13:24:36 -0400702 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500703 */
Chris Mason1edbb732009-09-02 13:24:36 -0400704
Chris Mason4845e442010-05-25 20:56:50 -0400705int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
706 int bits, int exclusive_bits, u64 *failed_start,
707 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500708{
709 struct extent_state *state;
710 struct extent_state *prealloc = NULL;
711 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500712 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500713 u64 last_start;
714 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400715
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400716 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500717again:
718 if (!prealloc && (mask & __GFP_WAIT)) {
719 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000720 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500721 }
722
Chris Masoncad321a2008-12-17 14:51:42 -0500723 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400724 if (cached_state && *cached_state) {
725 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400726 if (state->start <= start && state->end > start &&
727 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400728 node = &state->rb_node;
729 goto hit_next;
730 }
731 }
Chris Masond1310b22008-01-24 16:13:08 -0500732 /*
733 * this search will find all the extents that end after
734 * our range starts.
735 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500736 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500737 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000738 prealloc = alloc_extent_state_atomic(prealloc);
739 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400740 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500741 prealloc = NULL;
742 BUG_ON(err == -EEXIST);
743 goto out;
744 }
Chris Masond1310b22008-01-24 16:13:08 -0500745 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400746hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500747 last_start = state->start;
748 last_end = state->end;
749
750 /*
751 * | ---- desired range ---- |
752 * | state |
753 *
754 * Just lock what we found and keep going
755 */
756 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400757 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400758 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500759 *failed_start = state->start;
760 err = -EEXIST;
761 goto out;
762 }
Chris Mason42daec22009-09-23 19:51:09 -0400763
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000764 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400765
Chris Mason2c64c532009-09-02 15:04:12 -0400766 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500767 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400768 if (last_end == (u64)-1)
769 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400770
Yan Zheng5c939df2009-05-27 09:16:03 -0400771 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400772 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000773 if (next_node && start < end && prealloc && !need_resched()) {
774 state = rb_entry(next_node, struct extent_state,
775 rb_node);
776 if (state->start == start)
777 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400778 }
Chris Masond1310b22008-01-24 16:13:08 -0500779 goto search_again;
780 }
781
782 /*
783 * | ---- desired range ---- |
784 * | state |
785 * or
786 * | ------------- state -------------- |
787 *
788 * We need to split the extent we found, and may flip bits on
789 * second half.
790 *
791 * If the extent we found extends past our
792 * range, we just split and search again. It'll get split
793 * again the next time though.
794 *
795 * If the extent we found is inside our range, we set the
796 * desired bit on it.
797 */
798 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400799 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500800 *failed_start = start;
801 err = -EEXIST;
802 goto out;
803 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000804
805 prealloc = alloc_extent_state_atomic(prealloc);
806 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500807 err = split_state(tree, state, prealloc, start);
808 BUG_ON(err == -EEXIST);
809 prealloc = NULL;
810 if (err)
811 goto out;
812 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000813 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400814 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500815 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400816 if (last_end == (u64)-1)
817 goto out;
818 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500819 }
820 goto search_again;
821 }
822 /*
823 * | ---- desired range ---- |
824 * | state | or | state |
825 *
826 * There's a hole, we need to insert something in it and
827 * ignore the extent we found.
828 */
829 if (state->start > start) {
830 u64 this_end;
831 if (end < last_start)
832 this_end = end;
833 else
Chris Masond3977122009-01-05 21:25:51 -0500834 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000835
836 prealloc = alloc_extent_state_atomic(prealloc);
837 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000838
839 /*
840 * Avoid to free 'prealloc' if it can be merged with
841 * the later extent.
842 */
Chris Masond1310b22008-01-24 16:13:08 -0500843 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400844 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400845 BUG_ON(err == -EEXIST);
846 if (err) {
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000847 free_extent_state(prealloc);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400848 prealloc = NULL;
849 goto out;
850 }
Chris Mason2c64c532009-09-02 15:04:12 -0400851 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500852 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500853 start = this_end + 1;
854 goto search_again;
855 }
856 /*
857 * | ---- desired range ---- |
858 * | state |
859 * We need to split the extent, and set the bit
860 * on the first half
861 */
862 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400863 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500864 *failed_start = start;
865 err = -EEXIST;
866 goto out;
867 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000868
869 prealloc = alloc_extent_state_atomic(prealloc);
870 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500871 err = split_state(tree, state, prealloc, end + 1);
872 BUG_ON(err == -EEXIST);
873
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000874 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400875 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500876 merge_state(tree, prealloc);
877 prealloc = NULL;
878 goto out;
879 }
880
881 goto search_again;
882
883out:
Chris Masoncad321a2008-12-17 14:51:42 -0500884 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500885 if (prealloc)
886 free_extent_state(prealloc);
887
888 return err;
889
890search_again:
891 if (start > end)
892 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500893 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500894 if (mask & __GFP_WAIT)
895 cond_resched();
896 goto again;
897}
Chris Masond1310b22008-01-24 16:13:08 -0500898
Josef Bacik462d6fa2011-09-26 13:56:12 -0400899/**
900 * convert_extent - convert all bits in a given range from one bit to another
901 * @tree: the io tree to search
902 * @start: the start offset in bytes
903 * @end: the end offset in bytes (inclusive)
904 * @bits: the bits to set in this range
905 * @clear_bits: the bits to clear in this range
906 * @mask: the allocation mask
907 *
908 * This will go through and set bits for the given range. If any states exist
909 * already in this range they are set with the given bit and cleared of the
910 * clear_bits. This is only meant to be used by things that are mergeable, ie
911 * converting from say DELALLOC to DIRTY. This is not meant to be used with
912 * boundary bits like LOCK.
913 */
914int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
915 int bits, int clear_bits, gfp_t mask)
916{
917 struct extent_state *state;
918 struct extent_state *prealloc = NULL;
919 struct rb_node *node;
920 int err = 0;
921 u64 last_start;
922 u64 last_end;
923
924again:
925 if (!prealloc && (mask & __GFP_WAIT)) {
926 prealloc = alloc_extent_state(mask);
927 if (!prealloc)
928 return -ENOMEM;
929 }
930
931 spin_lock(&tree->lock);
932 /*
933 * this search will find all the extents that end after
934 * our range starts.
935 */
936 node = tree_search(tree, start);
937 if (!node) {
938 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500939 if (!prealloc) {
940 err = -ENOMEM;
941 goto out;
942 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400943 err = insert_state(tree, prealloc, start, end, &bits);
944 prealloc = NULL;
945 BUG_ON(err == -EEXIST);
946 goto out;
947 }
948 state = rb_entry(node, struct extent_state, rb_node);
949hit_next:
950 last_start = state->start;
951 last_end = state->end;
952
953 /*
954 * | ---- desired range ---- |
955 * | state |
956 *
957 * Just lock what we found and keep going
958 */
959 if (state->start == start && state->end <= end) {
960 struct rb_node *next_node;
961
962 set_state_bits(tree, state, &bits);
963 clear_state_bit(tree, state, &clear_bits, 0);
964
965 merge_state(tree, state);
966 if (last_end == (u64)-1)
967 goto out;
968
969 start = last_end + 1;
970 next_node = rb_next(&state->rb_node);
971 if (next_node && start < end && prealloc && !need_resched()) {
972 state = rb_entry(next_node, struct extent_state,
973 rb_node);
974 if (state->start == start)
975 goto hit_next;
976 }
977 goto search_again;
978 }
979
980 /*
981 * | ---- desired range ---- |
982 * | state |
983 * or
984 * | ------------- state -------------- |
985 *
986 * We need to split the extent we found, and may flip bits on
987 * second half.
988 *
989 * If the extent we found extends past our
990 * range, we just split and search again. It'll get split
991 * again the next time though.
992 *
993 * If the extent we found is inside our range, we set the
994 * desired bit on it.
995 */
996 if (state->start < start) {
997 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500998 if (!prealloc) {
999 err = -ENOMEM;
1000 goto out;
1001 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001002 err = split_state(tree, state, prealloc, start);
1003 BUG_ON(err == -EEXIST);
1004 prealloc = NULL;
1005 if (err)
1006 goto out;
1007 if (state->end <= end) {
1008 set_state_bits(tree, state, &bits);
1009 clear_state_bit(tree, state, &clear_bits, 0);
1010 merge_state(tree, state);
1011 if (last_end == (u64)-1)
1012 goto out;
1013 start = last_end + 1;
1014 }
1015 goto search_again;
1016 }
1017 /*
1018 * | ---- desired range ---- |
1019 * | state | or | state |
1020 *
1021 * There's a hole, we need to insert something in it and
1022 * ignore the extent we found.
1023 */
1024 if (state->start > start) {
1025 u64 this_end;
1026 if (end < last_start)
1027 this_end = end;
1028 else
1029 this_end = last_start - 1;
1030
1031 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001032 if (!prealloc) {
1033 err = -ENOMEM;
1034 goto out;
1035 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001036
1037 /*
1038 * Avoid to free 'prealloc' if it can be merged with
1039 * the later extent.
1040 */
1041 err = insert_state(tree, prealloc, start, this_end,
1042 &bits);
1043 BUG_ON(err == -EEXIST);
1044 if (err) {
1045 free_extent_state(prealloc);
1046 prealloc = NULL;
1047 goto out;
1048 }
1049 prealloc = NULL;
1050 start = this_end + 1;
1051 goto search_again;
1052 }
1053 /*
1054 * | ---- desired range ---- |
1055 * | state |
1056 * We need to split the extent, and set the bit
1057 * on the first half
1058 */
1059 if (state->start <= end && state->end > end) {
1060 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001061 if (!prealloc) {
1062 err = -ENOMEM;
1063 goto out;
1064 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001065
1066 err = split_state(tree, state, prealloc, end + 1);
1067 BUG_ON(err == -EEXIST);
1068
1069 set_state_bits(tree, prealloc, &bits);
1070 clear_state_bit(tree, prealloc, &clear_bits, 0);
1071
1072 merge_state(tree, prealloc);
1073 prealloc = NULL;
1074 goto out;
1075 }
1076
1077 goto search_again;
1078
1079out:
1080 spin_unlock(&tree->lock);
1081 if (prealloc)
1082 free_extent_state(prealloc);
1083
1084 return err;
1085
1086search_again:
1087 if (start > end)
1088 goto out;
1089 spin_unlock(&tree->lock);
1090 if (mask & __GFP_WAIT)
1091 cond_resched();
1092 goto again;
1093}
1094
Chris Masond1310b22008-01-24 16:13:08 -05001095/* wrappers around set/clear extent bit */
1096int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1097 gfp_t mask)
1098{
1099 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001100 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001101}
Chris Masond1310b22008-01-24 16:13:08 -05001102
1103int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1104 int bits, gfp_t mask)
1105{
1106 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001107 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001108}
Chris Masond1310b22008-01-24 16:13:08 -05001109
1110int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1111 int bits, gfp_t mask)
1112{
Chris Mason2c64c532009-09-02 15:04:12 -04001113 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001114}
Chris Masond1310b22008-01-24 16:13:08 -05001115
1116int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001117 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001118{
1119 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001120 EXTENT_DELALLOC | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001121 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001122}
Chris Masond1310b22008-01-24 16:13:08 -05001123
1124int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1125 gfp_t mask)
1126{
1127 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001128 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001129 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001130}
Chris Masond1310b22008-01-24 16:13:08 -05001131
1132int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1133 gfp_t mask)
1134{
1135 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001136 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001137}
Chris Masond1310b22008-01-24 16:13:08 -05001138
Chris Masond1310b22008-01-24 16:13:08 -05001139int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001140 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001141{
Arne Jansen507903b2011-04-06 10:02:20 +00001142 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1143 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001144}
Chris Masond1310b22008-01-24 16:13:08 -05001145
Chris Masond3977122009-01-05 21:25:51 -05001146static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001147 u64 end, struct extent_state **cached_state,
1148 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001149{
Chris Mason2c64c532009-09-02 15:04:12 -04001150 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001151 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001152}
Chris Masond1310b22008-01-24 16:13:08 -05001153
Chris Masond352ac62008-09-29 15:18:18 -04001154/*
1155 * either insert or lock state struct between start and end use mask to tell
1156 * us if waiting is desired.
1157 */
Chris Mason1edbb732009-09-02 13:24:36 -04001158int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001159 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001160{
1161 int err;
1162 u64 failed_start;
1163 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001164 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001165 EXTENT_LOCKED, &failed_start,
1166 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001167 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1168 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1169 start = failed_start;
1170 } else {
1171 break;
1172 }
1173 WARN_ON(start > end);
1174 }
1175 return err;
1176}
Chris Masond1310b22008-01-24 16:13:08 -05001177
Chris Mason1edbb732009-09-02 13:24:36 -04001178int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1179{
Chris Mason2c64c532009-09-02 15:04:12 -04001180 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001181}
1182
Josef Bacik25179202008-10-29 14:49:05 -04001183int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1184 gfp_t mask)
1185{
1186 int err;
1187 u64 failed_start;
1188
Chris Mason2c64c532009-09-02 15:04:12 -04001189 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1190 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001191 if (err == -EEXIST) {
1192 if (failed_start > start)
1193 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001194 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001195 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001196 }
Josef Bacik25179202008-10-29 14:49:05 -04001197 return 1;
1198}
Josef Bacik25179202008-10-29 14:49:05 -04001199
Chris Mason2c64c532009-09-02 15:04:12 -04001200int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1201 struct extent_state **cached, gfp_t mask)
1202{
1203 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1204 mask);
1205}
1206
Arne Jansen507903b2011-04-06 10:02:20 +00001207int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001208{
Chris Mason2c64c532009-09-02 15:04:12 -04001209 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1210 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001211}
Chris Masond1310b22008-01-24 16:13:08 -05001212
1213/*
Chris Masond1310b22008-01-24 16:13:08 -05001214 * helper function to set both pages and extents in the tree writeback
1215 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001216static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001217{
1218 unsigned long index = start >> PAGE_CACHE_SHIFT;
1219 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1220 struct page *page;
1221
1222 while (index <= end_index) {
1223 page = find_get_page(tree->mapping, index);
1224 BUG_ON(!page);
1225 set_page_writeback(page);
1226 page_cache_release(page);
1227 index++;
1228 }
Chris Masond1310b22008-01-24 16:13:08 -05001229 return 0;
1230}
Chris Masond1310b22008-01-24 16:13:08 -05001231
Chris Masond352ac62008-09-29 15:18:18 -04001232/* find the first state struct with 'bits' set after 'start', and
1233 * return it. tree->lock must be held. NULL will returned if
1234 * nothing was found after 'start'
1235 */
Chris Masond7fc6402008-02-18 12:12:38 -05001236struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1237 u64 start, int bits)
1238{
1239 struct rb_node *node;
1240 struct extent_state *state;
1241
1242 /*
1243 * this search will find all the extents that end after
1244 * our range starts.
1245 */
1246 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001247 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001248 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001249
Chris Masond3977122009-01-05 21:25:51 -05001250 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001251 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001252 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001253 return state;
Chris Masond3977122009-01-05 21:25:51 -05001254
Chris Masond7fc6402008-02-18 12:12:38 -05001255 node = rb_next(node);
1256 if (!node)
1257 break;
1258 }
1259out:
1260 return NULL;
1261}
Chris Masond7fc6402008-02-18 12:12:38 -05001262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001264 * find the first offset in the io tree with 'bits' set. zero is
1265 * returned if we find something, and *start_ret and *end_ret are
1266 * set to reflect the state struct that was found.
1267 *
1268 * If nothing was found, 1 is returned, < 0 on error
1269 */
1270int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1271 u64 *start_ret, u64 *end_ret, int bits)
1272{
1273 struct extent_state *state;
1274 int ret = 1;
1275
1276 spin_lock(&tree->lock);
1277 state = find_first_extent_bit_state(tree, start, bits);
1278 if (state) {
1279 *start_ret = state->start;
1280 *end_ret = state->end;
1281 ret = 0;
1282 }
1283 spin_unlock(&tree->lock);
1284 return ret;
1285}
1286
1287/*
Chris Masond352ac62008-09-29 15:18:18 -04001288 * find a contiguous range of bytes in the file marked as delalloc, not
1289 * more than 'max_bytes'. start and end are used to return the range,
1290 *
1291 * 1 is returned if we find something, 0 if nothing was in the tree
1292 */
Chris Masonc8b97812008-10-29 14:49:59 -04001293static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001294 u64 *start, u64 *end, u64 max_bytes,
1295 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001296{
1297 struct rb_node *node;
1298 struct extent_state *state;
1299 u64 cur_start = *start;
1300 u64 found = 0;
1301 u64 total_bytes = 0;
1302
Chris Masoncad321a2008-12-17 14:51:42 -05001303 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001304
Chris Masond1310b22008-01-24 16:13:08 -05001305 /*
1306 * this search will find all the extents that end after
1307 * our range starts.
1308 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001309 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001310 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001311 if (!found)
1312 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001313 goto out;
1314 }
1315
Chris Masond3977122009-01-05 21:25:51 -05001316 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001317 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001318 if (found && (state->start != cur_start ||
1319 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001320 goto out;
1321 }
1322 if (!(state->state & EXTENT_DELALLOC)) {
1323 if (!found)
1324 *end = state->end;
1325 goto out;
1326 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001327 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001328 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001329 *cached_state = state;
1330 atomic_inc(&state->refs);
1331 }
Chris Masond1310b22008-01-24 16:13:08 -05001332 found++;
1333 *end = state->end;
1334 cur_start = state->end + 1;
1335 node = rb_next(node);
1336 if (!node)
1337 break;
1338 total_bytes += state->end - state->start + 1;
1339 if (total_bytes >= max_bytes)
1340 break;
1341 }
1342out:
Chris Masoncad321a2008-12-17 14:51:42 -05001343 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001344 return found;
1345}
1346
Chris Masonc8b97812008-10-29 14:49:59 -04001347static noinline int __unlock_for_delalloc(struct inode *inode,
1348 struct page *locked_page,
1349 u64 start, u64 end)
1350{
1351 int ret;
1352 struct page *pages[16];
1353 unsigned long index = start >> PAGE_CACHE_SHIFT;
1354 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1355 unsigned long nr_pages = end_index - index + 1;
1356 int i;
1357
1358 if (index == locked_page->index && end_index == index)
1359 return 0;
1360
Chris Masond3977122009-01-05 21:25:51 -05001361 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001362 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001363 min_t(unsigned long, nr_pages,
1364 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001365 for (i = 0; i < ret; i++) {
1366 if (pages[i] != locked_page)
1367 unlock_page(pages[i]);
1368 page_cache_release(pages[i]);
1369 }
1370 nr_pages -= ret;
1371 index += ret;
1372 cond_resched();
1373 }
1374 return 0;
1375}
1376
1377static noinline int lock_delalloc_pages(struct inode *inode,
1378 struct page *locked_page,
1379 u64 delalloc_start,
1380 u64 delalloc_end)
1381{
1382 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1383 unsigned long start_index = index;
1384 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1385 unsigned long pages_locked = 0;
1386 struct page *pages[16];
1387 unsigned long nrpages;
1388 int ret;
1389 int i;
1390
1391 /* the caller is responsible for locking the start index */
1392 if (index == locked_page->index && index == end_index)
1393 return 0;
1394
1395 /* skip the page at the start index */
1396 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001397 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001398 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001399 min_t(unsigned long,
1400 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001401 if (ret == 0) {
1402 ret = -EAGAIN;
1403 goto done;
1404 }
1405 /* now we have an array of pages, lock them all */
1406 for (i = 0; i < ret; i++) {
1407 /*
1408 * the caller is taking responsibility for
1409 * locked_page
1410 */
Chris Mason771ed682008-11-06 22:02:51 -05001411 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001412 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001413 if (!PageDirty(pages[i]) ||
1414 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001415 ret = -EAGAIN;
1416 unlock_page(pages[i]);
1417 page_cache_release(pages[i]);
1418 goto done;
1419 }
1420 }
Chris Masonc8b97812008-10-29 14:49:59 -04001421 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001422 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001423 }
Chris Masonc8b97812008-10-29 14:49:59 -04001424 nrpages -= ret;
1425 index += ret;
1426 cond_resched();
1427 }
1428 ret = 0;
1429done:
1430 if (ret && pages_locked) {
1431 __unlock_for_delalloc(inode, locked_page,
1432 delalloc_start,
1433 ((u64)(start_index + pages_locked - 1)) <<
1434 PAGE_CACHE_SHIFT);
1435 }
1436 return ret;
1437}
1438
1439/*
1440 * find a contiguous range of bytes in the file marked as delalloc, not
1441 * more than 'max_bytes'. start and end are used to return the range,
1442 *
1443 * 1 is returned if we find something, 0 if nothing was in the tree
1444 */
1445static noinline u64 find_lock_delalloc_range(struct inode *inode,
1446 struct extent_io_tree *tree,
1447 struct page *locked_page,
1448 u64 *start, u64 *end,
1449 u64 max_bytes)
1450{
1451 u64 delalloc_start;
1452 u64 delalloc_end;
1453 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001454 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001455 int ret;
1456 int loops = 0;
1457
1458again:
1459 /* step one, find a bunch of delalloc bytes starting at start */
1460 delalloc_start = *start;
1461 delalloc_end = 0;
1462 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001463 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001464 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001465 *start = delalloc_start;
1466 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001467 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001468 return found;
1469 }
1470
1471 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001472 * start comes from the offset of locked_page. We have to lock
1473 * pages in order, so we can't process delalloc bytes before
1474 * locked_page
1475 */
Chris Masond3977122009-01-05 21:25:51 -05001476 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001477 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001478
1479 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001480 * make sure to limit the number of pages we try to lock down
1481 * if we're looping.
1482 */
Chris Masond3977122009-01-05 21:25:51 -05001483 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001484 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001485
Chris Masonc8b97812008-10-29 14:49:59 -04001486 /* step two, lock all the pages after the page that has start */
1487 ret = lock_delalloc_pages(inode, locked_page,
1488 delalloc_start, delalloc_end);
1489 if (ret == -EAGAIN) {
1490 /* some of the pages are gone, lets avoid looping by
1491 * shortening the size of the delalloc range we're searching
1492 */
Chris Mason9655d292009-09-02 15:22:30 -04001493 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001494 if (!loops) {
1495 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1496 max_bytes = PAGE_CACHE_SIZE - offset;
1497 loops = 1;
1498 goto again;
1499 } else {
1500 found = 0;
1501 goto out_failed;
1502 }
1503 }
1504 BUG_ON(ret);
1505
1506 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001507 lock_extent_bits(tree, delalloc_start, delalloc_end,
1508 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001509
1510 /* then test to make sure it is all still delalloc */
1511 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001512 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001513 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001514 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1515 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001516 __unlock_for_delalloc(inode, locked_page,
1517 delalloc_start, delalloc_end);
1518 cond_resched();
1519 goto again;
1520 }
Chris Mason9655d292009-09-02 15:22:30 -04001521 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001522 *start = delalloc_start;
1523 *end = delalloc_end;
1524out_failed:
1525 return found;
1526}
1527
1528int extent_clear_unlock_delalloc(struct inode *inode,
1529 struct extent_io_tree *tree,
1530 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001531 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001532{
1533 int ret;
1534 struct page *pages[16];
1535 unsigned long index = start >> PAGE_CACHE_SHIFT;
1536 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1537 unsigned long nr_pages = end_index - index + 1;
1538 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001539 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001540
Chris Masona791e352009-10-08 11:27:10 -04001541 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001542 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001543 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001544 clear_bits |= EXTENT_DIRTY;
1545
Chris Masona791e352009-10-08 11:27:10 -04001546 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001547 clear_bits |= EXTENT_DELALLOC;
1548
Chris Mason2c64c532009-09-02 15:04:12 -04001549 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001550 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1551 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1552 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001553 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001554
Chris Masond3977122009-01-05 21:25:51 -05001555 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001556 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001557 min_t(unsigned long,
1558 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001559 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001560
Chris Masona791e352009-10-08 11:27:10 -04001561 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001562 SetPagePrivate2(pages[i]);
1563
Chris Masonc8b97812008-10-29 14:49:59 -04001564 if (pages[i] == locked_page) {
1565 page_cache_release(pages[i]);
1566 continue;
1567 }
Chris Masona791e352009-10-08 11:27:10 -04001568 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001569 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001570 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001571 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001572 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001573 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001574 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001575 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001576 page_cache_release(pages[i]);
1577 }
1578 nr_pages -= ret;
1579 index += ret;
1580 cond_resched();
1581 }
1582 return 0;
1583}
Chris Masonc8b97812008-10-29 14:49:59 -04001584
Chris Masond352ac62008-09-29 15:18:18 -04001585/*
1586 * count the number of bytes in the tree that have a given bit(s)
1587 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1588 * cached. The total number found is returned.
1589 */
Chris Masond1310b22008-01-24 16:13:08 -05001590u64 count_range_bits(struct extent_io_tree *tree,
1591 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001592 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001593{
1594 struct rb_node *node;
1595 struct extent_state *state;
1596 u64 cur_start = *start;
1597 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001598 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001599 int found = 0;
1600
1601 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001602 WARN_ON(1);
1603 return 0;
1604 }
1605
Chris Masoncad321a2008-12-17 14:51:42 -05001606 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001607 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1608 total_bytes = tree->dirty_bytes;
1609 goto out;
1610 }
1611 /*
1612 * this search will find all the extents that end after
1613 * our range starts.
1614 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001615 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001616 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001617 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001618
Chris Masond3977122009-01-05 21:25:51 -05001619 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001620 state = rb_entry(node, struct extent_state, rb_node);
1621 if (state->start > search_end)
1622 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001623 if (contig && found && state->start > last + 1)
1624 break;
1625 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001626 total_bytes += min(search_end, state->end) + 1 -
1627 max(cur_start, state->start);
1628 if (total_bytes >= max_bytes)
1629 break;
1630 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001631 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001632 found = 1;
1633 }
Chris Masonec29ed52011-02-23 16:23:20 -05001634 last = state->end;
1635 } else if (contig && found) {
1636 break;
Chris Masond1310b22008-01-24 16:13:08 -05001637 }
1638 node = rb_next(node);
1639 if (!node)
1640 break;
1641 }
1642out:
Chris Masoncad321a2008-12-17 14:51:42 -05001643 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001644 return total_bytes;
1645}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001646
Chris Masond352ac62008-09-29 15:18:18 -04001647/*
1648 * set the private field for a given byte offset in the tree. If there isn't
1649 * an extent_state there already, this does nothing.
1650 */
Chris Masond1310b22008-01-24 16:13:08 -05001651int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1652{
1653 struct rb_node *node;
1654 struct extent_state *state;
1655 int ret = 0;
1656
Chris Masoncad321a2008-12-17 14:51:42 -05001657 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001658 /*
1659 * this search will find all the extents that end after
1660 * our range starts.
1661 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001662 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001663 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001664 ret = -ENOENT;
1665 goto out;
1666 }
1667 state = rb_entry(node, struct extent_state, rb_node);
1668 if (state->start != start) {
1669 ret = -ENOENT;
1670 goto out;
1671 }
1672 state->private = private;
1673out:
Chris Masoncad321a2008-12-17 14:51:42 -05001674 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001675 return ret;
1676}
1677
1678int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1679{
1680 struct rb_node *node;
1681 struct extent_state *state;
1682 int ret = 0;
1683
Chris Masoncad321a2008-12-17 14:51:42 -05001684 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001685 /*
1686 * this search will find all the extents that end after
1687 * our range starts.
1688 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001689 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001690 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001691 ret = -ENOENT;
1692 goto out;
1693 }
1694 state = rb_entry(node, struct extent_state, rb_node);
1695 if (state->start != start) {
1696 ret = -ENOENT;
1697 goto out;
1698 }
1699 *private = state->private;
1700out:
Chris Masoncad321a2008-12-17 14:51:42 -05001701 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001702 return ret;
1703}
1704
1705/*
1706 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001707 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001708 * has the bits set. Otherwise, 1 is returned if any bit in the
1709 * range is found set.
1710 */
1711int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001712 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001713{
1714 struct extent_state *state = NULL;
1715 struct rb_node *node;
1716 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001717
Chris Masoncad321a2008-12-17 14:51:42 -05001718 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001719 if (cached && cached->tree && cached->start <= start &&
1720 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001721 node = &cached->rb_node;
1722 else
1723 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001724 while (node && start <= end) {
1725 state = rb_entry(node, struct extent_state, rb_node);
1726
1727 if (filled && state->start > start) {
1728 bitset = 0;
1729 break;
1730 }
1731
1732 if (state->start > end)
1733 break;
1734
1735 if (state->state & bits) {
1736 bitset = 1;
1737 if (!filled)
1738 break;
1739 } else if (filled) {
1740 bitset = 0;
1741 break;
1742 }
Chris Mason46562cec2009-09-23 20:23:16 -04001743
1744 if (state->end == (u64)-1)
1745 break;
1746
Chris Masond1310b22008-01-24 16:13:08 -05001747 start = state->end + 1;
1748 if (start > end)
1749 break;
1750 node = rb_next(node);
1751 if (!node) {
1752 if (filled)
1753 bitset = 0;
1754 break;
1755 }
1756 }
Chris Masoncad321a2008-12-17 14:51:42 -05001757 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001758 return bitset;
1759}
Chris Masond1310b22008-01-24 16:13:08 -05001760
1761/*
1762 * helper function to set a given page up to date if all the
1763 * extents in the tree for that page are up to date
1764 */
1765static int check_page_uptodate(struct extent_io_tree *tree,
1766 struct page *page)
1767{
1768 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1769 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001770 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001771 SetPageUptodate(page);
1772 return 0;
1773}
1774
1775/*
1776 * helper function to unlock a page if all the extents in the tree
1777 * for that page are unlocked
1778 */
1779static int check_page_locked(struct extent_io_tree *tree,
1780 struct page *page)
1781{
1782 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1783 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001784 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001785 unlock_page(page);
1786 return 0;
1787}
1788
1789/*
1790 * helper function to end page writeback if all the extents
1791 * in the tree for that page are done with writeback
1792 */
1793static int check_page_writeback(struct extent_io_tree *tree,
1794 struct page *page)
1795{
Chris Mason1edbb732009-09-02 13:24:36 -04001796 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001797 return 0;
1798}
1799
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001800/*
1801 * When IO fails, either with EIO or csum verification fails, we
1802 * try other mirrors that might have a good copy of the data. This
1803 * io_failure_record is used to record state as we go through all the
1804 * mirrors. If another mirror has good data, the page is set up to date
1805 * and things continue. If a good mirror can't be found, the original
1806 * bio end_io callback is called to indicate things have failed.
1807 */
1808struct io_failure_record {
1809 struct page *page;
1810 u64 start;
1811 u64 len;
1812 u64 logical;
1813 unsigned long bio_flags;
1814 int this_mirror;
1815 int failed_mirror;
1816 int in_validation;
1817};
1818
1819static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1820 int did_repair)
1821{
1822 int ret;
1823 int err = 0;
1824 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1825
1826 set_state_private(failure_tree, rec->start, 0);
1827 ret = clear_extent_bits(failure_tree, rec->start,
1828 rec->start + rec->len - 1,
1829 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1830 if (ret)
1831 err = ret;
1832
1833 if (did_repair) {
1834 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1835 rec->start + rec->len - 1,
1836 EXTENT_DAMAGED, GFP_NOFS);
1837 if (ret && !err)
1838 err = ret;
1839 }
1840
1841 kfree(rec);
1842 return err;
1843}
1844
1845static void repair_io_failure_callback(struct bio *bio, int err)
1846{
1847 complete(bio->bi_private);
1848}
1849
1850/*
1851 * this bypasses the standard btrfs submit functions deliberately, as
1852 * the standard behavior is to write all copies in a raid setup. here we only
1853 * want to write the one bad copy. so we do the mapping for ourselves and issue
1854 * submit_bio directly.
1855 * to avoid any synchonization issues, wait for the data after writing, which
1856 * actually prevents the read that triggered the error from finishing.
1857 * currently, there can be no more than two copies of every data bit. thus,
1858 * exactly one rewrite is required.
1859 */
1860int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1861 u64 length, u64 logical, struct page *page,
1862 int mirror_num)
1863{
1864 struct bio *bio;
1865 struct btrfs_device *dev;
1866 DECLARE_COMPLETION_ONSTACK(compl);
1867 u64 map_length = 0;
1868 u64 sector;
1869 struct btrfs_bio *bbio = NULL;
1870 int ret;
1871
1872 BUG_ON(!mirror_num);
1873
1874 bio = bio_alloc(GFP_NOFS, 1);
1875 if (!bio)
1876 return -EIO;
1877 bio->bi_private = &compl;
1878 bio->bi_end_io = repair_io_failure_callback;
1879 bio->bi_size = 0;
1880 map_length = length;
1881
1882 ret = btrfs_map_block(map_tree, WRITE, logical,
1883 &map_length, &bbio, mirror_num);
1884 if (ret) {
1885 bio_put(bio);
1886 return -EIO;
1887 }
1888 BUG_ON(mirror_num != bbio->mirror_num);
1889 sector = bbio->stripes[mirror_num-1].physical >> 9;
1890 bio->bi_sector = sector;
1891 dev = bbio->stripes[mirror_num-1].dev;
1892 kfree(bbio);
1893 if (!dev || !dev->bdev || !dev->writeable) {
1894 bio_put(bio);
1895 return -EIO;
1896 }
1897 bio->bi_bdev = dev->bdev;
1898 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001899 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001900 wait_for_completion(&compl);
1901
1902 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1903 /* try to remap that extent elsewhere? */
1904 bio_put(bio);
1905 return -EIO;
1906 }
1907
1908 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1909 "sector %llu)\n", page->mapping->host->i_ino, start,
1910 dev->name, sector);
1911
1912 bio_put(bio);
1913 return 0;
1914}
1915
1916/*
1917 * each time an IO finishes, we do a fast check in the IO failure tree
1918 * to see if we need to process or clean up an io_failure_record
1919 */
1920static int clean_io_failure(u64 start, struct page *page)
1921{
1922 u64 private;
1923 u64 private_failure;
1924 struct io_failure_record *failrec;
1925 struct btrfs_mapping_tree *map_tree;
1926 struct extent_state *state;
1927 int num_copies;
1928 int did_repair = 0;
1929 int ret;
1930 struct inode *inode = page->mapping->host;
1931
1932 private = 0;
1933 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1934 (u64)-1, 1, EXTENT_DIRTY, 0);
1935 if (!ret)
1936 return 0;
1937
1938 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1939 &private_failure);
1940 if (ret)
1941 return 0;
1942
1943 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1944 BUG_ON(!failrec->this_mirror);
1945
1946 if (failrec->in_validation) {
1947 /* there was no real error, just free the record */
1948 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1949 failrec->start);
1950 did_repair = 1;
1951 goto out;
1952 }
1953
1954 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1955 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1956 failrec->start,
1957 EXTENT_LOCKED);
1958 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1959
1960 if (state && state->start == failrec->start) {
1961 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1962 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1963 failrec->len);
1964 if (num_copies > 1) {
1965 ret = repair_io_failure(map_tree, start, failrec->len,
1966 failrec->logical, page,
1967 failrec->failed_mirror);
1968 did_repair = !ret;
1969 }
1970 }
1971
1972out:
1973 if (!ret)
1974 ret = free_io_failure(inode, failrec, did_repair);
1975
1976 return ret;
1977}
1978
1979/*
1980 * this is a generic handler for readpage errors (default
1981 * readpage_io_failed_hook). if other copies exist, read those and write back
1982 * good data to the failed position. does not investigate in remapping the
1983 * failed extent elsewhere, hoping the device will be smart enough to do this as
1984 * needed
1985 */
1986
1987static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1988 u64 start, u64 end, int failed_mirror,
1989 struct extent_state *state)
1990{
1991 struct io_failure_record *failrec = NULL;
1992 u64 private;
1993 struct extent_map *em;
1994 struct inode *inode = page->mapping->host;
1995 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1996 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1997 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1998 struct bio *bio;
1999 int num_copies;
2000 int ret;
2001 int read_mode;
2002 u64 logical;
2003
2004 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2005
2006 ret = get_state_private(failure_tree, start, &private);
2007 if (ret) {
2008 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2009 if (!failrec)
2010 return -ENOMEM;
2011 failrec->start = start;
2012 failrec->len = end - start + 1;
2013 failrec->this_mirror = 0;
2014 failrec->bio_flags = 0;
2015 failrec->in_validation = 0;
2016
2017 read_lock(&em_tree->lock);
2018 em = lookup_extent_mapping(em_tree, start, failrec->len);
2019 if (!em) {
2020 read_unlock(&em_tree->lock);
2021 kfree(failrec);
2022 return -EIO;
2023 }
2024
2025 if (em->start > start || em->start + em->len < start) {
2026 free_extent_map(em);
2027 em = NULL;
2028 }
2029 read_unlock(&em_tree->lock);
2030
2031 if (!em || IS_ERR(em)) {
2032 kfree(failrec);
2033 return -EIO;
2034 }
2035 logical = start - em->start;
2036 logical = em->block_start + logical;
2037 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2038 logical = em->block_start;
2039 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2040 extent_set_compress_type(&failrec->bio_flags,
2041 em->compress_type);
2042 }
2043 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2044 "len=%llu\n", logical, start, failrec->len);
2045 failrec->logical = logical;
2046 free_extent_map(em);
2047
2048 /* set the bits in the private failure tree */
2049 ret = set_extent_bits(failure_tree, start, end,
2050 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2051 if (ret >= 0)
2052 ret = set_state_private(failure_tree, start,
2053 (u64)(unsigned long)failrec);
2054 /* set the bits in the inode's tree */
2055 if (ret >= 0)
2056 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2057 GFP_NOFS);
2058 if (ret < 0) {
2059 kfree(failrec);
2060 return ret;
2061 }
2062 } else {
2063 failrec = (struct io_failure_record *)(unsigned long)private;
2064 pr_debug("bio_readpage_error: (found) logical=%llu, "
2065 "start=%llu, len=%llu, validation=%d\n",
2066 failrec->logical, failrec->start, failrec->len,
2067 failrec->in_validation);
2068 /*
2069 * when data can be on disk more than twice, add to failrec here
2070 * (e.g. with a list for failed_mirror) to make
2071 * clean_io_failure() clean all those errors at once.
2072 */
2073 }
2074 num_copies = btrfs_num_copies(
2075 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2076 failrec->logical, failrec->len);
2077 if (num_copies == 1) {
2078 /*
2079 * we only have a single copy of the data, so don't bother with
2080 * all the retry and error correction code that follows. no
2081 * matter what the error is, it is very likely to persist.
2082 */
2083 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2084 "state=%p, num_copies=%d, next_mirror %d, "
2085 "failed_mirror %d\n", state, num_copies,
2086 failrec->this_mirror, failed_mirror);
2087 free_io_failure(inode, failrec, 0);
2088 return -EIO;
2089 }
2090
2091 if (!state) {
2092 spin_lock(&tree->lock);
2093 state = find_first_extent_bit_state(tree, failrec->start,
2094 EXTENT_LOCKED);
2095 if (state && state->start != failrec->start)
2096 state = NULL;
2097 spin_unlock(&tree->lock);
2098 }
2099
2100 /*
2101 * there are two premises:
2102 * a) deliver good data to the caller
2103 * b) correct the bad sectors on disk
2104 */
2105 if (failed_bio->bi_vcnt > 1) {
2106 /*
2107 * to fulfill b), we need to know the exact failing sectors, as
2108 * we don't want to rewrite any more than the failed ones. thus,
2109 * we need separate read requests for the failed bio
2110 *
2111 * if the following BUG_ON triggers, our validation request got
2112 * merged. we need separate requests for our algorithm to work.
2113 */
2114 BUG_ON(failrec->in_validation);
2115 failrec->in_validation = 1;
2116 failrec->this_mirror = failed_mirror;
2117 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2118 } else {
2119 /*
2120 * we're ready to fulfill a) and b) alongside. get a good copy
2121 * of the failed sector and if we succeed, we have setup
2122 * everything for repair_io_failure to do the rest for us.
2123 */
2124 if (failrec->in_validation) {
2125 BUG_ON(failrec->this_mirror != failed_mirror);
2126 failrec->in_validation = 0;
2127 failrec->this_mirror = 0;
2128 }
2129 failrec->failed_mirror = failed_mirror;
2130 failrec->this_mirror++;
2131 if (failrec->this_mirror == failed_mirror)
2132 failrec->this_mirror++;
2133 read_mode = READ_SYNC;
2134 }
2135
2136 if (!state || failrec->this_mirror > num_copies) {
2137 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2138 "next_mirror %d, failed_mirror %d\n", state,
2139 num_copies, failrec->this_mirror, failed_mirror);
2140 free_io_failure(inode, failrec, 0);
2141 return -EIO;
2142 }
2143
2144 bio = bio_alloc(GFP_NOFS, 1);
2145 bio->bi_private = state;
2146 bio->bi_end_io = failed_bio->bi_end_io;
2147 bio->bi_sector = failrec->logical >> 9;
2148 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2149 bio->bi_size = 0;
2150
2151 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2152
2153 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2154 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2155 failrec->this_mirror, num_copies, failrec->in_validation);
2156
2157 tree->ops->submit_bio_hook(inode, read_mode, bio, failrec->this_mirror,
2158 failrec->bio_flags, 0);
2159 return 0;
2160}
2161
Chris Masond1310b22008-01-24 16:13:08 -05002162/* lots and lots of room for performance fixes in the end_bio funcs */
2163
Jeff Mahoney87826df2012-02-15 16:23:57 +01002164int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2165{
2166 int uptodate = (err == 0);
2167 struct extent_io_tree *tree;
2168 int ret;
2169
2170 tree = &BTRFS_I(page->mapping->host)->io_tree;
2171
2172 if (tree->ops && tree->ops->writepage_end_io_hook) {
2173 ret = tree->ops->writepage_end_io_hook(page, start,
2174 end, NULL, uptodate);
2175 if (ret)
2176 uptodate = 0;
2177 }
2178
2179 if (!uptodate && tree->ops &&
2180 tree->ops->writepage_io_failed_hook) {
2181 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2182 start, end, NULL);
2183 /* Writeback already completed */
2184 if (ret == 0)
2185 return 1;
2186 }
2187
2188 if (!uptodate) {
2189 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2190 ClearPageUptodate(page);
2191 SetPageError(page);
2192 }
2193 return 0;
2194}
2195
Chris Masond1310b22008-01-24 16:13:08 -05002196/*
2197 * after a writepage IO is done, we need to:
2198 * clear the uptodate bits on error
2199 * clear the writeback bits in the extent tree for this IO
2200 * end_page_writeback if the page has no more pending IO
2201 *
2202 * Scheduling is not allowed, so the extent state tree is expected
2203 * to have one and only one object corresponding to this IO.
2204 */
Chris Masond1310b22008-01-24 16:13:08 -05002205static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002206{
Chris Masond1310b22008-01-24 16:13:08 -05002207 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002208 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002209 u64 start;
2210 u64 end;
2211 int whole_page;
2212
Chris Masond1310b22008-01-24 16:13:08 -05002213 do {
2214 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002215 tree = &BTRFS_I(page->mapping->host)->io_tree;
2216
Chris Masond1310b22008-01-24 16:13:08 -05002217 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2218 bvec->bv_offset;
2219 end = start + bvec->bv_len - 1;
2220
2221 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2222 whole_page = 1;
2223 else
2224 whole_page = 0;
2225
2226 if (--bvec >= bio->bi_io_vec)
2227 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002228
Jeff Mahoney87826df2012-02-15 16:23:57 +01002229 if (end_extent_writepage(page, err, start, end))
2230 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002231
Chris Masond1310b22008-01-24 16:13:08 -05002232 if (whole_page)
2233 end_page_writeback(page);
2234 else
2235 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002236 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002237
Chris Masond1310b22008-01-24 16:13:08 -05002238 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002239}
2240
2241/*
2242 * after a readpage IO is done, we need to:
2243 * clear the uptodate bits on error
2244 * set the uptodate bits if things worked
2245 * set the page up to date if all extents in the tree are uptodate
2246 * clear the lock bit in the extent tree
2247 * unlock the page if there are no other extents locked for it
2248 *
2249 * Scheduling is not allowed, so the extent state tree is expected
2250 * to have one and only one object corresponding to this IO.
2251 */
Chris Masond1310b22008-01-24 16:13:08 -05002252static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002253{
2254 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002255 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2256 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002257 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002258 u64 start;
2259 u64 end;
2260 int whole_page;
2261 int ret;
2262
Chris Masond20f7042008-12-08 16:58:54 -05002263 if (err)
2264 uptodate = 0;
2265
Chris Masond1310b22008-01-24 16:13:08 -05002266 do {
2267 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002268 struct extent_state *cached = NULL;
2269 struct extent_state *state;
2270
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2272 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2273 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002274 tree = &BTRFS_I(page->mapping->host)->io_tree;
2275
Chris Masond1310b22008-01-24 16:13:08 -05002276 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2277 bvec->bv_offset;
2278 end = start + bvec->bv_len - 1;
2279
2280 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2281 whole_page = 1;
2282 else
2283 whole_page = 0;
2284
Chris Mason4125bf72010-02-03 18:18:45 +00002285 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002286 prefetchw(&bvec->bv_page->flags);
2287
Arne Jansen507903b2011-04-06 10:02:20 +00002288 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002289 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002290 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002291 /*
2292 * take a reference on the state, unlock will drop
2293 * the ref
2294 */
2295 cache_state(state, &cached);
2296 }
2297 spin_unlock(&tree->lock);
2298
Chris Masond1310b22008-01-24 16:13:08 -05002299 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002300 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002301 state);
Chris Masond1310b22008-01-24 16:13:08 -05002302 if (ret)
2303 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002304 else
2305 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002306 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 if (!uptodate) {
Jan Schmidt32240a92011-11-20 07:33:38 -05002308 int failed_mirror;
2309 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002310 /*
2311 * The generic bio_readpage_error handles errors the
2312 * following way: If possible, new read requests are
2313 * created and submitted and will end up in
2314 * end_bio_extent_readpage as well (if we're lucky, not
2315 * in the !uptodate case). In that case it returns 0 and
2316 * we just go on with the next page in our bio. If it
2317 * can't handle the error it will return -EIO and we
2318 * remain responsible for that page.
2319 */
2320 ret = bio_readpage_error(bio, page, start, end,
2321 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002322 if (ret == 0) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002323error_handled:
Chris Mason3b951512008-04-17 11:29:12 -04002324 uptodate =
2325 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002326 if (err)
2327 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002328 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002329 continue;
2330 }
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002331 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2332 ret = tree->ops->readpage_io_failed_hook(
2333 bio, page, start, end,
2334 failed_mirror, state);
2335 if (ret == 0)
2336 goto error_handled;
2337 }
Chris Mason7e383262008-04-09 16:28:12 -04002338 }
Chris Mason70dec802008-01-29 09:59:12 -05002339
Chris Mason771ed682008-11-06 22:02:51 -05002340 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002341 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002342 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002343 }
Arne Jansen507903b2011-04-06 10:02:20 +00002344 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002345
Chris Mason70dec802008-01-29 09:59:12 -05002346 if (whole_page) {
2347 if (uptodate) {
2348 SetPageUptodate(page);
2349 } else {
2350 ClearPageUptodate(page);
2351 SetPageError(page);
2352 }
Chris Masond1310b22008-01-24 16:13:08 -05002353 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002354 } else {
2355 if (uptodate) {
2356 check_page_uptodate(tree, page);
2357 } else {
2358 ClearPageUptodate(page);
2359 SetPageError(page);
2360 }
Chris Masond1310b22008-01-24 16:13:08 -05002361 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002362 }
Chris Mason4125bf72010-02-03 18:18:45 +00002363 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002364
2365 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002366}
2367
Miao Xie88f794e2010-11-22 03:02:55 +00002368struct bio *
2369btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2370 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002371{
2372 struct bio *bio;
2373
2374 bio = bio_alloc(gfp_flags, nr_vecs);
2375
2376 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2377 while (!bio && (nr_vecs /= 2))
2378 bio = bio_alloc(gfp_flags, nr_vecs);
2379 }
2380
2381 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002382 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002383 bio->bi_bdev = bdev;
2384 bio->bi_sector = first_sector;
2385 }
2386 return bio;
2387}
2388
Chris Masonc8b97812008-10-29 14:49:59 -04002389static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2390 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002391{
Chris Masond1310b22008-01-24 16:13:08 -05002392 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002393 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2394 struct page *page = bvec->bv_page;
2395 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002396 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002397
2398 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002399
David Woodhouse902b22f2008-08-20 08:51:49 -04002400 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002401
2402 bio_get(bio);
2403
Chris Mason065631f2008-02-20 12:07:25 -05002404 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002405 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002406 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002407 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002408 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002409
Chris Masond1310b22008-01-24 16:13:08 -05002410 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2411 ret = -EOPNOTSUPP;
2412 bio_put(bio);
2413 return ret;
2414}
2415
2416static int submit_extent_page(int rw, struct extent_io_tree *tree,
2417 struct page *page, sector_t sector,
2418 size_t size, unsigned long offset,
2419 struct block_device *bdev,
2420 struct bio **bio_ret,
2421 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002422 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002423 int mirror_num,
2424 unsigned long prev_bio_flags,
2425 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002426{
2427 int ret = 0;
2428 struct bio *bio;
2429 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002430 int contig = 0;
2431 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2432 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002433 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002434
2435 if (bio_ret && *bio_ret) {
2436 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002437 if (old_compressed)
2438 contig = bio->bi_sector == sector;
2439 else
2440 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2441 sector;
2442
2443 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002444 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002445 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2446 bio_flags)) ||
2447 bio_add_page(bio, page, page_size, offset) < page_size) {
2448 ret = submit_one_bio(rw, bio, mirror_num,
2449 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002450 bio = NULL;
2451 } else {
2452 return 0;
2453 }
2454 }
Chris Masonc8b97812008-10-29 14:49:59 -04002455 if (this_compressed)
2456 nr = BIO_MAX_PAGES;
2457 else
2458 nr = bio_get_nr_vecs(bdev);
2459
Miao Xie88f794e2010-11-22 03:02:55 +00002460 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002461 if (!bio)
2462 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002463
Chris Masonc8b97812008-10-29 14:49:59 -04002464 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002465 bio->bi_end_io = end_io_func;
2466 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002467
Chris Masond3977122009-01-05 21:25:51 -05002468 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002469 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002470 else
Chris Masonc8b97812008-10-29 14:49:59 -04002471 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002472
2473 return ret;
2474}
2475
2476void set_page_extent_mapped(struct page *page)
2477{
2478 if (!PagePrivate(page)) {
2479 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002480 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002481 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002482 }
2483}
2484
Christoph Hellwigb2950862008-12-02 09:54:17 -05002485static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002486{
Chris Masoneb14ab82011-02-10 12:35:00 -05002487 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002488 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2489}
2490
2491/*
2492 * basic readpage implementation. Locked extent state structs are inserted
2493 * into the tree that are removed when the IO is done (by the end_io
2494 * handlers)
2495 */
2496static int __extent_read_full_page(struct extent_io_tree *tree,
2497 struct page *page,
2498 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002499 struct bio **bio, int mirror_num,
2500 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002501{
2502 struct inode *inode = page->mapping->host;
2503 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2504 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2505 u64 end;
2506 u64 cur = start;
2507 u64 extent_offset;
2508 u64 last_byte = i_size_read(inode);
2509 u64 block_start;
2510 u64 cur_end;
2511 sector_t sector;
2512 struct extent_map *em;
2513 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002514 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002515 int ret;
2516 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002517 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002518 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002519 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002520 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002521 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002522
2523 set_page_extent_mapped(page);
2524
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002525 if (!PageUptodate(page)) {
2526 if (cleancache_get_page(page) == 0) {
2527 BUG_ON(blocksize != PAGE_SIZE);
2528 goto out;
2529 }
2530 }
2531
Chris Masond1310b22008-01-24 16:13:08 -05002532 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002533 while (1) {
2534 lock_extent(tree, start, end, GFP_NOFS);
2535 ordered = btrfs_lookup_ordered_extent(inode, start);
2536 if (!ordered)
2537 break;
2538 unlock_extent(tree, start, end, GFP_NOFS);
2539 btrfs_start_ordered_extent(inode, ordered, 1);
2540 btrfs_put_ordered_extent(ordered);
2541 }
Chris Masond1310b22008-01-24 16:13:08 -05002542
Chris Masonc8b97812008-10-29 14:49:59 -04002543 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2544 char *userpage;
2545 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2546
2547 if (zero_offset) {
2548 iosize = PAGE_CACHE_SIZE - zero_offset;
2549 userpage = kmap_atomic(page, KM_USER0);
2550 memset(userpage + zero_offset, 0, iosize);
2551 flush_dcache_page(page);
2552 kunmap_atomic(userpage, KM_USER0);
2553 }
2554 }
Chris Masond1310b22008-01-24 16:13:08 -05002555 while (cur <= end) {
2556 if (cur >= last_byte) {
2557 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002558 struct extent_state *cached = NULL;
2559
David Sterba306e16c2011-04-19 14:29:38 +02002560 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002561 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002562 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002563 flush_dcache_page(page);
2564 kunmap_atomic(userpage, KM_USER0);
2565 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002566 &cached, GFP_NOFS);
2567 unlock_extent_cached(tree, cur, cur + iosize - 1,
2568 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002569 break;
2570 }
David Sterba306e16c2011-04-19 14:29:38 +02002571 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002572 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002573 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002574 SetPageError(page);
2575 unlock_extent(tree, cur, end, GFP_NOFS);
2576 break;
2577 }
Chris Masond1310b22008-01-24 16:13:08 -05002578 extent_offset = cur - em->start;
2579 BUG_ON(extent_map_end(em) <= cur);
2580 BUG_ON(end < cur);
2581
Li Zefan261507a02010-12-17 14:21:50 +08002582 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002583 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002584 extent_set_compress_type(&this_bio_flag,
2585 em->compress_type);
2586 }
Chris Masonc8b97812008-10-29 14:49:59 -04002587
Chris Masond1310b22008-01-24 16:13:08 -05002588 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2589 cur_end = min(extent_map_end(em) - 1, end);
2590 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002591 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2592 disk_io_size = em->block_len;
2593 sector = em->block_start >> 9;
2594 } else {
2595 sector = (em->block_start + extent_offset) >> 9;
2596 disk_io_size = iosize;
2597 }
Chris Masond1310b22008-01-24 16:13:08 -05002598 bdev = em->bdev;
2599 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002600 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2601 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002602 free_extent_map(em);
2603 em = NULL;
2604
2605 /* we've found a hole, just zero and go on */
2606 if (block_start == EXTENT_MAP_HOLE) {
2607 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002608 struct extent_state *cached = NULL;
2609
Chris Masond1310b22008-01-24 16:13:08 -05002610 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002611 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002612 flush_dcache_page(page);
2613 kunmap_atomic(userpage, KM_USER0);
2614
2615 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002616 &cached, GFP_NOFS);
2617 unlock_extent_cached(tree, cur, cur + iosize - 1,
2618 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002619 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002620 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002621 continue;
2622 }
2623 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002624 if (test_range_bit(tree, cur, cur_end,
2625 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002626 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002627 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2628 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002629 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002630 continue;
2631 }
Chris Mason70dec802008-01-29 09:59:12 -05002632 /* we have an inline extent but it didn't get marked up
2633 * to date. Error out
2634 */
2635 if (block_start == EXTENT_MAP_INLINE) {
2636 SetPageError(page);
2637 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2638 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002639 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002640 continue;
2641 }
Chris Masond1310b22008-01-24 16:13:08 -05002642
2643 ret = 0;
2644 if (tree->ops && tree->ops->readpage_io_hook) {
2645 ret = tree->ops->readpage_io_hook(page, cur,
2646 cur + iosize - 1);
2647 }
2648 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002649 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2650 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002651 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002652 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002653 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002654 end_bio_extent_readpage, mirror_num,
2655 *bio_flags,
2656 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002657 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002658 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002659 }
2660 if (ret)
2661 SetPageError(page);
2662 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002663 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002664 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002665out:
Chris Masond1310b22008-01-24 16:13:08 -05002666 if (!nr) {
2667 if (!PageError(page))
2668 SetPageUptodate(page);
2669 unlock_page(page);
2670 }
2671 return 0;
2672}
2673
2674int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002675 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002676{
2677 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002678 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002679 int ret;
2680
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002681 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002682 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002683 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002684 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002685 return ret;
2686}
Chris Masond1310b22008-01-24 16:13:08 -05002687
Chris Mason11c83492009-04-20 15:50:09 -04002688static noinline void update_nr_written(struct page *page,
2689 struct writeback_control *wbc,
2690 unsigned long nr_written)
2691{
2692 wbc->nr_to_write -= nr_written;
2693 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2694 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2695 page->mapping->writeback_index = page->index + nr_written;
2696}
2697
Chris Masond1310b22008-01-24 16:13:08 -05002698/*
2699 * the writepage semantics are similar to regular writepage. extent
2700 * records are inserted to lock ranges in the tree, and as dirty areas
2701 * are found, they are marked writeback. Then the lock bits are removed
2702 * and the end_io handler clears the writeback ranges
2703 */
2704static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2705 void *data)
2706{
2707 struct inode *inode = page->mapping->host;
2708 struct extent_page_data *epd = data;
2709 struct extent_io_tree *tree = epd->tree;
2710 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2711 u64 delalloc_start;
2712 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2713 u64 end;
2714 u64 cur = start;
2715 u64 extent_offset;
2716 u64 last_byte = i_size_read(inode);
2717 u64 block_start;
2718 u64 iosize;
2719 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002720 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002721 struct extent_map *em;
2722 struct block_device *bdev;
2723 int ret;
2724 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002725 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002726 size_t blocksize;
2727 loff_t i_size = i_size_read(inode);
2728 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2729 u64 nr_delalloc;
2730 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002731 int page_started;
2732 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002733 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002734 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002735 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002736
Chris Masonffbd5172009-04-20 15:50:09 -04002737 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002738 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002739 else
2740 write_flags = WRITE;
2741
liubo1abe9b82011-03-24 11:18:59 +00002742 trace___extent_writepage(page, inode, wbc);
2743
Chris Masond1310b22008-01-24 16:13:08 -05002744 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002745
2746 ClearPageError(page);
2747
Chris Mason7f3c74f2008-07-18 12:01:11 -04002748 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002749 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002750 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002751 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002752 unlock_page(page);
2753 return 0;
2754 }
2755
2756 if (page->index == end_index) {
2757 char *userpage;
2758
Chris Masond1310b22008-01-24 16:13:08 -05002759 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002760 memset(userpage + pg_offset, 0,
2761 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002762 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002763 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002764 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002765 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002766
2767 set_page_extent_mapped(page);
2768
Josef Bacik9e487102011-08-01 12:08:18 -04002769 if (!tree->ops || !tree->ops->fill_delalloc)
2770 fill_delalloc = false;
2771
Chris Masond1310b22008-01-24 16:13:08 -05002772 delalloc_start = start;
2773 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002774 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002775 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002776 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002777 /*
2778 * make sure the wbc mapping index is at least updated
2779 * to this page.
2780 */
2781 update_nr_written(page, wbc, 0);
2782
Chris Masond3977122009-01-05 21:25:51 -05002783 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002784 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002785 page,
2786 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002787 &delalloc_end,
2788 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002789 if (nr_delalloc == 0) {
2790 delalloc_start = delalloc_end + 1;
2791 continue;
2792 }
2793 tree->ops->fill_delalloc(inode, page, delalloc_start,
2794 delalloc_end, &page_started,
2795 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002796 /*
2797 * delalloc_end is already one less than the total
2798 * length, so we don't subtract one from
2799 * PAGE_CACHE_SIZE
2800 */
2801 delalloc_to_write += (delalloc_end - delalloc_start +
2802 PAGE_CACHE_SIZE) >>
2803 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002804 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002805 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002806 if (wbc->nr_to_write < delalloc_to_write) {
2807 int thresh = 8192;
2808
2809 if (delalloc_to_write < thresh * 2)
2810 thresh = delalloc_to_write;
2811 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2812 thresh);
2813 }
Chris Masonc8b97812008-10-29 14:49:59 -04002814
Chris Mason771ed682008-11-06 22:02:51 -05002815 /* did the fill delalloc function already unlock and start
2816 * the IO?
2817 */
2818 if (page_started) {
2819 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002820 /*
2821 * we've unlocked the page, so we can't update
2822 * the mapping's writeback index, just update
2823 * nr_to_write.
2824 */
2825 wbc->nr_to_write -= nr_written;
2826 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002827 }
Chris Masonc8b97812008-10-29 14:49:59 -04002828 }
Chris Mason247e7432008-07-17 12:53:51 -04002829 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002830 ret = tree->ops->writepage_start_hook(page, start,
2831 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002832 if (ret) {
2833 /* Fixup worker will requeue */
2834 if (ret == -EBUSY)
2835 wbc->pages_skipped++;
2836 else
2837 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002838 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002839 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002840 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002841 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002842 }
2843 }
2844
Chris Mason11c83492009-04-20 15:50:09 -04002845 /*
2846 * we don't want to touch the inode after unlocking the page,
2847 * so we update the mapping writeback index now
2848 */
2849 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002850
Chris Masond1310b22008-01-24 16:13:08 -05002851 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002852 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002853 if (tree->ops && tree->ops->writepage_end_io_hook)
2854 tree->ops->writepage_end_io_hook(page, start,
2855 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002856 goto done;
2857 }
2858
Chris Masond1310b22008-01-24 16:13:08 -05002859 blocksize = inode->i_sb->s_blocksize;
2860
2861 while (cur <= end) {
2862 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 if (tree->ops && tree->ops->writepage_end_io_hook)
2864 tree->ops->writepage_end_io_hook(page, cur,
2865 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002866 break;
2867 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002868 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002869 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002870 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002871 SetPageError(page);
2872 break;
2873 }
2874
2875 extent_offset = cur - em->start;
2876 BUG_ON(extent_map_end(em) <= cur);
2877 BUG_ON(end < cur);
2878 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2879 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2880 sector = (em->block_start + extent_offset) >> 9;
2881 bdev = em->bdev;
2882 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002883 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002884 free_extent_map(em);
2885 em = NULL;
2886
Chris Masonc8b97812008-10-29 14:49:59 -04002887 /*
2888 * compressed and inline extents are written through other
2889 * paths in the FS
2890 */
2891 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002892 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002893 /*
2894 * end_io notification does not happen here for
2895 * compressed extents
2896 */
2897 if (!compressed && tree->ops &&
2898 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002899 tree->ops->writepage_end_io_hook(page, cur,
2900 cur + iosize - 1,
2901 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002902 else if (compressed) {
2903 /* we don't want to end_page_writeback on
2904 * a compressed extent. this happens
2905 * elsewhere
2906 */
2907 nr++;
2908 }
2909
2910 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002911 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002912 continue;
2913 }
Chris Masond1310b22008-01-24 16:13:08 -05002914 /* leave this out until we have a page_mkwrite call */
2915 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002916 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002917 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002918 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002919 continue;
2920 }
Chris Masonc8b97812008-10-29 14:49:59 -04002921
Chris Masond1310b22008-01-24 16:13:08 -05002922 if (tree->ops && tree->ops->writepage_io_hook) {
2923 ret = tree->ops->writepage_io_hook(page, cur,
2924 cur + iosize - 1);
2925 } else {
2926 ret = 0;
2927 }
Chris Mason1259ab72008-05-12 13:39:03 -04002928 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002929 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002930 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002931 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002932
Chris Masond1310b22008-01-24 16:13:08 -05002933 set_range_writeback(tree, cur, cur + iosize - 1);
2934 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002935 printk(KERN_ERR "btrfs warning page %lu not "
2936 "writeback, cur %llu end %llu\n",
2937 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002938 (unsigned long long)end);
2939 }
2940
Chris Masonffbd5172009-04-20 15:50:09 -04002941 ret = submit_extent_page(write_flags, tree, page,
2942 sector, iosize, pg_offset,
2943 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002944 end_bio_extent_writepage,
2945 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002946 if (ret)
2947 SetPageError(page);
2948 }
2949 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002950 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002951 nr++;
2952 }
2953done:
2954 if (nr == 0) {
2955 /* make sure the mapping tag for page dirty gets cleared */
2956 set_page_writeback(page);
2957 end_page_writeback(page);
2958 }
Chris Masond1310b22008-01-24 16:13:08 -05002959 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002960
Chris Mason11c83492009-04-20 15:50:09 -04002961done_unlocked:
2962
Chris Mason2c64c532009-09-02 15:04:12 -04002963 /* drop our reference on any cached states */
2964 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002965 return 0;
2966}
2967
Chris Masond1310b22008-01-24 16:13:08 -05002968/**
Chris Mason4bef0842008-09-08 11:18:08 -04002969 * 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 -05002970 * @mapping: address space structure to write
2971 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2972 * @writepage: function called for each page
2973 * @data: data passed to writepage function
2974 *
2975 * If a page is already under I/O, write_cache_pages() skips it, even
2976 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2977 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2978 * and msync() need to guarantee that all the data which was dirty at the time
2979 * the call was made get new I/O started against them. If wbc->sync_mode is
2980 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2981 * existing IO to complete.
2982 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002983static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002984 struct address_space *mapping,
2985 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002986 writepage_t writepage, void *data,
2987 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002988{
Chris Masond1310b22008-01-24 16:13:08 -05002989 int ret = 0;
2990 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002991 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002992 struct pagevec pvec;
2993 int nr_pages;
2994 pgoff_t index;
2995 pgoff_t end; /* Inclusive */
2996 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00002997 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05002998
Chris Masond1310b22008-01-24 16:13:08 -05002999 pagevec_init(&pvec, 0);
3000 if (wbc->range_cyclic) {
3001 index = mapping->writeback_index; /* Start from prev offset */
3002 end = -1;
3003 } else {
3004 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3005 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003006 scanned = 1;
3007 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003008 if (wbc->sync_mode == WB_SYNC_ALL)
3009 tag = PAGECACHE_TAG_TOWRITE;
3010 else
3011 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003012retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003013 if (wbc->sync_mode == WB_SYNC_ALL)
3014 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003015 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003016 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3017 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003018 unsigned i;
3019
3020 scanned = 1;
3021 for (i = 0; i < nr_pages; i++) {
3022 struct page *page = pvec.pages[i];
3023
3024 /*
3025 * At this point we hold neither mapping->tree_lock nor
3026 * lock on the page itself: the page may be truncated or
3027 * invalidated (changing page->mapping to NULL), or even
3028 * swizzled back from swapper_space to tmpfs file
3029 * mapping
3030 */
Chris Mason01d658f2011-11-01 10:08:06 -04003031 if (tree->ops &&
3032 tree->ops->write_cache_pages_lock_hook) {
3033 tree->ops->write_cache_pages_lock_hook(page,
3034 data, flush_fn);
3035 } else {
3036 if (!trylock_page(page)) {
3037 flush_fn(data);
3038 lock_page(page);
3039 }
3040 }
Chris Masond1310b22008-01-24 16:13:08 -05003041
3042 if (unlikely(page->mapping != mapping)) {
3043 unlock_page(page);
3044 continue;
3045 }
3046
3047 if (!wbc->range_cyclic && page->index > end) {
3048 done = 1;
3049 unlock_page(page);
3050 continue;
3051 }
3052
Chris Masond2c3f4f2008-11-19 12:44:22 -05003053 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003054 if (PageWriteback(page))
3055 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003056 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003057 }
Chris Masond1310b22008-01-24 16:13:08 -05003058
3059 if (PageWriteback(page) ||
3060 !clear_page_dirty_for_io(page)) {
3061 unlock_page(page);
3062 continue;
3063 }
3064
3065 ret = (*writepage)(page, wbc, data);
3066
3067 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3068 unlock_page(page);
3069 ret = 0;
3070 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003071 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003072 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003073
3074 /*
3075 * the filesystem may choose to bump up nr_to_write.
3076 * We have to make sure to honor the new nr_to_write
3077 * at any time
3078 */
3079 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003080 }
3081 pagevec_release(&pvec);
3082 cond_resched();
3083 }
3084 if (!scanned && !done) {
3085 /*
3086 * We hit the last page and there is more work to be done: wrap
3087 * back to the start of the file
3088 */
3089 scanned = 1;
3090 index = 0;
3091 goto retry;
3092 }
Chris Masond1310b22008-01-24 16:13:08 -05003093 return ret;
3094}
Chris Masond1310b22008-01-24 16:13:08 -05003095
Chris Masonffbd5172009-04-20 15:50:09 -04003096static void flush_epd_write_bio(struct extent_page_data *epd)
3097{
3098 if (epd->bio) {
3099 if (epd->sync_io)
3100 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3101 else
3102 submit_one_bio(WRITE, epd->bio, 0, 0);
3103 epd->bio = NULL;
3104 }
3105}
3106
Chris Masond2c3f4f2008-11-19 12:44:22 -05003107static noinline void flush_write_bio(void *data)
3108{
3109 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003110 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003111}
3112
Chris Masond1310b22008-01-24 16:13:08 -05003113int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3114 get_extent_t *get_extent,
3115 struct writeback_control *wbc)
3116{
3117 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003118 struct extent_page_data epd = {
3119 .bio = NULL,
3120 .tree = tree,
3121 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003122 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003123 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003124 };
Chris Masond1310b22008-01-24 16:13:08 -05003125
Chris Masond1310b22008-01-24 16:13:08 -05003126 ret = __extent_writepage(page, wbc, &epd);
3127
Chris Masonffbd5172009-04-20 15:50:09 -04003128 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003129 return ret;
3130}
Chris Masond1310b22008-01-24 16:13:08 -05003131
Chris Mason771ed682008-11-06 22:02:51 -05003132int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3133 u64 start, u64 end, get_extent_t *get_extent,
3134 int mode)
3135{
3136 int ret = 0;
3137 struct address_space *mapping = inode->i_mapping;
3138 struct page *page;
3139 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3140 PAGE_CACHE_SHIFT;
3141
3142 struct extent_page_data epd = {
3143 .bio = NULL,
3144 .tree = tree,
3145 .get_extent = get_extent,
3146 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003147 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003148 };
3149 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003150 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003151 .nr_to_write = nr_pages * 2,
3152 .range_start = start,
3153 .range_end = end + 1,
3154 };
3155
Chris Masond3977122009-01-05 21:25:51 -05003156 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003157 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3158 if (clear_page_dirty_for_io(page))
3159 ret = __extent_writepage(page, &wbc_writepages, &epd);
3160 else {
3161 if (tree->ops && tree->ops->writepage_end_io_hook)
3162 tree->ops->writepage_end_io_hook(page, start,
3163 start + PAGE_CACHE_SIZE - 1,
3164 NULL, 1);
3165 unlock_page(page);
3166 }
3167 page_cache_release(page);
3168 start += PAGE_CACHE_SIZE;
3169 }
3170
Chris Masonffbd5172009-04-20 15:50:09 -04003171 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003172 return ret;
3173}
Chris Masond1310b22008-01-24 16:13:08 -05003174
3175int extent_writepages(struct extent_io_tree *tree,
3176 struct address_space *mapping,
3177 get_extent_t *get_extent,
3178 struct writeback_control *wbc)
3179{
3180 int ret = 0;
3181 struct extent_page_data epd = {
3182 .bio = NULL,
3183 .tree = tree,
3184 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003185 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003186 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003187 };
3188
Chris Mason4bef0842008-09-08 11:18:08 -04003189 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003190 __extent_writepage, &epd,
3191 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003192 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003193 return ret;
3194}
Chris Masond1310b22008-01-24 16:13:08 -05003195
3196int extent_readpages(struct extent_io_tree *tree,
3197 struct address_space *mapping,
3198 struct list_head *pages, unsigned nr_pages,
3199 get_extent_t get_extent)
3200{
3201 struct bio *bio = NULL;
3202 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003203 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003204
Chris Masond1310b22008-01-24 16:13:08 -05003205 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3206 struct page *page = list_entry(pages->prev, struct page, lru);
3207
3208 prefetchw(&page->flags);
3209 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003210 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003211 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003212 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003213 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003214 }
3215 page_cache_release(page);
3216 }
Chris Masond1310b22008-01-24 16:13:08 -05003217 BUG_ON(!list_empty(pages));
3218 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003219 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003220 return 0;
3221}
Chris Masond1310b22008-01-24 16:13:08 -05003222
3223/*
3224 * basic invalidatepage code, this waits on any locked or writeback
3225 * ranges corresponding to the page, and then deletes any extent state
3226 * records from the tree
3227 */
3228int extent_invalidatepage(struct extent_io_tree *tree,
3229 struct page *page, unsigned long offset)
3230{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003231 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003232 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3233 u64 end = start + PAGE_CACHE_SIZE - 1;
3234 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3235
Chris Masond3977122009-01-05 21:25:51 -05003236 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003237 if (start > end)
3238 return 0;
3239
Josef Bacik2ac55d42010-02-03 19:33:23 +00003240 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003241 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003242 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003243 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3244 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003245 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003246 return 0;
3247}
Chris Masond1310b22008-01-24 16:13:08 -05003248
3249/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003250 * a helper for releasepage, this tests for areas of the page that
3251 * are locked or under IO and drops the related state bits if it is safe
3252 * to drop the page.
3253 */
3254int try_release_extent_state(struct extent_map_tree *map,
3255 struct extent_io_tree *tree, struct page *page,
3256 gfp_t mask)
3257{
3258 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3259 u64 end = start + PAGE_CACHE_SIZE - 1;
3260 int ret = 1;
3261
Chris Mason211f90e2008-07-18 11:56:15 -04003262 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003263 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003264 ret = 0;
3265 else {
3266 if ((mask & GFP_NOFS) == GFP_NOFS)
3267 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003268 /*
3269 * at this point we can safely clear everything except the
3270 * locked bit and the nodatasum bit
3271 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003272 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003273 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3274 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003275
3276 /* if clear_extent_bit failed for enomem reasons,
3277 * we can't allow the release to continue.
3278 */
3279 if (ret < 0)
3280 ret = 0;
3281 else
3282 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003283 }
3284 return ret;
3285}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003286
3287/*
Chris Masond1310b22008-01-24 16:13:08 -05003288 * a helper for releasepage. As long as there are no locked extents
3289 * in the range corresponding to the page, both state records and extent
3290 * map records are removed
3291 */
3292int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003293 struct extent_io_tree *tree, struct page *page,
3294 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003295{
3296 struct extent_map *em;
3297 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3298 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003299
Chris Mason70dec802008-01-29 09:59:12 -05003300 if ((mask & __GFP_WAIT) &&
3301 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003302 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003303 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003304 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003305 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003306 em = lookup_extent_mapping(map, start, len);
David Sterbac7040052011-04-19 18:00:01 +02003307 if (IS_ERR_OR_NULL(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04003308 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003309 break;
3310 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003311 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3312 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003313 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003314 free_extent_map(em);
3315 break;
3316 }
3317 if (!test_range_bit(tree, em->start,
3318 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003319 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003320 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003321 remove_extent_mapping(map, em);
3322 /* once for the rb tree */
3323 free_extent_map(em);
3324 }
3325 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003326 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003327
3328 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003329 free_extent_map(em);
3330 }
Chris Masond1310b22008-01-24 16:13:08 -05003331 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003332 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003333}
Chris Masond1310b22008-01-24 16:13:08 -05003334
Chris Masonec29ed52011-02-23 16:23:20 -05003335/*
3336 * helper function for fiemap, which doesn't want to see any holes.
3337 * This maps until we find something past 'last'
3338 */
3339static struct extent_map *get_extent_skip_holes(struct inode *inode,
3340 u64 offset,
3341 u64 last,
3342 get_extent_t *get_extent)
3343{
3344 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3345 struct extent_map *em;
3346 u64 len;
3347
3348 if (offset >= last)
3349 return NULL;
3350
3351 while(1) {
3352 len = last - offset;
3353 if (len == 0)
3354 break;
3355 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3356 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003357 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003358 return em;
3359
3360 /* if this isn't a hole return it */
3361 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3362 em->block_start != EXTENT_MAP_HOLE) {
3363 return em;
3364 }
3365
3366 /* this is a hole, advance to the next extent */
3367 offset = extent_map_end(em);
3368 free_extent_map(em);
3369 if (offset >= last)
3370 break;
3371 }
3372 return NULL;
3373}
3374
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003375int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3376 __u64 start, __u64 len, get_extent_t *get_extent)
3377{
Josef Bacik975f84f2010-11-23 19:36:57 +00003378 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003379 u64 off = start;
3380 u64 max = start + len;
3381 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003382 u32 found_type;
3383 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003384 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003385 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003386 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003387 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003388 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003389 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003390 struct btrfs_path *path;
3391 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003392 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003393 u64 em_start = 0;
3394 u64 em_len = 0;
3395 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003396 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003397
3398 if (len == 0)
3399 return -EINVAL;
3400
Josef Bacik975f84f2010-11-23 19:36:57 +00003401 path = btrfs_alloc_path();
3402 if (!path)
3403 return -ENOMEM;
3404 path->leave_spinning = 1;
3405
Josef Bacik4d479cf2011-11-17 11:34:31 -05003406 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3407 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3408
Chris Masonec29ed52011-02-23 16:23:20 -05003409 /*
3410 * lookup the last file extent. We're not using i_size here
3411 * because there might be preallocation past i_size
3412 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003413 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003414 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003415 if (ret < 0) {
3416 btrfs_free_path(path);
3417 return ret;
3418 }
3419 WARN_ON(!ret);
3420 path->slots[0]--;
3421 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3422 struct btrfs_file_extent_item);
3423 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3424 found_type = btrfs_key_type(&found_key);
3425
Chris Masonec29ed52011-02-23 16:23:20 -05003426 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003427 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003428 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003429 /* have to trust i_size as the end */
3430 last = (u64)-1;
3431 last_for_get_extent = isize;
3432 } else {
3433 /*
3434 * remember the start of the last extent. There are a
3435 * bunch of different factors that go into the length of the
3436 * extent, so its much less complex to remember where it started
3437 */
3438 last = found_key.offset;
3439 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003440 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003441 btrfs_free_path(path);
3442
Chris Masonec29ed52011-02-23 16:23:20 -05003443 /*
3444 * we might have some extents allocated but more delalloc past those
3445 * extents. so, we trust isize unless the start of the last extent is
3446 * beyond isize
3447 */
3448 if (last < isize) {
3449 last = (u64)-1;
3450 last_for_get_extent = isize;
3451 }
3452
Josef Bacik2ac55d42010-02-03 19:33:23 +00003453 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3454 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003455
Josef Bacik4d479cf2011-11-17 11:34:31 -05003456 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003457 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003458 if (!em)
3459 goto out;
3460 if (IS_ERR(em)) {
3461 ret = PTR_ERR(em);
3462 goto out;
3463 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003464
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003465 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003466 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003467
Chris Masonea8efc72011-03-08 11:54:40 -05003468 /* break if the extent we found is outside the range */
3469 if (em->start >= max || extent_map_end(em) < off)
3470 break;
3471
3472 /*
3473 * get_extent may return an extent that starts before our
3474 * requested range. We have to make sure the ranges
3475 * we return to fiemap always move forward and don't
3476 * overlap, so adjust the offsets here
3477 */
3478 em_start = max(em->start, off);
3479
3480 /*
3481 * record the offset from the start of the extent
3482 * for adjusting the disk offset below
3483 */
3484 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003485 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003486 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003487 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003488 disko = 0;
3489 flags = 0;
3490
Chris Masonea8efc72011-03-08 11:54:40 -05003491 /*
3492 * bump off for our next call to get_extent
3493 */
3494 off = extent_map_end(em);
3495 if (off >= max)
3496 end = 1;
3497
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003498 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003499 end = 1;
3500 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003501 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003502 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3503 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003504 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003505 flags |= (FIEMAP_EXTENT_DELALLOC |
3506 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003507 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003508 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003509 }
3510 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3511 flags |= FIEMAP_EXTENT_ENCODED;
3512
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003513 free_extent_map(em);
3514 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003515 if ((em_start >= last) || em_len == (u64)-1 ||
3516 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003517 flags |= FIEMAP_EXTENT_LAST;
3518 end = 1;
3519 }
3520
Chris Masonec29ed52011-02-23 16:23:20 -05003521 /* now scan forward to see if this is really the last extent. */
3522 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3523 get_extent);
3524 if (IS_ERR(em)) {
3525 ret = PTR_ERR(em);
3526 goto out;
3527 }
3528 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003529 flags |= FIEMAP_EXTENT_LAST;
3530 end = 1;
3531 }
Chris Masonec29ed52011-02-23 16:23:20 -05003532 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3533 em_len, flags);
3534 if (ret)
3535 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003536 }
3537out_free:
3538 free_extent_map(em);
3539out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003540 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3541 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003542 return ret;
3543}
3544
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003545inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003546 unsigned long i)
3547{
3548 struct page *p;
3549 struct address_space *mapping;
3550
3551 if (i == 0)
3552 return eb->first_page;
3553 i += eb->start >> PAGE_CACHE_SHIFT;
3554 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003555 if (!mapping)
3556 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003557
3558 /*
3559 * extent_buffer_page is only called after pinning the page
3560 * by increasing the reference count. So we know the page must
3561 * be in the radix tree.
3562 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003563 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003564 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003565 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003566
Chris Masond1310b22008-01-24 16:13:08 -05003567 return p;
3568}
3569
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003570inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003571{
Chris Mason6af118ce2008-07-22 11:18:07 -04003572 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3573 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003574}
3575
Chris Masond1310b22008-01-24 16:13:08 -05003576static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3577 u64 start,
3578 unsigned long len,
3579 gfp_t mask)
3580{
3581 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003582#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003583 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003584#endif
Chris Masond1310b22008-01-24 16:13:08 -05003585
Chris Masond1310b22008-01-24 16:13:08 -05003586 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003587 if (eb == NULL)
3588 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003589 eb->start = start;
3590 eb->len = len;
Chris Masonbd681512011-07-16 15:23:14 -04003591 rwlock_init(&eb->lock);
3592 atomic_set(&eb->write_locks, 0);
3593 atomic_set(&eb->read_locks, 0);
3594 atomic_set(&eb->blocking_readers, 0);
3595 atomic_set(&eb->blocking_writers, 0);
3596 atomic_set(&eb->spinning_readers, 0);
3597 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003598 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003599 init_waitqueue_head(&eb->write_lock_wq);
3600 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003601
Chris Mason39351272009-02-04 09:24:05 -05003602#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003603 spin_lock_irqsave(&leak_lock, flags);
3604 list_add(&eb->leak_list, &buffers);
3605 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003606#endif
Chris Masond1310b22008-01-24 16:13:08 -05003607 atomic_set(&eb->refs, 1);
3608
3609 return eb;
3610}
3611
3612static void __free_extent_buffer(struct extent_buffer *eb)
3613{
Chris Mason39351272009-02-04 09:24:05 -05003614#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003615 unsigned long flags;
3616 spin_lock_irqsave(&leak_lock, flags);
3617 list_del(&eb->leak_list);
3618 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003619#endif
Chris Masond1310b22008-01-24 16:13:08 -05003620 kmem_cache_free(extent_buffer_cache, eb);
3621}
3622
Miao Xie897ca6e2010-10-26 20:57:29 -04003623/*
3624 * Helper for releasing extent buffer page.
3625 */
3626static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3627 unsigned long start_idx)
3628{
3629 unsigned long index;
3630 struct page *page;
3631
3632 if (!eb->first_page)
3633 return;
3634
3635 index = num_extent_pages(eb->start, eb->len);
3636 if (start_idx >= index)
3637 return;
3638
3639 do {
3640 index--;
3641 page = extent_buffer_page(eb, index);
3642 if (page)
3643 page_cache_release(page);
3644 } while (index != start_idx);
3645}
3646
3647/*
3648 * Helper for releasing the extent buffer.
3649 */
3650static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3651{
3652 btrfs_release_extent_buffer_page(eb, 0);
3653 __free_extent_buffer(eb);
3654}
3655
Chris Masond1310b22008-01-24 16:13:08 -05003656struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3657 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003658 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003659{
3660 unsigned long num_pages = num_extent_pages(start, len);
3661 unsigned long i;
3662 unsigned long index = start >> PAGE_CACHE_SHIFT;
3663 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003664 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003665 struct page *p;
3666 struct address_space *mapping = tree->mapping;
3667 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003668 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003669
Miao Xie19fe0a82010-10-26 20:57:29 -04003670 rcu_read_lock();
3671 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3672 if (eb && atomic_inc_not_zero(&eb->refs)) {
3673 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003674 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003675 return eb;
3676 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003677 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003678
David Sterbaba144192011-04-21 01:12:06 +02003679 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003680 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003681 return NULL;
3682
Chris Masond1310b22008-01-24 16:13:08 -05003683 if (page0) {
3684 eb->first_page = page0;
3685 i = 1;
3686 index++;
3687 page_cache_get(page0);
3688 mark_page_accessed(page0);
3689 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003690 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003691 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003692 } else {
3693 i = 0;
3694 }
3695 for (; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003696 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003697 if (!p) {
3698 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003699 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003700 }
3701 set_page_extent_mapped(p);
3702 mark_page_accessed(p);
3703 if (i == 0) {
3704 eb->first_page = p;
3705 set_page_extent_head(p, len);
3706 } else {
3707 set_page_private(p, EXTENT_PAGE_PRIVATE);
3708 }
3709 if (!PageUptodate(p))
3710 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003711
3712 /*
3713 * see below about how we avoid a nasty race with release page
3714 * and why we unlock later
3715 */
3716 if (i != 0)
3717 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003718 }
3719 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003720 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003721
Miao Xie19fe0a82010-10-26 20:57:29 -04003722 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3723 if (ret)
3724 goto free_eb;
3725
Chris Mason6af118ce2008-07-22 11:18:07 -04003726 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003727 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3728 if (ret == -EEXIST) {
3729 exists = radix_tree_lookup(&tree->buffer,
3730 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003731 /* add one reference for the caller */
3732 atomic_inc(&exists->refs);
3733 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003734 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003735 goto free_eb;
3736 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003737 /* add one reference for the tree */
3738 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003739 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003740 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003741
3742 /*
3743 * there is a race where release page may have
3744 * tried to find this extent buffer in the radix
3745 * but failed. It will tell the VM it is safe to
3746 * reclaim the, and it will clear the page private bit.
3747 * We must make sure to set the page private bit properly
3748 * after the extent buffer is in the radix tree so
3749 * it doesn't get lost
3750 */
3751 set_page_extent_mapped(eb->first_page);
3752 set_page_extent_head(eb->first_page, eb->len);
3753 if (!page0)
3754 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003755 return eb;
3756
Chris Mason6af118ce2008-07-22 11:18:07 -04003757free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003758 if (eb->first_page && !page0)
3759 unlock_page(eb->first_page);
3760
Chris Masond1310b22008-01-24 16:13:08 -05003761 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003762 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003763 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003764 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003765}
Chris Masond1310b22008-01-24 16:13:08 -05003766
3767struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003768 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003769{
Chris Masond1310b22008-01-24 16:13:08 -05003770 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003771
Miao Xie19fe0a82010-10-26 20:57:29 -04003772 rcu_read_lock();
3773 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3774 if (eb && atomic_inc_not_zero(&eb->refs)) {
3775 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003776 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003777 return eb;
3778 }
3779 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003780
Miao Xie19fe0a82010-10-26 20:57:29 -04003781 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003782}
Chris Masond1310b22008-01-24 16:13:08 -05003783
3784void free_extent_buffer(struct extent_buffer *eb)
3785{
Chris Masond1310b22008-01-24 16:13:08 -05003786 if (!eb)
3787 return;
3788
3789 if (!atomic_dec_and_test(&eb->refs))
3790 return;
3791
Chris Mason6af118ce2008-07-22 11:18:07 -04003792 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003793}
Chris Masond1310b22008-01-24 16:13:08 -05003794
3795int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3796 struct extent_buffer *eb)
3797{
Chris Masond1310b22008-01-24 16:13:08 -05003798 unsigned long i;
3799 unsigned long num_pages;
3800 struct page *page;
3801
Chris Masond1310b22008-01-24 16:13:08 -05003802 num_pages = num_extent_pages(eb->start, eb->len);
3803
3804 for (i = 0; i < num_pages; i++) {
3805 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003806 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003807 continue;
3808
Chris Masona61e6f22008-07-22 11:18:08 -04003809 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003810 WARN_ON(!PagePrivate(page));
3811
3812 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003813 if (i == 0)
3814 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003815
Chris Masond1310b22008-01-24 16:13:08 -05003816 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003817 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003818 if (!PageDirty(page)) {
3819 radix_tree_tag_clear(&page->mapping->page_tree,
3820 page_index(page),
3821 PAGECACHE_TAG_DIRTY);
3822 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003823 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04003824 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04003825 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003826 }
3827 return 0;
3828}
Chris Masond1310b22008-01-24 16:13:08 -05003829
Chris Masond1310b22008-01-24 16:13:08 -05003830int set_extent_buffer_dirty(struct extent_io_tree *tree,
3831 struct extent_buffer *eb)
3832{
3833 unsigned long i;
3834 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003835 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003836
Chris Masonb9473432009-03-13 11:00:37 -04003837 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003838 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003839 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003840 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003841 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003842}
Chris Masond1310b22008-01-24 16:13:08 -05003843
Chris Mason19b6caf2011-07-25 06:50:50 -04003844static int __eb_straddles_pages(u64 start, u64 len)
3845{
3846 if (len < PAGE_CACHE_SIZE)
3847 return 1;
3848 if (start & (PAGE_CACHE_SIZE - 1))
3849 return 1;
3850 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3851 return 1;
3852 return 0;
3853}
3854
3855static int eb_straddles_pages(struct extent_buffer *eb)
3856{
3857 return __eb_straddles_pages(eb->start, eb->len);
3858}
3859
Chris Mason1259ab72008-05-12 13:39:03 -04003860int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003861 struct extent_buffer *eb,
3862 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003863{
3864 unsigned long i;
3865 struct page *page;
3866 unsigned long num_pages;
3867
3868 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003869 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003870
Chris Mason19b6caf2011-07-25 06:50:50 -04003871 if (eb_straddles_pages(eb)) {
3872 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3873 cached_state, GFP_NOFS);
3874 }
Chris Mason1259ab72008-05-12 13:39:03 -04003875 for (i = 0; i < num_pages; i++) {
3876 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003877 if (page)
3878 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003879 }
3880 return 0;
3881}
3882
Chris Masond1310b22008-01-24 16:13:08 -05003883int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3884 struct extent_buffer *eb)
3885{
3886 unsigned long i;
3887 struct page *page;
3888 unsigned long num_pages;
3889
3890 num_pages = num_extent_pages(eb->start, eb->len);
3891
Chris Mason19b6caf2011-07-25 06:50:50 -04003892 if (eb_straddles_pages(eb)) {
3893 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3894 NULL, GFP_NOFS);
3895 }
Chris Masond1310b22008-01-24 16:13:08 -05003896 for (i = 0; i < num_pages; i++) {
3897 page = extent_buffer_page(eb, i);
3898 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3899 ((i == num_pages - 1) &&
3900 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3901 check_page_uptodate(tree, page);
3902 continue;
3903 }
3904 SetPageUptodate(page);
3905 }
3906 return 0;
3907}
Chris Masond1310b22008-01-24 16:13:08 -05003908
Chris Masonce9adaa2008-04-09 16:28:12 -04003909int extent_range_uptodate(struct extent_io_tree *tree,
3910 u64 start, u64 end)
3911{
3912 struct page *page;
3913 int ret;
3914 int pg_uptodate = 1;
3915 int uptodate;
3916 unsigned long index;
3917
Chris Mason19b6caf2011-07-25 06:50:50 -04003918 if (__eb_straddles_pages(start, end - start + 1)) {
3919 ret = test_range_bit(tree, start, end,
3920 EXTENT_UPTODATE, 1, NULL);
3921 if (ret)
3922 return 1;
3923 }
Chris Masond3977122009-01-05 21:25:51 -05003924 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003925 index = start >> PAGE_CACHE_SHIFT;
3926 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05003927 if (!page)
3928 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04003929 uptodate = PageUptodate(page);
3930 page_cache_release(page);
3931 if (!uptodate) {
3932 pg_uptodate = 0;
3933 break;
3934 }
3935 start += PAGE_CACHE_SIZE;
3936 }
3937 return pg_uptodate;
3938}
3939
Chris Masond1310b22008-01-24 16:13:08 -05003940int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003941 struct extent_buffer *eb,
3942 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003943{
Chris Mason728131d2008-04-09 16:28:12 -04003944 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003945 unsigned long num_pages;
3946 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003947 struct page *page;
3948 int pg_uptodate = 1;
3949
Chris Masonb4ce94d2009-02-04 09:25:08 -05003950 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003951 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003952
Chris Mason19b6caf2011-07-25 06:50:50 -04003953 if (eb_straddles_pages(eb)) {
3954 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3955 EXTENT_UPTODATE, 1, cached_state);
3956 if (ret)
3957 return ret;
3958 }
Chris Mason728131d2008-04-09 16:28:12 -04003959
3960 num_pages = num_extent_pages(eb->start, eb->len);
3961 for (i = 0; i < num_pages; i++) {
3962 page = extent_buffer_page(eb, i);
3963 if (!PageUptodate(page)) {
3964 pg_uptodate = 0;
3965 break;
3966 }
3967 }
Chris Mason42352982008-04-28 16:40:52 -04003968 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003969}
Chris Masond1310b22008-01-24 16:13:08 -05003970
3971int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02003972 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003973 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003974{
3975 unsigned long i;
3976 unsigned long start_i;
3977 struct page *page;
3978 int err;
3979 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003980 int locked_pages = 0;
3981 int all_uptodate = 1;
3982 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003983 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003984 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003985 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003986
Chris Masonb4ce94d2009-02-04 09:25:08 -05003987 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003988 return 0;
3989
Chris Mason19b6caf2011-07-25 06:50:50 -04003990 if (eb_straddles_pages(eb)) {
3991 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3992 EXTENT_UPTODATE, 1, NULL)) {
3993 return 0;
3994 }
Chris Masond1310b22008-01-24 16:13:08 -05003995 }
3996
3997 if (start) {
3998 WARN_ON(start < eb->start);
3999 start_i = (start >> PAGE_CACHE_SHIFT) -
4000 (eb->start >> PAGE_CACHE_SHIFT);
4001 } else {
4002 start_i = 0;
4003 }
4004
4005 num_pages = num_extent_pages(eb->start, eb->len);
4006 for (i = start_i; i < num_pages; i++) {
4007 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004008 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004009 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004010 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004011 } else {
4012 lock_page(page);
4013 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004014 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05004015 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004016 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004017 }
4018 if (all_uptodate) {
4019 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004020 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004021 goto unlock_exit;
4022 }
4023
4024 for (i = start_i; i < num_pages; i++) {
4025 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05004026
4027 WARN_ON(!PagePrivate(page));
4028
4029 set_page_extent_mapped(page);
4030 if (i == 0)
4031 set_page_extent_head(page, eb->len);
4032
Chris Masonce9adaa2008-04-09 16:28:12 -04004033 if (inc_all_pages)
4034 page_cache_get(page);
4035 if (!PageUptodate(page)) {
4036 if (start_i == 0)
4037 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04004038 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004039 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004040 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004041 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004042 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004043 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004044 } else {
4045 unlock_page(page);
4046 }
4047 }
4048
Chris Masona86c12c2008-02-07 10:50:54 -05004049 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04004050 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05004051
Arne Jansenbb82ab82011-06-10 14:06:53 +02004052 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004053 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004054
Chris Masond1310b22008-01-24 16:13:08 -05004055 for (i = start_i; i < num_pages; i++) {
4056 page = extent_buffer_page(eb, i);
4057 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004058 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004059 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004060 }
Chris Masond3977122009-01-05 21:25:51 -05004061
Chris Masond1310b22008-01-24 16:13:08 -05004062 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004063 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004064 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004065
4066unlock_exit:
4067 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004068 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004069 page = extent_buffer_page(eb, i);
4070 i++;
4071 unlock_page(page);
4072 locked_pages--;
4073 }
4074 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004075}
Chris Masond1310b22008-01-24 16:13:08 -05004076
4077void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4078 unsigned long start,
4079 unsigned long len)
4080{
4081 size_t cur;
4082 size_t offset;
4083 struct page *page;
4084 char *kaddr;
4085 char *dst = (char *)dstv;
4086 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4087 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004088
4089 WARN_ON(start > eb->len);
4090 WARN_ON(start + len > eb->start + eb->len);
4091
4092 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4093
Chris Masond3977122009-01-05 21:25:51 -05004094 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004095 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004096
4097 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004098 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004099 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004100
4101 dst += cur;
4102 len -= cur;
4103 offset = 0;
4104 i++;
4105 }
4106}
Chris Masond1310b22008-01-24 16:13:08 -05004107
4108int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004109 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004110 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004111 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004112{
4113 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4114 char *kaddr;
4115 struct page *p;
4116 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4117 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4118 unsigned long end_i = (start_offset + start + min_len - 1) >>
4119 PAGE_CACHE_SHIFT;
4120
4121 if (i != end_i)
4122 return -EINVAL;
4123
4124 if (i == 0) {
4125 offset = start_offset;
4126 *map_start = 0;
4127 } else {
4128 offset = 0;
4129 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4130 }
Chris Masond3977122009-01-05 21:25:51 -05004131
Chris Masond1310b22008-01-24 16:13:08 -05004132 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004133 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4134 "wanted %lu %lu\n", (unsigned long long)eb->start,
4135 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004136 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004137 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004138 }
4139
4140 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004141 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004142 *map = kaddr + offset;
4143 *map_len = PAGE_CACHE_SIZE - offset;
4144 return 0;
4145}
Chris Masond1310b22008-01-24 16:13:08 -05004146
Chris Masond1310b22008-01-24 16:13:08 -05004147int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4148 unsigned long start,
4149 unsigned long len)
4150{
4151 size_t cur;
4152 size_t offset;
4153 struct page *page;
4154 char *kaddr;
4155 char *ptr = (char *)ptrv;
4156 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4157 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4158 int ret = 0;
4159
4160 WARN_ON(start > eb->len);
4161 WARN_ON(start + len > eb->start + eb->len);
4162
4163 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4164
Chris Masond3977122009-01-05 21:25:51 -05004165 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004166 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004167
4168 cur = min(len, (PAGE_CACHE_SIZE - offset));
4169
Chris Masona6591712011-07-19 12:04:14 -04004170 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004171 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004172 if (ret)
4173 break;
4174
4175 ptr += cur;
4176 len -= cur;
4177 offset = 0;
4178 i++;
4179 }
4180 return ret;
4181}
Chris Masond1310b22008-01-24 16:13:08 -05004182
4183void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4184 unsigned long start, unsigned long len)
4185{
4186 size_t cur;
4187 size_t offset;
4188 struct page *page;
4189 char *kaddr;
4190 char *src = (char *)srcv;
4191 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4192 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4193
4194 WARN_ON(start > eb->len);
4195 WARN_ON(start + len > eb->start + eb->len);
4196
4197 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4198
Chris Masond3977122009-01-05 21:25:51 -05004199 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004200 page = extent_buffer_page(eb, i);
4201 WARN_ON(!PageUptodate(page));
4202
4203 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004204 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004205 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004206
4207 src += cur;
4208 len -= cur;
4209 offset = 0;
4210 i++;
4211 }
4212}
Chris Masond1310b22008-01-24 16:13:08 -05004213
4214void memset_extent_buffer(struct extent_buffer *eb, char c,
4215 unsigned long start, unsigned long len)
4216{
4217 size_t cur;
4218 size_t offset;
4219 struct page *page;
4220 char *kaddr;
4221 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4222 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4223
4224 WARN_ON(start > eb->len);
4225 WARN_ON(start + len > eb->start + eb->len);
4226
4227 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4228
Chris Masond3977122009-01-05 21:25:51 -05004229 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004230 page = extent_buffer_page(eb, i);
4231 WARN_ON(!PageUptodate(page));
4232
4233 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004234 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004235 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004236
4237 len -= cur;
4238 offset = 0;
4239 i++;
4240 }
4241}
Chris Masond1310b22008-01-24 16:13:08 -05004242
4243void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4244 unsigned long dst_offset, unsigned long src_offset,
4245 unsigned long len)
4246{
4247 u64 dst_len = dst->len;
4248 size_t cur;
4249 size_t offset;
4250 struct page *page;
4251 char *kaddr;
4252 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4253 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4254
4255 WARN_ON(src->len != dst_len);
4256
4257 offset = (start_offset + dst_offset) &
4258 ((unsigned long)PAGE_CACHE_SIZE - 1);
4259
Chris Masond3977122009-01-05 21:25:51 -05004260 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004261 page = extent_buffer_page(dst, i);
4262 WARN_ON(!PageUptodate(page));
4263
4264 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4265
Chris Masona6591712011-07-19 12:04:14 -04004266 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004267 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004268
4269 src_offset += cur;
4270 len -= cur;
4271 offset = 0;
4272 i++;
4273 }
4274}
Chris Masond1310b22008-01-24 16:13:08 -05004275
4276static void move_pages(struct page *dst_page, struct page *src_page,
4277 unsigned long dst_off, unsigned long src_off,
4278 unsigned long len)
4279{
Chris Masona6591712011-07-19 12:04:14 -04004280 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004281 if (dst_page == src_page) {
4282 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4283 } else {
Chris Masona6591712011-07-19 12:04:14 -04004284 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004285 char *p = dst_kaddr + dst_off + len;
4286 char *s = src_kaddr + src_off + len;
4287
4288 while (len--)
4289 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004290 }
Chris Masond1310b22008-01-24 16:13:08 -05004291}
4292
Sergei Trofimovich33872062011-04-11 21:52:52 +00004293static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4294{
4295 unsigned long distance = (src > dst) ? src - dst : dst - src;
4296 return distance < len;
4297}
4298
Chris Masond1310b22008-01-24 16:13:08 -05004299static void copy_pages(struct page *dst_page, struct page *src_page,
4300 unsigned long dst_off, unsigned long src_off,
4301 unsigned long len)
4302{
Chris Masona6591712011-07-19 12:04:14 -04004303 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004304 char *src_kaddr;
4305
Sergei Trofimovich33872062011-04-11 21:52:52 +00004306 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004307 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004308 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004309 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004310 BUG_ON(areas_overlap(src_off, dst_off, len));
4311 }
Chris Masond1310b22008-01-24 16:13:08 -05004312
4313 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004314}
4315
4316void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4317 unsigned long src_offset, unsigned long len)
4318{
4319 size_t cur;
4320 size_t dst_off_in_page;
4321 size_t src_off_in_page;
4322 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4323 unsigned long dst_i;
4324 unsigned long src_i;
4325
4326 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004327 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4328 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004329 BUG_ON(1);
4330 }
4331 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004332 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4333 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004334 BUG_ON(1);
4335 }
4336
Chris Masond3977122009-01-05 21:25:51 -05004337 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004338 dst_off_in_page = (start_offset + dst_offset) &
4339 ((unsigned long)PAGE_CACHE_SIZE - 1);
4340 src_off_in_page = (start_offset + src_offset) &
4341 ((unsigned long)PAGE_CACHE_SIZE - 1);
4342
4343 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4344 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4345
4346 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4347 src_off_in_page));
4348 cur = min_t(unsigned long, cur,
4349 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4350
4351 copy_pages(extent_buffer_page(dst, dst_i),
4352 extent_buffer_page(dst, src_i),
4353 dst_off_in_page, src_off_in_page, cur);
4354
4355 src_offset += cur;
4356 dst_offset += cur;
4357 len -= cur;
4358 }
4359}
Chris Masond1310b22008-01-24 16:13:08 -05004360
4361void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4362 unsigned long src_offset, unsigned long len)
4363{
4364 size_t cur;
4365 size_t dst_off_in_page;
4366 size_t src_off_in_page;
4367 unsigned long dst_end = dst_offset + len - 1;
4368 unsigned long src_end = src_offset + len - 1;
4369 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4370 unsigned long dst_i;
4371 unsigned long src_i;
4372
4373 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004374 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4375 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004376 BUG_ON(1);
4377 }
4378 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004379 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4380 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004381 BUG_ON(1);
4382 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004383 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004384 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4385 return;
4386 }
Chris Masond3977122009-01-05 21:25:51 -05004387 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004388 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4389 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4390
4391 dst_off_in_page = (start_offset + dst_end) &
4392 ((unsigned long)PAGE_CACHE_SIZE - 1);
4393 src_off_in_page = (start_offset + src_end) &
4394 ((unsigned long)PAGE_CACHE_SIZE - 1);
4395
4396 cur = min_t(unsigned long, len, src_off_in_page + 1);
4397 cur = min(cur, dst_off_in_page + 1);
4398 move_pages(extent_buffer_page(dst, dst_i),
4399 extent_buffer_page(dst, src_i),
4400 dst_off_in_page - cur + 1,
4401 src_off_in_page - cur + 1, cur);
4402
4403 dst_end -= cur;
4404 src_end -= cur;
4405 len -= cur;
4406 }
4407}
Chris Mason6af118ce2008-07-22 11:18:07 -04004408
Miao Xie19fe0a82010-10-26 20:57:29 -04004409static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4410{
4411 struct extent_buffer *eb =
4412 container_of(head, struct extent_buffer, rcu_head);
4413
4414 btrfs_release_extent_buffer(eb);
4415}
4416
Chris Mason6af118ce2008-07-22 11:18:07 -04004417int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4418{
4419 u64 start = page_offset(page);
4420 struct extent_buffer *eb;
4421 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004422
4423 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004424 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004425 if (!eb) {
4426 spin_unlock(&tree->buffer_lock);
4427 return ret;
4428 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004429
Chris Masonb9473432009-03-13 11:00:37 -04004430 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4431 ret = 0;
4432 goto out;
4433 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004434
Miao Xie19fe0a82010-10-26 20:57:29 -04004435 /*
4436 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4437 * Or go back.
4438 */
4439 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4440 ret = 0;
4441 goto out;
4442 }
4443
4444 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004445out:
4446 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004447
4448 /* at this point we can safely release the extent buffer */
4449 if (atomic_read(&eb->refs) == 0)
4450 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004451 return ret;
4452}