blob: 8d6f55fbd28e8a0564d931c9b564a2cd9b8b90d4 [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
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002157 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2158 failrec->this_mirror,
2159 failrec->bio_flags, 0);
2160 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002161}
2162
Chris Masond1310b22008-01-24 16:13:08 -05002163/* lots and lots of room for performance fixes in the end_bio funcs */
2164
Jeff Mahoney87826df2012-02-15 16:23:57 +01002165int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2166{
2167 int uptodate = (err == 0);
2168 struct extent_io_tree *tree;
2169 int ret;
2170
2171 tree = &BTRFS_I(page->mapping->host)->io_tree;
2172
2173 if (tree->ops && tree->ops->writepage_end_io_hook) {
2174 ret = tree->ops->writepage_end_io_hook(page, start,
2175 end, NULL, uptodate);
2176 if (ret)
2177 uptodate = 0;
2178 }
2179
2180 if (!uptodate && tree->ops &&
2181 tree->ops->writepage_io_failed_hook) {
2182 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2183 start, end, NULL);
2184 /* Writeback already completed */
2185 if (ret == 0)
2186 return 1;
2187 }
2188
2189 if (!uptodate) {
2190 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2191 ClearPageUptodate(page);
2192 SetPageError(page);
2193 }
2194 return 0;
2195}
2196
Chris Masond1310b22008-01-24 16:13:08 -05002197/*
2198 * after a writepage IO is done, we need to:
2199 * clear the uptodate bits on error
2200 * clear the writeback bits in the extent tree for this IO
2201 * end_page_writeback if the page has no more pending IO
2202 *
2203 * Scheduling is not allowed, so the extent state tree is expected
2204 * to have one and only one object corresponding to this IO.
2205 */
Chris Masond1310b22008-01-24 16:13:08 -05002206static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002207{
Chris Masond1310b22008-01-24 16:13:08 -05002208 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002209 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002210 u64 start;
2211 u64 end;
2212 int whole_page;
2213
Chris Masond1310b22008-01-24 16:13:08 -05002214 do {
2215 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002216 tree = &BTRFS_I(page->mapping->host)->io_tree;
2217
Chris Masond1310b22008-01-24 16:13:08 -05002218 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2219 bvec->bv_offset;
2220 end = start + bvec->bv_len - 1;
2221
2222 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2223 whole_page = 1;
2224 else
2225 whole_page = 0;
2226
2227 if (--bvec >= bio->bi_io_vec)
2228 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002229
Jeff Mahoney87826df2012-02-15 16:23:57 +01002230 if (end_extent_writepage(page, err, start, end))
2231 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002232
Chris Masond1310b22008-01-24 16:13:08 -05002233 if (whole_page)
2234 end_page_writeback(page);
2235 else
2236 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002237 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002238
Chris Masond1310b22008-01-24 16:13:08 -05002239 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002240}
2241
2242/*
2243 * after a readpage IO is done, we need to:
2244 * clear the uptodate bits on error
2245 * set the uptodate bits if things worked
2246 * set the page up to date if all extents in the tree are uptodate
2247 * clear the lock bit in the extent tree
2248 * unlock the page if there are no other extents locked for it
2249 *
2250 * Scheduling is not allowed, so the extent state tree is expected
2251 * to have one and only one object corresponding to this IO.
2252 */
Chris Masond1310b22008-01-24 16:13:08 -05002253static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002254{
2255 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002256 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2257 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002258 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002259 u64 start;
2260 u64 end;
2261 int whole_page;
2262 int ret;
2263
Chris Masond20f7042008-12-08 16:58:54 -05002264 if (err)
2265 uptodate = 0;
2266
Chris Masond1310b22008-01-24 16:13:08 -05002267 do {
2268 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002269 struct extent_state *cached = NULL;
2270 struct extent_state *state;
2271
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002272 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2273 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2274 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002275 tree = &BTRFS_I(page->mapping->host)->io_tree;
2276
Chris Masond1310b22008-01-24 16:13:08 -05002277 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2278 bvec->bv_offset;
2279 end = start + bvec->bv_len - 1;
2280
2281 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2282 whole_page = 1;
2283 else
2284 whole_page = 0;
2285
Chris Mason4125bf72010-02-03 18:18:45 +00002286 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002287 prefetchw(&bvec->bv_page->flags);
2288
Arne Jansen507903b2011-04-06 10:02:20 +00002289 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002290 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002291 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002292 /*
2293 * take a reference on the state, unlock will drop
2294 * the ref
2295 */
2296 cache_state(state, &cached);
2297 }
2298 spin_unlock(&tree->lock);
2299
Chris Masond1310b22008-01-24 16:13:08 -05002300 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002301 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002302 state);
Chris Masond1310b22008-01-24 16:13:08 -05002303 if (ret)
2304 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002305 else
2306 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002307 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002308 if (!uptodate) {
Jan Schmidt32240a92011-11-20 07:33:38 -05002309 int failed_mirror;
2310 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002311 /*
2312 * The generic bio_readpage_error handles errors the
2313 * following way: If possible, new read requests are
2314 * created and submitted and will end up in
2315 * end_bio_extent_readpage as well (if we're lucky, not
2316 * in the !uptodate case). In that case it returns 0 and
2317 * we just go on with the next page in our bio. If it
2318 * can't handle the error it will return -EIO and we
2319 * remain responsible for that page.
2320 */
2321 ret = bio_readpage_error(bio, page, start, end,
2322 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002323 if (ret == 0) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002324error_handled:
Chris Mason3b951512008-04-17 11:29:12 -04002325 uptodate =
2326 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002327 if (err)
2328 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002329 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002330 continue;
2331 }
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002332 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2333 ret = tree->ops->readpage_io_failed_hook(
2334 bio, page, start, end,
2335 failed_mirror, state);
2336 if (ret == 0)
2337 goto error_handled;
2338 }
Chris Mason7e383262008-04-09 16:28:12 -04002339 }
Chris Mason70dec802008-01-29 09:59:12 -05002340
Chris Mason771ed682008-11-06 22:02:51 -05002341 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002342 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002343 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002344 }
Arne Jansen507903b2011-04-06 10:02:20 +00002345 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002346
Chris Mason70dec802008-01-29 09:59:12 -05002347 if (whole_page) {
2348 if (uptodate) {
2349 SetPageUptodate(page);
2350 } else {
2351 ClearPageUptodate(page);
2352 SetPageError(page);
2353 }
Chris Masond1310b22008-01-24 16:13:08 -05002354 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002355 } else {
2356 if (uptodate) {
2357 check_page_uptodate(tree, page);
2358 } else {
2359 ClearPageUptodate(page);
2360 SetPageError(page);
2361 }
Chris Masond1310b22008-01-24 16:13:08 -05002362 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002363 }
Chris Mason4125bf72010-02-03 18:18:45 +00002364 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002365
2366 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002367}
2368
Miao Xie88f794e2010-11-22 03:02:55 +00002369struct bio *
2370btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2371 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002372{
2373 struct bio *bio;
2374
2375 bio = bio_alloc(gfp_flags, nr_vecs);
2376
2377 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2378 while (!bio && (nr_vecs /= 2))
2379 bio = bio_alloc(gfp_flags, nr_vecs);
2380 }
2381
2382 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002383 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002384 bio->bi_bdev = bdev;
2385 bio->bi_sector = first_sector;
2386 }
2387 return bio;
2388}
2389
Chris Masonc8b97812008-10-29 14:49:59 -04002390static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2391 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002392{
Chris Masond1310b22008-01-24 16:13:08 -05002393 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002394 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2395 struct page *page = bvec->bv_page;
2396 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002397 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002398
2399 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002400
David Woodhouse902b22f2008-08-20 08:51:49 -04002401 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002402
2403 bio_get(bio);
2404
Chris Mason065631f2008-02-20 12:07:25 -05002405 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002406 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002407 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002408 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002409 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002410
Chris Masond1310b22008-01-24 16:13:08 -05002411 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2412 ret = -EOPNOTSUPP;
2413 bio_put(bio);
2414 return ret;
2415}
2416
2417static int submit_extent_page(int rw, struct extent_io_tree *tree,
2418 struct page *page, sector_t sector,
2419 size_t size, unsigned long offset,
2420 struct block_device *bdev,
2421 struct bio **bio_ret,
2422 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002423 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002424 int mirror_num,
2425 unsigned long prev_bio_flags,
2426 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002427{
2428 int ret = 0;
2429 struct bio *bio;
2430 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002431 int contig = 0;
2432 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2433 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002434 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002435
2436 if (bio_ret && *bio_ret) {
2437 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002438 if (old_compressed)
2439 contig = bio->bi_sector == sector;
2440 else
2441 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2442 sector;
2443
2444 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002445 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002446 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2447 bio_flags)) ||
2448 bio_add_page(bio, page, page_size, offset) < page_size) {
2449 ret = submit_one_bio(rw, bio, mirror_num,
2450 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002451 bio = NULL;
2452 } else {
2453 return 0;
2454 }
2455 }
Chris Masonc8b97812008-10-29 14:49:59 -04002456 if (this_compressed)
2457 nr = BIO_MAX_PAGES;
2458 else
2459 nr = bio_get_nr_vecs(bdev);
2460
Miao Xie88f794e2010-11-22 03:02:55 +00002461 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002462 if (!bio)
2463 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002464
Chris Masonc8b97812008-10-29 14:49:59 -04002465 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002466 bio->bi_end_io = end_io_func;
2467 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002468
Chris Masond3977122009-01-05 21:25:51 -05002469 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002470 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002471 else
Chris Masonc8b97812008-10-29 14:49:59 -04002472 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002473
2474 return ret;
2475}
2476
2477void set_page_extent_mapped(struct page *page)
2478{
2479 if (!PagePrivate(page)) {
2480 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002481 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002482 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002483 }
2484}
2485
Christoph Hellwigb2950862008-12-02 09:54:17 -05002486static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05002487{
Chris Masoneb14ab82011-02-10 12:35:00 -05002488 WARN_ON(!PagePrivate(page));
Chris Masond1310b22008-01-24 16:13:08 -05002489 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2490}
2491
2492/*
2493 * basic readpage implementation. Locked extent state structs are inserted
2494 * into the tree that are removed when the IO is done (by the end_io
2495 * handlers)
2496 */
2497static int __extent_read_full_page(struct extent_io_tree *tree,
2498 struct page *page,
2499 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002500 struct bio **bio, int mirror_num,
2501 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002502{
2503 struct inode *inode = page->mapping->host;
2504 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2505 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2506 u64 end;
2507 u64 cur = start;
2508 u64 extent_offset;
2509 u64 last_byte = i_size_read(inode);
2510 u64 block_start;
2511 u64 cur_end;
2512 sector_t sector;
2513 struct extent_map *em;
2514 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002515 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002516 int ret;
2517 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002518 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002519 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002520 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002521 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002522 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002523
2524 set_page_extent_mapped(page);
2525
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002526 if (!PageUptodate(page)) {
2527 if (cleancache_get_page(page) == 0) {
2528 BUG_ON(blocksize != PAGE_SIZE);
2529 goto out;
2530 }
2531 }
2532
Chris Masond1310b22008-01-24 16:13:08 -05002533 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002534 while (1) {
2535 lock_extent(tree, start, end, GFP_NOFS);
2536 ordered = btrfs_lookup_ordered_extent(inode, start);
2537 if (!ordered)
2538 break;
2539 unlock_extent(tree, start, end, GFP_NOFS);
2540 btrfs_start_ordered_extent(inode, ordered, 1);
2541 btrfs_put_ordered_extent(ordered);
2542 }
Chris Masond1310b22008-01-24 16:13:08 -05002543
Chris Masonc8b97812008-10-29 14:49:59 -04002544 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2545 char *userpage;
2546 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2547
2548 if (zero_offset) {
2549 iosize = PAGE_CACHE_SIZE - zero_offset;
2550 userpage = kmap_atomic(page, KM_USER0);
2551 memset(userpage + zero_offset, 0, iosize);
2552 flush_dcache_page(page);
2553 kunmap_atomic(userpage, KM_USER0);
2554 }
2555 }
Chris Masond1310b22008-01-24 16:13:08 -05002556 while (cur <= end) {
2557 if (cur >= last_byte) {
2558 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002559 struct extent_state *cached = NULL;
2560
David Sterba306e16c2011-04-19 14:29:38 +02002561 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002562 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002563 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002564 flush_dcache_page(page);
2565 kunmap_atomic(userpage, KM_USER0);
2566 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002567 &cached, GFP_NOFS);
2568 unlock_extent_cached(tree, cur, cur + iosize - 1,
2569 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002570 break;
2571 }
David Sterba306e16c2011-04-19 14:29:38 +02002572 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002573 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002574 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002575 SetPageError(page);
2576 unlock_extent(tree, cur, end, GFP_NOFS);
2577 break;
2578 }
Chris Masond1310b22008-01-24 16:13:08 -05002579 extent_offset = cur - em->start;
2580 BUG_ON(extent_map_end(em) <= cur);
2581 BUG_ON(end < cur);
2582
Li Zefan261507a02010-12-17 14:21:50 +08002583 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002584 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002585 extent_set_compress_type(&this_bio_flag,
2586 em->compress_type);
2587 }
Chris Masonc8b97812008-10-29 14:49:59 -04002588
Chris Masond1310b22008-01-24 16:13:08 -05002589 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2590 cur_end = min(extent_map_end(em) - 1, end);
2591 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002592 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2593 disk_io_size = em->block_len;
2594 sector = em->block_start >> 9;
2595 } else {
2596 sector = (em->block_start + extent_offset) >> 9;
2597 disk_io_size = iosize;
2598 }
Chris Masond1310b22008-01-24 16:13:08 -05002599 bdev = em->bdev;
2600 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002601 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2602 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002603 free_extent_map(em);
2604 em = NULL;
2605
2606 /* we've found a hole, just zero and go on */
2607 if (block_start == EXTENT_MAP_HOLE) {
2608 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002609 struct extent_state *cached = NULL;
2610
Chris Masond1310b22008-01-24 16:13:08 -05002611 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002612 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002613 flush_dcache_page(page);
2614 kunmap_atomic(userpage, KM_USER0);
2615
2616 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002617 &cached, GFP_NOFS);
2618 unlock_extent_cached(tree, cur, cur + iosize - 1,
2619 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002620 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002621 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002622 continue;
2623 }
2624 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002625 if (test_range_bit(tree, cur, cur_end,
2626 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002627 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002628 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2629 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002630 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002631 continue;
2632 }
Chris Mason70dec802008-01-29 09:59:12 -05002633 /* we have an inline extent but it didn't get marked up
2634 * to date. Error out
2635 */
2636 if (block_start == EXTENT_MAP_INLINE) {
2637 SetPageError(page);
2638 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2639 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002640 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002641 continue;
2642 }
Chris Masond1310b22008-01-24 16:13:08 -05002643
2644 ret = 0;
2645 if (tree->ops && tree->ops->readpage_io_hook) {
2646 ret = tree->ops->readpage_io_hook(page, cur,
2647 cur + iosize - 1);
2648 }
2649 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002650 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2651 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002652 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002653 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002654 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002655 end_bio_extent_readpage, mirror_num,
2656 *bio_flags,
2657 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002658 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002659 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002660 }
2661 if (ret)
2662 SetPageError(page);
2663 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002664 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002665 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002666out:
Chris Masond1310b22008-01-24 16:13:08 -05002667 if (!nr) {
2668 if (!PageError(page))
2669 SetPageUptodate(page);
2670 unlock_page(page);
2671 }
2672 return 0;
2673}
2674
2675int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002676 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002677{
2678 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002679 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002680 int ret;
2681
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002682 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002683 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002684 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002685 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002686 return ret;
2687}
Chris Masond1310b22008-01-24 16:13:08 -05002688
Chris Mason11c83492009-04-20 15:50:09 -04002689static noinline void update_nr_written(struct page *page,
2690 struct writeback_control *wbc,
2691 unsigned long nr_written)
2692{
2693 wbc->nr_to_write -= nr_written;
2694 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2695 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2696 page->mapping->writeback_index = page->index + nr_written;
2697}
2698
Chris Masond1310b22008-01-24 16:13:08 -05002699/*
2700 * the writepage semantics are similar to regular writepage. extent
2701 * records are inserted to lock ranges in the tree, and as dirty areas
2702 * are found, they are marked writeback. Then the lock bits are removed
2703 * and the end_io handler clears the writeback ranges
2704 */
2705static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2706 void *data)
2707{
2708 struct inode *inode = page->mapping->host;
2709 struct extent_page_data *epd = data;
2710 struct extent_io_tree *tree = epd->tree;
2711 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2712 u64 delalloc_start;
2713 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2714 u64 end;
2715 u64 cur = start;
2716 u64 extent_offset;
2717 u64 last_byte = i_size_read(inode);
2718 u64 block_start;
2719 u64 iosize;
2720 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002721 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002722 struct extent_map *em;
2723 struct block_device *bdev;
2724 int ret;
2725 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002726 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002727 size_t blocksize;
2728 loff_t i_size = i_size_read(inode);
2729 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2730 u64 nr_delalloc;
2731 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002732 int page_started;
2733 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002734 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002735 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002736 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002737
Chris Masonffbd5172009-04-20 15:50:09 -04002738 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002739 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002740 else
2741 write_flags = WRITE;
2742
liubo1abe9b82011-03-24 11:18:59 +00002743 trace___extent_writepage(page, inode, wbc);
2744
Chris Masond1310b22008-01-24 16:13:08 -05002745 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002746
2747 ClearPageError(page);
2748
Chris Mason7f3c74f2008-07-18 12:01:11 -04002749 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002750 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002751 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002752 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002753 unlock_page(page);
2754 return 0;
2755 }
2756
2757 if (page->index == end_index) {
2758 char *userpage;
2759
Chris Masond1310b22008-01-24 16:13:08 -05002760 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002761 memset(userpage + pg_offset, 0,
2762 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002763 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002764 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002765 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002766 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002767
2768 set_page_extent_mapped(page);
2769
Josef Bacik9e487102011-08-01 12:08:18 -04002770 if (!tree->ops || !tree->ops->fill_delalloc)
2771 fill_delalloc = false;
2772
Chris Masond1310b22008-01-24 16:13:08 -05002773 delalloc_start = start;
2774 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002775 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002776 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002777 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002778 /*
2779 * make sure the wbc mapping index is at least updated
2780 * to this page.
2781 */
2782 update_nr_written(page, wbc, 0);
2783
Chris Masond3977122009-01-05 21:25:51 -05002784 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002785 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002786 page,
2787 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002788 &delalloc_end,
2789 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002790 if (nr_delalloc == 0) {
2791 delalloc_start = delalloc_end + 1;
2792 continue;
2793 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002794 ret = tree->ops->fill_delalloc(inode, page,
2795 delalloc_start,
2796 delalloc_end,
2797 &page_started,
2798 &nr_written);
2799 BUG_ON(ret);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002800 /*
2801 * delalloc_end is already one less than the total
2802 * length, so we don't subtract one from
2803 * PAGE_CACHE_SIZE
2804 */
2805 delalloc_to_write += (delalloc_end - delalloc_start +
2806 PAGE_CACHE_SIZE) >>
2807 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002808 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002809 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002810 if (wbc->nr_to_write < delalloc_to_write) {
2811 int thresh = 8192;
2812
2813 if (delalloc_to_write < thresh * 2)
2814 thresh = delalloc_to_write;
2815 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2816 thresh);
2817 }
Chris Masonc8b97812008-10-29 14:49:59 -04002818
Chris Mason771ed682008-11-06 22:02:51 -05002819 /* did the fill delalloc function already unlock and start
2820 * the IO?
2821 */
2822 if (page_started) {
2823 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002824 /*
2825 * we've unlocked the page, so we can't update
2826 * the mapping's writeback index, just update
2827 * nr_to_write.
2828 */
2829 wbc->nr_to_write -= nr_written;
2830 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002831 }
Chris Masonc8b97812008-10-29 14:49:59 -04002832 }
Chris Mason247e7432008-07-17 12:53:51 -04002833 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002834 ret = tree->ops->writepage_start_hook(page, start,
2835 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002836 if (ret) {
2837 /* Fixup worker will requeue */
2838 if (ret == -EBUSY)
2839 wbc->pages_skipped++;
2840 else
2841 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002842 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002843 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002844 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002845 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002846 }
2847 }
2848
Chris Mason11c83492009-04-20 15:50:09 -04002849 /*
2850 * we don't want to touch the inode after unlocking the page,
2851 * so we update the mapping writeback index now
2852 */
2853 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002854
Chris Masond1310b22008-01-24 16:13:08 -05002855 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002856 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857 if (tree->ops && tree->ops->writepage_end_io_hook)
2858 tree->ops->writepage_end_io_hook(page, start,
2859 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002860 goto done;
2861 }
2862
Chris Masond1310b22008-01-24 16:13:08 -05002863 blocksize = inode->i_sb->s_blocksize;
2864
2865 while (cur <= end) {
2866 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002867 if (tree->ops && tree->ops->writepage_end_io_hook)
2868 tree->ops->writepage_end_io_hook(page, cur,
2869 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002870 break;
2871 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002872 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002873 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002874 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002875 SetPageError(page);
2876 break;
2877 }
2878
2879 extent_offset = cur - em->start;
2880 BUG_ON(extent_map_end(em) <= cur);
2881 BUG_ON(end < cur);
2882 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2883 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2884 sector = (em->block_start + extent_offset) >> 9;
2885 bdev = em->bdev;
2886 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002887 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002888 free_extent_map(em);
2889 em = NULL;
2890
Chris Masonc8b97812008-10-29 14:49:59 -04002891 /*
2892 * compressed and inline extents are written through other
2893 * paths in the FS
2894 */
2895 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002896 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002897 /*
2898 * end_io notification does not happen here for
2899 * compressed extents
2900 */
2901 if (!compressed && tree->ops &&
2902 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002903 tree->ops->writepage_end_io_hook(page, cur,
2904 cur + iosize - 1,
2905 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002906 else if (compressed) {
2907 /* we don't want to end_page_writeback on
2908 * a compressed extent. this happens
2909 * elsewhere
2910 */
2911 nr++;
2912 }
2913
2914 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002915 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002916 continue;
2917 }
Chris Masond1310b22008-01-24 16:13:08 -05002918 /* leave this out until we have a page_mkwrite call */
2919 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002920 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002921 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002922 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002923 continue;
2924 }
Chris Masonc8b97812008-10-29 14:49:59 -04002925
Chris Masond1310b22008-01-24 16:13:08 -05002926 if (tree->ops && tree->ops->writepage_io_hook) {
2927 ret = tree->ops->writepage_io_hook(page, cur,
2928 cur + iosize - 1);
2929 } else {
2930 ret = 0;
2931 }
Chris Mason1259ab72008-05-12 13:39:03 -04002932 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002933 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002934 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002935 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002936
Chris Masond1310b22008-01-24 16:13:08 -05002937 set_range_writeback(tree, cur, cur + iosize - 1);
2938 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002939 printk(KERN_ERR "btrfs warning page %lu not "
2940 "writeback, cur %llu end %llu\n",
2941 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002942 (unsigned long long)end);
2943 }
2944
Chris Masonffbd5172009-04-20 15:50:09 -04002945 ret = submit_extent_page(write_flags, tree, page,
2946 sector, iosize, pg_offset,
2947 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002948 end_bio_extent_writepage,
2949 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002950 if (ret)
2951 SetPageError(page);
2952 }
2953 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002954 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002955 nr++;
2956 }
2957done:
2958 if (nr == 0) {
2959 /* make sure the mapping tag for page dirty gets cleared */
2960 set_page_writeback(page);
2961 end_page_writeback(page);
2962 }
Chris Masond1310b22008-01-24 16:13:08 -05002963 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002964
Chris Mason11c83492009-04-20 15:50:09 -04002965done_unlocked:
2966
Chris Mason2c64c532009-09-02 15:04:12 -04002967 /* drop our reference on any cached states */
2968 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002969 return 0;
2970}
2971
Chris Masond1310b22008-01-24 16:13:08 -05002972/**
Chris Mason4bef0842008-09-08 11:18:08 -04002973 * 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 -05002974 * @mapping: address space structure to write
2975 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2976 * @writepage: function called for each page
2977 * @data: data passed to writepage function
2978 *
2979 * If a page is already under I/O, write_cache_pages() skips it, even
2980 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2981 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2982 * and msync() need to guarantee that all the data which was dirty at the time
2983 * the call was made get new I/O started against them. If wbc->sync_mode is
2984 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2985 * existing IO to complete.
2986 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002987static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002988 struct address_space *mapping,
2989 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002990 writepage_t writepage, void *data,
2991 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002992{
Chris Masond1310b22008-01-24 16:13:08 -05002993 int ret = 0;
2994 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002995 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002996 struct pagevec pvec;
2997 int nr_pages;
2998 pgoff_t index;
2999 pgoff_t end; /* Inclusive */
3000 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003001 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003002
Chris Masond1310b22008-01-24 16:13:08 -05003003 pagevec_init(&pvec, 0);
3004 if (wbc->range_cyclic) {
3005 index = mapping->writeback_index; /* Start from prev offset */
3006 end = -1;
3007 } else {
3008 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3009 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003010 scanned = 1;
3011 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003012 if (wbc->sync_mode == WB_SYNC_ALL)
3013 tag = PAGECACHE_TAG_TOWRITE;
3014 else
3015 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003016retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003017 if (wbc->sync_mode == WB_SYNC_ALL)
3018 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003019 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003020 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3021 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003022 unsigned i;
3023
3024 scanned = 1;
3025 for (i = 0; i < nr_pages; i++) {
3026 struct page *page = pvec.pages[i];
3027
3028 /*
3029 * At this point we hold neither mapping->tree_lock nor
3030 * lock on the page itself: the page may be truncated or
3031 * invalidated (changing page->mapping to NULL), or even
3032 * swizzled back from swapper_space to tmpfs file
3033 * mapping
3034 */
Chris Mason01d658f2011-11-01 10:08:06 -04003035 if (tree->ops &&
3036 tree->ops->write_cache_pages_lock_hook) {
3037 tree->ops->write_cache_pages_lock_hook(page,
3038 data, flush_fn);
3039 } else {
3040 if (!trylock_page(page)) {
3041 flush_fn(data);
3042 lock_page(page);
3043 }
3044 }
Chris Masond1310b22008-01-24 16:13:08 -05003045
3046 if (unlikely(page->mapping != mapping)) {
3047 unlock_page(page);
3048 continue;
3049 }
3050
3051 if (!wbc->range_cyclic && page->index > end) {
3052 done = 1;
3053 unlock_page(page);
3054 continue;
3055 }
3056
Chris Masond2c3f4f2008-11-19 12:44:22 -05003057 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003058 if (PageWriteback(page))
3059 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003060 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003061 }
Chris Masond1310b22008-01-24 16:13:08 -05003062
3063 if (PageWriteback(page) ||
3064 !clear_page_dirty_for_io(page)) {
3065 unlock_page(page);
3066 continue;
3067 }
3068
3069 ret = (*writepage)(page, wbc, data);
3070
3071 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3072 unlock_page(page);
3073 ret = 0;
3074 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003075 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003076 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003077
3078 /*
3079 * the filesystem may choose to bump up nr_to_write.
3080 * We have to make sure to honor the new nr_to_write
3081 * at any time
3082 */
3083 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003084 }
3085 pagevec_release(&pvec);
3086 cond_resched();
3087 }
3088 if (!scanned && !done) {
3089 /*
3090 * We hit the last page and there is more work to be done: wrap
3091 * back to the start of the file
3092 */
3093 scanned = 1;
3094 index = 0;
3095 goto retry;
3096 }
Chris Masond1310b22008-01-24 16:13:08 -05003097 return ret;
3098}
Chris Masond1310b22008-01-24 16:13:08 -05003099
Chris Masonffbd5172009-04-20 15:50:09 -04003100static void flush_epd_write_bio(struct extent_page_data *epd)
3101{
3102 if (epd->bio) {
3103 if (epd->sync_io)
3104 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3105 else
3106 submit_one_bio(WRITE, epd->bio, 0, 0);
3107 epd->bio = NULL;
3108 }
3109}
3110
Chris Masond2c3f4f2008-11-19 12:44:22 -05003111static noinline void flush_write_bio(void *data)
3112{
3113 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003114 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003115}
3116
Chris Masond1310b22008-01-24 16:13:08 -05003117int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3118 get_extent_t *get_extent,
3119 struct writeback_control *wbc)
3120{
3121 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003122 struct extent_page_data epd = {
3123 .bio = NULL,
3124 .tree = tree,
3125 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003126 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003127 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003128 };
Chris Masond1310b22008-01-24 16:13:08 -05003129
Chris Masond1310b22008-01-24 16:13:08 -05003130 ret = __extent_writepage(page, wbc, &epd);
3131
Chris Masonffbd5172009-04-20 15:50:09 -04003132 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003133 return ret;
3134}
Chris Masond1310b22008-01-24 16:13:08 -05003135
Chris Mason771ed682008-11-06 22:02:51 -05003136int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3137 u64 start, u64 end, get_extent_t *get_extent,
3138 int mode)
3139{
3140 int ret = 0;
3141 struct address_space *mapping = inode->i_mapping;
3142 struct page *page;
3143 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3144 PAGE_CACHE_SHIFT;
3145
3146 struct extent_page_data epd = {
3147 .bio = NULL,
3148 .tree = tree,
3149 .get_extent = get_extent,
3150 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003151 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003152 };
3153 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003154 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003155 .nr_to_write = nr_pages * 2,
3156 .range_start = start,
3157 .range_end = end + 1,
3158 };
3159
Chris Masond3977122009-01-05 21:25:51 -05003160 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003161 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3162 if (clear_page_dirty_for_io(page))
3163 ret = __extent_writepage(page, &wbc_writepages, &epd);
3164 else {
3165 if (tree->ops && tree->ops->writepage_end_io_hook)
3166 tree->ops->writepage_end_io_hook(page, start,
3167 start + PAGE_CACHE_SIZE - 1,
3168 NULL, 1);
3169 unlock_page(page);
3170 }
3171 page_cache_release(page);
3172 start += PAGE_CACHE_SIZE;
3173 }
3174
Chris Masonffbd5172009-04-20 15:50:09 -04003175 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003176 return ret;
3177}
Chris Masond1310b22008-01-24 16:13:08 -05003178
3179int extent_writepages(struct extent_io_tree *tree,
3180 struct address_space *mapping,
3181 get_extent_t *get_extent,
3182 struct writeback_control *wbc)
3183{
3184 int ret = 0;
3185 struct extent_page_data epd = {
3186 .bio = NULL,
3187 .tree = tree,
3188 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003189 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003190 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003191 };
3192
Chris Mason4bef0842008-09-08 11:18:08 -04003193 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003194 __extent_writepage, &epd,
3195 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003196 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003197 return ret;
3198}
Chris Masond1310b22008-01-24 16:13:08 -05003199
3200int extent_readpages(struct extent_io_tree *tree,
3201 struct address_space *mapping,
3202 struct list_head *pages, unsigned nr_pages,
3203 get_extent_t get_extent)
3204{
3205 struct bio *bio = NULL;
3206 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003207 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003208
Chris Masond1310b22008-01-24 16:13:08 -05003209 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3210 struct page *page = list_entry(pages->prev, struct page, lru);
3211
3212 prefetchw(&page->flags);
3213 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003214 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003215 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003216 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003217 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003218 }
3219 page_cache_release(page);
3220 }
Chris Masond1310b22008-01-24 16:13:08 -05003221 BUG_ON(!list_empty(pages));
3222 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003223 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003224 return 0;
3225}
Chris Masond1310b22008-01-24 16:13:08 -05003226
3227/*
3228 * basic invalidatepage code, this waits on any locked or writeback
3229 * ranges corresponding to the page, and then deletes any extent state
3230 * records from the tree
3231 */
3232int extent_invalidatepage(struct extent_io_tree *tree,
3233 struct page *page, unsigned long offset)
3234{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003235 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003236 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3237 u64 end = start + PAGE_CACHE_SIZE - 1;
3238 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3239
Chris Masond3977122009-01-05 21:25:51 -05003240 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003241 if (start > end)
3242 return 0;
3243
Josef Bacik2ac55d42010-02-03 19:33:23 +00003244 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003245 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003246 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003247 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3248 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003249 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003250 return 0;
3251}
Chris Masond1310b22008-01-24 16:13:08 -05003252
3253/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003254 * a helper for releasepage, this tests for areas of the page that
3255 * are locked or under IO and drops the related state bits if it is safe
3256 * to drop the page.
3257 */
3258int try_release_extent_state(struct extent_map_tree *map,
3259 struct extent_io_tree *tree, struct page *page,
3260 gfp_t mask)
3261{
3262 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3263 u64 end = start + PAGE_CACHE_SIZE - 1;
3264 int ret = 1;
3265
Chris Mason211f90e2008-07-18 11:56:15 -04003266 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003267 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003268 ret = 0;
3269 else {
3270 if ((mask & GFP_NOFS) == GFP_NOFS)
3271 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003272 /*
3273 * at this point we can safely clear everything except the
3274 * locked bit and the nodatasum bit
3275 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003276 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003277 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3278 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003279
3280 /* if clear_extent_bit failed for enomem reasons,
3281 * we can't allow the release to continue.
3282 */
3283 if (ret < 0)
3284 ret = 0;
3285 else
3286 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003287 }
3288 return ret;
3289}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003290
3291/*
Chris Masond1310b22008-01-24 16:13:08 -05003292 * a helper for releasepage. As long as there are no locked extents
3293 * in the range corresponding to the page, both state records and extent
3294 * map records are removed
3295 */
3296int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003297 struct extent_io_tree *tree, struct page *page,
3298 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003299{
3300 struct extent_map *em;
3301 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3302 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003303
Chris Mason70dec802008-01-29 09:59:12 -05003304 if ((mask & __GFP_WAIT) &&
3305 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003306 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003307 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003308 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003309 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003310 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003311 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003312 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003313 break;
3314 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003315 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3316 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003317 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003318 free_extent_map(em);
3319 break;
3320 }
3321 if (!test_range_bit(tree, em->start,
3322 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003323 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003324 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003325 remove_extent_mapping(map, em);
3326 /* once for the rb tree */
3327 free_extent_map(em);
3328 }
3329 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003330 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003331
3332 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003333 free_extent_map(em);
3334 }
Chris Masond1310b22008-01-24 16:13:08 -05003335 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003336 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003337}
Chris Masond1310b22008-01-24 16:13:08 -05003338
Chris Masonec29ed52011-02-23 16:23:20 -05003339/*
3340 * helper function for fiemap, which doesn't want to see any holes.
3341 * This maps until we find something past 'last'
3342 */
3343static struct extent_map *get_extent_skip_holes(struct inode *inode,
3344 u64 offset,
3345 u64 last,
3346 get_extent_t *get_extent)
3347{
3348 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3349 struct extent_map *em;
3350 u64 len;
3351
3352 if (offset >= last)
3353 return NULL;
3354
3355 while(1) {
3356 len = last - offset;
3357 if (len == 0)
3358 break;
3359 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3360 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003361 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003362 return em;
3363
3364 /* if this isn't a hole return it */
3365 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3366 em->block_start != EXTENT_MAP_HOLE) {
3367 return em;
3368 }
3369
3370 /* this is a hole, advance to the next extent */
3371 offset = extent_map_end(em);
3372 free_extent_map(em);
3373 if (offset >= last)
3374 break;
3375 }
3376 return NULL;
3377}
3378
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003379int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3380 __u64 start, __u64 len, get_extent_t *get_extent)
3381{
Josef Bacik975f84f2010-11-23 19:36:57 +00003382 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003383 u64 off = start;
3384 u64 max = start + len;
3385 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003386 u32 found_type;
3387 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003388 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003389 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003390 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003391 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003392 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003393 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003394 struct btrfs_path *path;
3395 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003396 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003397 u64 em_start = 0;
3398 u64 em_len = 0;
3399 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003400 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003401
3402 if (len == 0)
3403 return -EINVAL;
3404
Josef Bacik975f84f2010-11-23 19:36:57 +00003405 path = btrfs_alloc_path();
3406 if (!path)
3407 return -ENOMEM;
3408 path->leave_spinning = 1;
3409
Josef Bacik4d479cf2011-11-17 11:34:31 -05003410 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3411 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3412
Chris Masonec29ed52011-02-23 16:23:20 -05003413 /*
3414 * lookup the last file extent. We're not using i_size here
3415 * because there might be preallocation past i_size
3416 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003417 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003418 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003419 if (ret < 0) {
3420 btrfs_free_path(path);
3421 return ret;
3422 }
3423 WARN_ON(!ret);
3424 path->slots[0]--;
3425 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3426 struct btrfs_file_extent_item);
3427 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3428 found_type = btrfs_key_type(&found_key);
3429
Chris Masonec29ed52011-02-23 16:23:20 -05003430 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003431 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003432 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003433 /* have to trust i_size as the end */
3434 last = (u64)-1;
3435 last_for_get_extent = isize;
3436 } else {
3437 /*
3438 * remember the start of the last extent. There are a
3439 * bunch of different factors that go into the length of the
3440 * extent, so its much less complex to remember where it started
3441 */
3442 last = found_key.offset;
3443 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003444 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003445 btrfs_free_path(path);
3446
Chris Masonec29ed52011-02-23 16:23:20 -05003447 /*
3448 * we might have some extents allocated but more delalloc past those
3449 * extents. so, we trust isize unless the start of the last extent is
3450 * beyond isize
3451 */
3452 if (last < isize) {
3453 last = (u64)-1;
3454 last_for_get_extent = isize;
3455 }
3456
Josef Bacik2ac55d42010-02-03 19:33:23 +00003457 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3458 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003459
Josef Bacik4d479cf2011-11-17 11:34:31 -05003460 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003461 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003462 if (!em)
3463 goto out;
3464 if (IS_ERR(em)) {
3465 ret = PTR_ERR(em);
3466 goto out;
3467 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003468
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003469 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003470 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003471
Chris Masonea8efc72011-03-08 11:54:40 -05003472 /* break if the extent we found is outside the range */
3473 if (em->start >= max || extent_map_end(em) < off)
3474 break;
3475
3476 /*
3477 * get_extent may return an extent that starts before our
3478 * requested range. We have to make sure the ranges
3479 * we return to fiemap always move forward and don't
3480 * overlap, so adjust the offsets here
3481 */
3482 em_start = max(em->start, off);
3483
3484 /*
3485 * record the offset from the start of the extent
3486 * for adjusting the disk offset below
3487 */
3488 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003489 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003490 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003491 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003492 disko = 0;
3493 flags = 0;
3494
Chris Masonea8efc72011-03-08 11:54:40 -05003495 /*
3496 * bump off for our next call to get_extent
3497 */
3498 off = extent_map_end(em);
3499 if (off >= max)
3500 end = 1;
3501
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003502 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003503 end = 1;
3504 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003505 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003506 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3507 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003508 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003509 flags |= (FIEMAP_EXTENT_DELALLOC |
3510 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003511 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003512 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003513 }
3514 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3515 flags |= FIEMAP_EXTENT_ENCODED;
3516
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003517 free_extent_map(em);
3518 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003519 if ((em_start >= last) || em_len == (u64)-1 ||
3520 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003521 flags |= FIEMAP_EXTENT_LAST;
3522 end = 1;
3523 }
3524
Chris Masonec29ed52011-02-23 16:23:20 -05003525 /* now scan forward to see if this is really the last extent. */
3526 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3527 get_extent);
3528 if (IS_ERR(em)) {
3529 ret = PTR_ERR(em);
3530 goto out;
3531 }
3532 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003533 flags |= FIEMAP_EXTENT_LAST;
3534 end = 1;
3535 }
Chris Masonec29ed52011-02-23 16:23:20 -05003536 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3537 em_len, flags);
3538 if (ret)
3539 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003540 }
3541out_free:
3542 free_extent_map(em);
3543out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003544 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3545 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003546 return ret;
3547}
3548
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003549inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003550 unsigned long i)
3551{
3552 struct page *p;
3553 struct address_space *mapping;
3554
3555 if (i == 0)
3556 return eb->first_page;
3557 i += eb->start >> PAGE_CACHE_SHIFT;
3558 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003559 if (!mapping)
3560 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003561
3562 /*
3563 * extent_buffer_page is only called after pinning the page
3564 * by increasing the reference count. So we know the page must
3565 * be in the radix tree.
3566 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003567 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003568 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003569 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003570
Chris Masond1310b22008-01-24 16:13:08 -05003571 return p;
3572}
3573
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003574inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003575{
Chris Mason6af118ce2008-07-22 11:18:07 -04003576 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3577 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003578}
3579
Chris Masond1310b22008-01-24 16:13:08 -05003580static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3581 u64 start,
3582 unsigned long len,
3583 gfp_t mask)
3584{
3585 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003586#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003587 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003588#endif
Chris Masond1310b22008-01-24 16:13:08 -05003589
Chris Masond1310b22008-01-24 16:13:08 -05003590 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003591 if (eb == NULL)
3592 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003593 eb->start = start;
3594 eb->len = len;
Chris Masonbd681512011-07-16 15:23:14 -04003595 rwlock_init(&eb->lock);
3596 atomic_set(&eb->write_locks, 0);
3597 atomic_set(&eb->read_locks, 0);
3598 atomic_set(&eb->blocking_readers, 0);
3599 atomic_set(&eb->blocking_writers, 0);
3600 atomic_set(&eb->spinning_readers, 0);
3601 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003602 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003603 init_waitqueue_head(&eb->write_lock_wq);
3604 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003605
Chris Mason39351272009-02-04 09:24:05 -05003606#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003607 spin_lock_irqsave(&leak_lock, flags);
3608 list_add(&eb->leak_list, &buffers);
3609 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003610#endif
Chris Masond1310b22008-01-24 16:13:08 -05003611 atomic_set(&eb->refs, 1);
3612
3613 return eb;
3614}
3615
3616static void __free_extent_buffer(struct extent_buffer *eb)
3617{
Chris Mason39351272009-02-04 09:24:05 -05003618#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003619 unsigned long flags;
3620 spin_lock_irqsave(&leak_lock, flags);
3621 list_del(&eb->leak_list);
3622 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003623#endif
Chris Masond1310b22008-01-24 16:13:08 -05003624 kmem_cache_free(extent_buffer_cache, eb);
3625}
3626
Miao Xie897ca6e2010-10-26 20:57:29 -04003627/*
3628 * Helper for releasing extent buffer page.
3629 */
3630static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3631 unsigned long start_idx)
3632{
3633 unsigned long index;
3634 struct page *page;
3635
3636 if (!eb->first_page)
3637 return;
3638
3639 index = num_extent_pages(eb->start, eb->len);
3640 if (start_idx >= index)
3641 return;
3642
3643 do {
3644 index--;
3645 page = extent_buffer_page(eb, index);
3646 if (page)
3647 page_cache_release(page);
3648 } while (index != start_idx);
3649}
3650
3651/*
3652 * Helper for releasing the extent buffer.
3653 */
3654static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3655{
3656 btrfs_release_extent_buffer_page(eb, 0);
3657 __free_extent_buffer(eb);
3658}
3659
Chris Masond1310b22008-01-24 16:13:08 -05003660struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3661 u64 start, unsigned long len,
David Sterbaba144192011-04-21 01:12:06 +02003662 struct page *page0)
Chris Masond1310b22008-01-24 16:13:08 -05003663{
3664 unsigned long num_pages = num_extent_pages(start, len);
3665 unsigned long i;
3666 unsigned long index = start >> PAGE_CACHE_SHIFT;
3667 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003668 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003669 struct page *p;
3670 struct address_space *mapping = tree->mapping;
3671 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003672 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003673
Miao Xie19fe0a82010-10-26 20:57:29 -04003674 rcu_read_lock();
3675 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3676 if (eb && atomic_inc_not_zero(&eb->refs)) {
3677 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003678 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003679 return eb;
3680 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003681 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003682
David Sterbaba144192011-04-21 01:12:06 +02003683 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003684 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003685 return NULL;
3686
Chris Masond1310b22008-01-24 16:13:08 -05003687 if (page0) {
3688 eb->first_page = page0;
3689 i = 1;
3690 index++;
3691 page_cache_get(page0);
3692 mark_page_accessed(page0);
3693 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003694 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003695 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003696 } else {
3697 i = 0;
3698 }
3699 for (; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003700 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003701 if (!p) {
3702 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003703 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003704 }
3705 set_page_extent_mapped(p);
3706 mark_page_accessed(p);
3707 if (i == 0) {
3708 eb->first_page = p;
3709 set_page_extent_head(p, len);
3710 } else {
3711 set_page_private(p, EXTENT_PAGE_PRIVATE);
3712 }
3713 if (!PageUptodate(p))
3714 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003715
3716 /*
3717 * see below about how we avoid a nasty race with release page
3718 * and why we unlock later
3719 */
3720 if (i != 0)
3721 unlock_page(p);
Chris Masond1310b22008-01-24 16:13:08 -05003722 }
3723 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003724 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003725
Miao Xie19fe0a82010-10-26 20:57:29 -04003726 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3727 if (ret)
3728 goto free_eb;
3729
Chris Mason6af118ce2008-07-22 11:18:07 -04003730 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003731 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3732 if (ret == -EEXIST) {
3733 exists = radix_tree_lookup(&tree->buffer,
3734 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003735 /* add one reference for the caller */
3736 atomic_inc(&exists->refs);
3737 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003738 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003739 goto free_eb;
3740 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003741 /* add one reference for the tree */
3742 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003743 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003744 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003745
3746 /*
3747 * there is a race where release page may have
3748 * tried to find this extent buffer in the radix
3749 * but failed. It will tell the VM it is safe to
3750 * reclaim the, and it will clear the page private bit.
3751 * We must make sure to set the page private bit properly
3752 * after the extent buffer is in the radix tree so
3753 * it doesn't get lost
3754 */
3755 set_page_extent_mapped(eb->first_page);
3756 set_page_extent_head(eb->first_page, eb->len);
3757 if (!page0)
3758 unlock_page(eb->first_page);
Chris Masond1310b22008-01-24 16:13:08 -05003759 return eb;
3760
Chris Mason6af118ce2008-07-22 11:18:07 -04003761free_eb:
Chris Masoneb14ab82011-02-10 12:35:00 -05003762 if (eb->first_page && !page0)
3763 unlock_page(eb->first_page);
3764
Chris Masond1310b22008-01-24 16:13:08 -05003765 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003766 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003767 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003768 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003769}
Chris Masond1310b22008-01-24 16:13:08 -05003770
3771struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003772 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003773{
Chris Masond1310b22008-01-24 16:13:08 -05003774 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003775
Miao Xie19fe0a82010-10-26 20:57:29 -04003776 rcu_read_lock();
3777 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3778 if (eb && atomic_inc_not_zero(&eb->refs)) {
3779 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003780 mark_page_accessed(eb->first_page);
Miao Xie19fe0a82010-10-26 20:57:29 -04003781 return eb;
3782 }
3783 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003784
Miao Xie19fe0a82010-10-26 20:57:29 -04003785 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003786}
Chris Masond1310b22008-01-24 16:13:08 -05003787
3788void free_extent_buffer(struct extent_buffer *eb)
3789{
Chris Masond1310b22008-01-24 16:13:08 -05003790 if (!eb)
3791 return;
3792
3793 if (!atomic_dec_and_test(&eb->refs))
3794 return;
3795
Chris Mason6af118ce2008-07-22 11:18:07 -04003796 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003797}
Chris Masond1310b22008-01-24 16:13:08 -05003798
3799int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3800 struct extent_buffer *eb)
3801{
Chris Masond1310b22008-01-24 16:13:08 -05003802 unsigned long i;
3803 unsigned long num_pages;
3804 struct page *page;
3805
Chris Masond1310b22008-01-24 16:13:08 -05003806 num_pages = num_extent_pages(eb->start, eb->len);
3807
3808 for (i = 0; i < num_pages; i++) {
3809 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003810 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003811 continue;
3812
Chris Masona61e6f22008-07-22 11:18:08 -04003813 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003814 WARN_ON(!PagePrivate(page));
3815
3816 set_page_extent_mapped(page);
Chris Masond1310b22008-01-24 16:13:08 -05003817 if (i == 0)
3818 set_page_extent_head(page, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05003819
Chris Masond1310b22008-01-24 16:13:08 -05003820 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003821 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003822 if (!PageDirty(page)) {
3823 radix_tree_tag_clear(&page->mapping->page_tree,
3824 page_index(page),
3825 PAGECACHE_TAG_DIRTY);
3826 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003827 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04003828 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04003829 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003830 }
3831 return 0;
3832}
Chris Masond1310b22008-01-24 16:13:08 -05003833
Chris Masond1310b22008-01-24 16:13:08 -05003834int set_extent_buffer_dirty(struct extent_io_tree *tree,
3835 struct extent_buffer *eb)
3836{
3837 unsigned long i;
3838 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003839 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003840
Chris Masonb9473432009-03-13 11:00:37 -04003841 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003842 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003843 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003844 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003845 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003846}
Chris Masond1310b22008-01-24 16:13:08 -05003847
Chris Mason19b6caf2011-07-25 06:50:50 -04003848static int __eb_straddles_pages(u64 start, u64 len)
3849{
3850 if (len < PAGE_CACHE_SIZE)
3851 return 1;
3852 if (start & (PAGE_CACHE_SIZE - 1))
3853 return 1;
3854 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3855 return 1;
3856 return 0;
3857}
3858
3859static int eb_straddles_pages(struct extent_buffer *eb)
3860{
3861 return __eb_straddles_pages(eb->start, eb->len);
3862}
3863
Chris Mason1259ab72008-05-12 13:39:03 -04003864int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003865 struct extent_buffer *eb,
3866 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003867{
3868 unsigned long i;
3869 struct page *page;
3870 unsigned long num_pages;
3871
3872 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003873 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003874
Chris Mason19b6caf2011-07-25 06:50:50 -04003875 if (eb_straddles_pages(eb)) {
3876 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3877 cached_state, GFP_NOFS);
3878 }
Chris Mason1259ab72008-05-12 13:39:03 -04003879 for (i = 0; i < num_pages; i++) {
3880 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003881 if (page)
3882 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003883 }
3884 return 0;
3885}
3886
Chris Masond1310b22008-01-24 16:13:08 -05003887int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3888 struct extent_buffer *eb)
3889{
3890 unsigned long i;
3891 struct page *page;
3892 unsigned long num_pages;
3893
3894 num_pages = num_extent_pages(eb->start, eb->len);
3895
Chris Mason19b6caf2011-07-25 06:50:50 -04003896 if (eb_straddles_pages(eb)) {
3897 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3898 NULL, GFP_NOFS);
3899 }
Chris Masond1310b22008-01-24 16:13:08 -05003900 for (i = 0; i < num_pages; i++) {
3901 page = extent_buffer_page(eb, i);
3902 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3903 ((i == num_pages - 1) &&
3904 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3905 check_page_uptodate(tree, page);
3906 continue;
3907 }
3908 SetPageUptodate(page);
3909 }
3910 return 0;
3911}
Chris Masond1310b22008-01-24 16:13:08 -05003912
Chris Masonce9adaa2008-04-09 16:28:12 -04003913int extent_range_uptodate(struct extent_io_tree *tree,
3914 u64 start, u64 end)
3915{
3916 struct page *page;
3917 int ret;
3918 int pg_uptodate = 1;
3919 int uptodate;
3920 unsigned long index;
3921
Chris Mason19b6caf2011-07-25 06:50:50 -04003922 if (__eb_straddles_pages(start, end - start + 1)) {
3923 ret = test_range_bit(tree, start, end,
3924 EXTENT_UPTODATE, 1, NULL);
3925 if (ret)
3926 return 1;
3927 }
Chris Masond3977122009-01-05 21:25:51 -05003928 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003929 index = start >> PAGE_CACHE_SHIFT;
3930 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05003931 if (!page)
3932 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04003933 uptodate = PageUptodate(page);
3934 page_cache_release(page);
3935 if (!uptodate) {
3936 pg_uptodate = 0;
3937 break;
3938 }
3939 start += PAGE_CACHE_SIZE;
3940 }
3941 return pg_uptodate;
3942}
3943
Chris Masond1310b22008-01-24 16:13:08 -05003944int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003945 struct extent_buffer *eb,
3946 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003947{
Chris Mason728131d2008-04-09 16:28:12 -04003948 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003949 unsigned long num_pages;
3950 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003951 struct page *page;
3952 int pg_uptodate = 1;
3953
Chris Masonb4ce94d2009-02-04 09:25:08 -05003954 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003955 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003956
Chris Mason19b6caf2011-07-25 06:50:50 -04003957 if (eb_straddles_pages(eb)) {
3958 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3959 EXTENT_UPTODATE, 1, cached_state);
3960 if (ret)
3961 return ret;
3962 }
Chris Mason728131d2008-04-09 16:28:12 -04003963
3964 num_pages = num_extent_pages(eb->start, eb->len);
3965 for (i = 0; i < num_pages; i++) {
3966 page = extent_buffer_page(eb, i);
3967 if (!PageUptodate(page)) {
3968 pg_uptodate = 0;
3969 break;
3970 }
3971 }
Chris Mason42352982008-04-28 16:40:52 -04003972 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003973}
Chris Masond1310b22008-01-24 16:13:08 -05003974
3975int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02003976 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003977 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003978{
3979 unsigned long i;
3980 unsigned long start_i;
3981 struct page *page;
3982 int err;
3983 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003984 int locked_pages = 0;
3985 int all_uptodate = 1;
3986 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003987 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003988 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003989 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003990
Chris Masonb4ce94d2009-02-04 09:25:08 -05003991 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003992 return 0;
3993
Chris Mason19b6caf2011-07-25 06:50:50 -04003994 if (eb_straddles_pages(eb)) {
3995 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3996 EXTENT_UPTODATE, 1, NULL)) {
3997 return 0;
3998 }
Chris Masond1310b22008-01-24 16:13:08 -05003999 }
4000
4001 if (start) {
4002 WARN_ON(start < eb->start);
4003 start_i = (start >> PAGE_CACHE_SHIFT) -
4004 (eb->start >> PAGE_CACHE_SHIFT);
4005 } else {
4006 start_i = 0;
4007 }
4008
4009 num_pages = num_extent_pages(eb->start, eb->len);
4010 for (i = start_i; i < num_pages; i++) {
4011 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004012 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004013 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004014 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004015 } else {
4016 lock_page(page);
4017 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004018 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05004019 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004020 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004021 }
4022 if (all_uptodate) {
4023 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004024 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004025 goto unlock_exit;
4026 }
4027
4028 for (i = start_i; i < num_pages; i++) {
4029 page = extent_buffer_page(eb, i);
Chris Masoneb14ab82011-02-10 12:35:00 -05004030
4031 WARN_ON(!PagePrivate(page));
4032
4033 set_page_extent_mapped(page);
4034 if (i == 0)
4035 set_page_extent_head(page, eb->len);
4036
Chris Masonce9adaa2008-04-09 16:28:12 -04004037 if (inc_all_pages)
4038 page_cache_get(page);
4039 if (!PageUptodate(page)) {
4040 if (start_i == 0)
4041 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04004042 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004043 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004044 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004045 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004046 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004047 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004048 } else {
4049 unlock_page(page);
4050 }
4051 }
4052
Chris Masona86c12c2008-02-07 10:50:54 -05004053 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04004054 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05004055
Arne Jansenbb82ab82011-06-10 14:06:53 +02004056 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004057 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004058
Chris Masond1310b22008-01-24 16:13:08 -05004059 for (i = start_i; i < num_pages; i++) {
4060 page = extent_buffer_page(eb, i);
4061 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004062 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004063 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004064 }
Chris Masond3977122009-01-05 21:25:51 -05004065
Chris Masond1310b22008-01-24 16:13:08 -05004066 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004067 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004068 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004069
4070unlock_exit:
4071 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004072 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004073 page = extent_buffer_page(eb, i);
4074 i++;
4075 unlock_page(page);
4076 locked_pages--;
4077 }
4078 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004079}
Chris Masond1310b22008-01-24 16:13:08 -05004080
4081void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4082 unsigned long start,
4083 unsigned long len)
4084{
4085 size_t cur;
4086 size_t offset;
4087 struct page *page;
4088 char *kaddr;
4089 char *dst = (char *)dstv;
4090 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4091 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004092
4093 WARN_ON(start > eb->len);
4094 WARN_ON(start + len > eb->start + eb->len);
4095
4096 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4097
Chris Masond3977122009-01-05 21:25:51 -05004098 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004099 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004100
4101 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004102 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004103 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004104
4105 dst += cur;
4106 len -= cur;
4107 offset = 0;
4108 i++;
4109 }
4110}
Chris Masond1310b22008-01-24 16:13:08 -05004111
4112int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004113 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004114 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004115 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004116{
4117 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4118 char *kaddr;
4119 struct page *p;
4120 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4121 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4122 unsigned long end_i = (start_offset + start + min_len - 1) >>
4123 PAGE_CACHE_SHIFT;
4124
4125 if (i != end_i)
4126 return -EINVAL;
4127
4128 if (i == 0) {
4129 offset = start_offset;
4130 *map_start = 0;
4131 } else {
4132 offset = 0;
4133 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4134 }
Chris Masond3977122009-01-05 21:25:51 -05004135
Chris Masond1310b22008-01-24 16:13:08 -05004136 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004137 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4138 "wanted %lu %lu\n", (unsigned long long)eb->start,
4139 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004140 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004141 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004142 }
4143
4144 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004145 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004146 *map = kaddr + offset;
4147 *map_len = PAGE_CACHE_SIZE - offset;
4148 return 0;
4149}
Chris Masond1310b22008-01-24 16:13:08 -05004150
Chris Masond1310b22008-01-24 16:13:08 -05004151int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4152 unsigned long start,
4153 unsigned long len)
4154{
4155 size_t cur;
4156 size_t offset;
4157 struct page *page;
4158 char *kaddr;
4159 char *ptr = (char *)ptrv;
4160 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4161 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4162 int ret = 0;
4163
4164 WARN_ON(start > eb->len);
4165 WARN_ON(start + len > eb->start + eb->len);
4166
4167 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4168
Chris Masond3977122009-01-05 21:25:51 -05004169 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004170 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004171
4172 cur = min(len, (PAGE_CACHE_SIZE - offset));
4173
Chris Masona6591712011-07-19 12:04:14 -04004174 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004175 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004176 if (ret)
4177 break;
4178
4179 ptr += cur;
4180 len -= cur;
4181 offset = 0;
4182 i++;
4183 }
4184 return ret;
4185}
Chris Masond1310b22008-01-24 16:13:08 -05004186
4187void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4188 unsigned long start, unsigned long len)
4189{
4190 size_t cur;
4191 size_t offset;
4192 struct page *page;
4193 char *kaddr;
4194 char *src = (char *)srcv;
4195 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4196 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4197
4198 WARN_ON(start > eb->len);
4199 WARN_ON(start + len > eb->start + eb->len);
4200
4201 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4202
Chris Masond3977122009-01-05 21:25:51 -05004203 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004204 page = extent_buffer_page(eb, i);
4205 WARN_ON(!PageUptodate(page));
4206
4207 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004208 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004209 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004210
4211 src += cur;
4212 len -= cur;
4213 offset = 0;
4214 i++;
4215 }
4216}
Chris Masond1310b22008-01-24 16:13:08 -05004217
4218void memset_extent_buffer(struct extent_buffer *eb, char c,
4219 unsigned long start, unsigned long len)
4220{
4221 size_t cur;
4222 size_t offset;
4223 struct page *page;
4224 char *kaddr;
4225 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4226 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4227
4228 WARN_ON(start > eb->len);
4229 WARN_ON(start + len > eb->start + eb->len);
4230
4231 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4232
Chris Masond3977122009-01-05 21:25:51 -05004233 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004234 page = extent_buffer_page(eb, i);
4235 WARN_ON(!PageUptodate(page));
4236
4237 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004238 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004239 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004240
4241 len -= cur;
4242 offset = 0;
4243 i++;
4244 }
4245}
Chris Masond1310b22008-01-24 16:13:08 -05004246
4247void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4248 unsigned long dst_offset, unsigned long src_offset,
4249 unsigned long len)
4250{
4251 u64 dst_len = dst->len;
4252 size_t cur;
4253 size_t offset;
4254 struct page *page;
4255 char *kaddr;
4256 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4257 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4258
4259 WARN_ON(src->len != dst_len);
4260
4261 offset = (start_offset + dst_offset) &
4262 ((unsigned long)PAGE_CACHE_SIZE - 1);
4263
Chris Masond3977122009-01-05 21:25:51 -05004264 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004265 page = extent_buffer_page(dst, i);
4266 WARN_ON(!PageUptodate(page));
4267
4268 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4269
Chris Masona6591712011-07-19 12:04:14 -04004270 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004271 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004272
4273 src_offset += cur;
4274 len -= cur;
4275 offset = 0;
4276 i++;
4277 }
4278}
Chris Masond1310b22008-01-24 16:13:08 -05004279
4280static void move_pages(struct page *dst_page, struct page *src_page,
4281 unsigned long dst_off, unsigned long src_off,
4282 unsigned long len)
4283{
Chris Masona6591712011-07-19 12:04:14 -04004284 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004285 if (dst_page == src_page) {
4286 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4287 } else {
Chris Masona6591712011-07-19 12:04:14 -04004288 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004289 char *p = dst_kaddr + dst_off + len;
4290 char *s = src_kaddr + src_off + len;
4291
4292 while (len--)
4293 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004294 }
Chris Masond1310b22008-01-24 16:13:08 -05004295}
4296
Sergei Trofimovich33872062011-04-11 21:52:52 +00004297static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4298{
4299 unsigned long distance = (src > dst) ? src - dst : dst - src;
4300 return distance < len;
4301}
4302
Chris Masond1310b22008-01-24 16:13:08 -05004303static void copy_pages(struct page *dst_page, struct page *src_page,
4304 unsigned long dst_off, unsigned long src_off,
4305 unsigned long len)
4306{
Chris Masona6591712011-07-19 12:04:14 -04004307 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004308 char *src_kaddr;
4309
Sergei Trofimovich33872062011-04-11 21:52:52 +00004310 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004311 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004312 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004313 src_kaddr = dst_kaddr;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004314 BUG_ON(areas_overlap(src_off, dst_off, len));
4315 }
Chris Masond1310b22008-01-24 16:13:08 -05004316
4317 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004318}
4319
4320void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4321 unsigned long src_offset, unsigned long len)
4322{
4323 size_t cur;
4324 size_t dst_off_in_page;
4325 size_t src_off_in_page;
4326 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4327 unsigned long dst_i;
4328 unsigned long src_i;
4329
4330 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004331 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4332 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004333 BUG_ON(1);
4334 }
4335 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004336 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4337 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004338 BUG_ON(1);
4339 }
4340
Chris Masond3977122009-01-05 21:25:51 -05004341 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004342 dst_off_in_page = (start_offset + dst_offset) &
4343 ((unsigned long)PAGE_CACHE_SIZE - 1);
4344 src_off_in_page = (start_offset + src_offset) &
4345 ((unsigned long)PAGE_CACHE_SIZE - 1);
4346
4347 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4348 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4349
4350 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4351 src_off_in_page));
4352 cur = min_t(unsigned long, cur,
4353 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4354
4355 copy_pages(extent_buffer_page(dst, dst_i),
4356 extent_buffer_page(dst, src_i),
4357 dst_off_in_page, src_off_in_page, cur);
4358
4359 src_offset += cur;
4360 dst_offset += cur;
4361 len -= cur;
4362 }
4363}
Chris Masond1310b22008-01-24 16:13:08 -05004364
4365void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4366 unsigned long src_offset, unsigned long len)
4367{
4368 size_t cur;
4369 size_t dst_off_in_page;
4370 size_t src_off_in_page;
4371 unsigned long dst_end = dst_offset + len - 1;
4372 unsigned long src_end = src_offset + len - 1;
4373 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4374 unsigned long dst_i;
4375 unsigned long src_i;
4376
4377 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004378 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4379 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004380 BUG_ON(1);
4381 }
4382 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004383 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4384 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004385 BUG_ON(1);
4386 }
Sergei Trofimovich33872062011-04-11 21:52:52 +00004387 if (!areas_overlap(src_offset, dst_offset, len)) {
Chris Masond1310b22008-01-24 16:13:08 -05004388 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4389 return;
4390 }
Chris Masond3977122009-01-05 21:25:51 -05004391 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004392 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4393 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4394
4395 dst_off_in_page = (start_offset + dst_end) &
4396 ((unsigned long)PAGE_CACHE_SIZE - 1);
4397 src_off_in_page = (start_offset + src_end) &
4398 ((unsigned long)PAGE_CACHE_SIZE - 1);
4399
4400 cur = min_t(unsigned long, len, src_off_in_page + 1);
4401 cur = min(cur, dst_off_in_page + 1);
4402 move_pages(extent_buffer_page(dst, dst_i),
4403 extent_buffer_page(dst, src_i),
4404 dst_off_in_page - cur + 1,
4405 src_off_in_page - cur + 1, cur);
4406
4407 dst_end -= cur;
4408 src_end -= cur;
4409 len -= cur;
4410 }
4411}
Chris Mason6af118ce2008-07-22 11:18:07 -04004412
Miao Xie19fe0a82010-10-26 20:57:29 -04004413static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4414{
4415 struct extent_buffer *eb =
4416 container_of(head, struct extent_buffer, rcu_head);
4417
4418 btrfs_release_extent_buffer(eb);
4419}
4420
Chris Mason6af118ce2008-07-22 11:18:07 -04004421int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4422{
4423 u64 start = page_offset(page);
4424 struct extent_buffer *eb;
4425 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004426
4427 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004428 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason45f49bc2010-11-21 22:27:44 -05004429 if (!eb) {
4430 spin_unlock(&tree->buffer_lock);
4431 return ret;
4432 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004433
Chris Masonb9473432009-03-13 11:00:37 -04004434 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
4435 ret = 0;
4436 goto out;
4437 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004438
Miao Xie19fe0a82010-10-26 20:57:29 -04004439 /*
4440 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4441 * Or go back.
4442 */
4443 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4444 ret = 0;
4445 goto out;
4446 }
4447
4448 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04004449out:
4450 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004451
4452 /* at this point we can safely release the extent buffer */
4453 if (atomic_read(&eb->refs) == 0)
4454 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004455 return ret;
4456}