blob: 9c066d687a42bd28b04e314977abad794a29468a [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 Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020018#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040020#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040021#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080022#include "backref.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;
Chris Mason9be33952013-05-17 18:30:14 -040026static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050027
Filipe Manana27a35072014-07-06 20:09:59 +010028static inline bool extent_state_in_tree(const struct extent_state *state)
29{
30 return !RB_EMPTY_NODE(&state->rb_node);
31}
32
Eric Sandeen6d49ba12013-04-22 16:12:31 +000033#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050034static LIST_HEAD(buffers);
35static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040036
Chris Masond3977122009-01-05 21:25:51 -050037static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000038
39static inline
40void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
41{
42 unsigned long flags;
43
44 spin_lock_irqsave(&leak_lock, flags);
45 list_add(new, head);
46 spin_unlock_irqrestore(&leak_lock, flags);
47}
48
49static inline
50void btrfs_leak_debug_del(struct list_head *entry)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&leak_lock, flags);
55 list_del(entry);
56 spin_unlock_irqrestore(&leak_lock, flags);
57}
58
59static inline
60void btrfs_leak_debug_check(void)
61{
62 struct extent_state *state;
63 struct extent_buffer *eb;
64
65 while (!list_empty(&states)) {
66 state = list_entry(states.next, struct extent_state, leak_list);
David Sterba9ee49a042015-01-14 19:52:13 +010067 pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
Filipe Manana27a35072014-07-06 20:09:59 +010068 state->start, state->end, state->state,
69 extent_state_in_tree(state),
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020070 atomic_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000071 list_del(&state->leak_list);
72 kmem_cache_free(extent_state_cache, state);
73 }
74
75 while (!list_empty(&buffers)) {
76 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Frank Holtonefe120a2013-12-20 11:37:06 -050077 printk(KERN_ERR "BTRFS: buffer leak start %llu len %lu "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020078 "refs %d\n",
79 eb->start, eb->len, atomic_read(&eb->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000080 list_del(&eb->leak_list);
81 kmem_cache_free(extent_buffer_cache, eb);
82 }
83}
David Sterba8d599ae2013-04-30 15:22:23 +000084
Josef Bacika5dee372013-12-13 10:02:44 -050085#define btrfs_debug_check_extent_io_range(tree, start, end) \
86 __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
David Sterba8d599ae2013-04-30 15:22:23 +000087static inline void __btrfs_debug_check_extent_io_range(const char *caller,
Josef Bacika5dee372013-12-13 10:02:44 -050088 struct extent_io_tree *tree, u64 start, u64 end)
David Sterba8d599ae2013-04-30 15:22:23 +000089{
Josef Bacika5dee372013-12-13 10:02:44 -050090 struct inode *inode;
91 u64 isize;
David Sterba8d599ae2013-04-30 15:22:23 +000092
Josef Bacika5dee372013-12-13 10:02:44 -050093 if (!tree->mapping)
94 return;
95
96 inode = tree->mapping->host;
97 isize = i_size_read(inode);
David Sterba8d599ae2013-04-30 15:22:23 +000098 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
David Sterba94647322015-10-08 11:01:36 +020099 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
100 "%s: ino %llu isize %llu odd range [%llu,%llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200101 caller, btrfs_ino(inode), isize, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000102 }
103}
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000104#else
105#define btrfs_leak_debug_add(new, head) do {} while (0)
106#define btrfs_leak_debug_del(entry) do {} while (0)
107#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000108#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400109#endif
Chris Masond1310b22008-01-24 16:13:08 -0500110
Chris Masond1310b22008-01-24 16:13:08 -0500111#define BUFFER_LRU_MAX 64
112
113struct tree_entry {
114 u64 start;
115 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500116 struct rb_node rb_node;
117};
118
119struct extent_page_data {
120 struct bio *bio;
121 struct extent_io_tree *tree;
122 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -0400123 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500124
125 /* tells writepage not to lock the state bits for this range
126 * it still does the unlocking
127 */
Chris Masonffbd5172009-04-20 15:50:09 -0400128 unsigned int extent_locked:1;
129
130 /* tells the submit_bio code to use a WRITE_SYNC */
131 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500132};
133
Qu Wenruod38ed272015-10-12 14:53:37 +0800134static void add_extent_changeset(struct extent_state *state, unsigned bits,
135 struct extent_changeset *changeset,
136 int set)
137{
138 int ret;
139
140 if (!changeset)
141 return;
142 if (set && (state->state & bits) == bits)
143 return;
144 changeset->bytes_changed += state->end - state->start + 1;
145 ret = ulist_add(changeset->range_changed, state->start, state->end,
146 GFP_ATOMIC);
147 /* ENOMEM */
148 BUG_ON(ret < 0);
149}
150
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400151static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400152static inline struct btrfs_fs_info *
153tree_fs_info(struct extent_io_tree *tree)
154{
Josef Bacika5dee372013-12-13 10:02:44 -0500155 if (!tree->mapping)
156 return NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400157 return btrfs_sb(tree->mapping->host->i_sb);
158}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400159
Chris Masond1310b22008-01-24 16:13:08 -0500160int __init extent_io_init(void)
161{
David Sterba837e1972012-09-07 03:00:48 -0600162 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200163 sizeof(struct extent_state), 0,
164 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500165 if (!extent_state_cache)
166 return -ENOMEM;
167
David Sterba837e1972012-09-07 03:00:48 -0600168 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200169 sizeof(struct extent_buffer), 0,
170 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500171 if (!extent_buffer_cache)
172 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400173
174 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
175 offsetof(struct btrfs_io_bio, bio));
176 if (!btrfs_bioset)
177 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700178
179 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
180 goto free_bioset;
181
Chris Masond1310b22008-01-24 16:13:08 -0500182 return 0;
183
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700184free_bioset:
185 bioset_free(btrfs_bioset);
186 btrfs_bioset = NULL;
187
Chris Mason9be33952013-05-17 18:30:14 -0400188free_buffer_cache:
189 kmem_cache_destroy(extent_buffer_cache);
190 extent_buffer_cache = NULL;
191
Chris Masond1310b22008-01-24 16:13:08 -0500192free_state_cache:
193 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400194 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500195 return -ENOMEM;
196}
197
198void extent_io_exit(void)
199{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000200 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000201
202 /*
203 * Make sure all delayed rcu free are flushed before we
204 * destroy caches.
205 */
206 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500207 if (extent_state_cache)
208 kmem_cache_destroy(extent_state_cache);
209 if (extent_buffer_cache)
210 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400211 if (btrfs_bioset)
212 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500213}
214
215void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200216 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500217{
Eric Paris6bef4d32010-02-23 19:43:04 +0000218 tree->state = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500219 tree->ops = NULL;
220 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500221 spin_lock_init(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500222 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500223}
Chris Masond1310b22008-01-24 16:13:08 -0500224
Christoph Hellwigb2950862008-12-02 09:54:17 -0500225static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500226{
227 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500228
229 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400230 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500231 return state;
232 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500233 state->private = 0;
Filipe Manana27a35072014-07-06 20:09:59 +0100234 RB_CLEAR_NODE(&state->rb_node);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000235 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500236 atomic_set(&state->refs, 1);
237 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100238 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500239 return state;
240}
Chris Masond1310b22008-01-24 16:13:08 -0500241
Chris Mason4845e442010-05-25 20:56:50 -0400242void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500243{
Chris Masond1310b22008-01-24 16:13:08 -0500244 if (!state)
245 return;
246 if (atomic_dec_and_test(&state->refs)) {
Filipe Manana27a35072014-07-06 20:09:59 +0100247 WARN_ON(extent_state_in_tree(state));
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000248 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100249 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500250 kmem_cache_free(extent_state_cache, state);
251 }
252}
Chris Masond1310b22008-01-24 16:13:08 -0500253
Filipe Mananaf2071b22014-02-12 15:05:53 +0000254static struct rb_node *tree_insert(struct rb_root *root,
255 struct rb_node *search_start,
256 u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000257 struct rb_node *node,
258 struct rb_node ***p_in,
259 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500260{
Filipe Mananaf2071b22014-02-12 15:05:53 +0000261 struct rb_node **p;
Chris Masond3977122009-01-05 21:25:51 -0500262 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500263 struct tree_entry *entry;
264
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000265 if (p_in && parent_in) {
266 p = *p_in;
267 parent = *parent_in;
268 goto do_insert;
269 }
270
Filipe Mananaf2071b22014-02-12 15:05:53 +0000271 p = search_start ? &search_start : &root->rb_node;
Chris Masond3977122009-01-05 21:25:51 -0500272 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500273 parent = *p;
274 entry = rb_entry(parent, struct tree_entry, rb_node);
275
276 if (offset < entry->start)
277 p = &(*p)->rb_left;
278 else if (offset > entry->end)
279 p = &(*p)->rb_right;
280 else
281 return parent;
282 }
283
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000284do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500285 rb_link_node(node, parent, p);
286 rb_insert_color(node, root);
287 return NULL;
288}
289
Chris Mason80ea96b2008-02-01 14:51:59 -0500290static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000291 struct rb_node **prev_ret,
292 struct rb_node **next_ret,
293 struct rb_node ***p_ret,
294 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500295{
Chris Mason80ea96b2008-02-01 14:51:59 -0500296 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000297 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500298 struct rb_node *prev = NULL;
299 struct rb_node *orig_prev = NULL;
300 struct tree_entry *entry;
301 struct tree_entry *prev_entry = NULL;
302
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000303 while (*n) {
304 prev = *n;
305 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500306 prev_entry = entry;
307
308 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000309 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500310 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000311 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500312 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000313 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500314 }
315
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000316 if (p_ret)
317 *p_ret = n;
318 if (parent_ret)
319 *parent_ret = prev;
320
Chris Masond1310b22008-01-24 16:13:08 -0500321 if (prev_ret) {
322 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500323 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500324 prev = rb_next(prev);
325 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
326 }
327 *prev_ret = prev;
328 prev = orig_prev;
329 }
330
331 if (next_ret) {
332 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500333 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500334 prev = rb_prev(prev);
335 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
336 }
337 *next_ret = prev;
338 }
339 return NULL;
340}
341
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000342static inline struct rb_node *
343tree_search_for_insert(struct extent_io_tree *tree,
344 u64 offset,
345 struct rb_node ***p_ret,
346 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500347{
Chris Mason70dec802008-01-29 09:59:12 -0500348 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500349 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500350
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000351 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500352 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500353 return prev;
354 return ret;
355}
356
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000357static inline struct rb_node *tree_search(struct extent_io_tree *tree,
358 u64 offset)
359{
360 return tree_search_for_insert(tree, offset, NULL, NULL);
361}
362
Josef Bacik9ed74f22009-09-11 16:12:44 -0400363static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
364 struct extent_state *other)
365{
366 if (tree->ops && tree->ops->merge_extent_hook)
367 tree->ops->merge_extent_hook(tree->mapping->host, new,
368 other);
369}
370
Chris Masond1310b22008-01-24 16:13:08 -0500371/*
372 * utility function to look for merge candidates inside a given range.
373 * Any extents with matching state are merged together into a single
374 * extent in the tree. Extents with EXTENT_IO in their state field
375 * are not merged because the end_io handlers need to be able to do
376 * operations on them without sleeping (or doing allocations/splits).
377 *
378 * This should be called with the tree lock held.
379 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000380static void merge_state(struct extent_io_tree *tree,
381 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500382{
383 struct extent_state *other;
384 struct rb_node *other_node;
385
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400386 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000387 return;
Chris Masond1310b22008-01-24 16:13:08 -0500388
389 other_node = rb_prev(&state->rb_node);
390 if (other_node) {
391 other = rb_entry(other_node, struct extent_state, rb_node);
392 if (other->end == state->start - 1 &&
393 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400394 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500395 state->start = other->start;
Chris Masond1310b22008-01-24 16:13:08 -0500396 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100397 RB_CLEAR_NODE(&other->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500398 free_extent_state(other);
399 }
400 }
401 other_node = rb_next(&state->rb_node);
402 if (other_node) {
403 other = rb_entry(other_node, struct extent_state, rb_node);
404 if (other->start == state->end + 1 &&
405 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400406 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400407 state->end = other->end;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400408 rb_erase(&other->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100409 RB_CLEAR_NODE(&other->rb_node);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400410 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500411 }
412 }
Chris Masond1310b22008-01-24 16:13:08 -0500413}
414
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000415static void set_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100416 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500417{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000418 if (tree->ops && tree->ops->set_bit_hook)
419 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500420}
421
422static void clear_state_cb(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +0100423 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500424{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400425 if (tree->ops && tree->ops->clear_bit_hook)
426 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500427}
428
Xiao Guangrong3150b692011-07-14 03:19:08 +0000429static void set_state_bits(struct extent_io_tree *tree,
Qu Wenruod38ed272015-10-12 14:53:37 +0800430 struct extent_state *state, unsigned *bits,
431 struct extent_changeset *changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000432
Chris Masond1310b22008-01-24 16:13:08 -0500433/*
434 * insert an extent_state struct into the tree. 'bits' are set on the
435 * struct before it is inserted.
436 *
437 * This may return -EEXIST if the extent is already there, in which case the
438 * state struct is freed.
439 *
440 * The tree lock is not taken internally. This is a utility function and
441 * probably isn't what you want to call (see set/clear_extent_bit).
442 */
443static int insert_state(struct extent_io_tree *tree,
444 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000445 struct rb_node ***p,
446 struct rb_node **parent,
Qu Wenruod38ed272015-10-12 14:53:37 +0800447 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500448{
449 struct rb_node *node;
450
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000451 if (end < start)
Frank Holtonefe120a2013-12-20 11:37:06 -0500452 WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200453 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500454 state->start = start;
455 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400456
Qu Wenruod38ed272015-10-12 14:53:37 +0800457 set_state_bits(tree, state, bits, changeset);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000458
Filipe Mananaf2071b22014-02-12 15:05:53 +0000459 node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500460 if (node) {
461 struct extent_state *found;
462 found = rb_entry(node, struct extent_state, rb_node);
Frank Holtonefe120a2013-12-20 11:37:06 -0500463 printk(KERN_ERR "BTRFS: found node %llu %llu on insert of "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200464 "%llu %llu\n",
465 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500466 return -EEXIST;
467 }
468 merge_state(tree, state);
469 return 0;
470}
471
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000472static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400473 u64 split)
474{
475 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000476 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400477}
478
Chris Masond1310b22008-01-24 16:13:08 -0500479/*
480 * split a given extent state struct in two, inserting the preallocated
481 * struct 'prealloc' as the newly created second half. 'split' indicates an
482 * offset inside 'orig' where it should be split.
483 *
484 * Before calling,
485 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
486 * are two extent state structs in the tree:
487 * prealloc: [orig->start, split - 1]
488 * orig: [ split, orig->end ]
489 *
490 * The tree locks are not taken by this function. They need to be held
491 * by the caller.
492 */
493static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
494 struct extent_state *prealloc, u64 split)
495{
496 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400497
498 split_cb(tree, orig, split);
499
Chris Masond1310b22008-01-24 16:13:08 -0500500 prealloc->start = orig->start;
501 prealloc->end = split - 1;
502 prealloc->state = orig->state;
503 orig->start = split;
504
Filipe Mananaf2071b22014-02-12 15:05:53 +0000505 node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
506 &prealloc->rb_node, NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500507 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500508 free_extent_state(prealloc);
509 return -EEXIST;
510 }
511 return 0;
512}
513
Li Zefancdc6a392012-03-12 16:39:48 +0800514static struct extent_state *next_state(struct extent_state *state)
515{
516 struct rb_node *next = rb_next(&state->rb_node);
517 if (next)
518 return rb_entry(next, struct extent_state, rb_node);
519 else
520 return NULL;
521}
522
Chris Masond1310b22008-01-24 16:13:08 -0500523/*
524 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800525 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500526 *
527 * If no bits are set on the state struct after clearing things, the
528 * struct is freed and removed from the tree
529 */
Li Zefancdc6a392012-03-12 16:39:48 +0800530static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
531 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +0100532 unsigned *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500533{
Li Zefancdc6a392012-03-12 16:39:48 +0800534 struct extent_state *next;
David Sterba9ee49a042015-01-14 19:52:13 +0100535 unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500536
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400537 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500538 u64 range = state->end - state->start + 1;
539 WARN_ON(range > tree->dirty_bytes);
540 tree->dirty_bytes -= range;
541 }
Chris Mason291d6732008-01-29 15:55:23 -0500542 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400543 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500544 if (wake)
545 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400546 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800547 next = next_state(state);
Filipe Manana27a35072014-07-06 20:09:59 +0100548 if (extent_state_in_tree(state)) {
Chris Masond1310b22008-01-24 16:13:08 -0500549 rb_erase(&state->rb_node, &tree->state);
Filipe Manana27a35072014-07-06 20:09:59 +0100550 RB_CLEAR_NODE(&state->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500551 free_extent_state(state);
552 } else {
553 WARN_ON(1);
554 }
555 } else {
556 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800557 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500558 }
Li Zefancdc6a392012-03-12 16:39:48 +0800559 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500560}
561
Xiao Guangrong82337672011-04-20 06:44:57 +0000562static struct extent_state *
563alloc_extent_state_atomic(struct extent_state *prealloc)
564{
565 if (!prealloc)
566 prealloc = alloc_extent_state(GFP_ATOMIC);
567
568 return prealloc;
569}
570
Eric Sandeen48a3b632013-04-25 20:41:01 +0000571static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400572{
573 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
574 "Extent tree was modified by another "
575 "thread while locked.");
576}
577
Chris Masond1310b22008-01-24 16:13:08 -0500578/*
579 * clear some bits on a range in the tree. This may require splitting
580 * or inserting elements in the tree, so the gfp mask is used to
581 * indicate which allocations or sleeping are allowed.
582 *
583 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
584 * the given range from the tree regardless of state (ie for truncate).
585 *
586 * the range [start, end] is inclusive.
587 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100588 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500589 */
590int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100591 unsigned bits, int wake, int delete,
Chris Mason2c64c532009-09-02 15:04:12 -0400592 struct extent_state **cached_state,
593 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500594{
595 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400596 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500597 struct extent_state *prealloc = NULL;
598 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400599 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500600 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000601 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500602
Josef Bacika5dee372013-12-13 10:02:44 -0500603 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000604
Josef Bacik7ee9e442013-06-21 16:37:03 -0400605 if (bits & EXTENT_DELALLOC)
606 bits |= EXTENT_NORESERVE;
607
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400608 if (delete)
609 bits |= ~EXTENT_CTLBITS;
610 bits |= EXTENT_FIRST_DELALLOC;
611
Josef Bacik2ac55d42010-02-03 19:33:23 +0000612 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
613 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500614again:
615 if (!prealloc && (mask & __GFP_WAIT)) {
Filipe Mananac7bc6312014-11-03 14:12:57 +0000616 /*
617 * Don't care for allocation failure here because we might end
618 * up not needing the pre-allocated extent state at all, which
619 * is the case if we only have in the tree extent states that
620 * cover our input range and don't cover too any other range.
621 * If we end up needing a new extent state we allocate it later.
622 */
Chris Masond1310b22008-01-24 16:13:08 -0500623 prealloc = alloc_extent_state(mask);
Chris Masond1310b22008-01-24 16:13:08 -0500624 }
625
Chris Masoncad321a2008-12-17 14:51:42 -0500626 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400627 if (cached_state) {
628 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000629
630 if (clear) {
631 *cached_state = NULL;
632 cached_state = NULL;
633 }
634
Filipe Manana27a35072014-07-06 20:09:59 +0100635 if (cached && extent_state_in_tree(cached) &&
636 cached->start <= start && cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000637 if (clear)
638 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400639 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400640 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400641 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000642 if (clear)
643 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400644 }
Chris Masond1310b22008-01-24 16:13:08 -0500645 /*
646 * this search will find the extents that end after
647 * our range starts
648 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500649 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500650 if (!node)
651 goto out;
652 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400653hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500654 if (state->start > end)
655 goto out;
656 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400657 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500658
Liu Bo04493142012-02-16 18:34:37 +0800659 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800660 if (!(state->state & bits)) {
661 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800662 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800663 }
Liu Bo04493142012-02-16 18:34:37 +0800664
Chris Masond1310b22008-01-24 16:13:08 -0500665 /*
666 * | ---- desired range ---- |
667 * | state | or
668 * | ------------- state -------------- |
669 *
670 * We need to split the extent we found, and may flip
671 * bits on second half.
672 *
673 * If the extent we found extends past our range, we
674 * just split and search again. It'll get split again
675 * the next time though.
676 *
677 * If the extent we found is inside our range, we clear
678 * the desired bit on it.
679 */
680
681 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000682 prealloc = alloc_extent_state_atomic(prealloc);
683 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500684 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400685 if (err)
686 extent_io_tree_panic(tree, err);
687
Chris Masond1310b22008-01-24 16:13:08 -0500688 prealloc = NULL;
689 if (err)
690 goto out;
691 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800692 state = clear_state_bit(tree, state, &bits, wake);
693 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500694 }
695 goto search_again;
696 }
697 /*
698 * | ---- desired range ---- |
699 * | state |
700 * We need to split the extent, and clear the bit
701 * on the first half
702 */
703 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000704 prealloc = alloc_extent_state_atomic(prealloc);
705 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500706 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400707 if (err)
708 extent_io_tree_panic(tree, err);
709
Chris Masond1310b22008-01-24 16:13:08 -0500710 if (wake)
711 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400712
Jeff Mahoney6763af82012-03-01 14:56:29 +0100713 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400714
Chris Masond1310b22008-01-24 16:13:08 -0500715 prealloc = NULL;
716 goto out;
717 }
Chris Mason42daec22009-09-23 19:51:09 -0400718
Li Zefancdc6a392012-03-12 16:39:48 +0800719 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800720next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400721 if (last_end == (u64)-1)
722 goto out;
723 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800724 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800725 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500726 goto search_again;
727
728out:
Chris Masoncad321a2008-12-17 14:51:42 -0500729 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500730 if (prealloc)
731 free_extent_state(prealloc);
732
Jeff Mahoney6763af82012-03-01 14:56:29 +0100733 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500734
735search_again:
736 if (start > end)
737 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500738 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500739 if (mask & __GFP_WAIT)
740 cond_resched();
741 goto again;
742}
Chris Masond1310b22008-01-24 16:13:08 -0500743
Jeff Mahoney143bede2012-03-01 14:56:26 +0100744static void wait_on_state(struct extent_io_tree *tree,
745 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500746 __releases(tree->lock)
747 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500748{
749 DEFINE_WAIT(wait);
750 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500751 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500752 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500753 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500754 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500755}
756
757/*
758 * waits for one or more bits to clear on a range in the state tree.
759 * The range [start, end] is inclusive.
760 * The tree lock is taken by this function
761 */
David Sterba41074882013-04-29 13:38:46 +0000762static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
763 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500764{
765 struct extent_state *state;
766 struct rb_node *node;
767
Josef Bacika5dee372013-12-13 10:02:44 -0500768 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000769
Chris Masoncad321a2008-12-17 14:51:42 -0500770 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500771again:
772 while (1) {
773 /*
774 * this search will find all the extents that end after
775 * our range starts
776 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500777 node = tree_search(tree, start);
Filipe Mananac50d3e72014-03-31 14:53:25 +0100778process_node:
Chris Masond1310b22008-01-24 16:13:08 -0500779 if (!node)
780 break;
781
782 state = rb_entry(node, struct extent_state, rb_node);
783
784 if (state->start > end)
785 goto out;
786
787 if (state->state & bits) {
788 start = state->start;
789 atomic_inc(&state->refs);
790 wait_on_state(tree, state);
791 free_extent_state(state);
792 goto again;
793 }
794 start = state->end + 1;
795
796 if (start > end)
797 break;
798
Filipe Mananac50d3e72014-03-31 14:53:25 +0100799 if (!cond_resched_lock(&tree->lock)) {
800 node = rb_next(node);
801 goto process_node;
802 }
Chris Masond1310b22008-01-24 16:13:08 -0500803 }
804out:
Chris Masoncad321a2008-12-17 14:51:42 -0500805 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500806}
Chris Masond1310b22008-01-24 16:13:08 -0500807
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000808static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500809 struct extent_state *state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800810 unsigned *bits, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500811{
David Sterba9ee49a042015-01-14 19:52:13 +0100812 unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400813
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000814 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400815 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500816 u64 range = state->end - state->start + 1;
817 tree->dirty_bytes += range;
818 }
Qu Wenruod38ed272015-10-12 14:53:37 +0800819 add_extent_changeset(state, bits_to_set, changeset, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400820 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500821}
822
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100823static void cache_state_if_flags(struct extent_state *state,
824 struct extent_state **cached_ptr,
David Sterba9ee49a042015-01-14 19:52:13 +0100825 unsigned flags)
Chris Mason2c64c532009-09-02 15:04:12 -0400826{
827 if (cached_ptr && !(*cached_ptr)) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100828 if (!flags || (state->state & flags)) {
Chris Mason2c64c532009-09-02 15:04:12 -0400829 *cached_ptr = state;
830 atomic_inc(&state->refs);
831 }
832 }
833}
834
Filipe Mananae38e2ed2014-10-13 12:28:38 +0100835static void cache_state(struct extent_state *state,
836 struct extent_state **cached_ptr)
837{
838 return cache_state_if_flags(state, cached_ptr,
839 EXTENT_IOBITS | EXTENT_BOUNDARY);
840}
841
Chris Masond1310b22008-01-24 16:13:08 -0500842/*
Chris Mason1edbb732009-09-02 13:24:36 -0400843 * set some bits on a range in the tree. This may require allocations or
844 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500845 *
Chris Mason1edbb732009-09-02 13:24:36 -0400846 * If any of the exclusive bits are set, this will fail with -EEXIST if some
847 * part of the range already has the desired bits set. The start of the
848 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500849 *
Chris Mason1edbb732009-09-02 13:24:36 -0400850 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500851 */
Chris Mason1edbb732009-09-02 13:24:36 -0400852
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100853static int __must_check
854__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +0100855 unsigned bits, unsigned exclusive_bits,
David Sterba41074882013-04-29 13:38:46 +0000856 u64 *failed_start, struct extent_state **cached_state,
Qu Wenruod38ed272015-10-12 14:53:37 +0800857 gfp_t mask, struct extent_changeset *changeset)
Chris Masond1310b22008-01-24 16:13:08 -0500858{
859 struct extent_state *state;
860 struct extent_state *prealloc = NULL;
861 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000862 struct rb_node **p;
863 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500864 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500865 u64 last_start;
866 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400867
Josef Bacika5dee372013-12-13 10:02:44 -0500868 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +0000869
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400870 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500871again:
872 if (!prealloc && (mask & __GFP_WAIT)) {
873 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000874 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500875 }
876
Chris Masoncad321a2008-12-17 14:51:42 -0500877 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400878 if (cached_state && *cached_state) {
879 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400880 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +0100881 extent_state_in_tree(state)) {
Chris Mason9655d292009-09-02 15:22:30 -0400882 node = &state->rb_node;
883 goto hit_next;
884 }
885 }
Chris Masond1310b22008-01-24 16:13:08 -0500886 /*
887 * this search will find all the extents that end after
888 * our range starts.
889 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000890 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500891 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000892 prealloc = alloc_extent_state_atomic(prealloc);
893 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000894 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +0800895 &p, &parent, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400896 if (err)
897 extent_io_tree_panic(tree, err);
898
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000899 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500900 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500901 goto out;
902 }
Chris Masond1310b22008-01-24 16:13:08 -0500903 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400904hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500905 last_start = state->start;
906 last_end = state->end;
907
908 /*
909 * | ---- desired range ---- |
910 * | state |
911 *
912 * Just lock what we found and keep going
913 */
914 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400915 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500916 *failed_start = state->start;
917 err = -EEXIST;
918 goto out;
919 }
Chris Mason42daec22009-09-23 19:51:09 -0400920
Qu Wenruod38ed272015-10-12 14:53:37 +0800921 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400922 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500923 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400924 if (last_end == (u64)-1)
925 goto out;
926 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800927 state = next_state(state);
928 if (start < end && state && state->start == start &&
929 !need_resched())
930 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500931 goto search_again;
932 }
933
934 /*
935 * | ---- desired range ---- |
936 * | state |
937 * or
938 * | ------------- state -------------- |
939 *
940 * We need to split the extent we found, and may flip bits on
941 * second half.
942 *
943 * If the extent we found extends past our
944 * range, we just split and search again. It'll get split
945 * again the next time though.
946 *
947 * If the extent we found is inside our range, we set the
948 * desired bit on it.
949 */
950 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400951 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500952 *failed_start = start;
953 err = -EEXIST;
954 goto out;
955 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000956
957 prealloc = alloc_extent_state_atomic(prealloc);
958 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500959 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400960 if (err)
961 extent_io_tree_panic(tree, err);
962
Chris Masond1310b22008-01-24 16:13:08 -0500963 prealloc = NULL;
964 if (err)
965 goto out;
966 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +0800967 set_state_bits(tree, state, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -0400968 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500969 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400970 if (last_end == (u64)-1)
971 goto out;
972 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800973 state = next_state(state);
974 if (start < end && state && state->start == start &&
975 !need_resched())
976 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500977 }
978 goto search_again;
979 }
980 /*
981 * | ---- desired range ---- |
982 * | state | or | state |
983 *
984 * There's a hole, we need to insert something in it and
985 * ignore the extent we found.
986 */
987 if (state->start > start) {
988 u64 this_end;
989 if (end < last_start)
990 this_end = end;
991 else
Chris Masond3977122009-01-05 21:25:51 -0500992 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000993
994 prealloc = alloc_extent_state_atomic(prealloc);
995 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000996
997 /*
998 * Avoid to free 'prealloc' if it can be merged with
999 * the later extent.
1000 */
Chris Masond1310b22008-01-24 16:13:08 -05001001 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001002 NULL, NULL, &bits, changeset);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001003 if (err)
1004 extent_io_tree_panic(tree, err);
1005
Chris Mason2c64c532009-09-02 15:04:12 -04001006 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001007 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001008 start = this_end + 1;
1009 goto search_again;
1010 }
1011 /*
1012 * | ---- desired range ---- |
1013 * | state |
1014 * We need to split the extent, and set the bit
1015 * on the first half
1016 */
1017 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -04001018 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001019 *failed_start = start;
1020 err = -EEXIST;
1021 goto out;
1022 }
Xiao Guangrong82337672011-04-20 06:44:57 +00001023
1024 prealloc = alloc_extent_state_atomic(prealloc);
1025 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -05001026 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001027 if (err)
1028 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -05001029
Qu Wenruod38ed272015-10-12 14:53:37 +08001030 set_state_bits(tree, prealloc, &bits, changeset);
Chris Mason2c64c532009-09-02 15:04:12 -04001031 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05001032 merge_state(tree, prealloc);
1033 prealloc = NULL;
1034 goto out;
1035 }
1036
1037 goto search_again;
1038
1039out:
Chris Masoncad321a2008-12-17 14:51:42 -05001040 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001041 if (prealloc)
1042 free_extent_state(prealloc);
1043
1044 return err;
1045
1046search_again:
1047 if (start > end)
1048 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -05001049 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001050 if (mask & __GFP_WAIT)
1051 cond_resched();
1052 goto again;
1053}
Chris Masond1310b22008-01-24 16:13:08 -05001054
David Sterba41074882013-04-29 13:38:46 +00001055int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001056 unsigned bits, u64 * failed_start,
David Sterba41074882013-04-29 13:38:46 +00001057 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001058{
1059 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001060 cached_state, mask, NULL);
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001061}
1062
1063
Josef Bacik462d6fa2011-09-26 13:56:12 -04001064/**
Liu Bo10983f22012-07-11 15:26:19 +08001065 * convert_extent_bit - convert all bits in a given range from one bit to
1066 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001067 * @tree: the io tree to search
1068 * @start: the start offset in bytes
1069 * @end: the end offset in bytes (inclusive)
1070 * @bits: the bits to set in this range
1071 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001072 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001073 * @mask: the allocation mask
1074 *
1075 * This will go through and set bits for the given range. If any states exist
1076 * already in this range they are set with the given bit and cleared of the
1077 * clear_bits. This is only meant to be used by things that are mergeable, ie
1078 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1079 * boundary bits like LOCK.
1080 */
1081int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001082 unsigned bits, unsigned clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -04001083 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084{
1085 struct extent_state *state;
1086 struct extent_state *prealloc = NULL;
1087 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001088 struct rb_node **p;
1089 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090 int err = 0;
1091 u64 last_start;
1092 u64 last_end;
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001093 bool first_iteration = true;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094
Josef Bacika5dee372013-12-13 10:02:44 -05001095 btrfs_debug_check_extent_io_range(tree, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +00001096
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097again:
1098 if (!prealloc && (mask & __GFP_WAIT)) {
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001099 /*
1100 * Best effort, don't worry if extent state allocation fails
1101 * here for the first iteration. We might have a cached state
1102 * that matches exactly the target range, in which case no
1103 * extent state allocations are needed. We'll only know this
1104 * after locking the tree.
1105 */
Josef Bacik462d6fa2011-09-26 13:56:12 -04001106 prealloc = alloc_extent_state(mask);
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001107 if (!prealloc && !first_iteration)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108 return -ENOMEM;
1109 }
1110
1111 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001112 if (cached_state && *cached_state) {
1113 state = *cached_state;
1114 if (state->start <= start && state->end > start &&
Filipe Manana27a35072014-07-06 20:09:59 +01001115 extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001116 node = &state->rb_node;
1117 goto hit_next;
1118 }
1119 }
1120
Josef Bacik462d6fa2011-09-26 13:56:12 -04001121 /*
1122 * this search will find all the extents that end after
1123 * our range starts.
1124 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001125 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001126 if (!node) {
1127 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001128 if (!prealloc) {
1129 err = -ENOMEM;
1130 goto out;
1131 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001132 err = insert_state(tree, prealloc, start, end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001133 &p, &parent, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001134 if (err)
1135 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001136 cache_state(prealloc, cached_state);
1137 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001138 goto out;
1139 }
1140 state = rb_entry(node, struct extent_state, rb_node);
1141hit_next:
1142 last_start = state->start;
1143 last_end = state->end;
1144
1145 /*
1146 * | ---- desired range ---- |
1147 * | state |
1148 *
1149 * Just lock what we found and keep going
1150 */
1151 if (state->start == start && state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001152 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001153 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001154 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001155 if (last_end == (u64)-1)
1156 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001157 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001158 if (start < end && state && state->start == start &&
1159 !need_resched())
1160 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001161 goto search_again;
1162 }
1163
1164 /*
1165 * | ---- desired range ---- |
1166 * | state |
1167 * or
1168 * | ------------- state -------------- |
1169 *
1170 * We need to split the extent we found, and may flip bits on
1171 * second half.
1172 *
1173 * If the extent we found extends past our
1174 * range, we just split and search again. It'll get split
1175 * again the next time though.
1176 *
1177 * If the extent we found is inside our range, we set the
1178 * desired bit on it.
1179 */
1180 if (state->start < start) {
1181 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001182 if (!prealloc) {
1183 err = -ENOMEM;
1184 goto out;
1185 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001186 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001187 if (err)
1188 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001189 prealloc = NULL;
1190 if (err)
1191 goto out;
1192 if (state->end <= end) {
Qu Wenruod38ed272015-10-12 14:53:37 +08001193 set_state_bits(tree, state, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001194 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001195 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001196 if (last_end == (u64)-1)
1197 goto out;
1198 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001199 if (start < end && state && state->start == start &&
1200 !need_resched())
1201 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001202 }
1203 goto search_again;
1204 }
1205 /*
1206 * | ---- desired range ---- |
1207 * | state | or | state |
1208 *
1209 * There's a hole, we need to insert something in it and
1210 * ignore the extent we found.
1211 */
1212 if (state->start > start) {
1213 u64 this_end;
1214 if (end < last_start)
1215 this_end = end;
1216 else
1217 this_end = last_start - 1;
1218
1219 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001220 if (!prealloc) {
1221 err = -ENOMEM;
1222 goto out;
1223 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001224
1225 /*
1226 * Avoid to free 'prealloc' if it can be merged with
1227 * the later extent.
1228 */
1229 err = insert_state(tree, prealloc, start, this_end,
Qu Wenruod38ed272015-10-12 14:53:37 +08001230 NULL, NULL, &bits, NULL);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001231 if (err)
1232 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001233 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001234 prealloc = NULL;
1235 start = this_end + 1;
1236 goto search_again;
1237 }
1238 /*
1239 * | ---- desired range ---- |
1240 * | state |
1241 * We need to split the extent, and set the bit
1242 * on the first half
1243 */
1244 if (state->start <= end && state->end > end) {
1245 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001246 if (!prealloc) {
1247 err = -ENOMEM;
1248 goto out;
1249 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001250
1251 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001252 if (err)
1253 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001254
Qu Wenruod38ed272015-10-12 14:53:37 +08001255 set_state_bits(tree, prealloc, &bits, NULL);
Josef Bacike6138872012-09-27 17:07:30 -04001256 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001257 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001258 prealloc = NULL;
1259 goto out;
1260 }
1261
1262 goto search_again;
1263
1264out:
1265 spin_unlock(&tree->lock);
1266 if (prealloc)
1267 free_extent_state(prealloc);
1268
1269 return err;
1270
1271search_again:
1272 if (start > end)
1273 goto out;
1274 spin_unlock(&tree->lock);
1275 if (mask & __GFP_WAIT)
1276 cond_resched();
Filipe Mananac8fd3de2014-10-13 12:28:39 +01001277 first_iteration = false;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001278 goto again;
1279}
1280
Chris Masond1310b22008-01-24 16:13:08 -05001281/* wrappers around set/clear extent bit */
1282int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1283 gfp_t mask)
1284{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001285 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001286 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001287}
Chris Masond1310b22008-01-24 16:13:08 -05001288
1289int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001290 unsigned bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001291{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001292 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001293 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001294}
Chris Masond1310b22008-01-24 16:13:08 -05001295
Qu Wenruod38ed272015-10-12 14:53:37 +08001296int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1297 unsigned bits, gfp_t mask,
1298 struct extent_changeset *changeset)
1299{
1300 /*
1301 * We don't support EXTENT_LOCKED yet, as current changeset will
1302 * record any bits changed, so for EXTENT_LOCKED case, it will
1303 * either fail with -EEXIST or changeset will record the whole
1304 * range.
1305 */
1306 BUG_ON(bits & EXTENT_LOCKED);
1307
1308 return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, mask,
1309 changeset);
1310}
1311
Chris Masond1310b22008-01-24 16:13:08 -05001312int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001313 unsigned bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001314{
Filipe Manana0f318712015-05-14 20:41:07 +01001315 int wake = 0;
1316
1317 if (bits & EXTENT_LOCKED)
1318 wake = 1;
1319
1320 return clear_extent_bit(tree, start, end, bits, wake, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001321}
Chris Masond1310b22008-01-24 16:13:08 -05001322
1323int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001324 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001325{
1326 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001327 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001328 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001329}
Chris Masond1310b22008-01-24 16:13:08 -05001330
Liu Bo9e8a4a82012-09-05 19:10:51 -06001331int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1332 struct extent_state **cached_state, gfp_t mask)
1333{
1334 return set_extent_bit(tree, start, end,
1335 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1336 NULL, cached_state, mask);
1337}
1338
Chris Masond1310b22008-01-24 16:13:08 -05001339int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1340 gfp_t mask)
1341{
1342 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001343 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001344 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001345}
Chris Masond1310b22008-01-24 16:13:08 -05001346
1347int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1348 gfp_t mask)
1349{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001350 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001351 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001352}
Chris Masond1310b22008-01-24 16:13:08 -05001353
Chris Masond1310b22008-01-24 16:13:08 -05001354int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001355 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001356{
Liu Bo6b67a322013-03-28 08:30:28 +00001357 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001358 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001359}
Chris Masond1310b22008-01-24 16:13:08 -05001360
Josef Bacik5fd02042012-05-02 14:00:54 -04001361int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1362 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001363{
Chris Mason2c64c532009-09-02 15:04:12 -04001364 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001365 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001366}
Chris Masond1310b22008-01-24 16:13:08 -05001367
Chris Masond352ac62008-09-29 15:18:18 -04001368/*
1369 * either insert or lock state struct between start and end use mask to tell
1370 * us if waiting is desired.
1371 */
Chris Mason1edbb732009-09-02 13:24:36 -04001372int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001373 unsigned bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001374{
1375 int err;
1376 u64 failed_start;
David Sterba9ee49a042015-01-14 19:52:13 +01001377
Chris Masond1310b22008-01-24 16:13:08 -05001378 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001379 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1380 EXTENT_LOCKED, &failed_start,
Qu Wenruod38ed272015-10-12 14:53:37 +08001381 cached_state, GFP_NOFS, NULL);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001382 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001383 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1384 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001385 } else
Chris Masond1310b22008-01-24 16:13:08 -05001386 break;
Chris Masond1310b22008-01-24 16:13:08 -05001387 WARN_ON(start > end);
1388 }
1389 return err;
1390}
Chris Masond1310b22008-01-24 16:13:08 -05001391
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001392int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001393{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001394 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001395}
1396
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001397int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001398{
1399 int err;
1400 u64 failed_start;
1401
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001402 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
Qu Wenruod38ed272015-10-12 14:53:37 +08001403 &failed_start, NULL, GFP_NOFS, NULL);
Yan Zheng66435582008-10-30 14:19:50 -04001404 if (err == -EEXIST) {
1405 if (failed_start > start)
1406 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001407 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001408 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001409 }
Josef Bacik25179202008-10-29 14:49:05 -04001410 return 1;
1411}
Josef Bacik25179202008-10-29 14:49:05 -04001412
Chris Mason2c64c532009-09-02 15:04:12 -04001413int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1414 struct extent_state **cached, gfp_t mask)
1415{
1416 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1417 mask);
1418}
1419
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001420int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001421{
Chris Mason2c64c532009-09-02 15:04:12 -04001422 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001423 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001424}
Chris Masond1310b22008-01-24 16:13:08 -05001425
Chris Mason4adaa612013-03-26 13:07:00 -04001426int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1427{
1428 unsigned long index = start >> PAGE_CACHE_SHIFT;
1429 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1430 struct page *page;
1431
1432 while (index <= end_index) {
1433 page = find_get_page(inode->i_mapping, index);
1434 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1435 clear_page_dirty_for_io(page);
1436 page_cache_release(page);
1437 index++;
1438 }
1439 return 0;
1440}
1441
1442int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1443{
1444 unsigned long index = start >> PAGE_CACHE_SHIFT;
1445 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1446 struct page *page;
1447
1448 while (index <= end_index) {
1449 page = find_get_page(inode->i_mapping, index);
1450 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Mason4adaa612013-03-26 13:07:00 -04001451 __set_page_dirty_nobuffers(page);
Konstantin Khebnikov8d386332015-02-11 15:26:55 -08001452 account_page_redirty(page);
Chris Mason4adaa612013-03-26 13:07:00 -04001453 page_cache_release(page);
1454 index++;
1455 }
1456 return 0;
1457}
1458
Chris Masond1310b22008-01-24 16:13:08 -05001459/*
Chris Masond1310b22008-01-24 16:13:08 -05001460 * helper function to set both pages and extents in the tree writeback
1461 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001462static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001463{
1464 unsigned long index = start >> PAGE_CACHE_SHIFT;
1465 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1466 struct page *page;
1467
1468 while (index <= end_index) {
1469 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001470 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001471 set_page_writeback(page);
1472 page_cache_release(page);
1473 index++;
1474 }
Chris Masond1310b22008-01-24 16:13:08 -05001475 return 0;
1476}
Chris Masond1310b22008-01-24 16:13:08 -05001477
Chris Masond352ac62008-09-29 15:18:18 -04001478/* find the first state struct with 'bits' set after 'start', and
1479 * return it. tree->lock must be held. NULL will returned if
1480 * nothing was found after 'start'
1481 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001482static struct extent_state *
1483find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba9ee49a042015-01-14 19:52:13 +01001484 u64 start, unsigned bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001485{
1486 struct rb_node *node;
1487 struct extent_state *state;
1488
1489 /*
1490 * this search will find all the extents that end after
1491 * our range starts.
1492 */
1493 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001494 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001495 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001496
Chris Masond3977122009-01-05 21:25:51 -05001497 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001498 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001499 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001500 return state;
Chris Masond3977122009-01-05 21:25:51 -05001501
Chris Masond7fc6402008-02-18 12:12:38 -05001502 node = rb_next(node);
1503 if (!node)
1504 break;
1505 }
1506out:
1507 return NULL;
1508}
Chris Masond7fc6402008-02-18 12:12:38 -05001509
Chris Masond352ac62008-09-29 15:18:18 -04001510/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001511 * find the first offset in the io tree with 'bits' set. zero is
1512 * returned if we find something, and *start_ret and *end_ret are
1513 * set to reflect the state struct that was found.
1514 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001515 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001516 */
1517int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba9ee49a042015-01-14 19:52:13 +01001518 u64 *start_ret, u64 *end_ret, unsigned bits,
Josef Bacike6138872012-09-27 17:07:30 -04001519 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001520{
1521 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001522 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001523 int ret = 1;
1524
1525 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001526 if (cached_state && *cached_state) {
1527 state = *cached_state;
Filipe Manana27a35072014-07-06 20:09:59 +01001528 if (state->end == start - 1 && extent_state_in_tree(state)) {
Josef Bacike6138872012-09-27 17:07:30 -04001529 n = rb_next(&state->rb_node);
1530 while (n) {
1531 state = rb_entry(n, struct extent_state,
1532 rb_node);
1533 if (state->state & bits)
1534 goto got_it;
1535 n = rb_next(n);
1536 }
1537 free_extent_state(*cached_state);
1538 *cached_state = NULL;
1539 goto out;
1540 }
1541 free_extent_state(*cached_state);
1542 *cached_state = NULL;
1543 }
1544
Xiao Guangrong69261c42011-07-14 03:19:45 +00001545 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001546got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001547 if (state) {
Filipe Mananae38e2ed2014-10-13 12:28:38 +01001548 cache_state_if_flags(state, cached_state, 0);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001549 *start_ret = state->start;
1550 *end_ret = state->end;
1551 ret = 0;
1552 }
Josef Bacike6138872012-09-27 17:07:30 -04001553out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001554 spin_unlock(&tree->lock);
1555 return ret;
1556}
1557
1558/*
Chris Masond352ac62008-09-29 15:18:18 -04001559 * find a contiguous range of bytes in the file marked as delalloc, not
1560 * more than 'max_bytes'. start and end are used to return the range,
1561 *
1562 * 1 is returned if we find something, 0 if nothing was in the tree
1563 */
Chris Masonc8b97812008-10-29 14:49:59 -04001564static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001565 u64 *start, u64 *end, u64 max_bytes,
1566 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001567{
1568 struct rb_node *node;
1569 struct extent_state *state;
1570 u64 cur_start = *start;
1571 u64 found = 0;
1572 u64 total_bytes = 0;
1573
Chris Masoncad321a2008-12-17 14:51:42 -05001574 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001575
Chris Masond1310b22008-01-24 16:13:08 -05001576 /*
1577 * this search will find all the extents that end after
1578 * our range starts.
1579 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001580 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001581 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001582 if (!found)
1583 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001584 goto out;
1585 }
1586
Chris Masond3977122009-01-05 21:25:51 -05001587 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001588 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001589 if (found && (state->start != cur_start ||
1590 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001591 goto out;
1592 }
1593 if (!(state->state & EXTENT_DELALLOC)) {
1594 if (!found)
1595 *end = state->end;
1596 goto out;
1597 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001598 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001599 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001600 *cached_state = state;
1601 atomic_inc(&state->refs);
1602 }
Chris Masond1310b22008-01-24 16:13:08 -05001603 found++;
1604 *end = state->end;
1605 cur_start = state->end + 1;
1606 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001607 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001608 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001609 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001610 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001611 break;
1612 }
1613out:
Chris Masoncad321a2008-12-17 14:51:42 -05001614 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001615 return found;
1616}
1617
Jeff Mahoney143bede2012-03-01 14:56:26 +01001618static noinline void __unlock_for_delalloc(struct inode *inode,
1619 struct page *locked_page,
1620 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001621{
1622 int ret;
1623 struct page *pages[16];
1624 unsigned long index = start >> PAGE_CACHE_SHIFT;
1625 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1626 unsigned long nr_pages = end_index - index + 1;
1627 int i;
1628
1629 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001630 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001631
Chris Masond3977122009-01-05 21:25:51 -05001632 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001633 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001634 min_t(unsigned long, nr_pages,
1635 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001636 for (i = 0; i < ret; i++) {
1637 if (pages[i] != locked_page)
1638 unlock_page(pages[i]);
1639 page_cache_release(pages[i]);
1640 }
1641 nr_pages -= ret;
1642 index += ret;
1643 cond_resched();
1644 }
Chris Masonc8b97812008-10-29 14:49:59 -04001645}
1646
1647static noinline int lock_delalloc_pages(struct inode *inode,
1648 struct page *locked_page,
1649 u64 delalloc_start,
1650 u64 delalloc_end)
1651{
1652 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1653 unsigned long start_index = index;
1654 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1655 unsigned long pages_locked = 0;
1656 struct page *pages[16];
1657 unsigned long nrpages;
1658 int ret;
1659 int i;
1660
1661 /* the caller is responsible for locking the start index */
1662 if (index == locked_page->index && index == end_index)
1663 return 0;
1664
1665 /* skip the page at the start index */
1666 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001667 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001668 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001669 min_t(unsigned long,
1670 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001671 if (ret == 0) {
1672 ret = -EAGAIN;
1673 goto done;
1674 }
1675 /* now we have an array of pages, lock them all */
1676 for (i = 0; i < ret; i++) {
1677 /*
1678 * the caller is taking responsibility for
1679 * locked_page
1680 */
Chris Mason771ed682008-11-06 22:02:51 -05001681 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001682 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001683 if (!PageDirty(pages[i]) ||
1684 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001685 ret = -EAGAIN;
1686 unlock_page(pages[i]);
1687 page_cache_release(pages[i]);
1688 goto done;
1689 }
1690 }
Chris Masonc8b97812008-10-29 14:49:59 -04001691 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001692 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001693 }
Chris Masonc8b97812008-10-29 14:49:59 -04001694 nrpages -= ret;
1695 index += ret;
1696 cond_resched();
1697 }
1698 ret = 0;
1699done:
1700 if (ret && pages_locked) {
1701 __unlock_for_delalloc(inode, locked_page,
1702 delalloc_start,
1703 ((u64)(start_index + pages_locked - 1)) <<
1704 PAGE_CACHE_SHIFT);
1705 }
1706 return ret;
1707}
1708
1709/*
1710 * find a contiguous range of bytes in the file marked as delalloc, not
1711 * more than 'max_bytes'. start and end are used to return the range,
1712 *
1713 * 1 is returned if we find something, 0 if nothing was in the tree
1714 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001715STATIC u64 find_lock_delalloc_range(struct inode *inode,
1716 struct extent_io_tree *tree,
1717 struct page *locked_page, u64 *start,
1718 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001719{
1720 u64 delalloc_start;
1721 u64 delalloc_end;
1722 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001723 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001724 int ret;
1725 int loops = 0;
1726
1727again:
1728 /* step one, find a bunch of delalloc bytes starting at start */
1729 delalloc_start = *start;
1730 delalloc_end = 0;
1731 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001732 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001733 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001734 *start = delalloc_start;
1735 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001736 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001737 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001738 }
1739
1740 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001741 * start comes from the offset of locked_page. We have to lock
1742 * pages in order, so we can't process delalloc bytes before
1743 * locked_page
1744 */
Chris Masond3977122009-01-05 21:25:51 -05001745 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001746 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001747
1748 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001749 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001750 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001751 if (delalloc_end + 1 - delalloc_start > max_bytes)
1752 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001753
Chris Masonc8b97812008-10-29 14:49:59 -04001754 /* step two, lock all the pages after the page that has start */
1755 ret = lock_delalloc_pages(inode, locked_page,
1756 delalloc_start, delalloc_end);
1757 if (ret == -EAGAIN) {
1758 /* some of the pages are gone, lets avoid looping by
1759 * shortening the size of the delalloc range we're searching
1760 */
Chris Mason9655d292009-09-02 15:22:30 -04001761 free_extent_state(cached_state);
Chris Mason7d788742014-05-21 05:49:54 -07001762 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001763 if (!loops) {
Josef Bacik7bf811a52013-10-07 22:11:09 -04001764 max_bytes = PAGE_CACHE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001765 loops = 1;
1766 goto again;
1767 } else {
1768 found = 0;
1769 goto out_failed;
1770 }
1771 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001772 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001773
1774 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001775 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001776
1777 /* then test to make sure it is all still delalloc */
1778 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001779 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001780 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001781 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1782 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001783 __unlock_for_delalloc(inode, locked_page,
1784 delalloc_start, delalloc_end);
1785 cond_resched();
1786 goto again;
1787 }
Chris Mason9655d292009-09-02 15:22:30 -04001788 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001789 *start = delalloc_start;
1790 *end = delalloc_end;
1791out_failed:
1792 return found;
1793}
1794
Josef Bacikc2790a22013-07-29 11:20:47 -04001795int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1796 struct page *locked_page,
David Sterba9ee49a042015-01-14 19:52:13 +01001797 unsigned clear_bits,
Josef Bacikc2790a22013-07-29 11:20:47 -04001798 unsigned long page_ops)
Chris Masonc8b97812008-10-29 14:49:59 -04001799{
Josef Bacikc2790a22013-07-29 11:20:47 -04001800 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Masonc8b97812008-10-29 14:49:59 -04001801 int ret;
1802 struct page *pages[16];
1803 unsigned long index = start >> PAGE_CACHE_SHIFT;
1804 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1805 unsigned long nr_pages = end_index - index + 1;
1806 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001807
Chris Mason2c64c532009-09-02 15:04:12 -04001808 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacikc2790a22013-07-29 11:20:47 -04001809 if (page_ops == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001810 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001811
Filipe Manana704de492014-10-06 22:14:22 +01001812 if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
1813 mapping_set_error(inode->i_mapping, -EIO);
1814
Chris Masond3977122009-01-05 21:25:51 -05001815 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001816 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001817 min_t(unsigned long,
1818 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001819 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001820
Josef Bacikc2790a22013-07-29 11:20:47 -04001821 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001822 SetPagePrivate2(pages[i]);
1823
Chris Masonc8b97812008-10-29 14:49:59 -04001824 if (pages[i] == locked_page) {
1825 page_cache_release(pages[i]);
1826 continue;
1827 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001828 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001829 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001830 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001831 set_page_writeback(pages[i]);
Filipe Manana704de492014-10-06 22:14:22 +01001832 if (page_ops & PAGE_SET_ERROR)
1833 SetPageError(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001834 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001835 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001836 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001837 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001838 page_cache_release(pages[i]);
1839 }
1840 nr_pages -= ret;
1841 index += ret;
1842 cond_resched();
1843 }
1844 return 0;
1845}
Chris Masonc8b97812008-10-29 14:49:59 -04001846
Chris Masond352ac62008-09-29 15:18:18 -04001847/*
1848 * count the number of bytes in the tree that have a given bit(s)
1849 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1850 * cached. The total number found is returned.
1851 */
Chris Masond1310b22008-01-24 16:13:08 -05001852u64 count_range_bits(struct extent_io_tree *tree,
1853 u64 *start, u64 search_end, u64 max_bytes,
David Sterba9ee49a042015-01-14 19:52:13 +01001854 unsigned bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001855{
1856 struct rb_node *node;
1857 struct extent_state *state;
1858 u64 cur_start = *start;
1859 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001860 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001861 int found = 0;
1862
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301863 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001864 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001865
Chris Masoncad321a2008-12-17 14:51:42 -05001866 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001867 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1868 total_bytes = tree->dirty_bytes;
1869 goto out;
1870 }
1871 /*
1872 * this search will find all the extents that end after
1873 * our range starts.
1874 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001875 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001876 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001877 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001878
Chris Masond3977122009-01-05 21:25:51 -05001879 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001880 state = rb_entry(node, struct extent_state, rb_node);
1881 if (state->start > search_end)
1882 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001883 if (contig && found && state->start > last + 1)
1884 break;
1885 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001886 total_bytes += min(search_end, state->end) + 1 -
1887 max(cur_start, state->start);
1888 if (total_bytes >= max_bytes)
1889 break;
1890 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001891 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001892 found = 1;
1893 }
Chris Masonec29ed52011-02-23 16:23:20 -05001894 last = state->end;
1895 } else if (contig && found) {
1896 break;
Chris Masond1310b22008-01-24 16:13:08 -05001897 }
1898 node = rb_next(node);
1899 if (!node)
1900 break;
1901 }
1902out:
Chris Masoncad321a2008-12-17 14:51:42 -05001903 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001904 return total_bytes;
1905}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001906
Chris Masond352ac62008-09-29 15:18:18 -04001907/*
1908 * set the private field for a given byte offset in the tree. If there isn't
1909 * an extent_state there already, this does nothing.
1910 */
Sergei Trofimovich171170c2013-08-14 23:27:46 +03001911static int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
Chris Masond1310b22008-01-24 16:13:08 -05001912{
1913 struct rb_node *node;
1914 struct extent_state *state;
1915 int ret = 0;
1916
Chris Masoncad321a2008-12-17 14:51:42 -05001917 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001918 /*
1919 * this search will find all the extents that end after
1920 * our range starts.
1921 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001922 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001923 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001924 ret = -ENOENT;
1925 goto out;
1926 }
1927 state = rb_entry(node, struct extent_state, rb_node);
1928 if (state->start != start) {
1929 ret = -ENOENT;
1930 goto out;
1931 }
1932 state->private = private;
1933out:
Chris Masoncad321a2008-12-17 14:51:42 -05001934 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001935 return ret;
1936}
1937
1938int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1939{
1940 struct rb_node *node;
1941 struct extent_state *state;
1942 int ret = 0;
1943
Chris Masoncad321a2008-12-17 14:51:42 -05001944 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001945 /*
1946 * this search will find all the extents that end after
1947 * our range starts.
1948 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001949 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001950 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001951 ret = -ENOENT;
1952 goto out;
1953 }
1954 state = rb_entry(node, struct extent_state, rb_node);
1955 if (state->start != start) {
1956 ret = -ENOENT;
1957 goto out;
1958 }
1959 *private = state->private;
1960out:
Chris Masoncad321a2008-12-17 14:51:42 -05001961 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001962 return ret;
1963}
1964
1965/*
1966 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001967 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001968 * has the bits set. Otherwise, 1 is returned if any bit in the
1969 * range is found set.
1970 */
1971int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba9ee49a042015-01-14 19:52:13 +01001972 unsigned bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001973{
1974 struct extent_state *state = NULL;
1975 struct rb_node *node;
1976 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001977
Chris Masoncad321a2008-12-17 14:51:42 -05001978 spin_lock(&tree->lock);
Filipe Manana27a35072014-07-06 20:09:59 +01001979 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001980 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001981 node = &cached->rb_node;
1982 else
1983 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001984 while (node && start <= end) {
1985 state = rb_entry(node, struct extent_state, rb_node);
1986
1987 if (filled && state->start > start) {
1988 bitset = 0;
1989 break;
1990 }
1991
1992 if (state->start > end)
1993 break;
1994
1995 if (state->state & bits) {
1996 bitset = 1;
1997 if (!filled)
1998 break;
1999 } else if (filled) {
2000 bitset = 0;
2001 break;
2002 }
Chris Mason46562cec2009-09-23 20:23:16 -04002003
2004 if (state->end == (u64)-1)
2005 break;
2006
Chris Masond1310b22008-01-24 16:13:08 -05002007 start = state->end + 1;
2008 if (start > end)
2009 break;
2010 node = rb_next(node);
2011 if (!node) {
2012 if (filled)
2013 bitset = 0;
2014 break;
2015 }
2016 }
Chris Masoncad321a2008-12-17 14:51:42 -05002017 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05002018 return bitset;
2019}
Chris Masond1310b22008-01-24 16:13:08 -05002020
2021/*
2022 * helper function to set a given page up to date if all the
2023 * extents in the tree for that page are up to date
2024 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002025static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05002026{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002027 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002028 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04002029 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05002030 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002031}
2032
Miao Xie8b110e32014-09-12 18:44:03 +08002033int free_io_failure(struct inode *inode, struct io_failure_record *rec)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002034{
2035 int ret;
2036 int err = 0;
2037 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2038
2039 set_state_private(failure_tree, rec->start, 0);
2040 ret = clear_extent_bits(failure_tree, rec->start,
2041 rec->start + rec->len - 1,
2042 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2043 if (ret)
2044 err = ret;
2045
David Woodhouse53b381b2013-01-29 18:40:14 -05002046 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2047 rec->start + rec->len - 1,
2048 EXTENT_DAMAGED, GFP_NOFS);
2049 if (ret && !err)
2050 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002051
2052 kfree(rec);
2053 return err;
2054}
2055
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002056/*
2057 * this bypasses the standard btrfs submit functions deliberately, as
2058 * the standard behavior is to write all copies in a raid setup. here we only
2059 * want to write the one bad copy. so we do the mapping for ourselves and issue
2060 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002061 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002062 * actually prevents the read that triggered the error from finishing.
2063 * currently, there can be no more than two copies of every data bit. thus,
2064 * exactly one rewrite is required.
2065 */
Miao Xie1203b682014-09-12 18:44:01 +08002066int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
2067 struct page *page, unsigned int pg_offset, int mirror_num)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002068{
Miao Xie1203b682014-09-12 18:44:01 +08002069 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002070 struct bio *bio;
2071 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002072 u64 map_length = 0;
2073 u64 sector;
2074 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002075 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002076 int ret;
2077
Ilya Dryomov908960c2013-11-03 19:06:39 +02002078 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002079 BUG_ON(!mirror_num);
2080
David Woodhouse53b381b2013-01-29 18:40:14 -05002081 /* we can't repair anything in raid56 yet */
2082 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2083 return 0;
2084
Chris Mason9be33952013-05-17 18:30:14 -04002085 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002086 if (!bio)
2087 return -EIO;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002088 bio->bi_iter.bi_size = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002089 map_length = length;
2090
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002091 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092 &map_length, &bbio, mirror_num);
2093 if (ret) {
2094 bio_put(bio);
2095 return -EIO;
2096 }
2097 BUG_ON(mirror_num != bbio->mirror_num);
2098 sector = bbio->stripes[mirror_num-1].physical >> 9;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002099 bio->bi_iter.bi_sector = sector;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002100 dev = bbio->stripes[mirror_num-1].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08002101 btrfs_put_bbio(bbio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002102 if (!dev || !dev->bdev || !dev->writeable) {
2103 bio_put(bio);
2104 return -EIO;
2105 }
2106 bio->bi_bdev = dev->bdev;
Miao Xieffdd2012014-09-12 18:44:00 +08002107 bio_add_page(bio, page, length, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108
Kent Overstreet33879d42013-11-23 22:33:32 -08002109 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 /* try to remap that extent elsewhere? */
2111 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002112 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 return -EIO;
2114 }
2115
David Sterbab14af3b2015-10-08 10:43:10 +02002116 btrfs_info_rl_in_rcu(fs_info,
2117 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
Miao Xie1203b682014-09-12 18:44:01 +08002118 btrfs_ino(inode), start,
2119 rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 bio_put(bio);
2121 return 0;
2122}
2123
Josef Bacikea466792012-03-26 21:57:36 -04002124int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2125 int mirror_num)
2126{
Josef Bacikea466792012-03-26 21:57:36 -04002127 u64 start = eb->start;
2128 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002129 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002130
Ilya Dryomov908960c2013-11-03 19:06:39 +02002131 if (root->fs_info->sb->s_flags & MS_RDONLY)
2132 return -EROFS;
2133
Josef Bacikea466792012-03-26 21:57:36 -04002134 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02002135 struct page *p = eb->pages[i];
Miao Xie1203b682014-09-12 18:44:01 +08002136
2137 ret = repair_io_failure(root->fs_info->btree_inode, start,
2138 PAGE_CACHE_SIZE, start, p,
2139 start - page_offset(p), mirror_num);
Josef Bacikea466792012-03-26 21:57:36 -04002140 if (ret)
2141 break;
2142 start += PAGE_CACHE_SIZE;
2143 }
2144
2145 return ret;
2146}
2147
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002148/*
2149 * each time an IO finishes, we do a fast check in the IO failure tree
2150 * to see if we need to process or clean up an io_failure_record
2151 */
Miao Xie8b110e32014-09-12 18:44:03 +08002152int clean_io_failure(struct inode *inode, u64 start, struct page *page,
2153 unsigned int pg_offset)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002154{
2155 u64 private;
2156 u64 private_failure;
2157 struct io_failure_record *failrec;
Ilya Dryomov908960c2013-11-03 19:06:39 +02002158 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002159 struct extent_state *state;
2160 int num_copies;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002161 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002162
2163 private = 0;
2164 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2165 (u64)-1, 1, EXTENT_DIRTY, 0);
2166 if (!ret)
2167 return 0;
2168
2169 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2170 &private_failure);
2171 if (ret)
2172 return 0;
2173
2174 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2175 BUG_ON(!failrec->this_mirror);
2176
2177 if (failrec->in_validation) {
2178 /* there was no real error, just free the record */
2179 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2180 failrec->start);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002181 goto out;
2182 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002183 if (fs_info->sb->s_flags & MS_RDONLY)
2184 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002185
2186 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2187 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2188 failrec->start,
2189 EXTENT_LOCKED);
2190 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2191
Miao Xie883d0de2013-07-25 19:22:35 +08002192 if (state && state->start <= failrec->start &&
2193 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002194 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2195 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002196 if (num_copies > 1) {
Miao Xie1203b682014-09-12 18:44:01 +08002197 repair_io_failure(inode, start, failrec->len,
Miao Xie454ff3d2014-09-12 18:43:58 +08002198 failrec->logical, page,
Miao Xie1203b682014-09-12 18:44:01 +08002199 pg_offset, failrec->failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002200 }
2201 }
2202
2203out:
Miao Xie454ff3d2014-09-12 18:43:58 +08002204 free_io_failure(inode, failrec);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205
Miao Xie454ff3d2014-09-12 18:43:58 +08002206 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002207}
2208
Miao Xief6124962014-09-12 18:44:04 +08002209/*
2210 * Can be called when
2211 * - hold extent lock
2212 * - under ordered extent
2213 * - the inode is freeing
2214 */
2215void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
2216{
2217 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2218 struct io_failure_record *failrec;
2219 struct extent_state *state, *next;
2220
2221 if (RB_EMPTY_ROOT(&failure_tree->state))
2222 return;
2223
2224 spin_lock(&failure_tree->lock);
2225 state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
2226 while (state) {
2227 if (state->start > end)
2228 break;
2229
2230 ASSERT(state->end <= end);
2231
2232 next = next_state(state);
2233
Satoru Takeuchi6e1103a2014-12-25 18:21:41 +09002234 failrec = (struct io_failure_record *)(unsigned long)state->private;
Miao Xief6124962014-09-12 18:44:04 +08002235 free_extent_state(state);
2236 kfree(failrec);
2237
2238 state = next;
2239 }
2240 spin_unlock(&failure_tree->lock);
2241}
2242
Miao Xie2fe63032014-09-12 18:43:59 +08002243int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
2244 struct io_failure_record **failrec_ret)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002245{
Miao Xie2fe63032014-09-12 18:43:59 +08002246 struct io_failure_record *failrec;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002247 u64 private;
2248 struct extent_map *em;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002249 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2250 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2251 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002252 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002253 u64 logical;
2254
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002255 ret = get_state_private(failure_tree, start, &private);
2256 if (ret) {
2257 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2258 if (!failrec)
2259 return -ENOMEM;
Miao Xie2fe63032014-09-12 18:43:59 +08002260
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002261 failrec->start = start;
2262 failrec->len = end - start + 1;
2263 failrec->this_mirror = 0;
2264 failrec->bio_flags = 0;
2265 failrec->in_validation = 0;
2266
2267 read_lock(&em_tree->lock);
2268 em = lookup_extent_mapping(em_tree, start, failrec->len);
2269 if (!em) {
2270 read_unlock(&em_tree->lock);
2271 kfree(failrec);
2272 return -EIO;
2273 }
2274
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002275 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002276 free_extent_map(em);
2277 em = NULL;
2278 }
2279 read_unlock(&em_tree->lock);
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002280 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002281 kfree(failrec);
2282 return -EIO;
2283 }
Miao Xie2fe63032014-09-12 18:43:59 +08002284
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002285 logical = start - em->start;
2286 logical = em->block_start + logical;
2287 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2288 logical = em->block_start;
2289 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2290 extent_set_compress_type(&failrec->bio_flags,
2291 em->compress_type);
2292 }
Miao Xie2fe63032014-09-12 18:43:59 +08002293
2294 pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n",
2295 logical, start, failrec->len);
2296
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002297 failrec->logical = logical;
2298 free_extent_map(em);
2299
2300 /* set the bits in the private failure tree */
2301 ret = set_extent_bits(failure_tree, start, end,
2302 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2303 if (ret >= 0)
2304 ret = set_state_private(failure_tree, start,
2305 (u64)(unsigned long)failrec);
2306 /* set the bits in the inode's tree */
2307 if (ret >= 0)
2308 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2309 GFP_NOFS);
2310 if (ret < 0) {
2311 kfree(failrec);
2312 return ret;
2313 }
2314 } else {
2315 failrec = (struct io_failure_record *)(unsigned long)private;
Miao Xie2fe63032014-09-12 18:43:59 +08002316 pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002317 failrec->logical, failrec->start, failrec->len,
2318 failrec->in_validation);
2319 /*
2320 * when data can be on disk more than twice, add to failrec here
2321 * (e.g. with a list for failed_mirror) to make
2322 * clean_io_failure() clean all those errors at once.
2323 */
2324 }
Miao Xie2fe63032014-09-12 18:43:59 +08002325
2326 *failrec_ret = failrec;
2327
2328 return 0;
2329}
2330
2331int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
2332 struct io_failure_record *failrec, int failed_mirror)
2333{
2334 int num_copies;
2335
Stefan Behrens5d964052012-11-05 14:59:07 +01002336 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2337 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002338 if (num_copies == 1) {
2339 /*
2340 * we only have a single copy of the data, so don't bother with
2341 * all the retry and error correction code that follows. no
2342 * matter what the error is, it is very likely to persist.
2343 */
Miao Xie2fe63032014-09-12 18:43:59 +08002344 pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
Miao Xie09a7f7a2013-07-25 19:22:32 +08002345 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002346 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002347 }
2348
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002349 /*
2350 * there are two premises:
2351 * a) deliver good data to the caller
2352 * b) correct the bad sectors on disk
2353 */
2354 if (failed_bio->bi_vcnt > 1) {
2355 /*
2356 * to fulfill b), we need to know the exact failing sectors, as
2357 * we don't want to rewrite any more than the failed ones. thus,
2358 * we need separate read requests for the failed bio
2359 *
2360 * if the following BUG_ON triggers, our validation request got
2361 * merged. we need separate requests for our algorithm to work.
2362 */
2363 BUG_ON(failrec->in_validation);
2364 failrec->in_validation = 1;
2365 failrec->this_mirror = failed_mirror;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002366 } else {
2367 /*
2368 * we're ready to fulfill a) and b) alongside. get a good copy
2369 * of the failed sector and if we succeed, we have setup
2370 * everything for repair_io_failure to do the rest for us.
2371 */
2372 if (failrec->in_validation) {
2373 BUG_ON(failrec->this_mirror != failed_mirror);
2374 failrec->in_validation = 0;
2375 failrec->this_mirror = 0;
2376 }
2377 failrec->failed_mirror = failed_mirror;
2378 failrec->this_mirror++;
2379 if (failrec->this_mirror == failed_mirror)
2380 failrec->this_mirror++;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002381 }
2382
Miao Xiefacc8a222013-07-25 19:22:34 +08002383 if (failrec->this_mirror > num_copies) {
Miao Xie2fe63032014-09-12 18:43:59 +08002384 pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002385 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie2fe63032014-09-12 18:43:59 +08002386 return 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002387 }
2388
Miao Xie2fe63032014-09-12 18:43:59 +08002389 return 1;
2390}
2391
2392
2393struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2394 struct io_failure_record *failrec,
2395 struct page *page, int pg_offset, int icsum,
Miao Xie8b110e32014-09-12 18:44:03 +08002396 bio_end_io_t *endio_func, void *data)
Miao Xie2fe63032014-09-12 18:43:59 +08002397{
2398 struct bio *bio;
2399 struct btrfs_io_bio *btrfs_failed_bio;
2400 struct btrfs_io_bio *btrfs_bio;
2401
Chris Mason9be33952013-05-17 18:30:14 -04002402 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Miao Xie2fe63032014-09-12 18:43:59 +08002403 if (!bio)
2404 return NULL;
2405
2406 bio->bi_end_io = endio_func;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002407 bio->bi_iter.bi_sector = failrec->logical >> 9;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002408 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002409 bio->bi_iter.bi_size = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08002410 bio->bi_private = data;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002411
Miao Xiefacc8a222013-07-25 19:22:34 +08002412 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2413 if (btrfs_failed_bio->csum) {
2414 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2415 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2416
2417 btrfs_bio = btrfs_io_bio(bio);
2418 btrfs_bio->csum = btrfs_bio->csum_inline;
Miao Xie2fe63032014-09-12 18:43:59 +08002419 icsum *= csum_size;
2420 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
Miao Xiefacc8a222013-07-25 19:22:34 +08002421 csum_size);
2422 }
2423
Miao Xie2fe63032014-09-12 18:43:59 +08002424 bio_add_page(bio, page, failrec->len, pg_offset);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002425
Miao Xie2fe63032014-09-12 18:43:59 +08002426 return bio;
2427}
2428
2429/*
2430 * this is a generic handler for readpage errors (default
2431 * readpage_io_failed_hook). if other copies exist, read those and write back
2432 * good data to the failed position. does not investigate in remapping the
2433 * failed extent elsewhere, hoping the device will be smart enough to do this as
2434 * needed
2435 */
2436
2437static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2438 struct page *page, u64 start, u64 end,
2439 int failed_mirror)
2440{
2441 struct io_failure_record *failrec;
2442 struct inode *inode = page->mapping->host;
2443 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2444 struct bio *bio;
2445 int read_mode;
2446 int ret;
2447
2448 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2449
2450 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
2451 if (ret)
2452 return ret;
2453
2454 ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
2455 if (!ret) {
2456 free_io_failure(inode, failrec);
2457 return -EIO;
2458 }
2459
2460 if (failed_bio->bi_vcnt > 1)
2461 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2462 else
2463 read_mode = READ_SYNC;
2464
2465 phy_offset >>= inode->i_sb->s_blocksize_bits;
2466 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
2467 start - page_offset(page),
Miao Xie8b110e32014-09-12 18:44:03 +08002468 (int)phy_offset, failed_bio->bi_end_io,
2469 NULL);
Miao Xie2fe63032014-09-12 18:43:59 +08002470 if (!bio) {
2471 free_io_failure(inode, failrec);
2472 return -EIO;
2473 }
2474
2475 pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n",
2476 read_mode, failrec->this_mirror, failrec->in_validation);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002477
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002478 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2479 failrec->this_mirror,
2480 failrec->bio_flags, 0);
Miao Xie6c387ab2014-09-12 18:43:57 +08002481 if (ret) {
Miao Xie454ff3d2014-09-12 18:43:58 +08002482 free_io_failure(inode, failrec);
Miao Xie6c387ab2014-09-12 18:43:57 +08002483 bio_put(bio);
2484 }
2485
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002486 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002487}
2488
Chris Masond1310b22008-01-24 16:13:08 -05002489/* lots and lots of room for performance fixes in the end_bio funcs */
2490
Jeff Mahoney87826df2012-02-15 16:23:57 +01002491int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2492{
2493 int uptodate = (err == 0);
2494 struct extent_io_tree *tree;
Eric Sandeen3e2426b2014-06-12 00:39:58 -05002495 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002496
2497 tree = &BTRFS_I(page->mapping->host)->io_tree;
2498
2499 if (tree->ops && tree->ops->writepage_end_io_hook) {
2500 ret = tree->ops->writepage_end_io_hook(page, start,
2501 end, NULL, uptodate);
2502 if (ret)
2503 uptodate = 0;
2504 }
2505
Jeff Mahoney87826df2012-02-15 16:23:57 +01002506 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002507 ClearPageUptodate(page);
2508 SetPageError(page);
Liu Bo5dca6ee2014-05-12 12:47:36 +08002509 ret = ret < 0 ? ret : -EIO;
2510 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002511 }
2512 return 0;
2513}
2514
Chris Masond1310b22008-01-24 16:13:08 -05002515/*
2516 * after a writepage IO is done, we need to:
2517 * clear the uptodate bits on error
2518 * clear the writeback bits in the extent tree for this IO
2519 * end_page_writeback if the page has no more pending IO
2520 *
2521 * Scheduling is not allowed, so the extent state tree is expected
2522 * to have one and only one object corresponding to this IO.
2523 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002524static void end_bio_extent_writepage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002525{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002526 struct bio_vec *bvec;
Chris Masond1310b22008-01-24 16:13:08 -05002527 u64 start;
2528 u64 end;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002529 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002530
Kent Overstreet2c30c712013-11-07 12:20:26 -08002531 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002532 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002533
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002534 /* We always issue full-page reads, but if some block
2535 * in a page fails to read, blk_update_request() will
2536 * advance bv_offset and adjust bv_len to compensate.
2537 * Print a warning for nonzero offsets, and an error
2538 * if they don't add up to a full page. */
Frank Holtonefe120a2013-12-20 11:37:06 -05002539 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
2540 if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
2541 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
2542 "partial page write in btrfs with offset %u and length %u",
2543 bvec->bv_offset, bvec->bv_len);
2544 else
2545 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
2546 "incomplete page write in btrfs with offset %u and "
2547 "length %u",
2548 bvec->bv_offset, bvec->bv_len);
2549 }
Chris Masond1310b22008-01-24 16:13:08 -05002550
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002551 start = page_offset(page);
2552 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002553
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002554 if (end_extent_writepage(page, bio->bi_error, start, end))
Jeff Mahoney87826df2012-02-15 16:23:57 +01002555 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002556
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002557 end_page_writeback(page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08002558 }
Chris Mason2b1f55b2008-09-24 11:48:04 -04002559
Chris Masond1310b22008-01-24 16:13:08 -05002560 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002561}
2562
Miao Xie883d0de2013-07-25 19:22:35 +08002563static void
2564endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2565 int uptodate)
2566{
2567 struct extent_state *cached = NULL;
2568 u64 end = start + len - 1;
2569
2570 if (uptodate && tree->track_uptodate)
2571 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2572 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2573}
2574
Chris Masond1310b22008-01-24 16:13:08 -05002575/*
2576 * after a readpage IO is done, we need to:
2577 * clear the uptodate bits on error
2578 * set the uptodate bits if things worked
2579 * set the page up to date if all extents in the tree are uptodate
2580 * clear the lock bit in the extent tree
2581 * unlock the page if there are no other extents locked for it
2582 *
2583 * Scheduling is not allowed, so the extent state tree is expected
2584 * to have one and only one object corresponding to this IO.
2585 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002586static void end_bio_extent_readpage(struct bio *bio)
Chris Masond1310b22008-01-24 16:13:08 -05002587{
Kent Overstreet2c30c712013-11-07 12:20:26 -08002588 struct bio_vec *bvec;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002589 int uptodate = !bio->bi_error;
Miao Xiefacc8a222013-07-25 19:22:34 +08002590 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002591 struct extent_io_tree *tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002592 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002593 u64 start;
2594 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002595 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002596 u64 extent_start = 0;
2597 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002598 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002599 int ret;
Kent Overstreet2c30c712013-11-07 12:20:26 -08002600 int i;
Chris Masond1310b22008-01-24 16:13:08 -05002601
Kent Overstreet2c30c712013-11-07 12:20:26 -08002602 bio_for_each_segment_all(bvec, bio, i) {
Chris Masond1310b22008-01-24 16:13:08 -05002603 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002604 struct inode *inode = page->mapping->host;
Arne Jansen507903b2011-04-06 10:02:20 +00002605
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002606 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002607 "mirror=%u\n", (u64)bio->bi_iter.bi_sector,
2608 bio->bi_error, io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002609 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002610
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002611 /* We always issue full-page reads, but if some block
2612 * in a page fails to read, blk_update_request() will
2613 * advance bv_offset and adjust bv_len to compensate.
2614 * Print a warning for nonzero offsets, and an error
2615 * if they don't add up to a full page. */
Frank Holtonefe120a2013-12-20 11:37:06 -05002616 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
2617 if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
2618 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
2619 "partial page read in btrfs with offset %u and length %u",
2620 bvec->bv_offset, bvec->bv_len);
2621 else
2622 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
2623 "incomplete page read in btrfs with offset %u and "
2624 "length %u",
2625 bvec->bv_offset, bvec->bv_len);
2626 }
Chris Masond1310b22008-01-24 16:13:08 -05002627
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002628 start = page_offset(page);
2629 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002630 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002631
Chris Mason9be33952013-05-17 18:30:14 -04002632 mirror = io_bio->mirror_num;
Miao Xief2a09da2013-07-25 19:22:33 +08002633 if (likely(uptodate && tree->ops &&
2634 tree->ops->readpage_end_io_hook)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002635 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2636 page, start, end,
2637 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002638 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002639 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002640 else
Miao Xie1203b682014-09-12 18:44:01 +08002641 clean_io_failure(inode, start, page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002642 }
Josef Bacikea466792012-03-26 21:57:36 -04002643
Miao Xief2a09da2013-07-25 19:22:33 +08002644 if (likely(uptodate))
2645 goto readpage_ok;
2646
2647 if (tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002648 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002649 if (!ret && !bio->bi_error)
Josef Bacikea466792012-03-26 21:57:36 -04002650 uptodate = 1;
Miao Xief2a09da2013-07-25 19:22:33 +08002651 } else {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002652 /*
2653 * The generic bio_readpage_error handles errors the
2654 * following way: If possible, new read requests are
2655 * created and submitted and will end up in
2656 * end_bio_extent_readpage as well (if we're lucky, not
2657 * in the !uptodate case). In that case it returns 0 and
2658 * we just go on with the next page in our bio. If it
2659 * can't handle the error it will return -EIO and we
2660 * remain responsible for that page.
2661 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002662 ret = bio_readpage_error(bio, offset, page, start, end,
2663 mirror);
Chris Mason7e383262008-04-09 16:28:12 -04002664 if (ret == 0) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002665 uptodate = !bio->bi_error;
Liu Bo38c1c2e2014-08-19 23:33:13 +08002666 offset += len;
Chris Mason7e383262008-04-09 16:28:12 -04002667 continue;
2668 }
2669 }
Miao Xief2a09da2013-07-25 19:22:33 +08002670readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002671 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002672 loff_t i_size = i_size_read(inode);
2673 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
Liu Boa583c022014-08-19 23:32:22 +08002674 unsigned off;
Josef Bacika71754f2013-06-17 17:14:39 -04002675
2676 /* Zero out the end if this page straddles i_size */
Liu Boa583c022014-08-19 23:32:22 +08002677 off = i_size & (PAGE_CACHE_SIZE-1);
2678 if (page->index == end_index && off)
2679 zero_user_segment(page, off, PAGE_CACHE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002680 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002681 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002682 ClearPageUptodate(page);
2683 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002684 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002685 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002686 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002687
2688 if (unlikely(!uptodate)) {
2689 if (extent_len) {
2690 endio_readpage_release_extent(tree,
2691 extent_start,
2692 extent_len, 1);
2693 extent_start = 0;
2694 extent_len = 0;
2695 }
2696 endio_readpage_release_extent(tree, start,
2697 end - start + 1, 0);
2698 } else if (!extent_len) {
2699 extent_start = start;
2700 extent_len = end + 1 - start;
2701 } else if (extent_start + extent_len == start) {
2702 extent_len += end + 1 - start;
2703 } else {
2704 endio_readpage_release_extent(tree, extent_start,
2705 extent_len, uptodate);
2706 extent_start = start;
2707 extent_len = end + 1 - start;
2708 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08002709 }
Chris Masond1310b22008-01-24 16:13:08 -05002710
Miao Xie883d0de2013-07-25 19:22:35 +08002711 if (extent_len)
2712 endio_readpage_release_extent(tree, extent_start, extent_len,
2713 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002714 if (io_bio->end_io)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002715 io_bio->end_io(io_bio, bio->bi_error);
Chris Masond1310b22008-01-24 16:13:08 -05002716 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002717}
2718
Chris Mason9be33952013-05-17 18:30:14 -04002719/*
2720 * this allocates from the btrfs_bioset. We're returning a bio right now
2721 * but you can call btrfs_io_bio for the appropriate container_of magic
2722 */
Miao Xie88f794e2010-11-22 03:02:55 +00002723struct bio *
2724btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2725 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002726{
Miao Xiefacc8a222013-07-25 19:22:34 +08002727 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002728 struct bio *bio;
2729
Chris Mason9be33952013-05-17 18:30:14 -04002730 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002731
2732 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002733 while (!bio && (nr_vecs /= 2)) {
2734 bio = bio_alloc_bioset(gfp_flags,
2735 nr_vecs, btrfs_bioset);
2736 }
Chris Masond1310b22008-01-24 16:13:08 -05002737 }
2738
2739 if (bio) {
2740 bio->bi_bdev = bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002741 bio->bi_iter.bi_sector = first_sector;
Miao Xiefacc8a222013-07-25 19:22:34 +08002742 btrfs_bio = btrfs_io_bio(bio);
2743 btrfs_bio->csum = NULL;
2744 btrfs_bio->csum_allocated = NULL;
2745 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002746 }
2747 return bio;
2748}
2749
Chris Mason9be33952013-05-17 18:30:14 -04002750struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2751{
Miao Xie23ea8e52014-09-12 18:43:54 +08002752 struct btrfs_io_bio *btrfs_bio;
2753 struct bio *new;
Chris Mason9be33952013-05-17 18:30:14 -04002754
Miao Xie23ea8e52014-09-12 18:43:54 +08002755 new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2756 if (new) {
2757 btrfs_bio = btrfs_io_bio(new);
2758 btrfs_bio->csum = NULL;
2759 btrfs_bio->csum_allocated = NULL;
2760 btrfs_bio->end_io = NULL;
Chris Mason3a9508b2015-08-21 10:05:39 -07002761
2762#ifdef CONFIG_BLK_CGROUP
Chris Masonda2f0f72015-07-02 13:57:22 -07002763 /* FIXME, put this into bio_clone_bioset */
2764 if (bio->bi_css)
2765 bio_associate_blkcg(new, bio->bi_css);
Chris Mason3a9508b2015-08-21 10:05:39 -07002766#endif
Miao Xie23ea8e52014-09-12 18:43:54 +08002767 }
2768 return new;
2769}
Chris Mason9be33952013-05-17 18:30:14 -04002770
2771/* this also allocates from the btrfs_bioset */
2772struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2773{
Miao Xiefacc8a222013-07-25 19:22:34 +08002774 struct btrfs_io_bio *btrfs_bio;
2775 struct bio *bio;
2776
2777 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2778 if (bio) {
2779 btrfs_bio = btrfs_io_bio(bio);
2780 btrfs_bio->csum = NULL;
2781 btrfs_bio->csum_allocated = NULL;
2782 btrfs_bio->end_io = NULL;
2783 }
2784 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002785}
2786
2787
Jeff Mahoney355808c2011-10-03 23:23:14 -04002788static int __must_check submit_one_bio(int rw, struct bio *bio,
2789 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002790{
Chris Masond1310b22008-01-24 16:13:08 -05002791 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002792 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2793 struct page *page = bvec->bv_page;
2794 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002795 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002796
Miao Xie4eee4fa2012-12-21 09:17:45 +00002797 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002798
David Woodhouse902b22f2008-08-20 08:51:49 -04002799 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002800
2801 bio_get(bio);
2802
Chris Mason065631f2008-02-20 12:07:25 -05002803 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002804 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002805 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002806 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002807 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002808
Chris Masond1310b22008-01-24 16:13:08 -05002809 bio_put(bio);
2810 return ret;
2811}
2812
David Woodhouse64a16702009-07-15 23:29:37 +01002813static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002814 unsigned long offset, size_t size, struct bio *bio,
2815 unsigned long bio_flags)
2816{
2817 int ret = 0;
2818 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002819 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002820 bio_flags);
2821 BUG_ON(ret < 0);
2822 return ret;
2823
2824}
2825
Chris Masond1310b22008-01-24 16:13:08 -05002826static int submit_extent_page(int rw, struct extent_io_tree *tree,
Chris Masonda2f0f72015-07-02 13:57:22 -07002827 struct writeback_control *wbc,
Chris Masond1310b22008-01-24 16:13:08 -05002828 struct page *page, sector_t sector,
2829 size_t size, unsigned long offset,
2830 struct block_device *bdev,
2831 struct bio **bio_ret,
2832 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002833 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002834 int mirror_num,
2835 unsigned long prev_bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01002836 unsigned long bio_flags,
2837 bool force_bio_submit)
Chris Masond1310b22008-01-24 16:13:08 -05002838{
2839 int ret = 0;
2840 struct bio *bio;
Chris Masonc8b97812008-10-29 14:49:59 -04002841 int contig = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002842 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002843 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002844
2845 if (bio_ret && *bio_ret) {
2846 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002847 if (old_compressed)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002848 contig = bio->bi_iter.bi_sector == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002849 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002850 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002851
2852 if (prev_bio_flags != bio_flags || !contig ||
Filipe Manana005efed2015-09-14 09:09:31 +01002853 force_bio_submit ||
David Woodhouse64a16702009-07-15 23:29:37 +01002854 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002855 bio_add_page(bio, page, page_size, offset) < page_size) {
2856 ret = submit_one_bio(rw, bio, mirror_num,
2857 prev_bio_flags);
Naohiro Aota289454a2015-01-06 01:01:03 +09002858 if (ret < 0) {
2859 *bio_ret = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002860 return ret;
Naohiro Aota289454a2015-01-06 01:01:03 +09002861 }
Chris Masond1310b22008-01-24 16:13:08 -05002862 bio = NULL;
2863 } else {
Chris Masonda2f0f72015-07-02 13:57:22 -07002864 if (wbc)
2865 wbc_account_io(wbc, page, page_size);
Chris Masond1310b22008-01-24 16:13:08 -05002866 return 0;
2867 }
2868 }
Chris Masonc8b97812008-10-29 14:49:59 -04002869
Kent Overstreetb54ffb72015-05-19 14:31:01 +02002870 bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
2871 GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002872 if (!bio)
2873 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002874
Chris Masonc8b97812008-10-29 14:49:59 -04002875 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002876 bio->bi_end_io = end_io_func;
2877 bio->bi_private = tree;
Chris Masonda2f0f72015-07-02 13:57:22 -07002878 if (wbc) {
2879 wbc_init_bio(wbc, bio);
2880 wbc_account_io(wbc, page, page_size);
2881 }
Chris Mason70dec802008-01-29 09:59:12 -05002882
Chris Masond3977122009-01-05 21:25:51 -05002883 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002884 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002885 else
Chris Masonc8b97812008-10-29 14:49:59 -04002886 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002887
2888 return ret;
2889}
2890
Eric Sandeen48a3b632013-04-25 20:41:01 +00002891static void attach_extent_buffer_page(struct extent_buffer *eb,
2892 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002893{
2894 if (!PagePrivate(page)) {
2895 SetPagePrivate(page);
2896 page_cache_get(page);
2897 set_page_private(page, (unsigned long)eb);
2898 } else {
2899 WARN_ON(page->private != (unsigned long)eb);
2900 }
2901}
2902
Chris Masond1310b22008-01-24 16:13:08 -05002903void set_page_extent_mapped(struct page *page)
2904{
2905 if (!PagePrivate(page)) {
2906 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002907 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002908 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002909 }
2910}
2911
Miao Xie125bac012013-07-25 19:22:37 +08002912static struct extent_map *
2913__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2914 u64 start, u64 len, get_extent_t *get_extent,
2915 struct extent_map **em_cached)
2916{
2917 struct extent_map *em;
2918
2919 if (em_cached && *em_cached) {
2920 em = *em_cached;
Filipe Mananacbc0e922014-02-25 14:15:12 +00002921 if (extent_map_in_tree(em) && start >= em->start &&
Miao Xie125bac012013-07-25 19:22:37 +08002922 start < extent_map_end(em)) {
2923 atomic_inc(&em->refs);
2924 return em;
2925 }
2926
2927 free_extent_map(em);
2928 *em_cached = NULL;
2929 }
2930
2931 em = get_extent(inode, page, pg_offset, start, len, 0);
2932 if (em_cached && !IS_ERR_OR_NULL(em)) {
2933 BUG_ON(*em_cached);
2934 atomic_inc(&em->refs);
2935 *em_cached = em;
2936 }
2937 return em;
2938}
Chris Masond1310b22008-01-24 16:13:08 -05002939/*
2940 * basic readpage implementation. Locked extent state structs are inserted
2941 * into the tree that are removed when the IO is done (by the end_io
2942 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002943 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002944 */
Miao Xie99740902013-07-25 19:22:36 +08002945static int __do_readpage(struct extent_io_tree *tree,
2946 struct page *page,
2947 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002948 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002949 struct bio **bio, int mirror_num,
Filipe Manana005efed2015-09-14 09:09:31 +01002950 unsigned long *bio_flags, int rw,
2951 u64 *prev_em_start)
Chris Masond1310b22008-01-24 16:13:08 -05002952{
2953 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002954 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002955 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2956 u64 end;
2957 u64 cur = start;
2958 u64 extent_offset;
2959 u64 last_byte = i_size_read(inode);
2960 u64 block_start;
2961 u64 cur_end;
2962 sector_t sector;
2963 struct extent_map *em;
2964 struct block_device *bdev;
2965 int ret;
2966 int nr = 0;
Mark Fasheh4b384312013-08-06 11:42:50 -07002967 int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
David Sterba306e16c2011-04-19 14:29:38 +02002968 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002969 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002970 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002971 size_t blocksize = inode->i_sb->s_blocksize;
Mark Fasheh4b384312013-08-06 11:42:50 -07002972 unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
Chris Masond1310b22008-01-24 16:13:08 -05002973
2974 set_page_extent_mapped(page);
2975
Miao Xie99740902013-07-25 19:22:36 +08002976 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002977 if (!PageUptodate(page)) {
2978 if (cleancache_get_page(page) == 0) {
2979 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002980 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002981 goto out;
2982 }
2983 }
2984
Chris Masonc8b97812008-10-29 14:49:59 -04002985 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2986 char *userpage;
2987 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2988
2989 if (zero_offset) {
2990 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002991 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002992 memset(userpage + zero_offset, 0, iosize);
2993 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002994 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002995 }
2996 }
Chris Masond1310b22008-01-24 16:13:08 -05002997 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002998 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
Filipe Manana005efed2015-09-14 09:09:31 +01002999 bool force_bio_submit = false;
Josef Bacikc8f2f242013-02-11 11:33:00 -05003000
Chris Masond1310b22008-01-24 16:13:08 -05003001 if (cur >= last_byte) {
3002 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003003 struct extent_state *cached = NULL;
3004
David Sterba306e16c2011-04-19 14:29:38 +02003005 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08003006 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003007 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003008 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003009 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003010 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003011 &cached, GFP_NOFS);
Mark Fasheh4b384312013-08-06 11:42:50 -07003012 if (!parent_locked)
3013 unlock_extent_cached(tree, cur,
3014 cur + iosize - 1,
3015 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003016 break;
3017 }
Miao Xie125bac012013-07-25 19:22:37 +08003018 em = __get_extent_map(inode, page, pg_offset, cur,
3019 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02003020 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003021 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003022 if (!parent_locked)
3023 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003024 break;
3025 }
Chris Masond1310b22008-01-24 16:13:08 -05003026 extent_offset = cur - em->start;
3027 BUG_ON(extent_map_end(em) <= cur);
3028 BUG_ON(end < cur);
3029
Li Zefan261507a02010-12-17 14:21:50 +08003030 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07003031 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08003032 extent_set_compress_type(&this_bio_flag,
3033 em->compress_type);
3034 }
Chris Masonc8b97812008-10-29 14:49:59 -04003035
Chris Masond1310b22008-01-24 16:13:08 -05003036 iosize = min(extent_map_end(em) - cur, end - cur + 1);
3037 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00003038 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04003039 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
3040 disk_io_size = em->block_len;
3041 sector = em->block_start >> 9;
3042 } else {
3043 sector = (em->block_start + extent_offset) >> 9;
3044 disk_io_size = iosize;
3045 }
Chris Masond1310b22008-01-24 16:13:08 -05003046 bdev = em->bdev;
3047 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04003048 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3049 block_start = EXTENT_MAP_HOLE;
Filipe Manana005efed2015-09-14 09:09:31 +01003050
3051 /*
3052 * If we have a file range that points to a compressed extent
3053 * and it's followed by a consecutive file range that points to
3054 * to the same compressed extent (possibly with a different
3055 * offset and/or length, so it either points to the whole extent
3056 * or only part of it), we must make sure we do not submit a
3057 * single bio to populate the pages for the 2 ranges because
3058 * this makes the compressed extent read zero out the pages
3059 * belonging to the 2nd range. Imagine the following scenario:
3060 *
3061 * File layout
3062 * [0 - 8K] [8K - 24K]
3063 * | |
3064 * | |
3065 * points to extent X, points to extent X,
3066 * offset 4K, length of 8K offset 0, length 16K
3067 *
3068 * [extent X, compressed length = 4K uncompressed length = 16K]
3069 *
3070 * If the bio to read the compressed extent covers both ranges,
3071 * it will decompress extent X into the pages belonging to the
3072 * first range and then it will stop, zeroing out the remaining
3073 * pages that belong to the other range that points to extent X.
3074 * So here we make sure we submit 2 bios, one for the first
3075 * range and another one for the third range. Both will target
3076 * the same physical extent from disk, but we can't currently
3077 * make the compressed bio endio callback populate the pages
3078 * for both ranges because each compressed bio is tightly
3079 * coupled with a single extent map, and each range can have
3080 * an extent map with a different offset value relative to the
3081 * uncompressed data of our extent and different lengths. This
3082 * is a corner case so we prioritize correctness over
3083 * non-optimal behavior (submitting 2 bios for the same extent).
3084 */
3085 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
3086 prev_em_start && *prev_em_start != (u64)-1 &&
3087 *prev_em_start != em->orig_start)
3088 force_bio_submit = true;
3089
3090 if (prev_em_start)
3091 *prev_em_start = em->orig_start;
3092
Chris Masond1310b22008-01-24 16:13:08 -05003093 free_extent_map(em);
3094 em = NULL;
3095
3096 /* we've found a hole, just zero and go on */
3097 if (block_start == EXTENT_MAP_HOLE) {
3098 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00003099 struct extent_state *cached = NULL;
3100
Cong Wang7ac687d2011-11-25 23:14:28 +08003101 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02003102 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05003103 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08003104 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05003105
3106 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00003107 &cached, GFP_NOFS);
Filipe Manana5e6ecb32015-10-13 16:36:09 +01003108 if (parent_locked)
3109 free_extent_state(cached);
3110 else
3111 unlock_extent_cached(tree, cur,
3112 cur + iosize - 1,
3113 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003114 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003115 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003116 continue;
3117 }
3118 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04003119 if (test_range_bit(tree, cur, cur_end,
3120 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04003121 check_page_uptodate(tree, page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003122 if (!parent_locked)
3123 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003124 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003125 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003126 continue;
3127 }
Chris Mason70dec802008-01-29 09:59:12 -05003128 /* we have an inline extent but it didn't get marked up
3129 * to date. Error out
3130 */
3131 if (block_start == EXTENT_MAP_INLINE) {
3132 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003133 if (!parent_locked)
3134 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05003135 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003136 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05003137 continue;
3138 }
Chris Masond1310b22008-01-24 16:13:08 -05003139
Josef Bacikc8f2f242013-02-11 11:33:00 -05003140 pnr -= page->index;
Chris Masonda2f0f72015-07-02 13:57:22 -07003141 ret = submit_extent_page(rw, tree, NULL, page,
David Sterba306e16c2011-04-19 14:29:38 +02003142 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04003143 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04003144 end_bio_extent_readpage, mirror_num,
3145 *bio_flags,
Filipe Manana005efed2015-09-14 09:09:31 +01003146 this_bio_flag,
3147 force_bio_submit);
Josef Bacikc8f2f242013-02-11 11:33:00 -05003148 if (!ret) {
3149 nr++;
3150 *bio_flags = this_bio_flag;
3151 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003152 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07003153 if (!parent_locked)
3154 unlock_extent(tree, cur, cur + iosize - 1);
Josef Bacikedd33c92012-10-05 16:40:32 -04003155 }
Chris Masond1310b22008-01-24 16:13:08 -05003156 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02003157 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003158 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06003159out:
Chris Masond1310b22008-01-24 16:13:08 -05003160 if (!nr) {
3161 if (!PageError(page))
3162 SetPageUptodate(page);
3163 unlock_page(page);
3164 }
3165 return 0;
3166}
3167
Miao Xie99740902013-07-25 19:22:36 +08003168static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
3169 struct page *pages[], int nr_pages,
3170 u64 start, u64 end,
3171 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003172 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003173 struct bio **bio, int mirror_num,
Filipe Manana808f80b2015-09-28 09:56:26 +01003174 unsigned long *bio_flags, int rw,
3175 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003176{
3177 struct inode *inode;
3178 struct btrfs_ordered_extent *ordered;
3179 int index;
3180
3181 inode = pages[0]->mapping->host;
3182 while (1) {
3183 lock_extent(tree, start, end);
3184 ordered = btrfs_lookup_ordered_range(inode, start,
3185 end - start + 1);
3186 if (!ordered)
3187 break;
3188 unlock_extent(tree, start, end);
3189 btrfs_start_ordered_extent(inode, ordered, 1);
3190 btrfs_put_ordered_extent(ordered);
3191 }
3192
3193 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08003194 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
Filipe Manana808f80b2015-09-28 09:56:26 +01003195 mirror_num, bio_flags, rw, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003196 page_cache_release(pages[index]);
3197 }
3198}
3199
3200static void __extent_readpages(struct extent_io_tree *tree,
3201 struct page *pages[],
3202 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08003203 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003204 struct bio **bio, int mirror_num,
Filipe Manana808f80b2015-09-28 09:56:26 +01003205 unsigned long *bio_flags, int rw,
3206 u64 *prev_em_start)
Miao Xie99740902013-07-25 19:22:36 +08003207{
Stefan Behrens35a36212013-08-14 18:12:25 +02003208 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08003209 u64 end = 0;
3210 u64 page_start;
3211 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02003212 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08003213
3214 for (index = 0; index < nr_pages; index++) {
3215 page_start = page_offset(pages[index]);
3216 if (!end) {
3217 start = page_start;
3218 end = start + PAGE_CACHE_SIZE - 1;
3219 first_index = index;
3220 } else if (end + 1 == page_start) {
3221 end += PAGE_CACHE_SIZE;
3222 } else {
3223 __do_contiguous_readpages(tree, &pages[first_index],
3224 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003225 end, get_extent, em_cached,
3226 bio, mirror_num, bio_flags,
Filipe Manana808f80b2015-09-28 09:56:26 +01003227 rw, prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003228 start = page_start;
3229 end = start + PAGE_CACHE_SIZE - 1;
3230 first_index = index;
3231 }
3232 }
3233
3234 if (end)
3235 __do_contiguous_readpages(tree, &pages[first_index],
3236 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003237 end, get_extent, em_cached, bio,
Filipe Manana808f80b2015-09-28 09:56:26 +01003238 mirror_num, bio_flags, rw,
3239 prev_em_start);
Miao Xie99740902013-07-25 19:22:36 +08003240}
3241
3242static int __extent_read_full_page(struct extent_io_tree *tree,
3243 struct page *page,
3244 get_extent_t *get_extent,
3245 struct bio **bio, int mirror_num,
3246 unsigned long *bio_flags, int rw)
3247{
3248 struct inode *inode = page->mapping->host;
3249 struct btrfs_ordered_extent *ordered;
3250 u64 start = page_offset(page);
3251 u64 end = start + PAGE_CACHE_SIZE - 1;
3252 int ret;
3253
3254 while (1) {
3255 lock_extent(tree, start, end);
3256 ordered = btrfs_lookup_ordered_extent(inode, start);
3257 if (!ordered)
3258 break;
3259 unlock_extent(tree, start, end);
3260 btrfs_start_ordered_extent(inode, ordered, 1);
3261 btrfs_put_ordered_extent(ordered);
3262 }
3263
Miao Xie125bac012013-07-25 19:22:37 +08003264 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
Filipe Manana005efed2015-09-14 09:09:31 +01003265 bio_flags, rw, NULL);
Miao Xie99740902013-07-25 19:22:36 +08003266 return ret;
3267}
3268
Chris Masond1310b22008-01-24 16:13:08 -05003269int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003270 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003271{
3272 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003273 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003274 int ret;
3275
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003276 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003277 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05003278 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003279 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003280 return ret;
3281}
Chris Masond1310b22008-01-24 16:13:08 -05003282
Mark Fasheh4b384312013-08-06 11:42:50 -07003283int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
3284 get_extent_t *get_extent, int mirror_num)
3285{
3286 struct bio *bio = NULL;
3287 unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED;
3288 int ret;
3289
3290 ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num,
Filipe Manana005efed2015-09-14 09:09:31 +01003291 &bio_flags, READ, NULL);
Mark Fasheh4b384312013-08-06 11:42:50 -07003292 if (bio)
3293 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
3294 return ret;
3295}
3296
Chris Mason11c83492009-04-20 15:50:09 -04003297static noinline void update_nr_written(struct page *page,
3298 struct writeback_control *wbc,
3299 unsigned long nr_written)
3300{
3301 wbc->nr_to_write -= nr_written;
3302 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
3303 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
3304 page->mapping->writeback_index = page->index + nr_written;
3305}
3306
Chris Masond1310b22008-01-24 16:13:08 -05003307/*
Chris Mason40f76582014-05-21 13:35:51 -07003308 * helper for __extent_writepage, doing all of the delayed allocation setup.
3309 *
3310 * This returns 1 if our fill_delalloc function did all the work required
3311 * to write the page (copy into inline extent). In this case the IO has
3312 * been started and the page is already unlocked.
3313 *
3314 * This returns 0 if all went well (page still locked)
3315 * This returns < 0 if there were errors (page still locked)
Chris Masond1310b22008-01-24 16:13:08 -05003316 */
Chris Mason40f76582014-05-21 13:35:51 -07003317static noinline_for_stack int writepage_delalloc(struct inode *inode,
3318 struct page *page, struct writeback_control *wbc,
3319 struct extent_page_data *epd,
3320 u64 delalloc_start,
3321 unsigned long *nr_written)
Chris Masond1310b22008-01-24 16:13:08 -05003322{
Chris Mason40f76582014-05-21 13:35:51 -07003323 struct extent_io_tree *tree = epd->tree;
3324 u64 page_end = delalloc_start + PAGE_CACHE_SIZE - 1;
3325 u64 nr_delalloc;
3326 u64 delalloc_to_write = 0;
3327 u64 delalloc_end = 0;
3328 int ret;
3329 int page_started = 0;
3330
3331 if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
3332 return 0;
3333
3334 while (delalloc_end < page_end) {
3335 nr_delalloc = find_lock_delalloc_range(inode, tree,
3336 page,
3337 &delalloc_start,
3338 &delalloc_end,
Josef Bacikdcab6a32015-02-11 15:08:59 -05003339 BTRFS_MAX_EXTENT_SIZE);
Chris Mason40f76582014-05-21 13:35:51 -07003340 if (nr_delalloc == 0) {
3341 delalloc_start = delalloc_end + 1;
3342 continue;
3343 }
3344 ret = tree->ops->fill_delalloc(inode, page,
3345 delalloc_start,
3346 delalloc_end,
3347 &page_started,
3348 nr_written);
3349 /* File system has been set read-only */
3350 if (ret) {
3351 SetPageError(page);
3352 /* fill_delalloc should be return < 0 for error
3353 * but just in case, we use > 0 here meaning the
3354 * IO is started, so we don't want to return > 0
3355 * unless things are going well.
3356 */
3357 ret = ret < 0 ? ret : -EIO;
3358 goto done;
3359 }
3360 /*
3361 * delalloc_end is already one less than the total
3362 * length, so we don't subtract one from
3363 * PAGE_CACHE_SIZE
3364 */
3365 delalloc_to_write += (delalloc_end - delalloc_start +
3366 PAGE_CACHE_SIZE) >>
3367 PAGE_CACHE_SHIFT;
3368 delalloc_start = delalloc_end + 1;
3369 }
3370 if (wbc->nr_to_write < delalloc_to_write) {
3371 int thresh = 8192;
3372
3373 if (delalloc_to_write < thresh * 2)
3374 thresh = delalloc_to_write;
3375 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3376 thresh);
3377 }
3378
3379 /* did the fill delalloc function already unlock and start
3380 * the IO?
3381 */
3382 if (page_started) {
3383 /*
3384 * we've unlocked the page, so we can't update
3385 * the mapping's writeback index, just update
3386 * nr_to_write.
3387 */
3388 wbc->nr_to_write -= *nr_written;
3389 return 1;
3390 }
3391
3392 ret = 0;
3393
3394done:
3395 return ret;
3396}
3397
3398/*
3399 * helper for __extent_writepage. This calls the writepage start hooks,
3400 * and does the loop to map the page into extents and bios.
3401 *
3402 * We return 1 if the IO is started and the page is unlocked,
3403 * 0 if all went well (page still locked)
3404 * < 0 if there were errors (page still locked)
3405 */
3406static noinline_for_stack int __extent_writepage_io(struct inode *inode,
3407 struct page *page,
3408 struct writeback_control *wbc,
3409 struct extent_page_data *epd,
3410 loff_t i_size,
3411 unsigned long nr_written,
3412 int write_flags, int *nr_ret)
3413{
Chris Masond1310b22008-01-24 16:13:08 -05003414 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003415 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003416 u64 page_end = start + PAGE_CACHE_SIZE - 1;
3417 u64 end;
3418 u64 cur = start;
3419 u64 extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05003420 u64 block_start;
3421 u64 iosize;
3422 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04003423 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003424 struct extent_map *em;
3425 struct block_device *bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003426 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003427 size_t blocksize;
Chris Mason40f76582014-05-21 13:35:51 -07003428 int ret = 0;
3429 int nr = 0;
3430 bool compressed;
Chris Masond1310b22008-01-24 16:13:08 -05003431
Chris Mason247e7432008-07-17 12:53:51 -04003432 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003433 ret = tree->ops->writepage_start_hook(page, start,
3434 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003435 if (ret) {
3436 /* Fixup worker will requeue */
3437 if (ret == -EBUSY)
3438 wbc->pages_skipped++;
3439 else
3440 redirty_page_for_writepage(wbc, page);
Chris Mason40f76582014-05-21 13:35:51 -07003441
Chris Mason11c83492009-04-20 15:50:09 -04003442 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003443 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003444 ret = 1;
Chris Mason11c83492009-04-20 15:50:09 -04003445 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003446 }
3447 }
3448
Chris Mason11c83492009-04-20 15:50:09 -04003449 /*
3450 * we don't want to touch the inode after unlocking the page,
3451 * so we update the mapping writeback index now
3452 */
3453 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003454
Chris Masond1310b22008-01-24 16:13:08 -05003455 end = page_end;
Chris Mason40f76582014-05-21 13:35:51 -07003456 if (i_size <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003457 if (tree->ops && tree->ops->writepage_end_io_hook)
3458 tree->ops->writepage_end_io_hook(page, start,
3459 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003460 goto done;
3461 }
3462
Chris Masond1310b22008-01-24 16:13:08 -05003463 blocksize = inode->i_sb->s_blocksize;
3464
3465 while (cur <= end) {
Chris Mason40f76582014-05-21 13:35:51 -07003466 u64 em_end;
3467 if (cur >= i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003468 if (tree->ops && tree->ops->writepage_end_io_hook)
3469 tree->ops->writepage_end_io_hook(page, cur,
3470 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003471 break;
3472 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003473 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003474 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003475 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003476 SetPageError(page);
Filipe Manana61391d52014-05-09 17:17:40 +01003477 ret = PTR_ERR_OR_ZERO(em);
Chris Masond1310b22008-01-24 16:13:08 -05003478 break;
3479 }
3480
3481 extent_offset = cur - em->start;
Chris Mason40f76582014-05-21 13:35:51 -07003482 em_end = extent_map_end(em);
3483 BUG_ON(em_end <= cur);
Chris Masond1310b22008-01-24 16:13:08 -05003484 BUG_ON(end < cur);
Chris Mason40f76582014-05-21 13:35:51 -07003485 iosize = min(em_end - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003486 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003487 sector = (em->block_start + extent_offset) >> 9;
3488 bdev = em->bdev;
3489 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003490 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003491 free_extent_map(em);
3492 em = NULL;
3493
Chris Masonc8b97812008-10-29 14:49:59 -04003494 /*
3495 * compressed and inline extents are written through other
3496 * paths in the FS
3497 */
3498 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003499 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003500 /*
3501 * end_io notification does not happen here for
3502 * compressed extents
3503 */
3504 if (!compressed && tree->ops &&
3505 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003506 tree->ops->writepage_end_io_hook(page, cur,
3507 cur + iosize - 1,
3508 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003509 else if (compressed) {
3510 /* we don't want to end_page_writeback on
3511 * a compressed extent. this happens
3512 * elsewhere
3513 */
3514 nr++;
3515 }
3516
3517 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003518 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003519 continue;
3520 }
Chris Masonc8b97812008-10-29 14:49:59 -04003521
Chris Masond1310b22008-01-24 16:13:08 -05003522 if (tree->ops && tree->ops->writepage_io_hook) {
3523 ret = tree->ops->writepage_io_hook(page, cur,
3524 cur + iosize - 1);
3525 } else {
3526 ret = 0;
3527 }
Chris Mason1259ab72008-05-12 13:39:03 -04003528 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003529 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003530 } else {
Chris Mason40f76582014-05-21 13:35:51 -07003531 unsigned long max_nr = (i_size >> PAGE_CACHE_SHIFT) + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003532
Chris Masond1310b22008-01-24 16:13:08 -05003533 set_range_writeback(tree, cur, cur + iosize - 1);
3534 if (!PageWriteback(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003535 btrfs_err(BTRFS_I(inode)->root->fs_info,
3536 "page %lu not writeback, cur %llu end %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003537 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003538 }
3539
Chris Masonda2f0f72015-07-02 13:57:22 -07003540 ret = submit_extent_page(write_flags, tree, wbc, page,
Chris Masonffbd5172009-04-20 15:50:09 -04003541 sector, iosize, pg_offset,
3542 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003543 end_bio_extent_writepage,
Filipe Manana005efed2015-09-14 09:09:31 +01003544 0, 0, 0, false);
Chris Masond1310b22008-01-24 16:13:08 -05003545 if (ret)
3546 SetPageError(page);
3547 }
3548 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003549 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003550 nr++;
3551 }
3552done:
Chris Mason40f76582014-05-21 13:35:51 -07003553 *nr_ret = nr;
Chris Mason771ed682008-11-06 22:02:51 -05003554
Chris Mason11c83492009-04-20 15:50:09 -04003555done_unlocked:
3556
Chris Mason2c64c532009-09-02 15:04:12 -04003557 /* drop our reference on any cached states */
3558 free_extent_state(cached_state);
Chris Mason40f76582014-05-21 13:35:51 -07003559 return ret;
3560}
3561
3562/*
3563 * the writepage semantics are similar to regular writepage. extent
3564 * records are inserted to lock ranges in the tree, and as dirty areas
3565 * are found, they are marked writeback. Then the lock bits are removed
3566 * and the end_io handler clears the writeback ranges
3567 */
3568static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3569 void *data)
3570{
3571 struct inode *inode = page->mapping->host;
3572 struct extent_page_data *epd = data;
3573 u64 start = page_offset(page);
3574 u64 page_end = start + PAGE_CACHE_SIZE - 1;
3575 int ret;
3576 int nr = 0;
3577 size_t pg_offset = 0;
3578 loff_t i_size = i_size_read(inode);
3579 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
3580 int write_flags;
3581 unsigned long nr_written = 0;
3582
3583 if (wbc->sync_mode == WB_SYNC_ALL)
3584 write_flags = WRITE_SYNC;
3585 else
3586 write_flags = WRITE;
3587
3588 trace___extent_writepage(page, inode, wbc);
3589
3590 WARN_ON(!PageLocked(page));
3591
3592 ClearPageError(page);
3593
3594 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
3595 if (page->index > end_index ||
3596 (page->index == end_index && !pg_offset)) {
3597 page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
3598 unlock_page(page);
3599 return 0;
3600 }
3601
3602 if (page->index == end_index) {
3603 char *userpage;
3604
3605 userpage = kmap_atomic(page);
3606 memset(userpage + pg_offset, 0,
3607 PAGE_CACHE_SIZE - pg_offset);
3608 kunmap_atomic(userpage);
3609 flush_dcache_page(page);
3610 }
3611
3612 pg_offset = 0;
3613
3614 set_page_extent_mapped(page);
3615
3616 ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
3617 if (ret == 1)
3618 goto done_unlocked;
3619 if (ret)
3620 goto done;
3621
3622 ret = __extent_writepage_io(inode, page, wbc, epd,
3623 i_size, nr_written, write_flags, &nr);
3624 if (ret == 1)
3625 goto done_unlocked;
3626
3627done:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003628 if (nr == 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003629 /* make sure the mapping tag for page dirty gets cleared */
Chris Mason771ed682008-11-06 22:02:51 -05003630 set_page_writeback(page);
3631 end_page_writeback(page);
3632 }
Filipe Manana61391d52014-05-09 17:17:40 +01003633 if (PageError(page)) {
3634 ret = ret < 0 ? ret : -EIO;
3635 end_extent_writepage(page, ret, start, page_end);
3636 }
Christoph Hellwigb2950862008-12-02 09:54:17 -05003637 unlock_page(page);
Chris Mason40f76582014-05-21 13:35:51 -07003638 return ret;
Chris Mason4bef0842008-09-08 11:18:08 -04003639
3640done_unlocked:
Chris Masond1310b22008-01-24 16:13:08 -05003641 return 0;
3642}
3643
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003644void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003645{
NeilBrown74316202014-07-07 15:16:04 +10003646 wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
3647 TASK_UNINTERRUPTIBLE);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003648}
3649
Chris Mason0e378df2014-05-19 20:55:27 -07003650static noinline_for_stack int
3651lock_extent_buffer_for_io(struct extent_buffer *eb,
3652 struct btrfs_fs_info *fs_info,
3653 struct extent_page_data *epd)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003654{
3655 unsigned long i, num_pages;
3656 int flush = 0;
3657 int ret = 0;
3658
3659 if (!btrfs_try_tree_write_lock(eb)) {
3660 flush = 1;
3661 flush_write_bio(epd);
3662 btrfs_tree_lock(eb);
3663 }
3664
3665 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3666 btrfs_tree_unlock(eb);
3667 if (!epd->sync_io)
3668 return 0;
3669 if (!flush) {
3670 flush_write_bio(epd);
3671 flush = 1;
3672 }
Chris Masona098d8e2012-03-21 12:09:56 -04003673 while (1) {
3674 wait_on_extent_buffer_writeback(eb);
3675 btrfs_tree_lock(eb);
3676 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3677 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003678 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003679 }
3680 }
3681
Josef Bacik51561ff2012-07-20 16:25:24 -04003682 /*
3683 * We need to do this to prevent races in people who check if the eb is
3684 * under IO since we can end up having no IO bits set for a short period
3685 * of time.
3686 */
3687 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003688 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3689 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003690 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003691 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003692 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3693 -eb->len,
3694 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003695 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003696 } else {
3697 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003698 }
3699
3700 btrfs_tree_unlock(eb);
3701
3702 if (!ret)
3703 return ret;
3704
3705 num_pages = num_extent_pages(eb->start, eb->len);
3706 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003707 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003708
3709 if (!trylock_page(p)) {
3710 if (!flush) {
3711 flush_write_bio(epd);
3712 flush = 1;
3713 }
3714 lock_page(p);
3715 }
3716 }
3717
3718 return ret;
3719}
3720
3721static void end_extent_buffer_writeback(struct extent_buffer *eb)
3722{
3723 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003724 smp_mb__after_atomic();
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003725 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3726}
3727
Filipe Manana656f30d2014-09-26 12:25:56 +01003728static void set_btree_ioerr(struct page *page)
3729{
3730 struct extent_buffer *eb = (struct extent_buffer *)page->private;
3731 struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode);
3732
3733 SetPageError(page);
3734 if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
3735 return;
3736
3737 /*
3738 * If writeback for a btree extent that doesn't belong to a log tree
3739 * failed, increment the counter transaction->eb_write_errors.
3740 * We do this because while the transaction is running and before it's
3741 * committing (when we call filemap_fdata[write|wait]_range against
3742 * the btree inode), we might have
3743 * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
3744 * returns an error or an error happens during writeback, when we're
3745 * committing the transaction we wouldn't know about it, since the pages
3746 * can be no longer dirty nor marked anymore for writeback (if a
3747 * subsequent modification to the extent buffer didn't happen before the
3748 * transaction commit), which makes filemap_fdata[write|wait]_range not
3749 * able to find the pages tagged with SetPageError at transaction
3750 * commit time. So if this happens we must abort the transaction,
3751 * otherwise we commit a super block with btree roots that point to
3752 * btree nodes/leafs whose content on disk is invalid - either garbage
3753 * or the content of some node/leaf from a past generation that got
3754 * cowed or deleted and is no longer valid.
3755 *
3756 * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
3757 * not be enough - we need to distinguish between log tree extents vs
3758 * non-log tree extents, and the next filemap_fdatawait_range() call
3759 * will catch and clear such errors in the mapping - and that call might
3760 * be from a log sync and not from a transaction commit. Also, checking
3761 * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
3762 * not done and would not be reliable - the eb might have been released
3763 * from memory and reading it back again means that flag would not be
3764 * set (since it's a runtime flag, not persisted on disk).
3765 *
3766 * Using the flags below in the btree inode also makes us achieve the
3767 * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
3768 * writeback for all dirty pages and before filemap_fdatawait_range()
3769 * is called, the writeback for all dirty pages had already finished
3770 * with errors - because we were not using AS_EIO/AS_ENOSPC,
3771 * filemap_fdatawait_range() would return success, as it could not know
3772 * that writeback errors happened (the pages were no longer tagged for
3773 * writeback).
3774 */
3775 switch (eb->log_index) {
3776 case -1:
3777 set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags);
3778 break;
3779 case 0:
3780 set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
3781 break;
3782 case 1:
3783 set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
3784 break;
3785 default:
3786 BUG(); /* unexpected, logic error */
3787 }
3788}
3789
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003790static void end_bio_extent_buffer_writepage(struct bio *bio)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003791{
Kent Overstreet2c30c712013-11-07 12:20:26 -08003792 struct bio_vec *bvec;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003793 struct extent_buffer *eb;
Kent Overstreet2c30c712013-11-07 12:20:26 -08003794 int i, done;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003795
Kent Overstreet2c30c712013-11-07 12:20:26 -08003796 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003797 struct page *page = bvec->bv_page;
3798
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003799 eb = (struct extent_buffer *)page->private;
3800 BUG_ON(!eb);
3801 done = atomic_dec_and_test(&eb->io_pages);
3802
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003803 if (bio->bi_error ||
3804 test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003805 ClearPageUptodate(page);
Filipe Manana656f30d2014-09-26 12:25:56 +01003806 set_btree_ioerr(page);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003807 }
3808
3809 end_page_writeback(page);
3810
3811 if (!done)
3812 continue;
3813
3814 end_extent_buffer_writeback(eb);
Kent Overstreet2c30c712013-11-07 12:20:26 -08003815 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003816
3817 bio_put(bio);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003818}
3819
Chris Mason0e378df2014-05-19 20:55:27 -07003820static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003821 struct btrfs_fs_info *fs_info,
3822 struct writeback_control *wbc,
3823 struct extent_page_data *epd)
3824{
3825 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
Josef Bacikf28491e2013-12-16 13:24:27 -05003826 struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003827 u64 offset = eb->start;
3828 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003829 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003830 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003831 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003832
Filipe Manana656f30d2014-09-26 12:25:56 +01003833 clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003834 num_pages = num_extent_pages(eb->start, eb->len);
3835 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003836 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3837 bio_flags = EXTENT_BIO_TREE_LOG;
3838
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003839 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02003840 struct page *p = eb->pages[i];
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003841
3842 clear_page_dirty_for_io(p);
3843 set_page_writeback(p);
Chris Masonda2f0f72015-07-02 13:57:22 -07003844 ret = submit_extent_page(rw, tree, wbc, p, offset >> 9,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003845 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3846 -1, end_bio_extent_buffer_writepage,
Filipe Manana005efed2015-09-14 09:09:31 +01003847 0, epd->bio_flags, bio_flags, false);
Josef Bacikde0022b2012-09-25 14:25:58 -04003848 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003849 if (ret) {
Filipe Manana656f30d2014-09-26 12:25:56 +01003850 set_btree_ioerr(p);
Filipe Manana55e3bd22014-09-22 17:41:04 +01003851 end_page_writeback(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003852 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3853 end_extent_buffer_writeback(eb);
3854 ret = -EIO;
3855 break;
3856 }
3857 offset += PAGE_CACHE_SIZE;
3858 update_nr_written(p, wbc, 1);
3859 unlock_page(p);
3860 }
3861
3862 if (unlikely(ret)) {
3863 for (; i < num_pages; i++) {
Chris Masonbbf65cf2014-10-04 09:56:45 -07003864 struct page *p = eb->pages[i];
Liu Bo81465022014-09-23 22:22:33 +08003865 clear_page_dirty_for_io(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003866 unlock_page(p);
3867 }
3868 }
3869
3870 return ret;
3871}
3872
3873int btree_write_cache_pages(struct address_space *mapping,
3874 struct writeback_control *wbc)
3875{
3876 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3877 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3878 struct extent_buffer *eb, *prev_eb = NULL;
3879 struct extent_page_data epd = {
3880 .bio = NULL,
3881 .tree = tree,
3882 .extent_locked = 0,
3883 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003884 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003885 };
3886 int ret = 0;
3887 int done = 0;
3888 int nr_to_write_done = 0;
3889 struct pagevec pvec;
3890 int nr_pages;
3891 pgoff_t index;
3892 pgoff_t end; /* Inclusive */
3893 int scanned = 0;
3894 int tag;
3895
3896 pagevec_init(&pvec, 0);
3897 if (wbc->range_cyclic) {
3898 index = mapping->writeback_index; /* Start from prev offset */
3899 end = -1;
3900 } else {
3901 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3902 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3903 scanned = 1;
3904 }
3905 if (wbc->sync_mode == WB_SYNC_ALL)
3906 tag = PAGECACHE_TAG_TOWRITE;
3907 else
3908 tag = PAGECACHE_TAG_DIRTY;
3909retry:
3910 if (wbc->sync_mode == WB_SYNC_ALL)
3911 tag_pages_for_writeback(mapping, index, end);
3912 while (!done && !nr_to_write_done && (index <= end) &&
3913 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3914 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3915 unsigned i;
3916
3917 scanned = 1;
3918 for (i = 0; i < nr_pages; i++) {
3919 struct page *page = pvec.pages[i];
3920
3921 if (!PagePrivate(page))
3922 continue;
3923
3924 if (!wbc->range_cyclic && page->index > end) {
3925 done = 1;
3926 break;
3927 }
3928
Josef Bacikb5bae262012-09-14 13:43:01 -04003929 spin_lock(&mapping->private_lock);
3930 if (!PagePrivate(page)) {
3931 spin_unlock(&mapping->private_lock);
3932 continue;
3933 }
3934
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003935 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003936
3937 /*
3938 * Shouldn't happen and normally this would be a BUG_ON
3939 * but no sense in crashing the users box for something
3940 * we can survive anyway.
3941 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303942 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003943 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003944 continue;
3945 }
3946
Josef Bacikb5bae262012-09-14 13:43:01 -04003947 if (eb == prev_eb) {
3948 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003949 continue;
3950 }
3951
Josef Bacikb5bae262012-09-14 13:43:01 -04003952 ret = atomic_inc_not_zero(&eb->refs);
3953 spin_unlock(&mapping->private_lock);
3954 if (!ret)
3955 continue;
3956
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003957 prev_eb = eb;
3958 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3959 if (!ret) {
3960 free_extent_buffer(eb);
3961 continue;
3962 }
3963
3964 ret = write_one_eb(eb, fs_info, wbc, &epd);
3965 if (ret) {
3966 done = 1;
3967 free_extent_buffer(eb);
3968 break;
3969 }
3970 free_extent_buffer(eb);
3971
3972 /*
3973 * the filesystem may choose to bump up nr_to_write.
3974 * We have to make sure to honor the new nr_to_write
3975 * at any time
3976 */
3977 nr_to_write_done = wbc->nr_to_write <= 0;
3978 }
3979 pagevec_release(&pvec);
3980 cond_resched();
3981 }
3982 if (!scanned && !done) {
3983 /*
3984 * We hit the last page and there is more work to be done: wrap
3985 * back to the start of the file
3986 */
3987 scanned = 1;
3988 index = 0;
3989 goto retry;
3990 }
3991 flush_write_bio(&epd);
3992 return ret;
3993}
3994
Chris Masond1310b22008-01-24 16:13:08 -05003995/**
3996 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
3997 * @mapping: address space structure to write
3998 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3999 * @writepage: function called for each page
4000 * @data: data passed to writepage function
4001 *
4002 * If a page is already under I/O, write_cache_pages() skips it, even
4003 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
4004 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
4005 * and msync() need to guarantee that all the data which was dirty at the time
4006 * the call was made get new I/O started against them. If wbc->sync_mode is
4007 * WB_SYNC_ALL then we were called for data integrity and we must wait for
4008 * existing IO to complete.
4009 */
Chris Mason4bef0842008-09-08 11:18:08 -04004010static int extent_write_cache_pages(struct extent_io_tree *tree,
4011 struct address_space *mapping,
4012 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004013 writepage_t writepage, void *data,
4014 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05004015{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004016 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05004017 int ret = 0;
4018 int done = 0;
Filipe Manana61391d52014-05-09 17:17:40 +01004019 int err = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004020 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004021 struct pagevec pvec;
4022 int nr_pages;
4023 pgoff_t index;
4024 pgoff_t end; /* Inclusive */
4025 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00004026 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05004027
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004028 /*
4029 * We have to hold onto the inode so that ordered extents can do their
4030 * work when the IO finishes. The alternative to this is failing to add
4031 * an ordered extent if the igrab() fails there and that is a huge pain
4032 * to deal with, so instead just hold onto the inode throughout the
4033 * writepages operation. If it fails here we are freeing up the inode
4034 * anyway and we'd rather not waste our time writing out stuff that is
4035 * going to be truncated anyway.
4036 */
4037 if (!igrab(inode))
4038 return 0;
4039
Chris Masond1310b22008-01-24 16:13:08 -05004040 pagevec_init(&pvec, 0);
4041 if (wbc->range_cyclic) {
4042 index = mapping->writeback_index; /* Start from prev offset */
4043 end = -1;
4044 } else {
4045 index = wbc->range_start >> PAGE_CACHE_SHIFT;
4046 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004047 scanned = 1;
4048 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00004049 if (wbc->sync_mode == WB_SYNC_ALL)
4050 tag = PAGECACHE_TAG_TOWRITE;
4051 else
4052 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05004053retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00004054 if (wbc->sync_mode == WB_SYNC_ALL)
4055 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004056 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00004057 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
4058 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05004059 unsigned i;
4060
4061 scanned = 1;
4062 for (i = 0; i < nr_pages; i++) {
4063 struct page *page = pvec.pages[i];
4064
4065 /*
4066 * At this point we hold neither mapping->tree_lock nor
4067 * lock on the page itself: the page may be truncated or
4068 * invalidated (changing page->mapping to NULL), or even
4069 * swizzled back from swapper_space to tmpfs file
4070 * mapping
4071 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05004072 if (!trylock_page(page)) {
4073 flush_fn(data);
4074 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04004075 }
Chris Masond1310b22008-01-24 16:13:08 -05004076
4077 if (unlikely(page->mapping != mapping)) {
4078 unlock_page(page);
4079 continue;
4080 }
4081
4082 if (!wbc->range_cyclic && page->index > end) {
4083 done = 1;
4084 unlock_page(page);
4085 continue;
4086 }
4087
Chris Masond2c3f4f2008-11-19 12:44:22 -05004088 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05004089 if (PageWriteback(page))
4090 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05004091 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004092 }
Chris Masond1310b22008-01-24 16:13:08 -05004093
4094 if (PageWriteback(page) ||
4095 !clear_page_dirty_for_io(page)) {
4096 unlock_page(page);
4097 continue;
4098 }
4099
4100 ret = (*writepage)(page, wbc, data);
4101
4102 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
4103 unlock_page(page);
4104 ret = 0;
4105 }
Filipe Manana61391d52014-05-09 17:17:40 +01004106 if (!err && ret < 0)
4107 err = ret;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04004108
4109 /*
4110 * the filesystem may choose to bump up nr_to_write.
4111 * We have to make sure to honor the new nr_to_write
4112 * at any time
4113 */
4114 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05004115 }
4116 pagevec_release(&pvec);
4117 cond_resched();
4118 }
Filipe Manana61391d52014-05-09 17:17:40 +01004119 if (!scanned && !done && !err) {
Chris Masond1310b22008-01-24 16:13:08 -05004120 /*
4121 * We hit the last page and there is more work to be done: wrap
4122 * back to the start of the file
4123 */
4124 scanned = 1;
4125 index = 0;
4126 goto retry;
4127 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04004128 btrfs_add_delayed_iput(inode);
Filipe Manana61391d52014-05-09 17:17:40 +01004129 return err;
Chris Masond1310b22008-01-24 16:13:08 -05004130}
Chris Masond1310b22008-01-24 16:13:08 -05004131
Chris Masonffbd5172009-04-20 15:50:09 -04004132static void flush_epd_write_bio(struct extent_page_data *epd)
4133{
4134 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04004135 int rw = WRITE;
4136 int ret;
4137
Chris Masonffbd5172009-04-20 15:50:09 -04004138 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04004139 rw = WRITE_SYNC;
4140
Josef Bacikde0022b2012-09-25 14:25:58 -04004141 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04004143 epd->bio = NULL;
4144 }
4145}
4146
Chris Masond2c3f4f2008-11-19 12:44:22 -05004147static noinline void flush_write_bio(void *data)
4148{
4149 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04004150 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05004151}
4152
Chris Masond1310b22008-01-24 16:13:08 -05004153int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
4154 get_extent_t *get_extent,
4155 struct writeback_control *wbc)
4156{
4157 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004158 struct extent_page_data epd = {
4159 .bio = NULL,
4160 .tree = tree,
4161 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004162 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004163 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004164 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004165 };
Chris Masond1310b22008-01-24 16:13:08 -05004166
Chris Masond1310b22008-01-24 16:13:08 -05004167 ret = __extent_writepage(page, wbc, &epd);
4168
Chris Masonffbd5172009-04-20 15:50:09 -04004169 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004170 return ret;
4171}
Chris Masond1310b22008-01-24 16:13:08 -05004172
Chris Mason771ed682008-11-06 22:02:51 -05004173int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
4174 u64 start, u64 end, get_extent_t *get_extent,
4175 int mode)
4176{
4177 int ret = 0;
4178 struct address_space *mapping = inode->i_mapping;
4179 struct page *page;
4180 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
4181 PAGE_CACHE_SHIFT;
4182
4183 struct extent_page_data epd = {
4184 .bio = NULL,
4185 .tree = tree,
4186 .get_extent = get_extent,
4187 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04004188 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004189 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05004190 };
4191 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05004192 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05004193 .nr_to_write = nr_pages * 2,
4194 .range_start = start,
4195 .range_end = end + 1,
4196 };
4197
Chris Masond3977122009-01-05 21:25:51 -05004198 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05004199 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
4200 if (clear_page_dirty_for_io(page))
4201 ret = __extent_writepage(page, &wbc_writepages, &epd);
4202 else {
4203 if (tree->ops && tree->ops->writepage_end_io_hook)
4204 tree->ops->writepage_end_io_hook(page, start,
4205 start + PAGE_CACHE_SIZE - 1,
4206 NULL, 1);
4207 unlock_page(page);
4208 }
4209 page_cache_release(page);
4210 start += PAGE_CACHE_SIZE;
4211 }
4212
Chris Masonffbd5172009-04-20 15:50:09 -04004213 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05004214 return ret;
4215}
Chris Masond1310b22008-01-24 16:13:08 -05004216
4217int extent_writepages(struct extent_io_tree *tree,
4218 struct address_space *mapping,
4219 get_extent_t *get_extent,
4220 struct writeback_control *wbc)
4221{
4222 int ret = 0;
4223 struct extent_page_data epd = {
4224 .bio = NULL,
4225 .tree = tree,
4226 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05004227 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04004228 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04004229 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05004230 };
4231
Chris Mason4bef0842008-09-08 11:18:08 -04004232 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05004233 __extent_writepage, &epd,
4234 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04004235 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05004236 return ret;
4237}
Chris Masond1310b22008-01-24 16:13:08 -05004238
4239int extent_readpages(struct extent_io_tree *tree,
4240 struct address_space *mapping,
4241 struct list_head *pages, unsigned nr_pages,
4242 get_extent_t get_extent)
4243{
4244 struct bio *bio = NULL;
4245 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04004246 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06004247 struct page *pagepool[16];
4248 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08004249 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06004250 int nr = 0;
Filipe Manana808f80b2015-09-28 09:56:26 +01004251 u64 prev_em_start = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05004252
Chris Masond1310b22008-01-24 16:13:08 -05004253 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06004254 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05004255
4256 prefetchw(&page->flags);
4257 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06004258 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04004259 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06004260 page_cache_release(page);
4261 continue;
Chris Masond1310b22008-01-24 16:13:08 -05004262 }
Liu Bo67c96842012-07-20 21:43:09 -06004263
4264 pagepool[nr++] = page;
4265 if (nr < ARRAY_SIZE(pagepool))
4266 continue;
Miao Xie125bac012013-07-25 19:22:37 +08004267 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Filipe Manana808f80b2015-09-28 09:56:26 +01004268 &bio, 0, &bio_flags, READ, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004269 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004270 }
Miao Xie99740902013-07-25 19:22:36 +08004271 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08004272 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Filipe Manana808f80b2015-09-28 09:56:26 +01004273 &bio, 0, &bio_flags, READ, &prev_em_start);
Liu Bo67c96842012-07-20 21:43:09 -06004274
Miao Xie125bac012013-07-25 19:22:37 +08004275 if (em_cached)
4276 free_extent_map(em_cached);
4277
Chris Masond1310b22008-01-24 16:13:08 -05004278 BUG_ON(!list_empty(pages));
4279 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05004281 return 0;
4282}
Chris Masond1310b22008-01-24 16:13:08 -05004283
4284/*
4285 * basic invalidatepage code, this waits on any locked or writeback
4286 * ranges corresponding to the page, and then deletes any extent state
4287 * records from the tree
4288 */
4289int extent_invalidatepage(struct extent_io_tree *tree,
4290 struct page *page, unsigned long offset)
4291{
Josef Bacik2ac55d42010-02-03 19:33:23 +00004292 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004293 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05004294 u64 end = start + PAGE_CACHE_SIZE - 1;
4295 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
4296
Qu Wenruofda28322013-02-26 08:10:22 +00004297 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05004298 if (start > end)
4299 return 0;
4300
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004301 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04004302 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004303 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004304 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4305 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004306 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004307 return 0;
4308}
Chris Masond1310b22008-01-24 16:13:08 -05004309
4310/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04004311 * a helper for releasepage, this tests for areas of the page that
4312 * are locked or under IO and drops the related state bits if it is safe
4313 * to drop the page.
4314 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004315static int try_release_extent_state(struct extent_map_tree *map,
4316 struct extent_io_tree *tree,
4317 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04004318{
Miao Xie4eee4fa2012-12-21 09:17:45 +00004319 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04004320 u64 end = start + PAGE_CACHE_SIZE - 1;
4321 int ret = 1;
4322
Chris Mason211f90e2008-07-18 11:56:15 -04004323 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04004324 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04004325 ret = 0;
4326 else {
4327 if ((mask & GFP_NOFS) == GFP_NOFS)
4328 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04004329 /*
4330 * at this point we can safely clear everything except the
4331 * locked bit and the nodatasum bit
4332 */
Chris Masone3f24cc2011-02-14 12:52:08 -05004333 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04004334 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
4335 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05004336
4337 /* if clear_extent_bit failed for enomem reasons,
4338 * we can't allow the release to continue.
4339 */
4340 if (ret < 0)
4341 ret = 0;
4342 else
4343 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004344 }
4345 return ret;
4346}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004347
4348/*
Chris Masond1310b22008-01-24 16:13:08 -05004349 * a helper for releasepage. As long as there are no locked extents
4350 * in the range corresponding to the page, both state records and extent
4351 * map records are removed
4352 */
4353int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004354 struct extent_io_tree *tree, struct page *page,
4355 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004356{
4357 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004358 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05004359 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004360
Chris Mason70dec802008-01-29 09:59:12 -05004361 if ((mask & __GFP_WAIT) &&
4362 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05004363 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004364 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004365 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004366 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004367 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004368 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004369 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004370 break;
4371 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004372 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4373 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004374 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004375 free_extent_map(em);
4376 break;
4377 }
4378 if (!test_range_bit(tree, em->start,
4379 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004380 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004381 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004382 remove_extent_mapping(map, em);
4383 /* once for the rb tree */
4384 free_extent_map(em);
4385 }
4386 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004387 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004388
4389 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004390 free_extent_map(em);
4391 }
Chris Masond1310b22008-01-24 16:13:08 -05004392 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004393 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004394}
Chris Masond1310b22008-01-24 16:13:08 -05004395
Chris Masonec29ed52011-02-23 16:23:20 -05004396/*
4397 * helper function for fiemap, which doesn't want to see any holes.
4398 * This maps until we find something past 'last'
4399 */
4400static struct extent_map *get_extent_skip_holes(struct inode *inode,
4401 u64 offset,
4402 u64 last,
4403 get_extent_t *get_extent)
4404{
4405 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
4406 struct extent_map *em;
4407 u64 len;
4408
4409 if (offset >= last)
4410 return NULL;
4411
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304412 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004413 len = last - offset;
4414 if (len == 0)
4415 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004416 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05004417 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004418 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004419 return em;
4420
4421 /* if this isn't a hole return it */
4422 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4423 em->block_start != EXTENT_MAP_HOLE) {
4424 return em;
4425 }
4426
4427 /* this is a hole, advance to the next extent */
4428 offset = extent_map_end(em);
4429 free_extent_map(em);
4430 if (offset >= last)
4431 break;
4432 }
4433 return NULL;
4434}
4435
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004436int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4437 __u64 start, __u64 len, get_extent_t *get_extent)
4438{
Josef Bacik975f84f2010-11-23 19:36:57 +00004439 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004440 u64 off = start;
4441 u64 max = start + len;
4442 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004443 u32 found_type;
4444 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004445 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004446 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004447 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004448 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004449 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004450 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004451 struct btrfs_path *path;
Josef Bacikdc046b12014-09-10 16:20:45 -04004452 struct btrfs_root *root = BTRFS_I(inode)->root;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004453 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004454 u64 em_start = 0;
4455 u64 em_len = 0;
4456 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004457
4458 if (len == 0)
4459 return -EINVAL;
4460
Josef Bacik975f84f2010-11-23 19:36:57 +00004461 path = btrfs_alloc_path();
4462 if (!path)
4463 return -ENOMEM;
4464 path->leave_spinning = 1;
4465
Qu Wenruo2c919432014-07-18 09:55:43 +08004466 start = round_down(start, BTRFS_I(inode)->root->sectorsize);
4467 len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start;
Josef Bacik4d479cf2011-11-17 11:34:31 -05004468
Chris Masonec29ed52011-02-23 16:23:20 -05004469 /*
4470 * lookup the last file extent. We're not using i_size here
4471 * because there might be preallocation past i_size
4472 */
Josef Bacikdc046b12014-09-10 16:20:45 -04004473 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
4474 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004475 if (ret < 0) {
4476 btrfs_free_path(path);
4477 return ret;
4478 }
4479 WARN_ON(!ret);
4480 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004481 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004482 found_type = found_key.type;
Josef Bacik975f84f2010-11-23 19:36:57 +00004483
Chris Masonec29ed52011-02-23 16:23:20 -05004484 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08004485 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004486 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004487 /* have to trust i_size as the end */
4488 last = (u64)-1;
4489 last_for_get_extent = isize;
4490 } else {
4491 /*
4492 * remember the start of the last extent. There are a
4493 * bunch of different factors that go into the length of the
4494 * extent, so its much less complex to remember where it started
4495 */
4496 last = found_key.offset;
4497 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004498 }
Liu Bofe09e162013-09-22 12:54:23 +08004499 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004500
Chris Masonec29ed52011-02-23 16:23:20 -05004501 /*
4502 * we might have some extents allocated but more delalloc past those
4503 * extents. so, we trust isize unless the start of the last extent is
4504 * beyond isize
4505 */
4506 if (last < isize) {
4507 last = (u64)-1;
4508 last_for_get_extent = isize;
4509 }
4510
Liu Boa52f4cd2013-05-01 16:23:41 +00004511 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004512 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004513
Josef Bacik4d479cf2011-11-17 11:34:31 -05004514 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004515 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004516 if (!em)
4517 goto out;
4518 if (IS_ERR(em)) {
4519 ret = PTR_ERR(em);
4520 goto out;
4521 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004522
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004523 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004524 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004525
Chris Masonea8efc72011-03-08 11:54:40 -05004526 /* break if the extent we found is outside the range */
4527 if (em->start >= max || extent_map_end(em) < off)
4528 break;
4529
4530 /*
4531 * get_extent may return an extent that starts before our
4532 * requested range. We have to make sure the ranges
4533 * we return to fiemap always move forward and don't
4534 * overlap, so adjust the offsets here
4535 */
4536 em_start = max(em->start, off);
4537
4538 /*
4539 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004540 * for adjusting the disk offset below. Only do this if the
4541 * extent isn't compressed since our in ram offset may be past
4542 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004543 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004544 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4545 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004546 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004547 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004548 disko = 0;
4549 flags = 0;
4550
Chris Masonea8efc72011-03-08 11:54:40 -05004551 /*
4552 * bump off for our next call to get_extent
4553 */
4554 off = extent_map_end(em);
4555 if (off >= max)
4556 end = 1;
4557
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004558 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004559 end = 1;
4560 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004561 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004562 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4563 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004564 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004565 flags |= (FIEMAP_EXTENT_DELALLOC |
4566 FIEMAP_EXTENT_UNKNOWN);
Josef Bacikdc046b12014-09-10 16:20:45 -04004567 } else if (fieinfo->fi_extents_max) {
4568 u64 bytenr = em->block_start -
4569 (em->start - em->orig_start);
Liu Bofe09e162013-09-22 12:54:23 +08004570
Chris Masonea8efc72011-03-08 11:54:40 -05004571 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004572
4573 /*
4574 * As btrfs supports shared space, this information
4575 * can be exported to userspace tools via
Josef Bacikdc046b12014-09-10 16:20:45 -04004576 * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
4577 * then we're just getting a count and we can skip the
4578 * lookup stuff.
Liu Bofe09e162013-09-22 12:54:23 +08004579 */
Josef Bacikdc046b12014-09-10 16:20:45 -04004580 ret = btrfs_check_shared(NULL, root->fs_info,
4581 root->objectid,
4582 btrfs_ino(inode), bytenr);
4583 if (ret < 0)
Liu Bofe09e162013-09-22 12:54:23 +08004584 goto out_free;
Josef Bacikdc046b12014-09-10 16:20:45 -04004585 if (ret)
Liu Bofe09e162013-09-22 12:54:23 +08004586 flags |= FIEMAP_EXTENT_SHARED;
Josef Bacikdc046b12014-09-10 16:20:45 -04004587 ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004588 }
4589 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4590 flags |= FIEMAP_EXTENT_ENCODED;
Josef Bacik0d2b2372015-05-19 10:44:04 -04004591 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4592 flags |= FIEMAP_EXTENT_UNWRITTEN;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004593
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004594 free_extent_map(em);
4595 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004596 if ((em_start >= last) || em_len == (u64)-1 ||
4597 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004598 flags |= FIEMAP_EXTENT_LAST;
4599 end = 1;
4600 }
4601
Chris Masonec29ed52011-02-23 16:23:20 -05004602 /* now scan forward to see if this is really the last extent. */
4603 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4604 get_extent);
4605 if (IS_ERR(em)) {
4606 ret = PTR_ERR(em);
4607 goto out;
4608 }
4609 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004610 flags |= FIEMAP_EXTENT_LAST;
4611 end = 1;
4612 }
Chris Masonec29ed52011-02-23 16:23:20 -05004613 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4614 em_len, flags);
Chengyu Song26e726a2015-03-24 18:12:56 -04004615 if (ret) {
4616 if (ret == 1)
4617 ret = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004618 goto out_free;
Chengyu Song26e726a2015-03-24 18:12:56 -04004619 }
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004620 }
4621out_free:
4622 free_extent_map(em);
4623out:
Liu Bofe09e162013-09-22 12:54:23 +08004624 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004625 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004626 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004627 return ret;
4628}
4629
Chris Mason727011e2010-08-06 13:21:20 -04004630static void __free_extent_buffer(struct extent_buffer *eb)
4631{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004632 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004633 kmem_cache_free(extent_buffer_cache, eb);
4634}
4635
Josef Bacika26e8c92014-03-28 17:07:27 -04004636int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004637{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004638 return (atomic_read(&eb->io_pages) ||
4639 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4640 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004641}
4642
Miao Xie897ca6e2010-10-26 20:57:29 -04004643/*
4644 * Helper for releasing extent buffer page.
4645 */
David Sterbaa50924e2014-07-31 00:51:36 +02004646static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
Miao Xie897ca6e2010-10-26 20:57:29 -04004647{
4648 unsigned long index;
4649 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004650 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004651
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004652 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004653
David Sterbaa50924e2014-07-31 00:51:36 +02004654 index = num_extent_pages(eb->start, eb->len);
4655 if (index == 0)
Miao Xie897ca6e2010-10-26 20:57:29 -04004656 return;
4657
4658 do {
4659 index--;
David Sterbafb85fc92014-07-31 01:03:53 +02004660 page = eb->pages[index];
Forrest Liu5d2361d2015-02-09 17:31:45 +08004661 if (!page)
4662 continue;
4663 if (mapped)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004664 spin_lock(&page->mapping->private_lock);
Forrest Liu5d2361d2015-02-09 17:31:45 +08004665 /*
4666 * We do this since we'll remove the pages after we've
4667 * removed the eb from the radix tree, so we could race
4668 * and have this page now attached to the new eb. So
4669 * only clear page_private if it's still connected to
4670 * this eb.
4671 */
4672 if (PagePrivate(page) &&
4673 page->private == (unsigned long)eb) {
4674 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
4675 BUG_ON(PageDirty(page));
4676 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004677 /*
Forrest Liu5d2361d2015-02-09 17:31:45 +08004678 * We need to make sure we haven't be attached
4679 * to a new eb.
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004680 */
Forrest Liu5d2361d2015-02-09 17:31:45 +08004681 ClearPagePrivate(page);
4682 set_page_private(page, 0);
4683 /* One for the page private */
Miao Xie897ca6e2010-10-26 20:57:29 -04004684 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004685 }
Forrest Liu5d2361d2015-02-09 17:31:45 +08004686
4687 if (mapped)
4688 spin_unlock(&page->mapping->private_lock);
4689
4690 /* One for when we alloced the page */
4691 page_cache_release(page);
David Sterbaa50924e2014-07-31 00:51:36 +02004692 } while (index != 0);
Miao Xie897ca6e2010-10-26 20:57:29 -04004693}
4694
4695/*
4696 * Helper for releasing the extent buffer.
4697 */
4698static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4699{
David Sterbaa50924e2014-07-31 00:51:36 +02004700 btrfs_release_extent_buffer_page(eb);
Miao Xie897ca6e2010-10-26 20:57:29 -04004701 __free_extent_buffer(eb);
4702}
4703
Josef Bacikf28491e2013-12-16 13:24:27 -05004704static struct extent_buffer *
4705__alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
David Sterba23d79d82014-06-15 02:55:29 +02004706 unsigned long len)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004707{
4708 struct extent_buffer *eb = NULL;
4709
Michal Hockod1b5c562015-08-19 14:17:40 +02004710 eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004711 eb->start = start;
4712 eb->len = len;
Josef Bacikf28491e2013-12-16 13:24:27 -05004713 eb->fs_info = fs_info;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004714 eb->bflags = 0;
4715 rwlock_init(&eb->lock);
4716 atomic_set(&eb->write_locks, 0);
4717 atomic_set(&eb->read_locks, 0);
4718 atomic_set(&eb->blocking_readers, 0);
4719 atomic_set(&eb->blocking_writers, 0);
4720 atomic_set(&eb->spinning_readers, 0);
4721 atomic_set(&eb->spinning_writers, 0);
4722 eb->lock_nested = 0;
4723 init_waitqueue_head(&eb->write_lock_wq);
4724 init_waitqueue_head(&eb->read_lock_wq);
4725
4726 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4727
4728 spin_lock_init(&eb->refs_lock);
4729 atomic_set(&eb->refs, 1);
4730 atomic_set(&eb->io_pages, 0);
4731
4732 /*
4733 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4734 */
4735 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4736 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4737 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4738
4739 return eb;
4740}
4741
4742struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4743{
4744 unsigned long i;
4745 struct page *p;
4746 struct extent_buffer *new;
4747 unsigned long num_pages = num_extent_pages(src->start, src->len);
4748
David Sterba3f556f72014-06-15 03:20:26 +02004749 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004750 if (new == NULL)
4751 return NULL;
4752
4753 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004754 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004755 if (!p) {
4756 btrfs_release_extent_buffer(new);
4757 return NULL;
4758 }
4759 attach_extent_buffer_page(new, p);
4760 WARN_ON(PageDirty(p));
4761 SetPageUptodate(p);
4762 new->pages[i] = p;
4763 }
4764
4765 copy_extent_buffer(new, src, 0, 0, src->len);
4766 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4767 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4768
4769 return new;
4770}
4771
David Sterba3f556f72014-06-15 03:20:26 +02004772struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
4773 u64 start)
Josef Bacikdb7f3432013-08-07 14:54:37 -04004774{
4775 struct extent_buffer *eb;
David Sterba3f556f72014-06-15 03:20:26 +02004776 unsigned long len;
4777 unsigned long num_pages;
Josef Bacikdb7f3432013-08-07 14:54:37 -04004778 unsigned long i;
4779
David Sterba3f556f72014-06-15 03:20:26 +02004780 if (!fs_info) {
4781 /*
4782 * Called only from tests that don't always have a fs_info
4783 * available, but we know that nodesize is 4096
4784 */
4785 len = 4096;
4786 } else {
4787 len = fs_info->tree_root->nodesize;
4788 }
4789 num_pages = num_extent_pages(0, len);
4790
4791 eb = __alloc_extent_buffer(fs_info, start, len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004792 if (!eb)
4793 return NULL;
4794
4795 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004796 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004797 if (!eb->pages[i])
4798 goto err;
4799 }
4800 set_extent_buffer_uptodate(eb);
4801 btrfs_set_header_nritems(eb, 0);
4802 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4803
4804 return eb;
4805err:
4806 for (; i > 0; i--)
4807 __free_page(eb->pages[i - 1]);
4808 __free_extent_buffer(eb);
4809 return NULL;
4810}
4811
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004812static void check_buffer_tree_ref(struct extent_buffer *eb)
4813{
Chris Mason242e18c2013-01-29 17:49:37 -05004814 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004815 /* the ref bit is tricky. We have to make sure it is set
4816 * if we have the buffer dirty. Otherwise the
4817 * code to free a buffer can end up dropping a dirty
4818 * page
4819 *
4820 * Once the ref bit is set, it won't go away while the
4821 * buffer is dirty or in writeback, and it also won't
4822 * go away while we have the reference count on the
4823 * eb bumped.
4824 *
4825 * We can't just set the ref bit without bumping the
4826 * ref on the eb because free_extent_buffer might
4827 * see the ref bit and try to clear it. If this happens
4828 * free_extent_buffer might end up dropping our original
4829 * ref by mistake and freeing the page before we are able
4830 * to add one more ref.
4831 *
4832 * So bump the ref count first, then set the bit. If someone
4833 * beat us to it, drop the ref we added.
4834 */
Chris Mason242e18c2013-01-29 17:49:37 -05004835 refs = atomic_read(&eb->refs);
4836 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4837 return;
4838
Josef Bacik594831c2012-07-20 16:11:08 -04004839 spin_lock(&eb->refs_lock);
4840 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004841 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004842 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004843}
4844
Mel Gorman2457aec2014-06-04 16:10:31 -07004845static void mark_extent_buffer_accessed(struct extent_buffer *eb,
4846 struct page *accessed)
Josef Bacik5df42352012-03-15 18:24:42 -04004847{
4848 unsigned long num_pages, i;
4849
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004850 check_buffer_tree_ref(eb);
4851
Josef Bacik5df42352012-03-15 18:24:42 -04004852 num_pages = num_extent_pages(eb->start, eb->len);
4853 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02004854 struct page *p = eb->pages[i];
4855
Mel Gorman2457aec2014-06-04 16:10:31 -07004856 if (p != accessed)
4857 mark_page_accessed(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004858 }
4859}
4860
Josef Bacikf28491e2013-12-16 13:24:27 -05004861struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
4862 u64 start)
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004863{
4864 struct extent_buffer *eb;
4865
4866 rcu_read_lock();
Josef Bacikf28491e2013-12-16 13:24:27 -05004867 eb = radix_tree_lookup(&fs_info->buffer_radix,
4868 start >> PAGE_CACHE_SHIFT);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004869 if (eb && atomic_inc_not_zero(&eb->refs)) {
4870 rcu_read_unlock();
Filipe Manana062c19e2015-04-23 11:28:48 +01004871 /*
4872 * Lock our eb's refs_lock to avoid races with
4873 * free_extent_buffer. When we get our eb it might be flagged
4874 * with EXTENT_BUFFER_STALE and another task running
4875 * free_extent_buffer might have seen that flag set,
4876 * eb->refs == 2, that the buffer isn't under IO (dirty and
4877 * writeback flags not set) and it's still in the tree (flag
4878 * EXTENT_BUFFER_TREE_REF set), therefore being in the process
4879 * of decrementing the extent buffer's reference count twice.
4880 * So here we could race and increment the eb's reference count,
4881 * clear its stale flag, mark it as dirty and drop our reference
4882 * before the other task finishes executing free_extent_buffer,
4883 * which would later result in an attempt to free an extent
4884 * buffer that is dirty.
4885 */
4886 if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
4887 spin_lock(&eb->refs_lock);
4888 spin_unlock(&eb->refs_lock);
4889 }
Mel Gorman2457aec2014-06-04 16:10:31 -07004890 mark_extent_buffer_accessed(eb, NULL);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004891 return eb;
4892 }
4893 rcu_read_unlock();
4894
4895 return NULL;
4896}
4897
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004898#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4899struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004900 u64 start)
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004901{
4902 struct extent_buffer *eb, *exists = NULL;
4903 int ret;
4904
4905 eb = find_extent_buffer(fs_info, start);
4906 if (eb)
4907 return eb;
David Sterba3f556f72014-06-15 03:20:26 +02004908 eb = alloc_dummy_extent_buffer(fs_info, start);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004909 if (!eb)
4910 return NULL;
4911 eb->fs_info = fs_info;
4912again:
4913 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4914 if (ret)
4915 goto free_eb;
4916 spin_lock(&fs_info->buffer_lock);
4917 ret = radix_tree_insert(&fs_info->buffer_radix,
4918 start >> PAGE_CACHE_SHIFT, eb);
4919 spin_unlock(&fs_info->buffer_lock);
4920 radix_tree_preload_end();
4921 if (ret == -EEXIST) {
4922 exists = find_extent_buffer(fs_info, start);
4923 if (exists)
4924 goto free_eb;
4925 else
4926 goto again;
4927 }
4928 check_buffer_tree_ref(eb);
4929 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
4930
4931 /*
4932 * We will free dummy extent buffer's if they come into
4933 * free_extent_buffer with a ref count of 2, but if we are using this we
4934 * want the buffers to stay in memory until we're done with them, so
4935 * bump the ref count again.
4936 */
4937 atomic_inc(&eb->refs);
4938 return eb;
4939free_eb:
4940 btrfs_release_extent_buffer(eb);
4941 return exists;
4942}
4943#endif
4944
Josef Bacikf28491e2013-12-16 13:24:27 -05004945struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
David Sterbace3e6982014-06-15 03:00:04 +02004946 u64 start)
Chris Masond1310b22008-01-24 16:13:08 -05004947{
David Sterbace3e6982014-06-15 03:00:04 +02004948 unsigned long len = fs_info->tree_root->nodesize;
Chris Masond1310b22008-01-24 16:13:08 -05004949 unsigned long num_pages = num_extent_pages(start, len);
4950 unsigned long i;
4951 unsigned long index = start >> PAGE_CACHE_SHIFT;
4952 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004953 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004954 struct page *p;
Josef Bacikf28491e2013-12-16 13:24:27 -05004955 struct address_space *mapping = fs_info->btree_inode->i_mapping;
Chris Masond1310b22008-01-24 16:13:08 -05004956 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004957 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004958
Josef Bacikf28491e2013-12-16 13:24:27 -05004959 eb = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004960 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004961 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004962
David Sterba23d79d82014-06-15 02:55:29 +02004963 eb = __alloc_extent_buffer(fs_info, start, len);
Peter2b114d12008-04-01 11:21:40 -04004964 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004965 return NULL;
4966
Chris Mason727011e2010-08-06 13:21:20 -04004967 for (i = 0; i < num_pages; i++, index++) {
Michal Hockod1b5c562015-08-19 14:17:40 +02004968 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
Josef Bacik4804b382012-10-05 16:43:45 -04004969 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004970 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004971
4972 spin_lock(&mapping->private_lock);
4973 if (PagePrivate(p)) {
4974 /*
4975 * We could have already allocated an eb for this page
4976 * and attached one so lets see if we can get a ref on
4977 * the existing eb, and if we can we know it's good and
4978 * we can just return that one, else we know we can just
4979 * overwrite page->private.
4980 */
4981 exists = (struct extent_buffer *)p->private;
4982 if (atomic_inc_not_zero(&exists->refs)) {
4983 spin_unlock(&mapping->private_lock);
4984 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004985 page_cache_release(p);
Mel Gorman2457aec2014-06-04 16:10:31 -07004986 mark_extent_buffer_accessed(exists, p);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004987 goto free_eb;
4988 }
Omar Sandoval5ca64f42015-02-24 02:47:05 -08004989 exists = NULL;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004990
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004991 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004992 * Do this so attach doesn't complain and we need to
4993 * drop the ref the old guy had.
4994 */
4995 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004996 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004997 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004998 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004999 attach_extent_buffer_page(eb, p);
5000 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005001 WARN_ON(PageDirty(p));
Chris Mason727011e2010-08-06 13:21:20 -04005002 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05005003 if (!PageUptodate(p))
5004 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05005005
5006 /*
5007 * see below about how we avoid a nasty race with release page
5008 * and why we unlock later
5009 */
Chris Masond1310b22008-01-24 16:13:08 -05005010 }
5011 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005012 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05005013again:
Miao Xie19fe0a82010-10-26 20:57:29 -04005014 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
5015 if (ret)
5016 goto free_eb;
5017
Josef Bacikf28491e2013-12-16 13:24:27 -05005018 spin_lock(&fs_info->buffer_lock);
5019 ret = radix_tree_insert(&fs_info->buffer_radix,
5020 start >> PAGE_CACHE_SHIFT, eb);
5021 spin_unlock(&fs_info->buffer_lock);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005022 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04005023 if (ret == -EEXIST) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005024 exists = find_extent_buffer(fs_info, start);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05005025 if (exists)
5026 goto free_eb;
5027 else
Josef Bacik115391d2012-03-09 09:51:43 -05005028 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04005029 }
Chris Mason6af118ce2008-07-22 11:18:07 -04005030 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005031 check_buffer_tree_ref(eb);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005032 set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
Chris Masoneb14ab82011-02-10 12:35:00 -05005033
5034 /*
5035 * there is a race where release page may have
5036 * tried to find this extent buffer in the radix
5037 * but failed. It will tell the VM it is safe to
5038 * reclaim the, and it will clear the page private bit.
5039 * We must make sure to set the page private bit properly
5040 * after the extent buffer is in the radix tree so
5041 * it doesn't get lost
5042 */
Chris Mason727011e2010-08-06 13:21:20 -04005043 SetPageChecked(eb->pages[0]);
5044 for (i = 1; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005045 p = eb->pages[i];
Chris Mason727011e2010-08-06 13:21:20 -04005046 ClearPageChecked(p);
5047 unlock_page(p);
5048 }
5049 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05005050 return eb;
5051
Chris Mason6af118ce2008-07-22 11:18:07 -04005052free_eb:
Omar Sandoval5ca64f42015-02-24 02:47:05 -08005053 WARN_ON(!atomic_dec_and_test(&eb->refs));
Chris Mason727011e2010-08-06 13:21:20 -04005054 for (i = 0; i < num_pages; i++) {
5055 if (eb->pages[i])
5056 unlock_page(eb->pages[i]);
5057 }
Chris Masoneb14ab82011-02-10 12:35:00 -05005058
Miao Xie897ca6e2010-10-26 20:57:29 -04005059 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005060 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05005061}
Chris Masond1310b22008-01-24 16:13:08 -05005062
Josef Bacik3083ee22012-03-09 16:01:49 -05005063static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
5064{
5065 struct extent_buffer *eb =
5066 container_of(head, struct extent_buffer, rcu_head);
5067
5068 __free_extent_buffer(eb);
5069}
5070
Josef Bacik3083ee22012-03-09 16:01:49 -05005071/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00005072static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05005073{
5074 WARN_ON(atomic_read(&eb->refs) == 0);
5075 if (atomic_dec_and_test(&eb->refs)) {
Josef Bacik34b41ac2013-12-13 10:41:51 -05005076 if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
Josef Bacikf28491e2013-12-16 13:24:27 -05005077 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik3083ee22012-03-09 16:01:49 -05005078
Jan Schmidt815a51c2012-05-16 17:00:02 +02005079 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05005080
Josef Bacikf28491e2013-12-16 13:24:27 -05005081 spin_lock(&fs_info->buffer_lock);
5082 radix_tree_delete(&fs_info->buffer_radix,
Jan Schmidt815a51c2012-05-16 17:00:02 +02005083 eb->start >> PAGE_CACHE_SHIFT);
Josef Bacikf28491e2013-12-16 13:24:27 -05005084 spin_unlock(&fs_info->buffer_lock);
Josef Bacik34b41ac2013-12-13 10:41:51 -05005085 } else {
5086 spin_unlock(&eb->refs_lock);
Jan Schmidt815a51c2012-05-16 17:00:02 +02005087 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005088
5089 /* Should be safe to release our pages at this point */
David Sterbaa50924e2014-07-31 00:51:36 +02005090 btrfs_release_extent_buffer_page(eb);
Josef Bacikbcb7e442015-03-16 17:38:02 -04005091#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
5092 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
5093 __free_extent_buffer(eb);
5094 return 1;
5095 }
5096#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05005097 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04005098 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05005099 }
5100 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04005101
5102 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05005103}
5104
Chris Masond1310b22008-01-24 16:13:08 -05005105void free_extent_buffer(struct extent_buffer *eb)
5106{
Chris Mason242e18c2013-01-29 17:49:37 -05005107 int refs;
5108 int old;
Chris Masond1310b22008-01-24 16:13:08 -05005109 if (!eb)
5110 return;
5111
Chris Mason242e18c2013-01-29 17:49:37 -05005112 while (1) {
5113 refs = atomic_read(&eb->refs);
5114 if (refs <= 3)
5115 break;
5116 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
5117 if (old == refs)
5118 return;
5119 }
5120
Josef Bacik3083ee22012-03-09 16:01:49 -05005121 spin_lock(&eb->refs_lock);
5122 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02005123 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
5124 atomic_dec(&eb->refs);
5125
5126 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005127 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005128 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005129 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5130 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05005131
Josef Bacik3083ee22012-03-09 16:01:49 -05005132 /*
5133 * I know this is terrible, but it's temporary until we stop tracking
5134 * the uptodate bits and such for the extent buffers.
5135 */
David Sterbaf7a52a42013-04-26 14:56:29 +00005136 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005137}
Chris Masond1310b22008-01-24 16:13:08 -05005138
Josef Bacik3083ee22012-03-09 16:01:49 -05005139void free_extent_buffer_stale(struct extent_buffer *eb)
5140{
5141 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05005142 return;
5143
Josef Bacik3083ee22012-03-09 16:01:49 -05005144 spin_lock(&eb->refs_lock);
5145 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
5146
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005147 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05005148 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
5149 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00005150 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05005151}
5152
Chris Mason1d4284b2012-03-28 20:31:37 -04005153void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005154{
Chris Masond1310b22008-01-24 16:13:08 -05005155 unsigned long i;
5156 unsigned long num_pages;
5157 struct page *page;
5158
Chris Masond1310b22008-01-24 16:13:08 -05005159 num_pages = num_extent_pages(eb->start, eb->len);
5160
5161 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005162 page = eb->pages[i];
Chris Masonb9473432009-03-13 11:00:37 -04005163 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05005164 continue;
5165
Chris Masona61e6f22008-07-22 11:18:08 -04005166 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05005167 WARN_ON(!PagePrivate(page));
5168
Chris Masond1310b22008-01-24 16:13:08 -05005169 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005170 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05005171 if (!PageDirty(page)) {
5172 radix_tree_tag_clear(&page->mapping->page_tree,
5173 page_index(page),
5174 PAGECACHE_TAG_DIRTY);
5175 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005176 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04005177 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04005178 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05005179 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005180 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05005181}
Chris Masond1310b22008-01-24 16:13:08 -05005182
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005183int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005184{
5185 unsigned long i;
5186 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04005187 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005188
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005189 check_buffer_tree_ref(eb);
5190
Chris Masonb9473432009-03-13 11:00:37 -04005191 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005192
Chris Masond1310b22008-01-24 16:13:08 -05005193 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05005194 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005195 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
5196
Chris Masonb9473432009-03-13 11:00:37 -04005197 for (i = 0; i < num_pages; i++)
David Sterbafb85fc92014-07-31 01:03:53 +02005198 set_page_dirty(eb->pages[i]);
Chris Masonb9473432009-03-13 11:00:37 -04005199 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05005200}
Chris Masond1310b22008-01-24 16:13:08 -05005201
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005202int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04005203{
5204 unsigned long i;
5205 struct page *page;
5206 unsigned long num_pages;
5207
Chris Masonb4ce94d2009-02-04 09:25:08 -05005208 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005209 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04005210 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005211 page = eb->pages[i];
Chris Mason33958dc2008-07-30 10:29:12 -04005212 if (page)
5213 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04005214 }
5215 return 0;
5216}
5217
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005218int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005219{
5220 unsigned long i;
5221 struct page *page;
5222 unsigned long num_pages;
5223
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005224 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005225 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05005226 for (i = 0; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005227 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005228 SetPageUptodate(page);
5229 }
5230 return 0;
5231}
Chris Masond1310b22008-01-24 16:13:08 -05005232
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005233int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05005234{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005235 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05005236}
Chris Masond1310b22008-01-24 16:13:08 -05005237
5238int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02005239 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04005240 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05005241{
5242 unsigned long i;
5243 unsigned long start_i;
5244 struct page *page;
5245 int err;
5246 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04005247 int locked_pages = 0;
5248 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05005249 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04005250 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005251 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04005252 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05005253
Chris Masonb4ce94d2009-02-04 09:25:08 -05005254 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05005255 return 0;
5256
Chris Masond1310b22008-01-24 16:13:08 -05005257 if (start) {
5258 WARN_ON(start < eb->start);
5259 start_i = (start >> PAGE_CACHE_SHIFT) -
5260 (eb->start >> PAGE_CACHE_SHIFT);
5261 } else {
5262 start_i = 0;
5263 }
5264
5265 num_pages = num_extent_pages(eb->start, eb->len);
5266 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005267 page = eb->pages[i];
Arne Jansenbb82ab82011-06-10 14:06:53 +02005268 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04005269 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04005270 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05005271 } else {
5272 lock_page(page);
5273 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005274 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04005275 if (!PageUptodate(page)) {
5276 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04005277 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04005278 }
Chris Masonce9adaa2008-04-09 16:28:12 -04005279 }
5280 if (all_uptodate) {
5281 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05005282 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04005283 goto unlock_exit;
5284 }
5285
Filipe Manana656f30d2014-09-26 12:25:56 +01005286 clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04005287 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005288 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04005289 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005290 page = eb->pages[i];
Chris Masonce9adaa2008-04-09 16:28:12 -04005291 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04005292 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05005293 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04005294 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005295 mirror_num, &bio_flags,
5296 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05005297 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05005298 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05005299 } else {
5300 unlock_page(page);
5301 }
5302 }
5303
Jeff Mahoney355808c2011-10-03 23:23:14 -04005304 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04005305 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
5306 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005307 if (err)
5308 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04005309 }
Chris Masona86c12c2008-02-07 10:50:54 -05005310
Arne Jansenbb82ab82011-06-10 14:06:53 +02005311 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05005312 return ret;
Chris Masond3977122009-01-05 21:25:51 -05005313
Chris Masond1310b22008-01-24 16:13:08 -05005314 for (i = start_i; i < num_pages; i++) {
David Sterbafb85fc92014-07-31 01:03:53 +02005315 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005316 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05005317 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05005318 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05005319 }
Chris Masond3977122009-01-05 21:25:51 -05005320
Chris Masond1310b22008-01-24 16:13:08 -05005321 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04005322
5323unlock_exit:
5324 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05005325 while (locked_pages > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005326 page = eb->pages[i];
Chris Masonce9adaa2008-04-09 16:28:12 -04005327 i++;
5328 unlock_page(page);
5329 locked_pages--;
5330 }
5331 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05005332}
Chris Masond1310b22008-01-24 16:13:08 -05005333
5334void read_extent_buffer(struct extent_buffer *eb, void *dstv,
5335 unsigned long start,
5336 unsigned long len)
5337{
5338 size_t cur;
5339 size_t offset;
5340 struct page *page;
5341 char *kaddr;
5342 char *dst = (char *)dstv;
5343 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5344 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05005345
5346 WARN_ON(start > eb->len);
5347 WARN_ON(start + len > eb->start + eb->len);
5348
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005349 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005350
Chris Masond3977122009-01-05 21:25:51 -05005351 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005352 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005353
5354 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04005355 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005356 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005357
5358 dst += cur;
5359 len -= cur;
5360 offset = 0;
5361 i++;
5362 }
5363}
Chris Masond1310b22008-01-24 16:13:08 -05005364
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005365int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
5366 unsigned long start,
5367 unsigned long len)
5368{
5369 size_t cur;
5370 size_t offset;
5371 struct page *page;
5372 char *kaddr;
5373 char __user *dst = (char __user *)dstv;
5374 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5375 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5376 int ret = 0;
5377
5378 WARN_ON(start > eb->len);
5379 WARN_ON(start + len > eb->start + eb->len);
5380
5381 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
5382
5383 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005384 page = eb->pages[i];
Gerhard Heift550ac1d2014-01-30 16:24:01 +01005385
5386 cur = min(len, (PAGE_CACHE_SIZE - offset));
5387 kaddr = page_address(page);
5388 if (copy_to_user(dst, kaddr + offset, cur)) {
5389 ret = -EFAULT;
5390 break;
5391 }
5392
5393 dst += cur;
5394 len -= cur;
5395 offset = 0;
5396 i++;
5397 }
5398
5399 return ret;
5400}
5401
Chris Masond1310b22008-01-24 16:13:08 -05005402int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04005403 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05005404 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04005405 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05005406{
5407 size_t offset = start & (PAGE_CACHE_SIZE - 1);
5408 char *kaddr;
5409 struct page *p;
5410 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5411 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5412 unsigned long end_i = (start_offset + start + min_len - 1) >>
5413 PAGE_CACHE_SHIFT;
5414
5415 if (i != end_i)
5416 return -EINVAL;
5417
5418 if (i == 0) {
5419 offset = start_offset;
5420 *map_start = 0;
5421 } else {
5422 offset = 0;
5423 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
5424 }
Chris Masond3977122009-01-05 21:25:51 -05005425
Chris Masond1310b22008-01-24 16:13:08 -05005426 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005427 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005428 "wanted %lu %lu\n",
5429 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04005430 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05005431 }
5432
David Sterbafb85fc92014-07-31 01:03:53 +02005433 p = eb->pages[i];
Chris Masona6591712011-07-19 12:04:14 -04005434 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05005435 *map = kaddr + offset;
5436 *map_len = PAGE_CACHE_SIZE - offset;
5437 return 0;
5438}
Chris Masond1310b22008-01-24 16:13:08 -05005439
Chris Masond1310b22008-01-24 16:13:08 -05005440int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
5441 unsigned long start,
5442 unsigned long len)
5443{
5444 size_t cur;
5445 size_t offset;
5446 struct page *page;
5447 char *kaddr;
5448 char *ptr = (char *)ptrv;
5449 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5450 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5451 int ret = 0;
5452
5453 WARN_ON(start > eb->len);
5454 WARN_ON(start + len > eb->start + eb->len);
5455
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005456 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005457
Chris Masond3977122009-01-05 21:25:51 -05005458 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005459 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005460
5461 cur = min(len, (PAGE_CACHE_SIZE - offset));
5462
Chris Masona6591712011-07-19 12:04:14 -04005463 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005464 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005465 if (ret)
5466 break;
5467
5468 ptr += cur;
5469 len -= cur;
5470 offset = 0;
5471 i++;
5472 }
5473 return ret;
5474}
Chris Masond1310b22008-01-24 16:13:08 -05005475
5476void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5477 unsigned long start, unsigned long len)
5478{
5479 size_t cur;
5480 size_t offset;
5481 struct page *page;
5482 char *kaddr;
5483 char *src = (char *)srcv;
5484 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5485 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5486
5487 WARN_ON(start > eb->len);
5488 WARN_ON(start + len > eb->start + eb->len);
5489
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005490 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005491
Chris Masond3977122009-01-05 21:25:51 -05005492 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005493 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005494 WARN_ON(!PageUptodate(page));
5495
5496 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005497 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005498 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005499
5500 src += cur;
5501 len -= cur;
5502 offset = 0;
5503 i++;
5504 }
5505}
Chris Masond1310b22008-01-24 16:13:08 -05005506
5507void memset_extent_buffer(struct extent_buffer *eb, char c,
5508 unsigned long start, unsigned long len)
5509{
5510 size_t cur;
5511 size_t offset;
5512 struct page *page;
5513 char *kaddr;
5514 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5515 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5516
5517 WARN_ON(start > eb->len);
5518 WARN_ON(start + len > eb->start + eb->len);
5519
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005520 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005521
Chris Masond3977122009-01-05 21:25:51 -05005522 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005523 page = eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005524 WARN_ON(!PageUptodate(page));
5525
5526 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005527 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005528 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005529
5530 len -= cur;
5531 offset = 0;
5532 i++;
5533 }
5534}
Chris Masond1310b22008-01-24 16:13:08 -05005535
5536void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5537 unsigned long dst_offset, unsigned long src_offset,
5538 unsigned long len)
5539{
5540 u64 dst_len = dst->len;
5541 size_t cur;
5542 size_t offset;
5543 struct page *page;
5544 char *kaddr;
5545 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5546 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5547
5548 WARN_ON(src->len != dst_len);
5549
5550 offset = (start_offset + dst_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005551 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005552
Chris Masond3977122009-01-05 21:25:51 -05005553 while (len > 0) {
David Sterbafb85fc92014-07-31 01:03:53 +02005554 page = dst->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05005555 WARN_ON(!PageUptodate(page));
5556
5557 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
5558
Chris Masona6591712011-07-19 12:04:14 -04005559 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005560 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005561
5562 src_offset += cur;
5563 len -= cur;
5564 offset = 0;
5565 i++;
5566 }
5567}
Chris Masond1310b22008-01-24 16:13:08 -05005568
Sergei Trofimovich33872062011-04-11 21:52:52 +00005569static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5570{
5571 unsigned long distance = (src > dst) ? src - dst : dst - src;
5572 return distance < len;
5573}
5574
Chris Masond1310b22008-01-24 16:13:08 -05005575static void copy_pages(struct page *dst_page, struct page *src_page,
5576 unsigned long dst_off, unsigned long src_off,
5577 unsigned long len)
5578{
Chris Masona6591712011-07-19 12:04:14 -04005579 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005580 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005581 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005582
Sergei Trofimovich33872062011-04-11 21:52:52 +00005583 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005584 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005585 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005586 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005587 if (areas_overlap(src_off, dst_off, len))
5588 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005589 }
Chris Masond1310b22008-01-24 16:13:08 -05005590
Chris Mason727011e2010-08-06 13:21:20 -04005591 if (must_memmove)
5592 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5593 else
5594 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005595}
5596
5597void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5598 unsigned long src_offset, unsigned long len)
5599{
5600 size_t cur;
5601 size_t dst_off_in_page;
5602 size_t src_off_in_page;
5603 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5604 unsigned long dst_i;
5605 unsigned long src_i;
5606
5607 if (src_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005608 btrfs_err(dst->fs_info,
5609 "memmove bogus src_offset %lu move "
5610 "len %lu dst len %lu", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005611 BUG_ON(1);
5612 }
5613 if (dst_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005614 btrfs_err(dst->fs_info,
5615 "memmove bogus dst_offset %lu move "
5616 "len %lu dst len %lu", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005617 BUG_ON(1);
5618 }
5619
Chris Masond3977122009-01-05 21:25:51 -05005620 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005621 dst_off_in_page = (start_offset + dst_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005622 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005623 src_off_in_page = (start_offset + src_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005624 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005625
5626 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5627 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5628
5629 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5630 src_off_in_page));
5631 cur = min_t(unsigned long, cur,
5632 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5633
David Sterbafb85fc92014-07-31 01:03:53 +02005634 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005635 dst_off_in_page, src_off_in_page, cur);
5636
5637 src_offset += cur;
5638 dst_offset += cur;
5639 len -= cur;
5640 }
5641}
Chris Masond1310b22008-01-24 16:13:08 -05005642
5643void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5644 unsigned long src_offset, unsigned long len)
5645{
5646 size_t cur;
5647 size_t dst_off_in_page;
5648 size_t src_off_in_page;
5649 unsigned long dst_end = dst_offset + len - 1;
5650 unsigned long src_end = src_offset + len - 1;
5651 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5652 unsigned long dst_i;
5653 unsigned long src_i;
5654
5655 if (src_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005656 btrfs_err(dst->fs_info, "memmove bogus src_offset %lu move "
5657 "len %lu len %lu", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005658 BUG_ON(1);
5659 }
5660 if (dst_offset + len > dst->len) {
David Sterbaf14d1042015-10-08 11:37:06 +02005661 btrfs_err(dst->fs_info, "memmove bogus dst_offset %lu move "
5662 "len %lu len %lu", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005663 BUG_ON(1);
5664 }
Chris Mason727011e2010-08-06 13:21:20 -04005665 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005666 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5667 return;
5668 }
Chris Masond3977122009-01-05 21:25:51 -05005669 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005670 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5671 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5672
5673 dst_off_in_page = (start_offset + dst_end) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005674 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005675 src_off_in_page = (start_offset + src_end) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005676 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005677
5678 cur = min_t(unsigned long, len, src_off_in_page + 1);
5679 cur = min(cur, dst_off_in_page + 1);
David Sterbafb85fc92014-07-31 01:03:53 +02005680 copy_pages(dst->pages[dst_i], dst->pages[src_i],
Chris Masond1310b22008-01-24 16:13:08 -05005681 dst_off_in_page - cur + 1,
5682 src_off_in_page - cur + 1, cur);
5683
5684 dst_end -= cur;
5685 src_end -= cur;
5686 len -= cur;
5687 }
5688}
Chris Mason6af118ce2008-07-22 11:18:07 -04005689
David Sterbaf7a52a42013-04-26 14:56:29 +00005690int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005691{
Chris Mason6af118ce2008-07-22 11:18:07 -04005692 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005693
Miao Xie19fe0a82010-10-26 20:57:29 -04005694 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005695 * We need to make sure noboody is attaching this page to an eb right
5696 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005697 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005698 spin_lock(&page->mapping->private_lock);
5699 if (!PagePrivate(page)) {
5700 spin_unlock(&page->mapping->private_lock);
5701 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005702 }
5703
Josef Bacik3083ee22012-03-09 16:01:49 -05005704 eb = (struct extent_buffer *)page->private;
5705 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005706
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005707 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005708 * This is a little awful but should be ok, we need to make sure that
5709 * the eb doesn't disappear out from under us while we're looking at
5710 * this page.
5711 */
5712 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005713 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005714 spin_unlock(&eb->refs_lock);
5715 spin_unlock(&page->mapping->private_lock);
5716 return 0;
5717 }
5718 spin_unlock(&page->mapping->private_lock);
5719
Josef Bacik3083ee22012-03-09 16:01:49 -05005720 /*
5721 * If tree ref isn't set then we know the ref on this eb is a real ref,
5722 * so just return, this page will likely be freed soon anyway.
5723 */
5724 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5725 spin_unlock(&eb->refs_lock);
5726 return 0;
5727 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005728
David Sterbaf7a52a42013-04-26 14:56:29 +00005729 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005730}