blob: 73f2bfe3ac9302091608beae85b4aecf28622240 [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>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/spinlock.h>
8#include <linux/blkdev.h>
9#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050010#include <linux/writeback.h>
11#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070012#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040016#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020019#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010020#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040021#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040022#include "rcu-string.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
27static LIST_HEAD(buffers);
28static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040029
Chris Masonb47eda82008-11-10 12:34:40 -050030#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050031#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050032static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040033#endif
Chris Masond1310b22008-01-24 16:13:08 -050034
Chris Masond1310b22008-01-24 16:13:08 -050035#define BUFFER_LRU_MAX 64
36
37struct tree_entry {
38 u64 start;
39 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050040 struct rb_node rb_node;
41};
42
43struct extent_page_data {
44 struct bio *bio;
45 struct extent_io_tree *tree;
46 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -040047 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -050048
49 /* tells writepage not to lock the state bits for this range
50 * it still does the unlocking
51 */
Chris Masonffbd5172009-04-20 15:50:09 -040052 unsigned int extent_locked:1;
53
54 /* tells the submit_bio code to use a WRITE_SYNC */
55 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050056};
57
Josef Bacik0b32f4b2012-03-13 09:38:00 -040058static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040059static inline struct btrfs_fs_info *
60tree_fs_info(struct extent_io_tree *tree)
61{
62 return btrfs_sb(tree->mapping->host->i_sb);
63}
Josef Bacik0b32f4b2012-03-13 09:38:00 -040064
Chris Masond1310b22008-01-24 16:13:08 -050065int __init extent_io_init(void)
66{
David Sterba837e1972012-09-07 03:00:48 -060067 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020068 sizeof(struct extent_state), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050070 if (!extent_state_cache)
71 return -ENOMEM;
72
David Sterba837e1972012-09-07 03:00:48 -060073 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020074 sizeof(struct extent_buffer), 0,
75 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050076 if (!extent_buffer_cache)
77 goto free_state_cache;
78 return 0;
79
80free_state_cache:
81 kmem_cache_destroy(extent_state_cache);
82 return -ENOMEM;
83}
84
85void extent_io_exit(void)
86{
87 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040088 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050089
90 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040091 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050092 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
93 "state %lu in tree %p refs %d\n",
94 (unsigned long long)state->start,
95 (unsigned long long)state->end,
96 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040097 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050098 kmem_cache_free(extent_state_cache, state);
99
100 }
101
Chris Mason2d2ae542008-03-26 16:24:23 -0400102 while (!list_empty(&buffers)) {
103 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500104 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
105 "refs %d\n", (unsigned long long)eb->start,
106 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400107 list_del(&eb->leak_list);
108 kmem_cache_free(extent_buffer_cache, eb);
109 }
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000110
111 /*
112 * Make sure all delayed rcu free are flushed before we
113 * destroy caches.
114 */
115 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500116 if (extent_state_cache)
117 kmem_cache_destroy(extent_state_cache);
118 if (extent_buffer_cache)
119 kmem_cache_destroy(extent_buffer_cache);
120}
121
122void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200123 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500124{
Eric Paris6bef4d32010-02-23 19:43:04 +0000125 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400126 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500127 tree->ops = NULL;
128 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500129 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400130 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500131 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500132}
Chris Masond1310b22008-01-24 16:13:08 -0500133
Christoph Hellwigb2950862008-12-02 09:54:17 -0500134static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500135{
136 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500137#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400138 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400139#endif
Chris Masond1310b22008-01-24 16:13:08 -0500140
141 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400142 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500143 return state;
144 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500145 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500146 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_add(&state->leak_list, &states);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 atomic_set(&state->refs, 1);
153 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100154 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500155 return state;
156}
Chris Masond1310b22008-01-24 16:13:08 -0500157
Chris Mason4845e442010-05-25 20:56:50 -0400158void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500159{
Chris Masond1310b22008-01-24 16:13:08 -0500160 if (!state)
161 return;
162 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500163#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400164 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400165#endif
Chris Mason70dec802008-01-29 09:59:12 -0500166 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500167#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400168 spin_lock_irqsave(&leak_lock, flags);
169 list_del(&state->leak_list);
170 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400171#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100172 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500173 kmem_cache_free(extent_state_cache, state);
174 }
175}
Chris Masond1310b22008-01-24 16:13:08 -0500176
177static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
178 struct rb_node *node)
179{
Chris Masond3977122009-01-05 21:25:51 -0500180 struct rb_node **p = &root->rb_node;
181 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct tree_entry *entry;
183
Chris Masond3977122009-01-05 21:25:51 -0500184 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500185 parent = *p;
186 entry = rb_entry(parent, struct tree_entry, rb_node);
187
188 if (offset < entry->start)
189 p = &(*p)->rb_left;
190 else if (offset > entry->end)
191 p = &(*p)->rb_right;
192 else
193 return parent;
194 }
195
Chris Masond1310b22008-01-24 16:13:08 -0500196 rb_link_node(node, parent, p);
197 rb_insert_color(node, root);
198 return NULL;
199}
200
Chris Mason80ea96b2008-02-01 14:51:59 -0500201static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500202 struct rb_node **prev_ret,
203 struct rb_node **next_ret)
204{
Chris Mason80ea96b2008-02-01 14:51:59 -0500205 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500206 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500207 struct rb_node *prev = NULL;
208 struct rb_node *orig_prev = NULL;
209 struct tree_entry *entry;
210 struct tree_entry *prev_entry = NULL;
211
Chris Masond3977122009-01-05 21:25:51 -0500212 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500213 entry = rb_entry(n, struct tree_entry, rb_node);
214 prev = n;
215 prev_entry = entry;
216
217 if (offset < entry->start)
218 n = n->rb_left;
219 else if (offset > entry->end)
220 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500221 else
Chris Masond1310b22008-01-24 16:13:08 -0500222 return n;
223 }
224
225 if (prev_ret) {
226 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500227 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500228 prev = rb_next(prev);
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
230 }
231 *prev_ret = prev;
232 prev = orig_prev;
233 }
234
235 if (next_ret) {
236 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500237 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500238 prev = rb_prev(prev);
239 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
240 }
241 *next_ret = prev;
242 }
243 return NULL;
244}
245
Chris Mason80ea96b2008-02-01 14:51:59 -0500246static inline struct rb_node *tree_search(struct extent_io_tree *tree,
247 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500248{
Chris Mason70dec802008-01-29 09:59:12 -0500249 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500250 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500251
Chris Mason80ea96b2008-02-01 14:51:59 -0500252 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500253 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500254 return prev;
255 return ret;
256}
257
Josef Bacik9ed74f22009-09-11 16:12:44 -0400258static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
259 struct extent_state *other)
260{
261 if (tree->ops && tree->ops->merge_extent_hook)
262 tree->ops->merge_extent_hook(tree->mapping->host, new,
263 other);
264}
265
Chris Masond1310b22008-01-24 16:13:08 -0500266/*
267 * utility function to look for merge candidates inside a given range.
268 * Any extents with matching state are merged together into a single
269 * extent in the tree. Extents with EXTENT_IO in their state field
270 * are not merged because the end_io handlers need to be able to do
271 * operations on them without sleeping (or doing allocations/splits).
272 *
273 * This should be called with the tree lock held.
274 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000275static void merge_state(struct extent_io_tree *tree,
276 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500277{
278 struct extent_state *other;
279 struct rb_node *other_node;
280
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400281 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000282 return;
Chris Masond1310b22008-01-24 16:13:08 -0500283
284 other_node = rb_prev(&state->rb_node);
285 if (other_node) {
286 other = rb_entry(other_node, struct extent_state, rb_node);
287 if (other->end == state->start - 1 &&
288 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500290 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500291 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500292 rb_erase(&other->rb_node, &tree->state);
293 free_extent_state(other);
294 }
295 }
296 other_node = rb_next(&state->rb_node);
297 if (other_node) {
298 other = rb_entry(other_node, struct extent_state, rb_node);
299 if (other->start == state->end + 1 &&
300 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400301 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400302 state->end = other->end;
303 other->tree = NULL;
304 rb_erase(&other->rb_node, &tree->state);
305 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500306 }
307 }
Chris Masond1310b22008-01-24 16:13:08 -0500308}
309
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000310static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400311 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500312{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000313 if (tree->ops && tree->ops->set_bit_hook)
314 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500315}
316
317static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500319{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400320 if (tree->ops && tree->ops->clear_bit_hook)
321 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500322}
323
Xiao Guangrong3150b692011-07-14 03:19:08 +0000324static void set_state_bits(struct extent_io_tree *tree,
325 struct extent_state *state, int *bits);
326
Chris Masond1310b22008-01-24 16:13:08 -0500327/*
328 * insert an extent_state struct into the tree. 'bits' are set on the
329 * struct before it is inserted.
330 *
331 * This may return -EEXIST if the extent is already there, in which case the
332 * state struct is freed.
333 *
334 * The tree lock is not taken internally. This is a utility function and
335 * probably isn't what you want to call (see set/clear_extent_bit).
336 */
337static int insert_state(struct extent_io_tree *tree,
338 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400339 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500340{
341 struct rb_node *node;
342
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000343 if (end < start)
344 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Chris Masond3977122009-01-05 21:25:51 -0500345 (unsigned long long)end,
346 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500347 state->start = start;
348 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400349
Xiao Guangrong3150b692011-07-14 03:19:08 +0000350 set_state_bits(tree, state, bits);
351
Chris Masond1310b22008-01-24 16:13:08 -0500352 node = tree_insert(&tree->state, end, &state->rb_node);
353 if (node) {
354 struct extent_state *found;
355 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500356 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
357 "%llu %llu\n", (unsigned long long)found->start,
358 (unsigned long long)found->end,
359 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500360 return -EEXIST;
361 }
Chris Mason70dec802008-01-29 09:59:12 -0500362 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500363 merge_state(tree, state);
364 return 0;
365}
366
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000367static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400368 u64 split)
369{
370 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000371 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400372}
373
Chris Masond1310b22008-01-24 16:13:08 -0500374/*
375 * split a given extent state struct in two, inserting the preallocated
376 * struct 'prealloc' as the newly created second half. 'split' indicates an
377 * offset inside 'orig' where it should be split.
378 *
379 * Before calling,
380 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
381 * are two extent state structs in the tree:
382 * prealloc: [orig->start, split - 1]
383 * orig: [ split, orig->end ]
384 *
385 * The tree locks are not taken by this function. They need to be held
386 * by the caller.
387 */
388static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
389 struct extent_state *prealloc, u64 split)
390{
391 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400392
393 split_cb(tree, orig, split);
394
Chris Masond1310b22008-01-24 16:13:08 -0500395 prealloc->start = orig->start;
396 prealloc->end = split - 1;
397 prealloc->state = orig->state;
398 orig->start = split;
399
400 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
401 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500402 free_extent_state(prealloc);
403 return -EEXIST;
404 }
Chris Mason70dec802008-01-29 09:59:12 -0500405 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500406 return 0;
407}
408
Li Zefancdc6a392012-03-12 16:39:48 +0800409static struct extent_state *next_state(struct extent_state *state)
410{
411 struct rb_node *next = rb_next(&state->rb_node);
412 if (next)
413 return rb_entry(next, struct extent_state, rb_node);
414 else
415 return NULL;
416}
417
Chris Masond1310b22008-01-24 16:13:08 -0500418/*
419 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800420 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500421 *
422 * If no bits are set on the state struct after clearing things, the
423 * struct is freed and removed from the tree
424 */
Li Zefancdc6a392012-03-12 16:39:48 +0800425static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
426 struct extent_state *state,
427 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500428{
Li Zefancdc6a392012-03-12 16:39:48 +0800429 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400430 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500431
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400432 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500433 u64 range = state->end - state->start + 1;
434 WARN_ON(range > tree->dirty_bytes);
435 tree->dirty_bytes -= range;
436 }
Chris Mason291d6732008-01-29 15:55:23 -0500437 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400438 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500439 if (wake)
440 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400441 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800442 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500443 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500444 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500445 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500446 free_extent_state(state);
447 } else {
448 WARN_ON(1);
449 }
450 } else {
451 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800452 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500453 }
Li Zefancdc6a392012-03-12 16:39:48 +0800454 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500455}
456
Xiao Guangrong82337672011-04-20 06:44:57 +0000457static struct extent_state *
458alloc_extent_state_atomic(struct extent_state *prealloc)
459{
460 if (!prealloc)
461 prealloc = alloc_extent_state(GFP_ATOMIC);
462
463 return prealloc;
464}
465
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400466void extent_io_tree_panic(struct extent_io_tree *tree, int err)
467{
468 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
469 "Extent tree was modified by another "
470 "thread while locked.");
471}
472
Chris Masond1310b22008-01-24 16:13:08 -0500473/*
474 * clear some bits on a range in the tree. This may require splitting
475 * or inserting elements in the tree, so the gfp mask is used to
476 * indicate which allocations or sleeping are allowed.
477 *
478 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
479 * the given range from the tree regardless of state (ie for truncate).
480 *
481 * the range [start, end] is inclusive.
482 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100483 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500484 */
485int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400486 int bits, int wake, int delete,
487 struct extent_state **cached_state,
488 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500489{
490 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400491 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500492 struct extent_state *prealloc = NULL;
493 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400494 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500495 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000496 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500497
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400498 if (delete)
499 bits |= ~EXTENT_CTLBITS;
500 bits |= EXTENT_FIRST_DELALLOC;
501
Josef Bacik2ac55d42010-02-03 19:33:23 +0000502 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
503 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500504again:
505 if (!prealloc && (mask & __GFP_WAIT)) {
506 prealloc = alloc_extent_state(mask);
507 if (!prealloc)
508 return -ENOMEM;
509 }
510
Chris Masoncad321a2008-12-17 14:51:42 -0500511 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400512 if (cached_state) {
513 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000514
515 if (clear) {
516 *cached_state = NULL;
517 cached_state = NULL;
518 }
519
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400520 if (cached && cached->tree && cached->start <= start &&
521 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000522 if (clear)
523 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400524 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400525 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400526 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000527 if (clear)
528 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400529 }
Chris Masond1310b22008-01-24 16:13:08 -0500530 /*
531 * this search will find the extents that end after
532 * our range starts
533 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500534 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500535 if (!node)
536 goto out;
537 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400538hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500539 if (state->start > end)
540 goto out;
541 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400542 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500543
Liu Bo04493142012-02-16 18:34:37 +0800544 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800545 if (!(state->state & bits)) {
546 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800547 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800548 }
Liu Bo04493142012-02-16 18:34:37 +0800549
Chris Masond1310b22008-01-24 16:13:08 -0500550 /*
551 * | ---- desired range ---- |
552 * | state | or
553 * | ------------- state -------------- |
554 *
555 * We need to split the extent we found, and may flip
556 * bits on second half.
557 *
558 * If the extent we found extends past our range, we
559 * just split and search again. It'll get split again
560 * the next time though.
561 *
562 * If the extent we found is inside our range, we clear
563 * the desired bit on it.
564 */
565
566 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000567 prealloc = alloc_extent_state_atomic(prealloc);
568 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500569 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400570 if (err)
571 extent_io_tree_panic(tree, err);
572
Chris Masond1310b22008-01-24 16:13:08 -0500573 prealloc = NULL;
574 if (err)
575 goto out;
576 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800577 state = clear_state_bit(tree, state, &bits, wake);
578 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500579 }
580 goto search_again;
581 }
582 /*
583 * | ---- desired range ---- |
584 * | state |
585 * We need to split the extent, and clear the bit
586 * on the first half
587 */
588 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000589 prealloc = alloc_extent_state_atomic(prealloc);
590 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500591 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400592 if (err)
593 extent_io_tree_panic(tree, err);
594
Chris Masond1310b22008-01-24 16:13:08 -0500595 if (wake)
596 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400597
Jeff Mahoney6763af82012-03-01 14:56:29 +0100598 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400599
Chris Masond1310b22008-01-24 16:13:08 -0500600 prealloc = NULL;
601 goto out;
602 }
Chris Mason42daec22009-09-23 19:51:09 -0400603
Li Zefancdc6a392012-03-12 16:39:48 +0800604 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800605next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400606 if (last_end == (u64)-1)
607 goto out;
608 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800609 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800610 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500611 goto search_again;
612
613out:
Chris Masoncad321a2008-12-17 14:51:42 -0500614 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500615 if (prealloc)
616 free_extent_state(prealloc);
617
Jeff Mahoney6763af82012-03-01 14:56:29 +0100618 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500619
620search_again:
621 if (start > end)
622 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500623 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500624 if (mask & __GFP_WAIT)
625 cond_resched();
626 goto again;
627}
Chris Masond1310b22008-01-24 16:13:08 -0500628
Jeff Mahoney143bede2012-03-01 14:56:26 +0100629static void wait_on_state(struct extent_io_tree *tree,
630 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500631 __releases(tree->lock)
632 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500633{
634 DEFINE_WAIT(wait);
635 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500636 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500637 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500638 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500639 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500640}
641
642/*
643 * waits for one or more bits to clear on a range in the state tree.
644 * The range [start, end] is inclusive.
645 * The tree lock is taken by this function
646 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100647void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500648{
649 struct extent_state *state;
650 struct rb_node *node;
651
Chris Masoncad321a2008-12-17 14:51:42 -0500652 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500653again:
654 while (1) {
655 /*
656 * this search will find all the extents that end after
657 * our range starts
658 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500659 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500660 if (!node)
661 break;
662
663 state = rb_entry(node, struct extent_state, rb_node);
664
665 if (state->start > end)
666 goto out;
667
668 if (state->state & bits) {
669 start = state->start;
670 atomic_inc(&state->refs);
671 wait_on_state(tree, state);
672 free_extent_state(state);
673 goto again;
674 }
675 start = state->end + 1;
676
677 if (start > end)
678 break;
679
Xiao Guangrongded91f02011-07-14 03:19:27 +0000680 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500681 }
682out:
Chris Masoncad321a2008-12-17 14:51:42 -0500683 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500684}
Chris Masond1310b22008-01-24 16:13:08 -0500685
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000686static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500687 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400688 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500689{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400691
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000692 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400693 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500694 u64 range = state->end - state->start + 1;
695 tree->dirty_bytes += range;
696 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400697 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500698}
699
Chris Mason2c64c532009-09-02 15:04:12 -0400700static void cache_state(struct extent_state *state,
701 struct extent_state **cached_ptr)
702{
703 if (cached_ptr && !(*cached_ptr)) {
704 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
705 *cached_ptr = state;
706 atomic_inc(&state->refs);
707 }
708 }
709}
710
Arne Jansen507903b2011-04-06 10:02:20 +0000711static void uncache_state(struct extent_state **cached_ptr)
712{
713 if (cached_ptr && (*cached_ptr)) {
714 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400715 *cached_ptr = NULL;
716 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000717 }
718}
719
Chris Masond1310b22008-01-24 16:13:08 -0500720/*
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * set some bits on a range in the tree. This may require allocations or
722 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500723 *
Chris Mason1edbb732009-09-02 13:24:36 -0400724 * If any of the exclusive bits are set, this will fail with -EEXIST if some
725 * part of the range already has the desired bits set. The start of the
726 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500727 *
Chris Mason1edbb732009-09-02 13:24:36 -0400728 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500729 */
Chris Mason1edbb732009-09-02 13:24:36 -0400730
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100731static int __must_check
732__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
733 int bits, int exclusive_bits, u64 *failed_start,
734 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500735{
736 struct extent_state *state;
737 struct extent_state *prealloc = NULL;
738 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500739 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500740 u64 last_start;
741 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400742
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400743 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500744again:
745 if (!prealloc && (mask & __GFP_WAIT)) {
746 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000747 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500748 }
749
Chris Masoncad321a2008-12-17 14:51:42 -0500750 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400751 if (cached_state && *cached_state) {
752 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400753 if (state->start <= start && state->end > start &&
754 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400755 node = &state->rb_node;
756 goto hit_next;
757 }
758 }
Chris Masond1310b22008-01-24 16:13:08 -0500759 /*
760 * this search will find all the extents that end after
761 * our range starts.
762 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500763 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500764 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000765 prealloc = alloc_extent_state_atomic(prealloc);
766 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400767 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400768 if (err)
769 extent_io_tree_panic(tree, err);
770
Chris Masond1310b22008-01-24 16:13:08 -0500771 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500772 goto out;
773 }
Chris Masond1310b22008-01-24 16:13:08 -0500774 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400775hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500776 last_start = state->start;
777 last_end = state->end;
778
779 /*
780 * | ---- desired range ---- |
781 * | state |
782 *
783 * Just lock what we found and keep going
784 */
785 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400786 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500787 *failed_start = state->start;
788 err = -EEXIST;
789 goto out;
790 }
Chris Mason42daec22009-09-23 19:51:09 -0400791
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000792 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400793 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500794 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400795 if (last_end == (u64)-1)
796 goto out;
797 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800798 state = next_state(state);
799 if (start < end && state && state->start == start &&
800 !need_resched())
801 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500802 goto search_again;
803 }
804
805 /*
806 * | ---- desired range ---- |
807 * | state |
808 * or
809 * | ------------- state -------------- |
810 *
811 * We need to split the extent we found, and may flip bits on
812 * second half.
813 *
814 * If the extent we found extends past our
815 * range, we just split and search again. It'll get split
816 * again the next time though.
817 *
818 * If the extent we found is inside our range, we set the
819 * desired bit on it.
820 */
821 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400822 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500823 *failed_start = start;
824 err = -EEXIST;
825 goto out;
826 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000827
828 prealloc = alloc_extent_state_atomic(prealloc);
829 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500830 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400831 if (err)
832 extent_io_tree_panic(tree, err);
833
Chris Masond1310b22008-01-24 16:13:08 -0500834 prealloc = NULL;
835 if (err)
836 goto out;
837 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000838 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400839 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500840 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400841 if (last_end == (u64)-1)
842 goto out;
843 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800844 state = next_state(state);
845 if (start < end && state && state->start == start &&
846 !need_resched())
847 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500848 }
849 goto search_again;
850 }
851 /*
852 * | ---- desired range ---- |
853 * | state | or | state |
854 *
855 * There's a hole, we need to insert something in it and
856 * ignore the extent we found.
857 */
858 if (state->start > start) {
859 u64 this_end;
860 if (end < last_start)
861 this_end = end;
862 else
Chris Masond3977122009-01-05 21:25:51 -0500863 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000864
865 prealloc = alloc_extent_state_atomic(prealloc);
866 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000867
868 /*
869 * Avoid to free 'prealloc' if it can be merged with
870 * the later extent.
871 */
Chris Masond1310b22008-01-24 16:13:08 -0500872 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400873 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400874 if (err)
875 extent_io_tree_panic(tree, err);
876
Chris Mason2c64c532009-09-02 15:04:12 -0400877 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500878 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500879 start = this_end + 1;
880 goto search_again;
881 }
882 /*
883 * | ---- desired range ---- |
884 * | state |
885 * We need to split the extent, and set the bit
886 * on the first half
887 */
888 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400889 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500890 *failed_start = start;
891 err = -EEXIST;
892 goto out;
893 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000894
895 prealloc = alloc_extent_state_atomic(prealloc);
896 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500897 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400898 if (err)
899 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500900
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000901 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400902 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500903 merge_state(tree, prealloc);
904 prealloc = NULL;
905 goto out;
906 }
907
908 goto search_again;
909
910out:
Chris Masoncad321a2008-12-17 14:51:42 -0500911 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500912 if (prealloc)
913 free_extent_state(prealloc);
914
915 return err;
916
917search_again:
918 if (start > end)
919 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500920 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500921 if (mask & __GFP_WAIT)
922 cond_resched();
923 goto again;
924}
Chris Masond1310b22008-01-24 16:13:08 -0500925
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100926int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
927 u64 *failed_start, struct extent_state **cached_state,
928 gfp_t mask)
929{
930 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
931 cached_state, mask);
932}
933
934
Josef Bacik462d6fa2011-09-26 13:56:12 -0400935/**
Liu Bo10983f22012-07-11 15:26:19 +0800936 * convert_extent_bit - convert all bits in a given range from one bit to
937 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400938 * @tree: the io tree to search
939 * @start: the start offset in bytes
940 * @end: the end offset in bytes (inclusive)
941 * @bits: the bits to set in this range
942 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400943 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400944 * @mask: the allocation mask
945 *
946 * This will go through and set bits for the given range. If any states exist
947 * already in this range they are set with the given bit and cleared of the
948 * clear_bits. This is only meant to be used by things that are mergeable, ie
949 * converting from say DELALLOC to DIRTY. This is not meant to be used with
950 * boundary bits like LOCK.
951 */
952int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacike6138872012-09-27 17:07:30 -0400953 int bits, int clear_bits,
954 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400955{
956 struct extent_state *state;
957 struct extent_state *prealloc = NULL;
958 struct rb_node *node;
959 int err = 0;
960 u64 last_start;
961 u64 last_end;
962
963again:
964 if (!prealloc && (mask & __GFP_WAIT)) {
965 prealloc = alloc_extent_state(mask);
966 if (!prealloc)
967 return -ENOMEM;
968 }
969
970 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400971 if (cached_state && *cached_state) {
972 state = *cached_state;
973 if (state->start <= start && state->end > start &&
974 state->tree) {
975 node = &state->rb_node;
976 goto hit_next;
977 }
978 }
979
Josef Bacik462d6fa2011-09-26 13:56:12 -0400980 /*
981 * this search will find all the extents that end after
982 * our range starts.
983 */
984 node = tree_search(tree, start);
985 if (!node) {
986 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500987 if (!prealloc) {
988 err = -ENOMEM;
989 goto out;
990 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400991 err = insert_state(tree, prealloc, start, end, &bits);
992 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400993 if (err)
994 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400995 goto out;
996 }
997 state = rb_entry(node, struct extent_state, rb_node);
998hit_next:
999 last_start = state->start;
1000 last_end = state->end;
1001
1002 /*
1003 * | ---- desired range ---- |
1004 * | state |
1005 *
1006 * Just lock what we found and keep going
1007 */
1008 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001009 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001010 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001011 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001012 if (last_end == (u64)-1)
1013 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001014 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001015 if (start < end && state && state->start == start &&
1016 !need_resched())
1017 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001018 goto search_again;
1019 }
1020
1021 /*
1022 * | ---- desired range ---- |
1023 * | state |
1024 * or
1025 * | ------------- state -------------- |
1026 *
1027 * We need to split the extent we found, and may flip bits on
1028 * second half.
1029 *
1030 * If the extent we found extends past our
1031 * range, we just split and search again. It'll get split
1032 * again the next time though.
1033 *
1034 * If the extent we found is inside our range, we set the
1035 * desired bit on it.
1036 */
1037 if (state->start < start) {
1038 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001039 if (!prealloc) {
1040 err = -ENOMEM;
1041 goto out;
1042 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001043 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001044 if (err)
1045 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001046 prealloc = NULL;
1047 if (err)
1048 goto out;
1049 if (state->end <= end) {
1050 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001051 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001052 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001053 if (last_end == (u64)-1)
1054 goto out;
1055 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001056 if (start < end && state && state->start == start &&
1057 !need_resched())
1058 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001059 }
1060 goto search_again;
1061 }
1062 /*
1063 * | ---- desired range ---- |
1064 * | state | or | state |
1065 *
1066 * There's a hole, we need to insert something in it and
1067 * ignore the extent we found.
1068 */
1069 if (state->start > start) {
1070 u64 this_end;
1071 if (end < last_start)
1072 this_end = end;
1073 else
1074 this_end = last_start - 1;
1075
1076 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001077 if (!prealloc) {
1078 err = -ENOMEM;
1079 goto out;
1080 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001081
1082 /*
1083 * Avoid to free 'prealloc' if it can be merged with
1084 * the later extent.
1085 */
1086 err = insert_state(tree, prealloc, start, this_end,
1087 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001088 if (err)
1089 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001090 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001091 prealloc = NULL;
1092 start = this_end + 1;
1093 goto search_again;
1094 }
1095 /*
1096 * | ---- desired range ---- |
1097 * | state |
1098 * We need to split the extent, and set the bit
1099 * on the first half
1100 */
1101 if (state->start <= end && state->end > end) {
1102 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001103 if (!prealloc) {
1104 err = -ENOMEM;
1105 goto out;
1106 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001107
1108 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001109 if (err)
1110 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001111
1112 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001113 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001114 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001115 prealloc = NULL;
1116 goto out;
1117 }
1118
1119 goto search_again;
1120
1121out:
1122 spin_unlock(&tree->lock);
1123 if (prealloc)
1124 free_extent_state(prealloc);
1125
1126 return err;
1127
1128search_again:
1129 if (start > end)
1130 goto out;
1131 spin_unlock(&tree->lock);
1132 if (mask & __GFP_WAIT)
1133 cond_resched();
1134 goto again;
1135}
1136
Chris Masond1310b22008-01-24 16:13:08 -05001137/* wrappers around set/clear extent bit */
1138int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1139 gfp_t mask)
1140{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001141 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001142 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
1145int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1146 int bits, gfp_t mask)
1147{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001148 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001149 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001150}
Chris Masond1310b22008-01-24 16:13:08 -05001151
1152int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1153 int bits, gfp_t mask)
1154{
Chris Mason2c64c532009-09-02 15:04:12 -04001155 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001156}
Chris Masond1310b22008-01-24 16:13:08 -05001157
1158int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001159 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001160{
1161 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001162 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001163 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001164}
Chris Masond1310b22008-01-24 16:13:08 -05001165
Liu Bo9e8a4a82012-09-05 19:10:51 -06001166int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1167 struct extent_state **cached_state, gfp_t mask)
1168{
1169 return set_extent_bit(tree, start, end,
1170 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1171 NULL, cached_state, mask);
1172}
1173
Chris Masond1310b22008-01-24 16:13:08 -05001174int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1175 gfp_t mask)
1176{
1177 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001178 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001179 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001180}
Chris Masond1310b22008-01-24 16:13:08 -05001181
1182int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1183 gfp_t mask)
1184{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001185 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001186 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001187}
Chris Masond1310b22008-01-24 16:13:08 -05001188
Chris Masond1310b22008-01-24 16:13:08 -05001189int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001190 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001191{
Arne Jansen507903b2011-04-06 10:02:20 +00001192 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001193 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001194}
Chris Masond1310b22008-01-24 16:13:08 -05001195
Josef Bacik5fd02042012-05-02 14:00:54 -04001196int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1197 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001198{
Chris Mason2c64c532009-09-02 15:04:12 -04001199 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001200 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001201}
Chris Masond1310b22008-01-24 16:13:08 -05001202
Chris Masond352ac62008-09-29 15:18:18 -04001203/*
1204 * either insert or lock state struct between start and end use mask to tell
1205 * us if waiting is desired.
1206 */
Chris Mason1edbb732009-09-02 13:24:36 -04001207int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001208 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001209{
1210 int err;
1211 u64 failed_start;
1212 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001213 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1214 EXTENT_LOCKED, &failed_start,
1215 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001216 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001217 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1218 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001219 } else
Chris Masond1310b22008-01-24 16:13:08 -05001220 break;
Chris Masond1310b22008-01-24 16:13:08 -05001221 WARN_ON(start > end);
1222 }
1223 return err;
1224}
Chris Masond1310b22008-01-24 16:13:08 -05001225
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001226int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001227{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001228 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001229}
1230
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001231int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001232{
1233 int err;
1234 u64 failed_start;
1235
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001236 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1237 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001238 if (err == -EEXIST) {
1239 if (failed_start > start)
1240 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001241 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001242 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001243 }
Josef Bacik25179202008-10-29 14:49:05 -04001244 return 1;
1245}
Josef Bacik25179202008-10-29 14:49:05 -04001246
Chris Mason2c64c532009-09-02 15:04:12 -04001247int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1248 struct extent_state **cached, gfp_t mask)
1249{
1250 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1251 mask);
1252}
1253
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001254int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001255{
Chris Mason2c64c532009-09-02 15:04:12 -04001256 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001257 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001258}
Chris Masond1310b22008-01-24 16:13:08 -05001259
Chris Mason4adaa612013-03-26 13:07:00 -04001260int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1261{
1262 unsigned long index = start >> PAGE_CACHE_SHIFT;
1263 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1264 struct page *page;
1265
1266 while (index <= end_index) {
1267 page = find_get_page(inode->i_mapping, index);
1268 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1269 clear_page_dirty_for_io(page);
1270 page_cache_release(page);
1271 index++;
1272 }
1273 return 0;
1274}
1275
1276int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1277{
1278 unsigned long index = start >> PAGE_CACHE_SHIFT;
1279 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1280 struct page *page;
1281
1282 while (index <= end_index) {
1283 page = find_get_page(inode->i_mapping, index);
1284 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1285 account_page_redirty(page);
1286 __set_page_dirty_nobuffers(page);
1287 page_cache_release(page);
1288 index++;
1289 }
1290 return 0;
1291}
1292
Chris Masond1310b22008-01-24 16:13:08 -05001293/*
Chris Masond1310b22008-01-24 16:13:08 -05001294 * helper function to set both pages and extents in the tree writeback
1295 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001296static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001297{
1298 unsigned long index = start >> PAGE_CACHE_SHIFT;
1299 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1300 struct page *page;
1301
1302 while (index <= end_index) {
1303 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001305 set_page_writeback(page);
1306 page_cache_release(page);
1307 index++;
1308 }
Chris Masond1310b22008-01-24 16:13:08 -05001309 return 0;
1310}
Chris Masond1310b22008-01-24 16:13:08 -05001311
Chris Masond352ac62008-09-29 15:18:18 -04001312/* find the first state struct with 'bits' set after 'start', and
1313 * return it. tree->lock must be held. NULL will returned if
1314 * nothing was found after 'start'
1315 */
Chris Masond7fc6402008-02-18 12:12:38 -05001316struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1317 u64 start, int bits)
1318{
1319 struct rb_node *node;
1320 struct extent_state *state;
1321
1322 /*
1323 * this search will find all the extents that end after
1324 * our range starts.
1325 */
1326 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001327 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001328 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001329
Chris Masond3977122009-01-05 21:25:51 -05001330 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001331 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001332 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001333 return state;
Chris Masond3977122009-01-05 21:25:51 -05001334
Chris Masond7fc6402008-02-18 12:12:38 -05001335 node = rb_next(node);
1336 if (!node)
1337 break;
1338 }
1339out:
1340 return NULL;
1341}
Chris Masond7fc6402008-02-18 12:12:38 -05001342
Chris Masond352ac62008-09-29 15:18:18 -04001343/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001344 * find the first offset in the io tree with 'bits' set. zero is
1345 * returned if we find something, and *start_ret and *end_ret are
1346 * set to reflect the state struct that was found.
1347 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001348 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001349 */
1350int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Josef Bacike6138872012-09-27 17:07:30 -04001351 u64 *start_ret, u64 *end_ret, int bits,
1352 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001353{
1354 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001355 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001356 int ret = 1;
1357
1358 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001359 if (cached_state && *cached_state) {
1360 state = *cached_state;
1361 if (state->end == start - 1 && state->tree) {
1362 n = rb_next(&state->rb_node);
1363 while (n) {
1364 state = rb_entry(n, struct extent_state,
1365 rb_node);
1366 if (state->state & bits)
1367 goto got_it;
1368 n = rb_next(n);
1369 }
1370 free_extent_state(*cached_state);
1371 *cached_state = NULL;
1372 goto out;
1373 }
1374 free_extent_state(*cached_state);
1375 *cached_state = NULL;
1376 }
1377
Xiao Guangrong69261c42011-07-14 03:19:45 +00001378 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001379got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001380 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001381 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001382 *start_ret = state->start;
1383 *end_ret = state->end;
1384 ret = 0;
1385 }
Josef Bacike6138872012-09-27 17:07:30 -04001386out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001387 spin_unlock(&tree->lock);
1388 return ret;
1389}
1390
1391/*
Chris Masond352ac62008-09-29 15:18:18 -04001392 * find a contiguous range of bytes in the file marked as delalloc, not
1393 * more than 'max_bytes'. start and end are used to return the range,
1394 *
1395 * 1 is returned if we find something, 0 if nothing was in the tree
1396 */
Chris Masonc8b97812008-10-29 14:49:59 -04001397static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001398 u64 *start, u64 *end, u64 max_bytes,
1399 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001400{
1401 struct rb_node *node;
1402 struct extent_state *state;
1403 u64 cur_start = *start;
1404 u64 found = 0;
1405 u64 total_bytes = 0;
1406
Chris Masoncad321a2008-12-17 14:51:42 -05001407 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001408
Chris Masond1310b22008-01-24 16:13:08 -05001409 /*
1410 * this search will find all the extents that end after
1411 * our range starts.
1412 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001413 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001414 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001415 if (!found)
1416 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001417 goto out;
1418 }
1419
Chris Masond3977122009-01-05 21:25:51 -05001420 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001421 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001422 if (found && (state->start != cur_start ||
1423 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001424 goto out;
1425 }
1426 if (!(state->state & EXTENT_DELALLOC)) {
1427 if (!found)
1428 *end = state->end;
1429 goto out;
1430 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001431 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001432 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001433 *cached_state = state;
1434 atomic_inc(&state->refs);
1435 }
Chris Masond1310b22008-01-24 16:13:08 -05001436 found++;
1437 *end = state->end;
1438 cur_start = state->end + 1;
1439 node = rb_next(node);
1440 if (!node)
1441 break;
1442 total_bytes += state->end - state->start + 1;
1443 if (total_bytes >= max_bytes)
1444 break;
1445 }
1446out:
Chris Masoncad321a2008-12-17 14:51:42 -05001447 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001448 return found;
1449}
1450
Jeff Mahoney143bede2012-03-01 14:56:26 +01001451static noinline void __unlock_for_delalloc(struct inode *inode,
1452 struct page *locked_page,
1453 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001454{
1455 int ret;
1456 struct page *pages[16];
1457 unsigned long index = start >> PAGE_CACHE_SHIFT;
1458 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1459 unsigned long nr_pages = end_index - index + 1;
1460 int i;
1461
1462 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001463 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001464
Chris Masond3977122009-01-05 21:25:51 -05001465 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001466 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001467 min_t(unsigned long, nr_pages,
1468 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001469 for (i = 0; i < ret; i++) {
1470 if (pages[i] != locked_page)
1471 unlock_page(pages[i]);
1472 page_cache_release(pages[i]);
1473 }
1474 nr_pages -= ret;
1475 index += ret;
1476 cond_resched();
1477 }
Chris Masonc8b97812008-10-29 14:49:59 -04001478}
1479
1480static noinline int lock_delalloc_pages(struct inode *inode,
1481 struct page *locked_page,
1482 u64 delalloc_start,
1483 u64 delalloc_end)
1484{
1485 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1486 unsigned long start_index = index;
1487 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1488 unsigned long pages_locked = 0;
1489 struct page *pages[16];
1490 unsigned long nrpages;
1491 int ret;
1492 int i;
1493
1494 /* the caller is responsible for locking the start index */
1495 if (index == locked_page->index && index == end_index)
1496 return 0;
1497
1498 /* skip the page at the start index */
1499 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001500 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001501 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001502 min_t(unsigned long,
1503 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001504 if (ret == 0) {
1505 ret = -EAGAIN;
1506 goto done;
1507 }
1508 /* now we have an array of pages, lock them all */
1509 for (i = 0; i < ret; i++) {
1510 /*
1511 * the caller is taking responsibility for
1512 * locked_page
1513 */
Chris Mason771ed682008-11-06 22:02:51 -05001514 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001515 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001516 if (!PageDirty(pages[i]) ||
1517 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001518 ret = -EAGAIN;
1519 unlock_page(pages[i]);
1520 page_cache_release(pages[i]);
1521 goto done;
1522 }
1523 }
Chris Masonc8b97812008-10-29 14:49:59 -04001524 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001525 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001526 }
Chris Masonc8b97812008-10-29 14:49:59 -04001527 nrpages -= ret;
1528 index += ret;
1529 cond_resched();
1530 }
1531 ret = 0;
1532done:
1533 if (ret && pages_locked) {
1534 __unlock_for_delalloc(inode, locked_page,
1535 delalloc_start,
1536 ((u64)(start_index + pages_locked - 1)) <<
1537 PAGE_CACHE_SHIFT);
1538 }
1539 return ret;
1540}
1541
1542/*
1543 * find a contiguous range of bytes in the file marked as delalloc, not
1544 * more than 'max_bytes'. start and end are used to return the range,
1545 *
1546 * 1 is returned if we find something, 0 if nothing was in the tree
1547 */
1548static noinline u64 find_lock_delalloc_range(struct inode *inode,
1549 struct extent_io_tree *tree,
1550 struct page *locked_page,
1551 u64 *start, u64 *end,
1552 u64 max_bytes)
1553{
1554 u64 delalloc_start;
1555 u64 delalloc_end;
1556 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001557 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001558 int ret;
1559 int loops = 0;
1560
1561again:
1562 /* step one, find a bunch of delalloc bytes starting at start */
1563 delalloc_start = *start;
1564 delalloc_end = 0;
1565 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001566 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001567 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001568 *start = delalloc_start;
1569 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001570 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001571 return found;
1572 }
1573
1574 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001575 * start comes from the offset of locked_page. We have to lock
1576 * pages in order, so we can't process delalloc bytes before
1577 * locked_page
1578 */
Chris Masond3977122009-01-05 21:25:51 -05001579 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001580 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001581
1582 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001583 * make sure to limit the number of pages we try to lock down
1584 * if we're looping.
1585 */
Chris Masond3977122009-01-05 21:25:51 -05001586 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001587 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001588
Chris Masonc8b97812008-10-29 14:49:59 -04001589 /* step two, lock all the pages after the page that has start */
1590 ret = lock_delalloc_pages(inode, locked_page,
1591 delalloc_start, delalloc_end);
1592 if (ret == -EAGAIN) {
1593 /* some of the pages are gone, lets avoid looping by
1594 * shortening the size of the delalloc range we're searching
1595 */
Chris Mason9655d292009-09-02 15:22:30 -04001596 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001597 if (!loops) {
1598 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1599 max_bytes = PAGE_CACHE_SIZE - offset;
1600 loops = 1;
1601 goto again;
1602 } else {
1603 found = 0;
1604 goto out_failed;
1605 }
1606 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001608
1609 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001610 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001611
1612 /* then test to make sure it is all still delalloc */
1613 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001614 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001615 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001616 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1617 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001618 __unlock_for_delalloc(inode, locked_page,
1619 delalloc_start, delalloc_end);
1620 cond_resched();
1621 goto again;
1622 }
Chris Mason9655d292009-09-02 15:22:30 -04001623 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001624 *start = delalloc_start;
1625 *end = delalloc_end;
1626out_failed:
1627 return found;
1628}
1629
1630int extent_clear_unlock_delalloc(struct inode *inode,
1631 struct extent_io_tree *tree,
1632 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001633 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001634{
1635 int ret;
1636 struct page *pages[16];
1637 unsigned long index = start >> PAGE_CACHE_SHIFT;
1638 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1639 unsigned long nr_pages = end_index - index + 1;
1640 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001641 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001642
Chris Masona791e352009-10-08 11:27:10 -04001643 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001644 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001645 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001646 clear_bits |= EXTENT_DIRTY;
1647
Chris Masona791e352009-10-08 11:27:10 -04001648 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001649 clear_bits |= EXTENT_DELALLOC;
1650
Chris Mason2c64c532009-09-02 15:04:12 -04001651 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001652 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1653 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1654 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001655 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001656
Chris Masond3977122009-01-05 21:25:51 -05001657 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001658 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001659 min_t(unsigned long,
1660 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001661 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001662
Chris Masona791e352009-10-08 11:27:10 -04001663 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001664 SetPagePrivate2(pages[i]);
1665
Chris Masonc8b97812008-10-29 14:49:59 -04001666 if (pages[i] == locked_page) {
1667 page_cache_release(pages[i]);
1668 continue;
1669 }
Chris Masona791e352009-10-08 11:27:10 -04001670 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001671 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001672 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001673 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001674 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001675 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001676 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001677 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001678 page_cache_release(pages[i]);
1679 }
1680 nr_pages -= ret;
1681 index += ret;
1682 cond_resched();
1683 }
1684 return 0;
1685}
Chris Masonc8b97812008-10-29 14:49:59 -04001686
Chris Masond352ac62008-09-29 15:18:18 -04001687/*
1688 * count the number of bytes in the tree that have a given bit(s)
1689 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1690 * cached. The total number found is returned.
1691 */
Chris Masond1310b22008-01-24 16:13:08 -05001692u64 count_range_bits(struct extent_io_tree *tree,
1693 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001694 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001695{
1696 struct rb_node *node;
1697 struct extent_state *state;
1698 u64 cur_start = *start;
1699 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001700 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001701 int found = 0;
1702
1703 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001704 WARN_ON(1);
1705 return 0;
1706 }
1707
Chris Masoncad321a2008-12-17 14:51:42 -05001708 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001709 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1710 total_bytes = tree->dirty_bytes;
1711 goto out;
1712 }
1713 /*
1714 * this search will find all the extents that end after
1715 * our range starts.
1716 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001717 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001718 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001719 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001720
Chris Masond3977122009-01-05 21:25:51 -05001721 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001722 state = rb_entry(node, struct extent_state, rb_node);
1723 if (state->start > search_end)
1724 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001725 if (contig && found && state->start > last + 1)
1726 break;
1727 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001728 total_bytes += min(search_end, state->end) + 1 -
1729 max(cur_start, state->start);
1730 if (total_bytes >= max_bytes)
1731 break;
1732 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001733 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001734 found = 1;
1735 }
Chris Masonec29ed52011-02-23 16:23:20 -05001736 last = state->end;
1737 } else if (contig && found) {
1738 break;
Chris Masond1310b22008-01-24 16:13:08 -05001739 }
1740 node = rb_next(node);
1741 if (!node)
1742 break;
1743 }
1744out:
Chris Masoncad321a2008-12-17 14:51:42 -05001745 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001746 return total_bytes;
1747}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001748
Chris Masond352ac62008-09-29 15:18:18 -04001749/*
1750 * set the private field for a given byte offset in the tree. If there isn't
1751 * an extent_state there already, this does nothing.
1752 */
Chris Masond1310b22008-01-24 16:13:08 -05001753int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1754{
1755 struct rb_node *node;
1756 struct extent_state *state;
1757 int ret = 0;
1758
Chris Masoncad321a2008-12-17 14:51:42 -05001759 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001760 /*
1761 * this search will find all the extents that end after
1762 * our range starts.
1763 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001764 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001765 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001766 ret = -ENOENT;
1767 goto out;
1768 }
1769 state = rb_entry(node, struct extent_state, rb_node);
1770 if (state->start != start) {
1771 ret = -ENOENT;
1772 goto out;
1773 }
1774 state->private = private;
1775out:
Chris Masoncad321a2008-12-17 14:51:42 -05001776 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001777 return ret;
1778}
1779
1780int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1781{
1782 struct rb_node *node;
1783 struct extent_state *state;
1784 int ret = 0;
1785
Chris Masoncad321a2008-12-17 14:51:42 -05001786 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001787 /*
1788 * this search will find all the extents that end after
1789 * our range starts.
1790 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001791 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001792 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001793 ret = -ENOENT;
1794 goto out;
1795 }
1796 state = rb_entry(node, struct extent_state, rb_node);
1797 if (state->start != start) {
1798 ret = -ENOENT;
1799 goto out;
1800 }
1801 *private = state->private;
1802out:
Chris Masoncad321a2008-12-17 14:51:42 -05001803 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001804 return ret;
1805}
1806
1807/*
1808 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001809 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001810 * has the bits set. Otherwise, 1 is returned if any bit in the
1811 * range is found set.
1812 */
1813int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001814 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001815{
1816 struct extent_state *state = NULL;
1817 struct rb_node *node;
1818 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001819
Chris Masoncad321a2008-12-17 14:51:42 -05001820 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001821 if (cached && cached->tree && cached->start <= start &&
1822 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001823 node = &cached->rb_node;
1824 else
1825 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001826 while (node && start <= end) {
1827 state = rb_entry(node, struct extent_state, rb_node);
1828
1829 if (filled && state->start > start) {
1830 bitset = 0;
1831 break;
1832 }
1833
1834 if (state->start > end)
1835 break;
1836
1837 if (state->state & bits) {
1838 bitset = 1;
1839 if (!filled)
1840 break;
1841 } else if (filled) {
1842 bitset = 0;
1843 break;
1844 }
Chris Mason46562ce2009-09-23 20:23:16 -04001845
1846 if (state->end == (u64)-1)
1847 break;
1848
Chris Masond1310b22008-01-24 16:13:08 -05001849 start = state->end + 1;
1850 if (start > end)
1851 break;
1852 node = rb_next(node);
1853 if (!node) {
1854 if (filled)
1855 bitset = 0;
1856 break;
1857 }
1858 }
Chris Masoncad321a2008-12-17 14:51:42 -05001859 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001860 return bitset;
1861}
Chris Masond1310b22008-01-24 16:13:08 -05001862
1863/*
1864 * helper function to set a given page up to date if all the
1865 * extents in the tree for that page are up to date
1866 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001867static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001868{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001869 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001870 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001871 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001872 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001873}
1874
1875/*
1876 * helper function to unlock a page if all the extents in the tree
1877 * for that page are unlocked
1878 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001879static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001880{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001881 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001882 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001883 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001884 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001885}
1886
1887/*
1888 * helper function to end page writeback if all the extents
1889 * in the tree for that page are done with writeback
1890 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001891static void check_page_writeback(struct extent_io_tree *tree,
1892 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001893{
Chris Mason1edbb732009-09-02 13:24:36 -04001894 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001895}
1896
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001897/*
1898 * When IO fails, either with EIO or csum verification fails, we
1899 * try other mirrors that might have a good copy of the data. This
1900 * io_failure_record is used to record state as we go through all the
1901 * mirrors. If another mirror has good data, the page is set up to date
1902 * and things continue. If a good mirror can't be found, the original
1903 * bio end_io callback is called to indicate things have failed.
1904 */
1905struct io_failure_record {
1906 struct page *page;
1907 u64 start;
1908 u64 len;
1909 u64 logical;
1910 unsigned long bio_flags;
1911 int this_mirror;
1912 int failed_mirror;
1913 int in_validation;
1914};
1915
1916static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1917 int did_repair)
1918{
1919 int ret;
1920 int err = 0;
1921 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1922
1923 set_state_private(failure_tree, rec->start, 0);
1924 ret = clear_extent_bits(failure_tree, rec->start,
1925 rec->start + rec->len - 1,
1926 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1927 if (ret)
1928 err = ret;
1929
David Woodhouse53b381b2013-01-29 18:40:14 -05001930 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1931 rec->start + rec->len - 1,
1932 EXTENT_DAMAGED, GFP_NOFS);
1933 if (ret && !err)
1934 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001935
1936 kfree(rec);
1937 return err;
1938}
1939
1940static void repair_io_failure_callback(struct bio *bio, int err)
1941{
1942 complete(bio->bi_private);
1943}
1944
1945/*
1946 * this bypasses the standard btrfs submit functions deliberately, as
1947 * the standard behavior is to write all copies in a raid setup. here we only
1948 * want to write the one bad copy. so we do the mapping for ourselves and issue
1949 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001950 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001951 * actually prevents the read that triggered the error from finishing.
1952 * currently, there can be no more than two copies of every data bit. thus,
1953 * exactly one rewrite is required.
1954 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001955int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001956 u64 length, u64 logical, struct page *page,
1957 int mirror_num)
1958{
1959 struct bio *bio;
1960 struct btrfs_device *dev;
1961 DECLARE_COMPLETION_ONSTACK(compl);
1962 u64 map_length = 0;
1963 u64 sector;
1964 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05001965 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001966 int ret;
1967
1968 BUG_ON(!mirror_num);
1969
David Woodhouse53b381b2013-01-29 18:40:14 -05001970 /* we can't repair anything in raid56 yet */
1971 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
1972 return 0;
1973
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974 bio = bio_alloc(GFP_NOFS, 1);
1975 if (!bio)
1976 return -EIO;
1977 bio->bi_private = &compl;
1978 bio->bi_end_io = repair_io_failure_callback;
1979 bio->bi_size = 0;
1980 map_length = length;
1981
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001982 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001983 &map_length, &bbio, mirror_num);
1984 if (ret) {
1985 bio_put(bio);
1986 return -EIO;
1987 }
1988 BUG_ON(mirror_num != bbio->mirror_num);
1989 sector = bbio->stripes[mirror_num-1].physical >> 9;
1990 bio->bi_sector = sector;
1991 dev = bbio->stripes[mirror_num-1].dev;
1992 kfree(bbio);
1993 if (!dev || !dev->bdev || !dev->writeable) {
1994 bio_put(bio);
1995 return -EIO;
1996 }
1997 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00001998 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001999 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002000 wait_for_completion(&compl);
2001
2002 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2003 /* try to remap that extent elsewhere? */
2004 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002005 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002006 return -EIO;
2007 }
2008
Anand Jaind5b025d2012-07-02 22:05:21 -06002009 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002010 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2011 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002012
2013 bio_put(bio);
2014 return 0;
2015}
2016
Josef Bacikea466792012-03-26 21:57:36 -04002017int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2018 int mirror_num)
2019{
Josef Bacikea466792012-03-26 21:57:36 -04002020 u64 start = eb->start;
2021 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002022 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002023
2024 for (i = 0; i < num_pages; i++) {
2025 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002026 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002027 start, p, mirror_num);
2028 if (ret)
2029 break;
2030 start += PAGE_CACHE_SIZE;
2031 }
2032
2033 return ret;
2034}
2035
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036/*
2037 * each time an IO finishes, we do a fast check in the IO failure tree
2038 * to see if we need to process or clean up an io_failure_record
2039 */
2040static int clean_io_failure(u64 start, struct page *page)
2041{
2042 u64 private;
2043 u64 private_failure;
2044 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002045 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046 struct extent_state *state;
2047 int num_copies;
2048 int did_repair = 0;
2049 int ret;
2050 struct inode *inode = page->mapping->host;
2051
2052 private = 0;
2053 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2054 (u64)-1, 1, EXTENT_DIRTY, 0);
2055 if (!ret)
2056 return 0;
2057
2058 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2059 &private_failure);
2060 if (ret)
2061 return 0;
2062
2063 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2064 BUG_ON(!failrec->this_mirror);
2065
2066 if (failrec->in_validation) {
2067 /* there was no real error, just free the record */
2068 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2069 failrec->start);
2070 did_repair = 1;
2071 goto out;
2072 }
2073
2074 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2075 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2076 failrec->start,
2077 EXTENT_LOCKED);
2078 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2079
2080 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002081 fs_info = BTRFS_I(inode)->root->fs_info;
2082 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2083 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002084 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002085 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086 failrec->logical, page,
2087 failrec->failed_mirror);
2088 did_repair = !ret;
2089 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002090 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002091 }
2092
2093out:
2094 if (!ret)
2095 ret = free_io_failure(inode, failrec, did_repair);
2096
2097 return ret;
2098}
2099
2100/*
2101 * this is a generic handler for readpage errors (default
2102 * readpage_io_failed_hook). if other copies exist, read those and write back
2103 * good data to the failed position. does not investigate in remapping the
2104 * failed extent elsewhere, hoping the device will be smart enough to do this as
2105 * needed
2106 */
2107
2108static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2109 u64 start, u64 end, int failed_mirror,
2110 struct extent_state *state)
2111{
2112 struct io_failure_record *failrec = NULL;
2113 u64 private;
2114 struct extent_map *em;
2115 struct inode *inode = page->mapping->host;
2116 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2117 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2118 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2119 struct bio *bio;
2120 int num_copies;
2121 int ret;
2122 int read_mode;
2123 u64 logical;
2124
2125 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2126
2127 ret = get_state_private(failure_tree, start, &private);
2128 if (ret) {
2129 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2130 if (!failrec)
2131 return -ENOMEM;
2132 failrec->start = start;
2133 failrec->len = end - start + 1;
2134 failrec->this_mirror = 0;
2135 failrec->bio_flags = 0;
2136 failrec->in_validation = 0;
2137
2138 read_lock(&em_tree->lock);
2139 em = lookup_extent_mapping(em_tree, start, failrec->len);
2140 if (!em) {
2141 read_unlock(&em_tree->lock);
2142 kfree(failrec);
2143 return -EIO;
2144 }
2145
2146 if (em->start > start || em->start + em->len < start) {
2147 free_extent_map(em);
2148 em = NULL;
2149 }
2150 read_unlock(&em_tree->lock);
2151
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002152 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002153 kfree(failrec);
2154 return -EIO;
2155 }
2156 logical = start - em->start;
2157 logical = em->block_start + logical;
2158 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2159 logical = em->block_start;
2160 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2161 extent_set_compress_type(&failrec->bio_flags,
2162 em->compress_type);
2163 }
2164 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2165 "len=%llu\n", logical, start, failrec->len);
2166 failrec->logical = logical;
2167 free_extent_map(em);
2168
2169 /* set the bits in the private failure tree */
2170 ret = set_extent_bits(failure_tree, start, end,
2171 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2172 if (ret >= 0)
2173 ret = set_state_private(failure_tree, start,
2174 (u64)(unsigned long)failrec);
2175 /* set the bits in the inode's tree */
2176 if (ret >= 0)
2177 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2178 GFP_NOFS);
2179 if (ret < 0) {
2180 kfree(failrec);
2181 return ret;
2182 }
2183 } else {
2184 failrec = (struct io_failure_record *)(unsigned long)private;
2185 pr_debug("bio_readpage_error: (found) logical=%llu, "
2186 "start=%llu, len=%llu, validation=%d\n",
2187 failrec->logical, failrec->start, failrec->len,
2188 failrec->in_validation);
2189 /*
2190 * when data can be on disk more than twice, add to failrec here
2191 * (e.g. with a list for failed_mirror) to make
2192 * clean_io_failure() clean all those errors at once.
2193 */
2194 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002195 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2196 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002197 if (num_copies == 1) {
2198 /*
2199 * we only have a single copy of the data, so don't bother with
2200 * all the retry and error correction code that follows. no
2201 * matter what the error is, it is very likely to persist.
2202 */
2203 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2204 "state=%p, num_copies=%d, next_mirror %d, "
2205 "failed_mirror %d\n", state, num_copies,
2206 failrec->this_mirror, failed_mirror);
2207 free_io_failure(inode, failrec, 0);
2208 return -EIO;
2209 }
2210
2211 if (!state) {
2212 spin_lock(&tree->lock);
2213 state = find_first_extent_bit_state(tree, failrec->start,
2214 EXTENT_LOCKED);
2215 if (state && state->start != failrec->start)
2216 state = NULL;
2217 spin_unlock(&tree->lock);
2218 }
2219
2220 /*
2221 * there are two premises:
2222 * a) deliver good data to the caller
2223 * b) correct the bad sectors on disk
2224 */
2225 if (failed_bio->bi_vcnt > 1) {
2226 /*
2227 * to fulfill b), we need to know the exact failing sectors, as
2228 * we don't want to rewrite any more than the failed ones. thus,
2229 * we need separate read requests for the failed bio
2230 *
2231 * if the following BUG_ON triggers, our validation request got
2232 * merged. we need separate requests for our algorithm to work.
2233 */
2234 BUG_ON(failrec->in_validation);
2235 failrec->in_validation = 1;
2236 failrec->this_mirror = failed_mirror;
2237 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2238 } else {
2239 /*
2240 * we're ready to fulfill a) and b) alongside. get a good copy
2241 * of the failed sector and if we succeed, we have setup
2242 * everything for repair_io_failure to do the rest for us.
2243 */
2244 if (failrec->in_validation) {
2245 BUG_ON(failrec->this_mirror != failed_mirror);
2246 failrec->in_validation = 0;
2247 failrec->this_mirror = 0;
2248 }
2249 failrec->failed_mirror = failed_mirror;
2250 failrec->this_mirror++;
2251 if (failrec->this_mirror == failed_mirror)
2252 failrec->this_mirror++;
2253 read_mode = READ_SYNC;
2254 }
2255
2256 if (!state || failrec->this_mirror > num_copies) {
2257 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2258 "next_mirror %d, failed_mirror %d\n", state,
2259 num_copies, failrec->this_mirror, failed_mirror);
2260 free_io_failure(inode, failrec, 0);
2261 return -EIO;
2262 }
2263
2264 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002265 if (!bio) {
2266 free_io_failure(inode, failrec, 0);
2267 return -EIO;
2268 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002269 bio->bi_private = state;
2270 bio->bi_end_io = failed_bio->bi_end_io;
2271 bio->bi_sector = failrec->logical >> 9;
2272 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2273 bio->bi_size = 0;
2274
2275 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2276
2277 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2278 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2279 failrec->this_mirror, num_copies, failrec->in_validation);
2280
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002281 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2282 failrec->this_mirror,
2283 failrec->bio_flags, 0);
2284 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002285}
2286
Chris Masond1310b22008-01-24 16:13:08 -05002287/* lots and lots of room for performance fixes in the end_bio funcs */
2288
Jeff Mahoney87826df2012-02-15 16:23:57 +01002289int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2290{
2291 int uptodate = (err == 0);
2292 struct extent_io_tree *tree;
2293 int ret;
2294
2295 tree = &BTRFS_I(page->mapping->host)->io_tree;
2296
2297 if (tree->ops && tree->ops->writepage_end_io_hook) {
2298 ret = tree->ops->writepage_end_io_hook(page, start,
2299 end, NULL, uptodate);
2300 if (ret)
2301 uptodate = 0;
2302 }
2303
Jeff Mahoney87826df2012-02-15 16:23:57 +01002304 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002305 ClearPageUptodate(page);
2306 SetPageError(page);
2307 }
2308 return 0;
2309}
2310
Chris Masond1310b22008-01-24 16:13:08 -05002311/*
2312 * after a writepage IO is done, we need to:
2313 * clear the uptodate bits on error
2314 * clear the writeback bits in the extent tree for this IO
2315 * end_page_writeback if the page has no more pending IO
2316 *
2317 * Scheduling is not allowed, so the extent state tree is expected
2318 * to have one and only one object corresponding to this IO.
2319 */
Chris Masond1310b22008-01-24 16:13:08 -05002320static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002321{
Chris Masond1310b22008-01-24 16:13:08 -05002322 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002323 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002324 u64 start;
2325 u64 end;
2326 int whole_page;
2327
Chris Masond1310b22008-01-24 16:13:08 -05002328 do {
2329 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002330 tree = &BTRFS_I(page->mapping->host)->io_tree;
2331
Miao Xie4eee4fa2012-12-21 09:17:45 +00002332 start = page_offset(page) + bvec->bv_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002333 end = start + bvec->bv_len - 1;
2334
2335 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2336 whole_page = 1;
2337 else
2338 whole_page = 0;
2339
2340 if (--bvec >= bio->bi_io_vec)
2341 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002342
Jeff Mahoney87826df2012-02-15 16:23:57 +01002343 if (end_extent_writepage(page, err, start, end))
2344 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002345
Chris Masond1310b22008-01-24 16:13:08 -05002346 if (whole_page)
2347 end_page_writeback(page);
2348 else
2349 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002350 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002351
Chris Masond1310b22008-01-24 16:13:08 -05002352 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002353}
2354
2355/*
2356 * after a readpage IO is done, we need to:
2357 * clear the uptodate bits on error
2358 * set the uptodate bits if things worked
2359 * set the page up to date if all extents in the tree are uptodate
2360 * clear the lock bit in the extent tree
2361 * unlock the page if there are no other extents locked for it
2362 *
2363 * Scheduling is not allowed, so the extent state tree is expected
2364 * to have one and only one object corresponding to this IO.
2365 */
Chris Masond1310b22008-01-24 16:13:08 -05002366static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002367{
2368 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002369 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2370 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002371 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002372 u64 start;
2373 u64 end;
2374 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002375 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002376 int ret;
2377
Chris Masond20f7042008-12-08 16:58:54 -05002378 if (err)
2379 uptodate = 0;
2380
Chris Masond1310b22008-01-24 16:13:08 -05002381 do {
2382 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002383 struct extent_state *cached = NULL;
2384 struct extent_state *state;
2385
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002386 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2387 "mirror=%ld\n", (u64)bio->bi_sector, err,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002388 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002389 tree = &BTRFS_I(page->mapping->host)->io_tree;
2390
Miao Xie4eee4fa2012-12-21 09:17:45 +00002391 start = page_offset(page) + bvec->bv_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002392 end = start + bvec->bv_len - 1;
2393
2394 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2395 whole_page = 1;
2396 else
2397 whole_page = 0;
2398
Chris Mason4125bf72010-02-03 18:18:45 +00002399 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002400 prefetchw(&bvec->bv_page->flags);
2401
Arne Jansen507903b2011-04-06 10:02:20 +00002402 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002403 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002404 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002405 /*
2406 * take a reference on the state, unlock will drop
2407 * the ref
2408 */
2409 cache_state(state, &cached);
2410 }
2411 spin_unlock(&tree->lock);
2412
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002413 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002414 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002415 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002416 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002417 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002418 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002419 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002420 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002421 }
Josef Bacikea466792012-03-26 21:57:36 -04002422
Josef Bacikea466792012-03-26 21:57:36 -04002423 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002424 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002425 if (!ret && !err &&
2426 test_bit(BIO_UPTODATE, &bio->bi_flags))
2427 uptodate = 1;
2428 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002429 /*
2430 * The generic bio_readpage_error handles errors the
2431 * following way: If possible, new read requests are
2432 * created and submitted and will end up in
2433 * end_bio_extent_readpage as well (if we're lucky, not
2434 * in the !uptodate case). In that case it returns 0 and
2435 * we just go on with the next page in our bio. If it
2436 * can't handle the error it will return -EIO and we
2437 * remain responsible for that page.
2438 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002439 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002440 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002441 uptodate =
2442 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002443 if (err)
2444 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002445 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002446 continue;
2447 }
2448 }
Chris Mason70dec802008-01-29 09:59:12 -05002449
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002450 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002451 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002452 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002453 }
Arne Jansen507903b2011-04-06 10:02:20 +00002454 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002455
Chris Mason70dec802008-01-29 09:59:12 -05002456 if (whole_page) {
2457 if (uptodate) {
2458 SetPageUptodate(page);
2459 } else {
2460 ClearPageUptodate(page);
2461 SetPageError(page);
2462 }
Chris Masond1310b22008-01-24 16:13:08 -05002463 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002464 } else {
2465 if (uptodate) {
2466 check_page_uptodate(tree, page);
2467 } else {
2468 ClearPageUptodate(page);
2469 SetPageError(page);
2470 }
Chris Masond1310b22008-01-24 16:13:08 -05002471 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002472 }
Chris Mason4125bf72010-02-03 18:18:45 +00002473 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002474
2475 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002476}
2477
Miao Xie88f794e2010-11-22 03:02:55 +00002478struct bio *
2479btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2480 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002481{
2482 struct bio *bio;
2483
2484 bio = bio_alloc(gfp_flags, nr_vecs);
2485
2486 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2487 while (!bio && (nr_vecs /= 2))
2488 bio = bio_alloc(gfp_flags, nr_vecs);
2489 }
2490
2491 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002492 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002493 bio->bi_bdev = bdev;
2494 bio->bi_sector = first_sector;
2495 }
2496 return bio;
2497}
2498
Jeff Mahoney355808c2011-10-03 23:23:14 -04002499static int __must_check submit_one_bio(int rw, struct bio *bio,
2500 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002501{
Chris Masond1310b22008-01-24 16:13:08 -05002502 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002503 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2504 struct page *page = bvec->bv_page;
2505 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002506 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002507
Miao Xie4eee4fa2012-12-21 09:17:45 +00002508 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002509
David Woodhouse902b22f2008-08-20 08:51:49 -04002510 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002511
2512 bio_get(bio);
2513
Chris Mason065631f2008-02-20 12:07:25 -05002514 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002515 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002516 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002517 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002518 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002519
Chris Masond1310b22008-01-24 16:13:08 -05002520 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2521 ret = -EOPNOTSUPP;
2522 bio_put(bio);
2523 return ret;
2524}
2525
David Woodhouse64a16702009-07-15 23:29:37 +01002526static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002527 unsigned long offset, size_t size, struct bio *bio,
2528 unsigned long bio_flags)
2529{
2530 int ret = 0;
2531 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002532 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002533 bio_flags);
2534 BUG_ON(ret < 0);
2535 return ret;
2536
2537}
2538
Chris Masond1310b22008-01-24 16:13:08 -05002539static int submit_extent_page(int rw, struct extent_io_tree *tree,
2540 struct page *page, sector_t sector,
2541 size_t size, unsigned long offset,
2542 struct block_device *bdev,
2543 struct bio **bio_ret,
2544 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002545 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002546 int mirror_num,
2547 unsigned long prev_bio_flags,
2548 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002549{
2550 int ret = 0;
2551 struct bio *bio;
2552 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002553 int contig = 0;
2554 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2555 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002556 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002557
2558 if (bio_ret && *bio_ret) {
2559 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002560 if (old_compressed)
2561 contig = bio->bi_sector == sector;
2562 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002563 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002564
2565 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002566 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002567 bio_add_page(bio, page, page_size, offset) < page_size) {
2568 ret = submit_one_bio(rw, bio, mirror_num,
2569 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002570 if (ret < 0)
2571 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002572 bio = NULL;
2573 } else {
2574 return 0;
2575 }
2576 }
Chris Masonc8b97812008-10-29 14:49:59 -04002577 if (this_compressed)
2578 nr = BIO_MAX_PAGES;
2579 else
2580 nr = bio_get_nr_vecs(bdev);
2581
Miao Xie88f794e2010-11-22 03:02:55 +00002582 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002583 if (!bio)
2584 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002585
Chris Masonc8b97812008-10-29 14:49:59 -04002586 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002587 bio->bi_end_io = end_io_func;
2588 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002589
Chris Masond3977122009-01-05 21:25:51 -05002590 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002591 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002592 else
Chris Masonc8b97812008-10-29 14:49:59 -04002593 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002594
2595 return ret;
2596}
2597
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002598void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2599{
2600 if (!PagePrivate(page)) {
2601 SetPagePrivate(page);
2602 page_cache_get(page);
2603 set_page_private(page, (unsigned long)eb);
2604 } else {
2605 WARN_ON(page->private != (unsigned long)eb);
2606 }
2607}
2608
Chris Masond1310b22008-01-24 16:13:08 -05002609void set_page_extent_mapped(struct page *page)
2610{
2611 if (!PagePrivate(page)) {
2612 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002613 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002614 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002615 }
2616}
2617
Chris Masond1310b22008-01-24 16:13:08 -05002618/*
2619 * basic readpage implementation. Locked extent state structs are inserted
2620 * into the tree that are removed when the IO is done (by the end_io
2621 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002622 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002623 */
2624static int __extent_read_full_page(struct extent_io_tree *tree,
2625 struct page *page,
2626 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002627 struct bio **bio, int mirror_num,
2628 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002629{
2630 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002631 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002632 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2633 u64 end;
2634 u64 cur = start;
2635 u64 extent_offset;
2636 u64 last_byte = i_size_read(inode);
2637 u64 block_start;
2638 u64 cur_end;
2639 sector_t sector;
2640 struct extent_map *em;
2641 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002642 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002643 int ret;
2644 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002645 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002646 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002647 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002648 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002649 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002650
2651 set_page_extent_mapped(page);
2652
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002653 if (!PageUptodate(page)) {
2654 if (cleancache_get_page(page) == 0) {
2655 BUG_ON(blocksize != PAGE_SIZE);
2656 goto out;
2657 }
2658 }
2659
Chris Masond1310b22008-01-24 16:13:08 -05002660 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002661 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002662 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002663 ordered = btrfs_lookup_ordered_extent(inode, start);
2664 if (!ordered)
2665 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002666 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002667 btrfs_start_ordered_extent(inode, ordered, 1);
2668 btrfs_put_ordered_extent(ordered);
2669 }
Chris Masond1310b22008-01-24 16:13:08 -05002670
Chris Masonc8b97812008-10-29 14:49:59 -04002671 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2672 char *userpage;
2673 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2674
2675 if (zero_offset) {
2676 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002677 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002678 memset(userpage + zero_offset, 0, iosize);
2679 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002680 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002681 }
2682 }
Chris Masond1310b22008-01-24 16:13:08 -05002683 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002684 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2685
Chris Masond1310b22008-01-24 16:13:08 -05002686 if (cur >= last_byte) {
2687 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002688 struct extent_state *cached = NULL;
2689
David Sterba306e16c2011-04-19 14:29:38 +02002690 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002691 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002692 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002693 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002694 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002695 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002696 &cached, GFP_NOFS);
2697 unlock_extent_cached(tree, cur, cur + iosize - 1,
2698 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002699 break;
2700 }
David Sterba306e16c2011-04-19 14:29:38 +02002701 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002702 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002703 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002704 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002705 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002706 break;
2707 }
Chris Masond1310b22008-01-24 16:13:08 -05002708 extent_offset = cur - em->start;
2709 BUG_ON(extent_map_end(em) <= cur);
2710 BUG_ON(end < cur);
2711
Li Zefan261507a02010-12-17 14:21:50 +08002712 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002713 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002714 extent_set_compress_type(&this_bio_flag,
2715 em->compress_type);
2716 }
Chris Masonc8b97812008-10-29 14:49:59 -04002717
Chris Masond1310b22008-01-24 16:13:08 -05002718 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2719 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002720 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002721 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2722 disk_io_size = em->block_len;
2723 sector = em->block_start >> 9;
2724 } else {
2725 sector = (em->block_start + extent_offset) >> 9;
2726 disk_io_size = iosize;
2727 }
Chris Masond1310b22008-01-24 16:13:08 -05002728 bdev = em->bdev;
2729 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002730 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2731 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002732 free_extent_map(em);
2733 em = NULL;
2734
2735 /* we've found a hole, just zero and go on */
2736 if (block_start == EXTENT_MAP_HOLE) {
2737 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002738 struct extent_state *cached = NULL;
2739
Cong Wang7ac687d2011-11-25 23:14:28 +08002740 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002741 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002742 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002743 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002744
2745 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002746 &cached, GFP_NOFS);
2747 unlock_extent_cached(tree, cur, cur + iosize - 1,
2748 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002749 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002750 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002751 continue;
2752 }
2753 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002754 if (test_range_bit(tree, cur, cur_end,
2755 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002756 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002757 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002758 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002759 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002760 continue;
2761 }
Chris Mason70dec802008-01-29 09:59:12 -05002762 /* we have an inline extent but it didn't get marked up
2763 * to date. Error out
2764 */
2765 if (block_start == EXTENT_MAP_INLINE) {
2766 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002767 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002768 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002769 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002770 continue;
2771 }
Chris Masond1310b22008-01-24 16:13:08 -05002772
Josef Bacikc8f2f242013-02-11 11:33:00 -05002773 pnr -= page->index;
2774 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002775 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002776 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002777 end_bio_extent_readpage, mirror_num,
2778 *bio_flags,
2779 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002780 if (!ret) {
2781 nr++;
2782 *bio_flags = this_bio_flag;
2783 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002784 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002785 unlock_extent(tree, cur, cur + iosize - 1);
2786 }
Chris Masond1310b22008-01-24 16:13:08 -05002787 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002788 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002789 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002790out:
Chris Masond1310b22008-01-24 16:13:08 -05002791 if (!nr) {
2792 if (!PageError(page))
2793 SetPageUptodate(page);
2794 unlock_page(page);
2795 }
2796 return 0;
2797}
2798
2799int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002800 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002801{
2802 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002803 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002804 int ret;
2805
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002806 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002807 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002808 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002809 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002810 return ret;
2811}
Chris Masond1310b22008-01-24 16:13:08 -05002812
Chris Mason11c83492009-04-20 15:50:09 -04002813static noinline void update_nr_written(struct page *page,
2814 struct writeback_control *wbc,
2815 unsigned long nr_written)
2816{
2817 wbc->nr_to_write -= nr_written;
2818 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2819 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2820 page->mapping->writeback_index = page->index + nr_written;
2821}
2822
Chris Masond1310b22008-01-24 16:13:08 -05002823/*
2824 * the writepage semantics are similar to regular writepage. extent
2825 * records are inserted to lock ranges in the tree, and as dirty areas
2826 * are found, they are marked writeback. Then the lock bits are removed
2827 * and the end_io handler clears the writeback ranges
2828 */
2829static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2830 void *data)
2831{
2832 struct inode *inode = page->mapping->host;
2833 struct extent_page_data *epd = data;
2834 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002835 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002836 u64 delalloc_start;
2837 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2838 u64 end;
2839 u64 cur = start;
2840 u64 extent_offset;
2841 u64 last_byte = i_size_read(inode);
2842 u64 block_start;
2843 u64 iosize;
2844 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002845 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002846 struct extent_map *em;
2847 struct block_device *bdev;
2848 int ret;
2849 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002850 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002851 size_t blocksize;
2852 loff_t i_size = i_size_read(inode);
2853 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2854 u64 nr_delalloc;
2855 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002856 int page_started;
2857 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002858 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002859 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002860 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002861
Chris Masonffbd5172009-04-20 15:50:09 -04002862 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002863 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002864 else
2865 write_flags = WRITE;
2866
liubo1abe9b82011-03-24 11:18:59 +00002867 trace___extent_writepage(page, inode, wbc);
2868
Chris Masond1310b22008-01-24 16:13:08 -05002869 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002870
2871 ClearPageError(page);
2872
Chris Mason7f3c74f2008-07-18 12:01:11 -04002873 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002874 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002875 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002876 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002877 unlock_page(page);
2878 return 0;
2879 }
2880
2881 if (page->index == end_index) {
2882 char *userpage;
2883
Cong Wang7ac687d2011-11-25 23:14:28 +08002884 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002885 memset(userpage + pg_offset, 0,
2886 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002887 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002888 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002889 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002890 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002891
2892 set_page_extent_mapped(page);
2893
Josef Bacik9e487102011-08-01 12:08:18 -04002894 if (!tree->ops || !tree->ops->fill_delalloc)
2895 fill_delalloc = false;
2896
Chris Masond1310b22008-01-24 16:13:08 -05002897 delalloc_start = start;
2898 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002899 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002900 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002901 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002902 /*
2903 * make sure the wbc mapping index is at least updated
2904 * to this page.
2905 */
2906 update_nr_written(page, wbc, 0);
2907
Chris Masond3977122009-01-05 21:25:51 -05002908 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002909 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002910 page,
2911 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002912 &delalloc_end,
2913 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002914 if (nr_delalloc == 0) {
2915 delalloc_start = delalloc_end + 1;
2916 continue;
2917 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002918 ret = tree->ops->fill_delalloc(inode, page,
2919 delalloc_start,
2920 delalloc_end,
2921 &page_started,
2922 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002923 /* File system has been set read-only */
2924 if (ret) {
2925 SetPageError(page);
2926 goto done;
2927 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002928 /*
2929 * delalloc_end is already one less than the total
2930 * length, so we don't subtract one from
2931 * PAGE_CACHE_SIZE
2932 */
2933 delalloc_to_write += (delalloc_end - delalloc_start +
2934 PAGE_CACHE_SIZE) >>
2935 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002936 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002937 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002938 if (wbc->nr_to_write < delalloc_to_write) {
2939 int thresh = 8192;
2940
2941 if (delalloc_to_write < thresh * 2)
2942 thresh = delalloc_to_write;
2943 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2944 thresh);
2945 }
Chris Masonc8b97812008-10-29 14:49:59 -04002946
Chris Mason771ed682008-11-06 22:02:51 -05002947 /* did the fill delalloc function already unlock and start
2948 * the IO?
2949 */
2950 if (page_started) {
2951 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002952 /*
2953 * we've unlocked the page, so we can't update
2954 * the mapping's writeback index, just update
2955 * nr_to_write.
2956 */
2957 wbc->nr_to_write -= nr_written;
2958 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002959 }
Chris Masonc8b97812008-10-29 14:49:59 -04002960 }
Chris Mason247e7432008-07-17 12:53:51 -04002961 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002962 ret = tree->ops->writepage_start_hook(page, start,
2963 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002964 if (ret) {
2965 /* Fixup worker will requeue */
2966 if (ret == -EBUSY)
2967 wbc->pages_skipped++;
2968 else
2969 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002970 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002971 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002972 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002973 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002974 }
2975 }
2976
Chris Mason11c83492009-04-20 15:50:09 -04002977 /*
2978 * we don't want to touch the inode after unlocking the page,
2979 * so we update the mapping writeback index now
2980 */
2981 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002982
Chris Masond1310b22008-01-24 16:13:08 -05002983 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002984 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002985 if (tree->ops && tree->ops->writepage_end_io_hook)
2986 tree->ops->writepage_end_io_hook(page, start,
2987 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002988 goto done;
2989 }
2990
Chris Masond1310b22008-01-24 16:13:08 -05002991 blocksize = inode->i_sb->s_blocksize;
2992
2993 while (cur <= end) {
2994 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002995 if (tree->ops && tree->ops->writepage_end_io_hook)
2996 tree->ops->writepage_end_io_hook(page, cur,
2997 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002998 break;
2999 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003000 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003001 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003002 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003003 SetPageError(page);
3004 break;
3005 }
3006
3007 extent_offset = cur - em->start;
3008 BUG_ON(extent_map_end(em) <= cur);
3009 BUG_ON(end < cur);
3010 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003011 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003012 sector = (em->block_start + extent_offset) >> 9;
3013 bdev = em->bdev;
3014 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003015 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003016 free_extent_map(em);
3017 em = NULL;
3018
Chris Masonc8b97812008-10-29 14:49:59 -04003019 /*
3020 * compressed and inline extents are written through other
3021 * paths in the FS
3022 */
3023 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003024 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003025 /*
3026 * end_io notification does not happen here for
3027 * compressed extents
3028 */
3029 if (!compressed && tree->ops &&
3030 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003031 tree->ops->writepage_end_io_hook(page, cur,
3032 cur + iosize - 1,
3033 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003034 else if (compressed) {
3035 /* we don't want to end_page_writeback on
3036 * a compressed extent. this happens
3037 * elsewhere
3038 */
3039 nr++;
3040 }
3041
3042 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003043 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003044 continue;
3045 }
Chris Masond1310b22008-01-24 16:13:08 -05003046 /* leave this out until we have a page_mkwrite call */
3047 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003048 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003049 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003050 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003051 continue;
3052 }
Chris Masonc8b97812008-10-29 14:49:59 -04003053
Chris Masond1310b22008-01-24 16:13:08 -05003054 if (tree->ops && tree->ops->writepage_io_hook) {
3055 ret = tree->ops->writepage_io_hook(page, cur,
3056 cur + iosize - 1);
3057 } else {
3058 ret = 0;
3059 }
Chris Mason1259ab72008-05-12 13:39:03 -04003060 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003061 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003062 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003063 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003064
Chris Masond1310b22008-01-24 16:13:08 -05003065 set_range_writeback(tree, cur, cur + iosize - 1);
3066 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003067 printk(KERN_ERR "btrfs warning page %lu not "
3068 "writeback, cur %llu end %llu\n",
3069 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003070 (unsigned long long)end);
3071 }
3072
Chris Masonffbd5172009-04-20 15:50:09 -04003073 ret = submit_extent_page(write_flags, tree, page,
3074 sector, iosize, pg_offset,
3075 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003076 end_bio_extent_writepage,
3077 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003078 if (ret)
3079 SetPageError(page);
3080 }
3081 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003082 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003083 nr++;
3084 }
3085done:
3086 if (nr == 0) {
3087 /* make sure the mapping tag for page dirty gets cleared */
3088 set_page_writeback(page);
3089 end_page_writeback(page);
3090 }
Chris Masond1310b22008-01-24 16:13:08 -05003091 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003092
Chris Mason11c83492009-04-20 15:50:09 -04003093done_unlocked:
3094
Chris Mason2c64c532009-09-02 15:04:12 -04003095 /* drop our reference on any cached states */
3096 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003097 return 0;
3098}
3099
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003100static int eb_wait(void *word)
3101{
3102 io_schedule();
3103 return 0;
3104}
3105
3106static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3107{
3108 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3109 TASK_UNINTERRUPTIBLE);
3110}
3111
3112static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3113 struct btrfs_fs_info *fs_info,
3114 struct extent_page_data *epd)
3115{
3116 unsigned long i, num_pages;
3117 int flush = 0;
3118 int ret = 0;
3119
3120 if (!btrfs_try_tree_write_lock(eb)) {
3121 flush = 1;
3122 flush_write_bio(epd);
3123 btrfs_tree_lock(eb);
3124 }
3125
3126 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3127 btrfs_tree_unlock(eb);
3128 if (!epd->sync_io)
3129 return 0;
3130 if (!flush) {
3131 flush_write_bio(epd);
3132 flush = 1;
3133 }
Chris Masona098d8e2012-03-21 12:09:56 -04003134 while (1) {
3135 wait_on_extent_buffer_writeback(eb);
3136 btrfs_tree_lock(eb);
3137 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3138 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003139 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003140 }
3141 }
3142
Josef Bacik51561ff2012-07-20 16:25:24 -04003143 /*
3144 * We need to do this to prevent races in people who check if the eb is
3145 * under IO since we can end up having no IO bits set for a short period
3146 * of time.
3147 */
3148 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003149 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3150 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003151 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003152 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003153 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3154 -eb->len,
3155 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003156 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003157 } else {
3158 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003159 }
3160
3161 btrfs_tree_unlock(eb);
3162
3163 if (!ret)
3164 return ret;
3165
3166 num_pages = num_extent_pages(eb->start, eb->len);
3167 for (i = 0; i < num_pages; i++) {
3168 struct page *p = extent_buffer_page(eb, i);
3169
3170 if (!trylock_page(p)) {
3171 if (!flush) {
3172 flush_write_bio(epd);
3173 flush = 1;
3174 }
3175 lock_page(p);
3176 }
3177 }
3178
3179 return ret;
3180}
3181
3182static void end_extent_buffer_writeback(struct extent_buffer *eb)
3183{
3184 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3185 smp_mb__after_clear_bit();
3186 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3187}
3188
3189static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3190{
3191 int uptodate = err == 0;
3192 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3193 struct extent_buffer *eb;
3194 int done;
3195
3196 do {
3197 struct page *page = bvec->bv_page;
3198
3199 bvec--;
3200 eb = (struct extent_buffer *)page->private;
3201 BUG_ON(!eb);
3202 done = atomic_dec_and_test(&eb->io_pages);
3203
3204 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3205 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3206 ClearPageUptodate(page);
3207 SetPageError(page);
3208 }
3209
3210 end_page_writeback(page);
3211
3212 if (!done)
3213 continue;
3214
3215 end_extent_buffer_writeback(eb);
3216 } while (bvec >= bio->bi_io_vec);
3217
3218 bio_put(bio);
3219
3220}
3221
3222static int write_one_eb(struct extent_buffer *eb,
3223 struct btrfs_fs_info *fs_info,
3224 struct writeback_control *wbc,
3225 struct extent_page_data *epd)
3226{
3227 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3228 u64 offset = eb->start;
3229 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003230 unsigned long bio_flags = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003231 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003232 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003233
3234 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3235 num_pages = num_extent_pages(eb->start, eb->len);
3236 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003237 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3238 bio_flags = EXTENT_BIO_TREE_LOG;
3239
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003240 for (i = 0; i < num_pages; i++) {
3241 struct page *p = extent_buffer_page(eb, i);
3242
3243 clear_page_dirty_for_io(p);
3244 set_page_writeback(p);
3245 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3246 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3247 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003248 0, epd->bio_flags, bio_flags);
3249 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003250 if (ret) {
3251 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3252 SetPageError(p);
3253 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3254 end_extent_buffer_writeback(eb);
3255 ret = -EIO;
3256 break;
3257 }
3258 offset += PAGE_CACHE_SIZE;
3259 update_nr_written(p, wbc, 1);
3260 unlock_page(p);
3261 }
3262
3263 if (unlikely(ret)) {
3264 for (; i < num_pages; i++) {
3265 struct page *p = extent_buffer_page(eb, i);
3266 unlock_page(p);
3267 }
3268 }
3269
3270 return ret;
3271}
3272
3273int btree_write_cache_pages(struct address_space *mapping,
3274 struct writeback_control *wbc)
3275{
3276 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3277 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3278 struct extent_buffer *eb, *prev_eb = NULL;
3279 struct extent_page_data epd = {
3280 .bio = NULL,
3281 .tree = tree,
3282 .extent_locked = 0,
3283 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003284 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003285 };
3286 int ret = 0;
3287 int done = 0;
3288 int nr_to_write_done = 0;
3289 struct pagevec pvec;
3290 int nr_pages;
3291 pgoff_t index;
3292 pgoff_t end; /* Inclusive */
3293 int scanned = 0;
3294 int tag;
3295
3296 pagevec_init(&pvec, 0);
3297 if (wbc->range_cyclic) {
3298 index = mapping->writeback_index; /* Start from prev offset */
3299 end = -1;
3300 } else {
3301 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3302 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3303 scanned = 1;
3304 }
3305 if (wbc->sync_mode == WB_SYNC_ALL)
3306 tag = PAGECACHE_TAG_TOWRITE;
3307 else
3308 tag = PAGECACHE_TAG_DIRTY;
3309retry:
3310 if (wbc->sync_mode == WB_SYNC_ALL)
3311 tag_pages_for_writeback(mapping, index, end);
3312 while (!done && !nr_to_write_done && (index <= end) &&
3313 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3314 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3315 unsigned i;
3316
3317 scanned = 1;
3318 for (i = 0; i < nr_pages; i++) {
3319 struct page *page = pvec.pages[i];
3320
3321 if (!PagePrivate(page))
3322 continue;
3323
3324 if (!wbc->range_cyclic && page->index > end) {
3325 done = 1;
3326 break;
3327 }
3328
Josef Bacikb5bae262012-09-14 13:43:01 -04003329 spin_lock(&mapping->private_lock);
3330 if (!PagePrivate(page)) {
3331 spin_unlock(&mapping->private_lock);
3332 continue;
3333 }
3334
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003335 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003336
3337 /*
3338 * Shouldn't happen and normally this would be a BUG_ON
3339 * but no sense in crashing the users box for something
3340 * we can survive anyway.
3341 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003342 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003343 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003344 WARN_ON(1);
3345 continue;
3346 }
3347
Josef Bacikb5bae262012-09-14 13:43:01 -04003348 if (eb == prev_eb) {
3349 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003350 continue;
3351 }
3352
Josef Bacikb5bae262012-09-14 13:43:01 -04003353 ret = atomic_inc_not_zero(&eb->refs);
3354 spin_unlock(&mapping->private_lock);
3355 if (!ret)
3356 continue;
3357
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003358 prev_eb = eb;
3359 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3360 if (!ret) {
3361 free_extent_buffer(eb);
3362 continue;
3363 }
3364
3365 ret = write_one_eb(eb, fs_info, wbc, &epd);
3366 if (ret) {
3367 done = 1;
3368 free_extent_buffer(eb);
3369 break;
3370 }
3371 free_extent_buffer(eb);
3372
3373 /*
3374 * the filesystem may choose to bump up nr_to_write.
3375 * We have to make sure to honor the new nr_to_write
3376 * at any time
3377 */
3378 nr_to_write_done = wbc->nr_to_write <= 0;
3379 }
3380 pagevec_release(&pvec);
3381 cond_resched();
3382 }
3383 if (!scanned && !done) {
3384 /*
3385 * We hit the last page and there is more work to be done: wrap
3386 * back to the start of the file
3387 */
3388 scanned = 1;
3389 index = 0;
3390 goto retry;
3391 }
3392 flush_write_bio(&epd);
3393 return ret;
3394}
3395
Chris Masond1310b22008-01-24 16:13:08 -05003396/**
Chris Mason4bef0842008-09-08 11:18:08 -04003397 * 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 -05003398 * @mapping: address space structure to write
3399 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3400 * @writepage: function called for each page
3401 * @data: data passed to writepage function
3402 *
3403 * If a page is already under I/O, write_cache_pages() skips it, even
3404 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3405 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3406 * and msync() need to guarantee that all the data which was dirty at the time
3407 * the call was made get new I/O started against them. If wbc->sync_mode is
3408 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3409 * existing IO to complete.
3410 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003411static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003412 struct address_space *mapping,
3413 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003414 writepage_t writepage, void *data,
3415 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003416{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003417 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003418 int ret = 0;
3419 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003420 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003421 struct pagevec pvec;
3422 int nr_pages;
3423 pgoff_t index;
3424 pgoff_t end; /* Inclusive */
3425 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003426 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003427
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003428 /*
3429 * We have to hold onto the inode so that ordered extents can do their
3430 * work when the IO finishes. The alternative to this is failing to add
3431 * an ordered extent if the igrab() fails there and that is a huge pain
3432 * to deal with, so instead just hold onto the inode throughout the
3433 * writepages operation. If it fails here we are freeing up the inode
3434 * anyway and we'd rather not waste our time writing out stuff that is
3435 * going to be truncated anyway.
3436 */
3437 if (!igrab(inode))
3438 return 0;
3439
Chris Masond1310b22008-01-24 16:13:08 -05003440 pagevec_init(&pvec, 0);
3441 if (wbc->range_cyclic) {
3442 index = mapping->writeback_index; /* Start from prev offset */
3443 end = -1;
3444 } else {
3445 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3446 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003447 scanned = 1;
3448 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003449 if (wbc->sync_mode == WB_SYNC_ALL)
3450 tag = PAGECACHE_TAG_TOWRITE;
3451 else
3452 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003453retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003454 if (wbc->sync_mode == WB_SYNC_ALL)
3455 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003456 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003457 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3458 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003459 unsigned i;
3460
3461 scanned = 1;
3462 for (i = 0; i < nr_pages; i++) {
3463 struct page *page = pvec.pages[i];
3464
3465 /*
3466 * At this point we hold neither mapping->tree_lock nor
3467 * lock on the page itself: the page may be truncated or
3468 * invalidated (changing page->mapping to NULL), or even
3469 * swizzled back from swapper_space to tmpfs file
3470 * mapping
3471 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003472 if (!trylock_page(page)) {
3473 flush_fn(data);
3474 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003475 }
Chris Masond1310b22008-01-24 16:13:08 -05003476
3477 if (unlikely(page->mapping != mapping)) {
3478 unlock_page(page);
3479 continue;
3480 }
3481
3482 if (!wbc->range_cyclic && page->index > end) {
3483 done = 1;
3484 unlock_page(page);
3485 continue;
3486 }
3487
Chris Masond2c3f4f2008-11-19 12:44:22 -05003488 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003489 if (PageWriteback(page))
3490 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003491 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003492 }
Chris Masond1310b22008-01-24 16:13:08 -05003493
3494 if (PageWriteback(page) ||
3495 !clear_page_dirty_for_io(page)) {
3496 unlock_page(page);
3497 continue;
3498 }
3499
3500 ret = (*writepage)(page, wbc, data);
3501
3502 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3503 unlock_page(page);
3504 ret = 0;
3505 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003506 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003507 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003508
3509 /*
3510 * the filesystem may choose to bump up nr_to_write.
3511 * We have to make sure to honor the new nr_to_write
3512 * at any time
3513 */
3514 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003515 }
3516 pagevec_release(&pvec);
3517 cond_resched();
3518 }
3519 if (!scanned && !done) {
3520 /*
3521 * We hit the last page and there is more work to be done: wrap
3522 * back to the start of the file
3523 */
3524 scanned = 1;
3525 index = 0;
3526 goto retry;
3527 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003528 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003529 return ret;
3530}
Chris Masond1310b22008-01-24 16:13:08 -05003531
Chris Masonffbd5172009-04-20 15:50:09 -04003532static void flush_epd_write_bio(struct extent_page_data *epd)
3533{
3534 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003535 int rw = WRITE;
3536 int ret;
3537
Chris Masonffbd5172009-04-20 15:50:09 -04003538 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003539 rw = WRITE_SYNC;
3540
Josef Bacikde0022b2012-09-25 14:25:58 -04003541 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003542 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003543 epd->bio = NULL;
3544 }
3545}
3546
Chris Masond2c3f4f2008-11-19 12:44:22 -05003547static noinline void flush_write_bio(void *data)
3548{
3549 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003550 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003551}
3552
Chris Masond1310b22008-01-24 16:13:08 -05003553int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3554 get_extent_t *get_extent,
3555 struct writeback_control *wbc)
3556{
3557 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003558 struct extent_page_data epd = {
3559 .bio = NULL,
3560 .tree = tree,
3561 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003562 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003563 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003564 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003565 };
Chris Masond1310b22008-01-24 16:13:08 -05003566
Chris Masond1310b22008-01-24 16:13:08 -05003567 ret = __extent_writepage(page, wbc, &epd);
3568
Chris Masonffbd5172009-04-20 15:50:09 -04003569 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003570 return ret;
3571}
Chris Masond1310b22008-01-24 16:13:08 -05003572
Chris Mason771ed682008-11-06 22:02:51 -05003573int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3574 u64 start, u64 end, get_extent_t *get_extent,
3575 int mode)
3576{
3577 int ret = 0;
3578 struct address_space *mapping = inode->i_mapping;
3579 struct page *page;
3580 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3581 PAGE_CACHE_SHIFT;
3582
3583 struct extent_page_data epd = {
3584 .bio = NULL,
3585 .tree = tree,
3586 .get_extent = get_extent,
3587 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003588 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003589 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003590 };
3591 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003592 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003593 .nr_to_write = nr_pages * 2,
3594 .range_start = start,
3595 .range_end = end + 1,
3596 };
3597
Chris Masond3977122009-01-05 21:25:51 -05003598 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003599 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3600 if (clear_page_dirty_for_io(page))
3601 ret = __extent_writepage(page, &wbc_writepages, &epd);
3602 else {
3603 if (tree->ops && tree->ops->writepage_end_io_hook)
3604 tree->ops->writepage_end_io_hook(page, start,
3605 start + PAGE_CACHE_SIZE - 1,
3606 NULL, 1);
3607 unlock_page(page);
3608 }
3609 page_cache_release(page);
3610 start += PAGE_CACHE_SIZE;
3611 }
3612
Chris Masonffbd5172009-04-20 15:50:09 -04003613 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003614 return ret;
3615}
Chris Masond1310b22008-01-24 16:13:08 -05003616
3617int extent_writepages(struct extent_io_tree *tree,
3618 struct address_space *mapping,
3619 get_extent_t *get_extent,
3620 struct writeback_control *wbc)
3621{
3622 int ret = 0;
3623 struct extent_page_data epd = {
3624 .bio = NULL,
3625 .tree = tree,
3626 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003627 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003628 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003629 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003630 };
3631
Chris Mason4bef0842008-09-08 11:18:08 -04003632 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003633 __extent_writepage, &epd,
3634 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003635 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003636 return ret;
3637}
Chris Masond1310b22008-01-24 16:13:08 -05003638
3639int extent_readpages(struct extent_io_tree *tree,
3640 struct address_space *mapping,
3641 struct list_head *pages, unsigned nr_pages,
3642 get_extent_t get_extent)
3643{
3644 struct bio *bio = NULL;
3645 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003646 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003647 struct page *pagepool[16];
3648 struct page *page;
3649 int i = 0;
3650 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003651
Chris Masond1310b22008-01-24 16:13:08 -05003652 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003653 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003654
3655 prefetchw(&page->flags);
3656 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003657 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003658 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003659 page_cache_release(page);
3660 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003661 }
Liu Bo67c96842012-07-20 21:43:09 -06003662
3663 pagepool[nr++] = page;
3664 if (nr < ARRAY_SIZE(pagepool))
3665 continue;
3666 for (i = 0; i < nr; i++) {
3667 __extent_read_full_page(tree, pagepool[i], get_extent,
3668 &bio, 0, &bio_flags);
3669 page_cache_release(pagepool[i]);
3670 }
3671 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003672 }
Liu Bo67c96842012-07-20 21:43:09 -06003673 for (i = 0; i < nr; i++) {
3674 __extent_read_full_page(tree, pagepool[i], get_extent,
3675 &bio, 0, &bio_flags);
3676 page_cache_release(pagepool[i]);
3677 }
3678
Chris Masond1310b22008-01-24 16:13:08 -05003679 BUG_ON(!list_empty(pages));
3680 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003682 return 0;
3683}
Chris Masond1310b22008-01-24 16:13:08 -05003684
3685/*
3686 * basic invalidatepage code, this waits on any locked or writeback
3687 * ranges corresponding to the page, and then deletes any extent state
3688 * records from the tree
3689 */
3690int extent_invalidatepage(struct extent_io_tree *tree,
3691 struct page *page, unsigned long offset)
3692{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003693 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003694 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003695 u64 end = start + PAGE_CACHE_SIZE - 1;
3696 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3697
Qu Wenruofda28322013-02-26 08:10:22 +00003698 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003699 if (start > end)
3700 return 0;
3701
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003702 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003703 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003704 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003705 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3706 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003707 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003708 return 0;
3709}
Chris Masond1310b22008-01-24 16:13:08 -05003710
3711/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003712 * a helper for releasepage, this tests for areas of the page that
3713 * are locked or under IO and drops the related state bits if it is safe
3714 * to drop the page.
3715 */
3716int try_release_extent_state(struct extent_map_tree *map,
3717 struct extent_io_tree *tree, struct page *page,
3718 gfp_t mask)
3719{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003720 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003721 u64 end = start + PAGE_CACHE_SIZE - 1;
3722 int ret = 1;
3723
Chris Mason211f90e2008-07-18 11:56:15 -04003724 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003725 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003726 ret = 0;
3727 else {
3728 if ((mask & GFP_NOFS) == GFP_NOFS)
3729 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003730 /*
3731 * at this point we can safely clear everything except the
3732 * locked bit and the nodatasum bit
3733 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003734 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003735 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3736 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003737
3738 /* if clear_extent_bit failed for enomem reasons,
3739 * we can't allow the release to continue.
3740 */
3741 if (ret < 0)
3742 ret = 0;
3743 else
3744 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003745 }
3746 return ret;
3747}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003748
3749/*
Chris Masond1310b22008-01-24 16:13:08 -05003750 * a helper for releasepage. As long as there are no locked extents
3751 * in the range corresponding to the page, both state records and extent
3752 * map records are removed
3753 */
3754int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003755 struct extent_io_tree *tree, struct page *page,
3756 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003757{
3758 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003759 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003760 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003761
Chris Mason70dec802008-01-29 09:59:12 -05003762 if ((mask & __GFP_WAIT) &&
3763 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003764 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003765 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003766 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003767 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003768 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003769 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003770 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003771 break;
3772 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003773 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3774 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003775 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003776 free_extent_map(em);
3777 break;
3778 }
3779 if (!test_range_bit(tree, em->start,
3780 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003781 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003782 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003783 remove_extent_mapping(map, em);
3784 /* once for the rb tree */
3785 free_extent_map(em);
3786 }
3787 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003788 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003789
3790 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003791 free_extent_map(em);
3792 }
Chris Masond1310b22008-01-24 16:13:08 -05003793 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003794 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003795}
Chris Masond1310b22008-01-24 16:13:08 -05003796
Chris Masonec29ed52011-02-23 16:23:20 -05003797/*
3798 * helper function for fiemap, which doesn't want to see any holes.
3799 * This maps until we find something past 'last'
3800 */
3801static struct extent_map *get_extent_skip_holes(struct inode *inode,
3802 u64 offset,
3803 u64 last,
3804 get_extent_t *get_extent)
3805{
3806 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3807 struct extent_map *em;
3808 u64 len;
3809
3810 if (offset >= last)
3811 return NULL;
3812
3813 while(1) {
3814 len = last - offset;
3815 if (len == 0)
3816 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003817 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003818 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003819 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003820 return em;
3821
3822 /* if this isn't a hole return it */
3823 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3824 em->block_start != EXTENT_MAP_HOLE) {
3825 return em;
3826 }
3827
3828 /* this is a hole, advance to the next extent */
3829 offset = extent_map_end(em);
3830 free_extent_map(em);
3831 if (offset >= last)
3832 break;
3833 }
3834 return NULL;
3835}
3836
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003837int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3838 __u64 start, __u64 len, get_extent_t *get_extent)
3839{
Josef Bacik975f84f2010-11-23 19:36:57 +00003840 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003841 u64 off = start;
3842 u64 max = start + len;
3843 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003844 u32 found_type;
3845 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003846 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003847 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003848 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003849 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003850 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003851 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003852 struct btrfs_path *path;
3853 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003854 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003855 u64 em_start = 0;
3856 u64 em_len = 0;
3857 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003858 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003859
3860 if (len == 0)
3861 return -EINVAL;
3862
Josef Bacik975f84f2010-11-23 19:36:57 +00003863 path = btrfs_alloc_path();
3864 if (!path)
3865 return -ENOMEM;
3866 path->leave_spinning = 1;
3867
Josef Bacik4d479cf2011-11-17 11:34:31 -05003868 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3869 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3870
Chris Masonec29ed52011-02-23 16:23:20 -05003871 /*
3872 * lookup the last file extent. We're not using i_size here
3873 * because there might be preallocation past i_size
3874 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003875 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003876 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003877 if (ret < 0) {
3878 btrfs_free_path(path);
3879 return ret;
3880 }
3881 WARN_ON(!ret);
3882 path->slots[0]--;
3883 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3884 struct btrfs_file_extent_item);
3885 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3886 found_type = btrfs_key_type(&found_key);
3887
Chris Masonec29ed52011-02-23 16:23:20 -05003888 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003889 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003890 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003891 /* have to trust i_size as the end */
3892 last = (u64)-1;
3893 last_for_get_extent = isize;
3894 } else {
3895 /*
3896 * remember the start of the last extent. There are a
3897 * bunch of different factors that go into the length of the
3898 * extent, so its much less complex to remember where it started
3899 */
3900 last = found_key.offset;
3901 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003902 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003903 btrfs_free_path(path);
3904
Chris Masonec29ed52011-02-23 16:23:20 -05003905 /*
3906 * we might have some extents allocated but more delalloc past those
3907 * extents. so, we trust isize unless the start of the last extent is
3908 * beyond isize
3909 */
3910 if (last < isize) {
3911 last = (u64)-1;
3912 last_for_get_extent = isize;
3913 }
3914
Josef Bacik2ac55d42010-02-03 19:33:23 +00003915 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003916 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003917
Josef Bacik4d479cf2011-11-17 11:34:31 -05003918 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003919 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003920 if (!em)
3921 goto out;
3922 if (IS_ERR(em)) {
3923 ret = PTR_ERR(em);
3924 goto out;
3925 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003926
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003927 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003928 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003929
Chris Masonea8efc72011-03-08 11:54:40 -05003930 /* break if the extent we found is outside the range */
3931 if (em->start >= max || extent_map_end(em) < off)
3932 break;
3933
3934 /*
3935 * get_extent may return an extent that starts before our
3936 * requested range. We have to make sure the ranges
3937 * we return to fiemap always move forward and don't
3938 * overlap, so adjust the offsets here
3939 */
3940 em_start = max(em->start, off);
3941
3942 /*
3943 * record the offset from the start of the extent
3944 * for adjusting the disk offset below
3945 */
3946 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003947 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003948 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003949 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003950 disko = 0;
3951 flags = 0;
3952
Chris Masonea8efc72011-03-08 11:54:40 -05003953 /*
3954 * bump off for our next call to get_extent
3955 */
3956 off = extent_map_end(em);
3957 if (off >= max)
3958 end = 1;
3959
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003960 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003961 end = 1;
3962 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003963 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003964 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3965 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003966 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003967 flags |= (FIEMAP_EXTENT_DELALLOC |
3968 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003969 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003970 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003971 }
3972 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3973 flags |= FIEMAP_EXTENT_ENCODED;
3974
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003975 free_extent_map(em);
3976 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003977 if ((em_start >= last) || em_len == (u64)-1 ||
3978 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003979 flags |= FIEMAP_EXTENT_LAST;
3980 end = 1;
3981 }
3982
Chris Masonec29ed52011-02-23 16:23:20 -05003983 /* now scan forward to see if this is really the last extent. */
3984 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3985 get_extent);
3986 if (IS_ERR(em)) {
3987 ret = PTR_ERR(em);
3988 goto out;
3989 }
3990 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003991 flags |= FIEMAP_EXTENT_LAST;
3992 end = 1;
3993 }
Chris Masonec29ed52011-02-23 16:23:20 -05003994 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3995 em_len, flags);
3996 if (ret)
3997 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003998 }
3999out_free:
4000 free_extent_map(em);
4001out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00004002 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
4003 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004004 return ret;
4005}
4006
Chris Mason727011e2010-08-06 13:21:20 -04004007static void __free_extent_buffer(struct extent_buffer *eb)
4008{
4009#if LEAK_DEBUG
4010 unsigned long flags;
4011 spin_lock_irqsave(&leak_lock, flags);
4012 list_del(&eb->leak_list);
4013 spin_unlock_irqrestore(&leak_lock, flags);
4014#endif
Chris Mason727011e2010-08-06 13:21:20 -04004015 kmem_cache_free(extent_buffer_cache, eb);
4016}
4017
Chris Masond1310b22008-01-24 16:13:08 -05004018static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4019 u64 start,
4020 unsigned long len,
4021 gfp_t mask)
4022{
4023 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05004024#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004025 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04004026#endif
Chris Masond1310b22008-01-24 16:13:08 -05004027
Chris Masond1310b22008-01-24 16:13:08 -05004028 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004029 if (eb == NULL)
4030 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004031 eb->start = start;
4032 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004033 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004034 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004035 rwlock_init(&eb->lock);
4036 atomic_set(&eb->write_locks, 0);
4037 atomic_set(&eb->read_locks, 0);
4038 atomic_set(&eb->blocking_readers, 0);
4039 atomic_set(&eb->blocking_writers, 0);
4040 atomic_set(&eb->spinning_readers, 0);
4041 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004042 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004043 init_waitqueue_head(&eb->write_lock_wq);
4044 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004045
Chris Mason39351272009-02-04 09:24:05 -05004046#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004047 spin_lock_irqsave(&leak_lock, flags);
4048 list_add(&eb->leak_list, &buffers);
4049 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04004050#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05004051 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004052 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004053 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004054
David Sterbab8dae312013-02-28 14:54:18 +00004055 /*
4056 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4057 */
4058 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4059 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4060 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004061
4062 return eb;
4063}
4064
Jan Schmidt815a51c2012-05-16 17:00:02 +02004065struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4066{
4067 unsigned long i;
4068 struct page *p;
4069 struct extent_buffer *new;
4070 unsigned long num_pages = num_extent_pages(src->start, src->len);
4071
4072 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4073 if (new == NULL)
4074 return NULL;
4075
4076 for (i = 0; i < num_pages; i++) {
4077 p = alloc_page(GFP_ATOMIC);
4078 BUG_ON(!p);
4079 attach_extent_buffer_page(new, p);
4080 WARN_ON(PageDirty(p));
4081 SetPageUptodate(p);
4082 new->pages[i] = p;
4083 }
4084
4085 copy_extent_buffer(new, src, 0, 0, src->len);
4086 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4087 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4088
4089 return new;
4090}
4091
4092struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4093{
4094 struct extent_buffer *eb;
4095 unsigned long num_pages = num_extent_pages(0, len);
4096 unsigned long i;
4097
4098 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4099 if (!eb)
4100 return NULL;
4101
4102 for (i = 0; i < num_pages; i++) {
4103 eb->pages[i] = alloc_page(GFP_ATOMIC);
4104 if (!eb->pages[i])
4105 goto err;
4106 }
4107 set_extent_buffer_uptodate(eb);
4108 btrfs_set_header_nritems(eb, 0);
4109 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4110
4111 return eb;
4112err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004113 for (; i > 0; i--)
4114 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004115 __free_extent_buffer(eb);
4116 return NULL;
4117}
4118
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004119static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004120{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004121 return (atomic_read(&eb->io_pages) ||
4122 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4123 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004124}
4125
Miao Xie897ca6e92010-10-26 20:57:29 -04004126/*
4127 * Helper for releasing extent buffer page.
4128 */
4129static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4130 unsigned long start_idx)
4131{
4132 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004133 unsigned long num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004134 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004135 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004136
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004137 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004138
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004139 num_pages = num_extent_pages(eb->start, eb->len);
4140 index = start_idx + num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004141 if (start_idx >= index)
4142 return;
4143
4144 do {
4145 index--;
4146 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004147 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004148 spin_lock(&page->mapping->private_lock);
4149 /*
4150 * We do this since we'll remove the pages after we've
4151 * removed the eb from the radix tree, so we could race
4152 * and have this page now attached to the new eb. So
4153 * only clear page_private if it's still connected to
4154 * this eb.
4155 */
4156 if (PagePrivate(page) &&
4157 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004158 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004159 BUG_ON(PageDirty(page));
4160 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004161 /*
4162 * We need to make sure we haven't be attached
4163 * to a new eb.
4164 */
4165 ClearPagePrivate(page);
4166 set_page_private(page, 0);
4167 /* One for the page private */
4168 page_cache_release(page);
4169 }
4170 spin_unlock(&page->mapping->private_lock);
4171
Jan Schmidt815a51c2012-05-16 17:00:02 +02004172 }
4173 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004174 /* One for when we alloced the page */
Miao Xie897ca6e92010-10-26 20:57:29 -04004175 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004176 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004177 } while (index != start_idx);
4178}
4179
4180/*
4181 * Helper for releasing the extent buffer.
4182 */
4183static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4184{
4185 btrfs_release_extent_buffer_page(eb, 0);
4186 __free_extent_buffer(eb);
4187}
4188
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004189static void check_buffer_tree_ref(struct extent_buffer *eb)
4190{
Chris Mason242e18c2013-01-29 17:49:37 -05004191 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004192 /* the ref bit is tricky. We have to make sure it is set
4193 * if we have the buffer dirty. Otherwise the
4194 * code to free a buffer can end up dropping a dirty
4195 * page
4196 *
4197 * Once the ref bit is set, it won't go away while the
4198 * buffer is dirty or in writeback, and it also won't
4199 * go away while we have the reference count on the
4200 * eb bumped.
4201 *
4202 * We can't just set the ref bit without bumping the
4203 * ref on the eb because free_extent_buffer might
4204 * see the ref bit and try to clear it. If this happens
4205 * free_extent_buffer might end up dropping our original
4206 * ref by mistake and freeing the page before we are able
4207 * to add one more ref.
4208 *
4209 * So bump the ref count first, then set the bit. If someone
4210 * beat us to it, drop the ref we added.
4211 */
Chris Mason242e18c2013-01-29 17:49:37 -05004212 refs = atomic_read(&eb->refs);
4213 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4214 return;
4215
Josef Bacik594831c2012-07-20 16:11:08 -04004216 spin_lock(&eb->refs_lock);
4217 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004218 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004219 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004220}
4221
Josef Bacik5df42352012-03-15 18:24:42 -04004222static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4223{
4224 unsigned long num_pages, i;
4225
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004226 check_buffer_tree_ref(eb);
4227
Josef Bacik5df42352012-03-15 18:24:42 -04004228 num_pages = num_extent_pages(eb->start, eb->len);
4229 for (i = 0; i < num_pages; i++) {
4230 struct page *p = extent_buffer_page(eb, i);
4231 mark_page_accessed(p);
4232 }
4233}
4234
Chris Masond1310b22008-01-24 16:13:08 -05004235struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004236 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004237{
4238 unsigned long num_pages = num_extent_pages(start, len);
4239 unsigned long i;
4240 unsigned long index = start >> PAGE_CACHE_SHIFT;
4241 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004242 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004243 struct page *p;
4244 struct address_space *mapping = tree->mapping;
4245 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004246 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004247
Miao Xie19fe0a82010-10-26 20:57:29 -04004248 rcu_read_lock();
4249 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4250 if (eb && atomic_inc_not_zero(&eb->refs)) {
4251 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004252 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004253 return eb;
4254 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004255 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004256
David Sterbaba144192011-04-21 01:12:06 +02004257 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004258 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004259 return NULL;
4260
Chris Mason727011e2010-08-06 13:21:20 -04004261 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004262 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004263 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004264 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004265
4266 spin_lock(&mapping->private_lock);
4267 if (PagePrivate(p)) {
4268 /*
4269 * We could have already allocated an eb for this page
4270 * and attached one so lets see if we can get a ref on
4271 * the existing eb, and if we can we know it's good and
4272 * we can just return that one, else we know we can just
4273 * overwrite page->private.
4274 */
4275 exists = (struct extent_buffer *)p->private;
4276 if (atomic_inc_not_zero(&exists->refs)) {
4277 spin_unlock(&mapping->private_lock);
4278 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004279 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004280 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004281 goto free_eb;
4282 }
4283
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004284 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004285 * Do this so attach doesn't complain and we need to
4286 * drop the ref the old guy had.
4287 */
4288 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004289 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004290 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004291 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004292 attach_extent_buffer_page(eb, p);
4293 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004294 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004295 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004296 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004297 if (!PageUptodate(p))
4298 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004299
4300 /*
4301 * see below about how we avoid a nasty race with release page
4302 * and why we unlock later
4303 */
Chris Masond1310b22008-01-24 16:13:08 -05004304 }
4305 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004306 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004307again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004308 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4309 if (ret)
4310 goto free_eb;
4311
Chris Mason6af118ce2008-07-22 11:18:07 -04004312 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004313 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4314 if (ret == -EEXIST) {
4315 exists = radix_tree_lookup(&tree->buffer,
4316 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004317 if (!atomic_inc_not_zero(&exists->refs)) {
4318 spin_unlock(&tree->buffer_lock);
4319 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004320 exists = NULL;
4321 goto again;
4322 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004323 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004324 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004325 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004326 goto free_eb;
4327 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004328 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004329 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004330 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004331 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004332
4333 /*
4334 * there is a race where release page may have
4335 * tried to find this extent buffer in the radix
4336 * but failed. It will tell the VM it is safe to
4337 * reclaim the, and it will clear the page private bit.
4338 * We must make sure to set the page private bit properly
4339 * after the extent buffer is in the radix tree so
4340 * it doesn't get lost
4341 */
Chris Mason727011e2010-08-06 13:21:20 -04004342 SetPageChecked(eb->pages[0]);
4343 for (i = 1; i < num_pages; i++) {
4344 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004345 ClearPageChecked(p);
4346 unlock_page(p);
4347 }
4348 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004349 return eb;
4350
Chris Mason6af118ce2008-07-22 11:18:07 -04004351free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004352 for (i = 0; i < num_pages; i++) {
4353 if (eb->pages[i])
4354 unlock_page(eb->pages[i]);
4355 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004356
Josef Bacik17de39a2012-05-04 15:16:06 -04004357 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e92010-10-26 20:57:29 -04004358 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004359 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004360}
Chris Masond1310b22008-01-24 16:13:08 -05004361
4362struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004363 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004364{
Chris Masond1310b22008-01-24 16:13:08 -05004365 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004366
Miao Xie19fe0a82010-10-26 20:57:29 -04004367 rcu_read_lock();
4368 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4369 if (eb && atomic_inc_not_zero(&eb->refs)) {
4370 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004371 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004372 return eb;
4373 }
4374 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004375
Miao Xie19fe0a82010-10-26 20:57:29 -04004376 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004377}
Chris Masond1310b22008-01-24 16:13:08 -05004378
Josef Bacik3083ee22012-03-09 16:01:49 -05004379static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4380{
4381 struct extent_buffer *eb =
4382 container_of(head, struct extent_buffer, rcu_head);
4383
4384 __free_extent_buffer(eb);
4385}
4386
Josef Bacik3083ee22012-03-09 16:01:49 -05004387/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004388static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004389{
4390 WARN_ON(atomic_read(&eb->refs) == 0);
4391 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004392 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4393 spin_unlock(&eb->refs_lock);
4394 } else {
4395 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004396
Jan Schmidt815a51c2012-05-16 17:00:02 +02004397 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004398
Jan Schmidt815a51c2012-05-16 17:00:02 +02004399 spin_lock(&tree->buffer_lock);
4400 radix_tree_delete(&tree->buffer,
4401 eb->start >> PAGE_CACHE_SHIFT);
4402 spin_unlock(&tree->buffer_lock);
4403 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004404
4405 /* Should be safe to release our pages at this point */
4406 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004407 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004408 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004409 }
4410 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004411
4412 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004413}
4414
Chris Masond1310b22008-01-24 16:13:08 -05004415void free_extent_buffer(struct extent_buffer *eb)
4416{
Chris Mason242e18c2013-01-29 17:49:37 -05004417 int refs;
4418 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004419 if (!eb)
4420 return;
4421
Chris Mason242e18c2013-01-29 17:49:37 -05004422 while (1) {
4423 refs = atomic_read(&eb->refs);
4424 if (refs <= 3)
4425 break;
4426 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4427 if (old == refs)
4428 return;
4429 }
4430
Josef Bacik3083ee22012-03-09 16:01:49 -05004431 spin_lock(&eb->refs_lock);
4432 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004433 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4434 atomic_dec(&eb->refs);
4435
4436 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004437 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004438 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004439 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4440 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004441
Josef Bacik3083ee22012-03-09 16:01:49 -05004442 /*
4443 * I know this is terrible, but it's temporary until we stop tracking
4444 * the uptodate bits and such for the extent buffers.
4445 */
4446 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004447}
Chris Masond1310b22008-01-24 16:13:08 -05004448
Josef Bacik3083ee22012-03-09 16:01:49 -05004449void free_extent_buffer_stale(struct extent_buffer *eb)
4450{
4451 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004452 return;
4453
Josef Bacik3083ee22012-03-09 16:01:49 -05004454 spin_lock(&eb->refs_lock);
4455 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4456
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004457 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004458 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4459 atomic_dec(&eb->refs);
4460 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004461}
4462
Chris Mason1d4284b2012-03-28 20:31:37 -04004463void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004464{
Chris Masond1310b22008-01-24 16:13:08 -05004465 unsigned long i;
4466 unsigned long num_pages;
4467 struct page *page;
4468
Chris Masond1310b22008-01-24 16:13:08 -05004469 num_pages = num_extent_pages(eb->start, eb->len);
4470
4471 for (i = 0; i < num_pages; i++) {
4472 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004473 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004474 continue;
4475
Chris Masona61e6f22008-07-22 11:18:08 -04004476 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004477 WARN_ON(!PagePrivate(page));
4478
Chris Masond1310b22008-01-24 16:13:08 -05004479 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004480 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004481 if (!PageDirty(page)) {
4482 radix_tree_tag_clear(&page->mapping->page_tree,
4483 page_index(page),
4484 PAGECACHE_TAG_DIRTY);
4485 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004486 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004487 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004488 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004489 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004490 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004491}
Chris Masond1310b22008-01-24 16:13:08 -05004492
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004493int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004494{
4495 unsigned long i;
4496 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004497 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004498
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004499 check_buffer_tree_ref(eb);
4500
Chris Masonb9473432009-03-13 11:00:37 -04004501 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004502
Chris Masond1310b22008-01-24 16:13:08 -05004503 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004504 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004505 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4506
Chris Masonb9473432009-03-13 11:00:37 -04004507 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004508 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004509 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004510}
Chris Masond1310b22008-01-24 16:13:08 -05004511
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004512static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004513{
4514 if (len < PAGE_CACHE_SIZE)
4515 return 1;
4516 if (start & (PAGE_CACHE_SIZE - 1))
4517 return 1;
4518 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4519 return 1;
4520 return 0;
4521}
4522
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004523int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004524{
4525 unsigned long i;
4526 struct page *page;
4527 unsigned long num_pages;
4528
Chris Masonb4ce94d2009-02-04 09:25:08 -05004529 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004530 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004531 for (i = 0; i < num_pages; i++) {
4532 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004533 if (page)
4534 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004535 }
4536 return 0;
4537}
4538
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004539int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004540{
4541 unsigned long i;
4542 struct page *page;
4543 unsigned long num_pages;
4544
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004545 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004546 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004547 for (i = 0; i < num_pages; i++) {
4548 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004549 SetPageUptodate(page);
4550 }
4551 return 0;
4552}
Chris Masond1310b22008-01-24 16:13:08 -05004553
Chris Masonce9adaa2008-04-09 16:28:12 -04004554int extent_range_uptodate(struct extent_io_tree *tree,
4555 u64 start, u64 end)
4556{
4557 struct page *page;
4558 int ret;
4559 int pg_uptodate = 1;
4560 int uptodate;
4561 unsigned long index;
4562
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004563 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004564 ret = test_range_bit(tree, start, end,
4565 EXTENT_UPTODATE, 1, NULL);
4566 if (ret)
4567 return 1;
4568 }
Chris Masond3977122009-01-05 21:25:51 -05004569 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004570 index = start >> PAGE_CACHE_SHIFT;
4571 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004572 if (!page)
4573 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004574 uptodate = PageUptodate(page);
4575 page_cache_release(page);
4576 if (!uptodate) {
4577 pg_uptodate = 0;
4578 break;
4579 }
4580 start += PAGE_CACHE_SIZE;
4581 }
4582 return pg_uptodate;
4583}
4584
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004585int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004586{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004587 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004588}
Chris Masond1310b22008-01-24 16:13:08 -05004589
4590int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004591 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004592 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004593{
4594 unsigned long i;
4595 unsigned long start_i;
4596 struct page *page;
4597 int err;
4598 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004599 int locked_pages = 0;
4600 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004601 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004602 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004603 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004604 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004605
Chris Masonb4ce94d2009-02-04 09:25:08 -05004606 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004607 return 0;
4608
Chris Masond1310b22008-01-24 16:13:08 -05004609 if (start) {
4610 WARN_ON(start < eb->start);
4611 start_i = (start >> PAGE_CACHE_SHIFT) -
4612 (eb->start >> PAGE_CACHE_SHIFT);
4613 } else {
4614 start_i = 0;
4615 }
4616
4617 num_pages = num_extent_pages(eb->start, eb->len);
4618 for (i = start_i; i < num_pages; i++) {
4619 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004620 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004621 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004622 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004623 } else {
4624 lock_page(page);
4625 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004626 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004627 if (!PageUptodate(page)) {
4628 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004629 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004630 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004631 }
4632 if (all_uptodate) {
4633 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004634 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004635 goto unlock_exit;
4636 }
4637
Josef Bacikea466792012-03-26 21:57:36 -04004638 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004639 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004640 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004641 for (i = start_i; i < num_pages; i++) {
4642 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004643 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004644 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004645 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004646 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004647 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004648 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004649 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004650 } else {
4651 unlock_page(page);
4652 }
4653 }
4654
Jeff Mahoney355808c2011-10-03 23:23:14 -04004655 if (bio) {
4656 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004657 if (err)
4658 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004659 }
Chris Masona86c12c2008-02-07 10:50:54 -05004660
Arne Jansenbb82ab82011-06-10 14:06:53 +02004661 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004662 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004663
Chris Masond1310b22008-01-24 16:13:08 -05004664 for (i = start_i; i < num_pages; i++) {
4665 page = extent_buffer_page(eb, i);
4666 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004667 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004668 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004669 }
Chris Masond3977122009-01-05 21:25:51 -05004670
Chris Masond1310b22008-01-24 16:13:08 -05004671 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004672
4673unlock_exit:
4674 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004675 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004676 page = extent_buffer_page(eb, i);
4677 i++;
4678 unlock_page(page);
4679 locked_pages--;
4680 }
4681 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004682}
Chris Masond1310b22008-01-24 16:13:08 -05004683
4684void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4685 unsigned long start,
4686 unsigned long len)
4687{
4688 size_t cur;
4689 size_t offset;
4690 struct page *page;
4691 char *kaddr;
4692 char *dst = (char *)dstv;
4693 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4694 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004695
4696 WARN_ON(start > eb->len);
4697 WARN_ON(start + len > eb->start + eb->len);
4698
4699 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4700
Chris Masond3977122009-01-05 21:25:51 -05004701 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004702 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004703
4704 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004705 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004706 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004707
4708 dst += cur;
4709 len -= cur;
4710 offset = 0;
4711 i++;
4712 }
4713}
Chris Masond1310b22008-01-24 16:13:08 -05004714
4715int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004716 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004717 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004718 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004719{
4720 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4721 char *kaddr;
4722 struct page *p;
4723 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4724 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4725 unsigned long end_i = (start_offset + start + min_len - 1) >>
4726 PAGE_CACHE_SHIFT;
4727
4728 if (i != end_i)
4729 return -EINVAL;
4730
4731 if (i == 0) {
4732 offset = start_offset;
4733 *map_start = 0;
4734 } else {
4735 offset = 0;
4736 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4737 }
Chris Masond3977122009-01-05 21:25:51 -05004738
Chris Masond1310b22008-01-24 16:13:08 -05004739 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004740 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004741 "wanted %lu %lu\n", (unsigned long long)eb->start,
4742 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004743 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004744 }
4745
4746 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004747 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004748 *map = kaddr + offset;
4749 *map_len = PAGE_CACHE_SIZE - offset;
4750 return 0;
4751}
Chris Masond1310b22008-01-24 16:13:08 -05004752
Chris Masond1310b22008-01-24 16:13:08 -05004753int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4754 unsigned long start,
4755 unsigned long len)
4756{
4757 size_t cur;
4758 size_t offset;
4759 struct page *page;
4760 char *kaddr;
4761 char *ptr = (char *)ptrv;
4762 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4763 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4764 int ret = 0;
4765
4766 WARN_ON(start > eb->len);
4767 WARN_ON(start + len > eb->start + eb->len);
4768
4769 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4770
Chris Masond3977122009-01-05 21:25:51 -05004771 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004772 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004773
4774 cur = min(len, (PAGE_CACHE_SIZE - offset));
4775
Chris Masona6591712011-07-19 12:04:14 -04004776 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004777 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004778 if (ret)
4779 break;
4780
4781 ptr += cur;
4782 len -= cur;
4783 offset = 0;
4784 i++;
4785 }
4786 return ret;
4787}
Chris Masond1310b22008-01-24 16:13:08 -05004788
4789void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4790 unsigned long start, unsigned long len)
4791{
4792 size_t cur;
4793 size_t offset;
4794 struct page *page;
4795 char *kaddr;
4796 char *src = (char *)srcv;
4797 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4798 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4799
4800 WARN_ON(start > eb->len);
4801 WARN_ON(start + len > eb->start + eb->len);
4802
4803 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4804
Chris Masond3977122009-01-05 21:25:51 -05004805 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004806 page = extent_buffer_page(eb, i);
4807 WARN_ON(!PageUptodate(page));
4808
4809 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004810 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004811 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004812
4813 src += cur;
4814 len -= cur;
4815 offset = 0;
4816 i++;
4817 }
4818}
Chris Masond1310b22008-01-24 16:13:08 -05004819
4820void memset_extent_buffer(struct extent_buffer *eb, char c,
4821 unsigned long start, unsigned long len)
4822{
4823 size_t cur;
4824 size_t offset;
4825 struct page *page;
4826 char *kaddr;
4827 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4828 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4829
4830 WARN_ON(start > eb->len);
4831 WARN_ON(start + len > eb->start + eb->len);
4832
4833 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4834
Chris Masond3977122009-01-05 21:25:51 -05004835 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004836 page = extent_buffer_page(eb, i);
4837 WARN_ON(!PageUptodate(page));
4838
4839 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004840 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004841 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004842
4843 len -= cur;
4844 offset = 0;
4845 i++;
4846 }
4847}
Chris Masond1310b22008-01-24 16:13:08 -05004848
4849void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4850 unsigned long dst_offset, unsigned long src_offset,
4851 unsigned long len)
4852{
4853 u64 dst_len = dst->len;
4854 size_t cur;
4855 size_t offset;
4856 struct page *page;
4857 char *kaddr;
4858 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4859 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4860
4861 WARN_ON(src->len != dst_len);
4862
4863 offset = (start_offset + dst_offset) &
4864 ((unsigned long)PAGE_CACHE_SIZE - 1);
4865
Chris Masond3977122009-01-05 21:25:51 -05004866 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004867 page = extent_buffer_page(dst, i);
4868 WARN_ON(!PageUptodate(page));
4869
4870 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4871
Chris Masona6591712011-07-19 12:04:14 -04004872 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004873 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004874
4875 src_offset += cur;
4876 len -= cur;
4877 offset = 0;
4878 i++;
4879 }
4880}
Chris Masond1310b22008-01-24 16:13:08 -05004881
4882static void move_pages(struct page *dst_page, struct page *src_page,
4883 unsigned long dst_off, unsigned long src_off,
4884 unsigned long len)
4885{
Chris Masona6591712011-07-19 12:04:14 -04004886 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004887 if (dst_page == src_page) {
4888 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4889 } else {
Chris Masona6591712011-07-19 12:04:14 -04004890 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004891 char *p = dst_kaddr + dst_off + len;
4892 char *s = src_kaddr + src_off + len;
4893
4894 while (len--)
4895 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004896 }
Chris Masond1310b22008-01-24 16:13:08 -05004897}
4898
Sergei Trofimovich33872062011-04-11 21:52:52 +00004899static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4900{
4901 unsigned long distance = (src > dst) ? src - dst : dst - src;
4902 return distance < len;
4903}
4904
Chris Masond1310b22008-01-24 16:13:08 -05004905static void copy_pages(struct page *dst_page, struct page *src_page,
4906 unsigned long dst_off, unsigned long src_off,
4907 unsigned long len)
4908{
Chris Masona6591712011-07-19 12:04:14 -04004909 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004910 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004911 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004912
Sergei Trofimovich33872062011-04-11 21:52:52 +00004913 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004914 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004915 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004916 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004917 if (areas_overlap(src_off, dst_off, len))
4918 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004919 }
Chris Masond1310b22008-01-24 16:13:08 -05004920
Chris Mason727011e2010-08-06 13:21:20 -04004921 if (must_memmove)
4922 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4923 else
4924 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004925}
4926
4927void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4928 unsigned long src_offset, unsigned long len)
4929{
4930 size_t cur;
4931 size_t dst_off_in_page;
4932 size_t src_off_in_page;
4933 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4934 unsigned long dst_i;
4935 unsigned long src_i;
4936
4937 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004938 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4939 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004940 BUG_ON(1);
4941 }
4942 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004943 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4944 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004945 BUG_ON(1);
4946 }
4947
Chris Masond3977122009-01-05 21:25:51 -05004948 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004949 dst_off_in_page = (start_offset + dst_offset) &
4950 ((unsigned long)PAGE_CACHE_SIZE - 1);
4951 src_off_in_page = (start_offset + src_offset) &
4952 ((unsigned long)PAGE_CACHE_SIZE - 1);
4953
4954 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4955 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4956
4957 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4958 src_off_in_page));
4959 cur = min_t(unsigned long, cur,
4960 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4961
4962 copy_pages(extent_buffer_page(dst, dst_i),
4963 extent_buffer_page(dst, src_i),
4964 dst_off_in_page, src_off_in_page, cur);
4965
4966 src_offset += cur;
4967 dst_offset += cur;
4968 len -= cur;
4969 }
4970}
Chris Masond1310b22008-01-24 16:13:08 -05004971
4972void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4973 unsigned long src_offset, unsigned long len)
4974{
4975 size_t cur;
4976 size_t dst_off_in_page;
4977 size_t src_off_in_page;
4978 unsigned long dst_end = dst_offset + len - 1;
4979 unsigned long src_end = src_offset + len - 1;
4980 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4981 unsigned long dst_i;
4982 unsigned long src_i;
4983
4984 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004985 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4986 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004987 BUG_ON(1);
4988 }
4989 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004990 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4991 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004992 BUG_ON(1);
4993 }
Chris Mason727011e2010-08-06 13:21:20 -04004994 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004995 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4996 return;
4997 }
Chris Masond3977122009-01-05 21:25:51 -05004998 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004999 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5000 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5001
5002 dst_off_in_page = (start_offset + dst_end) &
5003 ((unsigned long)PAGE_CACHE_SIZE - 1);
5004 src_off_in_page = (start_offset + src_end) &
5005 ((unsigned long)PAGE_CACHE_SIZE - 1);
5006
5007 cur = min_t(unsigned long, len, src_off_in_page + 1);
5008 cur = min(cur, dst_off_in_page + 1);
5009 move_pages(extent_buffer_page(dst, dst_i),
5010 extent_buffer_page(dst, src_i),
5011 dst_off_in_page - cur + 1,
5012 src_off_in_page - cur + 1, cur);
5013
5014 dst_end -= cur;
5015 src_end -= cur;
5016 len -= cur;
5017 }
5018}
Chris Mason6af118ce2008-07-22 11:18:07 -04005019
Josef Bacik3083ee22012-03-09 16:01:49 -05005020int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04005021{
Chris Mason6af118ce2008-07-22 11:18:07 -04005022 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005023
Miao Xie19fe0a82010-10-26 20:57:29 -04005024 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005025 * We need to make sure noboody is attaching this page to an eb right
5026 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005027 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005028 spin_lock(&page->mapping->private_lock);
5029 if (!PagePrivate(page)) {
5030 spin_unlock(&page->mapping->private_lock);
5031 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005032 }
5033
Josef Bacik3083ee22012-03-09 16:01:49 -05005034 eb = (struct extent_buffer *)page->private;
5035 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005036
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005037 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005038 * This is a little awful but should be ok, we need to make sure that
5039 * the eb doesn't disappear out from under us while we're looking at
5040 * this page.
5041 */
5042 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005043 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005044 spin_unlock(&eb->refs_lock);
5045 spin_unlock(&page->mapping->private_lock);
5046 return 0;
5047 }
5048 spin_unlock(&page->mapping->private_lock);
5049
5050 if ((mask & GFP_NOFS) == GFP_NOFS)
5051 mask = GFP_NOFS;
5052
5053 /*
5054 * If tree ref isn't set then we know the ref on this eb is a real ref,
5055 * so just return, this page will likely be freed soon anyway.
5056 */
5057 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5058 spin_unlock(&eb->refs_lock);
5059 return 0;
5060 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005061
Josef Bacike64860a2012-07-20 16:05:36 -04005062 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04005063}