blob: 0381b6007ae45d15250351b49d5770f923014b95 [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
Liu Bo04493142012-02-16 18:34:37 +0800516 if (state->end < end && !need_resched())
517 next_node = rb_next(&state->rb_node);
518 else
519 next_node = NULL;
520
521 /* the state doesn't have the wanted bits, go ahead */
522 if (!(state->state & bits))
523 goto next;
524
Chris Masond1310b22008-01-24 16:13:08 -0500525 /*
526 * | ---- desired range ---- |
527 * | state | or
528 * | ------------- state -------------- |
529 *
530 * We need to split the extent we found, and may flip
531 * bits on second half.
532 *
533 * If the extent we found extends past our range, we
534 * just split and search again. It'll get split again
535 * the next time though.
536 *
537 * If the extent we found is inside our range, we clear
538 * the desired bit on it.
539 */
540
541 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000542 prealloc = alloc_extent_state_atomic(prealloc);
543 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500544 err = split_state(tree, state, prealloc, start);
545 BUG_ON(err == -EEXIST);
546 prealloc = NULL;
547 if (err)
548 goto out;
549 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400550 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400551 if (last_end == (u64)-1)
552 goto out;
553 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500554 }
555 goto search_again;
556 }
557 /*
558 * | ---- desired range ---- |
559 * | state |
560 * We need to split the extent, and clear the bit
561 * on the first half
562 */
563 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000564 prealloc = alloc_extent_state_atomic(prealloc);
565 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500566 err = split_state(tree, state, prealloc, end + 1);
567 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500568 if (wake)
569 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400570
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400571 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400572
Chris Masond1310b22008-01-24 16:13:08 -0500573 prealloc = NULL;
574 goto out;
575 }
Chris Mason42daec22009-09-23 19:51:09 -0400576
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400577 set |= clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800578next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400579 if (last_end == (u64)-1)
580 goto out;
581 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400582 if (start <= end && next_node) {
583 state = rb_entry(next_node, struct extent_state,
584 rb_node);
Liu Bo692e5752012-02-16 18:34:36 +0800585 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400586 }
Chris Masond1310b22008-01-24 16:13:08 -0500587 goto search_again;
588
589out:
Chris Masoncad321a2008-12-17 14:51:42 -0500590 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500591 if (prealloc)
592 free_extent_state(prealloc);
593
594 return set;
595
596search_again:
597 if (start > end)
598 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500599 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500600 if (mask & __GFP_WAIT)
601 cond_resched();
602 goto again;
603}
Chris Masond1310b22008-01-24 16:13:08 -0500604
605static int wait_on_state(struct extent_io_tree *tree,
606 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500607 __releases(tree->lock)
608 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500609{
610 DEFINE_WAIT(wait);
611 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500612 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500613 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500614 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500615 finish_wait(&state->wq, &wait);
616 return 0;
617}
618
619/*
620 * waits for one or more bits to clear on a range in the state tree.
621 * The range [start, end] is inclusive.
622 * The tree lock is taken by this function
623 */
624int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
625{
626 struct extent_state *state;
627 struct rb_node *node;
628
Chris Masoncad321a2008-12-17 14:51:42 -0500629 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630again:
631 while (1) {
632 /*
633 * this search will find all the extents that end after
634 * our range starts
635 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500636 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500637 if (!node)
638 break;
639
640 state = rb_entry(node, struct extent_state, rb_node);
641
642 if (state->start > end)
643 goto out;
644
645 if (state->state & bits) {
646 start = state->start;
647 atomic_inc(&state->refs);
648 wait_on_state(tree, state);
649 free_extent_state(state);
650 goto again;
651 }
652 start = state->end + 1;
653
654 if (start > end)
655 break;
656
Xiao Guangrongded91f02011-07-14 03:19:27 +0000657 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500658 }
659out:
Chris Masoncad321a2008-12-17 14:51:42 -0500660 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500661 return 0;
662}
Chris Masond1310b22008-01-24 16:13:08 -0500663
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000664static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500665 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400666 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500667{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400668 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400669
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000670 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400671 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500672 u64 range = state->end - state->start + 1;
673 tree->dirty_bytes += range;
674 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400675 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500676}
677
Chris Mason2c64c532009-09-02 15:04:12 -0400678static void cache_state(struct extent_state *state,
679 struct extent_state **cached_ptr)
680{
681 if (cached_ptr && !(*cached_ptr)) {
682 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
683 *cached_ptr = state;
684 atomic_inc(&state->refs);
685 }
686 }
687}
688
Arne Jansen507903b2011-04-06 10:02:20 +0000689static void uncache_state(struct extent_state **cached_ptr)
690{
691 if (cached_ptr && (*cached_ptr)) {
692 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400693 *cached_ptr = NULL;
694 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000695 }
696}
697
Chris Masond1310b22008-01-24 16:13:08 -0500698/*
Chris Mason1edbb732009-09-02 13:24:36 -0400699 * set some bits on a range in the tree. This may require allocations or
700 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500701 *
Chris Mason1edbb732009-09-02 13:24:36 -0400702 * If any of the exclusive bits are set, this will fail with -EEXIST if some
703 * part of the range already has the desired bits set. The start of the
704 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500705 *
Chris Mason1edbb732009-09-02 13:24:36 -0400706 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500707 */
Chris Mason1edbb732009-09-02 13:24:36 -0400708
Chris Mason4845e442010-05-25 20:56:50 -0400709int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
710 int bits, int exclusive_bits, u64 *failed_start,
711 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500712{
713 struct extent_state *state;
714 struct extent_state *prealloc = NULL;
715 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500716 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500717 u64 last_start;
718 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400719
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400720 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500721again:
722 if (!prealloc && (mask & __GFP_WAIT)) {
723 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000724 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500725 }
726
Chris Masoncad321a2008-12-17 14:51:42 -0500727 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400728 if (cached_state && *cached_state) {
729 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400730 if (state->start <= start && state->end > start &&
731 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400732 node = &state->rb_node;
733 goto hit_next;
734 }
735 }
Chris Masond1310b22008-01-24 16:13:08 -0500736 /*
737 * this search will find all the extents that end after
738 * our range starts.
739 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500740 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500741 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000742 prealloc = alloc_extent_state_atomic(prealloc);
743 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400744 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500745 prealloc = NULL;
746 BUG_ON(err == -EEXIST);
747 goto out;
748 }
Chris Masond1310b22008-01-24 16:13:08 -0500749 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400750hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500751 last_start = state->start;
752 last_end = state->end;
753
754 /*
755 * | ---- desired range ---- |
756 * | state |
757 *
758 * Just lock what we found and keep going
759 */
760 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400761 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400762 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500763 *failed_start = state->start;
764 err = -EEXIST;
765 goto out;
766 }
Chris Mason42daec22009-09-23 19:51:09 -0400767
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000768 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400769
Chris Mason2c64c532009-09-02 15:04:12 -0400770 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500771 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400772 if (last_end == (u64)-1)
773 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400774
Yan Zheng5c939df2009-05-27 09:16:03 -0400775 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400776 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000777 if (next_node && start < end && prealloc && !need_resched()) {
778 state = rb_entry(next_node, struct extent_state,
779 rb_node);
780 if (state->start == start)
781 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400782 }
Chris Masond1310b22008-01-24 16:13:08 -0500783 goto search_again;
784 }
785
786 /*
787 * | ---- desired range ---- |
788 * | state |
789 * or
790 * | ------------- state -------------- |
791 *
792 * We need to split the extent we found, and may flip bits on
793 * second half.
794 *
795 * If the extent we found extends past our
796 * range, we just split and search again. It'll get split
797 * again the next time though.
798 *
799 * If the extent we found is inside our range, we set the
800 * desired bit on it.
801 */
802 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400803 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500804 *failed_start = start;
805 err = -EEXIST;
806 goto out;
807 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000808
809 prealloc = alloc_extent_state_atomic(prealloc);
810 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500811 err = split_state(tree, state, prealloc, start);
812 BUG_ON(err == -EEXIST);
813 prealloc = NULL;
814 if (err)
815 goto out;
816 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000817 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400818 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500819 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400820 if (last_end == (u64)-1)
821 goto out;
822 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500823 }
824 goto search_again;
825 }
826 /*
827 * | ---- desired range ---- |
828 * | state | or | state |
829 *
830 * There's a hole, we need to insert something in it and
831 * ignore the extent we found.
832 */
833 if (state->start > start) {
834 u64 this_end;
835 if (end < last_start)
836 this_end = end;
837 else
Chris Masond3977122009-01-05 21:25:51 -0500838 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000839
840 prealloc = alloc_extent_state_atomic(prealloc);
841 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000842
843 /*
844 * Avoid to free 'prealloc' if it can be merged with
845 * the later extent.
846 */
Chris Masond1310b22008-01-24 16:13:08 -0500847 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400848 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400849 BUG_ON(err == -EEXIST);
850 if (err) {
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000851 free_extent_state(prealloc);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400852 prealloc = NULL;
853 goto out;
854 }
Chris Mason2c64c532009-09-02 15:04:12 -0400855 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500856 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500857 start = this_end + 1;
858 goto search_again;
859 }
860 /*
861 * | ---- desired range ---- |
862 * | state |
863 * We need to split the extent, and set the bit
864 * on the first half
865 */
866 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400867 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500868 *failed_start = start;
869 err = -EEXIST;
870 goto out;
871 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000872
873 prealloc = alloc_extent_state_atomic(prealloc);
874 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500875 err = split_state(tree, state, prealloc, end + 1);
876 BUG_ON(err == -EEXIST);
877
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000878 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400879 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500880 merge_state(tree, prealloc);
881 prealloc = NULL;
882 goto out;
883 }
884
885 goto search_again;
886
887out:
Chris Masoncad321a2008-12-17 14:51:42 -0500888 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500889 if (prealloc)
890 free_extent_state(prealloc);
891
892 return err;
893
894search_again:
895 if (start > end)
896 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500897 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500898 if (mask & __GFP_WAIT)
899 cond_resched();
900 goto again;
901}
Chris Masond1310b22008-01-24 16:13:08 -0500902
Josef Bacik462d6fa2011-09-26 13:56:12 -0400903/**
904 * convert_extent - convert all bits in a given range from one bit to another
905 * @tree: the io tree to search
906 * @start: the start offset in bytes
907 * @end: the end offset in bytes (inclusive)
908 * @bits: the bits to set in this range
909 * @clear_bits: the bits to clear in this range
910 * @mask: the allocation mask
911 *
912 * This will go through and set bits for the given range. If any states exist
913 * already in this range they are set with the given bit and cleared of the
914 * clear_bits. This is only meant to be used by things that are mergeable, ie
915 * converting from say DELALLOC to DIRTY. This is not meant to be used with
916 * boundary bits like LOCK.
917 */
918int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
919 int bits, int clear_bits, gfp_t mask)
920{
921 struct extent_state *state;
922 struct extent_state *prealloc = NULL;
923 struct rb_node *node;
924 int err = 0;
925 u64 last_start;
926 u64 last_end;
927
928again:
929 if (!prealloc && (mask & __GFP_WAIT)) {
930 prealloc = alloc_extent_state(mask);
931 if (!prealloc)
932 return -ENOMEM;
933 }
934
935 spin_lock(&tree->lock);
936 /*
937 * this search will find all the extents that end after
938 * our range starts.
939 */
940 node = tree_search(tree, start);
941 if (!node) {
942 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500943 if (!prealloc) {
944 err = -ENOMEM;
945 goto out;
946 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400947 err = insert_state(tree, prealloc, start, end, &bits);
948 prealloc = NULL;
949 BUG_ON(err == -EEXIST);
950 goto out;
951 }
952 state = rb_entry(node, struct extent_state, rb_node);
953hit_next:
954 last_start = state->start;
955 last_end = state->end;
956
957 /*
958 * | ---- desired range ---- |
959 * | state |
960 *
961 * Just lock what we found and keep going
962 */
963 if (state->start == start && state->end <= end) {
964 struct rb_node *next_node;
965
966 set_state_bits(tree, state, &bits);
967 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400968 if (last_end == (u64)-1)
969 goto out;
970
971 start = last_end + 1;
972 next_node = rb_next(&state->rb_node);
973 if (next_node && start < end && prealloc && !need_resched()) {
974 state = rb_entry(next_node, struct extent_state,
975 rb_node);
976 if (state->start == start)
977 goto hit_next;
978 }
979 goto search_again;
980 }
981
982 /*
983 * | ---- desired range ---- |
984 * | state |
985 * or
986 * | ------------- state -------------- |
987 *
988 * We need to split the extent we found, and may flip bits on
989 * second half.
990 *
991 * If the extent we found extends past our
992 * range, we just split and search again. It'll get split
993 * again the next time though.
994 *
995 * If the extent we found is inside our range, we set the
996 * desired bit on it.
997 */
998 if (state->start < start) {
999 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001000 if (!prealloc) {
1001 err = -ENOMEM;
1002 goto out;
1003 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001004 err = split_state(tree, state, prealloc, start);
1005 BUG_ON(err == -EEXIST);
1006 prealloc = NULL;
1007 if (err)
1008 goto out;
1009 if (state->end <= end) {
1010 set_state_bits(tree, state, &bits);
1011 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001012 if (last_end == (u64)-1)
1013 goto out;
1014 start = last_end + 1;
1015 }
1016 goto search_again;
1017 }
1018 /*
1019 * | ---- desired range ---- |
1020 * | state | or | state |
1021 *
1022 * There's a hole, we need to insert something in it and
1023 * ignore the extent we found.
1024 */
1025 if (state->start > start) {
1026 u64 this_end;
1027 if (end < last_start)
1028 this_end = end;
1029 else
1030 this_end = last_start - 1;
1031
1032 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001033 if (!prealloc) {
1034 err = -ENOMEM;
1035 goto out;
1036 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001037
1038 /*
1039 * Avoid to free 'prealloc' if it can be merged with
1040 * the later extent.
1041 */
1042 err = insert_state(tree, prealloc, start, this_end,
1043 &bits);
1044 BUG_ON(err == -EEXIST);
1045 if (err) {
1046 free_extent_state(prealloc);
1047 prealloc = NULL;
1048 goto out;
1049 }
1050 prealloc = NULL;
1051 start = this_end + 1;
1052 goto search_again;
1053 }
1054 /*
1055 * | ---- desired range ---- |
1056 * | state |
1057 * We need to split the extent, and set the bit
1058 * on the first half
1059 */
1060 if (state->start <= end && state->end > end) {
1061 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001062 if (!prealloc) {
1063 err = -ENOMEM;
1064 goto out;
1065 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001066
1067 err = split_state(tree, state, prealloc, end + 1);
1068 BUG_ON(err == -EEXIST);
1069
1070 set_state_bits(tree, prealloc, &bits);
1071 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001072 prealloc = NULL;
1073 goto out;
1074 }
1075
1076 goto search_again;
1077
1078out:
1079 spin_unlock(&tree->lock);
1080 if (prealloc)
1081 free_extent_state(prealloc);
1082
1083 return err;
1084
1085search_again:
1086 if (start > end)
1087 goto out;
1088 spin_unlock(&tree->lock);
1089 if (mask & __GFP_WAIT)
1090 cond_resched();
1091 goto again;
1092}
1093
Chris Masond1310b22008-01-24 16:13:08 -05001094/* wrappers around set/clear extent bit */
1095int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1096 gfp_t mask)
1097{
1098 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001099 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001100}
Chris Masond1310b22008-01-24 16:13:08 -05001101
1102int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1103 int bits, gfp_t mask)
1104{
1105 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001106 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001107}
Chris Masond1310b22008-01-24 16:13:08 -05001108
1109int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1110 int bits, gfp_t mask)
1111{
Chris Mason2c64c532009-09-02 15:04:12 -04001112 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001113}
Chris Masond1310b22008-01-24 16:13:08 -05001114
1115int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001116 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001117{
1118 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001119 EXTENT_DELALLOC | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001120 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001121}
Chris Masond1310b22008-01-24 16:13:08 -05001122
1123int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1124 gfp_t mask)
1125{
1126 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001127 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001128 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001129}
Chris Masond1310b22008-01-24 16:13:08 -05001130
1131int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1132 gfp_t mask)
1133{
1134 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001135 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001136}
Chris Masond1310b22008-01-24 16:13:08 -05001137
Chris Masond1310b22008-01-24 16:13:08 -05001138int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001139 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001140{
Arne Jansen507903b2011-04-06 10:02:20 +00001141 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1142 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
Chris Masond3977122009-01-05 21:25:51 -05001145static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001146 u64 end, struct extent_state **cached_state,
1147 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001148{
Chris Mason2c64c532009-09-02 15:04:12 -04001149 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001150 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001151}
Chris Masond1310b22008-01-24 16:13:08 -05001152
Chris Masond352ac62008-09-29 15:18:18 -04001153/*
1154 * either insert or lock state struct between start and end use mask to tell
1155 * us if waiting is desired.
1156 */
Chris Mason1edbb732009-09-02 13:24:36 -04001157int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001158 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001159{
1160 int err;
1161 u64 failed_start;
1162 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001163 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001164 EXTENT_LOCKED, &failed_start,
1165 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001166 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1167 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1168 start = failed_start;
1169 } else {
1170 break;
1171 }
1172 WARN_ON(start > end);
1173 }
1174 return err;
1175}
Chris Masond1310b22008-01-24 16:13:08 -05001176
Chris Mason1edbb732009-09-02 13:24:36 -04001177int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1178{
Chris Mason2c64c532009-09-02 15:04:12 -04001179 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001180}
1181
Josef Bacik25179202008-10-29 14:49:05 -04001182int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1183 gfp_t mask)
1184{
1185 int err;
1186 u64 failed_start;
1187
Chris Mason2c64c532009-09-02 15:04:12 -04001188 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1189 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001190 if (err == -EEXIST) {
1191 if (failed_start > start)
1192 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001193 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001194 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001195 }
Josef Bacik25179202008-10-29 14:49:05 -04001196 return 1;
1197}
Josef Bacik25179202008-10-29 14:49:05 -04001198
Chris Mason2c64c532009-09-02 15:04:12 -04001199int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1200 struct extent_state **cached, gfp_t mask)
1201{
1202 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1203 mask);
1204}
1205
Arne Jansen507903b2011-04-06 10:02:20 +00001206int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001207{
Chris Mason2c64c532009-09-02 15:04:12 -04001208 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1209 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001210}
Chris Masond1310b22008-01-24 16:13:08 -05001211
1212/*
Chris Masond1310b22008-01-24 16:13:08 -05001213 * helper function to set both pages and extents in the tree writeback
1214 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001215static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001216{
1217 unsigned long index = start >> PAGE_CACHE_SHIFT;
1218 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1219 struct page *page;
1220
1221 while (index <= end_index) {
1222 page = find_get_page(tree->mapping, index);
1223 BUG_ON(!page);
1224 set_page_writeback(page);
1225 page_cache_release(page);
1226 index++;
1227 }
Chris Masond1310b22008-01-24 16:13:08 -05001228 return 0;
1229}
Chris Masond1310b22008-01-24 16:13:08 -05001230
Chris Masond352ac62008-09-29 15:18:18 -04001231/* find the first state struct with 'bits' set after 'start', and
1232 * return it. tree->lock must be held. NULL will returned if
1233 * nothing was found after 'start'
1234 */
Chris Masond7fc6402008-02-18 12:12:38 -05001235struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1236 u64 start, int bits)
1237{
1238 struct rb_node *node;
1239 struct extent_state *state;
1240
1241 /*
1242 * this search will find all the extents that end after
1243 * our range starts.
1244 */
1245 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001246 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001247 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001248
Chris Masond3977122009-01-05 21:25:51 -05001249 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001250 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001251 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001252 return state;
Chris Masond3977122009-01-05 21:25:51 -05001253
Chris Masond7fc6402008-02-18 12:12:38 -05001254 node = rb_next(node);
1255 if (!node)
1256 break;
1257 }
1258out:
1259 return NULL;
1260}
Chris Masond7fc6402008-02-18 12:12:38 -05001261
Chris Masond352ac62008-09-29 15:18:18 -04001262/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001263 * find the first offset in the io tree with 'bits' set. zero is
1264 * returned if we find something, and *start_ret and *end_ret are
1265 * set to reflect the state struct that was found.
1266 *
1267 * If nothing was found, 1 is returned, < 0 on error
1268 */
1269int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1270 u64 *start_ret, u64 *end_ret, int bits)
1271{
1272 struct extent_state *state;
1273 int ret = 1;
1274
1275 spin_lock(&tree->lock);
1276 state = find_first_extent_bit_state(tree, start, bits);
1277 if (state) {
1278 *start_ret = state->start;
1279 *end_ret = state->end;
1280 ret = 0;
1281 }
1282 spin_unlock(&tree->lock);
1283 return ret;
1284}
1285
1286/*
Chris Masond352ac62008-09-29 15:18:18 -04001287 * find a contiguous range of bytes in the file marked as delalloc, not
1288 * more than 'max_bytes'. start and end are used to return the range,
1289 *
1290 * 1 is returned if we find something, 0 if nothing was in the tree
1291 */
Chris Masonc8b97812008-10-29 14:49:59 -04001292static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001293 u64 *start, u64 *end, u64 max_bytes,
1294 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001295{
1296 struct rb_node *node;
1297 struct extent_state *state;
1298 u64 cur_start = *start;
1299 u64 found = 0;
1300 u64 total_bytes = 0;
1301
Chris Masoncad321a2008-12-17 14:51:42 -05001302 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001303
Chris Masond1310b22008-01-24 16:13:08 -05001304 /*
1305 * this search will find all the extents that end after
1306 * our range starts.
1307 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001308 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001309 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001310 if (!found)
1311 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001312 goto out;
1313 }
1314
Chris Masond3977122009-01-05 21:25:51 -05001315 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001316 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001317 if (found && (state->start != cur_start ||
1318 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001319 goto out;
1320 }
1321 if (!(state->state & EXTENT_DELALLOC)) {
1322 if (!found)
1323 *end = state->end;
1324 goto out;
1325 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001326 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001327 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001328 *cached_state = state;
1329 atomic_inc(&state->refs);
1330 }
Chris Masond1310b22008-01-24 16:13:08 -05001331 found++;
1332 *end = state->end;
1333 cur_start = state->end + 1;
1334 node = rb_next(node);
1335 if (!node)
1336 break;
1337 total_bytes += state->end - state->start + 1;
1338 if (total_bytes >= max_bytes)
1339 break;
1340 }
1341out:
Chris Masoncad321a2008-12-17 14:51:42 -05001342 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001343 return found;
1344}
1345
Chris Masonc8b97812008-10-29 14:49:59 -04001346static noinline int __unlock_for_delalloc(struct inode *inode,
1347 struct page *locked_page,
1348 u64 start, u64 end)
1349{
1350 int ret;
1351 struct page *pages[16];
1352 unsigned long index = start >> PAGE_CACHE_SHIFT;
1353 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1354 unsigned long nr_pages = end_index - index + 1;
1355 int i;
1356
1357 if (index == locked_page->index && end_index == index)
1358 return 0;
1359
Chris Masond3977122009-01-05 21:25:51 -05001360 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001361 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001362 min_t(unsigned long, nr_pages,
1363 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001364 for (i = 0; i < ret; i++) {
1365 if (pages[i] != locked_page)
1366 unlock_page(pages[i]);
1367 page_cache_release(pages[i]);
1368 }
1369 nr_pages -= ret;
1370 index += ret;
1371 cond_resched();
1372 }
1373 return 0;
1374}
1375
1376static noinline int lock_delalloc_pages(struct inode *inode,
1377 struct page *locked_page,
1378 u64 delalloc_start,
1379 u64 delalloc_end)
1380{
1381 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1382 unsigned long start_index = index;
1383 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1384 unsigned long pages_locked = 0;
1385 struct page *pages[16];
1386 unsigned long nrpages;
1387 int ret;
1388 int i;
1389
1390 /* the caller is responsible for locking the start index */
1391 if (index == locked_page->index && index == end_index)
1392 return 0;
1393
1394 /* skip the page at the start index */
1395 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001396 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001397 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001398 min_t(unsigned long,
1399 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001400 if (ret == 0) {
1401 ret = -EAGAIN;
1402 goto done;
1403 }
1404 /* now we have an array of pages, lock them all */
1405 for (i = 0; i < ret; i++) {
1406 /*
1407 * the caller is taking responsibility for
1408 * locked_page
1409 */
Chris Mason771ed682008-11-06 22:02:51 -05001410 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001411 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001412 if (!PageDirty(pages[i]) ||
1413 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001414 ret = -EAGAIN;
1415 unlock_page(pages[i]);
1416 page_cache_release(pages[i]);
1417 goto done;
1418 }
1419 }
Chris Masonc8b97812008-10-29 14:49:59 -04001420 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001421 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001422 }
Chris Masonc8b97812008-10-29 14:49:59 -04001423 nrpages -= ret;
1424 index += ret;
1425 cond_resched();
1426 }
1427 ret = 0;
1428done:
1429 if (ret && pages_locked) {
1430 __unlock_for_delalloc(inode, locked_page,
1431 delalloc_start,
1432 ((u64)(start_index + pages_locked - 1)) <<
1433 PAGE_CACHE_SHIFT);
1434 }
1435 return ret;
1436}
1437
1438/*
1439 * find a contiguous range of bytes in the file marked as delalloc, not
1440 * more than 'max_bytes'. start and end are used to return the range,
1441 *
1442 * 1 is returned if we find something, 0 if nothing was in the tree
1443 */
1444static noinline u64 find_lock_delalloc_range(struct inode *inode,
1445 struct extent_io_tree *tree,
1446 struct page *locked_page,
1447 u64 *start, u64 *end,
1448 u64 max_bytes)
1449{
1450 u64 delalloc_start;
1451 u64 delalloc_end;
1452 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001453 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001454 int ret;
1455 int loops = 0;
1456
1457again:
1458 /* step one, find a bunch of delalloc bytes starting at start */
1459 delalloc_start = *start;
1460 delalloc_end = 0;
1461 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001462 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001463 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001464 *start = delalloc_start;
1465 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001466 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001467 return found;
1468 }
1469
1470 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001471 * start comes from the offset of locked_page. We have to lock
1472 * pages in order, so we can't process delalloc bytes before
1473 * locked_page
1474 */
Chris Masond3977122009-01-05 21:25:51 -05001475 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001476 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001477
1478 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001479 * make sure to limit the number of pages we try to lock down
1480 * if we're looping.
1481 */
Chris Masond3977122009-01-05 21:25:51 -05001482 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001483 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001484
Chris Masonc8b97812008-10-29 14:49:59 -04001485 /* step two, lock all the pages after the page that has start */
1486 ret = lock_delalloc_pages(inode, locked_page,
1487 delalloc_start, delalloc_end);
1488 if (ret == -EAGAIN) {
1489 /* some of the pages are gone, lets avoid looping by
1490 * shortening the size of the delalloc range we're searching
1491 */
Chris Mason9655d292009-09-02 15:22:30 -04001492 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001493 if (!loops) {
1494 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1495 max_bytes = PAGE_CACHE_SIZE - offset;
1496 loops = 1;
1497 goto again;
1498 } else {
1499 found = 0;
1500 goto out_failed;
1501 }
1502 }
1503 BUG_ON(ret);
1504
1505 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001506 lock_extent_bits(tree, delalloc_start, delalloc_end,
1507 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001508
1509 /* then test to make sure it is all still delalloc */
1510 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001511 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001512 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001513 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1514 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001515 __unlock_for_delalloc(inode, locked_page,
1516 delalloc_start, delalloc_end);
1517 cond_resched();
1518 goto again;
1519 }
Chris Mason9655d292009-09-02 15:22:30 -04001520 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001521 *start = delalloc_start;
1522 *end = delalloc_end;
1523out_failed:
1524 return found;
1525}
1526
1527int extent_clear_unlock_delalloc(struct inode *inode,
1528 struct extent_io_tree *tree,
1529 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001530 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001531{
1532 int ret;
1533 struct page *pages[16];
1534 unsigned long index = start >> PAGE_CACHE_SHIFT;
1535 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1536 unsigned long nr_pages = end_index - index + 1;
1537 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001538 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001539
Chris Masona791e352009-10-08 11:27:10 -04001540 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001541 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001542 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001543 clear_bits |= EXTENT_DIRTY;
1544
Chris Masona791e352009-10-08 11:27:10 -04001545 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001546 clear_bits |= EXTENT_DELALLOC;
1547
Chris Mason2c64c532009-09-02 15:04:12 -04001548 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001549 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1550 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1551 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001552 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001553
Chris Masond3977122009-01-05 21:25:51 -05001554 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001555 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001556 min_t(unsigned long,
1557 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001558 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001559
Chris Masona791e352009-10-08 11:27:10 -04001560 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001561 SetPagePrivate2(pages[i]);
1562
Chris Masonc8b97812008-10-29 14:49:59 -04001563 if (pages[i] == locked_page) {
1564 page_cache_release(pages[i]);
1565 continue;
1566 }
Chris Masona791e352009-10-08 11:27:10 -04001567 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001568 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001569 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001570 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001571 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001572 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001573 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001574 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001575 page_cache_release(pages[i]);
1576 }
1577 nr_pages -= ret;
1578 index += ret;
1579 cond_resched();
1580 }
1581 return 0;
1582}
Chris Masonc8b97812008-10-29 14:49:59 -04001583
Chris Masond352ac62008-09-29 15:18:18 -04001584/*
1585 * count the number of bytes in the tree that have a given bit(s)
1586 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1587 * cached. The total number found is returned.
1588 */
Chris Masond1310b22008-01-24 16:13:08 -05001589u64 count_range_bits(struct extent_io_tree *tree,
1590 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001591 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001592{
1593 struct rb_node *node;
1594 struct extent_state *state;
1595 u64 cur_start = *start;
1596 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001597 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001598 int found = 0;
1599
1600 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001601 WARN_ON(1);
1602 return 0;
1603 }
1604
Chris Masoncad321a2008-12-17 14:51:42 -05001605 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001606 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1607 total_bytes = tree->dirty_bytes;
1608 goto out;
1609 }
1610 /*
1611 * this search will find all the extents that end after
1612 * our range starts.
1613 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001614 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001615 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001616 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001617
Chris Masond3977122009-01-05 21:25:51 -05001618 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001619 state = rb_entry(node, struct extent_state, rb_node);
1620 if (state->start > search_end)
1621 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001622 if (contig && found && state->start > last + 1)
1623 break;
1624 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001625 total_bytes += min(search_end, state->end) + 1 -
1626 max(cur_start, state->start);
1627 if (total_bytes >= max_bytes)
1628 break;
1629 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001630 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001631 found = 1;
1632 }
Chris Masonec29ed52011-02-23 16:23:20 -05001633 last = state->end;
1634 } else if (contig && found) {
1635 break;
Chris Masond1310b22008-01-24 16:13:08 -05001636 }
1637 node = rb_next(node);
1638 if (!node)
1639 break;
1640 }
1641out:
Chris Masoncad321a2008-12-17 14:51:42 -05001642 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001643 return total_bytes;
1644}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001645
Chris Masond352ac62008-09-29 15:18:18 -04001646/*
1647 * set the private field for a given byte offset in the tree. If there isn't
1648 * an extent_state there already, this does nothing.
1649 */
Chris Masond1310b22008-01-24 16:13:08 -05001650int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1651{
1652 struct rb_node *node;
1653 struct extent_state *state;
1654 int ret = 0;
1655
Chris Masoncad321a2008-12-17 14:51:42 -05001656 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001657 /*
1658 * this search will find all the extents that end after
1659 * our range starts.
1660 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001661 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001662 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001663 ret = -ENOENT;
1664 goto out;
1665 }
1666 state = rb_entry(node, struct extent_state, rb_node);
1667 if (state->start != start) {
1668 ret = -ENOENT;
1669 goto out;
1670 }
1671 state->private = private;
1672out:
Chris Masoncad321a2008-12-17 14:51:42 -05001673 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001674 return ret;
1675}
1676
1677int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1678{
1679 struct rb_node *node;
1680 struct extent_state *state;
1681 int ret = 0;
1682
Chris Masoncad321a2008-12-17 14:51:42 -05001683 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001684 /*
1685 * this search will find all the extents that end after
1686 * our range starts.
1687 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001688 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001689 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001690 ret = -ENOENT;
1691 goto out;
1692 }
1693 state = rb_entry(node, struct extent_state, rb_node);
1694 if (state->start != start) {
1695 ret = -ENOENT;
1696 goto out;
1697 }
1698 *private = state->private;
1699out:
Chris Masoncad321a2008-12-17 14:51:42 -05001700 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001701 return ret;
1702}
1703
1704/*
1705 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001706 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001707 * has the bits set. Otherwise, 1 is returned if any bit in the
1708 * range is found set.
1709 */
1710int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001711 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001712{
1713 struct extent_state *state = NULL;
1714 struct rb_node *node;
1715 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001716
Chris Masoncad321a2008-12-17 14:51:42 -05001717 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001718 if (cached && cached->tree && cached->start <= start &&
1719 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001720 node = &cached->rb_node;
1721 else
1722 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001723 while (node && start <= end) {
1724 state = rb_entry(node, struct extent_state, rb_node);
1725
1726 if (filled && state->start > start) {
1727 bitset = 0;
1728 break;
1729 }
1730
1731 if (state->start > end)
1732 break;
1733
1734 if (state->state & bits) {
1735 bitset = 1;
1736 if (!filled)
1737 break;
1738 } else if (filled) {
1739 bitset = 0;
1740 break;
1741 }
Chris Mason46562cec2009-09-23 20:23:16 -04001742
1743 if (state->end == (u64)-1)
1744 break;
1745
Chris Masond1310b22008-01-24 16:13:08 -05001746 start = state->end + 1;
1747 if (start > end)
1748 break;
1749 node = rb_next(node);
1750 if (!node) {
1751 if (filled)
1752 bitset = 0;
1753 break;
1754 }
1755 }
Chris Masoncad321a2008-12-17 14:51:42 -05001756 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001757 return bitset;
1758}
Chris Masond1310b22008-01-24 16:13:08 -05001759
1760/*
1761 * helper function to set a given page up to date if all the
1762 * extents in the tree for that page are up to date
1763 */
1764static int check_page_uptodate(struct extent_io_tree *tree,
1765 struct page *page)
1766{
1767 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1768 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001769 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001770 SetPageUptodate(page);
1771 return 0;
1772}
1773
1774/*
1775 * helper function to unlock a page if all the extents in the tree
1776 * for that page are unlocked
1777 */
1778static int check_page_locked(struct extent_io_tree *tree,
1779 struct page *page)
1780{
1781 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1782 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001783 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001784 unlock_page(page);
1785 return 0;
1786}
1787
1788/*
1789 * helper function to end page writeback if all the extents
1790 * in the tree for that page are done with writeback
1791 */
1792static int check_page_writeback(struct extent_io_tree *tree,
1793 struct page *page)
1794{
Chris Mason1edbb732009-09-02 13:24:36 -04001795 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001796 return 0;
1797}
1798
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001799/*
1800 * When IO fails, either with EIO or csum verification fails, we
1801 * try other mirrors that might have a good copy of the data. This
1802 * io_failure_record is used to record state as we go through all the
1803 * mirrors. If another mirror has good data, the page is set up to date
1804 * and things continue. If a good mirror can't be found, the original
1805 * bio end_io callback is called to indicate things have failed.
1806 */
1807struct io_failure_record {
1808 struct page *page;
1809 u64 start;
1810 u64 len;
1811 u64 logical;
1812 unsigned long bio_flags;
1813 int this_mirror;
1814 int failed_mirror;
1815 int in_validation;
1816};
1817
1818static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1819 int did_repair)
1820{
1821 int ret;
1822 int err = 0;
1823 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1824
1825 set_state_private(failure_tree, rec->start, 0);
1826 ret = clear_extent_bits(failure_tree, rec->start,
1827 rec->start + rec->len - 1,
1828 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1829 if (ret)
1830 err = ret;
1831
1832 if (did_repair) {
1833 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1834 rec->start + rec->len - 1,
1835 EXTENT_DAMAGED, GFP_NOFS);
1836 if (ret && !err)
1837 err = ret;
1838 }
1839
1840 kfree(rec);
1841 return err;
1842}
1843
1844static void repair_io_failure_callback(struct bio *bio, int err)
1845{
1846 complete(bio->bi_private);
1847}
1848
1849/*
1850 * this bypasses the standard btrfs submit functions deliberately, as
1851 * the standard behavior is to write all copies in a raid setup. here we only
1852 * want to write the one bad copy. so we do the mapping for ourselves and issue
1853 * submit_bio directly.
1854 * to avoid any synchonization issues, wait for the data after writing, which
1855 * actually prevents the read that triggered the error from finishing.
1856 * currently, there can be no more than two copies of every data bit. thus,
1857 * exactly one rewrite is required.
1858 */
1859int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1860 u64 length, u64 logical, struct page *page,
1861 int mirror_num)
1862{
1863 struct bio *bio;
1864 struct btrfs_device *dev;
1865 DECLARE_COMPLETION_ONSTACK(compl);
1866 u64 map_length = 0;
1867 u64 sector;
1868 struct btrfs_bio *bbio = NULL;
1869 int ret;
1870
1871 BUG_ON(!mirror_num);
1872
1873 bio = bio_alloc(GFP_NOFS, 1);
1874 if (!bio)
1875 return -EIO;
1876 bio->bi_private = &compl;
1877 bio->bi_end_io = repair_io_failure_callback;
1878 bio->bi_size = 0;
1879 map_length = length;
1880
1881 ret = btrfs_map_block(map_tree, WRITE, logical,
1882 &map_length, &bbio, mirror_num);
1883 if (ret) {
1884 bio_put(bio);
1885 return -EIO;
1886 }
1887 BUG_ON(mirror_num != bbio->mirror_num);
1888 sector = bbio->stripes[mirror_num-1].physical >> 9;
1889 bio->bi_sector = sector;
1890 dev = bbio->stripes[mirror_num-1].dev;
1891 kfree(bbio);
1892 if (!dev || !dev->bdev || !dev->writeable) {
1893 bio_put(bio);
1894 return -EIO;
1895 }
1896 bio->bi_bdev = dev->bdev;
1897 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001898 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001899 wait_for_completion(&compl);
1900
1901 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1902 /* try to remap that extent elsewhere? */
1903 bio_put(bio);
1904 return -EIO;
1905 }
1906
1907 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1908 "sector %llu)\n", page->mapping->host->i_ino, start,
1909 dev->name, sector);
1910
1911 bio_put(bio);
1912 return 0;
1913}
1914
1915/*
1916 * each time an IO finishes, we do a fast check in the IO failure tree
1917 * to see if we need to process or clean up an io_failure_record
1918 */
1919static int clean_io_failure(u64 start, struct page *page)
1920{
1921 u64 private;
1922 u64 private_failure;
1923 struct io_failure_record *failrec;
1924 struct btrfs_mapping_tree *map_tree;
1925 struct extent_state *state;
1926 int num_copies;
1927 int did_repair = 0;
1928 int ret;
1929 struct inode *inode = page->mapping->host;
1930
1931 private = 0;
1932 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1933 (u64)-1, 1, EXTENT_DIRTY, 0);
1934 if (!ret)
1935 return 0;
1936
1937 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1938 &private_failure);
1939 if (ret)
1940 return 0;
1941
1942 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1943 BUG_ON(!failrec->this_mirror);
1944
1945 if (failrec->in_validation) {
1946 /* there was no real error, just free the record */
1947 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1948 failrec->start);
1949 did_repair = 1;
1950 goto out;
1951 }
1952
1953 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1954 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1955 failrec->start,
1956 EXTENT_LOCKED);
1957 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1958
1959 if (state && state->start == failrec->start) {
1960 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1961 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1962 failrec->len);
1963 if (num_copies > 1) {
1964 ret = repair_io_failure(map_tree, start, failrec->len,
1965 failrec->logical, page,
1966 failrec->failed_mirror);
1967 did_repair = !ret;
1968 }
1969 }
1970
1971out:
1972 if (!ret)
1973 ret = free_io_failure(inode, failrec, did_repair);
1974
1975 return ret;
1976}
1977
1978/*
1979 * this is a generic handler for readpage errors (default
1980 * readpage_io_failed_hook). if other copies exist, read those and write back
1981 * good data to the failed position. does not investigate in remapping the
1982 * failed extent elsewhere, hoping the device will be smart enough to do this as
1983 * needed
1984 */
1985
1986static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1987 u64 start, u64 end, int failed_mirror,
1988 struct extent_state *state)
1989{
1990 struct io_failure_record *failrec = NULL;
1991 u64 private;
1992 struct extent_map *em;
1993 struct inode *inode = page->mapping->host;
1994 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1995 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1996 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1997 struct bio *bio;
1998 int num_copies;
1999 int ret;
2000 int read_mode;
2001 u64 logical;
2002
2003 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2004
2005 ret = get_state_private(failure_tree, start, &private);
2006 if (ret) {
2007 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2008 if (!failrec)
2009 return -ENOMEM;
2010 failrec->start = start;
2011 failrec->len = end - start + 1;
2012 failrec->this_mirror = 0;
2013 failrec->bio_flags = 0;
2014 failrec->in_validation = 0;
2015
2016 read_lock(&em_tree->lock);
2017 em = lookup_extent_mapping(em_tree, start, failrec->len);
2018 if (!em) {
2019 read_unlock(&em_tree->lock);
2020 kfree(failrec);
2021 return -EIO;
2022 }
2023
2024 if (em->start > start || em->start + em->len < start) {
2025 free_extent_map(em);
2026 em = NULL;
2027 }
2028 read_unlock(&em_tree->lock);
2029
2030 if (!em || IS_ERR(em)) {
2031 kfree(failrec);
2032 return -EIO;
2033 }
2034 logical = start - em->start;
2035 logical = em->block_start + logical;
2036 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2037 logical = em->block_start;
2038 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2039 extent_set_compress_type(&failrec->bio_flags,
2040 em->compress_type);
2041 }
2042 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2043 "len=%llu\n", logical, start, failrec->len);
2044 failrec->logical = logical;
2045 free_extent_map(em);
2046
2047 /* set the bits in the private failure tree */
2048 ret = set_extent_bits(failure_tree, start, end,
2049 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2050 if (ret >= 0)
2051 ret = set_state_private(failure_tree, start,
2052 (u64)(unsigned long)failrec);
2053 /* set the bits in the inode's tree */
2054 if (ret >= 0)
2055 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2056 GFP_NOFS);
2057 if (ret < 0) {
2058 kfree(failrec);
2059 return ret;
2060 }
2061 } else {
2062 failrec = (struct io_failure_record *)(unsigned long)private;
2063 pr_debug("bio_readpage_error: (found) logical=%llu, "
2064 "start=%llu, len=%llu, validation=%d\n",
2065 failrec->logical, failrec->start, failrec->len,
2066 failrec->in_validation);
2067 /*
2068 * when data can be on disk more than twice, add to failrec here
2069 * (e.g. with a list for failed_mirror) to make
2070 * clean_io_failure() clean all those errors at once.
2071 */
2072 }
2073 num_copies = btrfs_num_copies(
2074 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2075 failrec->logical, failrec->len);
2076 if (num_copies == 1) {
2077 /*
2078 * we only have a single copy of the data, so don't bother with
2079 * all the retry and error correction code that follows. no
2080 * matter what the error is, it is very likely to persist.
2081 */
2082 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2083 "state=%p, num_copies=%d, next_mirror %d, "
2084 "failed_mirror %d\n", state, num_copies,
2085 failrec->this_mirror, failed_mirror);
2086 free_io_failure(inode, failrec, 0);
2087 return -EIO;
2088 }
2089
2090 if (!state) {
2091 spin_lock(&tree->lock);
2092 state = find_first_extent_bit_state(tree, failrec->start,
2093 EXTENT_LOCKED);
2094 if (state && state->start != failrec->start)
2095 state = NULL;
2096 spin_unlock(&tree->lock);
2097 }
2098
2099 /*
2100 * there are two premises:
2101 * a) deliver good data to the caller
2102 * b) correct the bad sectors on disk
2103 */
2104 if (failed_bio->bi_vcnt > 1) {
2105 /*
2106 * to fulfill b), we need to know the exact failing sectors, as
2107 * we don't want to rewrite any more than the failed ones. thus,
2108 * we need separate read requests for the failed bio
2109 *
2110 * if the following BUG_ON triggers, our validation request got
2111 * merged. we need separate requests for our algorithm to work.
2112 */
2113 BUG_ON(failrec->in_validation);
2114 failrec->in_validation = 1;
2115 failrec->this_mirror = failed_mirror;
2116 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2117 } else {
2118 /*
2119 * we're ready to fulfill a) and b) alongside. get a good copy
2120 * of the failed sector and if we succeed, we have setup
2121 * everything for repair_io_failure to do the rest for us.
2122 */
2123 if (failrec->in_validation) {
2124 BUG_ON(failrec->this_mirror != failed_mirror);
2125 failrec->in_validation = 0;
2126 failrec->this_mirror = 0;
2127 }
2128 failrec->failed_mirror = failed_mirror;
2129 failrec->this_mirror++;
2130 if (failrec->this_mirror == failed_mirror)
2131 failrec->this_mirror++;
2132 read_mode = READ_SYNC;
2133 }
2134
2135 if (!state || failrec->this_mirror > num_copies) {
2136 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2137 "next_mirror %d, failed_mirror %d\n", state,
2138 num_copies, failrec->this_mirror, failed_mirror);
2139 free_io_failure(inode, failrec, 0);
2140 return -EIO;
2141 }
2142
2143 bio = bio_alloc(GFP_NOFS, 1);
2144 bio->bi_private = state;
2145 bio->bi_end_io = failed_bio->bi_end_io;
2146 bio->bi_sector = failrec->logical >> 9;
2147 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2148 bio->bi_size = 0;
2149
2150 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2151
2152 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2153 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2154 failrec->this_mirror, num_copies, failrec->in_validation);
2155
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002156 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2157 failrec->this_mirror,
2158 failrec->bio_flags, 0);
2159 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002160}
2161
Chris Masond1310b22008-01-24 16:13:08 -05002162/* lots and lots of room for performance fixes in the end_bio funcs */
2163
Jeff Mahoney87826df2012-02-15 16:23:57 +01002164int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2165{
2166 int uptodate = (err == 0);
2167 struct extent_io_tree *tree;
2168 int ret;
2169
2170 tree = &BTRFS_I(page->mapping->host)->io_tree;
2171
2172 if (tree->ops && tree->ops->writepage_end_io_hook) {
2173 ret = tree->ops->writepage_end_io_hook(page, start,
2174 end, NULL, uptodate);
2175 if (ret)
2176 uptodate = 0;
2177 }
2178
2179 if (!uptodate && tree->ops &&
2180 tree->ops->writepage_io_failed_hook) {
2181 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2182 start, end, NULL);
2183 /* Writeback already completed */
2184 if (ret == 0)
2185 return 1;
2186 }
2187
2188 if (!uptodate) {
2189 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2190 ClearPageUptodate(page);
2191 SetPageError(page);
2192 }
2193 return 0;
2194}
2195
Chris Masond1310b22008-01-24 16:13:08 -05002196/*
2197 * after a writepage IO is done, we need to:
2198 * clear the uptodate bits on error
2199 * clear the writeback bits in the extent tree for this IO
2200 * end_page_writeback if the page has no more pending IO
2201 *
2202 * Scheduling is not allowed, so the extent state tree is expected
2203 * to have one and only one object corresponding to this IO.
2204 */
Chris Masond1310b22008-01-24 16:13:08 -05002205static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002206{
Chris Masond1310b22008-01-24 16:13:08 -05002207 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002208 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002209 u64 start;
2210 u64 end;
2211 int whole_page;
2212
Chris Masond1310b22008-01-24 16:13:08 -05002213 do {
2214 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002215 tree = &BTRFS_I(page->mapping->host)->io_tree;
2216
Chris Masond1310b22008-01-24 16:13:08 -05002217 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2218 bvec->bv_offset;
2219 end = start + bvec->bv_len - 1;
2220
2221 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2222 whole_page = 1;
2223 else
2224 whole_page = 0;
2225
2226 if (--bvec >= bio->bi_io_vec)
2227 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002228
Jeff Mahoney87826df2012-02-15 16:23:57 +01002229 if (end_extent_writepage(page, err, start, end))
2230 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002231
Chris Masond1310b22008-01-24 16:13:08 -05002232 if (whole_page)
2233 end_page_writeback(page);
2234 else
2235 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002236 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002237
Chris Masond1310b22008-01-24 16:13:08 -05002238 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002239}
2240
2241/*
2242 * after a readpage IO is done, we need to:
2243 * clear the uptodate bits on error
2244 * set the uptodate bits if things worked
2245 * set the page up to date if all extents in the tree are uptodate
2246 * clear the lock bit in the extent tree
2247 * unlock the page if there are no other extents locked for it
2248 *
2249 * Scheduling is not allowed, so the extent state tree is expected
2250 * to have one and only one object corresponding to this IO.
2251 */
Chris Masond1310b22008-01-24 16:13:08 -05002252static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002253{
2254 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002255 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2256 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002257 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002258 u64 start;
2259 u64 end;
2260 int whole_page;
2261 int ret;
2262
Chris Masond20f7042008-12-08 16:58:54 -05002263 if (err)
2264 uptodate = 0;
2265
Chris Masond1310b22008-01-24 16:13:08 -05002266 do {
2267 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002268 struct extent_state *cached = NULL;
2269 struct extent_state *state;
2270
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002271 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2272 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2273 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002274 tree = &BTRFS_I(page->mapping->host)->io_tree;
2275
Chris Masond1310b22008-01-24 16:13:08 -05002276 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2277 bvec->bv_offset;
2278 end = start + bvec->bv_len - 1;
2279
2280 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2281 whole_page = 1;
2282 else
2283 whole_page = 0;
2284
Chris Mason4125bf72010-02-03 18:18:45 +00002285 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002286 prefetchw(&bvec->bv_page->flags);
2287
Arne Jansen507903b2011-04-06 10:02:20 +00002288 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002289 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002290 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002291 /*
2292 * take a reference on the state, unlock will drop
2293 * the ref
2294 */
2295 cache_state(state, &cached);
2296 }
2297 spin_unlock(&tree->lock);
2298
Chris Masond1310b22008-01-24 16:13:08 -05002299 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002300 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002301 state);
Chris Masond1310b22008-01-24 16:13:08 -05002302 if (ret)
2303 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002304 else
2305 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002306 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 if (!uptodate) {
Jan Schmidt32240a92011-11-20 07:33:38 -05002308 int failed_mirror;
2309 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002310 /*
2311 * The generic bio_readpage_error handles errors the
2312 * following way: If possible, new read requests are
2313 * created and submitted and will end up in
2314 * end_bio_extent_readpage as well (if we're lucky, not
2315 * in the !uptodate case). In that case it returns 0 and
2316 * we just go on with the next page in our bio. If it
2317 * can't handle the error it will return -EIO and we
2318 * remain responsible for that page.
2319 */
2320 ret = bio_readpage_error(bio, page, start, end,
2321 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002322 if (ret == 0) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002323error_handled:
Chris Mason3b951512008-04-17 11:29:12 -04002324 uptodate =
2325 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002326 if (err)
2327 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002328 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002329 continue;
2330 }
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002331 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2332 ret = tree->ops->readpage_io_failed_hook(
2333 bio, page, start, end,
2334 failed_mirror, state);
2335 if (ret == 0)
2336 goto error_handled;
2337 }
Chris Mason7e383262008-04-09 16:28:12 -04002338 }
Chris Mason70dec802008-01-29 09:59:12 -05002339
Chris Mason771ed682008-11-06 22:02:51 -05002340 if (uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002341 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002342 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002343 }
Arne Jansen507903b2011-04-06 10:02:20 +00002344 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002345
Chris Mason70dec802008-01-29 09:59:12 -05002346 if (whole_page) {
2347 if (uptodate) {
2348 SetPageUptodate(page);
2349 } else {
2350 ClearPageUptodate(page);
2351 SetPageError(page);
2352 }
Chris Masond1310b22008-01-24 16:13:08 -05002353 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002354 } else {
2355 if (uptodate) {
2356 check_page_uptodate(tree, page);
2357 } else {
2358 ClearPageUptodate(page);
2359 SetPageError(page);
2360 }
Chris Masond1310b22008-01-24 16:13:08 -05002361 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002362 }
Chris Mason4125bf72010-02-03 18:18:45 +00002363 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002364
2365 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002366}
2367
Miao Xie88f794e2010-11-22 03:02:55 +00002368struct bio *
2369btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2370 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002371{
2372 struct bio *bio;
2373
2374 bio = bio_alloc(gfp_flags, nr_vecs);
2375
2376 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2377 while (!bio && (nr_vecs /= 2))
2378 bio = bio_alloc(gfp_flags, nr_vecs);
2379 }
2380
2381 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002382 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002383 bio->bi_bdev = bdev;
2384 bio->bi_sector = first_sector;
2385 }
2386 return bio;
2387}
2388
Chris Masonc8b97812008-10-29 14:49:59 -04002389static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2390 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002391{
Chris Masond1310b22008-01-24 16:13:08 -05002392 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002393 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2394 struct page *page = bvec->bv_page;
2395 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002396 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002397
2398 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002399
David Woodhouse902b22f2008-08-20 08:51:49 -04002400 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002401
2402 bio_get(bio);
2403
Chris Mason065631f2008-02-20 12:07:25 -05002404 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002405 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002406 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002407 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002408 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002409
Chris Masond1310b22008-01-24 16:13:08 -05002410 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2411 ret = -EOPNOTSUPP;
2412 bio_put(bio);
2413 return ret;
2414}
2415
2416static int submit_extent_page(int rw, struct extent_io_tree *tree,
2417 struct page *page, sector_t sector,
2418 size_t size, unsigned long offset,
2419 struct block_device *bdev,
2420 struct bio **bio_ret,
2421 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002422 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002423 int mirror_num,
2424 unsigned long prev_bio_flags,
2425 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002426{
2427 int ret = 0;
2428 struct bio *bio;
2429 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002430 int contig = 0;
2431 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2432 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002433 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002434
2435 if (bio_ret && *bio_ret) {
2436 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002437 if (old_compressed)
2438 contig = bio->bi_sector == sector;
2439 else
2440 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2441 sector;
2442
2443 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002444 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002445 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2446 bio_flags)) ||
2447 bio_add_page(bio, page, page_size, offset) < page_size) {
2448 ret = submit_one_bio(rw, bio, mirror_num,
2449 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002450 bio = NULL;
2451 } else {
2452 return 0;
2453 }
2454 }
Chris Masonc8b97812008-10-29 14:49:59 -04002455 if (this_compressed)
2456 nr = BIO_MAX_PAGES;
2457 else
2458 nr = bio_get_nr_vecs(bdev);
2459
Miao Xie88f794e2010-11-22 03:02:55 +00002460 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002461 if (!bio)
2462 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002463
Chris Masonc8b97812008-10-29 14:49:59 -04002464 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002465 bio->bi_end_io = end_io_func;
2466 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002467
Chris Masond3977122009-01-05 21:25:51 -05002468 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002469 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002470 else
Chris Masonc8b97812008-10-29 14:49:59 -04002471 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002472
2473 return ret;
2474}
2475
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002476void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2477{
2478 if (!PagePrivate(page)) {
2479 SetPagePrivate(page);
2480 page_cache_get(page);
2481 set_page_private(page, (unsigned long)eb);
2482 } else {
2483 WARN_ON(page->private != (unsigned long)eb);
2484 }
2485}
2486
Chris Masond1310b22008-01-24 16:13:08 -05002487void set_page_extent_mapped(struct page *page)
2488{
2489 if (!PagePrivate(page)) {
2490 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002491 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002492 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002493 }
2494}
2495
Chris Masond1310b22008-01-24 16:13:08 -05002496/*
2497 * basic readpage implementation. Locked extent state structs are inserted
2498 * into the tree that are removed when the IO is done (by the end_io
2499 * handlers)
2500 */
2501static int __extent_read_full_page(struct extent_io_tree *tree,
2502 struct page *page,
2503 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002504 struct bio **bio, int mirror_num,
2505 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002506{
2507 struct inode *inode = page->mapping->host;
2508 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2509 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2510 u64 end;
2511 u64 cur = start;
2512 u64 extent_offset;
2513 u64 last_byte = i_size_read(inode);
2514 u64 block_start;
2515 u64 cur_end;
2516 sector_t sector;
2517 struct extent_map *em;
2518 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002519 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002520 int ret;
2521 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002522 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002523 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002524 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002525 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002526 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002527
2528 set_page_extent_mapped(page);
2529
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002530 if (!PageUptodate(page)) {
2531 if (cleancache_get_page(page) == 0) {
2532 BUG_ON(blocksize != PAGE_SIZE);
2533 goto out;
2534 }
2535 }
2536
Chris Masond1310b22008-01-24 16:13:08 -05002537 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002538 while (1) {
2539 lock_extent(tree, start, end, GFP_NOFS);
2540 ordered = btrfs_lookup_ordered_extent(inode, start);
2541 if (!ordered)
2542 break;
2543 unlock_extent(tree, start, end, GFP_NOFS);
2544 btrfs_start_ordered_extent(inode, ordered, 1);
2545 btrfs_put_ordered_extent(ordered);
2546 }
Chris Masond1310b22008-01-24 16:13:08 -05002547
Chris Masonc8b97812008-10-29 14:49:59 -04002548 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2549 char *userpage;
2550 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2551
2552 if (zero_offset) {
2553 iosize = PAGE_CACHE_SIZE - zero_offset;
2554 userpage = kmap_atomic(page, KM_USER0);
2555 memset(userpage + zero_offset, 0, iosize);
2556 flush_dcache_page(page);
2557 kunmap_atomic(userpage, KM_USER0);
2558 }
2559 }
Chris Masond1310b22008-01-24 16:13:08 -05002560 while (cur <= end) {
2561 if (cur >= last_byte) {
2562 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002563 struct extent_state *cached = NULL;
2564
David Sterba306e16c2011-04-19 14:29:38 +02002565 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002566 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002567 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002568 flush_dcache_page(page);
2569 kunmap_atomic(userpage, KM_USER0);
2570 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002571 &cached, GFP_NOFS);
2572 unlock_extent_cached(tree, cur, cur + iosize - 1,
2573 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002574 break;
2575 }
David Sterba306e16c2011-04-19 14:29:38 +02002576 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002577 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002578 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002579 SetPageError(page);
2580 unlock_extent(tree, cur, end, GFP_NOFS);
2581 break;
2582 }
Chris Masond1310b22008-01-24 16:13:08 -05002583 extent_offset = cur - em->start;
2584 BUG_ON(extent_map_end(em) <= cur);
2585 BUG_ON(end < cur);
2586
Li Zefan261507a02010-12-17 14:21:50 +08002587 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002588 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002589 extent_set_compress_type(&this_bio_flag,
2590 em->compress_type);
2591 }
Chris Masonc8b97812008-10-29 14:49:59 -04002592
Chris Masond1310b22008-01-24 16:13:08 -05002593 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2594 cur_end = min(extent_map_end(em) - 1, end);
2595 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002596 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2597 disk_io_size = em->block_len;
2598 sector = em->block_start >> 9;
2599 } else {
2600 sector = (em->block_start + extent_offset) >> 9;
2601 disk_io_size = iosize;
2602 }
Chris Masond1310b22008-01-24 16:13:08 -05002603 bdev = em->bdev;
2604 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002605 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2606 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002607 free_extent_map(em);
2608 em = NULL;
2609
2610 /* we've found a hole, just zero and go on */
2611 if (block_start == EXTENT_MAP_HOLE) {
2612 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002613 struct extent_state *cached = NULL;
2614
Chris Masond1310b22008-01-24 16:13:08 -05002615 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002616 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002617 flush_dcache_page(page);
2618 kunmap_atomic(userpage, KM_USER0);
2619
2620 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002621 &cached, GFP_NOFS);
2622 unlock_extent_cached(tree, cur, cur + iosize - 1,
2623 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002624 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002625 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002626 continue;
2627 }
2628 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002629 if (test_range_bit(tree, cur, cur_end,
2630 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002631 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002632 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2633 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002634 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002635 continue;
2636 }
Chris Mason70dec802008-01-29 09:59:12 -05002637 /* we have an inline extent but it didn't get marked up
2638 * to date. Error out
2639 */
2640 if (block_start == EXTENT_MAP_INLINE) {
2641 SetPageError(page);
2642 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2643 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002644 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002645 continue;
2646 }
Chris Masond1310b22008-01-24 16:13:08 -05002647
2648 ret = 0;
2649 if (tree->ops && tree->ops->readpage_io_hook) {
2650 ret = tree->ops->readpage_io_hook(page, cur,
2651 cur + iosize - 1);
2652 }
2653 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002654 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2655 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002656 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002657 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002658 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002659 end_bio_extent_readpage, mirror_num,
2660 *bio_flags,
2661 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002662 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002663 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002664 }
2665 if (ret)
2666 SetPageError(page);
2667 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002668 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002669 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002670out:
Chris Masond1310b22008-01-24 16:13:08 -05002671 if (!nr) {
2672 if (!PageError(page))
2673 SetPageUptodate(page);
2674 unlock_page(page);
2675 }
2676 return 0;
2677}
2678
2679int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002680 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002681{
2682 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002683 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002684 int ret;
2685
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002686 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002687 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002688 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002689 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002690 return ret;
2691}
Chris Masond1310b22008-01-24 16:13:08 -05002692
Chris Mason11c83492009-04-20 15:50:09 -04002693static noinline void update_nr_written(struct page *page,
2694 struct writeback_control *wbc,
2695 unsigned long nr_written)
2696{
2697 wbc->nr_to_write -= nr_written;
2698 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2699 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2700 page->mapping->writeback_index = page->index + nr_written;
2701}
2702
Chris Masond1310b22008-01-24 16:13:08 -05002703/*
2704 * the writepage semantics are similar to regular writepage. extent
2705 * records are inserted to lock ranges in the tree, and as dirty areas
2706 * are found, they are marked writeback. Then the lock bits are removed
2707 * and the end_io handler clears the writeback ranges
2708 */
2709static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2710 void *data)
2711{
2712 struct inode *inode = page->mapping->host;
2713 struct extent_page_data *epd = data;
2714 struct extent_io_tree *tree = epd->tree;
2715 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2716 u64 delalloc_start;
2717 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2718 u64 end;
2719 u64 cur = start;
2720 u64 extent_offset;
2721 u64 last_byte = i_size_read(inode);
2722 u64 block_start;
2723 u64 iosize;
2724 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002725 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002726 struct extent_map *em;
2727 struct block_device *bdev;
2728 int ret;
2729 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002730 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002731 size_t blocksize;
2732 loff_t i_size = i_size_read(inode);
2733 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2734 u64 nr_delalloc;
2735 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002736 int page_started;
2737 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002738 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002739 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002740 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002741
Chris Masonffbd5172009-04-20 15:50:09 -04002742 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002743 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002744 else
2745 write_flags = WRITE;
2746
liubo1abe9b82011-03-24 11:18:59 +00002747 trace___extent_writepage(page, inode, wbc);
2748
Chris Masond1310b22008-01-24 16:13:08 -05002749 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002750
2751 ClearPageError(page);
2752
Chris Mason7f3c74f2008-07-18 12:01:11 -04002753 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002754 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002755 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002756 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002757 unlock_page(page);
2758 return 0;
2759 }
2760
2761 if (page->index == end_index) {
2762 char *userpage;
2763
Chris Masond1310b22008-01-24 16:13:08 -05002764 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002765 memset(userpage + pg_offset, 0,
2766 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002767 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002768 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002769 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002770 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002771
2772 set_page_extent_mapped(page);
2773
Josef Bacik9e487102011-08-01 12:08:18 -04002774 if (!tree->ops || !tree->ops->fill_delalloc)
2775 fill_delalloc = false;
2776
Chris Masond1310b22008-01-24 16:13:08 -05002777 delalloc_start = start;
2778 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002779 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002780 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002781 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002782 /*
2783 * make sure the wbc mapping index is at least updated
2784 * to this page.
2785 */
2786 update_nr_written(page, wbc, 0);
2787
Chris Masond3977122009-01-05 21:25:51 -05002788 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002789 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002790 page,
2791 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002792 &delalloc_end,
2793 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002794 if (nr_delalloc == 0) {
2795 delalloc_start = delalloc_end + 1;
2796 continue;
2797 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002798 ret = tree->ops->fill_delalloc(inode, page,
2799 delalloc_start,
2800 delalloc_end,
2801 &page_started,
2802 &nr_written);
2803 BUG_ON(ret);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002804 /*
2805 * delalloc_end is already one less than the total
2806 * length, so we don't subtract one from
2807 * PAGE_CACHE_SIZE
2808 */
2809 delalloc_to_write += (delalloc_end - delalloc_start +
2810 PAGE_CACHE_SIZE) >>
2811 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002812 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002813 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002814 if (wbc->nr_to_write < delalloc_to_write) {
2815 int thresh = 8192;
2816
2817 if (delalloc_to_write < thresh * 2)
2818 thresh = delalloc_to_write;
2819 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2820 thresh);
2821 }
Chris Masonc8b97812008-10-29 14:49:59 -04002822
Chris Mason771ed682008-11-06 22:02:51 -05002823 /* did the fill delalloc function already unlock and start
2824 * the IO?
2825 */
2826 if (page_started) {
2827 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002828 /*
2829 * we've unlocked the page, so we can't update
2830 * the mapping's writeback index, just update
2831 * nr_to_write.
2832 */
2833 wbc->nr_to_write -= nr_written;
2834 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002835 }
Chris Masonc8b97812008-10-29 14:49:59 -04002836 }
Chris Mason247e7432008-07-17 12:53:51 -04002837 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002838 ret = tree->ops->writepage_start_hook(page, start,
2839 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002840 if (ret) {
2841 /* Fixup worker will requeue */
2842 if (ret == -EBUSY)
2843 wbc->pages_skipped++;
2844 else
2845 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002846 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002847 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002848 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002849 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002850 }
2851 }
2852
Chris Mason11c83492009-04-20 15:50:09 -04002853 /*
2854 * we don't want to touch the inode after unlocking the page,
2855 * so we update the mapping writeback index now
2856 */
2857 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002858
Chris Masond1310b22008-01-24 16:13:08 -05002859 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002860 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002861 if (tree->ops && tree->ops->writepage_end_io_hook)
2862 tree->ops->writepage_end_io_hook(page, start,
2863 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002864 goto done;
2865 }
2866
Chris Masond1310b22008-01-24 16:13:08 -05002867 blocksize = inode->i_sb->s_blocksize;
2868
2869 while (cur <= end) {
2870 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002871 if (tree->ops && tree->ops->writepage_end_io_hook)
2872 tree->ops->writepage_end_io_hook(page, cur,
2873 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002874 break;
2875 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002876 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002877 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002878 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002879 SetPageError(page);
2880 break;
2881 }
2882
2883 extent_offset = cur - em->start;
2884 BUG_ON(extent_map_end(em) <= cur);
2885 BUG_ON(end < cur);
2886 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2887 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2888 sector = (em->block_start + extent_offset) >> 9;
2889 bdev = em->bdev;
2890 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002891 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002892 free_extent_map(em);
2893 em = NULL;
2894
Chris Masonc8b97812008-10-29 14:49:59 -04002895 /*
2896 * compressed and inline extents are written through other
2897 * paths in the FS
2898 */
2899 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002900 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002901 /*
2902 * end_io notification does not happen here for
2903 * compressed extents
2904 */
2905 if (!compressed && tree->ops &&
2906 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002907 tree->ops->writepage_end_io_hook(page, cur,
2908 cur + iosize - 1,
2909 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002910 else if (compressed) {
2911 /* we don't want to end_page_writeback on
2912 * a compressed extent. this happens
2913 * elsewhere
2914 */
2915 nr++;
2916 }
2917
2918 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002919 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002920 continue;
2921 }
Chris Masond1310b22008-01-24 16:13:08 -05002922 /* leave this out until we have a page_mkwrite call */
2923 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002924 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002925 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002926 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002927 continue;
2928 }
Chris Masonc8b97812008-10-29 14:49:59 -04002929
Chris Masond1310b22008-01-24 16:13:08 -05002930 if (tree->ops && tree->ops->writepage_io_hook) {
2931 ret = tree->ops->writepage_io_hook(page, cur,
2932 cur + iosize - 1);
2933 } else {
2934 ret = 0;
2935 }
Chris Mason1259ab72008-05-12 13:39:03 -04002936 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002937 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002938 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002939 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002940
Chris Masond1310b22008-01-24 16:13:08 -05002941 set_range_writeback(tree, cur, cur + iosize - 1);
2942 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002943 printk(KERN_ERR "btrfs warning page %lu not "
2944 "writeback, cur %llu end %llu\n",
2945 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002946 (unsigned long long)end);
2947 }
2948
Chris Masonffbd5172009-04-20 15:50:09 -04002949 ret = submit_extent_page(write_flags, tree, page,
2950 sector, iosize, pg_offset,
2951 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002952 end_bio_extent_writepage,
2953 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002954 if (ret)
2955 SetPageError(page);
2956 }
2957 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002958 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002959 nr++;
2960 }
2961done:
2962 if (nr == 0) {
2963 /* make sure the mapping tag for page dirty gets cleared */
2964 set_page_writeback(page);
2965 end_page_writeback(page);
2966 }
Chris Masond1310b22008-01-24 16:13:08 -05002967 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002968
Chris Mason11c83492009-04-20 15:50:09 -04002969done_unlocked:
2970
Chris Mason2c64c532009-09-02 15:04:12 -04002971 /* drop our reference on any cached states */
2972 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002973 return 0;
2974}
2975
Chris Masond1310b22008-01-24 16:13:08 -05002976/**
Chris Mason4bef0842008-09-08 11:18:08 -04002977 * 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 -05002978 * @mapping: address space structure to write
2979 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2980 * @writepage: function called for each page
2981 * @data: data passed to writepage function
2982 *
2983 * If a page is already under I/O, write_cache_pages() skips it, even
2984 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2985 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2986 * and msync() need to guarantee that all the data which was dirty at the time
2987 * the call was made get new I/O started against them. If wbc->sync_mode is
2988 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2989 * existing IO to complete.
2990 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002991static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002992 struct address_space *mapping,
2993 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002994 writepage_t writepage, void *data,
2995 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002996{
Chris Masond1310b22008-01-24 16:13:08 -05002997 int ret = 0;
2998 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002999 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003000 struct pagevec pvec;
3001 int nr_pages;
3002 pgoff_t index;
3003 pgoff_t end; /* Inclusive */
3004 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003005 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003006
Chris Masond1310b22008-01-24 16:13:08 -05003007 pagevec_init(&pvec, 0);
3008 if (wbc->range_cyclic) {
3009 index = mapping->writeback_index; /* Start from prev offset */
3010 end = -1;
3011 } else {
3012 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3013 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003014 scanned = 1;
3015 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003016 if (wbc->sync_mode == WB_SYNC_ALL)
3017 tag = PAGECACHE_TAG_TOWRITE;
3018 else
3019 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003020retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003021 if (wbc->sync_mode == WB_SYNC_ALL)
3022 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003023 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003024 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3025 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003026 unsigned i;
3027
3028 scanned = 1;
3029 for (i = 0; i < nr_pages; i++) {
3030 struct page *page = pvec.pages[i];
3031
3032 /*
3033 * At this point we hold neither mapping->tree_lock nor
3034 * lock on the page itself: the page may be truncated or
3035 * invalidated (changing page->mapping to NULL), or even
3036 * swizzled back from swapper_space to tmpfs file
3037 * mapping
3038 */
Chris Mason01d658f2011-11-01 10:08:06 -04003039 if (tree->ops &&
3040 tree->ops->write_cache_pages_lock_hook) {
3041 tree->ops->write_cache_pages_lock_hook(page,
3042 data, flush_fn);
3043 } else {
3044 if (!trylock_page(page)) {
3045 flush_fn(data);
3046 lock_page(page);
3047 }
3048 }
Chris Masond1310b22008-01-24 16:13:08 -05003049
3050 if (unlikely(page->mapping != mapping)) {
3051 unlock_page(page);
3052 continue;
3053 }
3054
3055 if (!wbc->range_cyclic && page->index > end) {
3056 done = 1;
3057 unlock_page(page);
3058 continue;
3059 }
3060
Chris Masond2c3f4f2008-11-19 12:44:22 -05003061 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003062 if (PageWriteback(page))
3063 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003064 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003065 }
Chris Masond1310b22008-01-24 16:13:08 -05003066
3067 if (PageWriteback(page) ||
3068 !clear_page_dirty_for_io(page)) {
3069 unlock_page(page);
3070 continue;
3071 }
3072
3073 ret = (*writepage)(page, wbc, data);
3074
3075 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3076 unlock_page(page);
3077 ret = 0;
3078 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003079 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003080 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003081
3082 /*
3083 * the filesystem may choose to bump up nr_to_write.
3084 * We have to make sure to honor the new nr_to_write
3085 * at any time
3086 */
3087 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003088 }
3089 pagevec_release(&pvec);
3090 cond_resched();
3091 }
3092 if (!scanned && !done) {
3093 /*
3094 * We hit the last page and there is more work to be done: wrap
3095 * back to the start of the file
3096 */
3097 scanned = 1;
3098 index = 0;
3099 goto retry;
3100 }
Chris Masond1310b22008-01-24 16:13:08 -05003101 return ret;
3102}
Chris Masond1310b22008-01-24 16:13:08 -05003103
Chris Masonffbd5172009-04-20 15:50:09 -04003104static void flush_epd_write_bio(struct extent_page_data *epd)
3105{
3106 if (epd->bio) {
3107 if (epd->sync_io)
3108 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3109 else
3110 submit_one_bio(WRITE, epd->bio, 0, 0);
3111 epd->bio = NULL;
3112 }
3113}
3114
Chris Masond2c3f4f2008-11-19 12:44:22 -05003115static noinline void flush_write_bio(void *data)
3116{
3117 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003118 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003119}
3120
Chris Masond1310b22008-01-24 16:13:08 -05003121int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3122 get_extent_t *get_extent,
3123 struct writeback_control *wbc)
3124{
3125 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003126 struct extent_page_data epd = {
3127 .bio = NULL,
3128 .tree = tree,
3129 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003130 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003131 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003132 };
Chris Masond1310b22008-01-24 16:13:08 -05003133
Chris Masond1310b22008-01-24 16:13:08 -05003134 ret = __extent_writepage(page, wbc, &epd);
3135
Chris Masonffbd5172009-04-20 15:50:09 -04003136 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003137 return ret;
3138}
Chris Masond1310b22008-01-24 16:13:08 -05003139
Chris Mason771ed682008-11-06 22:02:51 -05003140int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3141 u64 start, u64 end, get_extent_t *get_extent,
3142 int mode)
3143{
3144 int ret = 0;
3145 struct address_space *mapping = inode->i_mapping;
3146 struct page *page;
3147 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3148 PAGE_CACHE_SHIFT;
3149
3150 struct extent_page_data epd = {
3151 .bio = NULL,
3152 .tree = tree,
3153 .get_extent = get_extent,
3154 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003155 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003156 };
3157 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003158 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003159 .nr_to_write = nr_pages * 2,
3160 .range_start = start,
3161 .range_end = end + 1,
3162 };
3163
Chris Masond3977122009-01-05 21:25:51 -05003164 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003165 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3166 if (clear_page_dirty_for_io(page))
3167 ret = __extent_writepage(page, &wbc_writepages, &epd);
3168 else {
3169 if (tree->ops && tree->ops->writepage_end_io_hook)
3170 tree->ops->writepage_end_io_hook(page, start,
3171 start + PAGE_CACHE_SIZE - 1,
3172 NULL, 1);
3173 unlock_page(page);
3174 }
3175 page_cache_release(page);
3176 start += PAGE_CACHE_SIZE;
3177 }
3178
Chris Masonffbd5172009-04-20 15:50:09 -04003179 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003180 return ret;
3181}
Chris Masond1310b22008-01-24 16:13:08 -05003182
3183int extent_writepages(struct extent_io_tree *tree,
3184 struct address_space *mapping,
3185 get_extent_t *get_extent,
3186 struct writeback_control *wbc)
3187{
3188 int ret = 0;
3189 struct extent_page_data epd = {
3190 .bio = NULL,
3191 .tree = tree,
3192 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003193 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003194 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003195 };
3196
Chris Mason4bef0842008-09-08 11:18:08 -04003197 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003198 __extent_writepage, &epd,
3199 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003200 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003201 return ret;
3202}
Chris Masond1310b22008-01-24 16:13:08 -05003203
3204int extent_readpages(struct extent_io_tree *tree,
3205 struct address_space *mapping,
3206 struct list_head *pages, unsigned nr_pages,
3207 get_extent_t get_extent)
3208{
3209 struct bio *bio = NULL;
3210 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003211 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003212
Chris Masond1310b22008-01-24 16:13:08 -05003213 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3214 struct page *page = list_entry(pages->prev, struct page, lru);
3215
3216 prefetchw(&page->flags);
3217 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003218 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003219 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003220 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003221 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003222 }
3223 page_cache_release(page);
3224 }
Chris Masond1310b22008-01-24 16:13:08 -05003225 BUG_ON(!list_empty(pages));
3226 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003227 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003228 return 0;
3229}
Chris Masond1310b22008-01-24 16:13:08 -05003230
3231/*
3232 * basic invalidatepage code, this waits on any locked or writeback
3233 * ranges corresponding to the page, and then deletes any extent state
3234 * records from the tree
3235 */
3236int extent_invalidatepage(struct extent_io_tree *tree,
3237 struct page *page, unsigned long offset)
3238{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003239 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003240 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3241 u64 end = start + PAGE_CACHE_SIZE - 1;
3242 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3243
Chris Masond3977122009-01-05 21:25:51 -05003244 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003245 if (start > end)
3246 return 0;
3247
Josef Bacik2ac55d42010-02-03 19:33:23 +00003248 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003249 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003250 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003251 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3252 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003253 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003254 return 0;
3255}
Chris Masond1310b22008-01-24 16:13:08 -05003256
3257/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003258 * a helper for releasepage, this tests for areas of the page that
3259 * are locked or under IO and drops the related state bits if it is safe
3260 * to drop the page.
3261 */
3262int try_release_extent_state(struct extent_map_tree *map,
3263 struct extent_io_tree *tree, struct page *page,
3264 gfp_t mask)
3265{
3266 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3267 u64 end = start + PAGE_CACHE_SIZE - 1;
3268 int ret = 1;
3269
Chris Mason211f90e2008-07-18 11:56:15 -04003270 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003271 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003272 ret = 0;
3273 else {
3274 if ((mask & GFP_NOFS) == GFP_NOFS)
3275 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003276 /*
3277 * at this point we can safely clear everything except the
3278 * locked bit and the nodatasum bit
3279 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003280 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003281 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3282 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003283
3284 /* if clear_extent_bit failed for enomem reasons,
3285 * we can't allow the release to continue.
3286 */
3287 if (ret < 0)
3288 ret = 0;
3289 else
3290 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003291 }
3292 return ret;
3293}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003294
3295/*
Chris Masond1310b22008-01-24 16:13:08 -05003296 * a helper for releasepage. As long as there are no locked extents
3297 * in the range corresponding to the page, both state records and extent
3298 * map records are removed
3299 */
3300int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003301 struct extent_io_tree *tree, struct page *page,
3302 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003303{
3304 struct extent_map *em;
3305 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3306 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003307
Chris Mason70dec802008-01-29 09:59:12 -05003308 if ((mask & __GFP_WAIT) &&
3309 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003310 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003311 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003312 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003313 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003314 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003315 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003316 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003317 break;
3318 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003319 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3320 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003321 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003322 free_extent_map(em);
3323 break;
3324 }
3325 if (!test_range_bit(tree, em->start,
3326 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003327 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003328 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003329 remove_extent_mapping(map, em);
3330 /* once for the rb tree */
3331 free_extent_map(em);
3332 }
3333 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003334 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003335
3336 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003337 free_extent_map(em);
3338 }
Chris Masond1310b22008-01-24 16:13:08 -05003339 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003340 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003341}
Chris Masond1310b22008-01-24 16:13:08 -05003342
Chris Masonec29ed52011-02-23 16:23:20 -05003343/*
3344 * helper function for fiemap, which doesn't want to see any holes.
3345 * This maps until we find something past 'last'
3346 */
3347static struct extent_map *get_extent_skip_holes(struct inode *inode,
3348 u64 offset,
3349 u64 last,
3350 get_extent_t *get_extent)
3351{
3352 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3353 struct extent_map *em;
3354 u64 len;
3355
3356 if (offset >= last)
3357 return NULL;
3358
3359 while(1) {
3360 len = last - offset;
3361 if (len == 0)
3362 break;
3363 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3364 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003365 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003366 return em;
3367
3368 /* if this isn't a hole return it */
3369 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3370 em->block_start != EXTENT_MAP_HOLE) {
3371 return em;
3372 }
3373
3374 /* this is a hole, advance to the next extent */
3375 offset = extent_map_end(em);
3376 free_extent_map(em);
3377 if (offset >= last)
3378 break;
3379 }
3380 return NULL;
3381}
3382
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003383int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3384 __u64 start, __u64 len, get_extent_t *get_extent)
3385{
Josef Bacik975f84f2010-11-23 19:36:57 +00003386 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003387 u64 off = start;
3388 u64 max = start + len;
3389 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003390 u32 found_type;
3391 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003392 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003393 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003394 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003395 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003396 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003397 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003398 struct btrfs_path *path;
3399 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003400 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003401 u64 em_start = 0;
3402 u64 em_len = 0;
3403 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003404 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003405
3406 if (len == 0)
3407 return -EINVAL;
3408
Josef Bacik975f84f2010-11-23 19:36:57 +00003409 path = btrfs_alloc_path();
3410 if (!path)
3411 return -ENOMEM;
3412 path->leave_spinning = 1;
3413
Josef Bacik4d479cf2011-11-17 11:34:31 -05003414 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3415 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3416
Chris Masonec29ed52011-02-23 16:23:20 -05003417 /*
3418 * lookup the last file extent. We're not using i_size here
3419 * because there might be preallocation past i_size
3420 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003421 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003422 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003423 if (ret < 0) {
3424 btrfs_free_path(path);
3425 return ret;
3426 }
3427 WARN_ON(!ret);
3428 path->slots[0]--;
3429 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3430 struct btrfs_file_extent_item);
3431 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3432 found_type = btrfs_key_type(&found_key);
3433
Chris Masonec29ed52011-02-23 16:23:20 -05003434 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003435 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003436 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003437 /* have to trust i_size as the end */
3438 last = (u64)-1;
3439 last_for_get_extent = isize;
3440 } else {
3441 /*
3442 * remember the start of the last extent. There are a
3443 * bunch of different factors that go into the length of the
3444 * extent, so its much less complex to remember where it started
3445 */
3446 last = found_key.offset;
3447 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003448 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003449 btrfs_free_path(path);
3450
Chris Masonec29ed52011-02-23 16:23:20 -05003451 /*
3452 * we might have some extents allocated but more delalloc past those
3453 * extents. so, we trust isize unless the start of the last extent is
3454 * beyond isize
3455 */
3456 if (last < isize) {
3457 last = (u64)-1;
3458 last_for_get_extent = isize;
3459 }
3460
Josef Bacik2ac55d42010-02-03 19:33:23 +00003461 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3462 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003463
Josef Bacik4d479cf2011-11-17 11:34:31 -05003464 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003465 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003466 if (!em)
3467 goto out;
3468 if (IS_ERR(em)) {
3469 ret = PTR_ERR(em);
3470 goto out;
3471 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003472
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003473 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003474 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003475
Chris Masonea8efc72011-03-08 11:54:40 -05003476 /* break if the extent we found is outside the range */
3477 if (em->start >= max || extent_map_end(em) < off)
3478 break;
3479
3480 /*
3481 * get_extent may return an extent that starts before our
3482 * requested range. We have to make sure the ranges
3483 * we return to fiemap always move forward and don't
3484 * overlap, so adjust the offsets here
3485 */
3486 em_start = max(em->start, off);
3487
3488 /*
3489 * record the offset from the start of the extent
3490 * for adjusting the disk offset below
3491 */
3492 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003493 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003494 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003495 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003496 disko = 0;
3497 flags = 0;
3498
Chris Masonea8efc72011-03-08 11:54:40 -05003499 /*
3500 * bump off for our next call to get_extent
3501 */
3502 off = extent_map_end(em);
3503 if (off >= max)
3504 end = 1;
3505
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003506 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003507 end = 1;
3508 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003509 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003510 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3511 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003512 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003513 flags |= (FIEMAP_EXTENT_DELALLOC |
3514 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003515 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003516 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003517 }
3518 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3519 flags |= FIEMAP_EXTENT_ENCODED;
3520
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003521 free_extent_map(em);
3522 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003523 if ((em_start >= last) || em_len == (u64)-1 ||
3524 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003525 flags |= FIEMAP_EXTENT_LAST;
3526 end = 1;
3527 }
3528
Chris Masonec29ed52011-02-23 16:23:20 -05003529 /* now scan forward to see if this is really the last extent. */
3530 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3531 get_extent);
3532 if (IS_ERR(em)) {
3533 ret = PTR_ERR(em);
3534 goto out;
3535 }
3536 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003537 flags |= FIEMAP_EXTENT_LAST;
3538 end = 1;
3539 }
Chris Masonec29ed52011-02-23 16:23:20 -05003540 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3541 em_len, flags);
3542 if (ret)
3543 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003544 }
3545out_free:
3546 free_extent_map(em);
3547out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3549 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003550 return ret;
3551}
3552
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003553inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003554 unsigned long i)
3555{
Chris Mason727011e2010-08-06 13:21:20 -04003556 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003557}
3558
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003559inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003560{
Chris Mason6af118ce2008-07-22 11:18:07 -04003561 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3562 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003563}
3564
Chris Mason727011e2010-08-06 13:21:20 -04003565static void __free_extent_buffer(struct extent_buffer *eb)
3566{
3567#if LEAK_DEBUG
3568 unsigned long flags;
3569 spin_lock_irqsave(&leak_lock, flags);
3570 list_del(&eb->leak_list);
3571 spin_unlock_irqrestore(&leak_lock, flags);
3572#endif
3573 if (eb->pages && eb->pages != eb->inline_pages)
3574 kfree(eb->pages);
3575 kmem_cache_free(extent_buffer_cache, eb);
3576}
3577
Chris Masond1310b22008-01-24 16:13:08 -05003578static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3579 u64 start,
3580 unsigned long len,
3581 gfp_t mask)
3582{
3583 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003584#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003585 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003586#endif
Chris Masond1310b22008-01-24 16:13:08 -05003587
Chris Masond1310b22008-01-24 16:13:08 -05003588 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003589 if (eb == NULL)
3590 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003591 eb->start = start;
3592 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003593 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003594 rwlock_init(&eb->lock);
3595 atomic_set(&eb->write_locks, 0);
3596 atomic_set(&eb->read_locks, 0);
3597 atomic_set(&eb->blocking_readers, 0);
3598 atomic_set(&eb->blocking_writers, 0);
3599 atomic_set(&eb->spinning_readers, 0);
3600 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003601 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003602 init_waitqueue_head(&eb->write_lock_wq);
3603 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003604
Chris Mason39351272009-02-04 09:24:05 -05003605#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003606 spin_lock_irqsave(&leak_lock, flags);
3607 list_add(&eb->leak_list, &buffers);
3608 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003609#endif
Chris Masond1310b22008-01-24 16:13:08 -05003610 atomic_set(&eb->refs, 1);
Chris Mason727011e2010-08-06 13:21:20 -04003611 atomic_set(&eb->pages_reading, 0);
3612
3613 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3614 struct page **pages;
3615 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3616 PAGE_CACHE_SHIFT;
3617 pages = kzalloc(num_pages, mask);
3618 if (!pages) {
3619 __free_extent_buffer(eb);
3620 return NULL;
3621 }
3622 eb->pages = pages;
3623 } else {
3624 eb->pages = eb->inline_pages;
3625 }
Chris Masond1310b22008-01-24 16:13:08 -05003626
3627 return eb;
3628}
3629
Miao Xie897ca6e2010-10-26 20:57:29 -04003630/*
3631 * Helper for releasing extent buffer page.
3632 */
3633static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3634 unsigned long start_idx)
3635{
3636 unsigned long index;
3637 struct page *page;
3638
Miao Xie897ca6e2010-10-26 20:57:29 -04003639 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);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003646 if (page) {
3647 spin_lock(&page->mapping->private_lock);
3648 /*
3649 * We do this since we'll remove the pages after we've
3650 * removed the eb from the radix tree, so we could race
3651 * and have this page now attached to the new eb. So
3652 * only clear page_private if it's still connected to
3653 * this eb.
3654 */
3655 if (PagePrivate(page) &&
3656 page->private == (unsigned long)eb) {
3657 /*
3658 * We need to make sure we haven't be attached
3659 * to a new eb.
3660 */
3661 ClearPagePrivate(page);
3662 set_page_private(page, 0);
3663 /* One for the page private */
3664 page_cache_release(page);
3665 }
3666 spin_unlock(&page->mapping->private_lock);
3667
3668 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04003669 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003670 }
Miao Xie897ca6e2010-10-26 20:57:29 -04003671 } while (index != start_idx);
3672}
3673
3674/*
3675 * Helper for releasing the extent buffer.
3676 */
3677static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3678{
3679 btrfs_release_extent_buffer_page(eb, 0);
3680 __free_extent_buffer(eb);
3681}
3682
Chris Masond1310b22008-01-24 16:13:08 -05003683struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04003684 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003685{
3686 unsigned long num_pages = num_extent_pages(start, len);
3687 unsigned long i;
3688 unsigned long index = start >> PAGE_CACHE_SHIFT;
3689 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003690 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003691 struct page *p;
3692 struct address_space *mapping = tree->mapping;
3693 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04003694 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003695
Miao Xie19fe0a82010-10-26 20:57:29 -04003696 rcu_read_lock();
3697 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3698 if (eb && atomic_inc_not_zero(&eb->refs)) {
3699 rcu_read_unlock();
Chris Mason727011e2010-08-06 13:21:20 -04003700 mark_page_accessed(eb->pages[0]);
Chris Mason6af118ce2008-07-22 11:18:07 -04003701 return eb;
3702 }
Miao Xie19fe0a82010-10-26 20:57:29 -04003703 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04003704
David Sterbaba144192011-04-21 01:12:06 +02003705 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04003706 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003707 return NULL;
3708
Chris Mason727011e2010-08-06 13:21:20 -04003709 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04003710 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003711 if (!p) {
3712 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003713 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003714 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003715
3716 spin_lock(&mapping->private_lock);
3717 if (PagePrivate(p)) {
3718 /*
3719 * We could have already allocated an eb for this page
3720 * and attached one so lets see if we can get a ref on
3721 * the existing eb, and if we can we know it's good and
3722 * we can just return that one, else we know we can just
3723 * overwrite page->private.
3724 */
3725 exists = (struct extent_buffer *)p->private;
3726 if (atomic_inc_not_zero(&exists->refs)) {
3727 spin_unlock(&mapping->private_lock);
3728 unlock_page(p);
3729 goto free_eb;
3730 }
3731
3732 /*
3733 * Do this so attach doesn't complain and we need to
3734 * drop the ref the old guy had.
3735 */
3736 ClearPagePrivate(p);
3737 page_cache_release(p);
3738 }
3739 attach_extent_buffer_page(eb, p);
3740 spin_unlock(&mapping->private_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003741 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04003742 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05003743 if (!PageUptodate(p))
3744 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05003745
3746 /*
3747 * see below about how we avoid a nasty race with release page
3748 * and why we unlock later
3749 */
Chris Masond1310b22008-01-24 16:13:08 -05003750 }
3751 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003752 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003753
Miao Xie19fe0a82010-10-26 20:57:29 -04003754 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
3755 if (ret)
3756 goto free_eb;
3757
Chris Mason6af118ce2008-07-22 11:18:07 -04003758 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003759 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
3760 if (ret == -EEXIST) {
3761 exists = radix_tree_lookup(&tree->buffer,
3762 start >> PAGE_CACHE_SHIFT);
Chris Mason6af118ce2008-07-22 11:18:07 -04003763 atomic_inc(&exists->refs);
3764 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003765 radix_tree_preload_end();
Chris Mason6af118ce2008-07-22 11:18:07 -04003766 goto free_eb;
3767 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003768 /* add one reference for the tree */
3769 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003770 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04003771 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05003772
3773 /*
3774 * there is a race where release page may have
3775 * tried to find this extent buffer in the radix
3776 * but failed. It will tell the VM it is safe to
3777 * reclaim the, and it will clear the page private bit.
3778 * We must make sure to set the page private bit properly
3779 * after the extent buffer is in the radix tree so
3780 * it doesn't get lost
3781 */
Chris Mason727011e2010-08-06 13:21:20 -04003782 SetPageChecked(eb->pages[0]);
3783 for (i = 1; i < num_pages; i++) {
3784 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04003785 ClearPageChecked(p);
3786 unlock_page(p);
3787 }
3788 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05003789 return eb;
3790
Chris Mason6af118ce2008-07-22 11:18:07 -04003791free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04003792 for (i = 0; i < num_pages; i++) {
3793 if (eb->pages[i])
3794 unlock_page(eb->pages[i]);
3795 }
Chris Masoneb14ab82011-02-10 12:35:00 -05003796
Chris Masond1310b22008-01-24 16:13:08 -05003797 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003798 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04003799 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003800 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003801}
Chris Masond1310b22008-01-24 16:13:08 -05003802
3803struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02003804 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05003805{
Chris Masond1310b22008-01-24 16:13:08 -05003806 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003807
Miao Xie19fe0a82010-10-26 20:57:29 -04003808 rcu_read_lock();
3809 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
3810 if (eb && atomic_inc_not_zero(&eb->refs)) {
3811 rcu_read_unlock();
Chris Mason727011e2010-08-06 13:21:20 -04003812 mark_page_accessed(eb->pages[0]);
Miao Xie19fe0a82010-10-26 20:57:29 -04003813 return eb;
3814 }
3815 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04003816
Miao Xie19fe0a82010-10-26 20:57:29 -04003817 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003818}
Chris Masond1310b22008-01-24 16:13:08 -05003819
3820void free_extent_buffer(struct extent_buffer *eb)
3821{
Chris Masond1310b22008-01-24 16:13:08 -05003822 if (!eb)
3823 return;
3824
3825 if (!atomic_dec_and_test(&eb->refs))
3826 return;
3827
Chris Mason6af118ce2008-07-22 11:18:07 -04003828 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003829}
Chris Masond1310b22008-01-24 16:13:08 -05003830
3831int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3832 struct extent_buffer *eb)
3833{
Chris Masond1310b22008-01-24 16:13:08 -05003834 unsigned long i;
3835 unsigned long num_pages;
3836 struct page *page;
3837
Chris Masond1310b22008-01-24 16:13:08 -05003838 num_pages = num_extent_pages(eb->start, eb->len);
3839
3840 for (i = 0; i < num_pages; i++) {
3841 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003842 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003843 continue;
3844
Chris Masona61e6f22008-07-22 11:18:08 -04003845 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05003846 WARN_ON(!PagePrivate(page));
3847
Chris Masond1310b22008-01-24 16:13:08 -05003848 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003849 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003850 if (!PageDirty(page)) {
3851 radix_tree_tag_clear(&page->mapping->page_tree,
3852 page_index(page),
3853 PAGECACHE_TAG_DIRTY);
3854 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003855 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04003856 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04003857 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003858 }
3859 return 0;
3860}
Chris Masond1310b22008-01-24 16:13:08 -05003861
Chris Masond1310b22008-01-24 16:13:08 -05003862int set_extent_buffer_dirty(struct extent_io_tree *tree,
3863 struct extent_buffer *eb)
3864{
3865 unsigned long i;
3866 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003867 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003868
Chris Masonb9473432009-03-13 11:00:37 -04003869 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003870 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003871 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003872 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003873 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003874}
Chris Masond1310b22008-01-24 16:13:08 -05003875
Chris Mason19b6caf2011-07-25 06:50:50 -04003876static int __eb_straddles_pages(u64 start, u64 len)
3877{
3878 if (len < PAGE_CACHE_SIZE)
3879 return 1;
3880 if (start & (PAGE_CACHE_SIZE - 1))
3881 return 1;
3882 if ((start + len) & (PAGE_CACHE_SIZE - 1))
3883 return 1;
3884 return 0;
3885}
3886
3887static int eb_straddles_pages(struct extent_buffer *eb)
3888{
3889 return __eb_straddles_pages(eb->start, eb->len);
3890}
3891
Chris Mason1259ab72008-05-12 13:39:03 -04003892int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003893 struct extent_buffer *eb,
3894 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003895{
3896 unsigned long i;
3897 struct page *page;
3898 unsigned long num_pages;
3899
3900 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003901 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003902
Chris Mason50653192012-02-22 12:36:24 -05003903 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3904 cached_state, GFP_NOFS);
3905
Chris Mason1259ab72008-05-12 13:39:03 -04003906 for (i = 0; i < num_pages; i++) {
3907 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003908 if (page)
3909 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003910 }
3911 return 0;
3912}
3913
Chris Masond1310b22008-01-24 16:13:08 -05003914int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3915 struct extent_buffer *eb)
3916{
3917 unsigned long i;
3918 struct page *page;
3919 unsigned long num_pages;
3920
3921 num_pages = num_extent_pages(eb->start, eb->len);
3922
Chris Mason19b6caf2011-07-25 06:50:50 -04003923 if (eb_straddles_pages(eb)) {
3924 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3925 NULL, GFP_NOFS);
3926 }
Chris Masond1310b22008-01-24 16:13:08 -05003927 for (i = 0; i < num_pages; i++) {
3928 page = extent_buffer_page(eb, i);
3929 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3930 ((i == num_pages - 1) &&
3931 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3932 check_page_uptodate(tree, page);
3933 continue;
3934 }
3935 SetPageUptodate(page);
3936 }
3937 return 0;
3938}
Chris Masond1310b22008-01-24 16:13:08 -05003939
Chris Masonce9adaa2008-04-09 16:28:12 -04003940int extent_range_uptodate(struct extent_io_tree *tree,
3941 u64 start, u64 end)
3942{
3943 struct page *page;
3944 int ret;
3945 int pg_uptodate = 1;
3946 int uptodate;
3947 unsigned long index;
3948
Chris Mason19b6caf2011-07-25 06:50:50 -04003949 if (__eb_straddles_pages(start, end - start + 1)) {
3950 ret = test_range_bit(tree, start, end,
3951 EXTENT_UPTODATE, 1, NULL);
3952 if (ret)
3953 return 1;
3954 }
Chris Masond3977122009-01-05 21:25:51 -05003955 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003956 index = start >> PAGE_CACHE_SHIFT;
3957 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05003958 if (!page)
3959 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04003960 uptodate = PageUptodate(page);
3961 page_cache_release(page);
3962 if (!uptodate) {
3963 pg_uptodate = 0;
3964 break;
3965 }
3966 start += PAGE_CACHE_SIZE;
3967 }
3968 return pg_uptodate;
3969}
3970
Chris Masond1310b22008-01-24 16:13:08 -05003971int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003972 struct extent_buffer *eb,
3973 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003974{
Chris Mason728131d2008-04-09 16:28:12 -04003975 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003976 unsigned long num_pages;
3977 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003978 struct page *page;
3979 int pg_uptodate = 1;
3980
Chris Masonb4ce94d2009-02-04 09:25:08 -05003981 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003982 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003983
Chris Mason19b6caf2011-07-25 06:50:50 -04003984 if (eb_straddles_pages(eb)) {
3985 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3986 EXTENT_UPTODATE, 1, cached_state);
3987 if (ret)
3988 return ret;
3989 }
Chris Mason728131d2008-04-09 16:28:12 -04003990
3991 num_pages = num_extent_pages(eb->start, eb->len);
3992 for (i = 0; i < num_pages; i++) {
3993 page = extent_buffer_page(eb, i);
3994 if (!PageUptodate(page)) {
3995 pg_uptodate = 0;
3996 break;
3997 }
3998 }
Chris Mason42352982008-04-28 16:40:52 -04003999 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05004000}
Chris Masond1310b22008-01-24 16:13:08 -05004001
4002int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004003 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004004 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004005{
4006 unsigned long i;
4007 unsigned long start_i;
4008 struct page *page;
4009 int err;
4010 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004011 int locked_pages = 0;
4012 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004013 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004014 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004015 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004016 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004017
Chris Masonb4ce94d2009-02-04 09:25:08 -05004018 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004019 return 0;
4020
Chris Mason19b6caf2011-07-25 06:50:50 -04004021 if (eb_straddles_pages(eb)) {
4022 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4023 EXTENT_UPTODATE, 1, NULL)) {
4024 return 0;
4025 }
Chris Masond1310b22008-01-24 16:13:08 -05004026 }
4027
4028 if (start) {
4029 WARN_ON(start < eb->start);
4030 start_i = (start >> PAGE_CACHE_SHIFT) -
4031 (eb->start >> PAGE_CACHE_SHIFT);
4032 } else {
4033 start_i = 0;
4034 }
4035
4036 num_pages = num_extent_pages(eb->start, eb->len);
4037 for (i = start_i; i < num_pages; i++) {
4038 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004039 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004040 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004041 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004042 } else {
4043 lock_page(page);
4044 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004045 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004046 if (!PageUptodate(page)) {
4047 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004048 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004049 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004050 }
4051 if (all_uptodate) {
4052 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004053 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004054 goto unlock_exit;
4055 }
4056
Chris Mason727011e2010-08-06 13:21:20 -04004057 atomic_set(&eb->pages_reading, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004058 for (i = start_i; i < num_pages; i++) {
4059 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004060 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004061 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004062 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004063 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004064 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004065 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004066 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004067 } else {
4068 unlock_page(page);
4069 }
4070 }
4071
Chris Masona86c12c2008-02-07 10:50:54 -05004072 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04004073 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05004074
Arne Jansenbb82ab82011-06-10 14:06:53 +02004075 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004076 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004077
Chris Masond1310b22008-01-24 16:13:08 -05004078 for (i = start_i; i < num_pages; i++) {
4079 page = extent_buffer_page(eb, i);
4080 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004081 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004082 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004083 }
Chris Masond3977122009-01-05 21:25:51 -05004084
Chris Masond1310b22008-01-24 16:13:08 -05004085 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004086 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004087 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004088
4089unlock_exit:
4090 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004091 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004092 page = extent_buffer_page(eb, i);
4093 i++;
4094 unlock_page(page);
4095 locked_pages--;
4096 }
4097 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004098}
Chris Masond1310b22008-01-24 16:13:08 -05004099
4100void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4101 unsigned long start,
4102 unsigned long len)
4103{
4104 size_t cur;
4105 size_t offset;
4106 struct page *page;
4107 char *kaddr;
4108 char *dst = (char *)dstv;
4109 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4110 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004111
4112 WARN_ON(start > eb->len);
4113 WARN_ON(start + len > eb->start + eb->len);
4114
4115 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4116
Chris Masond3977122009-01-05 21:25:51 -05004117 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004118 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004119
4120 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004121 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004122 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004123
4124 dst += cur;
4125 len -= cur;
4126 offset = 0;
4127 i++;
4128 }
4129}
Chris Masond1310b22008-01-24 16:13:08 -05004130
4131int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004132 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004133 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004134 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004135{
4136 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4137 char *kaddr;
4138 struct page *p;
4139 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4140 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4141 unsigned long end_i = (start_offset + start + min_len - 1) >>
4142 PAGE_CACHE_SHIFT;
4143
4144 if (i != end_i)
4145 return -EINVAL;
4146
4147 if (i == 0) {
4148 offset = start_offset;
4149 *map_start = 0;
4150 } else {
4151 offset = 0;
4152 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4153 }
Chris Masond3977122009-01-05 21:25:51 -05004154
Chris Masond1310b22008-01-24 16:13:08 -05004155 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004156 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4157 "wanted %lu %lu\n", (unsigned long long)eb->start,
4158 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004159 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004160 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004161 }
4162
4163 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004164 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004165 *map = kaddr + offset;
4166 *map_len = PAGE_CACHE_SIZE - offset;
4167 return 0;
4168}
Chris Masond1310b22008-01-24 16:13:08 -05004169
Chris Masond1310b22008-01-24 16:13:08 -05004170int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4171 unsigned long start,
4172 unsigned long len)
4173{
4174 size_t cur;
4175 size_t offset;
4176 struct page *page;
4177 char *kaddr;
4178 char *ptr = (char *)ptrv;
4179 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4180 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4181 int ret = 0;
4182
4183 WARN_ON(start > eb->len);
4184 WARN_ON(start + len > eb->start + eb->len);
4185
4186 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4187
Chris Masond3977122009-01-05 21:25:51 -05004188 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004189 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004190
4191 cur = min(len, (PAGE_CACHE_SIZE - offset));
4192
Chris Masona6591712011-07-19 12:04:14 -04004193 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004194 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004195 if (ret)
4196 break;
4197
4198 ptr += cur;
4199 len -= cur;
4200 offset = 0;
4201 i++;
4202 }
4203 return ret;
4204}
Chris Masond1310b22008-01-24 16:13:08 -05004205
4206void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4207 unsigned long start, unsigned long len)
4208{
4209 size_t cur;
4210 size_t offset;
4211 struct page *page;
4212 char *kaddr;
4213 char *src = (char *)srcv;
4214 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4215 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4216
4217 WARN_ON(start > eb->len);
4218 WARN_ON(start + len > eb->start + eb->len);
4219
4220 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4221
Chris Masond3977122009-01-05 21:25:51 -05004222 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004223 page = extent_buffer_page(eb, i);
4224 WARN_ON(!PageUptodate(page));
4225
4226 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004227 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004228 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004229
4230 src += cur;
4231 len -= cur;
4232 offset = 0;
4233 i++;
4234 }
4235}
Chris Masond1310b22008-01-24 16:13:08 -05004236
4237void memset_extent_buffer(struct extent_buffer *eb, char c,
4238 unsigned long start, unsigned long len)
4239{
4240 size_t cur;
4241 size_t offset;
4242 struct page *page;
4243 char *kaddr;
4244 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4245 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4246
4247 WARN_ON(start > eb->len);
4248 WARN_ON(start + len > eb->start + eb->len);
4249
4250 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4251
Chris Masond3977122009-01-05 21:25:51 -05004252 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004253 page = extent_buffer_page(eb, i);
4254 WARN_ON(!PageUptodate(page));
4255
4256 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004257 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004258 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004259
4260 len -= cur;
4261 offset = 0;
4262 i++;
4263 }
4264}
Chris Masond1310b22008-01-24 16:13:08 -05004265
4266void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4267 unsigned long dst_offset, unsigned long src_offset,
4268 unsigned long len)
4269{
4270 u64 dst_len = dst->len;
4271 size_t cur;
4272 size_t offset;
4273 struct page *page;
4274 char *kaddr;
4275 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4276 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4277
4278 WARN_ON(src->len != dst_len);
4279
4280 offset = (start_offset + dst_offset) &
4281 ((unsigned long)PAGE_CACHE_SIZE - 1);
4282
Chris Masond3977122009-01-05 21:25:51 -05004283 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004284 page = extent_buffer_page(dst, i);
4285 WARN_ON(!PageUptodate(page));
4286
4287 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4288
Chris Masona6591712011-07-19 12:04:14 -04004289 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004290 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004291
4292 src_offset += cur;
4293 len -= cur;
4294 offset = 0;
4295 i++;
4296 }
4297}
Chris Masond1310b22008-01-24 16:13:08 -05004298
4299static void move_pages(struct page *dst_page, struct page *src_page,
4300 unsigned long dst_off, unsigned long src_off,
4301 unsigned long len)
4302{
Chris Masona6591712011-07-19 12:04:14 -04004303 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004304 if (dst_page == src_page) {
4305 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4306 } else {
Chris Masona6591712011-07-19 12:04:14 -04004307 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004308 char *p = dst_kaddr + dst_off + len;
4309 char *s = src_kaddr + src_off + len;
4310
4311 while (len--)
4312 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004313 }
Chris Masond1310b22008-01-24 16:13:08 -05004314}
4315
Sergei Trofimovich33872062011-04-11 21:52:52 +00004316static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4317{
4318 unsigned long distance = (src > dst) ? src - dst : dst - src;
4319 return distance < len;
4320}
4321
Chris Masond1310b22008-01-24 16:13:08 -05004322static void copy_pages(struct page *dst_page, struct page *src_page,
4323 unsigned long dst_off, unsigned long src_off,
4324 unsigned long len)
4325{
Chris Masona6591712011-07-19 12:04:14 -04004326 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004327 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004328 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004329
Sergei Trofimovich33872062011-04-11 21:52:52 +00004330 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004331 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004332 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004333 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004334 if (areas_overlap(src_off, dst_off, len))
4335 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004336 }
Chris Masond1310b22008-01-24 16:13:08 -05004337
Chris Mason727011e2010-08-06 13:21:20 -04004338 if (must_memmove)
4339 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4340 else
4341 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004342}
4343
4344void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4345 unsigned long src_offset, unsigned long len)
4346{
4347 size_t cur;
4348 size_t dst_off_in_page;
4349 size_t src_off_in_page;
4350 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4351 unsigned long dst_i;
4352 unsigned long src_i;
4353
4354 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004355 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4356 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004357 BUG_ON(1);
4358 }
4359 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004360 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4361 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004362 BUG_ON(1);
4363 }
4364
Chris Masond3977122009-01-05 21:25:51 -05004365 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004366 dst_off_in_page = (start_offset + dst_offset) &
4367 ((unsigned long)PAGE_CACHE_SIZE - 1);
4368 src_off_in_page = (start_offset + src_offset) &
4369 ((unsigned long)PAGE_CACHE_SIZE - 1);
4370
4371 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4372 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4373
4374 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4375 src_off_in_page));
4376 cur = min_t(unsigned long, cur,
4377 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4378
4379 copy_pages(extent_buffer_page(dst, dst_i),
4380 extent_buffer_page(dst, src_i),
4381 dst_off_in_page, src_off_in_page, cur);
4382
4383 src_offset += cur;
4384 dst_offset += cur;
4385 len -= cur;
4386 }
4387}
Chris Masond1310b22008-01-24 16:13:08 -05004388
4389void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4390 unsigned long src_offset, unsigned long len)
4391{
4392 size_t cur;
4393 size_t dst_off_in_page;
4394 size_t src_off_in_page;
4395 unsigned long dst_end = dst_offset + len - 1;
4396 unsigned long src_end = src_offset + len - 1;
4397 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4398 unsigned long dst_i;
4399 unsigned long src_i;
4400
4401 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004402 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4403 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004404 BUG_ON(1);
4405 }
4406 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004407 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4408 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004409 BUG_ON(1);
4410 }
Chris Mason727011e2010-08-06 13:21:20 -04004411 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004412 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4413 return;
4414 }
Chris Masond3977122009-01-05 21:25:51 -05004415 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004416 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4417 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4418
4419 dst_off_in_page = (start_offset + dst_end) &
4420 ((unsigned long)PAGE_CACHE_SIZE - 1);
4421 src_off_in_page = (start_offset + src_end) &
4422 ((unsigned long)PAGE_CACHE_SIZE - 1);
4423
4424 cur = min_t(unsigned long, len, src_off_in_page + 1);
4425 cur = min(cur, dst_off_in_page + 1);
4426 move_pages(extent_buffer_page(dst, dst_i),
4427 extent_buffer_page(dst, src_i),
4428 dst_off_in_page - cur + 1,
4429 src_off_in_page - cur + 1, cur);
4430
4431 dst_end -= cur;
4432 src_end -= cur;
4433 len -= cur;
4434 }
4435}
Chris Mason6af118ce2008-07-22 11:18:07 -04004436
Miao Xie19fe0a82010-10-26 20:57:29 -04004437static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4438{
4439 struct extent_buffer *eb =
4440 container_of(head, struct extent_buffer, rcu_head);
4441
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004442 __free_extent_buffer(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004443}
4444
Chris Mason6af118ce2008-07-22 11:18:07 -04004445int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
4446{
4447 u64 start = page_offset(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004448 struct extent_buffer *eb = (struct extent_buffer *)page->private;
Chris Mason6af118ce2008-07-22 11:18:07 -04004449 int ret = 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004450
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004451 if (!PagePrivate(page) || !eb)
4452 return 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004453
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004454 spin_lock(&tree->buffer_lock);
Chris Mason727011e2010-08-06 13:21:20 -04004455 if (atomic_read(&eb->refs) > 1 ||
4456 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
Chris Masonb9473432009-03-13 11:00:37 -04004457 ret = 0;
4458 goto out;
4459 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004460
Miao Xie19fe0a82010-10-26 20:57:29 -04004461 /*
4462 * set @eb->refs to 0 if it is already 1, and then release the @eb.
4463 * Or go back.
4464 */
4465 if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
4466 ret = 0;
4467 goto out;
4468 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004469 radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004470 btrfs_release_extent_buffer_page(eb, 0);
Chris Mason6af118ce2008-07-22 11:18:07 -04004471out:
4472 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004473
4474 /* at this point we can safely release the extent buffer */
4475 if (atomic_read(&eb->refs) == 0)
4476 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Chris Mason6af118ce2008-07-22 11:18:07 -04004477 return ret;
4478}